home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv206.zip / ckuus6.c < prev    next >
C/C++ Source or Header  |  2002-10-19  |  373KB  |  10,713 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, 2002,
  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;
  991.         ckstrncpy(tmpbuf,brstrip(p),TMPBUFSIZ);
  992.         p = tmpbuf;
  993.         if (cx == XXASK || cx == XXASKQ) {
  994.             rc = gui_txt_dialog(NULL,p,(cx == XXASK),
  995.                                 line,LINBUFSIZ,NULL,mytimer);
  996.             asktimedout = (rc == -1 && mytimer);
  997.         if (rc == 1)
  998.           y = addmac(vnp,(char *)line); /* Add it to the macro table. */
  999.         timelimit = 0;
  1000.         return(success = (rc == 1 && (y >= 0)) && !asktimedout);
  1001.     } else if (cx == XXGOK) {
  1002.         rc = uq_ok(NULL, p, 3, NULL, 2);
  1003.         return(success = (rc == 1));
  1004.     }
  1005.     }
  1006. #endif /* KUI */
  1007. #endif /* OS2 */
  1008. #endif /* NOLOCAL */
  1009.  
  1010.     concb((char)escape);                /* Enter CBREAK mode */
  1011.     cmsavp(psave,PROMPTL);              /* Save old prompt */
  1012.     cmsetp(brstrip(p));                 /* Make new prompt */
  1013. reprompt:
  1014.     if (cx == XXASKQ) {                 /* For ASKQ, */
  1015.         cmini(0);                       /* no-echo mode. */
  1016.     } else {                            /* For others, regular echoing. */
  1017.         cmini(ckxech);
  1018.     }
  1019.     askflag = 1;
  1020.     x = -1;                             /* This means to reparse. */
  1021.     cmflgs = 0;
  1022.     if (pflag)
  1023.       prompt(xxstring);                 /* Issue prompt. */
  1024.  
  1025.     asktimedout = 0;                    /* Handle timed responses. */
  1026.     timelimit = mytimer;
  1027. reparse:
  1028.     cmres();
  1029.     if (cx == XXGOK) {                  /* GETOK */
  1030. #ifdef CK_RECALL
  1031.         on_recall = 0;
  1032. #endif /* CK_RECALL */
  1033.         askflag = 0;
  1034.         x = cmkey(yesno,nyesno,"","",xxstring); /* GETOK uses keyword table */
  1035.         if (x < 0) {                    /* Parse error */
  1036.             if (x == -10) {
  1037.                 x = 0;
  1038.         if (!nomsg)
  1039.           printf("?Timed out, assuming \"No\"");
  1040.         printf("\n");
  1041.                 asktimedout = 1;
  1042.                 goto gokdone;
  1043.             } else if (x == -3) {       /* No answer? */
  1044.                 printf("Please respond Yes or No\n"); /* Make them answer */
  1045.                 cmini(ckxech);
  1046.                 goto reprompt;
  1047.             } else if (x == -1) {
  1048.                 goto reparse;
  1049.             } else
  1050.               goto reprompt;
  1051.         }
  1052.         if (cmcfm() < 0)                /* Get confirmation */
  1053.           goto reparse;
  1054.   gokdone:
  1055.         askflag = 0;
  1056.         cmsetp(psave);                  /* Restore prompt */
  1057. #ifdef VMS
  1058.         if (cmdlvl > 0)                 /* In VMS and not at top level, */
  1059.           conres();                     /*  restore console again. */
  1060. #endif /* VMS */
  1061.         timelimit = 0;
  1062.         return(x);                      /* Return success or failure */
  1063.     } else if (cx == XXGETC             /* GETC */
  1064. #ifdef OS2
  1065.                || cx == XXGETK          /* or GETKEYCODE */
  1066. #endif /* OS2 */
  1067.                ) {                      /* GETC */
  1068.         char tmp[16];
  1069.         conbin((char)escape);           /* Put keyboard in raw mode */
  1070. #ifndef NOSETKEY
  1071. #ifdef OS2
  1072.         if (cx == XXGETK) {             /* GETKEYCODE */
  1073.             extern int os2gks;
  1074.             int t;
  1075.             t = os2gks;                 /* Turn off kverb recognition */
  1076.             os2gks = 0;
  1077.             x = congks(timelimit);      /* Read a key event, blocking */
  1078.             os2gks = t;                 /* Put back kverb recognition */
  1079.         } else                          /* GETC */
  1080. #endif /* OS2 */
  1081. #endif /* NOSETKEY */
  1082.         {
  1083.             debug(F101,"GETC conchk","",conchk());
  1084.             x = coninc(timelimit);      /* Just read one character */
  1085.             debug(F101,"GETC coninc","",x);
  1086.         }
  1087.         concb((char)escape);            /* Put keyboard back in cbreak mode */
  1088.         if (x > -1) {
  1089.             if (xcmdsrc == 0)
  1090.               printf("\r\n");
  1091. #ifdef OS2
  1092.             if (cx == XXGETK) {         /* GETKEYCODE */
  1093.                 sprintf(tmp,"%d",x);    /* SAFE */
  1094.             } else {
  1095. #endif /* OS2 */
  1096.                 tmp[0] = (char) (x & 0xff);
  1097.                 tmp[1] = NUL;
  1098. #ifdef OS2
  1099.             }
  1100. #endif /* OS2 */
  1101.             y = addmac(vnp,tmp);        /* Add it to the macro table. */
  1102.             debug(F111,"getc/getk addmac",vnp,y);
  1103.         } else y = -1;
  1104.         cmsetp(psave);                  /* Restore old prompt. */
  1105.         if (x < -1) {
  1106.             asktimedout = 1;
  1107.             if (!quiet && !nomsg)
  1108.               printf("?Timed out");
  1109.         printf("\n");
  1110.         }
  1111.         timelimit = 0;
  1112.         return(success = ((y < 0 ? 0 : 1) && (asktimedout == 0)));
  1113.     } else {                            /* ASK or ASKQ */
  1114. #ifdef CK_RECALL
  1115.         on_recall = 0;
  1116. #endif /* CK_RECALL */
  1117.         y = cmdgquo();                  /* Get current quoting */
  1118.         cmdsquo(0);                     /* Turn off quoting */
  1119.         while (x == -1) {               /* Prompt till they answer */
  1120.             x = cmtxt("Please respond.","",&s,NULL);
  1121.             debug(F111,"ASK cmtxt",s,x);
  1122.             cmres();
  1123.         }
  1124.         cmdsquo(y);                     /* Restore previous quoting */
  1125.         if (cx == XXASKQ)               /* ASKQ must echo CRLF here */
  1126.           printf("\r\n");
  1127.         if (x < 0) {                    /* If cmtxt parse error, */
  1128.             cmsetp(psave);              /* restore original prompt */
  1129. #ifdef VMS
  1130.             if (cmdlvl > 0)             /* In VMS and not at top level, */
  1131.               conres();                 /*  restore console again. */
  1132. #endif /* VMS */
  1133.             if (x == -10) {
  1134.         if (!nomsg)
  1135.           printf("?Timed out");
  1136.         printf("\n");
  1137.                 asktimedout = 1;
  1138.                 success = x = 0;    /* (was "x = -9;") */
  1139.             }
  1140.             timelimit = 0;
  1141.             return(x);                  /* and return cmtxt's error code. */
  1142.         }
  1143.         if (*s == NUL) {                /* If user typed a bare CR, */
  1144.             cmsetp(psave);              /* Restore old prompt, */
  1145.             delmac(vnp,0);              /* delete variable if it exists, */
  1146. #ifdef VMS
  1147.             if (cmdlvl > 0)             /* In VMS and not at top level, */
  1148.               conres();                 /*  restore console again. */
  1149. #endif /* VMS */
  1150.             timelimit = 0;
  1151.             return(success = 1);        /* and return. */
  1152.         }
  1153.         y = addmac(vnp,s);              /* Add it to the macro table. */
  1154.         debug(F111,"ask addmac",vnp,y);
  1155.         cmsetp(psave);                  /* Restore old prompt. */
  1156. #ifdef VMS
  1157.         if (cmdlvl > 0)                 /* In VMS and not at top level, */
  1158.           conres();                     /*  restore console again. */
  1159. #endif /* VMS */
  1160.         timelimit = 0;
  1161.         return(success = (y < 0 ? 0 : 1) && (asktimedout == 0));
  1162.     }
  1163. }
  1164. #endif /* NOSPL */
  1165.  
  1166. #ifndef NOSPL
  1167. int
  1168. doincr(cx) int cx; {                    /* INCREMENT, DECREMENT */
  1169.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  1170.     int eval = 0;
  1171.     eval = (cx == XX_DECR || cx == XX_INCR);
  1172.  
  1173.     if ((y = cmfld("Variable name","",&s, eval ? xxstring : NULL)) < 0) {
  1174.         if (y == -3) {
  1175.             printf("?Variable name required\n");
  1176.             return(-9);
  1177.         } else return(y);
  1178.     }
  1179.     ckstrncpy(vnambuf,s,VNAML);
  1180.     if ((y = cmnum("by amount","1",10,&x,xxstring)) < 0)
  1181.       return(y);
  1182.     if ((y = cmcfm()) < 0)
  1183.       return(y);
  1184.  
  1185.     z = (cx == XX_INCR || cx == XXINC) ? 1 : 0; /* Increment or decrement? */
  1186.  
  1187.     if (incvar(vnambuf,x,z) < 0) {
  1188.         printf("?Variable %s not defined or not numeric\n",vnambuf);
  1189.         return(success = 0);
  1190.     }
  1191.     return(success = 1);
  1192. }
  1193.  
  1194. /* Used by doundef() */
  1195. static int
  1196. xxundef(s,verbose,simulate) char * s; int verbose, simulate; {
  1197.     int rc = 0;
  1198.     if (!s) return(0);
  1199.     if (*s == CMDQ && *(s+1) == '%') {
  1200.         char c = *(s+2), * p = NULL;
  1201.         if (c >= '0' && c <= '9') {
  1202.             if (maclvl < 0)
  1203.               p = g_var[c];
  1204.             else
  1205.               p = m_arg[maclvl][c - '0'];
  1206.         } else {
  1207.             if (isupper(c)) c += ('a'-'A');
  1208.             if (c >= 'a' && c <= 'z')
  1209.               p = g_var[c];
  1210.         }
  1211.         if (!p) return(-1);
  1212.     }
  1213.     if (verbose)
  1214.       printf(" %s ",s);
  1215.     if (simulate) {
  1216.         printf("(SELECTED)\n");
  1217.     } else if ((x = delmac(s,1)) > -1) { /* Full name required */
  1218.         rc = 1;
  1219.         if (verbose) printf("(OK)\n");
  1220.     } else if (verbose)
  1221.       printf("(FAILED)\n");
  1222.     return(rc);
  1223. }
  1224.  
  1225. /* Do the (_)DEFINE, (_)ASSIGN, and UNDEFINE commands */
  1226.  
  1227. #define UND_MAT 1
  1228. #define UND_VRB 2
  1229. #define UND_EXC 3
  1230. #define UND_SIM 3
  1231.  
  1232. static struct keytab undefswi[] = {
  1233.     { "/list",     UND_VRB, 0 },
  1234. #ifdef COMMENT
  1235.     { "/except",   UND_EXC, CM_ARG },
  1236. #endif /* COMMENT */
  1237.     { "/matching", UND_MAT, 0 },
  1238.     { "/simulate", UND_SIM, 0 },
  1239.     { "/verbose",  UND_VRB, CM_INV }
  1240. };
  1241. static int nundefswi = sizeof(undefswi) / sizeof(struct keytab);
  1242.  
  1243. #define UNDEFMAX 64
  1244. static char ** undeflist = NULL;
  1245. int
  1246. doundef(cx) int cx; {                   /* UNDEF, _UNDEF */
  1247.     int i, j, n, rc = 0, arraymsg = 0;
  1248.     int domatch = 0, verbose = 0, errors = 0, simulate = 0, flag = 0;
  1249.     char *vnp, vnbuf[4];
  1250. #ifdef COMMENT
  1251.     char *except = NULL;
  1252. #endif /* COMMENT */
  1253.     struct FDB sw, fl;
  1254.     int getval;
  1255.     char c;
  1256.  
  1257.     if (!undeflist) {                   /* Allocate list if necessary */
  1258.         undeflist = (char **)malloc(UNDEFMAX * sizeof(char *));
  1259.         if (!undeflist) {
  1260.             printf("?Memory allocation failure\n");
  1261.             return(-9);
  1262.         }
  1263.         for (i = 0; i < UNDEFMAX; i++)
  1264.           undeflist[i] = NULL;
  1265.     }
  1266.     cmfdbi(&sw,                         /* First FDB - command switches */
  1267.            _CMKEY,                      /* fcode */
  1268.            "Variable name or switch",
  1269.            "",                          /* default */
  1270.            "",                          /* addtl string data */
  1271.            nundefswi,                   /* addtl numeric data 1: tbl size */
  1272.            4,                           /* addtl numeric data 2: 4 = cmswi */
  1273.            xxstring,                    /* Processing function */
  1274.            undefswi,                    /* Keyword table */
  1275.            &fl                          /* Pointer to next FDB */
  1276.            );
  1277.     cmfdbi(&fl,                         /* Anything that doesn't match */
  1278.            _CMFLD,                      /* fcode */
  1279.            "",                          /* hlpmsg */
  1280.            "",                          /* default */
  1281.            "",                          /* addtl string data */
  1282.            0,                           /* addtl numeric data 1 */
  1283.            0,                           /* addtl numeric data 2 */
  1284.            (cx == XXUNDEF) ? NULL : xxstring,
  1285.            NULL,
  1286.            NULL
  1287.            );
  1288.     while (1) {                         /* Parse 0 or more switches */
  1289.         x = cmfdb(&sw);                 /* Parse something */
  1290.         if (x < 0)
  1291.           return(x);
  1292.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  1293.           break;
  1294.         c = cmgbrk();
  1295.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  1296.             printf("?This switch does not take an argument\n");
  1297.             return(-9);
  1298.         }
  1299.         switch (cmresult.nresult) {
  1300.           case UND_MAT: domatch  = 1; break;
  1301.           case UND_SIM: simulate = 1; /* fall thru on purpose */
  1302.           case UND_VRB: verbose  = 1; break;
  1303.  
  1304. #ifdef COMMENT
  1305.           case UND_EXC:
  1306.             if (!getval) break;
  1307.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  1308.                 if (x == -3) {
  1309.                     printf("?Pattern required\n");
  1310.                     x = -9;
  1311.                 }
  1312.                 goto xgetx;
  1313.             }
  1314.             makestr(&except,cmresult.sresult);
  1315.             break;
  1316. #endif /* COMMENT */
  1317.  
  1318.           default:
  1319.             return(-2);
  1320.         }
  1321.     }
  1322.     n = 0;
  1323.     makestr(&(undeflist[n++]),cmresult.sresult);
  1324.     for (i = 1; i < UNDEFMAX; i++) {
  1325.         x = cmfld("Macro or variable name","",&s,
  1326.                   ((cx == XXUNDEF) ? NULL : xxstring)
  1327.                   );
  1328.         if (x == -3) {
  1329.             if ((y = cmcfm()) < 0)
  1330.               return(y);
  1331.             break;
  1332.         } else if (y < 0) {
  1333.             return(y);
  1334.         }
  1335.         makestr(&(undeflist[n++]),s);
  1336.     }
  1337.     /* Now we have a list of n variables or patterns to undefine */
  1338.  
  1339.     for (i = 0; i < n; i++) {
  1340.         flag = 0;
  1341.         if (!(vnp = undeflist[i]))
  1342.           continue;
  1343.         if (vnp[0] == CMDQ && (vnp[1] == '%' || vnp[1] == '&')) {
  1344.             flag++;
  1345.             vnp++;
  1346.         }
  1347.         if (!domatch) {                 /* Pattern match not requested */
  1348.             if (flag) {
  1349.                 if ((y = parsevar(vnp,&x,&z)) < 0) {
  1350.                     vnp--;
  1351.                     if (verbose) printf(" %s...error\n",vnp);
  1352.                     continue;
  1353.                 }
  1354.                 vnp--;
  1355.             }
  1356.             x = xxundef(vnp,verbose,simulate);
  1357.             if (x > -1) {
  1358.                 if (!x && !simulate) errors++;
  1359.                 rc += x;
  1360.             }
  1361.             continue;
  1362.         }
  1363.         /* Pattern match requested */
  1364.  
  1365.         if (!flag) {                    /* It's a macro */
  1366.             for (j = 0; j < nmac; j++) {
  1367.                 if (ckmatch(vnp,mactab[j].kwd,0,1)) {
  1368.                     x = xxundef(mactab[j].kwd,verbose,simulate);
  1369.                     if (x > -1) {
  1370.                         rc += x;
  1371.                         if (!x) errors++;
  1372.                     }
  1373.                     if (!simulate)
  1374.                       j--;              /* Because mactab shifted up */
  1375.                 }
  1376.             }
  1377.         } else if (vnp[0] == '%') {     /* It's a \%x variable */
  1378.             vnbuf[0] = CMDQ;
  1379.             vnbuf[1] = '%';
  1380.             vnbuf[3] = NUL;
  1381.             for (j = '0'; j <= 'z'; j++) { /* 0..9 a..z */
  1382.                 vnbuf[2] = j;
  1383.                 if (ckmatch(vnp,&vnbuf[1],0,1)) {
  1384.                     x = xxundef(vnbuf,verbose,simulate);
  1385.                     if (x > -1) {
  1386.                         if (!x) errors++;
  1387.                         rc += x;
  1388.                     }
  1389.                 }
  1390.                 if (j == '9') j = (int)'a' - 1; /* 9 -> a */
  1391.             }
  1392.         } else if (vnp[0] == '&') {
  1393.             if (!arraymsg && !quiet) {
  1394.                 printf("?UNDEFINE /MATCH can't be used with arrays.\n");
  1395.                 printf("(Type HELP ARRAY to see other methods.)\n");
  1396.             }
  1397.             arraymsg++;
  1398.             errors++;
  1399.         }
  1400.     }
  1401.     if (verbose)
  1402.       printf("undefined: %d, errors: %d\n",rc,errors);
  1403.  
  1404.     for (i = 0; i < UNDEFMAX; i++) {    /* Check them all */
  1405.         if (undeflist[i]) {             /* in case we were interrupted */
  1406.             free(undeflist[i]);         /* previously... */
  1407.             undeflist[i] = NULL;
  1408.         }
  1409.     }
  1410.     return(success = (errors == 0) ? 1 : 0);
  1411. }
  1412.  
  1413. int
  1414. dodef(cx) int cx; {
  1415.     extern int xxdot;
  1416.     extern char ppvnambuf[];
  1417.     int doeval = 0;
  1418.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  1419.     char *vnp;                          /* Pointer to same */
  1420.     int k, mydot;
  1421.     mydot = xxdot;                      /* Copy */
  1422.     xxdot = 0;                          /* and reset */
  1423. /*
  1424.   In case we got here from a command that begins like ".\%a", cmkey() has
  1425.   already evaluated \%a, but we don't want that, so we retrieve the variable
  1426.   name from a special pre-evaluation buffer in the command module, and we
  1427.   undo the "unget word" that would be done because of the token, because if
  1428.   the variable was defined, it will unget its value rather than its name.
  1429. */
  1430.     s = NULL;
  1431.  
  1432.     if (mydot && ppvnambuf[0] == '.' && ppvnambuf[1]) {
  1433.         s = ppvnambuf+1;
  1434.         unungw();
  1435.     }
  1436.     if (!s) {
  1437.         if (cx == XXDFX || cx == XXASX)
  1438.           /* Evaluate variable name */
  1439.           y = cmfld("Macro or variable name","",&s,xxstring);
  1440.         else
  1441.           /* Don't evaluate the variable name */
  1442.           y = cmfld("Macro or variable name","",&s,NULL);
  1443.         if (y < 0) {
  1444.             if (y == -3) {
  1445.                 printf("?Variable name required\n");
  1446.                 return(-9);
  1447.             } else return(y);
  1448.         }
  1449.     }
  1450.     k = strlen(s);
  1451.     if (k > VNAML) {
  1452.         printf("?Name too long: \"%s\"\n",s);
  1453.         return(-9);
  1454.     }
  1455.     ckstrncpy(vnambuf,s,VNAML);
  1456.     vnambuf[VNAML] = NUL;
  1457.     vnp = vnambuf;
  1458.     if (vnambuf[0] == CMDQ && (vnambuf[1] == '%' || vnambuf[1] == '&')) vnp++;
  1459.     if (*vnp == '%' || *vnp == '&') {
  1460.         if ((y = parsevar(vnp,&x,&z)) < 0) return(y);
  1461. #ifdef COMMENT
  1462.         if (cx == XXUNDEF) {            /* Undefine */
  1463.             if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1464.             delmac(vnp,0);
  1465.             return(success = 1);
  1466.         }
  1467. #endif /* COMMENT */
  1468.         debug(F101,"dodef parsevar x","",x);
  1469.         if (mydot) {
  1470.             if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1471.               return(doeval);
  1472.             if (doeval > 0)             /* Type of assignment */
  1473.               cx = XXASS;
  1474.         }
  1475.         if (y == 1) {                   /* Simple variable */
  1476.             if ((y = cmtxt("Definition of variable","",&s,NULL)) < 0)
  1477.               return(y);
  1478.             s = brstrip(s);
  1479.             debug(F110,"xxdef var name",vnp,0);
  1480.             debug(F110,"xxdef var def",s,0);
  1481.         } else if (y == 2) {            /* Array element */
  1482.             if ((y = arraynam(vnp,&x,&z)) < 0) return(y);
  1483.             if (x == 96) {
  1484.                 printf("?Argument vector array is read-only\n");
  1485.                 return(-9);
  1486.             }
  1487.             if (chkarray(x,z) < 0) return(-2);
  1488.             if ((y = cmtxt("Definition of array element","",&s,NULL)) < 0)
  1489.               return(y);
  1490.             debug(F110,"xxdef array ref",vnp,0);
  1491.             debug(F110,"xxdef array def",s,0);
  1492.         }
  1493.     } else {                            /* Macro */
  1494. #ifdef COMMENT
  1495.         if (cx == XXUNDEF) {            /* Undefine */
  1496.             if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1497.             delmac(vnp,0);
  1498.             return(success = 1);
  1499.         }
  1500. #endif /* COMMENT */
  1501.         if (mydot) {
  1502.             if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1503.               return(doeval);
  1504.             if (doeval > 0)
  1505.               cx = XXASS;
  1506.         }
  1507.         if ((y = cmtxt("Definition of macro","",&s,NULL)) < 0) return(y);
  1508. #ifdef DEBUG
  1509.         if (deblog) {
  1510.             debug(F110,"xxdef macro name",vnp,0);
  1511.             debug(F010,"xxdef macro def",s,0);
  1512.         }
  1513. #endif /* DEBUG */
  1514.         s = brstrip(s);
  1515.     }
  1516.     if (*s == NUL) {                    /* No arg given, undefine */
  1517.         delmac(vnp,1);                  /* silently... */
  1518.         return(success = 1);            /* even if it doesn't exist... */
  1519.     }
  1520.     /* Defining a new macro or variable */
  1521.  
  1522.     if (cx == XXASS || cx == XXASX) {   /* ASSIGN rather than DEFINE? */
  1523.         int t;
  1524.         t = LINBUFSIZ-1;
  1525.         lp = line;                      /* If so, expand its value now */
  1526.         zzstring(s,&lp,&t);
  1527.         s = line;
  1528.     }
  1529.     if (doeval == 2) {                  /* Arithmetic evaluation wanted too? */
  1530.         ckstrncpy(line,evala(s),LINBUFSIZ);
  1531.         line[LINBUFSIZ] = NUL;
  1532.     }
  1533.     /* debug(F111,"calling addmac",s,(int)strlen(s)); */
  1534.  
  1535.     y = addmac(vnp,s);                  /* Add it to the appropriate table. */
  1536.     if (y < 0) {
  1537.         printf("?%s failed\n",(cx == XXASS || cx == XXASX) ?
  1538.                "ASSIGN" : "DEFINE");
  1539.         return(success = 0);
  1540.     } else if (cx == XXASX || cx == XXDFX) /* For _ASG or _DEF, */
  1541.       return(1);                           /* don't change success variable */
  1542.     else
  1543.       return(success = 1);
  1544. }
  1545. #endif /* NOSPL */
  1546.  
  1547.  
  1548. #ifndef NODIAL
  1549. /*
  1550.    L U D I A L  --  Lookup up dialing directory entry.
  1551.  
  1552.    Call with string to look up and file descriptor of open dialing directory
  1553.    file.  On success, returns number of matches found, with numbers stored
  1554.    in an array accessible via getdnum().
  1555. */
  1556. static char *dn_p[MAXDNUMS + 1];        /* Dial Number pointers */
  1557. static char *dn_p2[MAXDNUMS + 1];       /* Converted dial number pointers */
  1558. static int dn_x[MAXDNUMS + 1];          /* Type of call */
  1559. static int dncount = 0;
  1560. char * d_name = NULL;                   /* Dial name pointer */
  1561.  
  1562. char *                                  /* Get dial directory entry name */
  1563. getdname() {
  1564.     return(d_name ? d_name : "");
  1565. }
  1566.  
  1567. char *
  1568. getdnum(n) int n; {                     /* Get dial number n from directory */
  1569.     if (n < 0 || n > dncount || n > MAXDNUMS)
  1570.       return("");
  1571.     else
  1572.       return(dn_p[n]);
  1573. }
  1574.  
  1575. char *                  /* Check area code for spurious leading digit */
  1576. chk_ac(i,buf) int i; char buf[]; {
  1577.     char *p;
  1578.     if (!buf)
  1579.       return("");
  1580.     p = (char *) buf;                   /* Country we are calling: */
  1581.     if (i ==  44 ||                     /* UK */
  1582.         i ==  49 ||                     /* Germany */
  1583.         i ==  39 ||                     /* Italy */
  1584.         i ==  31 ||                     /* Netherlands */
  1585.         i == 351 ||                     /* Portugal */
  1586.         i ==  55 ||                     /* Brazil */
  1587.         i == 972 ||                     /* Israel */
  1588.         i ==  41 ||                     /* Switzerland */
  1589.         i ==  43 ||                     /* Austria */
  1590.         i ==  42 ||                     /* Czech Republic */
  1591.         i ==  36 ||                     /* Hungary */
  1592.         i ==  30 ||                     /* Greece */
  1593.         i == 352 ||                     /* Luxembourg */
  1594.         i ==  48 ||                     /* Poland */
  1595.         i ==  27 ||                     /* South Africa */
  1596.         i ==  33 ||                     /* France (as of 1997) */
  1597.         i ==  358                       /* Finland (ditto) */
  1598.         ) {
  1599.         if (buf[0] == '0')
  1600.           p++;
  1601.     }
  1602.     return(p);
  1603. }
  1604.  
  1605. /* Call Is Long Distance -- Expand this to cover 10-digit local dialing etc */
  1606. /*
  1607.    src  = area code of caller
  1608.    dest = area code of callee
  1609.    Returns:
  1610.      0 if call is local
  1611.      1 if call is long distance
  1612.      2 if call is local but area code must be dialed anyway
  1613. */
  1614. static int
  1615. callisld(src, dest) char * src, * dest; {
  1616.     int i;
  1617.     if (dialfld)                        /* Force long distance? */
  1618.       return(1);
  1619.     if (!strcmp(src,dest)) {            /* Area codes are the same */
  1620.         for (i = 0; i < nlocalac; i++)  /* Is AC in the lc-area-codes list? */
  1621.           if (!strcmp(src,diallcac[i]))
  1622.             return(2);                  /* Yes so must be dialed */
  1623.         return(0);                      /* No so don't dial it. */
  1624.     }
  1625.     for (i = 0; i < nlocalac; i++)      /* ACs not the same so look in list */
  1626.       if (!strcmp(dest,diallcac[i]))    /* Match */
  1627.         return(2);                      /* So local call with area code */
  1628.     return(1);                          /* Not local so long-distance */
  1629. }
  1630.  
  1631. char pdsfx[64] = { NUL, NUL };
  1632.  
  1633. #ifndef NOSPL
  1634. static char *
  1635. xdial(s) char *s; {                     /* Run dial string thru macro */
  1636.     int x, m;
  1637.     if (!dialmac)                       /* Dial macro name given? */
  1638.       return(NULL);
  1639.     if ((x = mxlook(mactab,dialmac,nmac)) < 0) /* Is the macro defined? */
  1640.       return(NULL);
  1641.     m = maclvl;
  1642.     x = dodo(x,s,0);                    /* Set up the macro */
  1643.     if (x > 0) {
  1644.         while (maclvl > m)              /* Execute the parser */
  1645.           parser(1);
  1646.         return(mrval[maclvl+1]);        /* Return the result */
  1647.     }
  1648.     return(NULL);
  1649. }
  1650. #endif /* NOSPL */
  1651.  
  1652. static int
  1653. dncvt(k,cx, prefix, suffix)
  1654.     int k, cx, prefix, suffix; {        /* Dial Number Convert */
  1655.     int i, j, n, what;                  /* cx is top-level command index */
  1656.     char *ss;                           /* prefix - add prefixes? */
  1657.     char *p, *p2, *pxo;                 /* suffix - add suffixes? */
  1658.     char *lac;
  1659.     char *npr;
  1660.     char *sfx;
  1661.     /* char *psfx; */
  1662.     char ccbuf[128];
  1663.     int cc;
  1664.     char acbuf[24];
  1665.     char *acptr;
  1666.     char outbuf[256];
  1667. /*
  1668.   First pass for strict (punctuation-based) interpretation.
  1669.   If it fails, we try the looser (length-based) one.
  1670. */
  1671.     dialtype = -1;
  1672.     what = 0;                           /* Type of call */
  1673.     s = dn_p[k];                        /* Number to be converted. */
  1674.     debug(F111,"dncvt",s,k);
  1675.     if (dn_p2[k]) {
  1676.         free(dn_p2[k]);
  1677.         dn_p2[k] = NULL;
  1678.     }
  1679.     if (!s) {
  1680.         printf("Error - No phone number to convert\n");
  1681.         return(-1);
  1682.     }
  1683.     if ((int)strlen(s) > 200) {
  1684.         ckstrncpy(outbuf,s,40);
  1685.         printf("?Too long: \"%s...\"\n",outbuf);
  1686.         return(-1);
  1687.     }
  1688.     npr = (prefix && dialnpr) ? dialnpr : "";
  1689.     sfx = (suffix && dialsfx) ? dialsfx : "";
  1690.     /* if (partial) psfx = dialsfx ? dialsfx : ""; */
  1691.     pxo = (prefix && dialpxo) ? dialpxo : "";
  1692.     lac = diallac ? diallac : "";       /* Local area code */
  1693.  
  1694.     outbuf[0] = NUL;                    /* Initialize conversion buffer */
  1695.     ss = s;                             /* Remember original string */
  1696.  
  1697.     if (*s != '+') {                    /* Literal number */
  1698.         dn_x[k] = DN_UNK;               /* Sort key is "unknown". */
  1699.         ckmakmsg(outbuf,256,            /* Sandwich it between */
  1700.                  pxo,npr,s,sfx          /* DIAL PREFIX and SUFFIX */
  1701.                 );
  1702. #ifdef CK_TAPI
  1703.         if (tttapi &&                   /* TAPI does its own conversions */
  1704.             !tapipass &&                /* if not in passthru mode */
  1705.             tapiconv == CK_AUTO ||      /* and TAPI conversions are AUTO */
  1706.             tapiconv == CK_ON           /* OR if TAPI conversions are ON */
  1707.             ) {
  1708.             char * p = NULL;
  1709.             dialtype = -2;
  1710.             if (!cktapiConvertPhoneNumber(dn_p[k], &p))
  1711.               return(-1);
  1712.             makestr(&dn_p2[k], p);
  1713.             if (p) free(p);
  1714.             return(0);
  1715.         } else
  1716. #endif /* CK_TAPI */
  1717.           makestr(&dn_p2[k], outbuf);   /* Not TAPI */
  1718.         dialtype = what;
  1719.         return(0);                      /* Done. */
  1720.     }
  1721.     i = 0;                              /* Portable number */
  1722.     s++;                                /* Tiptoe past the plus sign */
  1723.     ccbuf[0] = NUL;                     /* Do country code first */
  1724.  
  1725.     if (!diallcc) {                     /* Do we know our own? */
  1726.         if (cx != XXLOOK)
  1727.           printf("Error - prior SET DIAL COUNTRY-CODE command required\n");
  1728.         return(-1);
  1729.     }
  1730.  
  1731.     /* Parse the number */
  1732.  
  1733.     while (1) {                         /* Get the country code */
  1734.         while (*s == HT || *s == SP)
  1735.           s++;
  1736.         if (!s)                         /* Not in standard format */
  1737.           break;
  1738.         if (*s == '(') {                /* Beginning of area code  */
  1739.             s++;                        /* Skip past parenthesis   */
  1740.             ccbuf[i] = NUL;             /* End of country code */
  1741.             if (!s) {                   /* Check for end of string */
  1742.                 printf("Error - phone number ends prematurely: \"%s\"\n",ss);
  1743.                 return(-1);
  1744.             }
  1745.             break;
  1746.         } else {                        /* Collect country code */
  1747.             if (isdigit(*s))
  1748.               ccbuf[i++] = *s;          /* copy this character */
  1749.             s++;
  1750.             if (!*s || i > 127)         /* watch out for memory leak */
  1751.               break;
  1752.         }
  1753.     }
  1754.     cc = atoi(ccbuf);                   /* Numeric version of country code */
  1755.  
  1756.     i = 0;                              /* Now get area code */
  1757.     acbuf[0] = NUL;                     /* Initialize area-code buffer */
  1758.     acptr = acbuf;                      /* and pointer. */
  1759.     while (1) {
  1760.         while (*s == HT || *s == SP)    /* Ignore whitespace */
  1761.           s++;
  1762.         if (!s)                         /* String finished */
  1763.           break;
  1764.         if (*s == ')') {                /* End of area code  */
  1765.             s++;                        /* Skip past parenthesis   */
  1766.             acbuf[i] = NUL;             /* Terminate area-code buffer */
  1767.             break;
  1768.         } else {                        /* Part of area code */
  1769.             if (isdigit(*s))            /* If it's a digit, */
  1770.               acbuf[i++] = *s;          /* copy this character */
  1771.             s++;                        /* Point to next */
  1772.             if (!*s || i > 23)          /* Watch out for overflow */
  1773.               break;
  1774.         }
  1775.     }
  1776.  
  1777. /*
  1778.    Here we strip any leading 0 for countries that we know have
  1779.    0 as a long-distance prefix and do not have any area codes that
  1780.    start with 0 (formerly also ditto for "9" in Finland...)
  1781. */
  1782.     i = atoi(ccbuf);
  1783.     acptr = chk_ac(i,acbuf);
  1784.  
  1785.     while (*s == HT || *s == SP)        /* Skip whitespace */
  1786.       s++;
  1787.  
  1788. /* printf("S=[%s], ACPTR=[%s]\n",s,acptr); */
  1789.  
  1790.     if (*s && *acptr) {                 /* Area code was delimited */
  1791.  
  1792.         while (*s == '-' || *s == '.')  /* Skip past gratuitious punctuation */
  1793.           s++;
  1794.         if (!*s) s--;                   /* But not to end of string */
  1795.  
  1796.         if (strcmp(diallcc,ccbuf)) {    /* Out of country? */
  1797.             if (!dialixp) {             /* Need intl-prefix */
  1798.                 if (cx != XXLOOK)
  1799.                   printf("Error - No international dialing prefix defined\n");
  1800.                 return(-1);
  1801.             }
  1802.             what = dn_x[k] = DN_INTL;
  1803.             p  = (prefix && dialixp) ? dialixp : ""; /* Intl-prefix */
  1804.             p2 = (suffix && dialixs) ? dialixs : ""; /* Intl-suffix */
  1805.  
  1806.             /* Form the final phone number */
  1807. #ifdef COMMENT
  1808.             sprintf(pdsfx,"%s%s",p2,sfx); /* UNSAFE */
  1809.             sprintf(outbuf,
  1810.                     "%s%s%s%s%s%s%s%s",
  1811.                     pxo,npr,p,ccbuf,acptr,s,p2,sfx
  1812.                     );
  1813. #else
  1814.             ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1815.             ckmakxmsg(outbuf,256,pxo,npr,p,ccbuf,acptr,s,p2,sfx,
  1816.                       NULL,NULL,NULL,NULL);
  1817. #endif /* COMMENT */
  1818.  
  1819.         } else if ((x = callisld(lac,acptr)) >= 1) { /* In-country LD */
  1820.             if (!diallac && cx != XXLOOK) { /* Don't know my own area code */
  1821.                 if (cc == 1)
  1822.                   printf("WARNING - Prior SET DIAL AREA-CODE needed\n");
  1823.             }
  1824.             if (x == 2) {               /* Local call with area code */
  1825.                 what = dn_x[k] = DN_LOCAL;      /* Local-call */
  1826.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  1827.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  1828.             } else {
  1829.                 what = dn_x[k] = DN_LONG;       /* Long-distance */
  1830.                 for (i = 0; i < ntollfree; i++) { /* But toll-free too? */
  1831.                     if (!strcmp(acptr,dialtfc[i])) {
  1832.                         what = dn_x[k] = DN_FREE;
  1833.                         break;
  1834.                     }
  1835.                 }
  1836.                 if (what == DN_FREE) {  /* Toll-free call */
  1837.                     p = (prefix && dialtfp) ? dialtfp :
  1838.                         ((prefix && dialldp) ? dialldp : "");
  1839.                     p2 = "";            /* no suffix */
  1840.                 } else {                        /* normal long distance */
  1841.                     p  = (prefix && dialldp) ? dialldp : ""; /* ld-prefix */
  1842.                     p2 = (suffix && diallds) ? diallds : ""; /* ld-suffix */
  1843.                 }
  1844.             }
  1845.             /* Form the number to be dialed */
  1846. #ifdef COMMENT
  1847.             sprintf(outbuf,"%s%s%s%s%s%s%s",
  1848.                     pxo,npr,p,acptr,s,p2,sfx
  1849.                     );
  1850.             sprintf(pdsfx,"%s%s",p2,sfx);
  1851. #else
  1852.             ckmakxmsg(outbuf,256,
  1853.                       pxo,npr,p,acptr,s,p2,sfx,
  1854.                       NULL,NULL,NULL,NULL,NULL);
  1855.             ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1856. #endif /* COMMENT */
  1857.         } else {                        /* Same country, same area code */
  1858.             what = dn_x[k] = DN_LOCAL;  /* So it's a local call. */
  1859.             if (!prefix || !(dialpxo || ndialpxx)) { /* Not dialing from PBX */
  1860.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  1861.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  1862. #ifdef COMMENT
  1863.                 if (x == 2)
  1864.                   sprintf(outbuf,"%s%s%s%s%s%s",npr,p,acptr,s,p2,sfx);
  1865.                 else
  1866.                   sprintf(outbuf,"%s%s%s%s%s",npr,p,s,p2,sfx);
  1867.                 sprintf(pdsfx,"%s%s",p2,sfx);
  1868. #else
  1869.                 if (x == 2)
  1870.                   ckmakxmsg(outbuf,256,
  1871.                             npr,p,acptr,s,p2,sfx,
  1872.                             NULL,NULL,NULL,NULL,NULL,NULL);
  1873.                 else
  1874.                   ckmakxmsg(outbuf,256,
  1875.                             npr,p,s,p2,sfx,
  1876.                             NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  1877.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  1878. #endif /* COMMENT */
  1879.  
  1880.             } else {                    /* Dialing from a PBX and not TAPI */
  1881.                 if (ndialpxx) {         /* Is it internal? */
  1882. #ifdef COMMENT
  1883.                     i = (int) strlen(dialpxx);
  1884.                     j = (int) strlen(s);
  1885.                     x = -1;
  1886.                     if (j > i)
  1887.                       x = ckstrcmp(dialpxx,s,i,0);
  1888. #else
  1889.                     int kx;
  1890.                     x = -1;
  1891.                     j = (int) strlen(s);
  1892.                     for (kx = 0; kx < ndialpxx; kx++) {
  1893.                         i = (int) strlen(dialpxx[kx]);
  1894.                         if (j > i)
  1895.                           if (!(x = ckstrcmp(dialpxx[kx],s,i,0)))
  1896.                             break;
  1897.                     }
  1898. #endif /* COMMENT */
  1899.                     if (!x) {
  1900.                         char * icp, buf[32];
  1901.                         makestr(&matchpxx,dialpxx[kx]);
  1902.                         debug(F111,"dncvt matchpxx",matchpxx,kx);
  1903.                         what = dn_x[kx] = DN_INTERN;   /* Internal call. */
  1904.                         s += i;
  1905.                         /* Internal-call prefix */
  1906.                         icp = dialpxi;
  1907. #ifndef NOSPL
  1908.                         if (icp) {
  1909.                             if (*icp == '\\') {
  1910.                                 char c, *bp;
  1911.                                 int n;
  1912.                                 c = *(icp+1);
  1913.                                 if (isupper(c)) c = tolower(c);
  1914.                                 if (c == 'v' || c == 'f') {
  1915.                                     n = 32;
  1916.                                     bp = buf;
  1917.                                     zzstring(icp,&bp,&n);
  1918.                                     icp = buf;
  1919.                                 }
  1920.                             }
  1921.                         }
  1922. #endif /* NOSPL */
  1923.                         p = (prefix && icp) ? icp : "";
  1924. #ifdef COMMENT
  1925.                         sprintf(outbuf,"%s%s%s%s",npr,p,s,sfx);
  1926. #else
  1927.                         ckmakmsg(outbuf,256,npr,p,s,sfx);
  1928. #endif /* COMMENT */
  1929.                     } else {            /* External local call */
  1930.                         /* local-prefix */
  1931.                         p  = (prefix && diallcp) ? diallcp : "";
  1932.                         /* local-suffix */
  1933.                         p2 = (prefix && diallcs) ? diallcs : "";
  1934. #ifdef COMMENT
  1935.                         if (x == 2)
  1936.                           sprintf(outbuf,"%s%s%s%s%s%s%s",
  1937.                                   dialpxo ? dialpxo : "",
  1938.                                   npr,p,acptr,s,p2,sfx);
  1939.                         else
  1940.                           sprintf(outbuf,
  1941.                                   "%s%s%s%s%s%s",
  1942.                                   dialpxo ? dialpxo : "",
  1943.                                   npr,p,s,p2,sfx
  1944.                                   );
  1945. #else
  1946.                         if (x == 2)
  1947.                           ckmakxmsg(outbuf, 256,
  1948.                                    dialpxo ? dialpxo : "",
  1949.                                    npr,p,acptr,s,p2,sfx,
  1950.                                    NULL,NULL,NULL,NULL,NULL);
  1951.                         else
  1952.                           ckmakxmsg(outbuf, 256,
  1953.                                     dialpxo ? dialpxo : "",
  1954.                                     npr,p,s,p2,sfx,
  1955.                                     NULL,NULL,NULL,NULL,NULL,NULL);
  1956. #endif /* COMMENT */
  1957.                     }
  1958.                 }
  1959.             }
  1960.         }
  1961.  
  1962.     } else {                            /* Area code was not delimited */
  1963.  
  1964.         char xbuf[256];                 /* Comparison based only on length */
  1965.         char ybuf[256];
  1966.         int x, j;
  1967.  
  1968.         s = ss;
  1969.  
  1970.         for (i = 0; i < 255; i++) {
  1971.             if (!*s) break;
  1972.             while (!isdigit(*s)) {      /* Pay attention only to digits */
  1973.                 s++;
  1974.                 if (!*s) break;
  1975.             }
  1976.             xbuf[i] = *s++;
  1977.         }
  1978.         xbuf[i] = NUL;
  1979.  
  1980.         x = 1;                          /* Assume LD */
  1981.         n = 0;
  1982.         if (!dialfld) {                 /* If LD not forced */
  1983.             for (j = 0; j < nlocalac; j++) { /* check local AC list? */
  1984.                 ckmakmsg(ybuf,256,diallcc,diallcac[j],NULL,NULL);
  1985.                 n = (int) strlen(ybuf);
  1986.                 if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) {
  1987.                     x = 2;
  1988.                     break;
  1989.                 }
  1990.             }
  1991.             if (x == 1) {               /* Or exact match with local CC+AC? */
  1992.                 ckmakmsg(ybuf,256,diallcc,lac,NULL,NULL);
  1993.                 n = (int) strlen(ybuf);
  1994.                 if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0))
  1995.                   x = 0;
  1996.             }
  1997.         }
  1998.         if (x == 0 || x == 2) {         /* Local call */
  1999.             int xx,kx;                  /* Begin 1 Dec 2001... */
  2000.             /* Account for PBX internal calls */
  2001.             if (ndialpxx) {
  2002.                 xx = -1;
  2003.                 j = (int) strlen(ybuf);
  2004.                 for (kx = 0; kx < ndialpxx; kx++) {
  2005.                     i = (int) strlen(dialpxx[kx]);
  2006.                     if (j >= i)
  2007.                       if (!(xx = ckstrcmp(dialpxx[kx],&xbuf[j],i,0)))
  2008.                         break;
  2009.                 }
  2010.             }
  2011.             if (!xx) {
  2012.                 char * icp, buf[32];
  2013.                 makestr(&matchpxx,dialpxx[kx]);
  2014.                 debug(F111,"dncvt matchpxx",matchpxx,kx);
  2015.                 what = dn_x[kx] = DN_INTERN; /* Internal call. */
  2016.                 s = xbuf + j + i;
  2017.                 icp = dialpxi;          /* Internal-call prefix */
  2018. #ifndef NOSPL
  2019.                 if (icp) {
  2020.                     if (*icp == '\\') {
  2021.                         char c, *bp;
  2022.                         int n;
  2023.                         c = *(icp+1);
  2024.                         if (isupper(c)) c = tolower(c);
  2025.                         if (c == 'v' || c == 'f') {
  2026.                             n = 32;
  2027.                             bp = buf;
  2028.                             zzstring(icp,&bp,&n);
  2029.                             icp = buf;
  2030.                         }
  2031.                     }
  2032.                 }
  2033. #endif /* NOSPL */
  2034.                 p = (prefix && icp) ? icp : "";
  2035.                 ckmakmsg(outbuf,256,npr,p,s,sfx);
  2036.                 /* End 1 Dec 2001... */
  2037.  
  2038.             } else {                    /* Not PBX internal */
  2039.  
  2040.                 dn_x[k] = DN_LOCAL;
  2041.                 p = (prefix && diallcp) ? diallcp : "";
  2042.                 p2 = (suffix && diallcs) ? diallcs : "";
  2043.                 s = (char *) (xbuf + ((x == 0) ? n : (int)strlen(diallcc)));
  2044.                 ckmakxmsg(outbuf,256,
  2045.                           pxo,npr,p,s,p2,sfx,
  2046.                           NULL,NULL,NULL,NULL,NULL,NULL);
  2047.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2048.             }
  2049.         } else {                        /* Not local */
  2050.             n = ckstrncpy(ybuf,diallcc,256);
  2051.             if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) { /* Long distance */
  2052.                 dn_x[k] = DN_LONG;
  2053.                 p = (prefix && dialldp) ? dialldp : "";
  2054.                 p2 = (suffix && diallds) ? diallds : "";
  2055.                 s = xbuf + n;
  2056.                 while (*s == '-' || *s == '.')
  2057.                   s++;
  2058. #ifdef COMMENT
  2059.                 sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,s,p2,sfx);
  2060.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2061. #else
  2062.                 ckmakxmsg(outbuf,256,
  2063.                           pxo,npr,p,s,p2,sfx,
  2064.                          NULL,NULL,NULL,NULL,NULL,NULL);
  2065.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2066. #endif /* COMMENT */
  2067.             } else {
  2068.                 dn_x[k] = DN_INTL;      /* International */
  2069.                 if (!dialixp) {
  2070.                     if (cx != XXLOOK) {
  2071.                         printf(
  2072.                           "Error - No international dialing prefix defined\n"
  2073.                                );
  2074.                         return(-1);
  2075.                     }
  2076.                 }
  2077.                 p = (prefix && dialixp) ? dialixp : "";
  2078.                 p2 = (suffix && dialixs) ? dialixs : "";
  2079. #ifdef COMMENT
  2080.                 sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,xbuf,p2,sfx);
  2081.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2082. #else
  2083.                 ckmakxmsg(outbuf,256,
  2084.                           pxo,npr,p,xbuf,p2,sfx,
  2085.                           NULL,NULL,NULL,NULL,NULL,NULL);
  2086.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2087. #endif /* COMMENT */
  2088.             }
  2089.         }
  2090.     }
  2091. #ifdef CK_TAPI
  2092.     if (tttapi &&                       /* TAPI performs the conversions */
  2093.         !tapipass &&
  2094.         tapiconv == CK_AUTO ||
  2095.         tapiconv == CK_ON
  2096.         ) {
  2097.         p = NULL;
  2098.         dialtype = -2;
  2099.         if (!cktapiConvertPhoneNumber(dn_p[k],&p))
  2100.           return(-1);
  2101.         makestr(&dn_p2[k], p);
  2102.         if (p) free(p);
  2103.         return(0);
  2104.     } else {
  2105. #endif /* CK_TAPI */
  2106.         makestr(&dn_p2[k], outbuf);
  2107. #ifdef CK_TAPI
  2108.     }
  2109. #endif /* CK_TAPI */
  2110.     dialtype = what;
  2111.     return(0);
  2112. }
  2113.  
  2114. static int
  2115. ddcvt(s, f, n) char * s; FILE * f; int n; { /* Dial Directory Convert */
  2116.     char linebuf[1024], *s2;            /* Buffers and pointers */
  2117. #ifdef VMS
  2118.     char * temp = NULL;
  2119. #endif /* VMS */
  2120.     char *info[8];                      /* Pointers to words from entry */
  2121.     FILE * f2 = NULL;
  2122.     int x, rc;
  2123.     rc = -1;
  2124.  
  2125.     debug(F110,"ddcvt file",s,0);
  2126.  
  2127.     if (!s || !f)                       /* No filename or file */
  2128.       return(-1);
  2129.     if (!*s)
  2130.  
  2131.     fclose(f);
  2132.     znewn(s,&s2);                       /* s2 = address of static buffer */
  2133.     debug(F110,"ddcvt newname",s2,0);
  2134.  
  2135. #ifdef VMS
  2136.     /* In VMS, znewn() returns the same file name with a new version number */
  2137.     makestr(&temp,s);                   /* Swap - otherwise the new */
  2138.     s = s2;                             /* version has the older version */
  2139.     s2 = temp;                          /* number... */
  2140.     debug(F110,"ddcvt after swap s",s,0);
  2141.     debug(F110,"ddcvt after swap s2",s2,0);
  2142.     makestr(&(dialdir[n]),s);           /* New file gets new version number */
  2143.     debug(F110,"ddcvt after makestr s2",s2,0);
  2144.     debug(F111,"ddcvt dialdir[n]",dialdir[n],n);
  2145. #else
  2146.     if (zrename(s,s2) < 0) {            /* Not VMS - rename old file */
  2147.         perror(s2);                     /* to new (wierd) name. */
  2148.         goto ddexit;
  2149.     }
  2150. #endif /* VMS */
  2151.     debug(F110,"ddcvt s2 (old)",s2,0);
  2152.     if ((f = fopen(s2,"r")) == NULL) {  /* Reopen old file with wierd name */
  2153.         debug(F110,"ddcvt s2 open error",ck_errstr(),0);
  2154.         dirline = 0;                    /* (or in VMS, old version) */
  2155.         perror(s2);
  2156.         goto ddexit;
  2157.     }
  2158.     debug(F110,"ddcvt fopen(s2) OK",s2,0);
  2159.  
  2160.     debug(F110,"ddcvt s (new)",s,0);
  2161.     if ((f2 = fopen(s,"w")) == NULL) {  /* Create new file with old name */
  2162.         debug(F110,"ddcvt s open error",ck_errstr(),0);
  2163.         perror(s);                      /* (or in VMS, new version) */
  2164.         goto ddexit;
  2165.     }
  2166.     debug(F110,"ddcvt fopen(s) OK",s,0);
  2167.  
  2168.     printf("\nSaving old directory as %s.\nConverting %s...",s2,s);
  2169.     fprintf(f2,"; %s - Kermit dialing directory\n", s);
  2170.     fprintf(f2,"%-16s %-20s ; %5s %-6s ; %s\n",
  2171.                "; Name","Number","Speed","Parity","Comment"
  2172.                );
  2173.  
  2174.     while (1) {
  2175.         linebuf[0] = NUL;               /* Read a line */
  2176.         if (fgets(linebuf,1023,f) == NULL)
  2177.           break;
  2178.         debug(F110,"ddcvt linebuf",linebuf,0);
  2179.         if (!linebuf[0]) {              /* Empty line */
  2180.             fprintf(f2,"\n");
  2181.             continue;
  2182.         }
  2183.         x = (int) strlen(linebuf);      /* Strip line terminator, */
  2184.         while (x-- > 0) {               /* if any. */
  2185.             if (linebuf[x] <= SP)
  2186.               linebuf[x] = NUL;
  2187.             else
  2188.               break;
  2189.         }
  2190.         xwords(linebuf,5,info,1);       /* Parse it the old way */
  2191.         for (x = 1; x < 6; x++)
  2192.           if (!info[x]) info[x] = "";
  2193.         fprintf(f2,"%-16s %-20s ; %5s %-6s %s\n",
  2194.                info[1],info[2],info[3],info[4],info[5]
  2195.                );
  2196.     }
  2197.     printf(" OK\n\n");
  2198.     rc = 0;                             /* Success */
  2199.   ddexit:
  2200.     if (f) fclose(f);
  2201.     if (f2) fclose(f2);
  2202. #ifdef VMS
  2203.     if (temp) free(temp);
  2204. #endif /* VMS */
  2205.     return(rc);
  2206. }
  2207.  
  2208. int                                     /* s = name to look up   */
  2209. #ifdef CK_ANSIC                         /* cx = index of command */
  2210. ludial(char *s, int cx)                 /* (DIAL, LOOKUP, etc)   */
  2211. #else
  2212. ludial(s, cx) char *s; int cx;
  2213. #endif /* CK_ANSIC */
  2214. /* ludial */ {
  2215.  
  2216.     int dd, n1, n2, n3, i, j, t;        /* Workers */
  2217.     int olddir, newdir, oldentry, newentry;
  2218.     int pass = 0;
  2219.     int oldflg = 0;
  2220.     int ambiguous = 0;                  /* Flag for lookup was ambiguous */
  2221.     char *info[7];                      /* Pointers to words from entry */
  2222.     char *pp;                           /* Pointer to element of array */
  2223.     FILE * f;
  2224.     char *line;                         /* File input buffer */
  2225.  
  2226. /* #define LUDEBUG */
  2227.  
  2228. #ifdef LUDEBUG
  2229. int zz = 1;
  2230. #endif /* LUDEBUG */
  2231.  
  2232.     if (!s || ndialdir < 1)             /* Validate arguments */
  2233.       return(-1);
  2234.  
  2235.     if ((n1 = (int) strlen(s)) < 1)     /* Length of string to look up */
  2236.       return(-1);
  2237.  
  2238.     if (!(line = malloc(1024)))         /* Allocate input buffer */
  2239.       return(-1);
  2240.  
  2241. #ifdef LUDEBUG
  2242. if (zz) printf("LUDIAL 1 s[%s], n1=%d\n",s,n1);
  2243. #endif /* LUDEBUG */
  2244.  
  2245.     pass = 0;
  2246.   lu_again:
  2247.     f = NULL;                           /* Dial directory file descriptor */
  2248.     t = dncount = 0;                    /* Dial-number match count */
  2249.     dd = 0;                             /* Directory counter */
  2250.     olddir = 0;
  2251.     newdir = 0;
  2252. /*
  2253.   We need to recognize both old- and new-style directories.
  2254.   But we can't allow old-style and new-style entries in the same
  2255.   directory because there is no way to tell for sure the difference between
  2256.   an old-style entry like this:
  2257.  
  2258.     foo  5551212  9600
  2259.  
  2260.   and a new-style literal entry like this:
  2261.  
  2262.     foo  555 9600
  2263.  
  2264.   I.e. is the "9600" a speed, or part of the phone number?
  2265. */
  2266.     while (1) {                         /* We make one pass */
  2267.         if (!f) {                       /* Directory not open */
  2268.             if (dd >= ndialdir)         /* No directories left? */
  2269.               break;                    /* Done. */
  2270.             debug(F111,"ludial dialdir[dd]",dialdir[dd],dd);
  2271.             if ((f = fopen(dialdir[dd],"r")) == NULL) { /* Open it */
  2272.                 perror(dialdir[dd]);    /* Can't, print message saying why */
  2273.                 if (line) {
  2274.                     free(line);
  2275.                     line = NULL;
  2276.                 }
  2277.                 dd++;                   /* Go on to next one, if any... */
  2278.                 continue;
  2279.             }
  2280.             dirline = 0;                /* Directory file line number */
  2281.             if (dialdpy && !pass)
  2282.               printf("Opening: %s...\n",dialdir[dd]);
  2283.             dd++;
  2284.             if (!oldflg) olddir = 0;
  2285.             newdir = 0;
  2286.         }
  2287.         oldentry = 0;
  2288.         newentry = 0;
  2289.         line[0] = NUL;
  2290.         if (getnct(line,1023,f,1) < 0) { /* Read a line */
  2291.             if (f) {                    /* f can be clobbered! */
  2292.                 fclose(f);              /* Close the file */
  2293.                 f = NULL;               /* Indicate next one needs opening */
  2294.                 oldflg = 0;
  2295.             }
  2296.             continue;
  2297.         }
  2298.         if (!line[0])                   /* Empty line */
  2299.           continue;
  2300. #ifdef LUDEBUG
  2301. if (zz) printf("LUDIAL 2 s[%s]\n",s);
  2302. #endif /* LUDEBUG */
  2303.  
  2304.         /* Make a copy and parse it the old way */
  2305.         /* A copy is needed because xwords() pokes NULs into the string */
  2306.  
  2307.         if ((pp = malloc((int)strlen(line) + 1))) {
  2308.             strcpy(pp,line);            /* safe */
  2309.             xwords(pp,5,info,0);        /* Parse it the old way */
  2310.  
  2311. #ifdef LUDEBUG
  2312. if (zz) printf("LUDIAL 3 s[%s]\n",s);
  2313. #endif /* LUDEBUG */
  2314.  
  2315.             if (!info[1])
  2316.               continue;
  2317.             if (*info[1] == ';') {      /* If full-line comment, */
  2318.                 newdir = 1;             /* (only new directories have them) */
  2319.                 continue;               /* keep reading. */
  2320.             }
  2321.             if (!info[2])
  2322.               continue;
  2323.             if (*info[2] == '+')
  2324.               newentry = 1;
  2325.             if (info[4]) {
  2326.                 if ((*info[4] == '=') ||
  2327.                     !ckstrcmp(info[4],"none", 4,0) ||
  2328.                     !ckstrcmp(info[4],"even", 4,0) ||
  2329.                     !ckstrcmp(info[4],"space",5,0) ||
  2330.                     !ckstrcmp(info[4],"mark", 4,0) ||
  2331.                     !ckstrcmp(info[4],"odd",  3,0)
  2332.                     )
  2333.                   oldentry = 1;
  2334.             }
  2335.         }
  2336.         if (pp) {
  2337.             free(pp);
  2338.             pp = NULL;
  2339.         }
  2340.  
  2341.         /* Check consistency */
  2342.  
  2343.         if ((oldentry || olddir) && (newentry || newdir)) {
  2344.             printf(
  2345. "\nERROR: You seem to have old- and new-format entries mixed in your\n");
  2346.             printf(
  2347. "dialing directory.  You'll have to edit it by hand to convert it to the\n");
  2348. #ifndef NOHELP
  2349.             printf("new format.  Type HELP DIAL for further information.\n\n");
  2350. #else
  2351.             printf("new format.\n\n");
  2352. #endif /* NOHELP */
  2353.             if (line) {
  2354.                 free(line);
  2355.                 line = NULL;
  2356.             }
  2357.             return(-1);
  2358.         }
  2359.         if (!olddir && oldentry) {
  2360.             int convert = 0;
  2361.             olddir = 1;
  2362.             if (dialcvt == 2) {         /* 2 == ASK */
  2363.                 sprintf(tmpbuf,
  2364. "WARNING: Old-style dialing directory detected:\n%s", line);
  2365.         convert = uq_ok(tmpbuf,
  2366.                 "Shall I convert it for you? ",3,NULL,0);
  2367.             } else
  2368.               convert = dialcvt;
  2369.             if (convert) {
  2370.                 debug(F111,"ludial calling ddcvt",dialdir[dd-1],dd);
  2371.                 if (ddcvt(dialdir[dd-1],f,dd-1) < 0) {
  2372.                     debug(F111,"ludial ddcvt failed",dialdir[dd-1],dd);
  2373.                     oldflg = 1;
  2374.                     printf(
  2375. "  Sorry, can't convert.");
  2376.                     printf(
  2377. "  Will ignore speed and parity fields, continuing...\n\n");
  2378.                 } else {
  2379.                     olddir = newdir = 0;
  2380.                     debug(F111,"ludial ddcvt ok",dialdir[dd-1],dd);
  2381.                 }
  2382.                 dd--;
  2383.                 f = NULL;
  2384.                 continue;
  2385.             } else {
  2386.                 if (dialcvt == 2)
  2387.                   printf(
  2388. "  OK, will ignore speed and parity fields, continuing...\n\n");
  2389.                 olddir = 1;
  2390.             }
  2391.         }
  2392.  
  2393. #ifdef LUDEBUG
  2394. if (zz) printf("LUDIAL XX s[%s], n1=%d\n",s,n1);
  2395. #endif /* LUDEBUG */
  2396.  
  2397.         /* Now parse again for real */
  2398.  
  2399.         if (oldentry)                   /* Parse it the old way */
  2400.           xwords(line,5,info,0);
  2401.         else                            /* Parse it the new way */
  2402.           xwords(line,2,info,1);
  2403.  
  2404. #ifdef LUDEBUG
  2405. if (zz) printf("LUDIAL YY s[%s], n1=%d\n",s,n1);
  2406. if (zz) printf("%s [%s]\n",info[1],info[2]);
  2407. #endif /* LUDEBUG */
  2408.  
  2409.         if (info[1]) {                  /* First word is entry name */
  2410.             if ((n3 = (int) strlen(info[1])) < 1) /* Its length */
  2411.               continue;                 /* If no first word, keep reading. */
  2412.             if (n3 < n1)                /* Search name is longer */
  2413.               continue;                 /* Can't possibly match */
  2414.             if (ambiguous && n3 != n1)
  2415.               continue;
  2416.  
  2417. #ifdef LUDEBUG
  2418. if (zz) printf("MATCHING: [%s] [%s], n1=%d\n",s,info[1],n1);
  2419. #endif /* LUDEBUG */
  2420.  
  2421.             if (ckstrcmp(s,info[1],n1,0)) /* Caseless string comparison */
  2422.               continue;
  2423.  
  2424. #ifdef LUDEBUG
  2425. if (zz) printf("MATCH OK: [%s] [%s], n1=%d\n",s,info[1],n1);
  2426. #endif /* LUDEBUG */
  2427.  
  2428.             if (!info[2])               /* No phone number given */
  2429.               continue;
  2430.             if ((n2 = (int) strlen(info[2])) < 1) /* Length of phone number */
  2431.               continue;                 /* Ignore empty phone numbers */
  2432.  
  2433.             /* Got one */
  2434.  
  2435.             if (!(pp = (char *)malloc(n2 + 1))) { /* Allocate storage for it */
  2436.                 printf("?internal error - ludial malloc 1\n");
  2437.                 if (line) {
  2438.                     free(line);
  2439.                     line = NULL;
  2440.                 }
  2441.                 dncount = 0;
  2442.                 return(-1);
  2443.             }
  2444.             strcpy(pp,info[2]);         /* safe */
  2445.  
  2446.             if (dncount > MAXDNUMS) {
  2447.                 printf("Warning: %d matches found, %d max\n",
  2448.                        dncount,
  2449.                        MAXDNUMS
  2450.                        );
  2451.                 dncount = MAXDNUMS;
  2452.                 break;
  2453.             }
  2454.             dn_p[dncount++] = pp;       /* Add pointer to array. */
  2455.             if (dncount == 1) {         /* First one... */
  2456.                 if (d_name) free(d_name);
  2457.                 if (!(d_name = (char *)malloc(n3 + 1))) { /* Save its name */
  2458.                     printf("?internal error - ludial malloc 2\n");
  2459.                     if (line) {
  2460.                         free(line);
  2461.                         line = NULL;
  2462.                     }
  2463.                     dncount = 0;
  2464.                     return(-1);
  2465.                 }
  2466.                 t = n3;                 /* And its length */
  2467.                 strcpy(d_name,info[1]); /* safe */
  2468.             } else {                    /* Second or subsequent one */
  2469.  
  2470. #ifdef LUDEBUG
  2471.                 if (zz)
  2472.                   printf("d_name=[%s],info[1]=%s,t=[%d]\n",d_name,info[1],t);
  2473. #endif /* LUDEBUG */
  2474.  
  2475.                 if ((int) strlen(info[1]) == t) /* Lengths compare */
  2476.                   if (!ckstrcmp(d_name,info[1],t,0)) /* Caseless compare OK */
  2477.                     continue;
  2478.  
  2479.                 /* Name given by user matches entries with different names */
  2480.  
  2481.                 if (ambiguous)          /* Been here before */
  2482.                   break;
  2483.  
  2484.                 ambiguous = 1;          /* Now an exact match is required */
  2485.                 for (j = 0; j < dncount; j++) { /* Clean out previous list */
  2486.                     if (dn_p[j]) {
  2487.                         free(dn_p[j]);
  2488.                         dn_p[j] = NULL;
  2489.                     }
  2490.                 }
  2491.                 pass++;                 /* Second pass... */
  2492.                 goto lu_again;          /* Do it all over again. */
  2493.             }
  2494.         }
  2495.     }
  2496.     if (line) free(line);
  2497.     if (dncount == 0 && ambiguous) {
  2498.         printf(" Lookup: \"%s\" - ambiguous%s\n",
  2499.                s,
  2500.                cx == XXLOOK ? "" : " - dialing skipped"
  2501.                );
  2502.         return(-2);
  2503.     }
  2504.     return(dncount);
  2505. }
  2506.  
  2507. char *
  2508. pncvt(s) char *s; {                     /* Phone number conversion */
  2509.     char *p = NULL;                     /* (just a wrapper for dncvt() */
  2510.     char *q = NULL;
  2511.     static char pnbuf[128];
  2512.     makestr(&p,dn_p[0]);                /* Save these in case they are */
  2513.     makestr(&q,dn_p2[0]);               /* being used */
  2514.     makestr(&dn_p[0],s);                /* Copy the argument string to here */
  2515.     dncvt(0,XXLOOK,1,1);                /* Convert it */
  2516.     if (!dn_p2[0])                      /* Put result where can return it */
  2517.       pnbuf[0] = NUL;
  2518.     else
  2519.       ckstrncpy(pnbuf,dn_p2[0],127);
  2520.     makestr(&dn_p[0],p);                /* Restore these */
  2521.     makestr(&dn_p2[0],q);
  2522.     makestr(&p,NULL);                   /* Free these */
  2523.     makestr(&q,NULL);
  2524.     return((char *)pnbuf);
  2525. }
  2526.  
  2527. int
  2528. dodial(cx) int cx; {                    /* DIAL or REDIAL */
  2529.     int i = 0, x = 0;                   /* Workers */
  2530.     int sparity = -1;                   /* For saving global parity value */
  2531.     int previous = 0;
  2532.     int len = 0;
  2533.     int literal = 0;
  2534.     int flowsave;
  2535.     int lufound = 0;                    /* Did any lookup succeed? */
  2536.     int prefix = 1;
  2537.     int postfix = 1;
  2538.     int wasalpha = 0;
  2539.     int xredial = 0;
  2540.     int braces = 0;
  2541.  
  2542.     char *p = NULL, *s3 = NULL, * sav = NULL;
  2543.     int j = 0, t = 0, n = 0;
  2544.     int xretries, xlcc;
  2545.  
  2546.     debug(F101,"dodial cx","",cx);
  2547.     debug(F111,"dodial diallcc",diallcc,diallcc);
  2548.  
  2549.     xretries = dialrtr;                 /* If retries not set, */
  2550.     if (diallcc) {                      /* choose default based on */
  2551.         xlcc = atoi(diallcc);           /* local country code. */
  2552.         if (xretries < 0) {
  2553.             switch (xlcc) {
  2554.               case 1: xretries = 10; break; /* No restrictions in NANP */
  2555.                 /* Add other country codes here */
  2556.                 /* that are known to have no restrictions on redialing. */
  2557.               default: xretries = 1;
  2558.             }
  2559.         }
  2560.     }
  2561.     if (cx == XXPDIA) {                 /* Shortcut... */
  2562.         cx = XXDIAL;
  2563.         partial = 1;
  2564.         debug(F100,"PDIAL sets partial=1","",0);
  2565.         postfix = 0;                    /* Do not add postfix */
  2566.     } else {
  2567.         partial = 0;
  2568.         debug(F100,"DIAL sets partial=0","",0);
  2569.     }
  2570.     previous = dialsta;                 /* Status of previous call, if any */
  2571.     if (previous == DIA_PART) {
  2572.         prefix = 0;                     /* do not add prefix */
  2573.     }
  2574.     s = NULL;                           /* Initialize user's dial string */
  2575.     if (cx == XXRED) {                  /* REDIAL or... */
  2576.         if ((y = cmcfm()) < 0)
  2577.           return(y);
  2578.     } else if (cx == XXANSW) {          /* ANSWER or ... */
  2579.         if ((y = cmnum("timeout (seconds)","0",10,&x,xxstring)) < 0)
  2580.           return(y);
  2581.         dialatmo = x;
  2582.         if ((y = cmcfm()) < 0)
  2583.           return(y);
  2584.     } else {                            /* DIAL or LOOKUP */
  2585.         if (ndialdir > 0)
  2586.           s3 = "Number to dial or entry from dial directory";
  2587.         else
  2588.           s3 = "Number to dial";
  2589.         if ((x = cmtxt(s3, dialnum ? dialnum : "",&s,xxstring)) < 0)
  2590.           return(x);
  2591.         if (s) {
  2592.             len = (int) strlen(s);
  2593.             ckstrncpy(tmpbuf,s,TMPBUFSIZ); /* Save literal copy */
  2594. #ifdef COMMENT
  2595.             if (len > 1) {              /* Strip outer braces if given */
  2596.                 if (*s == '{') {
  2597.                     if (s[len-1] == '}') {
  2598.                         s[len-1] = NUL;
  2599.                         s++;
  2600.                         len -= 2;
  2601.                     }
  2602.                 }
  2603.             }
  2604. #else
  2605.             s = brstrip(s);             /* Strip outer braces or quotes */
  2606. #endif /* COMMENT */
  2607.         }
  2608.     }
  2609.  
  2610.     if (cx != XXLOOK) {                 /* Not LOOKUP */
  2611. #ifdef IKSD
  2612.         if (inserver) {
  2613.             printf("Sorry, dialing is disabled.\r\n");
  2614.             return(success = 0);
  2615.         }
  2616. #endif /* IKSD */
  2617. #ifdef CK_TAPI
  2618.         if (tttapi && !tapipass) {
  2619.           ;                             /* Skip the modem test if TAPI */
  2620.         } else
  2621. #endif /* CK_TAPI */
  2622.         if (mdmtyp < 1 && !dialtest) {
  2623.             if (network
  2624. #ifdef TN_COMPORT
  2625.                  && !istncomport()
  2626. #endif /* TN_COMPORT */
  2627.                  )
  2628.               printf("Please SET HOST first, and then SET MODEM TYPE\n");
  2629.             else
  2630.               printf("Sorry, you must SET MODEM TYPE first\n");
  2631.             dialsta = DIA_NOMO;
  2632.             return(success = 0);
  2633.         }
  2634.         if (!local && !dialtest) {
  2635.             printf("Sorry, you must SET %s or SET HOST first\n",
  2636. #ifdef OS2
  2637.                    "PORT"
  2638. #else
  2639.                    "LINE"
  2640. #endif /* OS2 */
  2641.                    );
  2642.             dialsta = DIA_NOLI;
  2643.             return(success = 0);
  2644.         }
  2645.         if ((!network 
  2646. #ifdef TN_COMPORT
  2647.               || istncomport()
  2648. #endif /* TN_COMPORT */
  2649.               ) && !dialtest &&
  2650. #ifdef CK_TAPI
  2651.              !tttapi &&
  2652. #endif /* CK_TAPI */
  2653.             (speed < 0L)
  2654. #ifdef UNIX
  2655.             && (strcmp(ttname,"/dev/null"))
  2656. #else
  2657. #ifdef OSK
  2658.             && (strcmp(ttname,"/nil"))
  2659. #endif /* OSK */
  2660. #endif /* UNIX */
  2661.             ) {
  2662.             printf("\nSorry, you must SET SPEED first\n");
  2663.             dialsta = DIA_NOSP;
  2664.             return(success = 0);
  2665.         }
  2666.     }
  2667.     if (cx != XXANSW) {
  2668.         for (j = 0; j < MAXDNUMS; j++) { /* Initialize dial-number list */
  2669.             if (!dialnum) {             /* First time dialing */
  2670.                 dn_p[j] = NULL;         /* initialize all pointers. */
  2671.                 dn_p2[j] = NULL;
  2672.             } else if (dn_p[j]) {       /* Not the first time, */
  2673.                 free(dn_p[j]);          /* free previous, if any, */
  2674.                 dn_p[j] = NULL;         /* then set to NULL. */
  2675.                 if (dn_p2[j])
  2676.                   free(dn_p2[j]);
  2677.                 dn_p2[j] = NULL;
  2678.             } else break;               /* Already NULL */
  2679.         }
  2680.         if (len == 0)
  2681.           s = NULL;
  2682.         if (!s)
  2683.           s = dialnum;
  2684.         if (!s) {
  2685.             if (cx == XXLOOK)
  2686.               printf("?Lookup what?\n");
  2687.             else
  2688.               printf("%s\n", (cx == XXRED) ?
  2689.                    "?No DIAL command given yet" :
  2690.                    "?You must specify a number to dial"
  2691.                    );
  2692.             return(-9);
  2693.         }
  2694.  
  2695.     /* Now we have the "raw" dial or lookup string and s is not NULL */
  2696.  
  2697.         makestr(&dscopy,s);             /* Put it in a safe place */
  2698.         s = dscopy;
  2699.         n = 0;
  2700.  
  2701.         debug(F111,"dodial",s,ndialdir);
  2702.  
  2703.         wasalpha = 0;
  2704.         if (isalpha(*s)) {
  2705.             wasalpha = 1;
  2706.             if (ndialdir > 0) {         /* Do we have a dialing directory? */
  2707.                 n = ludial(s,cx);       /* Look up what the user typed */
  2708.                 if (n == 0)
  2709.                   printf(" Lookup: \"%s\" - not found%s\n",
  2710.                          s,
  2711.                          cx == XXLOOK ? "" : " - dialing as given\n"
  2712.                          );
  2713.             }
  2714.             debug(F101,"dodial",s,n);
  2715.             if (n < 0 && cx != XXLOOK) { /* Error out if they wanted to dial */
  2716.                 if (n == -1)            /* -2 means ludial already gave msg */
  2717.                   printf(" Lookup: fatal error - dialing skipped\n");
  2718.                 dialsta = DIA_DIR;
  2719.                 return(-9);
  2720.             }
  2721.             if (n > 0)                  /* A successful lookup */
  2722.               lufound = 1;
  2723.         } else if (*s == '=') {         /* If number starts with = sign */
  2724.             s++;                        /* strip it */
  2725.             literal = 1;                /* remember this */
  2726.             while (*s == SP) s++;       /* and then also any leading spaces */
  2727.         } else if (tmpbuf[0] == '{' && tmpbuf[1] == '{') {
  2728.             makelist(tmpbuf,dn_p,MAXDNUMS);
  2729.             makestr(&dscopy,tmpbuf);
  2730.             s = tmpbuf;
  2731.             for (n = 0; n < MAXDNUMS; n++) /* (have to count how many) */
  2732.               if (!dn_p[n]) break;
  2733.             braces = 1;
  2734.         }
  2735.         if (cx == XXLOOK && !wasalpha && !braces) {
  2736.             /* We've been told to lookup a number or a quoted name */
  2737.             char *p;
  2738.             n = 0;
  2739.             p = literal ? s : pncvt(dscopy);
  2740.             if (!p) p = "";
  2741.             if (*p) {
  2742.                 printf("%s  => %s\n", dscopy, p);
  2743.                 return(success = 1);
  2744.             } else {
  2745.                 printf("?Bad phone number\n");
  2746.                 return(success = 0);
  2747.             }
  2748.         }
  2749.         /* Save DIAL or successful LOOKUP string for future DIAL or REDIAL */
  2750.         /* But don't save pieces of partial dial ... */
  2751.  
  2752.         debug(F101,"DIAL save dialnum partial","",partial);
  2753.         debug(F101,"DIAL save dialnum previous","",previous);
  2754.         if ((cx == XXDIAL && partial == 0 && previous != DIA_PART) ||
  2755.             (cx == XXLOOK && n > 0)) {
  2756.             makestr(&dialnum,dscopy);
  2757.             if (!quiet && dscopy && !dialnum)
  2758.               printf("WARNING - memory allocation failure: redial number\n");
  2759.         }
  2760.         if (n > 0) {
  2761.             if (!quiet && !backgrd && !braces /* && dialdpy */ ) {
  2762.                 if (!strcmp(d_name,s))
  2763.                   printf(" Lookup: \"%s\" - exact match\n",s);
  2764.                 else
  2765.                   printf(" Lookup: \"%s\" - uniquely matches \"%s\"\n",
  2766.                          s,
  2767.                          d_name
  2768.                          );
  2769.             }
  2770.             if ((cx == XXLOOK) ||
  2771.                 ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2772.                 printf(" %d telephone number%sfound for \"%s\"%s\n",
  2773.                        n,
  2774.                        (n == 1) ? " " : "s ",
  2775.                        s,
  2776.                        (n > 0) ? ":" : "."
  2777.                        );
  2778.                 s3 = getdname();
  2779.             }
  2780.             for (i = 0; i < n; i++) {   /* Convert */
  2781.                 dn_x[i] = -1;
  2782.                 if (dncvt(i,cx,prefix,postfix) < 0) {
  2783.                     if (cx != XXLOOK) {
  2784.                         dialsta = DIA_DIR;
  2785.                         return(-9);
  2786.                     }
  2787.                 }
  2788.             }
  2789.             if (dialsrt && n > 1) {     /* Sort into optimal order */
  2790.                 for (i = 0; i < n-1; i++) {
  2791.                     for (j = i+1; j < n; j++) {
  2792.                         if (dn_x[j] < dn_x[i]) {
  2793.                             t = dn_x[j];
  2794.                             dn_x[j] = dn_x[i];
  2795.                             dn_x[i] = t;
  2796.                             p = dn_p[j];
  2797.                             dn_p[j] = dn_p[i];
  2798.                             dn_p[i] = p;
  2799.                             p = dn_p2[j];
  2800.                             dn_p2[j] = dn_p2[i];
  2801.                             dn_p2[i] = p;
  2802.                         }
  2803.                     }
  2804.                 }
  2805.             }
  2806.             if ((cx == XXLOOK) ||
  2807.                 ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2808.                 int nn = n;
  2809. #ifndef NOSPL
  2810.                 char * p;
  2811. #endif /* NOSPL */
  2812.                 if (cx != XXLOOK)
  2813.                   if (n > 12) nn = 12;
  2814.                 for (i = 0; i < nn; i++) {
  2815.                     printf("%3d. %-12s  %-20s =>  %-20s  (%d)\n",i+1,
  2816.                            s3, dn_p[i],
  2817.                            dn_p2[i] ? dn_p2[i] : "(processing failed)",
  2818.                            dn_x[i]
  2819.                            );
  2820.                 }
  2821.                 if (cx != XXLOOK && n != nn)
  2822.                   printf("And %d more...\n", n - nn);
  2823.             }
  2824.         } else if (n == 0) {            /* Not found in directory */
  2825.             makestr(&(dn_p[0]),literal ? s : dscopy);
  2826.             makestr(&d_name,literal ? s : dscopy);
  2827.             dncount = 1;
  2828.             n = 1;
  2829.             if (dncvt(0,cx,prefix,postfix) < 0) { /* In case they typed a */
  2830.                 dialsta = DIA_DIR;      /* portable-format number ... */
  2831.                 return(-9);
  2832.             }
  2833.         }
  2834.  
  2835. #ifndef NONET
  2836. #ifdef NETCONN
  2837.         /* It's not good that the networks directory depends on NOT-NODIAL.. */
  2838.         if (cx == XXLOOK && dscopy) {   /* Networks here too... */
  2839.             extern char *nh_p[], *nh_p2[], *n_name;
  2840.             extern char *nh_px[4][MAXDNUMS+1];
  2841.             n = -1;
  2842.             if (nnetdir > 0) {          /* Do we have a network directory? */
  2843.                 dirline = 0;
  2844.                 n = lunet(dscopy);      /* Look up what the user typed */
  2845.             }
  2846.             if (n > -1) {
  2847.                 int k;
  2848.                 if (n > 0)              /* A successful lookup */
  2849.                   lufound = 1;
  2850.                 if (cx == XXLOOK && n == 0)
  2851.                   printf(" Lookup: \"%s\" - not found\n",dscopy);
  2852.                 else
  2853.                   printf("%s %d network entr%s found for \"%s\"%s\n",
  2854.                          cx == XXLOOK ? " Lookup:" : "",
  2855.                          n,
  2856.                          (n == 1) ? "y" : "ies",
  2857.                          dscopy,
  2858.                          (n > 0) ? ":" : "."
  2859.                          );
  2860.  
  2861.                 for (i = 0; i < n; i++) {
  2862.  
  2863.                     printf("%3d. %-12s => %-9s %s",
  2864.                            i+1,n_name,nh_p2[i],nh_p[i]);
  2865.                     for (k = 0; k < 4; k++) {
  2866.                         if (nh_px[k][i]) {
  2867.                             printf(" %s",nh_px[k][i]);
  2868.                         } else
  2869.                           break;
  2870.                     }
  2871.                     printf("\n");
  2872.                 }
  2873.             }
  2874.         }
  2875. #endif /* NETCONN */
  2876. #endif /* NONET */
  2877.         if (cx == XXLOOK)
  2878.           return(success = lufound);
  2879.     } /* cx != XXANSW */
  2880.  
  2881. #ifdef VMS
  2882.     conres();                   /* So Ctrl-C/Y will work */
  2883. #endif /* VMS */
  2884. /*
  2885.   Some modems do not react well to parity.  Also, if we are dialing through a
  2886.   TCP/IP TELNET modem server, parity can be fatally misinterpreted as TELNET
  2887.   negotiations.
  2888.  
  2889.   This should work even if the user interrupts the DIAL command, because the
  2890.   DIAL module has its own interrupt handler.  BUT... if, for some reason, a
  2891.   dialing device actually *requires* parity (e.g. CCITT V.25bis says that even
  2892.   parity should be used), this might prevent successful dialing.  For that
  2893.   reason, we don't do this for V.25bis modems.
  2894. */
  2895.     sparity = parity;                   /* Save current parity */
  2896.     if ((dialcapas & CKD_V25) == 0)     /* If not V.25bis...  */
  2897.       parity = 0;                       /* Set parity to NONE */
  2898.  
  2899.     flowsave = flow;
  2900. /*
  2901.   These modems use some kind of screwy flow control while in command mode,
  2902.   and do not present CTS as they should.  So if RTS/CTS is set (or even if
  2903.   it isn't) disable flow control during dialing.
  2904. */
  2905. #ifndef MINIDIAL
  2906.     if (mdmtyp == n_ATT1910 || mdmtyp == n_ATT1900) {
  2907.         flow = FLO_NONE;                /* This is not enough */
  2908. #ifdef CK_TTSETFLOW
  2909.         ttsetflow(FLO_NONE);            /* Really turn it off */
  2910. #endif /* CK_TTSETFLOW */
  2911.     }
  2912. #endif /* MINIDIAL */
  2913.     if (!network
  2914. #ifdef TN_COMPORT
  2915.         || istncomport()
  2916. #endif /* TN_COMPORT */
  2917.          ) {
  2918.         int x;
  2919.         if ((x = ttgmdm()) > -1) {
  2920.             if (!x && msgflg) {
  2921.                 printf(
  2922. "WARNING - No modem signals detected.  Is your modem turned on?  If not,\n\
  2923. use Ctrl-C to interrupt dialing, turn on your modem, then %s.\n",
  2924.                        cx == XXANSW ?
  2925.                        "ANSWER again" :
  2926.                        "REDIAL"
  2927.                        );
  2928.             }
  2929.             if (flow == FLO_RTSC) {
  2930.                 if (!(x & BM_CTS)) {
  2931.                     if (msgflg)
  2932.                       printf(
  2933. "WARNING - SET FLOW RTS/CTS is in effect but modem's CTS signal is off.\n\
  2934. Disabling flow control temporarily %s...\n",
  2935.                              cx == XXANSW ?
  2936.                              "while waiting for call" :
  2937.                              "during dialing"
  2938.                              );
  2939.                     flow = FLO_NONE;
  2940.                 }
  2941.             }
  2942.         }
  2943.     }
  2944.     if (cx == XXANSW) {                 /* ANSWER */
  2945.         success = ckdial("",0,0,1,0);
  2946.         goto dialfin;
  2947.     }
  2948.  
  2949. /* Edit 192 adds the ability to dial repeatedly. */
  2950.  
  2951.     i = 0;
  2952.     dialcount = 0;
  2953.     do {
  2954.         if (i > 0) printf("\nDial attempt %d of %d...\n", i+1, xretries);
  2955.         dialcount = i+1;
  2956.         success = 0;
  2957.         /* And the ability to dial alternate numbers. */
  2958.         /* Loop to dial each in a list of numbers for the same name... */
  2959.         for (j = 0; j < n && !success; j++) { /* until one answers. */
  2960.             s = dn_p2[j];               /* Next number in list */
  2961.             if (dn_x[j] >= dialrstr) {  /* Dial restriction */
  2962.                 printf("Restricted: %s, skipping...\n",dn_p[j]);
  2963.                 continue;
  2964.             }
  2965.             xredial = (i == 0 && j == 0) ? 0 : 1;
  2966.             if (!s) s = dn_p[j];
  2967.  
  2968. #ifndef NOSPL
  2969.             sav = s;
  2970.             p = xdial(s);               /* Apply DIAL macro now */
  2971.             if (p) s = p;
  2972. #endif /* NOSPL */
  2973.  
  2974.         /* Dial confirmation */
  2975.         /* NOTE: the uq_xxx() calls allow for a GUI dialog */
  2976.  
  2977.             if (i == 0 && dialcnf) {
  2978.         char msgbuf[128];
  2979.         ckmakmsg(msgbuf,128,"Dialing ",s,NULL,NULL);
  2980.         x = uq_ok(msgbuf,"Is this number correct? ",3,NULL,0);
  2981.                 if (!x) {
  2982.  
  2983. #ifndef COMMENT
  2984.             x = uq_txt(        /* Allow GUI dialog */
  2985. #ifdef OS2
  2986. " Please enter the correct number,\r\n or press Enter to skip.",
  2987. #else
  2988. " Please enter the correct number,\r\n or press Return to skip.",
  2989. #endif /* OS2 */
  2990.                               "Corrected phone number: ",
  2991.                                1,
  2992.                    NULL,
  2993.                    atmbuf,
  2994.                    ATMBL,
  2995.                    s);
  2996.             if (x && atmbuf[0]) { /* They gave a new one */
  2997.             s = atmbuf;
  2998.             makestr(&(dn_p2[j]), s);
  2999.             }            
  3000.  
  3001. #else  /* COMMENT */
  3002.  
  3003. #ifdef CK_RECALL
  3004.                     extern int on_recall;
  3005. #endif /* CK_RECALL */
  3006.                     cmsavp(psave,PROMPTL);
  3007.                     cmsetp(
  3008. #ifdef OS2
  3009. " Please enter the correct number,\r\n or press Enter to skip: "
  3010. #else
  3011. " Please enter the correct number,\r\n or press Return to skip: "
  3012. #endif /* OS2 */
  3013.                            );
  3014.                     cmini(ckxech);
  3015.                     x = -1;
  3016.                     if (pflag) prompt(NULL);
  3017. #ifdef CK_RECALL
  3018.                     on_recall = 0;
  3019. #endif /* CK_RECALL */
  3020.                     y = cmdgquo();
  3021.                     cmdsquo(0);
  3022.                     while (x < 0) {
  3023.                         x = cmtxt("Corrected phone number","",&s,NULL);
  3024.                         cmres();
  3025.                     }
  3026.                     if ((int) strlen(s) < 1) {
  3027.                         cmsetp(psave);
  3028.                         continue;
  3029.                     }
  3030.                     makestr(&(dn_p2[j]), s);
  3031.                     cmdsquo(y);
  3032.                     cmsetp(psave);
  3033. #endif /* COMMENT */
  3034.                 }
  3035.             }
  3036.             if (dialtest) {             /* Just testing */
  3037.                 if (i + j == 0)
  3038.                   printf("\nTESTING...\n");
  3039.                 if (dialmac)
  3040.                   printf(" Number: \"%s\" => \"%s\"\n",sav,s);
  3041.                 else
  3042.                   printf(" Number: \"%s\"\n",s);
  3043.                 dialsta = DIA_BUSY;
  3044.                 success = 0;
  3045.             } else {
  3046.                 what |= W_DIALING;
  3047.                 success = ckdial(s,i,j,partial ? 3 : 0, xredial); /* Dial it */
  3048.                 what &= ~(W_DIALING);
  3049.                 if (!success) {
  3050.                     if (dialsta < 8 ||  /* Break out if unrecoverable error */
  3051.                         dialsta  == DIA_INTR ||
  3052.                         dialsta  == DIA_ERR  ||
  3053.                         previous == DIA_PART
  3054.                         )
  3055.                       break;
  3056.                 }
  3057.             }
  3058.         }
  3059.         if (success)                    /* Succeeded, leave the outer loop */
  3060.           break;
  3061.         if (dialsta < 8 ||              /* Break out if unrecoverable error */
  3062.             dialsta == DIA_INTR ||      /* Interrupted */
  3063.             dialsta == DIA_NODT ||      /* No dialtone */
  3064.             dialsta == DIA_NOAC ||      /* Access forbidden */
  3065.             dialsta == DIA_BLCK ||      /* Blacklisted */
  3066.             dialsta == DIA_DIR  ||      /* Dialing directory error */
  3067.             dialsta == DIA_ERR  ||      /* Modem command error */
  3068.             previous == DIA_PART)
  3069.           break;
  3070.         if (++i >= xretries)            /* Break out if too many tries */
  3071.           break;
  3072.         if (!backgrd && !quiet) {
  3073.             if (dialint > 5)
  3074.               printf(
  3075. "\nWill redial in %d second%s- press any key to redial immediately.\n",
  3076.                      dialint,
  3077.                      dialint == 1 ? " " : "s "
  3078.                      );
  3079.             printf("Ctrl-C to cancel...\n");
  3080.         }
  3081.         x = dialint;                    /* Redial interval */
  3082.         while (x-- > 0) {
  3083.             if ((y = conchk()) > 0) {   /* Did they type something? */
  3084.                 while (y--) coninc(0);  /* Yes, absorb it */
  3085.                 break;                  /* And wake up */
  3086.             }
  3087.             sleep(1);                   /* No interrupt, sleep a sec */
  3088.         }
  3089.     } while (!success);
  3090.  
  3091.   dialfin:
  3092.  
  3093.     if (cx != XXLOOK) {
  3094.         if (!success)
  3095.           bleep((short) BP_FAIL);
  3096.         else if (!quiet)
  3097.           bleep((short) BP_NOTE);
  3098. #ifdef OS2
  3099.         setint();                       /* Fix OS/2 interrupts */
  3100. #endif /* OS2 */
  3101.         if (sparity > -1)
  3102.           parity = sparity;             /* Restore parity if we saved it */
  3103.         flow = flowsave;
  3104. #ifdef OS2
  3105.         ttres();                        /* Restore DIAL device */
  3106. #endif /* OS2 */
  3107. #ifdef VMS
  3108.         concb((char)escape);            /* Restore console */
  3109. #endif /* VMS */
  3110. #ifdef OS2
  3111.         {                               /* Set session title */
  3112.             char * p, name[72];         /* in window list. */
  3113.             char * q;
  3114.             if (cx == XXANSW) {
  3115.                 q = "Incoming call";
  3116.             } else {
  3117.                 if (d_name)
  3118.                   q = d_name;
  3119.                 else if (dialnum)
  3120.                   q = dialnum;
  3121.                 else if (ttname[0])
  3122.                   q = ttname;
  3123.                 else q = "";
  3124.             }
  3125.             p = name;
  3126.             if (success) {
  3127.                 strncpy(name,q,48);
  3128.                 while (*p) {            /* Uppercase it for emphasis. */
  3129.                     if (islower(*p))
  3130.                       *p = toupper(*p);
  3131.                     p++;
  3132.                 }
  3133.             } else
  3134.               name[0] = NUL ;
  3135.             os2settitle((char *) name, TRUE);
  3136.         }
  3137. #endif /* OS2 */
  3138.     }
  3139.     if (cx != XXLOOK) {
  3140.         if (success) {
  3141.             if (reliable == SET_AUTO) { /* It's not a reliable connection. */
  3142.                 reliable = SET_OFF;
  3143.                 debug(F101,"dodial reliable","",reliable);
  3144.             }
  3145.         } else {
  3146. #ifndef NOHINTS
  3147.             extern int hints;
  3148.             if (hints && !quiet && dialsta != 9) { /* 9 == User interrupted */
  3149.                 extern int dialmhu, dialhng, dialdpy;
  3150.                 extern char * dialmsg[];
  3151.                 printf("\n*************************\n");
  3152.                 printf("DIAL-class command failed.\n");
  3153.                 printf("Modem type:  %s\n", gmdmtyp());
  3154.                 printf("Device:      %s\n", ttname);
  3155.                 printf("Speed:       %ld\n", speed);
  3156.                 printf("Dial status: %d",dialsta);
  3157.                 if (dialsta < 35 && dialmsg[dialsta])
  3158.                   printf(" [%s]",dialmsg[dialsta]);
  3159.                 printf("\n");
  3160.                 if (dialsta == DIA_TIMO ||
  3161.                     dialsta == DIA_NRDY ||
  3162.                    (dialsta > 13 && dialsta != DIA_BUSY && dialsta != DIA_NOAN)
  3163.                     ) {
  3164.                     switch (dialsta) {
  3165.                       case DIA_TIMO:
  3166.                         printf(
  3167. " . SET DIAL TIMEOUT to a greater value and try again.\n"
  3168.                                );
  3169.                         break;
  3170.                       case DIA_NRSP:
  3171.                       case DIA_NRDY:
  3172.                       case DIA_NOIN:
  3173.                         printf(
  3174. " . Is the modem turned on?\n"
  3175.                                );
  3176.                         printf(
  3177. " . Are you using the right communication port?\n"
  3178.                                );
  3179.                         break;
  3180.                       case DIA_NODT:
  3181.                         printf(
  3182. " . Is the modem connected to the telephone line?\n"
  3183.                                );
  3184.                     }
  3185.                     if (mdmtyp == n_GENERIC) {
  3186.                         printf(
  3187. " . Please choose a specific modem type with SET MODEM TYPE and try again.\n"
  3188.                                );
  3189.                         printf(
  3190. "    SET MODEM TYPE ? to see the list of known modem types.\n"
  3191.                                );
  3192.                     } else {
  3193.                         printf(
  3194. " . Are you sure you have chosen the appropriate modem type?\n"
  3195.                                );
  3196.                     }
  3197.                     if (speed > 19200L) {
  3198.                         printf(
  3199. " . Maybe the interface speed (%ld) is too fast:\n", speed
  3200.                                );
  3201.                         printf(
  3202. "    SET SPEED to a lower speed and try again.\n"
  3203.                                );
  3204.                         printf(
  3205. "    SET SPEED ? to see the list of valid speeds.\n"
  3206.                                );
  3207.                     }
  3208.                     if (dialhng) {
  3209.                         if (dialmhu)
  3210.                           printf(
  3211. " . SET MODEM HANGUP-METHOD RS232 and try again.\n"
  3212.                                  );
  3213.                         else
  3214.                           printf(
  3215. " . SET MODEM HANGUP-METHOD MODEM-COMMAND and try again.\n"
  3216.                                  );
  3217.                         printf(
  3218. " . If that doesn't work, try again with SET DIAL HANGUP OFF.\n"
  3219.                                );
  3220.                     } else {
  3221.                         printf(
  3222. " . Give a HANGUP or SET DIAL HANGUP ON command and try again.\n"
  3223.                                );
  3224.                     }
  3225.                     if (!dialdpy)
  3226.                       printf(
  3227. " . Use SET DIAL DISPLAY ON to watch the dialog between Kermit and modem.\n"
  3228.                              );
  3229.                 }
  3230. #ifndef NOSHOW
  3231.                 printf(
  3232. " . SHOW COMMUNICATIONS, SHOW MODEM, SHOW DIAL to see current settings.\n"
  3233.                        );
  3234. #endif /* NOSHOW */
  3235.  
  3236. #ifndef NOHELP
  3237.                 printf(
  3238. " . HELP SET MODEM, HELP SET DIAL, and HELP DIAL for more information.\n"
  3239.                        );
  3240. #endif /* NOHELP */
  3241.                 printf("(Use SET HINTS OFF to suppress future hints.)\n");
  3242.                 printf("*************************\n\n");
  3243.             }
  3244. #endif /* NOHINTS */
  3245.         }
  3246.     }
  3247.     return(success);
  3248. }
  3249. #endif /* NODIAL */
  3250.  
  3251. /*  D O T Y P E  --  Type (display) a file with various options...  */
  3252.  
  3253. #ifdef BIGBUFOK
  3254. #define TYPBUFL 16384
  3255. #else
  3256. #define TYPBUFL 256
  3257. #endif /* BIGBUFOK */
  3258.  
  3259. int typ_lines = 0;                      /* \v(ty_ln) */
  3260. int typ_mtchs = 0;                      /* \v(ty_lm) */
  3261. static int typ_int = 0;                 /* Flag if TYPE interrupted */
  3262.  
  3263. #ifdef UNICODE
  3264. extern int fcharset, fileorder, byteorder, ucsorder;
  3265. #define TYPXBUFL TYPBUFL+TYPBUFL+TYPBUFL+4
  3266. static char * mp = NULL;
  3267. static char * mbuf = NULL;
  3268. static long xn = 0L;
  3269.  
  3270. static int
  3271. #ifdef CK_ANSIC
  3272. storechar(char c)
  3273. #else
  3274. storechar(c) char c;
  3275. #endif /* CK_ANSIC */
  3276. {
  3277.     if (!mp) return(-1);
  3278.     if (++xn > TYPXBUFL)
  3279.       return(-1);
  3280.     debug(F111,"storechar xn",ckitoa((int)c),xn);
  3281.     *mp++ = c;
  3282.     return(0);
  3283. }
  3284. #endif /* UNICODE */
  3285.  
  3286. static FILE * ofp = NULL;               /* For /OUTPUT: file */
  3287.  
  3288. static int
  3289. typeline(buf,len,outcs,ofp) char * buf; int len, outcs; FILE * ofp; {
  3290.     register int i;
  3291.  
  3292.     debug(F011,"typeline buf",buf,len);
  3293.     /* debug(F101,"typeline outcs","",outcs); */
  3294.  
  3295. #ifdef OS2
  3296. #ifndef NOLOCAL
  3297. #ifdef UNICODE
  3298.     /* In K95 only, the buffer is guaranteed to be in UCS-2 if outcs >= 0. */
  3299.     /* Len is its length in bytes.  There is no line terminator. */
  3300.     /* outcs is the file character-set number (FC_xxx) of the target set */
  3301.     /* that was requested by the user. */
  3302.     if (!inserver && !k95stdout) {
  3303.         extern int wherex[], wherey[];
  3304.         extern unsigned char colorcmd;
  3305.  
  3306.         VscrnWrtUCS2StrAtt( VCMD, (unsigned short *)buf, len/2,
  3307.                            wherey[VCMD], wherex[VCMD], &colorcmd);
  3308.         printf("\r\n");
  3309.         return(0);
  3310.     }
  3311. #endif /* UNICODE */
  3312. #endif /* NOLOCAL */
  3313. #endif /* OS2 */
  3314.  
  3315. /* In Unix, VMS, etc, the line has already been converted to the desired  */
  3316. /* character-set, if one was given.  OR... on all platforms, including in */
  3317. /* K95, we don't know the character set.  In either case we dump the line */
  3318. /* byte by byte in case it contains NULs (printf() would truncate). */
  3319.  
  3320. #ifdef COMMENT
  3321.     for (i = 0; i < len; i++)
  3322.       putchar(buf[i]);
  3323. #else
  3324.     for (i = 0; i < len; i++) {
  3325.         if (ofp == stdout) {
  3326.             putchar(buf[i]);
  3327.         } else {
  3328.             putc(buf[i],ofp);
  3329.         }
  3330.     }
  3331. #endif /* COMMENT */
  3332.  
  3333. #ifdef IKSD
  3334.     if (inserver) {
  3335. #ifdef UNICODE
  3336.         if (outcs == FC_UCS2) {
  3337.             if (ofp == stdout) {
  3338.                 putchar(NUL);
  3339.             } else {
  3340.                 putc(NUL,ofp);
  3341.             }
  3342.         }
  3343. #endif /* UNICODE */
  3344.         if (ofp == stdout) {
  3345.             putchar('\r');
  3346.         } else {
  3347.             putc('\r',ofp);
  3348.         }
  3349.     }
  3350. #endif /* IKSD */
  3351. #ifdef UNICODE
  3352.     if (outcs == FC_UCS2) {
  3353.         if (ofp == stdout) {
  3354.             putchar(NUL);
  3355.         } else {
  3356.             putc(NUL,ofp);
  3357.         }
  3358.     }
  3359. #endif /* UNICODE */
  3360.     if (ofp == stdout) {
  3361.         putchar('\n');
  3362.     } else {
  3363.         putc('\n',ofp);
  3364.     }
  3365.     fflush(stdout);
  3366.     return(0);
  3367. }
  3368.  
  3369. static int                              /* Get translated line */
  3370. typegetline(incs, outcs, buf, n) int incs, outcs, n; char * buf; {
  3371.     int x = 0, c0, c1, len = 0, count = 0, eof = 0, xlate = 0;
  3372. #ifdef UNICODE
  3373.     int xxn = -1;
  3374.     int yyn = -9;
  3375.     xn = 0L;
  3376.  
  3377. #ifdef DEBUG
  3378.     if (deblog && typ_lines == 0) {
  3379.         debug(F101,"typegetline incs","",incs);
  3380.         debug(F101,"typegetline outcs","",outcs);
  3381.         debug(F101,"typegetline feol","",feol);
  3382.         debug(F101,"typegetline byteorder","",byteorder);
  3383.         debug(F101,"typegetline ucsorder ","",ucsorder);
  3384.         debug(F111,"typegetline fileorder","1",fileorder);
  3385.     }
  3386. #endif /* DEBUG */
  3387.  
  3388.     if (incs < 0)                       /* Shouldn't happen */
  3389.       return(-2);
  3390.  
  3391.     if (outcs == -1)                    /* Can happen */
  3392.       outcs = incs;
  3393.  
  3394.     if (incs != outcs || incs == FC_UCS2) { /* See if we should translate */
  3395.         xlate = 1;
  3396.         if (!mbuf) {                    /* Allocate buffer if not allocated */
  3397.             mbuf = (char *)malloc(TYPXBUFL+1); /* yet */
  3398.             if (!mbuf) {
  3399.                 printf("WARNING: Translation buffer allocation failure.\n");
  3400.                 printf("Translation will be skipped...\n");
  3401.                 xlate = 0;
  3402.             }
  3403.         }
  3404.     }
  3405.     if (xlate) {                        /* Translating... */
  3406.         mp = mbuf;                      /* Reset working buffer pointer */
  3407. /*
  3408.   Here we call xgnbyte() in a loop, having it return UCS-2 bytes.  In K95, we
  3409.   use UCS-2 directly.  Elsewhere, we feed the UCS-2 bytes into xpnbyte() to
  3410.   convert them to the desired target character set.  But since we are using
  3411.   UCS-2, we have several sources for confusion: (1) xgnbyte() might return in
  3412.   LE or BE byte order, with no explicit indication of what the order is; but
  3413.   (2) xpnbyte() wants BE; but (3) Windows wants LE.
  3414. */
  3415.         while (1) {
  3416.             if (typ_int)                /* Quit if interrupted */
  3417.               return(0);
  3418.             c0 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3419.             debug(F000,"typegetline c0","",c0);
  3420.             if (c0 < 0) {               /* EOF */
  3421.                 eof++;
  3422.                 break;
  3423.             }
  3424.             c1 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3425.             debug(F000,"typegetline c1","",c1);
  3426.             if (c1 < 0) {               /* EOF */
  3427.                 eof++;
  3428.                 break;
  3429.             }
  3430. #ifdef DEBUG
  3431.             if (deblog && typ_lines == 0) {
  3432.                 if (count == 0) /* Check fileorder after BOM */
  3433.                   debug(F111,"typegetline fileorder","2",fileorder);
  3434.             }
  3435. #endif /* DEBUG */
  3436.  
  3437. #ifdef COMMENT
  3438. /* Now we have the two UCS-2 bytes.  Which order are they in? */
  3439.  
  3440.             if (fileorder > 0) {        /* Little Endian */
  3441.                 int t;                  /* So swap them */
  3442.                 debug(F100,"typegetline swapping","",0);
  3443.                 t = c1;
  3444.                 c1 = c0;
  3445.                 c0 = t;
  3446.             }
  3447. #endif /* COMMENT */
  3448.             if (c0 == 0 && c1 == 0x0D)  /* Now see if we have EOL */
  3449.               yyn = xn;
  3450.  
  3451.             if (c0 == 0 && c1 == 0x0A)  /* Now see if we have EOL */
  3452.               xxn = xn;
  3453.  
  3454.             count++;                    /* Count byte */
  3455.  
  3456. /* Give the two bytes to xpnbyte() in BE order */
  3457.  
  3458.             if ((x = xpnbyte(c0,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3459.             if ((x = xpnbyte(c1,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3460.  
  3461.             if (xxn > -1) {             /* Have end of line? */
  3462.                 xn = xxn;
  3463.                 if (yyn == xxn - 2)     /* Adjust for CRLF */
  3464.                   xn = yyn;
  3465.                 break;                  /* And break out of loop. */
  3466.             }
  3467.         }
  3468.         mbuf[xn] = NUL;
  3469.         if (xn > n)                     /* Can truncate here... */
  3470.           xn = n;
  3471.         memcpy(buf,mbuf,xn);
  3472.         debug(F011,"typegetline xlate",buf,xn);
  3473.         return((eof && (xn == 0)) ? -1 : xn);
  3474.     }
  3475. #endif /* UNICODE */
  3476. #ifdef COMMENT
  3477.     /* We can't use this because, stupidly, zsinl() doesn't return a length. */
  3478.     /* It could be changed but then we'd have to change all ck?fio.c modules */
  3479.     x = zsinl(ZIFILE,buf,n);
  3480. #else
  3481.     /* So instead, we copy zsinl() to here... */
  3482.     /* But note: This does not necessarily handle UCS-2 alignment properly;  */
  3483.     /* that's what the code in the first section of this routine is for. */
  3484.     /* But it does tolerate files that contain NULs. */
  3485.     {
  3486.         int a;
  3487.         char *s;
  3488.  
  3489.         s = buf;
  3490.         a = -1;                         /* Current character, none yet. */
  3491.         debug(F101,"typegetline zsinl simulation","",n);
  3492.         while (n--) {                   /* Up to given length */
  3493. #ifdef COMMENT
  3494.             int old = 0;
  3495.             if (feol)                   /* Previous character */
  3496.               old = a;
  3497. #endif /* COMMENT */
  3498.             if (zchin(ZIFILE,&a) < 0) { /* Read a character from the file */
  3499.                 debug(F101,"typegetline zchin fail","",count);
  3500.                 if (count == 0)
  3501.                   x = -1;               /* EOF or other error */
  3502.                 break;
  3503.             } else
  3504.               count++;
  3505.             if (feol) {                 /* Single-character line terminator */
  3506.                 if (a == feol)
  3507.                   break;
  3508.             } else {                    /* CRLF line terminator */
  3509. #ifdef COMMENT
  3510. /* Debug log shows that in Windows, <CR><LF> is returned as <LF>. */
  3511. /* Apparently we're not reading the file in binary mode. */
  3512.  
  3513.                 if (a == '\015')        /* CR, get next character */
  3514.                   continue;
  3515.                 if (old == '\015') {    /* Previous character was CR */
  3516.                     if (a == '\012') {  /* This one is LF, so we have a line */
  3517.                         break;
  3518.                     } else {            /* Not LF, deposit CR */
  3519.                         *s++ = '\015';
  3520.                         n--;
  3521.                         len++;
  3522.                     }
  3523.                 }
  3524. #else
  3525.                 if (a == LF) {
  3526.                     if (s[len] == CR) { /* This probably won't happen */
  3527.                         s[len] = NUL;
  3528.                         s--;
  3529.                         len--;
  3530.                     }
  3531.                     break;
  3532.                 }
  3533. #endif /* COMMENT */
  3534.             }
  3535.             *s = a;                     /* Deposit character */
  3536.             s++;
  3537.             len++;
  3538.         }
  3539.         *s = '\0';                      /* Terminate the string */
  3540.     }
  3541. #endif /* COMMENT */
  3542.     return(x < 0 ? -1 : len);
  3543. }
  3544.  
  3545.  
  3546. #ifndef MAC
  3547. SIGTYP
  3548. #ifdef CK_ANSIC
  3549. tytrap(int foo)                         /* TYPE interrupt trap */
  3550. #else
  3551. tytrap(foo) int foo;
  3552. #endif /* CK_ANSIC */
  3553. /* tytrap */ {
  3554. #ifdef __EMX__
  3555.     signal(SIGINT, SIG_ACK);
  3556. #endif
  3557.     debug(F100,"type tytrap SIGINT","",0);
  3558.     typ_int = 1;                        /* (Need arg for ANSI C) */
  3559.     SIGRETURN;
  3560. }
  3561. #endif /* MAC */
  3562.  
  3563. int
  3564. dotype(file, paging, first, head, pat, width, prefix, incs, outcs, outfile, z)
  3565.     char * file, * pat, * prefix; int paging, first, head, width, incs, outcs;
  3566.     char * outfile; int z;
  3567. /* dotype */ {
  3568.     extern long ffc;
  3569.     char buf[TYPBUFL+2];
  3570.     char * s = NULL;
  3571.     int rc = 1, lines = 0, ucs2 = 0;
  3572.     char ** tail = NULL;
  3573.     int * tlen = NULL;
  3574.     int tailing = 0, counting = 0;
  3575.     int x, c, n, i, j, k = 0;
  3576.     int number = 0, save, len, pfxlen = 0, evalpfx = 1;
  3577. #ifdef UNICODE
  3578.     int ucsbom_sav;
  3579.     extern int ucsbom;
  3580. #endif /* UNICODE */
  3581.  
  3582. #ifndef MAC
  3583. #ifdef OS2
  3584. #ifdef NT
  3585.     SIGTYP (* oldsig)(int);             /* For saving old interrupt trap. */
  3586. #else /* NT */
  3587.     SIGTYP (* volatile oldsig)(int);
  3588. #endif /* NT */
  3589. #else /* OS2 */
  3590.     SIGTYP (* oldsig)();
  3591. #endif /* OS2 */
  3592. #endif /* MAC */
  3593.  
  3594.     if (!file) file = "";
  3595.     if (!*file) return(-2);
  3596.  
  3597.     if (ofp != stdout) {                /* In case of previous interruption */
  3598.         if (ofp) fclose(ofp);
  3599.         ofp = stdout;
  3600.     }
  3601.     if (!outfile) outfile = "";
  3602.     if (outfile[0]) {
  3603.         ofp = fopen(outfile,"w");       /* Open output file */
  3604.         if (!ofp) {
  3605.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  3606.             ofp = stdout;
  3607.             return(-9);
  3608.         }
  3609.     }
  3610.     number = z;
  3611.     if (number && prefix) prefix = NULL;
  3612.  
  3613. #ifdef UNICODE
  3614.     ucsbom_sav = ucsbom;                /* We are not creating a file */
  3615.     ucsbom = 0;                         /* Do not use BOM bytes */
  3616. #endif /* UNICODE */
  3617.  
  3618.     typ_int = 0;
  3619.  
  3620.     save = binary;                      /* Save file type */
  3621.  
  3622.     debug(F101,"dotype incs","",incs);
  3623.     debug(F101,"dotype outcs","",outcs);
  3624.  
  3625. #ifdef UNICODE
  3626.     debug(F111,"dotype fileorder","A",fileorder);
  3627. #ifdef OS2
  3628.     if (!inserver && !k95stdout)
  3629.       outcs = FC_UCS2;
  3630. #endif /* OS2 */
  3631.  
  3632.     if (outcs == FC_UCS2)               /* Output is UCS-2? */
  3633.       ucs2 = 1;
  3634.     if (fileorder < 0)
  3635.       fileorder = ucsorder;
  3636.     debug(F111,"dotype fileorder","B",fileorder);
  3637. #endif /* UNICODE */
  3638.  
  3639. #ifdef CK_TTGWSIZ
  3640. #ifdef OS2
  3641.     ttgcwsz();
  3642. #else /* OS2 */
  3643.     /* Check whether window size changed */
  3644.     if (ttgwsiz() > 0) {
  3645.         if (tt_rows > 0 && tt_cols > 0) {
  3646.             cmd_rows = tt_rows;
  3647.             cmd_cols = tt_cols;
  3648.             debug(F101,"dotype cmd_rows","",cmd_rows);
  3649.             debug(F101,"dotype cmd_cols","",cmd_cols);
  3650.         }
  3651.     }
  3652. #endif /* OS2 */
  3653. #endif /* CK_TTGWSIZ */
  3654.  
  3655.     if (prefix)
  3656.       pfxlen = strlen(prefix);
  3657.  
  3658.     if (paging < 0) {                   /* Count only, don't print */
  3659.         counting = 1;
  3660.         prefix = NULL;
  3661.         width = 0;
  3662.         paging = 0;
  3663.     }
  3664.     if (ucs2)                           /* Crude... */
  3665.       width *= 2;
  3666.  
  3667. #ifdef OS2
  3668.     if (*file) {
  3669.         ckstrncpy(buf, file, TYPBUFL);  /* Change / to \. */
  3670.         p = buf;
  3671.         while (*p) {
  3672.             if (*p == '/') *p = '\\';
  3673.             p++;
  3674.         }
  3675.         file = buf;
  3676.     } else {
  3677.         rc = 0;
  3678.         goto xdotype;
  3679.     }
  3680. #endif /* OS2 */
  3681.  
  3682.     if (zchki(file) == -2) {            /* It's a directory */
  3683.         debug(F111,"dotype zchki failure",file,-2);
  3684.         if (xcmdsrc == 0) {
  3685.             printf("?Not a regular file: \"%s\"\n",file);
  3686.             rc = -9;
  3687.         } else
  3688.           rc = 0;
  3689.         goto xdotype;
  3690.     }
  3691.     if (!zopeni(ZIFILE, file)) {        /* Not a directory, open it */
  3692.         debug(F111,"dotype zopeni failure",file,0);
  3693.         if (xcmdsrc == 0) {
  3694.             printf("?Can't open file: \"%s\"\n",file);
  3695.             rc = -9;
  3696.         } else
  3697.           rc = 0;
  3698.         goto xdotype;
  3699.     }
  3700.  
  3701. #ifndef AMIGA
  3702. #ifndef MAC
  3703.     errno = 0;
  3704.     oldsig = signal(SIGINT, tytrap);    /* Save current interrupt trap. */
  3705.     debug(F111,"type SIGINT trap set",ckitoa(errno),oldsig);
  3706. #endif /* MAC */
  3707. #endif /* AMIGA */
  3708.  
  3709.     if (paging > -1)                    /* More-prompting */
  3710.       xaskmore = paging;
  3711.  
  3712.     binary = 0;
  3713.  
  3714.     if (head < 0) {                     /* "tail" was requested */
  3715.         tailing = 1;                    /* Set flag */
  3716.         head = 0 - head;                /* Get absolute number of lines */
  3717.         if (!counting) {
  3718.             tail = (char **) malloc(head * sizeof(char *)); /* Allocate list */
  3719.             if (!tail) {
  3720.                 printf("?Memory allocation failure\n");
  3721.                 goto xdotype;
  3722.  
  3723.             }
  3724.             tlen = (int *) malloc(head * sizeof(int));
  3725.             if (!tlen) {
  3726.                 printf("?Memory allocation failure\n");
  3727.                 goto xdotype;
  3728.  
  3729.             }
  3730.             for (i = 0; i < head; i++) { /* Initialize each pointer in list. */
  3731.                 tail[i] = NULL;
  3732.                 tlen[i] = 0;
  3733.             }
  3734.         }
  3735.     }
  3736.     typ_lines = 0;
  3737.     typ_mtchs = 0;
  3738.  
  3739. #ifdef UNICODE
  3740.     if (outcs > -1 && (incs != outcs || incs == FC_UCS2)) { /* Translating? */
  3741.         ffc = 0L;
  3742.         initxlate(incs,outcs);          /* Set up translation functions */
  3743.     } else
  3744. #endif /* UNICODE */
  3745.       outcs = -1;                       /* Means we don't know the charset */
  3746.  
  3747.     debug(F101,"dotype ffc","",ffc);
  3748.     debug(F101,"dotype outcs 2","",outcs);
  3749. #ifdef UNICODE
  3750.     debug(F111,"dotype fileorder","C",fileorder);
  3751. #endif /* UNICODE */
  3752.  
  3753.     /* Allow the buffer to contain NULs */
  3754.  
  3755.     for (n = first;
  3756.          (len = typegetline(incs,outcs,buf,TYPBUFL)) > -1;
  3757.          lines++
  3758.          ) {
  3759.         debug(F011,"dotype line",buf,len);
  3760. #ifndef MAC
  3761.         if (typ_int) {                  /* Interrupted? */
  3762.             typ_int = 0;
  3763.             debug(F101,"type interrupted line","",lines);
  3764.             printf("^C...\n");          /* Print message */
  3765.             if (ofp != stdout) {        /* Close any output file */
  3766.                 if (ofp) fclose(ofp);
  3767.                 ofp = stdout;
  3768.             }
  3769.             goto xxdotype;
  3770.         }
  3771. #endif /* MAC */
  3772.         typ_lines++;                    /* For \v(ty_ln) */
  3773.         if (pat)                        /* Matching? */
  3774.           if (!ckmatch(pat,buf,1,1+4))  /* Line matches pattern? */
  3775.             continue;                   /* No, skip it */
  3776.         typ_mtchs++;
  3777.  
  3778.         if (head > 0 && !tailing && lines == head) /* Handle /HEAD:n */
  3779.           break;
  3780.  
  3781.         buf[TYPBUFL+1] = NUL;           /* Just in case... */
  3782.         if (prefix) {                   /* Add specified prefix to each line */
  3783.             char pbuf[64];
  3784.             char * pp;
  3785.             pp = prefix;
  3786. #ifndef NOSPL
  3787.             if (evalpfx) {              /* Prefix is a variable? */
  3788.                 int n = 63;             /* Maybe - evaluate it and see */
  3789.                 char * p = pbuf;
  3790.                 zzstring(prefix,&p,&n); /* If there is no change */
  3791.                 if (!strcmp(prefix,pbuf)) { /* it's not a variable */
  3792.                     evalpfx = 0;        /* So don't do this again. */
  3793.                 } else {                /* It was a variable */
  3794.                     pp = pbuf;          /* So substitute its value */
  3795.                     pfxlen = 63 - n;    /* and get its new length */
  3796.                 }
  3797.             }
  3798. #endif /* NOSPL */
  3799.             if (len + pfxlen + 2 < TYPBUFL) {
  3800.                 /* Shift right to make room for prefix */
  3801.                 memcpy((char *)line+pfxlen,(char *)buf,len);
  3802.                 lset((char *)line,pp,pfxlen,SP);
  3803.                 debug(F110,"dotype prefix",line,pfxlen);
  3804.                 len += pfxlen;
  3805.                 memcpy((char *)buf,(char *)line,len);
  3806.             }
  3807.         } else if (number) {            /* Line numbers */
  3808.             int x;
  3809.             sprintf(line,"%4d. ",typ_lines);
  3810.             x = strlen(line);
  3811.             len += x;
  3812.             if (len < LINBUFSIZ) {
  3813.                 memcpy((char *)&line[x],(char *)buf,len);
  3814.                 memcpy((char *)buf,(char *)line,len);
  3815.             }
  3816.         }
  3817.         if (width > 0 && width <= TYPBUFL) { /* Truncate at given width. */
  3818.             char * obuf = line;         /* But to do that first we must */
  3819.             int i,k,z;                  /* expand tabs; assume every 8 cols. */
  3820.             line[0] = NUL;
  3821.             for (i = 0, k = 0; i < width; k++) { /* Character loop... */
  3822.                 if (!buf[k])            /* No more chars in this line, done. */
  3823.                   break;
  3824.                 if (buf[k] != '\t') {   /* If it's not a tab */
  3825.                     if (i >= LINBUFSIZ) /* Check for overflow */
  3826.                       break;
  3827.                     obuf[i++] = buf[k]; /* and then deposit it. */
  3828.                     obuf[i] = NUL;      /* Keep it null-terminated */
  3829.                     continue;
  3830.                 }
  3831.                 z = 8 - (i % 8);        /* It's a tab, expand it. */
  3832.                 if (z == 0) z = 8;
  3833.                 for (j = 0; j < z && i < LINBUFSIZ; j++) {
  3834. #ifdef UNICODE
  3835.                     if (ucs2 && !ucsorder)
  3836.                       obuf[i++] = NUL;
  3837. #endif /* UNICODE */
  3838.                     obuf[i++] = ' ';
  3839. #ifdef UNICODE
  3840.                     if (ucs2 && ucsorder)
  3841.                       obuf[i++] = NUL;
  3842. #endif /* UNICODE */
  3843.                 }
  3844.                 obuf[i++] = NUL;
  3845.                 obuf[i] = NUL;
  3846.             }
  3847.             obuf[width] = NUL;          /* Now truncate at given width. */
  3848. #ifdef COMMENT
  3849.             /* This doesn't work for UCS-2 because it contains NULs */
  3850.             ckstrncpy(buf,obuf,TYPBUFL); /* and copy it back (again?) */
  3851. #else
  3852.             memcpy((char *)buf,(char *)obuf,i); /* Copy it back */
  3853. #endif /* COMMENT */
  3854.             len = (i > width) ? width : i; /* Spare us another strlen()... */
  3855.         }
  3856.         if (tailing) {                  /* If /TAIL:n... */
  3857.             k = lines % head;           /* save this line in circular buffer */
  3858.             if (!counting) {
  3859.                 if (tail[k]) free(tail[k]);
  3860.                 tail[k] = malloc(len+2);
  3861.                 if (!tail[k]) {
  3862.                     printf("?Memory allocation failure\n");
  3863.                     goto xdotype;
  3864.                 }
  3865.                 memcpy(tail[k],buf,len);
  3866.                 tlen[k] = len;
  3867.                 continue;
  3868.             }
  3869.         }
  3870.         if (counting)                   /* If only counting */
  3871.           continue;                     /* we're done with this line */
  3872.  
  3873.         if (paging) {                   /* Displaying this line... */
  3874.             int u;
  3875.             u = len;                    /* Length in BYTES */
  3876.             if (ucs2)                   /* If outputting in UCS-2 */
  3877.               u /= 2;                   /* convert length to CHARACTERS */
  3878.             x = (u / cmd_cols) + 1;     /* Crudely allow for wrap */
  3879.             if (cmd_rows > 0 && cmd_cols > 0)
  3880.               n += x;                   /* This assumes terminal will wrap */
  3881.         }
  3882.         typeline(buf,len,outcs,ofp);    /* Print line, length based */
  3883. #ifdef CK_TTGWSIZ
  3884.         debug(F101,"dotype n","",n);
  3885.         if (paging > 0 && ofp == stdout) { /* Pause at end of screen */
  3886.             if (cmd_rows > 0 && cmd_cols > 0) {
  3887.                 if (n > cmd_rows - 3) {
  3888.                     if (!askmore())
  3889.                       goto xdotype;
  3890.                     else
  3891.                       n = 0;
  3892.                 }
  3893.             }
  3894.         }
  3895. #endif /* CK_TTGWSIZ */
  3896.     }
  3897.  
  3898.   xdotype:
  3899.     if (counting) {
  3900.         fprintf(ofp,
  3901.                 "%s: %d line%s\n",file,typ_lines,typ_lines == 1 ? "" : "s");
  3902.         if (pat)
  3903.           fprintf(ofp,
  3904.                   "%s: %d match%s\n",pat,typ_mtchs,typ_mtchs == 1 ? "" : "es");
  3905.         goto xxdotype;
  3906.     }
  3907.     if (tailing && tail) {              /* Typing tail of file? */
  3908.         if (lines < head) {             /* Yes, show the lines we saved */
  3909.             k = 0;                      /* Show all lines */
  3910.         } else {                        /* More lines than tail number */
  3911.             lines = k;                  /* Last line to show */
  3912.             k++;                        /* First line to show */
  3913.             if (k >= head)
  3914.               k = 0;
  3915.         }
  3916.         n = first;                      /* Output line counter */
  3917.         for (i = k ;; i++) {            /* Loop thru circular buffer */
  3918. #ifndef MAC
  3919.             if (typ_int) {              /* Interrupted? */
  3920.                 printf("^C...\n");      /* Print message */
  3921.                 goto xxdotype;
  3922.             }
  3923. #endif /* MAC */
  3924.             j = i % head;               /* Index of this line */
  3925.             s = tail[j];                /* Point to line to display */
  3926.             if (!s)                     /* (shouldn't happen...) */
  3927.               break;
  3928.             if (paging) {               /* Crudely allow for line wrap */
  3929.                 x = tlen[j];
  3930.                 if (ucs2) x /= 2;
  3931.                 x = x / cmd_cols + 1;
  3932.                 if (cmd_rows > 0 && cmd_cols > 0)
  3933.                   n += x;
  3934.             }
  3935.             typeline(s,tlen[j],outcs,ofp); /* Display this line */
  3936.             if (paging && ofp == stdout) { /* Pause at end of screen */
  3937.                 if (cmd_rows > 0 && cmd_cols > 0) {
  3938.                     if (n > cmd_rows - 3) {
  3939.                         if (!askmore())
  3940.                           break;
  3941.                         else
  3942.                           n = 0;
  3943.                     }
  3944.                 }
  3945.             }
  3946.             tail[j] = NULL;
  3947.             free(s);                    /* Free the line */
  3948.             if (i % head == lines)      /* When to stop */
  3949.               break;
  3950.         }
  3951.         free((char *)tail);             /* Free the list */
  3952.         tail = NULL;
  3953.         if (tlen) free((char *)tlen);
  3954.         tlen = NULL;
  3955.     }
  3956.  
  3957. /* Come here when finished or on SIGINT */
  3958.  
  3959.   xxdotype:
  3960. #ifndef AMIGA
  3961. #ifndef MAC
  3962.     signal(SIGINT,oldsig);              /* Put old signal action back. */
  3963. #endif /* MAC */
  3964. #endif /* AMIGA */
  3965.     if (tailing && tail) {
  3966.         for (i = 0; i < head; i++) {    /* Free each line. */
  3967.             if (tail[i])
  3968.               free(tail[i]);
  3969.         }
  3970.         free((char *)tail);             /* Free list pointer */
  3971.         if (tlen)
  3972.           free((char *)tlen);
  3973.     }
  3974.     x = zclose(ZIFILE);                 /* Done, close the input file */
  3975.     if (ofp != stdout) {                /* Close any output file */
  3976.         if (ofp) fclose(ofp);
  3977.         ofp = stdout;
  3978.     }
  3979.     binary = save;                      /* Restore text/binary mode */
  3980. #ifdef UNICODE
  3981.     ucsbom = ucsbom_sav;                /* Restore BOM usage */
  3982. #endif /* UNICODE */
  3983.     return(rc);
  3984. }
  3985.  
  3986. /* GREP command */
  3987.  
  3988. #define GREP_CASE  0                    /* /CASE */
  3989. #define GREP_COUN  1                    /* /COUNT */
  3990. #define GREP_DOTF  2                    /* /DOTFILES */
  3991. #define GREP_NAME  3                    /* /NAMEONLY */
  3992. #define GREP_NOBK  4                    /* /NOBACKUP */
  3993. #define GREP_NODO  5                    /* /NODOTFILES */
  3994. #define GREP_NOLI  6                    /* /NOLIST = /QUIET */
  3995. #define GREP_NOMA  7                    /* /INVERT = /NOMATCH */
  3996. #define GREP_NOPA  8                    /* /NOPAGE */
  3997. #define GREP_NUMS  9                    /* /LINENUMBERS */
  3998. #define GREP_PAGE 10                    /* /PAGE */
  3999. #define GREP_RECU 11                    /* /RECURSIVE */
  4000. #define GREP_TYPE 12                    /* /TYPE: */
  4001. #define GREP_OUTP 13                    /* /OUTPUTFILE: */
  4002.  
  4003. static struct keytab greptab[] = {
  4004.     { "/count",        GREP_COUN, CM_ARG },
  4005.     { "/dotfiles",     GREP_DOTF, 0 },
  4006.     { "/nameonly",     GREP_NAME, 0 },
  4007.     { "/nobackupfiles",GREP_NOBK, 0 },
  4008.     { "/nocase",       GREP_CASE, 0 },
  4009.     { "/nodotfiles",   GREP_NODO, 0 },
  4010.     { "/nolist",       GREP_NOLI, 0 },
  4011.     { "/nomatch",      GREP_NOMA, 0 },
  4012.     { "/nopage",       GREP_NOPA, 0 },
  4013.     { "/output",       GREP_OUTP, CM_ARG },
  4014.     { "/linenumbers",  GREP_NUMS, 0 },
  4015.     { "/page",         GREP_PAGE, 0 },
  4016.     { "/quiet",        GREP_NOLI, CM_INV },
  4017. #ifdef RECURSIVE
  4018.     { "/recursive",    GREP_RECU, 0 },
  4019. #endif /* RECURSIVE */
  4020.     { "/type",         GREP_TYPE, CM_ARG },
  4021.     { "", 0, 0 }
  4022. };
  4023. static int ngreptab =  sizeof(greptab)/sizeof(struct keytab)-1;
  4024.  
  4025. int
  4026. dogrep() {
  4027.     int match, x, y, fc, getval, mc = 0, count = 0, bigcount = 0;
  4028.     int fline = 0, sline = 0, wild = 0, len = 0;
  4029.     int xmode = -1, scan = 0;
  4030.     char c, name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p, *s, *cv = NULL;
  4031.     FILE * fp = NULL;
  4032.  
  4033.     int                                 /* Switch values and defaults */
  4034.       gr_coun = 0,
  4035.       gr_name = 0,
  4036.       gr_nobk = 0,
  4037.       gr_case = 1,
  4038.       gr_noli = 0,
  4039.       gr_noma = 0,
  4040.       gr_nums = 0,
  4041.       gr_page = xaskmore;
  4042.  
  4043.     struct FDB sw, fl;
  4044.  
  4045.     g_matchdot = matchdot;              /* Save global matchdot setting */
  4046.     outfile[0] = NUL;
  4047.  
  4048.     if (ofp != stdout) {                /* In case of previous interruption */
  4049.         if (ofp) fclose(ofp);
  4050.         ofp = stdout;
  4051.     }
  4052.     cmfdbi(&sw,                         /* First FDB - command switches */
  4053.            _CMKEY,                      /* fcode */
  4054.            "String or pattern to search for, or switch",
  4055.            "",                          /* default */
  4056.            "",                          /* addtl string data */
  4057.            ngreptab,                    /* addtl numeric data 1: tbl size */
  4058.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4059.            xxstring,                    /* Processing function */
  4060.            greptab,                     /* Keyword table */
  4061.            &fl                          /* Pointer to next FDB */
  4062.            );
  4063.     cmfdbi(&fl,                         /* Anything that doesn't match */
  4064.            _CMFLD,                      /* fcode */
  4065.            "",                          /* hlpmsg */
  4066.            "",                          /* default */
  4067.            "",                          /* addtl string data */
  4068.            0,                           /* addtl numeric data 1 */
  4069.            0,                           /* addtl numeric data 2 */
  4070.            NULL,
  4071.            NULL,
  4072.            NULL
  4073.            );
  4074.     while (1) {                         /* Parse 0 or more switches */
  4075.         x = cmfdb(&sw);                 /* Parse something */
  4076.         if (x < 0)
  4077.           return(x);
  4078.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  4079.           break;
  4080.         c = cmgbrk();
  4081.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4082.             printf("?This switch does not take an argument\n");
  4083.             return(-9);
  4084.         }
  4085.         if ((cmresult.nresult != GREP_COUN) && !getval &&
  4086.             (cmgkwflgs() & CM_ARG)) {
  4087.             printf("?This switch requires an argument\n");
  4088.             return(-9);
  4089.         }
  4090.         switch (cmresult.nresult) {
  4091.           case GREP_COUN: {
  4092.               gr_coun++;
  4093.               gr_noli=0;
  4094.               if (getval) {
  4095.                   if ((x = cmfld("Variable for result","",&s,NULL)) < 0)
  4096.                     return(x);
  4097.                   makestr(&cv,s);
  4098.               }
  4099.               break;
  4100.           }
  4101.           case GREP_CASE: gr_case=0; break;
  4102.           case GREP_NAME: gr_name++; gr_noli=0; break;
  4103.           case GREP_NOBK: gr_nobk++; break;
  4104.           case GREP_NOLI: gr_noli++; gr_coun=0; gr_name=0; gr_nums=0; break;
  4105.           case GREP_NOMA: gr_noma++; break;
  4106.           case GREP_NOPA: gr_page=0; break;
  4107.           case GREP_NUMS: gr_nums++; gr_noli=0; break;
  4108.           case GREP_PAGE: gr_page++; gr_noli=0; break;
  4109.           case GREP_NODO:
  4110.             matchdot = 0;
  4111.             break;
  4112.           case GREP_DOTF:
  4113.             matchdot = 1;
  4114.             break;
  4115. #ifdef RECURSIVE
  4116.           case GREP_RECU:
  4117.             recursive = 1;
  4118.             break;
  4119. #endif /* RECURSIVE */
  4120.           case GREP_TYPE: {
  4121.               extern struct keytab txtbin[];
  4122.               if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  4123.                 return(x);
  4124.               if (x == 2) {             /* ALL */
  4125.                   xmode = -1;
  4126.               } else {                  /* TEXT or BINARY only */
  4127.                   xmode = x;
  4128.                   scan = 1;
  4129.               }
  4130.               break;
  4131.           }
  4132.           case GREP_OUTP:               /* Send output to file */
  4133.             if ((x = cmofi("File for GREP'd lines","",&s,xxstring)) < 0)
  4134.               return(x);
  4135.             ckstrncpy(outfile,s,CKMAXPATH);
  4136.             break;
  4137.         }
  4138.     }
  4139.     if (outfile[0]) {
  4140.         ofp = fopen(outfile,"w");       /* Open output file */
  4141.         if (!ofp) {
  4142.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  4143.             ofp = stdout;
  4144.             return(-9);
  4145.         }
  4146.         gr_page = 0;
  4147.     }
  4148.     s = cmresult.sresult;
  4149.     s = brstrip(s);                     /* Strip braces from pattern */
  4150.     if (!*s) {
  4151.         printf("?Pattern required\n");
  4152.         return(-9);
  4153.     }
  4154.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);      /* Save pattern */
  4155.     if ((x = cmifi("File(s) to search","",&s,&wild,xxstring)) < 0) {
  4156.         if (x == -3) {
  4157.             printf("?File specification required\n");
  4158.             x = -9;
  4159.         }
  4160.         return(x);
  4161.     }
  4162.     s = brstrip(s);                     /* Strip braces from filename */
  4163. #ifndef ZXREWIND
  4164.     ckstrncpy(line,s,LINBUFSIZ);
  4165. #endif /* ZXREWIND */
  4166.     if ((y = cmcfm()) < 0)
  4167.       return(y);
  4168.  
  4169.     if (gr_page > -1)
  4170.       xaskmore = gr_page;               /* Paging... */
  4171.  
  4172.     p = tmpbuf;                         /* Point to pattern */
  4173. #ifdef COMMENT
  4174. /* Now this is done in ckmatch */
  4175.     if (*p == '^') {                    /* '^' anchors pattern to beginning */
  4176.         p++;
  4177.     } else if (*p != '*') {             /* Otherwise prepend implied '*' */
  4178.         tmpbuf[0] = '*';
  4179.         p = tmpbuf;
  4180.     }
  4181.     x = strlen(p);                      /* Get length of result */
  4182.     if (x > 0 && x < TMPBUFSIZ) {       /* '$' at end anchors pattern to end */
  4183.         if (p[x-1] == '$') {
  4184.             p[x-1] = NUL;
  4185.         } else if (p[x-1] != '*') {
  4186.             p[x] = '*';
  4187.             p[x+1] = NUL;
  4188.         }
  4189.     }
  4190. #endif /* COMMENT */
  4191.     debug(F111,"grep pat",p,x);
  4192.  
  4193. #ifdef ZXREWIND
  4194.     fc = zxrewind();                    /* Rewind the file list */
  4195. #else
  4196.     {
  4197.         int flags = ZX_FILONLY;         /* Expand file list */
  4198.         if (matchdot)  flags |= ZX_MATCHDOT;
  4199.         if (recursive) flags |= ZX_RECURSE;
  4200.         fc = nzxpand(line,flags);
  4201.     }
  4202. #endif /* ZXREWIND */
  4203. #ifdef UNIX
  4204.     sh_sort(mtchs,NULL,fc,0,0,filecase);
  4205. #endif /* UNIX */
  4206.  
  4207.     debug(F101,"grep cmd_rows","",cmd_rows);
  4208.     debug(F101,"grep cmd_cols","",cmd_cols);
  4209.  
  4210.     while (1) {                         /* Loop for each file */
  4211.         znext(name);                    /* Get next file */
  4212.         if (!name[0])                   /* No more, done */
  4213.           break;
  4214.         if (gr_nobk)                    /* Skipping backup files? */
  4215.           if (ckmatch("*.~[1-9]*~",name,1,1)) /* Backup file? */
  4216.             continue;                   /* Yes, skip */
  4217.         if (scan) {                     /* /TYPE: given? */
  4218.             switch (scanfile(name,&y,nscanfile)) { /* Yes, scan the file */
  4219.               case FT_BIN:
  4220.                 if (xmode != 1)
  4221.                   continue;
  4222.                 break;
  4223.               case FT_TEXT:
  4224.               case FT_7BIT:
  4225.               case FT_8BIT:
  4226. #ifdef UNICODE
  4227.               case FT_UTF8:
  4228.               case FT_UCS2:
  4229. #endif /* UNICODE */
  4230.                 if (xmode != 0)
  4231.                   continue;
  4232.             }
  4233.         }
  4234.         fp = fopen(name,"r");           /* Open */
  4235.         if (!fp)                        /* Can't */
  4236.           continue;                     /* Skip */
  4237.         count = 0;                      /* Match count, this file */
  4238.         fline = 0;                      /* Line count, this file */
  4239.         while (1) {                     /* Loop for each line */
  4240.             if (fgets(line,LINBUFSIZ,fp) == NULL) { /* Get next line */
  4241.                 fclose(fp);
  4242.                 fp = NULL;
  4243.                 break;
  4244.             }
  4245.             fline++;                    /* Count this line */
  4246.             line[LINBUFSIZ] = NUL;      /* Make sure it's terminated */
  4247.             len = (int)strlen(line);    /* Get length */
  4248.             while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
  4249.               line[--len] = NUL;        /* Chop off terminators */
  4250.             match = ckmatch(p,line,gr_case,1+4); /* Match against pattern */
  4251.             if (gr_noma)                /* Invert match sense if requested */
  4252.               match = !match;
  4253.             if (match) {                /* Have a matching line */
  4254.                 mc++;                   /* Total match count */
  4255.                 count++;                /* Match count this file */
  4256.                 if (gr_name) {          /* Don't care how many lines match */
  4257.                     fclose(fp);         /* Close the file */
  4258.                     fp = NULL;          /* and quit the line-reading loop. */
  4259.                     break;
  4260.                 }
  4261.                 if (gr_coun || gr_noli) /* Not listing each line */
  4262.                   continue;             /* so don't print anything now. */
  4263.                 if (wild) {             /* If searching multiple files */
  4264.                     fprintf(ofp,"%s:",name); /* print filename. */
  4265.                     len += (int)strlen(name) + 1;
  4266.                 }
  4267.                 if (gr_nums) {          /* If line numbers wanted */
  4268.                     char nbuf[32];
  4269.                     len += ckmakmsg(nbuf,32,ckitoa(fline),":",NULL,NULL);
  4270.                     fprintf(ofp,"%s",nbuf);
  4271.                 }
  4272.                 if (cmd_rows > 0 && cmd_cols > 0)
  4273.                   sline += (len / cmd_cols) + 1;
  4274.                 fprintf(ofp,"%s\n",line); /* Print the line. */
  4275.                 if (sline > cmd_rows - 3) {
  4276.                     if (!askmore()) goto xgrep; else sline = 0;
  4277.                 }
  4278.             }
  4279.         }
  4280.         if (!gr_noli) {                 /* If not not listing... */
  4281.             x = 0;
  4282.             if (gr_coun) {              /* Show match count only */
  4283.                 fprintf(ofp,"%s:%d\n",name,count);
  4284.                 x++;
  4285.             } else if (gr_name && count > 0) { /* Show name only */
  4286.                 fprintf(ofp,"%s\n",name);
  4287.                 x++;
  4288.             }
  4289.             if (x > 0) {
  4290.                 if (++sline > cmd_rows - 3) {
  4291.                     if (!askmore()) goto xgrep; else sline = 0;
  4292.                 }
  4293.             }
  4294.         }
  4295.         bigcount += count;              /* Overall count */
  4296.     }
  4297.   xgrep:
  4298. #ifndef NOSPL
  4299.     if (gr_coun && cv) {                /* /COUNT:blah */
  4300.         addmac(cv,ckitoa(bigcount));    /* set the variable */
  4301.         makestr(&cv,NULL);              /* free this */
  4302.     }
  4303. #endif /* NOSPL */
  4304.     if (fp) fclose(fp);                 /* close input file if still open */
  4305.     if (ofp != stdout) {                /* Close any output file */
  4306.         if (ofp) fclose(ofp);
  4307.         ofp = stdout;
  4308.     }
  4309.     return(success = mc ? 1 : 0);
  4310. }
  4311.  
  4312. /* System-independent directory */
  4313.  
  4314. static char ** dirlist = NULL;
  4315. static int ndirlist = 0;
  4316.  
  4317. static VOID
  4318. freedirlist() {
  4319.     if (dirlist) {
  4320.         int i;
  4321.         for (i = 0; i < ndirlist; i++) {
  4322.             if (dirlist[i])
  4323.               free(dirlist[i]);
  4324.         }
  4325.         free((char *)dirlist);
  4326.         dirlist = NULL;
  4327.     }
  4328.     ndirlist = 0;
  4329. }
  4330.  
  4331. static struct keytab dirswtab[] = {     /* DIRECTORY command switches */
  4332.     { "/after",       DIR_AFT, CM_ARG },
  4333.     { "/all",         DIR_ALL, 0 },
  4334. #ifndef NOSPL
  4335.     { "/array",       DIR_ARR, CM_ARG },
  4336. #endif /* NOSPL */
  4337.     { "/ascending",   DIR_ASC, 0 },
  4338.     { "/backup",      DIR_BUP, 0 },
  4339.     { "/before",      DIR_BEF, CM_ARG },
  4340.     { "/brief",       DIR_BRF, 0 },
  4341.     { "/descending",  DIR_DSC, CM_INV },
  4342.     { "/directories", DIR_DIR, 0 },
  4343.     { "/dotfiles",    DIR_DOT, 0 },
  4344.     { "/englishdate", DIR_DAT, 0 },
  4345.     { "/except",      DIR_EXC, CM_ARG },
  4346.     { "/files",       DIR_FIL, 0 },
  4347.     { "/heading",     DIR_HDG, 0 },
  4348.     { "/isodate",     DIR_ISO, 0 },
  4349.     { "/larger-than", DIR_LAR, CM_ARG },
  4350. #ifdef CKSYMLINK
  4351.     { "/followlinks", DIR_LNK, 0 },
  4352. #endif /* CKSYMLINK */
  4353.     { "/message",     DIR_MSG, CM_ARG },
  4354.     { "/nobackupfiles",DIR_NOB, 0 },
  4355.     { "/nodotfiles",  DIR_NOD, 0 },
  4356. #ifdef CKSYMLINK
  4357.     { "/nofollowlinks",DIR_NLK, 0 },
  4358. #endif /* CKSYMLINK */
  4359.     { "/noheading",   DIR_NOH, 0 },
  4360.     { "/nomessage",   DIR_NOM, 0 },
  4361. #ifdef CK_TTGWSIZ
  4362.     { "/nopage",      DIR_NOP, 0 },
  4363. #endif /* CK_TTGWSIZ */
  4364. #ifdef RECURSIVE
  4365.     { "/norecursive", DIR_NOR, 0 },
  4366. #else
  4367. #ifdef VMS
  4368.     { "/norecursive", DIR_NOR, 0 },
  4369. #else
  4370. #ifdef datageneral
  4371.     { "/norecursive", DIR_NOR, 0 },
  4372. #endif /* datageneral */
  4373. #endif /* VMS */
  4374. #endif /* RECURSIVE */
  4375.     { "/nosort",      DIR_NOS, 0 },
  4376.     { "/not-after",   DIR_NAF, CM_ARG },
  4377.     { "/not-before",  DIR_NBF, CM_ARG },
  4378.     { "/not-since",   DIR_NAF, CM_INV|CM_ARG },
  4379.     { "/noxfermode",  DIR_NOT, 0 },
  4380.     { "/output",      DIR_OUT, CM_ARG },
  4381. #ifdef CK_TTGWSIZ
  4382.     { "/page",        DIR_PAG, 0 },
  4383. #endif /* CK_TTGWSIZ */
  4384. #ifdef RECURSIVE
  4385.     { "/recursive",   DIR_REC, 0 },
  4386. #else
  4387. #ifdef VMS
  4388.     { "/recursive",   DIR_REC, 0 },
  4389. #else
  4390. #ifdef datageneral
  4391.     { "/recursive",   DIR_REC, 0 },
  4392. #endif /* datageneral */
  4393. #endif /* VMS */
  4394. #endif /* RECURSIVE */
  4395.     { "/reverse",     DIR_DSC, 0 },
  4396.     { "/since",       DIR_AFT, CM_ARG|CM_INV },
  4397.     { "/smaller-than",DIR_SMA, CM_ARG },
  4398.     { "/sort",        DIR_SRT, CM_ARG },
  4399.     { "/summary",     DIR_SUM, 0 },
  4400.     { "/type",        DIR_BIN, CM_ARG },
  4401.     { "/xfermode",    DIR_TYP, 0 },
  4402.     { "/verbose",     DIR_VRB, 0 },
  4403.     { "",0,0 }
  4404. };
  4405. static int ndirswtab = (sizeof(dirswtab) / sizeof(struct keytab)) - 1;
  4406.  
  4407. static struct keytab dirsort[] = {      /* DIRECTORY /SORT: options */
  4408.     { "date",         DIRS_DT, 0 },
  4409.     { "name",         DIRS_NM, 0 },
  4410.     { "size",         DIRS_SZ, 0 }
  4411. };
  4412. static int ndirsort = (sizeof(dirsort) / sizeof(struct keytab));
  4413.  
  4414. static int dir_date = -1;               /* Option defaults (-1 means none) */
  4415. static int dir_page = -1;
  4416. static int dir_verb =  1;
  4417. static int dir_msg  = -1;
  4418. #ifdef VMS
  4419. static int dir_sort = -1;               /* Names are already sorted in VMS */
  4420. static int dir_rvrs = -1;
  4421. #else
  4422. static int dir_sort =  1;               /* Sort by default */
  4423. static int dir_rvrs =  0;               /* Not in reverse */
  4424. #endif /* VMS */
  4425. static int dir_skey = DIRS_NM;          /* By name */
  4426. #ifdef RECURSIVE
  4427. static int dir_recu = -1;
  4428. #endif /* RECURSIVE */
  4429. static int dir_mode = -1;
  4430. static int dir_show = -1;               /* Show all files by default */
  4431. int dir_dots =  -1;            /* Except dot files */
  4432. int dir_back =  1;
  4433. int dir_head =  0;
  4434. static char * dirmsg = NULL;
  4435. static int dirmsglen = 0;
  4436.  
  4437. #ifndef NOSHOW
  4438. VOID
  4439. showdiropts() {
  4440.     int x = 0;
  4441.     extern int optlines;
  4442.     prtopt(&optlines,"DIRECTORY");
  4443.     if (dir_show > 0) {
  4444.         prtopt(&optlines,(dir_show == 1) ? "/FILES" :
  4445.                ((dir_show == 2) ? "/DIRECTORIES" : "/ALL"));
  4446.         x++;
  4447.     } else {
  4448.         prtopt(&optlines,"/ALL");
  4449.         x++;
  4450.     }
  4451.     if (dir_verb > -1) {
  4452.         prtopt(&optlines,dir_verb ? "/VERBOSE" : "/BRIEF");
  4453.         x++;
  4454.     }
  4455.     if (dir_page > -1) {
  4456.         prtopt(&optlines,dir_page ? "/PAGE" : "/NOPAGE");
  4457.         x++;
  4458.     }
  4459.     if (dir_date > -1) {
  4460.         prtopt(&optlines,dir_date ? "/ENGLISHDATE" : "/ISODATE");
  4461.         x++;
  4462.     }
  4463.     if (dir_dots > -1) {
  4464.         prtopt(&optlines,dir_dots ? "/DOTFILES" : "/NODOTFILES");
  4465.         x++;
  4466.     }
  4467.     if (dir_back > -1) {
  4468.         prtopt(&optlines,dir_back ? "/BACKUP" : "/NOBACKUP");
  4469.         x++;
  4470.     }
  4471.     if (dir_head > -1) {
  4472.         prtopt(&optlines,dir_head ? "/HEADING" : "/NOHEADING");
  4473.         x++;
  4474.     }
  4475. #ifdef RECURSIVE
  4476.     if (dir_recu > -1) {
  4477.         prtopt(&optlines,dir_recu ? "/RECURSIVE" : "/NORECURSIVE");
  4478.         x++;
  4479.     }
  4480. #endif /* RECURSIVE */
  4481.     if (dir_mode > -1) {
  4482.         prtopt(&optlines,dir_mode ? "/XFERMODE" : "/NOXFERMODE");
  4483.         x++;
  4484.     }
  4485.     if (dir_sort == 0) {
  4486.         x++;
  4487.         prtopt(&optlines,"/NOSORT ");
  4488.     } else if (dir_sort > 0) {
  4489.         x++;
  4490.         if (dir_skey == DIRS_NM) s = "/SORT:NAME";
  4491.         else if (dir_skey == DIRS_SZ) s = "/SORT:SIZE";
  4492.         else if (dir_skey == DIRS_DT) s = "/SORT:DATE";
  4493.         prtopt(&optlines,s);
  4494.     }
  4495.     if (dir_rvrs > -1) {
  4496.         prtopt(&optlines,dir_rvrs ? "/REVERSE" : "/ASCENDING");
  4497.         x++;
  4498.     }
  4499.     if (dir_msg > -1) {
  4500.         if (dir_msg == 0) {
  4501.             prtopt(&optlines,"/NOMESSAGE");
  4502.         } else {
  4503.             ckmakmsg(tmpbuf,TMPBUFSIZ,"/MESSAGE:{",dirmsg,"}",NULL);
  4504.             prtopt(&optlines,tmpbuf);
  4505.         }
  4506.         x++;
  4507.     }
  4508.     if (!x) prtopt(&optlines,"(no options set)");
  4509.     prtopt(&optlines,"");
  4510. }
  4511. #endif /* NOSHOW */
  4512.  
  4513. int
  4514. setdiropts() {                          /* Set DIRECTORY option defaults */
  4515.     int xb = -1, xv = -1, xp = -1, xd = -1, xh = -1, xf = -1;
  4516.     int xk = -1, xr = -1, xs = -1, xx = -1, xm = -1, xa = -1, xg = -1;
  4517.     int getval;
  4518.     char c;
  4519.     while (1) {
  4520.         if ((y = cmswi(dirswtab,ndirswtab,"Switch","",xxstring)) < 0) {
  4521.             if (y == -3)
  4522.               break;
  4523.             else
  4524.               return(y);
  4525.         }
  4526.         c = cmgbrk();
  4527.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4528.             printf("?This switch does not take an argument\n");
  4529.             return(-9);
  4530.         }
  4531.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4532.             printf("?This switch requires an argument\n");
  4533.             return(-9);
  4534.         }
  4535.         switch (y) {
  4536.           case DIR_BRF: xv = 0; break;
  4537.           case DIR_VRB: xv = 1; break;
  4538.           case DIR_PAG: xp = 1; break;
  4539.           case DIR_NOP: xp = 0; break;
  4540.           case DIR_ISO: xd = 0; break;
  4541.           case DIR_DAT: xd = 1; break;
  4542.           case DIR_HDG: xh = 1; break;
  4543.           case DIR_NOH: xh = 0; break;
  4544.           case DIR_DOT: xf = 1; break;
  4545.           case DIR_NOD: xf = 0; break;
  4546.           case DIR_ALL: xa = 3; break;
  4547.           case DIR_DIR: xa = 2; break;
  4548.           case DIR_FIL: xa = 1; break;
  4549.           case DIR_SRT:
  4550.             x = DIRS_NM;
  4551.             if (getval)
  4552.               if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4553.                 return(x);
  4554.             xk = x;
  4555.             xs = 1;
  4556.             break;
  4557.           case DIR_NOS: xs = 0; break;
  4558.           case DIR_ASC: xx = 0; break;
  4559.           case DIR_DSC: xx = 1; break;
  4560.           case DIR_REC: xr = 1; break;
  4561.           case DIR_NOR: xr = 0; break;
  4562.           case DIR_TYP: xm = 1; break;
  4563.           case DIR_NOT: xm = 0; break;
  4564.           case DIR_BUP: xb = 1; break;
  4565.           case DIR_NOB: xb = 0; break;
  4566.           case DIR_NOM: xg = 0; break;
  4567.           case DIR_MSG:
  4568.             if (getval)
  4569.               if ((x = cmfld("Message to append to each line",
  4570.                              "",
  4571.                              &s,
  4572.                              xxstring
  4573.                              )) < 0)
  4574.                 return(x);
  4575.             xg = 1;
  4576.             ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4577.             break;
  4578.           default:
  4579.             printf("?This option can not be set\n");
  4580.             return(-9);
  4581.         }
  4582.     }
  4583.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  4584.       return(x);
  4585.     if (xv > -1) dir_verb = xv;         /* Confirmed, save defaults */
  4586.     if (xp > -1) dir_page = xp;
  4587.     if (xd > -1) dir_date = xd;
  4588.     if (xh > -1) dir_head = xh;
  4589.     if (xs > -1) dir_sort = xs;
  4590.     if (xk > -1) dir_skey = xk;
  4591.     if (xx > -1) dir_rvrs = xx;
  4592.     if (xf > -1) dir_dots = xf;
  4593.     if (xa > -1) dir_show = xa;
  4594.     if (xm > -1) dir_mode = xm;
  4595.     if (xb > -1) dir_back = xb;
  4596. #ifdef RECURSIVE
  4597.     if (xr > -1) dir_recu = xr;
  4598. #endif /* RECURSIVE */
  4599.     if (xg > -1) dir_msg  = xg;
  4600.     if (xg > 0)
  4601.       makestr(&dirmsg,tmpbuf);
  4602.     return(success = 1);
  4603. }
  4604.  
  4605. int
  4606. domydir() {                             /* Internal DIRECTORY command */
  4607.     extern char *months[];
  4608. #ifdef VMS
  4609.     _PROTOTYP( char * zrelname, (char *,char *) );
  4610.     char * cdp = NULL;
  4611. #endif /* VMS */
  4612.  
  4613.     char name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p = NULL, c = NUL;
  4614.     char linebuf[CKMAXPATH+256];
  4615.     char * mstr = NULL, * dstr = NULL, * s2 = NULL;
  4616.     long len = 0, ndirs = 0, nfiles = 0, nbytes = 0, nmatches = 0;
  4617.     int verbose = 0, wild = 0, page = 0, n = 0, engdate = 0, summary = 0;
  4618.     int heading = 0, xsort = 0, reverse = 0, sortby = 0, msg = 0;
  4619.     int k, i = 0, x = 0, nx = 0, skey = 0, dlen = 0, itsadir = 0;
  4620.     int show = 3, xfermod = 0, backup = 1, rc = 0, getval = 0;
  4621.     int fs = 0;
  4622.     int multiple = 0;
  4623.     int cmifn1 = 1, cmifn2 = 0;
  4624.     long minsize = -1L, maxsize = -1L;
  4625.     struct FDB sw, fi, fl;
  4626.     char dbuf[32], xbuf[32];
  4627.  
  4628. #ifndef NOSPL
  4629.     char array = NUL;
  4630.     char ** ap = NULL;
  4631. #endif /* NOSPL */
  4632.     char
  4633.       * dir_aft = NULL,
  4634.       * dir_bef = NULL,
  4635.       * dir_naf = NULL,
  4636.       * dir_nbf = NULL,
  4637.       * dir_exc = NULL;
  4638.     char * xlist[16];
  4639.  
  4640.     g_matchdot = matchdot;              /* Save global matchdot setting */
  4641.     nolinks = 2;                        /* (it should already be 2) */
  4642.     outfile[0] = NUL;                   /* No output file yet */
  4643.  
  4644.     if (ofp != stdout) {                /* In case of previous interruption */
  4645.         if (ofp) fclose(ofp);
  4646.         ofp = stdout;
  4647.     }
  4648.     for (i = 0; i < 16; i++) xlist[i] = NULL;
  4649.  
  4650.     name[0] = NUL;
  4651.     freedirlist();                      /* In case not freed last time */
  4652.     page      = dir_page > -1 ? dir_page : xaskmore; /* Set option defaults */
  4653.     engdate   = dir_date > -1 ? dir_date : 0;
  4654.     verbose   = dir_verb > -1 ? dir_verb : 1;
  4655.     heading   = dir_head > -1 ? dir_head : 0;
  4656.     xsort     = dir_sort > -1 ? dir_sort : 0;
  4657.     sortby    = dir_skey > -1 ? dir_skey : 0;
  4658.     reverse   = dir_rvrs > -1 ? dir_rvrs : 0;
  4659.     msg       = dir_msg  > -1 ? dir_msg  : 0;
  4660. #ifdef UNIXOROSK
  4661.     if (dir_dots > -1) matchdot = dir_dots;
  4662. #endif /* UNIXOROSK */
  4663.     xfermod   = dir_mode > -1 ? dir_mode : 0;
  4664.     backup    = dir_back > -1 ? dir_back : 1;
  4665. #ifdef RECURSIVE
  4666.     recursive = dir_recu > -1 ? dir_recu : 0;
  4667. #endif /* RECURSIVE */
  4668.     show      = dir_show > -1 ? dir_show : 3;
  4669.  
  4670. #ifdef CK_TTGWSIZ
  4671. #ifdef OS2
  4672.     ttgcwsz();                          /* Screen length for more-prompting */
  4673. #else /* OS2 */
  4674.     /* Check whether window size changed */
  4675.     if (ttgwsiz() > 0) {
  4676.         if (tt_rows > 0 && tt_cols > 0) {
  4677.             cmd_rows = tt_rows;
  4678.             cmd_cols = tt_cols;
  4679.         }
  4680.     }
  4681. #endif /* OS2 */
  4682. #endif /* CK_TTGWSIZ */
  4683.  
  4684.     diractive = 1;
  4685.     cmifn1 = nolinks | 1;               /* 1 = files or directories */
  4686.     cmifn2 = 0;                         /* 0 = not directories only */
  4687.  
  4688.   again:
  4689.  
  4690.     cmfdbi(&sw,                         /* First FDB - command switches */
  4691.            _CMKEY,                      /* fcode */
  4692.            "Enter or Return to confirm the command, or\n\
  4693.  file specification, or switch",
  4694.            "",                          /* default */
  4695.            "",                          /* addtl string data */
  4696.            ndirswtab,                   /* addtl numeric data 1: tbl size */
  4697.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4698.            xxstring,                    /* Processing function */
  4699.            dirswtab,                    /* Keyword table */
  4700.            &fi                          /* Pointer to next FDB */
  4701.            );
  4702.     cmfdbi(&fi,                         /* 2nd FDB - filespec to match */
  4703.            _CMIFI,                      /* fcode */
  4704.            "File specification",        /* hlpmsg */
  4705. #ifdef datageneral
  4706.            "+",                         /* Default filespec is wildcard */
  4707. #else                                   /* that matches all files... */
  4708. #ifdef VMS
  4709.            "*.*",
  4710. #else
  4711.            "*",
  4712. #endif /* VMS */
  4713. #endif /* datageneral */
  4714.            "",                          /* addtl string data */
  4715.            cmifn1,
  4716.            cmifn2,                      /* 1 = only dirs; 0 files or dirs */
  4717.            xxstring,
  4718.            NULL,
  4719.            &fl
  4720.            );
  4721.     cmfdbi(&fl,                         /* Anything that doesn't match */
  4722.            _CMFLD,                      /* fcode */
  4723.            "",                          /* hlpmsg */
  4724.            "",                          /* default */
  4725.            "",                          /* addtl string data */
  4726.            0,                           /* addtl numeric data 1 */
  4727.            0,                           /* addtl numeric data 2 */
  4728.            xxstring,
  4729.            NULL,
  4730.            NULL
  4731.            );
  4732.     while (1) {                         /* Parse 0 or more switches */
  4733.         x = cmfdb(&sw);                 /* Parse something */
  4734.         debug(F101,"domydir cmfdb","",x);
  4735.         if (x < 0)
  4736.           return(x);
  4737.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  4738.           break;
  4739.         c = cmgbrk();
  4740.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4741.             printf("?This switch does not take an argument\n");
  4742.             return(-9);
  4743.         }
  4744.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4745.             printf("?This switch requires an argument\n");
  4746.             return(-9);
  4747.         }
  4748.         switch (k = cmresult.nresult) {
  4749.           case DIR_BRF: verbose = 0; break;
  4750.           case DIR_VRB: verbose = 1; break;
  4751. #ifdef CK_TTGWSIZ
  4752.           case DIR_PAG: page = 1;    break;
  4753.           case DIR_NOP: page = 0;    break;
  4754. #endif /* CK_TTGWSIZ */
  4755.           case DIR_ISO: engdate = 0; break;
  4756.           case DIR_DAT: engdate = 1; break;
  4757.           case DIR_HDG: heading = 1; break;
  4758.           case DIR_NOH: heading = 0; break;
  4759. #ifdef UNIXOROSK
  4760.           case DIR_DOT: matchdot = 1; break;
  4761.           case DIR_NOD: matchdot = 0; break;
  4762. #endif /* UNIXOROSK */
  4763.           case DIR_ALL:
  4764.             show = 3;
  4765.             cmifn1 |= 1;
  4766.             cmifn2 = 0;
  4767.             goto again;
  4768.           case DIR_DIR:
  4769.             show = 2;
  4770.             cmifn1 |= 1;
  4771.             cmifn2 = 1;
  4772.             goto again;
  4773.           case DIR_FIL:
  4774.             show = 1;
  4775.             cmifn1 &= ~(1);
  4776.             cmifn2 = 0;
  4777.             goto again;
  4778.           case DIR_SRT:
  4779.             x = DIRS_NM;
  4780.             if (c == ':' || c == '=')
  4781.               if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4782.                 return(x);
  4783.             xsort = 1;
  4784.             sortby = x;
  4785.             break;
  4786.  
  4787.           case DIR_BUP: backup  = 1; fs++;   break;
  4788.           case DIR_NOB: backup  = 0; fs++;   break;
  4789.  
  4790.           case DIR_NOS: xsort = 0;     break;
  4791.           case DIR_ASC: reverse = 0;   break;
  4792.           case DIR_DSC: reverse = 1;   break;
  4793. #ifdef RECURSIVE
  4794.           case DIR_REC: recursive = 1; diractive = 1; break;
  4795.           case DIR_NOR: recursive = 0; diractive = 0; break;
  4796. #endif /* RECURSIVE */
  4797.           case DIR_TYP: xfermod = 1;   break;
  4798.           case DIR_NOT: xfermod = 0;   break;
  4799.  
  4800. #ifdef CKSYMLINK
  4801.           case DIR_LNK:                 /* Follow links */
  4802.             nolinks = 0;
  4803.             cmifn1 &= ~(2);
  4804.             goto again;
  4805.           case DIR_NLK:                 /* Don't follow links */
  4806.             nolinks = 2;
  4807.             cmifn1 &= ~(2);
  4808.             goto again;
  4809. #endif /* CKSYMLINK */
  4810.  
  4811.           case DIR_NOM: msg     = 0;   break;
  4812.           case DIR_MSG:
  4813.             if (c == ':' || c == '=')
  4814.               if ((x = cmfld("Message to append to each line",
  4815.                              "",
  4816.                              &s,
  4817.                              xxstring
  4818.                              )) < 0)
  4819.                 return(x);
  4820.             msg = 1;
  4821.             ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4822.             break;
  4823.  
  4824.           case DIR_SMA:
  4825.           case DIR_LAR:
  4826.             if (!getval) break;
  4827.             if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  4828.               return(x);
  4829.             fs++;
  4830.             show = 1;
  4831.             switch (cmresult.nresult) {
  4832.               case DIR_SMA: minsize = y; break;
  4833.               case DIR_LAR: maxsize = y; break;
  4834.             }
  4835.             break;
  4836.  
  4837. #ifndef NOSPL
  4838.           case DIR_ARR:
  4839.             if (c != ':' && c != '=') {
  4840.                 printf("?Array name required\n");
  4841.                 return(-9);
  4842.             }
  4843.             if ((x = cmfld("Array name (a single letter will do)",
  4844.                            "",
  4845.                            &s,
  4846.                            NULL
  4847.                            )) < 0) {
  4848.                 if (x == -3) {
  4849.                     printf("?Array name required\n");
  4850.                     return(-9);
  4851.                 } else
  4852.                   return(x);
  4853.             }
  4854.             if (!*s) {
  4855.                 printf("?Array name required\n");
  4856.                 return(-9);
  4857.             }
  4858.             s2 = s;
  4859.             if (*s == CMDQ) s++;
  4860.             if (*s == '&') s++;
  4861.             if (!isalpha(*s)) {
  4862.                 printf("?Bad array name - \"%s\"\n",s2);
  4863.                 return(-9);
  4864.             }
  4865.             array = *s++;
  4866.             if (isupper(array)) array = tolower(array);
  4867.             if (*s && (*s != '[' || *(s+1) != ']')) {
  4868.                 printf("?Bad array name - \"%s\"\n",s2);
  4869.                 return(-9);
  4870.             }
  4871.             break;
  4872. #endif /* NOSPL */
  4873.           case DIR_AFT:
  4874.           case DIR_BEF:
  4875.           case DIR_NAF:
  4876.           case DIR_NBF:
  4877.             if (!getval) break;
  4878.             if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  4879.                 if (x == -3) {
  4880.                     printf("?Date-time required\n");
  4881.                     rc = -9;
  4882.                 } else
  4883.                   rc = x;
  4884.                 goto xdomydir;
  4885.             }
  4886.             fs++;
  4887.             switch (k) {
  4888.               case DIR_AFT: makestr(&dir_aft,s); break;
  4889.               case DIR_BEF: makestr(&dir_bef,s); break;
  4890.               case DIR_NAF: makestr(&dir_naf,s); break;
  4891.               case DIR_NBF: makestr(&dir_nbf,s); break;
  4892.             }
  4893.             break;
  4894.           case DIR_EXC:
  4895.             if (!getval) break;
  4896.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4897.                 if (x == -3) {
  4898.                     printf("?Pattern required\n");
  4899.                     rc = -9;
  4900.                 } else
  4901.                   rc = x;
  4902.                 goto xdomydir;
  4903.             }
  4904.             fs++;
  4905.             makestr(&dir_exc,s);
  4906.             break;
  4907.  
  4908.           case DIR_SUM:
  4909.             summary = 1; break;
  4910.  
  4911.           case DIR_BIN: {
  4912.               extern struct keytab txtbin[];
  4913.               extern int xfiletype;
  4914.               if (!getval) break;
  4915.               if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0) {
  4916.                   rc = x;
  4917.                   goto xdomydir;
  4918.               }
  4919.               if (x == 2) {
  4920.                   xfiletype = -1;
  4921.               } else {
  4922.                   xfiletype = x;
  4923.                   fs = 1;
  4924.               }
  4925.               break;
  4926.           }
  4927.           case DIR_OUT:
  4928.             if ((x = cmofi("File for directory listing","",&s,xxstring)) < 0)
  4929.               return(x);
  4930.             ckstrncpy(outfile,s,CKMAXPATH+1);
  4931.             break;
  4932.  
  4933.           default:
  4934.             printf("?Sorry, not implemented yet - \"%s\"\n", atmbuf);
  4935.             goto xdomydir;
  4936.         }
  4937.     }
  4938.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Safe copy of filespec */
  4939.  
  4940. /* ^^^ START MULTIPLE */
  4941.     
  4942.     while (1) {
  4943.     x = cmfld("Another filespec or Enter","",&s,xxstring);
  4944.     if (x == -3)
  4945.       break;
  4946.     if (x < 0)
  4947.       return(x);
  4948.     ckstrncat(line,",",LINBUFSIZ);
  4949.     ckstrncat(line,s,LINBUFSIZ);
  4950.     multiple++;
  4951.     }
  4952.     ckmakmsg(tmpbuf,TMPBUFSIZ,"{",line,"}",NULL);
  4953.     ckstrncpy(line,tmpbuf,LINBUFSIZ);
  4954.     cmresult.nresult = 1;
  4955.     cmresult.fcode = _CMIFI;
  4956.  
  4957. /* ^^^ END MULTIPLE */
  4958.  
  4959.     s = line;
  4960.  
  4961.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  4962.       return(x);
  4963.     if (cmresult.fcode != _CMIFI) {     /* Nothing matched */
  4964.         char * m;
  4965.         if (*s == '/')
  4966. #ifdef UNIXOROSK
  4967.           m = "does not match switch or name of accessible file";
  4968. #else
  4969. #ifdef OS2
  4970.           m = "does not match switch or name of accessible file";
  4971. #else
  4972.           m = "no switches match";
  4973. #endif /* OS2 */
  4974. #endif /* UNIXOROSX */
  4975.         else
  4976.           m = "not found or not accessible";
  4977.         printf("\"%s\" - %s\n",s,m);
  4978.         rc = -9;
  4979.         goto xdomydir;
  4980.     }
  4981.     wild = cmresult.nresult;            /* Wildcard was given? */
  4982.     debug(F111,"domydir cmifi2",s,wild);
  4983.  
  4984.     if (outfile[0]) {
  4985.         ofp = fopen(outfile,"w");       /* Open output file */
  4986.         if (!ofp) {
  4987.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  4988.             ofp = stdout;
  4989.             return(-9);
  4990.         }
  4991.         page = 0;
  4992.     }
  4993.  
  4994. #ifdef OS2
  4995.     if (!wild) {
  4996.         if (zchki(s) == -2) {           /* Found a directory */
  4997.             p = s + (int)strlen(s) - 1; /* Yes */
  4998.             if (*p == '\\' || *p == '/')
  4999.               strcat(s, "*");
  5000.             else if (*p == ':')
  5001.               strcat(s, "./*");
  5002.             else
  5003.               strcat(s, "/*");
  5004.             wild = 1;                   /* Now it's wild */
  5005.         }
  5006.     }
  5007. #else
  5008.     if (!wild) if (isdir(s)) {          /* Is it a directory? */
  5009.         p = s + (int)strlen(s) - 1;     /* Yes */
  5010. #ifdef VMS
  5011.         {
  5012.             /* Convert from FOO.DIR;1 to [x.FOO] if necessary */
  5013.             char buf[CKMAXPATH+1];
  5014.             debug(F000,"domydir directory 0",s,*p);
  5015.             if (cvtdir(s,buf,CKMAXPATH) > 0)
  5016.               ckstrncpy(line,buf,LINBUFSIZ);
  5017.         }
  5018. #endif /* VMS */
  5019.         debug(F000,"domydir directory 1",s,*p);
  5020. #ifdef VMS
  5021.         if (*p == ']' || *p == '>' || *p == ':')
  5022.           strcat(s, "*.*");
  5023. #else
  5024. #ifdef datageneral
  5025.         if (*p == ':')
  5026.           strcat(s, "+");
  5027.         else
  5028.           strcat(s, ":+");
  5029. #else
  5030. #ifdef STRATUS
  5031.         if (*p == '>')
  5032.           strcat(s, "*");
  5033.         else
  5034.           strcat(s, ">*");
  5035. #endif /* STRATUS */
  5036. #endif /* datageneral */
  5037. #endif /* VMS */
  5038.         wild = 1;                       /* Now it's wild */
  5039.         debug(F000,"domydir directory 2",s,*p);
  5040.     }
  5041. #endif /* OS2 */
  5042.  
  5043. #ifdef ZXREWIND
  5044. /* cmifi() already called nzxpand so we can just re-use the same list. */
  5045.     if (!multiple) {
  5046.     x = zxrewind();            /* Rewind the list */
  5047.     debug(F111,"domydir zxrewind",s,x);
  5048.     } else {
  5049. #endif /* ZXREWIND */
  5050.     nzxopts = (show == ZX_DIRONLY) ? ZX_DIRONLY :
  5051.       (show == ZX_FILONLY ? ZX_FILONLY : 0);
  5052.     if (matchdot)  nzxopts |= ZX_MATCHDOT;
  5053.     if (recursive) nzxopts |= ZX_RECURSE;
  5054.     x = nzxpand(s,nzxopts);             /* Expand file list */
  5055.     debug(F111,"domydir nzxpand",s,x);
  5056. #ifdef ZXREWIND
  5057.     }
  5058. #endif /* ZXREWIND */
  5059.  
  5060. #ifndef NOSPL
  5061.     if (array) {
  5062.         int n, xx;
  5063.         n = (x < 0) ? 0 : x;
  5064.         if ((xx = dclarray(array,n)) < 0) {
  5065.             printf("?Array declaration failure\n");
  5066.             rc = -9;
  5067.             goto xdomydir;
  5068.         }
  5069.         array = xx;
  5070.         ap = a_ptr[array];
  5071.         if (n < 1) {
  5072.             rc = 0;
  5073.             goto xdomydir;
  5074.         }
  5075.     } else
  5076. #endif /* NOSPL */
  5077.       if (x < 1) {
  5078. #ifdef CKROOT
  5079.           extern int ckrooterr;
  5080.           if (ckrooterr)
  5081.             printf("?Off limits: %s\n",s);
  5082.           else
  5083. #endif /* CKROOT */
  5084.             if (x == 0 && isdir(s))
  5085.               printf("?Empty directory - \"%s\"\n", s);
  5086.             else
  5087.               printf("?%s %s match - \"%s\"\n",
  5088.                      (x == 0) ? "No" : "Too many",
  5089.                      (show == 2) ? "directories" : "files",
  5090.                      s
  5091.                      );
  5092.           rc = -9;
  5093.           goto xdomydir;
  5094.     }
  5095.     nx = x;                             /* Remember how many files */
  5096.  
  5097.     if (msg) {
  5098.         makestr(&dirmsg,tmpbuf);
  5099.         dirmsglen = strlen(tmpbuf);
  5100.     }
  5101.  
  5102. #ifdef VMS
  5103.     cdp = zgtdir();                     /* Get current directory */
  5104.     debug(F110,"domydir VMS zgtdir",cdp,0);
  5105. #endif /* VMS */
  5106.  
  5107.     if (xsort && verbose) {             /* If sorting, allocate space */
  5108.         if (!(dirlist = (char **) malloc((x + 1) * sizeof(char **)))) {
  5109.             if (!quiet) {
  5110.                 printf("* Warning: Failure to allocate memory for sorting.\n");
  5111.                 printf("* Will proceed without sorting...\n");
  5112.             }
  5113.             xsort = 0;
  5114.         }
  5115.         debug(F101,"domydir sort malloc","",xsort);
  5116.     }
  5117.  
  5118.     /* Display the listing */
  5119.  
  5120. #ifndef NOSPL
  5121.     if (array)                          /* Storing instead of printing */
  5122.       heading = 0;
  5123. #endif /* NOSPL */
  5124.  
  5125.     if (heading) {                      /* If /HEADING print heading */
  5126.         zfnqfp(s,TMPBUFSIZ,tmpbuf);
  5127.         fprintf(ofp,"\nDirectory of %s\n\n",tmpbuf);
  5128.         n += 3;
  5129.     }
  5130.     if (page > -1)                      /* Paging */
  5131.       xaskmore = page;
  5132.  
  5133.     if (!verbose) {                     /* /BRIEF */
  5134.         if (outfile[0]) {               /* To file  */
  5135.             int k = 0;
  5136.             znext(name);
  5137.             while (name[0]) {           /* One line per file */
  5138.                 k++;
  5139.                 if (fs) if (fileselect(name,
  5140.                                        dir_aft,dir_bef,dir_naf,dir_nbf,
  5141.                                        minsize,maxsize,!backup,16,xlist) < 1) {
  5142.                     znext(name);
  5143.                     continue;
  5144.                 }
  5145.                 fprintf(ofp,"%s\n",name);
  5146.                 znext(name);
  5147.             }
  5148.             if (heading)
  5149.               fprintf(ofp,"Files: %d\n\n",k);
  5150.             rc = 1;
  5151.             goto xdomydir;
  5152.         } else {
  5153.             rc = filhelp(x,"","",n,0);
  5154.             if (rc < 0)
  5155.               goto xdomydir;
  5156.             if (heading && rc > 0)
  5157.               fprintf(ofp,"Files: %d\n\n",x); /* (Might scroll a line or 2) */
  5158.             rc = 1;
  5159.             goto xdomydir;
  5160.         }
  5161.     }
  5162.     ndirs = nfiles = nbytes = 0L;       /* Initialize counters */
  5163.  
  5164.     if (dir_exc)                        /* Have exception list? */
  5165.       makelist(dir_exc,xlist,16);    /* Yes, convert to array */
  5166.  
  5167.     diractive = 1;
  5168.     znext(name);                        /* Get next file */
  5169.     while (name[0]) {                   /* Loop for each file */
  5170.         if (fs) if (fileselect(name,
  5171.                        dir_aft,dir_bef,dir_naf,dir_nbf,
  5172.                        minsize,maxsize,!backup,16,xlist) < 1) {
  5173.             znext(name);
  5174.             continue;
  5175.         }
  5176.         len = zgetfs(name);             /* Get file length */
  5177.         debug(F111,"domydir zgetfs",name,len);
  5178. #ifdef VMSORUNIX
  5179.         itsadir = zgfs_dir;             /* See if it's a directory */
  5180. #else
  5181.         itsadir = (len == -2 || isdir(name));
  5182. #endif /* VMSOUNIX */
  5183.         debug(F111,"domydir itsadir",name,itsadir);
  5184.         if ((itsadir && (show == 1)) || (!itsadir && (show == 2))) {
  5185.             znext(name);
  5186.             continue;
  5187.         }
  5188.         /* Get here when we know we have selected this file */
  5189.  
  5190.         nmatches ++;
  5191.         if (itsadir) {                  /* Accumulate totals for summary */
  5192.             ndirs++;
  5193.         } else {
  5194.             nfiles++;
  5195.             nbytes += len;
  5196.         }
  5197.         if (summary) {                  /* Summary only, no detail */
  5198.             znext(name);
  5199.             continue;
  5200.         }
  5201. #ifndef NOSPL
  5202.         if (array) {
  5203.             debug(F111,"domydir array",name,nfiles);
  5204.             if (ap)
  5205.               makestr(&(ap[nmatches]),name);
  5206.             znext(name);
  5207.             continue;
  5208.         }
  5209. #endif /* NOSPL */
  5210.  
  5211. /*
  5212.   NOTE: The sprintf's in this routine should be safe.  They involve
  5213.   permission strings, date/time strings, and filenames, all of which have
  5214.   known maximum lengths; none of these items is input from users.  The
  5215.   destination buffers are large enough to hold maximum sizes for any and
  5216.   all items.
  5217. */
  5218.         dstr = zfcdat(name);            /* Get modification date/time */
  5219.         debug(F111,"domydir zcfdat",dstr,0);
  5220.         if (!dstr) dstr = "";
  5221.         {
  5222. /*
  5223.   Note that zfcdat() always returns "" or yyyymmdd hh:mm:ss, so any warnings
  5224.   about possible out-of-bounds dstr[] array refs do not apply.  This block of
  5225.   code is to stifle the warnings and also allows for any out-of-spec
  5226.   zfcdat() implementations.
  5227. */
  5228.             int x;
  5229.             char * p = "00000000 00:00:00";
  5230.             x = ckstrncpy(xbuf,dstr,32);
  5231.             if (x < 17) ckstrncpy(&xbuf[x],p+x,32-x);
  5232.             dstr = xbuf;
  5233.         }
  5234.         if (engdate) {                  /* English date requested? */
  5235.             short month, day, year, hour, minute, seconds;
  5236.             month = (dstr[4]-48)*10 + (dstr[5]-48);
  5237.             mstr  = (month > 0 && month <= 12) ? months[month-1] : "xxx";
  5238.             day   = (dstr[6]-48)*10 + (dstr[7]-48);
  5239.             year  = (((dstr[0]-48)*10 +
  5240.                       (dstr[1]-48))*10 +
  5241.                       (dstr[2]-48))*10 +
  5242.                       (dstr[3]-48);
  5243.             hour  = (dstr[9]-48)*10 + (dstr[10]-48);
  5244.             minute = (dstr[12]-48)*10 + (dstr[13]-48);
  5245.             seconds = (dstr[15]-48)*10 + (dstr[16]-48);
  5246.             sprintf(dbuf,               /* SAFE */
  5247.                     "%2d-%s-%4d %02d:%02d:%02d",
  5248.                     day,mstr,year,hour,minute,seconds
  5249.                     );
  5250.             dstr = dbuf;
  5251.         } else {                        /* ISO date */
  5252.             dbuf[0] = dstr[0];          /* yyyy */
  5253.             dbuf[1] = dstr[1];
  5254.             dbuf[2] = dstr[2];
  5255.             dbuf[3] = dstr[3];
  5256.             dbuf[4] = '-';
  5257.             dbuf[5] = dstr[4];          /* mm (numeric) */
  5258.             dbuf[6] = dstr[5];
  5259.             dbuf[7] = '-';
  5260.             dbuf[8] = dstr[6];          /* dd */
  5261.             dbuf[9] = dstr[7];
  5262.             strcpy(dbuf+10,dstr+8);     /* hh:mm:ss */
  5263.             dstr = dbuf;
  5264.         }
  5265.         dlen = strlen(dbuf);            /* Length of date */
  5266.         name[CKMAXPATH] = NUL;
  5267. #ifdef CK_PERMS
  5268. #ifdef VMSORUNIX
  5269.         p = ziperm(name);               /* Get permissions */
  5270.         debug(F110,"ziperm perms",p,0);
  5271. #else
  5272.         p = zgperm(name);
  5273.         debug(F110,"zgperm perms",p,0);
  5274. #endif /* VMSORUNIX */
  5275. #else
  5276.         p = NULL;
  5277.         debug(F110,"NULL perms",p,0);
  5278. #endif /* CK_PERMS */
  5279.  
  5280. #ifdef VMS
  5281.         /* Get relative name to save space -- VMS fullnames are long... */
  5282.         ckstrncpy(name,zrelname(name,cdp),CKMAXPATH);
  5283. #endif /* VMS */
  5284.  
  5285.         if (itsadir && len < 0) {       /* Directory */
  5286. #ifdef VMS
  5287.             sprintf(linebuf,"%-22s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5288. #else
  5289.             if (p)
  5290.               sprintf(linebuf,"%10s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5291.             else
  5292.               sprintf(linebuf,"%-10s  %s  %s", "<DIR>", dstr, name);
  5293. #endif /* VMS */
  5294.         } else {                        /* Regular file */
  5295. #ifdef VMS
  5296.             sprintf(linebuf,"%-22s%10ld  %s  %s", p, len, dstr, name);
  5297. #else
  5298.             if (p)
  5299.               sprintf(linebuf,"%10s%10ld  %s  %s", p, len, dstr, name);
  5300.             else
  5301.               sprintf(linebuf,"%10ld  %s  %s", len, dstr, name);
  5302. #endif /* VMS */
  5303.         }
  5304. #ifdef UNIX
  5305. #ifdef CKSYMLINK
  5306.         if (zgfs_link) {
  5307.             int n, m;
  5308.             extern char linkname[];
  5309.             n = strlen(linebuf);
  5310.             m = strlen(linkname) + n;
  5311.             if (m < CKMAXPATH + 58)
  5312.               strcpy(linebuf+n, " -> "); /* safe (checked) */
  5313.             if (m + 4 < CKMAXPATH - 58)
  5314.               strcpy(linebuf+n+4, linkname); /* safe (checked) */
  5315.         } else
  5316. #endif /* CKSYMLINK */
  5317. #endif /* UNIX */
  5318.         if (xfermod) {                  /* Show transfer mode */
  5319.             int i, x, y;
  5320.             char * s = "";
  5321.             y = -1;
  5322.             x = scanfile(name,&y,nscanfile);
  5323.             switch (x) {
  5324.               case FT_TEXT: s = " (T)"; break;
  5325.               case FT_7BIT: s = " (T)(7BIT)"; break;
  5326.               case FT_8BIT: s = " (T)(8BIT)"; break;
  5327. #ifdef UNICODE
  5328.               case FT_UTF8: s = " (T)(UTF8)"; break;
  5329.               case FT_UCS2:
  5330.                 s = y ? " (T)(UCS2LE)" : " (T)(UCS2BE)";
  5331.                 break;
  5332. #endif /* UNICODE */
  5333.               case FT_BIN:  s = " (B)"; break;
  5334.             }
  5335.             if (!*s) {
  5336.                 s = binary ? " (B)" : " (T)";
  5337.             }
  5338.             if (*s) {
  5339.                 int n;
  5340.                 n = strlen(linebuf);
  5341.                 if (n + 4 < CKMAXPATH - 58)
  5342.                   strcpy(linebuf+n, s); /* safe (checked) */
  5343.             }
  5344.         }
  5345.         if (msg && dirmsg) {
  5346.             int n;
  5347.             n = strlen(linebuf);
  5348.             if (n + dirmsglen + 2 < CKMAXPATH)
  5349.               sprintf((char *)(linebuf+n)," %s", dirmsg); /* SAFE */
  5350.         }
  5351.         if (xsort) {                    /* Sorting - save line */
  5352.             i = strlen(linebuf);
  5353.             if ((ndirlist >= nx) ||
  5354.                 !(dirlist[ndirlist] = (char *)malloc(i+1))) {
  5355.                 printf("?Memory allocation error - try /NOSORT\n");
  5356.                 rc = -9;
  5357.                 goto xdomydir;
  5358.             }
  5359.             strcpy(dirlist[ndirlist],linebuf); /* safe */
  5360.             ndirlist++;
  5361.         }
  5362.         znext(name);                    /* Peek ahead to next file */
  5363.  
  5364.         if (!xsort) {
  5365.             fprintf(ofp,"%s\n",linebuf);
  5366.             if (page && (name[0] || heading)) { /* If /PAGE */
  5367.                 if (cmd_cols > 0) {
  5368.                     int x = strlen(linebuf);
  5369.                     int y;
  5370.                     y = (x % cmd_cols) ? 1 : 0;
  5371.                     n += x / cmd_cols + y;
  5372.                 } else {
  5373.                     n++;
  5374.                 }
  5375. #ifdef CK_TTGWSIZ
  5376.                 if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5377.                     if (!askmore()) {
  5378.                         rc = 0;
  5379.                         goto xdomydir;
  5380.                     } else
  5381.                       n = 0;
  5382.                 }
  5383. #endif /* CK_TTGWSIZ */
  5384.             }
  5385.         }
  5386.     }
  5387. #ifndef NOSPL
  5388.     if (array) {
  5389.         if (ap)
  5390.           makestr(&(ap[0]),ckitoa(nmatches));
  5391.         rc = 1;
  5392.         goto xdomydir;
  5393.     }
  5394. #endif /* NOSPL */
  5395.     if (xsort) {
  5396.         skey = 0;
  5397. #ifdef VMS
  5398.         switch (sortby) {
  5399.           case DIRS_NM: skey = dlen + 35; break;
  5400.           case DIRS_DT: skey = 33; break;
  5401.           case DIRS_SZ: skey = 21;
  5402.         }
  5403. #else
  5404.         if (p) {
  5405.             switch (sortby) {
  5406.               case DIRS_NM: skey = dlen + 24; break;
  5407.               case DIRS_DT: skey = 22; break;
  5408.               case DIRS_SZ: skey = 10;
  5409.             }
  5410.         } else {
  5411.             switch (sortby) {
  5412.               case DIRS_NM: skey = dlen + 14; break;
  5413.               case DIRS_DT: skey = 12; break;
  5414.               case DIRS_SZ: skey = 0;
  5415.             }
  5416.         }
  5417. #endif /* VMS */
  5418.         sh_sort(dirlist,NULL,ndirlist,skey,reverse,filecase);
  5419.         for (i = 0; i < ndirlist; i++) {
  5420.             fprintf(ofp,"%s\n",dirlist[i]);
  5421.             if (page && (i < ndirlist -1 || heading)) { /* If /PAGE */
  5422.                 if (cmd_cols > 0) {
  5423.                     int x = strlen(dirlist[i]);
  5424.                     int y;
  5425.                     y = (x % cmd_cols) ? 1 : 0;
  5426.                     n += ((int)strlen(dirlist[i]) / cmd_cols) + y;
  5427.                 } else {
  5428.                     n++;
  5429.                 }
  5430. #ifdef CK_TTGWSIZ
  5431.                 if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5432.                     if (!askmore()) {
  5433.                         rc = 0;
  5434.                         goto xdomydir;
  5435.                     } else
  5436.                       n = 0;
  5437.                 }
  5438. #endif /* CK_TTGWSIZ */
  5439.             }
  5440.         }
  5441.     }
  5442.  
  5443.     if (heading || summary) {
  5444. #ifdef CKFLOAT
  5445.         CKFLOAT gm;
  5446. #endif /* CKFLOAT */
  5447.         fprintf(ofp,"\n%ld director%s, %ld file%s, %ld byte%s",
  5448.                ndirs,
  5449.                (ndirs == 1) ? "y" : "ies",
  5450.                nfiles,
  5451.                (nfiles == 1) ? "" : "s",
  5452.                nbytes,
  5453.                (nbytes == 1) ? "" : "s"
  5454.                );
  5455. #ifdef CKFLOAT
  5456.         gm = ((CKFLOAT) nbytes ) / 1000000.0;
  5457.         if (gm > 1000.0)
  5458.           fprintf(ofp," (%0.2fGB)",(gm / 1000.0));
  5459.         else if (gm >= 0.01)
  5460.           fprintf(ofp," (%0.2fMB)",gm);
  5461. #endif /* CKFLOAD */
  5462.         fprintf(ofp,"\n\n");
  5463.     }
  5464.   xdomydir:
  5465.     if (g_matchdot > -1) {
  5466.         matchdot = g_matchdot;          /* Restore these... */
  5467.         g_matchdot = -1;
  5468.     }
  5469.     freedirlist();
  5470.     if (ofp != stdout) {                /* Close any output file */
  5471.         if (ofp) fclose(ofp);
  5472.         ofp = stdout;
  5473.     }
  5474.     if (rc > 0)
  5475.       success = 1;
  5476.     return(rc);
  5477. }
  5478.  
  5479. int
  5480. dodir(cx) int cx; {                     /* Do the DIRECTORY command */
  5481.     char *dc , *msg;
  5482.  
  5483. #ifdef OS2
  5484.     return(domydir());
  5485. #else /* OS2 */
  5486.     if (nopush
  5487. #ifdef DOMYDIR                          /* Builds that domydir() by default */
  5488.         || (cx == XXDIR || cx == XXLDIR)
  5489. #endif /* DOMYDIR */
  5490.         )
  5491.       return(domydir());                /* Built-in directory command */
  5492.  
  5493.     /* Use the system's directory command. */
  5494.  
  5495.     msg = (cx == XXLS) ?
  5496.       "Arguments for ls" :
  5497.         "Directory and/or file specification";
  5498.     if ((x = cmtxt(msg,"",&s,xxstring)) < 0)
  5499.       return(x);
  5500.  
  5501.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);      /* Copy the filespec */
  5502.     s = tmpbuf;
  5503.  
  5504.     if ((y = cmcfm()) < 0) return(y);
  5505.  
  5506.     lp = line;
  5507.     if (!(dc = getenv("CK_DIR")))
  5508.       dc = DIRCMD;
  5509.     ckmakmsg(lp,LINBUFSIZ,dc," ",s,NULL);
  5510.     debug(F110,"DIR",line,0);
  5511. #ifdef VMS
  5512.     conres();
  5513. #endif /* VMS */
  5514.     x = zshcmd(line);
  5515. #ifdef VMS
  5516.     concb((char)escape);
  5517. #endif /* VMS */
  5518.     return(success = (x < 1) ? 0 : 1);
  5519. #endif /* OS2 */
  5520. }
  5521.  
  5522. #ifndef NOSERVER
  5523. #ifndef NOFRILLS
  5524. /* Do the ENABLE and DISABLE commands */
  5525.  
  5526. int
  5527. doenable(y,x) int y, x; {
  5528. #ifdef CK_LOGIN
  5529.     if (isguest)                        /* IKSD: Don't let guests */
  5530.       return(0);                        /* enable anything that's disabled */
  5531. #endif /* CK_LOGIN */
  5532.     switch (x) {
  5533.       case EN_ALL:
  5534.         en_cwd = en_cpy = en_del = en_dir = en_fin = en_get = y;
  5535.         en_ren = en_sen = en_set = en_spa = en_typ = en_ret = y;
  5536.         if (!inserver)
  5537.           en_who = en_mai = en_pri = y;
  5538.         en_mkd = en_rmd = y;
  5539.         en_xit = y;
  5540. #ifndef datageneral
  5541.         en_bye = y;
  5542. #endif /* datageneral */
  5543. #ifndef NOPUSH
  5544.         if (!nopush && !inserver)
  5545.           en_hos = y;
  5546. #endif /* NOPUSH */
  5547. #ifndef NOSPL
  5548.         en_asg = en_que = y;
  5549. #endif /* NOSPL */
  5550.         break;
  5551.  
  5552.       case EN_BYE:
  5553. #ifndef datageneral
  5554. /*
  5555.   In Data General AOS/VS Kermit can't log out its superior process.
  5556. */
  5557.         en_bye = y;
  5558. #endif /* datageneral */
  5559.         break;
  5560.       case EN_CPY:
  5561.         en_cpy = y;
  5562.         break;
  5563.       case EN_CWD:
  5564.         en_cwd = y;
  5565. #ifdef IKSD
  5566.         if (inserver && y == 0) {
  5567.             fnrpath = PATH_OFF;
  5568.             fnspath = PATH_OFF;
  5569.         }
  5570. #endif /* IKSD */
  5571.         break;
  5572.       case EN_DEL:                      /* Deleting of files */
  5573.         en_del = y;
  5574.         break;
  5575.       case EN_DIR:
  5576.         en_dir = y;
  5577.         break;
  5578.       case EN_FIN:
  5579.         en_fin = y;
  5580.         break;
  5581.       case EN_GET:
  5582.         en_get = y;
  5583.         break;
  5584. #ifndef NOPUSH
  5585.       case EN_HOS:
  5586.         if (!nopush)
  5587.          en_hos = y;
  5588.         break;
  5589. #endif /* NOPUSH */
  5590.       case EN_REN:
  5591.         en_ren = y;
  5592.         break;
  5593.       case EN_SEN:
  5594.         en_sen = y;
  5595.         break;
  5596.       case EN_SET:
  5597.         en_set = y;
  5598.         break;
  5599.       case EN_SPA:
  5600.         en_spa = y;
  5601.         break;
  5602.       case EN_TYP:
  5603.         en_typ = y;
  5604.         break;
  5605.       case EN_WHO:
  5606.         en_who = y;
  5607.         break;
  5608. #ifndef NOSPL
  5609.       case EN_ASG:
  5610.         en_asg = y;
  5611.         break;
  5612.       case EN_QUE:
  5613.         en_que = y;
  5614.         break;
  5615. #endif /* NOSPL */
  5616.       case EN_RET:
  5617.         en_del = y;
  5618.         break;
  5619.       case EN_MAI:
  5620. #ifdef CK_LOGIN
  5621.         if (isguest && y) {
  5622.             printf("?Sorry, not valid for guests\n");
  5623.             return(-9);
  5624.         }
  5625. #endif /* CK_LOGIN */
  5626.         en_mai = y;
  5627.         break;
  5628.       case EN_PRI:
  5629. #ifdef CK_LOGIN
  5630.         if (isguest && y) {
  5631.             printf("?Sorry, not valid for guests\n");
  5632.             return(-9);
  5633.         }
  5634. #endif /* CK_LOGIN */
  5635.         en_pri = y;
  5636.         break;
  5637.       case EN_MKD:
  5638.         en_mkd = y;
  5639.         break;
  5640.       case EN_RMD:
  5641.         en_rmd = y;
  5642.         break;
  5643.       case EN_XIT:
  5644.         en_xit = y;
  5645.         break;
  5646.       case EN_ENA:
  5647.         if (((y & 1) && !(en_ena & 1)) ||
  5648.             ((y & 2) && !(en_ena & 2))) {
  5649.             printf("?Sorry, DISABLE ENABLE can not be undone\n");
  5650.             return(-9);
  5651.         } else {
  5652.             en_ena = y;
  5653.             break;
  5654.         }
  5655.       default:
  5656.         return(-2);
  5657.     }
  5658.     return(1);
  5659. }
  5660. #endif /* NOFRILLS */
  5661. #endif /* NOSERVER */
  5662.  
  5663. #ifndef NOFRILLS
  5664.  
  5665. static int del_lis = 0;
  5666. static int del_dot = 0;
  5667. static int del_hdg = 0;
  5668. static int del_pag = -1;
  5669. static int del_ask = 0;
  5670.  
  5671. #ifndef NOSHOW
  5672. VOID
  5673. showdelopts() {
  5674.     int x = 0;
  5675.     extern int optlines;
  5676.     prtopt(&optlines,"");
  5677.     prtopt(&optlines,"DELETE");
  5678.     if (del_ask > -1) {
  5679.         prtopt(&optlines, del_ask ? "/ASK" : "/NOASK");
  5680.         x++;
  5681.     }
  5682. #ifdef UNIXOROSK
  5683.     if (del_dot > -1) {
  5684.         prtopt(&optlines, del_dot ? "/DOTFILES" : "/NODOTFILES");
  5685.         x++;
  5686.     }
  5687. #endif /* UNIXOROSK */
  5688.     if (del_lis > -1) {
  5689.         prtopt(&optlines, del_lis ? "/LIST" : "/NOLIST");
  5690.         x++;
  5691.     }
  5692.     if (del_hdg > -1) {
  5693.         prtopt(&optlines, del_hdg ? "/HEADING" : "/NOHEADING");
  5694.         x++;
  5695.     }
  5696. #ifndef CK_TTGWSIZ
  5697.     if (del_pag > -1) {
  5698.         prtopt(&optlines, del_pag ? "/PAGE" : "/NOPAGE");
  5699.         x++;
  5700.     }
  5701. #endif /* CK_TTGWSIZ */
  5702.     if (!x) prtopt(&optlines,"(no options set)");
  5703.     prtopt(&optlines,"");
  5704. }
  5705. #endif /* NOSHOW */
  5706.  
  5707.  
  5708. int
  5709. setdelopts() {
  5710.     int x_lis = -1, x_pag = -1, x_dot = -1, x_hdg = -1, x_ask = -1;
  5711.     int getval = 0;
  5712.     char c;
  5713.     while (1) {
  5714.         if ((y = cmswi(deltab,ndeltab,"Switch","",xxstring)) < 0) {
  5715.             if (y == -3)
  5716.               break;
  5717.             else
  5718.               return(y);
  5719.         }
  5720.         c = cmgbrk();
  5721.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5722.             printf("?This switch does not take an argument\n");
  5723.             return(-9);
  5724.         }
  5725.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  5726.             printf("?This switch requires an argument\n");
  5727.             return(-9);
  5728.         }
  5729.         switch (y) {
  5730.           case DEL_DOT:
  5731.             x_dot = 1;
  5732.             break;
  5733.           case DEL_NOD:
  5734.             x_dot = 0;
  5735.             break;
  5736.           case DEL_HDG:
  5737.             x_hdg = 1;
  5738.             break;
  5739.           case DEL_LIS:
  5740.             x_lis = 1;
  5741.             break;
  5742.           case DEL_NOL:
  5743.             x_lis = 0;
  5744.             break;
  5745. #ifndef CK_TTGWSIZ
  5746.           case DEL_PAG:
  5747.             x_pag = 1;
  5748.             break;
  5749.           case DEL_NOP:
  5750.             x_pag = 0;
  5751.             break;
  5752. #endif /* CK_TTGWSIZ */
  5753.           case DEL_QUI:
  5754.             x_lis = 0;
  5755.             break;
  5756.           case DEL_VRB:
  5757.             x_lis = 1;
  5758.             break;
  5759.           case DEL_ASK:
  5760.             x_ask = 1;
  5761.             break;
  5762.           case DEL_NAS:
  5763.             x_ask = 0;
  5764.             break;
  5765.           default:
  5766.             printf("?Sorry, this option can not be set\n");
  5767.             return(-9);
  5768.         }
  5769.     }
  5770.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  5771.       return(x);
  5772.     if (x_pag > -1) del_pag = x_pag;
  5773.     if (x_dot > -1) del_dot = x_dot;
  5774.     if (x_hdg > -1) del_hdg = x_hdg;
  5775.     if (x_lis > -1) del_lis = x_lis;
  5776.     if (x_ask > -1) del_ask = x_ask;
  5777.     return(success = 1);
  5778. }
  5779.  
  5780. #ifdef OS2
  5781. static char ** xmtchs = NULL;
  5782. static int xmtchn = 0;
  5783. #endif /* OS2 */
  5784.  
  5785. int
  5786. dodel() {                               /* DELETE */
  5787.     int i, j, k, x;
  5788.     int fs = 0;                         /* Need to call fileselect() */
  5789.     int len = 0;
  5790.     int bad = 0;
  5791.     int getval = 0, asking = 0;
  5792.     int simulate = 0, rc = 0;
  5793.     long minsize = -1L, maxsize = -1L;
  5794.     int havename = 0, confirmed = 0;
  5795.     int qflag = 0;
  5796.     int summary = 0;
  5797.     int deldirs = 0;
  5798.     int deltree = 0;
  5799.     int itsadir = 0;
  5800.     int argisdir = 0;
  5801.     int xmode = -1, scan = 0, skip = 0;
  5802. #ifdef COMMENT
  5803.     int pass = 0;
  5804. #endif /* COMMENT */
  5805.     char c;
  5806.     char * deldef = "";
  5807.     char safebuf[CKMAXPATH+1];
  5808.     struct FDB sw, fi, fl;
  5809.     char
  5810.       * del_aft = NULL,
  5811.       * del_bef = NULL,
  5812.       * del_naf = NULL,
  5813.       * del_nbf = NULL,
  5814.       * del_exc = NULL;
  5815.     int
  5816.       x_lis = 0,
  5817.       /* x_dot = -1, */
  5818.       x_hdg = 0;
  5819.  
  5820.     char * dxlist[8];
  5821.  
  5822.     for (i = 0; i < 8; i++) dxlist[i] = NULL;
  5823.  
  5824.     g_matchdot = matchdot;
  5825.  
  5826.     if (del_lis > -1) x_lis    = del_lis;
  5827.     if (del_dot > -1) matchdot = del_dot;
  5828.     if (del_hdg > -1) x_hdg    = del_hdg;
  5829.     if (del_pag > -1) xaskmore = del_pag;
  5830.     if (del_ask > -1) asking   = del_ask;
  5831.  
  5832.     diractive = 1;
  5833.     nolinks = 2;                        /* By default don't follow links */
  5834.  
  5835.     cmfdbi(&sw,                         /* First FDB - command switches */
  5836.            _CMKEY,                      /* fcode */
  5837.            "File specification;\n or switch",
  5838.            "",                          /* default */
  5839.            "",                          /* addtl string data */
  5840.            ndeltab,                     /* addtl numeric data 1: tbl size */
  5841.            4,                           /* addtl numeric data 2: 4 = cmswi */
  5842.            xxstring,                    /* Processing function */
  5843.            deltab,                      /* Keyword table */
  5844.            &fi                          /* Pointer to next FDB */
  5845.            );
  5846.     cmfdbi(&fl,                         /* Anything that doesn't match */
  5847.            _CMFLD,                      /* fcode */
  5848.            "",                          /* hlpmsg */
  5849.            "",                          /* default */
  5850.            "",                          /* addtl string data */
  5851.            0,                           /* addtl numeric data 1 */
  5852.            0,                           /* addtl numeric data 2 */
  5853.            xxstring,
  5854.            NULL,
  5855.            NULL
  5856.            );
  5857.   again:
  5858.     cmfdbi(&fi,                         /* 2nd FDB - file to delete */
  5859.            _CMIFI,                      /* fcode */
  5860.            "File(s) to delete",         /* hlpmsg */
  5861.            deldef,                      /* default */
  5862.            "",                          /* addtl string data */
  5863.            nolinks | deldirs,           /* 0 = files, 1 = files or dirs */
  5864.            0,                           /* 1 = dirs only */
  5865.            xxstring,
  5866.            NULL,
  5867.            &fl
  5868.            );
  5869.     while (!havename && !confirmed) {
  5870.         x = cmfdb(&sw);                 /* Parse something */
  5871.         if (x < 0) {                    /* Error */
  5872.             if (x == -3)
  5873.               break;
  5874.             if (x == -2 || x == -9)
  5875.               printf("?Does not match switch or filename: \"%s\"\n",atmbuf);
  5876.             return(x);
  5877.         }
  5878.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  5879.           break;
  5880.         c = cmgbrk();                   /* Get break character */
  5881.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5882.             printf("?This switch does not take an argument\n");
  5883.             rc = -9;
  5884.             goto xdelete;
  5885.         }
  5886.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  5887.             printf("?This switch requires an argument\n");
  5888.             rc = -9;
  5889.             goto xdelete;
  5890.         }
  5891.         switch (k = cmresult.nresult) {
  5892.           case DEL_AFT:
  5893.           case DEL_BEF:
  5894.           case DEL_NAF:
  5895.           case DEL_NBF:
  5896.             if (!getval) break;
  5897.             if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  5898.                 if (x == -3) {
  5899.                     printf("?Date-time required\n");
  5900.                     x = -9;
  5901.                 } else
  5902.                   rc = x;
  5903.                 goto xdelete;
  5904.             }
  5905.             fs++;
  5906.             deltree = 0;
  5907.             switch (k) {
  5908.               case DEL_AFT: makestr(&del_aft,s); break;
  5909.               case DEL_BEF: makestr(&del_bef,s); break;
  5910.               case DEL_NAF: makestr(&del_naf,s); break;
  5911.               case DEL_NBF: makestr(&del_nbf,s); break;
  5912.             }
  5913.             break;
  5914.           case DEL_DOT:
  5915.             matchdot = 1;
  5916.             break;
  5917.           case DEL_NOD:
  5918.             matchdot = 0;
  5919.             break;
  5920.           case DEL_ALL:
  5921.             fs = 0;
  5922. #ifdef VMS
  5923.             deldef = "*.*";             /* UNIX, Windows, OS/2 */
  5924. #else
  5925. #ifdef datageneral
  5926.             deldef = "+";               /* AOS/VS */
  5927. #else
  5928.             deldef = "*";               /* UNIX, Windows, OS/2, VMS... */
  5929. #endif /* datageneral */
  5930. #endif /* VMS */
  5931.             deltree = 1;
  5932.             nolinks = 2;
  5933.             matchdot = 1;
  5934.             recursive = 1;              /* Fall through purposely... */
  5935.           case DEL_DIR:
  5936.             deldirs = 1;
  5937.             goto again;
  5938.           case DEL_EXC:
  5939.             if (!getval) break;
  5940.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5941.                 if (x == -3) {
  5942.                     printf("?Pattern required\n");
  5943.                     x = -9;
  5944.                 } else
  5945.                   rc = x;
  5946.                 goto xdelete;
  5947.             }
  5948.             fs++;
  5949.             deltree = 0;
  5950.             makestr(&del_exc,s);
  5951.             break;
  5952.           case DEL_HDG:
  5953.             x_hdg = 1;
  5954.             break;
  5955. #ifdef RECURSIVE
  5956.           case DEL_REC:
  5957.             recursive = 1;
  5958.             break;
  5959. #endif /* RECURSIVE */
  5960.           case DEL_LIS:
  5961.             x_lis = 1;
  5962.             break;
  5963.           case DEL_SUM:
  5964.             summary = 1;
  5965.             x_lis = 0;
  5966.             x_hdg = 1;
  5967.             break;
  5968.           case DEL_NOL:
  5969.             x_lis = 0;
  5970.             break;
  5971. #ifndef CK_TTGWSIZ
  5972.           case DEL_PAG:
  5973.             xaskmore = 1;
  5974.             break;
  5975.           case DEL_NOP:
  5976.             xaskmore = 0;
  5977.             break;
  5978. #endif /* CK_TTGWSIZ */
  5979.           case DEL_QUI:
  5980.             qflag = 1;
  5981.             x_lis = 0;
  5982.             break;
  5983.           case DEL_VRB:
  5984.             x_lis = 1;
  5985.             break;
  5986.  
  5987.           case DEL_SMA:
  5988.           case DEL_LAR:
  5989.             if (!getval) break;
  5990.             if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  5991.               return(x);
  5992.             fs++;
  5993.             deltree = 0;
  5994.             switch (cmresult.nresult) {
  5995.               case DEL_SMA: minsize = y; break;
  5996.               case DEL_LAR: maxsize = y; break;
  5997.             }
  5998.             break;
  5999.  
  6000.           case DEL_SIM:
  6001.             simulate = 1;
  6002.             x_lis = 1;
  6003.             break;
  6004.           case DEL_ASK:
  6005.             asking = 1;
  6006.             break;
  6007.           case DEL_NAS:
  6008.             asking = 0;
  6009.             break;
  6010.           case DEL_TYP: {
  6011.               extern struct keytab txtbin[];
  6012.               if (!getval) break;
  6013.               if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  6014.                 return(x);
  6015.               if (x == 2) {             /* ALL */
  6016.                   xmode = -1;
  6017.               } else {                  /* TEXT or BINARY only */
  6018.                   xmode = x;
  6019.                   scan = 1;
  6020.               }
  6021.               break;
  6022.           }
  6023.           default:
  6024.             printf("?Not implemented yet - \"%s\"\n",atmbuf);
  6025.             return(-9);
  6026.         }
  6027.     }
  6028.     if (qflag && (cmresult.fcode == _CMFLD)) {
  6029.         if ((x = cmcfm()) < 0)
  6030.           return(x);
  6031.         else
  6032.           return(success = 0);
  6033.     }
  6034.     if (cmresult.fcode != _CMIFI) {
  6035.         if (*atmbuf) {
  6036.         int x;
  6037.             if (iswild(atmbuf) && nzxpand(atmbuf,nzxopts) == 0)
  6038.               printf("?No files match: %s\n",atmbuf);
  6039.             else if ((x = zchki(atmbuf)) == -1)
  6040.           printf("?File not found: %s\n",atmbuf);
  6041.         else if (x == -2)
  6042.           printf("?Not a regular file: %s\n",atmbuf);
  6043.         else
  6044.               /* printf("?Not a deletable file: %s\n",atmbuf); */
  6045.               goto tryanyway;
  6046.         } else {
  6047.             printf("?A file specification is required\n");
  6048.         }
  6049.         return(-9);
  6050.     }
  6051.   tryanyway:
  6052.     ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ); /* Safe copy of filespec */
  6053.     if (deldirs) {
  6054.         ckstrncpy(safebuf,cmresult.sresult,CKMAXPATH);
  6055. #ifdef VMSORUNIX
  6056.         len = zgetfs(tmpbuf);           /* Is it a directory name? */
  6057.         argisdir = zgfs_dir;            /* Then because of how zxpand() */
  6058.         if (argisdir && zgfs_link)      /* works, we have to add it to */
  6059.           argisdir = 0;                 /* the list. */
  6060.         if (itsadir)
  6061.           len = -2;
  6062. #else
  6063.         len = zchki(tmpbuf);
  6064.         if (len < 0)
  6065.           argisdir = isdir(tmpbuf);
  6066. #endif /* VMSORUNIX */
  6067.     }
  6068.     debug(F110,"DELETE file",tmpbuf,0);
  6069.     if ((x = cmcfm()) < 0)
  6070.       return(x);
  6071.  
  6072. #ifdef IKSD
  6073. #ifdef CK_LOGIN
  6074.     if (inserver && isguest) {
  6075.         printf("?Sorry, DELETE unavailable to guests\n");
  6076.         return(-9);
  6077.     }
  6078. #endif /* CK_LOGIN */
  6079. #endif /* IKSD */
  6080.  
  6081. #ifndef OS2ORUNIX
  6082.     if (simulate) {
  6083.         printf("?Sorry, /SIMULATE not implemented on this platform\n");
  6084.         return(-9);
  6085.     }
  6086. #endif /* OS2ORUNIX */
  6087.  
  6088. #ifdef COMMENT
  6089.     /* (not needed) */
  6090.     if (!iswild(tmpbuf)) {
  6091.         char *m;
  6092.         errno = 0;
  6093.         x = zchki(tmpbuf);
  6094.         if (x < 0) {
  6095.             switch (x) {
  6096.               case -2: m = "Not a regular file"; break;
  6097.               case -1: m = "File not found or not accessible"; break;
  6098.               default: m = errno ? ck_errstr() : "Can't delete";
  6099.             }
  6100.             printf("?%s: \"%s\"\n",m,tmpbuf);
  6101.             return(-9);
  6102.         }
  6103.     }
  6104. #endif /* COMMENT */
  6105.  
  6106.     makelist(del_exc,dxlist,8);
  6107.  
  6108. /* tmpbuf[] has the name - now do any needed conversions on it */
  6109.  
  6110. #ifdef OS2
  6111.     {   /* Lower level functions change / to \, not good for CMD.EXE. */
  6112.         char *p = tmpbuf;
  6113.         while (*p) {                    /* Change them back to \ */
  6114.             if (*p == '/') *p = '\\';
  6115.             p++;
  6116.         }
  6117.     }
  6118. #endif /* OS2 */
  6119.  
  6120. #ifdef VMS
  6121.     if (iswild(tmpbuf)) {
  6122. #ifdef COMMENT
  6123.         /* Does not handle '.' as version separator */
  6124.         char *p = tmpbuf;
  6125.         x = 0;
  6126.         while (*p) {
  6127.             if (*p == ';') {
  6128.                 x = 1;
  6129.                 break;
  6130.             } else
  6131.               p++;
  6132.         }
  6133.         if (!x) ckstrncat(tmpbuf,";*",TMPBUFSIZ);
  6134. #else
  6135.         j = 0; x = 0;                   /* for end_dot and number of dots */
  6136.         i = strlen(tmpbuf);
  6137.         if (tmpbuf[i] == ';') {
  6138.             ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  6139.         } else {
  6140.             if (tmpbuf[i--] == '.')
  6141.               j++;
  6142.             for (; i >= 0; i--) {
  6143.                 if (tmpbuf[i] == ';' || tmpbuf[i] == ':' ||
  6144.                     tmpbuf[i] == ']' || tmpbuf[i] == '>')
  6145.                   break;
  6146.                 else if (tmpbuf[i] == '.')
  6147.                   x++;
  6148.             }
  6149.             if (tmpbuf[i] != ';') {     /* dot may have been used */
  6150.                 if (j) {                /* last char is dot */
  6151.                   if (x)                /* second is version separator */
  6152.                     ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  6153.                   else                  /* 'foo.' */
  6154.                     ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  6155.                 } else if (x == 1)      /* lacking a version separator */
  6156.                   ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  6157.                 else if (x == 0)        /* x == 2 has a version */
  6158.                   ckstrncat(tmpbuf,".*;0",TMPBUFSIZ);
  6159.             }
  6160.         }
  6161. #endif /* COMMENT */
  6162.     }
  6163. #endif /* VMS */
  6164.  
  6165.     debug(F110,"dodel tmpbuf",tmpbuf,0); /* Filename */
  6166.  
  6167. #ifndef OS2ORUNIX                       /* No built-in DELETE code... */
  6168.     /* Construct system command. */
  6169.     ckmakmsg(line,LINBUFSIZ,DELCMD," ",tmpbuf,NULL);
  6170. #else
  6171. #ifdef VMS
  6172.     if (asking) {                       /* Maybe overwrite in VMS */
  6173.         if (x_lis)                      /* if options are needed... */
  6174.           ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm/log ",tmpbuf,NULL);
  6175.         else
  6176.           ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm ",tmpbuf,NULL);
  6177.     } else if (x_lis)
  6178.       ckmakmsg(line,LINBUFSIZ,DELCMD," /log ",tmpbuf,NULL);
  6179.     conres();
  6180. #endif /* VMS */
  6181.  
  6182.     debug(F110,"dodel line",line,0);
  6183. #endif /* OS2ORUNIX */
  6184.  
  6185. #ifdef MAC
  6186.     success = (zdelet(tmpbuf) == 0);
  6187.  
  6188. #else  /* !MAC ... */
  6189.  
  6190. #ifdef OS2ORUNIX
  6191.     {
  6192.         int filespace = 0;
  6193.         int count = 0;
  6194.         int lines = 0;
  6195.         int n = 0;
  6196.  
  6197.         s = tmpbuf;
  6198.  
  6199. #ifdef CK_TTGWSIZ
  6200. #ifdef OS2
  6201.         ttgcwsz();
  6202. #else /* OS2 */
  6203.         /* Check whether window size changed */
  6204.         if (ttgwsiz() > 0) {
  6205.             if (tt_rows > 0 && tt_cols > 0) {
  6206.                 cmd_rows = tt_rows;
  6207.                 cmd_cols = tt_cols;
  6208.             }
  6209.         }
  6210. #endif /* OS2 */
  6211. #endif /* CK_TTGWSIZ */
  6212.  
  6213.         if (x_hdg > 0 && !summary) {
  6214.             printf("Deleting %s...%s\n", s, simulate ? " (SIMULATION)" : "");
  6215.             n += 2;
  6216.         }
  6217. #ifdef ZXREWIND
  6218.         z = zxrewind();                 /* Rewind file list */
  6219. #else
  6220.         if (!deldirs)
  6221.           nzxopts = ZX_FILONLY;
  6222.         if (recursive) nzxopts |= ZX_RECURSE;
  6223.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  6224.         errno = 0;
  6225.         z = nzxpand(s,nzxopts);         /* Expand file list */
  6226. #endif /* ZXREWIND */
  6227.         debug(F111,"dodel",s,z);
  6228.  
  6229.         /* If deleting directories, sort in reverse order */
  6230.         /* so we delete the files first, then the directory. */
  6231.  
  6232. #ifdef OS2
  6233.         /* In K95, we have no mtchs array, nor any control over */
  6234.         /* the order in which znext() returns filenames, so we  */
  6235.         /* must copy the array and sort it. */
  6236.         {
  6237.             int i;
  6238.             if (xmtchs) {               /* Free previous list in case */
  6239.                 debug(F101,"dodel freeing previous list","",xmtchn);
  6240.                 for (i = 0; i < xmtchn; i++) /* it wasn't freed last time. */
  6241.                   if (xmtchs[i])
  6242.                     free(xmtchs[i]);
  6243.                 free(xmtchs);
  6244.             }
  6245.             xmtchn = 0;
  6246.             xmtchs = (char **)malloc(z * (sizeof(char **))); /* Make new one */
  6247.             if (!xmtchs) {
  6248.                 printf("?Memory allocation failure\n");
  6249.                 return(-9);
  6250.             }
  6251.             for (i = 0; i < z; i++) {
  6252.                 xmtchs[i] = NULL;
  6253.                 znext(tmpbuf);
  6254.                 if (!*tmpbuf)
  6255.                   break;
  6256.                 makestr(&(xmtchs[i]),tmpbuf);
  6257.                 if (!xmtchs[i]) {
  6258.                     printf("?Memory allocation failure\n");
  6259.                     xmtchn = i - 1;
  6260.                     rc = -9;
  6261.                     goto xdelete;
  6262.                 }
  6263.                 /* debug(F111,"dodel add",xmtchs[i],i); */
  6264.             }
  6265.             xmtchn = i;
  6266.             debug(F101,"dodel xmtchn","",xmtchn);
  6267.             sh_sort(xmtchs,NULL,z,0,deldirs,0);
  6268.         }
  6269. #else
  6270. #ifdef UNIX
  6271.         sh_sort(mtchs,NULL,z,0,deldirs,filecase);
  6272. #endif /* UNIX */
  6273. #endif /* OS2 */
  6274.  
  6275.         if (z > 0) {
  6276.             int i;
  6277. #ifdef OS2
  6278.             int ix = 0;
  6279. #endif /* OS2 */
  6280.             success = 1;
  6281.             if (x_hdg > 0)
  6282.               printf("\n");
  6283.  
  6284.             while (
  6285. #ifdef OS2
  6286.                    ix < xmtchn
  6287. #else
  6288.                    1
  6289. #endif /* OS2 */
  6290.                    ) {                  /* Loop for all files */
  6291. #ifdef OS2
  6292.                 ckstrncpy(tmpbuf,xmtchs[ix++],TMPBUFSIZ);
  6293. #else
  6294.                 znext(tmpbuf);          /* Get next file */
  6295. #endif /* OS2 */
  6296.                 if (!*tmpbuf) {         /* No more */
  6297.                     if (deldirs && recursive && argisdir) {
  6298.                         ckstrncpy(tmpbuf,safebuf,TMPBUFSIZ);
  6299.                         argisdir = 0;   /* (only do this once) */
  6300.                     } else
  6301.                       break;
  6302.                 }
  6303.                 skip = 0;
  6304.                 if (!deltree) {
  6305.                     if (fs)
  6306.                       if (fileselect(tmpbuf,
  6307.                                      del_aft,del_bef,del_naf,del_nbf,
  6308.                                      minsize,maxsize,0,8,dxlist) < 1) {
  6309.                           skip++;
  6310.                       }
  6311.                 }
  6312.                 if (!skip && scan && itsadir) {
  6313.                     skip++;
  6314.                 }
  6315.                 if (!skip && scan) {
  6316.                       switch (scanfile(tmpbuf,&y,nscanfile)) {
  6317.                       case FT_BIN:
  6318.                         if (xmode != 1)
  6319.                           skip++;
  6320.                         break;
  6321.                       case FT_TEXT:
  6322.                       case FT_7BIT:
  6323.                       case FT_8BIT:
  6324. #ifdef UNICODE
  6325.                       case FT_UTF8:
  6326.                       case FT_UCS2:
  6327. #endif /* UNICODE */
  6328.                         if (xmode != 0)
  6329.                           skip++;
  6330.                     }
  6331.                 }
  6332.                 if (!skip && asking) {
  6333.                     int x;
  6334.                     ckmakmsg(line,LINBUFSIZ," Delete ",tmpbuf,"? ",NULL);
  6335.                     x = getyesno(line,3);
  6336.                     switch (x) {
  6337.                       case 0: continue;           /* no */
  6338.                       case 1: break;              /* yes */
  6339.                       case 2: goto xdelete;       /* quit */
  6340.                       case 3: asking = 0; break;  /* go */
  6341.                     }
  6342.                 }
  6343. #ifdef VMSORUNIX
  6344.                 len = zgetfs(tmpbuf);   /* Get length and accessibility */
  6345.                 itsadir = zgfs_dir;
  6346.                 if (itsadir && zgfs_link) { /* Treat links to directories */
  6347.                     itsadir = 0;        /* as regular files */
  6348.                     if (scan)           /* But not if /TYPE: was given */
  6349.                       skip++;
  6350.                 }
  6351.                 if (itsadir)            /* (emulate non-Unix code) */
  6352.                   len = -2;
  6353. #else
  6354.                 len = zchki(tmpbuf);    /* Get accessibility */
  6355.                 if (len < 0)            /* See if it's a directory */
  6356.                   itsadir = isdir(tmpbuf);
  6357. #endif /* VMSORUNIX */
  6358.  
  6359.                 if (skip) {
  6360. #ifdef COMMENT                          /* Too verbose */
  6361.                     if (x_lis > 0) {
  6362.                         lines++;
  6363.                         printf(" %s (SKIPPED)\n",tmpbuf);
  6364. #ifdef CK_TTGWSIZ
  6365.                         if (++n > cmd_rows - 3)
  6366.                           if (!askmore()) goto xdelete; else n = 0;
  6367. #endif /* CK_TTGWSIZ */
  6368.                     }
  6369. #endif /* COMMENT */
  6370.                     continue;
  6371.                 }
  6372.  
  6373.                 debug(F111,"DELETE len",tmpbuf,len);
  6374.                 if (simulate) {
  6375.                     filespace += len;
  6376.                     count++;
  6377.                     if (x_lis > 0) {
  6378.                         lines++;
  6379.                         printf(" %s (SELECTED)\n",tmpbuf);
  6380.                         if (++n > cmd_rows - 3) {
  6381.                             int xx;
  6382.                             xx = askmore();
  6383.                             if (!xx) goto xdelete; else n = 0;
  6384.                         }
  6385.                     }
  6386.                 } else if (len >= 0 || !itsadir) { /* Regular file */
  6387.                     zdelet(tmpbuf);                /* or symlink, etc... */
  6388.                     if (zchki(tmpbuf) < 0) {
  6389.                         filespace += len;
  6390.                         count++;
  6391.                         if (x_lis > 0) {
  6392.                             lines++;
  6393.                             printf(" %s (OK)\n",tmpbuf);
  6394.                             if (++n > cmd_rows - 3)
  6395.                               if (!askmore()) goto xdelete; else n = 0;
  6396.                         }
  6397.                     } else {
  6398.                         bad++;
  6399.                         success = 0;
  6400.                         if (x_lis > 0) {
  6401.                             lines++;
  6402.                             printf(" %s (FAILED: %s)\n",tmpbuf,ck_errstr());
  6403.                             if (++n > cmd_rows - 3)
  6404.                               if (!askmore()) goto xdelete; else n = 0;
  6405.                         }
  6406.                     }
  6407.                 } else if (/* pass > 0 && */ deldirs && itsadir) {
  6408.                     /* It's a directory */
  6409.                     if (zrmdir(tmpbuf) > -1) { /* Only works if empty */
  6410.                         count++;
  6411.                         if (x_lis > 0) {
  6412.                             lines++;
  6413.                             printf(" %s (OK)\n",tmpbuf);
  6414.                             if (++n > cmd_rows - 3)
  6415.                               if (!askmore()) goto xdelete; else n = 0;
  6416.                         }
  6417.                     } else {
  6418.                         success = 0;
  6419.                         if (x_lis > 0) {
  6420.                             lines++;
  6421.                             printf(" %s (FAILED: %s)\n",
  6422.                                    tmpbuf,
  6423.                                    ck_errstr());
  6424.                             if (++n > cmd_rows - 3)
  6425.                               if (!askmore()) goto xdelete; else n = 0;
  6426.                         }
  6427.                     }
  6428.                 } else if (x_lis > 0) {
  6429.                     lines++;
  6430.                     if (isdir(tmpbuf))
  6431.                       printf(" %s (FAILED: directory)\n",tmpbuf);
  6432.                     else
  6433.                       printf(" %s (FAILED: not a regular file)\n",tmpbuf);
  6434.                     if (++n > cmd_rows - 3)
  6435.                       if (!askmore()) goto xdelete; else n = 0;
  6436.                 }
  6437.             }
  6438.             if (x_hdg > 0) {
  6439.                 if (lines > 0)
  6440.                   printf("\n");
  6441.                 if (++n > cmd_rows - 3)
  6442.                   if (!askmore()) goto xdelete; else n = 0;
  6443.                 printf("%d file%s %sdeleted, %d byte%s %sfreed%s\n",
  6444.                        count,
  6445.                        count != 1 ? "s" : "",
  6446.                        simulate ? "would be " : "",
  6447.                        filespace,
  6448.                        filespace != 1 ? "s" : "",
  6449.                        simulate ? "would be " : "",
  6450.                        simulate ? " (maybe)" : ""
  6451.                        );
  6452.             }
  6453.             if (!x_lis && !success && !quiet) {
  6454.                 printf("?DELETE failed for %d file%s \
  6455. (use DELETE /LIST to see details)\n",
  6456.                        bad, bad == 1 ? "" : "s"
  6457.                        );
  6458.             }
  6459.         } else if (x_lis > 0) {
  6460.             if (errno)
  6461.               printf("?%s: %s\n",ck_errstr(), tmpbuf);
  6462.             else
  6463.               printf("?Can't delete: %s\n",tmpbuf);
  6464.         }
  6465.     }
  6466. #else /* OS2ORUNIX */
  6467. #ifndef VMS                             /* Others - let the system do it. */
  6468.     xsystem(line);
  6469.     x = nzxpand(tmpbuf,nzxopts);
  6470.     success = (x > 0) ? 0 : 1;
  6471.     if (x_hdg > 0)
  6472.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6473. #else
  6474.     if (asking)
  6475.       printf("\n");
  6476.     x = xsystem(line);                  /* zshcmd returns 1 for success */
  6477.     success = (x > 0) ? 1 : 0;
  6478.     if (x_hdg > 0 && !asking)
  6479.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6480.     concb((char)escape);
  6481. #endif /* VMS */
  6482. #endif /* OS2ORUNIX */
  6483. #endif /* MAC */
  6484.   xdelete:
  6485.     if (g_matchdot > -1) {
  6486.         matchdot = g_matchdot;          /* Restore these... */
  6487.         g_matchdot = -1;
  6488.     }
  6489. #ifdef OS2
  6490.     if (xmtchs) {
  6491.         int i;
  6492.         debug(F101,"dodel freeing list","",xmtchn);
  6493.         for (i = 0; i < xmtchn; i++)
  6494.           if (xmtchs[i]) free(xmtchs[i]);
  6495.         free(xmtchs);
  6496.         xmtchs = NULL;
  6497.         xmtchn = 0;
  6498.     }
  6499. #endif /* OS2 */
  6500.     debug(F101,"dodel result","",rc);
  6501.     return((rc < 0) ? rc : success);
  6502. }
  6503. #endif /* NOFRILLS */
  6504.  
  6505. #ifndef NOSPL                           /* The ELSE command */
  6506. _PROTOTYP( VOID pushqcmd, (char *) );
  6507.  
  6508. int
  6509. doelse() {
  6510.     if (!ifcmd[cmdlvl]) {
  6511.         printf("?ELSE doesn't follow IF\n");
  6512.         return(-2);
  6513.     }
  6514. #ifdef COMMENT
  6515. /*
  6516.   Wrong.  This prevents IF..ELSE IF...ELSE IF...ELSE IF...ELSE...
  6517.   from working.
  6518. */
  6519.     ifcmd[cmdlvl] = 0;
  6520. #endif /* COMMENT */
  6521.     if (!iftest[cmdlvl]) {              /* If IF was false do ELSE part */
  6522.         if (maclvl > -1 || tlevel > -1) { /* In macro or command file */
  6523.             debug(F100,"doelse pushing","",0);
  6524. #ifdef COMMENT
  6525.             pushcmd(NULL);              /* save rest of command. */
  6526. #else
  6527.             /* This fixes certain obscure problems */
  6528.             /* but breaks many other constructions that must work. */
  6529.             pushqcmd(NULL);
  6530. #endif /* COMMENT */
  6531.         } else {                        /* If interactive, */
  6532.             cmini(ckxech);              /* just start a new command */
  6533.             printf("\n");               /* (like in MS-DOS Kermit) */
  6534.             if (pflag) prompt(xxstring);
  6535.         }
  6536.     } else {                            /* Condition is false */
  6537.         if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  6538.           return(y);                    /* Gobble up rest of line */
  6539.     }
  6540.     return(0);
  6541. }
  6542. #endif /* NOSPL */
  6543.  
  6544. #ifndef NOSPL
  6545. int
  6546. doswitch() {
  6547.     char *lp, *ap;                      /* Macro argument pointer */
  6548.     int len = 0, x, y, pp = 0;
  6549.     char brbuf[3];
  6550.  
  6551.     /* Get variable name */
  6552.  
  6553.     tmpbuf[0] = NUL;
  6554.     brbuf[0] = '{';
  6555.     brbuf[1] = '}';
  6556.     brbuf[2] = NUL;
  6557.  
  6558.     y = cmfld("Variable name","",&s,xxstring);
  6559.     debug(F111,"doswitch cmfld",s,y);
  6560.     if (y < 0) {
  6561.         if (y == -3)            /* Because brstrip() writes */
  6562.           s = brbuf;            /* into its argument. */
  6563.         else
  6564.           return(y);
  6565.     }
  6566.     debug(F110,"doswitch A",s,0);
  6567.     if (!strcmp(s,"(")) {
  6568.         pp++;
  6569.         if ((y = cmfld("Variable name","",&s,xxstring)) < 0) {
  6570.             if (y == -3)
  6571.               s = brbuf;
  6572.             else
  6573.               return(y);
  6574.         debug(F110,"doswitch B",s,0);
  6575.         }
  6576.     }
  6577.     len = ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  6578.     if (tmpbuf[0] == CMDQ) {
  6579.         if (chkvar(s) < 1) {
  6580.             printf("?Variable name required\n");
  6581.             return(-9);
  6582.         }
  6583.     }
  6584.     if (pp > 0) {                       /* If open paren given parse closing */
  6585.         if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6586.           return(y);
  6587.         if (strcmp(atmbuf,")")) {
  6588.             printf("?Closing parenthesis required\n");
  6589.             return(-9);
  6590.         }
  6591.     }
  6592.     lp = line;
  6593.     x = ckstrncpy(lp,"_switx ",LINBUFSIZ); /* _switx + space */
  6594.     lp += x;
  6595.     ap = lp;
  6596.     debug(F010,"SWITCH a",line,0);
  6597.  
  6598. #ifdef COMMENT
  6599.     x = ckmakmsg(lp,LINBUFSIZ-x,tmpbuf," ",NULL,NULL); /* variable name + SP */
  6600. #else
  6601.     {                                   /* variable name + SP */
  6602.         char * p = tmpbuf;
  6603.     if (len > 0) {
  6604.         if (tmpbuf[0] == '(' && tmpbuf[len-1] == ')') {
  6605.         tmpbuf[len-1] = NUL;
  6606.         p++;
  6607.         }
  6608.         }
  6609.         x = ckmakmsg(lp,LINBUFSIZ-x,"{",brstrip(p),"}"," ");
  6610.     }
  6611. #endif /* COMMENT */
  6612.     debug(F010,"SWITCH b",line,0);
  6613.     lp += x;
  6614.  
  6615.     /* Get body */
  6616.  
  6617.     if ((y = cmtxt("series of cases","",&s,NULL)) < 0) return(y);
  6618.     if ((y = (int)strlen(s)) < 1) return(-2);
  6619.     if (s[0] != '{' && s[y-1] != '}') { /* Supply braces if missing */
  6620.         ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  6621.         s = tmpbuf;
  6622.     }
  6623.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  6624.         printf("?Unbalanced braces\n");
  6625.         return(0);
  6626.     }
  6627.     debug(F010,"SWITCH c",line,0);
  6628.  
  6629.     x = mlook(mactab,"_switx",nmac);    /* Look up SWITCH macro definition */
  6630.     if (x < 0) {                        /* Not there? */
  6631.         addmmac("_switx",sw_def);       /* Put it back. */
  6632.         if ((x = mlook(mactab,"_switx",nmac)) < 0) { /* Look it up again. */
  6633.             printf("?SWITCH macro definition gone!\n"); /* Shouldn't happen. */
  6634.             return(success = 0);
  6635.         }
  6636.     }
  6637.     debug(F010,"SWITCH command",line,0); /* Execute the SWITCH macro. */
  6638.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  6639. }
  6640.  
  6641. int
  6642. dofor() {                               /* The FOR command. */
  6643.     int i, fx, fy, fz;                  /* loop variables */
  6644.     char *ap, *di;                      /* macro argument pointer */
  6645.     int pp = 0;                         /* Paren level */
  6646.     int mustquote = 0;
  6647.  
  6648.     for (i = 0; i < 2; i++) {
  6649.         if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  6650.             if (y == -3) {
  6651.                 printf("?Variable name required\n");
  6652.                 return(-9);
  6653.             } else
  6654.               return(y);
  6655.         }
  6656.         if (strcmp(s,"("))
  6657.           break;
  6658.         pp++;
  6659.     }
  6660. #ifdef COMMENT
  6661.     if ((y = parsevar(s,&x,&z)) < 0)    /* Check variable. */
  6662.       return(y);
  6663. #else
  6664.     if (*s == CMDQ)                     /* If loop variable starts with */
  6665.       mustquote++;                      /* backslash, mustquote is > 0. */
  6666. #endif /* COMMENT */
  6667.  
  6668.     lp = line;                          /* Build a copy of the command */
  6669.     ckstrncpy(lp,"_forx ",LINBUFSIZ);
  6670.     lp += (int)strlen(line);            /* "_for" macro. */
  6671.     ap = lp;                            /* Save pointer to macro args. */
  6672.  
  6673.     if (*s == CMDQ) s++;                /* Skip past backslash if any. */
  6674.     while ((*lp++ = *s++)) ;            /* copy it */
  6675.     lp--; *lp++ = SP;                   /* add a space */
  6676.  
  6677.     if ((y = cmnum("initial value","",10,&fx,xxstring)) < 0) {
  6678.         if (y == -3) return(-2);
  6679.         else return(y);
  6680.     }
  6681.     debug(F101,"dofor fx","",fx);
  6682.     s = atmbuf;                         /* Copy the atom buffer */
  6683.  
  6684.     if ((int)strlen(s) < 1) goto badfor;
  6685. /*
  6686.   In edit 192, we change the loop variables to be evaluated at loop entry,
  6687.   not each time through the loop.  This was required in order to allow
  6688.   \v(argc) to be used as a loop variable, or in a loop-variable expression.
  6689.   Thus, we can't have FOR loops that modify their own exit conditions by
  6690.   changing the final value or the increment.  The problem with \v(argc) was
  6691.   that it is on the macro stack; after entry into the _forx macro, it is at
  6692.   the wrong place.
  6693. */
  6694.     sprintf(tmpbuf,"%d",fx);            /* (SAFE) Substitute actual value */
  6695.     s = tmpbuf;
  6696.     while ((*lp++ = *s++)) ;            /* (what they actually typed) */
  6697.     lp--; *lp++ = SP;
  6698. #ifdef DEBUG
  6699.     *lp = NUL;
  6700.     debug(F110,"FOR A",line,0);
  6701. #endif /* DEBUG */
  6702.  
  6703.     if ((y = cmnum("final value","",10,&fy,xxstring)) < 0) {
  6704.         if (y == -3) return(-2);
  6705.         else return(y);
  6706.     }
  6707.     debug(F101,"dofor fy","",fy);
  6708.     s = atmbuf;                         /* Same deal */
  6709.     if ((int)strlen(s) < 1)
  6710.       goto badfor;
  6711.  
  6712.     sprintf(tmpbuf,"%d",fy);            /* SAFE */
  6713.     s = tmpbuf;
  6714.     while ((*lp++ = *s++)) ;
  6715.     lp--;
  6716.     *lp++ = SP;
  6717. #ifdef DEBUG
  6718.     *lp = NUL;
  6719.     debug(F110,"FOR B",line,0);
  6720. #endif /* DEBUG */
  6721.  
  6722.     x_ifnum = 1;                        /* Increment or parenthesis */
  6723.     di = (fx < fy) ? "1" : "-1";        /* Default increment */
  6724.     if ((y = cmnum("increment",di,10,&fz,xxstring)) < 0) {
  6725.         debug(F111,"dofor increment",atmbuf,y);
  6726.         x_ifnum = 0;
  6727.         if (y == -3) {                  /* Premature termination */
  6728.             return(-2);
  6729.         } else if (y == -2) {           /* Maybe closing paren */
  6730.             if (!strcmp(atmbuf,")")) {
  6731.                 pp--;                   /* Count it */
  6732.                 s = di;                 /* supply default interval */
  6733.                 fz = atoi(s);
  6734.             } else                      /* Not closing paren, invalid */
  6735.               return(y);
  6736.         } else                          /* Other error */
  6737.           return(y);
  6738.     } else {                            /* Number */
  6739.         x_ifnum = 0;
  6740.         debug(F101,"dofor fz","",fz);
  6741.         s = atmbuf;                     /* Use it */
  6742.     }
  6743.     if ((int)strlen(s) < 1)
  6744.       goto badfor;
  6745.  
  6746.     sprintf(tmpbuf,"%d",fz);            /* (SAFE) Same deal */
  6747.     s = tmpbuf;
  6748.     while ((*lp++ = *s++)) ;
  6749.     lp--; *lp++ = SP;
  6750.  
  6751. #ifdef DEBUG
  6752.     *lp = NUL;
  6753.     debug(F110,"FOR C",line,0);
  6754. #endif /* DEBUG */
  6755.  
  6756.     /* Insert the appropriate comparison operator */
  6757.     if (fz < 0)
  6758.       *lp++ = '<';
  6759.     else
  6760.       *lp++ = '>';
  6761.     *lp++ = SP;
  6762.  
  6763. #ifdef DEBUG
  6764.     *lp = NUL;
  6765.     debug(F110,"FOR D",line,0);
  6766. #endif /* DEBUG */
  6767.  
  6768.     if (pp > 0) {                       /* If open paren given parse closing */
  6769.         if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6770.           return(y);
  6771.         if (strcmp(atmbuf,")")) {
  6772.             printf("?Closing parenthesis required\n");
  6773.             return(-9);
  6774.         }
  6775.     }
  6776.     if ((y = cmtxt("Command to execute","",&s,NULL)) < 0) return(y);
  6777.     if ((y = (int)strlen(s)) < 1) return(-2);
  6778.     if (s[0] != '{' && s[y-1] != '}') { /* Supply braces if missing */
  6779.         ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  6780.         s = tmpbuf;
  6781.     }
  6782.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  6783.         printf("?Unbalanced braces\n");
  6784.         return(0);
  6785.     }
  6786. #ifdef DEBUG
  6787.     *lp = NUL;
  6788.     debug(F110,"FOR E",line,0);
  6789. #endif /* DEBUG */
  6790.  
  6791. #ifdef COMMENT
  6792. /* Too strict */
  6793.     if (fz == 0) {
  6794.         printf("?Zero increment not allowed\n");
  6795.         return(0);
  6796.     }
  6797. #endif /* COMMENT */
  6798. /*
  6799.   In version 8.0 we decided to allow macro names anyplace a numeric-valed
  6800.   variable could appear.  But this caused trouble for the FOR loops because
  6801.   the quoting in for_def[] assumed a \%i-style loop variable.  We account
  6802.   for this here in the if (mustquote)...else logic by invoking separate
  6803.   FOR macro definitions in the two cases.
  6804. */
  6805.     if (mustquote) {                    /* \%i-style loop variable */
  6806.         x = mlook(mactab,"_forx",nmac); /* Look up FOR macro definition */
  6807.         if (x < 0) {                    /* Not there? */
  6808.             addmmac("_forx",for_def);   /* Put it back. */
  6809.             if ((x = mlook(mactab,"_forx",nmac)) < 0) { /* Look it up again. */
  6810.                 printf("?FOR macro definition gone!\n");
  6811.                 return(success = 0);
  6812.             }
  6813.         }
  6814.     } else {                            /* Loop variable is a macro */
  6815.         x = mlook(mactab,"_forz",nmac);
  6816.         if (x < 0) {
  6817.             addmmac("_forz",foz_def);
  6818.             if ((x = mlook(mactab,"_forz",nmac)) < 0) {
  6819.                 printf("?FOR macro definition gone!\n");
  6820.                 return(success = 0);
  6821.             }
  6822.         }
  6823.     }
  6824.     debug(F010,"FOR command",line,0);   /* Execute the FOR macro. */
  6825.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  6826.  
  6827. badfor:
  6828.     printf("?Incomplete FOR command\n");
  6829.     return(-2);
  6830. }
  6831. #endif /* NOSPL */
  6832.  
  6833. #ifndef NOFRILLS
  6834. /* Do the BUG command */
  6835.  
  6836. int
  6837. dobug() {
  6838.     int n;
  6839.     char * s = "";
  6840.     extern char * k_info_dir;
  6841.  
  6842.     if (k_info_dir)
  6843.       s = k_info_dir;
  6844.  
  6845. #ifdef COMMENT
  6846.     printf("\n%s,%s\n Numeric: %ld",versio,ckxsys,vernum);
  6847. #endif /* COMMENT */
  6848.     printf(
  6849. "\nBefore requesting technical support from Columbia U., please consult:\n\n"
  6850.            );
  6851.     n = 7;
  6852. #ifdef OS2
  6853.     printf(" . Your \"Kermit 95\" user manual (use the MANUAL command).\n");
  6854.     printf(" . The technical reference manual, \"Using C-Kermit\".\n");
  6855.     n += 2;
  6856. #else
  6857.     printf(" . The book \"Using C-Kermit\" (type HELP for more info).\n");
  6858.     n += 1;
  6859. #endif /* OS2 */
  6860.  
  6861.     printf(" . Your own organization's support staff, if any.\n");
  6862.     printf(
  6863. " . The comp.protocols.kermit.misc newsgroup.\n");
  6864.     printf(
  6865. " . The Kermit support website, http://www.columbia.edu/kermit/support.html \n"
  6866.            );
  6867.     printf(
  6868. " . The Kermit FAQ, http://www.columbia.edu/kermit/newfaq.html \n");
  6869.     printf(
  6870. " . The C-Kermit FAQ, http://www.columbia.edu/kermit/ckfaq.html \n");
  6871.     n += 4;
  6872.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6873.     printf("\n\
  6874. If you still need help or have a bug to report after consulting these sources,"
  6875.            );
  6876.     printf("\nsend e-mail to:\n\n");
  6877.     n += 2;
  6878.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6879.     printf("  mailto:kermit-support@columbia.edu\n\n");
  6880.     n += 1;
  6881.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6882.     printf("Or contact us by post:\n\n");
  6883.     printf(
  6884. "  Kermit, Columbia University, 612 W 115 Street, New York NY  10025, USA\n\n"
  6885.            );
  6886.     n += 1;
  6887.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6888.     printf("Or by fax at +1 (212) 663-8202.\n\n");
  6889.     n += 1;
  6890.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6891. #ifdef COMMENT
  6892.     printf("Telephone support is available too:\n\n");
  6893.     n += 1;
  6894.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6895.     printf(
  6896.     "  +1 (212) 854-5126, from anywhere, $25.00 USD per call, MC/Visa\n\n");
  6897.     n += 1;
  6898.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6899. #endif /* COMMENT */
  6900. #ifndef NOSHOW
  6901. #ifndef NOFRILLS
  6902.     printf(
  6903. "Before reporting problems, please use the SHOW FEATURES command\n");
  6904.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  6905.     printf(
  6906. "to get detailed program version and configuration information.\n\n");
  6907. #endif /* NOFRILLS */
  6908. #endif /* NOSHOW */
  6909.     return(1);
  6910. }
  6911. #endif /* NOFRILLS */
  6912.  
  6913. #ifndef NOSPL
  6914.  
  6915. /*  T O D 2 S E C  --  Convert time of day as hh:mm:ss to secs since midnite */
  6916. /*
  6917.   Call with a string hh:mm or hh:mm:ss.
  6918.   Returns a 0 to 86400 on success, or a negative number on failure.
  6919. */
  6920. long
  6921. tod2sec(t) char * t; {
  6922.     long t2;
  6923.     long hh = 0L, mm = 0L, ss = 0L;
  6924.  
  6925.     if (!t) t = "";
  6926.     if (!*t)
  6927.       return(-3L);
  6928.     debug(F110,"tod2sec",t,0);
  6929.  
  6930.     if (isdigit(*t))                    /* Get hours from argument */
  6931.       hh = *t++ - '0';
  6932.     else
  6933.       return(-1L);
  6934.     if (isdigit(*t))
  6935.       hh = hh * 10 + *t++ - '0';
  6936. #ifdef COMMENT
  6937.     if (hh > 24L)
  6938.       return(-1L);
  6939. #endif /* COMMENT */
  6940.     if (*t == ':')
  6941.       t++;
  6942.     else if (!*t)
  6943.       goto xtod2sec;
  6944.     else
  6945.       return(-1L);
  6946.  
  6947.     if (isdigit(*t))                    /* Minutes */
  6948.       mm = *t++ - '0';
  6949.     else
  6950.       return(-1L);
  6951.     if (isdigit(*t))
  6952.       mm = mm * 10 + *t++ - '0';
  6953.     if (mm > 60L)
  6954.       return(-1L);
  6955.     if (*t == ':')
  6956.       t++;
  6957.     else if (!*t)
  6958.       goto xtod2sec;
  6959.     else
  6960.       return(-1L);
  6961.  
  6962.     if (isdigit(*t))                    /* Seconds */
  6963.       ss = *t++ - '0';
  6964.     else
  6965.       return(-1L);
  6966.     if (isdigit(*t))
  6967.       ss = ss * 10 + *t++ - '0';
  6968.     if (ss > 60L)
  6969.       return(-1L);
  6970.  
  6971.     if (*t > 32)                        /* No trailing junk allowed */
  6972.       return(-1L);
  6973.  
  6974.   xtod2sec:
  6975.  
  6976.     t2 = hh * 3600L + mm * 60L + ss;    /* Seconds since midnight from arg */
  6977.     debug(F101,"tod2sec t2","",t2);
  6978.  
  6979.     return(t2);
  6980. }
  6981.  
  6982. int waitinterval = 1;
  6983.  
  6984. #ifdef OLDWAIT
  6985. #undef OLDWAIT
  6986. #endif /* OLDWAIT */
  6987.  
  6988. int kbchar = NUL;
  6989.  
  6990. int
  6991. dopaus(cx) int cx; {
  6992.     long zz;
  6993.     extern int sleepcan;
  6994.  
  6995. #ifdef OLDWAIT
  6996.     zz = -1L;
  6997.     x_ifnum = 1;                        /* Turn off internal complaints */
  6998.     if (cx == XXWAI)
  6999.       y = cmnum("seconds to wait, or time of day hh:mm:ss","1",10,&x,xxstring);
  7000.     else if (cx == XXPAU)
  7001.       y = cmnum("seconds to pause, or time of day hh:mm:ss",
  7002.                 "1",10,&x,xxstring);
  7003.     else
  7004.       y = cmnum("milliseconds to sleep, or time of day hh:mm:ss",
  7005.                 "100",10,&x,xxstring);
  7006.     x_ifnum = 0;
  7007.     if (y < 0) {
  7008.         if (y == -2) {                  /* Invalid number or expression */
  7009.             char *p = tmpbuf;           /* Retrieve string from atmbuf */
  7010.             int n = TMPBUFSIZ;
  7011.             *p = NUL;
  7012.             zzstring(atmbuf,&p,&n);     /* Evaluate in case it's a variable */
  7013.             zz = tod2sec(tmpbuf);       /* Convert to secs since midnight */
  7014.             if (zz < 0L) {
  7015.                 printf("?Number, expression, or time of day required\n");
  7016.                 return(-9);
  7017.             } else {
  7018.                 char now[32];           /* Current time */
  7019.                 char *p;
  7020.                 long tnow;
  7021.                 p = now;
  7022.                 ztime(&p);
  7023.                 tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  7024.                 if (zz < tnow)          /* User's time before now */
  7025.                   zz += 86400L;         /* So make it tomorrow */
  7026.                 zz -= tnow;             /* Seconds from now. */
  7027.             }
  7028.         } else
  7029.           return(y);
  7030.     }
  7031.     if (x < 0) x = 0;
  7032.     switch (cx) {
  7033.       case XXPAU:                       /* PAUSE */
  7034.       case XXMSL:                       /* MSLEEP */
  7035.         if ((y = cmcfm()) < 0) return(y);
  7036.         break;
  7037.       case XXWAI:                       /* WAIT */
  7038.         z = 0;                          /* Modem signal mask */
  7039.         while (1) {                     /* Read zero or more signal names */
  7040.             y = cmkey(mstab,nms,"modem signal","",xxstring);
  7041.             if (y == -3) break;         /* -3 means they typed CR */
  7042.             if (y < 0) return(y);       /* Other negatives are errors */
  7043.             z |= y;                     /* OR the bit into the signal mask */
  7044.         }
  7045.         if ((y = cmcfm()) < 0) return(y);
  7046.         break;
  7047.  
  7048.       default:                          /* Shouldn't happen */
  7049.         return(-2);
  7050.     }
  7051.  
  7052. /* Command is entered, now do it. */
  7053.  
  7054.     if (zz > -1L) {                     /* Time of day given? */
  7055.         x = zz;
  7056.         if (zz != (long) x) {
  7057.             printf(
  7058. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7059.                    );
  7060.             return(-9);
  7061.         }
  7062.     }
  7063.     if (cx == XXMSL) {                  /* Millisecond sleep */
  7064.         msleep(zz < 0 ? x : x * 1000);
  7065.         return(success = 1);
  7066.     }
  7067.     if (cx == XXPAU && !sleepcan) {     /* SLEEP CANCELLATION is OFF */
  7068.         sleep(x);
  7069.         return(success = 1);
  7070.     }
  7071.  
  7072.     /* WAIT, or else SLEEP with cancellation allowed... */
  7073.  
  7074.     do {                                /* Sleep loop */
  7075.         int mdmsig;
  7076.         if (sleepcan) {                 /* Keyboard cancellation allowed? */
  7077.             if (y = conchk()) {         /* Did they type something? */
  7078. #ifdef COMMENT
  7079.                 while (y--) coninc(0);  /* Yes, gobble it all up */
  7080. #else
  7081.                 /* There is a debate over whether PAUSE should absorb    */
  7082.                 /* its cancelling character(s).  There are several       */
  7083.                 /* reasons why it should gobble at least one character:  */
  7084.                 /* (1) MS-DOS Kermit does it                             */
  7085.                 /* (2) if not, subsequent PAUSE commands will terminate  */
  7086.                 /*     immediately                                       */
  7087.                 /* (3) if not, subsequent ASK commands will use it as    */
  7088.                 /*     valid input.  If \13, then it will get no input   */
  7089.                 /* (4) if not, then the character appears on the command */
  7090.                 /*     line after all enclosing macros are complete.     */
  7091.                 kbchar = coninc(0);     /* Gobble one up */
  7092. #endif /* COMMENT */
  7093.                 break;                  /* And quit PAUSing or WAITing */
  7094.             }
  7095.         }
  7096.         if (cx == XXWAI) {              /* WAIT (z == modem signal mask) */
  7097.             debug(F101,"WAIT x","",x);
  7098.             if (z > 0) {                /* Looking for any modem signals? */
  7099.                 mdmsig = ttgmdm();      /* Yes, get them */
  7100.                 if (mdmsig < 0)         /* Failed */
  7101.                   return(success = 0);
  7102.                 if ((mdmsig & z) == z)  /* Got what we wanted? */
  7103.                   return(success = 1);  /* Succeed */
  7104.             }
  7105.             if (x == 0)                 /* WAIT 0 and didn't get our signals */
  7106.               break;
  7107.         }
  7108.         sleep(1);                       /* No interrupt, sleep one second */
  7109.     } while (--x > 0);
  7110.  
  7111.     if (cx == XXWAI)                    /* If WAIT and loop exhausted */
  7112.       success = (z == 0);               /* Fail. */
  7113.     else                                /*  */
  7114.       success = (x == 0);               /* Set SUCCESS/FAILURE for PAUSE. */
  7115.     return(success);
  7116.  
  7117. #else  /* New code uses chained FDBs and allows FILE waits... */
  7118.  
  7119.     char * m = "";                      /* Help message */
  7120.     struct FDB nu, fl;                  /* Parse function descriptor blocks */
  7121.     int filewait = 0;
  7122.     int mdmsig = 0, fs = 0;
  7123.     char filedate[32];
  7124.  
  7125.     kbchar = 0;
  7126.  
  7127.     switch (cx) {
  7128.       case XXWAI: m = "seconds to wait, or time of day hh:mm:ss"; break;
  7129.       case XXPAU: m = "seconds to pause, or time of day hh:mm:ss"; break;
  7130.       case XXMSL: m = "milliseconds to sleep, or time of day hh:mm:ss"; break;
  7131.     }
  7132.     zz = -1L;
  7133.     cmfdbi(&nu,
  7134.            _CMNUM,                      /* Number */
  7135.            m,                           /* Help message */
  7136.            (cx == XXMSL) ? "100" : "1", /* Default */
  7137.            "",                          /* N/A */
  7138.            0,                           /* N/A */
  7139.            0,                           /* N/A */
  7140.            xxstring,                    /* Processing function */
  7141.            NULL,                        /* N/A */
  7142.            &fl                          /* Next */
  7143.            );
  7144.     cmfdbi(&fl,                         /* Time of day */
  7145.            _CMFLD,                      /* Field */
  7146.            "",                          /* hlpmsg */
  7147.            "",                          /* default */
  7148.            "",                          /* addtl string data */
  7149.            0,                           /* addtl numeric data 1 */
  7150.            0,                           /* addtl numeric data 2 */
  7151.            xxstring,                    /* processing func */
  7152.            NULL,                        /* N/A */
  7153.            NULL                         /* No next */
  7154.            );
  7155.     x = cmfdb(&nu);                     /* Parse a number or a field */
  7156.     if (x < 0) {
  7157.         if (x == -3)
  7158.           x = -2;
  7159.         return(x);
  7160.     }
  7161.     switch (cmresult.fcode) {
  7162.       case _CMNUM:                      /* Number */
  7163.         x = cmresult.nresult;
  7164.         break;
  7165.       case _CMFLD:                      /* Field */
  7166.         zz = tod2sec(cmresult.sresult); /* Convert to secs since midnight */
  7167.         if (zz < 0L) {
  7168.             printf("?Number, expression, or time of day required\n");
  7169.             return(-9);
  7170.         } else {
  7171.             char now[32];               /* Current time */
  7172.             char *p;
  7173.             long tnow;
  7174.             p = now;
  7175.             ztime(&p);
  7176.             tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  7177.             if (zz < tnow)              /* User's time before now */
  7178.               zz += 86400L;             /* So make it tomorrow */
  7179.             zz -= tnow;         /* Seconds from now. */
  7180.         }
  7181.     }
  7182.     debug(F101,"PAUSE/WAIT/MSLEEP zz","",zz);
  7183.     switch (cx) {
  7184.       case XXPAU:                       /* PAUSE */
  7185.       case XXMSL:                       /* MSLEEP */
  7186.         if ((y = cmcfm()) < 0) return(y);
  7187.         break;
  7188.       case XXWAI:                       /* WAIT */
  7189.         z = 0;                          /* Modem signal mask */
  7190.         y = cmkey(waittab,nwaittab,"","",xxstring);
  7191.         if (y < 0) {
  7192.             if (y == -3) {
  7193.                 if ((y = cmcfm()) < 0)
  7194.                   return(y);
  7195.                 break;
  7196.             } else
  7197.               return(y);
  7198.         }
  7199.         if (y == WAIT_FIL) {            /* FILE */
  7200.             int wild = 0;
  7201.             if ((z = cmkey(wfswi,nwfswi,"event","",xxstring)) < 0)
  7202.               return(z);
  7203.             filewait = z;
  7204.             if (filewait == WF_MOD || filewait == WF_DEL)
  7205.               z = cmifi("Filename","",&s,&wild,xxstring);
  7206.             else
  7207.               z = cmfld("Filename","",&s,xxstring);
  7208.             if (z < 0)
  7209.               return(z);
  7210.             if (wild || ((filewait == WF_CRE) && iswild(s))) {
  7211.                 printf("?Wildcards not valid here\n");
  7212.                 return(-9);
  7213.             }
  7214.             ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7215.             if ((z = cmcfm()) < 0)
  7216.               return(z);
  7217.             break;
  7218.         } else if (y != WAIT_MDM) {     /* A modem signal */
  7219.             z |= y;                     /* OR the bit into the signal mask */
  7220.         }
  7221.         if (!filewait) {                /* Modem signals... */
  7222.             while (1) {                 /* Get zero or more signal names */
  7223.                 y = cmkey(mstab,nms,"modem signal","",xxstring);
  7224.                 if (y == -3) break;     /* -3 means they typed CR */
  7225.                 if (y < 0) return(y);   /* Other negatives are errors */
  7226.                 z |= y;                 /* OR the bit into the signal mask */
  7227.             }
  7228.             if ((y = cmcfm()) < 0) return(y);
  7229.             break;
  7230.         }
  7231.  
  7232.       default:                          /* Shouldn't happen */
  7233.         return(-2);
  7234.     } /* switch (cx) */
  7235.  
  7236. /* Command is entered, now do it. */
  7237.  
  7238.     if (zz > -1L) {                     /* Time of day given? */
  7239.         x = zz;
  7240.         if (zz != (long) x) {
  7241.             printf(
  7242. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7243.                    );
  7244.             return(-9);
  7245.         }
  7246.     }
  7247.     if (sleepcan)
  7248.       concb((char)escape);              /* Ensure single-char wakeup */
  7249.  
  7250.     if (cx == XXMSL) {                  /* Millisecond sleep */
  7251.         msleep(zz < 0 ? x : x * 1000);
  7252.         return(success = 1);
  7253.     }
  7254.     if (cx == XXPAU && !sleepcan) {     /* SLEEP CANCELLATION is OFF */
  7255.         sleep(x);
  7256.         return(success = 1);
  7257.     }
  7258.     if (filewait) {                     /* FILE... */
  7259.         fs = zchki(tmpbuf);             /* Check if file exists */
  7260.         switch (filewait) {
  7261.           case WF_DEL:
  7262.             if (fs == -1)
  7263.               return(success = 1);
  7264.             break;
  7265.           case WF_MOD:
  7266.             if (fs == -1) {
  7267.                 printf("?File does not exit: %s\n",tmpbuf);
  7268.                 return(-9);
  7269.             }
  7270.             s = zfcdat(tmpbuf);         /* Get current modification date */
  7271.             if (!s) s = "";
  7272.             if (ckstrncpy(filedate,s,32) != 17) {
  7273.                 printf("?Can't get modification time: %s\n",tmpbuf);
  7274.                 return(-9);
  7275.             }
  7276.             break;
  7277.           case WF_CRE:
  7278.             if (fs > -1)
  7279.               return(success = 1);
  7280.             break;
  7281.         }
  7282.     }
  7283.     do {                                /* Polling loop */
  7284.         if (sleepcan) {                 /* Keyboard cancellation allowed? */
  7285.             if ((y = conchk()) > 0) {   /* Did they type something? */
  7286.                 kbchar = coninc(0);     /* Yes, get first char they typed */
  7287.                 debug(F000,"WAIT kbchar","",kbchar);
  7288. #ifdef COMMENT
  7289.                 while (--y > 0)         /* Gobble the rest up */
  7290.                   coninc(0);
  7291. #endif /* COMMENT */
  7292.                 return(success = 0);    /* And quit PAUSing or WAITing */
  7293.             }
  7294.         }
  7295.         if (filewait == 0) {
  7296.             if (cx == XXWAI) {          /* WAIT for modem signals */
  7297.                 if (z != 0) {
  7298.                     mdmsig = ttgmdm();  /* Get them. */
  7299.                     debug(F101,"WAIT ttgmdm","",mdmsig);
  7300.                     if (mdmsig < 0)     /* Failure to get them? */
  7301.                       return(success = 0); /* Fail. */
  7302.                     if ((mdmsig & z) == z) /* Got desired ones? */
  7303.                       return(success = 1); /* Succeed. */
  7304.                 } else if (x == 0)
  7305.                   return(success = 0);
  7306.             }
  7307.         } else {                        /* FILE... */
  7308.             fs = zchki(tmpbuf);         /* Get file status */
  7309.             if (filewait == WF_MOD) {   /* Wait for modification */
  7310.                 if (fs == -1)           /* Failure to get status */
  7311.                   return(success = 0);  /* so WAIT fails. */
  7312.                 s = zfcdat(tmpbuf);     /* Get current modification time */
  7313.                 if (!s) s = "";         /* And compare with the time */
  7314.                 if (strcmp(s,filedate)) /* when the WAIT started */
  7315.                   return(success = 1);
  7316.             } else if (filewait == WF_DEL) { /* Wait for deletion */
  7317.                 if (fs == -1)           /* If file doesn't exist, */
  7318.                   return(success = 1);  /* succeed. */
  7319.             } else if (filewait == WF_CRE) { /* Wait for creation */
  7320.                 if (fs != -1)           /* If file exists */
  7321.                   return(success = 1);  /* succeed. */
  7322.             }
  7323.         }
  7324.         if (x < 1)                      /* SLEEP/WAIT/PAUSE 0 */
  7325.           break;
  7326.         sleep(waitinterval);            /* No interrupt, sleep */
  7327.         x -= waitinterval;              /* Deduct sleep time */
  7328.     } while (x > 0);
  7329.  
  7330.     if (cx == XXWAI)                    /* WAIT time expired */
  7331.       success = (z == 0);               /* Succeed if no modem signals */
  7332.     else                                /* For SLEEP or PAUSE, success */
  7333.       success = (x == 0);               /* depends on whether it was */
  7334.     return(success);                    /* interrupted from the keyboard. */
  7335. #endif /* OLDWAIT */
  7336. }
  7337. #endif /* NOSPL */
  7338.  
  7339. #ifdef OS2ORUNIX
  7340. _PROTOTYP(int zcmpfn,(char *, char *));
  7341. #endif /* OS2ORUNIX */
  7342.  
  7343. #ifndef NOFRILLS
  7344. #ifdef NT
  7345. int 
  7346. dolink() {
  7347.     /* Parse a file or a directory name */
  7348.     int i, x, z, listing = 0, havename = 0, wild = 0, rc = 1;
  7349.     struct FDB sw, fi;
  7350.  
  7351.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7352.            _CMKEY,                      /* fcode */
  7353.            "Filename or switch",        /* hlpmsg */
  7354.            "",                          /* default */
  7355.            "",                          /* addtl string data */
  7356.            nqvswtab,                    /* addtl numeric data 1: tbl size */
  7357.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7358.            xxstring,                    /* Processing function */
  7359.            qvswtab,                     /* Keyword table */
  7360.            &fi                          /* Pointer to next FDB */
  7361.            );
  7362.  
  7363.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7364.            _CMIFI,                      /* fcode */
  7365.            "",                          /* hlpmsg */
  7366.            "",                          /* default */
  7367.            "",                          /* addtl string data */
  7368.            3,                           /* addtl numeric data 1 */
  7369.            0,                           /* addtl numeric data 2 */
  7370.            xxstring,
  7371.            NULL,
  7372.            NULL
  7373.            );
  7374.  
  7375.     while (!havename) {
  7376.         x = cmfdb(&sw);                 /* Parse something */
  7377.         if (x < 0)                      /* Error */
  7378.           return(x);
  7379.         switch (cmresult.fcode) {
  7380.           case _CMKEY:
  7381.             switch (cmresult.nresult) {
  7382.               case DEL_LIS:
  7383.               case DEL_VRB:
  7384.                 listing = 1;
  7385.                 break;
  7386.               case DEL_NOL:
  7387.               case DEL_QUI:
  7388.                 listing = 0;
  7389.                 break;
  7390.             }
  7391.             break;
  7392.           case _CMIFI:
  7393.             s = cmresult.sresult;
  7394.             havename = 1;
  7395.             break;
  7396.           default:
  7397.             return(-2);
  7398.         }
  7399.     }
  7400.     wild = cmresult.nresult;            /* Source specification wild? */
  7401.  
  7402.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7403.     s = line;
  7404.  
  7405.     if (!wild)
  7406.       wild = iswild(line);
  7407.  
  7408.     p = tmpbuf;                         /* Place for new name */
  7409.     if ((x = cmofi(wild ? "Target directory" : "New name",
  7410.                    "",&s,xxstring)) < 0) { /* Get new name */
  7411.         if (x == -3) {
  7412.             printf("?%s required\n", wild ? "Target directory" : "New name");
  7413.             return(-9);
  7414.         } else return(x);
  7415.     }
  7416.     ckstrncpy(p,s,TMPBUFSIZ);           /* Make a safe copy of the new name */
  7417.     if ((y = cmcfm()) < 0) return(y);
  7418.  
  7419.     if (!wild) {                        /* Just one */
  7420.         if (listing) printf("%s => %s ",line,p);
  7421.         if (zlink(line,p) < 0) {
  7422.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7423.             rc = 0;
  7424.         } else {
  7425.             if (listing) printf("(OK)\n");
  7426.         }
  7427.         return(success = rc);
  7428.     }
  7429.     if (!isdir(p)) {                    /* Multiple */
  7430.         printf(                         /* if target is not a directory */
  7431. "?Multiple source files not allowed if target is not a directory.\n");
  7432.         return(-9);
  7433.     }
  7434. #ifdef COMMENT
  7435.     else {                              /* Show full path of target */
  7436.         char buf[CKMAXPATH];            /* (too much) */
  7437.         if (zfnqfp(p,CKMAXPATH,buf))
  7438.           ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  7439.     }
  7440. #endif /* COMMENT */
  7441.  
  7442. #ifdef VMS
  7443.     conres();                           /* Let Ctrl-C work. */
  7444. #endif /* VMS */
  7445.     debug(F110,"dolink line",line,0);
  7446.  
  7447. #ifdef ZXREWIND
  7448.     z = zxrewind();                     /* Rewind file list */
  7449. #else
  7450.     z = nzxpand(s,0);                   /* Expand file list */
  7451. #endif /* ZXREWIND */
  7452.     debug(F111,"dolink p",p,z);
  7453.  
  7454. #ifdef UNIX
  7455.     if (wild && z > 1)
  7456.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7457. #endif /* UNIX */
  7458.  
  7459.     while (z-- > 0) {
  7460.         if (!(z == 0 && !wild))
  7461.           znext(line);
  7462.         if (!line[0])
  7463.           break;
  7464.         if (listing) printf("%s => %s ",line,p);
  7465.         if (zlink(line,p) < 0) {
  7466.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7467.             rc = 0;
  7468.         } else {
  7469.             if (listing) printf("(OK)\n");
  7470.         }
  7471.     }
  7472. #ifdef VMS
  7473.     concb((char)escape);
  7474. #endif /* VMS */
  7475.     return(success = rc);
  7476. }
  7477. #endif /* NT */
  7478.  
  7479. #ifdef ZCOPY
  7480. int
  7481. docopy() {
  7482.     int i, x, listing = 0, havename = 0;
  7483.     struct FDB sw, fi;
  7484.     int targetisdir = 0;
  7485.     int targetlen = 0;
  7486.     int swapping = 0;
  7487.     int appending = 0;
  7488.     int fromb64 = 0;
  7489.     int tob64 = 0;
  7490.     int wild = 0;
  7491.     int rc = 1;
  7492.  
  7493.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7494.            _CMKEY,                      /* fcode */
  7495.            "Filename or switch",        /* hlpmsg */
  7496.            "",                          /* default */
  7497.            "",                          /* addtl string data */
  7498.            ncopytab,                    /* addtl numeric data 1: tbl size */
  7499.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7500.            xxstring,                    /* Processing function */
  7501.            copytab,                     /* Keyword table */
  7502.            &fi                          /* Pointer to next FDB */
  7503.            );
  7504.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7505.            _CMIFI,                      /* fcode */
  7506.            "",                          /* hlpmsg */
  7507.            "",                          /* default */
  7508.            "",                          /* addtl string data */
  7509.            0,                           /* addtl numeric data 1 */
  7510.            0,                           /* addtl numeric data 2 */
  7511.            xxstring,
  7512.            NULL,
  7513.            NULL
  7514.            );
  7515.  
  7516.     while (!havename) {
  7517.         x = cmfdb(&sw);                 /* Parse something */
  7518.         if (x < 0)                      /* Error */
  7519.           return(x);
  7520.         switch (cmresult.fcode) {
  7521.           case _CMKEY:
  7522.             switch (cmresult.nresult) {
  7523.               case DEL_LIS:
  7524.               case DEL_VRB:
  7525.                 listing = 1;
  7526.                 break;
  7527.               case DEL_NOL:
  7528.               case DEL_QUI:
  7529.                 listing = 0;
  7530.                 break;
  7531.               case 999:
  7532.                 swapping = 1;
  7533.                 break;
  7534.               case 998:
  7535.                 appending = 1;
  7536.                 break;
  7537. #ifndef NOSPL
  7538.               case 997:
  7539.                 fromb64 = 1;
  7540.                 break;
  7541.               case 996:
  7542.                 tob64 = 1;
  7543.                 break;
  7544. #endif /* NOSPL */
  7545.             }
  7546.             break;
  7547.           case _CMIFI:
  7548.             s = cmresult.sresult;
  7549.             havename = 1;
  7550.             break;
  7551.           default:
  7552.             return(-2);
  7553.         }
  7554.     }
  7555.     wild = cmresult.nresult;
  7556.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7557.     s = line;
  7558.     p = tmpbuf;                         /* Place for new name */
  7559.  
  7560.     /* Get destination name */
  7561.     if ((x = cmofi("destination name and/or directory",
  7562. #ifdef UNIX
  7563.                    "."
  7564. #else
  7565.                    ""
  7566. #endif /* UNIX */
  7567.                    ,&s,xxstring)) < 0) {
  7568.         if (x == -3) {
  7569.             printf("?Name for destination file required\n");
  7570.             return(-9);
  7571.         } else return(x);
  7572.     }
  7573.     ckstrncpy(p,s,TMPBUFSIZ);           /* Safe copy of destination name */
  7574.     if ((y = cmcfm()) < 0) return(y);
  7575.     if (appending && swapping) {
  7576.         printf("?Sorry, /APPEND and /SWAP conflict\n");
  7577.         return(-9);
  7578.     }
  7579. #ifdef COMMENT
  7580. /*
  7581.   This unreasonably prevented "COPY /APPEND *.* bifile" from concatenating
  7582.   a bunch of files into one big file.
  7583. */
  7584.     if (appending && wild) {
  7585.         printf("?Sorry, /APPEND can be used only with single files\n");
  7586.         return(-9);
  7587.     }
  7588. #endif /* COMMENT */
  7589.     targetisdir = isdir(p);
  7590.     x = strlen(p);
  7591.     if (targetisdir) {
  7592. #ifdef UNIXOROSK
  7593.         if (p[x-1] != '/') {
  7594.             ckstrncat(p,"/",TMPBUFSIZ);
  7595.             x++;
  7596.         }
  7597. #else
  7598. #ifdef OS2
  7599.         if (p[x-1] != '/') {
  7600.             ckstrncat(p,"/",TMPBUFSIZ);
  7601.             x++;
  7602.         }
  7603. #else
  7604. #ifdef STRATUS
  7605.         if (p[x-1] != '>') {
  7606.             ckstrncat(p,">",TMPBUFSIZ);
  7607.             x++;
  7608.         }
  7609. #else
  7610. #ifdef datageneral
  7611.         if (p[x-1] != ':') {
  7612.             ckstrncat(p,":",TMPBUFSIZ);
  7613.             x++;
  7614.         }
  7615. #else
  7616.         if (p[x-1] != '/') {
  7617.             ckstrncat(p,"/",TMPBUFSIZ);
  7618.             x++;
  7619.         }
  7620. #endif /* datageneral */
  7621. #endif /* STRATUS */
  7622. #endif /* OS2 */
  7623. #endif /* UNIXOROSK */
  7624.     }
  7625.     targetlen = x;
  7626.  
  7627.     if (!appending) {            /* If /APPEND not given */
  7628.     if (wild && !targetisdir) {    /* No wildcards allowed */
  7629.         printf(            /* if target is not a directory */
  7630. "?Multiple source files not allowed if target is not a directory.\n");
  7631.         return(-9);
  7632.     }
  7633.     }
  7634.  
  7635. #ifdef VMS
  7636.     conres();                           /* Let Ctrl-C work. */
  7637. #endif /* VMS */
  7638.     debug(F110,"docopy line",line,0);
  7639.     debug(F110,"docopy p",p,0);
  7640.  
  7641. #ifdef ZXREWIND
  7642.     z = zxrewind();                     /* Rewind file list */
  7643. #else
  7644.     z = nzxpand(s,0);                   /* Expand file list */
  7645. #endif /* ZXREWIND */
  7646.  
  7647. #ifdef UNIX
  7648.     if (wild)
  7649.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7650. #endif /* UNIX */
  7651.  
  7652. #ifdef IKSD
  7653.     if (!targetisdir && zchki(p) > -1) { /* Destination file exists? */
  7654.         if (inserver && (!ENABLED(en_del)
  7655. #ifdef CK_LOGIN
  7656.                          || isguest
  7657. #endif /* CK_LOGIN */
  7658.                          )) {
  7659.             printf("?Sorry, overwriting existing files is disabled\n");
  7660.             return(-9);
  7661.         }
  7662.     }
  7663. #endif /* IKSD */
  7664.  
  7665.     if (tob64 && fromb64) {             /* To and from B64 = no conversion */
  7666.         tob64 = 0;
  7667.         fromb64 = 0;
  7668.     }
  7669.     debug(F110,"COPY dest",p,0);
  7670.  
  7671.     while (z > 0) {
  7672.  
  7673.         znext(line);
  7674.         if (!line[0])
  7675.           break;
  7676.  
  7677.         errno = 0;                      /* Reset errno */
  7678.  
  7679.         if (listing) printf("%s => %s ",line,p);
  7680.  
  7681.         /* Straight copy */
  7682.         if (!swapping && !appending && !fromb64 && !tob64) {
  7683.             debug(F110,"COPY zcopy",line,0);
  7684.  
  7685.             if ((x = zcopy(line,p)) < 0) { /* Let zcopy() do it. */
  7686.                 switch (x) {
  7687.                   case -2:
  7688.                     printf("(FAILED: Not a regular file)\n");
  7689.                     rc = 0;
  7690.                     break;
  7691.                   case -3:
  7692.                     printf("(FAILED: Not found or not accessible)\n");
  7693.                     rc = 0;
  7694.                     break;
  7695.                   case -4:
  7696.                     printf("(FAILED: Permission denied)\n");
  7697.                     rc = 0;
  7698.                     break;
  7699.                   case -5:
  7700.                     printf("(Source and destination are the same file)\n");
  7701.                     break;
  7702.                   case -6:
  7703.                     printf("(FAILED: Input/Output error)\n");
  7704.                     rc = 0;
  7705.                     break;
  7706.                   case -7:
  7707.                     printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7708.                     rc = 0;
  7709.                     break;
  7710.                   default:
  7711.                     printf("(FAILED: %s)\n",ck_errstr());
  7712.                     rc = 0;
  7713.                 }
  7714.             } else {
  7715.                 if (listing) printf("(OK)\n");
  7716.             }
  7717.  
  7718.         } else {                        /* Special options */
  7719.  
  7720.             int prev, y, x = 0;         /* Variables needed for them */
  7721.             int i, t;
  7722.             char ibuf[100];
  7723.             char obuf[200];
  7724.             FILE * in = NULL;
  7725.             FILE * out = NULL;
  7726.  
  7727.             if ((in = fopen(line,"r")) == NULL) { /* Open input file */
  7728.                 if (listing) printf("(FAILED: %s)\n",ck_errstr());
  7729.                 rc = 0;
  7730.                 continue;
  7731.             }
  7732.             if (targetisdir) {          /* Target is directory */
  7733.                 char * buf = NULL;      /* so append this filename to it */
  7734.                 zstrip(line,&buf);
  7735.                 p[targetlen] = NUL;
  7736.                 if (buf)
  7737.                   ckstrncat(p,buf,TMPBUFSIZ);
  7738.             }
  7739. #ifdef OS2ORUNIX
  7740.             if (zcmpfn(line,p)) {       /* Input and output are same file? */
  7741.                 if (listing)
  7742.                   printf("(FAILED: Source and destination identical)\n");
  7743.                 rc = 0;
  7744.                 continue;
  7745.             }
  7746. #endif /* OS2ORUNIX */
  7747.             if ((out = fopen(p, (appending ? "a" : "w"))) == NULL) {
  7748.                 fclose(in);
  7749.                 if (listing) printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7750.                 rc = 0;
  7751.                 continue;
  7752.             }
  7753. #ifndef NOSPL
  7754.             if (tob64) {                /* Converting to Base-64 */
  7755.  
  7756.                 debug(F110,"COPY tob64",line,0);
  7757.  
  7758.                 while (1) {             /* Loop... */
  7759.                     prev = x;
  7760.                     if ((x = fread(ibuf,1,54,in)) < 1) { /* EOF */
  7761.                         if (listing)
  7762.                           printf("(OK)\n");
  7763.                         break;
  7764.                     }
  7765.                     if (prev % 3) {
  7766.                         if (listing)
  7767.                           printf("(FAILED: Phase error at %d)\n",prev);
  7768.                         rc = 0;
  7769.                         break;
  7770.                     }
  7771.                     if (swapping) {
  7772.                         if (x & 1) {
  7773.                             if (listing)
  7774.                               printf("(FAILED: Swap error)\n");
  7775.                             rc = 0;
  7776.                             break;
  7777.                         }
  7778.                         for (i = 0; i < x; i+=2) {
  7779.                             t = ibuf[i];
  7780.                             ibuf[i] = ibuf[i+1];
  7781.                             ibuf[i+1] = t;
  7782.                         }
  7783.                     }
  7784.                     if ((y = b8tob64(ibuf,x,obuf,180)) < 0) {
  7785.                         if (listing)
  7786.                           printf("(FAILED: Encoding error)\n");
  7787.                         rc = 0;
  7788.                         break;
  7789.                     }
  7790.                     fprintf(out,"%s\n",obuf);
  7791.                 }
  7792.  
  7793.             } else if (fromb64) {       /* Converting from Base 64 */
  7794.  
  7795.                 debug(F110,"COPY fromb64",line,0);
  7796.  
  7797.                 if ((out = fopen(p,appending ? "a" : "w")) == NULL) {
  7798.                     fclose(in);
  7799.                     if (listing) printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7800.                     rc = 0;
  7801.                     continue;
  7802.                 }
  7803.                 x = 1;
  7804.                 while (x) {
  7805.                     x = fread(ibuf,1,80,in);
  7806.                     if ((y = b64tob8(ibuf,x,obuf,80)) < 0) {
  7807.                         if (listing)
  7808.                           printf("(FAILED: Decoding error)\n");
  7809.                         rc = 0;
  7810.                         break;
  7811.                     }
  7812.                     if (swapping) {
  7813.                         if (x & 1) {
  7814.                             if (listing)
  7815.                               printf("(FAILED: Swap error)\n");
  7816.                             rc = 0;
  7817.                             break;
  7818.                         }
  7819.                         for (i = 0; i < y; i+=2) {
  7820.                             t = obuf[i];
  7821.                             obuf[i] = obuf[i+1];
  7822.                             obuf[i+1] = t;
  7823.                         }
  7824.                     }
  7825.                     if (y > 0) {
  7826.                         if (fwrite(obuf,1,y,out) < 1) {
  7827.                             if (listing)
  7828.                               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7829.                             rc = 0;
  7830.                             break;
  7831.                         }
  7832.                     }
  7833.                 }
  7834.  
  7835.             } else
  7836. #endif /* NOSPL */
  7837.  
  7838.             if (swapping) {             /* Swapping bytes */
  7839.  
  7840.                 CHAR c[3];
  7841.                 c[2] = NUL;
  7842.  
  7843.                 debug(F110,"COPY swapping",line,0);
  7844.  
  7845.                 while (1) {
  7846.                     x = fread((char *)c,1,2,in);
  7847.                     if (x < 1) {
  7848.                         if (listing)
  7849.                           printf("(OK)\n");
  7850.                         break;
  7851.                     } else if (x == 1) {
  7852.                         c[1] = c[0];
  7853.                         c[0] = NUL;
  7854.                         printf(
  7855.                             "(WARNING: Odd byte count)");
  7856.                         if (!listing) printf("\n");
  7857.                     }
  7858.                     if (fprintf(out,"%c%c",c[1],c[0]) == EOF) {
  7859.                         if (listing)
  7860.                           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7861.                         rc = 0;
  7862.                         break;
  7863.                     }
  7864.                 }
  7865.  
  7866.             } else if (appending) {     /* Appending to target file */
  7867.  
  7868.                 char c;
  7869.  
  7870.                 debug(F110,"COPY appending",line,0);
  7871.  
  7872.                 while (1) {
  7873.                     x = fread(&c,1,1,in);
  7874.                     if (x < 1) {
  7875.                         if (listing)
  7876.                           printf("(OK)\n");
  7877.                         break;
  7878.                     }
  7879.                     if (fwrite(&c,1,1,out) < 1) {
  7880.                         if (listing)
  7881.                           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  7882.                         rc = 0;
  7883.                         break;
  7884.                     }
  7885.                 }
  7886.             }
  7887.             if (out) fclose(out);
  7888.             if (in) fclose(in);
  7889.         }
  7890. #ifdef VMSORUNIX
  7891.         concb((char)escape);
  7892. #endif /* VMSORUNIX */
  7893.     }
  7894.     if (rc > -1) success = rc;
  7895.     return(rc);
  7896. }
  7897. #endif /* ZCOPY */
  7898. #endif /* NOFRILLS */
  7899.  
  7900. #ifndef NORENAME
  7901. #ifndef NOFRILLS
  7902. #ifdef ZRENAME
  7903. int
  7904. dorenam() {
  7905.     /* Parse a file or a directory name */
  7906.     int i, x, z, listing = 0, havename = 0, wild = 0, rc = 1;
  7907.     struct FDB sw, fi;
  7908.  
  7909.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7910.            _CMKEY,                      /* fcode */
  7911.            "Filename or switch",        /* hlpmsg */
  7912.            "",                          /* default */
  7913.            "",                          /* addtl string data */
  7914.            nqvswtab,                    /* addtl numeric data 1: tbl size */
  7915.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7916.            xxstring,                    /* Processing function */
  7917.            qvswtab,                     /* Keyword table */
  7918.            &fi                          /* Pointer to next FDB */
  7919.            );
  7920.  
  7921.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7922.            _CMIFI,                      /* fcode */
  7923.            "",                          /* hlpmsg */
  7924.            "",                          /* default */
  7925.            "",                          /* addtl string data */
  7926.            3,                           /* addtl numeric data 1 */
  7927.            0,                           /* addtl numeric data 2 */
  7928.            xxstring,
  7929.            NULL,
  7930.            NULL
  7931.            );
  7932.  
  7933.     while (!havename) {
  7934.         x = cmfdb(&sw);                 /* Parse something */
  7935.         if (x < 0)                      /* Error */
  7936.           return(x);
  7937.         switch (cmresult.fcode) {
  7938.           case _CMKEY:
  7939.             switch (cmresult.nresult) {
  7940.               case DEL_LIS:
  7941.               case DEL_VRB:
  7942.                 listing = 1;
  7943.                 break;
  7944.               case DEL_NOL:
  7945.               case DEL_QUI:
  7946.                 listing = 0;
  7947.                 break;
  7948.             }
  7949.             break;
  7950.           case _CMIFI:
  7951.             s = cmresult.sresult;
  7952.             havename = 1;
  7953.             break;
  7954.           default:
  7955.             return(-2);
  7956.         }
  7957.     }
  7958.     wild = cmresult.nresult;            /* Source specification wild? */
  7959.  
  7960.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7961.     s = line;
  7962.  
  7963.     if (!wild)
  7964.       wild = iswild(line);
  7965.  
  7966.     p = tmpbuf;                         /* Place for new name */
  7967.     if ((x = cmofi(wild ? "Target directory" : "New name",
  7968.                    "",&s,xxstring)) < 0) { /* Get new name */
  7969.         if (x == -3) {
  7970.             printf("?%s required\n", wild ? "Target directory" : "New name");
  7971.             return(-9);
  7972.         } else return(x);
  7973.     }
  7974.     ckstrncpy(p,s,TMPBUFSIZ);           /* Make a safe copy of the new name */
  7975.     if ((y = cmcfm()) < 0) return(y);
  7976.  
  7977.     if (!wild) {                        /* Just one */
  7978.         if (listing) printf("%s => %s ",line,p);
  7979.         if (zrename(line,p) < 0) {
  7980.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7981.             rc = 0;
  7982.         } else {
  7983.             if (listing) printf("(OK)\n");
  7984.         }
  7985.         return(success = rc);
  7986.     }
  7987.     if (!isdir(p)) {                    /* Multiple */
  7988.         printf(                         /* if target is not a directory */
  7989. "?Multiple source files not allowed if target is not a directory.\n");
  7990.         return(-9);
  7991.     }
  7992. #ifdef COMMENT
  7993.     else {                              /* Show full path of target */
  7994.         char buf[CKMAXPATH];            /* (too much) */
  7995.         if (zfnqfp(p,CKMAXPATH,buf))
  7996.           ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  7997.     }
  7998. #endif /* COMMENT */
  7999.  
  8000. #ifdef VMS
  8001.     conres();                           /* Let Ctrl-C work. */
  8002. #endif /* VMS */
  8003.     debug(F110,"dorename line",line,0);
  8004.  
  8005. #ifdef ZXREWIND
  8006.     z = zxrewind();                     /* Rewind file list */
  8007. #else
  8008.     z = nzxpand(s,0);                   /* Expand file list */
  8009. #endif /* ZXREWIND */
  8010.     debug(F111,"dorename p",p,z);
  8011.  
  8012. #ifdef UNIX
  8013.     if (wild && z > 1)
  8014.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  8015. #endif /* UNIX */
  8016.  
  8017.     while (z-- > 0) {
  8018.         if (!(z == 0 && !wild))
  8019.           znext(line);
  8020.         if (!line[0])
  8021.           break;
  8022.         if (listing) printf("%s => %s ",line,p);
  8023.         if (zrename(line,p) < 0) {
  8024.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  8025.             rc = 0;
  8026.         } else {
  8027.             if (listing) printf("(OK)\n");
  8028.         }
  8029.     }
  8030. #ifdef VMS
  8031.     concb((char)escape);
  8032. #endif /* VMS */
  8033.     return(success = rc);
  8034. }
  8035. #endif /* ZRENAME */
  8036. #endif /* NOFRILLS */
  8037. #endif /* NORENAME */
  8038.  
  8039. #ifndef NOSPL
  8040.  
  8041. /* Do the RETURN command */
  8042.  
  8043. int
  8044. doreturn(s) char *s; {
  8045.     int x;
  8046.     extern int tra_asg;
  8047.     char * line, * lp;
  8048.  
  8049.     if (cmdlvl < 1) {
  8050.         printf("\n?Can't return from level %d\n",maclvl);
  8051.         return(success = 0);
  8052.     }
  8053.     line = malloc(LINBUFSIZ);
  8054.     if (line == NULL)
  8055.       return(success = 0);
  8056.     lp = line;                          /* Expand return value now */
  8057.     x = LINBUFSIZ-1;
  8058.     if (!s) s = "";
  8059.     debug(F110,"RETURN s",s,0);
  8060.     if (zzstring(s,&lp,&x) > -1) {
  8061.         s = line;
  8062.         debug(F110,"RETURN zzstring",s,0);
  8063.     }
  8064.  
  8065.     /* Pop from all FOR/WHILE/SWITCH/XIFs */
  8066.     while ((maclvl > 0) &&
  8067.            (m_arg[maclvl-1][0]) &&
  8068.            (cmdstk[cmdlvl].src == CMD_MD) &&
  8069.            (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  8070.             !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  8071.             !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  8072.             !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  8073.         debug(F111,"RETURN IF/FOR/WHI/SWI pop",m_arg[maclvl-1][0],maclvl);
  8074.         dogta(XXPTA);                   /* Put args back */
  8075.         popclvl();                      /* Pop up two levels */
  8076.         popclvl();
  8077.     }
  8078.     if (tra_asg) {                      /* If tracing show return value */
  8079.         if (*s)
  8080.           printf("<<< %s: \"%s\"\n", m_arg[maclvl][0], s);
  8081.         else
  8082.           printf("<<< %s: (null)\n", m_arg[maclvl][0]);
  8083.     }
  8084.     popclvl();                          /* Pop from enclosing TAKE or macro */
  8085.     debug(F111,"RETURN tolevel",s,maclvl);
  8086.     if (!s) s = "";
  8087.     if (!*s) s = NULL;
  8088.     makestr(&(mrval[maclvl+1]),s);      /* Set the RETURN value */
  8089.     free(line);
  8090.     return(success = 1);                /* Macro succeeds if we RETURN */
  8091. }
  8092. #endif /* NOSPL */
  8093.  
  8094. #ifndef NOSPL
  8095. /* Do the OPEN command */
  8096.  
  8097. int
  8098. doopen()  {                             /* OPEN { append, read, write } */
  8099.     int x, y, z = 0; char *s;
  8100.     static struct filinfo fcb;          /* (must be static) */
  8101.     if ((x = cmkey(opntab,nopn,"mode","",xxstring)) < 0) {
  8102.         if (x == -3) {
  8103.             printf("?Mode required\n");
  8104.             return(-9);
  8105.         } else return(x);
  8106.     }
  8107.     switch (x) {
  8108.       case OPN_FI_R:                    /* Old file (READ) */
  8109.         if (chkfn(ZRFILE) > 0) {
  8110.             printf("?Read file already open\n");
  8111.             return(-2);
  8112.         }
  8113.         if ((z = cmifi("File to read","",&s,&y,xxstring)) < 0) {
  8114.             if (z == -3) {
  8115.                 printf("?Input filename required\n");
  8116.                 return(-9);
  8117.             } else return(z);
  8118.         }
  8119.         if (y) {                                /* No wildcards allowed */
  8120.             printf("\n?Please specify a single file\n");
  8121.             return(-2);
  8122.         }
  8123.         ckstrncpy(line,s,LINBUFSIZ);
  8124.         if ((int)strlen(line) < 1) return(-2);
  8125.         if ((z = cmnum("buffer size","4096",10,&y,xxstring)) < 0)
  8126.           return(z);
  8127.         if (y < 1) {
  8128.             printf("?Positive number required\n");
  8129.             return(-9);
  8130.         }
  8131.         if ((z = cmcfm()) < 0) return(z);
  8132.         readblock = y;
  8133.         if (readbuf)
  8134.           free((char *)readbuf);
  8135.         if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  8136.             printf("?Can't allocate read buffer\n");
  8137.             return(-9);
  8138.         }
  8139.         return(success = zopeni(ZRFILE,line));
  8140.  
  8141. #ifndef MAC
  8142. #ifndef NOPUSH
  8143.       case OPN_PI_R:                    /* Pipe/Process (!READ) */
  8144.         if (nopush) {
  8145.             printf("?Read from pipe disabled\n");
  8146.             return(success=0);
  8147.         }
  8148.         if (chkfn(ZRFILE) > 0) {
  8149.             printf("?Read file already open\n");
  8150.             return(-2);
  8151.         }
  8152.         if ((y = cmtxt("System command to read from","",&s,xxstring)) < 0) {
  8153.             if (y == -3) {
  8154.                 printf("?Command name required\n");
  8155.                 return(-9);
  8156.             } else return(y);
  8157.         }
  8158.         ckstrncpy(line,brstrip(s),LINBUFSIZ);
  8159.         if (!line[0]) return(-2);
  8160.         if ((y = cmcfm()) < 0) return(y);
  8161.         if (!readbuf) {
  8162.             if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  8163.                 printf("?Can't allocate read buffer\n");
  8164.                 return(-9);
  8165.             }
  8166.         }
  8167.         return(success = zxcmd(ZRFILE,line));
  8168.  
  8169.       case OPN_PI_W:                    /* Write to pipe */
  8170.         if (nopush) {
  8171.             printf("?Write to pipe disabled\n");
  8172.             return(success=0);
  8173.         }
  8174.         if (chkfn(ZWFILE) > 0) {
  8175.             printf("?Write file already open\n");
  8176.             return(-2);
  8177.         }
  8178.         if ((y = cmtxt("System command to write to","",&s,xxstring)) < 0) {
  8179.             if (y == -3) {
  8180.                 printf("?Command name required\n");
  8181.                 return(-9);
  8182.             } else return(y);
  8183.         }
  8184.         ckstrncpy(line,brstrip(s),LINBUFSIZ);
  8185.         if (!line[0]) return(-2);
  8186.         if ((y = cmcfm()) < 0) return(y);
  8187.         success = zxcmd(ZWFILE,line);
  8188.         if (!success && msgflg)
  8189.           printf("Can't open process for writing: %s\n",line);
  8190.         return(success);
  8191. #endif /* NOPUSH */
  8192. #endif /* MAC */
  8193.  
  8194.       case OPN_FI_W:                    /* New file (WRITE) */
  8195.       case OPN_FI_A:                    /* (APPEND) */
  8196.         if ((z = cmofi("Name of local file to create","",&s,xxstring)) < 0) {
  8197.             if (z == -3) {
  8198.                 printf("?Filename required\n");
  8199.                 return(-9);
  8200.             } else return(z);
  8201.         }
  8202.         if (z == 2) {
  8203.             printf("?Sorry, %s is a directory name\n",s);
  8204.             return(-9);
  8205.         }
  8206.         if (chkfn(ZWFILE) > 0) {
  8207.             printf("?Write/Append file already open\n");
  8208.             return(-2);
  8209.         }
  8210.         fcb.bs = fcb.cs = fcb.rl = fcb.fmt = fcb.org = fcb.cc = fcb.typ = 0;
  8211.         fcb.lblopts = 0;
  8212.         fcb.dsp = (x == OPN_FI_W) ? XYFZ_N : XYFZ_A; /* Create or Append */
  8213.         ckstrncpy(line,s,LINBUFSIZ);
  8214.         if ((int)strlen(line) < 1) return(-2);
  8215.         if ((y = cmcfm()) < 0) return(y);
  8216.         return(success = zopeno(ZWFILE,line,NULL,&fcb));
  8217.  
  8218. #ifndef NOLOCAL
  8219.       case OPN_SER:                     /* OPEN PORT or LINE */
  8220.       case OPN_NET: {                   /* OPEN HOST */
  8221.           extern int didsetlin, ttnproto;
  8222.           if (x == OPN_NET) {
  8223.               z = ttnproto;
  8224.               ttnproto = NP_NONE;
  8225.           }
  8226.           if ((y = setlin((x == OPN_SER) ? XYLINE : XYHOST, 1, 0)) < 0) {
  8227.               if (x == OPN_NET)
  8228.                 ttnproto = z;
  8229.               success = 0;
  8230.           }
  8231.           didsetlin++;
  8232.           return(y);
  8233.       }
  8234. #endif /* NOLOCAL */
  8235.  
  8236.       default:
  8237.         printf("?Not implemented");
  8238.         return(-2);
  8239.     }
  8240. }
  8241. #endif /* NOSPL */
  8242.  
  8243. #ifndef NOXFER
  8244. /*  D O X G E T  --  GET command parser with switches  */
  8245.  
  8246. #ifdef CK_LABELED
  8247. int g_lf_opts = -1;
  8248. extern int lf_opts;
  8249. #endif /* CK_LABELED */
  8250.  
  8251. int
  8252. doxget(cx) int cx; {
  8253.     extern int                          /* External variables we need */
  8254. #ifdef RECURSIVE
  8255.       recursive,
  8256. #endif /* RECURSIVE */
  8257.       xfermode, fdispla, protocol, usepipes,
  8258.       g_binary, g_xfermode, g_displa, g_rpath, g_usepipes;
  8259.     extern char * rcv_move;             /* Directory to move new files to */
  8260.     extern char * rcv_rename;           /* What to rename new files to */
  8261.     extern char * rcvexcept[];          /* RECEIVE / GET exception list */
  8262.     int opkt  =  0;                     /* Flag for O-Packet needed */
  8263.  
  8264. #ifdef PIPESEND
  8265.     extern int pipesend;
  8266.     extern char * rcvfilter;
  8267. #endif /* PIPESEND */
  8268.     extern struct keytab rpathtab[];
  8269.     extern int nrpathtab;
  8270.     extern long calibrate;
  8271.     int asname = 0;                     /* Flag for have as-name */
  8272.     int konly = 0;                      /* Kermit-only function */
  8273.     int c, i, n, confirmed = 0;         /* Workers */
  8274.     int getval = 0;                     /* Whether to get switch value */
  8275.     int rcvcmd = 0;                     /* Whether it is the RECEIVE command */
  8276.     int mget = 0;                       /* Whether it is the MGET command */
  8277.     struct stringint {                  /* Temporary array for switch values */
  8278.         char * sval;
  8279.         int ival;
  8280.     } pv[SND_MAX+1];
  8281.     struct FDB sw, fl, cm;              /* FDBs for each parse function */
  8282.     char * cmdstr = "this command";
  8283.  
  8284. #ifdef NEWFTP
  8285.     if (cx == XXGET || cx == XXREGET || cx == XXMGET || cx == XXRETR) {
  8286.         extern int ftpget;
  8287.         extern int ftpisopen();
  8288.         if ((ftpget == 1) || ((ftpget == 2) && ftpisopen()))
  8289.           return(doftpget(cx,0));
  8290.     }
  8291. #endif /* NEWFTP */
  8292.  
  8293.     debug(F101,"xget cx","",cx);
  8294.  
  8295.     oopts = -1;
  8296.     omode = -1;
  8297.  
  8298.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  8299.         pv[i].sval = NULL;
  8300.         pv[i].ival = -1;
  8301.     }
  8302.     /* Preset switch values based on top-level command that called us */
  8303.  
  8304.     switch (cx) {
  8305.       case XXREC:                       /* RECEIVE */
  8306.         cmdstr = "RECEIVE";
  8307.         rcvcmd = 1; break;
  8308.       case XXGET:                       /* GET */
  8309.         cmdstr = "GET";
  8310.         konly = 1;
  8311.         break;
  8312. #ifdef CK_RESEND
  8313.       case XXREGET:                     /* REGET */
  8314.         cmdstr = "REGET";
  8315.         konly = 1;
  8316.         pv[SND_BIN].ival = 1;           /* Implies /BINARY */
  8317.         pv[SND_RES].ival = 1; break;
  8318. #endif /* CK_RESEND */
  8319.       case XXRETR:                      /* RETRIEVE */
  8320.         cmdstr = "RETRIEVE";
  8321.         konly = 1;
  8322.         pv[SND_DEL].ival = 1; break;
  8323. #ifdef PIPESEND
  8324.       case XXCREC:                      /* CRECEIVE */
  8325.         cmdstr = "CRECEIVE";
  8326.         konly = 1;
  8327.         rcvcmd = 1;
  8328.         pv[SND_CMD].ival = 1; break;
  8329.       case XXCGET:                      /* CGET */
  8330.         cmdstr = "CGET";
  8331.         konly = 1;
  8332.         pv[SND_CMD].ival = 1; break;
  8333. #endif /* PIPESEND */
  8334. #ifndef NOMGET
  8335.       case XXMGET:                      /* MGET */
  8336.         cmdstr = "MGET";
  8337.         konly = 1;
  8338.         mget = 1; break;
  8339. #endif /* NOMGET */
  8340.     }
  8341.     debug(F111,"xget rcvcmd",cmdstr,rcvcmd);
  8342.     debug(F101,"xget konly","",konly);
  8343.  
  8344. #ifdef CK_XYZ
  8345.     if (!rcvcmd && protocol != PROTO_K) {
  8346.         printf("?Sorry, %s works only with Kermit protocol\n",cmdstr);
  8347.         return(-9);
  8348.     }
  8349. #endif /* CK_XYZ */
  8350.  
  8351.     /* Set up chained parse functions... */
  8352.  
  8353.     cmfdbi(&sw,                         /* First FDB - command switches */
  8354.            _CMKEY,                      /* fcode */
  8355.            rcvcmd ?
  8356.            "Optional name/template to store incoming files under, or switch" :
  8357.            "Remote filename, or switch", /* hlpmsg */
  8358.            "",                          /* default */
  8359.            "",                          /* addtl string data */
  8360.            rcvcmd ? nrcvtab : ngettab,  /* addtl numeric data 1: tbl size */
  8361.            4,                           /* addtl numeric data 2: 4 = cmswi */
  8362.            xxstring,                    /* Processing function */
  8363.            rcvcmd ? rcvtab : gettab,    /* Keyword table */
  8364.            &fl                          /* Pointer to next FDB */
  8365.            );
  8366.     if (rcvcmd || mget)                 /* RECEIVE or MGET */
  8367.       cmfdbi(&fl,
  8368.            _CMTXT,                      /* fcode */
  8369.            rcvcmd ?                     /* hlpmsg */
  8370.              "Output filename or Command" : /* Output filename */
  8371.              "File(s) to GET",              /* Files we are asking for */
  8372.            "",                          /* default */
  8373.            "",                          /* addtl string data */
  8374.            0,                           /* addtl numeric data 1 */
  8375.            0,                           /* addtl numeric data 2 */
  8376. #ifdef CK_XYZ
  8377.            (protocol == PROTO_X || protocol == PROTO_XC) ?
  8378.              xxstring :
  8379.              (rcvcmd ? (xx_strp)0  : xxstring)
  8380. #else
  8381.            rcvcmd ? (xx_strp)0  : xxstring /* Processing function */
  8382. #endif /* CK_XYZ */
  8383.              ,
  8384.            NULL,
  8385.            &cm
  8386.            );
  8387.     else
  8388.       cmfdbi(&fl,                       /* Remote filename or command */
  8389.            _CMFLD,                      /* fcode */
  8390.            "Remote filename",           /* hlpmsg */
  8391.            "",                          /* default */
  8392.            "",                          /* addtl string data */
  8393.            0,                           /* addtl numeric data 1 */
  8394.            0,                           /* addtl numeric data 2 */
  8395.            xxstring,
  8396.            NULL,
  8397.            &cm
  8398.            );
  8399.     cmfdbi(&cm,                         /* Confirmation */
  8400.            _CMCFM,                      /* fcode */
  8401.            "",                          /* hlpmsg */
  8402.            "",                          /* default */
  8403.            "",                          /* addtl string data */
  8404.            0,                           /* addtl numeric data 1 */
  8405.            0,                           /* addtl numeric data 2 */
  8406.            NULL,
  8407.            NULL,
  8408.            NULL
  8409.            );
  8410.  
  8411.     /* (See doxsend() for fuller commentary) */
  8412.  
  8413.     while (1) {                         /* Parse 0 or more switches */
  8414.         x = cmfdb(&sw);                 /* Parse something */
  8415.         debug(F101,"xget cmfdb","",x);
  8416.         if (x < 0)                      /* Error */
  8417.           goto xgetx;                   /* or reparse needed */
  8418.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  8419.           break;
  8420.         c = cmgbrk();                   /* Get break character */
  8421.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  8422.             printf("?This switch does not take an argument\n");
  8423.             x = -9;
  8424.             goto xgetx;
  8425.         }
  8426.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  8427.             printf("?This switch requires an argument\n");
  8428.             x = -9;
  8429.             goto xgetx;
  8430.         }
  8431.         n = cmresult.nresult;           /* Numeric result = switch value */
  8432.         debug(F101,"xget switch","",n);
  8433.  
  8434.         switch (n) {                    /* Process the switch */
  8435. #ifdef PIPESEND
  8436.           case SND_CMD:                 /* These take no args */
  8437.             if (nopush) {
  8438.                 printf("?Sorry, system command access is disabled\n");
  8439.                 x = -9;
  8440.                 goto xgetx;
  8441.             } else if (rcvfilter) {
  8442.                 printf(
  8443. "?Sorry, no GET /COMMAND when RECEIVE FILTER selected\n");
  8444.                 x = -9;
  8445.                 goto xgetx;
  8446.             }
  8447.             if (rcvcmd)
  8448.               sw.hlpmsg = "Command, or switch"; /* Change help message */
  8449.             /* Fall thru... */
  8450. #endif /* PIPESEND */
  8451.  
  8452.           case SND_REC:                 /* /RECURSIVE */
  8453.             pv[SND_PTH].ival = PATH_REL; /* Implies relative pathnames */
  8454.             pv[n].ival = 1;             /* Set the recursive flag */
  8455.             break;
  8456.  
  8457.           case SND_RES:                 /* /RECOVER */
  8458.             pv[SND_BIN].ival = 1;       /* Implies /BINARY */
  8459.             pv[n].ival = 1;             /* Set the resend flag */
  8460.             break;
  8461.  
  8462.           case SND_DEL:                 /* /DELETE */
  8463.           case SND_SHH:                 /* /QUIET */
  8464.           case SND_CAL:                 /* /CALIBRATE */
  8465.           case SND_XPA:                 /* /TRANSPARENT */
  8466.             pv[n].ival = 1;             /* Just set the appropriate flag */
  8467.             break;
  8468.  
  8469.           case SND_PIP:                 /* /PIPES:{ON,OFF} */
  8470.             if (!getval) {
  8471.                 pv[n].ival = 1;
  8472.                 break;
  8473.             }
  8474.             if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  8475.               goto xgetx;
  8476.             if (!nopush)
  8477.               pv[n].ival = x;
  8478.             break;
  8479.  
  8480.           /* File transfer modes - each undoes the others */
  8481.  
  8482.           case SND_BIN:                 /* Binary */
  8483.           case SND_TXT:                 /* Text */
  8484.           case SND_IMG:                 /* Image */
  8485.           case SND_LBL:                 /* Labeled */
  8486.             pv[SND_BIN].ival = 0;       /* Unset all */
  8487.             pv[SND_TXT].ival = 0;
  8488.             pv[SND_IMG].ival = 0;
  8489.             pv[SND_LBL].ival = 0;
  8490.             pv[n].ival = 1;             /* Set the requested one */
  8491.             break;
  8492.  
  8493.           case SND_EXC:                 /* Excludes */
  8494.             if (!getval) break;
  8495.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  8496.                 if (x == -3) {
  8497.                     printf("?Pattern required\n");
  8498.                     x = -9;
  8499.                 }
  8500.                 goto xgetx;
  8501.             }
  8502.             if (pv[n].sval) free(pv[n].sval);
  8503.             y = strlen(s);
  8504.             if (y > 256) {
  8505.                 printf("?Pattern too long - 256 max\n");
  8506.                 x = -9;
  8507.                 goto xgetx;
  8508.             }
  8509.             pv[n].sval = malloc(y+1);
  8510.             if (pv[n].sval) {
  8511.                 strcpy(pv[n].sval,s);   /* safe */
  8512.                 pv[n].ival = 1;
  8513.             }
  8514.             break;
  8515.  
  8516. #ifdef COMMENT
  8517.           /* Not implemented */
  8518.           case SND_PRI:                 /* GET to printer */
  8519.             pv[n].ival = 1;
  8520.             if (!getval) break;
  8521.             if ((x = cmfld("Print options","",&s,xxstring)) < 0)
  8522.               goto xgetx;
  8523.             pv[n].sval = malloc((int)strlen(s)+1);
  8524.             if (pv[n].sval)
  8525.               strcpy(pv[n].sval,s);     /* safe */
  8526.             break;
  8527. #endif /* COMMENT */
  8528.  
  8529.           case SND_MOV:                 /* MOVE after */
  8530.           case SND_REN:                 /* RENAME after */
  8531.             if (!getval) break;
  8532.             if ((x = cmfld(n == SND_MOV ?
  8533.            "device and/or directory for source file after sending" :
  8534.            "new name for source file after sending",
  8535.                            "",
  8536.                            &s,
  8537.                            n == SND_MOV ? xxstring : NULL
  8538.                            )) < 0) {
  8539.                 if (x == -3) {
  8540.                     printf("%s\n", n == SND_MOV ?
  8541.                            "?Destination required" :
  8542.                            "?New name required"
  8543.                            );
  8544.                     x = -9;
  8545.                 }
  8546.                 goto xgetx;
  8547.             }
  8548.             if (pv[n].sval) {
  8549.                 free(pv[n].sval);
  8550.                 pv[n].sval = NULL;
  8551.             }
  8552.             s = brstrip(s);
  8553.             y = strlen(s);
  8554.             if (y > 0) {
  8555.                 pv[n].sval = malloc(y+1);
  8556.                 if (pv[n].sval) {
  8557.                     strcpy(pv[n].sval,s); /* safe */
  8558.                     pv[n].ival = 1;
  8559.                 }
  8560.             }
  8561.             break;
  8562.  
  8563.           case SND_ASN:                 /* As-name */
  8564.             if (!getval) break;
  8565.             if (mget) {
  8566.                 printf("?Sorry, as-name not allowed with MGET\n");
  8567.                 x = -9;
  8568.                 goto xgetx;
  8569.             }
  8570.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0)
  8571.               goto xgetx;
  8572.             s = brstrip(s);
  8573.             if ((y = strlen(s)) > 0) {
  8574.                 if (pv[n].sval) free(pv[n].sval);
  8575.                 pv[n].sval = malloc(y+1);
  8576.                 if (pv[n].sval) {
  8577.                     strcpy(pv[n].sval,s); /* safe */
  8578.                     pv[n].ival = 1;
  8579.                 }
  8580.             }
  8581.             break;
  8582.  
  8583. #ifdef PIPESEND
  8584.           case SND_FLT:                 /* Filter */
  8585.             debug(F101,"xget /filter getval","",getval);
  8586.             if (!getval) break;
  8587.             if ((x = cmfld("Filter program to receive through",
  8588.                            "",&s,NULL)) < 0) {
  8589.                 if (x == -3)
  8590.                   s = "";
  8591.                 else
  8592.                   goto xgetx;
  8593.             }
  8594.             if (*s) s = brstrip(s);
  8595.             y = strlen(s);
  8596.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  8597.                 if (s[x] != '\\') continue;
  8598.                 if (s[x+1] == 'v') break;
  8599.             }
  8600.             if (x == y) {
  8601.                 printf(
  8602.                 "?Filter must contain a replacement variable for filename.\n"
  8603.                        );
  8604.                 x = -9;
  8605.                 goto xgetx;
  8606.             }
  8607.             pv[n].ival = 1;
  8608.             if (pv[n].sval) {
  8609.                 free(pv[n].sval);
  8610.                 pv[n].sval = NULL;
  8611.             }
  8612.             if ((y = strlen(s)) > 0) {
  8613.                 if ((pv[n].sval = malloc(y+1)))
  8614.                   strcpy(pv[n].sval,s); /* safe */
  8615.             }
  8616.             break;
  8617. #endif /* PIPESEND */
  8618.  
  8619.           case SND_PTH:                 /* Pathnames */
  8620.             if (!getval) {
  8621.                 pv[n].ival = PATH_REL;
  8622.                 break;
  8623.             }
  8624.             if ((x = cmkey(rpathtab,nrpathtab,"","on",xxstring)) < 0)
  8625.               goto xgetx;
  8626.             pv[n].ival = x;             /* Ditto */
  8627.             break;
  8628.  
  8629.           case SND_NAM:                 /* Filenames */
  8630.             if (!getval) break;
  8631.             if ((x = cmkey(fntab,nfntab,"","converted",xxstring)) < 0)
  8632.               goto xgetx;
  8633.             pv[n].ival = x;
  8634.             break;
  8635.  
  8636.           case SND_PRO:                 /* Protocol to use */
  8637.             if (!getval) break;
  8638.             if ((x = cmkey(protos,nprotos,"File-transfer protocol","",
  8639.                            xxstring)) < 0) {
  8640.                 if (x == -3)
  8641.                   x = 0;
  8642.                 else
  8643.                   goto xgetx;
  8644.             }
  8645.             debug(F111,"xget /proto",atmbuf,x);
  8646.             pv[n].ival = x;
  8647.             if (konly && x != PROTO_K) {
  8648.                 printf(
  8649. "?Sorry, this command works only with Kermit protocol\n"
  8650.                        );
  8651.                 x = -9;
  8652.                 goto xgetx;
  8653.             }
  8654.             break;
  8655.  
  8656.           default:
  8657.             printf("?Unexpected switch value - %d\n",cmresult.nresult);
  8658.             x = -9;
  8659.             goto xgetx;
  8660.         }
  8661.     }
  8662.     debug(F101,"xget cmresult fcode","",cmresult.fcode);
  8663.  
  8664.     cmarg = line;                       /* Initialize string pointers */
  8665.     cmarg2 = tmpbuf;
  8666.     asname = 0;
  8667.     line[0] = NUL;                      /* and buffers. */
  8668.     tmpbuf[0] = NUL;
  8669.  
  8670.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  8671.       case _CMFLD:                      /* (3) Remote filespec */
  8672.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  8673.         break;
  8674.       case _CMTXT:                      /* (4) As-name */
  8675.         if (rcvcmd) {
  8676.             ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ);
  8677.             if ((int)strlen(tmpbuf) > 0)
  8678.               asname = 1;
  8679.         } else {
  8680.             ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  8681.         }
  8682.       case _CMCFM:                      /* (6) Confirmation */
  8683.         confirmed = 1;
  8684.         break;
  8685.       default:
  8686.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  8687.         x = -9;
  8688.         goto xgetx;
  8689.     }
  8690.     debug(F110,"xget string",cmarg,0);
  8691.     debug(F101,"xget confirmed","",confirmed);
  8692.  
  8693.     cmarg = brstrip(cmarg);             /* Strip any braces */
  8694.  
  8695.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  8696.         if (pv[SND_CMD].ival > 0) {
  8697.             debug(F100,"xget calling cmtxt","",0);
  8698.             x = cmtxt("Local command to pipe into","",&s,NULL);
  8699.             if (x < 0 && x != -3) goto xgetx;
  8700.             if (x != -3) {
  8701.                 ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8702.                 asname = 1;
  8703.             }
  8704.         } else if (!rcvcmd) {
  8705. #ifdef VMS
  8706.             /* cmofi() fails if you give it a directory name */
  8707.             x = cmfld("Name or directory for incoming file","",&s,NULL);
  8708.             debug(F111,"xget cmfld",s,x);
  8709. #else
  8710.             x = cmofi("Name or directory for incoming file","",&s,NULL);
  8711.             debug(F111,"xget cmofi",s,x);
  8712. #endif /* VMS */
  8713.             if (x < 0 && x != -3) goto xgetx;
  8714.             if (x != -3) {
  8715.                 ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8716.                 if ((x = cmcfm()) < 0) goto xgetx;
  8717.                 asname = 1;
  8718.             }
  8719.         }
  8720.     }
  8721.     /* Arrive here with cmarg and cmarg2 all set */
  8722.  
  8723.     debug(F111,"xget asname",cmarg2,asname);
  8724.     if (!asname) {
  8725.         if (pv[SND_ASN].sval)
  8726.           ckstrncpy(tmpbuf,pv[SND_ASN].sval,TMPBUFSIZ);
  8727.         else
  8728.           tmpbuf[0] = NUL;
  8729.     }
  8730.     cmarg2 = brstrip(cmarg2);           /* Strip outer braces if any. */
  8731.     debug(F110,"xget cmarg",cmarg,0);
  8732.     debug(F110,"xget cmarg2",cmarg2,0);
  8733.  
  8734.     if (!*cmarg &&
  8735.         (cx == XXGET || cx == XXREGET || cx == XXCGET || cx == XXMGET)) {
  8736.         printf("?A remote file specification is required\n");
  8737.         x = -9;
  8738.         goto xgetx;
  8739.     }
  8740. #ifdef PIPESEND
  8741.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND sets pipesend flag */
  8742.         x = -9;
  8743.         if (!*cmarg2) {
  8744.             printf("?Command required\n");
  8745.             goto xgetx;
  8746.         } else if (nopush) {
  8747.             printf("?Sorry, system command access is disabled\n");
  8748.             goto xgetx;
  8749.         } else if (rcvfilter) {
  8750.             printf("?Sorry, no GET /COMMAND while RECEIVE FILTER selected\n");
  8751.             goto xgetx;
  8752.         } else
  8753.           pipesend = 1;
  8754.     }
  8755.     debug(F101,"xget /COMMAND pipesend","",pipesend);
  8756. #endif /* PIPESEND */
  8757.  
  8758. #ifdef CK_RESEND
  8759.     if (pv[SND_RES].ival > 0) {         /* REGET or GET /RECOVER */
  8760. #ifdef RECURSIVE
  8761.         if (pv[SND_REC].ival > 0) {     /* RECURSIVE */
  8762. #ifdef COMMENT
  8763.             printf("?Unsupported option combination: /RECOVER /RECURSIVE\n");
  8764.             x = -9;
  8765.             goto xgetx;
  8766. #else
  8767.             opkt = 1;
  8768. #endif /* COMMENT */
  8769.         }
  8770. #endif /* RECURSIVE */
  8771.         if (pv[SND_DEL].ival > 0) {     /* /DELETE */
  8772. #ifdef COMMENT
  8773.             printf("?Unsupported option combination: /RECOVER /DELETE\n");
  8774.             x = -9;
  8775.             goto xgetx;
  8776. #else
  8777.             opkt = 1;
  8778. #endif /* COMMENT */
  8779.         }
  8780.     }
  8781. #endif /* CK_RESEND */
  8782.  
  8783.     if (pv[SND_EXC].ival > 0)           /* /EXCEPT */
  8784.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  8785.  
  8786. #ifdef IKS_OPTION
  8787.     if (!rcvcmd
  8788. #ifdef CK_XYZ
  8789.          && protocol == PROTO_K
  8790. #endif /* CK_XYZ */
  8791.          ) {
  8792.         if (!iks_wait(KERMIT_REQ_START,1)) {
  8793.             printf(
  8794.               "?A Kermit Server is not available to process this command\n");
  8795.             x = -9;                     /* correct the return code */
  8796.             goto xgetx;
  8797.         }
  8798.     }
  8799. #endif /* IKS_OPTION */
  8800.  
  8801. #ifdef CK_XYZ
  8802.     {
  8803.         int po, pg;                     /* (for clarity) */
  8804.         po = pv[SND_PRO].ival;          /* /PROTOCOL option */
  8805.         pg = protocol;                  /* Protocol global  */
  8806.         if ((rcvcmd && !*cmarg2) &&     /* If no as-name was given */
  8807.             /* and /PROTOCOL is XMODEM or global protocol is XMODEM... */
  8808.             ((po <  0 && (pg == PROTO_X || pg == PROTO_XC)) ||
  8809.              (po > -1 && (po == PROTO_X || po == PROTO_XC)))
  8810.             ) {
  8811.             printf(
  8812. "Sorry, you must specify a name when receiving a file with XMODEM protocol\n"
  8813.                    );
  8814.             x = -9;
  8815.             goto xgetx;
  8816.         }
  8817.     }
  8818. #endif /* CK_XYZ */
  8819.  
  8820. #ifdef RECURSIVE
  8821.     if (pv[SND_REC].ival > 0) {         /* RECURSIVE */
  8822.         recursive = 1;
  8823.         pv[SND_PTH].ival = PATH_REL;    /* Implies relative pathnames too */
  8824.     }
  8825. #endif /* RECURSIVE */
  8826.  
  8827.     if (pv[SND_PIP].ival > -1) {
  8828.         g_usepipes = usepipes;
  8829.         usepipes = pv[SND_PIP].ival;
  8830.     }
  8831.  
  8832.     /* Save global protocol parameters */
  8833.  
  8834.     g_proto = protocol;
  8835. #ifdef CK_LABELED
  8836.     g_lf_opts = lf_opts;                /* Save labeled transfer options */
  8837. #endif /* CK_LABELED */
  8838.     g_urpsiz = urpsiz;                  /* Receive packet length */
  8839.     g_spsizf = spsizf;                  /* Send packet length flag */
  8840.     g_spsiz = spsiz;                    /* Send packet length */
  8841.     g_spsizr = spsizr;                  /* etc etc */
  8842.     g_spmax = spmax;
  8843.     g_wslotr = wslotr;
  8844.     g_prefixing = prefixing;
  8845.     g_fncact = fncact;
  8846.     g_fncnv = fncnv;
  8847.     g_fnspath = fnspath;
  8848.     g_fnrpath = fnrpath;
  8849.     g_xfrxla = xfrxla;
  8850.  
  8851.     if (pv[SND_PRO].ival > -1) {        /* Change according to switch */
  8852.         protocol = pv[SND_PRO].ival;
  8853.         if (ptab[protocol].rpktlen > -1)   /* copied from initproto() */
  8854.             urpsiz = ptab[protocol].rpktlen;
  8855.         if (ptab[protocol].spktflg > -1)
  8856.             spsizf = ptab[protocol].spktflg;
  8857.         if (ptab[protocol].spktlen > -1) {
  8858.             spsiz = ptab[protocol].spktlen;
  8859.             if (spsizf)
  8860.                 spsizr = spmax = spsiz;
  8861.         }
  8862.         if (ptab[protocol].winsize > -1)
  8863.             wslotr = ptab[protocol].winsize;
  8864.         if (ptab[protocol].prefix > -1)
  8865.             prefixing = ptab[protocol].prefix;
  8866.         if (ptab[protocol].fnca > -1)
  8867.             fncact  = ptab[protocol].fnca;
  8868.         if (ptab[protocol].fncn > -1)
  8869.             fncnv   = ptab[protocol].fncn;
  8870.         if (ptab[protocol].fnsp > -1)
  8871.             fnspath = ptab[protocol].fnsp;
  8872.         if (ptab[protocol].fnrp > -1)
  8873.             fnrpath = ptab[protocol].fnrp;
  8874.     }
  8875.     debug(F101,"xget protocol","",protocol);
  8876.     debug(F111,"xget cmarg2",cmarg2,xfermode);
  8877.  
  8878.     g_xfermode = xfermode;
  8879.     g_binary = binary;
  8880.     if (pv[SND_BIN].ival > 0) {         /* Change according to switch */
  8881.         xfermode = XMODE_M;
  8882.         binary = XYFT_B;                /* FILE TYPE BINARY */
  8883.         omode = GMOD_BIN;               /* O-Packet mode */
  8884.         debug(F101,"doxget /BINARY xfermode","",xfermode);
  8885.     } else if (pv[SND_TXT].ival > 0) {  /* Ditto for /TEXT */
  8886.         xfermode = XMODE_M;
  8887.         binary = XYFT_T;
  8888.         omode = GMOD_TXT;
  8889.         debug(F101,"doxget /TEXT xfermode","",xfermode);
  8890.     } else if (pv[SND_IMG].ival > 0) {
  8891.         xfermode = XMODE_M;
  8892. #ifdef VMS
  8893.         binary = XYFT_I;
  8894. #else
  8895.         binary = XYFT_B;
  8896. #endif /* VMS */
  8897.         omode = GMOD_TXT;
  8898.         debug(F101,"doxget /IMAGE xfermode","",xfermode);
  8899.     }
  8900. #ifdef CK_LABELED
  8901.     else if (pv[SND_LBL].ival > 0) {
  8902.         xfermode = XMODE_M;
  8903.         binary = XYFT_L;
  8904.         omode = GMOD_LBL;
  8905.         debug(F101,"doxget /LABELED xfermode","",xfermode);
  8906.     }
  8907. #endif /* CK_LABELED */
  8908.     debug(F101,"xget binary","",binary);
  8909.     debug(F101,"xget omode","",omode);
  8910.  
  8911.     if (pv[SND_XPA].ival > 0)           /* /TRANSPARENT */
  8912.       xfrxla = 0;                       /* Don't translate character sets */
  8913.  
  8914. #ifdef PIPESEND
  8915.     if (pv[SND_FLT].ival > 0)
  8916.       makestr(&rcvfilter,pv[SND_FLT].sval);
  8917. #endif /* PIPESEND */
  8918.  
  8919. #ifdef CK_TMPDIR
  8920.     if (pv[SND_MOV].ival > 0) {
  8921.         int len;
  8922.         char * p = pv[SND_MOV].sval;
  8923. #ifdef CK_LOGIN
  8924.         if (isguest) {
  8925.             printf("?Sorry, /MOVE-TO not available to guests\n");
  8926.             x = -9;
  8927.             goto xgetx;
  8928.         }
  8929. #endif /* CK_LOGIN */
  8930.         len = strlen(p);
  8931.         if (!isdir(p)) {                /* Check directory */
  8932. #ifdef CK_MKDIR
  8933.             char * s = NULL;
  8934.             s = (char *)malloc(len + 4);
  8935.             if (s) {
  8936.                 strcpy(s,p);            /* safe */
  8937. #ifdef datageneral
  8938.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  8939. #else
  8940.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  8941. #endif /* datageneral */
  8942.                 s[len++] = 'X';
  8943.                 s[len] = NUL;
  8944.                 x = zmkdir(s);
  8945.                 free(s);
  8946.                 if (x < 0) {
  8947.                     printf("?Can't create \"%s\"\n",p);
  8948.                     x = -9;
  8949.                     goto xgetx;
  8950.                 }
  8951.             }
  8952. #else
  8953.             printf("?Directory \"%s\" not found\n",p);
  8954.             x = -9;
  8955.             goto xgetx;
  8956. #endif /* CK_MKDIR */
  8957.         }
  8958.         makestr(&rcv_move,p);
  8959.     }
  8960. #endif /* CK_TMPDIR */
  8961.  
  8962.     if (pv[SND_REN].ival > 0) {         /* /RENAME-TO:name */
  8963.         char * p = pv[SND_REN].sval;
  8964. #ifdef CK_LOGIN
  8965.         if (isguest) {
  8966.             printf("?Sorry, /RENAME-TO not available to guests\n");
  8967.             x = -9;
  8968.             goto xgetx;
  8969.         }
  8970. #endif /* CK_LOGIN */
  8971.         if (!p) p = "";
  8972.         if (!*p) {
  8973.             printf("?New name required for /RENAME\n");
  8974.             x = -9;
  8975.             goto xgetx;
  8976.         }
  8977.         p = brstrip(p);
  8978.         makestr(&rcv_rename,p);
  8979.         debug(F110,"xget rcv_rename","",0);
  8980.     }
  8981.  
  8982. #ifdef CALIBRATE
  8983.     if (pv[SND_CAL].ival > 0)
  8984.       calibrate = 1L;
  8985. #endif /* CALIBRATE */
  8986.     g_displa = fdispla;
  8987.     if (pv[SND_SHH].ival > 0)
  8988.       fdispla = 0;
  8989.     debug(F101,"xget display","",fdispla);
  8990.  
  8991.     if (pv[SND_NAM].ival > -1) {        /* /FILENAMES */
  8992.         g_fncnv = fncnv;                /* Save global value */
  8993.         fncnv = pv[SND_NAM].ival;
  8994.         debug(F101,"xsend fncnv","",fncnv);
  8995.         /* We should also handle O packet filename option here */
  8996.         /* but we don't really need to since WHATAMI already handles it */
  8997.     }
  8998.     if (pv[SND_PTH].ival > -1) {        /* PATHNAMES */
  8999.         g_rpath = fnrpath;              /* Save global values */
  9000.         fnrpath = pv[SND_PTH].ival;
  9001.         debug(F101,"xsend fnrpath","",fnrpath);
  9002. #ifndef NZLTOR
  9003.         if (fnrpath != PATH_OFF) {
  9004.             g_fncnv = fncnv;
  9005.             fncnv = XYFN_L;
  9006.             debug(F101,"xsend fncnv","",fncnv);
  9007.         }
  9008.         /* We should also handle O packet pathname option here */
  9009.         /* but we don't really need to since WHATAMI already handles it */
  9010. #endif /* NZLTOR */
  9011.     }
  9012.  
  9013.     /* Set protocol start state */
  9014.  
  9015.     if (opkt) {                         /* Extended GET Options*/
  9016.         sstate = (CHAR) 'o';
  9017.         oopts = 0;
  9018.         if (pv[SND_DEL].ival > 0) oopts |= GOPT_DEL; /* GET /DELETE */
  9019.         if (pv[SND_RES].ival > 0) oopts |= GOPT_RES; /* GET /RECOVER */
  9020.         if (pv[SND_REC].ival > 0) oopts |= GOPT_REC; /* GET /RECURSIVE */
  9021.     } else if (rcvcmd)
  9022.       sstate = (CHAR) 'v';              /* RECEIVE or CRECEIVE */
  9023.     else if (pv[SND_DEL].ival > 0)
  9024.       sstate = (CHAR) 'h';              /* GET /DELETE (= RETRIEVE) */
  9025.     else if (pv[SND_RES].ival > 0)
  9026.       sstate = (CHAR) 'j';              /* GET /RECOVER (= REGET) */
  9027.     else
  9028.       sstate = (CHAR) 'r';              /* Regular GET */
  9029.     getcmd = 1;
  9030.     debug(F000,"xget sstate","",sstate);
  9031. #ifdef MAC
  9032.     what = W_RECV;
  9033.     scrcreate();
  9034. #endif /* MAC */
  9035.     if (local) {
  9036.         if (pv[SND_SHH].ival != 0)
  9037.           displa = 1;
  9038.         ttflui();
  9039.     }
  9040.     x = 0;
  9041. #ifdef PIPESEND
  9042.     if (pipesend)
  9043.       goto xgetx;
  9044. #endif /* PIPESEND */
  9045.  
  9046. #ifdef CK_TMPDIR
  9047. /*
  9048.   cmarg2 is also allowed to be a device or directory name;
  9049.   even the name of a directory that doesn't exist.
  9050. */
  9051.     y = strlen(cmarg2);
  9052.     debug(F111,"xget strlen(cmarg2)",cmarg2,y);
  9053.     if ((y > 0) &&
  9054. #ifdef OS2
  9055.         ((isalpha(cmarg2[0]) &&
  9056.          cmarg2[1] == ':' &&
  9057.          cmarg2[2] == NUL) ||
  9058.         (cmarg[y-1] == '/' || cmarg[y-1] == '\\') ||
  9059.         isdir(cmarg2))
  9060. #else
  9061. #ifdef UNIXOROSK
  9062.         (cmarg2[y-1] == '/' || isdir(cmarg2))
  9063. #else
  9064. #ifdef VMS
  9065.         (cmarg2[y-1] == ']' || cmarg2[y-1] == '>' || isdir(cmarg2))
  9066. #else
  9067. #ifdef STRATUS
  9068.         (cmarg2[y-1] == '>' || isdir(cmarg2))
  9069. #else
  9070. #ifdef datageneral
  9071.         (cmarg2[y-1] == ':' || cmarg[0] == ':' || isdir(cmarg2))
  9072. #else
  9073.         isdir(cmarg2)
  9074. #endif /* datageneral */
  9075. #endif /* STRATUS */
  9076. #endif /* VMS */
  9077. #endif /* UNIXOROSK */
  9078. #endif /* OS2 */
  9079.         ) {
  9080.         debug(F110,"doxget RECEIVE cmarg2 disk or dir",cmarg2,0);
  9081.         if (!f_tmpdir) {
  9082.             int x;
  9083.             s = zgtdir();
  9084.             if (s) {
  9085.                 ckstrncpy(savdir,s,TMPDIRLEN); /* remember old disk/dir */
  9086.                 f_tmpdir = 1;   /* and that we did this */
  9087.             } else {
  9088.                 printf("?Can't get current directory\n");
  9089.                 cmarg2 = "";
  9090.                 f_tmpdir = 0;
  9091.                 x = -9;
  9092.                 goto xgetx;
  9093.             }
  9094. #ifdef CK_MKDIR
  9095.             x = zchki(cmarg2);          /* Does as-name exist? */
  9096.             if (x == -1) {              /* Doesn't exist */
  9097.                 char * p = NULL;        /* Try to create it */
  9098.                 x = strlen(cmarg2);
  9099.                 if ((p = (char *)malloc(x+4))) {
  9100.                     sprintf(p,"%s%s",cmarg2,"x.x"); /* SAFE (prechecked) */
  9101.                     x = zmkdir(p);
  9102.                     free(p);
  9103.                     if (x < 0) {
  9104.                         printf("?Can't create %s\n",cmarg2);
  9105.                         x = -9;
  9106.                         goto xgetx;
  9107.                     }
  9108.                 }
  9109.             }
  9110. #endif /* CK_MKDIR */
  9111.             if (!zchdir(cmarg2)) {      /* change to given disk/directory, */
  9112.                 printf("?Can't access %s\n",cmarg2);
  9113.                 x = -9;
  9114.                 goto xgetx;
  9115.             }
  9116.             cmarg2 = "";
  9117.         }
  9118.     }
  9119. #endif /* CK_TMPDIR */
  9120.  
  9121.     ckstrncpy(fspec,cmarg,CKMAXPATH);   /* Note - this is a REMOTE filespec */
  9122.     debug(F111,"xget fspec",fspec,fspeclen);
  9123.     debug(F110,"xget cmarg2",cmarg2,0);
  9124.  
  9125.   xgetx:
  9126.     for (i = 0; i < SND_MAX; i++)
  9127.       if (pv[i].sval)
  9128.         free(pv[i].sval);
  9129.     return(x);
  9130. }
  9131. #endif /* NOXFER */
  9132.  
  9133. #ifndef NOSPL
  9134.  
  9135. /*
  9136.   D O G T A  --  Do _GETARGS or _PUTARGS Command.
  9137.  
  9138.   Used by XIF, FOR, WHILE, and SWITCH, each of which are implemented as
  9139.   2-level macros; the first level defines the macro, the second runs it.
  9140.   This routine hides the fact that they are macros by importing the
  9141.   macro arguments (if any) from two levels up, to make them available
  9142.   in the IF, FOR, SWITCH, and WHILE commands themselves; for example as
  9143.   loop indices, etc, and within the IF/FOR/WHILE/SWITCH body itself.
  9144.   _PUTARGS is in case we changed any of these variables or used SHIFT
  9145.   on them, so the new values won't be lost as we pop up the stack.
  9146. */
  9147. int
  9148. dogta(cx) int cx; {
  9149.     int i, n;
  9150.     char c, *p,  mbuf[4];
  9151.     extern int topargc, cmdint;
  9152.     extern char ** topxarg;
  9153.  
  9154.     if ((y = cmcfm()) < 0)
  9155.       return(y);
  9156.     debug(F101,"dogta cx","",cx);
  9157.     debug(F101,"dogta maclvl","",maclvl);
  9158.     if (cx == XXGTA) {
  9159.         debug(F101,"dogta _GETARGS maclvl","",maclvl);
  9160.     } else if (cx == XXPTA) {
  9161.         debug(F101,"dogta _PUTARGS maclvl","",maclvl);
  9162.     } else {
  9163.         return(-2);
  9164.     }
  9165.     if (maclvl < 1)
  9166.       return(success = 0);
  9167.  
  9168.     /* Make new copies of macro arguments /%0..9 */
  9169.  
  9170.     mbuf[0] = '%'; mbuf[1] = '0'; mbuf[2] = NUL; /* Argument name buf */
  9171.  
  9172.     if (cx == XXPTA) {                  /* Go NOINT because _PUTARGS */
  9173.         if (cmdint)                     /* temporarily changes maclvl. */
  9174.           connoi();                     /* Interrupts OFF. */
  9175.     }
  9176.     for (i = 0; i < 10; i++) {          /* For all args */
  9177.         c = (char) (i + '0');           /* Make name */
  9178.         mbuf[1] = (char) c;             /* Insert digit */
  9179.         if (cx == XXGTA) {              /* Get arg from level-minus-2 */
  9180.             if (maclvl == 1) p = g_var[c]; /* If at level 1 use globals 0..9 */
  9181.             else p = m_arg[maclvl-2][i];   /* Otherwise they're on the stack */
  9182.             addmac(mbuf,p);
  9183. #ifdef COMMENT
  9184.             if (maclvl > 1)
  9185.               makestr(&(m_line[maclvl]),m_line[maclvl-2]);
  9186. #endif /* COMMENT */
  9187.         } else if (cx == XXPTA) {       /* Put args level+2 */
  9188.             maclvl -= 2;                /* This is gross, it's because we're */
  9189.             addmac(mbuf,m_arg[maclvl+2][i]); /* adding macros two levels up */
  9190.             maclvl += 2;                     /* and addmac() uses maclvl. */
  9191.             count[cmdlvl - 2]  = count[cmdlvl];
  9192.             intime[cmdlvl - 2] = intime[cmdlvl];
  9193.             inpcas[cmdlvl - 2] = inpcas[cmdlvl];
  9194.             takerr[cmdlvl - 2] = takerr[cmdlvl];
  9195.             merror[cmdlvl - 2] = merror[cmdlvl];
  9196.             xquiet[cmdlvl - 2] = xquiet[cmdlvl];
  9197.         } else return(success = 0);     /* Bad call to this routine */
  9198.     }
  9199.     if (cx == XXPTA) {                  /* Restore interrupts if we */
  9200.         if (cmdint)                     /* turned them off above. */
  9201.           conint(trap,stptrap);
  9202.     }
  9203.     /* Now take care of the argument vector array \&_[], \v(return), */
  9204.     /* and \v(argc) by just copying the pointers. */
  9205.  
  9206.     if (cx == XXGTA) {                  /* GETARGS from 2 levels up */
  9207.         if (maclvl == 1) {
  9208.             a_ptr[0] = topxarg;         /* \&_[] array */
  9209.             a_dim[0] = topargc - 1;     /* Dimension doesn't include [0] */
  9210.             m_xarg[maclvl] = topxarg;
  9211.             n_xarg[maclvl] = topargc;   /* But \v(argc) does include \%0 */
  9212.             macargc[maclvl] = topargc;
  9213.             makestr(&(mrval[maclvl+1]),mrval[0]); /* (see vnlook()) */
  9214.         } else {
  9215.             a_ptr[0] = m_xarg[maclvl-2];
  9216.             a_dim[0] = n_xarg[maclvl-2];
  9217.             m_xarg[maclvl] = m_xarg[maclvl-2];
  9218.             n_xarg[maclvl] = n_xarg[maclvl-2];
  9219.             macargc[maclvl] = n_xarg[maclvl-2];
  9220.             makestr(&(mrval[maclvl+1]),mrval[maclvl-1]); /* (see vnlook()) */
  9221.  
  9222.         }
  9223.     } else {                            /* PUTARGS 2 levels up */
  9224.         if (maclvl > 1) {
  9225.             a_ptr[0] = m_xarg[maclvl];
  9226.             m_xarg[maclvl-2] = m_xarg[maclvl];
  9227.             a_dim[0] = n_xarg[maclvl];
  9228.             n_xarg[maclvl-2] = n_xarg[maclvl];
  9229.             macargc[maclvl-2] = n_xarg[maclvl];
  9230.         }
  9231.     }
  9232.     return(1);                  /* Internal command - don't change success */
  9233. }
  9234. #endif /* NOSPL */
  9235.  
  9236. #ifndef NOSPL
  9237. /*
  9238.   Do the GOTO and [_]FORWARD commands.
  9239.   s = Label to search for, cx = function code: XXGOTO, XXFWD, or XXXFWD.
  9240. */
  9241. #ifdef BIGBUFOK
  9242. #define LBLMAXLEN 255                   /* Max label length */
  9243. #else
  9244. #define LBLMAXLEN 63
  9245. #endif /* BIGBUFOK */
  9246.  
  9247. int
  9248. dogoto(s, cx) char *s; int cx; {
  9249.     int i, j, x, y, z, bc;
  9250.     int empty = 0, stopflg = 0;
  9251.     char * cmd;                         /* Name of this command */
  9252.     char tmplbl[LBLMAXLEN+1], *lp;      /* Current label from command stream */
  9253.     char tmp2[LBLMAXLEN+1];             /* SWITCH label conversion buffer */
  9254.     char tmp3[LBLMAXLEN+1];             /* Target label */
  9255.  
  9256.     stopflg = (cx == XXXFWD);           /* _FORWARD (used in SWITCH) */
  9257.     bc = 0;                             /* Brace counter */
  9258.  
  9259.     cmd = (cx == XXGOTO) ? "GOTO" : ((cx == XXFWD) ? "FORWARD" : "_FORWARD");
  9260.     if (!s) s = "";
  9261.     if (!*s) empty = 1;
  9262.  
  9263. #ifdef DEBUG
  9264.     if (deblog) {
  9265.         debug(F111,"GOTO command",cmd,cx);
  9266.         debug(F101,"GOTO cmdlvl","",cmdlvl);
  9267.         debug(F101,"GOTO maclvl","",maclvl);
  9268.         debug(F101,"GOTO tlevel","",tlevel);
  9269.         debug(F111,"GOTO target",s,empty);
  9270.     }
  9271. #endif /* DEBUG */
  9272.     debug(F110,cmd,s,0);
  9273.     ckstrncpy(tmp3+1,s,LBLMAXLEN-1);
  9274.     s = tmp3+1;
  9275.     if (*s != ':') {                    /* Make copy of label */
  9276.         tmp3[0] = ':';                  /* guaranteed to start with ":" */
  9277.         s--;
  9278.     }
  9279.     if (!stopflg && !empty) {
  9280.         if (s[1] == '.' || s[1] == SP || s[1] == NUL) {
  9281.             printf("?Bad label syntax - '%s'\n",s);
  9282.             return(success = 0);
  9283.         }
  9284.     }
  9285.     if (cmdlvl == 0) {
  9286.         printf("?Sorry, %s only works in a command file or macro\n",cmd);
  9287.         return(success = 0);
  9288.     }
  9289.     y = strlen(s);                      /* y = length of target label */
  9290.     debug(F111,cmd,s,y);
  9291.  
  9292.     while (cmdlvl > 0) {                /* As long as not at top level... */
  9293.         if (cmdstk[cmdlvl].src == CMD_MD) { /* GOTO inside macro */
  9294.             int i, m, flag;
  9295.             char *xp, *tp;
  9296.  
  9297.             /* GOTO: rewind the macro; FORWARD: start at current position */
  9298.  
  9299.             lp = (cx == XXGOTO) ? macx[maclvl] : macp[maclvl];
  9300.             m = (int)strlen(lp) - y + 1;
  9301.             debug(F010,"GOTO in macro",lp,0);
  9302.  
  9303.             flag = 1;                   /* flag for valid label position */
  9304.             for (i = 0; i < m; i++,lp++) { /* search for label in macro body */
  9305.                 if (*lp == '{')         /* But only at this level */
  9306.                   bc++;                 /* Anything inside braces is off */
  9307.                 else if (*lp == '}')    /* limits. */
  9308.                   bc--;
  9309.                 if (stopflg && bc > 0)  /* This is good for SWITCH */
  9310.                   continue;             /* but interferes with WHILE, etc. */
  9311.                 if (*lp == ',') {
  9312.                     flag = 1;
  9313.                     continue;
  9314.                 }
  9315.                 if (flag) {             /* If in valid label position */
  9316.                     if (*lp == SP)      /* eat leading spaces */
  9317.                       continue;
  9318.                     if (*lp != ':') {   /* Look for label introducer */
  9319.                         flag = 0;       /* this isn't it */
  9320.                         continue;       /* keep looking */
  9321.                     }
  9322.                 }
  9323.                 if (!flag)              /* We don't have a label */
  9324.                   continue;             /*  so keep looking... */
  9325.                 xp = lp; tp = tmplbl;   /* Copy the label from the macro */
  9326.                 j = 0;                  /* to make it null-terminated */
  9327.                 while ((*tp = *xp)) {
  9328.                     if (j++ > LBLMAXLEN) /* j = length of word from macro */
  9329.                       break;
  9330. #ifdef COMMENT
  9331.                     if (*tp < 33 || *tp == ',') /* Look for end of word */
  9332. #else
  9333.                     if (!*tp || *tp == ',')     /* Look for end of word */
  9334. #endif /* COMMENT */
  9335.                       break;
  9336.                     else tp++, xp++;    /* Next character */
  9337.                 }
  9338.                 *tp = NUL;              /* In case we stopped early */
  9339.                 /* Now do caseless string comparison, using longest length */
  9340.                 debug(F111,"macro GOTO label",s,y);
  9341.                 debug(F111,"macro target label",tmplbl,j);
  9342.                 if (stopflg) {          /* Allow variables as SWITCH labels */
  9343.                     int n = LBLMAXLEN - 1;
  9344.                     char * p = tmp2;
  9345.                     zzstring(tmplbl,&p,&n);
  9346.                     ckstrncpy(tmplbl,tmp2,LBLMAXLEN);
  9347.                     tmp2[49] = NUL;
  9348.                 }
  9349.                 debug(F111,"GOTO s",s,y);
  9350.                 debug(F111,"GOTO tmplbl",tmplbl,j);
  9351.                 debug(F101,"GOTO empty",ckitoa(stopflg),empty);
  9352.  
  9353.                 if (empty) {           /* Empty target */
  9354.             z = (!strcmp(s,":") && /* String is empty */
  9355.              /* and Label is ":" or ":*"... */
  9356.              (!strcmp(tmplbl,":") || !strcmp(tmplbl,":*")))
  9357.             ? 0 : 1;
  9358.             debug(F111,"GOTO","A",z);
  9359.                 } else if (stopflg) {
  9360.                     z = ckmatch(tmplbl,s,inpcas[cmdlvl],1) ? 0 : 1;
  9361.             debug(F111,"GOTO","B",z);
  9362.                 } else {
  9363.                     z = (stopflg && inpcas[cmdlvl]) ?
  9364.                       strcmp(s,tmplbl) :
  9365.                         ckstrcmp(s,tmplbl,(y > j) ? y : j, 0);
  9366.             debug(F111,"GOTO","C",z);
  9367.                 }
  9368.                 if (!z) {
  9369.                     break;
  9370.                 } else if (stopflg &&
  9371.                          !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j, 0)) {
  9372.                     debug(F100,"GOTO DEFAULT","",0);
  9373.                     break;
  9374.                 } else {
  9375.                     flag = 0;
  9376.                 }
  9377.             }
  9378.             debug(F111,"GOTO macro i",cmd,i);
  9379.             debug(F111,"GOTO macro m",cmd,m);
  9380.             if (i >= m) {               /* Didn't find the label */
  9381.                 debug(F101,"GOTO failed cmdlvl","",cmdlvl);
  9382. #ifdef COMMENT
  9383.         /* MOVED TO AFTER POPCLVL ABOUT 20 LINES DOWN 5 AUG 2002 */
  9384.            if (stopflg)
  9385.                   return(0);
  9386. #endif /* COMMENT */
  9387.                 if ((maclvl > 0) &&
  9388.                        (m_arg[maclvl-1][0]) &&
  9389.                        (cmdstk[cmdlvl].src == CMD_MD) &&
  9390.                        (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  9391.                         !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  9392.                         !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  9393.                         !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  9394.                     dogta(XXPTA);       /* Restore args */
  9395.                     debug(F101,"GOTO in XIF/FOR/WHI/SWI popping","",cmdlvl);
  9396.                     popclvl();          /* Pop an extra level */
  9397.                 }
  9398.                 debug(F101,"GOTO popping","",cmdlvl);
  9399.                 if (!popclvl()) {       /* pop up to next higher level */
  9400.                     printf("?Label '%s' not found\n",s); /* if none */
  9401.                     return(0);          /* Quit */
  9402.                 } else if (stopflg) {    /* SWITCH no case label match */
  9403.             return(0);        /* and no DEFAULT lable. */
  9404.         } else {
  9405.             continue;        /* otherwise look again */
  9406.         }
  9407.             }
  9408.             debug(F110,"GOTO found macro label",tmplbl,0);
  9409.             macp[maclvl] = lp;          /* set macro buffer pointer */
  9410.             return(1);
  9411.         } else if (cmdstk[cmdlvl].src == CMD_TF) {
  9412.             x = 0;                      /* GOTO issued in take file */
  9413.             debug(F111,"GOTO in TAKE file",cmd,cx);
  9414.             if (cx == XXGOTO) {         /* If GOTO, but not FORWARD, */
  9415.                 rewind(tfile[tlevel]);  /* search file from beginning */
  9416.                 tfline[tlevel] = 0;
  9417.             }
  9418.             while (! feof(tfile[tlevel])) {
  9419. #ifdef COMMENT
  9420. /* This is wrong - it lets us jump to labels inside inferior blocks */
  9421.                 tfline[tlevel]++;
  9422.                 if (fgets(line,LINBUFSIZ,tfile[tlevel]) == NULL) /* Get line */
  9423. #else
  9424.                 if (getnct(line,LINBUFSIZ,tfile[tlevel],0) < 0)
  9425. #endif /* COMMENT */
  9426.                   break;                /* If no more, done, label not found */
  9427.                 lp = line;              /* Got line */
  9428.                 while (*lp == SP || *lp == HT)
  9429.                   lp++;                 /* Strip leading whitespace */
  9430.                 if (*lp != ':') continue; /* Check for label introducer */
  9431.                 while (*(lp+1) == SP) { /* Remove space between : and name */
  9432.                     *(lp+1) = ':';
  9433.                     lp++;               /* Strip leading whitespace */
  9434.                 }
  9435.                 tp = lp;                /* Get end of word */
  9436.                 j = 0;
  9437.                 while (*tp) {           /* And null-terminate it */
  9438.                     if (*tp < 33) {
  9439.                         *tp = NUL;
  9440.                         break;
  9441.                     } else tp++, j++;
  9442.                 }
  9443.                 if (!ckstrcmp(lp,s,(y > j) ? y : j,0)) { /* Caseless compare */
  9444.                     x = 1;              /* Got it */
  9445.                     break;              /* done. */
  9446.                 } else if (stopflg &&
  9447.                            !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j,0)) {
  9448.                     x = 1;
  9449.                     break;
  9450.                 }
  9451.             }
  9452.             if (x == 0) {               /* If not found, print message */
  9453.                 debug(F101,"GOTO failed at cmdlvl","",cmdlvl);
  9454.                 if (stopflg)
  9455.                   return(0);
  9456.                 if (!popclvl()) {       /* pop up to next higher level */
  9457.                     printf("?Label '%s' not found\n",s); /* if none */
  9458.                     return(0);          /* quit */
  9459.                 } else continue;        /* otherwise look again */
  9460.             }
  9461.             return(x);                  /* Send back return code */
  9462.         }
  9463.     }
  9464.     printf("?Stack problem in GOTO %s\n",s); /* Shouldn't see this */
  9465.     return(0);
  9466. }
  9467. #endif /* NOSPL */
  9468.  
  9469. /* Finish parsing and do the IF, XIF, and WHILE commands */
  9470.  
  9471. #ifndef NOSPL
  9472.  
  9473. /*  C H K V A R  --  Check (if it's a) Variable  */
  9474.  
  9475.  
  9476. #ifdef OLDCHKVAR
  9477. /*
  9478.   Crude and disgusting, but needed for OS/2, DOS, and Windows, where filenames
  9479.   have backslashes in them.  How do we know if a backslash in a filename is a
  9480.   directory separator, or if it's a Kermit backslash?  This routine does a
  9481.   rough syntax check of the next few characters and if it looks like it MIGHT
  9482.   be a variable, then it tries to evaluate it, and if the result is not empty,
  9483.   we say it's a variable, although sometimes it might not be -- some cases are
  9484.   truly ambiguous.  For example there might a DOS directory called \%a, and
  9485.   we also have a variable with the same name.  This is all for the sake of not
  9486.   having to tell PC users that they have to double all backslashes in file
  9487.   and directory names.
  9488. */
  9489. #else
  9490. /*
  9491.   Somewhat less crude & disgusting.  The previous method was nondeterministic
  9492.   and (worse) it interfered with macro argument passing.  So now we only
  9493.   check the syntax of backslash-items to see if they are variables, but we
  9494.   do NOT check their values.
  9495. */
  9496. #endif /* OLDCHKVAR */
  9497. /*
  9498.   Call with a string pointer pointing at the backslash of the purported
  9499.   variable.  Returns 1 if it has the syntax of a variable, 0 if not.
  9500. */
  9501. int
  9502. chkvar(s) char *s; {
  9503.     int z = 0;                          /* Return code - assume failure. */
  9504.     if (!s) s = "";                     /* Watch our for null pointers. */
  9505.     if (!*s) return(0);                 /* Empty arg so not a variable. */
  9506.     if (*s == CMDQ) {                   /* Object begins with backslash. */
  9507.         char c;
  9508.         c = s[1];                       /* Character following backslash. */
  9509.         if (c) {
  9510.             int t = 0;
  9511.             if (c == CMDQ)              /* Quoted backslash */
  9512.               return(1);
  9513.             c = (char) (islower(c) ? toupper(c) : c); /* Otherwise... */
  9514.             if (c == '%') {             /* Simple variable */
  9515. #ifdef OLDCHKVAR
  9516.                 t = 1;
  9517. #else
  9518.                 return(1);
  9519. #endif /* OLDCHKVAR */
  9520.             } else if (c == '&') {      /* Array */
  9521.                 if (!s[2]) return(0);
  9522.                 if (s[3] == '[')
  9523.                   t = ckindex("]",s,4,0,1);
  9524. #ifndef OLDCHKVAR
  9525.                 return((t > 0) ? 1 : 0);
  9526. #endif /* OLDCHKVAR */
  9527.             } else if (c == '$' ||      /* Environment variable */
  9528.                        c == 'V' ||      /* Built-in variable */
  9529.                        c == 'M') {      /* Macro name */
  9530.                 t = (s[2] == '(');
  9531. #ifndef OLDCHKVAR
  9532.                 return((t > 0) ? 1 : 0);
  9533. #endif /* OLDCHKVAR */
  9534.             } else if (c == 'F') {      /* Function reference */
  9535.                 /* Don't actually call it - it might have side effects */
  9536.                 int x;
  9537.                 if ((x = ckindex("(",s,3,0,1))) /* Just check syntax */
  9538.                   if ((x = ckindex(")",s,x,0,1)))
  9539.                     z = 1;
  9540.                 /* Insert a better syntax check here if necessary */
  9541.             }
  9542. #ifdef OLDCHKVAR
  9543.             if (t) {
  9544.                 t = 255;                /* This lets us test \v(xxx) */
  9545.                 lp = line;              /* and even \f...(xxx) */
  9546.                 zzstring(s,&lp,&t);     /* Evaluate it, whatever it is. */
  9547.                 t = strlen(line);       /* Get its length. */
  9548.                 debug(F111,"chkvar",line,t);
  9549.                 z = t > 0;              /* If length > 0, it's defined */
  9550.             }
  9551. #endif /* OLDCHKVAR */
  9552.         }
  9553.     }
  9554.     return(z);
  9555. }
  9556.  
  9557. /*  B O O L E X P  --  Evaluate a Boolean expression  */
  9558.  
  9559. #define BOOLLEN 1024
  9560. static char boolval[BOOLLEN];
  9561.  
  9562. int
  9563. boolexp(cx) int cx; {
  9564.     int x, y, z; char *s, *p;
  9565.     int parens = 0, pcount = 0, ecount = 0;
  9566.     char *q, *bx;
  9567.     struct FDB kw, nu;
  9568. #ifdef FNFLOAT
  9569.     struct FDB fl;
  9570.     CKFLOAT f1 = 0.0, f2 = 0.0;
  9571.     int f1flag = 0, f2flag = 0;
  9572. #endif /* FNFLOAT */
  9573. #ifdef OS2
  9574.     extern int keymac;
  9575. #endif /* OS2 */
  9576.  
  9577.     not = 0;                            /* Flag for whether "NOT" was seen */
  9578.     z = 0;                              /* Initial IF condition */
  9579.     ifargs = 0;                         /* Count of IF condition words */
  9580.     bx = boolval;                       /* Initialize boolean value */
  9581.     *bx = NUL;
  9582.  
  9583.   ifagain:
  9584.     cmfdbi(&kw,                         /* First FDB - command switches */
  9585.            _CMKEY,                      /* fcode */
  9586.            "Number, numeric-valued variable, Boolean expression, or keyword",
  9587.            "",                          /* default */
  9588.            "",                          /* addtl string data */
  9589.            nif,                         /* addtl numeric data 1: tbl size */
  9590.            0,                           /* addtl numeric data 2: 4 = silent */
  9591.            xxstring,                    /* Processing function */
  9592.            iftab,                       /* Keyword table */
  9593.            &nu                          /* Pointer to next FDB */
  9594.            );
  9595.     cmfdbi(&nu,                         /* 2nd FDB - An integer */
  9596.            _CMNUM,                      /* fcode */
  9597.            "",                          /* hlpmsg */
  9598.            "",                          /* Default */
  9599.            "",                          /* addtl string data */
  9600.            0,
  9601.            0,
  9602.            xxstring,
  9603.            NULL,
  9604. #ifdef FNFLOAT
  9605.            &fl
  9606. #else
  9607.            NULL
  9608. #endif /* FNFLOAT */
  9609.            );
  9610. #ifdef FNFLOAT
  9611.     cmfdbi(&fl,                         /* A floating-point number */
  9612.            _CMFLD,                      /* fcode */
  9613.            "",                          /* hlpmsg */
  9614.            "",                          /* default */
  9615.            "",                          /* addtl string data */
  9616.            0,                           /* addtl numeric data 1 */
  9617.            0,                           /* addtl numeric data 2 */
  9618.            xxstring,
  9619.            NULL,
  9620.            NULL
  9621.            );
  9622. #endif /* FNFLOAT */
  9623.     x = cmfdb(&kw);                     /* Parse a keyword or a number */
  9624.     debug(F111,"boolval cmfdb","",x);
  9625.     if (x < 0) {
  9626.         if (x == -3)
  9627.           x = -2;
  9628.         return(x);
  9629.     }
  9630.     debug(F111,"boolval switch","",cmresult.fcode);
  9631.     switch (cmresult.fcode) {           /* What did we get? */
  9632. #ifdef FNFLOAT
  9633.       case _CMFLD:                      /* A "field" */
  9634.         if (isfloat(cmresult.sresult,0)) { /* A floating-point number? */
  9635.             f1 = floatval;              /* Yes, get its value */
  9636.             f1flag = 1;                 /* remember we did this */
  9637.             ifc = 9999;                 /* Set special "if-code" */
  9638.         } else
  9639.           return(-2);
  9640. #endif /* FNFLOAT */
  9641.       case _CMNUM:                      /* A number... */
  9642.         ifc = 9999;                     /* Set special "if-code" */
  9643.         break;
  9644.       case _CMKEY:                      /* A keyword */
  9645.         ifc = cmresult.nresult;         /* Get if-code */
  9646.         break;
  9647.       default:
  9648.         return(-2);
  9649.     }
  9650.     switch (ifc) {                      /* set z = 1 for true, 0 for false */
  9651.       case 9999:                        /* Number */
  9652. #ifdef FNFLOAT
  9653.         if (f1flag) {
  9654.             z = (f1 == 0.0) ? 0 : 1;
  9655.         } else
  9656. #endif /* FNFLOAT */
  9657.         z = (cmresult.nresult == 0) ? 0 : 1;
  9658.         break;
  9659.       case XXIFLP:                      /* Left paren */
  9660.         if (pcount == 0 && ifargs > 0)
  9661.           return(-2);
  9662.         parens = 1;
  9663.         pcount++;
  9664.         ifargs++;
  9665.         *bx++ = '(';
  9666.         goto ifagain;
  9667.       case XXIFRP:                      /* Right paren */
  9668.         if (!parens)
  9669.           return(-2);
  9670.         if (--pcount < 0)
  9671.           return(-2);
  9672.         ifargs++;
  9673.         *bx++ = ')';
  9674.         *bx = NUL;
  9675.         if (pcount == 0)
  9676.           goto ifend;
  9677.         goto ifagain;
  9678.       case XXIFAN:                      /* AND (&&) */
  9679.         ifargs++;
  9680.         if (!ecount)
  9681.           return(-2);
  9682.         *bx++ = '&';
  9683.         goto ifagain;
  9684.       case XXIFOR:                      /* OR (||) */
  9685.         ifargs++;
  9686.         if (!ecount)
  9687.           return(-2);
  9688.         *bx++ = '|';
  9689.         goto ifagain;
  9690.       case XXIFNO:                      /* IF NOT [ NOT [ NOT ... ] ] */
  9691.         if (bx > boolval) {             /* evala() doesn't like cascaded */
  9692.             if (*(bx-1) == '!') {       /* unary operators... */
  9693.                 *(bx-1) = NUL;          /* So here, two wrongs make a right. */
  9694.                 bx--;
  9695.             } else {
  9696.                 *bx++ = '!';
  9697.             }
  9698.         } else {
  9699.             *bx++ = '!';
  9700.         }
  9701.         ifargs++;
  9702.         goto ifagain;
  9703.       case XXIFTR:                      /* IF TRUE */
  9704.         z = 1;
  9705.         debug(F101,"if true","",z);
  9706.         ifargs += 1;
  9707.         break;
  9708.       case XXIFNT:                      /* IF FALSE */
  9709.         z = 0;
  9710.         debug(F101,"if true","",z);
  9711.         ifargs += 1;
  9712.         break;
  9713.       case XXIFSU:                      /* IF SUCCESS */
  9714.         z = ( success != 0 ) ? 1 : 0;
  9715.         debug(F101,"if success","",z);
  9716.         ifargs += 1;
  9717.         break;
  9718.       case XXIFFA:                      /* IF FAILURE */
  9719.         z = ( success == 0 ) ? 1 : 0;
  9720.         debug(F101,"if failure","",z);
  9721.         ifargs += 1;
  9722.         break;
  9723.  
  9724.       case XXIFDE:                      /* IF DEFINED */
  9725.         if ((x = cmfld("Macro or variable name","",&s,NULL)) < 0)
  9726.           return((x == -3) ? -2 : x);
  9727.  
  9728.         if (*s == CMDQ) {
  9729.             char * lp;
  9730.             char line[256];
  9731.             int t, x;
  9732.             if (*(s+1) == 'f' || *(s+1) == 'F') { /* Built-in function */
  9733.                 extern struct keytab fnctab[];
  9734.                 extern int nfuncs;
  9735.                 ckstrncpy(line,s+2,256);
  9736.                 if (line[0]) {
  9737.                     lp = ckstrchr(line,'(');
  9738.                     if (lp) *lp = NUL;
  9739.                     x = lookup(fnctab,line,nfuncs,&t);
  9740.                     z = x > -1;
  9741.                 }
  9742.                 debug(F111,"if defined function",line,z);
  9743.             } else if (*(s+1) == 'v' || *(s+1) == 'V') { /* 8.0.200 */
  9744.                 extern struct keytab vartab[];
  9745.                 extern int nvars;
  9746.                 z = 0;
  9747.                 if (*(s+2) == '(') {
  9748.                     ckstrncpy(line,s+3,256);
  9749.                     if (line[0]) {
  9750.                         lp = ckstrchr(line,')');
  9751.                         if (lp) *lp = NUL;
  9752.                         x = lookup(vartab,line,nvars,&t);
  9753.                         z = x > -1;
  9754.             if (z) {    /* 8.0.203 */
  9755.                 int t;    /* It must have a value to succeed */
  9756.                 t = 255;    /* as in C-Kermit 6.0 and 7.0 */
  9757.                 lp = line;    /* (this was broken in 8.0.200-201) */
  9758.                 zzstring(s,&lp,&t);
  9759.                 t = strlen(line);
  9760.                 z = line[0] ? 1 : 0;
  9761.             }
  9762.                     }
  9763.                 }
  9764.                 debug(F111,"if defined variable",line,z);
  9765.             } else {
  9766.                 z = chkvar(s);          /* Starts with backslash */
  9767.                 if (z > 0) {            /* Yes... */
  9768.                     t = 255;            /* than buffer so if zzstring fails  */
  9769.                     lp = line;          /* check for that -- overflow means */
  9770.                     line[0] = NUL;      /* the quantity is defined. */
  9771.                     x = zzstring(s,&lp,&t);
  9772.                     if ((x < 0 && t != 255) || !line[0])
  9773.                       z = 0;
  9774.                     debug(F111,"if defined zzstring",line,z);
  9775.                     debug(F101,"if defined zzstring t","",t);
  9776.                 }
  9777.             }
  9778.         } else {
  9779.             z = (mxlook(mactab,s,nmac) > -1); /* Look for exact match */
  9780.         }
  9781.         debug(F111,"if defined final",s,z);
  9782.         ifargs += 2;
  9783.         break;
  9784.  
  9785.       case XXIFDC: {                    /* IF DECLARED */
  9786.           char * lp;
  9787.           char line[32];
  9788.           int j, k, t, x;
  9789.           if ((x = cmfld("Array name","",&s,NULL)) < 0)
  9790.             return((x == -3) ? -2 : x);
  9791.           if (*s == CMDQ) {
  9792.               if (*(s+1) != '&') {
  9793.                   t = 31;
  9794.                   lp = line;
  9795.                   line[0] = NUL;
  9796.                   x = zzstring(s,&lp,&t);
  9797.                   s = line;
  9798.               }
  9799.           }
  9800.           z = 0;
  9801.           if ((x = arraybounds(s,&j,&k)) > -1) {
  9802.               if (a_ptr[x]) {
  9803.                   if (j < 1)
  9804.                     z = 1;
  9805.                   else if (j <= a_dim[x])
  9806.                     z = 1;
  9807.                   if (z == 1 && k > a_dim[x])
  9808.                     z = 0;
  9809.               }
  9810.           }
  9811.           break;
  9812.       }
  9813.       case XXIFBG:                      /* IF BACKGROUND */
  9814.       case XXIFFG:                      /* IF FOREGROUND */
  9815.         bgchk();                        /* Check background status */
  9816.         if (ifc == XXIFFG)              /* Foreground */
  9817.           z = pflag ? 1 : 0;
  9818.         else z = pflag ? 0 : 1;         /* Background */
  9819.         ifargs += 1;
  9820.         break;
  9821.  
  9822.       case XXIFCO:                      /* IF COUNT */
  9823.         z = ( --count[cmdlvl] > 0 );
  9824.         if (cx == XXWHI) count[cmdlvl] += 2; /* Don't ask... */
  9825.         debug(F101,"if count","",z);
  9826.         ifargs += 1;
  9827.         break;
  9828.  
  9829.       case XXIFEX:                      /* IF EXIST */
  9830. #ifdef CK_TMPDIR
  9831.       case XXIFDI:                      /* IF DIRECTORY */
  9832. #endif /* CK_TMPDIR */
  9833.       case XXIFAB:                      /* IF ABSOLUTE */
  9834.         if ((x = cmfld(
  9835.                        ((ifc == XXIFDI) ? "Directory name" : "File"),
  9836.                        "",&s,
  9837. #ifdef OS2
  9838.                        NULL             /* This allows \'s in filenames */
  9839. #else
  9840.                        xxstring
  9841. #endif /* OS2 */
  9842.                        )) < 0) {
  9843.             if (x == -3) {
  9844.                 extern int cmflgs;
  9845.                 if (cmflgs == 1) {
  9846.                     printf("?File or directory name required\n");
  9847.                     return(-9);
  9848.                 }
  9849.             } else return(x);
  9850.         }
  9851.         s = brstrip(s);
  9852.         if (ifc == XXIFAB) {
  9853.             z = isabsolute(s);
  9854.         } else if (ifc == XXIFEX) {
  9855.             z = (zgetfs(s) > -1L);
  9856.             debug(F101,"if exist 1","",z);
  9857. #ifdef OS2
  9858.             if (!z) {                   /* File not found. */
  9859.                 int t;                  /* Try expanding variables */
  9860.                 t = LINBUFSIZ-1;        /* and looking again. */
  9861.                 lp = line;
  9862.                 zzstring(s,&lp,&t);
  9863.                 s = line;
  9864.                 z = ( zchki(s) > -1L );
  9865.                 debug(F101,"if exist 2","",z);
  9866.             }
  9867. #endif /* OS2 */
  9868. #ifdef CK_TMPDIR
  9869.         } else {
  9870. #ifdef VMS
  9871.             z = (zchki(s) == -2)
  9872. #else
  9873. /* Because this doesn't catch $DISK1:[FOO]BLAH.DIR;1 */
  9874.             z = isdir(s)
  9875. #ifdef OS2
  9876.               || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  9877. #endif /* OS2 */
  9878. #endif /* VMS */
  9879.               ;
  9880.             debug(F101,"if directory 1","",z);
  9881.  
  9882.             if (!z) {                   /* File not found. */
  9883.                 int t;                  /* Try expanding variables */
  9884.                 t = LINBUFSIZ-1;        /* and looking again. */
  9885.                 lp = line;
  9886.                 zzstring(s,&lp,&t);
  9887.                 s = line;
  9888.                 z = isdir(s)
  9889. #ifdef OS2
  9890.                   || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  9891. #endif /* OS2 */
  9892.                     ;
  9893.                 debug(F101,"if directory 2","",z);
  9894.             }
  9895. #endif /* CK_TMPDIR */
  9896.         }
  9897.         ifargs += 2;
  9898.         break;
  9899.  
  9900.       case XXIFEQ:                      /* IF EQUAL (string comparison) */
  9901.       case XXIFLL:                      /* IF Lexically Less Than */
  9902.       case XXIFLG:                      /* If Lexically Greater Than */
  9903.         if ((x = cmfld("first word or variable name","",&s,xxstring)) < 0) {
  9904.             if (x == -3) {
  9905.                 printf("?Text required\n");
  9906.                 return(-9);
  9907.             } else return(x);
  9908.         }
  9909.         s = brstrip(s);                 /* Strip braces */
  9910.         x = (int)strlen(s);
  9911.         if (x > LINBUFSIZ-1) {
  9912.             printf("?IF: strings too long\n");
  9913.             return(-2);
  9914.         }
  9915.         lp = line;                      /* lp points to first string */
  9916.         ckstrncpy(line,s,LINBUFSIZ);
  9917.         if ((y = cmfld("second word or variable name","",&s,xxstring)) < 0) {
  9918.             if (y == -3) {
  9919.                 printf("?Text required\n");
  9920.                 return(-9);
  9921.             } else return(y);
  9922.         }
  9923.         s = brstrip(s);
  9924.         y = (int)strlen(s);
  9925.         if (x + y + 2 > LINBUFSIZ) {
  9926.             printf("?IF: strings too long\n");
  9927.             return(-2);
  9928.         }
  9929.         tp = lp + x + 2;                /* tp points to second string */
  9930.         strcpy(tp,s);                   /* safe (checked) */
  9931.         x = ckstrcmp(lp,tp,-1,inpcas[cmdlvl]); /* Use longest length */
  9932.         switch (ifc) {
  9933.           case XXIFEQ:                  /* IF EQUAL (string comparison) */
  9934.             z = (x == 0);
  9935.             break;
  9936.           case XXIFLL:                  /* IF Lexically Less Than */
  9937.             z = (x < 0);
  9938.             break;
  9939.           case XXIFLG:                  /* If Lexically Greater Than */
  9940.             z = (x > 0);
  9941.             break;
  9942.         }
  9943.         debug(F101,"IF EQ result","",z);
  9944.         ifargs += 3;
  9945.         break;
  9946.  
  9947.       case XXIFVE:                      /* IF VERSION */
  9948.       case XXIFAE:                      /* IF (arithmetically) = */
  9949.       case XXIFNQ:                      /* IF != (not arithmetically equal) */
  9950.       case XXIFLT:                      /* IF <  */
  9951.       case XXIFLE:                      /* IF <= */
  9952.       case XXIFGE:                      /* IF >= */
  9953.       case XXIFGT: {                    /* IF >  */
  9954.  
  9955.         /* Really should use longs here... */
  9956.         /* But cmnum parses ints. */
  9957.         int xx, n1 = 0, n2 = 0;
  9958.         if (ifc == XXIFVE) {
  9959.             n1 = (int) vernum;
  9960.         } else {
  9961.             x = cmfld("first number or variable name","",&s,xxstring);
  9962.             if (x == -3) {
  9963.                 printf("?Quantity required\n");
  9964.                 return(-9);
  9965.             }
  9966.             if (x < 0) return(x);
  9967.             debug(F101,"xxifgt cmfld","",x);
  9968.             ckstrncpy(line,s,LINBUFSIZ);
  9969.             lp = brstrip(line);
  9970.             debug(F110,"xxifgt exp1",lp,0);
  9971.  
  9972. /* The following bit is for compatibility with old versions of MS-DOS Kermit */
  9973.  
  9974.             if (!ckstrcmp(lp,"count",5,0)) {
  9975.                 n1 = count[cmdlvl];
  9976.             } else if (!ckstrcmp(lp,"version",7,0)) {
  9977.                 n1 = (int) vernum;
  9978.             } else if (!ckstrcmp(lp,"argc",4,0)) {
  9979.                 n1 = (int) macargc[maclvl];
  9980.             } else {
  9981.  
  9982. /* End of compatibility bit */
  9983.  
  9984. #ifdef FNFLOAT
  9985.                 if (isfloat(lp,0) > 1) { /* Allow floating-point comparisons */
  9986.                     f1 = floatval;
  9987.                     f1flag = 1;
  9988.                 } else
  9989. #endif /* FNFLOAT */
  9990.                   if (chknum(lp)) {
  9991.                       n1 = atoi(lp);
  9992.                   } else {              /* Check for arithmetic expression */
  9993.                       q = evala(lp);    /* cmnum() does this but ... */
  9994.                       if (chknum(q))    /* we're not using cmnum(). */
  9995.                         n1 = atoi(q);
  9996.                       else
  9997.                         return(-2);
  9998.                   }
  9999.             }
  10000.         }
  10001.         y = cmfld("number or variable name","",&s,xxstring);
  10002.         if (y == -3) {
  10003.             printf("?Quantity required\n");
  10004.             return(-9);
  10005.         }
  10006.         if (y < 0) return(y);
  10007.         s = brstrip(s);
  10008.         if (!*s) return(-2);
  10009.         if (ifc == XXIFVE) {
  10010.             tp = line;
  10011.         } else {
  10012.             x = (int)strlen(lp);
  10013.             tp = line + x + 2;
  10014.         }
  10015.         ckstrncpy(tp,s,LINBUFSIZ-x-2);
  10016.         debug(F110,"xxifgt exp2",tp,0);
  10017.         if (!ckstrcmp(tp,"count",5,0)) {
  10018.             n2 = count[cmdlvl];
  10019.         } else if (!ckstrcmp(tp,"version",7,0)) {
  10020.             n2 = (int) vernum;
  10021.         } else if (!ckstrcmp(tp,"argc",4,0)) {
  10022.             n2 = (int) macargc[maclvl];
  10023.         } else {
  10024. #ifdef FNFLOAT
  10025.             if (isfloat(tp,0) > 1) {
  10026.                 f2 = floatval;
  10027.                 f2flag = 1;
  10028.             } else
  10029. #endif /* FNFLOAT */
  10030.             if (chknum(tp)) {
  10031.                 n2 = atoi(tp);
  10032.             } else {
  10033.                 q = evala(tp);
  10034.                 if (chknum(q))
  10035.                   n2 = atoi(q);
  10036.                 else
  10037.                   return(-2);
  10038.             }
  10039.         }
  10040.         xx = (ifc == XXIFVE) ? XXIFGE : ifc;
  10041.  
  10042. #ifdef FNFLOAT
  10043.         if (f1flag && !f2flag) {
  10044.             f2 = (CKFLOAT)n2;
  10045.             f2flag = 1;
  10046.         }
  10047.         if (f2flag && !f1flag)
  10048.           f1 = (CKFLOAT)n1;
  10049.         if (f1flag)
  10050.           z = ((f1 <  f2 && xx == XXIFLT)
  10051.                || (f1 != f2 && xx == XXIFNQ)
  10052.                || (f1 <= f2 && xx == XXIFLE)
  10053.                || (f1 == f2 && xx == XXIFAE)
  10054.                || (f1 >= f2 && xx == XXIFGE)
  10055.                || (f1 >  f2 && xx == XXIFGT));
  10056.         else
  10057. #endif /* FNFLOAT */
  10058.           z = ((n1 <  n2 && xx == XXIFLT)
  10059.                || (n1 != n2 && xx == XXIFNQ)
  10060.                || (n1 <= n2 && xx == XXIFLE)
  10061.                || (n1 == n2 && xx == XXIFAE)
  10062.                || (n1 >= n2 && xx == XXIFGE)
  10063.                || (n1 >  n2 && xx == XXIFGT));
  10064.         debug(F101,"xxifge z","",z);
  10065.         if (ifc == XXIFVE)
  10066.           ifargs += 2;
  10067.         else
  10068.           ifargs += 3;
  10069.         break;
  10070.       }
  10071.  
  10072.       case XXIFNU:                      /* IF NUMERIC */
  10073.         x = cmfld("variable name or constant","",&s,NULL);
  10074.         if (x == -3) {
  10075.             extern int cmflgs;
  10076.             if (cmflgs == 1) {
  10077.                 printf("?Quantity required\n");
  10078.                 return(-9);
  10079.             }
  10080.         } else if (x < 0)
  10081.           return(x);
  10082.         x = LINBUFSIZ-1;
  10083.         lp = line;
  10084.         zzstring(s,&lp,&x);
  10085.         lp = line;
  10086.         debug(F110,"xxifnu quantity",lp,0);
  10087.         z = chknum(lp);
  10088. #ifdef COMMENT
  10089. /*
  10090.   This works, but it's not wise -- IF NUMERIC is mostly used to see if a
  10091.   string really does contain only numeric characters.  If they want to force
  10092.   evaluation, they can use \feval() on the argument string.
  10093. */
  10094.         if (!z) {                       /* Not a number */
  10095.             x_ifnum = 1;                /* Avoid "eval" error messages */
  10096.             q = evala(lp);              /* Maybe it's an expression */
  10097.             z = chknum(q);              /* that evaluates to a number */
  10098.             x_ifnum = 0;                /* Put eval messages back to normal */
  10099.             if (z) debug(F110,"xxifnu exp",lp,0);
  10100.         }
  10101. #endif /* COMMENT */
  10102.         debug(F101,"xxifnu chknum","",z);
  10103.         ifargs += 2;
  10104.         break;
  10105.  
  10106. #ifdef ZFCDAT
  10107.       case XXIFNE: {                    /* IF NEWER */
  10108.         char d1[20], * d2;              /* Buffers for 2 dates */
  10109.         if ((z = cmifi("First file","",&s,&y,xxstring)) < 0)
  10110.           return(z);
  10111.         ckstrncpy(d1,zfcdat(s),20);
  10112.         if ((z = cmifi("Second file","",&s,&y,xxstring)) < 0)
  10113.           return(z);
  10114.         d2 = zfcdat(s);
  10115.         if ((int)strlen(d1) != 17 || (int)strlen(d2) != 17) {
  10116.             printf("?Failure to get file date\n");
  10117.             return(-9);
  10118.         }
  10119.         debug(F110,"xxifnewer d1",d1,0);
  10120.         debug(F110,"xxifnewer d2",d2,0);
  10121.         z = (strcmp(d1,d2) > 0) ? 1 : 0;
  10122.         debug(F101,"xxifnewer","",z);
  10123.         ifargs += 2;
  10124.         break;
  10125.       }
  10126. #endif /* ZFCDAT */
  10127.  
  10128. #ifdef CK_IFRO
  10129.       case XXIFRO:                      /* REMOTE-ONLY advisory */
  10130.         ifargs++;
  10131. #ifdef NOLOCAL
  10132.         z = 1;
  10133. #else
  10134.         z = remonly;
  10135. #endif /* NOLOCAL */
  10136.         break;
  10137. #endif /* CK_IFRO */
  10138.  
  10139.       case XXIFAL:                      /* ALARM */
  10140.         ifargs++;
  10141.         debug(F101,"IF ALARM ck_alarm","",ck_alarm);
  10142.         debug(F110,"IF ALARM alrm_date",alrm_date,0);
  10143.         debug(F110,"IF ALARM alrm_time",alrm_time,0);
  10144.  
  10145.         if (ck_alarm < 1L || alrm_date[0] < '0' || alrm_time[0] < '0') {
  10146.             z = 0;                      /* ALARM not SET */
  10147.             break;                      /* so IF ALARM fails */
  10148.         }
  10149.         /* Get current date and time */
  10150.         ckstrncpy(tmpbuf,ckcvtdate("",1),TMPBUFSIZ);
  10151.         s = tmpbuf;
  10152.         s[8] = NUL;
  10153.         z = (int) strncmp(tmpbuf,alrm_date,8); /* Compare dates */
  10154.         debug(F101,"IF ALARM date z","",z);
  10155.         if (z == 0) {                   /* Dates are the same */
  10156.             /* Compare times */
  10157.             z = (tod2sec(tmpbuf+9) >= atol(alrm_time)) ? 1 : -1;
  10158.             debug(F101,"IF ALARM time z","",z);
  10159.         }
  10160.         tmpbuf[0] = NUL;                /* z >= 0 if alarm is passed */
  10161.         z = ((z >= 0) ? 1 : 0);         /* z <  0 otherwise */
  10162.         debug(F101,"IF ALARM final z","",z);
  10163.         break;
  10164.  
  10165.       case XXIFOP:                      /* IF OPEN */
  10166.         if ((x = cmkey(iotab,niot,"file or log","",xxstring)) < 0)
  10167.           return(x);
  10168.         if (x == 9999 || x == ZSTDIO) {
  10169.             bgchk();                    /* Check background status */
  10170.             z = pflag ? 1 : 0;
  10171.         } else if (x == 8888) {
  10172.             z = local ? ttchk() > -1 : 0;
  10173. #ifdef CKLOGDIAL
  10174.         } else if (x == 7777) {
  10175.             extern int dialog;
  10176.             z = dialog ? 1 : 0;
  10177. #endif /* CKLOGDIAL */
  10178.         } else
  10179.           z = (chkfn(x) > 0) ? 1 : 0;
  10180.         ifargs += 1;
  10181.         break;
  10182.  
  10183. #ifdef OS2
  10184.       case XXIFSD:                      /* Started-From-Dialer */
  10185.         ifargs++;
  10186.         z = StartedFromDialer;
  10187.         break;
  10188.  
  10189.       case XXIFTM:                      /* Terminal-Macro */
  10190.         ifargs++;
  10191.         z = cmdstk[cmdlvl].ccflgs & CF_KMAC;
  10192.         break;
  10193. #endif /* OS2 */
  10194.  
  10195.       case XXIFEM:                      /* Emulation is active */
  10196. #ifdef OS2
  10197.         z = 1;
  10198. #else
  10199.         z = 0;
  10200. #endif /* OS2 */
  10201.         break;
  10202.  
  10203.       case XXIFIK:                      /* Running as IKSD? */
  10204.         z = inserver;
  10205.         break;
  10206.  
  10207.       case XXIFTA:                      /* Connection is TAPI */
  10208.         z = 0;
  10209. #ifndef NODIAL
  10210. #ifdef CK_TAPI
  10211.         if (local && !network && tttapi)
  10212.           z = 1;
  10213. #endif /* CK_TAPI */
  10214. #endif /* NODIAL */
  10215.         break;
  10216.  
  10217.       case XXIFMA:                      /* IF MATCH */
  10218.         x = cmfld("String or variable","",&s,xxstring);
  10219.         if (x == -3) {
  10220.             extern int cmflgs;
  10221.             if (cmflgs == 1) {
  10222.                 printf("?String required\n");
  10223.                 return(-9);
  10224.             }
  10225.         } else if (x < 0)
  10226.           return(x);
  10227.         ckstrncpy(line,s,LINBUFSIZ);
  10228.         s = brstrip(line);
  10229.         debug(F110,"xxifma string",line,0);
  10230.         x = cmfld("Pattern","",&p,xxstring);
  10231.         if (x == -3) {
  10232.             extern int cmflgs;
  10233.             if (cmflgs == 1) {
  10234.                 printf("?Pattern required\n");
  10235.                 return(-9);
  10236.             }
  10237.         } else if (x < 0)
  10238.           return(x);
  10239.         ckstrncpy(tmpbuf,p,TMPBUFSIZ);
  10240.         p = brstrip(tmpbuf);
  10241.         debug(F110,"xxifma pattern",tmpbuf,0);
  10242.         z = ckmatch(p,s,inpcas[cmdlvl],1);
  10243.         break;
  10244.  
  10245.       case XXIFFL: {                    /* IF FLAG */
  10246.           extern int ooflag;
  10247.           z = ooflag;
  10248.           break;
  10249.       }
  10250.       case XXIFAV: {                    /* IF AVAILABLE */
  10251.           if ((x = cmkey(availtab,availtabn,"","",xxstring)) < 0)
  10252.             return(x);
  10253.           switch (x) {
  10254.             case AV_KRB4:
  10255.               z = ck_krb4_is_installed();
  10256.               break;
  10257.             case AV_KRB5:
  10258.               z = ck_krb5_is_installed();
  10259.               break;
  10260.             case AV_SRP:
  10261.               z = ck_srp_is_installed();
  10262.               break;
  10263.             case AV_SSL:
  10264.               z = ck_ssleay_is_installed();
  10265.               break;
  10266.             case AV_NTLM:
  10267.               z = ck_ntlm_is_installed();
  10268.               break;
  10269.             case AV_CRYPTO:
  10270.               z = ck_crypt_is_installed();
  10271.               break;
  10272.             case AV_SSH:
  10273.               z = ck_ssh_is_installed();
  10274.               break;
  10275.             default:
  10276.               z = 0;
  10277.           }
  10278.           break;
  10279.       }
  10280.       case XXIFAT:                      /* IF ASKTIMEOUT */
  10281.         z = asktimedout;
  10282.         break;
  10283.  
  10284.       case XXIFRD:                      /* IF READABLE */
  10285.       case XXIFWR:                      /* IF WRITEABLE */
  10286.         if ((x = cmfld("File or directory name",
  10287.                        "",
  10288.                        &s,
  10289. #ifdef OS2
  10290.                        NULL             /* This allows \'s in filenames */
  10291. #else
  10292.                        xxstring
  10293. #endif /* OS2 */
  10294.                        )) < 0) {
  10295.             if (x == -3) {
  10296.                 extern int cmflgs;
  10297.                 if (cmflgs == 1) {
  10298.                     printf("?File or directory name required\n");
  10299.                     return(-9);
  10300.                 }
  10301.             } else return(x);
  10302.         }
  10303.         s = brstrip(s);
  10304. /*
  10305.   zchk[io]() do not do what we want here for directories, so we set
  10306.   a global flag telling it to behave specially in this case.  Othewise
  10307.   we'd have to change the API and change all ck?fio.c modules accordingly.
  10308. */
  10309.         y = 0;                          /* Try-again control */
  10310. #ifdef OS2
  10311.   ifrdagain:
  10312. #endif /* OS2 */
  10313.         if (ifc == XXIFRD) {            /* IF READABLE */
  10314.             zchkid = 1;
  10315.             z = zchki(s) > -1;
  10316.             zchkid = 0;
  10317.         } else if (ifc == XXIFWR) {     /* IF WRITEABLE */
  10318.             zchkod = 1;
  10319.             z = zchko(s) > -1;
  10320.             zchkod = 0;
  10321.         }
  10322. #ifdef OS2
  10323.         if (!z && !y) {                 /* File not found. */
  10324.             int t;                      /* Try expanding variables */
  10325.             t = LINBUFSIZ-1;            /* and looking again. */
  10326.             lp = line;
  10327.             zzstring(s,&lp,&t);
  10328.             s = line;
  10329.             z = zchko(s) > -1;
  10330.             y++;
  10331.             goto ifrdagain;
  10332.         }
  10333. #endif /* OS2 */
  10334.         ifargs += 2;
  10335.         break;
  10336.       case XXIFQU:                      /* IF QUIET */
  10337.         z = quiet ? 1 : 0;
  10338.         debug(F101,"if quiet","",z);
  10339.         ifargs += 1;
  10340.         break;
  10341.  
  10342.       case XXIFWI:                      /* WILD */
  10343.         if ((x = cmfld("File specification","",&s,xxstring)) < 0) return(x);
  10344.         z = iswild(s);
  10345.         break;
  10346.  
  10347.       case XXIFCK:                      /* C-KERMIT */
  10348. #ifdef OS2
  10349.         z = 0;
  10350. #else
  10351.         z = 1;
  10352. #endif /* OS2 */
  10353.         break;
  10354.  
  10355.       case XXIFK9:                      /* K-95 */
  10356. #ifdef OS2
  10357.         z = 1;
  10358. #else
  10359.         z = 0;
  10360. #endif /* OS2 */
  10361.         break;
  10362.  
  10363.       case XXIFGU:                      /* GUI */
  10364. #ifdef KUI
  10365.         z = 1;
  10366. #else
  10367.         z = 0;
  10368. #endif /* KUI */
  10369.         break;
  10370.  
  10371.       case XXIFMS:                      /* MS-KERMIT */
  10372.         z = 0;
  10373.         break;
  10374.  
  10375.       case XXIFLO:                      /* IF LOCAL */
  10376.         z = local ? 1 : 0;
  10377.         break;
  10378.  
  10379.       case XXIFCM: {                    /* IF COMMAND */
  10380.           extern struct keytab cmdtab[];
  10381.           extern int ncmd;
  10382.           if ((x = cmfld("Word","",&s,xxstring)) < 0)
  10383.             return(x);
  10384.           z = lookup(cmdtab,s,ncmd,&y);
  10385.           z = (z == -2 || z > -1) ? 1 : 0;
  10386.           break;
  10387.       }
  10388. #ifdef CKFLOAT
  10389.       case XXIFFP:                      /* IF FLOAT */
  10390.         if ((x = cmfld("Number","",&s,xxstring)) < 0)
  10391.           if (x != -3)                  /* e.g. empty variable */
  10392.             return(x);
  10393.         z = isfloat(s,0);
  10394.         break;
  10395. #endif /* CKFLOAT */
  10396.  
  10397.       case XXIFKB:                      /* KBHIT */
  10398.         z = conchk();
  10399.         if (z < 0) z = 0;
  10400.         if (z > 1) z = 1;
  10401.         break;
  10402.  
  10403.       case XXIFKG: {                    /* KERBANG */
  10404.           extern int cfilef;
  10405.           z = (xcmdsrc == 0) ? 0 : cfilef;
  10406.           break;
  10407.       }
  10408.  
  10409.       default:                          /* Shouldn't happen */
  10410.         return(-2);
  10411.     } /* end of switch */
  10412.  
  10413.     if (z)
  10414.       *bx++ = '1';
  10415.     else
  10416.       *bx++ = '0';
  10417.     *bx = NUL;
  10418.     if (bx > boolval + BOOLLEN - 2) {
  10419.         printf("?Boolean expression too long");
  10420.         return(-9);
  10421.     }
  10422.     ecount++;                           /* Expression counter */
  10423.     debug(F101,"boolexp parens","",parens);
  10424.     debug(F101,"boolexp pcount","",pcount);
  10425.     if (parens && pcount > 0)
  10426.       goto ifagain;
  10427.  
  10428.   ifend:                                /* No more - done */
  10429.     *bx = NUL;
  10430.     z = atoi(evalx(boolval));
  10431.     debug(F111,"boolexp boolval",boolval,z);
  10432.     return(z);
  10433. }
  10434.  
  10435. /*  D O I F  --  Do the IF command  */
  10436.  
  10437. int
  10438. doif(cx) int cx; {
  10439.     int x, y, z; char *s, *p;
  10440.     char *q;
  10441. #ifdef OS2
  10442.     extern int keymac;
  10443. #endif /* OS2 */
  10444.  
  10445.     debug(F101,"doif cx","",cx);
  10446.  
  10447.     z = boolexp(cx);                    /* Evaluate the condition(s) */
  10448.     debug(F010,"doif cmdbuf",cmdbuf,0);
  10449.     debug(F101,"doif boolexp","",z);
  10450.     if (z < 0)
  10451.       return(z);
  10452.  
  10453.     if (cx == XXIF) {                   /* Allow IF to have XIF semantics. */
  10454.         char * p;
  10455.         p = cmpeek();
  10456.         if (!p) p = "";
  10457.         while (*p) {
  10458.             if (*p == SP || *p == HT)
  10459.               p++;
  10460.             else
  10461.               break;
  10462.         }
  10463.         if (*p == '{')
  10464.           cx = XXIFX;
  10465.     }
  10466.     switch (cx) {                       /* Separate handling for IF and XIF */
  10467.  
  10468.       case XXASSER:                     /* And ASSERT */
  10469.         if ((x = cmcfm()) < 0)
  10470.           return(x);
  10471.         return(success = z);
  10472.  
  10473.       case XXIF:                        /* This is IF... */
  10474.         ifcmd[cmdlvl] = 1;              /* We just completed an IF command */
  10475.         debug(F101,"doif condition","",z);
  10476.         if (z) {                        /* Condition is true */
  10477.             iftest[cmdlvl] = 1;         /* Remember that IF succeeded */
  10478.             if (maclvl > -1) {          /* In macro, */
  10479.                 pushcmd(NULL);          /* save rest of command. */
  10480.             } else if (tlevel > -1) {   /* In take file, */
  10481.                 debug(F100, "doif: pushing command", "", 0);
  10482.                 pushcmd(NULL);          /* save rest of command. */
  10483.             } else {                    /* If interactive, */
  10484.                 cmini(ckxech);          /* just start a new command */
  10485.                 printf("\n");           /* (like in MS-DOS Kermit) */
  10486.                 if (pflag) prompt(xxstring);
  10487.             }
  10488.         } else {                        /* Condition is false */
  10489.             iftest[cmdlvl] = 0;         /* Remember command failed. */
  10490.             if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  10491.               return(y);                /* Gobble up rest of line */
  10492.         }
  10493.         return(0);
  10494.  
  10495.       case XXIFX: {                     /* This is XIF (Extended IF) */
  10496.           char *p;
  10497.           char e[5];
  10498.           int i;
  10499.           if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  10500.             return(y);                  /* Get object command. */
  10501.           p = s;
  10502.           lp = line;
  10503.           debug(F110,"doif THEN part",s,-54);
  10504.           if (litcmd(&p,&lp,LINBUFSIZ - 1) < 0) { /* Quote THEN-part */
  10505.               return(-2);
  10506.           }
  10507.           debug(F111,"doif THEN part 2",line,z);
  10508.  
  10509.           while (*p == SP) p++;         /* Strip trailing spaces */
  10510.           ifcmd[cmdlvl] = 0;            /* Assume ELSE part in same line */
  10511.           iftest[cmdlvl] = z ? 1 : 0;
  10512.           if (*p) {                     /* At end? */
  10513.               if (!z) {                 /* No, use ELSE-part, if any */
  10514.                   for (i = 0; i < 4; i++) e[i] = *p++;
  10515.                   if (ckstrcmp(e,"else",4,0)) /* See if we have an ELSE */
  10516.                     return(-2);         /* Something else - error. */
  10517.                   debug(F010,"doif ELSE line 1",p,0);
  10518.                   while (*p == SP) p++; /* Skip spaces */
  10519.                   if (*p != '{') {      /* Brace ELSE part if necessary */
  10520.                       ckmakmsg(tmpbuf,TMPBUFSIZ,"{",p," }",NULL);
  10521.                       p = tmpbuf;
  10522.                       debug(F010,"doif ELSE line 2",p,0);
  10523.                   }
  10524.                   lp = line;            /* Write over THEN part... */
  10525.                   *lp = NUL;            /* with ELSE part. */
  10526.                   if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) {
  10527.                       return(-2);
  10528.                   }
  10529.                   while (*p == SP) p++; /* Strip trailing spaces */
  10530.                   if (*p) return(-2);   /* Should be nothing here. */
  10531.                   debug(F010,"doif ELSE line 3",line,0);
  10532.               }
  10533.           } else {                      /* At end, treat like an IF command */
  10534.               if (!z) line[0] = NUL;    /* Condition not true and no ELSE */
  10535.               ifcmd[cmdlvl] = 1;        /* Allow ELSE on next line */
  10536.               debug(F101,"IF condition","",z);
  10537.           }
  10538.           if (line[0]) {
  10539.               x = mlook(mactab,"_xif",nmac); /* Get index of "_xif" macro. */
  10540.               if (x < 0) {                      /* Not there? */
  10541.                   addmmac("_xif",xif_def);      /* Put it back. */
  10542.                   if (mlook(mactab,"_xif",nmac) < 0) { /* Look it up again. */
  10543.                       printf("?XIF macro gone!\n");
  10544.                       return(success = 0);
  10545.                   }
  10546.               }
  10547.               dodo(x,line,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  10548.           }
  10549.           return(0);
  10550.       }
  10551.       case XXWHI: {                     /* WHILE Command */
  10552.           p = cmdbuf;                   /* Capture IF condition */
  10553.           ifcond[0] = NUL;              /* from command buffer */
  10554.           while (*p == SP) p++;
  10555.           while (*p != SP) p++;
  10556.           ifcp = ifcond;
  10557.           ifcp += ckstrncpy(ifcp,"{ \\flit(if ( not ",IFCONDLEN);
  10558. #ifdef COMMENT
  10559. /*
  10560.   This doesn't work because it breaks on the first left brace, which does
  10561.   not necessarily start the command list, e.g. "while equal \%a {\35}".
  10562. */
  10563.           while (*p != '{' && *p != NUL) *ifcp++ = *p++;
  10564.           p = " ) goto _..bot) } ";
  10565.           while (*ifcp++ = *p++) ;
  10566. #else
  10567. /*
  10568.   The command parser sets cmbptr to the spot where it left off parsing in
  10569.   the command buffer.
  10570. */
  10571.           {
  10572.               extern char * cmbptr;
  10573.               if (cmbptr) {
  10574.                   while (p < cmbptr && *p != NUL)
  10575.                     *ifcp++ = *p++;
  10576.                   p = " ) goto _..bot) } ";
  10577.                   while ((*ifcp++ = *p++)) ;
  10578.               } else {
  10579.                   printf("?Internal error parsing WHILE condition\n");
  10580.                   return(-9);
  10581.               }
  10582.           }
  10583. #endif /* COMMENT */
  10584.  
  10585.           debug(F110,"WHILE cmd",ifcond,0);
  10586.  
  10587.           if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  10588.             return(y);                  /* Get object command. */
  10589.           p = s;
  10590.           lp = line;
  10591.           if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) { /* Quote object command */
  10592.               return(-2);
  10593.           }
  10594.           debug(F101,"WHILE body",line,-54);
  10595.           if (line[0]) {
  10596.               char *p;
  10597.               x = mlook(mactab,"_while",nmac); /* index of "_while" macro. */
  10598.               if (x < 0) {              /* Not there? */
  10599.                   addmmac("_while",whil_def); /* Put it back. */
  10600.                   if (mlook(mactab,"_while",nmac) < 0) { /* Look it up again */
  10601.                       printf("?WHILE macro definition gone!\n");
  10602.                       return(success = 0);
  10603.                   }
  10604.               }
  10605.               p = malloc((int)strlen(ifcond) + (int)strlen(line) + 2);
  10606.               if (p) {
  10607.                   strcpy(p,ifcond);     /* safe (prechecked) */
  10608.                   strcat(p,line);       /* safe (prechecked) */
  10609.                   debug(F010,"WHILE dodo",p,0);
  10610.                   dodo(x,p,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  10611.                   free(p);
  10612.                   p = NULL;
  10613.               } else {
  10614.                   printf("?Can't allocate storage for WHILE command");
  10615.                   return(success = 0);
  10616.               }
  10617.           }
  10618.           return(0);
  10619.       }
  10620.       default:
  10621.         return(-2);
  10622.     }
  10623. }
  10624. #endif /* NOSPL */
  10625.  
  10626. /* Set up a TAKE command file */
  10627.  
  10628. int
  10629. dotake(s) char *s; {
  10630. #ifndef NOSPL
  10631.     extern int tra_cmd;
  10632. #endif /* NOSPL */
  10633. #ifndef NOLOCAL
  10634. #ifdef OS2
  10635.     extern int term_io;
  10636.     int term_io_sav = term_io;
  10637. #endif /* OS2 */
  10638. #endif /* NOLOCAL */
  10639.     int slen;
  10640.  
  10641.     debug(F110,"dotake",s,0);
  10642.     if (!s) s = "";
  10643.     if (!*s) return(success = 0);
  10644.     slen = strlen(s);
  10645.     debug(F101,"dotake len","",slen);
  10646.  
  10647.     if ((tfile[++tlevel] = fopen(s,"r")) == NULL) {
  10648.         perror(s);
  10649.         debug(F110,"dotake fail",s,0);
  10650.         tlevel--;
  10651.         return(success = 0);
  10652.     } else {
  10653.         tfline[tlevel] = 0;             /* Line counter */
  10654. #ifdef VMS
  10655.         conres();                       /* So Ctrl-C will work */
  10656. #endif /* VMS */
  10657. #ifndef NOLOCAL
  10658. #ifdef OS2
  10659.         term_io = 0;                    /* Disable Terminal Emulator I/O */
  10660. #endif /* OS2 */
  10661. #endif /* NOLOCAL */
  10662. #ifndef NOSPL
  10663.         cmdlvl++;                       /* Entering a new command level */
  10664.         debug(F111,"CMD +F",s,cmdlvl);
  10665.         debug(F101,"dotake cmdlvl","",cmdlvl);
  10666.         debug(F101,"dotake tlevel","",tlevel);
  10667.         if (cmdlvl > CMDSTKL) {
  10668.             debug(F100,"dotake stack overflow","",0);
  10669.             cmdlvl--;
  10670.             debug(F111,"CMD*-F",s,cmdlvl);
  10671.             fclose(tfile[tlevel--]);
  10672.             printf("?TAKE files and/or DO commands nested too deeply\n");
  10673.             return(success = 0);
  10674.         }
  10675.         if (tfnam[tlevel]) {            /* Copy the filename */
  10676.             free(tfnam[tlevel]);
  10677.             tfnam[tlevel] = NULL;
  10678.         }
  10679.         if ((tfnam[tlevel] = malloc(strlen(s) + 1))) {
  10680.             strcpy(tfnam[tlevel],s);    /* safe */
  10681.         } else {
  10682.             printf("?Memory allocation failure\n");
  10683.             return(success = 0);
  10684.         }
  10685.         ifcmd[cmdlvl] = 0;              /* Set variables for this cmd file */
  10686.         iftest[cmdlvl] = 0;
  10687.         count[cmdlvl]  = count[cmdlvl-1];  /* Inherit this */
  10688.         intime[cmdlvl] = intime[cmdlvl-1]; /* Inherit this */
  10689.         inpcas[cmdlvl] = inpcas[cmdlvl-1]; /* Inherit this */
  10690.         takerr[cmdlvl] = takerr[cmdlvl-1]; /* Inherit this */
  10691.         merror[cmdlvl] = merror[cmdlvl-1]; /* Inherit this */
  10692.         xquiet[cmdlvl] = quiet;
  10693.         xcmdsrc = CMD_TF;
  10694.         cmdstk[cmdlvl].src = CMD_TF;    /* Say we're in a TAKE file */
  10695.         cmdstk[cmdlvl].lvl = tlevel;    /* nested at this level */
  10696.         cmdstk[cmdlvl].ccflgs = cmdstk[cmdlvl-1].ccflgs;
  10697. #else
  10698.         takerr[tlevel] = takerr[tlevel-1]; /* Inherit this */
  10699. #endif /* NOSPL */
  10700.     }
  10701. #ifndef NOSPL
  10702.     if (tra_cmd)
  10703.       printf("[%d] +F: \"%s\"\n",cmdlvl,s);
  10704. #endif /* NOSPL */
  10705. #ifndef NOLOCAL
  10706. #ifdef OS2
  10707.     term_io = term_io_sav;
  10708. #endif /* OS2 */
  10709. #endif /* NOLOCAL */
  10710.     return(1);
  10711. }
  10712. #endif /* NOICP */
  10713.