1 /*
2  * mpage.h
3  */
4 
5 /*
6  * mpage:	A program to reduce pages of print so that several pages
7  * 	  	of output appear on one sheet of paper.
8  *
9  * Copyright (c) 1994-2004 Marcel J.E. Mol, The Netherlands
10  * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
11  *
12  *     This program is free software; you can redistribute it and/or
13  *     modify it under the terms of the GNU General Public License
14  *     as published by the Free Software Foundation; either version 2
15  *     of the License, or (at your option) any later version.
16  *
17  *     This program is distributed in the hope that it will be useful,
18  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *     GNU General Public License for more details.
21  *
22  *     You should have received a copy of the GNU General Public License
23  *     along with this program; if not, write to the Free Software
24  *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25  *
26  */
27 
28 /*
29  * Through-out the program comments I have tried to refer to pages a the
30  * logical printed page of text that gets reduced.  Sheets refer to physical
31  * pieces of paper.  Hence, mulitple pages appear on a sheet.  "page" is a
32  * logical or virtual entity, and "sheet" is physical entity.
33  */
34 
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <limits.h>
39 #include <string.h>
40 
41 #define	VERSION		"2.5.7 June 2017"
42 
43 #define	TRUE		1
44 #define	FALSE		0
45 
46 #define	LINESIZE	1024
47 
48 #define	FILE_CONT	0
49 #define	FILE_EOF	1
50 #define	FILE_MORE	2
51 
52 #define	LINE_MORE	5
53 #define	LINE_EOF_NOW	4
54 #define	LINE_EOF	3
55 #define	LINE_OVERPRINT	2
56 #define	LINE_BLANK	1
57 
58 #define SKIP_PS		1
59 #define STORE_PS	2
60 #define FLUSH_PS	3
61 
62 #define	TSIZE		12
63 #define HSIZE		TSIZE+2
64 
65 #define DEFAULTPMARGIN  4
66 
67 #if !defined(DEFAULTSMARGIN)
68 # define       DEFAULTSMARGIN  20
69 #endif
70 
71 #define DEFAULTTMARGIN  0
72 #define DEFAULTTABSTOP  8
73 
74 #if !defined(MAXINT)
75 # ifdef INT_MAX
76 #  define	MAXINT		INT_MAX
77 # else
78 #  define	MAXINT		(1 << 30)
79 # endif
80 #endif
81 
82 #if !defined(DEFAULT_ENCODING)
83 # define        DEFAULT_ENCODING  0
84 #endif
85 
86 #define MAXJARG         100
87 
88 /*
89  * to turn on debugging, define the preprocessor macro DEBUG and set
90  * the variable Debug_flag to the sum of the sections to debug.
91  */
92 # ifdef DEBUG
93 # define Debug(f,s,a)	if (Debug_flag & f) printf(s,a)
94 # define DB_GETLINE	0x0000001
95 # define DB_ONEPAGE	0x0000002
96 # define DB_STDIN	0x0000004
97 # define DB_PSDOC	0x0000008
98 # define DB_PSPAGE	0x0000010
99 # define DB_PSCHECK	0x0000020
100 # define DB_PSROFF	0x0000040
101 # define DB_PSMPAGE	0x0000080
102 # define DB_POINTS	0x0000100
103 # define DB_POST	0x0000200
104 # define DB_UNUSED	0x0000400
105 extern int Debug_flag;
106 extern int errno;
107 # else
108 # define Debug(f,s,a)
109 # endif /* DEBUG */
110 
111 /*
112  * definitions for sorting out types of postscript input
113  */
114 # define	PS_NONE		0
115 # define	PS_PSROFF	1
116 # define	PS_MPAGE	2
117 # define	PS_CONFORM	3
118 # define	PS_OTHER	4
119 # define	PS_MSWINDOWS	5
120 # define	PS_TEX		6
121 # define	PS_TEX2		7
122 
123 
124 /*
125  * Input file type selection types
126  */
127 # define	IN_AUTO		0
128 # define	IN_ASCII	1
129 # define	IN_PS		2
130 
131 
132 /*
133  * set default page size
134  */
135 #if !defined(PAGE_DEF)
136 # define PAGE_DEF	"A4"
137 #endif
138 
139 /*
140  * define print spooler types
141  */
142 #define ATT_SPOOLER    1
143 #define BSD_SPOOLER    2
144 #if !defined(SPOOLER)
145 # define SPOOLER	BSD_SPOOLER
146 #endif
147 
148 /*
149  * printing definitions
150  */
151 #if !defined(PRPROG)
152 # define PRPROG		"pr"
153 #endif
154 #if !defined(PRINTPROG)
155 # if SPOOLER == ATT_SPOOLER
156 #  define PRINTPROG   "lp"
157 # else /* BSD_SPOOLER */
158 #  define PRINTPROG   "lpr"
159 # endif
160 #endif
161 #if !defined(QUEARG)
162 # if SPOOLER == ATT_SPOOLER
163 #  define QUEARG              "-d"
164 # else /* BSD_SPOOLER */
165 #  define QUEARG              "-P"
166 # endif
167 #endif
168 
169 /*
170  * "Conforming" postscript flag string  (remember ps_check strips
171  * the "%!" flag from PS files
172  */
173 # define	PS_FLAG		"PS"
174 # define	PS_FLAG2	"-Adobe-"
175 
176 /*
177  * a sheet describes the measurements and orientatation of a page for
178  * use in constructing a sheet preabmles.
179  */
180 struct sheet {
181 	int sh_cwidth;		/* number of characters across a page */
182 	int sh_plength;		/* number of lines down a page */
183 	int (*sh_width)();	/* postscript width across a printed page */
184 	int (*sh_height)();	/* postscript height of a printed page */
185 	int sh_rotate;		/* angle to rotate the page */
186 	void (*sh_outline)();	/* text to print as outline for */
187 				/*    the printed sheet*/
188 	struct	pagepoints *sh_pagepoints; /* where to put pages on */
189 					     /*    the printed sheet */
190 };
191 
192 /*
193  * simple x and y coordinates for putting pages of output on printed sheet
194  * skip to skip this page???
195  */
196 struct pagepoints {
197 	int (*pp_origin_x)();
198 	int (*pp_origin_y)();
199 	int skip;
200 };
201 
202 
203 /*
204  * Definition of an optional annotate box around part of text
205  */
206 struct pagebox {
207 	int over; /* over from the left column */
208 	int lift; /* lift from the bottom line */
209 	int wide; /* columns wide */
210 	int high; /* lines high */
211 	int thick;/* line thickness */
212 };
213 
214 
215 /*
216  * Structure to describe a physical piece of paper, e.g. A4 or Letter
217  */
218 struct page_desc {
219     char *media;
220     int width;
221     int height;
222 };
223 
224 /*
225  * some basic PS parameters
226  */
227 extern int ps_width;	/* number of points in the X direction (8.5 inches) */
228 extern int ps_height;	/* number of points in the Y direction (11 inches) */
229 extern char * media;	/* name of output page media */
230 
231 
232 extern struct page_desc paper[];
233 
234 /* array of sheets where pages are ordered for coli ??? */
235 extern struct sheet coli[];
236 
237 /* array of sheets where pages are ordered for left to right reading */
238 extern struct sheet *left_right[];
239 
240 /* arrays for sheets where pages are ordered for top to bottom reading  */
241 extern struct sheet *up_down[];
242 
243 /* definitions for aspect and reading directions */
244 # define PORTRAIT	0
245 # define LANDSCAPE	1
246 # define LANDSCAPE_PORTRAIT	2
247 # define UPDOWN		0
248 # define LEFTRIGHT	1
249 
250 /*
251  * Variables for holding the chosen options,  The defaults are set here.
252  * the sheetlist pointer is set to point to the array for either up/down
253  * or left/right reading.  This array is index by sheetorder, and then
254  * sheetindex.  sheetindex encodes the number of reduced pages per printed
255  * sheet and indexes into the sheet list (0 = 1 page, 1 = two pages, 2 =
256  * four pages, 3 = eight pages).
257  */
258 extern struct sheet **sheetlist;/* array of sheet lists (up/down or left/right) */
259 extern int sheetaspect;		/* either normal or landscape */
260 extern int sheetorder;		/* up/down or left/right flag */
261 extern int sheetindex;		/* index to number of pages of sheet */
262 extern int sheetmargin_left;	/* non-printable border on sheet */
263 extern int sheetmargin_right;	/* non-printable border on sheet */
264 extern int sheetmargin_top;	/* non-printable border on sheet */
265 extern int sheetmargin_bottom;	/* non-printable border on sheet */
266 extern int pagemargin_left;	/* border for pages */
267 extern int pagemargin_right;	/* border for pages */
268 extern int pagemargin_top;	/* border for pages */
269 extern int pagemargin_bottom;	/* border for pages */
270 extern int textmargin_left;	/* border for textbox */
271 extern int textmargin_right;	/* border for textbox */
272 extern int textmargin_top;	/* border for textbox */
273 extern int textmargin_bottom;	/* border for textbox */
274 extern int sheetheader_left;    /* space for physical sheetheader */
275 extern int sheetheader_right;   /* space for physical sheetheader */
276 extern int sheetheader_top;     /* space for physical sheetheader */
277 extern int sheetheader_bottom;  /* space for physical sheetheader */
278 
279 extern struct pagepoints *points;
280 extern int orientation;		/* final orientation of text */
281 extern int fsize;		/* font scale size */
282 extern int opt_pr;		/* boolean, if true use pr to format output */
283 extern int opt_tabstop;		/* define tabstop width */
284 extern int opt_indent;		/* starting column for text printing */
285 extern int opt_lines;		/* number of lines to fit on an reduced page */
286 extern int opt_killtrail;       /* stop reading input on %%TRailer */
287 extern int opt_width;		/* number of columns to fit on reduced page */
288 extern int opt_mp_header;	/* let mpage create a header */
289 extern int opt_sheetheader;     /* let mpage create sheetheaders */
290 extern char * opt_page;		/* sheets size: a4 or us letter */
291 extern int opt_fold;		/* fold long lines */
292 extern int opt_outline;		/* print a nice outline around pages */
293 extern int opt_verbose;		/* print a count of pages sent to printer */
294 extern int opt_square;		/* Make pages with same aspect as sheets */
295 extern int opt_reverse;		/* Print sheets in reverse order */
296 extern int opt_jarg;		/* Number of -j arg sets */
297 extern int opt_first[MAXJARG];	/* First sheet # to print, 1 = first */
298 extern int opt_last[MAXJARG];	/* Last sheet # to print, 0 = EOF */
299 extern int opt_alt[MAXJARG];	/* Print every Nth sheet */
300 extern int opt_file;            /* should each file appera on a new sheet */
301 extern int opt_duplex;          /* duplex mode*/
302 extern int opt_tumble;          /* tumble every second pages */
303 extern int opt_textbox;         /* print a nice box box around text*/
304 extern int opt_input;           /* set input file type */
305 extern int opt_encoding;        /* use default encoding or not */
306 
307 extern struct pagebox textbox;
308 
309 
310 extern char * opt_header;	/* the header for pr's -h option */
311 extern char * printque;		/* the printer queuename */
312 extern char * prprog;		/* the pr filter program */
313 extern char * printprog;	/* the print program */
314 extern char * printarg;		/* define print queue to printprog */
315 extern int    doprint;		/* send output to printer or not */
316 extern char * charvec_file;	/* file to read character definitions from */
317 extern char * libdir;		/* pointer to get library files from */
318 extern char * fontname;		/* Font to use */
319 extern char * dateformat;	/* Date/time format for headers */
320 extern char * sheethead;	/* Header for each physical page */
321 
322 
323 /*
324  * various global information
325  */
326 extern char MPAGE[];		/* program name */
327 extern int ps_pagenum;		/* current page count (printed or not) */
328 extern int ps_outpages;		/* pages printed */
329 extern int had_ps;              /* did we process ps files */
330 extern int first_encoding;	/* first encoding in character set */
331 extern int last_encoding;	/* last encoding in character set */
332 extern int mpage_level;		/* keep track of multilevel mpaga calls */
333 extern int Coli;		/* value of 0=don't mess, 1 = 4,1 (outside pages), */
334 extern int use_utf8;            /* is the input UTF-8 or not */
335 extern int check_utf8;          /* do we want tocheck for UTF-8 or not */
336 
337 
338 
339 /* args.c */
340 int do_args();
341 int do_env();
342 /* file.c */
343 void do_file();
344 void do_pr_file();
345 void do_stdin();
346 void do_sheets();
347 /* glob.c */
348 void usage();
349 /* page.c */
350 void check_papersize();
351 void set_page();
352 int select_pagetype();
353 void show_pagetypes();
354 int xbase1(), xbase2();
355 int ybase1(), ybase2(), ybase3(), ybase4();
356 int ytop1(), ytop2(), ytop3(), ytop4();
357 int xwid1(), xwid2();
358 int yht1(), yht2(), yht4();
359 void outline_1();
360 void outline_2();
361 void outline_4();
362 void outline_8();
363 void sheetheader();
364 void mp_outline();
365 /* post.c */
366 int ps_check();
367 void do_ps_doc();
368 /* text.c */
369 void do_text_doc();
370 /* util.c */
371 void memgets_init();
372 char *memgets();
373 
374 /*
375  * For OS/2
376  */
377 #ifdef __EMX__
378 # define strcasecmp stricmp
379 #endif
380 
381 /*
382  * For Amiga
383  */
384 #ifdef AMIGA
385 # define popen      fopen
386 # define pclose     fclose
387 # define strcasecmp stricmp
388 #endif
389 
390