home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol126 / roff43.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-04-29  |  4.6 KB  |  221 lines

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