home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv200.zip / ckucmd.c < prev    next >
C/C++ Source or Header  |  2001-12-08  |  207KB  |  7,335 lines

  1. #define TOKPRECHECK
  2.  
  3. #include "ckcsym.h"
  4. #define DOCHKVAR
  5.  
  6. char *cmdv = "Command package 8.0.150, 8 Dec 2001";
  7.  
  8. /*  C K U C M D  --  Interactive command package for Unix  */
  9.  
  10. /*
  11.   Author: Frank da Cruz (fdc@columbia.edu),
  12.   Columbia University Academic Information Systems, New York City.
  13.  
  14.   Copyright (C) 1985, 2001,
  15.     Trustees of Columbia University in the City of New York.
  16.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  17.     copyright text in the ckcmai.c module for disclaimer and permissions.
  18. */
  19.  
  20. #ifdef OS2                    /* Command-terminal-to-C-Kermit character mask */
  21. int cmdmsk = 255;
  22. #else
  23. int cmdmsk = 255;            /* 31 Dec 2000 (was 127) */
  24. #endif /* OS2 */
  25.  
  26. #ifdef BS_DIRSEP            /* Directory separator is backslash */
  27. #undef BS_DIRSEP
  28. #endif /* BS_DIRSEP */
  29.  
  30. #ifdef OS2
  31. #define BS_DIRSEP
  32. #endif /* BS_DIRSEP */
  33.  
  34. #define CKUCMD_C
  35.  
  36. #include "ckcdeb.h"                     /* Formats for debug(), etc. */
  37. #include "ckcker.h"            /* Needed for BIGBUFOK definition */
  38. #include "ckcnet.h"            /* Needed for server-side Telnet */
  39. #include "ckucmd.h"            /* Needed for xx_strp prototype */
  40. #include "ckuusr.h"                     /* Needed for prompt length */
  41.  
  42. #undef CKUCMD_C
  43.  
  44. _PROTOTYP( int unhex, (char) );
  45. _PROTOTYP( static VOID cmdclrscn, (void) );
  46.  
  47. #ifdef CKLEARN
  48. _PROTOTYP( VOID learncmd, (char *) );
  49. #endif /* CKLEARN */
  50.  
  51. static char *moname[] = {
  52.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  53.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  54. };
  55.  
  56. struct keytab cmonths[] = {
  57.   { "april",     4, 0 },
  58.   { "august",    8, 0 },
  59.   { "december", 12, 0 },
  60.   { "february",  2, 0 },
  61.   { "january",   1, 0 },
  62.   { "july",      7, 0 },
  63.   { "june",      6, 0 },
  64.   { "march",     3, 0 },
  65.   { "may",       5, 0 },
  66.   { "november", 11, 0 },
  67.   { "october",  10, 0 },
  68.   { "september", 9, 0 }
  69. };
  70.  
  71. #ifndef NOICP     /* The rest only if interactive command parsing selected */
  72.  
  73. #ifndef NOSPL
  74. _PROTOTYP( int chkvar, (char *) );
  75. extern int askflag;
  76. #endif /* NOSPL */
  77.  
  78. #ifdef CKROOT
  79. extern int ckrooterr;
  80. #endif /* CKROOT */
  81.  
  82. #ifdef IKSD
  83. extern int inserver;
  84. #endif /* IKSD */
  85.  
  86. int cmfldflgs = 0;            /* Flags for cmfld() */
  87. int cmkwflgs = 0;            /* Flags from last keyword parse */
  88. static int blocklvl = 0;        /* Block nesting level */
  89. static int linebegin = 0;        /* Flag for at start of a line */
  90. static int quoting = 1;            /* Quoting is allowed */
  91. static int swarg = 0;            /* Parsing a switch argument */
  92. static int xcmfdb = 0;            /* Flag for parsing chained fdbs... */
  93. static int chsrc = 0;            /* Source of character, 1 = tty */
  94. static int newcmd = 0;            /* See addcmd() */
  95.  
  96. #ifdef BS_DIRSEP
  97. static int dirnamflg = 0;
  98. #endif /* BS_DIRSEP */
  99.  
  100. /*
  101. Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP. Features:
  102.  
  103. . parses and verifies keywords, filenames, text strings, numbers, other data
  104. . displays appropriate menu or help message when user types "?"
  105. . does keyword and filename completion when user types ESC or TAB
  106. . does partial filename completion
  107. . accepts any unique abbreviation for a keyword
  108. . allows keywords to have attributes, like "invisible" and "abbreviation"
  109. . can supply defaults for fields omitted by user
  110. . provides command retry and recall
  111. . provides command line editing (character, word, and line deletion)
  112. . accepts input from keyboard, command files, macros, or redirected stdin
  113. . allows for full or half duplex operation, character or line input
  114. . allows \-escapes for hard-to-type characters
  115. . allows specification of a user exit to expand variables, etc.
  116. . settable prompt, protected from deletion, dynamically re-evaluated each time.
  117. . allows chained parse functions.
  118.  
  119. Functions:
  120.  cmsetp - Set prompt (cmprom is prompt string)
  121.  cmsavp - Save current prompt
  122.  cmgetp = Get current prompt
  123.  prompt - Issue prompt
  124.  cmini  - Clear the command buffer (before parsing a new command)
  125.  cmres  - Reset command buffer pointers (before reparsing)
  126.  cmkey  - Parse a keyword or token (also cmkey2)
  127.  cmswi  - Parse a switch
  128.  cmnum  - Parse a number
  129.  cmifi  - Parse an input file name
  130.  cmofi  - Parse an output file name (also cmifip, cmifi2, ...)
  131.  cmdir  - Parse a directory name (also cmdirp)
  132.  cmfld  - Parse an arbitrary field
  133.  cmtxt  - Parse a text string
  134.  cmdate - Parse a date-time string
  135.  cmcfm  - Parse command confirmation (end of line)
  136.  cmfdb  - Parse any of a list of the foregoing (chained parse functions)
  137.  
  138. Return codes:
  139.  -9: like -2 except this module already printed the error message
  140.  -3: no input provided when required
  141.  -2: input was invalid (e.g. not a number when a number was required)
  142.  -1: reparse required (user deleted into a preceding field)
  143.   0 or greater: success
  144. See individual functions for greater detail.
  145.  
  146. Before using these routines, the caller should #include ckucmd.h, and set the
  147. program's prompt by calling cmsetp().  If the file parsing functions cmifi,
  148. cmofi, or cmdir are to be used, this module must be linked with a ck?fio file
  149. system support module for the appropriate system, e.g. ckufio for Unix.  If
  150. the caller puts the terminal in character wakeup ("cbreak") mode with no echo,
  151. then these functions will provide line editing -- character, word, and line
  152. deletion, as well as keyword and filename completion upon ESC and help
  153. strings, keyword, or file menus upon '?'.  If the caller puts the terminal
  154. into character wakeup/noecho mode, care should be taken to restore it before
  155. exit from or interruption of the program.  If the character wakeup mode is not
  156. set, the system's own line editor may be used.
  157.  
  158. NOTE: Contrary to expectations, many #ifdef's have been added to this module.
  159. Any operation requiring an #ifdef (like clear screen, get character from
  160. keyboard, erase character from screen, etc) should eventually be turned into a
  161. call to a function that is defined in ck?tio.c, but then all the ck?tio.c
  162. modules would have to be changed...
  163. */
  164.  
  165. /* Includes */
  166.  
  167. #include "ckcker.h"
  168. #include "ckcasc.h"            /* ASCII character symbols */
  169. #include "ckucmd.h"                     /* Command parsing definitions */
  170.  
  171. #ifdef OSF13
  172. #ifdef CK_ANSIC
  173. #ifdef _NO_PROTO
  174. #undef _NO_PROTO
  175. #endif /* _NO_PROTO */
  176. #endif /* CK_ANSIC */
  177. #endif /* OSF13 */
  178.  
  179. #include <errno.h>            /* Error number symbols */
  180.  
  181. #ifdef OS2
  182. #ifndef NT
  183. #define INCL_NOPM
  184. #define INCL_VIO            /* Needed for ckocon.h */
  185. #include <os2.h>
  186. #undef COMMENT
  187. #else
  188. #define APIRET ULONG
  189. #include <windows.h>
  190. #endif /* NT */
  191. #include "ckocon.h"
  192. #include <io.h>
  193. #endif /* OS2 */
  194.  
  195. #ifdef NT
  196. #define stricmp _stricmp
  197. #endif /* NT */
  198.  
  199. #ifdef OSK
  200. #define cc ccount            /* OS-9/68K compiler bug */
  201. #endif /* OSK */
  202.  
  203. #ifdef GEMDOS                /* Atari ST */
  204. #ifdef putchar
  205. #undef putchar
  206. #endif /* putchar */
  207. #define putchar(x) conoc(x)
  208. #endif /* GEMDOS */
  209.  
  210. #ifdef CK_AUTODL
  211. extern int cmdadl, justone;
  212. #endif /* CK_AUTODL */
  213.  
  214. extern int timelimit, nzxopts, nopush, nolocal, xcmdsrc;
  215.  
  216. #ifdef CKSYSLOG
  217. #ifdef UNIX
  218. #ifdef CKXPRINTF            /* Our printf macro conflicts with */
  219. #undef printf                /* use of "printf" in syslog.h */
  220. #endif /* CKXPRINTF */
  221. #ifdef RTAIX
  222. #include <sys/syslog.h>
  223. #else  /* RTAIX */
  224. #include <syslog.h>
  225. #endif /* RTAIX */
  226. #ifdef CKXPRINTF
  227. #define printf ckxprintf
  228. #endif /* CKXPRINTF */
  229. #endif /* UNIX */
  230. #endif /* CKSYSLOG */
  231.  
  232. /* Local variables */
  233.  
  234. static
  235. int psetf = 0,                          /* Flag that prompt has been set */
  236.     cc = 0,                             /* Character count */
  237.     dpx = 0,                            /* Duplex (0 = full) */
  238.     inword = 0;                /* In the middle of getting a word */
  239.  
  240. char *dfprom = "Command? ";             /* Default prompt */
  241.  
  242. int cmflgs;                             /* Command flags */
  243. int cmfsav;                /* A saved version of them */
  244.  
  245. static char pushc = NUL;
  246. static char brkchar = NUL;
  247.  
  248. #define CMDEFAULT 1023
  249. static char cmdefault[CMDEFAULT+1];
  250.  
  251. #ifdef DCMDBUF
  252. char *cmdbuf = NULL;            /* Command buffer */
  253. char *savbuf = NULL;            /* Buffer to save copy of command */
  254. char *atmbuf = NULL;            /* Atom buffer - for current field */
  255. char *atxbuf = NULL;            /* For expanding the atom buffer */
  256. static char *atybuf = NULL;        /* For copying atom buffer */
  257. static char *filbuf = NULL;        /* File name buffer */
  258. static char *cmprom = NULL;        /* Program's prompt */
  259. static char *cmprxx = NULL;        /* Program's prompt, unevaluated */
  260.  
  261. #ifdef CK_RECALL
  262. /*
  263.   Command recall is available only if we can make profligate use of malloc().
  264. */
  265. #define R_MAX 10            /* How many commands to save */
  266. int cm_recall = R_MAX;            /* Size of command recall buffer */
  267. int on_recall = 1;            /* Recall feature is ON */
  268. static int no_recall = 0;        /* Recall OFF for this cmd only */
  269. static int force_add = 0;        /* Force cmd into recall buffer */
  270. static int last_recall = -1;        /* Last recall-related action */
  271. /*
  272.   -1 = none
  273.    0 = CR (a command was entered)
  274.    1 = Up
  275.    2 = Down
  276. */
  277. int in_recall = 0;            /* Recall buffers are init'd */
  278. static int
  279.   current = -1,                /* Pointer to current command */
  280.   rlast = -1;                /* Index of last command in buffer */
  281. static char **recall = NULL;        /* Array of recall buffer pointers */
  282. #endif /* CK_RECALL */
  283. #else  /* !DCMDBUF */
  284. char cmdbuf[CMDBL+4];                   /* Command buffer */
  285. char savbuf[CMDBL+4];                   /* Buffer to save copy of command */
  286. char atmbuf[ATMBL+4];                   /* Atom buffer */
  287. char atxbuf[CMDBL+4];                   /* For expanding the atom buffer */
  288. static char atybuf[ATMBL+4];        /* For copying atom buffer */
  289. static char filbuf[ATMBL+4];        /* File name buffer */
  290. static char cmprom[PROMPTL+1];        /* Program's prompt */
  291. static char cmprxx[PROMPTL+1];        /* Program's prompt, unevaluated */
  292. #endif /* DCMDBUF */
  293.  
  294. /* Command buffer pointers */
  295.  
  296. #define PPVLEN 24
  297. char ppvnambuf[PPVLEN+1] = { NUL, NUL };
  298.  
  299. char * cmbptr = NULL;            /* Current position (for export) */
  300.  
  301. static char *bp,                        /* Current command buffer position */
  302.     *pp,                                /* Start of current field */
  303.     *np;                                /* Start of next field */
  304.  
  305. static int ungw,            /* For ungetting words */
  306.     atxn;                /* Expansion buffer (atxbuf) length */
  307.  
  308. #ifdef OS2
  309. extern int wideresult;
  310. #endif /* OS2 */
  311.  
  312. extern int cmd_cols, cmd_rows, local, quiet;
  313.  
  314. #ifdef TNCODE
  315. #ifdef IAC
  316. #undef IAC
  317. #endif /* IAC */
  318. #define IAC 255
  319. #endif /* TNCODE */
  320.  
  321. _PROTOTYP( static int gtword, (int) );
  322. _PROTOTYP( static int addbuf, (char *) );
  323. _PROTOTYP( static int setatm, (char *, int) );
  324. _PROTOTYP( static VOID cmdnewl, (char) );
  325. _PROTOTYP( static VOID cmdchardel, (void) );
  326. _PROTOTYP( static VOID cmdecho, (char, int) );
  327. _PROTOTYP( static int test, (int, int) );
  328. #ifdef GEMDOS
  329. _PROTOTYP( extern char *strchr, (char *, int) );
  330. #endif /* GEMDOS */
  331.  
  332. extern char * dftty;
  333.  
  334. /* The following are for use with chained FDB's */
  335.  
  336. static int crflag = 0;            /* Carriage return was typed */
  337. static int qmflag = 0;            /* Question mark was typed */
  338. static int esflag = 0;            /* Escape was typed */
  339.  
  340. /* Directory separator */
  341.  
  342. #ifdef GEMDOS
  343. static char dirsep = '\\';
  344. #else
  345. #ifdef datageneral
  346. static char dirsep = ':';
  347. #else
  348. #ifdef MAC
  349. static char dirsep = ':';
  350. #else
  351. #ifdef VMS
  352. static char dirsep = '.';
  353. #else
  354. #ifdef STRATUS
  355. static char dirsep = '>';
  356. #else
  357. static char dirsep = '/';        /* UNIX, OS/2, OS-9, Amiga, etc. */
  358. #endif /* STRATUS */
  359. #endif /* VMS */
  360. #endif /* MAC */
  361. #endif /* datageneral */
  362. #endif /* GEMDOS */
  363.  
  364. /*  C K S P R E A D  --  Print string double-spaced  */
  365.  
  366. static char * sprptr = NULL;
  367.  
  368. static char *
  369. ckspread(s) char * s; {
  370.     int n = 0;
  371.     char * p;
  372.     n = strlen(s);
  373.     if (sprptr)
  374.       free(sprptr);
  375.     sprptr = malloc(n + n + 3);
  376.     if (sprptr) {
  377.     p = sprptr;
  378.     while (*s) {
  379.         *p++ = *s++;
  380.         *p++ = SP;
  381.     }
  382.     *p = NUL;
  383.     }
  384.     return(sprptr ? sprptr : "");
  385. }
  386.  
  387. /*  T E S T  --  Bit test  */
  388.  
  389. static int
  390. test(x,m) int x, m; { /*  Returns 1 if any bits from m are on in x, else 0  */
  391.     return((x & m) ? 1 : 0);
  392. }
  393.  
  394. /*  K W D H E L P  --  Given a keyword table, print keywords in columns.  */
  395. /*
  396.   Call with:
  397.     s     - keyword table
  398.     n     - number of entries
  399.     pat   - pattern (left substring) that must match for each keyword
  400.     pre   - prefix to add to each keyword
  401.     post  - suffix to add to each keyword
  402.     off   - offset on first screenful, allowing room for introductory text
  403.     xhlp  - 1 to print any CM_INV keywords that are not also abbreviations.
  404.             2 to print CM_INV keywords if CM_HLP also set
  405.             4 if it's a switch table (to show ':' if CM_ARG)
  406.  
  407.   Arranges keywords in columns with width based on longest keyword.
  408.   Does "more?" prompting at end of screen.
  409.   Uses global cmd_rows and cmd_cols for screen size.
  410. */
  411. VOID
  412. kwdhelp(s,n,pat,pre,post,off,xhlp)
  413.     struct keytab s[]; int n, off, xhlp; char *pat, *pre, *post;
  414. /* kwdhelp */ {
  415.  
  416.     int width = 0;
  417.     int cc;
  418.     int cols, height, i, j, k, lc, n2 = 0;
  419.     char *b = NULL, *p, *q;
  420.     char *pa, *px;
  421.     char **s2 = NULL;
  422.     char *tmpbuf = NULL;
  423.  
  424.     cc = strlen(pat);
  425.  
  426.     if (!s) return;            /* Nothing to do */
  427.     if (n < 1) return;            /* Ditto */
  428.     if (off < 0) off = 0;        /* Offset for first page */
  429.     if (!pre) pre = "";            /* Handle null string pointers */
  430.     if (!post) post = "";
  431.     lc = off;                /* Screen-line counter */
  432.  
  433.     if (xhlp & 4)            /* For switches */
  434.       tmpbuf = (char *)malloc(TMPBUFSIZ+1);
  435.  
  436.     if ((s2 = (char **) malloc(n * sizeof(char *)))) {
  437.     for (i = 0; i < n; i++) {    /* Find longest keyword */
  438.         s2[i] = NULL;
  439.         if (ckstrcmp(s[i].kwd,pat,cc,0))
  440.           continue;
  441.  
  442.         if (s[i].flgs & CM_PSH    /* NOPUSH or nopush screening */
  443. #ifndef NOPUSH
  444.         && nopush
  445. #endif /* NOPUSH */
  446.         )
  447.           continue;
  448.         if (s[i].flgs & CM_LOC    /* NOLOCAL or nolocal screening */
  449. #ifndef NOLOCAL
  450.         && nolocal
  451. #endif /* NOLOCAL */
  452.         )
  453.           continue;
  454.  
  455.         if (s[i].flgs & CM_INV) {
  456. #ifdef COMMENT
  457. /* This code does not show invisible keywords at all except for "help ?" */
  458. /* and then only help topics (CM_HLP) in the top-level keyword list. */
  459.  
  460.         if ((xhlp & 2) == 0)
  461.           continue;
  462.         else if ((s[i].flgs & CM_HLP) == 0)
  463.           continue;
  464. #else
  465. /* This code shows invisible keywords that are not also abbreviations when */
  466. /* ? was typed AFTER the beginning of the field so the user can find out */
  467. /* what they are and (for example) why completion doesn't work at this point */
  468.  
  469.         if (s[i].flgs & CM_ABR)
  470.           continue;
  471.         else if ((xhlp & 3) == 0)
  472.           continue;
  473.         else if ((xhlp & 2) && ((s[i].flgs & CM_HLP) == 0))
  474.           continue;
  475. #endif /* COMMENT */
  476.         }
  477.         j = strlen(s[i].kwd);
  478.         if (!(xhlp & 4) || !tmpbuf) { /* Regular keyword table */
  479.         s2[n2++] = s[i].kwd;    /* Copy pointers to visible ones */
  480.         } else {            /* Switches */
  481.         ckmakmsg(tmpbuf,    /* Make a copy that shows ":" if */
  482.              TMPBUFSIZ,    /* the switch takes an argument. */
  483.              s[i].kwd,
  484.              (s[i].flgs & CM_ARG) ? ":" : "",
  485.              NULL,
  486.              NULL
  487.              );
  488.         makestr(&(s2[n2]),tmpbuf);
  489.         if (s[i].flgs & CM_ARG) j++;
  490.         n2++;
  491.         }
  492.         if (j > width)
  493.           width = j;
  494.     }
  495.     /* Column width */
  496.     n = n2;
  497.     }
  498.     if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer   */
  499.     char * bx;
  500.     bx = b + cmd_cols;
  501.     width += (int)strlen(pre) + (int)strlen(post) + 2;
  502.     cols = cmd_cols / width;    /* How many columns? */
  503.     if (cols < 1) cols = 1;
  504.     height = n / cols;        /* How long is each column? */
  505.     if (n % cols) height++;        /* Add one for remainder, if any */
  506.  
  507.     for (i = 0; i < height; i++) {        /* Loop for each row */
  508.         for (j = 0; j < cmd_cols; j++)  /* First fill row with blanks */
  509.           b[j] = SP;
  510.         for (j = 0; j < cols; j++) {    /* Loop for each column in row */
  511.         k = i + (j * height);       /* Index of next keyword */
  512.         if (k < n) {            /* In range? */
  513.             pa = pre;
  514.             px = post;
  515.             p = s2[k];            /* Point to verb name */
  516.             q = b + (j * width) + 1; /* Where to copy it to */
  517.             while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
  518.             q--;                         /* Back up over NUL */
  519.             while ((q < bx) && (*q++ = *p++)) ;     /* Copy filename */
  520.             q--;                         /* Back up over NUL */
  521.             while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
  522.             if (j < cols - 1) {
  523.             q--;
  524.             *q = SP;    /* Replace the space */
  525.             }
  526.         }
  527.         }
  528.         p = b + cmd_cols - 1;    /* Last char in line */
  529.         while (*p-- == SP) ;    /* Trim */
  530.         *(p+2) = NUL;
  531.         printf("%s\n",b);        /* Print the line */
  532.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  533.         if (!askmore())        /* Do more-prompting... */
  534.           goto xkwdhelp;
  535.         else
  536.           lc = 0;
  537.         }
  538.     }
  539.     /* printf("\n"); */        /* Blank line at end of report */
  540.     } else {                /* Malloc failure, no columns */
  541.     for (i = 0; i < n; i++) {
  542.         if (s[i].flgs & CM_INV)    /* Use original keyword table */
  543.           continue;            /* skipping invisible entries */
  544.         printf("%s%s%s\n",pre,s[i].kwd,post);
  545.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  546.         if (!askmore())        /* Do more-prompting... */
  547.           goto xkwdhelp;
  548.         else
  549.           lc = 0;
  550.         }
  551.     }
  552.     }
  553.   xkwdhelp:
  554.     if (xhlp & 4) {
  555.     if (tmpbuf) free((char *)tmpbuf);
  556.     for (i = 0; i < n; i++)
  557.       if (s2[i]) free(s2[i]);
  558.     }
  559.     if (s2) free(s2);            /* Free array copy */
  560.     if (b) free(b);            /* Free line buffer */
  561.     return;
  562. }
  563.  
  564. /*  F I L H E L P  --  Given a file list, print names in columns.  */
  565. /*
  566.   Call with:
  567.     n     - number of entries
  568.     pre   - prefix to add to each filename
  569.     post  - suffix to add to each filename
  570.     off   - offset on first screenful, allowing room for introductory text
  571.     cmdirflg - 1 if only directory names should be listed, 0 to list all files
  572.  
  573.   Arranges filenames in columns with width based on longest filename.
  574.   Does "more?" prompting at end of screen.
  575.   Uses global cmd_rows and cmd_cols for screen size.
  576. */
  577.  
  578. int
  579. filhelp(n,pre,post,off,cmdirflg) int n, off; char *pre, *post; int cmdirflg; {
  580.     char filbuf[CKMAXPATH + 1];        /* Temp buffer for one filename */
  581.     int width = 0;
  582.     int cols, height, i, j, k, lc, n2 = 0, rc = 0, itsadir = 0;
  583.     char *b = NULL, *p, *q;
  584.     char *pa, *px;
  585.     char **s2 = NULL;
  586. #ifdef VMS
  587.     char * cdp = zgtdir();
  588. #endif /* VMS */
  589.  
  590.     if (n < 1) return(0);
  591.     if (off < 0) off = 0;        /* Offset for first page */
  592.     if (!pre) pre = "";            /* Handle null string pointers */
  593.     if (!post) post = "";
  594.  
  595.     lc = off;                /* Screen-line counter */
  596.  
  597.     if ((s2 = (char **) malloc(n * sizeof(char *)))) {
  598.     for (i = 0; i < n; i++) {    /* Loop through filenames */
  599.         itsadir = 0;
  600.         s2[i] = NULL;        /* Initialize each pointer to NULL */
  601.         znext(filbuf);        /* Get next filename */
  602.         if (!filbuf[0])        /* Shouldn't happen */
  603.           break;
  604. #ifdef COMMENT
  605.         itsadir = isdir(filbuf);    /* Is it a directory? */
  606.         if (cmdirflg && !itsadir)    /* No, listing directories only? */
  607.           continue;            /* So skip this one. */
  608. #endif /* COMMENT */
  609. #ifdef VMS
  610.         ckstrncpy(filbuf,zrelname(filbuf,cdp),CKMAXPATH);
  611. #endif /* VMS */
  612.         j = strlen(filbuf);
  613. #ifndef VMS
  614.         if (itsadir && j < CKMAXPATH - 1 && j > 0) {
  615.         if (filbuf[j-1] != dirsep) {
  616.             filbuf[j++] = dirsep;
  617.             filbuf[j] = NUL;
  618.         }
  619.         }
  620. #endif /* VMS */
  621.         if (!(s2[n2] = malloc(j+1))) {
  622.         printf("?Memory allocation failure\n");
  623.         rc = -9;
  624.         goto xfilhelp;
  625.         }
  626.         if (j <= CKMAXPATH) {
  627.         strcpy(s2[n2],filbuf);
  628.         n2++;
  629.         } else {
  630.         printf("?Name too long - %s\n", filbuf);
  631.         rc = -9;
  632.         goto xfilhelp;
  633.         }
  634.         if (j > width)        /* Get width of widest one */
  635.           width = j;
  636.     }
  637.     n = n2;                /* How many we actually got */
  638.     }
  639.     sh_sort(s2,NULL,n,0,0,filecase);    /* Alphabetize the list */
  640.  
  641.     rc = 1;
  642.     if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */
  643.     char * bx;
  644.     bx = b + cmd_cols;
  645.     width += (int)strlen(pre) + (int)strlen(post) + 2;
  646.     cols = cmd_cols / width;    /* How many columns? */
  647.     if (cols < 1) cols = 1;
  648.     height = n / cols;        /* How long is each column? */
  649.     if (n % cols) height++;        /* Add one for remainder, if any */
  650.  
  651.     for (i = 0; i < height; i++) {        /* Loop for each row */
  652.         for (j = 0; j < cmd_cols; j++)  /* First fill row with blanks */
  653.           b[j] = SP;
  654.         for (j = 0; j < cols; j++) {    /* Loop for each column in row */
  655.         k = i + (j * height);       /* Index of next filename */
  656.         if (k < n) {            /* In range? */
  657.             pa = pre;
  658.             px = post;
  659.             p = s2[k];                       /* Point to filename */
  660.             q = b + (j * width) + 1;             /* and destination */
  661.             while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
  662.             q--;                         /* Back up over NUL */
  663.             while ((q < bx) && (*q++ = *p++)) ;     /* Copy filename */
  664.             q--;                         /* Back up over NUL */
  665.             while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
  666.             if (j < cols - 1) {
  667.             q--;
  668.             *q = SP;    /* Replace the space */
  669.             }
  670.         }
  671.         }
  672.         p = b + cmd_cols - 1;    /* Last char in line */
  673.         while (*p-- == SP) ;    /* Trim */
  674.         *(p+2) = NUL;
  675.         printf("%s\n",b);        /* Print the line */
  676.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  677.         if (!askmore()) {    /* Do more-prompting... */
  678.             rc = 0;
  679.             goto xfilhelp;
  680.         } else
  681.           lc = 0;
  682.         }
  683.     }
  684.     printf("\n");            /* Blank line at end of report */
  685.     goto xfilhelp;
  686.     } else {                /* Malloc failure, no columns */
  687.     for (i = 0; i < n; i++) {
  688.         znext(filbuf);
  689.         if (!filbuf[0]) break;
  690.         printf("%s%s%s\n",pre,filbuf,post);
  691.         if (++lc > (cmd_rows - 2)) { /* Screen full? */
  692.         if (!askmore()) {     /* Do more-prompting... */
  693.             rc = 0;
  694.             goto xfilhelp;
  695.         } else lc = 0;
  696.         }
  697.     }
  698. xfilhelp:
  699.     if (b) free(b);
  700.     for (i = 0; i < n2; i++)
  701.       if (s2[i]) free(s2[i]);
  702.     if (s2) free((char *)s2);
  703.     return(rc);
  704.     }
  705. }
  706.  
  707. /*  C M S E T U P  --  Set up command buffers  */
  708.  
  709. #ifdef DCMDBUF
  710. int
  711. cmsetup() {
  712.     if (!(cmdbuf = malloc(CMDBL + 4))) return(-1);
  713.     if (!(savbuf = malloc(CMDBL + 4))) return(-1);
  714.     savbuf[0] = '\0';
  715.     if (!(atmbuf = malloc(ATMBL + 4))) return(-1);
  716.     if (!(atxbuf = malloc(CMDBL + 4))) return(-1);
  717.     if (!(atybuf = malloc(ATMBL + 4))) return(-1);
  718.     if (!(filbuf = malloc(ATMBL + 4))) return(-1);
  719.     if (!(cmprom = malloc(PROMPTL + 4))) return(-1);
  720.     if (!(cmprxx = malloc(PROMPTL + 4))) return(-1);
  721. #ifdef CK_RECALL
  722.     cmrini(cm_recall);
  723. #endif /* CK_RECALL */
  724.     return(0);
  725. }
  726. #endif /* DCMDBUF */
  727.  
  728. /*  C M S E T P  --  Set the program prompt.  */
  729.  
  730. VOID
  731. cmsetp(s) char *s; {
  732.     if (!s) s = "";
  733.     ckstrncpy(cmprxx,s,PROMPTL);
  734.     psetf = 1;                          /* Flag that prompt has been set. */
  735. }
  736.  
  737. /*  C M S A V P  --  Save a copy of the current prompt.  */
  738.  
  739. VOID
  740. #ifdef CK_ANSIC
  741. cmsavp(char s[], int n)
  742. #else
  743. cmsavp(s,n) char s[]; int n;
  744. #endif /* CK_ANSIC */
  745. /* cmsavp */ {
  746.     if (psetf)                /* But not if no prompt is set. */
  747.       ckstrncpy(s,cmprxx,n);
  748. }
  749.  
  750. char *
  751. cmgetp() {
  752.     return(cmprxx);
  753. }
  754.  
  755. int
  756. cmgbrk() {
  757.     return(brkchar);
  758. }
  759.  
  760. int
  761. cmgkwflgs() {
  762.     return(cmkwflgs);
  763. }
  764.  
  765. /*  P R O M P T  --  Issue the program prompt.  */
  766.  
  767. VOID
  768. prompt(f) xx_strp f; {
  769.     char *sx, *sy; int n;
  770. #ifdef CK_SSL
  771.     extern int ssl_active_flag, tls_active_flag;
  772. #endif /* CK_SSL */
  773. #ifdef OS2
  774.     extern int display_demo;
  775.  
  776.     /* If there is a demo screen to be displayed, display it */
  777.     if (display_demo && xcmdsrc == 0) {
  778.         demoscrn();
  779.         display_demo = 0;
  780.     }
  781. #endif /* OS2 */
  782.  
  783.     if (psetf == 0)            /* If no prompt set, set default. */
  784.       cmsetp(dfprom);
  785.  
  786.     sx = cmprxx;            /* Unevaluated copy */
  787.     if (f) {                /* If conversion function given */
  788.     sy = cmprom;            /* Evaluate it */
  789.     debug(F101,"prompt sx","",sx);
  790.     debug(F101,"prompt sy","",sy);
  791.     n = PROMPTL;
  792.     if ((*f)(sx,&sy,&n) < 0)    /* If evaluation failed */
  793.       sx = cmprxx;            /* revert to unevaluated copy */
  794.     else if (!*cmprom)        /* ditto if it came up empty */
  795.       sx = cmprxx;
  796.     else
  797.       sx = cmprom;
  798.     } else
  799.       ckstrncpy(cmprom,sx,PROMPTL);
  800.     cmprom[PROMPTL-1] = NUL;
  801.     if (!*sx)                /* Don't print if empty */
  802.       return;
  803.  
  804. #ifdef OSK
  805.     fputs(sx, stdout);
  806. #else
  807. #ifdef MAC
  808.     printf("%s", sx);
  809. #else
  810. #ifdef IKSD
  811.     if (inserver) {            /* Print the prompt. */
  812.         ttoc(CR);            /* If TELNET Server */
  813.         ttoc(NUL);            /* must folloW CR by NUL */
  814.         printf("%s",sx);
  815.     } else
  816. #endif /* IKSD */
  817.       printf("\r%s",sx);
  818. #ifdef CK_SSL
  819.     if (!(ssl_active_flag || tls_active_flag))
  820. #endif /* CK_SSL */
  821.       fflush(stdout);            /* Now! */
  822. #endif /* MAC */
  823. #endif /* OSK */
  824. }
  825.  
  826. #ifndef NOSPL
  827. VOID
  828. pushcmd(s) char * s; {            /* For use with IF command. */
  829.     if (!s) s = np;
  830.     ckstrncpy(savbuf,s,CMDBL);        /* Save the dependent clause,  */
  831.     cmres();                /* and clear the command buffer. */
  832.     debug(F110, "pushcmd savbuf", savbuf, 0);
  833. }
  834.  
  835. VOID
  836. pushqcmd(s) char * s; {            /* For use with ELSE command. */
  837.     char c, * p = savbuf;        /* Dest */
  838.     if (!s) s = np;            /* Source */
  839.     while (*s) {            /* Get first nonwhitespace char */
  840.     if (*s != SP)
  841.       break;
  842.     else
  843.       s++;
  844.     }
  845.     if (*s != '{') {            /* If it's not "{" */
  846.     pushcmd(s);            /* do regular pushcmd */
  847.     return;
  848.     }
  849.     while ((c = *s++)) {        /* Otherwise insert quotes */
  850.     if (c == CMDQ)
  851.       *p++ = CMDQ;
  852.     *p++ = c;
  853.     }
  854.     cmres();                /* and clear the command buffer. */
  855.     debug(F110, "pushqcmd savbuf", savbuf, 0);
  856. }
  857. #endif /* NOSPL */
  858.  
  859. #ifdef COMMENT
  860. /* no longer used... */
  861. VOID
  862. popcmd() {
  863.     ckstrncpy(cmdbuf,savbuf,CMDBL);    /* Put back the saved material */
  864.     *savbuf = '\0';            /* and clear the save buffer */
  865.     cmres();
  866. }
  867. #endif /* COMMENT */
  868.  
  869. /*  C M R E S  --  Reset pointers to beginning of command buffer.  */
  870.  
  871. VOID
  872. cmres() {
  873.     inword = 0;                /* We're not in a word */
  874.     cc = 0;                /* Character count is zero */
  875.  
  876. /* Initialize pointers */
  877.  
  878.     pp = cmdbuf;            /* Beginning of current field */
  879.     bp = cmdbuf;            /* Current position within buffer */
  880.     np = cmdbuf;            /* Where to start next field */
  881.  
  882.     cmfldflgs = 0;
  883.     cmflgs = -5;                        /* Parse not yet started. */
  884.     ungw = 0;                /* Don't need to unget a word. */
  885. }
  886.  
  887. /*  C M I N I  --  Clear the command and atom buffers, reset pointers.  */
  888.  
  889. /*
  890. The argument specifies who is to echo the user's typein --
  891.   1 means the cmd package echoes
  892.   0 somebody else (system, front end, terminal) echoes
  893. */
  894. VOID
  895. cmini(d) int d; {
  896. #ifdef DCMDBUF
  897.     if (!atmbuf)
  898.       if (cmsetup()<0)
  899.     fatal("fatal error: unable to allocate command buffers");
  900. #endif /* DCMDBUF */
  901. #ifdef USE_MEMCPY
  902.     memset(cmdbuf,0,CMDBL);
  903.     memset(atmbuf,0,ATMBL);
  904. #else
  905.     for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  906.     for (bp = atmbuf; bp < atmbuf+ATMBL; bp++) *bp = NUL;
  907. #endif /* USE_MEMCPY */
  908.  
  909.     *atmbuf = *savbuf = *atxbuf = *atybuf = *filbuf = NUL;
  910.     blocklvl = 0;            /* Block level is 0 */
  911.     linebegin = 1;            /* At the beginning of a line */
  912.     dpx = d;                /* Global copy of the echo flag */
  913.     debug(F101,"cmini dpx","",dpx);
  914.     crflag = 0;                /* Reset flags */
  915.     qmflag = 0;
  916.     esflag = 0;
  917. #ifdef CK_RECALL
  918.     no_recall = 0;            /* Start out with recall enabled */
  919. #endif /* CK_RECALL */
  920.     cmres();                /* Sets bp etc */
  921.     newcmd = 1;                /* See addcmd() */
  922. }
  923.  
  924. #ifndef NOSPL
  925. /*
  926.   The following bits are to allow the command package to call itself
  927.   in the middle of a parse.  To do this, begin by calling cmpush, and
  928.   end by calling cmpop.  As you can see, this is rather expensive.
  929. */
  930. #ifdef DCMDBUF
  931. struct cmp {
  932.     int i[5];                /* stack for integers */
  933.     char *c[3];                /* stack for pointers */
  934.     char *b[8];                /* stack for buffer contents */
  935. };
  936. struct cmp *cmp = 0;
  937. #else
  938. int cmp_i[CMDDEP+1][5];            /* Stack for integers */
  939. char *cmp_c[CMDDEP+1][5];        /* for misc pointers */
  940. char *cmp_b[CMDDEP+1][7];        /* for buffer contents pointers */
  941. #endif /* DCMDBUF */
  942.  
  943. int cmddep = -1;            /* Current stack depth */
  944.  
  945. int
  946. cmpush() {                /* Save the command environment */
  947.     char *cp;                /* Character pointer */
  948.  
  949.     if (cmddep >= CMDDEP)        /* Enter a new command depth */
  950.       return(-1);
  951.     cmddep++;
  952.     debug(F101,"&cmpush to depth","",cmddep);
  953.  
  954. #ifdef DCMDBUF
  955.     /* allocate memory for cmp if not already done */
  956.     if (!cmp && !(cmp = (struct cmp *) malloc(sizeof(struct cmp)*(CMDDEP+1))))
  957.       fatal("cmpush: no memory for cmp");
  958.     cmp[cmddep].i[0] = cmflgs;        /* First do the global ints */
  959.     cmp[cmddep].i[1] = cmfsav;
  960.     cmp[cmddep].i[2] = atxn;
  961.     cmp[cmddep].i[3] = ungw;
  962.  
  963.     cmp[cmddep].c[0] = bp;        /* Then the global pointers */
  964.     cmp[cmddep].c[1] = pp;
  965.     cmp[cmddep].c[2] = np;
  966. #else
  967.     cmp_i[cmddep][0] = cmflgs;        /* First do the global ints */
  968.     cmp_i[cmddep][1] = cmfsav;
  969.     cmp_i[cmddep][2] = atxn;
  970.     cmp_i[cmddep][3] = ungw;
  971.  
  972.     cmp_c[cmddep][0] = bp;        /* Then the global pointers */
  973.     cmp_c[cmddep][1] = pp;
  974.     cmp_c[cmddep][2] = np;
  975. #endif /* DCMDBUF */
  976.  
  977.     /* Now the buffers themselves.  A lot of repititious code... */
  978.  
  979. #ifdef DCMDBUF
  980.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  981.     if (cp) strcpy(cp,cmdbuf);
  982.     cmp[cmddep].b[0] = cp;
  983.     if (cp == NULL) return(-1);
  984.  
  985.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  986.     if (cp) strcpy(cp,savbuf);
  987.     cmp[cmddep].b[1] = cp;
  988.     if (cp == NULL) return(-1);
  989.  
  990.     cmp[cmddep].b[2] = NULL;
  991.  
  992.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  993.     if (cp) strcpy(cp,atmbuf);
  994.     cmp[cmddep].b[3] = cp;
  995.     if (cp == NULL) return(-1);
  996.  
  997.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  998.     if (cp) strcpy(cp,atxbuf);
  999.     cmp[cmddep].b[4] = cp;
  1000.     if (cp == NULL) return(-1);
  1001.  
  1002.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  1003.     if (cp) strcpy(cp,atybuf);
  1004.     cmp[cmddep].b[5] = cp;
  1005.     if (cp == NULL) return(-1);
  1006.  
  1007.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  1008.     if (cp) strcpy(cp,filbuf);
  1009.     cmp[cmddep].b[6] = cp;
  1010.     if (cp == NULL) return(-1);
  1011. #else
  1012.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  1013.     if (cp) strcpy(cp,cmdbuf);
  1014.     cmp_b[cmddep][0] = cp;
  1015.     if (cp == NULL) return(-1);
  1016.  
  1017.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  1018.     if (cp) strcpy(cp,savbuf);
  1019.     cmp_b[cmddep][1] = cp;
  1020.     if (cp == NULL) return(-1);
  1021.  
  1022.     cmp_b[cmddep][2] = NULL;
  1023.  
  1024.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  1025.     if (cp) strcpy(cp,atmbuf);
  1026.     cmp_b[cmddep][3] = cp;
  1027.     if (cp == NULL) return(-1);
  1028.  
  1029.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  1030.     if (cp) strcpy(cp,atxbuf);
  1031.     cmp_b[cmddep][4] = cp;
  1032.     if (cp == NULL) return(-1);
  1033.  
  1034.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  1035.     if (cp) strcpy(cp,atybuf);
  1036.     cmp_b[cmddep][5] = cp;
  1037.     if (cp == NULL) return(-1);
  1038.  
  1039.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  1040.     if (cp) strcpy(cp,filbuf);
  1041.     cmp_b[cmddep][6] = cp;
  1042.     if (cp == NULL) return(-1);
  1043. #endif /* DCMDBUF */
  1044.  
  1045.     cmini(dpx);                /* Initize the command parser */
  1046.     return(0);
  1047. }
  1048.  
  1049. int
  1050. cmpop() {                /* Restore the command environment */
  1051.     if (cmddep < 0) {
  1052.     debug(F100,"&cmpop called from top level","",0);
  1053.     return(-1);            /* Don't pop too much! */
  1054.     }
  1055. #ifdef DCMDBUF
  1056.     cmflgs = cmp[cmddep].i[0];        /* First do the global ints */
  1057.     cmfsav = cmp[cmddep].i[1];
  1058.     atxn = cmp[cmddep].i[2];
  1059.     ungw = cmp[cmddep].i[3];
  1060.  
  1061.     bp = cmp[cmddep].c[0];        /* Then the global pointers */
  1062.     pp = cmp[cmddep].c[1];
  1063.     np = cmp[cmddep].c[2];
  1064. #else
  1065.     cmflgs = cmp_i[cmddep][0];        /* First do the global ints */
  1066.     cmfsav = cmp_i[cmddep][1];
  1067.     atxn = cmp_i[cmddep][2];
  1068.     ungw = cmp_i[cmddep][3];
  1069.  
  1070.     bp = cmp_c[cmddep][0];        /* Then the global pointers */
  1071.     pp = cmp_c[cmddep][1];
  1072.     np = cmp_c[cmddep][2];
  1073. #endif /* DCMDBUF */
  1074.  
  1075.     /* Now the buffers themselves. */
  1076.     /* Note: strncpy(), not ckstrncpy() -- Here we WANT the NUL padding... */
  1077.  
  1078. #ifdef DCMDBUF
  1079.     if (cmp[cmddep].b[0]) {
  1080.  
  1081.     strncpy(cmdbuf,cmp[cmddep].b[0],CMDBL); /* 0: Command buffer */
  1082.     free(cmp[cmddep].b[0]);
  1083.     cmp[cmddep].b[0] = NULL;
  1084.     }
  1085.     if (cmp[cmddep].b[1]) {
  1086.     strncpy(savbuf,cmp[cmddep].b[1],CMDBL); /* 1: Save buffer */
  1087.     free(cmp[cmddep].b[1]);
  1088.     cmp[cmddep].b[1] = NULL;
  1089.     }
  1090.     if (cmp[cmddep].b[3]) {
  1091.     strncpy(atmbuf,cmp[cmddep].b[3],ATMBL); /* 3: Atomic buffer! */
  1092.     free(cmp[cmddep].b[3]);
  1093.     cmp[cmddep].b[3] = NULL;
  1094.     }
  1095.     if (cmp[cmddep].b[4]) {
  1096.     strncpy(atxbuf,cmp[cmddep].b[4],ATMBL); /* 4: eXpansion buffer */
  1097.     free(cmp[cmddep].b[4]);
  1098.     cmp[cmddep].b[4] = NULL;
  1099.     }
  1100.     if (cmp[cmddep].b[5]) {
  1101.     strncpy(atybuf,cmp[cmddep].b[5],ATMBL); /* 5: Atom buffer copY */
  1102.     free(cmp[cmddep].b[5]);
  1103.     cmp[cmddep].b[5] = NULL;
  1104.     }
  1105.     if (cmp[cmddep].b[6]) {
  1106.     strncpy(filbuf,cmp[cmddep].b[6],ATMBL); /* 6: Filename buffer */
  1107.     free(cmp[cmddep].b[6]);
  1108.     cmp[cmddep].b[6] = NULL;
  1109.     }
  1110. #else
  1111.     if (cmp_b[cmddep][0]) {
  1112.     strncpy(cmdbuf,cmp_b[cmddep][0],CMDBL); /* 0: Command buffer */
  1113.     free(cmp_b[cmddep][0]);
  1114.     cmp_b[cmddep][0] = NULL;
  1115.     }
  1116.     if (cmp_b[cmddep][1]) {
  1117.     strncpy(savbuf,cmp_b[cmddep][1],CMDBL); /* 1: Save buffer */
  1118.     free(cmp_b[cmddep][1]);
  1119.     cmp_b[cmddep][1] = NULL;
  1120.     }
  1121.     if (cmp_b[cmddep][3]) {
  1122.     strncpy(atmbuf,cmp_b[cmddep][3],ATMBL); /* 3: Atomic buffer! */
  1123.     free(cmp_b[cmddep][3]);
  1124.     cmp_b[cmddep][3] = NULL;
  1125.     }
  1126.     if (cmp_b[cmddep][4]) {
  1127.     strncpy(atxbuf,cmp_b[cmddep][4],ATMBL); /* 4: eXpansion buffer */
  1128.     free(cmp_b[cmddep][4]);
  1129.     cmp_b[cmddep][4] = NULL;
  1130.     }
  1131.     if (cmp_b[cmddep][5]) {
  1132.     strncpy(atybuf,cmp_b[cmddep][5],ATMBL); /* 5: Atom buffer copY */
  1133.     free(cmp_b[cmddep][5]);
  1134.     cmp_b[cmddep][5] = NULL;
  1135.     }
  1136.     if (cmp_b[cmddep][6]) {
  1137.     strncpy(filbuf,cmp_b[cmddep][6],ATMBL); /* 6: Filename buffer */
  1138.     free(cmp_b[cmddep][6]);
  1139.     cmp_b[cmddep][6] = NULL;
  1140.     }
  1141. #endif /* DCMDBUF */
  1142.  
  1143.     cmddep--;                /* Rise, rise */
  1144.     debug(F101,"&cmpop to depth","",cmddep);
  1145.     return(cmddep);
  1146. }
  1147. #endif /* NOSPL */
  1148.  
  1149. #ifdef COMMENT
  1150. VOID                    /* Not used */
  1151. stripq(s) char *s; {                    /* Function to strip '\' quotes */
  1152.     char *t;
  1153.     while (*s) {
  1154.         if (*s == CMDQ) {
  1155.             for (t = s; *t != '\0'; t++) *t = *(t+1);
  1156.         }
  1157.         s++;
  1158.     }
  1159. }
  1160. #endif /* COMMENT */
  1161.  
  1162. /* Convert tabs to spaces, one for one */
  1163. VOID
  1164. untab(s) char *s; {
  1165.     while (*s) {
  1166.     if (*s == HT) *s = SP;
  1167.     s++;
  1168.     }
  1169. }
  1170.  
  1171. /*  C M N U M  --  Parse a number in the indicated radix  */
  1172.  
  1173. /*
  1174.  The radix is specified in the arg list.
  1175.  Parses unquoted numeric strings in the given radix.
  1176.  Parses backslash-quoted numbers in the radix indicated by the quote:
  1177.    \nnn = \dnnn = decimal, \onnn = octal, \xnn = Hexadecimal.
  1178.  If these fail, then if a preprocessing function is supplied, that is applied
  1179.  and then a second attempt is made to parse an unquoted decimal string.
  1180.  And if that fails, the preprocessed string is passed to an arithmetic
  1181.  expression evaluator.
  1182.  
  1183.  Returns:
  1184.    -3 if no input present when required,
  1185.    -2 if user typed an illegal number,
  1186.    -1 if reparse needed,
  1187.     0 otherwise, with argument n set to the number that was parsed
  1188. */
  1189. int
  1190. cmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; xx_strp f; {
  1191.     int x; char *s, *zp, *zq;
  1192. #ifdef COMMENT
  1193.     char lbrace, rbrace;
  1194. #endif /* COMMENT */
  1195.  
  1196.     if (!xhlp) xhlp = "";
  1197.     if (!xdef) xdef = "";
  1198.  
  1199. #ifdef COMMENT
  1200.     if (cmfldflgs & 1) {
  1201.     lbrace = '(';
  1202.     rbrace = ')';
  1203.     } else {
  1204.     lbrace = '{';
  1205.     rbrace = '}';
  1206.     }
  1207. #endif /* COMMENT */
  1208.  
  1209.     if (radix != 10 && radix != 8) {    /* Just do bases 8 and 10 */
  1210.         printf("cmnum: illegal radix - %d\n",radix);
  1211.         return(-2);
  1212.     } /* Easy to add others but there has never been a need for it. */
  1213.     x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  1214.     debug(F101,"cmnum: cmfld","",x);
  1215.     if (x < 0) return(x);        /* Parse a field */
  1216.     zp = atmbuf;
  1217. /*
  1218.   Edit 192 - Allow any number field to be braced.  This lets us include
  1219.   spaces in expressions, but perhaps more important lets us have user-defined
  1220.   functions in numeric fields.
  1221. */
  1222.     zp = brstrip(zp);            /* Strip braces */
  1223.     if (cmfldflgs & 1 && *zp == '(') {    /* Parens too.. */
  1224.     x = (int) strlen(atmbuf);
  1225.     if (x > 0) {
  1226.         if (*(atmbuf+x-1) == ')') {
  1227.         *(atmbuf+x-1) = NUL;
  1228.         zp++;
  1229.         }
  1230.     }
  1231.     }
  1232.     if (chknum(zp)) {            /* Check for number */
  1233.     if (radix == 8) {        /* If it's supposed to be octal */
  1234.         zp = ckradix(zp,8,10);    /* convert to decimal */
  1235.         if (!zp) return(-2);
  1236.         if (!strcmp(zp,"-1")) return(-2);
  1237.     }
  1238.         *n = atoi(zp);            /* Convert decimal string to int. */
  1239.     debug(F101,"cmnum 1st chknum ok","",*n);
  1240.         return(0);
  1241.     } else if ((x = xxesc(&zp)) > -1) {    /* Check for backslash escape */
  1242.  
  1243. #ifndef OS2
  1244.     *n = x;
  1245. #else
  1246.     *n = wideresult;
  1247. #endif /* OS2 */
  1248.  
  1249.     debug(F101,"cmnum xxesc ok","",*n);
  1250.     return(*zp ? -2 : 0);
  1251.     } else if (f) {            /* If conversion function given */
  1252.     zq = atxbuf;            /* Try that */
  1253.     atxn = CMDBL;
  1254.     if ((*f)(zp,&zq,&atxn) < 0)    /* Convert */
  1255.       return(-2);
  1256.     zp = atxbuf;
  1257.     }
  1258.     debug(F110,"cmnum zp 1",zp,0);
  1259.     if (!*zp) zp = xdef;        /* Result empty, substitute default */
  1260.     debug(F110,"cmnum zp 2",zp,0);
  1261.     if (chknum(zp)) {            /* Check again for decimal number */
  1262.     if (radix == 8) {        /* If it's supposed to be octal */
  1263.         zp = ckradix(zp,8,10);    /* convert to decimal */
  1264.         if (!zp) return(-2);
  1265.         if (!strcmp(zp,"-1")) return(-2);
  1266.     }
  1267.         *n = atoi(zp);            /* Got one, we're done. */
  1268.     debug(F101,"cmnum 2nd chknum ok","",*n);
  1269.         return(0);
  1270. #ifndef NOSPL
  1271.     }  else if ((x = xxesc(&zp)) > -1) { /* Check for backslash escape */
  1272. #ifndef OS2
  1273.     *n = x;
  1274. #else
  1275.     *n = wideresult;
  1276. #endif /* OS2 */
  1277.     debug(F101,"cmnum xxesc 2 ok","",*n);
  1278.     return(*zp ? -2 : 0);
  1279.     } else if (f) {            /* Not numeric, maybe an expression */
  1280.     char * p;
  1281.     p = evala(zp);
  1282.     if (chknum(p)) {
  1283.         if (radix == 8) {        /* If it's supposed to be octal */
  1284.         zp = ckradix(zp,8,10);    /* convert to decimal */
  1285.         if (!zp) return(-2);
  1286.         if (!strcmp(zp,"-1")) return(-2);
  1287.         }
  1288.         *n = atoi(p);
  1289.         debug(F101,"cmnum exp eval ok","",*n);
  1290.         return(0);
  1291.     } else return(-2);
  1292. #endif /* NOSPL */
  1293.     } else {                /* Not numeric */
  1294.     return(-2);
  1295.     }
  1296. }
  1297.  
  1298. #ifdef CKCHANNELIO
  1299. extern int z_error;
  1300. #endif /* CKCHANNELIO */
  1301.  
  1302. /*  C M O F I  --  Parse the name of an output file  */
  1303.  
  1304. /*
  1305.  Depends on the external function zchko(); if zchko() not available, use
  1306.  cmfld() to parse output file names.
  1307.  
  1308.  Returns:
  1309.    -9 like -2, except message already printed,
  1310.    -3 if no input present when required,
  1311.    -2 if permission would be denied to create the file,
  1312.    -1 if reparse needed,
  1313.     0 or 1 if file can be created, with xp pointing to name.
  1314.     2 if given the name of an existing directory.
  1315. */
  1316. int
  1317. cmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1318.     int x; char *s, *zq;
  1319. #ifdef DOCHKVAR
  1320.     int tries;
  1321. #endif /* DOCHKVAR */
  1322. #ifdef DTILDE
  1323.     char *dirp;
  1324. #endif /* DTILDE */
  1325.  
  1326.     cmfldflgs = 0;
  1327.  
  1328.     if (!xhlp) xhlp = "";
  1329.     if (!xdef) xdef = "";
  1330.  
  1331.     if (*xhlp == NUL) xhlp = "Output file";
  1332.     *xp = "";
  1333.  
  1334.     x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  1335.     debug(F111,"cmofi cmfld returns",s,x);
  1336.     if (x < 0)
  1337.       return(x);
  1338.  
  1339.     s = brstrip(s);            /* Strip enclosing braces */
  1340.     debug(F110,"cmofi 1.5",s,0);
  1341.  
  1342. #ifdef DOCHKVAR
  1343.     tries = 0;
  1344.     {
  1345.     char *p = s;
  1346.     /*
  1347.       This is really ugly.  If we skip conversion the first time through,
  1348.       then variable names like \%a will be used as filenames (e.g. creating
  1349.       a file called %A in the root directory).  If we DON'T skip conversion
  1350.       the first time through, then single backslashes used as directory
  1351.       separators in filenames will be misinterpreted as variable lead-ins.
  1352.       So we prescan to see if it has any variable references.  But this
  1353.       module is not supposed to know anything about variables, functions,
  1354.       etc, so this code does not really belong here, but rather it should
  1355.       be at the same level as zzstring().
  1356.     */
  1357. /*
  1358.   Hmmm, this looks a lot like chkvar() except it that includes \nnn number
  1359.   escapes.  But why?  This makes commands like "mkdir c:\123" impossible.
  1360.   And in fact, "mkdir c:\123" creates a directory called "c:{".  What's worse,
  1361.   rmdir(), which *does* call chkvar(), won't let us remove it.  So let's at
  1362.   least try making cmofi() symmetrical with cmifi()...
  1363. */
  1364. #ifdef COMMENT
  1365.     char * q;
  1366.     while ( (tries == 0) && (p = strchr(p,CMDQ)) ) {
  1367.         q = *(p+1);            /* Char after backslash */
  1368.         if (!q)            /* None, quit */
  1369.           break;
  1370.         if (isupper(q))        /* If letter, convert to lowercase */
  1371.           q = tolower(q);
  1372.         if (isdigit(q)) {        /* If it's a digit, */
  1373.         tries = 1;        /* assume it's a backslash code  */
  1374.         break;
  1375.         }
  1376.         switch (q) {
  1377.           case CMDQ:        /* Double backslash */
  1378.         tries = 1;        /* so call the conversion function */
  1379.         break;
  1380.           case '%':            /* Variable or array reference */
  1381.           case '&':            /* must be followed by letter */
  1382.         if (isalpha(*(p+2)) || (*(p+2) >= '0' && *(p+2) <= '9'))
  1383.           tries = 1;
  1384.         break;
  1385.           case 'm': case 'v': case '$': /* \m(), \v(), \$() */
  1386.         if (*(p+2) == '(')
  1387.           if (strchr(p+2,')'))
  1388.             tries = 1;
  1389.         break;
  1390.           case 'f':            /* \Fname() */
  1391.         if (strchr(p+2,'('))
  1392.           if (strchr(p+2,')'))
  1393.               tries = 1;
  1394.         break;
  1395.           case '{':            /* \{...} */
  1396.         if (strchr(p+2,'}'))
  1397.           tries = 1;
  1398.         break;
  1399.           case 'd': case 'o':    /* Decimal or Octal number */
  1400.             if (isdigit(*(p+2)))
  1401.           tries = 1;
  1402.         break;
  1403.           case 'x':            /* Hex number */
  1404.         if (isdigit(*(p+2)) ||
  1405.             ((*(p+2) >= 'a' && *(p+2) <= 'f') ||
  1406.              ((*(p+2) >= 'A' && *(p+2) <= 'F'))))
  1407.           tries = 1;
  1408.           default:
  1409.         break;
  1410.         }
  1411.         p++;
  1412.     }
  1413. #else
  1414. #ifndef NOSPL
  1415.     if (f) {            /* If a conversion function is given */
  1416.         char *s = p;        /* See if there are any variables in */
  1417.         while (*s) {        /* the string and if so, expand them */
  1418.         if (chkvar(s)) {
  1419.             tries = 1;
  1420.             break;
  1421.         }
  1422.         s++;
  1423.         }
  1424.     }
  1425. #endif /* NOSPL */
  1426. #endif /* COMMENT */
  1427.     }
  1428. #ifdef OS2
  1429. o_again:
  1430. #endif /* OS2 */
  1431.     if (tries == 1)
  1432. #endif /* DOCHKVAR */
  1433.     if (f) {                /* If a conversion function is given */
  1434.     zq = atxbuf;            /* do the conversion. */
  1435.     atxn = CMDBL;
  1436.     if ((x = (*f)(s,&zq,&atxn)) < 0)
  1437.       return(-2);
  1438.     s = atxbuf;
  1439.     if (!*s)            /* Result empty, substitute default */
  1440.       s = xdef;
  1441.     }
  1442.     debug(F111,"cmofi 2",s,x);
  1443.  
  1444. #ifdef DTILDE
  1445.     dirp = tilde_expand(s);        /* Expand tilde, if any, */
  1446.     if (*dirp != '\0') {        /* right in the atom buffer. */
  1447.     if (setatm(dirp,1) < 0) {
  1448.         printf("?Name too long\n");
  1449.         return(-9);
  1450.     }
  1451.     }
  1452.     s = atmbuf;
  1453.     debug(F110,"cmofi 3",s,0);
  1454. #endif /* DTILDE */
  1455.  
  1456.     if (iswild(s)) {
  1457.         printf("?Wildcards not allowed - %s\n",s);
  1458.         return(-2);
  1459.     }
  1460.     debug(F110,"cmofi 4",s,0);
  1461.  
  1462. #ifdef CK_TMPDIR
  1463.     /* isdir() function required for this! */
  1464.     if (isdir(s)) {
  1465.     debug(F110,"cmofi 5: is directory",s,0);
  1466.         *xp = s;
  1467.     return(2);
  1468.     }
  1469. #endif /* CK_TMPDIR */
  1470.  
  1471.     if (strcmp(s,CTTNAM) && (zchko(s) < 0)) { /* OK to write to console */
  1472. #ifdef COMMENT
  1473. #ifdef OS2
  1474. /*
  1475.   We don't try again because we already prescanned the string to see if
  1476.   if it contained anything that could be used by zzstring().
  1477. */
  1478.     if (tries++ < 1)
  1479.       goto o_again;
  1480. #endif /* OS2 */
  1481. #endif /* COMMENT */
  1482. /*
  1483.   Note: there are certain circumstances where zchko() can give a false
  1484.   positive, so don't rely on it to catch every conceivable situation in
  1485.   which the given output file can't be created.  In other words, we print
  1486.   a message and fail here if we KNOW the file can't be created.  If we
  1487.   succeed but the file can't be opened, the code that tries to open the file
  1488.   has to print a message.
  1489. */
  1490.     debug(F110,"cmofi 6: failure",s,0);
  1491. #ifdef CKROOT
  1492.     if (ckrooterr)
  1493.       printf("?Off Limits: %s\n",s);
  1494.     else
  1495. #endif /* CKROOT */
  1496.       printf("?Write permission denied - %s\n",s);
  1497. #ifdef CKCHANNELIO
  1498.     z_error = FX_ACC;
  1499. #endif /* CKCHANNELIO */
  1500.         return(-9);
  1501.     } else {
  1502.     debug(F110,"cmofi 7: ok",s,0);
  1503.         *xp = s;
  1504.         return(x);
  1505.     }
  1506. }
  1507.  
  1508. /*  C M I F I  --  Parse the name of an existing file  */
  1509.  
  1510. /*
  1511.  This function depends on the external functions:
  1512.    zchki()  - Check if input file exists and is readable.
  1513.    zxpand() - Expand a wild file specification into a list.
  1514.    znext()  - Return next file name from list.
  1515.  If these functions aren't available, then use cmfld() to parse filenames.
  1516. */
  1517. /*
  1518.  Returns
  1519.    -4 EOF
  1520.    -3 if no input present when required,
  1521.    -2 if file does not exist or is not readable,
  1522.    -1 if reparse needed,
  1523.     0 or 1 otherwise, with:
  1524.         xp pointing to name,
  1525.         wild = 1 if name contains '*' or '?', 0 otherwise.
  1526. */
  1527.  
  1528. /*
  1529.    C M I O F I  --  Parse an input file OR the name of a nonexistent file.
  1530.  
  1531.    Use this when an existing file is wanted (so we get help, completion, etc),
  1532.    but if a file of the given name does not exist, the name of a new file is
  1533.    accepted.  For example, with the EDIT command (edit an existing file, or
  1534.    create a new file).  Returns -9 if file does not exist.  It is up to the
  1535.    caller to check creatability.
  1536. */
  1537. static int nomsg = 0;
  1538. int
  1539. cmiofi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  1540.     int msgsave, x;
  1541.     msgsave = nomsg;
  1542.     nomsg = 1;
  1543.     x = cmifi2(xhlp,xdef,xp,wild,0,NULL,f,0);
  1544.     nomsg = msgsave;
  1545.     return(x);
  1546. }
  1547.  
  1548. int
  1549. cmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  1550.     return(cmifi2(xhlp,xdef,xp,wild,0,NULL,f,0));
  1551. }
  1552. /*
  1553.   cmifip() is called when we want to supply a path or path list to search
  1554.   in case the filename that the user gives is (a) not absolute, and (b) can't
  1555.   be found as given.  The path string can be the name of a single directory,
  1556.   or a list of directories separated by the PATHSEP character, defined in
  1557.   ckucmd.h.  Look in ckuusr.c and ckuus3.c for examples of usage.
  1558. */
  1559. int
  1560. cmifip(xhlp,xdef,xp,wild,d,path,f)
  1561.     char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; {
  1562.     return(cmifi2(xhlp,xdef,xp,wild,0,path,f,0));
  1563. }
  1564.  
  1565. /*  C M D I R  --  Parse a directory name  */
  1566.  
  1567. /*
  1568.  This function depends on the external functions:
  1569.    isdir(s)  - Check if string s is the name of a directory
  1570.    zchki(s)  - Check if input file s exists and what type it is.
  1571.  If these functions aren't available, then use cmfld() to parse dir names.
  1572.  
  1573.  Returns
  1574.    -9 For all sorts of reasons, after printing appropriate error message.
  1575.    -4 EOF
  1576.    -3 if no input present when required,
  1577.    -2 if out of space or other internal error,
  1578.    -1 if reparse needed,
  1579.     0 or 1, with xp pointing to name, if directory specified,
  1580. */
  1581. int
  1582. cmdir(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1583.     int wild;
  1584.     return(cmifi2(xhlp,xdef,xp,&wild,0,NULL,f,1));
  1585. }
  1586.  
  1587. /* Like CMDIR but includes PATH search */
  1588.  
  1589. int
  1590. cmdirp(xhlp,xdef,xp,path,f) char *xhlp, *xdef, **xp; char * path; xx_strp f; {
  1591.     int wild;
  1592.     return(cmifi2(xhlp,xdef,xp,&wild,0,path,f,1));
  1593. }
  1594.  
  1595. /*
  1596.   cmifi2() is the base filename parser called by cmifi, cmifip, cmdir, etc.
  1597.   Use it directly when you also want to parse a directory or device
  1598.   name as an input file, as in the DIRECTORY command.  Call with:
  1599.     xhlp  -- help message on ?
  1600.     xdef  -- default response
  1601.     xp    -- pointer to result (in our space, must be copied from here)
  1602.     wild  -- flag set upon return to indicate if filespec was wild
  1603.     d     -- 0 to parse files, 1 to parse files or directories
  1604.              Add 2 to inhibit following of symlinks.
  1605.     path  -- search path for files
  1606.     f     -- pointer to string processing function (e.g. to evaluate variables)
  1607.     dirflg -- 1 to parse *only* directories, 0 otherwise
  1608. */
  1609. int
  1610. cmifi2(xhlp,xdef,xp,wild,d,path,f,dirflg)
  1611.     char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; int dirflg; {
  1612.     extern int recursive, diractive, cdactive, dblquo;
  1613.     int i, x, itsadir, xc, expanded = 0, nfiles = 0, children = -1;
  1614.     int qflag = 0;
  1615.     long y;
  1616.     char *sp = NULL, *zq, *np = NULL;
  1617.     char *sv = NULL, *p = NULL;
  1618. #ifdef DTILDE
  1619.     char *dirp;
  1620. #endif /* DTILDE */
  1621.  
  1622. #ifndef NOPARTIAL
  1623. #ifndef OS2
  1624. #ifdef OSK
  1625.     /* This large array is dynamic for OS-9 -- should do for others too... */
  1626.     extern char **mtchs;
  1627. #else
  1628. #ifdef UNIX
  1629.     /* OK, for UNIX too */
  1630.     extern char **mtchs;
  1631. #else
  1632. #ifdef VMS
  1633.     extern char **mtchs;
  1634. #else
  1635.     extern char *mtchs[];
  1636. #endif /* VMS */
  1637. #endif /* UNIX */
  1638. #endif /* OSK */
  1639. #endif /* OS2 */
  1640. #endif /* NOPARTIAL */
  1641.  
  1642.     if (!xhlp) xhlp = "";
  1643.     if (!xdef) xdef = "";
  1644.  
  1645.     nzxopts = 0;            /* zxpand() options */
  1646.     debug(F101,"cmifi d","",d);
  1647.     if (d & 2) {            /* d & 2 means don't follow symlinks */
  1648.     d ^= 2;
  1649.     nzxopts = ZX_NOLINKS;
  1650.     }
  1651.     debug(F101,"cmifi nzxopts","",nzxopts);
  1652.     cmfldflgs = 0;
  1653.     if (path)
  1654.       if (!*path)
  1655.     path = NULL;
  1656.     if (path) {                /* Make a copy we can poke */
  1657.     x = strlen(path);
  1658.     np = (char *) malloc(x + 1);
  1659.     if (np) {
  1660.         strcpy(np, path);
  1661.         path = sp = np;
  1662.     }
  1663.     }
  1664.     debug(F110,"cmifi2 path",path,0);
  1665.  
  1666.     ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  1667.     xdef = cmdefault;
  1668.  
  1669.     inword = 0;                /* Initialize counts & pointers */
  1670.     cc = 0;
  1671.     xc = 0;
  1672.     *xp = "";                /* Pointer to result string */
  1673.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  1674. #ifdef BS_DIRSEP
  1675.     dirnamflg = 1;
  1676.         x = gtword(0);            /* No, get a word */
  1677.     dirnamflg = 0;
  1678. #else
  1679.         x = gtword(0);                  /* No, get a word */
  1680. #endif /* BS_DIRSEP */
  1681.     } else {                /* If so, use default, if any. */
  1682.         if (setatm(xdef,1) < 0) {
  1683.         printf("?Default name too long\n");
  1684.         if (np) free(np);
  1685.         return(-9);
  1686.     }
  1687.     }
  1688.   i_path:
  1689.     *xp = atmbuf;                       /* Point to result. */
  1690.  
  1691.     while (1) {
  1692.         xc += cc;                       /* Count this character. */
  1693.         debug(F111,"cmifi gtword",atmbuf,xc);
  1694.     debug(F101,"cmifi switch x","",x);
  1695.         switch (x) {            /* x = gtword() return code */
  1696.       case -10:
  1697.         if (gtimer() > timelimit) {
  1698. #ifdef IKSD
  1699.                 if (inserver) {
  1700.                     printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  1701.                     doexit(GOOD_EXIT,0);
  1702.                 }
  1703. #endif /* IKSD */
  1704.         if (!quiet) printf("?Timed out\n");
  1705.         return(-10);
  1706.         } else {
  1707.         x = gtword(0);
  1708.         continue;
  1709.         }
  1710.       case -9:
  1711.         printf("Command or field too long\n");
  1712.       case -4:            /* EOF */
  1713.       case -2:            /* Out of space. */
  1714.       case -1:            /* Reparse needed */
  1715.         if (np) free(np);
  1716.         return(x);
  1717.       case 1:            /* CR */
  1718.       case 0:            /* SP */
  1719.         if (xc == 0)        /* If no input... */
  1720.           *xp = xdef;        /* substitute the default */
  1721.         *xp = brstrip(*xp);        /* Strip braces */
  1722.         if (**xp == NUL) {        /* 12 mar 2001 */
  1723.         if (np) free(np);
  1724.         return(-3);
  1725.         }
  1726.         debug(F110,"cmifi brstrip",*xp,0);
  1727. #ifndef NOSPL
  1728.         if (f) {            /* If a conversion function is given */
  1729. #ifdef DOCHKVAR
  1730.         char *s = *xp;        /* See if there are any variables in */
  1731.         int x;
  1732.         while (*s) {        /* the string and if so, expand them */
  1733.             x = chkvar(s);
  1734.             debug(F111,"cmifi chkvar",*xp,x);
  1735.             if (x) {
  1736. #endif /* DOCHKVAR */
  1737.             zq = atxbuf;
  1738.             atxn = CMDBL;
  1739.             if ((*f)(*xp,&zq,&atxn) < 0) {
  1740.                 if (np) free(np);
  1741.                 return(-2);
  1742.             }
  1743.             *xp = atxbuf;
  1744.             if (!atxbuf[0])
  1745.               *xp = xdef;
  1746. #ifdef DOCHKVAR
  1747.             break;
  1748.             }
  1749.             s++;
  1750.         }
  1751. #endif /* DOCHKVAR */
  1752.         }
  1753. #endif /* NOSPL */
  1754.         if (**xp == NUL) {        /* 12 mar 2001 */
  1755.         if (np) free(np);
  1756.         return(-3);
  1757.         }
  1758. #ifdef DTILDE
  1759.         if (dirflg) {
  1760.         dirp = tilde_expand(*xp); /* Expand tilde, if any, */
  1761.         if (*dirp != '\0') {    /* in the atom buffer. */
  1762.             if (setatm(dirp,1) < 0) {
  1763.             printf("Expanded name too long\n");
  1764.             if (np) free(np);
  1765.             return(-9);
  1766.             }
  1767.         }
  1768.         *xp = atmbuf;
  1769.         debug(F110,"cmifi tilde_expand",*xp,0);
  1770.         }
  1771. #endif /* DTILDE */
  1772.         if (!sv) {            /* Only do this once */
  1773.         sv = malloc((int)strlen(*xp)+1); /* Make a safe copy */
  1774.         if (!sv) {
  1775.             printf("?cmifi: malloc error\n");
  1776.             if (np) free(np);
  1777.             return(-9);
  1778.         }
  1779.         strcpy(sv,*xp);
  1780.         debug(F110,"cmifi sv",sv,0);
  1781.         }
  1782.  
  1783. /* This is to get around "cd /" failing because "too many directories match" */
  1784.  
  1785.         expanded = 0;        /* Didn't call zxpand */
  1786. #ifdef datageneral
  1787.         debug(F110,"cmifi isdir 1",*xp,0);
  1788.         {
  1789.         int y; char *s;
  1790.         s = *xp;
  1791.         y = strlen(s);
  1792.         if (y > 1 &&
  1793.             (s[y-1] == ':' ||
  1794.              s[y-1] == '^' ||
  1795.              s[y-1] == '=')
  1796.             )
  1797.           s[y-1] = NUL;
  1798.         }
  1799.         debug(F110,"cmifi isdir 2",*xp,0);
  1800. #endif /*  datageneral */
  1801.  
  1802. #ifdef VMS
  1803.         if (dirflg) {
  1804.         if (!strcmp(*xp,"..")) { /* For UNIXers... */
  1805.             setatm("-",0);
  1806.             *xp = atmbuf;
  1807.         } else if (!strcmp(*xp,".")) {
  1808.             setatm("[]",0);
  1809.             *xp = atmbuf;
  1810.         }
  1811.         }
  1812. #endif /* VMS */
  1813.         itsadir = isdir(*xp);    /* Is it a directory? */
  1814.         debug(F111,"cmifi itsadir",*xp,itsadir);
  1815. #ifdef VMS
  1816.         /* If they said "blah" where "blah.dir" is a directory... */
  1817.         /* change it to [.blah]. */
  1818.         if (!itsadir) {
  1819.         char tmpbuf[600];
  1820.         int flag = 0; char c, * p;
  1821.         p = *xp;
  1822.         while ((c = *p++) && !flag)
  1823.           if (ckstrchr(".[]:*?<>",c))
  1824.             flag = 1;
  1825.         debug(F111,"cmifi VMS dirname flag",*xp,flag);
  1826.         if (!flag) {
  1827.             ckmakmsg(tmpbuf,TMPBUFSIZ,"[.",*xp,"]",NULL);
  1828.             itsadir = isdir(tmpbuf);
  1829.             if (itsadir) {
  1830.             setatm(tmpbuf,0);
  1831.             *xp = atmbuf;
  1832.             }
  1833.             debug(F111,"cmifi VMS dirname flag itsadir",*xp,itsadir);
  1834.         }
  1835.         } else if (itsadir == 1 && *(xp[0]) == '.' && *(xp[1])) {
  1836.         char *p;
  1837.         if (p = malloc(cc + 4)) {
  1838.             ckmakmsg(p,cc+4,"[",*xp,"]",NULL);
  1839.             setatm(p,0);
  1840.             *xp = atmbuf;
  1841.             debug(F110,"cmdir .foo",*xp,0);
  1842.             free(p);
  1843.         }
  1844.         } else if (itsadir == 2 && !diractive) {
  1845.         int x;            /* [FOO]BAR.DIR instead of [FOO.BAR] */
  1846.         char *p;
  1847.         p = malloc(cc + 4);
  1848.         if (p) {
  1849.             x = cvtdir(*xp,p,ATMBL); /* Convert to [FOO.BAR] */
  1850.             if (x > 0) {
  1851.             setatm(p,0);
  1852.             *xp = atmbuf;
  1853.             debug(F110,"cmdir cvtdir",*xp,0);
  1854.             }
  1855.             free(p);
  1856.         }
  1857.         }
  1858. #endif /* VMS */
  1859.  
  1860.         if (dirflg) {        /* Parsing a directory name? */
  1861.         /* Yes, does it contain wildcards? */
  1862.         if (iswild(*xp) ||
  1863.             (diractive && (!strcmp(*xp,".")  || !strcmp(*xp,"..")))
  1864.             ) {
  1865.             nzxopts |= ZX_DIRONLY; /* Match only directory names */
  1866.             if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1867.             if (recursive) nzxopts |= ZX_RECURSE;
  1868.             debug(F101,"cmifi nzxopts 2","",nzxopts);
  1869.             y = nzxpand(*xp,nzxopts);
  1870.             nfiles = y;
  1871.             expanded = 1;
  1872.         } else {
  1873. #ifdef VMS
  1874. /*
  1875.   This is to allow (e.g.) "cd foo", where FOO.DIR;1 is in the
  1876.   current directory.
  1877. */
  1878.             debug(F111,"cmdir itsadir",*xp,itsadir);
  1879.             if (!itsadir) {
  1880.             char *s;
  1881.             int n;
  1882.             s = *xp;
  1883.             n = strlen(s);
  1884.             if (n > 0 &&
  1885. #ifdef COMMENT
  1886.                 *s != '[' && s[n-1] != ']' &&
  1887.                 *s != '<' && s[n-1] != '>' &&
  1888. #else
  1889.                 ckindex("[",s,0,0,1) == 0 &&
  1890.                 ckindex("<",s,0,0,1) == 0 &&
  1891. #endif /* COMMENT */
  1892.                 s[n-1] != ':') {
  1893.                 char * dirbuf = NULL;
  1894.                 dirbuf = (char *)malloc(n+4);
  1895.                 if (dirbuf) {
  1896.                 if (*s == '.')
  1897.                   ckmakmsg(dirbuf,n+4,"[",s,"]",NULL);
  1898.                 else
  1899.                   ckmakmsg(dirbuf,n+4,"[.",s,"]",NULL);
  1900.                 itsadir = isdir(dirbuf);
  1901.                 debug(F111,"cmdir dirbuf",dirbuf,itsadir);
  1902.                 if (itsadir) {
  1903.                     setatm(dirbuf,0);
  1904.                     *xp = atmbuf;
  1905.                     debug(F110,"cmdir new *xp",*xp,0);
  1906.                 }
  1907.                 free(dirbuf);
  1908.                 }
  1909.  
  1910. /* This is to allow CDPATH to work in VMS... */
  1911.  
  1912.             } else if (n > 0) {
  1913.                 char * p; int i, j, k, d;
  1914.                 char rb[2] = "]";
  1915.                 if (p = malloc(x + 8)) {
  1916.                 ckstrncpy(p,*xp,x+8);
  1917.                 i = ckindex(".",p,-1,1,1);
  1918.                 d = ckindex(".dir",p,0,0,0);
  1919.                 j = ckindex("]",p,-1,1,1);
  1920.                 if (j == 0) {
  1921.                     j = ckindex(">",p,-1,1,1);
  1922.                     rb[0] = '>';
  1923.                 }
  1924.                 k = ckindex(":",p,-1,1,1);
  1925.                 if (i < j || i < k) i = 0;
  1926.                 if (d < j || d < k) d = 0;
  1927.                 /* Change [FOO]BAR or [FOO]BAR.DIR */
  1928.                 /* to [FOO.BAR] */
  1929.                 if (j > 0 && j < n) {
  1930.                     p[j-1] = '.';
  1931.                     if (d > 0) p[d-1] = NUL;
  1932.                     ckstrncat(p,rb,x+8);
  1933.                     debug(F110,"cmdir xxx",p,0);
  1934.                 }
  1935.                 itsadir = isdir(p);
  1936.                 debug(F111,"cmdir p",p,itsadir);
  1937.                 if (itsadir) {
  1938.                     setatm(p,0);
  1939.                     *xp = atmbuf;
  1940.                     debug(F110,"cmdir new *xp",*xp,0);
  1941.                 }
  1942.                 free(p);
  1943.                 }
  1944.             }
  1945.             }
  1946. #endif /* VMS */
  1947.             y = (!itsadir) ? 0 : 1;
  1948.             debug(F111,"cmifi y itsadir",*xp,y);
  1949.         }
  1950.         } else {            /* Parsing a filename. */
  1951.         debug(F110,"cmifi *xp pre-zxpand",*xp,0);
  1952. #ifndef COMMENT
  1953.         nzxopts |= (d == 0) ? ZX_FILONLY : 0; /* So always expand. */
  1954.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1955.         if (recursive) nzxopts |= ZX_RECURSE;
  1956.         y = nzxpand(*xp,nzxopts);
  1957. #else
  1958. /* Here we're trying to fix a problem in which a directory name is accepted */
  1959. /* as a filename, but this breaks too many other things. */
  1960.         /* nzxopts = 0; */
  1961.         if (!d) {
  1962.             if (itsadir & !iswild(*xp)) {
  1963.             debug(F100,"cmifi dir when filonly","",0);
  1964.             printf("?Not a regular file: \"%s\"\n",*xp);
  1965.             if (sv) free(sv);
  1966.             if (np) free(np);
  1967.             return(-9);
  1968.             } else {
  1969.             nzxopts |= ZX_FILONLY;
  1970.             if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1971.             if (recursive) nzxopts |= ZX_RECURSE;
  1972.             y = nzxpand(*xp,nzxopts);
  1973.             }
  1974.         }
  1975. #endif /* COMMENT */
  1976.         nfiles = y;
  1977.         debug(F111,"cmifi y nzxpand",*xp,y);
  1978.         debug(F111,"cmifi y atmbuf",atmbuf,itsadir);
  1979.         expanded = 1;
  1980.         }
  1981.         /* domydir() calls zxrewind() so we MUST call nzxpand() here */
  1982.         if (!expanded && diractive) {
  1983.         debug(F110,"cmifi diractive catch-all zxpand",*xp,0);
  1984.         nzxopts |= (d == 0) ? ZX_FILONLY : (dirflg ? ZX_DIRONLY : 0);
  1985.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  1986.         if (recursive) nzxopts |= ZX_RECURSE;
  1987.         y = nzxpand(*xp,nzxopts);
  1988.         nfiles = y;
  1989.         expanded = 1;
  1990.         }
  1991.         *wild = (iswild(sv) || (y > 1)) && (itsadir == 0);
  1992.  
  1993. #ifdef RECURSIVE
  1994.         if (!*wild) *wild = recursive;
  1995. #endif /* RECURSIVE */
  1996.  
  1997.         debug(F111,"cmifi sv wild",sv,*wild);
  1998.         if (dirflg && *wild && cdactive) {
  1999.         printf("?Wildcard matches more than one directory\n");
  2000.         if (sv) free(sv);
  2001.         if (np) free(np);
  2002.         return(-9);
  2003.         }
  2004.         if (itsadir && d && !dirflg) { /* It's a directory and not wild */
  2005.         if (sv) free(sv);    /* and it's ok to parse directories */
  2006.         if (np) free(np);
  2007.         return(x);
  2008.         }
  2009.         if (y == 0) {
  2010.         if (path && !isabsolute(sv)) {
  2011.             char * ptr = path;
  2012.             char c;
  2013.             while (1) {
  2014.             c = *ptr;
  2015.             if (c == PATHSEP || c == NUL) {
  2016.                 if (!*path) {
  2017.                 path = NULL;
  2018.                 break;
  2019.                 }
  2020.                 *ptr = NUL;
  2021. #ifdef UNIX
  2022. /* By definition of CDPATH, an empty member denotes the current directory */
  2023.                 if (!*path)
  2024.                   ckstrncpy(atmbuf,".",ATMBL);
  2025.                 else
  2026. #endif /* UNIX */
  2027.                   ckstrncpy(atmbuf,path,ATMBL);
  2028. #ifdef VMS
  2029.                 atmbuf[ATMBL] = NUL;
  2030. /* If we have a logical name, evaluate it recursively */
  2031.                 if (*(ptr-1) == ':') { /* Logical name ends in : */
  2032.                 char *p; int n;
  2033.                 while (((n = strlen(atmbuf))  > 0) &&
  2034.                        atmbuf[n-1] == ':') {
  2035.                     atmbuf[n-1] = NUL;
  2036.                     for (p = atmbuf; *p; p++)
  2037.                       if (islower(*p)) *p = toupper(*p);
  2038.                     debug(F111,"cmdir CDPATH LN 1",atmbuf,n);
  2039.                     p = getenv(atmbuf);
  2040.                     debug(F110,"cmdir CDPATH LN 2",p,0);
  2041.                     if (!p)
  2042.                       break;
  2043.                     strncpy(atmbuf,p,ATMBL);
  2044.                     atmbuf[ATMBL] = NUL;
  2045.                 }
  2046.                 }
  2047. #else
  2048. #ifdef OS2
  2049.                 if (*(ptr-1) != '\\' && *(ptr-1) != '/')
  2050.                   ckstrncat(atmbuf,"\\",ATMBL);
  2051. #else
  2052. #ifdef UNIX
  2053.                 if (*(ptr-1) != '/')
  2054.                   ckstrncat(atmbuf,"/",ATMBL);
  2055. #else
  2056. #ifdef datageneral
  2057.                 if (*(ptr-1) != ':')
  2058.                   ckstrncat(atmbuf,":",ATMBL);
  2059. #endif /* datageneral */
  2060. #endif /* UNIX */
  2061. #endif /* OS2 */
  2062. #endif /* VMS */
  2063.                 ckstrncat(atmbuf,sv,ATMBL);
  2064.                 debug(F110,"cmifip add path",atmbuf,0);
  2065.                 if (c == PATHSEP) ptr++;
  2066.                 path = ptr;
  2067.                 break;
  2068.             }
  2069.             ptr++;
  2070.             }
  2071.             x = 1;
  2072.             inword = 0;
  2073.             cc = 0;
  2074.             xc = (int) strlen(atmbuf);
  2075.             *xp = "";
  2076.             goto i_path;
  2077.         }
  2078.         if (d) {
  2079.             if (sv) free(sv);
  2080.             if (np) free(np);
  2081.             return(-2);
  2082.         } else {
  2083.             if (!nomsg) {
  2084. #ifdef CKROOT
  2085.             if (ckrooterr)
  2086.               printf("?Off Limits: %s\n",sv);
  2087.             else
  2088. #endif /* CKROOT */
  2089.               printf("?No %s match - %s\n",
  2090.                  dirflg ? "directories" : "files", sv);
  2091.             }
  2092.             if (sv) free(sv);
  2093.             if (np) free(np);
  2094.             return(-9);
  2095.         }
  2096.         } else if (y < 0) {
  2097. #ifdef CKROOT
  2098.         if (ckrooterr)
  2099.           printf("?Off Limits: %s\n",sv);
  2100.         else
  2101. #endif /* CKROOT */
  2102.           printf("?Too many %s match - %s\n",
  2103.              dirflg ? "directories" : "files", sv);
  2104.         if (sv) free(sv);
  2105.         if (np) free(np);
  2106.         return(-9);
  2107.         } else if (*wild || y > 1) {
  2108.         if (sv) free(sv);
  2109.         if (np) free(np);
  2110.         return(x);
  2111.         }
  2112.  
  2113.         /* If not wild, see if it exists and is readable. */
  2114.  
  2115.         debug(F111,"cmifi sv not wild",sv,*wild);
  2116.         if (expanded)
  2117.           znext(*xp);        /* Get first (only?) matching file */
  2118.         if (dirflg)            /* Maybe wild and expanded */
  2119.           itsadir = isdir(*xp);    /* so do this again. */
  2120.         y = dirflg ? itsadir : zchki(*xp); /* Now check accessibility */
  2121.         if (expanded) {
  2122. #ifdef ZXREWIND
  2123.         nfiles = zxrewind();    /* Rewind so next znext() gets 1st */
  2124. #else
  2125.  
  2126.         nzxopts |= dirflg ? ZX_DIRONLY : 0;
  2127.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  2128.         if (recursive) nzxopts |= ZX_RECURSE;
  2129.         nfiles = nzxpand(*xp,nzxopts);
  2130. #endif /* ZXREWIND */
  2131.         }
  2132.         debug(F111,"cmifi nfiles",*xp,nfiles);
  2133.         free(sv);            /* done with this */
  2134.         sv = NULL;
  2135.         if (dirflg && y == 0) {
  2136.         printf("?Not a directory - %s\n",*xp);
  2137. #ifdef CKCHANNELIO
  2138.         z_error = FX_ACC;
  2139. #endif /* CKCHANNELIO */
  2140.         return(-9);
  2141.         } else if (y == -3) {
  2142.         if (!xcmfdb) {
  2143.             if (diractive)
  2144.               /* Don't show filename if we're not allowed to see it */
  2145.               printf("?Read permission denied\n");
  2146.             else
  2147.               printf("?Read permission denied - %s\n",*xp);
  2148.         }
  2149.         if (np) free(np);
  2150. #ifdef CKCHANNELIO
  2151.         z_error = FX_ACC;
  2152. #endif /* CKCHANNELIO */
  2153.         return(xcmfdb ? -6 : -9);
  2154.         } else if (y == -2) {
  2155.         if (!recursive) {
  2156.             if (np) free(np);
  2157.             if (d) return(0);
  2158.             if (!xcmfdb)
  2159.               printf("?File not readable - %s\n",*xp);
  2160. #ifdef CKCHANNELIO
  2161.             z_error = FX_ACC;
  2162. #endif /* CKCHANNELIO */
  2163.             return(xcmfdb ? -6 : -9);
  2164.         }
  2165.         } else if (y < 0) {
  2166.         if (np) free(np);
  2167.         if (!nomsg && !xcmfdb)
  2168.           printf("?File not found - %s\n",*xp);
  2169. #ifdef CKCHANNELIO
  2170.         z_error = FX_FNF;
  2171. #endif /* CKCHANNELIO */
  2172.         return(xcmfdb ? -6 : -9);
  2173.         }
  2174.         if (np) free(np);
  2175.         return(x);
  2176.  
  2177. #ifndef MAC
  2178.       case 2:            /* ESC */
  2179.         debug(F101,"cmifi esc, xc","",xc);
  2180.         if (xc == 0) {
  2181.         if (*xdef) {
  2182.             printf("%s ",xdef); /* If at beginning of field */
  2183. #ifdef GEMDOS
  2184.             fflush(stdout);
  2185. #endif /* GEMDOS */
  2186.             inword = cmflgs = 0;
  2187.             addbuf(xdef);    /* Supply default. */
  2188.             if (setatm(xdef,0) < 0) {
  2189.             printf("Default name too long\n");
  2190.             if (np) free(np);
  2191.             return(-9);
  2192.             }
  2193.         } else {        /* No default */
  2194.             bleep(BP_WARN);
  2195.         }
  2196.         break;
  2197.         }
  2198.         if (**xp == '{') {        /* Did user type opening brace... */
  2199.         *xp = *xp + 1;
  2200.         xc--;
  2201.         cc--;
  2202.         qflag = '}';
  2203.         } else if (dblquo && **xp == '"') {    /* or doublequote? */
  2204.         *xp = *xp + 1;        /* If so ignore it and space past it */
  2205.         xc--;
  2206.         cc--;
  2207.         qflag = '"';
  2208.         }
  2209. #ifndef NOSPL
  2210.         if (f) {            /* If a conversion function is given */
  2211. #ifdef DOCHKVAR
  2212.         char *s = *xp;        /* See if there are any variables in */
  2213.         while (*s) {        /* the string and if so, expand it.  */
  2214.             if (chkvar(s)) {
  2215. #endif /* DOCHKVAR */
  2216.             zq = atxbuf;
  2217.             atxn = CMDBL;
  2218.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) {
  2219.                 if (np) free(np);
  2220.                 return(-2);
  2221.             }
  2222. #ifdef DOCHKVAR
  2223.             /* reduce cc by number of \\ consumed by conversion */
  2224.             /* function (needed for OS/2, where \ is path separator) */
  2225.             cc -= (strlen(*xp) - strlen(atxbuf));
  2226. #endif /* DOCHKVAR */
  2227.             *xp = atxbuf;
  2228.             if (!atxbuf[0]) { /* Result empty, use default */
  2229.                 *xp = xdef;
  2230.                 cc = strlen(xdef);
  2231.             }
  2232. #ifdef DOCHKVAR
  2233.             break;
  2234.             }
  2235.             s++;
  2236.         }
  2237. #endif /* DOCHKVAR */
  2238.         }
  2239. #endif /* NOSPL */
  2240.  
  2241. #ifdef DTILDE
  2242.         if (dirflg && *(*xp) == '~') {
  2243.         debug(F111,"cmifi tilde_expand A",*xp,cc);
  2244.         dirp = tilde_expand(*xp); /* Expand tilde, if any... */
  2245.         if (!dirp) dirp = "";
  2246.         if (*dirp) {
  2247.             int i, xx;
  2248.             char * sp;
  2249.             xc = cc;        /* Length of ~thing */
  2250.             xx = setatm(dirp,0); /* Copy expansion to atom buffer */
  2251.             debug(F111,"cmifi tilde_expand B",atmbuf,cc);
  2252.             if (xx < 0) {
  2253.             printf("Expanded name too long\n");
  2254.             if (np) free(np);
  2255.             return(-9);
  2256.             }
  2257.             debug(F111,"cmifi tilde_expand xc","",xc);
  2258.             for (i = 0; i < xc; i++) {
  2259.             cmdchardel();    /* Back up over ~thing */
  2260.             bp--;
  2261.             }
  2262.             xc = cc;        /* How many new ones we just got */
  2263.             sp = atmbuf;
  2264.             printf("%s",sp);    /* Print them */
  2265.             while ((*bp++ = *sp++)) ;    /* Copy to command buffer */
  2266.             bp--;                    /* Back up over NUL */
  2267.         }
  2268.         *xp = atmbuf;
  2269.         }
  2270. #endif /* DTILDE */
  2271.  
  2272.         sp = *xp + cc;
  2273.  
  2274. #ifdef UNIXOROSK
  2275.         if (!strcmp(atmbuf,"..")) {
  2276.         printf(" ");
  2277.         ckstrncat(cmdbuf," ",CMDBL);
  2278.         cc++;
  2279.         bp++;
  2280.         *wild = 0;
  2281.         *xp = atmbuf;
  2282.         break;
  2283.         } else if (!strcmp(atmbuf,".")) {
  2284.         bleep(BP_WARN);
  2285.         if (np) free(np);
  2286.         return(-1);
  2287.         } else {
  2288.         /* This patches a glitch when user types "./foo<ESC>" */
  2289.         /* in which the next two chars are omitted from the */
  2290.         /* expansion.  There should be a better fix, however, */
  2291.         /* since there is no problem with "../foo<ESC>". */
  2292.         char *p = *xp;
  2293.         if (*p == '.' && *(p+1) == '/')
  2294.           cc -= 2;
  2295.         }
  2296. #endif /* UNIXOROSK */
  2297.  
  2298. #ifdef datageneral
  2299.         *sp++ = '+';        /* Data General AOS wildcard */
  2300. #else
  2301.         *sp++ = '*';        /* Others */
  2302. #endif /* datageneral */
  2303.         *sp-- = '\0';
  2304. #ifdef GEMDOS
  2305.         if (!strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  2306.           strcat(*xp, ".*");    /* abc -> abc*.* */
  2307. #endif /* GEMDOS */
  2308.         /* Add wildcard and expand list. */
  2309. #ifdef COMMENT
  2310.         /* This kills partial completion when ESC given in path segment */
  2311.         nzxopts |= dirflg ? ZX_DIRONLY : (d ? 0 : ZX_FILONLY);
  2312. #else
  2313.         /* nzxopts = 0; */
  2314. #endif /* COMMENT */
  2315.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  2316.         if (recursive) nzxopts |= ZX_RECURSE;
  2317.         y = nzxpand(*xp,nzxopts);
  2318.         nfiles = y;
  2319.         debug(F111,"cmifi nzxpand",*xp,y);
  2320.         if (y > 0) {
  2321. #ifdef OS2
  2322.                 znext(filbuf);        /* Get first */
  2323. #ifdef ZXREWIND
  2324.         zxrewind();        /* Must "rewind" */
  2325. #else
  2326.         nzxpand(*xp,nxzopts);
  2327. #endif /* ZXREWIND */
  2328. #else  /* Not OS2 */
  2329.                 ckstrncpy(filbuf,mtchs[0],CKMAXPATH);
  2330. #endif /* OS2 */
  2331.         } else
  2332.           *filbuf = '\0';
  2333.         filbuf[CKMAXPATH] = NUL;
  2334.         *sp = '\0';            /* Remove wildcard. */
  2335.         debug(F111,"cmifi filbuf",filbuf,y);
  2336.         debug(F111,"cmifi *xp",*xp,cc);
  2337.  
  2338.         *wild = (y > 1);
  2339.         if (y == 0) {
  2340.         if (!nomsg) {
  2341. #ifdef CKROOT
  2342.             if (ckrooterr)
  2343.               printf("?Off Limits: %s\n",atmbuf);
  2344.             else
  2345. #endif /* CKROOT */
  2346.               printf("?No %s match - %s\n",
  2347.                dirflg ? "directories" : "files", atmbuf);
  2348.             if (np) free(np);
  2349.             return(-9);
  2350.         } else {
  2351.             bleep(BP_WARN);
  2352.             if (np) free(np);
  2353.             return(-1);
  2354.         }
  2355.         } else if (y < 0) {
  2356. #ifdef CKROOT
  2357.         if (ckrooterr)
  2358.           printf("?Off Limits: %s\n",atmbuf);
  2359.         else
  2360. #endif /* CKROOT */
  2361.           printf("?Too many %s match - %s\n",
  2362.              dirflg ? "directories" : "files", atmbuf);
  2363.         if (np) free(np);
  2364.         return(-9);
  2365.         } else if (y > 1        /* Not unique */
  2366. #ifndef VMS
  2367.                || (y == 1 && isdir(filbuf)) /* Unique directory */
  2368. #endif /* VMS */
  2369.                ) {
  2370. #ifndef NOPARTIAL
  2371. /* Partial filename completion */
  2372.         int j, k; char c;
  2373.         k = 0;
  2374.         debug(F111,"cmifi partial",filbuf,cc);
  2375. #ifdef OS2
  2376.         {
  2377.             int cur = 0,
  2378.             len = 0,
  2379.             len2 = 0,
  2380.             min = strlen(filbuf),
  2381.             found = 0;
  2382.             char localfn[CKMAXPATH+1];
  2383.  
  2384.             len = min;
  2385.             for (j = 1; j <= y; j++) {
  2386.             znext(localfn);
  2387.             if (dirflg && !isdir(localfn))
  2388.               continue;
  2389.             found = 1;
  2390.             len2 = strlen(localfn);
  2391.             for (cur = cc;
  2392.                  cur < len && cur < len2 && cur <= min;
  2393.                  cur++
  2394.                  ) {
  2395.                             /* OS/2 or Windows, case doesn't matter */
  2396.                 if (tolower(filbuf[cur]) != tolower(localfn[cur]))
  2397.                   break;
  2398.             }
  2399.             if (cur < min)
  2400.               min = cur;
  2401.             }
  2402.             if (!found)
  2403.               min = cc;
  2404.             filbuf[min] = NUL;
  2405.             if (min > cc)
  2406.               k++;
  2407.         }
  2408. #else /* OS2 */
  2409.         for (i = cc; (c = filbuf[i]); i++) {
  2410.             for (j = 1; j < y; j++)
  2411.               if (mtchs[j][i] != c) break;
  2412.             if (j == y) k++;
  2413.             else filbuf[i] = filbuf[i+1] = NUL;
  2414.         }
  2415. #endif /* OS2 */
  2416.  
  2417.  
  2418. #ifndef VMS
  2419.         /* isdir() function required for this! */
  2420.         if (y == 1 && isdir(filbuf)) { /* Dont we already know this? */
  2421.             int len;
  2422.             len = strlen(filbuf);
  2423.             if (len > 0 && len < ATMBL - 1) {
  2424.             if (filbuf[len-1] != dirsep) {
  2425.                 filbuf[len] = dirsep;
  2426.                 filbuf[len+1] = NUL;
  2427.             }
  2428.             }
  2429. /*
  2430.   At this point, before just doing partial completion, we should look first to
  2431.   see if the given directory does indeed have any subdirectories (dirflg) or
  2432.   files (!dirflg); if it doesn't we should do full completion.  Otherwise, the
  2433.   result looks funny to the user and "?" blows up the command for no good
  2434.   reason.
  2435. */
  2436.             {
  2437.             int flags = 0;
  2438.             filbuf[len+1] = '*';
  2439.             filbuf[len+2] = NUL;
  2440.             if (dirflg) flags = ZX_DIRONLY;
  2441.             children = nzxpand(filbuf,flags);
  2442.             debug(F111,"cmifi children",filbuf,children);
  2443.             filbuf[len+1] = NUL;
  2444.             nzxpand(filbuf,flags); /* Restore previous list */
  2445.             if (children == 0)
  2446.               goto NOSUBDIRS;
  2447.             }
  2448.             if (len + 1 > cc)
  2449.               k++;
  2450.         }
  2451.                 /* Add doublequotes if there are spaces in the name */
  2452.         {
  2453.             int x;
  2454.             if (qflag) {
  2455.             x = (qflag == '}'); /* (or braces) */
  2456.             } else {
  2457.             x = !dblquo;
  2458.             }
  2459.             if (filbuf[0] != '"' && filbuf[0] != '{')
  2460.               k = dquote(filbuf,ATMBL,x);
  2461.         }
  2462. #endif /* VMS */
  2463.         debug(F111,"cmifi REPAINT filbuf",filbuf,k);
  2464.         if (k > 0) {        /* Got more characters */
  2465.             debug(F101,"cmifi REPAINT cc","",cc);
  2466.             debug(F101,"cmifi REPAINT xc","",xc);
  2467.             debug(F110,"cmifi REPAINT bp-cc",bp-cc,0);
  2468.             debug(F110,"cmifi REPAINT bp-xc",bp-xc,0);
  2469.             sp = filbuf + cc;    /* Point to new ones */
  2470.             if (qflag || strncmp(filbuf,bp-cc,cc)) { /* Repaint? */
  2471.             int x;
  2472.             x = cc;
  2473.             if (qflag) x++;
  2474.             for (i = 0; i < x; i++) {
  2475.                 cmdchardel(); /* Back up over old partial spec */
  2476.                 bp--;
  2477.             }
  2478.             sp = filbuf;    /* Point to new word start */
  2479.             debug(F110,"cmifi erase ok",sp,0);
  2480.             }
  2481.             cc = k;        /* How many new ones we just got */
  2482.             printf("%s",sp);    /* Print them */
  2483.             while ((*bp++ = *sp++)) ;    /* Copy to command buffer */
  2484.             bp--;                    /* Back up over NUL */
  2485.             debug(F110,"cmifi partial cmdbuf",cmdbuf,0);
  2486.             if (setatm(filbuf,0) < 0) {
  2487.             printf("?Partial name too long\n");
  2488.             if (np) free(np);
  2489.             return(-9);
  2490.             }
  2491.             debug(F111,"cmifi partial atmbuf",atmbuf,cc);
  2492.             *xp = atmbuf;
  2493.         }
  2494. #endif /* NOPARTIAL */
  2495.         bleep(BP_WARN);
  2496.         } else {            /* Unique, complete it.  */
  2497. #ifndef VMS
  2498. #ifdef CK_TMPDIR
  2499.         /* isdir() function required for this! */
  2500.           NOSUBDIRS:
  2501.         debug(F111,"cmifi unique",filbuf,children);
  2502.         if (isdir(filbuf) && children > 0) {
  2503.             int len;
  2504.             len = strlen(filbuf);
  2505.             if (len > 0 && len < ATMBL - 1) {
  2506.             if (filbuf[len-1] != dirsep) {
  2507.                 filbuf[len] = dirsep;
  2508.                 filbuf[len+1] = NUL;
  2509.             }
  2510.             }
  2511.             sp = filbuf + cc;
  2512.             bleep(BP_WARN);
  2513.             printf("%s",sp);
  2514.             cc++;
  2515.             while ((*bp++ = *sp++)) ;
  2516.             bp--;
  2517.             if (setatm(filbuf,0) < 0) {
  2518.             printf("?Directory name too long\n");
  2519.             if (np) free(np);
  2520.             return(-9);
  2521.             }
  2522.             debug(F111,"cmifi directory atmbuf",atmbuf,cc);
  2523.             *xp = atmbuf;
  2524.         } else {        /* Not a directory or dirflg */
  2525. #endif /* CK_TMPDIR */
  2526. #endif /* VMS */
  2527. #ifndef VMS                /* VMS dir names are special */
  2528. #ifndef datageneral            /* VS dirnames must not end in ":" */
  2529.             if (dirflg) {
  2530.             int len;
  2531.             len = strlen(filbuf);
  2532.             if (len > 0 && len < ATMBL - 1) {
  2533.                 if (filbuf[len-1] != dirsep) {
  2534.                 filbuf[len] = dirsep;
  2535.                 filbuf[len+1] = NUL;
  2536.                 }
  2537.             }
  2538.             }
  2539. #endif /* datageneral */
  2540. #endif /* VMS */
  2541.             sp = filbuf + cc;    /* Point past what user typed. */
  2542.             {
  2543.             int x;
  2544.             if (qflag) {
  2545.                 x = (qflag == '}');
  2546.             } else {
  2547.                 x = !dblquo;
  2548.             }
  2549.             if (filbuf[0] != '"' && filbuf[0] != '{')
  2550.               dquote(filbuf,ATMBL,x);
  2551.             }
  2552.             if (qflag || strncmp(filbuf,bp-cc,cc)) { /* Repaint? */
  2553.             int x;
  2554.             x = cc;
  2555.             if (qflag) x++;
  2556.             for (i = 0; i < x; i++) {
  2557.                 cmdchardel(); /* Back up over old partial spec */
  2558.                 bp--;
  2559.             }
  2560.             sp = filbuf;    /* Point to new word start */
  2561.             debug(F111,"cmifi after erase sp=",sp,cc);
  2562.             }
  2563.             printf("%s ",sp);    /* Print the completed name. */
  2564. #ifdef GEMDOS
  2565.             fflush(stdout);
  2566. #endif /* GEMDOS */
  2567.             addbuf(sp);        /* Add the characters to cmdbuf. */
  2568.             if (setatm(filbuf,0) < 0) { /* And to atmbuf. */
  2569.             printf("?Completed name too long\n");
  2570.             if (np) free(np);
  2571.             return(-9);
  2572.             }
  2573.             inword = cmflgs = 0;
  2574.             *xp = brstrip(atmbuf); /* Return pointer to atmbuf. */
  2575.             if (dirflg && !isdir(*xp)) {
  2576.             printf("?Not a directory - %s\n", filbuf);
  2577.             if (np) free(np);
  2578.             return(-9);
  2579.             }
  2580.             if (np) free(np);
  2581.             return(0);
  2582. #ifndef VMS
  2583. #ifdef CK_TMPDIR
  2584.         }
  2585. #endif /* CK_TMPDIR */
  2586. #endif /* VMS */
  2587.         }
  2588.         break;
  2589.  
  2590.       case 3:            /* Question mark - file menu wanted */
  2591.         if (*xhlp == NUL)
  2592.           printf(dirflg ? " Directory name" : " Input file specification");
  2593.         else
  2594.           printf(" %s",xhlp);
  2595. #ifdef GEMDOS
  2596.         fflush(stdout);
  2597. #endif /* GEMDOS */
  2598.         /* If user typed an opening quote or brace, just skip past it */
  2599.  
  2600.         if (**xp == '"' || **xp == '{') {
  2601.         *xp = *xp + 1;
  2602.         xc--;
  2603.         cc--;
  2604.         }
  2605. #ifndef NOSPL
  2606.         if (f) {            /* If a conversion function is given */
  2607. #ifdef DOCHKVAR
  2608.         char *s = *xp;        /* See if there are any variables in */
  2609.         while (*s) {        /* the string and if so, expand them */
  2610.             if (chkvar(s)) {
  2611. #endif /* DOCHKVAR */
  2612.             zq = atxbuf;
  2613.             atxn = CMDBL;
  2614.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) {
  2615.                 if (np) free(np);
  2616.                 return(-2);
  2617.             }
  2618. #ifdef DOCHKVAR
  2619.             /* reduce cc by number of \\ consumed by conversion */
  2620.             /* function (needed for OS/2, where \ is path separator) */
  2621.             cc -= (strlen(*xp) - strlen(atxbuf));
  2622. #endif /* DOCHKVAR */
  2623.             *xp = atxbuf;
  2624. #ifdef DOCHKVAR
  2625.             break;
  2626.             }
  2627.             s++;
  2628.         }
  2629. #endif /* DOCHKVAR */
  2630.         }
  2631. #endif /* NOSPL */
  2632.         debug(F111,"cmifi ? *xp, cc",*xp,cc);
  2633.         sp = *xp + cc;        /* Insert "*" at end */
  2634. #ifdef datageneral
  2635.         *sp++ = '+';        /* Insert +, the DG wild card */
  2636. #else
  2637.         *sp++ = '*';
  2638. #endif /* datageneral */
  2639.         *sp-- = '\0';
  2640. #ifdef GEMDOS
  2641.         if (! strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  2642.           strcat(*xp, ".*");    /* abc -> abc*.* */
  2643. #endif /* GEMDOS */
  2644.         debug(F110,"cmifi ? wild",*xp,0);
  2645.  
  2646.         nzxopts |= dirflg ? ZX_DIRONLY : (d ? 0 : ZX_FILONLY);
  2647.  
  2648.         debug(F101,"cmifi matchdot","",matchdot);
  2649.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  2650.         if (recursive) nzxopts |= ZX_RECURSE;
  2651.         y = nzxpand(*xp,nzxopts);
  2652.         nfiles = y;
  2653.         *sp = '\0';
  2654.         if (y == 0) {
  2655.         if (nomsg) {
  2656.             printf(": %s\n",atmbuf);
  2657.             printf("%s%s",cmprom,cmdbuf);
  2658.             fflush(stdout);
  2659.             if (np) free(np);
  2660.             return(-1);
  2661.         } else {
  2662. #ifdef CKROOT
  2663.             if (ckrooterr)
  2664.               printf("?Off Limits: %s\n",atmbuf);
  2665.             else
  2666. #endif /* CKROOT */
  2667.               printf("?No %s match - %s\n",
  2668.                  dirflg ? "directories" : "files", atmbuf);
  2669.             if (np) free(np);
  2670.             return(-9);
  2671.         }
  2672.         } else if (y < 0) {
  2673. #ifdef CKROOT
  2674.         if (ckrooterr)
  2675.           printf("?Off Limits: %s\n",atmbuf);
  2676.         else
  2677. #endif /* CKROOT */
  2678.           printf("?Too many %s match - %s\n",
  2679.              dirflg ? "directories" : "files", atmbuf);
  2680.         if (np) free(np);
  2681.         return(-9);
  2682.         } else {
  2683.         printf(", one of the following:\n");
  2684.         if (filhelp((int)y,"","",1,dirflg) < 0) {
  2685.             if (np) free(np);
  2686.             return(-9);
  2687.         }
  2688.         }
  2689.         printf("%s%s",cmprom,cmdbuf);
  2690.         fflush(stdout);
  2691.         break;
  2692. #endif /* MAC */
  2693.         }
  2694. #ifdef BS_DIRSEP
  2695.         dirnamflg = 1;
  2696.         x = gtword(0);                  /* No, get a word */
  2697.     dirnamflg = 0;
  2698. #else
  2699.         x = gtword(0);                  /* No, get a word */
  2700. #endif /* BS_DIRSEP */
  2701.     *xp = atmbuf;
  2702.     }
  2703. }
  2704.  
  2705. /*  C M F L D  --  Parse an arbitrary field  */
  2706. /*
  2707.  Returns
  2708.    -3 if no input present when required,
  2709.    -2 if field too big for buffer,
  2710.    -1 if reparse needed,
  2711.     0 otherwise, xp pointing to string result.
  2712. */
  2713. int
  2714. cmfld(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  2715.     int x, xc;
  2716.     char *zq;
  2717.  
  2718.     inword = 0;                /* Initialize counts & pointers */
  2719.     cc = 0;
  2720.     xc = 0;
  2721.     *xp = "";
  2722.  
  2723.     debug(F110,"cmfld xdef 1",xdef,0);
  2724.  
  2725.     if (!xhlp) xhlp = "";
  2726.     if (!xdef) xdef = "";
  2727.     ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  2728.     xdef = cmdefault;
  2729.  
  2730.     debug(F111,"cmfld xdef 2",xdef,cmflgs);
  2731.     debug(F111,"cmfld atmbuf 1",atmbuf,xc);
  2732.  
  2733.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  2734.         x = gtword(0);                  /* No, get a word */
  2735.     } else {
  2736.     if (setatm(xdef,0) < 0) {    /* If so, use default, if any. */
  2737.         printf("?Default too long\n");
  2738.         return(-9);
  2739.     }
  2740.     }
  2741.     *xp = atmbuf;                       /* Point to result. */
  2742.     debug(F111,"cmfld atmbuf 2",atmbuf,cmflgs);
  2743.  
  2744.     while (1) {
  2745.         xc += cc;                       /* Count the characters. */
  2746.         debug(F111,"cmfld gtword",atmbuf,xc);
  2747.         debug(F101,"cmfld x","",x);
  2748.         switch (x) {
  2749.       case -9:
  2750.         printf("Command or field too long\n");
  2751.       case -4:            /* EOF */
  2752.       case -3:            /* Empty. */
  2753.       case -2:            /* Out of space. */
  2754.       case -1:            /* Reparse needed */
  2755.         return(x);
  2756.       case 1:            /* CR */
  2757.       case 0:            /* SP */
  2758.         debug(F111,"cmfld 1",atmbuf,xc);
  2759.         if (xc == 0) {        /* If no input, return default. */
  2760.         if (setatm(xdef,0) < 0) {
  2761.             printf("?Default too long\n");
  2762.             return(-9);
  2763.         }
  2764.         }
  2765.         *xp = atmbuf;        /* Point to what we got. */
  2766.         debug(F111,"cmfld 2",atmbuf,(f) ? 1 : 0);
  2767.         if (f) {            /* If a conversion function is given */
  2768.         zq = atxbuf;        /* employ it now. */
  2769.         atxn = CMDBL;
  2770.         if ((*f)(*xp,&zq,&atxn) < 0)
  2771.           return(-2);
  2772.         if (setatm(atxbuf,1) < 0) { /* Replace by new value */
  2773.             printf("Value too long\n");
  2774.             return(-9);
  2775.         }
  2776.         *xp = atmbuf;
  2777.         }
  2778.         debug(F111,"cmfld 3",atmbuf,xc);
  2779.         if (**xp == NUL) {        /* If variable evaluates to null */
  2780.         if (setatm(xdef,0) < 0) {
  2781.             printf("?Default too long\n");
  2782.             return(-9);
  2783.         }
  2784.         if (**xp == NUL) x = -3; /* If still empty, return -3. */
  2785.         }
  2786.         debug(F111,"cmfld returns",*xp,x);
  2787.         return(x);
  2788.       case 2:            /* ESC */
  2789.         if (xc == 0 && *xdef) {
  2790.         printf("%s ",xdef); /* If at beginning of field, */
  2791. #ifdef GEMDOS
  2792.         fflush(stdout);
  2793. #endif /* GEMDOS */
  2794.         addbuf(xdef);        /* Supply default. */
  2795.         inword = cmflgs = 0;
  2796.         if (setatm(xdef,0) < 0) {
  2797.             printf("?Default too long\n");
  2798.             return(-9);
  2799.         } else            /* Return as if whole field */
  2800.           return(0);        /* typed, followed by space. */
  2801.         } else {
  2802.         bleep(BP_WARN);
  2803.         }
  2804.         break;
  2805.       case 3:            /* Question mark */
  2806.         debug(F110,"cmfld QUESTIONMARK",cmdbuf,0);
  2807.         if (*xhlp == NUL)
  2808.           printf(" Please complete this field");
  2809.         else
  2810.           printf(" %s",xhlp);
  2811.         printf("\n%s%s",cmprom,cmdbuf);
  2812.         fflush(stdout);
  2813.         break;
  2814.         }
  2815.     debug(F111,"cmfld gtword A x",cmdbuf,x);
  2816.     x = gtword(0);
  2817.     debug(F111,"cmfld gtword B x",cmdbuf,x);
  2818.     }
  2819. }
  2820.  
  2821.  
  2822. /*  C M T X T  --  Get a text string, including confirmation  */
  2823.  
  2824. /*
  2825.   Print help message 'xhlp' if ? typed, supply default 'xdef' if null
  2826.   string typed.  Returns:
  2827.  
  2828.    -1 if reparse needed or buffer overflows.
  2829.     1 otherwise.
  2830.  
  2831.   with cmflgs set to return code, and xp pointing to result string.
  2832. */
  2833. int
  2834. cmtxt(xhlp,xdef,xp,f) char *xhlp; char *xdef; char **xp; xx_strp f; {
  2835.  
  2836.     int x, i;
  2837.     char *xx, *zq;
  2838.     static int xc;
  2839.  
  2840.     if (!xhlp) xhlp = "";
  2841.     if (!xdef) xdef = "";
  2842.  
  2843.     cmfldflgs = 0;
  2844.  
  2845.     cmdefault[0] = NUL;
  2846.     if (*xdef)
  2847.       ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  2848.     xdef = cmdefault;
  2849.  
  2850.     debug(F101,"cmtxt cmflgs","",cmflgs);
  2851.     inword = 0;                /* Start atmbuf counter off at 0 */
  2852.     cc = 0;
  2853.     if (cmflgs == -1) {                 /* If reparsing, */
  2854.     *xp = pp;
  2855.         xc = (int)strlen(*xp);        /* get back the total text length, */
  2856.     bp = *xp;            /* and back up the pointers. */
  2857.     np = *xp;
  2858.     pp = *xp;
  2859.     } else {                            /* otherwise, */
  2860.     /* debug(F100,"cmtxt: fresh start","",0); */
  2861.         *xp = "";                       /* start fresh. */
  2862.         xc = 0;
  2863.     }
  2864.     *atmbuf = NUL;                      /* And empty the atom buffer. */
  2865.     rtimer();                /* Reset timer */
  2866.     if ((x = cmflgs) != 1) {
  2867.     int done = 0;
  2868.     while (!done) {
  2869.         x = gtword(0);        /* Get first word. */
  2870.         *xp = pp;            /* Save pointer to it. */
  2871.         /* debug(F111,"cmtxt:",*xp,cc); */
  2872.         if (x == -10) {
  2873.         if (gtimer() > timelimit) {
  2874.             if (!quiet) printf("?Timed out\n");
  2875.             return(x);
  2876.         }
  2877.         } else
  2878.           done = 1;
  2879.     }
  2880.     }
  2881.     while (1) {                /* Loop for each word in text. */
  2882.         xc += cc;                       /* Char count for all words. */
  2883.         /* debug(F111,"cmtxt gtword",atmbuf,xc); */
  2884.         /* debug(F101,"cmtxt x","",x); */
  2885.         switch (x) {
  2886.       case -10:
  2887.         if (gtimer() > timelimit) {
  2888. #ifdef IKSD
  2889.                 extern int inserver;
  2890.                 if (inserver) {
  2891.                     printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  2892.                     doexit(GOOD_EXIT,0);
  2893.                 }
  2894. #endif /* IKSD */
  2895.         if (!quiet) printf("?Timed out\n");
  2896.         return(-10);
  2897.         } else {
  2898.         x = gtword(0);
  2899.         continue;
  2900.         }
  2901.       case -9:            /* Buffer overflow */
  2902.         printf("Command or field too long\n");
  2903.       case -4:            /* EOF */
  2904. #ifdef MAC
  2905.       case -3:            /* Quit/Timeout */
  2906. #endif /* MAC */
  2907.       case -2:            /* Overflow */
  2908.       case -1:            /* Deletion */
  2909.         return(x);
  2910.       case 0:            /* Space */
  2911.         xc++;            /* Just count it */
  2912.         break;
  2913.       case 1:            /* CR or LF */
  2914.         if (xc == 0) *xp = xdef;
  2915.         if (f) {            /* If a conversion function is given */
  2916.         char * sx = atxbuf;
  2917.         zq = atxbuf;        /* Point to the expansion buffer */
  2918.         atxn = CMDBL;        /* specify its length */
  2919.         debug(F111,"cmtxt calling (*f)",*xp,atxbuf);
  2920.         if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  2921.         sx = atxbuf;
  2922. #ifndef COMMENT
  2923.         cc = 0;
  2924.         while (*sx++) cc++;    /* (faster than calling strlen) */
  2925. #else
  2926.         cc = (int)strlen(atxbuf);
  2927. #endif /* COMMENT */
  2928.         /* Should be equal to (CMDBL - atxn) but isn't always. */
  2929.         /* Why not? */
  2930.         if (cc < 1) {        /* Nothing in expansion buffer? */
  2931.             *xp = xdef;        /* Point to default string instead. */
  2932. #ifndef COMMENT
  2933.             sx = xdef;
  2934.             while (*sx++) cc++;    /* (faster than calling strlen) */
  2935. #else
  2936.             cc = strlen(xdef);
  2937. #endif /* COMMENT */
  2938.         } else {        /* Expansion function got something */
  2939.             *xp = atxbuf;    /* return pointer to it. */
  2940.         }
  2941.         debug(F111,"cmtxt (*f)",*xp,cc);
  2942.         } else {            /* No expansion function */
  2943. #ifndef COMMENT
  2944.         /* Avoid a strlen() call */
  2945.         xx = *xp;
  2946.         cc = 0;
  2947.         while (*xx++) cc++;
  2948. #else
  2949.         /* NO!  xc is apparently not always set appropriately */
  2950.         cc = xc;
  2951. #endif /* COMMENT */
  2952.         }
  2953.         xx = *xp;
  2954. #ifdef COMMENT
  2955.         /* strlen() no longer needed */
  2956.         for (i = (int)strlen(xx) - 1; i > 0; i--)
  2957. #else
  2958.         for (i = cc - 1; i > 0; i--)
  2959. #endif /* COMMENT */
  2960.           if (xx[i] != SP)        /* Trim trailing blanks */
  2961.         break;
  2962.           else
  2963.         xx[i] = NUL;
  2964.         return(x);
  2965.       case 2:            /* ESC */
  2966.         if (xc == 0) {        /* Nothing typed yet */
  2967.         if (*xdef) {        /* Have a default for this field? */
  2968.             printf("%s ",xdef);    /* Yes, supply it */
  2969.             inword = cmflgs = 0;
  2970. #ifdef GEMDOS
  2971.             fflush(stdout);
  2972. #endif /* GEMDOS */
  2973.             cc = addbuf(xdef);
  2974.         } else bleep(BP_WARN);    /* No default */
  2975.         } else {            /* Already in field */
  2976.         int x; char *p;
  2977.         x = strlen(atmbuf);
  2978.         if (ckstrcmp(atmbuf,xdef,x,0)) {    /* Matches default? */
  2979.             bleep(BP_WARN);                /* No */
  2980.         } else if ((int)strlen(xdef) > x) { /* Yes */
  2981.             p = xdef + x;
  2982.             printf("%s ", p);
  2983. #ifdef GEMDOS
  2984.             fflush(stdout);
  2985. #endif /* GEMDOS */
  2986.             addbuf(p);
  2987.             inword = cmflgs = 0;
  2988.             debug(F110,"cmtxt: addbuf",cmdbuf,0);
  2989.         } else {
  2990.             bleep(BP_WARN);
  2991.         }
  2992.         }
  2993.         break;
  2994.       case 3:            /* Question Mark */
  2995.         if (*xhlp == NUL)
  2996.           printf(" Text string");
  2997.         else
  2998.           printf(" %s",xhlp);
  2999.         printf("\n%s%s",cmprom,cmdbuf);
  3000.         fflush(stdout);
  3001.         break;
  3002.       default:
  3003.         printf("?Unexpected return code from gtword() - %d\n",x);
  3004.         return(-2);
  3005.         }
  3006.         x = gtword(0);
  3007.     }
  3008. }
  3009.  
  3010. /*  C M K E Y  --  Parse a keyword  */
  3011.  
  3012. /*
  3013.  Call with:
  3014.    table    --  keyword table, in 'struct keytab' format;
  3015.    n        --  number of entries in table;
  3016.    xhlp     --  pointer to help string;
  3017.    xdef     --  pointer to default keyword;
  3018.    f        --  processing function (e.g. to evaluate variables)
  3019.    pmsg     --  0 = don't print error messages
  3020.                 1 = print error messages
  3021.                 2 = include CM_HLP keywords even if invisible
  3022.                 3 = 1+2
  3023.                 4 = parse a switch (keyword possibly ending in : or =)
  3024.  Returns:
  3025.    -3       --  no input supplied and no default available
  3026.    -2       --  input doesn't uniquely match a keyword in the table
  3027.    -1       --  user deleted too much, command reparse required
  3028.     n >= 0  --  value associated with keyword
  3029. */
  3030. int
  3031. cmkey(table,n,xhlp,xdef,f)
  3032. /* cmkey */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  3033.     return(cmkey2(table,n,xhlp,xdef,"",f,1));
  3034. }
  3035. int
  3036. cmkeyx(table,n,xhlp,xdef,f)
  3037. /* cmkeyx */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  3038.     return(cmkey2(table,n,xhlp,xdef,"",f,0));
  3039. }
  3040. int
  3041. cmswi(table,n,xhlp,xdef,f)
  3042. /* cmswi */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  3043.     return(cmkey2(table,n,xhlp,xdef,"",f,4));
  3044. }
  3045.  
  3046. int
  3047. cmkey2(table,n,xhlp,xdef,tok,f,pmsg)
  3048.     struct keytab table[];
  3049.     int n;
  3050.     char *xhlp, *xdef;
  3051.     char *tok;
  3052.     xx_strp f;
  3053.     int pmsg;
  3054. { /* cmkey2 */
  3055.     extern int havetoken;
  3056.     int i, tl, y, z = 0, zz, xc, wordlen = 0, cmswitch;
  3057.     char *xp, *zq;
  3058.  
  3059.     if (!xhlp) xhlp = "";
  3060.     if (!xdef) xdef = "";
  3061.  
  3062.     cmfldflgs = 0;
  3063.     if (!table) {
  3064.     printf("?Keyword table missing\n");
  3065.     return(-9);
  3066.     }
  3067.     tl = (int)strlen(tok);
  3068.  
  3069.     inword = xc = cc = 0;        /* Clear character counters. */
  3070.     cmswitch = pmsg & 4;        /* Flag for parsing a switch */
  3071.  
  3072.     debug(F101,"cmkey: pmsg","",pmsg);
  3073.     debug(F101,"cmkey: cmflgs","",cmflgs);
  3074.     debug(F101,"cmkey: cmswitch","",cmswitch);
  3075.     /* debug(F101,"cmkey: cmdbuf","",cmdbuf);*/
  3076.  
  3077.     ppvnambuf[0] = NUL;
  3078.  
  3079.     if ((zz = cmflgs) == 1) {        /* Command already entered? */
  3080.     if (setatm(xdef,0) < 0) {    /* Yes, copy default into atom buf */
  3081.         printf("?Default too long\n");
  3082.         return(-9);
  3083.     }
  3084.         rtimer();             /* Reset timer */
  3085.     } else {
  3086.         rtimer();             /* Reset timer */
  3087.         zz = gtword((pmsg == 4) ? 1 : 0);/* Otherwise get a command word */
  3088.     }
  3089.  
  3090.     debug(F101,"cmkey table length","",n);
  3091.     debug(F101,"cmkey cmflgs","",cmflgs);
  3092.     debug(F101,"cmkey cc","",cc);
  3093.  
  3094.     while (1) {
  3095.     xc += cc;
  3096.     debug(F111,"cmkey gtword xc",atmbuf,xc);
  3097.     debug(F101,"cmkey gtword zz","",zz);
  3098.  
  3099.     switch (zz) {
  3100.       case -10:            /* Timeout */
  3101.         if (gtimer() < timelimit) {
  3102.         zz = gtword((pmsg == 4) ? 1 : 0);
  3103.         continue;
  3104.         } else {
  3105. #ifdef IKSD
  3106.                 extern int inserver;
  3107.                 if (inserver) {
  3108.                     printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  3109.                     doexit(GOOD_EXIT,0);
  3110.                 }
  3111. #endif /* IKSD */
  3112.         return(-10);
  3113.             }
  3114.       case -5:
  3115.         return(cmflgs = 0);
  3116.       case -9:
  3117.         printf("Command or field too long\n");
  3118.       case -4:            /* EOF */
  3119.       case -3:            /* Null Command/Quit/Timeout */
  3120.       case -2:            /* Buffer overflow */
  3121.       case -1:            /* Or user did some deleting. */
  3122.         return(cmflgs = zz);
  3123.  
  3124.  
  3125.       case 1:            /* CR */
  3126.       case 0:            /* User terminated word with space */
  3127.       case 4:            /* or switch ending in : or = */
  3128.         wordlen = cc;        /* Length if no conversion */
  3129.         if (cc == 0) {        /* Supply default if we got nothing */
  3130.         if ((wordlen = setatm(xdef,(zz == 4) ? 2 : 0)) < 0) {
  3131.             printf("?Default too long\n");
  3132.             return(-9);
  3133.         }
  3134.         }
  3135.         if (zz == 1 && cc == 0)    /* Required field missing */
  3136.           return(-3);
  3137.  
  3138.         if (f) {            /* If a conversion function is given */
  3139.         char * p2;
  3140.         zq = atxbuf;        /* apply it */
  3141.         p2 = atxbuf;
  3142.         atxn = CMDBL;
  3143.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  3144.         debug(F110,"cmkey atxbuf after *f",atxbuf,0);
  3145.         if (!*p2)        /* Supply default if we got nothing */
  3146.           p2 = xdef;
  3147.         ckstrncpy(ppvnambuf,atmbuf,PPVLEN);
  3148.         if ((wordlen = setatm(p2,(zz == 4) ? 2 : 0)) < 0) {
  3149.             printf("Evaluated keyword too long\n");
  3150.             return(-9);
  3151.         }
  3152. #ifdef M_UNGW
  3153.         /*
  3154.           This bit lets us save more than one "word".
  3155.           For example, "define \%x echo one two three", "\%x".
  3156.           It works too, but it breaks labels, and therefore
  3157.           WHILE and FOR loops, etc.
  3158.         */
  3159.         if (p2[wordlen] >= SP) {
  3160.             p2 += wordlen;
  3161.             while (*p2 == SP) p2++;
  3162.             if (*p2) {
  3163.             ungword();
  3164.             pp = p2;
  3165.             }
  3166.         }
  3167. #endif /* M_UNGW */
  3168.         }
  3169.         if (cmswitch && *atmbuf != '/') {
  3170.         if (pmsg & 1) {
  3171.             bleep(BP_FAIL);
  3172.                     printf("?Not a switch - %s\n",atmbuf);
  3173.         }
  3174.         cmflgs = -2;
  3175.         return(-6);
  3176.         }
  3177.         if (cmswitch) {
  3178.         int i;
  3179.         for (i = 0; i < wordlen; i++) {
  3180.             if (atmbuf[i] == ':' || atmbuf[i] == '=') {
  3181.             brkchar = atmbuf[i];
  3182.             atmbuf[i] = NUL;
  3183.             break;
  3184.             }
  3185.         }
  3186.         }
  3187.  
  3188. #ifdef TOKPRECHECK
  3189. /* This was an effective optimization but it breaks sometimes on labels. */
  3190.         if (tl && !isalpha(atmbuf[0])) { /* Precheck for token */
  3191.         for (i = 0; i < tl; i++) { /* Save function call to ckstrchr */
  3192.             if (tok[i] == atmbuf[0]) {
  3193.             debug(F000,"cmkey token:",atmbuf,*atmbuf);
  3194.             ungword();  /* Put back the following word */
  3195.             return(-5); /* Special return code for token */
  3196.             }
  3197.         }
  3198.         }
  3199. #endif /* TOKPRECHECK */
  3200.  
  3201.         y = lookup(table,atmbuf,n,&z); /* Look up word in the table */
  3202.         debug(F111,"cmkey lookup",atmbuf,y);
  3203.         debug(F101,"cmkey zz","",zz);
  3204.         debug(F101,"cmkey cmflgs","",cmflgs);
  3205.         debug(F101,"cmkey crflag","",crflag);
  3206.         switch (y) {
  3207.           case -3:            /* Nothing to look up */
  3208.         break;
  3209.           case -2:            /* Ambiguous */
  3210.         cmflgs = -2;
  3211.         if (pmsg & 1) {
  3212.             bleep(BP_FAIL);
  3213.                     printf("?Ambiguous - %s\n",atmbuf);
  3214.             return(-9);
  3215.         }
  3216.         return(-2);
  3217.           case -1:            /* Not found at all */
  3218. #ifndef TOKPRECHECK
  3219.         if (tl) {
  3220.             for (i = 0; i < tl; i++) /* Check for token */
  3221.               if (tok[i] == *atmbuf) { /* Got one */
  3222.               debug(F000,"cmkey token:",atmbuf,*atmbuf);
  3223.               ungword();  /* Put back the following word */
  3224.               return(-5); /* Special return code for token */
  3225.               }
  3226.         }
  3227. #endif /* TOKPRECHECK */
  3228.  
  3229.         if (tl == 0) {        /* No tokens were included */
  3230. #ifdef OS2
  3231.             /* In OS/2 and Windows, allow for a disk letter like DOS */
  3232.             if (isalpha(*atmbuf) && *(atmbuf+1) == ':')
  3233.               return(-7);
  3234. #endif /* OS2 */
  3235.             if ((pmsg & 1) && !quiet) {
  3236.             bleep(BP_FAIL);
  3237.             printf("?No keywords match - %s\n",atmbuf); /* cmkey */
  3238.             }
  3239.             return(cmflgs = -9);
  3240.         } else {
  3241.             if (cmflgs == 1 || cmswitch) /* cmkey2 or cmswi */
  3242.               return(cmflgs = -6);
  3243.             else
  3244.               return(cmflgs = -2);
  3245.             /* The -6 code is to let caller try another table */
  3246.         }
  3247.         break;
  3248.           default:
  3249. #ifdef CK_RECALL
  3250.         if (test(table[z].flgs,CM_NOR)) no_recall = 1;
  3251. #endif /* CK_RECALL */
  3252.         if (zz == 4)
  3253.           swarg = 1;
  3254.         cmkwflgs = table[z].flgs;
  3255.         break;
  3256.         }
  3257.         return(y);
  3258.  
  3259.       case 2:            /* User terminated word with ESC */
  3260.         debug(F101,"cmkey Esc cc","",cc);
  3261.             if (cc == 0) {
  3262.         if (*xdef != NUL) {     /* Nothing in atmbuf */
  3263.             printf("%s ",xdef); /* Supply default if any */
  3264. #ifdef GEMDOS
  3265.             fflush(stdout);
  3266. #endif /* GEMDOS */
  3267.             addbuf(xdef);
  3268.             if (setatm(xdef,0) < 0) {
  3269.             printf("?Default too long\n");
  3270.             return(-9);
  3271.             }
  3272.             inword = cmflgs = 0;
  3273.             debug(F111,"cmkey: default",atmbuf,cc);
  3274.         } else {
  3275.             debug(F101,"cmkey Esc pmsg","",0);
  3276. #ifdef COMMENT
  3277. /*
  3278.   Chained FDBs...  The idea is that this function might not have a default,
  3279.   but the next one might.  But if it doesn't, there is no way to come back to
  3280.   this one.  To be revisited later...
  3281. */
  3282.             if (xcmfdb)        /* Chained fdb -- try next one */
  3283.               return(-3);
  3284. #endif /* COMMENT */
  3285.             if (pmsg & (1|4)) {    /* So for now just beep */
  3286.             bleep(BP_WARN);
  3287.             }
  3288.             break;
  3289.         }
  3290.             }
  3291.         if (f) {            /* If a conversion function is given */
  3292.         char * pp;
  3293.         zq = atxbuf;        /* apply it */
  3294.         pp = atxbuf;
  3295.         atxn = CMDBL;
  3296.         if ((*f)(atmbuf,&zq,&atxn) < 0)
  3297.           return(-2);
  3298.         if (!*pp)
  3299.           pp = xdef;
  3300.         if (setatm(pp,0) < 0) {
  3301.             printf("Evaluated keyword too long\n");
  3302.             return(-9);
  3303.         }
  3304.         }
  3305.         y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */
  3306.         debug(F111,"cmkey lookup y",atmbuf,y);
  3307.         debug(F111,"cmkey lookup z",atmbuf,z);
  3308.         if (y == -2 && z >= 0 && z < n) { /* Ambiguous */
  3309. #ifndef NOPARTIAL
  3310.         int j, k, len = 9999;    /* Do partial completion */
  3311.         /* Skip past any abbreviations in the table */
  3312.         for ( ; z < n; z++) {
  3313.             if ((table[z].flgs & CM_ABR) == 0)
  3314.               break;
  3315.             if (!(table[z].flgs & CM_HLP) || (pmsg & 2))
  3316.               break;
  3317.         }
  3318.         debug(F111,"cmkey partial z",atmbuf,z);
  3319.         debug(F111,"cmkey partial n",atmbuf,n);
  3320.         for (j = z+1; j < n; j++) {
  3321.             debug(F111,"cmkey partial j",table[j].kwd,j);
  3322.             if (ckstrcmp(atmbuf,table[j].kwd,cc,0))
  3323.               break;
  3324.             if (table[j].flgs & CM_ABR)
  3325.               continue;
  3326.             if ((table[j].flgs & CM_HLP) && !(pmsg & 2))
  3327.               continue;
  3328.             k = ckstrpre(table[z].kwd,table[j].kwd);
  3329.             debug(F111,"cmkey partial k",table[z].kwd,k);
  3330.             if (k < len)
  3331.               len = k; /* Length of longest common prefix */
  3332.         }
  3333.         debug(F111,"cmkey partial len",table[z].kwd,len);
  3334.         if (len != 9999 && len > cc) {
  3335.             ckstrncat(atmbuf,table[z].kwd+cc,ATMBL);
  3336.             atmbuf[len] = NUL;
  3337.             printf("%s",atmbuf+cc);
  3338.             ckstrncat(cmdbuf,atmbuf+cc,CMDBL);
  3339.             xc += (len - cc);
  3340.             cc = len;
  3341.         }
  3342. #endif /* NOPARTIAL */
  3343.         bleep(BP_WARN);
  3344.         break;
  3345.         } else if (y == -3) {
  3346.         bleep(BP_WARN);
  3347.         break;
  3348.         } else if (y == -1) {    /* Not found */
  3349.         if ((pmsg & 1) && !quiet) {
  3350.             bleep(BP_FAIL);
  3351.             printf("?No keywords match - \"%s\"\n",atmbuf);
  3352.         }
  3353.         cmflgs = -2;
  3354.         return(-9);
  3355.         }
  3356. /*
  3357.   If we found it, but it's a help-only keyword and the "help" bit is not
  3358.   set in pmsg, then not found.
  3359. */
  3360.         debug(F101,"cmkey flgs","",table[z].flgs);
  3361.         if (test(table[z].flgs,CM_HLP) && ((pmsg & 2) == 0)) {
  3362.         if ((pmsg & 1) && !quiet) {
  3363.             bleep(BP_FAIL);
  3364.             printf("?No keywords match - %s\n",atmbuf);
  3365.         }
  3366.         cmflgs = -2;
  3367.         return(-9);
  3368.         }
  3369. /*
  3370.   See if the keyword just found has the CM_ABR bit set in its flgs field, and
  3371.   if so, search forwards in the table for a keyword that has the same kwval
  3372.   but does not have CM_ABR (or CM_INV?) set, and then expand using the full
  3373.   keyword.  WARNING: This assumes that (a) keywords are in alphabetical order,
  3374.   and (b) the CM_ABR bit is set only if the the abbreviated keyword is a true
  3375.   abbreviation (left substring) of the full keyword.
  3376. */
  3377.         if (test(table[z].flgs,CM_ABR)) {
  3378.         int zz;
  3379.         for (zz = z+1; zz < n; zz++)
  3380.           if ((table[zz].kwval == table[z].kwval) &&
  3381.               (!test(table[zz].flgs,CM_ABR)) &&
  3382.               (!test(table[zz].flgs,CM_INV))) {
  3383.               z = zz;
  3384.               break;
  3385.           }
  3386.         }
  3387.         xp = table[z].kwd + cc;
  3388.         if (cmswitch && test(table[z].flgs,CM_ARG)) {
  3389. #ifdef VMS
  3390.         printf("%s=",xp);
  3391.         brkchar = '=';
  3392. #else
  3393.         printf("%s:",xp);
  3394.         brkchar = ':';
  3395. #endif /* VMS */
  3396.         } else {
  3397.         printf("%s ",xp);
  3398.         brkchar = SP;
  3399.         }
  3400. #ifdef CK_RECALL
  3401.         if (test(table[z].flgs,CM_NOR)) no_recall = 1;
  3402. #endif /* CK_RECALL */
  3403.         cmkwflgs = table[z].flgs;
  3404. #ifdef GEMDOS
  3405.         fflush(stdout);
  3406. #endif /* GEMDOS */
  3407.         addbuf(xp);
  3408.         if (cmswitch && test(table[z].flgs,CM_ARG)) {
  3409.         bp--;            /* Replace trailing space with : */
  3410. #ifdef VMS
  3411.         *bp++ = '=';
  3412. #else
  3413.         *bp++ = ':';
  3414. #endif /* VMS */
  3415.         *bp = NUL;
  3416.         np = bp;
  3417.         swarg = 1;
  3418.         }
  3419.         inword = 0;
  3420.         cmflgs = 0;
  3421.         debug(F110,"cmkey: addbuf",cmdbuf,0);
  3422.         return(y);
  3423.  
  3424.       case 3:            /* User typed "?" */
  3425.         if (f) {            /* If a conversion function is given */
  3426.         char * pp;
  3427.         zq = atxbuf;        /* do the conversion now. */
  3428.         pp = atxbuf;
  3429.         atxn = CMDBL;
  3430.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  3431.         if (setatm(pp,0) < 0) {
  3432.             printf("?Evaluated keyword too long\n");
  3433.             return(-9);
  3434.         }
  3435.         }
  3436.         y = lookup(table,atmbuf,n,&z); /* Look up what we have so far. */
  3437.         if (y == -1) {
  3438.         cmflgs = -2;
  3439.         if ((pmsg & 1) && !quiet) {
  3440.             bleep(BP_FAIL);
  3441.             printf(" No keywords match\n");
  3442.             return(-9);
  3443.         }
  3444.         return(-2);
  3445.         }
  3446. #ifndef COMMENT
  3447.         /* This is to allow ?-help to work immediately after a token */
  3448.         /* without having to type an intermediate space */
  3449.         if (tl) {
  3450.         for (i = 0; i < tl; i++) /* Check for token */
  3451.           if (tok[i] == *atmbuf) { /* Got one */
  3452.               debug(F000,"cmkey token:",atmbuf,*atmbuf);
  3453.               ungword();    /* Put back the following word */
  3454.               cmflgs = 3;    /* Force help next time around */
  3455.               return(-5);    /* Special return code for token */
  3456.           }
  3457.         }
  3458. #endif /* COMMENT */
  3459.  
  3460.         if (*xhlp == NUL)
  3461.           printf(" One of the following:\n");
  3462.         else
  3463.           printf(" %s, one of the following:\n",xhlp);
  3464.         {
  3465.         int x;
  3466.         x = pmsg & (2|4);    /* See kwdhelp() comments */
  3467.         if (atmbuf[0])        /* If not at beginning of field */
  3468.           x |= 1;        /* also show invisibles */
  3469.         kwdhelp(table,n,atmbuf,"","",1,x);
  3470.         }
  3471. #ifndef NOSPL
  3472.         if (!havetoken) {
  3473.         extern int topcmd;
  3474.         if (tl > 0 && topcmd != XXHLP) /* This is bad... */
  3475.           printf("or a macro name (\"do ?\" for a list) ");
  3476.         }
  3477. #endif /* NOSPL */
  3478.         if (*atmbuf == NUL && !havetoken) {
  3479.         if (tl == 1)
  3480.           printf("or the token %c\n",*tok);
  3481.         else if (tl > 1)
  3482.           printf("or one of the tokens: %s\n",ckspread(tok));
  3483.         }
  3484.         printf("%s%s", cmprom, cmdbuf);
  3485.         fflush(stdout);
  3486.         break;
  3487.  
  3488.       default:
  3489.         printf("\n%d - Unexpected return code from gtword\n",zz);
  3490.         return(cmflgs = -2);
  3491.     }
  3492.     zz = gtword((pmsg == 4) ? 1 : 0);
  3493.     debug(F111,"cmkey gtword zz",atmbuf,zz);
  3494.     }
  3495. }
  3496.  
  3497. int
  3498. chktok(tlist) char *tlist; {
  3499.     char *p;
  3500.     p = tlist;
  3501.     while (*p != NUL && *p != *atmbuf) p++;
  3502.     return((*p) ? (int) *p : 0);
  3503. }
  3504.  
  3505. /* Routines for parsing and converting dates and times */
  3506.  
  3507. #define isdatesep(c) (ckstrchr(" -/._",c))
  3508.  
  3509. #define CMDATEBUF 1024
  3510. char cmdatebuf[CMDATEBUF+4] = { NUL, NUL };
  3511. static char * cmdatebp = cmdatebuf;
  3512. char * cmdatemsg = NULL;
  3513.  
  3514. static struct keytab timeunits[] = {
  3515.     { "days",   TU_DAYS,   0 },
  3516.     { "months", TU_MONTHS, 0 },
  3517.     { "weeks",  TU_WEEKS,  0 },
  3518.     { "wks",    TU_WEEKS,  0 },
  3519.     { "years",  TU_YEARS,  0 },
  3520.     { "yrs",    TU_YEARS,  0 }
  3521. };
  3522. static int nunits = (sizeof(timeunits) / sizeof(struct keytab));
  3523.  
  3524. #define SYM_NOW  0
  3525. #define SYM_TODA 1
  3526. #define SYM_TOMO 2
  3527. #define SYM_YEST 3
  3528.  
  3529. static struct keytab symdaytab[] = {
  3530.     { "now",       SYM_NOW,  0 },
  3531.     { "today",     SYM_TODA, 0 },
  3532.     { "tomorrow",  SYM_TOMO, 0 },
  3533.     { "yesterday", SYM_YEST, 0 }
  3534. };
  3535. static int nsymdays = (sizeof(symdaytab) / sizeof(struct keytab));
  3536.  
  3537. static struct keytab daysofweek[] = {
  3538.     { "Friday",    5, 0 },
  3539.     { "Monday",    1, 0 },
  3540.     { "Saturday",  6, 0 },
  3541.     { "Sunday",    0, 0 },
  3542.     { "Thursday",  4, 0 },
  3543.     { "Tuesday",   2, 0 },
  3544.     { "Wednesday", 3, 0 }
  3545. };
  3546.  
  3547. static struct keytab usatz[] = {    /* RFC 822 timezones  */
  3548.     { "cdt",  5, 0 },            /* Values are GMT offsets */
  3549.     { "cst",  6, 0 },
  3550.     { "edt",  4, 0 },
  3551.     { "est",  5, 0 },
  3552.     { "gmt",  0, 0 },
  3553.     { "mdt",  6, 0 },
  3554.     { "mst",  7, 0 },
  3555.     { "pdt",  7, 0 },
  3556.     { "pst",  8, 0 },
  3557.     { "utc",  0, 0 },
  3558.     { "zulu", 0, 0 }
  3559. };
  3560. static int nusatz = (sizeof(usatz) / sizeof(struct keytab));
  3561.  
  3562.  
  3563. /*  C M C V T D A T E  --  Converts free-form date to standard form.  */
  3564.  
  3565. /*
  3566.    Call with
  3567.      s = pointer to free-format date, time, or date and time.
  3568.      t = 0: return time only if time was given in s.
  3569.      t = 1: always return time (00:00:00 if no time given in s).
  3570.      t = 2: allow time to be > 24:00:00.
  3571.    Returns:
  3572.      NULL on failure;
  3573.      Pointer to "yyyymmdd hh:mm:ss" (local date-time) on success.
  3574. */
  3575.  
  3576. /*
  3577.   Before final release the following long lines should be wrapped.
  3578.   Until then we leave them long since wrapping them wrecks EMACS's
  3579.   C indentation.
  3580. */
  3581.  
  3582. /* asctime pattern */
  3583. static char * atp1 = "[A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]";
  3584.  
  3585. /* asctime pattern with timezone */
  3586. static char * atp2 = "[A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [A-Z][A-Z][A-Z] [0-9][0-9][0-9][0-9]";
  3587.  
  3588. #define DATEBUFLEN 127
  3589. #define YYYYMMDD 12
  3590.  
  3591. #define isleap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  3592. static int mdays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  3593.  
  3594. #define NEED_DAYS 1
  3595. #define NEED_HRS  2
  3596. #define NEED_MINS 3
  3597. #define NEED_SECS 4
  3598. #define NEED_FRAC 5
  3599.  
  3600. #define DELTABUF 256
  3601. static char deltabuf[DELTABUF];
  3602. static char * deltabp = deltabuf;
  3603.  
  3604. char *
  3605. cmdelta(yy, mo, dd, hh, mm, ss, sign, dyy, dmo, ddd, dhh, dmm, dss)
  3606.     int yy, mo, dd, hh, mm, ss, sign, dyy, dmo, ddd, dhh, dmm, dss;
  3607. /* cmdelta */ {
  3608.     int zyy, zmo, zdd, zhh, zmm, zss;
  3609.     long t1, t2, t3, t4;
  3610.     long d1 = 0, d2, d3;
  3611.     char datebuf[DATEBUFLEN+1];
  3612.  
  3613. #ifdef DEBUG
  3614.     if (deblog) {
  3615.     debug(F101,"cmdelta yy","",yy);
  3616.     debug(F101,"cmdelta mo","",mo);
  3617.     debug(F101,"cmdelta dd","",dd);
  3618.     debug(F101,"cmdelta hh","",hh);
  3619.     debug(F101,"cmdelta mm","",mm);
  3620.     debug(F101,"cmdelta ss","",ss);
  3621.     debug(F101,"cmdelta sin","",sign);
  3622.     debug(F101,"cmdelta dyy","",dyy);
  3623.     debug(F101,"cmdelta dmo","",dmo);
  3624.     debug(F101,"cmdelta ddd","",ddd);
  3625.     debug(F101,"cmdelta dhh","",dhh);
  3626.     debug(F101,"cmdelta dmm","",dmm);
  3627.     debug(F101,"cmdelta dss","",dss);
  3628.     }
  3629. #endif /* DEBLOG */
  3630.  
  3631.     if (yy < 0 || yy > 9999) {
  3632.     makestr(&cmdatemsg,"Base year out of range");
  3633.     debug(F111,"cmdelta",cmdatemsg,-1);
  3634.     return(NULL);
  3635.     }
  3636.     if (mo < 1 || mo > 12) {
  3637.     makestr(&cmdatemsg,"Base month out of range");
  3638.     debug(F111,"cmdelta",cmdatemsg,-1);
  3639.     return(NULL);
  3640.     }
  3641.     if (dd < 1 || dd > mdays[mo]) {
  3642.     makestr(&cmdatemsg,"Base day out of range");
  3643.     debug(F111,"cmdelta",cmdatemsg,-1);
  3644.     return(NULL);
  3645.     }
  3646.     if (hh < 0 || hh > 23) {
  3647.     makestr(&cmdatemsg,"Base hour out of range");
  3648.     debug(F111,"cmdelta",cmdatemsg,-1);
  3649.     return(NULL);
  3650.     }
  3651.     if (mm < 0 || mm > 59) {
  3652.     makestr(&cmdatemsg,"Base minute out of range");
  3653.     debug(F111,"cmdelta",cmdatemsg,-1);
  3654.     return(NULL);
  3655.     }
  3656.     if (ss < 0 || ss > 60) {
  3657.     makestr(&cmdatemsg,"Base second out of range");
  3658.     debug(F111,"cmdelta",cmdatemsg,-1);
  3659.     return(NULL);
  3660.     }
  3661.     sign = (sign < 0) ? -1 : 1;
  3662.     if (dmo != 0) {
  3663.     mo += (sign * dmo);
  3664.     if (mo > 12 || mo < 0) {
  3665.         yy += mo / 12;
  3666.         mo = mo % 12;
  3667.     }
  3668.     }
  3669.     if (dyy != 0) {
  3670.     yy += (sign * dyy);
  3671.     if (yy > 9999 || yy < 0) {
  3672.         makestr(&cmdatemsg,"Result year out of range");
  3673.         debug(F111,"cmdelta",cmdatemsg,-1);
  3674.         return(NULL);
  3675.     }
  3676.     }
  3677.     sprintf(datebuf,"%04d%02d%02d %02d:%02d:%02d",yy,mo,dd,hh,mm,ss);
  3678.     d1 = mjd(datebuf);
  3679.     debug(F111,"cmdelta mjd",datebuf,d1);    
  3680.  
  3681.     t1 = hh * 3600 + mm * 60 + ss;    /* Base time to secs since midnight */
  3682.     t2 = dhh * 3600 + dmm * 60 + dss;    /* Delta time, ditto */
  3683.     t3 = t1 + (sign * t2);        /* Get sum (or difference) */
  3684.     
  3685.     d2 = (sign * ddd);            /* Delta days */
  3686.     d2 += t3 / 86400L;
  3687.  
  3688.     t4 = t3 % 86400L;            /* Fractional part of day */
  3689.     if (t4 < 0) {            /* If negative */
  3690.     d2--;                /* one less delta day */
  3691.     t4 += 86400L;            /* get positive seconds */
  3692.     }
  3693.     hh = (int) (t4 / 3600L);
  3694.     mm = (int) (t4 % 3600L) / 60;
  3695.     ss = (int) (t4 % 3600L) % 60;
  3696.  
  3697.     sprintf(datebuf,"%s %02d:%02d:%02d", mjd2date(d1+d2),hh,mm,ss);
  3698.     {
  3699.     int len, k, n;
  3700.     char * p;
  3701.     len = strlen(datebuf);
  3702.     k = deltabp - (char *)deltabuf;    /* Space used */
  3703.     n = DELTABUF - k - 1;        /* Space left */
  3704.     if (n < len) {            /* Not enough? */
  3705.         deltabp = deltabuf;        /* Wrap around */
  3706.         n = DELTABUF;
  3707.     }
  3708.     ckstrncpy(deltabp,datebuf,n);
  3709.     p = deltabp;
  3710.     deltabp += len + 1;
  3711.     return(p);
  3712.     }
  3713. }
  3714.  
  3715.  
  3716. /* Convert Delta Time to Seconds */
  3717.  
  3718. int
  3719. delta2sec(s,result) char * s; long * result; {
  3720.     long ddays = 0L, zz;
  3721.     int dsign = 1, dhours = 0, dmins = 0, dsecs = 0, units;
  3722.     int state = NEED_DAYS;
  3723.     char *p, *p2, *p3, c = 0;
  3724.     char buf[64];
  3725.  
  3726.     if (!s) s = "";
  3727.     if (!*s)
  3728.       return(-1);
  3729.     if ((int)strlen(s) > 63)
  3730.       return(-1);
  3731.     ckstrncpy(buf,s,64);
  3732.     p = buf;
  3733.  
  3734.     if (*p != '+' && *p != '-')
  3735.       return(-1);
  3736.  
  3737.     if (*p++ == '-')
  3738.       dsign = -1;
  3739.     while (*p == SP)            /* Skip intervening spaces */
  3740.       p++;
  3741.  
  3742.     while (state) {            /* FSA to parse delta time */
  3743.     if (state < 0 || !isdigit(*p))
  3744.       return(-1);
  3745.     p2 = p;                /* Get next numeric field */
  3746.     while (isdigit(*p2))
  3747.       p2++;
  3748.     c = *p2;            /* And break character */
  3749.     *p2 = NUL;            /* Terminate the number */
  3750.     switch (state) {        /* Interpret according to state */
  3751.       case NEED_DAYS:        /* Initial */
  3752.         if ((c == '-') ||        /* VMS format */
  3753.         ((c == 'd' || c == 'D')
  3754.          && !isalpha(*(p2+1)))) { /* Days */
  3755.         ddays = atol(p);
  3756.         if (!*(p2+1))            
  3757.           state = 0;
  3758.         else            /* if anything is left */
  3759.           state = NEED_HRS;    /* now we want hours. */
  3760.         } else if (c == ':') {    /* delimiter is colon */
  3761.         dhours = atoi(p);    /* so it's hours */
  3762.         state = NEED_MINS;    /* now we want minutes */
  3763.         } else if (!c) {        /* end of string */
  3764.         dhours = atoi(p);    /* it's still hours */
  3765.         state = 0;        /* and we're done */
  3766.         } else if (isalpha(c) || c == SP) {
  3767.         if (c == SP) {        /* It's a keyword? */
  3768.             p2++;        /* Skip spaces */
  3769.             while (*p2 == SP)
  3770.               p2++;
  3771.         } else {        /* or replace first letter */
  3772.             *p2 = c;
  3773.         }
  3774.         p3 = p2;        /* p2 points to beginning of keyword */
  3775.         while (isalpha(*p3))    /* Find end of keyword */
  3776.           p3++;
  3777.         c = *p3;        /* NUL it out so we can look it up */
  3778.         if (*p3)        /* p3 points to keyword terminator */
  3779.           *p3 = NUL;
  3780.         if ((units = lookup(timeunits,p2,nunits,NULL)) < 0)
  3781.           return(-1);
  3782.         *p2 = NUL;        /* Re-terminate the number */
  3783.         *p3 = c;
  3784.         while (*p3 == SP)    /* Point at field after units */
  3785.           p3++;
  3786.         p2 = p3;
  3787.         switch (units) {
  3788.           case TU_DAYS:
  3789.             ddays = atol(p);
  3790.             break;
  3791.           default:
  3792.             return(-1);
  3793.         }
  3794.         if (*p2) {
  3795.             state = NEED_HRS;
  3796.             p2--;
  3797.         } else
  3798.           state = 0;
  3799.         } else {            /* Anything else */
  3800.         state = -1;        /* is an error */
  3801.         }
  3802.         break;
  3803.       case NEED_HRS:        /* Looking for hours */
  3804.         if (c == ':') {
  3805.         dhours = atoi(p);
  3806.         state = NEED_MINS;
  3807.         } else if (!c) {
  3808.         dhours = atoi(p);
  3809.         state = 0;
  3810.         } else {
  3811.         state = -1;
  3812.         }
  3813.         break;
  3814.       case NEED_MINS:        /* Looking for minutes */
  3815.         if (c == ':') {
  3816.         dmins = atoi(p);
  3817.         state = NEED_SECS;
  3818.         } else if (!c) {
  3819.         dmins = atoi(p);
  3820.         state = 0;
  3821.         } else {
  3822.         state = -1;
  3823.         }
  3824.         break;
  3825.       case NEED_SECS:        /* Looking for seconds */
  3826.         if (c == '.') {
  3827.         dsecs = atoi(p);
  3828.         state = NEED_FRAC;
  3829.         } else if (!c) {
  3830.         dsecs = atoi(p);
  3831.         state = 0;
  3832.         } else {
  3833.         state = -1;
  3834.         }
  3835.         break;
  3836.       case NEED_FRAC:        /* Fraction of second */
  3837.         if (!c && rdigits(p)) {
  3838.         if (*p > '4')
  3839.           dsecs++;
  3840.         state = 0;
  3841.         } else {
  3842.         state = -1;
  3843.         }
  3844.         break;
  3845.     }
  3846.     if (c)                /* next field if any */
  3847.       p = p2 + 1;
  3848.     }
  3849.     if (state < 0)
  3850.       return(-1);
  3851.  
  3852.     /* if days > 24854 and sizeof(long) == 32 we overflow */
  3853.  
  3854.     zz = ddays * 86400L;
  3855.     if (zz < 0L)            /* This catches it */
  3856.       return(-2);
  3857.     zz += dhours * 3600L + dmins * 60L + dsecs;
  3858.     zz *= dsign;
  3859.     *result = zz;
  3860.     return(0);
  3861. }
  3862.  
  3863.  
  3864. char *
  3865. cmcvtdate(s,t) char * s; int t; {
  3866.     int x, i, j, k, hh, mm, ss, ff, pmflag = 0, nodate = 0, len, dow;
  3867.     int units, isgmt = 0, gmtsign = 0, d = 0, state = 0, nday;
  3868.     int kn = 0, ft[8], isletter = 0, f2len = 0;
  3869.  
  3870.     int zhh = 0;            /* Timezone adjustments */
  3871.     int zmm = 0;
  3872.     int zdd = 0;
  3873.  
  3874.     int dsign = 1;            /* Delta-time adjustments */
  3875.     int ddays = 0;
  3876.     int dmonths = 0;
  3877.     int dyears = 0;
  3878.     int dhours = 0;
  3879.     int dmins = 0;
  3880.     int dsecs = 0;
  3881.     int havedelta = 0;
  3882.  
  3883.     char * fld[8], * p = "", * p2, * p3; /* Assorted buffers and pointers  */
  3884.     char * s2, * s3;
  3885.     char * year = NULL, * month = NULL, * day = NULL;
  3886.     char * hour = "00", * min = "00", * sec = "00";
  3887.     char datesep = 0;
  3888.     char tmpbuf[8];
  3889.     char xbuf[DATEBUFLEN+1];
  3890.     char ybuf[DATEBUFLEN+1];
  3891.     char zbuf[DATEBUFLEN+1];
  3892.     char yyyymmdd[YYYYMMDD];
  3893.     char dbuf[26];
  3894.     char daybuf[3];
  3895.     char monbuf[3];
  3896.     char yearbuf[5];
  3897.     char timbuf[16], *tb, cc;
  3898.     char * dp = NULL;            /* Result pointer */
  3899.  
  3900.     if (!s) s = "";
  3901.     tmpbuf[0] = NUL;
  3902.  
  3903.     while (*s == SP) s++;        /* Gobble any leading blanks */
  3904.     if (isalpha(*s))            /* Remember if 1st char is a letter */
  3905.       isletter = 1;
  3906.  
  3907.     len = strlen(s);
  3908.     debug(F110,"cmcvtdate",s,len);
  3909.     if (len == 0) {            /* No arg - return current date-time */
  3910.     dp = ckdate();
  3911.     goto xcvtdate;
  3912.     }
  3913.     if (len > DATEBUFLEN) {        /* Check length of arg */
  3914.     makestr(&cmdatemsg,"Date-time string too long");
  3915.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  3916.     return(NULL);
  3917.     }
  3918.     hh = 0;                /* Init time to 00:00:00.0 */
  3919.     mm = 0;
  3920.     ss = 0;
  3921.     ff = 0;
  3922.     ztime(&p);
  3923.     if (!p)
  3924.       p  = "";
  3925.     if (*p) {                /* Init time to current time */
  3926.     x = ckstrncpy(dbuf,p,26);
  3927.     if (x > 17) {
  3928.         hh = atoi(&dbuf[11]);
  3929.         mm = atoi(&dbuf[14]);
  3930.         ss = atoi(&dbuf[17]);
  3931.     }
  3932.     }
  3933.     ckstrncpy(yyyymmdd,zzndate(),YYYYMMDD); /* Init date to current date */
  3934.     ckstrncpy(yearbuf,yyyymmdd,5);
  3935.     ckstrncpy(monbuf,&yyyymmdd[4],3);
  3936.     ckstrncpy(daybuf,&yyyymmdd[6],3);
  3937.     year = yearbuf;
  3938.     month = monbuf;
  3939.     day = daybuf;
  3940.     nday = atoi(daybuf);
  3941.  
  3942.     ckstrncpy(xbuf,s,DATEBUFLEN);    /* Make a local copy we can poke */
  3943.     s = xbuf;                /* Point to it */
  3944.     s[len] = NUL;
  3945.     if (s[0] == ':') {
  3946.     p = s;
  3947.     goto dotime;
  3948.     }
  3949.     /* Special preset formats... */
  3950.  
  3951.     if (len >= 14) {            /* FTP MDTM all-numeric date */
  3952.     char c;
  3953.     c = s[14];            /* e.g. 19980615100045.014 */
  3954.     s[14] = NUL;
  3955.     x = rdigits(s);
  3956.     s[14] = c;
  3957.     if (x) {
  3958.         ckstrncpy(yyyymmdd,s,8+1);
  3959.         year = NULL;
  3960.         p = &s[8];
  3961.         goto dotime;
  3962.     }
  3963.     }
  3964.     x = 0;                /* Becomes > 0 for asctime format */
  3965.     if (isalpha(s[0])) {
  3966.     if (len == 24) {        /* Asctime format? */
  3967.         /* Sat Jul 14 15:57:32 2001 */
  3968.         x = ckmatch(atp1,s,0,0);
  3969.         debug(F111,"cmcvtdate asctime",s,x);
  3970.     } else if (len == 28) {        /* Or Asctime plus timezone? */
  3971.         /* Sat Jul 14 15:15:39 EDT 2001 */
  3972.         x = ckmatch(atp2,s,0,0);
  3973.         debug(F111,"cmcvtdate asctime+timezone",s,x);
  3974.     }
  3975.     }
  3976.     if (x > 0) {            /* Asctime format */
  3977.         int xx;
  3978.         strncpy(yearbuf,s + len - 4,4);
  3979.         yearbuf[4] = NUL;
  3980.         for (i = 0; i < 3; i++)
  3981.           tmpbuf[i] = s[i+4];
  3982.         tmpbuf[3] = NUL;
  3983.     if ((xx = lookup(cmonths,tmpbuf,12,NULL)) < 0) {
  3984.         makestr(&cmdatemsg,"Invalid month");
  3985.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  3986.         return(NULL);
  3987.     }
  3988.         debug(F101,"cmcvtdate asctime month","",xx);
  3989.         monbuf[0] = (xx / 10) + '0'; 
  3990.         monbuf[1] = (xx % 10) + '0'; 
  3991.         monbuf[2] = NUL;
  3992.         daybuf[0] = (s[8] == ' ' ? '0' : s[8]);
  3993.         daybuf[1] = s[9];
  3994.         daybuf[2] = NUL;
  3995.     xbuf[0] = SP;
  3996.         for (i = 11; i < 19; i++)
  3997.           xbuf[i-10] = s[i];
  3998.         xbuf[9] = NUL;
  3999.     ckmakmsg(zbuf,18,yearbuf,monbuf,daybuf,xbuf);
  4000.     debug(F110,"cmcvtdate asctime ok",zbuf,0);
  4001.     if (len == 24) {
  4002.         dp = zbuf;
  4003.         goto xcvtdate;
  4004.     } else {
  4005.         int n;
  4006.         n = ckmakmsg(ybuf,DATEBUFLEN-4,zbuf," ",NULL,NULL);
  4007.         ybuf[n++] = s[20];
  4008.         ybuf[n++] = s[21];
  4009.         ybuf[n++] = s[22];
  4010.         ybuf[n++] = NUL;
  4011.         ckstrncpy(xbuf,ybuf,DATEBUFLEN);
  4012.         s = xbuf;
  4013.         isletter = 0;
  4014.     }
  4015.     }
  4016.  
  4017. /* Check for day of week */
  4018.  
  4019.     p = s;
  4020.     while (*p == SP) p++;
  4021.     dow = -1;
  4022.     if (*p) {
  4023.     p2 = p;
  4024.     cc = NUL;
  4025.     while (1) {
  4026.         if (*p2 == ',' || *p2 == SP || !*p2) {
  4027.         cc = *p2;        /* Save break char */
  4028.         *p2 = NUL;        /* NUL it out */
  4029.         p3 = p2;        /* Remember this spot */
  4030.         if ((dow = lookup(daysofweek,p,7,NULL)) > -1) {
  4031.             debug(F111,"cmcvtdate dow",p,dow);
  4032.             s = p2;
  4033.             if (cc == ',' || cc == SP) { /* Point to next field */
  4034.             s++;
  4035.             while (*s == SP) s++;
  4036.             }
  4037.             p = s;
  4038.             debug(F111,"cmcvtdate dow new p",p,dow);
  4039.             break;
  4040.         } else if (isalpha(*p) && cc == ',') {
  4041.             makestr(&cmdatemsg,"Unrecognized day of week");
  4042.             debug(F111,"cmcvtdate",cmdatemsg,-1);
  4043.             return(NULL);
  4044.         } else {
  4045.             *p3 = cc;
  4046.             break;
  4047.         }
  4048.         }
  4049.         p2++;
  4050.     }
  4051.     }
  4052.     len = strlen(s);        /* Update length */
  4053.  
  4054.     debug(F111,"cmcvtdate dow",p,dow);
  4055.     if (dow > -1) {            /* Have a day number */
  4056.     long zz; int n, j;
  4057.     zz = mjd(zzndate());        /* Get today's MJD */
  4058.     j = (((int)(zz % 7L)) + 3) % 7; /* Today's day-of-week number */
  4059.     if (j == dow) {
  4060.         ckstrncpy(yyyymmdd,zzndate(),YYYYMMDD);
  4061.         year = NULL;
  4062.     } else {
  4063.         n = dow - j;        /* Days from now */
  4064.         if (dow < j)
  4065.           n += 7;
  4066.         if (n < 0) n += 7;        /* Add to MJD */
  4067.         zz += n;
  4068.         ckstrncpy(yyyymmdd,mjd2date(zz),YYYYMMDD); /* New date */
  4069.         year = NULL;
  4070.     }
  4071.     if (len == 0) {
  4072.         ckmakmsg(zbuf,18,yyyymmdd," 00:00:00",NULL,NULL);
  4073.         goto xcvtdate;
  4074.     }
  4075.     isletter = 0;
  4076.     if (rdigits(p) && len < 8)    /* Next field is time? */
  4077.       goto dotime;            /* If so go straight to time section */
  4078.     else if (isdigit(*p)) {
  4079.         if (*(p+1) == ':')
  4080.           goto dotime;
  4081.         else if (isdigit(*(p+1)) && (*(p+2) == ':'))
  4082.           goto dotime;
  4083.     }
  4084.     debug(F110,"cmcvtdate A not time",p,0);
  4085.     goto normal;            /* Skip past symbolic date section */
  4086.     }
  4087.     if (*s == '+' || *s == '-') {    /* Delta time only - skip ahead. */
  4088.     p = s;
  4089.     goto delta;
  4090.     }
  4091.  
  4092.     /* Handle "today", "yesterday", "tomorrow", and +/- n units */
  4093.  
  4094.     if (ckstrchr("TtYyNn",s[0])) {
  4095.     int i, k, n, minus = 0;
  4096.     char c;
  4097.     long jd;
  4098.     jd = mjd(ckdate());
  4099.     debug(F111,"cmcvtdate mjd",s,jd);
  4100.  
  4101.     /* Symbolic date: TODAY, TOMORROW, etc...? */
  4102.  
  4103.     s2 = s;                /* Find end of keyword */
  4104.     i = 0;
  4105.     while (isalpha(*s2)) {        /* and get its length */
  4106.         i++;
  4107.         s2++;
  4108.     }
  4109.     c = *s2;            /* Zap but save delimiter */
  4110.     *s2 = NUL;
  4111.     k = lookup(symdaytab,s,nsymdays,NULL); /* Look up keyword */
  4112.     *s2 = c;            /* Replace delimiter */
  4113.     if (k < 0)            /* Keyword not found */
  4114.       goto normal;
  4115.     s3 = &s[i];
  4116.     while (*s3 == SP)        /* Skip whitespace */
  4117.       s3++;
  4118.     if (*s3 == '_' || *s3 == ':')
  4119.       s3++;
  4120.  
  4121.     switch (k) {            /* Have keyword */
  4122.       case SYM_NOW:            /* NOW */
  4123.         ckstrncpy(ybuf,ckdate(),DATEBUFLEN);
  4124.         ckstrncpy(yyyymmdd,ybuf,YYYYMMDD);
  4125.         year = NULL;
  4126.         if (*s3) {            /* No overwriting current time. */
  4127.         ckstrncat(ybuf," ",DATEBUFLEN);
  4128.         ckstrncat(ybuf,s3,DATEBUFLEN);
  4129.         }
  4130.         break;
  4131.       default:            /* Yesterday, Today, and Tomorrow */
  4132.         if (k == SYM_TOMO) {    /* TOMORROW */
  4133.         strncpy(ybuf,mjd2date(jd+1),8);
  4134.         } else if (k == SYM_YEST) {    /* YESTERDAY */
  4135.         strncpy(ybuf,mjd2date(jd-1),8);
  4136.         } else {            /* TODAY */
  4137.         strncpy(ybuf,ckdate(),8);
  4138.         }
  4139.         strncpy(ybuf+8," 00:00:00",DATEBUFLEN-8); /* Default time is 0 */
  4140.         ckstrncpy(yyyymmdd,ybuf,YYYYMMDD);
  4141.         year = NULL;
  4142.         if (*s3) {            /* If something follows keyword... */
  4143.         if (isdigit(*s3)) {    /* Time - overwrite default time */
  4144.             strncpy(ybuf+8,s+i,DATEBUFLEN-8);
  4145.         } else {        /* Something else, keep default time */
  4146.             ckstrncat(ybuf," ",DATEBUFLEN); /* and append */
  4147.             ckstrncat(ybuf,s3,DATEBUFLEN); /* whatever we have */
  4148.         }
  4149.         }
  4150.     }
  4151.     s = ybuf;            /* Point to rewritten date-time */
  4152.     len = strlen(s);        /* Update length */
  4153.     isletter = 0;            /* Cancel this */
  4154.     }
  4155.  
  4156. /* Regular free-format non-symbolic date */
  4157.  
  4158.   normal:
  4159.  
  4160.     debug(F111,"cmcvtdate NORMAL",s,len);
  4161.     if (yyyymmdd[0] && !year) {
  4162.     ckstrncpy(yearbuf,yyyymmdd,5);
  4163.     ckstrncpy(monbuf,&yyyymmdd[4],3);
  4164.     ckstrncpy(daybuf,&yyyymmdd[6],3);
  4165.     year = yearbuf;
  4166.     month = monbuf;
  4167.     day = daybuf;
  4168.     nday = atoi(daybuf);
  4169.     }
  4170.     if (isdigit(s[0])) {        /* Time without date? */
  4171.     p = s;
  4172.     if (s[1] == ':') {
  4173.         debug(F111,"cmcvtdate NORMAL X1",s,len);
  4174.         goto dotime;
  4175.     } else if (len > 1 && isdigit(s[1]) && s[2] == ':') {
  4176.         debug(F111,"cmcvtdate NORMAL X2",s,len);
  4177.         goto dotime;
  4178.     } else if (rdigits(s) && len < 8) {
  4179.         debug(F111,"cmcvtdate NORMAL X3",s,len);
  4180.         goto dotime;
  4181.     }
  4182.     }
  4183.     if (len >= 8 && isdigit(*s)) {    /* Check first for yyyymmdd* */
  4184.     debug(F111,"cmcvtdate NORMAL A",s,len);
  4185.     cc = s[8];
  4186.     s[8] = NUL;            /* Isolate first 8 characters */
  4187.     if (rdigits(s)) {
  4188.         /* Have valid time separator? */
  4189.         p2 = cc ? ckstrchr(" Tt_-:",cc) : NULL;
  4190.         if (!cc || p2) {
  4191.         ckstrncpy(yyyymmdd,s,YYYYMMDD);    /* Valid separator */
  4192.         year = NULL;
  4193.         s += 8;                    /* or time not given */
  4194.         if (cc) s++;                /* Keep date */
  4195.         p = s;                    /* and go handle time */
  4196.         goto dotime;
  4197.         } else if (!p2) {
  4198.         if (isdigit(cc))
  4199.           makestr(&cmdatemsg,"Numeric date too long");
  4200.         else
  4201.           makestr(&cmdatemsg,"Invalid date-time separator");
  4202.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4203.         return(NULL);
  4204.         }
  4205.     }
  4206.     s[8] = cc;            /* Put this back! */
  4207.     }
  4208.     debug(F111,"cmcvtdate NORMAL non-yyyymmdd",s,len);
  4209.  
  4210.     /* Free-format date -- figure it out */
  4211.  
  4212. #ifdef COMMENT
  4213.     if (*s && !isdigit(*s)) {
  4214.     makestr(&cmdatemsg,"Unrecognized word in date");
  4215.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4216.     return(NULL);
  4217.     }
  4218. #endif /* COMMENT */
  4219.     for (i = 0; i < 8; i++)        /* Field types */
  4220.       ft[i] = -1;
  4221.     fld[i = 0] = (p = s);        /* First field */
  4222.     while (*p) {            /* Get next two fields */
  4223.     if (isdatesep(*p)) {        /* Have a date separator */
  4224.         if (i == 0) {
  4225.         datesep = *p;
  4226.         } else if (i == 1 && *p != datesep) {
  4227.         makestr(&cmdatemsg,"Inconsistent date separators");
  4228.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4229.         return(NULL);
  4230.         }
  4231.         *p++ = NUL;            /* Replace by NUL */
  4232.         if (*p) {            /* Now we're at the next field */
  4233.         while (*p == SP) p++;    /* Skip leading spaces */
  4234.         if (!*p) break;        /* Make sure we still have something */
  4235.         if (i == 2)        /* Last one? */
  4236.           break;
  4237.         fld[++i] = p;        /* No, record pointer to this one */
  4238.         } else {
  4239.         break;
  4240.         }        
  4241.     } else if ((*p == 'T' || *p == 't') && isdigit(*(p+1))) { /* Time */
  4242.         *p++ = NUL;
  4243.         break;
  4244.     } else if (*p == ':') {
  4245.         if (i == 0 && p == s) {
  4246.         nodate = 1;
  4247.         break;
  4248.         } else if (i != 0) {    /* After a date */
  4249.         if (i == 2) {        /* OK as date-time separator (VMS) */
  4250.             *p++ = NUL;
  4251.             break;
  4252.         }
  4253.         if (i < 2)
  4254.           makestr(&cmdatemsg,"Too few fields in date");
  4255.         else
  4256.           makestr(&cmdatemsg,"Misplaced time separator");
  4257.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4258.         return(NULL);
  4259.         }
  4260.         nodate = 1;            /* Or without a date */
  4261.         break;
  4262.     }
  4263.     p++;
  4264.     }
  4265.     if (p > s && i == 0)        /* Make sure we have a date */
  4266.       nodate = 1;            /* No date. */
  4267.  
  4268.     if (nodate && dow > -1) {        /* Have implied date from DOW? */
  4269.     goto dotime;            /* Use, use that, go do time. */
  4270.  
  4271.     } else if (nodate) {        /* No date and no implied date */
  4272.     char *tmp;            /* Substitute today's date */
  4273.     ztime(&tmp);
  4274.     if (!tmp)
  4275.       tmp  = "";
  4276.     if (!*tmp) {
  4277.         makestr(&cmdatemsg,"Problem supplying current date");
  4278.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4279.         return(NULL);
  4280.     }
  4281.     ckstrncpy(dbuf,tmp,26);        /* Reformat */
  4282.     if (dbuf[8] == SP) dbuf[8] = '0';
  4283.     fld[0] = dbuf+8;        /* dd */
  4284.     dbuf[10] = NUL;
  4285.     fld[1] = dbuf+4;        /* mmm */
  4286.     dbuf[7] = NUL;
  4287.     fld[2] = dbuf+20;        /* yyyy */
  4288.     dbuf[24] = NUL;
  4289.     hh = atoi(&dbuf[11]);
  4290.     mm = atoi(&dbuf[14]);
  4291.     ss = atoi(&dbuf[17]);
  4292.     p = s;                /* Back up source pointer to reparse */
  4293.     } else if (i < 2) {
  4294.     makestr(&cmdatemsg,"Too few fields in date");
  4295.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4296.     return(NULL);
  4297.     }
  4298.     /* Have three date fields - see what they are */
  4299.  
  4300.     for (k = 0, j = 0; j < 3; j++) {    /* Get number of non-numeric fields */
  4301.     ft[j] = rdigits(fld[j]);
  4302.     debug(F111,"cmcvtdate fld",fld[j],j);
  4303.     if (ft[j] == 0)
  4304.       k++;
  4305.     }
  4306.     kn = k;                /* How many numeric fields */
  4307.     month = NULL;            /* Strike out default values */
  4308.     year = NULL;
  4309.     day = NULL;
  4310.  
  4311.     if (k == 2 && ft[2] > 0) {        /* Jul 20, 2001 */
  4312.     int xx;
  4313.     xx = strlen(fld[1]);
  4314.     p3 = fld[1];
  4315.     if (xx > 0) if (p3[xx-1] == ',') {
  4316.         p3[xx-1] = NUL;
  4317.         if (rdigits(p3)) {
  4318.         k = 1;    
  4319.         ft[1] = 1;
  4320.         } else p3[xx-1] = ',';
  4321.     }
  4322.     }
  4323.     if (k > 1) {            /* We can have only one non-numeric */
  4324.     if (nodate)
  4325.       makestr(&cmdatemsg,"Unrecognized word in date"); 
  4326.     else if (!ft[2] && isdigit(*(fld[2])))
  4327.       makestr(&cmdatemsg,"Invalid date-time separator"); 
  4328.     else
  4329.       makestr(&cmdatemsg,"Too many non-numeric fields in date");
  4330.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4331.     return(NULL);
  4332.     }
  4333.     if (!ft[0]) {
  4334.     k = 0;
  4335.     } else if (!ft[1]) {
  4336.     k = 1;
  4337.     } else if (!ft[2]) {
  4338.     makestr(&cmdatemsg,"Non-digit in third date field");
  4339.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4340.     return(NULL);
  4341.     } else
  4342.       k = -1;
  4343.  
  4344.     if (k > -1) {
  4345.     if ((x = lookup(cmonths,fld[k],12,NULL)) < 0) {
  4346.         makestr(&cmdatemsg,"Unknown month");
  4347.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4348.         return(NULL);
  4349.     }
  4350.     sprintf(tmpbuf,"%02d",x);
  4351.     month = tmpbuf;
  4352.     }
  4353.     f2len = strlen(fld[2]);        /* Length of 3rd field */
  4354.  
  4355.     if (k == 0) {            /* monthname dd, yyyy */
  4356.     day = fld[1];
  4357.     year = fld[2];
  4358.     } else if (((int)strlen(fld[0]) == 4)) { /* yyyy-xx-dd */
  4359.     year = fld[0];
  4360.     day = fld[2];
  4361.     if (!month)
  4362.       month = fld[1];        /* yyyy-mm-dd */
  4363.     } else if (f2len == 4) {        /* xx-xx-yyyy */
  4364.     year = fld[2];
  4365.     if (month) {            /* dd-name-yyyy */
  4366.         day = fld[0];
  4367.     } else {            /* xx-xx-yyyy */
  4368.         int f0, f1;
  4369.         f0 = atoi(fld[0]);
  4370.         f1 = atoi(fld[1]);
  4371.         if (((f0 > 12) && (f1 <= 12)) || (f1 <= 12 && f0 == f1)) {
  4372.         day = fld[0];        /* mm-dd-yyyy */
  4373.         month = fld[1];
  4374.         } else if ((f0 <= 12) && (f1 > 12)) {
  4375.         if (!rdigits(fld[1])) {
  4376.             makestr(&cmdatemsg,"Day not numeric");
  4377.             debug(F111,"cmcvtdate",cmdatemsg,-1);
  4378.             return(NULL);
  4379.         } else {
  4380.             day = fld[1];    /* dd-mm-yyyy */
  4381.         }
  4382.         month = fld[0];
  4383.         } else {
  4384.         if (!f0 || !f1)
  4385.           makestr(&cmdatemsg,"Day or month out of range");
  4386.         else
  4387.           makestr(&cmdatemsg,"Day and month are ambiguous");
  4388.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4389.         return(NULL);
  4390.         }
  4391.     }
  4392.     } else if ((f2len < 4) &&        /* dd mmm yy (RFC822) */
  4393.            !rdigits(fld[1]) &&    /* middle field is monthname */
  4394.            rdigits(fld[2])) {
  4395.     int tmpyear;
  4396.     day = fld[0];
  4397.     if (!fld[2][1]) {
  4398.         makestr(&cmdatemsg,"Too few digits in year");
  4399.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4400.         return(NULL);
  4401.     }
  4402.     tmpyear = atoi(fld[2]);
  4403.     if (tmpyear < 50)        /* RFC 2822 windowing */
  4404.       tmpyear += 2000;
  4405.     else                /* This includes 3-digit years. */
  4406.       tmpyear += 1900;
  4407.     year = ckitoa(tmpyear);
  4408.  
  4409.     } else if ((f2len < 4) && (k < 0) && ((int)strlen(fld[0]) < 4)) {
  4410.     makestr(&cmdatemsg,"Ambiguous numeric date");
  4411.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4412.     return(NULL);
  4413.     } else if ((f2len > 4) && ft[2]) {
  4414.     makestr(&cmdatemsg,"Too many digits in year");
  4415.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4416.     return(NULL);
  4417.     } else {
  4418.     makestr(&cmdatemsg,"Unexpected date format");
  4419.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4420.     return(NULL);
  4421.     }
  4422.     x = atoi(month);
  4423.     sprintf(tmpbuf,"%02d",x);        /* 2-digit numeric month */
  4424.  
  4425. /*
  4426.    state = 1 = hours
  4427.    state = 2 = minutes
  4428.    state = 3 = seconds
  4429.    state = 4 = fractions of seconds
  4430. */
  4431.  
  4432.   dotime:
  4433.     if (isletter && (s == p)) {
  4434.     makestr(&cmdatemsg,"Unknown date-time word");
  4435.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4436.     return(NULL);
  4437.     }
  4438.     if (!year && yyyymmdd[0]) {
  4439.     debug(F110,"cmcvtdate dotime yyyymmdd",yyyymmdd,0);
  4440.     for (i = 0; i < 4; i++)
  4441.       yearbuf[i] = yyyymmdd[i];
  4442.     yearbuf[4] = NUL;
  4443.     monbuf[0] = yyyymmdd[4];
  4444.     monbuf[1] = yyyymmdd[5];
  4445.     monbuf[2] = NUL;
  4446.     daybuf[0] = yyyymmdd[6];
  4447.     daybuf[1] = yyyymmdd[7];
  4448.     daybuf[2] = NUL;
  4449.     day = daybuf;
  4450.     nday = atoi(daybuf);
  4451.     month = monbuf;
  4452.     year = yearbuf;
  4453.     }
  4454.     if (!year) {
  4455.     makestr(&cmdatemsg,"Internal error - date not defaulted");
  4456.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4457.     return(NULL);
  4458.     }
  4459.     /* Get here with day, month, and year set */
  4460.     debug(F110,"cmcvtdate dotime day",day,0);
  4461.     debug(F110,"cmcvtdate dotime month",month,0);
  4462.     debug(F110,"cmcvtdate dotime year",year,0);
  4463.     debug(F110,"cmcvtdate dotime s",s,0);
  4464.     debug(F110,"cmcvtdate dotime p",p,0);
  4465.     x = atoi(month);
  4466.     if (x > 12 || x < 1) {
  4467.     makestr(&cmdatemsg,"Month out of range");
  4468.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4469.     return(NULL);
  4470.     }
  4471.     nday  = atoi(day);
  4472.     i = mdays[x];
  4473.     if (x == 2) if (isleap(atoi(year))) i++;
  4474.     if (nday > i || nday < 1) {
  4475.     makestr(&cmdatemsg,"Day out of range");
  4476.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4477.     return(NULL);
  4478.     }
  4479.     if (!*p && t == 0) {
  4480.     sprintf(zbuf,"%04d%02d%02d",atoi(year),atoi(month),nday);    
  4481.     dp = zbuf;
  4482.     goto xcvtdate;
  4483.     }
  4484.     if (*p == '+' || *p == '-') {    /* GMT offset without a time */
  4485.     hh = 0;                /* so default time to 00:00:00 */
  4486.     mm = 0;
  4487.     ss = 0;
  4488.     goto cmtimezone;        /* and go do timezone */
  4489.     }
  4490.     if (*p && !isdigit(*p) && *p != ':') {
  4491.     makestr(&cmdatemsg,"Invalid time");
  4492.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4493.     return(NULL);
  4494.     }
  4495.     sprintf(yyyymmdd,"%s%s%02d",year,month,nday); /* for tz calculations... */
  4496.  
  4497.     state = 1;                /* Initialize time-parsing FSA */
  4498.     hh = 0;                /* hours */
  4499.     mm = 0;                /* minutes */
  4500.     ss = 0;                /* seconds */
  4501.     ff = -1;                /* fraction */
  4502.     d = 0;                /* Digit counter */
  4503.     p2 = p;                /* Preliminary digit count... */
  4504.     while (isdigit(*p2)) {
  4505.     d++;
  4506.     p2++;
  4507.     }
  4508.     if (d > 6) {
  4509.     makestr(&cmdatemsg,"Too many time digits");
  4510.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4511.     return(NULL);
  4512.     }
  4513.     d = (d & 1 && *p2 != ':') ? 1 : 0;    /* Odd implies leading '0' */
  4514.  
  4515.     while (*p) {            /* Get the time, if any */
  4516.     if (isdigit(*p)) {        /* digit */
  4517.         if (d++ > 1) {
  4518.         state++;
  4519.         d = 1;
  4520.         }
  4521.         switch (state) {
  4522.           case 1:            /* Hours */
  4523.         hh = hh * 10 + (*p - '0');
  4524.         break;
  4525.           case 2:            /* Minutes */
  4526.         mm = mm * 10 + (*p - '0');
  4527.         break;
  4528.           case 3:            /* Seconds */
  4529.         ss = ss * 10 + (*p - '0');
  4530.         break;
  4531.           case 4:            /* Fraction of second */
  4532.         if (ff < 0)
  4533.           ff = (*p > '4') ? 1 : 0;
  4534.         break;
  4535.         }
  4536.     } else if (*p == ':') {        /* Colon */
  4537.         state++;
  4538.         d = 0;
  4539.         if (state > 3) {
  4540.         makestr(&cmdatemsg,"Too many time fields");
  4541.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4542.         return(NULL);
  4543.         }
  4544.     } else if (*p == '.') {
  4545.         if (state == 3) {
  4546.         state = 4;
  4547.         d = 0;
  4548.         } else {
  4549.         makestr(&cmdatemsg,"Improper fraction");
  4550.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4551.         return(NULL);
  4552.         }
  4553.     } else if (*p == SP) {        /* Space */
  4554.         while (*p && (*p == SP))    /* position to first nonspace */
  4555.           p++;
  4556.         break;
  4557.     } else if (isalpha(*p)) {    /* AM/PM/Z or timezone */
  4558.         break;
  4559.     } else if (*p == '+' || *p == '-') { /* GMT offset */
  4560.         break;
  4561.     } else {
  4562.         makestr(&cmdatemsg,"Invalid time characters");
  4563.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4564.         return(NULL);
  4565.     }
  4566.     p++;
  4567.     }
  4568.     if (!*p)                /* If nothing left */
  4569.       goto xcmdate;            /* go finish up */
  4570.  
  4571.     /* At this point we have HH, MM, SS, and FF */
  4572.     /* Now handle the rest: AM, PM, and/or timezone info */
  4573.  
  4574.     if (!ckstrcmp(p,"am",2,0)) {    /* AM/PM... */
  4575.     pmflag = 0;
  4576.     p += 2;
  4577.     } else if (!ckstrcmp(p,"a.m.",4,0)) {
  4578.     pmflag = 0;
  4579.     p += 4;
  4580.     } else if (!ckstrcmp(p,"pm",2,0)) {
  4581.     pmflag = 1;
  4582.     p += 2;
  4583.     } else if (!ckstrcmp(p,"p.m.",4,0)) {
  4584.     pmflag = 1;
  4585.     p += 4;
  4586.     }
  4587.     if (pmflag && hh < 12)        /* If PM was given */
  4588.       hh += 12;                /* add 12 to the hour */
  4589.  
  4590.     /* Now handle timezone */
  4591.  
  4592.   cmtimezone:
  4593.     debug(F110,"cmcvtdate timezone",p,0);
  4594.  
  4595.     zhh = 0;                /* GMT offset HH */
  4596.     zmm = 0;                /* GMT offset MM */
  4597.     gmtsign = 0;            /* Sign of GMT offset */
  4598.     isgmt = 0;                /* 1 if time is GMT */
  4599.  
  4600.     while (*p && *p == SP)        /* Gobble spaces */
  4601.       p++;
  4602.     if (!*p)                /* If nothing left */
  4603.       goto xcmdate;            /* we're done */
  4604.  
  4605.     if (isalpha(*p)) {            /* Something left */
  4606.     int zone = 0;            /* Alphabetic must be timezone */
  4607.     p2 = p;                /* Isolate timezone */
  4608.     p++;
  4609.     while (isalpha(*p))
  4610.       p++;
  4611.     p3 = p;
  4612.     cc = *p;
  4613.     *p = NUL;
  4614.     p = p2;                /* Have timezone, look it up */
  4615.     zone = lookup(usatz,p,nusatz,NULL);
  4616.     debug(F111,"cmcvtdate timezone alpha",p,zone);
  4617.  
  4618.     if (zone < 0) {            /* Not found */
  4619.         makestr(&cmdatemsg,"Unknown timezone");
  4620.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4621.         return(NULL);
  4622.     }
  4623.     isgmt++;            /* All dates are GMT from here down */
  4624.     if (zone != 0) {        /* But not this one so make it GMT */
  4625.         hh += zone;            /* RFC 822 timezone: EST etc */
  4626.         if (hh > 23) {        /* Offset crosses date boundary */
  4627.         long jd;
  4628.         jd = mjd(yyyymmdd);    /* Get MJD */
  4629.         jd += hh / 24;        /* Add new day(s) */
  4630.         hh = hh % 24;        /* and convert back to yyyymmdd */
  4631.         ckstrncpy(yyyymmdd,mjd2date(jd),YYYYMMDD);
  4632.         }
  4633.     }
  4634.     p = p3;                /* Put back whatever we poked above */
  4635.     *p = cc;
  4636.  
  4637.     } else if (*p == '+' || *p == '-') { /* GMT/UTC offset */
  4638.     p3 = p;
  4639.     debug(F110,"cmcvtdate timezone GMT offset",p,0);
  4640.     gmtsign = (*p == '+') ? -1 : 1;
  4641.     isgmt++;
  4642.     p++;
  4643.     while (*p == SP) p++;
  4644.     d = 0;
  4645.     p2 = p;
  4646.     while (isdigit(*p)) {        /* Count digits */
  4647.         d++;
  4648.         p++;
  4649.     }
  4650.     if (d != 4) {            /* Strict RFC [2]822 */
  4651.         isgmt = 0;            /* If not exactly 4 digits */
  4652.         p = p3;            /* it's not a GMT offset. */
  4653.         goto delta;            /* So treat it as a delta time. */
  4654.     }
  4655.     d = (d & 1 && *p != ':') ? 1 : 0; /* Odd implies leading '0' */
  4656.     p = p2;
  4657.     debug(F111,"cmcvtdate GMT offset sign",p,gmtsign);
  4658.     debug(F101,"cmcvtdate GMT offset d","",d);
  4659.     state = 1;
  4660.     while (*p) {
  4661.         if (isdigit(*p)) {        /* digit */
  4662.         if (d++ > 1) {
  4663.             state++;
  4664.             d = 1;
  4665.         }
  4666.         switch (state) {
  4667.           case 1:
  4668.             zhh = zhh * 10 + (*p - '0');
  4669.             break;
  4670.           case 2:
  4671.             zmm = zmm * 10 + (*p - '0');
  4672.             break;
  4673.           default:        /* Ignore seconds or fractions */
  4674.             break;
  4675.         }            
  4676.         } else if (*p == ':') {    /* Colon */
  4677.         state++;
  4678.         d = 0;
  4679.         } else if (*p == SP || *p == '(') {
  4680.         break;
  4681.         } else {
  4682.         p = p3;            /* Maybe it's not a GMT offset. */
  4683.         goto delta;        /* So treat it as a delta time. */
  4684.         }
  4685.         p++;
  4686.     }
  4687.     }
  4688.     debug(F110,"cmcvtdate after timezone",p,0);
  4689.  
  4690.     if (*p) {                /* Anything left? */
  4691.     p2 = p;
  4692.     while (*p2 == SP)        /* Skip past spaces */
  4693.       p2++;
  4694.     if (*p2 == '(') {        /* RFC-822 comment? */
  4695.         int pc = 1;            /* paren counter */
  4696.         p2++;
  4697.         while (*p2) {
  4698.         if (*p2 == ')') {
  4699.             if (--pc == 0) {
  4700.             p2++;
  4701.             break;
  4702.             }
  4703.         } else if (*p2 == ')') {
  4704.             pc++;
  4705.         }
  4706.         p2++;
  4707.         }        
  4708.         while (*p2 == SP)        /* Skip past spaces */
  4709.           p2++;
  4710.         if (!*p2)            /* Anything left? */
  4711.           *p = NUL;            /* No, erase comment */
  4712.     }
  4713.     if (!*p2)            /* Anything left? */
  4714.       goto xcmdate;            /* No, done. */
  4715.     p = p2;
  4716.  
  4717.       delta:
  4718.     if (*p == '+' || *p == '-') {    /* Delta time */
  4719.         int state = NEED_DAYS;    /* Start off looking for days */
  4720.         char c = 0;
  4721.         dsign = 1;            /* Get sign */
  4722.         if (*p++ == '-')
  4723.           dsign = -1;
  4724.         while (*p == SP)        /* Skip intervening spaces */
  4725.           p++;
  4726.         while (state) {        /* FSA to parse delta time */
  4727.         if (state < 0 || !isdigit(*p)) {
  4728.             makestr(&cmdatemsg,"Invalid delta time");
  4729.             debug(F111,"cmcvtdate",cmdatemsg,-1);
  4730.             return(NULL);
  4731.         }
  4732.         p2 = p;            /* Get next numeric field */
  4733.         while (isdigit(*p2))
  4734.           p2++;
  4735.         c = *p2;        /* And break character */
  4736.         *p2 = NUL;        /* Terminate the number */
  4737.  
  4738.         switch (state) {    /* Interpret according to state */
  4739.           case NEED_DAYS:    /* Initial */
  4740.             if ((c == '-') ||    /* VMS format */
  4741.             ((c == 'd' || c == 'D')
  4742.              && !isalpha(*(p2+1)))) { /* Days */
  4743.             ddays = atoi(p);
  4744.             if (!*(p2+1))            
  4745.               state = 0;
  4746.             else              /* if anything is left */
  4747.               state = NEED_HRS;   /* now we want hours. */
  4748.             } else if ((c == 'W' || c == 'w') && !isalpha(*(p2+1))) {
  4749.             ddays = atoi(p) * 7;   /* weeks... */
  4750.             if (!*(p2+1))            
  4751.               state = 0;
  4752.             else
  4753.               state = NEED_HRS;
  4754.             } else if ((c == 'M' || c == 'm') && !isalpha(*(p2+1))) {
  4755.             dmonths = atoi(p); /* months... */
  4756.             if (!*(p2+1))            
  4757.               state = 0;
  4758.             else
  4759.               state = NEED_HRS;
  4760.             } else if ((c == 'Y' || c == 'y') && !isalpha(*(p2+1))) {
  4761.             dyears = atoi(p); /* years... */
  4762.             if (!*(p2+1))            
  4763.               state = 0;
  4764.             else
  4765.               state = NEED_HRS;
  4766.             } else if (c == ':') { /* delimiter is colon */
  4767.             dhours = atoi(p);  /* so it's hours */
  4768.             state = NEED_MINS; /* now we want minutes */
  4769.             } else if (!c) {       /* end of string */
  4770.             dhours = atoi(p);  /* it's still hours */
  4771.             state = 0;         /* and we're done */
  4772.             } else if (isalpha(c) || c == SP) {
  4773.             if (c == SP) {    /* It's a keyword? */
  4774.                 p2++;    /* Skip spaces */
  4775.                 while (*p2 == SP)
  4776.                   p2++;
  4777.             } else {    /* or replace first letter */
  4778.                 *p2 = c;
  4779.             }
  4780.             p3 = p2;    /* p2 points to beginning of keyword */
  4781.             while (isalpha(*p3)) /* Find end of keyword */
  4782.               p3++;
  4783.             c = *p3;    /* NUL it out so we can look it up */
  4784.             if (*p3)    /* p3 points to keyword terminator */
  4785.               *p3 = NUL;
  4786.             units = lookup(timeunits,p2,nunits,NULL);
  4787.             if (units < 0) {
  4788.                 makestr(&cmdatemsg,"Invalid units in delta time");
  4789.                 debug(F111,"cmcvtdate",cmdatemsg,-1);
  4790.                 return(NULL);
  4791.             }
  4792.             *p2 = NUL;    /* Re-terminate the number */
  4793.             *p3 = c;
  4794.             while (*p3 == SP) /* Point at field after units */
  4795.               p3++;
  4796.             p2 = p3;
  4797.             switch (units) {
  4798.               case TU_DAYS:
  4799.                 ddays = atoi(p);
  4800.                 break;
  4801.               case TU_WEEKS:
  4802.                 ddays = atoi(p) * 7;
  4803.                 break;
  4804.               case TU_MONTHS:
  4805.                 dmonths = atoi(p);
  4806.                 break;
  4807.               case TU_YEARS:
  4808.                 dyears = atoi(p);
  4809.                 break;
  4810.             }
  4811.             if (*p2) {
  4812.                 state = NEED_HRS;
  4813.                 p2--;
  4814.             } else
  4815.               state = 0;
  4816.  
  4817.             } else {        /* Anything else */
  4818.             state = -1;    /* is an error */
  4819.             }
  4820.             break;
  4821.           case NEED_HRS:    /* Looking for hours */
  4822.             debug(F000,"cmcvtdate NEED_HRS",p,c);
  4823.             if (c == ':') {
  4824.             dhours = atoi(p);
  4825.             state = NEED_MINS;
  4826.             } else if (!c) {
  4827.             dhours = atoi(p);
  4828.             state = 0;
  4829.             } else {
  4830.             state = -1;
  4831.             }
  4832.             break;
  4833.           case NEED_MINS:    /* Looking for minutes */
  4834.             if (c == ':') {
  4835.             dmins = atoi(p);
  4836.             state = NEED_SECS;
  4837.             } else if (!c) {
  4838.             dmins = atoi(p);
  4839.             state = 0;
  4840.             } else {
  4841.             state = -1;
  4842.             }
  4843.             break;
  4844.           case NEED_SECS:    /* Looking for seconds */
  4845.             if (c == '.') {
  4846.             dsecs = atoi(p);
  4847.             state = NEED_FRAC;
  4848.             } else if (!c) {
  4849.             dsecs = atoi(p);
  4850.             state = 0;
  4851.             } else {
  4852.             state = -1;
  4853.             }
  4854.             break;
  4855.           case NEED_FRAC:    /* Fraction of second */
  4856.             if (!c && rdigits(p)) {
  4857.             if (*p > '4')
  4858.               dsecs++;
  4859.             state = 0;
  4860.             } else {
  4861.             state = -1;
  4862.             }
  4863.             break;
  4864.         }
  4865.         if (c)            /* next field if any */
  4866.           p = p2 + 1;
  4867.         }
  4868.         havedelta = 1;
  4869.  
  4870.     } else {
  4871.         makestr(&cmdatemsg,"Extraneous material at end");
  4872.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  4873.         return(NULL);
  4874.     }
  4875.     }
  4876.  
  4877.  xcmdate:
  4878.  
  4879.     if ((t != 2 && hh > 24) || hh < 0) { /* Hour range check */
  4880.     makestr(&cmdatemsg,"Invalid hours");
  4881.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4882.     return(NULL);
  4883.     }
  4884.     if (mm > 59) {            /* Minute range check */
  4885.     makestr(&cmdatemsg,"Invalid minutes");
  4886.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4887.     return(NULL);
  4888.     }
  4889.     if (ff > 0) {            /* Fraction of second? */
  4890.     if (ss < 59) {
  4891.         ss++;
  4892.         ff = 0;
  4893.     } else if (mm < 59) {
  4894.         ss = 0;
  4895.         mm++;
  4896.         ff = 0;
  4897.     } else if (hh < 24) {
  4898.         ss = 0;
  4899.         mm = 0;
  4900.         hh++;
  4901.         ff = 0;
  4902.     }
  4903.     /* Must add a day -- leave ff at 1... */
  4904.     /* (DO SOMETHING ABOUT THIS LATER) */
  4905.     }
  4906.     if (ss > 60) {            /* Seconds range check */
  4907.     makestr(&cmdatemsg,"Invalid seconds"); /* 60 is ok because of */
  4908.     debug(F111,"cmcvtdate",cmdatemsg,-1);  /* Leap Second. */
  4909.     return(NULL);
  4910.     }
  4911.     if ((mm < 0 || ss < 0) ||
  4912.     (t != 2 && (ss > 0 || mm > 0) && hh > 23)) {
  4913.     makestr(&cmdatemsg,"Invalid minutes or seconds");
  4914.     debug(F111,"cmcvtdate",cmdatemsg,-1);
  4915.     return(NULL);
  4916.     }
  4917.     debug(F110,"cmcvtdate year",year,0);
  4918.     debug(F110,"cmcvtdate month",month,0);
  4919.     debug(F101,"cmcvtdate nday","",nday);
  4920.     debug(F101,"cmcvtdate hh","",hh);
  4921.     debug(F101,"cmcvtdate mm","",mm);
  4922.     debug(F101,"cmcvtdate ss","",ss);
  4923.     debug(F101,"cmcvtdate gmtsign","",gmtsign);
  4924.     debug(F101,"cmcvtdate zhh","",zhh);
  4925.     debug(F101,"cmcvtdate zmm","",zmm);
  4926.     debug(F101,"cmcvtdate isgmt","",isgmt);
  4927.  
  4928. #ifdef ZLOCALTIME
  4929. /* Handle timezone -- first convert to GMT */
  4930.  
  4931.     zdd = 0;                /* Days changed */
  4932.     if (isgmt && (zmm || zhh)) {    /* If GMT offset given */
  4933.     long sec1, sec2, zz;
  4934.     sec1 = ss + 60 * mm + 3600 * hh;
  4935.     sec2 = gmtsign * (60 * zmm + 3600 * zhh);
  4936.     sec1 += sec2;
  4937.     if (sec1 < 0) {
  4938.         sec1 = 0 - sec1;
  4939.         zdd = 0L - (sec1 / 86400L);
  4940.         sec1 = sec1 % 86400L;
  4941.     } else if (sec1 > 86400L) {
  4942.         zdd = sec1 / 86400L;
  4943.         sec1 = sec1 % 86400L;
  4944.     }
  4945.     ss = sec1 % 60;
  4946.     zz = sec1 / 60;
  4947.     mm = zz % 60;
  4948.     hh = zz / 60;
  4949.     debug(F101,"cmcvtdate NEW hh","",hh);
  4950.     debug(F101,"cmcvtdate NEW mm","",mm);
  4951.     debug(F101,"cmcvtdate NEW dd","",zdd);
  4952.  
  4953. /* At this point hh:mm:ss is in GMT and zdd is the calendar adjustment */
  4954.  
  4955.     }
  4956. #endif /* ZLOCALTIME */
  4957.  
  4958.     sprintf(zbuf,"%04d%02d%02d %02d:%02d:%02d", /* SAFE */
  4959.         atoi(year),atoi(month),nday,hh,mm,ss
  4960.         );
  4961.     dp = zbuf;
  4962.  
  4963. #ifdef ZLOCALTIME
  4964.     /* Now convert from GMT to local time */
  4965.  
  4966.     if (isgmt) {            /* If GMT convert to local time */
  4967.     debug(F110,"cmcvtdate GMT 1",dp,0);
  4968.     if (zdd) {            /* Apply any calendar adjustment */
  4969.         long zz;
  4970.         zz = mjd(dp) + zdd;
  4971.         sprintf(zbuf,"%s %02d:%02d:%02d",mjd2date(zz),hh,mm,ss);
  4972.     }
  4973.     debug(F110,"cmcvtdate GMT 2",dp,0);
  4974.     if ((p = zlocaltime(dp))) {
  4975.         debug(F110,"cmcvtdate asctime zlocaltime",p,0);
  4976.         if (p) ckstrncpy(zbuf,p,18);
  4977.     }
  4978.     debug(F110,"cmcvtdate GMT 3",dp,0);
  4979.     for (i = 0; i < 4; i++)
  4980.       yearbuf[i] = dp[i];
  4981.     yearbuf[4] = NUL;
  4982.     monbuf[0] = dp[4];
  4983.     monbuf[1] = dp[5];
  4984.     monbuf[2] = NUL;
  4985.     daybuf[0] = dp[6];
  4986.     daybuf[1] = dp[7];
  4987.     daybuf[2] = NUL;
  4988.     day = daybuf;
  4989.     nday = atoi(daybuf);
  4990.     month = monbuf;
  4991.     year = yearbuf;
  4992.     hh = atoi(&dp[9]);
  4993.     mm = atoi(&dp[12]);
  4994.     ss = atoi(&dp[15]);
  4995.     }
  4996. #endif /* ZLOCALTIME */
  4997.  
  4998. #ifdef DEBUG
  4999.     if (deblog) {
  5000.     debug(F101,"cmcvtdate hour","",hh);
  5001.     debug(F101,"cmcvtdate minute","",mm);
  5002.     debug(F101,"cmcvtdate second","",ss);
  5003.     }
  5004. #endif /* DEBLOG */
  5005.  
  5006.     makestr(&cmdatemsg,NULL);
  5007.     if (havedelta) {
  5008. #ifdef DEBUG
  5009.     if (deblog) {
  5010.         debug(F110,"cmcvtdate base ",dp,0);
  5011.         debug(F101,"cmcvtdate delta sign","",dsign);
  5012.         debug(F101,"cmcvtdate delta yrs ","",dyears);
  5013.         debug(F101,"cmcvtdate delta mos ","",dmonths);
  5014.         debug(F101,"cmcvtdate delta days","",ddays);
  5015.         debug(F101,"cmcvtdate delta hrs ","",dhours);
  5016.         debug(F101,"cmcvtdate delta mins","",dmins);
  5017.         debug(F101,"cmcvtdate delta secs","",dsecs);
  5018.     }
  5019. #endif /* DEBLOG */
  5020.     if (!(dp = cmdelta(atoi(year),
  5021.             atoi(month),
  5022.             nday, hh, mm, ss,
  5023.             dsign, dyears, dmonths, ddays, dhours, dmins, dsecs))) {
  5024.         debug(F111,"cmcvtdate",cmdatemsg,-1);
  5025.         return(NULL);
  5026.     }
  5027.     }
  5028.  
  5029.   xcvtdate:                /* Exit point for success */
  5030.     {
  5031.     int len, k, n;
  5032.     char * p;
  5033.     if (!dp) dp = "";        /* Shouldn't happen */
  5034.     if (!*dp) return(NULL);        /* ... */
  5035.     len = strlen(dp);
  5036.     debug(F111,"cmcvtdate result",dp,len);
  5037.     k = cmdatebp - (char *)cmdatebuf; /* Space used */
  5038.     n = CMDATEBUF - k - 1;        /* Space left */
  5039.     if (n < len) {            /* Not enough? */
  5040.         cmdatebp = cmdatebuf;    /* Wrap around */
  5041.         n = CMDATEBUF;
  5042.     }
  5043.     ckstrncpy(cmdatebp,dp,n);
  5044.     p = cmdatebp;
  5045.     cmdatebp += len + 1;
  5046.     return(p);
  5047.     }
  5048. }
  5049.  
  5050. int
  5051. cmvdate(d) char * d; {            /* Verify date-time */
  5052.     int i;
  5053.     if (!d) return(0);
  5054.     if ((int)strlen(d) != 17) return(0);
  5055.     for (i = 0; i < 8; i++) { if (!isdigit(d[i])) return(0); }
  5056.     if (!isdigit(d[9])  || !isdigit(d[10]) ||
  5057.     !isdigit(d[12]) || !isdigit(d[13]) ||
  5058.     !isdigit(d[15]) || !isdigit(d[16]))
  5059.       return(0);
  5060.     if (!ckstrchr(" Tt_-:",d[8])) return(0);
  5061.     if (d[11] != ':' && d[14] != ':') return(0);
  5062.     return(1);
  5063. }
  5064.  
  5065. /* c m d i f f d a t e  --  Get difference between two date-times */
  5066.  
  5067. char *
  5068. cmdiffdate(d1,d2) char * d1, * d2; {
  5069.     char d1buf[9], d2buf[9];
  5070.     char x1buf[18], x2buf[18];
  5071.     char * p;
  5072.  
  5073.     int hh1 = 0, mm1 = 0, ss1 = 0;
  5074.     int hh2 = 0, mm2 = 0, ss2 = 0;
  5075.     int hh, mm, ss;
  5076.     int sign;
  5077.     long jd1, jd2, jd, f1, f2, fx;
  5078.     static char result[24], *rp;
  5079.  
  5080.     debug(F110,"cmdiffdate d1 A",d1,0);
  5081.     debug(F110,"cmdiffdate d2 A",d2,0);
  5082.  
  5083.     if (!(p = cmcvtdate(d1,1)))        /* Convert dates to standard format */
  5084.       return(NULL);
  5085.     ckstrncpy(x1buf,p,18);
  5086.     d1 = x1buf;
  5087.  
  5088.     if (!(p = cmcvtdate(d2,1)))
  5089.       return(NULL);
  5090.     ckstrncpy(x2buf,p,18);
  5091.     d2 = x2buf;
  5092.  
  5093.     debug(F110,"cmdiffdate d1 B",d1,0);
  5094.     debug(F110,"cmdiffdate d2 B",d2,0);
  5095.     if (!cmvdate(d1) || !cmvdate(d2))
  5096.       return(NULL);
  5097.  
  5098.     hh1 = atoi(&d1[9]);            /* Get hours, minutes, and seconds */
  5099.     mm1 = atoi(&d1[12]);        /* for first date */
  5100.     ss1 = atoi(&d1[15]);
  5101.     ckstrncpy(d1buf,d1,9);
  5102.  
  5103.     hh2 = atoi(&d2[9]);            /* ditto for second date */
  5104.     mm2 = atoi(&d2[12]);
  5105.     ss2 = atoi(&d2[15]);
  5106.     ckstrncpy(d2buf,d2,9);
  5107.     
  5108.     jd1 = mjd(d1buf);            /* Get the two Julian dates */
  5109.     jd2 = mjd(d2buf);
  5110.     f1 = ss1 + 60 * mm1 + 3600 * hh1;    /* Convert first time to seconds */
  5111.  
  5112.     f2 = ss2 + 60 * mm2 + 3600 * hh2;    /* Ditto for second time */
  5113.     debug(F101,"cmdiffdate jd1","",jd1);
  5114.     debug(F101,"cmdiffdate f1","",f1);
  5115.     debug(F101,"cmdiffdate jd2","",jd2);
  5116.     debug(F101,"cmdiffdate f2","",f2);
  5117.   
  5118.     if (jd2 > jd1 || (jd1 == jd2 && f2 > f1)) {
  5119.         sign = -1; 
  5120.         if (f1 > f2) {jd2--; f2 += 86400L;}
  5121.         jd = jd2 - jd1;
  5122.         fx = f2 - f1;
  5123.     } else {
  5124.         sign = 1;
  5125.         if (f2 > f1) {jd1--; f1 += 86400L;}
  5126.         jd = jd1 - jd2;
  5127.         fx = f1 - f2;
  5128.     }
  5129.     debug(F111,"cmdiffdate sign jd",sign<0?"-":"+",jd);
  5130.     debug(F101,"cmdiffdate fx","",fx);
  5131.   
  5132.     hh = (int) (fx / 3600L);        /* Convert seconds to hh:mm:ss */
  5133.  
  5134.     mm = (int) (fx % 3600L) / 60L;
  5135.     ss = (int) (fx % 3600L) % 60L;
  5136.  
  5137.     rp = result;            /* Format the result */
  5138.     *rp++ = (sign < 0) ? '-' : '+';
  5139.     if (jd != 0 && hh+mm+ss == 0) {
  5140.     sprintf(rp,"%ldd",jd);
  5141.     } else if (jd == 0) {
  5142.     if (ss == 0)
  5143.       sprintf(rp,"%d:%02d",hh,mm);
  5144.     else
  5145.       sprintf(rp,"%d:%02d:%02d",hh,mm,ss);
  5146.     } else {
  5147.     if (ss == 0)
  5148.       sprintf(rp,"%ldd%d:%02d",jd,hh,mm);
  5149.     else
  5150.       sprintf(rp,"%ldd%d:%02d:%02d",jd,hh,mm,ss);
  5151.     }
  5152.     debug(F110,"cmdiffdate result",result,0);
  5153.     return((char *)result);
  5154. }
  5155.  
  5156. /* s h u f f l e d a t e  --  Rearrange date string */
  5157.  
  5158. /*
  5159.   Call with:
  5160.     A date string in standard format: yyyymmdd hh:mm:ss (time optional).
  5161.     Options:
  5162.       1: Reformat date to yyyy-mmm-dd (mmm = English month abbreviation).
  5163.       2. Reformat date to dd-mmm-yyyy (mmm = English month abbreviation).
  5164.     Returns:
  5165.       Pointer to result if args valid, otherwise original arg pointer.
  5166. */
  5167. char *
  5168. shuffledate(p,opt) char * p; int opt; {
  5169.     int len;
  5170.     char ibuf[32];
  5171.     static char obuf[48];
  5172.     char c;
  5173.     int yy, dd, mm;
  5174.  
  5175.     if (!p) p = "";
  5176.     if (!*p) p = ckdate();
  5177.     if (opt < 1 || opt > 2)
  5178.       return(p);
  5179.     len = strlen(p);
  5180.     if (len < 8 || len > 31) return(p);
  5181.     ckstrncpy(ibuf,p,32);
  5182.     c = ibuf[4];            /* Warning: not Y10K compliant */
  5183.     ibuf[4] = NUL;
  5184.     if (!rdigits(ibuf))
  5185.       return(p);
  5186.     yy = atoi(ibuf);
  5187.     if (yy < 1 || yy > 9999)
  5188.       return(p);
  5189.     ibuf[4] = c;
  5190.     c = ibuf[6];
  5191.     ibuf[6] = NUL;
  5192.     if (!rdigits(&ibuf[4]))
  5193.       return(p);
  5194.     mm = atoi(&ibuf[4]);
  5195.     if (mm < 1 || mm > 12)
  5196.       return(p);
  5197.     ibuf[6] = c;
  5198.     c = ibuf[8];
  5199.     ibuf[8] = NUL;
  5200.     if (!rdigits(&ibuf[6]))
  5201.       return(p);
  5202.     dd = atoi(&ibuf[6]);
  5203.     ibuf[8] = c;
  5204.     if (dd < 1 || mm > 31)
  5205.       return(p);
  5206.     /* IGNORE WARNINGS ABOUT moname[] REFS OUT OF RANGE - it's prechecked. */
  5207.     switch (opt) {
  5208.       case 1:
  5209.     sprintf(obuf,"%04d-%s-%02d%s",yy,moname[mm-1],dd,&ibuf[8]);
  5210.     break;
  5211.       case 2:
  5212.     sprintf(obuf,"%02d-%s-%04d%s",dd,moname[mm-1],yy,&ibuf[8]);
  5213.     }
  5214.     return((char *)obuf);
  5215. }
  5216.  
  5217. /*  C K C V T D A T E  --  Like cmcvtdate(), but returns string.  */
  5218. /*  For use by date-related functions */
  5219. /*  See calling conventions for cmcvtdate() above. */
  5220.  
  5221. char *
  5222. ckcvtdate(p,t) char * p; int t; {
  5223.     char * s;
  5224.     if (!(s = cmcvtdate(p,t)))
  5225.       return("<BAD_DATE_OR_TIME>");    /* \fblah() error message */
  5226.     else
  5227.       return(s);
  5228. }
  5229.  
  5230.  
  5231. /*  C M D A T E  --  Parse a date and/or time  */
  5232.  
  5233. /*
  5234.   Accepts date in various formats.  If the date is recognized,
  5235.   this routine returns 0 or greater with the result string pointer
  5236.   pointing to a buffer containing the date as "yyyymmdd hh:mm:ss".
  5237. */
  5238. int
  5239. cmdate(xhlp,xdef,xp,quiet,f) char *xhlp, *xdef, **xp; int quiet; xx_strp f; {
  5240.     int x, rc;
  5241.     char *o, *s, *zq, *dp;
  5242.  
  5243.     cmfldflgs = 0;
  5244.     if (!xhlp) xhlp = "";
  5245.     if (!xdef) xdef = "";
  5246.     if (!*xhlp) xhlp = "Date and/or time";
  5247.     *xp = "";
  5248.  
  5249.     rc = cmfld(xhlp,xdef,&s,(xx_strp)0);
  5250.     debug(F101,"cmdate cmfld rc","",rc);
  5251.     if (rc < 0)
  5252.       return(rc);
  5253.     debug(F110,"cmdate 1",s,0);
  5254.     o = s;                /* Remember what they typed. */
  5255.     s = brstrip(s);
  5256.     debug(F110,"cmdate 2",s,0);
  5257.  
  5258.     x = 0;
  5259.     if (f) {                /* If a conversion function is given */
  5260.     char * pp;
  5261.     zq = atxbuf;            /* do the conversion. */
  5262.     pp = atxbuf;
  5263.     atxn = CMDBL;
  5264.     if ((x = (*f)(s,&zq,&atxn)) < 0) return(-2);
  5265.     if (!*pp)
  5266.       pp = xdef;
  5267.     if (setatm(pp,0) < 0) {
  5268.         if (!quiet) printf("?Evaluated date too long\n");
  5269.         return(-9);
  5270.     }
  5271.     s = atxbuf;
  5272.     }
  5273.     dp = cmcvtdate(s,1);
  5274.     if (!dp) {
  5275.     if (!quiet) printf("?%s\n",cmdatemsg);
  5276.     return(-9);
  5277.     }
  5278.     *xp = dp;
  5279.     return(0);
  5280. }
  5281.  
  5282. #ifdef CK_RECALL            /* Command-recall functions */
  5283.  
  5284. /*  C M R I N I  --  Initialize or change size of command recall buffer */
  5285.  
  5286. int
  5287. cmrini(n) int n; {
  5288.     int i;
  5289.     if (recall && in_recall) {        /* Free old storage, if any */
  5290.     for (i = 0; i < cm_recall; i++) {
  5291.         if (recall[i]) {
  5292.         free(recall[i]);
  5293.         recall[i] = NULL;
  5294.         }
  5295.     }
  5296.     free(recall);
  5297.     recall = NULL;
  5298.     }
  5299.     cm_recall = n;            /* Set new size */
  5300.     rlast = current = -1;        /* Initialize pointers */
  5301.     if (n > 0) {
  5302.     recall = (char **)malloc((cm_recall + 1) * sizeof(char *));
  5303.     if (!recall)
  5304.       return(1);
  5305.     for (i = 0; i < cm_recall; i++) {
  5306.         recall[i] = NULL;
  5307.     }
  5308.     in_recall = 1;            /* Recall buffers init'd */
  5309.     }
  5310.     return(0);
  5311. }
  5312.  
  5313. /*  C M A D D N E X T  --  Force addition of next command */
  5314.  
  5315. VOID
  5316. cmaddnext() {
  5317.     if (on_recall && in_recall) {    /* Even if it doesn't come */
  5318.     force_add = 1;            /* from the keyboard */
  5319.     no_recall = 0;
  5320.     }
  5321. }
  5322.  
  5323. /*  C M G E T C M D  --  Find most recent matching command  */
  5324.  
  5325. char *
  5326. cmgetcmd(s) char * s; {
  5327.     int i;
  5328.     for (i = current; i >= 0; i--) {    /* Search backward thru history list */
  5329.     if (!recall[i]) continue;    /* This one's null, skip it */
  5330.     if (ckmatch(s,recall[i],0,1))    /* Match? */
  5331.       return(recall[i]);        /* Yes, return pointer */
  5332.     }
  5333.     return(NULL);            /* No match, return NULL pointer */
  5334. }
  5335. #endif /* CK_RECALL */
  5336.  
  5337. /*  A D D C M D  --  Add a command to the recall buffer  */
  5338.  
  5339. VOID
  5340. addcmd(s) char * s; {
  5341.     int len = 0, nq = 0;
  5342.     char * p;
  5343. #ifdef CKLEARN
  5344.     extern int learning;
  5345. #endif /* CKLEARN */
  5346.  
  5347.     if (xcmdsrc)            /* Only for interactive commands */
  5348.       return;
  5349.  
  5350.     if (!newcmd)            /* The command has been here already */
  5351.       return;                /* so ignore it. */
  5352.     newcmd = 0;                /* It's new but do this only once. */
  5353.  
  5354.     if (!s) s = cmdbuf;
  5355.     if (s[0])
  5356.       len = strlen(s);
  5357.  
  5358.     if (len < 1)            /* Don't save empty commands */
  5359.       return;
  5360.  
  5361.     p = s;
  5362.     while (*p) { if (*p++ == '?') nq++; } /* Count question marks */
  5363.  
  5364. #ifdef CKLEARN
  5365.     if (learning)            /* If a learned script is active */
  5366.       learncmd(s);            /* record this command. */
  5367. #endif /* CKLEARN */
  5368.  
  5369.     debug(F010,"CMD(P)",s,0);        /* Maybe record it in the debug log */
  5370.  
  5371. #ifdef CKSYSLOG
  5372.     if (ckxlogging) {            /* Maybe record it in syslog */
  5373.     if (ckxsyslog >= SYSLG_CX || ckxsyslog >= SYSLG_CM)
  5374.       cksyslog(SYSLG_CX, 1, "command", s, NULL);
  5375.     }
  5376. #endif /* CKSYSLOG */
  5377.  
  5378. #ifdef CK_RECALL
  5379.     last_recall = 0;
  5380.  
  5381.     if (on_recall &&            /* Command recall is on? */
  5382.     cm_recall > 0 &&        /* Recall buffer size is > 0? */
  5383.     !no_recall) {            /* Not not saving this command? */
  5384.  
  5385.     if (!force_add && rlast > -1)    /* If previous command was identical */
  5386.       if (!strcmp(s,recall[rlast])) /* don't add another copy */
  5387.         return;
  5388.  
  5389.     force_add = 0;            /* Reset now in case it was set */
  5390.  
  5391.         if (rlast >= cm_recall - 1) {    /* Recall buffer full? */
  5392.         int i;
  5393.         if (recall[0]) {        /* Discard oldest command */
  5394.         free(recall[0]);
  5395.         recall[0] = NULL;
  5396.         }
  5397.         for (i = 0; i < rlast; i++) {  /* The rest */
  5398.         recall[i] = recall[i+1];   /* move back */
  5399.         }
  5400.         rlast--;            /* Now we have one less */
  5401.     }
  5402.         rlast++;            /* Index of last command in buffer */
  5403.     current = rlast;        /* Also now the current command */
  5404.     if (current >= cm_recall) {    /* Shouldn't happen */
  5405.         printf("?Command history error\n");    /* but if it does */
  5406.         on_recall = 0;                /* turn off command saving */
  5407. #ifdef COMMENT
  5408.     } else if (nq > 0) {        /* Have at least one question mark */
  5409.         recall[current] = malloc(len+nq+1);
  5410.         if (recall[current]) {
  5411.         p = recall[current];
  5412.         while (*s) {
  5413.             if (*s == '?')
  5414.               *p++ = '\\';
  5415.             *p++ = *s++;
  5416.         }
  5417.         *p = NUL;
  5418.         }
  5419. #endif /* COMMENT */
  5420.     } else {            /* Normal case, just copy */
  5421.         recall[current] = malloc(len+1);
  5422.         if (recall[current])
  5423.           ckstrncpy(recall[current],s,len+1);
  5424.     }
  5425.     }
  5426. #endif /* CK_RECALL */
  5427. }
  5428.  
  5429.  
  5430. #ifdef CK_RECALL
  5431.  
  5432. /* C M H I S T O R Y */
  5433.  
  5434. VOID
  5435. cmhistory() {
  5436.     int i, lc = 1;
  5437.     for (i = 0; i <= current; i++) {
  5438.     printf(" %s\n", recall[i]);
  5439.     if (++lc > (cmd_rows - 2)) {    /* Screen full? */
  5440.         if (!askmore())        /* Do more-prompting... */
  5441.           break;
  5442.         else
  5443.           lc = 0;
  5444.     }
  5445.     }
  5446. }
  5447.  
  5448. int
  5449. savhistory(s,disp) char *s; int disp; {
  5450.     FILE * fp;
  5451.     int i;
  5452.  
  5453.     fp = fopen(s, disp ? "a" : "w");
  5454.     if (!fp) {
  5455.     perror(s);
  5456.     return(0);
  5457.     }
  5458.     for (i = 0; i <= current; i++)
  5459.       fprintf(fp,"%s\n", recall[i]);
  5460.     fclose(fp);
  5461.     return(1);
  5462. }
  5463. #endif /* CK_RECALL */
  5464.  
  5465. #ifdef COMMENT
  5466. /* apparently not used */
  5467. int
  5468. cmgetlc(s) char * s; {            /* Get leading char */
  5469.     char c;
  5470.     while ((c = *s++) <= SP) {
  5471.     if (!c)
  5472.       break;
  5473.     }
  5474.     return(c);
  5475. }
  5476. #endif /* COMMENT */
  5477.  
  5478.  
  5479. /*  C M C F M  --  Parse command confirmation (end of line)  */
  5480.  
  5481. /*
  5482.  Returns
  5483.    -2: User typed anything but whitespace or newline
  5484.    -1: Reparse needed
  5485.     0: Confirmation was received
  5486. */
  5487. int
  5488. cmcfm() {
  5489.     int x, xc;
  5490.     debug(F101,"cmcfm: cmflgs","",cmflgs);
  5491.     debug(F110,"cmcfm: atmbuf",atmbuf,0);
  5492.     inword = xc = cc = 0;
  5493.  
  5494.     setatm("",0);            /* (Probably unnecessary) */
  5495.  
  5496.     while (cmflgs != 1) {
  5497.         x = gtword(0);
  5498.         xc += cc;
  5499.  
  5500.         switch (x) {
  5501.       case -9:
  5502.         printf("Command or field too long\n");
  5503.       case -4:            /* EOF */
  5504.       case -2:
  5505.       case -1:
  5506.         return(x);
  5507.       case 1:            /* End of line */
  5508.         if (xc > 0) {
  5509.         if (xcmfdb) {
  5510.             return(-6);
  5511.         } else {
  5512.             printf("?Not confirmed - %s\n",atmbuf);
  5513.             return(-9);
  5514.         }
  5515.         } else
  5516.           break;            /* Finish up below */
  5517.       case 2:            /* ESC */
  5518.         if (xc == 0) {
  5519.         bleep(BP_WARN);
  5520.         continue;        /* or fall thru. */
  5521.         }
  5522.       case 0:            /* Space */
  5523.         if (xc == 0)        /* If no chars typed, continue, */
  5524.           continue;            /* else fall thru. */
  5525.         /* else fall thru... */
  5526.  
  5527.       case 3:            /* Question mark */
  5528.         if (xc > 0) {
  5529.         if (xcmfdb) {
  5530.             return(-6);
  5531.         } else {
  5532.             printf("?Not confirmed - %s\n",atmbuf);
  5533.             return(-9);
  5534.         }
  5535.         }
  5536.         printf(
  5537.            "\n Press the Return or Enter key to confirm the command\n");
  5538.         printf("%s%s",cmprom,cmdbuf);
  5539.         fflush(stdout);
  5540.         continue;
  5541.     }
  5542.     }
  5543.     debok = 1;
  5544.     return(0);
  5545. }
  5546.  
  5547.  
  5548. /* The following material supports chained parsing functions. */
  5549. /* See ckucmd.h for FDB and OFDB definitions. */
  5550.  
  5551. struct OFDB cmresult = {        /* Universal cmfdb result holder */
  5552.     NULL,
  5553.     0,
  5554.     NULL,
  5555.     0
  5556. };
  5557.  
  5558. VOID
  5559. cmfdbi(p,fc,s1,s2,s3,n1,n2,f,k,nxt)    /* Initialize an FDB */
  5560.     struct FDB * p;
  5561.     int fc;
  5562.     char * s1, * s2, * s3;
  5563.     int n1, n2;
  5564.     xx_strp f;
  5565.     struct keytab * k;
  5566.     struct FDB * nxt; {
  5567.  
  5568.     p->fcode = fc;
  5569.     p->hlpmsg = s1;
  5570.     p->dflt = s2;
  5571.     p->sdata = s3;
  5572.     p->ndata1 = n1;
  5573.     p->ndata2 = n2;
  5574.     p->spf = f;
  5575.     p->kwdtbl = k;
  5576.     p->nxtfdb = nxt;
  5577. }
  5578.  
  5579. /*  C M F D B  --  Parse a field with several possible functions  */
  5580.  
  5581. int
  5582. cmfdb(fdbin) struct FDB * fdbin; {
  5583. #ifndef NOSPL
  5584.     extern int x_ifnum;                 /* IF NUMERIC - disables warnings */
  5585. #endif /* NOSPL */
  5586.     struct FDB * in = fdbin;
  5587.     struct OFDB * out = &cmresult;
  5588.     int x = 0, n, r;
  5589.     char *s, *xp, *m = NULL;
  5590.     int errbits = 0;
  5591.  
  5592.     xp = bp;
  5593.  
  5594.     out->fcode = -1;            /* Initialize output struct */
  5595.     out->fdbaddr = NULL;
  5596.     out->sresult = NULL;
  5597.     out->nresult = 0;
  5598. /*
  5599.   Currently we make one trip through the FDBs.  So if the user types Esc or
  5600.   Tab at the beginning of a field, only the first FDB is examined for a
  5601.   default.  If the user types ?, help is given only for one FDB.  We should
  5602.   search through the FDBs for all matching possibilities -- and in particular
  5603.   display the pertinent context-sensitive help for each function, rather than
  5604.   the only the first one that works, and then rewind the FDB pointer so we
  5605.   are not locked out of the earlier ones.
  5606. */
  5607.     cmfldflgs = 0;
  5608.     while (1) {                /* Loop through the chain of FDBs */
  5609.     nomsg = 1;
  5610.     xcmfdb = 1;
  5611.     s = NULL;
  5612.     n = 0;
  5613.     debug(F101,"cmfdb in->fcode","",in->fcode);
  5614.     switch (in->fcode) {        /* Current parsing function code */
  5615.       case _CMNUM:
  5616.         r = in->ndata1;
  5617.         if (r != 10 && r != 8) r = 10;
  5618. #ifndef NOSPL
  5619.             x_ifnum = 1;                /* Disables warning messages */
  5620. #endif /* NOSPL */
  5621.         x = cmnum(in->hlpmsg,in->dflt,r,&n,in->spf);
  5622. #ifndef NOSPL
  5623.             x_ifnum = 0;
  5624. #endif /* NOSPL */
  5625.         debug(F101,"cmfdb cmnum","",x);
  5626.         if (x < 0) errbits |= 1;
  5627.         break;
  5628.       case _CMOFI:
  5629.         x = cmofi(in->hlpmsg,in->dflt,&s,in->spf);
  5630.         debug(F101,"cmfdb cmofi","",x);
  5631.         if (x < 0) errbits |= 2;
  5632.         break;
  5633.       case _CMIFI:
  5634.         x = cmifi2(in->hlpmsg,
  5635.                in->dflt,
  5636.                &s,
  5637.                &n,
  5638.                in->ndata1,
  5639.                in->sdata,
  5640.                in->spf,
  5641.                in->ndata2
  5642.                );
  5643.         debug(F101,"cmfdb cmifi2 x","",x);
  5644.         debug(F101,"cmfdb cmifi2 n","",n);
  5645.         if (x < 0) errbits |= 4;
  5646.         break;
  5647.       case _CMFLD:
  5648.         cmfldflgs = in->ndata1;
  5649.         x = cmfld(in->hlpmsg,in->dflt,&s,in->spf);
  5650.         debug(F101,"cmfdb cmfld","",x);
  5651.         if (x < 0) errbits |= 8;
  5652.         break;
  5653.       case _CMTXT:
  5654.         x = cmtxt(in->hlpmsg,in->dflt,&s,in->spf);
  5655.         debug(F101,"cmfdb cmtxt","",x);
  5656.         if (x < 0) errbits |= 16;
  5657.         break;
  5658.       case _CMKEY:
  5659.         x = cmkey2(in->kwdtbl,
  5660.                in->ndata1,
  5661.                in->hlpmsg,in->dflt,in->sdata,in->spf,in->ndata2);
  5662.         debug(F101,"cmfdb cmkey","",x);
  5663.         if (x < 0) errbits |= ((in->ndata2 & 4) ? 32 : 64);
  5664.         break;
  5665.       case _CMCFM:
  5666.         x = cmcfm();
  5667.         debug(F101,"cmfdb cmcfm","",x);
  5668.         if (x < 0) errbits |= 128;
  5669.         break;
  5670.       default:
  5671.         debug(F101,"cmfdb - unexpected function code","",in->fcode);
  5672.         printf("?cmfdb - unexpected function code: %d\n",in->fcode);
  5673.     }
  5674.     debug(F101,"cmfdb x","",x);
  5675.     debug(F101,"cmfdb cmflgs","",cmflgs);
  5676.     debug(F101,"cmfdb crflag","",crflag);
  5677.     debug(F101,"cmfdb qmflag","",qmflag);
  5678.     debug(F101,"cmfdb esflag","",esflag);
  5679.  
  5680.     if (x > -1) {            /* Success */
  5681.         out->fcode = in->fcode;    /* Fill in output struct */
  5682.         out->fdbaddr = in;
  5683.         out->sresult = s;
  5684.         out->nresult = (in->fcode == _CMKEY) ? x : n;
  5685.         out->kflags = (in->fcode == _CMKEY) ? cmkwflgs : 0;
  5686.         debug(F111,"cmfdb out->nresult",out->sresult,out->nresult);
  5687.         nomsg = 0;
  5688.         xcmfdb = 0;
  5689.         /* debug(F111,"cmfdb cmdbuf & crflag",cmdbuf,crflag); */
  5690.         if (crflag) {
  5691.         cmflgs = 1;
  5692.         }
  5693.         return(x);            /* and return */
  5694.     }
  5695.     in = in->nxtfdb;        /* Failed, get next parsing function */
  5696.     nomsg = 0;
  5697.     xcmfdb = 0;
  5698.     if (!in) {            /* No more */
  5699.         debug(F101,"cmfdb failure x","",x);
  5700.         debug(F101,"cmfdb failure errbits","",errbits);
  5701.         if (x == -6)
  5702.           x = -9;
  5703.         if (x == -9) {
  5704. #ifdef CKROOT
  5705.         if (ckrooterr)
  5706.           m = "Off Limits";
  5707.         else
  5708. #endif /* CKROOT */
  5709.         /* Make informative messages for a few common cases */
  5710.         switch (errbits) {
  5711.           case 4+32: m = "Does not match filename or switch"; break;
  5712.           case 4+64: m = "Does not match filename or keyword"; break;
  5713.           case 1+32: m = "Not a number or valid keyword"; break;
  5714.           case 1+64: m = "Not a number or valid switch"; break;
  5715.           default: m = "Not valid in this position";
  5716.         }
  5717.         printf("?%s: \"%s\"\n",m, atmbuf);
  5718.         }
  5719.         return(x);
  5720.     }
  5721.     if (x != -2 && x != -6 && x != -9 && x != -3) /* Editing or somesuch */
  5722.       return(x);            /* Go back and reparse */
  5723.     pp = np = bp = xp;        /* Back up pointers */
  5724.     cmflgs = -1;            /* Force a reparse */
  5725.  
  5726.  
  5727. #ifndef NOSPL
  5728.     if (!askflag) {            /* If not executing ASK-class cmd... */
  5729. #endif /* NOSPL */
  5730.         if (crflag) {        /* If CR was typed, put it back */
  5731.         pushc = LF;        /* But as a linefeed */
  5732.         } else if (qmflag) {    /* Ditto for Question mark */
  5733.         pushc = '?';
  5734.         } else if (esflag) {    /* and Escape or Tab */
  5735.         pushc = ESC;
  5736.         }
  5737. #ifndef NOSPL
  5738.     }
  5739. #endif /* NOSPL */
  5740.     }
  5741. }
  5742.  
  5743.  
  5744. /*  G T W O R D  --  Gets a "word" from the command input stream  */
  5745.  
  5746. /*
  5747. Usage: retcode = gtword(brk);
  5748.   brk = 0 for normal word breaks (space, CR, Esc, ?)
  5749.   brk = 1 to add ':' and '=' (for parsing switches).  These characters
  5750.         act as break characters only if the first character of the field
  5751.         is slash ('/'), i.e. switch introducer.
  5752.  
  5753. Returns:
  5754. -10 Timelimit set and timed out
  5755.  -9 if input was too long
  5756.  -4 if end of file (e.g. pipe broken)
  5757.  -3 if null field
  5758.  -2 if command buffer overflows
  5759.  -1 if user did some deleting
  5760.   0 if word terminates with SP or tab
  5761.   1 if ... CR
  5762.   2 if ... ESC
  5763.   3 if ... ? (question mark)
  5764.   4 if ... : or = and called with brk != 0
  5765.  
  5766. With:
  5767.   pp pointing to beginning of word in buffer
  5768.   bp pointing to after current position
  5769.   atmbuf containing a copy of the word
  5770.   cc containing the number of characters in the word copied to atmbuf
  5771. */
  5772.  
  5773. int
  5774. ungword() {                /* Unget a word */
  5775.     debug(F101,"ungword cmflgs","",cmflgs);
  5776.     if (ungw) return(0);
  5777.     cmfsav = cmflgs;
  5778.     ungw = 1;
  5779.     cmflgs = 0;
  5780.     return(0);
  5781. }
  5782.  
  5783. /* Un-un-get word.  Undo ungword() if it has been done. */
  5784.  
  5785. VOID
  5786. unungw() {
  5787.     debug(F010,"unungw atmbuf",atmbuf,0);
  5788.     if (ungw) {
  5789.     ungw = 0;
  5790.     cmflgs = cmfsav;
  5791.     atmbuf[0] = NUL;
  5792.     }
  5793. }
  5794.  
  5795. static int
  5796. gtword(brk) int brk; {
  5797.     int c;                              /* Current char */
  5798.     int quote = 0;                      /* Flag for quote character */
  5799.     int echof = 0;                      /* Flag for whether to echo */
  5800.     int comment = 0;            /* Flag for in comment */
  5801.     char *cp = NULL;            /* Comment pointer */
  5802.     int eintr = 0;            /* Flag for syscall interrupted */
  5803.     int bracelvl = 0;            /* nested brace counter [jrs] */
  5804.     int iscontd = 0;            /* Flag for continuation */
  5805.     int realtty = 0;            /* Stdin is really a tty */
  5806.     char firstnb  = NUL;
  5807.     char lastchar = NUL;
  5808.     char prevchar = NUL;
  5809.     char lbrace, rbrace;
  5810.     int dq = 0;                /* Doublequote flag */
  5811.     int dqn = 0;            /* and count */
  5812.  
  5813. #ifdef RTU
  5814.     extern int rtu_bug;
  5815. #endif /* RTU */
  5816.  
  5817. #ifdef IKSD
  5818.     extern int inserver;
  5819. #endif /* IKSD */
  5820.     extern int kstartactive;
  5821.  
  5822. #ifdef datageneral
  5823.     extern int termtype;                /* DG terminal type flag */
  5824.     extern int con_reads_mt;            /* Console read asynch is active */
  5825.     if (con_reads_mt) connoi_mt();      /* Task would interfere w/cons read */
  5826. #endif /* datageneral */
  5827.  
  5828.  
  5829. #ifdef DEBUG
  5830.     if (deblog) {
  5831.     debug(F101,"gtword brk","",brk);
  5832.     debug(F101,"gtword cmfldflgs","",cmfldflgs);
  5833.     debug(F101,"gtword swarg","",swarg);
  5834.     debug(F101,"gtword dpx","",dpx);
  5835.     debug(F101,"gtword echof","",echof);
  5836. #ifndef NOSPL
  5837.     debug(F101,"gtword askflag","",askflag);
  5838.     debug(F101,"gtword timelimit","",timelimit);
  5839. #ifndef NOLOCAL
  5840. #ifndef NOXFER
  5841. #ifdef CK_AUTODL
  5842.     debug(F101,"gtword cmdadl","",cmdadl);
  5843. #endif /* CK_AUTODL */
  5844. #endif /* NOXFER */
  5845. #endif /* NOLOCAL */
  5846. #endif /* NOSPL */
  5847.     }
  5848. #endif /* DEBUG */
  5849.  
  5850.     realtty = is_a_tty(0);        /* Stdin is really a tty? */
  5851.  
  5852.     if (cmfldflgs & 1) {
  5853.     lbrace = '(';
  5854.     rbrace = ')';
  5855.     } else {
  5856.     lbrace = '{';
  5857.     rbrace = '}';
  5858.     }
  5859.     crflag = 0;
  5860.     qmflag = 0;
  5861.     esflag = 0;
  5862.  
  5863.     if (swarg) {            /* No leading space for switch args */
  5864.     inword = 1;
  5865.     swarg = 0;
  5866.     }
  5867.     if (ungw) {                /* Have a word saved? */
  5868. #ifdef M_UNGW
  5869.     /* Experimental code to allow ungetting multiple words. */
  5870.     /* See comments in ckmkey2() above. */
  5871.     int x;
  5872.     if (np > pp) pp = np;
  5873.     while (*pp == SP) pp++;
  5874.     if (!*pp) {
  5875.         ungw = 0;
  5876.         cmflgs = cmfsav;
  5877.     } else {
  5878.         if ((x = setatm(pp,2)) < 0) {
  5879.         printf("?Saved word too long\n");
  5880.         return(-9);
  5881.         }
  5882.         if (pp[x] >= SP) {
  5883.         char *p2;
  5884.         p2 = pp;
  5885.         p2 += x;
  5886.         while (*p2 == SP) p2++;
  5887.         if (*p2) {
  5888.             np = p2;
  5889.             ungword();
  5890.         }
  5891.         } else {
  5892.         ungw = 0;
  5893.         cmflgs = cmfsav;
  5894.         debug(F010,"gtword ungw return atmbuf",atmbuf,0);
  5895.         }
  5896.     }
  5897.     return(cmflgs);
  5898. #else
  5899.     /*
  5900.        You would think the following should be:
  5901.              while (*pp == SP) pp++;
  5902.            but you would be wrong -- making this change breaks GOTO.
  5903.         */
  5904.     while (*pp++ == SP) ;
  5905.     if (setatm(pp,2) < 0) {
  5906.         printf("?Saved word too long\n");
  5907.         return(-9);
  5908.     }
  5909.     ungw = 0;
  5910.     cmflgs = cmfsav;
  5911.     debug(F010,"gtword ungw return atmbuf",atmbuf,0);
  5912.     return(cmflgs);
  5913. #endif /* M_UNGW */
  5914.     }
  5915.     pp = np;                            /* Start of current field */
  5916.  
  5917. #ifdef COMMENT
  5918. #ifdef DEBUG
  5919.     if (deblog) {
  5920.     debug(F110,"gtword cmdbuf",cmdbuf,0);
  5921.     debug(F110,"gtword bp",bp,0);
  5922.     debug(F110,"gtword pp",pp,0);
  5923.     }
  5924. #endif /* DEBUG */
  5925. #endif /* COMMENT */
  5926.     {
  5927.     /* If we are reparsing we have to recount any braces or doublequotes */
  5928.     char * p = pp;
  5929.     char c;
  5930.     if (*p == '"')
  5931.       dq++;
  5932.     while ((c = *p++))
  5933.       if (c == lbrace)
  5934.         bracelvl++;
  5935.       else if (c == rbrace)
  5936.         bracelvl--;
  5937.       else if (dq && c == '"')
  5938.         dqn++;
  5939.     }
  5940.     while (bp < cmdbuf+CMDBL) {         /* Big get-a-character loop */
  5941.     echof = 0;            /* Assume we don't echo because */
  5942.     chsrc = 0;            /* character came from reparse buf. */
  5943. #ifdef BS_DIRSEP
  5944. CMDIRPARSE:
  5945. #endif /* BS_DIRSEP */
  5946.  
  5947.     c = *bp;
  5948.         if (!c) {            /* If no char waiting in reparse buf */
  5949.         if (dpx && (!pushc
  5950. #ifndef NOSPL
  5951.             || askflag
  5952. #endif /* NOSPL */
  5953.             ))        /* Get from tty, set echo flag */
  5954.           echof = 1;
  5955.         c = cmdgetc(timelimit);    /* Read a command character. */
  5956. #ifdef DEBUG
  5957.         debug(F101,"gtword c","",c);
  5958. #endif /* DEBUG */
  5959.  
  5960.         if (timelimit && c < -1) {    /* Timed out */
  5961.         return(-10);
  5962.         }
  5963.  
  5964. #ifndef NOXFER
  5965. /*
  5966.   The following allows packet recognition in the command parser.
  5967.   Presently it works only for Kermit packets, and if our current protocol
  5968.   happens to be anything besides Kermit, we simply force it to Kermit.
  5969.   We don't use the APC mechanism here for mechanical reasons, and also
  5970.   because this way, it works even with minimally configured interactive
  5971.   versions.  Add Zmodem later...
  5972. */
  5973. #ifdef CK_AUTODL
  5974.         if ((!local && cmdadl)    /* Autodownload enabled? */
  5975. #ifdef IKS_OPTION
  5976.         || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  5977. #endif /* IKS_OPTION */
  5978.         ) {
  5979.         int k;
  5980.         k = kstart((CHAR)c);    /* Kermit S or I packet? */
  5981.         if (k) {
  5982.             int ksign = 0;
  5983.             if (k < 0) {    /* Minus-Protocol? */
  5984. #ifdef NOSERVER
  5985.             goto noserver;    /* Need server mode for this */
  5986. #else
  5987.             ksign = 1;    /* Remember */
  5988.             k = 0 - k;    /* Convert to actual protocol */
  5989.             justone = 1;    /* Flag for protocol module */
  5990. #endif /* NOSERVER */
  5991.             } else
  5992.               justone = 0;
  5993.             k--;        /* Adjust kstart's return value */
  5994.             if (k == PROTO_K) {
  5995.             extern int protocol, g_proto;
  5996.             extern CHAR sstate;
  5997.             g_proto = protocol;
  5998.             protocol = PROTO_K; /* Crude... */
  5999.             sstate = ksign ? 'x' : 'v';
  6000.             cmdbuf[0] = NUL;
  6001.             return(-3);
  6002.             }
  6003.         }
  6004.         }
  6005. #ifdef NOSERVER
  6006.       noserver:
  6007. #endif /* NOSERVER */
  6008. #endif /* CK_AUTODL */
  6009. #endif /* NOXFER */
  6010.  
  6011.         chsrc = 1;            /* Remember character source is tty. */
  6012.         brkchar = c;
  6013.  
  6014. #ifdef IKSD
  6015.             if (inserver && c < 0) {    /* End of session? */
  6016.                 debug(F111,"gtword c < 0","exiting",c);
  6017.                 return(-4);             /* Cleanup and terminate */
  6018.             }
  6019. #endif /* IKSD */
  6020.  
  6021. #ifdef OS2
  6022.            if (c < 0) {            /* Error */
  6023.            if (c == -3) {        /* Empty word? */
  6024.            if (blocklvl > 0)    /* In a block */
  6025.              continue;        /* so keep looking for block end */
  6026.            else
  6027.              return(-3);    /* Otherwise say we got nothing */
  6028.            } else {            /* Not empty word */
  6029.            return(-4);        /* So some kind of i/o error */
  6030.            }
  6031.            }
  6032. #else
  6033. #ifdef MAC
  6034.        if (c == -3)            /* Empty word... */
  6035.          if (blocklvl > 0)
  6036.            continue;
  6037.          else
  6038.            return(-3);
  6039. #endif /* MAC */
  6040. #endif /* OS2 */
  6041.        if (c == EOF) {        /* This can happen if stdin not tty. */
  6042. #ifdef EINTR
  6043. /*
  6044.   Some operating and/or C runtime systems return EINTR for no good reason,
  6045.   when the end of the standard input "file" is encountered.  In cases like
  6046.   this, we get into an infinite loop; hence the eintr counter, which is reset
  6047.   to 0 upon each call to this routine.
  6048. */
  6049.         debug(F101,"gtword EOF","",errno);
  6050.         if (errno == EINTR && ++eintr < 4) /* When bg'd process is */
  6051.           continue;        /* fg'd again. */
  6052. #endif /* EINTR */
  6053.         return(-4);
  6054.         }
  6055.         c &= cmdmsk;        /* Strip any parity bit */
  6056.     }                /* if desired. */
  6057.  
  6058. /* Now we have the next character */
  6059.  
  6060.     if (!firstnb && c > SP) {    /* First nonblank */
  6061.         firstnb = c;
  6062.         if (c == '"')        /* Starts with doublequote */
  6063.           dq = 1;
  6064.     }
  6065.     if (c == '"')            /* Count doublequotes */
  6066.       dqn++;
  6067.  
  6068.     if (quote && (c == CR || c == LF)) { /* Enter key following quote */
  6069.         *bp++ = CMDQ;        /* Double it */
  6070.         *bp = NUL;
  6071.         quote = 0;
  6072.     }
  6073.         if (quote == 0) {        /* If this is not a quoted character */
  6074.         switch (c) {
  6075.           case CMDQ:        /* Got the quote character itself */
  6076.         if (!comment && quoting)
  6077.           quote = 1;        /* Flag it if not in a comment */
  6078.         break;
  6079.           case FF:            /* Formfeed. */
  6080.                 c = NL;                 /* Replace with newline */
  6081.         cmdclrscn();        /* Clear the screen */
  6082.         break;
  6083.           case HT:            /* Horizontal Tab */
  6084.         if (comment)        /* If in comment, */
  6085.           c = SP;        /* substitute space */
  6086.         else            /* otherwise */
  6087.           c = ESC;        /* substitute ESC (for completion) */
  6088.         break;
  6089.           case ';':            /* Trailing comment */
  6090.           case '#':
  6091.         if (inword == 0 && quoting) { /* If not in a word */
  6092.             comment = 1;    /* start a comment. */
  6093.             cp = bp;        /* remember where it starts. */
  6094.         }
  6095.         break;
  6096.         }
  6097.         if (!kstartactive &&    /* Not in possible Kermit packet */
  6098.         !comment && c == SP) {    /* Space not in comment */
  6099.                 *bp++ = (char) c;    /* deposit in buffer if not already */
  6100.         /* debug(F101,"gtword echof 2","",echof); */
  6101. #ifdef BEBOX
  6102.                 if (echof) {
  6103.                     putchar(c);        /* echo it. */
  6104.                     fflush(stdout);
  6105.                     fflush(stderr);
  6106.                 }
  6107. #else
  6108.                 if (echof) {        /* echo it. */
  6109.             putchar((CHAR)c);
  6110.             if (timelimit)
  6111.               fflush(stdout);
  6112.         }
  6113. #endif /* BEBOX */
  6114.                 if (inword == 0) {      /* If leading, gobble it. */
  6115.                     pp++;
  6116.                     continue;
  6117.                 } else {                /* If terminating, return. */
  6118.             if ((!dq && ((*pp != lbrace) || (bracelvl == 0))) ||
  6119.             (dq && dqn > 1 && *(bp-2) == '"')) {
  6120.             np = bp;
  6121.             cmbptr = np;
  6122.             if (setatm(pp,0) < 0) {
  6123.                 printf("?Field too long error 1\n");
  6124.                 debug(F111,"gtword too long #1",pp,strlen(pp));
  6125.                 return(-9);
  6126.             }
  6127.             brkchar = c;
  6128.             inword = cmflgs = 0;
  6129.             return(0);
  6130.             }
  6131.                     continue;
  6132.                 }
  6133.             }
  6134.             if (c == lbrace) {
  6135.         bracelvl++;
  6136.         /* debug(F101,"gtword bracelvl++","",bracelvl); */
  6137.         }
  6138.             if (c == rbrace && bracelvl > 0) {
  6139.                 bracelvl--;
  6140.         /* debug(F101,"gtword bracelvl--","",bracelvl); */
  6141.                 if (linebegin)
  6142.           blocklvl--;
  6143.             }
  6144.         if ((c == '=' || c == ':') &&
  6145.         !kstartactive && !comment && brk && (firstnb == '/')
  6146.         ) {
  6147.                 *bp++ = (char) c;    /* Switch argument separator */
  6148.         /* debug(F111,"gtword switch argsep",cmdbuf,brk); */
  6149. #ifdef BEBOX
  6150.                 if (echof) {
  6151.                     putchar(c);        /* Echo it. */
  6152.                     fflush(stdout);
  6153.                     fflush(stderr);
  6154.                 }
  6155. #else
  6156.         if (echof) {
  6157.             putchar((CHAR)c);
  6158.             if (timelimit)
  6159.               fflush(stdout);
  6160.         }
  6161. #endif /* BEBOX */
  6162.         if ((*pp != lbrace) || (bracelvl == 0)) {
  6163.             np = bp;
  6164.             cmbptr = np;
  6165.             if (setatm(pp,0) < 0) {
  6166.             printf("?Field too long error 1\n");
  6167.             debug(F111,"gtword too long #1",pp,strlen(pp));
  6168.             return(-9);
  6169.             }
  6170.             inword = cmflgs = 0;
  6171.             brkchar = c;
  6172.             return(4);
  6173.         }
  6174.             }
  6175.             if (c == LF || c == CR) {    /* CR or LF. */
  6176.         if (echof) {
  6177.                     cmdnewl((char)c);    /* echo it. */
  6178. #ifdef BEBOX
  6179.                     fflush(stdout);
  6180.                     fflush(stderr);
  6181. #endif /* BEBOX */
  6182.                 }
  6183.         {
  6184.             /* Trim trailing comment and whitespace */
  6185.             char *qq;
  6186.             if (comment) {    /* Erase comment */
  6187.             while (bp >= cp) /* Back to comment pointer */
  6188.               *bp-- = NUL;
  6189.             bp++;
  6190.             pp = bp;    /* Adjust other pointers */
  6191.             inword = 0;    /* and flags */
  6192.             comment = 0;
  6193.             cp = NULL;
  6194.             }
  6195.             qq = inword ? pp : (char *)cmdbuf;
  6196.             /* Erase trailing whitespace */
  6197.             while (bp > qq && (*(bp-1) == SP || *(bp-1) == HT)) {
  6198.             bp--;
  6199.             /* debug(F000,"erasing","",*bp); */
  6200.             *bp = NUL;
  6201.             }
  6202.             lastchar = (bp > qq) ? *(bp-1) : NUL;
  6203.             prevchar = (bp > qq+1) ? *(bp-2) : NUL;
  6204.         }
  6205.         if (linebegin && blocklvl > 0) /* Blank line in {...} block */
  6206.           continue;
  6207.  
  6208.         linebegin = 1;        /* At beginning of next line */
  6209.         iscontd = prevchar != CMDQ &&
  6210.           (lastchar == '-' || lastchar == lbrace);
  6211.         debug(F101,"gtword iscontd","",iscontd);
  6212.  
  6213.                 if (iscontd) {        /* If line is continued... */
  6214.                     if (chsrc) {    /* If reading from tty, */
  6215.                         if (*(bp-1) == lbrace) { /* Check for "begin block" */
  6216.                             *bp++ = SP;    /* Insert a space for neatness */
  6217.                             blocklvl++;    /* Count block nesting level */
  6218.                         } else {    /* Or hyphen */
  6219.                 bp--;    /* Overwrite the hyphen */
  6220.                         }
  6221.                         *bp = NUL;    /* erase the dash, */
  6222.                         continue;    /* and go back for next char now. */
  6223.                     }
  6224.         } else if (blocklvl > 0) { /* No continuation character */
  6225.             if (chsrc) {    /* But we're in a "block" */
  6226.             *bp++ = ',';    /* Add comma */
  6227.             *bp = NUL;
  6228.             continue;
  6229.             }
  6230.         } else {        /* No continuation, end of command. */
  6231.             *bp = NUL;        /* Terminate the command string. */
  6232.             if (comment) {    /* If we're in a comment, */
  6233.             comment = 0;    /* Say we're not any more, */
  6234.             *cp = NUL;    /* cut it off. */
  6235.             }
  6236.             np = bp;        /* Where to start next field. */
  6237.             cmbptr = np;
  6238.             if (setatm(pp,0) < 0) { /* Copy field to atom buffer */
  6239.             debug(F111,"gtword too long #2",pp,strlen(pp));
  6240.             printf("?Field too long error 2\n");
  6241.             return(-9);
  6242.             }
  6243.             inword = 0;        /* Not in a word any more. */
  6244.             crflag = 1;
  6245.                     /* debug(F110,"gtword","crflag is set",0); */
  6246. #ifdef CK_RECALL
  6247.             current = rlast;
  6248. #endif /* CK_RECALL */
  6249.             cmflgs = 1;
  6250.             if (!xcmdsrc || force_add)
  6251.               addcmd(cmdbuf);
  6252.             return(cmflgs);
  6253.         }
  6254.             }
  6255. /*
  6256.   This section handles interactive help, completion, editing, and history.
  6257.   Rearranged as a switch statement executed only if we're at top level since
  6258.   there is no need for any of this within command files and macros: Aug 2000.
  6259.   Jun 2001: Even if at top level, skip this if the character was fetched from
  6260.   the reparse or recall buffer, or if stdin is redirected.
  6261. */
  6262.         if ((xcmdsrc == 0        /* Only at top level */
  6263. #ifndef NOSPL
  6264.         || askflag        /* or user is typing ASK response */
  6265. #endif /* NOSPL */
  6266.          ) && chsrc != 0 && realtty) { /* from the real keyboard */
  6267.  
  6268.         switch (c) {
  6269.           case '?':        /* ?-Help */
  6270. #ifndef NOSPL
  6271.             if (askflag)    /* No help in ASK response */
  6272.               break;
  6273. #endif /* NOSPL */
  6274.             if (quoting
  6275.             && !kstartactive
  6276.             && !comment
  6277.             ) {
  6278.             putchar((CHAR)c);
  6279.             *bp = NUL;
  6280.             if (setatm(pp,0) < 0) {
  6281.                 debug(F111,"gtword too long ?",pp,strlen(pp));
  6282.                 printf("?Too long\n");
  6283.                 return(-9);
  6284.             }
  6285.             qmflag = 1;
  6286.             return(cmflgs = 3);
  6287.             }
  6288.  
  6289.           case ESC:        /* Esc or Tab completion */
  6290.             if (!comment) {
  6291.             *bp = NUL;
  6292.             if (setatm(pp,0) < 0) {
  6293.                 debug(F111,"gtword too long Esc",pp,strlen(pp));
  6294.                 printf("?Too long\n");
  6295.                 return(-9);
  6296.             }
  6297.             esflag = 1;
  6298.             return(cmflgs = 2);
  6299.             } else {
  6300.             bleep(BP_WARN);
  6301.             continue;
  6302.             }
  6303.  
  6304.           case BS:        /* Character deletion */
  6305.           case RUB:
  6306.             if (bp > cmdbuf) {    /* If still in buffer... */
  6307.             cmdchardel();    /* erase it. */
  6308.             bp--;        /* point behind it, */
  6309.             if (*bp == lbrace) bracelvl--; /* Adjust brace count */
  6310.             if (*bp == rbrace) bracelvl++;
  6311.             if ((*bp == SP) && /* Flag if current field gone */
  6312.                 (*pp != lbrace || bracelvl == 0))
  6313.               inword = 0;
  6314.             *bp = NUL;    /* Erase character from buffer. */
  6315.             } else {        /* Otherwise, */
  6316.             bleep(BP_WARN);
  6317.             cmres();    /* and start parsing a new command. */
  6318.             *bp = *atmbuf = NUL;
  6319.             }
  6320.             if (pp < bp)
  6321.               continue;
  6322.             else
  6323.               return(cmflgs = -1);
  6324.  
  6325.           case LDEL:        /* ^U, line deletion */
  6326.             while ((bp--) > cmdbuf) {
  6327.             cmdchardel();
  6328.             *bp = NUL;
  6329.             }
  6330.             cmres();        /* Restart the command. */
  6331.             *bp = *atmbuf = NUL;
  6332.             inword = 0;
  6333.             return(cmflgs = -1);
  6334.  
  6335.           case WDEL:        /* ^W, word deletion */
  6336.             if (bp <= cmdbuf) {    /* Beep if nothing to delete */
  6337.             bleep(BP_WARN);
  6338.             cmres();
  6339.             *bp = *atmbuf = NUL;
  6340.             return(cmflgs = -1);
  6341.             }
  6342.             bp--;
  6343.             /* Back up over any trailing nonalphanums */
  6344.             /* This is dependent on ASCII collating sequence */
  6345.             /* but isalphanum() is not available everywhere. */
  6346.             for ( ;
  6347.              (bp >= cmdbuf) &&
  6348.              ((*bp < '0') ||
  6349.              ((*bp > '9') && (*bp < '@')) ||
  6350.              ((*bp > 'Z') && (*bp < 'a')) ||
  6351.              (*bp > 'z'));
  6352.              bp--
  6353.              ) {
  6354.             cmdchardel();
  6355.             *bp = NUL;
  6356.             }
  6357.             /* Now delete back to rightmost remaining nonalphanum */
  6358.             for ( ; (bp >= cmdbuf) && (*bp) ; bp--) {
  6359.             if ((*bp < '0') ||
  6360.                 (*bp > '9' && *bp < '@') ||
  6361.                 (*bp > 'Z' && *bp < 'a') ||
  6362.                 (*bp > 'z'))
  6363.               break;
  6364.             cmdchardel();
  6365.             *bp = NUL;
  6366.             }
  6367.             bp++;
  6368.             inword = 0;
  6369.             return(cmflgs = -1);
  6370.  
  6371.           case RDIS: {        /* ^R, redisplay */
  6372.               char *cpx; char cx;
  6373.               *bp = NUL;
  6374.               printf("\n%s",cmprom);
  6375.               cpx = cmdbuf;
  6376.               while ((cx = *cpx++)) {
  6377. #ifdef isprint
  6378.               putchar((CHAR) (isprint(cx) ? cx : '^'));
  6379. #else
  6380.               putchar((CHAR) ((cx >= SP && cx < DEL) ? cx : '^'));
  6381. #endif /* isprint */
  6382.               }
  6383.               fflush(stdout);
  6384.               continue;
  6385.           }
  6386.         }
  6387.  
  6388.  
  6389. #ifdef CK_RECALL
  6390.         if (on_recall &&    /* Reading commands from keyboard? */
  6391.             (cm_recall > 0) &&    /* Saving commands? */
  6392.             (c == C_UP || c == C_UP2)) { /* Go up one */
  6393.             if (last_recall == 2 && current > 0)
  6394.               current--;
  6395.             if (current < 0) {    /* Nowhere to go, */
  6396.             bleep(BP_WARN);
  6397.             continue;
  6398.             }
  6399.             if (recall[current]) { /* We have a previous command */
  6400.             while ((bp--) > cmdbuf) { /* Erase current line */
  6401.                 cmdchardel();
  6402.                 *bp = NUL;
  6403.             }
  6404.             ckstrncpy(cmdbuf,recall[current],CMDBL);
  6405. #ifdef OSK
  6406.             fflush(stdout);
  6407.             write(fileno(stdout), "\r", 1);
  6408.             printf("%s%s",cmprom,cmdbuf);
  6409. #else
  6410.             printf("\r%s%s",cmprom,cmdbuf);
  6411. #endif /* OSK */
  6412.             current--;
  6413.             }
  6414.             last_recall = 1;
  6415.             return(cmflgs = -1); /* Force a reparse */
  6416.         }
  6417.         if (on_recall &&    /* Reading commands from keyboard? */
  6418.             (cm_recall > 0) &&    /* Saving commands? */
  6419.             (c == C_DN)) {    /* Down one */
  6420.             int x = 1;
  6421.             if (last_recall == 1)
  6422.               x++;
  6423.             if (current + x > rlast) { /* Already at bottom, beep */
  6424.             bleep(BP_WARN);
  6425.             continue;
  6426.             }
  6427.             current += x;    /* OK to go down */
  6428.             if (recall[current]) {
  6429.             while ((bp--) > cmdbuf) { /* Erase current line */
  6430.                 cmdchardel();
  6431.                 *bp = NUL;
  6432.             }
  6433.             ckstrncpy(cmdbuf,recall[current],CMDBL);
  6434. #ifdef OSK
  6435.             fflush(stdout);
  6436.             write(fileno(stdout), "\r", 1);
  6437.             printf("%s%s",cmprom,cmdbuf);
  6438. #else
  6439.             printf("\r%s%s",cmprom,cmdbuf);
  6440. #endif /* OSK */
  6441.             last_recall = 2;
  6442.             return(cmflgs = -1); /* Force reparse */
  6443.             }
  6444.         }
  6445. #endif /* CK_RECALL */
  6446.         }
  6447.  
  6448.         if (c < SP && quote == 0) { /* Any other unquoted control char */
  6449.         if (!chsrc) {        /* If cmd file, point past it */
  6450.             bp++;
  6451.         } else {
  6452.             bleep(BP_WARN);
  6453.         }
  6454.         continue;        /* continue, don't put in buffer */
  6455.         }
  6456.         linebegin = 0;        /* Not at beginning of line */
  6457. #ifdef BEBOX
  6458.         if (echof) {
  6459.                 cmdecho((char) c, 0);    /* Echo what was typed. */
  6460.                 fflush (stdout);
  6461.                 fflush(stderr);
  6462.             }
  6463. #else
  6464.             if (echof) cmdecho((char) c, 0); /* Echo what was typed. */
  6465. #endif /* BEBOX */
  6466.         } else {            /* This character was quoted. */
  6467.         int qf = 1;
  6468.         quote = 0;            /* Unset the quote flag. */
  6469.         /* debug(F000,"gtword quote 0","",c); */
  6470.         /* Quote character at this level is only for SP, ?, and controls */
  6471.             /* If anything else was quoted, leave quote in, and let */
  6472.         /* the command-specific parsing routines handle it, e.g. \007 */
  6473.         if (c > 32 && c != '?' && c != RUB && chsrc != 0) {
  6474.         /* debug(F000,"gtword quote 1","",c); */
  6475.         *bp++ = CMDQ;        /* Deposit \ if it came from tty */
  6476.         qf = 0;            /* and don't erase it from screen */
  6477.         linebegin = 0;        /* Not at beginning of line */
  6478. #ifdef BS_DIRSEP
  6479. /*
  6480.   This is a hack to handle "cd \" or "cd foo\" on OS/2 and similar systems.
  6481.   If we were called from cmdir() and the previous character was the quote
  6482.   character, i.e. backslash, and this character is the command terminator,
  6483.   then we stuff an extra backslash into the buffer without echoing, then
  6484.   we stuff the carriage return back in again, and go back and process it,
  6485.   this time with the quote flag off.
  6486. */
  6487.         } else if (dirnamflg && (c == CR || c == LF || c == SP)) {
  6488.         /* debug(F000,"gtword quote 2","",c); */
  6489.         *bp++ = CMDQ;
  6490.         linebegin = 0;        /* Not at beginning of line */
  6491.         *bp = (c == SP ? SP : CR);
  6492.         goto CMDIRPARSE;
  6493. #endif /* BS_DIRSEP */
  6494.         }
  6495. #ifdef BEBOX
  6496.         if (echof) {
  6497.                 cmdecho((char) c, qf);    /* Echo what was typed. */
  6498.                 fflush (stdout);
  6499.                 fflush(stderr);
  6500.             }
  6501. #else
  6502.         if (echof) cmdecho((char) c, qf); /* Now echo quoted character */
  6503. #endif /* BEBOX */
  6504.         /* debug(F111,"gtword quote",cmdbuf,c); */
  6505.     }
  6506. #ifdef COMMENT
  6507.         if (echof) cmdecho((char) c,quote); /* Echo what was typed. */
  6508. #endif /* COMMENT */
  6509.         if (!comment) inword = 1;    /* Flag we're in a word. */
  6510.     if (quote) continue;        /* Don't deposit quote character. */
  6511.         if (c != NL) {            /* Deposit command character. */
  6512.         *bp++ = (char) c;        /* and make sure there is a NUL */
  6513. #ifdef COMMENT
  6514.         *bp = NUL;            /* after it */
  6515. #endif /* COMMENT */
  6516.     }
  6517.     }                                   /* End of big while */
  6518.     bleep(BP_WARN);
  6519.     printf("?Command too long, maximum length: %d.\n",CMDBL);
  6520.     cmflgs = -2;
  6521.     return(-9);
  6522. }
  6523.  
  6524. /* Utility functions */
  6525.  
  6526. /* A D D B U F  -- Add the string pointed to by cp to the command buffer  */
  6527.  
  6528. static int
  6529. addbuf(cp) char *cp; {
  6530.     int len = 0;
  6531.     while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) {
  6532.         *bp++ = *cp++;                  /* Copy and */
  6533.         len++;                          /* count the characters. */
  6534.     }
  6535.     *bp++ = SP;                         /* Put a space at the end */
  6536.     *bp = NUL;                          /* Terminate with a null */
  6537.     np = bp;                            /* Update the next-field pointer */
  6538.     cmbptr = np;
  6539.     return(len);                        /* Return the length */
  6540. }
  6541.  
  6542. /*  S E T A T M  --  Deposit a token in the atom buffer.  */
  6543. /*
  6544.   Break on space, newline, carriage return, or NUL.
  6545.   Call with:
  6546.     cp = Pointer to string to copy to atom buffer.
  6547.     fcode = 0 means break on whitespace or EOL.
  6548.     fcode = 1 means don't break on space.
  6549.     fcode = 2 means break on space, ':', or '='.
  6550.   Null-terminate the result.
  6551.   Return length of token, and also set global "cc" to this length.
  6552.   Return -1 if token was too long.
  6553. */
  6554. static int
  6555. setatm(cp,fcode) char *cp; int fcode; {
  6556.     char *ap, *xp, *dqp = NULL, lbrace, rbrace;
  6557.     int bracelvl = 0, dq = 0;
  6558.  
  6559.     register char * s;
  6560.     register int n = 0;
  6561.  
  6562.     if (cmfldflgs & 1) {        /* Handle grouping */
  6563.     lbrace = '(';
  6564.     rbrace = ')';
  6565.     } else {
  6566.     lbrace = '{';
  6567.     rbrace = '}';
  6568.     }
  6569.     cc = 0;                /* Character counter */
  6570.     ap = atmbuf;            /* Address of atom buffer */
  6571.  
  6572.     s = cp;
  6573.  
  6574.     while (*s++) n++;            /* Save a call to strlen */
  6575.  
  6576.     if (n > ATMBL) {
  6577.     printf("?Command buffer overflow\n");
  6578.     return(-1);
  6579.     }
  6580.     /* debug(F111,"setatm",cp,n); */
  6581.     if (cp == ap) {            /* In case source is atom buffer */
  6582.     xp = atybuf;            /* make a copy */
  6583. #ifdef COMMENT
  6584.     strncpy(xp,ap,ATMBL);        /* so we can copy it back, edited. */
  6585.     cp = xp;
  6586. #else
  6587.     s = ap;
  6588.     while ((*xp++ = *s++)) ;    /* We already know it's big enough */
  6589.     cp = xp = atybuf;
  6590. #endif /* COMMENT */
  6591.     }
  6592.     *ap = NUL;                /* Zero the atom buffer */
  6593.     if (fcode == 1) {            /* Trim trailing blanks */
  6594.     while (--n >= 0 && cp[n] == SP)
  6595.       ;
  6596.     cp[n+1] = NUL;
  6597.     }
  6598.     while (*cp == SP) {            /* Trim leading spaces */
  6599.     cp++;
  6600.     n--;
  6601.     }
  6602.     if (*cp == '"') {            /* Starts with doublequote? */
  6603.     dq = 1;
  6604.     dqp = cp;
  6605.     }
  6606.     while (*cp) {
  6607.         if (*cp == lbrace)
  6608.       bracelvl++;
  6609.         else if (*cp == rbrace)
  6610.       bracelvl--;
  6611.     if (bracelvl < 0)
  6612.       bracelvl = 0;
  6613.     if (bracelvl == 0) {
  6614.         if (dq) {
  6615.         if (*cp == SP || *cp == HT) {
  6616.             if (cp > dqp+1) {
  6617.             if (*(cp-1) == '"' && *(cp-2) != CMDQ) {
  6618.                 break;
  6619.             }
  6620.             }
  6621.         }
  6622.         } else if ((*cp == SP || *cp == HT) && (fcode != 1))
  6623.           break;
  6624.         if ((fcode == 2) && (*cp == '=' || *cp == ':')) break;
  6625.         if (*cp == LF || *cp == CR) break;
  6626.     }
  6627.         *ap++ = *cp++;
  6628.         cc++;
  6629.     }
  6630.     *ap = NUL;                /* Terminate the string. */
  6631.     /* debug(F111,"setatm result",atmbuf,cc); */
  6632.     return(cc);                         /* Return length. */
  6633. }
  6634.  
  6635. /*
  6636.   These functions attempt to hide system dependencies from the mainline
  6637.   code in gtword().  Dummy arg for cmdgetc() needed for compatibility with
  6638.   coninc(), ttinc(), etc, since a pointer to this routine can be passed in
  6639.   place of those to tn_doop().
  6640.  
  6641.   No longer static.  Used by askmore().  Fri Aug 20 15:03:34 1999.
  6642. */
  6643. int
  6644. cmdgetc(timelimit) int timelimit; {    /* Get a character from the tty. */
  6645.     int c;
  6646. #ifdef IKSD
  6647.     extern int inserver;
  6648. #endif /* IKSD */
  6649. #ifdef CK_LOGIN
  6650.     extern int x_logged;
  6651. #endif /* CK_LOGIN */
  6652. #ifdef TNCODE
  6653.     static int got_cr = 0;
  6654.     extern int ckxech;
  6655.     int tx = 0, is_tn = 0;
  6656. #endif /* TNCODE */
  6657.  
  6658.     if (pushc
  6659. #ifndef NOSPL
  6660.     && !askflag
  6661. #endif /* NOSPL */
  6662.     ) {
  6663.         debug(F111,"cmdgetc()","pushc",pushc);
  6664.     c = pushc;
  6665.     pushc = NUL;
  6666.     if (xcmfdb && c == '?')        /* Don't echo ? twice if chaining. */
  6667.       cmdchardel();
  6668.     return(c);
  6669.     }
  6670. #ifdef datageneral
  6671.     {
  6672.     char ch;
  6673.     c = dgncinb(0,&ch,1);        /* -1 is EOF, -2 TO,
  6674.                                          * -c is AOS/VS error */
  6675.     if (c == -2) {            /* timeout was enabled? */
  6676.         resto(channel(0));        /* reset timeouts */
  6677.         c = dgncinb(0,&ch,1);    /* retry this now! */
  6678.     }
  6679.     if (c < 0) return(-4);        /* EOF or some error */
  6680.     else c = (int) ch & 0177;    /* Get char without parity */
  6681. /*    echof = 1; */
  6682.     }
  6683. #else /* Not datageneral */
  6684. #ifndef MINIX2
  6685.     if (
  6686. #ifdef IKSD
  6687.     (!local && inserver) ||
  6688. #endif /* IKSD */
  6689.     timelimit > 0) {
  6690. #ifdef TNCODE
  6691.           GETNEXTCH:
  6692.             is_tn = !pushc && !local && sstelnet;
  6693. #endif /* TNCODE */
  6694. #ifdef COMMENT
  6695.         c = coninc(timelimit > 0 ? 1 : 0);
  6696. #else /* COMMENT */
  6697.         /* This is likely to break the asktimeout... */
  6698.         c = coninc(timelimit);
  6699. #endif /* COMMENT */
  6700.         /* debug(F101,"cmdgetc coninc","",c); */
  6701. #ifdef TNCODE
  6702.             if (c >= 0 && is_tn) {    /* Server-side Telnet */
  6703.                 switch (c) {
  6704.           case IAC:
  6705.                     /* debug(F111,"gtword IAC","c",c); */
  6706.                     got_cr = 0;
  6707.                     if ((tx = tn_doop((CHAR)(c & 0xff),ckxech,coninc)) == 0) {
  6708.                         goto GETNEXTCH;
  6709.                     } else if (tx <= -1) { /* I/O error */
  6710.                         /* If there was a fatal I/O error then ttclos()    */
  6711.                         /* has been called and the next GETNEXTCH attempt  */
  6712.                         /* will be !is_tn since ttclos() sets sstelnet = 0 */
  6713.                         doexit(BAD_EXIT,-1); /* (or return(-4)? */
  6714.                     } else if (tx == 1) { /* ECHO change */
  6715.                         ckxech = dpx = 1; /* Get next char */
  6716.                         goto GETNEXTCH;
  6717.                     } else if (tx == 2) { /* ECHO change */
  6718.                         ckxech = dpx = 0; /* Get next char */
  6719.                         goto GETNEXTCH;
  6720.                     } else if (tx == 3) { /* Quoted IAC */
  6721.                         c = 255;    /* proceeed with it. */
  6722.                     }
  6723. #ifdef IKS_OPTION
  6724.                     else if (tx == 4) {    /* IKS State Change */
  6725.                         goto GETNEXTCH;
  6726.                     }
  6727. #endif /* IKS_OPTION */
  6728.                     else if (tx == 6) {    /* Remote Logout */
  6729.             doexit(GOOD_EXIT,0);
  6730.                     } else {
  6731.             goto GETNEXTCH;    /* Unknown, get next char */
  6732.             }
  6733.                     break;
  6734. #ifdef COMMENT
  6735.                   case CR:
  6736.                     if (!TELOPT_U(TELOPT_BINARY)) {
  6737.             if (got_cr) {
  6738.                 /* This means the sender is violating Telnet   */
  6739.                 /* protocol because we received two CRs in a   */
  6740.                 /* row without getting either LF or NUL.       */
  6741.                 /* This will not solve the problem but it      */
  6742.                 /* will at least allow two CRs to do something */
  6743.                 /* whereas before the user would have to guess */
  6744.                 /* to send LF or NUL after the CR.             */
  6745.                 debug(F100,"gtword CR telnet error","",0);
  6746.                 c = LF;
  6747.             } else {
  6748.                 debug(F100,"gtword skipping CR","",0);
  6749.                 got_cr = 1;    /* Remember a CR was received */
  6750.                 goto GETNEXTCH;
  6751.             }
  6752.                     } else {
  6753.             debug(F100,"gtword CR to LF","",0);
  6754.             c = LF;
  6755.                     }
  6756.                     break;
  6757.                   case LF:
  6758.                     if (!TELOPT_U(TELOPT_BINARY)) {
  6759.             got_cr = 0;
  6760.             debug(F100,"gtword LF","",0);
  6761.                     } else {
  6762.             if (got_cr) {
  6763.                 got_cr = 0;
  6764.                 debug(F100,"gtword skipping LF","",0);
  6765.                 goto GETNEXTCH;
  6766.             }
  6767.                     }
  6768.                     break;
  6769.                   case NUL:
  6770.                     if (!TELOPT_U(TELOPT_BINARY) && got_cr) {
  6771.             c = LF;
  6772.             debug(F100,"gtword NUL to LF","",0);
  6773.                     } else {
  6774.             debug(F100,"gtword NUL","",0);
  6775.                     }
  6776.                     got_cr = 0;
  6777.                     break;
  6778. #else /* COMMENT */
  6779.                   case CR:
  6780.                     if ( !TELOPT_U(TELOPT_BINARY) && got_cr ) {
  6781.                         /* This means the sender is violating Telnet   */
  6782.                         /* protocol because we received two CRs in a   */
  6783.                         /* row without getting either LF or NUL.       */
  6784.                         /* This will not solve the problem but it      */
  6785.                         /* will at least allow two CRs to do something */
  6786.                         /* whereas before the user would have to guess */
  6787.                         /* to send LF or NUL after the CR.             */
  6788.                         debug(F100,"gtword CR telnet error","",0);
  6789.                     } else {
  6790.                         got_cr = 1;    /* Remember a CR was received */
  6791.                     }
  6792.                     /* debug(F100,"gtword CR to LF","",0); */
  6793.                     c = LF;
  6794.             break;
  6795.                   case LF:
  6796.                     if (got_cr) {
  6797.                         got_cr = 0;
  6798.                         /* debug(F100,"gtword skipping LF","",0); */
  6799.                         goto GETNEXTCH;
  6800.                     }
  6801.             break;
  6802.                   case NUL:
  6803.                     if (got_cr) {
  6804.                         got_cr = 0;
  6805.                         /* debug(F100,"gtword skipping NUL","",0); */
  6806.                         goto GETNEXTCH;
  6807. #ifdef COMMENT
  6808.                     } else {
  6809.                       debug(F100,"gtword NUL","",0);
  6810. #endif /* COMMENT */
  6811.                     }
  6812.                     break;
  6813. #endif /* COMMENT */
  6814. #ifdef IKSD
  6815.           case ETX:        /* Ctrl-C... */
  6816.                   case EOT:        /* EOT = EOF */
  6817.                       if (inserver
  6818. #ifdef CK_LOGIN
  6819.               && !x_logged
  6820. #endif /* CK_LOGIN */
  6821.               )
  6822.                           return(-4);
  6823.             break;
  6824. #endif /* IKSD */
  6825.           default:
  6826.                       got_cr = 0;
  6827.                 }
  6828.             }
  6829. #endif /* TNCODE */
  6830.     } else {
  6831. #ifdef OS2
  6832.     c = coninc(0);
  6833. #else /* OS2 */
  6834.     c = getchar();
  6835.     /* debug(F101,"cmdgetc getchar","",c); */
  6836. #endif /* OS2 */
  6837.     }
  6838. #else  /* MINIX2 */
  6839. #undef getc
  6840.     c = getc(stdin);
  6841.     /* debug(F101,"cmdgetc getc","",c); */
  6842. #endif /* MINIX2 */
  6843. #ifdef RTU
  6844.     if (rtu_bug) {
  6845.     c = getchar();            /* RTU doesn't discard the ^Z */
  6846.     rtu_bug = 0;
  6847.     }
  6848. #endif /* RTU */
  6849. #endif /* datageneral */
  6850.     return(c);                /* Return what we got */
  6851. }
  6852.  
  6853. static VOID
  6854. cmdclrscn() {                /* Clear the screen */
  6855.     ck_cls();
  6856. }
  6857.  
  6858. static VOID                /* What to echo at end of command */
  6859. #ifdef CK_ANSIC
  6860. cmdnewl(char c)
  6861. #else
  6862. cmdnewl(c) char c;
  6863. #endif /* CK_ANSIC */
  6864. /* cmdnewl */ {
  6865. #ifdef OS2
  6866. #ifdef IKSD
  6867.     extern int inserver;
  6868.     if (inserver && c == LF)
  6869.       putchar(CR);
  6870. #endif /* IKSD */
  6871. #endif /* OS2 */
  6872.  
  6873.     putchar(c);                /* c is the terminating character */
  6874.  
  6875. #ifdef WINTCP                /* what is this doing here? */
  6876.     if (c == CR) putchar(NL);
  6877. #endif /* WINTCP */
  6878.  
  6879. /*
  6880.   A.A. Chernov, who sent in changes for FreeBSD, said we also needed this
  6881.   for SVORPOSIX because "setup terminal by termios and curses does
  6882.   not convert \r to \n, so additional \n needed in newline function."  But
  6883.   it is also very likely to result in unwanted blank lines.
  6884. */
  6885. #ifdef BSD44
  6886.     if (c == CR) putchar(NL);
  6887. #endif /* BSD44 */
  6888.  
  6889. #ifdef COMMENT
  6890.     /* OS2 no longer needs this as all CR are converted to NL in coninc() */
  6891.     /* This eliminates the ugly extra blank lines discussed above.        */
  6892. #ifdef OS2
  6893.     if (c == CR) putchar(NL);
  6894. #endif /* OS2 */
  6895. #endif /* COMMENT */
  6896. #ifdef aegis
  6897.     if (c == CR) putchar(NL);
  6898. #endif /* aegis */
  6899. #ifdef AMIGA
  6900.     if (c == CR) putchar(NL);
  6901. #endif /* AMIGA */
  6902. #ifdef datageneral
  6903.     if (c == CR) putchar(NL);
  6904. #endif /* datageneral */
  6905. #ifdef GEMDOS
  6906.     if (c == CR) putchar(NL);
  6907. #endif /* GEMDOS */
  6908. #ifdef STRATUS
  6909.     if (c == CR) putchar(NL);
  6910. #endif /* STRATUS */
  6911. }
  6912.  
  6913. static VOID
  6914. cmdchardel() {                /* Erase a character from the screen */
  6915.     if (!dpx) return;
  6916. #ifdef datageneral
  6917.     /* DG '\b' is EM (^y or \031) */
  6918.     if (termtype == 1)
  6919.       /* Erase a character from non-DG screen, */
  6920.       dgncoub(1,"\010 \010",3);
  6921.     else
  6922. #endif /* datageneral */
  6923.       printf("\b \b");
  6924. #ifdef GEMDOS
  6925.     fflush(stdout);
  6926. #else
  6927. #ifdef BEBOX
  6928.     fflush(stdout);
  6929. #endif /* BEBOX */
  6930. #endif /* GEMDOS */
  6931. }
  6932.  
  6933. static VOID
  6934. #ifdef CK_ANSIC
  6935. cmdecho(char c, int quote)
  6936. #else
  6937. cmdecho(c,quote) char c; int quote;
  6938. #endif /* CK_ANSIC */
  6939. { /* cmdecho */
  6940.     if (!dpx) return;
  6941.     /* Echo tty input character c */
  6942.     if (quote) {
  6943.     putchar(BS);
  6944.     putchar(SP);
  6945.     putchar(BS);
  6946. #ifdef isprint
  6947.     putchar((CHAR) (isprint(c) ? c : '^' ));
  6948. #else
  6949.     putchar((CHAR) ((c >= SP && c < DEL) ? c : '^'));
  6950. #endif /* isprint */
  6951.     } else putchar(c);
  6952. #ifdef OS2
  6953.     if (quote==1 && c==CR) putchar((CHAR) NL);
  6954. #endif /* OS2 */
  6955.     if (timelimit)
  6956.       fflush(stdout);
  6957. }
  6958.  
  6959. /* Return pointer to current position in command buffer. */
  6960.  
  6961. char *
  6962. cmpeek() {
  6963.     return(np);
  6964. }
  6965. #endif /* NOICP */
  6966.  
  6967.  
  6968. #ifdef NOICP
  6969. #include "ckcdeb.h"
  6970. #include "ckucmd.h"
  6971. #include "ckcasc.h"
  6972. #endif /* NOICP */
  6973.  
  6974. /*  X X E S C  --  Interprets backslash codes  */
  6975. /*  Returns the int value of the backslash code if it is > -1 and < 256 */
  6976. /*  and updates the string pointer to first character after backslash code. */
  6977. /*  If the argument is invalid, leaves pointer unchanged and returns -1. */
  6978.  
  6979. int
  6980. xxesc(s) char **s; {            /* Expand backslash escapes */
  6981.     int x, y, brace, radix;        /* Returns the int value */
  6982.     char hd = '9';            /* Highest digit in radix */
  6983.     char *p;
  6984.  
  6985.     p = *s;                /* pointer to beginning */
  6986.     if (!p) return(-1);            /* watch out for null pointer */
  6987.     x = *p++;                /* character at beginning */
  6988.     if (x != CMDQ) return(-1);        /* make sure it's a backslash code */
  6989.  
  6990.     x = *p;                /* it is, get the next character */
  6991.     if (x == '{') {            /* bracketed quantity? */
  6992.     p++;                /* begin past bracket */
  6993.     x = *p;
  6994.     brace = 1;
  6995.     } else brace = 0;
  6996.     switch (x) {            /* Start interpreting */
  6997.       case 'd':                /* Decimal radix indicator */
  6998.       case 'D':
  6999.     p++;                /* Just point past it and fall thru */
  7000.       case '0':                /* Starts with digit */
  7001.       case '1':
  7002.       case '2':  case '3':  case '4':  case '5':
  7003.       case '6':  case '7':  case '8':  case '9':
  7004.     radix = 10;            /* Decimal */
  7005.     hd = '9';            /* highest valid digit */
  7006.     break;
  7007.       case 'o':                /* Starts with o or O */
  7008.       case 'O':
  7009.     radix = 8;            /* Octal */
  7010.     hd = '7';            /* highest valid digit */
  7011.     p++;                /* point past radix indicator */
  7012.     break;
  7013.       case 'x':                /* Starts with x or X */
  7014.       case 'X':
  7015.     radix = 16;            /* Hexadecimal */
  7016.     p++;                /* point past radix indicator */
  7017.     break;
  7018.       default:                /* All others */
  7019. #ifdef COMMENT
  7020.     *s = p+1;            /* Treat as quote of next char */
  7021.     return(*p);
  7022. #else
  7023.     return(-1);
  7024. #endif /* COMMENT */
  7025.     }
  7026.     /* For OS/2, there are "wide" characters required for the keyboard
  7027.      * binding, i.e \644 and similar codes larger than 255 (byte).
  7028.      * For this purpose, give up checking for < 256. If someone means
  7029.      * \266 should result in \26 followed by a "6" character, he should
  7030.      * always write \{26}6 anyway.  Now, return only the lower byte of
  7031.      * the result, i.e. 10, but eat up the whole \266 sequence and
  7032.      * put the wide result 266 into a global variable.  Yes, that's not
  7033.      * the most beautiful programming style but requires the least
  7034.      * amount of changes to other routines.
  7035.      */
  7036.     if (radix <= 10) {            /* Number in radix 8 or 10 */
  7037.     for ( x = y = 0;
  7038.            (*p) && (*p >= '0') && (*p <= hd)
  7039. #ifdef OS2
  7040.                    && (y < 5) && (x*radix < KMSIZE);
  7041.               /* the maximum needed value \8196 is 4 digits long */
  7042.               /* while as octal it requires \1377, i.e. 5 digits */
  7043. #else
  7044.                    && (y < 3) && (x*radix < 256);
  7045. #endif /* OS2 */
  7046.           p++,y++) {
  7047.         x = x * radix + (int) *p - 48;
  7048.     }
  7049. #ifdef OS2
  7050.         wideresult = x;            /* Remember wide result */
  7051.         x &= 255;
  7052. #endif /* OS2 */
  7053.     if (y == 0 || x > 255) {    /* No valid digits? */
  7054.         *s = p;            /* point after it */
  7055.         return(-1);            /* return failure. */
  7056.     }
  7057.     } else if (radix == 16) {        /* Special case for hex */
  7058.     if ((x = unhex(*p++)) < 0) { *s = p - 1; return(-1); }
  7059.     if ((y = unhex(*p++)) < 0) { *s = p - 2; return(-1); }
  7060.     x = ((x << 4) & 0xF0) | (y & 0x0F);
  7061. #ifdef OS2
  7062.         wideresult = x;
  7063.         if ((y = unhex(*p)) >= 0) {
  7064.            p++;
  7065.        wideresult = ((x << 4) & 0xFF0) | (y & 0x0F);
  7066.            x = wideresult & 255;
  7067.         }
  7068. #endif /* OS2 */
  7069.     } else x = -1;
  7070.     if (brace && *p == '}' && x > -1)    /* Point past closing brace, if any */
  7071.       p++;
  7072.     *s = p;                /* Point to next char after sequence */
  7073.     return(x);                /* Return value of sequence */
  7074. }
  7075.  
  7076. int                    /* Convert hex string to int */
  7077. #ifdef CK_ANSIC
  7078. unhex(char x)
  7079. #else
  7080. unhex(x) char x;
  7081. #endif /* CK_ANSIC */
  7082. /* unhex */ {
  7083.  
  7084.     if (x >= '0' && x <= '9')        /* 0-9 is offset by hex 30 */
  7085.       return(x - 0x30);
  7086.     else if (x >= 'A' && x <= 'F')    /* A-F offset by hex 37 */
  7087.       return(x - 0x37);
  7088.     else if (x >= 'a' && x <= 'f')    /* a-f offset by hex 57 */
  7089.       return(x - 0x57);            /* (obviously ASCII dependent) */
  7090.     else return(-1);
  7091. }
  7092.  
  7093. /*  L O O K U P  --  Lookup the string in the given array of strings  */
  7094.  
  7095. /*
  7096.   Call this way:  v = lookup(table,word,n,&x);
  7097.  
  7098.     table - a 'struct keytab' table.
  7099.     word  - the target string to look up in the table.
  7100.     n     - the number of elements in the table.
  7101.     x     - address of an integer for returning the table array index,
  7102.         or NULL if you don't need a table index.
  7103.  
  7104.   The keyword table must be arranged in ascending alphabetical order;
  7105.   alphabetic case doesn't matter but letters are treated as lowercase
  7106.   for purposes of ordering; thus "^" and "_" come *before* the letters,
  7107.   not after them.
  7108.  
  7109.   Returns the keyword's associated value (zero or greater) if found,
  7110.   with the variable x set to the keyword-table index.  If is lookup()
  7111.   is not successful, it returns:
  7112.  
  7113.    -3 if nothing to look up (target was null),
  7114.    -2 if ambiguous,
  7115.    -1 if not found.
  7116.  
  7117.   A match is successful if the target matches a keyword exactly, or if
  7118.   the target is a prefix of exactly one keyword.  It is ambiguous if the
  7119.   target matches two or more keywords from the table.
  7120.  
  7121.   Lookup() is the critical routine in scripts and so is optimized with a
  7122.   simple static cache plus some other tricks.  Maybe it could be improved
  7123.   further with binary search or hash techniques but I doubt it since most
  7124.   keyword tables are fairly short.
  7125. */
  7126.  
  7127. #ifdef USE_LUCACHE            /* Lookup cache */
  7128. extern int lusize;            /* (initialized in ckuus5.c) */
  7129. extern char * lucmd[];
  7130. extern int luval[];
  7131. extern int luidx[];
  7132. extern struct keytab * lutab[];
  7133. long luhits = 0L;
  7134. long lucalls = 0L;
  7135. long xxhits = 0L;
  7136. long luloop = 0L;
  7137. #endif /* USE_LUCACHE */
  7138.  
  7139. int
  7140. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  7141.  
  7142.     register int i, m;
  7143.     int v, len, cmdlen = 0;
  7144.     char c = NUL, c1, *s;
  7145.  
  7146. /* Get 1st char of search object, if it's null return -3. */
  7147.  
  7148.     if (!cmd || n < 1)            /* Defense de nullarg */
  7149.       return(-3);
  7150.     c1 = *cmd;                /* First character */
  7151.     if (!c1)                /* Make sure there is one */
  7152.       return(-3);
  7153.     if (isupper(c1))            /* If letter make it lowercase */
  7154.       c1 = tolower(c1);
  7155.  
  7156. #ifdef USE_LUCACHE            /* lookup() cache */
  7157.     m = lusize;
  7158.     lucalls++;                /* Count this lookup() call */
  7159.     for (i = 0; i < m; i++) {        /* Loop thru cache */
  7160.     if (*(lucmd[i]) == c1) {    /* Same as 1st char of search item? */
  7161.         if (lutab[i] == table) {    /* Yes - same table too? */
  7162.         if (!strcmp(cmd,lucmd[i])) { /* Yes - compare */
  7163.             if (x) *x = luidx[i];    /* Match - return index */
  7164.             luhits++;                /* Count cache hit */
  7165.             return(luval[i]);        /* Return associated value */
  7166.         }
  7167.         }
  7168.     }
  7169.     }
  7170. #endif /* USE_LUCACHE */
  7171.  
  7172. /* Not null, not in cache, look it up */
  7173.  
  7174.     s = cmd;
  7175.     while (*s++) cmdlen++;        /* Length of target */
  7176. /*
  7177.   Quick binary search to find last table entry whose first character is
  7178.   lexically less than the first character of the search object.  This is
  7179.   the starting point of the next loop, which must go in sequence since it
  7180.   compares adjacent table entries.
  7181. */
  7182.     if (n < 5) {            /* Not worth it for small tables */
  7183.     i = 0;
  7184.     } else {
  7185.     int lo = 0;
  7186.     int hi = n;
  7187.     int count = 0;
  7188.     while (lo+2 < hi && ++count < 12) {
  7189.         i = lo + ((hi - lo) / 2);
  7190.         c = *(table[i].kwd);
  7191.         if (isupper(c)) c = tolower(c);
  7192.         if (c < c1) {
  7193.         lo = i;
  7194.         } else {
  7195.         hi = i;
  7196.         }
  7197.     }
  7198.     i = (c < c1) ? lo+1 : lo;
  7199. #ifdef USE_LUCACHE
  7200.     if (i > 0) xxhits++;
  7201. #endif /* USE_LUCACHE */
  7202.     }
  7203.     for ( ; i < n-1; i++) {
  7204. #ifdef USE_LUCACHE
  7205.     luloop++;
  7206. #endif /* USE_LUCACHE */
  7207.     v = 0;
  7208.     c = *(table[i].kwd);
  7209.     if (c) {
  7210.         if (isupper(c)) c = tolower(c);
  7211.  
  7212.         /* The following is a big performance booster but makes it */
  7213.         /* absolutely essential that all lookup() tables are in order. */
  7214.  
  7215.         if (c > c1)            /* Leave early if past our mark */
  7216.           return(-1);
  7217.  
  7218. #ifdef DEBUG
  7219.         /* Use LOG DEBUG to check */
  7220.  
  7221.         if (deblog) {
  7222.         if (ckstrcmp(table[i].kwd,table[i+1].kwd,0,0) > 0) {
  7223.             printf("TABLE OUT OF ORDER [%s] [%s]\n",
  7224.                table[i].kwd,table[i+1].kwd);
  7225.  
  7226.         }
  7227.         }
  7228. #endif /* DEBUG */
  7229.  
  7230.         if (c == c1) {
  7231.         len = 0;
  7232.         s = table[i].kwd;
  7233.         while (*s++) len++;
  7234.         if ((len == cmdlen && !ckstrcmp(table[i].kwd,cmd,len,0)) ||
  7235.             ((v = !ckstrcmp(table[i].kwd,cmd,cmdlen,0)) &&
  7236.              ckstrcmp(table[i+1].kwd,cmd,cmdlen,0))) {
  7237.             if (x) *x = i;
  7238.             return(table[i].kwval);
  7239.         }
  7240.         } else v = 0;
  7241.     }
  7242.         if (v) {            /* Ambiguous */
  7243.         if (x) *x = i;        /* Set index of first match */
  7244.         return(-2);
  7245.     }
  7246.     }
  7247.  
  7248. /* Last (or only) element */
  7249.  
  7250.     if (!ckstrcmp(table[n-1].kwd,cmd,cmdlen,0)) {
  7251.         if (x) *x = n-1;
  7252.     debug(F111,"lookup",table[i].kwd,table);
  7253.         return(table[n-1].kwval);
  7254.     } else return(-1);
  7255. }
  7256.  
  7257. /*
  7258.   x l o o k u p
  7259.  
  7260.   Like lookup, but requires a full (but case-independent) match
  7261.   and does NOT require the table to be in order.
  7262. */
  7263. int
  7264. xlookup(table,cmd,n,x) struct keytab table[]; char *cmd; int n, *x; {
  7265.     register int i;
  7266.     int len, cmdlen, one = 0;
  7267.     register char c, * s, * s2;
  7268.  
  7269.     if (!cmd) cmd = "";            /* Check args */
  7270.     if (!*cmd || n < 1) return(-3);
  7271.  
  7272.     c = *cmd;                /* First char of string to look up */
  7273.     if (!*(cmd+1)) {            /* Special handling for 1-char names */
  7274.     cmdlen = 1;
  7275.     if (isupper(c)) { c = tolower(c); *cmd = c; }
  7276.     one = 1;
  7277.     } else {
  7278.     cmdlen = 0;
  7279.     s = cmd;
  7280.     while (*s++) cmdlen++;
  7281.     c = *cmd;
  7282.     if (isupper(c))
  7283.       c = tolower(c);
  7284.     }
  7285.     if (cmdlen < 1)
  7286.       return(-3);
  7287.  
  7288.     for (i = 0; i < n; i++) {
  7289.     s = table[i].kwd;        /* This entry */
  7290.     if (!s) s = "";
  7291.     if (!*s) continue;        /* Empty table entry */
  7292.     if (c != *s) continue;        /* First char doesn't match */
  7293.     if (one) {            /* Name is one char long */
  7294.         if (!*(s+1)) {
  7295.         if (x) *x = i;
  7296.         return(table[i].kwval);    /* So is table entry */
  7297.         }
  7298.     } else {            /* Otherwise do string comparison */
  7299.         s2 = s;
  7300.         len = 0;
  7301.         while (*s2++) len++;
  7302.         if (len == cmdlen && !ckstrcmp(s,cmd,-1,0)) {
  7303.         if (x) *x = i;
  7304.         return(table[i].kwval);
  7305.         }
  7306.     }
  7307.     }
  7308.     return(-1);
  7309. }
  7310.  
  7311. /* Reverse lookup */
  7312.  
  7313. char *
  7314. rlookup(table,n,x) struct keytab table[]; int n, x; {
  7315.     int i;
  7316.     for (i = 0; i < n; i++) {
  7317.         if (table[i].kwval == x)
  7318.       return(table[i].kwd);
  7319.     }
  7320.     return(NULL);
  7321. }
  7322.  
  7323. #ifndef NOICP
  7324. int
  7325. cmdsquo(x) int x; {
  7326.     quoting = x;
  7327.     return(1);
  7328. }
  7329.  
  7330. int
  7331. cmdgquo() {
  7332.     return(quoting);
  7333. }
  7334. #endif /* NOICP */
  7335.