1 /*
2     psftools: Manipulate console fonts in the .PSF format
3     Copyright (C) 2003  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 /* All the conversion programs use this code to provide their main(). It
21  * is a basic framework for a one-to-one conversion program, handling
22  * option parsing and opening/closing files.
23  */
24 
25 
26 #include "cnvshell.h"
27 
28 #include "cnvshell.inc"
29 
30 /* main() parses arguments, opens files, calls the converter. */
31 
main(int argc,char ** argv)32 int main(int argc, char **argv)
33     {
34     int n;
35     int stoparg = 0;
36     char *fname1 = NULL,  *fname2 = NULL;
37     FILE *fpin   = stdin, *fpout  = stdout;
38     char *s;
39 
40     /* Some CP/M and DOS compilers don't support argv[0] */
41     if (argv[0][0]) cnv_progname = argv[0];
42     /* Argument parsing */
43     for (n = 1; n < argc; n++) if (isarg(argv[n]) && !stoparg)
44         {
45         if (!strcmp(argv[n], "--")) { stoparg = 1; continue; }
46 
47         /* Check for likely help commands */
48         if (!stricmp(argv[n],   "--help")) help();
49         if (!stricmp(argv[n]+1, "h"     )) help();
50 #ifdef __MSDOS__
51         if (!stricmp(argv[n]+1, "?"     )) help();
52 #endif
53 #ifdef CPM
54         if (!stricmp(argv[n]+1, "?"     )) help();
55         if (!stricmp(argv[n],   "//"    )) help();
56         if (!stricmp(argv[n],   "[help]")) help();
57         if (!stricmp(argv[n],   "[h]"   )) help();
58 #endif
59         /* OK, it isn't a help command. */
60         if (argv[n][0] == '-' && argv[n][1] == '-')
61             {
62             handle_option(1, argv[n]+2);
63             continue;
64             }
65         /* CP/M-style [VARIABLE=VALUE,VARIABLE=VALUE] options */
66 #ifdef CPM
67         if (argv[n][0] == '[')
68             {
69             char *s;
70 
71             do
72                 {
73                 s = handle_option(1, argv[n]+2);
74                 } while ( s && (*s) && (*s != ']'));
75             continue;
76             }
77 #endif
78         /* Short option */
79         handle_option(0, argv[n]+1);
80         }
81     else
82         {
83         if      (!fname1) fname1 = argv[n];
84         else if (!fname2) fname2 = argv[n];
85         else
86             {
87             fprintf(stderr, "%s: This program takes two filenames, so '%s' is ignored.\n",
88                             cnv_progname, argv[n]);
89             }
90         }
91     /* Options parsed */
92     if (fname1)
93         {
94         fpin = fopen(fname1, "rb");
95         if (!fpin)
96             {
97             perror(fname1);
98             exit(1);
99             }
100         }
101     else fname1 = "<stdin>";
102 
103     if (fname2)
104         {
105         fpout = fopen(fname2, "wb");
106         if (!fpout)
107             {
108             fclose(fpin);
109             perror(fname2);
110             exit(1);
111             }
112         }
113     else fname2 = "<stdout>";
114 
115     s = cnv_execute(fpin, fpout);
116 
117     if (fpin  != stdin)  fclose(fpin);
118     if (fpout != stdout) fclose(fpout);
119 
120     if (!s) return 0;
121     if (fpout != stdout) remove(fname2);
122 
123     fprintf(stderr, "%s: %s\n", cnv_progname, s);
124     return 1;
125     }
126 
127