home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 145_01 / roff43.c < prev    next >
Text File  |  1979-12-31  |  6KB  |  272 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 BDS
  66.         if(pd->bf = alloc(BUFSIZ)) /*if space for buffer*/
  67.             {if(fcreat(wbuf,pd->bf)!=ERROR)
  68. #endif
  69. #if C86
  70.         if (1)
  71.             {if((pd->bf = fopen(wbuf,"wb"))!=ERROR)
  72. #endif
  73. #if DeSmet
  74.         if (1)
  75.             {if((pd->bf = creat(wbuf,"w"))!=ERROR)
  76. #endif
  77.                 fprintf(stderr,
  78.                 "<%s> rewritten for .DI\n",
  79.                 wbuf);
  80.             else    {fprintf(stderr,
  81.                 "<%s> cannot be rewritten\n",
  82.                 wbuf); exit();
  83.                 }
  84.             }
  85.         else    {fprintf(stderr,
  86.                 "\nCan't get space for <%s>\n",
  87.                 wbuf); exit();
  88.             }
  89.     }
  90. else    /*not in table*/
  91.     {pw=TREND;
  92.     *pw=DLINK;
  93.     pc1=pc=pw+1;
  94.     wb=wbuf;
  95.     transfer(&wb,&pc,'\0');
  96.     DLINK=TREND;
  97.     pd=pc;
  98.     TREND = pd+1;
  99.     pd->nm = pc1;
  100.     pd->ls = pd->cs = 0;
  101. #if BDS
  102.     if(pd->bf = alloc(BUFSIZ))
  103.         {if(fcreat(wbuf,pd->bf)==ERROR)
  104. #endif
  105. #if C86
  106.     if (1)
  107.         {if((pd->bf = fopen(wbuf,"wb"))==ERROR)
  108. #endif
  109. #if DeSmet
  110.     if (1)
  111.         {if((pd->bf = creat(wbuf,"w"))==ERROR)
  112. #endif
  113.             {fprintf(stderr,
  114.                 "Can't create <%s>\n",
  115.                 wbuf);
  116.             exit();
  117.             }
  118.         }
  119.     else    {fprintf(stderr,"Can't allocate buf for %s\n",
  120.             wbuf);
  121.         exit();
  122.         }
  123.     }
  124. while(fgets2(LINE,iobuf))
  125.     {if((*LINE==COMMAND)&&(comtyp(LINE)==ED)) break;
  126.     if(pc=macq(LINE))
  127.         {pbstr(pc);
  128.         continue;
  129.         }
  130.     /*else*/
  131.     fputs(LINE,pd->bf);
  132.     (pd->ls)++;
  133.     (pd->cs) += 1+strlen(LINE);/*crlf=2*/
  134.     }
  135. }
  136. /****************************************/
  137. source()
  138. {struct divfd *pd;
  139. char name[MAXLINE];
  140. getwrd(LINE,name);
  141. if(getwrd(LINE,name)!=WE_HAVE_A_WORD)
  142.     {fprintf(stderr,".SO has no name\n");
  143.     return;
  144.     }
  145. ucstr(name);
  146.  
  147. if(pd=find2(name,DLINK)) dclose(pd);
  148. #if    BDS
  149. if(FPTR<FMAX) FSTACK[FPTR++]=iobuf;
  150. else    {fprintf(stderr,"FSTACK overflow\n");
  151.     exit();
  152.     }
  153. if(iobuf=alloc(BUFSIZ))
  154.     {if(fopen(name,iobuf)!=ERROR) return;
  155.     /*else*/
  156.     fprintf(stderr,"Can't open <%s>\n",name);
  157.     }
  158. else fprintf(stderr,".SO can't buffer <%s>\n",name);
  159. #else    /* More normal C compilers    */
  160. if(FPTR<FMAX) FSTACK[FPTR++]=iobuf;
  161. else    {fprintf(stderr,"FSTACK overflow\n");
  162.     exit();
  163.     }
  164. #if C86
  165. if((iobuf = fopen(name,"r"))!=ERROR) return;
  166. #endif
  167. #if DeSmet    /* Well, almost normal    */
  168. if((iobuf = fopen(name,"r"))!=FERR) return;
  169. #endif
  170. /*else*/
  171. fprintf(stderr,"Can't open <%s>\n",name);
  172. #endif
  173. endso();
  174. }
  175. /****************************************/
  176. showr()    /*lists register names and contents*/
  177. {int *pw, *pr;
  178. char *pc;
  179. fprintf(stderr,"REGISTERS and <values>:\n");
  180. pw=RLINK;
  181. while(pw)
  182.     {pc=pw+1;
  183.     fprintf(stderr,"%s ",pc);
  184.     pr=pc+1+strlen(pc);
  185.     fprintf(stderr,"<%d>\n",*pr);
  186.     pw=*pw;
  187.     }
  188. dashes();
  189. }
  190. /****************************************/
  191. showd()    /*shows all diversions and status*/
  192. {int *pw;
  193. char *pc;
  194. struct divfd *pd;
  195.  
  196. fprintf(stderr, "Diversion files:\n");
  197. pw=DLINK;
  198. while(pw)
  199.     {pc=pw+1;
  200.     fprintf(stderr,"%s: ",pc);
  201.     pd=pc+1+strlen(pc);
  202.     fprintf(stderr,"%d characters, %d lines [",
  203.             pd->cs, pd->ls );
  204.     if(pd->bf) fprintf(stderr,"open]\n");
  205.     else    fprintf(stderr,"closed]\n");
  206.     pw =*pw;
  207.     }
  208. dashes();
  209. }
  210. /****************************************/
  211. dsclose()    /*flushes and closes all open diversions*/
  212. {int *pw;
  213. char *pc;
  214. struct divfd *pd;
  215.  
  216. pw = DLINK;
  217. while(pw)
  218.     {pc=pw+1;
  219.     pd=pc+1+strlen(pc);
  220.     if(pd->bf) dclose(pd);
  221.     pw=*pw;
  222.     }
  223. }
  224. /****************************************/
  225. dclose(pd)    /*flushes and closes diversion*/
  226. struct divfd *pd;
  227. {if(pd&&(!(pd->bf))) return(FALSE);
  228. fprintf(stderr,"closing %s ",pd->nm);
  229. #if    MSDOS == 0
  230. putc(CPMEOF,pd->bf);
  231. #endif
  232. #if BDS
  233. fflush(pd->bf);
  234. #endif
  235. #if C86
  236. fflush(pd->bf);
  237. #endif
  238. if(fclose(pd->bf)==ERROR)
  239.     fprintf(stderr,"****bad close****\n");
  240. else    {fprintf(stderr,"[O.K.]\n");
  241. #if BDS
  242.     free(pd->bf);
  243. #endif
  244.     pd->bf = FALSE;
  245.     }
  246. return(TRUE);
  247. }
  248. /****************************************/
  249. ucstr(s)    /*convert s to upper case*/
  250. char *s;
  251. {while(*s)
  252.     {*s = toupper(*s);
  253.     s++;
  254.     }
  255. }
  256. /****************************************/
  257. endso()    /*called upon EOF to return to previous input file*/
  258. {if(FPTR)
  259.     {fclose(iobuf);
  260. #if    BDS
  261.     free(iobuf);
  262. #endif
  263.     iobuf=FSTACK[--FPTR];
  264.     if DEBUG fprintf(stderr, "endso: FPTR = %d\n", FPTR);
  265.     }
  266. BINP=0;
  267. }
  268. /****************************************/
  269. dashes()
  270. {fprintf(stderr,"-----------\n");
  271. }
  272.