beta-0.89.2
[luatex.git] / source / texk / kpathsea / win32 / mktexfmt.c
blob3171643d64fa8899ddf18c794c7e24918cfa400a
1 /* mktexfmt.c
3 Copyright 2000, 2015 Akira Kakuto.
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.
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.
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/>.
20 kpathsea functions
22 #include <kpathsea/kpathsea.h>
24 #define VERSION "0.1"
26 int main(int ac, char **av)
28 int savo, savi;
29 FILE *fnul;
30 char *fmtname;
31 char *p;
32 char orgname[256];
34 char texbindir[256];
35 char fullbin[256];
37 kpse_set_program_name(av[0], NULL);
39 p = kpse_program_name;
40 if(ac != 2) {
41 fprintf(stderr,"%s : Usage %s formatname\n", p, p);
42 fprintf(stderr,"formatname : (foo.fmt, foo.nfmt, foo.mem, foo.base)\n");
43 return 1;
45 if(!strncmp(av[1], "-h", 2) || !strncmp(av[1], "--h", 3)) {
46 fprintf(stderr,"%s : Usage %s formatname\n", p, p);
47 fprintf(stderr,"formatname : (foo.fmt, foo.nfmt, foo.mem, foo.base)\n");
48 return 0;
50 if(!strncmp(av[1], "-v", 2) || !strncmp(av[1], "--v", 3)) {
51 fprintf(stderr,"%s : Version %s\n", p, VERSION);
52 return 0;
55 if(strlen(av[1]) > 127) {
56 fprintf(stderr, "\nToo long a format name.\n");
57 return 100;
60 p = kpse_var_value("SELFAUTOLOC");
61 if(p == 0) {
62 fprintf(stderr, "I cannot get SELFAUTOLOC\n");
63 exit(1);
65 strcpy(texbindir, p);
66 free(p);
67 for(p=texbindir; *p; p++) {
68 if(*p == '/') *p = '\\';
70 strcat(texbindir, "\\");
72 strcpy(orgname, av[1]);
74 p = strrchr(av[1], '.');
75 if(!p) {
76 fprintf(stderr, "formatname needs a suffix.\n");
77 return 1;
80 if(stricmp(p, ".fmt") && stricmp(p, ".nfmt") &&
81 stricmp(p, ".base") && stricmp(p, ".mem")) {
82 fprintf(stderr, "%s : unknown format type.\n", av[1]);
83 return 1;
86 *p = '\0';
88 /* save stdout and stdin */
89 savo = _dup(fileno(stdout));
90 savi = _dup(fileno(stdin));
92 /* connect stdout to stderr */
93 _dup2(fileno(stderr), fileno(stdout));
95 /* connect stdin to nul device */
96 if(!(fnul = fopen("nul", "r"))) {
97 fprintf(stderr, "Cannot open nul device to read\n");
98 exit(100);
100 _dup2(fileno(fnul), fileno(stdin));
102 /* COMMAND */
103 strcpy(fullbin, texbindir);
104 strcat(fullbin, "fmtutil.exe");
105 fprintf(stderr, "Running the command %s\n", fullbin);
106 _spawnlp(_P_WAIT, fullbin, "fmtutil", "--byfmt", av[1], NULL);
108 /* END COMMAND */
110 /* return to original stdout and stdin */
111 _dup2(savo, fileno(stdout));
112 close(savo);
113 _dup2(savi, fileno(stdin));
114 close(savi);
116 /* close nul device */
117 fclose(fnul);
119 fmtname = kpse_find_file(orgname , kpse_fmt_format, 0);
120 if(fmtname) {
121 printf("%s\n", fmtname);
122 free(fmtname);
124 return 0;