Ticket #1838: enca.2.patch
File enca.2.patch, 8.1 KB (added by ASM, 15 years ago) |
---|
-
src/ext.c
diff --git a/src/ext.c b/src/ext.c index 779e5a2..233ae08 100644
a b exec_extension (const char *filename, const char *lc_data, int *move_dir, 301 301 #endif 302 302 303 303 /* 304 * Run the "file" command on the local file. 305 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors. 306 */ 304 * Run cmd_file with args, put in buf[buflen] result. 305 * If error, put in buf[0] = '\0'; 306 * 307 * NOTES: buf is string, always end '\0' char. 308 * */ 309 307 310 static int 308 get_ file_type_local (const char *filename,char *buf, int buflen)311 get_popen_information(const char *cmd_file, const char *args, char *buf, int buflen) 309 312 { 310 313 int read_bytes = 0; 311 314 312 char *tmp = name_quote (filename, 0); 313 char *command = g_strconcat (FILE_CMD, tmp, " 2>/dev/null", (char *) 0); 315 char *command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) 0); 314 316 FILE *f = popen (command, "r"); 315 317 316 g_free (tmp);317 318 g_free (command); 318 319 if (f != NULL) { 319 320 #ifdef __QNXNTO__ … … get_file_type_local (const char *filename, char *buf, int buflen) 325 326 read_bytes = (fgets (buf, buflen, f) 326 327 != NULL); 327 328 if (read_bytes == 0) 329 /* if(buflen > 0) // ;-) */ 328 330 buf[0] = 0; 329 331 pclose (f); 330 332 } else { 333 /* if(buflen > 0) // ;-) */ 334 buf[0] = 0; /* Paranoid termination */ 331 335 return -1; 332 336 } 337 /* Paranoid termination */ 338 buf[buflen - 1] = 0; 333 339 334 340 return (read_bytes > 0); 335 341 } 336 342 343 /* 344 * Run the "file" command on the local file. 345 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors. 346 */ 347 static int 348 get_file_type_local (const char *filename, char *buf, int buflen) 349 { 350 int read_bytes = 0; 351 char *tmp = name_quote (filename, 0); 352 353 return get_popen_information(FILE_CMD, tmp, buf, buflen); 354 } 355 356 /* 357 * Run the "enca" command on the local file. 358 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors. 359 */ 360 static int 361 get_file_encoding_local (const char *filename, char *buf, int buflen) 362 { 363 if(is_autodetect_codeset_enabled == TRUE) { 364 int read_bytes = 0; 365 char *tmp = name_quote (filename, 0); 366 char *lang = name_quote (autodetect_codeset, 0); 367 char *args= g_strconcat (" -L", lang, " -i ", tmp, (char *) 0); 368 int ret = get_popen_information("enca", args, buf, buflen); 369 g_free (args); 370 g_free (lang); 371 g_free (tmp); 372 return ret; 373 } 374 return 0; 375 } 376 337 377 338 378 /* 339 379 * Invoke the "file" command on the file and match its output against PTR. … … regex_check_type (const char *filename, const char *ptr, int *have_type) 348 388 349 389 /* Following variables are valid if *have_type is 1 */ 350 390 static char content_string[2048]; 391 static char encoding_id[21]; /* example: CSISO51INISCYRILLIC -- 20 */ 392 static int got_encoding_data = 0; 351 393 static int content_shift = 0; 352 394 static int got_data = 0; 353 395 … … regex_check_type (const char *filename, const char *ptr, int *have_type) 366 408 return -1; 367 409 368 410 realname = localfile; 369 got_data = 370 get_file_type_local (localfile, content_string, 371 sizeof (content_string)); 411 412 got_encoding_data = 413 get_file_encoding_local (localfile, encoding_id, 414 sizeof (encoding_id)); 372 415 mc_ungetlocalcopy (filename, localfile, 0); 373 416 374 if (got_data > 0) { 417 if( got_encoding_data > 0 ) 418 { 375 419 char *pp; 420 if ((pp = strchr (encoding_id, '\n')) != 0) 421 *pp = 0; 376 422 377 /* Paranoid termination */ 378 content_string[sizeof (content_string) - 1] = 0; 423 source_codepage = get_codepage_index( encoding_id ); 424 if(source_codepage == -1) 425 source_codepage = default_source_codepage; 426 } 379 427 380 if ((pp = strchr (content_string, '\n')) != 0)381 *pp = 0;382 428 383 if (!strncmp (content_string, realname, strlen (realname))) { 384 /* Skip "realname: " */ 385 content_shift = strlen (realname); 386 if (content_string[content_shift] == ':') { 387 /* Solaris' file prints tab(s) after ':' */ 388 for (content_shift++; 389 content_string[content_shift] == ' ' 390 || content_string[content_shift] == '\t'; 391 content_shift++); 392 } 393 } 394 } else { 395 /* No data */ 396 content_string[0] = 0; 429 got_data = 430 get_file_type_local (localfile, content_string, 431 sizeof (content_string)); 432 433 if (got_data > 0) { 434 char *pp; 435 436 if ((pp = strchr (content_string, '\n')) != 0) 437 *pp = 0; 438 439 if (!strncmp (content_string, realname, strlen (realname))) { 440 /* Skip "realname: " */ 441 content_shift = strlen (realname); 442 if (content_string[content_shift] == ':') { 443 /* Solaris' file prints tab(s) after ':' */ 444 for (content_shift++; 445 content_string[content_shift] == ' ' 446 || content_string[content_shift] == '\t'; 447 content_shift++); 397 448 } 449 } 450 } else { 451 /* No data */ 452 content_string[0] = 0; 453 } 398 454 g_free (realname); 399 455 } 400 456 -
src/main.c
diff --git a/src/main.c b/src/main.c index 3ce65db..1eda583 100644
a b main (int argc, char *argv[]) 2257 2257 done_key (); 2258 2258 #ifdef HAVE_CHARSET 2259 2259 free_codepages_list (); 2260 g_free(autodetect_codeset); 2260 2261 #endif 2261 2262 str_uninit_strings (); 2262 2263 -
src/main.h
diff --git a/src/main.h b/src/main.h index 9c843fa..b5aecd8 100644
a b extern int option_tab_spacing; 44 44 45 45 #ifdef HAVE_CHARSET 46 46 extern int source_codepage; 47 extern int default_source_codepage; 47 48 extern int display_codepage; 49 extern char* autodetect_codeset; 50 gboolean is_autodetect_codeset_enabled; 48 51 #else 49 52 extern int eight_bit_clean; 50 53 extern int full_eight_bits; -
src/selcodepage.c
diff --git a/src/selcodepage.c b/src/selcodepage.c index a3be1a1..9d7acb5 100644
a b 40 40 41 41 /* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */ 42 42 int source_codepage = -1; 43 int default_source_codepage = -1; 43 44 int display_codepage = -1; 45 char* autodetect_codeset = 0; 46 gboolean is_autodetect_codeset_enabled=FALSE; 44 47 45 48 static unsigned char 46 49 get_hotkey (int n) … … do_select_codepage (void) 119 122 return FALSE; 120 123 121 124 source_codepage = r; 125 default_source_codepage = source_codepage; 122 126 123 127 errmsg = init_translation_table (r == SELECT_CHARSET_NO_TRANSLATE ? 124 128 display_codepage : source_codepage, -
src/setup.c
diff --git a/src/setup.c b/src/setup.c index 3f7adca..413c8c0 100644
a b save_setup (void) 387 387 mc_config_set_string(mc_main_config, "Misc" , "display_codepage", 388 388 get_codepage_id( display_codepage )); 389 389 mc_config_set_string(mc_main_config, "Misc" , "source_codepage", 390 get_codepage_id( source_codepage )); 390 get_codepage_id( default_source_codepage )); 391 mc_config_set_string(mc_main_config, "Misc" , "autodetect_codeset", 392 autodetect_codeset ); 391 393 #endif /* HAVE_CHARSET */ 392 394 tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); 393 395 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL); … … load_setup (void) 813 815 buffer = mc_config_get_string(mc_main_config, "Misc", "source_codepage", ""); 814 816 if ( buffer[0] != '\0' ) 815 817 { 816 source_codepage = get_codepage_index( buffer ); 818 default_source_codepage = get_codepage_index( buffer ); 819 source_codepage = default_source_codepage; /* Mabye source_codepage don't needed this */ 817 820 cp_source = get_codepage_id (source_codepage); 818 821 } 819 822 g_free(buffer); 820 } 823 } 824 825 autodetect_codeset = mc_config_get_string(mc_main_config, "Misc", "autodetect_codeset", ""); 826 if ( (autodetect_codeset[0] != '\0') && ( strcmp(autodetect_codeset, "off") ) ) 827 is_autodetect_codeset_enabled=TRUE; 828 821 829 init_translation_table( source_codepage, display_codepage ); 822 830 if ( get_codepage_id( display_codepage ) ) 823 831 utf8_display = str_isutf8 (get_codepage_id( display_codepage )); -
src/viewer/display.c
diff --git a/src/viewer/display.c b/src/viewer/display.c index 8bf14ef..7c20e80 100644
a b mcview_display_status (mcview_t * view) 174 174 tty_printf (_(">= %s bytes"), size_trunc (filesize)); 175 175 } 176 176 } 177 if (width > 62 + 11){ 178 char* cp = get_codepage_id (source_codepage); 179 if(cp) 180 tty_printf (" %s", cp); 181 } 177 182 if (width > 26) { 178 183 mcview_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end); 179 184 }