home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / CKPM5X_S.ZIP / CKUCMD.C < prev    next >
C/C++ Source or Header  |  1990-03-03  |  56KB  |  1,585 lines

  1. char *cmdv = "Unix cmd package V3(031), 20 Feb 90";
  2.  
  3. /*  C K U C M D  --  Interactive command package for Unix  */
  4.  
  5. /*
  6.  Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  7.  Columbia University Center for Computing Activities.
  8.  First released January 1985.
  9.  Copyright (C) 1985, 1990, Trustees of Columbia University in the City of New 
  10.  York.  Permission is granted to any individual or institution to use, copy, or
  11.  redistribute this software so long as it is not sold for profit, provided this
  12.  copyright notice is retained. 
  13. */
  14.  
  15. /*
  16.  Modelled after the DECSYSTEM-20 command parser (the COMND JSYS)
  17.  
  18.  Features:
  19.  . parses and verifies keywords, filenames, text strings, numbers, other data
  20.  . displays appropriate menu or help message when user types "?"
  21.  . does keyword and filename completion when user types ESC or TAB
  22.  . accepts any unique abbreviation for a keyword
  23.  . allows keywords to have attributes, like "invisible"
  24.  . can supply defaults for fields omitted by user
  25.  . provides command line editing (character, word, and line deletion)
  26.  . accepts input from keyboard, command files, or redirected stdin
  27.  . allows for full or half duplex operation, character or line input
  28.  . settable prompt, protected from deletion
  29.  
  30.  Functions:
  31.   cmsetp - Set prompt (cmprom is prompt string, cmerrp is error msg prefix)
  32.   cmsavp - Save current prompt
  33.   prompt - Issue prompt 
  34.   cmini  - Clear the command buffer (before parsing a new command)
  35.   cmres  - Reset command buffer pointers (before reparsing)
  36.   cmkey  - Parse a keyword
  37.   cmnum  - Parse a number
  38.   cmifi  - Parse an input file name
  39.   cmofi  - Parse an output file name
  40.   cmdir  - Parse a directory name (UNIX only)
  41.   cmfld  - Parse an arbitrary field
  42.   cmtxt  - Parse a text string
  43.   cmcfm  - Parse command confirmation (end of line)
  44.   stripq - Strip out backslash quotes from a string (no longer used)
  45.  
  46.  Return codes:
  47.   -3: no input provided when required
  48.   -2: input was invalid
  49.   -1: reparse required (user deleted into a preceding field)
  50.    0 or greater: success
  51.   See individual functions for greater detail.
  52.  
  53.  Before using these routines, the caller should #include ckucmd.h, and
  54.  set the program's prompt by calling cmsetp().  If the file parsing
  55.  functions cmifi, cmofi, or cmdir are to be used, this module must be linked
  56.  with a ck?fio file system support module for the appropriate system,
  57.  e.g. ckufio for Unix.  If the caller puts the terminal in
  58.  character wakeup ("cbreak") mode with no echo, then these functions will
  59.  provide line editing -- character, word, and line deletion, as well as
  60.  keyword and filename completion upon ESC and help strings, keyword, or
  61.  file menus upon '?'.  If the caller puts the terminal into character
  62.  wakeup/noecho mode, care should be taken to restore it before exit from
  63.  or interruption of the program.  If the character wakeup mode is not
  64.  set, the system's own line editor may be used.
  65.  
  66.  NOTE: Contrary to expectations, many #ifdef's have been added to this
  67.  module.  Any operation requiring an #ifdef (like clear screen, get character
  68.  from keyboard, erase character from screen, etc) should eventually be turned 
  69.  into a call to a function that is defined in ck?tio.c, but then all the
  70.  ck?tio.c modules would have to be changed...
  71. */
  72.  
  73. /* Includes */
  74.  
  75. #include <stdio.h>                      /* Standard C I/O package */
  76. #include <ctype.h>                      /* Character types */
  77. #include "ckcasc.h"         /* ASCII character symbols */
  78. #include "ckucmd.h"                     /* Command parsing definitions */
  79. #include "ckcdeb.h"                     /* Formats for debug(), etc. */
  80. #ifdef OS2
  81. #define INCL_SUB
  82. #include <os2.h>
  83. #endif /* OS2 */
  84.  
  85. #ifdef OSK
  86. #define cc ccount           /* OS-9/68K compiler bug */
  87. #endif
  88.  
  89. /* Local variables */
  90.  
  91. int psetf = 0,                          /* Flag that prompt has been set */
  92.     cc = 0,                             /* Character count */
  93.     dpx = 0;                            /* Duplex (0 = full) */
  94.  
  95. int hw = HLPLW,                         /* Help line width */
  96.     hc = HLPCW,                         /* Help line column width */
  97.     hh,                                 /* Current help column number */
  98.     hx;                                 /* Current help line position */
  99.  
  100. #define PROML 60                        /* Maximum length for prompt */
  101.  
  102. char cmprom[PROML+1];                   /* Program's prompt */
  103. char *dfprom = "Command? ";             /* Default prompt */
  104.  
  105. char cmerrp[PROML+1];                   /* Program's error message prefix */
  106.  
  107. int cmflgs;                             /* Command flags */
  108. int cmflgsav;               /* A saved version of them */
  109.  
  110. char cmdbuf[CMDBL+4];                   /* Command buffer */
  111. char savbuf[CMDBL+4];           /* Buffer to save copy of command */
  112. char hlpbuf[HLPBL+4];                   /* Help string buffer */
  113. char atmbuf[ATMBL+4];                   /* Atom buffer */
  114. char atxbuf[CMDBL+4];           /* For expanding the atom buffer */
  115. char atybuf[ATMBL+4];                   /* For copying atom buffer */
  116. char filbuf[ATMBL+4];                   /* File name buffer */
  117.  
  118. /* Command buffer pointers */
  119.  
  120. static char *bp,                        /* Current command buffer position */
  121.     *pp,                                /* Start of current field */
  122.     *np;                                /* Start of next field */
  123.  
  124. static int ungw;            /* For ungetting words */
  125.  
  126. long zchki();                           /* From ck?fio.c. */
  127.  
  128.  
  129. /*  C M S E T P  --  Set the program prompt.  */
  130.  
  131. cmsetp(s) char *s; {
  132.     char *sx, *sy, *strncpy();
  133.     psetf = 1;                          /* Flag that prompt has been set. */
  134.     strncpy(cmprom,s,PROML - 1);    /* Copy the string. */
  135.     cmprom[PROML] = NUL;                /* Ensure null terminator. */
  136.     sx = cmprom; sy = cmerrp;           /* Also use as error message prefix. */
  137.     while (*sy++ = *sx++) ;             /* Copy. */
  138.     sy -= 2; if (*sy == '>') *sy = NUL; /* Delete any final '>'. */
  139. }
  140. /*  C M S A V P  --  Save a copy of the current prompt.  */
  141.  
  142. cmsavp(s,n) int n; char s[]; {
  143.     extern char *strncpy();                                     /* +1   */
  144.     strncpy(s,cmprom,n-1);
  145.     s[n] = NUL;
  146. }
  147.  
  148. /*  P R O M P T  --  Issue the program prompt.  */
  149.  
  150. prompt() {
  151.     if (psetf == 0) cmsetp(dfprom);     /* If no prompt set, set default. */
  152. #ifdef OSK
  153.     fputs(cmprom, stdout);
  154. #else
  155.     printf("\r%s",cmprom);              /* Print the prompt. */
  156. #endif
  157. }
  158.  
  159.  
  160. pushcmd() {             /* For use with IF command. */
  161.     strcpy(savbuf,np);          /* Save the dependent clause,  */
  162.     cmres();                /* and clear the command buffer. */
  163. }
  164.  
  165. popcmd() {
  166.     strcpy(cmdbuf,savbuf);      /* Put back the saved material */
  167.     *savbuf = '\0';         /* and clear the save buffer */
  168.     cmres();
  169. }
  170.  
  171. /*  C M R E S  --  Reset pointers to beginning of command buffer.  */
  172.  
  173. cmres() {  
  174.     cc = 0;                             /* Reset character counter. */
  175.     pp = np = bp = cmdbuf;              /* Point to command buffer. */
  176.     cmflgs = -5;                        /* Parse not yet started. */
  177.     ungw = 0;               /* Haven't ungotten a word. */
  178. }
  179.  
  180.  
  181. /*  C M I N I  --  Clear the command and atom buffers, reset pointers.  */
  182.  
  183. /*
  184. The argument specifies who is to echo the user's typein --
  185.   1 means the cmd package echoes
  186.   0 somebody else (system, front end, terminal) echoes
  187. */
  188. cmini(d) int d; {
  189.     for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  190.     *atmbuf = NUL;
  191.     dpx = d;
  192.     cmres();
  193. }
  194.  
  195. stripq(s) char *s; {                    /* Function to strip '\' quotes */
  196.     char *t;
  197.     while (*s) {
  198.         if (*s == CMDQ) {
  199.             for (t = s; *t != '\0'; t++) *t = *(t+1);
  200.         }
  201.         s++;
  202.     }
  203. }
  204.  
  205. /* Convert tabs to spaces */
  206. untab(s) char *s; {
  207.     while (*s++) 
  208.       if (*s == HT) *s = SP;
  209. }
  210.  
  211. /*  C M N U M  --  Parse a number in the indicated radix  */
  212.  
  213. /* 
  214.  For now, the only radix allows in unquoted numbers is 10.
  215.  Parses unquoted numeric strings in base 10.
  216.  Parses backslash-quoted numbers in the radix indicated by the quote:
  217.    \nnn = \dnnn = decimal, \onnn = octal, \xnn = hexidecimal.
  218.  If these fail, then if a preprocessing function is supplied, that is applied 
  219.  and then a second attempt is made to parse an unquoted decimal string. 
  220.  
  221.  Returns
  222.    -3 if no input present when required,
  223.    -2 if user typed an illegal number,
  224.    -1 if reparse needed,
  225.     0 otherwise, with argument n set to the number that was parsed
  226. */
  227. cmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; int (*f)(); {
  228.     int x; char *s, *zp, *zq;
  229.  
  230.     if (radix != 10) {                  /* Just do base 10 for now */
  231.         printf("cmnum: illegal radix - %d\n",radix);
  232.         return(-1);
  233.     }
  234.     x = cmfld(xhlp,xdef,&s,NULL);
  235.     debug(F101,"cmnum: cmfld","",x);
  236.     if (x < 0) return(x);       /* Parse a field */
  237.     zp = atmbuf;
  238.  
  239.     if (chknum(zp)) {           /* Check for decimal number */
  240.         *n = atoi(zp);          /* Got one, we're done. */
  241.         return(0);
  242.     } else if ((x = xxesc(&zp)) > -1) { /* Check for backslash escape */
  243.     *n = x;
  244.     return(*zp ? -2 : 0);
  245.     } else if (*f) {            /* If conversion function given */
  246.         zp = atmbuf;            /* Try that */
  247.     zq = atxbuf;
  248.     x = (*f)(zp,&zq);       /* Convert */
  249.     zp = atxbuf;
  250.     }
  251.     if (chknum(zp)) {           /* Check again for decimal number */
  252.         *n = atoi(zp);          /* Got one, we're done. */
  253.         return(0);
  254.     } else {                /* Not numeric */
  255.     printf("\n?not a number - %s\n",s);
  256.     return(-2);     
  257.     }
  258. }
  259.  
  260. /*  C M O F I  --  Parse the name of an output file  */
  261.  
  262. /*
  263.  Depends on the external function zchko(); if zchko() not available, use
  264.  cmfld() to parse output file names.
  265.  
  266.  Returns
  267.    -3 if no input present when required,
  268.    -2 if permission would be denied to create the file,
  269.    -1 if reparse needed,
  270.     0 or 1 otherwise, with xp pointing to name.
  271. */
  272. cmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; int (*f)(); {
  273.     int x; char *s, *zq;
  274. #ifdef DTILDE
  275.     char *tilde_expand(), *dirp;
  276. #endif 
  277.  
  278.     if (*xhlp == NUL) xhlp = "Output file";
  279.     *xp = "";
  280.  
  281.     if ((x = cmfld(xhlp,xdef,&s,NULL)) < 0) return(x);
  282.  
  283.     if (*f) {               /* If a conversion function is given */
  284.     zq = atxbuf;
  285.     x = (*f)(s,&zq);
  286.     s = atxbuf;
  287.     }
  288.  
  289. #ifdef DTILDE
  290.     dirp = tilde_expand(s);     /* Expand tilde, if any, */
  291.     if (*dirp != '\0') setatm(dirp);    /* right in the atom buffer. */
  292. #endif
  293.  
  294.     if (chkwld(s)) {
  295.         printf("\n?Wildcards not allowed - %s\n",s);
  296.         return(-2);
  297.     }
  298.     if (zchko(s) < 0) {
  299.         printf("\n?Write permission denied - %s\n",s);
  300.         return(-2);
  301.     } else {
  302.         *xp = s;
  303.         return(x);
  304.     }
  305. }
  306.  
  307.  
  308. /*  C M I F I  --  Parse the name of an existing file  */
  309.  
  310. /*
  311.  This function depends on the external functions:
  312.    zchki()  - Check if input file exists and is readable.
  313.    zxpand() - Expand a wild file specification into a list.
  314.    znext()  - Return next file name from list.
  315.  If these functions aren't available, then use cmfld() to parse filenames.
  316. */
  317. /*
  318.  Returns
  319.    -4 EOF
  320.    -3 if no input present when required,
  321.    -2 if file does not exist or is not readable,
  322.    -1 if reparse needed,
  323.     0 or 1 otherwise, with:
  324.         xp pointing to name,
  325.         wild = 1 if name contains '*' or '?', 0 otherwise.
  326. */
  327. cmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; int (*f)(); {
  328.     int i, x, xc; long y; char *sp, *zq;
  329. #ifdef DTILDE
  330.     char *tilde_expand(), *dirp;
  331. #endif
  332.  
  333.     cc = xc = 0;                        /* Initialize counts & pointers */
  334.     *xp = "";
  335.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  336.         x = gtword();                   /* No, get a word */
  337.     } else {
  338.         cc = setatm(xdef);              /* If so, use default, if any. */
  339.     }
  340.  
  341.     *xp = atmbuf;                       /* Point to result. */
  342.     *wild = chkwld(*xp);
  343.  
  344.     while (1) {
  345.         xc += cc;                       /* Count the characters. */
  346.         debug(F111,"cmifi: gtword",atmbuf,xc);
  347.         switch (x) {
  348.             case -4:                    /* EOF */
  349.             case -2:                    /* Out of space. */
  350.             case -1:                    /* Reparse needed */
  351.                 return(x);
  352.             case 0:                     /* SP or NL */
  353.             case 1:
  354.                 if (xc == 0) *xp = xdef;     /* If no input, return default. */
  355.                 else *xp = atmbuf;
  356.                 if (**xp == NUL) return(-3); /* If field empty, return -3. */
  357.  
  358.         if (*f) {       /* If a conversion function is given */
  359.             zq = atxbuf;    /* ... */
  360.             x = (*f)(*xp,&zq);  /* run it. */
  361.             *xp = atxbuf;
  362.         }
  363. #ifdef DTILDE
  364.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  365.         if (*dirp != '\0') setatm(dirp); /* right in atom buffer. */
  366. #endif
  367.                 /* If filespec is wild, see if there are any matches */
  368.  
  369.                 *wild = chkwld(*xp);
  370.                 debug(F101," *wild","",*wild);
  371.                 if (*wild != 0) {
  372.                     y = zxpand(*xp);
  373.                     if (y == 0) {
  374.                         printf("\n?No files match - %s\n",*xp);
  375.                         return(-2);
  376.                     } else if (y < 0) {
  377.                         printf("\n?Too many files match - %s\n",*xp);
  378.                         return(-2);
  379.                     } else return(x);
  380.                 }
  381.  
  382.                 /* If not wild, see if it exists and is readable. */
  383.  
  384.                 y = zchki(*xp);
  385.  
  386.                 if (y == -3) {
  387.                     printf("\n?Read permission denied - %s\n",*xp);
  388.                     return(-2);
  389.                 } else if (y == -2) {
  390.                     printf("\n?File not readable - %s\n",*xp);
  391.                     return(-2);
  392.                 } else if (y < 0) {
  393.                     printf("\n?File not found - %s\n",*xp);
  394.                     return(-2);
  395.                 }
  396.                 return(x);
  397.  
  398.             case 2:                     /* ESC */
  399.                 if (xc == 0) {
  400.                     if (*xdef != '\0') {
  401.                         printf("%s ",xdef); /* If at beginning of field, */
  402.                         addbuf(xdef);   /* supply default. */
  403.                         cc = setatm(xdef);
  404.                     } else {            /* No default */
  405.                         putchar(BEL);
  406.                     }
  407.                     break;
  408.                 } 
  409.         if (*f) {       /* If a conversion function is given */
  410.             zq = atxbuf;    /* ... */
  411.             x = (*f)(*xp,&zq);  /* run it. */
  412.             *xp = atxbuf;
  413.         }
  414. #ifdef DTILDE
  415.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  416.         if (*dirp != '\0') setatm(dirp); /* in the atom buffer. */
  417. #endif
  418.                 if (*wild = chkwld(*xp)) {  /* No completion if wild */
  419.                     putchar(BEL);
  420.                     break;
  421.                 }
  422.                 sp = atmbuf + cc;
  423.                 *sp++ = '*';
  424.                 *sp-- = '\0';
  425.                 y = zxpand(atmbuf);     /* Add * and expand list. */
  426.                 *sp = '\0';             /* Remove *. */
  427.  
  428.                 if (y == 0) {
  429.                     printf("\n?No files match - %s\n",atmbuf);
  430.                     return(-2);
  431.                 } else if (y < 0) {
  432.                     printf("\n?Too many files match - %s\n",atmbuf);
  433.                     return(-2);
  434.                 } else if (y > 1) {     /* Not unique, just beep. */
  435.                     putchar(BEL);
  436.                 } else {                /* Unique, complete it.  */
  437.                     znext(filbuf);      /* Get whole name of file. */
  438.                     sp = filbuf + cc;   /* Point past what user typed. */
  439.                     printf("%s ",sp);   /* Complete the name. */
  440.                     addbuf(sp);         /* Add the characters to cmdbuf. */
  441.                     setatm(pp);         /* And to atmbuf. */
  442.                     *xp = atmbuf;       /* Return pointer to atmbuf. */
  443.                     return(cmflgs = 0);
  444.                 }
  445.                 break;
  446.  
  447.             case 3:                     /* Question mark */
  448.                 if (*xhlp == NUL)
  449.                     printf(" Input file specification");
  450.                 else
  451.                     printf(" %s",xhlp);
  452.                 if (xc > 0) {
  453.             if (*f) {       /* If a conversion function is given */
  454.             zq = atxbuf;    /* ... */
  455.             x = (*f)(*xp,&zq); /* run it. */
  456.             *xp = atxbuf;
  457.             }
  458. #ifdef DTILDE
  459.             dirp = tilde_expand(*xp);    /* Expand tilde, if any */
  460.             if (*dirp != '\0') setatm(dirp);
  461. #endif
  462.                     sp = atmbuf + cc;   /* Insert "*" at end */
  463. #ifdef datageneral
  464.                     *sp++ = '+';        /* Insert +, the DG wild card */
  465. #else
  466.                     *sp++ = '*';
  467. #endif
  468.                     *sp-- = '\0';
  469.                     y = zxpand(atmbuf);
  470.                     *sp = '\0';
  471.                     if (y == 0) {                   
  472.                         printf("\n?No files match - %s\n",atmbuf);
  473.                         return(-2);
  474.                     } else if (y < 0) {
  475.                         printf("\n?Too many file match - %s\n",atmbuf);
  476.                         return(-2);
  477.                     } else {
  478.                         printf(", one of the following:\n");
  479.                         clrhlp();
  480.                         for (i = 0; i < y; i++) {
  481.                             znext(filbuf);
  482.                             addhlp(filbuf);
  483.                         }
  484.                         dmphlp();
  485.                     }
  486.                 } else printf("\n");
  487.                 printf("%s%s",cmprom,cmdbuf);
  488.                 break;
  489.         }
  490.     x = gtword();
  491.     }
  492. }
  493.  
  494. /*  C M D I R  --  Parse a directory specification  */
  495.  
  496. /*
  497.  This function depends on the external functions:
  498.    zchki()  - Check if input file exists and is readable.
  499.  If these functions aren't available, then use cmfld() to parse dir names.
  500.  Note: this function quickly cobbled together, mainly by deleting lots of
  501.  lines from cmifi().  It seems to work, but various services are missing,
  502.  like completion, lists of matching directories on "?", etc.
  503. */
  504. /*
  505.  Returns
  506.    -4 EOF
  507.    -3 if no input present when required,
  508.    -2 if out of space or other internal error,
  509.    -1 if reparse needed,
  510.     0 or 1, with xp pointing to name, if directory specified,
  511.     2 if a wildcard was included.
  512. */
  513. cmdir(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; int (*f)(); {
  514.     int x, xc; long y; char *zq;
  515. #ifdef DTILDE
  516.     char *tilde_expand(), *dirp;
  517. #endif 
  518.  
  519.     cc = xc = 0;                        /* Initialize counts & pointers */
  520.     *xp = "";
  521.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  522.         x = gtword();                   /* No, get a word */
  523.     } else {
  524.         cc = setatm(xdef);              /* If so, use default, if any. */
  525.     }
  526.     *xp = atmbuf;                       /* Point to result. */
  527.     while (1) {
  528.         xc += cc;                       /* Count the characters. */
  529.         debug(F111,"cmifi: gtword",atmbuf,xc);
  530.         switch (x) {
  531.             case -4:                    /* EOF */
  532.             case -2:                    /* Out of space. */
  533.             case -1:                    /* Reparse needed */
  534.                 return(x);
  535.             case 0:                     /* SP or NL */
  536.             case 1:
  537.                 if (xc == 0) *xp = xdef;     /* If no input, return default. */
  538.                 if (**xp == NUL) return(-3); /* If field empty, return -3. */
  539.         if (*f) {       /* If a conversion function is given */
  540.             zq = atxbuf;    /* ... */
  541.             x = (*f)(*xp,&zq);  /* run it. */
  542.             *xp = atxbuf;
  543.             cc = strlen(atxbuf);
  544.         }
  545. #ifdef DTILDE
  546.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  547.         if (*dirp != '\0') setatm(dirp); /* in the atom buffer. */
  548.         *xp = atmbuf;
  549. #endif
  550.         if (chkwld(*xp) != 0)   /* If wildcard included... */
  551.           return(2);
  552.  
  553.                 /* If not wild, see if it exists and is readable. */
  554.  
  555.                 y = zchki(*xp);
  556.  
  557.                 if (y == -3) {
  558.                     printf("\n?Read permission denied - %s\n",*xp);
  559.                     return(-2);
  560.         } else if (y == -2) {   /* Probably a directory... */
  561.             return(x);
  562.                 } else if (y < 0) {
  563.                     printf("\n?Not found - %s\n",*xp);
  564.                     return(-2);
  565.                 }
  566.                 return(x);
  567.             case 2:                     /* ESC */
  568.         putchar(BEL);
  569.         break;
  570.  
  571.             case 3:                     /* Question mark */
  572.                 if (*xhlp == NUL)
  573.                     printf(" Directory name");
  574.                 else
  575.                     printf(" %s",xhlp);
  576.                 printf("\n%s%s",cmprom,cmdbuf);
  577.                 break;
  578.         }
  579.     x = gtword();
  580.     }
  581. }
  582.  
  583. /*  C H K W L D  --  Check for wildcard characters '*' or '?'  */
  584.  
  585. chkwld(s) char *s; {
  586.  
  587.     for ( ; *s != '\0'; s++) {
  588. #ifdef datageneral
  589.         /* Valid DG wild cards are '-', '+', '#', or '*' */
  590.         if ( (*s <= '-') && (*s >= '#') &&
  591.             ((*s == '-') || (*s == '+') || (*s == '#') || (*s == '*')) )
  592. #else
  593.         if ((*s == '*') || (*s == '?'))
  594. #endif
  595.             return(1);
  596.     }
  597.     return(0);
  598. }
  599.  
  600.  
  601. /*  C M F L D  --  Parse an arbitrary field  */
  602. /*
  603.  Returns
  604.    -3 if no input present when required,
  605.    -2 if field too big for buffer,
  606.    -1 if reparse needed,
  607.     0 otherwise, xp pointing to string result.
  608. */
  609. cmfld(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; int (*f)(); {
  610.     int x, xc;
  611.     char *zq;
  612.  
  613. #ifdef OS2  /* Needed for COMMENT but below */
  614.    char * xx;
  615.    int i;
  616. #endif
  617.  
  618.     cc = xc = 0;                        /* Initialize counts & pointers */
  619.     *xp = "";
  620.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  621.         x = gtword();                   /* No, get a word */
  622.     } else {
  623.         cc = setatm(xdef);              /* If so, use default, if any. */
  624.     }
  625.     *xp = atmbuf;                       /* Point to result. */
  626.  
  627.     while (1) {
  628.         xc += cc;                       /* Count the characters. */
  629.         debug(F111,"cmfld: gtword",atmbuf,xc);
  630.         debug(F101,"cmfld x","",x);
  631.         switch (x) {
  632.             case -4:                    /* EOF */
  633.             case -2:                    /* Out of space. */
  634.             case -1:                    /* Reparse needed */
  635.                 return(x);
  636.             case 0:                     /* SP or NL */
  637.             case 1:
  638.                 if (xc == 0)        /* If no input, return default. */
  639.           cc = setatm(xdef);
  640.         *xp = atmbuf;
  641.         if (*f) {       /* If a conversion function is given */
  642.             zq = atxbuf;    /* ... */
  643.             (*f)(*xp,&zq);  /* run it. */
  644.             cc = setatm(atxbuf);
  645.             *xp = atmbuf;
  646.         }
  647.                 if (**xp == NUL) x = -3;    /* If field empty, return -3. */
  648. #ifdef COMMENT
  649. /* The following is apparently not necessary. */
  650. /* Remove it if nothing is broken, esp. TAKE file with trailing comments */
  651.         xx = *xp;
  652.         debug(F111,"cmfld before trim",*xp,x);
  653.         for (i = strlen(xx) - 1; i > 0; i--)
  654.           if (xx[i] != SP)  /* Trim trailing blanks */
  655.             break;
  656.           else
  657.             xx[i] = NUL;
  658.         debug(F111,"cmfld returns",*xp,x); 
  659. #endif /* COMMENT */
  660.         debug(F101,"cmfld: returns","",x);
  661.                 return(x);
  662.             case 2:                     /* ESC */
  663.                 if (xc == 0) {
  664.                     printf("%s ",xdef); /* If at beginning of field, */
  665.                     addbuf(xdef);       /* supply default. */
  666.                     cc = setatm(xdef);  /* Return as if whole field */
  667.                     return(0);          /* typed, followed by space. */
  668.                 } else {
  669.                     putchar(BEL);       /* Beep if already into field. */
  670.                 }                   
  671.                 break;
  672.             case 3:                     /* Question mark */
  673.                 if (*xhlp == NUL)
  674.                     printf(" Please complete this field");
  675.                 else
  676.                     printf(" %s",xhlp);
  677.                 printf("\n%s%s",cmprom,cmdbuf);
  678.                 break;
  679.         }
  680.     x = gtword();
  681.     }
  682. }
  683.  
  684.  
  685. /*  C M T X T  --  Get a text string, including confirmation  */
  686.  
  687. /*
  688.   Print help message 'xhlp' if ? typed, supply default 'xdef' if null
  689.   string typed.  Returns
  690.  
  691.    -1 if reparse needed or buffer overflows.
  692.     1 otherwise.
  693.  
  694.   with cmflgs set to return code, and xp pointing to result string.
  695. */
  696.  
  697. cmtxt(xhlp,xdef,xp,f) char *xhlp; char *xdef; char **xp; int (*f)(); {
  698.  
  699.     int x, i;
  700.     char *xx, *zq;
  701.     static int xc;
  702.  
  703.     debug(F101,"cmtxt, cmflgs","",cmflgs);
  704.     cc = 0;                             /* Start atmbuf counter off at 0 */
  705.     if (cmflgs == -1) {                 /* If reparsing, */
  706.         xc = strlen(*xp);               /* get back the total text length, */
  707.     } else {                            /* otherwise, */
  708.         *xp = "";                       /* start fresh. */
  709.         xc = 0;
  710.     }
  711.     *atmbuf = NUL;                      /* And empty the atom buffer. */
  712.     if ((x = cmflgs) != 1) {
  713.         x = gtword();                   /* Get first word. */
  714.         *xp = pp;                       /* Save pointer to it. */
  715.     }
  716.     while (1) {
  717.         xc += cc;                       /* Char count for all words. */
  718.         debug(F111,"cmtxt: gtword",atmbuf,xc);
  719.         debug(F101," x","",x);
  720.         switch (x) {
  721.             case -4:                    /* EOF */
  722.             case -2:                    /* Overflow */
  723.             case -1:                    /* Deletion */
  724.                 return(x);
  725.             case 0:                     /* Space */
  726.                 xc++;                   /* Just count it */
  727.                 break;
  728.             case 1:                     /* CR or LF */
  729.                 if (xc == 0) *xp = xdef;
  730.         if (*f) {       /* If a conversion function is given */
  731.             zq = atxbuf;    /* ... */
  732.             x = (*f)(*xp,&zq);  /* run it. */
  733.             *xp = atxbuf;
  734.             cc = strlen(atxbuf);
  735.         }
  736.         xx = *xp;
  737.         for (i = strlen(xx) - 1; i > 0; i--)
  738.           if (xx[i] != SP)  /* Trim trailing blanks */
  739.             break;
  740.           else
  741.             xx[i] = NUL;
  742.                 return(x);
  743.             case 2:                     /* ESC */
  744.                 if (xc == 0) {
  745.                     printf("%s ",xdef);
  746.                     cc = addbuf(xdef);
  747.                 } else {
  748.                     putchar(BEL);
  749.                 }
  750.                 break;
  751.             case 3:                     /* Question Mark */
  752.                 if (*xhlp == NUL)
  753.                     printf(" Text string");
  754.                 else
  755.                     printf(" %s",xhlp);
  756.                 printf("\n%s%s",cmprom,cmdbuf);
  757.                 break;
  758.             default:
  759.                 printf("\n?Unexpected return code from gtword() - %d\n",x);
  760.                 return(-2);
  761.         }
  762.         x = gtword();
  763.     }
  764. }
  765.  
  766.  
  767. /*  C M K E Y  --  Parse a keyword  */
  768.  
  769. /*
  770.  Call with:
  771.    table    --  keyword table, in 'struct keytab' format;
  772.    n        --  number of entries in table;
  773.    xhlp     --  pointer to help string;
  774.    xdef     --  pointer to default keyword;
  775.  
  776.  Returns:
  777.    -3       --  no input supplied and no default available
  778.    -2       --  input doesn't uniquely match a keyword in the table
  779.    -1       --  user deleted too much, command reparse required
  780.     n >= 0  --  value associated with keyword
  781. */
  782.  
  783. cmkey(table,n,xhlp,xdef,f)
  784. /* cmkey */  struct keytab table[]; int n; char *xhlp, *xdef; int (*f)(); {
  785.     return(cmkey2(table,n,xhlp,xdef,"",f));
  786. }
  787.  
  788. cmkey2(table,n,xhlp,xdef,tok,f)
  789.     struct keytab table[]; int n; char *xhlp, *xdef; char *tok; int (*f)(); {
  790.  
  791.     int i, tl, y, z, zz, xc;
  792.     char *xp, *zq;
  793.  
  794.     tl = strlen(tok);
  795.     xc = cc = 0;                        /* Clear character counters. */
  796.  
  797.     if ((zz = cmflgs) == 1)             /* Command already entered? */
  798.         setatm(xdef);           /* Yes, copy default into atom buf */
  799.     else zz = gtword();         /* Otherwise get a command word */
  800.  
  801. debug(F101,"cmkey: table length","",n);
  802. debug(F101," cmflgs","",cmflgs);
  803. debug(F101," zz","",zz);
  804. while (1) {
  805.     xc += cc;
  806.     debug(F111,"cmkey: gtword",atmbuf,xc);
  807.  
  808.     switch(zz) {
  809.         case -4:                        /* EOF */
  810.         case -2:                        /* Buffer overflow */
  811.         case -1:                        /* Or user did some deleting. */
  812.             return(zz);
  813.  
  814.         case 0:                         /* User terminated word with space */
  815.         case 1:                         /* or newline */
  816.             if (cc == 0) setatm(xdef);  /* Supply default if user typed nada */
  817.         if (*f) {           /* If a conversion function is given */
  818.         zq = atxbuf;        /* apply it */
  819.         (*f)(atmbuf,&zq);
  820.         cc = setatm(atxbuf);
  821.         }
  822.             y = lookup(table,atmbuf,n,&z); /* Look up the word in the table */
  823.             switch (y) {
  824.                 case -2:        /* Ambiguous */
  825.                     printf("\n?Ambiguous - %s\n",atmbuf);
  826.                     return(cmflgs = -2);
  827.                 case -1:        /* Not found at all */
  828.             if (tl) {
  829.             for (i = 0; i < tl; i++) /* Check for token */
  830.               if (tok[i] == *atmbuf) { /* Got one */
  831.                   ungword();  /* Put back the following word */
  832.                   return(-5); /* Special return code for token */
  833.               }
  834.             }
  835.             /* Kludge alert... only print error if */
  836.             /* we were called as cmkey2, but not cmkey... */
  837.             /* This doesn't seem to always work. */
  838.             if (tl == 0) {
  839.             /* printf("\n?Invalid - %s\n",atmbuf); /* cmkey */
  840.             return(cmflgs = -2);
  841.             } else { 
  842.             if (cmflgs == 1) return(cmflgs = -6); /* cmkey2 */
  843.             else return(cmflgs = -2);
  844.             /* The -6 code is to let caller try another table */
  845.             }
  846.                 default:
  847.                     break;
  848.         }
  849.             return(y);
  850.  
  851.         case 2:                         /* User terminated word with ESC */
  852.             if (cc == 0) {
  853.                 if (*xdef != NUL) {     /* Nothing in atmbuf */
  854.                     printf("%s ",xdef); /* Supply default if any */
  855.                     addbuf(xdef);
  856.                     cc = setatm(xdef);
  857.                     debug(F111,"cmkey: default",atmbuf,cc);
  858.                 } else {
  859.                     putchar(BEL);       /* No default, just beep */
  860.                     break;
  861.                 }
  862.             }
  863.         if (*f) {           /* If a conversion function is given */
  864.         zq = atxbuf;        /* apply it */
  865.         (*f)(atmbuf,&zq);
  866.         cc = setatm(atxbuf);
  867.         }
  868.             y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */
  869.             debug(F111,"cmkey: esc",atmbuf,y);
  870.             if (y == -2) {
  871.                 putchar(BEL);
  872.                 break;
  873.             }
  874.             if (y == -1) {
  875.                 if (tl == 0) printf("\n?Invalid - %s\n",atmbuf);
  876.                 return(cmflgs = -2);
  877.             }
  878.             xp = table[z].kwd + cc;
  879.             printf("%s ",xp);
  880.             addbuf(xp);
  881.             debug(F110,"cmkey: addbuf",cmdbuf,0);
  882.             return(y);
  883.  
  884.         case 3:                         /* User terminated word with "?" */
  885.         if (*f) {           /* If a conversion function is given */
  886.         zq = atxbuf;
  887.         (*f)(atmbuf,&zq);
  888.         cc = setatm(atxbuf);
  889.         }
  890.             y = lookup(table,atmbuf,n,&z);
  891.             if (y > -1) {
  892.                 printf(" %s\n%s%s",table[z].kwd,cmprom,cmdbuf);
  893.                 break;
  894.             } else if (y == -1) {
  895.                 if (tl == 0) printf("\n?Invalid\n");
  896.                 return(cmflgs = -2);
  897.             }
  898.             if (*xhlp == NUL)
  899.                 printf(" One of the following:\n");
  900.             else
  901.                 printf(" %s, one of the following:\n",xhlp);
  902.  
  903.             clrhlp();
  904.             for (i = 0; i < n; i++) {   
  905.                 if (!strncmp(table[i].kwd,atmbuf,cc)
  906.                         && !test(table[i].flgs,CM_INV))
  907.                     addhlp(table[i].kwd);
  908.             }
  909.             dmphlp();
  910.         if (*atmbuf == NUL) {
  911.         if (tl == 1)
  912.           printf("or the token '%c'\n",*tok);
  913.         else if (tl > 1) printf("or one of the tokens '%s'\n",tok);
  914.         }
  915.             printf("%s%s", cmprom, cmdbuf);
  916.             break;
  917.  
  918.         default:            
  919.             printf("\n%d - Unexpected return code from gtword\n",zz);
  920.             return(cmflgs = -2);
  921.         }
  922.         zz = gtword();
  923.     }
  924. }
  925.  
  926. chktok(tlist) char *tlist; {
  927.     return(*atmbuf);
  928. }
  929.  
  930. /*  C M C F M  --  Parse command confirmation (end of line)  */
  931.  
  932. /*
  933.  Returns
  934.    -2: User typed anything but whitespace or newline
  935.    -1: Reparse needed
  936.     0: Confirmation was received
  937. */
  938.  
  939. cmcfm() {
  940.     int x, xc;
  941.  
  942.     debug(F101,"cmcfm: cmflgs","",cmflgs);
  943.  
  944.     xc = cc = 0;
  945.     if (cmflgs == 1) return(0);
  946.  
  947.     while (1) {
  948.         x = gtword();
  949.         xc += cc;
  950.         debug(F111,"cmcfm: gtword",atmbuf,xc);
  951.         switch (x) {
  952.             case -4:                    /* EOF */
  953.             case -2:
  954.             case -1:
  955.                 return(x);
  956.  
  957.             case 0:                     /* Space */
  958.                 continue;
  959.             case 1:                     /* End of line */
  960.                 if (xc > 0) {
  961.                     printf("?Not confirmed - %s\n",atmbuf);
  962.                     return(-2);
  963.                 } else return(0);                   
  964.             case 2:         /* ESC */
  965.                 putchar(BEL);
  966.                 continue;
  967.  
  968.             case 3:         /* Question mark */
  969.                 if (xc > 0) {
  970.                     printf("\n?Not confirmed - %s\n",atmbuf);
  971.                     return(-2);
  972.                 }
  973.                 printf("\n Type a carriage return to confirm the command\n");
  974.                 printf("%s%s",cmprom,cmdbuf);
  975.                 continue;
  976.         }
  977.     }
  978. }
  979.  
  980. /* Keyword help routines */
  981.  
  982.  
  983. /*  C L R H L P -- Initialize/Clear the help line buffer  */
  984.  
  985. clrhlp() {                              /* Clear the help buffer */
  986.     hlpbuf[0] = NUL;
  987.     hh = hx = 0;
  988. }
  989.  
  990.  
  991. /*  A D D H L P  --  Add a string to the help line buffer  */
  992.  
  993. addhlp(s) char *s; {                    /* Add a word to the help buffer */
  994.     int j;
  995.  
  996.     hh++;                               /* Count this column */
  997.  
  998.     for (j = 0; (j < hc) && (*s != NUL); j++) { /* Fill the column */
  999.         hlpbuf[hx++] = *s++;
  1000.     }
  1001.     if (*s != NUL)                      /* Still some chars left in string? */
  1002.         hlpbuf[hx-1] = '+';             /* Mark as too long for column. */
  1003.  
  1004.     if (hh < (hw / hc)) {               /* Pad col with spaces if necessary */
  1005.         for (; j < hc; j++) {
  1006.             hlpbuf[hx++] = SP;
  1007.         }
  1008.     } else {                            /* If last column, */
  1009.         hlpbuf[hx++] = NUL;             /* no spaces. */
  1010.         dmphlp();                       /* Print it. */
  1011.         return;
  1012.     }
  1013. }
  1014.  
  1015.  
  1016. /*  D M P H L P  --  Dump the help line buffer  */
  1017.  
  1018. dmphlp() {                              /* Print the help buffer */
  1019.     hlpbuf[hx++] = NUL;
  1020.     printf(" %s\n",hlpbuf);
  1021.     clrhlp();
  1022. }
  1023.  
  1024.  
  1025. /*  L O O K U P  --  Lookup the string in the given array of strings  */
  1026.  
  1027. /*
  1028.  Call this way:  v = lookup(table,word,n,&x);
  1029.  
  1030.    table - a 'struct keytab' table.
  1031.    word  - the target string to look up in the table.
  1032.    n     - the number of elements in the table.
  1033.    x     - address of an integer for returning the table array index.
  1034.  
  1035.  The keyword table must be arranged in ascending alphabetical order, and
  1036.  all letters must be lowercase.
  1037.  
  1038.  Returns the keyword's associated value ( zero or greater ) if found,
  1039.  with the variable x set to the array index, or:
  1040.  
  1041.   -3 if nothing to look up (target was null),
  1042.   -2 if ambiguous,
  1043.   -1 if not found.
  1044.  
  1045.  A match is successful if the target matches a keyword exactly, or if
  1046.  the target is a prefix of exactly one keyword.  It is ambiguous if the
  1047.  target matches two or more keywords from the table.
  1048. */
  1049.  
  1050. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  1051.  
  1052.     int i, v, cmdlen;
  1053.  
  1054. /* Lowercase & get length of target, if it's null return code -3. */
  1055.  
  1056.     if ((((cmdlen = lower(cmd))) == 0) || (n < 1)) return(-3);
  1057.  
  1058. /* Not null, look it up */
  1059.  
  1060.     for (i = 0; i < n-1; i++) {
  1061.         if (!strcmp(table[i].kwd,cmd) ||
  1062.            ((v = !strncmp(table[i].kwd,cmd,cmdlen)) &&
  1063.              strncmp(table[i+1].kwd,cmd,cmdlen))) {
  1064.                 *x = i;
  1065.                 return(table[i].val);
  1066.              }
  1067.         if (v) return(-2);
  1068.     }   
  1069.  
  1070. /* Last (or only) element */
  1071.  
  1072.     if (!strncmp(table[n-1].kwd,cmd,cmdlen)) {
  1073.         *x = n-1;
  1074.         return(table[n-1].val);
  1075.     } else return(-1);
  1076. }
  1077.  
  1078.  
  1079. /*  G T W O R D  --  Gets a "word" from the command input stream  */
  1080.  
  1081. /*
  1082. Usage: retcode = gtword();
  1083.  
  1084. Returns:
  1085.  -4 if end of file (e.g. pipe broken)
  1086.  -2 if command buffer overflows
  1087.  -1 if user did some deleting
  1088.   0 if word terminates with SP or tab
  1089.   1 if ... CR
  1090.   2 if ... ESC
  1091.   3 if ... ?
  1092.  
  1093. With:
  1094.   pp pointing to beginning of word in buffer
  1095.   bp pointing to after current position
  1096.   atmbuf containing a copy of the word
  1097.   cc containing the number of characters in the word copied to atmbuf
  1098. */
  1099.  
  1100. ungword() {             /* unget a word */
  1101.     if (ungw) return(0);
  1102.     cmflgsav = cmflgs;
  1103.     debug(F101,"ungword cmflgs","",cmflgs);
  1104.     ungw = 1;
  1105.     cmflgs = 0;
  1106.     return(0);
  1107. }
  1108.  
  1109. gtword() {
  1110.     int c;                              /* Current char */
  1111.     static int inword = 0;              /* Flag for start of word found */
  1112.     int quote = 0;                      /* Flag for quote character */
  1113.     int echof = 0;                      /* Flag for whether to echo */
  1114.     int chsrc = 0;          /* Source of character, 1 = tty */
  1115.     int comment = 0;            /* Flag for in comment */
  1116.     char *cp;               /* Comment pointer */
  1117.  
  1118. #ifdef RTU
  1119.     extern int rtu_bug;
  1120. #endif
  1121.  
  1122. #ifdef datageneral
  1123.     extern int termtype;                /* DG terminal type flag */
  1124.     extern int con_reads_mt;            /* Console read asynch is active */
  1125.     if (con_reads_mt) connoi_mt();      /* Task would interfere w/cons read */
  1126. #endif 
  1127.  
  1128.     if (ungw) {             /* Have a word saved? */
  1129.     debug(F110,"gtword ungetting from pp",pp,0);
  1130.     while (*pp++ == SP) ;
  1131.     setatm(pp);
  1132.     ungw = 0;
  1133.     cmflgs = cmflgsav;
  1134.     debug(F111,"gtword returning atmbuf",atmbuf,cmflgs);
  1135.     return(cmflgs);
  1136.     }
  1137.     pp = np;                            /* Start of current field */
  1138.     debug(F111,"gtword: cmdbuf",cmdbuf,(int) cmdbuf);
  1139.     debug(F111," bp",bp,(int) bp);
  1140.     debug(F111," pp",pp,(int) pp);
  1141.  
  1142.     while (bp < cmdbuf+CMDBL) {         /* Big get-a-character loop */
  1143.     echof = 0;          /* Assume we don't echo because */
  1144.     chsrc = 0;          /* character came from reparse buf. */
  1145.         if ((c = *bp) == NUL) {         /* If no char waiting in reparse buf */
  1146.             if (dpx) echof = 1;         /* must get from tty, set echo flag. */
  1147.         c = cmdgetc();      /* Read a character from the tty. */
  1148.         chsrc = 1;          /* Remember character source is tty. */
  1149.             if (c == EOF) {     /* This can happen if stdin not tty. */
  1150.         return(-4);
  1151.         }
  1152.         c &= 127;           /* Strip any parity bit. */
  1153.     }               /* Change this for 8-bit UNIX! */
  1154.  
  1155. /* Now we have the next character */
  1156.  
  1157.         if (quote == 0) {       /* If this is not a quoted character */
  1158.             if (c == CMDQ) {        /* Got the quote character itself */
  1159.         if (!comment) quote = 1; /* Flag it if not in a comment */
  1160.             }
  1161.         if (c == FF) {      /* Formfeed. */
  1162.                 c = NL;                 /* Replace with newline */
  1163.         cmdclrscn();        /* Clear the screen */
  1164.             }
  1165.  
  1166.         if (c == HT) {      /* Tab */
  1167.         if (comment)        /* If in comment, */
  1168.           c = SP;       /* substitute space */
  1169.         else            /* otherwise */
  1170.           c = ESC;      /* substitute ESC (for completion) */
  1171.         }
  1172.         if (c == ';' || c == '#') { /* Trailing comment */
  1173.         if (inword == 0) {  /* If we're not in a word */
  1174.             comment = 1;    /* start a comment. */
  1175.             cp = bp;        /* remember where it starts. */
  1176.         }
  1177.         }
  1178.         if (!comment && c == SP) {  /* Space */
  1179.                 *bp++ = c;      /* deposit in buffer if not already */
  1180.                 if (echof) putchar(c);  /* echo it. */
  1181.                 if (inword == 0) {      /* If leading, gobble it. */
  1182.                     pp++;
  1183.                     continue;
  1184.                 } else {                /* If terminating, return. */
  1185.                     np = bp;
  1186.                     setatm(pp);
  1187.                     inword = 0;
  1188.             return(cmflgs = 0);
  1189.                 }
  1190.             }
  1191.             if (c == NL || c == CR) {   /* CR or LF. */
  1192.         if (echof) cmdnewl(c);  /* Echo it. */
  1193.         if (*(bp-1) == '-') {   /* Is this line continued? */
  1194.             if (chsrc) {    /* If reading from tty, */
  1195.             bp--;       /* back up the buffer pointer, */
  1196.             *bp = NUL;  /* erase the dash, */
  1197.             continue;   /* and go back for next char now. */
  1198.             }
  1199.         } else {        /* No, a command has been entered. */
  1200.             *bp = NUL;      /* Terminate the command string. */
  1201.             if (comment) {  /* If we're in a comment, */
  1202.             comment = 0;    /* Say we're not any more, */
  1203.             *cp = NUL;  /* cut it off. */
  1204.             }
  1205.             np = bp;        /* Where to start next field. */
  1206.             setatm(pp);     /* Copy this field to atom buffer. */
  1207.             inword = 0;     /* Not in a word any more. */
  1208.             return(cmflgs = 1);
  1209.         }
  1210.             }
  1211.             if (!comment && echof && (c == '?')) { /* Question mark */
  1212.                 putchar(c);
  1213.                 *bp = NUL;
  1214.                 setatm(pp);
  1215.                 return(cmflgs = 3);
  1216.             }
  1217.             if (c == ESC) {     /* ESC */
  1218.         if (!comment) {
  1219.             *bp = NUL;
  1220.             setatm(pp);
  1221.             return(cmflgs = 2);
  1222.         } else {
  1223.             putchar(BEL);
  1224.             continue;
  1225.         }
  1226.             }
  1227.             if (c == BS || c == RUB) {  /* Character deletion */
  1228.                 if (bp > cmdbuf) {      /* If still in buffer... */
  1229.             cmdchardel();   /* erase it. */
  1230.                     bp--;               /* point behind it, */
  1231.                     if (*bp == SP) inword = 0; /* Flag if current field gone */
  1232.                     *bp = NUL;          /* Erase character from buffer. */
  1233.                 } else {                /* Otherwise, */
  1234.                     putchar(BEL);       /* beep, */
  1235.                     cmres();            /* and start parsing a new command. */
  1236.                 }
  1237.                 if (pp < bp) continue;
  1238.                 else return(cmflgs = -1);
  1239.             }
  1240.             if (c == LDEL) {            /* ^U, line deletion */
  1241.                 while ((bp--) > cmdbuf) {
  1242.                     cmdchardel();
  1243.                     *bp = NUL;
  1244.                 }
  1245.                 cmres();                /* Restart the command. */
  1246.                 inword = 0;
  1247.                 return(cmflgs = -1);
  1248.             }
  1249.             if (c == WDEL) {            /* ^W, word deletion */
  1250.                 if (bp <= cmdbuf) {     /* Beep if nothing to delete */
  1251.                     putchar(BEL);
  1252.                     cmres();
  1253.                     return(cmflgs = -1);
  1254.                 }
  1255.                 bp--;
  1256.                 for ( ; (bp >= cmdbuf) && (*bp == SP) ; bp--) {
  1257.                     cmdchardel();
  1258.                     *bp = NUL;
  1259.                 }
  1260.                 for ( ; (bp >= cmdbuf) && (*bp != SP) ; bp--) {
  1261.                     cmdchardel();
  1262.                     *bp = NUL;
  1263.                 }
  1264.                 bp++;
  1265.                 inword = 0;
  1266.                 return(cmflgs = -1);
  1267.             }
  1268.             if (c == RDIS) {            /* ^R, redisplay */
  1269.                 *bp = NUL;
  1270.                 printf("\n%s%s",cmprom,cmdbuf);
  1271.                 continue;
  1272.             }
  1273.         if (c < SP && quote == 0) { /* Any other unquoted control */
  1274.         putchar(BEL);       /* character -- just beep and */
  1275.         continue;       /* continue, don't put in buffer */
  1276.         }
  1277.         } else {            /* This character was quoted. */
  1278.         quote = 0;          /* Unset the quote flag. */
  1279.         /* Quote character at this level is only for SP, ?, and controls */
  1280.             /* If anything else was quoted, leave quote in, and let */
  1281.         /* the command-specific parsing routines handle it, e.g. \007 */
  1282.         if (c > 32 && c != '?' && c != RUB && chsrc != 0)
  1283.           *bp++ = CMDQ;     /* Deposit \ if it came from tty */
  1284.         debug(F110,"gtword quote",cmdbuf,0);
  1285.     }
  1286.         if (echof) cmdecho(c,quote);    /* Echo what was typed. */
  1287.         if (!comment) inword = 1;   /* Flag we're in a word. */
  1288.     if (quote) continue;        /* Don't deposit quote character. */
  1289.         if (c != NL) *bp++ = c;     /* Deposit command character. */
  1290.     debug(F110,"gtword deposit",cmdbuf,0);
  1291.     }                                   /* End of big while */
  1292.     putchar(BEL);                       /* Get here if... */
  1293.     printf("\n?Buffer full\n");
  1294.     return(cmflgs = -2);
  1295. }
  1296.  
  1297. /* Utility functions */
  1298.  
  1299. /* A D D B U F  -- Add the string pointed to by cp to the command buffer  */
  1300.  
  1301. addbuf(cp) char *cp; {
  1302.     int len = 0;
  1303.     while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) {
  1304.         *bp++ = *cp++;                  /* Copy and */
  1305.         len++;                          /* count the characters. */
  1306.     }   
  1307.     *bp++ = SP;                         /* Put a space at the end */
  1308.     *bp = NUL;                          /* Terminate with a null */
  1309.     np = bp;                            /* Update the next-field pointer */
  1310.     return(len);                        /* Return the length */
  1311. }
  1312.  
  1313. /*  S E T A T M  --  Deposit a token in the atom buffer.  */
  1314. /*  Break on space, newline, carriage return, or null. */
  1315. /*  Null-terminate the result. */
  1316. /*  If the source pointer is the atom buffer itself, do nothing. */
  1317. /*  Return length of token, and also set global "cc" to this length. */
  1318.  
  1319. setatm(cp) char *cp; {
  1320.     char *ap, *xp;
  1321.  
  1322.     cc = 0;             /* Character couner */
  1323.     ap = atmbuf;            /* Address of atom buffer */
  1324.  
  1325.     if (cp == ap) {         /* In case source is atom buffer */
  1326.     xp = atybuf;            /* make a copy */
  1327.     strcpy(xp,ap);          /* so we can copy it back, edited. */
  1328.     cp = xp;
  1329.     }
  1330.     *ap = NUL;              /* Zero the atom buffer */
  1331.     while (*cp == SP) cp++;     /* Trim leading spaces */
  1332.     while ((*cp != SP) && (*cp != NL) && (*cp != NUL) && (*cp != CR)) {
  1333.         *ap++ = *cp++;          /* Copy up to SP, NL, CR, or end */
  1334.         cc++;               /* and count */
  1335.     }
  1336.     *ap++ = NUL;            /* Terminate the string. */
  1337.     return(cc);                         /* Return length. */
  1338. }
  1339.  
  1340. /*  R D I G I T S  -- Verify that all the characters in line ARE DIGITS  */
  1341.  
  1342. rdigits(s) char *s; {
  1343.     while (*s) {
  1344.         if (!isdigit(*s)) return(0);
  1345.         s++;
  1346.     }
  1347.     return(1);
  1348. }
  1349.  
  1350. /*  L O W E R  --  Lowercase a string  */
  1351.  
  1352. lower(s) char *s; {
  1353.     int n = 0;
  1354.     while (*s) {
  1355.         if (isupper(*s)) *s = tolower(*s);
  1356.         s++, n++;
  1357.     }
  1358.     return(n);
  1359. }
  1360.  
  1361. /*  T E S T  --  Bit test  */
  1362.  
  1363. test(x,m) int x, m; { /*  Returns 1 if any bits from m are on in x, else 0  */
  1364.     return((x & m) ? 1 : 0);
  1365. }
  1366.  
  1367. /* These functions attempt to hide system dependencies from the mainline */
  1368. /* code in gtword().  Ultimately they should be moved to ck*tio.c, where */
  1369. /* * = each and every system supported by C-Kermit. */
  1370.  
  1371. cmdgetc() {             /* Get a character from the tty. */
  1372.     int c;
  1373.  
  1374. #ifdef datageneral
  1375.     {
  1376.     char ch;
  1377.     c = dgncinb(0,&ch,1);       /* -1 is EOF, -2 TO, 
  1378.                                          * -c is AOS/VS error */
  1379.     if (c == -2) {          /* timeout was enabled? */
  1380.         resto(channel(0));      /* reset timeouts */
  1381.         c = dgncinb(0,&ch,1);   /* retry this now! */
  1382.     }
  1383.     if (c < 0) return(-4);      /* EOF or some error */
  1384.     else c = (int) ch & 0177;   /* Get char without parity */
  1385. /*  echof = 1; */
  1386.     }
  1387. #else
  1388. #ifdef OS2
  1389.     c = isatty(0) ? coninc(0) : getchar();
  1390.     if (c<0) return(-4);
  1391. #else
  1392.     c = getchar();          /* or from tty. */
  1393. #ifdef RTU
  1394.     if (rtu_bug) {
  1395.     c = getchar();          /* RTU doesn't discard the ^Z */
  1396.     rtu_bug = 0;
  1397.     }
  1398. #endif /* RTU */
  1399. #endif
  1400. #endif
  1401.     return(c);              /* Return what we got */
  1402. }
  1403.  
  1404.  
  1405. cmdclrscn() {               /* Clear the screen */
  1406.  
  1407. #ifdef aegis
  1408.     putchar(FF);
  1409. #else
  1410. #ifdef AMIGA
  1411.     putchar(FF);
  1412. #else
  1413. #ifdef OSK
  1414.     putchar(FF);
  1415. #else
  1416. #ifdef datageneral
  1417.     putchar(FF);
  1418. #else
  1419. #ifdef OS2
  1420.     { char cell[2];
  1421.       cell[0] = ' ';
  1422.       cell[1] = 7;
  1423. /*      VioScrollUp(0,0,-1,-1,-1,cell,0); */
  1424. /*      VioSetCurPos(0,0,0); */
  1425.       AVIOScrollUp(0,0,-1,-1,-1,cell);
  1426.       AVIOSetCurPos(0,0);
  1427. //      VioScrollUp(0,0,-1,-1,-1,cell,0);
  1428. //      VioSetCurPos(0,0,0);
  1429.   }
  1430. #else
  1431.     system("clear");
  1432. #endif
  1433. #endif
  1434. #endif
  1435. #endif
  1436. #endif
  1437. }
  1438.  
  1439. cmdnewl(c) char c; {            /* What to echo at end of command */
  1440.     putchar(c);             /* c is the terminating character */
  1441. #ifdef OS2
  1442.     if (c == CR) putchar(NL);
  1443. #endif
  1444. #ifdef aegis
  1445.     if (c == CR) putchar(NL);
  1446. #endif
  1447. #ifdef AMIGA
  1448.     if (c == CR) putchar(NL);
  1449. #endif
  1450. #ifdef datageneral
  1451.     if (c == CR) putchar(NL);
  1452. #endif
  1453. }
  1454.  
  1455. cmdchardel() {              /* Erase a character from the screen */
  1456. #ifdef datageneral
  1457.     /* DG '\b' is EM (^y or \031) */
  1458.     if (termtype == 1)
  1459.       /* Erase a character from non-DG screen, */
  1460.       dgncoub(1,"\010 \010",3);
  1461.     else
  1462. #endif
  1463.       printf("\b \b");
  1464. }
  1465.  
  1466. cmdecho(c,quote) char c; int quote; {   /* Echo tty input character c */
  1467.     putchar(c);
  1468. #ifdef OS2
  1469.     if (quote==1 && c==CR) putchar(NL);
  1470. #endif
  1471. }
  1472.  
  1473. /*  X X E S C  --  Interprets backslash codes  */
  1474. /*  Returns the int value of the backslash code if it is > -1 and < 256 */
  1475. /*  Otherwise returns -1 */
  1476. /*  Updates the string pointer to first character after backslash code */ 
  1477. /*  If the backslash code is invalid, leaves pointer pointing at the */
  1478. /*  second character after the backslash. */
  1479.  
  1480. xxesc(s) char **s; {            /* Expand backslash escapes */
  1481.     int x, y, brace, radix;     /* Returns the int value */
  1482.     char hd, *p;
  1483.  
  1484.     p = *s;             /* pointer to beginning */
  1485.     x = *p++;               /* character at beginning */
  1486.     if (x != CMDQ) return(-1);      /* make sure it's a backslash code */
  1487.  
  1488.     x = *p;             /* it is, get the next character */
  1489.     if (x == '{') {         /* bracketed quantity? */
  1490.     x = *(++p);         /* begin past bracket */
  1491.     brace = 1;
  1492.     } else brace = 0;
  1493.     switch (x) {            /* Start interpreting */
  1494.       case 'd':             /* Decimal radix indicator */
  1495.       case 'D':
  1496.     p++;                /* Just point past it and fall thru */
  1497.       case '0':             /* Starts with digit */
  1498.       case '1':
  1499.       case '2':  case '3':  case '4':  case '5':
  1500.       case '6':  case '7':  case '8':  case '9':
  1501.     radix = 10;         /* Decimal */
  1502.     hd = '9';           /* highest valid digit */
  1503.     break;
  1504.       case 'o':             /* Starts with o or O */
  1505.       case 'O':
  1506.     radix = 8;          /* Octal */
  1507.     hd = '7';           /* highest valid digit */
  1508.     p++;                /* point past radix indicator */
  1509.     break;
  1510.       case 'x':             /* Starts with x, X, h, or H */
  1511.       case 'X':
  1512.       case 'h':
  1513.       case 'H':
  1514.     radix = 16;         /* Hexidecimal */
  1515.     p++;                /* point past radix indicator */
  1516.     break;
  1517.       case CMDQ:            /* A second backslash */
  1518.     *s = p+1;           /* point past it (\\ becomes \) */
  1519.         return(CMDQ);
  1520.       default:              /* All others */
  1521.     *s = p;             /* Return current pointer */
  1522.     return(-1);         /* with failure indication. */
  1523.     }
  1524.     if (radix <= 10) {          /* Number in radix 8 or 10 */
  1525.     for ( x = y = 0;
  1526.           (*p) && (*p >= '0') && (*p <= hd) && (y < 3) && (x*radix < 256);
  1527.           p++,y++) {
  1528.         x = x * radix + (int) *p - 48;
  1529.     }
  1530.     if (y == 0 || x > 255) {    /* No valid digits? */
  1531.         *s = p;         /* point after it */
  1532.         return(-1);         /* return failure. */
  1533.     }
  1534.     } else if (radix == 16) {       /* Special case for hex */
  1535.     if ((x = unhex(*p++)) < 0) { *s = p - 1; return(-1); }
  1536.     if ((y = unhex(*p++)) < 0) { *s = p - 2; return(-1); }
  1537.     x = ((x << 4) & 0xF0) | (y & 0x0F);
  1538.     } else x = -1;
  1539.     if (brace && *p == '}' && x > -1)   /* Point past closing brace, if any */
  1540.       p++;
  1541.     *s = p;             /* Point to next char after sequence */
  1542.     return(x);              /* Return value of sequence */
  1543. }
  1544.  
  1545. unhex(x) char x; {
  1546.     if (x >= '0' && x <= '9')       /* 0-9 is offset by hex 30 */
  1547.       return(x - 0x30);
  1548.     else if (x >= 'A' && x <= 'F')  /* A-F offset by hex 37 */
  1549.       return(x - 0x37);
  1550.     else if (x >= 'a' && x <= 'f')  /* a-f offset by hex 57 */
  1551.       return(x - 0x57);         /* (obviously ASCII dependent) */
  1552.     else return(-1);
  1553. }
  1554.  
  1555. /* See if argument string is numeric */
  1556. /* Returns 1 if OK, zero if not OK */
  1557. /* If OK, string should be acceptable to atoi() */
  1558. /* Allows leading space, sign */
  1559.  
  1560. chknum(s) char *s; {            /* Check Numeric String */
  1561.     int x = 0;              /* Flag for past leading space */
  1562.     int y = 0;              /* Flag for digit seen */
  1563.     char c;
  1564.     debug(F110,"chknum",s,0);
  1565.     while (c = *s++) {          /* For each character in the string */
  1566.     switch (c) {
  1567.       case SP:          /* Allow leading spaces */
  1568.       case HT:
  1569.         if (x == 0) continue;
  1570.         else return(0);
  1571.       case '+':         /* Allow leading sign */
  1572.       case '-':
  1573.         if (x == 0) x = 1;
  1574.         else return(0);
  1575.         break;
  1576.       default:          /* After that, only decimal digits */
  1577.         if (c >= '0' && c <= '9') {
  1578.         x = y = 1;
  1579.         continue;
  1580.         } else return(0);
  1581.     }
  1582.     }
  1583.     return(y);
  1584. }
  1585.