home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume9 / teco / part01 / te_rdcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-11  |  7.1 KB  |  243 lines

  1. /* TECO for Ultrix   Copyright 1986 Matt Fichtenbaum                        */
  2. /* This program and its components belong to GenRad Inc, Concord MA 01742    */
  3. /* They may be copied if this copyright notice is included                    */
  4.  
  5. /* te_rdcmd.c  read in the command string  12/20/85 */
  6. #include "te_defs.h"
  7.  
  8. int ccount;                /* count of chars read in */
  9.  
  10. int read_cmdstr()
  11.     {
  12.     char c;                /* temporary character */
  13.     int i;                /* temporary */
  14.  
  15.     goto prompt;
  16.  
  17.   restart:                    /* prompt again: new line */
  18.     if (!eisw && !inp_noterm) crlf();        /* if input not from a file */
  19.   prompt:                    /* issue prompt */
  20.     if (!eisw && !inp_noterm)
  21.         {
  22.         reset_ctlo();        /* reset any ^O */
  23.         type_char('*');
  24.         }
  25.     ccount = 0;
  26.     lastc = ' ';
  27.  
  28. reline:                    /* continue reading */
  29.     for (;;)            /* loop to read command string chars */
  30.         {
  31.         if (!eisw && !inp_noterm)        /* if terminal input */
  32.             {
  33.             if ((c = gettty()) == DEL)        /* process rubout */
  34.                 {
  35.                 if (!ccount) goto restart;        /* if at beginning, ignore */
  36.                 --ccount;                        /* decrement char count */
  37.                 backc(&cmdstr);                    /* back up the command-string pointer */
  38.  
  39. /* look at the character just deleted */
  40.                 if (((c = cmdstr.p->ch[cmdstr.c]) < ' ') && (c != ESC))        /* control chars: set c to char erased */
  41.                     {
  42.                     if (c == LF) vt(VT_LINEUP);        /* line up */
  43.  
  44.                     else if ((c == CR) || (c == TAB))
  45.                         {
  46.                         i = find_lasteol();            /* back up to previous line */
  47.                         type_char(CR);                /* erase current line */
  48.                         vt(VT_EEOL);
  49.                         if (i == ccount) type_char('*');        /* if this was line with prompt, retype the prompt */
  50.                         for (; (t_qp.p != cmdstr.p) || (t_qp.c != cmdstr.c); fwdc(&t_qp))
  51.                             type_char(t_qp.p->ch[t_qp.c]);        /* retype line: stop before deleted position */
  52.                         }
  53.  
  54.                     else
  55.                         {
  56.                         vt(VT_BS2);                /* erase ordinary ctrl chars */
  57.                         char_count -= 2;
  58.                         }
  59.                     }
  60.  
  61.                 else
  62.                     {
  63.                     vt(VT_BS1);                /* erase printing chars */
  64.                     char_count--;
  65.                     }
  66.                 lastc = ' ';        /* disable dangerous last chars */
  67.                 continue;
  68.                 }                    /* end 'rubout' processing */
  69.  
  70.             else if (c == CTL(U))            /* process "erase current line" */
  71.                 {
  72.                 type_char(CR);                /* erase line */
  73.                 vt(VT_EEOL);
  74.                 if ((ccount -= find_lasteol()) <= 0) goto prompt;    /* back up to last eol: if beginning, restart */
  75.                 cmdstr.p = t_qp.p;            /* put command pointer back to this point */
  76.                 cmdstr.c = t_qp.c;
  77.                 lastc = ' ';
  78.                 continue;                /* and read it again */
  79.                 }
  80.  
  81.             else            /* not a rubout or ^U */
  82.                 {
  83.                 if (!ccount)        /* if at beginning of line */
  84.                     {
  85.                     if (c == '*')        /* save old command string */
  86.                         {
  87.                         type_char('*');        /* echo character */
  88.                         type_char(c = gettty());    /* read reg spec and echo */
  89.                         i = getqspec(0, c);
  90.                         free_blist(qreg[i].f);        /* return its previous contents */
  91.                         qreg[i].f = cbuf.f;            /* put the old command string in its place */
  92.                         qreg[i].f->b = (struct buffcell *) &qreg[i];
  93.                         qreg[i].z = cbuf.z;
  94.                         cbuf.f = NULL;                /* no old command string */
  95.                         err = 0;                    /* no previous error */
  96.                         goto restart;
  97.                         }
  98.                     else if ((c == '?') && (err))    /* echo previous command string up to error */
  99.                         {
  100.                         type_char('?');            /* echo ? */
  101.                         for (aa.p = cptr.p; aa.p->b->b != NULL; aa.p = aa.p->b);    /* find beginning */
  102.                         for (aa.c = 0; (aa.p != cptr.p) || (aa.c < cptr.c); fwdc(&aa)) type_char(aa.p->ch[aa.c]);
  103.                         type_char('?');                /* a final ? */
  104.                         err = 0;                /* reset error switch */
  105.                         goto restart;
  106.                         }
  107.                     else if ((c == LF) || (c == CTL (H)))    /* line feed, backspace */
  108.                         {
  109.                         dot += lines( (c == LF) ? 1 : -1);    /* pointer up or down one line */
  110.                         window(WIN_LINE);            /* display one line */
  111.                         goto restart;
  112.                         }
  113.  
  114.                     else                    /* first real command on a line */
  115.                         {
  116.                         make_buffer(&cbuf);    /* start a command string if need be */
  117.                         cmdstr.p = cbuf.f;    /* set cmdstr to point to start of command string */
  118.                         cmdstr.c = 0;
  119.                         cbuf.z = 0;            /* no chars in command string now */
  120.                         err = 0;            /* clear last error flag */
  121.                         }
  122.                     }    /* end of "if first char on line" */                
  123.  
  124.  
  125. /* check ^G-something */
  126.  
  127.                 if (lastc == CTL (G))
  128.                     {
  129.                     if (c == CTL(G))
  130.                         {
  131.                         cbuf.z = ccount;    /* save count for possible "save in q-reg" */
  132.                         goto restart;
  133.                         }
  134.                     if ((c == '*') || (c == ' '))
  135.                         {
  136.                         backc(&cmdstr);        /* remove the previous ^G from buffer */
  137.                         --ccount;
  138.                         crlf();
  139.                         retype_cmdstr(c);    /* retype appropriate part of command string */
  140.                         lastc = ' ';
  141.                         continue;
  142.                         }            /* end of ^G* and ^G<sp> processing */
  143.                     }            /* end of "last char was ^G" */
  144.                 }            /* end of "not rubout or ^U */
  145.             }            /* end of "if !eisw" */
  146.  
  147.         else            /* if input from indirect file or redirected std input */
  148.             {
  149.             if (!ccount)    /* first command? */
  150.                 {
  151.                 if (!cbuf.f)    /* start a command string if need be */
  152.                     {
  153.                     cbuf.f = get_bcell();
  154.                     cbuf.f->b = (struct buffcell *) &cbuf;
  155.                     }
  156.                 cmdstr.p = cbuf.f;        /* point cmdstr to start of command string */
  157.                 cbuf.z = cmdstr.c = 0;
  158.                 }
  159.  
  160.             c = (eisw) ? getc(eisw) : gettty() ;    /* get char */
  161.             if (eisw && (c == EOF))            /* if this is end of the indirect command file */
  162.                 {
  163.                 fclose(eisw);                /* close the input file */
  164.                 eisw = 0;                    /* reset the switch */
  165.                 lastc = ' ';
  166.                 continue;                    /* and go read more chars */
  167.                 }
  168.             else
  169.                 {
  170.                 if ((c == LF) && (lastc != CR) && !(ez_val & EZ_CRLF))    /* LF: store implied CR first */
  171.                     {
  172.                     cmdstr.p->ch[cmdstr.c] = CR;
  173.                     ++ccount;
  174.                     fwdcx(&cmdstr);
  175.                     }
  176.                 }
  177.             }            /* end of "if redirected std in or eisw" */
  178.  
  179.  
  180. /* store character in command string */
  181.  
  182.             cmdstr.p->ch[cmdstr.c] = c;        /* store the character */
  183.             ++ccount;                        /* keep count of chars */
  184.             if (!eisw && !inp_noterm) type_char(c);        /* echo the character */
  185.             fwdcx(&cmdstr);                    /* next char pos'n; extend command string if nec */
  186.  
  187.             if ((c == ESC) && (lastc == ESC)) break;    /* stop on 2nd ESC */
  188.             if ((c == CTL (C)) && (lastc == CTL (C))) return(-1);    /* immediate exit */
  189.             lastc = c;                                /* keep track of last char */
  190.             }                    /* end of read-char loop */
  191.  
  192.     cbuf.z = ccount;        /* indicate number of chars in command string */
  193.     if (!eisw && !inp_noterm) crlf();        /* final new-line */
  194.     return(0);
  195.     }                    /* end of read_cmdstr() */
  196.  
  197. /* back up to find most recent CR or LF in entered command string */
  198. /* return number of chars backed up */
  199.  
  200. int find_lasteol()
  201.     {
  202.     int i;
  203.  
  204.     for (i = 0, t_qp.p = cmdstr.p, t_qp.c = cmdstr.c; (backc(&t_qp)) ; i++)    /* look for beg. of line */
  205.         {
  206.         if ((t_qp.p->ch[t_qp.c] == CR) || (t_qp.p->ch[t_qp.c] == LF))
  207.             {
  208.             fwdc(&t_qp);    /* stop short of previous eol */
  209.             break;
  210.             }
  211.         }
  212.     char_count = 0;                /* reset tab count */
  213.     return(i);
  214.     }
  215.  
  216.  
  217.  
  218. /* retype command string: entirely (arg = '*') or most recent line (arg = ' ') */
  219.  
  220. retype_cmdstr(c)
  221.     char c;
  222.     {
  223.     int i;
  224.  
  225.     if (!inp_noterm)    /* if input is really from terminal */
  226.         {
  227.         if (c == ' ')        /* look for beginning of this line */
  228.             i = ccount - find_lasteol();    /* to last eol, and count char's backed up */
  229.         else
  230.             {
  231.             t_qp.p = cbuf.f;    /* retype whole command string */
  232.             i = t_qp.c = 0;
  233.             }
  234.         if (!i) type_char('*');    /* if from beginning, retype prompt */
  235.         for (; i < ccount; i++)        /* type command string from starting point */
  236.             {
  237.             type_char(t_qp.p->ch[t_qp.c]);
  238.             fwdc(&t_qp);
  239.             }
  240.         }
  241.     }
  242.  
  243.