| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2024 Japanese TeX Development Community <[email protected]> |
| 4 |
Distributed under the 3-Clause BSD License. |
| 5 |
|
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef _WIN32 |
| 9 |
#include <unistd.h> |
| 10 |
#endif |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <stdlib.h> |
| 14 |
|
| 15 |
#include <kpathsea/getopt.h> |
| 16 |
#include <kpathsea/progname.h> |
| 17 |
#include <kpathsea/lib.h> |
| 18 |
#ifdef _WIN32 |
| 19 |
#include <kpathsea/knj.h> |
| 20 |
#ifndef CP_UTF8 |
| 21 |
#define CP_UTF8 65001 |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
#include <ptexenc/ptexenc.h> |
| 25 |
#include <c-auto.h> |
| 26 |
|
| 27 |
static char opt_input_enc[5]; |
| 28 |
static char opt_output_enc[5]; |
| 29 |
static int flg_guess_enc; |
| 30 |
static int flg_output_buffer; |
| 31 |
|
| 32 |
#define GUESS_ONLY -1 |
| 33 |
#define GUESS_AND_OUTPUT +1 |
| 34 |
|
| 35 |
#define BUFFERLEN 4096 |
| 36 |
static char buff[BUFFERLEN]; |
| 37 |
|
| 38 |
static const char *optstr = "hvgbjseuGJSEU"; |
| 39 |
|
| 40 |
static struct option long_options[] = { |
| 41 |
{"help", 0, 0, 'h'}, |
| 42 |
{"version", 0, 0, 'v'}, |
| 43 |
{"guess", 0, 0, 'g'}, |
| 44 |
{"buffer", 0, 0, 'b'}, |
| 45 |
{0, 0, 0, 0} |
| 46 |
}; |
| 47 |
|
| 48 |
#define MY_VERSION "20240211" |
| 49 |
#define BUG_ADDRESS "[email protected]" |
| 50 |
|
| 51 |
static void show_version(void) |
| 52 |
{ |
| 53 |
printf("ptekf ver." MY_VERSION " (%s) (%s, %s)\n", get_enc_string(), ptexenc_version_string, TL_VERSION); |
| 54 |
printf(" Copyright (C) 2024 Japanese TeX Development Community\n"); |
| 55 |
} |
| 56 |
|
| 57 |
static void show_usage(void) |
| 58 |
{ |
| 59 |
printf("Usage: ptekf -[OPTION] [--] in_file1 [in_file2 ...]\n"); |
| 60 |
printf(" j/s/e/u Specify output encoding ISO-2022-JP, Shift_JIS, EUC-JP, UTF-8\n"); |
| 61 |
printf(" J/S/E/U Specify input encoding ISO-2022-JP, Shift_JIS, EUC-JP, UTF-8\n"); |
| 62 |
printf(" G Guess the input encoding and output to stdout or files\n"); |
| 63 |
printf(" --guess -g Guess the input encoding (no conversion)\n"); |
| 64 |
printf(" --buffer -b Output internal buffer without code conversion\n"); |
| 65 |
printf(" --version -v Print version number\n"); |
| 66 |
printf(" --help -h Print this help\n"); |
| 67 |
printf("Default input/output encoding depends on kpathsearch parameters PTEX_KANJI_ENC, guess_input_kanji_encoding\n"); |
| 68 |
printf("\nEmail bug reports to %s.\n", BUG_ADDRESS); |
| 69 |
} |
| 70 |
|
| 71 |
const char *infname; |
| 72 |
char *outfname; |
| 73 |
FILE *infp, *outfp; |
| 74 |
|
| 75 |
#if defined(_WIN32) |
| 76 |
#define R_MODE "rb" |
| 77 |
#define W_MODE "wb" |
| 78 |
#else |
| 79 |
#define R_MODE "r" |
| 80 |
#define W_MODE "w" |
| 81 |
#endif |
| 82 |
|
| 83 |
static char *mfgets(char *buff, int size, FILE *fp) |
| 84 |
{ |
| 85 |
int c, len; |
| 86 |
|
| 87 |
if ((len = input_line2(fp, (unsigned char *)buff, NULL, 0, size, &c)) == 0 |
| 88 |
&& c != '\r' && c != '\n') return NULL; |
| 89 |
if (c == '\n' || c == '\r') { |
| 90 |
if (len+1 < size) { buff[len]=(unsigned char)c; buff[len+1]='\0'; } |
| 91 |
else ungetc(c, fp); |
| 92 |
} |
| 93 |
if (c == EOF) return NULL; |
| 94 |
return buff; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
int |
| 99 |
main (int argc, char **argv) |
| 100 |
{ |
| 101 |
int c; |
| 102 |
int opt_ienc=0, opt_oenc=0; |
| 103 |
int (*fputs__)(const char*, FILE*); |
| 104 |
|
| 105 |
if (argc<=1) { |
| 106 |
show_version(); |
| 107 |
show_usage(); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
kpse_set_program_name(argv[0], "ptekf"); |
| 111 |
#if defined(_WIN32) |
| 112 |
{ |
| 113 |
int ac; |
| 114 |
char **av; |
| 115 |
file_system_codepage = CP_UTF8; |
| 116 |
is_cp932_system = 0; |
| 117 |
if (get_command_line_args_utf8("utf-8", &ac, &av)) { |
| 118 |
argv = av; |
| 119 |
argc = ac; |
| 120 |
} |
| 121 |
} |
| 122 |
#endif |
| 123 |
enable_UPTEX(true); |
| 124 |
|
| 125 |
while ((c = getopt_long(argc, argv, optstr, long_options, NULL)) != -1) { |
| 126 |
switch (c) { |
| 127 |
|
| 128 |
case 'G': /* guess and output to stdout or files */ |
| 129 |
flg_guess_enc = GUESS_AND_OUTPUT; |
| 130 |
opt_ienc++; |
| 131 |
break; |
| 132 |
|
| 133 |
case 'g': /* guess */ |
| 134 |
flg_guess_enc = GUESS_ONLY; |
| 135 |
opt_ienc++; |
| 136 |
break; |
| 137 |
|
| 138 |
case 'b': /* output internal buffer */ |
| 139 |
flg_output_buffer = 1; |
| 140 |
opt_oenc++; |
| 141 |
break; |
| 142 |
|
| 143 |
case 'h': /* help */ |
| 144 |
show_version(); |
| 145 |
show_usage(); |
| 146 |
return 0; |
| 147 |
|
| 148 |
case 'v': /* version */ |
| 149 |
show_version(); |
| 150 |
return 0; |
| 151 |
|
| 152 |
/* input encoding */ |
| 153 |
case 'J': strcpy(opt_input_enc, "jis") ; opt_ienc++; break; |
| 154 |
case 'S': strcpy(opt_input_enc, "sjis") ; opt_ienc++; break; |
| 155 |
case 'E': strcpy(opt_input_enc, "euc") ; opt_ienc++; break; |
| 156 |
case 'U': strcpy(opt_input_enc, "utf8") ; opt_ienc++; break; |
| 157 |
|
| 158 |
/* output encoding */ |
| 159 |
case 'j': strcpy(opt_output_enc, "jis") ; opt_oenc++; break; |
| 160 |
case 's': strcpy(opt_output_enc, "sjis") ; opt_oenc++; break; |
| 161 |
case 'e': strcpy(opt_output_enc, "euc") ; opt_oenc++; break; |
| 162 |
case 'u': strcpy(opt_output_enc, "utf8") ; opt_oenc++; break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if (opt_ienc>1) { |
| 167 |
fprintf(stderr, "Option (--guess or -g), -G, -J, -S, -E and -U are mutually exclusive\n"); |
| 168 |
exit(16); |
| 169 |
} |
| 170 |
if (opt_oenc>1) { |
| 171 |
fprintf(stderr, "Option (--buffer or -b), -j, -s, -e and -u are mutually exclusive\n"); |
| 172 |
exit(16); |
| 173 |
} |
| 174 |
|
| 175 |
fputs__ = flg_output_buffer ? fputs : fputs2; /* without/with code conversion */ |
| 176 |
|
| 177 |
while (optind < argc) { |
| 178 |
char *genc, *ret; |
| 179 |
|
| 180 |
infname = argv[optind++]; |
| 181 |
if ((infp = fopen(infname, R_MODE)) == 0) { |
| 182 |
fprintf(stderr, "ERROR: fail to open input file [%s].", infname); |
| 183 |
exit(32); |
| 184 |
} |
| 185 |
if (flg_guess_enc) { |
| 186 |
genc = ptenc_guess_enc(infp, 1, 1); |
| 187 |
printf("%s: %s\n", infname, genc); |
| 188 |
setinfileenc(infp, genc); |
| 189 |
free(genc); |
| 190 |
} |
| 191 |
|
| 192 |
if ((opt_ienc && flg_guess_enc!=GUESS_ONLY) || opt_oenc) { |
| 193 |
if (opt_oenc) { |
| 194 |
outfname = xmalloc(strlen(infname)+5); |
| 195 |
strcpy(outfname, infname); |
| 196 |
strcat(outfname, ".out"); |
| 197 |
|
| 198 |
if ((outfp = fopen(outfname, W_MODE)) == 0) { |
| 199 |
fprintf(stderr, "ERROR: fail to open output file [%s].", outfname); |
| 200 |
exit(32); |
| 201 |
} |
| 202 |
} |
| 203 |
else |
| 204 |
outfp = stdout; |
| 205 |
|
| 206 |
if (strlen(opt_input_enc)) { |
| 207 |
setinfileenc(infp, opt_input_enc); |
| 208 |
} |
| 209 |
if (strlen(opt_output_enc)) { |
| 210 |
setfileenc(opt_output_enc); |
| 211 |
} |
| 212 |
while ((ret = mfgets(buff, BUFFERLEN, infp)) != NULL) { |
| 213 |
(*fputs__)(buff, outfp); |
| 214 |
} |
| 215 |
if (ret == NULL && feof(infp)) { |
| 216 |
c = buff[strlen(buff)-1]; |
| 217 |
if (c != '\n' && c != '\r') |
| 218 |
(*fputs__)(buff, outfp); |
| 219 |
} |
| 220 |
if (fclose(infp)) { |
| 221 |
fprintf(stderr, "ERROR: fail to close input file [%s].", infname); |
| 222 |
exit(32); |
| 223 |
} |
| 224 |
if (outfp != stdout && fclose(outfp)) { |
| 225 |
fprintf(stderr, "ERROR: fail to close output file [%s].", outfname); |
| 226 |
exit(32); |
| 227 |
} |
| 228 |
if (outfname) |
| 229 |
free(outfname); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |