download.c 14 KB

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