download.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * download - host resident font downloader
  3. *
  4. * Prepends host resident fonts to PostScript input files. The program assumes
  5. * the input files are part of a single PostScript job and that requested fonts
  6. * can be downloaded at the start of each input file. Downloaded fonts are the
  7. * ones named in a %%DocumentFonts: comment and listed in a special map table.
  8. * Map table pathnames (supplied using the -m option) that begin with a / are
  9. * taken as is. Otherwise the final pathname is built using *hostfontdir (-H
  10. * option), *mapname (-m option), and *suffix.
  11. *
  12. * The map table consists of fontname-filename pairs, separated by white space.
  13. * Comments are introduced by % (as in PostScript) and extend to the end of the
  14. * current line. The only fonts that can be downloaded are the ones listed in
  15. * the active map table that point the program to a readable Unix file. A request
  16. * for an unlisted font or inaccessible file is ignored. All font requests are
  17. * ignored if the map table can't be read. In that case the program simply copies
  18. * the input files to stdout.
  19. *
  20. * An example (but not one to follow) of what can be in a map table is,
  21. *
  22. * %
  23. * % Map requests for Bookman-Light to file *hostfontdir/KR
  24. * %
  25. *
  26. * Bookman-Light KR % Keeping everything (including the map
  27. * % table) in *hostfontdir seems like the
  28. * % cleanest approach.
  29. *
  30. * %
  31. * % Map Palatino-Roman to file *hostfontdir/palatino/Roman
  32. * %
  33. * Palatino-Roman palatino/Roman
  34. *
  35. * % Map ZapfDingbats to file /usr/lib/host/dingbats
  36. *
  37. * ZapfDingbats /usr/lib/host/dingbats
  38. *
  39. * Once again, file names that begin with a / are taken as is. All others have
  40. * *hostfontdir/ prepended to the file string associated with a particular font.
  41. *
  42. * Map table can be associated with a printer model (e.g. a LaserWriter), a
  43. * printer destination, or whatever - the choice is up to an administrator.
  44. * By destination may be best if your spooler is running several private
  45. * printers. Host resident fonts are usually purchased under a license that
  46. * restricts their use to a limited number of printers. A font licensed for
  47. * a single printer should only be used on that printer.
  48. *
  49. * Was written quickly, so there's much room for improvement. Undoubtedly should
  50. * be a more general program (e.g. scan for other comments).
  51. */
  52. #define _BSD_EXTENSION
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <unistd.h>
  56. #include <fcntl.h>
  57. #include <string.h>
  58. #include <signal.h>
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61. #include "comments.h" /* PostScript file structuring comments */
  62. #include "gen.h" /* general purpose definitions */
  63. #include "path.h" /* for temporary directory */
  64. #include "ext.h" /* external variable declarations */
  65. #include "download.h" /* a few special definitions */
  66. char *temp_dir = TEMPDIR; /* temp directory - for copying stdin */
  67. char *hostfontdir = HOSTDIR; /* host resident directory */
  68. char *mapname = "map"; /* map table - usually in *hostfontdir */
  69. char *suffix = ""; /* appended to the map table pathname */
  70. Map *map = NULL; /* device font map table */
  71. char *stringspace = NULL; /* for storing font and file strings */
  72. int next = 0; /* next free slot in map[] */
  73. char *residentfonts = NULL; /* list of printer resident fonts */
  74. char *printer = NULL; /* printer name - only for Unix 4.0 lp */
  75. char buf[2048]; /* input file line buffer */
  76. char *comment = DOCUMENTFONTS; /* look for this comment */
  77. int atend = FALSE; /* TRUE only if a comment says so */
  78. FILE *fp_in = stdin; /* next input file */
  79. FILE *fp_temp = NULL; /* for copying stdin */
  80. void arguments(void);
  81. void copyfonts(char *);
  82. void copyinput(void);
  83. void done(void);
  84. void download(void);
  85. void init_signals(void);
  86. void options(void);
  87. void readmap(void);
  88. void readresident(void);
  89. main(agc, agv)
  90. int agc;
  91. char *agv[];
  92. {
  93. /*
  94. *
  95. * Host resident font downloader. The input files are assumed to be part of a
  96. * single PostScript job.
  97. *
  98. */
  99. argc = agc; /* other routines may want them */
  100. argv = agv;
  101. prog_name = argv[0]; /* just for error messages */
  102. init_signals(); /* sets up interrupt handling */
  103. options(); /* first get command line options */
  104. readmap(); /* read the font map table */
  105. readresident(); /* and the optional resident font list */
  106. arguments(); /* then process non-option arguments */
  107. done(); /* and clean things up */
  108. exit(x_stat); /* not much could be wrong */
  109. return 0;
  110. }
  111. void
  112. init_signals(void)
  113. {
  114. /*
  115. * Makes sure we handle interrupts properly.
  116. */
  117. if ( signal(SIGINT, interrupt) == SIG_IGN ) {
  118. signal(SIGINT, SIG_IGN);
  119. signal(SIGQUIT, SIG_IGN);
  120. signal(SIGHUP, SIG_IGN);
  121. } else {
  122. signal(SIGHUP, interrupt);
  123. signal(SIGQUIT, interrupt);
  124. }
  125. signal(SIGTERM, interrupt);
  126. }
  127. void
  128. options(void)
  129. {
  130. int ch; /* return value from getopt() */
  131. char *optnames = "c:fm:p:r:H:T:DI";
  132. extern char *optarg; /* used by getopt() */
  133. extern int optind;
  134. /*
  135. *
  136. * Reads and processes the command line options.
  137. *
  138. */
  139. while ( (ch = getopt(argc, argv, optnames)) != EOF ) {
  140. switch ( ch ) {
  141. case 'c': /* look for this comment */
  142. comment = optarg;
  143. break;
  144. case 'f': /* force a complete input file scan */
  145. atend = TRUE;
  146. break;
  147. case 'm': /* printer map table name */
  148. mapname = optarg;
  149. break;
  150. case 'p': /* printer name - for Unix 4.0 lp */
  151. printer = optarg;
  152. break;
  153. case 'r': /* resident font list */
  154. residentfonts = optarg;
  155. break;
  156. case 'H': /* host resident font directory */
  157. hostfontdir = optarg;
  158. break;
  159. case 'T': /* temporary file directory */
  160. temp_dir = optarg;
  161. break;
  162. case 'D': /* debug flag */
  163. debug = ON;
  164. break;
  165. case 'I': /* ignore FATAL errors */
  166. ignore = ON;
  167. break;
  168. case '?': /* don't understand the option */
  169. error(FATAL, "");
  170. break;
  171. default: /* don't know what to do for ch */
  172. error(FATAL, "missing case for option %c\n", ch);
  173. break;
  174. } /* End switch */
  175. } /* End while */
  176. argc -= optind; /* get ready for non-option args */
  177. argv += optind;
  178. }
  179. void
  180. readmap(void)
  181. {
  182. char *path;
  183. char *ptr;
  184. int fd;
  185. struct stat sbuf;
  186. /*
  187. * Initializes the map table by reading an ASCII mapping file. If mapname begins
  188. * with a / it's the map table. Otherwise hostfontdir, mapname, and suffix are
  189. * combined to build the final pathname. If we can open the file we read it all
  190. * into memory, erase comments, and separate the font and file name pairs. When
  191. * we leave next points to the next free slot in the map[] array. If it's zero
  192. * nothing was in the file or we couldn't open it.
  193. */
  194. if ( hostfontdir == NULL || mapname == NULL )
  195. return;
  196. if ( *mapname != '/' ) {
  197. if ( (path = malloc(strlen(hostfontdir) + strlen(mapname) +
  198. strlen(suffix) + 2)) == NULL )
  199. error(FATAL, "no memory");
  200. sprintf(path, "%s/%s%s", hostfontdir, mapname, suffix);
  201. } else path = mapname;
  202. if ( (fd = open(path, 0)) != -1 ) {
  203. if ( fstat(fd, &sbuf) == -1 )
  204. error(FATAL, "can't fstat %s", path);
  205. if ( (stringspace = malloc(sbuf.st_size + 2)) == NULL )
  206. error(FATAL, "no memory for %s (%d bytes)", path, sbuf.st_size + 2);
  207. if ( read(fd, stringspace, sbuf.st_size) == -1 )
  208. error(FATAL, "can't read %s", path);
  209. close(fd);
  210. stringspace[sbuf.st_size] = '\n'; /* just to be safe */
  211. stringspace[sbuf.st_size+1] = '\0';
  212. for ( ptr = stringspace; *ptr != '\0'; ptr++ ) /* erase comments */
  213. if ( *ptr == '%' )
  214. for ( ; *ptr != '\n' ; ptr++ )
  215. *ptr = ' ';
  216. for ( ptr = stringspace; ; next++ ) {
  217. if ( (next % 50) == 0 )
  218. map = allocate(map, next+50);
  219. map[next].downloaded = FALSE;
  220. map[next].font = strtok(ptr, " \t\n");
  221. map[next].file = strtok(ptr = NULL, " \t\n");
  222. if ( map[next].font == NULL )
  223. break;
  224. if ( map[next].file == NULL )
  225. error(FATAL, "map table format error - check %s", path);
  226. }
  227. }
  228. }
  229. void
  230. readresident(void)
  231. {
  232. FILE *fp;
  233. char *path;
  234. int ch;
  235. int n;
  236. /*
  237. * Reads a file that lists the resident fonts for a particular printer and marks
  238. * each font as already downloaded. Nothing's done if the file can't be read or
  239. * there's no mapping file. Comments, as in the map file, begin with a % and
  240. * extend to the end of the line. Added for Unix 4.0 lp.
  241. */
  242. if ( next == 0 || (printer == NULL && residentfonts == NULL) )
  243. return;
  244. if ( printer != NULL ) { /* use Unix 4.0 lp pathnames */
  245. sprintf(buf, "%s/printers/%s", HOSTDIR, printer);
  246. path = buf;
  247. } else path = residentfonts;
  248. if ( (fp = fopen(path, "r")) != NULL ) {
  249. while ( fscanf(fp, "%s", buf) != EOF )
  250. if ( buf[0] == '%' )
  251. while ( (ch = getc(fp)) != EOF && ch != '\n' ) ;
  252. else if ( (n = lookup(buf)) < next )
  253. map[n].downloaded = TRUE;
  254. fclose(fp);
  255. }
  256. }
  257. void
  258. arguments(void)
  259. {
  260. /*
  261. *
  262. * Makes sure all the non-option command line arguments are processed. If we get
  263. * here and there aren't any arguments left, or if '-' is one of the input files
  264. * we'll translate stdin. Assumes input files are part of a single PostScript
  265. * job and fonts can be downloaded at the start of each file.
  266. *
  267. */
  268. if ( argc < 1 )
  269. download();
  270. else {
  271. while ( argc > 0 ) {
  272. fp_temp = NULL;
  273. if ( strcmp(*argv, "-") == 0 )
  274. fp_in = stdin;
  275. else if ( (fp_in = fopen(*argv, "r")) == NULL )
  276. error(FATAL, "can't open %s", *argv);
  277. download();
  278. if ( fp_in != stdin )
  279. fclose(fp_in);
  280. if ( fp_temp != NULL )
  281. fclose(fp_temp);
  282. argc--;
  283. argv++;
  284. }
  285. }
  286. }
  287. void
  288. done(void)
  289. {
  290. /*
  291. * Clean things up before we quit.
  292. */
  293. if ( temp_file != NULL )
  294. unlink(temp_file);
  295. }
  296. void
  297. download(void)
  298. {
  299. int infontlist = FALSE;
  300. /*
  301. *
  302. * If next is zero the map table is empty and all we do is copy the input file
  303. * to stdout. Otherwise we read the input file looking for %%DocumentFonts: or
  304. * continuation comments, add any accessible fonts to the output file, and then
  305. * append the input file. When reading stdin we append lines to fp_temp and
  306. * recover them when we're ready to copy the input file. fp_temp will often
  307. * only contain part of stdin - if there's no %%DocumentFonts: (atend) comment
  308. * we stop reading fp_in after the header.
  309. *
  310. */
  311. if ( next > 0 ) {
  312. if ( fp_in == stdin ) {
  313. if ( (temp_file = tempnam(temp_dir, "post")) == NULL )
  314. error(FATAL, "can't generate temp file name");
  315. if ( (fp_temp = fopen(temp_file, "w+r")) == NULL )
  316. error(FATAL, "can't open %s", temp_file);
  317. unlink(temp_file);
  318. } /* End if */
  319. while ( fgets(buf, sizeof(buf), fp_in) != NULL ) {
  320. if ( fp_temp != NULL )
  321. fprintf(fp_temp, "%s", buf);
  322. if ( buf[0] != '%' || buf[1] != '%' ) {
  323. if ( (buf[0] != '%' || buf[1] != '!') && atend == FALSE )
  324. break;
  325. infontlist = FALSE;
  326. } else if ( strncmp(buf, comment, strlen(comment)) == 0 ) {
  327. copyfonts(buf);
  328. infontlist = TRUE;
  329. } else if ( buf[2] == '+' && infontlist == TRUE )
  330. copyfonts(buf);
  331. else infontlist = FALSE;
  332. }
  333. }
  334. copyinput();
  335. }
  336. void
  337. copyfonts(list)
  338. char *list;
  339. {
  340. char *font;
  341. char *path;
  342. int n;
  343. /*
  344. * list points to a %%DocumentFonts: or continuation comment. What follows the
  345. * the keyword will be a list of fonts separated by white space (or (atend)).
  346. * Look for each font in the map table and if it's found copy the font file to
  347. * stdout (once only).
  348. */
  349. strtok(list, " \n"); /* skip to the font list */
  350. while ( (font = strtok(NULL, " \t\n")) != NULL ) {
  351. if ( strcmp(font, ATEND) == 0 ) {
  352. atend = TRUE;
  353. break;
  354. }
  355. if ( (n = lookup(font)) < next ) {
  356. if ( *map[n].file != '/' ) {
  357. if ( (path = malloc(strlen(hostfontdir)+strlen(map[n].file)+2)) == NULL )
  358. error(FATAL, "no memory");
  359. sprintf(path, "%s/%s", hostfontdir, map[n].file);
  360. cat(path);
  361. free(path);
  362. } else cat(map[n].file);
  363. map[n].downloaded = TRUE;
  364. }
  365. }
  366. }
  367. void
  368. copyinput(void)
  369. {
  370. /*
  371. *
  372. * Copies the input file to stdout. If fp_temp isn't NULL seek to the start and
  373. * add it to the output file - it's a partial (or complete) copy of stdin made
  374. * by download(). Then copy fp_in, but only seek to the start if it's not stdin.
  375. *
  376. */
  377. if ( fp_temp != NULL ) {
  378. fseek(fp_temp, 0L, 0);
  379. while ( fgets(buf, sizeof(buf), fp_temp) != NULL )
  380. printf("%s", buf);
  381. } /* End if */
  382. if ( fp_in != stdin )
  383. fseek(fp_in, 0L, 0);
  384. while ( fgets(buf, sizeof(buf), fp_in) != NULL )
  385. printf("%s", buf);
  386. }
  387. lookup(font)
  388. char *font;
  389. {
  390. int i;
  391. /*
  392. *
  393. * Looks for *font in the map table. Return the map table index if found and
  394. * not yet downloaded - otherwise return next.
  395. *
  396. */
  397. for ( i = 0; i < next; i++ )
  398. if ( strcmp(font, map[i].font) == 0 ) {
  399. if ( map[i].downloaded == TRUE )
  400. i = next;
  401. break;
  402. } /* End if */
  403. return(i);
  404. } /* End of lookup */
  405. /*
  406. * Allocates space for num Map elements. Calls malloc() if ptr is NULL and
  407. * realloc() otherwise.
  408. */
  409. Map *
  410. allocate(Map *ptr, int num)
  411. {
  412. if (ptr == NULL)
  413. ptr = (Map *)malloc(num * sizeof(Map));
  414. else
  415. ptr = (Map *)realloc(ptr, num * sizeof(Map));
  416. if (ptr == NULL)
  417. error(FATAL, "no map memory");
  418. return ptr;
  419. }