| 1 |
/* mktexfmt.c |
| 2 |
|
| 3 |
Copyright 2000, 2022 Akira Kakuto. |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
kpathsea functions |
| 21 |
*/ |
| 22 |
#include <kpathsea/kpathsea.h> |
| 23 |
|
| 24 |
#define VERSION "0.3" |
| 25 |
|
| 26 |
int main(int ac, char **av) |
| 27 |
{ |
| 28 |
int savo, savi; |
| 29 |
FILE *fnul; |
| 30 |
char *p; |
| 31 |
char orgname[256]; |
| 32 |
|
| 33 |
char texbindir[256]; |
| 34 |
char fullbin[256]; |
| 35 |
|
| 36 |
int is_w32tex; |
| 37 |
|
| 38 |
kpse_set_program_name(av[0], NULL); |
| 39 |
|
| 40 |
p = kpse_var_value("jtex_filetype"); |
| 41 |
|
| 42 |
if (p) { |
| 43 |
is_w32tex = 1; |
| 44 |
free(p); |
| 45 |
} else { |
| 46 |
is_w32tex = 0; |
| 47 |
} |
| 48 |
|
| 49 |
p = kpse_program_name; |
| 50 |
if(ac != 2) { |
| 51 |
fprintf(stderr,"%s : Usage %s formatname\n", p, p); |
| 52 |
fprintf(stderr,"formatname : foo.fmt, foo.base\n"); |
| 53 |
fprintf(stderr," extension can be omitted\n"); |
| 54 |
return 1; |
| 55 |
} |
| 56 |
if(!strncmp(av[1], "-h", 2) || !strncmp(av[1], "--h", 3)) { |
| 57 |
fprintf(stderr,"%s : Usage %s formatname\n", p, p); |
| 58 |
fprintf(stderr,"formatname : foo.fmt, foo.base\n"); |
| 59 |
fprintf(stderr," extension can be omitted\n"); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
if(!strncmp(av[1], "-v", 2) || !strncmp(av[1], "--v", 3)) { |
| 63 |
fprintf(stderr,"%s : Version %s\n", p, VERSION); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
if(strlen(av[1]) > 127) { |
| 68 |
fprintf(stderr, "\nToo long a format name.\n"); |
| 69 |
return 100; |
| 70 |
} |
| 71 |
|
| 72 |
p = kpse_var_value("SELFAUTOLOC"); |
| 73 |
if(p == 0) { |
| 74 |
fprintf(stderr, "I cannot get SELFAUTOLOC\n"); |
| 75 |
exit(1); |
| 76 |
} |
| 77 |
strcpy(texbindir, p); |
| 78 |
free(p); |
| 79 |
for(p=texbindir; *p; p++) { |
| 80 |
if(*p == '/') *p = '\\'; |
| 81 |
} |
| 82 |
strcat(texbindir, "\\"); |
| 83 |
|
| 84 |
strcpy(orgname, av[1]); |
| 85 |
|
| 86 |
p = strrchr(av[1], '.'); |
| 87 |
|
| 88 |
if (p) { |
| 89 |
if(stricmp(p, ".fmt") && stricmp(p, ".base") && stricmp(p, ".mem")) { |
| 90 |
fprintf(stderr, "%s : unknown format type.\n", av[1]); |
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
*p = '\0'; |
| 95 |
} |
| 96 |
|
| 97 |
/* save stdout and stdin */ |
| 98 |
savo = _dup(fileno(stdout)); |
| 99 |
savi = _dup(fileno(stdin)); |
| 100 |
|
| 101 |
/* connect stdout to stderr */ |
| 102 |
_dup2(fileno(stderr), fileno(stdout)); |
| 103 |
|
| 104 |
/* connect stdin to nul device */ |
| 105 |
if(!(fnul = fopen("nul", "r"))) { |
| 106 |
fprintf(stderr, "Cannot open nul device to read\n"); |
| 107 |
exit(100); |
| 108 |
} |
| 109 |
_dup2(fileno(fnul), fileno(stdin)); |
| 110 |
|
| 111 |
/* COMMAND */ |
| 112 |
strcpy(fullbin, texbindir); |
| 113 |
if (is_w32tex) { |
| 114 |
strcat(fullbin, "fmtutil.exe"); |
| 115 |
fprintf(stderr, "Running the command %s\n", fullbin); |
| 116 |
_spawnlp(_P_WAIT, fullbin, "fmtutil", "--byfmt", av[1], NULL); |
| 117 |
} else { |
| 118 |
strcat(fullbin, "fmtutil-user.exe"); |
| 119 |
fprintf(stderr, "Running the command %s\n", fullbin); |
| 120 |
_spawnlp(_P_WAIT, fullbin, "fmtutil-user", "--byfmt", av[1], NULL); |
| 121 |
} |
| 122 |
/* END COMMAND */ |
| 123 |
|
| 124 |
/* return to original stdout and stdin */ |
| 125 |
_dup2(savo, fileno(stdout)); |
| 126 |
close(savo); |
| 127 |
_dup2(savi, fileno(stdin)); |
| 128 |
close(savi); |
| 129 |
|
| 130 |
/* close nul device */ |
| 131 |
fclose(fnul); |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |