home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / ccmd / cmpara.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-03  |  37.4 KB  |  1,379 lines

  1. /*
  2.  Author: Howie Kaye
  3.  
  4.  Columbia University Center for Computing Activities, July 1986.
  5.  Copyright (C) 1986, 1987, Trustees of Columbia University in the City
  6.  of New York.  Permission is granted to any individual or institution
  7.  to use, copy, or redistribute this software so long as it is not sold
  8.  for profit, provided this copyright notice is retained.
  9. */
  10.  
  11. /*
  12.  * cmpara
  13.  * parse 'chunks' of text, with some nice actions defined.
  14.  *
  15.  * texti style data input.
  16.  */
  17.  
  18. /*
  19.  * This file contains the code to parse chunks of text, simlilarly to the 
  20.  * way MM parses a message on the DEC-20
  21.  *
  22.  * Accepts a char *, which is:
  23.  *  a buffer of initialized text -- so that the function can be 
  24.  *    interrupted, and then continued.
  25.  *  and a list of action characters.  These are only in place when inside 
  26.  *  the actual paragraph parse.  When an action char is typed.
  27.  * 
  28.  * This is pretty hairy.  What it does, is to change the action routines
  29.  * being used, to allow the default/user defined actions to work.  It also
  30.  * has to manage it's own set of cmd buffers and work buffers, so that
  31.  * calls to ccmd insertion routines (cmsti) do not overflow the buffer
  32.  * space which ccmd has been given (cmbufs call).  The routines here are
  33.  * meant to be called after a confirm has been given, and no reparses into
  34.  * previous data will be necessary.  It can be used to parse large chunks
  35.  * of text, in a manner similar to the TEXTI jsys under TOPS-20.
  36.  *
  37.  * In order to make ccmd appear to be in a normal state whenever we leave
  38.  * the paragraph environment, we have to remember the old environment, and
  39.  * 'context switch' the old environment in (and out again) whenever an
  40.  * action character is typed (the action routine may make asumptions about
  41.  * the ccmd environment, and try to parse something).  To do this, all
  42.  * action character force a call to the break_hndlr() routine.  This then
  43.  * saves the environment, calls the real handler, and then restores the
  44.  * environment.  It also fixes the cmd internal buffers to reflect the new
  45.  * state.   
  46.  * 
  47.  * When a user supplied (or default) action routine is called, it is
  48.  * passed the text it is acting on (This buffer is malloc'ed to the correct
  49.  * size, and should not be written to if the write will go past the bounds
  50.  * of the current buffer).  The routine is also passed two flags.  If the
  51.  * buffer is modified, then the modified flag should be set.  If the
  52.  * parse should end (eof,abort,etc), then the ret flags should be set.  In
  53.  * all cases, a pointer to the new text should be returned.
  54.  */
  55.  
  56.                     /* allocate cmd stuff here */
  57. #define PARAERR
  58.  
  59.                     /* system structures/defines */
  60. #include "ccmdlib.h"
  61. #include "cmfncs.h"
  62.                     /* incremental buffer size */
  63. #define BSIZ 1000
  64.                     /* minimum space left before buffer */
  65.                     /* expansion */
  66. #define MINSIZE 133
  67.  
  68. #define beep() cmputc(BELL,cmcsb._cmoj)
  69.  
  70. int paraparse(), paracomplete(), parahelp(); /* forward declarations */
  71.  
  72. /* 
  73.  * paragraph break mask
  74.  */
  75. static brktab parabrk = {
  76.   {                    /* all print characters */
  77.                     /* except tab and space */
  78.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
  79.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  80.   },
  81.   {
  82.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
  83.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  84.   }
  85. };
  86.     
  87. /*
  88.  * The behavior of this parser is different from the standard
  89.  * ccmd interface, so we must set up our own action table.
  90.  */
  91.  
  92. /*
  93.  * forward declare our action routines
  94.  */
  95. char *redispact(), *eofact(), *xbegact(), *xfixact(), *xquoact(), *xdelact(),
  96.      *xwrdact(),*editact(),*dispact(),*insact(),*abortact(), *eolact(),
  97.      *autowrapact(), *autowraptab(), *autowrapspace(), *autowrapnl(),
  98.      *filteract(), *executeact();
  99.  
  100. /* 
  101.  * default paragraph actions
  102.  */
  103. para_actions def_actions[] = {
  104.   { '\002', insact },            /* ^B insert file */
  105.   { '\004', eofact },            /* ^D end of file */
  106.   { '\005', editact },            /* ^E invoke editor on this buffer */
  107.   { '\006', filteract },        /* ^F run buffer through filter */
  108.   { '\b', xdelact },            /* back space */
  109.   { '\t', autowraptab },        /* autowrap on tab */
  110.   { '\n', autowrapnl },            /* \n end of line wrap */
  111.   { '\r', autowrapnl },            /* CR or LF. */
  112.   { '\013', dispact },            /* ^K display whole buffer */
  113.   { '\f', redispact },            /* redisplay screen */
  114.   { '\016', abortact },            /* ^N abort this insertion */
  115.   { '\020', executeact },        /* ^P execute command and insert */
  116.   { '\022', xfixact },            /* ^R redisplay line */
  117.   { '\025', xbegact },            /* ^U delete line */
  118.   { '\026', xquoact },            /* ^V quota character */
  119.   { '\027', xwrdact },            /* ^W delete backwards word */
  120.   { ' ', autowrapspace },        /* SPC autowrap on space */
  121.   { '\177', xdelact },            /* DEL delete previous char */
  122.   { NULL, NULL },            /* end the table */
  123. };
  124.  
  125. static int (**oldact)();        /* old action table */
  126.  
  127.                     /* clean paragraph action table */
  128. static int (*(paraact[128]))() = {
  129.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  130.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  131.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  132.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  133.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  134.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  135.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  136.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  137.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  138.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  139.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  140.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  141.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  142.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  143.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  144.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL
  145. };
  146.  
  147.                     /* clean user defined action table */
  148. static char * (*(paraact1[128]))() = {
  149.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  150.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  151.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  152.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  153.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  154.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  155.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  156.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  157.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  158.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  159.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  160.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  161.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  162.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  163.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  164.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL
  165. };
  166.  
  167. /*
  168.  * global state variables
  169.  */
  170.  
  171. static int eof=FALSE;
  172. static int abort = FALSE;
  173. static csb savecsb;
  174. static int *cmdbuf=NULL;
  175. static int set=FALSE;            /* first call to parse, or an */
  176. static jmp_buf ojmp;
  177.  
  178.                     /* handler structure */
  179. ftspec ft_para = { paraparse, parahelp, paracomplete, 0, ¶brk };
  180.  
  181. static int (*oerrh)();
  182.  
  183. parareset(code) int code; {
  184.   bcopy(ojmp, cmerjb, sizeof (jmp_buf)); /* put back old error handler */
  185.   null_actions();            /* restore action block */
  186.   cmact(oldact);            /* put back the original action table*/
  187.   set = FALSE;                /* uninitialized...for next time */
  188.   cmcsb = savecsb;
  189.   cmcsb._cmerh = oerrh;
  190.   if (cmcsb._cmwbp)
  191.       free(cmcsb._cmwbp);
  192.   cmcsb._cmwbp = NULL;
  193.   return((*cmcsb._cmerh)(code));
  194. }
  195.  
  196. /*
  197.  * parse routine.
  198.  * parse until eof or abort get's set.
  199.  * returns buffer which has been typed, or NULL on abort
  200.  */
  201. PASSEDSTATIC int
  202. paraparse(text,textlen,fdbp,parselen,value)
  203. char *text;
  204. int textlen;
  205. fdb *fdbp;
  206. int *parselen;
  207. pval *value;
  208. {
  209.   int len,i;
  210.   brktab *btab;                /* break table to use */
  211.                     /* action call? */
  212.   static int flg;
  213.   static char *buf;
  214.  
  215.   if (!set) {                /* first time through, set things up */
  216.     bcopy(cmerjb, ojmp, sizeof (jmp_buf));
  217.     
  218.     savecsb = cmcsb;            /* save stae for later */
  219.     oerrh = cmcsb._cmerh;
  220.     if (cmdbuf != NULL)            /* get rid of old buffer */
  221.       free(cmdbuf);
  222.     if ((cmdbuf = (int *)malloc(BSIZ*sizeof(int))) == NULL)
  223.       return(PARAxNM);
  224.     if ((cmcsb._cmwbp = (char *)malloc(BSIZ)) == NULL)
  225.       return(PARAxNM);
  226.     cmcsb._cmwbc = BSIZ;
  227.     cmcsb._cmcnt = BSIZ;
  228.     cmcsb._cmptr = cmdbuf;
  229.     cmcsb._cmbfp = cmdbuf;
  230.     cmcsb._cminc = 0;
  231.     cmcsb._cmflg &= ~CM_NAC;
  232.     cmcsb._cmerh = parareset;
  233.     if (fdbp->_cmdat != NULL) {        /* insert specified starting text */
  234.       para_data *dat = (para_data *) fdbp->_cmdat;
  235.       if (dat->buf != NULL) {
  236.     int len = strlen(dat->buf);
  237.     if (len >= BSIZ) {        /* do we have enough space? */
  238.                     /* no!  get more */
  239.       if ((cmdbuf=(int *)cmrealloc(cmdbuf,(len+BSIZ)*sizeof(int)))==NULL){
  240.         return(PARAxNM);
  241.       }
  242.       cmcsb._cmptr = cmdbuf + (cmcsb._cmptr - cmcsb._cmbfp);
  243.       cmcsb._cmbfp = cmdbuf;
  244.       cmcsb._cmcnt = len + BSIZ;
  245.  
  246.       if ((cmcsb._cmwbp = (char *)cmrealloc(cmcsb._cmwbp,
  247.                         cmcsb._cmcnt)) == NULL){
  248.         return(PARAxNM);
  249.       }
  250.       cmcsb._cmwbc = cmcsb._cmcnt;
  251.     }
  252.     cmsti(dat->buf,CC_NEC|CC_QUO);    /* insert text, without echoing */
  253.     for (i = 0; i < cmcsb._cminc; i++) { /* turn off the hidden flag */
  254.       cmcsb._cmptr[i] &= ~(CC_HID|CC_NEC);
  255.       if (cmcsb._cmptr[i] == NEWLINE|CC_QUO) /*and don't quote newlines */
  256.         cmcsb._cmptr[i] &= ~(CC_QUO);
  257.     }
  258.       }
  259.       null_actions();            /* zero out the action table */
  260.       if (dat->actions != NULL) {    /* install user/default actions */
  261.     if (fdbp->_cmffl & PARA_DEF)
  262.       install_actions(def_actions);
  263.     install_actions(dat->actions);
  264.       }
  265.       else {
  266.     install_actions(def_actions);
  267.       }
  268.     }
  269.     oldact = cmcsb._cmact;        /* save old action table */
  270.     cmact(paraact);            /* now use our own action characters */
  271.     eof = FALSE;            /* note done yet */
  272.     abort = FALSE;
  273.     set = TRUE;                /* but we are initialized */
  274.   }
  275.  
  276.   if (cmcsb._cmcnt < MINSIZE) {        /* running out of buffer space? */
  277.                     /* then get more space */
  278.     if ((cmdbuf=(int *)cmrealloc(cmdbuf,(cmcsb._cmcnt+ BSIZ)
  279.                  *sizeof(int)))==NULL){
  280.       return(PARAxNM);
  281.     }
  282.     cmcsb._cmptr = cmdbuf + (cmcsb._cmptr - cmcsb._cmbfp);
  283.     cmcsb._cmbfp = cmdbuf;        /* and install it into the CSB */
  284.     cmcsb._cmcnt += BSIZ;        /* count it */
  285.  
  286.     if ((cmcsb._cmwbp = (char *)cmrealloc(cmcsb._cmwbp,cmcsb._cmcnt)) == NULL){
  287.       return(PARAxNM);
  288.     }
  289.   }
  290.  
  291.   if (eof) {                /* done! */
  292.     if (buf != NULL)            /* free previous buffer */
  293.       free(buf);
  294.     
  295.     if ((buf = (char *)malloc(textlen+1)) == NULL) { /* get space */
  296.       return(PARAxNM);
  297.     }
  298.     strncpy(buf,text,textlen);        /* copy text into buffer */
  299.     buf[textlen] = '\0';        /* NULL terminate it */
  300.     for (i = 0; i < textlen; i++)
  301.       buf[i] &= 0x7f;            /* strip those quota characters out. */
  302.     value->_pvpara = buf;        /* point return value at it */
  303.     *parselen = textlen;        /* set the length properly */
  304.     null_actions();            /* restore action block */
  305.     bcopy(cmerjb, ojmp, sizeof (jmp_buf)); /* put back old error handler */
  306.     cmact(oldact);            /* put back the original action table*/
  307.     set = FALSE;            /* initialized...for next time */
  308.     if (cmcsb._cmcol != 0)
  309.     cmxnl();
  310.     free(cmcsb._cmwbp);
  311.     cmcsb = savecsb;            /* restore ccmd's buffers */
  312.     return(CMxOK);            /* return done */
  313.   }
  314.   else
  315.     if (abort) {
  316.       bcopy(ojmp, cmerjb, sizeof (jmp_buf)); /* put back old error handler */
  317.       null_actions();            /* restore action block */
  318.       cmact(oldact);            /* put back the original action table*/
  319.       set = FALSE;            /* uninitialized...for next time */
  320.       value->_pvpara = NULL;
  321.       *parselen = textlen;
  322.       if (cmcsb._cmcol != 0)
  323.       cmxnl();
  324.       free(cmcsb._cmwbp);        /* restore ccmd's buffers */
  325.       cmcsb = savecsb;
  326.       return(CMxOK);
  327.     }
  328.   return(CMxINC);            /* nothing here...return incomplete */
  329. }
  330.  
  331. int break_hndlr();            /* forward declaration */
  332.  
  333. /*
  334.  * scan through para_action table, and install actions in 
  335.  * the real actions tables.
  336.  * terminates in NULL function.
  337.  */
  338. install_actions(actions) para_actions *actions; {
  339.   for(; !(actions->actionfunc==NULL && actions->actionchar==NULL);actions++) {
  340.     paraact[actions->actionchar] = break_hndlr;
  341.    paraact1[actions->actionchar] = actions->actionfunc;
  342.   }
  343. }
  344.  
  345. /*
  346.  * zero out the action tables
  347.  */
  348. null_actions() {
  349.   int i;
  350.   for (i = 0; i < 128; i++) {
  351.     paraact[i] = NULL;
  352.     paraact1[i] = NULL;
  353.   }
  354. }
  355.  
  356. /*
  357.  * parse completion.
  358.  * does nothing.
  359.  */
  360. PASSEDSTATIC int
  361. paracomplete(text,textlen,fdbp,full,cplt,cpltlen)
  362. char *text,**cplt;
  363. int textlen,full,*cpltlen;
  364. fdb *fdbp;
  365. {
  366.   *cplt = NULL;
  367.   return(CMP_BEL);        /* beep at them */
  368. }
  369.  
  370. /*
  371.  * helpless
  372.  */
  373.  
  374. PASSEDSTATIC int
  375. parahelp(text,textlen,fdbp,cust)
  376. char *text;
  377. int textlen,cust;
  378. fdb *fdbp;
  379. {
  380.   return(CMxOK);
  381. }
  382.  
  383. /*
  384.  * Action routines
  385.  */
  386.  
  387. /*
  388.  * eof action.
  389.  */
  390. char *
  391. eofact(text,modified,ret) char *text; int *modified,*ret; {
  392.   *ret = TRUE;
  393.   *modified = FALSE;
  394.   return(text);
  395. }
  396.  
  397.  
  398. /*
  399.  * redisplay the last screenful of text
  400.  */
  401.  
  402. char *
  403. redispact(text,modified,ret) char *text; int *modified, *ret;{
  404.   int i, li, co, cols, ov=FALSE;
  405.   char c;
  406.  
  407.   *ret = FALSE;
  408.   *modified = FALSE;
  409.   cmcls();
  410. #ifndef MSDOS
  411.   li = tgetnum("li");            /* get number of lines */
  412.   co = tgetnum("co");            /* and number of columns */
  413. #else
  414.   li = 24;                /* !!! really should check */
  415.   co = 80;                /* !!! display mode */
  416. #endif
  417.   for (i = strlen(text)-1, cols = 0; i >= 0; i--) { /* figure out what */
  418.     c = text[i] & 0x7f;            /*   we can display */
  419.     if (c == '\t')
  420.     cols = ((cols + 8) / 8) * 8;
  421.     else                /* ignore JIS escape */
  422.     if ((c == '\033') && (((text[i+1] == '$') && (text[i+2] == 'B')) ||
  423.                   ((text[i+1] == '(') && (text[i+2] == 'J'))))
  424.         cols -= 2;            /* saw a JIS escape */
  425.     else cols++;            /* incr column count */
  426.                     /* control char takes two chars */
  427.     if (iscntrl(c) && !isspace(c) && (c != 033))
  428.       cols++;                /*   to display, count the ^ */
  429.     if (cols > co) {        
  430.       --li;                /* we overflowed the line */
  431.       cols = 0;                /* reset column count */
  432.       ov = TRUE;            /* remember */
  433.     }
  434.     if (c == '\n') {            /* another line */
  435.       cols = 0;                /* reset the column count */
  436.       li--;
  437.       ov = FALSE;
  438.     }
  439.     if (li == 0)            /* can't display any more lines */
  440.       break;
  441.   }
  442.   if (ov && li == 0) {            /* top line doesn't fit on display */
  443.     int p;
  444.     p = i;
  445.     while ((c = text[p] & 0x7f) != '\n' &&  p > 0) /* find beginning of line */
  446.       p--;
  447.     p += ((i - p)/co + 1) * co;
  448.     i = p-1;                /* skip over the \n */
  449.   }
  450.     
  451.   cmcsb._cmcol = 0;
  452.   for(i++; i < cmcsb._cminc; i++) {    /* display the screenful */
  453.     c = text[i] & 0x7f;
  454.                     /* escape is not a control */
  455.     if (iscntrl(c) && !isspace(c) && c != 033) {
  456.       cmxputc('^');            /* display control chars as */
  457.       cmxputc(c | 0100);        /*   caret and uppercased */
  458.     }
  459.     else cmechx(c);
  460.   }
  461.   return(text);
  462. }
  463.  
  464.  
  465. /*
  466.  * delete to beginning of line.   mostly taken from begact() in stdact.c
  467.  */
  468. char *
  469. xbegact(text,modified,ret) char *text; int *modified, *ret; {
  470.   char *cp;                /* pointer to deletion site */
  471.   int cc;                /* character under examination */
  472.   char c;
  473.   int eralen;                /* # of chars erased */
  474.  
  475.   *modified = FALSE;
  476.   *ret = FALSE;
  477.   if (strlen(text) == 0) return(text);
  478.   cp = text + strlen(text) - 1;     /* point to end of buffer */
  479.   while (cp-- != text) {        /* search for nonhidden newline */
  480.     if (*cp == NEWLINE)            /* newline? */
  481.       break;                /* yup, stop looking */
  482.   }
  483.   cp++;                    /* point to char after break */
  484.   eralen = (text - cp) + strlen(text); /* get # of chars erased */
  485.   if (eralen <= 0) return(text);
  486.   if (cmcsb._cmflg & CM_CRT)
  487.     cmcsb._cmcol = cmxera(eralen,FALSE); /* erase the characters */
  488.   else {
  489.     cmxputs("^U");            /* signal line kill on hardcopy */
  490.     cmxnl();                /* move to a new line */
  491.   }
  492.   *modified = TRUE;
  493.   text[strlen(text)-eralen] = '\0';
  494.   return(text);
  495. }
  496.  
  497. /*
  498.  * wrdact:
  499.  * delete backwards one word.  If at the beginning of a line, 
  500.  * and previous line is empty, just deletes to beginning of
  501.  * previous line.
  502.  */
  503. char *
  504. xwrdact(text,modified,ret) char *text; int *modified, *ret; {
  505.   char *cp;                /* pointer to deletion site */
  506.   int cc;                /* character under examination */
  507.   char c;
  508.   int eralen;                /* # of chars erased */
  509.  
  510.   *modified = FALSE;
  511.   *ret = FALSE;
  512.   if (strlen(text) == 0) {
  513.     beep();
  514.     return(text);
  515.   }
  516.   cp = text + strlen(text) - 1;     /* point to end of buffer */
  517.   while (cp-- != text) {        /* search for a character which is */
  518.     c = *cp;
  519.     if ((c < '0') ||            /* not a letter or digit? */
  520.      ((c > '9') && (c < 'A')) ||
  521.      ((c > 'Z') && (c < 'a')) ||
  522.      (c > 'z')
  523.     )
  524.       break;                /* yup, stop looking */
  525.   }
  526.   cp++;                    /* point to char after break */
  527.   eralen = (text - cp) + cmcsb._cminc; /* get # of chars erased */
  528.   if (eralen <= 0) return(text);
  529.   if (cmcsb._cmflg & CM_CRT)
  530.     cmcsb._cmcol = cmxera(eralen,FALSE); /* erase the characters */
  531.   else {
  532.     cmxputs("^W");            /* signal line kill on hardcopy */
  533.   }
  534.   *modified = TRUE;
  535.   text[strlen(text)-eralen] = '\0';    /* modify in place */
  536.   return(text);
  537. }
  538.  
  539. /* fixact
  540. **
  541. ** Purpose:
  542. **   Refresh the display of the current line of text, back to the
  543. **   last unhidden newline character.  If the last character in the
  544. **   buffer is a newline, the previous line is refreshed.
  545. **/
  546.  
  547. char *
  548. xfixact(text,modified,ret) char *text; int *modified, *ret; {
  549.   char *cp;                /* pointer into buffer */
  550.   int cc;                /* character under examination */
  551.   char c;
  552.   int reflen;                /* # of chars refreshed */
  553.  
  554.   *ret = FALSE;
  555.   *modified = FALSE;
  556.   if (strlen(text) == 0) return(text);
  557.   cp = text + strlen(text) -1;        /* point to end of buffer */
  558.  
  559.   while (cp-- != text) {        /* search for nonhidden newline */
  560.     if (*cp == NEWLINE)            /* newline? */
  561.       break;                /* yup, stop looking */
  562.   }
  563.   cp++;                    /* point to char after break */
  564.  
  565.   reflen = (text - cp) + strlen(text); /* get # of chars to refresh */
  566.  
  567.   if (cmcsb._cmflg & CM_CRT)
  568.     cmcsb._cmcol = cmxera(reflen,FALSE); /* erase the characters from screen */
  569.   else {
  570.     cmxputs("^R");            /* signal line kill on hardcopy */
  571.     cmxnl();                /* move to a new line */
  572.   }
  573.   while (reflen-- > 0) {        /* retype buffer contents */
  574.     c = (cc = *cp++) & CC_CHR;        /* get next char */
  575.     cmechx(c);                /* do it again */
  576.   }
  577.   return(text);
  578. }
  579.  
  580. /* quoact
  581. **
  582. ** Purpose:
  583. **   Enter the next character into the buffer with its quote flag
  584. **   turned on, so it will be treated as a normal character regardless
  585. **   of any handling it would normally receive.
  586. **/
  587. char *
  588. xquoact(text,modified,ret) char *text; int *modified, *ret; {
  589.   char c;                /* quoted character */
  590.   static char *ntext=NULL;
  591.   
  592.   cmgetc(&c,cmcsb._cmij);        /* get another character */
  593.   cmechx(c);                /* do it again */
  594.   if (ntext != NULL) free(ntext);
  595.   if ((ntext = (char *)malloc(strlen(text)+2)) == NULL) {
  596.     cmxputs("?Out of memory\n");
  597.     return(text);
  598.   }
  599.   *modified = TRUE;            /* we modified it */
  600.   *ret = FALSE;                /* don't return yet */
  601.   strcpy(ntext,text);
  602.   ntext[strlen(text)] = c;   /* | CC_QUO; */
  603.   ntext[strlen(text)+1] = '\0';  
  604.   return(ntext);
  605. }
  606.  
  607.  
  608. /* delact
  609. **
  610. ** Purpose:
  611. **   Erase back to and including the last non-hidden character in the
  612. **   command buffer.
  613. **/
  614. char *
  615. xdelact(text,modified,ret) char *text; int *modified, *ret; {
  616.   char *cp;                /* pointer to deletion site */
  617.   int cc;                /* character under examination */
  618.   char c;
  619.   int eralen;                /* # of chars erased */
  620.  
  621.   *modified = FALSE;
  622.   *ret = FALSE;
  623.   if (strlen(text) == 0) {
  624.     beep();
  625.     return(text);
  626.   }
  627.   eralen = 1;
  628.   if (cmcsb._cmflg & CM_CRT)
  629.     cmcsb._cmcol = cmxera(eralen,FALSE); /* erase the characters */
  630.   else {
  631.     cmxputs("\\");            /* signal line kill on hardcopy */
  632.   }
  633.   *modified = TRUE;
  634.   text[strlen(text)-eralen] = '\0';    /* modify in place */
  635.   return(text);
  636. }
  637.  
  638.  
  639. /* 
  640.  * insact
  641.  * insert a file at the end of the buffer
  642.  */
  643. char *
  644. insact(text, modified, ret) char *text; int *modified, *ret; {
  645.                     /* filename fdb */
  646.   static fdb filfdb = { _CMFIL ,FIL_TYPE, NULL, NULL, NULL, NULL, NULL };
  647.                     /* confirm fdb */
  648.   static fdb filfdb1 = { _CMCFM, CM_SDH, &filfdb, NULL, "Confirm to cancel",
  649.              NULL, NULL };
  650.   static fdb cfmfdb = { _CMCFM, 0, NULL, NULL, NULL, NULL, NULL };
  651.   char c;
  652.   char *fname= NULL;
  653.   pval parseval;
  654.   fdb *used;
  655.   int fd;
  656.   struct stat buf;
  657.   static char *ntext;
  658.   int len;
  659.   static int cmdbuf[200];
  660.   static char atmbuf[200],wrkbuf[200];
  661.  
  662.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200);
  663.   cmseter();                /* errors come back to here */
  664.   prompt("Insert file: ");        /* prmpt for a filename */
  665.   cmsetrp();                /* reparses come back here. */
  666.   parse(&filfdb1, &parseval, &used);    /* parse the filename */
  667.   if (used == &filfdb1) {
  668.     cmxputs("...No file inserted\n"); 
  669.     *modified = FALSE;
  670.     return(text);
  671.   }
  672.   if (fname != NULL)            /* free up old filename if */
  673.     free(fname);            /* necessary */
  674.   if ((fname = (char *)malloc(strlen(parseval._pvfil[0])+1)) == NULL) {
  675.     cmxputs("?Out of memory\n");
  676.     return(text);
  677.   }
  678.   strcpy(fname,parseval._pvfil[0]);    /* copy the data */
  679.   parse(&cfmfdb,&parseval,&used);    /* get confirmation */
  680.  
  681.   fd = open(fname,O_RDONLY,0);        /* open up the file */
  682.   if (fd == -1) {            /* couldn't open it... */
  683.     cmxputs("?permission denied\n");    /* complain */
  684.     *modified = FALSE;            /* no modification done */
  685.     return(text);
  686.   }
  687.   stat(fname,&buf);            /* get length of file */
  688.   len = strlen(text) + buf.st_size;
  689.   if (ntext != NULL)
  690.     free(ntext);
  691.   if ((ntext = (char *)malloc(len+1)) == NULL) { /* make space */
  692.     cmxputs("?Out of memory\n"); 
  693.     return(text);
  694.   }
  695.   strcpy(ntext,text);            /* copy old text */
  696.                     /* and add in the file */  
  697.   len = strlen(text) + read(fd,&ntext[strlen(text)],buf.st_size);
  698.   close(fd);                /* close the file */
  699.   cmxputs("[OK]\n"); 
  700.   ntext[len] = '\0';            /* null terminate */
  701.   *modified = TRUE;            /* we changed things */
  702.   *ret = FALSE;
  703.   return(ntext);            /* return new text */
  704. }
  705.  
  706. /*
  707.  * display the whole buffer
  708.  */
  709. char *
  710. dispact(text, modified, ret) char *text; int *modified, *ret; {
  711.   if (cmcsb._cmcol != 0)
  712.     cmxnl();
  713.   cmxputs(text); 
  714.   return(text);
  715. }
  716.  
  717. /*
  718.  * invoke the editor on the buffer
  719.  */
  720. char *
  721. editact(text, modified, ret) char *text; int *modified, *ret; {
  722.   char fname[40];
  723.   char *e,*getenv();
  724.   int fd;
  725.   char buf[100];
  726.   static char *ntext=NULL;
  727.   char *mktemp();
  728.   struct stat sbuf;
  729.   int len,i;
  730.  
  731.   cmxputs("Invoking editor...\n");    /* announce our intentions */
  732.   e = getenv("EDITOR");            /* check editor variable */
  733.   if (e == NULL || *e == '\0')        /* no editor variable? */
  734.     e = DEF_EDITOR;            /* use default editor */
  735.   
  736. #if unix
  737.   strcpy(fname,"/tmp/cmdXXXXXX");    /* get a file name */
  738.   mktemp(fname);
  739. #else
  740.   strcpy(fname,mktemp(getenv("TMP"),"cmd")); /* get a file name */  
  741. #endif
  742.   fd = open(fname,O_WRONLY|O_CREAT,0700); /* open the tmp file */
  743.   if (fd < 0) {
  744.     cmxputs("?Could not create temporary file.\n"); 
  745.     perror(fname);
  746.     return(text);
  747.   }
  748.   if (write(fd,text,strlen(text)) != strlen(text)) {
  749.     cmxputs("?Could not write temporary file.\n"); 
  750.     perror(fname);
  751.     close(fd);
  752.     unlink(fname);
  753.     return(text);
  754.   }
  755.   close(fd);
  756.   strcpy(buf,e);
  757. #ifndef MSDOS
  758.   strcat(buf," +1000000");
  759. #endif
  760.   strcat(buf," ");
  761.   strcat(buf,fname);
  762.   cmtend();                /* put the tty back */
  763.   system(buf);                /* execute it */
  764.   cmtset();
  765.   if (stat(fname,&sbuf) == -1) {    /* get length of file */
  766.     cmxputs("?Temporary file disappeared\n"); 
  767.     return(text);
  768.   }
  769.   len = sbuf.st_size;
  770.   fd = open(fname,O_RDONLY,0);        /* open the file again */
  771.   if (fd < 0) {
  772.     cmxputs("?Could not open temporary file for read.\n"); 
  773.     perror(fname);
  774.     unlink(fname);
  775.     return(text);
  776.   }
  777.   if (ntext != NULL)            /* free up last buffer */
  778.     free(ntext);
  779.   if ((ntext = (char *)malloc(len+1)) == NULL) { /* make space */
  780.     cmxputs("?Out of memory\n"); 
  781.     return(text);
  782.   }
  783. #ifdef MSDOS
  784.   if ((i = read(fd,ntext,len)) < 0)  /* read in the file */
  785. #else
  786.   if ((i = read(fd,ntext,len)) != len) /* read in the file */
  787. #endif
  788.   {
  789.     cmxputs("?Could not read temporary file.\n"); 
  790.     perror(fname);
  791.     unlink(fname);
  792.     return(text);
  793.   }
  794.   close(fd);                /* close the file */
  795.   cmcls();
  796.   ntext[len] = '\0';            /* null terminate */
  797.   *modified = TRUE;
  798.   unlink(fname);
  799.   cmxputs("...Back again\n"); 
  800.   return(ntext);
  801. }
  802.  
  803. /*
  804.  * abort the current buffer
  805.  */
  806. char *
  807. abortact(text, modified, ret) char *text; int *modified, *ret; {
  808.   static fdb cfmfdb = { _CMCFM, CM_SDH, NULL, NULL, "Carriage Return to abort",
  809.             NULL, NULL };
  810.   char cmdbuf[200],atmbuf[200],wrkbuf[200];
  811.   pval parseval;
  812.   fdb *used;
  813.  
  814.   cmcsb._cmerr = CMxOK;
  815.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200);
  816.   cmseter();                /* errors come back to here */
  817.   if (cmcsb._cmerr == CFMxNOC) {
  818.     cmcsb._cmflg &= ~CM_ACT;        /* no pending actions now */
  819.     return(text);
  820.   }
  821.   prompt("[Confirm to Abort]");        /* prompt for a confirm */
  822.   cmsetrp();                /* reparses come back here. */
  823.   parse(&cfmfdb, &parseval, &used);    /* parse the confirm */
  824.   abort = TRUE;
  825.   return(text);
  826. }
  827.  
  828. /*
  829.  * just make us wake up on eol, to check on buffer space...
  830.  */
  831.  
  832. char *
  833. eolact(text, modified, ret) char *text; int *modified, *ret; {
  834.   static char *ntext=NULL;
  835.   
  836.   cmechx('\n');                /* do it again */
  837.   if (ntext != NULL) free(ntext);
  838.   if ((ntext = (char *)malloc(strlen(text)+2)) == NULL) {
  839.     cmxputs("?Out of memory\n"); 
  840.     return(text);
  841.   }
  842.   *modified = TRUE;            /* we modified it */
  843.   *ret = FALSE;                /* don't return yet */
  844.   strcpy(ntext,text);
  845.   ntext[strlen(text)] = '\n';
  846.   ntext[strlen(text)+1] = '\0';  
  847.   return(ntext);
  848. }  
  849.  
  850.  
  851. char *
  852. executeact(text, modified, ret) 
  853. char *text; 
  854. int *modified, *ret; 
  855. {
  856.   char fname[40];
  857.   char *getenv();
  858.   char *output;
  859.   char *cmd, *parse_cmdline();
  860.   struct stat sbuf;
  861.   int fd;
  862.   int len;
  863.   int total;
  864.  
  865.   if ((cmd = parse_cmdline("Command: ")) == NULL) {
  866.     *modified = FALSE;
  867.     return (text);
  868.   }
  869.  
  870. #if unix
  871.   strcpy(fname,"/tmp/cmdXXXXXX");    /* get a file name */
  872.   mktemp(fname);
  873. #else
  874.   strcpy(fname,mktemp(getenv("TMP"),"cmd")); /* get a file name */  
  875. #endif
  876.  
  877.   cmd = (char *) realloc (cmd, strlen(cmd)+strlen(fname)+strlen(" > ")+1);
  878.   strcat (cmd, " > ");
  879.   strcat (cmd, fname);
  880.   
  881.   cmsystem (cmd);
  882.   free (cmd);
  883.  
  884.   if (stat(fname, &sbuf) == -1) {
  885.     cmxputs ("?Temporary file missing\n");
  886.     return (text);
  887.   }
  888.   len = sbuf.st_size;
  889.   total = strlen(text)+len;
  890.   if ((output = (char *) malloc (total+1)) == NULL) {
  891.     cmxputs ("?Out of memory\n");
  892.     *modified = FALSE;
  893.     return (text);
  894.   }
  895.   strcpy (output, text);
  896.   if ((fd = open(fname, O_RDONLY,0)) < 0) {
  897.     cmxputs("?Could not open temporary file for read.\n"); 
  898.     perror(fname);
  899.     unlink(fname);
  900.     free (output);
  901.     return(text);
  902.   }
  903. #ifdef MSDOS
  904.   if (read(fd,output+strlen(text),len) < 0)
  905. #else
  906.   if (read(fd,output+strlen(text),len) != len)
  907. #endif
  908.   {
  909.     cmxputs("?Could not read temporary file.\n"); 
  910.     perror(fname);
  911.     unlink(fname);
  912.     free (output);
  913.     return(text);
  914.   }
  915.   close(fd);                /* close the file */
  916.   output[total] = '\0';
  917.   unlink (fname);
  918.   *modified = TRUE;
  919.   *ret = FALSE;
  920.   cmxputs("[Done]\n");
  921.   return (output);
  922. }
  923.  
  924.  
  925. static brktab fieldbrk = {
  926.   {                    /* print chars except ?, space, tab */
  927.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01,
  928.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  929.   },
  930.   {                    /* print chars except ?, space, tab */
  931.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01,
  932.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  933.   }
  934. };
  935. static filblk efilblk = { NULL, NULL, NULL };
  936. static fdb efilefdb = { _CMFIL, FIL_OLD|FIL_EXEC|CM_SDH, NULL, 
  937.               (pdat) &efilblk, NULL, NULL, NULL };
  938. static fdb filefdb = { _CMFIL, CM_SDH, NULL, 
  939.              (pdat) NULL, NULL, NULL, NULL };
  940. static fdb fieldfdb = { _CMFLD, CM_SDH, NULL, NULL, NULL, NULL, &fieldbrk };
  941. static fdb conffdb = { _CMCFM, CM_SDH, NULL, NULL, 
  942.              "command line", NULL, NULL };
  943. static char **path = NULL;
  944.  
  945. static char *
  946. parse_cmdline (p) char *p; {
  947.   char *cmd;
  948.   char *cmdend;
  949.   pval parseval;
  950.   fdb *used;
  951.   char **get_path();
  952.   char *parse_cmdline1();
  953.   static int cmdbuf[200];
  954.   static char atmbuf[200],wrkbuf[200];
  955.  
  956.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200);
  957.  
  958.   path = get_path();
  959.   efilblk.pathv = path;
  960.  
  961.   cmseter();
  962.   prompt (p);
  963.   cmsetrp();
  964.   parse (fdbchn(&conffdb, &efilefdb, &filefdb, &fieldfdb, NULL), 
  965.      &parseval, &used);
  966.  
  967.   if (used == &conffdb) {
  968.     cmxputs ("Aborting\n");
  969.     return (NULL);
  970.   }
  971.  
  972.   if (used == &efilefdb || used == &filefdb) {
  973.     cmd = (char *) malloc (strlen (parseval._pvfil[0]) + 1);
  974.     strcpy (cmd, parseval._pvfil[0]);
  975.   }
  976.   else if (used == &fieldfdb) {
  977.     cmd = (char *) malloc (strlen (parseval._pvstr) + 1);
  978.     strcpy (cmd, parseval._pvstr);
  979.   }
  980.   
  981.   if (cmdend = parse_cmdline1()) {
  982.       cmd = (char *) realloc (cmd, strlen (cmd)+strlen(cmdend)+2);
  983.       strcat (cmd, " ");
  984.       strcat (cmd, cmdend);
  985.       free (cmdend);
  986.   }
  987.   return (cmd);
  988. }
  989.  
  990.  
  991. static char *
  992. parse_cmdline1 () {
  993.   char *cmd;
  994.   char *cmdend;
  995.   pval parseval;
  996.   fdb *used;
  997.   
  998.   parse (fdbchn(&conffdb, &filefdb, &fieldfdb, NULL), 
  999.      &parseval, &used);
  1000.  
  1001.   if (used == &conffdb)
  1002.     return (NULL);
  1003.  
  1004.   if (used == &filefdb) {
  1005.     cmd = (char *) malloc (strlen (parseval._pvfil[0]) + 1);
  1006.     strcpy (cmd, parseval._pvfil[0]);
  1007.   }
  1008.   else if (used == &fieldfdb) {
  1009.     cmd = (char *) malloc (strlen (parseval._pvstr) + 1);
  1010.     strcpy (cmd, parseval._pvstr);
  1011.   }
  1012.   
  1013.   if (cmdend = parse_cmdline1()) {
  1014.       cmd = (char *) realloc (cmd, strlen (cmd)+strlen(cmdend)+2);
  1015.       strcat (cmd, " ");
  1016.       strcat (cmd, cmdend);
  1017.       free (cmdend);
  1018.   }
  1019.   return (cmd);
  1020. }
  1021.  
  1022.  
  1023. static char **
  1024. get_path() {
  1025.   char **path = NULL;
  1026.   int num = 0;
  1027.   char *PATH, *cp, *getenv();
  1028.   char *p, *index();
  1029.  
  1030.   if ((PATH = getenv ("PATH")) == NULL)
  1031.     return (NULL);
  1032.   else {
  1033.     cp = (char *) malloc (strlen(PATH)+1); /* space for private copy */
  1034.     strcpy (cp, PATH);
  1035.   }
  1036.  
  1037.   while ((p = index (cp, ':')) != NULL) { /* while we have dirs */
  1038.     *p = '\0';                /* tie of with a NULL */
  1039.     if (strlen(cp) != 0) {
  1040.       if (num == 0)             /* first path */
  1041.     path = (char **) malloc (sizeof (char *)); /* space for 1 */
  1042.       else                /* need more space */
  1043.     path = (char **) realloc (path, (num+1)*sizeof(char *));
  1044.       path[num] = (char *) malloc (strlen(cp)+1); /* space for path */
  1045.       strcpy (path[num], cp);        /* copy path into array */
  1046.       num++;                /* increment count */
  1047.     }
  1048.     cp = ++p;                /* move on */
  1049.   }
  1050.   /* the last path element */
  1051.   path = (char **) realloc (path, (num+2)*sizeof(char *));
  1052.   path[num] = (char *) malloc (strlen(cp)+1);
  1053.   strcpy (path[num], cp);
  1054.   num++;
  1055.   path[num] = NULL;            /* tie off with null path */
  1056.   return (path);
  1057. }
  1058.  
  1059. char *
  1060. autowrapspace (text, modified, ret)
  1061. char *text;
  1062. int *modified, *ret;
  1063. {
  1064.   return (autowrapact (text, modified, ret, ' '));
  1065. }
  1066.  
  1067. char *
  1068. autowrapnl (text, modified, ret)
  1069. char *text;
  1070. int *modified, *ret;
  1071. {
  1072.   return (autowrapact (text, modified, ret, '\n'));
  1073. }
  1074.  
  1075.  
  1076. char *
  1077. autowraptab (text, modified, ret)
  1078. char *text;
  1079. int *modified, *ret;
  1080. {
  1081.   return (autowrapact (text, modified, ret, '\t'));
  1082. }
  1083.  
  1084.  
  1085. char *
  1086. autowrapact(text, modified, ret, c)
  1087. char *text;
  1088. int *modified, *ret;
  1089. char c;
  1090. {
  1091.     int i,j;
  1092.     char *ntext;
  1093.     extern csb cmcsb;
  1094.     int wordbeg, spacebeg;
  1095.     int maxcol;
  1096.     *ret = FALSE;
  1097.     *modified = TRUE;
  1098.  
  1099.     maxcol = cmcsb._cmwrp >= 0 ? cmcsb._cmwrp : cmcsb._cmcmx + cmcsb._cmwrp;
  1100.     if (cmcsb._cmwrp != 0 && lastlinelen(text) >= maxcol) {
  1101.     for(i = strlen(text)-1; i >= 0; i--) {
  1102.         if (text[i] == ' ' || text[i] == '\t') {
  1103.         wordbeg = i+1;
  1104.         i--;
  1105.         while((text[i] == ' ' || text[i] == '\t') && i >= 0) i--;
  1106.         spacebeg = i+1;
  1107.         break;
  1108.         }
  1109.         else {
  1110.         if (text[i] == '\n' || i == 0 ||
  1111.             strlen(text) - i >= maxcol) {
  1112.             ntext = (char *)malloc(strlen(text)+2);
  1113.             strcpy(ntext,text);
  1114.             strcat(ntext,"\n");
  1115.             cmechx('\n');
  1116.             return(ntext);
  1117.         }
  1118.         }
  1119.     }
  1120.     if (cmcsb._cmcol != strlen(text) - wordbeg) {
  1121.         cmxera(strlen(text) - wordbeg);
  1122.         cmechx('\n');
  1123.         cmxputs(&text[wordbeg]);
  1124.     }
  1125.     cmechx(c);
  1126.     ntext = (char *)malloc(spacebeg + strlen(text) - spacebeg + 2); 
  1127.     strncpy(ntext,text,spacebeg);
  1128.     ntext[spacebeg] = '\0';
  1129.     strcat(ntext,"\n");
  1130.     strcat(ntext,&text[wordbeg]);
  1131.     ntext[strlen(ntext)+1] = '\0';
  1132.     ntext[strlen(ntext)] = c;
  1133.     return(ntext);
  1134.     }
  1135.     ntext = (char *)malloc(strlen(text) + 2);
  1136.     strcpy(ntext,text);
  1137.     ntext[strlen(ntext)+1] = '\0';
  1138.     ntext[strlen(ntext)] = c;
  1139.     cmechx(c);
  1140.     return(ntext);
  1141. }
  1142.  
  1143. static
  1144. lastlinelen(str) 
  1145. char *str;
  1146. {
  1147.     register int i,j=strlen(str);
  1148.     for(i = j - 1; i >= 0; i--)
  1149.     if (str[i] == '\n')
  1150.         return(tablen(str + i + 1));
  1151.     return(tablen(str));
  1152. }
  1153.  
  1154. /* 
  1155.  * calculate length of a line, even if there are tabs in it.
  1156.  */
  1157.  
  1158. static int
  1159. tablen(str) 
  1160. char *str;
  1161. {
  1162.     register char *cp = str;
  1163.     register int len=0;
  1164.     while(*cp) {
  1165.     if (*cp == '\t') {
  1166.         len = ((len + 8) / 8) * 8;
  1167.     }
  1168.     else
  1169.                 /* ignore JIS escapes */
  1170.         if ((*cp == '\033') && (((*(cp+1) == '$') && (*(cp+2) == 'B')) ||
  1171.                     ((*(cp+1) == '(') && (*(cp+2) == 'J'))))
  1172.           cp += 2;
  1173.         else len++;
  1174.     cp++;
  1175.     }
  1176.     return(len);
  1177. }
  1178.         
  1179.  
  1180. char *
  1181. filteract (text, modified, ret) 
  1182. char *text; 
  1183. int *modified, *ret; 
  1184. {
  1185.   char fname1[40];
  1186.   char fname2[40];
  1187.   char *getenv();
  1188.   char *output;
  1189.   char *cmd, *parse_cmdline();
  1190.   struct stat sbuf;
  1191.   int fd;
  1192.   int len;
  1193.   int total;
  1194.   FILE *pp;
  1195.  
  1196.   if ((cmd = parse_cmdline("Filter: ")) == NULL) {
  1197.     *modified = FALSE;
  1198.     return (text);
  1199.   }
  1200.  
  1201. #if unix
  1202.   strcpy(fname1,"/tmp/cmd1XXXXXX");    /* get a file name */
  1203.   mktemp(fname1);
  1204.   strcpy(fname2,"/tmp/cmd1XXXXXX");    /* get a file name */
  1205.   mktemp(fname2);
  1206. #else
  1207.   strcpy(fname1,mktemp(getenv("TMP"),"cmd1")); /* get a file name */  
  1208.   strcpy(fname2,mktemp(getenv("TMP"),"cmd2")); /* get a file name */  
  1209. #endif
  1210.  
  1211.   cmd = (char *) realloc (cmd, strlen(cmd)+strlen(fname1)+strlen(" < ")+
  1212.               strlen(" > ") + strlen(fname2) +1);
  1213.   strcat (cmd, " < ");
  1214.   strcat (cmd, fname1);
  1215.   strcat (cmd, " > ");
  1216.   strcat (cmd, fname2);
  1217.   if ((pp = fopen (fname1, "w")) == NULL) {
  1218.     cmxputs ("Could not open temp file to execute command\n");
  1219.     *modified = FALSE;
  1220.     free (cmd);
  1221.     return (text);
  1222.   }
  1223.   fprintf (pp, "%s", text);
  1224.   fclose (pp);
  1225.   system(cmd);
  1226.   free (cmd);
  1227.   unlink(fname1);
  1228.  
  1229.   if (stat(fname2, &sbuf) == -1) {
  1230.     cmxputs ("?Temporary file missing\n");
  1231.     return (text);
  1232.   }
  1233.   len = sbuf.st_size;
  1234.   if ((output = (char *) malloc (len+1)) == NULL) {
  1235.     cmxputs ("?Out of memory\n");
  1236.     *modified = FALSE;
  1237.     unlink(fname2);
  1238.     return (text);
  1239.   }
  1240.   if ((fd = open(fname2, O_RDONLY,0)) < 0) {
  1241.     cmxputs("?Could not open temporary file for read.\n"); 
  1242.     perror(fname2);
  1243.     unlink(fname2);
  1244.     free (output);
  1245.     return(text);
  1246.   }
  1247. #ifdef MSDOS
  1248.   if (read(fd,output,len) < 0)
  1249. #else
  1250.   if (read(fd,output,len) != len)
  1251. #endif
  1252.   {
  1253.     cmxputs("?Could not read temporary file.\n"); 
  1254.     perror(fname2);
  1255.     unlink(fname2);
  1256.     free (output);
  1257.     return(text);
  1258.   }
  1259.   close(fd);                /* close the file */
  1260.   output[len] = '\0';
  1261.   unlink (fname2);
  1262.   *modified = TRUE;
  1263.   *ret = FALSE;
  1264.   cmxputs("[Done]\n");
  1265.   return (output);
  1266. }
  1267.  
  1268.  
  1269.  
  1270. /*
  1271.  * paragraph action char handler.
  1272.  * calls user defined handlers, fixes up text, and returns
  1273.  * CMxGO
  1274.  * Must perform context switch on ccmd's buffers, and maintain enough
  1275.  * buffer space for the current buffer
  1276.  */
  1277. break_hndlr(fdblist,brk,deferred) 
  1278. fdb *fdblist;
  1279. char brk;
  1280. int deferred;
  1281. {
  1282.   char *text;
  1283.   char *ntext;
  1284.   int i,len,just_sti = 0;
  1285.   int ret=0,modified=0;
  1286.   jmp_buf rpbuf,erbuf;
  1287.   int cmcol;
  1288.   csb savecsb;
  1289.  
  1290.   if ((text = (char *)malloc(cmcsb._cminc+1)) == NULL) { /* copy text */
  1291.     return(PARAxNM);
  1292.   }
  1293.   for (i = 0; i < cmcsb._cminc; i++) {
  1294.     text[i] = cmcsb._cmptr[i] & 0x7f;
  1295.     if (text[i] == '\0') text[i] |= 0x80;
  1296.   }
  1297.   text[cmcsb._cminc] = '\0';
  1298.  
  1299.   savecsb = cmcsb;
  1300.   cmcsb._cmrty = "";            /* no prompt here. */
  1301.   cmact(oldact);            /* save old state */
  1302.   bcopy(cmrpjb,rpbuf,sizeof(jmp_buf));
  1303.   bcopy(cmerjb,erbuf,sizeof(jmp_buf));
  1304.  
  1305.   just_sti = 1;
  1306.   modified = ret = FALSE;        /* default values */
  1307.   if (paraact1[brk] != NULL) {
  1308.     just_sti = 0;
  1309.     ntext = (*paraact1[brk])(text,&modified,&ret); /* call handler with text */
  1310.   }
  1311.   cmcol = cmcsb._cmcol;            /* need to remember where we were on */
  1312.                     /* the screen.   this is independent */
  1313.                     /* of the rest of the csb state. */
  1314.   cmcsb = savecsb;            /* restore our state */
  1315.   cmcsb._cmcol = cmcol;
  1316.   bcopy(rpbuf,cmrpjb,sizeof(jmp_buf));
  1317.   bcopy(erbuf,cmerjb,sizeof(jmp_buf));
  1318.   cmact(paraact);
  1319.  
  1320.   if (just_sti) {
  1321.     cmsti1(brk,0);
  1322.   }
  1323.   else if (modified) {
  1324.     if (strlen(ntext) >= cmcsb._cmcnt) { /* out of space? */
  1325.                     /* then get some more space */
  1326.       cmcsb._cmcnt = strlen(ntext);
  1327.       if ((cmdbuf = 
  1328.     (int *)cmrealloc(cmdbuf, (cmcsb._cmcnt + BSIZ) * sizeof(int)))==NULL) {
  1329.     return(PARAxNM);
  1330.       }
  1331.       cmcsb._cmcnt += BSIZ;        /* count it */
  1332.       if ((cmcsb._cmwbp = (char *)cmrealloc(cmcsb._cmwbp,cmcsb._cmcnt))==NULL){
  1333.     return(PARAxNM);
  1334.       }
  1335.       cmcsb._cmwbc = cmcsb._cmcnt;
  1336.       cmcsb._cmptr = cmdbuf + (cmcsb._cmptr - cmcsb._cmbfp);
  1337.       cmcsb._cmbfp = cmdbuf;        /* and install it into the CSB */
  1338.     }
  1339.     len = strlen(ntext);
  1340.     for (i = 0; i < len; i++)        /* unquote everything */
  1341.       ntext[i] &= 0x7f;
  1342.     cmcsb._cmcnt += cmcsb._cminc;    /* update CSB counters */
  1343.     cmcsb._cminc = 0;
  1344.     cmsti(ntext,CC_NEC|CC_QUO);        /* insert text into buffer */
  1345.     for (i = 0; i < cmcsb._cminc; i++)
  1346.       cmcsb._cmptr[i] &= ~(CC_HID|CC_NEC); /* mark it unhidden */
  1347.   }
  1348.   eof = ret;
  1349.   free(text);
  1350.   return(CMxGO);
  1351. }
  1352.     
  1353. #ifdef MSDOS
  1354. bcopy(dest,src,count) char *dest,*src; int count; {
  1355.   while(count-- > 0)
  1356.     *dest++ = *src++;
  1357. }
  1358.  
  1359. char *
  1360. mktemp(dir,prefix) char *dir, *prefix; {
  1361.   static int inited=FALSE;
  1362.   int x;
  1363.   char name[100];
  1364.   struct stat sbuf;
  1365.   if (!inited++)
  1366.     srand(1);
  1367.   x = rand();
  1368.   if (dir == NULL)
  1369.     dir = "./";
  1370.   strcpy(name,dir);
  1371.   strcat(name,"\\");
  1372.   strcat(name,prefix);
  1373.   strcat(name,atoi(x));
  1374.   if (stat(name,&sbuf) == 0)
  1375.     return(mktemp(dir,prefix));
  1376.   return(name);
  1377. }
  1378. #endif /*  MSDOS */
  1379.