1 /*
2 	psftools: Manipulate console fonts in the .PSF format
3 	Copyright (C) 2000  John Elliott
4 
5 	This program is free software; you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation; either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	This program 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
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 /* fnt2fon uses this code to provide their main(). It
21  * is a basic framework for a many-to-one conversion program, handling
22  * option parsing and opening/closing the target file.
23  */
24 
25 
26 #include "cnvmulti.h"
27 
28 
29 #include "cnvshell.inc"
30 
31 /* The fun of short command lines... */
32 #ifdef CPM
33 #define SHORT_CMDLINE 1
34 #define PATH_MAX 20
35 #endif
36 
37 #ifdef __MSDOS__
38 #define SHORT_CMDLINE 1
39 #ifndef PATH_MAX
40 #define PATH_MAX 68
41 #endif
42 #endif
43 
44 
45 static char **fname;
46 static int  nfname = 0, mfname = 10;
47 
nomem(void)48 static void nomem(void)
49 {
50 	fprintf(stderr, "Out of memory building argument list\n");
51 #ifdef EXIT_FAILURE
52 	exit(EXIT_FAILURE);
53 #else
54 	exit(1);
55 #endif
56 }
57 
add_fname(char * s,int a)58 static void add_fname(char *s, int a)
59 {
60 	if (a)
61 	{
62 		char *t = malloc(1 + strlen(s));
63 		if (!t) nomem();
64 		strcpy(t, s);
65 		fname[nfname++] = t;
66 	}
67 	else fname[nfname++] = s;
68 	if (nfname == mfname)
69 	{
70 		fname = realloc(fname, 2 * mfname * sizeof(char *));
71 		if (!fname) nomem();
72 		mfname *= 2;
73 	}
74 }
75 
76 /* Read in a list of filenames */
77 #ifdef SHORT_CMDLINE
parse_listfile(char * s)78 static void parse_listfile(char *s)
79 {
80 	FILE *fp = fopen(s, "r");
81 	char buf[PATH_MAX + 20], *p;
82 	int ch;
83 
84 	if (!fp)
85 	{
86 		perror(s);
87 		exit(1);
88 	}
89 	while (fgets(buf, sizeof(buf), fp))
90 	{
91 		p = strchr(buf, '\n');
92 		if (p) *p = 0;
93 		else do	/* Swallow remainder of line */
94 		{
95 			ch = fgetc(fp);
96 		} while (ch != EOF && ch != '\n');
97 		for (p = strtok(buf, " \t"); p != NULL;
98 			 p = strtok(NULL, " \t"))
99 		{
100 			/* Don't include EOF */
101 			if (p[0] != 0x1A) add_fname(p, 1);
102 		}
103 	}
104 }
105 #endif
106 
107 
108 /* main() parses arguments, opens files, calls the converter. */
109 
main(int argc,char ** argv)110 int main(int argc, char **argv)
111 {
112 	int n;
113 	int stoparg = 0;
114 	char *s;
115 	FILE *fpout = stdout;
116 
117 	fname = malloc (10 * sizeof(char *));
118 	if (!fname) nomem();
119 
120 	/* Some CP/M and DOS compilers don't support argv[0] */
121 	if (argv[0][0]) cnv_progname = argv[0];
122 	/* Argument parsing */
123 	for (n = 1; n < argc; n++) if (isarg(argv[n]) && !stoparg)
124 	{
125 		if (!strcmp(argv[n], "--")) { stoparg = 1; continue; }
126 
127 		/* Check for likely help commands */
128 		if (!stricmp(argv[n],   "--help")) help();
129 		if (!stricmp(argv[n]+1, "h"	 )) help();
130 #ifdef __MSDOS__
131 		if (!stricmp(argv[n]+1, "?"	 )) help();
132 #endif
133 #ifdef CPM
134 		if (!stricmp(argv[n]+1, "?"	 )) help();
135 		if (!stricmp(argv[n],   "//"	)) help();
136 		if (!stricmp(argv[n],   "[help]")) help();
137 		if (!stricmp(argv[n],   "[h]"   )) help();
138 #endif
139 		/* OK, it isn't a help command. */
140 		if (argv[n][0] == '-' && argv[n][1] == '-')
141 		{
142 			handle_option(1, argv[n]+2);
143 			continue;
144 		}
145 		/* CP/M-style [VARIABLE=VALUE,VARIABLE=VALUE] options */
146 #ifdef CPM
147 		if (argv[n][0] == '[')
148 		{
149 			char *s;
150 
151 			do
152 			{
153 				s = handle_option(1, argv[n]+2);
154 			} while ( s && (*s) && (*s != ']'));
155 			continue;
156 		}
157 #endif
158 		/* Short option */
159 		handle_option(0, argv[n]+1);
160 	}
161 	else
162 	{
163 #ifdef SHORT_CMDLINE
164 		if (argv[n][0] == '@')
165 		{
166 			parse_listfile(argv[n] + 1);
167 		}
168 		else
169 #endif
170 		add_fname(argv[n], 0);
171 	}
172 	/* Options parsed */
173 	/* If filenames omitted, use stdio */
174 	if (nfname < 1) { fname[0] = "-"; ++nfname; }
175 	if (nfname < 2) { fname[1] = "-"; ++nfname; }
176 
177 	if (!strcmp(fname[nfname-1], "-"))
178 	{
179 		fname[nfname-1] = "stdin";
180 	fpout = stdout;
181 	}
182 	else
183 	{
184 		fpout = fopen(fname[nfname-1], "wb");
185 		if (!fpout)
186 		{
187 			perror(fname[nfname-1]);
188 			free(fname);
189 			exit(1);
190 		}
191 	}
192 
193 	s = cnv_multi(nfname-1, fname, fpout);
194 
195 	if (fpout != stdout) fclose(fpout);
196 
197 	if (!s) { free(fname); return 0; }
198 	if (fpout != stdout) remove(fname[nfname-1]);
199 
200 	free(fname);
201 	fprintf(stderr, "%s: %s\n", cnv_progname, s);
202 	return 1;
203 }
204 
205