home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 128_01 / roff43.c < prev    next >
Text File  |  1985-03-10  |  5KB  |  224 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.60            */
  4. /*                            */
  5. /*(C) 1983,4 by Ernest E. Bergmann            */
  6. /*        Physics, Building #16            */
  7. /*        Lehigh Univerisity            */
  8. /*        Bethlehem, Pa. 18015            */
  9. /*                            */
  10. /* Permission is hereby granted for all commercial and    */
  11. /* non-commercial reproduction and distribution of this    */
  12. /* material provided this notice is included.        */
  13. /*                            */
  14. /********************************************************/
  15. /*MAR 10, 1984*/
  16. #include "roff4.h"
  17. /****************************************/
  18. regist(s)    /*checks RLIST; creates new entry if needed;
  19.         returns pointer to integer variable*/
  20. char *s;
  21. {int *pw;
  22. char *pc,*s2;
  23. pw=find2(s,RLINK);
  24. if(pw) return(pw); /*else create new entry*/
  25. pw=TREND;
  26. *pw=RLINK;
  27. pc=pw+1;
  28. s2=s;
  29. transfer(&s2,&pc,'\0');
  30. RLINK=TREND;
  31. pw=pc;
  32. *pw=REGDEF;
  33. TREND=pw+1;
  34. return(pw);
  35. }
  36. /****************************************/
  37. dovar()    /*process .RG request*/
  38. {char typ, wbuf[MAXLINE];
  39. int val, *pw;
  40. getwrd(LINE,wbuf);
  41. skip_blanks(LINE);
  42. val=get_val(LINE,&typ);
  43. getwrd(LINE,wbuf);
  44. if(wbuf[0]==NUMSIGN && wbuf[1]=='\0')
  45.     {set(&NEWPAG,val,typ,NEWPAG,1,9999);
  46.     }
  47. else    {pw=regist(wbuf);
  48.     set(pw,val,typ,REGDEF,1,9999);
  49.     }
  50. }
  51. /****************************************/
  52. dodiv()        /*process .DIversion*/
  53. {char wbuf[MAXLINE],*pc,*pc1,*wb;
  54. struct divfd *pd;
  55. int *pw;
  56. getwrd(LINE,wbuf);
  57. if(getwrd(LINE,wbuf)!=WE_HAVE_A_WORD)
  58.     {strcpy(wbuf,"JUNK.$$$");
  59.     fprintf(STDERR,"DIVERSION has no name, %s assumed\n",
  60.                 wbuf);
  61.     }
  62. ucstr(wbuf);
  63. if(pd=find2(wbuf,DLINK))    /*if in table*/
  64.     {if(!(pd->bf))        /*if not open*/
  65.         if(pd->bf = alloc(BUFSIZ)) /*if space for buffer*/
  66.             {if(fcreat(wbuf,pd->bf)!=ERROR)
  67.                 fprintf(STDERR,
  68.                 "<%s> rewritten for .DI\n",
  69.                 wbuf);
  70.             else    {fprintf(STDERR,
  71.                 "<%s> cannot be rewritten\n",
  72.                 wbuf); exit();
  73.                 }
  74.             }
  75.         else    {fprintf(STDERR,
  76.                 "\nCan't get space for <%s>\n",
  77.                 wbuf); exit();
  78.             }
  79.     }
  80. else    /*not in table*/
  81.     {pw=TREND;
  82.     *pw=DLINK;
  83.     pc1=pc=pw+1;
  84.     wb=wbuf;
  85.     transfer(&wb,&pc,'\0');
  86.     DLINK=TREND;
  87.     pd=pc;
  88.     TREND = pd+1;
  89.     pd->nm = pc1;
  90.     pd->ls = pd->cs = 0;
  91.     if(pd->bf = alloc(BUFSIZ))
  92.         {if(fcreat(wbuf,pd->bf)==ERROR)
  93.             {fprintf(STDERR,
  94.                 "Can't create <%s>\n",
  95.                 wbuf);
  96.             exit();
  97.             }
  98.         }
  99.     else    {fprintf(STDERR,"Can't allocate buf for %s\n",
  100.             wbuf);
  101.         exit();
  102.         }
  103.     }
  104. while(fgets2(LINE,IOBUF))
  105.     {if((*LINE==COMMAND)&&(comtyp(LINE)==ED)) break;
  106.     if(pc=macq(LINE))
  107.         {pbstr(pc);
  108.         continue;
  109.         }
  110.     /*else*/
  111.     fputs(LINE,pd->bf);
  112.     (pd->ls)++;
  113.     (pd->cs) += 1+strlen(LINE);/*crlf=2*/
  114.     }
  115. }
  116. /****************************************/
  117. source()
  118. {struct divfd *pd;
  119. char name[MAXLINE];
  120. getwrd(LINE,name);
  121. if(getwrd(LINE,name)!=WE_HAVE_A_WORD)
  122.     {fprintf(STDERR,".SO has no name\n");
  123.     return;
  124.     }
  125. ucstr(name);
  126. if(pd=find2(name,DLINK)) dclose(pd);
  127. if(FPTR<FMAX) FSTACK[FPTR++]=IOBUF;
  128. else    {fprintf(STDERR,"FSTACK overflow\n");
  129.     exit();
  130.     }
  131. if(IOBUF=alloc(BUFSIZ))
  132.     {if(fopen(name,IOBUF)!=ERROR) return;
  133.     /*else*/
  134.     fprintf(STDERR,"Can't open <%s>\n",name);
  135.     }
  136. else fprintf(STDERR,".SO can't buffer <%s>\n",name);
  137. endso();
  138. }
  139. /****************************************/
  140. showr()    /*lists register names and contents*/
  141. {int *pw, *pr;
  142. char *pc;
  143. fprintf(STDERR,"REGISTERS and <values>:\n");
  144. pw=RLINK;
  145. while(pw)
  146.     {pc=pw+1;
  147.     fprintf(STDERR,"%s ",pc);
  148.     pr=pc+1+strlen(pc);
  149.     fprintf(STDERR,"<%d>\n",*pr);
  150.     pw=*pw;
  151.     }
  152. dashes();
  153. }
  154. /****************************************/
  155. showd()    /*shows all diversions and status*/
  156. {int *pw;
  157. char *pc;
  158. struct divfd *pd;
  159.  
  160. fprintf(STDERR, "Diversion files:\n");
  161. pw=DLINK;
  162. while(pw)
  163.     {pc=pw+1;
  164.     fprintf(STDERR,"%s: ",pc);
  165.     pd=pc+1+strlen(pc);
  166.     fprintf(STDERR,"%d characters, %d lines [",
  167.             pd->cs, pd->ls );
  168.     if(pd->bf) fprintf(STDERR,"open]\n");
  169.     else    fprintf(STDERR,"closed]\n");
  170.     pw =*pw;
  171.     }
  172. dashes();
  173. }
  174. /****************************************/
  175. dsclose()    /*flushes and closes all open diversions*/
  176. {int *pw;
  177. char *pc;
  178. struct divfd *pd;
  179.  
  180. pw = DLINK;
  181. while(pw)
  182.     {pc=pw+1;
  183.     pd=pc+1+strlen(pc);
  184.     if(pd->bf) dclose(pd);
  185.     pw=*pw;
  186.     }
  187. }
  188. /****************************************/
  189. dclose(pd)    /*flushes and closes diversion*/
  190. struct divfd *pd;
  191. {if(pd&&(!(pd->bf))) return(FALSE);
  192. fprintf(STDERR,"closing %s ",pd->nm);
  193. putc(CPMEOF,pd->bf);
  194. fflush(pd->bf);
  195. if(fclose(pd->bf)==ERROR)
  196.     fprintf(STDERR,"****bad close****\n");
  197. else    {fprintf(STDERR,"[O.K.]\n");
  198.     free(pd->bf);
  199.     pd->bf = FALSE;
  200.     }
  201. return(TRUE);
  202. }
  203. /****************************************/
  204. ucstr(s)    /*convert s to upper case*/
  205. char *s;
  206. {while(*s)
  207.     {*s = toupper(*s);
  208.     s++;
  209.     }
  210. }
  211. /****************************************/
  212. endso()    /*called upon EOF to return to previous input file*/
  213. {if(FPTR)
  214.     {fclose(IOBUF);
  215.     free(IOBUF);
  216.     IOBUF=FSTACK[--FPTR];
  217.     }
  218. BINP=0;
  219. }
  220. /****************************************/
  221. dashes()
  222. {fprintf(STDERR,"-----------\n");
  223. }
  224. ********************/