Ticket #1838: enca.2.patch

File enca.2.patch, 8.1 KB (added by ASM, 14 years ago)

Fixed version.

  • 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, 
    301301#endif 
    302302 
    303303/* 
    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 
    307310static int 
    308 get_file_type_local (const char *filename, char *buf, int buflen) 
     311get_popen_information(const char *cmd_file, const char *args, char *buf, int buflen) 
    309312{ 
    310313    int read_bytes = 0; 
    311314 
    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); 
    314316    FILE *f = popen (command, "r"); 
    315317 
    316     g_free (tmp); 
    317318    g_free (command); 
    318319    if (f != NULL) { 
    319320#ifdef __QNXNTO__ 
    get_file_type_local (const char *filename, char *buf, int buflen) 
    325326        read_bytes = (fgets (buf, buflen, f) 
    326327                      != NULL); 
    327328        if (read_bytes == 0) 
     329/* if(buflen > 0) // ;-) */ 
    328330            buf[0] = 0; 
    329331        pclose (f); 
    330332    } else { 
     333/* if(buflen > 0) // ;-) */ 
     334        buf[0] = 0; /* Paranoid termination */ 
    331335        return -1; 
    332336    } 
     337    /* Paranoid termination */ 
     338    buf[buflen - 1] = 0; 
    333339 
    334340    return (read_bytes > 0); 
    335341} 
    336342 
     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 */ 
     347static int 
     348get_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 */ 
     360static int 
     361get_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 
    337377 
    338378/* 
    339379 * 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) 
    348388 
    349389    /* Following variables are valid if *have_type is 1 */ 
    350390    static char content_string[2048]; 
     391    static char encoding_id[21]; /* example: CSISO51INISCYRILLIC -- 20 */ 
     392    static int got_encoding_data = 0; 
    351393    static int content_shift = 0; 
    352394    static int got_data = 0; 
    353395 
    regex_check_type (const char *filename, const char *ptr, int *have_type) 
    366408            return -1; 
    367409 
    368410        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)); 
    372415        mc_ungetlocalcopy (filename, localfile, 0); 
    373416 
    374         if (got_data > 0) { 
     417        if( got_encoding_data > 0 ) 
     418        { 
    375419            char *pp; 
     420            if ((pp = strchr (encoding_id, '\n')) != 0) 
     421                *pp = 0; 
    376422 
    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        } 
    379427 
    380             if ((pp = strchr (content_string, '\n')) != 0) 
    381                 *pp = 0; 
    382428 
    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++); 
    397448        } 
     449    } 
     450  } else { 
     451    /* No data */ 
     452    content_string[0] = 0; 
     453  } 
    398454        g_free (realname); 
    399455    } 
    400456 
  • src/main.c

    diff --git a/src/main.c b/src/main.c
    index 3ce65db..1eda583 100644
    a b main (int argc, char *argv[]) 
    22572257    done_key (); 
    22582258#ifdef HAVE_CHARSET 
    22592259    free_codepages_list (); 
     2260    g_free(autodetect_codeset); 
    22602261#endif 
    22612262    str_uninit_strings (); 
    22622263 
  • src/main.h

    diff --git a/src/main.h b/src/main.h
    index 9c843fa..b5aecd8 100644
    a b extern int option_tab_spacing; 
    4444 
    4545#ifdef HAVE_CHARSET 
    4646extern int source_codepage; 
     47extern int default_source_codepage; 
    4748extern int display_codepage; 
     49extern char* autodetect_codeset; 
     50gboolean is_autodetect_codeset_enabled; 
    4851#else 
    4952extern int eight_bit_clean; 
    5053extern int full_eight_bits; 
  • src/selcodepage.c

    diff --git a/src/selcodepage.c b/src/selcodepage.c
    index a3be1a1..9d7acb5 100644
    a b  
    4040 
    4141/* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */ 
    4242int source_codepage = -1; 
     43int default_source_codepage = -1; 
    4344int display_codepage = -1; 
     45char* autodetect_codeset = 0; 
     46gboolean is_autodetect_codeset_enabled=FALSE; 
    4447 
    4548static unsigned char 
    4649get_hotkey (int n) 
    do_select_codepage (void) 
    119122        return FALSE; 
    120123 
    121124    source_codepage = r; 
     125    default_source_codepage = source_codepage; 
    122126 
    123127    errmsg = init_translation_table (r == SELECT_CHARSET_NO_TRANSLATE ? 
    124128                                        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) 
    387387    mc_config_set_string(mc_main_config, "Misc" , "display_codepage", 
    388388                 get_codepage_id( display_codepage )); 
    389389    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 ); 
    391393#endif /* HAVE_CHARSET */ 
    392394    tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); 
    393395    ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL); 
    load_setup (void) 
    813815        buffer = mc_config_get_string(mc_main_config, "Misc", "source_codepage", ""); 
    814816        if ( buffer[0] != '\0' ) 
    815817        { 
    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 */ 
    817820            cp_source = get_codepage_id (source_codepage); 
    818821        } 
    819822        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 
    821829    init_translation_table( source_codepage, display_codepage ); 
    822830    if ( get_codepage_id( display_codepage ) ) 
    823831        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) 
    174174                tty_printf (_(">= %s bytes"), size_trunc (filesize)); 
    175175            } 
    176176        } 
     177        if (width > 62 + 11){ 
     178            char* cp = get_codepage_id (source_codepage); 
     179            if(cp) 
     180                tty_printf ("    %s", cp); 
     181        } 
    177182        if (width > 26) { 
    178183            mcview_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end); 
    179184        }