home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / c / cc03.arc / NRO.C < prev    next >
Text File  |  1985-09-12  |  7KB  |  272 lines

  1. /*
  2.  *      Word Processor
  3.  *      similar to Unix NROFF or RSX-11M RNO -
  4.  *      adaptation of text processor given in
  5.  *      "Software Tools", Kernighan and Plauger.
  6.  *
  7.  *      Stephen L. Browning
  8.  *      5723 North Parker Avenue
  9.  *      Indianapolis, Indiana 46220
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "nro.h"
  14. #define EXTERN
  15. #include "nroxtrn.h"
  16.  
  17. abs(foo)
  18. int foo;
  19. {
  20.         return( (foo > 0) ? foo : -foo);
  21. }
  22.  
  23.  
  24. main(argc,argv)
  25. int argc;
  26. char *argv[];
  27. {
  28.         int i;
  29.         int swflg;
  30.         int ifp, ofp;
  31.         
  32.         swflg = FALSE;
  33.         pout = stdout;
  34.         ifp = ofp = 0;
  35.         init();
  36.         for (i=1; i<argc; ++i) {
  37.                 if (*argv[i] == '-' || *argv[i] == '+') {
  38.                         if (pswitch(argv[i],&swflg) == ERR) exit(-1);
  39.                 }
  40.                 else if (*argv[i] == '>') {
  41.                         if (ofp == 0) {
  42.                                 if (!strcmp(argv[i]+1,"$P")) {
  43.                                         ofp = 1;
  44.                                         co.lpr = TRUE;
  45.                                 }
  46.                                 else if ((oub = fopen(argv[i]+1,"w")) == NULL) {
  47.                                         printf("nro: cannot create %s\n",argv[i]+1);
  48.                                         exit(-1);
  49.                                 }
  50.                                 else {
  51.                                         pout = oub;
  52.                                 }
  53.                         }
  54.                         else {
  55.                                 puts("nro: too many output files\n");
  56.                                 exit(-1);
  57.                         }
  58.                 }
  59.         }
  60.         for (i=1; i<argc; ++i) {
  61.                 if (*argv[i] != '-' && *argv[i] != '+' && *argv[i] != '>') {
  62.                         if ((sofile[0] = fopen(argv[i],"r")) == NULL) {
  63.                                 printf("nro: unable to open file %s\n",argv[i]);
  64.                                 exit(-1);
  65.                         }
  66.                         else {
  67.                                 profile();
  68.                                 fclose(sofile[0]);
  69.                         }
  70.                 }
  71.         }
  72.         if (argc == 1) {
  73.                 puts("Usage: nro [-n] [+n] [-pxx] [-v] [-b] [-mmacfile] infile ... [>outfile]\n");
  74.                 exit(-1);
  75.         }
  76.         if (pout != stdout) {
  77.                 putc(CPMEOF,pout);
  78.                 fflush(pout);
  79.                 fclose(pout);
  80.         }
  81. }
  82.  
  83.  
  84.  
  85. /*
  86.  *      retrieve one line of input text
  87.  */
  88.  
  89. getlin(p,in_buf)
  90. char *p;
  91. FILE *in_buf;
  92. {
  93.         int i;
  94.         int c;
  95.         char *q;
  96.  
  97.         q = p;
  98.         for (i=0; i<MAXLINE-1; ++i) {
  99.                 c = ngetc(in_buf);
  100.                 if (c == CPMEOF || c == EOF) {
  101.                         *q = EOS;
  102.                         c = strlen(p);
  103.                         return(c == 0 ? EOF : c);
  104.                 }
  105.                 *q++ = c;
  106.                 if (c == '\n') break;
  107.         }
  108.         *q = EOS;
  109.         return(strlen(p));
  110. }
  111.  
  112.  
  113.  
  114. /*
  115.  *      initialize parameters for nro word processor
  116.  */
  117.  
  118. init()
  119. {
  120.         int i;
  121.  
  122.         dc.fill = YES;
  123.         dc.lsval = 1;
  124.         dc.inval = 0;
  125.         dc.rmval = PAGEWIDTH - 1;
  126.         dc.tival = 0;
  127.         dc.ceval = 0;
  128.         dc.ulval = 0;
  129.         dc.cuval = 0;
  130.         dc.juval = YES;
  131.         dc.boval = 0;
  132.         dc.bsflg = FALSE;
  133.         dc.pgchr = '#';
  134.         dc.cmdchr = '.';
  135.         dc.prflg = TRUE;
  136.         dc.sprdir = 0;
  137.         for (i=0; i<26; ++i) dc.nr[i] = 0;
  138.         pg.curpag = 0;
  139.         pg.newpag = 1;
  140.         pg.lineno = 2;
  141.         pg.plval = PAGELEN;
  142.         pg.m1val = 2;
  143.         pg.m2val = 2;
  144.         pg.m3val = 2;
  145.         pg.m4val = 2;
  146.         pg.bottom = pg.plval - pg.m4val - pg.m3val;
  147.         pg.offset = 0;
  148.         pg.frstpg = 0;
  149.         pg.lastpg = 30000;
  150.         pg.ehead[0] = pg.ohead[0] = '\n';
  151.         pg.efoot[0] = pg.ofoot[0] = '\n';
  152.         for (i=1; i<MAXLINE; ++i) {
  153.                 pg.ehead[i] = pg.ohead[i] = EOS;
  154.                 pg.efoot[i] = pg.ofoot[i] = EOS;
  155.         }
  156.         pg.ehlim[LEFT] = pg.ohlim[LEFT] = dc.inval;
  157.         pg.eflim[LEFT] = pg.oflim[LEFT] = dc.inval;
  158.         pg.ehlim[RIGHT] = pg.ohlim[RIGHT] = dc.rmval;
  159.         pg.eflim[RIGHT] = pg.oflim[RIGHT] = dc.rmval;
  160.         co.outp = 0;
  161.         co.outw = 0;
  162.         co.outwds = 0;
  163.         co.lpr = FALSE;
  164.         for (i=0; i<MAXLINE; ++i) co.outbuf[i] = EOS;
  165.         for (i=0; i<MXMDEF; ++i) mac.mnames[i] = NULL;
  166.         mac.lastp = 0;
  167.         mac.emb = &mac.mb[0];
  168.         mac.ppb = NULL;
  169. }
  170.  
  171.  
  172. /*
  173.  *      get character from input file or push back buffer
  174.  */
  175.  
  176. ngetc(infp)
  177. FILE *infp;
  178. {
  179.         int c;
  180.  
  181.         if (mac.ppb >= &mac.pbb[0]) {
  182.                 c = *mac.ppb--;
  183.         }
  184.         else {
  185.                 c = getc(infp);
  186.         }
  187.         return(c);
  188. }
  189.  
  190.  
  191.  
  192. /*
  193.  *      process input files from command line
  194.  */
  195.  
  196. profile()
  197. {
  198.         char ibuf[MAXLINE];
  199.  
  200.         for (dc.flevel=0; dc.flevel>=0; --dc.flevel) {
  201.                 while (getlin(ibuf,sofile[dc.flevel]) != EOF) {
  202.                         if (ibuf[0] == dc.cmdchr) comand(ibuf);
  203.                         else text(ibuf);
  204.                 }
  205.                 if (dc.flevel > 0) fclose(sofile[dc.flevel]);
  206.         }
  207.         if (pg.lineno > 0) space(HUGE);
  208. }
  209.  
  210.  
  211.  
  212. /*
  213.  *      process switch values from command line
  214.  */
  215.  
  216. pswitch(p,q)
  217. char *p;
  218. int *q;
  219. {
  220.         int swgood;
  221.  
  222.         swgood = TRUE;
  223.         if (*p == '-') {
  224.                 switch (tolower(*++p)) {
  225.                 case 'b':
  226.                         dc.bsflg = TRUE;
  227.                         break;
  228.                 case 'm':
  229.                         if ((sofile[0] = fopen(++p,"r")) == NULL) {
  230.                                 printf("***nro: unable to open file %s\n",p);
  231.                                 exit(-1);
  232.                         }
  233.                         profile();
  234.                         fclose(sofile[0]);
  235.                         break;
  236.                 case 'p':
  237.                    set(&pg.offset,ctod(++p),'1',0,0,HUGE);
  238.                         break;
  239.                 case 'v':
  240.                         printf("NRO version 1.0\n");
  241.                         *q = TRUE;
  242.                         break;
  243.                 case '0':
  244.                 case '1':
  245.                 case '2':
  246.                 case '3':
  247.                 case '4':
  248.                 case '5':
  249.                 case '6':
  250.                 case '7':
  251.                 case '8':
  252.                 case '9':
  253.                         pg.lastpg = ctod(p);
  254.                         break;
  255.                 default:
  256.                         swgood = FALSE;
  257.                         break;
  258.                 }
  259.         }
  260.         else if (*p == '+') {
  261.                 pg.frstpg = ctod(++p);
  262.         }
  263.         else {
  264.                 swgood = FALSE;
  265.         }
  266.         if (swgood == FALSE) {
  267.                 printf("nro: illegal switch %s\n",p);
  268.                 return(ERR);
  269.         }
  270.         return(OK);
  271. }
  272. a