Import GNU Enscript version 1.6.4
[enscript.git] / src / gsint.h
1 /*
2  * Internal header file.
3  * Copyright (c) 1995-2000 Markku Rossi.
4  *
5  * Author: Markku Rossi <mtr@iki.fi>
6  */
7
8 /*
9  * This file is part of GNU enscript.
10  *
11  * This program 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 2, or (at your option)
14  * any later version.
15  *
16  * This program 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 this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 #ifndef GSINT_H
28 #define GSINT_H
29
30 /*
31  * Config stuffs.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #ifndef ___P
44 #if PROTOTYPES
45 #define ___P(protos) protos
46 #else /* no PROTOTYPES */
47 #define ___P(protos) ()
48 #endif /* no PROTOTYPES */
49 #endif
50
51 #if STDC_HEADERS
52
53 #include <stdlib.h>
54 #include <string.h>
55
56 #else /* no STDC_HEADERS */
57
58 #if HAVE_STDLIB_H
59 #include <stdlib.h>
60 #endif
61
62 #if HAVE_STRING_H
63 #include <string.h>
64 #endif
65
66 #ifndef HAVE_STRCHR
67 #define strchr index
68 #define strrchr rindex
69 #endif
70 char *strchr ();
71 char *strrchr ();
72
73 #ifndef HAVE_STRERROR
74 extern char *strerror ___P ((int));
75 #endif
76
77 #ifndef HAVE_MEMMOVE
78 extern void *memmove ___P ((void *, void *, size_t));
79 #endif
80
81 #ifndef HAVE_MEMCPY
82 extern void *memcpy ___P ((void *, void *, size_t));
83 #endif
84
85 #endif /* no STDC_HEADERS */
86
87 #if HAVE_UNISTD_H
88 #include <unistd.h>
89 #endif
90
91 #if HAVE_MATH_H
92 #include <math.h>
93 #else
94 extern double atan2 ___P ((double, double));
95 #endif
96
97 #include <errno.h>
98 #include <time.h>
99 #include <assert.h>
100 #include <ctype.h>
101
102 #if HAVE_PWD_H
103 #include <pwd.h>
104 #else
105 #include "dummypwd.h"
106 #endif
107
108 #if ENABLE_NLS
109 #include <libintl.h>
110 #define _(String) gettext (String)
111 #else
112 #define _(String) String
113 #endif
114
115 #if HAVE_LC_MESSAGES
116 #include <locale.h>
117 #endif
118
119 #ifndef HAVE_GETCWD
120 #if HAVE_GETWD
121 #define getcwd(buf, len) getwd(buf)
122 #endif /* HAVE_GETWD */
123 #endif /* not HAVE_GETCWD */
124
125 #include "afm.h"
126 #include "strhash.h"
127 #include "xalloc.h"
128
129 /*
130  * Types and definitions.
131  */
132
133 #define MATCH(a, b) (strcmp (a, b) == 0)
134
135 #define ISNUMBERDIGIT(ch) \
136   (('0' <= (ch) && (ch) <= '9') || (ch) == '.' || (ch) == '-' || (ch) == '+')
137
138 /* Return the width of the character <ch> */
139 #define CHAR_WIDTH(ch) (font_widths[(unsigned char) (ch)])
140
141 /* Current point y movement from line to line. */
142 #define LINESKIP (Fpt.h + baselineskip)
143
144
145 /* Constants for output files. */
146 #define OUTPUT_FILE_NONE   NULL
147 #define OUTPUT_FILE_STDOUT ((char *) 1)
148
149 /* Underlay styles. */
150 #define UL_STYLE_OUTLINE        0
151 #define UL_STYLE_FILLED         1
152
153 struct media_entry_st
154 {
155   struct media_entry_st *next;
156   char *name;
157   int w;
158   int h;
159   int llx;
160   int lly;
161   int urx;
162   int ury;
163 };
164
165 typedef struct media_entry_st MediaEntry;
166
167 typedef enum
168 {
169   HDR_NONE,
170   HDR_SIMPLE,
171   HDR_FANCY
172 } HeaderType;
173
174
175 typedef enum
176 {
177   ENC_ISO_8859_1,
178   ENC_ISO_8859_2,
179   ENC_ISO_8859_3,
180   ENC_ISO_8859_4,
181   ENC_ISO_8859_5,
182   ENC_ISO_8859_7,
183   ENC_ISO_8859_9,
184   ENC_ISO_8859_10,
185   ENC_ASCII,
186   ENC_ASCII_FISE,
187   ENC_ASCII_DKNO,
188   ENC_IBMPC,
189   ENC_MAC,
190   ENC_VMS,
191   ENC_HP8,
192   ENC_KOI8,
193   ENC_PS
194 } InputEncoding;
195
196 struct encoding_registry_st
197 {
198   char *names[3];
199   InputEncoding encoding;
200   int nl;
201   int bs;
202 };
203
204 typedef struct encoding_registry_st EncodingRegistry;
205
206 typedef enum
207 {
208   LABEL_SHORT,
209   LABEL_LONG
210 } PageLabelFormat;
211
212 typedef enum
213 {
214   MWLS_NONE     = 0,
215   MWLS_PLUS     = 1,
216   MWLS_BOX      = 2,
217   MWLS_ARROW    = 3
218 } MarkWrappedLinesStyle;
219
220 typedef enum
221 {
222   NPF_SPACE,
223   NPF_QUESTIONMARK,
224   NPF_CARET,
225   NPF_OCTAL
226 } NonPrintableFormat;
227
228 typedef enum
229 {
230   FORMFEED_COLUMN,
231   FORMFEED_PAGE,
232   FORMFEED_HCOLUMN
233 } FormFeedType;
234
235 typedef enum
236 {
237   LE_TRUNCATE,
238   LE_CHAR_WRAP,
239   LE_WORD_WRAP
240 } LineEndType;
241
242 struct buffer_st
243 {
244   char *data;
245   size_t allocated;
246   size_t len;
247 };
248
249 typedef struct buffer_st Buffer;
250
251 struct file_lookup_ctx_st
252 {
253   /* The name of the file to lookup. */
254   char *name;
255
256   /* The suffix of the file.  This string is appended to <name>. */
257   char *suffix;
258
259   /* Buffer to which the name of the file is constructed.  If the
260      file_lookup() returns 1, the name of the file is here.  The
261      caller of the file_lookup() must allocate this buffer. */
262   Buffer *fullname;
263 };
264
265 typedef struct file_lookup_ctx_st FileLookupCtx;
266
267 typedef int (*PathWalkProc) ___P ((char *path, void *context));
268
269
270 struct input_stream_st
271 {
272   int is_pipe;                  /* Is <fp> opened to pipe? */
273   FILE *fp;
274   unsigned char buf[4096];
275   unsigned int data_in_buf;
276   unsigned int bufpos;
277   unsigned int nreads;
278   unsigned char *unget_ch;
279   unsigned int unget_pos;
280   unsigned int unget_alloc;
281 };
282
283 typedef struct input_stream_st InputStream;
284
285
286 struct page_range_st
287 {
288   struct page_range_st *next;
289   int odd;
290   int even;
291   unsigned int start;
292   unsigned int end;
293 };
294
295 typedef struct page_range_st PageRange;
296
297 struct font_point_st
298 {
299   double w;                     /* width */
300   double h;                     /* height */
301 };
302
303 typedef struct font_point_st FontPoint;
304
305 struct color_st
306 {
307   float r;
308   float g;
309   float b;
310 };
311
312 typedef struct color_st Color;
313
314 struct cached_font_info_st
315 {
316   double font_widths[256];
317   char font_ctype[256];
318   AFMBoolean font_is_fixed;
319   AFMNumber font_bbox_lly;
320 };
321
322 typedef struct cached_font_info_st CachedFontInfo;
323
324
325 /*
326  * Global variables.
327  */
328
329 extern char *program;
330 extern FILE *ofp;
331 extern char *version_string;
332 extern char *ps_version_string;
333 extern char *date_string;
334 extern struct tm run_tm;
335 extern struct tm mod_tm;
336 extern struct passwd *passwd;
337 extern char *libpath;
338 extern char *afm_path;
339 extern MediaEntry *media_names;
340 extern MediaEntry *media;
341 extern char *no_job_header_switch;
342 extern char *output_first_line;
343 extern char *queue_param;
344 extern char *spooler_command;
345 extern int nl;
346 extern int bs;
347 extern unsigned int current_pagenum;
348 extern unsigned int input_filenum;
349 extern unsigned int current_file_linenum;
350 extern char *fname;
351
352 /* Statistics. */
353 extern int total_pages;
354 extern int num_truncated_lines;
355 extern int num_missing_chars;
356 extern int missing_chars[];
357 extern int num_non_printable_chars;
358 extern int non_printable_chars[];
359
360 /* Dimensions that are used during PostScript generation. */
361 extern int d_page_w;
362 extern int d_page_h;
363 extern int d_header_w;
364 extern int d_header_h;
365 extern int d_footer_h;
366 extern int d_output_w;
367 extern int d_output_h;
368 extern int d_output_x_margin;
369 extern int d_output_y_margin;
370 extern unsigned int nup_xpad;
371 extern unsigned int nup_ypad;
372
373 /* Document needed resources. */
374 extern StringHashPtr res_fonts;
375
376 /* Fonts to download. */
377 extern StringHashPtr download_fonts;
378
379 /* Additional key-value pairs, passed to the generated PostScript code. */
380 extern StringHashPtr pagedevice;
381 extern StringHashPtr statusdict;
382
383 /* User defined strings. */
384 extern StringHashPtr user_strings;
385
386 /* Cache for AFM files. */
387 extern StringHashPtr afm_cache;
388 extern StringHashPtr afm_info_cache;
389
390 /* AFM library handle. */
391 extern AFMHandle afm;
392
393 /* Fonts. */
394 extern char *HFname;
395 extern FontPoint HFpt;
396 extern char *Fname;
397 extern FontPoint Fpt;
398 extern FontPoint default_Fpt;
399 extern char *default_Fname;
400 extern InputEncoding default_Fencoding;
401
402 extern double font_widths[];
403 extern char font_ctype[];
404 extern int font_is_fixed;
405 extern double font_bbox_lly;
406
407 /* Known input encodings. */
408 extern EncodingRegistry encodings[];
409
410 /* Options. */
411
412 extern char *printer;
413 extern int verbose;
414 extern int num_copies;
415 extern char *title;
416 extern int num_columns;
417 extern LineEndType line_end;
418 extern int quiet;
419 extern int landscape;
420 extern HeaderType header;
421 extern char *fancy_header_name;
422 extern char *fancy_header_default;
423 extern double line_indent;
424 extern char *page_header;
425 extern char *page_footer;
426 extern char *output_file;
427 extern unsigned int lines_per_page;
428 extern InputEncoding encoding;
429 extern char *media_name;
430 extern char *encoding_name;
431 extern int special_escapes;
432 extern int escape_char;
433 extern int default_escape_char;
434 extern int tabsize;
435 extern double baselineskip;
436 extern FontPoint ul_ptsize;
437 extern double ul_gray;
438 extern char *ul_font;
439 extern char *underlay;
440 extern char *ul_position;
441 extern double ul_x;
442 extern double ul_y;
443 extern double ul_angle;
444 extern unsigned int ul_style;
445 extern char *ul_style_str;
446 extern int ul_position_p;
447 extern int ul_angle_p;
448 extern PageLabelFormat page_label;
449 extern char *page_label_format;
450 extern int pass_through;
451 extern int line_numbers;
452 extern unsigned int start_line_number;
453 extern int interpret_formfeed;
454 extern NonPrintableFormat non_printable_format;
455 extern MarkWrappedLinesStyle mark_wrapped_lines_style;
456 extern char *mark_wrapped_lines_style_name;
457 extern char *npf_name;
458 extern int clean_7bit;
459 extern int append_ctrl_D;
460 extern unsigned int highlight_bars;
461 extern double highlight_bar_gray;
462 extern int page_prefeed;
463 extern PageRange *page_ranges;
464 extern int borders;
465 extern double line_highlight_gray;
466 extern double bggray;
467 extern int accept_composites;
468 extern FormFeedType formfeed_type;
469 extern char *input_filter_stdin;
470 extern int toc;
471 extern FILE *toc_fp;
472 extern char *toc_fmt_string;
473 extern unsigned int file_align;
474 extern int slicing;
475 extern unsigned int slice;
476
477 extern char *states_binary;
478 extern int states_color;
479 extern char *states_config_file;
480 extern char *states_highlight_style;
481 extern char *states_path;
482
483 extern unsigned int nup;
484 extern unsigned int nup_rows;
485 extern unsigned int nup_columns;
486 extern int nup_landscape;
487 extern unsigned int nup_width;
488 extern unsigned int nup_height;
489 extern double nup_scale;
490 extern int nup_columnwise;
491 extern char *output_language;
492 extern int output_language_pass_through;
493 extern int generate_PageSize;
494 extern double horizontal_column_height;
495 extern unsigned int pslevel;
496 extern int rotate_even_pages;
497 extern int swap_even_page_margins;
498 extern int continuous_page_numbers;
499
500 \f
501 /*
502  * Prototypes for global functions.
503  */
504
505 /* Print message if <verbose> is >= <verbose_level>. */
506 #define MESSAGE(verbose_level, body)            \
507   do {                                          \
508     if (!quiet && verbose >= (verbose_level))   \
509       fprintf body;                             \
510   } while (0)
511
512 /* Report continuable error. */
513 #define ERROR(body)                     \
514   do {                                  \
515     fprintf (stderr, "%s: ", program);  \
516     fprintf body;                       \
517     fprintf (stderr, "\n");             \
518     fflush (stderr);                    \
519   } while (0)
520
521 /* Report fatal error and exit with status 1.  Function never returns. */
522 #define FATAL(body)                     \
523   do {                                  \
524     fprintf (stderr, "%s: ", program);  \
525     fprintf body;                       \
526     fprintf (stderr, "\n");             \
527     fflush (stderr);                    \
528     exit (1);                           \
529   } while (0)
530
531 /*
532  * Read config file <path, name>.  Returns bool.  If function fails, error
533  * is found from errno.
534  */
535 int read_config ___P ((char *path, char *name));
536
537 /* Print PostScript header to our output stream. */
538 void dump_ps_header ___P ((void));
539
540 /* Print PostScript trailer to our output stream. */
541 void dump_ps_trailer ___P ((void));
542
543 /*
544  * Open InputStream to <fp> or <fname>.  If <input_filter> is given
545  * it is used to pre-filter the incoming data stream.  Function returns
546  * 1 if stream could be opened or 0 otherwise.
547  */
548 int is_open ___P ((InputStream *is, FILE *fp, char *fname,
549                    char *input_filter));
550
551 /* Close InputStream <is>. */
552 void is_close ___P ((InputStream *is));
553
554 /*
555  * Read next character from the InputStream <is>.  Returns EOF if
556  * EOF was reached.
557  */
558 int is_getc ___P ((InputStream *is));
559
560 /*
561  * Put character <ch> back to the InputStream <is>.  Function returns EOF
562  * if character couldn't be unget.
563  */
564 int is_ungetc ___P ((int ch, InputStream *is));
565
566
567 void buffer_init ___P ((Buffer *buffer));
568
569 void buffer_uninit ___P ((Buffer *buffer));
570
571 Buffer *buffer_alloc ();
572
573 void buffer_free ___P ((Buffer *buffer));
574
575 void buffer_append ___P ((Buffer *buffer, const char *data));
576
577 void buffer_append_len ___P ((Buffer *buffer, const char *data, size_t len));
578
579 char *buffer_copy ___P ((Buffer *buffer));
580
581 void buffer_clear ___P ((Buffer *buffer));
582
583 char *buffer_ptr ___P ((Buffer *buffer));
584
585 size_t buffer_len ___P ((Buffer *buffer));
586
587
588 /*
589  * Process single input file <fp>.  File's name is given in <fname> and
590  * it is used to print headers.  The argument <is_toc> specifies whether
591  * the file is a table of contents file or not.
592  */
593 void process_file ___P ((char *fname, InputStream *fp, int is_toc));
594
595 /* Add a new media to the list of known media. */
596 void add_media ___P ((char *name, int w, int h, int llx, int lly, int urx,
597                       int ury));
598
599 /* Print a listing of missing characters. */
600 void do_list_missing_characters ___P ((int *array));
601
602 /*
603  * Check if file <name, suffix> exists.  Returns bool.  If function fails
604  * error can be found from errno.
605  */
606 int file_existsp ___P ((char *name, char *suffix));
607
608 /*
609  * Paste file <name, suffix> to output stream.  Returns bool. If
610  * function fails, error can be found from errno.
611  */
612 int paste_file ___P ((char *name, char *suffix));
613
614 /*
615  * Do tilde substitution for filename <fname>.  The function returns a
616  * xmalloc()ated result that must be freed by the caller.
617  */
618 char *tilde_subst ___P ((char *fname));
619
620 /*
621  * Parse one float dimension from string <string>.  If <units> is true,
622  * then number can be followed by an optional unit specifier.  If
623  * <horizontal> is true, then dimension is horizontal, otherwise it
624  * is vertical.
625  */
626 double parse_float ___P ((char *string, int units, int horizontal));
627
628 /*
629  * Parse font spec <spec> and return font's name, size, and encoding
630  * in variables <name_return>, <size_return>, and <encoding_return>.
631  * Returns 1 if <spec> was a valid font spec or 0 otherwise.  Returned
632  * name <name_return> is allocated with xcalloc() and must be freed by
633  * caller.
634  */
635 int parse_font_spec ___P ((char *spec, char **name_return,
636                            FontPoint *size_return,
637                            InputEncoding *encoding_return));
638
639 /*
640  * Read body font's character widths and character codes from AFM files.
641  */
642 void read_font_info ___P ((void));
643
644 /*
645  * Try to download font <name>.
646  */
647 void download_font ___P ((char *name));
648
649 /*
650  * Escape all PostScript string's special characters from string <string>.
651  * Returns a xmalloc()ated result.
652  */
653 char *escape_string ___P ((char *string));
654
655 /*
656  * Expand user escapes from string <string>.  Returns a xmalloc()ated
657  * result.
658  */
659 char *format_user_string ___P ((char *context_name, char *string));
660
661 /*
662  * Parses key-value pair <kv> and inserts/deletes key from <set>.
663  */
664 void parse_key_value_pair ___P ((StringHashPtr set, char *kv));
665
666 /*
667  * Count how many non-empty items there are in the key-value set <set>.
668  */
669 int count_key_value_set ___P ((StringHashPtr set));
670
671 /*
672  * Walk through path <path> and call <proc> once for each of its
673  * components.  Function returns 0 if all components were accessed.
674  * Callback <proc> can interrupt walking by returning a non zero
675  * return value.  In that case value is returned as the return value
676  * of the pathwalk().
677  */
678 int pathwalk ___P ((char *path, PathWalkProc proc, void *context));
679
680 /* Lookup file from path.  <context> must point to FileLookupCtx. */
681 int file_lookup ___P ((char *path, void *context));
682
683
684 /*
685  * Portable printer interface.
686  */
687
688 /*
689  * Open and initialize printer <cmd>, <options>, <queue_param> and
690  * <printer_name>.  Function returns a FILE pointer to which enscript
691  * can generate its PostScript output or NULL if printer
692  * initialization failed.  Command can store its context information
693  * to variable <context_return> wich is passed as an argument to the
694  * printer_close() function.
695  */
696 FILE *printer_open ___P ((char *cmd, char *options, char *queue_param,
697                           char *printer_name, void **context_return));
698
699 /*
700  * Flush all pending output to printer <context> and close it.
701  */
702 void printer_close ___P ((void *context));
703
704 #endif /* not GSINT_H */