xref: /illumos-gate/usr/src/cmd/lp/filter/postscript/download/download.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3	*/
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * download - host resident font downloader
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * Prepends host resident fonts to PostScript input files. The program assumes
37*7c478bd9Sstevel@tonic-gate  * the input files are part of a single PostScript job and that requested fonts
38*7c478bd9Sstevel@tonic-gate  * can be downloaded at the start of each input file. Downloaded fonts are the
39*7c478bd9Sstevel@tonic-gate  * ones named in a %%DocumentFonts: comment and listed in a special map table.
40*7c478bd9Sstevel@tonic-gate  * Map table pathnames (supplied using the -m option) that begin with a / are
41*7c478bd9Sstevel@tonic-gate  * taken as is. Otherwise the final pathname is built using *hostfontdir (-H
42*7c478bd9Sstevel@tonic-gate  * option), *mapname (-m option), and *suffix.
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  * The map table consists of fontname-filename pairs, separated by white space.
45*7c478bd9Sstevel@tonic-gate  * Comments are introduced by % (as in PostScript) and extend to the end of the
46*7c478bd9Sstevel@tonic-gate  * current line. The only fonts that can be downloaded are the ones listed in
47*7c478bd9Sstevel@tonic-gate  * the active map table that point the program to a readable Unix file. A request
48*7c478bd9Sstevel@tonic-gate  * for an unlisted font or inaccessible file is ignored. All font requests are
49*7c478bd9Sstevel@tonic-gate  * ignored if the map table can't be read. In that case the program simply copies
50*7c478bd9Sstevel@tonic-gate  * the input files to stdout.
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  * An example (but not one to follow) of what can be in a map table is,
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  *	%
55*7c478bd9Sstevel@tonic-gate  *	% Map requests for Bookman-Light to file *hostfontdir/KR
56*7c478bd9Sstevel@tonic-gate  *	%
57*7c478bd9Sstevel@tonic-gate  *
58*7c478bd9Sstevel@tonic-gate  *	  Bookman-Light		KR	% Keeping everything (including the map
59*7c478bd9Sstevel@tonic-gate  *					% table) in *hostfontdir seems like the
60*7c478bd9Sstevel@tonic-gate  *					% cleanest approach.
61*7c478bd9Sstevel@tonic-gate  *
62*7c478bd9Sstevel@tonic-gate  *	%
63*7c478bd9Sstevel@tonic-gate  *	% Map Palatino-Roman to file *hostfontdir/palatino/Roman
64*7c478bd9Sstevel@tonic-gate  *	%
65*7c478bd9Sstevel@tonic-gate  *	  Palatino-Roman	palatino/Roman
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  *	% Map ZapfDingbats to file /usr/lib/host/dingbats
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  *	  ZapfDingbats		/usr/lib/host/dingbats
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * Once again, file names that begin with a / are taken as is. All others have
72*7c478bd9Sstevel@tonic-gate  * *hostfontdir/ prepended to the file string associated with a particular font.
73*7c478bd9Sstevel@tonic-gate  *
74*7c478bd9Sstevel@tonic-gate  * Map table can be associated with a printer model (e.g. a LaserWriter), a
75*7c478bd9Sstevel@tonic-gate  * printer destination, or whatever - the choice is up to an administrator.
76*7c478bd9Sstevel@tonic-gate  * By destination may be best if your spooler is running several private
77*7c478bd9Sstevel@tonic-gate  * printers. Host resident fonts are usually purchased under a license that
78*7c478bd9Sstevel@tonic-gate  * restricts their use to a limited number of printers. A font licensed for
79*7c478bd9Sstevel@tonic-gate  * a single printer should only be used on that printer.
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * Was written quickly, so there's much room for improvement. Undoubtedly should
82*7c478bd9Sstevel@tonic-gate  * be a more general program (e.g. scan for other comments).
83*7c478bd9Sstevel@tonic-gate  *
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #include <stdio.h>
87*7c478bd9Sstevel@tonic-gate #include <signal.h>
88*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
89*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
90*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate #include "comments.h"			/* PostScript file structuring comments */
93*7c478bd9Sstevel@tonic-gate #include "gen.h"			/* general purpose definitions */
94*7c478bd9Sstevel@tonic-gate #include "path.h"			/* for temporary directory */
95*7c478bd9Sstevel@tonic-gate #include "ext.h"			/* external variable declarations */
96*7c478bd9Sstevel@tonic-gate #include "download.h"			/* a few special definitions */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate char	*temp_dir = TEMPDIR;		/* temp directory - for copying stdin */
99*7c478bd9Sstevel@tonic-gate char	*hostfontdir = HOSTFONTDIR;	/* host resident directory */
100*7c478bd9Sstevel@tonic-gate char	*mapname = "map";		/* map table - usually in *hostfontdir */
101*7c478bd9Sstevel@tonic-gate char	*suffix = "";			/* appended to the map table pathname */
102*7c478bd9Sstevel@tonic-gate Map	*map = NULL;			/* device font map table */
103*7c478bd9Sstevel@tonic-gate char	*stringspace = NULL;		/* for storing font and file strings */
104*7c478bd9Sstevel@tonic-gate int	next = 0;			/* next free slot in map[] */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate char	*residentfonts = NULL;		/* list of printer resident fonts */
107*7c478bd9Sstevel@tonic-gate char	*printer = NULL;		/* printer name - only for Unix 4.0 lp */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate char	buf[2048];			/* input file line buffer */
110*7c478bd9Sstevel@tonic-gate char	*comment = DOCUMENTFONTS;	/* look for this comment */
111*7c478bd9Sstevel@tonic-gate int	atend = FALSE;			/* TRUE only if a comment says so */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate FILE	*fp_in = stdin;			/* next input file */
114*7c478bd9Sstevel@tonic-gate FILE	*fp_temp = NULL;		/* for copying stdin */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate main(agc, agv)
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate     int		agc;
121*7c478bd9Sstevel@tonic-gate     char	*agv[];
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  *
127*7c478bd9Sstevel@tonic-gate  * Host resident font download. The input files are assumed to be part of a
128*7c478bd9Sstevel@tonic-gate  * single PostScript job.
129*7c478bd9Sstevel@tonic-gate  *
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate     argc = agc;				/* other routines may want them */
133*7c478bd9Sstevel@tonic-gate     argv = agv;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate     prog_name = argv[0];		/* just for error messages */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate     init_signals();			/* sets up interrupt handling */
138*7c478bd9Sstevel@tonic-gate     options();				/* first get command line options */
139*7c478bd9Sstevel@tonic-gate     readmap();				/* read the font map table */
140*7c478bd9Sstevel@tonic-gate     readresident();			/* and the optional resident font list */
141*7c478bd9Sstevel@tonic-gate     arguments();			/* then process non-option arguments */
142*7c478bd9Sstevel@tonic-gate     done();				/* and clean things up */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate     exit(x_stat);			/* not much could be wrong */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate }   /* End of main */
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate init_signals()
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate     void	interrupt();		/* handles signals if we catching them */
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate /*
157*7c478bd9Sstevel@tonic-gate  *
158*7c478bd9Sstevel@tonic-gate  * Makes sure we handle interrupts properly.
159*7c478bd9Sstevel@tonic-gate  *
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate     if ( signal(SIGINT, interrupt) == SIG_IGN )  {
163*7c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
164*7c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
165*7c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
166*7c478bd9Sstevel@tonic-gate     } else {
167*7c478bd9Sstevel@tonic-gate 	signal(SIGHUP, interrupt);
168*7c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, interrupt);
169*7c478bd9Sstevel@tonic-gate     }   /* End else */
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate     signal(SIGTERM, interrupt);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate }   /* End of init_signals */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate options()
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate     int		ch;			/* return value from getopt() */
182*7c478bd9Sstevel@tonic-gate     char	*optnames = "c:fm:p:r:H:T:DI";
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate     extern char	*optarg;		/* used by getopt() */
185*7c478bd9Sstevel@tonic-gate     extern int	optind;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /*
188*7c478bd9Sstevel@tonic-gate  *
189*7c478bd9Sstevel@tonic-gate  * Reads and processes the command line options.
190*7c478bd9Sstevel@tonic-gate  *
191*7c478bd9Sstevel@tonic-gate  */
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate     while ( (ch = getopt(argc, argv, optnames)) != EOF )  {
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	switch ( ch )  {
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	    case 'c':			/* look for this comment */
198*7c478bd9Sstevel@tonic-gate 		    comment = optarg;
199*7c478bd9Sstevel@tonic-gate 		    break;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	    case 'f':			/* force a complete input file scan */
202*7c478bd9Sstevel@tonic-gate 		    atend = TRUE;
203*7c478bd9Sstevel@tonic-gate 		    break;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	    case 'm':			/* printer map table name */
206*7c478bd9Sstevel@tonic-gate 		    mapname = optarg;
207*7c478bd9Sstevel@tonic-gate 		    break;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	    case 'p':			/* printer name - for Unix 4.0 lp */
210*7c478bd9Sstevel@tonic-gate 		    printer = optarg;
211*7c478bd9Sstevel@tonic-gate 		    break;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	    case 'r':			/* resident font list */
214*7c478bd9Sstevel@tonic-gate 		    residentfonts = optarg;
215*7c478bd9Sstevel@tonic-gate 		    break;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	    case 'H':			/* host resident font directory */
218*7c478bd9Sstevel@tonic-gate 		    hostfontdir = optarg;
219*7c478bd9Sstevel@tonic-gate 		    break;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	    case 'T':			/* temporary file directory */
222*7c478bd9Sstevel@tonic-gate 		    temp_dir = optarg;
223*7c478bd9Sstevel@tonic-gate 		    break;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	    case 'D':			/* debug flag */
226*7c478bd9Sstevel@tonic-gate 		    debug = ON;
227*7c478bd9Sstevel@tonic-gate 		    break;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	    case 'I':			/* ignore FATAL errors */
230*7c478bd9Sstevel@tonic-gate 		    ignore = ON;
231*7c478bd9Sstevel@tonic-gate 		    break;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	    case '?':			/* don't understand the option */
234*7c478bd9Sstevel@tonic-gate 		    error(FATAL, "");
235*7c478bd9Sstevel@tonic-gate 		    break;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	    default:			/* don't know what to do for ch */
238*7c478bd9Sstevel@tonic-gate 		    error(FATAL, "missing case for option %c\n", ch);
239*7c478bd9Sstevel@tonic-gate 		    break;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	}   /* End switch */
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate     }   /* End while */
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate     argc -= optind;			/* get ready for non-option args */
246*7c478bd9Sstevel@tonic-gate     argv += optind;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate }   /* End of options */
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate readmap()
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate {
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate     char	*path;
257*7c478bd9Sstevel@tonic-gate     char	*ptr;
258*7c478bd9Sstevel@tonic-gate     int		fd;
259*7c478bd9Sstevel@tonic-gate     struct stat	sbuf;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate /*
262*7c478bd9Sstevel@tonic-gate  *
263*7c478bd9Sstevel@tonic-gate  * Initializes the map table by reading an ASCII mapping file. If mapname begins
264*7c478bd9Sstevel@tonic-gate  * with a / it's the map table. Otherwise hostfontdir, mapname, and suffix are
265*7c478bd9Sstevel@tonic-gate  * combined to build the final pathname. If we can open the file we read it all
266*7c478bd9Sstevel@tonic-gate  * into memory, erase comments, and separate the font and file name pairs. When
267*7c478bd9Sstevel@tonic-gate  * we leave next points to the next free slot in the map[] array. If it's zero
268*7c478bd9Sstevel@tonic-gate  * nothing was in the file or we couldn't open it.
269*7c478bd9Sstevel@tonic-gate  *
270*7c478bd9Sstevel@tonic-gate  */
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate     if ( hostfontdir == NULL || mapname == NULL )
273*7c478bd9Sstevel@tonic-gate 	return;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate     if ( *mapname != '/' ) {
276*7c478bd9Sstevel@tonic-gate 	if ( (path = malloc(strlen(hostfontdir) + strlen(mapname) +
277*7c478bd9Sstevel@tonic-gate 						strlen(suffix) + 2)) == NULL )
278*7c478bd9Sstevel@tonic-gate 	    error(FATAL, "no memory");
279*7c478bd9Sstevel@tonic-gate 	sprintf(path, "%s/%s%s", hostfontdir, mapname, suffix);
280*7c478bd9Sstevel@tonic-gate     } else path = mapname;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate     if ( (fd = open(path, 0)) != -1 ) {
283*7c478bd9Sstevel@tonic-gate 	if ( fstat(fd, &sbuf) == -1 )
284*7c478bd9Sstevel@tonic-gate 	    error(FATAL, "can't fstat %s", path);
285*7c478bd9Sstevel@tonic-gate 	if ( (stringspace = malloc(sbuf.st_size + 2)) == NULL )
286*7c478bd9Sstevel@tonic-gate 	    error(FATAL, "no memory");
287*7c478bd9Sstevel@tonic-gate 	if ( read(fd, stringspace, sbuf.st_size) == -1 )
288*7c478bd9Sstevel@tonic-gate 	    error(FATAL, "can't read %s", path);
289*7c478bd9Sstevel@tonic-gate 	close(fd);
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	stringspace[sbuf.st_size] = '\n';	/* just to be safe */
292*7c478bd9Sstevel@tonic-gate 	stringspace[sbuf.st_size+1] = '\0';
293*7c478bd9Sstevel@tonic-gate 	for ( ptr = stringspace; *ptr != '\0'; ptr++ )	/* erase comments */
294*7c478bd9Sstevel@tonic-gate 	    if ( *ptr == '%' )
295*7c478bd9Sstevel@tonic-gate 		for ( ; *ptr != '\n' ; ptr++ )
296*7c478bd9Sstevel@tonic-gate 		    *ptr = ' ';
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	for ( ptr = stringspace; ; next++ ) {
299*7c478bd9Sstevel@tonic-gate 	    if ( (next % 50) == 0 )
300*7c478bd9Sstevel@tonic-gate 		map = allocate(map, next+50);
301*7c478bd9Sstevel@tonic-gate 	    map[next].downloaded = FALSE;
302*7c478bd9Sstevel@tonic-gate 	    map[next].font = strtok(ptr, " \t\n");
303*7c478bd9Sstevel@tonic-gate 	    map[next].file = strtok(ptr = NULL, " \t\n");
304*7c478bd9Sstevel@tonic-gate 	    if ( map[next].font == NULL )
305*7c478bd9Sstevel@tonic-gate 		break;
306*7c478bd9Sstevel@tonic-gate 	    if ( map[next].file == NULL )
307*7c478bd9Sstevel@tonic-gate 		error(FATAL, "map table format error - check %s", path);
308*7c478bd9Sstevel@tonic-gate 	}   /* End for */
309*7c478bd9Sstevel@tonic-gate     }	/* End if */
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate }   /* End of readmap */
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate readresident()
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate     FILE	*fp;
320*7c478bd9Sstevel@tonic-gate     char	*path;
321*7c478bd9Sstevel@tonic-gate     int		ch;
322*7c478bd9Sstevel@tonic-gate     int		n;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate /*
325*7c478bd9Sstevel@tonic-gate  *
326*7c478bd9Sstevel@tonic-gate  * Reads a file that lists the resident fonts for a particular printer and marks
327*7c478bd9Sstevel@tonic-gate  * each font as already downloaded. Nothing's done if the file can't be read or
328*7c478bd9Sstevel@tonic-gate  * there's no mapping file. Comments, as in the map file, begin with a % and
329*7c478bd9Sstevel@tonic-gate  * extend to the end of the line. Added for Unix 4.0 lp.
330*7c478bd9Sstevel@tonic-gate  *
331*7c478bd9Sstevel@tonic-gate  */
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate     if ( next == 0 || (printer == NULL && residentfonts == NULL) )
334*7c478bd9Sstevel@tonic-gate 	return;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate     if ( printer != NULL ) {		/* use Unix 4.0 lp pathnames */
337*7c478bd9Sstevel@tonic-gate 	sprintf(buf, "/etc/lp/printers/%s/residentfonts", printer);
338*7c478bd9Sstevel@tonic-gate 	path = buf;
339*7c478bd9Sstevel@tonic-gate     } else path = residentfonts;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate     if ( (fp = fopen(path, "r")) != NULL ) {
342*7c478bd9Sstevel@tonic-gate 	while ( fscanf(fp, "%s", buf) != EOF )
343*7c478bd9Sstevel@tonic-gate 	    if ( buf[0] == '%' )
344*7c478bd9Sstevel@tonic-gate 		while ( (ch = getc(fp)) != EOF && ch != '\n' ) ;
345*7c478bd9Sstevel@tonic-gate 	    else if ( (n = lookup(buf)) < next )
346*7c478bd9Sstevel@tonic-gate 		map[n].downloaded = TRUE;
347*7c478bd9Sstevel@tonic-gate 	fclose(fp);
348*7c478bd9Sstevel@tonic-gate     }	/* End if */
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate }   /* End of readresident */
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate arguments()
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate /*
359*7c478bd9Sstevel@tonic-gate  *
360*7c478bd9Sstevel@tonic-gate  * Makes sure all the non-option command line arguments are processed. If we get
361*7c478bd9Sstevel@tonic-gate  * here and there aren't any arguments left, or if '-' is one of the input files
362*7c478bd9Sstevel@tonic-gate  * we'll translate stdin. Assumes input files are part of a single PostScript
363*7c478bd9Sstevel@tonic-gate  * job and fonts can be downloaded at the start of each file.
364*7c478bd9Sstevel@tonic-gate  *
365*7c478bd9Sstevel@tonic-gate  */
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate     if ( argc < 1 )
368*7c478bd9Sstevel@tonic-gate 	download();
369*7c478bd9Sstevel@tonic-gate     else {
370*7c478bd9Sstevel@tonic-gate 	while ( argc > 0 ) {
371*7c478bd9Sstevel@tonic-gate 	    fp_temp = NULL;
372*7c478bd9Sstevel@tonic-gate 	    if ( strcmp(*argv, "-") == 0 )
373*7c478bd9Sstevel@tonic-gate 		fp_in = stdin;
374*7c478bd9Sstevel@tonic-gate 	    else if ( (fp_in = fopen(*argv, "r")) == NULL )
375*7c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open %s", *argv);
376*7c478bd9Sstevel@tonic-gate 	    download();
377*7c478bd9Sstevel@tonic-gate 	    if ( fp_in != stdin )
378*7c478bd9Sstevel@tonic-gate 		fclose(fp_in);
379*7c478bd9Sstevel@tonic-gate 	    if ( fp_temp != NULL )
380*7c478bd9Sstevel@tonic-gate 		fclose(fp_temp);
381*7c478bd9Sstevel@tonic-gate 	    argc--;
382*7c478bd9Sstevel@tonic-gate 	    argv++;
383*7c478bd9Sstevel@tonic-gate 	}   /* End while */
384*7c478bd9Sstevel@tonic-gate     }	/* End else */
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate }   /* End of arguments */
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate done()
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate {
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate /*
395*7c478bd9Sstevel@tonic-gate  *
396*7c478bd9Sstevel@tonic-gate  * Clean things up before we quit.
397*7c478bd9Sstevel@tonic-gate  *
398*7c478bd9Sstevel@tonic-gate  */
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate     if ( temp_file != NULL )
401*7c478bd9Sstevel@tonic-gate 	unlink(temp_file);
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate }   /* End of done */
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate download()
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate     int		infontlist = FALSE;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate  *
415*7c478bd9Sstevel@tonic-gate  * If next is zero the map table is empty and all we do is copy the input file
416*7c478bd9Sstevel@tonic-gate  * to stdout. Otherwise we read the input file looking for %%DocumentFonts: or
417*7c478bd9Sstevel@tonic-gate  * continuation comments, add any accessible fonts to the output file, and then
418*7c478bd9Sstevel@tonic-gate  * append the input file. When reading stdin we append lines to fp_temp and
419*7c478bd9Sstevel@tonic-gate  * recover them when we're ready to copy the input file. fp_temp will often
420*7c478bd9Sstevel@tonic-gate  * only contain part of stdin - if there's no %%DocumentFonts: (atend) comment
421*7c478bd9Sstevel@tonic-gate  * we stop reading fp_in after the header.
422*7c478bd9Sstevel@tonic-gate  *
423*7c478bd9Sstevel@tonic-gate  */
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate     if ( next > 0 ) {
426*7c478bd9Sstevel@tonic-gate 	if ( fp_in == stdin ) {
427*7c478bd9Sstevel@tonic-gate 	    if ( (temp_file = tempnam(temp_dir, "post")) == NULL )
428*7c478bd9Sstevel@tonic-gate 		error(FATAL, "can't generate temp file name");
429*7c478bd9Sstevel@tonic-gate 	    if ( (fp_temp = fopen(temp_file, "w+")) == NULL )
430*7c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open %s", temp_file);
431*7c478bd9Sstevel@tonic-gate 	    unlink(temp_file);
432*7c478bd9Sstevel@tonic-gate 	    temp_file = NULL;
433*7c478bd9Sstevel@tonic-gate 	}   /* End if */
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	while ( fgets(buf, sizeof(buf), fp_in) != NULL ) {
436*7c478bd9Sstevel@tonic-gate 	    if ( fp_temp != NULL )
437*7c478bd9Sstevel@tonic-gate 		fprintf(fp_temp, "%s", buf);
438*7c478bd9Sstevel@tonic-gate 	    if ( buf[0] != '%' || buf[1] != '%' ) {
439*7c478bd9Sstevel@tonic-gate 		if ( (buf[0] != '%' || buf[1] != '!') && atend == FALSE )
440*7c478bd9Sstevel@tonic-gate 		    break;
441*7c478bd9Sstevel@tonic-gate 		infontlist = FALSE;
442*7c478bd9Sstevel@tonic-gate 	    } else if ( strncmp(buf, comment, strlen(comment)) == 0 ) {
443*7c478bd9Sstevel@tonic-gate 		copyfonts(buf);
444*7c478bd9Sstevel@tonic-gate 		infontlist = TRUE;
445*7c478bd9Sstevel@tonic-gate 	    } else if ( buf[2] == '+' && infontlist == TRUE )
446*7c478bd9Sstevel@tonic-gate 		copyfonts(buf);
447*7c478bd9Sstevel@tonic-gate 	    else infontlist = FALSE;
448*7c478bd9Sstevel@tonic-gate 	}   /* End while */
449*7c478bd9Sstevel@tonic-gate     }	/* End if */
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate     copyinput();
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate }   /* End of download */
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate copyfonts(list)
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate     char	*list;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate {
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate     char	*font;
464*7c478bd9Sstevel@tonic-gate     char	*path;
465*7c478bd9Sstevel@tonic-gate     int		n;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate /*
468*7c478bd9Sstevel@tonic-gate  *
469*7c478bd9Sstevel@tonic-gate  * list points to a %%DocumentFonts: or continuation comment. What follows the
470*7c478bd9Sstevel@tonic-gate  * the keyword will be a list of fonts separated by white space (or (atend)).
471*7c478bd9Sstevel@tonic-gate  * Look for each font in the map table and if it's found copy the font file to
472*7c478bd9Sstevel@tonic-gate  * stdout (once only).
473*7c478bd9Sstevel@tonic-gate  *
474*7c478bd9Sstevel@tonic-gate  */
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate     strtok(list, " \n");		/* skip to the font list */
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate     while ( (font = strtok(NULL, " \t\n")) != NULL ) {
479*7c478bd9Sstevel@tonic-gate 	if ( strcmp(font, ATEND) == 0 ) {
480*7c478bd9Sstevel@tonic-gate 	    atend = TRUE;
481*7c478bd9Sstevel@tonic-gate 	    break;
482*7c478bd9Sstevel@tonic-gate 	}   /* End if */
483*7c478bd9Sstevel@tonic-gate 	if ( (n = lookup(font)) < next ) {
484*7c478bd9Sstevel@tonic-gate 	    if ( *map[n].file != '/' ) {
485*7c478bd9Sstevel@tonic-gate 		if ( (path = malloc(strlen(hostfontdir)+strlen(map[n].file)+2)) == NULL )
486*7c478bd9Sstevel@tonic-gate 		    error(FATAL, "no memory");
487*7c478bd9Sstevel@tonic-gate 		sprintf(path, "%s/%s", hostfontdir, map[n].file);
488*7c478bd9Sstevel@tonic-gate 		cat(path);
489*7c478bd9Sstevel@tonic-gate 		free(path);
490*7c478bd9Sstevel@tonic-gate 	    } else cat(map[n].file);
491*7c478bd9Sstevel@tonic-gate 	    map[n].downloaded = TRUE;
492*7c478bd9Sstevel@tonic-gate 	}   /* End if */
493*7c478bd9Sstevel@tonic-gate     }	/* End while */
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate }   /* End of copyfonts */
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate copyinput()
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate {
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate /*
504*7c478bd9Sstevel@tonic-gate  *
505*7c478bd9Sstevel@tonic-gate  * Copies the input file to stdout. If fp_temp isn't NULL seek to the start and
506*7c478bd9Sstevel@tonic-gate  * add it to the output file - it's a partial (or complete) copy of stdin made
507*7c478bd9Sstevel@tonic-gate  * by download(). Then copy fp_in, but only seek to the start if it's not stdin.
508*7c478bd9Sstevel@tonic-gate  *
509*7c478bd9Sstevel@tonic-gate  */
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate     if ( fp_temp != NULL ) {
512*7c478bd9Sstevel@tonic-gate 	fseek(fp_temp, 0L, 0);
513*7c478bd9Sstevel@tonic-gate 	while ( fgets(buf, sizeof(buf), fp_temp) != NULL )
514*7c478bd9Sstevel@tonic-gate 	    printf("%s", buf);
515*7c478bd9Sstevel@tonic-gate     }	/* End if */
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate     if ( fp_in != stdin )
518*7c478bd9Sstevel@tonic-gate 	fseek(fp_in, 0L, 0);
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate     while ( fgets(buf, sizeof(buf), fp_in) != NULL )
521*7c478bd9Sstevel@tonic-gate 	printf("%s", buf);
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate }   /* End of copyinput */
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate lookup(font)
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate     char	*font;
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate {
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate     int		i;
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate /*
536*7c478bd9Sstevel@tonic-gate  *
537*7c478bd9Sstevel@tonic-gate  * Looks for *font in the map table. Return the map table index if found and
538*7c478bd9Sstevel@tonic-gate  * not yet downloaded - otherwise return next.
539*7c478bd9Sstevel@tonic-gate  *
540*7c478bd9Sstevel@tonic-gate  */
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate     for ( i = 0; i < next; i++ )
543*7c478bd9Sstevel@tonic-gate 	if ( strcmp(font, map[i].font) == 0 ) {
544*7c478bd9Sstevel@tonic-gate 	    if ( map[i].downloaded == TRUE )
545*7c478bd9Sstevel@tonic-gate 		i = next;
546*7c478bd9Sstevel@tonic-gate 	    break;
547*7c478bd9Sstevel@tonic-gate 	}   /* End if */
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate     return(i);
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate }   /* End of lookup */
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate Map *allocate(ptr, num)
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate     Map		*ptr;
558*7c478bd9Sstevel@tonic-gate     int		num;
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate {
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate /*
563*7c478bd9Sstevel@tonic-gate  *
564*7c478bd9Sstevel@tonic-gate  * Allocates space for num Map elements. Calls malloc() if ptr is NULL and
565*7c478bd9Sstevel@tonic-gate  * realloc() otherwise.
566*7c478bd9Sstevel@tonic-gate  *
567*7c478bd9Sstevel@tonic-gate  */
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate     if ( ptr == NULL )
570*7c478bd9Sstevel@tonic-gate 	ptr = (Map *)malloc(num * sizeof(Map));
571*7c478bd9Sstevel@tonic-gate     else ptr = (Map *)realloc(ptr, num * sizeof(Map));
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate     if ( ptr == NULL )
574*7c478bd9Sstevel@tonic-gate 	error(FATAL, "no map memory");
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate     return(ptr);
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate }   /* End of allocate */
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate /*****************************************************************************/
581*7c478bd9Sstevel@tonic-gate 
582