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