home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d6xx / d671 / tr2tex.lha / tr2tex / tr2tex.zoo / subs.c < prev    next >
C/C++ Source or Header  |  1987-11-23  |  18KB  |  903 lines

  1. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  2. /* 
  3. These subroutines do (in general) small things for the translator.
  4. They appear in alphabetical order and their names are unique in the
  5. first six characters.
  6. */
  7.  
  8. #include        "setups.h"
  9. #include        "simil.h"
  10. #include        "greek.h"
  11. #include        "flip.h"
  12. #include        "forbid.h"
  13. #include        "maths.h"
  14. #include        "macros.h"
  15.  
  16. extern def_count;
  17. extern mydef_count;
  18.  
  19. /* compile-time counting of elements */
  20. int GRK_count = (sizeof(GRK_list)/sizeof(GRK_list[0]));
  21. int sim_count = (sizeof(sim_list)/sizeof(sim_list[0]));
  22. int flip_count = (sizeof(flip_list)/sizeof(flip_list[0]));
  23. int forbd_count = (sizeof(forbid)/sizeof(forbid[0]));
  24. int mathcom_count = (sizeof(math)/sizeof(struct math_equiv));
  25. int macro_count = (sizeof(macro)/sizeof(struct macro_table));
  26.  
  27. char *
  28. alternate(inbuf,outbuf,w)        /* alternate fonts (manual macro) */
  29. char *inbuf, *outbuf, *w;
  30. {
  31. int f1,f2;
  32. int which=1;
  33. char font[MAXWORD], font1[MAXWORD], font2[MAXWORD],
  34.      ww[MAXWORD], tmp[MAXWORD];
  35.  
  36. tmp[0] = NULL;
  37. f1 = w[1];    f2 = w[2];
  38. if (f1 == 'R')    strcpy(font1,"\\rm");
  39. if (f1 == 'I')    strcpy(font1,"\\it");
  40. if (f1 == 'B')    strcpy(font1,"\\bf");
  41. if (f2 == 'R')    strcpy(font2,"\\rm");
  42. if (f2 == 'I')    strcpy(font2,"\\it");
  43. if (f2 == 'B')    strcpy(font2,"\\bf");
  44.  
  45. strcpy(font,font1);
  46. while (*inbuf != '\n' && *inbuf != NULL)
  47.     {
  48.     inbuf += get_arg(inbuf,ww,1);
  49.     sprintf(tmp,"{%s %s}",font,ww);
  50.     outbuf = strapp(outbuf,tmp);
  51.     if (which == 1)
  52.         {
  53.         which = 2;
  54.         strcpy(font,font2);
  55.         }
  56.     else
  57.         {
  58.         which = 1;
  59.         strcpy(font,font1);
  60.         }
  61.     while (*inbuf == ' ' || *inbuf == '\t')
  62.         inbuf++;
  63.     }
  64.  
  65. return(outbuf);
  66. }
  67.  
  68. int
  69. CAP_GREEK(w)            /* check if w is in the GREEK list */
  70. char *w;
  71. {
  72. int i;
  73.  
  74. for (i=0; i < GRK_count ; i++)
  75.     {
  76.     if (strcmp(GRK_list[i],w) == 0)
  77.         return(1);
  78.     }
  79. return(-1);
  80. }
  81.  
  82. char *
  83. do_table(inbuf,outbuf,offset)
  84. char *inbuf, *outbuf;
  85. int *offset;                /* amount to offset inbuf */
  86. {
  87. char w[MAXWORD], ww[MAXWORD], format[MAXWORD], tmp[MAXWORD];
  88. char *ptr;
  89. int i,j,len,columns=0;
  90. int tab = '\t';                /* default tab */
  91.  
  92. tmp[0] = NULL;
  93. ptr = inbuf;                /* remember where we started */
  94. len = get_line(inbuf,w,0);
  95. if (w[strlen(w)-1] == ';')        /* options */
  96.     {
  97.     inbuf += len;
  98.     if (strncmp(w,"tab",3) == 0)    /* get the tab charecter */
  99.         tab = w[4];        /* expect something like tab(&); */
  100.     inbuf = skip_line(inbuf);
  101.     }
  102. while (*inbuf != NULL)            /* get the LAST format line */
  103.     {
  104.     len = get_line(inbuf,w,0);
  105.     if (w[strlen(w)-1] != '.')    break;    /* not a fromat line */
  106.     inbuf += len;
  107.     for (i=0, j=0; i<len-1; i++)
  108.         {
  109.         if (isspace(w[i]))    continue;
  110.         columns++;
  111.         if (w[i] == 'l')    format[j] = 'l';
  112.         else if (w[i] == 'r')    format[j] = 'r';
  113.         else            format[j] = 'c';
  114.         j++;
  115.         }
  116.     }
  117. if (columns == 0)
  118.     {
  119.     fprintf(stderr,"Sorry, I cannot do tables without a format line\n\
  120. Doing plain translation of table, lines will be commented\n\
  121. You need to fix it yourself\n");
  122.     while (*inbuf != NULL)
  123.         {
  124.         (void) getword(inbuf,w);
  125.         if (strcmp(w,".TE") ==  0)    {inbuf += 4;    break;}
  126.         inbuf += get_line(inbuf,w,1);
  127.         *outbuf++ = '%';
  128.         outbuf = strapp(outbuf,w);
  129.         outbuf = strapp(outbuf,"\n");
  130.         inbuf++;        /* skip the \n */
  131.         }
  132.     *offset = inbuf - ptr;
  133.     return(outbuf);
  134.     }
  135. format[j] = NULL;
  136. sprintf(tmp,"\\par\n\\begin{tabular}{%s}\n",format);
  137. outbuf = strapp(outbuf,tmp);
  138.  
  139. while (*inbuf != NULL)
  140.     {
  141.     for (i=0; i<columns-1; i++)
  142.         {
  143.         (void) getword(inbuf,w);
  144.         if (i == 0 && (strcmp(w,"\n") == 0 || strcmp(w,"_") == 0))
  145.             {inbuf++;    i--;    continue;}
  146.         if (strcmp(w,".TE") == 0)
  147.             {
  148.             inbuf += 4;
  149.             if (i == 0)
  150.                 {
  151.                 outbuf -= 3;    /* take back the \\ and the \n */
  152.                 *outbuf = NULL;
  153.                 }
  154.             outbuf = strapp(outbuf,"\n\\end{tabular}\n\\par\n");
  155.             *offset = inbuf - ptr;
  156.             return(outbuf);
  157.             }
  158.         inbuf += get_table_entry(inbuf,w,tab);
  159.         inbuf ++;        /* skip tab */
  160.         troff_tex(w,ww,0);
  161.         sprintf(tmp,"%s & ",ww);
  162.         outbuf = strapp(outbuf,tmp);
  163.         }
  164.     (void) getword(inbuf,w);
  165.     if (strcmp(w,".TE") == 0)
  166.         {
  167.         fprintf(stderr,"Oops! I goofed. I told I you I am not very good at tables\nI've encountered an unexpected end for the table\n\
  168. You need to fix it yourself\n");
  169.         inbuf += 4;
  170.         outbuf = strapp(outbuf,"\\end{tabular}\n\\par\n");
  171.         *offset = inbuf - ptr;
  172.         return(outbuf);
  173.         }
  174.     inbuf += get_table_entry(inbuf,w,'\n');
  175.     inbuf++;        /* skip tab */
  176.     troff_tex(w,ww,0);
  177.     outbuf = strapp(outbuf,ww);
  178.     outbuf = strapp(outbuf,"\\\\\n");
  179.     }
  180. fprintf(stderr,"Oops! I goofed. I told I you I am not very good at tables\n\
  181. File ended and I haven't finished the table!\n\
  182. You need to fix it yourself\n");
  183. *offset = inbuf - ptr;
  184. outbuf = strapp(outbuf,"\\end{tabular}\n\\par\n");
  185. return(outbuf);
  186. }
  187.  
  188. char *
  189. flip(outbuf,w)            /* do the flipping */
  190. char *outbuf, *w;
  191. {
  192. int lb=0, rb=0;
  193. char ww[MAXWORD], tmp[MAXWORD];
  194.  
  195. ww[0] = NULL;    tmp[0] = NULL;
  196. outbuf--;
  197. while (*outbuf == ' ' || *outbuf == '\t' || *outbuf == '\n')
  198.     outbuf--;
  199. while (1)
  200.     {
  201.     if (*outbuf == '{')
  202.         {
  203.         lb++;
  204.         if (lb > rb)    break;
  205.         }
  206.     if (*outbuf == '}')    rb++;
  207.     if (rb == 0)
  208.         {
  209.         if (*outbuf != ' ' && *outbuf != '\t' && *outbuf != '\n'
  210.             && *outbuf != '$')
  211.             {
  212.             outbuf--;
  213.             continue;
  214.             }
  215.         else    break;
  216.         }
  217.     outbuf--;
  218.     if (lb == rb && lb != 0)    break;
  219.     }
  220. outbuf++;
  221. if (*outbuf == '\\')
  222.     {
  223.     outbuf++;
  224.     (void) getword(outbuf,tmp);
  225.     sprintf(ww,"\\%s",tmp);
  226.     outbuf--;
  227.     }
  228. else if (*outbuf == '{')
  229.     (void) get_brace_arg(outbuf,ww);
  230. else
  231.     (void) getword(outbuf,ww);
  232. *outbuf = NULL;
  233. sprintf(tmp,"\\%s %s",w,ww);
  234. outbuf = strapp(outbuf,tmp);
  235. return(outbuf);
  236. }
  237.  
  238. char *
  239. flip_twice(outbuf,w,ww)        /* take care of things like x hat under */
  240. char *outbuf, *w, *ww;
  241. {
  242. int lb=0, rb=0;
  243. char tmp1[MAXWORD], tmp2[MAXWORD];
  244.  
  245. tmp1[0] = NULL;        tmp2[0] = NULL;
  246. outbuf--;
  247. while (*outbuf == ' ' || *outbuf == '\t' || *outbuf == '\n')
  248.     outbuf--;
  249. while (1)
  250.     {
  251.     if (*outbuf == '{')
  252.         {
  253.         lb++;
  254.         if (lb > rb)    break;
  255.         }
  256.     if (*outbuf == '}')    rb++;
  257.     if (rb == 0)
  258.         {
  259.         if (*outbuf != ' ' && *outbuf != '\t' && *outbuf != '\n'
  260.             && *outbuf != '$')
  261.             {
  262.             outbuf--;
  263.             continue;
  264.             }
  265.         else    break;
  266.         }
  267.     outbuf--;
  268.     if (lb == rb && lb != 0)    break;
  269.     }
  270. outbuf++;
  271. if (*outbuf == '\\')
  272.     {
  273.     outbuf++;
  274.     (void) getword(outbuf,tmp2);
  275.     sprintf(tmp1,"\\%s",tmp2);
  276.     outbuf--;
  277.     }
  278. else if (*outbuf == '{')
  279.     (void) get_brace_arg(outbuf,tmp1);
  280. else
  281.     (void) getword(outbuf,tmp1);
  282. *outbuf = NULL;
  283. sprintf(tmp2,"\\%s{\\%s %s}",w,ww,tmp1);
  284. outbuf = strapp(outbuf,tmp2);
  285. return(outbuf);
  286. }
  287.  
  288. int
  289. get_arg(inbuf,w,rec)        /* get argumnet */
  290. char *inbuf, *w;
  291. int rec;        /* rec=1 means recursive */
  292. {
  293. int c,len,i;
  294. char ww[MAXWORD];
  295. int delim;
  296.  
  297. len=0;
  298. while ((c = *inbuf) == ' ' || c == '\t')    /* skip spaces and tabs */
  299.         {inbuf++;    len++;}
  300. i=0;
  301. if (*inbuf == '{' || *inbuf == '\"')
  302.     {
  303.     if (*inbuf == '{')    delim = '}';
  304.     else            delim = '\"';
  305.     inbuf++;    len++;
  306.     while ((c = *inbuf++) != NULL && c != delim && i < MAXWORD)
  307.         {
  308.         if (c == ' ' && delim == '\"')    ww[i++] = '\\';
  309.         ww[i++] = (char)c;    len++;
  310.         }
  311.     len++;
  312.     }
  313. else
  314.     {
  315.     while ((c = *inbuf++) != NULL && c != ' ' && c != '\t' && c != '\n'
  316.         && c != '$' && c != '}' && i < MAXWORD)
  317.         {
  318.         if (math_mode && c == '~')    break;
  319.         ww[i++] = (char)c;    len++;
  320.         }
  321.     }
  322. ww[i] = NULL;
  323. if (rec == 1)                /* check if recursion is rquired */
  324.     troff_tex(ww,w,1);
  325. else
  326.     strcpy(w,ww);
  327. return(len);
  328. }
  329.  
  330. void
  331. get_brace_arg(buf,w)        /* get argumnet surrounded by braces */
  332. char *buf, *w;
  333. {
  334. int c,i, lb=0, rb=0;
  335.  
  336. i=0;
  337. while ((c = *buf++) != NULL)
  338.     {
  339.     w[i++] = (char)c;
  340.     if (c == '{')    lb++;
  341.     if (c == '}')    rb++;
  342.     if (lb == rb)    break;
  343.     }
  344. w[i] = NULL;
  345. }
  346.  
  347. int
  348. get_defword(inbuf,w,illegal)        /* get "define" or .de word */
  349. char *inbuf, *w;            /* delimited by space only */
  350. int *illegal;
  351. {
  352. int c,i;
  353.  
  354. *illegal = 0;
  355. for (i=0; (c = *inbuf++) != NULL && c != ' ' && c != '\n'
  356.         && c != '\t' && i < MAXWORD; i++)
  357.     {
  358.     w[i] = (char)c;
  359.     if (isalpha(c) == 0)    *illegal = 1;    /* illegal TeX macro */ 
  360.     }
  361. w[i] = NULL;
  362. if (*illegal == 0)
  363.     if (is_forbid(w) >= 0)        *illegal=1;
  364. return(i);
  365. }
  366.  
  367. int
  368. get_line(inbuf,w,rec)        /* get the rest of the line */
  369. char *inbuf, *w;
  370. int rec;            /* rec=1 means recursion is required */
  371. {
  372. int c,i,len;
  373. char ww[MAXLINE];
  374.  
  375. i=0;    len=0;
  376. while ((c = *inbuf++) != NULL && c != '\n' && len < MAXLINE)
  377.         {ww[i++] = (char)c;    len++;}
  378. ww[i] = NULL;
  379. if (rec == 1)
  380.     troff_tex(ww,w,0);
  381. else
  382.     strcpy(w,ww);
  383. return(len);
  384. }
  385.  
  386. int
  387. get_multi_line(inbuf,w)        /* get multi-line argument */
  388. char *inbuf, *w;
  389. {
  390. int len=0,l=0,lines=0;
  391. char tmp[MAXWORD];
  392. int c1,c2;
  393.  
  394. w[0] = NULL;    tmp[0] = NULL;
  395. while (*inbuf != NULL)
  396.     {
  397.     c1 = *inbuf;    c2 = *++inbuf;        --inbuf;
  398.     if (c1 == '.' && isupper(c2))        break; 
  399.     lines++;
  400.     if (lines > 1)
  401.         strcat(w," \\\\\n");
  402.     l = get_line(inbuf,tmp,1);
  403.     strcat(w,tmp);
  404.     len += l+1;    inbuf += l+1;
  405.     }
  406. len--;        inbuf--;
  407. return(len);
  408. }
  409.  
  410. int
  411. get_mydef(inbuf,w)        /* get the macro substitution */
  412. char *inbuf, *w;
  413. {
  414. int c1,c2,l,len;
  415. char tmp[MAXWORD];
  416.  
  417. tmp[0] = NULL;
  418. len=1;
  419. while (*inbuf != NULL)
  420.     {
  421.     c1 = *inbuf;    c2 = *++inbuf;        --inbuf;
  422.     if (c1 == '.' && c2 == '.')        break; 
  423.     l = get_line(inbuf,tmp,1);
  424.     strcat(w,tmp);
  425.     len += l+1;    inbuf += l+1;
  426.     }
  427. return(len);
  428. }
  429.  
  430. int
  431. get_N_lines(inbuf,w,N)        /* get N lines */
  432. char *inbuf, *w;
  433. int N;
  434. {
  435. int len=0,l=0,lines=0;
  436. char tmp[MAXWORD];
  437.  
  438. w[0] = NULL;    tmp[0] = NULL;
  439. while (*inbuf != NULL && lines < N)
  440.     {
  441.     lines++;
  442.     if (lines > 1)
  443.         strcat(w," \\\\\n");
  444.     l = get_line(inbuf,tmp,1);
  445.     strcat(w,tmp);
  446.     len += l+1;    inbuf += l+1;
  447.     }
  448. len--;        inbuf--;
  449. return(len);
  450. }
  451.  
  452. int
  453. get_no_math(inbuf,w)        /* get text surrounded by quotes in math mode */
  454. char *inbuf, *w;
  455. {
  456. int c,i,len;
  457.  
  458. len = 0;
  459. for (i=0; (c = *inbuf++) != NULL && c != '\"' && i < MAXWORD; i++)
  460.     {
  461.     if (c == '{' || c == '}')
  462.         {w[i] = '\\';    w[++i] = (char)c;}
  463.     else
  464.         w[i] = (char)c;
  465.     len++;
  466.     }
  467. w[i] = NULL;
  468. return(len);
  469. }
  470.  
  471. char *
  472. get_over_arg(inbuf,ww)        /* get the denominator of over */
  473. char *inbuf, *ww;
  474. {
  475. char w[MAXWORD], tmp1[MAXWORD], tmp2[MAXWORD];
  476. int len;
  477.  
  478. w[0] = NULL;    tmp1[0] = NULL;        tmp2[0] = NULL;
  479. inbuf += getword(inbuf,tmp1);        /* read first word */
  480. inbuf += skip_white(inbuf);
  481. len = getword(inbuf,tmp2);        /* read second word */
  482. if (strcmp(tmp2,".EN") != 0)    inbuf += len;
  483. strcat(w,tmp1);    strcat(w," ");
  484. /* as long as there is a sup or sub read the next two words */
  485. while (strcmp(tmp2,"sub") == 0 || strcmp(tmp2,"sup") == 0)
  486.     {
  487.     strcat(w,tmp2); strcat(w," ");
  488.     inbuf += skip_white(inbuf);
  489.     inbuf += getword(inbuf,tmp1);
  490.     strcat(w,tmp1); strcat(w," ");
  491.     inbuf += skip_white(inbuf);
  492.     len = getword(inbuf,tmp2);
  493.     if (strcmp(tmp2,".EN") != 0)    inbuf += len;
  494.     }
  495. troff_tex(w,ww,0);
  496. return(inbuf);
  497. }
  498.  
  499. int
  500. get_ref(inbuf,w)        /* get reference */
  501. char *inbuf, *w;
  502. {
  503. int len=0, l=0, lines=0;
  504. char tmp[MAXWORD];
  505.  
  506. w[0] = NULL;    tmp[0] = NULL;
  507. while (*inbuf != NULL)
  508.     {
  509.     if (*inbuf == '\n')        break;
  510.     (void) getword(inbuf,tmp);
  511.     if (tmp[0] == '.' && isupper(tmp[1]))
  512.         {
  513. /* these commands don't cause a break in reference */
  514.         if (strcmp(tmp,".R") != 0 && strcmp(tmp,".I") != 0
  515.             && strcmp(tmp,".B") != 0)
  516.             break; 
  517.         }
  518.     else if (tmp[0] == '.' && !(isupper(tmp[1])))
  519.         {
  520. /* these commands don't cause a break in reference */
  521.         if (strcmp(tmp,".br") != 0 && strcmp(tmp,".bp") != 0)
  522.             break; 
  523.         }
  524.     l = get_line(inbuf,tmp,1);
  525.     lines++;
  526.     if (lines > 1)        strcat(w," ");
  527.     strcat(w,tmp);
  528.     len += l+1;    inbuf += l+1;
  529.     }
  530. len--;        inbuf--;
  531. return(len);
  532. }
  533.  
  534. void
  535. get_size(ww,PARAMETER)
  536. char *ww;
  537. struct measure *PARAMETER;
  538. {
  539. int sign=0, units=0;
  540. float value;
  541.  
  542. if (ww[0] == NULL)
  543.     {
  544.     if (PARAMETER->def_value == 0)
  545.         {
  546.         PARAMETER->value = PARAMETER->old_value;
  547.         strcpy(PARAMETER->units,PARAMETER->old_units);
  548.         }
  549.     else
  550.         {
  551.         PARAMETER->value = PARAMETER->def_value;
  552.         strcpy(PARAMETER->units,PARAMETER->def_units);
  553.         }
  554.     }
  555. else
  556.     {
  557.     PARAMETER->old_value = PARAMETER->value;
  558.     strcpy(PARAMETER->old_units,PARAMETER->units);
  559.     parse_units(ww,&sign,&value,&units);
  560.     if (units == 'p')
  561.         strcpy(PARAMETER->units,"pt");
  562.     else if (units == 'i')
  563.         strcpy(PARAMETER->units,"in");
  564.     else if (units == 'c')
  565.         strcpy(PARAMETER->units,"cm");
  566.     else if (units == 'm')
  567.         strcpy(PARAMETER->units,"em");
  568.     else if (units == 'n')
  569.         {
  570.         value = .5*value;    /* n is about half the width of m */
  571.         strcpy(PARAMETER->units,"em");
  572.         }
  573.     else if (units == 'v')
  574.         strcpy(PARAMETER->units,"ex");
  575.     else if (units == 0)
  576.         {
  577.         if (sign == 0 || PARAMETER->old_units[0] == NULL)
  578.             strcpy(PARAMETER->units,PARAMETER->def_units);
  579.         else
  580.             strcpy(PARAMETER->units,PARAMETER->old_units);
  581.         }
  582.     else
  583.         {
  584.         fprintf(stderr,"unknown units %c, using default units\n");
  585.         strcpy(PARAMETER->units,PARAMETER->def_units);
  586.         }
  587.     if (sign == 0)    PARAMETER->value = value;
  588.     else        PARAMETER->value = PARAMETER->old_value + sign*value;
  589.     }
  590. }
  591.  
  592. int
  593. get_string(inbuf,w,rec)        /* get the rest of the line -- Nelson Beebe */
  594. char *inbuf, *w;
  595. int rec;            /* rec=1 means recursion is required */
  596. {
  597. register int c,i,len;
  598. char ww[MAXLINE];
  599. register char *start;
  600.  
  601. if (*inbuf != '\"')
  602.     return(get_line(inbuf,w,rec));
  603. start = inbuf;                /* remember start so we can find len */
  604. i=0;
  605. inbuf++;                /* point past initial quote */
  606. while ((c = *inbuf++) != NULL && c != '\"' && c != '\n' && i < MAXLINE)
  607.     ww[i++] = (char)c;
  608. ww[i] = NULL;
  609. if (c != '\n')                /* flush remainder of line */
  610.     while ((c = *inbuf++) != '\n')
  611.     /* NO-OP */;
  612. len = inbuf - start - 1;        /* count only up to NL, not past */
  613. if (rec == 1)
  614.     troff_tex(ww,w,0);
  615. else
  616.     strcpy(w,ww);
  617. return(len);
  618. }
  619.  
  620. int
  621. get_sub_arg(inbuf,w)        /* get the argument for sub and sup */
  622. char *inbuf, *w;
  623. {
  624. int c,len,i;
  625. char ww[MAXWORD], tmp[MAXWORD];
  626.  
  627. len=0;    tmp[0] = NULL;
  628. while ((c = *inbuf) == ' ' || c == '\t')
  629.         {inbuf++;    len++;}
  630. i=0;
  631. while ((c = *inbuf++) != NULL && c != ' ' && c != '\t' && c != '\n'
  632.         && c != '$' && c != '}' && c != '~' && i < MAXWORD)
  633.         {ww[i++] = (char)c;    len++;}
  634. ww[i] = NULL;
  635. if (strcmp(ww,"roman") == 0  || strcmp(ww,"bold") == 0 || strcmp(w,"italic") == 0)
  636.     {
  637.     (void) get_arg(inbuf,tmp,0);
  638.     sprintf(ww,"%s%c%s",ww,c,tmp);
  639.     len += strlen(tmp)+1;
  640.     }
  641. troff_tex(ww,w,0);        /* recursive */
  642. return(len);
  643. }
  644.  
  645. int
  646. get_table_entry(inbuf,w,tab)
  647. char *inbuf, *w;
  648. int tab;
  649. {
  650. int c, i=0;
  651.  
  652. for (i=0; (c = *inbuf++) != NULL && c != tab && i < MAXWORD; i++)
  653.         w[i] = (char)c;
  654. w[i] = NULL;
  655.  
  656. return(i);
  657. }
  658.  
  659. int
  660. getdef(inbuf,ww)        /* get the define substitution */
  661. char *inbuf, *ww;
  662. {
  663. int c,i,len;
  664. int def_delim;
  665. char w[MAXWORD];
  666.  
  667. def_delim = *inbuf++;        /* take first character as delimiter */
  668. len=1;        i=0;
  669. while ((c = *inbuf++) != NULL && c != def_delim && i < MAXWORD)
  670.     {len++;        w[i++] = (char)c;}
  671. w[i] = NULL;
  672. len++;
  673. if (c != def_delim)
  674.     {
  675.     fprintf(stderr,"WARNING: missing right delimiter in define, define=%s\n",w);
  676.     len--;
  677.     }
  678. troff_tex(w,ww,0);        /* now translate the substitution */
  679. return(len);
  680. }
  681.  
  682. int
  683. getword(inbuf,w)        /* get an alphanumeric word (dot also) */
  684. char *inbuf, *w;
  685. {
  686. int c,i;
  687.  
  688. for (i=0; (c = *inbuf++) != NULL
  689.     && (isalpha(c) || isdigit(c) || c == '.') && i < MAXWORD; i++)
  690.         w[i] = (char)c;
  691. if (i == 0 && c != NULL)
  692.     w[i++] = (char)c;
  693. w[i] = NULL;
  694. return(i);
  695. }
  696.  
  697. void
  698. GR_to_Greek(w,ww)            /* change GREEK to Greek */
  699. char *w, *ww;
  700. {
  701. *ww++ = '\\';        *ww++ = *w;
  702. while(*++w != NULL)
  703.     *ww++ = tolower(*w);
  704. *ww = NULL;
  705. }
  706.  
  707. int
  708. is_def(w)        /* check if w was defined by the user */
  709. char *w;
  710. {
  711. int i;
  712.  
  713. for (i=0; i < def_count; i++)
  714.     {
  715.     if (strcmp(def[i].def_macro,w) == 0)
  716.         return(i);
  717.     }
  718. return(-1);
  719. }
  720.  
  721. int
  722. is_flip(w)        /* check if w is in the flip list */
  723. char *w;
  724. {
  725. int i;
  726.  
  727. for (i=0; i < flip_count; i++)
  728.     {
  729.     if (strcmp(flip_list[i],w) == 0)
  730.         return(i);
  731.     }
  732. return(-1);
  733. }
  734.  
  735. int
  736. is_forbid(w)        /* check if w is one of those sacred macros */
  737. char *w;
  738. {
  739. int i;
  740.  
  741. for (i=0; i < forbd_count; i++)
  742.     {
  743.     if (strcmp(forbid[i],w) == 0)
  744.         return(i);
  745.     }
  746. return(-1);
  747. }
  748.  
  749. int
  750. is_mathcom(w,ww)    /* check if w has a simple correspondence in TeX */
  751. char *w,*ww;
  752. {
  753. int i;
  754.  
  755. for (i=0; i < mathcom_count; i++)
  756.     {
  757.     if (strcmp(math[i].troff_symb,w) == 0)
  758.         {
  759.         strcpy(ww,math[i].tex_symb);
  760.         return(i);
  761.         }
  762.     }
  763. return(-1);
  764. }
  765.  
  766. int
  767. is_mydef(w)        /* check if w is user-defined macro */
  768. char *w;
  769. {
  770. int i;
  771.  
  772. for (i=0; i < mydef_count; i++)
  773.     {
  774.     if (strcmp(mydef[i].def_macro,w) == 0)
  775.         return(i);
  776.     }
  777. return(-1);
  778. }
  779.  
  780. int
  781. is_troff_mac(w,ww,arg)    /* check if w is a macro or plain troff command */
  782. char *w,*ww;
  783. int *arg;
  784. {
  785. int i;
  786.  
  787. for (i=0; i < macro_count; i++)
  788.     {
  789.     if (strcmp(macro[i].troff_mac,w) == 0)
  790.         {
  791.         strcpy(ww,macro[i].tex_mac);
  792.         *arg = macro[i].arg;
  793.         return(i);
  794.         }
  795.     }
  796. return(-1);
  797. }
  798.  
  799. void
  800. parse_units(ww,sign,value,units)
  801. char *ww;
  802. int *sign, *units;
  803. float *value;
  804. {
  805. int len, k=0, i;
  806. char tmp[MAXWORD];
  807.  
  808. len = strlen(ww);
  809. if (ww[0] == '-')    *sign = -1;
  810. else if (ww[0] == '+')    *sign = 1;
  811. if (*sign != 0)        k++;
  812.  
  813. i=0;
  814. while (k < len)
  815.     {
  816.     if (isdigit(ww[k]) || ww[k] == '.')
  817.         tmp[i++] = ww[k++];
  818.     else    break;
  819.     }
  820. tmp[i] = NULL;
  821. sscanf(tmp,"%f",value);
  822. i=0;
  823. if (k < len)
  824.     {
  825.     *units = ww[k++];
  826.     if (k < len)
  827.         fprintf(stderr,
  828.         "Suspect problem in parsing %s, unit used is %c\n",ww,*units);
  829.     }
  830. }
  831.  
  832. void
  833. scrbuf(in,out)            /* copy input to output */
  834. FILE *in,*out;
  835. {
  836. int c;
  837. while ((c =getc(in)) != EOF)    putc(c,out);
  838. }
  839.  
  840. int
  841. similar(w)            /* check if w is in the similar list */
  842. char *w;
  843. {
  844. int i;
  845.  
  846. for (i=0; i < sim_count ; i++)
  847.     {
  848.     if (strcmp(sim_list[i],w) == 0)
  849.         return(1);
  850.     }
  851. return(-1);
  852. }
  853.  
  854. char *
  855. skip_line(inbuf)        /* ignore the rest of the line */
  856. char *inbuf;
  857. {
  858. while (*inbuf != '\n' && *inbuf != NULL)
  859.     inbuf++;
  860. if (*inbuf == NULL)    return(inbuf);
  861. else            return(++inbuf);
  862. }
  863.  
  864. int
  865. skip_white(inbuf)        /* skip white space */
  866. char *inbuf;
  867. {
  868. int c,len=0;
  869.  
  870. while ((c = *inbuf++) == ' ' || c == '\t' || c == '\n')
  871.     len++;    
  872. return(len);
  873. }
  874.  
  875. char *
  876. strapp(s,tail)  /* copy tail[] to s[], return ptr to terminal NULL in s[] */
  877. register char *s;    /* Nelson Beebe */
  878. register char *tail;
  879. {
  880. while (*s++ = *tail++)
  881.     /*NO-OP*/;
  882. return (s-1);            /* pointer to NULL at end of s[] */
  883. }
  884.  
  885. void
  886. tmpbuf(in,buffer)
  887. /* copy input to buffer, buffer holds only MAXLEN characters */
  888. FILE *in;
  889. char *buffer;
  890. {
  891. int c;
  892. unsigned int l=0;
  893.  
  894. while (l++ < MAXLEN && (c = getc(in)) != EOF)
  895.     *buffer++ = (char)c;
  896. if (l >= MAXLEN)
  897.     {
  898.     fprintf(stderr,"Sorry: document is too large\n");
  899.     exit(-1);
  900.     }
  901. *buffer = NULL;
  902. }
  903.