home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / NROFF1.ZIP / NROTXT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-07  |  13.6 KB  |  635 lines

  1. /*
  2.  *      Text processing portion of NRO word processor
  3.  *
  4.  *      Stephen L. Browning
  5.  *      5723 North Parker Avenue
  6.  *      Indianapolis, Indiana 46220
  7.  *
  8.  *    Ported to MS C 5.1 
  9.  *      by John Dudeck (jdudeck@polyslo.calpoly.edu) 11/25/90.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "nro.h"
  14. #include "nroxtrn.h"
  15.  
  16. text(p)
  17. char *p;
  18. {
  19.         int i;
  20.         char wrdbuf[MAXLINE];
  21.  
  22.         if (*p == ' ' || *p == '\n' || *p == '\r') leadbl(p);
  23.         expesc(p,wrdbuf);
  24.         if (dc.ulval > 0) {
  25.                 /*
  26.                 *       Because of the way underlining is handled,
  27.                 *       MAXLINE should be declared to be three times
  28.                 *       larger than the longest expected input line
  29.                 *       for underlining.  Since many of the character
  30.                 *       buffers use this parameter, a lot of memory
  31.                 *       can be allocated when it may not really be
  32.                 *       needed.  A MAXLINE of 180 would allow about
  33.                 *       60 characters in the output line to be
  34.                 *       underlined (remember that only alphanumerics
  35.                 *       get underlined - no spaces or punctuation).
  36.                 */
  37.                 if (dc.buflg) underl(p,wrdbuf,MAXLINE);
  38.                 --dc.ulval;
  39.         }
  40.         if (dc.cuval > 0) {
  41.                 if (dc.buflg) underl(p,wrdbuf,MAXLINE);
  42.                 --dc.cuval;
  43.         }
  44.         if (dc.boval > 0) {
  45.                 if (dc.buflg) bold(p,wrdbuf,MAXLINE);
  46.                 --dc.boval;
  47.         }
  48.         if (dc.ceval > 0) {
  49.                 center(p);
  50.                 put(p);
  51.                 --dc.ceval;
  52.         }
  53.         else if (*p == '\r' || *p == '\n') put(p); /* all blank line */
  54.         else if (dc.fill == NO) put(p);         /* unfilled */
  55.         else {
  56.                 while ((i = getwrd(p,wrdbuf)) > 0) {
  57.                         putwrd(wrdbuf);
  58.                         p += i;
  59.                 }
  60.         }
  61. }
  62.  
  63.  
  64. /*
  65.  *      insert bold face text
  66.  */
  67.  
  68. bold(p0,p1,size)
  69. char *p0, *p1;
  70. int size;
  71. {
  72.         int i, j;
  73.  
  74.         j = 0;
  75.         for (i=0; (p0[i] != '\n') && (j < size-1); ++i) {
  76.                 if (isalpha(p0[i]) || isdigit(p0[i])) {
  77.                         p1[j++] = p0[i];
  78.                         p1[j++] = '\b';
  79.                 }
  80.                 p1[j++] = p0[i];
  81.         }
  82.         p1[j++] = '\n';
  83.         p1[j] = EOS;
  84.         while (*p1 != EOS) *p0++ = *p1++;
  85.         *p0 = EOS;
  86. }
  87.  
  88.  
  89.  
  90.  
  91. /*
  92.  *      center a line by setting tival
  93.  */
  94.  
  95. center(p)
  96. char *p;
  97. {
  98.         dc.tival = max((dc.rmval + dc.tival - width(p)) >> 1,0);
  99. }
  100.  
  101.  
  102. /*
  103.  *      expand title buffer to include character string
  104.  */
  105.  
  106. expand(p0,c,s)
  107. char *p0;
  108. char c;
  109. char *s;
  110. {
  111.         char tmp[MAXLINE];
  112.         char *p, *q, *r;
  113.  
  114.         p = p0;
  115.         q = tmp;
  116.         while (*p != EOS) {
  117.                 if (*p == c) {
  118.                         r = s;
  119.                         while (*r != EOS) *q++ = *r++;
  120.                 }
  121.                 else *q++ = *p;
  122.                 ++p;
  123.         }
  124.         *q = EOS;
  125.         strcpy(p0,tmp);         /* copy it back */
  126. }
  127.  
  128.  
  129. /*
  130.  *      get field from title
  131.  */
  132.  
  133. char *getfield(p,q,delim)
  134. char *p, *q;
  135. char delim;
  136. {
  137.         while (*p != delim && *p != '\r' && *p != '\n' && *p != EOS) {
  138.                 *q++ = *p++;
  139.         }
  140.         *q = EOS;
  141.         if (*p == delim) ++p;
  142.         return(p);
  143. }
  144.  
  145.  
  146.  
  147. /*
  148.  *      get non-blank word from p0 into p1.
  149.  *      return number of characters processed.
  150.  */
  151.  
  152. getwrd(p0,p1)
  153. char *p0,*p1;
  154. {
  155.         int i;
  156.         char *p, c;
  157.  
  158.         i = 0;
  159.         while (*p0 == ' ' || *p0 == '\t') {
  160.                 ++i;
  161.                 ++p0;
  162.         }
  163.         p = p0;
  164.         while (*p0 != ' ' && *p0 != EOS && *p0 != '\t') {
  165.                 if (*p0 == '\n' || *p0 == '\r') break;
  166.                 *p1 = *p0++;
  167.                 ++p1;
  168.                 ++i;
  169.         }
  170.         c = *(p1-1);
  171.         if (c == '"') c = *(p1-2);
  172.         if (c == '?' || c == '!') {
  173.                 *p1++ = ' ';
  174.                 ++i;
  175.         }
  176.         if (c == '.' && (*p0 == '\n' || *p0 == '\r' || islower(*p))) {
  177.                 *p1++ = ' ';
  178.                 ++i;
  179.         }
  180.         *p1 = EOS;
  181.         return(i);
  182. }
  183.  
  184.  
  185. /*
  186.  *      convert integer to decimal ascii string
  187.  */
  188.  
  189. itoda(value,p,size)
  190. int value;
  191. char *p;
  192. int size;
  193. {
  194.         char c[7];
  195.         int i, j, k;
  196.         int aval;
  197.  
  198.         aval = abs(value);
  199.         c[0] = EOS;
  200.         i = 1;
  201.         do {
  202.                 c[i++] = (aval % 10) + '0';
  203.                 aval /= 10;
  204.         } while (aval > 0 && i <= size);
  205.         if (value < 0 && i <= size) c[i++] = '-';
  206.         for (j=0; j<i; ++j) *p++ = c[i-j-1];
  207.         return(i);
  208. }
  209.  
  210.  
  211. /*
  212.  *      center title text into print buffer
  213.  */
  214.  
  215. justcntr(p,q,limit)
  216. char *p, *q;
  217. int limit[];
  218. {
  219.         int len;
  220.  
  221.         len = width(p);
  222.         q = &q[(limit[RIGHT] + limit[LEFT] - len) >> 1];
  223.         while (*p != EOS) *q++ = *p++;
  224. }
  225.  
  226.  
  227.  
  228. /*
  229.  *      left justify title text into print buffer
  230.  */
  231.  
  232. justleft(p,q,limit)
  233. char *p, *q;
  234. int limit;
  235. {
  236.         q = &q[limit];
  237.         while (*p != EOS) *q++ = *p++;
  238. }
  239.  
  240.  
  241. /*
  242.  *      right justify title text into print buffer
  243.  */
  244.  
  245. justrite(p,q,limit)
  246. char *p, *q;
  247. int limit;
  248. {
  249.         int len;
  250.  
  251.         len = width(p);
  252.         q = &q[limit - len];
  253.         while (*p != EOS) *q++ = *p++;
  254. }
  255.  
  256.  
  257.  
  258.  
  259. /*
  260.  *      delete leading blanks, set tival
  261.  */
  262.  
  263. leadbl(p)
  264. char *p;
  265. {
  266.         int i,j;
  267.  
  268.         brk();
  269.         for (i=0; p[i] == ' '; ++i) ;
  270.         if (p[i] != '\n' && p[i] != '\r') dc.tival = i;
  271.         for (j=0; p[i] != EOS; ++j) p[j] = p[i++];
  272.         p[j] = EOS;
  273. }
  274.  
  275.  
  276.  
  277. /*
  278.  *      find minimum of two integer
  279.  */
  280.  
  281. min(v1,v2)
  282. int v1,v2;
  283. {
  284.         return((v1 < v2) ? v1 : v2);
  285. }
  286.  
  287.  
  288.  
  289. /*
  290.  *      find maximum of two integers
  291.  */
  292.  
  293. max(v1,v2)
  294. int v1,v2;
  295. {
  296.         return((v1 > v2) ? v1 : v2);
  297. }
  298.  
  299.  
  300.  
  301. /*
  302.  *      put out page footer
  303.  */
  304.  
  305. pfoot()
  306. {
  307.         if (dc.prflg == TRUE) {
  308.                 skip(pg.m3val);
  309.                 if (pg.m4val > 0) {
  310.                         if ((pg.curpag % 2) == 0) {
  311.                                 puttl(pg.efoot,pg.eflim,pg.curpag);
  312.                         }
  313.                         else {
  314.                                 puttl(pg.ofoot,pg.oflim,pg.curpag);
  315.                         }
  316.                         skip(pg.m4val - 1);
  317.                 }
  318.         }
  319. }
  320.  
  321.  
  322.  
  323. /*
  324.  *      put out page header
  325.  */
  326.  
  327. phead()
  328. {
  329.         pg.curpag = pg.newpag;
  330.         if (pg.curpag >= pg.frstpg && pg.curpag <= pg.lastpg) {
  331.                 dc.prflg = TRUE;
  332.         }
  333.         else {
  334.                 dc.prflg = FALSE;
  335.         }
  336.         ++pg.newpag;
  337.         if (dc.prflg == TRUE) {
  338.                 if (pg.m1val > 0) {
  339.                         skip(pg.m1val - 1);
  340.                         if ((pg.curpag % 2) == 0) {
  341.                                 puttl(pg.ehead,pg.ehlim,pg.curpag);
  342.                         }
  343.                         else {
  344.                                 puttl(pg.ohead,pg.ohlim,pg.curpag);
  345.                         }
  346.                 }
  347.                 skip(pg.m2val);
  348.         }
  349.         /*
  350.         *       initialize lineno for the next page
  351.         */
  352.         pg.lineno = pg.m1val + pg.m2val + 1;
  353. }
  354.  
  355.  
  356. /*
  357.  *      print character with test for printer
  358.  */
  359.  
  360. prchar(c,fp)
  361. char c;
  362. FILE *fp;
  363. {
  364.         putc(c,fp);
  365. }
  366.  
  367.  
  368.  
  369.  
  370. /*
  371.  *      put out line with proper spacing and indenting
  372.  */
  373.  
  374. put(p)
  375. char *p;
  376. {
  377.         char os[MAXLINE];
  378.         int j;
  379.  
  380.         if (pg.lineno == 0 || pg.lineno > pg.bottom) {
  381.                 phead();
  382.         }
  383.         if (dc.prflg == TRUE) {
  384.                 if (!dc.bsflg) {
  385.                         if (strkovr(p,os) == TRUE) {
  386.                                 for (j=0; j<pg.offset; ++j) prchar(' ',pout);
  387.                                 for (j=0; j<dc.tival; ++j) prchar(' ',pout);
  388.                                 putlin(os,pout);
  389.                         }
  390.                 }
  391.                 for (j=0; j<pg.offset; ++j) prchar(' ',pout);
  392.                 for (j=0; j<dc.tival; ++j) prchar(' ',pout);
  393.                 putlin(p,pout);
  394.         }
  395.         dc.tival = dc.inval;
  396.         skip(min(dc.lsval-1,pg.bottom-pg.lineno));
  397.         pg.lineno = pg.lineno + dc.lsval;
  398.         if (pg.lineno > pg.bottom) pfoot();
  399. }
  400.  
  401.  
  402. /*
  403.  *      output a null terminated string to the file
  404.  *      specified by pbuf.
  405.  */
  406.  
  407. putlin(p,pbuf)
  408. char *p;
  409. struct buf *pbuf;
  410. {
  411.         while (*p != EOS) prchar(*p++,pbuf);
  412. }
  413.  
  414.  
  415.  
  416. /*
  417.  *      put out title or footer
  418.  */
  419.  
  420. puttl(p,lim,pgno)
  421. char *p;
  422. int lim[];
  423. int pgno;
  424. {
  425.         int i;
  426.         char pn[8];
  427.         char t[MAXLINE];
  428.         char h[MAXLINE];
  429.         char delim;
  430.  
  431.         itoda(pgno,pn,6);
  432.         for (i=0; i<MAXLINE; ++i) h[i] = ' ';
  433.         delim = *p++;
  434.         p = getfield(p,t,delim);
  435.         expand(t,dc.pgchr,pn);
  436.         justleft(t,h,lim[LEFT]);
  437.         p = getfield(p,t,delim);
  438.         expand(t,dc.pgchr,pn);
  439.         justcntr(t,h,lim);
  440.         p = getfield(p,t,delim);
  441.         expand(t,dc.pgchr,pn);
  442.         justrite(t,h,lim[RIGHT]);
  443.         for (i=MAXLINE-4; h[i] == ' '; --i) h[i] = EOS;
  444.         h[++i] = '\n';
  445.         h[++i] = EOS;
  446.         if (strlen(h) > 2) {
  447.                 for (i=0; i<pg.offset; ++i) prchar(' ',pout);
  448.         }
  449.         putlin(h,pout);
  450. }
  451.  
  452.  
  453.  
  454. /*
  455.  *      put word in output buffer
  456.  */
  457.  
  458. putwrd(wrdbuf)
  459. char *wrdbuf;
  460. {
  461.         int w;
  462.         int last;
  463.         int llval;
  464.         char *p0, *p1;
  465.         int nextra;
  466.  
  467.         w = width(wrdbuf);
  468.         last = strlen(wrdbuf) + co.outp;
  469.         llval = dc.rmval - dc.tival;
  470.         if(((co.outp > 0) && ((co.outw + w) > llval)) || (last > MAXLINE)) {
  471.                 last -= co.outp;
  472.                 if(dc.juval == YES) {
  473.                         nextra = llval - co.outw + 1;
  474.                         /*
  475.                         *       Check whether last word was end of
  476.                         *       sentence and modify counts so that
  477.                         *       it is right justified.
  478.                         */
  479.                         if (co.outbuf[co.outp-2] == ' ') {
  480.                                 --co.outp;
  481.                                 ++nextra;
  482.                         }
  483.                         spread(co.outbuf,co.outp-1,nextra,co.outwds);
  484.                         if((nextra > 0) && (co.outwds > 1)) {
  485.                                 co.outp += (nextra - 1);
  486.                         }
  487.                 }
  488.                 brk();
  489.         }
  490.         p0 = wrdbuf;
  491.         p1 = co.outbuf + co.outp;
  492.         while(*p0 != EOS) *p1++ = *p0++;
  493.         co.outp = last;
  494.         co.outbuf[co.outp++] = ' ';
  495.         co.outw += w + 1;
  496.         ++co.outwds;
  497. }
  498.  
  499.  
  500. /*
  501.  *      skips the number of lines specified by n.
  502.  */
  503.  
  504. skip(n)
  505. int n;
  506. {
  507.         int i;
  508.  
  509.         if (dc.prflg == TRUE && n > 0) {
  510.                 for(i=0; i<n; ++i) {
  511.                         prchar('\n',pout);
  512.                 }
  513.             /*  prchar('\r',pout); */
  514.         }
  515. }
  516.  
  517.  
  518.  
  519. /*
  520.  *      spread words to justify right margin
  521.  */
  522.  
  523. spread(p,outp,nextra,outwds)
  524. char p[];
  525. int outp,nextra,outwds;
  526. {
  527.         int i,j;
  528.         int nb,ne,nholes;
  529.  
  530.         if((nextra <= 0) || (outwds <= 1)) return;
  531.         dc.sprdir = ~dc.sprdir;
  532.         ne = nextra;
  533.         nholes = outwds - 1;    /* holes between words */
  534.         i = outp - 1;   /* last non-blank character */
  535.         j = min(MAXLINE-3,i+ne); /* leave room for CR, LF, EOS  */
  536.         while(i < j) {
  537.                 p[j] = p[i];
  538.                 if(p[i] == ' ') {
  539.                         if(dc.sprdir == 0) nb = (ne - 1)/nholes + 1;
  540.                         else nb = ne/nholes;
  541.                         ne -= nb;
  542.                         --nholes;
  543.                         for(; nb>0; --nb) {
  544.                                 --j;
  545.                                 p[j] = ' ';
  546.                         }
  547.                 }
  548.                 --i;
  549.                 --j;
  550.         }
  551. }
  552.  
  553.  
  554.  
  555. /*
  556.  *      split overstrikes (backspaces) into seperate buffer
  557.  */
  558.  
  559. strkovr(p,q)
  560. char *p, *q;
  561. {
  562.         char *pp;
  563.         int bsflg;
  564.  
  565.         bsflg = FALSE;
  566.         pp = p;
  567.         while (*p != EOS) {
  568.                 *q = ' ';
  569.                 *pp = *p;
  570.                 ++p;
  571.                 if (*p == '\b') {
  572.                         if (*pp >= ' ' && *pp <= '~') {
  573.                                 bsflg = TRUE;
  574.                                 *q = *pp;
  575.                                 ++p;
  576.                                 *pp = *p;
  577.                                 ++p;
  578.                         }
  579.                 }
  580.                 ++q;
  581.                 ++pp;
  582.         }
  583.         *q++ = '\r';
  584.         *q = *pp = EOS;
  585.         return(bsflg);
  586. }
  587.  
  588.  
  589.  
  590. /*
  591.  *      underline a line
  592.  */
  593.  
  594. underl(p0,p1,size)
  595. char *p0,*p1;
  596. int size;
  597. {
  598.         int i,j;
  599.  
  600.         j = 0;
  601.         for (i=0; (p0[i] != '\n') && (j < size-1); ++i) {
  602.                 if (p0[i] >= ' ' && p0[i] <= '~') {
  603.                         if (isalpha(p0[i]) || isdigit(p0[i]) || dc.cuval > 0) {
  604.  
  605.                                 p1[j++] = '_';
  606.                                 p1[j++] = '\b';
  607.                         }
  608.                 }
  609.                 p1[j++] = p0[i];
  610.         }
  611.         p1[j++] = '\n';
  612.         p1[j] = EOS;
  613.         while (*p1 != EOS) *p0++ = *p1++;
  614.         *p0 = EOS;
  615. }
  616.  
  617.  
  618. /*
  619.  *      compute width of character string
  620.  */
  621.  
  622. width(s)
  623. char *s;
  624. {
  625.         int w;
  626.  
  627.         w = 0;
  628.         while (*s != EOS) {
  629.                 if (*s == '\b') --w;
  630.                 else if (*s != '\n' && *s != '\r') ++w;
  631.                 ++s;
  632.         }
  633.         return(w);
  634. }
  635.