home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv200.zip / ckuus6.c < prev    next >
C/C++ Source or Header  |  2001-12-07  |  285KB  |  10,343 lines

  1. #include "ckcsym.h"
  2. #ifndef NOICP
  3.  
  4. /*  C K U U S 6 --  "User Interface" for Unix Kermit (Part 6)  */
  5.  
  6. /*
  7.   Author: Frank da Cruz (fdc@columbia.edu),
  8.   Columbia University Academic Information Systems, New York City.
  9.  
  10.   Copyright (C) 1985, 2001,
  11.     Trustees of Columbia University in the City of New York.
  12.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  13.     copyright text in the ckcmai.c module for disclaimer and permissions.
  14. */
  15.  
  16. /* Includes */
  17.  
  18. #include "ckcdeb.h"
  19. #include "ckcasc.h"
  20. #include "ckcker.h"
  21. #include "ckuusr.h"
  22. #include "ckcxla.h"
  23. #include "ckcnet.h"            /* Network symbols */
  24. #include <signal.h>
  25.  
  26. #ifdef VMS
  27. #ifndef TCPSOCKET
  28. #include <errno.h>
  29. #endif /* TCPSOCKET */
  30. #endif /* VMS */
  31.  
  32. #ifdef datageneral
  33. #define fgets(stringbuf,max,fd) dg_fgets(stringbuf,max,fd)
  34. #endif /* datageneral */
  35.  
  36. #ifdef QNX6
  37. #define readblock kreadblock
  38. #endif /* QNX6 */
  39.  
  40. /* External Kermit Variables, see ckmain.c for description. */
  41.  
  42. extern xx_strp xxstring;
  43.  
  44. extern int local, xitsta, binary, parity, escape, flow, cmd_rows, turn,
  45.   turnch, duplex, ckxech, seslog, dfloc, cnflg, tlevel, pflag, msgflg, mdmtyp,
  46.   zincnt, quiet, repars, techo, network, nzxopts, what, filepeek, recursive;
  47.  
  48. extern int xaskmore, tt_rows, tt_cols, cmd_cols, g_matchdot, diractive,
  49.   xcmdsrc, nscanfile, reliable, nolinks;
  50.  
  51. #ifdef VMSORUNIX
  52. extern int zgfs_dir, zgfs_link;
  53. #endif /* VMSORUNIX */
  54.  
  55. #ifdef CK_IFRO
  56.   extern int remonly;
  57. #endif /* CK_IFRO */
  58.  
  59. #ifdef OS2
  60. extern int StartedFromDialer ;
  61. extern int vmode;
  62. extern int k95stdout;
  63. #ifndef NT
  64. #define INCL_NOPM
  65. #define INCL_VIO            /* Needed for ckocon.h */
  66. #include <os2.h>
  67. #undef COMMENT
  68. #else
  69. #define APIRET ULONG
  70. #include <windows.h>
  71. #include <tapi.h>
  72. #include "ckntap.h"
  73. #endif /* NT */
  74. #include "ckocon.h"
  75. #endif /* OS2 */
  76.  
  77. extern long vernum, speed;
  78. extern char *versio, *protv, *ckxv, *ckzv, *fnsv, *connv, *dftty, *cmdv;
  79. extern char *dialv, *loginv, *for_def[], *whil_def[], *xif_def[], *sw_def[];
  80. extern char *foz_def[];
  81. extern char *ckxsys, *ckzsys;
  82. #ifndef OS2
  83. extern char *DIRCMD;
  84. #ifndef UNIX
  85. extern char *DELCMD;
  86. #endif /* UNIX */
  87. #endif /* OS2 */
  88. extern char ttname[], filnam[];
  89. extern CHAR sstate, feol;
  90. extern char *zinptr;
  91.  
  92. #ifdef UNIX
  93. extern char ** mtchs;            /* zxpand() file list */
  94. #endif /* UNIX */
  95.  
  96. #ifndef NOXFER
  97. extern int oopts, omode, oname, opath;    /* O-Packet options */
  98.  
  99. extern int stdinf, sndsrc, size, rpsiz, urpsiz, fncnv, fnrpath, displa,
  100.   stdouf, isguest, pktlog, nfils, keep, maxrps, fblksiz, frecl, frecfm,
  101.   atcapr, atdiso, spsizf, spsiz, spsizr, spmax, wslotr, prefixing,
  102.   fncact, fnspath, nprotos, g_proto, g_urpsiz, g_spsizf,
  103.   g_spsiz, g_spsizr, g_spmax, g_wslotr, g_prefixing, g_fncact, g_fncnv,
  104.   g_fnspath, g_fnrpath, xfrxla, g_xfrxla;
  105.  
  106. extern char *cmarg, *cmarg2;
  107.  
  108. #ifndef NOMSEND                /* Multiple SEND */
  109. extern char *msfiles[];
  110. #endif /* NOMSEND */
  111. extern char fspec[];            /* Most recent filespec */
  112. extern int fspeclen;
  113.  
  114. #ifdef CK_TMPDIR
  115. extern int f_tmpdir;            /* Directory changed temporarily */
  116. extern char savdir[];            /* For saving current directory */
  117. #endif /* CK_TMPDIR */
  118.  
  119. extern struct keytab protos[];        /* File transfer protocols */
  120. extern struct ck_p ptab[NPROTOS];
  121. #endif /* NOXFER */
  122.  
  123. #ifdef DCMDBUF                /* Declarations from cmd package */
  124. extern char *cmdbuf, *atmbuf;        /* Command buffers */
  125. #else
  126. extern char cmdbuf[], atmbuf[];        /* Command buffers */
  127. #endif /* DCMDBUF */
  128.  
  129. extern int nopush;
  130.  
  131. #ifndef NOSPL
  132. int askflag = 0;            /* ASK-class command active */
  133. extern char **a_ptr[];
  134. extern int a_dim[];
  135. extern char **m_xarg[];
  136. extern int n_xarg[];
  137. extern struct mtab *mactab;
  138. extern int nmac;
  139. extern long ck_alarm;
  140. extern char alrm_date[], alrm_time[];
  141. extern int x_ifnum;
  142. #endif /* NOSPL */
  143.  
  144. extern int inserver;            /* I am IKSD */
  145. extern int backgrd;            /* Kermit executing in background */
  146. extern char psave[];            /* For saving & restoring prompt */
  147. extern char *tp;            /* Temporary buffer */
  148.  
  149. int readblock = 4096;            /* READ buffer size */
  150. CHAR * readbuf = NULL;            /* Pointer to read buffer */
  151. int readsize = 0;            /* Number of chars actually read */
  152. int getcmd = 0;                /* GET-class command was given */
  153.  
  154. extern int zchkod, zchkid;
  155.  
  156. struct keytab deltab[] = {        /* DELETE Command Options */
  157.     { "/all",           DEL_ALL,  CM_INV },
  158.     { "/after",         DEL_AFT,  CM_ARG },
  159.     { "/ask",           DEL_ASK,  0 },
  160.     { "/before",        DEL_BEF,  CM_ARG },
  161.     { "/directories",   DEL_DIR,  0 },
  162.     { "/dotfiles",      DEL_DOT,  0 },
  163.     { "/except",        DEL_EXC,  CM_ARG },
  164.     { "/heading",       DEL_HDG,  0 },
  165.     { "/l",             DEL_LIS,  CM_INV|CM_ABR },
  166.     { "/larger-than",   DEL_LAR,  CM_ARG },
  167.     { "/list",          DEL_LIS,  0 },
  168.     { "/log",           DEL_LIS,  CM_INV },
  169.     { "/noask",         DEL_NAS,  0 },
  170.     { "/nodotfiles",    DEL_NOD,  0 },
  171.     { "/noheading",     DEL_NOH,  0 },
  172.     { "/nol",           DEL_NOL,  CM_INV|CM_ABR },
  173.     { "/nolist",        DEL_NOL,  0 },
  174.     { "/nolog",         DEL_NOL,  CM_INV },
  175.     { "/nopage",        DEL_NOP,  0 },
  176.     { "/not-after",     DEL_NAF,  CM_ARG },
  177.     { "/not-before",    DEL_NBF,  CM_ARG },
  178.     { "/not-since",     DEL_NAF,  CM_INV|CM_ARG },
  179.     { "/page",          DEL_PAG,  0 },
  180.     { "/quiet",         DEL_QUI,  CM_INV },
  181.     { "/recursive",     DEL_REC,  0 },
  182.     { "/simulate",      DEL_SIM,  0 },
  183.     { "/since",         DEL_AFT,  CM_ARG|CM_INV },
  184.     { "/smaller-than",  DEL_SMA,  CM_ARG },
  185.     { "/summary",       DEL_SUM,  0 },
  186.     { "/tree",          DEL_ALL,  0 },
  187.     { "/type",          DEL_TYP,  CM_ARG },
  188.     { "/verbose",       DEL_VRB,  CM_INV }
  189. };
  190. int ndeltab = sizeof(deltab)/sizeof(struct keytab);
  191.  
  192. /* /QUIET-/VERBOSE (/LIST-/NOLIST) (/LOG-/NOLOG) table */
  193.  
  194. struct keytab qvswtab[] = {
  195.     { "/l",           DEL_LIS,  CM_INV|CM_ABR },
  196.     { "/list",        DEL_LIS,  0 },
  197.     { "/log",         DEL_LIS,  CM_INV },
  198.     { "/nol",         DEL_NOL,  CM_INV|CM_ABR },
  199.     { "/nolist",      DEL_NOL,  0 },
  200.     { "/nolog",       DEL_NOL,  CM_INV },
  201.     { "/quiet",       DEL_QUI,  CM_INV },
  202.     { "/verbose",     DEL_VRB,  CM_INV }
  203. };
  204. int nqvswtab = sizeof(qvswtab)/sizeof(struct keytab);
  205.  
  206. struct keytab copytab[] = {
  207.     { "/append",      998,      0 },
  208. #ifndef NOSPL
  209.     { "/fromb64",     997,      0 },
  210. #endif /* NOSPL */
  211.     { "/l",           DEL_LIS,  CM_INV|CM_ABR },
  212.     { "/list",        DEL_LIS,  0 },
  213.     { "/log",         DEL_LIS,  CM_INV },
  214.     { "/nol",         DEL_NOL,  CM_INV|CM_ABR },
  215.     { "/nolist",      DEL_NOL,  0 },
  216.     { "/nolog",       DEL_NOL,  CM_INV },
  217.     { "/quiet",       DEL_QUI,  CM_INV },
  218.     { "/swap-bytes",  999,      0 },
  219. #ifndef NOSPL
  220.     { "/tob64",       996,      0 },
  221. #endif /* NOSPL */
  222.     { "/verbose",     DEL_VRB,  CM_INV }
  223. };
  224. int ncopytab = sizeof(copytab)/sizeof(struct keytab);
  225.  
  226. #ifndef NOXFER
  227. static struct keytab gettab[] = {    /* GET options */
  228.     { "/as-name",         SND_ASN, CM_ARG },
  229.     { "/binary",          SND_BIN, 0 },
  230. #ifdef CALIBRATE
  231.     { "/calibrate",       SND_CAL, CM_INV },
  232. #endif /* CALIBRATE */
  233. #ifdef PIPESEND
  234.     { "/command",         SND_CMD, CM_PSH },
  235. #endif /* PIPESEND */
  236.     { "/delete",          SND_DEL, 0 },
  237.     { "/except",          SND_EXC, CM_ARG },
  238.     { "/filenames",       SND_NAM, CM_ARG },
  239. #ifdef PIPESEND
  240.     { "/filter",          SND_FLT, CM_ARG|CM_PSH },
  241. #endif /* PIPESEND */
  242. #ifdef VMS
  243.     { "/image",           SND_IMG, 0 },
  244.     { "/labeled",         SND_LBL, 0 },
  245. #else
  246.     { "/image",           SND_BIN, CM_INV },
  247. #endif /* VMS */
  248. #ifdef CK_TMPDIR
  249.     { "/move-to",         SND_MOV, CM_ARG },
  250. #endif /* CK_TMPDIR */
  251.     { "/pathnames",       SND_PTH, CM_ARG },
  252.     { "/pipes",           SND_PIP, CM_ARG|CM_PSH },
  253.     { "/quiet",           SND_SHH, 0 },
  254. #ifdef CK_RESEND
  255.     { "/recover",         SND_RES, 0 },
  256. #endif /* CK_RESEND */
  257.     { "/recursive",       SND_REC, 0 },
  258.     { "/rename-to",       SND_REN, CM_ARG },
  259. #ifdef COMMENT
  260.     { "/smaller-than",    SND_SMA, CM_ARG },
  261. #endif /* COMMENT */
  262.     { "/subdirectories",  SND_REC, CM_INV },
  263.     { "/text",            SND_TXT, 0 },
  264.     { "/transparent",     SND_XPA, 0 }
  265. };
  266. #define NGETTAB sizeof(gettab)/sizeof(struct keytab)
  267. static int ngettab = NGETTAB;
  268.  
  269. static struct keytab rcvtab[] = {    /* RECEIVE options */
  270.     { "/as-name",         SND_ASN, CM_ARG },
  271.     { "/binary",          SND_BIN, 0 },
  272. #ifdef CALIBRATE
  273.     { "/calibrate",       SND_CAL, CM_INV },
  274. #endif /* CALIBRATE */
  275. #ifdef PIPESEND
  276.     { "/command",         SND_CMD, CM_PSH },
  277. #endif /* PIPESEND */
  278.     { "/except",          SND_EXC, CM_ARG },
  279.     { "/filenames",       SND_NAM, CM_ARG },
  280. #ifdef PIPESEND
  281.     { "/filter",          SND_FLT, CM_ARG|CM_PSH },
  282. #endif /* PIPESEND */
  283. #ifdef VMS
  284.     { "/image",           SND_IMG, 0 },
  285.     { "/labeled",         SND_LBL, 0 },
  286. #else
  287.     { "/image",           SND_BIN, CM_INV },
  288. #endif /* VMS */
  289. #ifdef CK_TMPDIR
  290.     { "/move-to",         SND_MOV, CM_ARG },
  291. #endif /* CK_TMPDIR */
  292.     { "/pathnames",       SND_PTH, CM_ARG },
  293.     { "/pipes",           SND_PIP, CM_ARG|CM_PSH },
  294. #ifdef CK_XYZ
  295.     { "/protocol",        SND_PRO, CM_ARG },
  296. #else
  297.     { "/protocol",        SND_PRO, CM_ARG|CM_INV },
  298. #endif /* CK_XYZ */
  299.     { "/quiet",           SND_SHH, 0 },
  300.     { "/recursive",       SND_REC, 0 },
  301.     { "/rename-to",       SND_REN, CM_ARG },
  302.     { "/text",            SND_TXT, 0 },
  303.     { "/transparent",     SND_XPA, 0 }
  304. };
  305. #define NRCVTAB sizeof(rcvtab)/sizeof(struct keytab)
  306. static int nrcvtab = NRCVTAB;
  307. #endif /* NOXFER */
  308.  
  309. /* WAIT table */
  310.  
  311. #define WAIT_FIL 997
  312. #define WAIT_MDM 998
  313.  
  314. struct keytab waittab[] = {
  315.     { "cd",            BM_DCD,   CM_INV }, /* (Carrier Detect) */
  316.     { "cts",           BM_CTS,   CM_INV }, /* (Clear To Send)  */
  317.     { "dsr",           BM_DSR,   CM_INV }, /* (Data Set Ready) */
  318.     { "file",          WAIT_FIL, 0 },       /* New category selector keywords */
  319.     { "modem-signals", WAIT_MDM, 0 },       /* ... */
  320.     { "ri",            BM_RNG,   CM_INV    }  /* (Ring Indicator) */
  321. };
  322. int nwaittab = (sizeof(waittab) / sizeof(struct keytab));
  323.  
  324. /* Modem signal table */
  325.  
  326. struct keytab mstab[] = {
  327.     { "cd",    BM_DCD, 0 },        /* Carrier Detect */
  328.     { "cts",   BM_CTS, 0 },        /* Clear To Send  */
  329.     { "dsr",   BM_DSR, 0 },        /* Data Set Ready */
  330.     { "ri",    BM_RNG, 0 }        /* Ring Indicator */
  331. };
  332. int nms = (sizeof(mstab) / sizeof(struct keytab));
  333.  
  334. #define WF_MOD 1
  335. #define WF_DEL 2
  336. #define WF_CRE 3
  337.  
  338. struct keytab wfswi[] = {        /* WAIT FILE switches */
  339.     { "creation",     WF_CRE, 0 },    /* Wait for file to be created */
  340.     { "deletion",     WF_DEL, 0 },    /* Wait for file to be deleted */
  341.     { "modification", WF_MOD, 0 }    /* Wait for file to be modified */
  342. };
  343. int nwfswi = (sizeof(wfswi) / sizeof(struct keytab));
  344.  
  345. #ifndef NOSPL
  346. struct keytab asgtab[] = {        /* Assignment operators for "." */
  347.     { "::=", 2, 0 },            /* ASSIGN and EVALUATE */
  348.     { ":=",  1, 0 },            /* ASSIGN */
  349.     { "=",   0, 0 }            /* DEFINE */
  350. };
  351. int nasgtab = (sizeof(asgtab) / sizeof(struct keytab));
  352.  
  353. struct keytab opntab[] = {
  354. #ifndef NOPUSH
  355.     { "!read",  OPN_PI_R, CM_INV },
  356.     { "!write", OPN_PI_W, CM_INV },
  357. #endif /* NOPUSH */
  358.     { "append", OPN_FI_A, 0 },
  359.     { "host",   OPN_NET,  0 },
  360. #ifdef OS2
  361.     { "line",   OPN_SER,  CM_INV },
  362.     { "port",   OPN_SER,  0 },
  363. #else
  364.     { "line",   OPN_SER,  0 },
  365.     { "port",   OPN_SER,  CM_INV },
  366. #endif /* OS2 */
  367.     { "read",   OPN_FI_R, 0 },
  368.     { "write",  OPN_FI_W, 0 }
  369. };
  370. int nopn = (sizeof(opntab) / sizeof(struct keytab));
  371.  
  372. /* IF conditions */
  373.  
  374. #define  XXIFCO 0       /* IF COUNT */
  375. #define  XXIFER 1       /* IF ERRORLEVEL */
  376. #define  XXIFEX 2       /* IF EXIST */
  377. #define  XXIFFA 3       /* IF FAILURE */
  378. #define  XXIFSU 4       /* IF SUCCESS */
  379. #define  XXIFNO 5       /* IF NOT */
  380. #define  XXIFDE 6       /* IF DEFINED */
  381. #define  XXIFEQ 7    /* IF EQUAL (strings) */
  382. #define  XXIFAE 8       /* IF = (numbers) */
  383. #define  XXIFLT 9       /* IF < (numbers) */
  384. #define  XXIFGT 10      /* IF > (numbers) */
  385. #define  XXIFLL 11      /* IF Lexically Less Than (strings) */
  386. #define  XXIFLG 12      /* IF Lexically Greater Than (strings) */
  387. #define  XXIFEO 13      /* IF EOF (READ file) */
  388. #define  XXIFBG 14      /* IF BACKGROUND */
  389. #define  XXIFNU 15    /* IF NUMERIC */
  390. #define  XXIFFG 16      /* IF FOREGROUND */
  391. #define  XXIFDI 17      /* IF DIRECTORY */
  392. #define  XXIFNE 18      /* IF NEWER */
  393. #define  XXIFRO 19      /* IF REMOTE-ONLY */
  394. #define  XXIFAL 20    /* IF ALARM */
  395. #define  XXIFSD 21      /* IF STARTED-FROM-DIALER */
  396. #define  XXIFTR 22      /* IF TRUE */
  397. #define  XXIFNT 23      /* IF FALSE */
  398. #define  XXIFTM 24      /* IF TERMINAL-MACRO */
  399. #define  XXIFEM 25      /* IF EMULATION */
  400. #define  XXIFOP 26    /* IF OPEN */
  401. #define  XXIFLE 27    /* IF <= */
  402. #define  XXIFGE 28    /* IF >= */
  403. #define  XXIFIP 29      /* IF INPATH */
  404. #define  XXIFTA 30      /* IF TAPI */
  405. #define  XXIFMA 31    /* IF MATCH */
  406. #define  XXIFFL 32    /* IF FLAG */
  407. #define  XXIFAB 33    /* IF ABSOLUTE */
  408. #define  XXIFAV 34    /* IF AVAILABLE */
  409. #define  XXIFAT 35      /* IF ASKTIMEOUT */
  410. #define  XXIFRD 36      /* IF READABLE */
  411. #define  XXIFWR 37      /* IF WRITEABLE */
  412. #define  XXIFAN 38    /* IF ... AND ... */
  413. #define  XXIFOR 39    /* IF ... OR ... */
  414. #define  XXIFLP 40      /* IF left parenthesis */
  415. #define  XXIFRP 41      /* IF right parenthesis */
  416. #define  XXIFNQ 42      /* IF != (== "NOT =") */
  417. #define  XXIFQU 43      /* IF QUIET */
  418. #define  XXIFCK 44    /* IF C-KERMIT */
  419. #define  XXIFK9 45    /* IF K-95 */
  420. #define  XXIFMS 46    /* IF MS-KERMIT */
  421. #define  XXIFWI 47    /* IF WILD */
  422. #define  XXIFLO 48    /* IF LOCAL */
  423. #define  XXIFCM 49    /* IF COMMAND */
  424. #define  XXIFFP 50    /* IF FLOAT */
  425. #define  XXIFIK 51      /* IF IKS */
  426. #define  XXIFKB 52    /* IF KBHIT */
  427. #define  XXIFKG 53    /* IF KERBANG */
  428. #define  XXIFVE 54    /* IF VERSION */
  429. #define  XXIFDC 55    /* IF DECLARED */
  430. #define  XXIFGU 56    /* IF GUI */
  431.  
  432. struct keytab iftab[] = {        /* IF commands */
  433.     { "!",          XXIFNO, 0 },
  434.     { "!=",         XXIFNQ, 0 },
  435.     { "&&",         XXIFAN, 0 },
  436.     { "(",          XXIFLP, 0 },
  437.     { ")",          XXIFRP, 0 },
  438.     { "<",          XXIFLT, 0 },
  439.     { "<=",         XXIFLE, 0 },
  440.     { "=",          XXIFAE, 0 },
  441.     { "==",         XXIFAE, CM_INV },
  442.     { ">",          XXIFGT, 0 },
  443.     { ">=",         XXIFGE, 0 },
  444.     { "absolute",   XXIFAB, 0 },
  445.     { "alarm",      XXIFAL, 0 },
  446.     { "and",        XXIFAN, 0 },
  447.     { "asktimeout", XXIFAT, 0 },
  448.     { "available",  XXIFAV, 0 },
  449.     { "background", XXIFBG, 0 },
  450.     { "c-kermit",   XXIFCK, 0 },
  451.     { "command",    XXIFCM, 0 },
  452.     { "count",      XXIFCO, 0 },
  453.     { "dcl",        XXIFDC, CM_INV },
  454.     { "declared",   XXIFDC, 0 },
  455.     { "defined",    XXIFDE, 0 },
  456. #ifdef CK_TMPDIR
  457.     { "directory",  XXIFDI, 0 },
  458. #endif /* CK_TMPDIR */
  459.     { "emulation",  XXIFEM, 0 },
  460. #ifdef COMMENT
  461.     { "eof",        XXIFEO, 0 },
  462. #endif /* COMMENT */
  463.     { "equal",      XXIFEQ, 0 },
  464.     { "error",      XXIFFA, CM_INV },
  465.     { "exist",      XXIFEX, 0 },
  466.     { "failure",    XXIFFA, 0 },
  467.     { "false",      XXIFNT, 0 },
  468.     { "flag",       XXIFFL, 0 },
  469. #ifdef CKFLOAT
  470.     { "float",      XXIFFP, 0 },
  471. #endif /* CKFLOAT */
  472.     { "foreground", XXIFFG, 0 },
  473. #ifdef OS2
  474.     { "gui",        XXIFGU, 0 },
  475. #else
  476.     { "gui",        XXIFGU, CM_INV },
  477. #endif /* OS2 */
  478. #ifdef IKSD
  479.     { "iksd",       XXIFIK, 0 },
  480. #else
  481.     { "iksd",       XXIFIK, CM_INV },
  482. #endif /* IKSD */
  483.     { "integer",    XXIFNU, CM_INV },
  484.     { "k-95",       XXIFK9, 0 },
  485.     { "kbhit",      XXIFKB, 0 },
  486. #ifdef UNIX
  487.     { "kerbang",    XXIFKG, 0 },
  488. #else
  489.     { "kerbang",    XXIFKG, CM_INV },
  490. #endif /* UNIX */
  491.     { "lgt",        XXIFLG, 0 },
  492.     { "llt",        XXIFLL, 0 },
  493.     { "local",      XXIFLO, 0 },
  494.     { "match",      XXIFMA, 0 },
  495.     { "ms-kermit",  XXIFMS, CM_INV },
  496. #ifdef ZFCDAT
  497.     { "newer",      XXIFNE, 0 },
  498. #endif /* ZFCDAT */
  499.     { "not",        XXIFNO, 0 },
  500.     { "numeric",    XXIFNU, 0 },
  501.     { "ok",         XXIFSU, CM_INV },
  502.     { "open",       XXIFOP, 0 },
  503.     { "or",         XXIFOR, 0 },
  504.     { "quiet",      XXIFQU, 0 },
  505.     { "readable",   XXIFRD, 0 },
  506.     { "remote-only",XXIFRO, 0 },
  507.     { "started-from-dialer",XXIFSD, CM_INV },
  508.     { "success",    XXIFSU, 0 },
  509.     { "tapi",       XXIFTA, 0 },
  510. #ifdef OS2
  511.     { "terminal-macro", XXIFTM, CM_INV },
  512. #endif /* OS2 */
  513.     { "true",       XXIFTR, 0 },
  514.     { "version",    XXIFVE, 0 },
  515.     { "wild",       XXIFWI, 0 },
  516.     { "writeable",  XXIFWR, 0 },
  517.     { "||",         XXIFOR, 0 },
  518.     { "", 0, 0 }
  519. };
  520. int nif = (sizeof(iftab) / sizeof(struct keytab)) - 1;
  521.  
  522. struct keytab iotab[] = {        /* Keywords for IF OPEN */
  523.     { "!read-file",      ZRFILE, CM_INV },
  524.     { "!write-file",     ZWFILE, CM_INV },
  525.     { "append-file",     ZWFILE, CM_INV },
  526.     { "connection",      8888,   0 },
  527. #ifdef CKLOGDIAL
  528.     { "cx-log",          7777,   0 },
  529. #endif /* CKLOGDIAL */
  530.     { "debug-log",       ZDFILE, 0 },
  531.     { "error",           9999,   0 },
  532.     { "packet-log",      ZPFILE, 0 },
  533.     { "read-file",       ZRFILE, 0 },
  534.     { "screen",          ZSTDIO, 0 },
  535.     { "session-log",     ZSFILE, 0 },
  536.     { "transaction-log", ZTFILE, 0 },
  537.     { "write-file",      ZWFILE, 0 }
  538. };
  539. int niot = (sizeof(iotab) / sizeof(struct keytab));
  540. #endif /* NOSPL */
  541.  
  542. /* Variables and prototypes */
  543.  
  544. #ifdef NETCONN
  545. extern int nnetdir;            /* How many network directories */
  546. #endif /* NETCONN */
  547. #ifdef CK_AUTHENTICATION
  548. _PROTOTYP(int ck_krb4_is_installed,(void));
  549. _PROTOTYP(int ck_krb5_is_installed,(void));
  550. _PROTOTYP(int ck_ntlm_is_installed,(void));
  551. _PROTOTYP(int ck_srp_is_installed,(void));
  552. _PROTOTYP(int ck_ssleay_is_installed,(void));
  553. _PROTOTYP(int ck_crypt_is_installed,(void));
  554. #else
  555. #define ck_krb4_is_installed() (0)
  556. #define ck_krb5_is_installed() (0)
  557. #define ck_ntlm_is_installed() (0)
  558. #define ck_srp_is_installed() (0)
  559. #define ck_ssleay_is_installed() (0)
  560. #define ck_crypt_is_installed() (0)
  561. #endif /* CK_AUTHENTICATION */
  562.  
  563. #define AV_KRB4   1
  564. #define AV_KRB5   2
  565. #define AV_NTLM   3
  566. #define AV_SRP    4
  567. #define AV_SSL    5
  568. #define AV_CRYPTO 6
  569.  
  570. struct keytab availtab[] = {         /* Available authentication types */
  571.     { "crypto",     AV_CRYPTO, CM_INV }, /* and encryption */
  572.     { "encryption", AV_CRYPTO, 0 },
  573.     { "k4",         AV_KRB4,   CM_INV },
  574.     { "k5",         AV_KRB5,   CM_INV },
  575.     { "kerberos4",  AV_KRB4,   0 },
  576.     { "kerberos5",  AV_KRB5,   0 },
  577.     { "krb4",       AV_KRB4,   CM_INV },
  578.     { "krb5",       AV_KRB5,   CM_INV },
  579.     { "ntlm",       AV_NTLM,   0 },
  580.     { "srp",        AV_SRP,    0 },
  581.     { "ssl",        AV_SSL,    0 },
  582.     { "tls",        AV_SSL,    0 },
  583.     { "",           0,         0 }
  584. };
  585. int availtabn = sizeof(availtab)/sizeof(struct keytab)-1;
  586.  
  587. #ifndef NODIAL
  588. _PROTOTYP(static int ddcvt, (char *, FILE *, int) );
  589. _PROTOTYP(static int dncvt, (int, int, int, int) );
  590. _PROTOTYP(char * getdname, (void) );
  591.  
  592. static int partial  = 0;        /* For partial dial */
  593. static char *dscopy = NULL;
  594. int dialtype = -1;
  595.  
  596. char *dialnum = (char *)0;        /* Remember DIAL number for REDIAL */
  597. int dirline = 0;            /* Dial directory line number */
  598. extern char * dialdir[];        /* Dial directory file names */
  599. extern int dialdpy;            /* DIAL DISPLAY on/off */
  600. extern int ndialdir;            /* How many dial directories */
  601. extern int ntollfree;            /* Toll-free call info */
  602. extern int ndialpxx;            /* List of PBX exchanges */
  603. extern char *dialtfc[];
  604. char * matchpxx = NULL;            /* PBX exchange that matched */
  605. extern int nlocalac;            /* Local area-code list */
  606. extern char * diallcac[];
  607. extern int tttapi;
  608. #ifdef CK_TAPI
  609. extern int tapiconv;            /* TAPI Conversions */
  610. extern int tapipass;            /* TAPI Passthrough */
  611. #endif /* CK_TAPI */
  612. extern int dialatmo;
  613. extern char * dialnpr, * dialsfx;
  614. extern char * dialixp, * dialixs, * dialmac;
  615. extern char * dialldp, * diallds, * dialtfp;
  616. extern char * dialpxi, * dialpxo, * diallac;
  617. extern char * diallcp, * diallcs, * diallcc;
  618. extern char * dialpxx[];
  619.  
  620. extern int dialcnf;            /* DIAL CONFIRMATION */
  621. int dialfld = 0;            /* DIAL FORCE-LONG-DISTANCE */
  622. int dialsrt = 1;            /* DIAL SORT ON */
  623. int dialrstr = 6;            /* DIAL RESTRICTION */
  624. int dialtest = 0;            /* DIAL TEST */
  625. int dialcount = 0;            /* \v(dialcount) */
  626.  
  627. extern int dialsta;            /* Dial status */
  628. int dialrtr = -1,            /* Dial retries */
  629.     dialint = 10;            /* Dial retry interval */
  630. extern long dialcapas;            /* Modem capabilities */
  631. extern int dialcvt;            /* DIAL CONVERT-DIRECTORY */
  632. #endif /* NODIAL */
  633.  
  634. #ifndef NOSPL
  635. #define IFCONDLEN 256
  636. int ifc,                /* IF case */
  637.     not = 0,                /* Flag for IF NOT */
  638.     ifargs = 0;                /* Count of IF condition words */
  639. char ifcond[IFCONDLEN];            /* IF condition text */
  640. char *ifcp;                /* Pointer to IF condition text */
  641. #ifdef DCMDBUF
  642. extern int
  643.  *ifcmd,  *count,  *iftest, *intime,
  644.  *inpcas, *takerr, *merror, *xquiet;
  645. #else
  646. extern int ifcmd[];            /* Last command was IF */
  647. extern int iftest[];            /* Last IF was true */
  648. extern int count[];            /* For IF COUNT, one for each cmdlvl */
  649. extern int intime[];            /* Ditto for other stackables... */
  650. extern int inpcas[];
  651. extern int takerr[];
  652. extern int merror[];
  653. extern int xquiet[];
  654. #endif /* DCMDBUF */
  655. #else
  656. extern int takerr[];
  657. #endif /* NOSPL */
  658.  
  659. #ifdef DCMDBUF
  660. extern char *line;            /* Character buffer for anything */
  661. extern char *tmpbuf;
  662. #else
  663. extern char line[], tmpbuf[];
  664. #endif /* DCMDBUF */
  665. extern char *lp;            /* Pointer to line buffer */
  666.  
  667. int cwdf = 0;                /* CWD has been done */
  668.  
  669. /* Flags for ENABLE/DISABLE */
  670. extern int en_cwd, en_cpy, en_del, en_dir, en_fin,
  671.    en_get, en_hos, en_ren, en_sen, en_set, en_spa, en_typ, en_who, en_bye,
  672.    en_asg, en_que, en_ret, en_mai, en_pri, en_mkd, en_rmd, en_xit, en_ena;
  673.  
  674. extern FILE *tfile[];            /* File pointers for TAKE command */
  675. extern char *tfnam[];            /* Names of TAKE files */
  676. extern int tfline[];            /* TAKE-file line number */
  677.  
  678. extern int success;            /* Command success/failure flag */
  679. extern int cmdlvl;            /* Current position in command stack */
  680.  
  681. #ifndef NOSPL
  682. extern int maclvl;            /* Macro to execute */
  683. extern char *macx[];            /* Index of current macro */
  684. extern char *mrval[];            /* Macro return value */
  685. extern char *macp[];            /* Pointer to macro */
  686. extern int macargc[];            /* ARGC from macro invocation */
  687.  
  688. #ifdef COMMENT
  689. extern char *m_line[];
  690. #endif /* COMMENT */
  691.  
  692. extern char *m_arg[MACLEVEL][NARGS];    /* Stack of macro arguments */
  693. extern char *g_var[];            /* Global variables %a, %b, etc */
  694.  
  695. #ifdef DCMDBUF
  696. extern struct cmdptr *cmdstk;        /* The command stack itself */
  697. #else
  698. extern struct cmdptr cmdstk[];        /* The command stack itself */
  699. #endif /* DCMDBUF */
  700. #endif /* NOSPL */
  701.  
  702. #define xsystem(s) zsyscmd(s)
  703.  
  704. static int x, y, z = 0;
  705. static char *s, *p;
  706.  
  707. #ifdef OS2
  708. _PROTOTYP( int os2settitle, (char *, int) );
  709. #endif /* OS2 */
  710.  
  711. extern struct keytab yesno[], onoff[], fntab[];
  712. extern int nyesno, nfntab;
  713.  
  714. #ifndef NOSPL
  715.  
  716. /* Do the ASK, ASKQ, GETOK, and READ commands */
  717.  
  718. int asktimedout = 0;
  719.  
  720. #define ASK_PUP 1
  721. #define ASK_TMO 2
  722.  
  723.  
  724. static struct keytab asktab[] = {
  725. #ifdef OS2
  726.     { "/popup",   ASK_PUP,      0 },
  727. #endif /* OS2 */
  728.     { "/timeout", ASK_TMO, CM_ARG },
  729.     { "", 0, 0 }
  730. };
  731. static int nasktab = sizeof(asktab)/sizeof(struct keytab)-1;
  732.  
  733. int
  734. doask(cx) int cx; {
  735.     extern int cmflgs, asktimer, timelimit;
  736. #ifdef CK_RECALL
  737.     extern int on_recall;
  738. #endif /* CK_RECALL */
  739.     int popupflg = 0, mytimer = 0;
  740. #ifdef CK_APC
  741.     extern int apcactive, apcstatus;
  742. #endif /* CK_APC */
  743.  
  744.     char vnambuf[VNAML+1];        /* Buffer for variable names */
  745.     char *vnp = NULL;            /* Pointer to same */
  746.  
  747. #ifdef CK_APC
  748.     if ( apcactive != APC_INACTIVE && (apcstatus & APC_NOINP) ) {
  749.         return(success = 0);
  750.     }
  751. #endif /* CK_APC */
  752.  
  753.     mytimer = asktimer;            /* Inherit global ASK timer */
  754.  
  755.     if (cx == XXASK || cx == XXASKQ) {
  756.     struct FDB sw, fl;
  757.     int getval;
  758.     char c;
  759.     if (cx == XXASKQ)        /* Don't log ASKQ response */
  760.       debok = 0;
  761.     cmfdbi(&sw,            /* First FDB - command switches */
  762.            _CMKEY,            /* fcode */
  763.            "Variable name or switch",
  764.            "",            /* default */
  765.            "",            /* addtl string data */
  766.            nasktab,            /* addtl numeric data 1: tbl size */
  767.            4,            /* addtl numeric data 2: 4 = cmswi */
  768.            xxstring,        /* Processing function */
  769.            asktab,            /* Keyword table */
  770.            &fl            /* Pointer to next FDB */
  771.            );
  772.     cmfdbi(&fl,            /* Anything that doesn't match */
  773.            _CMFLD,            /* fcode */
  774.            "",            /* hlpmsg */
  775.            "",            /* default */
  776.            "",            /* addtl string data */
  777.            0,            /* addtl numeric data 1 */
  778.            0,            /* addtl numeric data 2 */
  779.            NULL,
  780.            NULL,
  781.            NULL
  782.            );
  783.     while (1) {            /* Parse 0 or more switches */
  784.         x = cmfdb(&sw);        /* Parse something */
  785.         if (x < 0)
  786.           return(x);
  787.         if (cmresult.fcode != _CMKEY) /* Break out if not a switch */
  788.           break;
  789.         c = cmgbrk();
  790.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  791.         printf("?This switch does not take an argument\n");
  792.         return(-9);
  793.         }
  794.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  795.         printf("?This switch requires an argument\n");
  796.         return(-9);
  797.         }
  798.         switch (cmresult.nresult) {
  799.           case ASK_PUP:
  800.         popupflg = 1;
  801.         break;
  802.           case ASK_TMO: {
  803.           if ((y = cmnum("seconds","1",10,&x,xxstring)) < 0)
  804.             return(y);
  805.           if (x < 0)
  806.             x = 0;
  807.           mytimer = x;
  808.           break;
  809.           }
  810.           default: return(-2);
  811.         }
  812.     }
  813.     /* Have variable name, make copy. */
  814.     ckstrncpy(vnambuf,cmresult.sresult,VNAML);
  815.     vnp = vnambuf;
  816.     if (vnambuf[0] == CMDQ &&
  817.         (vnambuf[1] == '%' || vnambuf[1] == '&'))
  818.       vnp++;
  819.     y = 0;
  820.     if (*vnp == '%' || *vnp == '&') {
  821.         if ((y = parsevar(vnp,&x,&z)) < 0)
  822.           return(y);
  823.     }
  824.     } else if (cx != XXGOK && cx != XXRDBL) { /* Get variable name */
  825.     if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  826.         if (y == -3) {
  827.         printf("?Variable name required\n");
  828.         return(-9);
  829.         } else return(y);
  830.     }
  831.     ckstrncpy(vnambuf,s,VNAML);    /* Make a copy. */
  832.     vnp = vnambuf;
  833.     if (vnambuf[0] == CMDQ &&
  834.         (vnambuf[1] == '%' || vnambuf[1] == '&'))
  835.       vnp++;
  836.     y = 0;
  837.     if (*vnp == '%' || *vnp == '&') {
  838.         if ((y = parsevar(vnp,&x,&z)) < 0)
  839.           return(y);
  840.     }
  841.     }
  842.     if (cx == XXREA || cx == XXRDBL) {    /* READ or READBLOCK command */
  843.     if ((y = cmcfm()) < 0)        /* Get confirmation */
  844.       return(y);
  845.     if (chkfn(ZRFILE) < 1) {    /* File open? */
  846.         printf("?Read file not open\n");
  847.         return(success = 0);
  848.     }
  849.     if (!(s = (char *)readbuf)) {        /* Where to read into. */
  850.         printf("?Oops, no READ buffer!\n");
  851.         return(success = 0);
  852.     }
  853.     y = zsinl(ZRFILE, s, readblock); /* Read a line. */
  854.     debug(F111,"read zsinl",s,y);
  855.     if (y < 0) {            /* On EOF or other error, */
  856.         zclose(ZRFILE);        /* close the file, */
  857.         delmac(vnp,0);        /* delete the variable, */
  858.         return(success = 0);    /* and return failure. */
  859.     } else {            /* Read was OK. */
  860.         readsize = (int) strlen(s);
  861.         success = (addmac(vnp,s) < 0 ? 0 : 1); /* Define variable */
  862.         debug(F111,"read addmac",vnp,success);
  863.         return(success);        /* Return success. */
  864.     }
  865.     }
  866.  
  867.     /* ASK, ASKQ, GETOK, or GETC */
  868.  
  869.     if ((y = cmtxt("Prompt, enclose in { braces } to preserve\n\
  870. leading and trailing spaces, precede question mark with backslash (\\).",
  871.            "",&p,xxstring)) < 0) {
  872.     return(y);
  873.     }
  874.     if (!p) p = "";
  875. #ifndef NOLOCAL
  876. #ifdef OS2
  877.     if (popupflg) {            /* Popup requested */
  878.     ckstrncpy(tmpbuf,brstrip(p),TMPBUFSIZ);
  879.     p = tmpbuf;
  880.     if (cx == XXASK || cx == XXASKQ) {
  881.         int rc;
  882.         if (cx == XXASK)
  883.           rc = popup_readtext(vmode,p,line,LINBUFSIZ,mytimer);
  884.         else
  885.           rc = popup_readpass(vmode,p,line,LINBUFSIZ,mytimer);
  886.             asktimedout = ( rc == -1 && mytimer );
  887.     }
  888.     y = addmac(vnp,(char *)line);    /* Add it to the macro table. */
  889.     timelimit = 0;
  890.     return(success = y < 0 ? 0 : 1);
  891.     }
  892. #endif /* OS2 */
  893. #endif /* NOLOCAL */
  894.  
  895.     concb((char)escape);        /* Enter CBREAK mode */
  896.     cmsavp(psave,PROMPTL);        /* Save old prompt */
  897.     cmsetp(brstrip(p));            /* Make new prompt */
  898. reprompt:
  899.     if (cx == XXASKQ) {            /* For ASKQ, */
  900.     cmini(0);            /* no-echo mode. */
  901.     } else {                /* For others, regular echoing. */
  902.     cmini(ckxech);
  903.     }
  904.     askflag = 1;
  905.     x = -1;                /* This means to reparse. */
  906.     cmflgs = 0;
  907.     if (pflag)
  908.       prompt(xxstring);            /* Issue prompt. */
  909.  
  910.     asktimedout = 0;            /* Handle timed responses. */
  911.     timelimit = mytimer;
  912. reparse:
  913.     cmres();
  914.     if (cx == XXGOK) {            /* GETOK */
  915. #ifdef CK_RECALL
  916.     on_recall = 0;
  917. #endif /* CK_RECALL */
  918.     askflag = 0;
  919.     x = cmkey(yesno,nyesno,"","",xxstring);    /* GETOK uses keyword table */
  920.     if (x < 0) {            /* Parse error */
  921.         if (x == -10) {
  922.         x = 0;
  923.         printf("?Timed out, assuming \"No\"\n");
  924.         asktimedout = 1;
  925.         goto gokdone;
  926.         } else if (x == -3) {    /* No answer? */
  927.         printf("Please respond Yes or No\n"); /* Make them answer */
  928.         cmini(ckxech);
  929.         goto reprompt;
  930.         } else if (x == -1) {
  931.         goto reparse;
  932.         } else
  933.           goto reprompt;
  934.     }
  935.     if (cmcfm() < 0)        /* Get confirmation */
  936.       goto reparse;
  937.   gokdone:
  938.     askflag = 0;
  939.     cmsetp(psave);            /* Restore prompt */
  940. #ifdef VMS
  941.     if (cmdlvl > 0)            /* In VMS and not at top level, */
  942.       conres();            /*  restore console again. */
  943. #endif /* VMS */
  944.     timelimit = 0;
  945.     return(x);            /* Return success or failure */
  946.     } else if (cx == XXGETC        /* GETC */
  947. #ifdef OS2
  948.            || cx == XXGETK        /* or GETKEYCODE */
  949. #endif /* OS2 */
  950.            ) {            /* GETC */
  951.     char tmp[16];
  952.     conbin((char)escape);        /* Put keyboard in raw mode */
  953. #ifndef NOSETKEY
  954. #ifdef OS2
  955.     if (cx == XXGETK) {        /* GETKEYCODE */
  956.         extern int os2gks;
  957.         int t;
  958.         t = os2gks;            /* Turn off kverb recognition */
  959.         os2gks = 0;
  960.         x = congks(timelimit);    /* Read a key event, blocking */
  961.         os2gks = t;            /* Put back kverb recognition */
  962.     } else                 /* GETC */
  963. #endif /* OS2 */
  964. #endif /* NOSETKEY */
  965.     {
  966.         debug(F101,"GETC conchk","",conchk());
  967.         x = coninc(timelimit);    /* Just read one character */
  968.         debug(F101,"GETC coninc","",x);
  969.     }
  970.     concb((char)escape);        /* Put keyboard back in cbreak mode */
  971.     if (x > -1) {
  972.         if (xcmdsrc == 0)
  973.           printf("\r\n");
  974. #ifdef OS2
  975.         if (cx == XXGETK) {        /* GETKEYCODE */
  976.         sprintf(tmp,"%d",x);    /* SAFE */
  977.         } else {
  978. #endif /* OS2 */
  979.         tmp[0] = (char) (x & 0xff);
  980.         tmp[1] = NUL;
  981. #ifdef OS2
  982.         }
  983. #endif /* OS2 */
  984.         y = addmac(vnp,tmp);    /* Add it to the macro table. */
  985.         debug(F111,"getc/getk addmac",vnp,y);
  986.     } else y = -1;
  987.     cmsetp(psave);            /* Restore old prompt. */
  988.     if (x < -1) {
  989.         asktimedout = 1;
  990.         if (!quiet)
  991.           printf("?Timed out\n");
  992.     }
  993.     timelimit = 0;
  994.     return(success = y < 0 ? 0 : 1);
  995.     } else {                /* ASK or ASKQ */
  996. #ifdef CK_RECALL
  997.     on_recall = 0;
  998. #endif /* CK_RECALL */
  999.     y = cmdgquo();            /* Get current quoting */
  1000.     cmdsquo(0);            /* Turn off quoting */
  1001.     while (x == -1) {        /* Prompt till they answer */
  1002.         x = cmtxt("Please respond.","",&s,NULL);
  1003.         debug(F111,"ASK cmtxt",s,x);
  1004.         cmres();
  1005.     }
  1006.     cmdsquo(y);            /* Restore previous quoting */
  1007.     if (cx == XXASKQ)        /* ASKQ must echo CRLF here */
  1008.       printf("\r\n");
  1009.     if (x < 0) {            /* If cmtxt parse error, */
  1010.         cmsetp(psave);        /* restore original prompt */
  1011. #ifdef VMS
  1012.         if (cmdlvl > 0)        /* In VMS and not at top level, */
  1013.           conres();            /*  restore console again. */
  1014. #endif /* VMS */
  1015.         if (x == -10) {
  1016.         printf("?Timed out\n");
  1017.         asktimedout = 1;
  1018.         x = -9;
  1019.         }
  1020.         timelimit = 0;
  1021.         return(x);            /* and return cmtxt's error code. */
  1022.     }
  1023.     if (*s == NUL) {        /* If user typed a bare CR, */
  1024.         cmsetp(psave);        /* Restore old prompt, */
  1025.         delmac(vnp,0);        /* delete variable if it exists, */
  1026. #ifdef VMS
  1027.         if (cmdlvl > 0)        /* In VMS and not at top level, */
  1028.           conres();            /*  restore console again. */
  1029. #endif /* VMS */
  1030.         timelimit = 0;
  1031.         return(success = 1);    /* and return. */
  1032.     }
  1033.     y = addmac(vnp,s);        /* Add it to the macro table. */
  1034.     debug(F111,"ask addmac",vnp,y);
  1035.     cmsetp(psave);            /* Restore old prompt. */
  1036. #ifdef VMS
  1037.     if (cmdlvl > 0)            /* In VMS and not at top level, */
  1038.       conres();            /*  restore console again. */
  1039. #endif /* VMS */
  1040.     timelimit = 0;
  1041.     return(success = y < 0 ? 0 : 1);
  1042.     }
  1043. }
  1044. #endif /* NOSPL */
  1045.  
  1046. #ifndef NOSPL
  1047. int
  1048. doincr(cx) int cx; {            /* INCREMENT, DECREMENT */
  1049.     char vnambuf[VNAML+1];        /* Buffer for variable names */
  1050.     int eval = 0;
  1051.     eval = (cx == XX_DECR || cx == XX_INCR);
  1052.  
  1053.     if ((y = cmfld("Variable name","",&s, eval ? xxstring : NULL)) < 0) {
  1054.     if (y == -3) {
  1055.         printf("?Variable name required\n");
  1056.         return(-9);
  1057.     } else return(y);
  1058.     }
  1059.     ckstrncpy(vnambuf,s,VNAML);
  1060.     if ((y = cmnum("by amount","1",10,&x,xxstring)) < 0)
  1061.       return(y);
  1062.     if ((y = cmcfm()) < 0)
  1063.       return(y);
  1064.  
  1065.     z = (cx == XX_INCR || cx == XXINC) ? 1 : 0;    /* Increment or decrement? */
  1066.  
  1067.     if (incvar(vnambuf,x,z) < 0) {
  1068.     printf("?Variable %s not defined or not numeric\n",vnambuf);
  1069.     return(success = 0);
  1070.     }
  1071.     return(success = 1);
  1072. }
  1073.  
  1074. /* Used by doundef() */
  1075. static int
  1076. xxundef(s,verbose,simulate) char * s; int verbose, simulate; {
  1077.     int rc = 0;
  1078.     if (!s) return(0);
  1079.     if (*s == CMDQ && *(s+1) == '%') {
  1080.     char c = *(s+2), * p = NULL;
  1081.     if (c >= '0' && c <= '9') {
  1082.         if (maclvl < 0)
  1083.           p = g_var[c];
  1084.         else
  1085.           p = m_arg[maclvl][c - '0'];
  1086.     } else {
  1087.         if (isupper(c)) c += ('a'-'A');
  1088.         if (c >= 'a' && c <= 'z')
  1089.           p = g_var[c];
  1090.     }
  1091.     if (!p) return(-1);
  1092.     }
  1093.     if (verbose)
  1094.       printf(" %s ",s);
  1095.     if (simulate) {
  1096.     printf("(SELECTED)\n");
  1097.     } else if ((x = delmac(s,1)) > -1) { /* Full name required */
  1098.     rc = 1;
  1099.     if (verbose) printf("(OK)\n");
  1100.     } else if (verbose)
  1101.       printf("(FAILED)\n");
  1102.     return(rc);
  1103. }
  1104.  
  1105. /* Do the (_)DEFINE, (_)ASSIGN, and UNDEFINE commands */
  1106.  
  1107. #define UND_MAT 1
  1108. #define UND_VRB 2
  1109. #define UND_EXC 3
  1110. #define UND_SIM 3
  1111.  
  1112. static struct keytab undefswi[] = {
  1113.     { "/list",     UND_VRB, 0 },
  1114. #ifdef COMMENT
  1115.     { "/except",   UND_EXC, CM_ARG },
  1116. #endif /* COMMENT */
  1117.     { "/matching", UND_MAT, 0 },
  1118.     { "/simulate", UND_SIM, 0 },
  1119.     { "/verbose",  UND_VRB, CM_INV }
  1120. };
  1121. static int nundefswi = sizeof(undefswi) / sizeof(struct keytab);
  1122.  
  1123. #define UNDEFMAX 64
  1124. static char ** undeflist = NULL;
  1125. int
  1126. doundef(cx) int cx; {            /* UNDEF, _UNDEF */
  1127.     int i, j, n, rc = 0, arraymsg = 0;
  1128.     int domatch = 0, verbose = 0, errors = 0, simulate = 0, flag = 0;
  1129.     char *vnp, vnbuf[4];
  1130. #ifdef COMMENT
  1131.     char *except = NULL;
  1132. #endif /* COMMENT */
  1133.     struct FDB sw, fl;
  1134.     int getval;
  1135.     char c;
  1136.  
  1137.     if (!undeflist) {            /* Allocate list if necessary */
  1138.     undeflist = (char **)malloc(UNDEFMAX * sizeof(char *));
  1139.     if (!undeflist) {
  1140.         printf("?Memory allocation failure\n");
  1141.         return(-9);
  1142.     }
  1143.     for (i = 0; i < UNDEFMAX; i++)
  1144.       undeflist[i] = NULL;
  1145.     }
  1146.     cmfdbi(&sw,                /* First FDB - command switches */
  1147.        _CMKEY,            /* fcode */
  1148.        "Variable name or switch",
  1149.        "",                /* default */
  1150.        "",                /* addtl string data */
  1151.        nundefswi,            /* addtl numeric data 1: tbl size */
  1152.        4,                /* addtl numeric data 2: 4 = cmswi */
  1153.        xxstring,            /* Processing function */
  1154.        undefswi,            /* Keyword table */
  1155.        &fl                /* Pointer to next FDB */
  1156.        );
  1157.     cmfdbi(&fl,                /* Anything that doesn't match */
  1158.        _CMFLD,            /* fcode */
  1159.        "",                /* hlpmsg */
  1160.        "",                /* default */
  1161.        "",                /* addtl string data */
  1162.        0,                /* addtl numeric data 1 */
  1163.        0,                /* addtl numeric data 2 */
  1164.        (cx == XXUNDEF) ? NULL : xxstring,
  1165.        NULL,
  1166.        NULL
  1167.        );
  1168.     while (1) {                /* Parse 0 or more switches */
  1169.     x = cmfdb(&sw);            /* Parse something */
  1170.     if (x < 0)
  1171.       return(x);
  1172.     if (cmresult.fcode != _CMKEY)    /* Break out if not a switch */
  1173.       break;
  1174.     c = cmgbrk();
  1175.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  1176.         printf("?This switch does not take an argument\n");
  1177.         return(-9);
  1178.     }
  1179.     switch (cmresult.nresult) {
  1180.       case UND_MAT: domatch  = 1; break;
  1181.       case UND_SIM: simulate = 1; /* fall thru on purpose */
  1182.       case UND_VRB: verbose  = 1; break;
  1183.  
  1184. #ifdef COMMENT
  1185.       case UND_EXC:
  1186.         if (!getval) break;
  1187.         if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  1188.         if (x == -3) {
  1189.             printf("?Pattern required\n");
  1190.             x = -9;
  1191.         }
  1192.         goto xgetx;
  1193.         }
  1194.         makestr(&except,cmresult.sresult);
  1195.         break;
  1196. #endif /* COMMENT */
  1197.  
  1198.       default:
  1199.         return(-2);
  1200.     }
  1201.     }
  1202.     n = 0;
  1203.     makestr(&(undeflist[n++]),cmresult.sresult);
  1204.     for (i = 1; i < UNDEFMAX; i++) {
  1205.     x = cmfld("Macro or variable name","",&s,
  1206.           ((cx == XXUNDEF) ? NULL : xxstring)
  1207.           );
  1208.     if (x == -3) {
  1209.         if ((y = cmcfm()) < 0)
  1210.           return(y);
  1211.         break;
  1212.     } else if (y < 0) {
  1213.         return(y);
  1214.     }
  1215.     makestr(&(undeflist[n++]),s);
  1216.     }
  1217.     /* Now we have a list of n variables or patterns to undefine */
  1218.  
  1219.     for (i = 0; i < n; i++) {
  1220.     flag = 0;
  1221.     if (!(vnp = undeflist[i]))
  1222.       continue;
  1223.     if (vnp[0] == CMDQ && (vnp[1] == '%' || vnp[1] == '&')) {
  1224.         flag++;
  1225.         vnp++;
  1226.     }
  1227.     if (!domatch) {            /* Pattern match not requested */
  1228.         if (flag) {
  1229.         if ((y = parsevar(vnp,&x,&z)) < 0) {
  1230.             vnp--;
  1231.             if (verbose) printf(" %s...error\n",vnp);
  1232.             continue;
  1233.         }
  1234.         vnp--;
  1235.         }
  1236.         x = xxundef(vnp,verbose,simulate);
  1237.         if (x > -1) {
  1238.         if (!x && !simulate) errors++;
  1239.         rc += x;
  1240.         }
  1241.         continue;
  1242.     }
  1243.     /* Pattern match requested */
  1244.  
  1245.     if (!flag) {            /* It's a macro */
  1246.         for (j = 0; j < nmac; j++) {
  1247.         if (ckmatch(vnp,mactab[j].kwd,0,1)) {
  1248.             x = xxundef(mactab[j].kwd,verbose,simulate);
  1249.             if (x > -1) {
  1250.             rc += x;
  1251.             if (!x) errors++;
  1252.             }
  1253.             if (!simulate)
  1254.               j--;        /* Because mactab shifted up */
  1255.         }
  1256.         }
  1257.     } else if (vnp[0] == '%') {    /* It's a \%x variable */
  1258.         vnbuf[0] = CMDQ;
  1259.         vnbuf[1] = '%';
  1260.         vnbuf[3] = NUL;
  1261.         for (j = '0'; j <= 'z'; j++) { /* 0..9 a..z */
  1262.         vnbuf[2] = j;
  1263.         if (ckmatch(vnp,&vnbuf[1],0,1)) {
  1264.             x = xxundef(vnbuf,verbose,simulate);
  1265.             if (x > -1) {
  1266.             if (!x) errors++;
  1267.             rc += x;
  1268.             }
  1269.         }
  1270.         if (j == '9') j = (int)'a' - 1; /* 9 -> a */
  1271.         }
  1272.     } else if (vnp[0] == '&') {
  1273.         if (!arraymsg && !quiet) {
  1274.         printf("?UNDEFINE /MATCH can't be used with arrays.\n");
  1275.         printf("(Type HELP ARRAY to see other methods.)\n");
  1276.         }
  1277.         arraymsg++;
  1278.         errors++;
  1279.     }
  1280.     }
  1281.     if (verbose)
  1282.       printf("undefined: %d, errors: %d\n",rc,errors);
  1283.  
  1284.     for (i = 0; i < UNDEFMAX; i++) {    /* Check them all */
  1285.     if (undeflist[i]) {        /* in case we were interrupted */
  1286.         free(undeflist[i]);        /* previously... */
  1287.         undeflist[i] = NULL;
  1288.     }
  1289.     }
  1290.     return(success = (errors == 0) ? 1 : 0);
  1291. }
  1292.  
  1293. int
  1294. dodef(cx) int cx; {
  1295.     extern int xxdot;
  1296.     extern char ppvnambuf[];
  1297.     int doeval = 0;
  1298.     char vnambuf[VNAML+1];        /* Buffer for variable names */
  1299.     char *vnp;                /* Pointer to same */
  1300.     int k, mydot;
  1301.     mydot = xxdot;            /* Copy */
  1302.     xxdot = 0;                /* and reset */
  1303. /*
  1304.   In case we got here from a command that begins like ".\%a", cmkey() has
  1305.   already evaluated \%a, but we don't want that, so we retrieve the variable
  1306.   name from a special pre-evaluation buffer in the command module, and we
  1307.   undo the "unget word" that would be done because of the token, because if
  1308.   the variable was defined, it will unget its value rather than its name.
  1309. */
  1310.     s = NULL;
  1311.  
  1312.     if (mydot && ppvnambuf[0] == '.' && ppvnambuf[1]) {
  1313.     s = ppvnambuf+1;
  1314.     unungw();
  1315.     }
  1316.     if (!s) {
  1317.     if (cx == XXDFX || cx == XXASX)
  1318.       /* Evaluate variable name */
  1319.       y = cmfld("Macro or variable name","",&s,xxstring);
  1320.     else
  1321.       /* Don't evaluate the variable name */
  1322.       y = cmfld("Macro or variable name","",&s,NULL);
  1323.     if (y < 0) {
  1324.         if (y == -3) {
  1325.         printf("?Variable name required\n");
  1326.         return(-9);
  1327.         } else return(y);
  1328.     }
  1329.     }
  1330.     k = strlen(s);
  1331.     if (k > VNAML) {
  1332.     printf("?Name too long: \"%s\"\n",s);
  1333.     return(-9);
  1334.     }
  1335.     ckstrncpy(vnambuf,s,VNAML);
  1336.     vnambuf[VNAML] = NUL;
  1337.     vnp = vnambuf;
  1338.     if (vnambuf[0] == CMDQ && (vnambuf[1] == '%' || vnambuf[1] == '&')) vnp++;
  1339.     if (*vnp == '%' || *vnp == '&') {
  1340.     if ((y = parsevar(vnp,&x,&z)) < 0) return(y);
  1341. #ifdef COMMENT
  1342.     if (cx == XXUNDEF) {        /* Undefine */
  1343.         if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1344.         delmac(vnp,0);
  1345.         return(success = 1);
  1346.     }
  1347. #endif /* COMMENT */
  1348.     debug(F101,"dodef parsevar x","",x);
  1349.     if (mydot) {
  1350.         if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1351.           return(doeval);
  1352.         if (doeval > 0)        /* Type of assignment */
  1353.           cx = XXASS;
  1354.     }
  1355.     if (y == 1) {            /* Simple variable */
  1356.         if ((y = cmtxt("Definition of variable","",&s,NULL)) < 0)
  1357.           return(y);
  1358.         s = brstrip(s);
  1359.         debug(F110,"xxdef var name",vnp,0);
  1360.         debug(F110,"xxdef var def",s,0);
  1361.     } else if (y == 2) {        /* Array element */
  1362.         if ((y = arraynam(vnp,&x,&z)) < 0) return(y);
  1363.         if (x == 96) {
  1364.         printf("?Argument vector array is read-only\n");
  1365.         return(-9);
  1366.         }
  1367.         if (chkarray(x,z) < 0) return(-2);
  1368.         if ((y = cmtxt("Definition of array element","",&s,NULL)) < 0)
  1369.           return(y);
  1370.         debug(F110,"xxdef array ref",vnp,0);
  1371.         debug(F110,"xxdef array def",s,0);
  1372.     }
  1373.     } else {                /* Macro */
  1374. #ifdef COMMENT
  1375.     if (cx == XXUNDEF) {        /* Undefine */
  1376.         if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1377.         delmac(vnp,0);
  1378.         return(success = 1);
  1379.     }
  1380. #endif /* COMMENT */
  1381.     if (mydot) {
  1382.         if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1383.           return(doeval);
  1384.         if (doeval > 0)
  1385.           cx = XXASS;
  1386.     }
  1387.     if ((y = cmtxt("Definition of macro","",&s,NULL)) < 0) return(y);
  1388. #ifdef DEBUG
  1389.     if (deblog) {
  1390.         debug(F110,"xxdef macro name",vnp,0);
  1391.         debug(F010,"xxdef macro def",s,0);
  1392.     }
  1393. #endif /* DEBUG */
  1394.     s = brstrip(s);
  1395.     }
  1396.     if (*s == NUL) {            /* No arg given, undefine */
  1397.     delmac(vnp,0);            /* silently... */
  1398.     return(success = 1);        /* even if it doesn't exist... */
  1399.     }
  1400.     /* Defining a new macro or variable */
  1401.  
  1402.     if (cx == XXASS || cx == XXASX) {    /* ASSIGN rather than DEFINE? */
  1403.     int t;
  1404.     t = LINBUFSIZ-1;
  1405.     lp = line;            /* If so, expand its value now */
  1406.     zzstring(s,&lp,&t);
  1407.     s = line;
  1408.     }
  1409.     if (doeval == 2) {            /* Arithmetic evaluation wanted too? */
  1410.     ckstrncpy(line,evala(s),LINBUFSIZ);
  1411.     line[LINBUFSIZ] = NUL;
  1412.     }
  1413.     /* debug(F111,"calling addmac",s,(int)strlen(s)); */
  1414.  
  1415.     y = addmac(vnp,s);            /* Add it to the appropriate table. */
  1416.     if (y < 0) {
  1417.     printf("?%s failed\n",(cx == XXASS || cx == XXASX) ?
  1418.            "ASSIGN" : "DEFINE");
  1419.     return(success = 0);
  1420.     } else if (cx == XXASX || cx == XXDFX) /* For _ASG or _DEF, */
  1421.       return(1);               /* don't change success variable */
  1422.     else
  1423.       return(success = 1);
  1424. }
  1425. #endif /* NOSPL */
  1426.  
  1427.  
  1428. #ifndef NODIAL
  1429. /*
  1430.    L U D I A L  --  Lookup up dialing directory entry.
  1431.  
  1432.    Call with string to look up and file descriptor of open dialing directory
  1433.    file.  On success, returns number of matches found, with numbers stored
  1434.    in an array accessible via getdnum().
  1435. */
  1436. static char *dn_p[MAXDNUMS + 1];    /* Dial Number pointers */
  1437. static char *dn_p2[MAXDNUMS + 1];    /* Converted dial number pointers */
  1438. static int dn_x[MAXDNUMS + 1];        /* Type of call */
  1439. static int dncount = 0;
  1440. char * d_name = NULL;            /* Dial name pointer */
  1441.  
  1442. char *                    /* Get dial directory entry name */
  1443. getdname() {
  1444.     return(d_name ? d_name : "");
  1445. }
  1446.  
  1447. char *
  1448. getdnum(n) int n; {            /* Get dial number n from directory */
  1449.     if (n < 0 || n > dncount || n > MAXDNUMS)
  1450.       return("");
  1451.     else
  1452.       return(dn_p[n]);
  1453. }
  1454.  
  1455. char *            /* Check area code for spurious leading digit */
  1456. chk_ac(i,buf) int i; char buf[]; {
  1457.     char *p;
  1458.     if (!buf)
  1459.       return("");
  1460.     p = (char *) buf;            /* Country we are calling: */
  1461.     if (i ==  44 ||            /* UK */
  1462.     i ==  49 ||            /* Germany */
  1463.     i ==  39 ||            /* Italy */
  1464.     i ==  31 ||            /* Netherlands */
  1465.     i == 351 ||            /* Portugal */
  1466.     i ==  55 ||            /* Brazil */
  1467.     i == 972 ||            /* Israel */
  1468.     i ==  41 ||            /* Switzerland */
  1469.     i ==  43 ||            /* Austria */
  1470.     i ==  42 ||            /* Czech Republic */
  1471.     i ==  36 ||            /* Hungary */
  1472.     i ==  30 ||            /* Greece */
  1473.     i == 352 ||            /* Luxembourg */
  1474.     i ==  48 ||            /* Poland */
  1475.     i ==  27 ||            /* South Africa */
  1476.     i ==  33 ||            /* France (as of 1997) */
  1477.     i ==  358            /* Finland (ditto) */
  1478.     ) {
  1479.     if (buf[0] == '0')
  1480.       p++;
  1481.     }
  1482.     return(p);
  1483. }
  1484.  
  1485. /* Call Is Long Distance -- Expand this to cover 10-digit local dialing etc */
  1486. /*
  1487.    src  = area code of caller
  1488.    dest = area code of callee
  1489.    Returns:
  1490.      0 if call is local
  1491.      1 if call is long distance
  1492.      2 if call is local but area code must be dialed anyway
  1493. */
  1494. static int
  1495. callisld(src, dest) char * src, * dest; {
  1496.     int i;
  1497.     if (dialfld)            /* Force long distance? */
  1498.       return(1);
  1499.     if (!strcmp(src,dest)) {        /* Area codes are the same */
  1500.     for (i = 0; i < nlocalac; i++)    /* Is AC in the lc-area-codes list? */
  1501.       if (!strcmp(src,diallcac[i]))
  1502.         return(2);            /* Yes so must be dialed */
  1503.     return(0);            /* No so don't dial it. */
  1504.     }
  1505.     for (i = 0; i < nlocalac; i++)    /* ACs not the same so look in list */
  1506.       if (!strcmp(dest,diallcac[i]))    /* Match */
  1507.     return(2);            /* So local call with area code */
  1508.     return(1);                /* Not local so long-distance */
  1509. }
  1510.  
  1511. char pdsfx[64] = { NUL, NUL };
  1512.  
  1513. #ifndef NOSPL
  1514. static char *
  1515. xdial(s) char *s; {            /* Run dial string thru macro */
  1516.     int x, m;
  1517.     if (!dialmac)            /* Dial macro name given? */
  1518.       return(NULL);
  1519.     if ((x = mxlook(mactab,dialmac,nmac)) < 0) /* Is the macro defined? */
  1520.       return(NULL);
  1521.     m = maclvl;
  1522.     x = dodo(x,s,0);            /* Set up the macro */
  1523.     if (x > 0) {
  1524.     while (maclvl > m)        /* Execute the parser */
  1525.       parser(1);
  1526.     return(mrval[maclvl+1]);    /* Return the result */
  1527.     }
  1528.     return(NULL);
  1529. }
  1530. #endif /* NOSPL */
  1531.  
  1532. static int
  1533. dncvt(k,cx, prefix, suffix)
  1534.     int k, cx, prefix, suffix; {        /* Dial Number Convert */
  1535.     int i, j, n, what;            /* cx is top-level command index */
  1536.     char *ss;                           /* prefix - add prefixes? */
  1537.     char *p, *p2, *pxo;                 /* suffix - add suffixes? */
  1538.     char *lac;
  1539.     char *npr;
  1540.     char *sfx;
  1541.     /* char *psfx; */
  1542.     char ccbuf[128];
  1543.     int cc;
  1544.     char acbuf[24];
  1545.     char *acptr;
  1546.     char outbuf[256];
  1547. /*
  1548.   First pass for strict (punctuation-based) interpretation.
  1549.   If it fails, we try the looser (length-based) one.
  1550. */
  1551.     dialtype = -1;
  1552.     what = 0;                /* Type of call */
  1553.     s = dn_p[k];            /* Number to be converted. */
  1554.     debug(F111,"dncvt",s,k);
  1555.     if (dn_p2[k]) {
  1556.     free(dn_p2[k]);
  1557.     dn_p2[k] = NULL;
  1558.     }
  1559.     if (!s) {
  1560.     printf("Error - No phone number to convert\n");
  1561.     return(-1);
  1562.     }
  1563.     if ((int)strlen(s) > 200) {
  1564.     ckstrncpy(outbuf,s,40);
  1565.     printf("?Too long: \"%s...\"\n",outbuf);
  1566.     return(-1);
  1567.     }
  1568.     npr = (prefix && dialnpr) ? dialnpr : "";
  1569.     sfx = (suffix && dialsfx) ? dialsfx : "";
  1570.     /* if (partial) psfx = dialsfx ? dialsfx : ""; */
  1571.     pxo = (prefix && dialpxo) ? dialpxo : "";
  1572.     lac = diallac ? diallac : "";    /* Local area code */
  1573.  
  1574.     outbuf[0] = NUL;            /* Initialize conversion buffer */
  1575.     ss = s;                /* Remember original string */
  1576.  
  1577.     if (*s != '+') {            /* Literal number */
  1578.     dn_x[k] = DN_UNK;        /* Sort key is "unknown". */
  1579.     ckmakmsg(outbuf,256,        /* Sandwich it between */
  1580.          pxo,npr,s,sfx        /* DIAL PREFIX and SUFFIX */
  1581.         );
  1582. #ifdef CK_TAPI
  1583.     if (tttapi &&            /* TAPI does its own conversions */
  1584.         !tapipass &&        /* if not in passthru mode */
  1585.         tapiconv == CK_AUTO ||    /* and TAPI conversions are AUTO */
  1586.         tapiconv == CK_ON        /* OR if TAPI conversions are ON */
  1587.         ) {
  1588.         char * p = NULL;
  1589.         dialtype = -2;
  1590.         if (!cktapiConvertPhoneNumber(dn_p[k], &p))
  1591.           return(-1);
  1592.         makestr(&dn_p2[k], p);
  1593.         if (p) free(p);
  1594.         return(0);
  1595.     } else
  1596. #endif /* CK_TAPI */
  1597.       makestr(&dn_p2[k], outbuf);    /* Not TAPI */
  1598.     dialtype = what;
  1599.     return(0);            /* Done. */
  1600.     }
  1601.     i = 0;                /* Portable number */
  1602.     s++;                /* Tiptoe past the plus sign */
  1603.     ccbuf[0] = NUL;            /* Do country code first */
  1604.  
  1605.     if (!diallcc) {            /* Do we know our own? */
  1606.     if (cx != XXLOOK)
  1607.       printf("Error - prior SET DIAL COUNTRY-CODE command required\n");
  1608.     return(-1);
  1609.     }
  1610.  
  1611.     /* Parse the number */
  1612.  
  1613.     while (1) {                /* Get the country code */
  1614.         while (*s == HT || *s == SP)
  1615.       s++;
  1616.     if (!s)                /* Not in standard format */
  1617.       break;
  1618.         if (*s == '(') {        /* Beginning of area code  */
  1619.         s++;            /* Skip past parenthesis   */
  1620.         ccbuf[i] = NUL;        /* End of country code */
  1621.         if (!s) {            /* Check for end of string */
  1622.         printf("Error - phone number ends prematurely: \"%s\"\n",ss);
  1623.         return(-1);
  1624.         }
  1625.         break;
  1626.     } else {            /* Collect country code */
  1627.         if (isdigit(*s))
  1628.           ccbuf[i++] = *s;        /* copy this character */
  1629.         s++;
  1630.         if (!*s || i > 127)        /* watch out for memory leak */
  1631.           break;
  1632.     }
  1633.     }
  1634.     cc = atoi(ccbuf);            /* Numeric version of country code */
  1635.  
  1636.     i = 0;                /* Now get area code */
  1637.     acbuf[0] = NUL;            /* Initialize area-code buffer */
  1638.     acptr = acbuf;            /* and pointer. */
  1639.     while (1) {
  1640.         while (*s == HT || *s == SP)    /* Ignore whitespace */
  1641.       s++;
  1642.     if (!s)                /* String finished */
  1643.       break;
  1644.     if (*s == ')') {        /* End of area code  */
  1645.         s++;            /* Skip past parenthesis   */
  1646.         acbuf[i] = NUL;        /* Terminate area-code buffer */
  1647.         break;
  1648.     } else {            /* Part of area code */
  1649.         if (isdigit(*s))        /* If it's a digit, */
  1650.           acbuf[i++] = *s;        /* copy this character */
  1651.         s++;            /* Point to next */
  1652.         if (!*s || i > 23)        /* Watch out for overflow */
  1653.           break;
  1654.     }
  1655.     }
  1656.  
  1657. /*
  1658.    Here we strip any leading 0 for countries that we know have
  1659.    0 as a long-distance prefix and do not have any area codes that
  1660.    start with 0 (formerly also ditto for "9" in Finland...)
  1661. */
  1662.     i = atoi(ccbuf);
  1663.     acptr = chk_ac(i,acbuf);
  1664.  
  1665.     while (*s == HT || *s == SP)    /* Skip whitespace */
  1666.       s++;
  1667.  
  1668. /* printf("S=[%s], ACPTR=[%s]\n",s,acptr); */
  1669.  
  1670.     if (*s && *acptr) {            /* Area code was delimited */
  1671.  
  1672.     while (*s == '-' || *s == '.')    /* Skip past gratuitious punctuation */
  1673.       s++;
  1674.     if (!*s) s--;            /* But not to end of string */
  1675.  
  1676.     if (strcmp(diallcc,ccbuf)) {    /* Out of country? */
  1677.         if (!dialixp) {        /* Need intl-prefix */
  1678.         if (cx != XXLOOK)
  1679.           printf("Error - No international dialing prefix defined\n");
  1680.         return(-1);
  1681.         }
  1682.         what = dn_x[k] = DN_INTL;
  1683.         p  = (prefix && dialixp) ? dialixp : ""; /* Intl-prefix */
  1684.         p2 = (suffix && dialixs) ? dialixs : ""; /* Intl-suffix */
  1685.  
  1686.         /* Form the final phone number */
  1687. #ifdef COMMENT
  1688.         sprintf(pdsfx,"%s%s",p2,sfx); /* UNSAFE */
  1689.         sprintf(outbuf,
  1690.             "%s%s%s%s%s%s%s%s",
  1691.             pxo,npr,p,ccbuf,acptr,s,p2,sfx
  1692.             );
  1693. #else
  1694.         ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1695.         ckmakxmsg(outbuf,256,pxo,npr,p,ccbuf,acptr,s,p2,sfx,
  1696.               NULL,NULL,NULL,NULL);
  1697. #endif /* COMMENT */
  1698.  
  1699.     } else if ((x = callisld(lac,acptr)) >= 1) { /* In-country LD */
  1700.         if (!diallac && cx != XXLOOK) { /* Don't know my own area code */
  1701.         if (cc == 1)
  1702.           printf("WARNING - Prior SET DIAL AREA-CODE needed\n");
  1703.         }
  1704.             if (x == 2) {        /* Local call with area code */
  1705.             what = dn_x[k] = DN_LOCAL;    /* Local-call */
  1706.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  1707.         p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  1708.             } else {
  1709.             what = dn_x[k] = DN_LONG;    /* Long-distance */
  1710.                 for (i = 0; i < ntollfree; i++) { /* But toll-free too? */
  1711.                     if (!strcmp(acptr,dialtfc[i])) {
  1712.                         what = dn_x[k] = DN_FREE;
  1713.                         break;
  1714.                     }
  1715.                 }
  1716.                 if (what == DN_FREE) {    /* Toll-free call */
  1717.                     p = (prefix && dialtfp) ? dialtfp :
  1718.                         ((prefix && dialldp) ? dialldp : "");
  1719.                     p2 = "";        /* no suffix */
  1720.                 } else {            /* normal long distance */
  1721.                     p  = (prefix && dialldp) ? dialldp : ""; /* ld-prefix */
  1722.                     p2 = (suffix && diallds) ? diallds : ""; /* ld-suffix */
  1723.                 }
  1724.             }
  1725.         /* Form the number to be dialed */
  1726. #ifdef COMMENT
  1727.         sprintf(outbuf,"%s%s%s%s%s%s%s",
  1728.             pxo,npr,p,acptr,s,p2,sfx
  1729.             );
  1730.         sprintf(pdsfx,"%s%s",p2,sfx);
  1731. #else
  1732.         ckmakxmsg(outbuf,256,
  1733.               pxo,npr,p,acptr,s,p2,sfx,
  1734.               NULL,NULL,NULL,NULL,NULL);
  1735.         ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1736. #endif /* COMMENT */
  1737.     } else {            /* Same country, same area code */
  1738.         what = dn_x[k] = DN_LOCAL;    /* So it's a local call. */
  1739.         if (!prefix || !(dialpxo || ndialpxx)) { /* Not dialing from PBX */
  1740.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  1741.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  1742. #ifdef COMMENT
  1743.         if (x == 2)
  1744.           sprintf(outbuf,"%s%s%s%s%s%s",npr,p,acptr,s,p2,sfx);
  1745.         else
  1746.           sprintf(outbuf,"%s%s%s%s%s",npr,p,s,p2,sfx);
  1747.         sprintf(pdsfx,"%s%s",p2,sfx);
  1748. #else
  1749.         if (x == 2)
  1750.           ckmakxmsg(outbuf,256,
  1751.                 npr,p,acptr,s,p2,sfx,
  1752.                 NULL,NULL,NULL,NULL,NULL,NULL);
  1753.         else
  1754.           ckmakxmsg(outbuf,256,
  1755.                 npr,p,s,p2,sfx,
  1756.                 NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  1757.         ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1758. #endif /* COMMENT */
  1759.  
  1760.         } else {            /* Dialing from a PBX and not TAPI */
  1761.         if (ndialpxx) {        /* Is it internal? */
  1762. #ifdef COMMENT
  1763.             i = (int) strlen(dialpxx);
  1764.             j = (int) strlen(s);
  1765.             x = -1;
  1766.             if (j > i)
  1767.               x = ckstrcmp(dialpxx,s,i,0);
  1768. #else
  1769.             int kx;
  1770.             x = -1;
  1771.             j = (int) strlen(s);
  1772.             for (kx = 0; kx < ndialpxx; kx++) {
  1773.             i = (int) strlen(dialpxx[kx]);
  1774.             if (j > i)
  1775.               if (!(x = ckstrcmp(dialpxx[kx],s,i,0)))
  1776.                 break;
  1777.             }
  1778. #endif /* COMMENT */
  1779.             if (!x) {
  1780.             char * icp, buf[32];
  1781.             makestr(&matchpxx,dialpxx[kx]);
  1782.             debug(F111,"dncvt matchpxx",matchpxx,kx);
  1783.             what = dn_x[kx] = DN_INTERN;   /* Internal call. */
  1784.             s += i;
  1785.             /* Internal-call prefix */
  1786.             icp = dialpxi;
  1787. #ifndef NOSPL
  1788.             if (icp) {
  1789.                 if (*icp == '\\') {
  1790.                 char c, *bp;
  1791.                 int n;
  1792.                 c = *(icp+1);
  1793.                 if (isupper(c)) c = tolower(c);
  1794.                 if (c == 'v' || c == 'f') {
  1795.                     n = 32;
  1796.                     bp = buf;
  1797.                     zzstring(icp,&bp,&n);
  1798.                     icp = buf;
  1799.                 }
  1800.                 }
  1801.             }
  1802. #endif /* NOSPL */
  1803.             p = (prefix && icp) ? icp : "";
  1804. #ifdef COMMENT
  1805.             sprintf(outbuf,"%s%s%s%s",npr,p,s,sfx);
  1806. #else
  1807.             ckmakmsg(outbuf,256,npr,p,s,sfx);
  1808. #endif /* COMMENT */
  1809.             } else {        /* External local call */
  1810.             /* local-prefix */
  1811.                         p  = (prefix && diallcp) ? diallcp : "";
  1812.             /* local-suffix */
  1813.                         p2 = (prefix && diallcs) ? diallcs : "";
  1814. #ifdef COMMENT
  1815.             if (x == 2)
  1816.               sprintf(outbuf,"%s%s%s%s%s%s%s",
  1817.                   dialpxo ? dialpxo : "",
  1818.                   npr,p,acptr,s,p2,sfx);
  1819.             else
  1820.               sprintf(outbuf,
  1821.                   "%s%s%s%s%s%s",
  1822.                   dialpxo ? dialpxo : "",
  1823.                   npr,p,s,p2,sfx
  1824.                   );
  1825. #else
  1826.             if (x == 2)
  1827.               ckmakxmsg(outbuf, 256,
  1828.                    dialpxo ? dialpxo : "",
  1829.                    npr,p,acptr,s,p2,sfx,
  1830.                    NULL,NULL,NULL,NULL,NULL);
  1831.             else
  1832.               ckmakxmsg(outbuf, 256,
  1833.                     dialpxo ? dialpxo : "",
  1834.                     npr,p,s,p2,sfx,
  1835.                     NULL,NULL,NULL,NULL,NULL,NULL);
  1836. #endif /* COMMENT */
  1837.             }
  1838.         }
  1839.         }
  1840.     }
  1841.  
  1842.     } else {                /* Area code was not delimited */
  1843.  
  1844.     char xbuf[256];            /* Comparison based only on length */
  1845.     char ybuf[256];
  1846.     int x, j;
  1847.  
  1848.     s = ss;
  1849.  
  1850.     for (i = 0; i < 255; i++) {
  1851.         if (!*s) break;
  1852.         while (!isdigit(*s)) {    /* Pay attention only to digits */
  1853.         s++;
  1854.         if (!*s) break;
  1855.         }
  1856.         xbuf[i] = *s++;
  1857.     }
  1858.     xbuf[i] = NUL;
  1859.  
  1860.     x = 1;                /* Assume LD */
  1861.     n = 0;
  1862.     if (!dialfld) {            /* If LD not forced */
  1863.         for (j = 0; j < nlocalac; j++) { /* check local AC list? */
  1864.         ckmakmsg(ybuf,256,diallcc,diallcac[j],NULL,NULL);
  1865.         n = (int) strlen(ybuf);
  1866.         if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) {
  1867.             x = 2;
  1868.             break;
  1869.         }
  1870.         }
  1871.         if (x == 1) {        /* Or exact match with local CC+AC? */
  1872.         ckmakmsg(ybuf,256,diallcc,lac,NULL,NULL);
  1873.         n = (int) strlen(ybuf);
  1874.         if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0))
  1875.           x = 0;
  1876.         }
  1877.     }
  1878.     if (x == 0 || x == 2) {        /* Local call */
  1879.         int xx,kx;            /* Begin 1 Dec 2001... */
  1880.         /* Account for PBX internal calls */
  1881.         if (ndialpxx) {
  1882.         xx = -1;
  1883.         j = (int) strlen(ybuf);
  1884.         for (kx = 0; kx < ndialpxx; kx++) {
  1885.             i = (int) strlen(dialpxx[kx]);
  1886.             if (j >= i)
  1887.               if (!(xx = ckstrcmp(dialpxx[kx],&xbuf[j],i,0)))
  1888.             break;
  1889.         }
  1890.         }
  1891.         if (!xx) {
  1892.         char * icp, buf[32];
  1893.         makestr(&matchpxx,dialpxx[kx]);
  1894.         debug(F111,"dncvt matchpxx",matchpxx,kx);
  1895.         what = dn_x[kx] = DN_INTERN; /* Internal call. */
  1896.         s = xbuf + j + i;
  1897.         icp = dialpxi;        /* Internal-call prefix */
  1898. #ifndef NOSPL
  1899.         if (icp) {
  1900.             if (*icp == '\\') {
  1901.             char c, *bp;
  1902.             int n;
  1903.             c = *(icp+1);
  1904.             if (isupper(c)) c = tolower(c);
  1905.             if (c == 'v' || c == 'f') {
  1906.                 n = 32;
  1907.                 bp = buf;
  1908.                 zzstring(icp,&bp,&n);
  1909.                 icp = buf;
  1910.             }
  1911.             }
  1912.         }
  1913. #endif /* NOSPL */
  1914.         p = (prefix && icp) ? icp : "";
  1915.         ckmakmsg(outbuf,256,npr,p,s,sfx);
  1916.         /* End 1 Dec 2001... */
  1917.  
  1918.         } else {            /* Not PBX internal */
  1919.  
  1920.         dn_x[k] = DN_LOCAL;
  1921.         p = (prefix && diallcp) ? diallcp : "";
  1922.         p2 = (suffix && diallcs) ? diallcs : "";
  1923.         s = (char *) (xbuf + ((x == 0) ? n : (int)strlen(diallcc)));
  1924.         ckmakxmsg(outbuf,256,
  1925.               pxo,npr,p,s,p2,sfx,
  1926.               NULL,NULL,NULL,NULL,NULL,NULL);
  1927.         ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1928.         }
  1929.     } else {            /* Not local */
  1930.         n = ckstrncpy(ybuf,diallcc,256);
  1931.         if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) { /* Long distance */
  1932.         dn_x[k] = DN_LONG;
  1933.         p = (prefix && dialldp) ? dialldp : "";
  1934.         p2 = (suffix && diallds) ? diallds : "";
  1935.         s = xbuf + n;
  1936.         while (*s == '-' || *s == '.')
  1937.           s++;
  1938. #ifdef COMMENT
  1939.         sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,s,p2,sfx);
  1940.         sprintf(pdsfx,"%s%s",p2,sfx);
  1941. #else
  1942.         ckmakxmsg(outbuf,256,
  1943.               pxo,npr,p,s,p2,sfx,
  1944.              NULL,NULL,NULL,NULL,NULL,NULL);
  1945.         ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1946. #endif /* COMMENT */
  1947.         } else {
  1948.         dn_x[k] = DN_INTL;    /* International */
  1949.         if (!dialixp) {
  1950.             if (cx != XXLOOK) {
  1951.             printf(
  1952.               "Error - No international dialing prefix defined\n"
  1953.                    );
  1954.             return(-1);
  1955.             }
  1956.         }
  1957.         p = (prefix && dialixp) ? dialixp : "";
  1958.         p2 = (suffix && dialixs) ? dialixs : "";
  1959. #ifdef COMMENT
  1960.         sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,xbuf,p2,sfx);
  1961.         sprintf(pdsfx,"%s%s",p2,sfx);
  1962. #else
  1963.         ckmakxmsg(outbuf,256,
  1964.               pxo,npr,p,xbuf,p2,sfx,
  1965.               NULL,NULL,NULL,NULL,NULL,NULL);
  1966.         ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1967. #endif /* COMMENT */
  1968.         }
  1969.     }
  1970.     }
  1971. #ifdef CK_TAPI
  1972.     if (tttapi &&            /* TAPI performs the conversions */
  1973.     !tapipass &&
  1974.     tapiconv == CK_AUTO ||
  1975.     tapiconv == CK_ON
  1976.     ) {
  1977.     p = NULL;
  1978.     dialtype = -2;
  1979.     if (!cktapiConvertPhoneNumber(dn_p[k],&p))
  1980.       return(-1);
  1981.     makestr(&dn_p2[k], p);
  1982.     if (p) free(p);
  1983.     return(0);
  1984.     } else {
  1985. #endif /* CK_TAPI */
  1986.     makestr(&dn_p2[k], outbuf);
  1987. #ifdef CK_TAPI
  1988.     }
  1989. #endif /* CK_TAPI */
  1990.     dialtype = what;
  1991.     return(0);
  1992. }
  1993.  
  1994. static int
  1995. ddcvt(s, f, n) char * s; FILE * f; int n; { /* Dial Directory Convert */
  1996.     char linebuf[1024], *s2;        /* Buffers and pointers */
  1997. #ifdef VMS
  1998.     char * temp = NULL;
  1999. #endif /* VMS */
  2000.     char *info[8];            /* Pointers to words from entry */
  2001.     FILE * f2 = NULL;
  2002.     int x, rc;
  2003.     rc = -1;
  2004.  
  2005.     debug(F110,"ddcvt file",s,0);
  2006.  
  2007.     if (!s || !f)            /* No filename or file */
  2008.       return(-1);
  2009.     if (!*s)
  2010.  
  2011.     fclose(f);
  2012.     znewn(s,&s2);            /* s2 = address of static buffer */
  2013.     debug(F110,"ddcvt newname",s2,0);
  2014.  
  2015. #ifdef VMS
  2016.     /* In VMS, znewn() returns the same file name with a new version number */
  2017.     makestr(&temp,s);            /* Swap - otherwise the new */
  2018.     s = s2;                /* version has the older version */
  2019.     s2 = temp;                /* number... */
  2020.     debug(F110,"ddcvt after swap s",s,0);
  2021.     debug(F110,"ddcvt after swap s2",s2,0);
  2022.     makestr(&(dialdir[n]),s);        /* New file gets new version number */
  2023.     debug(F110,"ddcvt after makestr s2",s2,0);
  2024.     debug(F111,"ddcvt dialdir[n]",dialdir[n],n);
  2025. #else
  2026.     if (zrename(s,s2) < 0) {        /* Not VMS - rename old file */
  2027.     perror(s2);            /* to new (wierd) name. */
  2028.     goto ddexit;
  2029.     }
  2030. #endif /* VMS */
  2031.     debug(F110,"ddcvt s2 (old)",s2,0);
  2032.     if ((f = fopen(s2,"r")) == NULL) {    /* Reopen old file with wierd name */
  2033.     debug(F110,"ddcvt s2 open error",ck_errstr(),0);
  2034.     dirline = 0;            /* (or in VMS, old version) */
  2035.     perror(s2);
  2036.     goto ddexit;
  2037.     }
  2038.     debug(F110,"ddcvt fopen(s2) OK",s2,0);
  2039.  
  2040.     debug(F110,"ddcvt s (new)",s,0);
  2041.     if ((f2 = fopen(s,"w")) == NULL) {    /* Create new file with old name */
  2042.     debug(F110,"ddcvt s open error",ck_errstr(),0);
  2043.     perror(s);            /* (or in VMS, new version) */
  2044.     goto ddexit;
  2045.     }
  2046.     debug(F110,"ddcvt fopen(s) OK",s,0);
  2047.  
  2048.     printf("\nSaving old directory as %s.\nConverting %s...",s2,s);
  2049.     fprintf(f2,"; %s - Kermit dialing directory\n", s);
  2050.     fprintf(f2,"%-16s %-20s ; %5s %-6s ; %s\n",
  2051.            "; Name","Number","Speed","Parity","Comment"
  2052.            );
  2053.  
  2054.     while (1) {
  2055.     linebuf[0] = NUL;        /* Read a line */
  2056.     if (fgets(linebuf,1023,f) == NULL)
  2057.       break;
  2058.     debug(F110,"ddcvt linebuf",linebuf,0);
  2059.     if (!linebuf[0]) {        /* Empty line */
  2060.         fprintf(f2,"\n");
  2061.         continue;
  2062.     }
  2063.     x = (int) strlen(linebuf);    /* Strip line terminator, */
  2064.     while (x-- > 0) {        /* if any. */
  2065.         if (linebuf[x] <= SP)
  2066.           linebuf[x] = NUL;
  2067.         else
  2068.           break;
  2069.     }
  2070.     xwords(linebuf,5,info,1);    /* Parse it the old way */
  2071.     for (x = 1; x < 6; x++)
  2072.       if (!info[x]) info[x] = "";
  2073.     fprintf(f2,"%-16s %-20s ; %5s %-6s %s\n",
  2074.            info[1],info[2],info[3],info[4],info[5]
  2075.            );
  2076.     }
  2077.     printf(" OK\n\n");
  2078.     rc = 0;                /* Success */
  2079.   ddexit:
  2080.     if (f) fclose(f);
  2081.     if (f2) fclose(f2);
  2082. #ifdef VMS
  2083.     if (temp) free(temp);
  2084. #endif /* VMS */
  2085.     return(rc);
  2086. }
  2087.  
  2088. int                    /* s = name to look up   */
  2089. #ifdef CK_ANSIC                /* cx = index of command */
  2090. ludial(char *s, int cx)            /* (DIAL, LOOKUP, etc)   */
  2091. #else
  2092. ludial(s, cx) char *s; int cx;
  2093. #endif /* CK_ANSIC */
  2094. /* ludial */ {
  2095.  
  2096.     int dd, n1, n2, n3, i, j, t;    /* Workers */
  2097.     int olddir, newdir, oldentry, newentry;
  2098.     int pass = 0;
  2099.     int oldflg = 0;
  2100.     int ambiguous = 0;            /* Flag for lookup was ambiguous */
  2101.     char *info[7];            /* Pointers to words from entry */
  2102.     char *pp;                /* Pointer to element of array */
  2103.     FILE * f;
  2104.     char *line;                /* File input buffer */
  2105.  
  2106. /* #define LUDEBUG */
  2107.  
  2108. #ifdef LUDEBUG
  2109. int zz = 1;
  2110. #endif /* LUDEBUG */
  2111.  
  2112.     if (!s || ndialdir < 1)        /* Validate arguments */
  2113.       return(-1);
  2114.  
  2115.     if ((n1 = (int) strlen(s)) < 1)    /* Length of string to look up */
  2116.       return(-1);
  2117.  
  2118.     if (!(line = malloc(1024)))        /* Allocate input buffer */
  2119.       return(-1);
  2120.  
  2121. #ifdef LUDEBUG
  2122. if (zz) printf("LUDIAL 1 s[%s], n1=%d\n",s,n1);
  2123. #endif /* LUDEBUG */
  2124.  
  2125.     pass = 0;
  2126.   lu_again:
  2127.     f = NULL;                /* Dial directory file descriptor */
  2128.     t = dncount = 0;            /* Dial-number match count */
  2129.     dd = 0;                /* Directory counter */
  2130.     olddir = 0;
  2131.     newdir = 0;
  2132. /*
  2133.   We need to recognize both old- and new-style directories.
  2134.   But we can't allow old-style and new-style entries in the same
  2135.   directory because there is no way to tell for sure the difference between
  2136.   an old-style entry like this:
  2137.  
  2138.     foo  5551212  9600
  2139.  
  2140.   and a new-style literal entry like this:
  2141.  
  2142.     foo  555 9600
  2143.  
  2144.   I.e. is the "9600" a speed, or part of the phone number?
  2145. */
  2146.     while (1) {                /* We make one pass */
  2147.     if (!f) {            /* Directory not open */
  2148.             if (dd >= ndialdir)        /* No directories left? */
  2149.           break;            /* Done. */
  2150.         debug(F111,"ludial dialdir[dd]",dialdir[dd],dd);
  2151.         if ((f = fopen(dialdir[dd],"r")) == NULL) { /* Open it */
  2152.         perror(dialdir[dd]);    /* Can't, print message saying why */
  2153.         if (line) {
  2154.             free(line);
  2155.             line = NULL;
  2156.         }
  2157.         dd++;            /* Go on to next one, if any... */
  2158.         continue;
  2159.         }
  2160.         dirline = 0;        /* Directory file line number */
  2161.         if (dialdpy && !pass)
  2162.           printf("Opening: %s...\n",dialdir[dd]);
  2163.             dd++;
  2164.         if (!oldflg) olddir = 0;
  2165.         newdir = 0;
  2166.     }
  2167.     oldentry = 0;
  2168.     newentry = 0;
  2169.     line[0] = NUL;
  2170.     if (getnct(line,1023,f,1) < 0) { /* Read a line */
  2171.         if (f) {            /* f can be clobbered! */
  2172.         fclose(f);        /* Close the file */
  2173.         f = NULL;        /* Indicate next one needs opening */
  2174.         oldflg = 0;
  2175.         }
  2176.         continue;
  2177.     }
  2178.     if (!line[0])            /* Empty line */
  2179.       continue;
  2180. #ifdef LUDEBUG
  2181. if (zz) printf("LUDIAL 2 s[%s]\n",s);
  2182. #endif /* LUDEBUG */
  2183.  
  2184.     /* Make a copy and parse it the old way */
  2185.     /* A copy is needed because xwords() pokes NULs into the string */
  2186.  
  2187.     if ((pp = malloc((int)strlen(line) + 1))) {
  2188.         strcpy(pp,line);        /* safe */
  2189.         xwords(pp,5,info,0);    /* Parse it the old way */
  2190.  
  2191. #ifdef LUDEBUG
  2192. if (zz) printf("LUDIAL 3 s[%s]\n",s);
  2193. #endif /* LUDEBUG */
  2194.  
  2195.         if (!info[1])
  2196.           continue;
  2197.         if (*info[1] == ';') {    /* If full-line comment, */
  2198.         newdir = 1;        /* (only new directories have them) */
  2199.         continue;        /* keep reading. */
  2200.         }
  2201.         if (!info[2])
  2202.           continue;
  2203.         if (*info[2] == '+')
  2204.           newentry = 1;
  2205.         if (info[4]) {
  2206.         if ((*info[4] == '=') ||
  2207.             !ckstrcmp(info[4],"none", 4,0) ||
  2208.             !ckstrcmp(info[4],"even", 4,0) ||
  2209.             !ckstrcmp(info[4],"space",5,0) ||
  2210.             !ckstrcmp(info[4],"mark", 4,0) ||
  2211.             !ckstrcmp(info[4],"odd",  3,0)
  2212.             )
  2213.           oldentry = 1;
  2214.         }
  2215.     }
  2216.     if (pp) {
  2217.         free(pp);
  2218.         pp = NULL;
  2219.     }
  2220.  
  2221.     /* Check consistency */
  2222.  
  2223.     if ((oldentry || olddir) && (newentry || newdir)) {
  2224.         printf(
  2225. "\nERROR: You seem to have old- and new-format entries mixed in your\n");
  2226.         printf(
  2227. "dialing directory.  You'll have to edit it by hand to convert it to the\n");
  2228. #ifndef NOHELP
  2229.         printf("new format.  Type HELP DIAL for further information.\n\n");
  2230. #else
  2231.         printf("new format.\n\n");
  2232. #endif /* NOHELP */
  2233.         if (line) {
  2234.         free(line);
  2235.         line = NULL;
  2236.         }
  2237.         return(-1);
  2238.     }
  2239.     if (!olddir && oldentry) {
  2240.         int convert = 0;
  2241.         olddir = 1;
  2242.         if (dialcvt == 2) {        /* 2 == ASK */
  2243.         printf(
  2244. "\nWARNING: Old-style dialing directory detected:\n%s\n\n", line);
  2245.         convert = getyesno("Shall I convert it for you? ",0);
  2246.         } else
  2247.           convert = dialcvt;
  2248.         if (convert) {
  2249.         debug(F111,"ludial calling ddcvt",dialdir[dd-1],dd);
  2250.         if (ddcvt(dialdir[dd-1],f,dd-1) < 0) {
  2251.             debug(F111,"ludial ddcvt failed",dialdir[dd-1],dd);
  2252.             oldflg = 1;
  2253.             printf(
  2254. "  Sorry, can't convert.");
  2255.             printf(
  2256. "  Will ignore speed and parity fields, continuing...\n\n");
  2257.         } else {
  2258.             olddir = newdir = 0;
  2259.             debug(F111,"ludial ddcvt ok",dialdir[dd-1],dd);
  2260.         }
  2261.         dd--;
  2262.         f = NULL;
  2263.         continue;
  2264.         } else {
  2265.         if (dialcvt == 2)
  2266.           printf(
  2267. "  OK, will ignore speed and parity fields, continuing...\n\n");
  2268.         olddir = 1;
  2269.         }
  2270.     }
  2271.  
  2272. #ifdef LUDEBUG
  2273. if (zz) printf("LUDIAL XX s[%s], n1=%d\n",s,n1);
  2274. #endif /* LUDEBUG */
  2275.  
  2276.     /* Now parse again for real */
  2277.  
  2278.     if (oldentry)            /* Parse it the old way */
  2279.       xwords(line,5,info,0);
  2280.     else                /* Parse it the new way */
  2281.       xwords(line,2,info,1);
  2282.  
  2283. #ifdef LUDEBUG
  2284. if (zz) printf("LUDIAL YY s[%s], n1=%d\n",s,n1);
  2285. if (zz) printf("%s [%s]\n",info[1],info[2]);
  2286. #endif /* LUDEBUG */
  2287.  
  2288.     if (info[1]) {            /* First word is entry name */
  2289.         if ((n3 = (int) strlen(info[1])) < 1) /* Its length */
  2290.           continue;            /* If no first word, keep reading. */
  2291.         if (n3 < n1)        /* Search name is longer */
  2292.           continue;            /* Can't possibly match */
  2293.         if (ambiguous && n3 != n1)
  2294.           continue;
  2295.  
  2296. #ifdef LUDEBUG
  2297. if (zz) printf("MATCHING: [%s] [%s], n1=%d\n",s,info[1],n1);
  2298. #endif /* LUDEBUG */
  2299.  
  2300.         if (ckstrcmp(s,info[1],n1,0)) /* Caseless string comparison */
  2301.           continue;
  2302.  
  2303. #ifdef LUDEBUG
  2304. if (zz) printf("MATCH OK: [%s] [%s], n1=%d\n",s,info[1],n1);
  2305. #endif /* LUDEBUG */
  2306.  
  2307.         if (!info[2])        /* No phone number given */
  2308.           continue;
  2309.         if ((n2 = (int) strlen(info[2])) < 1) /* Length of phone number */
  2310.           continue;            /* Ignore empty phone numbers */
  2311.  
  2312.         /* Got one */
  2313.  
  2314.         if (!(pp = (char *)malloc(n2 + 1))) { /* Allocate storage for it */
  2315.         printf("?internal error - ludial malloc 1\n");
  2316.         if (line) {
  2317.             free(line);
  2318.             line = NULL;
  2319.         }
  2320.         dncount = 0;
  2321.         return(-1);
  2322.         }
  2323.         strcpy(pp,info[2]);        /* safe */
  2324.  
  2325.         if (dncount > MAXDNUMS) {
  2326.         printf("Warning: %d matches found, %d max\n",
  2327.                dncount,
  2328.                MAXDNUMS
  2329.                );
  2330.         dncount = MAXDNUMS;
  2331.         break;
  2332.         }
  2333.         dn_p[dncount++] = pp;    /* Add pointer to array. */
  2334.         if (dncount == 1) {        /* First one... */
  2335.         if (d_name) free(d_name);
  2336.         if (!(d_name = (char *)malloc(n3 + 1))) { /* Save its name */
  2337.             printf("?internal error - ludial malloc 2\n");
  2338.             if (line) {
  2339.             free(line);
  2340.             line = NULL;
  2341.             }
  2342.             dncount = 0;
  2343.             return(-1);
  2344.         }
  2345.         t = n3;            /* And its length */
  2346.         strcpy(d_name,info[1]);    /* safe */
  2347.         } else {            /* Second or subsequent one */
  2348.  
  2349. #ifdef LUDEBUG
  2350.         if (zz)
  2351.           printf("d_name=[%s],info[1]=%s,t=[%d]\n",d_name,info[1],t);
  2352. #endif /* LUDEBUG */
  2353.  
  2354.         if ((int) strlen(info[1]) == t) /* Lengths compare */
  2355.           if (!ckstrcmp(d_name,info[1],t,0)) /* Caseless compare OK */
  2356.             continue;
  2357.  
  2358.         /* Name given by user matches entries with different names */
  2359.  
  2360.         if (ambiguous)        /* Been here before */
  2361.           break;
  2362.  
  2363.         ambiguous = 1;        /* Now an exact match is required */
  2364.         for (j = 0; j < dncount; j++) { /* Clean out previous list */
  2365.             if (dn_p[j]) {
  2366.             free(dn_p[j]);
  2367.             dn_p[j] = NULL;
  2368.             }
  2369.         }
  2370.         pass++;            /* Second pass... */
  2371.         goto lu_again;        /* Do it all over again. */
  2372.         }
  2373.     }
  2374.     }
  2375.     if (line) free(line);
  2376.     if (dncount == 0 && ambiguous) {
  2377.     printf(" Lookup: \"%s\" - ambiguous%s\n",
  2378.            s,
  2379.            cx == XXLOOK ? "" : " - dialing skipped"
  2380.            );
  2381.     return(-2);
  2382.     }
  2383.     return(dncount);
  2384. }
  2385.  
  2386. char *
  2387. pncvt(s) char *s; {            /* Phone number conversion */
  2388.     char *p = NULL;            /* (just a wrapper for dncvt() */
  2389.     char *q = NULL;
  2390.     static char pnbuf[128];
  2391.     makestr(&p,dn_p[0]);        /* Save these in case they are */
  2392.     makestr(&q,dn_p2[0]);        /* being used */
  2393.     makestr(&dn_p[0],s);        /* Copy the argument string to here */
  2394.     dncvt(0,XXLOOK,1,1);        /* Convert it */
  2395.     if (!dn_p2[0])            /* Put result where can return it */
  2396.       pnbuf[0] = NUL;
  2397.     else
  2398.       ckstrncpy(pnbuf,dn_p2[0],127);
  2399.     makestr(&dn_p[0],p);        /* Restore these */
  2400.     makestr(&dn_p2[0],q);
  2401.     makestr(&p,NULL);            /* Free these */
  2402.     makestr(&q,NULL);
  2403.     return((char *)pnbuf);
  2404. }
  2405.  
  2406. int
  2407. dodial(cx) int cx; {            /* DIAL or REDIAL */
  2408.     int i = 0, x = 0;            /* Workers */
  2409.     int sparity = -1;            /* For saving global parity value */
  2410.     int previous = 0;
  2411.     int len = 0;
  2412.     int literal = 0;
  2413.     int flowsave;
  2414.     int lufound = 0;            /* Did any lookup succeed? */
  2415.     int prefix = 1;
  2416.     int postfix = 1;
  2417.     int wasalpha = 0;
  2418.     int xredial = 0;
  2419.     int braces = 0;
  2420.  
  2421.     char *p = NULL, *s3 = NULL, * sav = NULL;
  2422.     int j = 0, t = 0, n = 0;
  2423.     int xretries, xlcc;
  2424.  
  2425.     debug(F101,"dodial cx","",cx);
  2426.     debug(F111,"dodial diallcc",diallcc,diallcc);
  2427.  
  2428.     xretries = dialrtr;            /* If retries not set, */
  2429.     if (diallcc) {            /* choose default based on */
  2430.         xlcc = atoi(diallcc);        /* local country code. */
  2431.     if (xretries < 0) {
  2432.         switch (xlcc) {
  2433.           case 1: xretries = 10; break; /* No restrictions in NANP */
  2434.         /* Add other country codes here */
  2435.         /* that are known to have no restrictions on redialing. */
  2436.           default: xretries = 1;
  2437.         }
  2438.     }
  2439.     }
  2440.     if (cx == XXPDIA) {            /* Shortcut... */
  2441.     cx = XXDIAL;
  2442.     partial = 1;
  2443.     debug(F100,"PDIAL sets partial=1","",0);
  2444.     postfix = 0;                    /* Do not add postfix */
  2445.     } else {
  2446.     partial = 0;
  2447.     debug(F100,"DIAL sets partial=0","",0);
  2448.     }
  2449.     previous = dialsta;            /* Status of previous call, if any */
  2450.     if (previous == DIA_PART) {
  2451.     prefix = 0;            /* do not add prefix */
  2452.     }
  2453.     s = NULL;                /* Initialize user's dial string */
  2454.     if (cx == XXRED) {            /* REDIAL or... */
  2455.     if ((y = cmcfm()) < 0)
  2456.       return(y);
  2457.     } else if (cx == XXANSW) {        /* ANSWER or ... */
  2458.     if ((y = cmnum("timeout (seconds)","0",10,&x,xxstring)) < 0)
  2459.       return(y);
  2460.     dialatmo = x;
  2461.     if ((y = cmcfm()) < 0)
  2462.       return(y);
  2463.     } else {                /* DIAL or LOOKUP */
  2464.     if (ndialdir > 0)
  2465.       s3 = "Number to dial or entry from dial directory";
  2466.     else
  2467.       s3 = "Number to dial";
  2468.     if ((x = cmtxt(s3, dialnum ? dialnum : "",&s,xxstring)) < 0)
  2469.       return(x);
  2470.     if (s) {
  2471.         len = (int) strlen(s);
  2472.         ckstrncpy(tmpbuf,s,TMPBUFSIZ); /* Save literal copy */
  2473. #ifdef COMMENT
  2474.         if (len > 1) {        /* Strip outer braces if given */
  2475.         if (*s == '{') {
  2476.             if (s[len-1] == '}') {
  2477.             s[len-1] = NUL;
  2478.             s++;
  2479.             len -= 2;
  2480.             }
  2481.         }
  2482.         }
  2483. #else
  2484.         s = brstrip(s);        /* Strip outer braces or quotes */
  2485. #endif /* COMMENT */
  2486.     }
  2487.     }
  2488.  
  2489.     if (cx != XXLOOK) {            /* Not LOOKUP */
  2490. #ifdef IKSD
  2491.         if (inserver) {
  2492.             printf("Sorry, dialing is disabled.\r\n");
  2493.             return(success = 0);
  2494.         }
  2495. #endif /* IKSD */
  2496. #ifdef CK_TAPI
  2497.     if (tttapi && !tapipass) {
  2498.       ;                 /* Skip the modem test if TAPI */
  2499.     } else
  2500. #endif /* CK_TAPI */
  2501.     if (mdmtyp < 1 && !dialtest) {
  2502.         if (network)
  2503.           printf("Please SET HOST first, and then SET MODEM TYPE\n");
  2504.         else
  2505.           printf("Sorry, you must SET MODEM TYPE first\n");
  2506.         dialsta = DIA_NOMO;
  2507.         return(success = 0);
  2508.     }
  2509.     if (!local && !dialtest) {
  2510.         printf("Sorry, you must SET %s or SET HOST first\n",
  2511. #ifdef OS2
  2512.            "PORT"
  2513. #else
  2514.            "LINE"
  2515. #endif /* OS2 */
  2516.            );
  2517.         dialsta = DIA_NOLI;
  2518.         return(success = 0);
  2519.     }
  2520.     if (!network && !dialtest &&
  2521. #ifdef CK_TAPI
  2522.          !tttapi &&
  2523. #endif /* CK_TAPI */
  2524.         (speed < 0L)
  2525. #ifdef UNIX
  2526.         && (strcmp(ttname,"/dev/null"))
  2527. #else
  2528. #ifdef OSK
  2529.         && (strcmp(ttname,"/nil"))
  2530. #endif /* OSK */
  2531. #endif /* UNIX */
  2532.         ) {
  2533.         printf("\nSorry, you must SET SPEED first\n");
  2534.         dialsta = DIA_NOSP;
  2535.         return(success = 0);
  2536.     }
  2537.     }
  2538.     if (cx != XXANSW) {
  2539.     for (j = 0; j < MAXDNUMS; j++) { /* Initialize dial-number list */
  2540.         if (!dialnum) {        /* First time dialing */
  2541.         dn_p[j] = NULL;        /* initialize all pointers. */
  2542.         dn_p2[j] = NULL;
  2543.         } else if (dn_p[j]) {    /* Not the first time, */
  2544.         free(dn_p[j]);        /* free previous, if any, */
  2545.         dn_p[j] = NULL;        /* then set to NULL. */
  2546.         if (dn_p2[j])
  2547.           free(dn_p2[j]);
  2548.         dn_p2[j] = NULL;
  2549.         } else break;        /* Already NULL */
  2550.     }
  2551.     if (len == 0)
  2552.       s = NULL;
  2553.     if (!s)
  2554.       s = dialnum;
  2555.     if (!s) {
  2556.         if (cx == XXLOOK)
  2557.           printf("?Lookup what?\n");
  2558.         else
  2559.           printf("%s\n", (cx == XXRED) ?
  2560.            "?No DIAL command given yet" :
  2561.            "?You must specify a number to dial"
  2562.            );
  2563.         return(-9);
  2564.     }
  2565.  
  2566.     /* Now we have the "raw" dial or lookup string and s is not NULL */
  2567.  
  2568.     makestr(&dscopy,s);        /* Put it in a safe place */
  2569.     s = dscopy;
  2570.     n = 0;
  2571.  
  2572.     debug(F111,"dodial",s,ndialdir);
  2573.  
  2574.     wasalpha = 0;
  2575.     if (isalpha(*s)) {
  2576.         wasalpha = 1;
  2577.         if (ndialdir > 0) {        /* Do we have a dialing directory? */
  2578.         n = ludial(s,cx);    /* Look up what the user typed */
  2579.         if (n == 0)
  2580.           printf(" Lookup: \"%s\" - not found%s\n",
  2581.              s,
  2582.              cx == XXLOOK ? "" : " - dialing as given\n"
  2583.              );
  2584.         }
  2585.         debug(F101,"dodial",s,n);
  2586.         if (n < 0 && cx != XXLOOK) { /* Error out if they wanted to dial */
  2587.         if (n == -1)        /* -2 means ludial already gave msg */
  2588.           printf(" Lookup: fatal error - dialing skipped\n");
  2589.         dialsta = DIA_DIR;
  2590.         return(-9);
  2591.         }
  2592.         if (n > 0)            /* A successful lookup */
  2593.           lufound = 1;
  2594.     } else if (*s == '=') {        /* If number starts with = sign */
  2595.         s++;            /* strip it */
  2596.         literal = 1;        /* remember this */
  2597.         while (*s == SP) s++;    /* and then also any leading spaces */
  2598.     } else if (tmpbuf[0] == '{' && tmpbuf[1] == '{') {
  2599.         makelist(tmpbuf,dn_p,MAXDNUMS);
  2600.         makestr(&dscopy,tmpbuf);
  2601.         s = tmpbuf;
  2602.         for (n = 0; n < MAXDNUMS; n++) /* (have to count how many) */
  2603.           if (!dn_p[n]) break;
  2604.         braces = 1;
  2605.     }
  2606.     if (cx == XXLOOK && !wasalpha && !braces) {
  2607.         /* We've been told to lookup a number or a quoted name */
  2608.         char *p;
  2609.         n = 0;
  2610.         p = literal ? s : pncvt(dscopy);
  2611.         if (!p) p = "";
  2612.         if (*p) {
  2613.         printf("%s  => %s\n", dscopy, p);
  2614.         return(success = 1);
  2615.         } else {
  2616.         printf("?Bad phone number\n");
  2617.         return(success = 0);
  2618.         }
  2619.     }
  2620.     /* Save DIAL or successful LOOKUP string for future DIAL or REDIAL */
  2621.     /* But don't save pieces of partial dial ... */
  2622.  
  2623.     debug(F101,"DIAL save dialnum partial","",partial);
  2624.     debug(F101,"DIAL save dialnum previous","",previous);
  2625.     if ((cx == XXDIAL && partial == 0 && previous != DIA_PART) ||
  2626.         (cx == XXLOOK && n > 0)) {
  2627.         makestr(&dialnum,dscopy);
  2628.         if (!quiet && dscopy && !dialnum)
  2629.           printf("WARNING - memory allocation failure: redial number\n");
  2630.     }
  2631.     if (n > 0) {
  2632.         if (!quiet && !backgrd && !braces /* && dialdpy */ ) {
  2633.         if (!strcmp(d_name,s))
  2634.           printf(" Lookup: \"%s\" - exact match\n",s);
  2635.         else
  2636.           printf(" Lookup: \"%s\" - uniquely matches \"%s\"\n",
  2637.              s,
  2638.              d_name
  2639.              );
  2640.         }
  2641.         if ((cx == XXLOOK) ||
  2642.         ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2643.         printf(" %d telephone number%sfound for \"%s\"%s\n",
  2644.                n,
  2645.                (n == 1) ? " " : "s ",
  2646.                s,
  2647.                (n > 0) ? ":" : "."
  2648.                );
  2649.         s3 = getdname();
  2650.         }
  2651.         for (i = 0; i < n; i++) {    /* Convert */
  2652.         dn_x[i] = -1;
  2653.         if (dncvt(i,cx,prefix,postfix) < 0) {
  2654.             if (cx != XXLOOK) {
  2655.             dialsta = DIA_DIR;
  2656.             return(-9);
  2657.             }
  2658.         }
  2659.         }
  2660.         if (dialsrt && n > 1) {    /* Sort into optimal order */
  2661.         for (i = 0; i < n-1; i++) {
  2662.             for (j = i+1; j < n; j++) {
  2663.             if (dn_x[j] < dn_x[i]) {
  2664.                 t = dn_x[j];
  2665.                 dn_x[j] = dn_x[i];
  2666.                 dn_x[i] = t;
  2667.                 p = dn_p[j];
  2668.                 dn_p[j] = dn_p[i];
  2669.                 dn_p[i] = p;
  2670.                 p = dn_p2[j];
  2671.                 dn_p2[j] = dn_p2[i];
  2672.                 dn_p2[i] = p;
  2673.             }
  2674.             }
  2675.         }
  2676.         }
  2677.         if ((cx == XXLOOK) ||
  2678.         ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2679.         int nn = n;
  2680. #ifndef NOSPL
  2681.         char * p;
  2682. #endif /* NOSPL */
  2683.         if (cx != XXLOOK)
  2684.           if (n > 12) nn = 12;
  2685.         for (i = 0; i < nn; i++) {
  2686.             printf("%3d. %-12s  %-20s =>  %-20s  (%d)\n",i+1,
  2687.                s3, dn_p[i],
  2688.                dn_p2[i] ? dn_p2[i] : "(processing failed)",
  2689.                dn_x[i]
  2690.                );
  2691.         }
  2692.         if (cx != XXLOOK && n != nn)
  2693.           printf("And %d more...\n", n - nn);
  2694.         }
  2695.     } else if (n == 0) {        /* Not found in directory */
  2696.         makestr(&(dn_p[0]),literal ? s : dscopy);
  2697.         makestr(&d_name,literal ? s : dscopy);
  2698.         dncount = 1;
  2699.         n = 1;
  2700.         if (dncvt(0,cx,prefix,postfix) < 0) { /* In case they typed a */
  2701.         dialsta = DIA_DIR;    /* portable-format number ... */
  2702.         return(-9);
  2703.         }
  2704.     }
  2705.  
  2706. #ifndef NONET
  2707. #ifdef NETCONN
  2708.     /* It's not good that the networks directory depends on NOT-NODIAL.. */
  2709.     if (cx == XXLOOK && dscopy) {    /* Networks here too... */
  2710.         extern char *nh_p[], *nh_p2[], *n_name;
  2711.         extern char *nh_px[4][MAXDNUMS+1];
  2712.         n = -1;
  2713.         if (nnetdir > 0) {        /* Do we have a network directory? */
  2714.         dirline = 0;
  2715.         n = lunet(dscopy);    /* Look up what the user typed */
  2716.         }
  2717.         if (n > -1) {
  2718.         int k;
  2719.         if (n > 0)        /* A successful lookup */
  2720.           lufound = 1;
  2721.         if (cx == XXLOOK && n == 0)
  2722.           printf(" Lookup: \"%s\" - not found\n",dscopy);
  2723.         else
  2724.           printf("%s %d network entr%s found for \"%s\"%s\n",
  2725.              cx == XXLOOK ? " Lookup:" : "",
  2726.              n,
  2727.              (n == 1) ? "y" : "ies",
  2728.              dscopy,
  2729.              (n > 0) ? ":" : "."
  2730.              );
  2731.  
  2732.         for (i = 0; i < n; i++) {
  2733.  
  2734.             printf("%3d. %-12s => %-9s %s",
  2735.                i+1,n_name,nh_p2[i],nh_p[i]);
  2736.             for (k = 0; k < 4; k++) {
  2737.             if (nh_px[k][i]) {
  2738.                 printf(" %s",nh_px[k][i]);
  2739.             } else
  2740.               break;
  2741.             }
  2742.             printf("\n");
  2743.         }
  2744.         }
  2745.     }
  2746. #endif /* NETCONN */
  2747. #endif /* NONET */
  2748.     if (cx == XXLOOK)
  2749.       return(success = lufound);
  2750.     } /* cx != XXANSW */
  2751.  
  2752. #ifdef VMS
  2753.     conres();            /* So Ctrl-C/Y will work */
  2754. #endif /* VMS */
  2755. /*
  2756.   Some modems do not react well to parity.  Also, if we are dialing through a
  2757.   TCP/IP TELNET modem server, parity can be fatally misinterpreted as TELNET
  2758.   negotiations.
  2759.  
  2760.   This should work even if the user interrupts the DIAL command, because the
  2761.   DIAL module has its own interrupt handler.  BUT... if, for some reason, a
  2762.   dialing device actually *requires* parity (e.g. CCITT V.25bis says that even
  2763.   parity should be used), this might prevent successful dialing.  For that
  2764.   reason, we don't do this for V.25bis modems.
  2765. */
  2766.     sparity = parity;            /* Save current parity */
  2767.     if ((dialcapas & CKD_V25) == 0)    /* If not V.25bis...  */
  2768.       parity = 0;            /* Set parity to NONE */
  2769.  
  2770.     flowsave = flow;
  2771. /*
  2772.   These modems use some kind of screwy flow control while in command mode,
  2773.   and do not present CTS as they should.  So if RTS/CTS is set (or even if
  2774.   it isn't) disable flow control during dialing.
  2775. */
  2776. #ifndef MINIDIAL
  2777.     if (mdmtyp == n_ATT1910 || mdmtyp == n_ATT1900) {
  2778.     flow = FLO_NONE;        /* This is not enough */
  2779. #ifdef CK_TTSETFLOW
  2780.         ttsetflow(FLO_NONE);        /* Really turn it off */
  2781. #endif /* CK_TTSETFLOW */
  2782.     }
  2783. #endif /* MINIDIAL */
  2784.     if (!network) {
  2785.     int x;
  2786.     if ((x = ttgmdm()) > -1) {
  2787.         if (!x && msgflg) {
  2788.         printf(
  2789. "WARNING - No modem signals detected.  Is your modem turned on?  If not,\n\
  2790. use Ctrl-C to interrupt dialing, turn on your modem, then %s.\n",
  2791.                cx == XXANSW ?
  2792.                "ANSWER again" :
  2793.                "REDIAL"
  2794.                );
  2795.         }
  2796.         if (flow == FLO_RTSC) {
  2797.         if (!(x & BM_CTS)) {
  2798.             if (msgflg)
  2799.               printf(
  2800. "WARNING - SET FLOW RTS/CTS is in effect but modem's CTS signal is off.\n\
  2801. Disabling flow control temporarily %s...\n",
  2802.                  cx == XXANSW ?
  2803.                  "while waiting for call" :
  2804.                  "during dialing"
  2805.                  );
  2806.             flow = FLO_NONE;
  2807.         }
  2808.         }
  2809.     }
  2810.     }
  2811.     if (cx == XXANSW) {            /* ANSWER */
  2812.     success = ckdial("",0,0,1,0);
  2813.     goto dialfin;
  2814.     }
  2815.  
  2816. /* Edit 192 adds the ability to dial repeatedly. */
  2817.  
  2818.     i = 0;
  2819.     dialcount = 0;
  2820.     do {
  2821.     if (i > 0) printf("\nDial attempt %d of %d...\n", i+1, xretries);
  2822.     dialcount = i+1;
  2823.     success = 0;
  2824.     /* And the ability to dial alternate numbers. */
  2825.     /* Loop to dial each in a list of numbers for the same name... */
  2826.     for (j = 0; j < n && !success; j++) { /* until one answers. */
  2827.         s = dn_p2[j];        /* Next number in list */
  2828.         if (dn_x[j] >= dialrstr) {    /* Dial restriction */
  2829.         printf("Restricted: %s, skipping...\n",dn_p[j]);
  2830.         continue;
  2831.         }
  2832.         xredial = (i == 0 && j == 0) ? 0 : 1;
  2833.         if (!s) s = dn_p[j];
  2834.  
  2835. #ifndef NOSPL
  2836.         sav = s;
  2837.         p = xdial(s);        /* Apply DIAL macro now */
  2838.         if (p) s = p;
  2839. #endif /* NOSPL */
  2840.  
  2841.         if (i == 0 && dialcnf) {
  2842.         printf("Dialing %s\n",s);
  2843.         x = getyesno(" Is this number correct? ",0);
  2844.         if (!x) {
  2845. #ifdef CK_RECALL
  2846.             extern int on_recall;
  2847. #endif /* CK_RECALL */
  2848.             cmsavp(psave,PROMPTL);
  2849.             cmsetp(
  2850. #ifdef OS2
  2851. " Please enter the correct number,\r\n or press Enter to skip: "
  2852. #else
  2853. " Please enter the correct number,\r\n or press Return to skip: "
  2854. #endif /* OS2 */
  2855.                );
  2856.             cmini(ckxech);
  2857.             x = -1;
  2858.             if (pflag) prompt(NULL);
  2859. #ifdef CK_RECALL
  2860.             on_recall = 0;
  2861. #endif /* CK_RECALL */
  2862.             y = cmdgquo();
  2863.             cmdsquo(0);
  2864.             while (x < 0) {
  2865.             x = cmtxt("Corrected phone number","",&s,NULL);
  2866.             cmres();
  2867.             }
  2868.             if ((int) strlen(s) < 1) {
  2869.             cmsetp(psave);
  2870.             continue;
  2871.             }
  2872.             makestr(&(dn_p2[j]), s);
  2873.             cmdsquo(y);
  2874.             cmsetp(psave);
  2875.         }
  2876.         }
  2877.         if (dialtest) {        /* Just testing */
  2878.         if (i + j == 0)
  2879.           printf("\nTESTING...\n");
  2880.         if (dialmac)
  2881.           printf(" Number: \"%s\" => \"%s\"\n",sav,s);
  2882.         else
  2883.           printf(" Number: \"%s\"\n",s);
  2884.         dialsta = DIA_BUSY;
  2885.         success = 0;
  2886.         } else {
  2887.         what |= W_DIALING;
  2888.         success = ckdial(s,i,j,partial ? 3 : 0, xredial); /* Dial it */
  2889.         what &= ~(W_DIALING);
  2890.         if (!success) {
  2891.             if (dialsta < 8 ||    /* Break out if unrecoverable error */
  2892.             dialsta  == DIA_INTR ||
  2893.             dialsta  == DIA_ERR  ||
  2894.             previous == DIA_PART
  2895.             )
  2896.               break;
  2897.         }
  2898.         }
  2899.     }
  2900.     if (success)            /* Succeeded, leave the outer loop */
  2901.       break;
  2902.     if (dialsta < 8 ||        /* Break out if unrecoverable error */
  2903.         dialsta == DIA_INTR ||    /* Interrupted */
  2904.         dialsta == DIA_NODT ||    /* No dialtone */
  2905.         dialsta == DIA_NOAC ||    /* Access forbidden */
  2906.         dialsta == DIA_BLCK ||    /* Blacklisted */
  2907.         dialsta == DIA_DIR  ||    /* Dialing directory error */
  2908.         dialsta == DIA_ERR  ||    /* Modem command error */
  2909.         previous == DIA_PART)
  2910.       break;
  2911.     if (++i >= xretries)        /* Break out if too many tries */
  2912.       break;
  2913.     if (!backgrd && !quiet) {
  2914.         if (dialint > 5)
  2915.           printf(
  2916. "\nWill redial in %d second%s- press any key to redial immediately.\n",
  2917.              dialint,
  2918.              dialint == 1 ? " " : "s "
  2919.              );
  2920.         printf("Ctrl-C to cancel...\n");
  2921.     }
  2922.     x = dialint;            /* Redial interval */
  2923.     while (x-- > 0) {
  2924.         if ((y = conchk()) > 0) {    /* Did they type something? */
  2925.         while (y--) coninc(0);    /* Yes, absorb it */
  2926.         break;            /* And wake up */
  2927.         }
  2928.         sleep(1);            /* No interrupt, sleep a sec */
  2929.     }
  2930.     } while (!success);
  2931.  
  2932.   dialfin:
  2933.  
  2934.     if (cx != XXLOOK) {
  2935.     if (!success)
  2936.       bleep((short) BP_FAIL);
  2937.     else if (!quiet)
  2938.       bleep((short) BP_NOTE);
  2939. #ifdef OS2
  2940.     setint();            /* Fix OS/2 interrupts */
  2941. #endif /* OS2 */
  2942.     if (sparity > -1)
  2943.       parity = sparity;        /* Restore parity if we saved it */
  2944.     flow = flowsave;
  2945. #ifdef OS2
  2946.     ttres();            /* Restore DIAL device */
  2947. #endif /* OS2 */
  2948. #ifdef VMS
  2949.     concb((char)escape);        /* Restore console */
  2950. #endif /* VMS */
  2951. #ifdef OS2
  2952.     {                /* Set session title */
  2953.         char * p, name[72];        /* in window list. */
  2954.         char * q;
  2955.         if (cx == XXANSW) {
  2956.         q = "Incoming call";
  2957.         } else {
  2958.         if (d_name)
  2959.           q = d_name;
  2960.         else if (dialnum)
  2961.           q = dialnum;
  2962.         else if (ttname[0])
  2963.           q = ttname;
  2964.         else q = "";
  2965.         }
  2966.         p = name;
  2967.         if (success) {
  2968.         strncpy(name,q,48);
  2969.         while (*p) {        /* Uppercase it for emphasis. */
  2970.             if (islower(*p))
  2971.               *p = toupper(*p);
  2972.             p++;
  2973.         }
  2974.         } else
  2975.           name[0] = NUL ;
  2976.         os2settitle((char *) name, TRUE);
  2977.     }
  2978. #endif /* OS2 */
  2979.     }
  2980.     if (cx != XXLOOK) {
  2981.     if (success) {
  2982.         if (reliable == SET_AUTO) {    /* It's not a reliable connection. */
  2983.         reliable = SET_OFF;
  2984.         debug(F101,"dodial reliable","",reliable);
  2985.         }
  2986.     } else {
  2987. #ifndef NOHINTS
  2988.         extern int hints;
  2989.         if (hints && !quiet && dialsta != 9) { /* 9 == User interrupted */
  2990.         extern int dialmhu, dialhng, dialdpy;
  2991.         extern char * dialmsg[];
  2992.         printf("\n*************************\n");
  2993.         printf("DIAL-class command failed.\n");
  2994.         printf("Modem type:  %s\n", gmdmtyp());
  2995.         printf("Device:      %s\n", ttname);
  2996.         printf("Speed:       %ld\n", speed);
  2997.         printf("Dial status: %d",dialsta);
  2998.         if (dialsta < 35 && dialmsg[dialsta])
  2999.           printf(" [%s]",dialmsg[dialsta]);
  3000.         printf("\n");
  3001.         if (dialsta == DIA_TIMO ||
  3002.             dialsta == DIA_NRDY ||
  3003.            (dialsta > 13 && dialsta != DIA_BUSY && dialsta != DIA_NOAN)
  3004.             ) {
  3005.             switch (dialsta) {
  3006.               case DIA_TIMO:
  3007.             printf(
  3008. " . SET DIAL TIMEOUT to a greater value and try again.\n"
  3009.                    );
  3010.             break;
  3011.               case DIA_NRSP:
  3012.               case DIA_NRDY:
  3013.               case DIA_NOIN:
  3014.             printf(
  3015. " . Is the modem turned on?\n"
  3016.                    );
  3017.             printf(
  3018. " . Are you using the right communication port?\n"
  3019.                    );
  3020.             break;
  3021.               case DIA_NODT:
  3022.             printf(
  3023. " . Is the modem connected to the telephone line?\n"
  3024.                    );
  3025.             }
  3026.             if (mdmtyp == n_GENERIC) {
  3027.             printf(
  3028. " . Please choose a specific modem type with SET MODEM TYPE and try again.\n"
  3029.                    );
  3030.             printf(
  3031. "    SET MODEM TYPE ? to see the list of known modem types.\n"
  3032.                    );
  3033.             } else {
  3034.             printf(
  3035. " . Are you sure you have chosen the appropriate modem type?\n"
  3036.                    );
  3037.             }
  3038.             if (speed > 19200L) {
  3039.             printf(
  3040. " . Maybe the interface speed (%ld) is too fast:\n", speed
  3041.                    );
  3042.             printf(
  3043. "    SET SPEED to a lower speed and try again.\n"
  3044.                    );
  3045.             printf(
  3046. "    SET SPEED ? to see the list of valid speeds.\n"
  3047.                    );
  3048.             }
  3049.             if (dialhng) {
  3050.             if (dialmhu)
  3051.               printf(
  3052. " . SET MODEM HANGUP-METHOD RS232 and try again.\n"
  3053.                  );
  3054.             else
  3055.               printf(
  3056. " . SET MODEM HANGUP-METHOD MODEM-COMMAND and try again.\n"
  3057.                  );
  3058.             printf(
  3059. " . If that doesn't work, try again with SET DIAL HANGUP OFF.\n"
  3060.                    );
  3061.             } else {
  3062.             printf(
  3063. " . Give a HANGUP or SET DIAL HANGUP ON command and try again.\n"
  3064.                    );
  3065.             }
  3066.             if (!dialdpy)
  3067.               printf(
  3068. " . Use SET DIAL DISPLAY ON to watch the dialog between Kermit and modem.\n"
  3069.                  );
  3070.         }
  3071. #ifndef NOSHOW
  3072.         printf(
  3073. " . SHOW COMMUNICATIONS, SHOW MODEM, SHOW DIAL to see current settings.\n"
  3074.                );
  3075. #endif /* NOSHOW */
  3076.  
  3077. #ifndef NOHELP
  3078.         printf(
  3079. " . HELP SET MODEM, HELP SET DIAL, and HELP DIAL for more information.\n"
  3080.                );
  3081. #endif /* NOHELP */
  3082.         printf("(Use SET HINTS OFF to suppress future hints.)\n");
  3083.         printf("*************************\n\n");
  3084.         }
  3085. #endif /* NOHINTS */
  3086.     }
  3087.     }
  3088.     return(success);
  3089. }
  3090. #endif /* NODIAL */
  3091.  
  3092. /*  D O T Y P E  --  Type (display) a file with various options...  */
  3093.  
  3094. #ifdef BIGBUFOK
  3095. #define TYPBUFL 16384
  3096. #else
  3097. #define TYPBUFL 256
  3098. #endif /* BIGBUFOK */
  3099.  
  3100. int typ_lines = 0;            /* \v(ty_ln) */
  3101. int typ_mtchs = 0;            /* \v(ty_lm) */
  3102. static int typ_int = 0;            /* Flag if TYPE interrupted */
  3103.  
  3104. #ifdef UNICODE
  3105. extern int fcharset, fileorder, byteorder, ucsorder;
  3106. #define TYPXBUFL TYPBUFL+TYPBUFL+TYPBUFL+4
  3107. static char * mp = NULL;
  3108. static char * mbuf = NULL;
  3109. static long xn = 0L;
  3110.  
  3111. static int
  3112. #ifdef CK_ANSIC
  3113. storechar(char c)
  3114. #else
  3115. storechar(c) char c;
  3116. #endif /* CK_ANSIC */
  3117. {
  3118.     if (!mp) return(-1);
  3119.     if (++xn > TYPXBUFL)
  3120.       return(-1);
  3121.     debug(F111,"storechar xn",ckitoa((int)c),xn);
  3122.     *mp++ = c;
  3123.     return(0);
  3124. }
  3125. #endif /* UNICODE */
  3126.  
  3127. static FILE * ofp = NULL;        /* For /OUTPUT: file */
  3128.  
  3129. static int
  3130. typeline(buf,len,outcs,ofp) char * buf; int len, outcs; FILE * ofp; {
  3131.     register int i;
  3132.  
  3133.     debug(F011,"typeline buf",buf,len);
  3134.     /* debug(F101,"typeline outcs","",outcs); */
  3135.  
  3136. #ifdef OS2
  3137. #ifndef NOLOCAL
  3138. #ifdef UNICODE
  3139.     /* In K95 only, the buffer is guaranteed to be in UCS-2 if outcs >= 0. */
  3140.     /* Len is its length in bytes.  There is no line terminator. */
  3141.     /* outcs is the file character-set number (FC_xxx) of the target set */
  3142.     /* that was requested by the user. */
  3143.     if (!inserver && !k95stdout) {
  3144.         extern int wherex[], wherey[];
  3145.     extern unsigned char colorcmd;
  3146.  
  3147.     VscrnWrtUCS2StrAtt( VCMD, (unsigned short *)buf, len/2,
  3148.                wherey[VCMD], wherex[VCMD], &colorcmd);
  3149.         printf("\r\n");
  3150.     return(0);
  3151.     }
  3152. #endif /* UNICODE */
  3153. #endif /* NOLOCAL */
  3154. #endif /* OS2 */
  3155.  
  3156. /* In Unix, VMS, etc, the line has already been converted to the desired  */
  3157. /* character-set, if one was given.  OR... on all platforms, including in */
  3158. /* K95, we don't know the character set.  In either case we dump the line */
  3159. /* byte by byte in case it contains NULs (printf() would truncate). */
  3160.  
  3161. #ifdef COMMENT
  3162.     for (i = 0; i < len; i++)
  3163.       putchar(buf[i]);
  3164. #else
  3165.     for (i = 0; i < len; i++) {
  3166.     if (ofp == stdout) {
  3167.         putchar(buf[i]);
  3168.     } else {
  3169.         putc(buf[i],ofp);
  3170.     }
  3171.     }
  3172. #endif /* COMMENT */
  3173.  
  3174. #ifdef IKSD
  3175.     if (inserver) {
  3176. #ifdef UNICODE
  3177.     if (outcs == FC_UCS2) {
  3178.         if (ofp == stdout) {
  3179.         putchar(NUL);
  3180.         } else {
  3181.         putc(NUL,ofp);
  3182.         }
  3183.     }
  3184. #endif /* UNICODE */
  3185.     if (ofp == stdout) {
  3186.         putchar('\r');
  3187.     } else {
  3188.         putc('\r',ofp);
  3189.     }
  3190.     }
  3191. #endif /* IKSD */
  3192. #ifdef UNICODE
  3193.     if (outcs == FC_UCS2) {
  3194.     if (ofp == stdout) {
  3195.         putchar(NUL);
  3196.     } else {
  3197.         putc(NUL,ofp);
  3198.     }
  3199.     }
  3200. #endif /* UNICODE */
  3201.     if (ofp == stdout) {
  3202.     putchar('\n');
  3203.     } else {
  3204.     putc('\n',ofp);
  3205.     }
  3206.     fflush(stdout);
  3207.     return(0);
  3208. }
  3209.  
  3210. static int                /* Get translated line */
  3211. typegetline(incs, outcs, buf, n) int incs, outcs, n; char * buf; {
  3212.     int x = 0, c0, c1, len = 0, count = 0, eof = 0, xlate = 0;
  3213. #ifdef UNICODE
  3214.     int xxn = -1;
  3215.     int yyn = -9;
  3216.     xn = 0L;
  3217.  
  3218. #ifdef DEBUG
  3219.     if (deblog && typ_lines == 0) {
  3220.     debug(F101,"typegetline incs","",incs);
  3221.     debug(F101,"typegetline outcs","",outcs);
  3222.     debug(F101,"typegetline feol","",feol);
  3223.     debug(F101,"typegetline byteorder","",byteorder);
  3224.     debug(F101,"typegetline ucsorder ","",ucsorder);
  3225.     debug(F111,"typegetline fileorder","1",fileorder);
  3226.     }
  3227. #endif /* DEBUG */
  3228.  
  3229.     if (incs < 0)            /* Shouldn't happen */
  3230.       return(-2);
  3231.  
  3232.     if (outcs == -1)            /* Can happen */
  3233.       outcs = incs;
  3234.  
  3235.     if (incs != outcs || incs == FC_UCS2) { /* See if we should translate */
  3236.     xlate = 1;
  3237.         if (!mbuf) {            /* Allocate buffer if not allocated */
  3238.         mbuf = (char *)malloc(TYPXBUFL+1); /* yet */
  3239.         if (!mbuf) {
  3240.         printf("WARNING: Translation buffer allocation failure.\n");
  3241.         printf("Translation will be skipped...\n");
  3242.         xlate = 0;
  3243.         }
  3244.     }
  3245.     }
  3246.     if (xlate) {            /* Translating... */
  3247.     mp = mbuf;            /* Reset working buffer pointer */
  3248. /*
  3249.   Here we call xgnbyte() in a loop, having it return UCS-2 bytes.  In K95, we
  3250.   use UCS-2 directly.  Elsewhere, we feed the UCS-2 bytes into xpnbyte() to
  3251.   convert them to the desired target character set.  But since we are using
  3252.   UCS-2, we have several sources for confusion: (1) xgnbyte() might return in
  3253.   LE or BE byte order, with no explicit indication of what the order is; but
  3254.   (2) xpnbyte() wants BE; but (3) Windows wants LE.
  3255. */
  3256.     while (1) {
  3257.         if (typ_int)        /* Quit if interrupted */
  3258.           return(0);
  3259.         c0 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3260.         debug(F000,"typegetline c0","",c0);
  3261.         if (c0 < 0) {        /* EOF */
  3262.         eof++;
  3263.         break;
  3264.         }
  3265.         c1 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3266.         debug(F000,"typegetline c1","",c1);
  3267.         if (c1 < 0) {        /* EOF */
  3268.         eof++;
  3269.         break;
  3270.         }
  3271. #ifdef DEBUG
  3272.         if (deblog && typ_lines == 0) {
  3273.         if (count == 0)    /* Check fileorder after BOM */
  3274.           debug(F111,"typegetline fileorder","2",fileorder);
  3275.         }
  3276. #endif /* DEBUG */
  3277.  
  3278. /* Now we have the two UCS-2 bytes.  Which order are they in? */
  3279.  
  3280.         if (fileorder > 0) {    /* Little Endian */
  3281.         int t;            /* So swap them */
  3282.         debug(F100,"typegetline swapping","",0);
  3283.         t = c1;
  3284.         c1 = c0;
  3285.         c0 = t;
  3286.         }
  3287.         if (c0 == 0 && c1 == 0x0D)    /* Now see if we have EOL */
  3288.           yyn = xn;
  3289.  
  3290.         if (c0 == 0 && c1 == 0x0A)    /* Now see if we have EOL */
  3291.           xxn = xn;
  3292.  
  3293.         count++;            /* Count byte */
  3294.  
  3295. /* Give the two bytes to xpnbyte() in BE order */
  3296.  
  3297.         if ((x = xpnbyte(c0,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3298.         if ((x = xpnbyte(c1,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3299.  
  3300.         if (xxn > -1) {        /* Have end of line? */
  3301.         xn = xxn;
  3302.         if (yyn == xxn - 2)    /* Adjust for CRLF */
  3303.           xn = yyn;
  3304.         break;            /* And break out of loop. */
  3305.         }
  3306.     }
  3307.     mbuf[xn] = NUL;
  3308.     if (xn > n)            /* Can truncate here... */
  3309.       xn = n;
  3310.     memcpy(buf,mbuf,xn);
  3311.     debug(F011,"typegetline xlate",buf,xn);
  3312.     return((eof && (xn == 0)) ? -1 : xn);
  3313.     }
  3314. #endif /* UNICODE */
  3315. #ifdef COMMENT
  3316.     /* We can't use this because, stupidly, zsinl() doesn't return a length. */
  3317.     /* It could be changed but then we'd have to change all ck?fio.c modules */
  3318.     x = zsinl(ZIFILE,buf,n);
  3319. #else
  3320.     /* So instead, we copy zsinl() to here... */
  3321.     /* But note: This does not necessarily handle UCS-2 alignment properly;  */
  3322.     /* that's what the code in the first section of this routine is for. */
  3323.     /* But it does tolerate files that contain NULs. */
  3324.     {
  3325.     int a;
  3326.     char *s;
  3327.  
  3328.     s = buf;
  3329.     a = -1;                /* Current character, none yet. */
  3330.     debug(F101,"typegetline zsinl simulation","",n);
  3331.     while (n--) {            /* Up to given length */
  3332. #ifdef COMMENT
  3333.         int old = 0;
  3334.         if (feol)            /* Previous character */
  3335.           old = a;
  3336. #endif /* COMMENT */
  3337.         if (zchin(ZIFILE,&a) < 0) {    /* Read a character from the file */
  3338.         debug(F101,"typegetline zchin fail","",count);
  3339.         if (count == 0)
  3340.           x = -1;        /* EOF or other error */
  3341.         break;
  3342.         } else
  3343.           count++;
  3344.         if (feol) {            /* Single-character line terminator */
  3345.         if (a == feol)
  3346.           break;
  3347.         } else {            /* CRLF line terminator */
  3348. #ifdef COMMENT
  3349. /* Debug log shows that in Windows, <CR><LF> is returned as <LF>. */
  3350. /* Apparently we're not reading the file in binary mode. */
  3351.  
  3352.         if (a == '\015')    /* CR, get next character */
  3353.           continue;
  3354.         if (old == '\015') {    /* Previous character was CR */
  3355.             if (a == '\012') {    /* This one is LF, so we have a line */
  3356.             break;
  3357.             } else {        /* Not LF, deposit CR */
  3358.             *s++ = '\015';
  3359.             n--;
  3360.             len++;
  3361.             }
  3362.         }
  3363. #else
  3364.         if (a == LF) {
  3365.             if (s[len] == CR) {    /* This probably won't happen */
  3366.             s[len] = NUL;
  3367.             s--;
  3368.             len--;
  3369.             }
  3370.             break;
  3371.         }
  3372. #endif /* COMMENT */
  3373.         }
  3374.         *s = a;            /* Deposit character */
  3375.         s++;
  3376.         len++;
  3377.     }
  3378.     *s = '\0';            /* Terminate the string */
  3379.     }
  3380. #endif /* COMMENT */
  3381.     return(x < 0 ? -1 : len);
  3382. }
  3383.  
  3384.  
  3385. #ifndef MAC
  3386. SIGTYP
  3387. #ifdef CK_ANSIC
  3388. tytrap(int foo)                         /* TYPE interrupt trap */
  3389. #else
  3390. tytrap(foo) int foo;
  3391. #endif /* CK_ANSIC */
  3392. /* tytrap */ {
  3393. #ifdef __EMX__
  3394.     signal(SIGINT, SIG_ACK);
  3395. #endif
  3396.     debug(F100,"type tytrap SIGINT","",0);
  3397.     typ_int = 1;            /* (Need arg for ANSI C) */
  3398.     SIGRETURN;
  3399. }
  3400. #endif /* MAC */
  3401.  
  3402. int
  3403. dotype(file, paging, first, head, pat, width, prefix, incs, outcs, outfile, z)
  3404.     char * file, * pat, * prefix; int paging, first, head, width, incs, outcs;
  3405.     char * outfile; int z;
  3406. /* dotype */ {
  3407.     extern long ffc;
  3408.     char buf[TYPBUFL+2];
  3409.     char * s = NULL;
  3410.     int rc = 1, lines = 0, ucs2 = 0;
  3411.     char ** tail = NULL;
  3412.     int * tlen = NULL;
  3413.     int tailing = 0, counting = 0;
  3414.     int x, c, n, i, j, k = 0;
  3415.     int number = 0, save, len, pfxlen = 0, evalpfx = 1;
  3416. #ifdef UNICODE
  3417.     int ucsbom_sav;
  3418.     extern int ucsbom;
  3419. #endif /* UNICODE */
  3420.  
  3421. #ifndef MAC
  3422. #ifdef OS2
  3423. #ifdef NT
  3424.     SIGTYP (* oldsig)(int);             /* For saving old interrupt trap. */
  3425. #else /* NT */
  3426.     SIGTYP (* volatile oldsig)(int);
  3427. #endif /* NT */
  3428. #else /* OS2 */
  3429.     SIGTYP (* oldsig)();
  3430. #endif /* OS2 */
  3431. #endif /* MAC */
  3432.  
  3433.     if (!file) file = "";
  3434.     if (!*file) return(-2);
  3435.  
  3436.     if (ofp != stdout) {        /* In case of previous interruption */
  3437.     if (ofp) fclose(ofp);
  3438.     ofp = stdout;
  3439.     }
  3440.     if (!outfile) outfile = "";
  3441.     if (outfile[0]) {
  3442.     ofp = fopen(outfile,"w");    /* Open output file */
  3443.     if (!ofp) {
  3444.         printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  3445.         ofp = stdout;
  3446.         return(-9);
  3447.     }
  3448.     }
  3449.     number = z;
  3450.     if (number && prefix) prefix = NULL;
  3451.  
  3452. #ifdef UNICODE
  3453.     ucsbom_sav = ucsbom;                /* We are not creating a file */
  3454.     ucsbom = 0;                         /* Do not use BOM bytes */
  3455. #endif /* UNICODE */
  3456.  
  3457.     typ_int = 0;
  3458.  
  3459.     save = binary;            /* Save file type */
  3460.  
  3461.     debug(F101,"dotype incs","",incs);
  3462.     debug(F101,"dotype outcs","",outcs);
  3463.  
  3464. #ifdef UNICODE
  3465.     debug(F111,"dotype fileorder","A",fileorder);
  3466. #ifdef OS2
  3467.     if (!inserver && !k95stdout)
  3468.       outcs = FC_UCS2;
  3469. #endif /* OS2 */
  3470.  
  3471.     if (outcs == FC_UCS2)        /* Output is UCS-2? */
  3472.       ucs2 = 1;
  3473.     if (fileorder < 0)
  3474.       fileorder = ucsorder;
  3475.     debug(F111,"dotype fileorder","B",fileorder);
  3476. #endif /* UNICODE */
  3477.  
  3478. #ifdef CK_TTGWSIZ
  3479. #ifdef OS2
  3480.     ttgcwsz();
  3481. #else /* OS2 */
  3482.     /* Check whether window size changed */
  3483.     if (ttgwsiz() > 0) {
  3484.         if (tt_rows > 0 && tt_cols > 0) {
  3485.             cmd_rows = tt_rows;
  3486.             cmd_cols = tt_cols;
  3487.         debug(F101,"dotype cmd_rows","",cmd_rows);
  3488.         debug(F101,"dotype cmd_cols","",cmd_cols);
  3489.         }
  3490.     }
  3491. #endif /* OS2 */
  3492. #endif /* CK_TTGWSIZ */
  3493.  
  3494.     if (prefix)
  3495.       pfxlen = strlen(prefix);
  3496.  
  3497.     if (paging < 0) {            /* Count only, don't print */
  3498.     counting = 1;
  3499.     prefix = NULL;
  3500.     width = 0;
  3501.     paging = 0;
  3502.     }
  3503.     if (ucs2)                /* Crude... */
  3504.       width *= 2;
  3505.  
  3506. #ifdef OS2
  3507.     if (*file) {
  3508.         ckstrncpy(buf, file, TYPBUFL);    /* Change / to \. */
  3509.         p = buf;
  3510.         while (*p) {
  3511.             if (*p == '/') *p = '\\';
  3512.             p++;
  3513.         }
  3514.     file = buf;
  3515.     } else {
  3516.     rc = 0;
  3517.     goto xdotype;
  3518.     }
  3519. #endif /* OS2 */
  3520.  
  3521.     if (zchki(file) == -2) {        /* It's a directory */
  3522.     debug(F111,"dotype zchki failure",file,-2);
  3523.     if (xcmdsrc == 0) {
  3524.         printf("?Not a regular file: \"%s\"\n",file);
  3525.         rc = -9;
  3526.     } else
  3527.       rc = 0;
  3528.     goto xdotype;
  3529.     }
  3530.     if (!zopeni(ZIFILE, file)) {    /* Not a directory, open it */
  3531.     debug(F111,"dotype zopeni failure",file,0);
  3532.     if (xcmdsrc == 0) {
  3533.         printf("?Can't open file: \"%s\"\n",file);
  3534.         rc = -9;
  3535.     } else
  3536.       rc = 0;
  3537.     goto xdotype;
  3538.     }
  3539.  
  3540. #ifndef AMIGA
  3541. #ifndef MAC
  3542.     errno = 0;
  3543.     oldsig = signal(SIGINT, tytrap);    /* Save current interrupt trap. */
  3544.     debug(F111,"type SIGINT trap set",ckitoa(errno),oldsig);
  3545. #endif /* MAC */
  3546. #endif /* AMIGA */
  3547.  
  3548.     if (paging > -1)            /* More-prompting */
  3549.       xaskmore = paging;
  3550.  
  3551.     binary = 0;
  3552.  
  3553.     if (head < 0) {            /* "tail" was requested */
  3554.     tailing = 1;            /* Set flag */
  3555.     head = 0 - head;        /* Get absolute number of lines */
  3556.     if (!counting) {
  3557.         tail = (char **) malloc(head * sizeof(char *)); /* Allocate list */
  3558.         if (!tail) {
  3559.         printf("?Memory allocation failure\n");
  3560.         goto xdotype;
  3561.  
  3562.         }
  3563.         tlen = (int *) malloc(head * sizeof(int));
  3564.         if (!tlen) {
  3565.         printf("?Memory allocation failure\n");
  3566.         goto xdotype;
  3567.  
  3568.         }
  3569.         for (i = 0; i < head; i++) { /* Initialize each pointer in list. */
  3570.         tail[i] = NULL;
  3571.         tlen[i] = 0;
  3572.         }
  3573.     }
  3574.     }
  3575.     typ_lines = 0;
  3576.     typ_mtchs = 0;
  3577.  
  3578. #ifdef UNICODE
  3579.     if (outcs > -1 && (incs != outcs || incs == FC_UCS2)) { /* Translating? */
  3580.     ffc = 0L;
  3581.     initxlate(incs,outcs);        /* Set up translation functions */
  3582.     } else
  3583. #endif /* UNICODE */
  3584.       outcs = -1;            /* Means we don't know the charset */
  3585.  
  3586.     debug(F101,"dotype ffc","",ffc);
  3587.     debug(F101,"dotype outcs 2","",outcs);
  3588. #ifdef UNICODE
  3589.     debug(F111,"dotype fileorder","C",fileorder);
  3590. #endif /* UNICODE */
  3591.  
  3592.     /* Allow the buffer to contain NULs */
  3593.  
  3594.     for (n = first;
  3595.      (len = typegetline(incs,outcs,buf,TYPBUFL)) > -1;
  3596.      lines++
  3597.      ) {
  3598.     debug(F011,"dotype line",buf,len);
  3599. #ifndef MAC
  3600.         if (typ_int) {            /* Interrupted? */
  3601.         typ_int = 0;
  3602.         debug(F101,"type interrupted line","",lines);
  3603.             printf("^C...\n");          /* Print message */
  3604.         if (ofp != stdout) {    /* Close any output file */
  3605.         if (ofp) fclose(ofp);
  3606.         ofp = stdout;
  3607.         }
  3608.             goto xxdotype;
  3609.         }
  3610. #endif /* MAC */
  3611.     typ_lines++;            /* For \v(ty_ln) */
  3612.     if (pat)            /* Matching? */
  3613.       if (!ckmatch(pat,buf,1,1+4))    /* Line matches pattern? */
  3614.         continue;            /* No, skip it */
  3615.     typ_mtchs++;
  3616.  
  3617.     if (head > 0 && !tailing && lines == head) /* Handle /HEAD:n */
  3618.       break;
  3619.  
  3620.     buf[TYPBUFL+1] = NUL;        /* Just in case... */
  3621.     if (prefix) {            /* Add specified prefix to each line */
  3622.         char pbuf[64];
  3623.         char * pp;
  3624.         pp = prefix;
  3625. #ifndef NOSPL
  3626.         if (evalpfx) {        /* Prefix is a variable? */
  3627.         int n = 63;        /* Maybe - evaluate it and see */
  3628.         char * p = pbuf;
  3629.         zzstring(prefix,&p,&n); /* If there is no change */
  3630.         if (!strcmp(prefix,pbuf)) { /* it's not a variable */
  3631.             evalpfx = 0;    /* So don't do this again. */
  3632.         } else {        /* It was a variable */
  3633.             pp = pbuf;        /* So substitute its value */
  3634.             pfxlen = 63 - n;    /* and get its new length */
  3635.         }
  3636.         }
  3637. #endif /* NOSPL */
  3638.         if (len + pfxlen + 2 < TYPBUFL) {
  3639.         /* Shift right to make room for prefix */
  3640.         memcpy((char *)line+pfxlen,(char *)buf,len);
  3641.         lset((char *)line,pp,pfxlen,SP);
  3642.         debug(F110,"dotype prefix",line,pfxlen);
  3643.         len += pfxlen;
  3644.         memcpy((char *)buf,(char *)line,len);
  3645.         }
  3646.     } else if (number) {        /* Line numbers */
  3647.         int x;
  3648.         sprintf(line,"%4d. ",typ_lines);
  3649.         x = strlen(line);
  3650.         len += x;
  3651.         if (len < LINBUFSIZ) {
  3652.         memcpy((char *)&line[x],(char *)buf,len);
  3653.         memcpy((char *)buf,(char *)line,len);
  3654.         }
  3655.     }
  3656.     if (width > 0 && width <= TYPBUFL) { /* Truncate at given width. */
  3657.         char * obuf = line;        /* But to do that first we must */
  3658.         int i,k,z;            /* expand tabs; assume every 8 cols. */
  3659.         line[0] = NUL;
  3660.         for (i = 0, k = 0; i < width; k++) { /* Character loop... */
  3661.         if (!buf[k])        /* No more chars in this line, done. */
  3662.           break;
  3663.         if (buf[k] != '\t') {    /* If it's not a tab */
  3664.             if (i >= LINBUFSIZ)    /* Check for overflow */
  3665.               break;
  3666.             obuf[i++] = buf[k];    /* and then deposit it. */
  3667.             obuf[i] = NUL;    /* Keep it null-terminated */
  3668.             continue;
  3669.         }
  3670.         z = 8 - (i % 8);    /* It's a tab, expand it. */
  3671.         if (z == 0) z = 8;
  3672.         for (j = 0; j < z && i < LINBUFSIZ; j++) {
  3673. #ifdef UNICODE
  3674.             if (ucs2 && !ucsorder)
  3675.               obuf[i++] = NUL;
  3676. #endif /* UNICODE */
  3677.             obuf[i++] = ' ';
  3678. #ifdef UNICODE
  3679.             if (ucs2 && ucsorder)
  3680.               obuf[i++] = NUL;
  3681. #endif /* UNICODE */
  3682.         }
  3683.         obuf[i++] = NUL;
  3684.         obuf[i] = NUL;
  3685.         }
  3686.         obuf[width] = NUL;        /* Now truncate at given width. */
  3687. #ifdef COMMENT
  3688.         /* This doesn't work for UCS-2 because it contains NULs */
  3689.         ckstrncpy(buf,obuf,TYPBUFL); /* and copy it back (again?) */
  3690. #else
  3691.         memcpy((char *)buf,(char *)obuf,i); /* Copy it back */
  3692. #endif /* COMMENT */
  3693.         len = (i > width) ? width : i; /* Spare us another strlen()... */
  3694.     }
  3695.     if (tailing) {            /* If /TAIL:n... */
  3696.         k = lines % head;        /* save this line in circular buffer */
  3697.         if (!counting) {
  3698.         if (tail[k]) free(tail[k]);
  3699.         tail[k] = malloc(len+2);
  3700.         if (!tail[k]) {
  3701.             printf("?Memory allocation failure\n");
  3702.             goto xdotype;
  3703.         }
  3704.         memcpy(tail[k],buf,len);
  3705.         tlen[k] = len;
  3706.         continue;
  3707.         }
  3708.     }
  3709.     if (counting)            /* If only counting */
  3710.       continue;            /* we're done with this line */
  3711.  
  3712.     if (paging) {            /* Displaying this line... */
  3713.         int u;
  3714.         u = len;            /* Length in BYTES */
  3715.         if (ucs2)            /* If outputting in UCS-2 */
  3716.           u /= 2;            /* convert length to CHARACTERS */
  3717.         x = (u / cmd_cols) + 1;    /* Crudely allow for wrap */
  3718.         if (cmd_rows > 0 && cmd_cols > 0)
  3719.           n += x;            /* This assumes terminal will wrap */
  3720.     }
  3721.     typeline(buf,len,outcs,ofp);    /* Print line, length based */
  3722. #ifdef CK_TTGWSIZ
  3723.     debug(F101,"dotype n","",n);
  3724.     if (paging > 0 && ofp == stdout) { /* Pause at end of screen */
  3725.         if (cmd_rows > 0 && cmd_cols > 0) {
  3726.         if (n > cmd_rows - 3) {
  3727.             if (!askmore())
  3728.               goto xdotype;
  3729.             else
  3730.               n = 0;
  3731.         }
  3732.         }
  3733.     }
  3734. #endif /* CK_TTGWSIZ */
  3735.     }
  3736.  
  3737.   xdotype:
  3738.     if (counting) {
  3739.     fprintf(ofp,
  3740.         "%s: %d line%s\n",file,typ_lines,typ_lines == 1 ? "" : "s");
  3741.     if (pat)
  3742.       fprintf(ofp,
  3743.           "%s: %d match%s\n",pat,typ_mtchs,typ_mtchs == 1 ? "" : "es");
  3744.     goto xxdotype;
  3745.     }
  3746.     if (tailing && tail) {        /* Typing tail of file? */
  3747.     if (lines < head) {        /* Yes, show the lines we saved */
  3748.         k = 0;            /* Show all lines */
  3749.     } else {            /* More lines than tail number */
  3750.         lines = k;            /* Last line to show */
  3751.         k++;            /* First line to show */
  3752.         if (k >= head)
  3753.           k = 0;
  3754.     }
  3755.     n = first;            /* Output line counter */
  3756.     for (i = k ;; i++) {        /* Loop thru circular buffer */
  3757. #ifndef MAC
  3758.         if (typ_int) {        /* Interrupted? */
  3759.         printf("^C...\n");    /* Print message */
  3760.         goto xxdotype;
  3761.         }
  3762. #endif /* MAC */
  3763.         j = i % head;        /* Index of this line */
  3764.         s = tail[j];        /* Point to line to display */
  3765.         if (!s)            /* (shouldn't happen...) */
  3766.           break;
  3767.         if (paging) {        /* Crudely allow for line wrap */
  3768.         x = tlen[j];
  3769.         if (ucs2) x /= 2;
  3770.         x = x / cmd_cols + 1;
  3771.         if (cmd_rows > 0 && cmd_cols > 0)
  3772.           n += x;
  3773.         }
  3774.         typeline(s,tlen[j],outcs,ofp); /* Display this line */
  3775.         if (paging && ofp == stdout) { /* Pause at end of screen */
  3776.         if (cmd_rows > 0 && cmd_cols > 0) {
  3777.             if (n > cmd_rows - 3) {
  3778.             if (!askmore())
  3779.               break;
  3780.             else
  3781.               n = 0;
  3782.             }
  3783.         }
  3784.         }
  3785.         tail[j] = NULL;
  3786.         free(s);            /* Free the line */
  3787.         if (i % head == lines)    /* When to stop */
  3788.           break;
  3789.     }
  3790.     free((char *)tail);        /* Free the list */
  3791.     tail = NULL;
  3792.     if (tlen) free((char *)tlen);
  3793.         tlen = NULL;
  3794.     }
  3795.  
  3796. /* Come here when finished or on SIGINT */
  3797.  
  3798.   xxdotype:
  3799. #ifndef AMIGA
  3800. #ifndef MAC
  3801.     signal(SIGINT,oldsig);              /* Put old signal action back. */
  3802. #endif /* MAC */
  3803. #endif /* AMIGA */
  3804.     if (tailing && tail) {
  3805.     for (i = 0; i < head; i++) {    /* Free each line. */
  3806.         if (tail[i])
  3807.           free(tail[i]);
  3808.     }
  3809.     free((char *)tail);        /* Free list pointer */
  3810.         if (tlen)
  3811.       free((char *)tlen);
  3812.     }
  3813.     x = zclose(ZIFILE);            /* Done, close the input file */
  3814.     if (ofp != stdout) {        /* Close any output file */
  3815.     if (ofp) fclose(ofp);
  3816.     ofp = stdout;
  3817.     }
  3818.     binary = save;            /* Restore text/binary mode */
  3819. #ifdef UNICODE
  3820.     ucsbom = ucsbom_sav;                /* Restore BOM usage */
  3821. #endif /* UNICODE */
  3822.     return(rc);
  3823. }
  3824.  
  3825. /* GREP command */
  3826.  
  3827. #define GREP_CASE  0            /* /CASE */
  3828. #define GREP_COUN  1            /* /COUNT */
  3829. #define GREP_DOTF  2            /* /DOTFILES */
  3830. #define GREP_NAME  3            /* /NAMEONLY */
  3831. #define GREP_NOBK  4            /* /NOBACKUP */
  3832. #define GREP_NODO  5            /* /NODOTFILES */
  3833. #define GREP_NOLI  6            /* /NOLIST = /QUIET */
  3834. #define GREP_NOMA  7            /* /INVERT = /NOMATCH */
  3835. #define GREP_NOPA  8            /* /NOPAGE */
  3836. #define GREP_NUMS  9            /* /LINENUMBERS */
  3837. #define GREP_PAGE 10            /* /PAGE */
  3838. #define GREP_RECU 11            /* /RECURSIVE */
  3839. #define GREP_TYPE 12            /* /TYPE: */
  3840. #define GREP_OUTP 13            /* /OUTPUTFILE: */
  3841.  
  3842. static struct keytab greptab[] = {
  3843.     { "/count",        GREP_COUN, CM_ARG },
  3844.     { "/dotfiles",     GREP_DOTF, 0 },
  3845.     { "/nameonly",     GREP_NAME, 0 },
  3846.     { "/nobackupfiles",GREP_NOBK, 0 },
  3847.     { "/nocase",       GREP_CASE, 0 },
  3848.     { "/nodotfiles",   GREP_NODO, 0 },
  3849.     { "/nolist",       GREP_NOLI, 0 },
  3850.     { "/nomatch",      GREP_NOMA, 0 },
  3851.     { "/nopage",       GREP_NOPA, 0 },
  3852.     { "/output",       GREP_OUTP, CM_ARG },
  3853.     { "/linenumbers",  GREP_NUMS, 0 },
  3854.     { "/page",         GREP_PAGE, 0 },
  3855.     { "/quiet",        GREP_NOLI, CM_INV },
  3856. #ifdef RECURSIVE
  3857.     { "/recursive",    GREP_RECU, 0 },
  3858. #endif /* RECURSIVE */
  3859.     { "/type",         GREP_TYPE, CM_ARG },
  3860.     { "", 0, 0 }
  3861. };
  3862. static int ngreptab =  sizeof(greptab)/sizeof(struct keytab)-1;
  3863.  
  3864. int
  3865. dogrep() {
  3866.     int match, x, y, fc, getval, mc = 0, count = 0, bigcount = 0;
  3867.     int fline = 0, sline = 0, wild = 0, len = 0;
  3868.     int xmode = -1, scan = 0;
  3869.     char c, name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p, *s, *cv = NULL;
  3870.     FILE * fp = NULL;
  3871.  
  3872.     int                    /* Switch values and defaults */
  3873.       gr_coun = 0,
  3874.       gr_name = 0,
  3875.       gr_nobk = 0,
  3876.       gr_case = 1,
  3877.       gr_noli = 0,
  3878.       gr_noma = 0,
  3879.       gr_nums = 0,
  3880.       gr_page = xaskmore;
  3881.  
  3882.     struct FDB sw, fl;
  3883.  
  3884.     g_matchdot = matchdot;        /* Save global matchdot setting */
  3885.     outfile[0] = NUL;
  3886.  
  3887.     if (ofp != stdout) {        /* In case of previous interruption */
  3888.     if (ofp) fclose(ofp);
  3889.     ofp = stdout;
  3890.     }
  3891.     cmfdbi(&sw,                /* First FDB - command switches */
  3892.        _CMKEY,            /* fcode */
  3893.        "String or pattern to search for, or switch",
  3894.        "",                /* default */
  3895.        "",                /* addtl string data */
  3896.        ngreptab,            /* addtl numeric data 1: tbl size */
  3897.        4,                /* addtl numeric data 2: 4 = cmswi */
  3898.        xxstring,            /* Processing function */
  3899.        greptab,            /* Keyword table */
  3900.        &fl                /* Pointer to next FDB */
  3901.        );
  3902.     cmfdbi(&fl,                /* Anything that doesn't match */
  3903.        _CMFLD,            /* fcode */
  3904.        "",                /* hlpmsg */
  3905.        "",                /* default */
  3906.        "",                /* addtl string data */
  3907.        0,                /* addtl numeric data 1 */
  3908.        0,                /* addtl numeric data 2 */
  3909.        NULL,
  3910.        NULL,
  3911.        NULL
  3912.        );
  3913.     while (1) {                /* Parse 0 or more switches */
  3914.     x = cmfdb(&sw);            /* Parse something */
  3915.     if (x < 0)
  3916.       return(x);
  3917.     if (cmresult.fcode != _CMKEY)    /* Break out if not a switch */
  3918.       break;
  3919.     c = cmgbrk();
  3920.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  3921.         printf("?This switch does not take an argument\n");
  3922.         return(-9);
  3923.     }
  3924.     if ((cmresult.nresult != GREP_COUN) && !getval &&
  3925.         (cmgkwflgs() & CM_ARG)) {
  3926.         printf("?This switch requires an argument\n");
  3927.         return(-9);
  3928.     }
  3929.     switch (cmresult.nresult) {
  3930.       case GREP_COUN: {
  3931.           gr_coun++;
  3932.           gr_noli=0;
  3933.           if (getval) {
  3934.           if ((x = cmfld("Variable for result","",&s,NULL)) < 0)
  3935.             return(x);
  3936.           makestr(&cv,s);
  3937.           }
  3938.           break;
  3939.       }
  3940.       case GREP_CASE: gr_case=0; break;
  3941.       case GREP_NAME: gr_name++; gr_noli=0; break;
  3942.       case GREP_NOBK: gr_nobk++; break;
  3943.       case GREP_NOLI: gr_noli++; gr_coun=0; gr_name=0; gr_nums=0; break;
  3944.       case GREP_NOMA: gr_noma++; break;
  3945.       case GREP_NOPA: gr_page=0; break;
  3946.       case GREP_NUMS: gr_nums++; gr_noli=0; break;
  3947.       case GREP_PAGE: gr_page++; gr_noli=0; break;
  3948.       case GREP_NODO:
  3949.         matchdot = 0;
  3950.         break;
  3951.       case GREP_DOTF:
  3952.         matchdot = 1;
  3953.         break;
  3954. #ifdef RECURSIVE
  3955.       case GREP_RECU:
  3956.         recursive = 1;
  3957.         break;
  3958. #endif /* RECURSIVE */
  3959.       case GREP_TYPE: {
  3960.           extern struct keytab txtbin[];
  3961.           if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  3962.         return(x);
  3963.           if (x == 2) {        /* ALL */
  3964.           xmode = -1;
  3965.           } else {            /* TEXT or BINARY only */
  3966.           xmode = x;
  3967.           scan = 1;
  3968.           }
  3969.           break;
  3970.       }
  3971.       case GREP_OUTP:        /* Send output to file */
  3972.         if ((x = cmofi("File for GREP'd lines","",&s,xxstring)) < 0)
  3973.           return(x);
  3974.         ckstrncpy(outfile,s,CKMAXPATH);
  3975.         break;
  3976.     }
  3977.     }
  3978.     if (outfile[0]) {
  3979.     ofp = fopen(outfile,"w");    /* Open output file */
  3980.     if (!ofp) {
  3981.         printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  3982.         ofp = stdout;
  3983.         return(-9);
  3984.     }
  3985.     gr_page = 0;
  3986.     }
  3987.     s = cmresult.sresult;
  3988.     s = brstrip(s);            /* Strip braces from pattern */
  3989.     if (!*s) {
  3990.     printf("?Pattern required\n");
  3991.     return(-9);
  3992.     }
  3993.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);    /* Save pattern */
  3994.     if ((x = cmifi("File(s) to search","",&s,&wild,xxstring)) < 0) {
  3995.     if (x == -3) {
  3996.         printf("?File specification required\n");
  3997.         x = -9;
  3998.     }
  3999.     return(x);
  4000.     }
  4001.     s = brstrip(s);            /* Strip braces from filename */
  4002. #ifndef ZXREWIND
  4003.     ckstrncpy(line,s,LINBUFSIZ);
  4004. #endif /* ZXREWIND */
  4005.     if ((y = cmcfm()) < 0)
  4006.       return(y);
  4007.  
  4008.     if (gr_page > -1)
  4009.       xaskmore = gr_page;        /* Paging... */
  4010.  
  4011.     p = tmpbuf;                /* Point to pattern */
  4012. #ifdef COMMENT
  4013. /* Now this is done in ckmatch */
  4014.     if (*p == '^') {            /* '^' anchors pattern to beginning */
  4015.     p++;
  4016.     } else if (*p != '*') {        /* Otherwise prepend implied '*' */
  4017.     tmpbuf[0] = '*';
  4018.     p = tmpbuf;
  4019.     }
  4020.     x = strlen(p);            /* Get length of result */
  4021.     if (x > 0 && x < TMPBUFSIZ) {    /* '$' at end anchors pattern to end */
  4022.     if (p[x-1] == '$') {
  4023.         p[x-1] = NUL;
  4024.     } else if (p[x-1] != '*') {
  4025.         p[x] = '*';
  4026.         p[x+1] = NUL;
  4027.     }
  4028.     }
  4029. #endif /* COMMENT */
  4030.     debug(F111,"grep pat",p,x);
  4031.  
  4032. #ifdef ZXREWIND
  4033.     fc = zxrewind();            /* Rewind the file list */
  4034. #else
  4035.     {
  4036.     int flags = ZX_FILONLY;        /* Expand file list */
  4037.     if (matchdot)  flags |= ZX_MATCHDOT;
  4038.     if (recursive) flags |= ZX_RECURSE;
  4039.     fc = nzxpand(line,flags);
  4040.     }
  4041. #endif /* ZXREWIND */
  4042. #ifdef UNIX
  4043.     sh_sort(mtchs,NULL,fc,0,0,filecase);
  4044. #endif /* UNIX */
  4045.  
  4046.     debug(F101,"grep cmd_rows","",cmd_rows);
  4047.     debug(F101,"grep cmd_cols","",cmd_cols);
  4048.  
  4049.     while (1) {                /* Loop for each file */
  4050.     znext(name);            /* Get next file */
  4051.     if (!name[0])            /* No more, done */
  4052.       break;
  4053.     if (gr_nobk)            /* Skipping backup files? */
  4054.       if (ckmatch("*.~[1-9]*~",name,1,1)) /* Backup file? */
  4055.         continue;            /* Yes, skip */
  4056.     if (scan) {            /* /TYPE: given? */
  4057.         switch (scanfile(name,&y,nscanfile)) { /* Yes, scan the file */
  4058.           case FT_BIN:
  4059.         if (xmode != 1)
  4060.           continue;
  4061.         break;
  4062.           case FT_TEXT:
  4063.           case FT_7BIT:
  4064.           case FT_8BIT:
  4065. #ifdef UNICODE
  4066.           case FT_UTF8:
  4067.           case FT_UCS2:
  4068. #endif /* UNICODE */
  4069.         if (xmode != 0)
  4070.           continue;
  4071.         }
  4072.     }
  4073.     fp = fopen(name,"r");        /* Open */
  4074.     if (!fp)            /* Can't */
  4075.       continue;            /* Skip */
  4076.     count = 0;            /* Match count, this file */
  4077.     fline = 0;            /* Line count, this file */
  4078.     while (1) {            /* Loop for each line */
  4079.         if (fgets(line,LINBUFSIZ,fp) == NULL) { /* Get next line */
  4080.         fclose(fp);
  4081.         fp = NULL;
  4082.         break;
  4083.         }
  4084.         fline++;            /* Count this line */
  4085.         line[LINBUFSIZ] = NUL;    /* Make sure it's terminated */
  4086.         len = (int)strlen(line);    /* Get length */
  4087.         while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
  4088.           line[--len] = NUL;    /* Chop off terminators */
  4089.         match = ckmatch(p,line,gr_case,1+4); /* Match against pattern */
  4090.         if (gr_noma)        /* Invert match sense if requested */
  4091.           match = !match;
  4092.         if (match) {        /* Have a matching line */
  4093.         mc++;            /* Total match count */
  4094.         count++;        /* Match count this file */
  4095.         if (gr_name) {        /* Don't care how many lines match */
  4096.             fclose(fp);        /* Close the file */
  4097.             fp = NULL;        /* and quit the line-reading loop. */
  4098.             break;
  4099.         }
  4100.         if (gr_coun || gr_noli) /* Not listing each line */
  4101.           continue;        /* so don't print anything now. */
  4102.         if (wild) {        /* If searching multiple files */
  4103.             fprintf(ofp,"%s:",name); /* print filename. */
  4104.             len += (int)strlen(name) + 1;
  4105.         }
  4106.         if (gr_nums) {        /* If line numbers wanted */
  4107.             char nbuf[32];
  4108.             len += ckmakmsg(nbuf,32,ckitoa(fline),":",NULL,NULL);
  4109.             fprintf(ofp,"%s",nbuf);
  4110.         }
  4111.         if (cmd_rows > 0 && cmd_cols > 0)
  4112.           sline += (len / cmd_cols) + 1;
  4113.         fprintf(ofp,"%s\n",line); /* Print the line. */
  4114.         if (sline > cmd_rows - 3) {
  4115.             if (!askmore()) goto xgrep; else sline = 0;
  4116.         }
  4117.         }
  4118.     }
  4119.     if (!gr_noli) {            /* If not not listing... */
  4120.         x = 0;
  4121.         if (gr_coun) {        /* Show match count only */
  4122.         fprintf(ofp,"%s:%d\n",name,count);
  4123.         x++;
  4124.         } else if (gr_name && count > 0) { /* Show name only */
  4125.         fprintf(ofp,"%s\n",name);
  4126.         x++;
  4127.         }
  4128.         if (x > 0) {
  4129.         if (++sline > cmd_rows - 3) {
  4130.             if (!askmore()) goto xgrep; else sline = 0;
  4131.         }
  4132.         }
  4133.     }
  4134.     bigcount += count;        /* Overall count */
  4135.     }
  4136.   xgrep:
  4137. #ifndef NOSPL
  4138.     if (gr_coun && cv) {        /* /COUNT:blah */
  4139.     addmac(cv,ckitoa(bigcount));    /* set the variable */
  4140.     makestr(&cv,NULL);        /* free this */
  4141.     }
  4142. #endif /* NOSPL */
  4143.     if (fp) fclose(fp);            /* close input file if still open */
  4144.     if (ofp != stdout) {        /* Close any output file */
  4145.     if (ofp) fclose(ofp);
  4146.     ofp = stdout;
  4147.     }
  4148.     return(success = mc ? 1 : 0);
  4149. }
  4150.  
  4151. /* System-independent directory */
  4152.  
  4153. static char ** dirlist = NULL;
  4154. static int ndirlist = 0;
  4155.  
  4156. static VOID
  4157. freedirlist() {
  4158.     if (dirlist) {
  4159.     int i;
  4160.     for (i = 0; i < ndirlist; i++) {
  4161.         if (dirlist[i])
  4162.           free(dirlist[i]);
  4163.     }
  4164.     free((char *)dirlist);
  4165.     dirlist = NULL;
  4166.     }
  4167.     ndirlist = 0;
  4168. }
  4169.  
  4170. static struct keytab dirswtab[] = {    /* DIRECTORY command switches */
  4171.     { "/after",       DIR_AFT, CM_ARG },
  4172.     { "/all",         DIR_ALL, 0 },
  4173. #ifndef NOSPL
  4174.     { "/array",       DIR_ARR, CM_ARG },
  4175. #endif /* NOSPL */
  4176.     { "/ascending",   DIR_ASC, 0 },
  4177.     { "/backup",      DIR_BUP, 0 },
  4178.     { "/before",      DIR_BEF, CM_ARG },
  4179.     { "/brief",       DIR_BRF, 0 },
  4180.     { "/descending",  DIR_DSC, CM_INV },
  4181.     { "/directories", DIR_DIR, 0 },
  4182.     { "/dotfiles",    DIR_DOT, 0 },
  4183.     { "/englishdate", DIR_DAT, 0 },
  4184.     { "/except",      DIR_EXC, CM_ARG },
  4185.     { "/files",       DIR_FIL, 0 },
  4186.     { "/heading",     DIR_HDG, 0 },
  4187.     { "/isodate",     DIR_ISO, 0 },
  4188.     { "/larger-than", DIR_LAR, CM_ARG },
  4189. #ifdef CKSYMLINK
  4190.     { "/followlinks", DIR_LNK, 0 },
  4191. #endif /* CKSYMLINK */
  4192.     { "/message",     DIR_MSG, CM_ARG },
  4193.     { "/nobackupfiles",DIR_NOB, 0 },
  4194.     { "/nodotfiles",  DIR_NOD, 0 },
  4195. #ifdef CKSYMLINK
  4196.     { "/nofollowlinks",DIR_NLK, 0 },
  4197. #endif /* CKSYMLINK */
  4198.     { "/noheading",   DIR_NOH, 0 },
  4199.     { "/nomessage",   DIR_NOM, 0 },
  4200. #ifdef CK_TTGWSIZ
  4201.     { "/nopage",      DIR_NOP, 0 },
  4202. #endif /* CK_TTGWSIZ */
  4203. #ifdef RECURSIVE
  4204.     { "/norecursive", DIR_NOR, 0 },
  4205. #else
  4206. #ifdef VMS
  4207.     { "/norecursive", DIR_NOR, 0 },
  4208. #else
  4209. #ifdef datageneral
  4210.     { "/norecursive", DIR_NOR, 0 },
  4211. #endif /* datageneral */
  4212. #endif /* VMS */
  4213. #endif /* RECURSIVE */
  4214.     { "/nosort",      DIR_NOS, 0 },
  4215.     { "/not-after",   DIR_NAF, CM_ARG },
  4216.     { "/not-before",  DIR_NBF, CM_ARG },
  4217.     { "/not-since",   DIR_NAF, CM_INV|CM_ARG },
  4218.     { "/noxfermode",  DIR_NOT, 0 },
  4219.     { "/output",      DIR_OUT, CM_ARG },
  4220. #ifdef CK_TTGWSIZ
  4221.     { "/page",        DIR_PAG, 0 },
  4222. #endif /* CK_TTGWSIZ */
  4223. #ifdef RECURSIVE
  4224.     { "/recursive",   DIR_REC, 0 },
  4225. #else
  4226. #ifdef VMS
  4227.     { "/recursive",   DIR_REC, 0 },
  4228. #else
  4229. #ifdef datageneral
  4230.     { "/recursive",   DIR_REC, 0 },
  4231. #endif /* datageneral */
  4232. #endif /* VMS */
  4233. #endif /* RECURSIVE */
  4234.     { "/reverse",     DIR_DSC, 0 },
  4235.     { "/since",       DIR_AFT, CM_ARG|CM_INV },
  4236.     { "/smaller-than",DIR_SMA, CM_ARG },
  4237.     { "/sort",        DIR_SRT, CM_ARG },
  4238.     { "/summary",     DIR_SUM, 0 },
  4239.     { "/type",        DIR_BIN, CM_ARG },
  4240.     { "/xfermode",    DIR_TYP, 0 },
  4241.     { "/verbose",     DIR_VRB, 0 },
  4242.     { "",0,0 }
  4243. };
  4244. static int ndirswtab = (sizeof(dirswtab) / sizeof(struct keytab)) - 1;
  4245.  
  4246. static struct keytab dirsort[] = {    /* DIRECTORY /SORT: options */
  4247.     { "date",         DIRS_DT, 0 },
  4248.     { "name",         DIRS_NM, 0 },
  4249.     { "size",         DIRS_SZ, 0 }
  4250. };
  4251. static int ndirsort = (sizeof(dirsort) / sizeof(struct keytab));
  4252.  
  4253. static int dir_date = -1;        /* Option defaults (-1 means none) */
  4254. static int dir_page = -1;
  4255. static int dir_verb =  1;
  4256. static int dir_msg  = -1;
  4257. #ifdef VMS
  4258. static int dir_sort = -1;        /* Names are already sorted in VMS */
  4259. static int dir_rvrs = -1;
  4260. #else
  4261. static int dir_sort =  1;        /* Sort by default */
  4262. static int dir_rvrs =  0;        /* Not in reverse */
  4263. #endif /* VMS */
  4264. static int dir_skey = DIRS_NM;        /* By name */
  4265. #ifdef RECURSIVE
  4266. static int dir_recu = -1;
  4267. #endif /* RECURSIVE */
  4268. static int dir_mode = -1;
  4269. static int dir_show = -1;        /* Show all files by default */
  4270. int dir_dots =  0;            /* Except dot files */
  4271. int dir_back =  1;
  4272. int dir_head =  0;
  4273. static char * dirmsg = NULL;
  4274. static int dirmsglen = 0;
  4275.  
  4276. #ifndef NOSHOW
  4277. VOID
  4278. showdiropts() {
  4279.     int x = 0;
  4280.     extern int optlines;
  4281.     prtopt(&optlines,"DIRECTORY");
  4282.     if (dir_show > 0) {
  4283.     prtopt(&optlines,(dir_show == 1) ? "/FILES" :
  4284.            ((dir_show == 2) ? "/DIRECTORIES" : "/ALL"));
  4285.     x++;
  4286.     } else {
  4287.     prtopt(&optlines,"/ALL");
  4288.     x++;
  4289.     }
  4290.     if (dir_verb > -1) {
  4291.     prtopt(&optlines,dir_verb ? "/VERBOSE" : "/BRIEF");
  4292.     x++;
  4293.     }
  4294.     if (dir_page > -1) {
  4295.     prtopt(&optlines,dir_page ? "/PAGE" : "/NOPAGE");
  4296.     x++;
  4297.     }
  4298.     if (dir_date > -1) {
  4299.     prtopt(&optlines,dir_date ? "/ENGLISHDATE" : "/ISODATE");
  4300.     x++;
  4301.     }
  4302.     if (dir_dots > -1) {
  4303.     prtopt(&optlines,dir_dots ? "/DOTFILES" : "/NODOTFILES");
  4304.     x++;
  4305.     }
  4306.     if (dir_back > -1) {
  4307.     prtopt(&optlines,dir_back ? "/BACKUP" : "/NOBACKUP");
  4308.     x++;
  4309.     }
  4310.     if (dir_head > -1) {
  4311.     prtopt(&optlines,dir_head ? "/HEADING" : "/NOHEADING");
  4312.     x++;
  4313.     }
  4314. #ifdef RECURSIVE
  4315.     if (dir_recu > -1) {
  4316.     prtopt(&optlines,dir_recu ? "/RECURSIVE" : "/NORECURSIVE");
  4317.     x++;
  4318.     }
  4319. #endif /* RECURSIVE */
  4320.     if (dir_mode > -1) {
  4321.     prtopt(&optlines,dir_mode ? "/XFERMODE" : "/NOXFERMODE");
  4322.     x++;
  4323.     }
  4324.     if (dir_sort == 0) {
  4325.     x++;
  4326.     prtopt(&optlines,"/NOSORT ");
  4327.     } else if (dir_sort > 0) {
  4328.     x++;
  4329.     if (dir_skey == DIRS_NM) s = "/SORT:NAME";
  4330.     else if (dir_skey == DIRS_SZ) s = "/SORT:SIZE";
  4331.     else if (dir_skey == DIRS_DT) s = "/SORT:DATE";
  4332.     prtopt(&optlines,s);
  4333.     }
  4334.     if (dir_rvrs > -1) {
  4335.     prtopt(&optlines,dir_rvrs ? "/REVERSE" : "/ASCENDING");
  4336.     x++;
  4337.     }
  4338.     if (dir_msg > -1) {
  4339.     if (dir_msg == 0) {
  4340.         prtopt(&optlines,"/NOMESSAGE");
  4341.     } else {
  4342.         ckmakmsg(tmpbuf,TMPBUFSIZ,"/MESSAGE:{",dirmsg,"}",NULL);
  4343.         prtopt(&optlines,tmpbuf);
  4344.     }
  4345.     x++;
  4346.     }
  4347.     if (!x) prtopt(&optlines,"(no options set)");
  4348.     prtopt(&optlines,"");
  4349. }
  4350. #endif /* NOSHOW */
  4351.  
  4352. int
  4353. setdiropts() {                /* Set DIRECTORY option defaults */
  4354.     int xb = -1, xv = -1, xp = -1, xd = -1, xh = -1, xf = -1;
  4355.     int xk = -1, xr = -1, xs = -1, xx = -1, xm = -1, xa = -1, xg = -1;
  4356.     int getval;
  4357.     char c;
  4358.     while (1) {
  4359.     if ((y = cmswi(dirswtab,ndirswtab,"Switch","",xxstring)) < 0) {
  4360.         if (y == -3)
  4361.           break;
  4362.         else
  4363.           return(y);
  4364.     }
  4365.     c = cmgbrk();
  4366.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4367.         printf("?This switch does not take an argument\n");
  4368.         return(-9);
  4369.     }
  4370.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  4371.         printf("?This switch requires an argument\n");
  4372.         return(-9);
  4373.     }
  4374.     switch (y) {
  4375.       case DIR_BRF: xv = 0; break;
  4376.       case DIR_VRB: xv = 1; break;
  4377.       case DIR_PAG: xp = 1; break;
  4378.       case DIR_NOP: xp = 0; break;
  4379.       case DIR_ISO: xd = 0; break;
  4380.       case DIR_DAT: xd = 1; break;
  4381.       case DIR_HDG: xh = 1; break;
  4382.       case DIR_NOH: xh = 0; break;
  4383.       case DIR_DOT: xf = 1; break;
  4384.       case DIR_NOD: xf = 0; break;
  4385.       case DIR_ALL: xa = 3; break;
  4386.       case DIR_DIR: xa = 2; break;
  4387.       case DIR_FIL: xa = 1; break;
  4388.       case DIR_SRT:
  4389.         x = DIRS_NM;
  4390.         if (getval)
  4391.           if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4392.         return(x);
  4393.         xk = x;
  4394.         xs = 1;
  4395.         break;
  4396.       case DIR_NOS: xs = 0; break;
  4397.       case DIR_ASC: xx = 0; break;
  4398.       case DIR_DSC: xx = 1; break;
  4399.       case DIR_REC: xr = 1; break;
  4400.       case DIR_NOR: xr = 0; break;
  4401.       case DIR_TYP: xm = 1; break;
  4402.       case DIR_NOT: xm = 0; break;
  4403.       case DIR_BUP: xb = 1; break;
  4404.       case DIR_NOB: xb = 0; break;
  4405.       case DIR_NOM: xg = 0; break;
  4406.       case DIR_MSG:
  4407.         if (getval)
  4408.           if ((x = cmfld("Message to append to each line",
  4409.                  "",
  4410.                  &s,
  4411.                  xxstring
  4412.                  )) < 0)
  4413.         return(x);
  4414.         xg = 1;
  4415.         ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4416.         break;
  4417.       default:
  4418.         printf("?This option can not be set\n");
  4419.         return(-9);
  4420.     }
  4421.     }
  4422.     if ((x = cmcfm()) < 0)        /* Get confirmation */
  4423.       return(x);
  4424.     if (xv > -1) dir_verb = xv;        /* Confirmed, save defaults */
  4425.     if (xp > -1) dir_page = xp;
  4426.     if (xd > -1) dir_date = xd;
  4427.     if (xh > -1) dir_head = xh;
  4428.     if (xs > -1) dir_sort = xs;
  4429.     if (xk > -1) dir_skey = xk;
  4430.     if (xx > -1) dir_rvrs = xx;
  4431.     if (xf > -1) dir_dots = xf;
  4432.     if (xa > -1) dir_show = xa;
  4433.     if (xm > -1) dir_mode = xm;
  4434.     if (xb > -1) dir_back = xb;
  4435. #ifdef RECURSIVE
  4436.     if (xr > -1) dir_recu = xr;
  4437. #endif /* RECURSIVE */
  4438.     if (xg > -1) dir_msg  = xg;
  4439.     if (xg > 0)
  4440.       makestr(&dirmsg,tmpbuf);
  4441.     return(success = 1);
  4442. }
  4443.  
  4444. int
  4445. domydir() {                /* Internal DIRECTORY command */
  4446.     extern char *months[];
  4447. #ifdef VMS
  4448.     _PROTOTYP( char * zrelname, (char *,char *) );
  4449.     char * cdp = NULL;
  4450. #endif /* VMS */
  4451.  
  4452.     char name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p = NULL, c = NUL;
  4453.     char linebuf[CKMAXPATH+256];
  4454.     char * mstr = NULL, * dstr = NULL, * s2 = NULL;
  4455.     long len = 0, ndirs = 0, nfiles = 0, nbytes = 0, nmatches = 0;
  4456.     int verbose = 0, wild = 0, page = 0, n = 0, engdate = 0, summary = 0;
  4457.     int heading = 0, xsort = 0, reverse = 0, sortby = 0, msg = 0;
  4458.     int k, i = 0, x = 0, nx = 0, skey = 0, dlen = 0, itsadir = 0;
  4459.     int show = 3, xfermod = 0, backup = 1, rc = 0, getval = 0;
  4460.     int fs = 0;
  4461.     int cmifn1 = 1, cmifn2 = 0;
  4462.     long minsize = -1L, maxsize = -1L;
  4463.     struct FDB sw, fi, fl;
  4464.     char dbuf[32], xbuf[32];
  4465.  
  4466. #ifndef NOSPL
  4467.     char array = NUL;
  4468.     char ** ap = NULL;
  4469. #endif /* NOSPL */
  4470.     char
  4471.       * dir_aft = NULL,
  4472.       * dir_bef = NULL,
  4473.       * dir_naf = NULL,
  4474.       * dir_nbf = NULL,
  4475.       * dir_exc = NULL;
  4476.     char * xlist[8];
  4477.  
  4478.     g_matchdot = matchdot;        /* Save global matchdot setting */
  4479.     nolinks = 2;            /* (it should already be 2) */
  4480.     outfile[0] = NUL;            /* No output file yet */
  4481.  
  4482.     if (ofp != stdout) {        /* In case of previous interruption */
  4483.     if (ofp) fclose(ofp);
  4484.     ofp = stdout;
  4485.     }
  4486.     for (i = 0; i < 8; i++) xlist[i] = NULL;
  4487.  
  4488.     name[0] = NUL;
  4489.     freedirlist();            /* In case not freed last time */
  4490.     page      = dir_page > -1 ? dir_page : xaskmore; /* Set option defaults */
  4491.     engdate   = dir_date > -1 ? dir_date : 0;
  4492.     verbose   = dir_verb > -1 ? dir_verb : 1;
  4493.     heading   = dir_head > -1 ? dir_head : 0;
  4494.     xsort     = dir_sort > -1 ? dir_sort : 0;
  4495.     sortby    = dir_skey > -1 ? dir_skey : 0;
  4496.     reverse   = dir_rvrs > -1 ? dir_rvrs : 0;
  4497.     msg       = dir_msg  > -1 ? dir_msg  : 0;
  4498. #ifdef UNIXOROSK
  4499.     matchdot  = dir_dots > -1 ? dir_dots : 0;
  4500. #endif /* UNIXOROSK */
  4501.     xfermod   = dir_mode > -1 ? dir_mode : 0;
  4502.     backup    = dir_back > -1 ? dir_back : 1;
  4503. #ifdef RECURSIVE
  4504.     recursive = dir_recu > -1 ? dir_recu : 0;
  4505. #endif /* RECURSIVE */
  4506.     show      = dir_show > -1 ? dir_show : 3;
  4507.  
  4508. #ifdef CK_TTGWSIZ
  4509. #ifdef OS2
  4510.     ttgcwsz();                /* Screen length for more-prompting */
  4511. #else /* OS2 */
  4512.     /* Check whether window size changed */
  4513.     if (ttgwsiz() > 0) {
  4514.         if (tt_rows > 0 && tt_cols > 0) {
  4515.             cmd_rows = tt_rows;
  4516.             cmd_cols = tt_cols;
  4517.         }
  4518.     }
  4519. #endif /* OS2 */
  4520. #endif /* CK_TTGWSIZ */
  4521.  
  4522.     diractive = 1;
  4523.     cmifn1 = nolinks | 1;        /* 1 = files or directories */
  4524.     cmifn2 = 0;                /* 0 = not directories only */
  4525.  
  4526.   again:
  4527.  
  4528.     cmfdbi(&sw,                /* First FDB - command switches */
  4529.        _CMKEY,            /* fcode */
  4530.        "Enter or Return to confirm the command, or\n\
  4531.  file specification, or switch",
  4532.        "",                /* default */
  4533.        "",                /* addtl string data */
  4534.        ndirswtab,            /* addtl numeric data 1: tbl size */
  4535.        4,                /* addtl numeric data 2: 4 = cmswi */
  4536.        xxstring,            /* Processing function */
  4537.        dirswtab,            /* Keyword table */
  4538.        &fi                /* Pointer to next FDB */
  4539.        );
  4540.     cmfdbi(&fi,                /* 2nd FDB - file to delete */
  4541.        _CMIFI,            /* fcode */
  4542.        "File specification",    /* hlpmsg */
  4543. #ifdef datageneral
  4544.        "+",                /* Default filespec is wildcard */
  4545. #else                    /* that matches all files... */
  4546. #ifdef VMS
  4547.        "*.*",
  4548. #else
  4549.        "*",
  4550. #endif /* VMS */
  4551. #endif /* datageneral */
  4552.        "",                /* addtl string data */
  4553.        cmifn1,
  4554.        cmifn2,            /* 1 = only dirs; 0 files or dirs */
  4555.        xxstring,
  4556.        NULL,
  4557.        &fl
  4558.        );
  4559.     cmfdbi(&fl,                /* Anything that doesn't match */
  4560.        _CMFLD,            /* fcode */
  4561.        "",                /* hlpmsg */
  4562.        "",                /* default */
  4563.        "",                /* addtl string data */
  4564.        0,                /* addtl numeric data 1 */
  4565.        0,                /* addtl numeric data 2 */
  4566.        xxstring,
  4567.        NULL,
  4568.        NULL
  4569.        );
  4570.     while (1) {                /* Parse 0 or more switches */
  4571.     x = cmfdb(&sw);            /* Parse something */
  4572.     debug(F101,"domydir cmfdb","",x);
  4573.     if (x < 0)
  4574.       return(x);
  4575.     if (cmresult.fcode != _CMKEY)    /* Break out if not a switch */
  4576.       break;
  4577.     c = cmgbrk();
  4578.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4579.         printf("?This switch does not take an argument\n");
  4580.         return(-9);
  4581.     }
  4582.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  4583.         printf("?This switch requires an argument\n");
  4584.         return(-9);
  4585.     }
  4586.     switch (k = cmresult.nresult) {
  4587.       case DIR_BRF: verbose = 0; break;
  4588.       case DIR_VRB: verbose = 1; break;
  4589. #ifdef CK_TTGWSIZ
  4590.       case DIR_PAG: page = 1;    break;
  4591.       case DIR_NOP: page = 0;    break;
  4592. #endif /* CK_TTGWSIZ */
  4593.           case DIR_ISO: engdate = 0; break;
  4594.       case DIR_DAT: engdate = 1; break;
  4595.       case DIR_HDG: heading = 1; break;
  4596.           case DIR_NOH: heading = 0; break;
  4597. #ifdef UNIXOROSK
  4598.       case DIR_DOT: matchdot = 1; break;
  4599.           case DIR_NOD: matchdot = 0; break;
  4600. #endif /* UNIXOROSK */
  4601.       case DIR_ALL:
  4602.         show = 3;
  4603.         cmifn1 |= 1;
  4604.         cmifn2 = 0;
  4605.         goto again;
  4606.       case DIR_DIR:
  4607.         show = 2;
  4608.         cmifn1 |= 1;
  4609.         cmifn2 = 1;
  4610.         goto again;
  4611.       case DIR_FIL:
  4612.         show = 1;
  4613.         cmifn1 &= ~(1);
  4614.         cmifn2 = 0;
  4615.         goto again;
  4616.           case DIR_SRT:
  4617.         x = DIRS_NM;
  4618.         if (c == ':' || c == '=')
  4619.           if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4620.         return(x);
  4621.         xsort = 1;
  4622.         sortby = x;
  4623.         break;
  4624.  
  4625.       case DIR_BUP: backup  = 1; fs++;   break;
  4626.       case DIR_NOB: backup  = 0; fs++;   break;
  4627.  
  4628.           case DIR_NOS: xsort = 0;     break;
  4629.       case DIR_ASC: reverse = 0;   break;
  4630.       case DIR_DSC: reverse = 1;   break;
  4631. #ifdef RECURSIVE
  4632.       case DIR_REC: recursive = 1; diractive = 1; break;
  4633.       case DIR_NOR: recursive = 0; diractive = 0; break;
  4634. #endif /* RECURSIVE */
  4635.       case DIR_TYP: xfermod = 1;   break;
  4636.       case DIR_NOT: xfermod = 0;   break;
  4637.  
  4638. #ifdef CKSYMLINK
  4639.       case DIR_LNK:            /* Follow links */
  4640.         nolinks = 0; 
  4641.         cmifn1 &= ~(2);
  4642.         goto again;
  4643.       case DIR_NLK:            /* Don't follow links */
  4644.         nolinks = 2;
  4645.         cmifn1 &= ~(2);
  4646.         goto again;
  4647. #endif /* CKSYMLINK */
  4648.  
  4649.       case DIR_NOM: msg     = 0;   break;
  4650.       case DIR_MSG:
  4651.         if (c == ':' || c == '=')
  4652.           if ((x = cmfld("Message to append to each line",
  4653.                  "",
  4654.                  &s,
  4655.                  xxstring
  4656.                  )) < 0)
  4657.         return(x);
  4658.         msg = 1;
  4659.         ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4660.         break;
  4661.  
  4662.       case DIR_SMA:
  4663.       case DIR_LAR:
  4664.         if (!getval) break;
  4665.         if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  4666.           return(x);
  4667.         fs++;
  4668.         show = 1;
  4669.         switch (cmresult.nresult) {
  4670.           case DIR_SMA: minsize = y; break;
  4671.           case DIR_LAR: maxsize = y; break;
  4672.         }
  4673.         break;
  4674.  
  4675. #ifndef NOSPL
  4676.       case DIR_ARR:
  4677.         if (c != ':' && c != '=') {
  4678.         printf("?Array name required\n");
  4679.         return(-9);
  4680.         }
  4681.         if ((x = cmfld("Array name (a single letter will do)",
  4682.                "",
  4683.                &s,
  4684.                NULL
  4685.                )) < 0) {
  4686.         if (x == -3) {
  4687.             printf("?Array name required\n");
  4688.             return(-9);
  4689.         } else
  4690.           return(x);
  4691.         }
  4692.         if (!*s) {
  4693.         printf("?Array name required\n");
  4694.         return(-9);
  4695.         }
  4696.         s2 = s;
  4697.         if (*s == CMDQ) s++;
  4698.         if (*s == '&') s++;
  4699.         if (!isalpha(*s)) {
  4700.         printf("?Bad array name - \"%s\"\n",s2);
  4701.         return(-9);
  4702.         }
  4703.         array = *s++;
  4704.         if (isupper(array)) array = tolower(array);
  4705.         if (*s && (*s != '[' || *(s+1) != ']')) {
  4706.         printf("?Bad array name - \"%s\"\n",s2);
  4707.         return(-9);
  4708.         }
  4709.         break;
  4710. #endif /* NOSPL */
  4711.       case DIR_AFT:
  4712.       case DIR_BEF:
  4713.       case DIR_NAF:
  4714.       case DIR_NBF:
  4715.         if (!getval) break;
  4716.         if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  4717.         if (x == -3) {
  4718.             printf("?Date-time required\n");
  4719.             rc = -9;
  4720.         } else
  4721.           rc = x;
  4722.         goto xdomydir;
  4723.         }
  4724.         fs++;
  4725.         switch (k) {
  4726.           case DIR_AFT: makestr(&dir_aft,s); break;
  4727.           case DIR_BEF: makestr(&dir_bef,s); break;
  4728.           case DIR_NAF: makestr(&dir_naf,s); break;
  4729.           case DIR_NBF: makestr(&dir_nbf,s); break;
  4730.         }
  4731.         break;
  4732.       case DIR_EXC:
  4733.         if (!getval) break;
  4734.         if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4735.         if (x == -3) {
  4736.             printf("?Pattern required\n");
  4737.             rc = -9;
  4738.         } else
  4739.           rc = x;
  4740.         goto xdomydir;
  4741.         }
  4742.         fs++;
  4743.         makestr(&dir_exc,s);
  4744.         break;
  4745.  
  4746.       case DIR_SUM:
  4747.         summary = 1; break;
  4748.  
  4749.       case DIR_BIN: {
  4750.           extern struct keytab txtbin[];
  4751.           extern int xfiletype;
  4752.           if (!getval) break;
  4753.           if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0) {
  4754.           rc = x;
  4755.           goto xdomydir;
  4756.           }
  4757.           if (x == 2) {
  4758.           xfiletype = -1;
  4759.           } else {
  4760.           xfiletype = x;
  4761.           fs = 1;
  4762.           }
  4763.           break;
  4764.       }
  4765.       case DIR_OUT:
  4766.         if ((x = cmofi("File for directory listing","",&s,xxstring)) < 0)
  4767.           return(x);
  4768.         ckstrncpy(outfile,s,CKMAXPATH+1);
  4769.         break;
  4770.  
  4771.       default:
  4772.         printf("?Sorry, not implemented yet - \"%s\"\n", atmbuf);
  4773.         goto xdomydir;
  4774.     }
  4775.     }
  4776.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Safe copy of filespec */
  4777.     s = line;
  4778.     if ((x = cmcfm()) < 0)        /* Get confirmation */
  4779.       return(x);
  4780.     if (cmresult.fcode != _CMIFI) {    /* Nothing matched */
  4781.     char * m;
  4782.     if (*s == '/')
  4783. #ifdef UNIXOROSK
  4784.       m = "does not match switch or name of accessible file";
  4785. #else
  4786. #ifdef OS2
  4787.       m = "does not match switch or name of accessible file";
  4788. #else
  4789.       m = "no switches match";
  4790. #endif /* OS2 */
  4791. #endif /* UNIXOROSX */
  4792.     else
  4793.       m = "not found or not accessible";
  4794.     printf("\"%s\" - %s\n",s,m);
  4795.     rc = -9;
  4796.     goto xdomydir;
  4797.     }
  4798.     wild = cmresult.nresult;        /* Wildcard was given? */
  4799.     debug(F111,"domydir cmifi2",s,wild);
  4800.  
  4801.     if (outfile[0]) {
  4802.     ofp = fopen(outfile,"w");    /* Open output file */
  4803.     if (!ofp) {
  4804.         printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  4805.         ofp = stdout;
  4806.         return(-9);
  4807.     }
  4808.     page = 0;
  4809.     }
  4810.  
  4811. #ifdef OS2
  4812.     if (!wild) {
  4813.     if (zchki(s) == -2) {        /* Found a directory */
  4814.         p = s + (int)strlen(s) - 1;    /* Yes */
  4815.         if (*p == '\\' || *p == '/')
  4816.           strcat(s, "*");
  4817.         else if (*p == ':')
  4818.           strcat(s, "./*");
  4819.         else
  4820.           strcat(s, "/*");
  4821.         wild = 1;            /* Now it's wild */
  4822.     }
  4823.     }
  4824. #else
  4825.     if (!wild) if (isdir(s)) {        /* Is it a directory? */
  4826.     p = s + (int)strlen(s) - 1;    /* Yes */
  4827. #ifdef VMS
  4828.     {
  4829.         /* Convert from FOO.DIR;1 to [x.FOO] if necessary */
  4830.         char buf[CKMAXPATH+1];
  4831.         debug(F000,"domydir directory 0",s,*p);
  4832.         if (cvtdir(s,buf,CKMAXPATH) > 0)
  4833.           ckstrncpy(line,buf,LINBUFSIZ);
  4834.     }
  4835. #endif /* VMS */
  4836.     debug(F000,"domydir directory 1",s,*p);
  4837. #ifdef VMS
  4838.     if (*p == ']' || *p == '>' || *p == ':')
  4839.       strcat(s, "*.*");
  4840. #else
  4841. #ifdef datageneral
  4842.     if (*p == ':')
  4843.       strcat(s, "+");
  4844.     else
  4845.       strcat(s, ":+");
  4846. #else
  4847. #ifdef STRATUS
  4848.     if (*p == '>')
  4849.       strcat(s, "*");
  4850.     else
  4851.       strcat(s, ">*");
  4852. #endif /* STRATUS */
  4853. #endif /* datageneral */
  4854. #endif /* VMS */
  4855.     wild = 1;            /* Now it's wild */
  4856.     debug(F000,"domydir directory 2",s,*p);
  4857.     }
  4858. #endif /* OS2 */
  4859.  
  4860. #ifdef ZXREWIND
  4861. /* cmifi() already called nzxpand so we can just re-use the same list. */
  4862.     x = zxrewind();            /* Rewind the list */
  4863.     debug(F111,"domydir zxrewind",s,x);
  4864. #else
  4865.     nzxopts = (show == ZX_DIRONLY) ? ZX_DIRONLY :
  4866.       (show == ZX_FILONLY ? ZX_FILONLY : 0);
  4867.     if (matchdot)  nzxopts |= ZX_MATCHDOT;
  4868.     if (recursive) nzxopts |= ZX_RECURSE;
  4869.     x = nzxpand(s,nzxopts);        /* Expand file list */
  4870.     debug(F111,"domydir nzxpand",s,x);
  4871. #endif /* ZXREWIND */
  4872. #ifndef NOSPL
  4873.     if (array) {
  4874.     int n, xx;
  4875.     n = (x < 0) ? 0 : x;
  4876.     if ((xx = dclarray(array,n)) < 0) {
  4877.         printf("?Array declaration failure\n");
  4878.         rc = -9;
  4879.         goto xdomydir;
  4880.     }
  4881.     array = xx;
  4882.     ap = a_ptr[array];
  4883.     if (n < 1) {
  4884.         rc = 0;
  4885.         goto xdomydir;
  4886.     }
  4887.     } else
  4888. #endif /* NOSPL */
  4889.       if (x < 1) {
  4890. #ifdef CKROOT
  4891.       extern int ckrooterr;
  4892.       if (ckrooterr)
  4893.         printf("?Off limits: %s\n",s);
  4894.       else
  4895. #endif /* CKROOT */
  4896.         if (x == 0 && isdir(s))
  4897.           printf("?Empty directory - \"%s\"\n", s);
  4898.         else
  4899.           printf("?%s %s match - \"%s\"\n",
  4900.              (x == 0) ? "No" : "Too many",
  4901.              (show == 2) ? "directories" : "files",
  4902.              s
  4903.              );
  4904.       rc = -9;
  4905.       goto xdomydir;
  4906.     }
  4907.     nx = x;                /* Remember how many files */
  4908.  
  4909.     if (msg) {
  4910.     makestr(&dirmsg,tmpbuf);
  4911.     dirmsglen = strlen(tmpbuf);
  4912.     }
  4913.  
  4914. #ifdef VMS
  4915.     cdp = zgtdir();            /* Get current directory */
  4916.     debug(F110,"domydir VMS zgtdir",cdp,0);
  4917. #endif /* VMS */
  4918.  
  4919.     if (xsort && verbose) {        /* If sorting, allocate space */
  4920.     if (!(dirlist = (char **) malloc((x + 1) * sizeof(char **)))) {
  4921.         if (!quiet) {
  4922.         printf("* Warning: Failure to allocate memory for sorting.\n");
  4923.         printf("* Will proceed without sorting...\n");
  4924.         }
  4925.         xsort = 0;
  4926.     }
  4927.     debug(F101,"domydir sort malloc","",xsort);
  4928.     }
  4929.  
  4930.     /* Display the listing */
  4931.  
  4932. #ifndef NOSPL
  4933.     if (array)                /* Storing instead of printing */
  4934.       heading = 0;
  4935. #endif /* NOSPL */
  4936.  
  4937.     if (heading) {            /* If /HEADING print heading */
  4938.     zfnqfp(s,TMPBUFSIZ,tmpbuf);
  4939.     fprintf(ofp,"\nDirectory of %s\n\n",tmpbuf);
  4940.     n += 3;
  4941.     }
  4942.     if (page > -1)            /* Paging */
  4943.       xaskmore = page;
  4944.  
  4945.     if (!verbose) {            /* /BRIEF */
  4946.     if (outfile[0]) {        /* To file  */
  4947.         int k = 0;
  4948.         znext(name);
  4949.         while (name[0]) {        /* One line per file */
  4950.         k++;
  4951.         if (fs) if (fileselect(name,
  4952.                        dir_aft,dir_bef,dir_naf,dir_nbf,
  4953.                        minsize,maxsize,!backup,8,xlist) < 1) {
  4954.             znext(name);
  4955.             continue;
  4956.         }
  4957.         fprintf(ofp,"%s\n",name);
  4958.         znext(name);
  4959.         }
  4960.         if (heading)
  4961.           fprintf(ofp,"Files: %d\n\n",k);
  4962.         rc = 1;
  4963.         goto xdomydir;
  4964.     } else {
  4965.         rc = filhelp(x,"","",n,0);
  4966.         if (rc < 0)
  4967.           goto xdomydir;
  4968.         if (heading && rc > 0)
  4969.           fprintf(ofp,"Files: %d\n\n",x); /* (Might scroll a line or 2) */
  4970.         rc = 1;
  4971.         goto xdomydir;
  4972.     }
  4973.     }
  4974.     ndirs = nfiles = nbytes = 0L;    /* Initialize counters */
  4975.  
  4976.     if (dir_exc)            /* Have exception list? */
  4977.       makelist(dir_exc,xlist,8);    /* Yes, convert to array */
  4978.  
  4979.     diractive = 1;
  4980.     znext(name);            /* Get next file */
  4981.     while (name[0]) {            /* Loop for each file */
  4982.     if (fs) if (fileselect(name,
  4983.                dir_aft,dir_bef,dir_naf,dir_nbf,
  4984.                minsize,maxsize,!backup,8,xlist) < 1) {
  4985.         znext(name);
  4986.         continue;
  4987.     }
  4988.     len = zgetfs(name);        /* Get file length */
  4989.     debug(F111,"domydir zgetfs",name,len);
  4990. #ifdef VMSORUNIX
  4991.     itsadir = zgfs_dir;        /* See if it's a directory */
  4992. #else
  4993.     itsadir = (len == -2 || isdir(name));
  4994. #endif /* VMSOUNIX */
  4995.     debug(F111,"domydir itsadir",name,itsadir);
  4996.     if ((itsadir && (show == 1)) || (!itsadir && (show == 2))) {
  4997.         znext(name);
  4998.         continue;
  4999.     }
  5000.     /* Get here when we know we have selected this file */
  5001.  
  5002.     nmatches ++;
  5003.     if (itsadir) {            /* Accumulate totals for summary */
  5004.         ndirs++;
  5005.     } else {
  5006.         nfiles++;
  5007.         nbytes += len;
  5008.     }
  5009.     if (summary) {            /* Summary only, no detail */
  5010.         znext(name);
  5011.         continue;
  5012.     }
  5013. #ifndef NOSPL
  5014.     if (array) {
  5015.         debug(F111,"domydir array",name,nfiles);
  5016.         if (ap)
  5017.           makestr(&(ap[nmatches]),name);
  5018.         znext(name);
  5019.         continue;
  5020.     }
  5021. #endif /* NOSPL */
  5022.  
  5023. /*
  5024.   NOTE: The sprintf's in this routine should be safe.  They involve
  5025.   permission strings, date/time strings, and filenames, all of which have
  5026.   known maximum lengths; none of these items is input from users.  The
  5027.   destination buffers are large enough to hold maximum sizes for any and
  5028.   all items.
  5029. */
  5030.     dstr = zfcdat(name);        /* Get modification date/time */
  5031.     debug(F111,"domydir zcfdat",dstr,0);
  5032.     if (!dstr) dstr = "";
  5033.     {
  5034. /*
  5035.   Note that zfcdat() always returns "" or yyyymmdd hh:mm:ss, so any warnings
  5036.   about possible out-of-bounds dstr[] array refs do not apply.  This block of
  5037.   code is to stifle the warnings and also allows for any out-of-spec
  5038.   zfcdat() implementations.
  5039. */
  5040.         int x;
  5041.         char * p = "00000000 00:00:00";
  5042.         x = ckstrncpy(xbuf,dstr,32);
  5043.         if (x < 17) ckstrncpy(&xbuf[x],p+x,32-x);
  5044.         dstr = xbuf;
  5045.     }
  5046.     if (engdate) {            /* English date requested? */
  5047.         short month, day, year, hour, minute, seconds;
  5048.         month = (dstr[4]-48)*10 + (dstr[5]-48);
  5049.         mstr  = (month > 0 && month <= 12) ? months[month-1] : "xxx";
  5050.         day   = (dstr[6]-48)*10 + (dstr[7]-48);
  5051.         year  = (((dstr[0]-48)*10 +
  5052.               (dstr[1]-48))*10 +
  5053.               (dstr[2]-48))*10 +
  5054.               (dstr[3]-48);
  5055.         hour  = (dstr[9]-48)*10 + (dstr[10]-48);
  5056.         minute = (dstr[12]-48)*10 + (dstr[13]-48);
  5057.         seconds = (dstr[15]-48)*10 + (dstr[16]-48);
  5058.         sprintf(dbuf,        /* SAFE */
  5059.             "%2d-%s-%4d %02d:%02d:%02d",
  5060.             day,mstr,year,hour,minute,seconds
  5061.             );
  5062.         dstr = dbuf;
  5063.     } else {            /* ISO date */
  5064.         dbuf[0] = dstr[0];        /* yyyy */
  5065.         dbuf[1] = dstr[1];
  5066.         dbuf[2] = dstr[2];
  5067.         dbuf[3] = dstr[3];
  5068.         dbuf[4] = '-';
  5069.         dbuf[5] = dstr[4];        /* mm (numeric) */
  5070.         dbuf[6] = dstr[5];
  5071.         dbuf[7] = '-';
  5072.         dbuf[8] = dstr[6];        /* dd */
  5073.         dbuf[9] = dstr[7];
  5074.         strcpy(dbuf+10,dstr+8);    /* hh:mm:ss */
  5075.         dstr = dbuf;
  5076.     }
  5077.     dlen = strlen(dbuf);        /* Length of date */
  5078.     name[CKMAXPATH] = NUL;
  5079. #ifdef CK_PERMS
  5080. #ifdef VMSORUNIX
  5081.     p = ziperm(name);        /* Get permissions */
  5082.     debug(F110,"ziperm perms",p,0);
  5083. #else
  5084.     p = zgperm(name);
  5085.     debug(F110,"zgperm perms",p,0);
  5086. #endif /* VMSORUNIX */
  5087. #else
  5088.     p = NULL;
  5089.     debug(F110,"NULL perms",p,0);
  5090. #endif /* CK_PERMS */
  5091.  
  5092. #ifdef VMS
  5093.     /* Get relative name to save space -- VMS fullnames are long... */
  5094.     ckstrncpy(name,zrelname(name,cdp),CKMAXPATH);
  5095. #endif /* VMS */
  5096.  
  5097.     if (itsadir && len < 0) {    /* Directory */
  5098. #ifdef VMS
  5099.         sprintf(linebuf,"%-22s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5100. #else
  5101.         if (p)
  5102.           sprintf(linebuf,"%10s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5103.         else
  5104.           sprintf(linebuf,"%-10s  %s  %s", "<DIR>", dstr, name);
  5105. #endif /* VMS */
  5106.     } else {            /* Regular file */
  5107. #ifdef VMS
  5108.         sprintf(linebuf,"%-22s%10ld  %s  %s", p, len, dstr, name);
  5109. #else
  5110.         if (p)
  5111.           sprintf(linebuf,"%10s%10ld  %s  %s", p, len, dstr, name);
  5112.         else
  5113.           sprintf(linebuf,"%10ld  %s  %s", len, dstr, name);
  5114. #endif /* VMS */
  5115.     }
  5116. #ifdef UNIX
  5117. #ifdef CKSYMLINK
  5118.     if (zgfs_link) {
  5119.         int n, m;
  5120.         extern char linkname[];
  5121.         n = strlen(linebuf);
  5122.         m = strlen(linkname) + n;
  5123.         if (m < CKMAXPATH + 58)
  5124.           strcpy(linebuf+n, " -> "); /* safe (checked) */
  5125.         if (m + 4 < CKMAXPATH - 58)
  5126.           strcpy(linebuf+n+4, linkname); /* safe (checked) */
  5127.     } else
  5128. #endif /* CKSYMLINK */
  5129. #endif /* UNIX */
  5130.     if (xfermod) {            /* Show transfer mode */
  5131.         int i, x, y;
  5132.         char * s = "";
  5133.         y = -1;
  5134.         x = scanfile(name,&y,nscanfile);
  5135.         switch (x) {
  5136.           case FT_TEXT: s = " (T)"; break;
  5137.           case FT_7BIT: s = " (T)(7BIT)"; break;
  5138.           case FT_8BIT: s = " (T)(8BIT)"; break;
  5139. #ifdef UNICODE
  5140.           case FT_UTF8: s = " (T)(UTF8)"; break;
  5141.           case FT_UCS2:
  5142.         s = y ? " (T)(UCS2LE)" : " (T)(UCS2BE)";
  5143.         break;
  5144. #endif /* UNICODE */
  5145.           case FT_BIN:  s = " (B)"; break;
  5146.         }
  5147.         if (!*s) {
  5148.         s = binary ? " (B)" : " (T)";
  5149.         }
  5150.         if (*s) {
  5151.         int n;
  5152.         n = strlen(linebuf);
  5153.         if (n + 4 < CKMAXPATH - 58)
  5154.           strcpy(linebuf+n, s); /* safe (checked) */
  5155.         }
  5156.     }
  5157.     if (msg && dirmsg) {
  5158.         int n;
  5159.         n = strlen(linebuf);
  5160.         if (n + dirmsglen + 2 < CKMAXPATH)
  5161.           sprintf((char *)(linebuf+n)," %s", dirmsg); /* SAFE */
  5162.     }
  5163.     if (xsort) {            /* Sorting - save line */
  5164.         i = strlen(linebuf);
  5165.         if ((ndirlist >= nx) ||
  5166.         !(dirlist[ndirlist] = (char *)malloc(i+1))) {
  5167.         printf("?Memory allocation error - try /NOSORT\n");
  5168.         rc = -9;
  5169.         goto xdomydir;
  5170.         }
  5171.         strcpy(dirlist[ndirlist],linebuf); /* safe */
  5172.         ndirlist++;
  5173.     }
  5174.     znext(name);            /* Peek ahead to next file */
  5175.  
  5176.     if (!xsort) {
  5177.         fprintf(ofp,"%s\n",linebuf);
  5178.         if (page && (name[0] || heading)) {    /* If /PAGE */
  5179.         if (cmd_cols > 0) {
  5180.             int x = strlen(linebuf);
  5181.             int y;
  5182.             y = (x % cmd_cols) ? 1 : 0;
  5183.             n += x / cmd_cols + y;
  5184.         } else {
  5185.             n++;
  5186.         }
  5187. #ifdef CK_TTGWSIZ
  5188.         if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5189.             if (!askmore()) {
  5190.             rc = 0;
  5191.             goto xdomydir;
  5192.             } else
  5193.               n = 0;
  5194.         }
  5195. #endif /* CK_TTGWSIZ */
  5196.         }
  5197.     }
  5198.     }
  5199. #ifndef NOSPL
  5200.     if (array) {
  5201.     if (ap)
  5202.       makestr(&(ap[0]),ckitoa(nmatches));
  5203.     rc = 1;
  5204.     goto xdomydir;
  5205.     }
  5206. #endif /* NOSPL */
  5207.     if (xsort) {
  5208.     skey = 0;
  5209. #ifdef VMS
  5210.     switch (sortby) {
  5211.       case DIRS_NM: skey = dlen + 35; break;
  5212.       case DIRS_DT: skey = 33; break;
  5213.       case DIRS_SZ: skey = 21;
  5214.     }
  5215. #else
  5216.     if (p) {
  5217.         switch (sortby) {
  5218.           case DIRS_NM: skey = dlen + 24; break;
  5219.           case DIRS_DT: skey = 22; break;
  5220.           case DIRS_SZ: skey = 10;
  5221.         }
  5222.     } else {
  5223.         switch (sortby) {
  5224.           case DIRS_NM: skey = dlen + 14; break;
  5225.           case DIRS_DT: skey = 12; break;
  5226.           case DIRS_SZ: skey = 0;
  5227.         }
  5228.     }
  5229. #endif /* VMS */
  5230.     sh_sort(dirlist,NULL,ndirlist,skey,reverse,filecase);
  5231.     for (i = 0; i < ndirlist; i++) {
  5232.         fprintf(ofp,"%s\n",dirlist[i]);
  5233.         if (page && (i < ndirlist -1 || heading)) {    /* If /PAGE */
  5234.         if (cmd_cols > 0) {
  5235.             int x = strlen(dirlist[i]);
  5236.             int y;
  5237.             y = (x % cmd_cols) ? 1 : 0;
  5238.             n += ((int)strlen(dirlist[i]) / cmd_cols) + y;
  5239.         } else {
  5240.             n++;
  5241.         }
  5242. #ifdef CK_TTGWSIZ
  5243.         if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5244.             if (!askmore()) {
  5245.             rc = 0;
  5246.             goto xdomydir;
  5247.             } else
  5248.               n = 0;
  5249.         }
  5250. #endif /* CK_TTGWSIZ */
  5251.         }
  5252.     }
  5253.     }
  5254.  
  5255.     if (heading || summary) {
  5256. #ifdef CKFLOAT
  5257.     CKFLOAT gm;
  5258. #endif /* CKFLOAT */
  5259.     fprintf(ofp,"\n%ld director%s, %ld file%s, %ld byte%s",
  5260.            ndirs,
  5261.            (ndirs == 1) ? "y" : "ies",
  5262.            nfiles,
  5263.            (nfiles == 1) ? "" : "s",
  5264.            nbytes,
  5265.            (nbytes == 1) ? "" : "s"
  5266.            );
  5267. #ifdef CKFLOAT
  5268.     gm = ((CKFLOAT) nbytes ) / 1000000.0;
  5269.     if (gm > 1000.0)
  5270.       fprintf(ofp," (%0.2fGB)",(gm / 1000.0));
  5271.     else if (gm >= 0.01)
  5272.       fprintf(ofp," (%0.2fMB)",gm);
  5273. #endif /* CKFLOAD */
  5274.     fprintf(ofp,"\n\n");
  5275.     }
  5276.   xdomydir:
  5277.     if (g_matchdot > -1) {
  5278.     matchdot = g_matchdot;        /* Restore these... */
  5279.     g_matchdot = -1;
  5280.     }
  5281.     freedirlist();
  5282.     if (ofp != stdout) {        /* Close any output file */
  5283.     if (ofp) fclose(ofp);
  5284.     ofp = stdout;
  5285.     }
  5286.     if (rc > 0)
  5287.       success = 1;
  5288.     return(rc);
  5289. }
  5290.  
  5291. int
  5292. dodir(cx) int cx; {            /* Do the DIRECTORY command */
  5293.     char *dc , *msg;
  5294.  
  5295. #ifdef OS2
  5296.     return(domydir());
  5297. #else /* OS2 */
  5298.     if (nopush
  5299. #ifdef DOMYDIR                /* Systems that domydir() by default */
  5300.     || cx == XXDIR
  5301. #endif /* DOMYDIR */
  5302.     )
  5303.       return(domydir());        /* Built-in directory command */
  5304.  
  5305.     /* Use the system's directory command. */
  5306.  
  5307.     msg = (cx == XXLS) ?
  5308.       "Arguments for ls" :
  5309.     "Directory and/or file specification";
  5310.     if ((x = cmtxt(msg,"",&s,xxstring)) < 0)
  5311.       return(x);
  5312.  
  5313.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);    /* Copy the filespec */
  5314.     s = tmpbuf;
  5315.  
  5316.     if ((y = cmcfm()) < 0) return(y);
  5317.  
  5318.     lp = line;
  5319.     if (!(dc = getenv("CK_DIR")))
  5320.       dc = DIRCMD;
  5321.     ckmakmsg(lp,LINBUFSIZ,dc," ",s,NULL);
  5322.     debug(F110,"DIR",line,0);
  5323. #ifdef VMS
  5324.     conres();
  5325. #endif /* VMS */
  5326.     x = zshcmd(line);
  5327. #ifdef VMS
  5328.     concb((char)escape);
  5329. #endif /* VMS */
  5330.     return(success = (x < 1) ? 0 : 1);
  5331. #endif /* OS2 */
  5332. }
  5333.  
  5334. #ifndef NOSERVER
  5335. #ifndef NOFRILLS
  5336. /* Do the ENABLE and DISABLE commands */
  5337.  
  5338. int
  5339. doenable(y,x) int y, x; {
  5340. #ifdef CK_LOGIN
  5341.     if (isguest)            /* IKSD: Don't let guests */
  5342.       return(0);            /* enable anything that's disabled */
  5343. #endif /* CK_LOGIN */
  5344.     switch (x) {
  5345.       case EN_ALL:
  5346.     en_cwd = en_cpy = en_del = en_dir = en_fin = en_get = y;
  5347.     en_ren = en_sen = en_set = en_spa = en_typ = en_ret = y;
  5348.         if (!inserver)
  5349.       en_who = en_mai = en_pri = y;
  5350.     en_mkd = en_rmd = y;
  5351.     en_xit = y;
  5352. #ifndef datageneral
  5353.         en_bye = y;
  5354. #endif /* datageneral */
  5355. #ifndef NOPUSH
  5356.     if (!nopush && !inserver)
  5357.       en_hos = y;
  5358. #endif /* NOPUSH */
  5359. #ifndef NOSPL
  5360.     en_asg = en_que = y;
  5361. #endif /* NOSPL */
  5362.     break;
  5363.  
  5364.       case EN_BYE:
  5365. #ifndef datageneral
  5366. /*
  5367.   In Data General AOS/VS Kermit can't log out its superior process.
  5368. */
  5369.         en_bye = y;
  5370. #endif /* datageneral */
  5371.     break;
  5372.       case EN_CPY:
  5373.         en_cpy = y;
  5374.         break;
  5375.       case EN_CWD:
  5376.     en_cwd = y;
  5377. #ifdef IKSD
  5378.     if (inserver && y == 0) {
  5379.         fnrpath = PATH_OFF;
  5380.         fnspath = PATH_OFF;
  5381.     }
  5382. #endif /* IKSD */
  5383.     break;
  5384.       case EN_DEL:            /* Deleting of files */
  5385.     en_del = y;
  5386.     break;
  5387.       case EN_DIR:
  5388.     en_dir = y;
  5389.     break;
  5390.       case EN_FIN:
  5391.     en_fin = y;
  5392.     break;
  5393.       case EN_GET:
  5394.     en_get = y;
  5395.     break;
  5396. #ifndef NOPUSH
  5397.       case EN_HOS:
  5398.     if (!nopush)
  5399.          en_hos = y;
  5400.     break;
  5401. #endif /* NOPUSH */
  5402.       case EN_REN:
  5403.         en_ren = y;
  5404.         break;
  5405.       case EN_SEN:
  5406.     en_sen = y;
  5407.     break;
  5408.       case EN_SET:
  5409.     en_set = y;
  5410.     break;
  5411.       case EN_SPA:
  5412.     en_spa = y;
  5413.     break;
  5414.       case EN_TYP:
  5415.     en_typ = y;
  5416.     break;
  5417.       case EN_WHO:
  5418.     en_who = y;
  5419.     break;
  5420. #ifndef NOSPL
  5421.       case EN_ASG:
  5422.     en_asg = y;
  5423.     break;
  5424.       case EN_QUE:
  5425.     en_que = y;
  5426.     break;
  5427. #endif /* NOSPL */
  5428.       case EN_RET:
  5429.     en_del = y;
  5430.     break;
  5431.       case EN_MAI:
  5432. #ifdef CK_LOGIN
  5433.     if (isguest && y) {
  5434.         printf("?Sorry, not valid for guests\n");
  5435.         return(-9);
  5436.     }
  5437. #endif /* CK_LOGIN */
  5438.     en_mai = y;
  5439.     break;
  5440.       case EN_PRI:
  5441. #ifdef CK_LOGIN
  5442.     if (isguest && y) {
  5443.         printf("?Sorry, not valid for guests\n");
  5444.         return(-9);
  5445.     }
  5446. #endif /* CK_LOGIN */
  5447.     en_pri = y;
  5448.     break;
  5449.       case EN_MKD:
  5450.     en_mkd = y;
  5451.     break;
  5452.       case EN_RMD:
  5453.     en_rmd = y;
  5454.     break;
  5455.       case EN_XIT:
  5456.     en_xit = y;
  5457.     break;
  5458.       case EN_ENA:
  5459.     if (((y & 1) && !(en_ena & 1)) ||
  5460.         ((y & 2) && !(en_ena & 2))) {
  5461.         printf("?Sorry, DISABLE ENABLE can not be undone\n");
  5462.         return(-9);
  5463.     } else {
  5464.         en_ena = y;
  5465.         break;
  5466.     }
  5467.       default:
  5468.     return(-2);
  5469.     }
  5470.     return(1);
  5471. }
  5472. #endif /* NOFRILLS */
  5473. #endif /* NOSERVER */
  5474.  
  5475. #ifndef NOFRILLS
  5476.  
  5477. static int del_lis = 0;
  5478. static int del_dot = 0;
  5479. static int del_hdg = 0;
  5480. static int del_pag = -1;
  5481. static int del_ask = 0;
  5482.  
  5483. #ifndef NOSHOW
  5484. VOID
  5485. showdelopts() {
  5486.     int x = 0;
  5487.     extern int optlines;
  5488.     prtopt(&optlines,"");
  5489.     prtopt(&optlines,"DELETE");
  5490.     if (del_ask > -1) {
  5491.     prtopt(&optlines, del_ask ? "/ASK" : "/NOASK");
  5492.     x++;
  5493.     }
  5494. #ifdef UNIXOROSK
  5495.     if (del_dot > -1) {
  5496.     prtopt(&optlines, del_dot ? "/DOTFILES" : "/NODOTFILES");
  5497.     x++;
  5498.     }
  5499. #endif /* UNIXOROSK */
  5500.     if (del_lis > -1) {
  5501.     prtopt(&optlines, del_lis ? "/LIST" : "/NOLIST");
  5502.     x++;
  5503.     }
  5504.     if (del_hdg > -1) {
  5505.     prtopt(&optlines, del_hdg ? "/HEADING" : "/NOHEADING");
  5506.     x++;
  5507.     }
  5508. #ifndef CK_TTGWSIZ
  5509.     if (del_pag > -1) {
  5510.     prtopt(&optlines, del_pag ? "/PAGE" : "/NOPAGE");
  5511.     x++;
  5512.     }
  5513. #endif /* CK_TTGWSIZ */
  5514.     if (!x) prtopt(&optlines,"(no options set)");
  5515.     prtopt(&optlines,"");
  5516. }
  5517. #endif /* NOSHOW */
  5518.  
  5519.  
  5520. int
  5521. setdelopts() {
  5522.     int x_lis = -1, x_pag = -1, x_dot = -1, x_hdg = -1, x_ask = -1;
  5523.     int getval = 0;
  5524.     char c;
  5525.     while (1) {
  5526.     if ((y = cmswi(deltab,ndeltab,"Switch","",xxstring)) < 0) {
  5527.         if (y == -3)
  5528.           break;
  5529.         else
  5530.           return(y);
  5531.     }
  5532.     c = cmgbrk();
  5533.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5534.         printf("?This switch does not take an argument\n");
  5535.         return(-9);
  5536.     }
  5537.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  5538.         printf("?This switch requires an argument\n");
  5539.         return(-9);
  5540.     }
  5541.     switch (y) {
  5542.       case DEL_DOT:
  5543.         x_dot = 1;
  5544.         break;
  5545.       case DEL_NOD:
  5546.         x_dot = 0;
  5547.         break;
  5548.       case DEL_HDG:
  5549.         x_hdg = 1;
  5550.         break;
  5551.       case DEL_LIS:
  5552.         x_lis = 1;
  5553.         break;
  5554.       case DEL_NOL:
  5555.         x_lis = 0;
  5556.         break;
  5557. #ifndef CK_TTGWSIZ
  5558.       case DEL_PAG:
  5559.         x_pag = 1;
  5560.         break;
  5561.       case DEL_NOP:
  5562.         x_pag = 0;
  5563.         break;
  5564. #endif /* CK_TTGWSIZ */
  5565.       case DEL_QUI:
  5566.         x_lis = 0;
  5567.         break;
  5568.       case DEL_VRB:
  5569.         x_lis = 1;
  5570.         break;
  5571.       case DEL_ASK:
  5572.         x_ask = 1;
  5573.         break;
  5574.       case DEL_NAS:
  5575.         x_ask = 0;
  5576.         break;
  5577.       default:
  5578.         printf("?Sorry, this option can not be set\n");
  5579.         return(-9);
  5580.     }
  5581.     }
  5582.     if ((x = cmcfm()) < 0)        /* Get confirmation */
  5583.       return(x);
  5584.     if (x_pag > -1) del_pag = x_pag;
  5585.     if (x_dot > -1) del_dot = x_dot;
  5586.     if (x_hdg > -1) del_hdg = x_hdg;
  5587.     if (x_lis > -1) del_lis = x_lis;
  5588.     if (x_ask > -1) del_ask = x_ask;
  5589.     return(success = 1);
  5590. }
  5591.  
  5592. #ifdef OS2
  5593. static char ** xmtchs = NULL;
  5594. static int xmtchn = 0;
  5595. #endif /* OS2 */
  5596.  
  5597. int
  5598. dodel() {                /* DELETE */
  5599.     int i, j, k, x;
  5600.     int fs = 0;                /* Need to call fileselect() */
  5601.     int len = 0;
  5602.     int bad = 0;
  5603.     int getval = 0, asking = 0;
  5604.     int simulate = 0, rc = 0;
  5605.     long minsize = -1L, maxsize = -1L;
  5606.     int havename = 0, confirmed = 0;
  5607.     int qflag = 0;
  5608.     int summary = 0;
  5609.     int deldirs = 0;
  5610.     int deltree = 0;
  5611.     int itsadir = 0;
  5612.     int argisdir = 0;
  5613.     int xmode = -1, scan = 0, skip = 0;
  5614. #ifdef COMMENT
  5615.     int pass = 0;
  5616. #endif /* COMMENT */
  5617.     char c;
  5618.     char * deldef = "";
  5619.     char safebuf[CKMAXPATH+1];
  5620.     struct FDB sw, fi, fl;
  5621.     char
  5622.       * del_aft = NULL,
  5623.       * del_bef = NULL,
  5624.       * del_naf = NULL,
  5625.       * del_nbf = NULL,
  5626.       * del_exc = NULL;
  5627.     int
  5628.       x_lis = 0,
  5629.       /* x_dot = -1, */
  5630.       x_hdg = 0;
  5631.  
  5632.     char * dxlist[8];
  5633.  
  5634.     for (i = 0; i < 8; i++) dxlist[i] = NULL;
  5635.  
  5636.     g_matchdot = matchdot;
  5637.  
  5638.     if (del_lis > -1) x_lis    = del_lis;
  5639.     if (del_dot > -1) matchdot = del_dot;
  5640.     if (del_hdg > -1) x_hdg    = del_hdg;
  5641.     if (del_pag > -1) xaskmore = del_pag;
  5642.     if (del_ask > -1) asking   = del_ask;
  5643.  
  5644.     diractive = 1;
  5645.     nolinks = 2;            /* By default don't follow links */
  5646.  
  5647.     cmfdbi(&sw,                /* First FDB - command switches */
  5648.        _CMKEY,            /* fcode */
  5649.        "File specification;\n or switch",
  5650.        "",                /* default */
  5651.        "",                /* addtl string data */
  5652.        ndeltab,            /* addtl numeric data 1: tbl size */
  5653.        4,                /* addtl numeric data 2: 4 = cmswi */
  5654.        xxstring,            /* Processing function */
  5655.        deltab,            /* Keyword table */
  5656.        &fi                /* Pointer to next FDB */
  5657.        );
  5658.     cmfdbi(&fl,                /* Anything that doesn't match */
  5659.        _CMFLD,            /* fcode */
  5660.        "",                /* hlpmsg */
  5661.        "",                /* default */
  5662.        "",                /* addtl string data */
  5663.        0,                /* addtl numeric data 1 */
  5664.        0,                /* addtl numeric data 2 */
  5665.        xxstring,
  5666.        NULL,
  5667.        NULL
  5668.        );
  5669.   again:
  5670.     cmfdbi(&fi,                /* 2nd FDB - file to delete */
  5671.        _CMIFI,            /* fcode */
  5672.        "File(s) to delete",        /* hlpmsg */
  5673.        deldef,            /* default */
  5674.        "",                /* addtl string data */
  5675.        nolinks | deldirs,        /* 0 = files, 1 = files or dirs */
  5676.        0,                /* 1 = dirs only */
  5677.        xxstring,
  5678.        NULL,
  5679.        &fl
  5680.        );
  5681.     while (!havename && !confirmed) {
  5682.     x = cmfdb(&sw);            /* Parse something */
  5683.     if (x < 0) {            /* Error */
  5684.         if (x == -3)
  5685.           break;
  5686.         if (x == -2 || x == -9)
  5687.           printf("?Does not match switch or filename: \"%s\"\n",atmbuf);
  5688.         return(x);
  5689.     }
  5690.     if (cmresult.fcode != _CMKEY)    /* Break out if not a switch */
  5691.       break;
  5692.     c = cmgbrk();            /* Get break character */
  5693.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5694.         printf("?This switch does not take an argument\n");
  5695.         rc = -9;
  5696.         goto xdelete;
  5697.     }
  5698.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  5699.         printf("?This switch requires an argument\n");
  5700.         rc = -9;
  5701.         goto xdelete;
  5702.     }
  5703.     switch (k = cmresult.nresult) {
  5704.       case DEL_AFT:
  5705.       case DEL_BEF:
  5706.       case DEL_NAF:
  5707.       case DEL_NBF:
  5708.         if (!getval) break;
  5709.         if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  5710.         if (x == -3) {
  5711.             printf("?Date-time required\n");
  5712.             x = -9;
  5713.         } else
  5714.           rc = x;
  5715.         goto xdelete;
  5716.         }
  5717.         fs++;
  5718.         deltree = 0;
  5719.         switch (k) {
  5720.           case DEL_AFT: makestr(&del_aft,s); break;
  5721.           case DEL_BEF: makestr(&del_bef,s); break;
  5722.           case DEL_NAF: makestr(&del_naf,s); break;
  5723.           case DEL_NBF: makestr(&del_nbf,s); break;
  5724.         }
  5725.         break;
  5726.       case DEL_DOT:
  5727.         matchdot = 1;
  5728.         break;
  5729.       case DEL_NOD:
  5730.         matchdot = 0;
  5731.         break;
  5732.       case DEL_ALL:
  5733.         fs = 0;
  5734. #ifdef VMS
  5735.         deldef = "*.*";        /* UNIX, Windows, OS/2 */
  5736. #else
  5737. #ifdef datageneral
  5738.         deldef = "+";        /* AOS/VS */
  5739. #else
  5740.         deldef = "*";        /* UNIX, Windows, OS/2, VMS... */
  5741. #endif /* datageneral */
  5742. #endif /* VMS */
  5743.         deltree = 1;
  5744.         nolinks = 2;
  5745.         matchdot = 1;
  5746.         recursive = 1;        /* Fall through purposely... */
  5747.       case DEL_DIR:
  5748.         deldirs = 1;
  5749.         goto again;
  5750.       case DEL_EXC:
  5751.         if (!getval) break;
  5752.         if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5753.         if (x == -3) {
  5754.             printf("?Pattern required\n");
  5755.             x = -9;
  5756.         } else
  5757.           rc = x;
  5758.         goto xdelete;
  5759.         }
  5760.         fs++;
  5761.         deltree = 0;
  5762.         makestr(&del_exc,s);
  5763.         break;
  5764.       case DEL_HDG:
  5765.         x_hdg = 1;
  5766.         break;
  5767. #ifdef RECURSIVE
  5768.       case DEL_REC:
  5769.         recursive = 1;
  5770.         break;
  5771. #endif /* RECURSIVE */
  5772.       case DEL_LIS:
  5773.         x_lis = 1;
  5774.         break;
  5775.       case DEL_SUM:
  5776.         summary = 1;
  5777.         x_lis = 0;
  5778.         x_hdg = 1;
  5779.         break;
  5780.       case DEL_NOL:
  5781.         x_lis = 0;
  5782.         break;
  5783. #ifndef CK_TTGWSIZ
  5784.       case DEL_PAG:
  5785.         xaskmore = 1;
  5786.         break;
  5787.       case DEL_NOP:
  5788.         xaskmore = 0;
  5789.         break;
  5790. #endif /* CK_TTGWSIZ */
  5791.       case DEL_QUI:
  5792.         qflag = 1;
  5793.         x_lis = 0;
  5794.         break;
  5795.       case DEL_VRB:
  5796.         x_lis = 1;
  5797.         break;
  5798.  
  5799.       case DEL_SMA:
  5800.       case DEL_LAR:
  5801.         if (!getval) break;
  5802.         if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  5803.           return(x);
  5804.         fs++;
  5805.         deltree = 0;
  5806.         switch (cmresult.nresult) {
  5807.           case DEL_SMA: minsize = y; break;
  5808.           case DEL_LAR: maxsize = y; break;
  5809.         }
  5810.         break;
  5811.  
  5812.       case DEL_SIM:
  5813.         simulate = 1;
  5814.         x_lis = 1;
  5815.         break;
  5816.       case DEL_ASK:
  5817.         asking = 1;
  5818.         break;
  5819.       case DEL_NAS:
  5820.         asking = 0;
  5821.         break;
  5822.       case DEL_TYP: {
  5823.           extern struct keytab txtbin[];
  5824.           if (!getval) break;
  5825.           if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  5826.         return(x);
  5827.           if (x == 2) {        /* ALL */
  5828.           xmode = -1;
  5829.           } else {            /* TEXT or BINARY only */
  5830.           xmode = x;
  5831.           scan = 1;
  5832.           }
  5833.           break;
  5834.       }
  5835.       default:
  5836.         printf("?Not implemented yet - \"%s\"\n",atmbuf);
  5837.         return(-9);
  5838.     }
  5839.     }
  5840.     if (qflag && (cmresult.fcode == _CMFLD)) {
  5841.     if ((x = cmcfm()) < 0)
  5842.       return(x);
  5843.     else
  5844.       return(success = 0);
  5845.     }
  5846.     if (cmresult.fcode != _CMIFI) {
  5847.     if (*atmbuf) {
  5848.         if (iswild(atmbuf) && nzxpand(atmbuf,nzxopts) == 0)
  5849.           printf("?No files match: %s\n",atmbuf);
  5850.         else
  5851.           /* printf("?Not a deletable file: %s\n",atmbuf); */
  5852.           goto tryanyway;
  5853.     } else {
  5854.         printf("?A file specification is required\n");
  5855.     }
  5856.     return(-9);
  5857.     }
  5858.   tryanyway:
  5859.     ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ); /* Safe copy of filespec */
  5860.     if (deldirs) {
  5861.     ckstrncpy(safebuf,cmresult.sresult,CKMAXPATH);
  5862. #ifdef VMSORUNIX
  5863.     len = zgetfs(tmpbuf);        /* Is it a directory name? */
  5864.     argisdir = zgfs_dir;        /* Then because of how zxpand() */
  5865.     if (argisdir && zgfs_link)    /* works, we have to add it to */
  5866.       argisdir = 0;            /* the list. */
  5867.     if (itsadir)
  5868.       len = -2;
  5869. #else
  5870.     len = zchki(tmpbuf);
  5871.     if (len < 0)
  5872.       argisdir = isdir(tmpbuf);
  5873. #endif /* VMSORUNIX */
  5874.     }
  5875.     debug(F110,"DELETE file",tmpbuf,0);
  5876.     if ((x = cmcfm()) < 0)
  5877.       return(x);
  5878.  
  5879. #ifdef IKSD
  5880. #ifdef CK_LOGIN
  5881.     if (inserver && isguest) {
  5882.     printf("?Sorry, DELETE unavailable to guests\n");
  5883.     return(-9);
  5884.     }
  5885. #endif /* CK_LOGIN */
  5886. #endif /* IKSD */
  5887.  
  5888. #ifndef OS2ORUNIX
  5889.     if (simulate) {
  5890.     printf("?Sorry, /SIMULATE not implemented on this platform\n");
  5891.     return(-9);
  5892.     }
  5893. #endif /* OS2ORUNIX */
  5894.  
  5895. #ifdef COMMENT
  5896.     /* (not needed) */
  5897.     if (!iswild(tmpbuf)) {
  5898.     char *m;
  5899.     errno = 0;
  5900.     x = zchki(tmpbuf);
  5901.     if (x < 0) {
  5902.         switch (x) {
  5903.           case -2: m = "Not a regular file"; break;
  5904.           case -1: m = "File not found or not accessible"; break;
  5905.           default: m = errno ? ck_errstr() : "Can't delete";
  5906.         }
  5907.         printf("?%s: \"%s\"\n",m,tmpbuf);
  5908.         return(-9);
  5909.     }
  5910.     }
  5911. #endif /* COMMENT */
  5912.  
  5913.     makelist(del_exc,dxlist,8);
  5914.  
  5915. /* tmpbuf[] has the name - now do any needed conversions on it */
  5916.  
  5917. #ifdef OS2
  5918.     {   /* Lower level functions change / to \, not good for CMD.EXE. */
  5919.     char *p = tmpbuf;
  5920.     while (*p) {            /* Change them back to \ */
  5921.         if (*p == '/') *p = '\\';
  5922.         p++;
  5923.     }
  5924.     }
  5925. #endif /* OS2 */
  5926.  
  5927. #ifdef VMS
  5928.     if (iswild(tmpbuf)) {
  5929. #ifdef COMMENT
  5930.     /* Does not handle '.' as version separator */
  5931.     char *p = tmpbuf;
  5932.     x = 0;
  5933.     while (*p) {
  5934.         if (*p == ';') {
  5935.         x = 1;
  5936.         break;
  5937.         } else
  5938.           p++;
  5939.     }
  5940.     if (!x) ckstrncat(tmpbuf,";*",TMPBUFSIZ);
  5941. #else
  5942.         j = 0; x = 0;            /* for end_dot and number of dots */
  5943.         i = strlen(tmpbuf);
  5944.         if (tmpbuf[i] == ';') {
  5945.             ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  5946.         } else {
  5947.             if (tmpbuf[i--] == '.')
  5948.               j++;
  5949.             for (; i >= 0; i--) {
  5950.                 if (tmpbuf[i] == ';' || tmpbuf[i] == ':' ||
  5951.                     tmpbuf[i] == ']' || tmpbuf[i] == '>')
  5952.                   break;
  5953.                 else if (tmpbuf[i] == '.')
  5954.                   x++;
  5955.             }
  5956.             if (tmpbuf[i] != ';') {    /* dot may have been used */
  5957.                 if (j) {        /* last char is dot */
  5958.                   if (x)        /* second is version separator */
  5959.                     ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  5960.                   else            /* 'foo.' */
  5961.                     ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  5962.                 } else if (x == 1)    /* lacking a version separator */
  5963.           ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  5964.         else if (x == 0)    /* x == 2 has a version */
  5965.           ckstrncat(tmpbuf,".*;0",TMPBUFSIZ);
  5966.             }
  5967.         }
  5968. #endif /* COMMENT */
  5969.     }
  5970. #endif /* VMS */
  5971.  
  5972.     debug(F110,"dodel tmpbuf",tmpbuf,0); /* Filename */
  5973.  
  5974. #ifndef OS2ORUNIX            /* No built-in DELETE code... */
  5975.     /* Construct system command. */
  5976.     ckmakmsg(line,LINBUFSIZ,DELCMD," ",tmpbuf,NULL);
  5977. #else
  5978. #ifdef VMS
  5979.     if (asking) {            /* Maybe overwrite in VMS */
  5980.     if (x_lis)            /* if options are needed... */
  5981.       ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm/log ",tmpbuf,NULL);
  5982.     else
  5983.       ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm ",tmpbuf,NULL);
  5984.     } else if (x_lis)
  5985.       ckmakmsg(line,LINBUFSIZ,DELCMD," /log ",tmpbuf,NULL);
  5986.     conres();
  5987. #endif /* VMS */
  5988.  
  5989.     debug(F110,"dodel line",line,0);
  5990. #endif /* OS2ORUNIX */
  5991.  
  5992. #ifdef MAC
  5993.     success = (zdelet(tmpbuf) == 0);
  5994.  
  5995. #else  /* !MAC ... */
  5996.  
  5997. #ifdef OS2ORUNIX
  5998.     {
  5999.         int filespace = 0;
  6000.         int count = 0;
  6001.     int lines = 0;
  6002.     int n = 0;
  6003.  
  6004.         s = tmpbuf;
  6005.  
  6006. #ifdef CK_TTGWSIZ
  6007. #ifdef OS2
  6008.     ttgcwsz();
  6009. #else /* OS2 */
  6010.     /* Check whether window size changed */
  6011.     if (ttgwsiz() > 0) {
  6012.         if (tt_rows > 0 && tt_cols > 0) {
  6013.         cmd_rows = tt_rows;
  6014.         cmd_cols = tt_cols;
  6015.         }
  6016.     }
  6017. #endif /* OS2 */
  6018. #endif /* CK_TTGWSIZ */
  6019.  
  6020.     if (x_hdg > 0 && !summary) {
  6021.         printf("Deleting %s...%s\n", s, simulate ? " (SIMULATION)" : "");
  6022.         n += 2;
  6023.     }
  6024. #ifdef ZXREWIND
  6025.     z = zxrewind();            /* Rewind file list */
  6026. #else
  6027.     if (!deldirs)
  6028.       nzxopts = ZX_FILONLY;
  6029.     if (recursive) nzxopts |= ZX_RECURSE;
  6030.     if (matchdot)  nzxopts |= ZX_MATCHDOT;
  6031.     errno = 0;
  6032.     z = nzxpand(s,nzxopts);        /* Expand file list */
  6033. #endif /* ZXREWIND */
  6034.     debug(F111,"dodel",s,z);
  6035.  
  6036.     /* If deleting directories, sort in reverse order */
  6037.     /* so we delete the files first, then the directory. */
  6038.  
  6039. #ifdef OS2
  6040.     /* In K95, we have no mtchs array, nor any control over */
  6041.     /* the order in which znext() returns filenames, so we  */
  6042.     /* must copy the array and sort it. */
  6043.     {
  6044.         int i;
  6045.         if (xmtchs) {        /* Free previous list in case */
  6046.         debug(F101,"dodel freeing previous list","",xmtchn);
  6047.         for (i = 0; i < xmtchn; i++) /* it wasn't freed last time. */
  6048.           if (xmtchs[i])
  6049.             free(xmtchs[i]);
  6050.         free(xmtchs);
  6051.         }
  6052.         xmtchn = 0;
  6053.         xmtchs = (char **)malloc(z * (sizeof(char **))); /* Make new one */
  6054.         if (!xmtchs) {
  6055.         printf("?Memory allocation failure\n");
  6056.         return(-9);
  6057.         }
  6058.         for (i = 0; i < z; i++) {
  6059.         xmtchs[i] = NULL;
  6060.         znext(tmpbuf);
  6061.         if (!*tmpbuf)
  6062.           break;
  6063.         makestr(&(xmtchs[i]),tmpbuf);
  6064.         if (!xmtchs[i]) {
  6065.             printf("?Memory allocation failure\n");
  6066.             xmtchn = i - 1;
  6067.             rc = -9;
  6068.             goto xdelete;
  6069.         }
  6070.         /* debug(F111,"dodel add",xmtchs[i],i); */
  6071.         }
  6072.         xmtchn = i;
  6073.         debug(F101,"dodel xmtchn","",xmtchn);
  6074.         sh_sort(xmtchs,NULL,z,0,deldirs,0);
  6075.     }
  6076. #else
  6077. #ifdef UNIX
  6078.     sh_sort(mtchs,NULL,z,0,deldirs,filecase);
  6079. #endif /* UNIX */
  6080. #endif /* OS2 */
  6081.  
  6082.         if (z > 0) {
  6083.             int i;
  6084. #ifdef OS2
  6085.         int ix = 0;
  6086. #endif /* OS2 */
  6087.             success = 1;
  6088.             if (x_hdg > 0)
  6089.               printf("\n");
  6090.  
  6091.         while (
  6092. #ifdef OS2
  6093.            ix < xmtchn
  6094. #else
  6095.            1
  6096. #endif /* OS2 */
  6097.            ) {            /* Loop for all files */
  6098. #ifdef OS2
  6099.         ckstrncpy(tmpbuf,xmtchs[ix++],TMPBUFSIZ);
  6100. #else
  6101.         znext(tmpbuf);        /* Get next file */
  6102. #endif /* OS2 */
  6103.         if (!*tmpbuf) {        /* No more */
  6104.             if (deldirs && recursive && argisdir) {
  6105.             ckstrncpy(tmpbuf,safebuf,TMPBUFSIZ);
  6106.             argisdir = 0;    /* (only do this once) */
  6107.             } else
  6108.               break;
  6109.         }
  6110.         skip = 0;
  6111.         if (!deltree) {
  6112.             if (fs)
  6113.               if (fileselect(tmpbuf,
  6114.                      del_aft,del_bef,del_naf,del_nbf,
  6115.                      minsize,maxsize,0,8,dxlist) < 1) {
  6116.               skip++;
  6117.               }
  6118.         }
  6119.         if (!skip && scan && itsadir) {
  6120.             skip++;
  6121.         }
  6122.         if (!skip && scan) {
  6123.               switch (scanfile(tmpbuf,&y,nscanfile)) {
  6124.               case FT_BIN:
  6125.             if (xmode != 1)
  6126.               skip++;
  6127.             break;
  6128.               case FT_TEXT:
  6129.               case FT_7BIT:
  6130.               case FT_8BIT:
  6131. #ifdef UNICODE
  6132.               case FT_UTF8:
  6133.               case FT_UCS2:
  6134. #endif /* UNICODE */
  6135.             if (xmode != 0)
  6136.               skip++;
  6137.             }
  6138.         }
  6139.         if (!skip && asking) {
  6140.             int x;
  6141.             ckmakmsg(line,LINBUFSIZ," Delete ",tmpbuf,"? ",NULL);
  6142.             x = getyesno(line,3);
  6143.             switch (x) {
  6144.               case 0: continue;          /* no */
  6145.               case 1: break;              /* yes */
  6146.               case 2: goto xdelete;       /* quit */
  6147.               case 3: asking = 0; break;  /* go */
  6148.             }
  6149.         }
  6150. #ifdef VMSORUNIX
  6151.         len = zgetfs(tmpbuf);    /* Get length and accessibility */
  6152.         itsadir = zgfs_dir;
  6153.         if (itsadir && zgfs_link) { /* Treat links to directories */
  6154.             itsadir = 0;    /* as regular files */
  6155.             if (scan)        /* But not if /TYPE: was given */
  6156.               skip++;
  6157.         }
  6158.         if (itsadir)        /* (emulate non-Unix code) */
  6159.           len = -2;
  6160. #else
  6161.         len = zchki(tmpbuf);    /* Get accessibility */
  6162.         if (len < 0)        /* See if it's a directory */
  6163.           itsadir = isdir(tmpbuf);
  6164. #endif /* VMSORUNIX */
  6165.  
  6166.         if (skip) {
  6167. #ifdef COMMENT                /* Too verbose */
  6168.             if (x_lis > 0) {
  6169.             lines++;
  6170.             printf(" %s (SKIPPED)\n",tmpbuf);
  6171. #ifdef CK_TTGWSIZ
  6172.             if (++n > cmd_rows - 3)
  6173.               if (!askmore()) goto xdelete; else n = 0;
  6174. #endif /* CK_TTGWSIZ */
  6175.             }
  6176. #endif /* COMMENT */
  6177.             continue;
  6178.         }
  6179.  
  6180.         debug(F111,"DELETE len",tmpbuf,len);
  6181.         if (simulate) {
  6182.             filespace += len;
  6183.             count++;
  6184.             if (x_lis > 0) {
  6185.             lines++;
  6186.             printf(" %s (SELECTED)\n",tmpbuf);
  6187.             if (++n > cmd_rows - 3) {
  6188.                 int xx;
  6189.                 xx = askmore();
  6190.                 if (!xx) goto xdelete; else n = 0;
  6191.             }
  6192.             }
  6193.         } else if (len >= 0 || !itsadir) { /* Regular file */
  6194.             zdelet(tmpbuf);               /* or symlink, etc... */
  6195.             if (zchki(tmpbuf) < 0) {
  6196.             filespace += len;
  6197.             count++;
  6198.             if (x_lis > 0) {
  6199.                 lines++;
  6200.                 printf(" %s (OK)\n",tmpbuf);
  6201.                 if (++n > cmd_rows - 3)
  6202.                   if (!askmore()) goto xdelete; else n = 0;
  6203.             }
  6204.             } else {
  6205.             bad++;
  6206.             success = 0;
  6207.             if (x_lis > 0) {
  6208.                 lines++;
  6209.                 printf(" %s (FAILED: %s)\n",tmpbuf,ck_errstr());
  6210.                 if (++n > cmd_rows - 3)
  6211.                   if (!askmore()) goto xdelete; else n = 0;
  6212.             }
  6213.             }
  6214.         } else if (/* pass > 0 && */ deldirs && itsadir) {
  6215.             /* It's a directory */
  6216.             if (zrmdir(tmpbuf) > -1) { /* Only works if empty */
  6217.             count++;
  6218.             if (x_lis > 0) {
  6219.                 lines++;
  6220.                 printf(" %s (OK)\n",tmpbuf);
  6221.                 if (++n > cmd_rows - 3)
  6222.                   if (!askmore()) goto xdelete; else n = 0;
  6223.             }
  6224.             } else {
  6225.             success = 0;
  6226.             if (x_lis > 0) {
  6227.                 lines++;
  6228.                 printf(" %s (FAILED: %s)\n",
  6229.                    tmpbuf,
  6230.                    ck_errstr());
  6231.                 if (++n > cmd_rows - 3)
  6232.                   if (!askmore()) goto xdelete; else n = 0;
  6233.             }
  6234.             }
  6235.         } else if (x_lis > 0) {
  6236.             lines++;
  6237.             if (isdir(tmpbuf))
  6238.               printf(" %s (FAILED: directory)\n",tmpbuf);
  6239.             else
  6240.               printf(" %s (FAILED: not a regular file)\n",tmpbuf);
  6241.             if (++n > cmd_rows - 3)
  6242.               if (!askmore()) goto xdelete; else n = 0;
  6243.         }
  6244.             }
  6245.         if (x_hdg > 0) {
  6246.         if (lines > 0)
  6247.           printf("\n");
  6248.         if (++n > cmd_rows - 3)
  6249.           if (!askmore()) goto xdelete; else n = 0;
  6250.         printf("%d file%s %sdeleted, %d byte%s %sfreed%s\n",
  6251.                count,
  6252.                count != 1 ? "s" : "",
  6253.                simulate ? "would be " : "",
  6254.                filespace,
  6255.                filespace != 1 ? "s" : "",
  6256.                simulate ? "would be " : "",
  6257.                simulate ? " (maybe)" : ""
  6258.                );
  6259.         }
  6260.         if (!x_lis && !success && !quiet) {
  6261.         printf("?DELETE failed for %d file%s \
  6262. (use DELETE /LIST to see details)\n",
  6263.                bad, bad == 1 ? "" : "s"
  6264.                );
  6265.         }
  6266.         } else if (x_lis > 0) {
  6267.             if (errno)
  6268.           printf("?%s: %s\n",ck_errstr(), tmpbuf);
  6269.             else
  6270.           printf("?Can't delete: %s\n",tmpbuf);
  6271.         }
  6272.     }
  6273. #else /* OS2ORUNIX */
  6274. #ifndef VMS                /* Others - let the system do it. */
  6275.     xsystem(line);
  6276.     x = nzxpand(tmpbuf,nzxopts);
  6277.     success = (x > 0) ? 0 : 1;
  6278.     if (x_hdg > 0)
  6279.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6280. #else
  6281.     if (asking)
  6282.       printf("\n");
  6283.     x = xsystem(line);                  /* zshcmd returns 1 for success */
  6284.     success = (x > 0) ? 1 : 0;
  6285.     if (x_hdg > 0 && !asking)
  6286.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6287.     concb((char)escape);
  6288. #endif /* VMS */
  6289. #endif /* OS2ORUNIX */
  6290. #endif /* MAC */
  6291.   xdelete:
  6292.     if (g_matchdot > -1) {
  6293.     matchdot = g_matchdot;        /* Restore these... */
  6294.     g_matchdot = -1;
  6295.     }
  6296. #ifdef OS2
  6297.     if (xmtchs) {
  6298.     int i;
  6299.     debug(F101,"dodel freeing list","",xmtchn);
  6300.     for (i = 0; i < xmtchn; i++)
  6301.       if (xmtchs[i]) free(xmtchs[i]);
  6302.     free(xmtchs);
  6303.     xmtchs = NULL;
  6304.     xmtchn = 0;
  6305.     }
  6306. #endif /* OS2 */
  6307.     debug(F101,"dodel result","",rc);
  6308.     return((rc < 0) ? rc : success);
  6309. }
  6310. #endif /* NOFRILLS */
  6311.  
  6312. #ifndef NOSPL                /* The ELSE command */
  6313. _PROTOTYP( VOID pushqcmd, (char *) );
  6314.  
  6315. int
  6316. doelse() {
  6317.     if (!ifcmd[cmdlvl]) {
  6318.     printf("?ELSE doesn't follow IF\n");
  6319.     return(-2);
  6320.     }
  6321. #ifdef COMMENT
  6322. /*
  6323.   Wrong.  This prevents IF..ELSE IF...ELSE IF...ELSE IF...ELSE...
  6324.   from working.
  6325. */
  6326.     ifcmd[cmdlvl] = 0;
  6327. #endif /* COMMENT */
  6328.     if (!iftest[cmdlvl]) {        /* If IF was false do ELSE part */
  6329.     if (maclvl > -1 || tlevel > -1) { /* In macro or command file */
  6330.         debug(F100,"doelse pushing","",0);
  6331. #ifdef COMMENT
  6332.         pushcmd(NULL);        /* save rest of command. */
  6333. #else
  6334.         /* This fixes certain obscure problems */
  6335.         /* but breaks many other constructions that must work. */
  6336.         pushqcmd(NULL);
  6337. #endif /* COMMENT */
  6338.     } else {            /* If interactive, */
  6339.         cmini(ckxech);        /* just start a new command */
  6340.         printf("\n");        /* (like in MS-DOS Kermit) */
  6341.         if (pflag) prompt(xxstring);
  6342.     }
  6343.     } else {                /* Condition is false */
  6344.     if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  6345.       return(y);            /* Gobble up rest of line */
  6346.     }
  6347.     return(0);
  6348. }
  6349. #endif /* NOSPL */
  6350.  
  6351. #ifndef NOSPL
  6352. int
  6353. doswitch() {
  6354.     char *lp, *ap;            /* Macro argument pointer */
  6355.     int len = 0, x, y, pp = 0;
  6356.  
  6357.     /* Get variable name */
  6358.  
  6359.     tmpbuf[0] = NUL;
  6360.     if ((y = cmfld("Variable name","",&s,xxstring)) < 0) {
  6361.     if (y == -3)
  6362.       s = "{}";
  6363.     else
  6364.       return(y);
  6365.     }
  6366.     if (!strcmp(s,"(")) {
  6367.     pp++;
  6368.     if ((y = cmfld("Variable name","",&s,xxstring)) < 0) {
  6369.         if (y == -3)
  6370.           s = "{}";
  6371.         else
  6372.           return(y);
  6373.     }
  6374.     }
  6375.     len = ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  6376.     if (tmpbuf[0] == CMDQ) {
  6377.     if (chkvar(s) < 1) {
  6378.         printf("?Variable name required\n");
  6379.         return(-9);
  6380.     }
  6381.     }
  6382.     if (pp > 0) {            /* If open paren given parse closing */
  6383.     if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6384.       return(y);
  6385.     if (strcmp(atmbuf,")")) {
  6386.         printf("?Closing parenthesis required\n");
  6387.         return(-9);
  6388.     }
  6389.     }
  6390.     lp = line;
  6391.     x = ckstrncpy(lp,"_switx ",LINBUFSIZ); /* _switx + space */
  6392.     lp += x;
  6393.     ap = lp;
  6394.     debug(F010,"SWITCH a",line,0);
  6395.  
  6396. #ifdef COMMENT
  6397.     x = ckmakmsg(lp,LINBUFSIZ-x,tmpbuf," ",NULL,NULL); /* variable name + SP */
  6398. #else
  6399.     {                    /* variable name + SP */
  6400.     char * p = tmpbuf;
  6401.     if (tmpbuf[0] == '(' && tmpbuf[len-1] == ')') {
  6402.         tmpbuf[len-1] = NUL;
  6403.         p++;
  6404.     }
  6405.     x = ckmakmsg(lp,LINBUFSIZ-x,"{",brstrip(p),"}"," ");
  6406.     }
  6407. #endif /* COMMENT */
  6408.     debug(F010,"SWITCH b",line,0);
  6409.     lp += x;
  6410.  
  6411.     /* Get body */
  6412.  
  6413.     if ((y = cmtxt("series of cases","",&s,NULL)) < 0) return(y);
  6414.     if ((y = (int)strlen(s)) < 1) return(-2);
  6415.     if (s[0] != '{' && s[y-1] != '}') {    /* Supply braces if missing */
  6416.     ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  6417.     s = tmpbuf;
  6418.     }
  6419.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  6420.     printf("?Unbalanced braces\n");
  6421.     return(0);
  6422.     }
  6423.     debug(F010,"SWITCH c",line,0);
  6424.  
  6425.     x = mlook(mactab,"_switx",nmac);    /* Look up SWITCH macro definition */
  6426.     if (x < 0) {            /* Not there? */
  6427.     addmmac("_switx",sw_def);    /* Put it back. */
  6428.     if ((x = mlook(mactab,"_switx",nmac)) < 0) { /* Look it up again. */
  6429.         printf("?SWITCH macro definition gone!\n"); /* Shouldn't happen. */
  6430.         return(success = 0);
  6431.     }
  6432.     }
  6433.     debug(F010,"SWITCH command",line,0); /* Execute the SWITCH macro. */
  6434.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  6435. }
  6436.  
  6437. int
  6438. dofor() {                /* The FOR command. */
  6439.     int i, fx, fy, fz;            /* loop variables */
  6440.     char *ap, *di;            /* macro argument pointer */
  6441.     int pp = 0;                /* Paren level */
  6442.     int mustquote = 0;
  6443.  
  6444.     for (i = 0; i < 2; i++) {
  6445.     if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  6446.         if (y == -3) {
  6447.         printf("?Variable name required\n");
  6448.         return(-9);
  6449.         } else
  6450.           return(y);
  6451.     }
  6452.     if (strcmp(s,"("))
  6453.       break;
  6454.     pp++;
  6455.     }
  6456. #ifdef COMMENT
  6457.     if ((y = parsevar(s,&x,&z)) < 0)    /* Check variable. */
  6458.       return(y);
  6459. #else
  6460.     if (*s == CMDQ)            /* If loop variable starts with */
  6461.       mustquote++;            /* backslash, mustquote is > 0. */
  6462. #endif /* COMMENT */
  6463.  
  6464.     lp = line;                /* Build a copy of the command */
  6465.     ckstrncpy(lp,"_forx ",LINBUFSIZ);
  6466.     lp += (int)strlen(line);        /* "_for" macro. */
  6467.     ap = lp;                /* Save pointer to macro args. */
  6468.  
  6469.     if (*s == CMDQ) s++;        /* Skip past backslash if any. */
  6470.     while ((*lp++ = *s++)) ;        /* copy it */
  6471.     lp--; *lp++ = SP;            /* add a space */
  6472.  
  6473.     if ((y = cmnum("initial value","",10,&fx,xxstring)) < 0) {
  6474.     if (y == -3) return(-2);
  6475.     else return(y);
  6476.     }
  6477.     debug(F101,"dofor fx","",fx);
  6478.     s = atmbuf;                /* Copy the atom buffer */
  6479.  
  6480.     if ((int)strlen(s) < 1) goto badfor;
  6481. /*
  6482.   In edit 192, we change the loop variables to be evaluated at loop entry,
  6483.   not each time through the loop.  This was required in order to allow
  6484.   \v(argc) to be used as a loop variable, or in a loop-variable expression.
  6485.   Thus, we can't have FOR loops that modify their own exit conditions by
  6486.   changing the final value or the increment.  The problem with \v(argc) was
  6487.   that it is on the macro stack; after entry into the _forx macro, it is at
  6488.   the wrong place.
  6489. */
  6490.     sprintf(tmpbuf,"%d",fx);        /* (SAFE) Substitute actual value */
  6491.     s = tmpbuf;
  6492.     while ((*lp++ = *s++)) ;        /* (what they actually typed) */
  6493.     lp--; *lp++ = SP;
  6494. #ifdef DEBUG
  6495.     *lp = NUL;
  6496.     debug(F110,"FOR A",line,0);
  6497. #endif /* DEBUG */
  6498.  
  6499.     if ((y = cmnum("final value","",10,&fy,xxstring)) < 0) {
  6500.     if (y == -3) return(-2);
  6501.     else return(y);
  6502.     }
  6503.     debug(F101,"dofor fy","",fy);
  6504.     s = atmbuf;                /* Same deal */
  6505.     if ((int)strlen(s) < 1)
  6506.       goto badfor;
  6507.  
  6508.     sprintf(tmpbuf,"%d",fy);        /* SAFE */
  6509.     s = tmpbuf;
  6510.     while ((*lp++ = *s++)) ;
  6511.     lp--;
  6512.     *lp++ = SP;
  6513. #ifdef DEBUG
  6514.     *lp = NUL;
  6515.     debug(F110,"FOR B",line,0);
  6516. #endif /* DEBUG */
  6517.  
  6518.     x_ifnum = 1;            /* Increment or parenthesis */
  6519.     di = (fx < fy) ? "1" : "-1";    /* Default increment */
  6520.     if ((y = cmnum("increment",di,10,&fz,xxstring)) < 0) {
  6521.     debug(F111,"dofor increment",atmbuf,y);
  6522.     x_ifnum = 0;
  6523.     if (y == -3) {            /* Premature termination */
  6524.         return(-2);
  6525.     } else if (y == -2) {        /* Maybe closing paren */
  6526.         if (!strcmp(atmbuf,")")) {
  6527.         pp--;            /* Count it */
  6528.         s = di;            /* supply default interval */
  6529.         fz = atoi(s);
  6530.         } else            /* Not closing paren, invalid */
  6531.           return(y);
  6532.     } else                /* Other error */
  6533.       return(y);
  6534.     } else {                /* Number */
  6535.     x_ifnum = 0;
  6536.     debug(F101,"dofor fz","",fz);
  6537.     s = atmbuf;            /* Use it */
  6538.     }
  6539.     if ((int)strlen(s) < 1)
  6540.       goto badfor;
  6541.  
  6542.     sprintf(tmpbuf,"%d",fz);        /* (SAFE) Same deal */
  6543.     s = tmpbuf;
  6544.     while ((*lp++ = *s++)) ;
  6545.     lp--; *lp++ = SP;
  6546.  
  6547. #ifdef DEBUG
  6548.     *lp = NUL;
  6549.     debug(F110,"FOR C",line,0);
  6550. #endif /* DEBUG */
  6551.  
  6552.     /* Insert the appropriate comparison operator */
  6553.     if (fz < 0)
  6554.       *lp++ = '<';
  6555.     else
  6556.       *lp++ = '>';
  6557.     *lp++ = SP;
  6558.  
  6559. #ifdef DEBUG
  6560.     *lp = NUL;
  6561.     debug(F110,"FOR D",line,0);
  6562. #endif /* DEBUG */
  6563.  
  6564.     if (pp > 0) {            /* If open paren given parse closing */
  6565.     if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6566.       return(y);
  6567.     if (strcmp(atmbuf,")")) {
  6568.         printf("?Closing parenthesis required\n");
  6569.         return(-9);
  6570.     }
  6571.     }
  6572.     if ((y = cmtxt("Command to execute","",&s,NULL)) < 0) return(y);
  6573.     if ((y = (int)strlen(s)) < 1) return(-2);
  6574.     if (s[0] != '{' && s[y-1] != '}') {    /* Supply braces if missing */
  6575.     ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  6576.     s = tmpbuf;
  6577.     }
  6578.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  6579.     printf("?Unbalanced braces\n");
  6580.     return(0);
  6581.     }
  6582. #ifdef DEBUG
  6583.     *lp = NUL;
  6584.     debug(F110,"FOR E",line,0);
  6585. #endif /* DEBUG */
  6586.  
  6587. #ifdef COMMENT
  6588. /* Too strict */
  6589.     if (fz == 0) {
  6590.     printf("?Zero increment not allowed\n");
  6591.     return(0);
  6592.     }
  6593. #endif /* COMMENT */
  6594. /*
  6595.   In version 8.0 we decided to allow macro names anyplace a numeric-valed
  6596.   variable could appear.  But this caused trouble for the FOR loops because
  6597.   the quoting in for_def[] assumed a \%i-style loop variable.  We account
  6598.   for this here in the if (mustquote)...else logic by invoking separate
  6599.   FOR macro definitions in the two cases.
  6600. */
  6601.     if (mustquote) {            /* \%i-style loop variable */
  6602.         x = mlook(mactab,"_forx",nmac);    /* Look up FOR macro definition */
  6603.         if (x < 0) {            /* Not there? */
  6604.         addmmac("_forx",for_def);    /* Put it back. */
  6605.         if ((x = mlook(mactab,"_forx",nmac)) < 0) { /* Look it up again. */
  6606.         printf("?FOR macro definition gone!\n");
  6607.         return(success = 0);
  6608.         }
  6609.     }
  6610.     } else {                /* Loop variable is a macro */
  6611.         x = mlook(mactab,"_forz",nmac);
  6612.         if (x < 0) {
  6613.         addmmac("_forz",foz_def);
  6614.         if ((x = mlook(mactab,"_forz",nmac)) < 0) {
  6615.         printf("?FOR macro definition gone!\n");
  6616.         return(success = 0);
  6617.         }
  6618.     }
  6619.     }
  6620.     debug(F010,"FOR command",line,0);    /* Execute the FOR macro. */
  6621.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  6622.  
  6623. badfor:
  6624.     printf("?Incomplete FOR command\n");
  6625.     return(-2);
  6626. }
  6627. #endif /* NOSPL */
  6628.  
  6629. #ifndef NOFRILLS
  6630. /* Do the BUG command */
  6631.  
  6632. int
  6633. dobug() {
  6634.     int n;
  6635.     char * s = "";
  6636.     extern char * k_info_dir;
  6637.  
  6638.     if (k_info_dir)
  6639.       s = k_info_dir;
  6640.  
  6641. #ifdef COMMENT
  6642.     printf("\n%s,%s\n Numeric: %ld",versio,ckxsys,vernum);
  6643. #endif /* COMMENT */
  6644.     printf(
  6645. "\nBefore requesting technical support from Columbia U., please consult:\n\n"
  6646.        );
  6647.     n = 7;
  6648. #ifdef OS2
  6649.     printf(" . Your \"Kermit 95\" user manual (use the MANUAL command).\n");
  6650.     printf(" . The technical reference manual, \"Using C-Kermit\".\n");
  6651.     n += 2;
  6652. #else
  6653.     printf(" . The book \"Using C-Kermit\" (type HELP for more info).\n");
  6654.     n += 1;
  6655. #endif /* OS2 */
  6656.  
  6657.     printf(" . Your own organization's support staff, if any.\n");
  6658.     printf(
  6659. " . The comp.protocols.kermit.misc newsgroup.\n");
  6660.     printf(
  6661. " . The Kermit support website, http://www.columbia.edu/kermit/support.html \n"
  6662.        );
  6663.     printf(
  6664. " . The Kermit FAQ, http://www.columbia.edu/kermit/newfaq.html \n");
  6665.     printf(
  6666. " . The C-Kermit FAQ, http://www.columbia.edu/kermit/ckfaq.html \n");
  6667.     n += 4;
  6668.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6669.     printf("\n\
  6670. If you still need help or have a bug to report after consulting these sources,"
  6671.        );
  6672.     printf("\nsend e-mail to:\n\n");
  6673.     n += 2;
  6674.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6675.     printf("  mailto:kermit-support@columbia.edu\n\n");
  6676.     n += 1;
  6677.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6678.     printf("Or contact us by post:\n\n");
  6679.     printf(
  6680. "  Kermit, Columbia University, 612 W 115 Street, New York NY  10025, USA\n\n"
  6681.        );
  6682.     n += 1;
  6683.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6684.     printf("Or by fax at +1 (212) 663-8202.\n\n");
  6685.     n += 1;
  6686.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6687. #ifdef COMMENT
  6688.     printf("Telephone support is available too:\n\n");
  6689.     n += 1;
  6690.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6691.     printf(
  6692.     "  +1 (212) 854-5126, from anywhere, $25.00 USD per call, MC/Visa\n\n");
  6693.     n += 1;
  6694.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6695. #endif /* COMMENT */
  6696. #ifndef NOSHOW
  6697. #ifndef NOFRILLS
  6698.     printf(
  6699. "Before reporting problems, please use the SHOW FEATURES command\n");
  6700.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6701.     printf(
  6702. "to get detailed program version and configuration information.\n\n");
  6703. #endif /* NOFRILLS */
  6704. #endif /* NOSHOW */
  6705.     return(1);
  6706. }
  6707. #endif /* NOFRILLS */
  6708.  
  6709. #ifndef NOSPL
  6710.  
  6711. /*  T O D 2 S E C  --  Convert time of day as hh:mm:ss to secs since midnite */
  6712. /*
  6713.   Call with a string hh:mm or hh:mm:ss.
  6714.   Returns a 0 to 86400 on success, or a negative number on failure.
  6715. */
  6716. long
  6717. tod2sec(t) char * t; {
  6718.     long t2;
  6719.     long hh = 0L, mm = 0L, ss = 0L;
  6720.  
  6721.     if (!t) t = "";
  6722.     if (!*t)
  6723.       return(-3L);
  6724.     debug(F110,"tod2sec",t,0);
  6725.  
  6726.     if (isdigit(*t))            /* Get hours from argument */
  6727.       hh = *t++ - '0';
  6728.     else
  6729.       return(-1L);
  6730.     if (isdigit(*t))
  6731.       hh = hh * 10 + *t++ - '0';
  6732. #ifdef COMMENT
  6733.     if (hh > 24L)
  6734.       return(-1L);
  6735. #endif /* COMMENT */
  6736.     if (*t == ':')
  6737.       t++;
  6738.     else if (!*t)
  6739.       goto xtod2sec;
  6740.     else
  6741.       return(-1L);
  6742.  
  6743.     if (isdigit(*t))            /* Minutes */
  6744.       mm = *t++ - '0';
  6745.     else
  6746.       return(-1L);
  6747.     if (isdigit(*t))
  6748.       mm = mm * 10 + *t++ - '0';
  6749.     if (mm > 60L)
  6750.       return(-1L);
  6751.     if (*t == ':')
  6752.       t++;
  6753.     else if (!*t)
  6754.       goto xtod2sec;
  6755.     else
  6756.       return(-1L);
  6757.  
  6758.     if (isdigit(*t))            /* Seconds */
  6759.       ss = *t++ - '0';
  6760.     else
  6761.       return(-1L);
  6762.     if (isdigit(*t))
  6763.       ss = ss * 10 + *t++ - '0';
  6764.     if (ss > 60L)
  6765.       return(-1L);
  6766.  
  6767.     if (*t > 32)            /* No trailing junk allowed */
  6768.       return(-1L);
  6769.  
  6770.   xtod2sec:
  6771.  
  6772.     t2 = hh * 3600L + mm * 60L + ss;    /* Seconds since midnight from arg */
  6773.     debug(F101,"tod2sec t2","",t2);
  6774.  
  6775.     return(t2);
  6776. }
  6777.  
  6778. int waitinterval = 1;
  6779.  
  6780. #ifdef OLDWAIT
  6781. #undef OLDWAIT
  6782. #endif /* OLDWAIT */
  6783.  
  6784. int kbchar = NUL;
  6785.  
  6786. int
  6787. dopaus(cx) int cx; {
  6788.     long zz;
  6789.     extern int sleepcan;
  6790.  
  6791. #ifdef OLDWAIT
  6792.     zz = -1L;
  6793.     x_ifnum = 1;            /* Turn off internal complaints */
  6794.     if (cx == XXWAI)
  6795.       y = cmnum("seconds to wait, or time of day hh:mm:ss","1",10,&x,xxstring);
  6796.     else if (cx == XXPAU)
  6797.       y = cmnum("seconds to pause, or time of day hh:mm:ss",
  6798.         "1",10,&x,xxstring);
  6799.     else
  6800.       y = cmnum("milliseconds to sleep, or time of day hh:mm:ss",
  6801.         "100",10,&x,xxstring);
  6802.     x_ifnum = 0;
  6803.     if (y < 0) {
  6804.     if (y == -2) {            /* Invalid number or expression */
  6805.         char *p = tmpbuf;        /* Retrieve string from atmbuf */
  6806.         int n = TMPBUFSIZ;
  6807.         *p = NUL;
  6808.         zzstring(atmbuf,&p,&n);    /* Evaluate in case it's a variable */
  6809.         zz = tod2sec(tmpbuf);    /* Convert to secs since midnight */
  6810.         if (zz < 0L) {
  6811.         printf("?Number, expression, or time of day required\n");
  6812.         return(-9);
  6813.         } else {
  6814.         char now[32];        /* Current time */
  6815.         char *p;
  6816.         long tnow;
  6817.         p = now;
  6818.         ztime(&p);
  6819.         tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  6820.         if (zz < tnow)        /* User's time before now */
  6821.           zz += 86400L;        /* So make it tomorrow */
  6822.         zz -= tnow;        /* Seconds from now. */
  6823.         }
  6824.     } else
  6825.       return(y);
  6826.     }
  6827.     if (x < 0) x = 0;
  6828.     switch (cx) {
  6829.       case XXPAU:            /* PAUSE */
  6830.       case XXMSL:            /* MSLEEP */
  6831.     if ((y = cmcfm()) < 0) return(y);
  6832.     break;
  6833.       case XXWAI:            /* WAIT */
  6834.     z = 0;                /* Modem signal mask */
  6835.     while (1) {            /* Read zero or more signal names */
  6836.         y = cmkey(mstab,nms,"modem signal","",xxstring);
  6837.         if (y == -3) break;        /* -3 means they typed CR */
  6838.         if (y < 0) return(y);    /* Other negatives are errors */
  6839.         z |= y;            /* OR the bit into the signal mask */
  6840.     }
  6841.     if ((y = cmcfm()) < 0) return(y);
  6842.     break;
  6843.  
  6844.       default:                /* Shouldn't happen */
  6845.     return(-2);
  6846.     }
  6847.  
  6848. /* Command is entered, now do it. */
  6849.  
  6850.     if (zz > -1L) {            /* Time of day given? */
  6851.     x = zz;
  6852.     if (zz != (long) x) {
  6853.         printf(
  6854. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  6855.            );
  6856.         return(-9);
  6857.     }
  6858.     }
  6859.     if (cx == XXMSL) {            /* Millisecond sleep */
  6860.     msleep(zz < 0 ? x : x * 1000);
  6861.     return(success = 1);
  6862.     }
  6863.     if (cx == XXPAU && !sleepcan) {    /* SLEEP CANCELLATION is OFF */
  6864.     sleep(x);
  6865.     return(success = 1);
  6866.     }
  6867.  
  6868.     /* WAIT, or else SLEEP with cancellation allowed... */
  6869.  
  6870.     do {                /* Sleep loop */
  6871.     int mdmsig;
  6872.     if (sleepcan) {            /* Keyboard cancellation allowed? */
  6873.         if (y = conchk()) {        /* Did they type something? */
  6874. #ifdef COMMENT
  6875.         while (y--) coninc(0);    /* Yes, gobble it all up */
  6876. #else
  6877.         /* There is a debate over whether PAUSE should absorb    */
  6878.         /* its cancelling character(s).  There are several       */
  6879.         /* reasons why it should gobble at least one character:  */
  6880.         /* (1) MS-DOS Kermit does it                             */
  6881.         /* (2) if not, subsequent PAUSE commands will terminate  */
  6882.         /*     immediately                                       */
  6883.         /* (3) if not, subsequent ASK commands will use it as    */
  6884.         /*     valid input.  If \13, then it will get no input   */
  6885.         /* (4) if not, then the character appears on the command */
  6886.         /*     line after all enclosing macros are complete.     */
  6887.         kbchar = coninc(0);    /* Gobble one up */
  6888. #endif /* COMMENT */
  6889.         break;            /* And quit PAUSing or WAITing */
  6890.         }
  6891.     }
  6892.     if (cx == XXWAI) {        /* WAIT (z == modem signal mask) */
  6893.         debug(F101,"WAIT x","",x);
  6894.         if (z > 0) {        /* Looking for any modem signals? */
  6895.         mdmsig = ttgmdm();    /* Yes, get them */
  6896.         if (mdmsig < 0)        /* Failed */
  6897.           return(success = 0);
  6898.         if ((mdmsig & z) == z)    /* Got what we wanted? */
  6899.           return(success = 1);    /* Succeed */
  6900.         }
  6901.         if (x == 0)            /* WAIT 0 and didn't get our signals */
  6902.           break;
  6903.     }
  6904.     sleep(1);            /* No interrupt, sleep one second */
  6905.     } while (--x > 0);
  6906.  
  6907.     if (cx == XXWAI)            /* If WAIT and loop exhausted */
  6908.       success = (z == 0);        /* Fail. */
  6909.     else                /*  */
  6910.       success = (x == 0);        /* Set SUCCESS/FAILURE for PAUSE. */
  6911.     return(success);
  6912.  
  6913. #else  /* New code uses chained FDBs and allows FILE waits... */
  6914.  
  6915.     char * m = "";            /* Help message */
  6916.     struct FDB nu, fl;            /* Parse function descriptor blocks */
  6917.     int filewait = 0;
  6918.     int mdmsig = 0, fs = 0;
  6919.     char filedate[32];
  6920.  
  6921.     kbchar = 0;
  6922.  
  6923.     switch (cx) {
  6924.       case XXWAI: m = "seconds to wait, or time of day hh:mm:ss"; break;
  6925.       case XXPAU: m = "seconds to pause, or time of day hh:mm:ss"; break;
  6926.       case XXMSL: m = "milliseconds to sleep, or time of day hh:mm:ss"; break;
  6927.     }
  6928.     zz = -1L;
  6929.     cmfdbi(&nu,
  6930.        _CMNUM,            /* Number */
  6931.        m,                /* Help message */
  6932.        (cx == XXMSL) ? "100" : "1",    /* Default */
  6933.        "",                /* N/A */
  6934.        0,                /* N/A */
  6935.        0,                /* N/A */
  6936.        xxstring,            /* Processing function */
  6937.        NULL,            /* N/A */
  6938.        &fl                /* Next */
  6939.        );
  6940.     cmfdbi(&fl,                /* Time of day */
  6941.        _CMFLD,            /* Field */
  6942.        "",                /* hlpmsg */
  6943.        "",                /* default */
  6944.        "",                /* addtl string data */
  6945.        0,                /* addtl numeric data 1 */
  6946.        0,                /* addtl numeric data 2 */
  6947.        xxstring,            /* processing func */
  6948.        NULL,            /* N/A */
  6949.        NULL                /* No next */
  6950.        );
  6951.     x = cmfdb(&nu);            /* Parse a number or a field */
  6952.     if (x < 0) {
  6953.     if (x == -3)
  6954.       x = -2;
  6955.     return(x);
  6956.     }
  6957.     switch (cmresult.fcode) {
  6958.       case _CMNUM:            /* Number */
  6959.     x = cmresult.nresult;
  6960.     break;
  6961.       case _CMFLD:            /* Field */
  6962.     zz = tod2sec(cmresult.sresult);    /* Convert to secs since midnight */
  6963.     if (zz < 0L) {
  6964.         printf("?Number, expression, or time of day required\n");
  6965.         return(-9);
  6966.     } else {
  6967.         char now[32];        /* Current time */
  6968.         char *p;
  6969.         long tnow;
  6970.         p = now;
  6971.         ztime(&p);
  6972.         tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  6973.         if (zz < tnow)        /* User's time before now */
  6974.           zz += 86400L;        /* So make it tomorrow */
  6975.         zz -= tnow;        /* Seconds from now. */
  6976.     }
  6977.     }
  6978.     debug(F101,"PAUSE/WAIT/MSLEEP zz","",zz);
  6979.     switch (cx) {
  6980.       case XXPAU:            /* PAUSE */
  6981.       case XXMSL:            /* MSLEEP */
  6982.     if ((y = cmcfm()) < 0) return(y);
  6983.     break;
  6984.       case XXWAI:            /* WAIT */
  6985.     z = 0;                /* Modem signal mask */
  6986.     y = cmkey(waittab,nwaittab,"","",xxstring);
  6987.     if (y < 0) {
  6988.         if (y == -3) {
  6989.         if ((y = cmcfm()) < 0)
  6990.           return(y);
  6991.         break;
  6992.         } else
  6993.           return(y);
  6994.     }
  6995.     if (y == WAIT_FIL) {        /* FILE */
  6996.         int wild = 0;
  6997.         if ((z = cmkey(wfswi,nwfswi,"event","",xxstring)) < 0)
  6998.           return(z);
  6999.         filewait = z;
  7000.         if (filewait == WF_MOD || filewait == WF_DEL)
  7001.           z = cmifi("Filename","",&s,&wild,xxstring);
  7002.         else
  7003.           z = cmfld("Filename","",&s,xxstring);
  7004.         if (z < 0)
  7005.           return(z);
  7006.         if (wild || ((filewait == WF_CRE) && iswild(s))) {
  7007.         printf("?Wildcards not valid here\n");
  7008.         return(-9);
  7009.         }
  7010.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7011.         if ((z = cmcfm()) < 0)
  7012.           return(z);
  7013.         break;
  7014.     } else if (y != WAIT_MDM) {    /* A modem signal */
  7015.         z |= y;            /* OR the bit into the signal mask */
  7016.     }
  7017.     if (!filewait) {        /* Modem signals... */
  7018.         while (1) {            /* Get zero or more signal names */
  7019.         y = cmkey(mstab,nms,"modem signal","",xxstring);
  7020.         if (y == -3) break;    /* -3 means they typed CR */
  7021.         if (y < 0) return(y);    /* Other negatives are errors */
  7022.         z |= y;            /* OR the bit into the signal mask */
  7023.         }
  7024.         if ((y = cmcfm()) < 0) return(y);
  7025.         break;
  7026.     }
  7027.  
  7028.       default:                /* Shouldn't happen */
  7029.     return(-2);
  7030.     } /* switch (cx) */
  7031.  
  7032. /* Command is entered, now do it. */
  7033.  
  7034.     if (zz > -1L) {            /* Time of day given? */
  7035.     x = zz;
  7036.     if (zz != (long) x) {
  7037.         printf(
  7038. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7039.            );
  7040.         return(-9);
  7041.     }
  7042.     }
  7043.     if (sleepcan)
  7044.       concb((char)escape);        /* Ensure single-char wakeup */
  7045.  
  7046.     if (cx == XXMSL) {            /* Millisecond sleep */
  7047.     msleep(zz < 0 ? x : x * 1000);
  7048.     return(success = 1);
  7049.     }
  7050.     if (cx == XXPAU && !sleepcan) {    /* SLEEP CANCELLATION is OFF */
  7051.     sleep(x);
  7052.     return(success = 1);
  7053.     }
  7054.     if (filewait) {            /* FILE... */
  7055.     fs = zchki(tmpbuf);        /* Check if file exists */
  7056.     switch (filewait) {
  7057.       case WF_DEL:
  7058.         if (fs == -1)
  7059.           return(success = 1);
  7060.         break;
  7061.       case WF_MOD:
  7062.         if (fs == -1) {
  7063.         printf("?File does not exit: %s\n",tmpbuf);
  7064.         return(-9);
  7065.         }
  7066.         s = zfcdat(tmpbuf);        /* Get current modification date */
  7067.         if (!s) s = "";
  7068.         if (ckstrncpy(filedate,s,32) != 17) {
  7069.         printf("?Can't get modification time: %s\n",tmpbuf);
  7070.         return(-9);
  7071.         }
  7072.         break;
  7073.       case WF_CRE:
  7074.         if (fs > -1)
  7075.           return(success = 1);
  7076.         break;
  7077.     }
  7078.     }
  7079.     do {                /* Polling loop */
  7080.     if (sleepcan) {            /* Keyboard cancellation allowed? */
  7081.         if ((y = conchk()) > 0) {    /* Did they type something? */
  7082.         kbchar = coninc(0);    /* Yes, get first char they typed */
  7083.         debug(F000,"WAIT kbchar","",kbchar);
  7084. #ifdef COMMENT
  7085.         while (--y > 0)        /* Gobble the rest up */
  7086.           coninc(0);
  7087. #endif /* COMMENT */
  7088.         return(success = 0);    /* And quit PAUSing or WAITing */
  7089.         }
  7090.     }
  7091.     if (filewait == 0) {
  7092.         if (cx == XXWAI) {        /* WAIT for modem signals */
  7093.         if (z != 0) {
  7094.             mdmsig = ttgmdm();    /* Get them. */
  7095.             debug(F101,"WAIT ttgmdm","",mdmsig);
  7096.             if (mdmsig < 0)    /* Failure to get them? */
  7097.               return(success = 0); /* Fail. */
  7098.             if ((mdmsig & z) == z) /* Got desired ones? */
  7099.               return(success = 1); /* Succeed. */
  7100.         } else if (x == 0)
  7101.           return(success = 0);
  7102.         }
  7103.     } else {            /* FILE... */
  7104.         fs = zchki(tmpbuf);        /* Get file status */
  7105.         if (filewait == WF_MOD) {    /* Wait for modification */
  7106.         if (fs == -1)        /* Failure to get status */
  7107.           return(success = 0);    /* so WAIT fails. */
  7108.         s = zfcdat(tmpbuf);    /* Get current modification time */
  7109.         if (!s) s = "";        /* And compare with the time */
  7110.         if (strcmp(s,filedate))    /* when the WAIT started */
  7111.           return(success = 1);
  7112.         } else if (filewait == WF_DEL) { /* Wait for deletion */
  7113.         if (fs == -1)        /* If file doesn't exist, */
  7114.           return(success = 1);    /* succeed. */
  7115.         } else if (filewait == WF_CRE) { /* Wait for creation */
  7116.         if (fs != -1)        /* If file exists */
  7117.           return(success = 1);    /* succeed. */
  7118.         }
  7119.     }
  7120.     if (x < 1)            /* SLEEP/WAIT/PAUSE 0 */
  7121.       break;
  7122.     sleep(waitinterval);        /* No interrupt, sleep */
  7123.     x -= waitinterval;        /* Deduct sleep time */
  7124.     } while (x > 0);
  7125.  
  7126.     if (cx == XXWAI)            /* WAIT time expired */
  7127.       success = (z == 0);        /* Succeed if no modem signals */
  7128.     else                /* For SLEEP or PAUSE, success */
  7129.       success = (x == 0);        /* depends on whether it was */
  7130.     return(success);            /* interrupted from the keyboard. */
  7131. #endif /* OLDWAIT */
  7132. }
  7133. #endif /* NOSPL */
  7134.  
  7135. #ifdef OS2ORUNIX
  7136. _PROTOTYP(int zcmpfn,(char *, char *));
  7137. #endif /* OS2ORUNIX */
  7138.  
  7139. #ifndef NOFRILLS
  7140. #ifdef ZCOPY
  7141. int
  7142. docopy() {
  7143.     int i, x, listing = 0, havename = 0;
  7144.     struct FDB sw, fi;
  7145.     int targetisdir = 0;
  7146.     int targetlen = 0;
  7147.     int swapping = 0;
  7148.     int appending = 0;
  7149.     int fromb64 = 0;
  7150.     int tob64 = 0;
  7151.     int wild = 0;
  7152.     int rc = 1;
  7153.  
  7154.     cmfdbi(&sw,                /* 2nd FDB - optional /PAGE switch */
  7155.        _CMKEY,            /* fcode */
  7156.        "Filename or switch",    /* hlpmsg */
  7157.        "",                /* default */
  7158.        "",                /* addtl string data */
  7159.        ncopytab,            /* addtl numeric data 1: tbl size */
  7160.        4,                /* addtl numeric data 2: 4 = cmswi */
  7161.        xxstring,            /* Processing function */
  7162.        copytab,            /* Keyword table */
  7163.        &fi                /* Pointer to next FDB */
  7164.        );
  7165.     cmfdbi(&fi,                /* 1st FDB - file to type */
  7166.        _CMIFI,            /* fcode */
  7167.        "",                /* hlpmsg */
  7168.        "",                /* default */
  7169.        "",                /* addtl string data */
  7170.        0,                /* addtl numeric data 1 */
  7171.        0,                /* addtl numeric data 2 */
  7172.        xxstring,
  7173.        NULL,
  7174.        NULL
  7175.        );
  7176.  
  7177.     while (!havename) {
  7178.     x = cmfdb(&sw);            /* Parse something */
  7179.     if (x < 0)            /* Error */
  7180.       return(x);
  7181.     switch (cmresult.fcode) {
  7182.       case _CMKEY:
  7183.         switch (cmresult.nresult) {
  7184.           case DEL_LIS:
  7185.           case DEL_VRB:
  7186.         listing = 1;
  7187.         break;
  7188.           case DEL_NOL:
  7189.           case DEL_QUI:
  7190.         listing = 0;
  7191.         break;
  7192.           case 999:
  7193.         swapping = 1;
  7194.         break;
  7195.           case 998:
  7196.         appending = 1;
  7197.         break;
  7198. #ifndef NOSPL
  7199.           case 997:
  7200.         fromb64 = 1;
  7201.         break;
  7202.           case 996:
  7203.         tob64 = 1;
  7204.         break;
  7205. #endif /* NOSPL */
  7206.         }
  7207.         break;
  7208.       case _CMIFI:
  7209.         s = cmresult.sresult;
  7210.         havename = 1;
  7211.         break;
  7212.       default:
  7213.         return(-2);
  7214.     }
  7215.     }
  7216.     wild = cmresult.nresult;
  7217.     ckstrncpy(line,s,LINBUFSIZ);    /* Make a safe copy of source name */
  7218.     s = line;
  7219.     p = tmpbuf;                /* Place for new name */
  7220.  
  7221.     /* Get destination name */
  7222.     if ((x = cmofi("destination name and/or directory",
  7223. #ifdef UNIX
  7224.            "."
  7225. #else
  7226.            ""
  7227. #endif /* UNIX */
  7228.            ,&s,xxstring)) < 0) {
  7229.     if (x == -3) {
  7230.         printf("?Name for destination file required\n");
  7231.         return(-9);
  7232.     } else return(x);
  7233.     }
  7234.     ckstrncpy(p,s,TMPBUFSIZ);        /* Safe copy of destination name */
  7235.     if ((y = cmcfm()) < 0) return(y);
  7236.     if (appending && swapping) {
  7237.     printf("?Sorry, /APPEND and /SWAP conflict\n");
  7238.     return(-9);
  7239.     }
  7240.     if (appending && wild) {
  7241.     printf("?Sorry, /APPEND can be used only with single files\n");
  7242.     return(-9);
  7243.     }
  7244.     targetisdir = isdir(p);
  7245.     x = strlen(p);
  7246.     if (targetisdir) {
  7247. #ifdef UNIXOROSK
  7248.     if (p[x-1] != '/') {
  7249.         ckstrncat(p,"/",TMPBUFSIZ);
  7250.         x++;
  7251.     }
  7252. #else
  7253. #ifdef OS2
  7254.     if (p[x-1] != '/') {
  7255.         ckstrncat(p,"/",TMPBUFSIZ);
  7256.         x++;
  7257.     }
  7258. #else
  7259. #ifdef STRATUS
  7260.     if (p[x-1] != '>') {
  7261.         ckstrncat(p,">",TMPBUFSIZ);
  7262.         x++;
  7263.     }
  7264. #else
  7265. #ifdef datageneral
  7266.     if (p[x-1] != ':') {
  7267.         ckstrncat(p,":",TMPBUFSIZ);
  7268.         x++;
  7269.     }
  7270. #else
  7271.     if (p[x-1] != '/') {
  7272.         ckstrncat(p,"/",TMPBUFSIZ);
  7273.         x++;
  7274.     }
  7275. #endif /* datageneral */
  7276. #endif /* STRATUS */
  7277. #endif /* OS2 */
  7278. #endif /* UNIXOROSK */
  7279.     }
  7280.     targetlen = x;
  7281.  
  7282.     if (wild && !targetisdir) {        /* No wildcards allowed */
  7283.     printf(                /* if target is not a directory */
  7284. "?Multiple source files not allowed if target is not a directory.\n");
  7285.     return(-9);
  7286.     }
  7287.  
  7288. #ifdef VMS
  7289.     conres();                /* Let Ctrl-C work. */
  7290. #endif /* VMS */
  7291.     debug(F110,"docopy line",line,0);
  7292.     debug(F110,"docopy p",p,0);
  7293.  
  7294. #ifdef ZXREWIND
  7295.     z = zxrewind();            /* Rewind file list */
  7296. #else
  7297.     z = nzxpand(s,0);            /* Expand file list */
  7298. #endif /* ZXREWIND */
  7299.  
  7300. #ifdef UNIX
  7301.     if (wild)
  7302.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7303. #endif /* UNIX */
  7304.  
  7305. #ifdef IKSD
  7306.     if (!targetisdir && zchki(p) > -1) { /* Destination file exists? */
  7307.     if (inserver && (!ENABLED(en_del)
  7308. #ifdef CK_LOGIN
  7309.              || isguest
  7310. #endif /* CK_LOGIN */
  7311.              )) {
  7312.         printf("?Sorry, overwriting existing files is disabled\n");
  7313.         return(-9);
  7314.     }
  7315.     }
  7316. #endif /* IKSD */
  7317.  
  7318.     if (tob64 && fromb64) {        /* To and from B64 = no conversion */
  7319.     tob64 = 0;
  7320.     fromb64 = 0;
  7321.     }
  7322.  
  7323.     debug(F110,"COPY dest",p,0);
  7324.  
  7325.     while (z > 0) {
  7326.  
  7327.     znext(line);
  7328.     if (!line[0])
  7329.       break;
  7330.  
  7331.     errno = 0;            /* Reset errno */
  7332.  
  7333.     if (listing) printf("%s => %s ",line,p);
  7334.  
  7335.     /* Straight copy */
  7336.     if (!swapping && !appending && !fromb64 && !tob64) {
  7337.         debug(F110,"COPY zcopy",line,0);
  7338.  
  7339.         if ((x = zcopy(line,p)) < 0) { /* Let zcopy() do it. */
  7340.         switch (x) {
  7341.           case -2:
  7342.             printf("(FAILED: Not a regular file)\n");
  7343.             rc = 0;
  7344.             break;
  7345.           case -3:
  7346.             printf("(FAILED: Not found or not accessible)\n");
  7347.             rc = 0;
  7348.             break;
  7349.           case -4:
  7350.             printf("(FAILED: Permission denied)\n");
  7351.             rc = 0;
  7352.             break;
  7353.           case -5:
  7354.             printf("(Source and destination are the same file)\n");
  7355.             break;
  7356.           case -6:
  7357.             printf("(FAILED: Input/Output error)\n");
  7358.             rc = 0;
  7359.             break;
  7360.           case -7:
  7361.             printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7362.             rc = 0;
  7363.             break;
  7364.           default:
  7365.             printf("(FAILED: %s)\n",ck_errstr());
  7366.             rc = 0;
  7367.         }
  7368.         } else {
  7369.         if (listing) printf("(OK)\n");
  7370.         }
  7371.  
  7372.     } else {            /* Special options */
  7373.  
  7374.         int prev, y, x = 0;        /* Variables needed for them */
  7375.         int i, t;
  7376.         char ibuf[100];
  7377.         char obuf[200];
  7378.         FILE * in = NULL;
  7379.         FILE * out = NULL;
  7380.  
  7381.         if ((in = fopen(line,"r")) == NULL) { /* Open input file */
  7382.         if (listing) printf("(FAILED: %s)\n",ck_errstr());
  7383.         rc = 0;
  7384.         continue;
  7385.         }
  7386.         if (targetisdir) {        /* Target is directory */
  7387.         char * buf = NULL;    /* so append this filename to it */
  7388.         zstrip(line,&buf);
  7389.         p[targetlen] = NUL;
  7390.         if (buf)
  7391.           ckstrncat(p,buf,TMPBUFSIZ);
  7392.         }
  7393. #ifdef OS2ORUNIX
  7394.         if (zcmpfn(line,p)) {    /* Input and output are same file? */
  7395.         if (listing)
  7396.           printf("(FAILED: Source and destination identical)\n");
  7397.         rc = 0;
  7398.         continue;
  7399.         }
  7400. #endif /* OS2ORUNIX */
  7401.         if ((out = fopen(p, (appending ? "a" : "w"))) == NULL) {
  7402.         fclose(in);
  7403.         if (listing) printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7404.         rc = 0;
  7405.         continue;
  7406.         }
  7407. #ifndef NOSPL
  7408.         if (tob64) {        /* Converting to Base-64 */
  7409.  
  7410.         debug(F110,"COPY tob64",line,0);
  7411.  
  7412.         while (1) {        /* Loop... */
  7413.             prev = x;
  7414.             if ((x = fread(ibuf,1,54,in)) < 1) { /* EOF */
  7415.             if (listing)
  7416.               printf("(OK)\n");
  7417.             break;
  7418.             }
  7419.             if (prev % 3) {
  7420.             if (listing)
  7421.               printf("(FAILED: Phase error at %d)\n",prev);
  7422.             rc = 0;
  7423.             break;
  7424.             }
  7425.             if (swapping) {
  7426.             if (x & 1) {
  7427.                 if (listing)
  7428.                   printf("(FAILED: Swap error)\n");
  7429.                 rc = 0;
  7430.                 break;
  7431.             }
  7432.             for (i = 0; i < x; i+=2) {
  7433.                 t = ibuf[i];
  7434.                 ibuf[i] = ibuf[i+1];
  7435.                 ibuf[i+1] = t;
  7436.             }
  7437.             }
  7438.             if ((y = b8tob64(ibuf,x,obuf,180)) < 0) {
  7439.             if (listing)
  7440.               printf("(FAILED: Encoding error)\n");
  7441.             rc = 0;
  7442.             break;
  7443.             }
  7444.             fprintf(out,"%s\n",obuf);
  7445.         }
  7446.  
  7447.         } else if (fromb64) {    /* Converting from Base 64 */
  7448.  
  7449.         debug(F110,"COPY fromb64",line,0);
  7450.  
  7451.         if ((out = fopen(p,appending ? "a" : "w")) == NULL) {
  7452.             fclose(in);
  7453.             if (listing) printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7454.             rc = 0;
  7455.             continue;
  7456.         }
  7457.         x = 1;
  7458.         while (x) {
  7459.             x = fread(ibuf,1,80,in);
  7460.             if ((y = b64tob8(ibuf,x,obuf,80)) < 0) {
  7461.             if (listing)
  7462.               printf("(FAILED: Decoding error)\n");
  7463.             rc = 0;
  7464.             break;
  7465.             }
  7466.             if (swapping) {
  7467.             if (x & 1) {
  7468.                 if (listing)
  7469.                   printf("(FAILED: Swap error)\n");
  7470.                 rc = 0;
  7471.                 break;
  7472.             }
  7473.             for (i = 0; i < y; i+=2) {
  7474.                 t = obuf[i];
  7475.                 obuf[i] = obuf[i+1];
  7476.                 obuf[i+1] = t;
  7477.             }
  7478.             }
  7479.             if (y > 0) {
  7480.             if (fwrite(obuf,1,y,out) < 1) {
  7481.                 if (listing)
  7482.                   printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7483.                 rc = 0;
  7484.                 break;
  7485.             }
  7486.             }
  7487.         }
  7488.  
  7489.         } else
  7490. #endif /* NOSPL */
  7491.  
  7492.         if (swapping) {        /* Swapping bytes */
  7493.  
  7494.         CHAR c[3];
  7495.         c[2] = NUL;
  7496.  
  7497.         debug(F110,"COPY swapping",line,0);
  7498.  
  7499.         while (1) {
  7500.             x = fread((char *)c,1,2,in);
  7501.             if (x < 1) {
  7502.             if (listing)
  7503.               printf("(OK)\n");
  7504.             break;
  7505.             } else if (x == 1) {
  7506.             c[1] = c[0];
  7507.             c[0] = NUL;
  7508.             printf(
  7509.                 "(WARNING: Odd byte count)");
  7510.             if (!listing) printf("\n");
  7511.             }
  7512.             if (fprintf(out,"%c%c",c[1],c[0]) == EOF) {
  7513.             if (listing)
  7514.               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7515.             rc = 0;
  7516.             break;
  7517.             }
  7518.         }
  7519.  
  7520.         } else if (appending) {    /* Appending to target file */
  7521.  
  7522.         char c;
  7523.  
  7524.         debug(F110,"COPY appending",line,0);
  7525.  
  7526.         while (1) {
  7527.             x = fread(&c,1,1,in);
  7528.             if (x < 1) {
  7529.             if (listing)
  7530.               printf("(OK)\n");
  7531.             break;
  7532.             }
  7533.             if (fwrite(&c,1,1,out) < 1) {
  7534.             if (listing)
  7535.               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7536.             rc = 0;
  7537.             break;
  7538.             }
  7539.         }
  7540.         }
  7541.         if (out) fclose(out);
  7542.         if (in) fclose(in);
  7543.     }
  7544. #ifdef VMSORUNIX
  7545. concb((char)escape);
  7546. #endif /* VMSORUNIX */
  7547.     }
  7548.     if (rc > -1) success = rc;
  7549.     return(rc);
  7550. }
  7551. #endif /* ZCOPY */
  7552. #endif /* NOFRILLS */
  7553.  
  7554. #ifndef NORENAME
  7555. #ifndef NOFRILLS
  7556. #ifdef ZRENAME
  7557. int
  7558. dorenam() {
  7559.     /* Parse a file or a directory name */
  7560.     int i, x, z, listing = 0, havename = 0, wild = 0, rc = 1;
  7561.     struct FDB sw, fi;
  7562.  
  7563.     cmfdbi(&sw,                /* 2nd FDB - optional /PAGE switch */
  7564.        _CMKEY,            /* fcode */
  7565.        "Filename or switch",    /* hlpmsg */
  7566.        "",                /* default */
  7567.        "",                /* addtl string data */
  7568.        nqvswtab,            /* addtl numeric data 1: tbl size */
  7569.        4,                /* addtl numeric data 2: 4 = cmswi */
  7570.        xxstring,            /* Processing function */
  7571.        qvswtab,            /* Keyword table */
  7572.        &fi                /* Pointer to next FDB */
  7573.        );
  7574.  
  7575.     cmfdbi(&fi,                /* 1st FDB - file to type */
  7576.        _CMIFI,            /* fcode */
  7577.        "",                /* hlpmsg */
  7578.        "",                /* default */
  7579.        "",                /* addtl string data */
  7580.        3,                /* addtl numeric data 1 */
  7581.        0,                /* addtl numeric data 2 */
  7582.        xxstring,
  7583.        NULL,
  7584.        NULL
  7585.        );
  7586.  
  7587.     while (!havename) {
  7588.     x = cmfdb(&sw);            /* Parse something */
  7589.     if (x < 0)            /* Error */
  7590.       return(x);
  7591.     switch (cmresult.fcode) {
  7592.       case _CMKEY:
  7593.         switch (cmresult.nresult) {
  7594.           case DEL_LIS:
  7595.           case DEL_VRB:
  7596.         listing = 1;
  7597.         break;
  7598.           case DEL_NOL:
  7599.           case DEL_QUI:
  7600.         listing = 0;
  7601.         break;
  7602.         }
  7603.         break;
  7604.       case _CMIFI:
  7605.         s = cmresult.sresult;
  7606.         havename = 1;
  7607.         break;
  7608.       default:
  7609.         return(-2);
  7610.     }
  7611.     }
  7612.     wild = cmresult.nresult;        /* Source specification wild? */
  7613.  
  7614.     if (!wild)
  7615.       wild = iswild(line);
  7616.  
  7617.     ckstrncpy(line,s,LINBUFSIZ);    /* Make a safe copy of source name */
  7618.     s = line;
  7619.     p = tmpbuf;                /* Place for new name */
  7620.     if ((x = cmofi(wild ? "Target directory" : "New name",
  7621.            "",&s,xxstring)) < 0) { /* Get new name */
  7622.     if (x == -3) {
  7623.         printf("?%s required\n", wild ? "Target directory" : "New name");
  7624.         return(-9);
  7625.     } else return(x);
  7626.     }
  7627.     ckstrncpy(p,s,TMPBUFSIZ);        /* Make a safe copy of the new name */
  7628.     if ((y = cmcfm()) < 0) return(y);
  7629.  
  7630.     if (!wild) {            /* Just one */
  7631.     if (listing) printf("%s => %s ",line,p);
  7632.     if (zrename(line,p) < 0) {
  7633.         if (listing) printf("(FAILED: %s\n",ck_errstr());
  7634.         rc = 0;
  7635.     } else {
  7636.         if (listing) printf("(OK)\n");
  7637.     }
  7638.         return(success = rc);
  7639.     }
  7640.     if (!isdir(p)) {            /* Multiple */
  7641.     printf(                /* if target is not a directory */
  7642. "?Multiple source files not allowed if target is not a directory.\n");
  7643.     return(-9);
  7644.     }
  7645. #ifdef COMMENT
  7646.     else {                /* Show full path of target */
  7647.     char buf[CKMAXPATH];        /* (too much) */
  7648.     if (zfnqfp(p,CKMAXPATH,buf))
  7649.       ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  7650.     }
  7651. #endif /* COMMENT */
  7652.  
  7653. #ifdef VMS
  7654.     conres();                /* Let Ctrl-C work. */
  7655. #endif /* VMS */
  7656.     debug(F110,"dorename line",line,0);
  7657.  
  7658. #ifdef ZXREWIND
  7659.     z = zxrewind();            /* Rewind file list */
  7660. #else
  7661.     z = nzxpand(s,0);            /* Expand file list */
  7662. #endif /* ZXREWIND */
  7663.     debug(F111,"dorename p",p,z);
  7664.  
  7665. #ifdef UNIX
  7666.     if (wild && z > 1)
  7667.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7668. #endif /* UNIX */
  7669.  
  7670.     while (z-- > 0) {
  7671.     if (!(z == 0 && !wild))
  7672.           znext(line);
  7673.     if (!line[0])
  7674.       break;
  7675.     if (listing) printf("%s => %s ",line,p);
  7676.     if (zrename(line,p) < 0) {
  7677.         if (listing) printf("(FAILED: %s\n",ck_errstr());
  7678.         rc = 0;
  7679.     } else {
  7680.         if (listing) printf("(OK)\n");
  7681.     }
  7682.     }
  7683. #ifdef VMS
  7684.     concb((char)escape);
  7685. #endif /* VMS */
  7686.     return(success = rc);
  7687. }
  7688. #endif /* ZRENAME */
  7689. #endif /* NOFRILLS */
  7690. #endif /* NORENAME */
  7691.  
  7692. #ifndef NOSPL
  7693.  
  7694. /* Do the RETURN command */
  7695.  
  7696. int
  7697. doreturn(s) char *s; {
  7698.     int x;
  7699.     extern int tra_asg;
  7700.     char * line, * lp;
  7701.  
  7702.     if (cmdlvl < 1) {
  7703.     printf("\n?Can't return from level %d\n",maclvl);
  7704.     return(success = 0);
  7705.     }
  7706.     line = malloc(LINBUFSIZ);
  7707.     if (line == NULL)
  7708.       return(success = 0);
  7709.     lp = line;                /* Expand return value now */
  7710.     x = LINBUFSIZ-1;
  7711.     if (!s) s = "";
  7712.     debug(F110,"RETURN s",s,0);
  7713.     if (zzstring(s,&lp,&x) > -1) {
  7714.     s = line;
  7715.     debug(F110,"RETURN zzstring",s,0);
  7716.     }
  7717.  
  7718.     /* Pop from all FOR/WHILE/SWITCH/XIFs */
  7719.     while ((maclvl > 0) &&
  7720.        (m_arg[maclvl-1][0]) &&
  7721.        (cmdstk[cmdlvl].src == CMD_MD) &&
  7722.        (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  7723.         !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  7724.         !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  7725.         !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  7726.     debug(F111,"RETURN IF/FOR/WHI/SWI pop",m_arg[maclvl-1][0],maclvl);
  7727.     dogta(XXPTA);            /* Put args back */
  7728.     popclvl();            /* Pop up two levels */
  7729.     popclvl();
  7730.     }
  7731.     if (tra_asg) {            /* If tracing show return value */
  7732.     if (*s)
  7733.       printf("<<< %s: \"%s\"\n", m_arg[maclvl][0], s);
  7734.     else
  7735.       printf("<<< %s: (null)\n", m_arg[maclvl][0]);
  7736.     }
  7737.     popclvl();                /* Pop from enclosing TAKE or macro */
  7738.     debug(F111,"RETURN tolevel",s,maclvl);
  7739.     if (!s) s = "";
  7740.     if (!*s) s = NULL;
  7741.     makestr(&(mrval[maclvl+1]),s);    /* Set the RETURN value */
  7742.     free(line);
  7743.     return(success = 1);        /* Macro succeeds if we RETURN */
  7744. }
  7745. #endif /* NOSPL */
  7746.  
  7747. #ifndef NOSPL
  7748. /* Do the OPEN command */
  7749.  
  7750. int
  7751. doopen()  {                /* OPEN { append, read, write } */
  7752.     int x, y, z = 0; char *s;
  7753.     static struct filinfo fcb;        /* (must be static) */
  7754.     if ((x = cmkey(opntab,nopn,"mode","",xxstring)) < 0) {
  7755.     if (x == -3) {
  7756.         printf("?Mode required\n");
  7757.         return(-9);
  7758.     } else return(x);
  7759.     }
  7760.     switch (x) {
  7761.       case OPN_FI_R:            /* Old file (READ) */
  7762.     if (chkfn(ZRFILE) > 0) {
  7763.         printf("?Read file already open\n");
  7764.         return(-2);
  7765.     }
  7766.     if ((z = cmifi("File to read","",&s,&y,xxstring)) < 0) {
  7767.         if (z == -3) {
  7768.         printf("?Input filename required\n");
  7769.         return(-9);
  7770.         } else return(z);
  7771.     }
  7772.     if (y) {                /* No wildcards allowed */
  7773.         printf("\n?Please specify a single file\n");
  7774.         return(-2);
  7775.     }
  7776.     ckstrncpy(line,s,LINBUFSIZ);
  7777.     if ((int)strlen(line) < 1) return(-2);
  7778.     if ((z = cmnum("buffer size","4096",10,&y,xxstring)) < 0)
  7779.       return(z);
  7780.     if (y < 1) {
  7781.         printf("?Positive number required\n");
  7782.         return(-9);
  7783.     }
  7784.     if ((z = cmcfm()) < 0) return(z);
  7785.         readblock = y;
  7786.     if (readbuf)
  7787.       free((char *)readbuf);
  7788.     if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  7789.         printf("?Can't allocate read buffer\n");
  7790.         return(-9);
  7791.     }
  7792.     return(success = zopeni(ZRFILE,line));
  7793.  
  7794. #ifndef MAC
  7795. #ifndef NOPUSH
  7796.       case OPN_PI_R:            /* Pipe/Process (!READ) */
  7797.     if (nopush) {
  7798.         printf("?Read from pipe disabled\n");
  7799.         return(success=0);
  7800.     }
  7801.     if (chkfn(ZRFILE) > 0) {
  7802.         printf("?Read file already open\n");
  7803.         return(-2);
  7804.     }
  7805.         if ((y = cmtxt("System command to read from","",&s,xxstring)) < 0) {
  7806.         if (y == -3) {
  7807.         printf("?Command name required\n");
  7808.         return(-9);
  7809.         } else return(y);
  7810.     }
  7811.     ckstrncpy(line,brstrip(s),LINBUFSIZ);
  7812.     if (!line[0]) return(-2);
  7813.     if ((y = cmcfm()) < 0) return(y);
  7814.     if (!readbuf) {
  7815.         if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  7816.         printf("?Can't allocate read buffer\n");
  7817.         return(-9);
  7818.         }
  7819.     }
  7820.     return(success = zxcmd(ZRFILE,line));
  7821.  
  7822.       case OPN_PI_W:            /* Write to pipe */
  7823.     if (nopush) {
  7824.         printf("?Write to pipe disabled\n");
  7825.         return(success=0);
  7826.     }
  7827.     if (chkfn(ZWFILE) > 0) {
  7828.         printf("?Write file already open\n");
  7829.         return(-2);
  7830.     }
  7831.         if ((y = cmtxt("System command to write to","",&s,xxstring)) < 0) {
  7832.         if (y == -3) {
  7833.         printf("?Command name required\n");
  7834.         return(-9);
  7835.         } else return(y);
  7836.     }
  7837.     ckstrncpy(line,brstrip(s),LINBUFSIZ);
  7838.     if (!line[0]) return(-2);
  7839.     if ((y = cmcfm()) < 0) return(y);
  7840.     success = zxcmd(ZWFILE,line);
  7841.     if (!success && msgflg)
  7842.       printf("Can't open process for writing: %s\n",line);
  7843.     return(success);
  7844. #endif /* NOPUSH */
  7845. #endif /* MAC */
  7846.  
  7847.       case OPN_FI_W:            /* New file (WRITE) */
  7848.       case OPN_FI_A:            /* (APPEND) */
  7849.     if ((z = cmofi("Name of local file to create","",&s,xxstring)) < 0) {
  7850.         if (z == -3) {
  7851.         printf("?Filename required\n");
  7852.         return(-9);
  7853.         } else return(z);
  7854.     }
  7855.     if (z == 2) {
  7856.         printf("?Sorry, %s is a directory name\n",s);
  7857.         return(-9);
  7858.     }
  7859.     if (chkfn(ZWFILE) > 0) {
  7860.         printf("?Write/Append file already open\n");
  7861.         return(-2);
  7862.     }
  7863.         fcb.bs = fcb.cs = fcb.rl = fcb.fmt = fcb.org = fcb.cc = fcb.typ = 0;
  7864.     fcb.lblopts = 0;
  7865.     fcb.dsp = (x == OPN_FI_W) ? XYFZ_N : XYFZ_A; /* Create or Append */
  7866.     ckstrncpy(line,s,LINBUFSIZ);
  7867.     if ((int)strlen(line) < 1) return(-2);
  7868.     if ((y = cmcfm()) < 0) return(y);
  7869.     return(success = zopeno(ZWFILE,line,NULL,&fcb));
  7870.  
  7871. #ifndef NOLOCAL
  7872.       case OPN_SER:            /* OPEN PORT or LINE */
  7873.       case OPN_NET: {            /* OPEN HOST */
  7874.       extern int didsetlin, ttnproto;
  7875.       if (x == OPN_NET) {
  7876.           z = ttnproto;
  7877.           ttnproto = NP_NONE;
  7878.       }
  7879.       if ((y = setlin((x == OPN_SER) ? XYLINE : XYHOST, 1, 0)) < 0) {
  7880.           if (x == OPN_NET)
  7881.         ttnproto = z;
  7882.           success = 0;
  7883.       }
  7884.       didsetlin++;
  7885.       return(y);
  7886.       }
  7887. #endif /* NOLOCAL */
  7888.  
  7889.       default:
  7890.     printf("?Not implemented");
  7891.     return(-2);
  7892.     }
  7893. }
  7894. #endif /* NOSPL */
  7895.  
  7896. #ifndef NOXFER
  7897. /*  D O X G E T  --  GET command parser with switches  */
  7898.  
  7899. #ifdef CK_LABELED
  7900. int g_lf_opts = -1;
  7901. extern int lf_opts;
  7902. #endif /* CK_LABELED */
  7903.  
  7904. int
  7905. doxget(cx) int cx; {
  7906.     extern int                /* External variables we need */
  7907. #ifdef RECURSIVE
  7908.       recursive,
  7909. #endif /* RECURSIVE */
  7910.       xfermode, fdispla, protocol, usepipes,
  7911.       g_binary, g_xfermode, g_displa, g_rpath, g_usepipes;
  7912.     extern char * rcv_move;        /* Directory to move new files to */
  7913.     extern char * rcv_rename;        /* What to rename new files to */
  7914.     extern char * rcvexcept[];        /* RECEIVE / GET exception list */
  7915.     int opkt  =  0;            /* Flag for O-Packet needed */
  7916.  
  7917. #ifdef PIPESEND
  7918.     extern int pipesend;
  7919.     extern char * rcvfilter;
  7920. #endif /* PIPESEND */
  7921.     extern struct keytab rpathtab[];
  7922.     extern int nrpathtab;
  7923.     extern long calibrate;
  7924.     int asname = 0;            /* Flag for have as-name */
  7925.     int konly = 0;            /* Kermit-only function */
  7926.     int c, i, n, confirmed = 0;        /* Workers */
  7927.     int getval = 0;            /* Whether to get switch value */
  7928.     int rcvcmd = 0;            /* Whether it is the RECEIVE command */
  7929.     int mget = 0;            /* Whether it is the MGET command */
  7930.     struct stringint {            /* Temporary array for switch values */
  7931.     char * sval;
  7932.     int ival;
  7933.     } pv[SND_MAX+1];
  7934.     struct FDB sw, fl, cm;        /* FDBs for each parse function */
  7935.     char * cmdstr = "this command";
  7936.  
  7937. #ifdef NEWFTP
  7938.     if (cx == XXGET || cx == XXREGET || cx == XXMGET) {
  7939.     extern int ftpget;
  7940.     extern int ftpisopen();
  7941.     if ((ftpget == 1) || ((ftpget == 2) && ftpisopen()))
  7942.           return(doftpget(cx,0));
  7943.     }
  7944. #endif /* NEWFTP */
  7945.  
  7946.     debug(F101,"xget cx","",cx);
  7947.  
  7948.     oopts = -1;
  7949.     omode = -1;
  7950.  
  7951.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  7952.     pv[i].sval = NULL;
  7953.     pv[i].ival = -1;
  7954.     }
  7955.     /* Preset switch values based on top-level command that called us */
  7956.  
  7957.     switch (cx) {
  7958.       case XXREC:            /* RECEIVE */
  7959.     cmdstr = "RECEIVE";
  7960.     rcvcmd = 1; break;
  7961.       case XXGET:            /* GET */
  7962.     cmdstr = "GET";
  7963.     konly = 1;
  7964.     break;
  7965. #ifdef CK_RESEND
  7966.       case XXREGET:            /* REGET */
  7967.     cmdstr = "REGET";
  7968.     konly = 1;
  7969.     pv[SND_BIN].ival = 1;        /* Implies /BINARY */
  7970.     pv[SND_RES].ival = 1; break;
  7971. #endif /* CK_RESEND */
  7972.       case XXRETR:            /* RETRIEVE */
  7973.     cmdstr = "RETRIEVE";
  7974.     konly = 1;
  7975.     pv[SND_DEL].ival = 1; break;
  7976. #ifdef PIPESEND
  7977.       case XXCREC:            /* CRECEIVE */
  7978.     cmdstr = "CRECEIVE";
  7979.     konly = 1;
  7980.     rcvcmd = 1;
  7981.     pv[SND_CMD].ival = 1; break;
  7982.       case XXCGET:            /* CGET */
  7983.     cmdstr = "CGET";
  7984.     konly = 1;
  7985.     pv[SND_CMD].ival = 1; break;
  7986. #endif /* PIPESEND */
  7987. #ifndef NOMGET
  7988.       case XXMGET:            /* MGET */
  7989.     cmdstr = "MGET";
  7990.     konly = 1;
  7991.     mget = 1; break;
  7992. #endif /* NOMGET */
  7993.     }
  7994.     debug(F111,"xget rcvcmd",cmdstr,rcvcmd);
  7995.     debug(F101,"xget konly","",konly);
  7996.  
  7997. #ifdef CK_XYZ
  7998.     if (!rcvcmd && protocol != PROTO_K) {
  7999.     printf("?Sorry, %s works only with Kermit protocol\n",cmdstr);
  8000.     return(-9);
  8001.     }
  8002. #endif /* CK_XYZ */
  8003.  
  8004.     /* Set up chained parse functions... */
  8005.  
  8006.     cmfdbi(&sw,                /* First FDB - command switches */
  8007.        _CMKEY,            /* fcode */
  8008.        rcvcmd ?
  8009.        "Optional name/template to store incoming files under, or switch" :
  8010.        "Remote filename, or switch", /* hlpmsg */
  8011.        "",                /* default */
  8012.        "",                /* addtl string data */
  8013.        rcvcmd ? nrcvtab : ngettab,    /* addtl numeric data 1: tbl size */
  8014.        4,                /* addtl numeric data 2: 4 = cmswi */
  8015.        xxstring,            /* Processing function */
  8016.        rcvcmd ? rcvtab : gettab,    /* Keyword table */
  8017.        &fl                /* Pointer to next FDB */
  8018.        );
  8019.     if (rcvcmd || mget)            /* RECEIVE or MGET */
  8020.       cmfdbi(&fl,
  8021.        _CMTXT,            /* fcode */
  8022.        rcvcmd ?            /* hlpmsg */
  8023.          "Output filename or Command" : /* Output filename */
  8024.          "File(s) to GET",            /* Files we are asking for */
  8025.        "",                /* default */
  8026.        "",                /* addtl string data */
  8027.        0,                /* addtl numeric data 1 */
  8028.        0,                /* addtl numeric data 2 */
  8029. #ifdef CK_XYZ
  8030.        (protocol == PROTO_X || protocol == PROTO_XC) ?
  8031.          xxstring :
  8032.          (rcvcmd ? (xx_strp)0  : xxstring)
  8033. #else
  8034.        rcvcmd ? (xx_strp)0  : xxstring /* Processing function */
  8035. #endif /* CK_XYZ */
  8036.          ,
  8037.        NULL,
  8038.        &cm
  8039.        );
  8040.     else
  8041.       cmfdbi(&fl,            /* Remote filename or command */
  8042.        _CMFLD,            /* fcode */
  8043.        "Remote filename",        /* hlpmsg */
  8044.        "",                /* default */
  8045.        "",                /* addtl string data */
  8046.        0,                /* addtl numeric data 1 */
  8047.        0,                /* addtl numeric data 2 */
  8048.        xxstring,
  8049.        NULL,
  8050.        &cm
  8051.        );
  8052.     cmfdbi(&cm,                /* Confirmation */
  8053.        _CMCFM,            /* fcode */
  8054.        "",                /* hlpmsg */
  8055.        "",                /* default */
  8056.        "",                /* addtl string data */
  8057.        0,                /* addtl numeric data 1 */
  8058.        0,                /* addtl numeric data 2 */
  8059.        NULL,
  8060.        NULL,
  8061.        NULL
  8062.        );
  8063.  
  8064.     /* (See doxsend() for fuller commentary) */
  8065.  
  8066.     while (1) {                /* Parse 0 or more switches */
  8067.     x = cmfdb(&sw);            /* Parse something */
  8068.     debug(F101,"xget cmfdb","",x);
  8069.     if (x < 0)            /* Error */
  8070.       goto xgetx;            /* or reparse needed */
  8071.     if (cmresult.fcode != _CMKEY)    /* Break out if not a switch */
  8072.       break;
  8073.     c = cmgbrk();            /* Get break character */
  8074.     if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  8075.         printf("?This switch does not take an argument\n");
  8076.         x = -9;
  8077.         goto xgetx;
  8078.     }
  8079.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  8080.         printf("?This switch requires an argument\n");
  8081.         x = -9;
  8082.         goto xgetx;
  8083.     }
  8084.     n = cmresult.nresult;        /* Numeric result = switch value */
  8085.     debug(F101,"xget switch","",n);
  8086.  
  8087.     switch (n) {            /* Process the switch */
  8088. #ifdef PIPESEND
  8089.       case SND_CMD:            /* These take no args */
  8090.         if (nopush) {
  8091.         printf("?Sorry, system command access is disabled\n");
  8092.         x = -9;
  8093.         goto xgetx;
  8094.         } else if (rcvfilter) {
  8095.         printf(
  8096. "?Sorry, no GET /COMMAND when RECEIVE FILTER selected\n");
  8097.         x = -9;
  8098.         goto xgetx;
  8099.         }
  8100.         if (rcvcmd)
  8101.           sw.hlpmsg = "Command, or switch"; /* Change help message */
  8102.         /* Fall thru... */
  8103. #endif /* PIPESEND */
  8104.  
  8105.       case SND_REC:            /* /RECURSIVE */
  8106.         pv[SND_PTH].ival = PATH_REL; /* Implies relative pathnames */
  8107.         pv[n].ival = 1;        /* Set the recursive flag */
  8108.         break;
  8109.  
  8110.       case SND_RES:            /* /RECOVER */
  8111.         pv[SND_BIN].ival = 1;    /* Implies /BINARY */
  8112.         pv[n].ival = 1;        /* Set the resend flag */
  8113.         break;
  8114.  
  8115.       case SND_DEL:            /* /DELETE */
  8116.       case SND_SHH:            /* /QUIET */
  8117.           case SND_CAL:            /* /CALIBRATE */
  8118.       case SND_XPA:            /* /TRANSPARENT */
  8119.         pv[n].ival = 1;        /* Just set the appropriate flag */
  8120.         break;
  8121.  
  8122.       case SND_PIP:            /* /PIPES:{ON,OFF} */
  8123.         if (!getval) {
  8124.         pv[n].ival = 1;
  8125.         break;
  8126.         }
  8127.         if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  8128.           goto xgetx;
  8129.         if (!nopush)
  8130.           pv[n].ival = x;
  8131.         break;
  8132.  
  8133.       /* File transfer modes - each undoes the others */
  8134.  
  8135.       case SND_BIN:            /* Binary */
  8136.       case SND_TXT:            /* Text */
  8137.       case SND_IMG:            /* Image */
  8138.       case SND_LBL:            /* Labeled */
  8139.         pv[SND_BIN].ival = 0;    /* Unset all */
  8140.         pv[SND_TXT].ival = 0;
  8141.         pv[SND_IMG].ival = 0;
  8142.         pv[SND_LBL].ival = 0;
  8143.         pv[n].ival = 1;        /* Set the requested one */
  8144.         break;
  8145.  
  8146.       case SND_EXC:            /* Excludes */
  8147.         if (!getval) break;
  8148.         if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  8149.         if (x == -3) {
  8150.             printf("?Pattern required\n");
  8151.             x = -9;
  8152.         }
  8153.         goto xgetx;
  8154.         }
  8155.         if (pv[n].sval) free(pv[n].sval);
  8156.         y = strlen(s);
  8157.         if (y > 256) {
  8158.         printf("?Pattern too long - 256 max\n");
  8159.         x = -9;
  8160.         goto xgetx;
  8161.         }
  8162.         pv[n].sval = malloc(y+1);
  8163.         if (pv[n].sval) {
  8164.         strcpy(pv[n].sval,s);    /* safe */
  8165.         pv[n].ival = 1;
  8166.         }
  8167.         break;
  8168.  
  8169. #ifdef COMMENT
  8170.       /* Not implemented */
  8171.       case SND_PRI:            /* GET to printer */
  8172.         pv[n].ival = 1;
  8173.         if (!getval) break;
  8174.         if ((x = cmfld("Print options","",&s,xxstring)) < 0)
  8175.           goto xgetx;
  8176.         pv[n].sval = malloc((int)strlen(s)+1);
  8177.         if (pv[n].sval)
  8178.           strcpy(pv[n].sval,s);    /* safe */
  8179.         break;
  8180. #endif /* COMMENT */
  8181.  
  8182.       case SND_MOV:            /* MOVE after */
  8183.       case SND_REN:            /* RENAME after */
  8184.         if (!getval) break;
  8185.         if ((x = cmfld(n == SND_MOV ?
  8186.        "device and/or directory for source file after sending" :
  8187.        "new name for source file after sending",
  8188.                "",
  8189.                &s,
  8190.                n == SND_MOV ? xxstring : NULL
  8191.                )) < 0) {
  8192.         if (x == -3) {
  8193.             printf("%s\n", n == SND_MOV ?
  8194.                "?Destination required" :
  8195.                "?New name required"
  8196.                );
  8197.             x = -9;
  8198.         }
  8199.         goto xgetx;
  8200.         }
  8201.         if (pv[n].sval) {
  8202.         free(pv[n].sval);
  8203.         pv[n].sval = NULL;
  8204.         }
  8205.         s = brstrip(s);
  8206.         y = strlen(s);
  8207.         if (y > 0) {
  8208.         pv[n].sval = malloc(y+1);
  8209.         if (pv[n].sval) {
  8210.             strcpy(pv[n].sval,s); /* safe */
  8211.             pv[n].ival = 1;
  8212.         }
  8213.         }
  8214.         break;
  8215.  
  8216.       case SND_ASN:            /* As-name */
  8217.         if (!getval) break;
  8218.         if (mget) {
  8219.         printf("?Sorry, as-name not allowed with MGET\n");
  8220.         x = -9;
  8221.         goto xgetx;
  8222.         }
  8223.         if ((x = cmfld("Name to store it under","",&s,NULL)) < 0)
  8224.           goto xgetx;
  8225.         s = brstrip(s);
  8226.         if ((y = strlen(s)) > 0) {
  8227.         if (pv[n].sval) free(pv[n].sval);
  8228.         pv[n].sval = malloc(y+1);
  8229.         if (pv[n].sval) {
  8230.             strcpy(pv[n].sval,s); /* safe */
  8231.             pv[n].ival = 1;
  8232.         }
  8233.         }
  8234.         break;
  8235.  
  8236. #ifdef PIPESEND
  8237.       case SND_FLT:            /* Filter */
  8238.         debug(F101,"xget /filter getval","",getval);
  8239.         if (!getval) break;
  8240.         if ((x = cmfld("Filter program to receive through",
  8241.                "",&s,NULL)) < 0) {
  8242.         if (x == -3)
  8243.           s = "";
  8244.         else
  8245.           goto xgetx;
  8246.         }
  8247.         s = brstrip(s);
  8248.         y = strlen(s);
  8249.         for (x = 0; x < y; x++) {    /* Make sure they included "\v(...)" */
  8250.         if (s[x] != '\\') continue;
  8251.         if (s[x+1] == 'v') break;
  8252.         }
  8253.         if (x == y) {
  8254.         printf(
  8255.         "?Filter must contain a replacement variable for filename.\n"
  8256.                );
  8257.         x = -9;
  8258.         goto xgetx;
  8259.         }
  8260.         pv[n].ival = 1;
  8261.         if (pv[n].sval) {
  8262.         free(pv[n].sval);
  8263.         pv[n].sval = NULL;
  8264.         }
  8265.         if ((y = strlen(s)) > 0) {
  8266.         if ((pv[n].sval = malloc(y+1)))
  8267.           strcpy(pv[n].sval,s);    /* safe */
  8268.         }
  8269.         break;
  8270. #endif /* PIPESEND */
  8271.  
  8272.       case SND_PTH:            /* Pathnames */
  8273.         if (!getval) {
  8274.         pv[n].ival = PATH_REL;
  8275.         break;
  8276.         }
  8277.         if ((x = cmkey(rpathtab,nrpathtab,"","on",xxstring)) < 0)
  8278.           goto xgetx;
  8279.         pv[n].ival = x;        /* Ditto */
  8280.         break;
  8281.  
  8282.       case SND_NAM:            /* Filenames */
  8283.         if (!getval) break;
  8284.         if ((x = cmkey(fntab,nfntab,"","converted",xxstring)) < 0)
  8285.           goto xgetx;
  8286.         pv[n].ival = x;
  8287.         break;
  8288.  
  8289.       case SND_PRO:            /* Protocol to use */
  8290.         if (!getval) break;
  8291.         if ((x = cmkey(protos,nprotos,"File-transfer protocol","",
  8292.                xxstring)) < 0) {
  8293.         if (x == -3)
  8294.           x = 0;
  8295.         else
  8296.           goto xgetx;
  8297.         }
  8298.         debug(F111,"xget /proto",atmbuf,x);
  8299.         pv[n].ival = x;
  8300.         if (konly && x != PROTO_K) {
  8301.         printf(
  8302. "?Sorry, this command works only with Kermit protocol\n"
  8303.                );
  8304.         x = -9;
  8305.         goto xgetx;
  8306.         }
  8307.         break;
  8308.  
  8309.       default:
  8310.         printf("?Unexpected switch value - %d\n",cmresult.nresult);
  8311.         x = -9;
  8312.         goto xgetx;
  8313.     }
  8314.     }
  8315.     debug(F101,"xget cmresult fcode","",cmresult.fcode);
  8316.  
  8317.     cmarg = line;            /* Initialize string pointers */
  8318.     cmarg2 = tmpbuf;
  8319.     asname = 0;
  8320.     line[0] = NUL;            /* and buffers. */
  8321.     tmpbuf[0] = NUL;
  8322.  
  8323.     switch (cmresult.fcode) {        /* How did we get out of switch loop */
  8324.       case _CMFLD:            /* (3) Remote filespec */
  8325.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  8326.     break;
  8327.       case _CMTXT:            /* (4) As-name */
  8328.     if (rcvcmd) {
  8329.         ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ);
  8330.         if ((int)strlen(tmpbuf) > 0)
  8331.           asname = 1;
  8332.     } else {
  8333.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  8334.     }
  8335.       case _CMCFM:            /* (6) Confirmation */
  8336.     confirmed = 1;
  8337.     break;
  8338.       default:
  8339.     printf("?Unexpected function code: %d\n",cmresult.fcode);
  8340.     x = -9;
  8341.     goto xgetx;
  8342.     }
  8343.     debug(F110,"xget string",cmarg,0);
  8344.     debug(F101,"xget confirmed","",confirmed);
  8345.  
  8346.     cmarg = brstrip(cmarg);        /* Strip any braces */
  8347.  
  8348.     if (!confirmed) {            /* CR not typed yet, get more fields */
  8349.     if (pv[SND_CMD].ival > 0) {
  8350.         debug(F100,"xget calling cmtxt","",0);
  8351.         x = cmtxt("Local command to pipe into","",&s,NULL);
  8352.         if (x < 0 && x != -3) goto xgetx;
  8353.         if (x != -3) {
  8354.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8355.         asname = 1;
  8356.         }
  8357.     } else if (!rcvcmd) {
  8358. #ifdef VMS
  8359.         /* cmofi() fails if you give it a directory name */
  8360.         x = cmfld("Name or directory for incoming file","",&s,NULL);
  8361.         debug(F111,"xget cmfld",s,x);
  8362. #else
  8363.         x = cmofi("Name or directory for incoming file","",&s,NULL);
  8364.         debug(F111,"xget cmofi",s,x);
  8365. #endif /* VMS */
  8366.         if (x < 0 && x != -3) goto xgetx;
  8367.         if (x != -3) {
  8368.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8369.         if ((x = cmcfm()) < 0) goto xgetx;
  8370.         asname = 1;
  8371.         }
  8372.     }
  8373.     }
  8374.     /* Arrive here with cmarg and cmarg2 all set */
  8375.  
  8376.     debug(F111,"xget asname",cmarg2,asname);
  8377.     if (!asname) {
  8378.     if (pv[SND_ASN].sval)
  8379.       ckstrncpy(tmpbuf,pv[SND_ASN].sval,TMPBUFSIZ);
  8380.     else
  8381.       tmpbuf[0] = NUL;
  8382.     }
  8383.     cmarg2 = brstrip(cmarg2);        /* Strip outer braces if any. */
  8384.     debug(F110,"xget cmarg",cmarg,0);
  8385.     debug(F110,"xget cmarg2",cmarg2,0);
  8386.  
  8387.     if (!*cmarg &&
  8388.     (cx == XXGET || cx == XXREGET || cx == XXCGET || cx == XXMGET)) {
  8389.     printf("?A remote file specification is required\n");
  8390.     x = -9;
  8391.     goto xgetx;
  8392.     }
  8393. #ifdef PIPESEND
  8394.     if (pv[SND_CMD].ival > 0) {        /* /COMMAND sets pipesend flag */
  8395.     x = -9;
  8396.     if (!*cmarg2) {
  8397.         printf("?Command required\n");
  8398.         goto xgetx;
  8399.     } else if (nopush) {
  8400.         printf("?Sorry, system command access is disabled\n");
  8401.         goto xgetx;
  8402.     } else if (rcvfilter) {
  8403.         printf("?Sorry, no GET /COMMAND while RECEIVE FILTER selected\n");
  8404.         goto xgetx;
  8405.     } else
  8406.       pipesend = 1;
  8407.     }
  8408.     debug(F101,"xget /COMMAND pipesend","",pipesend);
  8409. #endif /* PIPESEND */
  8410.  
  8411. #ifdef CK_RESEND
  8412.     if (pv[SND_RES].ival > 0) {        /* REGET or GET /RECOVER */
  8413. #ifdef RECURSIVE
  8414.     if (pv[SND_REC].ival > 0) {    /* RECURSIVE */
  8415. #ifdef COMMENT
  8416.         printf("?Unsupported option combination: /RECOVER /RECURSIVE\n");
  8417.         x = -9;
  8418.         goto xgetx;
  8419. #else
  8420.         opkt = 1;
  8421. #endif /* COMMENT */
  8422.     }
  8423. #endif /* RECURSIVE */
  8424.     if (pv[SND_DEL].ival > 0) {    /* /DELETE */
  8425. #ifdef COMMENT
  8426.         printf("?Unsupported option combination: /RECOVER /DELETE\n");
  8427.         x = -9;
  8428.         goto xgetx;
  8429. #else
  8430.         opkt = 1;
  8431. #endif /* COMMENT */
  8432.     }
  8433.     }
  8434. #endif /* CK_RESEND */
  8435.  
  8436.     if (pv[SND_EXC].ival > 0)        /* /EXCEPT */
  8437.       makelist(pv[SND_EXC].sval,rcvexcept,8);
  8438.  
  8439. #ifdef IKS_OPTION
  8440.     if (!rcvcmd
  8441. #ifdef CK_XYZ
  8442.          && protocol == PROTO_K
  8443. #endif /* CK_XYZ */
  8444.          ) {
  8445.         if (!iks_wait(KERMIT_REQ_START,1)) {
  8446.         printf(
  8447.           "?A Kermit Server is not available to process this command\n");
  8448.         x = -9;            /* correct the return code */
  8449.         goto xgetx;
  8450.         }
  8451.     }
  8452. #endif /* IKS_OPTION */
  8453.  
  8454. #ifdef CK_XYZ
  8455.     {
  8456.     int po, pg;            /* (for clarity) */
  8457.     po = pv[SND_PRO].ival;        /* /PROTOCOL option */
  8458.     pg = protocol;            /* Protocol global  */
  8459.     if ((rcvcmd && !*cmarg2) &&    /* If no as-name was given */
  8460.         /* and /PROTOCOL is XMODEM or global protocol is XMODEM... */
  8461.         ((po <  0 && (pg == PROTO_X || pg == PROTO_XC)) ||
  8462.          (po > -1 && (po == PROTO_X || po == PROTO_XC)))
  8463.         ) {
  8464.         printf(
  8465. "Sorry, you must specify a name when receiving a file with XMODEM protocol\n"
  8466.            );
  8467.         x = -9;
  8468.         goto xgetx;
  8469.     }
  8470.     }
  8471. #endif /* CK_XYZ */
  8472.  
  8473. #ifdef RECURSIVE
  8474.     if (pv[SND_REC].ival > 0) {        /* RECURSIVE */
  8475.     recursive = 1;
  8476.     pv[SND_PTH].ival = PATH_REL;    /* Implies relative pathnames too */
  8477.     }
  8478. #endif /* RECURSIVE */
  8479.  
  8480.     if (pv[SND_PIP].ival > -1) {
  8481.     g_usepipes = usepipes;
  8482.     usepipes = pv[SND_PIP].ival;
  8483.     }
  8484.  
  8485.     /* Save global protocol parameters */
  8486.  
  8487.     g_proto = protocol;
  8488. #ifdef CK_LABELED
  8489.     g_lf_opts = lf_opts;        /* Save labeled transfer options */
  8490. #endif /* CK_LABELED */
  8491.     g_urpsiz = urpsiz;            /* Receive packet length */
  8492.     g_spsizf = spsizf;            /* Send packet length flag */
  8493.     g_spsiz = spsiz;            /* Send packet length */
  8494.     g_spsizr = spsizr;            /* etc etc */
  8495.     g_spmax = spmax;
  8496.     g_wslotr = wslotr;
  8497.     g_prefixing = prefixing;
  8498.     g_fncact = fncact;
  8499.     g_fncnv = fncnv;
  8500.     g_fnspath = fnspath;
  8501.     g_fnrpath = fnrpath;
  8502.     g_xfrxla = xfrxla;
  8503.  
  8504.     if (pv[SND_PRO].ival > -1) {    /* Change according to switch */
  8505.     protocol = pv[SND_PRO].ival;
  8506.         if (ptab[protocol].rpktlen > -1)   /* copied from initproto() */
  8507.             urpsiz = ptab[protocol].rpktlen;
  8508.         if (ptab[protocol].spktflg > -1)
  8509.             spsizf = ptab[protocol].spktflg;
  8510.         if (ptab[protocol].spktlen > -1) {
  8511.             spsiz = ptab[protocol].spktlen;
  8512.             if (spsizf)
  8513.                 spsizr = spmax = spsiz;
  8514.         }
  8515.         if (ptab[protocol].winsize > -1)
  8516.             wslotr = ptab[protocol].winsize;
  8517.         if (ptab[protocol].prefix > -1)
  8518.             prefixing = ptab[protocol].prefix;
  8519.         if (ptab[protocol].fnca > -1)
  8520.             fncact  = ptab[protocol].fnca;
  8521.         if (ptab[protocol].fncn > -1)
  8522.             fncnv   = ptab[protocol].fncn;
  8523.         if (ptab[protocol].fnsp > -1)
  8524.             fnspath = ptab[protocol].fnsp;
  8525.         if (ptab[protocol].fnrp > -1)
  8526.             fnrpath = ptab[protocol].fnrp;
  8527.     }
  8528.     debug(F101,"xget protocol","",protocol);
  8529.     debug(F111,"xget cmarg2",cmarg2,xfermode);
  8530.  
  8531.     g_xfermode = xfermode;
  8532.     g_binary = binary;
  8533.     if (pv[SND_BIN].ival > 0) {        /* Change according to switch */
  8534.     xfermode = XMODE_M;
  8535.     binary = XYFT_B;        /* FILE TYPE BINARY */
  8536.     omode = GMOD_BIN;        /* O-Packet mode */
  8537.     debug(F101,"doxget /BINARY xfermode","",xfermode);
  8538.     } else if (pv[SND_TXT].ival > 0) {    /* Ditto for /TEXT */
  8539.     xfermode = XMODE_M;
  8540.     binary = XYFT_T;
  8541.     omode = GMOD_TXT;
  8542.     debug(F101,"doxget /TEXT xfermode","",xfermode);
  8543.     } else if (pv[SND_IMG].ival > 0) {
  8544.     xfermode = XMODE_M;
  8545. #ifdef VMS
  8546.     binary = XYFT_I;
  8547. #else
  8548.     binary = XYFT_B;
  8549. #endif /* VMS */
  8550.     omode = GMOD_TXT;
  8551.     debug(F101,"doxget /IMAGE xfermode","",xfermode);
  8552.     }
  8553. #ifdef CK_LABELED
  8554.     else if (pv[SND_LBL].ival > 0) {
  8555.     xfermode = XMODE_M;
  8556.     binary = XYFT_L;
  8557.     omode = GMOD_LBL;
  8558.     debug(F101,"doxget /LABELED xfermode","",xfermode);
  8559.     }
  8560. #endif /* CK_LABELED */
  8561.     debug(F101,"xget binary","",binary);
  8562.     debug(F101,"xget omode","",omode);
  8563.  
  8564.     if (pv[SND_XPA].ival > 0)        /* /TRANSPARENT */
  8565.       xfrxla = 0;            /* Don't translate character sets */
  8566.  
  8567. #ifdef PIPESEND
  8568.     if (pv[SND_FLT].ival > 0)
  8569.       makestr(&rcvfilter,pv[SND_FLT].sval);
  8570. #endif /* PIPESEND */
  8571.  
  8572. #ifdef CK_TMPDIR
  8573.     if (pv[SND_MOV].ival > 0) {
  8574.     int len;
  8575.     char * p = pv[SND_MOV].sval;
  8576. #ifdef CK_LOGIN
  8577.     if (isguest) {
  8578.         printf("?Sorry, /MOVE-TO not available to guests\n");
  8579.         x = -9;
  8580.         goto xgetx;
  8581.     }
  8582. #endif /* CK_LOGIN */
  8583.     len = strlen(p);
  8584.     if (!isdir(p)) {        /* Check directory */
  8585. #ifdef CK_MKDIR
  8586.         char * s = NULL;
  8587.         s = (char *)malloc(len + 4);
  8588.         if (s) {
  8589.         strcpy(s,p);        /* safe */
  8590. #ifdef datageneral
  8591.         if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  8592. #else
  8593.         if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  8594. #endif /* datageneral */
  8595.         s[len++] = 'X';
  8596.         s[len] = NUL;
  8597.         x = zmkdir(s);
  8598.         free(s);
  8599.         if (x < 0) {
  8600.             printf("?Can't create \"%s\"\n",p);
  8601.             x = -9;
  8602.             goto xgetx;
  8603.         }
  8604.         }
  8605. #else
  8606.         printf("?Directory \"%s\" not found\n",p);
  8607.         x = -9;
  8608.         goto xgetx;
  8609. #endif /* CK_MKDIR */
  8610.     }
  8611.     makestr(&rcv_move,p);
  8612.     }
  8613. #endif /* CK_TMPDIR */
  8614.  
  8615.     if (pv[SND_REN].ival > 0) {        /* /RENAME-TO:name */
  8616.     char * p = pv[SND_REN].sval;
  8617. #ifdef CK_LOGIN
  8618.     if (isguest) {
  8619.         printf("?Sorry, /RENAME-TO not available to guests\n");
  8620.         x = -9;
  8621.         goto xgetx;
  8622.     }
  8623. #endif /* CK_LOGIN */
  8624.     if (!p) p = "";
  8625.     if (!*p) {
  8626.         printf("?New name required for /RENAME\n");
  8627.         x = -9;
  8628.         goto xgetx;
  8629.     }
  8630.     p = brstrip(p);
  8631.     makestr(&rcv_rename,p);
  8632.     debug(F110,"xget rcv_rename","",0);
  8633.     }
  8634.  
  8635. #ifdef CALIBRATE
  8636.     if (pv[SND_CAL].ival > 0)
  8637.       calibrate = 1L;
  8638. #endif /* CALIBRATE */
  8639.     g_displa = fdispla;
  8640.     if (pv[SND_SHH].ival > 0)
  8641.       fdispla = 0;
  8642.     debug(F101,"xget display","",fdispla);
  8643.  
  8644.     if (pv[SND_NAM].ival > -1) {    /* /FILENAMES */
  8645.     g_fncnv = fncnv;        /* Save global value */
  8646.     fncnv = pv[SND_NAM].ival;
  8647.     debug(F101,"xsend fncnv","",fncnv);
  8648.     /* We should also handle O packet filename option here */
  8649.     /* but we don't really need to since WHATAMI already handles it */
  8650.     }
  8651.     if (pv[SND_PTH].ival > -1) {    /* PATHNAMES */
  8652.     g_rpath = fnrpath;        /* Save global values */
  8653.     fnrpath = pv[SND_PTH].ival;
  8654.     debug(F101,"xsend fnrpath","",fnrpath);
  8655. #ifndef NZLTOR
  8656.     if (fnrpath != PATH_OFF) {
  8657.         g_fncnv = fncnv;
  8658.         fncnv = XYFN_L;
  8659.         debug(F101,"xsend fncnv","",fncnv);
  8660.     }
  8661.     /* We should also handle O packet pathname option here */
  8662.     /* but we don't really need to since WHATAMI already handles it */
  8663. #endif /* NZLTOR */
  8664.     }
  8665.  
  8666.     /* Set protocol start state */
  8667.  
  8668.     if (opkt) {                /* Extended GET Options*/
  8669.     sstate = (CHAR) 'o';
  8670.     oopts = 0;
  8671.     if (pv[SND_DEL].ival > 0) oopts |= GOPT_DEL; /* GET /DELETE */
  8672.     if (pv[SND_RES].ival > 0) oopts |= GOPT_RES; /* GET /RECOVER */
  8673.     if (pv[SND_REC].ival > 0) oopts |= GOPT_REC; /* GET /RECURSIVE */
  8674.     } else if (rcvcmd)
  8675.       sstate = (CHAR) 'v';        /* RECEIVE or CRECEIVE */
  8676.     else if (pv[SND_DEL].ival > 0)
  8677.       sstate = (CHAR) 'h';        /* GET /DELETE (= RETRIEVE) */
  8678.     else if (pv[SND_RES].ival > 0)
  8679.       sstate = (CHAR) 'j';        /* GET /RECOVER (= REGET) */
  8680.     else
  8681.       sstate = (CHAR) 'r';        /* Regular GET */
  8682.     getcmd = 1;
  8683.     debug(F000,"xget sstate","",sstate);
  8684. #ifdef MAC
  8685.     what = W_RECV;
  8686.     scrcreate();
  8687. #endif /* MAC */
  8688.     if (local) {
  8689.     if (pv[SND_SHH].ival != 0)
  8690.       displa = 1;
  8691.     ttflui();
  8692.     }
  8693.     x = 0;
  8694. #ifdef PIPESEND
  8695.     if (pipesend)
  8696.       goto xgetx;
  8697. #endif /* PIPESEND */
  8698.  
  8699. #ifdef CK_TMPDIR
  8700. /*
  8701.   cmarg2 is also allowed to be a device or directory name;
  8702.   even the name of a directory that doesn't exist.
  8703. */
  8704.     y = strlen(cmarg2);
  8705.     debug(F111,"xget strlen(cmarg2)",cmarg2,y);
  8706.     if ((y > 0) &&
  8707. #ifdef OS2
  8708.     ((isalpha(cmarg2[0]) &&
  8709.      cmarg2[1] == ':' &&
  8710.      cmarg2[2] == NUL) ||
  8711.     (cmarg[y-1] == '/' || cmarg[y-1] == '\\') ||
  8712.     isdir(cmarg2))
  8713. #else
  8714. #ifdef UNIXOROSK
  8715.     (cmarg2[y-1] == '/' || isdir(cmarg2))
  8716. #else
  8717. #ifdef VMS
  8718.     (cmarg2[y-1] == ']' || cmarg2[y-1] == '>' || isdir(cmarg2))
  8719. #else
  8720. #ifdef STRATUS
  8721.     (cmarg2[y-1] == '>' || isdir(cmarg2))
  8722. #else
  8723. #ifdef datageneral
  8724.     (cmarg2[y-1] == ':' || cmarg[0] == ':' || isdir(cmarg2))
  8725. #else
  8726.     isdir(cmarg2)
  8727. #endif /* datageneral */
  8728. #endif /* STRATUS */
  8729. #endif /* VMS */
  8730. #endif /* UNIXOROSK */
  8731. #endif /* OS2 */
  8732.     ) {
  8733.     debug(F110,"doxget RECEIVE cmarg2 disk or dir",cmarg2,0);
  8734.     if (!f_tmpdir) {
  8735.         int x;
  8736.         s = zgtdir();
  8737.         if (s) {
  8738.         ckstrncpy(savdir,s,TMPDIRLEN); /* remember old disk/dir */
  8739.         f_tmpdir = 1;    /* and that we did this */
  8740.         } else {
  8741.         printf("?Can't get current directory\n");
  8742.         cmarg2 = "";
  8743.         f_tmpdir = 0;
  8744.         x = -9;
  8745.         goto xgetx;
  8746.         }
  8747. #ifdef CK_MKDIR
  8748.         x = zchki(cmarg2);        /* Does as-name exist? */
  8749.         if (x == -1) {        /* Doesn't exist */
  8750.         char * p = NULL;    /* Try to create it */
  8751.         x = strlen(cmarg2);
  8752.         if ((p = (char *)malloc(x+4))) {
  8753.             sprintf(p,"%s%s",cmarg2,"x.x"); /* SAFE (prechecked) */
  8754.             x = zmkdir(p);
  8755.             free(p);
  8756.             if (x < 0) {
  8757.             printf("?Can't create %s\n",cmarg2);
  8758.             x = -9;
  8759.             goto xgetx;
  8760.             }
  8761.         }
  8762.         }
  8763. #endif /* CK_MKDIR */
  8764.         if (!zchdir(cmarg2)) {    /* change to given disk/directory, */
  8765.         printf("?Can't access %s\n",cmarg2);
  8766.         x = -9;
  8767.         goto xgetx;
  8768.         }
  8769.         cmarg2 = "";
  8770.     }
  8771.     }
  8772. #endif /* CK_TMPDIR */
  8773.  
  8774.     ckstrncpy(fspec,cmarg,CKMAXPATH);    /* Note - this is a REMOTE filespec */
  8775.     debug(F111,"xget fspec",fspec,fspeclen);
  8776.     debug(F110,"xget cmarg2",cmarg2,0);
  8777.  
  8778.   xgetx:
  8779.     for (i = 0; i < SND_MAX; i++)
  8780.       if (pv[i].sval)
  8781.     free(pv[i].sval);
  8782.     return(x);
  8783. }
  8784. #endif /* NOXFER */
  8785.  
  8786. #ifndef NOSPL
  8787.  
  8788. /*
  8789.   D O G T A  --  Do _GETARGS or _PUTARGS Command.
  8790.  
  8791.   Used by XIF, FOR, WHILE, and SWITCH, each of which are implemented as
  8792.   2-level macros; the first level defines the macro, the second runs it.
  8793.   This routine hides the fact that they are macros by importing the
  8794.   macro arguments (if any) from two levels up, to make them available
  8795.   in the IF, FOR, SWITCH, and WHILE commands themselves; for example as
  8796.   loop indices, etc, and within the IF/FOR/WHILE/SWITCH body itself.
  8797.   _PUTARGS is in case we changed any of these variables or used SHIFT
  8798.   on them, so the new values won't be lost as we pop up the stack.
  8799. */
  8800. int
  8801. dogta(cx) int cx; {
  8802.     int i, n;
  8803.     char c, *p,  mbuf[4];
  8804.     extern int topargc, cmdint;
  8805.     extern char ** topxarg;
  8806.  
  8807.     if ((y = cmcfm()) < 0)
  8808.       return(y);
  8809.     debug(F101,"dogta cx","",cx);
  8810.     debug(F101,"dogta maclvl","",maclvl);
  8811.     if (cx == XXGTA) {
  8812.     debug(F101,"dogta _GETARGS maclvl","",maclvl);
  8813.     } else if (cx == XXPTA) {
  8814.     debug(F101,"dogta _PUTARGS maclvl","",maclvl);
  8815.     } else {
  8816.     return(-2);
  8817.     }
  8818.     if (maclvl < 1)
  8819.       return(success = 0);
  8820.  
  8821.     /* Make new copies of macro arguments /%0..9 */
  8822.  
  8823.     mbuf[0] = '%'; mbuf[1] = '0'; mbuf[2] = NUL; /* Argument name buf */
  8824.  
  8825.     if (cx == XXPTA) {            /* Go NOINT because _PUTARGS */
  8826.     if (cmdint)            /* temporarily changes maclvl. */
  8827.       connoi();            /* Interrupts OFF. */
  8828.     }
  8829.     for (i = 0; i < 10; i++) {        /* For all args */
  8830.     c = (char) (i + '0');        /* Make name */
  8831.     mbuf[1] = (char) c;        /* Insert digit */
  8832.     if (cx == XXGTA) {        /* Get arg from level-minus-2 */
  8833.         if (maclvl == 1) p = g_var[c]; /* If at level 1 use globals 0..9 */
  8834.         else p = m_arg[maclvl-2][i];   /* Otherwise they're on the stack */
  8835.         addmac(mbuf,p);
  8836. #ifdef COMMENT
  8837.         if (maclvl > 1)
  8838.           makestr(&(m_line[maclvl]),m_line[maclvl-2]);
  8839. #endif /* COMMENT */
  8840.     } else if (cx == XXPTA) {    /* Put args level+2 */
  8841.         maclvl -= 2;        /* This is gross, it's because we're */
  8842.         addmac(mbuf,m_arg[maclvl+2][i]); /* adding macros two levels up */
  8843.         maclvl += 2;             /* and addmac() uses maclvl. */
  8844.         count[cmdlvl - 2]  = count[cmdlvl];
  8845.         intime[cmdlvl - 2] = intime[cmdlvl];
  8846.         inpcas[cmdlvl - 2] = inpcas[cmdlvl];
  8847.         takerr[cmdlvl - 2] = takerr[cmdlvl];
  8848.         merror[cmdlvl - 2] = merror[cmdlvl];
  8849.         xquiet[cmdlvl - 2] = xquiet[cmdlvl];
  8850.     } else return(success = 0);    /* Bad call to this routine */
  8851.     }
  8852.     if (cx == XXPTA) {            /* Restore interrupts if we */
  8853.     if (cmdint)            /* turned them off above. */
  8854.       conint(trap,stptrap);
  8855.     }
  8856.     /* Now take care of the argument vector array \&_[], \v(return), */
  8857.     /* and \v(argc) by just copying the pointers. */
  8858.  
  8859.     if (cx == XXGTA) {            /* GETARGS from 2 levels up */
  8860.     if (maclvl == 1) {
  8861.         a_ptr[0] = topxarg;        /* \&_[] array */
  8862.         a_dim[0] = topargc - 1;    /* Dimension doesn't include [0] */
  8863.         m_xarg[maclvl] = topxarg;
  8864.         n_xarg[maclvl] = topargc;    /* But \v(argc) does include \%0 */
  8865.         macargc[maclvl] = topargc;
  8866.             makestr(&(mrval[maclvl+1]),mrval[0]); /* (see vnlook()) */
  8867.     } else {
  8868.         a_ptr[0] = m_xarg[maclvl-2];
  8869.         a_dim[0] = n_xarg[maclvl-2];
  8870.         m_xarg[maclvl] = m_xarg[maclvl-2];
  8871.         n_xarg[maclvl] = n_xarg[maclvl-2];
  8872.         macargc[maclvl] = n_xarg[maclvl-2];
  8873.             makestr(&(mrval[maclvl+1]),mrval[maclvl-1]); /* (see vnlook()) */
  8874.  
  8875.     }
  8876.     } else {                /* PUTARGS 2 levels up */
  8877.     if (maclvl > 1) {
  8878.         a_ptr[0] = m_xarg[maclvl];
  8879.         m_xarg[maclvl-2] = m_xarg[maclvl];
  8880.         a_dim[0] = n_xarg[maclvl];
  8881.         n_xarg[maclvl-2] = n_xarg[maclvl];
  8882.         macargc[maclvl-2] = n_xarg[maclvl];
  8883.     }
  8884.     }
  8885.     return(1);            /* Internal command - don't change success */
  8886. }
  8887. #endif /* NOSPL */
  8888.  
  8889. #ifndef NOSPL
  8890. /*
  8891.   Do the GOTO and [_]FORWARD commands.
  8892.   s = Label to search for, cx = function code: XXGOTO, XXFWD, or XXXFWD.
  8893. */
  8894. #ifdef BIGBUFOK
  8895. #define LBLMAXLEN 255            /* Max label length */
  8896. #else
  8897. #define LBLMAXLEN 63
  8898. #endif /* BIGBUFOK */
  8899.  
  8900. int
  8901. dogoto(s, cx) char *s; int cx; {
  8902.     int i, j, x, y, z, bc;
  8903.     int empty = 0, stopflg = 0;
  8904.     char * cmd;                /* Name of this command */
  8905.     char tmplbl[LBLMAXLEN+1], *lp;    /* Current label from command stream */
  8906.     char tmp2[LBLMAXLEN+1];        /* SWITCH label conversion buffer */
  8907.     char tmp3[LBLMAXLEN+1];        /* Target label */
  8908.  
  8909.     stopflg = (cx == XXXFWD);        /* _FORWARD (used in SWITCH) */
  8910.     bc = 0;                /* Brace counter */
  8911.  
  8912.     cmd = (cx == XXGOTO) ? "GOTO" : ((cx == XXFWD) ? "FORWARD" : "_FORWARD");
  8913.     if (!s) s = "";
  8914.     if (!*s) empty = 1;
  8915.  
  8916. #ifdef DEBUG
  8917.     if (deblog) {
  8918.     debug(F111,"GOTO command",cmd,cx);
  8919.     debug(F101,"GOTO cmdlvl","",cmdlvl);
  8920.     debug(F101,"GOTO maclvl","",maclvl);
  8921.     debug(F101,"GOTO tlevel","",tlevel);
  8922.     debug(F111,"GOTO target",s,empty);
  8923.     }
  8924. #endif /* DEBUG */
  8925.     debug(F110,cmd,s,0);
  8926.     ckstrncpy(tmp3+1,s,LBLMAXLEN-1);
  8927.     s = tmp3+1;
  8928.     if (*s != ':') {            /* Make copy of label */
  8929.     tmp3[0] = ':';            /* guaranteed to start with ":" */
  8930.     s--;
  8931.     }
  8932.     if (!stopflg && !empty) {
  8933.     if (s[1] == '.' || s[1] == SP || s[1] == NUL) {
  8934.         printf("?Bad label syntax - '%s'\n",s);
  8935.         return(success = 0);
  8936.     }
  8937.     }
  8938.     if (cmdlvl == 0) {
  8939.     printf("?Sorry, %s only works in a command file or macro\n",cmd);
  8940.     return(success = 0);
  8941.     }
  8942.     y = strlen(s);            /* y = length of target label */
  8943.     debug(F111,cmd,s,y);
  8944.  
  8945.     while (cmdlvl > 0) {        /* As long as not at top level... */
  8946.     if (cmdstk[cmdlvl].src == CMD_MD) { /* GOTO inside macro */
  8947.         int i, m, flag;
  8948.         char *xp, *tp;
  8949.  
  8950.         /* GOTO: rewind the macro; FORWARD: start at current position */
  8951.  
  8952.         lp = (cx == XXGOTO) ? macx[maclvl] : macp[maclvl];
  8953.         m = (int)strlen(lp) - y + 1;
  8954.         debug(F010,"GOTO in macro",lp,0);
  8955.  
  8956.         flag = 1;            /* flag for valid label position */
  8957.         for (i = 0; i < m; i++,lp++) { /* search for label in macro body */
  8958.         if (*lp == '{')        /* But only at this level */
  8959.           bc++;            /* Anything inside braces is off */
  8960.         else if (*lp == '}')    /* limits. */
  8961.           bc--;
  8962.         if (stopflg && bc > 0)    /* This is good for SWITCH */
  8963.           continue;        /* but interferes with WHILE, etc. */
  8964.         if (*lp == ',') {
  8965.             flag = 1;
  8966.             continue;
  8967.         }
  8968.         if (flag) {        /* If in valid label position */
  8969.             if (*lp == SP)    /* eat leading spaces */
  8970.               continue;
  8971.             if (*lp != ':') {    /* Look for label introducer */
  8972.             flag = 0;    /* this isn't it */
  8973.             continue;    /* keep looking */
  8974.             }
  8975.         }
  8976.         if (!flag)        /* We don't have a label */
  8977.           continue;        /*  so keep looking... */
  8978.         xp = lp; tp = tmplbl;    /* Copy the label from the macro */
  8979.         j = 0;            /* to make it null-terminated */
  8980.         while ((*tp = *xp)) {
  8981.             if (j++ > LBLMAXLEN) /* j = length of word from macro */
  8982.               break;
  8983. #ifdef COMMENT
  8984.             if (*tp < 33 || *tp == ',')    /* Look for end of word */
  8985. #else
  8986.             if (!*tp || *tp == ',')    /* Look for end of word */
  8987. #endif /* COMMENT */
  8988.               break;
  8989.             else tp++, xp++;    /* Next character */
  8990.         }
  8991.         *tp = NUL;        /* In case we stopped early */
  8992.         /* Now do caseless string comparison, using longest length */
  8993.         debug(F111,"macro GOTO label",s,y);
  8994.         debug(F111,"macro target label",tmplbl,j);
  8995.         if (stopflg) {        /* Allow variables as SWITCH labels */
  8996.             int n = LBLMAXLEN - 1;
  8997.             char * p = tmp2;
  8998.             zzstring(tmplbl,&p,&n);
  8999.             ckstrncpy(tmplbl,tmp2,LBLMAXLEN);
  9000.             tmp2[49] = NUL;
  9001.         }
  9002.         debug(F111,"GOTO s",s,y);
  9003.         debug(F111,"GOTO tmplbl",tmplbl,j);
  9004.         debug(F101,"GOTO empty",ckitoa(stopflg),empty);
  9005.  
  9006.         if (empty) {
  9007.             z = 1;
  9008.         } else if (stopflg) {
  9009.             z = ckmatch(tmplbl,s,inpcas[cmdlvl],1) ? 0 : 1;
  9010.         } else {
  9011.             z = (stopflg && inpcas[cmdlvl]) ?
  9012.               strcmp(s,tmplbl) :
  9013.             ckstrcmp(s,tmplbl,(y > j) ? y : j, 0);
  9014.         }
  9015.         debug(F101,"GOTO z","",z);
  9016.         if (!z) {
  9017.             break;
  9018.         } else if (stopflg &&
  9019.              !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j, 0)) {
  9020.             debug(F100,"GOTO DEFAULT","",0);
  9021.             break;
  9022.         } else {
  9023.             flag = 0;
  9024.         }
  9025.         }
  9026.         debug(F111,"GOTO macro i",cmd,i);
  9027.         debug(F111,"GOTO macro m",cmd,m);
  9028.         if (i >= m) {        /* Didn't find the label */
  9029.         debug(F101,"GOTO failed cmdlvl","",cmdlvl);
  9030.         if (stopflg)
  9031.           return(0);
  9032.         if ((maclvl > 0) &&
  9033.                (m_arg[maclvl-1][0]) &&
  9034.                (cmdstk[cmdlvl].src == CMD_MD) &&
  9035.                (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  9036.             !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  9037.             !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  9038.             !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  9039.             dogta(XXPTA);    /* Restore args */
  9040.             debug(F101,"GOTO in XIF/FOR/WHI/SWI popping","",cmdlvl);
  9041.             popclvl();        /* Pop an extra level */
  9042.         }
  9043.         debug(F101,"GOTO popping","",cmdlvl);
  9044.         if (!popclvl()) {    /* pop up to next higher level */
  9045.             printf("?Label '%s' not found\n",s); /* if none */
  9046.             return(0);        /* quit */
  9047.         } else continue;    /* otherwise look again */
  9048.         }
  9049.         debug(F110,"GOTO found macro label",tmplbl,0);
  9050.         macp[maclvl] = lp;        /* set macro buffer pointer */
  9051.         return(1);
  9052.     } else if (cmdstk[cmdlvl].src == CMD_TF) {
  9053.         x = 0;            /* GOTO issued in take file */
  9054.         debug(F111,"GOTO in TAKE file",cmd,cx);
  9055.         if (cx == XXGOTO) {        /* If GOTO, but not FORWARD, */
  9056.         rewind(tfile[tlevel]);    /* search file from beginning */
  9057.         tfline[tlevel] = 0;
  9058.         }
  9059.         while (! feof(tfile[tlevel])) {
  9060. #ifdef COMMENT
  9061. /* This is wrong - it lets us jump to labels inside inferior blocks */
  9062.         if (fgets(line,LINBUFSIZ,tfile[tlevel]) == NULL) /* Get line */
  9063. #else
  9064.         if (getnct(line,LINBUFSIZ,tfile[tlevel],0) < 0)
  9065. #endif /* COMMENT */
  9066.           break;        /* If no more, done, label not found */
  9067.         tfline[tlevel]++;
  9068.         lp = line;        /* Got line */
  9069.         while (*lp == SP || *lp == HT)
  9070.           lp++;            /* Strip leading whitespace */
  9071.         if (*lp != ':') continue; /* Check for label introducer */
  9072.         while (*(lp+1) == SP) {    /* Remove space between : and name */
  9073.             *(lp+1) = ':';
  9074.             lp++;        /* Strip leading whitespace */
  9075.         }
  9076.         tp = lp;        /* Get end of word */
  9077.         j = 0;
  9078.         while (*tp) {        /* And null-terminate it */
  9079.             if (*tp < 33) {
  9080.             *tp = NUL;
  9081.             break;
  9082.             } else tp++, j++;
  9083.         }
  9084.         if (!ckstrcmp(lp,s,(y > j) ? y : j,0)) { /* Caseless compare */
  9085.             x = 1;        /* Got it */
  9086.             break;        /* done. */
  9087.         } else if (stopflg &&
  9088.                !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j,0)) {
  9089.             x = 1;
  9090.             break;
  9091.         }
  9092.         }
  9093.         if (x == 0) {        /* If not found, print message */
  9094.         debug(F101,"GOTO failed at cmdlvl","",cmdlvl);
  9095.         if (stopflg)
  9096.           return(0);
  9097.         if (!popclvl()) {    /* pop up to next higher level */
  9098.             printf("?Label '%s' not found\n",s); /* if none */
  9099.             return(0);        /* quit */
  9100.         } else continue;    /* otherwise look again */
  9101.         }
  9102.         return(x);            /* Send back return code */
  9103.     }
  9104.     }
  9105.     printf("?Stack problem in GOTO %s\n",s); /* Shouldn't see this */
  9106.     return(0);
  9107. }
  9108. #endif /* NOSPL */
  9109.  
  9110. /* Finish parsing and do the IF, XIF, and WHILE commands */
  9111.  
  9112. #ifndef NOSPL
  9113.  
  9114. /*  C H K V A R  --  Check (if it's a) Variable  */
  9115.  
  9116.  
  9117. #ifdef OLDCHKVAR
  9118. /*
  9119.   Crude and disgusting, but needed for OS/2, DOS, and Windows, where filenames
  9120.   have backslashes in them.  How do we know if a backslash in a filename is a
  9121.   directory separator, or if it's a Kermit backslash?  This routine does a
  9122.   rough syntax check of the next few characters and if it looks like it MIGHT
  9123.   be a variable, then it tries to evaluate it, and if the result is not empty,
  9124.   we say it's a variable, although sometimes it might not be -- some cases are
  9125.   truly ambiguous.  For example there might a DOS directory called \%a, and
  9126.   we also have a variable with the same name.  This is all for the sake of not
  9127.   having to tell PC users that they have to double all backslashes in file
  9128.   and directory names.
  9129. */
  9130. #else
  9131. /*
  9132.   Somewhat less crude & disgusting.  The previous method was nondeterministic
  9133.   and (worse) it interfered with macro argument passing.  So now we only
  9134.   check the syntax of backslash-items to see if they are variables, but we
  9135.   do NOT check their values.
  9136. */
  9137. #endif /* OLDCHKVAR */
  9138. /*
  9139.   Call with a string pointer pointing at the backslash of the purported
  9140.   variable.  Returns 1 if it has the syntax of a variable, 0 if not.
  9141. */
  9142. int
  9143. chkvar(s) char *s; {
  9144.     int z = 0;                /* Return code - assume failure. */
  9145.     if (!s) s = "";            /* Watch our for null pointers. */
  9146.     if (!*s) return(0);            /* Empty arg so not a variable. */
  9147.     if (*s == CMDQ) {            /* Object begins with backslash. */
  9148.     char c;
  9149.     c = s[1];            /* Character following backslash. */
  9150.     if (c) {
  9151.         int t = 0;
  9152.         if (c == CMDQ)        /* Quoted backslash */
  9153.           return(1);
  9154.         c = (char) (islower(c) ? toupper(c) : c); /* Otherwise... */
  9155.         if (c == '%') {        /* Simple variable */
  9156. #ifdef OLDCHKVAR
  9157.         t = 1;
  9158. #else
  9159.         return(1);
  9160. #endif /* OLDCHKVAR */
  9161.         } else if (c == '&') {    /* Array */
  9162.         if (!s[2]) return(0);
  9163.         if (s[3] == '[')
  9164.           t = ckindex("]",s,4,0,1);
  9165. #ifndef OLDCHKVAR
  9166.         return((t > 0) ? 1 : 0);
  9167. #endif /* OLDCHKVAR */
  9168.         } else if (c == '$' ||    /* Environment variable */
  9169.                c == 'V' ||    /* Built-in variable */
  9170.                c == 'M') {    /* Macro name */
  9171.             t = (s[2] == '(');
  9172. #ifndef OLDCHKVAR
  9173.         return((t > 0) ? 1 : 0);
  9174. #endif /* OLDCHKVAR */
  9175.         } else if (c == 'F') {    /* Function reference */
  9176.         /* Don't actually call it - it might have side effects */
  9177.         int x;
  9178.         if ((x = ckindex("(",s,3,0,1))) /* Just check syntax */
  9179.           if ((x = ckindex(")",s,x,0,1)))
  9180.             z = 1;
  9181.         /* Insert a better syntax check here if necessary */
  9182.         }
  9183. #ifdef OLDCHKVAR
  9184.         if (t) {
  9185.         t = 255;        /* This lets us test \v(xxx) */
  9186.         lp = line;        /* and even \f...(xxx) */
  9187.         zzstring(s,&lp,&t);    /* Evaluate it, whatever it is. */
  9188.         t = strlen(line);    /* Get its length. */
  9189.         debug(F111,"chkvar",line,t);
  9190.         z = t > 0;        /* If length > 0, it's defined */
  9191.         }
  9192. #endif /* OLDCHKVAR */
  9193.     }
  9194.     }
  9195.     return(z);
  9196. }
  9197.  
  9198. /*  B O O L E X P  --  Evaluate a Boolean expression  */
  9199.  
  9200. #define BOOLLEN 1024
  9201. static char boolval[BOOLLEN];
  9202.  
  9203. int
  9204. boolexp(cx) int cx; {
  9205.     int x, y, z; char *s, *p;
  9206.     int parens = 0, pcount = 0, ecount = 0;
  9207.     char *q, *bx;
  9208.     struct FDB kw, nu;
  9209. #ifdef FNFLOAT
  9210.     struct FDB fl;
  9211.     CKFLOAT f1 = 0.0, f2 = 0.0;
  9212.     int f1flag = 0, f2flag = 0;
  9213. #endif /* FNFLOAT */
  9214. #ifdef OS2
  9215.     extern int keymac;
  9216. #endif /* OS2 */
  9217.  
  9218.     not = 0;                /* Flag for whether "NOT" was seen */
  9219.     z = 0;                /* Initial IF condition */
  9220.     ifargs = 0;                /* Count of IF condition words */
  9221.     bx = boolval;            /* Initialize boolean value */
  9222.     *bx = NUL;
  9223.  
  9224.   ifagain:
  9225.     cmfdbi(&kw,                /* First FDB - command switches */
  9226.        _CMKEY,            /* fcode */
  9227.        "Number, numeric-valued variable, Boolean expression, or keyword",
  9228.        "",                /* default */
  9229.        "",                /* addtl string data */
  9230.        nif,                /* addtl numeric data 1: tbl size */
  9231.        0,                /* addtl numeric data 2: 4 = silent */
  9232.        xxstring,            /* Processing function */
  9233.        iftab,            /* Keyword table */
  9234.        &nu                /* Pointer to next FDB */
  9235.        );
  9236.     cmfdbi(&nu,                /* 2nd FDB - An integer */
  9237.        _CMNUM,            /* fcode */
  9238.        "",                /* hlpmsg */
  9239.        "",                /* Default */
  9240.        "",                /* addtl string data */
  9241.        0,
  9242.        0,
  9243.        xxstring,
  9244.        NULL,
  9245. #ifdef FNFLOAT
  9246.        &fl
  9247. #else
  9248.        NULL
  9249. #endif /* FNFLOAT */
  9250.        );
  9251. #ifdef FNFLOAT
  9252.     cmfdbi(&fl,                /* A floating-point number */
  9253.        _CMFLD,            /* fcode */
  9254.        "",                /* hlpmsg */
  9255.        "",                /* default */
  9256.        "",                /* addtl string data */
  9257.        0,                /* addtl numeric data 1 */
  9258.        0,                /* addtl numeric data 2 */
  9259.        xxstring,
  9260.        NULL,
  9261.        NULL
  9262.        );
  9263. #endif /* FNFLOAT */
  9264.     x = cmfdb(&kw);            /* Parse a keyword or a number */
  9265.     debug(F111,"boolval cmfdb","",x);
  9266.     if (x < 0) {
  9267.     if (x == -3)
  9268.       x = -2;
  9269.     return(x);
  9270.     }
  9271.     debug(F111,"boolval switch","",cmresult.fcode);
  9272.     switch (cmresult.fcode) {        /* What did we get? */
  9273. #ifdef FNFLOAT
  9274.       case _CMFLD:            /* A "field" */
  9275.     if (isfloat(cmresult.sresult,0)) { /* A floating-point number? */
  9276.         f1 = floatval;        /* Yes, get its value */
  9277.         f1flag = 1;            /* remember we did this */
  9278.         ifc = 9999;            /* Set special "if-code" */
  9279.     } else
  9280.       return(-2);
  9281. #endif /* FNFLOAT */
  9282.       case _CMNUM:            /* A number... */
  9283.     ifc = 9999;            /* Set special "if-code" */
  9284.     break;
  9285.       case _CMKEY:            /* A keyword */
  9286.     ifc = cmresult.nresult;        /* Get if-code */
  9287.     break;
  9288.       default:
  9289.     return(-2);
  9290.     }
  9291.     switch (ifc) {            /* set z = 1 for true, 0 for false */
  9292.       case 9999:            /* Number */
  9293. #ifdef FNFLOAT
  9294.     if (f1flag) {
  9295.         z = (f1 == 0.0) ? 0 : 1;
  9296.     } else
  9297. #endif /* FNFLOAT */
  9298.     z = (cmresult.nresult == 0) ? 0 : 1;
  9299.     break;
  9300.       case XXIFLP:            /* Left paren */
  9301.     if (pcount == 0 && ifargs > 0)
  9302.       return(-2);
  9303.     parens = 1;
  9304.     pcount++;
  9305.     ifargs++;
  9306.     *bx++ = '(';
  9307.     goto ifagain;
  9308.       case XXIFRP:            /* Right paren */
  9309.     if (!parens)
  9310.       return(-2);
  9311.     if (--pcount < 0)
  9312.       return(-2);
  9313.     ifargs++;
  9314.     *bx++ = ')';
  9315.     *bx = NUL;
  9316.     if (pcount == 0)
  9317.       goto ifend;
  9318.     goto ifagain;
  9319.       case XXIFAN:            /* AND (&&) */
  9320.     ifargs++;
  9321.     if (!ecount)
  9322.       return(-2);
  9323.     *bx++ = '&';
  9324.     goto ifagain;
  9325.       case XXIFOR:            /* OR (||) */
  9326.     ifargs++;
  9327.     if (!ecount)
  9328.       return(-2);
  9329.     *bx++ = '|';
  9330.     goto ifagain;
  9331.       case XXIFNO:            /* IF NOT [ NOT [ NOT ... ] ] */
  9332.     if (bx > boolval) {        /* evala() doesn't like cascaded */
  9333.         if (*(bx-1) == '!') {    /* unary operators... */
  9334.         *(bx-1) = NUL;        /* So here, two wrongs make a right. */
  9335.         bx--;
  9336.         } else {
  9337.         *bx++ = '!';
  9338.         }
  9339.     } else {
  9340.         *bx++ = '!';
  9341.     }
  9342.     ifargs++;
  9343.     goto ifagain;
  9344.       case XXIFTR:            /* IF TRUE */
  9345.     z = 1;
  9346.     debug(F101,"if true","",z);
  9347.     ifargs += 1;
  9348.     break;
  9349.       case XXIFNT:            /* IF FALSE */
  9350.     z = 0;
  9351.     debug(F101,"if true","",z);
  9352.     ifargs += 1;
  9353.     break;
  9354.       case XXIFSU:            /* IF SUCCESS */
  9355.     z = ( success != 0 ) ? 1 : 0;
  9356.     debug(F101,"if success","",z);
  9357.     ifargs += 1;
  9358.     break;
  9359.       case XXIFFA:            /* IF FAILURE */
  9360.     z = ( success == 0 ) ? 1 : 0;
  9361.     debug(F101,"if failure","",z);
  9362.     ifargs += 1;
  9363.     break;
  9364.  
  9365.       case XXIFDE:            /* IF DEFINED */
  9366.     if ((x = cmfld("Macro or variable name","",&s,NULL)) < 0)
  9367.       return((x == -3) ? -2 : x);
  9368.  
  9369.     if (*s == CMDQ) {
  9370.         char * lp;
  9371.         char line[256];
  9372.         int t, x;
  9373.         if (*(s+1) == 'f' || *(s+1) == 'F') { /* Built-in function */
  9374.         extern struct keytab fnctab[];
  9375.         extern int nfuncs;
  9376.         ckstrncpy(line,s+2,256);
  9377.         if (line[0]) {
  9378.             lp = ckstrchr(line,'(');
  9379.             if (lp) *lp = NUL;
  9380.             x = lookup(fnctab,line,nfuncs,&t);
  9381.             z = x > -1;
  9382.         }
  9383.         debug(F111,"if defined function",line,z);
  9384.         } else if (*(s+1) == 'v' || *(s+1) == 'V') {
  9385.         extern struct keytab vartab[];
  9386.         extern int nvars;
  9387.         z = 0;
  9388.         if (*(s+2) == '(') {
  9389.             ckstrncpy(line,s+3,256);
  9390.             if (line[0]) {
  9391.             lp = ckstrchr(line,')');
  9392.             if (lp) *lp = NUL;
  9393.             x = lookup(vartab,line,nvars,&t);
  9394.             z = x > -1;
  9395.             }
  9396.         }
  9397.         debug(F111,"if defined variable",line,z);
  9398.         } else {
  9399.         z = chkvar(s);        /* Starts with backslash */
  9400.         if (z > 0) {        /* Yes... */
  9401.             t = 255;        /* than buffer so if zzstring fails  */
  9402.             lp = line;        /* check for that -- overflow means */
  9403.             line[0] = NUL;    /* the quantity is defined. */
  9404.             x = zzstring(s,&lp,&t);
  9405.             if ((x < 0 && t != 255) || !line[0])
  9406.               z = 0;
  9407.             debug(F111,"if defined zzstring",line,z);
  9408.             debug(F101,"if defined zzstring t","",t);
  9409.         }
  9410.         }
  9411.     } else {
  9412.         z = (mxlook(mactab,s,nmac) > -1); /* Look for exact match */
  9413.     }
  9414.     debug(F111,"if defined final",s,z);
  9415.     ifargs += 2;
  9416.     break;
  9417.  
  9418.       case XXIFDC: {            /* IF DECLARED */
  9419.       char * lp;
  9420.       char line[32];
  9421.       int j, k, t, x;
  9422.       if ((x = cmfld("Array name","",&s,NULL)) < 0)
  9423.         return((x == -3) ? -2 : x);
  9424.       if (*s == CMDQ) {
  9425.           if (*(s+1) != '&') {
  9426.           t = 31;
  9427.           lp = line;
  9428.           line[0] = NUL;
  9429.           x = zzstring(s,&lp,&t);
  9430.           s = line;
  9431.           }
  9432.       }
  9433.       z = 0;
  9434.       if ((x = arraybounds(s,&j,&k)) > -1) {
  9435.           if (a_ptr[x]) {
  9436.           if (j < 1)
  9437.             z = 1;
  9438.           else if (j <= a_dim[x])
  9439.             z = 1;
  9440.           if (z == 1 && k > a_dim[x])
  9441.             z = 0;
  9442.           }
  9443.       }
  9444.       break;
  9445.       }
  9446.       case XXIFBG:            /* IF BACKGROUND */
  9447.       case XXIFFG:            /* IF FOREGROUND */
  9448.     bgchk();            /* Check background status */
  9449.     if (ifc == XXIFFG)        /* Foreground */
  9450.       z = pflag ? 1 : 0;
  9451.         else z = pflag ? 0 : 1;        /* Background */
  9452.     ifargs += 1;
  9453.     break;
  9454.  
  9455.       case XXIFCO:            /* IF COUNT */
  9456.     z = ( --count[cmdlvl] > 0 );
  9457.     if (cx == XXWHI) count[cmdlvl] += 2; /* Don't ask... */
  9458.     debug(F101,"if count","",z);
  9459.     ifargs += 1;
  9460.     break;
  9461.  
  9462.       case XXIFEX:            /* IF EXIST */
  9463. #ifdef CK_TMPDIR
  9464.       case XXIFDI:            /* IF DIRECTORY */
  9465. #endif /* CK_TMPDIR */
  9466.       case XXIFAB:            /* IF ABSOLUTE */
  9467.     if ((x = cmfld(
  9468.                ((ifc == XXIFDI) ? "Directory name" : "File"),
  9469.                "",&s,
  9470. #ifdef OS2
  9471.                NULL        /* This allows \'s in filenames */
  9472. #else
  9473.                xxstring
  9474. #endif /* OS2 */
  9475.                )) < 0) {
  9476.         if (x == -3) {
  9477.         extern int cmflgs;
  9478.         if (cmflgs == 1) {
  9479.             printf("?File or directory name required\n");
  9480.             return(-9);
  9481.         }
  9482.         } else return(x);
  9483.     }
  9484.     s = brstrip(s);
  9485.     if (ifc == XXIFAB) {
  9486.         z = isabsolute(s);
  9487.     } else if (ifc == XXIFEX) {
  9488.         z = (zgetfs(s) > -1L);
  9489.         debug(F101,"if exist 1","",z);
  9490. #ifdef OS2
  9491.         if (!z) {            /* File not found. */
  9492.         int t;            /* Try expanding variables */
  9493.         t = LINBUFSIZ-1;    /* and looking again. */
  9494.         lp = line;
  9495.         zzstring(s,&lp,&t);
  9496.         s = line;
  9497.         z = ( zchki(s) > -1L );
  9498.         debug(F101,"if exist 2","",z);
  9499.         }
  9500. #endif /* OS2 */
  9501. #ifdef CK_TMPDIR
  9502.     } else {
  9503. #ifdef VMS
  9504.         z = (zchki(s) == -2)
  9505. #else
  9506. /* Because this doesn't catch $DISK1:[FOO]BLAH.DIR;1 */
  9507.         z = isdir(s)
  9508. #ifdef OS2
  9509.           || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  9510. #endif /* OS2 */
  9511. #endif /* VMS */
  9512.           ;
  9513.         debug(F101,"if directory 1","",z);
  9514.  
  9515.         if (!z) {            /* File not found. */
  9516.         int t;            /* Try expanding variables */
  9517.         t = LINBUFSIZ-1;    /* and looking again. */
  9518.         lp = line;
  9519.         zzstring(s,&lp,&t);
  9520.         s = line;
  9521.         z = isdir(s)
  9522. #ifdef OS2
  9523.           || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  9524. #endif /* OS2 */
  9525.             ;
  9526.         debug(F101,"if directory 2","",z);
  9527.         }
  9528. #endif /* CK_TMPDIR */
  9529.     }
  9530.     ifargs += 2;
  9531.     break;
  9532.  
  9533.       case XXIFEQ:             /* IF EQUAL (string comparison) */
  9534.       case XXIFLL:            /* IF Lexically Less Than */
  9535.       case XXIFLG:            /* If Lexically Greater Than */
  9536.     if ((x = cmfld("first word or variable name","",&s,xxstring)) < 0) {
  9537.         if (x == -3) {
  9538.         printf("?Text required\n");
  9539.         return(-9);
  9540.         } else return(x);
  9541.     }
  9542.     s = brstrip(s);            /* Strip braces */
  9543.     x = (int)strlen(s);
  9544.     if (x > LINBUFSIZ-1) {
  9545.         printf("?IF: strings too long\n");
  9546.         return(-2);
  9547.     }
  9548.     lp = line;            /* lp points to first string */
  9549.         ckstrncpy(line,s,LINBUFSIZ);
  9550.     if ((y = cmfld("second word or variable name","",&s,xxstring)) < 0) {
  9551.         if (y == -3) {
  9552.         printf("?Text required\n");
  9553.         return(-9);
  9554.         } else return(y);
  9555.     }
  9556.     s = brstrip(s);
  9557.     y = (int)strlen(s);
  9558.     if (x + y + 2 > LINBUFSIZ) {
  9559.         printf("?IF: strings too long\n");
  9560.         return(-2);
  9561.     }
  9562.     tp = lp + x + 2;        /* tp points to second string */
  9563.     strcpy(tp,s);            /* safe (checked) */
  9564.     x = ckstrcmp(lp,tp,-1,inpcas[cmdlvl]); /* Use longest length */
  9565.     switch (ifc) {
  9566.       case XXIFEQ:             /* IF EQUAL (string comparison) */
  9567.         z = (x == 0);
  9568.         break;
  9569.       case XXIFLL:            /* IF Lexically Less Than */
  9570.         z = (x < 0);
  9571.         break;
  9572.       case XXIFLG:            /* If Lexically Greater Than */
  9573.         z = (x > 0);
  9574.         break;
  9575.     }
  9576.     debug(F101,"IF EQ result","",z);
  9577.     ifargs += 3;
  9578.     break;
  9579.  
  9580.       case XXIFVE:            /* IF VERSION */
  9581.       case XXIFAE:            /* IF (arithmetically) = */
  9582.       case XXIFNQ:            /* IF != (not arithmetically equal) */
  9583.       case XXIFLT:            /* IF <  */
  9584.       case XXIFLE:            /* IF <= */
  9585.       case XXIFGE:            /* IF >= */
  9586.       case XXIFGT: {            /* IF >  */
  9587.  
  9588.     /* Really should use longs here... */
  9589.     /* But cmnum parses ints. */
  9590.     int xx, n1 = 0, n2 = 0;
  9591.     if (ifc == XXIFVE) {
  9592.         n1 = (int) vernum;
  9593.     } else {
  9594.         x = cmfld("first number or variable name","",&s,xxstring);
  9595.         if (x == -3) {
  9596.         printf("?Quantity required\n");
  9597.         return(-9);
  9598.         }
  9599.         if (x < 0) return(x);
  9600.         debug(F101,"xxifgt cmfld","",x);
  9601.         ckstrncpy(line,s,LINBUFSIZ);
  9602.         lp = brstrip(line);
  9603.         debug(F110,"xxifgt exp1",lp,0);
  9604.  
  9605. /* The following bit is for compatibility with old versions of MS-DOS Kermit */
  9606.  
  9607.         if (!ckstrcmp(lp,"count",5,0)) {
  9608.         n1 = count[cmdlvl];
  9609.         } else if (!ckstrcmp(lp,"version",7,0)) {
  9610.         n1 = (int) vernum;
  9611.         } else if (!ckstrcmp(lp,"argc",4,0)) {
  9612.         n1 = (int) macargc[maclvl];
  9613.         } else {
  9614.  
  9615. /* End of compatibility bit */
  9616.  
  9617. #ifdef FNFLOAT
  9618.         if (isfloat(lp,0) > 1) { /* Allow floating-point comparisons */
  9619.             f1 = floatval;
  9620.             f1flag = 1;
  9621.         } else
  9622. #endif /* FNFLOAT */
  9623.           if (chknum(lp)) {
  9624.               n1 = atoi(lp);
  9625.           } else {        /* Check for arithmetic expression */
  9626.               q = evala(lp);    /* cmnum() does this but ... */
  9627.               if (chknum(q))    /* we're not using cmnum(). */
  9628.             n1 = atoi(q);
  9629.               else
  9630.             return(-2);
  9631.           }
  9632.         }
  9633.     }
  9634.     y = cmfld("number or variable name","",&s,xxstring);
  9635.     if (y == -3) {
  9636.         printf("?Quantity required\n");
  9637.         return(-9);
  9638.     }
  9639.     if (y < 0) return(y);
  9640.     s = brstrip(s);
  9641.         if (!*s) return(-2);
  9642.     if (ifc == XXIFVE) {
  9643.         tp = line;
  9644.     } else {
  9645.         x = (int)strlen(lp);
  9646.         tp = line + x + 2;
  9647.     }
  9648.     ckstrncpy(tp,s,LINBUFSIZ-x-2);
  9649.     debug(F110,"xxifgt exp2",tp,0);
  9650.     if (!ckstrcmp(tp,"count",5,0)) {
  9651.         n2 = count[cmdlvl];
  9652.     } else if (!ckstrcmp(tp,"version",7,0)) {
  9653.         n2 = (int) vernum;
  9654.     } else if (!ckstrcmp(tp,"argc",4,0)) {
  9655.         n2 = (int) macargc[maclvl];
  9656.     } else {
  9657. #ifdef FNFLOAT
  9658.         if (isfloat(tp,0) > 1) {
  9659.         f2 = floatval;
  9660.         f2flag = 1;
  9661.         } else
  9662. #endif /* FNFLOAT */
  9663.         if (chknum(tp)) {
  9664.         n2 = atoi(tp);
  9665.         } else {
  9666.         q = evala(tp);
  9667.         if (chknum(q))
  9668.           n2 = atoi(q);
  9669.         else
  9670.           return(-2);
  9671.         }
  9672.     }
  9673.     xx = (ifc == XXIFVE) ? XXIFGE : ifc;
  9674.  
  9675. #ifdef FNFLOAT
  9676.     if (f1flag && !f2flag) {
  9677.         f2 = (CKFLOAT)n2;
  9678.         f2flag = 1;
  9679.     }
  9680.     if (f2flag && !f1flag)
  9681.       f1 = (CKFLOAT)n1;
  9682.     if (f1flag)
  9683.       z = ((f1 <  f2 && xx == XXIFLT)
  9684.            || (f1 != f2 && xx == XXIFNQ)
  9685.            || (f1 <= f2 && xx == XXIFLE)
  9686.            || (f1 == f2 && xx == XXIFAE)
  9687.            || (f1 >= f2 && xx == XXIFGE)
  9688.            || (f1 >  f2 && xx == XXIFGT));
  9689.     else
  9690. #endif /* FNFLOAT */
  9691.       z = ((n1 <  n2 && xx == XXIFLT)
  9692.            || (n1 != n2 && xx == XXIFNQ)
  9693.            || (n1 <= n2 && xx == XXIFLE)
  9694.            || (n1 == n2 && xx == XXIFAE)
  9695.            || (n1 >= n2 && xx == XXIFGE)
  9696.            || (n1 >  n2 && xx == XXIFGT));
  9697.     debug(F101,"xxifge z","",z);
  9698.     if (ifc == XXIFVE)
  9699.       ifargs += 2;
  9700.     else
  9701.       ifargs += 3;
  9702.     break;
  9703.       }
  9704.  
  9705.       case XXIFNU:            /* IF NUMERIC */
  9706.     x = cmfld("variable name or constant","",&s,NULL);
  9707.     if (x == -3) {
  9708.         extern int cmflgs;
  9709.         if (cmflgs == 1) {
  9710.         printf("?Quantity required\n");
  9711.         return(-9);
  9712.         }
  9713.     } else if (x < 0)
  9714.       return(x);
  9715.     x = LINBUFSIZ-1;
  9716.     lp = line;
  9717.     zzstring(s,&lp,&x);
  9718.     lp = line;
  9719.     debug(F110,"xxifnu quantity",lp,0);
  9720.         z = chknum(lp);
  9721. #ifdef COMMENT
  9722. /*
  9723.   This works, but it's not wise -- IF NUMERIC is mostly used to see if a
  9724.   string really does contain only numeric characters.  If they want to force
  9725.   evaluation, they can use \feval() on the argument string.
  9726. */
  9727.     if (!z) {            /* Not a number */
  9728.         x_ifnum = 1;        /* Avoid "eval" error messages */
  9729.         q = evala(lp);        /* Maybe it's an expression */
  9730.         z = chknum(q);        /* that evaluates to a number */
  9731.         x_ifnum = 0;        /* Put eval messages back to normal */
  9732.         if (z) debug(F110,"xxifnu exp",lp,0);
  9733.     }
  9734. #endif /* COMMENT */
  9735.         debug(F101,"xxifnu chknum","",z);
  9736.     ifargs += 2;
  9737.     break;
  9738.  
  9739. #ifdef ZFCDAT
  9740.       case XXIFNE: {            /* IF NEWER */
  9741.     char d1[20], * d2;        /* Buffers for 2 dates */
  9742.     if ((z = cmifi("First file","",&s,&y,xxstring)) < 0)
  9743.       return(z);
  9744.     ckstrncpy(d1,zfcdat(s),20);
  9745.     if ((z = cmifi("Second file","",&s,&y,xxstring)) < 0)
  9746.       return(z);
  9747.     d2 = zfcdat(s);
  9748.     if ((int)strlen(d1) != 17 || (int)strlen(d2) != 17) {
  9749.         printf("?Failure to get file date\n");
  9750.         return(-9);
  9751.     }
  9752.     debug(F110,"xxifnewer d1",d1,0);
  9753.     debug(F110,"xxifnewer d2",d2,0);
  9754.     z = (strcmp(d1,d2) > 0) ? 1 : 0;
  9755.         debug(F101,"xxifnewer","",z);
  9756.     ifargs += 2;
  9757.     break;
  9758.       }
  9759. #endif /* ZFCDAT */
  9760.  
  9761. #ifdef CK_IFRO
  9762.       case XXIFRO:            /* REMOTE-ONLY advisory */
  9763.     ifargs++;
  9764. #ifdef NOLOCAL
  9765.     z = 1;
  9766. #else
  9767.     z = remonly;
  9768. #endif /* NOLOCAL */
  9769.     break;
  9770. #endif /* CK_IFRO */
  9771.  
  9772.       case XXIFAL:            /* ALARM */
  9773.     ifargs++;
  9774.     debug(F101,"IF ALARM ck_alarm","",ck_alarm);
  9775.     debug(F110,"IF ALARM alrm_date",alrm_date,0);
  9776.     debug(F110,"IF ALARM alrm_time",alrm_time,0);
  9777.  
  9778.     if (ck_alarm < 1L || alrm_date[0] < '0' || alrm_time[0] < '0') {
  9779.         z = 0;            /* ALARM not SET */
  9780.         break;            /* so IF ALARM fails */
  9781.     }
  9782.     /* Get current date and time */
  9783.     ckstrncpy(tmpbuf,ckcvtdate("",1),TMPBUFSIZ);
  9784.     s = tmpbuf;
  9785.     s[8] = NUL;
  9786.     z = (int) strncmp(tmpbuf,alrm_date,8); /* Compare dates */
  9787.     debug(F101,"IF ALARM date z","",z);
  9788.     if (z == 0) {            /* Dates are the same */
  9789.         /* Compare times */
  9790.         z = (tod2sec(tmpbuf+9) >= atol(alrm_time)) ? 1 : -1;
  9791.         debug(F101,"IF ALARM time z","",z);
  9792.     }
  9793.     tmpbuf[0] = NUL;        /* z >= 0 if alarm is passed */
  9794.     z = ((z >= 0) ? 1 : 0);        /* z <  0 otherwise */
  9795.     debug(F101,"IF ALARM final z","",z);
  9796.     break;
  9797.  
  9798.       case XXIFOP:            /* IF OPEN */
  9799.     if ((x = cmkey(iotab,niot,"file or log","",xxstring)) < 0)
  9800.       return(x);
  9801.     if (x == 9999 || x == ZSTDIO) {
  9802.         bgchk();            /* Check background status */
  9803.         z = pflag ? 1 : 0;
  9804.     } else if (x == 8888) {
  9805.         z = local ? ttchk() > -1 : 0;
  9806. #ifdef CKLOGDIAL
  9807.         } else if (x == 7777) {
  9808.         extern int dialog;
  9809.         z = dialog ? 1 : 0;
  9810. #endif /* CKLOGDIAL */
  9811.     } else
  9812.       z = (chkfn(x) > 0) ? 1 : 0;
  9813.     ifargs += 1;
  9814.     break;
  9815.  
  9816. #ifdef OS2
  9817.       case XXIFSD:            /* Started-From-Dialer */
  9818.     ifargs++;
  9819.     z = StartedFromDialer;
  9820.     break;
  9821.  
  9822.       case XXIFTM:            /* Terminal-Macro */
  9823.     ifargs++;
  9824.     z = cmdstk[cmdlvl].ccflgs & CF_KMAC;
  9825.     break;
  9826. #endif /* OS2 */
  9827.  
  9828.       case XXIFEM:            /* Emulation is active */
  9829. #ifdef OS2
  9830.     z = 1;
  9831. #else
  9832.     z = 0;
  9833. #endif /* OS2 */
  9834.     break;
  9835.  
  9836.       case XXIFIK:                      /* Running as IKSD? */
  9837.         z = inserver;
  9838.         break;
  9839.  
  9840.       case XXIFTA:            /* Connection is TAPI */
  9841.     z = 0;
  9842. #ifndef NODIAL
  9843. #ifdef CK_TAPI
  9844.     if (local && !network && tttapi)
  9845.       z = 1;
  9846. #endif /* CK_TAPI */
  9847. #endif /* NODIAL */
  9848.     break;
  9849.  
  9850.       case XXIFMA:            /* IF MATCH */
  9851.     x = cmfld("String or variable","",&s,xxstring);
  9852.     if (x == -3) {
  9853.         extern int cmflgs;
  9854.         if (cmflgs == 1) {
  9855.         printf("?String required\n");
  9856.         return(-9);
  9857.         }
  9858.     } else if (x < 0)
  9859.       return(x);
  9860.     ckstrncpy(line,s,LINBUFSIZ);
  9861.     s = brstrip(line);
  9862.     debug(F110,"xxifma string",line,0);
  9863.     x = cmfld("Pattern","",&p,xxstring);
  9864.     if (x == -3) {
  9865.         extern int cmflgs;
  9866.         if (cmflgs == 1) {
  9867.         printf("?Pattern required\n");
  9868.         return(-9);
  9869.         }
  9870.     } else if (x < 0)
  9871.       return(x);
  9872.     ckstrncpy(tmpbuf,p,TMPBUFSIZ);
  9873.     p = brstrip(tmpbuf);
  9874.     debug(F110,"xxifma pattern",tmpbuf,0);
  9875.     z = ckmatch(p,s,inpcas[cmdlvl],1);
  9876.     break;
  9877.  
  9878.       case XXIFFL: {            /* IF FLAG */
  9879.       extern int ooflag;
  9880.       z = ooflag;
  9881.       break;
  9882.       }
  9883.       case XXIFAV: {            /* IF AVAILABLE */
  9884.       if ((x = cmkey(availtab,availtabn,"","",xxstring)) < 0)
  9885.         return(x);
  9886.       switch (x) {
  9887.         case AV_KRB4:
  9888.               z = ck_krb4_is_installed();
  9889.               break;
  9890.         case AV_KRB5:
  9891.               z = ck_krb5_is_installed();
  9892.               break;
  9893.             case AV_SRP:
  9894.               z = ck_srp_is_installed();
  9895.               break;
  9896.             case AV_SSL:
  9897.               z = ck_ssleay_is_installed();
  9898.           break;
  9899.             case AV_NTLM:
  9900.               z = ck_ntlm_is_installed();
  9901.           break;
  9902.             case AV_CRYPTO:
  9903.           z = ck_crypt_is_installed();
  9904.           break;
  9905.         default:
  9906.               z = 0;
  9907.       }
  9908.       break;
  9909.       }
  9910.       case XXIFAT:            /* IF ASKTIMEOUT */
  9911.     z = asktimedout;
  9912.     break;
  9913.  
  9914.       case XXIFRD:            /* IF READABLE */
  9915.       case XXIFWR:            /* IF WRITEABLE */
  9916.     if ((x = cmfld("File or directory name",
  9917.                "",
  9918.                &s,
  9919. #ifdef OS2
  9920.                NULL        /* This allows \'s in filenames */
  9921. #else
  9922.                xxstring
  9923. #endif /* OS2 */
  9924.                )) < 0) {
  9925.         if (x == -3) {
  9926.         extern int cmflgs;
  9927.         if (cmflgs == 1) {
  9928.             printf("?File or directory name required\n");
  9929.             return(-9);
  9930.         }
  9931.         } else return(x);
  9932.     }
  9933.     s = brstrip(s);
  9934. /*
  9935.   zchk[io]() do not do what we want here for directories, so we set
  9936.   a global flag telling it to behave specially in this case.  Othewise
  9937.   we'd have to change the API and change all ck?fio.c modules accordingly.
  9938. */
  9939.     y = 0;                /* Try-again control */
  9940. #ifdef OS2
  9941.   ifrdagain:
  9942. #endif /* OS2 */
  9943.     if (ifc == XXIFRD) {        /* IF READABLE */
  9944.         zchkid = 1;
  9945.         z = zchki(s) > -1;
  9946.         zchkid = 0;
  9947.     } else if (ifc == XXIFWR) {    /* IF WRITEABLE */
  9948.         zchkod = 1;
  9949.         z = zchko(s) > -1;
  9950.         zchkod = 0;
  9951.     }
  9952. #ifdef OS2
  9953.     if (!z && !y) {            /* File not found. */
  9954.         int t;            /* Try expanding variables */
  9955.         t = LINBUFSIZ-1;        /* and looking again. */
  9956.         lp = line;
  9957.         zzstring(s,&lp,&t);
  9958.         s = line;
  9959.         z = zchko(s) > -1;
  9960.         y++;
  9961.         goto ifrdagain;
  9962.     }
  9963. #endif /* OS2 */
  9964.     ifargs += 2;
  9965.     break;
  9966.       case XXIFQU:            /* IF QUIET */
  9967.     z = quiet ? 1 : 0;
  9968.     debug(F101,"if quiet","",z);
  9969.     ifargs += 1;
  9970.     break;
  9971.  
  9972.       case XXIFWI:            /* WILD */
  9973.     if ((x = cmfld("File specification","",&s,xxstring)) < 0) return(x);
  9974.     z = iswild(s);
  9975.     break;
  9976.  
  9977.       case XXIFCK:            /* C-KERMIT */
  9978. #ifdef OS2
  9979.         z = 0;
  9980. #else
  9981.         z = 1;
  9982. #endif /* OS2 */
  9983.         break;
  9984.  
  9985.       case XXIFK9:            /* K-95 */
  9986. #ifdef OS2
  9987.         z = 1;
  9988. #else
  9989.         z = 0;
  9990. #endif /* OS2 */
  9991.         break;
  9992.  
  9993.       case XXIFGU:            /* GUI */
  9994. #ifdef KUI
  9995.         z = 1;
  9996. #else
  9997.         z = 0;
  9998. #endif /* KUI */
  9999.         break;
  10000.  
  10001.       case XXIFMS:            /* MS-KERMIT */
  10002.         z = 0;
  10003.         break;
  10004.  
  10005.       case XXIFLO:            /* IF LOCAL */
  10006.     z = local ? 1 : 0;
  10007.         break;
  10008.  
  10009.       case XXIFCM: {            /* IF COMMAND */
  10010.       extern struct keytab cmdtab[];
  10011.       extern int ncmd;
  10012.       if ((x = cmfld("Word","",&s,xxstring)) < 0)
  10013.         return(x);
  10014.       z = lookup(cmdtab,s,ncmd,&y);
  10015.       z = (z == -2 || z > -1) ? 1 : 0;
  10016.       break;
  10017.       }
  10018. #ifdef CKFLOAT
  10019.       case XXIFFP:            /* IF FLOAT */
  10020.     if ((x = cmfld("Number","",&s,xxstring)) < 0)
  10021.       if (x != -3)            /* e.g. empty variable */
  10022.         return(x);
  10023.     z = isfloat(s,0);
  10024.     break;
  10025. #endif /* CKFLOAT */
  10026.  
  10027.       case XXIFKB:            /* KBHIT */
  10028.     z = conchk();
  10029.     if (z < 0) z = 0;
  10030.     if (z > 1) z = 1;
  10031.     break;
  10032.  
  10033.       case XXIFKG: {            /* KERBANG */
  10034.       extern int cfilef;
  10035.       z = (xcmdsrc == 0) ? 0 : cfilef;
  10036.       break;
  10037.       }
  10038.  
  10039.       default:                /* Shouldn't happen */
  10040.     return(-2);
  10041.     } /* end of switch */
  10042.  
  10043.     if (z)
  10044.       *bx++ = '1';
  10045.     else
  10046.       *bx++ = '0';
  10047.     *bx = NUL;
  10048.     if (bx > boolval + BOOLLEN - 2) {
  10049.     printf("?Boolean expression too long");
  10050.     return(-9);
  10051.     }
  10052.     ecount++;                /* Expression counter */
  10053.     debug(F101,"boolexp parens","",parens);
  10054.     debug(F101,"boolexp pcount","",pcount);
  10055.     if (parens && pcount > 0)
  10056.       goto ifagain;
  10057.  
  10058.   ifend:                /* No more - done */
  10059.     *bx = NUL;
  10060.     z = atoi(evalx(boolval));
  10061.     debug(F111,"boolexp boolval",boolval,z);
  10062.     return(z);
  10063. }
  10064.  
  10065. /*  D O I F  --  Do the IF command  */
  10066.  
  10067. int
  10068. doif(cx) int cx; {
  10069.     int x, y, z; char *s, *p;
  10070.     char *q;
  10071. #ifdef OS2
  10072.     extern int keymac;
  10073. #endif /* OS2 */
  10074.  
  10075.     debug(F101,"doif cx","",cx);
  10076.  
  10077.     z = boolexp(cx);            /* Evaluate the condition(s) */
  10078.     debug(F010,"doif cmdbuf",cmdbuf,0);
  10079.     debug(F101,"doif boolexp","",z);
  10080.     if (z < 0)
  10081.       return(z);
  10082.  
  10083.     if (cx == XXIF) {            /* Allow IF to have XIF semantics. */
  10084.     char * p;
  10085.     p = cmpeek();
  10086.     if (!p) p = "";
  10087.     while (*p) {
  10088.         if (*p == SP || *p == HT)
  10089.           p++;
  10090.         else
  10091.           break;
  10092.     }
  10093.     if (*p == '{')
  10094.       cx = XXIFX;
  10095.     }
  10096.     switch (cx) {            /* Separate handling for IF and XIF */
  10097.  
  10098.       case XXASSER:            /* And ASSERT */
  10099.     if ((x = cmcfm()) < 0)
  10100.       return(x);
  10101.     return(success = z);
  10102.  
  10103.       case XXIF:            /* This is IF... */
  10104.     ifcmd[cmdlvl] = 1;        /* We just completed an IF command */
  10105.         debug(F101,"doif condition","",z);
  10106.     if (z) {            /* Condition is true */
  10107.         iftest[cmdlvl] = 1;        /* Remember that IF succeeded */
  10108.         if (maclvl > -1) {        /* In macro, */
  10109.         pushcmd(NULL);        /* save rest of command. */
  10110.         } else if (tlevel > -1) {    /* In take file, */
  10111.         debug(F100, "doif: pushing command", "", 0);
  10112.         pushcmd(NULL);        /* save rest of command. */
  10113.         } else {            /* If interactive, */
  10114.         cmini(ckxech);        /* just start a new command */
  10115.         printf("\n");        /* (like in MS-DOS Kermit) */
  10116.         if (pflag) prompt(xxstring);
  10117.         }
  10118.     } else {            /* Condition is false */
  10119.         iftest[cmdlvl] = 0;        /* Remember command failed. */
  10120.         if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  10121.           return(y);        /* Gobble up rest of line */
  10122.     }
  10123.     return(0);
  10124.  
  10125.       case XXIFX: {            /* This is XIF (Extended IF) */
  10126.       char *p;
  10127.       char e[5];
  10128.       int i;
  10129.       if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  10130.         return(y);            /* Get object command. */
  10131.       p = s;
  10132.       lp = line;
  10133.       debug(F110,"doif THEN part",s,-54);
  10134.       if (litcmd(&p,&lp,LINBUFSIZ - 1) < 0) { /* Quote THEN-part */
  10135.           return(-2);
  10136.       }
  10137.       debug(F111,"doif THEN part 2",line,z);
  10138.  
  10139.       while (*p == SP) p++;        /* Strip trailing spaces */
  10140.       ifcmd[cmdlvl] = 0;        /* Assume ELSE part in same line */
  10141.       iftest[cmdlvl] = z ? 1 : 0;
  10142.       if (*p) {            /* At end? */
  10143.           if (!z) {            /* No, use ELSE-part, if any */
  10144.           for (i = 0; i < 4; i++) e[i] = *p++;
  10145.           if (ckstrcmp(e,"else",4,0)) /* See if we have an ELSE */
  10146.             return(-2);        /* Something else - error. */
  10147.           debug(F010,"doif ELSE line 1",p,0);
  10148.           while (*p == SP) p++;    /* Skip spaces */
  10149.           if (*p != '{') {    /* Brace ELSE part if necessary */
  10150.               ckmakmsg(tmpbuf,TMPBUFSIZ,"{",p," }",NULL);
  10151.                       p = tmpbuf;
  10152.               debug(F010,"doif ELSE line 2",p,0);
  10153.           }
  10154.           lp = line;        /* Write over THEN part... */
  10155.           *lp = NUL;        /* with ELSE part. */
  10156.           if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) {
  10157.               return(-2);
  10158.           }
  10159.           while (*p == SP) p++;    /* Strip trailing spaces */
  10160.           if (*p) return(-2);    /* Should be nothing here. */
  10161.           debug(F010,"doif ELSE line 3",line,0);
  10162.           }
  10163.       } else {            /* At end, treat like an IF command */
  10164.           if (!z) line[0] = NUL;    /* Condition not true and no ELSE */
  10165.           ifcmd[cmdlvl] = 1;    /* Allow ELSE on next line */
  10166.           debug(F101,"IF condition","",z);
  10167.       }
  10168.       if (line[0]) {
  10169.           x = mlook(mactab,"_xif",nmac); /* Get index of "_xif" macro. */
  10170.           if (x < 0) {            /* Not there? */
  10171.           addmmac("_xif",xif_def);    /* Put it back. */
  10172.           if (mlook(mactab,"_xif",nmac) < 0) { /* Look it up again. */
  10173.               printf("?XIF macro gone!\n");
  10174.               return(success = 0);
  10175.           }
  10176.           }
  10177.           dodo(x,line,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  10178.       }
  10179.       return(0);
  10180.       }
  10181.       case XXWHI: {            /* WHILE Command */
  10182.       p = cmdbuf;            /* Capture IF condition */
  10183.       ifcond[0] = NUL;        /* from command buffer */
  10184.       while (*p == SP) p++;
  10185.       while (*p != SP) p++;
  10186.       ifcp = ifcond;
  10187.       ifcp += ckstrncpy(ifcp,"{ \\flit(if ( not ",IFCONDLEN);
  10188. #ifdef COMMENT
  10189. /*
  10190.   This doesn't work because it breaks on the first left brace, which does
  10191.   not necessarily start the command list, e.g. "while equal \%a {\35}".
  10192. */
  10193.       while (*p != '{' && *p != NUL) *ifcp++ = *p++;
  10194.       p = " ) goto _..bot) } ";
  10195.       while (*ifcp++ = *p++) ;
  10196. #else
  10197. /*
  10198.   The command parser sets cmbptr to the spot where it left off parsing in
  10199.   the command buffer.
  10200. */
  10201.       {
  10202.           extern char * cmbptr;
  10203.           if (cmbptr) {
  10204.           while (p < cmbptr && *p != NUL)
  10205.             *ifcp++ = *p++;
  10206.           p = " ) goto _..bot) } ";
  10207.           while ((*ifcp++ = *p++)) ;
  10208.           } else {
  10209.           printf("?Internal error parsing WHILE condition\n");
  10210.           return(-9);
  10211.           }
  10212.       }
  10213. #endif /* COMMENT */
  10214.  
  10215.       debug(F110,"WHILE cmd",ifcond,0);
  10216.  
  10217.       if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  10218.         return(y);            /* Get object command. */
  10219.       p = s;
  10220.       lp = line;
  10221.       if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) { /* Quote object command */
  10222.           return(-2);
  10223.       }
  10224.       debug(F101,"WHILE body",line,-54);
  10225.       if (line[0]) {
  10226.           char *p;
  10227.           x = mlook(mactab,"_while",nmac); /* index of "_while" macro. */
  10228.           if (x < 0) {        /* Not there? */
  10229.           addmmac("_while",whil_def); /* Put it back. */
  10230.           if (mlook(mactab,"_while",nmac) < 0) { /* Look it up again */
  10231.               printf("?WHILE macro definition gone!\n");
  10232.               return(success = 0);
  10233.           }
  10234.           }
  10235.           p = malloc((int)strlen(ifcond) + (int)strlen(line) + 2);
  10236.           if (p) {
  10237.           strcpy(p,ifcond);    /* safe (prechecked) */
  10238.           strcat(p,line);    /* safe (prechecked) */
  10239.           debug(F010,"WHILE dodo",p,0);
  10240.           dodo(x,p,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  10241.           free(p);
  10242.           p = NULL;
  10243.           } else {
  10244.           printf("?Can't allocate storage for WHILE command");
  10245.           return(success = 0);
  10246.           }
  10247.       }
  10248.       return(0);
  10249.       }
  10250.       default:
  10251.     return(-2);
  10252.     }
  10253. }
  10254. #endif /* NOSPL */
  10255.  
  10256. /* Set up a TAKE command file */
  10257.  
  10258. int
  10259. dotake(s) char *s; {
  10260. #ifndef NOSPL
  10261.     extern int tra_cmd;
  10262. #endif /* NOSPL */
  10263. #ifndef NOLOCAL
  10264. #ifdef OS2
  10265.     extern int term_io;
  10266.     int term_io_sav = term_io;
  10267. #endif /* OS2 */
  10268. #endif /* NOLOCAL */
  10269.     int slen;
  10270.  
  10271.     debug(F110,"dotake",s,0);
  10272.     if (!s) s = "";
  10273.     if (!*s) return(success = 0);
  10274.     slen = strlen(s);
  10275.     debug(F101,"dotake len","",slen);
  10276.  
  10277.     if ((tfile[++tlevel] = fopen(s,"r")) == NULL) {
  10278.     perror(s);
  10279.     debug(F110,"dotake fail",s,0);
  10280.     tlevel--;
  10281.     return(success = 0);
  10282.     } else {
  10283.     tfline[tlevel] = 0;        /* Line counter */
  10284. #ifdef VMS
  10285.     conres();            /* So Ctrl-C will work */
  10286. #endif /* VMS */
  10287. #ifndef NOLOCAL
  10288. #ifdef OS2
  10289.     term_io = 0;            /* Disable Terminal Emulator I/O */
  10290. #endif /* OS2 */
  10291. #endif /* NOLOCAL */
  10292. #ifndef NOSPL
  10293.     cmdlvl++;            /* Entering a new command level */
  10294.     debug(F111,"CMD +F",s,cmdlvl);
  10295.     debug(F101,"dotake cmdlvl","",cmdlvl);
  10296.     debug(F101,"dotake tlevel","",tlevel);
  10297.     if (cmdlvl > CMDSTKL) {
  10298.         debug(F100,"dotake stack overflow","",0);
  10299.         cmdlvl--;
  10300.         debug(F111,"CMD*-F",s,cmdlvl);
  10301.         fclose(tfile[tlevel--]);
  10302.         printf("?TAKE files and/or DO commands nested too deeply\n");
  10303.         return(success = 0);
  10304.     }
  10305.     if (tfnam[tlevel]) {        /* Copy the filename */
  10306.         free(tfnam[tlevel]);
  10307.         tfnam[tlevel] = NULL;
  10308.     }
  10309.     if ((tfnam[tlevel] = malloc(strlen(s) + 1))) {
  10310.         strcpy(tfnam[tlevel],s);    /* safe */
  10311.     } else {
  10312.         printf("?Memory allocation failure\n");
  10313.         return(success = 0);
  10314.     }
  10315.     ifcmd[cmdlvl] = 0;        /* Set variables for this cmd file */
  10316.     iftest[cmdlvl] = 0;
  10317.     count[cmdlvl]  = count[cmdlvl-1];  /* Inherit this */
  10318.     intime[cmdlvl] = intime[cmdlvl-1]; /* Inherit this */
  10319.     inpcas[cmdlvl] = inpcas[cmdlvl-1]; /* Inherit this */
  10320.     takerr[cmdlvl] = takerr[cmdlvl-1]; /* Inherit this */
  10321.     merror[cmdlvl] = merror[cmdlvl-1]; /* Inherit this */
  10322.     xquiet[cmdlvl] = quiet;
  10323.         xcmdsrc = CMD_TF;
  10324.     cmdstk[cmdlvl].src = CMD_TF;    /* Say we're in a TAKE file */
  10325.     cmdstk[cmdlvl].lvl = tlevel;    /* nested at this level */
  10326.     cmdstk[cmdlvl].ccflgs = cmdstk[cmdlvl-1].ccflgs;
  10327. #else
  10328.     takerr[tlevel] = takerr[tlevel-1]; /* Inherit this */
  10329. #endif /* NOSPL */
  10330.     }
  10331. #ifndef NOSPL
  10332.     if (tra_cmd)
  10333.       printf("[%d] +F: \"%s\"\n",cmdlvl,s);
  10334. #endif /* NOSPL */
  10335. #ifndef NOLOCAL
  10336. #ifdef OS2
  10337.     term_io = term_io_sav;
  10338. #endif /* OS2 */
  10339. #endif /* NOLOCAL */
  10340.     return(1);
  10341. }
  10342. #endif /* NOICP */
  10343.