home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv196.zip / ckucmd.c < prev    next >
C/C++ Source or Header  |  2000-01-03  |  160KB  |  5,582 lines

  1. #include "ckcsym.h"
  2. #define DOCHKVAR
  3.  
  4. char *cmdv = "Command package 7.0.121, 24 Dec 1999";
  5.  
  6. /*  C K U C M D  --  Interactive command package for Unix  */
  7.  
  8. /*
  9.   Author: Frank da Cruz (fdc@columbia.edu),
  10.   Columbia University Academic Information Systems, New York City.
  11.  
  12.   Copyright (C) 1985, 2000,
  13.     Trustees of Columbia University in the City of New York.
  14.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  15.     copyright text in the ckcmai.c module for disclaimer and permissions.
  16. */
  17.  
  18. #ifdef OS2                    /* Command-terminal-to-C-Kermit character mask */
  19. int cmdmsk = 255;
  20. #else
  21. int cmdmsk = 127;
  22. #endif /* OS2 */
  23.  
  24. #ifdef BS_DIRSEP            /* Directory separator is backslash */
  25. #undef BS_DIRSEP
  26. #endif /* BS_DIRSEP */
  27.  
  28. #ifdef OS2
  29. #define BS_DIRSEP
  30. #endif /* BS_DIRSEP */
  31.  
  32. #define CKUCMD_C
  33.  
  34. #include "ckcdeb.h"                     /* Formats for debug(), etc. */
  35. #include "ckcker.h"            /* Needed for BIGBUFOK definition */
  36. #include "ckcnet.h"            /* Needed for server-side Telnet */
  37. #include "ckucmd.h"            /* Needed for xx_strp prototype */
  38. #include "ckuusr.h"                     /* Needed for prompt length */
  39.  
  40. #undef CKUCMD_C
  41.  
  42. _PROTOTYP( int unhex, (char) );
  43. _PROTOTYP( static VOID cmdclrscn, (void) );
  44.  
  45. struct keytab cmonths[] = {
  46.   { "april",     4, 0 },
  47.   { "august",    8, 0 },
  48.   { "december", 12, 0 },
  49.   { "february",  2, 0 },
  50.   { "january",   1, 0 },
  51.   { "july",      7, 0 },
  52.   { "june",      6, 0 },
  53.   { "march",     3, 0 },
  54.   { "may",       5, 0 },
  55.   { "november", 11, 0 },
  56.   { "october",  10, 0 },
  57.   { "september", 9, 0 }
  58. };
  59.  
  60. #ifndef NOICP     /* The rest only if interactive command parsing selected */
  61.  
  62. #ifndef NOSPL
  63. _PROTOTYP( int chkvar, (char *) );
  64. extern int askflag;
  65. #endif /* NOSPL */
  66.  
  67. int cmfldflgs = 0;            /* Flags for cmfld() */
  68. static int cmkwflgs = 0;        /* Flags from last keyword parse */
  69. static int blocklvl = 0;        /* Block nesting level */
  70. static int linebegin = 0;        /* Flag for at start of a line */
  71. static int quoting = 1;            /* Quoting is allowed */
  72. static int swarg = 0;            /* Parsing a switch argument */
  73. static int xcmfdb = 0;            /* Flag for parsing chained fdbs... */
  74. static int chsrc = 0;            /* Source of character, 1 = tty */
  75.  
  76. #ifdef BS_DIRSEP
  77. static int dirnamflg = 0;
  78. #endif /* BS_DIRSEP */
  79.  
  80. /*
  81. Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP. Features:
  82.  
  83. . parses and verifies keywords, filenames, text strings, numbers, other data
  84. . displays appropriate menu or help message when user types "?"
  85. . does keyword and filename completion when user types ESC or TAB
  86. . does partial filename completion
  87. . accepts any unique abbreviation for a keyword
  88. . allows keywords to have attributes, like "invisible" and "abbreviation"
  89. . can supply defaults for fields omitted by user
  90. . provides command retry and recall
  91. . provides command line editing (character, word, and line deletion)
  92. . accepts input from keyboard, command files, macros, or redirected stdin
  93. . allows for full or half duplex operation, character or line input
  94. . allows \-escapes for hard-to-type characters
  95. . allows specification of a user exit to expand variables, etc.
  96. . settable prompt, protected from deletion, dynamically re-evaluated each time.
  97. . allows chained parse functions.
  98.  
  99. Functions:
  100.  cmsetp - Set prompt (cmprom is prompt string)
  101.  cmsavp - Save current prompt
  102.  prompt - Issue prompt
  103.  cmini  - Clear the command buffer (before parsing a new command)
  104.  cmres  - Reset command buffer pointers (before reparsing)
  105.  cmkey  - Parse a keyword or token (also cmkey2)
  106.  cmswi  - Parse a switch
  107.  cmnum  - Parse a number
  108.  cmifi  - Parse an input file name
  109.  cmofi  - Parse an output file name (also cmifip, cmifi2, ...)
  110.  cmdir  - Parse a directory name (also cmdirp)
  111.  cmfld  - Parse an arbitrary field
  112.  cmtxt  - Parse a text string
  113.  cmdate - Parse a date-time string
  114.  cmcfm  - Parse command confirmation (end of line)
  115.  cmfdb  - Parse any of a list of the foregoing (chained parse functions)
  116.  
  117. Return codes:
  118.  -9: like -2 except this module already printed the error message
  119.  -3: no input provided when required
  120.  -2: input was invalid (e.g. not a number when a number was required)
  121.  -1: reparse required (user deleted into a preceding field)
  122.   0 or greater: success
  123. See individual functions for greater detail.
  124.  
  125. Before using these routines, the caller should #include ckucmd.h, and set the
  126. program's prompt by calling cmsetp().  If the file parsing functions cmifi,
  127. cmofi, or cmdir are to be used, this module must be linked with a ck?fio file
  128. system support module for the appropriate system, e.g. ckufio for Unix.  If
  129. the caller puts the terminal in character wakeup ("cbreak") mode with no echo,
  130. then these functions will provide line editing -- character, word, and line
  131. deletion, as well as keyword and filename completion upon ESC and help
  132. strings, keyword, or file menus upon '?'.  If the caller puts the terminal
  133. into character wakeup/noecho mode, care should be taken to restore it before
  134. exit from or interruption of the program.  If the character wakeup mode is not
  135. set, the system's own line editor may be used.
  136.  
  137. NOTE: Contrary to expectations, many #ifdef's have been added to this module.
  138. Any operation requiring an #ifdef (like clear screen, get character from
  139. keyboard, erase character from screen, etc) should eventually be turned into a
  140. call to a function that is defined in ck?tio.c, but then all the ck?tio.c
  141. modules would have to be changed...
  142. */
  143.  
  144. /* Includes */
  145.  
  146. #include "ckcker.h"
  147. #include "ckcasc.h"            /* ASCII character symbols */
  148. #include "ckucmd.h"                     /* Command parsing definitions */
  149. #include <errno.h>            /* Error number symbols */
  150.  
  151. #ifdef OS2
  152. #ifndef NT
  153. #define INCL_NOPM
  154. #define INCL_VIO            /* Needed for ckocon.h */
  155. #include <os2.h>
  156. #undef COMMENT
  157. #else
  158. #define APIRET ULONG
  159. #include <windows.h>
  160. #endif /* NT */
  161. #include "ckocon.h"
  162. #include <io.h>
  163. #endif /* OS2 */
  164.  
  165. #ifdef NT
  166. #define stricmp _stricmp
  167. #endif /* NT */
  168.  
  169. #ifdef OSK
  170. #define cc ccount            /* OS-9/68K compiler bug */
  171. #endif /* OSK */
  172.  
  173. #ifdef GEMDOS                /* Atari ST */
  174. #ifdef putchar
  175. #undef putchar
  176. #endif /* putchar */
  177. #define putchar(x) conoc(x)
  178. #endif /* GEMDOS */
  179.  
  180. #ifdef CK_AUTODL
  181. extern int cmdadl, justone;
  182. #endif /* CK_AUTODL */
  183.  
  184. extern int timelimit, nzxopts;
  185.  
  186. #ifdef CKSYSLOG
  187. #ifdef UNIX
  188. #ifdef CKXPRINTF            /* Our printf macro conflicts with */
  189. #undef printf                /* use of "printf" in syslog.h */
  190. #endif /* CKXPRINTF */
  191. #include <syslog.h>
  192. #ifdef CKXPRINTF
  193. #define printf ckxprintf
  194. #endif /* CKXPRINTF */
  195. #endif /* UNIX */
  196. #endif /* CKSYSLOG */
  197.  
  198. /* Local variables */
  199.  
  200. static
  201. int psetf = 0,                          /* Flag that prompt has been set */
  202.     cc = 0,                             /* Character count */
  203.     dpx = 0,                            /* Duplex (0 = full) */
  204.     inword = 0;                /* In the middle of getting a word */
  205.  
  206. #ifdef OLDHELP
  207. static
  208. int hw = HLPLW,                         /* Help line width */
  209.     hc = HLPCW,                         /* Help line column width */
  210.     hh,                                 /* Current help column number */
  211.     hx;                                 /* Current help line position */
  212. #endif /* OLDHELP */
  213. char *dfprom = "Command? ";             /* Default prompt */
  214.  
  215. int cmflgs;                             /* Command flags */
  216. int cmfsav;                /* A saved version of them */
  217.  
  218. static char pushc = NUL;
  219. static char brkchar = NUL;
  220.  
  221. #define CMDEFAULT 1023
  222. static char cmdefault[CMDEFAULT+1];
  223.  
  224. #ifdef DCMDBUF
  225. char *cmdbuf = NULL;            /* Command buffer */
  226. char *savbuf = NULL;            /* Buffer to save copy of command */
  227. char *atmbuf = NULL;            /* Atom buffer - for current field */
  228. char *atxbuf = NULL;            /* For expanding the atom buffer */
  229. #ifdef OLDHELP
  230. static char *hlpbuf;            /* Help string buffer */
  231. #endif /* OLDHELP */
  232. static char *atybuf = NULL;        /* For copying atom buffer */
  233. static char *filbuf = NULL;        /* File name buffer */
  234. static char *cmprom = NULL;        /* Program's prompt */
  235. static char *cmprxx = NULL;        /* Program's prompt, unevaluated */
  236.  
  237. #ifdef CK_RECALL
  238. /*
  239.   Command recall is available only if we can make profligate use of malloc().
  240. */
  241. #define R_MAX 10            /* How many commands to save */
  242. int cm_recall = R_MAX;            /* Size of command recall buffer */
  243. int on_recall = 1;            /* Recall feature is ON */
  244. static int no_recall = 0;        /* Recall OFF for this cmd only */
  245. static int force_add = 0;        /* Force cmd into recall buffer */
  246. int in_recall = 0;            /* Recall buffers are init'd */
  247. static int
  248.   current = -1,                /* Pointer to current command */
  249.   rlast = -1;                /* Index of last command in buffer */
  250. static char **recall = NULL;        /* Array of recall buffer pointers */
  251. #endif /* CK_RECALL */
  252. #else
  253. char cmdbuf[CMDBL+4];                   /* Command buffer */
  254. char savbuf[CMDBL+4];                   /* Buffer to save copy of command */
  255. char atmbuf[ATMBL+4];                   /* Atom buffer */
  256. char atxbuf[CMDBL+4];                   /* For expanding the atom buffer */
  257. #ifdef OLDHELP
  258. static char hlpbuf[HLPBL+4];        /* Help string buffer */
  259. #endif /* OLDHELP */
  260. static char atybuf[ATMBL+4];        /* For copying atom buffer */
  261. static char filbuf[ATMBL+4];        /* File name buffer */
  262. static char cmprom[PROMPTL+1];        /* Program's prompt */
  263. static char cmprxx[PROMPTL+1];        /* Program's prompt, unevaluated */
  264. #endif /* DCMDBUF */
  265.  
  266. /* Command buffer pointers */
  267.  
  268. #define PPVLEN 24
  269. char ppvnambuf[PPVLEN+1] = { NUL, NUL };
  270.  
  271. char * cmbptr = NULL;            /* Current position (for export) */
  272.  
  273. static char *bp,                        /* Current command buffer position */
  274.     *pp,                                /* Start of current field */
  275.     *np;                                /* Start of next field */
  276.  
  277. static int ungw,            /* For ungetting words */
  278.     atxn;                /* Expansion buffer (atxbuf) length */
  279.  
  280. #ifdef OS2
  281. extern int wideresult;
  282. #endif /* OS2 */
  283.  
  284. extern int cmd_cols, cmd_rows, local, quiet;
  285.  
  286. #ifdef TNCODE
  287. #ifdef IAC
  288. #undef IAC
  289. #endif /* IAC */
  290. #define IAC 255
  291. #endif /* TNCODE */
  292.  
  293. #ifdef OLDHELP
  294. _PROTOTYP( static VOID addhlp, (char *) );
  295. _PROTOTYP( static VOID clrhlp, (void) );
  296. _PROTOTYP( static VOID dmphlp, (void) );
  297. #endif /* OLDHELP */
  298. _PROTOTYP( static int gtword, (int) );
  299. _PROTOTYP( static int addbuf, (char *) );
  300. _PROTOTYP( static int setatm, (char *, int) );
  301. _PROTOTYP( static VOID cmdnewl, (char) );
  302. _PROTOTYP( static VOID cmdchardel, (void) );
  303. _PROTOTYP( static VOID cmdecho, (char, int) );
  304. _PROTOTYP( static int test, (int, int) );
  305. #ifdef GEMDOS
  306. _PROTOTYP( extern char *strchr, (char *, int) );
  307. #endif /* GEMDOS */
  308.  
  309. /* The following are for use with chained FDB's */
  310.  
  311. static int crflag = 0;            /* Carriage return was typed */
  312. static int qmflag = 0;            /* Question mark was typed */
  313. static int esflag = 0;            /* Escape was typed */
  314.  
  315. /* Directory separator */
  316.  
  317. #ifdef GEMDOS
  318. static char dirsep = '\\';
  319. #else
  320. #ifdef datageneral
  321. static char dirsep = ':';
  322. #else
  323. #ifdef MAC
  324. static char dirsep = ':';
  325. #else
  326. #ifdef VMS
  327. static char dirsep = '.';
  328. #else
  329. #ifdef STRATUS
  330. static char dirsep = '>';
  331. #else
  332. static char dirsep = '/';        /* UNIX, OS/2, OS-9, Amiga, etc. */
  333. #endif /* STRATUS */
  334. #endif /* VMS */
  335. #endif /* MAC */
  336. #endif /* datageneral */
  337. #endif /* GEMDOS */
  338.  
  339. /*  C K S P R E A D  --  Print string double-spaced  */
  340.  
  341. static char * sprptr = NULL;
  342.  
  343. static char *
  344. ckspread(s) char * s; {
  345.     int n = 0;
  346.     char * p;
  347.     n = strlen(s);
  348.     if (sprptr)
  349.       free(sprptr);
  350.     sprptr = malloc(n + n + 3);
  351.     if (sprptr) {
  352.     p = sprptr;
  353.     while (*s) {
  354.         *p++ = *s++;
  355.         *p++ = SP;
  356.     }
  357.     *p = NUL;
  358.     }
  359.     return(sprptr ? sprptr : "");
  360. }
  361.  
  362. /*  T E S T  --  Bit test  */
  363.  
  364. static int
  365. test(x,m) int x, m; { /*  Returns 1 if any bits from m are on in x, else 0  */
  366.     return((x & m) ? 1 : 0);
  367. }
  368.  
  369. /*  K W D H E L P  --  Given a keyword table, print keywords in columns.  */
  370. /*
  371.   Call with:
  372.     s     - keyword table
  373.     n     - number of entries
  374.     pat   - pattern (left substring) that must match for each keyword
  375.     pre   - prefix to add to each keyword
  376.     post  - suffix to add to each keyword
  377.     off   - offset on first screenful, allowing room for introductory text
  378.     xhlp  - 1 to print any CM_INV keywords that are not also abbreviations.
  379.             2 to print CM_INV keywords if CM_HLP also set
  380.             4 if it's a switch table (to show ':' if CM_ARG)
  381.  
  382.   Arranges keywords in columns with width based on longest keyword.
  383.   Does "more?" prompting at end of screen.
  384.   Uses global cmd_rows and cmd_cols for screen size.
  385. */
  386. VOID
  387. kwdhelp(s,n,pat,pre,post,off,xhlp)
  388.     struct keytab s[]; int n, off, xhlp; char *pat, *pre, *post;
  389. /* kwdhelp */ {
  390.  
  391.     int width = 0;
  392.     int cc;
  393.     int cols, height, i, j, k, lc, n2 = 0;
  394.     char *b = NULL, *p, *q;
  395.     char *pa, *px;
  396.     char **s2 = NULL;
  397.     char *tmpbuf = NULL;
  398.  
  399.     cc = strlen(pat);
  400.  
  401.     if (!s) return;            /* Nothing to do */
  402.     if (n < 1) return;            /* Ditto */
  403.     if (off < 0) off = 0;        /* Offset for first page */
  404.     if (!pre) pre = "";            /* Handle null string pointers */
  405.     if (!post) post = "";
  406.     lc = off;                /* Screen-line counter */
  407.  
  408.     if (xhlp & 4)            /* For switches */
  409.       tmpbuf = (char *)malloc(TMPBUFSIZ+1);
  410.  
  411.     if (s2 = (char **) malloc(n * sizeof(char *))) {
  412.     for (i = 0; i < n; i++) {    /* Find longest keyword */
  413.         s2[i] = NULL;
  414.         if (ckstrcmp(s[i].kwd,pat,cc,0))
  415.           continue;
  416.         if (s[i].flgs & CM_INV) {
  417. #ifdef COMMENT
  418. /* This code does not show invisible keywords at all except for "help ?" */
  419. /* and then only help topics (CM_HLP) in the top-level keyword list. */
  420.  
  421.         if ((xhlp & 2) == 0)
  422.           continue;
  423.         else if ((s[i].flgs & CM_HLP) == 0)
  424.           continue;
  425. #else
  426. /* This code shows invisible keywords that are not also abbreviations when */
  427. /* ? was typed AFTER the beginning of the field so the user can find out */
  428. /* what they are and (for example) why completion doesn't work at this point */
  429.  
  430.         if (s[i].flgs & CM_ABR)
  431.           continue;
  432.         else if ((xhlp & 3) == 0)
  433.           continue;
  434.         else if ((xhlp & 2) && ((s[i].flgs & CM_HLP) == 0))
  435.           continue;
  436. #endif /* COMMENT */
  437.         }
  438.         j = strlen(s[i].kwd);
  439.         if (!(xhlp & 4) || !tmpbuf) { /* Regular keyword table */
  440.         s2[n2++] = s[i].kwd;    /* Copy pointers to visible ones */
  441.         } else {            /* Switches */
  442.         sprintf(tmpbuf,        /* Make a copy that shows ":" if */
  443.             "%s%s",        /* the switch takes an argument. */
  444.             s[i].kwd,
  445.             (s[i].flgs & CM_ARG) ? ":" : ""
  446.             );
  447.         makestr(&(s2[n2]),tmpbuf);
  448.         if (s[i].flgs & CM_ARG) j++;
  449.         n2++;
  450.         }
  451.         if (j > width)
  452.           width = j;
  453.     }
  454.     /* Column width */
  455.     n = n2;
  456.     }
  457.     if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer   */
  458.     char * bx;
  459.     bx = b + cmd_cols;
  460.     width += (int)strlen(pre) + (int)strlen(post) + 2;
  461.     cols = cmd_cols / width;    /* How many columns? */
  462.     if (cols < 1) cols = 1;
  463.     height = n / cols;        /* How long is each column? */
  464.     if (n % cols) height++;        /* Add one for remainder, if any */
  465.  
  466.     for (i = 0; i < height; i++) {        /* Loop for each row */
  467.         for (j = 0; j < cmd_cols; j++)  /* First fill row with blanks */
  468.           b[j] = SP;
  469.         for (j = 0; j < cols; j++) {    /* Loop for each column in row */
  470.         k = i + (j * height);       /* Index of next keyword */
  471.         if (k < n) {            /* In range? */
  472.             pa = pre;
  473.             px = post;
  474.             p = s2[k];            /* Point to verb name */
  475.             q = b + (j * width) + 1; /* Where to copy it to */
  476.             while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
  477.             q--;                         /* Back up over NUL */
  478.             while ((q < bx) && (*q++ = *p++)) ;     /* Copy filename */
  479.             q--;                         /* Back up over NUL */
  480.             while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
  481.             if (j < cols - 1) {
  482.             q--;
  483.             *q = SP;    /* Replace the space */
  484.             }
  485.         }
  486.         }
  487.         p = b + cmd_cols - 1;    /* Last char in line */
  488.         while (*p-- == SP) ;    /* Trim */
  489.         *(p+2) = NUL;
  490.         printf("%s\n",b);        /* Print the line */
  491.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  492.         if (!askmore())        /* Do more-prompting... */
  493.           goto xkwdhelp;
  494.         else
  495.           lc = 0;
  496.         }
  497.     }
  498.     /* printf("\n"); */        /* Blank line at end of report */
  499.     } else {                /* Malloc failure, no columns */
  500.     for (i = 0; i < n; i++) {
  501.         if (s[i].flgs & CM_INV)    /* Use original keyword table */
  502.           continue;            /* skipping invisible entries */
  503.         printf("%s%s%s\n",pre,s[i].kwd,post);
  504.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  505.         if (!askmore())        /* Do more-prompting... */
  506.           goto xkwdhelp;
  507.         else
  508.           lc = 0;
  509.         }
  510.     }
  511.     }
  512.   xkwdhelp:
  513.     if (xhlp & 4) {
  514.     if (tmpbuf) free(tmpbuf);
  515.     for (i = 0; i < n; i++)
  516.       if (s2[i]) free(s2[i]);
  517.     }
  518.     if (s2) free(s2);            /* Free array copy */
  519.     if (b) free(b);            /* Free line buffer */
  520.     return;
  521. }
  522.  
  523. /*  F I L H E L P  --  Given a file list, print names in columns.  */
  524. /*
  525.   Call with:
  526.     n     - number of entries
  527.     pre   - prefix to add to each filename
  528.     post  - suffix to add to each filename
  529.     off   - offset on first screenful, allowing room for introductory text
  530.     cmdirflg - 1 if only directory names should be listed, 0 to list all files
  531.  
  532.   Arranges filenames in columns with width based on longest filename.
  533.   Does "more?" prompting at end of screen.
  534.   Uses global cmd_rows and cmd_cols for screen size.
  535. */
  536.  
  537. int
  538. filhelp(n,pre,post,off,cmdirflg) int n, off; char *pre, *post; int cmdirflg; {
  539.     char filbuf[CKMAXPATH + 1];        /* Temp buffer for one filename */
  540.     int width = 0;
  541.     int cols, height, i, j, k, lc, n2 = 0, rc = 0, itsadir = 0;
  542.     char *b = NULL, *p, *q;
  543.     char *pa, *px;
  544.     char **s2 = NULL;
  545. #ifdef VMS
  546.     char * cdp = zgtdir();
  547. #endif /* VMS */
  548.  
  549.     if (n < 1) return(0);
  550.     if (off < 0) off = 0;        /* Offset for first page */
  551.     if (!pre) pre = "";            /* Handle null string pointers */
  552.     if (!post) post = "";
  553.     lc = off;                /* Screen-line counter */
  554.  
  555.     if (s2 = (char **) malloc(n * sizeof(char *))) {
  556.     for (i = 0; i < n; i++) {    /* Loop through filenames */
  557.         itsadir = 0;
  558.         s2[i] = NULL;        /* Initialize each pointer to NULL */
  559.         znext(filbuf);        /* Get next filename */
  560.         if (!filbuf[0])        /* Shouldn't happen */
  561.           break;
  562. #ifdef COMMENT
  563.         itsadir = isdir(filbuf);    /* Is it a directory? */
  564.         if (cmdirflg && !itsadir)    /* No, listing directories only? */
  565.           continue;            /* So skip this one. */
  566. #endif /* COMMENT */
  567. #ifdef VMS
  568.         ckstrncpy(filbuf,zrelname(filbuf,cdp),CKMAXPATH);
  569. #endif /* VMS */
  570.         j = strlen(filbuf);
  571. #ifndef VMS
  572.         if (itsadir && j < CKMAXPATH - 1 && j > 0) {
  573.         if (filbuf[j-1] != dirsep) {
  574.             filbuf[j++] = dirsep;
  575.             filbuf[j] = NUL;
  576.         }
  577.         }
  578. #endif /* VMS */
  579.         if (!(s2[n2] = malloc(j+1))) {
  580.         printf("?Memory allocation failure\n");
  581.         rc = -9;
  582.         goto xfilhelp;
  583.         }
  584.         if (j <= CKMAXPATH) {
  585.         strcpy(s2[n2],filbuf);
  586.         n2++;
  587.         } else {
  588.         printf("?Name too long - %s\n", filbuf);
  589.         rc = -9;
  590.         goto xfilhelp;
  591.         }
  592.         if (j > width)        /* Get width of widest one */
  593.           width = j;
  594.     }
  595.     n = n2;                /* How many we actually got */
  596.     }
  597.     sh_sort(s2,NULL,n,0,0,filecase);    /* Alphabetize the list */
  598.  
  599.     rc = 1;
  600.     if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */
  601.     char * bx;
  602.     bx = b + cmd_cols;
  603.     width += (int)strlen(pre) + (int)strlen(post) + 2;
  604.     cols = cmd_cols / width;    /* How many columns? */
  605.     if (cols < 1) cols = 1;
  606.     height = n / cols;        /* How long is each column? */
  607.     if (n % cols) height++;        /* Add one for remainder, if any */
  608.  
  609.     for (i = 0; i < height; i++) {        /* Loop for each row */
  610.         for (j = 0; j < cmd_cols; j++)  /* First fill row with blanks */
  611.           b[j] = SP;
  612.         for (j = 0; j < cols; j++) {    /* Loop for each column in row */
  613.         k = i + (j * height);       /* Index of next filename */
  614.         if (k < n) {            /* In range? */
  615.             pa = pre;
  616.             px = post;
  617.             p = s2[k];                       /* Point to filename */
  618.             q = b + (j * width) + 1;             /* and destination */
  619.             while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
  620.             q--;                         /* Back up over NUL */
  621.             while ((q < bx) && (*q++ = *p++)) ;     /* Copy filename */
  622.             q--;                         /* Back up over NUL */
  623.             while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
  624.             if (j < cols - 1) {
  625.             q--;
  626.             *q = SP;    /* Replace the space */
  627.             }
  628.         }
  629.         }
  630.         p = b + cmd_cols - 1;    /* Last char in line */
  631.         while (*p-- == SP) ;    /* Trim */
  632.         *(p+2) = NUL;
  633.         printf("%s\n",b);        /* Print the line */
  634.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  635.         if (!askmore()) {    /* Do more-prompting... */
  636.             rc = 0;
  637.             goto xfilhelp;
  638.         } else
  639.           lc = 0;
  640.         }
  641.     }
  642.     printf("\n");            /* Blank line at end of report */
  643.     goto xfilhelp;
  644.     } else {                /* Malloc failure, no columns */
  645.     for (i = 0; i < n; i++) {
  646.         znext(filbuf);
  647.         if (!filbuf[0]) break;
  648.         printf("%s%s%s\n",pre,filbuf,post);
  649.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  650.         if (!askmore()) {     /* Do more-prompting... */
  651.             rc = 0;
  652.             goto xfilhelp;
  653.         } else lc = 0;
  654.         }
  655.     }
  656. xfilhelp:
  657.     if (b) free(b);
  658.     for (i = 0; i < n2; i++)
  659.       if (s2[i]) free(s2[i]);
  660.     if (s2) free(s2);
  661.     return(rc);
  662.     }
  663. }
  664.  
  665. /*  C M S E T U P  --  Set up command buffers  */
  666.  
  667. #ifdef DCMDBUF
  668. int
  669. cmsetup() {
  670.     if (!(cmdbuf = malloc(CMDBL + 4))) return(-1);
  671.     if (!(savbuf = malloc(CMDBL + 4))) return(-1);
  672.     savbuf[0] = '\0';
  673. #ifdef OLDHELP
  674.     if (!(hlpbuf = malloc(HLPBL + 4))) return(-1);
  675. #endif /* OLDHELP */
  676.     if (!(atmbuf = malloc(ATMBL + 4))) return(-1);
  677.     if (!(atxbuf = malloc(CMDBL + 4))) return(-1);
  678.     if (!(atybuf = malloc(ATMBL + 4))) return(-1);
  679.     if (!(filbuf = malloc(ATMBL + 4))) return(-1);
  680.     if (!(cmprom = malloc(PROMPTL + 4))) return(-1);
  681.     if (!(cmprxx = malloc(PROMPTL + 4))) return(-1);
  682. #ifdef CK_RECALL
  683.     cmrini(cm_recall);
  684. #endif /* CK_RECALL */
  685.     return(0);
  686. }
  687. #endif /* DCMDBUF */
  688.  
  689. /*  C M S E T P  --  Set the program prompt.  */
  690.  
  691. VOID
  692. cmsetp(s) char *s; {
  693.     if (!s) s = "";
  694.     ckstrncpy(cmprxx,s,PROMPTL);
  695.     psetf = 1;                          /* Flag that prompt has been set. */
  696. }
  697.  
  698. /*  C M S A V P  --  Save a copy of the current prompt.  */
  699.  
  700. VOID
  701. #ifdef CK_ANSIC
  702. cmsavp(char s[], int n)
  703. #else
  704. cmsavp(s,n) char s[]; int n;
  705. #endif /* CK_ANSIC */
  706. /* cmsavp */ {
  707.     if (psetf)                /* But not if no prompt is set. */
  708.       ckstrncpy(s,cmprxx,n);
  709. }
  710.  
  711. int
  712. cmgbrk() {
  713.     return(brkchar);
  714. }
  715.  
  716. int
  717. cmgkwflgs() {
  718.     return(cmkwflgs);
  719. }
  720.  
  721. /*  P R O M P T  --  Issue the program prompt.  */
  722.  
  723. VOID
  724. prompt(f) xx_strp f; {
  725.     char *sx, *sy; int n;
  726. #ifdef CK_SSL
  727.     extern int ssl_active_flag, tls_active_flag;
  728. #endif /* CK_SSL */
  729. #ifdef OS2
  730.     extern int display_demo;
  731.  
  732.     /* If there is a demo screen to be displayed, display it */
  733.     if (display_demo && cmdsrc() == 0) {
  734.         demoscrn();
  735.         display_demo = 0;
  736.     }
  737. #endif /* OS2 */
  738.  
  739.     if (psetf == 0)            /* If no prompt set, set default. */
  740.       cmsetp(dfprom);
  741.  
  742.     sx = cmprxx;            /* Unevaluated copy */
  743.     if (f) {                /* If conversion function given */
  744.     sy = cmprom;            /* Evaluate it */
  745.     debug(F101,"prompt sx","",sx);
  746.     debug(F101,"prompt sy","",sy);
  747.     n = PROMPTL;
  748.     if ((*f)(sx,&sy,&n) < 0)    /* If evaluation failed */
  749.       sx = cmprxx;            /* revert to unevaluated copy */
  750.     else if (!*cmprom)        /* ditto if it came up empty */
  751.       sx = cmprxx;
  752.     else
  753.       sx = cmprom;
  754.     } else
  755.       ckstrncpy(cmprom,sx,PROMPTL);
  756.     cmprom[PROMPTL-1] = NUL;
  757.     if (!*sx)                /* Don't print if empty */
  758.       return;
  759.  
  760. #ifdef OSK
  761.     fputs(sx, stdout);
  762. #else
  763. #ifdef MAC
  764.     printf("%s", sx);
  765. #else
  766.     printf("\r%s",sx);            /* Print the prompt. */
  767. #ifdef CK_SSL
  768.     if (!(ssl_active_flag || tls_active_flag))
  769. #endif /* CK_SSL */
  770.       fflush(stdout);            /* Now! */
  771. #endif /* MAC */
  772. #endif /* OSK */
  773. }
  774.  
  775. #ifndef NOSPL
  776. VOID
  777. pushcmd(s) char * s; {            /* For use with IF command. */
  778.     if (!s) s = np;
  779.     ckstrncpy(savbuf,s,CMDBL);        /* Save the dependent clause,  */
  780.     cmres();                /* and clear the command buffer. */
  781.     debug(F110, "pushcmd savbuf", savbuf, 0);
  782. }
  783.  
  784. VOID
  785. pushqcmd(s) char * s; {            /* For use with ELSE command. */
  786.     char c, * p = savbuf;        /* Dest */
  787.     if (!s) s = np;            /* Source */
  788.     while (*s) {            /* Get first nonwhitespace char */
  789.     if (*s != SP)
  790.       break;
  791.     else
  792.       s++;
  793.     }
  794.     if (*s != '{') {            /* If it's not "{" */
  795.     pushcmd(s);            /* do regular pushcmd */
  796.     return;
  797.     }
  798.     while (c = *s++) {            /* Otherwise insert quotes */
  799.     if (c == CMDQ)
  800.       *p++ = CMDQ;
  801.     *p++ = c;
  802.     }
  803.     cmres();                /* and clear the command buffer. */
  804.     debug(F110, "pushqcmd savbuf", savbuf, 0);
  805. }
  806. #endif /* NOSPL */
  807.  
  808. #ifdef COMMENT
  809. /* no longer used... */
  810. VOID
  811. popcmd() {
  812.     ckstrncpy(cmdbuf,savbuf,CMDBL);    /* Put back the saved material */
  813.     *savbuf = '\0';            /* and clear the save buffer */
  814.     cmres();
  815. }
  816. #endif /* COMMENT */
  817.  
  818. /*  C M R E S  --  Reset pointers to beginning of command buffer.  */
  819.  
  820. VOID
  821. cmres() {
  822.     inword = 0;                /* We're not in a word */
  823.     cc = 0;                /* Character count is zero */
  824.  
  825. /* Initialize pointers */
  826.  
  827.     pp = cmdbuf;            /* Beginning of current field */
  828.     bp = cmdbuf;            /* Current position within buffer */
  829.     np = cmdbuf;            /* Where to start next field */
  830.  
  831.     cmfldflgs = 0;
  832.     cmflgs = -5;                        /* Parse not yet started. */
  833.     ungw = 0;                /* Don't need to unget a word. */
  834. }
  835.  
  836. /*  C M I N I  --  Clear the command and atom buffers, reset pointers.  */
  837.  
  838. /*
  839. The argument specifies who is to echo the user's typein --
  840.   1 means the cmd package echoes
  841.   0 somebody else (system, front end, terminal) echoes
  842. */
  843. VOID
  844. cmini(d) int d; {
  845. #ifdef DCMDBUF
  846.     if (!atmbuf)
  847.       if (cmsetup()<0)
  848.     fatal("fatal error: unable to allocate command buffers");
  849. #endif /* DCMDBUF */
  850. #ifdef USE_MEMCPY
  851.     memset(cmdbuf,0,CMDBL);
  852.     memset(atmbuf,0,ATMBL);
  853. #else
  854.     for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  855.     for (bp = atmbuf; bp < atmbuf+ATMBL; bp++) *bp = NUL;
  856. #endif /* USE_MEMCPY */
  857.  
  858.     *atmbuf = *savbuf = *atxbuf = *atybuf = *filbuf = NUL;
  859. #ifdef OLDHELP
  860.     *hlpbuf = NUL;
  861. #endif /* OLDHELP */
  862.     blocklvl = 0;        /* Block level is 0 */
  863.     linebegin = 1;        /* And we're at the beginning of a line */
  864.     dpx = d;            /* Make a global copy of the echo flag */
  865.     debug(F101,"cmini dpx","",dpx);
  866.     crflag = 0;
  867.     qmflag = 0;
  868.     esflag = 0;
  869. #ifdef CK_RECALL
  870.     no_recall = 0;
  871. #endif /* CK_RECALL */
  872.     cmres();
  873. }
  874.  
  875. #ifndef NOSPL
  876. /* The following bits are to allow the command package to call itself */
  877. /* in the middle of a parse.  To do this, begin by calling cmpush, and */
  878. /* end by calling cmpop. */
  879.  
  880. #ifdef DCMDBUF
  881. struct cmp {
  882.     int i[5];                /* stack for integers */
  883.     char *c[3];                /* stack for pointers */
  884.     char *b[8];                /* stack for buffer contents */
  885. };
  886. struct cmp *cmp = 0;
  887. #else
  888. int cmp_i[CMDDEP+1][5];            /* Stack for integers */
  889. char *cmp_c[CMDDEP+1][5];        /* for misc pointers */
  890. char *cmp_b[CMDDEP+1][7];        /* for buffer contents pointers */
  891. #endif /* DCMDBUF */
  892.  
  893. int cmddep = -1;            /* Current stack depth */
  894.  
  895. int
  896. cmpush() {                /* Save the command environment */
  897.     char *cp;                /* Character pointer */
  898.  
  899.     if (cmddep >= CMDDEP)        /* Enter a new command depth */
  900.       return(-1);
  901.     cmddep++;
  902.     debug(F101,"&cmpush","",cmddep);
  903.  
  904. #ifdef DCMDBUF
  905.     /* allocate memory for cmp if not already done */
  906.     if (!cmp && !(cmp = (struct cmp *) malloc(sizeof(struct cmp)*(CMDDEP+1))))
  907.       fatal("cmpush: no memory for cmp");
  908.     cmp[cmddep].i[0] = cmflgs;        /* First do the global ints */
  909.     cmp[cmddep].i[1] = cmfsav;
  910.     cmp[cmddep].i[2] = atxn;
  911.     cmp[cmddep].i[3] = ungw;
  912.  
  913.     cmp[cmddep].c[0] = bp;        /* Then the global pointers */
  914.     cmp[cmddep].c[1] = pp;
  915.     cmp[cmddep].c[2] = np;
  916. #else
  917.     cmp_i[cmddep][0] = cmflgs;        /* First do the global ints */
  918.     cmp_i[cmddep][1] = cmfsav;
  919.     cmp_i[cmddep][2] = atxn;
  920.     cmp_i[cmddep][3] = ungw;
  921.  
  922.     cmp_c[cmddep][0] = bp;        /* Then the global pointers */
  923.     cmp_c[cmddep][1] = pp;
  924.     cmp_c[cmddep][2] = np;
  925. #endif /* DCMDBUF */
  926.  
  927.     /* Now the buffers themselves.  A lot of repititious code... */
  928.  
  929. #ifdef DCMDBUF
  930.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  931.     if (cp) strcpy(cp,cmdbuf);
  932.     cmp[cmddep].b[0] = cp;
  933.     if (cp == NULL) return(-1);
  934.  
  935.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  936.     if (cp) strcpy(cp,savbuf);
  937.     cmp[cmddep].b[1] = cp;
  938.     if (cp == NULL) return(-1);
  939.  
  940. #ifdef OLDHELP
  941.     cp = malloc((int)strlen(hlpbuf)+1);    /* 2: Help string buffer */
  942.     if (cp) strcpy(cp,hlpbuf);
  943.     cmp[cmddep].b[2] = cp;
  944.     if (cp == NULL) return(-1);
  945. #else
  946.     cmp[cmddep].b[2] = NULL;
  947. #endif /* OLDHELP */
  948.  
  949.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  950.     if (cp) strcpy(cp,atmbuf);
  951.     cmp[cmddep].b[3] = cp;
  952.     if (cp == NULL) return(-1);
  953.  
  954.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  955.     if (cp) strcpy(cp,atxbuf);
  956.     cmp[cmddep].b[4] = cp;
  957.     if (cp == NULL) return(-1);
  958.  
  959.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  960.     if (cp) strcpy(cp,atybuf);
  961.     cmp[cmddep].b[5] = cp;
  962.     if (cp == NULL) return(-1);
  963.  
  964.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  965.     if (cp) strcpy(cp,filbuf);
  966.     cmp[cmddep].b[6] = cp;
  967.     if (cp == NULL) return(-1);
  968. #else
  969.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  970.     if (cp) strcpy(cp,cmdbuf);
  971.     cmp_b[cmddep][0] = cp;
  972.     if (cp == NULL) return(-1);
  973.  
  974.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  975.     if (cp) strcpy(cp,savbuf);
  976.     cmp_b[cmddep][1] = cp;
  977.     if (cp == NULL) return(-1);
  978.  
  979. #ifdef OLDHELP
  980.     cp = malloc((int)strlen(hlpbuf)+1);    /* 2: Help string buffer */
  981.     if (cp) strcpy(cp,hlpbuf);
  982.     cmp_b[cmddep][2] = cp;
  983.     if (cp == NULL) return(-1);
  984. #else
  985.     cmp_b[cmddep][2] = NULL;
  986. #endif /* OLDHELP */
  987.  
  988.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  989.     if (cp) strcpy(cp,atmbuf);
  990.     cmp_b[cmddep][3] = cp;
  991.     if (cp == NULL) return(-1);
  992.  
  993.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  994.     if (cp) strcpy(cp,atxbuf);
  995.     cmp_b[cmddep][4] = cp;
  996.     if (cp == NULL) return(-1);
  997.  
  998.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  999.     if (cp) strcpy(cp,atybuf);
  1000.     cmp_b[cmddep][5] = cp;
  1001.     if (cp == NULL) return(-1);
  1002.  
  1003.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  1004.     if (cp) strcpy(cp,filbuf);
  1005.     cmp_b[cmddep][6] = cp;
  1006.     if (cp == NULL) return(-1);
  1007. #endif /* DCMDBUF */
  1008.  
  1009.     cmini(dpx);                /* Initize the command parser */
  1010.     return(0);
  1011. }
  1012.  
  1013. int
  1014. cmpop() {                /* Restore the command environment */
  1015.     debug(F101,"&cmpop","",cmddep);
  1016.     if (cmddep < 0) return(-1);        /* Don't pop too much! */
  1017.  
  1018. #ifdef DCMDBUF
  1019.     cmflgs = cmp[cmddep].i[0];        /* First do the global ints */
  1020.     cmfsav = cmp[cmddep].i[1];
  1021.     atxn = cmp[cmddep].i[2];
  1022.     ungw = cmp[cmddep].i[3];
  1023.  
  1024.     bp = cmp[cmddep].c[0];        /* Then the global pointers */
  1025.     pp = cmp[cmddep].c[1];
  1026.     np = cmp[cmddep].c[2];
  1027. #else
  1028.     cmflgs = cmp_i[cmddep][0];        /* First do the global ints */
  1029.     cmfsav = cmp_i[cmddep][1];
  1030.     atxn = cmp_i[cmddep][2];
  1031.     ungw = cmp_i[cmddep][3];
  1032.  
  1033.     bp = cmp_c[cmddep][0];        /* Then the global pointers */
  1034.     pp = cmp_c[cmddep][1];
  1035.     np = cmp_c[cmddep][2];
  1036. #endif /* DCMDBUF */
  1037.  
  1038.     /* Now the buffers themselves. */
  1039.     /* Note: strncpy(), not ckstrncpy() -- Here we WANT the NUL padding... */
  1040.  
  1041. #ifdef DCMDBUF
  1042.     if (cmp[cmddep].b[0]) {
  1043.  
  1044.     strncpy(cmdbuf,cmp[cmddep].b[0],CMDBL); /* 0: Command buffer */
  1045.     free(cmp[cmddep].b[0]);
  1046.     cmp[cmddep].b[0] = NULL;
  1047.     }
  1048.     if (cmp[cmddep].b[1]) {
  1049.     strncpy(savbuf,cmp[cmddep].b[1],CMDBL); /* 1: Save buffer */
  1050.     free(cmp[cmddep].b[1]);
  1051.     cmp[cmddep].b[1] = NULL;
  1052.     }
  1053. #ifdef OLDHELP
  1054.     if (cmp[cmddep].b[2]) {
  1055.     strncpy(hlpbuf,cmp[cmddep].b[2],HLPBL); /* 2: Help buffer */
  1056.     free(cmp[cmddep].b[2]);
  1057.     cmp[cmddep].b[2] = NULL;
  1058.     }
  1059. #endif /* OLDHELP */
  1060.     if (cmp[cmddep].b[3]) {
  1061.     strncpy(atmbuf,cmp[cmddep].b[3],ATMBL); /* 3: Atomic buffer! */
  1062.     free(cmp[cmddep].b[3]);
  1063.     cmp[cmddep].b[3] = NULL;
  1064.     }
  1065.     if (cmp[cmddep].b[4]) {
  1066.     strncpy(atxbuf,cmp[cmddep].b[4],ATMBL); /* 4: eXpansion buffer */
  1067.     free(cmp[cmddep].b[4]);
  1068.     cmp[cmddep].b[4] = NULL;
  1069.     }
  1070.     if (cmp[cmddep].b[5]) {
  1071.     strncpy(atybuf,cmp[cmddep].b[5],ATMBL); /* 5: Atom buffer copY */
  1072.     free(cmp[cmddep].b[5]);
  1073.     cmp[cmddep].b[5] = NULL;
  1074.     }
  1075.     if (cmp[cmddep].b[6]) {
  1076.     strncpy(filbuf,cmp[cmddep].b[6],ATMBL); /* 6: Filename buffer */
  1077.     free(cmp[cmddep].b[6]);
  1078.     cmp[cmddep].b[6] = NULL;
  1079.     }
  1080. #else
  1081.     if (cmp_b[cmddep][0]) {
  1082.     strncpy(cmdbuf,cmp_b[cmddep][0],CMDBL); /* 0: Command buffer */
  1083.     free(cmp_b[cmddep][0]);
  1084.     cmp_b[cmddep][0] = NULL;
  1085.     }
  1086.     if (cmp_b[cmddep][1]) {
  1087.     strncpy(savbuf,cmp_b[cmddep][1],CMDBL); /* 1: Save buffer */
  1088.     free(cmp_b[cmddep][1]);
  1089.     cmp_b[cmddep][1] = NULL;
  1090.     }
  1091. #ifdef OLDHELP
  1092.     if (cmp_b[cmddep][2]) {
  1093.     strncpy(hlpbuf,cmp_b[cmddep][2],HLPBL); /* 2: Help buffer */
  1094.     free(cmp_b[cmddep][2]);
  1095.     cmp_b[cmddep][2] = NULL;
  1096.     }
  1097. #endif /* OLDHELP */
  1098.     if (cmp_b[cmddep][3]) {
  1099.     strncpy(atmbuf,cmp_b[cmddep][3],ATMBL); /* 3: Atomic buffer! */
  1100.     free(cmp_b[cmddep][3]);
  1101.     cmp_b[cmddep][3] = NULL;
  1102.     }
  1103.     if (cmp_b[cmddep][4]) {
  1104.     strncpy(atxbuf,cmp_b[cmddep][4],ATMBL); /* 4: eXpansion buffer */
  1105.     free(cmp_b[cmddep][4]);
  1106.     cmp_b[cmddep][4] = NULL;
  1107.     }
  1108.     if (cmp_b[cmddep][5]) {
  1109.     strncpy(atybuf,cmp_b[cmddep][5],ATMBL); /* 5: Atom buffer copY */
  1110.     free(cmp_b[cmddep][5]);
  1111.     cmp_b[cmddep][5] = NULL;
  1112.     }
  1113.     if (cmp_b[cmddep][6]) {
  1114.     strncpy(filbuf,cmp_b[cmddep][6],ATMBL); /* 6: Filename buffer */
  1115.     free(cmp_b[cmddep][6]);
  1116.     cmp_b[cmddep][6] = NULL;
  1117.     }
  1118. #endif /* DCMDBUF */
  1119.  
  1120.     cmddep--;                /* Rise, rise */
  1121.     debug(F101,"&cmpop","",cmddep);
  1122.     return(cmddep);
  1123. }
  1124. #endif /* NOSPL */
  1125.  
  1126. #ifdef COMMENT
  1127. VOID
  1128. stripq(s) char *s; {                    /* Function to strip '\' quotes */
  1129.     char *t;
  1130.     while (*s) {
  1131.         if (*s == CMDQ) {
  1132.             for (t = s; *t != '\0'; t++) *t = *(t+1);
  1133.         }
  1134.         s++;
  1135.     }
  1136. }
  1137. #endif /* COMMENT */
  1138.  
  1139. /* Convert tabs to spaces, one for one */
  1140. VOID
  1141. untab(s) char *s; {
  1142.     while (*s) {
  1143.     if (*s == HT) *s = SP;
  1144.     s++;
  1145.     }
  1146. }
  1147.  
  1148. /*  C M N U M  --  Parse a number in the indicated radix  */
  1149.  
  1150. /*
  1151.  The only radix allowed in unquoted numbers is 10.
  1152.  Parses unquoted numeric strings in base 10.
  1153.  Parses backslash-quoted numbers in the radix indicated by the quote:
  1154.    \nnn = \dnnn = decimal, \onnn = octal, \xnn = Hexadecimal.
  1155.  If these fail, then if a preprocessing function is supplied, that is applied
  1156.  and then a second attempt is made to parse an unquoted decimal string.
  1157.  And if that fails, the preprocessed string is passed to an arithmetic
  1158.  expression evaluator.
  1159.  
  1160.  Returns:
  1161.    -3 if no input present when required,
  1162.    -2 if user typed an illegal number,
  1163.    -1 if reparse needed,
  1164.     0 otherwise, with argument n set to the number that was parsed
  1165. */
  1166. int
  1167. cmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; xx_strp f; {
  1168.     int x; char *s, *zp, *zq;
  1169.     char lbrace, rbrace;
  1170.  
  1171.     if (!xhlp) xhlp = "";
  1172.     if (!xdef) xdef = "";
  1173.  
  1174.     if (cmfldflgs & 1) {
  1175.     lbrace = '(';
  1176.     rbrace = ')';
  1177.     } else {
  1178.     lbrace = '{';
  1179.     rbrace = '}';
  1180.     }
  1181.     if (radix != 10) {                  /* Just do base 10 */
  1182.         printf("cmnum: illegal radix - %d\n",radix);
  1183.         return(-1);
  1184.     } /* Easy to add others but there has never been a need for it. */
  1185.     x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  1186.     debug(F101,"cmnum: cmfld","",x);
  1187.     if (x < 0) return(x);        /* Parse a field */
  1188.     zp = atmbuf;
  1189. /*
  1190.   Edit 192 - Allow any number field to be braced.  This lets us include
  1191.   spaces in expressions, but perhaps more important lets us have user-defined
  1192.   functions in numeric fields.
  1193. */
  1194.     if (*zp == lbrace) {        /* Braced field, strip braces */
  1195.     x = (int) strlen(atmbuf);
  1196.     if (x > 0) {            /* The "if" is to shut up optimizers */
  1197.         if (*(atmbuf+x-1) == rbrace) {
  1198.         *(atmbuf+x-1) = NUL;    /* that complain about a possible */
  1199.         zp++;            /* reference to atbmbuf[-1] even */
  1200.         }
  1201.     }                /* though we know that x > 0. */
  1202.     }
  1203.     if (chknum(zp)) {            /* Check for decimal number */
  1204.         *n = atoi(zp);            /* Got one, we're done. */
  1205.     debug(F101,"cmnum 1st chknum ok","",*n);
  1206.         return(0);
  1207.     } else if ((x = xxesc(&zp)) > -1) {    /* Check for backslash escape */
  1208.  
  1209. #ifndef OS2
  1210.     *n = x;
  1211. #else
  1212.     *n = wideresult;
  1213. #endif /* OS2 */
  1214.  
  1215.     debug(F101,"cmnum xxesc ok","",*n);
  1216.     return(*zp ? -2 : 0);
  1217.     } else if (f) {            /* If conversion function given */
  1218.     zq = atxbuf;            /* Try that */
  1219.     atxn = CMDBL;
  1220.     if ((*f)(zp,&zq,&atxn) < 0)    /* Convert */
  1221.       return(-2);
  1222.     zp = atxbuf;
  1223.     }
  1224.     debug(F110,"cmnum zp 1",zp,0);
  1225.     if (!*zp) zp = xdef;        /* Result empty, substitute default */
  1226.     debug(F110,"cmnum zp 2",zp,0);
  1227.     if (chknum(zp)) {            /* Check again for decimal number */
  1228.         *n = atoi(zp);            /* Got one, we're done. */
  1229.     debug(F101,"cmnum 2nd chknum ok","",*n);
  1230.         return(0);
  1231. #ifndef NOSPL
  1232.     }  else if ((x = xxesc(&zp)) > -1) { /* Check for backslash escape */
  1233. #ifndef OS2
  1234.     *n = x;
  1235. #else
  1236.     *n = wideresult;
  1237. #endif /* OS2 */
  1238.     debug(F101,"cmnum xxesc 2 ok","",*n);
  1239.     return(*zp ? -2 : 0);
  1240.     } else if (f) {            /* Not numeric, maybe an expression */
  1241.     char * p;
  1242.     p = evala(zp);
  1243.     if (chknum(p)) {
  1244.         *n = atoi(p);
  1245.         debug(F101,"cmnum exp eval ok","",*n);
  1246.         return(0);
  1247.     } else return(-2);
  1248. #endif /* NOSPL */
  1249.     } else {                /* Not numeric */
  1250.     return(-2);
  1251.     }
  1252. }
  1253.  
  1254. #ifdef CKCHANNELIO
  1255. extern int z_error;
  1256. #endif /* CKCHANNELIO */
  1257.  
  1258. /*  C M O F I  --  Parse the name of an output file  */
  1259.  
  1260. /*
  1261.  Depends on the external function zchko(); if zchko() not available, use
  1262.  cmfld() to parse output file names.
  1263.  
  1264.  Returns:
  1265.    -9 like -2, except message already printed,
  1266.    -3 if no input present when required,
  1267.    -2 if permission would be denied to create the file,
  1268.    -1 if reparse needed,
  1269.     0 or 1 if file can be created, with xp pointing to name.
  1270.     2 if given the name of an existing directory.
  1271. */
  1272. int
  1273. cmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1274.     int x; char *s, *zq;
  1275. #ifdef DOCHKVAR
  1276.     int tries;
  1277. #endif /* DOCHKVAR */
  1278. #ifdef DTILDE
  1279.     char *dirp;
  1280. #endif /* DTILDE */
  1281.  
  1282.     cmfldflgs = 0;
  1283.  
  1284.     if (!xhlp) xhlp = "";
  1285.     if (!xdef) xdef = "";
  1286.  
  1287.     if (*xhlp == NUL) xhlp = "Output file";
  1288.     *xp = "";
  1289.  
  1290.     x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  1291.     debug(F111,"cmofi cmfld returns",s,x);
  1292.     if (x < 0)
  1293.       return(x);
  1294.  
  1295.     if (*s == '{') {            /* Strip enclosing braces */
  1296.     int n;
  1297.     n = strlen(s);
  1298.     if (s[n-1] == '}') {
  1299.         s[n-1] = NUL;
  1300.         s++;
  1301.     }
  1302.     }
  1303.     debug(F110,"cmofi 1.5",s,0);
  1304.  
  1305. #ifdef DOCHKVAR
  1306.     tries = 0;
  1307.     {
  1308.     char *p = s;
  1309.     /*
  1310.       This is really ugly.  If we skip conversion the first time through,
  1311.       then variable names like \%a will be used as filenames (e.g. creating
  1312.       a file called %A in the root directory).  If we DON'T skip conversion
  1313.       the first time through, then single backslashes used as directory
  1314.       separators in filenames will be misinterpreted as variable lead-ins.
  1315.       So we prescan to see if it has any variable references.  But this
  1316.       module is not supposed to know anything about variables, functions,
  1317.       etc, so this code does not really belong here, but rather it should
  1318.       be at the same level as zzstring().
  1319.     */
  1320. /*
  1321.   Hmmm, this looks a lot like chkvar() except it that includes \nnn number
  1322.   escapes.  But why?  This makes commands like "mkdir c:\123" impossible.
  1323.   And in fact, "mkdir c:\123" creates a directory called "c:{".  What's worse,
  1324.   rmdir(), which *does* call chkvar(), won't let us remove it.  So let's at
  1325.   least try making cmofi() symmetrical with cmifi()...
  1326. */
  1327. #ifdef COMMENT
  1328.     char * q;
  1329.     while ( (tries == 0) && (p = strchr(p,CMDQ)) ) {
  1330.         q = *(p+1);            /* Char after backslash */
  1331.         if (!q)            /* None, quit */
  1332.           break;
  1333.         if (isupper(q))        /* If letter, convert to lowercase */
  1334.           q = tolower(q);
  1335.         if (isdigit(q)) {        /* If it's a digit, */
  1336.         tries = 1;        /* assume it's a backslash code  */
  1337.         break;
  1338.         }
  1339.         switch (q) {
  1340.           case CMDQ:        /* Double backslash */
  1341.         tries = 1;        /* so call the conversion function */
  1342.         break;
  1343.           case '%':            /* Variable or array reference */
  1344.           case '&':            /* must be followed by letter */
  1345.         if (isalpha(*(p+2)) || (*(p+2) >= '0' && *(p+2) <= '9'))
  1346.           tries = 1;
  1347.         break;
  1348.           case 'm': case 'v': case '$': /* \m(), \v(), \$() */
  1349.         if (*(p+2) == '(')
  1350.           if (strchr(p+2,')'))
  1351.             tries = 1;
  1352.         break;
  1353.           case 'f':            /* \Fname() */
  1354.         if (strchr(p+2,'('))
  1355.           if (strchr(p+2,')'))
  1356.               tries = 1;
  1357.         break;
  1358.           case '{':            /* \{...} */
  1359.         if (strchr(p+2,'}'))
  1360.           tries = 1;
  1361.         break;
  1362.           case 'd': case 'o':    /* Decimal or Octal number */
  1363.             if (isdigit(*(p+2)))
  1364.           tries = 1;
  1365.         break;
  1366.           case 'x':            /* Hex number */
  1367.         if (isdigit(*(p+2)) ||
  1368.             ((*(p+2) >= 'a' && *(p+2) <= 'f') ||
  1369.              ((*(p+2) >= 'A' && *(p+2) <= 'F'))))
  1370.           tries = 1;
  1371.           default:
  1372.         break;
  1373.         }
  1374.         p++;
  1375.     }
  1376. #else
  1377. #ifndef NOSPL
  1378.     if (f) {            /* If a conversion function is given */
  1379.         char *s = p;        /* See if there are any variables in */
  1380.         while (*s) {        /* the string and if so, expand them */
  1381.         if (chkvar(s)) {
  1382.             tries = 1;
  1383.             break;
  1384.         }
  1385.         s++;
  1386.         }
  1387.     }
  1388. #endif /* NOSPL */
  1389. #endif /* COMMENT */
  1390.     }
  1391. #ifdef OS2
  1392. o_again:
  1393. #endif /* OS2 */
  1394.     if (tries == 1)
  1395. #endif /* DOCHKVAR */
  1396.     if (f) {                /* If a conversion function is given */
  1397.     zq = atxbuf;            /* do the conversion. */
  1398.     atxn = CMDBL;
  1399.     if ((x = (*f)(s,&zq,&atxn)) < 0)
  1400.       return(-2);
  1401.     s = atxbuf;
  1402.     if (!*s)            /* Result empty, substitute default */
  1403.       s = xdef;
  1404.     }
  1405.     debug(F111,"cmofi 2",s,x);
  1406.  
  1407. #ifdef DTILDE
  1408.     dirp = tilde_expand(s);        /* Expand tilde, if any, */
  1409.     if (*dirp != '\0') {        /* right in the atom buffer. */
  1410.     if (setatm(dirp,1) < 0) {
  1411.         printf("?Name too long\n");
  1412.         return(-9);
  1413.     }
  1414.     }
  1415.     s = atmbuf;
  1416.     debug(F110,"cmofi 3",s,0);
  1417. #endif /* DTILDE */
  1418.  
  1419.     if (iswild(s)) {
  1420.         printf("?Wildcards not allowed - %s\n",s);
  1421.         return(-2);
  1422.     }
  1423.     debug(F110,"cmofi 4",s,0);
  1424.  
  1425. #ifdef CK_TMPDIR
  1426.     /* isdir() function required for this! */
  1427.     if (isdir(s)) {
  1428.     debug(F110,"cmofi 5: is directory",s,0);
  1429.         *xp = s;
  1430.     return(2);
  1431.     }
  1432. #endif /* CK_TMPDIR */
  1433.  
  1434.     if (strcmp(s,CTTNAM) && (zchko(s) < 0)) { /* OK to write to console */
  1435. #ifdef COMMENT
  1436. #ifdef OS2
  1437. /*
  1438.   We don't try again because we already prescanned the string to see if
  1439.   if it contained anything that could be used by zzstring().
  1440. */
  1441.     if (tries++ < 1)
  1442.       goto o_again;
  1443. #endif /* OS2 */
  1444. #endif /* COMMENT */
  1445. /*
  1446.   Note: there are certain circumstances where zchko() can give a false
  1447.   positive, so don't rely on it to catch every conceivable situation in
  1448.   which the given output file can't be created.  In other words, we print
  1449.   a message and fail here if we KNOW the file can't be created.  If we
  1450.   succeed but the file can't be opened, the code that tries to open the file
  1451.   has to print a message.
  1452. */
  1453.     debug(F110,"cmofi 6: failure",s,0);
  1454.         printf("?Write permission denied - %s\n",s);
  1455. #ifdef CKCHANNELIO
  1456.     z_error = FX_ACC;
  1457. #endif /* CKCHANNELIO */
  1458.         return(-9);
  1459.     } else {
  1460.     debug(F110,"cmofi 7: ok",s,0);
  1461.         *xp = s;
  1462.         return(x);
  1463.     }
  1464. }
  1465.  
  1466. /*  C M I F I  --  Parse the name of an existing file  */
  1467.  
  1468. /*
  1469.  This function depends on the external functions:
  1470.    zchki()  - Check if input file exists and is readable.
  1471.    zxpand() - Expand a wild file specification into a list.
  1472.    znext()  - Return next file name from list.
  1473.  If these functions aren't available, then use cmfld() to parse filenames.
  1474. */
  1475. /*
  1476.  Returns
  1477.    -4 EOF
  1478.    -3 if no input present when required,
  1479.    -2 if file does not exist or is not readable,
  1480.    -1 if reparse needed,
  1481.     0 or 1 otherwise, with:
  1482.         xp pointing to name,
  1483.         wild = 1 if name contains '*' or '?', 0 otherwise.
  1484. */
  1485.  
  1486. /*
  1487.    C M I O F I  --  Parse an input file OR the name of a nonexistent file.
  1488.  
  1489.    Use this when an existing file is wanted (so we get help, completion, etc),
  1490.    but if a file of the given name does not exist, the name of a new file is
  1491.    accepted.  For example, with the EDIT command (edit an existing file, or
  1492.    create a new file).  Returns -9 if file does not exist.  It is up to the
  1493.    caller to check creatability.
  1494. */
  1495. static int nomsg = 0;
  1496. int
  1497. cmiofi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  1498.     int msgsave, x;
  1499.     msgsave = nomsg;
  1500.     nomsg = 1;
  1501.     x = cmifi2(xhlp,xdef,xp,wild,0,NULL,f,0);
  1502.     nomsg = msgsave;
  1503.     return(x);
  1504. }
  1505.  
  1506. int
  1507. cmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  1508.     return(cmifi2(xhlp,xdef,xp,wild,0,NULL,f,0));
  1509. }
  1510. /*
  1511.   cmifip() is called when we want to supply a path or path list to search
  1512.   in case the filename that the user gives is (a) not absolute, and (b) can't
  1513.   be found as given.  The path string can be the name of a single directory,
  1514.   or a list of directories separated by the PATHSEP character, defined in
  1515.   ckucmd.h.  Look in ckuusr.c and ckuus3.c for examples of usage.
  1516. */
  1517. int
  1518. cmifip(xhlp,xdef,xp,wild,d,path,f)
  1519.     char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; {
  1520.     return(cmifi2(xhlp,xdef,xp,wild,0,path,f,0));
  1521. }
  1522.  
  1523. /*  C M D I R  --  Parse a directory name  */
  1524.  
  1525. /*
  1526.  This function depends on the external functions:
  1527.    isdir(s)  - Check if string s is the name of a directory
  1528.    zchki(s)  - Check if input file s exists and what type it is.
  1529.  If these functions aren't available, then use cmfld() to parse dir names.
  1530.  
  1531.  Returns
  1532.    -9 For all sorts of reasons, after printing appropriate error message.
  1533.    -4 EOF
  1534.    -3 if no input present when required,
  1535.    -2 if out of space or other internal error,
  1536.    -1 if reparse needed,
  1537.     0 or 1, with xp pointing to name, if directory specified,
  1538. */
  1539. int
  1540. cmdir(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1541.     int wild;
  1542.     return(cmifi2(xhlp,xdef,xp,&wild,0,NULL,f,1));
  1543. }
  1544.  
  1545. /* Like CMDIR but includes PATH search */
  1546.  
  1547. int
  1548. cmdirp(xhlp,xdef,xp,path,f) char *xhlp, *xdef, **xp; char * path; xx_strp f; {
  1549.     int wild;
  1550.     return(cmifi2(xhlp,xdef,xp,&wild,0,path,f,1));
  1551. }
  1552.  
  1553. /*
  1554.   cmifi2() is the base filename parser called by cmifi, cmifip, cmdir, etc.
  1555.   Use it directly when you also want to parse a directory or device
  1556.   name as an input file, as in the DIRECTORY command.  Call with:
  1557.     xhlp  -- help message on ?
  1558.     xdef  -- default response
  1559.     xp    -- pointer to result (in our space, must be copied from here)
  1560.     wild  -- flag set upon return to indicate if filespec was wild
  1561.     d     -- 0 to parse files, 1 to parse files or directories
  1562.     path  -- search path for files
  1563.     f     -- pointer to string processing function (e.g. to evaluate variables)
  1564.     dirflg -- 1 to parse *only* directories, 0 otherwise
  1565. */
  1566. int
  1567. cmifi2(xhlp,xdef,xp,wild,d,path,f,dirflg)
  1568.     char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; int dirflg; {
  1569.     extern int recursive, diractive;
  1570.     int i, x, itsadir, xc, expanded = 0, nfiles = 0;
  1571.     long y;
  1572.     char *sp = NULL, *zq, *np = NULL;
  1573.     char *sv = NULL, *p = NULL;
  1574. #ifdef DTILDE
  1575.     char *dirp;
  1576. #endif /* DTILDE */
  1577.  
  1578. #ifndef NOPARTIAL
  1579. #ifndef OS2
  1580. #ifdef OSK
  1581.     /* This large array is dynamic for OS-9 -- should do for others too... */
  1582.     extern char **mtchs;
  1583. #else
  1584. #ifdef UNIX
  1585.     /* OK, for UNIX too */
  1586.     extern char **mtchs;
  1587. #else
  1588. #ifdef VMS
  1589.     extern char **mtchs;
  1590. #else
  1591.     extern char *mtchs[];
  1592. #endif /* VMS */
  1593. #endif /* UNIX */
  1594. #endif /* OSK */
  1595. #endif /* OS2 */
  1596. #endif /* NOPARTIAL */
  1597.  
  1598.     if (!xhlp) xhlp = "";
  1599.     if (!xdef) xdef = "";
  1600.  
  1601.     cmfldflgs = 0;
  1602.     if (path)
  1603.       if (!*path)
  1604.     path = NULL;
  1605.     if (path) {                /* Make a copy we can poke */
  1606.     x = strlen(path);
  1607.     np = (char *) malloc(x + 1);
  1608.     if (np) {
  1609.         strcpy(np, path);
  1610.         path = sp = np;
  1611.     }
  1612.     }
  1613.     debug(F110,"cmifi2 path",path,0);
  1614.  
  1615.     ckstrncpy(cmdefault,xdef,CMDEFAULT);    /* Copy default */
  1616.     xdef = cmdefault;
  1617.  
  1618.     inword = 0;                /* Initialize counts & pointers */
  1619.     cc = 0;
  1620.     xc = 0;
  1621.     *xp = "";                /* Pointer to result string */
  1622.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  1623. #ifdef BS_DIRSEP
  1624.     dirnamflg = 1;
  1625.         x = gtword(0);            /* No, get a word */
  1626.     dirnamflg = 0;
  1627. #else
  1628.         x = gtword(0);                  /* No, get a word */
  1629. #endif /* BS_DIRSEP */
  1630.     } else {                /* If so, use default, if any. */
  1631.         if (setatm(xdef,1) < 0) {
  1632.         printf("?Default name too long\n");
  1633.         if (np) free(np);
  1634.         return(-9);
  1635.     }
  1636.     }
  1637.   i_path:
  1638.     *xp = atmbuf;                       /* Point to result. */
  1639.  
  1640.     while (1) {
  1641.         xc += cc;                       /* Count this character. */
  1642.         debug(F111,"cmifi gtword",atmbuf,xc);
  1643.     debug(F101,"cmifi switch x","",x);
  1644.         switch (x) {            /* x = gtword() return code */
  1645.       case -10:
  1646.         if (gtimer() > timelimit) {
  1647. #ifdef IKSD
  1648.                 extern int inserver;
  1649.                 if (inserver) {
  1650.                     printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  1651.                     doexit(GOOD_EXIT,0);
  1652.                 }
  1653. #endif /* IKSD */
  1654.         if (!quiet) printf("?Timed out\n");
  1655.         return(-10);
  1656.         } else {
  1657.         x = gtword(0);
  1658.         continue;
  1659.         }
  1660.       case -9:
  1661.         printf("Command or field too long\n");
  1662.       case -4:            /* EOF */
  1663.       case -2:            /* Out of space. */
  1664.       case -1:            /* Reparse needed */
  1665.         if (np) free(np);
  1666.         return(x);
  1667.       case 0:            /* SP or NL */
  1668.       case 1:
  1669.         if (xc == 0)        /* If no input... */
  1670.           *xp = xdef;        /* substitute the default */
  1671.         if (**xp == NUL) {        /* If field still empty return -3. */
  1672.         if (np) free(np);
  1673.         return(-3);
  1674.         }
  1675.         *xp = brstrip(*xp);        /* Strip braces */
  1676.         debug(F110,"cmifi brstrip",*xp,0);
  1677. #ifndef NOSPL
  1678.         if (f) {            /* If a conversion function is given */
  1679. #ifdef DOCHKVAR
  1680.         char *s = *xp;        /* See if there are any variables in */
  1681.         int x;
  1682.         while (*s) {        /* the string and if so, expand them */
  1683.             if (chkvar(s)) {
  1684. #endif /* DOCHKVAR */
  1685.             zq = atxbuf;
  1686.             atxn = CMDBL;
  1687.             if ((*f)(*xp,&zq,&atxn) < 0) {
  1688.                 if (np) free(np);
  1689.                 return(-2);
  1690.             }
  1691.             *xp = atxbuf;
  1692.             if (!atxbuf[0])
  1693.               *xp = xdef;
  1694. #ifdef DOCHKVAR
  1695.             break;
  1696.             }
  1697.             s++;
  1698.         }
  1699. #endif /* DOCHKVAR */
  1700.         }
  1701. #endif /* NOSPL */
  1702.  
  1703. #ifdef DTILDE
  1704.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  1705.         if (*dirp != '\0') {    /* in the atom buffer. */
  1706.         if (setatm(dirp,1) < 0) {
  1707.             printf("Expanded name too long\n");
  1708.             if (np) free(np);
  1709.             return(-9);
  1710.         }
  1711.         }
  1712.         *xp = atmbuf;
  1713. #endif /* DTILDE */
  1714.         debug(F110,"cmifi tilde_expand",*xp,0);
  1715.         if (!sv) {            /* Only do this once */
  1716.         sv = malloc((int)strlen(*xp)+1); /* Make a safe copy */
  1717.         if (!sv) {
  1718.             printf("?cmifi: malloc error\n");
  1719.             if (np) free(np);
  1720.             return(-9);
  1721.         }
  1722.         strcpy(sv,*xp);
  1723.         debug(F110,"cmifi sv",sv,0);
  1724.         }
  1725.  
  1726. /* This is to get around "cd /" failing because "too many directories match" */
  1727.  
  1728.         expanded = 0;        /* Didn't call zxpand */
  1729. #ifdef datageneral
  1730.         debug(F110,"cmifi isdir 1",*xp,0);
  1731.         {
  1732.         int y; char *s;
  1733.         s = *xp;
  1734.         y = strlen(s);
  1735.         if (y > 1 &&
  1736.             (s[y-1] == ':' ||
  1737.              s[y-1] == '^' ||
  1738.              s[y-1] == '=')
  1739.             )
  1740.           s[y-1] = NUL;
  1741.         }
  1742.         debug(F110,"cmifi isdir 2",*xp,0);
  1743. #endif /*  datageneral */
  1744.  
  1745. #ifdef VMS
  1746.         if (dirflg) {
  1747.         if (!strcmp(*xp,"..")) { /* For UNIXers... */
  1748.             setatm("-",0);
  1749.             *xp = atmbuf;
  1750.         } else if (!strcmp(*xp,".")) {
  1751.             setatm("[]",0);
  1752.             *xp = atmbuf;
  1753.         }
  1754.         }
  1755. #endif /* VMS */
  1756.         itsadir = isdir(*xp);    /* Is it a directory? */
  1757.         debug(F111,"cmifi itsadir",*xp,itsadir);
  1758. #ifdef VMS
  1759.         /* If they said "blah" where "blah.dir" is a directory... */
  1760.         /* change it to [.blah]. */
  1761.         if (!itsadir) {
  1762.         char tmpbuf[600];
  1763.         int flag = 0; char c, * p;
  1764.         p = *xp;
  1765.         while ((c = *p++) && !flag)
  1766.           if (ckstrchr(".[]:*?<>",c))
  1767.             flag = 1;
  1768.         debug(F111,"cmifi VMS dirname flag",*xp,flag);
  1769.         if (!flag) {
  1770.             sprintf(tmpbuf,"[.%s]",*xp);
  1771.             itsadir = isdir(tmpbuf);
  1772.             if (itsadir) {
  1773.             setatm(tmpbuf,0);
  1774.             *xp = atmbuf;
  1775.             }
  1776.             debug(F111,"cmifi VMS dirname flag itsadir",*xp,itsadir);
  1777.         }
  1778.         } else if (itsadir == 1 && *(xp[0]) == '.' && *(xp[1])) {
  1779.         char *p;
  1780.         if (p = malloc(cc + 4)) {
  1781.             sprintf(p,"[%s]",*xp);
  1782.             setatm(p,0);
  1783.             *xp = atmbuf;
  1784.             debug(F110,"cmdir .foo",*xp,0);
  1785.             free(p);
  1786.         }
  1787.         } else if (itsadir == 2 && !diractive) {
  1788.         int x;            /* [FOO]BAR.DIR instead of [FOO.BAR] */
  1789.         char *p;
  1790.         p = malloc(cc + 4);
  1791.         if (p) {
  1792.             x = cvtdir(*xp,p);    /* Convert to [FOO.BAR] */
  1793.             if (x > 0) {
  1794.             setatm(p,0);
  1795.             *xp = atmbuf;
  1796.             debug(F110,"cmdir cvtdir",*xp,0);
  1797.             }
  1798.             free(p);
  1799.         }
  1800.         }
  1801. #endif /* VMS */
  1802.  
  1803.         if (dirflg) {        /* Parsing a directory name? */
  1804.         /* Yes, does it contain wildcards? */
  1805.         if (iswild(*xp) ||
  1806.             diractive && (!strcmp(*xp,".")  || !strcmp(*xp,".."))
  1807.             ) {
  1808.             nzxopts = ZX_DIRONLY; /* Match only directory names */
  1809.             if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1810.             if (recursive) nzxopts |= ZX_RECURSE;
  1811.             y = nzxpand(*xp,nzxopts);
  1812.             nfiles = y;
  1813.             expanded = 1;
  1814.         } else {
  1815. #ifdef VMS
  1816. /*
  1817.   This is to allow (e.g.) "cd foo", where FOO.DIR;1 is in the
  1818.   current directory.
  1819. */
  1820.             debug(F111,"cmdir itsadir",*xp,itsadir);
  1821.             if (!itsadir) {
  1822.             char *s;
  1823.             int n;
  1824.             s = *xp;
  1825.             n = strlen(s);
  1826.             if (n > 0 &&
  1827. #ifdef COMMENT
  1828.                 *s != '[' && s[n-1] != ']' &&
  1829.                 *s != '<' && s[n-1] != '>' &&
  1830. #else
  1831.                 ckindex("[",s,0,0,1) == 0 &&
  1832.                 ckindex("<",s,0,0,1) == 0 &&
  1833. #endif /* COMMENT */
  1834.                 s[n-1] != ':') {
  1835.                 char * dirbuf = NULL;
  1836.                 dirbuf = (char *)malloc(n+4);
  1837.                 if (dirbuf) {
  1838.                 if (*s == '.')
  1839.                   sprintf(dirbuf,"[%s]",s);
  1840.                 else
  1841.                   sprintf(dirbuf,"[.%s]",s);
  1842.                 itsadir = isdir(dirbuf);
  1843.                 debug(F111,"cmdir dirbuf",dirbuf,itsadir);
  1844.                 if (itsadir) {
  1845.                     setatm(dirbuf,0);
  1846.                     *xp = atmbuf;
  1847.                     debug(F110,"cmdir new *xp",*xp,0);
  1848.                 }
  1849.                 free(dirbuf);
  1850.                 }
  1851.  
  1852. /* This is to allow CDPATH to work in VMS... */
  1853.  
  1854.             } else if (n > 0) {
  1855.                 char * p; int i, j, k, d;
  1856.                 char rb[2] = "]";
  1857.                 if (p = malloc(x + 8)) {
  1858.                 strcpy(p,*xp);
  1859.                 i = ckindex(".",p,-1,1,1);
  1860.                 d = ckindex(".dir",p,0,0,0);
  1861.                 j = ckindex("]",p,-1,1,1);
  1862.                 if (j == 0) {
  1863.                     j = ckindex(">",p,-1,1,1);
  1864.                     rb[0] = '>';
  1865.                 }
  1866.                 k = ckindex(":",p,-1,1,1);
  1867.                 if (i < j || i < k) i = 0;
  1868.                 if (d < j || d < k) d = 0;
  1869.                 /* Change [FOO]BAR or [FOO]BAR.DIR */
  1870.                 /* to [FOO.BAR] */
  1871.                 if (j > 0 && j < n) {
  1872.                     p[j-1] = '.';
  1873.                     if (d > 0) p[d-1] = NUL;
  1874.                     strcat(p,rb);
  1875.                     debug(F110,"cmdir xxx",p,0);
  1876.                 }
  1877.                 itsadir = isdir(p);
  1878.                 debug(F111,"cmdir p",p,itsadir);
  1879.                 if (itsadir) {
  1880.                     setatm(p,0);
  1881.                     *xp = atmbuf;
  1882.                     debug(F110,"cmdir new *xp",*xp,0);
  1883.                 }
  1884.                 free(p);
  1885.                 }
  1886.             }
  1887.             }
  1888. #endif /* VMS */
  1889.             y = (!itsadir) ? 0 : 1;
  1890.             debug(F111,"cmifi y itsadir",*xp,y);
  1891.         }
  1892.         } else {            /* Parsing a filename. */
  1893.         debug(F110,"cmifi *xp pre-zxpand",*xp,0);
  1894. #ifndef COMMENT
  1895.         nzxopts = (d == 0) ? ZX_FILONLY : 0; /* So always expand. */
  1896.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1897.         if (recursive) nzxopts |= ZX_RECURSE;
  1898.         y = nzxpand(*xp,nzxopts);
  1899. #else
  1900. /* Here we're trying to fix a problem in which a directory name is accepted */
  1901. /* as a filename, but this breaks too many other things. */
  1902.         nzxopts = 0;
  1903.         if (!d) {
  1904.             if (itsadir & !iswild(*xp)) {
  1905.             debug(F100,"cmifi dir when filonly","",0);
  1906.             printf("?Not a regular file: \"%s\"\n",*xp);
  1907.             if (sv) free(sv);
  1908.             if (np) free(np);
  1909.             return(-9);
  1910.             } else {
  1911.             nzxopts = ZX_FILONLY;
  1912.             if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1913.             if (recursive) nzxopts |= ZX_RECURSE;
  1914.             y = nzxpand(*xp,nzxopts);
  1915.             }
  1916.         }
  1917. #endif /* COMMENT */
  1918.         nfiles = y;
  1919.         debug(F111,"cmifi y nzxpand",*xp,y);
  1920.         debug(F111,"cmifi y atmbuf",atmbuf,itsadir);
  1921.         expanded = 1;
  1922.         }
  1923.         /* domydir() calls zxrewind() so we MUST call nzxpand() here */
  1924.         if (!expanded && diractive) {
  1925.         debug(F110,"cmifi diractive catch-all zxpand",*xp,0);
  1926.         nzxopts = (d == 0) ? ZX_FILONLY : (dirflg ? ZX_DIRONLY : 0);
  1927.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1928.         if (recursive) nzxopts |= ZX_RECURSE;
  1929.         y = nzxpand(*xp,nzxopts);
  1930.         nfiles = y;
  1931.         expanded = 1;
  1932.         }
  1933.         *wild = (iswild(sv) || (y > 1)) && (itsadir == 0);
  1934.  
  1935. #ifdef RECURSIVE
  1936.         if (!*wild) *wild = recursive;
  1937. #endif /* RECURSIVE */
  1938.  
  1939.         debug(F111,"cmifi sv wild",sv,*wild);
  1940.         if (dirflg && *wild && !diractive) {
  1941.         printf("?Wildcard matches more than one directory\n");
  1942.         if (sv) free(sv);
  1943.         if (np) free(np);
  1944.         return(-9);
  1945.         }
  1946.         if (itsadir && d && !dirflg) { /* It's a directory and not wild */
  1947.         if (sv) free(sv);    /* and it's ok to parse directories */
  1948.         if (np) free(np);
  1949.         return(x);
  1950.         }
  1951.         if (y == 0) {
  1952.         if (path && !isabsolute(sv)) {
  1953.             char * ptr = path;
  1954.             char c;
  1955.             while (1) {
  1956.             c = *ptr;
  1957.             if (c == PATHSEP || c == NUL) {
  1958.                 if (!*path) {
  1959.                 path = NULL;
  1960.                 break;
  1961.                 }
  1962.                 *ptr = NUL;
  1963. #ifdef UNIX
  1964. /* By definition of CDPATH, an empty member denotes the current directory */
  1965.                 if (!*path)
  1966.                   strcpy(atmbuf,".");
  1967.                 else
  1968. #endif /* UNIX */
  1969.                   strncpy(atmbuf,path,ATMBL);
  1970. #ifdef VMS
  1971.                 atmbuf[ATMBL] = NUL;
  1972. /* If we have a logical name, evaluate it recursively */
  1973.                 if (*(ptr-1) == ':') { /* Logical name ends in : */
  1974.                 char *p; int n;
  1975.                 while (((n = strlen(atmbuf))  > 0) &&
  1976.                        atmbuf[n-1] == ':') {
  1977.                     atmbuf[n-1] = NUL;
  1978.                     for (p = atmbuf; *p; p++)
  1979.                       if (islower(*p)) *p = toupper(*p);
  1980.                     debug(F111,"cmdir CDPATH LN 1",atmbuf,n);
  1981.                     p = getenv(atmbuf);
  1982.                     debug(F110,"cmdir CDPATH LN 2",p,0);
  1983.                     if (!p)
  1984.                       break;
  1985.                     strncpy(atmbuf,p,ATMBL);
  1986.                     atmbuf[ATMBL] = NUL;
  1987.                 }
  1988.                 }
  1989. #else
  1990. #ifdef OS2
  1991.                 if (*(ptr-1) != '\\' && *(ptr-1) != '/')
  1992.                   strcat(atmbuf,"\\");
  1993. #else
  1994. #ifdef UNIX
  1995.                 if (*(ptr-1) != '/')
  1996.                   strcat(atmbuf,"/");
  1997. #else
  1998. #ifdef datageneral
  1999.                 if (*(ptr-1) != ':')
  2000.                   strcat(atmbuf,":");
  2001. #endif /* datageneral */
  2002. #endif /* UNIX */
  2003. #endif /* OS2 */
  2004. #endif /* VMS */
  2005.                 strcat(atmbuf,sv);
  2006.                 debug(F110,"cmifip add path",atmbuf,0);
  2007.                 if (c == PATHSEP) ptr++;
  2008.                 path = ptr;
  2009.                 break;
  2010.             }
  2011.             ptr++;
  2012.             }
  2013.             x = 1;
  2014.             inword = 0;
  2015.             cc = 0;
  2016.             xc = (int) strlen(atmbuf);
  2017.             *xp = "";
  2018.             goto i_path;
  2019.         }
  2020.         if (d) {
  2021.             if (sv) free(sv);
  2022.             if (np) free(np);
  2023.             return(-2);
  2024.         } else {
  2025.             if (!nomsg)
  2026.               printf("?No %s match - %s\n",
  2027.                  dirflg ? "directories" : "files", sv);
  2028.             if (sv) free(sv);
  2029.             if (np) free(np);
  2030.             return(-9);
  2031.         }
  2032.         } else if (y < 0) {
  2033.         printf("?Too many %s match - %s\n",
  2034.                dirflg ? "directories" : "files", sv);
  2035.         if (sv) free(sv);
  2036.         if (np) free(np);
  2037.         return(-9);
  2038.         } else if (*wild || y > 1) {
  2039.         if (sv) free(sv);
  2040.         if (np) free(np);
  2041.         return(x);
  2042.         }
  2043.  
  2044.         /* If not wild, see if it exists and is readable. */
  2045.  
  2046.         debug(F111,"cmifi sv not wild",sv,*wild);
  2047.  
  2048.         if (expanded)
  2049.           znext(*xp);        /* Get first (only?) matching file */
  2050.         if (dirflg)            /* Maybe wild and expanded */
  2051.           itsadir = isdir(*xp);    /* so do this again. */
  2052.         y = dirflg ? itsadir : zchki(*xp); /* Now check accessibility */
  2053.         if (expanded) {
  2054. #ifdef ZXREWIND
  2055.         nfiles = zxrewind();    /* Rewind so next znext() gets 1st */
  2056. #else
  2057.  
  2058.         nzxopts = dirflg ? ZX_DIRONLY : 0;
  2059.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  2060.         if (recursive) nzxopts |= ZX_RECURSE;
  2061.         nfiles = nzxpand(*xp,nzxopts);
  2062. #endif /* ZXREWIND */
  2063.         }
  2064.         debug(F111,"cmifi nfiles",*xp,nfiles);
  2065.         free(sv);            /* done with this */
  2066.         sv = NULL;
  2067.         if (dirflg && y == 0) {
  2068.         printf("?Not a directory - %s\n",*xp);
  2069. #ifdef CKCHANNELIO
  2070.         z_error = FX_ACC;
  2071. #endif /* CKCHANNELIO */
  2072.         return(-9);
  2073.         } else if (y == -3) {
  2074.         if (!xcmfdb) {
  2075.             if (diractive)
  2076.               /* Don't show filename if we're not allowed to see it */
  2077.               printf("?Read permission denied\n");
  2078.             else
  2079.               printf("?Read permission denied - %s\n",*xp);
  2080.         }
  2081.         if (np) free(np);
  2082. #ifdef CKCHANNELIO
  2083.         z_error = FX_ACC;
  2084. #endif /* CKCHANNELIO */
  2085.         return(xcmfdb ? -6 : -9);
  2086.         } else if (y == -2) {
  2087.         if (!recursive) {
  2088.             if (np) free(np);
  2089.             if (d) return(0);
  2090.             if (!xcmfdb)
  2091.               printf("?File not readable - %s\n",*xp);
  2092. #ifdef CKCHANNELIO
  2093.             z_error = FX_ACC;
  2094. #endif /* CKCHANNELIO */
  2095.             return(xcmfdb ? -6 : -9);
  2096.         }
  2097.         } else if (y < 0) {
  2098.         if (np) free(np);
  2099.         if (!nomsg && !xcmfdb)
  2100.           printf("?File not found - %s\n",*xp);
  2101. #ifdef CKCHANNELIO
  2102.         z_error = FX_FNF;
  2103. #endif /* CKCHANNELIO */
  2104.         return(xcmfdb ? -6 : -9);
  2105.         }
  2106.         if (np) free(np);
  2107.         return(x);
  2108.  
  2109. #ifndef MAC
  2110.       case 2:            /* ESC */
  2111.         debug(F101,"cmifi esc, xc","",xc);
  2112.         if (xc == 0) {
  2113.         if (*xdef) {
  2114.             printf("%s ",xdef); /* If at beginning of field */
  2115. #ifdef GEMDOS
  2116.             fflush(stdout);
  2117. #endif /* GEMDOS */
  2118.             inword = cmflgs = 0;
  2119.             addbuf(xdef);    /* Supply default. */
  2120.             if (setatm(xdef,0) < 0) {
  2121.             printf("Default name too long\n");
  2122.             if (np) free(np);
  2123.             return(-9);
  2124.             }
  2125.         } else {        /* No default */
  2126.             bleep(BP_WARN);
  2127.         }
  2128.         break;
  2129.         }
  2130. #ifndef NOSPL
  2131.         if (f) {            /* If a conversion function is given */
  2132. #ifdef DOCHKVAR
  2133.         char *s = *xp;        /* See if there are any variables in */
  2134.         while (*s) {        /* the string and if so, expand it.  */
  2135.             if (chkvar(s)) {
  2136. #endif /* DOCHKVAR */
  2137.             zq = atxbuf;
  2138.             atxn = CMDBL;
  2139.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) {
  2140.                 if (np) free(np);
  2141.                 return(-2);
  2142.             }
  2143. #ifdef DOCHKVAR
  2144.             /* reduce cc by number of \\ consumed by conversion */
  2145.             /* function (needed for OS/2, where \ is path separator) */
  2146.             cc -= (strlen(*xp) - strlen(atxbuf));
  2147. #endif /* DOCHKVAR */
  2148.             *xp = atxbuf;
  2149.             if (!atxbuf[0]) { /* Result empty, use default */
  2150.                 *xp = xdef;
  2151.                 cc = strlen(xdef);
  2152.             }
  2153. #ifdef DOCHKVAR
  2154.             break;
  2155.             }
  2156.             s++;
  2157.         }
  2158. #endif /* DOCHKVAR */
  2159.         }
  2160. #endif /* NOSPL */
  2161. #ifdef DTILDE
  2162.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  2163.         if (*dirp != '\0') {    /* in the atom buffer. */
  2164.         if (setatm(dirp,0) < 0) {
  2165.             printf("Expanded name too long\n");
  2166.             if (np) free(np);
  2167.             return(-9);
  2168.         }
  2169.         }
  2170.         *xp = atmbuf;
  2171. #endif /* DTILDE */
  2172.         sp = *xp + cc;
  2173. #ifdef UNIXOROSK
  2174.         if (!strcmp(atmbuf,"..")) {
  2175.         printf(" ");
  2176.         strcat(cmdbuf," ");
  2177.         cc++;
  2178.         bp++;
  2179.         *wild = 0;
  2180.         *xp = atmbuf;
  2181.         break;
  2182.         } else if (!strcmp(atmbuf,".")) {
  2183.         bleep(BP_WARN);
  2184.         if (np) free(np);
  2185.         return(-1);
  2186.         } else {
  2187.         /* This patches a glitch when user types "./foo<ESC>" */
  2188.         /* in which the next two chars are omitted from the */
  2189.         /* expansion.  There should be a better fix, however, */
  2190.         /* since there is no problem with "../foo<ESC>". */
  2191.         char *p = *xp;
  2192.         if (*p == '.' && *(p+1) == '/')
  2193.           cc -= 2;
  2194.         }
  2195. #endif /* UNIXOROSK */
  2196.  
  2197. #ifdef datageneral
  2198.         *sp++ = '+';        /* Data General AOS wildcard */
  2199. #else
  2200.         *sp++ = '*';        /* Others */
  2201. #endif /* datageneral */
  2202.         *sp-- = '\0';
  2203. #ifdef GEMDOS
  2204.         if (!strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  2205.           strcat(*xp, ".*");    /* abc -> abc*.* */
  2206. #endif /* GEMDOS */
  2207.         /* Add wildcard and expand list. */
  2208. #ifdef COMMENT
  2209.         /* This kills partial completion when ESC given in path segment */
  2210.         nzxopts = dirflg ? ZX_DIRONLY : (d ? 0 : ZX_FILONLY);
  2211. #else
  2212.         nzxopts = 0;
  2213. #endif /* COMMENT */
  2214.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  2215.         if (recursive) nzxopts |= ZX_RECURSE;
  2216.         y = nzxpand(*xp,nzxopts);
  2217.         nfiles = y;
  2218.         debug(F111,"cmifi nzxpand",*xp,y);
  2219.         if (y > 0) {
  2220. #ifdef OS2
  2221.                 znext(filbuf);        /* Get first */
  2222. #ifdef ZXREWIND
  2223.         zxrewind();        /* Must "rewind" */
  2224. #else
  2225.         nzxpand(*xp,nxzopts);
  2226. #endif /* ZXREWIND */
  2227. #else  /* Not OS2 */
  2228.                 ckstrncpy(filbuf,mtchs[0],CKMAXPATH);
  2229. #endif /* OS2 */
  2230.         } else
  2231.           *filbuf = '\0';
  2232.         filbuf[CKMAXPATH] = NUL;
  2233.         debug(F111,"cmifi filbuf",filbuf,y);
  2234.         *sp = '\0';            /* Remove wildcard. */
  2235.         *wild = (y > 1);
  2236.         if (y == 0) {
  2237.         if (!nomsg) {
  2238.             printf("?No %s match - %s\n",
  2239.                dirflg ? "directories" : "files", atmbuf);
  2240.             if (np) free(np);
  2241.             return(-9);
  2242.         } else {
  2243.             bleep(BP_WARN);
  2244.             if (np) free(np);
  2245.             return(-1);
  2246.         }
  2247.         } else if (y < 0) {
  2248.         printf("?Too many %s match - %s\n",
  2249.                dirflg ? "directories" : "files", atmbuf);
  2250.         if (np) free(np);
  2251.         return(-9);
  2252.         } else if (y > 1) {     /* Not unique. */
  2253. #ifndef NOPARTIAL
  2254. /* Partial filename completion */
  2255.         int j, k; char c;
  2256.         k = 0;
  2257.         debug(F111,"cmifi partial",filbuf,cc);
  2258. #ifdef OS2
  2259.         {
  2260.             int cur = 0,
  2261.             len = 0,
  2262.             len2 = 0,
  2263.             min = strlen(filbuf);
  2264.             char localfn[CKMAXPATH+1];
  2265.  
  2266.             len = min;
  2267.             for (j = 1; j <= y; j++) {
  2268.             znext(localfn);
  2269.             if (dirflg && !isdir(localfn))
  2270.               continue;
  2271.             len2 = strlen(localfn);
  2272.             for (cur = cc;
  2273.                  cur < len && cur < len2 && cur <= min;
  2274.                  cur++
  2275.                  ) {
  2276.                             /* OS/2 or Windows, case doesn't matter */
  2277.                 if (tolower(filbuf[cur]) != tolower(localfn[cur]))
  2278.                   break;
  2279.             }
  2280.             if (cur < min)
  2281.               min = cur;
  2282.             }
  2283.             filbuf[min] = NUL;
  2284.             if (min > cc)
  2285.               k++;
  2286.         }
  2287. #else /* OS2 */
  2288.         for (i = cc; (c = filbuf[i]); i++) {
  2289.             for (j = 1; j < y; j++)
  2290.               if (mtchs[j][i] != c) break;
  2291.             if (j == y) k++;
  2292.             else filbuf[i] = filbuf[i+1] = NUL;
  2293.         }
  2294. #endif /* OS2 */
  2295.         debug(F111,"cmifi partial k",filbuf,k);
  2296.         if (k > 0) {        /* Got more characters */
  2297.             sp = filbuf + cc;    /* Point to new ones */
  2298. #ifdef VMS
  2299.             for (i = 0; i < cc; i++) {
  2300.             cmdchardel();    /* Back up over old partial spec */
  2301.             bp--;
  2302.             }
  2303.             sp = filbuf;    /* Point to new word start */
  2304.             debug(F100,"cmifi vms erase ok","",0);
  2305. #endif /* VMS */
  2306.             cc = k;        /* How many new ones we just got */
  2307.             printf("%s",sp);    /* Print them */
  2308.             while (*bp++ = *sp++) ;    /* Copy to command buffer */
  2309.             bp--;                    /* Back up over NUL */
  2310.             debug(F110,"cmifi partial cmdbuf",cmdbuf,0);
  2311.             if (setatm(filbuf,0) < 0) {
  2312.             printf("?Partial name too long\n");
  2313.             if (np) free(np);
  2314.             return(-9);
  2315.             }
  2316.             debug(F111,"cmifi partial atmbuf",atmbuf,cc);
  2317.             *xp = atmbuf;
  2318.         }
  2319. #endif /* NOPARTIAL */
  2320.         bleep(BP_WARN);
  2321.         } else {            /* Unique, complete it.  */
  2322. #ifndef VMS
  2323. #ifdef CK_TMPDIR
  2324.         /* isdir() function required for this! */
  2325.         debug(F111,"cmifi unique",filbuf,cc);
  2326.         if (isdir(filbuf) && !dirflg) {
  2327.             int len;
  2328.             len = strlen(filbuf);
  2329.             if (len > 0 && len < ATMBL - 1) {
  2330.             if (filbuf[len-1] != dirsep) {
  2331.                 filbuf[len] = dirsep;
  2332.                 filbuf[len+1] = NUL;
  2333.             }
  2334.             }
  2335.             sp = filbuf + cc;
  2336.             bleep(BP_WARN);
  2337.             printf("%s",sp);
  2338.             cc++;
  2339.             while (*bp++ = *sp++) ;
  2340.             bp--;
  2341.             if (setatm(filbuf,0) < 0) {
  2342.             printf("?Directory name too long\n");
  2343.             if (np) free(np);
  2344.             return(-9);
  2345.             }
  2346.             debug(F111,"cmifi directory atmbuf",atmbuf,cc);
  2347.             *xp = atmbuf;
  2348.         } else {        /* Not a directory or dirflg */
  2349. #endif /* CK_TMPDIR */
  2350. #endif /* VMS */
  2351. #ifndef VMS                /* VMS dir names are special */
  2352. #ifndef datageneral            /* VS dirnames must not end in ":" */
  2353.             if (dirflg) {
  2354.             int len;
  2355.             len = strlen(filbuf);
  2356.             if (len > 0 && len < ATMBL - 1) {
  2357.                 if (filbuf[len-1] != dirsep) {
  2358.                 filbuf[len] = dirsep;
  2359.                 filbuf[len+1] = NUL;
  2360.                 }
  2361.             }
  2362.             }
  2363. #endif /* datageneral */
  2364. #endif /* VMS */
  2365.             sp = filbuf + cc;    /* Point past what user typed. */
  2366. #ifdef VMS
  2367.             debug(F111,"cmifi VMS erasing",filbuf,cc);
  2368.             for (i = 0; i < cc; i++) {
  2369.             cmdchardel();    /* Back up over old partial spec */
  2370.             bp--;
  2371.             }
  2372.             sp = filbuf;    /* Point to new word start */
  2373.             debug(F111,"cmifi after VMS erase sp=",sp,cc);
  2374. #endif /* VMS */
  2375.             /* Complete the name. */
  2376. #ifdef COMMENT
  2377.             printf("%s%s",dirflg ? "" : " ",sp);
  2378. #else
  2379.             printf("%s ",sp); /* Complete the name. */
  2380. #endif /* COMMENT */
  2381. #ifdef GEMDOS
  2382.             fflush(stdout);
  2383. #endif /* GEMDOS */
  2384.             addbuf(sp);        /* Add the characters to cmdbuf. */
  2385.             if (setatm(filbuf,0) < 0) { /* And to atmbuf. */
  2386.             printf("?Completed name too long\n");
  2387.             if (np) free(np);
  2388.             return(-9);
  2389.             }
  2390.             if (dirflg && !isdir(filbuf)) {
  2391.             printf("?Not a directory - %s\n", filbuf);
  2392.             if (np) free(np);
  2393.             return(-9);
  2394.             }
  2395.             inword = cmflgs = 0;
  2396.             *xp = atmbuf;    /* Return pointer to atmbuf. */
  2397.             if (np) free(np);
  2398.             return(0);
  2399. #ifndef VMS
  2400. #ifdef CK_TMPDIR
  2401.         }
  2402. #endif /* CK_TMPDIR */
  2403. #endif /* VMS */
  2404.         }
  2405.         break;
  2406.  
  2407.       case 3:            /* Question mark - file menu wanted */
  2408.         if (*xhlp == NUL)
  2409.           printf(dirflg ? " Directory name" : " Input file specification");
  2410.         else
  2411.           printf(" %s",xhlp);
  2412. #ifdef GEMDOS
  2413.         fflush(stdout);
  2414. #endif /* GEMDOS */
  2415. #ifdef OLDHELP
  2416.         if (xc > 0) {
  2417. #endif /* OLDHELP */
  2418. #ifndef NOSPL
  2419.         if (f) {        /* If a conversion function is given */
  2420. #ifdef DOCHKVAR
  2421.             char *s = *xp;    /* See if there are any variables in */
  2422.             while (*s) {    /* the string and if so, expand them */
  2423.             if (chkvar(s)) {
  2424. #endif /* DOCHKVAR */
  2425.                 zq = atxbuf;
  2426.                 atxn = CMDBL;
  2427.                 if ((x = (*f)(*xp,&zq,&atxn)) < 0) {
  2428.                 if (np) free(np);
  2429.                 return(-2);
  2430.                 }
  2431. #ifdef DOCHKVAR
  2432.                     /* reduce cc by number of \\ consumed by conversion */
  2433.                     /* function (needed for OS/2, where \ is path separator) */
  2434.                 cc -= (strlen(*xp) - strlen(atxbuf));
  2435. #endif /* DOCHKVAR */
  2436.                 *xp = atxbuf;
  2437. #ifdef DOCHKVAR
  2438.                 break;
  2439.             }
  2440.             s++;
  2441.             }
  2442. #endif /* DOCHKVAR */
  2443.         }
  2444. #endif /* NOSPL */
  2445. #ifdef DTILDE
  2446.         dirp = tilde_expand(*xp); /* Expand tilde, if any */
  2447.         if (*dirp != '\0') {
  2448.             if (setatm(dirp,0) < 0) {
  2449.             printf("?Expanded name too long\n");
  2450.             if (np) free(np);
  2451.             return(-9);
  2452.             }
  2453.         }
  2454.         *xp = atmbuf;
  2455. #endif /* DTILDE */
  2456.         debug(F111,"cmifi ? *xp, cc",*xp,cc);
  2457.         sp = *xp + cc;        /* Insert "*" at end */
  2458. #ifdef datageneral
  2459.         *sp++ = '+';        /* Insert +, the DG wild card */
  2460. #else
  2461.         *sp++ = '*';
  2462. #endif /* datageneral */
  2463.         *sp-- = '\0';
  2464. #ifdef GEMDOS
  2465.         if (! strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  2466.           strcat(*xp, ".*");    /* abc -> abc*.* */
  2467. #endif /* GEMDOS */
  2468.         debug(F110,"cmifi ? wild",*xp,0);
  2469.  
  2470. #ifdef COMMENT
  2471.         /* This kills file lists when we're still the path part */
  2472.         nzxopts = dirflg ? ZX_DIRONLY : (d ? 0 : ZX_FILONLY);
  2473. #else
  2474. #ifdef COMMENT
  2475.         /* But this makes "cd ?" list regular files */
  2476.         nzxopts = 0;
  2477. #else
  2478.         nzxopts = dirflg ? ZX_DIRONLY : 0;
  2479. #endif /* COMMENT */
  2480. #endif /* COMMENT */
  2481.         debug(F101,"cmifi matchdot","",matchdot);
  2482.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  2483.         if (recursive) nzxopts |= ZX_RECURSE;
  2484.         y = nzxpand(*xp,nzxopts);
  2485.         nfiles = y;
  2486.         *sp = '\0';
  2487.         if (y == 0) {
  2488.             if (nomsg) {
  2489.             printf(": %s\n",atmbuf);
  2490.             printf("%s%s",cmprom,cmdbuf);
  2491.             fflush(stdout);
  2492.             if (np) free(np);
  2493.             return(-1);
  2494.             } else {
  2495.             printf("?No %s match - %s\n",
  2496.                    dirflg ? "directories" : "files", atmbuf);
  2497.             if (np) free(np);
  2498.             return(-9);
  2499.             }
  2500.         } else if (y < 0) {
  2501.             printf("?Too many %s match - %s\n",
  2502.                dirflg ? "directories" : "files", atmbuf);
  2503.             if (np) free(np);
  2504.             return(-9);
  2505.         } else {
  2506.             printf(", one of the following:\n");
  2507. #ifdef OLDHELP
  2508.             clrhlp();
  2509.             for (i = 0; i < y; i++) {
  2510.             znext(filbuf);
  2511.             if (!dirflg || isdir(filbuf)) {
  2512. #ifdef VMS
  2513.                 printf(" %s\n",filbuf); /* VMS names can be long */
  2514. #else
  2515.                 addhlp(filbuf);
  2516. #endif /* VMS */
  2517.             }
  2518.             }
  2519.             dmphlp();
  2520. #else  /* New way... */
  2521.             if (filhelp(y,"","",1,dirflg) < 0) {
  2522.             if (np) free(np);
  2523.             return(-9);
  2524.             }
  2525. #endif /* OLDHELP */
  2526.         }
  2527. #ifdef OLDHELP
  2528.         } else
  2529.           printf("\n");
  2530. #endif /* OLDHELP */
  2531.         printf("%s%s",cmprom,cmdbuf);
  2532.         fflush(stdout);
  2533.         break;
  2534. #endif /* MAC */
  2535.         }
  2536. #ifdef BS_DIRSEP
  2537.         dirnamflg = 1;
  2538.         x = gtword(0);                  /* No, get a word */
  2539.     dirnamflg = 0;
  2540. #else
  2541.         x = gtword(0);                  /* No, get a word */
  2542. #endif /* BS_DIRSEP */
  2543.     *xp = atmbuf;
  2544.     }
  2545. }
  2546.  
  2547. /*  C M F L D  --  Parse an arbitrary field  */
  2548. /*
  2549.  Returns
  2550.    -3 if no input present when required,
  2551.    -2 if field too big for buffer,
  2552.    -1 if reparse needed,
  2553.     0 otherwise, xp pointing to string result.
  2554. */
  2555. int
  2556. cmfld(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  2557.     int x, xc;
  2558.     char *zq;
  2559.  
  2560.     inword = 0;                /* Initialize counts & pointers */
  2561.     cc = 0;
  2562.     xc = 0;
  2563.     *xp = "";
  2564.  
  2565.     debug(F110,"cmfld xdef 1",xdef,0);
  2566.  
  2567.     if (!xhlp) xhlp = "";
  2568.     if (!xdef) xdef = "";
  2569.     ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  2570.     xdef = cmdefault;
  2571.  
  2572.     debug(F111,"cmfld xdef 2",xdef,cmflgs);
  2573.  
  2574.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  2575.         x = gtword(0);                  /* No, get a word */
  2576.     } else {
  2577.     if (setatm(xdef,0) < 0) {    /* If so, use default, if any. */
  2578.         printf("?Default too long\n");
  2579.         return(-9);
  2580.     }
  2581.     }
  2582.     *xp = atmbuf;                       /* Point to result. */
  2583.     debug(F111,"cmfld atmbuf 1",atmbuf,cmflgs);
  2584.  
  2585.     while (1) {
  2586.         xc += cc;                       /* Count the characters. */
  2587.         debug(F111,"cmfld: gtword",atmbuf,xc);
  2588.         debug(F101,"cmfld x","",x);
  2589.         switch (x) {
  2590.       case -9:
  2591.         printf("Command or field too long\n");
  2592.       case -4:            /* EOF */
  2593.       case -3:            /* Empty. */
  2594.       case -2:            /* Out of space. */
  2595.       case -1:            /* Reparse needed */
  2596.         return(x);
  2597.       case 0:            /* SP or NL */
  2598.       case 1:
  2599.         debug(F111,"cmfld 1",atmbuf,xc);
  2600.         if (xc == 0) {        /* If no input, return default. */
  2601.         if (setatm(xdef,0) < 0) {
  2602.             printf("?Default too long\n");
  2603.             return(-9);
  2604.         }
  2605.         }
  2606.         *xp = atmbuf;        /* Point to what we got. */
  2607.         debug(F111,"cmfld 2",atmbuf,(f) ? 1 : 0);
  2608.         if (f) {            /* If a conversion function is given */
  2609.         zq = atxbuf;        /* employ it now. */
  2610.         atxn = CMDBL;
  2611.         debug(F111,"cmfld zzstring",atxbuf,x);
  2612.         if ((*f)(*xp,&zq,&atxn) < 0)
  2613.           return(-2);
  2614.         if (setatm(atxbuf,1) < 0) { /* Replace by new value */
  2615.             printf("Value too long\n");
  2616.             return(-9);
  2617.         }
  2618.         *xp = atmbuf;
  2619.         }
  2620.         debug(F111,"cmfld 3",atmbuf,xc);
  2621.         if (**xp == NUL) {        /* If variable evaluates to null */
  2622.         if (setatm(xdef,0) < 0) {
  2623.             printf("?Default too long\n");
  2624.             return(-9);
  2625.         }
  2626.         if (**xp == NUL) x = -3; /* If still empty, return -3. */
  2627.         }
  2628.         debug(F111,"cmfld returns",*xp,x);
  2629.         return(x);
  2630.       case 2:            /* ESC */
  2631.         if (xc == 0 && *xdef) {
  2632.         printf("%s ",xdef); /* If at beginning of field, */
  2633. #ifdef GEMDOS
  2634.         fflush(stdout);
  2635. #endif /* GEMDOS */
  2636.         addbuf(xdef);        /* Supply default. */
  2637.         inword = cmflgs = 0;
  2638.         if (setatm(xdef,0) < 0) {
  2639.             printf("?Default too long\n");
  2640.             return(-9);
  2641.         } else            /* Return as if whole field */
  2642.           return(0);        /* typed, followed by space. */
  2643.         } else {
  2644.         bleep(BP_WARN);
  2645.         }
  2646.         break;
  2647.       case 3:            /* Question mark */
  2648.         if (*xhlp == NUL)
  2649.           printf(" Please complete this field");
  2650.         else
  2651.           printf(" %s",xhlp);
  2652.         printf("\n%s%s",cmprom,cmdbuf);
  2653.         fflush(stdout);
  2654.         break;
  2655.         }
  2656.     x = gtword(0);
  2657.     /* *xp = atmbuf; */
  2658.     }
  2659. }
  2660.  
  2661.  
  2662. /*  C M T X T  --  Get a text string, including confirmation  */
  2663.  
  2664. /*
  2665.   Print help message 'xhlp' if ? typed, supply default 'xdef' if null
  2666.   string typed.  Returns:
  2667.  
  2668.    -1 if reparse needed or buffer overflows.
  2669.     1 otherwise.
  2670.  
  2671.   with cmflgs set to return code, and xp pointing to result string.
  2672. */
  2673. int
  2674. cmtxt(xhlp,xdef,xp,f) char *xhlp; char *xdef; char **xp; xx_strp f; {
  2675.  
  2676.     int x, i;
  2677.     char *xx, *zq;
  2678.     static int xc;
  2679.  
  2680.     if (!xhlp) xhlp = "";
  2681.     if (!xdef) xdef = "";
  2682.  
  2683.     cmfldflgs = 0;
  2684.     ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  2685.     xdef = cmdefault;
  2686.  
  2687.     debug(F101,"cmtxt, cmflgs","",cmflgs);
  2688.     inword = 0;                /* Start atmbuf counter off at 0 */
  2689.     cc = 0;
  2690.     if (cmflgs == -1) {                 /* If reparsing, */
  2691.     *xp = pp;
  2692.         xc = (int)strlen(*xp);        /* get back the total text length, */
  2693.     bp = *xp;            /* and back up the pointers. */
  2694.     np = *xp;
  2695.     pp = *xp;
  2696.     } else {                            /* otherwise, */
  2697.     debug(F100,"cmtxt: fresh start","",0);
  2698.         *xp = "";                       /* start fresh. */
  2699.         xc = 0;
  2700.     }
  2701.     *atmbuf = NUL;                      /* And empty the atom buffer. */
  2702.     rtimer();                /* Reset timer */
  2703.     if ((x = cmflgs) != 1) {
  2704.     int done = 0;
  2705.     while (!done) {
  2706.         x = gtword(0);        /* Get first word. */
  2707.         *xp = pp;            /* Save pointer to it. */
  2708.         debug(F111,"cmtxt:",*xp,cc);
  2709.         if (x == -10) {
  2710.         if (gtimer() > timelimit) {
  2711.             if (!quiet) printf("?Timed out\n");
  2712.             return(x);
  2713.         }
  2714.         } else
  2715.           done = 1;
  2716.     }
  2717.     }
  2718.     while (1) {                /* Loop for each word in text. */
  2719.         xc += cc;                       /* Char count for all words. */
  2720.         debug(F111,"cmtxt gtword",atmbuf,xc);
  2721.         debug(F101,"cmtxt x","",x);
  2722.         switch (x) {
  2723.       case -10:
  2724.         if (gtimer() > timelimit) {
  2725. #ifdef IKSD
  2726.                 extern int inserver;
  2727.                 if (inserver) {
  2728.                     printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  2729.                     doexit(GOOD_EXIT,0);
  2730.                 }
  2731. #endif /* IKSD */
  2732.         if (!quiet) printf("?Timed out\n");
  2733.         return(-10);
  2734.         } else {
  2735.         x = gtword(0);
  2736.         continue;
  2737.         }
  2738.       case -9:            /* Buffer overflow */
  2739.         printf("Command or field too long\n");
  2740.       case -4:            /* EOF */
  2741. #ifdef MAC
  2742.       case -3:            /* Quit/Timeout */
  2743. #endif /* MAC */
  2744.       case -2:            /* Overflow */
  2745.       case -1:            /* Deletion */
  2746.         return(x);
  2747.       case 0:            /* Space */
  2748.         xc++;            /* Just count it */
  2749.         break;
  2750.       case 1:            /* CR or LF */
  2751.         if (xc == 0) *xp = xdef;
  2752.         if (f) {            /* If a conversion function is given */
  2753.         zq = atxbuf;        /* Point to the expansion buffer */
  2754.         atxn = CMDBL;        /* specify its length */
  2755.         debug(F110,"cmtxt calling (*f)",*xp,0);
  2756.         if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  2757.         cc = (int)strlen(atxbuf);
  2758.         if (cc < 1) {
  2759.             *xp = xdef;
  2760.             cc = strlen(xdef);
  2761.         } else {
  2762.             *xp = atxbuf;    /* and return pointer to it. */
  2763.         }
  2764.         debug(F111,"cmtxt (*f) returns",*xp,cc);
  2765.         }
  2766.         xx = *xp;
  2767.         for (i = (int)strlen(xx) - 1; i > 0; i--)
  2768.           if (xx[i] != SP)        /* Trim trailing blanks */
  2769.         break;
  2770.           else
  2771.         xx[i] = NUL;
  2772. #ifdef CK_RECALL
  2773.         addcmd(cmdbuf);
  2774. #endif /* CK_RECALL */
  2775.         return(x);
  2776.       case 2:            /* ESC */
  2777.         if (xc == 0) {        /* Nothing typed yet */
  2778.         if (*xdef) {        /* Have a default for this field? */
  2779.             printf("%s ",xdef);    /* Yes, supply it */
  2780.             inword = cmflgs = 0;
  2781. #ifdef GEMDOS
  2782.             fflush(stdout);
  2783. #endif /* GEMDOS */
  2784.             cc = addbuf(xdef);
  2785.         } else bleep(BP_WARN);    /* No default */
  2786.         } else {            /* Already in field */
  2787.         int x; char *p;
  2788.         x = strlen(atmbuf);
  2789.         if (ckstrcmp(atmbuf,xdef,x,0)) /* Matches default? */
  2790.           bleep(BP_WARN);    /* No */
  2791.         else {            /* Yes */
  2792.             p = xdef + x;
  2793.             printf("%s ", p);
  2794. #ifdef GEMDOS
  2795.             fflush(stdout);
  2796. #endif /* GEMDOS */
  2797.             addbuf(p);
  2798.             inword = cmflgs = 0;
  2799.             debug(F110,"cmtxt: addbuf",cmdbuf,0);
  2800.         }
  2801.         }
  2802.         break;
  2803.       case 3:            /* Question Mark */
  2804.         if (*xhlp == NUL)
  2805.           printf(" Text string");
  2806.         else
  2807.           printf(" %s",xhlp);
  2808.         printf("\n%s%s",cmprom,cmdbuf);
  2809.         fflush(stdout);
  2810.         break;
  2811.       default:
  2812.         printf("?Unexpected return code from gtword() - %d\n",x);
  2813.         return(-2);
  2814.         }
  2815.         x = gtword(0);
  2816.     }
  2817. }
  2818.  
  2819. /*  C M K E Y  --  Parse a keyword  */
  2820.  
  2821. /*
  2822.  Call with:
  2823.    table    --  keyword table, in 'struct keytab' format;
  2824.    n        --  number of entries in table;
  2825.    xhlp     --  pointer to help string;
  2826.    xdef     --  pointer to default keyword;
  2827.    f        --  processing function (e.g. to evaluate variables)
  2828.    pmsg     --  0 = don't print error messages
  2829.                 1 = print error messages
  2830.                 2 = include CM_HLP keywords even if invisible
  2831.                 3 = 1+2
  2832.                 4 = parse a switch (keyword possibly ending in : or =)
  2833.  Returns:
  2834.    -3       --  no input supplied and no default available
  2835.    -2       --  input doesn't uniquely match a keyword in the table
  2836.    -1       --  user deleted too much, command reparse required
  2837.     n >= 0  --  value associated with keyword
  2838. */
  2839. int
  2840. cmkey(table,n,xhlp,xdef,f)
  2841. /* cmkey */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  2842.     return(cmkey2(table,n,xhlp,xdef,"",f,1));
  2843. }
  2844. int
  2845. cmkeyx(table,n,xhlp,xdef,f)
  2846. /* cmkeyx */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  2847.     return(cmkey2(table,n,xhlp,xdef,"",f,0));
  2848. }
  2849. int
  2850. cmswi(table,n,xhlp,xdef,f)
  2851. /* cmswi */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  2852.     return(cmkey2(table,n,xhlp,xdef,"",f,4));
  2853. }
  2854.  
  2855. int
  2856. cmkey2(table,n,xhlp,xdef,tok,f,pmsg)
  2857.     struct keytab table[];
  2858.     int n;
  2859.     char *xhlp,
  2860.     *xdef;
  2861.     char *tok;
  2862.     xx_strp f;
  2863.     int pmsg;
  2864. { /* cmkey2 */
  2865.     int i, tl, y, z = 0, zz, xc, wordlen = 0, cmswitch;
  2866.     char *xp, *zq;
  2867.  
  2868.     if (!xhlp) xhlp = "";
  2869.     if (!xdef) xdef = "";
  2870.  
  2871.     cmfldflgs = 0;
  2872.     if (!table) {
  2873.     printf("?Keyword table missing\n");
  2874.     return(-9);
  2875.     }
  2876.     tl = (int)strlen(tok);
  2877.     inword = xc = cc = 0;        /* Clear character counters. */
  2878.     cmswitch = pmsg & 4;        /* Flag for parsing a switch */
  2879.  
  2880.     debug(F101,"cmkey: pmsg","",pmsg);
  2881.     debug(F101,"cmkey: cmflgs","",cmflgs);
  2882.     /* debug(F101,"cmkey: cmdbuf","",cmdbuf);*/
  2883.  
  2884.     ppvnambuf[0] = NUL;
  2885.  
  2886.     if ((zz = cmflgs) == 1) {        /* Command already entered? */
  2887.     if (setatm(xdef,0) < 0) {    /* Yes, copy default into atom buf */
  2888.         printf("?Default too long\n");
  2889.         return(-9);
  2890.     }
  2891.     } else
  2892.       zz = gtword((pmsg == 4) ? 1 : 0);    /* Otherwise get a command word */
  2893.  
  2894.     debug(F101,"cmkey table length","",n);
  2895.     debug(F101,"cmkey cmflgs","",cmflgs);
  2896.     debug(F101,"cmkey zz","",zz);
  2897.     debug(F101,"cmkey cc","",cc);
  2898.     rtimer();                /* Reset timer */
  2899.  
  2900.     while (1) {
  2901.     xc += cc;
  2902.     debug(F111,"cmkey gtword xc",atmbuf,xc);
  2903.  
  2904.     switch (zz) {
  2905.       case -10:            /* Timeout */
  2906.         if (gtimer() < timelimit) {
  2907.         zz = gtword((pmsg == 4) ? 1 : 0);
  2908.         continue;
  2909.         } else {
  2910. #ifdef IKSD
  2911.                 extern int inserver;
  2912.                 if (inserver) {
  2913.                     printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  2914.                     doexit(GOOD_EXIT,0);
  2915.                 }
  2916. #endif /* IKSD */
  2917.         return(-10);
  2918.             }
  2919.       case -5:
  2920.         return(cmflgs = 0);
  2921.       case -9:
  2922.         printf("Command or field too long\n");
  2923.       case -4:            /* EOF */
  2924.       case -3:            /* Null Command/Quit/Timeout */
  2925.       case -2:            /* Buffer overflow */
  2926.       case -1:            /* Or user did some deleting. */
  2927.         return(cmflgs = zz);
  2928.  
  2929.       case 0:            /* User terminated word with space */
  2930.       case 1:            /* or newline */
  2931.       case 4:            /* or switch ending in : or = */
  2932.         wordlen = cc;        /* Length if no conversion */
  2933.         if (cc == 0) {        /* Supply default if we got nothing */
  2934.         if ((wordlen = setatm(xdef,(zz == 4) ? 2 : 0)) < 0) {
  2935.             printf("?Default too long\n");
  2936.             return(-9);
  2937.         }
  2938.         }
  2939.         if (zz == 1 && cc == 0)    /* Required field missing */
  2940.           return(-3);
  2941.  
  2942.         if (f) {            /* If a conversion function is given */
  2943.         char * pp;
  2944.         zq = atxbuf;        /* apply it */
  2945.         pp = atxbuf;
  2946.         atxn = CMDBL;
  2947.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  2948.         debug(F110,"cmkey atxbuf after *f",atxbuf,0);
  2949.         if (!*pp)        /* Supply default if we got nothing */
  2950.           pp = xdef;
  2951.         ckstrncpy(ppvnambuf,atmbuf,PPVLEN);
  2952.         if ((wordlen = setatm(pp,(zz == 4) ? 2 : 0)) < 0) {
  2953.             printf("Evaluated keyword too long\n");
  2954.             return(-9);
  2955.         }
  2956.         }
  2957.         if (cmswitch && *atmbuf != '/') {
  2958.         if (pmsg & 1) {
  2959.             bleep(BP_FAIL);
  2960.                     printf("?Not a switch - %s\n",atmbuf);
  2961.         }
  2962.         cmflgs = -2;
  2963.         return(-6);
  2964.         }
  2965.         if (cmswitch) {
  2966.         int i;
  2967.         for (i = 0; i < wordlen; i++) {
  2968.             if (atmbuf[i] == ':' || atmbuf[i] == '=') {
  2969.             atmbuf[i] = NUL;
  2970.             break;
  2971.             }
  2972.         }
  2973.         }
  2974.         y = lookup(table,atmbuf,n,&z); /* Look up the word in the table */
  2975.         switch (y) {
  2976.           case -3:            /* Nothing to look up */
  2977.         break;
  2978.           case -2:            /* Ambiguous */
  2979.         cmflgs = -2;
  2980.         if (pmsg & 1) {
  2981.             bleep(BP_FAIL);
  2982.                     printf("?Ambiguous - %s\n",atmbuf);
  2983.             return(-9);
  2984.         }
  2985.         return(-2);
  2986.           case -1:            /* Not found at all */
  2987.         if (tl) {
  2988.             for (i = 0; i < tl; i++) /* Check for token */
  2989.               if (tok[i] == *atmbuf) { /* Got one */
  2990.               debug(F000,"cmkey token:",atmbuf,*atmbuf);
  2991.               ungword();  /* Put back the following word */
  2992.               return(-5); /* Special return code for token */
  2993.               }
  2994.         }
  2995.         if (tl == 0) {        /* No tokens were included */
  2996. #ifdef OS2
  2997.             /* In OS/2 and Windows, allow for a disk letter like DOS */
  2998.             if (isalpha(*atmbuf) && *(atmbuf+1) == ':')
  2999.               return(-7);
  3000. #endif /* OS2 */
  3001.             if ((pmsg & 1) && !quiet) {
  3002.             bleep(BP_FAIL);
  3003.             printf("?No keywords match - %s\n",atmbuf); /* cmkey */
  3004.             }
  3005.             return(cmflgs = -9);
  3006.         } else {
  3007.             if (cmflgs == 1 || cmswitch) /* cmkey2 or cmswi */
  3008.               return(cmflgs = -6);
  3009.             else
  3010.               return(cmflgs = -2);
  3011.             /* The -6 code is to let caller try another table */
  3012.         }
  3013.         break;
  3014.           default:
  3015. #ifdef CK_RECALL
  3016.         if (test(table[z].flgs,CM_NOR)) no_recall = 1;
  3017. #endif /* CK_RECALL */
  3018.         if (zz == 4)
  3019.           swarg = 1;
  3020.         cmkwflgs = table[z].flgs;
  3021.         break;
  3022.         }
  3023.         return(y);
  3024.  
  3025.       case 2:            /* User terminated word with ESC */
  3026.         debug(F101,"cmkey Esc cc","",cc);
  3027.             if (cc == 0) {
  3028.         if (*xdef != NUL) {     /* Nothing in atmbuf */
  3029.             printf("%s ",xdef); /* Supply default if any */
  3030. #ifdef GEMDOS
  3031.             fflush(stdout);
  3032. #endif /* GEMDOS */
  3033.             addbuf(xdef);
  3034.             if (setatm(xdef,0) < 0) {
  3035.             printf("?Default too long\n");
  3036.             return(-9);
  3037.             }
  3038.             inword = cmflgs = 0;
  3039.             debug(F111,"cmkey: default",atmbuf,cc);
  3040.         } else {
  3041.             debug(F101,"cmkey Esc pmsg","",0);
  3042. #ifdef COMMENT
  3043. /*
  3044.   Chained FDBs...  The idea is that this function might not have a default,
  3045.   but the next one might.  But if it doesn't, there is no way to come back to
  3046.   this one.  To be revisited later...
  3047. */
  3048.             if (xcmfdb)        /* Chained fdb -- try next one */
  3049.               return(-3);
  3050. #endif /* COMMENT */
  3051.             if (pmsg & (1|4)) {    /* So for now just beep */
  3052.             bleep(BP_WARN);
  3053.             }
  3054.             break;
  3055.         }
  3056.             }
  3057.         if (f) {            /* If a conversion function is given */
  3058.         char * pp;
  3059.         zq = atxbuf;        /* apply it */
  3060.         pp = atxbuf;
  3061.         atxn = CMDBL;
  3062.         if ((*f)(atmbuf,&zq,&atxn) < 0)
  3063.           return(-2);
  3064.         if (!*pp)
  3065.           pp = xdef;
  3066.         if (setatm(pp,0) < 0) {
  3067.             printf("Evaluated keyword too long\n");
  3068.             return(-9);
  3069.         }
  3070.         }
  3071.         y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */
  3072.         debug(F111,"cmkey lookup y",atmbuf,y);
  3073.         debug(F111,"cmkey lookup z",atmbuf,z);
  3074.         if (y == -2 && z >= 0 && z < n) { /* Ambiguous */
  3075. #ifndef NOPARTIAL
  3076.         int j, k, len = 9999;    /* Do partial completion */
  3077.         /* Skip past any abbreviations in the table */
  3078.         for ( ; z < n; z++) {
  3079.             if (table[z].flgs & CM_ABR == 0)
  3080.               break;
  3081.             if (!(table[z].flgs & CM_HLP) || (pmsg & 2))
  3082.               break;
  3083.         }
  3084.         debug(F111,"cmkey partial z",atmbuf,z);
  3085.         debug(F111,"cmkey partial n",atmbuf,n);
  3086.         for (j = z+1; j < n; j++) {
  3087.             debug(F111,"cmkey partial j",table[j].kwd,j);
  3088.             if (ckstrcmp(atmbuf,table[j].kwd,cc,0))
  3089.               break;
  3090.             if (table[j].flgs & CM_ABR)
  3091.               continue;
  3092.             if ((table[j].flgs & CM_HLP) && !(pmsg & 2))
  3093.               continue;
  3094.             k = ckstrpre(table[z].kwd,table[j].kwd);
  3095.             debug(F111,"cmkey partial k",table[z].kwd,k);
  3096.             if (k < len)
  3097.               len = k; /* Length of longest common prefix */
  3098.         }
  3099.         debug(F111,"cmkey partial len",table[z].kwd,len);
  3100.         if (len != 9999 && len > cc) {
  3101.             strcat(atmbuf,table[z].kwd+cc);
  3102.             atmbuf[len] = NUL;
  3103.             printf("%s",atmbuf+cc);
  3104.             strcat(cmdbuf,atmbuf+cc);
  3105.             xc += (len - cc);
  3106.             cc = len;
  3107.         }
  3108. #endif /* NOPARTIAL */
  3109.         bleep(BP_WARN);
  3110.         break;
  3111.         } else if (y == -3) {
  3112.         bleep(BP_WARN);
  3113.         break;
  3114.         } else if (y == -1) {    /* Not found */
  3115.         if ((pmsg & 1) && !quiet) {
  3116.             bleep(BP_FAIL);
  3117.             printf("?No keywords match - \"%s\"\n",atmbuf);
  3118.         }
  3119.         cmflgs = -2;
  3120.         return(-9);
  3121.         }
  3122. /*
  3123.   If we found it, but it's a help-only keyword and the "help" bit is not
  3124.   set in pmsg, then not found.
  3125. */
  3126.         debug(F101,"cmkey flgs","",table[z].flgs);
  3127.         if (test(table[z].flgs,CM_HLP) && ((pmsg & 2) == 0)) {
  3128.         if ((pmsg & 1) && !quiet) {
  3129.             bleep(BP_FAIL);
  3130.             printf("?No keywords match - %s\n",atmbuf);
  3131.         }
  3132.         cmflgs = -2;
  3133.         return(-9);
  3134.         }
  3135. /*
  3136.   See if the keyword just found has the CM_ABR bit set in its flgs field, and
  3137.   if so, search forwards in the table for a keyword that has the same kwval
  3138.   but does not have CM_ABR (or CM_INV?) set, and then expand using the full
  3139.   keyword.  WARNING: This assumes that (a) keywords are in alphabetical order,
  3140.   and (b) the CM_ABR bit is set only if the the abbreviated keyword is a true
  3141.   abbreviation (left substring) of the full keyword.
  3142. */
  3143.         if (test(table[z].flgs,CM_ABR)) {
  3144.         int zz;
  3145.         for (zz = z+1; zz < n; zz++)
  3146.           if ((table[zz].kwval == table[z].kwval) &&
  3147.               (!test(table[zz].flgs,CM_ABR)) &&
  3148.               (!test(table[zz].flgs,CM_INV))) {
  3149.               z = zz;
  3150.               break;
  3151.           }
  3152.         }
  3153.         xp = table[z].kwd + cc;
  3154.         if (cmswitch && test(table[z].flgs,CM_ARG)) {
  3155. #ifdef VMS
  3156.         printf("%s=",xp);
  3157.         brkchar = '=';
  3158. #else
  3159.         printf("%s:",xp);
  3160.         brkchar = ':';
  3161. #endif /* VMS */
  3162.         } else {
  3163.         printf("%s ",xp);
  3164.         brkchar = SP;
  3165.         }
  3166. #ifdef CK_RECALL
  3167.         if (test(table[z].flgs,CM_NOR)) no_recall = 1;
  3168. #endif /* CK_RECALL */
  3169.         cmkwflgs = table[z].flgs;
  3170. #ifdef GEMDOS
  3171.         fflush(stdout);
  3172. #endif /* GEMDOS */
  3173.         addbuf(xp);
  3174.         if (cmswitch && test(table[z].flgs,CM_ARG)) {
  3175.         bp--;            /* Replace trailing space with : */
  3176. #ifdef VMS
  3177.         *bp++ = '=';
  3178. #else
  3179.         *bp++ = ':';
  3180. #endif /* VMS */
  3181.         *bp = NUL;
  3182.         np = bp;
  3183.         swarg = 1;
  3184.         }
  3185.         inword = 0;
  3186.         cmflgs = 0;
  3187.         debug(F110,"cmkey: addbuf",cmdbuf,0);
  3188.         return(y);
  3189.  
  3190.       case 3:            /* User typed "?" */
  3191.         if (f) {            /* If a conversion function is given */
  3192.         char * pp;
  3193.         zq = atxbuf;        /* do the conversion now. */
  3194.         pp = atxbuf;
  3195.         atxn = CMDBL;
  3196.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  3197.         if (setatm(pp,0) < 0) {
  3198.             printf("?Evaluated keyword too long\n");
  3199.             return(-9);
  3200.         }
  3201.         }
  3202.         y = lookup(table,atmbuf,n,&z); /* Look up what we have so far. */
  3203.         if (y == -1) {
  3204.         cmflgs = -2;
  3205.         if ((pmsg & 1) && !quiet) {
  3206.             bleep(BP_FAIL);
  3207.             printf(" No keywords match\n");
  3208.             return(-9);
  3209.         }
  3210.         return(-2);
  3211.         }
  3212.         if (*xhlp == NUL)
  3213.           printf(" One of the following:\n");
  3214.         else
  3215.           printf(" %s, one of the following:\n",xhlp);
  3216.  
  3217. #ifdef OLDHELP
  3218.         if ((y > -1) &&
  3219.         !test(table[z].flgs,CM_ABR) &&
  3220.         ((z >= n-1) || ckstrcmp(table[z].kwd,table[z+1].kwd,cc,0))
  3221.         ) {
  3222.         printf(" %s\n",table[z].kwd);
  3223.         } else {
  3224.         clrhlp();
  3225.         for (i = 0; i < n; i++) {
  3226.             if (!ckstrcmp(table[i].kwd,atmbuf,cc,0)
  3227.             && !test(table[i].flgs,CM_INV)
  3228.             )
  3229.               addhlp(table[i].kwd);
  3230.         }
  3231.         dmphlp();
  3232.         }
  3233. #else  /* New way ... */
  3234.         {
  3235.         int x;
  3236.         x = pmsg & (2|4);    /* See kwdhelp() comments */
  3237.         if (atmbuf[0])        /* If not at beginning of field */
  3238.           x |= 1;        /* also show invisibles */
  3239.         kwdhelp(table,n,atmbuf,"","",1,x);
  3240.         }
  3241. #endif /* OLDHELP */
  3242. #ifndef NOSPL
  3243.         {
  3244.         extern int topcmd;
  3245.         if (tl > 0 && topcmd != XXHLP) /* This is bad... */
  3246.           printf("or the name of a macro (\"do ?\" for a list)\n");
  3247.         }
  3248. #endif /* NOSPL */
  3249.         if (*atmbuf == NUL) {
  3250.         if (tl == 1)
  3251.           printf("or the token %c\n",*tok);
  3252.         else if (tl > 1)
  3253.           printf("or one of the tokens: %s\n",ckspread(tok));
  3254.         }
  3255.         printf("%s%s", cmprom, cmdbuf);
  3256.         fflush(stdout);
  3257.         break;
  3258.  
  3259.       default:
  3260.         printf("\n%d - Unexpected return code from gtword\n",zz);
  3261.         return(cmflgs = -2);
  3262.     }
  3263.     zz = gtword(0);
  3264.     debug(F111,"cmkey gtword zz",atmbuf,zz);
  3265.     }
  3266. }
  3267.  
  3268. int
  3269. chktok(tlist) char *tlist; {
  3270.     char *p;
  3271.     p = tlist;
  3272.     while (*p != NUL && *p != *atmbuf) p++;
  3273.     return((*p) ? (int) *p : 0);
  3274. }
  3275.  
  3276. /* Routines for parsing and converting dates and times */
  3277.  
  3278. #define isdatesep(c) (c==SP||c=='-'||c=='/'||c=='.'||c=='_')
  3279. #define istimesep(c) (c==':' || c=='P'||c=='p'||c=='A'||c=='a')
  3280.  
  3281. char cmdatebuf[18] = { NUL, NUL };
  3282.  
  3283. #define TU_DAYS   0
  3284. #define TU_WEEKS  1
  3285. #define TU_MONTHS 2
  3286. #define TU_YEARS  3
  3287.  
  3288. static struct keytab timeunits[] = {
  3289.   { "days",   TU_DAYS,   0 },
  3290.   { "months", TU_MONTHS, 0 },
  3291.   { "weeks",  TU_WEEKS,  0 },
  3292.   { "wks",    TU_WEEKS,  0 },
  3293.   { "years",  TU_YEARS,  0 },
  3294.   { "yrs",    TU_YEARS,  0 }
  3295. };
  3296. static int nunits = (sizeof(timeunits) / sizeof(struct keytab));
  3297.  
  3298. /*  C M C V T D A T E  --  Converts free-form date to standard form.  */
  3299.  
  3300. /*
  3301.    Call with
  3302.      s = pointer to free-format date-time
  3303.      t = 0: return time only if time was given in s
  3304.      t = 1: always return time (00:00:00 if no time given in s)
  3305.      t = 2: allow time to be > 24:00:00
  3306.    Returns:
  3307.      -1 on failure, 0 or greater on success with result in cmdate[].
  3308. */
  3309. int
  3310. cmcvtdate(s,t) char * s; int t; {
  3311.     int rc = 0, x, i, hh, mm, ss, pmflag = 0, nodate = 0, len;
  3312.     int units;
  3313.     char * fld[3], * p;
  3314.     char * year, * month = NULL, * day;
  3315.     char * hour = "00", * min = "00", * sec = "00";
  3316.     char tmpbuf[8];
  3317.     char xbuf[32];
  3318.     char ybuf[32];
  3319.     char dbuf[26];
  3320.     char daybuf[3];
  3321.     char monbuf[3];
  3322.     char yearbuf[5];
  3323.  
  3324.     debug(F110,"cmcvtdate",s,0);
  3325.     if (!s) s = "";
  3326.  
  3327.     tmpbuf[0] = NUL;
  3328.  
  3329.     while (*s == SP) s++;        /* Gobble any leading blanks */
  3330.  
  3331.     len = strlen(s);
  3332.     if (len == 0) {
  3333.     s = ckdate();
  3334.     len = 17;
  3335.     debug(F110,"cmcvtdate now",s,0);
  3336.     }
  3337.     if (len > 30) {            /* Check length of arg */
  3338.     debug(F101,"cmcvtdate date too long","",-1);
  3339.     return(-1);
  3340.     }
  3341.     ckstrncpy(xbuf,s,32);        /* Make a local copy we can poke */
  3342.     s = xbuf;                /* Point to it */
  3343.     s[len] = NUL;
  3344.  
  3345. /* First we handle "today", "yesterday", "tomorrow", etc */
  3346.  
  3347.     if (ckstrchr("+-TtYy",s[0])) {
  3348.     int i, k, n, minus = 0;
  3349.     long jd;
  3350.     jd = mjd(ckdate());
  3351.     debug(F111,"cmcvtdate NEW",s,jd);
  3352.     if (s[0] == '+' || s[0] == '-') { /* {+,-} <number> <timeunits> */
  3353.         char * kp, * np;
  3354.         char tmpyear[5];
  3355.         char * dp;
  3356.         if (s[0] == '-')        /* Sign */
  3357.           minus = 1;
  3358.         kp = s+1;            /* Skip intervening spaces */
  3359.         while (*kp) {
  3360.         if (*kp == SP) kp++;
  3361.         else break;
  3362.         }
  3363.         if (!*kp)
  3364.           return(-1);
  3365.         np = kp;            /* Number */
  3366.         while (*kp) {
  3367.         if (isdigit(*kp)) kp++;
  3368.         else break;
  3369.         }
  3370.         if (!*kp)
  3371.           return(-1);
  3372.         n = atoi(np);
  3373.         if (minus) n = 0 - n;
  3374.         debug(F101,"cmcvtdate offset n","",n);
  3375.         while (*kp) {        /* Find end of number */
  3376.         if (isdigit(*kp)) kp++;
  3377.         else break;
  3378.         }
  3379.         while (*kp) {        /* Skip spaces again */
  3380.         if (*kp == SP) kp++;
  3381.         else break;
  3382.         }
  3383.         debug(F110,"cmcvtdate unit start",kp,0);
  3384.         p = kp;            /* Units */
  3385.         while (*p) {
  3386.         if (isalpha(*p)) {
  3387.             p++;
  3388.         } else if (isdatesep(*p)) { /* Have a date separator */
  3389.             *p++ = NUL;
  3390.             break;
  3391.         } else
  3392.           return(-1);
  3393.         }
  3394.         while (*p) {        /* Skip any spaces */
  3395.         if (*p == SP) p++;
  3396.         else break;
  3397.         }
  3398.         debug(F110,"cmcvtdate time pointer",p,0);
  3399.         debug(F110,"cmcvtdate unit",kp,0);
  3400.         x = lookup(timeunits,kp,nunits,&k);    /* Look up units */
  3401.         if (x < 0) {
  3402.         debug(F111,"cmcvtdate lookup fails",kp,x);
  3403.         return(-1);
  3404.         }
  3405.         units = x;
  3406.         debug(F111,"cmcvtdate offset units",timeunits[k].kwd,units);
  3407.  
  3408.         switch (units) {        /* Handle each unit */
  3409.           case TU_DAYS:        /* Days */
  3410.         jd += n;
  3411.         strcpy(ybuf,mjd2date(jd));
  3412.         if (*p) {
  3413.             ybuf[8] = ' ';
  3414.             strcpy(ybuf+9,p);
  3415.         } else
  3416.           strcpy(ybuf+8," 00:00:00");
  3417.         s = ybuf;
  3418.         len = strlen(ybuf);
  3419.         debug(F111,"cmcvtdate days",s,len);
  3420.         goto normal;
  3421.  
  3422.           case TU_WEEKS:        /* Weeks */
  3423.         jd += (7 * n);
  3424.         strcpy(ybuf,mjd2date(jd));
  3425.         if (*p) {
  3426.             ybuf[8] = ' ';
  3427.             strcpy(ybuf+9,p);
  3428.         } else
  3429.           strcpy(ybuf+8," 00:00:00");
  3430.         s = ybuf;
  3431.         len = strlen(ybuf);
  3432.         debug(F111,"cmcvtdate weeks",s,len);
  3433.         goto normal;
  3434.  
  3435.           case TU_MONTHS: {        /* Months */
  3436.           char tmpmonth[3];
  3437.           int xyear, xmonth;
  3438.           dp = ckdate();
  3439.           tmpyear[0] = dp[0];
  3440.           tmpyear[1] = dp[1];
  3441.           tmpyear[2] = dp[2];
  3442.           tmpyear[3] = dp[3];
  3443.           tmpyear[4] = NUL;
  3444.           tmpmonth[0] = dp[4];
  3445.           tmpmonth[1] = dp[5];
  3446.           tmpmonth[2] = NUL;
  3447.           xyear = atoi(tmpyear);
  3448.           xmonth = atoi(tmpmonth);
  3449.           xmonth += n;
  3450.           xyear += (xmonth / 12);
  3451.           xmonth = (xmonth % 12);
  3452.           if (xmonth <= 0) {
  3453.               xmonth += 12;
  3454.               xyear--;
  3455.           }
  3456.           sprintf(ybuf,"%04d%02d%s",xyear,xmonth,dp+6);
  3457.           if (*p) {
  3458.               ybuf[8] = ' ';
  3459.               strcpy(ybuf+9,p);
  3460.           } else
  3461.             strcpy(ybuf+8," 00:00:00");
  3462.           s = ybuf;
  3463.           len = strlen(ybuf);
  3464.           debug(F111,"cmcvtdate months",s,len);
  3465.           goto normal;
  3466.           }
  3467.           case TU_YEARS: {        /* Years */
  3468.           dp = ckdate();
  3469.           tmpyear[0] = dp[0];
  3470.           tmpyear[1] = dp[1];
  3471.           tmpyear[2] = dp[2];
  3472.           tmpyear[3] = dp[3];
  3473.           tmpyear[4] = NUL;
  3474.           sprintf(ybuf,"%04d%s",(atoi(tmpyear)+n),dp+4);
  3475.           if (*p) {
  3476.               ybuf[8] = ' ';
  3477.               strcpy(ybuf+9,p);
  3478.           } else
  3479.             strcpy(ybuf+8," 00:00:00");
  3480.           s = ybuf;
  3481.           len = strlen(ybuf);
  3482.           debug(F111,"cmcvtdate years",s,len);
  3483.           goto normal;
  3484.           }
  3485.         }
  3486.         return(-1);
  3487.     }
  3488.     i = ckstrpre(s,"today");    /* TODAY */
  3489.     if (i > 2 && (s[i] == NUL || isdatesep(s[i]))) {
  3490.         strncpy(ybuf,ckdate(),8);
  3491.         strcpy(ybuf+8," 00:00:00");
  3492.         if (s[i])
  3493.           strcpy(ybuf+8,s+i);
  3494.         s = ybuf;
  3495.         len = strlen(s);
  3496.         debug(F111,"cmcvtdate today",s,len);
  3497.         goto normal;
  3498.     }
  3499.     i = ckstrpre(s,"tomorrow");    /* TOMORROW */
  3500.     if (i > 2 && (s[i] == NUL || isdatesep(s[i]))) {
  3501.         jd++;
  3502.         strncpy(ybuf,mjd2date(jd),8);
  3503.         strcpy(ybuf+8," 00:00:00");
  3504.         if (s[i]) strcpy(ybuf+8,s+i);
  3505.         s = ybuf;
  3506.         len = strlen(s);
  3507.         debug(F111,"cmcvtdate tomorrow",s,len);
  3508.         goto normal;
  3509.  
  3510.     }
  3511.     i = ckstrpre(s,"yesterday");    /* YESTERDAY */
  3512.     if (i > 0 && (s[i] == NUL || isdatesep(s[i]))) {
  3513.         jd--;
  3514.         strncpy(ybuf,mjd2date(jd),8);
  3515.         strcpy(ybuf+8," 00:00:00");
  3516.         if (s[i]) strcpy(ybuf+8,s+i);
  3517.         s = ybuf;
  3518.         len = strlen(s);
  3519.         debug(F111,"cmcvtdate yesterday",s,len);
  3520.         goto normal;
  3521.     }
  3522.     }
  3523.  
  3524.   normal:
  3525.  
  3526.     if (len >= 8) {            /* Already in right format? */
  3527.     if (isdigit(s[0])  && isdigit(s[1])  &&
  3528.         isdigit(s[2])  && isdigit(s[3])  &&
  3529.         isdigit(s[4])  && isdigit(s[5])  &&
  3530.         isdigit(s[6])  && isdigit(s[7]))
  3531.       if (!s[8]) {
  3532.           strcat(s," 00:00:00");
  3533.           ckstrncpy(cmdatebuf,s,18);
  3534.           debug(F111,"cmcvtdate yyyymmdd",s,rc);
  3535.           return(rc);
  3536.       } else if (len == 17 &&
  3537.              isdigit(s[9])  && isdigit(s[10]) &&
  3538.              isdigit(s[12]) && isdigit(s[13]) &&
  3539.              isdigit(s[15]) && isdigit(s[16]) &&
  3540.              s[11] == ':'   && s[14] == ':'   &&
  3541.              (s[8] == SP || s[8] == '-' || s[8] == '_')) {
  3542.           ckstrncpy(cmdatebuf,s,18);
  3543.           debug(F111,"cmcvtdate yyyymmdd hh:mm:ss",s,rc);
  3544.           return(rc);
  3545.     } else {            /* We have a numeric date */
  3546.         debug(F111,"cmcvtdate yyyymmdd xxx",s,rc);
  3547.         p = s+9;            /* Start of time field */
  3548.  
  3549.         yearbuf[0] = s[0]; yearbuf[1] = s[1];
  3550.         yearbuf[2] = s[2]; yearbuf[3] = s[3];
  3551.         yearbuf[4] = NUL;  year = yearbuf;
  3552.  
  3553.         monbuf[0] = s[4];  monbuf[1] = s[5];
  3554.         monbuf[2] = NUL;   month = monbuf;
  3555.  
  3556.         daybuf[0] = s[6];  daybuf[1] = s[7];
  3557.         daybuf[2] = NUL;   day = daybuf;
  3558.  
  3559.         goto dotime;
  3560.     }
  3561.     }
  3562.     fld[i = 0] = (p = s);        /* First field */
  3563.     while (*p) {            /* Get next two fields */
  3564.     if (isdatesep(*p)) {        /* Have a date separator */
  3565.         *p++ = NUL;            /* Replace by NUL */
  3566.         if (*p) {            /* Now we're at the next field */
  3567.         while (*p == SP) p++;    /* Skip leading spaces */
  3568.         if (!*p) break;        /* Make sure we still have something */
  3569.         if (i == 2)        /* Last one? */
  3570.           break;
  3571.         fld[++i] = p;        /* No, record pointer to this one */
  3572.         } else
  3573.           break;
  3574.     } else if (istimesep(*p)) {    /* Have a time separator */
  3575.         if (isalpha(*p) && !isdigit(*(p-1))) { /* Might be letter */
  3576.         p++;            /* in month name... */
  3577.         continue;
  3578.         }
  3579.         if (i != 0) {        /* After a date */
  3580.         debug(F111,"cmcvtdate date bad timesep",p,-1);
  3581.         return(-1);
  3582.         }
  3583.         nodate = 1;            /* Or without a date */
  3584.         break;
  3585.     }
  3586.     p++;
  3587.     }
  3588.     if (p > s && i == 0)        /* Make sure we have a date */
  3589.       nodate = 1;
  3590.  
  3591.     if (nodate) {            /* No date */
  3592.     char *tmp;            /* Substitute today's date */
  3593.     ztime(&tmp);
  3594.     if (!tmp) {
  3595.         debug(F101,"cmcvtdate null ztime","",-1);
  3596.         return(-1);
  3597.     }
  3598.     if (!*tmp) {
  3599.         debug(F101,"cmcvtdate emtpy ztime","",-1);
  3600.         return(-1);
  3601.     }
  3602.     ckstrncpy(dbuf,tmp,26);        /* Reformat */
  3603.     if (dbuf[8] == SP) dbuf[8] = '0';
  3604.     fld[0] = dbuf+8;
  3605.     dbuf[10] = NUL;
  3606.     fld[1] = dbuf+4;
  3607.     dbuf[7] = NUL;
  3608.     fld[2] = dbuf+20;
  3609.     dbuf[24] = NUL;
  3610.     p = s;                /* Back up source pointer to reparse */
  3611.     } else if (i != 2) {
  3612.     debug(F101,"cmcvtdate fail A","",-1);
  3613.     return(-1);
  3614.     }
  3615.     if (!rdigits(fld[0])) {        /* Now parse the date */
  3616.     debug(F101,"cmcvtdate fail B","",-1);
  3617.     return(-1);
  3618.     }
  3619.     if (!rdigits(fld[1])) {
  3620.     if ((x = lookup(cmonths,fld[1],12,NULL)) < 0) {
  3621.         debug(F101,"cmcvtdate fail C","",-1);
  3622.         return(-1);
  3623.     }
  3624.     sprintf(tmpbuf,"%02d",x);
  3625.     month = tmpbuf;
  3626.     }
  3627.     if (((int)strlen(fld[0]) == 4)) {    /* yyyy-xx-dd */
  3628.     year = fld[0];
  3629.     day = fld[2];
  3630.     if (!month)
  3631.       month = fld[1];        /* yyyy-mm-dd */
  3632.     } else if (((int)strlen(fld[2]) == 4)) { /* xx-xx-yyyy */
  3633.     year = fld[2];
  3634.     if (month) {            /* dd-name-yyyy */
  3635.         day = fld[0];
  3636.     } else {            /* xx-xx-yyyy */
  3637.         int f0, f1;
  3638.         f0 = atoi(fld[0]);
  3639.         f1 = atoi(fld[1]);
  3640.         if ((f0 > 12) && (f1 <= 12)) {
  3641.         day = fld[0];        /* mm-dd-yyyy */
  3642.         month = fld[1];
  3643.         } else if ((f0 <= 12) && (f1 > 12)) {
  3644.         if (!rdigits(fld[1]))
  3645.           return(-1);
  3646.         else
  3647.           day = fld[1];        /* dd-mm-yyyy */
  3648.         month = fld[0];
  3649. #ifdef COMMENT
  3650.         } else if ((f0 <= 12) && (f1 <= 12)) {
  3651.         if (!quiet)
  3652.           printf("?Day and month are ambiguous - \"%s\"\n", o);
  3653.         return(-9);
  3654. #endif /* COMMENT */
  3655.         } else {
  3656.         debug(F101,"cmcvtdate fail D","",-1);
  3657.         return(-1);
  3658.         }
  3659.     }
  3660.     } else {
  3661.     debug(F101,"cmcvtdate fail E","",-1);
  3662.     return(-1);
  3663.     }
  3664.     x = atoi(month);
  3665.     sprintf(tmpbuf,"%02d",x);        /* 2-digit numeric month */
  3666.  
  3667.   dotime:
  3668.     debug(F110,"cmcvtdate dotime s",s,0);
  3669.     debug(F110,"cmcvtdate dotime p",p,0);
  3670.     if ((x  = atoi(day)) > 31) {
  3671.     debug(F101,"cmcvtdate fail K","",-1);
  3672.     return(-1);
  3673.     }
  3674.     if (!*p && t == 0) {
  3675.     sprintf(cmdatebuf,"%s%s%02d",year,month,x);
  3676.     return(0);
  3677.     }
  3678.     fld[i = 0] = *p ? p : "00";        /* First time field */
  3679.     fld[1] = "00";
  3680.     fld[2] = "00";
  3681.     while (*p) {            /* Get the rest, if any */
  3682.     if (istimesep(*p)) {
  3683.         debug(F000,"cmcvtdate timesep:",p,*p);
  3684.         if (*p == 'P' || *p == 'p') {
  3685.         if (*(p+1) != 'M' && *(p+1) != 'm') {
  3686.             debug(F101,"cmcvtdate fail F","",-1);
  3687.             return(-1);
  3688.         } else if (!*(p+2)) {
  3689.             pmflag = 1;
  3690.             *p = NUL;
  3691.             break;
  3692.         } else {
  3693.             debug(F101,"cmcvtdate fail F","",-1);
  3694.             return(-1);
  3695.         }
  3696.         } else if (*p == 'A' || *p == 'a') {
  3697.         if (*(p+1) != 'M' && *(p+1) != 'm') {
  3698.             debug(F101,"cmcvtdate fail F","",-1);
  3699.             return(-1);
  3700.         }
  3701.         if (*(p+2)) {
  3702.             debug(F101,"cmcvtdate fail F","",-1);
  3703.             return(-1);
  3704.         }
  3705.         *p = NUL;
  3706.         break;
  3707.         }
  3708.         *p++ = NUL;
  3709.         if (*p) {
  3710.         while (*p == SP) p++;
  3711.         if (!*p) break;
  3712.         if (i == 2)
  3713.           break;
  3714.         fld[++i] = p;
  3715.         } else
  3716.           break;
  3717.     }
  3718.     p++;
  3719.     }
  3720.     debug(F101,"cmcvtdate time i","",i);
  3721.     debug(F110,"cmcvtdate time fld[0]",fld[0],0);
  3722.     debug(F110,"cmcvtdate time fld[1]",fld[1],0);
  3723.     debug(F110,"cmcvtdate time fld[2]",fld[2],0);
  3724.  
  3725.     if (!rdigits(fld[0]))
  3726.       return(-1);
  3727.  
  3728.     hour = fld[0];
  3729.     if (i >= 1) {
  3730.     if (!rdigits(fld[1]))
  3731.       return(-1);
  3732.     else
  3733.       min = fld[1];
  3734.     }
  3735.     if (i == 2) {
  3736.     if (!rdigits(fld[2]))
  3737.       return(-1);
  3738.     else
  3739.       sec = fld[2];
  3740.     }
  3741.     hh = atoi(hour);
  3742.     if (pmflag && hh <= 11)
  3743.       hh += 12;
  3744.     if ((t != 2 && hh > 24) || hh < 0) {
  3745.     debug(F101,"cmcvtdate fail G","",-1);
  3746.     return(-1);
  3747.     }
  3748.     if ((mm = atoi(min)) > 59) {
  3749.     debug(F101,"cmcvtdate fail H","",-1);
  3750.     return(-1);
  3751.     }
  3752.     if ((ss = atoi(sec)) > 59) {
  3753.     debug(F101,"cmcvtdate fail I","",-1);
  3754.     return(-1);
  3755.     }
  3756.     if (mm < 0 || ss < 0) return(-1);
  3757.     if (t != 2 && (ss > 0 || mm > 0) && hh > 23) {
  3758.     debug(F101,"cmcvtdate fail J","",-1);
  3759.     return(-1);
  3760.     }
  3761.     debug(F110,"cmcvtdate year",year,0);
  3762.     debug(F110,"cmcvtdate month",month,0);
  3763.     debug(F101,"cmcvtdate x","",x);
  3764.     debug(F101,"cmcvtdate hh","",hh);
  3765.     debug(F101,"cmcvtdate mm","",mm);
  3766.     debug(F101,"cmcvtdate ss","",ss);
  3767.  
  3768.     sprintf(cmdatebuf,"%s%s%02d %02d:%02d:%02d",year,month,x,hh,mm,ss);
  3769.  
  3770. #ifdef DEBUG
  3771.     if (deblog) {
  3772.     debug(F101,"cmcvtdate hour","",hh);
  3773.     debug(F101,"cmcvtdate minute","",mm);
  3774.     debug(F101,"cmcvtdate second","",ss);
  3775.     debug(F111,"cmcvtdate result",cmdatebuf,rc);
  3776.     }
  3777. #endif /* DEBLOG */
  3778.     return(0);
  3779. }
  3780.  
  3781.  
  3782. /*  C K C V T D A T E  --  Like cmcvtdate(), but returns string.  */
  3783. /*  For use by date-related functions */
  3784. /*  See calling conventions for cmcvtdate() above. */
  3785.  
  3786. char *
  3787. ckcvtdate(p,t) char * p; int t; {
  3788.     if (cmcvtdate(p,t) < 0)
  3789.       return("<BAD_DATE_OR_TIME>");    /* \fblah() error message */
  3790.     else
  3791.       return((char *) cmdatebuf);
  3792. }
  3793.  
  3794.  
  3795. /*  C M D A T E  --  Parse a date and/or time  */
  3796.  
  3797. /*
  3798.   Accepts date in various formats.  If the date is recognized,
  3799.   this routine returns 0 or greater with the result string pointer
  3800.   pointing to a buffer containing the date as "yyyymmdd hh:mm:ss".
  3801. */
  3802. int
  3803. cmdate(xhlp,xdef,xp,quiet,f) char *xhlp, *xdef, **xp; int quiet; xx_strp f; {
  3804.     int /* i, */ x, rc;
  3805.     char *o, *s, *zq;
  3806.  
  3807.     cmfldflgs = 0;
  3808.     if (!xhlp) xhlp = "";
  3809.     if (!xdef) xdef = "";
  3810.     if (!*xhlp) xhlp = "Date and/or time";
  3811.     *xp = "";
  3812.  
  3813.     rc = cmfld(xhlp,xdef,&s,(xx_strp)0);
  3814.     debug(F101,"cmdate cmfld rc","",rc);
  3815.     if (rc < 0)
  3816.       return(rc);
  3817.     debug(F110,"cmdate 1",s,0);
  3818.     o = s;                /* Remember what they typed. */
  3819.     s = brstrip(s);
  3820.     debug(F110,"cmdate 2",s,0);
  3821.  
  3822.     x = 0;
  3823.     if (f) {                /* If a conversion function is given */
  3824.     char * pp;
  3825.     zq = atxbuf;            /* do the conversion. */
  3826.     pp = atxbuf;
  3827.     atxn = CMDBL;
  3828.     if ((x = (*f)(s,&zq,&atxn)) < 0) return(-2);
  3829.     if (!*pp)
  3830.       pp = xdef;
  3831.     if (setatm(pp,0) < 0) {
  3832.         if (!quiet) printf("?Evaluated date too long\n");
  3833.         return(-9);
  3834.     }
  3835.     s = atxbuf;
  3836.     }
  3837.     rc = cmcvtdate(s,1);
  3838.     if (rc < 0) {
  3839.     if (!quiet) printf("Invalid date or time - \"%s\"\n", o);
  3840.     return(-9);
  3841.     }
  3842.     *xp = cmdatebuf;
  3843.     return(rc);
  3844. }
  3845.  
  3846. #ifdef CK_RECALL            /* Command-recall functions */
  3847.  
  3848. /*  C M R I N I  --  Initialize or change size of command recall buffer */
  3849.  
  3850. int
  3851. cmrini(n) int n; {
  3852.     int i;
  3853.     if (recall && in_recall) {        /* Free old storage, if any */
  3854.     for (i = 0; i < cm_recall; i++) {
  3855.         if (recall[i]) {
  3856.         free(recall[i]);
  3857.         recall[i] = NULL;
  3858.         }
  3859.     }
  3860.     free(recall);
  3861.     recall = NULL;
  3862.     }
  3863.     cm_recall = n;            /* Set new size */
  3864.     rlast = current = -1;        /* Initialize pointers */
  3865.     if (n > 0) {
  3866.     recall = (char **)malloc((cm_recall + 1) * sizeof(char *));
  3867.     if (!recall)
  3868.       return(1);
  3869.     for (i = 0; i < cm_recall; i++) {
  3870.         recall[i] = NULL;
  3871.     }
  3872.     in_recall = 1;            /* Recall buffers init'd */
  3873.     }
  3874.     return(0);
  3875. }
  3876.  
  3877. /*  C M A D D N E X T  --  Force addition of next command */
  3878.  
  3879. VOID
  3880. cmaddnext() {
  3881.     if (on_recall && in_recall) {    /* Even if it doesn't come */
  3882.     force_add = 1;            /* from the keyboard */
  3883.     no_recall = 0;
  3884.     }
  3885. }
  3886.  
  3887. /*  C M G E T C M D  --  Find most recent matching command  */
  3888.  
  3889. char *
  3890. cmgetcmd(s) char * s; {
  3891.     int i;
  3892.     for (i = current; i >= 0; i--) {    /* Search backward thru history list */
  3893.     if (!recall[i]) continue;    /* This one's null, skip it */
  3894.     if (ckmatch(s,recall[i],0,1))    /* Match? */
  3895.       return(recall[i]);        /* Yes, return pointer */
  3896.     }
  3897.     return(NULL);            /* No match, return NULL pointer */
  3898. }
  3899.  
  3900. /*  A D D C M D  --  Add a command to the recall buffer  */
  3901.  
  3902. VOID
  3903. addcmd(s) char * s; {
  3904.     int len;
  3905.  
  3906.     if (!s) s = cmdbuf;
  3907.     len = strlen(s);
  3908.  
  3909. #ifdef CKSYSLOG
  3910.     /* Log all interactive commands */
  3911.     /* Logging macros & TAKE files is way too much */
  3912.     if (ckxlogging) {
  3913.     if (ckxsyslog >= SYSLG_CX || ckxsyslog >= SYSLG_CM && !cmdsrc())
  3914.       cksyslog(SYSLG_CX, 1, "command", s, NULL);
  3915.     }
  3916. #endif /* CKSYSLOG */
  3917.  
  3918.     if ((!cmdsrc() || force_add) &&    /* Reading commands from keyboard? */
  3919.     (on_recall) &&            /* Recall is turned on? */
  3920.     (cm_recall > 0) &&        /* Saving commands? */
  3921.     !no_recall &&            /* Not not saving this command? */
  3922.     len > 0) {            /* Non-null command? */
  3923.  
  3924.     force_add = 0;
  3925.  
  3926.         if (rlast >= cm_recall - 1) {    /* Yes, buffer full? */
  3927.         int i;            /* Yes. */
  3928.         if (recall[0]) {        /* Discard oldest command */
  3929.         free(recall[0]);
  3930.         recall[0] = NULL;
  3931.         }
  3932.         for (i = 0; i < rlast; i++) { /* The rest */
  3933.         recall[i] = recall[i+1]; /* move back */
  3934.         }
  3935.         rlast--;            /* Now we have one less */
  3936.     }
  3937.         rlast++;            /* Index of last command in buffer */
  3938.     current = rlast;        /* Also now the current command */
  3939.     if (current >= cm_recall) {
  3940.         printf("Oops, command recall error\n");
  3941.     } else {
  3942.         recall[current] = malloc(len+1);
  3943.         if (recall[current])
  3944.           strcpy(recall[current],s);
  3945.     }
  3946.     }
  3947. }
  3948. #endif /* CK_RECALL */
  3949.  
  3950. int
  3951. cmgetlc(s) char * s; {            /* Get leading char */
  3952.     char c;
  3953.     while ((c = *s++) <= SP) ;
  3954.     return(c);
  3955. }
  3956.  
  3957.  
  3958. /*  C M C F M  --  Parse command confirmation (end of line)  */
  3959.  
  3960. /*
  3961.  Returns
  3962.    -2: User typed anything but whitespace or newline
  3963.    -1: Reparse needed
  3964.     0: Confirmation was received
  3965. */
  3966. int
  3967. cmcfm() {
  3968.     int x, xc;
  3969.     debug(F101,"cmcfm: cmflgs","",cmflgs);
  3970.     debug(F110,"cmcfm: atmbuf",atmbuf,0);
  3971.     inword = xc = cc = 0;
  3972.  
  3973.     setatm("",0);            /* (Probably unnecessary) */
  3974.  
  3975.     while (cmflgs != 1) {
  3976.         x = gtword(0);
  3977.         xc += cc;
  3978.  
  3979.         switch (x) {
  3980.       case -9:
  3981.         printf("Command or field too long\n");
  3982.       case -4:            /* EOF */
  3983.       case -2:
  3984.       case -1:
  3985.         return(x);
  3986.       case 1:            /* End of line */
  3987.         if (xc > 0) {
  3988.         if (xcmfdb) {
  3989.             return(-6);
  3990.         } else {
  3991.             printf("?Not confirmed - %s\n",atmbuf);
  3992.             return(-9);
  3993.         }
  3994.         } else break;        /* Finish up below */
  3995.       case 2:            /* ESC */
  3996.         if (xc == 0) {
  3997.         bleep(BP_WARN);
  3998.         continue;        /* or fall thru. */
  3999.         }
  4000.       case 0:            /* Space */
  4001.         if (xc == 0)        /* If no chars typed, continue, */
  4002.           continue;            /* else fall thru. */
  4003.         /* else fall thru... */
  4004.  
  4005.       case 3:            /* Question mark */
  4006.         if (xc > 0) {
  4007.         if (xcmfdb) {
  4008.             return(-6);
  4009.         } else {
  4010.             printf("?Not confirmed - %s\n",atmbuf);
  4011.             return(-9);
  4012.         }
  4013.         }
  4014.         printf(
  4015.            "\n Press the Return or Enter key to confirm the command\n");
  4016.         printf("%s%s",cmprom,cmdbuf);
  4017.         fflush(stdout);
  4018.         continue;
  4019.     }
  4020.     }
  4021. #ifdef CK_RECALL
  4022.     addcmd(cmdbuf);
  4023. #endif /* CK_RECALL */
  4024.     return(0);
  4025. }
  4026.  
  4027.  
  4028. /* The following material supports chained parsing functions. */
  4029. /* See ckucmd.h for FDB and OFDB definitions. */
  4030.  
  4031. struct OFDB cmresult = {        /* Universal cmfdb result holder */
  4032.     NULL,
  4033.     0,
  4034.     NULL,
  4035.     0
  4036. };
  4037.  
  4038. VOID
  4039. cmfdbi(p,fc,s1,s2,s3,n1,n2,f,k,nxt)    /* Initialize an FDB */
  4040.     struct FDB * p;
  4041.     int fc;
  4042.     char * s1, * s2, * s3;
  4043.     int n1, n2;
  4044.     xx_strp f;
  4045.     struct keytab * k;
  4046.     struct FDB * nxt; {
  4047.  
  4048.     p->fcode = fc;
  4049.     p->hlpmsg = s1;
  4050.     p->dflt = s2;
  4051.     p->sdata = s3;
  4052.     p->ndata1 = n1;
  4053.     p->ndata2 = n2;
  4054.     p->spf = f;
  4055.     p->kwdtbl = k;
  4056.     p->nxtfdb = nxt;
  4057. }
  4058.  
  4059. /*  C M F D B  --  Parse a field with several possible functions  */
  4060.  
  4061. int
  4062. cmfdb(fdbin) struct FDB * fdbin; {
  4063. #ifndef NOSPL
  4064.     extern int x_ifnum;                 /* IF NUMERIC - disables warnings */
  4065. #endif /* NOSPL */
  4066.     struct FDB * in = fdbin;
  4067.     struct OFDB * out = &cmresult;
  4068.     int x = 0, n;
  4069.     char *s, *xp, *m = NULL;
  4070.     int errbits = 0;
  4071.  
  4072.     xp = bp;
  4073.  
  4074.     out->fcode = -1;            /* Initialize output struct */
  4075.     out->fdbaddr = NULL;
  4076.     out->sresult = NULL;
  4077.     out->nresult = 0;
  4078. /*
  4079.   Currently we make one trip through the FDBs.  So if the user types Esc or
  4080.   Tab at the beginning of a field, only the first FDB is examined for a
  4081.   default.  If the user types ?, help is given only for one FDB.  We should
  4082.   search through the FDBs for all matching possibilities -- and in particular
  4083.   display the pertinent context-sensitive help for each function, rather than
  4084.   the only the first one that works, and then rewind the FDB pointer so we
  4085.   are not locked out of the earlier ones.
  4086. */
  4087.     cmfldflgs = 0;
  4088.     while (1) {                /* Loop through the chain of FDBs */
  4089.     nomsg = 1;
  4090.     xcmfdb = 1;
  4091.     s = NULL;
  4092.     n = 0;
  4093.     debug(F101,"cmfdb in->fcode","",in->fcode);
  4094.     switch (in->fcode) {        /* Current parsing function code */
  4095.       case _CMNUM:
  4096. #ifndef NOSPL
  4097.             x_ifnum = 1;                /* Disables warning messages */
  4098. #endif /* NOSPL */
  4099.         x = cmnum(in->hlpmsg,in->dflt,10,&n,in->spf);
  4100. #ifndef NOSPL
  4101.             x_ifnum = 0;
  4102. #endif /* NOSPL */
  4103.         debug(F101,"cmfdb cmnum","",x);
  4104.         if (x < 0) errbits |= 1;
  4105.         break;
  4106.       case _CMOFI:
  4107.         x = cmofi(in->hlpmsg,in->dflt,&s,in->spf);
  4108.         debug(F101,"cmfdb cmofi","",x);
  4109.         if (x < 0) errbits |= 2;
  4110.         break;
  4111.       case _CMIFI:
  4112.         x = cmifi2(in->hlpmsg,
  4113.                in->dflt,
  4114.                &s,
  4115.                &n,
  4116.                in->ndata1,
  4117.                in->sdata,
  4118.                in->spf,
  4119.                in->ndata2
  4120.                );
  4121.         debug(F101,"cmfdb cmifi2 x","",x);
  4122.         debug(F101,"cmfdb cmifi2 n","",n);
  4123.         if (x < 0) errbits |= 4;
  4124.         break;
  4125.       case _CMFLD:
  4126.         cmfldflgs = in->ndata1;
  4127.         x = cmfld(in->hlpmsg,in->dflt,&s,in->spf);
  4128.         debug(F101,"cmfdb cmfld","",x);
  4129.         if (x < 0) errbits |= 8;
  4130.         break;
  4131.       case _CMTXT:
  4132.         x = cmtxt(in->hlpmsg,in->dflt,&s,in->spf);
  4133.         debug(F101,"cmfdb cmtxt","",x);
  4134.         if (x < 0) errbits |= 16;
  4135.         break;
  4136.       case _CMKEY:
  4137.         x = cmkey2(in->kwdtbl,
  4138.                in->ndata1,
  4139.                in->hlpmsg,in->dflt,in->sdata,in->spf,in->ndata2);
  4140.         debug(F101,"cmfdb cmkey","",x);
  4141.         if (x < 0) errbits |= ((in->ndata2 & 4) ? 32 : 64);
  4142.         break;
  4143.       case _CMCFM:
  4144.         x = cmcfm();
  4145.         debug(F101,"cmfdb cmcfm","",x);
  4146.         if (x < 0) errbits |= 128;
  4147.         break;
  4148.       default:
  4149.         debug(F101,"cmfdb - unexpected function code","",in->fcode);
  4150.         printf("?cmfdb - unexpected function code: %d\n",in->fcode);
  4151.     }
  4152.     debug(F101,"cmfdb x","",x);
  4153.     debug(F101,"cmfdb cmflgs","",cmflgs);
  4154.     debug(F101,"cmfdb crflag","",crflag);
  4155.     debug(F101,"cmfdb qmflag","",qmflag);
  4156.     debug(F101,"cmfdb esflag","",esflag);
  4157.  
  4158.     if (x > -1) {            /* Success */
  4159.         out->fcode = in->fcode;    /* Fill in output struct */
  4160.         out->fdbaddr = in;
  4161.         out->sresult = s;
  4162.         out->nresult = (in->fcode == _CMKEY) ? x : n;
  4163.         out->kflags = (in->fcode == _CMKEY) ? cmkwflgs : 0;
  4164.         debug(F101,"cmfdb out->nresult","",out->nresult);
  4165.         nomsg = 0;
  4166.         xcmfdb = 0;
  4167.         debug(F111,"cmfdb cmdbuf & crflag",cmdbuf,crflag);
  4168.         if (crflag) {
  4169.         cmflgs = 1;
  4170. #ifdef CK_RECALL
  4171.         debug(F101,"cmfdb code","",in->fcode);
  4172.         if (in->fcode != _CMCFM)
  4173.           addcmd(cmdbuf);
  4174. #endif /* CK_RECALL */
  4175.         }
  4176.         return(x);            /* and return */
  4177.     }
  4178.     in = in->nxtfdb;        /* Failed, get next parsing function */
  4179.     nomsg = 0;
  4180.     xcmfdb = 0;
  4181.     if (!in) {            /* No more */
  4182.         debug(F101,"cmfdb failure x","",x);
  4183.         debug(F101,"cmfdb failure errbits","",errbits);
  4184.         if (x == -6)
  4185.           x = -9;
  4186.         if (x == -9) {
  4187.         /* Make informative messages for a few common cases */
  4188.         switch (errbits) {
  4189.           case 4+32: m = "Does not match filename or switch"; break;
  4190.           case 4+64: m = "Does not match filename or keyword"; break;
  4191.           case 1+32: m = "Not a number or valid keyword"; break;
  4192.           case 1+64: m = "Not a number or valid switch"; break;
  4193.           default: m = "Not valid in this position";
  4194.         }
  4195.         printf("?%s: \"%s\"\n",m, atmbuf);
  4196.         }
  4197.         return(x);
  4198.     }
  4199.     if (x != -2 && x != -6 && x != -9 && x != -3) /* Editing or somesuch */
  4200.       return(x);            /* Go back and reparse */
  4201.     pp = np = bp = xp;        /* Back up pointers */
  4202.     cmflgs = -1;            /* Force a reparse */
  4203.  
  4204.  
  4205. #ifndef NOSPL
  4206.     if (!askflag) {            /* If not executing ASK-class cmd... */
  4207. #endif /* NOSPL */
  4208.         if (crflag) {        /* If CR was typed, put it back */
  4209.         pushc = LF;        /* But as a linefeed */
  4210.         } else if (qmflag) {    /* Ditto for Question mark */
  4211.         pushc = '?';
  4212.         } else if (esflag) {    /* and Escape or Tab */
  4213.         pushc = ESC;
  4214.         }
  4215. #ifndef NOSPL
  4216.     }
  4217. #endif /* NOSPL */
  4218.     }
  4219. }
  4220.  
  4221. #ifdef OLDHELP
  4222. /* Keyword help routines */
  4223.  
  4224. /*  C L R H L P -- Initialize/Clear the help line buffer  */
  4225.  
  4226. static VOID
  4227. clrhlp() {                              /* Clear the help buffer */
  4228.     hlpbuf[0] = NUL;
  4229.     hh = hx = 0;
  4230. }
  4231.  
  4232.  
  4233. /*  A D D H L P  --  Add a string to the help line buffer  */
  4234.  
  4235. static VOID
  4236. addhlp(s) char *s; {                    /* Add a word to the help buffer */
  4237.     int j;
  4238.  
  4239.     hh++;                               /* Count this column */
  4240.  
  4241.     for (j = 0; (j < hc) && (*s != NUL); j++) { /* Fill the column */
  4242.         hlpbuf[hx++] = *s++;
  4243.     }
  4244.     if (*s != NUL)                      /* Still some chars left in string? */
  4245.         hlpbuf[hx-1] = '+';             /* Mark as too long for column. */
  4246.  
  4247.     if (hh < (hw / hc)) {               /* Pad col with spaces if necessary */
  4248.         for (; j < hc; j++) {
  4249.             hlpbuf[hx++] = SP;
  4250.         }
  4251.     } else {                            /* If last column, */
  4252.         hlpbuf[hx++] = NUL;             /* no spaces. */
  4253.         dmphlp();                       /* Print it. */
  4254.         return;
  4255.     }
  4256. }
  4257.  
  4258. /*  D M P H L P  --  Dump the help line buffer  */
  4259.  
  4260. static VOID
  4261. dmphlp() {                              /* Print the help buffer */
  4262.     hlpbuf[hx++] = NUL;
  4263.     if ( hlpbuf[0] )
  4264.        printf(" %s\n",hlpbuf);
  4265.     clrhlp();
  4266. }
  4267. #endif /* OLDHELP */
  4268.  
  4269. /*  G T W O R D  --  Gets a "word" from the command input stream  */
  4270.  
  4271. /*
  4272. Usage: retcode = gtword(brk);
  4273.   brk = 0 for normal word breaks (space, CR, Esc, ?)
  4274.   brk = 1 to add / : = (for parsing switches)
  4275.  
  4276. Returns:
  4277. -10 Timelimit set and timed out
  4278.  -9 if input was too long
  4279.  -4 if end of file (e.g. pipe broken)
  4280.  -3 if null field
  4281.  -2 if command buffer overflows
  4282.  -1 if user did some deleting
  4283.   0 if word terminates with SP or tab
  4284.   1 if ... CR
  4285.   2 if ... ESC
  4286.   3 if ... ? (question mark)
  4287.   4 if ... : or = and called with brk != 0
  4288.  
  4289. With:
  4290.   pp pointing to beginning of word in buffer
  4291.   bp pointing to after current position
  4292.   atmbuf containing a copy of the word
  4293.   cc containing the number of characters in the word copied to atmbuf
  4294. */
  4295.  
  4296. int
  4297. ungword() {                /* Unget a word */
  4298.     debug(F101,"ungword cmflgs","",cmflgs);
  4299.     if (ungw) return(0);
  4300.     cmfsav = cmflgs;
  4301.     ungw = 1;
  4302.     cmflgs = 0;
  4303.     return(0);
  4304. }
  4305.  
  4306. /* Un-un-get word.  Undo ungword() if it has been done. */
  4307.  
  4308. VOID
  4309. unungw() {
  4310.     debug(F110,"unungw atmbuf",atmbuf,cmflgs);
  4311.     if (ungw) {
  4312.     ungw = 0;
  4313.     cmflgs = cmfsav;
  4314.     atmbuf[0] = NUL;
  4315.     }
  4316. }
  4317.  
  4318. static int
  4319. gtword(brk) int brk; {
  4320.     int c;                              /* Current char */
  4321.     int quote = 0;                      /* Flag for quote character */
  4322.     int echof = 0;                      /* Flag for whether to echo */
  4323.     int comment = 0;            /* Flag for in comment */
  4324.     char *cp = NULL;            /* Comment pointer */
  4325.     int eintr = 0;
  4326.     int bracelvl = 0;            /* nested brace counter [jrs] */
  4327.     int iscontd = 0;
  4328.     char lastchar = NUL;
  4329.     char prevchar = NUL;
  4330.     char lbrace, rbrace;
  4331.  
  4332. #ifdef RTU
  4333.     extern int rtu_bug;
  4334. #endif /* RTU */
  4335.  
  4336. #ifdef IKSD
  4337.     extern int inserver;
  4338. #endif /* IKSD */
  4339. #ifdef CK_LOGIN
  4340.     extern int x_logged;
  4341. #endif /* CK_LOGIN */
  4342.  
  4343.     extern int kstartactive;
  4344.  
  4345. #ifdef datageneral
  4346.     extern int termtype;                /* DG terminal type flag */
  4347.     extern int con_reads_mt;            /* Console read asynch is active */
  4348.     if (con_reads_mt) connoi_mt();      /* Task would interfere w/cons read */
  4349. #endif /* datageneral */
  4350.  
  4351.     if (cmfldflgs & 1) {
  4352.     lbrace = '(';
  4353.     rbrace = ')';
  4354.     } else {
  4355.     lbrace = '{';
  4356.     rbrace = '}';
  4357.     }
  4358.     crflag = 0;
  4359.     qmflag = 0;
  4360.     esflag = 0;
  4361.  
  4362.     if (swarg) {            /* No leading space for switch args */
  4363.     inword = 1;
  4364.     swarg = 0;
  4365.     }
  4366.     debug(F000,"gtword brkchar","",brkchar);
  4367.     debug(F101,"gtword brk","",brk);
  4368.     if (ungw) {                /* Have a word saved? */
  4369.     int x;
  4370.     debug(F110,"gtword ungetting from pp",pp,0);
  4371.     while (*pp++ == SP) ;
  4372.     if (setatm(pp,2) < 0) {
  4373.         printf("?Saved word too long\n");
  4374.         return(-9);
  4375.     }
  4376.     strncpy(atmbuf,pp,ATMBL);
  4377.     atmbuf[ATMBL] = NUL;
  4378.     x = strlen(atmbuf);
  4379.     while (x > 0 && atmbuf[x-1] == SP) /* Trim trailing spaces */
  4380.       atmbuf[--x] = NUL;
  4381.     ungw = 0;
  4382.     cmflgs = cmfsav;
  4383.     debug(F111,"gtword returning atmbuf",atmbuf,cmflgs);
  4384.     return(cmflgs);
  4385.     }
  4386.     pp = np;                            /* Start of current field */
  4387.  
  4388.     debug(F110,"gtword cmdbuf",cmdbuf,0);
  4389.     debug(F110,"gtword bp",bp,0);
  4390.     debug(F110,"gtword pp",pp,0);
  4391.  
  4392.     while (bp < cmdbuf+CMDBL) {         /* Big get-a-character loop */
  4393.     echof = 0;            /* Assume we don't echo because */
  4394.     chsrc = 0;            /* character came from reparse buf. */
  4395. #ifdef BS_DIRSEP
  4396. CMDIRPARSE:
  4397. #endif /* BS_DIRSEP */
  4398.     c = *bp;
  4399.         if (!c) {            /* If no char waiting in reparse buf */
  4400.         if (dpx && (!pushc
  4401. #ifndef NOSPL
  4402.             || askflag
  4403. #endif /* NOSPL */
  4404.             )) /* get from tty, set echo flag */
  4405.           echof = 1;
  4406.         debug(F101,"gtword timelimit","",timelimit);
  4407.         c = cmdgetc(timelimit);    /* Read a command character. */
  4408.         debug(F101,"gtword c","",c);
  4409.         debug(F111,"gtword dpx,echof",ckitoa(dpx),echof);
  4410.         if (timelimit && c < -1) {    /* Timed out */
  4411.         return(-10);
  4412.         }
  4413.  
  4414. #ifndef NOXFER
  4415. /*
  4416.   The following allows packet recognition in the command parser.
  4417.   Presently it works only for Kermit packets, and if our current protocol
  4418.   happens to be anything besides Kermit, we simply force it to Kermit.
  4419.   We don't use the APC mechanism here for mechanical reasons, and also
  4420.   because this way, it works even with minimally configured interactive
  4421.   versions.  Add Zmodem later...
  4422. */
  4423. #ifdef CK_AUTODL
  4424.         if (!local && cmdadl    /* Autodownload enabled? */
  4425. #ifdef IKS_OPTION
  4426.         || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  4427. #endif /* IKS_OPTION */
  4428.         ) {
  4429.         int k;
  4430.         k = kstart((CHAR)c);    /* Kermit S or I packet? */
  4431.         if (k) {
  4432.             int ksign = 0;
  4433.             if (k < 0) {    /* Minus-Protocol? */
  4434. #ifdef NOSERVER
  4435.             goto noserver;    /* Need server mode for this */
  4436. #else
  4437.             ksign = 1;    /* Remember */
  4438.             k = 0 - k;    /* Convert to actual protocol */
  4439.             justone = 1;    /* Flag for protocol module */
  4440. #endif /* NOSERVER */
  4441.             } else
  4442.               justone = 0;
  4443.             k--;        /* Adjust kstart's return value */
  4444.             if (k == PROTO_K) {
  4445.             extern int protocol, g_proto;
  4446.             extern CHAR sstate;
  4447.             g_proto = protocol;
  4448.             protocol = PROTO_K; /* Crude... */
  4449.             sstate = ksign ? 'x' : 'v';
  4450.             cmdbuf[0] = NUL;
  4451.             return(-3);
  4452.             }
  4453.         }
  4454.         }
  4455. #ifdef NOSERVER
  4456.       noserver:
  4457. #endif /* NOSERVER */
  4458. #endif /* CK_AUTODL */
  4459. #endif /* NOXFER */
  4460.  
  4461.         chsrc = 1;            /* Remember character source is tty. */
  4462.         brkchar = c;
  4463.  
  4464. #ifdef IKSD
  4465.             if (inserver && c < 0) {    /* End of session? */
  4466.                 debug(F111,"gtword c < 0","exiting",c);
  4467.                 return(-4);             /* Cleanup and terminate */
  4468.             }
  4469. #endif /* IKSD */
  4470.  
  4471. #ifdef OS2
  4472.            if (c < 0) {            /* Error */
  4473.            if (c == -3) {        /* Empty word? */
  4474.            if (blocklvl > 0)    /* In a block */
  4475.              continue;        /* so keep looking for block end */
  4476.            else
  4477.              return(-3);    /* Otherwise say we got nothing */
  4478.            } else {            /* Not empty word */
  4479.            return(-4);        /* So some kind of i/o error */
  4480.            }
  4481.            }
  4482. #else
  4483. #ifdef MAC
  4484.        if (c == -3)            /* Empty word... */
  4485.          if (blocklvl > 0)
  4486.            continue;
  4487.          else
  4488.            return(-3);
  4489. #endif /* MAC */
  4490. #endif /* OS2 */
  4491.        if (c == EOF) {        /* This can happen if stdin not tty. */
  4492. #ifdef EINTR
  4493. /*
  4494.   Some operating and/or C runtime systems return EINTR for no good reason,
  4495.   when the end of the standard input "file" is encountered.  In cases like
  4496.   this, we get into an infinite loop; hence the eintr counter, which is reset
  4497.   to 0 upon each call to this routine.
  4498. */
  4499.         debug(F101,"gtword EOF","",errno);
  4500.         if (errno == EINTR && ++eintr < 4) /* When bg'd process is */
  4501.           continue;        /* fg'd again. */
  4502. #endif /* EINTR */
  4503.         return(-4);
  4504.         }
  4505.         c &= cmdmsk;        /* Strip any parity bit */
  4506.     }                /* if desired. */
  4507.  
  4508. /* Now we have the next character */
  4509.  
  4510.     debug(F000,"gtword char","",c);
  4511.  
  4512.     if (quote && (c == CR || c == LF)) { /* Enter key following quote */
  4513.         *bp++ = CMDQ;        /* Double it */
  4514.         *bp = NUL;
  4515.         quote = 0;
  4516.     }
  4517.         if (quote == 0) {        /* If this is not a quoted character */
  4518.             if (c == CMDQ) {        /* Got the quote character itself */
  4519.         if (!comment && quoting)
  4520.           quote = 1;        /* Flag it if not in a comment */
  4521.             }
  4522.         if (c == FF) {        /* Formfeed. */
  4523.                 c = NL;                 /* Replace with newline */
  4524.         cmdclrscn();        /* Clear the screen */
  4525.             }
  4526.         if (c == HT) {        /* Tab */
  4527.         if (comment)        /* If in comment, */
  4528.           c = SP;        /* substitute space */
  4529.         else            /* otherwise */
  4530.           c = ESC;        /* substitute ESC (for completion) */
  4531.         }
  4532.         if (c == ';' || c == '#') { /* Trailing comment */
  4533.         if (inword == 0 && quoting) { /* If not in a word */
  4534.             comment = 1;    /* start a comment. */
  4535.             cp = bp;        /* remember where it starts. */
  4536.         }
  4537.         }
  4538.         if (!kstartactive &&    /* Not in possible Kermit packet */
  4539.         !comment && c == SP) {    /* Space not in comment */
  4540.                 *bp++ = (char) c;    /* deposit in buffer if not already */
  4541.         debug(F101,"gtword echof 2","",echof);
  4542. #ifdef BEBOX
  4543.                 if (echof) {
  4544.                     putchar(c);        /* echo it. */
  4545.                     fflush(stdout);
  4546.                     fflush(stderr);
  4547.                 }
  4548. #else
  4549.                 if (echof) {        /* echo it. */
  4550.             putchar((CHAR)c);
  4551.             if (timelimit)
  4552.               fflush(stdout);
  4553.         }
  4554. #endif /* BEBOX */
  4555.                 if (inword == 0) {      /* If leading, gobble it. */
  4556.                     pp++;
  4557.                     continue;
  4558.                 } else {                /* If terminating, return. */
  4559.             if ((*pp != lbrace) || (bracelvl == 0)) {
  4560.             np = bp;
  4561.             cmbptr = np;
  4562.             if (setatm(pp,0) < 0) {
  4563.                 printf("?Field too long error 1\n");
  4564.                 debug(F111,"gtword too long #1",pp,strlen(pp));
  4565.                 return(-9);
  4566.             }
  4567.             brkchar = c;
  4568.             inword = cmflgs = 0;
  4569.             return(0);
  4570.             }
  4571.                     continue;
  4572.                 }
  4573.             }
  4574.         if (!kstartactive && !comment && brk && (c == '=' || c == ':')) {
  4575.                 *bp++ = (char) c;
  4576. #ifdef BEBOX
  4577.                 if (echof) {
  4578.                     putchar(c);        /* echo it. */
  4579.                     fflush(stdout);
  4580.                     fflush(stderr);
  4581.                 }
  4582. #else
  4583.         if (echof) {
  4584.             putchar((CHAR)c);
  4585.             if (timelimit)
  4586.               fflush(stdout);
  4587.         }
  4588. #endif /* BEBOX */
  4589.         if ((*pp != lbrace) || (bracelvl == 0)) {
  4590.             np = bp;
  4591.             cmbptr = np;
  4592.             if (setatm(pp,0) < 0) {
  4593.             printf("?Field too long error 1\n");
  4594.             debug(F111,"gtword too long #1",pp,strlen(pp));
  4595.             return(-9);
  4596.             }
  4597.             inword = cmflgs = 0;
  4598.             brkchar = c;
  4599.             return(4);
  4600.         }
  4601.             }
  4602.             if (c == lbrace)
  4603.               bracelvl++;
  4604.             if (c == rbrace) {
  4605.                 bracelvl--;
  4606.                 if (linebegin)
  4607.           blocklvl--;
  4608.             }
  4609.             if (c == LF || c == CR) {    /* CR or LF. */
  4610.         if (echof) {
  4611.                     cmdnewl((char)c);    /* echo it. */
  4612. #ifdef BEBOX
  4613.                     fflush(stdout);
  4614.                     fflush(stderr);
  4615. #endif /* BEBOX */
  4616.                 }
  4617.         {
  4618.             /* Trim trailing comment and whitespace */
  4619.             char *qq;
  4620.             if (comment) {    /* Erase comment */
  4621.             while (bp >= cp) /* Back to comment pointer */
  4622.               *bp-- = NUL;
  4623.             bp++;
  4624.             pp = bp;    /* Adjust other pointers */
  4625.             inword = 0;    /* and flags */
  4626.             comment = 0;
  4627.             cp = NULL;
  4628.             }
  4629.             qq = inword ? pp : (char *)cmdbuf;
  4630.             /* Erase trailing whitespace */
  4631.             while (bp > qq && (*(bp-1) == SP || *(bp-1) == HT)) {
  4632.             bp--;
  4633.             debug(F000,"erasing","",*bp);
  4634.             *bp = NUL;
  4635.             }
  4636.             lastchar = (bp > qq) ? *(bp-1) : NUL;
  4637.             prevchar = (bp > qq+1) ? *(bp-2) : NUL;
  4638.         }
  4639.         if (linebegin && blocklvl > 0) /* Blank line in {...} block */
  4640.           continue;
  4641.  
  4642.         linebegin = 1;        /* At beginning of next line */
  4643.         iscontd = prevchar != CMDQ &&
  4644.           (lastchar == '-' || lastchar == lbrace);
  4645.         debug(F101,"gtword iscontd","",iscontd);
  4646.  
  4647.                 if (iscontd) {        /* If line is continued... */
  4648.                     if (chsrc) {    /* If reading from tty, */
  4649.                         if (*(bp-1) == lbrace) { /* Check for "begin block" */
  4650.                             *bp++ = SP;    /* Insert a space for neatness */
  4651.                             blocklvl++;    /* Count block nesting level */
  4652.                         } else {    /* Or hyphen */
  4653.                 bp--;    /* Overwrite the hyphen */
  4654.                         }
  4655.                         *bp = NUL;    /* erase the dash, */
  4656.                         continue;    /* and go back for next char now. */
  4657.                     }
  4658.         } else if (blocklvl > 0) { /* No continuation character */
  4659.             if (chsrc) {    /* But we're in a "block" */
  4660.             *bp++ = ',';    /* Add comma */
  4661.             *bp = NUL;
  4662.             continue;
  4663.             }
  4664.         } else {        /* No continuation, end of command. */
  4665.             *bp = NUL;        /* Terminate the command string. */
  4666.             if (comment) {    /* If we're in a comment, */
  4667.             comment = 0;    /* Say we're not any more, */
  4668.             *cp = NUL;    /* cut it off. */
  4669.             }
  4670.             np = bp;        /* Where to start next field. */
  4671.             cmbptr = np;
  4672.             if (setatm(pp,0) < 0) { /* Copy field to atom buffer */
  4673.             debug(F111,"gtword too long #2",pp,strlen(pp));
  4674.             printf("?Field too long error 2\n");
  4675.             return(-9);
  4676.             }
  4677.             inword = 0;        /* Not in a word any more. */
  4678.             crflag = 1;
  4679.                     debug(F110,"gtword","crflag is set",0);
  4680.             return(cmflgs = 1);
  4681.         }
  4682.             }
  4683.  
  4684.         /* Question mark */
  4685.  
  4686.             if ((c == '?')
  4687.         && quoting
  4688.         && !kstartactive
  4689.         && !comment
  4690.         && cmdsrc() == 0    /* Commands coming from terminal */
  4691.         && chsrc != 0        /* and NOT from reparse buffer! */
  4692.         ) {
  4693.                 putchar((CHAR)c);
  4694.                 *bp = NUL;
  4695.                 if (setatm(pp,0) < 0) {
  4696.             debug(F111,"gtword too long #3",pp,strlen(pp));
  4697.             printf("?Too long #3\n");
  4698.             return(-9);
  4699.         }
  4700.         qmflag = 1;
  4701.                 return(cmflgs = 3);
  4702.             }
  4703.             if (c == ESC) {        /* ESC */
  4704.         if (!comment) {
  4705.             *bp = NUL;
  4706.             if (setatm(pp,0) < 0) {
  4707.             debug(F111,"gtword too long #4",pp,strlen(pp));
  4708.             printf("?Too long #4\n");
  4709.             return(-9);
  4710.             }
  4711.             esflag = 1;
  4712.             return(cmflgs = 2);
  4713.         } else {
  4714.             bleep(BP_WARN);
  4715.             continue;
  4716.         }
  4717.             }
  4718.             if (c == BS || c == RUB) {  /* Character deletion */
  4719.                 if (bp > cmdbuf) {      /* If still in buffer... */
  4720.             cmdchardel();    /* erase it. */
  4721.                     bp--;               /* point behind it, */
  4722. #ifdef COMMENT
  4723.                     if (*bp == SP) inword = 0; /* Flag if current field gone */
  4724. #else
  4725. /* fixed by Ulli Schlueter */
  4726.                     if (*bp == lbrace) bracelvl--; /* Adjust brace count */
  4727.                     if (*bp == rbrace) bracelvl++;
  4728.                     if ((*bp == SP) &&       /* Flag if current field gone */
  4729.             (*pp != lbrace || bracelvl == 0))
  4730.               inword = 0;
  4731. #endif /* COMMENT */
  4732.                     *bp = NUL;          /* Erase character from buffer. */
  4733.                 } else {                /* Otherwise, */
  4734.             bleep(BP_WARN);
  4735.                     cmres();            /* and start parsing a new command. */
  4736.             *bp = *atmbuf = NUL;
  4737.                 }
  4738.                 if (pp < bp) continue;
  4739.                 else return(cmflgs = -1);
  4740.             }
  4741.             if (c == LDEL) {            /* ^U, line deletion */
  4742.                 while ((bp--) > cmdbuf) {
  4743.                     cmdchardel();
  4744.                     *bp = NUL;
  4745.                 }
  4746.                 cmres();                /* Restart the command. */
  4747.         *bp = *atmbuf = NUL;
  4748.                 inword = 0;
  4749.                 return(cmflgs = -1);
  4750.             }
  4751.             if (c == WDEL) {            /* ^W, word deletion */
  4752.                 if (bp <= cmdbuf) {     /* Beep if nothing to delete */
  4753.             bleep(BP_WARN);
  4754.                     cmres();
  4755.             *bp = *atmbuf = NUL;
  4756.                     return(cmflgs = -1);
  4757.                 }
  4758.                 bp--;
  4759.         /* Back up over any trailing nonalphanums */
  4760.         /* This is dependent on ASCII collating sequence */
  4761.         /* but isalphanum() is not available everywhere. */
  4762.                 for ( ; (bp >= cmdbuf) &&
  4763.              (*bp < '0') ||
  4764.              (*bp > '9' && *bp < '@') ||
  4765.              (*bp > 'Z' && *bp < 'a') ||
  4766.              (*bp > 'z') ; bp--) {
  4767.                     cmdchardel();
  4768.                     *bp = NUL;
  4769.                 }
  4770.         /* Now delete back to rightmost remaining nonalphanum */
  4771.                 for ( ; (bp >= cmdbuf) && (*bp) ; bp--) {
  4772.             if ((*bp < '0') ||
  4773.             (*bp > '9' && *bp < '@') ||
  4774.             (*bp > 'Z' && *bp < 'a') ||
  4775.             (*bp > 'z'))
  4776.               break;
  4777.                     cmdchardel();
  4778.                     *bp = NUL;
  4779.                 }
  4780.                 bp++;
  4781.                 inword = 0;
  4782.                 return(cmflgs = -1);
  4783.             }
  4784.             if (c == RDIS) {            /* ^R, redisplay */
  4785. #ifdef COMMENT
  4786.                 *bp = NUL;
  4787.                 printf("\n%s%s",cmprom,cmdbuf);
  4788. #else
  4789.         char *cpx; char cx;
  4790.                 *bp = NUL;
  4791.                 printf("\n%s",cmprom);
  4792.         cpx = cmdbuf;
  4793.         while (cx = *cpx++) {
  4794. #ifdef isprint
  4795.             putchar((CHAR) (isprint(cx) ? cx : '^'));
  4796. #else
  4797.             putchar((CHAR) ((cx >= SP && cx < DEL) ? cx : '^'));
  4798. #endif /* isprint */
  4799.         }
  4800. #endif /* COMMENT */
  4801.         fflush(stdout);
  4802.                 continue;
  4803.             }
  4804. #ifdef CK_RECALL
  4805.         if (chsrc && on_recall &&    /* Reading commands from keyboard? */
  4806.         (cm_recall > 0) &&    /* Saving commands? */
  4807.         (c == C_UP || c == C_UP2)) { /* Go up one */
  4808.         if (current < 0) {    /* Nowhere to go, */
  4809.             bleep(BP_WARN);
  4810.             continue;
  4811.         }
  4812.         if (recall[current]) {
  4813.             if (!strcmp(recall[current],cmdbuf)) {
  4814.             if (current > 0) {
  4815.                 current--;
  4816.             } else {
  4817.                 bleep(BP_WARN);
  4818.                 continue;
  4819.             }
  4820.             }
  4821.         }
  4822.         if (recall[current]) { /* We have a previous command */
  4823.             while ((bp--) > cmdbuf) { /* Erase current line */
  4824.             cmdchardel();
  4825.             *bp = NUL;
  4826.             }
  4827.             ckstrncpy(cmdbuf,recall[current],CMDBL);
  4828. #ifdef OSK
  4829.             fflush(stdout);
  4830.             write(fileno(stdout), "\r", 1);
  4831.             printf("%s%s",cmprom,cmdbuf);
  4832. #else
  4833.             printf("\r%s%s",cmprom,cmdbuf);
  4834. #endif /* OSK */
  4835.             current--;
  4836.         }
  4837.         return(cmflgs = -1);    /* Force a reparse */
  4838.         }
  4839.         if (chsrc && on_recall &&    /* Reading commands from keyboard? */
  4840.         (cm_recall > 0) &&    /* Saving commands? */
  4841.         (c == C_DN)) {        /* Down one */
  4842.         if (current + 1 > rlast) { /* Already at bottom, just beep */
  4843.             bleep(BP_WARN);
  4844.             continue;
  4845.         }
  4846.         current++;        /* OK to go down */
  4847.         if (recall[current]) {
  4848.             if (!strcmp(recall[current],cmdbuf)) {
  4849.             if (current + 1 > rlast) { /* At bottom, beep */
  4850.                 bleep(BP_WARN);
  4851.                 continue;
  4852.             } else
  4853.               current++;
  4854.             }
  4855.         }
  4856.         if (recall[current]) {
  4857.             while ((bp--) > cmdbuf) { /* Erase current line */
  4858.             cmdchardel();
  4859.             *bp = NUL;
  4860.             }
  4861.             ckstrncpy(cmdbuf,recall[current],CMDBL);
  4862. #ifdef OSK
  4863.             fflush(stdout);
  4864.             write(fileno(stdout), "\r", 1);
  4865.             printf("%s%s",cmprom,cmdbuf);
  4866. #else
  4867.             printf("\r%s%s",cmprom,cmdbuf);
  4868. #endif /* OSK */
  4869.             return(cmflgs = -1); /* Force reparse */
  4870.         }
  4871.         }
  4872. #endif /* CK_RECALL */
  4873.  
  4874.         if (c < SP && quote == 0) { /* Any other unquoted control char */
  4875.         if (!chsrc) {        /* If cmd file, point past it */
  4876.             bp++;
  4877.         } else {
  4878.             bleep(BP_WARN);
  4879.         }
  4880.         continue;        /* continue, don't put in buffer */
  4881.         }
  4882.         linebegin = 0;        /* Not at beginning of line */
  4883. #ifdef BEBOX
  4884.         if (echof) {
  4885.                 cmdecho((char) c, 0);    /* Echo what was typed. */
  4886.                 fflush (stdout);
  4887.                 fflush(stderr);
  4888.             }
  4889. #else
  4890.             if (echof) cmdecho((char) c, 0); /* Echo what was typed. */
  4891. #endif /* BEBOX */
  4892.         } else {            /* This character was quoted. */
  4893.         int qf = 1;
  4894.         quote = 0;            /* Unset the quote flag. */
  4895.         debug(F000,"gtword quote 0","",c);
  4896.         /* Quote character at this level is only for SP, ?, and controls */
  4897.             /* If anything else was quoted, leave quote in, and let */
  4898.         /* the command-specific parsing routines handle it, e.g. \007 */
  4899.         if (c > 32 && c != '?' && c != RUB && chsrc != 0) {
  4900.         debug(F000,"gtword quote 1","",c);
  4901.         *bp++ = CMDQ;        /* Deposit \ if it came from tty */
  4902.         qf = 0;            /* and don't erase it from screen */
  4903.         linebegin = 0;        /* Not at beginning of line */
  4904. #ifdef BS_DIRSEP
  4905. /*
  4906.   This is a hack to handle "cd \" or "cd foo\" on OS/2 and similar systems.
  4907.   If we were called from cmdir() and the previous character was the quote
  4908.   character, i.e. backslash, and this character is the command terminator,
  4909.   then we stuff an extra backslash into the buffer without echoing, then
  4910.   we stuff the carriage return back in again, and go back and process it,
  4911.   this time with the quote flag off.
  4912. */
  4913.         } else if (dirnamflg && (c == CR || c == LF || c == SP)) {
  4914.         debug(F000,"gtword quote 2","",c);
  4915.         *bp++ = CMDQ;
  4916.         linebegin = 0;        /* Not at beginning of line */
  4917.         *bp = (c == SP ? SP : CR);
  4918.         goto CMDIRPARSE ;
  4919. #endif /* BS_DIRSEP */
  4920.         } else {
  4921.         debug(F000,"gtword quote 3","",c);
  4922.         }
  4923. #ifdef BEBOX
  4924.         if (echof) {
  4925.                 cmdecho((char) c, qf);    /* Echo what was typed. */
  4926.                 fflush (stdout);
  4927.                 fflush(stderr);
  4928.             }
  4929. #else
  4930.         if (echof) cmdecho((char) c, qf); /* Now echo quoted character */
  4931. #endif /* BEBOX */
  4932.         debug(F111,"gtword quote",cmdbuf,c);
  4933.     }
  4934. #ifdef COMMENT
  4935.         if (echof) cmdecho((char) c,quote); /* Echo what was typed. */
  4936. #endif /* COMMENT */
  4937.         if (!comment) inword = 1;    /* Flag we're in a word. */
  4938.     if (quote) continue;        /* Don't deposit quote character. */
  4939.         if (c != NL) {            /* Deposit command character. */
  4940.         *bp++ = (char) c;        /* and make sure there is a NUL */
  4941. #ifdef COMMENT
  4942.         *bp = NUL;            /* after it */
  4943. #endif /* COMMENT */
  4944.     }
  4945.     }                                   /* End of big while */
  4946.     bleep(BP_WARN);
  4947.     printf("?Command too long, maximum length: %d.\n",CMDBL);
  4948.     cmflgs = -2;
  4949.     return(-9);
  4950. }
  4951.  
  4952. /* Utility functions */
  4953.  
  4954. /* A D D B U F  -- Add the string pointed to by cp to the command buffer  */
  4955.  
  4956. static int
  4957. addbuf(cp) char *cp; {
  4958.     int len = 0;
  4959.     while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) {
  4960.         *bp++ = *cp++;                  /* Copy and */
  4961.         len++;                          /* count the characters. */
  4962.     }
  4963.     *bp++ = SP;                         /* Put a space at the end */
  4964.     *bp = NUL;                          /* Terminate with a null */
  4965.     np = bp;                            /* Update the next-field pointer */
  4966.     cmbptr = np;
  4967.     return(len);                        /* Return the length */
  4968. }
  4969.  
  4970. /*  S E T A T M  --  Deposit a token in the atom buffer.  */
  4971. /*
  4972.   Break on space, newline, carriage return, or NUL.
  4973.   Call with:
  4974.     cp = Pointer to string to copy to atom buffer.
  4975.     fcode = 0 means break on whitespace or EOL.
  4976.     fcode = 1 means don't break on space.
  4977.     fcode = 2 means break on space, ':', or '='.
  4978.   Null-terminate the result.
  4979.   If the source pointer is the atom buffer itself, do nothing.
  4980.   (no longer true.  now we copy the buffer, edit it, and copy it back.)
  4981.   Return length of token, and also set global "cc" to this length.
  4982.   Return -1 if token was too long.
  4983. */
  4984. static int
  4985. setatm(cp,fcode) char *cp; int fcode; {
  4986.     char *ap, *xp;
  4987.     int  bracelvl, n;
  4988.     char lbrace, rbrace;
  4989.  
  4990.     if (cmfldflgs & 1) {
  4991.     lbrace = '(';
  4992.     rbrace = ')';
  4993.     } else {
  4994.     lbrace = '{';
  4995.     rbrace = '}';
  4996.     }
  4997.     cc = 0;                /* Character counter */
  4998.     ap = atmbuf;            /* Address of atom buffer */
  4999.  
  5000.     if ((int) strlen(cp) > ATMBL)
  5001.       return(-1);
  5002.  
  5003.     if (cp == ap) {            /* In case source is atom buffer */
  5004.     xp = atybuf;            /* make a copy */
  5005.     strncpy(xp,ap,ATMBL);        /* so we can copy it back, edited. */
  5006.     cp = xp;
  5007.     }
  5008.     *ap = NUL;                /* Zero the atom buffer */
  5009.     if (fcode == 1) {            /* Trim trailing blanks */
  5010.     n = strlen(cp);
  5011.     while (--n >= 0)
  5012.       if (cp[n] != SP) break;
  5013.     cp[n+1] = NUL;
  5014.     }
  5015.     while (*cp == SP) cp++;        /* Trim leading spaces */
  5016.     bracelvl = 0;
  5017.     while (*cp) {
  5018.         if (*cp == lbrace) bracelvl++;
  5019.         if (*cp == rbrace) bracelvl--;
  5020.     if (bracelvl < 0) bracelvl = 0;
  5021.     if (bracelvl == 0) {
  5022.         if ((*cp == SP || *cp == HT) && (fcode != 1)) break;
  5023.         if ((fcode == 2) && (*cp == '=' || *cp == ':')) break;
  5024.         if (*cp == LF || *cp == CR) break;
  5025.     }
  5026.         *ap++ = *cp++;
  5027.         cc++;
  5028.     }
  5029.     *ap = NUL;                /* Terminate the string. */
  5030.     return(cc);                         /* Return length. */
  5031. }
  5032.  
  5033. /*
  5034.   These functions attempt to hide system dependencies from the mainline
  5035.   code in gtword().  Dummy arg for cmdgetc() needed for compatibility with
  5036.   coninc(), ttinc(), etc, since a pointer to this routine can be passed in
  5037.   place of those to tn_doop().
  5038.  
  5039.   No longer static.  Used by askmore().  Fri Aug 20 15:03:34 1999.
  5040. */
  5041. int
  5042. cmdgetc(timelimit) int timelimit; {    /* Get a character from the tty. */
  5043.     int c;
  5044. #ifdef IKSD
  5045.     extern int inserver;
  5046. #endif /* IKSD */
  5047. #ifdef CK_LOGIN
  5048.     extern int x_logged;
  5049. #endif /* CK_LOGIN */
  5050. #ifdef TNCODE
  5051.     static int got_cr = 0;
  5052.     extern int ckxech;
  5053.     int tx = 0, is_tn = 0;
  5054. #endif /* TNCODE */
  5055.  
  5056.     if (pushc
  5057. #ifndef NOSPL
  5058.     && !askflag
  5059. #endif /* NOSPL */
  5060.     ) {
  5061.         debug(F111,"cmdgetc()","pushc",pushc);
  5062.     c = pushc;
  5063.     pushc = NUL;
  5064.     if (xcmfdb && c == '?')        /* Don't echo ? twice if chaining. */
  5065.       cmdchardel();
  5066.     return(c);
  5067.     }
  5068. #ifdef datageneral
  5069.     {
  5070.     char ch;
  5071.     c = dgncinb(0,&ch,1);        /* -1 is EOF, -2 TO,
  5072.                                          * -c is AOS/VS error */
  5073.     if (c == -2) {            /* timeout was enabled? */
  5074.         resto(channel(0));        /* reset timeouts */
  5075.         c = dgncinb(0,&ch,1);    /* retry this now! */
  5076.     }
  5077.     if (c < 0) return(-4);        /* EOF or some error */
  5078.     else c = (int) ch & 0177;    /* Get char without parity */
  5079. /*    echof = 1; */
  5080.     }
  5081. #else /* Not datageneral */
  5082. #ifndef MINIX2
  5083.     if (
  5084. #ifdef IKSD
  5085.     (!local && inserver) ||
  5086. #endif /* IKSD */
  5087.     timelimit > 0) {
  5088. #ifdef TNCODE
  5089.           GETNEXTCH:
  5090.             is_tn = !pushc && !local && sstelnet;
  5091. #endif /* TNCODE */
  5092. #ifdef COMMENT
  5093.         c = coninc(timelimit > 0 ? 1 : 0);
  5094. #else /* COMMENT */
  5095.         /* This is likely to break the asktimeout... */
  5096.         c = coninc(timelimit);
  5097. #endif /* COMMENT */
  5098.         /* debug(F101,"cmdgetc coninc","",c); */
  5099. #ifdef TNCODE
  5100.             if (c >= 0 && is_tn) {    /* Server-side Telnet */
  5101.                 switch (c) {
  5102.           case IAC:
  5103.                     debug(F111,"gtword IAC","c",c);
  5104.                     got_cr = 0;
  5105.                     if ((tx = tn_doop((CHAR)(c & 0xff),ckxech,coninc)) == 0) {
  5106.                         goto GETNEXTCH;
  5107.                     } else if (tx <= -1) { /* I/O error */
  5108.                         /* If there was a fatal I/O error then ttclos()    */
  5109.                         /* has been called and the next GETNEXTCH attempt  */
  5110.                         /* will be !is_tn since ttclos() sets sstelnet = 0 */
  5111.                         doexit(BAD_EXIT,-1); /* (or return(-4)? */
  5112.                     } else if (tx == 1) { /* ECHO change */
  5113.                         ckxech = dpx = 1; /* Get next char */
  5114.                         goto GETNEXTCH;
  5115.                     } else if (tx == 2) { /* ECHO change */
  5116.                         ckxech = dpx = 0; /* Get next char */
  5117.                         goto GETNEXTCH;
  5118.                     } else if (tx == 3) { /* Quoted IAC */
  5119.                         c = 255;    /* proceeed with it. */
  5120.                     }
  5121. #ifdef IKS_OPTION
  5122.                     else if (tx == 4) {    /* IKS State Change */
  5123.                         goto GETNEXTCH;
  5124.                     }
  5125. #endif /* IKS_OPTION */
  5126.                     else if (tx == 6) {    /* Remote Logout */
  5127.             doexit(GOOD_EXIT,0);
  5128.                     } else {
  5129.             goto GETNEXTCH;    /* Unknown, get next char */
  5130.             }
  5131.                     break;
  5132. #ifdef COMMENT
  5133.                   case CR:
  5134.                     if (!TELOPT_U(TELOPT_BINARY)) {
  5135.             if ( got_cr ) {
  5136.                 /* This means the sender is violating Telnet   */
  5137.                 /* protocol because we received two CRs in a   */
  5138.                 /* row without getting either LF or NUL.       */
  5139.                 /* This will not solve the problem but it      */
  5140.                 /* will at least allow two CRs to do something */
  5141.                 /* whereas before the user would have to guess */
  5142.                 /* to send LF or NUL after the CR.             */
  5143.                 debug(F100,"gtword CR telnet error","",0);
  5144.                 c = LF;
  5145.             } else {
  5146.                 debug(F100,"gtword skipping CR","",0);
  5147.                 got_cr = 1;    /* Remember a CR was received */
  5148.                 goto GETNEXTCH;
  5149.             }
  5150.                     } else {
  5151.             debug(F100,"gtword CR to LF","",0);
  5152.             c = LF;
  5153.                     }
  5154.                     break;
  5155.                   case LF:
  5156.                     if (!TELOPT_U(TELOPT_BINARY)) {
  5157.             got_cr = 0;
  5158.             debug(F100,"gtword LF","",0);
  5159.                     } else {
  5160.             if (got_cr) {
  5161.                 got_cr = 0;
  5162.                 debug(F100,"gtword skipping LF","",0);
  5163.                 goto GETNEXTCH;
  5164.             }
  5165.                     }
  5166.                     break;
  5167.                   case NUL:
  5168.                     if (!TELOPT_U(TELOPT_BINARY) && got_cr) {
  5169.             c = LF;
  5170.             debug(F100,"gtword NUL to LF","",0);
  5171.                     } else {
  5172.             debug(F100,"gtword NUL","",0);
  5173.                     }
  5174.                     got_cr = 0;
  5175.                     break;
  5176. #else /* COMMENT */
  5177.                   case CR:
  5178.                     if ( !TELOPT_U(TELOPT_BINARY) && got_cr ) {
  5179.                         /* This means the sender is violating Telnet   */
  5180.                         /* protocol because we received two CRs in a   */
  5181.                         /* row without getting either LF or NUL.       */
  5182.                         /* This will not solve the problem but it      */
  5183.                         /* will at least allow two CRs to do something */
  5184.                         /* whereas before the user would have to guess */
  5185.                         /* to send LF or NUL after the CR.             */
  5186.                         debug(F100,"gtword CR telnet error","",0);
  5187.                     } else {
  5188.                         got_cr = 1;    /* Remember a CR was received */
  5189.                     }
  5190.                     debug(F100,"gtword CR to LF","",0);
  5191.                     c = LF;
  5192.             break;
  5193.                   case LF:
  5194.                     if (got_cr) {
  5195.                         got_cr = 0;
  5196.                         debug(F100,"gtword skipping LF","",0);
  5197.                         goto GETNEXTCH;
  5198.                     }
  5199.             break;
  5200.                   case NUL:
  5201.                     if (got_cr) {
  5202.                         got_cr = 0;
  5203.                         debug(F100,"gtword skipping NUL","",0);
  5204.                         goto GETNEXTCH;
  5205.                     } else {
  5206.                       debug(F100,"gtword NUL","",0);
  5207.                     }
  5208.                     break;
  5209. #endif /* COMMENT */
  5210. #ifdef IKSD
  5211.           case ETX:        /* Ctrl-C... */
  5212.                   case EOT:        /* EOT = EOF */
  5213.                       debug(F000,"gtword","",c);
  5214.                       if (inserver
  5215. #ifdef CK_LOGIN
  5216.               && !x_logged
  5217. #endif /* CK_LOGIN */
  5218.               )
  5219.                           return(-4);
  5220.             break;
  5221. #endif /* IKSD */
  5222.           default:
  5223.                       got_cr = 0;
  5224.                 }
  5225.             }
  5226. #endif /* TNCODE */
  5227.     } else {
  5228.     /* debug(F100,"cmdgetc getchar 1","",0); */
  5229. #ifdef OS2
  5230.     c = coninc(0);
  5231. #else /* OS2 */
  5232.     c = getchar();
  5233.     /* debug(F101,"cmdgetc getchar 2","",c); */
  5234. #endif /* OS2 */
  5235.     }
  5236. #else  /* MINIX2 */
  5237. #undef getc
  5238.     c = getc(stdin);
  5239. #endif /* MINIX2 */
  5240. #ifdef RTU
  5241.     if (rtu_bug) {
  5242.     c = getchar();            /* RTU doesn't discard the ^Z */
  5243.     rtu_bug = 0;
  5244.     }
  5245. #endif /* RTU */
  5246. #endif /* datageneral */
  5247.     return(c);                /* Return what we got */
  5248. }
  5249.  
  5250. static VOID
  5251. cmdclrscn() {                /* Clear the screen */
  5252.     ck_cls();
  5253. }
  5254.  
  5255. static VOID                /* What to echo at end of command */
  5256. #ifdef CK_ANSIC
  5257. cmdnewl(char c)
  5258. #else
  5259. cmdnewl(c) char c;
  5260. #endif /* CK_ANSIC */
  5261. /* cmdnewl */ {
  5262. #ifdef OS2
  5263. #ifdef IKSD
  5264.     extern int inserver;
  5265.     if (inserver && c == LF)
  5266.       putchar(CR);
  5267. #endif /* IKSD */
  5268. #endif /* OS2 */
  5269.  
  5270.     putchar(c);                /* c is the terminating character */
  5271.  
  5272. #ifdef WINTCP                /* what is this doing here? */
  5273.     if (c == CR) putchar(NL);
  5274. #endif /* WINTCP */
  5275.  
  5276. /*
  5277.   A.A. Chernov, who sent in changes for FreeBSD, said we also needed this
  5278.   for SVORPOSIX because "setup terminal by termios and curses does
  5279.   not convert \r to \n, so additional \n needed in newline function."  But
  5280.   it is also very likely to result in unwanted blank lines.
  5281. */
  5282. #ifdef BSD44
  5283.     if (c == CR) putchar(NL);
  5284. #endif /* BSD44 */
  5285.  
  5286. #ifdef COMMENT
  5287.     /* OS2 no longer needs this as all CR are converted to NL in coninc() */
  5288.     /* This eliminates the ugly extra blank lines discussed above.        */
  5289. #ifdef OS2
  5290.     if (c == CR) putchar(NL);
  5291. #endif /* OS2 */
  5292. #endif /* COMMENT */
  5293. #ifdef aegis
  5294.     if (c == CR) putchar(NL);
  5295. #endif /* aegis */
  5296. #ifdef AMIGA
  5297.     if (c == CR) putchar(NL);
  5298. #endif /* AMIGA */
  5299. #ifdef datageneral
  5300.     if (c == CR) putchar(NL);
  5301. #endif /* datageneral */
  5302. #ifdef GEMDOS
  5303.     if (c == CR) putchar(NL);
  5304. #endif /* GEMDOS */
  5305. #ifdef STRATUS
  5306.     if (c == CR) putchar(NL);
  5307. #endif /* STRATUS */
  5308. }
  5309.  
  5310. static VOID
  5311. cmdchardel() {                /* Erase a character from the screen */
  5312.     if (!dpx) return;
  5313. #ifdef datageneral
  5314.     /* DG '\b' is EM (^y or \031) */
  5315.     if (termtype == 1)
  5316.       /* Erase a character from non-DG screen, */
  5317.       dgncoub(1,"\010 \010",3);
  5318.     else
  5319. #endif /* datageneral */
  5320.       printf("\b \b");
  5321. #ifdef GEMDOS
  5322.     fflush(stdout);
  5323. #else
  5324. #ifdef BEBOX
  5325.     fflush(stdout);
  5326. #endif /* BEBOX */
  5327. #endif /* GEMDOS */
  5328. }
  5329.  
  5330. static VOID
  5331. #ifdef CK_ANSIC
  5332. cmdecho(char c, int quote)
  5333. #else
  5334. cmdecho(c,quote) char c; int quote;
  5335. #endif /* CK_ANSIC */
  5336. { /* cmdecho */
  5337.     if (!dpx) return;
  5338.     /* Echo tty input character c */
  5339.     if (quote) {
  5340.     putchar(BS); putchar(SP); putchar(BS);
  5341. #ifdef isprint
  5342.     putchar((CHAR) (isprint(c) ? c : '^' ));
  5343. #else
  5344.     putchar((CHAR) ((c >= SP && c < DEL) ? c : '^'));
  5345. #endif /* isprint */
  5346.     } else putchar(c);
  5347. #ifdef OS2
  5348.     if (quote==1 && c==CR) putchar((CHAR) NL);
  5349. #endif /* OS2 */
  5350.     if (timelimit)
  5351.       fflush(stdout);
  5352. }
  5353.  
  5354. /* Return pointer to current position in command buffer. */
  5355.  
  5356. char *
  5357. cmpeek() {
  5358.     return(np);
  5359. }
  5360. #endif /* NOICP */
  5361.  
  5362.  
  5363. #ifdef NOICP
  5364. #include "ckcdeb.h"
  5365. #include "ckucmd.h"
  5366. #include "ckcasc.h"
  5367. #endif /* NOICP */
  5368.  
  5369. /*  X X E S C  --  Interprets backslash codes  */
  5370. /*  Returns the int value of the backslash code if it is > -1 and < 256 */
  5371. /*  and updates the string pointer to first character after backslash code. */
  5372. /*  If the argument is invalid, leaves pointer unchanged and returns -1. */
  5373.  
  5374. int
  5375. xxesc(s) char **s; {            /* Expand backslash escapes */
  5376.     int x, y, brace, radix;        /* Returns the int value */
  5377.     char hd = '9';            /* Highest digit in radix */
  5378.     char *p;
  5379.  
  5380.     p = *s;                /* pointer to beginning */
  5381.     if (!p) return(-1);            /* watch out for null pointer */
  5382.     x = *p++;                /* character at beginning */
  5383.     if (x != CMDQ) return(-1);        /* make sure it's a backslash code */
  5384.  
  5385.     x = *p;                /* it is, get the next character */
  5386.     if (x == '{') {            /* bracketed quantity? */
  5387.     p++;                /* begin past bracket */
  5388.     x = *p;
  5389.     brace = 1;
  5390.     } else brace = 0;
  5391.     switch (x) {            /* Start interpreting */
  5392.       case 'd':                /* Decimal radix indicator */
  5393.       case 'D':
  5394.     p++;                /* Just point past it and fall thru */
  5395.       case '0':                /* Starts with digit */
  5396.       case '1':
  5397.       case '2':  case '3':  case '4':  case '5':
  5398.       case '6':  case '7':  case '8':  case '9':
  5399.     radix = 10;            /* Decimal */
  5400.     hd = '9';            /* highest valid digit */
  5401.     break;
  5402.       case 'o':                /* Starts with o or O */
  5403.       case 'O':
  5404.     radix = 8;            /* Octal */
  5405.     hd = '7';            /* highest valid digit */
  5406.     p++;                /* point past radix indicator */
  5407.     break;
  5408.       case 'x':                /* Starts with x or X */
  5409.       case 'X':
  5410.     radix = 16;            /* Hexadecimal */
  5411.     p++;                /* point past radix indicator */
  5412.     break;
  5413.       default:                /* All others */
  5414. #ifdef COMMENT
  5415.     *s = p+1;            /* Treat as quote of next char */
  5416.     return(*p);
  5417. #else
  5418.     return(-1);
  5419. #endif /* COMMENT */
  5420.     }
  5421.     /* For OS/2, there are "wide" characters required for the keyboard
  5422.      * binding, i.e \644 and similar codes larger than 255 (byte).
  5423.      * For this purpose, give up checking for < 256. If someone means
  5424.      * \266 should result in \26 followed by a "6" character, he should
  5425.      * always write \{26}6 anyway.  Now, return only the lower byte of
  5426.      * the result, i.e. 10, but eat up the whole \266 sequence and
  5427.      * put the wide result 266 into a global variable.  Yes, that's not
  5428.      * the most beautiful programming style but requires the least
  5429.      * amount of changes to other routines.
  5430.      */
  5431.     if (radix <= 10) {            /* Number in radix 8 or 10 */
  5432.     for ( x = y = 0;
  5433.            (*p) && (*p >= '0') && (*p <= hd)
  5434. #ifdef OS2
  5435.                    && (y < 5) && (x*radix < KMSIZE);
  5436.               /* the maximum needed value \8196 is 4 digits long */
  5437.               /* while as octal it requires \1377, i.e. 5 digits */
  5438. #else
  5439.                    && (y < 3) && (x*radix < 256);
  5440. #endif /* OS2 */
  5441.           p++,y++) {
  5442.         x = x * radix + (int) *p - 48;
  5443.     }
  5444. #ifdef OS2
  5445.         wideresult = x;            /* Remember wide result */
  5446.         x &= 255;
  5447. #endif /* OS2 */
  5448.     if (y == 0 || x > 255) {    /* No valid digits? */
  5449.         *s = p;            /* point after it */
  5450.         return(-1);            /* return failure. */
  5451.     }
  5452.     } else if (radix == 16) {        /* Special case for hex */
  5453.     if ((x = unhex(*p++)) < 0) { *s = p - 1; return(-1); }
  5454.     if ((y = unhex(*p++)) < 0) { *s = p - 2; return(-1); }
  5455.     x = ((x << 4) & 0xF0) | (y & 0x0F);
  5456. #ifdef OS2
  5457.         wideresult = x;
  5458.         if ((y = unhex(*p)) >= 0) {
  5459.            p++;
  5460.        wideresult = ((x << 4) & 0xFF0) | (y & 0x0F);
  5461.            x = wideresult & 255;
  5462.         }
  5463. #endif /* OS2 */
  5464.     } else x = -1;
  5465.     if (brace && *p == '}' && x > -1)    /* Point past closing brace, if any */
  5466.       p++;
  5467.     *s = p;                /* Point to next char after sequence */
  5468.     return(x);                /* Return value of sequence */
  5469. }
  5470.  
  5471. int                    /* Convert hex string to int */
  5472. #ifdef CK_ANSIC
  5473. unhex(char x)
  5474. #else
  5475. unhex(x) char x;
  5476. #endif /* CK_ANSIC */
  5477. /* unhex */ {
  5478.  
  5479.     if (x >= '0' && x <= '9')        /* 0-9 is offset by hex 30 */
  5480.       return(x - 0x30);
  5481.     else if (x >= 'A' && x <= 'F')    /* A-F offset by hex 37 */
  5482.       return(x - 0x37);
  5483.     else if (x >= 'a' && x <= 'f')    /* a-f offset by hex 57 */
  5484.       return(x - 0x57);            /* (obviously ASCII dependent) */
  5485.     else return(-1);
  5486. }
  5487.  
  5488. /*  L O O K U P  --  Lookup the string in the given array of strings  */
  5489.  
  5490. /*
  5491.  Call this way:  v = lookup(table,word,n,&x);
  5492.  
  5493.    table - a 'struct keytab' table.
  5494.    word  - the target string to look up in the table.
  5495.    n     - the number of elements in the table.
  5496.    x     - address of an integer for returning the table array index,
  5497.            or NULL if you don't need a table index.
  5498.  
  5499.  The keyword table must be arranged in ascending alphabetical order.
  5500.  Alphabetic case doesn't matter.
  5501.  
  5502.  Returns the keyword's associated value (zero or greater) if found,
  5503.  with the variable x set to the keyword-table index, or:
  5504.  
  5505.   -3 if nothing to look up (target was null),
  5506.   -2 if ambiguous,
  5507.   -1 if not found.
  5508.  
  5509.  A match is successful if the target matches a keyword exactly, or if
  5510.  the target is a prefix of exactly one keyword.  It is ambiguous if the
  5511.  target matches two or more keywords from the table.
  5512. */
  5513. int
  5514. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  5515.  
  5516.     int i, v, len, cmdlen;
  5517.  
  5518. /* Get length of search object, if it's null return code -3. */
  5519.  
  5520.     if (!cmd)
  5521.       return(-3);
  5522.     if (((cmdlen = (int) strlen(cmd)) == 0) || (n < 1))
  5523.       return(-3);
  5524.  
  5525. /* Not null, look it up */
  5526.  
  5527.     for (i = 0; i < n-1; i++) {
  5528.     len = strlen(table[i].kwd);
  5529.         if ((len == cmdlen && !ckstrcmp(table[i].kwd,cmd,len,0)) ||
  5530.         ((v = !ckstrcmp(table[i].kwd,cmd,cmdlen,0)) &&
  5531.          ckstrcmp(table[i+1].kwd,cmd,cmdlen,0))) {
  5532.         if (x) *x = i;
  5533.         return(table[i].kwval);
  5534.     }
  5535.         if (v) {            /* Ambiguous */
  5536.         if (x) *x = i;        /* Set index of first match */
  5537.         return(-2);
  5538.     }
  5539.     }
  5540.  
  5541. /* Last (or only) element */
  5542.  
  5543.     if (!ckstrcmp(table[n-1].kwd,cmd,cmdlen,0)) {
  5544.         if (x) *x = n-1;
  5545.         return(table[n-1].kwval);
  5546.     } else return(-1);
  5547. }
  5548.  
  5549. /* Like lookup, but requires a full (but case-independent) match */
  5550.  
  5551. int
  5552. xlookup(table,cmd,n,x) struct keytab table[]; char *cmd; int n, *x; {
  5553.     int i, cmdlen;
  5554.  
  5555.     if (!cmd)
  5556.       return(-3);
  5557.     if (((cmdlen = (int) strlen(cmd)) == 0) || (n < 1))
  5558.       return(-3);
  5559.  
  5560.     for (i = 0; i < n; i++) {
  5561.     if (((int)strlen(table[i].kwd) == cmdlen) &&
  5562.         (!ckstrcmp(table[i].kwd,cmd,cmdlen,0))) {
  5563.         if (x) *x = i;
  5564.         return(table[i].kwval);
  5565.     }
  5566.     }
  5567.     return(-1);
  5568. }
  5569.  
  5570. #ifndef NOICP
  5571. int
  5572. cmdsquo(x) int x; {
  5573.     quoting = x;
  5574.     return(1);
  5575. }
  5576.  
  5577. int
  5578. cmdgquo() {
  5579.     return(quoting);
  5580. }
  5581. #endif /* NOICP */
  5582.