-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
693 lines (587 loc) · 16.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
* $Id: main.c,v 1.3 1992/10/07 22:06:08 craigs Exp $
*
* This code was written by Craig Southeren whilst under contract
* to Computer Sciences of Australia, Systems Engineering Division.
* It has been kindly released by CSA into the public domain.
*
* Neither CSA or me guarantee that this source code is fit for anything,
* so use it at your peril. I don't even work for CSA any more, so
* don't bother them about it. If you have any suggestions or comments
* (or money, cheques, free trips =8^) !!!!! ) please contact me
* care of [email protected]
*
* handling for different classification banners for 1st page added by
* Daniel Risacher ([email protected])
*
*/
#include "machdep.h"
#include "defs.h"
#include "version.h"
#include "print.h"
#include "main.h"
#include "paper.h"
#include "postscri.h"
/********************************
defines
********************************/
/* environment variables */
#define PRINTER "PRINTER" /* sets name of printer */
#define NENSCRIPT "NENSCRIPT" /* contains options */
/********************************
exports
********************************/
char *progname;
/********************************
usage
********************************/
void usage ()
{
printf ("usage: %s -?12BGghlNnRrSsVWwZ -f font -F titlefont -b header -i filetitle -L lines -p filename -P printer -S classification -U classification_1st_page -T papertype -t tabstop -# copies file...\n", progname);
exit (0);
}
/*********************************
variables set by options
*********************************/
static int landscape = False; /* True if in ladnscape mode, i.e. -r specified */
static int columns = 1; /* number of columns to print with */
static char *outputfilename = NULL; /* name of output file, if still NULL after parsing args then use lpr */
static char *bodyfont = NULL; /* font to use for text */
static char *titlefont = NULL; /* font to use for titles */
static int wrap = True; /* True if to wrap long lines */
static int titlesenabled = True; /* True if page titles enables */
static char *title = NULL; /* set to title if specified */
static int copies = 1; /* number of copies of each page */
static int gaudy = False; /* enables gaudy mode */
static int force_lines = 0; /* force number of lines per page */
static char *classification_1st = NULL; /* security classification for 1st page */
static char *classification = NULL; /* security classification for body of document */
static int line_numbers = False; /* true if to display line numbers */
static char *printername = DEF_PRINTER; /* name of printer to use when output goes to lpr */
static int tabstop = 8; /* tab stops */
static int passthroughflag = False; /* if True, then check for %! to allow passthrough of raw postscript */
static char *filetitle = NULL; /* set file title */
#ifdef US_VERSION
static char *papertype = "US";
#else
static char *papertype = "A4";
#endif
/*
* forward declarations if in ANSI mode
*/
#ifdef __STDC__
char * get_string (char **);
void parse_options (int *, char ***);
void parse_env (char *);
#endif
/********************************
parse_options
********************************/
#define ARGC (*argc)
#define ARGV (*argv)
void parse_options (argc, argv)
int *argc;
char ***argv;
{
char c;
char *s;
int i;
/* parse the options */
next_argv:
while (--ARGC > 0 && (*++ARGV)[0] == '-')
while (c = *++ARGV[0])
switch (c) {
/* -? : print help message */
case '?' :
case 'h' :
usage ();
break;
/* -1 : single column mode */
case '1' :
columns = 1;
break;
/* -2 : double column mode */
case '2' :
columns = 2;
break;
/* -#: set number of copies */
case '#' :
if (*++ARGV[0])
copies = atoi (ARGV[0]);
else {
if (ARGC < 1)
fprintf (stderr, "%s: -# option requires argument\n", progname);
else {
--ARGC;
copies = atoi (*++ARGV);
}
}
if (copies < 1) {
fprintf (stderr, "%s: illegal number of copies specified - defaulting to one\n", progname);
copies = 1;
}
goto next_argv;
break;
/* -b: set page title */
case 'b' :
if (*++ARGV[0])
title = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -b option requires argument\n", progname);
else {
--ARGC;
title = *++ARGV;
}
}
goto next_argv;
break;
/* -f : set font to use for printing body */
case 'f' :
if (*++ARGV[0])
bodyfont = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -f option requires argument\n", progname);
else {
--ARGC;
bodyfont = *++ARGV;
}
}
goto next_argv;
break;
/* -N : display line numbers */
case 'N' :
line_numbers = True;
break;
/* -n : disable line numbers */
case 'n' :
line_numbers = False;
break;
/* -p: send to file rather than lpr */
case 'p' :
if (*++ARGV[0])
outputfilename = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -p option requires argument\n", progname);
else {
--ARGC;
outputfilename = *++ARGV;
}
}
goto next_argv;
break;
/* -r : landscape mode */
case 'r' :
landscape = True;
break;
/* -w : wrap mode */
case 'w' :
wrap = True;
break;
/* -Z : check for postscript passthrough mode */
case 'Z' :
passthroughflag = True;
break;
/* -B : disable page headings and disable gaudy mode */
case 'B' :
titlesenabled = False;
break;
/* -F : set font to use for printing titles */
case 'F' :
if (*++ARGV[0])
titlefont = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -F option requires argument\n", progname);
else {
--ARGC;
titlefont = *++ARGV;
}
}
goto next_argv;
break;
/* -g : disable gaudy mode */
case 'g' :
gaudy = False;
break;
/* -G : enable gaudy mode */
case 'G' :
gaudy = True;
break;
/* -i: set file title when inputting from stdin */
case 'i' :
if (*++ARGV[0])
filetitle = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -i option requires argument\n", progname);
else {
--ARGC;
filetitle = *++ARGV;
}
}
goto next_argv;
break;
/* -l : don't set number of lines of page, i.e. use maximum */
case 'l' :
force_lines = 0;
break;
/* -L : force number of lines per page */
case 'L' :
if (*++ARGV[0])
force_lines = atoi(ARGV[0]);
else {
if (ARGC < 1)
fprintf (stderr, "%s: -x option requires argument\n", progname);
else {
--ARGC;
force_lines = atoi (*++ARGV);
}
}
if (force_lines <= 0) {
fprintf (stderr, "%s: ignoring illegal arguement to -L option\n", progname);
force_lines = 0;
}
goto next_argv;
break;
/* -P: set printer name - overrides PRINTER environment variables */
case 'P' :
if (*++ARGV[0])
printername = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -P option requires argument\n", progname);
else {
--ARGC;
printername = *++ARGV;
}
}
goto next_argv;
break;
/* -R : portrait mode */
case 'R' :
landscape = False;
break;
/* -U: set security classification for first page only*/
case 'U' :
if (*++ARGV[0])
classification_1st = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -U option requires argument\n", progname);
else {
--ARGC;
classification_1st = *++ARGV;
}
}
goto next_argv;
break;
/* -S: set security classification */
case 'S' :
if (*++ARGV[0])
classification = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -S option requires argument\n", progname);
else {
--ARGC;
classification = *++ARGV;
}
}
goto next_argv;
break;
/* -s : disable security classification printing */
case 's' :
classification_1st = NULL;
classification = NULL;
break;
/* -T: set paper size to either US or A4 */
case 'T' :
if (*++ARGV[0])
papertype = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -T option requires argument\n", progname);
else {
--ARGC;
papertype = *++ARGV;
}
}
goto next_argv;
break;
/* -t: set tab stop size */
case 't' :
if (*++ARGV[0])
s = ARGV[0];
else {
if (ARGC < 1) {
fprintf (stderr, "%s: -t option requires argument\n", progname);
s = 0; /* We should flag the error in some way */
} else {
--ARGC;
s = *++ARGV;
}
}
if ((i = atoi (s)) <= 0)
fprintf (stderr, "%s: illegal tab setting %s ignored\n", progname, s);
else
tabstop = i;
goto next_argv;
break;
/* -V : print version number */
case 'V' :
fputs (version_string, stderr);
fputs ("\n", stderr);
/* fputs (copyright_string, stderr); */
exit (0);
break;
/* -W : turn off wrap mode */
case 'W' :
wrap = False;
break;
/* single char option */
/* case 'x' :
allow_checkouts = True;
break;
*/
/* option with string */
/* case 'x' :
if (*++ARGV[0])
optionstring = ARGV[0];
else {
if (ARGC < 1)
fprintf (stderr, "%s: -x option requires argument\n", progname);
else {
--ARGC;
optionstring = *++ARGV;
}
}
goto next_argv;
break;
*/
default :
fprintf (stderr, "%s: unknown option %c\n", progname, c);
usage();
break;
}
}
/********************************
get_string
return the next string from the argument
********************************/
char * get_string (str)
char **str;
#define STR (*str)
{
char *ret = NULL;
int in_dquote, in_squote, in_bquote;
int quote_count;
/* skip leading whitespace */
while (*STR == ' ' ||
*STR == '\t')
STR++;
/* return if end of string */
if (*STR == '\0')
return NULL;
/* collect characters until end of string or whitespace */
in_dquote = -1;
in_squote = -1;
in_bquote = -1;
quote_count = 0;
ret = STR;
while (*STR != '\0' &&
(quote_count > 0 || (*STR != ' ' && *STR != '\t'))
) {
switch (*STR) {
case '"':
in_dquote = -in_dquote;
quote_count += in_dquote;
break;
case '\'':
in_squote = -in_squote;
quote_count += in_squote;
break;
case '`':
in_bquote = -in_bquote;
quote_count += in_bquote;
break;
case '\\':
if (STR[1] != '\0')
STR++;
break;
}
STR++;
}
if (*STR != '\0') {
*STR = '\0';
STR++;
}
if (quote_count > 0)
fprintf (stderr, "unmatched quote in %s environment variable\n", NENSCRIPT);
return ret;
}
/********************************
parse_env
process the options specified in the supplied string
********************************/
void parse_env (env)
char *env;
{
int argc;
char *s;
char **argv;
if (env == NULL)
return;
/* insert a "fake" argv[0] which corresponds to the real
argv[0] passed to main. The allows us to use same routine
for handling the real arguments */
argv = (char **)malloc (sizeof (char *));
argc = 1;
argv[0] = progname;
/* split the single environment string into separate strings so we can pass
them to parse_options */
while ((s = get_string (&env)) != NULL) {
argv = (char **)realloc ((void *)argv, (argc+1) * sizeof (char *));
argv[argc] = s;
argc++;
}
/* parse the options */
parse_options (&argc, &argv);
}
/********************************
passthrough
********************************/
static void passthrough (input, output)
FILE * input;
FILE * output;
{
char buff [512];
int len;
while ((len = fread (buff, 1, 512, input)) > 0)
fwrite (buff, 1, len, output);
}
/********************************
main
********************************/
int main (argc, argv)
int argc;
char *argv[];
{
char *env;
char cmd[1024];
struct PaperMetrics * papermetrics;
int i;
int ch1;
int ch2;
FILE *outputstream; /* opened output stream - either file or lpr */
FILE *inputstream; /* opened input stream, 0 if max */
/* extract the program name */
if ((progname = strrchr (argv[0], '/')) == NULL)
progname = argv[0];
else
progname++;
/* get the printer environment variable */
if ((env = getenv (PRINTER)) != NULL)
printername = env;
/* handle the NENSCRIPT environment variable */
if ((env = getenv (NENSCRIPT)) != NULL)
parse_env (STRDUP(env));
/* parse arguments */
parse_options (&argc, &argv);
/* open either the output file or a pipe to lpr */
if (outputfilename != NULL) {
if (strcmp (outputfilename, "-") == 0)
outputstream = stdout;
else if ((outputstream = fopen (outputfilename, "w")) == NULL) {
perror (outputfilename);
exit (1);
}
} else {
#ifdef MSDOS
if ((outputstream = fopen (printername, "w")) == NULL) {
perror (printername);
exit (1);
}
#else
sprintf (cmd, "%s %s", LPR, printername);
if ((outputstream = popen (cmd, "w")) == NULL) {
perror (LPR);
exit (1);
}
#endif
}
/* can't be in passthrough mode with more than one argument */
if (argc > 1 && passthroughflag) {
fprintf (stderr, "%s: ignoring -Z flag as there is more than one argument", progname);
passthroughflag = False;
}
/* if in passthrough mode, open the input stream and check for %! */
if (passthroughflag) {
if (argc < 1)
inputstream = stdin;
else if ((inputstream = fopen (*argv, "r")) == NULL) {
perror (*argv);
exit (1);
}
ch1 = fgetc (inputstream);
ch2 = fgetc (inputstream);
ungetc (ch2, inputstream);
ungetc (ch1, inputstream);
if (ch1 == '%' && ch2 == '!') {
passthrough (inputstream, outputstream);
exit (0);
}
} else
inputstream = NULL;
/* check the paper type and get ptr to record */
papermetrics = NULL;
for (i = 0; PaperTypes[i].Name != NULL; i++)
if (STRICMP ((papermetrics = &PaperTypes[i])->Name, papertype) == 0)
break;
/* if the papermetric was not set, give error */
if (PaperTypes[i].Name == NULL) {
fprintf (stderr, "%s: paper type %s not known\n", progname, papertype);
exit (1);
}
/* set up the fonts */
if (bodyfont == NULL)
bodyfont = (columns == 2 && landscape) ? papermetrics->LandscapeFont : papermetrics->PortraitFont;
if (titlefont == NULL)
titlefont = papermetrics->TitleFont;
/* display the header */
StartJob (outputstream,
*argv,
landscape,
columns,
bodyfont,
titlefont,
wrap,
titlesenabled,
title,
copies,
gaudy,
force_lines,
classification_1st,
classification,
papermetrics,
tabstop);
/* if no arguments, then accept stdin */
if (argc < 1)
print_file (stdin, outputstream, (filetitle ? filetitle : "stdin"), line_numbers);
else for (;argc > 0;argc--) {
if (inputstream == NULL && (inputstream = fopen (*argv, "r")) == NULL)
perror (*argv);
else {
print_file (inputstream, outputstream, (filetitle ? filetitle : *argv), line_numbers);
fclose(inputstream);
inputstream = NULL;
}
argv++;
}
/* output trailer */
EndJob (outputstream);
/* finished */
return (0);
}