2
Purpose: call LATEX after preprocessing of the .tex file by
3
the cjk conversion tool. The old f_name.bat script
4
is not working anymore.
5
Author : F. Popineau <Fabrice.Popineau@supelec.fr>
14
Copyright (C) 2001-2012 F. Popineau <Fabrice.Popineau@supelec.fr>
16
This program is free software; you can redistribute it and/or modify
17
it under the terms of the GNU General Public License as published by
18
the Free Software Foundation; either version 2 of the License, or
19
(at your option) any later version.
21
This program is distributed in the hope that it will be useful,
22
but WITHOUT ANY WARRANTY; without even the implied warranty of
23
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
GNU General Public License for more details.
26
You should have received a copy of the GNU General Public License
27
along with this program in doc/COPYING; if not, write to the Free
28
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
35
#include <kpathsea/config.h>
36
#include <kpathsea/lib.h>
37
#include <kpathsea/getopt.h>
39
static const char *cjklatex_version_string = "1.0";
41
static const char *usage_str[] = {
42
"Usage: %s OPTIONS FILE\n",
43
"Calls `" LATEX "' on FILE after conversion by the filter\n",
44
"specified by OPTIONS.\n",
45
"--conv=bg5\tfor traditional Chinese, encoding Big 5,\n",
46
"--conv=cef\tfor Chinese Encoding Framework, encoding CEF,\n",
47
"--conv=cef5\tidem CEF, also converts Big5 characters,\n",
48
"--conv=cefs\tidem CEF, also converts SJIS characters,\n",
49
"--conv=gbk\tfor Chinese, encoding GBK,\n",
50
"--conv=sjis\tfor Japanese, SJIS encoding.",
51
"\nAlternatively, for compatibility with the previous DOS batch files,\n",
52
"you can also copy this program to any of the following names:\n",
53
"bg5" LATEX ".exe, cef5" LATEX ".exe, cef" LATEX ".exe, cefs" LATEX ".exe,\n",
54
"gbk" LATEX ".exe and sjis" LATEX ".exe .\n",
55
"Then running one of these programs will be identical to specify\n",
56
"the corresponding option.\n",
57
"\nAdditional options:\n",
58
"--verbose\tbe a bit more verbose about what is happening,\n",
59
"--nocleanup\tdo not remove intermediate files,\n",
60
"--latex=engine\tuse `engine' instead of `" LATEX "' to process the file.\n",
64
static char *progname = NULL;
66
static struct _conv_table {
68
const char *processor;
71
{ "bg5" LATEX, "bg5conv" },
72
{ "cef" LATEX, "cefconv" },
73
{ "cef5" LATEX, "cef5conv" },
74
{ "cefs" LATEX, "cefsconv" },
75
{ "gbk" LATEX, "extconv" },
76
{ "sjis" LATEX, "sjisconv" }
79
#define PROGRAM_IS(p) FILESTRCASEEQ (p, progname)
80
#define ARGUMENT_IS(a) STREQ (long_options[option_index].name, a)
82
static int program_number = -1;
83
static int opt_verbose = 0;
84
static int opt_nocleanup = 0;
85
static char *texname, *cjkname, *texengine;
87
static struct option long_options [] = {
90
{ "version", 0, 0, 0},
91
{ "verbose", 0, 0, 0},
92
{ "nocleanup", 0, 0, 0},
98
static BOOL sigint_handler(DWORD dwCtrlType)
100
/* Fix me : there is a problem if a system() command is running.
101
We should wait for the son process to be interrupted.
102
Only way I can think of to do that : rewrite system() based on
103
spawn() with parsing of the command line and set a global pid
104
Next cwait(pid) in the HandlerRoutine.
107
/* This is not that good, but else we would need to wait for
108
the child processes to finish ! */
111
fprintf(stderr, "Sending Ctrl+Break!\n");
112
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0);
119
return FALSE; /* return value obligatory */
122
static void usage(void)
125
fprintf(stderr, "CJK" LATEX " version %s\n", cjklatex_version_string);
126
fprintf(stderr,usage_str[0], progname );
128
for(i = 1; usage_str[i]; ++i)
129
fputs(usage_str[i], stderr);
132
static int do_process(const char *processor, const char *filename)
135
char cmd[_MAX_PATH*3];
139
if (strlen(filename) > 4 && _strnicmp(filename + strlen(filename) - 4, ".tex", 4) == 0) {
140
texname = xstrdup(filename);
143
texname = concat(filename, ".tex");
145
for (p = texname; p && *p; p++)
146
*p = (*p == '\\' ? '/' : *p);
148
dwFA = GetFileAttributes(texname);
149
if (dwFA == 0xFFFFFFFF || (dwFA & FILE_ATTRIBUTE_DIRECTORY)) {
150
fprintf(stderr, "%s: %s is an invalid input file.\n",
155
cjkname = xstrdup(texname);
156
ext = strrchr(cjkname, '.');
158
strcpy(cjkname + (ext - cjkname), ".cjk");
160
sprintf(cmd, "%s < %s > %s", processor, texname, cjkname);
162
fprintf(stderr, "%s: running command `%s'.\n", progname, cmd);
165
sprintf(cmd, "%s %s", texengine, cjkname);
167
fprintf(stderr, "%s: running command `%s'.\n", progname, cmd);
180
int main(int argc, char *argv[])
182
int g; /* getopt return code */
190
kpse_set_program_name (progname, NULL);
191
progname = kpse_program_name;
193
for (i = 0; i < sizeof(CJKtable)/sizeof(CJKtable[0]) && program_number < 0; i++) {
194
if (STREQ(progname, CJKtable[i].progname)
196
|| (STREQ(progname, CJKtable[i].progname)
197
&& STREQ(progname+strlen(CJKtable[i].progname), ".exe"))
204
if (program_number == -1) {
205
fprintf(stderr, "%s: this program has been incorrecty copied to the name %s.\n", progname, progname);
211
g = getopt_long_only (argc, argv, "", long_options, &option_index);
217
usage(); /* Unknown option. */
221
/* assert (g == 0); */ /* We have no short option names. */
223
FIXME : try 'mktexpk --mfmode --bdpi 600 ...'
225
if (ARGUMENT_IS ("debug")) {
226
kpathsea_debug |= atoi (optarg);
228
else if (ARGUMENT_IS ("help")) {
232
else if (ARGUMENT_IS ("verbose")) {
235
else if (ARGUMENT_IS ("nocleanup")) {
238
else if (ARGUMENT_IS ("latex")) {
239
texengine = xstrdup(optarg);
241
else if (ARGUMENT_IS ("version")) {
242
fprintf(stderr, "%s of %s.\n", progname, cjklatex_version_string);
245
else if (PROGRAM_IS("cjk" LATEX)) {
246
if (ARGUMENT_IS("conv")) {
247
for (i = 1; i < sizeof(CJKtable)/sizeof(CJKtable[0]) && program_number <= 0; i++) {
248
if (STRNEQ(optarg, CJKtable[i].progname, strlen(optarg))) {
256
/* shifting options from argv[] list */
257
for (i = 1; optind < argc; i++, optind++)
258
argv[i] = argv[optind];
264
fprintf (stderr, "%s: Missing argument(s).\nTry `%s --help' for more information.\n", progname, kpse_program_name);
269
fprintf(stderr, "%s: Extra arguments", progname);
270
for (i = 2; i < argc; i++)
271
fprintf (stderr, " \"%s\"", argv[i]);
272
fprintf (stderr, "\nTry `%s --help' for more information.\n",
277
SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigint_handler, TRUE);
279
filename = xstrdup(argv[1]);
281
assert(program_number > 0);
284
texengine = xstrdup(LATEX);
287
do_process(CJKtable[program_number].processor, filename);