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