1 | /*
|
---|
2 | ============================================================================
|
---|
3 | genmain.c -- general purpose main
|
---|
4 | Version 1 -- 1987-11-10 -- D.N. Lynx Crowe
|
---|
5 |
|
---|
6 | Skeleton main program and option processing functions.
|
---|
7 | Designed to be copied and hacked up into a real program.
|
---|
8 | Handles all of the usual Unix(tm) style command line option forms.
|
---|
9 |
|
---|
10 | The functions Done(), Process(), Usage() and findopt() will need to be
|
---|
11 | modified to make this into a real program. It may also be useful
|
---|
12 | to modify Die() for any error cleanup necessary.
|
---|
13 | ============================================================================
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include "stdio.h"
|
---|
17 | #include "ctype.h"
|
---|
18 | #include "stddefs.h"
|
---|
19 |
|
---|
20 | /* Things that will change from program to program */
|
---|
21 |
|
---|
22 | #define PGMNAME "genmain" /* program name to use in messages */
|
---|
23 |
|
---|
24 | #define NULLFILE "stdin" /* null input file default */
|
---|
25 |
|
---|
26 | #define A_DFLTN -1 /* default value for a_argn */
|
---|
27 | #define A_ALTN 1 /* alternate default for a_argn */
|
---|
28 | #define C_DFLTC 'C' /* default character for c_argc */
|
---|
29 | #define C_DFLTN -1 /* default value for c_argn */
|
---|
30 | #define C_ALTN 1 /* alternate default for c_argn */
|
---|
31 | #define H_DFLTS "h default" /* defalut value for h_args */
|
---|
32 | #define L_DFLTN -1 /* default value for l_argn */
|
---|
33 | #define S_ALTC 's' /* alternate default for s_argc */
|
---|
34 | #define S_DFLTC 'S' /* default character for s_argc */
|
---|
35 |
|
---|
36 | #define NUM_DFLT -1 /* default value for num_arg */
|
---|
37 |
|
---|
38 | int a_argn = A_DFLTN; /* switch a numeric argument */
|
---|
39 | int c_argn = C_DFLTN; /* switch c numeric argument */
|
---|
40 | int l_argn = L_DFLTN; /* switch l numeric argument */
|
---|
41 | int num_arg = NUM_DFLT; /* general numeric argument */
|
---|
42 |
|
---|
43 | int x_argf = FALSE; /* switch x value */
|
---|
44 |
|
---|
45 | char *h_args = H_DFLTS; /* switch h string argument */
|
---|
46 |
|
---|
47 | char c_argc = C_DFLTC; /* switch c character argument */
|
---|
48 | char s_argc = S_DFLTC; /* switch s character argument */
|
---|
49 |
|
---|
50 | /* |
---|
51 |
|
---|
52 | */
|
---|
53 |
|
---|
54 | /* Standard stuff, unlikely to change much, if at all */
|
---|
55 |
|
---|
56 | int nfdone = 0; /* number of file arguments processed */
|
---|
57 | int exitarg = 0; /* exit() argument */
|
---|
58 | int nerrors = 0; /* number of errors encountered */
|
---|
59 |
|
---|
60 | #define L_PLURAL(N) ((N) ? ((N) GT 1 ? "s" : "") : "")
|
---|
61 | #define U_PLURAL(N) ((N) ? ((N) GT 1 ? "S" : "") : "")
|
---|
62 |
|
---|
63 | #define EN_MSGL(X) ((X) ? "enabled" : "disabled")
|
---|
64 | #define EN_MSGM(X) ((X) ? "Enabled" : "Disabled")
|
---|
65 | #define EN_MSGU(X) ((X) ? "ENABLED" : "DISABLED")
|
---|
66 |
|
---|
67 | #define TF_MSGL(X) ((X) ? "true" : "false")
|
---|
68 | #define TF_MSGM(X) ((X) ? "True" : "False")
|
---|
69 | #define TF_MSGU(X) ((X) ? "TRUE" : "FALSE")
|
---|
70 |
|
---|
71 | #define SW_MSGL(X) ((X) ? "on" : "off")
|
---|
72 | #define SW_MSGM(X) ((X) ? "On" : "Off")
|
---|
73 | #define SW_MSGU(X) ((X) ? "ON" : "OFF")
|
---|
74 |
|
---|
75 | #define GETCARG(N,C,D) if ((N = intopt(argv, &C)) < 0) N = D
|
---|
76 | #define GETARG(N,D) if ((N = atoix(argv)) < 0) N = D
|
---|
77 |
|
---|
78 | #define PGM_ERR(S) fprintf(stderr, "%s: ERROR - %s", PGMNAME, S)
|
---|
79 | #define PGM_WARN(S) fprintf(stderr, "%s: WARNING - %s", PGMNAME, S)
|
---|
80 | #define PGM_MSG(S) fprintf(stderr, "%s: %s", PGMNAME, S)
|
---|
81 |
|
---|
82 | #define NOFILE ""
|
---|
83 |
|
---|
84 | /* |
---|
85 |
|
---|
86 | */
|
---|
87 |
|
---|
88 | /*
|
---|
89 | =============================================================================
|
---|
90 | Die() -- print an error message and exit
|
---|
91 | =============================================================================
|
---|
92 | */
|
---|
93 |
|
---|
94 | Die(s)
|
---|
95 | char *s; /* message string */
|
---|
96 | {
|
---|
97 | ++nerrors;
|
---|
98 | PGM_ERR(s);
|
---|
99 | fprintf(stderr, "\n%s: Terminated with %d error%s.\n",
|
---|
100 | PGMNAME, nerrors, L_PLURAL(nerrors));
|
---|
101 |
|
---|
102 | exit(1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | =============================================================================
|
---|
107 | n_errs() -- create a "One|No|n errors was|were" message string
|
---|
108 |
|
---|
109 | If n is:
|
---|
110 |
|
---|
111 | LE 0 "No errors were"
|
---|
112 | EQ 1 "One error was"
|
---|
113 | GT 1 "n errors were" (n is the number of errors)
|
---|
114 | =============================================================================
|
---|
115 | */
|
---|
116 |
|
---|
117 | char *
|
---|
118 | n_errs(n)
|
---|
119 | int n; /* number of errors */
|
---|
120 | {
|
---|
121 | static char buf[100];
|
---|
122 |
|
---|
123 | if (n GT 1) {
|
---|
124 |
|
---|
125 | sprintf(buf, "%d errors were", n);
|
---|
126 | return(buf);
|
---|
127 |
|
---|
128 | } else if (n EQ 1)
|
---|
129 | return("One error was");
|
---|
130 | else
|
---|
131 | return("No errors were");
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* |
---|
135 |
|
---|
136 | */
|
---|
137 |
|
---|
138 | /*
|
---|
139 | =============================================================================
|
---|
140 | ffiler() -- create a "Can't open" message string for Die()
|
---|
141 | =============================================================================
|
---|
142 | */
|
---|
143 |
|
---|
144 | char *
|
---|
145 | ffiler(s)
|
---|
146 | char *s; /* file name string pointer */
|
---|
147 | {
|
---|
148 | static char buf[100];
|
---|
149 |
|
---|
150 | sprintf(buf, "Can't open %s", s);
|
---|
151 | return (buf);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*
|
---|
155 | =============================================================================
|
---|
156 | badopt() -- create an "Invalid option 'c'" message string for Die()
|
---|
157 | =============================================================================
|
---|
158 | */
|
---|
159 |
|
---|
160 | char *
|
---|
161 | badopt(c)
|
---|
162 | char c; /* character to complain about */
|
---|
163 | {
|
---|
164 | static char buf[100];
|
---|
165 |
|
---|
166 | sprintf(buf, "Invalid option \047%c\047", c);
|
---|
167 | return (buf);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /* |
---|
171 |
|
---|
172 | */
|
---|
173 |
|
---|
174 | /*
|
---|
175 | =============================================================================
|
---|
176 | Usage() -- print the usage message
|
---|
177 |
|
---|
178 | This shows a way to do it, complete with current defaults.
|
---|
179 | =============================================================================
|
---|
180 | */
|
---|
181 |
|
---|
182 | Usage()
|
---|
183 | {
|
---|
184 | fprintf(stderr, "\n%s usage:\n\n", PGMNAME);
|
---|
185 |
|
---|
186 | fprintf(stderr, " %s -an -ccn -h string -ln -sc -x\n\n", PGMNAME);
|
---|
187 |
|
---|
188 | fprintf(stderr, " -an argument a (%d)\n",
|
---|
189 | A_ALTN);
|
---|
190 | fprintf(stderr, " -ccn argument c (\047%c\047, %d)\n",
|
---|
191 | C_DFLTC, C_DFLTN);
|
---|
192 | fprintf(stderr, " -h string argument h (\042%s\042)\n",
|
---|
193 | h_args);
|
---|
194 | fprintf(stderr, " -ln argument l (%d)\n",
|
---|
195 | 0);
|
---|
196 | fprintf(stderr, " -sc argument s (\047%c\047)\n",
|
---|
197 | S_DFLTC);
|
---|
198 | fprintf(stderr, " -x argument x \042%s\042\n",
|
---|
199 | TF_MSGM(x_argf));
|
---|
200 |
|
---|
201 | fprintf(stderr, "\n");
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* |
---|
205 |
|
---|
206 | */
|
---|
207 |
|
---|
208 | /*
|
---|
209 | =============================================================================
|
---|
210 | Done() -- final processing
|
---|
211 |
|
---|
212 | This is where any end-of-program processing would be done.
|
---|
213 | =============================================================================
|
---|
214 | */
|
---|
215 |
|
---|
216 | Done()
|
---|
217 | {
|
---|
218 | printf("\n%s: Final values were -\n\n", PGMNAME);
|
---|
219 |
|
---|
220 | printf(" a_argn = %d\n", a_argn);
|
---|
221 | printf(" c_argn = %d\n", c_argn);
|
---|
222 | printf(" c_argc = \047%c\047\n", c_argc);
|
---|
223 | printf(" h_args = \042%s\042\n", h_args);
|
---|
224 | printf(" l_argn = %d\n", l_argn);
|
---|
225 | printf(" num_arg = %d\n", num_arg);
|
---|
226 | printf(" s_argc = \047%c\047\n", s_argc);
|
---|
227 | printf(" x_argf = %s\n", TF_MSGM(x_argf));
|
---|
228 |
|
---|
229 | if (nerrors) /* if any errors, exit with an error value */
|
---|
230 | exitarg = 1;
|
---|
231 |
|
---|
232 | printf("\n%s: Processing complete. %s encountered\n",
|
---|
233 | PGMNAME, n_errs(nerrors));
|
---|
234 | }
|
---|
235 |
|
---|
236 | /* |
---|
237 |
|
---|
238 | */
|
---|
239 |
|
---|
240 | /*
|
---|
241 | =============================================================================
|
---|
242 | Process() -- process a file name argument
|
---|
243 |
|
---|
244 | This is where each file in the argument list would be processed.
|
---|
245 | =============================================================================
|
---|
246 | */
|
---|
247 |
|
---|
248 | Process(s)
|
---|
249 | char *s; /* file name string pointer */
|
---|
250 | {
|
---|
251 | if (*s) { /* process a file */
|
---|
252 |
|
---|
253 | printf("%s processed\n", s);
|
---|
254 |
|
---|
255 | } else { /* process a null file (usually means use stdin) */
|
---|
256 |
|
---|
257 | printf("null file (%s) processed\n", NULLFILE);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* |
---|
262 |
|
---|
263 | */
|
---|
264 |
|
---|
265 | /*
|
---|
266 | =============================================================================
|
---|
267 | atoix() -- get a numeric argument or return 0
|
---|
268 | =============================================================================
|
---|
269 | */
|
---|
270 |
|
---|
271 | atoix(p)
|
---|
272 | register char **p; /* pointer to the string pointer */
|
---|
273 | {
|
---|
274 | register int n, c;
|
---|
275 |
|
---|
276 | n = 0;
|
---|
277 |
|
---|
278 | while (isdigit(c = *++*p))
|
---|
279 | n = 10 * n + c - '0';
|
---|
280 |
|
---|
281 | --*p;
|
---|
282 | return (n);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | =============================================================================
|
---|
287 | intopt() -- get an optional character and numeric argument
|
---|
288 |
|
---|
289 | option form: -cn
|
---|
290 |
|
---|
291 | where: c is the optional character
|
---|
292 | n is the (non-zero) numeric value
|
---|
293 | =============================================================================
|
---|
294 | */
|
---|
295 |
|
---|
296 | intopt(argv, optp)
|
---|
297 | char *argv[]; /* argv pointer */
|
---|
298 | char *optp; /* pointer to the character variable to be set */
|
---|
299 | {
|
---|
300 | int c;
|
---|
301 |
|
---|
302 | if ((c = (*argv)[1]) NE '\0' && !isdigit(c)) {
|
---|
303 | *optp = c;
|
---|
304 | ++*argv;
|
---|
305 | }
|
---|
306 |
|
---|
307 | return ((c = atoix(argv)) NE 0 ? c : -1);
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* |
---|
311 |
|
---|
312 | */
|
---|
313 |
|
---|
314 | /*
|
---|
315 | =============================================================================
|
---|
316 | findopt() -- extract and process options up to the first file argument
|
---|
317 | =============================================================================
|
---|
318 | */
|
---|
319 |
|
---|
320 | int
|
---|
321 | findopt(argc, argv)
|
---|
322 | int argc; /* argument count */
|
---|
323 | char *argv[]; /* argument pointer array pointer */
|
---|
324 | {
|
---|
325 | char **eargv; /* pointer to argument pointer */
|
---|
326 | int eargc; /* remaining arguments after options */
|
---|
327 | int c; /* current character being processed */
|
---|
328 | int strsw; /* string argument grabbed switch */
|
---|
329 |
|
---|
330 | eargv = argv; /* start with first argument */
|
---|
331 | eargc = 0; /* set file count to zero */
|
---|
332 | strsw = FALSE; /* indicate no string grabbed */
|
---|
333 |
|
---|
334 | while (--argc > 0) {
|
---|
335 |
|
---|
336 | switch (c = **++argv) {
|
---|
337 |
|
---|
338 | case '-': /* options start with '-' ... */
|
---|
339 |
|
---|
340 | case '+': /* ... or they can start with '+' */
|
---|
341 |
|
---|
342 | if ((c = *++*argv) EQ '\0') /* just '-' is end */
|
---|
343 | break;
|
---|
344 | /* |
---|
345 |
|
---|
346 | */
|
---|
347 | do {
|
---|
348 | if (isdigit(c)) { /* general numeric arg */
|
---|
349 |
|
---|
350 | --*argv;
|
---|
351 | num_arg = atoix(argv);
|
---|
352 |
|
---|
353 | } else
|
---|
354 |
|
---|
355 | switch (c) { /* switch on option letter */
|
---|
356 |
|
---|
357 | case 'a': /* -an */
|
---|
358 |
|
---|
359 | GETARG(a_argn, A_ALTN);
|
---|
360 | continue;
|
---|
361 |
|
---|
362 | case 'c': /* -ccn */
|
---|
363 |
|
---|
364 | GETCARG(c_argn, c_argc, C_ALTN);
|
---|
365 | continue;
|
---|
366 |
|
---|
367 | case 'h': /* -h string */
|
---|
368 |
|
---|
369 | if (--argc > 0) {
|
---|
370 |
|
---|
371 | strsw = TRUE;
|
---|
372 | h_args = argv[1];
|
---|
373 | }
|
---|
374 |
|
---|
375 | continue;
|
---|
376 |
|
---|
377 | case 'l': /* -ln */
|
---|
378 |
|
---|
379 | l_argn = atoix(argv);
|
---|
380 | continue;
|
---|
381 |
|
---|
382 | case 's': /* -sc */
|
---|
383 |
|
---|
384 | if ((s_argc = (*argv)[1]) NE '\0')
|
---|
385 | ++*argv;
|
---|
386 | else
|
---|
387 | s_argc = S_ALTC;
|
---|
388 |
|
---|
389 | continue;
|
---|
390 | /* |
---|
391 |
|
---|
392 | */
|
---|
393 |
|
---|
394 | case 'x': /* -x */
|
---|
395 |
|
---|
396 | x_argf = NOT x_argf;
|
---|
397 | continue;
|
---|
398 |
|
---|
399 | default : /* unrecognized option */
|
---|
400 |
|
---|
401 | Usage();
|
---|
402 | Die(badopt(c));
|
---|
403 | }
|
---|
404 |
|
---|
405 | } while ((c = *++*argv) NE '\0');
|
---|
406 |
|
---|
407 | if (strsw) { /* if we've eaten an argument */
|
---|
408 |
|
---|
409 | strsw = FALSE;
|
---|
410 | ++argv;
|
---|
411 | }
|
---|
412 |
|
---|
413 | continue;
|
---|
414 | }
|
---|
415 |
|
---|
416 | *eargv++ = *argv; /* update the argv list */
|
---|
417 | ++eargc; /* update the argument count */
|
---|
418 | }
|
---|
419 |
|
---|
420 | return (eargc);
|
---|
421 | }
|
---|
422 |
|
---|
423 | /* |
---|
424 |
|
---|
425 | */
|
---|
426 |
|
---|
427 | /*
|
---|
428 | =============================================================================
|
---|
429 | main() -- the main function for this program
|
---|
430 | =============================================================================
|
---|
431 | */
|
---|
432 |
|
---|
433 | main(argc, argv)
|
---|
434 | int argc;
|
---|
435 | char *argv[];
|
---|
436 | {
|
---|
437 | nfdone = 0;
|
---|
438 |
|
---|
439 | for (argc = findopt(argc, argv); argc > 0; --argc, ++argv) {
|
---|
440 |
|
---|
441 | Process(*argv);
|
---|
442 | ++nfdone;
|
---|
443 | }
|
---|
444 |
|
---|
445 | if (!nfdone) /* no files named, use NOFILE as argument */
|
---|
446 | Process(NOFILE);
|
---|
447 |
|
---|
448 | Done(); /* final processing */
|
---|
449 |
|
---|
450 | exit(exitarg);
|
---|
451 | }
|
---|