home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku208.zip / ckuus6.c < prev    next >
C/C++ Source or Header  |  2003-03-03  |  375KB  |  10,792 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, 2003,
  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_SECURITY
  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_ssh_is_installed,(void));
  554. _PROTOTYP(int ck_crypt_is_installed,(void));
  555. #else
  556. #define ck_krb4_is_installed() (0)
  557. #define ck_krb5_is_installed() (0)
  558. #define ck_ntlm_is_installed() (0)
  559. #define ck_srp_is_installed() (0)
  560. #define ck_ssleay_is_installed() (0)
  561. #define ck_ssh_is_installed() (0)
  562. #define ck_crypt_is_installed() (0)
  563. #endif /* CK_SECURITY */
  564.  
  565. #define AV_KRB4   1
  566. #define AV_KRB5   2
  567. #define AV_NTLM   3
  568. #define AV_SRP    4
  569. #define AV_SSL    5
  570. #define AV_CRYPTO 6
  571. #define AV_SSH    7
  572.  
  573. struct keytab availtab[] = {             /* Available authentication types */
  574.     { "crypto",     AV_CRYPTO, CM_INV }, /* and encryption */
  575.     { "encryption", AV_CRYPTO, 0 },
  576.     { "k4",         AV_KRB4,   CM_INV },
  577.     { "k5",         AV_KRB5,   CM_INV },
  578.     { "kerberos4",  AV_KRB4,   0 },
  579.     { "kerberos5",  AV_KRB5,   0 },
  580.     { "krb4",       AV_KRB4,   CM_INV },
  581.     { "krb5",       AV_KRB5,   CM_INV },
  582.     { "ntlm",       AV_NTLM,   0 },
  583.     { "srp",        AV_SRP,    0 },
  584.     { "ssh",        AV_SSH,    0 },
  585.     { "ssl",        AV_SSL,    0 },
  586.     { "tls",        AV_SSL,    0 },
  587.     { "",           0,         0 }
  588. };
  589. int availtabn = sizeof(availtab)/sizeof(struct keytab)-1;
  590.  
  591. #ifndef NODIAL
  592. _PROTOTYP(static int ddcvt, (char *, FILE *, int) );
  593. _PROTOTYP(static int dncvt, (int, int, int, int) );
  594. _PROTOTYP(char * getdname, (void) );
  595.  
  596. static int partial  = 0;                /* For partial dial */
  597. static char *dscopy = NULL;
  598. int dialtype = -1;
  599.  
  600. char *dialnum = (char *)0;              /* Remember DIAL number for REDIAL */
  601. int dirline = 0;                        /* Dial directory line number */
  602. extern char * dialdir[];                /* Dial directory file names */
  603. extern int dialdpy;                     /* DIAL DISPLAY on/off */
  604. extern int ndialdir;                    /* How many dial directories */
  605. extern int ntollfree;                   /* Toll-free call info */
  606. extern int ndialpxx;                    /* List of PBX exchanges */
  607. extern char *dialtfc[];
  608. char * matchpxx = NULL;                 /* PBX exchange that matched */
  609. extern int nlocalac;                    /* Local area-code list */
  610. extern char * diallcac[];
  611. extern int tttapi;
  612. #ifdef CK_TAPI
  613. extern int tapiconv;                    /* TAPI Conversions */
  614. extern int tapipass;                    /* TAPI Passthrough */
  615. #endif /* CK_TAPI */
  616. extern int dialatmo;
  617. extern char * dialnpr, * dialsfx;
  618. extern char * dialixp, * dialixs, * dialmac;
  619. extern char * dialldp, * diallds, * dialtfp;
  620. extern char * dialpxi, * dialpxo, * diallac;
  621. extern char * diallcp, * diallcs, * diallcc;
  622. extern char * dialpxx[];
  623.  
  624. extern int dialcnf;                     /* DIAL CONFIRMATION */
  625. int dialfld = 0;                        /* DIAL FORCE-LONG-DISTANCE */
  626. int dialsrt = 1;                        /* DIAL SORT ON */
  627. int dialrstr = 6;                       /* DIAL RESTRICTION */
  628. int dialtest = 0;                       /* DIAL TEST */
  629. int dialcount = 0;                      /* \v(dialcount) */
  630.  
  631. extern int dialsta;                     /* Dial status */
  632. int dialrtr = -1,                       /* Dial retries */
  633.     dialint = 10;                       /* Dial retry interval */
  634. extern long dialcapas;                  /* Modem capabilities */
  635. extern int dialcvt;                     /* DIAL CONVERT-DIRECTORY */
  636. #endif /* NODIAL */
  637.  
  638. #ifndef NOSPL
  639. #define IFCONDLEN 256
  640. int ifc,                                /* IF case */
  641.     not = 0,                            /* Flag for IF NOT */
  642.     ifargs = 0;                         /* Count of IF condition words */
  643. char ifcond[IFCONDLEN];                 /* IF condition text */
  644. char *ifcp;                             /* Pointer to IF condition text */
  645. #ifdef DCMDBUF
  646. extern int
  647.  *ifcmd,  *count,  *iftest, *intime,
  648.  *inpcas, *takerr, *merror, *xquiet;
  649. #else
  650. extern int ifcmd[];                     /* Last command was IF */
  651. extern int iftest[];                    /* Last IF was true */
  652. extern int count[];                     /* For IF COUNT, one for each cmdlvl */
  653. extern int intime[];                    /* Ditto for other stackables... */
  654. extern int inpcas[];
  655. extern int takerr[];
  656. extern int merror[];
  657. extern int xquiet[];
  658. #endif /* DCMDBUF */
  659. #else
  660. extern int takerr[];
  661. #endif /* NOSPL */
  662.  
  663. #ifdef DCMDBUF
  664. extern char *line;                      /* Character buffer for anything */
  665. extern char *tmpbuf;
  666. #else
  667. extern char line[], tmpbuf[];
  668. #endif /* DCMDBUF */
  669. extern char *lp;                        /* Pointer to line buffer */
  670.  
  671. int cwdf = 0;                           /* CWD has been done */
  672.  
  673. /* Flags for ENABLE/DISABLE */
  674. extern int en_cwd, en_cpy, en_del, en_dir, en_fin,
  675.    en_get, en_hos, en_ren, en_sen, en_set, en_spa, en_typ, en_who, en_bye,
  676.    en_asg, en_que, en_ret, en_mai, en_pri, en_mkd, en_rmd, en_xit, en_ena;
  677.  
  678. extern FILE *tfile[];                   /* File pointers for TAKE command */
  679. extern char *tfnam[];                   /* Names of TAKE files */
  680. extern int tfline[];                    /* TAKE-file line number */
  681.  
  682. extern int success;                     /* Command success/failure flag */
  683. extern int cmdlvl;                      /* Current position in command stack */
  684.  
  685. #ifndef NOSPL
  686. extern int maclvl;                      /* Macro to execute */
  687. extern char *macx[];                    /* Index of current macro */
  688. extern char *mrval[];                   /* Macro return value */
  689. extern char *macp[];                    /* Pointer to macro */
  690. extern int macargc[];                   /* ARGC from macro invocation */
  691.  
  692. #ifdef COMMENT
  693. extern char *m_line[];
  694. #endif /* COMMENT */
  695.  
  696. extern char *m_arg[MACLEVEL][NARGS];    /* Stack of macro arguments */
  697. extern char *g_var[];                   /* Global variables %a, %b, etc */
  698.  
  699. #ifdef DCMDBUF
  700. extern struct cmdptr *cmdstk;           /* The command stack itself */
  701. #else
  702. extern struct cmdptr cmdstk[];          /* The command stack itself */
  703. #endif /* DCMDBUF */
  704. #endif /* NOSPL */
  705.  
  706. #define xsystem(s) zsyscmd(s)
  707.  
  708. static int x, y, z = 0;
  709. static char *s, *p;
  710.  
  711. #ifdef OS2
  712. _PROTOTYP( int os2settitle, (char *, int) );
  713. #endif /* OS2 */
  714.  
  715. extern struct keytab yesno[], onoff[], fntab[];
  716. extern int nyesno, nfntab;
  717.  
  718. #ifndef NOSPL
  719.  
  720. /* Do the ASK, ASKQ, GETOK, and READ commands */
  721.  
  722. int asktimedout = 0;
  723.  
  724. #define ASK_PUP 1
  725. #define ASK_TMO 2
  726. #define ASK_GUI 3
  727. #define ASK_QUI 4
  728.  
  729. static struct keytab asktab[] = {
  730.     {  "/gui",    ASK_GUI,      
  731. #ifdef KUI
  732.            0
  733. #else /* KUI */
  734.            CM_INV
  735. #endif /* KUI */
  736.     },
  737.     { "/popup",   ASK_PUP,   
  738. #ifdef OS2
  739.            0
  740. #else /* OS2 */
  741.            CM_INV
  742. #endif /* OS2 */
  743.     },
  744.     { "/quiet",   ASK_QUI, 0 },
  745.     { "/timeout", ASK_TMO, CM_ARG },
  746.     { "", 0, 0 }
  747. };
  748. static int nasktab = sizeof(asktab)/sizeof(struct keytab)-1;
  749.  
  750. int
  751. doask(cx) int cx; {
  752.     extern int cmflgs, asktimer, timelimit;
  753. #ifdef CK_RECALL
  754.     extern int on_recall;
  755. #endif /* CK_RECALL */
  756.     int popupflg = 0;
  757.     int guiflg = 0;
  758.     int nomsg = 0;
  759.     int mytimer = 0;
  760. #ifdef CK_APC
  761.     extern int apcactive, apcstatus;
  762. #endif /* CK_APC */
  763.  
  764.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  765.     char *vnp = NULL;                   /* Pointer to same */
  766.  
  767. #ifdef CK_APC
  768.     if ( apcactive != APC_INACTIVE && (apcstatus & APC_NOINP) ) {
  769.         return(success = 0);
  770.     }
  771. #endif /* CK_APC */
  772.  
  773.     mytimer = asktimer;                 /* Inherit global ASK timer */
  774.  
  775.     if (cx == XXASK || cx == XXASKQ) {
  776.         struct FDB sw, fl;
  777.         int getval;
  778.         char c;
  779.         if (cx == XXASKQ)               /* Don't log ASKQ response */
  780.           debok = 0;
  781.         cmfdbi(&sw,                     /* First FDB - command switches */
  782.                _CMKEY,                  /* fcode */
  783.                "Variable name or switch",
  784.                "",                      /* default */
  785.                "",                      /* addtl string data */
  786.                nasktab,                 /* addtl numeric data 1: tbl size */
  787.                4,                       /* addtl numeric data 2: 4 = cmswi */
  788.                xxstring,                /* Processing function */
  789.                asktab,                  /* Keyword table */
  790.                &fl                      /* Pointer to next FDB */
  791.                );
  792.         cmfdbi(&fl,                     /* Anything that doesn't match */
  793.                _CMFLD,                  /* fcode */
  794.                "",                      /* hlpmsg */
  795.                "",                      /* default */
  796.                "",                      /* addtl string data */
  797.                0,                       /* addtl numeric data 1 */
  798.                0,                       /* addtl numeric data 2 */
  799.                NULL,
  800.                NULL,
  801.                NULL
  802.                );
  803.         while (1) {                     /* Parse 0 or more switches */
  804.             x = cmfdb(&sw);             /* Parse something */
  805.             if (x < 0)
  806.               return(x);
  807.             if (cmresult.fcode != _CMKEY) /* Break out if not a switch */
  808.               break;
  809.             c = cmgbrk();
  810.             if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  811.                 printf("?This switch does not take an argument\n");
  812.                 return(-9);
  813.             }
  814.             if (!getval && (cmgkwflgs() & CM_ARG)) {
  815.                 printf("?This switch requires an argument\n");
  816.                 return(-9);
  817.             }
  818.             switch (cmresult.nresult) {
  819.           case ASK_QUI:
  820.         nomsg = 1;
  821.         break;
  822.               case ASK_PUP:
  823.                 popupflg = 1;
  824.                 break;
  825.           case ASK_GUI:
  826.         guiflg = 1;
  827.         break;
  828.               case ASK_TMO: {
  829.                   if ((y = cmnum("seconds","1",10,&x,xxstring)) < 0)
  830.                     return(y);
  831.                   if (x < 0)
  832.                     x = 0;
  833.                   mytimer = x;
  834.                   break;
  835.               }
  836.               default: return(-2);
  837.             }
  838.         }
  839.         /* Have variable name, make copy. */
  840.         ckstrncpy(vnambuf,cmresult.sresult,VNAML);
  841.         vnp = vnambuf;
  842.         if (vnambuf[0] == CMDQ &&
  843.             (vnambuf[1] == '%' || vnambuf[1] == '&'))
  844.           vnp++;
  845.         y = 0;
  846.         if (*vnp == '%' || *vnp == '&') {
  847.             if ((y = parsevar(vnp,&x,&z)) < 0)
  848.               return(y);
  849.         }
  850.     } else if (cx != XXGOK && cx != XXRDBL) { /* Get variable name */
  851.         if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  852.             if (y == -3) {
  853.                 printf("?Variable name required\n");
  854.                 return(-9);
  855.             } else return(y);
  856.         }
  857.         ckstrncpy(vnambuf,s,VNAML);     /* Make a copy. */
  858.         vnp = vnambuf;
  859.         if (vnambuf[0] == CMDQ &&
  860.             (vnambuf[1] == '%' || vnambuf[1] == '&'))
  861.           vnp++;
  862.         y = 0;
  863.         if (*vnp == '%' || *vnp == '&') {
  864.             if ((y = parsevar(vnp,&x,&z)) < 0)
  865.               return(y);
  866.         }
  867.     }
  868.     if (cx == XXREA || cx == XXRDBL) {  /* READ or READBLOCK command */
  869.         if ((y = cmcfm()) < 0)          /* Get confirmation */
  870.           return(y);
  871.         if (chkfn(ZRFILE) < 1) {        /* File open? */
  872.             printf("?Read file not open\n");
  873.             return(success = 0);
  874.         }
  875.         if (!(s = (char *)readbuf)) {           /* Where to read into. */
  876.             printf("?Oops, no READ buffer!\n");
  877.             return(success = 0);
  878.         }
  879.         y = zsinl(ZRFILE, s, readblock); /* Read a line. */
  880.         debug(F111,"read zsinl",s,y);
  881.         if (y < 0) {                    /* On EOF or other error, */
  882.             zclose(ZRFILE);             /* close the file, */
  883.             delmac(vnp,0);              /* delete the variable, */
  884.             return(success = 0);        /* and return failure. */
  885.         } else {                        /* Read was OK. */
  886.             readsize = (int) strlen(s);
  887.             success = (addmac(vnp,s) < 0 ? 0 : 1); /* Define variable */
  888.             debug(F111,"read addmac",vnp,success);
  889.             return(success);            /* Return success. */
  890.         }
  891.     }
  892.  
  893.     /* ASK, ASKQ, GETOK, or GETC */
  894.  
  895.     if (cx == XXGOK) {            /* GETOK can take switches */
  896.         struct FDB sw, fl;
  897.         int getval;
  898.         char c;
  899.         cmfdbi(&sw,                     /* First FDB - command switches */
  900.                _CMKEY,                  /* fcode */
  901.                "Variable name or question prompt",
  902.                "",                      /* default */
  903.                "",                      /* addtl string data */
  904.                nasktab,                 /* addtl numeric data 1: tbl size */
  905.                4,                       /* addtl numeric data 2: 4 = cmswi */
  906.                xxstring,                /* Processing function */
  907.                asktab,                  /* Keyword table */
  908.                &fl                      /* Pointer to next FDB */
  909.                );
  910.         cmfdbi(&fl,                     /* Anything that doesn't match */
  911.                _CMTXT,                  /* fcode */
  912.                "",                      /* hlpmsg */
  913.                "",                      /* default */
  914.                "",                      /* addtl string data */
  915.                0,                       /* addtl numeric data 1 */
  916.                0,                       /* addtl numeric data 2 */
  917.                NULL,
  918.                NULL,
  919.                NULL
  920.                );
  921.         while (1) {                     /* Parse 0 or more switches */
  922.             x = cmfdb(&sw);             /* Parse something */
  923.             if (x < 0)
  924.               return(x);
  925.             if (cmresult.fcode != _CMKEY) /* Break out if not a switch */
  926.               break;
  927.             c = cmgbrk();
  928.             if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  929.                 printf("?This switch does not take an argument\n");
  930.                 return(-9);
  931.             }
  932.             if (!getval && (cmgkwflgs() & CM_ARG)) {
  933.                 printf("?This switch requires an argument\n");
  934.                 return(-9);
  935.             }
  936.             switch (cmresult.nresult) {
  937.               case ASK_PUP:
  938.                 popupflg = 1;
  939.                 break;
  940.           case ASK_GUI:
  941.         guiflg = 1;
  942.         break;
  943.               case ASK_TMO: {
  944.                   if ((y = cmnum("seconds","1",10,&x,xxstring)) < 0)
  945.                     return(y);
  946.                   if (x < 0)
  947.                     x = 0;
  948.                   mytimer = x;
  949.                   break;
  950.               }
  951.           case ASK_QUI:
  952.         nomsg = 1;
  953.         break;
  954.               default: return(-2);
  955.             }
  956.     }
  957.     p = cmresult.sresult;
  958.     } else
  959.       if ((y = cmtxt(
  960. "Prompt, enclose in { braces } or \" quotes \" to preserve\n\
  961. leading and trailing spaces, precede question mark with backslash (\\).",
  962.                    "",&p,xxstring)) < 0)
  963.         return(y);
  964.  
  965.     if (!p) p = "";
  966. #ifndef NOLOCAL
  967. #ifdef OS2
  968.     if (popupflg) {                     /* Popup requested */
  969.         int len = -1;
  970.         ckstrncpy(tmpbuf,brstrip(p),TMPBUFSIZ);
  971.         p = tmpbuf;
  972.         if (cx == XXASK || cx == XXASKQ) {
  973.             if (cx == XXASK)
  974.               len = popup_readtext(vmode,NULL,p,line,LINBUFSIZ,mytimer);
  975.             else
  976.               len = popup_readpass(vmode,NULL,p,line,LINBUFSIZ,mytimer);
  977.             asktimedout = ( len < 0 && mytimer );
  978.         } else if (cx == XXGOK) {
  979.         printf("?Sorry, GETOK /POPUP not implemented yet\n");
  980.         timelimit = 0;
  981.         return(-9);
  982.     }
  983.         if (len >= 0)
  984.       y = addmac(vnp,(char *)line);    /* Add it to the macro table. */
  985.         timelimit = 0;
  986.         return(success = ((len >= 0) && (y >= 0)) && !asktimedout);
  987.     }
  988. #ifdef KUI
  989.     if (guiflg) {                       /* GUI requested */
  990.         int rc, n;
  991.     char * s1;
  992.     s1 = tmpbuf;
  993.     n = TMPBUFSIZ-1;
  994.     zzstring(brstrip(p),&s1,&n);
  995.         p = tmpbuf;
  996.         if (cx == XXASK || cx == XXASKQ) {
  997.             rc = gui_txt_dialog(NULL,p,(cx == XXASK),
  998.                                 line,LINBUFSIZ,NULL,mytimer);
  999.             asktimedout = (rc == -1 && mytimer);
  1000.         if (rc == 1)
  1001.           y = addmac(vnp,(char *)line); /* Add it to the macro table. */
  1002.         timelimit = 0;
  1003.         return(success = (rc == 1 && (y >= 0)) && !asktimedout);
  1004.     } else if (cx == XXGOK) {
  1005.         rc = uq_ok(NULL, p, 3, NULL, 2);
  1006.         return(success = (rc == 1));
  1007.     }
  1008.     }
  1009. #endif /* KUI */
  1010. #endif /* OS2 */
  1011. #endif /* NOLOCAL */
  1012.  
  1013.     concb((char)escape);                /* Enter CBREAK mode */
  1014.     cmsavp(psave,PROMPTL);              /* Save old prompt */
  1015.     cmsetp(brstrip(p));                 /* Make new prompt */
  1016. reprompt:
  1017.     if (cx == XXASKQ) {                 /* For ASKQ, */
  1018.         cmini(0);                       /* no-echo mode. */
  1019.     } else {                            /* For others, regular echoing. */
  1020.         cmini(ckxech);
  1021.     }
  1022.     askflag = 1;
  1023.     x = -1;                             /* This means to reparse. */
  1024.     cmflgs = 0;
  1025.     if (pflag)
  1026.       prompt(xxstring);                 /* Issue prompt. */
  1027.  
  1028.     asktimedout = 0;                    /* Handle timed responses. */
  1029.     timelimit = mytimer;
  1030. reparse:
  1031.     cmres();
  1032.     if (cx == XXGOK) {                  /* GETOK */
  1033. #ifdef CK_RECALL
  1034.         on_recall = 0;
  1035. #endif /* CK_RECALL */
  1036.         askflag = 0;
  1037.         x = cmkey(yesno,nyesno,"","",xxstring); /* GETOK uses keyword table */
  1038.         if (x < 0) {                    /* Parse error */
  1039.             if (x == -10) {
  1040.                 x = 0;
  1041.         if (!nomsg)
  1042.           printf("?Timed out, assuming \"No\"");
  1043.         printf("\n");
  1044.                 asktimedout = 1;
  1045.                 goto gokdone;
  1046.             } else if (x == -3) {       /* No answer? */
  1047.                 printf("Please respond Yes or No\n"); /* Make them answer */
  1048.                 cmini(ckxech);
  1049.                 goto reprompt;
  1050.             } else if (x == -1) {
  1051.                 goto reparse;
  1052.             } else
  1053.               goto reprompt;
  1054.         }
  1055.         if (cmcfm() < 0)                /* Get confirmation */
  1056.           goto reparse;
  1057.   gokdone:
  1058.         askflag = 0;
  1059.         cmsetp(psave);                  /* Restore prompt */
  1060. #ifdef VMS
  1061.         if (cmdlvl > 0)                 /* In VMS and not at top level, */
  1062.           conres();                     /*  restore console again. */
  1063. #endif /* VMS */
  1064.         timelimit = 0;
  1065.         return(x);                      /* Return success or failure */
  1066.     } else if (cx == XXGETC             /* GETC */
  1067. #ifdef OS2
  1068.                || cx == XXGETK          /* or GETKEYCODE */
  1069. #endif /* OS2 */
  1070.                ) {                      /* GETC */
  1071.         char tmp[16];
  1072.         conbin((char)escape);           /* Put keyboard in raw mode */
  1073. #ifndef NOSETKEY
  1074. #ifdef OS2
  1075.         if (cx == XXGETK) {             /* GETKEYCODE */
  1076.             extern int os2gks;
  1077.             int t;
  1078.             t = os2gks;                 /* Turn off kverb recognition */
  1079.             os2gks = 0;
  1080.             x = congks(timelimit);      /* Read a key event, blocking */
  1081.             os2gks = t;                 /* Put back kverb recognition */
  1082.         } else                          /* GETC */
  1083. #endif /* OS2 */
  1084. #endif /* NOSETKEY */
  1085.         {
  1086.             debug(F101,"GETC conchk","",conchk());
  1087.             x = coninc(timelimit);      /* Just read one character */
  1088.             debug(F101,"GETC coninc","",x);
  1089.         }
  1090.         concb((char)escape);            /* Put keyboard back in cbreak mode */
  1091.         if (x > -1) {
  1092.             if (xcmdsrc == 0)
  1093.               printf("\r\n");
  1094. #ifdef OS2
  1095.             if (cx == XXGETK) {         /* GETKEYCODE */
  1096.                 sprintf(tmp,"%d",x);    /* SAFE */
  1097.             } else {
  1098. #endif /* OS2 */
  1099.                 tmp[0] = (char) (x & 0xff);
  1100.                 tmp[1] = NUL;
  1101. #ifdef OS2
  1102.             }
  1103. #endif /* OS2 */
  1104.             y = addmac(vnp,tmp);        /* Add it to the macro table. */
  1105.             debug(F111,"getc/getk addmac",vnp,y);
  1106.         } else y = -1;
  1107.         cmsetp(psave);                  /* Restore old prompt. */
  1108.         if (x < -1) {
  1109.             asktimedout = 1;
  1110.             if (!quiet && !nomsg)
  1111.               printf("?Timed out");
  1112.         printf("\n");
  1113.         }
  1114.         timelimit = 0;
  1115.         return(success = ((y < 0 ? 0 : 1) && (asktimedout == 0)));
  1116.     } else {                            /* ASK or ASKQ */
  1117. #ifdef CK_RECALL
  1118.         on_recall = 0;
  1119. #endif /* CK_RECALL */
  1120.         y = cmdgquo();                  /* Get current quoting */
  1121.         cmdsquo(0);                     /* Turn off quoting */
  1122.         while (x == -1) {               /* Prompt till they answer */
  1123.             x = cmtxt("Please respond.","",&s,NULL);
  1124.             debug(F111,"ASK cmtxt",s,x);
  1125.             cmres();
  1126.         }
  1127.         cmdsquo(y);                     /* Restore previous quoting */
  1128.         if (cx == XXASKQ)               /* ASKQ must echo CRLF here */
  1129.           printf("\r\n");
  1130.         if (x < 0) {                    /* If cmtxt parse error, */
  1131.             cmsetp(psave);              /* restore original prompt */
  1132. #ifdef VMS
  1133.             if (cmdlvl > 0)             /* In VMS and not at top level, */
  1134.               conres();                 /*  restore console again. */
  1135. #endif /* VMS */
  1136.             if (x == -10) {
  1137.         if (!nomsg)
  1138.           printf("?Timed out");
  1139.         printf("\n");
  1140.                 asktimedout = 1;
  1141.                 success = x = 0;    /* (was "x = -9;") */
  1142.             }
  1143.             timelimit = 0;
  1144.             return(x);                  /* and return cmtxt's error code. */
  1145.         }
  1146.         if (*s == NUL) {                /* If user typed a bare CR, */
  1147.             cmsetp(psave);              /* Restore old prompt, */
  1148.             delmac(vnp,0);              /* delete variable if it exists, */
  1149. #ifdef VMS
  1150.             if (cmdlvl > 0)             /* In VMS and not at top level, */
  1151.               conres();                 /*  restore console again. */
  1152. #endif /* VMS */
  1153.             timelimit = 0;
  1154.             return(success = 1);        /* and return. */
  1155.         }
  1156.         y = addmac(vnp,s);              /* Add it to the macro table. */
  1157.         debug(F111,"ask addmac",vnp,y);
  1158.         cmsetp(psave);                  /* Restore old prompt. */
  1159. #ifdef VMS
  1160.         if (cmdlvl > 0)                 /* In VMS and not at top level, */
  1161.           conres();                     /*  restore console again. */
  1162. #endif /* VMS */
  1163.         timelimit = 0;
  1164.         return(success = (y < 0 ? 0 : 1) && (asktimedout == 0));
  1165.     }
  1166. }
  1167. #endif /* NOSPL */
  1168.  
  1169. #ifndef NOSPL
  1170. int
  1171. doincr(cx) int cx; {                    /* INCREMENT, DECREMENT */
  1172.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  1173.     int eval = 0;
  1174.     eval = (cx == XX_DECR || cx == XX_INCR);
  1175.  
  1176.     if ((y = cmfld("Variable name","",&s, eval ? xxstring : NULL)) < 0) {
  1177.         if (y == -3) {
  1178.             printf("?Variable name required\n");
  1179.             return(-9);
  1180.         } else return(y);
  1181.     }
  1182.     ckstrncpy(vnambuf,s,VNAML);
  1183.     if ((y = cmnum("by amount","1",10,&x,xxstring)) < 0)
  1184.       return(y);
  1185.     if ((y = cmcfm()) < 0)
  1186.       return(y);
  1187.  
  1188.     z = (cx == XX_INCR || cx == XXINC) ? 1 : 0; /* Increment or decrement? */
  1189.  
  1190.     if (incvar(vnambuf,x,z) < 0) {
  1191.         printf("?Variable %s not defined or not numeric\n",vnambuf);
  1192.         return(success = 0);
  1193.     }
  1194.     return(success = 1);
  1195. }
  1196.  
  1197. /* Used by doundef() */
  1198. static int
  1199. xxundef(s,verbose,simulate) char * s; int verbose, simulate; {
  1200.     int rc = 0;
  1201.     if (!s) return(0);
  1202.     if (*s == CMDQ && *(s+1) == '%') {
  1203.         char c = *(s+2), * p = NULL;
  1204.         if (c >= '0' && c <= '9') {
  1205.             if (maclvl < 0)
  1206.               p = g_var[c];
  1207.             else
  1208.               p = m_arg[maclvl][c - '0'];
  1209.         } else {
  1210.             if (isupper(c)) c += ('a'-'A');
  1211.             if (c >= 'a' && c <= 'z')
  1212.               p = g_var[c];
  1213.         }
  1214.         if (!p) return(-1);
  1215.     }
  1216.     if (verbose)
  1217.       printf(" %s ",s);
  1218.     if (simulate) {
  1219.         printf("(SELECTED)\n");
  1220.     } else if ((x = delmac(s,1)) > -1) { /* Full name required */
  1221.         rc = 1;
  1222.         if (verbose) printf("(OK)\n");
  1223.     } else if (verbose)
  1224.       printf("(FAILED)\n");
  1225.     return(rc);
  1226. }
  1227.  
  1228. /* Do the (_)DEFINE, (_)ASSIGN, and UNDEFINE commands */
  1229.  
  1230. #define UND_MAT 1
  1231. #define UND_VRB 2
  1232. #define UND_EXC 3
  1233. #define UND_SIM 3
  1234.  
  1235. static struct keytab undefswi[] = {
  1236.     { "/list",     UND_VRB, 0 },
  1237. #ifdef COMMENT
  1238.     { "/except",   UND_EXC, CM_ARG },
  1239. #endif /* COMMENT */
  1240.     { "/matching", UND_MAT, 0 },
  1241.     { "/simulate", UND_SIM, 0 },
  1242.     { "/verbose",  UND_VRB, CM_INV }
  1243. };
  1244. static int nundefswi = sizeof(undefswi) / sizeof(struct keytab);
  1245.  
  1246. #define UNDEFMAX 64
  1247. static char ** undeflist = NULL;
  1248. int
  1249. doundef(cx) int cx; {                   /* UNDEF, _UNDEF */
  1250.     int i, j, n, rc = 0, arraymsg = 0;
  1251.     int domatch = 0, verbose = 0, errors = 0, simulate = 0, flag = 0;
  1252.     char *vnp, vnbuf[4];
  1253. #ifdef COMMENT
  1254.     char *except = NULL;
  1255. #endif /* COMMENT */
  1256.     struct FDB sw, fl;
  1257.     int getval;
  1258.     char c;
  1259.  
  1260.     if (!undeflist) {                   /* Allocate list if necessary */
  1261.         undeflist = (char **)malloc(UNDEFMAX * sizeof(char *));
  1262.         if (!undeflist) {
  1263.             printf("?Memory allocation failure\n");
  1264.             return(-9);
  1265.         }
  1266.         for (i = 0; i < UNDEFMAX; i++)
  1267.           undeflist[i] = NULL;
  1268.     }
  1269.     cmfdbi(&sw,                         /* First FDB - command switches */
  1270.            _CMKEY,                      /* fcode */
  1271.            "Variable name or switch",
  1272.            "",                          /* default */
  1273.            "",                          /* addtl string data */
  1274.            nundefswi,                   /* addtl numeric data 1: tbl size */
  1275.            4,                           /* addtl numeric data 2: 4 = cmswi */
  1276.            xxstring,                    /* Processing function */
  1277.            undefswi,                    /* Keyword table */
  1278.            &fl                          /* Pointer to next FDB */
  1279.            );
  1280.     cmfdbi(&fl,                         /* Anything that doesn't match */
  1281.            _CMFLD,                      /* fcode */
  1282.            "",                          /* hlpmsg */
  1283.            "",                          /* default */
  1284.            "",                          /* addtl string data */
  1285.            0,                           /* addtl numeric data 1 */
  1286.            0,                           /* addtl numeric data 2 */
  1287.            (cx == XXUNDEF) ? NULL : xxstring,
  1288.            NULL,
  1289.            NULL
  1290.            );
  1291.     while (1) {                         /* Parse 0 or more switches */
  1292.         x = cmfdb(&sw);                 /* Parse something */
  1293.         if (x < 0)
  1294.           return(x);
  1295.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  1296.           break;
  1297.         c = cmgbrk();
  1298.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  1299.             printf("?This switch does not take an argument\n");
  1300.             return(-9);
  1301.         }
  1302.         switch (cmresult.nresult) {
  1303.           case UND_MAT: domatch  = 1; break;
  1304.           case UND_SIM: simulate = 1; /* fall thru on purpose */
  1305.           case UND_VRB: verbose  = 1; break;
  1306.  
  1307. #ifdef COMMENT
  1308.           case UND_EXC:
  1309.             if (!getval) break;
  1310.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  1311.                 if (x == -3) {
  1312.                     printf("?Pattern required\n");
  1313.                     x = -9;
  1314.                 }
  1315.                 goto xgetx;
  1316.             }
  1317.             makestr(&except,cmresult.sresult);
  1318.             break;
  1319. #endif /* COMMENT */
  1320.  
  1321.           default:
  1322.             return(-2);
  1323.         }
  1324.     }
  1325.     n = 0;
  1326.     makestr(&(undeflist[n++]),cmresult.sresult);
  1327.     for (i = 1; i < UNDEFMAX; i++) {
  1328.         x = cmfld("Macro or variable name","",&s,
  1329.                   ((cx == XXUNDEF) ? NULL : xxstring)
  1330.                   );
  1331.         if (x == -3) {
  1332.             if ((y = cmcfm()) < 0)
  1333.               return(y);
  1334.             break;
  1335.         } else if (y < 0) {
  1336.             return(y);
  1337.         }
  1338.         makestr(&(undeflist[n++]),s);
  1339.     }
  1340.     /* Now we have a list of n variables or patterns to undefine */
  1341.  
  1342.     for (i = 0; i < n; i++) {
  1343.         flag = 0;
  1344.         if (!(vnp = undeflist[i]))
  1345.           continue;
  1346.         if (vnp[0] == CMDQ && (vnp[1] == '%' || vnp[1] == '&')) {
  1347.             flag++;
  1348.             vnp++;
  1349.         }
  1350.         if (!domatch) {                 /* Pattern match not requested */
  1351.             if (flag) {
  1352.                 if ((y = parsevar(vnp,&x,&z)) < 0) {
  1353.                     vnp--;
  1354.                     if (verbose) printf(" %s...error\n",vnp);
  1355.                     continue;
  1356.                 }
  1357.                 vnp--;
  1358.             }
  1359.             x = xxundef(vnp,verbose,simulate);
  1360.             if (x > -1) {
  1361.                 if (!x && !simulate) errors++;
  1362.                 rc += x;
  1363.             }
  1364.             continue;
  1365.         }
  1366.         /* Pattern match requested */
  1367.  
  1368.         if (!flag) {                    /* It's a macro */
  1369.             for (j = 0; j < nmac; j++) {
  1370.                 if (ckmatch(vnp,mactab[j].kwd,0,1)) {
  1371.                     x = xxundef(mactab[j].kwd,verbose,simulate);
  1372.                     if (x > -1) {
  1373.                         rc += x;
  1374.                         if (!x) errors++;
  1375.                     }
  1376.                     if (!simulate)
  1377.                       j--;              /* Because mactab shifted up */
  1378.                 }
  1379.             }
  1380.         } else if (vnp[0] == '%') {     /* It's a \%x variable */
  1381.             vnbuf[0] = CMDQ;
  1382.             vnbuf[1] = '%';
  1383.             vnbuf[3] = NUL;
  1384.             for (j = '0'; j <= 'z'; j++) { /* 0..9 a..z */
  1385.                 vnbuf[2] = j;
  1386.                 if (ckmatch(vnp,&vnbuf[1],0,1)) {
  1387.                     x = xxundef(vnbuf,verbose,simulate);
  1388.                     if (x > -1) {
  1389.                         if (!x) errors++;
  1390.                         rc += x;
  1391.                     }
  1392.                 }
  1393.                 if (j == '9') j = (int)'a' - 1; /* 9 -> a */
  1394.             }
  1395.         } else if (vnp[0] == '&') {
  1396.             if (!arraymsg && !quiet) {
  1397.                 printf("?UNDEFINE /MATCH can't be used with arrays.\n");
  1398.                 printf("(Type HELP ARRAY to see other methods.)\n");
  1399.             }
  1400.             arraymsg++;
  1401.             errors++;
  1402.         }
  1403.     }
  1404.     if (verbose)
  1405.       printf("undefined: %d, errors: %d\n",rc,errors);
  1406.  
  1407.     for (i = 0; i < UNDEFMAX; i++) {    /* Check them all */
  1408.         if (undeflist[i]) {             /* in case we were interrupted */
  1409.             free(undeflist[i]);         /* previously... */
  1410.             undeflist[i] = NULL;
  1411.         }
  1412.     }
  1413.     return(success = (errors == 0) ? 1 : 0);
  1414. }
  1415.  
  1416. int
  1417. dodef(cx) int cx; {
  1418.     extern int xxdot;
  1419.     extern char ppvnambuf[];
  1420.     int doeval = 0;
  1421.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  1422.     char *vnp;                          /* Pointer to same */
  1423.     int k, mydot;
  1424.     mydot = xxdot;                      /* Copy */
  1425.     xxdot = 0;                          /* and reset */
  1426. /*
  1427.   In case we got here from a command that begins like ".\%a", cmkey() has
  1428.   already evaluated \%a, but we don't want that, so we retrieve the variable
  1429.   name from a special pre-evaluation buffer in the command module, and we
  1430.   undo the "unget word" that would be done because of the token, because if
  1431.   the variable was defined, it will unget its value rather than its name.
  1432. */
  1433.     s = NULL;
  1434.  
  1435.     if (mydot && ppvnambuf[0] == '.' && ppvnambuf[1]) {
  1436.         s = ppvnambuf+1;
  1437.         unungw();
  1438.     }
  1439.     if (!s) {
  1440.         if (cx == XXDFX || cx == XXASX)
  1441.           /* Evaluate variable name */
  1442.           y = cmfld("Macro or variable name","",&s,xxstring);
  1443.         else
  1444.           /* Don't evaluate the variable name */
  1445.           y = cmfld("Macro or variable name","",&s,NULL);
  1446.         if (y < 0) {
  1447.             if (y == -3) {
  1448.                 printf("?Variable name required\n");
  1449.                 return(-9);
  1450.             } else return(y);
  1451.         }
  1452.     }
  1453.     k = strlen(s);
  1454.     if (k > VNAML) {
  1455.         printf("?Name too long: \"%s\"\n",s);
  1456.         return(-9);
  1457.     }
  1458.     ckstrncpy(vnambuf,s,VNAML);
  1459.     vnambuf[VNAML] = NUL;
  1460.     vnp = vnambuf;
  1461.     if (vnambuf[0] == CMDQ && (vnambuf[1] == '%' || vnambuf[1] == '&')) vnp++;
  1462.     if (*vnp == '%' || *vnp == '&') {
  1463.         if ((y = parsevar(vnp,&x,&z)) < 0) return(y);
  1464. #ifdef COMMENT
  1465.         if (cx == XXUNDEF) {            /* Undefine */
  1466.             if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1467.             delmac(vnp,0);
  1468.             return(success = 1);
  1469.         }
  1470. #endif /* COMMENT */
  1471.         debug(F101,"dodef parsevar x","",x);
  1472.         if (mydot) {
  1473.             if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1474.               return(doeval);
  1475.             if (doeval > 0)             /* Type of assignment */
  1476.               cx = XXASS;
  1477.         }
  1478.         if (y == 1) {                   /* Simple variable */
  1479.             if ((y = cmtxt("Definition of variable","",&s,NULL)) < 0)
  1480.               return(y);
  1481.             s = brstrip(s);
  1482.             debug(F110,"xxdef var name",vnp,0);
  1483.             debug(F110,"xxdef var def",s,0);
  1484.         } else if (y == 2) {            /* Array element */
  1485.             if ((y = arraynam(vnp,&x,&z)) < 0) return(y);
  1486.             if (x == 96) {
  1487.                 printf("?Argument vector array is read-only\n");
  1488.                 return(-9);
  1489.             }
  1490.             if (chkarray(x,z) < 0) return(-2);
  1491.             if ((y = cmtxt("Definition of array element","",&s,NULL)) < 0)
  1492.               return(y);
  1493.             debug(F110,"xxdef array ref",vnp,0);
  1494.             debug(F110,"xxdef array def",s,0);
  1495.         }
  1496.     } else {                            /* Macro */
  1497. #ifdef COMMENT
  1498.         if (cx == XXUNDEF) {            /* Undefine */
  1499.             if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1500.             delmac(vnp,0);
  1501.             return(success = 1);
  1502.         }
  1503. #endif /* COMMENT */
  1504.         if (mydot) {
  1505.             if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1506.               return(doeval);
  1507.             if (doeval > 0)
  1508.               cx = XXASS;
  1509.         }
  1510.         if ((y = cmtxt("Definition of macro","",&s,NULL)) < 0) return(y);
  1511. #ifdef DEBUG
  1512.         if (deblog) {
  1513.             debug(F110,"xxdef macro name",vnp,0);
  1514.             debug(F010,"xxdef macro def",s,0);
  1515.         }
  1516. #endif /* DEBUG */
  1517.         s = brstrip(s);
  1518.     }
  1519.     if (*s == NUL) {                    /* No arg given, undefine */
  1520.         delmac(vnp,1);                  /* silently... */
  1521.         return(success = 1);            /* even if it doesn't exist... */
  1522.     }
  1523.     /* Defining a new macro or variable */
  1524.  
  1525.     if (cx == XXASS || cx == XXASX) {   /* ASSIGN rather than DEFINE? */
  1526.         int t;
  1527.         t = LINBUFSIZ-1;
  1528.         lp = line;                      /* If so, expand its value now */
  1529.         zzstring(s,&lp,&t);
  1530.         s = line;
  1531.     }
  1532.     if (doeval == 2) {                  /* Arithmetic evaluation wanted too? */
  1533.         ckstrncpy(line,evala(s),LINBUFSIZ);
  1534.         line[LINBUFSIZ] = NUL;
  1535.     }
  1536.     /* debug(F111,"calling addmac",s,(int)strlen(s)); */
  1537.  
  1538.     y = addmac(vnp,s);                  /* Add it to the appropriate table. */
  1539.     if (y < 0) {
  1540.         printf("?%s failed\n",(cx == XXASS || cx == XXASX) ?
  1541.                "ASSIGN" : "DEFINE");
  1542.         return(success = 0);
  1543.     } else if (cx == XXASX || cx == XXDFX) /* For _ASG or _DEF, */
  1544.       return(1);                           /* don't change success variable */
  1545.     else
  1546.       return(success = 1);
  1547. }
  1548. #endif /* NOSPL */
  1549.  
  1550.  
  1551. #ifndef NODIAL
  1552. /*
  1553.    L U D I A L  --  Lookup up dialing directory entry.
  1554.  
  1555.    Call with string to look up and file descriptor of open dialing directory
  1556.    file.  On success, returns number of matches found, with numbers stored
  1557.    in an array accessible via getdnum().
  1558. */
  1559. static char *dn_p[MAXDNUMS + 1];        /* Dial Number pointers */
  1560. static char *dn_p2[MAXDNUMS + 1];       /* Converted dial number pointers */
  1561. static int dn_x[MAXDNUMS + 1];          /* Type of call */
  1562. static int dncount = 0;
  1563. char * d_name = NULL;                   /* Dial name pointer */
  1564.  
  1565. char *                                  /* Get dial directory entry name */
  1566. getdname() {
  1567.     return(d_name ? d_name : "");
  1568. }
  1569.  
  1570. char *
  1571. getdnum(n) int n; {                     /* Get dial number n from directory */
  1572.     if (n < 0 || n > dncount || n > MAXDNUMS)
  1573.       return("");
  1574.     else
  1575.       return(dn_p[n]);
  1576. }
  1577.  
  1578. char *                  /* Check area code for spurious leading digit */
  1579. chk_ac(i,buf) int i; char buf[]; {
  1580.     char *p;
  1581.     if (!buf)
  1582.       return("");
  1583.     p = (char *) buf;                   /* Country we are calling: */
  1584.     if (i ==  44 ||                     /* UK */
  1585.         i ==  49 ||                     /* Germany */
  1586.         i ==  39 ||                     /* Italy */
  1587.         i ==  31 ||                     /* Netherlands */
  1588.         i == 351 ||                     /* Portugal */
  1589.         i ==  55 ||                     /* Brazil */
  1590.         i == 972 ||                     /* Israel */
  1591.         i ==  41 ||                     /* Switzerland */
  1592.         i ==  43 ||                     /* Austria */
  1593.         i ==  42 ||                     /* Czech Republic */
  1594.         i ==  36 ||                     /* Hungary */
  1595.         i ==  30 ||                     /* Greece */
  1596.         i == 352 ||                     /* Luxembourg */
  1597.         i ==  48 ||                     /* Poland */
  1598.         i ==  27 ||                     /* South Africa */
  1599.         i ==  33 ||                     /* France (as of 1997) */
  1600.         i ==  358                       /* Finland (ditto) */
  1601.         ) {
  1602.         if (buf[0] == '0')
  1603.           p++;
  1604.     }
  1605.     return(p);
  1606. }
  1607.  
  1608. /* Call Is Long Distance -- Expand this to cover 10-digit local dialing etc */
  1609. /*
  1610.    src  = area code of caller
  1611.    dest = area code of callee
  1612.    Returns:
  1613.      0 if call is local
  1614.      1 if call is long distance
  1615.      2 if call is local but area code must be dialed anyway
  1616. */
  1617. static int
  1618. callisld(src, dest) char * src, * dest; {
  1619.     int i;
  1620.     if (dialfld)                        /* Force long distance? */
  1621.       return(1);
  1622.     if (!strcmp(src,dest)) {            /* Area codes are the same */
  1623.         for (i = 0; i < nlocalac; i++)  /* Is AC in the lc-area-codes list? */
  1624.           if (!strcmp(src,diallcac[i]))
  1625.             return(2);                  /* Yes so must be dialed */
  1626.         return(0);                      /* No so don't dial it. */
  1627.     }
  1628.     for (i = 0; i < nlocalac; i++)      /* ACs not the same so look in list */
  1629.       if (!strcmp(dest,diallcac[i]))    /* Match */
  1630.         return(2);                      /* So local call with area code */
  1631.     return(1);                          /* Not local so long-distance */
  1632. }
  1633.  
  1634. char pdsfx[64] = { NUL, NUL };
  1635.  
  1636. #ifndef NOSPL
  1637. static char *
  1638. xdial(s) char *s; {                     /* Run dial string thru macro */
  1639.     int x, m;
  1640.     if (!dialmac)                       /* Dial macro name given? */
  1641.       return(NULL);
  1642.     if ((x = mxlook(mactab,dialmac,nmac)) < 0) /* Is the macro defined? */
  1643.       return(NULL);
  1644.     m = maclvl;
  1645.     x = dodo(x,s,0);                    /* Set up the macro */
  1646.     if (x > 0) {
  1647.         while (maclvl > m)              /* Execute the parser */
  1648.           parser(1);
  1649.         return(mrval[maclvl+1]);        /* Return the result */
  1650.     }
  1651.     return(NULL);
  1652. }
  1653. #endif /* NOSPL */
  1654.  
  1655. static int
  1656. dncvt(k,cx, prefix, suffix)
  1657.     int k, cx, prefix, suffix; {        /* Dial Number Convert */
  1658.     int i, j, n, what;                  /* cx is top-level command index */
  1659.     char *ss;                           /* prefix - add prefixes? */
  1660.     char *p, *p2, *pxo;                 /* suffix - add suffixes? */
  1661.     char *lac;
  1662.     char *npr;
  1663.     char *sfx;
  1664.     /* char *psfx; */
  1665.     char ccbuf[128];
  1666.     int cc;
  1667.     char acbuf[24];
  1668.     char *acptr;
  1669.     char outbuf[256];
  1670. /*
  1671.   First pass for strict (punctuation-based) interpretation.
  1672.   If it fails, we try the looser (length-based) one.
  1673. */
  1674.     dialtype = -1;
  1675.     what = 0;                           /* Type of call */
  1676.     s = dn_p[k];                        /* Number to be converted. */
  1677.     debug(F111,"dncvt",s,k);
  1678.     if (dn_p2[k]) {
  1679.         free(dn_p2[k]);
  1680.         dn_p2[k] = NULL;
  1681.     }
  1682.     if (!s) {
  1683.         printf("Error - No phone number to convert\n");
  1684.         return(-1);
  1685.     }
  1686.     if ((int)strlen(s) > 200) {
  1687.         ckstrncpy(outbuf,s,40);
  1688.         printf("?Too long: \"%s...\"\n",outbuf);
  1689.         return(-1);
  1690.     }
  1691.     npr = (prefix && dialnpr) ? dialnpr : "";
  1692.     sfx = (suffix && dialsfx) ? dialsfx : "";
  1693.     /* if (partial) psfx = dialsfx ? dialsfx : ""; */
  1694.     pxo = (prefix && dialpxo) ? dialpxo : "";
  1695.     lac = diallac ? diallac : "";       /* Local area code */
  1696.  
  1697.     outbuf[0] = NUL;                    /* Initialize conversion buffer */
  1698.     ss = s;                             /* Remember original string */
  1699.  
  1700.     if (*s != '+') {                    /* Literal number */
  1701.         dn_x[k] = DN_UNK;               /* Sort key is "unknown". */
  1702.         ckmakmsg(outbuf,256,            /* Sandwich it between */
  1703.                  pxo,npr,s,sfx          /* DIAL PREFIX and SUFFIX */
  1704.                 );
  1705. #ifdef CK_TAPI
  1706.         if (tttapi &&                   /* TAPI does its own conversions */
  1707.             !tapipass &&                /* if not in passthru mode */
  1708.             tapiconv == CK_AUTO ||      /* and TAPI conversions are AUTO */
  1709.             tapiconv == CK_ON           /* OR if TAPI conversions are ON */
  1710.             ) {
  1711.             char * p = NULL;
  1712.             dialtype = -2;
  1713.             if (!cktapiConvertPhoneNumber(dn_p[k], &p))
  1714.               return(-1);
  1715.             makestr(&dn_p2[k], p);
  1716.             if (p) free(p);
  1717.             return(0);
  1718.         } else
  1719. #endif /* CK_TAPI */
  1720.           makestr(&dn_p2[k], outbuf);   /* Not TAPI */
  1721.         dialtype = what;
  1722.         return(0);                      /* Done. */
  1723.     }
  1724.     i = 0;                              /* Portable number */
  1725.     s++;                                /* Tiptoe past the plus sign */
  1726.     ccbuf[0] = NUL;                     /* Do country code first */
  1727.  
  1728.     if (!diallcc) {                     /* Do we know our own? */
  1729.         if (cx != XXLOOK)
  1730.           printf("Error - prior SET DIAL COUNTRY-CODE command required\n");
  1731.         return(-1);
  1732.     }
  1733.  
  1734.     /* Parse the number */
  1735.  
  1736.     while (1) {                         /* Get the country code */
  1737.         while (*s == HT || *s == SP)
  1738.           s++;
  1739.         if (!s)                         /* Not in standard format */
  1740.           break;
  1741.         if (*s == '(') {                /* Beginning of area code  */
  1742.             s++;                        /* Skip past parenthesis   */
  1743.             ccbuf[i] = NUL;             /* End of country code */
  1744.             if (!s) {                   /* Check for end of string */
  1745.                 printf("Error - phone number ends prematurely: \"%s\"\n",ss);
  1746.                 return(-1);
  1747.             }
  1748.             break;
  1749.         } else {                        /* Collect country code */
  1750.             if (isdigit(*s))
  1751.               ccbuf[i++] = *s;          /* copy this character */
  1752.             s++;
  1753.             if (!*s || i > 127)         /* watch out for memory leak */
  1754.               break;
  1755.         }
  1756.     }
  1757.     cc = atoi(ccbuf);                   /* Numeric version of country code */
  1758.  
  1759.     i = 0;                              /* Now get area code */
  1760.     acbuf[0] = NUL;                     /* Initialize area-code buffer */
  1761.     acptr = acbuf;                      /* and pointer. */
  1762.     while (1) {
  1763.         while (*s == HT || *s == SP)    /* Ignore whitespace */
  1764.           s++;
  1765.         if (!s)                         /* String finished */
  1766.           break;
  1767.         if (*s == ')') {                /* End of area code  */
  1768.             s++;                        /* Skip past parenthesis   */
  1769.             acbuf[i] = NUL;             /* Terminate area-code buffer */
  1770.             break;
  1771.         } else {                        /* Part of area code */
  1772.             if (isdigit(*s))            /* If it's a digit, */
  1773.               acbuf[i++] = *s;          /* copy this character */
  1774.             s++;                        /* Point to next */
  1775.             if (!*s || i > 23)          /* Watch out for overflow */
  1776.               break;
  1777.         }
  1778.     }
  1779.  
  1780. /*
  1781.    Here we strip any leading 0 for countries that we know have
  1782.    0 as a long-distance prefix and do not have any area codes that
  1783.    start with 0 (formerly also ditto for "9" in Finland...)
  1784. */
  1785.     i = atoi(ccbuf);
  1786.     acptr = chk_ac(i,acbuf);
  1787.  
  1788.     while (*s == HT || *s == SP)        /* Skip whitespace */
  1789.       s++;
  1790.  
  1791. /* printf("S=[%s], ACPTR=[%s]\n",s,acptr); */
  1792.  
  1793.     if (*s && *acptr) {                 /* Area code was delimited */
  1794.  
  1795.         while (*s == '-' || *s == '.')  /* Skip past gratuitious punctuation */
  1796.           s++;
  1797.         if (!*s) s--;                   /* But not to end of string */
  1798.  
  1799.         if (strcmp(diallcc,ccbuf)) {    /* Out of country? */
  1800.             if (!dialixp) {             /* Need intl-prefix */
  1801.                 if (cx != XXLOOK)
  1802.                   printf("Error - No international dialing prefix defined\n");
  1803.                 return(-1);
  1804.             }
  1805.             what = dn_x[k] = DN_INTL;
  1806.             p  = (prefix && dialixp) ? dialixp : ""; /* Intl-prefix */
  1807.             p2 = (suffix && dialixs) ? dialixs : ""; /* Intl-suffix */
  1808.  
  1809.             /* Form the final phone number */
  1810. #ifdef COMMENT
  1811.             sprintf(pdsfx,"%s%s",p2,sfx); /* UNSAFE */
  1812.             sprintf(outbuf,
  1813.                     "%s%s%s%s%s%s%s%s",
  1814.                     pxo,npr,p,ccbuf,acptr,s,p2,sfx
  1815.                     );
  1816. #else
  1817.             ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1818.             ckmakxmsg(outbuf,256,pxo,npr,p,ccbuf,acptr,s,p2,sfx,
  1819.                       NULL,NULL,NULL,NULL);
  1820. #endif /* COMMENT */
  1821.  
  1822.         } else if ((x = callisld(lac,acptr)) >= 1) { /* In-country LD */
  1823.             if (!diallac && cx != XXLOOK) { /* Don't know my own area code */
  1824.                 if (cc == 1)
  1825.                   printf("WARNING - Prior SET DIAL AREA-CODE needed\n");
  1826.             }
  1827.             if (x == 2) {               /* Local call with area code */
  1828.                 what = dn_x[k] = DN_LOCAL;      /* Local-call */
  1829.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  1830.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  1831.             } else {
  1832.                 what = dn_x[k] = DN_LONG;       /* Long-distance */
  1833.                 for (i = 0; i < ntollfree; i++) { /* But toll-free too? */
  1834.                     if (!strcmp(acptr,dialtfc[i])) {
  1835.                         what = dn_x[k] = DN_FREE;
  1836.                         break;
  1837.                     }
  1838.                 }
  1839.                 if (what == DN_FREE) {  /* Toll-free call */
  1840.                     p = (prefix && dialtfp) ? dialtfp :
  1841.                         ((prefix && dialldp) ? dialldp : "");
  1842.                     p2 = "";            /* no suffix */
  1843.                 } else {                        /* normal long distance */
  1844.                     p  = (prefix && dialldp) ? dialldp : ""; /* ld-prefix */
  1845.                     p2 = (suffix && diallds) ? diallds : ""; /* ld-suffix */
  1846.                 }
  1847.             }
  1848.             /* Form the number to be dialed */
  1849. #ifdef COMMENT
  1850.             sprintf(outbuf,"%s%s%s%s%s%s%s",
  1851.                     pxo,npr,p,acptr,s,p2,sfx
  1852.                     );
  1853.             sprintf(pdsfx,"%s%s",p2,sfx);
  1854. #else
  1855.             ckmakxmsg(outbuf,256,
  1856.                       pxo,npr,p,acptr,s,p2,sfx,
  1857.                       NULL,NULL,NULL,NULL,NULL);
  1858.             ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1859. #endif /* COMMENT */
  1860.         } else {                        /* Same country, same area code */
  1861.             what = dn_x[k] = DN_LOCAL;  /* So it's a local call. */
  1862.             if (!prefix || !(dialpxo || ndialpxx)) { /* Not dialing from PBX */
  1863.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  1864.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  1865. #ifdef COMMENT
  1866.                 if (x == 2)
  1867.                   sprintf(outbuf,"%s%s%s%s%s%s",npr,p,acptr,s,p2,sfx);
  1868.                 else
  1869.                   sprintf(outbuf,"%s%s%s%s%s",npr,p,s,p2,sfx);
  1870.                 sprintf(pdsfx,"%s%s",p2,sfx);
  1871. #else
  1872.                 if (x == 2)
  1873.                   ckmakxmsg(outbuf,256,
  1874.                             npr,p,acptr,s,p2,sfx,
  1875.                             NULL,NULL,NULL,NULL,NULL,NULL);
  1876.                 else
  1877.                   ckmakxmsg(outbuf,256,
  1878.                             npr,p,s,p2,sfx,
  1879.                             NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  1880.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1881. #endif /* COMMENT */
  1882.  
  1883.             } else {                    /* Dialing from a PBX and not TAPI */
  1884.                 if (ndialpxx) {         /* Is it internal? */
  1885. #ifdef COMMENT
  1886.                     i = (int) strlen(dialpxx);
  1887.                     j = (int) strlen(s);
  1888.                     x = -1;
  1889.                     if (j > i)
  1890.                       x = ckstrcmp(dialpxx,s,i,0);
  1891. #else
  1892.                     int kx;
  1893.                     x = -1;
  1894.                     j = (int) strlen(s);
  1895.                     for (kx = 0; kx < ndialpxx; kx++) {
  1896.                         i = (int) strlen(dialpxx[kx]);
  1897.                         if (j > i)
  1898.                           if (!(x = ckstrcmp(dialpxx[kx],s,i,0)))
  1899.                             break;
  1900.                     }
  1901. #endif /* COMMENT */
  1902.                     if (!x) {
  1903.                         char * icp, buf[32];
  1904.                         makestr(&matchpxx,dialpxx[kx]);
  1905.                         debug(F111,"dncvt matchpxx",matchpxx,kx);
  1906.                         what = dn_x[kx] = DN_INTERN;   /* Internal call. */
  1907.                         s += i;
  1908.                         /* Internal-call prefix */
  1909.                         icp = dialpxi;
  1910. #ifndef NOSPL
  1911.                         if (icp) {
  1912.                             if (*icp == '\\') {
  1913.                                 char c, *bp;
  1914.                                 int n;
  1915.                                 c = *(icp+1);
  1916.                                 if (isupper(c)) c = tolower(c);
  1917.                                 if (c == 'v' || c == 'f') {
  1918.                                     n = 32;
  1919.                                     bp = buf;
  1920.                                     zzstring(icp,&bp,&n);
  1921.                                     icp = buf;
  1922.                                 }
  1923.                             }
  1924.                         }
  1925. #endif /* NOSPL */
  1926.                         p = (prefix && icp) ? icp : "";
  1927. #ifdef COMMENT
  1928.                         sprintf(outbuf,"%s%s%s%s",npr,p,s,sfx);
  1929. #else
  1930.                         ckmakmsg(outbuf,256,npr,p,s,sfx);
  1931. #endif /* COMMENT */
  1932.                     } else {            /* External local call */
  1933.                         /* local-prefix */
  1934.                         p  = (prefix && diallcp) ? diallcp : "";
  1935.                         /* local-suffix */
  1936.                         p2 = (prefix && diallcs) ? diallcs : "";
  1937. #ifdef COMMENT
  1938.                         if (x == 2)
  1939.                           sprintf(outbuf,"%s%s%s%s%s%s%s",
  1940.                                   dialpxo ? dialpxo : "",
  1941.                                   npr,p,acptr,s,p2,sfx);
  1942.                         else
  1943.                           sprintf(outbuf,
  1944.                                   "%s%s%s%s%s%s",
  1945.                                   dialpxo ? dialpxo : "",
  1946.                                   npr,p,s,p2,sfx
  1947.                                   );
  1948. #else
  1949.                         if (x == 2)
  1950.                           ckmakxmsg(outbuf, 256,
  1951.                                    dialpxo ? dialpxo : "",
  1952.                                    npr,p,acptr,s,p2,sfx,
  1953.                                    NULL,NULL,NULL,NULL,NULL);
  1954.                         else
  1955.                           ckmakxmsg(outbuf, 256,
  1956.                                     dialpxo ? dialpxo : "",
  1957.                                     npr,p,s,p2,sfx,
  1958.                                     NULL,NULL,NULL,NULL,NULL,NULL);
  1959. #endif /* COMMENT */
  1960.                     }
  1961.                 }
  1962.             }
  1963.         }
  1964.  
  1965.     } else {                            /* Area code was not delimited */
  1966.  
  1967.         char xbuf[256];                 /* Comparison based only on length */
  1968.         char ybuf[256];
  1969.         int x, j;
  1970.  
  1971.         s = ss;
  1972.  
  1973.         for (i = 0; i < 255; i++) {
  1974.             if (!*s) break;
  1975.             while (!isdigit(*s)) {      /* Pay attention only to digits */
  1976.                 s++;
  1977.                 if (!*s) break;
  1978.             }
  1979.             xbuf[i] = *s++;
  1980.         }
  1981.         xbuf[i] = NUL;
  1982.  
  1983.         x = 1;                          /* Assume LD */
  1984.         n = 0;
  1985.         if (!dialfld) {                 /* If LD not forced */
  1986.             for (j = 0; j < nlocalac; j++) { /* check local AC list? */
  1987.                 ckmakmsg(ybuf,256,diallcc,diallcac[j],NULL,NULL);
  1988.                 n = (int) strlen(ybuf);
  1989.                 if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) {
  1990.                     x = 2;
  1991.                     break;
  1992.                 }
  1993.             }
  1994.             if (x == 1) {               /* Or exact match with local CC+AC? */
  1995.                 ckmakmsg(ybuf,256,diallcc,lac,NULL,NULL);
  1996.                 n = (int) strlen(ybuf);
  1997.                 if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0))
  1998.                   x = 0;
  1999.             }
  2000.         }
  2001.         if (x == 0 || x == 2) {         /* Local call */
  2002.             int xx,kx;                  /* Begin 1 Dec 2001... */
  2003.             /* Account for PBX internal calls */
  2004.             if (ndialpxx) {
  2005.                 xx = -1;
  2006.                 j = (int) strlen(ybuf);
  2007.                 for (kx = 0; kx < ndialpxx; kx++) {
  2008.                     i = (int) strlen(dialpxx[kx]);
  2009.                     if (j >= i)
  2010.                       if (!(xx = ckstrcmp(dialpxx[kx],&xbuf[j],i,0)))
  2011.                         break;
  2012.                 }
  2013.             }
  2014.             if (!xx) {
  2015.                 char * icp, buf[32];
  2016.                 makestr(&matchpxx,dialpxx[kx]);
  2017.                 debug(F111,"dncvt matchpxx",matchpxx,kx);
  2018.                 what = dn_x[kx] = DN_INTERN; /* Internal call. */
  2019.                 s = xbuf + j + i;
  2020.                 icp = dialpxi;          /* Internal-call prefix */
  2021. #ifndef NOSPL
  2022.                 if (icp) {
  2023.                     if (*icp == '\\') {
  2024.                         char c, *bp;
  2025.                         int n;
  2026.                         c = *(icp+1);
  2027.                         if (isupper(c)) c = tolower(c);
  2028.                         if (c == 'v' || c == 'f') {
  2029.                             n = 32;
  2030.                             bp = buf;
  2031.                             zzstring(icp,&bp,&n);
  2032.                             icp = buf;
  2033.                         }
  2034.                     }
  2035.                 }
  2036. #endif /* NOSPL */
  2037.                 p = (prefix && icp) ? icp : "";
  2038.                 ckmakmsg(outbuf,256,npr,p,s,sfx);
  2039.                 /* End 1 Dec 2001... */
  2040.  
  2041.             } else {                    /* Not PBX internal */
  2042.  
  2043.                 dn_x[k] = DN_LOCAL;
  2044.                 p = (prefix && diallcp) ? diallcp : "";
  2045.                 p2 = (suffix && diallcs) ? diallcs : "";
  2046.                 s = (char *) (xbuf + ((x == 0) ? n : (int)strlen(diallcc)));
  2047.                 ckmakxmsg(outbuf,256,
  2048.                           pxo,npr,p,s,p2,sfx,
  2049.                           NULL,NULL,NULL,NULL,NULL,NULL);
  2050.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2051.             }
  2052.         } else {                        /* Not local */
  2053.             n = ckstrncpy(ybuf,diallcc,256);
  2054.             if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) { /* Long distance */
  2055.                 dn_x[k] = DN_LONG;
  2056.                 p = (prefix && dialldp) ? dialldp : "";
  2057.                 p2 = (suffix && diallds) ? diallds : "";
  2058.                 s = xbuf + n;
  2059.                 while (*s == '-' || *s == '.')
  2060.                   s++;
  2061. #ifdef COMMENT
  2062.                 sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,s,p2,sfx);
  2063.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2064. #else
  2065.                 ckmakxmsg(outbuf,256,
  2066.                           pxo,npr,p,s,p2,sfx,
  2067.                          NULL,NULL,NULL,NULL,NULL,NULL);
  2068.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2069. #endif /* COMMENT */
  2070.             } else {
  2071.                 dn_x[k] = DN_INTL;      /* International */
  2072.                 if (!dialixp) {
  2073.                     if (cx != XXLOOK) {
  2074.                         printf(
  2075.                           "Error - No international dialing prefix defined\n"
  2076.                                );
  2077.                         return(-1);
  2078.                     }
  2079.                 }
  2080.                 p = (prefix && dialixp) ? dialixp : "";
  2081.                 p2 = (suffix && dialixs) ? dialixs : "";
  2082. #ifdef COMMENT
  2083.                 sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,xbuf,p2,sfx);
  2084.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2085. #else
  2086.                 ckmakxmsg(outbuf,256,
  2087.                           pxo,npr,p,xbuf,p2,sfx,
  2088.                           NULL,NULL,NULL,NULL,NULL,NULL);
  2089.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2090. #endif /* COMMENT */
  2091.             }
  2092.         }
  2093.     }
  2094. #ifdef CK_TAPI
  2095.     if (tttapi &&                       /* TAPI performs the conversions */
  2096.         !tapipass &&
  2097.         tapiconv == CK_AUTO ||
  2098.         tapiconv == CK_ON
  2099.         ) {
  2100.         p = NULL;
  2101.         dialtype = -2;
  2102.         if (!cktapiConvertPhoneNumber(dn_p[k],&p))
  2103.           return(-1);
  2104.         makestr(&dn_p2[k], p);
  2105.         if (p) free(p);
  2106.         return(0);
  2107.     } else {
  2108. #endif /* CK_TAPI */
  2109.         makestr(&dn_p2[k], outbuf);
  2110. #ifdef CK_TAPI
  2111.     }
  2112. #endif /* CK_TAPI */
  2113.     dialtype = what;
  2114.     return(0);
  2115. }
  2116.  
  2117. static int
  2118. ddcvt(s, f, n) char * s; FILE * f; int n; { /* Dial Directory Convert */
  2119.     char linebuf[1024], *s2;            /* Buffers and pointers */
  2120. #ifdef VMS
  2121.     char * temp = NULL;
  2122. #endif /* VMS */
  2123.     char *info[8];                      /* Pointers to words from entry */
  2124.     FILE * f2 = NULL;
  2125.     int x, rc;
  2126.     rc = -1;
  2127.  
  2128.     debug(F110,"ddcvt file",s,0);
  2129.  
  2130.     if (!s || !f)                       /* No filename or file */
  2131.       return(-1);
  2132.     if (!*s)
  2133.  
  2134.     fclose(f);
  2135.     znewn(s,&s2);                       /* s2 = address of static buffer */
  2136.     debug(F110,"ddcvt newname",s2,0);
  2137.  
  2138. #ifdef VMS
  2139.     /* In VMS, znewn() returns the same file name with a new version number */
  2140.     makestr(&temp,s);                   /* Swap - otherwise the new */
  2141.     s = s2;                             /* version has the older version */
  2142.     s2 = temp;                          /* number... */
  2143.     debug(F110,"ddcvt after swap s",s,0);
  2144.     debug(F110,"ddcvt after swap s2",s2,0);
  2145.     makestr(&(dialdir[n]),s);           /* New file gets new version number */
  2146.     debug(F110,"ddcvt after makestr s2",s2,0);
  2147.     debug(F111,"ddcvt dialdir[n]",dialdir[n],n);
  2148. #else
  2149.     if (zrename(s,s2) < 0) {            /* Not VMS - rename old file */
  2150.         perror(s2);                     /* to new (wierd) name. */
  2151.         goto ddexit;
  2152.     }
  2153. #endif /* VMS */
  2154.     debug(F110,"ddcvt s2 (old)",s2,0);
  2155.     if ((f = fopen(s2,"r")) == NULL) {  /* Reopen old file with wierd name */
  2156.         debug(F110,"ddcvt s2 open error",ck_errstr(),0);
  2157.         dirline = 0;                    /* (or in VMS, old version) */
  2158.         perror(s2);
  2159.         goto ddexit;
  2160.     }
  2161.     debug(F110,"ddcvt fopen(s2) OK",s2,0);
  2162.  
  2163.     debug(F110,"ddcvt s (new)",s,0);
  2164.     if ((f2 = fopen(s,"w")) == NULL) {  /* Create new file with old name */
  2165.         debug(F110,"ddcvt s open error",ck_errstr(),0);
  2166.         perror(s);                      /* (or in VMS, new version) */
  2167.         goto ddexit;
  2168.     }
  2169.     debug(F110,"ddcvt fopen(s) OK",s,0);
  2170.  
  2171.     printf("\nSaving old directory as %s.\nConverting %s...",s2,s);
  2172.     fprintf(f2,"; %s - Kermit dialing directory\n", s);
  2173.     fprintf(f2,"%-16s %-20s ; %5s %-6s ; %s\n",
  2174.                "; Name","Number","Speed","Parity","Comment"
  2175.                );
  2176.  
  2177.     while (1) {
  2178.         linebuf[0] = NUL;               /* Read a line */
  2179.         if (fgets(linebuf,1023,f) == NULL)
  2180.           break;
  2181.         debug(F110,"ddcvt linebuf",linebuf,0);
  2182.         if (!linebuf[0]) {              /* Empty line */
  2183.             fprintf(f2,"\n");
  2184.             continue;
  2185.         }
  2186.         x = (int) strlen(linebuf);      /* Strip line terminator, */
  2187.         while (x-- > 0) {               /* if any. */
  2188.             if (linebuf[x] <= SP)
  2189.               linebuf[x] = NUL;
  2190.             else
  2191.               break;
  2192.         }
  2193.         xwords(linebuf,5,info,1);       /* Parse it the old way */
  2194.         for (x = 1; x < 6; x++)
  2195.           if (!info[x]) info[x] = "";
  2196.         fprintf(f2,"%-16s %-20s ; %5s %-6s %s\n",
  2197.                info[1],info[2],info[3],info[4],info[5]
  2198.                );
  2199.     }
  2200.     printf(" OK\n\n");
  2201.     rc = 0;                             /* Success */
  2202.   ddexit:
  2203.     if (f) fclose(f);
  2204.     if (f2) fclose(f2);
  2205. #ifdef VMS
  2206.     if (temp) free(temp);
  2207. #endif /* VMS */
  2208.     return(rc);
  2209. }
  2210.  
  2211. int                                     /* s = name to look up   */
  2212. #ifdef CK_ANSIC                         /* cx = index of command */
  2213. ludial(char *s, int cx)                 /* (DIAL, LOOKUP, etc)   */
  2214. #else
  2215. ludial(s, cx) char *s; int cx;
  2216. #endif /* CK_ANSIC */
  2217. /* ludial */ {
  2218.  
  2219.     int dd, n1, n2, n3, i, j, t;        /* Workers */
  2220.     int olddir, newdir, oldentry, newentry;
  2221.     int pass = 0;
  2222.     int oldflg = 0;
  2223.     int ambiguous = 0;                  /* Flag for lookup was ambiguous */
  2224.     char *info[7];                      /* Pointers to words from entry */
  2225.     char *pp;                           /* Pointer to element of array */
  2226.     FILE * f;
  2227.     char *line;                         /* File input buffer */
  2228.  
  2229. /* #define LUDEBUG */
  2230.  
  2231. #ifdef LUDEBUG
  2232. int zz = 1;
  2233. #endif /* LUDEBUG */
  2234.  
  2235.     if (!s || ndialdir < 1)             /* Validate arguments */
  2236.       return(-1);
  2237.  
  2238.     if ((n1 = (int) strlen(s)) < 1)     /* Length of string to look up */
  2239.       return(-1);
  2240.  
  2241.     if (!(line = malloc(1024)))         /* Allocate input buffer */
  2242.       return(-1);
  2243.  
  2244. #ifdef LUDEBUG
  2245. if (zz) printf("LUDIAL 1 s[%s], n1=%d\n",s,n1);
  2246. #endif /* LUDEBUG */
  2247.  
  2248.     pass = 0;
  2249.   lu_again:
  2250.     f = NULL;                           /* Dial directory file descriptor */
  2251.     t = dncount = 0;                    /* Dial-number match count */
  2252.     dd = 0;                             /* Directory counter */
  2253.     olddir = 0;
  2254.     newdir = 0;
  2255. /*
  2256.   We need to recognize both old- and new-style directories.
  2257.   But we can't allow old-style and new-style entries in the same
  2258.   directory because there is no way to tell for sure the difference between
  2259.   an old-style entry like this:
  2260.  
  2261.     foo  5551212  9600
  2262.  
  2263.   and a new-style literal entry like this:
  2264.  
  2265.     foo  555 9600
  2266.  
  2267.   I.e. is the "9600" a speed, or part of the phone number?
  2268. */
  2269.     while (1) {                         /* We make one pass */
  2270.         if (!f) {                       /* Directory not open */
  2271.             if (dd >= ndialdir)         /* No directories left? */
  2272.               break;                    /* Done. */
  2273.             debug(F111,"ludial dialdir[dd]",dialdir[dd],dd);
  2274.             if ((f = fopen(dialdir[dd],"r")) == NULL) { /* Open it */
  2275.                 perror(dialdir[dd]);    /* Can't, print message saying why */
  2276.                 if (line) {
  2277.                     free(line);
  2278.                     line = NULL;
  2279.                 }
  2280.                 dd++;                   /* Go on to next one, if any... */
  2281.                 continue;
  2282.             }
  2283.             dirline = 0;                /* Directory file line number */
  2284.             if (dialdpy && !pass)
  2285.               printf("Opening: %s...\n",dialdir[dd]);
  2286.             dd++;
  2287.             if (!oldflg) olddir = 0;
  2288.             newdir = 0;
  2289.         }
  2290.         oldentry = 0;
  2291.         newentry = 0;
  2292.         line[0] = NUL;
  2293.         if (getnct(line,1023,f,1) < 0) { /* Read a line */
  2294.             if (f) {                    /* f can be clobbered! */
  2295.                 fclose(f);              /* Close the file */
  2296.                 f = NULL;               /* Indicate next one needs opening */
  2297.                 oldflg = 0;
  2298.             }
  2299.             continue;
  2300.         }
  2301.         if (!line[0])                   /* Empty line */
  2302.           continue;
  2303. #ifdef LUDEBUG
  2304. if (zz) printf("LUDIAL 2 s[%s]\n",s);
  2305. #endif /* LUDEBUG */
  2306.  
  2307.         /* Make a copy and parse it the old way */
  2308.         /* A copy is needed because xwords() pokes NULs into the string */
  2309.  
  2310.         if ((pp = malloc((int)strlen(line) + 1))) {
  2311.             strcpy(pp,line);            /* safe */
  2312.             xwords(pp,5,info,0);        /* Parse it the old way */
  2313.  
  2314. #ifdef LUDEBUG
  2315. if (zz) printf("LUDIAL 3 s[%s]\n",s);
  2316. #endif /* LUDEBUG */
  2317.  
  2318.             if (!info[1])
  2319.               continue;
  2320.             if (*info[1] == ';') {      /* If full-line comment, */
  2321.                 newdir = 1;             /* (only new directories have them) */
  2322.                 continue;               /* keep reading. */
  2323.             }
  2324.             if (!info[2])
  2325.               continue;
  2326.             if (*info[2] == '+')
  2327.               newentry = 1;
  2328.             if (info[4]) {
  2329.                 if ((*info[4] == '=') ||
  2330.                     !ckstrcmp(info[4],"none", 4,0) ||
  2331.                     !ckstrcmp(info[4],"even", 4,0) ||
  2332.                     !ckstrcmp(info[4],"space",5,0) ||
  2333.                     !ckstrcmp(info[4],"mark", 4,0) ||
  2334.                     !ckstrcmp(info[4],"odd",  3,0)
  2335.                     )
  2336.                   oldentry = 1;
  2337.             }
  2338.         }
  2339.         if (pp) {
  2340.             free(pp);
  2341.             pp = NULL;
  2342.         }
  2343.  
  2344.         /* Check consistency */
  2345.  
  2346.         if ((oldentry || olddir) && (newentry || newdir)) {
  2347.             printf(
  2348. "\nERROR: You seem to have old- and new-format entries mixed in your\n");
  2349.             printf(
  2350. "dialing directory.  You'll have to edit it by hand to convert it to the\n");
  2351. #ifndef NOHELP
  2352.             printf("new format.  Type HELP DIAL for further information.\n\n");
  2353. #else
  2354.             printf("new format.\n\n");
  2355. #endif /* NOHELP */
  2356.             if (line) {
  2357.                 free(line);
  2358.                 line = NULL;
  2359.             }
  2360.             return(-1);
  2361.         }
  2362.         if (!olddir && oldentry) {
  2363.             int convert = 0;
  2364.             olddir = 1;
  2365.             if (dialcvt == 2) {         /* 2 == ASK */
  2366.                 sprintf(tmpbuf,
  2367. "WARNING: Old-style dialing directory detected:\n%s", line);
  2368.         convert = uq_ok(tmpbuf,
  2369.                 "Shall I convert it for you? ",3,NULL,0);
  2370.             } else
  2371.               convert = dialcvt;
  2372.             if (convert) {
  2373.                 debug(F111,"ludial calling ddcvt",dialdir[dd-1],dd);
  2374.                 if (ddcvt(dialdir[dd-1],f,dd-1) < 0) {
  2375.                     debug(F111,"ludial ddcvt failed",dialdir[dd-1],dd);
  2376.                     oldflg = 1;
  2377.                     printf(
  2378. "  Sorry, can't convert.");
  2379.                     printf(
  2380. "  Will ignore speed and parity fields, continuing...\n\n");
  2381.                 } else {
  2382.                     olddir = newdir = 0;
  2383.                     debug(F111,"ludial ddcvt ok",dialdir[dd-1],dd);
  2384.                 }
  2385.                 dd--;
  2386.                 f = NULL;
  2387.                 continue;
  2388.             } else {
  2389.                 if (dialcvt == 2)
  2390.                   printf(
  2391. "  OK, will ignore speed and parity fields, continuing...\n\n");
  2392.                 olddir = 1;
  2393.             }
  2394.         }
  2395.  
  2396. #ifdef LUDEBUG
  2397. if (zz) printf("LUDIAL XX s[%s], n1=%d\n",s,n1);
  2398. #endif /* LUDEBUG */
  2399.  
  2400.         /* Now parse again for real */
  2401.  
  2402.         if (oldentry)                   /* Parse it the old way */
  2403.           xwords(line,5,info,0);
  2404.         else                            /* Parse it the new way */
  2405.           xwords(line,2,info,1);
  2406.  
  2407. #ifdef LUDEBUG
  2408. if (zz) printf("LUDIAL YY s[%s], n1=%d\n",s,n1);
  2409. if (zz) printf("%s [%s]\n",info[1],info[2]);
  2410. #endif /* LUDEBUG */
  2411.  
  2412.         if (info[1]) {                  /* First word is entry name */
  2413.             if ((n3 = (int) strlen(info[1])) < 1) /* Its length */
  2414.               continue;                 /* If no first word, keep reading. */
  2415.             if (n3 < n1)                /* Search name is longer */
  2416.               continue;                 /* Can't possibly match */
  2417.             if (ambiguous && n3 != n1)
  2418.               continue;
  2419.  
  2420. #ifdef LUDEBUG
  2421. if (zz) printf("MATCHING: [%s] [%s], n1=%d\n",s,info[1],n1);
  2422. #endif /* LUDEBUG */
  2423.  
  2424.             if (ckstrcmp(s,info[1],n1,0)) /* Caseless string comparison */
  2425.               continue;
  2426.  
  2427. #ifdef LUDEBUG
  2428. if (zz) printf("MATCH OK: [%s] [%s], n1=%d\n",s,info[1],n1);
  2429. #endif /* LUDEBUG */
  2430.  
  2431.             if (!info[2])               /* No phone number given */
  2432.               continue;
  2433.             if ((n2 = (int) strlen(info[2])) < 1) /* Length of phone number */
  2434.               continue;                 /* Ignore empty phone numbers */
  2435.  
  2436.             /* Got one */
  2437.  
  2438.             if (!(pp = (char *)malloc(n2 + 1))) { /* Allocate storage for it */
  2439.                 printf("?internal error - ludial malloc 1\n");
  2440.                 if (line) {
  2441.                     free(line);
  2442.                     line = NULL;
  2443.                 }
  2444.                 dncount = 0;
  2445.                 return(-1);
  2446.             }
  2447.             strcpy(pp,info[2]);         /* safe */
  2448.  
  2449.             if (dncount > MAXDNUMS) {
  2450.                 printf("Warning: %d matches found, %d max\n",
  2451.                        dncount,
  2452.                        MAXDNUMS
  2453.                        );
  2454.                 dncount = MAXDNUMS;
  2455.                 break;
  2456.             }
  2457.             dn_p[dncount++] = pp;       /* Add pointer to array. */
  2458.             if (dncount == 1) {         /* First one... */
  2459.                 if (d_name) free(d_name);
  2460.                 if (!(d_name = (char *)malloc(n3 + 1))) { /* Save its name */
  2461.                     printf("?internal error - ludial malloc 2\n");
  2462.                     if (line) {
  2463.                         free(line);
  2464.                         line = NULL;
  2465.                     }
  2466.                     dncount = 0;
  2467.                     return(-1);
  2468.                 }
  2469.                 t = n3;                 /* And its length */
  2470.                 strcpy(d_name,info[1]); /* safe */
  2471.             } else {                    /* Second or subsequent one */
  2472.  
  2473. #ifdef LUDEBUG
  2474.                 if (zz)
  2475.                   printf("d_name=[%s],info[1]=%s,t=[%d]\n",d_name,info[1],t);
  2476. #endif /* LUDEBUG */
  2477.  
  2478.                 if ((int) strlen(info[1]) == t) /* Lengths compare */
  2479.                   if (!ckstrcmp(d_name,info[1],t,0)) /* Caseless compare OK */
  2480.                     continue;
  2481.  
  2482.                 /* Name given by user matches entries with different names */
  2483.  
  2484.                 if (ambiguous)          /* Been here before */
  2485.                   break;
  2486.  
  2487.                 ambiguous = 1;          /* Now an exact match is required */
  2488.                 for (j = 0; j < dncount; j++) { /* Clean out previous list */
  2489.                     if (dn_p[j]) {
  2490.                         free(dn_p[j]);
  2491.                         dn_p[j] = NULL;
  2492.                     }
  2493.                 }
  2494.                 pass++;                 /* Second pass... */
  2495.                 goto lu_again;          /* Do it all over again. */
  2496.             }
  2497.         }
  2498.     }
  2499.     if (line) free(line);
  2500.     if (dncount == 0 && ambiguous) {
  2501.         printf(" Lookup: \"%s\" - ambiguous%s\n",
  2502.                s,
  2503.                cx == XXLOOK ? "" : " - dialing skipped"
  2504.                );
  2505.         return(-2);
  2506.     }
  2507.     return(dncount);
  2508. }
  2509.  
  2510. char *
  2511. pncvt(s) char *s; {                     /* Phone number conversion */
  2512.     char *p = NULL;                     /* (just a wrapper for dncvt() */
  2513.     char *q = NULL;
  2514.     static char pnbuf[128];
  2515.     makestr(&p,dn_p[0]);                /* Save these in case they are */
  2516.     makestr(&q,dn_p2[0]);               /* being used */
  2517.     makestr(&dn_p[0],s);                /* Copy the argument string to here */
  2518.     dncvt(0,XXLOOK,1,1);                /* Convert it */
  2519.     if (!dn_p2[0])                      /* Put result where can return it */
  2520.       pnbuf[0] = NUL;
  2521.     else
  2522.       ckstrncpy(pnbuf,dn_p2[0],127);
  2523.     makestr(&dn_p[0],p);                /* Restore these */
  2524.     makestr(&dn_p2[0],q);
  2525.     makestr(&p,NULL);                   /* Free these */
  2526.     makestr(&q,NULL);
  2527.     return((char *)pnbuf);
  2528. }
  2529.  
  2530. int
  2531. dodial(cx) int cx; {                    /* DIAL or REDIAL */
  2532.     int i = 0, x = 0;                   /* Workers */
  2533.     int sparity = -1;                   /* For saving global parity value */
  2534.     int previous = 0;
  2535.     int len = 0;
  2536.     int literal = 0;
  2537.     int flowsave;
  2538.     int lufound = 0;                    /* Did any lookup succeed? */
  2539.     int prefix = 1;
  2540.     int postfix = 1;
  2541.     int wasalpha = 0;
  2542.     int xredial = 0;
  2543.     int braces = 0;
  2544.  
  2545.     char *p = NULL, *s3 = NULL, * sav = NULL;
  2546.     int j = 0, t = 0, n = 0;
  2547.     int xretries, xlcc;
  2548.  
  2549.     debug(F101,"dodial cx","",cx);
  2550.     debug(F111,"dodial diallcc",diallcc,diallcc);
  2551.  
  2552.     xretries = dialrtr;                 /* If retries not set, */
  2553.     if (diallcc) {                      /* choose default based on */
  2554.         xlcc = atoi(diallcc);           /* local country code. */
  2555.         if (xretries < 0) {
  2556.             switch (xlcc) {
  2557.               case 1: xretries = 10; break; /* No restrictions in NANP */
  2558.                 /* Add other country codes here */
  2559.                 /* that are known to have no restrictions on redialing. */
  2560.               default: xretries = 1;
  2561.             }
  2562.         }
  2563.     }
  2564.     if (cx == XXPDIA) {                 /* Shortcut... */
  2565.         cx = XXDIAL;
  2566.         partial = 1;
  2567.         debug(F100,"PDIAL sets partial=1","",0);
  2568.         postfix = 0;                    /* Do not add postfix */
  2569.     } else {
  2570.         partial = 0;
  2571.         debug(F100,"DIAL sets partial=0","",0);
  2572.     }
  2573.     previous = dialsta;                 /* Status of previous call, if any */
  2574.     if (previous == DIA_PART) {
  2575.         prefix = 0;                     /* do not add prefix */
  2576.     }
  2577.     s = NULL;                           /* Initialize user's dial string */
  2578.     if (cx == XXRED) {                  /* REDIAL or... */
  2579.         if ((y = cmcfm()) < 0)
  2580.           return(y);
  2581.     } else if (cx == XXANSW) {          /* ANSWER or ... */
  2582.         if ((y = cmnum("timeout (seconds)","0",10,&x,xxstring)) < 0)
  2583.           return(y);
  2584.         dialatmo = x;
  2585.         if ((y = cmcfm()) < 0)
  2586.           return(y);
  2587.     } else {                            /* DIAL or LOOKUP */
  2588.         if (ndialdir > 0)
  2589.           s3 = "Number to dial or entry from dial directory";
  2590.         else
  2591.           s3 = "Number to dial";
  2592.         if ((x = cmtxt(s3, dialnum ? dialnum : "",&s,xxstring)) < 0)
  2593.           return(x);
  2594.         if (s) {
  2595.             len = (int) strlen(s);
  2596.             ckstrncpy(tmpbuf,s,TMPBUFSIZ); /* Save literal copy */
  2597. #ifdef COMMENT
  2598.             if (len > 1) {              /* Strip outer braces if given */
  2599.                 if (*s == '{') {
  2600.                     if (s[len-1] == '}') {
  2601.                         s[len-1] = NUL;
  2602.                         s++;
  2603.                         len -= 2;
  2604.                     }
  2605.                 }
  2606.             }
  2607. #else
  2608.             s = brstrip(s);             /* Strip outer braces or quotes */
  2609. #endif /* COMMENT */
  2610.         }
  2611.     }
  2612.  
  2613.     if (cx != XXLOOK) {                 /* Not LOOKUP */
  2614. #ifdef IKSD
  2615.         if (inserver) {
  2616.             printf("Sorry, dialing is disabled.\r\n");
  2617.             return(success = 0);
  2618.         }
  2619. #endif /* IKSD */
  2620. #ifdef CK_TAPI
  2621.         if (tttapi && !tapipass) {
  2622.           ;                             /* Skip the modem test if TAPI */
  2623.         } else
  2624. #endif /* CK_TAPI */
  2625.         if (mdmtyp < 1 && !dialtest) {
  2626.             if (network
  2627. #ifdef TN_COMPORT
  2628.                  && !istncomport()
  2629. #endif /* TN_COMPORT */
  2630.                  )
  2631.               printf("Please SET HOST first, and then SET MODEM TYPE\n");
  2632.             else
  2633.               printf("Sorry, you must SET MODEM TYPE first\n");
  2634.             dialsta = DIA_NOMO;
  2635.             return(success = 0);
  2636.         }
  2637.         if (!local && !dialtest) {
  2638.             printf("Sorry, you must SET %s or SET HOST first\n",
  2639. #ifdef OS2
  2640.                    "PORT"
  2641. #else
  2642.                    "LINE"
  2643. #endif /* OS2 */
  2644.                    );
  2645.             dialsta = DIA_NOLI;
  2646.             return(success = 0);
  2647.         }
  2648.         if ((!network 
  2649. #ifdef TN_COMPORT
  2650.               || istncomport()
  2651. #endif /* TN_COMPORT */
  2652.               ) && !dialtest &&
  2653. #ifdef CK_TAPI
  2654.              !tttapi &&
  2655. #endif /* CK_TAPI */
  2656.             (speed < 0L)
  2657. #ifdef UNIX
  2658.             && (strcmp(ttname,"/dev/null"))
  2659. #else
  2660. #ifdef OSK
  2661.             && (strcmp(ttname,"/nil"))
  2662. #endif /* OSK */
  2663. #endif /* UNIX */
  2664.             ) {
  2665.             printf("\nSorry, you must SET SPEED first\n");
  2666.             dialsta = DIA_NOSP;
  2667.             return(success = 0);
  2668.         }
  2669.     }
  2670.     if (cx != XXANSW) {
  2671.         for (j = 0; j < MAXDNUMS; j++) { /* Initialize dial-number list */
  2672.             if (!dialnum) {             /* First time dialing */
  2673.                 dn_p[j] = NULL;         /* initialize all pointers. */
  2674.                 dn_p2[j] = NULL;
  2675.             } else if (dn_p[j]) {       /* Not the first time, */
  2676.                 free(dn_p[j]);          /* free previous, if any, */
  2677.                 dn_p[j] = NULL;         /* then set to NULL. */
  2678.                 if (dn_p2[j])
  2679.                   free(dn_p2[j]);
  2680.                 dn_p2[j] = NULL;
  2681.             } else break;               /* Already NULL */
  2682.         }
  2683.         if (len == 0)
  2684.           s = NULL;
  2685.         if (!s)
  2686.           s = dialnum;
  2687.         if (!s) {
  2688.             if (cx == XXLOOK)
  2689.               printf("?Lookup what?\n");
  2690.             else
  2691.               printf("%s\n", (cx == XXRED) ?
  2692.                    "?No DIAL command given yet" :
  2693.                    "?You must specify a number to dial"
  2694.                    );
  2695.             return(-9);
  2696.         }
  2697.  
  2698.     /* Now we have the "raw" dial or lookup string and s is not NULL */
  2699.  
  2700.         makestr(&dscopy,s);             /* Put it in a safe place */
  2701.         s = dscopy;
  2702.         n = 0;
  2703.  
  2704.         debug(F111,"dodial",s,ndialdir);
  2705.  
  2706.         wasalpha = 0;
  2707.         if (isalpha(*s)) {
  2708.             wasalpha = 1;
  2709.             if (ndialdir > 0) {         /* Do we have a dialing directory? */
  2710.                 n = ludial(s,cx);       /* Look up what the user typed */
  2711.                 if (n == 0)
  2712.                   printf(" Lookup: \"%s\" - not found%s\n",
  2713.                          s,
  2714.                          cx == XXLOOK ? "" : " - dialing as given\n"
  2715.                          );
  2716.             }
  2717.             debug(F101,"dodial",s,n);
  2718.             if (n < 0 && cx != XXLOOK) { /* Error out if they wanted to dial */
  2719.                 if (n == -1)            /* -2 means ludial already gave msg */
  2720.                   printf(" Lookup: fatal error - dialing skipped\n");
  2721.                 dialsta = DIA_DIR;
  2722.                 return(-9);
  2723.             }
  2724.             if (n > 0)                  /* A successful lookup */
  2725.               lufound = 1;
  2726.         } else if (*s == '=') {         /* If number starts with = sign */
  2727.             s++;                        /* strip it */
  2728.             literal = 1;                /* remember this */
  2729.             while (*s == SP) s++;       /* and then also any leading spaces */
  2730.         } else if (tmpbuf[0] == '{' && tmpbuf[1] == '{') {
  2731.             makelist(tmpbuf,dn_p,MAXDNUMS);
  2732.             makestr(&dscopy,tmpbuf);
  2733.             s = tmpbuf;
  2734.             for (n = 0; n < MAXDNUMS; n++) /* (have to count how many) */
  2735.               if (!dn_p[n]) break;
  2736.             braces = 1;
  2737.         }
  2738.         if (cx == XXLOOK && !wasalpha && !braces) {
  2739.             /* We've been told to lookup a number or a quoted name */
  2740.             char *p;
  2741.             n = 0;
  2742.             p = literal ? s : pncvt(dscopy);
  2743.             if (!p) p = "";
  2744.             if (*p) {
  2745.                 printf("%s  => %s\n", dscopy, p);
  2746.                 return(success = 1);
  2747.             } else {
  2748.                 printf("?Bad phone number\n");
  2749.                 return(success = 0);
  2750.             }
  2751.         }
  2752.         /* Save DIAL or successful LOOKUP string for future DIAL or REDIAL */
  2753.         /* But don't save pieces of partial dial ... */
  2754.  
  2755.         debug(F101,"DIAL save dialnum partial","",partial);
  2756.         debug(F101,"DIAL save dialnum previous","",previous);
  2757.         if ((cx == XXDIAL && partial == 0 && previous != DIA_PART) ||
  2758.             (cx == XXLOOK && n > 0)) {
  2759.             makestr(&dialnum,dscopy);
  2760.             if (!quiet && dscopy && !dialnum)
  2761.               printf("WARNING - memory allocation failure: redial number\n");
  2762.         }
  2763.         if (n > 0) {
  2764.             if (!quiet && !backgrd && !braces /* && dialdpy */ ) {
  2765.                 if (!strcmp(d_name,s))
  2766.                   printf(" Lookup: \"%s\" - exact match\n",s);
  2767.                 else
  2768.                   printf(" Lookup: \"%s\" - uniquely matches \"%s\"\n",
  2769.                          s,
  2770.                          d_name
  2771.                          );
  2772.             }
  2773.             if ((cx == XXLOOK) ||
  2774.                 ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2775.                 printf(" %d telephone number%sfound for \"%s\"%s\n",
  2776.                        n,
  2777.                        (n == 1) ? " " : "s ",
  2778.                        s,
  2779.                        (n > 0) ? ":" : "."
  2780.                        );
  2781.                 s3 = getdname();
  2782.             }
  2783.             for (i = 0; i < n; i++) {   /* Convert */
  2784.                 dn_x[i] = -1;
  2785.                 if (dncvt(i,cx,prefix,postfix) < 0) {
  2786.                     if (cx != XXLOOK) {
  2787.                         dialsta = DIA_DIR;
  2788.                         return(-9);
  2789.                     }
  2790.                 }
  2791.             }
  2792.             if (dialsrt && n > 1) {     /* Sort into optimal order */
  2793.                 for (i = 0; i < n-1; i++) {
  2794.                     for (j = i+1; j < n; j++) {
  2795.                         if (dn_x[j] < dn_x[i]) {
  2796.                             t = dn_x[j];
  2797.                             dn_x[j] = dn_x[i];
  2798.                             dn_x[i] = t;
  2799.                             p = dn_p[j];
  2800.                             dn_p[j] = dn_p[i];
  2801.                             dn_p[i] = p;
  2802.                             p = dn_p2[j];
  2803.                             dn_p2[j] = dn_p2[i];
  2804.                             dn_p2[i] = p;
  2805.                         }
  2806.                     }
  2807.                 }
  2808.             }
  2809.             if ((cx == XXLOOK) ||
  2810.                 ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2811.                 int nn = n;
  2812. #ifndef NOSPL
  2813.                 char * p;
  2814. #endif /* NOSPL */
  2815.                 if (cx != XXLOOK)
  2816.                   if (n > 12) nn = 12;
  2817.                 for (i = 0; i < nn; i++) {
  2818.                     printf("%3d. %-12s  %-20s =>  %-20s  (%d)\n",i+1,
  2819.                            s3, dn_p[i],
  2820.                            dn_p2[i] ? dn_p2[i] : "(processing failed)",
  2821.                            dn_x[i]
  2822.                            );
  2823.                 }
  2824.                 if (cx != XXLOOK && n != nn)
  2825.                   printf("And %d more...\n", n - nn);
  2826.             }
  2827.         } else if (n == 0) {            /* Not found in directory */
  2828.             makestr(&(dn_p[0]),literal ? s : dscopy);
  2829.             makestr(&d_name,literal ? s : dscopy);
  2830.             dncount = 1;
  2831.             n = 1;
  2832.             if (dncvt(0,cx,prefix,postfix) < 0) { /* In case they typed a */
  2833.                 dialsta = DIA_DIR;      /* portable-format number ... */
  2834.                 return(-9);
  2835.             }
  2836.         }
  2837.  
  2838. #ifndef NONET
  2839. #ifdef NETCONN
  2840.         /* It's not good that the networks directory depends on NOT-NODIAL.. */
  2841.         if (cx == XXLOOK && dscopy) {   /* Networks here too... */
  2842.             extern char *nh_p[], *nh_p2[], *n_name;
  2843.             extern char *nh_px[4][MAXDNUMS+1];
  2844.             n = -1;
  2845.             if (nnetdir > 0) {          /* Do we have a network directory? */
  2846.                 dirline = 0;
  2847.                 n = lunet(dscopy);      /* Look up what the user typed */
  2848.             }
  2849.             if (n > -1) {
  2850.                 int k;
  2851.                 if (n > 0)              /* A successful lookup */
  2852.                   lufound = 1;
  2853.                 if (cx == XXLOOK && n == 0)
  2854.                   printf(" Lookup: \"%s\" - not found\n",dscopy);
  2855.                 else
  2856.                   printf("%s %d network entr%s found for \"%s\"%s\n",
  2857.                          cx == XXLOOK ? " Lookup:" : "",
  2858.                          n,
  2859.                          (n == 1) ? "y" : "ies",
  2860.                          dscopy,
  2861.                          (n > 0) ? ":" : "."
  2862.                          );
  2863.  
  2864.                 for (i = 0; i < n; i++) {
  2865.  
  2866.                     printf("%3d. %-12s => %-9s %s",
  2867.                            i+1,n_name,nh_p2[i],nh_p[i]);
  2868.                     for (k = 0; k < 4; k++) {
  2869.                         if (nh_px[k][i]) {
  2870.                             printf(" %s",nh_px[k][i]);
  2871.                         } else
  2872.                           break;
  2873.                     }
  2874.                     printf("\n");
  2875.                 }
  2876.             }
  2877.         }
  2878. #endif /* NETCONN */
  2879. #endif /* NONET */
  2880.         if (cx == XXLOOK)
  2881.           return(success = lufound);
  2882.     } /* cx != XXANSW */
  2883.  
  2884. #ifdef VMS
  2885.     conres();                   /* So Ctrl-C/Y will work */
  2886. #endif /* VMS */
  2887. /*
  2888.   Some modems do not react well to parity.  Also, if we are dialing through a
  2889.   TCP/IP TELNET modem server, parity can be fatally misinterpreted as TELNET
  2890.   negotiations.
  2891.  
  2892.   This should work even if the user interrupts the DIAL command, because the
  2893.   DIAL module has its own interrupt handler.  BUT... if, for some reason, a
  2894.   dialing device actually *requires* parity (e.g. CCITT V.25bis says that even
  2895.   parity should be used), this might prevent successful dialing.  For that
  2896.   reason, we don't do this for V.25bis modems.
  2897. */
  2898.     sparity = parity;                   /* Save current parity */
  2899.     if ((dialcapas & CKD_V25) == 0)     /* If not V.25bis...  */
  2900.       parity = 0;                       /* Set parity to NONE */
  2901.  
  2902.     flowsave = flow;
  2903. /*
  2904.   These modems use some kind of screwy flow control while in command mode,
  2905.   and do not present CTS as they should.  So if RTS/CTS is set (or even if
  2906.   it isn't) disable flow control during dialing.
  2907. */
  2908. #ifndef MINIDIAL
  2909.     if (mdmtyp == n_ATT1910 || mdmtyp == n_ATT1900) {
  2910.         flow = FLO_NONE;                /* This is not enough */
  2911. #ifdef CK_TTSETFLOW
  2912.         ttsetflow(FLO_NONE);            /* Really turn it off */
  2913. #endif /* CK_TTSETFLOW */
  2914.     }
  2915. #endif /* MINIDIAL */
  2916.     if (!network
  2917. #ifdef TN_COMPORT
  2918.         || istncomport()
  2919. #endif /* TN_COMPORT */
  2920.          ) {
  2921.         int x;
  2922.         if ((x = ttgmdm()) > -1) {
  2923.             if (!x && msgflg) {
  2924.                 printf(
  2925. "WARNING - No modem signals detected.  Is your modem turned on?  If not,\n\
  2926. use Ctrl-C to interrupt dialing, turn on your modem, then %s.\n",
  2927.                        cx == XXANSW ?
  2928.                        "ANSWER again" :
  2929.                        "REDIAL"
  2930.                        );
  2931.             }
  2932.             if (flow == FLO_RTSC) {
  2933.                 if (!(x & BM_CTS)) {
  2934.                     if (msgflg)
  2935.                       printf(
  2936. "WARNING - SET FLOW RTS/CTS is in effect but modem's CTS signal is off.\n\
  2937. Disabling flow control temporarily %s...\n",
  2938.                              cx == XXANSW ?
  2939.                              "while waiting for call" :
  2940.                              "during dialing"
  2941.                              );
  2942.                     flow = FLO_NONE;
  2943.                 }
  2944.             }
  2945.         }
  2946.     }
  2947.     if (cx == XXANSW) {                 /* ANSWER */
  2948.         success = ckdial("",0,0,1,0);
  2949.         goto dialfin;
  2950.     }
  2951.  
  2952. /* Edit 192 adds the ability to dial repeatedly. */
  2953.  
  2954.     i = 0;
  2955.     dialcount = 0;
  2956.     do {
  2957.         if (i > 0) printf("\nDial attempt %d of %d...\n", i+1, xretries);
  2958.         dialcount = i+1;
  2959.         success = 0;
  2960.         /* And the ability to dial alternate numbers. */
  2961.         /* Loop to dial each in a list of numbers for the same name... */
  2962.         for (j = 0; j < n && !success; j++) { /* until one answers. */
  2963.             s = dn_p2[j];               /* Next number in list */
  2964.             if (dn_x[j] >= dialrstr) {  /* Dial restriction */
  2965.                 printf("Restricted: %s, skipping...\n",dn_p[j]);
  2966.                 continue;
  2967.             }
  2968.             xredial = (i == 0 && j == 0) ? 0 : 1;
  2969.             if (!s) s = dn_p[j];
  2970.  
  2971. #ifndef NOSPL
  2972.             sav = s;
  2973.             p = xdial(s);               /* Apply DIAL macro now */
  2974.             if (p) s = p;
  2975. #endif /* NOSPL */
  2976.  
  2977.         /* Dial confirmation */
  2978.         /* NOTE: the uq_xxx() calls allow for a GUI dialog */
  2979.  
  2980.             if (i == 0 && dialcnf) {
  2981.         char msgbuf[128];
  2982.         ckmakmsg(msgbuf,128,"Dialing ",s,NULL,NULL);
  2983.         x = uq_ok(msgbuf,"Is this number correct? ",3,NULL,0);
  2984.                 if (!x) {
  2985.  
  2986. #ifndef COMMENT
  2987.             x = uq_txt(        /* Allow GUI dialog */
  2988. #ifdef OS2
  2989. " Please enter the correct number,\r\n or press Enter to skip.",
  2990. #else
  2991. " Please enter the correct number,\r\n or press Return to skip.",
  2992. #endif /* OS2 */
  2993.                               "Corrected phone number: ",
  2994.                                1,
  2995.                    NULL,
  2996.                    atmbuf,
  2997.                    ATMBL,
  2998.                    s);
  2999.             if (x && atmbuf[0]) { /* They gave a new one */
  3000.             s = atmbuf;
  3001.             makestr(&(dn_p2[j]), s);
  3002.             }            
  3003.  
  3004. #else  /* COMMENT */
  3005.  
  3006. #ifdef CK_RECALL
  3007.                     extern int on_recall;
  3008. #endif /* CK_RECALL */
  3009.                     cmsavp(psave,PROMPTL);
  3010.                     cmsetp(
  3011. #ifdef OS2
  3012. " Please enter the correct number,\r\n or press Enter to skip: "
  3013. #else
  3014. " Please enter the correct number,\r\n or press Return to skip: "
  3015. #endif /* OS2 */
  3016.                            );
  3017.                     cmini(ckxech);
  3018.                     x = -1;
  3019.                     if (pflag) prompt(NULL);
  3020. #ifdef CK_RECALL
  3021.                     on_recall = 0;
  3022. #endif /* CK_RECALL */
  3023.                     y = cmdgquo();
  3024.                     cmdsquo(0);
  3025.                     while (x < 0) {
  3026.                         x = cmtxt("Corrected phone number","",&s,NULL);
  3027.                         cmres();
  3028.                     }
  3029.                     if ((int) strlen(s) < 1) {
  3030.                         cmsetp(psave);
  3031.                         continue;
  3032.                     }
  3033.                     makestr(&(dn_p2[j]), s);
  3034.                     cmdsquo(y);
  3035.                     cmsetp(psave);
  3036. #endif /* COMMENT */
  3037.                 }
  3038.             }
  3039.             if (dialtest) {             /* Just testing */
  3040.                 if (i + j == 0)
  3041.                   printf("\nTESTING...\n");
  3042.                 if (dialmac)
  3043.                   printf(" Number: \"%s\" => \"%s\"\n",sav,s);
  3044.                 else
  3045.                   printf(" Number: \"%s\"\n",s);
  3046.                 dialsta = DIA_BUSY;
  3047.                 success = 0;
  3048.             } else {
  3049.                 what |= W_DIALING;
  3050.                 success = ckdial(s,i,j,partial ? 3 : 0, xredial); /* Dial it */
  3051.                 what &= ~(W_DIALING);
  3052.                 if (!success) {
  3053.                     if (dialsta < 8 ||  /* Break out if unrecoverable error */
  3054.                         dialsta  == DIA_INTR ||
  3055.                         dialsta  == DIA_ERR  ||
  3056.                         previous == DIA_PART
  3057.                         )
  3058.                       break;
  3059.                 }
  3060.             }
  3061.         }
  3062.         if (success)                    /* Succeeded, leave the outer loop */
  3063.           break;
  3064.         if (dialsta < 8 ||              /* Break out if unrecoverable error */
  3065.             dialsta == DIA_INTR ||      /* Interrupted */
  3066.             dialsta == DIA_NODT ||      /* No dialtone */
  3067.             dialsta == DIA_NOAC ||      /* Access forbidden */
  3068.             dialsta == DIA_BLCK ||      /* Blacklisted */
  3069.             dialsta == DIA_DIR  ||      /* Dialing directory error */
  3070.             dialsta == DIA_ERR  ||      /* Modem command error */
  3071.             previous == DIA_PART)
  3072.           break;
  3073.         if (++i >= xretries)            /* Break out if too many tries */
  3074.           break;
  3075.         if (!backgrd && !quiet) {
  3076.             if (dialint > 5)
  3077.               printf(
  3078. "\nWill redial in %d second%s- press any key to redial immediately.\n",
  3079.                      dialint,
  3080.                      dialint == 1 ? " " : "s "
  3081.                      );
  3082.             printf("Ctrl-C to cancel...\n");
  3083.         }
  3084.         x = dialint;                    /* Redial interval */
  3085.         while (x-- > 0) {
  3086.             if ((y = conchk()) > 0) {   /* Did they type something? */
  3087.                 while (y--) coninc(0);  /* Yes, absorb it */
  3088.                 break;                  /* And wake up */
  3089.             }
  3090.             sleep(1);                   /* No interrupt, sleep a sec */
  3091.         }
  3092.     } while (!success);
  3093.  
  3094.   dialfin:
  3095.  
  3096.     if (cx != XXLOOK) {
  3097.         if (!success)
  3098.           bleep((short) BP_FAIL);
  3099.         else if (!quiet)
  3100.           bleep((short) BP_NOTE);
  3101. #ifdef OS2
  3102.         setint();                       /* Fix OS/2 interrupts */
  3103. #endif /* OS2 */
  3104.         if (sparity > -1)
  3105.           parity = sparity;             /* Restore parity if we saved it */
  3106.         flow = flowsave;
  3107. #ifdef OS2
  3108.         ttres();                        /* Restore DIAL device */
  3109. #endif /* OS2 */
  3110. #ifdef VMS
  3111.         concb((char)escape);            /* Restore console */
  3112. #endif /* VMS */
  3113. #ifdef OS2
  3114.         {                               /* Set session title */
  3115.             char * p, name[72];         /* in window list. */
  3116.             char * q;
  3117.             if (cx == XXANSW) {
  3118.                 q = "Incoming call";
  3119.             } else {
  3120.                 if (d_name)
  3121.                   q = d_name;
  3122.                 else if (dialnum)
  3123.                   q = dialnum;
  3124.                 else if (ttname[0])
  3125.                   q = ttname;
  3126.                 else q = "";
  3127.             }
  3128.             p = name;
  3129.             if (success) {
  3130.                 strncpy(name,q,48);
  3131.                 while (*p) {            /* Uppercase it for emphasis. */
  3132.                     if (islower(*p))
  3133.                       *p = toupper(*p);
  3134.                     p++;
  3135.                 }
  3136.             } else
  3137.               name[0] = NUL ;
  3138.             os2settitle((char *) name, TRUE);
  3139.         }
  3140. #endif /* OS2 */
  3141.     }
  3142.     if (cx != XXLOOK) {
  3143.         if (success) {
  3144.             if (reliable == SET_AUTO) { /* It's not a reliable connection. */
  3145.                 reliable = SET_OFF;
  3146.                 debug(F101,"dodial reliable","",reliable);
  3147.             }
  3148.         } else {
  3149. #ifndef NOHINTS
  3150.             extern int hints;
  3151.             if (hints && !quiet && dialsta != 9) { /* 9 == User interrupted */
  3152.                 extern int dialmhu, dialhng, dialdpy;
  3153.                 extern char * dialmsg[];
  3154.                 printf("\n*************************\n");
  3155.                 printf("DIAL-class command failed.\n");
  3156.                 printf("Modem type:  %s\n", gmdmtyp());
  3157.                 printf("Device:      %s\n", ttname);
  3158.                 printf("Speed:       %ld\n", speed);
  3159.                 printf("Dial status: %d",dialsta);
  3160.                 if (dialsta < 35 && dialmsg[dialsta])
  3161.                   printf(" [%s]",dialmsg[dialsta]);
  3162.                 printf("\n");
  3163.                 if (dialsta == DIA_TIMO ||
  3164.                     dialsta == DIA_NRDY ||
  3165.                    (dialsta > 13 && dialsta != DIA_BUSY && dialsta != DIA_NOAN)
  3166.                     ) {
  3167.                     switch (dialsta) {
  3168.                       case DIA_TIMO:
  3169.                         printf(
  3170. " . SET DIAL TIMEOUT to a greater value and try again.\n"
  3171.                                );
  3172.                         break;
  3173.                       case DIA_NRSP:
  3174.                       case DIA_NRDY:
  3175.                       case DIA_NOIN:
  3176.                         printf(
  3177. " . Is the modem turned on?\n"
  3178.                                );
  3179.                         printf(
  3180. " . Are you using the right communication port?\n"
  3181.                                );
  3182.                         break;
  3183.                       case DIA_NODT:
  3184.                         printf(
  3185. " . Is the modem connected to the telephone line?\n"
  3186.                                );
  3187.                     }
  3188.                     if (mdmtyp == n_GENERIC) {
  3189.                         printf(
  3190. " . Please choose a specific modem type with SET MODEM TYPE and try again.\n"
  3191.                                );
  3192.                         printf(
  3193. "    SET MODEM TYPE ? to see the list of known modem types.\n"
  3194.                                );
  3195.                     } else {
  3196.                         printf(
  3197. " . Are you sure you have chosen the appropriate modem type?\n"
  3198.                                );
  3199.                     }
  3200.                     if (speed > 19200L) {
  3201.                         printf(
  3202. " . Maybe the interface speed (%ld) is too fast:\n", speed
  3203.                                );
  3204.                         printf(
  3205. "    SET SPEED to a lower speed and try again.\n"
  3206.                                );
  3207.                         printf(
  3208. "    SET SPEED ? to see the list of valid speeds.\n"
  3209.                                );
  3210.                     }
  3211.                     if (dialhng) {
  3212.                         if (dialmhu)
  3213.                           printf(
  3214. " . SET MODEM HANGUP-METHOD RS232 and try again.\n"
  3215.                                  );
  3216.                         else
  3217.                           printf(
  3218. " . SET MODEM HANGUP-METHOD MODEM-COMMAND and try again.\n"
  3219.                                  );
  3220.                         printf(
  3221. " . If that doesn't work, try again with SET DIAL HANGUP OFF.\n"
  3222.                                );
  3223.                     } else {
  3224.                         printf(
  3225. " . Give a HANGUP or SET DIAL HANGUP ON command and try again.\n"
  3226.                                );
  3227.                     }
  3228.                     if (!dialdpy)
  3229.                       printf(
  3230. " . Use SET DIAL DISPLAY ON to watch the dialog between Kermit and modem.\n"
  3231.                              );
  3232.                 }
  3233. #ifndef NOSHOW
  3234.                 printf(
  3235. " . SHOW COMMUNICATIONS, SHOW MODEM, SHOW DIAL to see current settings.\n"
  3236.                        );
  3237. #endif /* NOSHOW */
  3238.  
  3239. #ifndef NOHELP
  3240.                 printf(
  3241. " . HELP SET MODEM, HELP SET DIAL, and HELP DIAL for more information.\n"
  3242.                        );
  3243. #endif /* NOHELP */
  3244.                 printf("(Use SET HINTS OFF to suppress future hints.)\n");
  3245.                 printf("*************************\n\n");
  3246.             }
  3247. #endif /* NOHINTS */
  3248.         }
  3249.     }
  3250.     return(success);
  3251. }
  3252. #endif /* NODIAL */
  3253.  
  3254. /*  D O T Y P E  --  Type (display) a file with various options...  */
  3255.  
  3256. #ifdef BIGBUFOK
  3257. #define TYPBUFL 16384
  3258. #else
  3259. #define TYPBUFL 256
  3260. #endif /* BIGBUFOK */
  3261.  
  3262. int typ_lines = 0;                      /* \v(ty_ln) */
  3263. int typ_mtchs = 0;                      /* \v(ty_lm) */
  3264. static int typ_int = 0;                 /* Flag if TYPE interrupted */
  3265.  
  3266. #ifdef UNICODE
  3267. extern int fcharset, fileorder, byteorder, ucsorder;
  3268. #define TYPXBUFL TYPBUFL+TYPBUFL+TYPBUFL+4
  3269. static char * mp = NULL;
  3270. static char * mbuf = NULL;
  3271. static long xn = 0L;
  3272.  
  3273. static int
  3274. #ifdef CK_ANSIC
  3275. storechar(char c)
  3276. #else
  3277. storechar(c) char c;
  3278. #endif /* CK_ANSIC */
  3279. {
  3280.     if (!mp) return(-1);
  3281.     if (++xn > TYPXBUFL)
  3282.       return(-1);
  3283.     debug(F111,"storechar xn",ckitoa((int)c),xn);
  3284.     *mp++ = c;
  3285.     return(0);
  3286. }
  3287. #endif /* UNICODE */
  3288.  
  3289. static FILE * ofp = NULL;               /* For /OUTPUT: file */
  3290.  
  3291. static int
  3292. typeline(buf,len,outcs,ofp) char * buf; int len, outcs; FILE * ofp; {
  3293.     register int i;
  3294.  
  3295.     debug(F011,"typeline buf",buf,len);
  3296.     /* debug(F101,"typeline outcs","",outcs); */
  3297.  
  3298. #ifdef OS2
  3299. #ifndef NOLOCAL
  3300. #ifdef UNICODE
  3301.     /* In K95 only, the buffer is guaranteed to be in UCS-2 if outcs >= 0. */
  3302.     /* Len is its length in bytes.  There is no line terminator. */
  3303.     /* outcs is the file character-set number (FC_xxx) of the target set */
  3304.     /* that was requested by the user. */
  3305.     if (!inserver && !k95stdout) {
  3306.         extern int wherex[], wherey[];
  3307.         extern unsigned char colorcmd;
  3308.  
  3309.         VscrnWrtUCS2StrAtt( VCMD, (unsigned short *)buf, len/2,
  3310.                            wherey[VCMD], wherex[VCMD], &colorcmd);
  3311.         printf("\r\n");
  3312.         return(0);
  3313.     }
  3314. #endif /* UNICODE */
  3315. #endif /* NOLOCAL */
  3316. #endif /* OS2 */
  3317.  
  3318. /* In Unix, VMS, etc, the line has already been converted to the desired  */
  3319. /* character-set, if one was given.  OR... on all platforms, including in */
  3320. /* K95, we don't know the character set.  In either case we dump the line */
  3321. /* byte by byte in case it contains NULs (printf() would truncate). */
  3322.  
  3323. #ifdef COMMENT
  3324.     for (i = 0; i < len; i++)
  3325.       putchar(buf[i]);
  3326. #else
  3327.     for (i = 0; i < len; i++) {
  3328.         if (ofp == stdout) {
  3329.             putchar(buf[i]);
  3330.         } else {
  3331.             putc(buf[i],ofp);
  3332.         }
  3333.     }
  3334. #endif /* COMMENT */
  3335.  
  3336. #ifdef IKSD
  3337.     if (inserver) {
  3338. #ifdef UNICODE
  3339.         if (outcs == FC_UCS2) {
  3340.             if (ofp == stdout) {
  3341.                 putchar(NUL);
  3342.             } else {
  3343.                 putc(NUL,ofp);
  3344.             }
  3345.         }
  3346. #endif /* UNICODE */
  3347.         if (ofp == stdout) {
  3348.             putchar('\r');
  3349.         } else {
  3350.             putc('\r',ofp);
  3351.         }
  3352.     }
  3353. #endif /* IKSD */
  3354. #ifdef UNICODE
  3355.     if (outcs == FC_UCS2) {
  3356.         if (ofp == stdout) {
  3357.             putchar(NUL);
  3358.         } else {
  3359.             putc(NUL,ofp);
  3360.         }
  3361.     }
  3362. #endif /* UNICODE */
  3363.     if (ofp == stdout) {
  3364.         putchar('\n');
  3365.     } else {
  3366.         putc('\n',ofp);
  3367.     }
  3368.     fflush(stdout);
  3369.     return(0);
  3370. }
  3371.  
  3372. static int                              /* Get translated line */
  3373. typegetline(incs, outcs, buf, n) int incs, outcs, n; char * buf; {
  3374.     int x = 0, c0, c1, len = 0, count = 0, eof = 0, xlate = 0;
  3375. #ifdef UNICODE
  3376.     int xxn = -1;
  3377.     int yyn = -9;
  3378.     xn = 0L;
  3379.  
  3380. #ifdef DEBUG
  3381.     if (deblog && typ_lines == 0) {
  3382.         debug(F101,"typegetline incs","",incs);
  3383.         debug(F101,"typegetline outcs","",outcs);
  3384.         debug(F101,"typegetline feol","",feol);
  3385.         debug(F101,"typegetline byteorder","",byteorder);
  3386.         debug(F101,"typegetline ucsorder ","",ucsorder);
  3387.         debug(F111,"typegetline fileorder","1",fileorder);
  3388.     }
  3389. #endif /* DEBUG */
  3390.  
  3391.     if (incs < 0)                       /* Shouldn't happen */
  3392.       return(-2);
  3393.  
  3394.     if (outcs == -1)                    /* Can happen */
  3395.       outcs = incs;
  3396.  
  3397.     if (incs != outcs || incs == FC_UCS2) { /* See if we should translate */
  3398.         xlate = 1;
  3399.         if (!mbuf) {                    /* Allocate buffer if not allocated */
  3400.             mbuf = (char *)malloc(TYPXBUFL+1); /* yet */
  3401.             if (!mbuf) {
  3402.                 printf("WARNING: Translation buffer allocation failure.\n");
  3403.                 printf("Translation will be skipped...\n");
  3404.                 xlate = 0;
  3405.             }
  3406.         }
  3407.     }
  3408.     if (xlate) {                        /* Translating... */
  3409.         mp = mbuf;                      /* Reset working buffer pointer */
  3410. /*
  3411.   Here we call xgnbyte() in a loop, having it return UCS-2 bytes.  In K95, we
  3412.   use UCS-2 directly.  Elsewhere, we feed the UCS-2 bytes into xpnbyte() to
  3413.   convert them to the desired target character set.  But since we are using
  3414.   UCS-2, we have several sources for confusion: (1) xgnbyte() might return in
  3415.   LE or BE byte order, with no explicit indication of what the order is; but
  3416.   (2) xpnbyte() wants BE; but (3) Windows wants LE.
  3417. */
  3418.         while (1) {
  3419.             if (typ_int)                /* Quit if interrupted */
  3420.               return(0);
  3421.             c0 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3422.             debug(F000,"typegetline c0","",c0);
  3423.             if (c0 < 0) {               /* EOF */
  3424.                 eof++;
  3425.                 break;
  3426.             }
  3427.             c1 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3428.             debug(F000,"typegetline c1","",c1);
  3429.             if (c1 < 0) {               /* EOF */
  3430.                 eof++;
  3431.                 break;
  3432.             }
  3433. #ifdef DEBUG
  3434.             if (deblog && typ_lines == 0) {
  3435.                 if (count == 0) /* Check fileorder after BOM */
  3436.                   debug(F111,"typegetline fileorder","2",fileorder);
  3437.             }
  3438. #endif /* DEBUG */
  3439.  
  3440. #ifdef COMMENT
  3441. /* Now we have the two UCS-2 bytes.  Which order are they in? */
  3442.  
  3443.             if (fileorder > 0) {        /* Little Endian */
  3444.                 int t;                  /* So swap them */
  3445.                 debug(F100,"typegetline swapping","",0);
  3446.                 t = c1;
  3447.                 c1 = c0;
  3448.                 c0 = t;
  3449.             }
  3450. #endif /* COMMENT */
  3451.             if (c0 == 0 && c1 == 0x0D)  /* Now see if we have EOL */
  3452.               yyn = xn;
  3453.  
  3454.             if (c0 == 0 && c1 == 0x0A)  /* Now see if we have EOL */
  3455.               xxn = xn;
  3456.  
  3457.             count++;                    /* Count byte */
  3458.  
  3459. /* Give the two bytes to xpnbyte() in BE order */
  3460.  
  3461.             if ((x = xpnbyte(c0,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3462.             if ((x = xpnbyte(c1,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3463.  
  3464.             if (xxn > -1) {             /* Have end of line? */
  3465.                 xn = xxn;
  3466.                 if (yyn == xxn - 2)     /* Adjust for CRLF */
  3467.                   xn = yyn;
  3468.                 break;                  /* And break out of loop. */
  3469.             }
  3470.         }
  3471.         mbuf[xn] = NUL;
  3472.         if (xn > n)                     /* Can truncate here... */
  3473.           xn = n;
  3474.         memcpy(buf,mbuf,xn);
  3475.         debug(F011,"typegetline xlate",buf,xn);
  3476.         return((eof && (xn == 0)) ? -1 : xn);
  3477.     }
  3478. #endif /* UNICODE */
  3479. #ifdef COMMENT
  3480.     /* We can't use this because, stupidly, zsinl() doesn't return a length. */
  3481.     /* It could be changed but then we'd have to change all ck?fio.c modules */
  3482.     x = zsinl(ZIFILE,buf,n);
  3483. #else
  3484.     /* So instead, we copy zsinl() to here... */
  3485.     /* But note: This does not necessarily handle UCS-2 alignment properly;  */
  3486.     /* that's what the code in the first section of this routine is for. */
  3487.     /* But it does tolerate files that contain NULs. */
  3488.     {
  3489.         int a;
  3490.         char *s;
  3491.  
  3492.         s = buf;
  3493.         a = -1;                         /* Current character, none yet. */
  3494.         debug(F101,"typegetline zsinl simulation","",n);
  3495.         while (n--) {                   /* Up to given length */
  3496. #ifdef COMMENT
  3497.             int old = 0;
  3498.             if (feol)                   /* Previous character */
  3499.               old = a;
  3500. #endif /* COMMENT */
  3501.             if (zchin(ZIFILE,&a) < 0) { /* Read a character from the file */
  3502.                 debug(F101,"typegetline zchin fail","",count);
  3503.                 if (count == 0)
  3504.                   x = -1;               /* EOF or other error */
  3505.                 break;
  3506.             } else
  3507.               count++;
  3508.             if (feol) {                 /* Single-character line terminator */
  3509.                 if (a == feol)
  3510.                   break;
  3511.             } else {                    /* CRLF line terminator */
  3512. #ifdef COMMENT
  3513. /* Debug log shows that in Windows, <CR><LF> is returned as <LF>. */
  3514. /* Apparently we're not reading the file in binary mode. */
  3515.  
  3516.                 if (a == '\015')        /* CR, get next character */
  3517.                   continue;
  3518.                 if (old == '\015') {    /* Previous character was CR */
  3519.                     if (a == '\012') {  /* This one is LF, so we have a line */
  3520.                         break;
  3521.                     } else {            /* Not LF, deposit CR */
  3522.                         *s++ = '\015';
  3523.                         n--;
  3524.                         len++;
  3525.                     }
  3526.                 }
  3527. #else
  3528.                 if (a == LF) {
  3529.                     if (s[len] == CR) { /* This probably won't happen */
  3530.                         s[len] = NUL;
  3531.                         s--;
  3532.                         len--;
  3533.                     }
  3534.                     break;
  3535.                 }
  3536. #endif /* COMMENT */
  3537.             }
  3538.             *s = a;                     /* Deposit character */
  3539.             s++;
  3540.             len++;
  3541.         }
  3542.         *s = '\0';                      /* Terminate the string */
  3543.     }
  3544. #endif /* COMMENT */
  3545.     return(x < 0 ? -1 : len);
  3546. }
  3547.  
  3548.  
  3549. #ifndef MAC
  3550. SIGTYP
  3551. #ifdef CK_ANSIC
  3552. tytrap(int foo)                         /* TYPE interrupt trap */
  3553. #else
  3554. tytrap(foo) int foo;
  3555. #endif /* CK_ANSIC */
  3556. /* tytrap */ {
  3557. #ifdef __EMX__
  3558.     signal(SIGINT, SIG_ACK);
  3559. #endif
  3560.     debug(F100,"type tytrap SIGINT","",0);
  3561.     typ_int = 1;                        /* (Need arg for ANSI C) */
  3562.     SIGRETURN;
  3563. }
  3564. #endif /* MAC */
  3565.  
  3566. int
  3567. dotype(file, paging, first, head, pat, width, prefix, incs, outcs, outfile, z)
  3568.     char * file, * pat, * prefix; int paging, first, head, width, incs, outcs;
  3569.     char * outfile; int z;
  3570. /* dotype */ {
  3571.     extern long ffc;
  3572.     char buf[TYPBUFL+2];
  3573.     char * s = NULL;
  3574.     int rc = 1, lines = 0, ucs2 = 0;
  3575.     char ** tail = NULL;
  3576.     int * tlen = NULL;
  3577.     int tailing = 0, counting = 0;
  3578.     int x, c, n, i, j, k = 0;
  3579.     int number = 0, save, len, pfxlen = 0, evalpfx = 1;
  3580. #ifdef UNICODE
  3581.     int ucsbom_sav;
  3582.     extern int ucsbom;
  3583. #endif /* UNICODE */
  3584.  
  3585. #ifndef MAC
  3586. #ifdef OS2
  3587. #ifdef NT
  3588.     SIGTYP (* oldsig)(int);             /* For saving old interrupt trap. */
  3589. #else /* NT */
  3590.     SIGTYP (* volatile oldsig)(int);
  3591. #endif /* NT */
  3592. #else /* OS2 */
  3593.     SIGTYP (* oldsig)();
  3594. #endif /* OS2 */
  3595. #endif /* MAC */
  3596.  
  3597.     if (!file) file = "";
  3598.     if (!*file) return(-2);
  3599.  
  3600.     if (ofp != stdout) {                /* In case of previous interruption */
  3601.         if (ofp) fclose(ofp);
  3602.         ofp = stdout;
  3603.     }
  3604.     if (!outfile) outfile = "";
  3605.     if (outfile[0]) {
  3606.         ofp = fopen(outfile,"w");       /* Open output file */
  3607.         if (!ofp) {
  3608.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  3609.             ofp = stdout;
  3610.             return(-9);
  3611.         }
  3612.     }
  3613.     number = z;
  3614.     if (number && prefix) prefix = NULL;
  3615.  
  3616. #ifdef UNICODE
  3617.     ucsbom_sav = ucsbom;                /* We are not creating a file */
  3618.     ucsbom = 0;                         /* Do not use BOM bytes */
  3619. #endif /* UNICODE */
  3620.  
  3621.     typ_int = 0;
  3622.  
  3623.     save = binary;                      /* Save file type */
  3624.  
  3625.     debug(F101,"dotype incs","",incs);
  3626.     debug(F101,"dotype outcs","",outcs);
  3627.  
  3628. #ifdef UNICODE
  3629.     debug(F111,"dotype fileorder","A",fileorder);
  3630. #ifdef OS2
  3631.     if (!inserver && !k95stdout)
  3632.       outcs = FC_UCS2;
  3633. #endif /* OS2 */
  3634.  
  3635.     if (outcs == FC_UCS2)               /* Output is UCS-2? */
  3636.       ucs2 = 1;
  3637.     if (fileorder < 0)
  3638.       fileorder = ucsorder;
  3639.     debug(F111,"dotype fileorder","B",fileorder);
  3640. #endif /* UNICODE */
  3641.  
  3642. #ifdef CK_TTGWSIZ
  3643. #ifdef OS2
  3644.     ttgcwsz();
  3645. #else /* OS2 */
  3646.     /* Check whether window size changed */
  3647.     if (ttgwsiz() > 0) {
  3648.         if (tt_rows > 0 && tt_cols > 0) {
  3649.             cmd_rows = tt_rows;
  3650.             cmd_cols = tt_cols;
  3651.             debug(F101,"dotype cmd_rows","",cmd_rows);
  3652.             debug(F101,"dotype cmd_cols","",cmd_cols);
  3653.         }
  3654.     }
  3655. #endif /* OS2 */
  3656. #endif /* CK_TTGWSIZ */
  3657.  
  3658.     if (prefix)
  3659.       pfxlen = strlen(prefix);
  3660.  
  3661.     if (paging < 0) {                   /* Count only, don't print */
  3662.         counting = 1;
  3663.         prefix = NULL;
  3664.         width = 0;
  3665.         paging = 0;
  3666.     }
  3667.     if (ucs2)                           /* Crude... */
  3668.       width *= 2;
  3669.  
  3670. #ifdef OS2
  3671.     if (*file) {
  3672.         ckstrncpy(buf, file, TYPBUFL);  /* Change / to \. */
  3673.         p = buf;
  3674.         while (*p) {
  3675.             if (*p == '/') *p = '\\';
  3676.             p++;
  3677.         }
  3678.         file = buf;
  3679.     } else {
  3680.         rc = 0;
  3681.         goto xdotype;
  3682.     }
  3683. #endif /* OS2 */
  3684.  
  3685.     if (zchki(file) == -2) {            /* It's a directory */
  3686.         debug(F111,"dotype zchki failure",file,-2);
  3687.         if (xcmdsrc == 0) {
  3688.             printf("?Not a regular file: \"%s\"\n",file);
  3689.             rc = -9;
  3690.         } else
  3691.           rc = 0;
  3692.         goto xdotype;
  3693.     }
  3694.     if (!zopeni(ZIFILE, file)) {        /* Not a directory, open it */
  3695.         debug(F111,"dotype zopeni failure",file,0);
  3696.         if (xcmdsrc == 0) {
  3697.             printf("?Can't open file: \"%s\"\n",file);
  3698.             rc = -9;
  3699.         } else
  3700.           rc = 0;
  3701.         goto xdotype;
  3702.     }
  3703.  
  3704. #ifndef AMIGA
  3705. #ifndef MAC
  3706.     errno = 0;
  3707.     oldsig = signal(SIGINT, tytrap);    /* Save current interrupt trap. */
  3708.     debug(F111,"type SIGINT trap set",ckitoa(errno),oldsig);
  3709. #endif /* MAC */
  3710. #endif /* AMIGA */
  3711.  
  3712.     if (paging > -1)                    /* More-prompting */
  3713.       xaskmore = paging;
  3714.  
  3715.     binary = 0;
  3716.  
  3717.     if (head < 0) {                     /* "tail" was requested */
  3718.         tailing = 1;                    /* Set flag */
  3719.         head = 0 - head;                /* Get absolute number of lines */
  3720.         if (!counting) {
  3721.             tail = (char **) malloc(head * sizeof(char *)); /* Allocate list */
  3722.             if (!tail) {
  3723.                 printf("?Memory allocation failure\n");
  3724.                 goto xdotype;
  3725.  
  3726.             }
  3727.             tlen = (int *) malloc(head * sizeof(int));
  3728.             if (!tlen) {
  3729.                 printf("?Memory allocation failure\n");
  3730.                 goto xdotype;
  3731.  
  3732.             }
  3733.             for (i = 0; i < head; i++) { /* Initialize each pointer in list. */
  3734.                 tail[i] = NULL;
  3735.                 tlen[i] = 0;
  3736.             }
  3737.         }
  3738.     }
  3739.     typ_lines = 0;
  3740.     typ_mtchs = 0;
  3741.  
  3742. #ifdef UNICODE
  3743.     if (outcs > -1 && (incs != outcs || incs == FC_UCS2)) { /* Translating? */
  3744.         ffc = 0L;
  3745.         initxlate(incs,outcs);          /* Set up translation functions */
  3746.     } else
  3747. #endif /* UNICODE */
  3748.       outcs = -1;                       /* Means we don't know the charset */
  3749.  
  3750.     debug(F101,"dotype ffc","",ffc);
  3751.     debug(F101,"dotype outcs 2","",outcs);
  3752. #ifdef UNICODE
  3753.     debug(F111,"dotype fileorder","C",fileorder);
  3754. #endif /* UNICODE */
  3755.  
  3756.     /* Allow the buffer to contain NULs */
  3757.  
  3758.     for (n = first;
  3759.          (len = typegetline(incs,outcs,buf,TYPBUFL)) > -1;
  3760.          lines++
  3761.          ) {
  3762.         debug(F011,"dotype line",buf,len);
  3763. #ifndef MAC
  3764.         if (typ_int) {                  /* Interrupted? */
  3765.             typ_int = 0;
  3766.             debug(F101,"type interrupted line","",lines);
  3767.             printf("^C...\n");          /* Print message */
  3768.             if (ofp != stdout) {        /* Close any output file */
  3769.                 if (ofp) fclose(ofp);
  3770.                 ofp = stdout;
  3771.             }
  3772.             goto xxdotype;
  3773.         }
  3774. #endif /* MAC */
  3775.         typ_lines++;                    /* For \v(ty_ln) */
  3776.         if (pat)                        /* Matching? */
  3777.           if (!ckmatch(pat,buf,1,1+4))  /* Line matches pattern? */
  3778.             continue;                   /* No, skip it */
  3779.         typ_mtchs++;
  3780.  
  3781.         if (head > 0 && !tailing && lines == head) /* Handle /HEAD:n */
  3782.           break;
  3783.  
  3784.         buf[TYPBUFL+1] = NUL;           /* Just in case... */
  3785.         if (prefix) {                   /* Add specified prefix to each line */
  3786.             char pbuf[64];
  3787.             char * pp;
  3788.             pp = prefix;
  3789. #ifndef NOSPL
  3790.             if (evalpfx) {              /* Prefix is a variable? */
  3791.                 int n = 63;             /* Maybe - evaluate it and see */
  3792.                 char * p = pbuf;
  3793.                 zzstring(prefix,&p,&n); /* If there is no change */
  3794.                 if (!strcmp(prefix,pbuf)) { /* it's not a variable */
  3795.                     evalpfx = 0;        /* So don't do this again. */
  3796.                 } else {                /* It was a variable */
  3797.                     pp = pbuf;          /* So substitute its value */
  3798.                     pfxlen = 63 - n;    /* and get its new length */
  3799.                 }
  3800.             }
  3801. #endif /* NOSPL */
  3802.             if (len + pfxlen + 2 < TYPBUFL) {
  3803.                 /* Shift right to make room for prefix */
  3804.                 memcpy((char *)line+pfxlen,(char *)buf,len);
  3805.                 lset((char *)line,pp,pfxlen,SP);
  3806.                 debug(F110,"dotype prefix",line,pfxlen);
  3807.                 len += pfxlen;
  3808.                 memcpy((char *)buf,(char *)line,len);
  3809.             }
  3810.         } else if (number) {            /* Line numbers */
  3811.             int x;
  3812.             sprintf(line,"%4d. ",typ_lines);
  3813.             x = strlen(line);
  3814.             len += x;
  3815.             if (len < LINBUFSIZ) {
  3816.                 memcpy((char *)&line[x],(char *)buf,len);
  3817.                 memcpy((char *)buf,(char *)line,len);
  3818.             }
  3819.         }
  3820.         if (width > 0 && width <= TYPBUFL) { /* Truncate at given width. */
  3821.             char * obuf = line;         /* But to do that first we must */
  3822.             int i,k,z;                  /* expand tabs; assume every 8 cols. */
  3823.             line[0] = NUL;
  3824.             for (i = 0, k = 0; i < width; k++) { /* Character loop... */
  3825.                 if (!buf[k])            /* No more chars in this line, done. */
  3826.                   break;
  3827.                 if (buf[k] != '\t') {   /* If it's not a tab */
  3828.                     if (i >= LINBUFSIZ) /* Check for overflow */
  3829.                       break;
  3830.                     obuf[i++] = buf[k]; /* and then deposit it. */
  3831.                     obuf[i] = NUL;      /* Keep it null-terminated */
  3832.                     continue;
  3833.                 }
  3834.                 z = 8 - (i % 8);        /* It's a tab, expand it. */
  3835.                 if (z == 0) z = 8;
  3836.                 for (j = 0; j < z && i < LINBUFSIZ; j++) {
  3837. #ifdef UNICODE
  3838.                     if (ucs2 && !ucsorder)
  3839.                       obuf[i++] = NUL;
  3840. #endif /* UNICODE */
  3841.                     obuf[i++] = ' ';
  3842. #ifdef UNICODE
  3843.                     if (ucs2 && ucsorder)
  3844.                       obuf[i++] = NUL;
  3845. #endif /* UNICODE */
  3846.                 }
  3847.                 obuf[i++] = NUL;
  3848.                 obuf[i] = NUL;
  3849.             }
  3850.             obuf[width] = NUL;          /* Now truncate at given width. */
  3851. #ifdef COMMENT
  3852.             /* This doesn't work for UCS-2 because it contains NULs */
  3853.             ckstrncpy(buf,obuf,TYPBUFL); /* and copy it back (again?) */
  3854. #else
  3855.             memcpy((char *)buf,(char *)obuf,i); /* Copy it back */
  3856. #endif /* COMMENT */
  3857.             len = (i > width) ? width : i; /* Spare us another strlen()... */
  3858.         }
  3859.         if (tailing) {                  /* If /TAIL:n... */
  3860.             k = lines % head;           /* save this line in circular buffer */
  3861.             if (!counting) {
  3862.                 if (tail[k]) free(tail[k]);
  3863.                 tail[k] = malloc(len+2);
  3864.                 if (!tail[k]) {
  3865.                     printf("?Memory allocation failure\n");
  3866.                     goto xdotype;
  3867.                 }
  3868.                 memcpy(tail[k],buf,len);
  3869.                 tlen[k] = len;
  3870.                 continue;
  3871.             }
  3872.         }
  3873.         if (counting)                   /* If only counting */
  3874.           continue;                     /* we're done with this line */
  3875.  
  3876.         if (paging) {                   /* Displaying this line... */
  3877.             int u;
  3878.             u = len;                    /* Length in BYTES */
  3879.             if (ucs2)                   /* If outputting in UCS-2 */
  3880.               u /= 2;                   /* convert length to CHARACTERS */
  3881.             x = (u / cmd_cols) + 1;     /* Crudely allow for wrap */
  3882.             if (cmd_rows > 0 && cmd_cols > 0)
  3883.               n += x;                   /* This assumes terminal will wrap */
  3884.         }
  3885.         typeline(buf,len,outcs,ofp);    /* Print line, length based */
  3886. #ifdef CK_TTGWSIZ
  3887.         debug(F101,"dotype n","",n);
  3888.         if (paging > 0 && ofp == stdout) { /* Pause at end of screen */
  3889.             if (cmd_rows > 0 && cmd_cols > 0) {
  3890.                 if (n > cmd_rows - 3) {
  3891.                     if (!askmore())
  3892.                       goto xdotype;
  3893.                     else
  3894.                       n = 0;
  3895.                 }
  3896.             }
  3897.         }
  3898. #endif /* CK_TTGWSIZ */
  3899.     }
  3900.  
  3901.   xdotype:
  3902.     if (counting) {
  3903.         fprintf(ofp,
  3904.                 "%s: %d line%s\n",file,typ_lines,typ_lines == 1 ? "" : "s");
  3905.         if (pat)
  3906.           fprintf(ofp,
  3907.                   "%s: %d match%s\n",pat,typ_mtchs,typ_mtchs == 1 ? "" : "es");
  3908.         goto xxdotype;
  3909.     }
  3910.     if (tailing && tail) {              /* Typing tail of file? */
  3911.         if (lines < head) {             /* Yes, show the lines we saved */
  3912.             k = 0;                      /* Show all lines */
  3913.         } else {                        /* More lines than tail number */
  3914.             lines = k;                  /* Last line to show */
  3915.             k++;                        /* First line to show */
  3916.             if (k >= head)
  3917.               k = 0;
  3918.         }
  3919.         n = first;                      /* Output line counter */
  3920.         for (i = k ;; i++) {            /* Loop thru circular buffer */
  3921. #ifndef MAC
  3922.             if (typ_int) {              /* Interrupted? */
  3923.                 printf("^C...\n");      /* Print message */
  3924.                 goto xxdotype;
  3925.             }
  3926. #endif /* MAC */
  3927.             j = i % head;               /* Index of this line */
  3928.             s = tail[j];                /* Point to line to display */
  3929.             if (!s)                     /* (shouldn't happen...) */
  3930.               break;
  3931.             if (paging) {               /* Crudely allow for line wrap */
  3932.                 x = tlen[j];
  3933.                 if (ucs2) x /= 2;
  3934.                 x = x / cmd_cols + 1;
  3935.                 if (cmd_rows > 0 && cmd_cols > 0)
  3936.                   n += x;
  3937.             }
  3938.             typeline(s,tlen[j],outcs,ofp); /* Display this line */
  3939.             if (paging && ofp == stdout) { /* Pause at end of screen */
  3940.                 if (cmd_rows > 0 && cmd_cols > 0) {
  3941.                     if (n > cmd_rows - 3) {
  3942.                         if (!askmore())
  3943.                           break;
  3944.                         else
  3945.                           n = 0;
  3946.                     }
  3947.                 }
  3948.             }
  3949.             tail[j] = NULL;
  3950.             free(s);                    /* Free the line */
  3951.             if (i % head == lines)      /* When to stop */
  3952.               break;
  3953.         }
  3954.         free((char *)tail);             /* Free the list */
  3955.         tail = NULL;
  3956.         if (tlen) free((char *)tlen);
  3957.         tlen = NULL;
  3958.     }
  3959.  
  3960. /* Come here when finished or on SIGINT */
  3961.  
  3962.   xxdotype:
  3963. #ifndef AMIGA
  3964. #ifndef MAC
  3965.     signal(SIGINT,oldsig);              /* Put old signal action back. */
  3966. #endif /* MAC */
  3967. #endif /* AMIGA */
  3968.     if (tailing && tail) {
  3969.         for (i = 0; i < head; i++) {    /* Free each line. */
  3970.             if (tail[i])
  3971.               free(tail[i]);
  3972.         }
  3973.         free((char *)tail);             /* Free list pointer */
  3974.         if (tlen)
  3975.           free((char *)tlen);
  3976.     }
  3977.     x = zclose(ZIFILE);                 /* Done, close the input file */
  3978.     if (ofp != stdout) {                /* Close any output file */
  3979.         if (ofp) fclose(ofp);
  3980.         ofp = stdout;
  3981.     }
  3982.     binary = save;                      /* Restore text/binary mode */
  3983. #ifdef UNICODE
  3984.     ucsbom = ucsbom_sav;                /* Restore BOM usage */
  3985. #endif /* UNICODE */
  3986.     return(rc);
  3987. }
  3988.  
  3989. /* GREP command */
  3990.  
  3991. #define GREP_CASE  0                    /* /CASE */
  3992. #define GREP_COUN  1                    /* /COUNT */
  3993. #define GREP_DOTF  2                    /* /DOTFILES */
  3994. #define GREP_NAME  3                    /* /NAMEONLY */
  3995. #define GREP_NOBK  4                    /* /NOBACKUP */
  3996. #define GREP_NODO  5                    /* /NODOTFILES */
  3997. #define GREP_NOLI  6                    /* /NOLIST = /QUIET */
  3998. #define GREP_NOMA  7                    /* /INVERT = /NOMATCH */
  3999. #define GREP_NOPA  8                    /* /NOPAGE */
  4000. #define GREP_NUMS  9                    /* /LINENUMBERS */
  4001. #define GREP_PAGE 10                    /* /PAGE */
  4002. #define GREP_RECU 11                    /* /RECURSIVE */
  4003. #define GREP_TYPE 12                    /* /TYPE: */
  4004. #define GREP_OUTP 13                    /* /OUTPUTFILE: */
  4005.  
  4006. static struct keytab greptab[] = {
  4007.     { "/count",        GREP_COUN, CM_ARG },
  4008.     { "/dotfiles",     GREP_DOTF, 0 },
  4009.     { "/nameonly",     GREP_NAME, 0 },
  4010.     { "/nobackupfiles",GREP_NOBK, 0 },
  4011.     { "/nocase",       GREP_CASE, 0 },
  4012.     { "/nodotfiles",   GREP_NODO, 0 },
  4013.     { "/nolist",       GREP_NOLI, 0 },
  4014.     { "/nomatch",      GREP_NOMA, 0 },
  4015.     { "/nopage",       GREP_NOPA, 0 },
  4016.     { "/output",       GREP_OUTP, CM_ARG },
  4017.     { "/linenumbers",  GREP_NUMS, 0 },
  4018.     { "/page",         GREP_PAGE, 0 },
  4019.     { "/quiet",        GREP_NOLI, CM_INV },
  4020. #ifdef RECURSIVE
  4021.     { "/recursive",    GREP_RECU, 0 },
  4022. #endif /* RECURSIVE */
  4023.     { "/type",         GREP_TYPE, CM_ARG },
  4024.     { "", 0, 0 }
  4025. };
  4026. static int ngreptab =  sizeof(greptab)/sizeof(struct keytab)-1;
  4027.  
  4028. int
  4029. dogrep() {
  4030.     int match, x, y, fc, getval, mc = 0, count = 0, bigcount = 0;
  4031.     int fline = 0, sline = 0, wild = 0, len = 0;
  4032.     int xmode = -1, scan = 0;
  4033.     char c, name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p, *s, *cv = NULL;
  4034.     FILE * fp = NULL;
  4035.  
  4036.     int                                 /* Switch values and defaults */
  4037.       gr_coun = 0,
  4038.       gr_name = 0,
  4039.       gr_nobk = 0,
  4040.       gr_case = 1,
  4041.       gr_noli = 0,
  4042.       gr_noma = 0,
  4043.       gr_nums = 0,
  4044.       gr_page = xaskmore;
  4045.  
  4046.     struct FDB sw, fl;
  4047.  
  4048.     g_matchdot = matchdot;              /* Save global matchdot setting */
  4049.     outfile[0] = NUL;
  4050.  
  4051.     if (ofp != stdout) {                /* In case of previous interruption */
  4052.         if (ofp) fclose(ofp);
  4053.         ofp = stdout;
  4054.     }
  4055.     cmfdbi(&sw,                         /* First FDB - command switches */
  4056.            _CMKEY,                      /* fcode */
  4057.            "String or pattern to search for, or switch",
  4058.            "",                          /* default */
  4059.            "",                          /* addtl string data */
  4060.            ngreptab,                    /* addtl numeric data 1: tbl size */
  4061.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4062.            xxstring,                    /* Processing function */
  4063.            greptab,                     /* Keyword table */
  4064.            &fl                          /* Pointer to next FDB */
  4065.            );
  4066.     cmfdbi(&fl,                         /* Anything that doesn't match */
  4067.            _CMFLD,                      /* fcode */
  4068.            "",                          /* hlpmsg */
  4069.            "",                          /* default */
  4070.            "",                          /* addtl string data */
  4071.            0,                           /* addtl numeric data 1 */
  4072.            0,                           /* addtl numeric data 2 */
  4073.            xxstring,            /* xxstring */
  4074.            NULL,
  4075.            NULL
  4076.            );
  4077.     while (1) {                         /* Parse 0 or more switches */
  4078.         x = cmfdb(&sw);                 /* Parse something */
  4079.         if (x < 0)
  4080.           return(x);
  4081.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  4082.           break;
  4083.         c = cmgbrk();
  4084.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4085.             printf("?This switch does not take an argument\n");
  4086.             return(-9);
  4087.         }
  4088.         if ((cmresult.nresult != GREP_COUN) && !getval &&
  4089.             (cmgkwflgs() & CM_ARG)) {
  4090.             printf("?This switch requires an argument\n");
  4091.             return(-9);
  4092.         }
  4093.         switch (cmresult.nresult) {
  4094.           case GREP_COUN: {
  4095.               gr_coun++;
  4096.               gr_noli=0;
  4097.               if (getval) {
  4098.                   if ((x = cmfld("Variable for result","",&s,NULL)) < 0)
  4099.                     return(x);
  4100.                   makestr(&cv,s);
  4101.               }
  4102.               break;
  4103.           }
  4104.           case GREP_CASE: gr_case=0; break;
  4105.           case GREP_NAME: gr_name++; gr_noli=0; break;
  4106.           case GREP_NOBK: gr_nobk++; break;
  4107.           case GREP_NOLI: gr_noli++; gr_coun=0; gr_name=0; gr_nums=0; break;
  4108.           case GREP_NOMA: gr_noma++; break;
  4109.           case GREP_NOPA: gr_page=0; break;
  4110.           case GREP_NUMS: gr_nums++; gr_noli=0; break;
  4111.           case GREP_PAGE: gr_page++; gr_noli=0; break;
  4112.           case GREP_NODO:
  4113.             matchdot = 0;
  4114.             break;
  4115.           case GREP_DOTF:
  4116.             matchdot = 1;
  4117.             break;
  4118. #ifdef RECURSIVE
  4119.           case GREP_RECU:
  4120.             recursive = 1;
  4121.             break;
  4122. #endif /* RECURSIVE */
  4123.           case GREP_TYPE: {
  4124.               extern struct keytab txtbin[];
  4125.               if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  4126.                 return(x);
  4127.               if (x == 2) {             /* ALL */
  4128.                   xmode = -1;
  4129.               } else {                  /* TEXT or BINARY only */
  4130.                   xmode = x;
  4131.                   scan = 1;
  4132.               }
  4133.               break;
  4134.           }
  4135.           case GREP_OUTP:               /* Send output to file */
  4136.             if ((x = cmofi("File for GREP'd lines","",&s,xxstring)) < 0)
  4137.               return(x);
  4138.             ckstrncpy(outfile,s,CKMAXPATH);
  4139.             break;
  4140.         }
  4141.     }
  4142.     if (outfile[0]) {
  4143.         ofp = fopen(outfile,"w");       /* Open output file */
  4144.         if (!ofp) {
  4145.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  4146.             ofp = stdout;
  4147.             return(-9);
  4148.         }
  4149.         gr_page = 0;
  4150.     }
  4151.     s = cmresult.sresult;
  4152.     s = brstrip(s);                     /* Strip braces from pattern */
  4153.     if (!*s) {
  4154.         printf("?Pattern required\n");
  4155.         return(-9);
  4156.     }
  4157.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);      /* Save pattern */
  4158.     if ((x = cmifi("File(s) to search","",&s,&wild,xxstring)) < 0) {
  4159.         if (x == -3) {
  4160.             printf("?File specification required\n");
  4161.             x = -9;
  4162.         }
  4163.         return(x);
  4164.     }
  4165.     s = brstrip(s);                     /* Strip braces from filename */
  4166. #ifndef ZXREWIND
  4167.     ckstrncpy(line,s,LINBUFSIZ);
  4168. #endif /* ZXREWIND */
  4169.     if ((y = cmcfm()) < 0)
  4170.       return(y);
  4171.  
  4172.     if (gr_page > -1)
  4173.       xaskmore = gr_page;               /* Paging... */
  4174.  
  4175.     p = tmpbuf;                         /* Point to pattern */
  4176. #ifdef COMMENT
  4177. /* Now this is done in ckmatch */
  4178.     if (*p == '^') {                    /* '^' anchors pattern to beginning */
  4179.         p++;
  4180.     } else if (*p != '*') {             /* Otherwise prepend implied '*' */
  4181.         tmpbuf[0] = '*';
  4182.         p = tmpbuf;
  4183.     }
  4184.     x = strlen(p);                      /* Get length of result */
  4185.     if (x > 0 && x < TMPBUFSIZ) {       /* '$' at end anchors pattern to end */
  4186.         if (p[x-1] == '$') {
  4187.             p[x-1] = NUL;
  4188.         } else if (p[x-1] != '*') {
  4189.             p[x] = '*';
  4190.             p[x+1] = NUL;
  4191.         }
  4192.     }
  4193. #endif /* COMMENT */
  4194.     debug(F111,"grep pat",p,x);
  4195.  
  4196. #ifdef ZXREWIND
  4197.     fc = zxrewind();                    /* Rewind the file list */
  4198. #else
  4199.     {
  4200.         int flags = ZX_FILONLY;         /* Expand file list */
  4201.         if (matchdot)  flags |= ZX_MATCHDOT;
  4202.         if (recursive) flags |= ZX_RECURSE;
  4203.         fc = nzxpand(line,flags);
  4204.     }
  4205. #endif /* ZXREWIND */
  4206. #ifdef UNIX
  4207.     sh_sort(mtchs,NULL,fc,0,0,filecase);
  4208. #endif /* UNIX */
  4209.  
  4210.     debug(F101,"grep cmd_rows","",cmd_rows);
  4211.     debug(F101,"grep cmd_cols","",cmd_cols);
  4212.  
  4213.     while (1) {                         /* Loop for each file */
  4214.         znext(name);                    /* Get next file */
  4215.         if (!name[0])                   /* No more, done */
  4216.           break;
  4217.         if (gr_nobk)                    /* Skipping backup files? */
  4218.           if (ckmatch("*.~[1-9]*~",name,1,1)) /* Backup file? */
  4219.             continue;                   /* Yes, skip */
  4220.         if (scan) {                     /* /TYPE: given? */
  4221.             switch (scanfile(name,&y,nscanfile)) { /* Yes, scan the file */
  4222.               case FT_BIN:
  4223.                 if (xmode != 1)
  4224.                   continue;
  4225.                 break;
  4226.               case FT_TEXT:
  4227.               case FT_7BIT:
  4228.               case FT_8BIT:
  4229. #ifdef UNICODE
  4230.               case FT_UTF8:
  4231.               case FT_UCS2:
  4232. #endif /* UNICODE */
  4233.                 if (xmode != 0)
  4234.                   continue;
  4235.             }
  4236.         }
  4237.         fp = fopen(name,"r");           /* Open */
  4238.         if (!fp)                        /* Can't */
  4239.           continue;                     /* Skip */
  4240.         count = 0;                      /* Match count, this file */
  4241.         fline = 0;                      /* Line count, this file */
  4242.         while (1) {                     /* Loop for each line */
  4243.             if (fgets(line,LINBUFSIZ,fp) == NULL) { /* Get next line */
  4244.                 fclose(fp);
  4245.                 fp = NULL;
  4246.         debug(F100,"GREP EOF","",0);
  4247.                 break;
  4248.             }
  4249.             fline++;                    /* Count this line */
  4250.             line[LINBUFSIZ] = NUL;      /* Make sure it's terminated */
  4251.         debug(F111,"GREP",line,fline);
  4252.             len = (int)strlen(line);    /* Get length */
  4253.             while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
  4254.               line[--len] = NUL;        /* Chop off terminators */
  4255.             match = ckmatch(p,line,gr_case,1+4); /* Match against pattern */
  4256.             if (gr_noma)                /* Invert match sense if requested */
  4257.               match = !match;
  4258.             if (match) {                /* Have a matching line */
  4259.                 mc++;                   /* Total match count */
  4260.                 count++;                /* Match count this file */
  4261.                 if (gr_name) {          /* Don't care how many lines match */
  4262.                     fclose(fp);         /* Close the file */
  4263.                     fp = NULL;          /* and quit the line-reading loop. */
  4264.                     break;
  4265.                 }
  4266.                 if (gr_coun || gr_noli) /* Not listing each line */
  4267.                   continue;             /* so don't print anything now. */
  4268.                 if (wild) {             /* If searching multiple files */
  4269.                     fprintf(ofp,"%s:",name); /* print filename. */
  4270.                     len += (int)strlen(name) + 1;
  4271.                 }
  4272.                 if (gr_nums) {          /* If line numbers wanted */
  4273.                     char nbuf[32];
  4274.                     len += ckmakmsg(nbuf,32,ckitoa(fline),":",NULL,NULL);
  4275.                     fprintf(ofp,"%s",nbuf);
  4276.                 }
  4277.                 if (cmd_rows > 0 && cmd_cols > 0)
  4278.                   sline += (len / cmd_cols) + 1;
  4279.                 fprintf(ofp,"%s\n",line); /* Print the line. */
  4280.                 if (sline > cmd_rows - 3) {
  4281.                     if (!askmore()) goto xgrep; else sline = 0;
  4282.                 }
  4283.             }
  4284.         }
  4285.         if (!gr_noli) {                 /* If not not listing... */
  4286.             x = 0;
  4287.             if (gr_coun) {              /* Show match count only */
  4288.                 fprintf(ofp,"%s:%d\n",name,count);
  4289.                 x++;
  4290.             } else if (gr_name && count > 0) { /* Show name only */
  4291.                 fprintf(ofp,"%s\n",name);
  4292.                 x++;
  4293.             }
  4294.             if (x > 0) {
  4295.                 if (++sline > cmd_rows - 3) {
  4296.                     if (!askmore()) goto xgrep; else sline = 0;
  4297.                 }
  4298.             }
  4299.         }
  4300.         bigcount += count;              /* Overall count */
  4301.     }
  4302.   xgrep:
  4303. #ifndef NOSPL
  4304.     if (gr_coun && cv) {                /* /COUNT:blah */
  4305.         addmac(cv,ckitoa(bigcount));    /* set the variable */
  4306.         makestr(&cv,NULL);              /* free this */
  4307.     }
  4308. #endif /* NOSPL */
  4309.     if (fp) fclose(fp);                 /* close input file if still open */
  4310.     if (ofp != stdout) {                /* Close any output file */
  4311.         if (ofp) fclose(ofp);
  4312.         ofp = stdout;
  4313.     }
  4314.     return(success = mc ? 1 : 0);
  4315. }
  4316.  
  4317. /* System-independent directory */
  4318.  
  4319. static char ** dirlist = NULL;
  4320. static int ndirlist = 0;
  4321.  
  4322. static VOID
  4323. freedirlist() {
  4324.     if (dirlist) {
  4325.         int i;
  4326.         for (i = 0; i < ndirlist; i++) {
  4327.             if (dirlist[i])
  4328.               free(dirlist[i]);
  4329.         }
  4330.         free((char *)dirlist);
  4331.         dirlist = NULL;
  4332.     }
  4333.     ndirlist = 0;
  4334. }
  4335.  
  4336. static struct keytab dirswtab[] = {     /* DIRECTORY command switches */
  4337.     { "/after",       DIR_AFT, CM_ARG },
  4338.     { "/all",         DIR_ALL, 0 },
  4339. #ifndef NOSPL
  4340.     { "/array",       DIR_ARR, CM_ARG },
  4341. #endif /* NOSPL */
  4342.     { "/ascending",   DIR_ASC, 0 },
  4343.     { "/backup",      DIR_BUP, 0 },
  4344.     { "/before",      DIR_BEF, CM_ARG },
  4345.     { "/brief",       DIR_BRF, 0 },
  4346.     { "/descending",  DIR_DSC, CM_INV },
  4347.     { "/directories", DIR_DIR, 0 },
  4348.     { "/dotfiles",    DIR_DOT, 0 },
  4349.     { "/englishdate", DIR_DAT, 0 },
  4350.     { "/except",      DIR_EXC, CM_ARG },
  4351.     { "/files",       DIR_FIL, 0 },
  4352.     { "/heading",     DIR_HDG, 0 },
  4353.     { "/isodate",     DIR_ISO, 0 },
  4354.     { "/larger-than", DIR_LAR, CM_ARG },
  4355. #ifdef CKSYMLINK
  4356.     { "/followlinks", DIR_LNK, 0 },
  4357. #endif /* CKSYMLINK */
  4358.     { "/message",     DIR_MSG, CM_ARG },
  4359.     { "/nobackupfiles",DIR_NOB, 0 },
  4360.     { "/nodotfiles",  DIR_NOD, 0 },
  4361. #ifdef CKSYMLINK
  4362.     { "/nofollowlinks",DIR_NLK, 0 },
  4363. #endif /* CKSYMLINK */
  4364.     { "/noheading",   DIR_NOH, 0 },
  4365.     { "/nomessage",   DIR_NOM, 0 },
  4366. #ifdef CK_TTGWSIZ
  4367.     { "/nopage",      DIR_NOP, 0 },
  4368. #endif /* CK_TTGWSIZ */
  4369. #ifdef RECURSIVE
  4370.     { "/norecursive", DIR_NOR, 0 },
  4371. #else
  4372. #ifdef VMS
  4373.     { "/norecursive", DIR_NOR, 0 },
  4374. #else
  4375. #ifdef datageneral
  4376.     { "/norecursive", DIR_NOR, 0 },
  4377. #endif /* datageneral */
  4378. #endif /* VMS */
  4379. #endif /* RECURSIVE */
  4380.     { "/nosort",      DIR_NOS, 0 },
  4381.     { "/not-after",   DIR_NAF, CM_ARG },
  4382.     { "/not-before",  DIR_NBF, CM_ARG },
  4383.     { "/not-since",   DIR_NAF, CM_INV|CM_ARG },
  4384.     { "/noxfermode",  DIR_NOT, 0 },
  4385.     { "/output",      DIR_OUT, CM_ARG },
  4386. #ifdef CK_TTGWSIZ
  4387.     { "/page",        DIR_PAG, 0 },
  4388. #endif /* CK_TTGWSIZ */
  4389. #ifdef RECURSIVE
  4390.     { "/recursive",   DIR_REC, 0 },
  4391. #else
  4392. #ifdef VMS
  4393.     { "/recursive",   DIR_REC, 0 },
  4394. #else
  4395. #ifdef datageneral
  4396.     { "/recursive",   DIR_REC, 0 },
  4397. #endif /* datageneral */
  4398. #endif /* VMS */
  4399. #endif /* RECURSIVE */
  4400.     { "/reverse",     DIR_DSC, 0 },
  4401.     { "/since",       DIR_AFT, CM_ARG|CM_INV },
  4402.     { "/smaller-than",DIR_SMA, CM_ARG },
  4403.     { "/sort",        DIR_SRT, CM_ARG },
  4404.     { "/summary",     DIR_SUM, 0 },
  4405.     { "/type",        DIR_BIN, CM_ARG },
  4406.     { "/xfermode",    DIR_TYP, 0 },
  4407.     { "/verbose",     DIR_VRB, 0 },
  4408.     { "",0,0 }
  4409. };
  4410. static int ndirswtab = (sizeof(dirswtab) / sizeof(struct keytab)) - 1;
  4411.  
  4412. static struct keytab dirsort[] = {      /* DIRECTORY /SORT: options */
  4413.     { "date",         DIRS_DT, 0 },
  4414.     { "name",         DIRS_NM, 0 },
  4415.     { "size",         DIRS_SZ, 0 }
  4416. };
  4417. static int ndirsort = (sizeof(dirsort) / sizeof(struct keytab));
  4418.  
  4419. static int dir_date = -1;               /* Option defaults (-1 means none) */
  4420. static int dir_page = -1;
  4421. static int dir_verb =  1;
  4422. static int dir_msg  = -1;
  4423. #ifdef VMS
  4424. static int dir_sort = -1;               /* Names are already sorted in VMS */
  4425. static int dir_rvrs = -1;
  4426. #else
  4427. static int dir_sort =  1;               /* Sort by default */
  4428. static int dir_rvrs =  0;               /* Not in reverse */
  4429. #endif /* VMS */
  4430. static int dir_skey = DIRS_NM;          /* By name */
  4431. #ifdef RECURSIVE
  4432. static int dir_recu = -1;
  4433. #endif /* RECURSIVE */
  4434. static int dir_mode = -1;
  4435. static int dir_show = -1;               /* Show all files by default */
  4436. int dir_dots =  -1;            /* Except dot files */
  4437. int dir_back =  1;
  4438. int dir_head =  0;
  4439. static char * dirmsg = NULL;
  4440. static int dirmsglen = 0;
  4441.  
  4442. #ifndef NOSHOW
  4443. VOID
  4444. showdiropts() {
  4445.     int x = 0;
  4446.     extern int optlines;
  4447.     prtopt(&optlines,"DIRECTORY");
  4448.     if (dir_show > 0) {
  4449.         prtopt(&optlines,(dir_show == 1) ? "/FILES" :
  4450.                ((dir_show == 2) ? "/DIRECTORIES" : "/ALL"));
  4451.         x++;
  4452.     } else {
  4453.         prtopt(&optlines,"/ALL");
  4454.         x++;
  4455.     }
  4456.     if (dir_verb > -1) {
  4457.         prtopt(&optlines,dir_verb ? "/VERBOSE" : "/BRIEF");
  4458.         x++;
  4459.     }
  4460.     if (dir_page > -1) {
  4461.         prtopt(&optlines,dir_page ? "/PAGE" : "/NOPAGE");
  4462.         x++;
  4463.     }
  4464.     if (dir_date > -1) {
  4465.         prtopt(&optlines,dir_date ? "/ENGLISHDATE" : "/ISODATE");
  4466.         x++;
  4467.     }
  4468.     if (dir_dots > -1) {
  4469.         prtopt(&optlines,dir_dots ? "/DOTFILES" : "/NODOTFILES");
  4470.         x++;
  4471.     }
  4472.     if (dir_back > -1) {
  4473.         prtopt(&optlines,dir_back ? "/BACKUP" : "/NOBACKUP");
  4474.         x++;
  4475.     }
  4476.     if (dir_head > -1) {
  4477.         prtopt(&optlines,dir_head ? "/HEADING" : "/NOHEADING");
  4478.         x++;
  4479.     }
  4480. #ifdef RECURSIVE
  4481.     if (dir_recu > -1) {
  4482.         prtopt(&optlines,dir_recu ? "/RECURSIVE" : "/NORECURSIVE");
  4483.         x++;
  4484.     }
  4485. #endif /* RECURSIVE */
  4486.     if (dir_mode > -1) {
  4487.         prtopt(&optlines,dir_mode ? "/XFERMODE" : "/NOXFERMODE");
  4488.         x++;
  4489.     }
  4490.     if (dir_sort == 0) {
  4491.         x++;
  4492.         prtopt(&optlines,"/NOSORT ");
  4493.     } else if (dir_sort > 0) {
  4494.         x++;
  4495.         if (dir_skey == DIRS_NM) s = "/SORT:NAME";
  4496.         else if (dir_skey == DIRS_SZ) s = "/SORT:SIZE";
  4497.         else if (dir_skey == DIRS_DT) s = "/SORT:DATE";
  4498.         prtopt(&optlines,s);
  4499.     }
  4500.     if (dir_rvrs > -1) {
  4501.         prtopt(&optlines,dir_rvrs ? "/REVERSE" : "/ASCENDING");
  4502.         x++;
  4503.     }
  4504.     if (dir_msg > -1) {
  4505.         if (dir_msg == 0) {
  4506.             prtopt(&optlines,"/NOMESSAGE");
  4507.         } else {
  4508.             ckmakmsg(tmpbuf,TMPBUFSIZ,"/MESSAGE:{",dirmsg,"}",NULL);
  4509.             prtopt(&optlines,tmpbuf);
  4510.         }
  4511.         x++;
  4512.     }
  4513.     if (!x) prtopt(&optlines,"(no options set)");
  4514.     prtopt(&optlines,"");
  4515. }
  4516. #endif /* NOSHOW */
  4517.  
  4518. int
  4519. setdiropts() {                          /* Set DIRECTORY option defaults */
  4520.     int xb = -1, xv = -1, xp = -1, xd = -1, xh = -1, xf = -1;
  4521.     int xk = -1, xr = -1, xs = -1, xx = -1, xm = -1, xa = -1, xg = -1;
  4522.     int getval;
  4523.     char c;
  4524.     while (1) {
  4525.         if ((y = cmswi(dirswtab,ndirswtab,"Switch","",xxstring)) < 0) {
  4526.             if (y == -3)
  4527.               break;
  4528.             else
  4529.               return(y);
  4530.         }
  4531.         c = cmgbrk();
  4532.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4533.             printf("?This switch does not take an argument\n");
  4534.             return(-9);
  4535.         }
  4536.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4537.             printf("?This switch requires an argument\n");
  4538.             return(-9);
  4539.         }
  4540.         switch (y) {
  4541.           case DIR_BRF: xv = 0; break;
  4542.           case DIR_VRB: xv = 1; break;
  4543.           case DIR_PAG: xp = 1; break;
  4544.           case DIR_NOP: xp = 0; break;
  4545.           case DIR_ISO: xd = 0; break;
  4546.           case DIR_DAT: xd = 1; break;
  4547.           case DIR_HDG: xh = 1; break;
  4548.           case DIR_NOH: xh = 0; break;
  4549.           case DIR_DOT: xf = 1; break;
  4550.           case DIR_NOD: xf = 0; break;
  4551.           case DIR_ALL: xa = 3; break;
  4552.           case DIR_DIR: xa = 2; break;
  4553.           case DIR_FIL: xa = 1; break;
  4554.           case DIR_SRT:
  4555.             x = DIRS_NM;
  4556.             if (getval)
  4557.               if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4558.                 return(x);
  4559.             xk = x;
  4560.             xs = 1;
  4561.             break;
  4562.           case DIR_NOS: xs = 0; break;
  4563.           case DIR_ASC: xx = 0; break;
  4564.           case DIR_DSC: xx = 1; break;
  4565.           case DIR_REC: xr = 1; break;
  4566.           case DIR_NOR: xr = 0; break;
  4567.           case DIR_TYP: xm = 1; break;
  4568.           case DIR_NOT: xm = 0; break;
  4569.           case DIR_BUP: xb = 1; break;
  4570.           case DIR_NOB: xb = 0; break;
  4571.           case DIR_NOM: xg = 0; break;
  4572.           case DIR_MSG:
  4573.             if (getval)
  4574.               if ((x = cmfld("Message to append to each line",
  4575.                              "",
  4576.                              &s,
  4577.                              xxstring
  4578.                              )) < 0)
  4579.                 return(x);
  4580.             xg = 1;
  4581.             ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4582.             break;
  4583.           default:
  4584.             printf("?This option can not be set\n");
  4585.             return(-9);
  4586.         }
  4587.     }
  4588.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  4589.       return(x);
  4590.     if (xv > -1) dir_verb = xv;         /* Confirmed, save defaults */
  4591.     if (xp > -1) dir_page = xp;
  4592.     if (xd > -1) dir_date = xd;
  4593.     if (xh > -1) dir_head = xh;
  4594.     if (xs > -1) dir_sort = xs;
  4595.     if (xk > -1) dir_skey = xk;
  4596.     if (xx > -1) dir_rvrs = xx;
  4597.     if (xf > -1) dir_dots = xf;
  4598.     if (xa > -1) dir_show = xa;
  4599.     if (xm > -1) dir_mode = xm;
  4600.     if (xb > -1) dir_back = xb;
  4601. #ifdef RECURSIVE
  4602.     if (xr > -1) dir_recu = xr;
  4603. #endif /* RECURSIVE */
  4604.     if (xg > -1) dir_msg  = xg;
  4605.     if (xg > 0)
  4606.       makestr(&dirmsg,tmpbuf);
  4607.     return(success = 1);
  4608. }
  4609.  
  4610. int
  4611. domydir() {                             /* Internal DIRECTORY command */
  4612.     extern char *months[];
  4613. #ifdef VMS
  4614.     _PROTOTYP( char * zrelname, (char *,char *) );
  4615.     char * cdp = NULL;
  4616. #endif /* VMS */
  4617.  
  4618.     char name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p = NULL, c = NUL;
  4619.     char linebuf[CKMAXPATH+256];
  4620.     char * mstr = NULL, * dstr = NULL, * s2 = NULL;
  4621.     long len = 0, ndirs = 0, nfiles = 0, nbytes = 0, nmatches = 0;
  4622.     int verbose = 0, wild = 0, page = 0, n = 0, engdate = 0, summary = 0;
  4623.     int heading = 0, xsort = 0, reverse = 0, sortby = 0, msg = 0;
  4624.     int k, i = 0, x = 0, nx = 0, skey = 0, dlen = 0, itsadir = 0;
  4625.     int show = 3, xfermod = 0, backup = 1, rc = 0, getval = 0;
  4626.     int fs = 0;
  4627.     int multiple = 0;
  4628.     int cmifn1 = 1, cmifn2 = 0;
  4629.     long minsize = -1L, maxsize = -1L;
  4630.     struct FDB sw, fi, fl;
  4631.     char dbuf[32], xbuf[32];
  4632.  
  4633. #ifndef NOSPL
  4634.     char array = NUL;
  4635.     char ** ap = NULL;
  4636. #endif /* NOSPL */
  4637.     char
  4638.       * dir_aft = NULL,
  4639.       * dir_bef = NULL,
  4640.       * dir_naf = NULL,
  4641.       * dir_nbf = NULL,
  4642.       * dir_exc = NULL;
  4643.     char * xlist[16];
  4644.  
  4645.     g_matchdot = matchdot;              /* Save global matchdot setting */
  4646.     nolinks = 2;                        /* (it should already be 2) */
  4647.     outfile[0] = NUL;                   /* No output file yet */
  4648.  
  4649.     if (ofp != stdout) {                /* In case of previous interruption */
  4650.         if (ofp) fclose(ofp);
  4651.         ofp = stdout;
  4652.     }
  4653.     for (i = 0; i < 16; i++) xlist[i] = NULL;
  4654.  
  4655.     name[0] = NUL;
  4656.     freedirlist();                      /* In case not freed last time */
  4657.     page      = dir_page > -1 ? dir_page : xaskmore; /* Set option defaults */
  4658.     engdate   = dir_date > -1 ? dir_date : 0;
  4659.     verbose   = dir_verb > -1 ? dir_verb : 1;
  4660.     heading   = dir_head > -1 ? dir_head : 0;
  4661.     xsort     = dir_sort > -1 ? dir_sort : 0;
  4662.     sortby    = dir_skey > -1 ? dir_skey : 0;
  4663.     reverse   = dir_rvrs > -1 ? dir_rvrs : 0;
  4664.     msg       = dir_msg  > -1 ? dir_msg  : 0;
  4665. #ifdef UNIXOROSK
  4666.     if (dir_dots > -1) matchdot = dir_dots;
  4667. #endif /* UNIXOROSK */
  4668.     xfermod   = dir_mode > -1 ? dir_mode : 0;
  4669.     backup    = dir_back > -1 ? dir_back : 1;
  4670. #ifdef RECURSIVE
  4671.     recursive = dir_recu > -1 ? dir_recu : 0;
  4672. #endif /* RECURSIVE */
  4673.     show      = dir_show > -1 ? dir_show : 3;
  4674.  
  4675. #ifdef CK_TTGWSIZ
  4676. #ifdef OS2
  4677.     ttgcwsz();                          /* Screen length for more-prompting */
  4678. #else /* OS2 */
  4679.     /* Check whether window size changed */
  4680.     if (ttgwsiz() > 0) {
  4681.         if (tt_rows > 0 && tt_cols > 0) {
  4682.             cmd_rows = tt_rows;
  4683.             cmd_cols = tt_cols;
  4684.         }
  4685.     }
  4686. #endif /* OS2 */
  4687. #endif /* CK_TTGWSIZ */
  4688.  
  4689.     diractive = 1;
  4690.     cmifn1 = nolinks | 1;               /* 1 = files or directories */
  4691.     cmifn2 = 0;                         /* 0 = not directories only */
  4692.  
  4693.   again:
  4694.  
  4695.     cmfdbi(&sw,                         /* First FDB - command switches */
  4696.            _CMKEY,                      /* fcode */
  4697.            "Enter or Return to confirm the command, or\n\
  4698.  file specification, or switch",
  4699.            "",                          /* default */
  4700.            "",                          /* addtl string data */
  4701.            ndirswtab,                   /* addtl numeric data 1: tbl size */
  4702.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4703.            xxstring,                    /* Processing function */
  4704.            dirswtab,                    /* Keyword table */
  4705.            &fi                          /* Pointer to next FDB */
  4706.            );
  4707.     cmfdbi(&fi,                         /* 2nd FDB - filespec to match */
  4708.            _CMIFI,                      /* fcode */
  4709.            "File specification",        /* hlpmsg */
  4710. #ifdef datageneral
  4711.            "+",                         /* Default filespec is wildcard */
  4712. #else                                   /* that matches all files... */
  4713. #ifdef VMS
  4714.            "*.*",
  4715. #else
  4716.            "*",
  4717. #endif /* VMS */
  4718. #endif /* datageneral */
  4719.            "",                          /* addtl string data */
  4720.            cmifn1,
  4721.            cmifn2,                      /* 1 = only dirs; 0 files or dirs */
  4722.            xxstring,
  4723.            NULL,
  4724.            &fl
  4725.            );
  4726.     cmfdbi(&fl,                         /* Anything that doesn't match */
  4727.            _CMFLD,                      /* fcode */
  4728.            "",                          /* hlpmsg */
  4729.            "",                          /* default */
  4730.            "",                          /* addtl string data */
  4731.            0,                           /* addtl numeric data 1 */
  4732.            0,                           /* addtl numeric data 2 */
  4733.            xxstring,
  4734.            NULL,
  4735.            NULL
  4736.            );
  4737.     while (1) {                         /* Parse 0 or more switches */
  4738.         x = cmfdb(&sw);                 /* Parse something */
  4739.         debug(F101,"domydir cmfdb","",x);
  4740.         if (x < 0)
  4741.           return(x);
  4742.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  4743.           break;
  4744.         c = cmgbrk();
  4745.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4746.             printf("?This switch does not take an argument\n");
  4747.             return(-9);
  4748.         }
  4749.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4750.             printf("?This switch requires an argument\n");
  4751.             return(-9);
  4752.         }
  4753.         switch (k = cmresult.nresult) {
  4754.           case DIR_BRF: verbose = 0; break;
  4755.           case DIR_VRB: verbose = 1; break;
  4756. #ifdef CK_TTGWSIZ
  4757.           case DIR_PAG: page = 1;    break;
  4758.           case DIR_NOP: page = 0;    break;
  4759. #endif /* CK_TTGWSIZ */
  4760.           case DIR_ISO: engdate = 0; break;
  4761.           case DIR_DAT: engdate = 1; break;
  4762.           case DIR_HDG: heading = 1; break;
  4763.           case DIR_NOH: heading = 0; break;
  4764. #ifdef UNIXOROSK
  4765.           case DIR_DOT: matchdot = 1; break;
  4766.           case DIR_NOD: matchdot = 0; break;
  4767. #endif /* UNIXOROSK */
  4768.           case DIR_ALL:
  4769.             show = 3;
  4770.             cmifn1 |= 1;
  4771.             cmifn2 = 0;
  4772.             goto again;
  4773.           case DIR_DIR:
  4774.             show = 2;
  4775.             cmifn1 |= 1;
  4776.             cmifn2 = 1;
  4777.             goto again;
  4778.           case DIR_FIL:
  4779.             show = 1;
  4780.             cmifn1 &= ~(1);
  4781.             cmifn2 = 0;
  4782.             goto again;
  4783.           case DIR_SRT:
  4784.             x = DIRS_NM;
  4785.             if (c == ':' || c == '=')
  4786.               if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4787.                 return(x);
  4788.             xsort = 1;
  4789.             sortby = x;
  4790.             break;
  4791.  
  4792.           case DIR_BUP: backup  = 1; fs++;   break;
  4793.           case DIR_NOB: backup  = 0; fs++;   break;
  4794.  
  4795.           case DIR_NOS: xsort = 0;     break;
  4796.           case DIR_ASC: reverse = 0;   break;
  4797.           case DIR_DSC: reverse = 1;   break;
  4798. #ifdef RECURSIVE
  4799.           case DIR_REC: recursive = 1; diractive = 1; break;
  4800.           case DIR_NOR: recursive = 0; diractive = 0; break;
  4801. #endif /* RECURSIVE */
  4802.           case DIR_TYP: xfermod = 1;   break;
  4803.           case DIR_NOT: xfermod = 0;   break;
  4804.  
  4805. #ifdef CKSYMLINK
  4806.           case DIR_LNK:                 /* Follow links */
  4807.             nolinks = 0;
  4808.             cmifn1 &= ~(2);
  4809.             goto again;
  4810.           case DIR_NLK:                 /* Don't follow links */
  4811.             nolinks = 2;
  4812.             cmifn1 &= ~(2);
  4813.             goto again;
  4814. #endif /* CKSYMLINK */
  4815.  
  4816.           case DIR_NOM: msg     = 0;   break;
  4817.           case DIR_MSG:
  4818.             if (c == ':' || c == '=')
  4819.               if ((x = cmfld("Message to append to each line",
  4820.                              "",
  4821.                              &s,
  4822.                              xxstring
  4823.                              )) < 0)
  4824.                 return(x);
  4825.             msg = 1;
  4826.             ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4827.             break;
  4828.  
  4829.           case DIR_SMA:
  4830.           case DIR_LAR:
  4831.             if (!getval) break;
  4832.             if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  4833.               return(x);
  4834.             fs++;
  4835.             show = 1;
  4836.             switch (cmresult.nresult) {
  4837.               case DIR_SMA: minsize = y; break;
  4838.               case DIR_LAR: maxsize = y; break;
  4839.             }
  4840.             break;
  4841.  
  4842. #ifndef NOSPL
  4843.           case DIR_ARR:
  4844.             if (c != ':' && c != '=') {
  4845.                 printf("?Array name required\n");
  4846.                 return(-9);
  4847.             }
  4848.             if ((x = cmfld("Array name (a single letter will do)",
  4849.                            "",
  4850.                            &s,
  4851.                            NULL
  4852.                            )) < 0) {
  4853.                 if (x == -3) {
  4854.                     printf("?Array name required\n");
  4855.                     return(-9);
  4856.                 } else
  4857.                   return(x);
  4858.             }
  4859.             if (!*s) {
  4860.                 printf("?Array name required\n");
  4861.                 return(-9);
  4862.             }
  4863.             s2 = s;
  4864.             if (*s == CMDQ) s++;
  4865.             if (*s == '&') s++;
  4866.             if (!isalpha(*s)) {
  4867.                 printf("?Bad array name - \"%s\"\n",s2);
  4868.                 return(-9);
  4869.             }
  4870.             array = *s++;
  4871.             if (isupper(array)) array = tolower(array);
  4872.             if (*s && (*s != '[' || *(s+1) != ']')) {
  4873.                 printf("?Bad array name - \"%s\"\n",s2);
  4874.                 return(-9);
  4875.             }
  4876.             break;
  4877. #endif /* NOSPL */
  4878.           case DIR_AFT:
  4879.           case DIR_BEF:
  4880.           case DIR_NAF:
  4881.           case DIR_NBF:
  4882.             if (!getval) break;
  4883.             if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  4884.                 if (x == -3) {
  4885.                     printf("?Date-time required\n");
  4886.                     rc = -9;
  4887.                 } else
  4888.                   rc = x;
  4889.                 goto xdomydir;
  4890.             }
  4891.             fs++;
  4892.             switch (k) {
  4893.               case DIR_AFT: makestr(&dir_aft,s); break;
  4894.               case DIR_BEF: makestr(&dir_bef,s); break;
  4895.               case DIR_NAF: makestr(&dir_naf,s); break;
  4896.               case DIR_NBF: makestr(&dir_nbf,s); break;
  4897.             }
  4898.             break;
  4899.           case DIR_EXC:
  4900.             if (!getval) break;
  4901.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4902.                 if (x == -3) {
  4903.                     printf("?Pattern required\n");
  4904.                     rc = -9;
  4905.                 } else
  4906.                   rc = x;
  4907.                 goto xdomydir;
  4908.             }
  4909.             fs++;
  4910.             makestr(&dir_exc,s);
  4911.             break;
  4912.  
  4913.           case DIR_SUM:
  4914.             summary = 1; break;
  4915.  
  4916.           case DIR_BIN: {
  4917.               extern struct keytab txtbin[];
  4918.               extern int xfiletype;
  4919.               if (!getval) break;
  4920.               if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0) {
  4921.                   rc = x;
  4922.                   goto xdomydir;
  4923.               }
  4924.               if (x == 2) {
  4925.                   xfiletype = -1;
  4926.               } else {
  4927.                   xfiletype = x;
  4928.                   fs = 1;
  4929.               }
  4930.               break;
  4931.           }
  4932.           case DIR_OUT:
  4933.             if ((x = cmofi("File for directory listing","",&s,xxstring)) < 0)
  4934.               return(x);
  4935.             ckstrncpy(outfile,s,CKMAXPATH+1);
  4936.             break;
  4937.  
  4938.           default:
  4939.             printf("?Sorry, not implemented yet - \"%s\"\n", atmbuf);
  4940.             goto xdomydir;
  4941.         }
  4942.     }
  4943.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Safe copy of filespec */
  4944.  
  4945. /* ^^^ START MULTIPLE */
  4946.     
  4947.     while (1) {
  4948.     x = cmfld("Another filespec or Enter","",&s,xxstring);
  4949.     if (x == -3)
  4950.       break;
  4951.     if (x < 0)
  4952.       return(x);
  4953.     ckstrncat(line,",",LINBUFSIZ);
  4954.     ckstrncat(line,s,LINBUFSIZ);
  4955.     multiple++;
  4956.     }
  4957.     ckmakmsg(tmpbuf,TMPBUFSIZ,"{",line,"}",NULL);
  4958.     ckstrncpy(line,tmpbuf,LINBUFSIZ);
  4959.     cmresult.nresult = 1;
  4960.     cmresult.fcode = _CMIFI;
  4961.  
  4962. /* ^^^ END MULTIPLE */
  4963.  
  4964.     s = line;
  4965.  
  4966.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  4967.       return(x);
  4968.     if (cmresult.fcode != _CMIFI) {     /* Nothing matched */
  4969.         char * m;
  4970.         if (*s == '/')
  4971. #ifdef UNIXOROSK
  4972.           m = "does not match switch or name of accessible file";
  4973. #else
  4974. #ifdef OS2
  4975.           m = "does not match switch or name of accessible file";
  4976. #else
  4977.           m = "no switches match";
  4978. #endif /* OS2 */
  4979. #endif /* UNIXOROSX */
  4980.         else
  4981.           m = "not found or not accessible";
  4982.         printf("\"%s\" - %s\n",s,m);
  4983.         rc = -9;
  4984.         goto xdomydir;
  4985.     }
  4986.     wild = cmresult.nresult;            /* Wildcard was given? */
  4987.     debug(F111,"domydir cmifi2",s,wild);
  4988.  
  4989.     if (outfile[0]) {
  4990.         ofp = fopen(outfile,"w");       /* Open output file */
  4991.         if (!ofp) {
  4992.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  4993.             ofp = stdout;
  4994.             return(-9);
  4995.         }
  4996.         page = 0;
  4997.     }
  4998.  
  4999. #ifdef OS2
  5000.     if (!wild) {
  5001.         if (zchki(s) == -2) {           /* Found a directory */
  5002.             p = s + (int)strlen(s) - 1; /* Yes */
  5003.             if (*p == '\\' || *p == '/')
  5004.               strcat(s, "*");
  5005.             else if (*p == ':')
  5006.               strcat(s, "./*");
  5007.             else
  5008.               strcat(s, "/*");
  5009.             wild = 1;                   /* Now it's wild */
  5010.         }
  5011.     }
  5012. #else
  5013.     if (!wild) if (isdir(s)) {          /* Is it a directory? */
  5014.         p = s + (int)strlen(s) - 1;     /* Yes */
  5015. #ifdef VMS
  5016.         {
  5017.             /* Convert from FOO.DIR;1 to [x.FOO] if necessary */
  5018.             char buf[CKMAXPATH+1];
  5019.             debug(F000,"domydir directory 0",s,*p);
  5020.             if (cvtdir(s,buf,CKMAXPATH) > 0)
  5021.               ckstrncpy(line,buf,LINBUFSIZ);
  5022.         }
  5023. #endif /* VMS */
  5024.         debug(F000,"domydir directory 1",s,*p);
  5025. #ifdef VMS
  5026.         if (*p == ']' || *p == '>' || *p == ':')
  5027.           strcat(s, "*.*");
  5028. #else
  5029. #ifdef datageneral
  5030.         if (*p == ':')
  5031.           strcat(s, "+");
  5032.         else
  5033.           strcat(s, ":+");
  5034. #else
  5035. #ifdef STRATUS
  5036.         if (*p == '>')
  5037.           strcat(s, "*");
  5038.         else
  5039.           strcat(s, ">*");
  5040. #endif /* STRATUS */
  5041. #endif /* datageneral */
  5042. #endif /* VMS */
  5043.         wild = 1;                       /* Now it's wild */
  5044.         debug(F000,"domydir directory 2",s,*p);
  5045.     }
  5046. #endif /* OS2 */
  5047.  
  5048. #ifdef ZXREWIND
  5049. /* cmifi() already called nzxpand so we can just re-use the same list. */
  5050.     if (!multiple) {
  5051.     x = zxrewind();            /* Rewind the list */
  5052.     debug(F111,"domydir zxrewind",s,x);
  5053.     } else {
  5054. #endif /* ZXREWIND */
  5055.     nzxopts = (show == ZX_DIRONLY) ? ZX_DIRONLY :
  5056.       (show == ZX_FILONLY ? ZX_FILONLY : 0);
  5057.     if (matchdot)  nzxopts |= ZX_MATCHDOT;
  5058.     if (recursive) nzxopts |= ZX_RECURSE;
  5059.     x = nzxpand(s,nzxopts);             /* Expand file list */
  5060.     debug(F111,"domydir nzxpand",s,x);
  5061. #ifdef ZXREWIND
  5062.     }
  5063. #endif /* ZXREWIND */
  5064.  
  5065. #ifndef NOSPL
  5066.     if (array) {
  5067.         int n, xx;
  5068.         n = (x < 0) ? 0 : x;
  5069.         if ((xx = dclarray(array,n)) < 0) {
  5070.             printf("?Array declaration failure\n");
  5071.             rc = -9;
  5072.             goto xdomydir;
  5073.         }
  5074.         array = xx;
  5075.         ap = a_ptr[array];
  5076.         if (n < 1) {
  5077.             rc = 0;
  5078.             goto xdomydir;
  5079.         }
  5080.     } else
  5081. #endif /* NOSPL */
  5082.       if (x < 1) {
  5083. #ifdef CKROOT
  5084.           extern int ckrooterr;
  5085.           if (ckrooterr)
  5086.             printf("?Off limits: %s\n",s);
  5087.           else
  5088. #endif /* CKROOT */
  5089.             if (x == 0 && isdir(s))
  5090.               printf("?Empty directory - \"%s\"\n", s);
  5091.             else
  5092.               printf("?%s %s match - \"%s\"\n",
  5093.                      (x == 0) ? "No" : "Too many",
  5094.                      (show == 2) ? "directories" : "files",
  5095.                      s
  5096.                      );
  5097.           rc = -9;
  5098.           goto xdomydir;
  5099.     }
  5100.     nx = x;                             /* Remember how many files */
  5101.  
  5102.     if (msg) {
  5103.         makestr(&dirmsg,tmpbuf);
  5104.         dirmsglen = strlen(tmpbuf);
  5105.     }
  5106.  
  5107. #ifdef VMS
  5108.     cdp = zgtdir();                     /* Get current directory */
  5109.     debug(F110,"domydir VMS zgtdir",cdp,0);
  5110. #endif /* VMS */
  5111.  
  5112.     if (xsort && verbose) {             /* If sorting, allocate space */
  5113.         if (!(dirlist = (char **) malloc((x + 1) * sizeof(char **)))) {
  5114.             if (!quiet) {
  5115.                 printf("* Warning: Failure to allocate memory for sorting.\n");
  5116.                 printf("* Will proceed without sorting...\n");
  5117.             }
  5118.             xsort = 0;
  5119.         }
  5120.         debug(F101,"domydir sort malloc","",xsort);
  5121.     }
  5122.  
  5123.     /* Display the listing */
  5124.  
  5125. #ifndef NOSPL
  5126.     if (array)                          /* Storing instead of printing */
  5127.       heading = 0;
  5128. #endif /* NOSPL */
  5129.  
  5130.     if (heading) {                      /* If /HEADING print heading */
  5131.         zfnqfp(s,TMPBUFSIZ,tmpbuf);
  5132.         fprintf(ofp,"\nDirectory of %s\n\n",tmpbuf);
  5133.         n += 3;
  5134.     }
  5135.     if (page > -1)                      /* Paging */
  5136.       xaskmore = page;
  5137.  
  5138.     if (!verbose) {                     /* /BRIEF */
  5139.         if (outfile[0]) {               /* To file  */
  5140.             int k = 0;
  5141.             znext(name);
  5142.             while (name[0]) {           /* One line per file */
  5143.                 k++;
  5144.                 if (fs) if (fileselect(name,
  5145.                                        dir_aft,dir_bef,dir_naf,dir_nbf,
  5146.                                        minsize,maxsize,!backup,16,xlist) < 1) {
  5147.                     znext(name);
  5148.                     continue;
  5149.                 }
  5150.                 fprintf(ofp,"%s\n",name);
  5151.                 znext(name);
  5152.             }
  5153.             if (heading)
  5154.               fprintf(ofp,"Files: %d\n\n",k);
  5155.             rc = 1;
  5156.             goto xdomydir;
  5157.         } else {
  5158.             rc = filhelp(x,"","",n,0);
  5159.             if (rc < 0)
  5160.               goto xdomydir;
  5161.             if (heading && rc > 0)
  5162.               fprintf(ofp,"Files: %d\n\n",x); /* (Might scroll a line or 2) */
  5163.             rc = 1;
  5164.             goto xdomydir;
  5165.         }
  5166.     }
  5167.     ndirs = nfiles = nbytes = 0L;       /* Initialize counters */
  5168.  
  5169.     if (dir_exc)                        /* Have exception list? */
  5170.       makelist(dir_exc,xlist,16);    /* Yes, convert to array */
  5171.  
  5172.     diractive = 1;
  5173.     znext(name);                        /* Get next file */
  5174.     while (name[0]) {                   /* Loop for each file */
  5175.         if (fs) if (fileselect(name,
  5176.                        dir_aft,dir_bef,dir_naf,dir_nbf,
  5177.                        minsize,maxsize,!backup,16,xlist) < 1) {
  5178.             znext(name);
  5179.             continue;
  5180.         }
  5181.         len = zgetfs(name);             /* Get file length */
  5182.         debug(F111,"domydir zgetfs",name,len);
  5183. #ifdef VMSORUNIX
  5184.         itsadir = zgfs_dir;             /* See if it's a directory */
  5185. #else
  5186.         itsadir = (len == -2 || isdir(name));
  5187. #endif /* VMSOUNIX */
  5188.         debug(F111,"domydir itsadir",name,itsadir);
  5189.         if ((itsadir && (show == 1)) || (!itsadir && (show == 2))) {
  5190.             znext(name);
  5191.             continue;
  5192.         }
  5193.         /* Get here when we know we have selected this file */
  5194.  
  5195.         nmatches ++;
  5196.         if (itsadir) {                  /* Accumulate totals for summary */
  5197.             ndirs++;
  5198.         } else {
  5199.             nfiles++;
  5200.             nbytes += len;
  5201.         }
  5202.         if (summary) {                  /* Summary only, no detail */
  5203.             znext(name);
  5204.             continue;
  5205.         }
  5206. #ifndef NOSPL
  5207.         if (array) {
  5208.             debug(F111,"domydir array",name,nfiles);
  5209.             if (ap)
  5210.               makestr(&(ap[nmatches]),name);
  5211.             znext(name);
  5212.             continue;
  5213.         }
  5214. #endif /* NOSPL */
  5215.  
  5216. /*
  5217.   NOTE: The sprintf's in this routine should be safe.  They involve
  5218.   permission strings, date/time strings, and filenames, all of which have
  5219.   known maximum lengths; none of these items is input from users.  The
  5220.   destination buffers are large enough to hold maximum sizes for any and
  5221.   all items.
  5222. */
  5223.         dstr = zfcdat(name);            /* Get modification date/time */
  5224.         debug(F111,"domydir zcfdat",dstr,0);
  5225.         if (!dstr) dstr = "";
  5226.         {
  5227. /*
  5228.   Note that zfcdat() always returns "" or yyyymmdd hh:mm:ss, so any warnings
  5229.   about possible out-of-bounds dstr[] array refs do not apply.  This block of
  5230.   code is to stifle the warnings and also allows for any out-of-spec
  5231.   zfcdat() implementations.
  5232. */
  5233.             int x;
  5234.             char * p = "00000000 00:00:00";
  5235.             x = ckstrncpy(xbuf,dstr,32);
  5236.             if (x < 17) ckstrncpy(&xbuf[x],p+x,32-x);
  5237.             dstr = xbuf;
  5238.         }
  5239.         if (engdate) {                  /* English date requested? */
  5240.             short month, day, year, hour, minute, seconds;
  5241.             month = (dstr[4]-48)*10 + (dstr[5]-48);
  5242.             mstr  = (month > 0 && month <= 12) ? months[month-1] : "xxx";
  5243.             day   = (dstr[6]-48)*10 + (dstr[7]-48);
  5244.             year  = (((dstr[0]-48)*10 +
  5245.                       (dstr[1]-48))*10 +
  5246.                       (dstr[2]-48))*10 +
  5247.                       (dstr[3]-48);
  5248.             hour  = (dstr[9]-48)*10 + (dstr[10]-48);
  5249.             minute = (dstr[12]-48)*10 + (dstr[13]-48);
  5250.             seconds = (dstr[15]-48)*10 + (dstr[16]-48);
  5251.             sprintf(dbuf,               /* SAFE */
  5252.                     "%2d-%s-%4d %02d:%02d:%02d",
  5253.                     day,mstr,year,hour,minute,seconds
  5254.                     );
  5255.             dstr = dbuf;
  5256.         } else {                        /* ISO date */
  5257.             dbuf[0] = dstr[0];          /* yyyy */
  5258.             dbuf[1] = dstr[1];
  5259.             dbuf[2] = dstr[2];
  5260.             dbuf[3] = dstr[3];
  5261.             dbuf[4] = '-';
  5262.             dbuf[5] = dstr[4];          /* mm (numeric) */
  5263.             dbuf[6] = dstr[5];
  5264.             dbuf[7] = '-';
  5265.             dbuf[8] = dstr[6];          /* dd */
  5266.             dbuf[9] = dstr[7];
  5267.             strcpy(dbuf+10,dstr+8);     /* hh:mm:ss */
  5268.             dstr = dbuf;
  5269.         }
  5270.         dlen = strlen(dbuf);            /* Length of date */
  5271.         name[CKMAXPATH] = NUL;
  5272. #ifdef CK_PERMS
  5273. #ifdef VMSORUNIX
  5274.         p = ziperm(name);               /* Get permissions */
  5275.         debug(F110,"ziperm perms",p,0);
  5276. #else
  5277.         p = zgperm(name);
  5278.         debug(F110,"zgperm perms",p,0);
  5279. #endif /* VMSORUNIX */
  5280. #else
  5281.         p = NULL;
  5282.         debug(F110,"NULL perms",p,0);
  5283. #endif /* CK_PERMS */
  5284.  
  5285. #ifdef VMS
  5286.         /* Get relative name to save space -- VMS fullnames are long... */
  5287.         ckstrncpy(name,zrelname(name,cdp),CKMAXPATH);
  5288. #endif /* VMS */
  5289.  
  5290.         if (itsadir && len < 0) {       /* Directory */
  5291. #ifdef VMS
  5292.             sprintf(linebuf,"%-22s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5293. #else
  5294.             if (p)
  5295.               sprintf(linebuf,"%10s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5296.             else
  5297.               sprintf(linebuf,"%-10s  %s  %s", "<DIR>", dstr, name);
  5298. #endif /* VMS */
  5299.         } else {                        /* Regular file */
  5300. #ifdef VMS
  5301.             sprintf(linebuf,"%-22s%10ld  %s  %s", p, len, dstr, name);
  5302. #else
  5303.             if (p)
  5304.               sprintf(linebuf,"%10s%10ld  %s  %s", p, len, dstr, name);
  5305.             else
  5306.               sprintf(linebuf,"%10ld  %s  %s", len, dstr, name);
  5307. #endif /* VMS */
  5308.         }
  5309. #ifdef UNIX
  5310. #ifdef CKSYMLINK
  5311.         if (zgfs_link) {
  5312.             int n, m;
  5313.             extern char linkname[];
  5314.             n = strlen(linebuf);
  5315.             m = strlen(linkname) + n;
  5316.             if (m < CKMAXPATH + 58)
  5317.               strcpy(linebuf+n, " -> "); /* safe (checked) */
  5318.             if (m + 4 < CKMAXPATH - 58)
  5319.               strcpy(linebuf+n+4, linkname); /* safe (checked) */
  5320.         } else
  5321. #endif /* CKSYMLINK */
  5322. #endif /* UNIX */
  5323.         if (xfermod) {                  /* Show transfer mode */
  5324.             int i, x, y;
  5325.             char * s = "";
  5326.             y = -1;
  5327.             x = scanfile(name,&y,nscanfile);
  5328.             switch (x) {
  5329.               case FT_TEXT: s = " (T)"; break;
  5330.               case FT_7BIT: s = " (T)(7BIT)"; break;
  5331.               case FT_8BIT: s = " (T)(8BIT)"; break;
  5332. #ifdef UNICODE
  5333.               case FT_UTF8: s = " (T)(UTF8)"; break;
  5334.               case FT_UCS2:
  5335.                 s = y ? " (T)(UCS2LE)" : " (T)(UCS2BE)";
  5336.                 break;
  5337. #endif /* UNICODE */
  5338.               case FT_BIN:  s = " (B)"; break;
  5339.             }
  5340.             if (!*s) {
  5341.                 s = binary ? " (B)" : " (T)";
  5342.             }
  5343.             if (*s) {
  5344.                 int n;
  5345.                 n = strlen(linebuf);
  5346.                 if (n + 4 < CKMAXPATH - 58)
  5347.                   strcpy(linebuf+n, s); /* safe (checked) */
  5348.             }
  5349.         }
  5350.         if (msg && dirmsg) {
  5351.             int n;
  5352.             n = strlen(linebuf);
  5353.             if (n + dirmsglen + 2 < CKMAXPATH)
  5354.               sprintf((char *)(linebuf+n)," %s", dirmsg); /* SAFE */
  5355.         }
  5356.         if (xsort) {                    /* Sorting - save line */
  5357.             i = strlen(linebuf);
  5358.             if ((ndirlist >= nx) ||
  5359.                 !(dirlist[ndirlist] = (char *)malloc(i+1))) {
  5360.                 printf("?Memory allocation error - try /NOSORT\n");
  5361.                 rc = -9;
  5362.                 goto xdomydir;
  5363.             }
  5364.             strcpy(dirlist[ndirlist],linebuf); /* safe */
  5365.             ndirlist++;
  5366.         }
  5367.         znext(name);                    /* Peek ahead to next file */
  5368.  
  5369.         if (!xsort) {
  5370.             fprintf(ofp,"%s\n",linebuf);
  5371.             if (page && (name[0] || heading)) { /* If /PAGE */
  5372.                 if (cmd_cols > 0) {
  5373.                     int x = strlen(linebuf);
  5374.                     int y;
  5375.                     y = (x % cmd_cols) ? 1 : 0;
  5376.                     n += x / cmd_cols + y;
  5377.                 } else {
  5378.                     n++;
  5379.                 }
  5380. #ifdef CK_TTGWSIZ
  5381.                 if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5382.                     if (!askmore()) {
  5383.                         rc = 0;
  5384.                         goto xdomydir;
  5385.                     } else
  5386.                       n = 0;
  5387.                 }
  5388. #endif /* CK_TTGWSIZ */
  5389.             }
  5390.         }
  5391.     }
  5392. #ifndef NOSPL
  5393.     if (array) {
  5394.         if (ap)
  5395.           makestr(&(ap[0]),ckitoa(nmatches));
  5396.         rc = 1;
  5397.         goto xdomydir;
  5398.     }
  5399. #endif /* NOSPL */
  5400.     if (xsort) {
  5401.         skey = 0;
  5402. #ifdef VMS
  5403.         switch (sortby) {
  5404.           case DIRS_NM: skey = dlen + 35; break;
  5405.           case DIRS_DT: skey = 33; break;
  5406.           case DIRS_SZ: skey = 21;
  5407.         }
  5408. #else
  5409.         if (p) {
  5410.             switch (sortby) {
  5411.               case DIRS_NM: skey = dlen + 24; break;
  5412.               case DIRS_DT: skey = 22; break;
  5413.               case DIRS_SZ: skey = 10;
  5414.             }
  5415.         } else {
  5416.             switch (sortby) {
  5417.               case DIRS_NM: skey = dlen + 14; break;
  5418.               case DIRS_DT: skey = 12; break;
  5419.               case DIRS_SZ: skey = 0;
  5420.             }
  5421.         }
  5422. #endif /* VMS */
  5423.         sh_sort(dirlist,NULL,ndirlist,skey,reverse,filecase);
  5424.         for (i = 0; i < ndirlist; i++) {
  5425.             fprintf(ofp,"%s\n",dirlist[i]);
  5426.             if (page && (i < ndirlist -1 || heading)) { /* If /PAGE */
  5427.                 if (cmd_cols > 0) {
  5428.                     int x = strlen(dirlist[i]);
  5429.                     int y;
  5430.                     y = (x % cmd_cols) ? 1 : 0;
  5431.                     n += ((int)strlen(dirlist[i]) / cmd_cols) + y;
  5432.                 } else {
  5433.                     n++;
  5434.                 }
  5435. #ifdef CK_TTGWSIZ
  5436.                 if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5437.                     if (!askmore()) {
  5438.                         rc = 0;
  5439.                         goto xdomydir;
  5440.                     } else
  5441.                       n = 0;
  5442.                 }
  5443. #endif /* CK_TTGWSIZ */
  5444.             }
  5445.         }
  5446.     }
  5447.  
  5448.     if (heading || summary) {
  5449. #ifdef CKFLOAT
  5450.         CKFLOAT gm;
  5451. #endif /* CKFLOAT */
  5452.         fprintf(ofp,"\n%ld director%s, %ld file%s, %ld byte%s",
  5453.                ndirs,
  5454.                (ndirs == 1) ? "y" : "ies",
  5455.                nfiles,
  5456.                (nfiles == 1) ? "" : "s",
  5457.                nbytes,
  5458.                (nbytes == 1) ? "" : "s"
  5459.                );
  5460. #ifdef CKFLOAT
  5461.         gm = ((CKFLOAT) nbytes ) / 1000000.0;
  5462.         if (gm > 1000.0)
  5463.           fprintf(ofp," (%0.2fGB)",(gm / 1000.0));
  5464.         else if (gm >= 0.01)
  5465.           fprintf(ofp," (%0.2fMB)",gm);
  5466. #endif /* CKFLOAD */
  5467.         fprintf(ofp,"\n\n");
  5468.     }
  5469.   xdomydir:
  5470.     if (g_matchdot > -1) {
  5471.         matchdot = g_matchdot;          /* Restore these... */
  5472.         g_matchdot = -1;
  5473.     }
  5474.     freedirlist();
  5475.     if (ofp != stdout) {                /* Close any output file */
  5476.         if (ofp) fclose(ofp);
  5477.         ofp = stdout;
  5478.     }
  5479.     if (rc > 0)
  5480.       success = 1;
  5481.     return(rc);
  5482. }
  5483.  
  5484. int
  5485. dodir(cx) int cx; {                     /* Do the DIRECTORY command */
  5486.     char *dc , *msg;
  5487.  
  5488. #ifdef OS2
  5489.     return(domydir());
  5490. #else /* OS2 */
  5491.     if (nopush
  5492. #ifdef DOMYDIR                          /* Builds that domydir() by default */
  5493.         || (cx == XXDIR || cx == XXLDIR)
  5494. #endif /* DOMYDIR */
  5495.         )
  5496.       return(domydir());                /* Built-in directory command */
  5497.  
  5498.     /* Use the system's directory command. */
  5499.  
  5500.     msg = (cx == XXLS) ?
  5501.       "Arguments for ls" :
  5502.         "Directory and/or file specification";
  5503.     if ((x = cmtxt(msg,"",&s,xxstring)) < 0)
  5504.       return(x);
  5505.  
  5506.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);      /* Copy the filespec */
  5507.     s = tmpbuf;
  5508.  
  5509.     if ((y = cmcfm()) < 0) return(y);
  5510.  
  5511.     lp = line;
  5512.     if (!(dc = getenv("CK_DIR")))
  5513.       dc = DIRCMD;
  5514.     ckmakmsg(lp,LINBUFSIZ,dc," ",s,NULL);
  5515.     debug(F110,"DIR",line,0);
  5516. #ifdef VMS
  5517.     conres();
  5518. #endif /* VMS */
  5519.     x = zshcmd(line);
  5520. #ifdef VMS
  5521.     concb((char)escape);
  5522. #endif /* VMS */
  5523.     return(success = (x < 1) ? 0 : 1);
  5524. #endif /* OS2 */
  5525. }
  5526.  
  5527. #ifndef NOSERVER
  5528. #ifndef NOFRILLS
  5529. /* Do the ENABLE and DISABLE commands */
  5530.  
  5531. int
  5532. doenable(y,x) int y, x; {
  5533. #ifdef CK_LOGIN
  5534.     if (isguest)                        /* IKSD: Don't let guests */
  5535.       return(0);                        /* enable anything that's disabled */
  5536. #endif /* CK_LOGIN */
  5537.     switch (x) {
  5538.       case EN_ALL:
  5539.         en_cwd = en_cpy = en_del = en_dir = en_fin = en_get = y;
  5540.         en_ren = en_sen = en_set = en_spa = en_typ = en_ret = y;
  5541.         if (!inserver)
  5542.           en_who = en_mai = en_pri = y;
  5543.         en_mkd = en_rmd = y;
  5544.         en_xit = y;
  5545. #ifndef datageneral
  5546.         en_bye = y;
  5547. #endif /* datageneral */
  5548. #ifndef NOPUSH
  5549.         if (!nopush && !inserver)
  5550.           en_hos = y;
  5551. #endif /* NOPUSH */
  5552. #ifndef NOSPL
  5553.         en_asg = en_que = y;
  5554. #endif /* NOSPL */
  5555.         break;
  5556.  
  5557.       case EN_BYE:
  5558. #ifndef datageneral
  5559. /*
  5560.   In Data General AOS/VS Kermit can't log out its superior process.
  5561. */
  5562.         en_bye = y;
  5563. #endif /* datageneral */
  5564.         break;
  5565.       case EN_CPY:
  5566.         en_cpy = y;
  5567.         break;
  5568.       case EN_CWD:
  5569.         en_cwd = y;
  5570. #ifdef IKSD
  5571.         if (inserver && y == 0) {
  5572.             fnrpath = PATH_OFF;
  5573.             fnspath = PATH_OFF;
  5574.         }
  5575. #endif /* IKSD */
  5576.         break;
  5577.       case EN_DEL:                      /* Deleting of files */
  5578.         en_del = y;
  5579.         break;
  5580.       case EN_DIR:
  5581.         en_dir = y;
  5582.         break;
  5583.       case EN_FIN:
  5584.         en_fin = y;
  5585.         break;
  5586.       case EN_GET:
  5587.         en_get = y;
  5588.         break;
  5589. #ifndef NOPUSH
  5590.       case EN_HOS:
  5591.         if (!nopush)
  5592.          en_hos = y;
  5593.         break;
  5594. #endif /* NOPUSH */
  5595.       case EN_REN:
  5596.         en_ren = y;
  5597.         break;
  5598.       case EN_SEN:
  5599.         en_sen = y;
  5600.         break;
  5601.       case EN_SET:
  5602.         en_set = y;
  5603.         break;
  5604.       case EN_SPA:
  5605.         en_spa = y;
  5606.         break;
  5607.       case EN_TYP:
  5608.         en_typ = y;
  5609.         break;
  5610.       case EN_WHO:
  5611.         en_who = y;
  5612.         break;
  5613. #ifndef NOSPL
  5614.       case EN_ASG:
  5615.         en_asg = y;
  5616.         break;
  5617.       case EN_QUE:
  5618.         en_que = y;
  5619.         break;
  5620. #endif /* NOSPL */
  5621.       case EN_RET:
  5622.         en_del = y;
  5623.         break;
  5624.       case EN_MAI:
  5625. #ifdef CK_LOGIN
  5626.         if (isguest && y) {
  5627.             printf("?Sorry, not valid for guests\n");
  5628.             return(-9);
  5629.         }
  5630. #endif /* CK_LOGIN */
  5631.         en_mai = y;
  5632.         break;
  5633.       case EN_PRI:
  5634. #ifdef CK_LOGIN
  5635.         if (isguest && y) {
  5636.             printf("?Sorry, not valid for guests\n");
  5637.             return(-9);
  5638.         }
  5639. #endif /* CK_LOGIN */
  5640.         en_pri = y;
  5641.         break;
  5642.       case EN_MKD:
  5643.         en_mkd = y;
  5644.         break;
  5645.       case EN_RMD:
  5646.         en_rmd = y;
  5647.         break;
  5648.       case EN_XIT:
  5649.         en_xit = y;
  5650.         break;
  5651.       case EN_ENA:
  5652.         if (((y & 1) && !(en_ena & 1)) ||
  5653.             ((y & 2) && !(en_ena & 2))) {
  5654.             printf("?Sorry, DISABLE ENABLE can not be undone\n");
  5655.             return(-9);
  5656.         } else {
  5657.             en_ena = y;
  5658.             break;
  5659.         }
  5660.       default:
  5661.         return(-2);
  5662.     }
  5663.     return(1);
  5664. }
  5665. #endif /* NOFRILLS */
  5666. #endif /* NOSERVER */
  5667.  
  5668. #ifndef NOFRILLS
  5669.  
  5670. static int del_lis = 0;
  5671. static int del_dot = 0;
  5672. static int del_hdg = 0;
  5673. static int del_pag = -1;
  5674. static int del_ask = 0;
  5675.  
  5676. #ifndef NOSHOW
  5677. VOID
  5678. showdelopts() {
  5679.     int x = 0;
  5680.     extern int optlines;
  5681.     prtopt(&optlines,"");
  5682.     prtopt(&optlines,"DELETE");
  5683.     if (del_ask > -1) {
  5684.         prtopt(&optlines, del_ask ? "/ASK" : "/NOASK");
  5685.         x++;
  5686.     }
  5687. #ifdef UNIXOROSK
  5688.     if (del_dot > -1) {
  5689.         prtopt(&optlines, del_dot ? "/DOTFILES" : "/NODOTFILES");
  5690.         x++;
  5691.     }
  5692. #endif /* UNIXOROSK */
  5693.     if (del_lis > -1) {
  5694.         prtopt(&optlines, del_lis ? "/LIST" : "/NOLIST");
  5695.         x++;
  5696.     }
  5697.     if (del_hdg > -1) {
  5698.         prtopt(&optlines, del_hdg ? "/HEADING" : "/NOHEADING");
  5699.         x++;
  5700.     }
  5701. #ifndef CK_TTGWSIZ
  5702.     if (del_pag > -1) {
  5703.         prtopt(&optlines, del_pag ? "/PAGE" : "/NOPAGE");
  5704.         x++;
  5705.     }
  5706. #endif /* CK_TTGWSIZ */
  5707.     if (!x) prtopt(&optlines,"(no options set)");
  5708.     prtopt(&optlines,"");
  5709. }
  5710. #endif /* NOSHOW */
  5711.  
  5712.  
  5713. int
  5714. setdelopts() {
  5715.     int x_lis = -1, x_pag = -1, x_dot = -1, x_hdg = -1, x_ask = -1;
  5716.     int getval = 0;
  5717.     char c;
  5718.     while (1) {
  5719.         if ((y = cmswi(deltab,ndeltab,"Switch","",xxstring)) < 0) {
  5720.             if (y == -3)
  5721.               break;
  5722.             else
  5723.               return(y);
  5724.         }
  5725.         c = cmgbrk();
  5726.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5727.             printf("?This switch does not take an argument\n");
  5728.             return(-9);
  5729.         }
  5730.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  5731.             printf("?This switch requires an argument\n");
  5732.             return(-9);
  5733.         }
  5734.         switch (y) {
  5735.           case DEL_DOT:
  5736.             x_dot = 1;
  5737.             break;
  5738.           case DEL_NOD:
  5739.             x_dot = 0;
  5740.             break;
  5741.           case DEL_HDG:
  5742.             x_hdg = 1;
  5743.             break;
  5744.           case DEL_LIS:
  5745.             x_lis = 1;
  5746.             break;
  5747.           case DEL_NOL:
  5748.             x_lis = 0;
  5749.             break;
  5750. #ifndef CK_TTGWSIZ
  5751.           case DEL_PAG:
  5752.             x_pag = 1;
  5753.             break;
  5754.           case DEL_NOP:
  5755.             x_pag = 0;
  5756.             break;
  5757. #endif /* CK_TTGWSIZ */
  5758.           case DEL_QUI:
  5759.             x_lis = 0;
  5760.             break;
  5761.           case DEL_VRB:
  5762.             x_lis = 1;
  5763.             break;
  5764.           case DEL_ASK:
  5765.             x_ask = 1;
  5766.             break;
  5767.           case DEL_NAS:
  5768.             x_ask = 0;
  5769.             break;
  5770.           default:
  5771.             printf("?Sorry, this option can not be set\n");
  5772.             return(-9);
  5773.         }
  5774.     }
  5775.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  5776.       return(x);
  5777.     if (x_pag > -1) del_pag = x_pag;
  5778.     if (x_dot > -1) del_dot = x_dot;
  5779.     if (x_hdg > -1) del_hdg = x_hdg;
  5780.     if (x_lis > -1) del_lis = x_lis;
  5781.     if (x_ask > -1) del_ask = x_ask;
  5782.     return(success = 1);
  5783. }
  5784.  
  5785. #ifdef OS2
  5786. static char ** xmtchs = NULL;
  5787. static int xmtchn = 0;
  5788. #endif /* OS2 */
  5789.  
  5790. int
  5791. dodel() {                               /* DELETE */
  5792.     int i, j, k, x;
  5793.     int fs = 0;                         /* Need to call fileselect() */
  5794.     int len = 0;
  5795.     int bad = 0;
  5796.     int getval = 0, asking = 0;
  5797.     int simulate = 0, rc = 0;
  5798.     long minsize = -1L, maxsize = -1L;
  5799.     int havename = 0, confirmed = 0;
  5800.     int qflag = 0;
  5801.     int summary = 0;
  5802.     int deldirs = 0;
  5803.     int deltree = 0;
  5804.     int itsadir = 0;
  5805.     int argisdir = 0;
  5806.     int xmode = -1, scan = 0, skip = 0;
  5807. #ifdef COMMENT
  5808.     int pass = 0;
  5809. #endif /* COMMENT */
  5810.     char c;
  5811.     char * deldef = "";
  5812.     char safebuf[CKMAXPATH+1];
  5813.     struct FDB sw, fi, fl;
  5814.     char
  5815.       * del_aft = NULL,
  5816.       * del_bef = NULL,
  5817.       * del_naf = NULL,
  5818.       * del_nbf = NULL,
  5819.       * del_exc = NULL;
  5820.     int
  5821.       x_lis = 0,
  5822.       /* x_dot = -1, */
  5823.       x_hdg = 0;
  5824.  
  5825.     char * dxlist[8];
  5826.  
  5827.     for (i = 0; i < 8; i++) dxlist[i] = NULL;
  5828.  
  5829.     g_matchdot = matchdot;
  5830.  
  5831.     if (del_lis > -1) x_lis    = del_lis;
  5832.     if (del_dot > -1) matchdot = del_dot;
  5833.     if (del_hdg > -1) x_hdg    = del_hdg;
  5834.     if (del_pag > -1) xaskmore = del_pag;
  5835.     if (del_ask > -1) asking   = del_ask;
  5836.  
  5837.     diractive = 1;
  5838.     nolinks = 2;                        /* By default don't follow links */
  5839.  
  5840.     cmfdbi(&sw,                         /* First FDB - command switches */
  5841.            _CMKEY,                      /* fcode */
  5842.            "File specification;\n or switch",
  5843.            "",                          /* default */
  5844.            "",                          /* addtl string data */
  5845.            ndeltab,                     /* addtl numeric data 1: tbl size */
  5846.            4,                           /* addtl numeric data 2: 4 = cmswi */
  5847.            xxstring,                    /* Processing function */
  5848.            deltab,                      /* Keyword table */
  5849.            &fi                          /* Pointer to next FDB */
  5850.            );
  5851.     cmfdbi(&fl,                         /* Anything that doesn't match */
  5852.            _CMFLD,                      /* fcode */
  5853.            "",                          /* hlpmsg */
  5854.            "",                          /* default */
  5855.            "",                          /* addtl string data */
  5856.            0,                           /* addtl numeric data 1 */
  5857.            0,                           /* addtl numeric data 2 */
  5858.            xxstring,
  5859.            NULL,
  5860.            NULL
  5861.            );
  5862.   again:
  5863.     cmfdbi(&fi,                         /* 2nd FDB - file to delete */
  5864.            _CMIFI,                      /* fcode */
  5865.            "File(s) to delete",         /* hlpmsg */
  5866.            deldef,                      /* default */
  5867.            "",                          /* addtl string data */
  5868.            nolinks | deldirs,           /* 0 = files, 1 = files or dirs */
  5869.            0,                           /* 1 = dirs only */
  5870.            xxstring,
  5871.            NULL,
  5872.            &fl
  5873.            );
  5874.     while (!havename && !confirmed) {
  5875.         x = cmfdb(&sw);                 /* Parse something */
  5876.         if (x < 0) {                    /* Error */
  5877.             if (x == -3)
  5878.               break;
  5879.             if (x == -2 || x == -9)
  5880.               printf("?Does not match switch or filename: \"%s\"\n",atmbuf);
  5881.             return(x);
  5882.         }
  5883.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  5884.           break;
  5885.         c = cmgbrk();                   /* Get break character */
  5886.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5887.             printf("?This switch does not take an argument\n");
  5888.             rc = -9;
  5889.             goto xdelete;
  5890.         }
  5891.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  5892.             printf("?This switch requires an argument\n");
  5893.             rc = -9;
  5894.             goto xdelete;
  5895.         }
  5896.         switch (k = cmresult.nresult) {
  5897.           case DEL_AFT:
  5898.           case DEL_BEF:
  5899.           case DEL_NAF:
  5900.           case DEL_NBF:
  5901.             if (!getval) break;
  5902.             if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  5903.                 if (x == -3) {
  5904.                     printf("?Date-time required\n");
  5905.                     x = -9;
  5906.                 } else
  5907.                   rc = x;
  5908.                 goto xdelete;
  5909.             }
  5910.             fs++;
  5911.             deltree = 0;
  5912.             switch (k) {
  5913.               case DEL_AFT: makestr(&del_aft,s); break;
  5914.               case DEL_BEF: makestr(&del_bef,s); break;
  5915.               case DEL_NAF: makestr(&del_naf,s); break;
  5916.               case DEL_NBF: makestr(&del_nbf,s); break;
  5917.             }
  5918.             break;
  5919.           case DEL_DOT:
  5920.             matchdot = 1;
  5921.             break;
  5922.           case DEL_NOD:
  5923.             matchdot = 0;
  5924.             break;
  5925.           case DEL_ALL:
  5926.             fs = 0;
  5927. #ifdef VMS
  5928.             deldef = "*.*";             /* UNIX, Windows, OS/2 */
  5929. #else
  5930. #ifdef datageneral
  5931.             deldef = "+";               /* AOS/VS */
  5932. #else
  5933.             deldef = "*";               /* UNIX, Windows, OS/2, VMS... */
  5934. #endif /* datageneral */
  5935. #endif /* VMS */
  5936.             deltree = 1;
  5937.             nolinks = 2;
  5938.             matchdot = 1;
  5939.             recursive = 1;              /* Fall through purposely... */
  5940.           case DEL_DIR:
  5941.             deldirs = 1;
  5942.             goto again;
  5943.           case DEL_EXC:
  5944.             if (!getval) break;
  5945.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5946.                 if (x == -3) {
  5947.                     printf("?Pattern required\n");
  5948.                     x = -9;
  5949.                 } else
  5950.                   rc = x;
  5951.                 goto xdelete;
  5952.             }
  5953.             fs++;
  5954.             deltree = 0;
  5955.             makestr(&del_exc,s);
  5956.             break;
  5957.           case DEL_HDG:
  5958.             x_hdg = 1;
  5959.             break;
  5960. #ifdef RECURSIVE
  5961.           case DEL_REC:
  5962.             recursive = 1;
  5963.             break;
  5964. #endif /* RECURSIVE */
  5965.           case DEL_LIS:
  5966.             x_lis = 1;
  5967.             break;
  5968.           case DEL_SUM:
  5969.             summary = 1;
  5970.             x_lis = 0;
  5971.             x_hdg = 1;
  5972.             break;
  5973.           case DEL_NOL:
  5974.             x_lis = 0;
  5975.             break;
  5976. #ifndef CK_TTGWSIZ
  5977.           case DEL_PAG:
  5978.             xaskmore = 1;
  5979.             break;
  5980.           case DEL_NOP:
  5981.             xaskmore = 0;
  5982.             break;
  5983. #endif /* CK_TTGWSIZ */
  5984.           case DEL_QUI:
  5985.             qflag = 1;
  5986.             x_lis = 0;
  5987.             break;
  5988.           case DEL_VRB:
  5989.             x_lis = 1;
  5990.             break;
  5991.  
  5992.           case DEL_SMA:
  5993.           case DEL_LAR:
  5994.             if (!getval) break;
  5995.             if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  5996.               return(x);
  5997.             fs++;
  5998.             deltree = 0;
  5999.             switch (cmresult.nresult) {
  6000.               case DEL_SMA: minsize = y; break;
  6001.               case DEL_LAR: maxsize = y; break;
  6002.             }
  6003.             break;
  6004.  
  6005.           case DEL_SIM:
  6006.             simulate = 1;
  6007.             x_lis = 1;
  6008.             break;
  6009.           case DEL_ASK:
  6010.             asking = 1;
  6011.             break;
  6012.           case DEL_NAS:
  6013.             asking = 0;
  6014.             break;
  6015.           case DEL_TYP: {
  6016.               extern struct keytab txtbin[];
  6017.               if (!getval) break;
  6018.               if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  6019.                 return(x);
  6020.               if (x == 2) {             /* ALL */
  6021.                   xmode = -1;
  6022.               } else {                  /* TEXT or BINARY only */
  6023.                   xmode = x;
  6024.                   scan = 1;
  6025.               }
  6026.               break;
  6027.           }
  6028.           default:
  6029.             printf("?Not implemented yet - \"%s\"\n",atmbuf);
  6030.             return(-9);
  6031.         }
  6032.     }
  6033.     if (qflag && (cmresult.fcode == _CMFLD)) {
  6034.         if ((x = cmcfm()) < 0)
  6035.           return(x);
  6036.         else
  6037.           return(success = 0);
  6038.     }
  6039.     if (cmresult.fcode != _CMIFI) {
  6040.         if (*atmbuf) {
  6041.         int x;
  6042.             if (iswild(atmbuf) && nzxpand(atmbuf,nzxopts) == 0)
  6043.               printf("?No files match: %s\n",atmbuf);
  6044.             else if ((x = zchki(atmbuf)) == -1)
  6045.           printf("?File not found: %s\n",atmbuf);
  6046.         else if (x == -2)
  6047.           printf("?Not a regular file: %s\n",atmbuf);
  6048.         else
  6049.               /* printf("?Not a deletable file: %s\n",atmbuf); */
  6050.               goto tryanyway;
  6051.         } else {
  6052.             printf("?A file specification is required\n");
  6053.         }
  6054.         return(-9);
  6055.     }
  6056.   tryanyway:
  6057.     ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ); /* Safe copy of filespec */
  6058.     if (deldirs) {
  6059.         ckstrncpy(safebuf,cmresult.sresult,CKMAXPATH);
  6060. #ifdef VMSORUNIX
  6061.         len = zgetfs(tmpbuf);           /* Is it a directory name? */
  6062.         argisdir = zgfs_dir;            /* Then because of how zxpand() */
  6063.         if (argisdir && zgfs_link)      /* works, we have to add it to */
  6064.           argisdir = 0;                 /* the list. */
  6065.         if (itsadir)
  6066.           len = -2;
  6067. #else
  6068.         len = zchki(tmpbuf);
  6069.         if (len < 0)
  6070.           argisdir = isdir(tmpbuf);
  6071. #endif /* VMSORUNIX */
  6072.     }
  6073.     debug(F110,"DELETE file",tmpbuf,0);
  6074.     if ((x = cmcfm()) < 0)
  6075.       return(x);
  6076.  
  6077. #ifdef IKSD
  6078. #ifdef CK_LOGIN
  6079.     if (inserver && isguest) {
  6080.         printf("?Sorry, DELETE unavailable to guests\n");
  6081.         return(-9);
  6082.     }
  6083. #endif /* CK_LOGIN */
  6084. #endif /* IKSD */
  6085.  
  6086. #ifndef OS2ORUNIX
  6087.     if (simulate) {
  6088.         printf("?Sorry, /SIMULATE not implemented on this platform\n");
  6089.         return(-9);
  6090.     }
  6091. #endif /* OS2ORUNIX */
  6092.  
  6093. #ifdef COMMENT
  6094.     /* (not needed) */
  6095.     if (!iswild(tmpbuf)) {
  6096.         char *m;
  6097.         errno = 0;
  6098.         x = zchki(tmpbuf);
  6099.         if (x < 0) {
  6100.             switch (x) {
  6101.               case -2: m = "Not a regular file"; break;
  6102.               case -1: m = "File not found or not accessible"; break;
  6103.               default: m = errno ? ck_errstr() : "Can't delete";
  6104.             }
  6105.             printf("?%s: \"%s\"\n",m,tmpbuf);
  6106.             return(-9);
  6107.         }
  6108.     }
  6109. #endif /* COMMENT */
  6110.  
  6111.     makelist(del_exc,dxlist,8);
  6112.  
  6113. /* tmpbuf[] has the name - now do any needed conversions on it */
  6114.  
  6115. #ifdef OS2
  6116.     {   /* Lower level functions change / to \, not good for CMD.EXE. */
  6117.         char *p = tmpbuf;
  6118.         while (*p) {                    /* Change them back to \ */
  6119.             if (*p == '/') *p = '\\';
  6120.             p++;
  6121.         }
  6122.     }
  6123. #endif /* OS2 */
  6124.  
  6125. #ifdef VMS
  6126.     if (iswild(tmpbuf)) {
  6127. #ifdef COMMENT
  6128.         /* Does not handle '.' as version separator */
  6129.         char *p = tmpbuf;
  6130.         x = 0;
  6131.         while (*p) {
  6132.             if (*p == ';') {
  6133.                 x = 1;
  6134.                 break;
  6135.             } else
  6136.               p++;
  6137.         }
  6138.         if (!x) ckstrncat(tmpbuf,";*",TMPBUFSIZ);
  6139. #else
  6140.         j = 0; x = 0;                   /* for end_dot and number of dots */
  6141.         i = strlen(tmpbuf);
  6142.         if (tmpbuf[i] == ';') {
  6143.             ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  6144.         } else {
  6145.             if (tmpbuf[i--] == '.')
  6146.               j++;
  6147.             for (; i >= 0; i--) {
  6148.                 if (tmpbuf[i] == ';' || tmpbuf[i] == ':' ||
  6149.                     tmpbuf[i] == ']' || tmpbuf[i] == '>')
  6150.                   break;
  6151.                 else if (tmpbuf[i] == '.')
  6152.                   x++;
  6153.             }
  6154.             if (tmpbuf[i] != ';') {     /* dot may have been used */
  6155.                 if (j) {                /* last char is dot */
  6156.                   if (x)                /* second is version separator */
  6157.                     ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  6158.                   else                  /* 'foo.' */
  6159.                     ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  6160.                 } else if (x == 1)      /* lacking a version separator */
  6161.                   ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  6162.                 else if (x == 0)        /* x == 2 has a version */
  6163.                   ckstrncat(tmpbuf,".*;0",TMPBUFSIZ);
  6164.             }
  6165.         }
  6166. #endif /* COMMENT */
  6167.     }
  6168. #endif /* VMS */
  6169.  
  6170.     debug(F110,"dodel tmpbuf",tmpbuf,0); /* Filename */
  6171.  
  6172. #ifndef OS2ORUNIX                       /* No built-in DELETE code... */
  6173.     /* Construct system command. */
  6174.     ckmakmsg(line,LINBUFSIZ,DELCMD," ",tmpbuf,NULL);
  6175. #else
  6176. #ifdef VMS
  6177.     if (asking) {                       /* Maybe overwrite in VMS */
  6178.         if (x_lis)                      /* if options are needed... */
  6179.           ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm/log ",tmpbuf,NULL);
  6180.         else
  6181.           ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm ",tmpbuf,NULL);
  6182.     } else if (x_lis)
  6183.       ckmakmsg(line,LINBUFSIZ,DELCMD," /log ",tmpbuf,NULL);
  6184.     conres();
  6185. #endif /* VMS */
  6186.  
  6187.     debug(F110,"dodel line",line,0);
  6188. #endif /* OS2ORUNIX */
  6189.  
  6190. #ifdef MAC
  6191.     success = (zdelet(tmpbuf) == 0);
  6192.  
  6193. #else  /* !MAC ... */
  6194.  
  6195. #ifdef OS2ORUNIX
  6196.     {
  6197.         int filespace = 0;
  6198.         int count = 0;
  6199.         int lines = 0;
  6200.         int n = 0;
  6201.  
  6202.         s = tmpbuf;
  6203.  
  6204. #ifdef CK_TTGWSIZ
  6205. #ifdef OS2
  6206.         ttgcwsz();
  6207. #else /* OS2 */
  6208.         /* Check whether window size changed */
  6209.         if (ttgwsiz() > 0) {
  6210.             if (tt_rows > 0 && tt_cols > 0) {
  6211.                 cmd_rows = tt_rows;
  6212.                 cmd_cols = tt_cols;
  6213.             }
  6214.         }
  6215. #endif /* OS2 */
  6216. #endif /* CK_TTGWSIZ */
  6217.  
  6218.         if (x_hdg > 0 && !summary) {
  6219.             printf("Deleting %s...%s\n", s, simulate ? " (SIMULATION)" : "");
  6220.             n += 2;
  6221.         }
  6222. #ifdef ZXREWIND
  6223.         z = zxrewind();                 /* Rewind file list */
  6224. #else
  6225.         if (!deldirs)
  6226.           nzxopts = ZX_FILONLY;
  6227.         if (recursive) nzxopts |= ZX_RECURSE;
  6228.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  6229.         errno = 0;
  6230.         z = nzxpand(s,nzxopts);         /* Expand file list */
  6231. #endif /* ZXREWIND */
  6232.         debug(F111,"dodel",s,z);
  6233.  
  6234.         /* If deleting directories, sort in reverse order */
  6235.         /* so we delete the files first, then the directory. */
  6236.  
  6237. #ifdef OS2
  6238.         /* In K95, we have no mtchs array, nor any control over */
  6239.         /* the order in which znext() returns filenames, so we  */
  6240.         /* must copy the array and sort it. */
  6241.         {
  6242.             int i;
  6243.             if (xmtchs) {               /* Free previous list in case */
  6244.                 debug(F101,"dodel freeing previous list","",xmtchn);
  6245.                 for (i = 0; i < xmtchn; i++) /* it wasn't freed last time. */
  6246.                   if (xmtchs[i])
  6247.                     free(xmtchs[i]);
  6248.                 free(xmtchs);
  6249.             }
  6250.             xmtchn = 0;
  6251.             xmtchs = (char **)malloc(z * (sizeof(char **))); /* Make new one */
  6252.             if (!xmtchs) {
  6253.                 printf("?Memory allocation failure\n");
  6254.                 return(-9);
  6255.             }
  6256.             for (i = 0; i < z; i++) {
  6257.                 xmtchs[i] = NULL;
  6258.                 znext(tmpbuf);
  6259.                 if (!*tmpbuf)
  6260.                   break;
  6261.                 makestr(&(xmtchs[i]),tmpbuf);
  6262.                 if (!xmtchs[i]) {
  6263.                     printf("?Memory allocation failure\n");
  6264.                     xmtchn = i - 1;
  6265.                     rc = -9;
  6266.                     goto xdelete;
  6267.                 }
  6268.                 /* debug(F111,"dodel add",xmtchs[i],i); */
  6269.             }
  6270.             xmtchn = i;
  6271.             debug(F101,"dodel xmtchn","",xmtchn);
  6272.             sh_sort(xmtchs,NULL,z,0,deldirs,0);
  6273.         }
  6274. #else
  6275. #ifdef UNIX
  6276.         sh_sort(mtchs,NULL,z,0,deldirs,filecase);
  6277. #endif /* UNIX */
  6278. #endif /* OS2 */
  6279.  
  6280.         if (z > 0) {
  6281.             int i;
  6282. #ifdef OS2
  6283.             int ix = 0;
  6284. #endif /* OS2 */
  6285.             success = 1;
  6286.             if (x_hdg > 0)
  6287.               printf("\n");
  6288.  
  6289.             while (
  6290. #ifdef OS2
  6291.                    ix < xmtchn
  6292. #else
  6293.                    1
  6294. #endif /* OS2 */
  6295.                    ) {                  /* Loop for all files */
  6296. #ifdef OS2
  6297.                 ckstrncpy(tmpbuf,xmtchs[ix++],TMPBUFSIZ);
  6298. #else
  6299.                 znext(tmpbuf);          /* Get next file */
  6300. #endif /* OS2 */
  6301.                 if (!*tmpbuf) {         /* No more */
  6302.                     if (deldirs && recursive && argisdir) {
  6303.                         ckstrncpy(tmpbuf,safebuf,TMPBUFSIZ);
  6304.                         argisdir = 0;   /* (only do this once) */
  6305.                     } else
  6306.                       break;
  6307.                 }
  6308.                 skip = 0;
  6309.                 if (!deltree) {
  6310.                     if (fs)
  6311.                       if (fileselect(tmpbuf,
  6312.                                      del_aft,del_bef,del_naf,del_nbf,
  6313.                                      minsize,maxsize,0,8,dxlist) < 1) {
  6314.                           skip++;
  6315.                       }
  6316.                 }
  6317.                 if (!skip && scan && itsadir) {
  6318.                     skip++;
  6319.                 }
  6320.                 if (!skip && scan) {
  6321.                       switch (scanfile(tmpbuf,&y,nscanfile)) {
  6322.                       case FT_BIN:
  6323.                         if (xmode != 1)
  6324.                           skip++;
  6325.                         break;
  6326.                       case FT_TEXT:
  6327.                       case FT_7BIT:
  6328.                       case FT_8BIT:
  6329. #ifdef UNICODE
  6330.                       case FT_UTF8:
  6331.                       case FT_UCS2:
  6332. #endif /* UNICODE */
  6333.                         if (xmode != 0)
  6334.                           skip++;
  6335.                     }
  6336.                 }
  6337.                 if (!skip && asking) {
  6338.                     int x;
  6339.                     ckmakmsg(line,LINBUFSIZ," Delete ",tmpbuf,"? ",NULL);
  6340.                     x = getyesno(line,3);
  6341.                     switch (x) {
  6342.                       case 0: continue;           /* no */
  6343.                       case 1: break;              /* yes */
  6344.                       case 2: goto xdelete;       /* quit */
  6345.                       case 3: asking = 0; break;  /* go */
  6346.                     }
  6347.                 }
  6348. #ifdef VMSORUNIX
  6349.                 len = zgetfs(tmpbuf);   /* Get length and accessibility */
  6350.                 itsadir = zgfs_dir;
  6351.                 if (itsadir && zgfs_link) { /* Treat links to directories */
  6352.                     itsadir = 0;        /* as regular files */
  6353.                     if (scan)           /* But not if /TYPE: was given */
  6354.                       skip++;
  6355.                 }
  6356.                 if (itsadir)            /* (emulate non-Unix code) */
  6357.                   len = -2;
  6358. #else
  6359.                 len = zchki(tmpbuf);    /* Get accessibility */
  6360.                 if (len < 0)            /* See if it's a directory */
  6361.                   itsadir = isdir(tmpbuf);
  6362. #endif /* VMSORUNIX */
  6363.  
  6364.                 if (skip) {
  6365. #ifdef COMMENT                          /* Too verbose */
  6366.                     if (x_lis > 0) {
  6367.                         lines++;
  6368.                         printf(" %s (SKIPPED)\n",tmpbuf);
  6369. #ifdef CK_TTGWSIZ
  6370.                         if (++n > cmd_rows - 3)
  6371.                           if (!askmore()) goto xdelete; else n = 0;
  6372. #endif /* CK_TTGWSIZ */
  6373.                     }
  6374. #endif /* COMMENT */
  6375.                     continue;
  6376.                 }
  6377.  
  6378.                 debug(F111,"DELETE len",tmpbuf,len);
  6379.                 if (simulate) {
  6380.                     filespace += len;
  6381.                     count++;
  6382.                     if (x_lis > 0) {
  6383.                         lines++;
  6384.                         printf(" %s (SELECTED)\n",tmpbuf);
  6385.                         if (++n > cmd_rows - 3) {
  6386.                             int xx;
  6387.                             xx = askmore();
  6388.                             if (!xx) goto xdelete; else n = 0;
  6389.                         }
  6390.                     }
  6391.                 } else if (len >= 0 || !itsadir) { /* Regular file */
  6392.                     zdelet(tmpbuf);                /* or symlink, etc... */
  6393.                     if (zchki(tmpbuf) < 0) {
  6394.                         filespace += len;
  6395.                         count++;
  6396.                         if (x_lis > 0) {
  6397.                             lines++;
  6398.                             printf(" %s (OK)\n",tmpbuf);
  6399.                             if (++n > cmd_rows - 3)
  6400.                               if (!askmore()) goto xdelete; else n = 0;
  6401.                         }
  6402.                     } else {
  6403.                         bad++;
  6404.                         success = 0;
  6405.                         if (x_lis > 0) {
  6406.                             lines++;
  6407.                             printf(" %s (FAILED: %s)\n",tmpbuf,ck_errstr());
  6408.                             if (++n > cmd_rows - 3)
  6409.                               if (!askmore()) goto xdelete; else n = 0;
  6410.                         }
  6411.                     }
  6412.                 } else if (/* pass > 0 && */ deldirs && itsadir) {
  6413.                     /* It's a directory */
  6414.                     if (zrmdir(tmpbuf) > -1) { /* Only works if empty */
  6415.                         count++;
  6416.                         if (x_lis > 0) {
  6417.                             lines++;
  6418.                             printf(" %s (OK)\n",tmpbuf);
  6419.                             if (++n > cmd_rows - 3)
  6420.                               if (!askmore()) goto xdelete; else n = 0;
  6421.                         }
  6422.                     } else {
  6423.                         success = 0;
  6424.                         if (x_lis > 0) {
  6425.                             lines++;
  6426.                             printf(" %s (FAILED: %s)\n",
  6427.                                    tmpbuf,
  6428.                                    ck_errstr());
  6429.                             if (++n > cmd_rows - 3)
  6430.                               if (!askmore()) goto xdelete; else n = 0;
  6431.                         }
  6432.                     }
  6433.                 } else if (x_lis > 0) {
  6434.                     lines++;
  6435.                     if (isdir(tmpbuf))
  6436.                       printf(" %s (FAILED: directory)\n",tmpbuf);
  6437.                     else
  6438.                       printf(" %s (FAILED: not a regular file)\n",tmpbuf);
  6439.                     if (++n > cmd_rows - 3)
  6440.                       if (!askmore()) goto xdelete; else n = 0;
  6441.                 }
  6442.             }
  6443.             if (x_hdg > 0) {
  6444.                 if (lines > 0)
  6445.                   printf("\n");
  6446.                 if (++n > cmd_rows - 3)
  6447.                   if (!askmore()) goto xdelete; else n = 0;
  6448.                 printf("%d file%s %sdeleted, %d byte%s %sfreed%s\n",
  6449.                        count,
  6450.                        count != 1 ? "s" : "",
  6451.                        simulate ? "would be " : "",
  6452.                        filespace,
  6453.                        filespace != 1 ? "s" : "",
  6454.                        simulate ? "would be " : "",
  6455.                        simulate ? " (maybe)" : ""
  6456.                        );
  6457.             }
  6458.             if (!x_lis && !success && !quiet) {
  6459.                 printf("?DELETE failed for %d file%s \
  6460. (use DELETE /LIST to see details)\n",
  6461.                        bad, bad == 1 ? "" : "s"
  6462.                        );
  6463.             }
  6464.         } else if (x_lis > 0) {
  6465.             if (errno)
  6466.               printf("?%s: %s\n",ck_errstr(), tmpbuf);
  6467.             else
  6468.               printf("?Can't delete: %s\n",tmpbuf);
  6469.         }
  6470.     }
  6471. #else /* OS2ORUNIX */
  6472. #ifndef VMS                             /* Others - let the system do it. */
  6473.     xsystem(line);
  6474.     x = nzxpand(tmpbuf,nzxopts);
  6475.     success = (x > 0) ? 0 : 1;
  6476.     if (x_hdg > 0)
  6477.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6478. #else
  6479.     if (asking)
  6480.       printf("\n");
  6481.     x = xsystem(line);                  /* zshcmd returns 1 for success */
  6482.     success = (x > 0) ? 1 : 0;
  6483.     if (x_hdg > 0 && !asking)
  6484.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6485.     concb((char)escape);
  6486. #endif /* VMS */
  6487. #endif /* OS2ORUNIX */
  6488. #endif /* MAC */
  6489.   xdelete:
  6490.     if (g_matchdot > -1) {
  6491.         matchdot = g_matchdot;          /* Restore these... */
  6492.         g_matchdot = -1;
  6493.     }
  6494. #ifdef OS2
  6495.     if (xmtchs) {
  6496.         int i;
  6497.         debug(F101,"dodel freeing list","",xmtchn);
  6498.         for (i = 0; i < xmtchn; i++)
  6499.           if (xmtchs[i]) free(xmtchs[i]);
  6500.         free(xmtchs);
  6501.         xmtchs = NULL;
  6502.         xmtchn = 0;
  6503.     }
  6504. #endif /* OS2 */
  6505.     debug(F101,"dodel result","",rc);
  6506.     return((rc < 0) ? rc : success);
  6507. }
  6508. #endif /* NOFRILLS */
  6509.  
  6510. #ifndef NOSPL                           /* The ELSE command */
  6511. _PROTOTYP( VOID pushqcmd, (char *) );
  6512.  
  6513. int
  6514. doelse() {
  6515.     if (!ifcmd[cmdlvl]) {
  6516.         printf("?ELSE doesn't follow IF\n");
  6517.         return(-2);
  6518.     }
  6519. #ifdef COMMENT
  6520. /*
  6521.   Wrong.  This prevents IF..ELSE IF...ELSE IF...ELSE IF...ELSE...
  6522.   from working.
  6523. */
  6524.     ifcmd[cmdlvl] = 0;
  6525. #endif /* COMMENT */
  6526.     if (!iftest[cmdlvl]) {              /* If IF was false do ELSE part */
  6527.         if (maclvl > -1 || tlevel > -1) { /* In macro or command file */
  6528.             debug(F100,"doelse pushing","",0);
  6529. #ifdef COMMENT
  6530.             pushcmd(NULL);              /* save rest of command. */
  6531. #else
  6532.             /* This fixes certain obscure problems */
  6533.             /* but breaks many other constructions that must work. */
  6534.             pushqcmd(NULL);
  6535. #endif /* COMMENT */
  6536.         } else {                        /* If interactive, */
  6537.             cmini(ckxech);              /* just start a new command */
  6538.             printf("\n");               /* (like in MS-DOS Kermit) */
  6539.             if (pflag) prompt(xxstring);
  6540.         }
  6541.     } else {                            /* Condition is false */
  6542.         if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  6543.           return(y);                    /* Gobble up rest of line */
  6544.     }
  6545.     return(0);
  6546. }
  6547. #endif /* NOSPL */
  6548.  
  6549. #ifndef NOSPL
  6550. int
  6551. doswitch() {
  6552.     char *lp, *ap;                      /* Macro argument pointer */
  6553.     int len = 0, x, y, pp = 0;
  6554.     char brbuf[3];
  6555.  
  6556.     /* Get variable name */
  6557.  
  6558.     tmpbuf[0] = NUL;
  6559.     brbuf[0] = '{';
  6560.     brbuf[1] = '}';
  6561.     brbuf[2] = NUL;
  6562.  
  6563.     y = cmfld("Variable name","",&s,xxstring);
  6564.     debug(F111,"doswitch cmfld",s,y);
  6565.     if (y < 0) {
  6566.         if (y == -3)            /* Because brstrip() writes */
  6567.           s = brbuf;            /* into its argument. */
  6568.         else
  6569.           return(y);
  6570.     }
  6571.     debug(F110,"doswitch A",s,0);
  6572.     if (!strcmp(s,"(")) {
  6573.         pp++;
  6574.         if ((y = cmfld("Variable name","",&s,xxstring)) < 0) {
  6575.             if (y == -3)
  6576.               s = brbuf;
  6577.             else
  6578.               return(y);
  6579.         debug(F110,"doswitch B",s,0);
  6580.         }
  6581.     }
  6582.     len = ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  6583.     if (tmpbuf[0] == CMDQ) {
  6584.         if (chkvar(s) < 1) {
  6585.             printf("?Variable name required\n");
  6586.             return(-9);
  6587.         }
  6588.     }
  6589.     if (pp > 0) {                       /* If open paren given parse closing */
  6590.         if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6591.           return(y);
  6592.         if (strcmp(atmbuf,")")) {
  6593.             printf("?Closing parenthesis required\n");
  6594.             return(-9);
  6595.         }
  6596.     }
  6597.     lp = line;
  6598.     x = ckstrncpy(lp,"_switx ",LINBUFSIZ); /* _switx + space */
  6599.     lp += x;
  6600.     ap = lp;
  6601.     debug(F010,"SWITCH a",line,0);
  6602.  
  6603. #ifdef COMMENT
  6604.     x = ckmakmsg(lp,LINBUFSIZ-x,tmpbuf," ",NULL,NULL); /* variable name + SP */
  6605. #else
  6606.     {                                   /* variable name + SP */
  6607.         char * p = tmpbuf;
  6608.     if (len > 0) {
  6609.         if (tmpbuf[0] == '(' && tmpbuf[len-1] == ')') {
  6610.         tmpbuf[len-1] = NUL;
  6611.         p++;
  6612.         }
  6613.         }
  6614.         x = ckmakmsg(lp,LINBUFSIZ-x,"{",brstrip(p),"}"," ");
  6615.     }
  6616. #endif /* COMMENT */
  6617.     debug(F010,"SWITCH b",line,0);
  6618.     lp += x;
  6619.  
  6620.     /* Get body */
  6621.  
  6622.     if ((y = cmtxt("series of cases","",&s,NULL)) < 0) return(y);
  6623.     if ((y = (int)strlen(s)) < 1) return(-2);
  6624.     if (s[0] != '{' && s[y-1] != '}') { /* Supply braces if missing */
  6625.         ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  6626.         s = tmpbuf;
  6627.     }
  6628.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  6629.         printf("?Unbalanced braces\n");
  6630.         return(0);
  6631.     }
  6632.     debug(F010,"SWITCH c",line,0);
  6633.  
  6634.     x = mlook(mactab,"_switx",nmac);    /* Look up SWITCH macro definition */
  6635.     if (x < 0) {                        /* Not there? */
  6636.         addmmac("_switx",sw_def);       /* Put it back. */
  6637.         if ((x = mlook(mactab,"_switx",nmac)) < 0) { /* Look it up again. */
  6638.             printf("?SWITCH macro definition gone!\n"); /* Shouldn't happen. */
  6639.             return(success = 0);
  6640.         }
  6641.     }
  6642.     debug(F010,"SWITCH command",line,0); /* Execute the SWITCH macro. */
  6643.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  6644. }
  6645.  
  6646. int
  6647. dofor() {                               /* The FOR command. */
  6648.     int i, fx, fy, fz;                  /* loop variables */
  6649.     char *ap, *di;                      /* macro argument pointer */
  6650.     int pp = 0;                         /* Paren level */
  6651.     int mustquote = 0;
  6652.  
  6653.     for (i = 0; i < 2; i++) {
  6654.         if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  6655.             if (y == -3) {
  6656.                 printf("?Variable name required\n");
  6657.                 return(-9);
  6658.             } else
  6659.               return(y);
  6660.         }
  6661.         if (strcmp(s,"("))
  6662.           break;
  6663.         pp++;
  6664.     }
  6665. #ifdef COMMENT
  6666.     if ((y = parsevar(s,&x,&z)) < 0)    /* Check variable. */
  6667.       return(y);
  6668. #else
  6669.     if (*s == CMDQ)                     /* If loop variable starts with */
  6670.       mustquote++;                      /* backslash, mustquote is > 0. */
  6671. #endif /* COMMENT */
  6672.  
  6673.     lp = line;                          /* Build a copy of the command */
  6674.     ckstrncpy(lp,"_forx ",LINBUFSIZ);
  6675.     lp += (int)strlen(line);            /* "_for" macro. */
  6676.     ap = lp;                            /* Save pointer to macro args. */
  6677.  
  6678.     if (*s == CMDQ) s++;                /* Skip past backslash if any. */
  6679.     while ((*lp++ = *s++)) ;            /* copy it */
  6680.     lp--; *lp++ = SP;                   /* add a space */
  6681.  
  6682.     if ((y = cmnum("initial value","",10,&fx,xxstring)) < 0) {
  6683.         if (y == -3) return(-2);
  6684.         else return(y);
  6685.     }
  6686.     debug(F101,"dofor fx","",fx);
  6687.     s = atmbuf;                         /* Copy the atom buffer */
  6688.  
  6689.     if ((int)strlen(s) < 1) goto badfor;
  6690. /*
  6691.   In edit 192, we change the loop variables to be evaluated at loop entry,
  6692.   not each time through the loop.  This was required in order to allow
  6693.   \v(argc) to be used as a loop variable, or in a loop-variable expression.
  6694.   Thus, we can't have FOR loops that modify their own exit conditions by
  6695.   changing the final value or the increment.  The problem with \v(argc) was
  6696.   that it is on the macro stack; after entry into the _forx macro, it is at
  6697.   the wrong place.
  6698. */
  6699.     sprintf(tmpbuf,"%d",fx);            /* (SAFE) Substitute actual value */
  6700.     s = tmpbuf;
  6701.     while ((*lp++ = *s++)) ;            /* (what they actually typed) */
  6702.     lp--; *lp++ = SP;
  6703. #ifdef DEBUG
  6704.     *lp = NUL;
  6705.     debug(F110,"FOR A",line,0);
  6706. #endif /* DEBUG */
  6707.  
  6708.     if ((y = cmnum("final value","",10,&fy,xxstring)) < 0) {
  6709.         if (y == -3) return(-2);
  6710.         else return(y);
  6711.     }
  6712.     debug(F101,"dofor fy","",fy);
  6713.     s = atmbuf;                         /* Same deal */
  6714.     if ((int)strlen(s) < 1)
  6715.       goto badfor;
  6716.  
  6717.     sprintf(tmpbuf,"%d",fy);            /* SAFE */
  6718.     s = tmpbuf;
  6719.     while ((*lp++ = *s++)) ;
  6720.     lp--;
  6721.     *lp++ = SP;
  6722. #ifdef DEBUG
  6723.     *lp = NUL;
  6724.     debug(F110,"FOR B",line,0);
  6725. #endif /* DEBUG */
  6726.  
  6727.     x_ifnum = 1;                        /* Increment or parenthesis */
  6728.     di = (fx < fy) ? "1" : "-1";        /* Default increment */
  6729.     if ((y = cmnum("increment",di,10,&fz,xxstring)) < 0) {
  6730.         debug(F111,"dofor increment",atmbuf,y);
  6731.         x_ifnum = 0;
  6732.         if (y == -3) {                  /* Premature termination */
  6733.             return(-2);
  6734.         } else if (y == -2) {           /* Maybe closing paren */
  6735.             if (!strcmp(atmbuf,")")) {
  6736.                 pp--;                   /* Count it */
  6737.                 s = di;                 /* supply default interval */
  6738.                 fz = atoi(s);
  6739.             } else                      /* Not closing paren, invalid */
  6740.               return(y);
  6741.         } else                          /* Other error */
  6742.           return(y);
  6743.     } else {                            /* Number */
  6744.         x_ifnum = 0;
  6745.         debug(F101,"dofor fz","",fz);
  6746.         s = atmbuf;                     /* Use it */
  6747.     }
  6748.     if ((int)strlen(s) < 1)
  6749.       goto badfor;
  6750.  
  6751.     sprintf(tmpbuf,"%d",fz);            /* (SAFE) Same deal */
  6752.     s = tmpbuf;
  6753.     while ((*lp++ = *s++)) ;
  6754.     lp--; *lp++ = SP;
  6755.  
  6756. #ifdef DEBUG
  6757.     *lp = NUL;
  6758.     debug(F110,"FOR C",line,0);
  6759. #endif /* DEBUG */
  6760.  
  6761.     /* Insert the appropriate comparison operator */
  6762.     if (fz < 0)
  6763.       *lp++ = '<';
  6764.     else
  6765.       *lp++ = '>';
  6766.     *lp++ = SP;
  6767.  
  6768. #ifdef DEBUG
  6769.     *lp = NUL;
  6770.     debug(F110,"FOR D",line,0);
  6771. #endif /* DEBUG */
  6772.  
  6773.     if (pp > 0) {                       /* If open paren given parse closing */
  6774.         if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6775.           return(y);
  6776.         if (strcmp(atmbuf,")")) {
  6777.             printf("?Closing parenthesis required\n");
  6778.             return(-9);
  6779.         }
  6780.     }
  6781.     if ((y = cmtxt("Command to execute","",&s,NULL)) < 0) return(y);
  6782.     if ((y = (int)strlen(s)) < 1) return(-2);
  6783.     if (s[0] != '{' && s[y-1] != '}') { /* Supply braces if missing */
  6784.         ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  6785.         s = tmpbuf;
  6786.     }
  6787.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  6788.         printf("?Unbalanced braces\n");
  6789.         return(0);
  6790.     }
  6791. #ifdef DEBUG
  6792.     *lp = NUL;
  6793.     debug(F110,"FOR E",line,0);
  6794. #endif /* DEBUG */
  6795.  
  6796. #ifdef COMMENT
  6797. /* Too strict */
  6798.     if (fz == 0) {
  6799.         printf("?Zero increment not allowed\n");
  6800.         return(0);
  6801.     }
  6802. #endif /* COMMENT */
  6803. /*
  6804.   In version 8.0 we decided to allow macro names anyplace a numeric-valed
  6805.   variable could appear.  But this caused trouble for the FOR loops because
  6806.   the quoting in for_def[] assumed a \%i-style loop variable.  We account
  6807.   for this here in the if (mustquote)...else logic by invoking separate
  6808.   FOR macro definitions in the two cases.
  6809. */
  6810.     if (mustquote) {                    /* \%i-style loop variable */
  6811.         x = mlook(mactab,"_forx",nmac); /* Look up FOR macro definition */
  6812.         if (x < 0) {                    /* Not there? */
  6813.             addmmac("_forx",for_def);   /* Put it back. */
  6814.             if ((x = mlook(mactab,"_forx",nmac)) < 0) { /* Look it up again. */
  6815.                 printf("?FOR macro definition gone!\n");
  6816.                 return(success = 0);
  6817.             }
  6818.         }
  6819.     } else {                            /* Loop variable is a macro */
  6820.         x = mlook(mactab,"_forz",nmac);
  6821.         if (x < 0) {
  6822.             addmmac("_forz",foz_def);
  6823.             if ((x = mlook(mactab,"_forz",nmac)) < 0) {
  6824.                 printf("?FOR macro definition gone!\n");
  6825.                 return(success = 0);
  6826.             }
  6827.         }
  6828.     }
  6829.     debug(F010,"FOR command",line,0);   /* Execute the FOR macro. */
  6830.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  6831.  
  6832. badfor:
  6833.     printf("?Incomplete FOR command\n");
  6834.     return(-2);
  6835. }
  6836. #endif /* NOSPL */
  6837.  
  6838. #ifndef NOFRILLS
  6839. /* Do the BUG command */
  6840.  
  6841. int
  6842. dobug() {
  6843.     int n;
  6844.     char * s = "";
  6845.     extern char * k_info_dir;
  6846.  
  6847.     if (k_info_dir)
  6848.       s = k_info_dir;
  6849.  
  6850. #ifdef COMMENT
  6851.     printf("\n%s,%s\n Numeric: %ld",versio,ckxsys,vernum);
  6852. #endif /* COMMENT */
  6853.     printf(
  6854. "\nBefore requesting technical support from Columbia U., please consult:\n\n"
  6855.            );
  6856.     n = 7;
  6857. #ifdef OS2
  6858.     printf(" . Your \"Kermit 95\" user manual (use the MANUAL command).\n");
  6859.     printf(" . The technical reference manual, \"Using C-Kermit\".\n");
  6860.     n += 2;
  6861. #else
  6862.     printf(" . The book \"Using C-Kermit\" (type HELP for more info).\n");
  6863.     n += 1;
  6864. #endif /* OS2 */
  6865.  
  6866.     printf(" . Your own organization's support staff, if any.\n");
  6867.     printf(
  6868. " . The comp.protocols.kermit.misc newsgroup.\n");
  6869.     printf(
  6870. " . The Kermit support website, http://www.columbia.edu/kermit/support.html \n"
  6871.            );
  6872.     printf(
  6873.  
  6874. " . The Kermit FAQ, http://www.columbia.edu/kermit/newfaq.html \n");
  6875. #ifdef OS2
  6876.     printf(
  6877. " . The Kermit 95 FAQ, http://www.columbia.edu/kermit/k95faq.html \n");
  6878.     n++;
  6879. #endif /* OS2 */
  6880.  
  6881.     printf(
  6882. " . The C-Kermit FAQ, http://www.columbia.edu/kermit/ckfaq.html \n");
  6883.     n += 4;
  6884.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6885.     printf("\n\
  6886. If you still need help or have a bug to report after consulting these sources,"
  6887.            );
  6888.     printf("\nsend e-mail to:\n\n");
  6889.     n += 2;
  6890.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6891.     printf("  mailto:kermit-support@columbia.edu\n\n");
  6892.     n += 1;
  6893.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6894.     printf("Or contact us by post:\n\n");
  6895.     printf(
  6896. "  Kermit, Columbia University, 612 W 115 Street, New York NY  10025, USA\n\n"
  6897.            );
  6898.     n += 1;
  6899.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6900.     printf("Or by fax at +1 (212) 662-6442.\n\n");
  6901.     n += 1;
  6902.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6903. #ifdef COMMENT
  6904.     printf("Telephone support is available too:\n\n");
  6905.     n += 1;
  6906.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6907.     printf(
  6908.     "  +1 (212) 854-5126, from anywhere, $25.00 USD per call, MC/Visa\n\n");
  6909.     n += 1;
  6910.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6911. #endif /* COMMENT */
  6912. #ifndef NOSHOW
  6913. #ifndef NOFRILLS
  6914.     printf(
  6915. "Before reporting problems, please use the SHOW FEATURES command\n");
  6916.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6917.     printf(
  6918. "to get detailed program version and configuration information.\n\n");
  6919. #endif /* NOFRILLS */
  6920. #endif /* NOSHOW */
  6921.     return(1);
  6922. }
  6923. #endif /* NOFRILLS */
  6924.  
  6925. #ifndef NOSPL
  6926.  
  6927. /*  T O D 2 S E C  --  Convert time of day as hh:mm:ss to secs since midnite */
  6928. /*
  6929.   Call with a string hh:mm or hh:mm:ss.
  6930.   Returns a 0 to 86400 on success, or a negative number on failure.
  6931. */
  6932. long
  6933. tod2sec(t) char * t; {
  6934.     long t2;
  6935.     long hh = 0L, mm = 0L, ss = 0L;
  6936.  
  6937.     if (!t) t = "";
  6938.     if (!*t)
  6939.       return(-3L);
  6940.     debug(F110,"tod2sec",t,0);
  6941.  
  6942.     if (isdigit(*t))                    /* Get hours from argument */
  6943.       hh = *t++ - '0';
  6944.     else
  6945.       return(-1L);
  6946.     if (isdigit(*t))
  6947.       hh = hh * 10 + *t++ - '0';
  6948. #ifdef COMMENT
  6949.     if (hh > 24L)
  6950.       return(-1L);
  6951. #endif /* COMMENT */
  6952.     if (*t == ':')
  6953.       t++;
  6954.     else if (!*t)
  6955.       goto xtod2sec;
  6956.     else
  6957.       return(-1L);
  6958.  
  6959.     if (isdigit(*t))                    /* Minutes */
  6960.       mm = *t++ - '0';
  6961.     else
  6962.       return(-1L);
  6963.     if (isdigit(*t))
  6964.       mm = mm * 10 + *t++ - '0';
  6965.     if (mm > 60L)
  6966.       return(-1L);
  6967.     if (*t == ':')
  6968.       t++;
  6969.     else if (!*t)
  6970.       goto xtod2sec;
  6971.     else
  6972.       return(-1L);
  6973.  
  6974.     if (isdigit(*t))                    /* Seconds */
  6975.       ss = *t++ - '0';
  6976.     else
  6977.       return(-1L);
  6978.     if (isdigit(*t))
  6979.       ss = ss * 10 + *t++ - '0';
  6980.     if (ss > 60L)
  6981.       return(-1L);
  6982.  
  6983.     if (*t > 32)                        /* No trailing junk allowed */
  6984.       return(-1L);
  6985.  
  6986.   xtod2sec:
  6987.  
  6988.     t2 = hh * 3600L + mm * 60L + ss;    /* Seconds since midnight from arg */
  6989.     debug(F101,"tod2sec t2","",t2);
  6990.  
  6991.     return(t2);
  6992. }
  6993.  
  6994. int waitinterval = 1;
  6995.  
  6996. #ifdef OLDWAIT
  6997. #undef OLDWAIT
  6998. #endif /* OLDWAIT */
  6999.  
  7000. int kbchar = NUL;
  7001.  
  7002. int
  7003. dopaus(cx) int cx; {
  7004.     long zz;
  7005.     extern int sleepcan;
  7006.  
  7007. #ifdef OLDWAIT
  7008.     zz = -1L;
  7009.     x_ifnum = 1;                        /* Turn off internal complaints */
  7010.     if (cx == XXWAI)
  7011.       y = cmnum("seconds to wait, or time of day hh:mm:ss","1",10,&x,xxstring);
  7012.     else if (cx == XXPAU)
  7013.       y = cmnum("seconds to pause, or time of day hh:mm:ss",
  7014.                 "1",10,&x,xxstring);
  7015.     else
  7016.       y = cmnum("milliseconds to sleep, or time of day hh:mm:ss",
  7017.                 "100",10,&x,xxstring);
  7018.     x_ifnum = 0;
  7019.     if (y < 0) {
  7020.         if (y == -2) {                  /* Invalid number or expression */
  7021.             char *p = tmpbuf;           /* Retrieve string from atmbuf */
  7022.             int n = TMPBUFSIZ;
  7023.             *p = NUL;
  7024.             zzstring(atmbuf,&p,&n);     /* Evaluate in case it's a variable */
  7025.             zz = tod2sec(tmpbuf);       /* Convert to secs since midnight */
  7026.             if (zz < 0L) {
  7027.                 printf("?Number, expression, or time of day required\n");
  7028.                 return(-9);
  7029.             } else {
  7030.                 char now[32];           /* Current time */
  7031.                 char *p;
  7032.                 long tnow;
  7033.                 p = now;
  7034.                 ztime(&p);
  7035.                 tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  7036.                 if (zz < tnow)          /* User's time before now */
  7037.                   zz += 86400L;         /* So make it tomorrow */
  7038.                 zz -= tnow;             /* Seconds from now. */
  7039.             }
  7040.         } else
  7041.           return(y);
  7042.     }
  7043.     if (x < 0) x = 0;
  7044.     switch (cx) {
  7045.       case XXPAU:                       /* PAUSE */
  7046.       case XXMSL:                       /* MSLEEP */
  7047.         if ((y = cmcfm()) < 0) return(y);
  7048.         break;
  7049.       case XXWAI:                       /* WAIT */
  7050.         z = 0;                          /* Modem signal mask */
  7051.         while (1) {                     /* Read zero or more signal names */
  7052.             y = cmkey(mstab,nms,"modem signal","",xxstring);
  7053.             if (y == -3) break;         /* -3 means they typed CR */
  7054.             if (y < 0) return(y);       /* Other negatives are errors */
  7055.             z |= y;                     /* OR the bit into the signal mask */
  7056.         }
  7057.         if ((y = cmcfm()) < 0) return(y);
  7058.         break;
  7059.  
  7060.       default:                          /* Shouldn't happen */
  7061.         return(-2);
  7062.     }
  7063.  
  7064. /* Command is entered, now do it. */
  7065.  
  7066.     if (zz > -1L) {                     /* Time of day given? */
  7067.         x = zz;
  7068.         if (zz != (long) x) {
  7069.             printf(
  7070. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7071.                    );
  7072.             return(-9);
  7073.         }
  7074.     }
  7075.     if (cx == XXMSL) {                  /* Millisecond sleep */
  7076.         msleep(zz < 0 ? x : x * 1000);
  7077.         return(success = 1);
  7078.     }
  7079.     if (cx == XXPAU && !sleepcan) {     /* SLEEP CANCELLATION is OFF */
  7080.         sleep(x);
  7081.         return(success = 1);
  7082.     }
  7083.  
  7084.     /* WAIT, or else SLEEP with cancellation allowed... */
  7085.  
  7086.     do {                                /* Sleep loop */
  7087.         int mdmsig;
  7088.         if (sleepcan) {                 /* Keyboard cancellation allowed? */
  7089.             if (y = conchk()) {         /* Did they type something? */
  7090. #ifdef COMMENT
  7091.                 while (y--) coninc(0);  /* Yes, gobble it all up */
  7092. #else
  7093.                 /* There is a debate over whether PAUSE should absorb    */
  7094.                 /* its cancelling character(s).  There are several       */
  7095.                 /* reasons why it should gobble at least one character:  */
  7096.                 /* (1) MS-DOS Kermit does it                             */
  7097.                 /* (2) if not, subsequent PAUSE commands will terminate  */
  7098.                 /*     immediately                                       */
  7099.                 /* (3) if not, subsequent ASK commands will use it as    */
  7100.                 /*     valid input.  If \13, then it will get no input   */
  7101.                 /* (4) if not, then the character appears on the command */
  7102.                 /*     line after all enclosing macros are complete.     */
  7103.                 kbchar = coninc(0);     /* Gobble one up */
  7104. #endif /* COMMENT */
  7105.                 break;                  /* And quit PAUSing or WAITing */
  7106.             }
  7107.         }
  7108.         if (cx == XXWAI) {              /* WAIT (z == modem signal mask) */
  7109.             debug(F101,"WAIT x","",x);
  7110.             if (z > 0) {                /* Looking for any modem signals? */
  7111.                 mdmsig = ttgmdm();      /* Yes, get them */
  7112.                 if (mdmsig < 0)         /* Failed */
  7113.                   return(success = 0);
  7114.                 if ((mdmsig & z) == z)  /* Got what we wanted? */
  7115.                   return(success = 1);  /* Succeed */
  7116.             }
  7117.             if (x == 0)                 /* WAIT 0 and didn't get our signals */
  7118.               break;
  7119.         }
  7120.         sleep(1);                       /* No interrupt, sleep one second */
  7121.     } while (--x > 0);
  7122.  
  7123.     if (cx == XXWAI)                    /* If WAIT and loop exhausted */
  7124.       success = (z == 0);               /* Fail. */
  7125.     else                                /*  */
  7126.       success = (x == 0);               /* Set SUCCESS/FAILURE for PAUSE. */
  7127.     return(success);
  7128.  
  7129. #else  /* New code uses chained FDBs and allows FILE waits... */
  7130.  
  7131.     char * m = "";                      /* Help message */
  7132.     struct FDB nu, fl;                  /* Parse function descriptor blocks */
  7133.     int filewait = 0;
  7134.     int mdmsig = 0, fs = 0;
  7135.     char filedate[32];
  7136.  
  7137.     kbchar = 0;
  7138.  
  7139.     switch (cx) {
  7140.       case XXWAI: m = "seconds to wait, or time of day hh:mm:ss"; break;
  7141.       case XXPAU: m = "seconds to pause, or time of day hh:mm:ss"; break;
  7142.       case XXMSL: m = "milliseconds to sleep, or time of day hh:mm:ss"; break;
  7143.     }
  7144.     zz = -1L;
  7145.     cmfdbi(&nu,
  7146.            _CMNUM,                      /* Number */
  7147.            m,                           /* Help message */
  7148.            (cx == XXMSL) ? "100" : "1", /* Default */
  7149.            "",                          /* N/A */
  7150.            0,                           /* N/A */
  7151.            0,                           /* N/A */
  7152.            xxstring,                    /* Processing function */
  7153.            NULL,                        /* N/A */
  7154.            &fl                          /* Next */
  7155.            );
  7156.     cmfdbi(&fl,                         /* Time of day */
  7157.            _CMFLD,                      /* Field */
  7158.            "",                          /* hlpmsg */
  7159.            "",                          /* default */
  7160.            "",                          /* addtl string data */
  7161.            0,                           /* addtl numeric data 1 */
  7162.            0,                           /* addtl numeric data 2 */
  7163.            xxstring,                    /* processing func */
  7164.            NULL,                        /* N/A */
  7165.            NULL                         /* No next */
  7166.            );
  7167.     x = cmfdb(&nu);                     /* Parse a number or a field */
  7168.     if (x < 0) {
  7169.         if (x == -3)
  7170.           x = -2;
  7171.         return(x);
  7172.     }
  7173.     switch (cmresult.fcode) {
  7174.       case _CMNUM:                      /* Number */
  7175.         x = cmresult.nresult;
  7176.         break;
  7177.       case _CMFLD:                      /* Field */
  7178.         zz = tod2sec(cmresult.sresult); /* Convert to secs since midnight */
  7179.         if (zz < 0L) {
  7180.             printf("?Number, expression, or time of day required\n");
  7181.             return(-9);
  7182.         } else {
  7183.             char now[32];               /* Current time */
  7184.             char *p;
  7185.             long tnow;
  7186.             p = now;
  7187.             ztime(&p);
  7188.             tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  7189.             if (zz < tnow)              /* User's time before now */
  7190.               zz += 86400L;             /* So make it tomorrow */
  7191.             zz -= tnow;         /* Seconds from now. */
  7192.         }
  7193.     }
  7194.     debug(F101,"PAUSE/WAIT/MSLEEP zz","",zz);
  7195.     switch (cx) {
  7196.       case XXPAU:                       /* PAUSE */
  7197.       case XXMSL:                       /* MSLEEP */
  7198.         if ((y = cmcfm()) < 0) return(y);
  7199.         break;
  7200.       case XXWAI:                       /* WAIT */
  7201.         z = 0;                          /* Modem signal mask */
  7202.         y = cmkey(waittab,nwaittab,"","",xxstring);
  7203.         if (y < 0) {
  7204.             if (y == -3) {
  7205.                 if ((y = cmcfm()) < 0)
  7206.                   return(y);
  7207.                 break;
  7208.             } else
  7209.               return(y);
  7210.         }
  7211.         if (y == WAIT_FIL) {            /* FILE */
  7212.             int wild = 0;
  7213.             if ((z = cmkey(wfswi,nwfswi,"event","",xxstring)) < 0)
  7214.               return(z);
  7215.             filewait = z;
  7216.             if (filewait == WF_MOD || filewait == WF_DEL)
  7217.               z = cmifi("Filename","",&s,&wild,xxstring);
  7218.             else
  7219.               z = cmfld("Filename","",&s,xxstring);
  7220.             if (z < 0)
  7221.               return(z);
  7222.             if (wild || ((filewait == WF_CRE) && iswild(s))) {
  7223.                 printf("?Wildcards not valid here\n");
  7224.                 return(-9);
  7225.             }
  7226.             ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7227.             if ((z = cmcfm()) < 0)
  7228.               return(z);
  7229.             break;
  7230.         } else if (y != WAIT_MDM) {     /* A modem signal */
  7231.             z |= y;                     /* OR the bit into the signal mask */
  7232.         }
  7233.         if (!filewait) {                /* Modem signals... */
  7234.             while (1) {                 /* Get zero or more signal names */
  7235.                 y = cmkey(mstab,nms,"modem signal","",xxstring);
  7236.                 if (y == -3) break;     /* -3 means they typed CR */
  7237.                 if (y < 0) return(y);   /* Other negatives are errors */
  7238.                 z |= y;                 /* OR the bit into the signal mask */
  7239.             }
  7240.             if ((y = cmcfm()) < 0) return(y);
  7241.             break;
  7242.         }
  7243.  
  7244.       default:                          /* Shouldn't happen */
  7245.         return(-2);
  7246.     } /* switch (cx) */
  7247.  
  7248. /* Command is entered, now do it. */
  7249.  
  7250.     if (zz > -1L) {                     /* Time of day given? */
  7251.         x = zz;
  7252.         if (zz != (long) x) {
  7253.             printf(
  7254. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7255.                    );
  7256.             return(-9);
  7257.         }
  7258.     }
  7259.     if (sleepcan)
  7260.       concb((char)escape);              /* Ensure single-char wakeup */
  7261.  
  7262.     if (cx == XXMSL) {                  /* Millisecond sleep */
  7263.         msleep(zz < 0 ? x : x * 1000);
  7264.         return(success = 1);
  7265.     }
  7266.     if (cx == XXPAU && !sleepcan) {     /* SLEEP CANCELLATION is OFF */
  7267.         sleep(x);
  7268.         return(success = 1);
  7269.     }
  7270.     if (filewait) {                     /* FILE... */
  7271.         fs = zchki(tmpbuf);             /* Check if file exists */
  7272.         switch (filewait) {
  7273.           case WF_DEL:
  7274.             if (fs == -1)
  7275.               return(success = 1);
  7276.             break;
  7277.           case WF_MOD:
  7278.             if (fs == -1) {
  7279.                 printf("?File does not exit: %s\n",tmpbuf);
  7280.                 return(-9);
  7281.             }
  7282.             s = zfcdat(tmpbuf);         /* Get current modification date */
  7283.             if (!s) s = "";
  7284.             if (ckstrncpy(filedate,s,32) != 17) {
  7285.                 printf("?Can't get modification time: %s\n",tmpbuf);
  7286.                 return(-9);
  7287.             }
  7288.             break;
  7289.           case WF_CRE:
  7290.             if (fs > -1)
  7291.               return(success = 1);
  7292.             break;
  7293.         }
  7294.     }
  7295.     do {                                /* Polling loop */
  7296.         if (sleepcan) {                 /* Keyboard cancellation allowed? */
  7297.             if ((y = conchk()) > 0) {   /* Did they type something? */
  7298.                 kbchar = coninc(0);     /* Yes, get first char they typed */
  7299.                 debug(F000,"WAIT kbchar","",kbchar);
  7300. #ifdef COMMENT
  7301.                 while (--y > 0)         /* Gobble the rest up */
  7302.                   coninc(0);
  7303. #endif /* COMMENT */
  7304.                 return(success = 0);    /* And quit PAUSing or WAITing */
  7305.             }
  7306.         }
  7307.         if (filewait == 0) {
  7308.             if (cx == XXWAI) {          /* WAIT for modem signals */
  7309.                 if (z != 0) {
  7310.                     mdmsig = ttgmdm();  /* Get them. */
  7311.                     debug(F101,"WAIT ttgmdm","",mdmsig);
  7312.                     if (mdmsig < 0)     /* Failure to get them? */
  7313.                       return(success = 0); /* Fail. */
  7314.                     if ((mdmsig & z) == z) /* Got desired ones? */
  7315.                       return(success = 1); /* Succeed. */
  7316.                 } else if (x == 0)
  7317.                   return(success = 0);
  7318.             }
  7319.         } else {                        /* FILE... */
  7320.             fs = zchki(tmpbuf);         /* Get file status */
  7321.             if (filewait == WF_MOD) {   /* Wait for modification */
  7322.                 if (fs == -1)           /* Failure to get status */
  7323.                   return(success = 0);  /* so WAIT fails. */
  7324.                 s = zfcdat(tmpbuf);     /* Get current modification time */
  7325.                 if (!s) s = "";         /* And compare with the time */
  7326.                 if (strcmp(s,filedate)) /* when the WAIT started */
  7327.                   return(success = 1);
  7328.             } else if (filewait == WF_DEL) { /* Wait for deletion */
  7329.                 if (fs == -1)           /* If file doesn't exist, */
  7330.                   return(success = 1);  /* succeed. */
  7331.             } else if (filewait == WF_CRE) { /* Wait for creation */
  7332.                 if (fs != -1)           /* If file exists */
  7333.                   return(success = 1);  /* succeed. */
  7334.             }
  7335.         }
  7336.         if (x < 1)                      /* SLEEP/WAIT/PAUSE 0 */
  7337.           break;
  7338.         sleep(waitinterval);            /* No interrupt, sleep */
  7339.         x -= waitinterval;              /* Deduct sleep time */
  7340.     } while (x > 0);
  7341.  
  7342.     if (cx == XXWAI)                    /* WAIT time expired */
  7343.       success = (z == 0);               /* Succeed if no modem signals */
  7344.     else                                /* For SLEEP or PAUSE, success */
  7345.       success = (x == 0);               /* depends on whether it was */
  7346.     return(success);                    /* interrupted from the keyboard. */
  7347. #endif /* OLDWAIT */
  7348. }
  7349. #endif /* NOSPL */
  7350.  
  7351. #ifdef OS2ORUNIX
  7352. _PROTOTYP(int zcmpfn,(char *, char *));
  7353. #endif /* OS2ORUNIX */
  7354.  
  7355. #ifndef NOFRILLS
  7356. #ifdef NT
  7357. int 
  7358. dolink() {
  7359.     /* Parse a file or a directory name */
  7360.     int i, x, z, listing = 0, havename = 0, wild = 0, rc = 1;
  7361.     struct FDB sw, fi;
  7362.  
  7363.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7364.            _CMKEY,                      /* fcode */
  7365.            "Filename or switch",        /* hlpmsg */
  7366.            "",                          /* default */
  7367.            "",                          /* addtl string data */
  7368.            nqvswtab,                    /* addtl numeric data 1: tbl size */
  7369.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7370.            xxstring,                    /* Processing function */
  7371.            qvswtab,                     /* Keyword table */
  7372.            &fi                          /* Pointer to next FDB */
  7373.            );
  7374.  
  7375.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7376.            _CMIFI,                      /* fcode */
  7377.            "",                          /* hlpmsg */
  7378.            "",                          /* default */
  7379.            "",                          /* addtl string data */
  7380.            3,                           /* addtl numeric data 1 */
  7381.            0,                           /* addtl numeric data 2 */
  7382.            xxstring,
  7383.            NULL,
  7384.            NULL
  7385.            );
  7386.  
  7387.     while (!havename) {
  7388.         x = cmfdb(&sw);                 /* Parse something */
  7389.         if (x < 0)                      /* Error */
  7390.           return(x);
  7391.         switch (cmresult.fcode) {
  7392.           case _CMKEY:
  7393.             switch (cmresult.nresult) {
  7394.               case DEL_LIS:
  7395.               case DEL_VRB:
  7396.                 listing = 1;
  7397.                 break;
  7398.               case DEL_NOL:
  7399.               case DEL_QUI:
  7400.                 listing = 0;
  7401.                 break;
  7402.             }
  7403.             break;
  7404.           case _CMIFI:
  7405.             s = cmresult.sresult;
  7406.             havename = 1;
  7407.             break;
  7408.           default:
  7409.             return(-2);
  7410.         }
  7411.     }
  7412.     wild = cmresult.nresult;            /* Source specification wild? */
  7413.  
  7414.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7415.     s = line;
  7416.  
  7417.     if (!wild)
  7418.       wild = iswild(line);
  7419.  
  7420.     p = tmpbuf;                         /* Place for new name */
  7421.     if ((x = cmofi(wild ? "Target directory" : "New name",
  7422.                    "",&s,xxstring)) < 0) { /* Get new name */
  7423.         if (x == -3) {
  7424.             printf("?%s required\n", wild ? "Target directory" : "New name");
  7425.             return(-9);
  7426.         } else return(x);
  7427.     }
  7428.     ckstrncpy(p,s,TMPBUFSIZ);           /* Make a safe copy of the new name */
  7429.     if ((y = cmcfm()) < 0) return(y);
  7430.  
  7431.     if (!wild) {                        /* Just one */
  7432.         if (listing) printf("%s => %s ",line,p);
  7433.         if (zlink(line,p) < 0) {
  7434.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7435.             rc = 0;
  7436.         } else {
  7437.             if (listing) printf("(OK)\n");
  7438.         }
  7439.         return(success = rc);
  7440.     }
  7441.     if (!isdir(p)) {                    /* Multiple */
  7442.         printf(                         /* if target is not a directory */
  7443. "?Multiple source files not allowed if target is not a directory.\n");
  7444.         return(-9);
  7445.     }
  7446. #ifdef COMMENT
  7447.     else {                              /* Show full path of target */
  7448.         char buf[CKMAXPATH];            /* (too much) */
  7449.         if (zfnqfp(p,CKMAXPATH,buf))
  7450.           ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  7451.     }
  7452. #endif /* COMMENT */
  7453.  
  7454. #ifdef VMS
  7455.     conres();                           /* Let Ctrl-C work. */
  7456. #endif /* VMS */
  7457.     debug(F110,"dolink line",line,0);
  7458.  
  7459. #ifdef ZXREWIND
  7460.     z = zxrewind();                     /* Rewind file list */
  7461. #else
  7462.     z = nzxpand(s,0);                   /* Expand file list */
  7463. #endif /* ZXREWIND */
  7464.     debug(F111,"dolink p",p,z);
  7465.  
  7466. #ifdef UNIX
  7467.     if (wild && z > 1)
  7468.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7469. #endif /* UNIX */
  7470.  
  7471.     while (z-- > 0) {
  7472.         if (!(z == 0 && !wild))
  7473.           znext(line);
  7474.         if (!line[0])
  7475.           break;
  7476.         if (listing) printf("%s => %s ",line,p);
  7477.         if (zlink(line,p) < 0) {
  7478.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7479.             rc = 0;
  7480.         } else {
  7481.             if (listing) printf("(OK)\n");
  7482.         }
  7483.     }
  7484. #ifdef VMS
  7485.     concb((char)escape);
  7486. #endif /* VMS */
  7487.     return(success = rc);
  7488. }
  7489. #endif /* NT */
  7490.  
  7491. #ifdef ZCOPY
  7492. int
  7493. docopy() {
  7494.     int i, x, listing = 0, nolist = 0, havename = 0;
  7495.     struct FDB sw, fi;
  7496.     int targetisdir = 0;
  7497.     int targetlen = 0;
  7498.     int swapping = 0;
  7499.     int appending = 0;
  7500.     int fromb64 = 0;
  7501.     int tob64 = 0;
  7502.     int wild = 0;
  7503.     int rc = 1;
  7504.  
  7505.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7506.            _CMKEY,                      /* fcode */
  7507.            "Filename or switch",        /* hlpmsg */
  7508.            "",                          /* default */
  7509.            "",                          /* addtl string data */
  7510.            ncopytab,                    /* addtl numeric data 1: tbl size */
  7511.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7512.            xxstring,                    /* Processing function */
  7513.            copytab,                     /* Keyword table */
  7514.            &fi                          /* Pointer to next FDB */
  7515.            );
  7516.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7517.            _CMIFI,                      /* fcode */
  7518.            "",                          /* hlpmsg */
  7519.            "",                          /* default */
  7520.            "",                          /* addtl string data */
  7521.            0,                           /* addtl numeric data 1 */
  7522.            0,                           /* addtl numeric data 2 */
  7523.            xxstring,
  7524.            NULL,
  7525.            NULL
  7526.            );
  7527.  
  7528.     while (!havename) {
  7529.         x = cmfdb(&sw);                 /* Parse something */
  7530.         if (x < 0)                      /* Error */
  7531.           return(x);
  7532.         switch (cmresult.fcode) {
  7533.           case _CMKEY:
  7534.             switch (cmresult.nresult) {
  7535.               case DEL_LIS:
  7536.               case DEL_VRB:
  7537.         nolist = 0;
  7538.                 listing = 1;
  7539.                 break;
  7540.               case DEL_NOL:
  7541.               case DEL_QUI:
  7542.         nolist = 1;
  7543.                 listing = 0;
  7544.                 break;
  7545.               case 999:
  7546.                 swapping = 1;
  7547.                 break;
  7548.               case 998:
  7549.                 appending = 1;
  7550.                 break;
  7551. #ifndef NOSPL
  7552.               case 997:
  7553.                 fromb64 = 1;
  7554.                 break;
  7555.               case 996:
  7556.                 tob64 = 1;
  7557.                 break;
  7558. #endif /* NOSPL */
  7559.             }
  7560.             break;
  7561.           case _CMIFI:
  7562.             s = cmresult.sresult;
  7563.             havename = 1;
  7564.             break;
  7565.           default:
  7566.             return(-2);
  7567.         }
  7568.     }
  7569.     wild = cmresult.nresult;
  7570.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7571.     s = line;
  7572.     p = tmpbuf;                         /* Place for new name */
  7573.  
  7574.     /* Get destination name */
  7575.     if ((x = cmofi("destination name and/or directory",
  7576. #ifdef UNIX
  7577.                    "."
  7578. #else
  7579.                    ""
  7580. #endif /* UNIX */
  7581.                    ,&s,xxstring)) < 0) {
  7582.         if (x == -3) {
  7583.             printf("?Name for destination file required\n");
  7584.             return(-9);
  7585.         } else return(x);
  7586.     }
  7587.     ckstrncpy(p,s,TMPBUFSIZ);           /* Safe copy of destination name */
  7588.     if ((y = cmcfm()) < 0) return(y);
  7589.     if (appending && swapping) {
  7590.         printf("?Sorry, /APPEND and /SWAP conflict\n");
  7591.         return(-9);
  7592.     }
  7593. #ifdef COMMENT
  7594. /*
  7595.   This unreasonably prevented "COPY /APPEND *.* bifile" from concatenating
  7596.   a bunch of files into one big file.
  7597. */
  7598.     if (appending && wild) {
  7599.         printf("?Sorry, /APPEND can be used only with single files\n");
  7600.         return(-9);
  7601.     }
  7602. #endif /* COMMENT */
  7603.     targetisdir = isdir(p);
  7604.     x = strlen(p);
  7605.     if (targetisdir) {
  7606. #ifdef UNIXOROSK
  7607.         if (p[x-1] != '/') {
  7608.             ckstrncat(p,"/",TMPBUFSIZ);
  7609.             x++;
  7610.         }
  7611. #else
  7612. #ifdef OS2
  7613.         if (p[x-1] != '/') {
  7614.             ckstrncat(p,"/",TMPBUFSIZ);
  7615.             x++;
  7616.         }
  7617. #else
  7618. #ifdef STRATUS
  7619.         if (p[x-1] != '>') {
  7620.             ckstrncat(p,">",TMPBUFSIZ);
  7621.             x++;
  7622.         }
  7623. #else
  7624. #ifdef datageneral
  7625.         if (p[x-1] != ':') {
  7626.             ckstrncat(p,":",TMPBUFSIZ);
  7627.             x++;
  7628.         }
  7629. #else
  7630.         if (p[x-1] != '/') {
  7631.             ckstrncat(p,"/",TMPBUFSIZ);
  7632.             x++;
  7633.         }
  7634. #endif /* datageneral */
  7635. #endif /* STRATUS */
  7636. #endif /* OS2 */
  7637. #endif /* UNIXOROSK */
  7638.     }
  7639.     targetlen = x;
  7640.  
  7641.     if (!appending) {            /* If /APPEND not given */
  7642.     if (wild && !targetisdir) {    /* No wildcards allowed */
  7643.         printf(            /* if target is not a directory */
  7644. "?Multiple source files not allowed if target is not a directory.\n");
  7645.         return(-9);
  7646.     }
  7647.     }
  7648.  
  7649. #ifdef VMS
  7650.     conres();                           /* Let Ctrl-C work. */
  7651. #endif /* VMS */
  7652.     debug(F110,"docopy line",line,0);
  7653.     debug(F110,"docopy p",p,0);
  7654.  
  7655. #ifdef ZXREWIND
  7656.     z = zxrewind();                     /* Rewind file list */
  7657. #else
  7658.     z = nzxpand(s,0);                   /* Expand file list */
  7659. #endif /* ZXREWIND */
  7660.  
  7661. #ifdef UNIX
  7662.     if (wild)
  7663.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7664. #endif /* UNIX */
  7665.  
  7666. #ifdef IKSD
  7667.     if (!targetisdir && zchki(p) > -1) { /* Destination file exists? */
  7668.         if (inserver && (!ENABLED(en_del)
  7669. #ifdef CK_LOGIN
  7670.                          || isguest
  7671. #endif /* CK_LOGIN */
  7672.                          )) {
  7673.             printf("?Sorry, overwriting existing files is disabled\n");
  7674.             return(-9);
  7675.         }
  7676.     }
  7677. #endif /* IKSD */
  7678.  
  7679.     if (tob64 && fromb64) {             /* To and from B64 = no conversion */
  7680.         tob64 = 0;
  7681.         fromb64 = 0;
  7682.     }
  7683.     debug(F110,"COPY dest",p,0);
  7684.  
  7685.     while (z > 0) {
  7686.  
  7687.         znext(line);
  7688.         if (!line[0])
  7689.           break;
  7690.  
  7691.         errno = 0;                      /* Reset errno */
  7692.  
  7693.         if (listing) printf("%s => %s ",line,p);
  7694.  
  7695.         /* Straight copy */
  7696.         if (!swapping && !appending && !fromb64 && !tob64) {
  7697.             debug(F110,"COPY zcopy",line,0);
  7698.  
  7699.             if ((x = zcopy(line,p)) < 0) { /* Let zcopy() do it. */
  7700.                 switch (x) {
  7701.                   case -2:
  7702.             if (listing)
  7703.               printf("(FAILED: Not a regular file)\n");
  7704.             else if (!nolist)
  7705.               printf("?Not a regular file - %s\n",line);
  7706.                     rc = 0;
  7707.                     break;
  7708.                   case -3:
  7709.             if (listing)
  7710.               printf("(FAILED: Not found or not accessible)\n");
  7711.             else if (!nolist)
  7712.               printf("?Not found or not accessible - %s\n",line);
  7713.                     rc = 0;
  7714.                     break;
  7715.                   case -4:
  7716.             if (listing)
  7717.               printf("(FAILED: Permission denied)\n");
  7718.             else if (!nolist)
  7719.               printf("?Permission denied - %s\n",line);
  7720.                     rc = 0;
  7721.                     break;
  7722.                   case -5:
  7723.             if (listing)
  7724.               printf("(Source and destination are the same file)\n");
  7725.             else if (!nolist)
  7726.               printf(
  7727.               "?Source and destination are the same file - %s\n",
  7728.               line
  7729.               );
  7730.                     break;
  7731.                   case -6:
  7732.             if (listing)
  7733.               printf("(FAILED: Input/Output error)\n");
  7734.             else if (!nolist)
  7735.               printf("?Input/Output error - %s\n",line);
  7736.                     rc = 0;
  7737.                     break;
  7738.                   case -7:
  7739.             if (listing)
  7740.               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7741.             else if (!nolist)
  7742.               printf("?%s - %s\n",ck_errstr(),p);
  7743.                     rc = 0;
  7744.                     break;
  7745.                   default:
  7746.             if (listing)
  7747.               printf("(FAILED: %s)\n",ck_errstr());
  7748.             else if (!nolist)
  7749.               printf("?%s\n",ck_errstr());
  7750.                     rc = 0;
  7751.                 }
  7752.             } else {
  7753.                 if (listing) printf("(OK)\n");
  7754.             }
  7755.  
  7756.         } else {                        /* Special options */
  7757.  
  7758.             int prev, y, x = 0;         /* Variables needed for them */
  7759.             int i, t;
  7760.             char ibuf[100];
  7761.             char obuf[200];
  7762.             FILE * in = NULL;
  7763.             FILE * out = NULL;
  7764.  
  7765.             if ((in = fopen(line,"r")) == NULL) { /* Open input file */
  7766.                 if (listing)
  7767.           printf("(FAILED: %s)\n",ck_errstr());
  7768.         else if (!nolist)
  7769.           printf("?%s - %s)\n",ck_errstr(),line);
  7770.                 rc = 0;
  7771.                 continue;
  7772.             }
  7773.             if (targetisdir) {          /* Target is directory */
  7774.                 char * buf = NULL;      /* so append this filename to it */
  7775.                 zstrip(line,&buf);
  7776.                 p[targetlen] = NUL;
  7777.                 if (buf)
  7778.                   ckstrncat(p,buf,TMPBUFSIZ);
  7779.             }
  7780. #ifdef OS2ORUNIX
  7781.             if (zcmpfn(line,p)) {       /* Input and output are same file? */
  7782.                 if (listing)
  7783.                   printf("(FAILED: Source and destination identical)\n");
  7784.         else if (!nolist)
  7785.                   printf("?Source and destination identical - %s\n", line); 
  7786.                 rc = 0;
  7787.                 continue;
  7788.             }
  7789. #endif /* OS2ORUNIX */
  7790.             if ((out = fopen(p, (appending ? "a" : "w"))) == NULL) {
  7791.                 fclose(in);
  7792.                 if (listing)
  7793.           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7794.         else if (!nolist)
  7795.           printf("?%s - %s\n",p,ck_errstr());
  7796.                 rc = 0;
  7797.                 continue;
  7798.             }
  7799. #ifndef NOSPL
  7800.             if (tob64) {                /* Converting to Base-64 */
  7801.  
  7802.                 debug(F110,"COPY tob64",line,0);
  7803.  
  7804.                 while (1) {             /* Loop... */
  7805.                     prev = x;
  7806.                     if ((x = fread(ibuf,1,54,in)) < 1) { /* EOF */
  7807.                         if (listing)
  7808.                           printf("(OK)\n");
  7809.                         break;
  7810.                     }
  7811.                     if (prev % 3) {
  7812.                         if (listing)
  7813.                           printf("(FAILED: Phase error at %d)\n",prev);
  7814.             else if (!nolist)
  7815.                           printf("?Phase error at %d\n",prev);
  7816.                         rc = 0;
  7817.                         break;
  7818.                     }
  7819.                     if (swapping) {
  7820.                         if (x & 1) {
  7821.                             if (listing)
  7822.                               printf("(FAILED: Swap error)\n");
  7823.                 else if (!nolist)
  7824.                   printf("?Swap error\n");
  7825.                             rc = 0;
  7826.                             break;
  7827.                         }
  7828.                         for (i = 0; i < x; i+=2) {
  7829.                             t = ibuf[i];
  7830.                             ibuf[i] = ibuf[i+1];
  7831.                             ibuf[i+1] = t;
  7832.                         }
  7833.                     }
  7834.                     if ((y = b8tob64(ibuf,x,obuf,180)) < 0) {
  7835.                         if (listing)
  7836.                           printf("(FAILED: Encoding error)\n");
  7837.             else if (!nolist)
  7838.               printf("?Encoding error\n");
  7839.                         rc = 0;
  7840.                         break;
  7841.                     }
  7842.                     fprintf(out,"%s\n",obuf);
  7843.                 }
  7844.  
  7845.             } else if (fromb64) {       /* Converting from Base 64 */
  7846.  
  7847.                 debug(F110,"COPY fromb64",line,0);
  7848.  
  7849.                 if ((out = fopen(p,appending ? "a" : "w")) == NULL) {
  7850.                     fclose(in);
  7851.                     if (listing)
  7852.               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7853.             else if (!nolist)
  7854.               printf("?%s - %s\n",p,ck_errstr());
  7855.                     rc = 0;
  7856.                     continue;
  7857.                 }
  7858.                 x = 1;
  7859.                 while (x) {
  7860.                     x = fread(ibuf,1,80,in);
  7861.                     if ((y = b64tob8(ibuf,x,obuf,80)) < 0) {
  7862.                         if (listing)
  7863.                           printf("(FAILED: Decoding error)\n");
  7864.             else if (!nolist)
  7865.               printf("?Decoding error\n");
  7866.                         rc = 0;
  7867.                         break;
  7868.                     }
  7869.                     if (swapping) {
  7870.                         if (x & 1) {
  7871.                             if (listing)
  7872.                               printf("(FAILED: Swap error)\n");
  7873.                 else if (!nolist)
  7874.                   printf("?Swap error\n");
  7875.                             rc = 0;
  7876.                             break;
  7877.                         }
  7878.                         for (i = 0; i < y; i+=2) {
  7879.                             t = obuf[i];
  7880.                             obuf[i] = obuf[i+1];
  7881.                             obuf[i+1] = t;
  7882.                         }
  7883.                     }
  7884.                     if (y > 0) {
  7885.                         if (fwrite(obuf,1,y,out) < 1) {
  7886.                             if (listing)
  7887.                               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7888.                 else if (!nolist)
  7889.                   printf("?%s - %s\n",p,ck_errstr());
  7890.                             rc = 0;
  7891.                             break;
  7892.                         }
  7893.                     }
  7894.                 }
  7895.  
  7896.             } else
  7897. #endif /* NOSPL */
  7898.  
  7899.             if (swapping) {             /* Swapping bytes */
  7900.  
  7901.                 CHAR c[3];
  7902.                 c[2] = NUL;
  7903.  
  7904.                 debug(F110,"COPY swapping",line,0);
  7905.  
  7906.                 while (1) {
  7907.                     x = fread((char *)c,1,2,in);
  7908.                     if (x < 1) {
  7909.                         if (listing)
  7910.                           printf("(OK)\n");
  7911.                         break;
  7912.                     } else if (x == 1) {
  7913.                         c[1] = c[0];
  7914.                         c[0] = NUL;
  7915.                         printf(
  7916.                             "(WARNING: Odd byte count)");
  7917.                         if (!listing) printf("\n");
  7918.                     }
  7919.                     if (fprintf(out,"%c%c",c[1],c[0]) == EOF) {
  7920.                         if (listing)
  7921.                           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7922.             else if (!nolist)
  7923.                           printf("?%s - %s\n",p,ck_errstr());
  7924.                         rc = 0;
  7925.                         break;
  7926.                     }
  7927.                 }
  7928.  
  7929.             } else if (appending) {     /* Appending to target file */
  7930.  
  7931.                 char c;
  7932.  
  7933.                 debug(F110,"COPY appending",line,0);
  7934.  
  7935.                 while (1) {
  7936.                     x = fread(&c,1,1,in);
  7937.                     if (x < 1) {
  7938.                         if (listing)
  7939.                           printf("(OK)\n");
  7940.                         break;
  7941.                     }
  7942.                     if (fwrite(&c,1,1,out) < 1) {
  7943.                         if (listing)
  7944.                           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7945.             else if (!nolist)
  7946.                           printf("?%s - %s\n",p,ck_errstr());
  7947.                         rc = 0;
  7948.                         break;
  7949.                     }
  7950.                 }
  7951.             }
  7952.             if (out) fclose(out);
  7953.             if (in) fclose(in);
  7954.         }
  7955. #ifdef VMSORUNIX
  7956.         concb((char)escape);
  7957. #endif /* VMSORUNIX */
  7958.     }
  7959.     if (rc > -1) success = rc;
  7960.     return(rc);
  7961. }
  7962. #endif /* ZCOPY */
  7963. #endif /* NOFRILLS */
  7964.  
  7965. #ifndef NORENAME
  7966. #ifndef NOFRILLS
  7967. #ifdef ZRENAME
  7968. int
  7969. dorenam() {
  7970.     /* Parse a file or a directory name */
  7971.     int i, x, z, listing = 0, havename = 0, wild = 0, rc = 1;
  7972.     int nolist = 0;
  7973.     struct FDB sw, fi;
  7974.  
  7975.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7976.            _CMKEY,                      /* fcode */
  7977.            "Filename or switch",        /* hlpmsg */
  7978.            "",                          /* default */
  7979.            "",                          /* addtl string data */
  7980.            nqvswtab,                    /* addtl numeric data 1: tbl size */
  7981.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7982.            xxstring,                    /* Processing function */
  7983.            qvswtab,                     /* Keyword table */
  7984.            &fi                          /* Pointer to next FDB */
  7985.            );
  7986.  
  7987.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7988.            _CMIFI,                      /* fcode */
  7989.            "",                          /* hlpmsg */
  7990.            "",                          /* default */
  7991.            "",                          /* addtl string data */
  7992.            3,                           /* addtl numeric data 1 */
  7993.            0,                           /* addtl numeric data 2 */
  7994.            xxstring,
  7995.            NULL,
  7996.            NULL
  7997.            );
  7998.  
  7999.     while (!havename) {
  8000.         x = cmfdb(&sw);                 /* Parse something */
  8001.         if (x < 0)                      /* Error */
  8002.           return(x);
  8003.         switch (cmresult.fcode) {
  8004.           case _CMKEY:
  8005.             switch (cmresult.nresult) {
  8006.               case DEL_LIS:
  8007.               case DEL_VRB:
  8008.                 listing = 1;
  8009.                 break;
  8010.               case DEL_NOL:
  8011.               case DEL_QUI:
  8012.         nolist = 1;
  8013.                 listing = 0;
  8014.                 break;
  8015.             }
  8016.             break;
  8017.           case _CMIFI:
  8018.             s = cmresult.sresult;
  8019.             havename = 1;
  8020.             break;
  8021.           default:
  8022.             return(-2);
  8023.         }
  8024.     }
  8025.     wild = cmresult.nresult;            /* Source specification wild? */
  8026.  
  8027.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  8028.     s = line;
  8029.  
  8030.     if (!wild)
  8031.       wild = iswild(line);
  8032.  
  8033.     p = tmpbuf;                         /* Place for new name */
  8034.     if ((x = cmofi(wild ? "Target directory" : "New name",
  8035.                    "",&s,xxstring)) < 0) { /* Get new name */
  8036.         if (x == -3) {
  8037.             printf("?%s required\n", wild ? "Target directory" : "New name");
  8038.             return(-9);
  8039.         } else return(x);
  8040.     }
  8041.     ckstrncpy(p,s,TMPBUFSIZ);           /* Make a safe copy of the new name */
  8042.     if ((y = cmcfm()) < 0) return(y);
  8043.  
  8044.     if (!wild) {                        /* Just one */
  8045.         if (listing) printf("%s => %s ",line,p);
  8046.         if (zrename(line,p) < 0) {
  8047.             if (listing)
  8048.           printf("(FAILED: %s)\n",ck_errstr());
  8049.         else if (!nolist)
  8050.           printf("?%s\n",ck_errstr());
  8051.             rc = 0;
  8052.         } else {
  8053.             if (listing) printf("(OK)\n");
  8054.         }
  8055.         return(success = rc);
  8056.     }
  8057.     if (!isdir(p)) {                    /* Multiple */
  8058.         printf(                         /* if target is not a directory */
  8059. "?Multiple source files not allowed if target is not a directory.\n");
  8060.         return(-9);
  8061.     }
  8062. #ifdef COMMENT
  8063.     else {                              /* Show full path of target */
  8064.         char buf[CKMAXPATH];            /* (too much) */
  8065.         if (zfnqfp(p,CKMAXPATH,buf))
  8066.           ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  8067.     }
  8068. #endif /* COMMENT */
  8069.  
  8070. #ifdef VMS
  8071.     conres();                           /* Let Ctrl-C work. */
  8072. #endif /* VMS */
  8073.     debug(F110,"dorename line",line,0);
  8074.  
  8075. #ifdef ZXREWIND
  8076.     z = zxrewind();                     /* Rewind file list */
  8077. #else
  8078.     z = nzxpand(s,0);                   /* Expand file list */
  8079. #endif /* ZXREWIND */
  8080.     debug(F111,"dorename p",p,z);
  8081.  
  8082. #ifdef UNIX
  8083.     if (wild && z > 1)
  8084.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  8085. #endif /* UNIX */
  8086.  
  8087. /*
  8088.   Note: COPY, RENAME, DELETE and similar commands should have options to
  8089.   stop or proceed when they are operating on multiple files and the operation
  8090.   fails.
  8091. */
  8092.     while (z-- > 0) {
  8093.         if (!(z == 0 && !wild))
  8094.           znext(line);
  8095.         if (!line[0])
  8096.           break;
  8097.         if (listing) printf("%s => %s ",line,p);
  8098.         if (zrename(line,p) < 0) {
  8099.             if (listing)
  8100.           printf("(FAILED: %s)\n",ck_errstr());
  8101.         else if (!nolist)
  8102.           printf("?%s - %s\n",ck_errstr(),line);
  8103.             rc = 0;
  8104.         } else {
  8105.             if (listing) printf("(OK)\n");
  8106.         }
  8107.     }
  8108. #ifdef VMS
  8109.     concb((char)escape);
  8110. #endif /* VMS */
  8111.     return(success = rc);
  8112. }
  8113. #endif /* ZRENAME */
  8114. #endif /* NOFRILLS */
  8115. #endif /* NORENAME */
  8116.  
  8117. #ifndef NOSPL
  8118.  
  8119. /* Do the RETURN command */
  8120.  
  8121. int
  8122. doreturn(s) char *s; {
  8123.     int x;
  8124.     extern int tra_asg;
  8125.     char * line, * lp;
  8126.  
  8127.     if (cmdlvl < 1) {
  8128.         printf("\n?Can't return from level %d\n",maclvl);
  8129.         return(success = 0);
  8130.     }
  8131.     line = malloc(LINBUFSIZ);
  8132.     if (line == NULL)
  8133.       return(success = 0);
  8134.     lp = line;                          /* Expand return value now */
  8135.     x = LINBUFSIZ-1;
  8136.     if (!s) s = "";
  8137.     debug(F110,"RETURN s",s,0);
  8138.     if (zzstring(s,&lp,&x) > -1) {
  8139.         s = line;
  8140.         debug(F110,"RETURN zzstring",s,0);
  8141.     }
  8142.  
  8143.     /* Pop from all FOR/WHILE/SWITCH/XIFs */
  8144.     while ((maclvl > 0) &&
  8145.            (m_arg[maclvl-1][0]) &&
  8146.            (cmdstk[cmdlvl].src == CMD_MD) &&
  8147.            (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  8148.             !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  8149.             !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  8150.             !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  8151.         debug(F111,"RETURN IF/FOR/WHI/SWI pop",m_arg[maclvl-1][0],maclvl);
  8152.         dogta(XXPTA);                   /* Put args back */
  8153.         popclvl();                      /* Pop up two levels */
  8154.         popclvl();
  8155.     }
  8156.     if (tra_asg) {                      /* If tracing show return value */
  8157.         if (*s)
  8158.           printf("<<< %s: \"%s\"\n", m_arg[maclvl][0], s);
  8159.         else
  8160.           printf("<<< %s: (null)\n", m_arg[maclvl][0]);
  8161.     }
  8162.     popclvl();                          /* Pop from enclosing TAKE or macro */
  8163.     debug(F111,"RETURN tolevel",s,maclvl);
  8164.     if (!s) s = "";
  8165.     if (!*s) s = NULL;
  8166.     makestr(&(mrval[maclvl+1]),s);      /* Set the RETURN value */
  8167.     free(line);
  8168.     return(success = 1);                /* Macro succeeds if we RETURN */
  8169. }
  8170. #endif /* NOSPL */
  8171.  
  8172. #ifndef NOSPL
  8173. /* Do the OPEN command */
  8174.  
  8175. int
  8176. doopen()  {                             /* OPEN { append, read, write } */
  8177.     int x, y, z = 0; char *s;
  8178.     static struct filinfo fcb;          /* (must be static) */
  8179.     if ((x = cmkey(opntab,nopn,"mode","",xxstring)) < 0) {
  8180.         if (x == -3) {
  8181.             printf("?Mode required\n");
  8182.             return(-9);
  8183.         } else return(x);
  8184.     }
  8185.     switch (x) {
  8186.       case OPN_FI_R:                    /* Old file (READ) */
  8187.         if (chkfn(ZRFILE) > 0) {
  8188.             printf("?Read file already open\n");
  8189.             return(-2);
  8190.         }
  8191.         if ((z = cmifi("File to read","",&s,&y,xxstring)) < 0) {
  8192.             if (z == -3) {
  8193.                 printf("?Input filename required\n");
  8194.                 return(-9);
  8195.             } else return(z);
  8196.         }
  8197.         if (y) {                                /* No wildcards allowed */
  8198.             printf("\n?Please specify a single file\n");
  8199.             return(-2);
  8200.         }
  8201.         ckstrncpy(line,s,LINBUFSIZ);
  8202.         if ((int)strlen(line) < 1) return(-2);
  8203.         if ((z = cmnum("buffer size","4096",10,&y,xxstring)) < 0)
  8204.           return(z);
  8205.         if (y < 1) {
  8206.             printf("?Positive number required\n");
  8207.             return(-9);
  8208.         }
  8209.         if ((z = cmcfm()) < 0) return(z);
  8210.         readblock = y;
  8211.         if (readbuf)
  8212.           free((char *)readbuf);
  8213.         if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  8214.             printf("?Can't allocate read buffer\n");
  8215.             return(-9);
  8216.         }
  8217.         return(success = zopeni(ZRFILE,line));
  8218.  
  8219. #ifndef MAC
  8220. #ifndef NOPUSH
  8221.       case OPN_PI_R:                    /* Pipe/Process (!READ) */
  8222.         if (nopush) {
  8223.             printf("?Read from pipe disabled\n");
  8224.             return(success=0);
  8225.         }
  8226.         if (chkfn(ZRFILE) > 0) {
  8227.             printf("?Read file already open\n");
  8228.             return(-2);
  8229.         }
  8230.         if ((y = cmtxt("System command to read from","",&s,xxstring)) < 0) {
  8231.             if (y == -3) {
  8232.                 printf("?Command name required\n");
  8233.                 return(-9);
  8234.             } else return(y);
  8235.         }
  8236.         ckstrncpy(line,brstrip(s),LINBUFSIZ);
  8237.         if (!line[0]) return(-2);
  8238.         if ((y = cmcfm()) < 0) return(y);
  8239.         if (!readbuf) {
  8240.             if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  8241.                 printf("?Can't allocate read buffer\n");
  8242.                 return(-9);
  8243.             }
  8244.         }
  8245.         return(success = zxcmd(ZRFILE,line));
  8246.  
  8247.       case OPN_PI_W:                    /* Write to pipe */
  8248.         if (nopush) {
  8249.             printf("?Write to pipe disabled\n");
  8250.             return(success=0);
  8251.         }
  8252.         if (chkfn(ZWFILE) > 0) {
  8253.             printf("?Write file already open\n");
  8254.             return(-2);
  8255.         }
  8256.         if ((y = cmtxt("System command to write to","",&s,xxstring)) < 0) {
  8257.             if (y == -3) {
  8258.                 printf("?Command name required\n");
  8259.                 return(-9);
  8260.             } else return(y);
  8261.         }
  8262.         ckstrncpy(line,brstrip(s),LINBUFSIZ);
  8263.         if (!line[0]) return(-2);
  8264.         if ((y = cmcfm()) < 0) return(y);
  8265.         success = zxcmd(ZWFILE,line);
  8266.         if (!success && msgflg)
  8267.           printf("Can't open process for writing: %s\n",line);
  8268.         return(success);
  8269. #endif /* NOPUSH */
  8270. #endif /* MAC */
  8271.  
  8272.       case OPN_FI_W:                    /* New file (WRITE) */
  8273.       case OPN_FI_A:                    /* (APPEND) */
  8274.         if ((z = cmofi("Name of local file to create","",&s,xxstring)) < 0) {
  8275.             if (z == -3) {
  8276.                 printf("?Filename required\n");
  8277.                 return(-9);
  8278.             } else return(z);
  8279.         }
  8280.         if (z == 2) {
  8281.             printf("?Sorry, %s is a directory name\n",s);
  8282.             return(-9);
  8283.         }
  8284.         if (chkfn(ZWFILE) > 0) {
  8285.             printf("?Write/Append file already open\n");
  8286.             return(-2);
  8287.         }
  8288.         fcb.bs = fcb.cs = fcb.rl = fcb.fmt = fcb.org = fcb.cc = fcb.typ = 0;
  8289.         fcb.lblopts = 0;
  8290.         fcb.dsp = (x == OPN_FI_W) ? XYFZ_N : XYFZ_A; /* Create or Append */
  8291.         ckstrncpy(line,s,LINBUFSIZ);
  8292.         if ((int)strlen(line) < 1) return(-2);
  8293.         if ((y = cmcfm()) < 0) return(y);
  8294.         return(success = zopeno(ZWFILE,line,NULL,&fcb));
  8295.  
  8296. #ifndef NOLOCAL
  8297.       case OPN_SER:                     /* OPEN PORT or LINE */
  8298.       case OPN_NET: {                   /* OPEN HOST */
  8299.           extern int didsetlin, ttnproto;
  8300.           if (x == OPN_NET) {
  8301.               z = ttnproto;
  8302.               ttnproto = NP_NONE;
  8303.           }
  8304.           if ((y = setlin((x == OPN_SER) ? XYLINE : XYHOST, 1, 0)) < 0) {
  8305.               if (x == OPN_NET)
  8306.                 ttnproto = z;
  8307.               success = 0;
  8308.           }
  8309.           didsetlin++;
  8310.           return(y);
  8311.       }
  8312. #endif /* NOLOCAL */
  8313.  
  8314.       default:
  8315.         printf("?Not implemented");
  8316.         return(-2);
  8317.     }
  8318. }
  8319. #endif /* NOSPL */
  8320.  
  8321. #ifndef NOXFER
  8322. /*  D O X G E T  --  GET command parser with switches  */
  8323.  
  8324. #ifdef CK_LABELED
  8325. int g_lf_opts = -1;
  8326. extern int lf_opts;
  8327. #endif /* CK_LABELED */
  8328.  
  8329. int
  8330. doxget(cx) int cx; {
  8331.     extern int                          /* External variables we need */
  8332. #ifdef RECURSIVE
  8333.       recursive,
  8334. #endif /* RECURSIVE */
  8335.       xfermode, fdispla, protocol, usepipes,
  8336.       g_binary, g_xfermode, g_displa, g_rpath, g_usepipes;
  8337.     extern char * rcv_move;             /* Directory to move new files to */
  8338.     extern char * rcv_rename;           /* What to rename new files to */
  8339.     extern char * rcvexcept[];          /* RECEIVE / GET exception list */
  8340.     int opkt  =  0;                     /* Flag for O-Packet needed */
  8341.  
  8342. #ifdef PIPESEND
  8343.     extern int pipesend;
  8344.     extern char * rcvfilter;
  8345. #endif /* PIPESEND */
  8346.     extern struct keytab rpathtab[];
  8347.     extern int nrpathtab;
  8348.     extern long calibrate;
  8349.     int asname = 0;                     /* Flag for have as-name */
  8350.     int konly = 0;                      /* Kermit-only function */
  8351.     int c, i, n, confirmed = 0;         /* Workers */
  8352.     int getval = 0;                     /* Whether to get switch value */
  8353.     int rcvcmd = 0;                     /* Whether it is the RECEIVE command */
  8354.     int mget = 0;                       /* Whether it is the MGET command */
  8355.     struct stringint {                  /* Temporary array for switch values */
  8356.         char * sval;
  8357.         int ival;
  8358.     } pv[SND_MAX+1];
  8359.     struct FDB sw, fl, cm;              /* FDBs for each parse function */
  8360.     char * cmdstr = "this command";
  8361.  
  8362. #ifdef NEWFTP
  8363.     if (cx == XXGET || cx == XXREGET || cx == XXMGET || cx == XXRETR) {
  8364.         extern int ftpget;
  8365.         extern int ftpisopen();
  8366.         if ((ftpget == 1) || ((ftpget == 2) && ftpisopen()))
  8367.           return(doftpget(cx,0));
  8368.     }
  8369. #endif /* NEWFTP */
  8370.  
  8371.     debug(F101,"xget cx","",cx);
  8372.  
  8373.     oopts = -1;
  8374.     omode = -1;
  8375.  
  8376.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  8377.         pv[i].sval = NULL;
  8378.         pv[i].ival = -1;
  8379.     }
  8380.     /* Preset switch values based on top-level command that called us */
  8381.  
  8382.     switch (cx) {
  8383.       case XXREC:                       /* RECEIVE */
  8384.         cmdstr = "RECEIVE";
  8385.         rcvcmd = 1; break;
  8386.       case XXGET:                       /* GET */
  8387.         cmdstr = "GET";
  8388.         konly = 1;
  8389.         break;
  8390. #ifdef CK_RESEND
  8391.       case XXREGET:                     /* REGET */
  8392.         cmdstr = "REGET";
  8393.         konly = 1;
  8394.         pv[SND_BIN].ival = 1;           /* Implies /BINARY */
  8395.         pv[SND_RES].ival = 1; break;
  8396. #endif /* CK_RESEND */
  8397.       case XXRETR:                      /* RETRIEVE */
  8398.         cmdstr = "RETRIEVE";
  8399.         konly = 1;
  8400.         pv[SND_DEL].ival = 1; break;
  8401. #ifdef PIPESEND
  8402.       case XXCREC:                      /* CRECEIVE */
  8403.         cmdstr = "CRECEIVE";
  8404.         konly = 1;
  8405.         rcvcmd = 1;
  8406.         pv[SND_CMD].ival = 1; break;
  8407.       case XXCGET:                      /* CGET */
  8408.         cmdstr = "CGET";
  8409.         konly = 1;
  8410.         pv[SND_CMD].ival = 1; break;
  8411. #endif /* PIPESEND */
  8412. #ifndef NOMGET
  8413.       case XXMGET:                      /* MGET */
  8414.         cmdstr = "MGET";
  8415.         konly = 1;
  8416.         mget = 1; break;
  8417. #endif /* NOMGET */
  8418.     }
  8419.     debug(F111,"xget rcvcmd",cmdstr,rcvcmd);
  8420.     debug(F101,"xget konly","",konly);
  8421.  
  8422. #ifdef CK_XYZ
  8423.     if (!rcvcmd && protocol != PROTO_K) {
  8424.         printf("?Sorry, %s works only with Kermit protocol\n",cmdstr);
  8425.         return(-9);
  8426.     }
  8427. #endif /* CK_XYZ */
  8428.  
  8429.     /* Set up chained parse functions... */
  8430.  
  8431.     cmfdbi(&sw,                         /* First FDB - command switches */
  8432.            _CMKEY,                      /* fcode */
  8433.            rcvcmd ?
  8434.            "Optional name/template to store incoming files under, or switch" :
  8435.            "Remote filename, or switch", /* hlpmsg */
  8436.            "",                          /* default */
  8437.            "",                          /* addtl string data */
  8438.            rcvcmd ? nrcvtab : ngettab,  /* addtl numeric data 1: tbl size */
  8439.            4,                           /* addtl numeric data 2: 4 = cmswi */
  8440.            xxstring,                    /* Processing function */
  8441.            rcvcmd ? rcvtab : gettab,    /* Keyword table */
  8442.            &fl                          /* Pointer to next FDB */
  8443.            );
  8444.     if (rcvcmd || mget)                 /* RECEIVE or MGET */
  8445.       cmfdbi(&fl,
  8446.            _CMTXT,                      /* fcode */
  8447.            rcvcmd ?                     /* hlpmsg */
  8448.              "Output filename or Command" : /* Output filename */
  8449.              "File(s) to GET",              /* Files we are asking for */
  8450.            "",                          /* default */
  8451.            "",                          /* addtl string data */
  8452.            0,                           /* addtl numeric data 1 */
  8453.            0,                           /* addtl numeric data 2 */
  8454. #ifdef CK_XYZ
  8455.            (protocol == PROTO_X || protocol == PROTO_XC) ?
  8456.              xxstring :
  8457.              (rcvcmd ? (xx_strp)0  : xxstring)
  8458. #else
  8459.            rcvcmd ? (xx_strp)0  : xxstring /* Processing function */
  8460. #endif /* CK_XYZ */
  8461.              ,
  8462.            NULL,
  8463.            &cm
  8464.            );
  8465.     else
  8466.       cmfdbi(&fl,                       /* Remote filename or command */
  8467.            _CMFLD,                      /* fcode */
  8468.            "Remote filename",           /* hlpmsg */
  8469.            "",                          /* default */
  8470.            "",                          /* addtl string data */
  8471.            0,                           /* addtl numeric data 1 */
  8472.            0,                           /* addtl numeric data 2 */
  8473.            xxstring,
  8474.            NULL,
  8475.            &cm
  8476.            );
  8477.     cmfdbi(&cm,                         /* Confirmation */
  8478.            _CMCFM,                      /* fcode */
  8479.            "",                          /* hlpmsg */
  8480.            "",                          /* default */
  8481.            "",                          /* addtl string data */
  8482.            0,                           /* addtl numeric data 1 */
  8483.            0,                           /* addtl numeric data 2 */
  8484.            NULL,
  8485.            NULL,
  8486.            NULL
  8487.            );
  8488.  
  8489.     /* (See doxsend() for fuller commentary) */
  8490.  
  8491.     while (1) {                         /* Parse 0 or more switches */
  8492.         x = cmfdb(&sw);                 /* Parse something */
  8493.         debug(F101,"xget cmfdb","",x);
  8494.         if (x < 0)                      /* Error */
  8495.           goto xgetx;                   /* or reparse needed */
  8496.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  8497.           break;
  8498.         c = cmgbrk();                   /* Get break character */
  8499.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  8500.             printf("?This switch does not take an argument\n");
  8501.             x = -9;
  8502.             goto xgetx;
  8503.         }
  8504.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  8505.             printf("?This switch requires an argument\n");
  8506.             x = -9;
  8507.             goto xgetx;
  8508.         }
  8509.         n = cmresult.nresult;           /* Numeric result = switch value */
  8510.         debug(F101,"xget switch","",n);
  8511.  
  8512.         switch (n) {                    /* Process the switch */
  8513. #ifdef PIPESEND
  8514.           case SND_CMD:                 /* These take no args */
  8515.             if (nopush) {
  8516.                 printf("?Sorry, system command access is disabled\n");
  8517.                 x = -9;
  8518.                 goto xgetx;
  8519.             } else if (rcvfilter) {
  8520.                 printf(
  8521. "?Sorry, no GET /COMMAND when RECEIVE FILTER selected\n");
  8522.                 x = -9;
  8523.                 goto xgetx;
  8524.             }
  8525.             if (rcvcmd)
  8526.               sw.hlpmsg = "Command, or switch"; /* Change help message */
  8527.             /* Fall thru... */
  8528. #endif /* PIPESEND */
  8529.  
  8530.           case SND_REC:                 /* /RECURSIVE */
  8531.             pv[SND_PTH].ival = PATH_REL; /* Implies relative pathnames */
  8532.             pv[n].ival = 1;             /* Set the recursive flag */
  8533.             break;
  8534.  
  8535.           case SND_RES:                 /* /RECOVER */
  8536.             pv[SND_BIN].ival = 1;       /* Implies /BINARY */
  8537.             pv[n].ival = 1;             /* Set the resend flag */
  8538.             break;
  8539.  
  8540.           case SND_DEL:                 /* /DELETE */
  8541.           case SND_SHH:                 /* /QUIET */
  8542.           case SND_CAL:                 /* /CALIBRATE */
  8543.           case SND_XPA:                 /* /TRANSPARENT */
  8544.             pv[n].ival = 1;             /* Just set the appropriate flag */
  8545.             break;
  8546.  
  8547.           case SND_PIP:                 /* /PIPES:{ON,OFF} */
  8548.             if (!getval) {
  8549.                 pv[n].ival = 1;
  8550.                 break;
  8551.             }
  8552.             if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  8553.               goto xgetx;
  8554.             if (!nopush)
  8555.               pv[n].ival = x;
  8556.             break;
  8557.  
  8558.           /* File transfer modes - each undoes the others */
  8559.  
  8560.           case SND_BIN:                 /* Binary */
  8561.           case SND_TXT:                 /* Text */
  8562.           case SND_IMG:                 /* Image */
  8563.           case SND_LBL:                 /* Labeled */
  8564.             pv[SND_BIN].ival = 0;       /* Unset all */
  8565.             pv[SND_TXT].ival = 0;
  8566.             pv[SND_IMG].ival = 0;
  8567.             pv[SND_LBL].ival = 0;
  8568.             pv[n].ival = 1;             /* Set the requested one */
  8569.             break;
  8570.  
  8571.           case SND_EXC:                 /* Excludes */
  8572.             if (!getval) break;
  8573.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  8574.                 if (x == -3) {
  8575.                     printf("?Pattern required\n");
  8576.                     x = -9;
  8577.                 }
  8578.                 goto xgetx;
  8579.             }
  8580.             if (pv[n].sval) free(pv[n].sval);
  8581.             y = strlen(s);
  8582.             if (y > 256) {
  8583.                 printf("?Pattern too long - 256 max\n");
  8584.                 x = -9;
  8585.                 goto xgetx;
  8586.             }
  8587.             pv[n].sval = malloc(y+1);
  8588.             if (pv[n].sval) {
  8589.                 strcpy(pv[n].sval,s);   /* safe */
  8590.                 pv[n].ival = 1;
  8591.             }
  8592.             break;
  8593.  
  8594. #ifdef COMMENT
  8595.           /* Not implemented */
  8596.           case SND_PRI:                 /* GET to printer */
  8597.             pv[n].ival = 1;
  8598.             if (!getval) break;
  8599.             if ((x = cmfld("Print options","",&s,xxstring)) < 0)
  8600.               goto xgetx;
  8601.             pv[n].sval = malloc((int)strlen(s)+1);
  8602.             if (pv[n].sval)
  8603.               strcpy(pv[n].sval,s);     /* safe */
  8604.             break;
  8605. #endif /* COMMENT */
  8606.  
  8607.           case SND_MOV:                 /* MOVE after */
  8608.           case SND_REN:                 /* RENAME after */
  8609.             if (!getval) break;
  8610.             if ((x = cmfld(n == SND_MOV ?
  8611.            "device and/or directory for source file after sending" :
  8612.            "new name for source file after sending",
  8613.                            "",
  8614.                            &s,
  8615.                            n == SND_MOV ? xxstring : NULL
  8616.                            )) < 0) {
  8617.                 if (x == -3) {
  8618.                     printf("%s\n", n == SND_MOV ?
  8619.                            "?Destination required" :
  8620.                            "?New name required"
  8621.                            );
  8622.                     x = -9;
  8623.                 }
  8624.                 goto xgetx;
  8625.             }
  8626.             if (pv[n].sval) {
  8627.                 free(pv[n].sval);
  8628.                 pv[n].sval = NULL;
  8629.             }
  8630.             s = brstrip(s);
  8631.             y = strlen(s);
  8632.             if (y > 0) {
  8633.                 pv[n].sval = malloc(y+1);
  8634.                 if (pv[n].sval) {
  8635.                     strcpy(pv[n].sval,s); /* safe */
  8636.                     pv[n].ival = 1;
  8637.                 }
  8638.             }
  8639.             break;
  8640.  
  8641.           case SND_ASN:                 /* As-name */
  8642.             if (!getval) break;
  8643.             if (mget) {
  8644.                 printf("?Sorry, as-name not allowed with MGET\n");
  8645.                 x = -9;
  8646.                 goto xgetx;
  8647.             }
  8648.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0)
  8649.               goto xgetx;
  8650.             s = brstrip(s);
  8651.             if ((y = strlen(s)) > 0) {
  8652.                 if (pv[n].sval) free(pv[n].sval);
  8653.                 pv[n].sval = malloc(y+1);
  8654.                 if (pv[n].sval) {
  8655.                     strcpy(pv[n].sval,s); /* safe */
  8656.                     pv[n].ival = 1;
  8657.                 }
  8658.             }
  8659.             break;
  8660.  
  8661. #ifdef PIPESEND
  8662.           case SND_FLT:                 /* Filter */
  8663.             debug(F101,"xget /filter getval","",getval);
  8664.             if (!getval) break;
  8665.             if ((x = cmfld("Filter program to receive through",
  8666.                            "",&s,NULL)) < 0) {
  8667.                 if (x == -3)
  8668.                   s = "";
  8669.                 else
  8670.                   goto xgetx;
  8671.             }
  8672.             if (*s) s = brstrip(s);
  8673.             y = strlen(s);
  8674.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  8675.                 if (s[x] != '\\') continue;
  8676.                 if (s[x+1] == 'v') break;
  8677.             }
  8678.             if (x == y) {
  8679.                 printf(
  8680.                 "?Filter must contain a replacement variable for filename.\n"
  8681.                        );
  8682.                 x = -9;
  8683.                 goto xgetx;
  8684.             }
  8685.             pv[n].ival = 1;
  8686.             if (pv[n].sval) {
  8687.                 free(pv[n].sval);
  8688.                 pv[n].sval = NULL;
  8689.             }
  8690.             if ((y = strlen(s)) > 0) {
  8691.                 if ((pv[n].sval = malloc(y+1)))
  8692.                   strcpy(pv[n].sval,s); /* safe */
  8693.             }
  8694.             break;
  8695. #endif /* PIPESEND */
  8696.  
  8697.           case SND_PTH:                 /* Pathnames */
  8698.             if (!getval) {
  8699.                 pv[n].ival = PATH_REL;
  8700.                 break;
  8701.             }
  8702.             if ((x = cmkey(rpathtab,nrpathtab,"","on",xxstring)) < 0)
  8703.               goto xgetx;
  8704.             pv[n].ival = x;             /* Ditto */
  8705.             break;
  8706.  
  8707.           case SND_NAM:                 /* Filenames */
  8708.             if (!getval) break;
  8709.             if ((x = cmkey(fntab,nfntab,"","converted",xxstring)) < 0)
  8710.               goto xgetx;
  8711.             pv[n].ival = x;
  8712.             break;
  8713.  
  8714.           case SND_PRO:                 /* Protocol to use */
  8715.             if (!getval) break;
  8716.             if ((x = cmkey(protos,nprotos,"File-transfer protocol","",
  8717.                            xxstring)) < 0) {
  8718.                 if (x == -3)
  8719.                   x = 0;
  8720.                 else
  8721.                   goto xgetx;
  8722.             }
  8723.             debug(F111,"xget /proto",atmbuf,x);
  8724.             pv[n].ival = x;
  8725.             if (konly && x != PROTO_K) {
  8726.                 printf(
  8727. "?Sorry, this command works only with Kermit protocol\n"
  8728.                        );
  8729.                 x = -9;
  8730.                 goto xgetx;
  8731.             }
  8732.             break;
  8733.  
  8734.           default:
  8735.             printf("?Unexpected switch value - %d\n",cmresult.nresult);
  8736.             x = -9;
  8737.             goto xgetx;
  8738.         }
  8739.     }
  8740.     debug(F101,"xget cmresult fcode","",cmresult.fcode);
  8741.  
  8742.     cmarg = line;                       /* Initialize string pointers */
  8743.     cmarg2 = tmpbuf;
  8744.     asname = 0;
  8745.     line[0] = NUL;                      /* and buffers. */
  8746.     tmpbuf[0] = NUL;
  8747.  
  8748.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  8749.       case _CMFLD:                      /* (3) Remote filespec */
  8750.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  8751.         break;
  8752.       case _CMTXT:                      /* (4) As-name */
  8753.         if (rcvcmd) {
  8754.             ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ);
  8755.             if ((int)strlen(tmpbuf) > 0)
  8756.               asname = 1;
  8757.         } else {
  8758.             ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  8759.         }
  8760.       case _CMCFM:                      /* (6) Confirmation */
  8761.         confirmed = 1;
  8762.         break;
  8763.       default:
  8764.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  8765.         x = -9;
  8766.         goto xgetx;
  8767.     }
  8768.     debug(F110,"xget string",cmarg,0);
  8769.     debug(F101,"xget confirmed","",confirmed);
  8770.  
  8771.     cmarg = brstrip(cmarg);             /* Strip any braces */
  8772.  
  8773.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  8774.         if (pv[SND_CMD].ival > 0) {
  8775.             debug(F100,"xget calling cmtxt","",0);
  8776.             x = cmtxt("Local command to pipe into","",&s,NULL);
  8777.             if (x < 0 && x != -3) goto xgetx;
  8778.             if (x != -3) {
  8779.                 ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8780.                 asname = 1;
  8781.             }
  8782.         } else if (!rcvcmd) {
  8783. #ifdef VMS
  8784.             /* cmofi() fails if you give it a directory name */
  8785.             x = cmfld("Name or directory for incoming file","",&s,NULL);
  8786.             debug(F111,"xget cmfld",s,x);
  8787. #else
  8788.             x = cmofi("Name or directory for incoming file","",&s,NULL);
  8789.             debug(F111,"xget cmofi",s,x);
  8790. #endif /* VMS */
  8791.             if (x < 0 && x != -3) goto xgetx;
  8792.             if (x != -3) {
  8793.                 ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8794.                 if ((x = cmcfm()) < 0) goto xgetx;
  8795.                 asname = 1;
  8796.             }
  8797.         }
  8798.     }
  8799.     /* Arrive here with cmarg and cmarg2 all set */
  8800.  
  8801.     debug(F111,"xget asname",cmarg2,asname);
  8802.     if (!asname) {
  8803.         if (pv[SND_ASN].sval)
  8804.           ckstrncpy(tmpbuf,pv[SND_ASN].sval,TMPBUFSIZ);
  8805.         else
  8806.           tmpbuf[0] = NUL;
  8807.     }
  8808.     cmarg2 = brstrip(cmarg2);           /* Strip outer braces if any. */
  8809.     debug(F110,"xget cmarg",cmarg,0);
  8810.     debug(F110,"xget cmarg2",cmarg2,0);
  8811.  
  8812.     if (!*cmarg &&
  8813.         (cx == XXGET || cx == XXREGET || cx == XXCGET || cx == XXMGET)) {
  8814.         printf("?A remote file specification is required\n");
  8815.         x = -9;
  8816.         goto xgetx;
  8817.     }
  8818. #ifdef PIPESEND
  8819.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND sets pipesend flag */
  8820.         x = -9;
  8821.         if (!*cmarg2) {
  8822.             printf("?Command required\n");
  8823.             goto xgetx;
  8824.         } else if (nopush) {
  8825.             printf("?Sorry, system command access is disabled\n");
  8826.             goto xgetx;
  8827.         } else if (rcvfilter) {
  8828.             printf("?Sorry, no GET /COMMAND while RECEIVE FILTER selected\n");
  8829.             goto xgetx;
  8830.         } else
  8831.           pipesend = 1;
  8832.     }
  8833.     debug(F101,"xget /COMMAND pipesend","",pipesend);
  8834. #endif /* PIPESEND */
  8835.  
  8836. #ifdef CK_RESEND
  8837.     if (pv[SND_RES].ival > 0) {         /* REGET or GET /RECOVER */
  8838. #ifdef RECURSIVE
  8839.         if (pv[SND_REC].ival > 0) {     /* RECURSIVE */
  8840. #ifdef COMMENT
  8841.             printf("?Unsupported option combination: /RECOVER /RECURSIVE\n");
  8842.             x = -9;
  8843.             goto xgetx;
  8844. #else
  8845.             opkt = 1;
  8846. #endif /* COMMENT */
  8847.         }
  8848. #endif /* RECURSIVE */
  8849.         if (pv[SND_DEL].ival > 0) {     /* /DELETE */
  8850. #ifdef COMMENT
  8851.             printf("?Unsupported option combination: /RECOVER /DELETE\n");
  8852.             x = -9;
  8853.             goto xgetx;
  8854. #else
  8855.             opkt = 1;
  8856. #endif /* COMMENT */
  8857.         }
  8858.     }
  8859. #endif /* CK_RESEND */
  8860.  
  8861.     if (pv[SND_EXC].ival > 0)           /* /EXCEPT */
  8862.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  8863.  
  8864. #ifdef IKS_OPTION
  8865.     if (!rcvcmd
  8866. #ifdef CK_XYZ
  8867.          && protocol == PROTO_K
  8868. #endif /* CK_XYZ */
  8869.          ) {
  8870.         if (!iks_wait(KERMIT_REQ_START,1)) {
  8871.             printf(
  8872.               "?A Kermit Server is not available to process this command\n");
  8873.             x = -9;                     /* correct the return code */
  8874.             goto xgetx;
  8875.         }
  8876.     }
  8877. #endif /* IKS_OPTION */
  8878.  
  8879. #ifdef CK_XYZ
  8880.     {
  8881.         int po, pg;                     /* (for clarity) */
  8882.         po = pv[SND_PRO].ival;          /* /PROTOCOL option */
  8883.         pg = protocol;                  /* Protocol global  */
  8884.         if ((rcvcmd && !*cmarg2) &&     /* If no as-name was given */
  8885.             /* and /PROTOCOL is XMODEM or global protocol is XMODEM... */
  8886.             ((po <  0 && (pg == PROTO_X || pg == PROTO_XC)) ||
  8887.              (po > -1 && (po == PROTO_X || po == PROTO_XC)))
  8888.             ) {
  8889.             printf(
  8890. "Sorry, you must specify a name when receiving a file with XMODEM protocol\n"
  8891.                    );
  8892.             x = -9;
  8893.             goto xgetx;
  8894.         }
  8895.     }
  8896. #endif /* CK_XYZ */
  8897.  
  8898. #ifdef RECURSIVE
  8899.     if (pv[SND_REC].ival > 0) {         /* RECURSIVE */
  8900.         recursive = 1;
  8901.         pv[SND_PTH].ival = PATH_REL;    /* Implies relative pathnames too */
  8902.     }
  8903. #endif /* RECURSIVE */
  8904.  
  8905.     if (pv[SND_PIP].ival > -1) {
  8906.         g_usepipes = usepipes;
  8907.         usepipes = pv[SND_PIP].ival;
  8908.     }
  8909.  
  8910.     /* Save global protocol parameters */
  8911.  
  8912.     g_proto = protocol;
  8913. #ifdef CK_LABELED
  8914.     g_lf_opts = lf_opts;                /* Save labeled transfer options */
  8915. #endif /* CK_LABELED */
  8916.     g_urpsiz = urpsiz;                  /* Receive packet length */
  8917.     g_spsizf = spsizf;                  /* Send packet length flag */
  8918.     g_spsiz = spsiz;                    /* Send packet length */
  8919.     g_spsizr = spsizr;                  /* etc etc */
  8920.     g_spmax = spmax;
  8921.     g_wslotr = wslotr;
  8922.     g_prefixing = prefixing;
  8923.     g_fncact = fncact;
  8924.     g_fncnv = fncnv;
  8925.     g_fnspath = fnspath;
  8926.     g_fnrpath = fnrpath;
  8927.     g_xfrxla = xfrxla;
  8928.  
  8929.     if (pv[SND_PRO].ival > -1) {        /* Change according to switch */
  8930.         protocol = pv[SND_PRO].ival;
  8931.         if (ptab[protocol].rpktlen > -1)   /* copied from initproto() */
  8932.             urpsiz = ptab[protocol].rpktlen;
  8933.         if (ptab[protocol].spktflg > -1)
  8934.             spsizf = ptab[protocol].spktflg;
  8935.         if (ptab[protocol].spktlen > -1) {
  8936.             spsiz = ptab[protocol].spktlen;
  8937.             if (spsizf)
  8938.                 spsizr = spmax = spsiz;
  8939.         }
  8940.         if (ptab[protocol].winsize > -1)
  8941.             wslotr = ptab[protocol].winsize;
  8942.         if (ptab[protocol].prefix > -1)
  8943.             prefixing = ptab[protocol].prefix;
  8944.         if (ptab[protocol].fnca > -1)
  8945.             fncact  = ptab[protocol].fnca;
  8946.         if (ptab[protocol].fncn > -1)
  8947.             fncnv   = ptab[protocol].fncn;
  8948.         if (ptab[protocol].fnsp > -1)
  8949.             fnspath = ptab[protocol].fnsp;
  8950.         if (ptab[protocol].fnrp > -1)
  8951.             fnrpath = ptab[protocol].fnrp;
  8952.     }
  8953.     debug(F101,"xget protocol","",protocol);
  8954.     debug(F111,"xget cmarg2",cmarg2,xfermode);
  8955.  
  8956.     g_xfermode = xfermode;
  8957.     g_binary = binary;
  8958.     if (pv[SND_BIN].ival > 0) {         /* Change according to switch */
  8959.         xfermode = XMODE_M;
  8960.         binary = XYFT_B;                /* FILE TYPE BINARY */
  8961.         omode = GMOD_BIN;               /* O-Packet mode */
  8962.         debug(F101,"doxget /BINARY xfermode","",xfermode);
  8963.     } else if (pv[SND_TXT].ival > 0) {  /* Ditto for /TEXT */
  8964.         xfermode = XMODE_M;
  8965.         binary = XYFT_T;
  8966.         omode = GMOD_TXT;
  8967.         debug(F101,"doxget /TEXT xfermode","",xfermode);
  8968.     } else if (pv[SND_IMG].ival > 0) {
  8969.         xfermode = XMODE_M;
  8970. #ifdef VMS
  8971.         binary = XYFT_I;
  8972. #else
  8973.         binary = XYFT_B;
  8974. #endif /* VMS */
  8975.         omode = GMOD_TXT;
  8976.         debug(F101,"doxget /IMAGE xfermode","",xfermode);
  8977.     }
  8978. #ifdef CK_LABELED
  8979.     else if (pv[SND_LBL].ival > 0) {
  8980.         xfermode = XMODE_M;
  8981.         binary = XYFT_L;
  8982.         omode = GMOD_LBL;
  8983.         debug(F101,"doxget /LABELED xfermode","",xfermode);
  8984.     }
  8985. #endif /* CK_LABELED */
  8986.     debug(F101,"xget binary","",binary);
  8987.     debug(F101,"xget omode","",omode);
  8988.  
  8989.     if (pv[SND_XPA].ival > 0)           /* /TRANSPARENT */
  8990.       xfrxla = 0;                       /* Don't translate character sets */
  8991.  
  8992. #ifdef PIPESEND
  8993.     if (pv[SND_FLT].ival > 0)
  8994.       makestr(&rcvfilter,pv[SND_FLT].sval);
  8995. #endif /* PIPESEND */
  8996.  
  8997. #ifdef CK_TMPDIR
  8998.     if (pv[SND_MOV].ival > 0) {
  8999.         int len;
  9000.         char * p = pv[SND_MOV].sval;
  9001. #ifdef CK_LOGIN
  9002.         if (isguest) {
  9003.             printf("?Sorry, /MOVE-TO not available to guests\n");
  9004.             x = -9;
  9005.             goto xgetx;
  9006.         }
  9007. #endif /* CK_LOGIN */
  9008.         len = strlen(p);
  9009.         if (!isdir(p)) {                /* Check directory */
  9010. #ifdef CK_MKDIR
  9011.             char * s = NULL;
  9012.             s = (char *)malloc(len + 4);
  9013.             if (s) {
  9014.                 strcpy(s,p);            /* safe */
  9015. #ifdef datageneral
  9016.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  9017. #else
  9018.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  9019. #endif /* datageneral */
  9020.                 s[len++] = 'X';
  9021.                 s[len] = NUL;
  9022.                 x = zmkdir(s);
  9023.                 free(s);
  9024.                 if (x < 0) {
  9025.                     printf("?Can't create \"%s\"\n",p);
  9026.                     x = -9;
  9027.                     goto xgetx;
  9028.                 }
  9029.             }
  9030. #else
  9031.             printf("?Directory \"%s\" not found\n",p);
  9032.             x = -9;
  9033.             goto xgetx;
  9034. #endif /* CK_MKDIR */
  9035.         }
  9036.         zfnqfp(p,LINBUFSIZ,line);
  9037.         makestr(&rcv_move,line);
  9038.     }
  9039. #endif /* CK_TMPDIR */
  9040.  
  9041.     if (pv[SND_REN].ival > 0) {         /* /RENAME-TO:name */
  9042.         char * p = pv[SND_REN].sval;
  9043. #ifdef CK_LOGIN
  9044.         if (isguest) {
  9045.             printf("?Sorry, /RENAME-TO not available to guests\n");
  9046.             x = -9;
  9047.             goto xgetx;
  9048.         }
  9049. #endif /* CK_LOGIN */
  9050.         if (!p) p = "";
  9051.         if (!*p) {
  9052.             printf("?New name required for /RENAME\n");
  9053.             x = -9;
  9054.             goto xgetx;
  9055.         }
  9056.         p = brstrip(p);
  9057.         makestr(&rcv_rename,p);
  9058.         debug(F110,"xget rcv_rename","",0);
  9059.     }
  9060.  
  9061. #ifdef CALIBRATE
  9062.     if (pv[SND_CAL].ival > 0)
  9063.       calibrate = 1L;
  9064. #endif /* CALIBRATE */
  9065.     g_displa = fdispla;
  9066.     if (pv[SND_SHH].ival > 0)
  9067.       fdispla = 0;
  9068.     debug(F101,"xget display","",fdispla);
  9069.  
  9070.     if (pv[SND_NAM].ival > -1) {        /* /FILENAMES */
  9071.         g_fncnv = fncnv;                /* Save global value */
  9072.         fncnv = pv[SND_NAM].ival;
  9073.         debug(F101,"xsend fncnv","",fncnv);
  9074.         /* We should also handle O packet filename option here */
  9075.         /* but we don't really need to since WHATAMI already handles it */
  9076.     }
  9077.     if (pv[SND_PTH].ival > -1) {        /* PATHNAMES */
  9078.         g_rpath = fnrpath;              /* Save global values */
  9079.         fnrpath = pv[SND_PTH].ival;
  9080.         debug(F101,"xsend fnrpath","",fnrpath);
  9081. #ifndef NZLTOR
  9082.         if (fnrpath != PATH_OFF) {
  9083.             g_fncnv = fncnv;
  9084.             fncnv = XYFN_L;
  9085.             debug(F101,"xsend fncnv","",fncnv);
  9086.         }
  9087.         /* We should also handle O packet pathname option here */
  9088.         /* but we don't really need to since WHATAMI already handles it */
  9089. #endif /* NZLTOR */
  9090.     }
  9091.  
  9092.     /* Set protocol start state */
  9093.  
  9094.     if (opkt) {                         /* Extended GET Options*/
  9095.         sstate = (CHAR) 'o';
  9096.         oopts = 0;
  9097.         if (pv[SND_DEL].ival > 0) oopts |= GOPT_DEL; /* GET /DELETE */
  9098.         if (pv[SND_RES].ival > 0) oopts |= GOPT_RES; /* GET /RECOVER */
  9099.         if (pv[SND_REC].ival > 0) oopts |= GOPT_REC; /* GET /RECURSIVE */
  9100.     } else if (rcvcmd)
  9101.       sstate = (CHAR) 'v';              /* RECEIVE or CRECEIVE */
  9102.     else if (pv[SND_DEL].ival > 0)
  9103.       sstate = (CHAR) 'h';              /* GET /DELETE (= RETRIEVE) */
  9104.     else if (pv[SND_RES].ival > 0)
  9105.       sstate = (CHAR) 'j';              /* GET /RECOVER (= REGET) */
  9106.     else
  9107.       sstate = (CHAR) 'r';              /* Regular GET */
  9108.     getcmd = 1;
  9109.     debug(F000,"xget sstate","",sstate);
  9110. #ifdef MAC
  9111.     what = W_RECV;
  9112.     scrcreate();
  9113. #endif /* MAC */
  9114.     if (local) {
  9115.         if (pv[SND_SHH].ival != 0)
  9116.           displa = 1;
  9117.         ttflui();
  9118.     }
  9119.     x = 0;
  9120. #ifdef PIPESEND
  9121.     if (pipesend)
  9122.       goto xgetx;
  9123. #endif /* PIPESEND */
  9124.  
  9125. #ifdef CK_TMPDIR
  9126. /*
  9127.   cmarg2 is also allowed to be a device or directory name;
  9128.   even the name of a directory that doesn't exist.
  9129. */
  9130.     y = strlen(cmarg2);
  9131.     debug(F111,"xget strlen(cmarg2)",cmarg2,y);
  9132.     if ((y > 0) &&
  9133. #ifdef OS2
  9134.         ((isalpha(cmarg2[0]) &&
  9135.          cmarg2[1] == ':' &&
  9136.          cmarg2[2] == NUL) ||
  9137.         (cmarg[y-1] == '/' || cmarg[y-1] == '\\') ||
  9138.         isdir(cmarg2))
  9139. #else
  9140. #ifdef UNIXOROSK
  9141.         (cmarg2[y-1] == '/' || isdir(cmarg2))
  9142. #else
  9143. #ifdef VMS
  9144.         (cmarg2[y-1] == ']' || cmarg2[y-1] == '>' || isdir(cmarg2))
  9145. #else
  9146. #ifdef STRATUS
  9147.         (cmarg2[y-1] == '>' || isdir(cmarg2))
  9148. #else
  9149. #ifdef datageneral
  9150.         (cmarg2[y-1] == ':' || cmarg[0] == ':' || isdir(cmarg2))
  9151. #else
  9152.         isdir(cmarg2)
  9153. #endif /* datageneral */
  9154. #endif /* STRATUS */
  9155. #endif /* VMS */
  9156. #endif /* UNIXOROSK */
  9157. #endif /* OS2 */
  9158.         ) {
  9159.         debug(F110,"doxget RECEIVE cmarg2 disk or dir",cmarg2,0);
  9160.         if (!f_tmpdir) {
  9161.             int x;
  9162.             s = zgtdir();
  9163.             if (s) {
  9164.                 ckstrncpy(savdir,s,TMPDIRLEN); /* remember old disk/dir */
  9165.                 f_tmpdir = 1;   /* and that we did this */
  9166.             } else {
  9167.                 printf("?Can't get current directory\n");
  9168.                 cmarg2 = "";
  9169.                 f_tmpdir = 0;
  9170.                 x = -9;
  9171.                 goto xgetx;
  9172.             }
  9173. #ifdef CK_MKDIR
  9174.             x = zchki(cmarg2);          /* Does as-name exist? */
  9175.             if (x == -1) {              /* Doesn't exist */
  9176.                 char * p = NULL;        /* Try to create it */
  9177.                 x = strlen(cmarg2);
  9178.                 if ((p = (char *)malloc(x+4))) {
  9179.                     sprintf(p,"%s%s",cmarg2,"x.x"); /* SAFE (prechecked) */
  9180.                     x = zmkdir(p);
  9181.                     free(p);
  9182.                     if (x < 0) {
  9183.                         printf("?Can't create %s\n",cmarg2);
  9184.                         x = -9;
  9185.                         goto xgetx;
  9186.                     }
  9187.                 }
  9188.             }
  9189. #endif /* CK_MKDIR */
  9190.             if (!zchdir(cmarg2)) {      /* change to given disk/directory, */
  9191.                 printf("?Can't access %s\n",cmarg2);
  9192.                 x = -9;
  9193.                 goto xgetx;
  9194.             }
  9195.             cmarg2 = "";
  9196.         }
  9197.     }
  9198. #endif /* CK_TMPDIR */
  9199.  
  9200.     ckstrncpy(fspec,cmarg,CKMAXPATH);   /* Note - this is a REMOTE filespec */
  9201.     debug(F111,"xget fspec",fspec,fspeclen);
  9202.     debug(F110,"xget cmarg2",cmarg2,0);
  9203.  
  9204.   xgetx:
  9205.     for (i = 0; i < SND_MAX; i++)
  9206.       if (pv[i].sval)
  9207.         free(pv[i].sval);
  9208.     return(x);
  9209. }
  9210. #endif /* NOXFER */
  9211.  
  9212. #ifndef NOSPL
  9213.  
  9214. /*
  9215.   D O G T A  --  Do _GETARGS or _PUTARGS Command.
  9216.  
  9217.   Used by XIF, FOR, WHILE, and SWITCH, each of which are implemented as
  9218.   2-level macros; the first level defines the macro, the second runs it.
  9219.   This routine hides the fact that they are macros by importing the
  9220.   macro arguments (if any) from two levels up, to make them available
  9221.   in the IF, FOR, SWITCH, and WHILE commands themselves; for example as
  9222.   loop indices, etc, and within the IF/FOR/WHILE/SWITCH body itself.
  9223.   _PUTARGS is in case we changed any of these variables or used SHIFT
  9224.   on them, so the new values won't be lost as we pop up the stack.
  9225. */
  9226. int
  9227. dogta(cx) int cx; {
  9228.     int i, n;
  9229.     char c, *p,  mbuf[4];
  9230.     extern int topargc, cmdint;
  9231.     extern char ** topxarg;
  9232.  
  9233.     if ((y = cmcfm()) < 0)
  9234.       return(y);
  9235.     debug(F101,"dogta cx","",cx);
  9236.     debug(F101,"dogta maclvl","",maclvl);
  9237.     if (cx == XXGTA) {
  9238.         debug(F101,"dogta _GETARGS maclvl","",maclvl);
  9239.     } else if (cx == XXPTA) {
  9240.         debug(F101,"dogta _PUTARGS maclvl","",maclvl);
  9241.     } else {
  9242.         return(-2);
  9243.     }
  9244.     if (maclvl < 1)
  9245.       return(success = 0);
  9246.  
  9247.     /* Make new copies of macro arguments /%0..9 */
  9248.  
  9249.     mbuf[0] = '%'; mbuf[1] = '0'; mbuf[2] = NUL; /* Argument name buf */
  9250.  
  9251.     if (cx == XXPTA) {                  /* Go NOINT because _PUTARGS */
  9252.         if (cmdint)                     /* temporarily changes maclvl. */
  9253.           connoi();                     /* Interrupts OFF. */
  9254.     }
  9255.     for (i = 0; i < 10; i++) {          /* For all args */
  9256.         c = (char) (i + '0');           /* Make name */
  9257.         mbuf[1] = (char) c;             /* Insert digit */
  9258.         if (cx == XXGTA) {              /* Get arg from level-minus-2 */
  9259.             if (maclvl == 1) p = g_var[c]; /* If at level 1 use globals 0..9 */
  9260.             else p = m_arg[maclvl-2][i];   /* Otherwise they're on the stack */
  9261.             addmac(mbuf,p);
  9262. #ifdef COMMENT
  9263.             if (maclvl > 1)
  9264.               makestr(&(m_line[maclvl]),m_line[maclvl-2]);
  9265. #endif /* COMMENT */
  9266.         } else if (cx == XXPTA) {       /* Put args level+2 */
  9267.             maclvl -= 2;                /* This is gross, it's because we're */
  9268.             addmac(mbuf,m_arg[maclvl+2][i]); /* adding macros two levels up */
  9269.             maclvl += 2;                     /* and addmac() uses maclvl. */
  9270.             count[cmdlvl - 2]  = count[cmdlvl];
  9271.             intime[cmdlvl - 2] = intime[cmdlvl];
  9272.             inpcas[cmdlvl - 2] = inpcas[cmdlvl];
  9273.             takerr[cmdlvl - 2] = takerr[cmdlvl];
  9274.             merror[cmdlvl - 2] = merror[cmdlvl];
  9275.             xquiet[cmdlvl - 2] = xquiet[cmdlvl];
  9276.         } else return(success = 0);     /* Bad call to this routine */
  9277.     }
  9278.     if (cx == XXPTA) {                  /* Restore interrupts if we */
  9279.         if (cmdint)                     /* turned them off above. */
  9280.           conint(trap,stptrap);
  9281.     }
  9282.     /* Now take care of the argument vector array \&_[], \v(return), */
  9283.     /* and \v(argc) by just copying the pointers. */
  9284.  
  9285.     if (cx == XXGTA) {                  /* GETARGS from 2 levels up */
  9286.         if (maclvl == 1) {
  9287.             a_ptr[0] = topxarg;         /* \&_[] array */
  9288.             a_dim[0] = topargc - 1;     /* Dimension doesn't include [0] */
  9289.             m_xarg[maclvl] = topxarg;
  9290.             n_xarg[maclvl] = topargc;   /* But \v(argc) does include \%0 */
  9291.             macargc[maclvl] = topargc;
  9292.             makestr(&(mrval[maclvl+1]),mrval[0]); /* (see vnlook()) */
  9293.         } else {
  9294.             a_ptr[0] = m_xarg[maclvl-2];
  9295.             a_dim[0] = n_xarg[maclvl-2];
  9296.             m_xarg[maclvl] = m_xarg[maclvl-2];
  9297.             n_xarg[maclvl] = n_xarg[maclvl-2];
  9298.             macargc[maclvl] = n_xarg[maclvl-2];
  9299.             makestr(&(mrval[maclvl+1]),mrval[maclvl-1]); /* (see vnlook()) */
  9300.  
  9301.         }
  9302.     } else {                            /* PUTARGS 2 levels up */
  9303.         if (maclvl > 1) {
  9304.             a_ptr[0] = m_xarg[maclvl];
  9305.             m_xarg[maclvl-2] = m_xarg[maclvl];
  9306.             a_dim[0] = n_xarg[maclvl];
  9307.             n_xarg[maclvl-2] = n_xarg[maclvl];
  9308.             macargc[maclvl-2] = n_xarg[maclvl];
  9309.         }
  9310.     }
  9311.     return(1);                  /* Internal command - don't change success */
  9312. }
  9313. #endif /* NOSPL */
  9314.  
  9315. #ifndef NOSPL
  9316. /*
  9317.   Do the GOTO and [_]FORWARD commands.
  9318.   s = Label to search for, cx = function code: XXGOTO, XXFWD, or XXXFWD.
  9319. */
  9320. #ifdef BIGBUFOK
  9321. #define LBLMAXLEN 255                   /* Max label length */
  9322. #else
  9323. #define LBLMAXLEN 63
  9324. #endif /* BIGBUFOK */
  9325.  
  9326. int
  9327. dogoto(s, cx) char *s; int cx; {
  9328.     int i, j, x, y, z, bc;
  9329.     int empty = 0, stopflg = 0;
  9330.     char * cmd;                         /* Name of this command */
  9331.     char tmplbl[LBLMAXLEN+1], *lp;      /* Current label from command stream */
  9332.     char tmp2[LBLMAXLEN+1];             /* SWITCH label conversion buffer */
  9333.     char tmp3[LBLMAXLEN+1];             /* Target label */
  9334.  
  9335.     stopflg = (cx == XXXFWD);           /* _FORWARD (used in SWITCH) */
  9336.     bc = 0;                             /* Brace counter */
  9337.  
  9338.     cmd = (cx == XXGOTO) ? "GOTO" : ((cx == XXFWD) ? "FORWARD" : "_FORWARD");
  9339.     if (!s) s = "";
  9340.     if (!*s) empty = 1;
  9341.  
  9342. #ifdef DEBUG
  9343.     if (deblog) {
  9344.         debug(F111,"GOTO command",cmd,cx);
  9345.         debug(F101,"GOTO cmdlvl","",cmdlvl);
  9346.         debug(F101,"GOTO maclvl","",maclvl);
  9347.         debug(F101,"GOTO tlevel","",tlevel);
  9348.         debug(F111,"GOTO target",s,empty);
  9349.     }
  9350. #endif /* DEBUG */
  9351.     debug(F110,cmd,s,0);
  9352.     ckstrncpy(tmp3+1,s,LBLMAXLEN-1);
  9353.     s = tmp3+1;
  9354.     if (*s != ':') {                    /* Make copy of label */
  9355.         tmp3[0] = ':';                  /* guaranteed to start with ":" */
  9356.         s--;
  9357.     }
  9358.     if (!stopflg && !empty) {
  9359.         if (s[1] == '.' || s[1] == SP || s[1] == NUL) {
  9360.             printf("?Bad label syntax - '%s'\n",s);
  9361.             return(success = 0);
  9362.         }
  9363.     }
  9364.     if (cmdlvl == 0) {
  9365.         printf("?Sorry, %s only works in a command file or macro\n",cmd);
  9366.         return(success = 0);
  9367.     }
  9368.     y = strlen(s);                      /* y = length of target label */
  9369.     debug(F111,cmd,s,y);
  9370.  
  9371.     while (cmdlvl > 0) {                /* As long as not at top level... */
  9372.         if (cmdstk[cmdlvl].src == CMD_MD) { /* GOTO inside macro */
  9373.             int i, m, flag;
  9374.             char *xp, *tp;
  9375.  
  9376.             /* GOTO: rewind the macro; FORWARD: start at current position */
  9377.  
  9378.             lp = (cx == XXGOTO) ? macx[maclvl] : macp[maclvl];
  9379.             m = (int)strlen(lp) - y + 1;
  9380.             debug(F010,"GOTO in macro",lp,0);
  9381.  
  9382.             flag = 1;                   /* flag for valid label position */
  9383.             for (i = 0; i < m; i++,lp++) { /* search for label in macro body */
  9384.                 if (*lp == '{')         /* But only at this level */
  9385.                   bc++;                 /* Anything inside braces is off */
  9386.                 else if (*lp == '}')    /* limits. */
  9387.                   bc--;
  9388.                 if (stopflg && bc > 0)  /* This is good for SWITCH */
  9389.                   continue;             /* but interferes with WHILE, etc. */
  9390.                 if (*lp == ',') {
  9391.                     flag = 1;
  9392.                     continue;
  9393.                 }
  9394.                 if (flag) {             /* If in valid label position */
  9395.                     if (*lp == SP)      /* eat leading spaces */
  9396.                       continue;
  9397.                     if (*lp != ':') {   /* Look for label introducer */
  9398.                         flag = 0;       /* this isn't it */
  9399.                         continue;       /* keep looking */
  9400.                     }
  9401.                 }
  9402.                 if (!flag)              /* We don't have a label */
  9403.                   continue;             /*  so keep looking... */
  9404.                 xp = lp; tp = tmplbl;   /* Copy the label from the macro */
  9405.                 j = 0;                  /* to make it null-terminated */
  9406.                 while ((*tp = *xp)) {
  9407.                     if (j++ > LBLMAXLEN) /* j = length of word from macro */
  9408.                       break;
  9409. #ifdef COMMENT
  9410.                     if (*tp < 33 || *tp == ',') /* Look for end of word */
  9411. #else
  9412.                     if (!*tp || *tp == ',')     /* Look for end of word */
  9413. #endif /* COMMENT */
  9414.                       break;
  9415.                     else tp++, xp++;    /* Next character */
  9416.                 }
  9417.                 *tp = NUL;              /* In case we stopped early */
  9418.                 /* Now do caseless string comparison, using longest length */
  9419.                 debug(F111,"macro GOTO label",s,y);
  9420.                 debug(F111,"macro target label",tmplbl,j);
  9421.                 if (stopflg) {          /* Allow variables as SWITCH labels */
  9422.                     int n = LBLMAXLEN - 1;
  9423.                     char * p = tmp2;
  9424.                     zzstring(tmplbl,&p,&n);
  9425.                     ckstrncpy(tmplbl,tmp2,LBLMAXLEN);
  9426.                     tmp2[49] = NUL;
  9427.                 }
  9428.                 debug(F111,"GOTO s",s,y);
  9429.                 debug(F111,"GOTO tmplbl",tmplbl,j);
  9430.                 debug(F101,"GOTO empty",ckitoa(stopflg),empty);
  9431.  
  9432.                 if (empty) {           /* Empty target */
  9433.             z = (!strcmp(s,":") && /* String is empty */
  9434.              /* and Label is ":" or ":*"... */
  9435.              (!strcmp(tmplbl,":") || !strcmp(tmplbl,":*")))
  9436.             ? 0 : 1;
  9437.             debug(F111,"GOTO","A",z);
  9438.                 } else if (stopflg) {
  9439.                     z = ckmatch(tmplbl,s,inpcas[cmdlvl],1) ? 0 : 1;
  9440.             debug(F111,"GOTO","B",z);
  9441.                 } else {
  9442.                     z = (stopflg && inpcas[cmdlvl]) ?
  9443.                       strcmp(s,tmplbl) :
  9444.                         ckstrcmp(s,tmplbl,(y > j) ? y : j, 0);
  9445.             debug(F111,"GOTO","C",z);
  9446.                 }
  9447.                 if (!z) {
  9448.                     break;
  9449.                 } else if (stopflg &&
  9450.                          !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j, 0)) {
  9451.                     debug(F100,"GOTO DEFAULT","",0);
  9452.                     break;
  9453.                 } else {
  9454.                     flag = 0;
  9455.                 }
  9456.             }
  9457.             debug(F111,"GOTO macro i",cmd,i);
  9458.             debug(F111,"GOTO macro m",cmd,m);
  9459.             if (i >= m) {               /* Didn't find the label */
  9460.                 debug(F101,"GOTO failed cmdlvl","",cmdlvl);
  9461. #ifdef COMMENT
  9462.         /* MOVED TO AFTER POPCLVL ABOUT 20 LINES DOWN 5 AUG 2002 */
  9463.            if (stopflg)
  9464.                   return(0);
  9465. #endif /* COMMENT */
  9466.                 if ((maclvl > 0) &&
  9467.                        (m_arg[maclvl-1][0]) &&
  9468.                        (cmdstk[cmdlvl].src == CMD_MD) &&
  9469.                        (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  9470.                         !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  9471.                         !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  9472.                         !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  9473.                     dogta(XXPTA);       /* Restore args */
  9474.                     debug(F101,"GOTO in XIF/FOR/WHI/SWI popping","",cmdlvl);
  9475.                     popclvl();          /* Pop an extra level */
  9476.                 }
  9477.                 debug(F101,"GOTO popping","",cmdlvl);
  9478.                 if (!popclvl()) {       /* pop up to next higher level */
  9479.                     printf("?Label '%s' not found\n",s); /* if none */
  9480.                     return(0);          /* Quit */
  9481.                 } else if (stopflg) {    /* SWITCH no case label match */
  9482.             return(0);        /* and no DEFAULT lable. */
  9483.         } else {
  9484.             continue;        /* otherwise look again */
  9485.         }
  9486.             }
  9487.             debug(F110,"GOTO found macro label",tmplbl,0);
  9488.             macp[maclvl] = lp;          /* set macro buffer pointer */
  9489.             return(1);
  9490.         } else if (cmdstk[cmdlvl].src == CMD_TF) {
  9491.             x = 0;                      /* GOTO issued in take file */
  9492.             debug(F111,"GOTO in TAKE file",cmd,cx);
  9493.             if (cx == XXGOTO) {         /* If GOTO, but not FORWARD, */
  9494.                 rewind(tfile[tlevel]);  /* search file from beginning */
  9495.                 tfline[tlevel] = 0;
  9496.             }
  9497.             while (! feof(tfile[tlevel])) {
  9498. #ifdef COMMENT
  9499. /* This is wrong - it lets us jump to labels inside inferior blocks */
  9500.                 tfline[tlevel]++;
  9501.                 if (fgets(line,LINBUFSIZ,tfile[tlevel]) == NULL) /* Get line */
  9502. #else
  9503.                 if (getnct(line,LINBUFSIZ,tfile[tlevel],0) < 0)
  9504. #endif /* COMMENT */
  9505.                   break;                /* If no more, done, label not found */
  9506.                 lp = line;              /* Got line */
  9507.                 while (*lp == SP || *lp == HT)
  9508.                   lp++;                 /* Strip leading whitespace */
  9509.                 if (*lp != ':') continue; /* Check for label introducer */
  9510.                 while (*(lp+1) == SP) { /* Remove space between : and name */
  9511.                     *(lp+1) = ':';
  9512.                     lp++;               /* Strip leading whitespace */
  9513.                 }
  9514.                 tp = lp;                /* Get end of word */
  9515.                 j = 0;
  9516.                 while (*tp) {           /* And null-terminate it */
  9517.                     if (*tp < 33) {
  9518.                         *tp = NUL;
  9519.                         break;
  9520.                     } else tp++, j++;
  9521.                 }
  9522.                 if (!ckstrcmp(lp,s,(y > j) ? y : j,0)) { /* Caseless compare */
  9523.                     x = 1;              /* Got it */
  9524.                     break;              /* done. */
  9525.                 } else if (stopflg &&
  9526.                            !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j,0)) {
  9527.                     x = 1;
  9528.                     break;
  9529.                 }
  9530.             }
  9531.             if (x == 0) {               /* If not found, print message */
  9532.                 debug(F101,"GOTO failed at cmdlvl","",cmdlvl);
  9533.                 if (stopflg)
  9534.                   return(0);
  9535.                 if (!popclvl()) {       /* pop up to next higher level */
  9536.                     printf("?Label '%s' not found\n",s); /* if none */
  9537.                     return(0);          /* quit */
  9538.                 } else continue;        /* otherwise look again */
  9539.             }
  9540.             return(x);                  /* Send back return code */
  9541.         }
  9542.     }
  9543.     printf("?Stack problem in GOTO %s\n",s); /* Shouldn't see this */
  9544.     return(0);
  9545. }
  9546. #endif /* NOSPL */
  9547.  
  9548. /* Finish parsing and do the IF, XIF, and WHILE commands */
  9549.  
  9550. #ifndef NOSPL
  9551.  
  9552. /*  C H K V A R  --  Check (if it's a) Variable  */
  9553.  
  9554.  
  9555. #ifdef OLDCHKVAR
  9556. /*
  9557.   Crude and disgusting, but needed for OS/2, DOS, and Windows, where filenames
  9558.   have backslashes in them.  How do we know if a backslash in a filename is a
  9559.   directory separator, or if it's a Kermit backslash?  This routine does a
  9560.   rough syntax check of the next few characters and if it looks like it MIGHT
  9561.   be a variable, then it tries to evaluate it, and if the result is not empty,
  9562.   we say it's a variable, although sometimes it might not be -- some cases are
  9563.   truly ambiguous.  For example there might a DOS directory called \%a, and
  9564.   we also have a variable with the same name.  This is all for the sake of not
  9565.   having to tell PC users that they have to double all backslashes in file
  9566.   and directory names.
  9567. */
  9568. #else
  9569. /*
  9570.   Somewhat less crude & disgusting.  The previous method was nondeterministic
  9571.   and (worse) it interfered with macro argument passing.  So now we only
  9572.   check the syntax of backslash-items to see if they are variables, but we
  9573.   do NOT check their values.
  9574. */
  9575. #endif /* OLDCHKVAR */
  9576. /*
  9577.   Call with a string pointer pointing at the backslash of the purported
  9578.   variable.  Returns 1 if it has the syntax of a variable, 0 if not.
  9579. */
  9580. int
  9581. chkvar(s) char *s; {
  9582.     int z = 0;                          /* Return code - assume failure. */
  9583.     if (!s) s = "";                     /* Watch our for null pointers. */
  9584.     if (!*s) return(0);                 /* Empty arg so not a variable. */
  9585.     if (*s == CMDQ) {                   /* Object begins with backslash. */
  9586.         char c;
  9587.         c = s[1];                       /* Character following backslash. */
  9588.         if (c) {
  9589.             int t = 0;
  9590.             if (c == CMDQ)              /* Quoted backslash */
  9591.               return(1);
  9592.             c = (char) (islower(c) ? toupper(c) : c); /* Otherwise... */
  9593.             if (c == '%') {             /* Simple variable */
  9594. #ifdef OLDCHKVAR
  9595.                 t = 1;
  9596. #else
  9597.                 return(1);
  9598. #endif /* OLDCHKVAR */
  9599.             } else if (c == '&') {      /* Array */
  9600.                 if (!s[2]) return(0);
  9601.                 if (s[3] == '[')
  9602.                   t = ckindex("]",s,4,0,1);
  9603. #ifndef OLDCHKVAR
  9604.                 return((t > 0) ? 1 : 0);
  9605. #endif /* OLDCHKVAR */
  9606.             } else if (c == '$' ||      /* Environment variable */
  9607.                        c == 'V' ||      /* Built-in variable */
  9608.                        c == 'M') {      /* Macro name */
  9609.                 t = (s[2] == '(');
  9610. #ifndef OLDCHKVAR
  9611.                 return((t > 0) ? 1 : 0);
  9612. #endif /* OLDCHKVAR */
  9613.             } else if (c == 'F') {      /* Function reference */
  9614.                 /* Don't actually call it - it might have side effects */
  9615.                 int x;
  9616.                 if ((x = ckindex("(",s,3,0,1))) /* Just check syntax */
  9617.                   if ((x = ckindex(")",s,x,0,1)))
  9618.                     z = 1;
  9619.                 /* Insert a better syntax check here if necessary */
  9620.             }
  9621. #ifdef OLDCHKVAR
  9622.             if (t) {
  9623.                 t = 255;                /* This lets us test \v(xxx) */
  9624.                 lp = line;              /* and even \f...(xxx) */
  9625.                 zzstring(s,&lp,&t);     /* Evaluate it, whatever it is. */
  9626.                 t = strlen(line);       /* Get its length. */
  9627.                 debug(F111,"chkvar",line,t);
  9628.                 z = t > 0;              /* If length > 0, it's defined */
  9629.             }
  9630. #endif /* OLDCHKVAR */
  9631.         }
  9632.     }
  9633.     return(z);
  9634. }
  9635.  
  9636. /*  B O O L E X P  --  Evaluate a Boolean expression  */
  9637.  
  9638. #define BOOLLEN 1024
  9639. static char boolval[BOOLLEN];
  9640.  
  9641. int
  9642. boolexp(cx) int cx; {
  9643.     int x, y, z; char *s, *p;
  9644.     int parens = 0, pcount = 0, ecount = 0;
  9645.     char *q, *bx;
  9646.     struct FDB kw, nu;
  9647. #ifdef FNFLOAT
  9648.     struct FDB fl;
  9649.     CKFLOAT f1 = 0.0, f2 = 0.0;
  9650.     int f1flag = 0, f2flag = 0;
  9651. #endif /* FNFLOAT */
  9652. #ifdef OS2
  9653.     extern int keymac;
  9654. #endif /* OS2 */
  9655.  
  9656.     not = 0;                            /* Flag for whether "NOT" was seen */
  9657.     z = 0;                              /* Initial IF condition */
  9658.     ifargs = 0;                         /* Count of IF condition words */
  9659.     bx = boolval;                       /* Initialize boolean value */
  9660.     *bx = NUL;
  9661.  
  9662.   ifagain:
  9663.     cmfdbi(&kw,                         /* First FDB - command switches */
  9664.            _CMKEY,                      /* fcode */
  9665.            "Number, numeric-valued variable, Boolean expression, or keyword",
  9666.            "",                          /* default */
  9667.            "",                          /* addtl string data */
  9668.            nif,                         /* addtl numeric data 1: tbl size */
  9669.            0,                           /* addtl numeric data 2: 4 = silent */
  9670.            xxstring,                    /* Processing function */
  9671.            iftab,                       /* Keyword table */
  9672.            &nu                          /* Pointer to next FDB */
  9673.            );
  9674.     cmfdbi(&nu,                         /* 2nd FDB - An integer */
  9675.            _CMNUM,                      /* fcode */
  9676.            "",                          /* hlpmsg */
  9677.            "",                          /* Default */
  9678.            "",                          /* addtl string data */
  9679.            0,
  9680.            0,
  9681.            xxstring,
  9682.            NULL,
  9683. #ifdef FNFLOAT
  9684.            &fl
  9685. #else
  9686.            NULL
  9687. #endif /* FNFLOAT */
  9688.            );
  9689. #ifdef FNFLOAT
  9690.     cmfdbi(&fl,                         /* A floating-point number */
  9691.            _CMFLD,                      /* fcode */
  9692.            "",                          /* hlpmsg */
  9693.            "",                          /* default */
  9694.            "",                          /* addtl string data */
  9695.            0,                           /* addtl numeric data 1 */
  9696.            0,                           /* addtl numeric data 2 */
  9697.            xxstring,
  9698.            NULL,
  9699.            NULL
  9700.            );
  9701. #endif /* FNFLOAT */
  9702.     x = cmfdb(&kw);                     /* Parse a keyword or a number */
  9703.     debug(F111,"boolval cmfdb","",x);
  9704.     if (x < 0) {
  9705.         if (x == -3)
  9706.           x = -2;
  9707.         return(x);
  9708.     }
  9709.     debug(F111,"boolval switch","",cmresult.fcode);
  9710.     switch (cmresult.fcode) {           /* What did we get? */
  9711. #ifdef FNFLOAT
  9712.       case _CMFLD:                      /* A "field" */
  9713.         if (isfloat(cmresult.sresult,0)) { /* A floating-point number? */
  9714.             f1 = floatval;              /* Yes, get its value */
  9715.             f1flag = 1;                 /* remember we did this */
  9716.             ifc = 9999;                 /* Set special "if-code" */
  9717.         } else
  9718.           return(-2);
  9719. #endif /* FNFLOAT */
  9720.       case _CMNUM:                      /* A number... */
  9721.         ifc = 9999;                     /* Set special "if-code" */
  9722.         break;
  9723.       case _CMKEY:                      /* A keyword */
  9724.         ifc = cmresult.nresult;         /* Get if-code */
  9725.         break;
  9726.       default:
  9727.         return(-2);
  9728.     }
  9729.     switch (ifc) {                      /* set z = 1 for true, 0 for false */
  9730.       case 9999:                        /* Number */
  9731. #ifdef FNFLOAT
  9732.         if (f1flag) {
  9733.             z = (f1 == 0.0) ? 0 : 1;
  9734.         } else
  9735. #endif /* FNFLOAT */
  9736.         z = (cmresult.nresult == 0) ? 0 : 1;
  9737.         break;
  9738.       case XXIFLP:                      /* Left paren */
  9739.         if (pcount == 0 && ifargs > 0)
  9740.           return(-2);
  9741.         parens = 1;
  9742.         pcount++;
  9743.         ifargs++;
  9744.         *bx++ = '(';
  9745.         goto ifagain;
  9746.       case XXIFRP:                      /* Right paren */
  9747.         if (!parens)
  9748.           return(-2);
  9749.         if (--pcount < 0)
  9750.           return(-2);
  9751.         ifargs++;
  9752.         *bx++ = ')';
  9753.         *bx = NUL;
  9754.         if (pcount == 0)
  9755.           goto ifend;
  9756.         goto ifagain;
  9757.       case XXIFAN:                      /* AND (&&) */
  9758.         ifargs++;
  9759.         if (!ecount)
  9760.           return(-2);
  9761.         *bx++ = '&';
  9762.         goto ifagain;
  9763.       case XXIFOR:                      /* OR (||) */
  9764.         ifargs++;
  9765.         if (!ecount)
  9766.           return(-2);
  9767.         *bx++ = '|';
  9768.         goto ifagain;
  9769.       case XXIFNO:                      /* IF NOT [ NOT [ NOT ... ] ] */
  9770.         if (bx > boolval) {             /* evala() doesn't like cascaded */
  9771.             if (*(bx-1) == '!') {       /* unary operators... */
  9772.                 *(bx-1) = NUL;          /* So here, two wrongs make a right. */
  9773.                 bx--;
  9774.             } else {
  9775.                 *bx++ = '!';
  9776.             }
  9777.         } else {
  9778.             *bx++ = '!';
  9779.         }
  9780.         ifargs++;
  9781.         goto ifagain;
  9782.       case XXIFTR:                      /* IF TRUE */
  9783.         z = 1;
  9784.         debug(F101,"if true","",z);
  9785.         ifargs += 1;
  9786.         break;
  9787.       case XXIFNT:                      /* IF FALSE */
  9788.         z = 0;
  9789.         debug(F101,"if true","",z);
  9790.         ifargs += 1;
  9791.         break;
  9792.       case XXIFSU:                      /* IF SUCCESS */
  9793.         z = ( success != 0 ) ? 1 : 0;
  9794.         debug(F101,"if success","",z);
  9795.         ifargs += 1;
  9796.         break;
  9797.       case XXIFFA:                      /* IF FAILURE */
  9798.         z = ( success == 0 ) ? 1 : 0;
  9799.         debug(F101,"if failure","",z);
  9800.         ifargs += 1;
  9801.         break;
  9802.  
  9803.       case XXIFDE:                      /* IF DEFINED */
  9804.         if ((x = cmfld("Macro or variable name","",&s,NULL)) < 0)
  9805.           return((x == -3) ? -2 : x);
  9806.  
  9807.         if (*s == CMDQ) {
  9808.             char * lp;
  9809.             char line[256];
  9810.             int t, x;
  9811.             if (*(s+1) == 'f' || *(s+1) == 'F') { /* Built-in function */
  9812.                 extern struct keytab fnctab[];
  9813.                 extern int nfuncs;
  9814.                 ckstrncpy(line,s+2,256);
  9815.                 if (line[0]) {
  9816.                     lp = ckstrchr(line,'(');
  9817.                     if (lp) *lp = NUL;
  9818.                     x = lookup(fnctab,line,nfuncs,&t);
  9819.                     z = x > -1;
  9820.                 }
  9821.                 debug(F111,"if defined function",line,z);
  9822.             } else if (*(s+1) == 'v' || *(s+1) == 'V') { /* 8.0.200 */
  9823.                 extern struct keytab vartab[];
  9824.                 extern int nvars;
  9825.                 z = 0;
  9826.                 if (*(s+2) == '(') {
  9827.                     ckstrncpy(line,s+3,256);
  9828.                     if (line[0]) {
  9829.                         lp = ckstrchr(line,')');
  9830.                         if (lp) *lp = NUL;
  9831.                         x = lookup(vartab,line,nvars,&t);
  9832.                         z = x > -1;
  9833.             if (z) {    /* 8.0.203 */
  9834.                 int t;    /* It must have a value to succeed */
  9835.                 t = 255;    /* as in C-Kermit 6.0 and 7.0 */
  9836.                 lp = line;    /* (this was broken in 8.0.200-201) */
  9837.                 zzstring(s,&lp,&t);
  9838.                 t = strlen(line);
  9839.                 z = line[0] ? 1 : 0;
  9840.             }
  9841.                     }
  9842.                 }
  9843.                 debug(F111,"if defined variable",line,z);
  9844.             } else {
  9845.                 z = chkvar(s);          /* Starts with backslash */
  9846.                 if (z > 0) {            /* Yes... */
  9847.                     t = 255;            /* than buffer so if zzstring fails  */
  9848.                     lp = line;          /* check for that -- overflow means */
  9849.                     line[0] = NUL;      /* the quantity is defined. */
  9850.                     x = zzstring(s,&lp,&t);
  9851.                     if ((x < 0 && t != 255) || !line[0])
  9852.                       z = 0;
  9853.                     debug(F111,"if defined zzstring",line,z);
  9854.                     debug(F101,"if defined zzstring t","",t);
  9855.                 }
  9856.             }
  9857.         } else {
  9858.             z = (mxlook(mactab,s,nmac) > -1); /* Look for exact match */
  9859.         }
  9860.         debug(F111,"if defined final",s,z);
  9861.         ifargs += 2;
  9862.         break;
  9863.  
  9864.       case XXIFDC: {                    /* IF DECLARED */
  9865.           char * lp;
  9866.           char line[32];
  9867.           int j, k, t, x;
  9868.           if ((x = cmfld("Array name","",&s,NULL)) < 0)
  9869.             return((x == -3) ? -2 : x);
  9870.           if (*s == CMDQ) {
  9871.               if (*(s+1) != '&') {
  9872.                   t = 31;
  9873.                   lp = line;
  9874.                   line[0] = NUL;
  9875.                   x = zzstring(s,&lp,&t);
  9876.                   s = line;
  9877.               }
  9878.           }
  9879.           z = 0;
  9880.           if ((x = arraybounds(s,&j,&k)) > -1) {
  9881.               if (a_ptr[x]) {
  9882.                   if (j < 1)
  9883.                     z = 1;
  9884.                   else if (j <= a_dim[x])
  9885.                     z = 1;
  9886.                   if (z == 1 && k > a_dim[x])
  9887.                     z = 0;
  9888.               }
  9889.           }
  9890.           break;
  9891.       }
  9892.       case XXIFBG:                      /* IF BACKGROUND */
  9893.       case XXIFFG:                      /* IF FOREGROUND */
  9894.         bgchk();                        /* Check background status */
  9895.         if (ifc == XXIFFG)              /* Foreground */
  9896.           z = pflag ? 1 : 0;
  9897.         else z = pflag ? 0 : 1;         /* Background */
  9898.         ifargs += 1;
  9899.         break;
  9900.  
  9901.       case XXIFCO:                      /* IF COUNT */
  9902.         z = ( --count[cmdlvl] > 0 );
  9903.         if (cx == XXWHI) count[cmdlvl] += 2; /* Don't ask... */
  9904.         debug(F101,"if count","",z);
  9905.         ifargs += 1;
  9906.         break;
  9907.  
  9908.       case XXIFEX:                      /* IF EXIST */
  9909. #ifdef CK_TMPDIR
  9910.       case XXIFDI:                      /* IF DIRECTORY */
  9911. #endif /* CK_TMPDIR */
  9912.       case XXIFAB:                      /* IF ABSOLUTE */
  9913.         if ((x = cmfld(
  9914.                        ((ifc == XXIFDI) ? "Directory name" : "File"),
  9915.                        "",&s,
  9916. #ifdef OS2
  9917.                        NULL             /* This allows \'s in filenames */
  9918. #else
  9919.                        xxstring
  9920. #endif /* OS2 */
  9921.                        )) < 0) {
  9922.             if (x == -3) {
  9923.                 extern int cmflgs;
  9924.                 if (cmflgs == 1) {
  9925.                     printf("?File or directory name required\n");
  9926.                     return(-9);
  9927.                 }
  9928.             } else return(x);
  9929.         }
  9930.         s = brstrip(s);
  9931.         if (ifc == XXIFAB) {
  9932.             z = isabsolute(s);
  9933.         } else if (ifc == XXIFEX) {
  9934.             z = (zgetfs(s) > -1L);
  9935.             debug(F101,"if exist 1","",z);
  9936. #ifdef OS2
  9937.             if (!z) {                   /* File not found. */
  9938.                 int t;                  /* Try expanding variables */
  9939.                 t = LINBUFSIZ-1;        /* and looking again. */
  9940.                 lp = line;
  9941.                 zzstring(s,&lp,&t);
  9942.                 s = line;
  9943.                 z = ( zchki(s) > -1L );
  9944.                 debug(F101,"if exist 2","",z);
  9945.             }
  9946. #endif /* OS2 */
  9947. #ifdef CK_TMPDIR
  9948.         } else {
  9949. #ifdef VMS
  9950.             z = (zchki(s) == -2)
  9951. #else
  9952. /* Because this doesn't catch $DISK1:[FOO]BLAH.DIR;1 */
  9953.             z = isdir(s)
  9954. #ifdef OS2
  9955.               || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  9956. #endif /* OS2 */
  9957. #endif /* VMS */
  9958.               ;
  9959.             debug(F101,"if directory 1","",z);
  9960.  
  9961.             if (!z) {                   /* File not found. */
  9962.                 int t;                  /* Try expanding variables */
  9963.                 t = LINBUFSIZ-1;        /* and looking again. */
  9964.                 lp = line;
  9965.                 zzstring(s,&lp,&t);
  9966.                 s = line;
  9967.                 z = isdir(s)
  9968. #ifdef OS2
  9969.                   || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  9970. #endif /* OS2 */
  9971.                     ;
  9972.                 debug(F101,"if directory 2","",z);
  9973.             }
  9974. #endif /* CK_TMPDIR */
  9975.         }
  9976.         ifargs += 2;
  9977.         break;
  9978.  
  9979.       case XXIFEQ:                      /* IF EQUAL (string comparison) */
  9980.       case XXIFLL:                      /* IF Lexically Less Than */
  9981.       case XXIFLG:                      /* If Lexically Greater Than */
  9982.         if ((x = cmfld("first word or variable name","",&s,xxstring)) < 0) {
  9983.             if (x == -3) {
  9984.                 printf("?Text required\n");
  9985.                 return(-9);
  9986.             } else return(x);
  9987.         }
  9988.         s = brstrip(s);                 /* Strip braces */
  9989.         x = (int)strlen(s);
  9990.         if (x > LINBUFSIZ-1) {
  9991.             printf("?IF: strings too long\n");
  9992.             return(-2);
  9993.         }
  9994.         lp = line;                      /* lp points to first string */
  9995.         ckstrncpy(line,s,LINBUFSIZ);
  9996.         if ((y = cmfld("second word or variable name","",&s,xxstring)) < 0) {
  9997.             if (y == -3) {
  9998.                 printf("?Text required\n");
  9999.                 return(-9);
  10000.             } else return(y);
  10001.         }
  10002.         s = brstrip(s);
  10003.         y = (int)strlen(s);
  10004.         if (x + y + 2 > LINBUFSIZ) {
  10005.             printf("?IF: strings too long\n");
  10006.             return(-2);
  10007.         }
  10008.         tp = lp + x + 2;                /* tp points to second string */
  10009.         strcpy(tp,s);                   /* safe (checked) */
  10010.         x = ckstrcmp(lp,tp,-1,inpcas[cmdlvl]); /* Use longest length */
  10011.         switch (ifc) {
  10012.           case XXIFEQ:                  /* IF EQUAL (string comparison) */
  10013.             z = (x == 0);
  10014.             break;
  10015.           case XXIFLL:                  /* IF Lexically Less Than */
  10016.             z = (x < 0);
  10017.             break;
  10018.           case XXIFLG:                  /* If Lexically Greater Than */
  10019.             z = (x > 0);
  10020.             break;
  10021.         }
  10022.         debug(F101,"IF EQ result","",z);
  10023.         ifargs += 3;
  10024.         break;
  10025.  
  10026.       case XXIFVE:                      /* IF VERSION */
  10027.       case XXIFAE:                      /* IF (arithmetically) = */
  10028.       case XXIFNQ:                      /* IF != (not arithmetically equal) */
  10029.       case XXIFLT:                      /* IF <  */
  10030.       case XXIFLE:                      /* IF <= */
  10031.       case XXIFGE:                      /* IF >= */
  10032.       case XXIFGT: {                    /* IF >  */
  10033.  
  10034.         /* Really should use longs here... */
  10035.         /* But cmnum parses ints. */
  10036.         int xx, n1 = 0, n2 = 0;
  10037.         if (ifc == XXIFVE) {
  10038.             n1 = (int) vernum;
  10039.         } else {
  10040.             x = cmfld("first number or variable name","",&s,xxstring);
  10041.             if (x == -3) {
  10042.                 printf("?Quantity required\n");
  10043.                 return(-9);
  10044.             }
  10045.             if (x < 0) return(x);
  10046.             debug(F101,"xxifgt cmfld","",x);
  10047.             ckstrncpy(line,s,LINBUFSIZ);
  10048.             lp = brstrip(line);
  10049.             debug(F110,"xxifgt exp1",lp,0);
  10050.  
  10051. /* The following bit is for compatibility with old versions of MS-DOS Kermit */
  10052.  
  10053.             if (!ckstrcmp(lp,"count",5,0)) {
  10054.                 n1 = count[cmdlvl];
  10055.             } else if (!ckstrcmp(lp,"version",7,0)) {
  10056.                 n1 = (int) vernum;
  10057.             } else if (!ckstrcmp(lp,"argc",4,0)) {
  10058.                 n1 = (int) macargc[maclvl];
  10059.             } else {
  10060.  
  10061. /* End of compatibility bit */
  10062.  
  10063. #ifdef FNFLOAT
  10064.                 if (isfloat(lp,0) > 1) { /* Allow floating-point comparisons */
  10065.                     f1 = floatval;
  10066.                     f1flag = 1;
  10067.                 } else
  10068. #endif /* FNFLOAT */
  10069.                   if (chknum(lp)) {
  10070.                       n1 = atoi(lp);
  10071.                   } else {              /* Check for arithmetic expression */
  10072.                       q = evala(lp);    /* cmnum() does this but ... */
  10073.                       if (chknum(q))    /* we're not using cmnum(). */
  10074.                         n1 = atoi(q);
  10075.                       else
  10076.                         return(-2);
  10077.                   }
  10078.             }
  10079.         }
  10080.         y = cmfld("number or variable name","",&s,xxstring);
  10081.         if (y == -3) {
  10082.             printf("?Quantity required\n");
  10083.             return(-9);
  10084.         }
  10085.         if (y < 0) return(y);
  10086.         s = brstrip(s);
  10087.         if (!*s) return(-2);
  10088.         if (ifc == XXIFVE) {
  10089.             tp = line;
  10090.         } else {
  10091.             x = (int)strlen(lp);
  10092.             tp = line + x + 2;
  10093.         }
  10094.         ckstrncpy(tp,s,LINBUFSIZ-x-2);
  10095.         debug(F110,"xxifgt exp2",tp,0);
  10096.         if (!ckstrcmp(tp,"count",5,0)) {
  10097.             n2 = count[cmdlvl];
  10098.         } else if (!ckstrcmp(tp,"version",7,0)) {
  10099.             n2 = (int) vernum;
  10100.         } else if (!ckstrcmp(tp,"argc",4,0)) {
  10101.             n2 = (int) macargc[maclvl];
  10102.         } else {
  10103. #ifdef FNFLOAT
  10104.             if (isfloat(tp,0) > 1) {
  10105.                 f2 = floatval;
  10106.                 f2flag = 1;
  10107.             } else
  10108. #endif /* FNFLOAT */
  10109.             if (chknum(tp)) {
  10110.                 n2 = atoi(tp);
  10111.             } else {
  10112.                 q = evala(tp);
  10113.                 if (chknum(q))
  10114.                   n2 = atoi(q);
  10115.                 else
  10116.                   return(-2);
  10117.             }
  10118.         }
  10119.         xx = (ifc == XXIFVE) ? XXIFGE : ifc;
  10120.  
  10121. #ifdef FNFLOAT
  10122.         if (f1flag && !f2flag) {
  10123.             f2 = (CKFLOAT)n2;
  10124.             f2flag = 1;
  10125.         }
  10126.         if (f2flag && !f1flag)
  10127.           f1 = (CKFLOAT)n1;
  10128.         if (f1flag)
  10129.           z = ((f1 <  f2 && xx == XXIFLT)
  10130.                || (f1 != f2 && xx == XXIFNQ)
  10131.                || (f1 <= f2 && xx == XXIFLE)
  10132.                || (f1 == f2 && xx == XXIFAE)
  10133.                || (f1 >= f2 && xx == XXIFGE)
  10134.                || (f1 >  f2 && xx == XXIFGT));
  10135.         else
  10136. #endif /* FNFLOAT */
  10137.           z = ((n1 <  n2 && xx == XXIFLT)
  10138.                || (n1 != n2 && xx == XXIFNQ)
  10139.                || (n1 <= n2 && xx == XXIFLE)
  10140.                || (n1 == n2 && xx == XXIFAE)
  10141.                || (n1 >= n2 && xx == XXIFGE)
  10142.                || (n1 >  n2 && xx == XXIFGT));
  10143.         debug(F101,"xxifge z","",z);
  10144.         if (ifc == XXIFVE)
  10145.           ifargs += 2;
  10146.         else
  10147.           ifargs += 3;
  10148.         break;
  10149.       }
  10150.  
  10151.       case XXIFNU:                      /* IF NUMERIC */
  10152.         x = cmfld("variable name or constant","",&s,NULL);
  10153.         if (x == -3) {
  10154.             extern int cmflgs;
  10155.             if (cmflgs == 1) {
  10156.                 printf("?Quantity required\n");
  10157.                 return(-9);
  10158.             }
  10159.         } else if (x < 0)
  10160.           return(x);
  10161.         x = LINBUFSIZ-1;
  10162.         lp = line;
  10163.         zzstring(s,&lp,&x);
  10164.         lp = line;
  10165.         debug(F110,"xxifnu quantity",lp,0);
  10166.         z = chknum(lp);
  10167. #ifdef COMMENT
  10168. /*
  10169.   This works, but it's not wise -- IF NUMERIC is mostly used to see if a
  10170.   string really does contain only numeric characters.  If they want to force
  10171.   evaluation, they can use \feval() on the argument string.
  10172. */
  10173.         if (!z) {                       /* Not a number */
  10174.             x_ifnum = 1;                /* Avoid "eval" error messages */
  10175.             q = evala(lp);              /* Maybe it's an expression */
  10176.             z = chknum(q);              /* that evaluates to a number */
  10177.             x_ifnum = 0;                /* Put eval messages back to normal */
  10178.             if (z) debug(F110,"xxifnu exp",lp,0);
  10179.         }
  10180. #endif /* COMMENT */
  10181.         debug(F101,"xxifnu chknum","",z);
  10182.         ifargs += 2;
  10183.         break;
  10184.  
  10185. #ifdef ZFCDAT
  10186.       case XXIFNE: {                    /* IF NEWER */
  10187.         char d1[20], * d2;              /* Buffers for 2 dates */
  10188.         if ((z = cmifi("First file","",&s,&y,xxstring)) < 0)
  10189.           return(z);
  10190.         ckstrncpy(d1,zfcdat(s),20);
  10191.         if ((z = cmifi("Second file","",&s,&y,xxstring)) < 0)
  10192.           return(z);
  10193.         d2 = zfcdat(s);
  10194.         if ((int)strlen(d1) != 17 || (int)strlen(d2) != 17) {
  10195.             printf("?Failure to get file date\n");
  10196.             return(-9);
  10197.         }
  10198.         debug(F110,"xxifnewer d1",d1,0);
  10199.         debug(F110,"xxifnewer d2",d2,0);
  10200.         z = (strcmp(d1,d2) > 0) ? 1 : 0;
  10201.         debug(F101,"xxifnewer","",z);
  10202.         ifargs += 2;
  10203.         break;
  10204.       }
  10205. #endif /* ZFCDAT */
  10206.  
  10207. #ifdef CK_IFRO
  10208.       case XXIFRO:                      /* REMOTE-ONLY advisory */
  10209.         ifargs++;
  10210. #ifdef NOLOCAL
  10211.         z = 1;
  10212. #else
  10213.         z = remonly;
  10214. #endif /* NOLOCAL */
  10215.         break;
  10216. #endif /* CK_IFRO */
  10217.  
  10218.       case XXIFAL:                      /* ALARM */
  10219.         ifargs++;
  10220.         debug(F101,"IF ALARM ck_alarm","",ck_alarm);
  10221.         debug(F110,"IF ALARM alrm_date",alrm_date,0);
  10222.         debug(F110,"IF ALARM alrm_time",alrm_time,0);
  10223.  
  10224.         if (ck_alarm < 1L || alrm_date[0] < '0' || alrm_time[0] < '0') {
  10225.             z = 0;                      /* ALARM not SET */
  10226.             break;                      /* so IF ALARM fails */
  10227.         }
  10228.         /* Get current date and time */
  10229.         ckstrncpy(tmpbuf,ckcvtdate("",1),TMPBUFSIZ);
  10230.         s = tmpbuf;
  10231.         s[8] = NUL;
  10232.         z = (int) strncmp(tmpbuf,alrm_date,8); /* Compare dates */
  10233.         debug(F101,"IF ALARM date z","",z);
  10234.         if (z == 0) {                   /* Dates are the same */
  10235.             /* Compare times */
  10236.             z = (tod2sec(tmpbuf+9) >= atol(alrm_time)) ? 1 : -1;
  10237.             debug(F101,"IF ALARM time z","",z);
  10238.         }
  10239.         tmpbuf[0] = NUL;                /* z >= 0 if alarm is passed */
  10240.         z = ((z >= 0) ? 1 : 0);         /* z <  0 otherwise */
  10241.         debug(F101,"IF ALARM final z","",z);
  10242.         break;
  10243.  
  10244.       case XXIFOP:                      /* IF OPEN */
  10245.         if ((x = cmkey(iotab,niot,"file or log","",xxstring)) < 0)
  10246.           return(x);
  10247.         if (x == 9999 || x == ZSTDIO) {
  10248.             bgchk();                    /* Check background status */
  10249.             z = pflag ? 1 : 0;
  10250.         } else if (x == 8888) {
  10251.             z = local ? ttchk() > -1 : 0;
  10252. #ifdef CKLOGDIAL
  10253.         } else if (x == 7777) {
  10254.             extern int dialog;
  10255.             z = dialog ? 1 : 0;
  10256. #endif /* CKLOGDIAL */
  10257.         } else
  10258.           z = (chkfn(x) > 0) ? 1 : 0;
  10259.         ifargs += 1;
  10260.         break;
  10261.  
  10262. #ifdef OS2
  10263.       case XXIFSD:                      /* Started-From-Dialer */
  10264.         ifargs++;
  10265.         z = StartedFromDialer;
  10266.         break;
  10267.  
  10268.       case XXIFTM:                      /* Terminal-Macro */
  10269.         ifargs++;
  10270.         z = cmdstk[cmdlvl].ccflgs & CF_KMAC;
  10271.         break;
  10272. #endif /* OS2 */
  10273.  
  10274.       case XXIFEM:                      /* Emulation is active */
  10275. #ifdef OS2
  10276.         z = 1;
  10277. #else
  10278.         z = 0;
  10279. #endif /* OS2 */
  10280.         break;
  10281.  
  10282.       case XXIFIK:                      /* Running as IKSD? */
  10283.         z = inserver;
  10284.         break;
  10285.  
  10286.       case XXIFTA:                      /* Connection is TAPI */
  10287.         z = 0;
  10288. #ifndef NODIAL
  10289. #ifdef CK_TAPI
  10290.         if (local && !network && tttapi)
  10291.           z = 1;
  10292. #endif /* CK_TAPI */
  10293. #endif /* NODIAL */
  10294.         break;
  10295.  
  10296.       case XXIFMA:                      /* IF MATCH */
  10297.         x = cmfld("String or variable","",&s,xxstring);
  10298.         if (x == -3) {
  10299.             extern int cmflgs;
  10300.             if (cmflgs == 1) {
  10301.                 printf("?String required\n");
  10302.                 return(-9);
  10303.             }
  10304.         } else if (x < 0)
  10305.           return(x);
  10306.         ckstrncpy(line,s,LINBUFSIZ);
  10307.         s = brstrip(line);
  10308.         debug(F110,"xxifma string",line,0);
  10309.         x = cmfld("Pattern","",&p,xxstring);
  10310.         if (x == -3) {
  10311.             extern int cmflgs;
  10312.             if (cmflgs == 1) {
  10313.                 printf("?Pattern required\n");
  10314.                 return(-9);
  10315.             }
  10316.         } else if (x < 0)
  10317.           return(x);
  10318.         ckstrncpy(tmpbuf,p,TMPBUFSIZ);
  10319.         p = brstrip(tmpbuf);
  10320.         debug(F110,"xxifma pattern",tmpbuf,0);
  10321.         z = ckmatch(p,s,inpcas[cmdlvl],1);
  10322.         break;
  10323.  
  10324.       case XXIFFL: {                    /* IF FLAG */
  10325.           extern int ooflag;
  10326.           z = ooflag;
  10327.           break;
  10328.       }
  10329.       case XXIFAV: {                    /* IF AVAILABLE */
  10330.           if ((x = cmkey(availtab,availtabn,"","",xxstring)) < 0)
  10331.             return(x);
  10332.           switch (x) {
  10333.             case AV_KRB4:
  10334.               z = ck_krb4_is_installed();
  10335.               break;
  10336.             case AV_KRB5:
  10337.               z = ck_krb5_is_installed();
  10338.               break;
  10339.             case AV_SRP:
  10340.               z = ck_srp_is_installed();
  10341.               break;
  10342.             case AV_SSL:
  10343.               z = ck_ssleay_is_installed();
  10344.               break;
  10345.             case AV_NTLM:
  10346.               z = ck_ntlm_is_installed();
  10347.               break;
  10348.             case AV_CRYPTO:
  10349.               z = ck_crypt_is_installed();
  10350.               break;
  10351.             case AV_SSH:
  10352.               z = ck_ssh_is_installed();
  10353.               break;
  10354.             default:
  10355.               z = 0;
  10356.           }
  10357.           break;
  10358.       }
  10359.       case XXIFAT:                      /* IF ASKTIMEOUT */
  10360.         z = asktimedout;
  10361.         break;
  10362.  
  10363.       case XXIFRD:                      /* IF READABLE */
  10364.       case XXIFWR:                      /* IF WRITEABLE */
  10365.         if ((x = cmfld("File or directory name",
  10366.                        "",
  10367.                        &s,
  10368. #ifdef OS2
  10369.                        NULL             /* This allows \'s in filenames */
  10370. #else
  10371.                        xxstring
  10372. #endif /* OS2 */
  10373.                        )) < 0) {
  10374.             if (x == -3) {
  10375.                 extern int cmflgs;
  10376.                 if (cmflgs == 1) {
  10377.                     printf("?File or directory name required\n");
  10378.                     return(-9);
  10379.                 }
  10380.             } else return(x);
  10381.         }
  10382.         s = brstrip(s);
  10383. /*
  10384.   zchk[io]() do not do what we want here for directories, so we set
  10385.   a global flag telling it to behave specially in this case.  Othewise
  10386.   we'd have to change the API and change all ck?fio.c modules accordingly.
  10387. */
  10388.         y = 0;                          /* Try-again control */
  10389. #ifdef OS2
  10390.   ifrdagain:
  10391. #endif /* OS2 */
  10392.         if (ifc == XXIFRD) {            /* IF READABLE */
  10393.             zchkid = 1;
  10394.             z = zchki(s) > -1;
  10395.             zchkid = 0;
  10396.         } else if (ifc == XXIFWR) {     /* IF WRITEABLE */
  10397.             zchkod = 1;
  10398.             z = zchko(s) > -1;
  10399.             zchkod = 0;
  10400.         }
  10401. #ifdef OS2
  10402.         if (!z && !y) {                 /* File not found. */
  10403.             int t;                      /* Try expanding variables */
  10404.             t = LINBUFSIZ-1;            /* and looking again. */
  10405.             lp = line;
  10406.             zzstring(s,&lp,&t);
  10407.             s = line;
  10408.             z = zchko(s) > -1;
  10409.             y++;
  10410.             goto ifrdagain;
  10411.         }
  10412. #endif /* OS2 */
  10413.         ifargs += 2;
  10414.         break;
  10415.       case XXIFQU:                      /* IF QUIET */
  10416.         z = quiet ? 1 : 0;
  10417.         debug(F101,"if quiet","",z);
  10418.         ifargs += 1;
  10419.         break;
  10420.  
  10421.       case XXIFWI:                      /* WILD */
  10422.         if ((x = cmfld("File specification","",&s,xxstring)) < 0) return(x);
  10423.         z = iswild(s);
  10424.         break;
  10425.  
  10426.       case XXIFCK:                      /* C-KERMIT */
  10427. #ifdef OS2
  10428.         z = 0;
  10429. #else
  10430.         z = 1;
  10431. #endif /* OS2 */
  10432.         break;
  10433.  
  10434.       case XXIFK9:                      /* K-95 */
  10435. #ifdef OS2
  10436.         z = 1;
  10437. #else
  10438.         z = 0;
  10439. #endif /* OS2 */
  10440.         break;
  10441.  
  10442.       case XXIFGU:                      /* GUI */
  10443. #ifdef KUI
  10444.         z = 1;
  10445. #else
  10446.         z = 0;
  10447. #endif /* KUI */
  10448.         break;
  10449.  
  10450.       case XXIFMS:                      /* MS-KERMIT */
  10451.         z = 0;
  10452.         break;
  10453.  
  10454.       case XXIFLO:                      /* IF LOCAL */
  10455.         z = local ? 1 : 0;
  10456.         break;
  10457.  
  10458.       case XXIFCM: {                    /* IF COMMAND */
  10459.           extern struct keytab cmdtab[];
  10460.           extern int ncmd;
  10461.           if ((x = cmfld("Word","",&s,xxstring)) < 0)
  10462.             return(x);
  10463.           z = lookup(cmdtab,s,ncmd,&y);
  10464.           z = (z == -2 || z > -1) ? 1 : 0;
  10465.           break;
  10466.       }
  10467. #ifdef CKFLOAT
  10468.       case XXIFFP:                      /* IF FLOAT */
  10469.         if ((x = cmfld("Number","",&s,xxstring)) < 0)
  10470.           if (x != -3)                  /* e.g. empty variable */
  10471.             return(x);
  10472.         z = isfloat(s,0);
  10473.         break;
  10474. #endif /* CKFLOAT */
  10475.  
  10476.       case XXIFKB:                      /* KBHIT */
  10477.         z = conchk();
  10478.         if (z < 0) z = 0;
  10479.         if (z > 1) z = 1;
  10480.         break;
  10481.  
  10482.       case XXIFKG: {                    /* KERBANG */
  10483.           extern int cfilef;
  10484.           z = (xcmdsrc == 0) ? 0 : cfilef;
  10485.           break;
  10486.       }
  10487.  
  10488.       default:                          /* Shouldn't happen */
  10489.         return(-2);
  10490.     } /* end of switch */
  10491.  
  10492.     if (z)
  10493.       *bx++ = '1';
  10494.     else
  10495.       *bx++ = '0';
  10496.     *bx = NUL;
  10497.     if (bx > boolval + BOOLLEN - 2) {
  10498.         printf("?Boolean expression too long");
  10499.         return(-9);
  10500.     }
  10501.     ecount++;                           /* Expression counter */
  10502.     debug(F101,"boolexp parens","",parens);
  10503.     debug(F101,"boolexp pcount","",pcount);
  10504.     if (parens && pcount > 0)
  10505.       goto ifagain;
  10506.  
  10507.   ifend:                                /* No more - done */
  10508.     *bx = NUL;
  10509.     z = atoi(evalx(boolval));
  10510.     debug(F111,"boolexp boolval",boolval,z);
  10511.     return(z);
  10512. }
  10513.  
  10514. /*  D O I F  --  Do the IF command  */
  10515.  
  10516. int
  10517. doif(cx) int cx; {
  10518.     int x, y, z; char *s, *p;
  10519.     char *q;
  10520. #ifdef OS2
  10521.     extern int keymac;
  10522. #endif /* OS2 */
  10523.  
  10524.     debug(F101,"doif cx","",cx);
  10525.  
  10526.     z = boolexp(cx);                    /* Evaluate the condition(s) */
  10527.     debug(F010,"doif cmdbuf",cmdbuf,0);
  10528.     debug(F101,"doif boolexp","",z);
  10529.     if (z < 0)
  10530.       return(z);
  10531.  
  10532.     if (cx == XXIF) {                   /* Allow IF to have XIF semantics. */
  10533.         char * p;
  10534.         p = cmpeek();
  10535.         if (!p) p = "";
  10536.         while (*p) {
  10537.             if (*p == SP || *p == HT)
  10538.               p++;
  10539.             else
  10540.               break;
  10541.         }
  10542.         if (*p == '{')
  10543.           cx = XXIFX;
  10544.     }
  10545.     switch (cx) {                       /* Separate handling for IF and XIF */
  10546.  
  10547.       case XXASSER:                     /* And ASSERT */
  10548.         if ((x = cmcfm()) < 0)
  10549.           return(x);
  10550.         return(success = z);
  10551.  
  10552.       case XXIF:                        /* This is IF... */
  10553.         ifcmd[cmdlvl] = 1;              /* We just completed an IF command */
  10554.         debug(F101,"doif condition","",z);
  10555.         if (z) {                        /* Condition is true */
  10556.             iftest[cmdlvl] = 1;         /* Remember that IF succeeded */
  10557.             if (maclvl > -1) {          /* In macro, */
  10558.                 pushcmd(NULL);          /* save rest of command. */
  10559.             } else if (tlevel > -1) {   /* In take file, */
  10560.                 debug(F100, "doif: pushing command", "", 0);
  10561.                 pushcmd(NULL);          /* save rest of command. */
  10562.             } else {                    /* If interactive, */
  10563.                 cmini(ckxech);          /* just start a new command */
  10564.                 printf("\n");           /* (like in MS-DOS Kermit) */
  10565.                 if (pflag) prompt(xxstring);
  10566.             }
  10567.         } else {                        /* Condition is false */
  10568.             iftest[cmdlvl] = 0;         /* Remember command failed. */
  10569.             if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  10570.               return(y);                /* Gobble up rest of line */
  10571.         }
  10572.         return(0);
  10573.  
  10574.       case XXIFX: {                     /* This is XIF (Extended IF) */
  10575.           char *p;
  10576.           char e[5];
  10577.           int i;
  10578.           if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  10579.             return(y);                  /* Get object command. */
  10580.           p = s;
  10581.           lp = line;
  10582.           debug(F110,"doif THEN part",s,-54);
  10583.           if (litcmd(&p,&lp,LINBUFSIZ - 1) < 0) { /* Quote THEN-part */
  10584.               return(-2);
  10585.           }
  10586.           debug(F111,"doif THEN part 2",line,z);
  10587.  
  10588.           while (*p == SP) p++;         /* Strip trailing spaces */
  10589.           ifcmd[cmdlvl] = 0;            /* Assume ELSE part in same line */
  10590.           iftest[cmdlvl] = z ? 1 : 0;
  10591.           if (*p) {                     /* At end? */
  10592.               if (!z) {                 /* No, use ELSE-part, if any */
  10593.                   for (i = 0; i < 4; i++) e[i] = *p++;
  10594.                   if (ckstrcmp(e,"else",4,0)) /* See if we have an ELSE */
  10595.                     return(-2);         /* Something else - error. */
  10596.                   debug(F010,"doif ELSE line 1",p,0);
  10597.                   while (*p == SP) p++; /* Skip spaces */
  10598.                   if (*p != '{') {      /* Brace ELSE part if necessary */
  10599.                       ckmakmsg(tmpbuf,TMPBUFSIZ,"{",p," }",NULL);
  10600.                       p = tmpbuf;
  10601.                       debug(F010,"doif ELSE line 2",p,0);
  10602.                   }
  10603.                   lp = line;            /* Write over THEN part... */
  10604.                   *lp = NUL;            /* with ELSE part. */
  10605.                   if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) {
  10606.                       return(-2);
  10607.                   }
  10608.                   while (*p == SP) p++; /* Strip trailing spaces */
  10609.                   if (*p) return(-2);   /* Should be nothing here. */
  10610.                   debug(F010,"doif ELSE line 3",line,0);
  10611.               }
  10612.           } else {                      /* At end, treat like an IF command */
  10613.               if (!z) line[0] = NUL;    /* Condition not true and no ELSE */
  10614.               ifcmd[cmdlvl] = 1;        /* Allow ELSE on next line */
  10615.               debug(F101,"IF condition","",z);
  10616.           }
  10617.           if (line[0]) {
  10618.               x = mlook(mactab,"_xif",nmac); /* Get index of "_xif" macro. */
  10619.               if (x < 0) {                      /* Not there? */
  10620.                   addmmac("_xif",xif_def);      /* Put it back. */
  10621.                   if (mlook(mactab,"_xif",nmac) < 0) { /* Look it up again. */
  10622.                       printf("?XIF macro gone!\n");
  10623.                       return(success = 0);
  10624.                   }
  10625.               }
  10626.               dodo(x,line,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  10627.           }
  10628.           return(0);
  10629.       }
  10630.       case XXWHI: {                     /* WHILE Command */
  10631.           p = cmdbuf;                   /* Capture IF condition */
  10632.           ifcond[0] = NUL;              /* from command buffer */
  10633.           while (*p == SP) p++;
  10634.           while (*p != SP) p++;
  10635.           ifcp = ifcond;
  10636.           ifcp += ckstrncpy(ifcp,"{ \\flit(if ( not ",IFCONDLEN);
  10637. #ifdef COMMENT
  10638. /*
  10639.   This doesn't work because it breaks on the first left brace, which does
  10640.   not necessarily start the command list, e.g. "while equal \%a {\35}".
  10641. */
  10642.           while (*p != '{' && *p != NUL) *ifcp++ = *p++;
  10643.           p = " ) goto _..bot) } ";
  10644.           while (*ifcp++ = *p++) ;
  10645. #else
  10646. /*
  10647.   The command parser sets cmbptr to the spot where it left off parsing in
  10648.   the command buffer.
  10649. */
  10650.           {
  10651.               extern char * cmbptr;
  10652.               if (cmbptr) {
  10653.                   while (p < cmbptr && *p != NUL)
  10654.                     *ifcp++ = *p++;
  10655.                   p = " ) goto _..bot) } ";
  10656.                   while ((*ifcp++ = *p++)) ;
  10657.               } else {
  10658.                   printf("?Internal error parsing WHILE condition\n");
  10659.                   return(-9);
  10660.               }
  10661.           }
  10662. #endif /* COMMENT */
  10663.  
  10664.           debug(F110,"WHILE cmd",ifcond,0);
  10665.  
  10666.           if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  10667.             return(y);                  /* Get object command. */
  10668.           p = s;
  10669.           lp = line;
  10670.           if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) { /* Quote object command */
  10671.               return(-2);
  10672.           }
  10673.           debug(F101,"WHILE body",line,-54);
  10674.           if (line[0]) {
  10675.               char *p;
  10676.               x = mlook(mactab,"_while",nmac); /* index of "_while" macro. */
  10677.               if (x < 0) {              /* Not there? */
  10678.                   addmmac("_while",whil_def); /* Put it back. */
  10679.                   if (mlook(mactab,"_while",nmac) < 0) { /* Look it up again */
  10680.                       printf("?WHILE macro definition gone!\n");
  10681.                       return(success = 0);
  10682.                   }
  10683.               }
  10684.               p = malloc((int)strlen(ifcond) + (int)strlen(line) + 2);
  10685.               if (p) {
  10686.                   strcpy(p,ifcond);     /* safe (prechecked) */
  10687.                   strcat(p,line);       /* safe (prechecked) */
  10688.                   debug(F010,"WHILE dodo",p,0);
  10689.                   dodo(x,p,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  10690.                   free(p);
  10691.                   p = NULL;
  10692.               } else {
  10693.                   printf("?Can't allocate storage for WHILE command");
  10694.                   return(success = 0);
  10695.               }
  10696.           }
  10697.           return(0);
  10698.       }
  10699.       default:
  10700.         return(-2);
  10701.     }
  10702. }
  10703. #endif /* NOSPL */
  10704.  
  10705. /* Set up a TAKE command file */
  10706.  
  10707. int
  10708. dotake(s) char *s; {
  10709. #ifndef NOSPL
  10710.     extern int tra_cmd;
  10711. #endif /* NOSPL */
  10712. #ifndef NOLOCAL
  10713. #ifdef OS2
  10714.     extern int term_io;
  10715.     int term_io_sav = term_io;
  10716. #endif /* OS2 */
  10717. #endif /* NOLOCAL */
  10718.     int slen;
  10719.  
  10720.     debug(F110,"dotake",s,0);
  10721.     if (!s) s = "";
  10722.     if (!*s) return(success = 0);
  10723.     slen = strlen(s);
  10724.     debug(F101,"dotake len","",slen);
  10725.  
  10726.     if ((tfile[++tlevel] = fopen(s,"r")) == NULL) {
  10727.         perror(s);
  10728.         debug(F110,"dotake fail",s,0);
  10729.         tlevel--;
  10730.         return(success = 0);
  10731.     } else {
  10732.         tfline[tlevel] = 0;             /* Line counter */
  10733. #ifdef VMS
  10734.         conres();                       /* So Ctrl-C will work */
  10735. #endif /* VMS */
  10736. #ifndef NOLOCAL
  10737. #ifdef OS2
  10738.         term_io = 0;                    /* Disable Terminal Emulator I/O */
  10739. #endif /* OS2 */
  10740. #endif /* NOLOCAL */
  10741. #ifndef NOSPL
  10742.         cmdlvl++;                       /* Entering a new command level */
  10743.         debug(F111,"CMD +F",s,cmdlvl);
  10744.         debug(F101,"dotake cmdlvl","",cmdlvl);
  10745.         debug(F101,"dotake tlevel","",tlevel);
  10746.         if (cmdlvl > CMDSTKL) {
  10747.             debug(F100,"dotake stack overflow","",0);
  10748.             cmdlvl--;
  10749.             debug(F111,"CMD*-F",s,cmdlvl);
  10750.             fclose(tfile[tlevel--]);
  10751.             printf("?TAKE files and/or DO commands nested too deeply\n");
  10752.             return(success = 0);
  10753.         }
  10754.         if (tfnam[tlevel]) {            /* Copy the filename */
  10755.             free(tfnam[tlevel]);
  10756.             tfnam[tlevel] = NULL;
  10757.         }
  10758.         if ((tfnam[tlevel] = malloc(strlen(s) + 1))) {
  10759.             strcpy(tfnam[tlevel],s);    /* safe */
  10760.         } else {
  10761.             printf("?Memory allocation failure\n");
  10762.             return(success = 0);
  10763.         }
  10764.         ifcmd[cmdlvl] = 0;              /* Set variables for this cmd file */
  10765.         iftest[cmdlvl] = 0;
  10766.         count[cmdlvl]  = count[cmdlvl-1];  /* Inherit this */
  10767.         intime[cmdlvl] = intime[cmdlvl-1]; /* Inherit this */
  10768.         inpcas[cmdlvl] = inpcas[cmdlvl-1]; /* Inherit this */
  10769.         takerr[cmdlvl] = takerr[cmdlvl-1]; /* Inherit this */
  10770.         merror[cmdlvl] = merror[cmdlvl-1]; /* Inherit this */
  10771.         xquiet[cmdlvl] = quiet;
  10772.         xcmdsrc = CMD_TF;
  10773.         cmdstk[cmdlvl].src = CMD_TF;    /* Say we're in a TAKE file */
  10774.         cmdstk[cmdlvl].lvl = tlevel;    /* nested at this level */
  10775.         cmdstk[cmdlvl].ccflgs = cmdstk[cmdlvl-1].ccflgs;
  10776. #else
  10777.         takerr[tlevel] = takerr[tlevel-1]; /* Inherit this */
  10778. #endif /* NOSPL */
  10779.     }
  10780. #ifndef NOSPL
  10781.     if (tra_cmd)
  10782.       printf("[%d] +F: \"%s\"\n",cmdlvl,s);
  10783. #endif /* NOSPL */
  10784. #ifndef NOLOCAL
  10785. #ifdef OS2
  10786.     term_io = term_io_sav;
  10787. #endif /* OS2 */
  10788. #endif /* NOLOCAL */
  10789.     return(1);
  10790. }
  10791. #endif /* NOICP */
  10792.