home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2092 / utils.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  9.4 KB  |  422 lines

  1. /*    Copyright 1985, 1986, 1987, 1988 Chris Lewis
  2.         All Rights Reserved
  3.  
  4.     Permission to copy and further distribute is freely given provided
  5.     this copyright notice remains intact and that this software is not
  6.     sold for profit.
  7.  
  8.     Project:    Generic Troff drivers
  9.     Module:        utils.c
  10.     Author:     Chris Lewis
  11.     Specs:        Utility functions
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. #ifndef    lint
  17. static char SCCSid[] =
  18.     "@(#)utils.c: 2.3 Copyright 90/10/24 13:37:08 Chris Lewis";
  19. #endif
  20.  
  21. extern struct cattab tabN[], tabS[];
  22.  
  23. int lastFont, lastPoints;
  24. int lastYPos, lastXPos;
  25.  
  26. extern char *malloc();
  27.  
  28. char *skipblanks(p)
  29. register char *p; {
  30.     while (*p && isspace(*p)) p++;
  31.     return(p);
  32. }
  33.  
  34. char *gettok(p, bp)
  35. register char *p, *bp; {
  36.     p = skipblanks(p);
  37.     while(*p && !isspace(*p)) *bp++ = *p++;
  38.     *bp = '\0';
  39.     return(p);
  40. }
  41.  
  42. interp(buf, xlator, suf)
  43. char *buf, *suf; FUNC xlator; {
  44.     register char *p;
  45.     char token[512];
  46.     p = gettok(buf, token);
  47.     if (0 == strcmp(token, "include") || 0 == strcmp(token, "binclude")) {
  48.     int binary;
  49.     FILE *inc;
  50.     binary = (token[0] == 'b') ? 1: 0;
  51.     p = gettok(p, token);
  52.     DBP((D_SPEC, "Trying to include %s\n", token));
  53.     if (!(inc = fopen(token, "r"))) {
  54.         strcat(token, ".");
  55.         strcat(token, suf);
  56.         DBP((D_SPEC, "Trying to include %s\n", token));
  57.         if (!(inc = fopen(token, "r"))) {
  58.         char nbuf[512];
  59.         if (token[0] != '/') {
  60.             sprintf(nbuf, "%s/%s", LIBDIR, token);
  61.             DBP((D_SPEC, "Trying to include %s\n", nbuf));
  62.             inc = fopen(nbuf, "r");
  63.         }
  64.         }
  65.     }
  66.     if (!inc) {
  67.         fprintf(stderr, "%s: cannot open file %s\n", progname, token);
  68.         exit(1);
  69.     } else {
  70.         DBP((D_SPEC, "Successfull include\n"));
  71.         if (binary || !xlator)
  72.         while ((binary = fread(token, 1, sizeof(token), inc)) > 0)
  73.             fwrite(token, 1, binary, stdout);
  74.         else
  75.         (*xlator)(inc);
  76.         fclose(inc);
  77.     }
  78.     }
  79.  
  80.     else {
  81.     fprintf(stderr, "%s: unknown directive: %s\n", progname, token);
  82.     exit(1);
  83.     }
  84. }
  85.  
  86. char nodename[25];
  87. getnodename() {
  88.     FILE *uuname;
  89.     if ((uuname = popen(NODECMD, "r")) == NULL)
  90.     strcpy(nodename, "<noname>");
  91.     else {
  92.     fscanf(uuname, "%s", nodename);
  93.     if (pclose(uuname))
  94.         strcpy(nodename, "<noname>");
  95.     }
  96. }
  97.  
  98. struct fonttable fonttable[MAXFONTS+1];
  99. struct fonttable *xlatetable[8];
  100.  
  101. #define    READFONT    0
  102. #define    READNORM    1
  103. #define    READSYMB    2
  104.  
  105. loadfont(normfont, symfont)
  106. struct troff2befont *normfont, *symfont; {
  107.  
  108.     char buffer[BUFSIZ];
  109.     char buf[6][64];
  110.     int tableindex;
  111.     int state, i, count;
  112.     register char *first;
  113.     extern int atoi();
  114.     struct cattab *tab;
  115.     struct troff2befont *otab;
  116.     int done;
  117.  
  118.     FILE *fontfile;
  119.  
  120.     for (tableindex = 0; tableindex < 8; tableindex++) {
  121.     xlatetable[tableindex] = &fonttable[tableindex];
  122.     }
  123.  
  124.     fontfile = libopen(printer, "fonts");
  125.  
  126.     tableindex = 0;
  127.  
  128.     state = READFONT;
  129.  
  130.     while(fgets(buffer, sizeof(buffer), fontfile)) {
  131.  
  132.     first = buffer;
  133.  
  134.     for(;*first && isspace(*first); first++);
  135.  
  136.     if (!*first || *first == '#')
  137.         continue;
  138.  
  139.     switch(count = sscanf(first, "%s%s%s%s%s%s",
  140.         buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])) {
  141.         case 1: buf[1][0] = '\0';
  142.         case 2: buf[2][0] = '\0';
  143.         case 3: buf[3][0] = '\0';
  144.         case 4: buf[4][0] = '\0';
  145.         case 5: buf[5][0] = '\0';
  146.         case 6:
  147.         if        (strcmp(buf[0], "normal") == 0) {
  148.             state = READNORM;
  149.             break;
  150.         } else if (strcmp(buf[0], "symbol") == 0) {
  151.             state = READSYMB;
  152.             break;
  153.         }
  154.         switch(state) {
  155.             case READFONT:
  156.             if (tableindex == MAXFONTS) {
  157.                 fprintf(stderr, "%s: Too many fonts for %s!\n",
  158.                 progname, printer);
  159.                 exit(1);
  160.             }
  161.             for(i = 0; i < 4; i++) {
  162.                 first = mustmalloc(strlen(buf[i])+1,
  163.                 "fonttable");
  164.                 strcpy(first, buf[i]);
  165.                 fonttable[tableindex].tab[i] = first;
  166.             }
  167. #ifdef    OPT
  168.             fonttable[tableindex].widthtable = (char *) NULL;
  169. #endif
  170.             tableindex++;
  171.             break;
  172.             case READNORM: case READSYMB:
  173.             done = 0;
  174.             if (count <= 1) {
  175.                 fprintf(stderr, "%s: bad font.%s line: %s\n",
  176.                 progname, printer, buffer);
  177.                 exit(1);
  178.             }
  179.             tab = (state == READNORM) ? tabN : tabS;
  180.             otab = (state == READNORM) ? normfont : symfont;
  181.             for (i = 0; tab[i].ch_name; i++) {
  182.                 if (strcmp(tab[i].ch_name, buf[0]) == 0) {
  183.                 switch(buf[1][0]) {
  184.                     case 'N':
  185.                     otab[i].t2b_font = N; break;
  186.                     case 'S':
  187.                     otab[i].t2b_font = S; break;
  188.                     case 'D':
  189.                     otab[i].t2b_font = D; break;
  190.                     case '0':
  191.                     case '1':
  192.                     case '2':
  193.                     case '3':
  194.                     case '4':
  195.                     case '5':
  196.                     case '6':
  197.                     case '7':
  198.                     case '8':
  199.                     case '9':
  200.                     otab[i].t2b_font = atoi(buf[1]);
  201.                     break;
  202.                     default:
  203.                     fprintf(stderr, "%s: bad font in %s\n",
  204.                         progname, buffer);
  205.                     exit(1);
  206.                 }
  207.                 switch(count) {
  208.                     case 6:
  209.                     otab[i].t2b_scale = atoi(buf[5]);
  210.                     case 5:
  211.                     otab[i].t2b_yc = atoi(buf[4]);
  212.                     case 4:
  213.                     otab[i].t2b_xc = atoi(buf[3]);
  214.                     case 3:
  215.                     octdecode(buf[2]);
  216.                     first = mustmalloc(strlen(buf[2]) + 1,
  217.                         "fonttable");
  218.                     strcpy(first, buf[2]);
  219.                     otab[i].t2b_charseq = first;
  220.                 }
  221.                 done = 1;
  222.                 break;
  223.                 }
  224.             }
  225.             if (!done) {
  226.                 fprintf(stderr, "%s: couldn't apply fonts.%s: %s\n",
  227.                 progname, printer, buffer);
  228.                 exit(1);
  229.             }
  230.         }
  231.     }
  232.     }
  233.     fclose(fontfile);
  234. #ifdef    OPT
  235.     loadwidth(xlatetable[0]);
  236.     loadwidth(xlatetable[1]);
  237.     loadwidth(xlatetable[2]);
  238.     loadwidth(xlatetable[3]);
  239. #endif
  240. }
  241.  
  242. /*    Translate any octal escapes *inplace* */
  243. octdecode(str)
  244. register char *str; {
  245.     register char *from, *to;
  246.     int tmp, i;
  247.     from = to = str;
  248.     while(*from) {
  249.     if (*from != '\\')
  250.         *to++ = *from++;
  251.     else {
  252.         tmp = 0;
  253.         from++;
  254.         i = 3;
  255.         while(i-- > 0 && *from && *from >= '0' && *from <= '7')
  256.         tmp = (tmp << 3) + *from++ - '0';
  257.         *to++ = tmp&0xff;
  258.     }
  259.     }
  260.     *to++ = '\0';
  261. }
  262.  
  263.  
  264. dumpsequence(sequence)
  265. register char *sequence; {
  266.     printf("\t");
  267.     for(;*sequence;sequence++)
  268.     if (isascii(*sequence) && isprint(*sequence))
  269.         printf("%c", *sequence);
  270.     else
  271.         printf("\\%03o", (*sequence)&0xff);
  272. }
  273.  
  274. dumpline(name, tp)
  275. register char *name;
  276. register struct troff2befont *tp; {
  277.     if (name != NOC) {
  278.     printf("%s\t", name);
  279.     switch(tp->t2b_font) {
  280.         case N: printf("N"); break;
  281.         case S: printf("S"); break;
  282.         case D: printf("D"); break;
  283.         default: printf("%d", tp->t2b_font); break;
  284.     }
  285.     dumpsequence(tp->t2b_charseq);
  286.     printf("\t%d\t%d\t%d", tp->t2b_xc, tp->t2b_yc, tp->t2b_scale);
  287.     printf("\n");
  288.     }
  289. }
  290.  
  291. dumplist(namelist, tp)
  292. register struct cattab *namelist;
  293. register struct troff2befont *tp; {
  294.     register int i;
  295.     for (i = 0; namelist[i].ch_name && namelist[i].ch_catidx != NTC;
  296.     i++, tp++) {
  297.     if (strlen(namelist[i].ch_name) == 0)
  298.         continue;
  299.     if (strcmp(namelist[i].ch_name, "hy") == 0)
  300.         dumpline("-", tp);
  301.     dumpline(namelist[i].ch_name, tp);
  302.     }
  303. }
  304.  
  305. dumptables(bep)
  306. struct backend *bep; {
  307.     if (!bep) {
  308.     fprintf(stderr, "%s: no backend selected for table dump\n", progname);
  309.     exit(1);
  310.     }
  311.     printf("normal\n");
  312.     dumplist(tabN, bep->bestdfont);
  313.     printf("symbol\n");
  314.     dumplist(tabS, bep->besymfont);
  315. }
  316.  
  317. FontSel(from, to)
  318. char from, *to; {
  319. #ifdef    DEBUG
  320.     register int i;
  321. #endif
  322.     register struct fonttable *p;
  323.     DBP((D_SPEC, "FontSel: %c -> %s\n", from, to));
  324.     if (from < '1' || from > '8') {
  325.     fprintf(stderr, "Bad arguments to FontSel: %c %s\n", from, to);
  326.     return;
  327.     }
  328.  
  329.     for (p = fonttable; p->troffName; p++)
  330.     if (strcmp(p->troffName, to) == 0) {
  331.         xlatetable[from - '1'] = p;
  332.         break;
  333.     }
  334.     if (!p->troffName) {
  335.     fprintf(stderr, "Could not translate font %c (%s)\n", from, to);
  336.     }
  337. #ifdef    OPT
  338.     if (p->troffName && !p->widthtable)
  339.     loadwidth(p);
  340. #endif
  341.  
  342. #ifdef    DEBUG
  343.     for (i = 0; i < 8; i++)
  344.     DBP((D_SPEC, "Font %d->%s\n", i+1, xlatetable[i]->fontName));
  345. #endif
  346. }
  347.  
  348. FILE *libopen(pref, suf)
  349. register char *pref, *suf; {
  350.  
  351.     register char *buffer = mustmalloc(strlen(LIBDIR) + 50, "libopen");
  352.     register FILE *library;
  353.  
  354.     sprintf(buffer, "%s.%s", pref, suf);
  355.     if ((library = fopen(buffer, "r")) == NULL) {
  356.     sprintf(buffer, "lib/%s.%s", pref, suf);
  357.     if ((library = fopen(buffer, "r")) == NULL) {
  358.         sprintf(buffer, "%s/%s.%s", LIBDIR, pref, suf);
  359.         if ((library = fopen(buffer, "r")) == NULL) {
  360.         fprintf(stderr, "Cannot find %s.%s in . or lib or %s\n", pref, suf,
  361.             LIBDIR);
  362.         exit(1);
  363.         }
  364.     }
  365.     }
  366.     free(buffer);
  367.     return(library);
  368. }
  369.  
  370. resetState() {
  371.     lastFont = -1;
  372.     lastPoints = -1;
  373.     lastYPos = -1;
  374.     lastXPos = -1;
  375. }
  376.  
  377. doprologs() {
  378.     if (!prologs)
  379.     return;
  380.     while(*prologs)
  381.     dospecial(*prologs++);
  382. }
  383.  
  384. #ifdef    OPT
  385. loadwidth(p)
  386. struct fonttable *p; {
  387.     FILE *f;
  388.     int c;
  389.     if ((int) p->widthtable == 1)
  390.     return;
  391.     p->widthtable = mustmalloc(224, "widthtable");
  392.     strcpy(widthptr, "ft");
  393.     strcat(widthptr, p->troffName);
  394.     DBP((D_SPEC, "Attempting to read font file %s\n", widthtables));
  395.     if (f = fopen(widthtables, "r")) {
  396.     for (c = 0; c < HEADERSIZE; c++) getc(f);
  397.     if (224 != fread(p->widthtable, 1, 224, f)) {
  398.         fprintf(stderr, "%s: badly formed width table %s\n",
  399.         progname, widthtables);
  400.         free(p->widthtable);
  401.         p->widthtable = (char *) 1;
  402.         DBP((D_SPEC, "Load of %s failed\n", widthtables));
  403.     } else {
  404.         for (c = 0; c < 224; c++)
  405.         p->widthtable[c] &= 0x3f;
  406.         DBP((D_SPEC, "Load of %s succeeded\n", widthtables));
  407.     }
  408.     } else {
  409.     DBP((D_SPEC, "Failed to open widthtable %s\n", widthtables));
  410.     free(p->widthtable);
  411.     p->widthtable = (char *) 1;
  412.     }
  413. }
  414. #endif
  415.  
  416. #ifdef    NULLCHECK
  417. mab() {
  418.     fprintf(stderr, "KABOOM\n");
  419.     abort();
  420. }
  421. #endif
  422.