17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*f928ce67Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*f928ce67Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * download - host resident font downloader
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Prepends host resident fonts to PostScript input files. The program assumes
377c478bd9Sstevel@tonic-gate  * the input files are part of a single PostScript job and that requested fonts
387c478bd9Sstevel@tonic-gate  * can be downloaded at the start of each input file. Downloaded fonts are the
397c478bd9Sstevel@tonic-gate  * ones named in a %%DocumentFonts: comment and listed in a special map table.
407c478bd9Sstevel@tonic-gate  * Map table pathnames (supplied using the -m option) that begin with a / are
417c478bd9Sstevel@tonic-gate  * taken as is. Otherwise the final pathname is built using *hostfontdir (-H
427c478bd9Sstevel@tonic-gate  * option), *mapname (-m option), and *suffix.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The map table consists of fontname-filename pairs, separated by white space.
457c478bd9Sstevel@tonic-gate  * Comments are introduced by % (as in PostScript) and extend to the end of the
467c478bd9Sstevel@tonic-gate  * current line. The only fonts that can be downloaded are the ones listed in
477c478bd9Sstevel@tonic-gate  * the active map table that point the program to a readable Unix file. A request
487c478bd9Sstevel@tonic-gate  * for an unlisted font or inaccessible file is ignored. All font requests are
497c478bd9Sstevel@tonic-gate  * ignored if the map table can't be read. In that case the program simply copies
507c478bd9Sstevel@tonic-gate  * the input files to stdout.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * An example (but not one to follow) of what can be in a map table is,
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *	%
557c478bd9Sstevel@tonic-gate  *	% Map requests for Bookman-Light to file *hostfontdir/KR
567c478bd9Sstevel@tonic-gate  *	%
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *	  Bookman-Light		KR	% Keeping everything (including the map
597c478bd9Sstevel@tonic-gate  *					% table) in *hostfontdir seems like the
607c478bd9Sstevel@tonic-gate  *					% cleanest approach.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  *	%
637c478bd9Sstevel@tonic-gate  *	% Map Palatino-Roman to file *hostfontdir/palatino/Roman
647c478bd9Sstevel@tonic-gate  *	%
657c478bd9Sstevel@tonic-gate  *	  Palatino-Roman	palatino/Roman
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  *	% Map ZapfDingbats to file /usr/lib/host/dingbats
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  *	  ZapfDingbats		/usr/lib/host/dingbats
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * Once again, file names that begin with a / are taken as is. All others have
727c478bd9Sstevel@tonic-gate  * *hostfontdir/ prepended to the file string associated with a particular font.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Map table can be associated with a printer model (e.g. a LaserWriter), a
757c478bd9Sstevel@tonic-gate  * printer destination, or whatever - the choice is up to an administrator.
767c478bd9Sstevel@tonic-gate  * By destination may be best if your spooler is running several private
777c478bd9Sstevel@tonic-gate  * printers. Host resident fonts are usually purchased under a license that
787c478bd9Sstevel@tonic-gate  * restricts their use to a limited number of printers. A font licensed for
797c478bd9Sstevel@tonic-gate  * a single printer should only be used on that printer.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * Was written quickly, so there's much room for improvement. Undoubtedly should
827c478bd9Sstevel@tonic-gate  * be a more general program (e.g. scan for other comments).
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <stdio.h>
877c478bd9Sstevel@tonic-gate #include <signal.h>
887c478bd9Sstevel@tonic-gate #include <fcntl.h>
897c478bd9Sstevel@tonic-gate #include <sys/types.h>
907c478bd9Sstevel@tonic-gate #include <sys/stat.h>
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #include "comments.h"			/* PostScript file structuring comments */
937c478bd9Sstevel@tonic-gate #include "gen.h"			/* general purpose definitions */
947c478bd9Sstevel@tonic-gate #include "path.h"			/* for temporary directory */
957c478bd9Sstevel@tonic-gate #include "ext.h"			/* external variable declarations */
967c478bd9Sstevel@tonic-gate #include "download.h"			/* a few special definitions */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate char	*temp_dir = TEMPDIR;		/* temp directory - for copying stdin */
997c478bd9Sstevel@tonic-gate char	*hostfontdir = HOSTFONTDIR;	/* host resident directory */
1007c478bd9Sstevel@tonic-gate char	*mapname = "map";		/* map table - usually in *hostfontdir */
1017c478bd9Sstevel@tonic-gate char	*suffix = "";			/* appended to the map table pathname */
1027c478bd9Sstevel@tonic-gate Map	*map = NULL;			/* device font map table */
1037c478bd9Sstevel@tonic-gate char	*stringspace = NULL;		/* for storing font and file strings */
1047c478bd9Sstevel@tonic-gate int	next = 0;			/* next free slot in map[] */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate char	*residentfonts = NULL;		/* list of printer resident fonts */
1077c478bd9Sstevel@tonic-gate char	*printer = NULL;		/* printer name - only for Unix 4.0 lp */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate char	buf[2048];			/* input file line buffer */
1107c478bd9Sstevel@tonic-gate char	*comment = DOCUMENTFONTS;	/* look for this comment */
1117c478bd9Sstevel@tonic-gate int	atend = FALSE;			/* TRUE only if a comment says so */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate FILE	*fp_in = stdin;			/* next input file */
1147c478bd9Sstevel@tonic-gate FILE	*fp_temp = NULL;		/* for copying stdin */
1157c478bd9Sstevel@tonic-gate 
116*f928ce67Sceastha static Map *allocate(Map *, int);
117*f928ce67Sceastha static void arguments(void);
118*f928ce67Sceastha static void copyfonts(char *);
119*f928ce67Sceastha static void copyinput(void);
120*f928ce67Sceastha static void done(void);
121*f928ce67Sceastha static void download(void);
122*f928ce67Sceastha static void init_signals(void);
123*f928ce67Sceastha static int lookup(char *);
124*f928ce67Sceastha static void options(void);
125*f928ce67Sceastha static void readmap(void);
126*f928ce67Sceastha static void readresident(void);
1277c478bd9Sstevel@tonic-gate 
128*f928ce67Sceastha /*****************************************************************************/
1297c478bd9Sstevel@tonic-gate 
130*f928ce67Sceastha int
main(int agc,char * agv[])131*f928ce67Sceastha main(int agc, char *agv[])
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * Host resident font download. The input files are assumed to be part of a
1377c478bd9Sstevel@tonic-gate  * single PostScript job.
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate     argc = agc;				/* other routines may want them */
1427c478bd9Sstevel@tonic-gate     argv = agv;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate     prog_name = argv[0];		/* just for error messages */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate     init_signals();			/* sets up interrupt handling */
1477c478bd9Sstevel@tonic-gate     options();				/* first get command line options */
1487c478bd9Sstevel@tonic-gate     readmap();				/* read the font map table */
1497c478bd9Sstevel@tonic-gate     readresident();			/* and the optional resident font list */
1507c478bd9Sstevel@tonic-gate     arguments();			/* then process non-option arguments */
1517c478bd9Sstevel@tonic-gate     done();				/* and clean things up */
1527c478bd9Sstevel@tonic-gate 
153*f928ce67Sceastha     return (x_stat);			/* not much could be wrong */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate }   /* End of main */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*****************************************************************************/
1587c478bd9Sstevel@tonic-gate 
159*f928ce67Sceastha static void
init_signals(void)160*f928ce67Sceastha init_signals(void)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate     void	interrupt();		/* handles signals if we catching them */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  * Makes sure we handle interrupts properly.
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate     if ( signal(SIGINT, interrupt) == SIG_IGN )  {
1717c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
1727c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
1737c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
1747c478bd9Sstevel@tonic-gate     } else {
1757c478bd9Sstevel@tonic-gate 	signal(SIGHUP, interrupt);
1767c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, interrupt);
1777c478bd9Sstevel@tonic-gate     }   /* End else */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate     signal(SIGTERM, interrupt);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate }   /* End of init_signals */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*****************************************************************************/
1847c478bd9Sstevel@tonic-gate 
185*f928ce67Sceastha static void
options(void)186*f928ce67Sceastha options(void)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate     int		ch;			/* return value from getopt() */
1907c478bd9Sstevel@tonic-gate     char	*optnames = "c:fm:p:r:H:T:DI";
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate     extern char	*optarg;		/* used by getopt() */
1937c478bd9Sstevel@tonic-gate     extern int	optind;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * Reads and processes the command line options.
1987c478bd9Sstevel@tonic-gate  *
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate     while ( (ch = getopt(argc, argv, optnames)) != EOF )  {
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	switch ( ch )  {
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	    case 'c':			/* look for this comment */
2067c478bd9Sstevel@tonic-gate 		    comment = optarg;
2077c478bd9Sstevel@tonic-gate 		    break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	    case 'f':			/* force a complete input file scan */
2107c478bd9Sstevel@tonic-gate 		    atend = TRUE;
2117c478bd9Sstevel@tonic-gate 		    break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	    case 'm':			/* printer map table name */
2147c478bd9Sstevel@tonic-gate 		    mapname = optarg;
2157c478bd9Sstevel@tonic-gate 		    break;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	    case 'p':			/* printer name - for Unix 4.0 lp */
2187c478bd9Sstevel@tonic-gate 		    printer = optarg;
2197c478bd9Sstevel@tonic-gate 		    break;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	    case 'r':			/* resident font list */
2227c478bd9Sstevel@tonic-gate 		    residentfonts = optarg;
2237c478bd9Sstevel@tonic-gate 		    break;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	    case 'H':			/* host resident font directory */
2267c478bd9Sstevel@tonic-gate 		    hostfontdir = optarg;
2277c478bd9Sstevel@tonic-gate 		    break;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	    case 'T':			/* temporary file directory */
2307c478bd9Sstevel@tonic-gate 		    temp_dir = optarg;
2317c478bd9Sstevel@tonic-gate 		    break;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	    case 'D':			/* debug flag */
2347c478bd9Sstevel@tonic-gate 		    debug = ON;
2357c478bd9Sstevel@tonic-gate 		    break;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	    case 'I':			/* ignore FATAL errors */
2387c478bd9Sstevel@tonic-gate 		    ignore = ON;
2397c478bd9Sstevel@tonic-gate 		    break;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	    case '?':			/* don't understand the option */
2427c478bd9Sstevel@tonic-gate 		    error(FATAL, "");
2437c478bd9Sstevel@tonic-gate 		    break;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	    default:			/* don't know what to do for ch */
2467c478bd9Sstevel@tonic-gate 		    error(FATAL, "missing case for option %c\n", ch);
2477c478bd9Sstevel@tonic-gate 		    break;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	}   /* End switch */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate     }   /* End while */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate     argc -= optind;			/* get ready for non-option args */
2547c478bd9Sstevel@tonic-gate     argv += optind;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate }   /* End of options */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*****************************************************************************/
2597c478bd9Sstevel@tonic-gate 
260*f928ce67Sceastha static void
readmap(void)261*f928ce67Sceastha readmap(void)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate     char	*path;
2647c478bd9Sstevel@tonic-gate     char	*ptr;
2657c478bd9Sstevel@tonic-gate     int		fd;
2667c478bd9Sstevel@tonic-gate     struct stat	sbuf;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  * Initializes the map table by reading an ASCII mapping file. If mapname begins
2717c478bd9Sstevel@tonic-gate  * with a / it's the map table. Otherwise hostfontdir, mapname, and suffix are
2727c478bd9Sstevel@tonic-gate  * combined to build the final pathname. If we can open the file we read it all
2737c478bd9Sstevel@tonic-gate  * into memory, erase comments, and separate the font and file name pairs. When
2747c478bd9Sstevel@tonic-gate  * we leave next points to the next free slot in the map[] array. If it's zero
2757c478bd9Sstevel@tonic-gate  * nothing was in the file or we couldn't open it.
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate     if ( hostfontdir == NULL || mapname == NULL )
2807c478bd9Sstevel@tonic-gate 	return;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate     if ( *mapname != '/' ) {
2837c478bd9Sstevel@tonic-gate 	if ( (path = malloc(strlen(hostfontdir) + strlen(mapname) +
2847c478bd9Sstevel@tonic-gate 						strlen(suffix) + 2)) == NULL )
2857c478bd9Sstevel@tonic-gate 	    error(FATAL, "no memory");
2867c478bd9Sstevel@tonic-gate 	sprintf(path, "%s/%s%s", hostfontdir, mapname, suffix);
2877c478bd9Sstevel@tonic-gate     } else path = mapname;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate     if ( (fd = open(path, 0)) != -1 ) {
2907c478bd9Sstevel@tonic-gate 	if ( fstat(fd, &sbuf) == -1 )
2917c478bd9Sstevel@tonic-gate 	    error(FATAL, "can't fstat %s", path);
2927c478bd9Sstevel@tonic-gate 	if ( (stringspace = malloc(sbuf.st_size + 2)) == NULL )
2937c478bd9Sstevel@tonic-gate 	    error(FATAL, "no memory");
2947c478bd9Sstevel@tonic-gate 	if ( read(fd, stringspace, sbuf.st_size) == -1 )
2957c478bd9Sstevel@tonic-gate 	    error(FATAL, "can't read %s", path);
2967c478bd9Sstevel@tonic-gate 	close(fd);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	stringspace[sbuf.st_size] = '\n';	/* just to be safe */
2997c478bd9Sstevel@tonic-gate 	stringspace[sbuf.st_size+1] = '\0';
3007c478bd9Sstevel@tonic-gate 	for ( ptr = stringspace; *ptr != '\0'; ptr++ )	/* erase comments */
3017c478bd9Sstevel@tonic-gate 	    if ( *ptr == '%' )
3027c478bd9Sstevel@tonic-gate 		for ( ; *ptr != '\n' ; ptr++ )
3037c478bd9Sstevel@tonic-gate 		    *ptr = ' ';
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	for ( ptr = stringspace; ; next++ ) {
3067c478bd9Sstevel@tonic-gate 	    if ( (next % 50) == 0 )
3077c478bd9Sstevel@tonic-gate 		map = allocate(map, next+50);
3087c478bd9Sstevel@tonic-gate 	    map[next].downloaded = FALSE;
3097c478bd9Sstevel@tonic-gate 	    map[next].font = strtok(ptr, " \t\n");
3107c478bd9Sstevel@tonic-gate 	    map[next].file = strtok(ptr = NULL, " \t\n");
3117c478bd9Sstevel@tonic-gate 	    if ( map[next].font == NULL )
3127c478bd9Sstevel@tonic-gate 		break;
3137c478bd9Sstevel@tonic-gate 	    if ( map[next].file == NULL )
3147c478bd9Sstevel@tonic-gate 		error(FATAL, "map table format error - check %s", path);
3157c478bd9Sstevel@tonic-gate 	}   /* End for */
3167c478bd9Sstevel@tonic-gate     }	/* End if */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate }   /* End of readmap */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*****************************************************************************/
3217c478bd9Sstevel@tonic-gate 
322*f928ce67Sceastha static void
readresident(void)323*f928ce67Sceastha readresident(void)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate     FILE	*fp;
3267c478bd9Sstevel@tonic-gate     char	*path;
3277c478bd9Sstevel@tonic-gate     int		ch;
3287c478bd9Sstevel@tonic-gate     int		n;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  *
3327c478bd9Sstevel@tonic-gate  * Reads a file that lists the resident fonts for a particular printer and marks
3337c478bd9Sstevel@tonic-gate  * each font as already downloaded. Nothing's done if the file can't be read or
3347c478bd9Sstevel@tonic-gate  * there's no mapping file. Comments, as in the map file, begin with a % and
3357c478bd9Sstevel@tonic-gate  * extend to the end of the line. Added for Unix 4.0 lp.
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate     if ( next == 0 || (printer == NULL && residentfonts == NULL) )
3407c478bd9Sstevel@tonic-gate 	return;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate     if ( printer != NULL ) {		/* use Unix 4.0 lp pathnames */
3437c478bd9Sstevel@tonic-gate 	sprintf(buf, "/etc/lp/printers/%s/residentfonts", printer);
3447c478bd9Sstevel@tonic-gate 	path = buf;
3457c478bd9Sstevel@tonic-gate     } else path = residentfonts;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate     if ( (fp = fopen(path, "r")) != NULL ) {
3487c478bd9Sstevel@tonic-gate 	while ( fscanf(fp, "%s", buf) != EOF )
3497c478bd9Sstevel@tonic-gate 	    if ( buf[0] == '%' )
3507c478bd9Sstevel@tonic-gate 		while ( (ch = getc(fp)) != EOF && ch != '\n' ) ;
3517c478bd9Sstevel@tonic-gate 	    else if ( (n = lookup(buf)) < next )
3527c478bd9Sstevel@tonic-gate 		map[n].downloaded = TRUE;
3537c478bd9Sstevel@tonic-gate 	fclose(fp);
3547c478bd9Sstevel@tonic-gate     }	/* End if */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate }   /* End of readresident */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*****************************************************************************/
3597c478bd9Sstevel@tonic-gate 
360*f928ce67Sceastha static void
arguments(void)361*f928ce67Sceastha arguments(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  *
3667c478bd9Sstevel@tonic-gate  * Makes sure all the non-option command line arguments are processed. If we get
3677c478bd9Sstevel@tonic-gate  * here and there aren't any arguments left, or if '-' is one of the input files
3687c478bd9Sstevel@tonic-gate  * we'll translate stdin. Assumes input files are part of a single PostScript
3697c478bd9Sstevel@tonic-gate  * job and fonts can be downloaded at the start of each file.
3707c478bd9Sstevel@tonic-gate  *
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate     if ( argc < 1 )
3747c478bd9Sstevel@tonic-gate 	download();
3757c478bd9Sstevel@tonic-gate     else {
3767c478bd9Sstevel@tonic-gate 	while ( argc > 0 ) {
3777c478bd9Sstevel@tonic-gate 	    fp_temp = NULL;
3787c478bd9Sstevel@tonic-gate 	    if ( strcmp(*argv, "-") == 0 )
3797c478bd9Sstevel@tonic-gate 		fp_in = stdin;
3807c478bd9Sstevel@tonic-gate 	    else if ( (fp_in = fopen(*argv, "r")) == NULL )
3817c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open %s", *argv);
3827c478bd9Sstevel@tonic-gate 	    download();
3837c478bd9Sstevel@tonic-gate 	    if ( fp_in != stdin )
3847c478bd9Sstevel@tonic-gate 		fclose(fp_in);
3857c478bd9Sstevel@tonic-gate 	    if ( fp_temp != NULL )
3867c478bd9Sstevel@tonic-gate 		fclose(fp_temp);
3877c478bd9Sstevel@tonic-gate 	    argc--;
3887c478bd9Sstevel@tonic-gate 	    argv++;
3897c478bd9Sstevel@tonic-gate 	}   /* End while */
3907c478bd9Sstevel@tonic-gate     }	/* End else */
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate }   /* End of arguments */
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*****************************************************************************/
3957c478bd9Sstevel@tonic-gate 
396*f928ce67Sceastha static void
done(void)397*f928ce67Sceastha done(void)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  *
4027c478bd9Sstevel@tonic-gate  * Clean things up before we quit.
4037c478bd9Sstevel@tonic-gate  *
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate     if ( temp_file != NULL )
4077c478bd9Sstevel@tonic-gate 	unlink(temp_file);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate }   /* End of done */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*****************************************************************************/
4127c478bd9Sstevel@tonic-gate 
413*f928ce67Sceastha static void
download(void)414*f928ce67Sceastha download(void)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate     int		infontlist = FALSE;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  *
4207c478bd9Sstevel@tonic-gate  * If next is zero the map table is empty and all we do is copy the input file
4217c478bd9Sstevel@tonic-gate  * to stdout. Otherwise we read the input file looking for %%DocumentFonts: or
4227c478bd9Sstevel@tonic-gate  * continuation comments, add any accessible fonts to the output file, and then
4237c478bd9Sstevel@tonic-gate  * append the input file. When reading stdin we append lines to fp_temp and
4247c478bd9Sstevel@tonic-gate  * recover them when we're ready to copy the input file. fp_temp will often
4257c478bd9Sstevel@tonic-gate  * only contain part of stdin - if there's no %%DocumentFonts: (atend) comment
4267c478bd9Sstevel@tonic-gate  * we stop reading fp_in after the header.
4277c478bd9Sstevel@tonic-gate  *
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate     if ( next > 0 ) {
4317c478bd9Sstevel@tonic-gate 	if ( fp_in == stdin ) {
4327c478bd9Sstevel@tonic-gate 	    if ( (temp_file = tempnam(temp_dir, "post")) == NULL )
4337c478bd9Sstevel@tonic-gate 		error(FATAL, "can't generate temp file name");
4347c478bd9Sstevel@tonic-gate 	    if ( (fp_temp = fopen(temp_file, "w+")) == NULL )
4357c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open %s", temp_file);
4367c478bd9Sstevel@tonic-gate 	    unlink(temp_file);
4377c478bd9Sstevel@tonic-gate 	    temp_file = NULL;
4387c478bd9Sstevel@tonic-gate 	}   /* End if */
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	while ( fgets(buf, sizeof(buf), fp_in) != NULL ) {
4417c478bd9Sstevel@tonic-gate 	    if ( fp_temp != NULL )
4427c478bd9Sstevel@tonic-gate 		fprintf(fp_temp, "%s", buf);
4437c478bd9Sstevel@tonic-gate 	    if ( buf[0] != '%' || buf[1] != '%' ) {
4447c478bd9Sstevel@tonic-gate 		if ( (buf[0] != '%' || buf[1] != '!') && atend == FALSE )
4457c478bd9Sstevel@tonic-gate 		    break;
4467c478bd9Sstevel@tonic-gate 		infontlist = FALSE;
4477c478bd9Sstevel@tonic-gate 	    } else if ( strncmp(buf, comment, strlen(comment)) == 0 ) {
4487c478bd9Sstevel@tonic-gate 		copyfonts(buf);
4497c478bd9Sstevel@tonic-gate 		infontlist = TRUE;
4507c478bd9Sstevel@tonic-gate 	    } else if ( buf[2] == '+' && infontlist == TRUE )
4517c478bd9Sstevel@tonic-gate 		copyfonts(buf);
4527c478bd9Sstevel@tonic-gate 	    else infontlist = FALSE;
4537c478bd9Sstevel@tonic-gate 	}   /* End while */
4547c478bd9Sstevel@tonic-gate     }	/* End if */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate     copyinput();
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate }   /* End of download */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*****************************************************************************/
4617c478bd9Sstevel@tonic-gate 
462*f928ce67Sceastha static void
copyfonts(char * list)463*f928ce67Sceastha copyfonts(char *list)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate     char	*font;
4667c478bd9Sstevel@tonic-gate     char	*path;
4677c478bd9Sstevel@tonic-gate     int		n;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate  *
4717c478bd9Sstevel@tonic-gate  * list points to a %%DocumentFonts: or continuation comment. What follows the
4727c478bd9Sstevel@tonic-gate  * the keyword will be a list of fonts separated by white space (or (atend)).
4737c478bd9Sstevel@tonic-gate  * Look for each font in the map table and if it's found copy the font file to
4747c478bd9Sstevel@tonic-gate  * stdout (once only).
4757c478bd9Sstevel@tonic-gate  *
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate     strtok(list, " \n");		/* skip to the font list */
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate     while ( (font = strtok(NULL, " \t\n")) != NULL ) {
4817c478bd9Sstevel@tonic-gate 	if ( strcmp(font, ATEND) == 0 ) {
4827c478bd9Sstevel@tonic-gate 	    atend = TRUE;
4837c478bd9Sstevel@tonic-gate 	    break;
4847c478bd9Sstevel@tonic-gate 	}   /* End if */
4857c478bd9Sstevel@tonic-gate 	if ( (n = lookup(font)) < next ) {
4867c478bd9Sstevel@tonic-gate 	    if ( *map[n].file != '/' ) {
4877c478bd9Sstevel@tonic-gate 		if ( (path = malloc(strlen(hostfontdir)+strlen(map[n].file)+2)) == NULL )
4887c478bd9Sstevel@tonic-gate 		    error(FATAL, "no memory");
4897c478bd9Sstevel@tonic-gate 		sprintf(path, "%s/%s", hostfontdir, map[n].file);
4907c478bd9Sstevel@tonic-gate 		cat(path);
4917c478bd9Sstevel@tonic-gate 		free(path);
4927c478bd9Sstevel@tonic-gate 	    } else cat(map[n].file);
4937c478bd9Sstevel@tonic-gate 	    map[n].downloaded = TRUE;
4947c478bd9Sstevel@tonic-gate 	}   /* End if */
4957c478bd9Sstevel@tonic-gate     }	/* End while */
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate }   /* End of copyfonts */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*****************************************************************************/
5007c478bd9Sstevel@tonic-gate 
501*f928ce67Sceastha static void
copyinput(void)502*f928ce67Sceastha copyinput(void)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * Copies the input file to stdout. If fp_temp isn't NULL seek to the start and
5087c478bd9Sstevel@tonic-gate  * add it to the output file - it's a partial (or complete) copy of stdin made
5097c478bd9Sstevel@tonic-gate  * by download(). Then copy fp_in, but only seek to the start if it's not stdin.
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate     if ( fp_temp != NULL ) {
5147c478bd9Sstevel@tonic-gate 	fseek(fp_temp, 0L, 0);
5157c478bd9Sstevel@tonic-gate 	while ( fgets(buf, sizeof(buf), fp_temp) != NULL )
5167c478bd9Sstevel@tonic-gate 	    printf("%s", buf);
5177c478bd9Sstevel@tonic-gate     }	/* End if */
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate     if ( fp_in != stdin )
5207c478bd9Sstevel@tonic-gate 	fseek(fp_in, 0L, 0);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate     while ( fgets(buf, sizeof(buf), fp_in) != NULL )
5237c478bd9Sstevel@tonic-gate 	printf("%s", buf);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate }   /* End of copyinput */
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*****************************************************************************/
5287c478bd9Sstevel@tonic-gate 
529*f928ce67Sceastha static int
lookup(char * font)530*f928ce67Sceastha lookup(char *font)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate     int		i;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  *
5367c478bd9Sstevel@tonic-gate  * Looks for *font in the map table. Return the map table index if found and
5377c478bd9Sstevel@tonic-gate  * not yet downloaded - otherwise return next.
5387c478bd9Sstevel@tonic-gate  *
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate     for ( i = 0; i < next; i++ )
5427c478bd9Sstevel@tonic-gate 	if ( strcmp(font, map[i].font) == 0 ) {
5437c478bd9Sstevel@tonic-gate 	    if ( map[i].downloaded == TRUE )
5447c478bd9Sstevel@tonic-gate 		i = next;
5457c478bd9Sstevel@tonic-gate 	    break;
5467c478bd9Sstevel@tonic-gate 	}   /* End if */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate     return(i);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate }   /* End of lookup */
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*****************************************************************************/
5537c478bd9Sstevel@tonic-gate 
554*f928ce67Sceastha static Map *
allocate(Map * ptr,int num)555*f928ce67Sceastha allocate(Map *ptr, int num)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate  *
5607c478bd9Sstevel@tonic-gate  * Allocates space for num Map elements. Calls malloc() if ptr is NULL and
5617c478bd9Sstevel@tonic-gate  * realloc() otherwise.
5627c478bd9Sstevel@tonic-gate  *
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate     if ( ptr == NULL )
5667c478bd9Sstevel@tonic-gate 	ptr = (Map *)malloc(num * sizeof(Map));
5677c478bd9Sstevel@tonic-gate     else ptr = (Map *)realloc(ptr, num * sizeof(Map));
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate     if ( ptr == NULL )
5707c478bd9Sstevel@tonic-gate 	error(FATAL, "no map memory");
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate     return(ptr);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate }   /* End of allocate */
575