📄 2utf.c
字号:
if (strlen (paths[index]) > strlen (paths[index - 1])) length = strlen (paths[index]); pathname = xmalloc (length + strlen (charmap_name) + 5); if (Debug >= 9) fprintf (stderr, "2UTF: malloc (%i) \n", (int)(length + strlen (charmap_name) + 5)); for (index = 0; index < paths_number; index++) { strcat (strcpy (pathname, paths[index]), charmap_name); if (Debug >= 3) fprintf (stderr, "2UTF: Looking for '%s' \n", pathname); if ((charmap_file_p->stream = fopen (pathname, "rt")) != NULL) { charmap_file_p->format = Charmap_file_format_type (pathname); if (verbose) fprintf (stderr, "2UTF: %s '%s' \n", using, pathname); break; } } if (charmap_file_p->stream == NULL) { for (index = 0; index < paths_number; index++) { strcat (strcpy (pathname, paths[index]), "*"); strcat (strcat (pathname, charmap_name), "*"); if (Debug >= 3) fprintf (stderr, "2UTF: Looking for '%s' \n", pathname); /*c = glob (pathname, GLOB_NOSORT, NULL, &glob_buffer); */ c = glob (pathname, 0, NULL, &glob_buffer); switch (c) { case 0: if (glob_buffer.gl_pathc >= 1) { if (glob_buffer.gl_pathc >= 2) fprintf (stderr, "2UTF: %s '%s' (%i). \n", multiply_matches, pathname, glob_buffer.gl_pathc); if ((charmap_file_p->stream = fopen (glob_buffer.gl_pathv[0], "rt")) != NULL) { charmap_file_p->format = Charmap_file_format_type (glob_buffer.gl_pathv[0]); if (glob_buffer.gl_pathc != 1) fprintf (stderr, "2UTF: %s '%s' \n", using, glob_buffer.gl_pathv[0]); goto break_for; } else if (verbose) fprintf (stderr, "2UTF: %s '%s' \n", can_not_open, glob_buffer.gl_pathv[0]); } break; case GLOB_NOSPACE: Error (out_of_mem); default: if (Debug >= 5) fprintf (stderr, "2UTF: glob() returned %i \n", c); } }break_for: globfree (&glob_buffer); } free (pathname); free (charmap_name); if (!found_alias && verbose && charmap_file_p->stream != NULL) fprintf (stderr, "2UTF: %s \n", needs_update); } if (found_alias && charmap_file_p->stream == NULL) fprintf (stderr, "2UTF: %s \n", needs_update); if (((found_alias && charmap_file_p->stream == NULL) || (!found_alias && charmap_file_p->stream != NULL)) &&strcmp (charmap_format[0], default_charmap_format) == 0) if (!can_not_create_aliases_file) if (!Create_aliases ()) { if (verbose) fprintf (stderr, "2UTF: %s '%s' \n", can_not_create, aliases_pathname); } else if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL) Error (internal_err); else if (EOF == fclose (aliases_file)) Error (IO_err); } return (charmap_file_p->stream);}/* Bits Hex Min Hex Max Byte Sequence in Binary * 7 00000000 0000007f 0vvvvvvv * 11 00000080 000007FF 110vvvvv 10vvvvvv * 16 00000800 0000FFFF 1110vvvv 10vvvvvv 10vvvvvv * 21 00010000 001FFFFF 11110vvv 10vvvvvv 10vvvvvv 10vvvvvv * 26 00200000 03FFFFFF 111110vv 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv * 31 04000000 7FFFFFFF 1111110v 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv */ inline intFreadmb (wchar_t * wchar_p, FILE * stream){ char mb[6]; register int length, i, c; if ((c = getc (stream)) == EOF) return (EOF); else if ((c & 0x80) == 0) { *wchar_p = c; return (1); } else if ((c & 0x40) == 0) return (0); else if ((c & 0x20) == 0) length = 2; else if ((c & 0x10) == 0) length = 3; else if ((c & 0x8) == 0) length = 4; else if ((c & 0x4) == 0) length = 5; else if ((c & 0x2) == 0) length = 6; else return (0); mb[0] = c; for (i = 1; i < length; i++) { if ((c = getc (stream)) == EOF) return (EOF); else if ((c & 0xC0) != 0x80) /* !=10xx xxxx */ { ungetc (c, stream); return (0); } else { mb[i] = c; } } if (mbtowc (wchar_p, mb, length) != length) return (0); else return (1);}extern size_t Getdelim (unsigned char **linebuf_p, size_t *bufsize_p, int delimiter, FILE *stream){ size_t current_ind = 0; int c; do { if (*bufsize_p <= current_ind + 10) *linebuf_p = xrealloc (*linebuf_p, *bufsize_p += 256); if ((c = getc (stream)) == EOF || ((*linebuf_p)[current_ind++] = c) == delimiter) break; } while (TRUE); (*linebuf_p)[current_ind] = '\0'; return (current_ind ? current_ind : -1);}intGet_charmap (wchar_t * charmap, struct charmap_file_type *charmap_file_p, wchar_t unknown_wchar){ char s[max_string_length]; int index; if (charmap_file_p->stream == NULL) return (err_internal); for (index = 0; index < 256; index++) charmap[index] = unknown_wchar; for (index = 0; index < MAXINT; index++) { do if (fgets (s, max_string_length, charmap_file_p->stream) == NULL) if (feof (charmap_file_p->stream)) if (index < 20) return (err_few_chars); else if (index < 256) return (err_short); else if (index == 256) return (OK); else return (err_long); else return (err_IO); while (!Parse_string (charmap_format[charmap_file_p->format], charmap, s)); } return (err_internal);} voidHelp (unsigned char default_unknown_char){ fprintf (stderr, help, version, charmap_format[DEFAULT], default_unknown_char, (unsigned int) default_unknown_char);}voidLong2short_options (int argc, char **argv, const struct long2short_options_type *long2short_options){ char *equal_ptr; int arg, long_opt_ind, matched_long_opt; for (arg=1; arg < argc; arg++) { if (argv[arg][0]=='-') if (argv[arg][1]=='-') { if (argv[arg][2]=='\0') break; if ((equal_ptr=strchr (argv[arg], '='))) *(equal_ptr) = '\0'; matched_long_opt = -1; for (long_opt_ind=0; long2short_options[long_opt_ind].long_option!=NULL; long_opt_ind++) { if (Str_has_prefix (long2short_options[long_opt_ind].long_option, argv[arg]+2)==0) if (matched_long_opt == -1) matched_long_opt = long_opt_ind; else if (long2short_options[long_opt_ind].short_option != long2short_options[matched_long_opt].short_option) { fprintf (stderr, "2UTF: '%s' - %s \n", argv[arg], ambig_opt); exit (1); } } if (matched_long_opt == -1) { fprintf (stderr, "2UTF: '%s' - %s \n", argv[arg], unknown_opt); exit (1); } else { *(argv[arg]+1)=long2short_options[matched_long_opt].short_option; if (equal_ptr) memmove (argv[arg]+2, equal_ptr+1, strlen (equal_ptr+1) + 1); else *(argv[arg]+2)='\0'; } } else if (argv[arg][1]=='?') argv[arg][1]='h'; else if (strcmp (argv[arg]+1, "help")==0) argv[arg][1]='h', argv[arg][2]='\0'; }}#define Print_approx_Macro(c, wchar, unknown_char) \{ \ /*#include "approx.c"*/ \ int index = 257; \ \ if ((wchar) != 0xFEFF) /* skip byte order mark */ \ { \ if ((c) == 1) \ { \ for (index = 0; index < 256; index++) \ if (charset_p->charmap[index] == (wchar)) \ { \ if (putc (index, charset_p->pipe) == EOF) \ Error (IO_err); \ break; \ } \ } \ if (index >= 256) \ if ((size_t) (wchar) \ <NUM_OF_ELEM (approx2ascii) \ &&approx2ascii[wchar][0] != '\0') \ { \ for (index = 0; approx2ascii[wchar][index] != '\0' \ && index < (int) NUM_OF_ELEM (approx2ascii[0]); index++) \ if (charset_p->charmap[approx2ascii[wchar][index]] \ !=approx2ascii[wchar][index]) \ { \ index = -1; \ break; \ } /* check for non US-ASCII compatibles */ \ if (index != -1) \ if (html_doc) \ for (index = 0; approx2ascii[wchar][index] != '\0' \ && index < (int) NUM_OF_ELEM (approx2ascii[0]); index++) \ Putchar_html_Macro (approx2ascii[wchar][index]) \ else \ fprintf (charset_p->pipe, "%.*s", \ (int) NUM_OF_ELEM (approx2ascii[0]), approx2ascii[wchar]); \ else if (putc (unknown_char, charset_p->pipe) == EOF) \ Error (IO_err); \ } \ else if (putc (unknown_char, charset_p->pipe) == EOF) \ Error (IO_err); \ } \}/*if ((((int)some) < 0x20) || ((((int)some) > 0x7F) && (((int)some) < 0xA0)))*/#define Putchar_html_Macro(some) \{ \ int exit_code; \ \ switch (some) \ { \ case '"': \ exit_code = fputs(""", charset_p->pipe); \ break; \ case '&': \ exit_code = fputs("&", charset_p->pipe); \ break; \ case '<': \ exit_code = fputs("<", charset_p->pipe); \ break; \ case '>': \ exit_code = fputs(">", charset_p->pipe); \ break; \ default: \ exit_code = putc(some, charset_p->pipe); \ } \ if (exit_code == EOF) \ Error (IO_err); \} voidPrint_paths (void){ int i; unsigned char *config_pathname = CONFIG_PATHNAMES; fprintf (stderr, paths_help_config_pathname); do fprintf (stderr, "'%s' \n", config_pathname); /* check for double null terminator */ while (*(config_pathname = strchr (config_pathname, '\0') + 1) != '\0'); fprintf (stderr, paths_help_compiled); for (i = 0; i < compiled_paths_number; i++) fprintf (stderr, "'%s' \n", compiled_paths[i]); fprintf (stderr, paths_help_used); for (i = 0; i < paths_number; i++) fprintf (stderr, "'%s' \n", paths[i]); fprintf (stderr, paths_help_aliases);} intList_charmaps (){ FILE *aliases_file; char *ptr, s[max_string_length]; int index; if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL) { fprintf (stderr, "2UTF: %s %s", can_not_open, aliases_pathname); if (!Create_aliases ()) fprintf (stderr, "2UTF: %s '%s' \n", can_not_create, aliases_pathname); else if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL) Error (internal_err); } if (aliases_file == NULL) return (0); fprintf (stderr, "\n2UTF: %s \n\n", avail_charmaps); while (fgets (s, max_string_length, aliases_file) != NULL) if ((ptr = strchr (s, ' ')) == NULL) fprintf (stderr, "\n2UTF: %s '%s' \n", bad_line, s); else fprintf (stderr, "%s", ptr + 1); if (EOF == fclose (aliases_file)) Error (IO_err); fprintf (stderr, "\n2UTF: %s \n\n", avail_ext_charsets); for (index = 0; index < ext_charsets_number; index++) fprintf (stderr, "%s \n", ext_charsets[index].names); return (1);} inline intLook_for_ext_charset (enum USASCII_is_subset_type USASCII_is_subset){ FILE *ret; unsigned char *charset_name, *command_line, *cp2; int index; *(charset_name = xmalloc (strlen (charset_p->name) + 3)) = ' '; *(cp2 = Stpcpy (charset_name + 1, charset_p->name)) = ' '; *(cp2 + 1) = '\0'; Strtoupper (charset_name); if (Debug >= 3) fprintf (stderr, "2UTF: Looking for name '%s' in configuration file \n", charset_name); for (index = 0; index < ext_charsets_number; index++) { if (Str_str (ext_charsets[index].names, charset_name)) { charset_p->USASCII_is_subset = ext_charsets[index].USASCII_is_subset; if (reverse || show_charmap) command_line = ext_charsets[index].from_UTF; else command_line = ext_charsets[index].to_UTF; if (command_line == NULL) break; if (command_line[0] == '%') { command_line++; charset_p->type = NON_BUF_PIPE; } else charset_p->type = BUF_PIPE; if (USASCII_is_subset == IS && charset_p->USASCII_is_subset == NO) { charset_p = &USASCII_charset; return (TRUE); } ret = popen (command_line, "w"); if (Debug >= 4) fprintf (stderr, "2UTF: popen (\"%s\", \"w\") returned %lx \n", command_line, (unsigned long int) ret); if (ret == NULL) { if (verbose) fprintf (stderr, "2UTF: %s \n '%s' \n ", can_not_exec, command_line); return (EOF); } else { if (verbose) fprintf (stderr, "2UTF: %s '%s' \n", using, ext_charsets[index].names); /* For the output of interractive programms */ setvbuf (ret, NULL, Mail ? _IOLBF : _IONBF, 0); charset_p->pipe = ret; return (TRUE); } } } return (FALSE);} inline intOpen_iconv (struct charset_type * charset_p){ iconv_t h; char charset_name[sizeof(charset_p->name)]; h = (iconv_t) -1; Stpcpy(charset_name, charset_p->name); /* some stupid iconv libraries can't cope with uppercase names */ Strtolower(charset_name); if (reverse) { if (Debug >= 3) fprintf (stderr, "2UTF: Trying reverse iconv_open('%s','utf-8')\n", charset_name); h = iconv_open(charset_p->name, "utf-8"); } else { if (Debug >= 3) fprintf (stderr, "2UTF: Trying iconv_open('utf-8','%s')\n", charset_name); h = iconv_open("utf-8", charset_name); } if (h != (iconv_t) -1) { charset_p->type = ICONV; charset_p->pipe = stdout; charset_p->USASCII_is_subset = NO; charset_p->iconv_p = xmalloc(sizeof(*(charset_p->iconv_p))); charset_p->iconv_p->iconv_handle = h; *(charset_p->iconv_p->inendptr = charset_p->iconv_p->in_buf) = *(charset_p->iconv_p->outptr = charset_p->iconv_p->out_buf) = '\0'; charset_p->iconv_p->inleft = 0; if (Debug >= 2) fprintf (stderr, "2UTF: Got '%x' for '%s' from iconv \n", h, charset_p->name); return TRUE; } else { return FALSE; }}voidOutput_consumed_chars (unsigned char *space_between_enc_words, unsigned char *charset_name, size_t charset_name_length, unsigned char *encoding){ int index = -1; unsigned char *charp, *encp; while (space_between_enc_words[++index]) PUTC_IN_UTF8 (space_between_enc_words[index]); space_between_enc_words[0] = '\0'; charp = charset_name; PUTC_IN_UTF8 ('='); PUTC_IN_UTF8 ('?'); while ((size_t) (charp - charset_name) < charset_name_length) { PUTC_IN_UTF8 (*charp); charp++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -