home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckucmd.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  213KB  |  7,822 lines

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