3e8fe090dd79b8239a8920e0f58c2b17971e7e7e
[enscript.git] / src / main.c
1 /*
2  * Argument handling and main.
3  * Copyright (c) 1995-2003 Markku Rossi.
4  *
5  * Author: Markku Rossi <mtr@iki.fi>
6  */
7
8 /*
9  * This file is part of GNU Enscript.
10  *
11  * Enscript is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * Enscript is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Enscript.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include "gsint.h"
26 #include "getopt.h"
27
28 /*
29  * Prototypes for static functions.
30  */
31
32 /*
33  * Open output file according to user options.  Void if output file
34  * has already been opened.
35  */
36 static void open_output_file ();
37
38 /* Close output file. */
39 static void close_output_file ();
40
41 /* Handle options from environment variable <var> */
42 static void handle_env_options ___P ((char *var));
43
44 /* Handle options from <argv> array. */
45 static void handle_options ___P ((int argc, char *argv[]));
46
47 /* Print usage info. */
48 static void usage ();
49
50 /* Print version info. */
51 static void version ();
52
53
54 /*
55  * Global variables.
56  */
57
58 char *program;                  /* Program's name, used for messages. */
59 FILE *ofp = NULL;               /* Output file. */
60 void *printer_context;          /* Context for the printer. */
61 char *date_string = NULL;       /* Preformatted time string. */
62 struct tm run_tm;               /* Time when program is run. */
63 struct tm mod_tm;               /* Last modification time for current file. */
64 struct passwd *passwd;          /* Passwd entry for the user running this
65                                    program. */
66
67 /* Path to our library. */
68 char *enscript_library = LIBRARY;
69
70 /* Library lookup path. */
71 char *libpath = NULL;
72
73 /* AFM library lookup path. */
74 char *afm_path = NULL;
75
76 MediaEntry *media_names = NULL; /* List of known media. */
77 MediaEntry *media = NULL;       /* Entry for used media. */
78 int bs = 8;                     /* The backspace character. */
79
80 /* Statistics. */
81 int total_pages = 0;            /* Total number of pages printed. */
82 int num_truncated_lines = 0;    /* Number of lines truncated. */
83 int num_missing_chars = 0;      /* Number of unknown characters. */
84 int missing_chars[256] = {0};   /* Table of unknown characters. */
85 int num_non_printable_chars = 0; /* Number of non-printable characters. */
86 int non_printable_chars[256] = {0}; /* Table of non-printable characters. */
87
88 /* Output media dimensions that are used during PostScript emission. */
89 int d_page_w = 0;               /* page's width */
90 int d_page_h = 0;               /* page's height */
91 int d_header_w = 0;             /* fancy header's width */
92 int d_header_h = 0;             /* fancy header's height */
93 int d_footer_h = 0;             /* fancy footer's height */
94 int d_output_w = 0;             /* output area's width */
95 int d_output_h = 0;             /* output area's height  */
96 int d_output_x_margin = 5;      /* output area's x marginal */
97 int d_output_y_margin = 2;      /* output area's y marginal */
98
99 /* Document needed resources. */
100 StringHashPtr res_fonts;        /* fonts */
101
102 /* Fonts to download. */
103 StringHashPtr download_fonts;
104
105 /* Additional key-value pairs, passed to the generated PostScript code. */
106 StringHashPtr pagedevice;       /* for setpagedevice */
107 StringHashPtr statusdict;       /* for statusdict */
108
109 /* User defined strings. */
110 StringHashPtr user_strings;
111
112 /* Cache for AFM files. */
113 StringHashPtr afm_cache = NULL;
114 StringHashPtr afm_info_cache = NULL;
115
116 /* AFM library handle. */
117 AFMHandle afm = NULL;
118
119
120 /* Options. */
121
122 /*
123  * Free single-letter options are: Q, x, y, Y
124  */
125
126 /*
127  * -#
128  *
129  * An alias for -n, --copies.
130  */
131
132 /*
133  * -1, -2, -3, -4, -5, -6, -7, -8, -9, --columns=NUM
134  *
135  * Number of columns per page.  The default is 1 column.
136  */
137 int num_columns = 1;
138
139 /*
140  * -a PAGES, --pages=PAGES
141  *
142  * Specify which pages are printed.
143  */
144 PageRange *page_ranges = NULL;
145
146 /*
147  * -A ALIGN, --file-align=ALIGN
148  *
149  * Align input files to start from ALIGN page count.  This is handy
150  * for two-side printings.
151  */
152 unsigned int file_align = 1;
153
154 /*
155  * -b STRING, --header=STRING
156  *
157  * Set the string that is used as the page header.  As a default, page
158  * header is constructed from filename, date and page number.
159  */
160 char *page_header = NULL;
161
162 /*
163  * -B, --no-header
164  *
165  * Do not print page headers.
166  */
167
168 /*
169  * -c, --truncate-lines
170  *
171  * Truncate lines that are longer than the page width.  Default is character
172  * wrap.
173  */
174 LineEndType line_end = LE_CHAR_WRAP;
175
176 /*
177  * -C [START], --line-numbers[=START]
178  *
179  * Precede each line with its line number.  As a default, do not mark
180  * line numbers.  If the optional argument START is given, it
181  * specifies the number from which the line numbers are assumed to
182  * start in the file.  This is useful if the file contains a region
183  * of a bigger file.
184  */
185 int line_numbers = 0;
186 unsigned int start_line_number = 1;
187
188 /*
189  * -d, -P, --printer
190  *
191  * Name of the printer to which output is send.  Defaults to system's
192  * default printer.
193  */
194 char *printer = NULL;
195
196 /*
197  * -e [CHAR], --escapes[=CHAR]
198  *
199  * Enable special escape ('\000') interpretation.  If option CHAR is given
200  * it is assumed to specify the escape character.
201  */
202 int special_escapes = 0;
203 int escape_char = '\0';
204 int default_escape_char;
205
206 /*
207  * -E [LANG], --highlight=[LANG] (deprecated --pretty-print[=LANG])
208  *
209  * Highlight program source code.  Highlighting is handled by creating
210  * an input filter with the states-program.  States makes an educated
211  * guess about the start state but sometimes it fails, so the start
212  * state can also be specified to be LANG.  This option overwrites
213  * input filter and enables special escapes.
214  */
215
216 int highlight = 0;
217 char *hl_start_state = NULL;
218
219 /*
220  * -f, --font
221  *
222  * Select body font.
223  */
224 char *Fname = "Courier";
225 FontPoint Fpt = {10.0, 10.0};
226 FontPoint default_Fpt;          /* Point size of the original font. */
227 char *default_Fname;            /* Name of the original font. */
228 InputEncoding default_Fencoding; /* The encoding of the original font. */
229 int user_body_font_defined = 0; /* Has user defined new body font? */
230
231 double font_widths[256];        /* Width array for body font. */
232 char font_ctype[256];           /* Font character types. */
233 int font_is_fixed;              /* Is body font a fixed pitch font? */
234 double font_bbox_lly;           /* Font's bounding box's lly-coordinate. */
235
236 /*
237  * -F, --header-font
238  *
239  * Select font to be used to print the standard simple header.
240  */
241 char *HFname = "Courier-Bold";
242 FontPoint HFpt = {10.0, 10.0};
243
244 /*
245  * -g, --print-anyway
246  *
247  * Print document even it contains binary data.  This does nothing
248  * since enscript prints files anyway.
249  */
250
251 /*
252  * -G, --fancy-header
253  *
254  * Add a fancy header to top of every page.  There are several header styles
255  * but the default is 'no fancy header'.
256  */
257 HeaderType header = HDR_SIMPLE;
258 char *fancy_header_name = NULL;
259 char *fancy_header_default = NULL;
260
261 /*
262  * -h, --no-job-header
263  *
264  * Supress the job header page.
265  */
266 static int no_job_header = 0;
267
268 /*
269  * -H num, --highlight-bars=num
270  *
271  * Print highlight bars under text.  Bars will be <num> lines high.
272  * As a default, do not print bars.
273  */
274 unsigned int highlight_bars = 0;
275
276 /*
277  * -i, --indent
278  *
279  * Indent every line this many characters.
280  */
281 double line_indent = 0.0;
282 char *line_indent_spec = "0";
283
284 /*
285  * -I CMD, --filter=CMD
286  *
287  * Read input files through input filter CMD.
288  */
289 char *input_filter = NULL;
290
291 /*
292  * -j, --borders
293  *
294  * Print borders around columns.
295  */
296 int borders = 0;
297
298 /*
299  * -J
300  *
301  * An alias for -t, --title.
302  */
303
304 /*
305  * -k, --page-prefeed
306  * -K, --no-page-prefeed
307  *
308  * Control page prefeed.
309  */
310 int page_prefeed = 0;
311
312 /*
313  * -l, --lineprinter
314  *
315  * Emulate lineprinter -  make pages 66 lines long and omit headers.
316  */
317
318 /*
319  * -L, --lines-per-page
320  *
321  * Specify how many lines should be printed on a single page.  Normally
322  * enscript counts it from font point sizes.
323  */
324 unsigned int lines_per_page = (unsigned int) -1;
325
326 /*
327  * -m, --mail
328  *
329  * Send mail notification to user after print job has been completed.
330  */
331 int mail = 0;
332
333 /*
334  * -M, --media
335  *
336  * Name of the output media.  Default is A4.
337  */
338 char *media_name = NULL;
339
340 /*
341  * -n, --copies
342  *
343  * Number of copies to print.
344  */
345 int num_copies = 1;
346
347 /*
348  * -N, --newline
349  *
350  * Set the newline character: '\n' or '\r'.  As a default, the newline
351  * character is specified by the input encoding.
352  */
353 int nl = -1;
354
355 /*
356  * -o, -p, --output
357  *
358  * Leave output to the specified file.  As a default result is spooled to
359  * printer.
360  */
361 char *output_file = OUTPUT_FILE_NONE;
362
363 /*
364  * -O, --missing-characters
365  *
366  * List all missing characters.  Default is no listing.
367  */
368 int list_missing_characters = 0;
369
370 /*
371  * -q, --quiet
372  *
373  * Do not tell what we are doing.  Default is to tell something but
374  * not --verbose.
375  */
376 int quiet = 0;
377
378 /*
379  * -r, --landscape
380  * -R, --portrait
381  *
382  * Print with page rotated 90 degrees (landscape mode).  Default is
383  * portrait.
384  */
385 int landscape = 0;
386
387 /*
388  * -s, --baselineskip
389  *
390  * Specify baselineskip value that is used when enscript moves to
391  * a new line.  Current point movement is font_point_size + baselineskip.
392  */
393 double baselineskip = 1.0;
394
395 /*
396  * -t, --title
397  *
398  * Title which is printed to the banner page.  If this option is given
399  * from the command line, this sets also the name of the stdin which
400  * is by the default "".
401  */
402 char *title = "Enscript Output";
403 int title_given = 0;
404
405 /*
406  * -T, --tabsize
407  *
408  * Specify tabulator size.
409  */
410 int tabsize = 8;
411
412 /*
413  * -u, --underlay
414  *
415  * Place text under every page.  Default is no underlay.
416  */
417 double ul_gray = .8;
418 FontPoint ul_ptsize = {200.0, 200.0};
419 char *ul_font = "Times-Roman";
420 char *underlay = NULL;
421 char *ul_position = NULL;       /* Position info as a string. */
422 double ul_x;                    /* Position x-coordinate. */
423 double ul_y;                    /* Position y-coordinate. */
424 double ul_angle;
425 unsigned int ul_style = UL_STYLE_OUTLINE;
426 char *ul_style_str = NULL;
427 int ul_position_p = 0;          /* Is ul-position given? */
428 int ul_angle_p = 0;             /* Is ul-angle given? */
429
430 /*
431  * -U NUM, --nup=NUM
432  *
433  * Print NUM PostScript pages on each output page (n-up printing).
434  */
435 unsigned int nup = 1;
436 unsigned int nup_exp = 0;
437 unsigned int nup_rows = 1;
438 unsigned int nup_columns = 1;
439 int nup_landscape = 0;
440 unsigned int nup_width;
441 unsigned int nup_height;
442 double nup_scale;
443
444 /*
445  * -v, --verbose
446  *
447  * Tell what we are doing.  Default is no verbose outputs.
448  */
449 int verbose = 0;
450
451 /*
452  * -V, --version
453  *
454  * Print version information.
455  */
456
457 /*
458  * -w LANGUAGE, --language=LANGUAGE
459  *
460  * Generate output for language LANGUAGE.  The default is PostScript.
461  */
462 char *output_language = "PostScript";
463 int output_language_pass_through = 0;
464
465 /*
466  * -W APP,option, --options=APP,OPTION
467  *
468  * Pass additional option to enscript's helper applications.  The
469  * first part of the option's argument (APP) specifies the
470  * helper application to which the options are added.  Currently the
471  * following helper application are defined:
472  *
473  *   s  states
474  */
475 Buffer *helper_options[256] = {0};
476
477 /*
478  * -X, --encoding
479  *
480  * Specifies input encoding.  Default is ISO-8859.1.
481  */
482 InputEncoding encoding = ENC_ISO_8859_1;
483 char *encoding_name = NULL;
484
485 /*
486  * -z, --no-formfeed
487  *
488  * Do not interpret form feed characters.  As a default, form feed
489  * characters are interpreted.
490  */
491 int interpret_formfeed = 1;
492
493 /*
494  * -Z, --pass-through
495  *
496  * Pass through all PostScript and PCL files without any modifications.
497  * As a default, don't.
498  */
499 int pass_through = 0;
500
501 /*
502  * --color[=bool]
503  *
504  * Create color output with states?
505  */
506
507 /*
508  * --continuous-page-numbers
509  *
510  * Count page numbers across input files.  Don't restart numbering
511  * at beginning of each file.
512  */
513 int continuous_page_numbers = 0;
514
515 /*
516  * --download-font=FONT
517  *
518  * Download font FONT to printer.
519  */
520
521 /*
522  * --extended-return-values
523  *
524  * Enable extended return values.
525  */
526 int extended_return_values = 0;
527
528 /*
529  * --filter-stdin=STR
530  *
531  * How stdin is shown to the filter command.  The default is "" but
532  * some utilities might want it as "-".
533  */
534 char *input_filter_stdin = "";
535
536 /*
537  * --footer=STRING
538  *
539  * Set the string that is used as the page footer.  As a default, the
540  * page has no footer.  Setting this option does not necessary show
541  * any footer strings in the output.  It depends on the selected
542  * header (`.hdr' file) whether it supports footer strings or not.
543  */
544 char *page_footer = NULL;
545
546 /*
547  * --h-column-height=HEIGHT
548  *
549  * Set the horizontal column (channel) height to be HEIGHT.  This option
550  * also sets the FormFeedType to `hcolumn'.  The default value is set to be
551  * big enough to cause a jump to the next vertical column (100m).
552  */
553 double horizontal_column_height = 283465.0;
554
555 /*
556  * --help-highlight (deprecated --help-pretty-print)
557  *
558  * Descript all supported -E, --highlight languages and file formats.
559  */
560 int help_highlight = 0;
561
562 /*
563  * --highlight-bar-gray=val
564  *
565  * Specify the gray level for highlight bars.
566  */
567 double highlight_bar_gray = .97;
568
569 /*
570  * --list-media
571  *
572  * List all known media.  As a default do not list media names.
573  */
574 int list_media = 0;
575
576 /*
577  * --margins=LEFT:RIGHT:TOP:BOTTOM
578  *
579  * Adjust page marginals.
580  */
581 char *margins_spec = NULL;
582
583 /*
584  * --mark-wrapped-lines[=STYLE]
585  *
586  * Mark wrapped lines so that they can be easily detected from the printout.
587  * Optional parameter STYLE specifies the marking style, the system default
588  * is black box.
589  */
590 char *mark_wrapped_lines_style_name = NULL;
591 MarkWrappedLinesStyle mark_wrapped_lines_style = MWLS_NONE;
592
593 /*
594  * --non-printable-format=FORMAT
595  *
596  * Format in which non-printable characters are printed.
597  */
598 char *npf_name = NULL;
599 NonPrintableFormat non_printable_format = NPF_OCTAL;
600
601 /*
602  * --nup-columnwise
603  *
604  * Layout N-up pages colunwise instead of row-wise.
605  */
606 int nup_columnwise = 0;
607
608 /*
609  * --nup-xpad=NUM
610  *
611  * The x-padding between N-up subpages.
612  */
613 unsigned int nup_xpad = 10;
614
615 /*
616  * --nup-ypad=NUM
617  *
618  * The y-padding between N-up subpages.
619  */
620 unsigned int nup_ypad = 10;
621
622 /*
623  * --page-label-format=FORMAT
624  *
625  * Format in which page labels are printed; the default is "short".
626  */
627 char *page_label_format = NULL;
628 PageLabelFormat page_label;
629
630 /*
631  * --ps-level=LEVEL
632  *
633  * The PostScript language level that enscript should use; the default is 2.
634  */
635 unsigned int pslevel = 2;
636
637 /*
638  * --printer-options=OPTIONS
639  *
640  * Pass extra options OPTIONS to the printer spooler.
641  */
642 char *printer_options = NULL;
643
644 /*
645  * --rotate-even-pages
646  *
647  * Rotate each even-numbered page 180 degrees.  This might be handy in
648  * two-side printing when the resulting pages are bind from some side.
649  * Greetings to Jussi-Pekka Sairanen.
650  */
651 int rotate_even_pages = 0;
652
653 /*
654  * --slice=NUM
655  *
656  * Horizontal input slicing.  Print only NUMth wrapped input pages.
657  */
658 int slicing = 0;
659 unsigned int slice = 1;
660
661 /*
662  * --swap-even-page-margins
663  *
664  * Swap left and right side margins for each even numbered page.  This
665  * might be handy in two-side printing.
666  */
667 int swap_even_page_margins = 0;
668
669 /*
670  * --toc
671  *
672  * Print Table of Contents page.
673  */
674 int toc = 0;
675 FILE *toc_fp;
676 char *toc_fmt_string;
677
678 /*
679  * --word-wrap
680  *
681  * Wrap long lines from word boundaries.  The default is character wrap.
682  */
683
684 /*
685  * AcceptCompositeCharacters: bool
686  *
687  * Specify whatever we accept composite characters or should them be
688  * considered as non-existent.  As a default, do not accept them.
689  */
690 int accept_composites = 0;
691
692 /*
693  * AppendCtrlD: bool
694  *
695  * Append ^D character to the end of the output.  Some printers require this
696  * but the default is false.
697  */
698 int append_ctrl_D = 0;
699
700 /*
701  * Clean7Bit: bool
702  *
703  * Specify how characters greater than 127 are printed.
704  */
705 int clean_7bit = 1;
706
707 /*
708  * FormFeedType: type
709  *
710  * Specify what to do when a formfeed character is encountered from the
711  * input stream.  The default action is to jump to the beginning of the
712  * next column.
713  */
714 FormFeedType formfeed_type = FORMFEED_COLUMN;
715
716 /*
717  * GeneratePageSize: bool
718  *
719  * Specify whether the `PageSize' pagedevice definitions should be
720  * generated to the output.
721  */
722 int generate_PageSize = 1;
723
724 /*
725  * NoJobHeaderSwitch: switch
726  *
727  * Spooler switch to suppress the job header (-h).
728  */
729 char *no_job_header_switch = NULL;
730
731 /*
732  * OutputFirstLine: line
733  *
734  * Set the PostScript output's first line to something your system can handle.
735  * The default is "%!PS-Adobe-3.0"
736  */
737 char *output_first_line = NULL;
738
739 /*
740  * QueueParam: param
741  *
742  * The spooler command switch to select the printer queue (-P).
743  */
744 char *queue_param = NULL;
745
746 /*
747  * Spooler: command
748  *
749  * The spooler command name (lpr).
750  */
751 char *spooler_command = NULL;
752
753 /*
754  * StatesBinary: path
755  *
756  * An absolute path to the `states' binary.
757  */
758
759 char *states_binary = NULL;
760
761 /*
762  * StatesColor: bool
763  *
764  * Should the States program generate color outputs.
765  */
766 int states_color = 0;
767
768 /*
769  * StatesConfigFile: file
770  *
771  * The name of the states' configuration file.
772  */
773 char *states_config_file = NULL;
774
775 /*
776  * StatesHighlightStyle: style
777  *
778  * The highlight style.
779  */
780 char *states_highlight_style = NULL;
781
782 /*
783  * StatesPath: path
784  *
785  * Define the path for the states program.  The states program will
786  * lookup its state definition files from this path.
787  */
788 char *states_path = NULL;
789
790 /* ^@shade{GRAY}, set the line highlight gray. */
791 double line_highlight_gray = 1.0;
792
793 /* ^@bggray{GRAY}, set the text background gray. */
794 double bggray = 1.0;
795
796 EncodingRegistry encodings[] =
797 {
798   {{"88591", "latin1", NULL},           ENC_ISO_8859_1,         '\n', 8},
799   {{"88592", "latin2", NULL},           ENC_ISO_8859_2,         '\n', 8},
800   {{"88593", "latin3", NULL},           ENC_ISO_8859_3,         '\n', 8},
801   {{"88594", "latin4", NULL},           ENC_ISO_8859_4,         '\n', 8},
802   {{"88595", "cyrillic", NULL},         ENC_ISO_8859_5,         '\n', 8},
803   {{"88597", "greek", NULL},            ENC_ISO_8859_7,         '\n', 8},
804   {{"88599", "latin5", NULL},           ENC_ISO_8859_9,         '\n', 8},
805   {{"885910", "latin6", NULL},          ENC_ISO_8859_10,        '\n', 8},
806   {{"ascii", NULL, NULL},               ENC_ASCII,              '\n', 8},
807   {{"asciifise", "asciifi", "asciise"}, ENC_ASCII_FISE,         '\n', 8},
808   {{"asciidkno", "asciidk", "asciino"}, ENC_ASCII_DKNO,         '\n', 8},
809   {{"ibmpc", "pc", "dos"},              ENC_IBMPC,              '\n', 8},
810   {{"mac", NULL, NULL},                 ENC_MAC,                '\r', 8},
811   {{"vms", NULL, NULL},                 ENC_VMS,                '\n', 8},
812   {{"hp8", NULL, NULL},                 ENC_HP8,                '\n', 8},
813   {{"koi8", NULL, NULL},                ENC_KOI8,               '\n', 8},
814   {{"ps", "PS", NULL},                  ENC_PS,                 '\n', 8},
815   {{"pslatin1", "ISOLatin1Encoding", NULL},     ENC_ISO_8859_1, '\n', 8},
816
817   {{NULL, NULL, NULL}, 0, 0, 0},
818 };
819
820
821 /*
822  * Static variables.
823  */
824
825 static struct option long_options[] =
826 {
827   {"columns",                   required_argument,      0, 0},
828   {"pages",                     required_argument,      0, 'a'},
829   {"file-align",                required_argument,      0, 'A'},
830   {"header",                    required_argument,      0, 'b'},
831   {"no-header",                 no_argument,            0, 'B'},
832   {"truncate-lines",            no_argument,            0, 'c'},
833   {"line-numbers",              optional_argument,      0, 'C'},
834   {"printer",                   required_argument,      0, 'd'},
835   {"setpagedevice",             required_argument,      0, 'D'},
836   {"escapes",                   optional_argument,      0, 'e'},
837   {"highlight",                 optional_argument,      0, 'E'},
838   {"font",                      required_argument,      0, 'f'},
839   {"header-font",               required_argument,      0, 'F'},
840   {"print-anyway",              no_argument,            0, 'g'},
841   {"fancy-header",              optional_argument,      0, 'G'},
842   {"no-job-header",             no_argument,            0, 'h'},
843   {"highlight-bars",            optional_argument,      0, 'H'},
844   {"indent",                    required_argument,      0, 'i'},
845   {"filter",                    required_argument,      0, 'I'},
846   {"borders",                   no_argument,            0, 'j'},
847   {"page-prefeed",              no_argument,            0, 'k'},
848   {"no-page-prefeed",           no_argument,            0, 'K'},
849   {"lineprinter",               no_argument,            0, 'l'},
850   {"lines-per-page",            required_argument,      0, 'L'},
851   {"mail",                      no_argument,            0, 'm'},
852   {"media",                     required_argument,      0, 'M'},
853   {"copies",                    required_argument,      0, 'n'},
854   {"newline",                   required_argument,      0, 'N'},
855   {"output",                    required_argument,      0, 'p'},
856   {"missing-characters",        no_argument,            0, 'O'},
857   {"quiet",                     no_argument,            0, 'q'},
858   {"silent",                    no_argument,            0, 'q'},
859   {"landscape",                 no_argument,            0, 'r'},
860   {"portrait",                  no_argument,            0, 'R'},
861   {"baselineskip",              required_argument,      0, 's'},
862   {"statusdict",                required_argument,      0, 'S'},
863   {"title",                     required_argument,      0, 't'},
864   {"tabsize",                   required_argument,      0, 'T'},
865   {"underlay",                  optional_argument,      0, 'u'},
866   {"nup",                       required_argument,      0, 'U'},
867   {"verbose",                   optional_argument,      0, 'v'},
868   {"version",                   no_argument,            0, 'V'},
869   {"language",                  required_argument,      0, 'w'},
870   {"option",                    required_argument,      0, 'W'},
871   {"encoding",                  required_argument,      0, 'X'},
872   {"no-formfeed",               no_argument,            0, 'z'},
873   {"pass-through",              no_argument,            0, 'Z'},
874
875   /* Long options without short counterparts.  Next free is 157. */
876   {"color",                     optional_argument,      0, 142},
877   {"continuous-page-numbers",   no_argument,            0, 156},
878   {"download-font",             required_argument,      0, 131},
879   {"extended-return-values",    no_argument,            0, 154},
880   {"filter-stdin",              required_argument,      0, 138},
881   {"footer",                    required_argument,      0, 155},
882   {"h-column-height",           required_argument,      0, 148},
883   {"help",                      no_argument,            0, 135},
884   {"help-highlight",            no_argument,            0, 141},
885   {"highlight-bar-gray",        required_argument,      0, 136},
886   {"list-media",                no_argument,            &list_media, 1},
887   {"margins",                   required_argument,      0, 144},
888   {"mark-wrapped-lines",        optional_argument,      0, 143},
889   {"non-printable-format",      required_argument,      0, 134},
890   {"nup-columnwise",            no_argument,            0, 152},
891   {"nup-xpad",                  required_argument,      0, 145},
892   {"nup-ypad",                  required_argument,      0, 146},
893   {"page-label-format",         required_argument,      0, 130},
894   {"ps-level",                  required_argument,      0, 149},
895   {"printer-options",           required_argument,      0, 139},
896   {"rotate-even-pages",         no_argument,            0, 150},
897   {"slice",                     required_argument,      0, 140},
898   {"style",                     required_argument,      0, 151},
899   {"swap-even-page-margins",    no_argument,            0, 153},
900   {"toc",                       no_argument,            &toc, 1},
901   {"word-wrap",                 no_argument,            0, 147},
902   {"ul-angle",                  required_argument,      0, 132},
903   {"ul-font",                   required_argument,      0, 128},
904   {"ul-gray",                   required_argument,      0, 129},
905   {"ul-position",               required_argument,      0, 133},
906   {"ul-style",                  required_argument,      0, 137},
907
908   /* Backwards compatiblity options. */
909   {"pretty-print",              optional_argument,      0, 'E'},
910   {"help-pretty-print",         no_argument,            0, 141},
911
912   {NULL, 0, 0, 0},
913 };
914
915
916 /*
917  * Global functions.
918  */
919
920 int
921 main (int argc, char *argv[])
922 {
923   InputStream is;
924   time_t tim;
925   struct tm *tm;
926   int i, j, found;
927   unsigned int ui;
928   MediaEntry *mentry;
929   AFMError afm_error;
930   char *cp, *cp2;
931   int retval = 0;
932   Buffer buffer;
933
934   /* Init our dynamic memory buffer. */
935   buffer_init (&buffer);
936
937   /* Get program's name. */
938   program = strrchr (argv[0], '/');
939   if (program == NULL)
940     program = argv[0];
941   else
942     program++;
943
944   /* Make getopt_long() to use our modified programname. */
945   argv[0] = program;
946
947   /* Create the default TOC format string.  Wow, this is cool! */
948   /* xgettext:no-c-format */
949   toc_fmt_string = _("$3v $-40N $3% pages $4L lines  $E $C");
950
951   /* Internationalization. */
952 #if HAVE_SETLOCALE
953   /*
954    * We want to change only messages (gs do not like decimals in 0,1
955    * format ;)
956    */
957 #if HAVE_LC_MESSAGES
958   setlocale (LC_MESSAGES, "");
959 #endif
960 #endif
961 #if ENABLE_NLS
962   bindtextdomain (PACKAGE, LOCALEDIR);
963   textdomain (PACKAGE);
964 #endif
965
966   /* Create date string. */
967
968   tim = time (NULL);
969   tm = localtime (&tim);
970   memcpy (&run_tm, tm, sizeof (*tm));
971
972   date_string = xstrdup (asctime (&run_tm));
973   i = strlen (date_string);
974   date_string[i - 1] = '\0';
975
976   /* Get user's passwd entry. */
977   passwd = getpwuid (getuid ());
978   if (passwd == NULL)
979     FATAL ((stderr, _("couldn't get passwd entry for uid=%d: %s"), getuid (),
980             strerror (errno)));
981
982   /* Defaults for some options. */
983   media_name            = xstrdup ("A4");
984   encoding_name         = xstrdup ("88591");
985   npf_name              = xstrdup ("octal");
986   page_label_format     = xstrdup ("short");
987   ul_style_str          = xstrdup ("outline");
988   ul_position           = xstrdup ("+0-0");
989   spooler_command       = xstrdup ("lpr");
990   queue_param           = xstrdup ("-P");
991   no_job_header_switch  = xstrdup ("-h");
992   fancy_header_default  = xstrdup ("enscript");
993   output_first_line     = xstrdup ("%!PS-Adobe-3.0");
994
995   /* Check ENSCRIPT_LIBRARY for custom library location. */
996   cp = getenv ("ENSCRIPT_LIBRARY");
997   if (cp)
998     enscript_library = cp;
999
1000   /* Fill up build-in libpath. */
1001
1002   cp = getenv ("HOME");
1003   if (cp == NULL)
1004     cp = passwd->pw_dir;
1005
1006   buffer_clear (&buffer);
1007   buffer_append (&buffer, enscript_library);
1008   buffer_append (&buffer, PATH_SEPARATOR_STR);
1009   buffer_append (&buffer, cp);
1010   buffer_append (&buffer, "/.enscript");
1011   libpath = buffer_copy (&buffer);
1012
1013   /* Defaults for the states filter. */
1014
1015   states_binary = xstrdup ("states"); /* Take it from the user path. */
1016
1017   buffer_clear (&buffer);
1018   buffer_append (&buffer, enscript_library);
1019   buffer_append (&buffer, "/hl/enscript.st");
1020   states_config_file = buffer_copy (&buffer);
1021
1022   states_highlight_style = xstrdup ("emacs");
1023
1024   /* The <cp> holds the user's home directory. */
1025   buffer_clear (&buffer);
1026   buffer_append (&buffer, cp);
1027   buffer_append (&buffer, "/.enscript");
1028   buffer_append (&buffer, PATH_SEPARATOR_STR);
1029   buffer_append (&buffer, enscript_library);
1030   buffer_append (&buffer, "/hl");
1031   states_path = buffer_copy (&buffer);
1032
1033   /* Initialize resource sets. */
1034   res_fonts = strhash_init ();
1035   download_fonts = strhash_init ();
1036   pagedevice = strhash_init ();
1037   statusdict = strhash_init ();
1038   user_strings = strhash_init ();
1039
1040
1041   /*
1042    * Read configuration files.
1043    */
1044
1045   /* Global config. */
1046 #define CFG_FILE_NAME "enscript.cfg"
1047   if (!read_config (SYSCONFDIR, CFG_FILE_NAME))
1048     {
1049       int saved_errno = errno;
1050
1051       /* Try to read it from our library directory.  This is mostly
1052          the case for the micro ports.  */
1053       if (!read_config (enscript_library, CFG_FILE_NAME))
1054         {
1055           /* Try `enscript_library/../../etc/'.  This is the case for
1056              installations which set the prefix after the compilation
1057              and our SYSCONFDIR points to wrong directory. */
1058
1059           buffer_clear (&buffer);
1060           buffer_append (&buffer, enscript_library);
1061           buffer_append (&buffer, "/../../etc");
1062
1063           if (!read_config (buffer_ptr (&buffer), CFG_FILE_NAME))
1064             {
1065               /* Maybe we are not installed yet, let's try `../lib'
1066                  and `../../lib'. */
1067               if (!read_config ("../lib", CFG_FILE_NAME)
1068                   && !read_config ("../../lib", CFG_FILE_NAME))
1069                 {
1070                   /* No luck, report error from the original config file. */
1071                   ERROR ((stderr, _("couldn't read config file \"%s/%s\": %s"),
1072                           enscript_library, CFG_FILE_NAME,
1073                           strerror (saved_errno)));
1074                   ERROR ((stderr,
1075                           _("I did also try the following directories:")));
1076                   ERROR ((stderr, _("\t%s"), SYSCONFDIR));
1077                   ERROR ((stderr, _("\t%s"), enscript_library));
1078                   ERROR ((stderr, _("\t%s"), buffer_ptr (&buffer)));
1079                   ERROR ((stderr, _("\t../lib")));
1080                   ERROR ((stderr, _("\t../../lib")));
1081                   ERROR ((stderr,
1082 _("This is probably an installation error.  Please, try to rebuild:")));
1083                   ERROR ((stderr, _("\tmake distclean")));
1084                   ERROR ((stderr, _("\t./configure --prefix=PREFIX")));
1085                   ERROR ((stderr, _("\tmake")));
1086                   ERROR ((stderr, _("\tmake check")));
1087                   ERROR ((stderr, _("\tmake install")));
1088                   ERROR ((stderr,
1089 _("or set the environment variable `ENSCRIPT_LIBRARY' to point to your")));
1090                   ERROR ((stderr,
1091 _("library directory.")));
1092                   exit (1);
1093                 }
1094
1095               /* Ok, we are not installed yet.  Here is a small kludge
1096                  to conform the GNU coding standards: we must be able
1097                  to run without being installed, so we must append the
1098                  `../lib' and `../../lib' directories to the libpath.
1099                  The later allows us to be run form the `src/tests'
1100                  directory.  */
1101               buffer_clear (&buffer);
1102               buffer_append (&buffer, libpath);
1103               buffer_append (&buffer, PATH_SEPARATOR_STR);
1104               buffer_append (&buffer, "../lib");
1105               buffer_append (&buffer, PATH_SEPARATOR_STR);
1106               buffer_append (&buffer, "../../lib");
1107
1108               xfree (libpath);
1109               libpath = buffer_copy (&buffer);
1110             }
1111         }
1112     }
1113
1114   /* Site config. */
1115   read_config (SYSCONFDIR, "enscriptsite.cfg");
1116
1117   /* Personal config. */
1118   read_config (cp, ".enscriptrc");
1119
1120   /*
1121    * Options.
1122    */
1123
1124   /* Environment variables. */
1125   handle_env_options ("ENSCRIPT");
1126   handle_env_options ("GENSCRIPT");
1127
1128   /* Command line arguments. */
1129   handle_options (argc, argv);
1130
1131   /*
1132    * Check options which have some validity conditions.
1133    */
1134
1135   /*
1136    * Save the user-specified escape char so ^@escape{default} knows
1137    * what to set.
1138    */
1139   default_escape_char = escape_char;
1140
1141   /* Input encoding. */
1142
1143   found = 0;
1144   for (i = 0; !found && encodings[i].names[0]; i++)
1145     for (j = 0; j < 3; j++)
1146       if (encodings[i].names[j] != NULL && MATCH (encodings[i].names[j],
1147                                                   encoding_name))
1148         {
1149           /* Found a match for this encoding.  Use the first
1150              "official" name. */
1151
1152           encoding = encodings[i].encoding;
1153           xfree (encoding_name);
1154           encoding_name = xstrdup (encodings[i].names[0]);
1155
1156           if (nl < 0)
1157             nl = encodings[i].nl;
1158           bs = encodings[i].bs;
1159           found = 1;
1160           break;
1161         }
1162   if (!found)
1163     FATAL ((stderr, _("unknown encoding: %s"), encoding_name));
1164
1165   /* Fonts. */
1166
1167   /* Default font for landscape, 2 column printing is Courier 7. */
1168   if (!user_body_font_defined && landscape && num_columns > 1)
1169     Fpt.w = Fpt.h = 7.0;
1170
1171   /* Cache for font AFM information. */
1172   afm_cache = strhash_init ();
1173   afm_info_cache = strhash_init ();
1174
1175   /* Open AFM library. */
1176   afm_error = afm_create (afm_path, verbose, &afm);
1177   if (afm_error != AFM_SUCCESS)
1178     {
1179       char buf[256];
1180
1181       afm_error_to_string (afm_error, buf);
1182       FATAL ((stderr, _("couldn't open AFM library: %s"), buf));
1183     }
1184
1185   /*
1186    * Save default Fpt and Fname since special escape 'font' can change
1187    * it and later we might want to switch back to the "default" font.
1188    */
1189   default_Fpt.w = Fpt.w;
1190   default_Fpt.h = Fpt.h;
1191   default_Fname = Fname;
1192   default_Fencoding = encoding;
1193
1194   /* Register that document uses at least these fonts. */
1195   strhash_put (res_fonts, Fname, strlen (Fname) + 1, NULL, NULL);
1196   strhash_put (res_fonts, HFname, strlen (HFname) + 1, NULL, NULL);
1197
1198   /* As a default, download both named fonts. */
1199   strhash_put (download_fonts, Fname, strlen (Fname) + 1, NULL, NULL);
1200   strhash_put (download_fonts, HFname, strlen (HFname) + 1, NULL, NULL);
1201
1202   /* Read font's character widths and character types. */
1203   read_font_info ();
1204
1205   /* Count the line indentation. */
1206   line_indent = parse_float (line_indent_spec, 1, 1);
1207
1208   /* List media names. */
1209   if (list_media)
1210     {
1211       printf (_("known media:\n\
1212 name             width\theight\tllx\tlly\turx\tury\n\
1213 ------------------------------------------------------------\n"));
1214       for (mentry = media_names; mentry; mentry = mentry->next)
1215         printf ("%-16s %d\t%d\t%d\t%d\t%d\t%d\n",
1216                 mentry->name, mentry->w, mentry->h,
1217                 mentry->llx, mentry->lly, mentry->urx, mentry->ury);
1218       /* Exit after listing. */
1219       exit (0);
1220     }
1221
1222   /* Output media. */
1223   for (mentry = media_names; mentry; mentry = mentry->next)
1224     if (strcmp (media_name, mentry->name) == 0)
1225       {
1226         media = mentry;
1227         break;
1228       }
1229   if (media == NULL)
1230     FATAL ((stderr, _("do not know anything about media \"%s\""), media_name));
1231
1232   if (margins_spec)
1233     {
1234       /* Adjust marginals. */
1235       for (i = 0; i < 4; i++)
1236         {
1237           if (*margins_spec == '\0')
1238             /* All done. */
1239             break;
1240
1241           if (*margins_spec == ':')
1242             {
1243               margins_spec++;
1244               continue;
1245             }
1246
1247           j = atoi (margins_spec);
1248           for (; *margins_spec != ':' && *margins_spec != '\0'; margins_spec++)
1249             ;
1250           if (*margins_spec == ':')
1251             margins_spec++;
1252
1253           switch (i)
1254             {
1255             case 0:             /* left */
1256               media->llx = j;
1257               break;
1258
1259             case 1:             /* right */
1260               media->urx = media->w - j;
1261               break;
1262
1263             case 2:             /* top */
1264               media->ury = media->h - j;
1265               break;
1266
1267             case 3:             /* bottom */
1268               media->lly = j;
1269               break;
1270             }
1271         }
1272       MESSAGE (1,
1273                (stderr,
1274                 _("set new marginals for media `%s' (%dx%d): llx=%d, lly=%d, urx=%d, ury=%d\n"),
1275                 media->name, media->w, media->h, media->llx, media->lly,
1276                 media->urx, media->ury));
1277     }
1278
1279   /* Page label format. */
1280   if (MATCH (page_label_format, "short"))
1281     page_label = LABEL_SHORT;
1282   else if (MATCH (page_label_format, "long"))
1283     page_label = LABEL_LONG;
1284   else
1285     FATAL ((stderr, _("illegal page label format \"%s\""), page_label_format));
1286
1287   /* Non-printable format. */
1288   if (MATCH (npf_name, "space"))
1289     non_printable_format = NPF_SPACE;
1290   else if (MATCH (npf_name, "questionmark"))
1291     non_printable_format = NPF_QUESTIONMARK;
1292   else if (MATCH (npf_name, "caret"))
1293     non_printable_format = NPF_CARET;
1294   else if (MATCH (npf_name, "octal"))
1295     non_printable_format = NPF_OCTAL;
1296   else
1297     FATAL ((stderr, _("illegal non-printable format \"%s\""), npf_name));
1298
1299   /* Mark wrapped lines style. */
1300   if (mark_wrapped_lines_style_name)
1301     {
1302       if (MATCH (mark_wrapped_lines_style_name, "none"))
1303         mark_wrapped_lines_style = MWLS_NONE;
1304       else if (MATCH (mark_wrapped_lines_style_name, "plus"))
1305         mark_wrapped_lines_style = MWLS_PLUS;
1306       else if (MATCH (mark_wrapped_lines_style_name, "box"))
1307         mark_wrapped_lines_style = MWLS_BOX;
1308       else if (MATCH (mark_wrapped_lines_style_name, "arrow"))
1309         mark_wrapped_lines_style = MWLS_ARROW;
1310       else
1311         FATAL ((stderr, _("illegal style for wrapped line marker: \"%s\""),
1312                 mark_wrapped_lines_style_name));
1313     }
1314
1315   /* Count N-up stuffs. */
1316   for (i = 0; ; i++)
1317     {
1318       ui = nup >> i;
1319
1320       if (ui == 0)
1321         FATAL ((stderr, _("illegal N-up argument: %d"), nup));
1322
1323       if (ui & 0x1)
1324         {
1325           if (ui != 1)
1326             FATAL ((stderr, _("N-up argument must be power of 2: %d"), nup));
1327
1328           nup_exp = i;
1329           break;
1330         }
1331     }
1332
1333   nup_rows = nup_exp / 2 * 2;
1334   if (nup_rows == 0)
1335     nup_rows = 1;
1336   nup_columns = (nup_exp + 1) / 2 * 2;
1337   if (nup_columns == 0)
1338     nup_columns = 1;
1339
1340   nup_landscape = nup_exp & 0x1;
1341
1342
1343   /*
1344    * Count output media dimensions.
1345    */
1346
1347   if (landscape)
1348     {
1349       d_page_w = media->ury - media->lly;
1350       d_page_h = media->urx - media->llx;
1351     }
1352   else
1353     {
1354       d_page_w = media->urx - media->llx;
1355       d_page_h = media->ury - media->lly;
1356     }
1357
1358   /*
1359    * Count N-up page width, height and scale.
1360    */
1361
1362   if (nup_landscape)
1363     {
1364       nup_width = media->ury - media->lly;
1365       nup_height = media->urx - media->llx;
1366     }
1367   else
1368     {
1369       nup_width = media->urx - media->llx;
1370       nup_height = media->ury - media->lly;
1371     }
1372
1373   {
1374     double w, h;
1375
1376     w = ((double) nup_width - (nup_columns - 1) * nup_xpad) / nup_columns;
1377     h = ((double) nup_height - (nup_rows - 1) * nup_ypad) / nup_rows;
1378
1379     nup_width = w;
1380     nup_height = h;
1381
1382     w = w / (media->urx - media->llx);
1383     h = h / (media->ury - media->lly);
1384
1385     nup_scale = w < h ? w : h;
1386   }
1387
1388   /*
1389    * Underlay (this must come after output media dimensions, because
1390    * `underlay position' needs them).
1391    */
1392   if (underlay != NULL)
1393     {
1394       strhash_put (res_fonts, ul_font, strlen (ul_font) + 1, NULL, NULL);
1395       underlay = escape_string (underlay);
1396     }
1397
1398   /* Underlay X-coordinate. */
1399   ul_x = strtod (ul_position, &cp);
1400   if (cp == ul_position)
1401     {
1402     malformed_position:
1403       FATAL ((stderr, _("malformed underlay position: %s"), ul_position));
1404     }
1405   if (ul_position[0] == '-')
1406     ul_x += d_page_w;
1407
1408   /* Underlay Y-coordinate. */
1409   ul_y = strtod (cp, &cp2);
1410   if (cp2 == cp)
1411     goto malformed_position;
1412   if (cp[0] == '-')
1413     ul_y += d_page_h;
1414
1415   /* Underlay Angle. */
1416   if (!ul_angle_p)
1417     /* No angle given, count the default. */
1418     ul_angle = (atan2 (-d_page_h, d_page_w) / 3.14159265 * 180);
1419
1420   /* Underlay style. */
1421   if (strcmp (ul_style_str, "outline") == 0)
1422     ul_style = UL_STYLE_OUTLINE;
1423   else if (strcmp (ul_style_str, "filled") == 0)
1424     ul_style = UL_STYLE_FILLED;
1425   else
1426     FATAL ((stderr, _("illegal underlay style: %s"), ul_style_str));
1427
1428   /*
1429    * Header.  Note! The header attributes can be changed from
1430    * the `.hdr' files, these are only the defaults.
1431    */
1432
1433   d_header_w = d_page_w;
1434   switch (header)
1435     {
1436     case HDR_NONE:
1437       d_header_h = 0;
1438       break;
1439
1440     case HDR_SIMPLE:
1441       d_header_h = HFpt.h * 1.5;
1442       break;
1443
1444     case HDR_FANCY:
1445       d_header_h = 36;
1446       break;
1447     }
1448
1449   /* Help highlight. */
1450   if (help_highlight)
1451     {
1452       /* Create description with states. */
1453       printf (_("Highlighting is supported for the following languages and file formats:\n\n"));
1454       fflush (stdout);
1455
1456       buffer_clear (&buffer);
1457       buffer_append (&buffer, states_binary);
1458       buffer_append (&buffer, " -f \"");
1459       buffer_append (&buffer, states_config_file);
1460       buffer_append (&buffer, "\" -p \"");
1461       buffer_append (&buffer, states_path);
1462       buffer_append (&buffer, "\" -s describe_languages ");
1463       buffer_append (&buffer, enscript_library);
1464       buffer_append (&buffer, "/hl/*.st");
1465
1466       system (buffer_ptr (&buffer));
1467       exit (0);
1468     }
1469
1470   /*
1471    * And now to the main business.  The actual input file processing
1472    * is divided to two parts: PostScript generation and everything else.
1473    * The PostScript generation is handled in the conventional way, we
1474    * process the input and generate PostScript.  However all other input
1475    * languages will be handled with States, we only pass enscript's
1476    * options to the states pre-filter and dump output.
1477    */
1478   if (output_language_pass_through)
1479     {
1480       char *start_state;
1481       Buffer cmd;
1482       char intbuf[256];
1483
1484       /* The States output generation. */
1485
1486       /* Resolve the start state. */
1487       if (hl_start_state)
1488         start_state = hl_start_state;
1489       else if (highlight)
1490         start_state = NULL;
1491       else
1492         start_state = "passthrough";
1493
1494       /* Create the states command. */
1495
1496       buffer_init (&cmd);
1497
1498       buffer_append (&cmd, states_binary);
1499       buffer_append (&cmd, " -f \"");
1500       buffer_append (&cmd, states_config_file);
1501       buffer_append (&cmd, "\" -p \"");
1502       buffer_append (&cmd, states_path);
1503       buffer_append (&cmd, "\" ");
1504
1505       if (verbose > 0)
1506         buffer_append (&cmd, "-v ");
1507
1508       if (start_state)
1509         {
1510           buffer_append (&cmd, "-s");
1511           buffer_append (&cmd, start_state);
1512           buffer_append (&cmd, " ");
1513         }
1514
1515       buffer_append (&cmd, "-Dcolor=");
1516       buffer_append (&cmd, states_color ? "1" : "0");
1517       buffer_append (&cmd, " ");
1518
1519       buffer_append (&cmd, "-Dstyle=");
1520       buffer_append (&cmd, states_highlight_style);
1521       buffer_append (&cmd, " ");
1522
1523       buffer_append (&cmd, "-Dlanguage=");
1524       buffer_append (&cmd, output_language);
1525       buffer_append (&cmd, " ");
1526
1527       buffer_append (&cmd, "-Dnum_input_files=");
1528       sprintf (intbuf, "%d", optind == argc ? 1 : argc - optind);
1529       buffer_append (&cmd, intbuf);
1530       buffer_append (&cmd, " ");
1531
1532       buffer_append (&cmd, "-Ddocument_title=\'");
1533       if ((cp = shell_escape (title)) != NULL)
1534         {
1535           buffer_append (&cmd, cp);
1536           free (cp);
1537         }
1538       buffer_append (&cmd, "\' ");
1539
1540       buffer_append (&cmd, "-Dtoc=");
1541       buffer_append (&cmd, toc ? "1" : "0");
1542
1543       /* Additional options for states? */
1544       if (helper_options['s'])
1545         {
1546           Buffer *opts = helper_options['s'];
1547
1548           buffer_append (&cmd, " ");
1549           buffer_append_len (&cmd, buffer_ptr (opts), buffer_len (opts));
1550         }
1551
1552       /* Append input files. */
1553       for (i = optind; i < argc; i++)
1554         {
1555           char *cp;
1556           if ((cp = shell_escape (argv[i])) != NULL)
1557             {
1558               buffer_append (&cmd, " \'");
1559               buffer_append (&cmd, cp);
1560               buffer_append (&cmd, "\'");
1561               free (cp);
1562             }
1563         }
1564
1565       /* And do the job. */
1566       if (is_open (&is, stdin, NULL, buffer_ptr (&cmd)))
1567         {
1568           open_output_file ();
1569           process_file ("unused", &is, 0);
1570           is_close (&is);
1571         }
1572
1573       buffer_uninit (&cmd);
1574     }
1575   else
1576     {
1577       /* The conventional way. */
1578
1579       /* Highlighting. */
1580       if (highlight)
1581         {
1582           char fbuf[256];
1583
1584           /* Create a highlight input filter. */
1585           buffer_clear (&buffer);
1586           buffer_append (&buffer, states_binary);
1587           buffer_append (&buffer, " -f \"");
1588           buffer_append (&buffer, states_config_file);
1589           buffer_append (&buffer, "\" -p \"");
1590           buffer_append (&buffer, states_path);
1591           buffer_append (&buffer, "\"");
1592
1593           if (verbose > 0)
1594             buffer_append (&buffer, " -v");
1595
1596           if (hl_start_state)
1597             {
1598               buffer_append (&buffer, " -s ");
1599               buffer_append (&buffer, hl_start_state);
1600             }
1601
1602           buffer_append (&buffer, " -Dcolor=");
1603           buffer_append (&buffer, states_color ? "1" : "0");
1604
1605           buffer_append (&buffer, " -Dstyle=");
1606           buffer_append (&buffer, states_highlight_style);
1607
1608           buffer_append (&buffer, " -Dfont_spec=");
1609           buffer_append (&buffer, Fname);
1610           sprintf (fbuf, "@%g/%g", Fpt.w, Fpt.h);
1611           buffer_append (&buffer, fbuf);
1612
1613           /* Additional options for states? */
1614           if (helper_options['s'])
1615             {
1616               Buffer *opts = helper_options['s'];
1617
1618               buffer_append (&buffer, " ");
1619               buffer_append_len (&buffer,
1620                                  buffer_ptr (opts), buffer_len (opts));
1621             }
1622
1623           buffer_append (&buffer, " \'%s\'");
1624
1625           input_filter = buffer_copy (&buffer);
1626           input_filter_stdin = "-";
1627         }
1628
1629       /* Table of Contents. */
1630       if (toc)
1631         {
1632           toc_fp = tmpfile ();
1633           if (toc_fp == NULL)
1634             FATAL ((stderr, _("couldn't create temporary toc file: %s"),
1635                     strerror (errno)));
1636         }
1637
1638
1639       /*
1640        * Process files.
1641        */
1642
1643       if (optind == argc)
1644         {
1645           /* stdin's modification time is the current time. */
1646           memcpy (&mod_tm, &run_tm, sizeof (run_tm));
1647
1648           if (is_open (&is, stdin, NULL, input_filter))
1649             {
1650               /* Open output file. */
1651               open_output_file ();
1652               process_file (title_given ? title : "", &is, 0);
1653               is_close (&is);
1654             }
1655         }
1656       else
1657         {
1658           for (; optind < argc; optind++)
1659             {
1660               if (is_open (&is, NULL, argv[optind], input_filter))
1661                 {
1662                   struct stat stat_st;
1663
1664                   /* Get modification time. */
1665                   if (stat (argv[optind], &stat_st) == 0)
1666                     {
1667                       tim = stat_st.st_mtime;
1668                       tm = localtime (&tim);
1669                       memcpy (&mod_tm, tm, sizeof (*tm));
1670
1671                       /*
1672                        * Open output file.  Output file opening is delayed to
1673                        * this point so we can optimize the case when a
1674                        * non-existing input file is printed => we do nothing.
1675                        */
1676                       open_output_file ();
1677
1678                       process_file (argv[optind], &is, 0);
1679                     }
1680                   else
1681                     ERROR ((stderr, _("couldn't stat input file \"%s\": %s"),
1682                             argv[optind],
1683                             strerror (errno)));
1684
1685                   is_close (&is);
1686                 }
1687             }
1688         }
1689
1690       /* Table of Contents. */
1691       if (toc)
1692         {
1693           /* This is really cool... */
1694
1695           /* Set the printing options for toc. */
1696           toc = 0;
1697           special_escapes = 1;
1698           line_numbers = 0;
1699
1700           if (fseek (toc_fp, 0, SEEK_SET) != 0)
1701             FATAL ((stderr, _("couldn't rewind toc file: %s"),
1702                     strerror (errno)));
1703
1704           memcpy (&mod_tm, &run_tm, sizeof (run_tm));
1705           if (is_open (&is, toc_fp, NULL, NULL))
1706             {
1707               process_file (_("Table of Contents"), &is, 1);
1708               is_close (&is);
1709             }
1710         }
1711
1712       /* Give trailer a chance to dump itself. */
1713       dump_ps_trailer ();
1714
1715       /*
1716        * Append ^D to the end of the output?  Note! It must be ^D followed
1717        * by a newline.
1718        */
1719       if (ofp != NULL && append_ctrl_D)
1720         fprintf (ofp, "\004\n");
1721     }
1722
1723   /* Close output file. */
1724   close_output_file ();
1725
1726   /* Tell how things went. */
1727   if (ofp == NULL)
1728     {
1729       /*
1730        * The value of <ofp> is not reset in close_output_file(),
1731        * this is ugly but it saves one flag.
1732        */
1733       MESSAGE (0, (stderr, _("no output generated\n")));
1734     }
1735   else if (output_language_pass_through)
1736     {
1737       if (output_file == OUTPUT_FILE_NONE)
1738         MESSAGE (0, (stderr, _("output sent to %s\n"),
1739                      printer ? printer : _("printer")));
1740       else
1741         MESSAGE (0, (stderr, _("output left in %s\n"),
1742                      output_file == OUTPUT_FILE_STDOUT ? "-" : output_file));
1743     }
1744   else
1745     {
1746       unsigned int real_total_pages;
1747
1748       if (nup > 1)
1749         {
1750           if (total_pages > 0)
1751             real_total_pages = (total_pages - 1) / nup + 1;
1752           else
1753             real_total_pages = 0;
1754         }
1755       else
1756         real_total_pages = total_pages;
1757
1758       /* We did something, tell what.  */
1759       MESSAGE (0, (stderr, _("[ %d pages * %d copy ]"), real_total_pages,
1760                    num_copies));
1761       if (output_file == OUTPUT_FILE_NONE)
1762         MESSAGE (0, (stderr, _(" sent to %s\n"),
1763                      printer ? printer : _("printer")));
1764       else
1765         MESSAGE (0, (stderr, _(" left in %s\n"),
1766                      output_file == OUTPUT_FILE_STDOUT ? "-" : output_file));
1767       if (num_truncated_lines)
1768         {
1769           retval |= 2;
1770           MESSAGE (0, (stderr,
1771                        ngettext("%d line was %s\n",
1772                                 "%d lines were %s\n",
1773                                 num_truncated_lines),
1774                        num_truncated_lines,
1775                        line_end == LE_TRUNCATE
1776                        ? _("truncated") : _("wrapped")));
1777         }
1778
1779       if (num_missing_chars)
1780         {
1781           retval |= 4;
1782           MESSAGE (0, (stderr,
1783                        ngettext("%d character was missing\n",
1784                                 "%d characters were missing\n",
1785                                 num_missing_chars),
1786                        num_missing_chars));
1787           if (list_missing_characters)
1788             {
1789               MESSAGE (0, (stderr, _("missing character codes (decimal):\n")));
1790               do_list_missing_characters (missing_chars);
1791             }
1792         }
1793
1794       if (num_non_printable_chars)
1795         {
1796           retval |= 8;
1797           MESSAGE (0, (stderr,
1798                        ngettext("%d non-printable character\n",
1799                                 "%d non-printable characters\n",
1800                                 num_non_printable_chars),
1801                        num_non_printable_chars));
1802           if (list_missing_characters)
1803             {
1804               MESSAGE (0, (stderr,
1805                            _("non-printable character codes (decimal):\n")));
1806               do_list_missing_characters (non_printable_chars);
1807             }
1808         }
1809     }
1810
1811   /* Uninit our dynamic memory buffer. */
1812   buffer_uninit (&buffer);
1813
1814   /* Return the extended return values only if requested. */
1815   if (!extended_return_values)
1816     retval = 0;
1817
1818   /* This is the end. */
1819   return retval;
1820 }
1821
1822
1823 /*
1824  * Static functions.
1825  */
1826
1827 static void
1828 open_output_file ()
1829 {
1830   if (ofp)
1831     /* Output file has already been opened, do nothing. */
1832     return;
1833
1834   if (output_file == OUTPUT_FILE_NONE)
1835     {
1836       char spooler_options[512];
1837
1838       /* Format spooler options. */
1839       spooler_options[0] = '\0';
1840       if (mail)
1841         strcat (spooler_options, "-m ");
1842       if (no_job_header)
1843         {
1844           strcat (spooler_options, no_job_header_switch);
1845           strcat (spooler_options, " ");
1846         }
1847       if (printer_options)
1848         strcat (spooler_options, printer_options);
1849
1850       /* Open printer. */
1851       ofp = printer_open (spooler_command, spooler_options, queue_param,
1852                           printer, &printer_context);
1853       if (ofp == NULL)
1854         FATAL ((stderr, _("couldn't open printer `%s': %s"), printer,
1855                 strerror (errno)));
1856     }
1857   else if (output_file == OUTPUT_FILE_STDOUT)
1858     ofp = stdout;
1859   else
1860     {
1861       ofp = fopen (output_file, "w");
1862       if (ofp == NULL)
1863         FATAL ((stderr, _("couldn't create output file \"%s\": %s"),
1864                 output_file, strerror (errno)));
1865     }
1866 }
1867
1868
1869 static void
1870 close_output_file ()
1871 {
1872   if (ofp == NULL)
1873     /* Output file hasn't been opened, we are done. */
1874     return;
1875
1876   if (output_file == OUTPUT_FILE_NONE)
1877     printer_close (printer_context);
1878   else if (output_file != OUTPUT_FILE_STDOUT)
1879     if (fclose (ofp))
1880       FATAL ((stderr, _("couldn't close output file \"%s\": %s"),
1881               output_file, strerror (errno)));
1882
1883   /* We do not reset <ofp> since its value is needed in diagnostigs. */
1884 }
1885
1886
1887 static void
1888 handle_env_options (char *var)
1889 {
1890   int argc;
1891   char **argv;
1892   char *string;
1893   char *str;
1894   int i;
1895
1896   string = getenv (var);
1897   if (string == NULL)
1898     return;
1899
1900   MESSAGE (2, (stderr, "handle_env_options(): %s=\"%s\"\n", var, string));
1901
1902   /* Copy string so we can modify it in place. */
1903   str = xstrdup (string);
1904
1905   /*
1906    * We can count this, each option takes at least 1 character and one
1907    * space.  We also need one for program's name and one for the
1908    * trailing NULL.
1909    */
1910   argc = (strlen (str) + 1) / 2 + 2;
1911   argv = xcalloc (argc, sizeof (char *));
1912
1913   /* Set program name. */
1914   argc = 0;
1915   argv[argc++] = program;
1916
1917   /* Split string and set arguments to argv array. */
1918   i = 0;
1919   while (str[i])
1920     {
1921       /* Skip leading whitespace. */
1922       for (; str[i] && isspace (str[i]); i++)
1923         ;
1924       if (!str[i])
1925         break;
1926
1927       /* Check for quoted arguments. */
1928       if (str[i] == '"' || str[i] == '\'')
1929         {
1930           int endch = str[i++];
1931
1932           argv[argc++] = str + i;
1933
1934           /* Skip until we found the end of the quotation. */
1935           for (; str[i] && str[i] != endch; i++)
1936             ;
1937           if (!str[i])
1938             FATAL ((stderr, _("syntax error in option string %s=\"%s\":\n\
1939 missing end of quotation: %c"), var, string, endch));
1940
1941           str[i++] = '\0';
1942         }
1943       else
1944         {
1945           argv[argc++] = str + i;
1946
1947           /* Skip until whitespace if found. */
1948           for (; str[i] && !isspace (str[i]); i++)
1949             ;
1950           if (str[i])
1951             str[i++] = '\0';
1952         }
1953     }
1954
1955   /* argv[argc] must be NULL. */
1956   argv[argc] = NULL;
1957
1958   MESSAGE (2, (stderr, "found following options (argc=%d):\n", argc));
1959   for (i = 0; i < argc; i++)
1960     MESSAGE (2, (stderr, "%3d = \"%s\"\n", i, argv[i]));
1961
1962   /* Process options. */
1963   handle_options (argc, argv);
1964
1965   /* Check that all got processed. */
1966   if (optind != argc)
1967     {
1968       MESSAGE (0,
1969                (stderr,
1970                 _("warning: didn't process following options from \
1971 environment variable %s:\n"),
1972                 var));
1973       for (; optind < argc; optind++)
1974         MESSAGE (0, (stderr, _("  option %d = \"%s\"\n"), optind,
1975                      argv[optind]));
1976     }
1977
1978   /* Cleanup. */
1979   xfree (argv);
1980
1981   /*
1982    * <str> must not be freed, since some global variables can point to
1983    * its elements
1984    */
1985 }
1986
1987
1988 static void
1989 handle_options (int argc, char *argv[])
1990 {
1991   int c;
1992   PageRange *prange;
1993
1994   /* Reset optind. */
1995   optind = 0;
1996
1997   while (1)
1998     {
1999       int option_index = 0;
2000       const char *cp;
2001       int i;
2002
2003       c = getopt_long (argc, argv,
2004                        "#:123456789a:A:b:BcC::d:D:e::E::f:F:gGhH::i:I:jJ:kKlL:mM:n:N:o:Op:P:qrRs:S:t:T:u::U:vVw:W:X:zZ",
2005                        long_options, &option_index);
2006
2007       if (c == -1)
2008         break;
2009
2010       switch (c)
2011         {
2012         case 0:                 /* Long option found. */
2013           cp = long_options[option_index].name;
2014
2015           if (strcmp (cp, "columns") == 0)
2016             {
2017               num_columns = atoi (optarg);
2018               if (num_columns < 1)
2019                 FATAL ((stderr,
2020                         _("number of columns must be larger than zero")));
2021             }
2022           break;
2023
2024           /* Short options. */
2025
2026         case '1':               /* 1 column */
2027         case '2':               /* 2 columns */
2028         case '3':               /* 3 columns */
2029         case '4':               /* 4 columns */
2030         case '5':               /* 5 columns */
2031         case '6':               /* 6 columns */
2032         case '7':               /* 7 columns */
2033         case '8':               /* 8 columns */
2034         case '9':               /* 9 columns */
2035           num_columns = c - '0';
2036           break;
2037
2038         case 'a':               /* pages */
2039           prange = (PageRange *) xcalloc (1, sizeof (PageRange));
2040
2041           if (strcmp (optarg, "odd") == 0)
2042             prange->odd = 1;
2043           else if (strcmp (optarg, "even") == 0)
2044             prange->even = 1;
2045           else
2046             {
2047               cp = strchr (optarg, '-');
2048               if (cp)
2049                 {
2050                   if (optarg[0] == '-')
2051                     /* -end */
2052                     prange->end = atoi (optarg + 1);
2053                   else if (cp[1] == '\0')
2054                     {
2055                       /* begin- */
2056                       prange->start = atoi (optarg);
2057                       prange->end = (unsigned int) -1;
2058                     }
2059                   else
2060                     {
2061                       /* begin-end */
2062                       prange->start = atoi (optarg);
2063                       prange->end = atoi (cp + 1);
2064                     }
2065                 }
2066               else
2067                 /* pagenumber */
2068                 prange->start = prange->end = atoi (optarg);
2069             }
2070
2071           prange->next = page_ranges;
2072           page_ranges = prange;
2073           break;
2074
2075         case 'A':               /* file alignment */
2076           file_align = atoi (optarg);
2077           if (file_align == 0)
2078             FATAL ((stderr, _("file alignment must be larger than zero")));
2079           break;
2080
2081         case 'b':               /* page header */
2082           page_header = optarg;
2083           break;
2084
2085         case 'B':               /* no page headers */
2086           header = HDR_NONE;
2087           break;
2088
2089         case 'c':               /* truncate (cut) long lines */
2090           line_end = LE_TRUNCATE;
2091           break;
2092
2093         case 'C':               /* line numbers */
2094           line_numbers = 1;
2095           if (optarg)
2096             start_line_number = atoi (optarg);
2097           break;
2098
2099         case 'd':               /* specify printer */
2100         case 'P':
2101           xfree (printer);
2102           printer = xstrdup (optarg);
2103           output_file = OUTPUT_FILE_NONE;
2104           break;
2105
2106         case 'D':               /* setpagedevice */
2107           parse_key_value_pair (pagedevice, optarg);
2108           break;
2109
2110         case 'e':               /* special escapes */
2111           special_escapes = 1;
2112           if (optarg)
2113             {
2114               /* Specify the escape character. */
2115               if (isdigit (optarg[0]))
2116                 /* As decimal, octal, or hexadicimal number. */
2117                 escape_char = (int) strtoul (optarg, NULL, 0);
2118               else
2119                 /* As character directly. */
2120                 escape_char = ((unsigned char *) optarg)[0];
2121             }
2122           break;
2123
2124         case 'E':               /* highlight */
2125           highlight = 1;
2126           special_escapes = 1;
2127           escape_char = '\0';
2128           hl_start_state = optarg;
2129           break;
2130
2131         case 'f':               /* font */
2132           if (!parse_font_spec (optarg, &Fname, &Fpt, NULL))
2133             FATAL ((stderr, _("malformed font spec: %s"), optarg));
2134           user_body_font_defined = 1;
2135           break;
2136
2137         case 'F':               /* header font */
2138           if (!parse_font_spec (optarg, &HFname, &HFpt, NULL))
2139             FATAL ((stderr, _("malformed font spec: %s"), optarg));
2140           break;
2141
2142         case 'g':               /* print anyway */
2143           /* nothing. */
2144           break;
2145
2146         case 'G':               /* fancy header */
2147           header = HDR_FANCY;
2148           if (optarg)
2149             fancy_header_name = optarg;
2150           else
2151             fancy_header_name = fancy_header_default;
2152
2153           if (!file_existsp (fancy_header_name, ".hdr"))
2154             FATAL ((stderr,
2155                     _("couldn't find header definition file \"%s.hdr\""),
2156                     fancy_header_name));
2157           break;
2158
2159         case 'h':               /* no job header */
2160           no_job_header = 1;
2161           break;
2162
2163         case 'H':               /* highlight bars */
2164           if (optarg)
2165             highlight_bars = atoi (optarg);
2166           else
2167             highlight_bars = 2;
2168           break;
2169
2170         case 'i':               /* line indent */
2171           line_indent_spec = optarg;
2172           break;
2173
2174         case 'I':               /* input filter */
2175           input_filter = optarg;
2176           break;
2177
2178         case 'j':               /* borders */
2179           borders = 1;
2180           break;
2181
2182         case 'k':               /* enable page prefeed */
2183           page_prefeed = 1;
2184           break;
2185
2186         case 'K':               /* disable page prefeed */
2187           page_prefeed = 0;
2188           break;
2189
2190         case 'l':               /* emulate lineprinter */
2191           lines_per_page = 66;
2192           header = HDR_NONE;
2193           break;
2194
2195         case 'L':               /* lines per page */
2196           lines_per_page = atoi (optarg);
2197           if (lines_per_page <= 0)
2198             FATAL ((stderr,
2199                     _("must print at least one line per each page: %s"),
2200                     argv[optind]));
2201           break;
2202
2203         case 'm':               /* send mail upon completion */
2204           mail = 1;
2205           break;
2206
2207         case 'M':               /* select output media */
2208           media_name = xstrdup (optarg);
2209           break;
2210
2211         case 'n':               /* num copies */
2212         case '#':
2213           num_copies = atoi (optarg);
2214           break;
2215
2216         case 'N':               /* newline character */
2217           if (!(optarg[0] == 'n' || optarg[0] == 'r') || optarg[1] != '\0')
2218             {
2219               fprintf (stderr, _("%s: illegal newline character specifier: \
2220 '%s': expected 'n' or 'r'\n"),
2221                        program, optarg);
2222               goto option_error;
2223             }
2224           if (optarg[0] == 'n')
2225             nl = '\n';
2226           else
2227             nl = '\r';
2228           break;
2229
2230         case 'o':
2231         case 'p':               /* output file */
2232           /* Check output file "-". */
2233           if (strcmp (optarg, "-") == 0)
2234             output_file = OUTPUT_FILE_STDOUT;
2235           else
2236             output_file = optarg;
2237           break;
2238
2239         case 'O':               /* list missing characters */
2240           list_missing_characters = 1;
2241           break;
2242
2243         case 'q':               /* quiet */
2244           quiet = 1;
2245           verbose = 0;
2246           break;
2247
2248         case 'r':               /* landscape */
2249           landscape = 1;
2250           break;
2251
2252         case 'R':               /* portrait */
2253           landscape = 0;
2254           break;
2255
2256         case 's':               /* baselineskip */
2257           baselineskip = atof (optarg);
2258           break;
2259
2260         case 'S':               /* statusdict */
2261           parse_key_value_pair (statusdict, optarg);
2262           break;
2263
2264         case 't':               /* title */
2265         case 'J':
2266           title = optarg;
2267           title_given = 1;
2268           break;
2269
2270         case 'T':               /* tabulator size */
2271           tabsize = atoi (optarg);
2272           if (tabsize <= 0)
2273             tabsize = 1;
2274           break;
2275
2276         case 'u':               /* underlay */
2277           underlay = optarg;
2278           break;
2279
2280         case 'U':               /* nup */
2281           nup = atoi (optarg);
2282           break;
2283
2284         case 'v':               /* verbose */
2285           if (optarg)
2286             verbose = atoi (optarg);
2287           else
2288             verbose++;
2289           quiet = 0;
2290           break;
2291
2292         case 'V':               /* version */
2293           version ();
2294           exit (0);
2295           break;
2296
2297         case 'w':               /* output language */
2298           output_language = optarg;
2299           if (strcmp (output_language, "PostScript") != 0)
2300             /* Other output languages are handled with states. */
2301             output_language_pass_through = 1;
2302           break;
2303
2304         case 'W':               /* a helper application option */
2305           cp = strchr (optarg, ',');
2306           if (cp == NULL)
2307             FATAL ((stderr,
2308                     _("malformed argument `%s' for option -W, --option: \
2309 no comma found"),
2310                       optarg));
2311
2312           if (cp - optarg != 1)
2313             FATAL ((stderr, _("helper application specification must be \
2314 single character: %s"),
2315                               optarg));
2316
2317           /* Take the index of the helper application and update `cp'
2318              to point to the beginning of the option. */
2319           i = *optarg;
2320           cp++;
2321
2322           if (helper_options[i] == NULL)
2323             helper_options[i] = buffer_alloc ();
2324           else
2325             {
2326               /* We already had some options for this helper
2327                  application.  Let's separate these arguments. */
2328               buffer_append (helper_options[i], " ");
2329             }
2330
2331           /* Add this new option. */
2332           buffer_append (helper_options[i], cp);
2333           break;
2334
2335         case 'X':               /* input encoding */
2336           xfree (encoding_name);
2337           encoding_name = xstrdup (optarg);
2338           break;
2339
2340         case 'z':               /* no form feeds */
2341           interpret_formfeed = 0;
2342           break;
2343
2344         case 'Z':               /* pass through */
2345           pass_through = 1;
2346           break;
2347
2348         case 128:               /* underlay font */
2349           if (!parse_font_spec (optarg, &ul_font, &ul_ptsize, NULL))
2350             FATAL ((stderr, _("malformed font spec: %s"), optarg));
2351           break;
2352
2353         case 129:               /* underlay gray */
2354           ul_gray = atof (optarg);
2355           break;
2356
2357         case 130:               /* page label format */
2358           xfree (page_label_format);
2359           page_label_format = xstrdup (optarg);
2360           break;
2361
2362         case 131:               /* download font */
2363           strhash_put (download_fonts, optarg, strlen (optarg) + 1, NULL,
2364                        NULL);
2365           break;
2366
2367         case 132:               /* underlay angle */
2368           ul_angle = atof (optarg);
2369           ul_angle_p = 1;
2370           break;
2371
2372         case 133:               /* underlay position */
2373           xfree (ul_position);
2374           ul_position = xstrdup (optarg);
2375           ul_position_p = 1;
2376           break;
2377
2378         case 134:               /* non-printable format */
2379           xfree (npf_name);
2380           npf_name = xstrdup (optarg);
2381           break;
2382
2383         case 135:               /* help */
2384           usage ();
2385           exit (0);
2386           break;
2387
2388         case 136:               /* highlight bar gray */
2389           highlight_bar_gray = atof (optarg);
2390           break;
2391
2392         case 137:               /* underlay style */
2393           xfree (ul_style_str);
2394           ul_style_str = xstrdup (optarg);
2395           break;
2396
2397         case 138:               /* filter stdin */
2398           input_filter_stdin = optarg;
2399           break;
2400
2401         case 139:               /* extra options for the printer spooler */
2402           printer_options = optarg;
2403           break;
2404
2405         case 140:               /* slicing */
2406           slicing = 1;
2407           slice = atoi (optarg);
2408           if (slice <= 0)
2409             FATAL ((stderr, _("slice must be greater than zero")));
2410           break;
2411
2412         case 141:               /* help-highlight */
2413           help_highlight = 1;
2414           break;
2415
2416         case 142:               /* States color? */
2417           if (optarg == NULL)
2418             states_color = 1;
2419           else
2420             states_color = atoi (optarg);
2421           break;
2422
2423         case 143:               /* mark-wrapped-lines */
2424           if (optarg)
2425             {
2426               xfree (mark_wrapped_lines_style_name);
2427               mark_wrapped_lines_style_name = xstrdup (optarg);
2428             }
2429           else
2430             /* Set the system default. */
2431             mark_wrapped_lines_style = MWLS_BOX;
2432           break;
2433
2434         case 144:               /* adjust margins */
2435           margins_spec = optarg;
2436           break;
2437
2438         case 145:               /* N-up x-pad */
2439           nup_xpad = atoi (optarg);
2440           break;
2441
2442         case 146:               /* N-up y-pad */
2443           nup_ypad = atoi (optarg);
2444           break;
2445
2446         case 147:               /* word wrap */
2447           line_end = LE_WORD_WRAP;
2448           break;
2449
2450         case 148:               /* horizontal column height */
2451           horizontal_column_height = atof (optarg);
2452           formfeed_type = FORMFEED_HCOLUMN;
2453           break;
2454
2455         case 149:               /* PostScript language level */
2456           pslevel = atoi (optarg);
2457           break;
2458
2459         case 150:               /* rotate even-numbered pages */
2460           rotate_even_pages = 1;
2461           break;
2462
2463         case 151:               /* highlight style */
2464           xfree (states_highlight_style);
2465           states_highlight_style = xstrdup (optarg);
2466           break;
2467
2468         case 152:               /* N-up colunwise */
2469           nup_columnwise = 1;
2470           break;
2471
2472         case 153:               /* swap even page margins */
2473           swap_even_page_margins = 1;
2474           break;
2475
2476         case 154:               /* extended return values */
2477           extended_return_values = 1;
2478           break;
2479
2480         case 155:               /* footer */
2481           page_footer = optarg;
2482           break;
2483
2484         case 156:               /* continuous page numbers */
2485           continuous_page_numbers = 1;
2486           break;
2487
2488         case '?':               /* Errors found during getopt_long(). */
2489         option_error:
2490           fprintf (stderr, _("Try `%s --help' for more information.\n"),
2491                    program);
2492           exit (1);
2493           break;
2494
2495         default:
2496           printf ("Hey!  main() didn't handle option \"%c\" (%d)", c, c);
2497           if (optarg)
2498             printf (" with arg %s", optarg);
2499           printf ("\n");
2500           FATAL ((stderr, "This is a bug!"));
2501           break;
2502         }
2503     }
2504 }
2505
2506
2507 static void
2508 usage ()
2509 {
2510   printf (_("\
2511 Usage: %s [OPTION]... [FILE]...\n\
2512 Mandatory arguments to long options are mandatory for short options too.\n\
2513   -#                         an alias for option -n, --copies\n\
2514   -1                         same as --columns=1\n\
2515   -2                         same as --columns=2\n\
2516       --columns=NUM          specify the number of columns per page\n\
2517   -a, --pages=PAGES          specify which pages are printed\n\
2518   -A, --file-align=ALIGN     align separate input files to ALIGN\n\
2519   -b, --header=HEADER        set page header\n\
2520   -B, --no-header            no page headers\n\
2521   -c, --truncate-lines       cut long lines (default is to wrap)\n\
2522   -C[START], --line-numbers[=START]\n\
2523                              precede each line with its line number\n\
2524   -d                         an alias for option --printer\n\
2525   -D, --setpagedevice=KEY[:VALUE]\n\
2526                              pass a page device definition to output\n\
2527   -e[CHAR], --escapes[=CHAR]       enable special escape interpretation\n"),
2528           program);
2529
2530   printf (_("\
2531   -E[LANG], --highlight[=LANG]     highlight source code\n"));
2532
2533   printf (_("\
2534   -f, --font=NAME            use font NAME for body text\n\
2535   -F, --header-font=NAME     use font NAME for header texts\n\
2536   -g, --print-anyway         nothing (compatibility option)\n\
2537   -G                         same as --fancy-header\n\
2538       --fancy-header[=NAME]  select fancy page header\n\
2539   -h, --no-job-header        suppress the job header page\n\
2540   -H[NUM], --highlight-bars[=NUM]  specify how high highlight bars are\n\
2541   -i, --indent=NUM           set line indent to NUM characters\n\
2542   -I, --filter=CMD           read input files through input filter CMD\n\
2543   -j, --borders              print borders around columns\n\
2544   -J,                        an alias for option --title\n\
2545   -k, --page-prefeed         enable page prefeed\n\
2546   -K, --no-page-prefeed      disable page prefeed\n\
2547   -l, --lineprinter          simulate lineprinter, this is an alias for:\n\
2548                                --lines-per-page=66, --no-header, --portrait,\n\
2549                                --columns=1\n"));
2550
2551   printf (_("\
2552   -L, --lines-per-page=NUM   specify how many lines are printed on each page\n\
2553   -m, --mail                 send mail upon completion\n\
2554   -M, --media=NAME           use output media NAME\n\
2555   -n, --copies=NUM           print NUM copies of each page\n\
2556   -N, --newline=NL           select the newline character.  Possible\n\
2557                              values for NL are: n (`\\n') and r (`\\r').\n\
2558   -o                         an alias for option --output\n\
2559   -O, --missing-characters   list missing characters\n\
2560   -p, --output=FILE          leave output to file FILE.  If FILE is `-',\n\
2561                              leave output to stdout.\n\
2562   -P, --printer=NAME         print output to printer NAME\n\
2563   -q, --quiet, --silent      be really quiet\n\
2564   -r, --landscape            print in landscape mode\n\
2565   -R, --portrait             print in portrait mode\n"));
2566
2567   printf (_("\
2568   -s, --baselineskip=NUM     set baselineskip to NUM\n\
2569   -S, --statusdict=KEY[:VALUE]\n\
2570                              pass a statusdict definition to the output\n\
2571   -t, --title=TITLE          set banner page's job title to TITLE.  Option\n\
2572                              sets also the name of the input file stdin.\n\
2573   -T, --tabsize=NUM          set tabulator size to NUM\n\
2574   -u[TEXT], --underlay[=TEXT]      print TEXT under every page\n\
2575   -U, --nup=NUM              print NUM logical pages on each output page\n\
2576   -v, --verbose              tell what we are doing\n\
2577   -V, --version              print version number\n\
2578   -w, --language=LANG        set output language to LANG\n\
2579   -W, --options=APP,OPTION   pass option OPTION to helper application APP\n\
2580   -X, --encoding=NAME        use input encoding NAME\n\
2581   -z, --no-formfeed          do not interpret form feed characters\n\
2582   -Z, --pass-through         pass through PostScript and PCL files\n\
2583                              without any modifications\n"));
2584
2585   printf (_("Long-only options:\n\
2586   --color[=bool]             create color outputs with states\n\
2587   --continuous-page-numbers  count page numbers across input files.  Don't\n\
2588                              restart numbering at beginning of each file.\n\
2589   --download-font=NAME       download font NAME\n\
2590   --extended-return-values   enable extended return values\n\
2591   --filter-stdin=NAME        specify how stdin is shown to the input filter\n\
2592   --footer=FOOTER            set page footer\n\
2593   --h-column-height=HEIGHT   set the horizontal column height to HEIGHT\n\
2594   --help                     print this help and exit\n"));
2595
2596   printf (_("\
2597   --help-highlight           describe all supported --highlight languages\n\
2598                              and file formats\n\
2599   --highlight-bar-gray=NUM   print highlight bars with gray NUM (0 - 1)\n\
2600   --list-media               list names of all known media\n\
2601   --margins=LEFT:RIGHT:TOP:BOTTOM\n\
2602                              adjust page marginals\n\
2603   --mark-wrapped-lines[STYLE]\n\
2604                              mark wrapped lines in the output with STYLE\n\
2605   --non-printable-format=FMT specify how non-printable chars are printed\n"));
2606
2607   printf (_("\
2608   --nup-columnwise           layout pages in the N-up printing columnwise\n\
2609   --nup-xpad=NUM             set the page x-padding of N-up printing to NUM\n\
2610   --nup-ypad=NUM             set the page y-padding of N-up printing to NUM\n\
2611   --page-label-format=FMT    set page label format to FMT\n\
2612   --ps-level=LEVEL           set the PostScript language level that enscript\n\
2613                              should use\n\
2614   --printer-options=OPTIONS  pass extra options to the printer command\n\
2615   --rotate-even-pages        rotate even-numbered pages 180 degrees\n"));
2616
2617   printf (_("\
2618   --slice=NUM                print vertical slice NUM\n\
2619   --style=STYLE              use highlight style STYLE\n\
2620   --swap-even-page-margins   swap left and right side margins for each even\n\
2621                              numbered page\n\
2622   --toc                      print table of contents\n\
2623   --ul-angle=ANGLE           set underlay text's angle to ANGLE\n\
2624   --ul-font=NAME             print underlays with font NAME\n\
2625   --ul-gray=NUM              print underlays with gray value NUM\n\
2626   --ul-position=POS          set underlay's starting position to POS\n\
2627   --ul-style=STYLE           print underlays with style STYLE\n\
2628   --word-wrap                wrap long lines from word boundaries\n\
2629 "));
2630
2631   printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2632 }
2633
2634
2635 static void
2636 version ()
2637 {
2638   printf ("%s\n\
2639 Copyright (C) 1995-2003, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.\n\
2640 %s comes with NO WARRANTY, to the extent permitted by law.\n\
2641 You may redistribute copies of %s under the terms of the GNU\n\
2642 General Public License, version 3 or, at your option, any later version.\n\
2643 For more information about these matters, see the files named COPYING.\n",
2644           PACKAGE_STRING,
2645           PACKAGE_NAME,
2646           PACKAGE_NAME);
2647 }