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