home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckucmd.c < prev    next >
C/C++ Source or Header  |  1994-10-02  |  82KB  |  2,662 lines

  1. #include "ckcsym.h"
  2.  
  3. int cmdmsk = 127; /* Command-terminal-to-C-Kermit character mask */
  4.  
  5. #include "ckcdeb.h"                     /* Formats for debug(), etc. */
  6. #include "ckucmd.h"            /* Needed for xx_strp prototype */
  7. _PROTOTYP( int unhex, (char) );
  8. static int quoting = 1;
  9.  
  10. #ifndef NOICP     /* The rest only if interactive command parsing selected */
  11.  
  12. char *cmdv = "Command package 5A(067), 2 Oct 94";
  13.  
  14. /*  C K U C M D  --  Interactive command package for Unix  */
  15.  
  16. /*
  17.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  18.   Columbia University Academic Information Systems, New York City.
  19.  
  20.   Copyright (C) 1985, 1994, Trustees of Columbia University in the City of New
  21.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  22.   sold for profit as a software product itself, nor may it be included in or
  23.   distributed with commercial products or otherwise distributed by commercial
  24.   concerns to their clients or customers without written permission of the
  25.   Office of Kermit Development and Distribution, Columbia University.  This
  26.   copyright notice must not be removed, altered, or obscured.
  27. */
  28.  
  29. /*
  30. Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP. Features:
  31.  
  32. . parses and verifies keywords, filenames, text strings, numbers, other data
  33. . displays appropriate menu or help message when user types "?"
  34. . does keyword and filename completion when user types ESC or TAB
  35. . does partial filename completion
  36. . accepts any unique abbreviation for a keyword
  37. . allows keywords to have attributes, like "invisible" and "abbreviation"
  38. . can supply defaults for fields omitted by user
  39. . provides command retry and recall
  40. . provides command line editing (character, word, and line deletion)
  41. . accepts input from keyboard, command files, macros, or redirected stdin
  42. . allows for full or half duplex operation, character or line input
  43. . allows \-escapes for hard-to-type characters
  44. . allows specification of a user exit to expand variables, etc.
  45. . settable prompt, protected from deletion, dynamically re-evaluated each time.
  46.  
  47. Functions:
  48.  cmsetp - Set prompt (cmprom is prompt string)
  49.  cmsavp - Save current prompt
  50.  prompt - Issue prompt
  51.  cmini - Clear the command buffer (before parsing a new command)
  52.  cmres - Reset command buffer pointers (before reparsing)
  53.  cmkey - Parse a keyword or token
  54.  cmnum - Parse a number
  55.  cmifi - Parse an input file name
  56.  cmofi - Parse an output file name
  57.  cmdir - Parse a directory name
  58.  cmfld - Parse an arbitrary field
  59.  cmtxt - Parse a text string
  60.  cmcfm - Parse command confirmation (end of line)
  61.  
  62. Return codes:
  63.  -3: no input provided when required
  64.  -2: input was invalid (e.g. not a number when a number was required)
  65.  -1: reparse required (user deleted into a preceding field)
  66.   0 or greater: success
  67. See individual functions for greater detail.
  68.  
  69. Before using these routines, the caller should #include ckucmd.h, and set the
  70. program's prompt by calling cmsetp().  If the file parsing functions cmifi,
  71. cmofi, or cmdir are to be used, this module must be linked with a ck?fio file
  72. system support module for the appropriate system, e.g. ckufio for Unix.  If
  73. the caller puts the terminal in character wakeup ("cbreak") mode with no echo,
  74. then these functions will provide line editing -- character, word, and line
  75. deletion, as well as keyword and filename completion upon ESC and help
  76. strings, keyword, or file menus upon '?'.  If the caller puts the terminal
  77. into character wakeup/noecho mode, care should be taken to restore it before
  78. exit from or interruption of the program.  If the character wakeup mode is not
  79. set, the system's own line editor may be used.
  80.  
  81. NOTE: Contrary to expectations, many #ifdef's have been added to this module.
  82. Any operation requiring an #ifdef (like clear screen, get character from
  83. keyboard, erase character from screen, etc) should eventually be turned into a
  84. call to a function that is defined in ck?tio.c, but then all the ck?tio.c
  85. modules would have to be changed...
  86. */
  87.  
  88. /* Includes */
  89.  
  90. #include "ckcker.h"
  91. #include "ckcasc.h"            /* ASCII character symbols */
  92. #include "ckucmd.h"                     /* Command parsing definitions */
  93. #include <errno.h>            /* Error number symbols */
  94.  
  95. #ifdef OSK
  96. #define cc ccount            /* OS-9/68K compiler bug */
  97. #endif /* OSK */
  98.  
  99. #ifdef GEMDOS                /* Atari ST */
  100. #ifdef putchar
  101. #undef putchar
  102. #endif /* putchar */
  103. #define putchar(x) conoc(x)        /* Why doesn't everyone do this? */
  104. #endif /* GEMDOS */
  105.  
  106. /* Local variables */
  107.  
  108. static
  109. int psetf = 0,                          /* Flag that prompt has been set */
  110.     cc = 0,                             /* Character count */
  111.     dpx = 0,                            /* Duplex (0 = full) */
  112.     inword = 0;                /* In the middle of getting a word */
  113.  
  114. static
  115. int hw = HLPLW,                         /* Help line width */
  116.     hc = HLPCW,                         /* Help line column width */
  117.     hh,                                 /* Current help column number */
  118.     hx;                                 /* Current help line position */
  119.  
  120. #define PROML 160                       /* Maximum length for prompt */
  121.  
  122. char *dfprom = "Command? ";             /* Default prompt */
  123.  
  124. int cmflgs;                             /* Command flags */
  125. int cmfsav;                /* A saved version of them */
  126.  
  127. #ifdef DCMDBUF
  128. char *cmdbuf;                /* Command buffer */
  129. char *savbuf;                /* Buffer to save copy of command */
  130. char *atmbuf;                /* Atom buffer - for current field */
  131. char *atxbuf;                /* For expanding the atom buffer */
  132. static char *hlpbuf;            /* Help string buffer */
  133. static char *atybuf;            /* For copying atom buffer */
  134. static char *filbuf;            /* File name buffer */
  135. static char *cmprom;            /* Program's prompt */
  136. static char *cmprxx;            /* Program's prompt, unevaluated */
  137.  
  138. #ifdef CK_RECALL
  139. /*
  140.   Command recall is available only if we can make profligate use of malloc().
  141. */
  142. #define R_MAX 10            /* Max number of commands to save */
  143. int cm_recall = R_MAX;            /* Size of command recall buffer */
  144. int on_recall = 1;            /* Recall feature is ON */
  145. static int
  146.   current = -1,                /* Pointer to current command */
  147.   rlast = -1;                /* Index of last command in buffer */
  148. static char **recall = NULL;        /* Array of recall buffer pointers */
  149. #endif /* CK_RECALL */
  150. #else
  151. char cmdbuf[CMDBL+4];                   /* Command buffer */
  152. char savbuf[CMDBL+4];                   /* Buffer to save copy of command */
  153. char atmbuf[ATMBL+4];                   /* Atom buffer */
  154. char atxbuf[CMDBL+4];                   /* For expanding the atom buffer */
  155. static char hlpbuf[HLPBL+4];        /* Help string buffer */
  156. static char atybuf[ATMBL+4];        /* For copying atom buffer */
  157. static char filbuf[ATMBL+4];        /* File name buffer */
  158. static char cmprom[PROML+1];        /* Program's prompt */
  159. static char cmprxx[PROML+1];        /* Program's prompt, unevaluated */
  160. #endif /* DCMDBUF */
  161.  
  162. /* Command buffer pointers */
  163.  
  164. static char *bp,                        /* Current command buffer position */
  165.     *pp,                                /* Start of current field */
  166.     *np;                                /* Start of next field */
  167.  
  168. static int ungw,            /* For ungetting words */
  169.     atxn;                /* Expansion buffer (atxbuf) length */
  170.  
  171. #ifdef OS2
  172.     extern int wideresult;
  173. #endif /* OS2 */
  174.  
  175. _PROTOTYP( static VOID addhlp, (char *) );
  176. _PROTOTYP( static VOID clrhlp, (void) );
  177. _PROTOTYP( static VOID dmphlp, (void) );
  178. _PROTOTYP( static int gtword, (void) );
  179. _PROTOTYP( static int addbuf, (char *) );
  180. _PROTOTYP( static int setatm, (char *, int) );
  181. _PROTOTYP( static int cmdgetc, (void) );
  182. _PROTOTYP( static VOID cmdnewl, (char) );
  183. _PROTOTYP( static VOID cmdchardel, (void) );
  184. _PROTOTYP( static VOID cmdecho, (char, int) );
  185. _PROTOTYP( static int test, (int, int) );
  186. #ifdef GEMDOS
  187. _PROTOTYP( extern char *strchr, (char *, int) );
  188. #endif /* GEMDOS */
  189.  
  190. /*  T E S T  --  Bit test  */
  191.  
  192. static int
  193. test(x,m) int x, m; { /*  Returns 1 if any bits from m are on in x, else 0  */
  194.     return((x & m) ? 1 : 0);
  195. }
  196.  
  197. /*  C M S E T U P  --  Set up command buffers  */
  198.  
  199. #ifdef DCMDBUF
  200. int
  201. cmsetup() {
  202.     if (!(cmdbuf = malloc(CMDBL + 4))) return(-1);
  203.     if (!(savbuf = malloc(CMDBL + 4))) return(-1);
  204.     savbuf[0] = '\0';
  205.     if (!(hlpbuf = malloc(HLPBL + 4))) return(-1);
  206.     if (!(atmbuf = malloc(ATMBL + 4))) return(-1);
  207.     if (!(atxbuf = malloc(CMDBL + 4))) return(-1);
  208.     if (!(atybuf = malloc(ATMBL + 4))) return(-1);
  209.     if (!(filbuf = malloc(ATMBL + 4))) return(-1);
  210.     if (!(cmprom = malloc(PROML + 4))) return(-1);
  211.     if (!(cmprxx = malloc(PROML + 4))) return(-1);
  212. #ifdef CK_RECALL
  213.     cmrini(cm_recall);
  214. #endif /* CK_RECALL */
  215.     return(0);
  216. }
  217.  
  218. #ifdef CK_RECALL
  219. /* Initialize or change size of command recall buffer */
  220. int
  221. cmrini(n) int n; {
  222.     int i;
  223.     if (recall) {            /* Free old storage, if any */
  224.     for (i = 0; i < cm_recall; i++)
  225.       if (recall[i])
  226.         free(recall[i]);
  227.     free(recall);
  228.     recall = NULL;
  229.     }
  230.     cm_recall = n;            /* Set new size */
  231.     rlast = current = -1;        /* Initialize pointers */
  232.     if (n > 0) {
  233.     recall = (char **)malloc((cm_recall + 1) * sizeof(char *));
  234.     if (!recall)
  235.       return(1);
  236.     for (i = 0; i < cm_recall; i++)
  237.       recall[i] = NULL;
  238.     }
  239.     return(0);
  240. }
  241. #endif /* CK_RECALL */
  242. #endif /* DCMDBUF */
  243.  
  244. /*  C M S E T P  --  Set the program prompt.  */
  245.  
  246. VOID
  247. cmsetp(s) char *s; {
  248.     strncpy(cmprxx,s,PROML - 1);
  249.     cmprxx[PROML] = NUL;
  250.     psetf = 1;                          /* Flag that prompt has been set. */
  251. }
  252. /*  C M S A V P  --  Save a copy of the current prompt.  */
  253.  
  254. VOID
  255. #ifdef CK_ANSIC
  256. cmsavp(char s[], int n)
  257. #else
  258. cmsavp(s,n) char s[]; int n;
  259. #endif /* CK_ANSIC */
  260. /* cmsavp */ {
  261.     strncpy(s,cmprxx,n-1);
  262.     s[n-1] = NUL;
  263. }
  264.  
  265. /*  P R O M P T  --  Issue the program prompt.  */
  266.  
  267. VOID
  268. prompt(f) xx_strp f; {
  269.     char *sx, *sy; int n;
  270.  
  271.     if (psetf == 0) cmsetp(dfprom);     /* If no prompt set, set default. */
  272.  
  273.     sx = cmprxx;            /* Unevaluated copy */
  274.     if (f) {                /* If conversion function given */
  275.     sy = cmprom;            /* Evaluate it */
  276.     debug(F101,"PROMPT sx","",sx);
  277.     debug(F101,"PROMPT sy","",sy);
  278.     n = PROML;
  279.     if ((*f)(sx,&sy,&n) < 0)    /* If evaluation failed */
  280.       sx = cmprxx;            /* revert to unevaluated copy */
  281.     else
  282.       sx = cmprom;
  283.     } else strcpy(cmprom,sx);
  284. #ifdef OSK
  285.     fputs(sx, stdout);
  286. #else
  287. #ifdef MAC
  288.     printf("%s", sx);
  289. #else
  290.     printf("\r%s",sx);            /* Print the prompt. */
  291.     fflush(stdout);            /* Now! */
  292. #endif /* MAC */
  293. #endif /* OSK */
  294. }
  295.  
  296. #ifndef NOSPL
  297. VOID
  298. pushcmd() {                /* For use with IF command. */
  299.     strncpy(savbuf,np,CMDBL);        /* Save the dependent clause,  */
  300.     cmres();                /* and clear the command buffer. */
  301.     debug(F110, "pushcmd: savbuf:", savbuf, 0);
  302. }
  303. #endif /* NOSPL */
  304.  
  305. #ifdef COMMENT
  306. /* no longer used... */
  307. VOID
  308. popcmd() {
  309.     strncpy(cmdbuf,savbuf,CMDBL);    /* Put back the saved material */
  310.     *savbuf = '\0';            /* and clear the save buffer */
  311.     cmres();
  312. }
  313. #endif /* COMMENT */
  314.  
  315. /*  C M R E S  --  Reset pointers to beginning of command buffer.  */
  316.  
  317. VOID
  318. cmres() {
  319.     inword = cc = 0;            /* Reset character counter. */
  320.     pp = np = bp = cmdbuf;              /* Point to command buffer. */
  321.     cmflgs = -5;                        /* Parse not yet started. */
  322.     ungw = 0;                /* Don't need to unget a word. */
  323. }
  324.  
  325. /*  C M I N I  --  Clear the command and atom buffers, reset pointers.  */
  326.  
  327. /*
  328. The argument specifies who is to echo the user's typein --
  329.   1 means the cmd package echoes
  330.   0 somebody else (system, front end, terminal) echoes
  331. */
  332. VOID
  333. cmini(d) int d; {
  334.     for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  335.     *atmbuf = *savbuf = *atxbuf = *atybuf = *hlpbuf = *filbuf = NUL;
  336.     dpx = d;
  337.     cmres();
  338. }
  339.  
  340. #ifndef NOSPL
  341. /* The following bits are to allow the command package to call itself */
  342. /* in the middle of a parse.  To do this, begin by calling cmpush, and */
  343. /* end by calling cmpop. */
  344.  
  345. #ifdef DCMDBUF
  346. struct cmp {
  347.     int i[5];                /* stack for integers */
  348.     char *c[3];                /* stack for pointers */
  349.     char *b[8];                /* stack for buffer contents */
  350. };
  351. struct cmp *cmp = 0;
  352. #else
  353. int cmp_i[CMDDEP+1][5];            /* Stack for integers */
  354. char *cmp_c[CMDDEP+1][5];        /* for misc pointers */
  355. char *cmp_b[CMDDEP+1][7];        /* for buffer contents pointers */
  356. #endif /* DCMDBUF */
  357.  
  358. int cmddep = -1;            /* Current stack depth */
  359.  
  360. int
  361. cmpush() {                /* Save the command environment */
  362.     char *cp;                /* Character pointer */
  363.  
  364.     if (cmddep >= CMDDEP)        /* Enter a new command depth */
  365.       return(-1);
  366.     cmddep++;
  367.     debug(F101,"&cmpush","",cmddep);
  368.  
  369. #ifdef DCMDBUF
  370.     /* allocate memory for cmp if not already done */
  371.     if (!cmp && !(cmp = (struct cmp *) malloc(sizeof(struct cmp)*(CMDDEP+1))))
  372.       fatal("cmpush: no memory for cmp");
  373.     cmp[cmddep].i[0] = cmflgs;        /* First do the global ints */
  374.     cmp[cmddep].i[1] = cmfsav;
  375.     cmp[cmddep].i[2] = atxn;
  376.     cmp[cmddep].i[3] = ungw;
  377.  
  378.     cmp[cmddep].c[0] = bp;        /* Then the global pointers */
  379.     cmp[cmddep].c[1] = pp;
  380.     cmp[cmddep].c[2] = np;
  381. #else
  382.     cmp_i[cmddep][0] = cmflgs;        /* First do the global ints */
  383.     cmp_i[cmddep][1] = cmfsav;
  384.     cmp_i[cmddep][2] = atxn;
  385.     cmp_i[cmddep][3] = ungw;
  386.  
  387.     cmp_c[cmddep][0] = bp;        /* Then the global pointers */
  388.     cmp_c[cmddep][1] = pp;
  389.     cmp_c[cmddep][2] = np;
  390. #endif /* DCMDBUF */
  391.  
  392.     /* Now the buffers themselves.  A lot of repititious code... */
  393.  
  394. #ifdef DCMDBUF
  395.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  396.     if (cp) strcpy(cp,cmdbuf);
  397.     cmp[cmddep].b[0] = cp;
  398.     if (cp == NULL) return(-1);
  399.  
  400.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  401.     if (cp) strcpy(cp,savbuf);
  402.     cmp[cmddep].b[1] = cp;
  403.     if (cp == NULL) return(-1);
  404.  
  405.     cp = malloc((int)strlen(hlpbuf)+1);    /* 2: Help string buffer */
  406.     if (cp) strcpy(cp,hlpbuf);
  407.     cmp[cmddep].b[2] = cp;
  408.     if (cp == NULL) return(-1);
  409.  
  410.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  411.     if (cp) strcpy(cp,atmbuf);
  412.     cmp[cmddep].b[3] = cp;
  413.     if (cp == NULL) return(-1);
  414.  
  415.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  416.     if (cp) strcpy(cp,atxbuf);
  417.     cmp[cmddep].b[4] = cp;
  418.     if (cp == NULL) return(-1);
  419.  
  420.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  421.     if (cp) strcpy(cp,atybuf);
  422.     cmp[cmddep].b[5] = cp;
  423.     if (cp == NULL) return(-1);
  424.  
  425.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  426.     if (cp) strcpy(cp,filbuf);
  427.     cmp[cmddep].b[6] = cp;
  428.     if (cp == NULL) return(-1);
  429. #else
  430.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  431.     if (cp) strcpy(cp,cmdbuf);
  432.     cmp_b[cmddep][0] = cp;
  433.     if (cp == NULL) return(-1);
  434.  
  435.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  436.     if (cp) strcpy(cp,savbuf);
  437.     cmp_b[cmddep][1] = cp;
  438.     if (cp == NULL) return(-1);
  439.  
  440.     cp = malloc((int)strlen(hlpbuf)+1);    /* 2: Help string buffer */
  441.     if (cp) strcpy(cp,hlpbuf);
  442.     cmp_b[cmddep][2] = cp;
  443.     if (cp == NULL) return(-1);
  444.  
  445.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  446.     if (cp) strcpy(cp,atmbuf);
  447.     cmp_b[cmddep][3] = cp;
  448.     if (cp == NULL) return(-1);
  449.  
  450.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  451.     if (cp) strcpy(cp,atxbuf);
  452.     cmp_b[cmddep][4] = cp;
  453.     if (cp == NULL) return(-1);
  454.  
  455.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  456.     if (cp) strcpy(cp,atybuf);
  457.     cmp_b[cmddep][5] = cp;
  458.     if (cp == NULL) return(-1);
  459.  
  460.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  461.     if (cp) strcpy(cp,filbuf);
  462.     cmp_b[cmddep][6] = cp;
  463.     if (cp == NULL) return(-1);
  464. #endif /* DCMDBUF */
  465.  
  466.     cmini(dpx);                /* Initize the command parser */
  467.     return(0);
  468. }
  469.  
  470. int
  471. cmpop() {                /* Restore the command environment */
  472.     debug(F101,"&cmpop","",cmddep);
  473.     if (cmddep < 0) return(-1);        /* Don't pop too much! */
  474.  
  475. #ifdef DCMDBUF
  476.     cmflgs = cmp[cmddep].i[0];        /* First do the global ints */
  477.     cmfsav = cmp[cmddep].i[1];
  478.     atxn = cmp[cmddep].i[2];
  479.     ungw = cmp[cmddep].i[3];
  480.  
  481.     bp = cmp[cmddep].c[0];        /* Then the global pointers */
  482.     pp = cmp[cmddep].c[1];
  483.     np = cmp[cmddep].c[2];
  484. #else
  485.     cmflgs = cmp_i[cmddep][0];        /* First do the global ints */
  486.     cmfsav = cmp_i[cmddep][1];
  487.     atxn = cmp_i[cmddep][2];
  488.     ungw = cmp_i[cmddep][3];
  489.  
  490.     bp = cmp_c[cmddep][0];        /* Then the global pointers */
  491.     pp = cmp_c[cmddep][1];
  492.     np = cmp_c[cmddep][2];
  493. #endif /* DCMDBUF */
  494.  
  495.     /* Now the buffers themselves. */
  496.  
  497. #ifdef DCMDBUF
  498.     if (cmp[cmddep].b[0]) {
  499.     strncpy(cmdbuf,cmp[cmddep].b[0],CMDBL); /* 0: Command buffer */
  500.     free(cmp[cmddep].b[0]);
  501.     cmp[cmddep].b[0] = NULL;
  502.     }
  503.     if (cmp[cmddep].b[1]) {
  504.     strncpy(savbuf,cmp[cmddep].b[1],CMDBL); /* 1: Save buffer */
  505.     free(cmp[cmddep].b[1]);
  506.     cmp[cmddep].b[1] = NULL;
  507.     }
  508.     if (cmp[cmddep].b[2]) {
  509.     strncpy(hlpbuf,cmp[cmddep].b[2],HLPBL); /* 2: Help buffer */
  510.     free(cmp[cmddep].b[2]);
  511.     cmp[cmddep].b[2] = NULL;
  512.     }
  513.     if (cmp[cmddep].b[3]) {
  514.     strncpy(atmbuf,cmp[cmddep].b[3],ATMBL); /* 3: Atomic buffer! */
  515.     free(cmp[cmddep].b[3]);
  516.     cmp[cmddep].b[3] = NULL;
  517.     }
  518.     if (cmp[cmddep].b[4]) {
  519.     strncpy(atxbuf,cmp[cmddep].b[4],ATMBL); /* 4: eXpansion buffer */
  520.     free(cmp[cmddep].b[4]);
  521.     cmp[cmddep].b[4] = NULL;
  522.     }
  523.     if (cmp[cmddep].b[5]) {
  524.     strncpy(atybuf,cmp[cmddep].b[5],ATMBL); /* 5: Atom buffer copY */
  525.     free(cmp[cmddep].b[5]);
  526.     cmp[cmddep].b[5] = NULL;
  527.     }
  528.     if (cmp[cmddep].b[6]) {
  529.     strncpy(filbuf,cmp[cmddep].b[6],ATMBL); /* 6: Filename buffer */
  530.     free(cmp[cmddep].b[6]);
  531.     cmp[cmddep].b[6] = NULL;
  532.     }
  533. #else
  534.     if (cmp_b[cmddep][0]) {
  535.     strncpy(cmdbuf,cmp_b[cmddep][0],CMDBL); /* 0: Command buffer */
  536.     free(cmp_b[cmddep][0]);
  537.     cmp_b[cmddep][0] = NULL;
  538.     }
  539.     if (cmp_b[cmddep][1]) {
  540.     strncpy(savbuf,cmp_b[cmddep][1],CMDBL); /* 1: Save buffer */
  541.     free(cmp_b[cmddep][1]);
  542.     cmp_b[cmddep][1] = NULL;
  543.     }
  544.     if (cmp_b[cmddep][2]) {
  545.     strncpy(hlpbuf,cmp_b[cmddep][2],HLPBL); /* 2: Help buffer */
  546.     free(cmp_b[cmddep][2]);
  547.     cmp_b[cmddep][2] = NULL;
  548.     }
  549.     if (cmp_b[cmddep][3]) {
  550.     strncpy(atmbuf,cmp_b[cmddep][3],ATMBL); /* 3: Atomic buffer! */
  551.     free(cmp_b[cmddep][3]);
  552.     cmp_b[cmddep][3] = NULL;
  553.     }
  554.     if (cmp_b[cmddep][4]) {
  555.     strncpy(atxbuf,cmp_b[cmddep][4],ATMBL); /* 4: eXpansion buffer */
  556.     free(cmp_b[cmddep][4]);
  557.     cmp_b[cmddep][4] = NULL;
  558.     }
  559.     if (cmp_b[cmddep][5]) {
  560.     strncpy(atybuf,cmp_b[cmddep][5],ATMBL); /* 5: Atom buffer copY */
  561.     free(cmp_b[cmddep][5]);
  562.     cmp_b[cmddep][5] = NULL;
  563.     }
  564.     if (cmp_b[cmddep][6]) {
  565.     strncpy(filbuf,cmp_b[cmddep][6],ATMBL); /* 6: Filename buffer */
  566.     free(cmp_b[cmddep][6]);
  567.     cmp_b[cmddep][6] = NULL;
  568.     }
  569. #endif /* DCMDBUF */
  570.  
  571.     cmddep--;                /* Rise, rise */
  572.     debug(F101,"&cmpop","",cmddep);
  573.     return(cmddep);
  574. }
  575. #endif /* NOSPL */
  576.  
  577. #ifdef COMMENT
  578. VOID
  579. stripq(s) char *s; {                    /* Function to strip '\' quotes */
  580.     char *t;
  581.     while (*s) {
  582.         if (*s == CMDQ) {
  583.             for (t = s; *t != '\0'; t++) *t = *(t+1);
  584.         }
  585.         s++;
  586.     }
  587. }
  588. #endif /* COMMENT */
  589.  
  590. /* Convert tabs to spaces, one for one */
  591. VOID
  592. untab(s) char *s; {
  593.     while (*s) {
  594.     if (*s == HT) *s = SP;
  595.     s++;
  596.     }
  597. }
  598.  
  599. /*  C M N U M  --  Parse a number in the indicated radix  */
  600.  
  601. /*
  602.  The only radix allowed in unquoted numbers is 10.
  603.  Parses unquoted numeric strings in base 10.
  604.  Parses backslash-quoted numbers in the radix indicated by the quote:
  605.    \nnn = \dnnn = decimal, \onnn = octal, \xnn = Hexadecimal.
  606.  If these fail, then if a preprocessing function is supplied, that is applied
  607.  and then a second attempt is made to parse an unquoted decimal string.
  608.  
  609.  Returns:
  610.    -3 if no input present when required,
  611.    -2 if user typed an illegal number,
  612.    -1 if reparse needed,
  613.     0 otherwise, with argument n set to the number that was parsed
  614. */
  615. int
  616. cmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; xx_strp f; {
  617.     int x; char *s, *zp, *zq;
  618.  
  619.     if (radix != 10) {                  /* Just do base 10 */
  620.         printf("cmnum: illegal radix - %d\n",radix);
  621.         return(-1);
  622.     }
  623.     x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  624.     debug(F101,"cmnum: cmfld","",x);
  625.     if (x < 0) return(x);        /* Parse a field */
  626.     zp = atmbuf;
  627.  
  628.     if (chknum(zp)) {            /* Check for decimal number */
  629.         *n = atoi(zp);            /* Got one, we're done. */
  630.     debug(F101,"cmnum 1st chknum ok","",*n);
  631.         return(0);
  632.     } else if ((x = xxesc(&zp)) > -1) {    /* Check for backslash escape */
  633.  
  634. #ifndef OS2
  635.     *n = x;
  636. #else
  637.     *n = wideresult;
  638. #endif /* OS2 */
  639.  
  640.     debug(F101,"cmnum xxesc ok","",*n);
  641.     return(*zp ? -2 : 0);
  642.     } else if (f) {            /* If conversion function given */
  643.         zp = atmbuf;            /* Try that */
  644.     zq = atxbuf;
  645.     atxn = CMDBL;
  646.     (*f)(zp,&zq,&atxn);        /* Convert */
  647.     zp = atxbuf;
  648.     }
  649.     debug(F110,"cmnum zp",zp,0);
  650.     if (chknum(zp)) {            /* Check again for decimal number */
  651.         *n = atoi(zp);            /* Got one, we're done. */
  652.     debug(F101,"cmnum 2nd chknum ok","",*n);
  653.         return(0);
  654.     } else {                /* Not numeric */
  655.     return(-2);
  656.     }
  657. }
  658.  
  659. /*  C M O F I  --  Parse the name of an output file  */
  660.  
  661. /*
  662.  Depends on the external function zchko(); if zchko() not available, use
  663.  cmfld() to parse output file names.
  664.  
  665.  Returns:
  666.    -9 like -2, except message already printed,
  667.    -3 if no input present when required,
  668.    -2 if permission would be denied to create the file,
  669.    -1 if reparse needed,
  670.     0 or 1 if file can be created, with xp pointing to name.
  671.     2 if given the name of an existing directory.
  672. */
  673. int
  674. cmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  675.     int x; char *s, *zq;
  676. #ifdef OS2
  677.     int tries;
  678. #endif /* OS2 */
  679. #ifdef DTILDE
  680.     _PROTOTYP( char * tilde_expand, (char *) );
  681.     char *dirp;
  682. #endif /* DTILDE */
  683.  
  684.     if (*xhlp == NUL) xhlp = "Output file";
  685.     *xp = "";
  686.  
  687.     if ((x = cmfld(xhlp,xdef,&s,(xx_strp)0)) < 0) return(x);
  688.     debug(F111,"cmofi 1",s,x);
  689.  
  690.     if (*s == '{') {            /* Strip enclosing braces */
  691.     int n;
  692.     n = strlen(s);
  693.     if (s[n-1] == '}') {
  694.         s[n-1] = NUL;
  695.         s++;
  696.     }
  697.     }
  698.     debug(F110,"cmofi 1.5",s,0);
  699.  
  700. #ifdef OS2
  701.     tries = 0;
  702.     {
  703.     char *p = s, q;
  704.     /*
  705.       This is really ugly.  If we skip conversion the first time through,
  706.       then variable names like \%a will be used as filenames (e.g. creating
  707.       a file called %A in the root directory).  If we DON'T skip conversion
  708.       the first time through, then single backslashes used as directory
  709.       separators in filenames will be misinterpreted as variable lead-ins.
  710.       So we prescan to see if it has any variable references.  But this
  711.       module is not supposed to know anything about variables, functions,
  712.       etc, so this code does not really belong here, but rather it should
  713.       be at the same level as zzstring().
  714.     */
  715.     while ( (tries == 0) && (p = strchr(p,CMDQ)) ) {
  716.         q = *(p+1);            /* Char after backslash */
  717.         if (!q)            /* None, quit */
  718.           break;
  719.         if (isupper(q))        /* If letter, convert to lowercase */
  720.           q = tolower(q);
  721.         if (isdigit(q)) {        /* If it's a digit, */
  722.         tries = 1;        /* assume it's a backslash code  */
  723.         break;
  724.         }
  725.         switch (q) {
  726.           case CMDQ:        /* Double backslash */
  727.         tries = 1;        /* so call the conversion function */
  728.         break;
  729.           case '%':            /* Variable or array reference */
  730.           case '&':            /* must be followed by letter */
  731.         if (isalpha(*(p+2)))
  732.           tries = 1;
  733.         break;
  734.           case 'm': case 'v': case '$': /* \m(), \v(), \$() */
  735.         if (*(p+2) == '(')
  736.           if (strchr(p+2,')'))
  737.             tries = 1;
  738.         break;
  739.           case 'f':            /* \Fname() */
  740.         if (strchr(p+2,'('))
  741.           if (strchr(p+2,')'))
  742.               tries = 1;
  743.         break;
  744.           case '{':            /* \{...} */
  745.         if (strchr(p+2,'}'))
  746.           tries = 1;
  747.         break;
  748.           case 'd': case 'o':    /* Decimal or Octal number */
  749.             if (isdigit(*(p+2)))
  750.           tries = 1;
  751.         break;
  752.           case 'x':            /* Hex number */
  753.         if (isdigit(*(p+2)) ||
  754.             ((*(p+2) >= 'a' && *(p+2) <= 'f') ||
  755.              ((*(p+2) >= 'A' && *(p+2) <= 'F'))))
  756.           tries = 1;
  757.           default:
  758.         break;
  759.         }
  760.         p++;
  761.     }
  762.     }
  763. o_again:
  764.     if (tries == 1)
  765. #endif /* OS2 */
  766.     if (f) {                /* If a conversion function is given */
  767.     zq = atxbuf;            /* do the conversion. */
  768.     atxn = CMDBL;
  769.     if ((x = (*f)(s,&zq,&atxn)) < 0)
  770.       return(-2);
  771.     s = atxbuf;
  772.     }
  773.     debug(F111,"cmofi 2",s,x);
  774.  
  775. #ifdef DTILDE
  776.     dirp = tilde_expand(s);        /* Expand tilde, if any, */
  777.     if (*dirp != '\0') setatm(dirp,1);    /* right in the atom buffer. */
  778.     s = atmbuf;
  779.     debug(F110,"cmofi 3",s,0);
  780. #endif /* DTILDE */
  781.  
  782.     if (iswild(s)) {
  783.         printf("?Wildcards not allowed - %s\n",s);
  784.         return(-2);
  785.     }
  786.     debug(F110,"cmofi 4",s,0);
  787.  
  788. #ifdef CK_TMPDIR
  789.     /* isdir() function required for this! */
  790.     if (isdir(s)) {
  791.     debug(F110,"cmofi 5: is directory",s,0);
  792.         *xp = s;
  793.     return(2);
  794.     }
  795. #endif /* CK_TMPDIR */
  796.  
  797.     if (strcmp(s,CTTNAM) && (zchko(s) < 0)) { /* OK to write to console */
  798. #ifdef COMMENT
  799. #ifdef OS2
  800. /*
  801.   We don't try again because we already prescanned the string to see if
  802.   if contained anything that could be used by zzstring().
  803. */
  804.     if (tries++ < 1)
  805.       goto o_again;
  806. #endif /* OS2 */
  807. #endif /* COMMENT */
  808.     debug(F110,"cmofi 6: failure",s,0);
  809.         printf("?Write permission denied - %s\n",s);
  810.         return(-9);
  811.     } else {
  812.     debug(F110,"cmofi 7: ok",s,0);
  813.         *xp = s;
  814.         return(x);
  815.     }
  816. }
  817.  
  818. /*  C M I F I  --  Parse the name of an existing file  */
  819.  
  820. /*
  821.  This function depends on the external functions:
  822.    zchki()  - Check if input file exists and is readable.
  823.    zxpand() - Expand a wild file specification into a list.
  824.    znext()  - Return next file name from list.
  825.  If these functions aren't available, then use cmfld() to parse filenames.
  826. */
  827. /*
  828.  Returns
  829.    -4 EOF
  830.    -3 if no input present when required,
  831.    -2 if file does not exist or is not readable,
  832.    -1 if reparse needed,
  833.     0 or 1 otherwise, with:
  834.         xp pointing to name,
  835.         wild = 1 if name contains '*' or '?', 0 otherwise.
  836. */
  837. int
  838. cmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  839.     return(cmifi2(xhlp,xdef,xp,wild,0,f));
  840. }
  841. /*
  842.   cmifi2() is for use when you also want to parse a directory or device
  843.   name as an input file, as in the DIRECTORY command.
  844. */
  845. int
  846. cmifi2(xhlp,xdef,xp,wild,d,f) char *xhlp,*xdef,**xp; int *wild, d; xx_strp f; {
  847.     int i, x, xc; long y; char *sp, *zq;
  848.     char *sv = NULL;
  849. #ifdef DTILDE
  850.     char *tilde_expand(), *dirp;
  851. #endif /* DTILDE */
  852. #ifdef OS2
  853.     int tries;
  854. #endif /* OS2 */
  855.  
  856. #ifndef NOPARTIAL
  857.     extern char *mtchs[];
  858. #endif /* NOPARTIAL */
  859.  
  860.     inword = cc = xc = 0;        /* Initialize counts & pointers */
  861.     *xp = "";
  862.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  863.         x = gtword();                   /* No, get a word */
  864.     } else {
  865.         setatm(xdef,0);            /* If so, use default, if any. */
  866.     }
  867.  
  868.     *xp = atmbuf;                       /* Point to result. */
  869.  
  870.     while (1) {
  871.         xc += cc;                       /* Count the characters. */
  872.         debug(F111,"cmifi gtword",atmbuf,xc);
  873.         switch (x) {
  874.             case -4:                    /* EOF */
  875.             case -2:                    /* Out of space. */
  876.             case -1:                    /* Reparse needed */
  877.                 return(x);
  878.             case 0:                     /* SP or NL */
  879.             case 1:
  880.                 if (xc == 0) *xp = xdef;     /* If no input, return default. */
  881.                 if (**xp == NUL) return(-3); /* If field empty, return -3. */
  882.  
  883.         if (**xp == '{') {    /* Strip enclosing braces first */
  884.             char *s = *xp;
  885.             int n;
  886.             n = strlen(s);
  887.             if (s[n-1] == '}') {
  888.             s[n-1] = NUL;
  889.             s++;
  890.             *xp = s;
  891.             }
  892.         }
  893. #ifdef OS2
  894.         if (d && ((int)strlen(*xp) == 2))
  895.           if (isalpha(**xp) && *(*xp + 1) == ':')
  896.             return(x);
  897.         tries = 0;
  898. i_again:
  899.         if (tries > 0)
  900. #endif /* OS2 */
  901.         if (f) {        /* If a conversion function is given */
  902.             zq = atxbuf;    /* ... */
  903.             atxn = CMDBL;
  904.             if ((y = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  905.             *xp = atxbuf;
  906.         }
  907.         debug(F110,"cmifi atxbuf",atxbuf,0);
  908.         if (sv) free(sv);
  909.         sv = malloc((int)strlen(*xp)+1); /* Make a safe copy */
  910.         if (!sv) {
  911.             printf("?malloc error 73, cmifi\n");
  912.             return(-9);
  913.         }
  914.         strcpy(sv,*xp);
  915.         debug(F110,"cmifi sv",sv,0);
  916.         y = zxpand(*xp);
  917.         *wild = (y > 1);
  918.         debug(F111,"cmifi sv wild",sv,*wild);
  919.         if (y == 0) {
  920. #ifdef OS2
  921.             if (tries++ < 1)
  922.               goto i_again;
  923. #endif /* OS2 */
  924.             if (sv) free(sv);
  925.             if (d) {
  926.             return(-2);
  927.             } else {
  928.             printf("?No files match - %s\n",*xp);
  929.             return(-9);
  930.             }
  931.         } else if (y < 0) {
  932.             printf("?Too many files match - %s\n",*xp);
  933.             if (sv) free(sv);
  934.             return(-9);
  935.         } else if (y > 1) return(x);
  936.  
  937.                 /* If not wild, see if it exists and is readable. */
  938.  
  939.         debug(F111,"cmifi sv not wild",sv,*wild);
  940.  
  941.         znext(*xp);        /* Get first (only?) matching file */
  942.                 y = zchki(*xp);        /* Check its accessibility */
  943.         zxpand(sv);        /* Rewind so next znext() gets 1st */
  944.         free(sv);        /* done with this */
  945.         sv = NULL;
  946.                 if (y == -3) {
  947.                     printf("?Read permission denied - %s\n",*xp);
  948.                     return(-9);
  949.                 } else if (y == -2) {
  950.             if (d) return(0);
  951.                     printf("?File not readable - %s\n",*xp);
  952.                     return(-9);
  953.                 } else if (y < 0) {
  954.                     printf("?File not found - %s\n",*xp);
  955.                     return(-9);
  956.                 }
  957.                 return(x);
  958.  
  959. #ifndef MAC
  960.             case 2:                     /* ESC */
  961.         debug(F101,"cmifi esc, xc","",xc);
  962. #ifdef OS2
  963.         tries = 0;
  964. #endif /* OS2 */
  965.                 if (xc == 0) {
  966.                     if (*xdef != '\0') {
  967.                         printf("%s ",xdef); /* If at beginning of field, */
  968. #ifdef GEMDOS
  969.             fflush(stdout);
  970. #endif /* GEMDOS */
  971.             inword = cmflgs = 0;
  972.                         addbuf(xdef);   /* supply default. */
  973.                         setatm(xdef,0);
  974.                     } else {            /* No default */
  975.                         putchar(BEL);
  976.                     }
  977.                     break;
  978.                 }
  979. #ifdef OS2
  980. e_again:
  981.         if (tries > 0)
  982. #endif /* OS2 */
  983.         if (f) {        /* If a conversion function is given */
  984.             zq = atxbuf;    /* ... */
  985.             atxn = CMDBL;
  986.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  987.                     /* reduce cc by number of \\ consumed by conversion */
  988.             /* function (needed for OS/2, where \ is path separator) */
  989.                     cc -= (strlen(*xp) - strlen(atxbuf));
  990.             *xp = atxbuf;
  991.         }
  992. #ifdef DTILDE
  993.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  994.         if (*dirp != '\0') setatm(dirp,0); /* in the atom buffer. */
  995.                 *xp = atmbuf;
  996. #endif /* DTILDE */
  997.                 sp = *xp + cc;
  998. #ifdef datageneral
  999.                 *sp++ = '+';        /* Data General AOS wildcard */
  1000. #else
  1001.                 *sp++ = '*';        /* Others */
  1002. #endif /* datageneral */
  1003.                 *sp-- = '\0';
  1004. #ifdef GEMDOS
  1005.         if (! strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  1006.           strcat(*xp, ".*");    /* abc -> abc*.* */
  1007. #endif /* GEMDOS */
  1008.                 y = zxpand(*xp);    /* Add wildcard and expand list. */
  1009.         if (y > 0) strcpy(filbuf,mtchs[0]);
  1010.         else *filbuf = '\0';
  1011.                 *sp = '\0';             /* Remove wildcard. */
  1012.         *wild = (y > 1);
  1013.                 if (y == 0) {
  1014. #ifdef OS2
  1015.             if (tries++ < 1)
  1016.               goto e_again;
  1017.             else
  1018. #endif /* OS2 */
  1019.               printf("?No files match - %s\n",atmbuf);
  1020.                     return(-9);
  1021.                 } else if (y < 0) {
  1022.                     printf("?Too many files match - %s\n",atmbuf);
  1023.                     return(-9);
  1024.                 } else if (y > 1) {     /* Not unique. */
  1025. #ifndef NOPARTIAL
  1026. /* Partial filename completion */
  1027.             int i, j, k; char c;
  1028.             k = 0;
  1029.             debug(F111,"cmifi partial",filbuf,cc);
  1030.             for (i = cc; (c = filbuf[i]); i++) {
  1031.             for (j = 1; j < y; j++)
  1032.               if (mtchs[j][i] != c) break;
  1033.             if (j == y) k++;
  1034.             else filbuf[i] = filbuf[i+1] = NUL;
  1035.             }
  1036.             debug(F111,"cmifi partial k",filbuf,k);
  1037.             if (k > 0) {    /* Got more characters */
  1038.             sp = filbuf + cc; /* Point to new ones */
  1039. #ifdef VMS
  1040.             for (i = 0; i < cc; i++) {
  1041.                 cmdchardel(); /* Back up over old partial spec */
  1042.                 bp--;
  1043.             }
  1044.             sp = filbuf;    /* Point to new word start */
  1045.             debug(F100,"cmifi vms erase ok","",0);
  1046. #endif /* VMS */
  1047.             cc = k;        /* How many new ones we just got */
  1048.             printf("%s",sp);        /* Print them */
  1049.             while (*bp++ = *sp++) ;    /* Copy to command buffer */
  1050.             bp--;                    /* Back up over NUL */
  1051.             debug(F110,"cmifi partial cmdbuf",cmdbuf,0);
  1052.             setatm(filbuf,0);
  1053.             debug(F111,"cmifi partial atmbuf",atmbuf,cc);
  1054.             *xp = atmbuf;
  1055.             }
  1056. #endif /* NOPARTIAL */
  1057.             putchar(BEL);    /* Beep because not unique. */
  1058.                 } else {                /* Unique, complete it.  */
  1059. #ifndef VMS
  1060. #ifdef CK_TMPDIR    
  1061.             /* isdir() function required for this! */
  1062.             if (isdir(filbuf)) {
  1063. #ifdef UNIX
  1064.             strcat(filbuf,"/");
  1065. #endif
  1066. #ifdef OS2
  1067.             strcat(filbuf,"/");
  1068. #endif
  1069. #ifdef AMIGA
  1070.             strcat(filbuf,"/");
  1071. #endif
  1072. #ifdef OSK
  1073.             strcat(filbuf,"/");
  1074. #endif
  1075. #ifdef datageneral
  1076.             strcat(filbuf,":");
  1077. #endif
  1078. #ifdef MAC
  1079.             strcat(filbuf,":");
  1080. #endif
  1081. #ifdef STRATUS
  1082.             strcat(filbuf,">");
  1083. #endif
  1084. #ifdef GEMDOS
  1085.             strcat(filbuf,"\\");
  1086. #endif
  1087.             sp = filbuf + cc;
  1088.             printf("%s%c",sp,BEL);
  1089.             cc++;
  1090.             while (*bp++ = *sp++) ;
  1091.             bp--;
  1092.             setatm(filbuf,0);
  1093.             debug(F111,"cmifi directory atmbuf",atmbuf,cc);
  1094.             *xp = atmbuf;
  1095.             } else { /* Not a directory... */
  1096. #endif /* CK_TMPDIR */
  1097. #endif /* VMS */
  1098.             sp = filbuf + cc; /* Point past what user typed. */
  1099. #ifdef VMS
  1100.             for (i = 0; i < cc; i++) {
  1101.                 cmdchardel(); /* Back up over old partial spec */
  1102.                 bp--;
  1103.             }
  1104.             sp = filbuf;    /* Point to new word start */
  1105. #endif /* VMS */
  1106.             printf("%s ",sp); /* Complete the name. */
  1107. #ifdef GEMDOS
  1108.             fflush(stdout);
  1109. #endif /* GEMDOS */
  1110.             addbuf(sp);    /* Add the characters to cmdbuf. */
  1111.             setatm(filbuf,0); /* And to atmbuf. */
  1112.             inword = cmflgs = 0;
  1113.             *xp = atmbuf;    /* Return pointer to atmbuf. */
  1114.             return(0);
  1115. #ifndef VMS
  1116. #ifdef CK_TMPDIR
  1117.             }
  1118. #endif /* CK_TMPDIR */
  1119. #endif /* VMS */
  1120.                 }
  1121.                 break;
  1122.  
  1123.             case 3:                     /* Question mark */
  1124. #ifdef OS2
  1125.         tries = 0;
  1126. #endif /* OS2 */
  1127.                 if (*xhlp == NUL)
  1128.           printf(" Input file specification");
  1129.                 else
  1130.           printf(" %s",xhlp);
  1131. #ifdef GEMDOS
  1132.         fflush(stdout);
  1133. #endif /* GEMDOS */
  1134.                 if (xc > 0) {
  1135. #ifdef OS2
  1136. q_again:
  1137.             if (tries > 0)
  1138. #endif /* OS2 */
  1139.             if (f) {        /* If a conversion function is given */
  1140.             zq = atxbuf;    /* ... */
  1141.             atxn = CMDBL;
  1142.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  1143.             *xp = atxbuf;
  1144.             }
  1145. #ifdef DTILDE
  1146.             dirp = tilde_expand(*xp);    /* Expand tilde, if any */
  1147.             if (*dirp != '\0') setatm(dirp,0);
  1148.             *xp = atmbuf;
  1149. #endif /* DTILDE */
  1150.             debug(F111,"cmifi ? *xp, cc",*xp,cc);
  1151.                     sp = *xp + cc;    /* Insert "*" at end */
  1152. #ifdef datageneral
  1153.                     *sp++ = '+';        /* Insert +, the DG wild card */
  1154. #else
  1155.                     *sp++ = '*';
  1156. #endif /* datageneral */
  1157.                     *sp-- = '\0';
  1158. #ifdef GEMDOS
  1159.             if (! strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  1160.               strcat(*xp, ".*");    /* abc -> abc*.* */
  1161. #endif /* GEMDOS */
  1162.             debug(F110,"cmifi ? wild",*xp,0);
  1163.                     y = zxpand(*xp);
  1164.                     *sp = '\0';
  1165.                     if (y == 0) {
  1166. #ifdef OS2
  1167.             if (tries++ < 1)
  1168.               goto q_again;
  1169.             else
  1170. #endif /* OS2 */
  1171.               printf("?No files match - %s\n",atmbuf);
  1172.                         return(-9);
  1173.                     } else if (y < 0) {
  1174.                         printf("?Too many files match - %s\n",atmbuf);
  1175.                         return(-9);
  1176.                     } else {
  1177.                         printf(", one of the following:\n");
  1178.                         clrhlp();
  1179.                         for (i = 0; i < y; i++) {
  1180.                             znext(filbuf);
  1181. #ifdef VMS
  1182.                 printf(" %s\n",filbuf); /* VMS names can be long */
  1183. #else
  1184.                             addhlp(filbuf);
  1185. #endif /* VMS */
  1186.                         }
  1187.                         dmphlp();
  1188.                     }
  1189.                 } else printf("\n");
  1190.                 printf("%s%s",cmprom,cmdbuf);
  1191.         fflush(stdout);
  1192.                 break;
  1193. #endif /* MAC */
  1194.         }
  1195.     x = gtword();
  1196.     *xp = atmbuf;
  1197.     }
  1198. }
  1199.  
  1200. /*  C M D I R  --  Parse a directory specification  */
  1201.  
  1202. /*
  1203.  This function depends on the external functions:
  1204.    zchki()  - Check if input file exists and is readable.
  1205.  If these functions aren't available, then use cmfld() to parse dir names.
  1206.  Note: this function quickly cobbled together, mainly by deleting lots of
  1207.  lines from cmifi().  It seems to work, but various services are missing,
  1208.  like completion, lists of matching directories on "?", etc.
  1209. */
  1210. /*
  1211.  Returns
  1212.    -4 EOF
  1213.    -3 if no input present when required,
  1214.    -2 if out of space or other internal error,
  1215.    -1 if reparse needed,
  1216.     0 or 1, with xp pointing to name, if directory specified,
  1217.     2 if a wildcard was included.
  1218. */
  1219. int
  1220. cmdir(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1221.     int x, xc; char *zq;
  1222. #ifdef DTILDE
  1223.     char *tilde_expand(), *dirp;
  1224. #endif /* DTILDE */
  1225.  
  1226.     inword = cc = xc = 0;        /* Initialize counts & pointers */
  1227.     *xp = "";
  1228.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  1229.         x = gtword();                   /* No, get a word */
  1230.     } else {
  1231.         setatm(xdef,0);            /* If so, use default, if any. */
  1232.     }
  1233.     *xp = atmbuf;                       /* Point to result. */
  1234.     while (1) {
  1235.         xc += cc;                       /* Count the characters. */
  1236.         debug(F111,"cmdir gtword",atmbuf,xc);
  1237.         switch (x) {
  1238.             case -4:                    /* EOF */
  1239.             case -2:                    /* Out of space. */
  1240.             case -1:                    /* Reparse needed */
  1241.                 return(x);
  1242.             case 0:                     /* SP or NL */
  1243.             case 1:
  1244.                 if (xc == 0) *xp = xdef;     /* If no input, return default. */
  1245.         else *xp = atmbuf;
  1246.                 if (**xp == NUL) return(-3); /* If field empty, return -3. */
  1247.  
  1248.         if (**xp == '{') {    /* Strip enclosing braces first */
  1249.             char *s = *xp;
  1250.             int n;
  1251.             n = strlen(s);
  1252.             if (s[n-1] == '}') {
  1253.             s[n-1] = NUL;
  1254.             s++;
  1255.             *xp = s;
  1256.             }
  1257.         }
  1258. #ifndef OS2
  1259.         if (f) {        /* If a conversion function is given */
  1260.             zq = atxbuf;    /* ... */
  1261.             atxn = CMDBL;
  1262.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  1263.             *xp = atxbuf;
  1264.             cc = (int)strlen(atxbuf);
  1265.         }
  1266. #ifdef DTILDE
  1267.         dirp = tilde_expand(*xp); /* Expand tilde, if any, */
  1268.         if (*dirp == '~') {    /* Still starts with tilde? */
  1269.             char *tp;        /* Yes, convert to lowercase */
  1270.             tp = *xp;        /* and try again. */
  1271.             while (*tp) {
  1272.             if (isupper(*tp)) *tp = tolower(*tp);
  1273.             tp++;
  1274.             }
  1275.         }
  1276.         dirp = tilde_expand(*xp); /* Expand tilde, if any, */
  1277. #ifdef COMMENT
  1278.         if (*dirp != '\0') setatm(dirp,0); /* in the atom buffer. */
  1279. #else
  1280.         /* This allows for directory names that contain spaces. */
  1281.         if (*dirp != '\0')
  1282.           strcpy(atmbuf,dirp);
  1283.         *xp = atmbuf;
  1284. #endif /* COMMENT */
  1285. #endif /* DTILDE */
  1286. #else  /* OS2 */
  1287.         if (isdir(*xp)) {    /* OS/2 version has this function */
  1288.             return(x);
  1289.         } else {
  1290.             if (f) {        /* If a conversion function is given */
  1291.             zq = atxbuf;    /* ... */
  1292.             atxn = CMDBL;
  1293.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  1294.             *xp = atxbuf;
  1295.             cc = (int)strlen(atxbuf);
  1296.             }
  1297.         }
  1298. #endif /* OS2 */
  1299.         if (iswild(*xp)) return(2);
  1300.         else return(x);
  1301.  
  1302.             case 2:                     /* ESC */
  1303.         putchar(BEL);
  1304.         break;
  1305.  
  1306.             case 3:                     /* Question mark */
  1307.                 if (*xhlp == NUL)
  1308.           printf(" Directory name");
  1309.                 else
  1310.           printf(" %s",xhlp);
  1311.                 printf("\n%s%s",cmprom,cmdbuf);
  1312.         fflush(stdout);
  1313.                 break;
  1314.         }
  1315.     x = gtword();
  1316. /*  *xp = atmbuf;  */
  1317.     }
  1318. }
  1319.  
  1320. /*  C M F L D  --  Parse an arbitrary field  */
  1321. /*
  1322.  Returns
  1323.    -3 if no input present when required,
  1324.    -2 if field too big for buffer,
  1325.    -1 if reparse needed,
  1326.     0 otherwise, xp pointing to string result.
  1327. */
  1328. int
  1329. cmfld(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1330.     int x, xc;
  1331.     char *zq;
  1332.  
  1333.     inword = cc = xc = 0;        /* Initialize counts & pointers */
  1334.     *xp = "";
  1335.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  1336.         x = gtword();                   /* No, get a word */
  1337.     } else {
  1338.         setatm(xdef,0);            /* If so, use default, if any. */
  1339.     }
  1340.     *xp = atmbuf;                       /* Point to result. */
  1341.  
  1342.     while (1) {
  1343.         xc += cc;                       /* Count the characters. */
  1344.         debug(F111,"cmfld: gtword",atmbuf,xc);
  1345.         debug(F101,"cmfld x","",x);
  1346.         switch (x) {
  1347.             case -4:                    /* EOF */
  1348.             case -2:                    /* Out of space. */
  1349.             case -1:                    /* Reparse needed */
  1350.                 return(x);
  1351.             case 0:                     /* SP or NL */
  1352.             case 1:
  1353.                 if (xc == 0)         /* If no input, return default. */
  1354.           setatm(xdef,0);
  1355.         *xp = atmbuf;
  1356.         if (f) {        /* If a conversion function is given */
  1357.             int n;        /* employ it now. */
  1358.             zq = atxbuf;
  1359.             atxn = CMDBL;
  1360.             if ((*f)(*xp,&zq,&atxn) < 0)
  1361.               return(-2);
  1362.             setatm(atxbuf,0);
  1363.             *xp = atmbuf;
  1364.         }
  1365.                 if (**xp == NUL) {    /* If variable evaluates to null */
  1366.             setatm(xdef,0);    /* Stick in the default again. */
  1367.             if (**xp == NUL) x = -3; /* If still empty, return -3. */
  1368.         }
  1369. #ifdef COMMENT
  1370. /* The following is apparently not necessary. */
  1371. /* Remove it if nothing is broken, esp. TAKE file with trailing comments */
  1372.         xx = *xp;
  1373.         debug(F111,"cmfld before trim",*xp,x);
  1374.         for (i = (int)strlen(xx) - 1; i > 0; i--)
  1375.           if (xx[i] != SP)    /* Trim trailing blanks */
  1376.             break;
  1377.           else
  1378.             xx[i] = NUL;
  1379. #endif /* COMMENT */
  1380.         debug(F111,"cmfld returns",*xp,x);
  1381.                 return(x);
  1382.             case 2:                     /* ESC */
  1383.                 if (xc == 0 && *xdef != NUL) {
  1384.                     printf("%s ",xdef); /* If at beginning of field, */
  1385. #ifdef GEMDOS
  1386.             fflush(stdout);
  1387. #endif /* GEMDOS */
  1388.                     addbuf(xdef);       /* supply default. */
  1389.             inword = cmflgs = 0;
  1390.                     setatm(xdef,0);    /* Return as if whole field */
  1391.                     return(0);          /* typed, followed by space. */
  1392.                 } else {
  1393.                     putchar(BEL);       /* Beep if already into field. */
  1394.                 }
  1395.                 break;
  1396.             case 3:                     /* Question mark */
  1397.                 if (*xhlp == NUL)
  1398.                     printf(" Please complete this field");
  1399.                 else
  1400.                     printf(" %s",xhlp);
  1401.                 printf("\n%s%s",cmprom,cmdbuf);
  1402.         fflush(stdout);
  1403.                 break;
  1404.         }
  1405.     x = gtword();
  1406. /*  *xp = atmbuf; */
  1407.     }
  1408. }
  1409.  
  1410.  
  1411. /*  C M T X T  --  Get a text string, including confirmation  */
  1412.  
  1413. /*
  1414.   Print help message 'xhlp' if ? typed, supply default 'xdef' if null
  1415.   string typed.  Returns
  1416.  
  1417.    -1 if reparse needed or buffer overflows.
  1418.     1 otherwise.
  1419.  
  1420.   with cmflgs set to return code, and xp pointing to result string.
  1421. */
  1422. int
  1423. cmtxt(xhlp,xdef,xp,f) char *xhlp; char *xdef; char **xp; xx_strp f; {
  1424.  
  1425.     int x, i;
  1426.     char *xx, *zq;
  1427.     static int xc;
  1428.  
  1429.     debug(F101,"cmtxt, cmflgs","",cmflgs);
  1430.     inword = cc = 0;            /* Start atmbuf counter off at 0 */
  1431.     if (cmflgs == -1) {                 /* If reparsing, */
  1432.         xc = (int)strlen(*xp);        /* get back the total text length, */
  1433.     } else {                            /* otherwise, */
  1434.         *xp = "";                       /* start fresh. */
  1435.         xc = 0;
  1436.     }
  1437.     *atmbuf = NUL;                      /* And empty the atom buffer. */
  1438.     if ((x = cmflgs) != 1) {
  1439.         x = gtword();                   /* Get first word. */
  1440.         *xp = pp;                       /* Save pointer to it. */
  1441.     }
  1442.     debug(F101,"cmtxt (*f)","", f);
  1443.     while (1) {                /* Loop for each word in text. */
  1444.         xc += cc;                       /* Char count for all words. */
  1445.         debug(F111,"cmtxt: gtword",atmbuf,xc);
  1446.         debug(F101," x","",x);
  1447.         switch (x) {
  1448.       case -9:            /* Buffer overflow */
  1449.       case -4:            /* EOF */
  1450. #ifdef MAC
  1451.       case -3:            /* Quit/Timeout */
  1452. #endif /* MAC */
  1453.       case -2:            /* Overflow */
  1454.       case -1:            /* Deletion */
  1455.         return(x);
  1456.       case 0:            /* Space */
  1457.         xc++;            /* Just count it */
  1458.         break;
  1459.       case 1:            /* CR or LF */
  1460.         if (xc == 0) *xp = xdef;
  1461.         if (f) {            /* If a conversion function is given */
  1462.         zq = atxbuf;        /* Point to the expansion buffer */
  1463.         atxn = CMDBL;        /* specify its length */
  1464.         debug(F110,"cmtxt calling (*f)",*xp,0);
  1465.         if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  1466.         cc = (int)strlen(atxbuf);
  1467.         *xp = atxbuf;        /* and return pointer to it. */
  1468.         debug(F111,"cmtxt (*f) returns",*xp,cc);
  1469.         }
  1470.         xx = *xp;
  1471.         for (i = (int)strlen(xx) - 1; i > 0; i--)
  1472.           if (xx[i] != SP)        /* Trim trailing blanks */
  1473.         break;
  1474.           else
  1475.         xx[i] = NUL;
  1476.         return(x);
  1477.       case 2:            /* ESC */
  1478.         if (xc == 0) {
  1479.         printf("%s ",xdef);
  1480.         inword = cmflgs = 0;
  1481. #ifdef GEMDOS
  1482.         fflush(stdout);
  1483. #endif /* GEMDOS */
  1484.         cc = addbuf(xdef);
  1485.         } else {
  1486.         putchar(BEL);
  1487.         }
  1488.         break;
  1489.       case 3:            /* Question Mark */
  1490.         if (*xhlp == NUL)
  1491.           printf(" Text string");
  1492.         else
  1493.           printf(" %s",xhlp);
  1494.         printf("\n%s%s",cmprom,cmdbuf);
  1495.         fflush(stdout);
  1496.         break;
  1497.       default:
  1498.         printf("?Unexpected return code from gtword() - %d\n",x);
  1499.         return(-2);
  1500.         }
  1501.         x = gtword();
  1502.     }
  1503. }
  1504.  
  1505. /*  C M K E Y  --  Parse a keyword  */
  1506.  
  1507. /*
  1508.  Call with:
  1509.    table    --  keyword table, in 'struct keytab' format;
  1510.    n        --  number of entries in table;
  1511.    xhlp     --  pointer to help string;
  1512.    xdef     --  pointer to default keyword;
  1513.  
  1514.  Returns:
  1515.    -3       --  no input supplied and no default available
  1516.    -2       --  input doesn't uniquely match a keyword in the table
  1517.    -1       --  user deleted too much, command reparse required
  1518.     n >= 0  --  value associated with keyword
  1519. */
  1520. int
  1521. cmkey(table,n,xhlp,xdef,f)
  1522. /* cmkey */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  1523.     return(cmkey2(table,n,xhlp,xdef,"",f));
  1524. }
  1525. int
  1526. cmkey2(table,n,xhlp,xdef,tok,f)
  1527.     struct keytab table[]; int n; char *xhlp, *xdef; char *tok; xx_strp f; {
  1528.  
  1529.     int i, tl, y, z, zz, xc;
  1530.     char *xp, *zq;
  1531.  
  1532.     tl = (int)strlen(tok);
  1533.     inword = xc = cc = 0;        /* Clear character counters. */
  1534.  
  1535.     if ((zz = cmflgs) == 1)             /* Command already entered? */
  1536.       setatm(xdef,0);            /* Yes, copy default into atom buf */
  1537.     else zz = gtword();            /* Otherwise get a command word */
  1538.  
  1539. debug(F101,"cmkey: table length","",n);
  1540. debug(F101," cmflgs","",cmflgs);
  1541. debug(F101," zz","",zz);
  1542. while (1) {
  1543.     xc += cc;
  1544.     debug(F111,"cmkey: gtword",atmbuf,xc);
  1545.  
  1546.     switch(zz) {
  1547.         case -4:                        /* EOF */
  1548. #ifdef MAC
  1549.     case -3:            /* Quit/Timeout */
  1550. #endif /* MAC */
  1551.         case -2:                        /* Buffer overflow */
  1552.         case -1:                        /* Or user did some deleting. */
  1553.             return(cmflgs = zz);
  1554.  
  1555.         case 0:                         /* User terminated word with space */
  1556.         case 1:                         /* or newline */
  1557.             if (cc == 0) setatm(xdef,0); /* Supply default if we got nothing */
  1558.         if (f) {            /* If a conversion function is given */
  1559.         zq = atxbuf;        /* apply it */
  1560.         atxn = CMDBL;
  1561.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  1562.         debug(F110,"cmkey atxbuf after *f",atxbuf,0);
  1563.         setatm(atxbuf,0);
  1564.         }
  1565.             y = lookup(table,atmbuf,n,&z); /* Look up the word in the table */
  1566.             switch (y) {
  1567.                 case -2:        /* Ambiguous */
  1568.                     printf("?Ambiguous - %s\n",atmbuf);
  1569.             cmflgs = -2;
  1570.                     return(-9);
  1571.                 case -1:        /* Not found at all */
  1572.             if (tl) {
  1573.             for (i = 0; i < tl; i++) /* Check for token */
  1574.               if (tok[i] == *atmbuf) { /* Got one */
  1575.                   ungword();  /* Put back the following word */
  1576.                   return(-5); /* Special return code for token */
  1577.               }
  1578.             }
  1579.             /* Kludge alert... only print error if */
  1580.             /* we were called as cmkey2, but not cmkey... */
  1581.             /* This doesn't seem to always work. */
  1582.             if (tl == 0) {
  1583.             printf("?No keywords match - %s\n",atmbuf); /* cmkey */
  1584.             return(cmflgs = -9);
  1585.             } else {
  1586.             if (cmflgs == 1) return(cmflgs = -6); /* cmkey2 */
  1587.             else return(cmflgs = -2);
  1588.             /* The -6 code is to let caller try another table */
  1589.             }
  1590.                 default:
  1591.                     break;
  1592.         }
  1593.             return(y);
  1594.  
  1595.         case 2:                         /* User terminated word with ESC */
  1596.             if (cc == 0) {
  1597.                 if (*xdef != NUL) {     /* Nothing in atmbuf */
  1598.                     printf("%s ",xdef); /* Supply default if any */
  1599. #ifdef GEMDOS
  1600.             fflush(stdout);
  1601. #endif /* GEMDOS */
  1602.                     addbuf(xdef);
  1603.                     setatm(xdef,0);
  1604.             inword = cmflgs = 0;
  1605.                     debug(F111,"cmkey: default",atmbuf,cc);
  1606.                 } else {
  1607.                     putchar(BEL);       /* No default, just beep */
  1608.                     break;
  1609.                 }
  1610.             }
  1611.         if (f) {            /* If a conversion function is given */
  1612.         zq = atxbuf;        /* apply it */
  1613.         atxn = CMDBL;
  1614.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  1615.         setatm(atxbuf,0);
  1616.         }
  1617.             y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */
  1618.             debug(F111,"cmkey: esc",atmbuf,y);
  1619.             if (y == -2) {        /* Ambiguous */
  1620.                 putchar(BEL);
  1621.                 break;
  1622.             }
  1623.             if (y == -1) {        /* Not found */
  1624.                 /* if (tl == 0) */ printf("?No keywords match - %s\n",atmbuf);
  1625.         cmflgs = -2;
  1626.                 return(-9);
  1627.             }
  1628. /*
  1629.   See if the keyword just found has the CM_ABR bit set in its flgs field, and
  1630.   if so, search forwards in the table for a keyword that has the same kwval
  1631.   but does not have CM_ABR (or CM_INV?) set, and then expand using the full
  1632.   keyword.  WARNING: This assumes that (a) keywords are in alphabetical order,
  1633.   and (b) the CM_ABR bit is set only if the the abbreviated keyword is a true
  1634.   abbreviation (left substring) of the full keyword.
  1635. */
  1636.         if (test(table[z].flgs,CM_ABR)) {
  1637.         int zz;
  1638.         for (zz = z+1; zz < n; zz++)
  1639.           if ((table[zz].kwval == table[z].kwval) &&
  1640.               (!test(table[zz].flgs,CM_ABR))) {
  1641.               z = zz;
  1642.               break;
  1643.           }
  1644.             }
  1645.             xp = table[z].kwd + cc;
  1646.             printf("%s ",xp);
  1647. #ifdef GEMDOS
  1648.         fflush(stdout);
  1649. #endif /* GEMDOS */
  1650.             addbuf(xp);
  1651.         inword = cmflgs = 0;
  1652.             debug(F110,"cmkey: addbuf",cmdbuf,0);
  1653.             return(y);
  1654.  
  1655.         case 3:                         /* User typed "?" */
  1656.         if (f) {            /* If a conversion function is given */
  1657.         zq = atxbuf;        /* do the conversion now. */
  1658.         atxn = CMDBL;
  1659.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  1660.         setatm(atxbuf,0);
  1661.         }
  1662.             y = lookup(table,atmbuf,n,&z); /* Look up what we have so far. */
  1663.  
  1664.         if (y == -1) {
  1665.                 /* if (tl == 0) */ printf(" No keywords match\n");
  1666.         cmflgs = -2;
  1667.                 return(-9);
  1668.             }
  1669.             if (*xhlp == NUL)
  1670.                 printf(" One of the following:\n");
  1671.             else
  1672.                 printf(" %s, one of the following:\n",xhlp);
  1673.  
  1674.         if ((y > -1) &&
  1675.         !test(table[z].flgs,CM_ABR) &&
  1676.         ((z >= n-1) || strncmp(table[z].kwd,table[z+1].kwd,cc))
  1677.          ) {
  1678.         printf(" %s\n",table[z].kwd);
  1679.         } else {
  1680.         clrhlp();
  1681.         for (i = 0; i < n; i++) {
  1682.             if (!strncmp(table[i].kwd,atmbuf,cc)
  1683.             && !test(table[i].flgs,CM_INV)
  1684.             )
  1685.               addhlp(table[i].kwd);
  1686.         }
  1687.         dmphlp();
  1688.         }
  1689.         if (*atmbuf == NUL) {
  1690.         if (tl == 1)
  1691.           printf("or the token %c\n",*tok);
  1692.         else if (tl > 1) printf("or one of the tokens: %s\n",tok);
  1693.         }
  1694.             printf("%s%s", cmprom, cmdbuf);
  1695.         fflush(stdout);
  1696.             break;
  1697.  
  1698.         default:
  1699.             printf("\n%d - Unexpected return code from gtword\n",zz);
  1700.             return(cmflgs = -2);
  1701.         }
  1702.         zz = gtword();
  1703.     }
  1704. }
  1705. int
  1706. chktok(tlist) char *tlist; {
  1707.     char *p;
  1708.     p = tlist;
  1709.     while (*p != NUL && *p != *atmbuf) p++;
  1710.     return((*p) ? (int) *p : 0);
  1711. }
  1712.  
  1713. /*  C M C F M  --  Parse command confirmation (end of line)  */
  1714.  
  1715. /*
  1716.  Returns
  1717.    -2: User typed anything but whitespace or newline
  1718.    -1: Reparse needed
  1719.     0: Confirmation was received
  1720. */
  1721. int
  1722. cmcfm() {
  1723.     int x, xc;
  1724.  
  1725.     debug(F101,"cmcfm: cmflgs","",cmflgs);
  1726.     debug(F110,"cmcfm: atmbuf",atmbuf,0);
  1727.     inword = xc = cc = 0;
  1728.     if (cmflgs == 1) return(0);
  1729.  
  1730.     setatm("",0);            /* (Probably unnecessary) */
  1731.  
  1732.     while (1) {
  1733.         x = gtword();
  1734.         xc += cc;
  1735.         switch (x) {
  1736.             case -4:                    /* EOF */
  1737.             case -2:
  1738.             case -1:
  1739.                 return(x);
  1740.  
  1741.             case 1:                     /* End of line */
  1742.                 if (xc > 0) {
  1743.                     printf("?Not confirmed - %s\n",atmbuf);
  1744.                     return(-9);
  1745.                 } else return(0);
  1746.             case 2:            /* ESC */
  1747.         if (xc == 0) {
  1748.             putchar(BEL);    /* beep & continue */
  1749.             continue;        /* or fall thru. */
  1750.         }
  1751.             case 0:                     /* Space */
  1752.         if (xc == 0)        /* If no chars typed, continue, */
  1753.           continue;        /* else fall thru. */
  1754.             case 3:            /* Question mark */
  1755.                 if (xc > 0) {
  1756.                     printf("?Not confirmed - %s\n",atmbuf);
  1757.                     return(-9);
  1758.                 }
  1759.                 printf("\n Type a carriage return to confirm the command\n");
  1760.                 printf("%s%s",cmprom,cmdbuf);
  1761.         fflush(stdout);
  1762.                 continue;
  1763.         }
  1764.     }
  1765. }
  1766.  
  1767. /* Keyword help routines */
  1768.  
  1769.  
  1770. /*  C L R H L P -- Initialize/Clear the help line buffer  */
  1771.  
  1772. static VOID
  1773. clrhlp() {                              /* Clear the help buffer */
  1774.     hlpbuf[0] = NUL;
  1775.     hh = hx = 0;
  1776. }
  1777.  
  1778.  
  1779. /*  A D D H L P  --  Add a string to the help line buffer  */
  1780.  
  1781. static VOID
  1782. addhlp(s) char *s; {                    /* Add a word to the help buffer */
  1783.     int j;
  1784.  
  1785.     hh++;                               /* Count this column */
  1786.  
  1787.     for (j = 0; (j < hc) && (*s != NUL); j++) { /* Fill the column */
  1788.         hlpbuf[hx++] = *s++;
  1789.     }
  1790.     if (*s != NUL)                      /* Still some chars left in string? */
  1791.         hlpbuf[hx-1] = '+';             /* Mark as too long for column. */
  1792.  
  1793.     if (hh < (hw / hc)) {               /* Pad col with spaces if necessary */
  1794.         for (; j < hc; j++) {
  1795.             hlpbuf[hx++] = SP;
  1796.         }
  1797.     } else {                            /* If last column, */
  1798.         hlpbuf[hx++] = NUL;             /* no spaces. */
  1799.         dmphlp();                       /* Print it. */
  1800.         return;
  1801.     }
  1802. }
  1803.  
  1804.  
  1805. /*  D M P H L P  --  Dump the help line buffer  */
  1806.  
  1807. static VOID
  1808. dmphlp() {                              /* Print the help buffer */
  1809.     hlpbuf[hx++] = NUL;
  1810.     printf(" %s\n",hlpbuf);
  1811.     clrhlp();
  1812. }
  1813.  
  1814.  
  1815. /*  G T W O R D  --  Gets a "word" from the command input stream  */
  1816.  
  1817. /*
  1818. Usage: retcode = gtword();
  1819.  
  1820. Returns:
  1821.  -4 if end of file (e.g. pipe broken)
  1822.  -2 if command buffer overflows
  1823.  -1 if user did some deleting
  1824.   0 if word terminates with SP or tab
  1825.   1 if ... CR
  1826.   2 if ... ESC
  1827.   3 if ... ? (question mark)
  1828.  
  1829. With:
  1830.   pp pointing to beginning of word in buffer
  1831.   bp pointing to after current position
  1832.   atmbuf containing a copy of the word
  1833.   cc containing the number of characters in the word copied to atmbuf
  1834. */
  1835.  
  1836. int
  1837. ungword() {                /* unget a word */
  1838.     if (ungw) return(0);
  1839.     cmfsav = cmflgs;
  1840.     debug(F101,"ungword cmflgs","",cmflgs);
  1841.     ungw = 1;
  1842.     cmflgs = 0;
  1843.     return(0);
  1844. }
  1845.  
  1846. static int
  1847. gtword() {
  1848.     int c;                              /* Current char */
  1849.     int quote = 0;                      /* Flag for quote character */
  1850.     int echof = 0;                      /* Flag for whether to echo */
  1851.     int chsrc = 0;            /* Source of character, 1 = tty */
  1852.     int comment = 0;            /* Flag for in comment */
  1853.     char *cp = NULL;            /* Comment pointer */
  1854.     int eintr = 0;
  1855.     int    brack_knt = 0;            /* nested bracket counter [jrs]        */
  1856.  
  1857. #ifdef RTU
  1858.     extern int rtu_bug;
  1859. #endif /* RTU */
  1860.  
  1861. #ifdef datageneral
  1862.     extern int termtype;                /* DG terminal type flag */
  1863.     extern int con_reads_mt;            /* Console read asynch is active */
  1864.     if (con_reads_mt) connoi_mt();      /* Task would interfere w/cons read */
  1865. #endif /* datageneral */
  1866.  
  1867.     if (ungw) {                /* Have a word saved? */
  1868.     debug(F110,"gtword ungetting from pp",pp,0);
  1869.     while (*pp++ == SP) ;
  1870.     setatm(pp,0);
  1871.     strcpy(atmbuf,pp);
  1872.     ungw = 0;
  1873.     cmflgs = cmfsav;
  1874.     debug(F111,"gtword returning atmbuf",atmbuf,cmflgs);
  1875.     return(cmflgs);
  1876.     }
  1877.     pp = np;                            /* Start of current field */
  1878.  
  1879.     debug(F111,"gtword: cmdbuf",cmdbuf,cmdbuf);
  1880.     debug(F111," bp",bp,bp);
  1881.     debug(F111," pp",pp,pp);
  1882.  
  1883.     while (bp < cmdbuf+CMDBL) {         /* Big get-a-character loop */
  1884.     echof = 0;            /* Assume we don't echo because */
  1885.     chsrc = 0;            /* character came from reparse buf. */
  1886.  
  1887.         if ((c = *bp) == NUL) {         /* If no char waiting in reparse buf */
  1888.             if (dpx) echof = 1;         /* must get from tty, set echo flag. */
  1889.         c = cmdgetc();        /* Read a character from the tty. */
  1890.         chsrc = 1;            /* Remember character source is tty. */
  1891. #ifdef MAC
  1892.         if (c == -3)        /* If null command... */
  1893.           return(-3);
  1894. #endif /* MAC */
  1895.             if (c == EOF) {        /* This can happen if stdin not tty. */
  1896. #ifdef EINTR
  1897. /*
  1898.   Some operating and/or C runtime systems return EINTR for no good reason,
  1899.   when the end of the standard input "file" is encountered.  In cases like
  1900.   this, we get into an infinite loop; hence the eintr counter, which is reset
  1901.   to 0 upon each call to this routine.
  1902. */
  1903.         debug(F101,"gtword EOF","",errno);
  1904.         if (errno == EINTR && ++eintr < 4) /* When bg'd process is */
  1905.           continue;        /* fg'd again. */
  1906. #endif /* EINTR */
  1907.         return(-4);
  1908.         }
  1909.         c &= cmdmsk;        /* Strip any parity bit */
  1910.     }                /* if desired. */
  1911.  
  1912. /* Now we have the next character */
  1913.  
  1914.         if (quote == 0) {        /* If this is not a quoted character */
  1915.             if (c == CMDQ) {        /* Got the quote character itself */
  1916.         if (!comment && quoting)
  1917.           quote = 1;        /* Flag it if not in a comment */
  1918.             }
  1919.         if (c == FF) {        /* Formfeed. */
  1920.                 c = NL;                 /* Replace with newline */
  1921. #ifdef COMMENT
  1922. /* No more screen clearing... */
  1923.         cmdclrscn();        /* Clear the screen */
  1924. #endif /* COMMENT */
  1925.             }
  1926.         if (c == HT) {        /* Tab */
  1927.         if (comment)        /* If in comment, */
  1928.           c = SP;        /* substitute space */
  1929.         else            /* otherwise */
  1930.           c = ESC;        /* substitute ESC (for completion) */
  1931.         }
  1932.         if (c == ';' || c == '#') { /* Trailing comment */
  1933.         if (inword == 0 && quoting) { /* If not in a word */
  1934.             comment = 1;    /* start a comment. */
  1935.             cp = bp;        /* remember where it starts. */
  1936.         }
  1937.         }
  1938.         if (!comment && c == SP) {    /* Space */
  1939.                 *bp++ = c;        /* deposit in buffer if not already */
  1940.                 if (echof) putchar(c);  /* echo it. */
  1941.                 if (inword == 0) {      /* If leading, gobble it. */
  1942.                     pp++;
  1943.                     continue;
  1944.                 } else {                /* If terminating, return. */
  1945. #ifdef COMMENT
  1946.                     np = bp;
  1947.                     setatm(pp,0);
  1948.                     inword = cmflgs = 0;
  1949.             return(0);
  1950. #else
  1951. /* This allows { ... } grouping */
  1952.             if ((*pp != '{') || (brack_knt == 0)) {
  1953.             np = bp;
  1954.             setatm(pp,0);
  1955.             inword = cmflgs = 0;
  1956.             return(0);
  1957.             }
  1958.                     continue;
  1959. #endif /* COMMENT */
  1960.                 }
  1961.             }
  1962.         if (c == '{') brack_knt++;    /* jrs */
  1963.         if (c == '}') brack_knt--;    /* jrs */
  1964.  
  1965.             if (c == NL || c == CR) {   /* CR or LF. */
  1966.         if (echof) cmdnewl((char)c); /* Echo it. */
  1967.         while (bp > pp && (*(bp-1) == SP || *(bp-1) == HT)) /* Trim */
  1968.           bp--;            /* trailing */
  1969.         *bp = NUL;        /* whitespace. */
  1970.         if ((bp > pp) && (*(bp-1) == '-')) { /* This line continued? */
  1971.             if (chsrc) {    /* If reading from tty, */
  1972. #ifdef COMMENT
  1973.             bp--, pp--;    /* back up the buffer pointer, */
  1974. #else
  1975.             bp--;
  1976. #endif /* COMMENT */
  1977.             *bp = NUL;    /* erase the dash, */
  1978.             continue;    /* and go back for next char now. */
  1979.             }
  1980.         } else {        /* No, a command has been entered. */
  1981.             *bp = NUL;        /* Terminate the command string. */
  1982.             if (comment) {    /* If we're in a comment, */
  1983.             comment = 0;    /* Say we're not any more, */
  1984.             *cp = NUL;    /* cut it off. */
  1985.             }
  1986.             np = bp;        /* Where to start next field. */
  1987.             setatm(pp,0);    /* Copy this field to atom buffer. */
  1988.             inword = 0;        /* Not in a word any more. */
  1989. #ifdef CK_RECALL
  1990.             if (chsrc &&    /* Reading commands from keyboard? */
  1991.             (on_recall) &&           /* Recall is turned on? */
  1992.             (cm_recall > 0) &&     /* Saving commands? */
  1993.             (int)strlen(cmdbuf)) { /* Non-null command? */
  1994.             if (rlast >= cm_recall - 1) { /* Yes, buffer full? */
  1995.                 int i;       /* Yes. */
  1996.                 if (recall[0]) /* Discard oldest command */
  1997.                   free(recall[0]);
  1998.                 for (i = 0; i < current; i++) /* The rest */
  1999.                   recall[i] = recall[i+1]; /* move back */
  2000.                 rlast--;     /* Now we have one less */
  2001.             }
  2002.             rlast++;     /* Index of last command in buffer */
  2003.             current = rlast; /* Also now the current command */
  2004.             recall[current] = malloc((int)strlen(cmdbuf)+1);
  2005.             if (recall[current])
  2006.               strcpy(recall[current],cmdbuf);
  2007.             }
  2008. #endif /* CK_RECALL */
  2009.             return(cmflgs = 1);
  2010.         }
  2011.             }
  2012.         /* Question mark */
  2013.             if (!comment && quoting && echof && (c == '?')) {
  2014.                 putchar(c);
  2015.                 *bp = NUL;
  2016.                 setatm(pp,0);
  2017.                 return(cmflgs = 3);
  2018.             }
  2019.             if (c == ESC) {        /* ESC */
  2020.         if (!comment) {
  2021.             *bp = NUL;
  2022.             setatm(pp,0);
  2023.             return(cmflgs = 2);
  2024.         } else {
  2025.             putchar(BEL);
  2026.             continue;
  2027.         }
  2028.             }
  2029.             if (c == BS || c == RUB) {  /* Character deletion */
  2030.                 if (bp > cmdbuf) {      /* If still in buffer... */
  2031.             cmdchardel();    /* erase it. */
  2032.                     bp--;               /* point behind it, */
  2033.                     if (*bp == SP) inword = 0; /* Flag if current field gone */
  2034.                     *bp = NUL;          /* Erase character from buffer. */
  2035.                 } else {                /* Otherwise, */
  2036.                     putchar(BEL);       /* beep, */
  2037.                     cmres();            /* and start parsing a new command. */
  2038.             *bp = *atmbuf = NUL;
  2039.                 }
  2040.                 if (pp < bp) continue;
  2041.                 else return(cmflgs = -1);
  2042.             }
  2043.             if (c == LDEL) {            /* ^U, line deletion */
  2044.                 while ((bp--) > cmdbuf) {
  2045.                     cmdchardel();
  2046.                     *bp = NUL;
  2047.                 }
  2048.                 cmres();                /* Restart the command. */
  2049.         *bp = *atmbuf = NUL;
  2050.                 inword = 0;
  2051.                 return(cmflgs = -1);
  2052.             }
  2053.             if (c == WDEL) {            /* ^W, word deletion */
  2054.                 if (bp <= cmdbuf) {     /* Beep if nothing to delete */
  2055.                     putchar(BEL);
  2056.                     cmres();
  2057.             *bp = *atmbuf = NUL;
  2058.                     return(cmflgs = -1);
  2059.                 }
  2060.                 bp--;
  2061.                 for ( ; (bp >= cmdbuf) && (*bp == SP) ; bp--) {
  2062.                     cmdchardel();
  2063.                     *bp = NUL;
  2064.                 }
  2065.                 for ( ; (bp >= cmdbuf) && (*bp != SP) ; bp--) {
  2066.                     cmdchardel();
  2067.                     *bp = NUL;
  2068.                 }
  2069.                 bp++;
  2070.                 inword = 0;
  2071.                 return(cmflgs = -1);
  2072.             }
  2073.             if (c == RDIS) {            /* ^R, redisplay */
  2074. #ifdef COMMENT
  2075.                 *bp = NUL;
  2076.                 printf("\n%s%s",cmprom,cmdbuf);
  2077. #else
  2078.         char *cpx; char cx;
  2079.                 *bp = NUL;
  2080.                 printf("\n%s",cmprom);
  2081.         cpx = cmdbuf;
  2082.         while (cx = *cpx++) {
  2083. #ifdef isprint
  2084.             putchar(isprint(cx) ? cx : '^');
  2085. #else
  2086.             putchar((cx >= SP && cx < DEL) ? cx : '^');
  2087. #endif /* isprint */
  2088.         }
  2089. #endif /* COMMENT */
  2090.         fflush(stdout);
  2091.                 continue;
  2092.             }
  2093. #ifdef CK_RECALL
  2094.         if (chsrc &&        /* Reading commands from keyboard? */
  2095.         (cm_recall > 0) &&    /* Saving commands? */
  2096.         (c == C_UP || c == C_UP2)) { /* Go up one */
  2097.         if (current < 0) {    /* Nowhere to go, */
  2098.             putchar(BEL);    /* just beep. */
  2099.             continue;
  2100.         }    
  2101.         if (recall[current]) {
  2102.             if (!strcmp(recall[current],cmdbuf)) {
  2103.             if (current > 0) {
  2104.                 current--;              
  2105.             } else {
  2106.                 putchar(BEL);
  2107.                 continue;
  2108.             }
  2109.             }
  2110.         }
  2111.         if (recall[current]) { /* We have a previous command */
  2112.             while ((bp--) > cmdbuf) { /* Erase current line */
  2113.             cmdchardel();
  2114.             *bp = NUL;
  2115.             }
  2116.             strcpy(cmdbuf,recall[current]);
  2117.             printf("\r%s%s",cmprom,cmdbuf);
  2118.             current--;
  2119.         }
  2120.         return(cmflgs = -1);    /* Force a reparse */
  2121.         }
  2122.         if (chsrc &&        /* Reading commands from keyboard? */
  2123.         (cm_recall > 0) &&    /* Saving commands? */
  2124.         (c == C_DN)) {        /* Down one */
  2125.         if (current + 1 > rlast) { /* Already at bottom, just beep */
  2126.             putchar(BEL);
  2127.             continue;
  2128.         }
  2129.         current++;        /* OK to go down */
  2130.         if (recall[current])
  2131.           if (!strcmp(recall[current],cmdbuf))
  2132.             current++;
  2133.         if (recall[current]) {
  2134.             while ((bp--) > cmdbuf) { /* Erase current line */
  2135.             cmdchardel();
  2136.             *bp = NUL;
  2137.             }
  2138.             strcpy(cmdbuf,recall[current]);
  2139.             printf("\r%s%s",cmprom,cmdbuf);
  2140.             return(cmflgs = -1); /* Force reparse */
  2141.         }
  2142.         }
  2143. #endif /* CK_RECALL */
  2144.         if (c < SP && quote == 0) { /* Any other unquoted control char */
  2145.         if (!chsrc) bp++;    /* If cmd file, point past it */
  2146.         else putchar(BEL);    /* otherwise just beep and */
  2147.         continue;        /* continue, don't put in buffer */
  2148.         }
  2149.         if (echof) cmdecho((char) c, 0); /* Echo what was typed. */
  2150.         } else {            /* This character was quoted. */
  2151.         int qf = 1;
  2152.         quote = 0;            /* Unset the quote flag. */
  2153.         /* Quote character at this level is only for SP, ?, and controls */
  2154.             /* If anything else was quoted, leave quote in, and let */
  2155.         /* the command-specific parsing routines handle it, e.g. \007 */
  2156.         if (c > 32 && c != '?' && c != RUB && chsrc != 0) {
  2157.         *bp++ = CMDQ;        /* Deposit \ if it came from tty */
  2158.         qf = 0;            /* and don't erase it from screen */
  2159.         }
  2160.         if (echof) cmdecho((char) c, qf); /* Now echo quoted character */
  2161.         debug(F111,"gtword quote",cmdbuf,c);
  2162.     }
  2163. #ifdef COMMENT
  2164.         if (echof) cmdecho((char) c,quote); /* Echo what was typed. */
  2165. #endif /* COMMENT */
  2166.         if (!comment) inword = 1;    /* Flag we're in a word. */
  2167.     if (quote) continue;        /* Don't deposit quote character. */
  2168.         if (c != NL) *bp++ = c;        /* Deposit command character. */
  2169.     }                                   /* End of big while */
  2170.     putchar(BEL);                       /* Get here if... */
  2171.     printf("?Command too long, maximum length: %d.\n",CMDBL);
  2172.     cmflgs = -2;
  2173.     return(-9);
  2174. }
  2175.  
  2176. /* Utility functions */
  2177.  
  2178. /* A D D B U F  -- Add the string pointed to by cp to the command buffer  */
  2179.  
  2180. static int
  2181. addbuf(cp) char *cp; {
  2182.     int len = 0;
  2183.     while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) {
  2184.         *bp++ = *cp++;                  /* Copy and */
  2185.         len++;                          /* count the characters. */
  2186.     }
  2187.     *bp++ = SP;                         /* Put a space at the end */
  2188.     *bp = NUL;                          /* Terminate with a null */
  2189.     np = bp;                            /* Update the next-field pointer */
  2190.     return(len);                        /* Return the length */
  2191. }
  2192.  
  2193. /*  S E T A T M  --  Deposit a token in the atom buffer.  */
  2194. /*
  2195.   Break on space, newline, carriage return, or NUL.
  2196.   Except flag != 0 means to allow imbedded spaces in selected fields.
  2197.   Null-terminate the result.
  2198.   If the source pointer is the atom buffer itself, do nothing.
  2199.   Return length of token, and also set global "cc" to this length.
  2200. */
  2201. static int
  2202. setatm(cp,flag) char *cp; int flag; {
  2203.     char *ap, *xp;
  2204.     int  brack_knt, n;
  2205.  
  2206.     cc = 0;                /* Character counter */
  2207.     ap = atmbuf;            /* Address of atom buffer */
  2208.  
  2209.     if (cp == ap) {            /* In case source is atom buffer */
  2210.     xp = atybuf;            /* make a copy */
  2211.     strcpy(xp,ap);            /* so we can copy it back, edited. */
  2212.     cp = xp;
  2213.     }
  2214.     *ap = NUL;                /* Zero the atom buffer */
  2215.     if (flag) {                /* Trim trailing blanks */
  2216.     n = strlen(cp);
  2217.     while (--n >= 0)
  2218.       if (cp[n] != SP) break;
  2219.     cp[n+1] = NUL;
  2220.     }
  2221.     while (*cp == SP) cp++;        /* Trim leading spaces */
  2222. #ifdef COMMENT
  2223. /* This one doesn't work for items like "input 20 {\13\10$ }" */
  2224.     brack_knt = (*cp == '{');        /* jrs */
  2225.     while ( /* (*cp != SP) && */ (*cp != NL) && (*cp != NUL) && (*cp != CR)) {
  2226.         if ((*cp == SP) && (flag == 0) && (brack_knt == 0)) break; /* jrs */
  2227.         *ap++ = *cp++;            /* Copy up to SP, NL, CR, or end */
  2228.         if (*cp == '{') brack_knt++;    /* jrs */
  2229.         if (*cp == '}') brack_knt--;    /* jrs */
  2230.         cc++;                /* and count */
  2231.     }
  2232. #else
  2233.     brack_knt = 0;
  2234.     while (*cp) {
  2235.         if (*cp == '{') brack_knt++;
  2236.         if (*cp == '}') brack_knt--;
  2237.     if (brack_knt == 0) {
  2238.         if ((*cp == SP || *cp == HT) && (flag == 0)) break;
  2239.         if (*cp == LF || *cp == CR) break;
  2240.     }
  2241.         *ap++ = *cp++;
  2242.         cc++;
  2243.     }
  2244. #endif /* COMMENT */
  2245.     *ap = NUL;                /* Terminate the string. */
  2246.     return(cc);                         /* Return length. */
  2247. }
  2248.  
  2249. /*  R D I G I T S  -- Verify that all the characters in line ARE DIGITS  */
  2250.  
  2251. int
  2252. rdigits(s) char *s; {
  2253.     while (*s) {
  2254.         if (!isdigit(*s)) return(0);
  2255.         s++;
  2256.     }
  2257.     return(1);
  2258. }
  2259.  
  2260. /* These functions attempt to hide system dependencies from the mainline */
  2261. /* code in gtword().  Ultimately they should be moved to ck?tio.c, where */
  2262. /* ? = each and every system supported by C-Kermit. */
  2263.  
  2264. static int
  2265. cmdgetc() {                /* Get a character from the tty. */
  2266.     int c;
  2267.  
  2268. #ifdef datageneral
  2269.     {
  2270.     char ch;
  2271.     c = dgncinb(0,&ch,1);        /* -1 is EOF, -2 TO,
  2272.                                          * -c is AOS/VS error */
  2273.     if (c == -2) {            /* timeout was enabled? */
  2274.         resto(channel(0));        /* reset timeouts */
  2275.         c = dgncinb(0,&ch,1);    /* retry this now! */
  2276.     }
  2277.     if (c < 0) return(-4);        /* EOF or some error */
  2278.     else c = (int) ch & 0177;    /* Get char without parity */
  2279. /*    echof = 1; */
  2280.     }
  2281. #else /* Not datageneral */
  2282. #ifdef GEMDOS
  2283.     c = isatty(0) ? coninc(0) : getchar();
  2284. #else
  2285. #ifdef OS2
  2286.     c = isatty(0) ? coninc(0) : getchar();
  2287.     if (c < 0) return(-4);
  2288. #else /* Not OS2 */
  2289.     c = getchar();            /* or from tty. */
  2290. #ifdef RTU
  2291.     if (rtu_bug) {
  2292.     c = getchar();            /* RTU doesn't discard the ^Z */
  2293.     rtu_bug = 0;
  2294.     }
  2295. #endif /* RTU */
  2296. #endif /* OS2 */
  2297. #endif /* GEMDOS */
  2298. #endif /* datageneral */
  2299.     return(c);                /* Return what we got */
  2300. }
  2301.  
  2302.  
  2303. #ifdef COMMENT
  2304. /*
  2305.   No more screen clearing.  If you wanna clear the screen, define a macro
  2306.   to do it, like "define cls write screen \27[;H\27[2J".
  2307. */
  2308. cmdclrscn() {                /* Clear the screen */
  2309.  
  2310. #ifdef aegis
  2311.     putchar(FF);
  2312. #else
  2313. #ifdef AMIGA
  2314.     putchar(FF);
  2315. #else
  2316. #ifdef OSK
  2317.     putchar(FF);
  2318. #else
  2319. #ifdef datageneral
  2320.     putchar(FF);
  2321. #else
  2322. #ifdef OS2
  2323.     zsystem("cls");
  2324. #else
  2325.     zsystem("clear");
  2326. #endif /* OS2 */
  2327. #endif /* datageneral */
  2328. #endif /* OSK */
  2329. #endif /* AMIGA */
  2330. #endif /* aegis */
  2331. }
  2332. #endif /* COMMENT */
  2333.  
  2334. static VOID                /* What to echo at end of command */
  2335. #ifdef CK_ANSIC
  2336. cmdnewl(char c)
  2337. #else
  2338. cmdnewl(c) char c;
  2339. #endif /* CK_ANSIC */
  2340. /* cmdnewl */ {
  2341.     putchar(c);                /* c is the terminating character */
  2342. #ifdef WINTCP
  2343.     if (c == CR) putchar(NL);
  2344. #endif /* WINTCP */
  2345. #ifdef OS2
  2346.     if (c == CR) putchar(NL);
  2347. #endif /* OS2 */
  2348. #ifdef aegis
  2349.     if (c == CR) putchar(NL);
  2350. #endif /* aegis */
  2351. #ifdef AMIGA
  2352.     if (c == CR) putchar(NL);
  2353. #endif /* AMIGA */
  2354. #ifdef datageneral
  2355.     if (c == CR) putchar(NL);
  2356. #endif /* datageneral */
  2357. #ifdef GEMDOS
  2358.     if (c == CR) putchar(NL);
  2359. #endif /* GEMDOS */
  2360. #ifdef STRATUS
  2361.     if (c == CR) putchar(NL);
  2362. #endif /* STRATUS */
  2363. }
  2364.  
  2365. static VOID
  2366. cmdchardel() {                /* Erase a character from the screen */
  2367.     if (!dpx) return;
  2368. #ifdef datageneral
  2369.     /* DG '\b' is EM (^y or \031) */
  2370.     if (termtype == 1)
  2371.       /* Erase a character from non-DG screen, */
  2372.       dgncoub(1,"\010 \010",3);
  2373.     else
  2374. #endif
  2375.       printf("\b \b");
  2376. #ifdef GEMDOS
  2377.     fflush(stdout);
  2378. #endif /* GEMDOS */
  2379. }
  2380.  
  2381. static VOID
  2382. #ifdef CK_ANSIC
  2383. cmdecho(char c, int quote)
  2384. #else
  2385. cmdecho(c,quote) char c; int quote;
  2386. #endif /* CK_ANSIC */
  2387. { /* cmdecho */
  2388.     if (!dpx) return;
  2389.     /* Echo tty input character c */
  2390.     if (quote) {
  2391.     putchar(BS); putchar(SP); putchar(BS);
  2392. #ifdef isprint
  2393.     putchar( isprint(c) ? c : '^' );
  2394. #else
  2395.     putchar((c >= SP && c < DEL) ? c : '^');
  2396. #endif /* isprint */
  2397.     } else putchar(c);
  2398. #ifdef OS2
  2399.     if (quote==1 && c==CR) putchar(NL);
  2400. #endif /* OS2 */
  2401. }
  2402.  
  2403. #endif /* NOICP */
  2404.  
  2405. #ifdef NOICP
  2406. #include "ckcdeb.h"
  2407. #include "ckucmd.h"
  2408. #include "ckcasc.h"
  2409. /*** #include <ctype.h> (ckcdeb.h already includes this) ***/
  2410. #endif /* NOICP */
  2411.  
  2412. /*  X X E S C  --  Interprets backslash codes  */
  2413. /*  Returns the int value of the backslash code if it is > -1 and < 256 */
  2414. /*  and updates the string pointer to first character after backslash code. */
  2415. /*  If the argument is invalid, leaves pointer unchanged and returns -1. */
  2416.  
  2417. int
  2418. xxesc(s) char **s; {            /* Expand backslash escapes */
  2419.     int x, y, brace, radix;        /* Returns the int value */
  2420.     char hd = '9';            /* Highest digit in radix */
  2421.     char *p;
  2422.  
  2423.     p = *s;                /* pointer to beginning */
  2424.     if (!p) return(-1);            /* watch out for null pointer */
  2425.     x = *p++;                /* character at beginning */
  2426.     if (x != CMDQ) return(-1);        /* make sure it's a backslash code */
  2427.  
  2428.     x = *p;                /* it is, get the next character */
  2429.     if (x == '{') {            /* bracketed quantity? */
  2430.     p++;                /* begin past bracket */
  2431.     x = *p;
  2432.     brace = 1;
  2433.     } else brace = 0;
  2434.     switch (x) {            /* Start interpreting */
  2435.       case 'd':                /* Decimal radix indicator */
  2436.       case 'D':
  2437.     p++;                /* Just point past it and fall thru */
  2438.       case '0':                /* Starts with digit */
  2439.       case '1':
  2440.       case '2':  case '3':  case '4':  case '5':
  2441.       case '6':  case '7':  case '8':  case '9':
  2442.     radix = 10;            /* Decimal */
  2443.     hd = '9';            /* highest valid digit */
  2444.     break;
  2445.       case 'o':                /* Starts with o or O */
  2446.       case 'O':
  2447.     radix = 8;            /* Octal */
  2448.     hd = '7';            /* highest valid digit */
  2449.     p++;                /* point past radix indicator */
  2450.     break;
  2451.       case 'x':                /* Starts with x or X */
  2452.       case 'X':
  2453.     radix = 16;            /* Hexadecimal */
  2454.     p++;                /* point past radix indicator */
  2455.     break;
  2456.       default:                /* All others */
  2457. #ifdef COMMENT
  2458.     *s = p+1;            /* Treat as quote of next char */
  2459.     return(*p);
  2460. #else
  2461.     return(-1);
  2462. #endif /* COMMENT */
  2463.     }
  2464.     /* For OS/2, there are "wide" characters required for the keyboard
  2465.      * binding, i.e \644 and similar codes larger than 255 (byte).
  2466.      * For this purpose, give up checking for < 256. If someone means
  2467.      * \266 should result in \26 followed by a "6" character, he should
  2468.      * always write \{26}6 anyway.  Now, return only the lower byte of
  2469.      * the result, i.e. 10, but eat up the whole \266 sequence and
  2470.      * put the wide result 266 into a global variable.  Yes, that's not
  2471.      * the most beautiful programming style but requires the least
  2472.      * amount of changes to other routines.
  2473.      */
  2474.     if (radix <= 10) {            /* Number in radix 8 or 10 */
  2475.     for ( x = y = 0;
  2476.            (*p) && (*p >= '0') && (*p <= hd)
  2477. #ifdef OS2
  2478.                    && (y < 4) && (x*radix < 768);
  2479.               /* the maximum needed value \767 is still only 3 digits long */
  2480.               /* while as octal it requires \1377, i.e. 4 digits */
  2481. #else
  2482.                    && (y < 3) && (x*radix < 256);
  2483. #endif /* OS2 */
  2484.           p++,y++) {
  2485.         x = x * radix + (int) *p - 48;
  2486.     }
  2487. #ifdef OS2
  2488.         wideresult = x;            /* Remember wide result */
  2489.         x &= 255;
  2490. #endif /* OS2 */
  2491.     if (y == 0 || x > 255) {    /* No valid digits? */
  2492.         *s = p;            /* point after it */
  2493.         return(-1);            /* return failure. */
  2494.     }
  2495.     } else if (radix == 16) {        /* Special case for hex */
  2496.     if ((x = unhex(*p++)) < 0) { *s = p - 1; return(-1); }
  2497.     if ((y = unhex(*p++)) < 0) { *s = p - 2; return(-1); }
  2498.     x = ((x << 4) & 0xF0) | (y & 0x0F);
  2499. #ifdef OS2
  2500.         wideresult = x;
  2501.         if ((y = unhex(*p)) >= 0) {
  2502.            p++;
  2503.        wideresult = ((x << 4) & 0xFF0) | (y & 0x0F);
  2504.            x = wideresult & 255;
  2505.         }
  2506. #endif /* OS2 */
  2507.     } else x = -1;
  2508.     if (brace && *p == '}' && x > -1)    /* Point past closing brace, if any */
  2509.       p++;
  2510.     *s = p;                /* Point to next char after sequence */
  2511.     return(x);                /* Return value of sequence */
  2512. }
  2513.  
  2514. int                    /* Convert hex string to int */
  2515. #ifdef CK_ANSIC
  2516. unhex(char x)
  2517. #else
  2518. unhex(x) char x;
  2519. #endif /* CK_ANSIC */
  2520. /* unhex */ {
  2521.  
  2522.     if (x >= '0' && x <= '9')        /* 0-9 is offset by hex 30 */
  2523.       return(x - 0x30);
  2524.     else if (x >= 'A' && x <= 'F')    /* A-F offset by hex 37 */
  2525.       return(x - 0x37);
  2526.     else if (x >= 'a' && x <= 'f')    /* a-f offset by hex 57 */
  2527.       return(x - 0x57);            /* (obviously ASCII dependent) */
  2528.     else return(-1);
  2529. }
  2530.  
  2531. /* See if argument string is numeric */
  2532. /* Returns 1 if OK, zero if not OK */
  2533. /* If OK, string should be acceptable to atoi() */
  2534. /* Allows leading space, sign */
  2535.  
  2536. int
  2537. chknum(s) char *s; {            /* Check Numeric String */
  2538.     int x = 0;                /* Flag for past leading space */
  2539.     int y = 0;                /* Flag for digit seen */
  2540.     char c;
  2541.     debug(F110,"chknum",s,0);
  2542.     while (c = *s++) {            /* For each character in the string */
  2543.     switch (c) {
  2544.       case SP:            /* Allow leading spaces */
  2545.       case HT:
  2546.         if (x == 0) continue;
  2547.         else return(0);
  2548.       case '+':            /* Allow leading sign */
  2549.       case '-':
  2550.         if (x == 0) x = 1;
  2551.         else return(0);
  2552.         break;
  2553.       default:            /* After that, only decimal digits */
  2554.         if (c >= '0' && c <= '9') {
  2555.         x = y = 1;
  2556.         continue;
  2557.         } else return(0);
  2558.     }
  2559.     }
  2560.     return(y);
  2561. }
  2562.  
  2563. /*  L O W E R  --  Lowercase a string  */
  2564.  
  2565. int
  2566. lower(s) char *s; {
  2567.     int n = 0;
  2568.     while (*s) {
  2569.         if (isupper(*s)) *s = tolower(*s);
  2570.         s++, n++;
  2571.     }
  2572.     return(n);
  2573. }
  2574.  
  2575. /*  L O O K U P  --  Lookup the string in the given array of strings  */
  2576.  
  2577. /*
  2578.  Call this way:  v = lookup(table,word,n,&x);
  2579.  
  2580.    table - a 'struct keytab' table.
  2581.    word  - the target string to look up in the table.
  2582.    n     - the number of elements in the table.
  2583.    x     - address of an integer for returning the table array index.
  2584.  
  2585.  The keyword table must be arranged in ascending alphabetical order, and
  2586.  all letters must be lowercase.
  2587.  
  2588.  Returns the keyword's associated value ( zero or greater ) if found,
  2589.  with the variable x set to the array index, or:
  2590.  
  2591.   -3 if nothing to look up (target was null),
  2592.   -2 if ambiguous,
  2593.   -1 if not found.
  2594.  
  2595.  A match is successful if the target matches a keyword exactly, or if
  2596.  the target is a prefix of exactly one keyword.  It is ambiguous if the
  2597.  target matches two or more keywords from the table.
  2598. */
  2599.  
  2600. int
  2601. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  2602.  
  2603.     int i, v, cmdlen;
  2604.  
  2605. /* Lowercase & get length of target, if it's null return code -3. */
  2606.  
  2607.     if ((((cmdlen = lower(cmd))) == 0) || (n < 1)) return(-3);
  2608.  
  2609. /* Not null, look it up */
  2610.  
  2611.     for (i = 0; i < n-1; i++) {
  2612.         if (!strcmp(table[i].kwd,cmd) ||
  2613.            ((v = !strncmp(table[i].kwd,cmd,cmdlen)) &&
  2614.              strncmp(table[i+1].kwd,cmd,cmdlen))) {
  2615.                 *x = i;
  2616.                 return(table[i].kwval);
  2617.              }
  2618.         if (v) return(-2);
  2619.     }
  2620.  
  2621. /* Last (or only) element */
  2622.  
  2623.     if (!strncmp(table[n-1].kwd,cmd,cmdlen)) {
  2624.         *x = n-1;
  2625.         return(table[n-1].kwval);
  2626.     } else return(-1);
  2627. }
  2628.  
  2629. /* Like lookup, but requires an exact match */
  2630.  
  2631. int
  2632. xlookup(table,cmd,n,x) struct keytab table[]; char *cmd; int n, *x; {
  2633.     int i, cmdlen;
  2634.     char *p;
  2635.     cmdlen = (int) strlen(cmd);
  2636.     p = (char *) malloc(cmdlen + 1);
  2637.     if (p) {                /* Make a copy that can be changed */
  2638.     strcpy(p,cmd);
  2639.     cmd = p;
  2640.     if ((((cmdlen = lower(cmd))) == 0) || (n < 1)) return(-3);
  2641.     for (i = 0; i < n; i++)
  2642.       if (((int)strlen(table[i].kwd) == cmdlen) &&
  2643.           (!strncmp(table[i].kwd,cmd,cmdlen))) {
  2644.           free(p);
  2645.           return(table[i].kwval);
  2646.       }
  2647.     free(p);
  2648.     }
  2649.     return(-1);
  2650. }
  2651.  
  2652. int
  2653. cmdsquo(x) int x; {
  2654.     quoting = x;
  2655.     return(1);
  2656. }
  2657.  
  2658. int
  2659. cmdgquo() {
  2660.     return(quoting);
  2661. }
  2662.