home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp4 / ckuus6.c < prev    next >
C/C++ Source or Header  |  2009-10-16  |  412KB  |  12,039 lines

  1. #include "ckcsym.h"
  2. #ifndef NOICP
  3.  
  4. /*
  5.   Authors:
  6.     Frank da Cruz <fdc@columbia.edu>,
  7.       The Kermit Project, Columbia University, New York City
  8.     Jeffrey E Altman <jaltman@secure-endpoints.com>
  9.       Secure Endpoints Inc., New York City
  10.  
  11.   Copyright (C) 1985, 2009,
  12.     Trustees of Columbia University in the City of New York.
  13.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  14.     copyright text in the ckcmai.c module for disclaimer and permissions.
  15. */
  16.  
  17. /* Includes */
  18.  
  19. #include "ckcdeb.h"
  20. #include "ckcasc.h"
  21. #include "ckcker.h"
  22. #include "ckuusr.h"
  23. #include "ckcxla.h"
  24. #include "ckcnet.h"                     /* Network symbols */
  25. #include <signal.h>
  26.  
  27. #ifndef NOSTAT
  28. #include <sys/stat.h>
  29. #endif /* NOSTAT */
  30.  
  31. #ifdef VMS
  32. #ifndef TCPSOCKET
  33. #include <errno.h>
  34. #endif /* TCPSOCKET */
  35. #endif /* VMS */
  36.  
  37. #ifdef datageneral
  38. #define fgets(stringbuf,max,fd) dg_fgets(stringbuf,max,fd)
  39. #endif /* datageneral */
  40.  
  41. #ifdef QNX6
  42. #define readblock kreadblock
  43. #endif /* QNX6 */
  44.  
  45. /* External Kermit Variables, see ckmain.c for description. */
  46.  
  47. extern xx_strp xxstring;
  48.  
  49. extern int local, xitsta, binary, parity, escape, flow, cmd_rows, turn,
  50.   turnch, duplex, ckxech, seslog, dfloc, cnflg, tlevel, pflag, msgflg, mdmtyp,
  51.   zincnt, quiet, repars, techo, network, nzxopts, what, filepeek, recursive;
  52.  
  53. extern int xaskmore, tt_rows, tt_cols, cmd_cols, g_matchdot, diractive,
  54.   xcmdsrc, nscanfile, reliable, nolinks, cmflgs;
  55.  
  56. #ifdef VMSORUNIX
  57. extern int zgfs_dir, zgfs_link;
  58. #endif /* VMSORUNIX */
  59.  
  60. #ifdef CK_IFRO
  61.   extern int remonly;
  62. #endif /* CK_IFRO */
  63.  
  64. #ifdef OS2
  65. extern int StartedFromDialer ;
  66. extern int vmode;
  67. extern int k95stdout;
  68. #ifndef NT
  69. #define INCL_NOPM
  70. #define INCL_VIO                        /* Needed for ckocon.h */
  71. #include <os2.h>
  72. #undef COMMENT
  73. #else
  74. #define APIRET ULONG
  75. #include <windows.h>
  76. #include <tapi.h>
  77. #include "ckntap.h"
  78. #endif /* NT */
  79. #include "ckocon.h"
  80. #endif /* OS2 */
  81.  
  82. extern long vernum, speed;
  83. extern char *versio, *protv, *ckxv, *ckzv, *fnsv, *connv, *dftty, *cmdv;
  84. extern char *dialv, *loginv, *for_def[], *whil_def[], *xif_def[], *sw_def[];
  85. extern char *foz_def[];
  86. extern char *ckxsys, *ckzsys;
  87. #ifndef OS2
  88. extern char *DIRCMD;
  89. #ifndef UNIX
  90. extern char *DELCMD;
  91. #endif /* UNIX */
  92. #endif /* OS2 */
  93. extern char ttname[], filnam[];
  94. extern CHAR sstate, feol;
  95. extern char *zinptr;
  96.  
  97. #ifdef UNIX
  98. extern char ** mtchs;                   /* zxpand() file list */
  99. #endif /* UNIX */
  100.  
  101. #ifndef NOXFER
  102. extern int oopts, omode, oname, opath;  /* O-Packet options */
  103.  
  104. extern int stdinf, sndsrc, size, rpsiz, urpsiz, fncnv, fnrpath, displa,
  105.   stdouf, isguest, pktlog, nfils, keep, maxrps, fblksiz, frecl, frecfm,
  106.   atcapr, atdiso, spsizf, spsiz, spsizr, spmax, wslotr, prefixing,
  107.   fncact, fnspath, nprotos, g_proto, g_urpsiz, g_spsizf,
  108.   g_spsiz, g_spsizr, g_spmax, g_wslotr, g_prefixing, g_fncact, g_fncnv,
  109.   g_fnspath, g_fnrpath, xfrxla, g_xfrxla;
  110.  
  111. extern char *cmarg, *cmarg2;
  112.  
  113. #ifndef NOMSEND                         /* Multiple SEND */
  114. extern char *msfiles[];
  115. #endif /* NOMSEND */
  116. extern char fspec[];                    /* Most recent filespec */
  117. extern int fspeclen;
  118.  
  119. #ifdef CK_TMPDIR
  120. extern int f_tmpdir;                    /* Directory changed temporarily */
  121. extern char savdir[];                   /* For saving current directory */
  122. #endif /* CK_TMPDIR */
  123.  
  124. extern struct keytab protos[];          /* File transfer protocols */
  125. extern struct ck_p ptab[NPROTOS];
  126. #endif /* NOXFER */
  127.  
  128. #ifdef DCMDBUF                          /* Declarations from cmd package */
  129. extern char *cmdbuf, *atmbuf;           /* Command buffers */
  130. #else
  131. extern char cmdbuf[], atmbuf[];         /* Command buffers */
  132. #endif /* DCMDBUF */
  133.  
  134. extern int nopush;
  135.  
  136. #ifndef NOSPL
  137. int askflag = 0;                        /* ASK-class command active */
  138. int echostars = 0;            /* ASKQ should echo asterisks */
  139. extern char **a_ptr[];
  140. extern int a_dim[];
  141. extern char **m_xarg[];
  142. extern int n_xarg[];
  143. extern struct mtab *mactab;
  144. extern int nmac;
  145. extern long ck_alarm;
  146. extern char alrm_date[], alrm_time[];
  147. extern int x_ifnum;
  148. #endif /* NOSPL */
  149.  
  150. extern int inserver;                    /* I am IKSD */
  151. extern int backgrd;                     /* Kermit executing in background */
  152. extern char psave[];                    /* For saving & restoring prompt */
  153. extern char *tp;                        /* Temporary buffer */
  154.  
  155. int readblock = 4096;                   /* READ buffer size */
  156. CHAR * readbuf = NULL;                  /* Pointer to read buffer */
  157. int readsize = 0;                       /* Number of chars actually read */
  158. int getcmd = 0;                         /* GET-class command was given */
  159.  
  160. extern int zchkod, zchkid;
  161.  
  162. /*  C K U U S 6 --  "User Interface" for Unix Kermit (Part 6)  */
  163.  
  164. struct keytab deltab[] = {              /* DELETE Command Options */
  165.     { "/all",           DEL_ALL,  CM_INV },
  166.     { "/after",         DEL_AFT,  CM_ARG },
  167.     { "/ask",           DEL_ASK,  0 },
  168.     { "/before",        DEL_BEF,  CM_ARG },
  169.     { "/directories",   DEL_DIR,  0 },
  170.     { "/dotfiles",      DEL_DOT,  0 },
  171.     { "/except",        DEL_EXC,  CM_ARG },
  172.     { "/heading",       DEL_HDG,  0 },
  173.     { "/l",             DEL_LIS,  CM_INV|CM_ABR },
  174.     { "/larger-than",   DEL_LAR,  CM_ARG },
  175.     { "/list",          DEL_LIS,  0 },
  176.     { "/log",           DEL_LIS,  CM_INV },
  177.     { "/noask",         DEL_NAS,  0 },
  178.     { "/nodotfiles",    DEL_NOD,  0 },
  179.     { "/noheading",     DEL_NOH,  0 },
  180.     { "/nol",           DEL_NOL,  CM_INV|CM_ABR },
  181.     { "/nolist",        DEL_NOL,  0 },
  182.     { "/nolog",         DEL_NOL,  CM_INV },
  183.     { "/nopage",        DEL_NOP,  0 },
  184.     { "/not-after",     DEL_NAF,  CM_ARG },
  185.     { "/not-before",    DEL_NBF,  CM_ARG },
  186.     { "/not-since",     DEL_NAF,  CM_INV|CM_ARG },
  187.     { "/page",          DEL_PAG,  0 },
  188.     { "/quiet",         DEL_QUI,  CM_INV },
  189.     { "/recursive",     DEL_REC,  0 },
  190.     { "/simulate",      DEL_SIM,  0 },
  191.     { "/since",         DEL_AFT,  CM_ARG|CM_INV },
  192.     { "/smaller-than",  DEL_SMA,  CM_ARG },
  193.     { "/summary",       DEL_SUM,  0 },
  194.     { "/tree",          DEL_ALL,  0 },
  195.     { "/type",          DEL_TYP,  CM_ARG },
  196.     { "/verbose",       DEL_VRB,  CM_INV }
  197. };
  198. int ndeltab = sizeof(deltab)/sizeof(struct keytab);
  199.  
  200. /* /QUIET-/VERBOSE (/LIST-/NOLIST) (/LOG-/NOLOG) table */
  201.  
  202. struct keytab qvswtab[] = {
  203.     { "/l",           DEL_LIS,  CM_INV|CM_ABR },
  204.     { "/list",        DEL_LIS,  0 },
  205.     { "/log",         DEL_LIS,  CM_INV },
  206.     { "/nol",         DEL_NOL,  CM_INV|CM_ABR },
  207.     { "/nolist",      DEL_NOL,  0 },
  208.     { "/nolog",       DEL_NOL,  CM_INV },
  209.     { "/quiet",       DEL_QUI,  CM_INV },
  210.     { "/verbose",     DEL_VRB,  CM_INV }
  211. };
  212. int nqvswtab = sizeof(qvswtab)/sizeof(struct keytab);
  213.  
  214. static struct keytab renamsw[] = {
  215.     { "/collision",   REN_OVW,  CM_ARG },
  216. #ifndef NOUNICODE
  217.     { "/convert",     REN_XLA,  CM_ARG },
  218. #endif    /* NOUNICODE */
  219.     { "/fixspaces",   REN_SPA,  CM_ARG },
  220.     { "/l",           DEL_LIS,  CM_INV|CM_ABR },
  221.     { "/list",        DEL_LIS,  0      },
  222.     { "/log",         DEL_LIS,  CM_INV },
  223.     { "/lower",       REN_LOW,  CM_ARG },
  224.     { "/nol",         DEL_NOL,  CM_INV|CM_ABR },
  225.     { "/nolist",      DEL_NOL,  0      },
  226.     { "/nolog",       DEL_NOL,  CM_INV },
  227.     { "/quiet",       DEL_QUI,  CM_INV },
  228.     { "/replace",     REN_RPL,  CM_ARG },
  229.     { "/simulate",    DEL_SIM,  0      },
  230.     { "/upper",       REN_UPP,  CM_ARG },
  231.     { "/verbose",     DEL_VRB,  CM_INV }
  232. };
  233. static int nrenamsw = sizeof(renamsw)/sizeof(struct keytab);
  234.  
  235. static struct keytab renamset[] = {
  236.     { "collision",    REN_OVW,  0 },
  237.     { "list",         DEL_LIS,  0 }
  238. };
  239. static int nrenamset = sizeof(renamset)/sizeof(struct keytab);
  240.  
  241. /* Args for RENAME /LOWER: and /UPPER: */
  242.  
  243. static struct keytab r_upper[] = {
  244.     { "all",   1, 0 },
  245.     { "lower", 0, 0 }
  246. };
  247.  
  248. static struct keytab r_lower[] = {
  249.     { "all",   1, 0 },
  250.     { "upper", 0, 0 }
  251. };
  252.  
  253. /* Args for RENAME /COLLISION... */
  254.  
  255. #define RENX_FAIL 0
  256. #define RENX_OVWR 1
  257. #define RENX_SKIP 2
  258.  
  259. static struct keytab r_collision[] = {
  260.     { "fail",      RENX_FAIL, 0 },
  261.     { "overwrite", RENX_OVWR, 0 },
  262.     { "proceed",   RENX_SKIP, CM_INV },
  263.     { "skip",      RENX_SKIP, 0 }
  264. };
  265. static int nr_collision = sizeof(r_collision)/sizeof(struct keytab);
  266.  
  267. struct keytab copytab[] = {
  268.     { "/append",      998,      0 },
  269. #ifndef NOSPL
  270.     { "/fromb64",     997,      0 },
  271. #endif /* NOSPL */
  272.     { "/l",           DEL_LIS,  CM_INV|CM_ABR },
  273.     { "/list",        DEL_LIS,  0 },
  274.     { "/log",         DEL_LIS,  CM_INV },
  275.     { "/nol",         DEL_NOL,  CM_INV|CM_ABR },
  276.     { "/nolist",      DEL_NOL,  0 },
  277.     { "/nolog",       DEL_NOL,  CM_INV },
  278.     { "/overwrite",   994,      CM_ARG },
  279. #ifndef NOXFER
  280.     { "/preserve",    995,      0 },
  281. #endif    /* NOXFER */
  282.     { "/quiet",       DEL_QUI,  CM_INV },
  283.     { "/swap-bytes",  999,      0 },
  284. #ifndef NOSPL
  285.     { "/tob64",       996,      0 },
  286. #endif /* NOSPL */
  287.     { "/verbose",     DEL_VRB,  CM_INV }
  288. };
  289. int ncopytab = sizeof(copytab)/sizeof(struct keytab);
  290.  
  291. #define OVW_ALWAYS 0
  292. #define OVW_NEVER  1
  293. #define OVW_OLDER  2
  294. #define OVW_NEWER  3
  295.  
  296. static struct keytab ovwtab[] = {
  297.     { "always", OVW_ALWAYS, 0 },
  298.     { "never",  OVW_NEVER, 0 },
  299.     { "newer",  OVW_NEWER, 0 },
  300.     { "older",  OVW_OLDER, 0 }
  301. };
  302. static int novwtab = 4;
  303.  
  304. #ifndef NOXFER
  305. static struct keytab gettab[] = {       /* GET options */
  306.     { "/as-name",         SND_ASN, CM_ARG },
  307.     { "/binary",          SND_BIN, 0 },
  308. #ifdef CALIBRATE
  309.     { "/calibrate",       SND_CAL, CM_INV },
  310. #endif /* CALIBRATE */
  311. #ifdef PIPESEND
  312.     { "/command",         SND_CMD, CM_PSH },
  313. #endif /* PIPESEND */
  314.     { "/delete",          SND_DEL, 0 },
  315.     { "/except",          SND_EXC, CM_ARG },
  316.     { "/filenames",       SND_NAM, CM_ARG },
  317. #ifdef PIPESEND
  318.     { "/filter",          SND_FLT, CM_ARG|CM_PSH },
  319. #endif /* PIPESEND */
  320. #ifdef VMS
  321.     { "/image",           SND_IMG, 0 },
  322.     { "/labeled",         SND_LBL, 0 },
  323. #else
  324.     { "/image",           SND_BIN, CM_INV },
  325. #endif /* VMS */
  326. #ifdef CK_TMPDIR
  327.     { "/move-to",         SND_MOV, CM_ARG },
  328. #endif /* CK_TMPDIR */
  329.     { "/pathnames",       SND_PTH, CM_ARG },
  330.     { "/pipes",           SND_PIP, CM_ARG|CM_PSH },
  331.     { "/quiet",           SND_SHH, 0 },
  332. #ifdef CK_RESEND
  333.     { "/recover",         SND_RES, 0 },
  334. #endif /* CK_RESEND */
  335.     { "/recursive",       SND_REC, 0 },
  336.     { "/rename-to",       SND_REN, CM_ARG },
  337. #ifdef COMMENT
  338.     { "/smaller-than",    SND_SMA, CM_ARG },
  339. #endif /* COMMENT */
  340.     { "/subdirectories",  SND_REC, CM_INV },
  341.     { "/text",            SND_TXT, 0 },
  342.     { "/transparent",     SND_XPA, 0 }
  343. };
  344. #define NGETTAB sizeof(gettab)/sizeof(struct keytab)
  345. static int ngettab = NGETTAB;
  346.  
  347. static struct keytab rcvtab[] = {       /* RECEIVE options */
  348.     { "/as-name",         SND_ASN, CM_ARG },
  349.     { "/binary",          SND_BIN, 0 },
  350. #ifdef CALIBRATE
  351.     { "/calibrate",       SND_CAL, CM_INV },
  352. #endif /* CALIBRATE */
  353. #ifdef PIPESEND
  354.     { "/command",         SND_CMD, CM_PSH },
  355. #endif /* PIPESEND */
  356.     { "/except",          SND_EXC, CM_ARG },
  357.     { "/filenames",       SND_NAM, CM_ARG },
  358. #ifdef PIPESEND
  359.     { "/filter",          SND_FLT, CM_ARG|CM_PSH },
  360. #endif /* PIPESEND */
  361. #ifdef VMS
  362.     { "/image",           SND_IMG, 0 },
  363.     { "/labeled",         SND_LBL, 0 },
  364. #else
  365.     { "/image",           SND_BIN, CM_INV },
  366. #endif /* VMS */
  367. #ifdef CK_TMPDIR
  368.     { "/move-to",         SND_MOV, CM_ARG },
  369. #endif /* CK_TMPDIR */
  370.     { "/pathnames",       SND_PTH, CM_ARG },
  371.     { "/pipes",           SND_PIP, CM_ARG|CM_PSH },
  372. #ifdef CK_XYZ
  373.     { "/protocol",        SND_PRO, CM_ARG },
  374. #else
  375.     { "/protocol",        SND_PRO, CM_ARG|CM_INV },
  376. #endif /* CK_XYZ */
  377.     { "/quiet",           SND_SHH, 0 },
  378.     { "/recursive",       SND_REC, 0 },
  379.     { "/rename-to",       SND_REN, CM_ARG },
  380.     { "/text",            SND_TXT, 0 },
  381.     { "/transparent",     SND_XPA, 0 }
  382. };
  383. #define NRCVTAB sizeof(rcvtab)/sizeof(struct keytab)
  384. static int nrcvtab = NRCVTAB;
  385. #endif /* NOXFER */
  386.  
  387. /* WAIT table */
  388.  
  389. #define WAIT_FIL 997
  390. #define WAIT_MDM 998
  391.  
  392. struct keytab waittab[] = {
  393.     { "cd",            BM_DCD,   CM_INV }, /* (Carrier Detect) */
  394.     { "cts",           BM_CTS,   CM_INV }, /* (Clear To Send)  */
  395.     { "dsr",           BM_DSR,   CM_INV }, /* (Data Set Ready) */
  396.     { "file",          WAIT_FIL, 0 },      /* New category selector keywords */
  397.     { "modem-signals", WAIT_MDM, 0 },      /* ... */
  398.     { "ri",            BM_RNG,   CM_INV }  /* (Ring Indicator) */
  399. };
  400. int nwaittab = (sizeof(waittab) / sizeof(struct keytab));
  401.  
  402. /* Modem signal table */
  403.  
  404. struct keytab mstab[] = {
  405.     { "cd",    BM_DCD, 0 },             /* Carrier Detect */
  406.     { "cts",   BM_CTS, 0 },             /* Clear To Send  */
  407.     { "dsr",   BM_DSR, 0 },             /* Data Set Ready */
  408.     { "ri",    BM_RNG, 0 }              /* Ring Indicator */
  409. };
  410. int nms = (sizeof(mstab) / sizeof(struct keytab));
  411.  
  412. #define WF_MOD 1
  413. #define WF_DEL 2
  414. #define WF_CRE 3
  415.  
  416. struct keytab wfswi[] = {               /* WAIT FILE switches */
  417.     { "creation",     WF_CRE, 0 },      /* Wait for file to be created */
  418.     { "deletion",     WF_DEL, 0 },      /* Wait for file to be deleted */
  419.     { "modification", WF_MOD, 0 }       /* Wait for file to be modified */
  420. };
  421. int nwfswi = (sizeof(wfswi) / sizeof(struct keytab));
  422.  
  423. #ifndef NOSPL
  424. struct keytab asgtab[] = {              /* Assignment operators for "." */
  425.     { "::=", 2, 0 },                    /* ASSIGN and EVALUATE */
  426.     { ":=",  1, 0 },                    /* ASSIGN */
  427.     { "=",   0, 0 }                     /* DEFINE */
  428. };
  429. int nasgtab = (sizeof(asgtab) / sizeof(struct keytab));
  430.  
  431. struct keytab opntab[] = {
  432. #ifndef NOPUSH
  433.     { "!read",  OPN_PI_R, CM_INV },
  434.     { "!write", OPN_PI_W, CM_INV },
  435. #endif /* NOPUSH */
  436.     { "append", OPN_FI_A, 0 },
  437.     { "host",   OPN_NET,  0 },
  438. #ifdef OS2
  439.     { "line",   OPN_SER,  CM_INV },
  440.     { "port",   OPN_SER,  0 },
  441. #else
  442.     { "line",   OPN_SER,  0 },
  443.     { "port",   OPN_SER,  CM_INV },
  444. #endif /* OS2 */
  445.     { "read",   OPN_FI_R, 0 },
  446.     { "write",  OPN_FI_W, 0 }
  447. };
  448. int nopn = (sizeof(opntab) / sizeof(struct keytab));
  449.  
  450. /* IF conditions */
  451.  
  452. #define  XXIFCO 0       /* IF COUNT */
  453. #define  XXIFER 1       /* IF ERRORLEVEL */
  454. #define  XXIFEX 2       /* IF EXIST */
  455. #define  XXIFFA 3       /* IF FAILURE */
  456. #define  XXIFSU 4       /* IF SUCCESS */
  457. #define  XXIFNO 5       /* IF NOT */
  458. #define  XXIFDE 6       /* IF DEFINED */
  459. #define  XXIFEQ 7       /* IF EQUAL (strings) */
  460. #define  XXIFAE 8       /* IF = (numbers) */
  461. #define  XXIFLT 9       /* IF < (numbers) */
  462. #define  XXIFGT 10      /* IF > (numbers) */
  463. #define  XXIFLL 11      /* IF Lexically Less Than (strings) */
  464. #define  XXIFLG 12      /* IF Lexically Greater Than (strings) */
  465. #define  XXIFEO 13      /* IF EOF (READ file) */
  466. #define  XXIFBG 14      /* IF BACKGROUND */
  467. #define  XXIFNU 15      /* IF NUMERIC */
  468. #define  XXIFFG 16      /* IF FOREGROUND */
  469. #define  XXIFDI 17      /* IF DIRECTORY */
  470. #define  XXIFNE 18      /* IF NEWER */
  471. #define  XXIFRO 19      /* IF REMOTE-ONLY */
  472. #define  XXIFAL 20      /* IF ALARM */
  473. #define  XXIFSD 21      /* IF STARTED-FROM-DIALER */
  474. #define  XXIFTR 22      /* IF TRUE */
  475. #define  XXIFNT 23      /* IF FALSE */
  476. #define  XXIFTM 24      /* IF TERMINAL-MACRO */
  477. #define  XXIFEM 25      /* IF EMULATION */
  478. #define  XXIFOP 26      /* IF OPEN */
  479. #define  XXIFLE 27      /* IF <= */
  480. #define  XXIFGE 28      /* IF >= */
  481. #define  XXIFIP 29      /* IF INPATH */
  482. #define  XXIFTA 30      /* IF TAPI */
  483. #define  XXIFMA 31      /* IF MATCH */
  484. #define  XXIFFL 32      /* IF FLAG */
  485. #define  XXIFAB 33      /* IF ABSOLUTE */
  486. #define  XXIFAV 34      /* IF AVAILABLE */
  487. #define  XXIFAT 35      /* IF ASKTIMEOUT */
  488. #define  XXIFRD 36      /* IF READABLE */
  489. #define  XXIFWR 37      /* IF WRITEABLE */
  490. #define  XXIFAN 38      /* IF ... AND ... */
  491. #define  XXIFOR 39      /* IF ... OR ... */
  492. #define  XXIFLP 40      /* IF left parenthesis */
  493. #define  XXIFRP 41      /* IF right parenthesis */
  494. #define  XXIFNQ 42      /* IF != (== "NOT =") */
  495. #define  XXIFQU 43      /* IF QUIET */
  496. #define  XXIFCK 44      /* IF C-KERMIT */
  497. #define  XXIFK9 45      /* IF K-95 */
  498. #define  XXIFMS 46      /* IF MS-KERMIT */
  499. #define  XXIFWI 47      /* IF WILD */
  500. #define  XXIFLO 48      /* IF LOCAL */
  501. #define  XXIFCM 49      /* IF COMMAND */
  502. #define  XXIFFP 50      /* IF FLOAT */
  503. #define  XXIFIK 51      /* IF IKS */
  504. #define  XXIFKB 52      /* IF KBHIT */
  505. #define  XXIFKG 53      /* IF KERBANG */
  506. #define  XXIFVE 54      /* IF VERSION */
  507. #define  XXIFDC 55      /* IF DECLARED */
  508. #define  XXIFGU 56      /* IF GUI */
  509. #define  XXIFLN 57    /* IF LINK */
  510.  
  511. struct keytab iftab[] = {               /* IF commands */
  512.     { "!",          XXIFNO, 0 },
  513.     { "!=",         XXIFNQ, 0 },
  514.     { "&&",         XXIFAN, 0 },
  515.     { "(",          XXIFLP, 0 },
  516.     { ")",          XXIFRP, 0 },
  517.     { "<",          XXIFLT, 0 },
  518.     { "<=",         XXIFLE, 0 },
  519.     { "=",          XXIFAE, 0 },
  520.     { "==",         XXIFAE, CM_INV },
  521.     { ">",          XXIFGT, 0 },
  522.     { ">=",         XXIFGE, 0 },
  523.     { "absolute",   XXIFAB, 0 },
  524.     { "alarm",      XXIFAL, 0 },
  525.     { "and",        XXIFAN, 0 },
  526.     { "asktimeout", XXIFAT, 0 },
  527.     { "available",  XXIFAV, 0 },
  528.     { "background", XXIFBG, 0 },
  529.     { "c-kermit",   XXIFCK, 0 },
  530.     { "command",    XXIFCM, 0 },
  531.     { "count",      XXIFCO, 0 },
  532.     { "dcl",        XXIFDC, CM_INV },
  533.     { "declared",   XXIFDC, 0 },
  534.     { "defined",    XXIFDE, 0 },
  535. #ifdef CK_TMPDIR
  536.     { "directory",  XXIFDI, 0 },
  537. #endif /* CK_TMPDIR */
  538.     { "emulation",  XXIFEM, 0 },
  539. #ifdef COMMENT
  540.     { "eof",        XXIFEO, 0 },
  541. #endif /* COMMENT */
  542.     { "equal",      XXIFEQ, 0 },
  543.     { "error",      XXIFFA, CM_INV },
  544.     { "exist",      XXIFEX, 0 },
  545.     { "failure",    XXIFFA, 0 },
  546.     { "false",      XXIFNT, 0 },
  547.     { "flag",       XXIFFL, 0 },
  548. #ifdef CKFLOAT
  549.     { "float",      XXIFFP, 0 },
  550. #endif /* CKFLOAT */
  551.     { "foreground", XXIFFG, 0 },
  552. #ifdef OS2
  553.     { "gui",        XXIFGU, 0 },
  554. #else
  555.     { "gui",        XXIFGU, CM_INV },
  556. #endif /* OS2 */
  557. #ifdef IKSD
  558.     { "iksd",       XXIFIK, 0 },
  559. #else
  560.     { "iksd",       XXIFIK, CM_INV },
  561. #endif /* IKSD */
  562.     { "integer",    XXIFNU, CM_INV },
  563.     { "k-95",       XXIFK9, 0 },
  564.     { "kbhit",      XXIFKB, 0 },
  565. #ifdef UNIX
  566.     { "kerbang",    XXIFKG, 0 },
  567. #else
  568.     { "kerbang",    XXIFKG, CM_INV },
  569. #endif /* UNIX */
  570.     { "lgt",        XXIFLG, 0 },
  571. #ifdef UNIX
  572.     { "link",       XXIFLN, 0 },
  573. #endif /* UNIX */
  574.     { "llt",        XXIFLL, 0 },
  575.     { "local",      XXIFLO, 0 },
  576.     { "match",      XXIFMA, 0 },
  577.     { "ms-kermit",  XXIFMS, CM_INV },
  578. #ifdef ZFCDAT
  579.     { "newer",      XXIFNE, 0 },
  580. #endif /* ZFCDAT */
  581.     { "not",        XXIFNO, 0 },
  582.     { "numeric",    XXIFNU, 0 },
  583.     { "ok",         XXIFSU, CM_INV },
  584.     { "open",       XXIFOP, 0 },
  585.     { "or",         XXIFOR, 0 },
  586.     { "quiet",      XXIFQU, 0 },
  587.     { "readable",   XXIFRD, 0 },
  588.     { "remote-only",XXIFRO, 0 },
  589.     { "started-from-dialer",XXIFSD, CM_INV },
  590.     { "success",    XXIFSU, 0 },
  591.     { "tapi",       XXIFTA, 0 },
  592. #ifdef OS2
  593.     { "terminal-macro", XXIFTM, 0 },
  594. #else
  595.     { "terminal-macro", XXIFTM, CM_INV },
  596. #endif /* OS2 */
  597.     { "true",       XXIFTR, 0 },
  598.     { "version",    XXIFVE, 0 },
  599.     { "wild",       XXIFWI, 0 },
  600.     { "writeable",  XXIFWR, 0 },
  601.     { "||",         XXIFOR, 0 },
  602.     { "", 0, 0 }
  603. };
  604. int nif = (sizeof(iftab) / sizeof(struct keytab)) - 1;
  605.  
  606. struct keytab iotab[] = {               /* Keywords for IF OPEN */
  607.     { "!read-file",      ZRFILE, CM_INV },
  608.     { "!write-file",     ZWFILE, CM_INV },
  609.     { "append-file",     ZWFILE, CM_INV },
  610.     { "connection",      8888,   0 },
  611. #ifdef CKLOGDIAL
  612.     { "cx-log",          7777,   0 },
  613. #endif /* CKLOGDIAL */
  614.     { "debug-log",       ZDFILE, 0 },
  615.     { "error",           9999,   0 },
  616.     { "packet-log",      ZPFILE, 0 },
  617.     { "read-file",       ZRFILE, 0 },
  618.     { "screen",          ZSTDIO, 0 },
  619.     { "session-log",     ZSFILE, 0 },
  620.     { "transaction-log", ZTFILE, 0 },
  621.     { "write-file",      ZWFILE, 0 }
  622. };
  623. int niot = (sizeof(iotab) / sizeof(struct keytab));
  624. #endif /* NOSPL */
  625.  
  626. /* Variables and prototypes */
  627.  
  628. _PROTOTYP(static int doymdir,(int));
  629. _PROTOTYP(static int renameone,(char *,char *,
  630.                 int,int,int,int,int,int,int,int,int,int,int));
  631.  
  632. #ifdef NETCONN
  633. extern int nnetdir;                     /* How many network directories */
  634. #endif /* NETCONN */
  635. #ifdef CK_SECURITY
  636. _PROTOTYP(int ck_krb4_is_installed,(void));
  637. _PROTOTYP(int ck_krb5_is_installed,(void));
  638. _PROTOTYP(int ck_ntlm_is_installed,(void));
  639. _PROTOTYP(int ck_srp_is_installed,(void));
  640. _PROTOTYP(int ck_ssleay_is_installed,(void));
  641. _PROTOTYP(int ck_ssh_is_installed,(void));
  642. _PROTOTYP(int ck_crypt_is_installed,(void));
  643. #else
  644. #define ck_krb4_is_installed() (0)
  645. #define ck_krb5_is_installed() (0)
  646. #define ck_ntlm_is_installed() (0)
  647. #define ck_srp_is_installed() (0)
  648. #define ck_ssleay_is_installed() (0)
  649. #define ck_ssh_is_installed() (0)
  650. #define ck_crypt_is_installed() (0)
  651. #endif /* CK_SECURITY */
  652.  
  653. #define AV_KRB4   1
  654. #define AV_KRB5   2
  655. #define AV_NTLM   3
  656. #define AV_SRP    4
  657. #define AV_SSL    5
  658. #define AV_CRYPTO 6
  659. #define AV_SSH    7
  660.  
  661. struct keytab availtab[] = {             /* Available authentication types */
  662.     { "crypto",     AV_CRYPTO, CM_INV }, /* and encryption */
  663.     { "encryption", AV_CRYPTO, 0 },
  664.     { "k4",         AV_KRB4,   CM_INV },
  665.     { "k5",         AV_KRB5,   CM_INV },
  666.     { "kerberos4",  AV_KRB4,   0 },
  667.     { "kerberos5",  AV_KRB5,   0 },
  668.     { "krb4",       AV_KRB4,   CM_INV },
  669.     { "krb5",       AV_KRB5,   CM_INV },
  670.     { "ntlm",       AV_NTLM,   0 },
  671.     { "srp",        AV_SRP,    0 },
  672.     { "ssh",        AV_SSH,    0 },
  673.     { "ssl",        AV_SSL,    0 },
  674.     { "tls",        AV_SSL,    0 },
  675.     { "",           0,         0 }
  676. };
  677. int availtabn = sizeof(availtab)/sizeof(struct keytab)-1;
  678.  
  679. #ifndef NODIAL
  680. _PROTOTYP(static int ddcvt, (char *, FILE *, int) );
  681. _PROTOTYP(static int dncvt, (int, int, int, int) );
  682. _PROTOTYP(char * getdname, (void) );
  683.  
  684. static int partial  = 0;                /* For partial dial */
  685. static char *dscopy = NULL;
  686. int dialtype = -1;
  687.  
  688. char *dialnum = (char *)0;              /* Remember DIAL number for REDIAL */
  689. int dirline = 0;                        /* Dial directory line number */
  690. extern char * dialdir[];                /* Dial directory file names */
  691. extern int dialdpy;                     /* DIAL DISPLAY on/off */
  692. extern int ndialdir;                    /* How many dial directories */
  693. extern int ntollfree;                   /* Toll-free call info */
  694. extern int ndialpxx;                    /* List of PBX exchanges */
  695. extern char *dialtfc[];
  696. char * matchpxx = NULL;                 /* PBX exchange that matched */
  697. extern int nlocalac;                    /* Local area-code list */
  698. extern char * diallcac[];
  699. extern int tttapi;
  700. #ifdef CK_TAPI
  701. extern int tapiconv;                    /* TAPI Conversions */
  702. extern int tapipass;                    /* TAPI Passthrough */
  703. #endif /* CK_TAPI */
  704. extern int dialatmo;
  705. extern char * dialnpr, * dialsfx;
  706. extern char * dialixp, * dialixs, * dialmac;
  707. extern char * dialldp, * diallds, * dialtfp;
  708. extern char * dialpxi, * dialpxo, * diallac;
  709. extern char * diallcp, * diallcs, * diallcc;
  710. extern char * dialpxx[];
  711.  
  712. extern int dialcnf;                     /* DIAL CONFIRMATION */
  713. int dialfld = 0;                        /* DIAL FORCE-LONG-DISTANCE */
  714. int dialsrt = 1;                        /* DIAL SORT ON */
  715. int dialrstr = 6;                       /* DIAL RESTRICTION */
  716. int dialtest = 0;                       /* DIAL TEST */
  717. int dialcount = 0;                      /* \v(dialcount) */
  718.  
  719. extern int dialsta;                     /* Dial status */
  720. int dialrtr = -1,                       /* Dial retries */
  721.     dialint = 10;                       /* Dial retry interval */
  722. extern long dialcapas;                  /* Modem capabilities */
  723. extern int dialcvt;                     /* DIAL CONVERT-DIRECTORY */
  724. #endif /* NODIAL */
  725.  
  726. #ifndef NOSPL
  727. #define IFCONDLEN 256
  728. int ifc,                                /* IF case */
  729.     not = 0,                            /* Flag for IF NOT */
  730.     ifargs = 0;                         /* Count of IF condition words */
  731. char ifcond[IFCONDLEN];                 /* IF condition text */
  732. char *ifcp;                             /* Pointer to IF condition text */
  733. extern int vareval;
  734. #ifdef DCMDBUF
  735. extern int
  736.  *ifcmd,  *count,  *iftest, *intime,
  737.     *inpcas, *takerr, *merror, *xquiet, *xvarev;
  738. #else
  739. extern int ifcmd[];                     /* Last command was IF */
  740. extern int iftest[];                    /* Last IF was true */
  741. extern int count[];                     /* For IF COUNT, one for each cmdlvl */
  742. extern int intime[];                    /* Ditto for other stackables... */
  743. extern int inpcas[];
  744. extern int takerr[];
  745. extern int merror[];
  746. extern int xquiet[];
  747. extern int xvarev[];
  748. #endif /* DCMDBUF */
  749. #else
  750. extern int takerr[];
  751. #endif /* NOSPL */
  752.  
  753. #ifdef DCMDBUF
  754. extern char *line;                      /* Character buffer for anything */
  755. extern char *tmpbuf;
  756. #else
  757. extern char line[], tmpbuf[];
  758. #endif /* DCMDBUF */
  759. extern char *lp;                        /* Pointer to line buffer */
  760.  
  761. int cwdf = 0;                           /* CWD has been done */
  762.  
  763. /* Flags for ENABLE/DISABLE */
  764. extern int en_cwd, en_cpy, en_del, en_dir, en_fin,
  765.    en_get, en_hos, en_ren, en_sen, en_set, en_spa, en_typ, en_who, en_bye,
  766.    en_asg, en_que, en_ret, en_mai, en_pri, en_mkd, en_rmd, en_xit, en_ena;
  767.  
  768. extern FILE *tfile[];                   /* File pointers for TAKE command */
  769. extern char *tfnam[];                   /* Names of TAKE files */
  770. extern int tfline[];                    /* TAKE-file line number */
  771.  
  772. extern int success;                     /* Command success/failure flag */
  773. extern int cmdlvl;                      /* Current position in command stack */
  774.  
  775. #ifndef NOSPL
  776. extern int maclvl;                      /* Macro to execute */
  777. extern char *macx[];                    /* Index of current macro */
  778. extern char *mrval[];                   /* Macro return value */
  779. extern char *macp[];                    /* Pointer to macro */
  780. extern int macargc[];                   /* ARGC from macro invocation */
  781.  
  782. #ifdef COMMENT
  783. extern char *m_line[];
  784. #endif /* COMMENT */
  785.  
  786. extern char *m_arg[MACLEVEL][NARGS];    /* Stack of macro arguments */
  787. extern char *g_var[];                   /* Global variables %a, %b, etc */
  788.  
  789. #ifdef DCMDBUF
  790. extern struct cmdptr *cmdstk;           /* The command stack itself */
  791. #else
  792. extern struct cmdptr cmdstk[];          /* The command stack itself */
  793. #endif /* DCMDBUF */
  794. #endif /* NOSPL */
  795.  
  796. #define xsystem(s) zsyscmd(s)
  797.  
  798. static int x, y, z = 0;
  799. static char *s, *p;
  800.  
  801. #ifdef OS2
  802. _PROTOTYP( int os2settitle, (char *, int) );
  803. #endif /* OS2 */
  804.  
  805. extern struct keytab yesno[], onoff[], fntab[];
  806. extern int nyesno, nfntab;
  807.  
  808. #ifndef NOSPL
  809.  
  810. /* Do the ASK, ASKQ, GETOK, and READ commands */
  811.  
  812. int asktimedout = 0;
  813.  
  814. #define ASK_PUP 1
  815. #define ASK_TMO 2
  816. #define ASK_GUI 3
  817. #define ASK_QUI 4
  818. #define ASK_DEF 5
  819. #define ASK_ECH 6
  820.  
  821. static struct keytab asktab[] = {
  822.     {  "/default", ASK_DEF, CM_ARG },
  823.     {  "/gui",     ASK_GUI,      
  824. #ifdef KUI
  825.            0
  826. #else /* KUI */
  827.            CM_INV
  828. #endif /* KUI */
  829.     },
  830.     { "/popup",    ASK_PUP,   
  831. #ifdef OS2
  832.            0
  833. #else /* OS2 */
  834.            CM_INV
  835. #endif /* OS2 */
  836.     },
  837.     { "/quiet",    ASK_QUI, 0 },
  838.     { "/timeout",  ASK_TMO, CM_ARG },
  839.     { "", 0, 0 }
  840. };
  841. static int nasktab = sizeof(asktab)/sizeof(struct keytab)-1;
  842.  
  843. static struct keytab askqtab[] = {
  844.     { "/default",  ASK_DEF, CM_ARG },
  845.     { "/echo",     ASK_ECH, CM_ARG },
  846.     { "/gui",      ASK_GUI,      
  847. #ifdef KUI
  848.            0
  849. #else /* KUI */
  850.            CM_INV
  851. #endif /* KUI */
  852.     },
  853.     { "/noecho",   ASK_QUI, CM_INV },
  854.     { "/popup",    ASK_PUP,   
  855. #ifdef OS2
  856.            0
  857. #else /* OS2 */
  858.            CM_INV
  859. #endif /* OS2 */
  860.     },
  861.     { "/quiet",    ASK_QUI, 0 },
  862.     { "/timeout",  ASK_TMO, CM_ARG },
  863.     { "", 0, 0 }
  864. };
  865. static int naskqtab = sizeof(askqtab)/sizeof(struct keytab)-1;
  866.  
  867. int
  868. doask(cx) int cx; {
  869.     extern int asktimer, timelimit;
  870. #ifdef CK_RECALL
  871.     extern int on_recall;
  872. #endif /* CK_RECALL */
  873.     int echochar = 0;
  874.     int popupflg = 0;
  875.     int guiflg = 0;
  876.     int nomsg = 0;
  877.     int mytimer = 0;
  878. #ifdef CK_APC
  879.     extern int apcactive, apcstatus;
  880. #endif /* CK_APC */
  881.  
  882.     char dfbuf[1024];            /* Buffer for default answer */
  883.     char * dfanswer = NULL;        /* Pointer to it */
  884.  
  885.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  886.     char *vnp = NULL;                   /* Pointer to same */
  887.     
  888.     dfbuf[0] = NUL;
  889.     vnambuf[0] = NUL;
  890.  
  891. #ifdef CK_APC
  892.     if ( apcactive != APC_INACTIVE && (apcstatus & APC_NOINP) ) {
  893.         return(success = 0);
  894.     }
  895. #endif /* CK_APC */
  896.  
  897.     mytimer = asktimer;                 /* Inherit global ASK timer */
  898.     echostars = 0;            /* For ASKQ */
  899.  
  900.     if (cx == XXASK || cx == XXASKQ) {
  901.         struct FDB sw, fl;
  902.         int getval;
  903.         char c;
  904.         if (cx == XXASKQ)               /* Don't log ASKQ response */
  905.           debok = 0;
  906.         cmfdbi(&sw,                     /* First FDB - command switches */
  907.                _CMKEY,                  /* fcode */
  908.                "Variable name or switch",
  909.                "",                      /* default */
  910.                "",                      /* addtl string data */
  911.            ((cx == XXASK) ? nasktab : naskqtab), /* Table size */
  912.                4,                       /* addtl numeric data 2: 4 = cmswi */
  913.                xxstring,                /* Processing function */
  914.            ((cx == XXASK) ? asktab : askqtab), /* Keyword table */
  915.                &fl                      /* Pointer to next FDB */
  916.                );
  917.         cmfdbi(&fl,                     /* Anything that doesn't match */
  918.                _CMFLD,                  /* fcode */
  919.                "",                      /* hlpmsg */
  920.                "",                      /* default */
  921.                "",                      /* addtl string data */
  922.                0,                       /* addtl numeric data 1 */
  923.                0,                       /* addtl numeric data 2 */
  924.                NULL,
  925.                NULL,
  926.                NULL
  927.                );
  928.         while (1) {                     /* Parse 0 or more switches */
  929.             x = cmfdb(&sw);             /* Parse something */
  930.             if (x < 0)
  931.               return(x);
  932.             if (cmresult.fcode != _CMKEY) /* Break out if not a switch */
  933.               break;
  934.             c = cmgbrk();
  935.             if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  936.                 printf("?This switch does not take an argument\n");
  937.                 return(-9);
  938.             }
  939.             if (!getval && (cmgkwflgs() & CM_ARG)) {
  940.                 printf("?This switch requires an argument\n");
  941.                 return(-9);
  942.             }
  943.             switch (cmresult.nresult) {
  944.           case ASK_QUI:
  945.         nomsg = 1;
  946.         if (cx == XXASKQ)
  947.           echostars = 0;
  948.         break;
  949.               case ASK_PUP:
  950.                 popupflg = 1;
  951.                 break;
  952.           case ASK_GUI:
  953.         guiflg = 1;
  954.         break;
  955.               case ASK_TMO: {
  956.                   if ((y = cmnum("seconds","1",10,&x,xxstring)) < 0)
  957.                     return(y);
  958.                   if (x < 0)
  959.                     x = 0;
  960.                   mytimer = x;
  961.                   break;
  962.               }
  963.               case ASK_ECH: {
  964.                   if ((y = cmfld("Character to echo","*",&s,xxstring)) < 0)
  965.                     return(y);
  966.           echochar = *s;
  967.                   break;
  968.               }
  969.               case ASK_DEF: {
  970.                   if ((y = cmfld("Text to supply if reply is empty",
  971.                  "",&s,xxstring)) < 0)
  972.                     return(y);
  973.           ckstrncpy(dfbuf,s,1024);
  974.           dfanswer = dfbuf;
  975.                   break;
  976.               }
  977.               default: return(-2);
  978.             }
  979.         }
  980.         /* Have variable name, make copy. */
  981.         ckstrncpy(vnambuf,cmresult.sresult,VNAML);
  982.         vnp = vnambuf;
  983.         if (vnambuf[0] == CMDQ &&
  984.             (vnambuf[1] == '%' || vnambuf[1] == '&'))
  985.           vnp++;
  986.         y = 0;
  987.         if (*vnp == '%' || *vnp == '&') {
  988.             if ((y = parsevar(vnp,&x,&z)) < 0)
  989.           return(y);
  990.         }
  991.     } else if (cx != XXGOK && cx != XXRDBL) { /* Get variable name */
  992.         if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  993.             if (y == -3) {
  994.                 printf("?Variable name required\n");
  995.                 return(-9);
  996.             } else return(y);
  997.         }
  998.         ckstrncpy(vnambuf,s,VNAML);     /* Make a copy. */
  999.         vnp = vnambuf;
  1000.         if (vnambuf[0] == CMDQ &&
  1001.             (vnambuf[1] == '%' || vnambuf[1] == '&'))
  1002.           vnp++;
  1003.         y = 0;
  1004.         if (*vnp == '%' || *vnp == '&') {
  1005.             if ((y = parsevar(vnp,&x,&z)) < 0)
  1006.               return(y);
  1007.         }
  1008.     }
  1009.     if (cx == XXREA || cx == XXRDBL) {  /* READ or READBLOCK command */
  1010.         if ((y = cmcfm()) < 0)          /* Get confirmation */
  1011.           return(y);
  1012.         if (chkfn(ZRFILE) < 1) {        /* File open? */
  1013.             printf("?Read file not open\n");
  1014.             return(success = 0);
  1015.         }
  1016.         if (!(s = (char *)readbuf)) {           /* Where to read into. */
  1017.             printf("?Oops, no READ buffer!\n");
  1018.             return(success = 0);
  1019.         }
  1020.         y = zsinl(ZRFILE, s, readblock); /* Read a line. */
  1021.         debug(F111,"read zsinl",s,y);
  1022.         if (y < 0) {                    /* On EOF or other error, */
  1023.             zclose(ZRFILE);             /* close the file, */
  1024.             delmac(vnp,0);              /* delete the variable, */
  1025.             return(success = 0);        /* and return failure. */
  1026.         } else {                        /* Read was OK. */
  1027.             readsize = (int) strlen(s);
  1028.             success = (addmac(vnp,s) < 0 ? 0 : 1); /* Define variable */
  1029.             debug(F111,"read addmac",vnp,success);
  1030.             return(success);            /* Return success. */
  1031.         }
  1032.     }
  1033.  
  1034.     /* ASK, ASKQ, GETOK, or GETC */
  1035.  
  1036.     if (cx == XXGOK) {            /* GETOK can take switches */
  1037.         struct FDB sw, fl;
  1038.         int getval;
  1039.         char c;
  1040.         cmfdbi(&sw,                     /* First FDB - command switches */
  1041.                _CMKEY,                  /* fcode */
  1042.                "Variable name or question prompt",
  1043.                "",                      /* default */
  1044.                "",                      /* addtl string data */
  1045.                nasktab,                 /* addtl numeric data 1: tbl size */
  1046.                4,                       /* addtl numeric data 2: 4 = cmswi */
  1047.                xxstring,                /* Processing function */
  1048.                asktab,                  /* Keyword table */
  1049.                &fl                      /* Pointer to next FDB */
  1050.                );
  1051.         cmfdbi(&fl,                     /* Anything that doesn't match */
  1052.                _CMTXT,                  /* fcode */
  1053.                "",                      /* hlpmsg */
  1054.                "",                      /* default */
  1055.                "",                      /* addtl string data */
  1056.                0,                       /* addtl numeric data 1 */
  1057.                0,                       /* addtl numeric data 2 */
  1058.                NULL,
  1059.                NULL,
  1060.                NULL
  1061.                );
  1062.         while (1) {                     /* Parse 0 or more switches */
  1063.             x = cmfdb(&sw);             /* Parse something */
  1064.             if (x < 0)
  1065.               return(x);
  1066.             if (cmresult.fcode != _CMKEY) /* Break out if not a switch */
  1067.               break;
  1068.             c = cmgbrk();
  1069.             if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  1070.                 printf("?This switch does not take an argument\n");
  1071.                 return(-9);
  1072.             }
  1073.             if (!getval && (cmgkwflgs() & CM_ARG)) {
  1074.                 printf("?This switch requires an argument\n");
  1075.                 return(-9);
  1076.             }
  1077.             switch (cmresult.nresult) {
  1078.               case ASK_PUP:
  1079.                 popupflg = 1;
  1080.                 break;
  1081.           case ASK_GUI:
  1082.         guiflg = 1;
  1083.         break;
  1084.               case ASK_TMO: {
  1085.                   if ((y = cmnum("seconds","1",10,&x,xxstring)) < 0)
  1086.                     return(y);
  1087.                   if (x < 0)
  1088.                     x = 0;
  1089.                   mytimer = x;
  1090.                   break;
  1091.               }
  1092.               case ASK_DEF: {
  1093.                   if ((y = cmfld("Text to supply if reply is empty",
  1094.                  "",&s,xxstring)) < 0)
  1095.                     return(y);
  1096.           ckstrncpy(dfbuf,s,1024);
  1097.           dfanswer = dfbuf;
  1098.                   break;
  1099.               }
  1100.           case ASK_QUI:
  1101.         nomsg = 1;
  1102.         break;
  1103.               default: return(-2);
  1104.             }
  1105.     }
  1106.     p = cmresult.sresult;
  1107.     } else
  1108.       if ((y = cmtxt(
  1109. "Prompt,\n\
  1110.  enclose in { braces } or \" quotes \" to preserve leading and trailing\n\
  1111.  spaces, precede question mark with backslash (\\).",
  1112.                    "",&p,xxstring)) < 0)
  1113.         return(y);
  1114.  
  1115.     if (!p) p = "";
  1116. #ifndef NOLOCAL
  1117. #ifdef OS2
  1118.     if (popupflg) {                     /* Popup requested */
  1119.         int len = -1;
  1120.         ckstrncpy(tmpbuf,brstrip(p),TMPBUFSIZ);
  1121.         p = tmpbuf;
  1122.         if (cx == XXASK || cx == XXASKQ) {
  1123.             if (cx == XXASK)
  1124.               len = popup_readtext(vmode,NULL,p,line,LINBUFSIZ,mytimer);
  1125.             else
  1126.               len = popup_readpass(vmode,NULL,p,line,LINBUFSIZ,mytimer);
  1127.             asktimedout = ( len < 0 && mytimer );
  1128.         } else if (cx == XXGOK) {
  1129.         printf("?Sorry, GETOK /POPUP not implemented yet\n");
  1130.         timelimit = 0;
  1131.         return(-9);
  1132.     }
  1133.         if (len >= 0) {
  1134.         y = addmac(vnp,(char *)line); /* Add it to the macro table. */
  1135.         } else if ( asktimedout && dfanswer ) {
  1136.             y = addmac(vnp,dfanswer);        /* Add it to the macro table. */
  1137.             asktimedout = 0;
  1138.             len = 0;
  1139.         }
  1140.         timelimit = 0;
  1141.         return(success = ((len >= 0) && (y >= 0)) && !asktimedout);
  1142.     }
  1143. #ifdef KUI
  1144.     if (guiflg) {                       /* GUI requested */
  1145.         int rc, n;
  1146.     char * s1;
  1147.     s1 = tmpbuf;
  1148.     n = TMPBUFSIZ-1;
  1149.     zzstring(brstrip(p),&s1,&n);
  1150.         p = tmpbuf;
  1151.         if (cx == XXASK || cx == XXASKQ) {
  1152.             rc = gui_txt_dialog(NULL,p,(cx == XXASK),
  1153.                                 line,LINBUFSIZ,dfanswer,mytimer);
  1154.             asktimedout = (rc == -1 && mytimer);
  1155.             if (rc == 1) {
  1156.                 y = addmac(vnp,(char *)line); /* Add it to the macro table. */
  1157.             } else if ( asktimedout && dfanswer ) {
  1158.                 y = addmac(vnp,dfanswer); /* Add default to macro table. */
  1159.                 asktimedout = 0;
  1160.                 rc = 1;
  1161.             }
  1162.         timelimit = 0;
  1163.         return(success = (rc == 1 && (y >= 0)) && !asktimedout);
  1164.     } else if (cx == XXGOK) {
  1165.         int x;
  1166.         x = lookup(yesno,dfanswer,nyesno,NULL);
  1167.         if (x != 1) x = 2;
  1168.         rc = uq_ok(NULL, p, 3, NULL, x);
  1169.         return(success = (rc == 1));
  1170.     }
  1171.     }
  1172. #endif /* KUI */
  1173. #endif /* OS2 */
  1174. #endif /* NOLOCAL */
  1175.  
  1176.     concb((char)escape);                /* Enter CBREAK mode */
  1177.     cmsavp(psave,PROMPTL);              /* Save old prompt */
  1178.     cmsetp(brstrip(p));                 /* Make new prompt */
  1179.  
  1180. reprompt:
  1181.     if (cx == XXASKQ) {                 /* For ASKQ, */
  1182.         cmini(0);                       /* no-echo mode. */
  1183.     if (echochar)
  1184.       echostars = echochar;
  1185.     } else {                            /* For others, regular echoing. */
  1186.         cmini(ckxech);
  1187.     echostars = 0;
  1188.     }
  1189.     askflag = 1;
  1190.     x = -1;                             /* This means to reparse. */
  1191.     cmflgs = 0;
  1192.     if (pflag)
  1193.       prompt(xxstring);                 /* Issue prompt. */
  1194.  
  1195.     asktimedout = 0;                    /* Handle timed responses. */
  1196.     timelimit = mytimer;
  1197. reparse:
  1198.     cmres();
  1199.     if (cx == XXGOK) {                  /* GETOK */
  1200. #ifdef CK_RECALL
  1201.         on_recall = 0;
  1202. #endif /* CK_RECALL */
  1203.         askflag = 0;
  1204.     /* GETOK uses keyword table */
  1205.         x = cmkey(yesno,nyesno,"",dfanswer,xxstring);
  1206.         if (x < 0) {                    /* Parse error */
  1207.             if (x == -10) {
  1208.         char * ds;
  1209.         ds = dfanswer ? dfanswer : "No";
  1210.         if (!nomsg)
  1211.           printf("?Timed out, assuming \"%s\"",ds);
  1212.         printf("\n");
  1213.                 asktimedout = 1;
  1214.         x = lookup(yesno,ds,nyesno,NULL);
  1215.         if (x != 1) x = 0;
  1216.                 goto gokdone;
  1217.             } else if (x == -3) {       /* No answer? */
  1218.                 printf("Please respond Yes or No\n"); /* Make them answer */
  1219.                 cmini(ckxech);
  1220.                 goto reprompt;
  1221.             } else if (x == -1) {
  1222.                 goto reparse;
  1223.             } else
  1224.               goto reprompt;
  1225.         }
  1226.         if (cmcfm() < 0)                /* Get confirmation */
  1227.           goto reparse;
  1228.   gokdone:
  1229.         askflag = 0;
  1230.         cmsetp(psave);                  /* Restore prompt */
  1231. #ifdef VMS
  1232.         if (cmdlvl > 0)                 /* In VMS and not at top level, */
  1233.           conres();                     /*  restore console again. */
  1234. #endif /* VMS */
  1235.         timelimit = 0;
  1236.         return(x);                      /* Return success or failure */
  1237.     } else if (cx == XXGETC             /* GETC */
  1238. #ifdef OS2
  1239.                || cx == XXGETK          /* or GETKEYCODE */
  1240. #endif /* OS2 */
  1241.                ) {                      /* GETC */
  1242.         char tmp[16];
  1243.         conbin((char)escape);           /* Put keyboard in raw mode */
  1244. #ifndef NOSETKEY
  1245. #ifdef OS2
  1246.         if (cx == XXGETK) {             /* GETKEYCODE */
  1247.             extern int os2gks;
  1248.             int t;
  1249.             t = os2gks;                 /* Turn off kverb recognition */
  1250.             os2gks = 0;
  1251.             x = congks(timelimit);      /* Read a key event, blocking */
  1252.             os2gks = t;                 /* Put back kverb recognition */
  1253.         } else                          /* GETC */
  1254. #endif /* OS2 */
  1255. #endif /* NOSETKEY */
  1256.         {
  1257.             debug(F101,"GETC conchk","",conchk());
  1258.             x = coninc(timelimit);      /* Just read one character */
  1259.             debug(F101,"GETC coninc","",x);
  1260.         }
  1261.         concb((char)escape);            /* Put keyboard back in cbreak mode */
  1262.         if (x > -1) {
  1263.             if (xcmdsrc == 0)
  1264.               printf("\r\n");
  1265. #ifdef OS2
  1266.             if (cx == XXGETK) {         /* GETKEYCODE */
  1267.                 sprintf(tmp,"%d",x);    /* SAFE */
  1268.             } else {
  1269. #endif /* OS2 */
  1270.                 tmp[0] = (char) (x & 0xff);
  1271.                 tmp[1] = NUL;
  1272. #ifdef OS2
  1273.             }
  1274. #endif /* OS2 */
  1275.             y = addmac(vnp,tmp);        /* Add it to the macro table. */
  1276.             debug(F111,"getc/getk addmac",vnp,y);
  1277.         } else y = -1;
  1278.         cmsetp(psave);                  /* Restore old prompt. */
  1279.         if (x < -1) {
  1280.             asktimedout = 1;
  1281.             if (!quiet && !nomsg)
  1282.               printf("?Timed out");
  1283.         printf("\n");
  1284.         }
  1285.         timelimit = 0;
  1286.         return(success = ((y < 0 ? 0 : 1) && (asktimedout == 0)));
  1287.     } else {                            /* ASK or ASKQ */
  1288. #ifdef CK_RECALL
  1289.         on_recall = 0;            /* Don't put response in recall buf */
  1290. #endif /* CK_RECALL */
  1291.     askflag = 1;            /* ASK[Q] always goes to terminal */
  1292.         y = cmdgquo();                  /* Get current quoting */
  1293.         cmdsquo(0);                     /* Turn off quoting */
  1294.         while (x == -1) {               /* Prompt till they answer */
  1295.             x = cmtxt("Please respond.",dfanswer,&s,NULL);
  1296.             debug(F111,"ASK cmtxt",s,x);
  1297.             cmres();
  1298.         }
  1299.         cmdsquo(y);                     /* Restore previous quoting */
  1300.         if (cx == XXASKQ)               /* ASKQ must echo CRLF here */
  1301.           printf("\r\n");
  1302.     if (x == -10 && dfanswer) {    /* Don't fail on timeout if */
  1303.         s = dfanswer;        /* a default was specified */
  1304.         asktimedout = 0;        /* and don't fail */
  1305.         x = 0;
  1306.     }
  1307.         if (x < 0) {                    /* If cmtxt parse error, */
  1308.             cmsetp(psave);              /* restore original prompt */
  1309. #ifdef VMS
  1310.             if (cmdlvl > 0)             /* In VMS and not at top level, */
  1311.               conres();                 /*  restore console again. */
  1312. #endif /* VMS */
  1313.             if (x == -10) {        /* Timed out with no response */
  1314.         if (!nomsg)
  1315.           printf("?Timed out");
  1316.         printf("\n");
  1317.                 asktimedout = 1;
  1318.         if (dfanswer)        /* Supply default answer if any */
  1319.           s = dfanswer;
  1320.                 success = x = 0;    /* (was "x = -9;") */
  1321.             }
  1322.             timelimit = 0;
  1323.             return(x);                  /* and return cmtxt's error code. */
  1324.         }
  1325.         if (!s || *s == NUL) {        /* If user typed a bare CR, */
  1326.             cmsetp(psave);              /* Restore old prompt, */
  1327.             delmac(vnp,0);              /* delete variable if it exists, */
  1328. #ifdef VMS
  1329.             if (cmdlvl > 0)             /* In VMS and not at top level, */
  1330.               conres();                 /*  restore console again. */
  1331. #endif /* VMS */
  1332.             timelimit = 0;
  1333.             return(success = 1);        /* and return. */
  1334.         }
  1335.         y = addmac(vnp,s);              /* Add it to the macro table. */
  1336.         debug(F111,"ask addmac",vnp,y);
  1337.         cmsetp(psave);                  /* Restore old prompt. */
  1338. #ifdef VMS
  1339.         if (cmdlvl > 0)                 /* In VMS and not at top level, */
  1340.           conres();                     /*  restore console again. */
  1341. #endif /* VMS */
  1342.         timelimit = 0;
  1343.         return(success = (y < 0 ? 0 : 1) && (asktimedout == 0));
  1344.     }
  1345. }
  1346. #endif /* NOSPL */
  1347.  
  1348. #ifndef NOSPL
  1349. int
  1350. doincr(cx) int cx; {                    /* INCREMENT, DECREMENT */
  1351.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  1352.     int eval = 0;
  1353.     CK_OFF_T x;
  1354.     eval = (cx == XX_DECR || cx == XX_INCR);
  1355.  
  1356.     if ((y = cmfld("Variable name","",&s, eval ? xxstring : NULL)) < 0) {
  1357.         if (y == -3) {
  1358.             printf("?Variable name required\n");
  1359.             return(-9);
  1360.         } else return(y);
  1361.     }
  1362.     ckstrncpy(vnambuf,s,VNAML);
  1363.     if ((y = cmnumw("by amount","1",10,&x,xxstring)) < 0)
  1364.       return(y);
  1365.     if ((y = cmcfm()) < 0)
  1366.       return(y);
  1367.  
  1368.     z = (cx == XX_INCR || cx == XXINC) ? 1 : 0; /* Increment or decrement? */
  1369.  
  1370.     if (incvar(vnambuf,x,z) < 0) {
  1371.         printf("?Variable %s not defined or not numeric\n",vnambuf);
  1372.         return(success = 0);
  1373.     }
  1374.     return(success = 1);
  1375. }
  1376.  
  1377. /* Used by doundef() */
  1378. static int
  1379. xxundef(s,verbose,simulate) char * s; int verbose, simulate; {
  1380.     int rc = 0;
  1381.     if (!s) return(0);
  1382.     if (*s == CMDQ && *(s+1) == '%') {
  1383.         char c = *(s+2), * p = NULL;
  1384.         if (c >= '0' && c <= '9') {
  1385.             if (maclvl < 0)
  1386.               p = g_var[c];
  1387.             else
  1388.               p = m_arg[maclvl][c - '0'];
  1389.         } else {
  1390.             if (isupper(c)) c += ('a'-'A');
  1391.             if (c >= 'a' && c <= 'z')
  1392.               p = g_var[c];
  1393.         }
  1394.         if (!p) return(-1);
  1395.     }
  1396.     if (verbose)
  1397.       printf(" %s ",s);
  1398.     if (simulate) {
  1399.         printf("(SELECTED)\n");
  1400.     } else if ((x = delmac(s,1)) > -1) { /* Full name required */
  1401.         rc = 1;
  1402.         if (verbose) printf("(OK)\n");
  1403.     } else if (verbose)
  1404.       printf("(FAILED)\n");
  1405.     return(rc);
  1406. }
  1407.  
  1408. /* Do the (_)DEFINE, (_)ASSIGN, and UNDEFINE commands */
  1409.  
  1410. #define UND_MAT 1
  1411. #define UND_VRB 2
  1412. #define UND_EXC 3
  1413. #define UND_SIM 3
  1414.  
  1415. static struct keytab undefswi[] = {
  1416.     { "/list",     UND_VRB, 0 },
  1417. #ifdef COMMENT
  1418.     { "/except",   UND_EXC, CM_ARG },
  1419. #endif /* COMMENT */
  1420.     { "/matching", UND_MAT, 0 },
  1421.     { "/simulate", UND_SIM, 0 },
  1422.     { "/verbose",  UND_VRB, CM_INV }
  1423. };
  1424. static int nundefswi = sizeof(undefswi) / sizeof(struct keytab);
  1425.  
  1426. #define UNDEFMAX 64
  1427. static char ** undeflist = NULL;
  1428. int
  1429. doundef(cx) int cx; {                   /* UNDEF, _UNDEF */
  1430.     int i, j, n, rc = 0, arraymsg = 0;
  1431.     int domatch = 0, verbose = 0, errors = 0, simulate = 0, flag = 0;
  1432.     char *vnp, vnbuf[4];
  1433. #ifdef COMMENT
  1434.     char *except = NULL;
  1435. #endif /* COMMENT */
  1436.     struct FDB sw, fl;
  1437.     int getval;
  1438.     char c;
  1439.  
  1440.     if (!undeflist) {                   /* Allocate list if necessary */
  1441.         undeflist = (char **)malloc(UNDEFMAX * sizeof(char *));
  1442.         if (!undeflist) {
  1443.             printf("?Memory allocation failure\n");
  1444.             return(-9);
  1445.         }
  1446.         for (i = 0; i < UNDEFMAX; i++)
  1447.           undeflist[i] = NULL;
  1448.     }
  1449.     cmfdbi(&sw,                         /* First FDB - command switches */
  1450.            _CMKEY,                      /* fcode */
  1451.            "Variable name or switch",
  1452.            "",                          /* default */
  1453.            "",                          /* addtl string data */
  1454.            nundefswi,                   /* addtl numeric data 1: tbl size */
  1455.            4,                           /* addtl numeric data 2: 4 = cmswi */
  1456.            xxstring,                    /* Processing function */
  1457.            undefswi,                    /* Keyword table */
  1458.            &fl                          /* Pointer to next FDB */
  1459.            );
  1460.     cmfdbi(&fl,                         /* Anything that doesn't match */
  1461.            _CMFLD,                      /* fcode */
  1462.            "",                          /* hlpmsg */
  1463.            "",                          /* default */
  1464.            "",                          /* addtl string data */
  1465.            0,                           /* addtl numeric data 1 */
  1466.            0,                           /* addtl numeric data 2 */
  1467.            (cx == XXUNDEF) ? NULL : xxstring,
  1468.            NULL,
  1469.            NULL
  1470.            );
  1471.     while (1) {                         /* Parse 0 or more switches */
  1472.         x = cmfdb(&sw);                 /* Parse something */
  1473.         if (x < 0)
  1474.           return(x);
  1475.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  1476.           break;
  1477.         c = cmgbrk();
  1478.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  1479.             printf("?This switch does not take an argument\n");
  1480.             return(-9);
  1481.         }
  1482.         switch (cmresult.nresult) {
  1483.           case UND_MAT: domatch  = 1; break;
  1484.           case UND_SIM: simulate = 1; /* fall thru on purpose */
  1485.           case UND_VRB: verbose  = 1; break;
  1486.  
  1487. #ifdef COMMENT
  1488.           case UND_EXC:
  1489.             if (!getval) break;
  1490.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  1491.                 if (x == -3) {
  1492.                     printf("?Pattern required\n");
  1493.                     x = -9;
  1494.                 }
  1495.                 goto xgetx;
  1496.             }
  1497.             makestr(&except,cmresult.sresult);
  1498.             break;
  1499. #endif /* COMMENT */
  1500.  
  1501.           default:
  1502.             return(-2);
  1503.         }
  1504.     }
  1505.     n = 0;
  1506.     makestr(&(undeflist[n++]),cmresult.sresult);
  1507.     for (i = 1; i < UNDEFMAX; i++) {
  1508.         x = cmfld("Macro or variable name","",&s,
  1509.                   ((cx == XXUNDEF) ? NULL : xxstring)
  1510.                   );
  1511.         if (x == -3) {
  1512.             if ((y = cmcfm()) < 0)
  1513.               return(y);
  1514.             break;
  1515.         } else if (y < 0) {
  1516.             return(y);
  1517.         }
  1518.         makestr(&(undeflist[n++]),s);
  1519.     }
  1520.     /* Now we have a list of n variables or patterns to undefine */
  1521.  
  1522.     for (i = 0; i < n; i++) {
  1523.         flag = 0;
  1524.         if (!(vnp = undeflist[i]))
  1525.           continue;
  1526.         if (vnp[0] == CMDQ && (vnp[1] == '%' || vnp[1] == '&')) {
  1527.             flag++;
  1528.             vnp++;
  1529.         }
  1530.         if (!domatch) {                 /* Pattern match not requested */
  1531.             if (flag) {
  1532.                 if ((y = parsevar(vnp,&x,&z)) < 0) {
  1533.                     vnp--;
  1534.                     if (verbose) printf(" %s...error\n",vnp);
  1535.                     continue;
  1536.                 }
  1537.                 vnp--;
  1538.             }
  1539.             x = xxundef(vnp,verbose,simulate);
  1540.             if (x > -1) {
  1541.                 if (!x && !simulate) errors++;
  1542.                 rc += x;
  1543.             }
  1544.             continue;
  1545.         }
  1546.         /* Pattern match requested */
  1547.  
  1548.         if (!flag) {                    /* It's a macro */
  1549.             for (j = 0; j < nmac; j++) {
  1550.                 if (ckmatch(vnp,mactab[j].kwd,0,1)) {
  1551.                     x = xxundef(mactab[j].kwd,verbose,simulate);
  1552.                     if (x > -1) {
  1553.                         rc += x;
  1554.                         if (!x) errors++;
  1555.                     }
  1556.                     if (!simulate)
  1557.                       j--;              /* Because mactab shifted up */
  1558.                 }
  1559.             }
  1560.         } else if (vnp[0] == '%') {     /* It's a \%x variable */
  1561.             vnbuf[0] = CMDQ;
  1562.             vnbuf[1] = '%';
  1563.             vnbuf[3] = NUL;
  1564.             for (j = '0'; j <= 'z'; j++) { /* 0..9 a..z */
  1565.                 vnbuf[2] = j;
  1566.                 if (ckmatch(vnp,&vnbuf[1],0,1)) {
  1567.                     x = xxundef(vnbuf,verbose,simulate);
  1568.                     if (x > -1) {
  1569.                         if (!x) errors++;
  1570.                         rc += x;
  1571.                     }
  1572.                 }
  1573.                 if (j == '9') j = (int)'a' - 1; /* 9 -> a */
  1574.             }
  1575.         } else if (vnp[0] == '&') {
  1576.             if (!arraymsg && !quiet) {
  1577.                 printf("?UNDEFINE /MATCH can't be used with arrays.\n");
  1578.                 printf("(Type HELP ARRAY to see other methods.)\n");
  1579.             }
  1580.             arraymsg++;
  1581.             errors++;
  1582.         }
  1583.     }
  1584.     if (verbose)
  1585.       printf("undefined: %d, errors: %d\n",rc,errors);
  1586.  
  1587.     for (i = 0; i < UNDEFMAX; i++) {    /* Check them all */
  1588.         if (undeflist[i]) {             /* in case we were interrupted */
  1589.             free(undeflist[i]);         /* previously... */
  1590.             undeflist[i] = NULL;
  1591.         }
  1592.     }
  1593.     return(success = (errors == 0) ? 1 : 0);
  1594. }
  1595.  
  1596. int
  1597. dodef(cx) int cx; {
  1598.     extern int xxdot;
  1599.     extern char ppvnambuf[];
  1600.     int doeval = 0;
  1601.     char vnambuf[VNAML+1];              /* Buffer for variable names */
  1602.     char *vnp;                          /* Pointer to same */
  1603.     int k, mydot;
  1604.     mydot = xxdot;                      /* Copy */
  1605.     xxdot = 0;                          /* and reset */
  1606. /*
  1607.   In case we got here from a command that begins like ".\%a", cmkey() has
  1608.   already evaluated \%a, but we don't want that, so we retrieve the variable
  1609.   name from a special pre-evaluation buffer in the command module, and we
  1610.   undo the "unget word" that would be done because of the token, because if
  1611.   the variable was defined, it will unget its value rather than its name.
  1612. */
  1613.     s = NULL;
  1614.  
  1615.     if (mydot && ppvnambuf[0] == '.' && ppvnambuf[1]) {
  1616.         s = ppvnambuf+1;
  1617.         unungw();
  1618.     }
  1619.     if (!s) {
  1620.         if (cx == XXDFX || cx == XXASX)
  1621.           /* Evaluate variable name */
  1622.           y = cmfld("Macro or variable name","",&s,xxstring);
  1623.         else
  1624.           /* Don't evaluate the variable name */
  1625.           y = cmfld("Macro or variable name","",&s,NULL);
  1626.         if (y < 0) {
  1627.             if (y == -3) {
  1628.                 printf("?Variable name required\n");
  1629.                 return(-9);
  1630.             } else return(y);
  1631.         }
  1632.     }
  1633.     k = strlen(s);
  1634.     if (k > VNAML) {
  1635.         printf("?Name too long: \"%s\"\n",s);
  1636.         return(-9);
  1637.     }
  1638.     ckstrncpy(vnambuf,s,VNAML);
  1639.     vnambuf[VNAML] = NUL;
  1640.     vnp = vnambuf;
  1641.     if (vnambuf[0] == CMDQ && (vnambuf[1] == '%' || vnambuf[1] == '&')) vnp++;
  1642.     if (*vnp == '%' || *vnp == '&') {
  1643.         if ((y = parsevar(vnp,&x,&z)) < 0) return(y);
  1644. #ifdef COMMENT
  1645.         if (cx == XXUNDEF) {            /* Undefine */
  1646.             if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1647.             delmac(vnp,0);
  1648.             return(success = 1);
  1649.         }
  1650. #endif /* COMMENT */
  1651.         debug(F101,"dodef parsevar x","",x);
  1652.         if (mydot) {
  1653.             if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1654.               return(doeval);
  1655.             if (doeval > 0)             /* Type of assignment */
  1656.               cx = XXASS;
  1657.         }
  1658.         if (y == 1) {                   /* Simple variable */
  1659.             if ((y = cmtxt("Definition of variable","",&s,NULL)) < 0)
  1660.               return(y);
  1661.             s = brstrip(s);
  1662.             debug(F110,"xxdef var name",vnp,0);
  1663.             debug(F110,"xxdef var def",s,0);
  1664.         } else if (y == 2) {            /* Array element */
  1665.             if ((y = arraynam(vnp,&x,&z)) < 0) return(y);
  1666.             if (x == 96) {
  1667.                 printf("?Argument vector array is read-only\n");
  1668.                 return(-9);
  1669.             }
  1670.             if (chkarray(x,z) < 0) return(-2);
  1671.             if ((y = cmtxt("Definition of array element","",&s,NULL)) < 0)
  1672.               return(y);
  1673.             debug(F110,"xxdef array ref",vnp,0);
  1674.             debug(F110,"xxdef array def",s,0);
  1675.         }
  1676.     } else {                            /* Macro */
  1677. #ifdef COMMENT
  1678.         if (cx == XXUNDEF) {            /* Undefine */
  1679.             if ((y = cmtxt("Text to be ignored","",&s,NULL)) < 0) return(y);
  1680.             delmac(vnp,0);
  1681.             return(success = 1);
  1682.         }
  1683. #endif /* COMMENT */
  1684.         if (mydot) {
  1685.             if ((doeval = cmkey(asgtab,nasgtab,"operator","=",NULL)) < 0)
  1686.               return(doeval);
  1687.             if (doeval > 0)
  1688.               cx = XXASS;
  1689.         }
  1690.         if ((y = cmtxt("Definition of macro","",&s,NULL)) < 0) return(y);
  1691. #ifdef DEBUG
  1692.         if (deblog) {
  1693.             debug(F110,"xxdef macro name",vnp,0);
  1694.             debug(F010,"xxdef macro def",s,0);
  1695.         }
  1696. #endif /* DEBUG */
  1697.         s = brstrip(s);
  1698.     }
  1699.     if (*s == NUL) {                    /* No arg given, undefine */
  1700.         delmac(vnp,1);                  /* silently... */
  1701.         return(success = 1);            /* even if it doesn't exist... */
  1702.     }
  1703.     /* Defining a new macro or variable */
  1704.  
  1705.     if (cx == XXASS || cx == XXASX) {   /* ASSIGN rather than DEFINE? */
  1706.         int t;
  1707.         t = LINBUFSIZ-1;
  1708.         lp = line;                      /* If so, expand its value now */
  1709.         zzstring(s,&lp,&t);
  1710.         s = line;
  1711.     }
  1712.     if (doeval == 2) {                  /* Arithmetic evaluation wanted too? */
  1713.         ckstrncpy(line,evala(s),LINBUFSIZ);
  1714.         line[LINBUFSIZ] = NUL;
  1715.     }
  1716.     /* debug(F111,"calling addmac",s,(int)strlen(s)); */
  1717.  
  1718.     y = addmac(vnp,s);                  /* Add it to the appropriate table. */
  1719.     if (y < 0) {
  1720.         printf("?%s failed\n",(cx == XXASS || cx == XXASX) ?
  1721.                "ASSIGN" : "DEFINE");
  1722.         return(success = 0);
  1723.     } else if (cx == XXASX || cx == XXDFX) /* For _ASG or _DEF, */
  1724.       return(1);                           /* don't change success variable */
  1725.     else
  1726.       return(success = 1);
  1727. }
  1728. #endif /* NOSPL */
  1729.  
  1730.  
  1731. #ifndef NODIAL
  1732. /*
  1733.    L U D I A L  --  Lookup up dialing directory entry.
  1734.  
  1735.    Call with string to look up and file descriptor of open dialing directory
  1736.    file.  On success, returns number of matches found, with numbers stored
  1737.    in an array accessible via getdnum().
  1738. */
  1739. static char *dn_p[MAXDNUMS + 1];        /* Dial Number pointers */
  1740. static char *dn_p2[MAXDNUMS + 1];       /* Converted dial number pointers */
  1741. static int dn_x[MAXDNUMS + 1];          /* Type of call */
  1742. static int dncount = 0;
  1743. char * d_name = NULL;                   /* Dial name pointer */
  1744.  
  1745. char *                                  /* Get dial directory entry name */
  1746. getdname() {
  1747.     return(d_name ? d_name : "");
  1748. }
  1749.  
  1750. char *
  1751. getdnum(n) int n; {                     /* Get dial number n from directory */
  1752.     if (n < 0 || n > dncount || n > MAXDNUMS)
  1753.       return("");
  1754.     else
  1755.       return(dn_p[n]);
  1756. }
  1757.  
  1758. char *                  /* Check area code for spurious leading digit */
  1759. chk_ac(i,buf) int i; char buf[]; {
  1760.     char *p;
  1761.     if (!buf)
  1762.       return("");
  1763.     p = (char *) buf;                   /* Country we are calling: */
  1764.     if (i ==  44 ||                     /* UK */
  1765.         i ==  49 ||                     /* Germany */
  1766.         i ==  39 ||                     /* Italy */
  1767.         i ==  31 ||                     /* Netherlands */
  1768.         i == 351 ||                     /* Portugal */
  1769.         i ==  55 ||                     /* Brazil */
  1770.         i == 972 ||                     /* Israel */
  1771.         i ==  41 ||                     /* Switzerland */
  1772.         i ==  43 ||                     /* Austria */
  1773.         i ==  42 ||                     /* Czech Republic */
  1774.         i ==  36 ||                     /* Hungary */
  1775.         i ==  30 ||                     /* Greece */
  1776.         i == 352 ||                     /* Luxembourg */
  1777.         i ==  48 ||                     /* Poland */
  1778.         i ==  27 ||                     /* South Africa */
  1779.         i ==  33 ||                     /* France (as of 1997) */
  1780.         i ==  358                       /* Finland (ditto) */
  1781.         ) {
  1782.         if (buf[0] == '0')
  1783.           p++;
  1784.     }
  1785.     return(p);
  1786. }
  1787.  
  1788. /* Call Is Long Distance -- Expand this to cover 10-digit local dialing etc */
  1789. /*
  1790.    src  = area code of caller
  1791.    dest = area code of callee
  1792.    Returns:
  1793.      0 if call is local
  1794.      1 if call is long distance
  1795.      2 if call is local but area code must be dialed anyway
  1796. */
  1797. static int
  1798. callisld(src, dest) char * src, * dest; {
  1799.     int i;
  1800.     if (dialfld)                        /* Force long distance? */
  1801.       return(1);
  1802.     if (!strcmp(src,dest)) {            /* Area codes are the same */
  1803.         for (i = 0; i < nlocalac; i++)  /* Is AC in the lc-area-codes list? */
  1804.           if (!strcmp(src,diallcac[i]))
  1805.             return(2);                  /* Yes so must be dialed */
  1806.         return(0);                      /* No so don't dial it. */
  1807.     }
  1808.     for (i = 0; i < nlocalac; i++)      /* ACs not the same so look in list */
  1809.       if (!strcmp(dest,diallcac[i]))    /* Match */
  1810.         return(2);                      /* So local call with area code */
  1811.     return(1);                          /* Not local so long-distance */
  1812. }
  1813.  
  1814. char pdsfx[64] = { NUL, NUL };
  1815.  
  1816. #ifndef NOSPL
  1817. static char *
  1818. xdial(s) char *s; {                     /* Run dial string thru macro */
  1819.     int x, m;
  1820.     char * s2;
  1821.     s2 = NULL;
  1822.     makestr(&s2,s);            /* Copy the argument */
  1823.     if (!dialmac)                       /* Dial macro name given? */
  1824.       return(NULL);
  1825.     if ((x = mxlook(mactab,dialmac,nmac)) < 0) /* Is the macro defined? */
  1826.       return(NULL);
  1827.     m = maclvl;
  1828.     x = dodo(x,s2,0);            /* Set up the macro */
  1829.     if (s2) free(s2);
  1830.     if (x > 0) {
  1831.         while (maclvl > m)              /* Execute the parser */
  1832.           parser(1);
  1833.         return(mrval[maclvl+1]);        /* Return the result */
  1834.     }
  1835.     return(NULL);
  1836. }
  1837. #endif /* NOSPL */
  1838.  
  1839. static int
  1840. dncvt(k,cx, prefix, suffix)
  1841.     int k, cx, prefix, suffix; {        /* Dial Number Convert */
  1842.     int i, j, n, what;                  /* cx is top-level command index */
  1843.     char *ss;                           /* prefix - add prefixes? */
  1844.     char *p, *p2, *pxo;                 /* suffix - add suffixes? */
  1845.     char *lac;
  1846.     char *npr;
  1847.     char *sfx;
  1848.     /* char *psfx; */
  1849.     char ccbuf[128];
  1850.     int cc;
  1851.     char acbuf[24];
  1852.     char *acptr;
  1853.     char outbuf[256];
  1854. /*
  1855.   First pass for strict (punctuation-based) interpretation.
  1856.   If it fails, we try the looser (length-based) one.
  1857. */
  1858.     dialtype = -1;
  1859.     what = 0;                           /* Type of call */
  1860.     s = dn_p[k];                        /* Number to be converted. */
  1861.     debug(F111,"dncvt",s,k);
  1862.     if (dn_p2[k]) {
  1863.         free(dn_p2[k]);
  1864.         dn_p2[k] = NULL;
  1865.     }
  1866.     if (!s) {
  1867.         printf("Error - No phone number to convert\n");
  1868.         return(-1);
  1869.     }
  1870.     if ((int)strlen(s) > 200) {
  1871.         ckstrncpy(outbuf,s,40);
  1872.         printf("?Too long: \"%s...\"\n",outbuf);
  1873.         return(-1);
  1874.     }
  1875.     npr = (prefix && dialnpr) ? dialnpr : "";
  1876.     sfx = (suffix && dialsfx) ? dialsfx : "";
  1877.     /* if (partial) psfx = dialsfx ? dialsfx : ""; */
  1878.     pxo = (prefix && dialpxo) ? dialpxo : "";
  1879.     lac = diallac ? diallac : "";       /* Local area code */
  1880.  
  1881.     outbuf[0] = NUL;                    /* Initialize conversion buffer */
  1882.     ss = s;                             /* Remember original string */
  1883.  
  1884.     if (*s != '+') {                    /* Literal number */
  1885.         dn_x[k] = DN_UNK;               /* Sort key is "unknown". */
  1886.         ckmakmsg(outbuf,256,            /* Sandwich it between */
  1887.                  pxo,npr,s,sfx          /* DIAL PREFIX and SUFFIX */
  1888.                 );
  1889. #ifdef CK_TAPI
  1890.         if (tttapi &&                   /* TAPI does its own conversions */
  1891.             !tapipass &&                /* if not in passthru mode */
  1892.             tapiconv == CK_AUTO ||      /* and TAPI conversions are AUTO */
  1893.             tapiconv == CK_ON           /* OR if TAPI conversions are ON */
  1894.             ) {
  1895.             char * p = NULL;
  1896.             dialtype = -2;
  1897.             if (!cktapiConvertPhoneNumber(dn_p[k], &p))
  1898.               return(-1);
  1899.             makestr(&dn_p2[k], p);
  1900.             if (p) free(p);
  1901.             return(0);
  1902.         } else
  1903. #endif /* CK_TAPI */
  1904.           makestr(&dn_p2[k], outbuf);   /* Not TAPI */
  1905.         dialtype = what;
  1906.         return(0);                      /* Done. */
  1907.     }
  1908.     i = 0;                              /* Portable number */
  1909.     s++;                                /* Tiptoe past the plus sign */
  1910.     ccbuf[0] = NUL;                     /* Do country code first */
  1911.  
  1912.     if (!diallcc) {                     /* Do we know our own? */
  1913.         if (cx != XXLOOK)
  1914.           printf("Error - prior SET DIAL COUNTRY-CODE command required\n");
  1915.         return(-1);
  1916.     }
  1917.  
  1918.     /* Parse the number */
  1919.  
  1920.     while (1) {                         /* Get the country code */
  1921.         while (*s == HT || *s == SP)
  1922.           s++;
  1923.         if (!s)                         /* Not in standard format */
  1924.           break;
  1925.         if (*s == '(') {                /* Beginning of area code  */
  1926.             s++;                        /* Skip past parenthesis   */
  1927.             ccbuf[i] = NUL;             /* End of country code */
  1928.             if (!s) {                   /* Check for end of string */
  1929.                 printf("Error - phone number ends prematurely: \"%s\"\n",ss);
  1930.                 return(-1);
  1931.             }
  1932.             break;
  1933.         } else {                        /* Collect country code */
  1934.             if (isdigit(*s))
  1935.               ccbuf[i++] = *s;          /* copy this character */
  1936.             s++;
  1937.             if (!*s || i > 127)         /* watch out for memory leak */
  1938.               break;
  1939.         }
  1940.     }
  1941.     cc = atoi(ccbuf);                   /* Numeric version of country code */
  1942.  
  1943.     i = 0;                              /* Now get area code */
  1944.     acbuf[0] = NUL;                     /* Initialize area-code buffer */
  1945.     acptr = acbuf;                      /* and pointer. */
  1946.     while (1) {
  1947.         while (*s == HT || *s == SP)    /* Ignore whitespace */
  1948.           s++;
  1949.         if (!s)                         /* String finished */
  1950.           break;
  1951.         if (*s == ')') {                /* End of area code  */
  1952.             s++;                        /* Skip past parenthesis   */
  1953.             acbuf[i] = NUL;             /* Terminate area-code buffer */
  1954.             break;
  1955.         } else {                        /* Part of area code */
  1956.             if (isdigit(*s))            /* If it's a digit, */
  1957.               acbuf[i++] = *s;          /* copy this character */
  1958.             s++;                        /* Point to next */
  1959.             if (!*s || i > 23)          /* Watch out for overflow */
  1960.               break;
  1961.         }
  1962.     }
  1963.  
  1964. /*
  1965.    Here we strip any leading 0 for countries that we know have
  1966.    0 as a long-distance prefix and do not have any area codes that
  1967.    start with 0 (formerly also ditto for "9" in Finland...)
  1968. */
  1969.     i = atoi(ccbuf);
  1970.     acptr = chk_ac(i,acbuf);
  1971.  
  1972.     while (*s == HT || *s == SP)        /* Skip whitespace */
  1973.       s++;
  1974.  
  1975. /* printf("S=[%s], ACPTR=[%s]\n",s,acptr); */
  1976.  
  1977.     if (*s && *acptr) {                 /* Area code was delimited */
  1978.  
  1979.         while (*s == '-' || *s == '.')  /* Skip past gratuitious punctuation */
  1980.           s++;
  1981.         if (!*s) s--;                   /* But not to end of string */
  1982.  
  1983.         if (strcmp(diallcc,ccbuf)) {    /* Out of country? */
  1984.             if (!dialixp) {             /* Need intl-prefix */
  1985.                 if (cx != XXLOOK)
  1986.                   printf("Error - No international dialing prefix defined\n");
  1987.                 return(-1);
  1988.             }
  1989.             what = dn_x[k] = DN_INTL;
  1990.             p  = (prefix && dialixp) ? dialixp : ""; /* Intl-prefix */
  1991.             p2 = (suffix && dialixs) ? dialixs : ""; /* Intl-suffix */
  1992.  
  1993.             /* Form the final phone number */
  1994. #ifdef COMMENT
  1995.             sprintf(pdsfx,"%s%s",p2,sfx); /* UNSAFE */
  1996.             sprintf(outbuf,
  1997.                     "%s%s%s%s%s%s%s%s",
  1998.                     pxo,npr,p,ccbuf,acptr,s,p2,sfx
  1999.                     );
  2000. #else
  2001.             ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2002.             ckmakxmsg(outbuf,256,pxo,npr,p,ccbuf,acptr,s,p2,sfx,
  2003.                       NULL,NULL,NULL,NULL);
  2004. #endif /* COMMENT */
  2005.  
  2006.         } else if ((x = callisld(lac,acptr)) >= 1) { /* In-country LD */
  2007.             if (!diallac && cx != XXLOOK) { /* Don't know my own area code */
  2008.                 if (cc == 1)
  2009.                   printf("WARNING - Prior SET DIAL AREA-CODE needed\n");
  2010.             }
  2011.             if (x == 2) {               /* Local call with area code */
  2012.                 what = dn_x[k] = DN_LOCAL;      /* Local-call */
  2013.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  2014.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  2015.             } else {
  2016.                 what = dn_x[k] = DN_LONG;       /* Long-distance */
  2017.                 for (i = 0; i < ntollfree; i++) { /* But toll-free too? */
  2018.                     if (!strcmp(acptr,dialtfc[i])) {
  2019.                         what = dn_x[k] = DN_FREE;
  2020.                         break;
  2021.                     }
  2022.                 }
  2023.                 if (what == DN_FREE) {  /* Toll-free call */
  2024.                     p = (prefix && dialtfp) ? dialtfp :
  2025.                         ((prefix && dialldp) ? dialldp : "");
  2026.                     p2 = "";            /* no suffix */
  2027.                 } else {                        /* normal long distance */
  2028.                     p  = (prefix && dialldp) ? dialldp : ""; /* ld-prefix */
  2029.                     p2 = (suffix && diallds) ? diallds : ""; /* ld-suffix */
  2030.                 }
  2031.             }
  2032.             /* Form the number to be dialed */
  2033. #ifdef COMMENT
  2034.             sprintf(outbuf,"%s%s%s%s%s%s%s",
  2035.                     pxo,npr,p,acptr,s,p2,sfx
  2036.                     );
  2037.             sprintf(pdsfx,"%s%s",p2,sfx);
  2038. #else
  2039.             ckmakxmsg(outbuf,256,
  2040.                       pxo,npr,p,acptr,s,p2,sfx,
  2041.                       NULL,NULL,NULL,NULL,NULL);
  2042.             ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2043. #endif /* COMMENT */
  2044.         } else {                        /* Same country, same area code */
  2045.             what = dn_x[k] = DN_LOCAL;  /* So it's a local call. */
  2046.             if (!prefix || !(dialpxo || ndialpxx)) { /* Not dialing from PBX */
  2047.                 p  = (prefix && diallcp) ? diallcp : ""; /* local-prefix */
  2048.                 p2 = (suffix && diallcs) ? diallcs : ""; /* local-suffix */
  2049. #ifdef COMMENT
  2050.                 if (x == 2)
  2051.                   sprintf(outbuf,"%s%s%s%s%s%s",npr,p,acptr,s,p2,sfx);
  2052.                 else
  2053.                   sprintf(outbuf,"%s%s%s%s%s",npr,p,s,p2,sfx);
  2054.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2055. #else
  2056.                 if (x == 2)
  2057.                   ckmakxmsg(outbuf,256,
  2058.                             npr,p,acptr,s,p2,sfx,
  2059.                             NULL,NULL,NULL,NULL,NULL,NULL);
  2060.                 else
  2061.                   ckmakxmsg(outbuf,256,
  2062.                             npr,p,s,p2,sfx,
  2063.                             NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  2064.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2065. #endif /* COMMENT */
  2066.  
  2067.             } else {                    /* Dialing from a PBX and not TAPI */
  2068.                 if (ndialpxx) {         /* Is it internal? */
  2069. #ifdef COMMENT
  2070.                     i = (int) strlen(dialpxx);
  2071.                     j = (int) strlen(s);
  2072.                     x = -1;
  2073.                     if (j > i)
  2074.                       x = ckstrcmp(dialpxx,s,i,0);
  2075. #else
  2076.                     int kx;
  2077.                     x = -1;
  2078.                     j = (int) strlen(s);
  2079.                     for (kx = 0; kx < ndialpxx; kx++) {
  2080.                         i = (int) strlen(dialpxx[kx]);
  2081.                         if (j > i)
  2082.                           if (!(x = ckstrcmp(dialpxx[kx],s,i,0)))
  2083.                             break;
  2084.                     }
  2085. #endif /* COMMENT */
  2086.                     if (!x) {
  2087.                         char * icp, buf[32];
  2088.                         makestr(&matchpxx,dialpxx[kx]);
  2089.                         debug(F111,"dncvt matchpxx",matchpxx,kx);
  2090.                         what = dn_x[kx] = DN_INTERN;   /* Internal call. */
  2091.                         s += i;
  2092.                         /* Internal-call prefix */
  2093.                         icp = dialpxi;
  2094. #ifndef NOSPL
  2095.                         if (icp) {
  2096.                             if (*icp == '\\') {
  2097.                                 char c, *bp;
  2098.                                 int n;
  2099.                                 c = *(icp+1);
  2100.                                 if (isupper(c)) c = tolower(c);
  2101.                                 if (c == 'v' || c == 'f') {
  2102.                                     n = 32;
  2103.                                     bp = buf;
  2104.                                     zzstring(icp,&bp,&n);
  2105.                                     icp = buf;
  2106.                                 }
  2107.                             }
  2108.                         }
  2109. #endif /* NOSPL */
  2110.                         p = (prefix && icp) ? icp : "";
  2111. #ifdef COMMENT
  2112.                         sprintf(outbuf,"%s%s%s%s",npr,p,s,sfx);
  2113. #else
  2114.                         ckmakmsg(outbuf,256,npr,p,s,sfx);
  2115. #endif /* COMMENT */
  2116.                     } else {            /* External local call */
  2117.                         /* local-prefix */
  2118.                         p  = (prefix && diallcp) ? diallcp : "";
  2119.                         /* local-suffix */
  2120.                         p2 = (prefix && diallcs) ? diallcs : "";
  2121. #ifdef COMMENT
  2122.                         if (x == 2)
  2123.                           sprintf(outbuf,"%s%s%s%s%s%s%s",
  2124.                                   dialpxo ? dialpxo : "",
  2125.                                   npr,p,acptr,s,p2,sfx);
  2126.                         else
  2127.                           sprintf(outbuf,
  2128.                                   "%s%s%s%s%s%s",
  2129.                                   dialpxo ? dialpxo : "",
  2130.                                   npr,p,s,p2,sfx
  2131.                                   );
  2132. #else
  2133.                         if (x == 2)
  2134.                           ckmakxmsg(outbuf, 256,
  2135.                                    dialpxo ? dialpxo : "",
  2136.                                    npr,p,acptr,s,p2,sfx,
  2137.                                    NULL,NULL,NULL,NULL,NULL);
  2138.                         else
  2139.                           ckmakxmsg(outbuf, 256,
  2140.                                     dialpxo ? dialpxo : "",
  2141.                                     npr,p,s,p2,sfx,
  2142.                                     NULL,NULL,NULL,NULL,NULL,NULL);
  2143. #endif /* COMMENT */
  2144.                     }
  2145.                 }
  2146.             }
  2147.         }
  2148.  
  2149.     } else {                            /* Area code was not delimited */
  2150.  
  2151.         char xbuf[256];                 /* Comparison based only on length */
  2152.         char ybuf[256];
  2153.         int x, j;
  2154.  
  2155.         s = ss;
  2156.  
  2157.         for (i = 0; i < 255; i++) {
  2158.             if (!*s) break;
  2159.             while (!isdigit(*s)) {      /* Pay attention only to digits */
  2160.                 s++;
  2161.                 if (!*s) break;
  2162.             }
  2163.             xbuf[i] = *s++;
  2164.         }
  2165.         xbuf[i] = NUL;
  2166.  
  2167.         x = 1;                          /* Assume LD */
  2168.         n = 0;
  2169.         if (!dialfld) {                 /* If LD not forced */
  2170.             for (j = 0; j < nlocalac; j++) { /* check local AC list? */
  2171.                 ckmakmsg(ybuf,256,diallcc,diallcac[j],NULL,NULL);
  2172.                 n = (int) strlen(ybuf);
  2173.                 if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) {
  2174.                     x = 2;
  2175.                     break;
  2176.                 }
  2177.             }
  2178.             if (x == 1) {               /* Or exact match with local CC+AC? */
  2179.                 ckmakmsg(ybuf,256,diallcc,lac,NULL,NULL);
  2180.                 n = (int) strlen(ybuf);
  2181.                 if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0))
  2182.                   x = 0;
  2183.             }
  2184.         }
  2185.         if (x == 0 || x == 2) {         /* Local call */
  2186.             int xx,kx;                  /* Begin 1 Dec 2001... */
  2187.             /* Account for PBX internal calls */
  2188.             if (ndialpxx) {
  2189.                 xx = -1;
  2190.                 j = (int) strlen(ybuf);
  2191.                 for (kx = 0; kx < ndialpxx; kx++) {
  2192.                     i = (int) strlen(dialpxx[kx]);
  2193.                     if (j >= i)
  2194.                       if (!(xx = ckstrcmp(dialpxx[kx],&xbuf[j],i,0)))
  2195.                         break;
  2196.                 }
  2197.             }
  2198.             if (!xx) {
  2199.                 char * icp, buf[32];
  2200.                 makestr(&matchpxx,dialpxx[kx]);
  2201.                 debug(F111,"dncvt matchpxx",matchpxx,kx);
  2202.                 what = dn_x[kx] = DN_INTERN; /* Internal call. */
  2203.                 s = xbuf + j + i;
  2204.                 icp = dialpxi;          /* Internal-call prefix */
  2205. #ifndef NOSPL
  2206.                 if (icp) {
  2207.                     if (*icp == '\\') {
  2208.                         char c, *bp;
  2209.                         int n;
  2210.                         c = *(icp+1);
  2211.                         if (isupper(c)) c = tolower(c);
  2212.                         if (c == 'v' || c == 'f') {
  2213.                             n = 32;
  2214.                             bp = buf;
  2215.                             zzstring(icp,&bp,&n);
  2216.                             icp = buf;
  2217.                         }
  2218.                     }
  2219.                 }
  2220. #endif /* NOSPL */
  2221.                 p = (prefix && icp) ? icp : "";
  2222.                 ckmakmsg(outbuf,256,npr,p,s,sfx);
  2223.                 /* End 1 Dec 2001... */
  2224.  
  2225.             } else {                    /* Not PBX internal */
  2226.  
  2227.                 dn_x[k] = DN_LOCAL;
  2228.                 p = (prefix && diallcp) ? diallcp : "";
  2229.                 p2 = (suffix && diallcs) ? diallcs : "";
  2230.                 s = (char *) (xbuf + ((x == 0) ? n : (int)strlen(diallcc)));
  2231.                 ckmakxmsg(outbuf,256,
  2232.                           pxo,npr,p,s,p2,sfx,
  2233.                           NULL,NULL,NULL,NULL,NULL,NULL);
  2234.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2235.             }
  2236.         } else {                        /* Not local */
  2237.             n = ckstrncpy(ybuf,diallcc,256);
  2238.             if (n > 0 && !ckstrcmp(xbuf,ybuf,n,0)) { /* Long distance */
  2239.                 dn_x[k] = DN_LONG;
  2240.                 p = (prefix && dialldp) ? dialldp : "";
  2241.                 p2 = (suffix && diallds) ? diallds : "";
  2242.                 s = xbuf + n;
  2243.                 while (*s == '-' || *s == '.')
  2244.                   s++;
  2245. #ifdef COMMENT
  2246.                 sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,s,p2,sfx);
  2247.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2248. #else
  2249.                 ckmakxmsg(outbuf,256,
  2250.                           pxo,npr,p,s,p2,sfx,
  2251.                          NULL,NULL,NULL,NULL,NULL,NULL);
  2252.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2253. #endif /* COMMENT */
  2254.             } else {
  2255.                 dn_x[k] = DN_INTL;      /* International */
  2256.                 if (!dialixp) {
  2257.                     if (cx != XXLOOK) {
  2258.                         printf(
  2259.                           "Error - No international dialing prefix defined\n"
  2260.                                );
  2261.                         return(-1);
  2262.                     }
  2263.                 }
  2264.                 p = (prefix && dialixp) ? dialixp : "";
  2265.                 p2 = (suffix && dialixs) ? dialixs : "";
  2266. #ifdef COMMENT
  2267.                 sprintf(outbuf,"%s%s%s%s%s%s",pxo,npr,p,xbuf,p2,sfx);
  2268.                 sprintf(pdsfx,"%s%s",p2,sfx);
  2269. #else
  2270.                 ckmakxmsg(outbuf,256,
  2271.                           pxo,npr,p,xbuf,p2,sfx,
  2272.                           NULL,NULL,NULL,NULL,NULL,NULL);
  2273.                 ckmakmsg(pdsfx,64,p2,sfx,NULL,NULL);
  2274. #endif /* COMMENT */
  2275.             }
  2276.         }
  2277.     }
  2278. #ifdef CK_TAPI
  2279.     if (tttapi &&                       /* TAPI performs the conversions */
  2280.         !tapipass &&
  2281.         tapiconv == CK_AUTO ||
  2282.         tapiconv == CK_ON
  2283.         ) {
  2284.         p = NULL;
  2285.         dialtype = -2;
  2286.         if (!cktapiConvertPhoneNumber(dn_p[k],&p))
  2287.           return(-1);
  2288.         makestr(&dn_p2[k], p);
  2289.         if (p) free(p);
  2290.         return(0);
  2291.     } else {
  2292. #endif /* CK_TAPI */
  2293.         makestr(&dn_p2[k], outbuf);
  2294. #ifdef CK_TAPI
  2295.     }
  2296. #endif /* CK_TAPI */
  2297.     dialtype = what;
  2298.     return(0);
  2299. }
  2300.  
  2301. static int
  2302. ddcvt(s, f, n) char * s; FILE * f; int n; { /* Dial Directory Convert */
  2303.     char linebuf[1024], *s2;            /* Buffers and pointers */
  2304. #ifdef VMS
  2305.     char * temp = NULL;
  2306. #endif /* VMS */
  2307.     char *info[8];                      /* Pointers to words from entry */
  2308.     FILE * f2 = NULL;
  2309.     int x, rc;
  2310.     rc = -1;
  2311.  
  2312.     debug(F110,"ddcvt file",s,0);
  2313.  
  2314.     if (!s || !f)                       /* No filename or file */
  2315.       return(-1);
  2316.     if (!*s)
  2317.  
  2318.     fclose(f);
  2319.     znewn(s,&s2);                       /* s2 = address of static buffer */
  2320.     debug(F110,"ddcvt newname",s2,0);
  2321.  
  2322. #ifdef VMS
  2323.     /* In VMS, znewn() returns the same file name with a new version number */
  2324.     makestr(&temp,s);                   /* Swap - otherwise the new */
  2325.     s = s2;                             /* version has the older version */
  2326.     s2 = temp;                          /* number... */
  2327.     debug(F110,"ddcvt after swap s",s,0);
  2328.     debug(F110,"ddcvt after swap s2",s2,0);
  2329.     makestr(&(dialdir[n]),s);           /* New file gets new version number */
  2330.     debug(F110,"ddcvt after makestr s2",s2,0);
  2331.     debug(F111,"ddcvt dialdir[n]",dialdir[n],n);
  2332. #else
  2333.     if (zrename(s,s2) < 0) {            /* Not VMS - rename old file */
  2334.         perror(s2);                     /* to new (wierd) name. */
  2335.         goto ddexit;
  2336.     }
  2337. #endif /* VMS */
  2338.     debug(F110,"ddcvt s2 (old)",s2,0);
  2339.     if ((f = fopen(s2,"r")) == NULL) {  /* Reopen old file with wierd name */
  2340.         debug(F110,"ddcvt s2 open error",ck_errstr(),0);
  2341.         dirline = 0;                    /* (or in VMS, old version) */
  2342.         perror(s2);
  2343.         goto ddexit;
  2344.     }
  2345.     debug(F110,"ddcvt fopen(s2) OK",s2,0);
  2346.  
  2347.     debug(F110,"ddcvt s (new)",s,0);
  2348.     if ((f2 = fopen(s,"w")) == NULL) {  /* Create new file with old name */
  2349.         debug(F110,"ddcvt s open error",ck_errstr(),0);
  2350.         perror(s);                      /* (or in VMS, new version) */
  2351.         goto ddexit;
  2352.     }
  2353.     debug(F110,"ddcvt fopen(s) OK",s,0);
  2354.  
  2355.     printf("\nSaving old directory as %s.\nConverting %s...",s2,s);
  2356.     fprintf(f2,"; %s - Kermit dialing directory\n", s);
  2357.     fprintf(f2,"%-16s %-20s ; %5s %-6s ; %s\n",
  2358.                "; Name","Number","Speed","Parity","Comment"
  2359.                );
  2360.  
  2361.     while (1) {
  2362.         linebuf[0] = NUL;               /* Read a line */
  2363.         if (fgets(linebuf,1023,f) == NULL)
  2364.           break;
  2365.         debug(F110,"ddcvt linebuf",linebuf,0);
  2366.         if (!linebuf[0]) {              /* Empty line */
  2367.             fprintf(f2,"\n");
  2368.             continue;
  2369.         }
  2370.         x = (int) strlen(linebuf);      /* Strip line terminator, */
  2371.         while (x-- > 0) {               /* if any. */
  2372.             if (linebuf[x] <= SP)
  2373.               linebuf[x] = NUL;
  2374.             else
  2375.               break;
  2376.         }
  2377.         xwords(linebuf,5,info,1);       /* Parse it the old way */
  2378.         for (x = 1; x < 6; x++)
  2379.           if (!info[x]) info[x] = "";
  2380.         fprintf(f2,"%-16s %-20s ; %5s %-6s %s\n",
  2381.                info[1],info[2],info[3],info[4],info[5]
  2382.                );
  2383.     }
  2384.     printf(" OK\n\n");
  2385.     rc = 0;                             /* Success */
  2386.   ddexit:
  2387.     if (f) fclose(f);
  2388.     if (f2) fclose(f2);
  2389. #ifdef VMS
  2390.     if (temp) free(temp);
  2391. #endif /* VMS */
  2392.     return(rc);
  2393. }
  2394.  
  2395. int                                     /* s = name to look up   */
  2396. #ifdef CK_ANSIC                         /* cx = index of command */
  2397. ludial(char *s, int cx)                 /* (DIAL, LOOKUP, etc)   */
  2398. #else
  2399. ludial(s, cx) char *s; int cx;
  2400. #endif /* CK_ANSIC */
  2401. /* ludial */ {
  2402.  
  2403.     int dd, n1, n2, n3, i, j, t;        /* Workers */
  2404.     int olddir, newdir, oldentry, newentry;
  2405.     int pass = 0;
  2406.     int oldflg = 0;
  2407.     int ambiguous = 0;                  /* Flag for lookup was ambiguous */
  2408.     char *info[7];                      /* Pointers to words from entry */
  2409.     char *pp;                           /* Pointer to element of array */
  2410.     FILE * f;
  2411.     char *line;                         /* File input buffer */
  2412.  
  2413. /* #define LUDEBUG */
  2414.  
  2415. #ifdef LUDEBUG
  2416. int zz = 1;
  2417. #endif /* LUDEBUG */
  2418.  
  2419.     if (!s || ndialdir < 1)             /* Validate arguments */
  2420.       return(-1);
  2421.  
  2422.     if ((n1 = (int) strlen(s)) < 1)     /* Length of string to look up */
  2423.       return(-1);
  2424.  
  2425.     if (!(line = malloc(1024)))         /* Allocate input buffer */
  2426.       return(-1);
  2427.  
  2428. #ifdef LUDEBUG
  2429. if (zz) printf("LUDIAL 1 s[%s], n1=%d\n",s,n1);
  2430. #endif /* LUDEBUG */
  2431.  
  2432.     pass = 0;
  2433.   lu_again:
  2434.     f = NULL;                           /* Dial directory file descriptor */
  2435.     t = dncount = 0;                    /* Dial-number match count */
  2436.     dd = 0;                             /* Directory counter */
  2437.     olddir = 0;
  2438.     newdir = 0;
  2439. /*
  2440.   We need to recognize both old- and new-style directories.
  2441.   But we can't allow old-style and new-style entries in the same
  2442.   directory because there is no way to tell for sure the difference between
  2443.   an old-style entry like this:
  2444.  
  2445.     foo  5551212  9600
  2446.  
  2447.   and a new-style literal entry like this:
  2448.  
  2449.     foo  555 9600
  2450.  
  2451.   I.e. is the "9600" a speed, or part of the phone number?
  2452. */
  2453.     while (1) {                         /* We make one pass */
  2454.         if (!f) {                       /* Directory not open */
  2455.             if (dd >= ndialdir)         /* No directories left? */
  2456.               break;                    /* Done. */
  2457.             debug(F111,"ludial dialdir[dd]",dialdir[dd],dd);
  2458.             if ((f = fopen(dialdir[dd],"r")) == NULL) { /* Open it */
  2459.                 perror(dialdir[dd]);    /* Can't, print message saying why */
  2460.                 if (line) {
  2461.                     free(line);
  2462.                     line = NULL;
  2463.                 }
  2464.                 dd++;                   /* Go on to next one, if any... */
  2465.                 continue;
  2466.             }
  2467.             dirline = 0;                /* Directory file line number */
  2468.             if (dialdpy && !pass)
  2469.               printf("Opening: %s...\n",dialdir[dd]);
  2470.             dd++;
  2471.             if (!oldflg) olddir = 0;
  2472.             newdir = 0;
  2473.         }
  2474.         oldentry = 0;
  2475.         newentry = 0;
  2476.         line[0] = NUL;
  2477.         if (getnct(line,1023,f,1) < 0) { /* Read a line */
  2478.             if (f) {                    /* f can be clobbered! */
  2479.                 fclose(f);              /* Close the file */
  2480.                 f = NULL;               /* Indicate next one needs opening */
  2481.                 oldflg = 0;
  2482.             }
  2483.             continue;
  2484.         }
  2485.         if (!line[0])                   /* Empty line */
  2486.           continue;
  2487. #ifdef LUDEBUG
  2488. if (zz) printf("LUDIAL 2 s[%s]\n",s);
  2489. #endif /* LUDEBUG */
  2490.  
  2491.         /* Make a copy and parse it the old way */
  2492.         /* A copy is needed because xwords() pokes NULs into the string */
  2493.  
  2494.         if ((pp = malloc((int)strlen(line) + 1))) {
  2495.             strcpy(pp,line);            /* safe */
  2496.             xwords(pp,5,info,0);        /* Parse it the old way */
  2497.  
  2498. #ifdef LUDEBUG
  2499. if (zz) printf("LUDIAL 3 s[%s]\n",s);
  2500. #endif /* LUDEBUG */
  2501.  
  2502.             if (!info[1])
  2503.               continue;
  2504.             if (*info[1] == ';') {      /* If full-line comment, */
  2505.                 newdir = 1;             /* (only new directories have them) */
  2506.                 continue;               /* keep reading. */
  2507.             }
  2508.             if (!info[2])
  2509.               continue;
  2510.             if (*info[2] == '+')
  2511.               newentry = 1;
  2512.             if (info[4]) {
  2513.                 if ((*info[4] == '=') ||
  2514.                     !ckstrcmp(info[4],"none", 4,0) ||
  2515.                     !ckstrcmp(info[4],"even", 4,0) ||
  2516.                     !ckstrcmp(info[4],"space",5,0) ||
  2517.                     !ckstrcmp(info[4],"mark", 4,0) ||
  2518.                     !ckstrcmp(info[4],"odd",  3,0)
  2519.                     )
  2520.                   oldentry = 1;
  2521.             }
  2522.         }
  2523.         if (pp) {
  2524.             free(pp);
  2525.             pp = NULL;
  2526.         }
  2527.  
  2528.         /* Check consistency */
  2529.  
  2530.         if ((oldentry || olddir) && (newentry || newdir)) {
  2531.             printf(
  2532. "\nERROR: You seem to have old- and new-format entries mixed in your\n");
  2533.             printf(
  2534. "dialing directory.  You'll have to edit it by hand to convert it to the\n");
  2535. #ifndef NOHELP
  2536.             printf("new format.  Type HELP DIAL for further information.\n\n");
  2537. #else
  2538.             printf("new format.\n\n");
  2539. #endif /* NOHELP */
  2540.             if (line) {
  2541.                 free(line);
  2542.                 line = NULL;
  2543.             }
  2544.             return(-1);
  2545.         }
  2546.         if (!olddir && oldentry) {
  2547.             int convert = 0;
  2548.             olddir = 1;
  2549.             if (dialcvt == 2) {         /* 2 == ASK */
  2550.                 sprintf(tmpbuf,
  2551. "WARNING: Old-style dialing directory detected:\n%s", line);
  2552.         convert = uq_ok(tmpbuf,
  2553.                 "Shall I convert it for you? ",3,NULL,0);
  2554.             } else
  2555.               convert = dialcvt;
  2556.             if (convert) {
  2557.                 debug(F111,"ludial calling ddcvt",dialdir[dd-1],dd);
  2558.                 if (ddcvt(dialdir[dd-1],f,dd-1) < 0) {
  2559.                     debug(F111,"ludial ddcvt failed",dialdir[dd-1],dd);
  2560.                     oldflg = 1;
  2561.                     printf(
  2562. "  Sorry, can't convert.");
  2563.                     printf(
  2564. "  Will ignore speed and parity fields, continuing...\n\n");
  2565.                 } else {
  2566.                     olddir = newdir = 0;
  2567.                     debug(F111,"ludial ddcvt ok",dialdir[dd-1],dd);
  2568.                 }
  2569.                 dd--;
  2570.                 f = NULL;
  2571.                 continue;
  2572.             } else {
  2573.                 if (dialcvt == 2)
  2574.                   printf(
  2575. "  OK, will ignore speed and parity fields, continuing...\n\n");
  2576.                 olddir = 1;
  2577.             }
  2578.         }
  2579.  
  2580. #ifdef LUDEBUG
  2581. if (zz) printf("LUDIAL XX s[%s], n1=%d\n",s,n1);
  2582. #endif /* LUDEBUG */
  2583.  
  2584.         /* Now parse again for real */
  2585.  
  2586.         if (oldentry)                   /* Parse it the old way */
  2587.           xwords(line,5,info,0);
  2588.         else                            /* Parse it the new way */
  2589.           xwords(line,2,info,1);
  2590.  
  2591. #ifdef LUDEBUG
  2592. if (zz) printf("LUDIAL YY s[%s], n1=%d\n",s,n1);
  2593. if (zz) printf("%s [%s]\n",info[1],info[2]);
  2594. #endif /* LUDEBUG */
  2595.  
  2596.         if (info[1]) {                  /* First word is entry name */
  2597.             if ((n3 = (int) strlen(info[1])) < 1) /* Its length */
  2598.               continue;                 /* If no first word, keep reading. */
  2599.             if (n3 < n1)                /* Search name is longer */
  2600.               continue;                 /* Can't possibly match */
  2601.             if (ambiguous && n3 != n1)
  2602.               continue;
  2603.  
  2604. #ifdef LUDEBUG
  2605. if (zz) printf("MATCHING: [%s] [%s], n1=%d\n",s,info[1],n1);
  2606. #endif /* LUDEBUG */
  2607.  
  2608.             if (ckstrcmp(s,info[1],n1,0)) /* Caseless string comparison */
  2609.               continue;
  2610.  
  2611. #ifdef LUDEBUG
  2612. if (zz) printf("MATCH OK: [%s] [%s], n1=%d\n",s,info[1],n1);
  2613. #endif /* LUDEBUG */
  2614.  
  2615.             if (!info[2])               /* No phone number given */
  2616.               continue;
  2617.             if ((n2 = (int) strlen(info[2])) < 1) /* Length of phone number */
  2618.               continue;                 /* Ignore empty phone numbers */
  2619.  
  2620.             /* Got one */
  2621.  
  2622.             if (!(pp = (char *)malloc(n2 + 1))) { /* Allocate storage for it */
  2623.                 printf("?internal error - ludial malloc 1\n");
  2624.                 if (line) {
  2625.                     free(line);
  2626.                     line = NULL;
  2627.                 }
  2628.                 dncount = 0;
  2629.                 return(-1);
  2630.             }
  2631.             strcpy(pp,info[2]);         /* safe */
  2632.  
  2633.             if (dncount > MAXDNUMS) {
  2634.                 printf("Warning: %d matches found, %d max\n",
  2635.                        dncount,
  2636.                        MAXDNUMS
  2637.                        );
  2638.                 dncount = MAXDNUMS;
  2639.                 break;
  2640.             }
  2641.             dn_p[dncount++] = pp;       /* Add pointer to array. */
  2642.             if (dncount == 1) {         /* First one... */
  2643.                 if (d_name) free(d_name);
  2644.                 if (!(d_name = (char *)malloc(n3 + 1))) { /* Save its name */
  2645.                     printf("?internal error - ludial malloc 2\n");
  2646.                     if (line) {
  2647.                         free(line);
  2648.                         line = NULL;
  2649.                     }
  2650.                     dncount = 0;
  2651.                     return(-1);
  2652.                 }
  2653.                 t = n3;                 /* And its length */
  2654.                 strcpy(d_name,info[1]); /* safe */
  2655.             } else {                    /* Second or subsequent one */
  2656.  
  2657. #ifdef LUDEBUG
  2658.                 if (zz)
  2659.                   printf("d_name=[%s],info[1]=%s,t=[%d]\n",d_name,info[1],t);
  2660. #endif /* LUDEBUG */
  2661.  
  2662.                 if ((int) strlen(info[1]) == t) /* Lengths compare */
  2663.                   if (!ckstrcmp(d_name,info[1],t,0)) /* Caseless compare OK */
  2664.                     continue;
  2665.  
  2666.                 /* Name given by user matches entries with different names */
  2667.  
  2668.                 if (ambiguous)          /* Been here before */
  2669.                   break;
  2670.  
  2671.                 ambiguous = 1;          /* Now an exact match is required */
  2672.                 for (j = 0; j < dncount; j++) { /* Clean out previous list */
  2673.                     if (dn_p[j]) {
  2674.                         free(dn_p[j]);
  2675.                         dn_p[j] = NULL;
  2676.                     }
  2677.                 }
  2678.                 pass++;                 /* Second pass... */
  2679.                 goto lu_again;          /* Do it all over again. */
  2680.             }
  2681.         }
  2682.     }
  2683.     if (line) free(line);
  2684.     if (dncount == 0 && ambiguous) {
  2685.         printf(" Lookup: \"%s\" - ambiguous%s\n",
  2686.                s,
  2687.                cx == XXLOOK ? "" : " - dialing skipped"
  2688.                );
  2689.         return(-2);
  2690.     }
  2691.     return(dncount);
  2692. }
  2693.  
  2694. char *
  2695. pncvt(s) char *s; {                     /* Phone number conversion */
  2696.     char *p = NULL;                     /* (just a wrapper for dncvt() */
  2697.     char *q = NULL;
  2698.     static char pnbuf[128];
  2699.     makestr(&p,dn_p[0]);                /* Save these in case they are */
  2700.     makestr(&q,dn_p2[0]);               /* being used */
  2701.     makestr(&dn_p[0],s);                /* Copy the argument string to here */
  2702.     dncvt(0,XXLOOK,1,1);                /* Convert it */
  2703.     if (!dn_p2[0])                      /* Put result where can return it */
  2704.       pnbuf[0] = NUL;
  2705.     else
  2706.       ckstrncpy(pnbuf,dn_p2[0],127);
  2707.     makestr(&dn_p[0],p);                /* Restore these */
  2708.     makestr(&dn_p2[0],q);
  2709.     makestr(&p,NULL);                   /* Free these */
  2710.     makestr(&q,NULL);
  2711.     return((char *)pnbuf);
  2712. }
  2713.  
  2714. int
  2715. dodial(cx) int cx; {                    /* DIAL or REDIAL */
  2716.     int i = 0, x = 0;                   /* Workers */
  2717.     int sparity = -1;                   /* For saving global parity value */
  2718.     int previous = 0;
  2719.     int len = 0;
  2720.     int literal = 0;
  2721.     int flowsave;
  2722.     int lufound = 0;                    /* Did any lookup succeed? */
  2723.     int prefix = 1;
  2724.     int postfix = 1;
  2725.     int wasalpha = 0;
  2726.     int xredial = 0;
  2727.     int braces = 0;
  2728.  
  2729.     char *p = NULL, *s3 = NULL, * sav = NULL;
  2730.     int j = 0, t = 0, n = 0;
  2731.     int xretries, xlcc;
  2732.  
  2733. #ifdef COMMENT
  2734.     debug(F101,"dodial cx","",cx);
  2735.     debug(F111,"dodial diallcc",diallcc,diallcc);
  2736. #endif    /* COMMENT */
  2737.  
  2738.     xretries = dialrtr;                 /* If retries not set, */
  2739.     if (diallcc) {                      /* choose default based on */
  2740.         xlcc = atoi(diallcc);           /* local country code. */
  2741.         if (xretries < 0) {
  2742.             switch (xlcc) {
  2743.               case 1: xretries = 10; break; /* No restrictions in NANP */
  2744.                 /* Add other country codes here */
  2745.                 /* that are known to have no restrictions on redialing. */
  2746.               default: xretries = 1;
  2747.             }
  2748.         }
  2749.     }
  2750.     if (cx == XXPDIA) {                 /* Shortcut... */
  2751.         cx = XXDIAL;
  2752.         partial = 1;
  2753.         debug(F100,"PDIAL sets partial=1","",0);
  2754.         postfix = 0;                    /* Do not add postfix */
  2755.     } else {
  2756.         partial = 0;
  2757.         debug(F100,"DIAL sets partial=0","",0);
  2758.     }
  2759.     previous = dialsta;                 /* Status of previous call, if any */
  2760.     if (previous == DIA_PART) {
  2761.         prefix = 0;                     /* do not add prefix */
  2762.     }
  2763.     s = NULL;                           /* Initialize user's dial string */
  2764.     if (cx == XXRED) {                  /* REDIAL or... */
  2765.         if ((y = cmcfm()) < 0)
  2766.           return(y);
  2767.     } else if (cx == XXANSW) {          /* ANSWER or ... */
  2768.         if ((y = cmnum("timeout (seconds)","0",10,&x,xxstring)) < 0)
  2769.           return(y);
  2770.         dialatmo = x;
  2771.         if ((y = cmcfm()) < 0)
  2772.           return(y);
  2773.     } else {                            /* DIAL or LOOKUP */
  2774.         if (ndialdir > 0)
  2775.           s3 = "Number to dial or entry from dial directory";
  2776.         else
  2777.           s3 = "Number to dial";
  2778.         if ((x = cmtxt(s3, dialnum ? dialnum : "",&s,xxstring)) < 0)
  2779.           return(x);
  2780.         if (s) {
  2781.             len = (int) strlen(s);
  2782.             ckstrncpy(tmpbuf,s,TMPBUFSIZ); /* Save literal copy */
  2783. #ifdef COMMENT
  2784.             if (len > 1) {              /* Strip outer braces if given */
  2785.                 if (*s == '{') {
  2786.                     if (s[len-1] == '}') {
  2787.                         s[len-1] = NUL;
  2788.                         s++;
  2789.                         len -= 2;
  2790.                     }
  2791.                 }
  2792.             }
  2793. #else
  2794.             s = brstrip(s);             /* Strip outer braces or quotes */
  2795. #endif /* COMMENT */
  2796.         }
  2797.     }
  2798.  
  2799.     if (cx != XXLOOK) {                 /* Not LOOKUP */
  2800. #ifdef IKSD
  2801.         if (inserver) {
  2802.             printf("Sorry, dialing is disabled.\r\n");
  2803.             return(success = 0);
  2804.         }
  2805. #endif /* IKSD */
  2806. #ifdef CK_TAPI
  2807.         if (tttapi && !tapipass) {
  2808.           ;                             /* Skip the modem test if TAPI */
  2809.         } else
  2810. #endif /* CK_TAPI */
  2811.         if (mdmtyp < 1 && !dialtest) {
  2812.             if (network
  2813. #ifdef TN_COMPORT
  2814.                  && !istncomport()
  2815. #endif /* TN_COMPORT */
  2816.                  )
  2817.               printf("Please SET HOST first, and then SET MODEM TYPE\n");
  2818.             else
  2819.               printf("Sorry, you must SET MODEM TYPE first\n");
  2820.             dialsta = DIA_NOMO;
  2821.             return(success = 0);
  2822.         }
  2823.         if (!local && !dialtest) {
  2824.             printf("Sorry, you must SET %s or SET HOST first\n",
  2825. #ifdef OS2
  2826.                    "PORT"
  2827. #else
  2828.                    "LINE"
  2829. #endif /* OS2 */
  2830.                    );
  2831.             dialsta = DIA_NOLI;
  2832.             return(success = 0);
  2833.         }
  2834.         if ((!network 
  2835. #ifdef TN_COMPORT
  2836.               || istncomport()
  2837. #endif /* TN_COMPORT */
  2838.               ) && !dialtest &&
  2839. #ifdef CK_TAPI
  2840.              !tttapi &&
  2841. #endif /* CK_TAPI */
  2842.             (speed < 0L)
  2843. #ifdef UNIX
  2844.             && (strcmp(ttname,"/dev/null"))
  2845. #else
  2846. #ifdef OSK
  2847.             && (strcmp(ttname,"/nil"))
  2848. #endif /* OSK */
  2849. #endif /* UNIX */
  2850.             ) {
  2851.             printf("\nSorry, you must SET SPEED first\n");
  2852.             dialsta = DIA_NOSP;
  2853.             return(success = 0);
  2854.         }
  2855.     }
  2856.     if (cx != XXANSW) {
  2857.         for (j = 0; j < MAXDNUMS; j++) { /* Initialize dial-number list */
  2858.             if (!dialnum) {             /* First time dialing */
  2859.                 dn_p[j] = NULL;         /* initialize all pointers. */
  2860.                 dn_p2[j] = NULL;
  2861.             } else if (dn_p[j]) {       /* Not the first time, */
  2862.                 free(dn_p[j]);          /* free previous, if any, */
  2863.                 dn_p[j] = NULL;         /* then set to NULL. */
  2864.                 if (dn_p2[j])
  2865.                   free(dn_p2[j]);
  2866.                 dn_p2[j] = NULL;
  2867.             } else break;               /* Already NULL */
  2868.         }
  2869.         if (len == 0)
  2870.           s = NULL;
  2871.         if (!s)
  2872.           s = dialnum;
  2873.         if (!s) {
  2874.             if (cx == XXLOOK)
  2875.               printf("?Lookup what?\n");
  2876.             else
  2877.               printf("%s\n", (cx == XXRED) ?
  2878.                    "?No DIAL command given yet" :
  2879.                    "?You must specify a number to dial"
  2880.                    );
  2881.             return(-9);
  2882.         }
  2883.  
  2884.     /* Now we have the "raw" dial or lookup string and s is not NULL */
  2885.  
  2886.         makestr(&dscopy,s);             /* Put it in a safe place */
  2887.         s = dscopy;
  2888.         n = 0;
  2889.  
  2890.         debug(F111,"dodial",s,ndialdir);
  2891.  
  2892.         wasalpha = 0;
  2893.         if (isalpha(*s)) {
  2894.             wasalpha = 1;
  2895.             if (ndialdir > 0) {         /* Do we have a dialing directory? */
  2896.                 n = ludial(s,cx);       /* Look up what the user typed */
  2897.                 if (n == 0)
  2898.                   printf(" Lookup: \"%s\" - not found%s\n",
  2899.                          s,
  2900.                          cx == XXLOOK ? "" : " - dialing as given\n"
  2901.                          );
  2902.             }
  2903.             debug(F101,"dodial",s,n);
  2904.             if (n < 0 && cx != XXLOOK) { /* Error out if they wanted to dial */
  2905.                 if (n == -1)            /* -2 means ludial already gave msg */
  2906.                   printf(" Lookup: fatal error - dialing skipped\n");
  2907.                 dialsta = DIA_DIR;
  2908.                 return(-9);
  2909.             }
  2910.             if (n > 0)                  /* A successful lookup */
  2911.               lufound = 1;
  2912.         } else if (*s == '=') {         /* If number starts with = sign */
  2913.             s++;                        /* strip it */
  2914.             literal = 1;                /* remember this */
  2915.             while (*s == SP) s++;       /* and then also any leading spaces */
  2916.         } else if (tmpbuf[0] == '{' && tmpbuf[1] == '{') {
  2917.             makelist(tmpbuf,dn_p,MAXDNUMS);
  2918.             makestr(&dscopy,tmpbuf);
  2919.             s = tmpbuf;
  2920.             for (n = 0; n < MAXDNUMS; n++) /* (have to count how many) */
  2921.               if (!dn_p[n]) break;
  2922.             braces = 1;
  2923.         }
  2924.         if (cx == XXLOOK && !wasalpha && !braces) {
  2925.             /* We've been told to lookup a number or a quoted name */
  2926.             char *p;
  2927.             n = 0;
  2928.             p = literal ? s : pncvt(dscopy);
  2929.             if (!p) p = "";
  2930.             if (*p) {
  2931.                 printf("%s  => %s\n", dscopy, p);
  2932.                 return(success = 1);
  2933.             } else {
  2934.                 printf("?Bad phone number\n");
  2935.                 return(success = 0);
  2936.             }
  2937.         }
  2938.         /* Save DIAL or successful LOOKUP string for future DIAL or REDIAL */
  2939.         /* But don't save pieces of partial dial ... */
  2940.  
  2941.         debug(F101,"DIAL save dialnum partial","",partial);
  2942.         debug(F101,"DIAL save dialnum previous","",previous);
  2943.         if ((cx == XXDIAL && partial == 0 && previous != DIA_PART) ||
  2944.             (cx == XXLOOK && n > 0)) {
  2945.             makestr(&dialnum,dscopy);
  2946.             if (!quiet && dscopy && !dialnum)
  2947.               printf("WARNING - memory allocation failure: redial number\n");
  2948.         }
  2949.         if (n > 0) {
  2950.             if (!quiet && !backgrd && !braces /* && dialdpy */ ) {
  2951.                 if (!strcmp(d_name,s))
  2952.                   printf(" Lookup: \"%s\" - exact match\n",s);
  2953.                 else
  2954.                   printf(" Lookup: \"%s\" - uniquely matches \"%s\"\n",
  2955.                          s,
  2956.                          d_name
  2957.                          );
  2958.             }
  2959.             if ((cx == XXLOOK) ||
  2960.                 ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2961.                 printf(" %d telephone number%sfound for \"%s\"%s\n",
  2962.                        n,
  2963.                        (n == 1) ? " " : "s ",
  2964.                        s,
  2965.                        (n > 0) ? ":" : "."
  2966.                        );
  2967.                 s3 = getdname();
  2968.             }
  2969.             for (i = 0; i < n; i++) {   /* Convert */
  2970.                 dn_x[i] = -1;
  2971.                 if (dncvt(i,cx,prefix,postfix) < 0) {
  2972.                     if (cx != XXLOOK) {
  2973.                         dialsta = DIA_DIR;
  2974.                         return(-9);
  2975.                     }
  2976.                 }
  2977.             }
  2978.             if (dialsrt && n > 1) {     /* Sort into optimal order */
  2979.                 for (i = 0; i < n-1; i++) {
  2980.                     for (j = i+1; j < n; j++) {
  2981.                         if (dn_x[j] < dn_x[i]) {
  2982.                             t = dn_x[j];
  2983.                             dn_x[j] = dn_x[i];
  2984.                             dn_x[i] = t;
  2985.                             p = dn_p[j];
  2986.                             dn_p[j] = dn_p[i];
  2987.                             dn_p[i] = p;
  2988.                             p = dn_p2[j];
  2989.                             dn_p2[j] = dn_p2[i];
  2990.                             dn_p2[i] = p;
  2991.                         }
  2992.                     }
  2993.                 }
  2994.             }
  2995.             if ((cx == XXLOOK) ||
  2996.                 ((n > 1) && !quiet && !backgrd /* && dialdpy */ )) {
  2997.                 int nn = n;
  2998. #ifndef NOSPL
  2999.                 char * p;
  3000. #endif /* NOSPL */
  3001.                 if (cx != XXLOOK)
  3002.                   if (n > 12) nn = 12;
  3003.                 for (i = 0; i < nn; i++) {
  3004.                     printf("%3d. %-12s  %-20s =>  %-20s  (%d)\n",i+1,
  3005.                            s3, dn_p[i],
  3006.                            dn_p2[i] ? dn_p2[i] : "(processing failed)",
  3007.                            dn_x[i]
  3008.                            );
  3009.                 }
  3010.                 if (cx != XXLOOK && n != nn)
  3011.                   printf("And %d more...\n", n - nn);
  3012.             }
  3013.         } else if (n == 0) {            /* Not found in directory */
  3014.             makestr(&(dn_p[0]),literal ? s : dscopy);
  3015.             makestr(&d_name,literal ? s : dscopy);
  3016.             dncount = 1;
  3017.             n = 1;
  3018.             if (dncvt(0,cx,prefix,postfix) < 0) { /* In case they typed a */
  3019.                 dialsta = DIA_DIR;      /* portable-format number ... */
  3020.                 return(-9);
  3021.             }
  3022.         }
  3023.  
  3024. #ifndef NONET
  3025. #ifdef NETCONN
  3026.         /* It's not good that the networks directory depends on NOT-NODIAL.. */
  3027.         if (cx == XXLOOK && dscopy) {   /* Networks here too... */
  3028.             extern char *nh_p[], *nh_p2[], *n_name;
  3029.             extern char *nh_px[4][MAXDNUMS+1];
  3030.             n = -1;
  3031.             if (nnetdir > 0) {          /* Do we have a network directory? */
  3032.                 dirline = 0;
  3033.                 n = lunet(dscopy);      /* Look up what the user typed */
  3034.             }
  3035.             if (n > -1) {
  3036.                 int k;
  3037.                 if (n > 0)              /* A successful lookup */
  3038.                   lufound = 1;
  3039.                 if (cx == XXLOOK && n == 0)
  3040.                   printf(" Lookup: \"%s\" - not found\n",dscopy);
  3041.                 else
  3042.                   printf("%s %d network entr%s found for \"%s\"%s\n",
  3043.                          cx == XXLOOK ? " Lookup:" : "",
  3044.                          n,
  3045.                          (n == 1) ? "y" : "ies",
  3046.                          dscopy,
  3047.                          (n > 0) ? ":" : "."
  3048.                          );
  3049.  
  3050.                 for (i = 0; i < n; i++) {
  3051.  
  3052.                     printf("%3d. %-12s => %-9s %s",
  3053.                            i+1,n_name,nh_p2[i],nh_p[i]);
  3054.                     for (k = 0; k < 4; k++) {
  3055.                         if (nh_px[k][i]) {
  3056.                             printf(" %s",nh_px[k][i]);
  3057.                         } else
  3058.                           break;
  3059.                     }
  3060.                     printf("\n");
  3061.                 }
  3062.             }
  3063.         }
  3064. #endif /* NETCONN */
  3065. #endif /* NONET */
  3066.         if (cx == XXLOOK)
  3067.           return(success = lufound);
  3068.     } /* cx != XXANSW */
  3069.  
  3070. #ifdef VMS
  3071.     conres();                   /* So Ctrl-C/Y will work */
  3072. #endif /* VMS */
  3073. /*
  3074.   Some modems do not react well to parity.  Also, if we are dialing through a
  3075.   TCP/IP TELNET modem server, parity can be fatally misinterpreted as TELNET
  3076.   negotiations.
  3077.  
  3078.   This should work even if the user interrupts the DIAL command, because the
  3079.   DIAL module has its own interrupt handler.  BUT... if, for some reason, a
  3080.   dialing device actually *requires* parity (e.g. CCITT V.25bis says that even
  3081.   parity should be used), this might prevent successful dialing.  For that
  3082.   reason, we don't do this for V.25bis modems.
  3083. */
  3084.     sparity = parity;                   /* Save current parity */
  3085.     if ((dialcapas & CKD_V25) == 0)     /* If not V.25bis...  */
  3086.       parity = 0;                       /* Set parity to NONE */
  3087.  
  3088.     flowsave = flow;
  3089. /*
  3090.   These modems use some kind of screwy flow control while in command mode,
  3091.   and do not present CTS as they should.  So if RTS/CTS is set (or even if
  3092.   it isn't) disable flow control during dialing.
  3093. */
  3094. #ifndef MINIDIAL
  3095.     if (mdmtyp == n_ATT1910 || mdmtyp == n_ATT1900) {
  3096.         flow = FLO_NONE;                /* This is not enough */
  3097. #ifdef CK_TTSETFLOW
  3098.         ttsetflow(FLO_NONE);            /* Really turn it off */
  3099. #endif /* CK_TTSETFLOW */
  3100.     }
  3101. #endif /* MINIDIAL */
  3102.     if (!network
  3103. #ifdef TN_COMPORT
  3104.         || istncomport()
  3105. #endif /* TN_COMPORT */
  3106.          ) {
  3107.         int x;
  3108.         if ((x = ttgmdm()) > -1) {
  3109.             if (!x && msgflg) {
  3110.                 printf(
  3111. "WARNING - No modem signals detected.  Is your modem turned on?  If not,\n\
  3112. use Ctrl-C to interrupt dialing, turn on your modem, then %s.\n",
  3113.                        cx == XXANSW ?
  3114.                        "ANSWER again" :
  3115.                        "REDIAL"
  3116.                        );
  3117.             }
  3118.             if (flow == FLO_RTSC) {
  3119.                 if (!(x & BM_CTS)) {
  3120.                     if (msgflg)
  3121.                       printf(
  3122. "WARNING - SET FLOW RTS/CTS is in effect but modem's CTS signal is off.\n\
  3123. Disabling flow control temporarily %s...\n",
  3124.                              cx == XXANSW ?
  3125.                              "while waiting for call" :
  3126.                              "during dialing"
  3127.                              );
  3128.                     flow = FLO_NONE;
  3129.                 }
  3130.             }
  3131.         }
  3132.     }
  3133.     if (cx == XXANSW) {                 /* ANSWER */
  3134.         success = ckdial("",0,0,1,0);
  3135.         goto dialfin;
  3136.     }
  3137.  
  3138. /* Edit 192 adds the ability to dial repeatedly. */
  3139.  
  3140.     i = 0;
  3141.     dialcount = 0;
  3142.     do {
  3143.         if (i > 0) printf("\nDial attempt %d of %d...\n", i+1, xretries);
  3144.         dialcount = i+1;
  3145.         success = 0;
  3146.         /* And the ability to dial alternate numbers. */
  3147.         /* Loop to dial each in a list of numbers for the same name... */
  3148.         for (j = 0; j < n && !success; j++) { /* until one answers. */
  3149.             s = dn_p2[j];               /* Next number in list */
  3150.             if (dn_x[j] >= dialrstr) {  /* Dial restriction */
  3151.                 printf("Restricted: %s, skipping...\n",dn_p[j]);
  3152.                 continue;
  3153.             }
  3154.             xredial = (i == 0 && j == 0) ? 0 : 1;
  3155.             if (!s) s = dn_p[j];
  3156.  
  3157. #ifndef NOSPL
  3158.             sav = s;
  3159.             p = xdial(s);               /* Apply DIAL macro now */
  3160.             if (p) if (*p) s = p;
  3161. #endif /* NOSPL */
  3162.  
  3163.         /* Dial confirmation */
  3164.         /* NOTE: the uq_xxx() calls allow for a GUI dialog */
  3165.  
  3166.             if (i == 0 && dialcnf) {
  3167.         char msgbuf[128];
  3168.         ckmakmsg(msgbuf,128,"Dialing ",s,NULL,NULL);
  3169.         x = uq_ok(msgbuf,"Is this number correct? ",3,NULL,0);
  3170.                 if (!x) {
  3171.  
  3172. #ifndef COMMENT
  3173.             x = uq_txt(        /* Allow GUI dialog */
  3174. #ifdef OS2
  3175. " Please enter the correct number,\r\n or press Enter to skip.",
  3176. #else
  3177. " Please enter the correct number,\r\n or press Return to skip.",
  3178. #endif /* OS2 */
  3179.                               "Corrected phone number: ",
  3180.                                1,
  3181.                    NULL,
  3182.                    atmbuf,
  3183.                    ATMBL,
  3184.                                s,
  3185.                                DEFAULT_UQ_TIMEOUT
  3186.                                );
  3187.             if (x && atmbuf[0]) { /* They gave a new one */
  3188.             s = atmbuf;
  3189.             makestr(&(dn_p2[j]), s);
  3190.             }            
  3191.  
  3192. #else  /* COMMENT */
  3193.  
  3194. #ifdef CK_RECALL
  3195.                     extern int on_recall;
  3196. #endif /* CK_RECALL */
  3197.                     cmsavp(psave,PROMPTL);
  3198.                     cmsetp(
  3199. #ifdef OS2
  3200. " Please enter the correct number,\r\n or press Enter to skip: "
  3201. #else
  3202. " Please enter the correct number,\r\n or press Return to skip: "
  3203. #endif /* OS2 */
  3204.                            );
  3205.                     cmini(ckxech);
  3206.                     x = -1;
  3207.                     if (pflag) prompt(NULL);
  3208. #ifdef CK_RECALL
  3209.                     on_recall = 0;
  3210. #endif /* CK_RECALL */
  3211.                     y = cmdgquo();
  3212.                     cmdsquo(0);
  3213.                     while (x < 0) {
  3214.                         x = cmtxt("Corrected phone number","",&s,NULL);
  3215.                         cmres();
  3216.                     }
  3217.                     if ((int) strlen(s) < 1) {
  3218.                         cmsetp(psave);
  3219.                         continue;
  3220.                     }
  3221.                     makestr(&(dn_p2[j]), s);
  3222.                     cmdsquo(y);
  3223.                     cmsetp(psave);
  3224. #endif /* COMMENT */
  3225.                 }
  3226.             }
  3227.             if (dialtest) {             /* Just testing */
  3228.                 if (i + j == 0)
  3229.                   printf("\nTESTING...\n");
  3230.                 if (dialmac)
  3231.                   printf(" Number: \"%s\" => \"%s\"\n",sav,s);
  3232.                 else
  3233.                   printf(" Number: \"%s\"\n",s);
  3234.                 dialsta = DIA_BUSY;
  3235.                 success = 0;
  3236.             } else {
  3237.                 what |= W_DIALING;
  3238.                 success = ckdial(s,i,j,partial ? 3 : 0, xredial); /* Dial it */
  3239.                 what &= ~(W_DIALING);
  3240.                 if (!success) {
  3241.                     if (dialsta < 8 ||  /* Break out if unrecoverable error */
  3242.                         dialsta  == DIA_INTR ||
  3243.                         dialsta  == DIA_ERR  ||
  3244.                         previous == DIA_PART
  3245.                         )
  3246.                       break;
  3247.                 }
  3248.             }
  3249.         }
  3250.         if (success)                    /* Succeeded, leave the outer loop */
  3251.           break;
  3252.         if (dialsta < 8 ||              /* Break out if unrecoverable error */
  3253.             dialsta == DIA_INTR ||      /* Interrupted */
  3254.             dialsta == DIA_NODT ||      /* No dialtone */
  3255.             dialsta == DIA_NOAC ||      /* Access forbidden */
  3256.             dialsta == DIA_BLCK ||      /* Blacklisted */
  3257.             dialsta == DIA_DIR  ||      /* Dialing directory error */
  3258.             dialsta == DIA_ERR  ||      /* Modem command error */
  3259.             previous == DIA_PART)
  3260.           break;
  3261.         if (++i >= xretries)            /* Break out if too many tries */
  3262.           break;
  3263.         if (!backgrd && !quiet) {
  3264.             if (dialint > 5)
  3265.               printf(
  3266. "\nWill redial in %d second%s- press any key to redial immediately.\n",
  3267.                      dialint,
  3268.                      dialint == 1 ? " " : "s "
  3269.                      );
  3270.             printf("Ctrl-C to cancel...\n");
  3271.         }
  3272.         x = dialint;                    /* Redial interval */
  3273.         while (x-- > 0) {
  3274.             if ((y = conchk()) > 0) {   /* Did they type something? */
  3275.                 while (y--) coninc(0);  /* Yes, absorb it */
  3276.                 break;                  /* And wake up */
  3277.             }
  3278.             sleep(1);                   /* No interrupt, sleep a sec */
  3279.         }
  3280.     } while (!success);
  3281.  
  3282.   dialfin:
  3283.  
  3284.     if (cx != XXLOOK) {
  3285.         if (!success)
  3286.           bleep((short) BP_FAIL);
  3287.         else if (!quiet)
  3288.           bleep((short) BP_NOTE);
  3289. #ifdef OS2
  3290.         setint();                       /* Fix OS/2 interrupts */
  3291. #endif /* OS2 */
  3292.         if (sparity > -1)
  3293.           parity = sparity;             /* Restore parity if we saved it */
  3294.         flow = flowsave;
  3295. #ifdef OS2
  3296.         ttres();                        /* Restore DIAL device */
  3297. #endif /* OS2 */
  3298. #ifdef VMS
  3299.         concb((char)escape);            /* Restore console */
  3300. #endif /* VMS */
  3301. #ifdef OS2
  3302.         {                               /* Set session title */
  3303.             char * p, name[72];         /* in window list. */
  3304.             char * q;
  3305.             if (cx == XXANSW) {
  3306.                 q = "Incoming call";
  3307.             } else {
  3308.                 if (d_name)
  3309.                   q = d_name;
  3310.                 else if (dialnum)
  3311.                   q = dialnum;
  3312.                 else if (ttname[0])
  3313.                   q = ttname;
  3314.                 else q = "";
  3315.             }
  3316.             p = name;
  3317.             if (success) {
  3318.                 strncpy(name,q,48);
  3319.                 while (*p) {            /* Uppercase it for emphasis. */
  3320.                     if (islower(*p))
  3321.                       *p = toupper(*p);
  3322.                     p++;
  3323.                 }
  3324.             } else
  3325.               name[0] = NUL ;
  3326.             os2settitle((char *) name, TRUE);
  3327.         }
  3328. #endif /* OS2 */
  3329.     }
  3330.     if (cx != XXLOOK) {
  3331.         if (success) {
  3332.             if (reliable == SET_AUTO) { /* It's not a reliable connection. */
  3333.                 reliable = SET_OFF;
  3334.                 debug(F101,"dodial reliable","",reliable);
  3335.             }
  3336.         } else {
  3337. #ifndef NOHINTS
  3338.             extern int hints;
  3339.             if (hints && !quiet && dialsta != 9) { /* 9 == User interrupted */
  3340.                 extern int dialmhu, dialhng, dialdpy;
  3341.                 extern char * dialmsg[];
  3342.                 printf("\n*************************\n");
  3343.                 printf("DIAL-class command failed.\n");
  3344.                 printf("Modem type:  %s\n", gmdmtyp());
  3345.                 printf("Device:      %s\n", ttname);
  3346.                 printf("Speed:       %ld\n", speed);
  3347.                 printf("Dial status: %d",dialsta);
  3348.                 if (dialsta < 35 && dialmsg[dialsta])
  3349.                   printf(" [%s]",dialmsg[dialsta]);
  3350.                 printf("\n");
  3351.                 if (dialsta == DIA_TIMO ||
  3352.                     dialsta == DIA_NRDY ||
  3353.                    (dialsta > 13 && dialsta != DIA_BUSY && dialsta != DIA_NOAN)
  3354.                     ) {
  3355.                     switch (dialsta) {
  3356.                       case DIA_TIMO:
  3357.                         printf(
  3358. " . SET DIAL TIMEOUT to a greater value and try again.\n"
  3359.                                );
  3360.                         break;
  3361.                       case DIA_NRSP:
  3362.                       case DIA_NRDY:
  3363.                       case DIA_NOIN:
  3364.                         printf(
  3365. " . Is the modem turned on?\n"
  3366.                                );
  3367.                         printf(
  3368. " . Are you using the right communication port?\n"
  3369.                                );
  3370.                         break;
  3371.                       case DIA_NODT:
  3372.                         printf(
  3373. " . Is the modem connected to the telephone line?\n"
  3374.                                );
  3375.                     }
  3376.                     if (mdmtyp == n_GENERIC) {
  3377.                         printf(
  3378. " . Please choose a specific modem type with SET MODEM TYPE and try again.\n"
  3379.                                );
  3380.                         printf(
  3381. "    SET MODEM TYPE ? to see the list of known modem types.\n"
  3382.                                );
  3383.                     } else {
  3384.                         printf(
  3385. " . Are you sure you have chosen the appropriate modem type?\n"
  3386.                                );
  3387.                     }
  3388.                     if (speed > 19200L) {
  3389.                         printf(
  3390. " . Maybe the interface speed (%ld) is too fast:\n", speed
  3391.                                );
  3392.                         printf(
  3393. "    SET SPEED to a lower speed and try again.\n"
  3394.                                );
  3395.                         printf(
  3396. "    SET SPEED ? to see the list of valid speeds.\n"
  3397.                                );
  3398.                     }
  3399.                     if (dialhng) {
  3400.                         if (dialmhu)
  3401.                           printf(
  3402. " . SET MODEM HANGUP-METHOD RS232 and try again.\n"
  3403.                                  );
  3404.                         else
  3405.                           printf(
  3406. " . SET MODEM HANGUP-METHOD MODEM-COMMAND and try again.\n"
  3407.                                  );
  3408.                         printf(
  3409. " . If that doesn't work, try again with SET DIAL HANGUP OFF.\n"
  3410.                                );
  3411.                     } else {
  3412.                         printf(
  3413. " . Give a HANGUP or SET DIAL HANGUP ON command and try again.\n"
  3414.                                );
  3415.                     }
  3416.                     if (!dialdpy)
  3417.                       printf(
  3418. " . Use SET DIAL DISPLAY ON to watch the dialog between Kermit and modem.\n"
  3419.                              );
  3420.                 }
  3421. #ifndef NOSHOW
  3422.                 printf(
  3423. " . SHOW COMMUNICATIONS, SHOW MODEM, SHOW DIAL to see current settings.\n"
  3424.                        );
  3425. #endif /* NOSHOW */
  3426.  
  3427. #ifndef NOHELP
  3428.                 printf(
  3429. " . HELP SET MODEM, HELP SET DIAL, and HELP DIAL for more information.\n"
  3430.                        );
  3431. #endif /* NOHELP */
  3432.                 printf("(Use SET HINTS OFF to suppress future hints.)\n");
  3433.                 printf("*************************\n\n");
  3434.             }
  3435. #endif /* NOHINTS */
  3436.         }
  3437.     }
  3438.     return(success);
  3439. }
  3440. #endif /* NODIAL */
  3441.  
  3442. /*  D O T Y P E  --  Type (display) a file with various options...  */
  3443.  
  3444. #ifdef BIGBUFOK
  3445. #define TYPBUFL 16384
  3446. #else
  3447. #define TYPBUFL 256
  3448. #endif /* BIGBUFOK */
  3449.  
  3450. int typ_lines = 0;                      /* \v(ty_ln) */
  3451. int typ_mtchs = 0;                      /* \v(ty_lm) */
  3452. static int typ_int = 0;                 /* Flag if TYPE interrupted */
  3453.  
  3454. #ifdef UNICODE
  3455. extern int fcharset, fileorder, byteorder, ucsorder;
  3456. #define TYPXBUFL TYPBUFL+TYPBUFL+TYPBUFL+4
  3457. static char * mp = NULL;
  3458. static char * mbuf = NULL;
  3459. static long xn = 0L;
  3460.  
  3461. static int
  3462. #ifdef CK_ANSIC
  3463. storechar(char c)
  3464. #else
  3465. storechar(c) char c;
  3466. #endif /* CK_ANSIC */
  3467. {
  3468.     if (!mp) return(-1);
  3469.     if (++xn > TYPXBUFL)
  3470.       return(-1);
  3471.     debug(F111,"storechar xn",ckitoa((int)c),xn);
  3472.     *mp++ = c;
  3473.     return(0);
  3474. }
  3475. #endif /* UNICODE */
  3476.  
  3477. static FILE * ofp = NULL;               /* For /OUTPUT: file */
  3478.  
  3479. static int
  3480. typeline(buf,len,outcs,ofp) char * buf; int len, outcs; FILE * ofp; {
  3481.     register int i;
  3482.  
  3483.     debug(F011,"typeline buf",buf,len);
  3484.     /* debug(F101,"typeline outcs","",outcs); */
  3485.  
  3486. #ifdef OS2
  3487. #ifndef NOLOCAL
  3488. #ifdef UNICODE
  3489.     /* In K95 only, the buffer is guaranteed to be in UCS-2 if outcs >= 0. */
  3490.     /* Len is its length in bytes.  There is no line terminator. */
  3491.     /* outcs is the file character-set number (FC_xxx) of the target set */
  3492.     /* that was requested by the user. */
  3493.     if (!inserver && !k95stdout) {
  3494.         extern int wherex[], wherey[];
  3495.         extern unsigned char colorcmd;
  3496.  
  3497.         VscrnWrtUCS2StrAtt( VCMD, (unsigned short *)buf, len/2,
  3498.                            wherey[VCMD], wherex[VCMD], &colorcmd);
  3499.         printf("\r\n");
  3500.         return(0);
  3501.     }
  3502. #endif /* UNICODE */
  3503. #endif /* NOLOCAL */
  3504. #endif /* OS2 */
  3505.  
  3506. /* In Unix, VMS, etc, the line has already been converted to the desired  */
  3507. /* character-set, if one was given.  OR... on all platforms, including in */
  3508. /* K95, we don't know the character set.  In either case we dump the line */
  3509. /* byte by byte in case it contains NULs (printf() would truncate). */
  3510.  
  3511. #ifdef COMMENT
  3512.     for (i = 0; i < len; i++)
  3513.       putchar(buf[i]);
  3514. #else
  3515.     for (i = 0; i < len; i++) {
  3516.         if (ofp == stdout) {
  3517.             putchar(buf[i]);
  3518.         } else {
  3519.             putc(buf[i],ofp);
  3520.         }
  3521.     }
  3522. #endif /* COMMENT */
  3523.  
  3524. #ifdef IKSD
  3525.     if (inserver) {
  3526. #ifdef UNICODE
  3527.         if (outcs == FC_UCS2) {
  3528.             if (ofp == stdout) {
  3529.                 putchar(NUL);
  3530.             } else {
  3531.                 putc(NUL,ofp);
  3532.             }
  3533.         }
  3534. #endif /* UNICODE */
  3535.         if (ofp == stdout) {
  3536.             putchar('\r');
  3537.         } else {
  3538.             putc('\r',ofp);
  3539.         }
  3540.     }
  3541. #endif /* IKSD */
  3542. #ifdef UNICODE
  3543.     if (outcs == FC_UCS2) {
  3544.         if (ofp == stdout) {
  3545.             putchar(NUL);
  3546.         } else {
  3547.             putc(NUL,ofp);
  3548.         }
  3549.     }
  3550. #endif /* UNICODE */
  3551.     if (ofp == stdout) {
  3552.         putchar('\n');
  3553.     } else {
  3554.         putc('\n',ofp);
  3555.     }
  3556.     fflush(stdout);
  3557.     return(0);
  3558. }
  3559.  
  3560. static int                              /* Get translated line */
  3561. typegetline(incs, outcs, buf, n) int incs, outcs, n; char * buf; {
  3562.     int x = 0, c0, c1, len = 0, count = 0, eof = 0, xlate = 0;
  3563. #ifdef UNICODE
  3564.     int xxn = -1;
  3565.     int yyn = -9;
  3566.     xn = 0L;
  3567.  
  3568. #ifdef DEBUG
  3569.     if (deblog && typ_lines == 0) {
  3570.         debug(F101,"typegetline incs","",incs);
  3571.         debug(F101,"typegetline outcs","",outcs);
  3572.         debug(F101,"typegetline feol","",feol);
  3573.         debug(F101,"typegetline byteorder","",byteorder);
  3574.         debug(F101,"typegetline ucsorder ","",ucsorder);
  3575.         debug(F111,"typegetline fileorder","1",fileorder);
  3576.     }
  3577. #endif /* DEBUG */
  3578.  
  3579.     if (incs < 0)                       /* Shouldn't happen */
  3580.       return(-2);
  3581.  
  3582.     if (outcs == -1)                    /* Can happen */
  3583.       outcs = incs;
  3584.  
  3585.     if (incs != outcs || incs == FC_UCS2) { /* See if we should translate */
  3586.         xlate = 1;
  3587.         if (!mbuf) {                    /* Allocate buffer if not allocated */
  3588.             mbuf = (char *)malloc(TYPXBUFL+1); /* yet */
  3589.             if (!mbuf) {
  3590.                 printf("WARNING: Translation buffer allocation failure.\n");
  3591.                 printf("Translation will be skipped...\n");
  3592.                 xlate = 0;
  3593.             }
  3594.         }
  3595.     }
  3596.     if (xlate) {                        /* Translating... */
  3597.         mp = mbuf;                      /* Reset working buffer pointer */
  3598. /*
  3599.   Here we call xgnbyte() in a loop, having it return UCS-2 bytes.  In K95, we
  3600.   use UCS-2 directly.  Elsewhere, we feed the UCS-2 bytes into xpnbyte() to
  3601.   convert them to the desired target character set.  But since we are using
  3602.   UCS-2, we have several sources for confusion: (1) xgnbyte() might return in
  3603.   LE or BE byte order, with no explicit indication of what the order is; but
  3604.   (2) xpnbyte() wants BE; but (3) Windows wants LE.
  3605. */
  3606.         while (1) {
  3607.             if (typ_int)                /* Quit if interrupted */
  3608.               return(0);
  3609.             c0 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3610.             debug(F000,"typegetline c0","",c0);
  3611.             if (c0 < 0) {               /* EOF */
  3612.                 eof++;
  3613.                 break;
  3614.             }
  3615.             c1 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  3616.             debug(F000,"typegetline c1","",c1);
  3617.             if (c1 < 0) {               /* EOF */
  3618.                 eof++;
  3619.                 break;
  3620.             }
  3621. #ifdef DEBUG
  3622.             if (deblog && typ_lines == 0) {
  3623.                 if (count == 0) /* Check fileorder after BOM */
  3624.                   debug(F111,"typegetline fileorder","2",fileorder);
  3625.             }
  3626. #endif /* DEBUG */
  3627.  
  3628. #ifdef COMMENT
  3629. /* Now we have the two UCS-2 bytes.  Which order are they in? */
  3630.  
  3631.             if (fileorder > 0) {        /* Little Endian */
  3632.                 int t;                  /* So swap them */
  3633.                 debug(F100,"typegetline swapping","",0);
  3634.                 t = c1;
  3635.                 c1 = c0;
  3636.                 c0 = t;
  3637.             }
  3638. #endif /* COMMENT */
  3639.             if (c0 == 0 && c1 == 0x0D)  /* Now see if we have EOL */
  3640.               yyn = xn;
  3641.  
  3642.             if (c0 == 0 && c1 == 0x0A)  /* Now see if we have EOL */
  3643.               xxn = xn;
  3644.  
  3645.             count++;                    /* Count byte */
  3646.  
  3647. /* Give the two bytes to xpnbyte() in BE order */
  3648.  
  3649.             if ((x = xpnbyte(c0,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3650.             if ((x = xpnbyte(c1,TC_UCS2,outcs,storechar)) < 0) return(-1);
  3651.  
  3652.             if (xxn > -1) {             /* Have end of line? */
  3653.                 xn = xxn;
  3654.                 if (yyn == xxn - 2)     /* Adjust for CRLF */
  3655.                   xn = yyn;
  3656.                 break;                  /* And break out of loop. */
  3657.             }
  3658.         }
  3659.         mbuf[xn] = NUL;
  3660.         if (xn > n)                     /* Can truncate here... */
  3661.           xn = n;
  3662.         memcpy(buf,mbuf,xn);
  3663.         debug(F011,"typegetline xlate",buf,xn);
  3664.         return((eof && (xn == 0)) ? -1 : xn);
  3665.     }
  3666. #endif /* UNICODE */
  3667. #ifdef COMMENT
  3668.     /* We can't use this because, stupidly, zsinl() doesn't return a length. */
  3669.     /* It could be changed but then we'd have to change all ck?fio.c modules */
  3670.     x = zsinl(ZIFILE,buf,n);
  3671. #else
  3672.     /* So instead, we copy zsinl() to here... */
  3673.     /* But note: This does not necessarily handle UCS-2 alignment properly;  */
  3674.     /* that's what the code in the first section of this routine is for. */
  3675.     /* But it does tolerate files that contain NULs. */
  3676.     {
  3677.         int a;
  3678.         char *s;
  3679.  
  3680.         s = buf;
  3681.         a = -1;                         /* Current character, none yet. */
  3682.         debug(F101,"typegetline zsinl simulation","",n);
  3683.         while (n--) {                   /* Up to given length */
  3684. #ifdef COMMENT
  3685.             int old = 0;
  3686.             if (feol)                   /* Previous character */
  3687.               old = a;
  3688. #endif /* COMMENT */
  3689.             if (zchin(ZIFILE,&a) < 0) { /* Read a character from the file */
  3690.                 debug(F101,"typegetline zchin fail","",count);
  3691.                 if (count == 0)
  3692.                   x = -1;               /* EOF or other error */
  3693.                 break;
  3694.             } else
  3695.               count++;
  3696.             if (feol) {                 /* Single-character line terminator */
  3697.                 if (a == feol)
  3698.                   break;
  3699.             } else {                    /* CRLF line terminator */
  3700. #ifdef COMMENT
  3701. /* Debug log shows that in Windows, <CR><LF> is returned as <LF>. */
  3702. /* Apparently we're not reading the file in binary mode. */
  3703.  
  3704.                 if (a == '\015')        /* CR, get next character */
  3705.                   continue;
  3706.                 if (old == '\015') {    /* Previous character was CR */
  3707.                     if (a == '\012') {  /* This one is LF, so we have a line */
  3708.                         break;
  3709.                     } else {            /* Not LF, deposit CR */
  3710.                         *s++ = '\015';
  3711.                         n--;
  3712.                         len++;
  3713.                     }
  3714.                 }
  3715. #else
  3716.                 if (a == LF) {
  3717.                     if (s[len] == CR) { /* This probably won't happen */
  3718.                         s[len] = NUL;
  3719.                         s--;
  3720.                         len--;
  3721.                     }
  3722.                     break;
  3723.                 }
  3724. #endif /* COMMENT */
  3725.             }
  3726.             *s = a;                     /* Deposit character */
  3727.             s++;
  3728.             len++;
  3729.         }
  3730.         *s = '\0';                      /* Terminate the string */
  3731.     }
  3732. #endif /* COMMENT */
  3733.     return(x < 0 ? -1 : len);
  3734. }
  3735.  
  3736.  
  3737. #ifndef MAC
  3738. SIGTYP
  3739. #ifdef CK_ANSIC
  3740. tytrap(int foo)                         /* TYPE interrupt trap */
  3741. #else
  3742. tytrap(foo) int foo;
  3743. #endif /* CK_ANSIC */
  3744. /* tytrap */ {
  3745. #ifdef __EMX__
  3746.     signal(SIGINT, SIG_ACK);
  3747. #endif
  3748.     debug(F100,"type tytrap SIGINT","",0);
  3749.     typ_int = 1;                        /* (Need arg for ANSI C) */
  3750.     SIGRETURN;
  3751. }
  3752. #endif /* MAC */
  3753.  
  3754. int
  3755. dotype(file, paging, first, head, pat, width, prefix, incs, outcs, outfile, z)
  3756.     char * file, * pat, * prefix; int paging, first, head, width, incs, outcs;
  3757.     char * outfile; int z;
  3758. /* dotype */ {
  3759.     extern CK_OFF_T ffc;
  3760.     char buf[TYPBUFL+2];
  3761.     char * s = NULL;
  3762.     int rc = 1, lines = 0, ucs2 = 0;
  3763.     char ** tail = NULL;
  3764.     int * tlen = NULL;
  3765.     int tailing = 0, counting = 0;
  3766.     int x, c, n, i, j, k = 0;
  3767.     int number = 0, save, len, pfxlen = 0, evalpfx = 1;
  3768. #ifdef UNICODE
  3769.     int ucsbom_sav;
  3770.     extern int ucsbom;
  3771. #endif /* UNICODE */
  3772. #ifdef NT
  3773.     int gui = 0;
  3774. #endif /* NT */
  3775.  
  3776. #ifndef MAC
  3777. #ifdef OS2
  3778. #ifdef NT
  3779.     SIGTYP (* oldsig)(int);             /* For saving old interrupt trap. */
  3780. #else /* NT */
  3781.     SIGTYP (* volatile oldsig)(int);
  3782. #endif /* NT */
  3783. #else /* OS2 */
  3784.     SIGTYP (* oldsig)();
  3785. #endif /* OS2 */
  3786. #endif /* MAC */
  3787.  
  3788. #ifdef KUI
  3789.     if (outfile == (char *)1) {
  3790.         gui = 1;
  3791.         outfile = "";
  3792.     }
  3793. #endif /* KUI */
  3794.  
  3795.     if (!file) file = "";
  3796.     if (!*file) return(-2);
  3797.  
  3798.     if (ofp != stdout) {                /* In case of previous interruption */
  3799.         if (ofp) fclose(ofp);
  3800.         ofp = stdout;
  3801.     }
  3802.     if (!outfile) outfile = "";
  3803.     if (outfile[0]) {
  3804.         ofp = fopen(outfile,"w");       /* Open output file */
  3805.         if (!ofp) {
  3806.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  3807.             ofp = stdout;
  3808.             return(-9);
  3809.         }
  3810.     }
  3811.     number = z;
  3812.     if (number && prefix) prefix = NULL;
  3813.  
  3814. #ifdef UNICODE
  3815.     ucsbom_sav = ucsbom;                /* We are not creating a file */
  3816.     ucsbom = 0;                         /* Do not use BOM bytes */
  3817. #endif /* UNICODE */
  3818.  
  3819.     typ_int = 0;
  3820.  
  3821.     save = binary;                      /* Save file type */
  3822.  
  3823.     debug(F101,"dotype incs","",incs);
  3824.     debug(F101,"dotype outcs","",outcs);
  3825.  
  3826. #ifdef UNICODE
  3827.     debug(F111,"dotype fileorder","A",fileorder);
  3828. #ifdef OS2
  3829.     if (!inserver && !k95stdout)
  3830.       outcs = FC_UCS2;
  3831. #endif /* OS2 */
  3832.  
  3833.     if (outcs == FC_UCS2)               /* Output is UCS-2? */
  3834.       ucs2 = 1;
  3835.     if (fileorder < 0)
  3836.       fileorder = ucsorder;
  3837.     debug(F111,"dotype fileorder","B",fileorder);
  3838. #endif /* UNICODE */
  3839.  
  3840. #ifdef CK_TTGWSIZ
  3841. #ifdef OS2
  3842.     ttgcwsz();
  3843. #else /* OS2 */
  3844.     /* Check whether window size changed */
  3845.     if (ttgwsiz() > 0) {
  3846.         if (tt_rows > 0 && tt_cols > 0) {
  3847.             cmd_rows = tt_rows;
  3848.             cmd_cols = tt_cols;
  3849.             debug(F101,"dotype cmd_rows","",cmd_rows);
  3850.             debug(F101,"dotype cmd_cols","",cmd_cols);
  3851.         }
  3852.     }
  3853. #endif /* OS2 */
  3854. #endif /* CK_TTGWSIZ */
  3855.  
  3856.     if (prefix)
  3857.       pfxlen = strlen(prefix);
  3858.  
  3859.     if (paging < 0) {                   /* Count only, don't print */
  3860.         counting = 1;
  3861.         prefix = NULL;
  3862.         width = 0;
  3863.         paging = 0;
  3864.     }
  3865.     if (ucs2)                           /* Crude... */
  3866.       width *= 2;
  3867.  
  3868. #ifdef OS2
  3869.     if (*file) {
  3870.         ckstrncpy(buf, file, TYPBUFL);  /* Change / to \. */
  3871.         p = buf;
  3872.         while (*p) {
  3873.             if (*p == '/') *p = '\\';
  3874.             p++;
  3875.         }
  3876.         file = buf;
  3877.     } else {
  3878.         rc = 0;
  3879.         goto xdotype;
  3880.     }
  3881. #endif /* OS2 */
  3882.  
  3883.     if (zchki(file) == -2) {            /* It's a directory */
  3884.         debug(F111,"dotype zchki failure",file,-2);
  3885.         if (xcmdsrc == 0) {
  3886.             printf("?Not a regular file: \"%s\"\n",file);
  3887.             rc = -9;
  3888.         } else
  3889.           rc = 0;
  3890.         goto xdotype;
  3891.     }
  3892.     if (!zopeni(ZIFILE, file)) {        /* Not a directory, open it */
  3893.         debug(F111,"dotype zopeni failure",file,0);
  3894.         if (xcmdsrc == 0) {
  3895.             printf("?Can't open file: \"%s\"\n",file);
  3896.             rc = -9;
  3897.         } else
  3898.           rc = 0;
  3899.         goto xdotype;
  3900.     }
  3901.  
  3902. #ifndef AMIGA
  3903. #ifndef MAC
  3904.     errno = 0;
  3905.     oldsig = signal(SIGINT, tytrap);    /* Save current interrupt trap. */
  3906.     /* debug(F111,"type SIGINT trap set",ckitoa(errno),oldsig); */
  3907. #endif /* MAC */
  3908. #endif /* AMIGA */
  3909.  
  3910.     if (paging > -1)                    /* More-prompting */
  3911.       xaskmore = paging;
  3912.  
  3913.     binary = 0;
  3914.  
  3915.     if (head < 0) {                     /* "tail" was requested */
  3916.         tailing = 1;                    /* Set flag */
  3917.         head = 0 - head;                /* Get absolute number of lines */
  3918.         if (!counting) {
  3919.             tail = (char **) malloc(head * sizeof(char *)); /* Allocate list */
  3920.             if (!tail) {
  3921.                 printf("?Memory allocation failure\n");
  3922.                 goto xdotype;
  3923.  
  3924.             }
  3925.             tlen = (int *) malloc(head * sizeof(int));
  3926.             if (!tlen) {
  3927.                 printf("?Memory allocation failure\n");
  3928.                 goto xdotype;
  3929.  
  3930.             }
  3931.             for (i = 0; i < head; i++) { /* Initialize each pointer in list. */
  3932.                 tail[i] = NULL;
  3933.                 tlen[i] = 0;
  3934.             }
  3935.         }
  3936.     }
  3937.     typ_lines = 0;
  3938.     typ_mtchs = 0;
  3939.  
  3940. #ifdef UNICODE
  3941.     if (outcs > -1 && (incs != outcs || incs == FC_UCS2)) { /* Translating? */
  3942.         ffc = (CK_OFF_T)0;
  3943.         initxlate(incs,outcs);          /* Set up translation functions */
  3944.     } else
  3945. #endif /* UNICODE */
  3946.       outcs = -1;                       /* Means we don't know the charset */
  3947.  
  3948.     debug(F101,"dotype ffc","",ffc);
  3949.     debug(F101,"dotype outcs 2","",outcs);
  3950. #ifdef UNICODE
  3951.     debug(F111,"dotype fileorder","C",fileorder);
  3952. #endif /* UNICODE */
  3953.  
  3954.     /* Allow the buffer to contain NULs */
  3955.  
  3956.     for (n = first;
  3957.          (len = typegetline(incs,outcs,buf,TYPBUFL)) > -1;
  3958.          lines++
  3959.          ) {
  3960.         debug(F011,"dotype line",buf,len);
  3961. #ifndef MAC
  3962.         if (typ_int) {                  /* Interrupted? */
  3963.             typ_int = 0;
  3964.             debug(F101,"type interrupted line","",lines);
  3965.             printf("^C...\n");          /* Print message */
  3966.             if (ofp != stdout) {        /* Close any output file */
  3967.                 if (ofp) fclose(ofp);
  3968.                 ofp = stdout;
  3969.             }
  3970.             goto xxdotype;
  3971.         }
  3972. #endif /* MAC */
  3973.         typ_lines++;                    /* For \v(ty_ln) */
  3974.         if (pat)                        /* Matching? */
  3975.           if (!ckmatch(pat,buf,1,1+4))  /* Line matches pattern? */
  3976.             continue;                   /* No, skip it */
  3977.         typ_mtchs++;
  3978.  
  3979.         if (head > 0 && !tailing && lines == head) /* Handle /HEAD:n */
  3980.           break;
  3981.  
  3982.         buf[TYPBUFL+1] = NUL;           /* Just in case... */
  3983.         if (prefix) {                   /* Add specified prefix to each line */
  3984.             char pbuf[64];
  3985.             char * pp;
  3986.             pp = prefix;
  3987. #ifndef NOSPL
  3988.             if (evalpfx) {              /* Prefix is a variable? */
  3989.                 int n = 63;             /* Maybe - evaluate it and see */
  3990.                 char * p = pbuf;
  3991.                 zzstring(prefix,&p,&n); /* If there is no change */
  3992.                 if (!strcmp(prefix,pbuf)) { /* it's not a variable */
  3993.                     evalpfx = 0;        /* So don't do this again. */
  3994.                 } else {                /* It was a variable */
  3995.                     pp = pbuf;          /* So substitute its value */
  3996.                     pfxlen = 63 - n;    /* and get its new length */
  3997.                 }
  3998.             }
  3999. #endif /* NOSPL */
  4000.             if (len + pfxlen + 2 < TYPBUFL) {
  4001.                 /* Shift right to make room for prefix */
  4002.                 memcpy((char *)line+pfxlen,(char *)buf,len);
  4003.                 lset((char *)line,pp,pfxlen,SP);
  4004.                 debug(F110,"dotype prefix",line,pfxlen);
  4005.                 len += pfxlen;
  4006.                 memcpy((char *)buf,(char *)line,len);
  4007.             }
  4008.         } else if (number) {            /* Line numbers */
  4009.             int x;
  4010.             sprintf(line,"%4d. ",typ_lines);
  4011.             x = strlen(line);
  4012.             len += x;
  4013.             if (len < LINBUFSIZ) {
  4014.                 memcpy((char *)&line[x],(char *)buf,len);
  4015.                 memcpy((char *)buf,(char *)line,len);
  4016.             }
  4017.         }
  4018.         if (width > 0 && width <= TYPBUFL) { /* Truncate at given width. */
  4019.             char * obuf = line;         /* But to do that first we must */
  4020.             int i,k,z;                  /* expand tabs; assume every 8 cols. */
  4021.             line[0] = NUL;
  4022.             for (i = 0, k = 0; i < width; k++) { /* Character loop... */
  4023.                 if (!buf[k])            /* No more chars in this line, done. */
  4024.                   break;
  4025.                 if (buf[k] != '\t') {   /* If it's not a tab */
  4026.                     if (i >= LINBUFSIZ) /* Check for overflow */
  4027.                       break;
  4028.                     obuf[i++] = buf[k]; /* and then deposit it. */
  4029.                     obuf[i] = NUL;      /* Keep it null-terminated */
  4030.                     continue;
  4031.                 }
  4032.                 z = 8 - (i % 8);        /* It's a tab, expand it. */
  4033.                 if (z == 0) z = 8;
  4034.                 for (j = 0; j < z && i < LINBUFSIZ; j++) {
  4035. #ifdef UNICODE
  4036.                     if (ucs2 && !ucsorder)
  4037.                       obuf[i++] = NUL;
  4038. #endif /* UNICODE */
  4039.                     obuf[i++] = ' ';
  4040. #ifdef UNICODE
  4041.                     if (ucs2 && ucsorder)
  4042.                       obuf[i++] = NUL;
  4043. #endif /* UNICODE */
  4044.                 }
  4045.                 obuf[i++] = NUL;
  4046.                 obuf[i] = NUL;
  4047.             }
  4048.             obuf[width] = NUL;          /* Now truncate at given width. */
  4049. #ifdef COMMENT
  4050.             /* This doesn't work for UCS-2 because it contains NULs */
  4051.             ckstrncpy(buf,obuf,TYPBUFL); /* and copy it back (again?) */
  4052. #else
  4053.             memcpy((char *)buf,(char *)obuf,i); /* Copy it back */
  4054. #endif /* COMMENT */
  4055.             len = (i > width) ? width : i; /* Spare us another strlen()... */
  4056.         }
  4057.         if (tailing) {                  /* If /TAIL:n... */
  4058.             k = lines % head;           /* save this line in circular buffer */
  4059.             if (!counting) {
  4060.                 if (tail[k]) free(tail[k]);
  4061.                 tail[k] = malloc(len+2);
  4062.                 if (!tail[k]) {
  4063.                     printf("?Memory allocation failure\n");
  4064.                     goto xdotype;
  4065.                 }
  4066.                 memcpy(tail[k],buf,len);
  4067.                 tlen[k] = len;
  4068.                 continue;
  4069.             }
  4070.         }
  4071.         if (counting)                   /* If only counting */
  4072.           continue;                     /* we're done with this line */
  4073.  
  4074.         if (paging) {                   /* Displaying this line... */
  4075.             int u;
  4076.             u = len;                    /* Length in BYTES */
  4077.             if (ucs2)                   /* If outputting in UCS-2 */
  4078.               u /= 2;                   /* convert length to CHARACTERS */
  4079.             x = (u / cmd_cols) + 1;     /* Crudely allow for wrap */
  4080.             if (cmd_rows > 0 && cmd_cols > 0)
  4081.               n += x;                   /* This assumes terminal will wrap */
  4082.         }
  4083. #ifdef KUI
  4084.         if ( gui ) {
  4085.             int i;
  4086.             unsigned short * uch = (unsigned short *)buf;
  4087.             for ( i=0; i<len/2; i++)
  4088.                 gui_text_popup_append(uch[i]);
  4089.             gui_text_popup_append(CR);
  4090.             gui_text_popup_append(LF);
  4091.         } 
  4092.         else
  4093. #endif /* KUI */
  4094.         typeline(buf,len,outcs,ofp);    /* Print line, length based */
  4095. #ifdef CK_TTGWSIZ
  4096.         debug(F101,"dotype n","",n);
  4097.         if (paging > 0 && ofp == stdout) { /* Pause at end of screen */
  4098.             if (cmd_rows > 0 && cmd_cols > 0) {
  4099.                 if (n > cmd_rows - 3) {
  4100.                     if (!askmore())
  4101.                       goto xdotype;
  4102.                     else
  4103.                       n = 0;
  4104.                 }
  4105.             }
  4106.         }
  4107. #endif /* CK_TTGWSIZ */
  4108.     }
  4109.  
  4110.   xdotype:
  4111.     if (counting) {
  4112.         fprintf(ofp,
  4113.                 "%s: %d line%s\n",file,typ_lines,typ_lines == 1 ? "" : "s");
  4114.         if (pat)
  4115.           fprintf(ofp,
  4116.                   "%s: %d match%s\n",pat,typ_mtchs,typ_mtchs == 1 ? "" : "es");
  4117.         goto xxdotype;
  4118.     }
  4119.     if (tailing && tail) {              /* Typing tail of file? */
  4120.         if (lines < head) {             /* Yes, show the lines we saved */
  4121.             k = 0;                      /* Show all lines */
  4122.         } else {                        /* More lines than tail number */
  4123.             lines = k;                  /* Last line to show */
  4124.             k++;                        /* First line to show */
  4125.             if (k >= head)
  4126.               k = 0;
  4127.         }
  4128.         n = first;                      /* Output line counter */
  4129.         for (i = k ;; i++) {            /* Loop thru circular buffer */
  4130. #ifndef MAC
  4131.             if (typ_int) {              /* Interrupted? */
  4132.                 printf("^C...\n");      /* Print message */
  4133.                 goto xxdotype;
  4134.             }
  4135. #endif /* MAC */
  4136.             j = i % head;               /* Index of this line */
  4137.             s = tail[j];                /* Point to line to display */
  4138.             if (!s)                     /* (shouldn't happen...) */
  4139.               break;
  4140.             if (paging) {               /* Crudely allow for line wrap */
  4141.                 x = tlen[j];
  4142.                 if (ucs2) x /= 2;
  4143.                 x = x / cmd_cols + 1;
  4144.                 if (cmd_rows > 0 && cmd_cols > 0)
  4145.                   n += x;
  4146.             }
  4147.             typeline(s,tlen[j],outcs,ofp); /* Display this line */
  4148.             if (paging && ofp == stdout) { /* Pause at end of screen */
  4149.                 if (cmd_rows > 0 && cmd_cols > 0) {
  4150.                     if (n > cmd_rows - 3) {
  4151.                         if (!askmore())
  4152.                           break;
  4153.                         else
  4154.                           n = 0;
  4155.                     }
  4156.                 }
  4157.             }
  4158.             tail[j] = NULL;
  4159.             free(s);                    /* Free the line */
  4160.             if (i % head == lines)      /* When to stop */
  4161.               break;
  4162.         }
  4163.         free((char *)tail);             /* Free the list */
  4164.         tail = NULL;
  4165.         if (tlen) free((char *)tlen);
  4166.         tlen = NULL;
  4167.     }
  4168.  
  4169. /* Come here when finished or on SIGINT */
  4170.  
  4171.   xxdotype:
  4172. #ifndef AMIGA
  4173. #ifndef MAC
  4174.     signal(SIGINT,oldsig);              /* Put old signal action back. */
  4175. #endif /* MAC */
  4176. #endif /* AMIGA */
  4177.     if (tailing && tail) {
  4178.         for (i = 0; i < head; i++) {    /* Free each line. */
  4179.             if (tail[i])
  4180.               free(tail[i]);
  4181.         }
  4182.         free((char *)tail);             /* Free list pointer */
  4183.         if (tlen)
  4184.           free((char *)tlen);
  4185.     }
  4186.     x = zclose(ZIFILE);                 /* Done, close the input file */
  4187.     if (ofp != stdout) {                /* Close any output file */
  4188.         if (ofp) fclose(ofp);
  4189.         ofp = stdout;
  4190.     }
  4191.     binary = save;                      /* Restore text/binary mode */
  4192. #ifdef UNICODE
  4193.     ucsbom = ucsbom_sav;                /* Restore BOM usage */
  4194. #endif /* UNICODE */
  4195.  
  4196. #ifdef KUI
  4197.     if ( gui )
  4198.         gui_text_popup_wait(-1);        /* Wait for user to close the dialog */
  4199. #endif /* KUI */
  4200.     return(rc);
  4201. }
  4202.  
  4203. /* GREP command */
  4204.  
  4205. #define GREP_CASE  0                    /* /CASE */
  4206. #define GREP_COUN  1                    /* /COUNT */
  4207. #define GREP_DOTF  2                    /* /DOTFILES */
  4208. #define GREP_NAME  3                    /* /NAMEONLY */
  4209. #define GREP_NOBK  4                    /* /NOBACKUP */
  4210. #define GREP_NODO  5                    /* /NODOTFILES */
  4211. #define GREP_NOLI  6                    /* /NOLIST */
  4212. #define GREP_NOMA  7                    /* /INVERT = /NOMATCH */
  4213. #define GREP_NOPA  8                    /* /NOPAGE */
  4214. #define GREP_NUMS  9                    /* /LINENUMBERS */
  4215. #define GREP_PAGE 10                    /* /PAGE */
  4216. #define GREP_RECU 11                    /* /RECURSIVE */
  4217. #define GREP_TYPE 12                    /* /TYPE: */
  4218. #define GREP_OUTP 13                    /* /OUTPUTFILE: */
  4219. #define GREP_EXCP 14            /* /EXCEPT: */
  4220.  
  4221. static struct keytab greptab[] = {
  4222.     { "/count",        GREP_COUN, CM_ARG },
  4223.     { "/dotfiles",     GREP_DOTF, 0 },
  4224.     { "/except",       GREP_EXCP, CM_ARG },
  4225.     { "/linenumbers",  GREP_NUMS, 0 },
  4226.     { "/nameonly",     GREP_NAME, 0 },
  4227.     { "/nobackupfiles",GREP_NOBK, 0 },
  4228.     { "/nocase",       GREP_CASE, 0 },
  4229.     { "/nodotfiles",   GREP_NODO, 0 },
  4230.     { "/nolist",       GREP_NOLI, 0 },
  4231.     { "/nomatch",      GREP_NOMA, 0 },
  4232.     { "/nopage",       GREP_NOPA, 0 },
  4233.     { "/output",       GREP_OUTP, CM_ARG },
  4234.     { "/page",         GREP_PAGE, 0 },
  4235.     { "/quiet",        GREP_NOLI, CM_INV },
  4236. #ifdef RECURSIVE
  4237.     { "/recursive",    GREP_RECU, 0 },
  4238. #endif /* RECURSIVE */
  4239.     { "/type",         GREP_TYPE, CM_ARG },
  4240.     { "", 0, 0 }
  4241. };
  4242. static int ngreptab =  sizeof(greptab)/sizeof(struct keytab)-1;
  4243.  
  4244. static char * grep_except = NULL;
  4245.  
  4246. int
  4247. dogrep() {
  4248.     int match, x, y, fc, getval, mc = 0, count = 0, bigcount = 0;
  4249.     int fline = 0, sline = 0, wild = 0, len = 0;
  4250.     int xmode = -1, scan = 0;
  4251.     char c, name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p, *s, *cv = NULL;
  4252.     FILE * fp = NULL;
  4253.  
  4254.     int                                 /* Switch values and defaults */
  4255.       gr_coun = 0,
  4256.       gr_name = 0,
  4257.       gr_nobk = 0,
  4258.       gr_case = 1,
  4259.       gr_noli = 0,
  4260.       gr_noma = 0,
  4261.       gr_nums = 0,
  4262.       gr_excp = 0,
  4263.       gr_page = xaskmore;
  4264.  
  4265.     struct FDB sw, fl;
  4266.  
  4267.     g_matchdot = matchdot;              /* Save global matchdot setting */
  4268.     outfile[0] = NUL;
  4269.     makestr(&grep_except,NULL);
  4270.  
  4271.     if (ofp != stdout) {                /* In case of previous interruption */
  4272.         if (ofp) fclose(ofp);
  4273.         ofp = stdout;
  4274.     }
  4275.     cmfdbi(&sw,                         /* First FDB - command switches */
  4276.            _CMKEY,                      /* fcode */
  4277.            "String or pattern to search for, or switch",
  4278.            "",                          /* default */
  4279.            "",                          /* addtl string data */
  4280.            ngreptab,                    /* addtl numeric data 1: tbl size */
  4281.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4282.            xxstring,                    /* Processing function */
  4283.            greptab,                     /* Keyword table */
  4284.            &fl                          /* Pointer to next FDB */
  4285.            );
  4286.     cmfdbi(&fl,                         /* Anything that doesn't match */
  4287.            _CMFLD,                      /* fcode */
  4288.            "",                          /* hlpmsg */
  4289.            "",                          /* default */
  4290.            "",                          /* addtl string data */
  4291.            0,                           /* addtl numeric data 1 */
  4292.            0,                           /* addtl numeric data 2 */
  4293.            xxstring,            /* xxstring */
  4294.            NULL,
  4295.            NULL
  4296.            );
  4297.     while (1) {                         /* Parse 0 or more switches */
  4298.         x = cmfdb(&sw);                 /* Parse something */
  4299.         if (x < 0)
  4300.           return(x);
  4301.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  4302.           break;
  4303.         c = cmgbrk();
  4304.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4305.             printf("?This switch does not take an argument\n");
  4306.             return(-9);
  4307.         }
  4308.         if ((cmresult.nresult != GREP_COUN) && !getval &&
  4309.             (cmgkwflgs() & CM_ARG)) {
  4310.             printf("?This switch requires an argument\n");
  4311.             return(-9);
  4312.         }
  4313.         switch (cmresult.nresult) {
  4314.           case GREP_COUN: {
  4315.               gr_coun++;
  4316.               if (getval) {
  4317.                   if ((x = cmfld("Variable for result","",&s,NULL)) < 0)
  4318.                     return(x);
  4319.                   makestr(&cv,s);
  4320.               }
  4321.               break;
  4322.           }
  4323.           case GREP_CASE: gr_case=0; break;
  4324.           case GREP_NAME: gr_name++; gr_noli=0; break;
  4325.           case GREP_NOBK: gr_nobk++; break;
  4326.           case GREP_NOLI: gr_noli++; gr_name=0; gr_nums=0; break;
  4327.           case GREP_NOMA: gr_noma++; break;
  4328.           case GREP_NOPA: gr_page=0; break;
  4329.           case GREP_NUMS: gr_nums++; gr_noli=0; break;
  4330.           case GREP_PAGE: gr_page++; gr_noli=0; break;
  4331.           case GREP_NODO:
  4332.             matchdot = 0;
  4333.             break;
  4334.           case GREP_DOTF:
  4335.             matchdot = 1;
  4336.             break;
  4337. #ifdef RECURSIVE
  4338.           case GREP_RECU:
  4339.             recursive = 1;
  4340.             break;
  4341. #endif /* RECURSIVE */
  4342.           case GREP_TYPE: {
  4343.               extern struct keytab txtbin[];
  4344.               if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  4345.                 return(x);
  4346.               if (x == 2) {             /* ALL */
  4347.                   xmode = -1;
  4348.               } else {                  /* TEXT or BINARY only */
  4349.                   xmode = x;
  4350.                   scan = 1;
  4351.               }
  4352.               break;
  4353.           }
  4354.           case GREP_OUTP:               /* Send output to file */
  4355.             if ((x = cmofi("File for GREP'd lines","",&s,xxstring)) < 0)
  4356.               return(x);
  4357.             ckstrncpy(outfile,s,CKMAXPATH);
  4358.             break;
  4359.       case GREP_EXCP:        /* Exception pattern */
  4360.         if (getval) {
  4361.         if ((x = cmfld("Exception pattern",
  4362.                    "",
  4363.                    &s,
  4364.                    xxstring
  4365.                    )) < 0)
  4366.           return(x);
  4367.         gr_excp++;
  4368.         makestr(&grep_except,s);
  4369.         }
  4370.         }
  4371.     }
  4372.     if (outfile[0]) {
  4373.         ofp = fopen(outfile,"w");       /* Open output file */
  4374.         if (!ofp) {
  4375.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  4376.             ofp = stdout;
  4377.             return(-9);
  4378.         }
  4379.         gr_page = 0;
  4380.     }
  4381.     s = cmresult.sresult;
  4382.     s = brstrip(s);                     /* Strip braces from pattern */
  4383.     if (!*s) {
  4384.         printf("?Pattern required\n");
  4385.         return(-9);
  4386.     }
  4387.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);      /* Save pattern */
  4388.     if ((x = cmifi("File(s) to search","",&s,&wild,xxstring)) < 0) {
  4389.         if (x == -3) {
  4390.             printf("?File specification required\n");
  4391.             x = -9;
  4392.         }
  4393.         return(x);
  4394.     }
  4395.     s = brstrip(s);                     /* Strip braces from filename */
  4396. #ifndef ZXREWIND
  4397.     ckstrncpy(line,s,LINBUFSIZ);
  4398. #endif /* ZXREWIND */
  4399.     if ((y = cmcfm()) < 0)
  4400.       return(y);
  4401.  
  4402.     if (gr_page > -1)
  4403.       xaskmore = gr_page;               /* Paging... */
  4404.  
  4405.     p = tmpbuf;                         /* Point to pattern */
  4406. #ifdef COMMENT
  4407. /* Now this is done in ckmatch */
  4408.     if (*p == '^') {                    /* '^' anchors pattern to beginning */
  4409.         p++;
  4410.     } else if (*p != '*') {             /* Otherwise prepend implied '*' */
  4411.         tmpbuf[0] = '*';
  4412.         p = tmpbuf;
  4413.     }
  4414.     x = strlen(p);                      /* Get length of result */
  4415.     if (x > 0 && x < TMPBUFSIZ) {       /* '$' at end anchors pattern to end */
  4416.         if (p[x-1] == '$') {
  4417.             p[x-1] = NUL;
  4418.         } else if (p[x-1] != '*') {
  4419.             p[x] = '*';
  4420.             p[x+1] = NUL;
  4421.         }
  4422.     }
  4423. #endif /* COMMENT */
  4424.     debug(F111,"grep pat",p,x);
  4425.  
  4426. #ifdef ZXREWIND
  4427.     fc = zxrewind();                    /* Rewind the file list */
  4428. #else
  4429.     {
  4430.         int flags = ZX_FILONLY;         /* Expand file list */
  4431.         if (matchdot)  flags |= ZX_MATCHDOT;
  4432.         if (recursive) flags |= ZX_RECURSE;
  4433.         fc = nzxpand(line,flags);
  4434.     }
  4435. #endif /* ZXREWIND */
  4436. #ifdef UNIX
  4437.     sh_sort(mtchs,NULL,fc,0,0,filecase);
  4438. #endif /* UNIX */
  4439.  
  4440.     debug(F101,"grep cmd_rows","",cmd_rows);
  4441.     debug(F101,"grep cmd_cols","",cmd_cols);
  4442.  
  4443.     while (1) {                         /* Loop for each file */
  4444.         znext(name);                    /* Get next file */
  4445.         if (!name[0])                   /* No more, done */
  4446.           break;
  4447.         if (gr_nobk)                    /* Skipping backup files? */
  4448.           if (ckmatch("*.~[1-9]*~",name,1,1)) /* Backup file? */
  4449.             continue;                   /* Yes, skip */
  4450.         if (scan) {                     /* /TYPE: given? */
  4451.             switch (scanfile(name,&y,nscanfile)) { /* Yes, scan the file */
  4452.               case FT_BIN:
  4453.                 if (xmode != 1)
  4454.                   continue;
  4455.                 break;
  4456.               case FT_TEXT:
  4457.               case FT_7BIT:
  4458.               case FT_8BIT:
  4459. #ifdef UNICODE
  4460.               case FT_UTF8:
  4461.               case FT_UCS2:
  4462. #endif /* UNICODE */
  4463.                 if (xmode != 0)
  4464.                   continue;
  4465.             }
  4466.         }
  4467.         fp = fopen(name,"r");           /* Open */
  4468.         if (!fp)                        /* Can't */
  4469.           continue;                     /* Skip */
  4470.         count = 0;                      /* Match count, this file */
  4471.         fline = 0;                      /* Line count, this file */
  4472.         while (1) {                     /* Loop for each line */
  4473.             if (fgets(line,LINBUFSIZ,fp) == NULL) { /* Get next line */
  4474.                 fclose(fp);
  4475.                 fp = NULL;
  4476.         debug(F100,"GREP EOF","",0);
  4477.                 break;
  4478.             }
  4479.             fline++;                    /* Count this line */
  4480.             line[LINBUFSIZ] = NUL;      /* Make sure it's terminated */
  4481.         debug(F111,"GREP",line,fline);
  4482.             len = (int)strlen(line);    /* Get length */
  4483.             while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
  4484.               line[--len] = NUL;        /* Chop off terminators */
  4485.             match = ckmatch(p,line,gr_case,1+4); /* Match against pattern */
  4486.         if (match && gr_excp) {
  4487.         if (ckmatch(grep_except,line,gr_case,1+4))
  4488.             match = 0;
  4489.         }
  4490.             if (gr_noma)                /* Invert match sense if requested */
  4491.               match = !match;
  4492.             if (match) {                /* Have a matching line */
  4493.                 mc++;                   /* Total match count */
  4494.                 count++;                /* Match count this file */
  4495.                 if (gr_name) {          /* Don't care how many lines match */
  4496.                     fclose(fp);         /* Close the file */
  4497.                     fp = NULL;          /* and quit the line-reading loop. */
  4498.                     break;
  4499.                 }
  4500.                 if (gr_coun || gr_noli) /* Not listing each line */
  4501.                   continue;             /* so don't print anything now. */
  4502.                 if (wild) {        /* If searching multiple files */
  4503.                     fprintf(ofp,"%s:",name); /* print filename. */
  4504.                     len += (int)strlen(name) + 1;
  4505.                 }
  4506.                 if (gr_nums) {          /* If line numbers wanted */
  4507.                     char nbuf[32];
  4508.                     len += ckmakmsg(nbuf,32,ckitoa(fline),":",NULL,NULL);
  4509.                     fprintf(ofp,"%s",nbuf);
  4510.                 }
  4511.                 if (cmd_rows > 0 && cmd_cols > 0)
  4512.                   sline += (len / cmd_cols) + 1;
  4513.                 fprintf(ofp,"%s\n",line); /* Print the line. */
  4514.                 if (sline > cmd_rows - 3) {
  4515.                     if (!askmore()) goto xgrep; else sline = 0;
  4516.                 }
  4517.             }
  4518.         }
  4519.         if (!gr_noli) {            /* If not not listing... */
  4520.             x = 0;
  4521.             if (gr_coun) {              /* Show match count only */
  4522.                 fprintf(ofp,"%s:%d\n",name,count);
  4523.                 x++;
  4524.             } else if (gr_name && count > 0) { /* Show name only */
  4525.                 fprintf(ofp,"%s\n",name);
  4526.                 x++;
  4527.             }
  4528.             if (x > 0) {
  4529.                 if (++sline > cmd_rows - 3) {
  4530.                     if (!askmore()) goto xgrep; else sline = 0;
  4531.                 }
  4532.             }
  4533.         }
  4534.         bigcount += count;              /* Overall count */
  4535.     }
  4536.   xgrep:
  4537. #ifndef NOSPL
  4538.     if (gr_coun && cv) {                /* /COUNT:blah */
  4539.         addmac(cv,ckitoa(bigcount));    /* set the variable */
  4540.         makestr(&cv,NULL);              /* free this */
  4541.     }
  4542. #endif /* NOSPL */
  4543.     if (fp) fclose(fp);                 /* close input file if still open */
  4544.     if (ofp != stdout) {                /* Close any output file */
  4545.         if (ofp) fclose(ofp);
  4546.         ofp = stdout;
  4547.     }
  4548.     return(success = mc ? 1 : 0);
  4549. }
  4550.  
  4551. /* System-independent directory */
  4552.  
  4553. static char ** dirlist = NULL;
  4554. static int ndirlist = 0;
  4555.  
  4556. static VOID
  4557. freedirlist() {
  4558.     if (dirlist) {
  4559.         int i;
  4560.         for (i = 0; i < ndirlist; i++) {
  4561.             if (dirlist[i])
  4562.               free(dirlist[i]);
  4563.         }
  4564.         free((char *)dirlist);
  4565.         dirlist = NULL;
  4566.     }
  4567.     ndirlist = 0;
  4568. }
  4569.  
  4570. static struct keytab dirswtab[] = {     /* DIRECTORY command switches */
  4571.     { "/after",       DIR_AFT, CM_ARG },
  4572.     { "/all",         DIR_ALL, 0 },
  4573. #ifndef NOSPL
  4574.     { "/array",       DIR_ARR, CM_ARG },
  4575. #endif /* NOSPL */
  4576.     { "/ascending",   DIR_ASC, 0 },
  4577.     { "/backup",      DIR_BUP, 0 },
  4578.     { "/before",      DIR_BEF, CM_ARG },
  4579.     { "/brief",       DIR_BRF, 0 },
  4580.     { "/count",       DIR_COU, CM_ARG },
  4581.     { "/descending",  DIR_DSC, CM_INV },
  4582.     { "/directories", DIR_DIR, 0 },
  4583.     { "/dotfiles",    DIR_DOT, 0 },
  4584.     { "/englishdate", DIR_DAT, 0 },
  4585.     { "/except",      DIR_EXC, CM_ARG },
  4586.     { "/files",       DIR_FIL, 0 },
  4587.     { "/heading",     DIR_HDG, 0 },
  4588.     { "/isodate",     DIR_ISO, 0 },
  4589.     { "/larger-than", DIR_LAR, CM_ARG },
  4590. #ifdef CKSYMLINK
  4591.     { "/followlinks", DIR_LNK, 0 },
  4592. #endif /* CKSYMLINK */
  4593.     { "/message",     DIR_MSG, CM_ARG },
  4594.     { "/nobackupfiles",DIR_NOB, 0 },
  4595.     { "/nodotfiles",  DIR_NOD, 0 },
  4596. #ifdef CKSYMLINK
  4597.     { "/nofollowlinks",DIR_NLK, 0 },
  4598. #endif /* CKSYMLINK */
  4599.     { "/noheading",   DIR_NOH, 0 },
  4600. #ifdef CKSYMLINK
  4601.     { "/nolinks",     DIR_NOL, 0 },
  4602. #endif /* CKSYMLINK */
  4603.     { "/nomessage",   DIR_NOM, 0 },
  4604. #ifdef CK_TTGWSIZ
  4605.     { "/nopage",      DIR_NOP, 0 },
  4606. #endif /* CK_TTGWSIZ */
  4607. #ifdef RECURSIVE
  4608.     { "/norecursive", DIR_NOR, 0 },
  4609. #else
  4610. #ifdef VMS
  4611.     { "/norecursive", DIR_NOR, 0 },
  4612. #else
  4613. #ifdef datageneral
  4614.     { "/norecursive", DIR_NOR, 0 },
  4615. #endif /* datageneral */
  4616. #endif /* VMS */
  4617. #endif /* RECURSIVE */
  4618.     { "/nosort",      DIR_NOS, 0 },
  4619.     { "/not-after",   DIR_NAF, CM_ARG },
  4620.     { "/not-before",  DIR_NBF, CM_ARG },
  4621.     { "/not-since",   DIR_NAF, CM_INV|CM_ARG },
  4622.     { "/noxfermode",  DIR_NOT, 0 },
  4623.     { "/output",      DIR_OUT, CM_ARG },
  4624. #ifdef CK_TTGWSIZ
  4625.     { "/page",        DIR_PAG, 0 },
  4626. #endif /* CK_TTGWSIZ */
  4627. #ifdef RECURSIVE
  4628.     { "/recursive",   DIR_REC, 0 },
  4629. #else
  4630. #ifdef VMS
  4631.     { "/recursive",   DIR_REC, 0 },
  4632. #else
  4633. #ifdef datageneral
  4634.     { "/recursive",   DIR_REC, 0 },
  4635. #endif /* datageneral */
  4636. #endif /* VMS */
  4637. #endif /* RECURSIVE */
  4638.     { "/reverse",     DIR_DSC, 0 },
  4639.     { "/since",       DIR_AFT, CM_ARG|CM_INV },
  4640.     { "/smaller-than",DIR_SMA, CM_ARG },
  4641.     { "/sort",        DIR_SRT, CM_ARG },
  4642.     { "/summary",     DIR_SUM, 0 },
  4643.     { "/top",         DIR_TOP, CM_ARG },
  4644.     { "/type",        DIR_BIN, CM_ARG },
  4645.     { "/xfermode",    DIR_TYP, 0 },
  4646.     { "/verbose",     DIR_VRB, 0 },
  4647.     { "",0,0 }
  4648. };
  4649. static int ndirswtab = (sizeof(dirswtab) / sizeof(struct keytab)) - 1;
  4650.  
  4651. static struct keytab dirsort[] = {      /* DIRECTORY /SORT: options */
  4652.     { "date",         DIRS_DT, 0 },
  4653.     { "name",         DIRS_NM, 0 },
  4654.     { "size",         DIRS_SZ, 0 }
  4655. };
  4656. static int ndirsort = (sizeof(dirsort) / sizeof(struct keytab));
  4657.  
  4658. static int dir_date = -1;               /* Option defaults (-1 means none) */
  4659. static int dir_page = -1;
  4660. static int dir_verb =  1;
  4661. static int dir_msg  = -1;
  4662. #ifdef VMS
  4663. static int dir_sort = -1;               /* Names are already sorted in VMS */
  4664. static int dir_rvrs = -1;
  4665. #else
  4666. static int dir_sort =  1;               /* Sort by default */
  4667. static int dir_rvrs =  0;               /* Not in reverse */
  4668. #endif /* VMS */
  4669. static int dir_skey = DIRS_NM;          /* By name */
  4670. #ifdef RECURSIVE
  4671. static int dir_recu = -1;
  4672. #endif /* RECURSIVE */
  4673. static int dir_mode = -1;
  4674. static int dir_show = -1;               /* Show all files by default */
  4675. int dir_dots =  -1;            /* Except dot files */
  4676. int dir_back =  1;
  4677. int dir_head =  0;
  4678. static char * dirmsg = NULL;
  4679. static int dirmsglen = 0;
  4680.  
  4681. #ifndef NOSHOW
  4682. VOID
  4683. showdiropts() {
  4684.     int x = 0;
  4685.     extern int optlines;
  4686.     prtopt(&optlines,"DIRECTORY");
  4687.     if (dir_show > 0) {
  4688.         prtopt(&optlines,(dir_show == 1) ? "/FILES" :
  4689.                ((dir_show == 2) ? "/DIRECTORIES" : "/ALL"));
  4690.         x++;
  4691.     } else {
  4692.         prtopt(&optlines,"/ALL");
  4693.         x++;
  4694.     }
  4695.     if (dir_verb > -1) {
  4696.         prtopt(&optlines,dir_verb ? "/VERBOSE" : "/BRIEF");
  4697.         x++;
  4698.     }
  4699.     if (dir_page > -1) {
  4700.         prtopt(&optlines,dir_page ? "/PAGE" : "/NOPAGE");
  4701.         x++;
  4702.     }
  4703.     if (dir_date > -1) {
  4704.         prtopt(&optlines,dir_date ? "/ENGLISHDATE" : "/ISODATE");
  4705.         x++;
  4706.     }
  4707.     if (dir_dots > -1) {
  4708.         prtopt(&optlines,dir_dots ? "/DOTFILES" : "/NODOTFILES");
  4709.         x++;
  4710.     }
  4711.     if (dir_back > -1) {
  4712.         prtopt(&optlines,dir_back ? "/BACKUP" : "/NOBACKUP");
  4713.         x++;
  4714.     }
  4715.     if (dir_head > -1) {
  4716.         prtopt(&optlines,dir_head ? "/HEADING" : "/NOHEADING");
  4717.         x++;
  4718.     }
  4719. #ifdef RECURSIVE
  4720.     if (dir_recu > -1) {
  4721.         prtopt(&optlines,dir_recu ? "/RECURSIVE" : "/NORECURSIVE");
  4722.         x++;
  4723.     }
  4724. #endif /* RECURSIVE */
  4725.     if (dir_mode > -1) {
  4726.         prtopt(&optlines,dir_mode ? "/XFERMODE" : "/NOXFERMODE");
  4727.         x++;
  4728.     }
  4729.     if (dir_sort == 0) {
  4730.         x++;
  4731.         prtopt(&optlines,"/NOSORT ");
  4732.     } else if (dir_sort > 0) {
  4733.         x++;
  4734.         if (dir_skey == DIRS_NM) s = "/SORT:NAME";
  4735.         else if (dir_skey == DIRS_SZ) s = "/SORT:SIZE";
  4736.         else if (dir_skey == DIRS_DT) s = "/SORT:DATE";
  4737.         prtopt(&optlines,s);
  4738.     }
  4739.     if (dir_rvrs > -1) {
  4740.         prtopt(&optlines,dir_rvrs ? "/REVERSE" : "/ASCENDING");
  4741.         x++;
  4742.     }
  4743.     if (dir_msg > -1) {
  4744.         if (dir_msg == 0) {
  4745.             prtopt(&optlines,"/NOMESSAGE");
  4746.         } else {
  4747.             ckmakmsg(tmpbuf,TMPBUFSIZ,"/MESSAGE:{",dirmsg,"}",NULL);
  4748.             prtopt(&optlines,tmpbuf);
  4749.         }
  4750.         x++;
  4751.     }
  4752.     if (!x) prtopt(&optlines,"(no options set)");
  4753.     prtopt(&optlines,"");
  4754. }
  4755. #endif /* NOSHOW */
  4756.  
  4757. int
  4758. setdiropts() {                          /* Set DIRECTORY option defaults */
  4759.     int xb = -1, xv = -1, xp = -1, xd = -1, xh = -1, xf = -1;
  4760.     int xk = -1, xr = -1, xs = -1, xx = -1, xm = -1, xa = -1, xg = -1;
  4761.     int getval;
  4762.     char c;
  4763.     while (1) {
  4764.         if ((y = cmswi(dirswtab,ndirswtab,"Switch","",xxstring)) < 0) {
  4765.             if (y == -3)
  4766.               break;
  4767.             else
  4768.               return(y);
  4769.         }
  4770.         c = cmgbrk();
  4771.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  4772.             printf("?This switch does not take an argument\n");
  4773.             return(-9);
  4774.         }
  4775.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4776.             printf("?This switch requires an argument\n");
  4777.             return(-9);
  4778.         }
  4779.         switch (y) {
  4780.           case DIR_BRF: xv = 0; break;
  4781.           case DIR_VRB: xv = 1; break;
  4782.           case DIR_PAG: xp = 1; break;
  4783.           case DIR_NOP: xp = 0; break;
  4784.           case DIR_ISO: xd = 0; break;
  4785.           case DIR_DAT: xd = 1; break;
  4786.           case DIR_HDG: xh = 1; break;
  4787.           case DIR_NOH: xh = 0; break;
  4788.           case DIR_DOT: xf = 1; break;
  4789.           case DIR_NOD: xf = 0; break;
  4790.           case DIR_ALL: xa = 3; break;
  4791.           case DIR_DIR: xa = 2; break;
  4792.           case DIR_FIL: xa = 1; break;
  4793.           case DIR_SRT:
  4794.             x = DIRS_NM;
  4795.             if (getval)
  4796.               if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  4797.                 return(x);
  4798.             xk = x;
  4799.             xs = 1;
  4800.             break;
  4801.           case DIR_NOS: xs = 0; break;
  4802.           case DIR_ASC: xx = 0; break;
  4803.           case DIR_DSC: xx = 1; break;
  4804.           case DIR_REC: xr = 1; break;
  4805.           case DIR_NOR: xr = 0; break;
  4806.           case DIR_TYP: xm = 1; break;
  4807.           case DIR_NOT: xm = 0; break;
  4808.           case DIR_BUP: xb = 1; break;
  4809.           case DIR_NOB: xb = 0; break;
  4810.           case DIR_NOM: xg = 0; break;
  4811.           case DIR_MSG:
  4812.             if (getval)
  4813.               if ((x = cmfld("Message to append to each line",
  4814.                              "",
  4815.                              &s,
  4816.                              xxstring
  4817.                              )) < 0)
  4818.                 return(x);
  4819.             xg = 1;
  4820.             ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  4821.             break;
  4822.           default:
  4823.             printf("?This option can not be set\n");
  4824.             return(-9);
  4825.         }
  4826.     }
  4827.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  4828.       return(x);
  4829.     if (xv > -1) dir_verb = xv;         /* Confirmed, save defaults */
  4830.     if (xp > -1) dir_page = xp;
  4831.     if (xd > -1) dir_date = xd;
  4832.     if (xh > -1) dir_head = xh;
  4833.     if (xs > -1) dir_sort = xs;
  4834.     if (xk > -1) dir_skey = xk;
  4835.     if (xx > -1) dir_rvrs = xx;
  4836.     if (xf > -1) dir_dots = xf;
  4837.     if (xa > -1) dir_show = xa;
  4838.     if (xm > -1) dir_mode = xm;
  4839.     if (xb > -1) dir_back = xb;
  4840. #ifdef RECURSIVE
  4841.     if (xr > -1) dir_recu = xr;
  4842. #endif /* RECURSIVE */
  4843.     if (xg > -1) dir_msg  = xg;
  4844.     if (xg > 0)
  4845.       makestr(&dirmsg,tmpbuf);
  4846.     return(success = 1);
  4847. }
  4848.  
  4849. int
  4850. domydir(cx) int cx; {            /* Internal DIRECTORY command */
  4851.     extern char *months[];
  4852. #ifdef VMS
  4853.     _PROTOTYP( char * zrelname, (char *,char *) );
  4854.     char * cdp = NULL;
  4855. #endif /* VMS */
  4856.  
  4857.     char name[CKMAXPATH+1], outfile[CKMAXPATH+1], *p = NULL, c = NUL;
  4858.     char linebuf[CKMAXPATH+256];
  4859.     char * mstr = NULL, * dstr = NULL, * s2 = NULL, * cv = NULL;
  4860.     CK_OFF_T len = (CK_OFF_T)0, nbytes = (CK_OFF_T)0;
  4861.     CK_OFF_T minsize = (CK_OFF_T)-1, maxsize = (CK_OFF_T)-1;
  4862.     long ndirs = 0, nfiles = 0, nmatches = 0;
  4863.     int verbose = 0, wild = 0, page = 0, n = 0, engdate = 0, summary = 0;
  4864.     int heading = 0, xsort = 0, reverse = 0, sortby = 0, msg = 0;
  4865.     int k, i = 0, x = 0, nx = 0, skey = 0, dlen = 0, itsadir = 0;
  4866.     int show = 3, xfermod = 0, backup = 1, rc = 0, getval = 0;
  4867.     int touch = 0;
  4868.     int fs = 0;
  4869.     int multiple = 0;
  4870.     int cmifn1 = 1, cmifn2 = 0;
  4871.     int dir_top = 0, dir_cou = 0;
  4872.     int dontshowlinks = 0;
  4873.     int dontfollowlinks = 0;
  4874.     int arrayindex = -1;
  4875.     struct FDB sw, fi, fl;
  4876.     char dbuf[32], xbuf[32];
  4877.  
  4878. #ifndef NOSPL
  4879.     char array = NUL;
  4880.     char ** ap = NULL;
  4881. #endif /* NOSPL */
  4882.     char
  4883.       * dir_aft = NULL,
  4884.       * dir_bef = NULL,
  4885.       * dir_naf = NULL,
  4886.       * dir_nbf = NULL,
  4887.       * dir_exc = NULL;
  4888.     char * xlist[16];
  4889.  
  4890.     debug(F101,"domydir cx","",cx);
  4891.  
  4892.     g_matchdot = matchdot;              /* Save global matchdot setting */
  4893. #ifdef COMMENT
  4894.     nolinks = 2;                        /* (it should already be 2) */
  4895. #endif    /* COMMENT */
  4896.     outfile[0] = NUL;                   /* No output file yet */
  4897.  
  4898.     if (ofp != stdout) {                /* In case of previous interruption */
  4899.         if (ofp) fclose(ofp);
  4900.         ofp = stdout;
  4901.     }
  4902.     for (i = 0; i < 16; i++) xlist[i] = NULL;
  4903.  
  4904.     dir_top = 0;
  4905.     name[0] = NUL;
  4906.     freedirlist();                      /* In case not freed last time */
  4907.     page      = dir_page > -1 ? dir_page : xaskmore; /* Set option defaults */
  4908.     engdate   = dir_date > -1 ? dir_date : 0;
  4909.     verbose   = dir_verb > -1 ? dir_verb : 1;
  4910.     heading   = dir_head > -1 ? dir_head : 0;
  4911.     xsort     = dir_sort > -1 ? dir_sort : 0;
  4912.     sortby    = dir_skey > -1 ? dir_skey : 0;
  4913.     reverse   = dir_rvrs > -1 ? dir_rvrs : 0;
  4914.     msg       = dir_msg  > -1 ? dir_msg  : 0;
  4915. #ifdef UNIXOROSK
  4916.     if (dir_dots > -1) matchdot = dir_dots;
  4917. #endif /* UNIXOROSK */
  4918.     xfermod   = dir_mode > -1 ? dir_mode : 0;
  4919.     backup    = dir_back > -1 ? dir_back : 1;
  4920. #ifdef RECURSIVE
  4921.     recursive = dir_recu > -1 ? dir_recu : 0;
  4922. #endif /* RECURSIVE */
  4923.     show      = dir_show > -1 ? dir_show : 3;
  4924.  
  4925.     if (cx == XXWDIR) {            /* WDIRECTORY */
  4926.     debug(F100,"domydir WDIRECTORY","",0);
  4927.     reverse = 1;            /* Reverse chronological order */
  4928.     xsort = 1;
  4929.     sortby = DIRS_DT;
  4930.     } else if (cx == XXHDIR) {        /* HDIRECTORY */
  4931.     debug(F100,"domydir HDIRECTORY","",0);
  4932.     reverse = 1;            /* Reverse order by size */
  4933.     xsort = 1;
  4934.     sortby = DIRS_SZ;
  4935.     } else if (cx == XXTOUC) {
  4936.     touch = 1;
  4937.     verbose = 0;
  4938.     }    
  4939.  
  4940. #ifdef CK_TTGWSIZ
  4941. #ifdef OS2
  4942.     ttgcwsz();                          /* Screen length for more-prompting */
  4943. #else /* OS2 */
  4944.     /* Check whether window size changed */
  4945.     if (ttgwsiz() > 0) {
  4946.         if (tt_rows > 0 && tt_cols > 0) {
  4947.             cmd_rows = tt_rows;
  4948.             cmd_cols = tt_cols;
  4949.         }
  4950.     }
  4951. #endif /* OS2 */
  4952. #endif /* CK_TTGWSIZ */
  4953.  
  4954.     diractive = 1;
  4955.     cmifn1 = nolinks | 1;               /* 1 = files or directories */
  4956.     cmifn2 = 0;                         /* 0 = not directories only */
  4957.  
  4958.   again:
  4959.  
  4960.     cmfdbi(&sw,                         /* First FDB - command switches */
  4961.            _CMKEY,                      /* fcode */
  4962.            "Enter or Return to confirm the command, or\n\
  4963.  file specification, or switch",
  4964.            "",                          /* default */
  4965.            "",                          /* addtl string data */
  4966.            ndirswtab,                   /* addtl numeric data 1: tbl size */
  4967.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4968.            xxstring,                    /* Processing function */
  4969.            dirswtab,                    /* Keyword table */
  4970.            &fi                          /* Pointer to next FDB */
  4971.            );
  4972.     cmfdbi(&fi,                         /* 2nd FDB - filespec to match */
  4973.            _CMIFI,                      /* fcode */
  4974.            "File specification",        /* hlpmsg */
  4975. #ifdef datageneral
  4976.            "+",                         /* Default filespec is wildcard */
  4977. #else                                   /* that matches all files... */
  4978. #ifdef VMS
  4979.            "*.*",
  4980. #else
  4981.            "*",
  4982. #endif /* VMS */
  4983. #endif /* datageneral */
  4984.            "",                          /* addtl string data */
  4985.            cmifn1,
  4986.            cmifn2,                      /* 1 = only dirs; 0 files or dirs */
  4987.            xxstring,
  4988.            NULL,
  4989.            &fl
  4990.            );
  4991.     cmfdbi(&fl,                         /* Anything that doesn't match */
  4992.            _CMFLD,                      /* fcode */
  4993.            "",                          /* hlpmsg */
  4994.            "",                          /* default */
  4995.            "",                          /* addtl string data */
  4996.            0,                           /* addtl numeric data 1 */
  4997.            0,                           /* addtl numeric data 2 */
  4998.            xxstring,
  4999.            NULL,
  5000.            NULL
  5001.            );
  5002.     while (1) {                         /* Parse 0 or more switches */
  5003.         x = cmfdb(&sw);                 /* Parse something */
  5004.         debug(F101,"domydir cmfdb","",x);
  5005.         if (x < 0)
  5006.           return(x); 
  5007.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  5008.           break;
  5009.         c = cmgbrk();
  5010.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  5011.             printf("?This switch does not take an argument\n");
  5012.             return(-9);
  5013.         }
  5014.     k = cmresult.nresult;
  5015.         if (!getval &&
  5016.         (cmgkwflgs() & CM_ARG) && k != DIR_TOP && k != DIR_COU) {
  5017.             printf("?This switch requires an argument\n");
  5018.             return(-9);
  5019.         }
  5020.         switch (k) {
  5021.           case DIR_COU: {
  5022.               dir_cou++;
  5023.               if (getval) {
  5024.                   if ((x = cmfld("Variable for result","",&s,NULL)) < 0)
  5025.                     return(x);
  5026.                   makestr(&cv,s);
  5027.               }
  5028.               break;
  5029.           }
  5030.           case DIR_BRF: verbose = 0; break;
  5031.           case DIR_VRB: verbose = 1; break;
  5032. #ifdef CK_TTGWSIZ
  5033.           case DIR_PAG: page = 1;    break;
  5034.           case DIR_NOP: page = 0;    break;
  5035. #endif /* CK_TTGWSIZ */
  5036.           case DIR_ISO: engdate = 0; break;
  5037.           case DIR_DAT: engdate = 1; break;
  5038.           case DIR_HDG: heading = 1; break;
  5039.           case DIR_NOH: heading = 0; break;
  5040. #ifdef UNIXOROSK
  5041.           case DIR_DOT: matchdot = 1; break;
  5042.           case DIR_NOD: matchdot = 0; break;
  5043. #endif /* UNIXOROSK */
  5044.           case DIR_ALL:
  5045.             show = 3;
  5046.             cmifn1 |= 1;
  5047.             cmifn2 = 0;
  5048.             goto again;
  5049.           case DIR_DIR:
  5050.             show = 2;
  5051.             cmifn1 |= 1;
  5052.             cmifn2 = 1;
  5053.             goto again;
  5054.           case DIR_FIL:
  5055.             show = 1;
  5056.             cmifn1 &= ~(1);
  5057.             cmifn2 = 0;
  5058.             goto again;
  5059.           case DIR_SRT:
  5060.             x = DIRS_NM;
  5061.             if (c == ':' || c == '=')
  5062.               if ((x = cmkey(dirsort,ndirsort,"Sort key","name",xxstring)) < 0)
  5063.                 return(x);
  5064.             xsort = 1;
  5065.             sortby = x;
  5066.             break;
  5067.  
  5068.           case DIR_BUP: backup  = 1; fs++;   break;
  5069.           case DIR_NOB: backup  = 0; fs++;   break;
  5070.  
  5071.           case DIR_NOS: xsort = 0;     break;
  5072.           case DIR_ASC: reverse = 0;   break;
  5073.           case DIR_DSC: reverse = 1;   break;
  5074. #ifdef RECURSIVE
  5075.           case DIR_REC: recursive = 1; diractive = 1; break;
  5076.           case DIR_NOR: recursive = 0; diractive = 0; break;
  5077. #endif /* RECURSIVE */
  5078.           case DIR_TYP: xfermod = 1;   break;
  5079.           case DIR_NOT: xfermod = 0;   break;
  5080.  
  5081. #ifdef CKSYMLINK
  5082.           case DIR_LNK:                 /* Follow links */
  5083. #ifdef COMMENT
  5084.         /* A command switch shouldn't be setting a global value! */
  5085.             nolinks = 0;
  5086. #endif    /* COMMENT */
  5087.             cmifn1 &= ~(2);
  5088.         dontfollowlinks = 0;
  5089.             goto again;
  5090.           case DIR_NLK:                 /* Don't follow links */
  5091. #ifdef COMMENT
  5092.             nolinks = 2;
  5093. #endif    /* COMMENT */
  5094.             cmifn1 &= ~(2);
  5095.         dontfollowlinks = 1;
  5096.             goto again;
  5097.       case DIR_NOL:            /* Don't show links at all */
  5098.         dontshowlinks = 1;
  5099.         goto again;
  5100. #endif /* CKSYMLINK */
  5101.  
  5102.           case DIR_NOM: msg     = 0;   break;
  5103.           case DIR_MSG:
  5104.             if (c == ':' || c == '=')
  5105.               if ((x = cmfld("Message to append to each line",
  5106.                              "",
  5107.                              &s,
  5108.                              xxstring
  5109.                              )) < 0)
  5110.                 return(x);
  5111.             msg = 1;
  5112.             ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  5113.             break;
  5114.  
  5115.           case DIR_SMA:
  5116.           case DIR_LAR: {
  5117.           CK_OFF_T y;
  5118.           if (!getval) break;
  5119.           if ((x = cmnumw("File size in bytes","0",10,&y,xxstring)) < 0)
  5120.         return(x);
  5121.           fs++;
  5122.           show = 1;
  5123.           switch (cmresult.nresult) {
  5124.         case DIR_SMA: minsize = y; break;
  5125.         case DIR_LAR: maxsize = y; break;
  5126.           }
  5127.           break;
  5128.       }
  5129.       case DIR_TOP:
  5130.         dir_top = 10;
  5131.         if (!getval) break;
  5132.           if ((x = cmnum("How many lines to show","10",10,&y,xxstring))< 0)
  5133.         return(x);
  5134.           dir_top = y;
  5135.           break;
  5136.  
  5137. #ifndef NOSPL
  5138.           case DIR_ARR:
  5139.             if (c != ':' && c != '=') {
  5140.                 printf("?Array name required\n");
  5141.                 return(-9);
  5142.             }
  5143.             if ((x = cmfld("Array name (a single letter will do)",
  5144.                            "",
  5145.                            &s,
  5146.                            NULL
  5147.                            )) < 0) {
  5148.                 if (x == -3) {
  5149.                     printf("?Array name required\n");
  5150.                     return(-9);
  5151.                 } else
  5152.                   return(x);
  5153.             }
  5154.             if (!*s) {
  5155.                 printf("?Array name required\n");
  5156.                 return(-9);
  5157.             }
  5158.             s2 = s;
  5159.             if (*s == CMDQ) s++;
  5160.             if (*s == '&') s++;
  5161.             if (!isalpha(*s)) {
  5162.                 printf("?Bad array name - \"%s\"\n",s2);
  5163.                 return(-9);
  5164.             }
  5165.             array = *s++;
  5166.             if (isupper(array)) array = tolower(array);
  5167.             if (*s && (*s != '[' || *(s+1) != ']')) {
  5168.                 printf("?Bad array name - \"%s\"\n",s2);
  5169.                 return(-9);
  5170.             }
  5171.             break;
  5172. #endif /* NOSPL */
  5173.           case DIR_AFT:
  5174.           case DIR_BEF:
  5175.           case DIR_NAF:
  5176.           case DIR_NBF:
  5177.             if (!getval) break;
  5178.             if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  5179.                 if (x == -3) {
  5180.                     printf("?Date-time required\n");
  5181.                     rc = -9;
  5182.                 } else
  5183.                   rc = x;
  5184.                 goto xdomydir;
  5185.             }
  5186.             fs++;
  5187.             switch (k) {
  5188.               case DIR_AFT: makestr(&dir_aft,s); break;
  5189.               case DIR_BEF: makestr(&dir_bef,s); break;
  5190.               case DIR_NAF: makestr(&dir_naf,s); break;
  5191.               case DIR_NBF: makestr(&dir_nbf,s); break;
  5192.             }
  5193.             break;
  5194.           case DIR_EXC:
  5195.             if (!getval) break;
  5196.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5197.                 if (x == -3) {
  5198.                     printf("?Pattern required\n");
  5199.                     rc = -9;
  5200.                 } else
  5201.                   rc = x;
  5202.                 goto xdomydir;
  5203.             }
  5204.             fs++;
  5205.             makestr(&dir_exc,s);
  5206.             break;
  5207.  
  5208.           case DIR_SUM:
  5209.             summary = 1; break;
  5210.  
  5211.           case DIR_BIN: {
  5212.               extern struct keytab txtbin[];
  5213.               extern int xfiletype;
  5214.               if (!getval) break;
  5215.               if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0) {
  5216.                   rc = x;
  5217.                   goto xdomydir;
  5218.               }
  5219.               if (x == 2) {
  5220.                   xfiletype = -1;
  5221.               } else {
  5222.                   xfiletype = x;
  5223.                   fs = 1;
  5224.               }
  5225.               break;
  5226.           }
  5227.           case DIR_OUT:
  5228.             if ((x = cmofi("File for directory listing","",&s,xxstring)) < 0)
  5229.               return(x);
  5230.             ckstrncpy(outfile,s,CKMAXPATH+1);
  5231.             break;
  5232.  
  5233.           default:
  5234.             printf("?Sorry, not implemented yet - \"%s\"\n", atmbuf);
  5235.             goto xdomydir;
  5236.         }
  5237.     }
  5238.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Safe copy of filespec */
  5239.  
  5240. /* ^^^ START MULTIPLE */
  5241.     
  5242.     while (!touch) {
  5243.     x = cmfld("Another filespec or Enter","",&s,xxstring);
  5244.     if (x == -3)
  5245.       break;
  5246.     if (x < 0)
  5247.       return(x);
  5248.     ckstrncat(line,",",LINBUFSIZ);
  5249.     ckstrncat(line,s,LINBUFSIZ);
  5250.     multiple++;
  5251.     }
  5252.     ckmakmsg(tmpbuf,TMPBUFSIZ,"{",line,"}",NULL);
  5253.     ckstrncpy(line,tmpbuf,LINBUFSIZ);
  5254.     cmresult.nresult = 1;
  5255.     cmresult.fcode = _CMIFI;
  5256.  
  5257. /* ^^^ END MULTIPLE */
  5258.  
  5259.     s = line;
  5260.  
  5261.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  5262.       return(x);
  5263.  
  5264. /*
  5265.   Command is TOUCH and file doesn't exist.
  5266. */
  5267.     if (touch) {            /* TOUCH */
  5268.     if ((cmresult.fcode == _CMIFI && zchki(s) == (CK_OFF_T)-1)) {
  5269.         FILE * fp;
  5270.         s = brstrip(s);
  5271.         if (!iswild(s)) {
  5272. #ifdef UNIX
  5273.         if (s[0] == '~')
  5274.           s = tilde_expand(s);
  5275. #endif    /* UNIX */
  5276.         fp = fopen(s,"w");    /* Create file */
  5277.         if (!fp) {
  5278.             printf("?TOUCH %s: %s\n",s,ck_errstr());
  5279.             rc = -9;
  5280.             goto xdomydir;
  5281.         }
  5282.         fclose(fp);
  5283.         cx = XXDIR;        /* Now maybe list it. */
  5284.         multiple++;        /* Force new directory scan */
  5285.         }
  5286.     }
  5287.     } else
  5288.  
  5289.     if (cmresult.fcode != _CMIFI) {     /* Nothing matched */
  5290.     /*
  5291.       Note - this never gets executed because after the "begin
  5292.       multiple" hack above, the result is always _CMIFI).
  5293.     */
  5294.         char * m;
  5295.     if (*s == '/')
  5296. #ifdef UNIXOROSK
  5297.       m = "does not match switch or name of accessible file";
  5298. #else
  5299. #ifdef OS2
  5300.     m = "does not match switch or name of accessible file";
  5301. #else
  5302.     m = "no switches match";
  5303. #endif /* OS2 */
  5304. #endif /* UNIXOROSX */
  5305.     else
  5306.       m = "not found or not accessible";
  5307.     printf("\"%s\" - %s\n",s,m);
  5308.     rc = -9;
  5309.     goto xdomydir;
  5310.     }
  5311. #ifdef COMMENT
  5312.     /* This can't be right because it's based on _CMCFM */
  5313.     wild = cmresult.nresult;            /* Wildcard was given? */
  5314.     debug(F111,"domydir cmifi2",s,wild);
  5315. #else
  5316.     wild = 0;
  5317. #endif    /* COMMENT */
  5318.  
  5319.     if (outfile[0]) {            /* If an output file was specified */
  5320.         ofp = fopen(outfile,"w");       /* open it */
  5321.         if (!ofp) {
  5322.             printf("?Can't open output file %s: %s\n",outfile,ck_errstr());
  5323.             ofp = stdout;
  5324.             return(-9);
  5325.         }
  5326.         page = 0;
  5327.     }
  5328.  
  5329. #ifdef OS2
  5330.     if (!wild) {
  5331.         if (zchki(s) == -2) {           /* Found a directory */
  5332.             p = s + (int)strlen(s) - 1; /* Yes */
  5333.             if (*p == '\\' || *p == '/')
  5334.               strcat(s, "*");
  5335.             else if (*p == ':')
  5336.               strcat(s, "./*");
  5337.             else
  5338.               strcat(s, "/*");
  5339.             wild = 1;                   /* Now it's wild */
  5340.         }
  5341.     }
  5342. #else
  5343.     if (!wild) if (isdir(s)) {          /* Is it a directory? */
  5344.         p = s + (int)strlen(s) - 1;     /* Yes */
  5345. #ifdef VMS
  5346.         {
  5347.             /* Convert from FOO.DIR;1 to [x.FOO] if necessary */
  5348.             char buf[CKMAXPATH+1];
  5349.             debug(F000,"domydir directory 0",s,*p);
  5350.             if (cvtdir(s,buf,CKMAXPATH) > 0)
  5351.               ckstrncpy(line,buf,LINBUFSIZ);
  5352.         }
  5353. #endif /* VMS */
  5354.         debug(F000,"domydir directory 1",s,*p);
  5355. #ifdef VMS
  5356.         if (*p == ']' || *p == '>' || *p == ':')
  5357.           strcat(s, "*.*");
  5358. #else
  5359. #ifdef datageneral
  5360.         if (*p == ':')
  5361.           strcat(s, "+");
  5362.         else
  5363.           strcat(s, ":+");
  5364. #else
  5365. #ifdef STRATUS
  5366.         if (*p == '>')
  5367.           strcat(s, "*");
  5368.         else
  5369.           strcat(s, ">*");
  5370. #endif /* STRATUS */
  5371. #endif /* datageneral */
  5372. #endif /* VMS */
  5373.         wild = 1;                       /* Now it's wild */
  5374.         debug(F000,"domydir directory 2",s,*p);
  5375.     }
  5376. #endif /* OS2 */
  5377.  
  5378. #ifdef ZXREWIND
  5379. /* cmifi() already called nzxpand so we can just re-use the same list. */
  5380.     if (!multiple) {
  5381.     x = zxrewind();            /* Rewind the list */
  5382.     debug(F111,"domydir zxrewind",s,x);
  5383.     } else {
  5384. #endif /* ZXREWIND */
  5385. /*
  5386.   In case we gave multiple filespecs they are now in {a,b,c} list format.
  5387.   Which is a valid wildcard.  We pass it to nzxpand() to get back the list
  5388.   of files that match.  This is fine for DIRECTORY but it's not find for
  5389.   TOUCH because we want TOUCH to see those names so it can create the files.
  5390.   So for now at least, if TOUCH is to be used to create files -- as opposed
  5391.   to changing the timestamps of existing files -- it can only do one file
  5392.   at a time.
  5393. */
  5394.     nzxopts = (show == ZX_DIRONLY) ? ZX_DIRONLY :
  5395.       (show == ZX_FILONLY ? ZX_FILONLY : 0);
  5396.     if (matchdot)  nzxopts |= ZX_MATCHDOT;
  5397.     if (recursive) nzxopts |= ZX_RECURSE;
  5398.     x = nzxpand(s,nzxopts);             /* Expand file list */
  5399.     debug(F111,"domydir nzxpand",s,x);
  5400. #ifdef ZXREWIND
  5401.     }
  5402. #endif /* ZXREWIND */
  5403.  
  5404. #ifndef NOSPL
  5405.     if (array) {
  5406.         int n, xx;
  5407.         n = (x < 0) ? 0 : x;
  5408.         if ((xx = dclarray(array,n)) < 0) {
  5409.             printf("?Array declaration failure\n");
  5410.             rc = -9;
  5411.             goto xdomydir;
  5412.         }
  5413.     arrayindex = xx;
  5414.         ap = a_ptr[xx];            /* Pointer to list of elements */
  5415.         if (ap)                /* Set element 0 to dimension */
  5416.           makestr(&(ap[0]),"0");    /* which so far is zero */
  5417.         if (n < 1) {            /* No files matched, done. */
  5418.             rc = 0;
  5419.             goto xdomydir;
  5420.         }
  5421.     } else
  5422. #endif /* NOSPL */
  5423.       if (!touch && x < 1) {
  5424. #ifdef CKROOT
  5425.           extern int ckrooterr;
  5426.           if (ckrooterr)
  5427.             printf("?Off limits: %s\n",s);
  5428.           else
  5429. #endif /* CKROOT */
  5430.             if (x == 0 && isdir(s))
  5431.               printf("?Empty directory - \"%s\"\n", s);
  5432.             else
  5433.               printf("?%s %s match - \"%s\"\n",
  5434.                      (x == 0) ? "No" : "Too many",
  5435.                      (show == 2) ? "directories" : "files",
  5436.                      brstrip(s)
  5437.                      );
  5438.           rc = -9;
  5439.           goto xdomydir;
  5440.     }
  5441.     nx = x;                             /* Remember how many files */
  5442.     if (nx < 2) xsort = 0;        /* Skip sorting if none or one */
  5443.  
  5444.     if (msg) {
  5445.         makestr(&dirmsg,tmpbuf);
  5446.         dirmsglen = strlen(tmpbuf);
  5447.     }
  5448.  
  5449. #ifdef VMS
  5450.     cdp = zgtdir();                     /* Get current directory */
  5451.     debug(F110,"domydir VMS zgtdir",cdp,0);
  5452. #endif /* VMS */
  5453.  
  5454.     if (xsort && verbose) {             /* If sorting, allocate space */
  5455.         if (!(dirlist = (char **) malloc((x + 1) * sizeof(char **)))) {
  5456.             if (!quiet) {
  5457.                 printf("* Warning: Failure to allocate memory for sorting.\n");
  5458.                 printf("* Will proceed without sorting...\n");
  5459.             }
  5460.             xsort = 0;
  5461.         }
  5462.         debug(F101,"domydir sort malloc","",xsort);
  5463.     }
  5464.  
  5465.     /* Display the listing */
  5466.  
  5467. #ifndef NOSPL
  5468.     if (array)                          /* Storing instead of printing */
  5469.       heading = 0;
  5470. #endif /* NOSPL */
  5471.  
  5472.     if (heading) {                      /* If /HEADING print heading */
  5473.         zfnqfp(s,TMPBUFSIZ,tmpbuf);
  5474.         fprintf(ofp,"\nDirectory of %s\n\n",tmpbuf);
  5475.         n += 3;
  5476.     }
  5477.     if (page > -1)                      /* Paging */
  5478.       xaskmore = page;
  5479.  
  5480.     if (!verbose && !touch) {        /* /BRIEF */
  5481.         if (outfile[0]) {               /* To file  */
  5482.             int k = 0;
  5483.             znext(name);
  5484.             while (name[0]) {           /* One line per file */
  5485.                 k++;
  5486.                 if (fs) if (fileselect(name,
  5487.                                        dir_aft,dir_bef,dir_naf,dir_nbf,
  5488.                                        minsize,maxsize,!backup,16,xlist) < 1) {
  5489.                     znext(name);
  5490.                     continue;
  5491.                 }
  5492.                 fprintf(ofp,"%s\n",name);
  5493.                 znext(name);
  5494.             }
  5495.             if (heading)
  5496.               fprintf(ofp,"Files: %d\n\n",k);
  5497.             rc = 1;
  5498.             goto xdomydir;
  5499.         } else {
  5500.             rc = xfilhelp(x,"","",n,0,1,
  5501.               dir_aft,dir_bef,dir_naf,dir_nbf,
  5502.               minsize,maxsize,!backup,16,xlist);
  5503.             if (rc < 0)
  5504.               goto xdomydir;
  5505.             if (heading && rc > 0)
  5506.               fprintf(ofp,"Files: %d\n\n",x); /* (Might scroll a line or 2) */
  5507.             rc = 1;
  5508.             goto xdomydir;
  5509.         }
  5510.     }
  5511.     ndirs = nfiles = 0L;        /* Initialize counters */
  5512.     nbytes = (CK_OFF_T)0;
  5513.  
  5514.     if (dir_exc)                        /* Have exception list? */
  5515.       makelist(dir_exc,xlist,16);    /* Yes, convert to array */
  5516.  
  5517.     diractive = 1;
  5518.     znext(name);                        /* Get next file */
  5519.     while (name[0]) {                   /* Loop for each file */
  5520.         if (fs) if (fileselect(name,
  5521.                        dir_aft,dir_bef,dir_naf,dir_nbf,
  5522.                        minsize,maxsize,!backup,16,xlist) < 1) {
  5523.             znext(name);
  5524.             continue;
  5525.         }
  5526.         len = zgetfs(name);             /* Get file length */
  5527.         debug(F111,"domydir zgetfs",name,len);
  5528. #ifdef VMSORUNIX
  5529.         itsadir = zgfs_dir;             /* See if it's a directory */
  5530. #else
  5531.         itsadir = (len == (CK_OFF_T)-2 || isdir(name));
  5532. #endif /* VMSOUNIX */
  5533.         debug(F111,"domydir itsadir",name,itsadir);
  5534.         if ((itsadir && (show == 1)) || (!itsadir && (show == 2))) {
  5535.             znext(name);
  5536.             continue;
  5537.         }
  5538.         /* Get here when we know we have selected this file */
  5539.  
  5540.         nmatches++;
  5541.         if (itsadir) {                  /* Accumulate totals for summary */
  5542.             ndirs++;
  5543.         } else {
  5544.             nfiles++;
  5545.             nbytes += len;
  5546.         }
  5547.     dstr = NULL;
  5548.     if (cx == XXTOUC) {        /* Command was TOUCH, not DIRECTORY */
  5549.         char * filename;
  5550.         struct zattr xx;
  5551.         dstr = ckcvtdate("",0);
  5552.         xx.date.val = dstr;
  5553.         xx.date.len = (int)strlen(xx.date.val);
  5554.         xx.lprotect.len = 0;
  5555.         debug(F110,"domydir touch",name,0);
  5556.         debug(F110,"domydir touch",dstr,0);
  5557.         if (zstime(name,&xx,0) < 0) {
  5558.         printf("?TOUCH %s: %s\n",name,ck_errstr());
  5559.         rc = -9;
  5560.         goto xdomydir;
  5561.         }
  5562.         if (!verbose) {        /* No listing so skip detail */
  5563.         znext(name);
  5564.         continue;
  5565.         }
  5566.     }
  5567.         if (summary) {            /* Summary only, no detail */
  5568.             znext(name);
  5569.             continue;
  5570.         }
  5571.  
  5572. #ifndef NOSPL
  5573.         if (array) {
  5574.             debug(F111,"domydir array",name,nfiles);
  5575.             if (ap)
  5576.               makestr(&(ap[nmatches]),name);
  5577.             znext(name);
  5578.             continue;
  5579.         }
  5580. #endif /* NOSPL */
  5581.  
  5582. /*
  5583.   NOTE: The sprintf's in this routine should be safe.  They involve
  5584.   permission strings, date/time strings, and filenames, all of which have
  5585.   known maximum lengths; none of these items is input from users.  The
  5586.   destination buffers are large enough to hold maximum sizes for any and
  5587.   all items.  NOTE 2: If command was TOUCH, dstr was already set just
  5588.   above.
  5589. */
  5590.     if (!dstr) {            /* Get file's modification date/time */
  5591.         dstr = zfcdat(name);
  5592.         debug(F111,"domydir zcfdat",dstr,0);
  5593.     }
  5594.         if (!dstr) dstr = "";
  5595.         {
  5596. /*
  5597.   Note that zfcdat() always returns "" or yyyymmdd hh:mm:ss, so any warnings
  5598.   about possible out-of-bounds dstr[] array refs do not apply.  This block of
  5599.   code is to stifle the warnings and also allows for any out-of-spec
  5600.   zfcdat() implementations.
  5601. */
  5602.             int x;
  5603.             char * p = "00000000 00:00:00";
  5604.             x = ckstrncpy(xbuf,dstr,32);
  5605.             if (x < 17) ckstrncpy(&xbuf[x],p+x,32-x);
  5606.             dstr = xbuf;
  5607.         }
  5608.         if (engdate) {                  /* English date requested? */
  5609.             short month, day, year, hour, minute, seconds;
  5610.             month = (dstr[4]-48)*10 + (dstr[5]-48);
  5611.             mstr  = (month > 0 && month <= 12) ? months[month-1] : "xxx";
  5612.             day   = (dstr[6]-48)*10 + (dstr[7]-48);
  5613.             year  = (((dstr[0]-48)*10 +
  5614.                       (dstr[1]-48))*10 +
  5615.                       (dstr[2]-48))*10 +
  5616.                       (dstr[3]-48);
  5617.             hour  = (dstr[9]-48)*10 + (dstr[10]-48);
  5618.             minute = (dstr[12]-48)*10 + (dstr[13]-48);
  5619.             seconds = (dstr[15]-48)*10 + (dstr[16]-48);
  5620.             sprintf(dbuf,               /* SAFE */
  5621.                     "%2d-%s-%4d %02d:%02d:%02d",
  5622.                     day,mstr,year,hour,minute,seconds
  5623.                     );
  5624.             dstr = dbuf;
  5625.         } else {                        /* ISO date */
  5626.             dbuf[0] = dstr[0];          /* yyyy */
  5627.             dbuf[1] = dstr[1];
  5628.             dbuf[2] = dstr[2];
  5629.             dbuf[3] = dstr[3];
  5630.             dbuf[4] = '-';
  5631.             dbuf[5] = dstr[4];          /* mm (numeric) */
  5632.             dbuf[6] = dstr[5];
  5633.             dbuf[7] = '-';
  5634.             dbuf[8] = dstr[6];          /* dd */
  5635.             dbuf[9] = dstr[7];
  5636.             strcpy(dbuf+10,dstr+8);     /* hh:mm:ss */
  5637.             dstr = dbuf;
  5638.         }
  5639.         dlen = strlen(dbuf);            /* Length of date */
  5640.         name[CKMAXPATH] = NUL;
  5641. #ifdef CK_PERMS
  5642. #ifdef VMSORUNIX
  5643.         p = ziperm(name);               /* Get permissions */
  5644.         debug(F110,"ziperm perms",p,0);
  5645. #else
  5646.         p = zgperm(name);
  5647.         debug(F110,"zgperm perms",p,0);
  5648. #endif /* VMSORUNIX */
  5649. #else
  5650.         p = NULL;
  5651.         debug(F110,"NULL perms",p,0);
  5652. #endif /* CK_PERMS */
  5653.  
  5654. #ifdef VMS
  5655.         /* Get relative name to save space -- VMS fullnames are long... */
  5656.         ckstrncpy(name,zrelname(name,cdp),CKMAXPATH);
  5657. #endif /* VMS */
  5658.  
  5659.         if (itsadir && len < (CK_OFF_T)0) { /* Directory */
  5660. #ifdef VMS
  5661.             sprintf(linebuf,"%-22s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5662. #else
  5663.             if (p)
  5664.               sprintf(linebuf,"%10s%-10s  %s  %s",p,"<DIR>",dstr,name);
  5665.             else
  5666.               sprintf(linebuf,"%-10s  %s  %s", "<DIR>", dstr, name);
  5667. #endif /* VMS */
  5668.         } else {                        /* Regular file */
  5669. #ifdef VMS
  5670.             sprintf(linebuf,"%-22s%10s  %s  %s", p, ckfstoa(len), dstr, name);
  5671. #else
  5672.             if (p)
  5673.               sprintf(linebuf,"%10s%10s  %s  %s", p, ckfstoa(len), dstr, name);
  5674.             else
  5675.               sprintf(linebuf,"%10s  %s  %s", ckfstoa(len), dstr, name);
  5676. #endif /* VMS */
  5677.         }
  5678. #ifdef UNIX
  5679. #ifdef CKSYMLINK
  5680.         if (zgfs_link) {        /* If it's a symlink */
  5681.         if (dontshowlinks) {    /* If /NOLINKS don't show it */
  5682.         znext(name);
  5683.         continue;
  5684.         }
  5685.     }
  5686.         if (zgfs_link && !dontfollowlinks) { /* Symlink and following links */
  5687.             int n, m;            /* Show what the link points to */
  5688.             extern char linkname[];
  5689.             n = strlen(linebuf);
  5690.             m = strlen(linkname) + n;
  5691.             if (m < CKMAXPATH + 58)
  5692.               strcpy(linebuf+n, " -> "); /* safe (checked) */
  5693.             if (m + 4 < CKMAXPATH - 58)
  5694.               strcpy(linebuf+n+4, linkname); /* safe (checked) */
  5695.         } else
  5696. #endif /* CKSYMLINK */
  5697. #endif /* UNIX */
  5698.         if (xfermod) {                  /* Show transfer mode */
  5699.             int i, x, y;
  5700.             char * s = "";
  5701.             y = -1;
  5702.             x = scanfile(name,&y,nscanfile);
  5703.             switch (x) {
  5704.               case FT_TEXT: s = " (T)"; break;
  5705.               case FT_7BIT: s = " (T)(7BIT)"; break;
  5706.               case FT_8BIT: s = " (T)(8BIT)"; break;
  5707. #ifdef UNICODE
  5708.               case FT_UTF8: s = " (T)(UTF8)"; break;
  5709.               case FT_UCS2:
  5710.                 s = y ? " (T)(UCS2LE)" : " (T)(UCS2BE)";
  5711.                 break;
  5712. #endif /* UNICODE */
  5713.               case FT_BIN:  s = " (B)"; break;
  5714.             }
  5715.             if (!*s) {
  5716.                 s = binary ? " (B)" : " (T)";
  5717.             }
  5718.             if (*s) {
  5719.                 int n;
  5720.                 n = strlen(linebuf);
  5721.                 if (n + 4 < CKMAXPATH - 58)
  5722.                   strcpy(linebuf+n, s); /* safe (checked) */
  5723.             }
  5724.         }
  5725.         if (msg && dirmsg) {
  5726.             int n;
  5727.             n = strlen(linebuf);
  5728.             if (n + dirmsglen + 2 < CKMAXPATH)
  5729.               sprintf((char *)(linebuf+n)," %s", dirmsg); /* SAFE */
  5730.         }
  5731.         if (xsort) {            /* Sorting - save line */
  5732.             i = strlen(linebuf);
  5733.             if ((ndirlist >= nx) ||
  5734.                 !(dirlist[ndirlist] = (char *)malloc(i+1))) {
  5735.                 printf("?Memory allocation error - try /NOSORT\n");
  5736.                 rc = -9;
  5737.                 goto xdomydir;
  5738.             }
  5739.             strcpy(dirlist[ndirlist],linebuf); /* safe */
  5740.             ndirlist++;
  5741.         }
  5742.         znext(name);                    /* Peek ahead to next file */
  5743.         if (!xsort) {
  5744.         if (!touch || (touch && verbose))
  5745.           fprintf(ofp,"%s\n",linebuf);
  5746.             if (page && (name[0] || heading)) { /* If /PAGE */
  5747.                 if (cmd_cols > 0) {
  5748.                     int x = strlen(linebuf);
  5749.                     int y;
  5750.                     y = (x % cmd_cols) ? 1 : 0;
  5751.                     n += x / cmd_cols + y;
  5752.                 } else {
  5753.                     n++;
  5754.                 }
  5755. #ifdef CK_TTGWSIZ
  5756.                 if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5757.                     if (!askmore()) {
  5758.                         rc = 0;
  5759.                         goto xdomydir;
  5760.                     } else
  5761.                       n = 0;
  5762.                 }
  5763. #endif /* CK_TTGWSIZ */
  5764.             }
  5765.         }
  5766.     }
  5767. #ifndef NOSPL
  5768.     if (array) {
  5769.         if (ap)
  5770.           makestr(&(ap[0]),ckitoa(nmatches));
  5771.         rc = 1;
  5772.         goto xdomydir;
  5773.     }
  5774. #endif /* NOSPL */
  5775.     if (xsort) {
  5776.     int namepos;
  5777.         skey = 0;
  5778. #ifdef VMS
  5779.     namepos = dlen + 35;
  5780.         switch (sortby) {
  5781.           case DIRS_NM: skey = namepos; break;
  5782.           case DIRS_DT: skey = 33; break;
  5783.           case DIRS_SZ: skey = 21;
  5784.         }
  5785. #else
  5786.         if (p) {
  5787.         namepos = dlen + 24;
  5788.             switch (sortby) {
  5789.               case DIRS_NM: skey = namepos; break;
  5790.               case DIRS_DT: skey = 22; break;
  5791.               case DIRS_SZ: skey = 10;
  5792.             }
  5793.         } else {
  5794.         namepos = dlen + 14;
  5795.             switch (sortby) {
  5796.               case DIRS_NM: skey = namepos; break;
  5797.               case DIRS_DT: skey = 12; break;
  5798.               case DIRS_SZ: skey = 0;
  5799.             }
  5800.         }
  5801. #endif /* VMS */
  5802.         sh_sort(dirlist,NULL,ndirlist,skey,reverse,filecase);
  5803.     if (dir_top > 0 && dir_top < ndirlist)
  5804.       ndirlist = dir_top;
  5805.         for (i = 0; i < ndirlist; i++) {
  5806.             fprintf(ofp,"%s\n",dirlist[i]);
  5807.             if (page && (i < ndirlist -1 || heading)) { /* If /PAGE */
  5808.                 if (cmd_cols > 0) {
  5809.                     int x = strlen(dirlist[i]);
  5810.                     int y;
  5811.                     y = (x % cmd_cols) ? 1 : 0;
  5812.                     n += ((int)strlen(dirlist[i]) / cmd_cols) + y;
  5813.                 } else {
  5814.                     n++;
  5815.                 }
  5816. #ifdef CK_TTGWSIZ
  5817.                 if (n > (cmd_rows - 3)) { /* Do more-prompting */
  5818.                     if (!askmore()) {
  5819.                         rc = 0;
  5820.                         goto xdomydir;
  5821.                     } else
  5822.                       n = 0;
  5823.                 }
  5824. #endif /* CK_TTGWSIZ */
  5825.             }
  5826.         }
  5827.     }
  5828.  
  5829.     if (heading || summary) {
  5830. #ifdef CKFLOAT
  5831.         CKFLOAT gm;
  5832. #endif /* CKFLOAT */
  5833.         fprintf(ofp,"\n%ld director%s, %ld file%s, %s byte%s",
  5834.                ndirs,
  5835.                (ndirs == 1) ? "y" : "ies",
  5836.                nfiles,
  5837.                (nfiles == 1) ? "" : "s",
  5838.            ckfstoa(nbytes),
  5839.                (nbytes == 1) ? "" : "s"
  5840.                );
  5841. #ifdef CKFLOAT
  5842.         gm = ((CKFLOAT) nbytes ) / 1000000.0;
  5843.         if (gm > 1000.0)
  5844.           fprintf(ofp," (%0.2fGB)",(gm / 1000.0));
  5845.         else if (gm >= 0.01)
  5846.           fprintf(ofp," (%0.2fMB)",gm);
  5847. #endif /* CKFLOAD */
  5848.         fprintf(ofp,"\n\n");
  5849.     } else if (dir_cou && !cv) {
  5850.     fprintf(ofp,"\n Files: %ld\n\n",nfiles);
  5851.     }
  5852.   xdomydir:
  5853. #ifndef NOSPL
  5854.     if (dir_cou && cv) {                /* /COUNT:var */
  5855.         addmac(cv,ckitoa(nfiles));    /* set the variable */
  5856.         makestr(&cv,NULL);              /* free this */
  5857.     }
  5858.     if (ap) {                /* If we have a result array */
  5859.     if (a_dim[arrayindex] > nmatches) /* but it was not filled */
  5860.       a_dim[arrayindex] = nmatches;   /* adjust dimension */
  5861.     }
  5862. #endif    /* NOSPL */
  5863.     if (g_matchdot > -1) {
  5864.         matchdot = g_matchdot;          /* Restore these... */
  5865.         g_matchdot = -1;
  5866.     }
  5867.     freedirlist();
  5868.     if (ofp != stdout) {                /* Close any output file */
  5869.         if (ofp) fclose(ofp);
  5870.         ofp = stdout;
  5871.     }
  5872.     if (rc > 0)
  5873.       success = 1;
  5874.     return(rc);
  5875. }
  5876.  
  5877. int
  5878. dodir(cx) int cx; {                     /* Do the DIRECTORY command */
  5879.     char *dc , *msg;
  5880.  
  5881. #ifdef OS2
  5882.     return(domydir(cx));
  5883. #else /* OS2 */
  5884.     if (nopush
  5885. #ifdef DOMYDIR                          /* Builds that domydir() by default */
  5886.         || (cx == XXDIR  || cx == XXLDIR || cx == XXWDIR ||
  5887.         cx == XXHDIR || cx == XXTOUC)
  5888. #endif /* DOMYDIR */
  5889.         )
  5890.       return(domydir(cx));        /* Built-in directory command */
  5891.  
  5892.     /* Use the system's directory command. */
  5893.  
  5894.     msg = (cx == XXLS) ?
  5895.       "Arguments for ls" :
  5896.         "Directory and/or file specification";
  5897.     if ((x = cmtxt(msg,"",&s,xxstring)) < 0)
  5898.       return(x);
  5899.  
  5900.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);      /* Copy the filespec */
  5901.     s = tmpbuf;
  5902.  
  5903.     if ((y = cmcfm()) < 0) return(y);
  5904.  
  5905.     lp = line;
  5906.     if (!(dc = getenv("CK_DIR")))
  5907.       dc = DIRCMD;
  5908.     ckmakmsg(lp,LINBUFSIZ,dc," ",s,NULL);
  5909.     debug(F110,"DIR",line,0);
  5910. #ifdef VMS
  5911.     conres();
  5912. #endif /* VMS */
  5913.     x = zshcmd(line);
  5914. #ifdef VMS
  5915.     concb((char)escape);
  5916. #endif /* VMS */
  5917.     return(success = (x < 1) ? 0 : 1);
  5918. #endif /* OS2 */
  5919. }
  5920.  
  5921. #ifndef NOSERVER
  5922. #ifndef NOFRILLS
  5923. /* Do the ENABLE and DISABLE commands */
  5924.  
  5925. int
  5926. doenable(y,x) int y, x; {
  5927. #ifdef CK_LOGIN
  5928.     if (isguest)                        /* IKSD: Don't let guests */
  5929.       return(0);                        /* enable anything that's disabled */
  5930. #endif /* CK_LOGIN */
  5931.     switch (x) {
  5932.       case EN_ALL:
  5933.         en_cwd = en_cpy = en_del = en_dir = en_fin = en_get = y;
  5934.         en_ren = en_sen = en_set = en_spa = en_typ = en_ret = y;
  5935.         if (!inserver)
  5936.           en_who = en_mai = en_pri = y;
  5937.         en_mkd = en_rmd = y;
  5938.         en_xit = y;
  5939. #ifndef datageneral
  5940.         en_bye = y;
  5941. #endif /* datageneral */
  5942. #ifndef NOPUSH
  5943.         if (!nopush && !inserver)
  5944.           en_hos = y;
  5945. #endif /* NOPUSH */
  5946. #ifndef NOSPL
  5947.         en_asg = en_que = y;
  5948. #endif /* NOSPL */
  5949.         break;
  5950.  
  5951.       case EN_BYE:
  5952. #ifndef datageneral
  5953. /*
  5954.   In Data General AOS/VS Kermit can't log out its superior process.
  5955. */
  5956.         en_bye = y;
  5957. #endif /* datageneral */
  5958.         break;
  5959.       case EN_CPY:
  5960.         en_cpy = y;
  5961.         break;
  5962.       case EN_CWD:
  5963.         en_cwd = y;
  5964. #ifdef IKSD
  5965.         if (inserver && y == 0) {
  5966.             fnrpath = PATH_OFF;
  5967.             fnspath = PATH_OFF;
  5968.         }
  5969. #endif /* IKSD */
  5970.         break;
  5971.       case EN_DEL:                      /* Deleting of files */
  5972.         en_del = y;
  5973.         break;
  5974.       case EN_DIR:
  5975.         en_dir = y;
  5976.         break;
  5977.       case EN_FIN:
  5978.         en_fin = y;
  5979.         break;
  5980.       case EN_GET:
  5981.         en_get = y;
  5982.         break;
  5983. #ifndef NOPUSH
  5984.       case EN_HOS:
  5985.         if (!nopush)
  5986.          en_hos = y;
  5987.         break;
  5988. #endif /* NOPUSH */
  5989.       case EN_REN:
  5990.         en_ren = y;
  5991.         break;
  5992.       case EN_SEN:
  5993.         en_sen = y;
  5994.         break;
  5995.       case EN_SET:
  5996.         en_set = y;
  5997.         break;
  5998.       case EN_SPA:
  5999.         en_spa = y;
  6000.         break;
  6001.       case EN_TYP:
  6002.         en_typ = y;
  6003.         break;
  6004.       case EN_WHO:
  6005.         en_who = y;
  6006.         break;
  6007. #ifndef NOSPL
  6008.       case EN_ASG:
  6009.         en_asg = y;
  6010.         break;
  6011.       case EN_QUE:
  6012.         en_que = y;
  6013.         break;
  6014. #endif /* NOSPL */
  6015.       case EN_RET:
  6016.         en_del = y;
  6017.         break;
  6018.       case EN_MAI:
  6019. #ifdef CK_LOGIN
  6020.         if (isguest && y) {
  6021.             printf("?Sorry, not valid for guests\n");
  6022.             return(-9);
  6023.         }
  6024. #endif /* CK_LOGIN */
  6025.         en_mai = y;
  6026.         break;
  6027.       case EN_PRI:
  6028. #ifdef CK_LOGIN
  6029.         if (isguest && y) {
  6030.             printf("?Sorry, not valid for guests\n");
  6031.             return(-9);
  6032.         }
  6033. #endif /* CK_LOGIN */
  6034.         en_pri = y;
  6035.         break;
  6036.       case EN_MKD:
  6037.         en_mkd = y;
  6038.         break;
  6039.       case EN_RMD:
  6040.         en_rmd = y;
  6041.         break;
  6042.       case EN_XIT:
  6043.         en_xit = y;
  6044.         break;
  6045.       case EN_ENA:
  6046.         if (((y & 1) && !(en_ena & 1)) ||
  6047.             ((y & 2) && !(en_ena & 2))) {
  6048.             printf("?Sorry, DISABLE ENABLE can not be undone\n");
  6049.             return(-9);
  6050.         } else {
  6051.             en_ena = y;
  6052.             break;
  6053.         }
  6054.       default:
  6055.         return(-2);
  6056.     }
  6057.     return(1);
  6058. }
  6059. #endif /* NOFRILLS */
  6060. #endif /* NOSERVER */
  6061.  
  6062. #ifndef NOFRILLS
  6063.  
  6064. static int del_lis = 0;
  6065. static int del_dot = 0;
  6066. static int del_hdg = 0;
  6067. static int del_pag = -1;
  6068. static int del_ask = 0;
  6069.  
  6070. #ifndef NOSHOW
  6071. VOID
  6072. showdelopts() {
  6073.     int x = 0;
  6074.     extern int optlines;
  6075.     prtopt(&optlines,"");
  6076.     prtopt(&optlines,"DELETE");
  6077.     if (del_ask > -1) {
  6078.         prtopt(&optlines, del_ask ? "/ASK" : "/NOASK");
  6079.         x++;
  6080.     }
  6081. #ifdef UNIXOROSK
  6082.     if (del_dot > -1) {
  6083.         prtopt(&optlines, del_dot ? "/DOTFILES" : "/NODOTFILES");
  6084.         x++;
  6085.     }
  6086. #endif /* UNIXOROSK */
  6087.     if (del_lis > -1) {
  6088.         prtopt(&optlines, del_lis ? "/LIST" : "/NOLIST");
  6089.         x++;
  6090.     }
  6091.     if (del_hdg > -1) {
  6092.         prtopt(&optlines, del_hdg ? "/HEADING" : "/NOHEADING");
  6093.         x++;
  6094.     }
  6095. #ifndef CK_TTGWSIZ
  6096.     if (del_pag > -1) {
  6097.         prtopt(&optlines, del_pag ? "/PAGE" : "/NOPAGE");
  6098.         x++;
  6099.     }
  6100. #endif /* CK_TTGWSIZ */
  6101.     if (!x) prtopt(&optlines,"(no options set)");
  6102.     prtopt(&optlines,"");
  6103. }
  6104. #endif /* NOSHOW */
  6105.  
  6106.  
  6107. int
  6108. setdelopts() {
  6109.     int x_lis = -1, x_pag = -1, x_dot = -1, x_hdg = -1, x_ask = -1;
  6110.     int getval = 0;
  6111.     char c;
  6112.     while (1) {
  6113.         if ((y = cmswi(deltab,ndeltab,"Switch","",xxstring)) < 0) {
  6114.             if (y == -3)
  6115.               break;
  6116.             else
  6117.               return(y);
  6118.         }
  6119.         c = cmgbrk();
  6120.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  6121.             printf("?This switch does not take an argument\n");
  6122.             return(-9);
  6123.         }
  6124.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  6125.             printf("?This switch requires an argument\n");
  6126.             return(-9);
  6127.         }
  6128.         switch (y) {
  6129.           case DEL_DOT:
  6130.             x_dot = 1;
  6131.             break;
  6132.           case DEL_NOD:
  6133.             x_dot = 0;
  6134.             break;
  6135.           case DEL_HDG:
  6136.             x_hdg = 1;
  6137.             break;
  6138.           case DEL_LIS:
  6139.             x_lis = 1;
  6140.             break;
  6141.           case DEL_NOL:
  6142.             x_lis = 0;
  6143.             break;
  6144. #ifndef CK_TTGWSIZ
  6145.           case DEL_PAG:
  6146.             x_pag = 1;
  6147.             break;
  6148.           case DEL_NOP:
  6149.             x_pag = 0;
  6150.             break;
  6151. #endif /* CK_TTGWSIZ */
  6152.           case DEL_QUI:
  6153.             x_lis = 0;
  6154.             break;
  6155.           case DEL_VRB:
  6156.             x_lis = 1;
  6157.             break;
  6158.           case DEL_ASK:
  6159.             x_ask = 1;
  6160.             break;
  6161.           case DEL_NAS:
  6162.             x_ask = 0;
  6163.             break;
  6164.           default:
  6165.             printf("?Sorry, this option can not be set\n");
  6166.             return(-9);
  6167.         }
  6168.     }
  6169.     if ((x = cmcfm()) < 0)              /* Get confirmation */
  6170.       return(x);
  6171.     if (x_pag > -1) del_pag = x_pag;
  6172.     if (x_dot > -1) del_dot = x_dot;
  6173.     if (x_hdg > -1) del_hdg = x_hdg;
  6174.     if (x_lis > -1) del_lis = x_lis;
  6175.     if (x_ask > -1) del_ask = x_ask;
  6176.     return(success = 1);
  6177. }
  6178.  
  6179. #ifdef OS2
  6180. static char ** xmtchs = NULL;
  6181. static int xmtchn = 0;
  6182. #endif /* OS2 */
  6183.  
  6184. int
  6185. dodel() {                               /* DELETE */
  6186.     int i, j, k, x;
  6187.     int fs = 0;                         /* Need to call fileselect() */
  6188.     int len = 0;
  6189.     int bad = 0;
  6190.     int getval = 0, asking = 0;
  6191.     int simulate = 0, rc = 0;
  6192.     CK_OFF_T minsize = -1L, maxsize = -1L;
  6193.     int havename = 0, confirmed = 0;
  6194.     int qflag = 0;
  6195.     int summary = 0;
  6196.     int deldirs = 0;
  6197.     int deltree = 0;
  6198.     int itsadir = 0;
  6199.     int argisdir = 0;
  6200.     int xmode = -1, scan = 0, skip = 0;
  6201. #ifdef COMMENT
  6202.     int pass = 0;
  6203. #endif /* COMMENT */
  6204.     char c;
  6205.     char * deldef = "";
  6206.     char safebuf[CKMAXPATH+1];
  6207.     struct FDB sw, fi, fl;
  6208.     char
  6209.       * del_aft = NULL,
  6210.       * del_bef = NULL,
  6211.       * del_naf = NULL,
  6212.       * del_nbf = NULL,
  6213.       * del_exc = NULL;
  6214.     int
  6215.       x_lis = 0,
  6216.       /* x_dot = -1, */
  6217.       x_hdg = 0;
  6218.  
  6219.     char * dxlist[8];
  6220.  
  6221.     for (i = 0; i < 8; i++) dxlist[i] = NULL;
  6222.  
  6223.     g_matchdot = matchdot;
  6224.  
  6225.     if (del_lis > -1) x_lis    = del_lis;
  6226.     if (del_dot > -1) matchdot = del_dot;
  6227.     if (del_hdg > -1) x_hdg    = del_hdg;
  6228.     if (del_pag > -1) xaskmore = del_pag;
  6229.     if (del_ask > -1) asking   = del_ask;
  6230.  
  6231.     diractive = 1;
  6232.     nolinks = 2;                        /* By default don't follow links */
  6233.  
  6234.     cmfdbi(&sw,                         /* First FDB - command switches */
  6235.            _CMKEY,                      /* fcode */
  6236.            "File specification;\n or switch",
  6237.            "",                          /* default */
  6238.            "",                          /* addtl string data */
  6239.            ndeltab,                     /* addtl numeric data 1: tbl size */
  6240.            4,                           /* addtl numeric data 2: 4 = cmswi */
  6241.            xxstring,                    /* Processing function */
  6242.            deltab,                      /* Keyword table */
  6243.            &fi                          /* Pointer to next FDB */
  6244.            );
  6245.     cmfdbi(&fl,                         /* Anything that doesn't match */
  6246.            _CMFLD,                      /* fcode */
  6247.            "",                          /* hlpmsg */
  6248.            "",                          /* default */
  6249.            "",                          /* addtl string data */
  6250.            0,                           /* addtl numeric data 1 */
  6251.            0,                           /* addtl numeric data 2 */
  6252.            xxstring,
  6253.            NULL,
  6254.            NULL
  6255.            );
  6256.   again:
  6257.     cmfdbi(&fi,                         /* 2nd FDB - file to delete */
  6258.            _CMIFI,                      /* fcode */
  6259.            "File(s) to delete",         /* hlpmsg */
  6260.            deldef,                      /* default */
  6261.            "",                          /* addtl string data */
  6262.            nolinks | deldirs,           /* 0 = files, 1 = files or dirs */
  6263.            0,                           /* 1 = dirs only */
  6264.            xxstring,
  6265.            NULL,
  6266.            &fl
  6267.            );
  6268.     while (!havename && !confirmed) {
  6269.         x = cmfdb(&sw);                 /* Parse something */
  6270.         if (x < 0) {                    /* Error */
  6271.             if (x == -3)
  6272.               break;
  6273.             if (x == -2 || x == -9)
  6274.               printf("?Does not match switch or filename: \"%s\"\n",atmbuf);
  6275.             return(x);
  6276.         }
  6277.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  6278.           break;
  6279.         c = cmgbrk();                   /* Get break character */
  6280.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  6281.             printf("?This switch does not take an argument\n");
  6282.             rc = -9;
  6283.             goto xdelete;
  6284.         }
  6285.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  6286.             printf("?This switch requires an argument\n");
  6287.             rc = -9;
  6288.             goto xdelete;
  6289.         }
  6290.         switch (k = cmresult.nresult) {
  6291.           case DEL_AFT:
  6292.           case DEL_BEF:
  6293.           case DEL_NAF:
  6294.           case DEL_NBF:
  6295.             if (!getval) break;
  6296.             if ((x = cmdate("File-time","",&s,0,xxstring)) < 0) {
  6297.                 if (x == -3) {
  6298.                     printf("?Date-time required\n");
  6299.                     x = -9;
  6300.                 } else
  6301.                   rc = x;
  6302.                 goto xdelete;
  6303.             }
  6304.             fs++;
  6305.             deltree = 0;
  6306.             switch (k) {
  6307.               case DEL_AFT: makestr(&del_aft,s); break;
  6308.               case DEL_BEF: makestr(&del_bef,s); break;
  6309.               case DEL_NAF: makestr(&del_naf,s); break;
  6310.               case DEL_NBF: makestr(&del_nbf,s); break;
  6311.             }
  6312.             break;
  6313.           case DEL_DOT:
  6314.             matchdot = 1;
  6315.             break;
  6316.           case DEL_NOD:
  6317.             matchdot = 0;
  6318.             break;
  6319.           case DEL_ALL:
  6320.             fs = 0;
  6321. #ifdef VMS
  6322.             deldef = "*.*";             /* UNIX, Windows, OS/2 */
  6323. #else
  6324. #ifdef datageneral
  6325.             deldef = "+";               /* AOS/VS */
  6326. #else
  6327.             deldef = "*";               /* UNIX, Windows, OS/2, VMS... */
  6328. #endif /* datageneral */
  6329. #endif /* VMS */
  6330.             deltree = 1;
  6331.             nolinks = 2;
  6332.             matchdot = 1;
  6333.             recursive = 1;              /* Fall through purposely... */
  6334.           case DEL_DIR:
  6335.             deldirs = 1;
  6336.             goto again;
  6337.           case DEL_EXC:
  6338.             if (!getval) break;
  6339.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  6340.                 if (x == -3) {
  6341.                     printf("?Pattern required\n");
  6342.                     x = -9;
  6343.                 } else
  6344.                   rc = x;
  6345.                 goto xdelete;
  6346.             }
  6347.             fs++;
  6348.             deltree = 0;
  6349.             makestr(&del_exc,s);
  6350.             break;
  6351.           case DEL_HDG:
  6352.             x_hdg = 1;
  6353.             break;
  6354. #ifdef RECURSIVE
  6355.           case DEL_REC:
  6356.             recursive = 1;
  6357.             break;
  6358. #endif /* RECURSIVE */
  6359.           case DEL_LIS:
  6360.             x_lis = 1;
  6361.             break;
  6362.           case DEL_SUM:
  6363.             summary = 1;
  6364.             x_lis = 0;
  6365.             x_hdg = 1;
  6366.             break;
  6367.           case DEL_NOL:
  6368.             x_lis = 0;
  6369.             break;
  6370. #ifndef CK_TTGWSIZ
  6371.           case DEL_PAG:
  6372.             xaskmore = 1;
  6373.             break;
  6374.           case DEL_NOP:
  6375.             xaskmore = 0;
  6376.             break;
  6377. #endif /* CK_TTGWSIZ */
  6378.           case DEL_QUI:
  6379.             qflag = 1;
  6380.             x_lis = 0;
  6381.             break;
  6382.           case DEL_VRB:
  6383.             x_lis = 1;
  6384.             break;
  6385.  
  6386.           case DEL_SMA:
  6387.           case DEL_LAR:
  6388.             if (!getval) break;
  6389.             if ((x = cmnum("File size in bytes","0",10,&y,xxstring)) < 0)
  6390.               return(x);
  6391.             fs++;
  6392.             deltree = 0;
  6393.             switch (cmresult.nresult) {
  6394.               case DEL_SMA: minsize = y; break;
  6395.               case DEL_LAR: maxsize = y; break;
  6396.             }
  6397.             break;
  6398.  
  6399.           case DEL_SIM:
  6400.             simulate = 1;
  6401.             x_lis = 1;
  6402.             break;
  6403.           case DEL_ASK:
  6404.             asking = 1;
  6405.             break;
  6406.           case DEL_NAS:
  6407.             asking = 0;
  6408.             break;
  6409.           case DEL_TYP: {
  6410.               extern struct keytab txtbin[];
  6411.               if (!getval) break;
  6412.               if ((x = cmkey(txtbin,3,"","",xxstring)) < 0)
  6413.                 return(x);
  6414.               if (x == 2) {             /* ALL */
  6415.                   xmode = -1;
  6416.               } else {                  /* TEXT or BINARY only */
  6417.                   xmode = x;
  6418.                   scan = 1;
  6419.               }
  6420.               break;
  6421.           }
  6422.           default:
  6423.             printf("?Not implemented yet - \"%s\"\n",atmbuf);
  6424.             return(-9);
  6425.         }
  6426.     }
  6427.     if (qflag && (cmresult.fcode == _CMFLD)) {
  6428.         if ((x = cmcfm()) < 0)
  6429.           return(x);
  6430.         else
  6431.           return(success = 0);
  6432.     }
  6433.     if (cmresult.fcode != _CMIFI) {
  6434.         if (*atmbuf) {
  6435.         int x;
  6436.             if (iswild(atmbuf) && nzxpand(atmbuf,nzxopts) == 0)
  6437.               printf("?No files match: %s\n",brstrip(atmbuf));
  6438.             else if ((x = zchki(atmbuf)) == -1)
  6439.           printf("?File not found: %s\n",brstrip(atmbuf));
  6440.         else if (x == -2)
  6441.           printf("?Not a regular file: %s\n",atmbuf);
  6442.         else
  6443.               /* printf("?Not a deletable file: %s\n",atmbuf); */
  6444.               goto tryanyway;
  6445.         } else {
  6446.             printf("?A file specification is required\n");
  6447.         }
  6448.         return(-9);
  6449.     }
  6450.   tryanyway:
  6451.     ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ); /* Safe copy of filespec */
  6452.     if (deldirs) {
  6453.         ckstrncpy(safebuf,cmresult.sresult,CKMAXPATH);
  6454. #ifdef VMSORUNIX
  6455.         len = zgetfs(tmpbuf);           /* Is it a directory name? */
  6456.         argisdir = zgfs_dir;            /* Then because of how zxpand() */
  6457.         if (argisdir && zgfs_link)      /* works, we have to add it to */
  6458.           argisdir = 0;                 /* the list. */
  6459.         if (itsadir)
  6460.           len = -2;
  6461. #else
  6462.         len = zchki(tmpbuf);
  6463.         if (len < 0)
  6464.           argisdir = isdir(tmpbuf);
  6465. #endif /* VMSORUNIX */
  6466.     }
  6467.     debug(F110,"DELETE file",tmpbuf,0);
  6468.     if ((x = cmcfm()) < 0)
  6469.       return(x);
  6470.  
  6471. #ifdef IKSD
  6472. #ifdef CK_LOGIN
  6473.     if (inserver && isguest) {
  6474.         printf("?Sorry, DELETE unavailable to guests\n");
  6475.         return(-9);
  6476.     }
  6477. #endif /* CK_LOGIN */
  6478. #endif /* IKSD */
  6479.  
  6480. #ifndef OS2ORUNIX
  6481.     if (simulate) {
  6482.         printf("?Sorry, /SIMULATE not implemented on this platform\n");
  6483.         return(-9);
  6484.     }
  6485. #endif /* OS2ORUNIX */
  6486.  
  6487. #ifdef COMMENT
  6488.     /* (not needed) */
  6489.     if (!iswild(tmpbuf)) {
  6490.         char *m;
  6491.         errno = 0;
  6492.         x = zchki(tmpbuf);
  6493.         if (x < 0) {
  6494.             switch (x) {
  6495.               case -2: m = "Not a regular file"; break;
  6496.               case -1: m = "File not found or not accessible"; break;
  6497.               default: m = errno ? ck_errstr() : "Can't delete";
  6498.             }
  6499.             printf("?%s: \"%s\"\n",m,tmpbuf);
  6500.             return(-9);
  6501.         }
  6502.     }
  6503. #endif /* COMMENT */
  6504.  
  6505.     makelist(del_exc,dxlist,8);
  6506.  
  6507. /* tmpbuf[] has the name - now do any needed conversions on it */
  6508.  
  6509. #ifdef OS2
  6510.     {   /* Lower level functions change / to \, not good for CMD.EXE. */
  6511.         char *p = tmpbuf;
  6512.         while (*p) {                    /* Change them back to \ */
  6513.             if (*p == '/') *p = '\\';
  6514.             p++;
  6515.         }
  6516.     }
  6517. #endif /* OS2 */
  6518.  
  6519. #ifdef VMS
  6520.     if (iswild(tmpbuf)) {
  6521. #ifdef COMMENT
  6522.         /* Does not handle '.' as version separator */
  6523.         char *p = tmpbuf;
  6524.         x = 0;
  6525.         while (*p) {
  6526.             if (*p == ';') {
  6527.                 x = 1;
  6528.                 break;
  6529.             } else
  6530.               p++;
  6531.         }
  6532.         if (!x) ckstrncat(tmpbuf,";*",TMPBUFSIZ);
  6533. #else
  6534.         j = 0; x = 0;                   /* for end_dot and number of dots */
  6535.         i = strlen(tmpbuf);
  6536.         if (tmpbuf[i] == ';') {
  6537.             ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  6538.         } else {
  6539.             if (tmpbuf[i--] == '.')
  6540.               j++;
  6541.             for (; i >= 0; i--) {
  6542.                 if (tmpbuf[i] == ';' || tmpbuf[i] == ':' ||
  6543.                     tmpbuf[i] == ']' || tmpbuf[i] == '>')
  6544.                   break;
  6545.                 else if (tmpbuf[i] == '.')
  6546.                   x++;
  6547.             }
  6548.             if (tmpbuf[i] != ';') {     /* dot may have been used */
  6549.                 if (j) {                /* last char is dot */
  6550.                   if (x)                /* second is version separator */
  6551.                     ckstrncat(tmpbuf,"0",TMPBUFSIZ);
  6552.                   else                  /* 'foo.' */
  6553.                     ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  6554.                 } else if (x == 1)      /* lacking a version separator */
  6555.                   ckstrncat(tmpbuf,";0",TMPBUFSIZ);
  6556.                 else if (x == 0)        /* x == 2 has a version */
  6557.                   ckstrncat(tmpbuf,".*;0",TMPBUFSIZ);
  6558.             }
  6559.         }
  6560. #endif /* COMMENT */
  6561.     }
  6562. #endif /* VMS */
  6563.  
  6564.     debug(F110,"dodel tmpbuf",tmpbuf,0); /* Filename */
  6565.  
  6566. #ifndef OS2ORUNIX                       /* No built-in DELETE code... */
  6567.     /* Construct system command. */
  6568.     ckmakmsg(line,LINBUFSIZ,DELCMD," ",tmpbuf,NULL);
  6569. #else
  6570. #ifdef VMS
  6571.     if (asking) {                       /* Maybe overwrite in VMS */
  6572.         if (x_lis)                      /* if options are needed... */
  6573.           ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm/log ",tmpbuf,NULL);
  6574.         else
  6575.           ckmakmsg(line,LINBUFSIZ,DELCMD," /confirm ",tmpbuf,NULL);
  6576.     } else if (x_lis)
  6577.       ckmakmsg(line,LINBUFSIZ,DELCMD," /log ",tmpbuf,NULL);
  6578.     conres();
  6579. #endif /* VMS */
  6580.  
  6581.     debug(F110,"dodel line",line,0);
  6582. #endif /* OS2ORUNIX */
  6583.  
  6584. #ifdef MAC
  6585.     success = (zdelet(tmpbuf) == 0);
  6586.  
  6587. #else  /* !MAC ... */
  6588.  
  6589. #ifdef OS2ORUNIX
  6590.     {
  6591.         int filespace = 0;
  6592.         int count = 0;
  6593.         int lines = 0;
  6594.         int n = 0;
  6595.  
  6596.         s = tmpbuf;
  6597.  
  6598. #ifdef CK_TTGWSIZ
  6599. #ifdef OS2
  6600.         ttgcwsz();
  6601. #else /* OS2 */
  6602.         /* Check whether window size changed */
  6603.         if (ttgwsiz() > 0) {
  6604.             if (tt_rows > 0 && tt_cols > 0) {
  6605.                 cmd_rows = tt_rows;
  6606.                 cmd_cols = tt_cols;
  6607.             }
  6608.         }
  6609. #endif /* OS2 */
  6610. #endif /* CK_TTGWSIZ */
  6611.  
  6612.         if (x_hdg > 0 && !summary) {
  6613.             printf("Deleting %s...%s\n", s, simulate ? " (SIMULATION)" : "");
  6614.             n += 2;
  6615.         }
  6616. #ifdef ZXREWIND
  6617.         z = zxrewind();                 /* Rewind file list */
  6618. #else
  6619.         if (!deldirs)
  6620.           nzxopts = ZX_FILONLY;
  6621.         if (recursive) nzxopts |= ZX_RECURSE;
  6622.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  6623.         errno = 0;
  6624.         z = nzxpand(s,nzxopts);         /* Expand file list */
  6625. #endif /* ZXREWIND */
  6626.         debug(F111,"dodel",s,z);
  6627.  
  6628.         /* If deleting directories, sort in reverse order */
  6629.         /* so we delete the files first, then the directory. */
  6630.  
  6631. #ifdef OS2
  6632.         /* In K95, we have no mtchs array, nor any control over */
  6633.         /* the order in which znext() returns filenames, so we  */
  6634.         /* must copy the array and sort it. */
  6635.         {
  6636.             int i;
  6637.             if (xmtchs) {               /* Free previous list in case */
  6638.                 debug(F101,"dodel freeing previous list","",xmtchn);
  6639.                 for (i = 0; i < xmtchn; i++) /* it wasn't freed last time. */
  6640.                   if (xmtchs[i])
  6641.                     free(xmtchs[i]);
  6642.                 free(xmtchs);
  6643.             }
  6644.             xmtchn = 0;
  6645.             xmtchs = (char **)malloc(z * (sizeof(char **))); /* Make new one */
  6646.             if (!xmtchs) {
  6647.                 printf("?Memory allocation failure\n");
  6648.                 return(-9);
  6649.             }
  6650.             for (i = 0; i < z; i++) {
  6651.                 xmtchs[i] = NULL;
  6652.                 znext(tmpbuf);
  6653.                 if (!*tmpbuf)
  6654.                   break;
  6655.                 makestr(&(xmtchs[i]),tmpbuf);
  6656.                 if (!xmtchs[i]) {
  6657.                     printf("?Memory allocation failure\n");
  6658.                     xmtchn = i - 1;
  6659.                     rc = -9;
  6660.                     goto xdelete;
  6661.                 }
  6662.                 /* debug(F111,"dodel add",xmtchs[i],i); */
  6663.             }
  6664.             xmtchn = i;
  6665.             debug(F101,"dodel xmtchn","",xmtchn);
  6666.             sh_sort(xmtchs,NULL,z,0,deldirs,0);
  6667.         }
  6668. #else
  6669. #ifdef UNIX
  6670.         sh_sort(mtchs,NULL,z,0,deldirs,filecase);
  6671. #endif /* UNIX */
  6672. #endif /* OS2 */
  6673.  
  6674.         if (z > 0) {
  6675.             int i;
  6676. #ifdef OS2
  6677.             int ix = 0;
  6678. #endif /* OS2 */
  6679.             success = 1;
  6680.             if (x_hdg > 0)
  6681.               printf("\n");
  6682.  
  6683.             while (
  6684. #ifdef OS2
  6685.                    ix < xmtchn
  6686. #else
  6687.                    1
  6688. #endif /* OS2 */
  6689.                    ) {                  /* Loop for all files */
  6690. #ifdef OS2
  6691.                 ckstrncpy(tmpbuf,xmtchs[ix++],TMPBUFSIZ);
  6692. #else
  6693.                 znext(tmpbuf);          /* Get next file */
  6694. #endif /* OS2 */
  6695.                 if (!*tmpbuf) {         /* No more */
  6696.                     if (deldirs && recursive && argisdir) {
  6697.                         ckstrncpy(tmpbuf,safebuf,TMPBUFSIZ);
  6698.                         argisdir = 0;   /* (only do this once) */
  6699.                     } else
  6700.                       break;
  6701.                 }
  6702.                 skip = 0;
  6703.                 if (!deltree) {
  6704.                     if (fs)
  6705.                       if (fileselect(tmpbuf,
  6706.                                      del_aft,del_bef,del_naf,del_nbf,
  6707.                                      minsize,maxsize,0,8,dxlist) < 1) {
  6708.                           skip++;
  6709.                       }
  6710.                 }
  6711.                 if (!skip && scan && itsadir) {
  6712.                     skip++;
  6713.                 }
  6714.                 if (!skip && scan) {
  6715.                       switch (scanfile(tmpbuf,&y,nscanfile)) {
  6716.                       case FT_BIN:
  6717.                         if (xmode != 1)
  6718.                           skip++;
  6719.                         break;
  6720.                       case FT_TEXT:
  6721.                       case FT_7BIT:
  6722.                       case FT_8BIT:
  6723. #ifdef UNICODE
  6724.                       case FT_UTF8:
  6725.                       case FT_UCS2:
  6726. #endif /* UNICODE */
  6727.                         if (xmode != 0)
  6728.                           skip++;
  6729.                     }
  6730.                 }
  6731.                 if (!skip && asking) {
  6732.                     int x;
  6733.                     ckmakmsg(line,LINBUFSIZ," Delete ",tmpbuf,"? ",NULL);
  6734.                     x = getyesno(line,3);
  6735.                     switch (x) {
  6736.                       case 0: continue;           /* no */
  6737.                       case 1: break;              /* yes */
  6738.                       case 2: goto xdelete;       /* quit */
  6739.                       case 3: asking = 0; break;  /* go */
  6740.                     }
  6741.                 }
  6742. #ifdef VMSORUNIX
  6743.                 len = zgetfs(tmpbuf);   /* Get length and accessibility */
  6744.                 itsadir = zgfs_dir;
  6745.                 if (itsadir && zgfs_link) { /* Treat links to directories */
  6746.                     itsadir = 0;        /* as regular files */
  6747.                     if (scan)           /* But not if /TYPE: was given */
  6748.                       skip++;
  6749.                 }
  6750.                 if (itsadir)            /* (emulate non-Unix code) */
  6751.                   len = -2;
  6752. #else
  6753.                 len = zchki(tmpbuf);    /* Get accessibility */
  6754.                 if (len < 0)            /* See if it's a directory */
  6755.                   itsadir = isdir(tmpbuf);
  6756. #endif /* VMSORUNIX */
  6757.  
  6758.                 if (skip) {
  6759. #ifdef COMMENT                          /* Too verbose */
  6760.                     if (x_lis > 0) {
  6761.                         lines++;
  6762.                         printf(" %s (SKIPPED)\n",tmpbuf);
  6763. #ifdef CK_TTGWSIZ
  6764.                         if (++n > cmd_rows - 3)
  6765.                           if (!askmore()) goto xdelete; else n = 0;
  6766. #endif /* CK_TTGWSIZ */
  6767.                     }
  6768. #endif /* COMMENT */
  6769.                     continue;
  6770.                 }
  6771.  
  6772.                 debug(F111,"DELETE len",tmpbuf,len);
  6773.                 if (simulate) {
  6774.                     filespace += len;
  6775.                     count++;
  6776.                     if (x_lis > 0) {
  6777.                         lines++;
  6778.                         printf(" %s (SELECTED)\n",tmpbuf);
  6779.                         if (++n > cmd_rows - 3) {
  6780.                             int xx;
  6781.                             xx = askmore();
  6782.                             if (!xx) goto xdelete; else n = 0;
  6783.                         }
  6784.                     }
  6785.                 } else if (len >= 0 || !itsadir) { /* Regular file */
  6786.                     zdelet(tmpbuf);                /* or symlink, etc... */
  6787.                     if (zchki(tmpbuf) < 0) {
  6788.                         filespace += len;
  6789.                         count++;
  6790.                         if (x_lis > 0) {
  6791.                             lines++;
  6792.                             printf(" %s (OK)\n",tmpbuf);
  6793.                             if (++n > cmd_rows - 3)
  6794.                               if (!askmore()) goto xdelete; else n = 0;
  6795.                         }
  6796.                     } else {
  6797.                         bad++;
  6798.                         success = 0;
  6799.                         if (x_lis > 0) {
  6800.                             lines++;
  6801.                             printf(" %s (FAILED: %s)\n",tmpbuf,ck_errstr());
  6802.                             if (++n > cmd_rows - 3)
  6803.                               if (!askmore()) goto xdelete; else n = 0;
  6804.                         }
  6805.                     }
  6806.                 } else if (/* pass > 0 && */ deldirs && itsadir) {
  6807.                     /* It's a directory */
  6808.                     if (zrmdir(tmpbuf) > -1) { /* Only works if empty */
  6809.                         count++;
  6810.                         if (x_lis > 0) {
  6811.                             lines++;
  6812.                             printf(" %s (OK)\n",tmpbuf);
  6813.                             if (++n > cmd_rows - 3)
  6814.                               if (!askmore()) goto xdelete; else n = 0;
  6815.                         }
  6816.                     } else {
  6817.                         success = 0;
  6818.                         if (x_lis > 0) {
  6819.                             lines++;
  6820.                             printf(" %s (FAILED: %s)\n",
  6821.                                    tmpbuf,
  6822.                                    ck_errstr());
  6823.                             if (++n > cmd_rows - 3)
  6824.                               if (!askmore()) goto xdelete; else n = 0;
  6825.                         }
  6826.                     }
  6827.                 } else if (x_lis > 0) {
  6828.                     lines++;
  6829.                     if (isdir(tmpbuf))
  6830.                       printf(" %s (FAILED: directory)\n",tmpbuf);
  6831.                     else
  6832.                       printf(" %s (FAILED: not a regular file)\n",tmpbuf);
  6833.                     if (++n > cmd_rows - 3)
  6834.                       if (!askmore()) goto xdelete; else n = 0;
  6835.                 }
  6836.             }
  6837.             if (x_hdg > 0) {
  6838.                 if (lines > 0)
  6839.                   printf("\n");
  6840.                 if (++n > cmd_rows - 3)
  6841.                   if (!askmore()) goto xdelete; else n = 0;
  6842.                 printf("%d file%s %sdeleted, %d byte%s %sfreed%s\n",
  6843.                        count,
  6844.                        count != 1 ? "s" : "",
  6845.                        simulate ? "would be " : "",
  6846.                        filespace,
  6847.                        filespace != 1 ? "s" : "",
  6848.                        simulate ? "would be " : "",
  6849.                        simulate ? " (maybe)" : ""
  6850.                        );
  6851.             }
  6852.             if (!x_lis && !success && !quiet) {
  6853.                 printf("?DELETE failed for %d file%s \
  6854. (use DELETE /LIST to see details)\n",
  6855.                        bad, bad == 1 ? "" : "s"
  6856.                        );
  6857.             }
  6858.         } else if (x_lis > 0) {
  6859.             if (errno)
  6860.               printf("?%s: %s\n",ck_errstr(), tmpbuf);
  6861.             else
  6862.               printf("?Can't delete: %s\n",tmpbuf);
  6863.         }
  6864.     }
  6865. #else /* OS2ORUNIX */
  6866. #ifndef VMS                             /* Others - let the system do it. */
  6867.     xsystem(line);
  6868.     x = nzxpand(tmpbuf,nzxopts);
  6869.     success = (x > 0) ? 0 : 1;
  6870.     if (x_hdg > 0)
  6871.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6872. #else
  6873.     if (asking)
  6874.       printf("\n");
  6875.     x = xsystem(line);                  /* zshcmd returns 1 for success */
  6876.     success = (x > 0) ? 1 : 0;
  6877.     if (x_hdg > 0 && !asking)
  6878.       printf("%s - %sdeleted\n", tmpbuf, success ? "" : "not ");
  6879.     concb((char)escape);
  6880. #endif /* VMS */
  6881. #endif /* OS2ORUNIX */
  6882. #endif /* MAC */
  6883.   xdelete:
  6884.     if (g_matchdot > -1) {
  6885.         matchdot = g_matchdot;          /* Restore these... */
  6886.         g_matchdot = -1;
  6887.     }
  6888. #ifdef OS2
  6889.     if (xmtchs) {
  6890.         int i;
  6891.         debug(F101,"dodel freeing list","",xmtchn);
  6892.         for (i = 0; i < xmtchn; i++)
  6893.           if (xmtchs[i]) free(xmtchs[i]);
  6894.         free(xmtchs);
  6895.         xmtchs = NULL;
  6896.         xmtchn = 0;
  6897.     }
  6898. #endif /* OS2 */
  6899.     debug(F101,"dodel result","",rc);
  6900.     return((rc < 0) ? rc : success);
  6901. }
  6902. #endif /* NOFRILLS */
  6903.  
  6904. #ifndef NOSPL                           /* The ELSE command */
  6905. _PROTOTYP( VOID pushqcmd, (char *) );
  6906.  
  6907. int
  6908. doelse() {
  6909.     if (!ifcmd[cmdlvl]) {
  6910.         printf("?ELSE doesn't follow IF\n");
  6911.         return(-2);
  6912.     }
  6913. #ifdef COMMENT
  6914. /*
  6915.   Wrong.  This prevents IF..ELSE IF...ELSE IF...ELSE IF...ELSE...
  6916.   from working.
  6917. */
  6918.     ifcmd[cmdlvl] = 0;
  6919. #endif /* COMMENT */
  6920.     if (!iftest[cmdlvl]) {              /* If IF was false do ELSE part */
  6921.         if (maclvl > -1 || tlevel > -1) { /* In macro or command file */
  6922.             debug(F100,"doelse pushing","",0);
  6923. #ifndef COMMENT
  6924.             pushcmd(NULL);              /* save rest of command. */
  6925. #else
  6926.             /* This fixes certain obscure problems */
  6927.             /* but breaks many other constructions that must work. */
  6928.             pushqcmd(NULL);
  6929. #endif /* COMMENT */
  6930.         } else {                        /* If interactive, */
  6931.             cmini(ckxech);              /* just start a new command */
  6932.             printf("\n");               /* (like in MS-DOS Kermit) */
  6933.             if (pflag) prompt(xxstring);
  6934.         }
  6935.     } else {                            /* Condition is false */
  6936.         if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  6937.           return(y);                    /* Gobble up rest of line */
  6938.     }
  6939.     return(0);
  6940. }
  6941. #endif /* NOSPL */
  6942.  
  6943. #ifndef NOSPL
  6944. int
  6945. doswitch() {
  6946.     char *lp, *ap;                      /* Macro argument pointer */
  6947.     int len = 0, x, y, pp = 0;
  6948.     char brbuf[3];
  6949.  
  6950.     /* Get variable name */
  6951.  
  6952.     tmpbuf[0] = NUL;
  6953.     brbuf[0] = '{';
  6954.     brbuf[1] = '}';
  6955.     brbuf[2] = NUL;
  6956.  
  6957.     y = cmfld("Variable name","",&s,xxstring);
  6958.     debug(F111,"doswitch cmfld",s,y);
  6959.     if (y < 0) {
  6960.         if (y == -3)            /* Because brstrip() writes */
  6961.           s = brbuf;            /* into its argument. */
  6962.         else
  6963.           return(y);
  6964.     }
  6965.     debug(F110,"doswitch A",s,0);
  6966.     if (!strcmp(s,"(")) {
  6967.         pp++;
  6968.         if ((y = cmfld("Variable name","",&s,xxstring)) < 0) {
  6969.             if (y == -3)
  6970.               s = brbuf;
  6971.             else
  6972.               return(y);
  6973.         debug(F110,"doswitch B",s,0);
  6974.         }
  6975.     }
  6976.     len = ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  6977.     if (tmpbuf[0] == CMDQ) {
  6978.         if (chkvar(s) < 1) {
  6979.             printf("?Variable name required\n");
  6980.             return(-9);
  6981.         }
  6982.     }
  6983.     if (pp > 0) {                       /* If open paren given parse closing */
  6984.         if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  6985.           return(y);
  6986.         if (strcmp(atmbuf,")")) {
  6987.             printf("?Closing parenthesis required\n");
  6988.             return(-9);
  6989.         }
  6990.     }
  6991.     lp = line;
  6992.     x = ckstrncpy(lp,"_switx ",LINBUFSIZ); /* _switx + space */
  6993.     lp += x;
  6994.     ap = lp;
  6995.     debug(F010,"SWITCH a",line,0);
  6996.  
  6997. #ifdef COMMENT
  6998.     x = ckmakmsg(lp,LINBUFSIZ-x,tmpbuf," ",NULL,NULL); /* variable name + SP */
  6999. #else
  7000.     {                                   /* variable name + SP */
  7001.         char * p = tmpbuf;
  7002.     if (len > 0) {
  7003.         if (tmpbuf[0] == '(' && tmpbuf[len-1] == ')') {
  7004.         tmpbuf[len-1] = NUL;
  7005.         p++;
  7006.         }
  7007.         }
  7008.         x = ckmakmsg(lp,LINBUFSIZ-x,"{",brstrip(p),"}"," ");
  7009.     }
  7010. #endif /* COMMENT */
  7011.     debug(F010,"SWITCH b",line,0);
  7012.     lp += x;
  7013.  
  7014.     /* Get body */
  7015.  
  7016.     if ((y = cmtxt("series of cases","",&s,NULL)) < 0) return(y);
  7017.     if ((y = (int)strlen(s)) < 1) return(-2);
  7018.     if (s[0] != '{' && s[y-1] != '}') { /* Supply braces if missing */
  7019.         ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  7020.         s = tmpbuf;
  7021.     }
  7022.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  7023.         printf("?Unbalanced braces\n");
  7024.         return(0);
  7025.     }
  7026.     debug(F010,"SWITCH c",line,0);
  7027.  
  7028.     x = mlook(mactab,"_switx",nmac);    /* Look up SWITCH macro definition */
  7029.     if (x < 0) {                        /* Not there? */
  7030.         addmmac("_switx",sw_def);       /* Put it back. */
  7031.         if ((x = mlook(mactab,"_switx",nmac)) < 0) { /* Look it up again. */
  7032.             printf("?SWITCH macro definition gone!\n"); /* Shouldn't happen. */
  7033.             return(success = 0);
  7034.         }
  7035.     }
  7036.     debug(F010,"SWITCH command",line,0); /* Execute the SWITCH macro. */
  7037.     success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  7038.     debug(F101,"SWITCH status","",success);
  7039.     return(success);
  7040. }
  7041.  
  7042. int
  7043. dofor() {                               /* The FOR command. */
  7044.     int i, fx, fy, fz;                  /* loop variables */
  7045.     char *ap, *di;                      /* macro argument pointer */
  7046.     int pp = 0;                         /* Paren level */
  7047.     int mustquote = 0;
  7048.  
  7049.     for (i = 0; i < 2; i++) {
  7050.         if ((y = cmfld("Variable name","",&s,NULL)) < 0) {
  7051.             if (y == -3) {
  7052.                 printf("?Variable name required\n");
  7053.                 return(-9);
  7054.             } else
  7055.               return(y);
  7056.         }
  7057.         if (strcmp(s,"("))
  7058.           break;
  7059.         pp++;
  7060.     }
  7061. #ifdef COMMENT
  7062.     if ((y = parsevar(s,&x,&z)) < 0)    /* Check variable. */
  7063.       return(y);
  7064. #else
  7065.     if (*s == CMDQ)                     /* If loop variable starts with */
  7066.       mustquote++;                      /* backslash, mustquote is > 0. */
  7067. #endif /* COMMENT */
  7068.  
  7069.     lp = line;                          /* Build a copy of the command */
  7070.     ckstrncpy(lp,"_forx ",LINBUFSIZ);
  7071.     lp += (int)strlen(line);            /* "_for" macro. */
  7072.     ap = lp;                            /* Save pointer to macro args. */
  7073.  
  7074.     if (*s == CMDQ) s++;                /* Skip past backslash if any. */
  7075.     while ((*lp++ = *s++)) ;            /* copy it */
  7076.     lp--; *lp++ = SP;                   /* add a space */
  7077.  
  7078.     if ((y = cmnum("initial value","",10,&fx,xxstring)) < 0) {
  7079.         if (y == -3) return(-2);
  7080.         else return(y);
  7081.     }
  7082.     debug(F101,"dofor fx","",fx);
  7083.     s = atmbuf;                         /* Copy the atom buffer */
  7084.  
  7085.     if ((int)strlen(s) < 1) goto badfor;
  7086. /*
  7087.   In edit 192, we change the loop variables to be evaluated at loop entry,
  7088.   not each time through the loop.  This was required in order to allow
  7089.   \v(argc) to be used as a loop variable, or in a loop-variable expression.
  7090.   Thus, we can't have FOR loops that modify their own exit conditions by
  7091.   changing the final value or the increment.  The problem with \v(argc) was
  7092.   that it is on the macro stack; after entry into the _forx macro, it is at
  7093.   the wrong place.
  7094. */
  7095.     sprintf(tmpbuf,"%d",fx);            /* (SAFE) Substitute actual value */
  7096.     s = tmpbuf;
  7097.     while ((*lp++ = *s++)) ;            /* (what they actually typed) */
  7098.     lp--; *lp++ = SP;
  7099. #ifdef DEBUG
  7100.     *lp = NUL;
  7101.     debug(F110,"FOR A",line,0);
  7102. #endif /* DEBUG */
  7103.  
  7104.     if ((y = cmnum("final value","",10,&fy,xxstring)) < 0) {
  7105.         if (y == -3) return(-2);
  7106.         else return(y);
  7107.     }
  7108.     debug(F101,"dofor fy","",fy);
  7109.     s = atmbuf;                         /* Same deal */
  7110.     if ((int)strlen(s) < 1)
  7111.       goto badfor;
  7112.  
  7113.     sprintf(tmpbuf,"%d",fy);            /* SAFE */
  7114.     s = tmpbuf;
  7115.     while ((*lp++ = *s++)) ;
  7116.     lp--;
  7117.     *lp++ = SP;
  7118. #ifdef DEBUG
  7119.     *lp = NUL;
  7120.     debug(F110,"FOR B",line,0);
  7121. #endif /* DEBUG */
  7122.  
  7123.     x_ifnum = 1;                        /* Increment or parenthesis */
  7124.     di = (fx < fy) ? "1" : "-1";        /* Default increment */
  7125.     if ((y = cmnum("increment",di,10,&fz,xxstring)) < 0) {
  7126.         debug(F111,"dofor increment",atmbuf,y);
  7127.         x_ifnum = 0;
  7128.         if (y == -3) {                  /* Premature termination */
  7129.             return(-2);
  7130.         } else if (y == -2) {           /* Maybe closing paren */
  7131.             if (!strcmp(atmbuf,")")) {
  7132.                 pp--;                   /* Count it */
  7133.                 s = di;                 /* supply default interval */
  7134.                 fz = atoi(s);
  7135.             } else                      /* Not closing paren, invalid */
  7136.               return(y);
  7137.         } else                          /* Other error */
  7138.           return(y);
  7139.     } else {                            /* Number */
  7140.         x_ifnum = 0;
  7141.         debug(F101,"dofor fz","",fz);
  7142.         s = atmbuf;                     /* Use it */
  7143.     }
  7144.     if ((int)strlen(s) < 1)
  7145.       goto badfor;
  7146.  
  7147.     sprintf(tmpbuf,"%d",fz);            /* (SAFE) Same deal */
  7148.     s = tmpbuf;
  7149.     while ((*lp++ = *s++)) ;
  7150.     lp--; *lp++ = SP;
  7151.  
  7152. #ifdef DEBUG
  7153.     *lp = NUL;
  7154.     debug(F110,"FOR C",line,0);
  7155. #endif /* DEBUG */
  7156.  
  7157.     /* Insert the appropriate comparison operator */
  7158.     if (fz < 0)
  7159.       *lp++ = '<';
  7160.     else
  7161.       *lp++ = '>';
  7162.     *lp++ = SP;
  7163.  
  7164. #ifdef DEBUG
  7165.     *lp = NUL;
  7166.     debug(F110,"FOR D",line,0);
  7167. #endif /* DEBUG */
  7168.  
  7169.     if (pp > 0) {                       /* If open paren given parse closing */
  7170.         if ((y = cmfld("Closing parenthesis","",&s,NULL)) < 0)
  7171.           return(y);
  7172.         if (strcmp(atmbuf,")")) {
  7173.             printf("?Closing parenthesis required\n");
  7174.             return(-9);
  7175.         }
  7176.     }
  7177.     if ((y = cmtxt("Command to execute","",&s,NULL)) < 0) return(y);
  7178.     if ((y = (int)strlen(s)) < 1) return(-2);
  7179.     if (s[0] != '{' && s[y-1] != '}') { /* Supply braces if missing */
  7180.         ckmakmsg(tmpbuf,TMPBUFSIZ,"{ ",s," }",NULL);
  7181.         s = tmpbuf;
  7182.     }
  7183.     if (litcmd(&s,&lp,(LINBUFSIZ - (lp - (char *)line) - 2)) < 0) {
  7184.         printf("?Unbalanced braces\n");
  7185.         return(0);
  7186.     }
  7187. #ifdef DEBUG
  7188.     *lp = NUL;
  7189.     debug(F110,"FOR E",line,0);
  7190. #endif /* DEBUG */
  7191.  
  7192. #ifdef COMMENT
  7193. /* Too strict */
  7194.     if (fz == 0) {
  7195.         printf("?Zero increment not allowed\n");
  7196.         return(0);
  7197.     }
  7198. #endif /* COMMENT */
  7199. /*
  7200.   In version 8.0 we decided to allow macro names anyplace a numeric-valed
  7201.   variable could appear.  But this caused trouble for the FOR loops because
  7202.   the quoting in for_def[] assumed a \%i-style loop variable.  We account
  7203.   for this here in the if (mustquote)...else logic by invoking separate
  7204.   FOR macro definitions in the two cases.
  7205. */
  7206.     if (mustquote) {                    /* \%i-style loop variable */
  7207.         x = mlook(mactab,"_forx",nmac); /* Look up FOR macro definition */
  7208.         if (x < 0) {                    /* Not there? */
  7209.             addmmac("_forx",for_def);   /* Put it back. */
  7210.             if ((x = mlook(mactab,"_forx",nmac)) < 0) { /* Look it up again. */
  7211.                 printf("?FOR macro definition gone!\n");
  7212.                 return(success = 0);
  7213.             }
  7214.         }
  7215.     } else {                            /* Loop variable is a macro */
  7216.         x = mlook(mactab,"_forz",nmac);
  7217.         if (x < 0) {
  7218.             addmmac("_forz",foz_def);
  7219.             if ((x = mlook(mactab,"_forz",nmac)) < 0) {
  7220.                 printf("?FOR macro definition gone!\n");
  7221.                 return(success = 0);
  7222.             }
  7223.         }
  7224.     }
  7225.     debug(F010,"FOR command",line,0);   /* Execute the FOR macro. */
  7226.     return(success = dodo(x,ap,cmdstk[cmdlvl].ccflgs | CF_IMAC));
  7227.  
  7228. badfor:
  7229.     printf("?Incomplete FOR command\n");
  7230.     return(-2);
  7231. }
  7232. #endif /* NOSPL */
  7233.  
  7234. #ifndef NOFRILLS
  7235. /* Do the BUG command */
  7236.  
  7237. int
  7238. dobug() {
  7239.     int n;
  7240.     char * s = "";
  7241.     extern char * k_info_dir;
  7242.  
  7243.     if (k_info_dir)
  7244.       s = k_info_dir;
  7245.  
  7246. #ifdef COMMENT
  7247.     printf("\n%s,%s\n Numeric: %ld",versio,ckxsys,vernum);
  7248. #endif /* COMMENT */
  7249.     printf(
  7250. "\nBefore requesting technical support from Columbia U., please consult:\n\n"
  7251.            );
  7252.     n = 7;
  7253. #ifdef OS2
  7254.     printf(" . Your \"Kermit 95\" user manual (use the MANUAL command).\n");
  7255.     printf(" . The technical reference manual, \"Using C-Kermit\".\n");
  7256.     n += 2;
  7257. #else
  7258.     printf(" . The book \"Using C-Kermit\" (type HELP for more info).\n");
  7259.     n += 1;
  7260. #endif /* OS2 */
  7261.  
  7262.     printf(" . Your own organization's support staff, if any.\n");
  7263.     printf(
  7264. " . The comp.protocols.kermit.misc newsgroup.\n");
  7265.     printf(
  7266. " . The Kermit support website, http://www.columbia.edu/kermit/support.html \n"
  7267.            );
  7268.     printf(
  7269.  
  7270. " . The Kermit FAQ, http://www.columbia.edu/kermit/newfaq.html \n");
  7271. #ifdef OS2
  7272.     printf(
  7273. " . The Kermit 95 FAQ, http://www.columbia.edu/kermit/k95faq.html \n");
  7274.     n++;
  7275. #endif /* OS2 */
  7276.  
  7277.     printf(
  7278. " . The C-Kermit FAQ, http://www.columbia.edu/kermit/ckfaq.html \n");
  7279.     n += 4;
  7280.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7281.     printf("\n\
  7282. If you still need help or have a bug to report after consulting these sources,"
  7283.            );
  7284.     printf("\nsend e-mail to:\n\n");
  7285.     n += 2;
  7286.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7287.     printf("  mailto:kermit-support@columbia.edu\n\n");
  7288.     n += 1;
  7289.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7290.     printf("Or contact us by post:\n\n");
  7291.     printf(
  7292. "  Kermit, Columbia University, 612 W 115 Street, New York NY  10025, USA\n\n"
  7293.            );
  7294.     n += 1;
  7295.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7296.     printf("Or by fax at +1 (212) 662-6442.\n\n");
  7297.     n += 1;
  7298.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7299. #ifdef COMMENT
  7300.     printf("Telephone support is available too:\n\n");
  7301.     n += 1;
  7302.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7303.     printf(
  7304.     "  +1 (212) 854-5126, from anywhere, $25.00 USD per call, MC/Visa\n\n");
  7305.     n += 1;
  7306.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7307. #endif /* COMMENT */
  7308. #ifndef NOSHOW
  7309. #ifndef NOFRILLS
  7310.     printf(
  7311. "Before reporting problems, please use the SHOW FEATURES command\n");
  7312.     if (++n > cmd_rows - 3) if (!askmore()) return(0); else n = 0;
  7313.     printf(
  7314. "to get detailed program version and configuration information.\n\n");
  7315. #endif /* NOFRILLS */
  7316. #endif /* NOSHOW */
  7317.     return(1);
  7318. }
  7319. #endif /* NOFRILLS */
  7320.  
  7321. #ifndef NOSPL
  7322.  
  7323. /*  T O D 2 S E C  --  Convert time of day as hh:mm:ss to secs since midnite */
  7324. /*
  7325.   Call with a string hh:mm or hh:mm:ss.
  7326.   Returns a 0 to 86400 on success, or a negative number on failure.
  7327. */
  7328. long
  7329. tod2sec(t) char * t; {
  7330.     long t2;
  7331.     long hh = 0L, mm = 0L, ss = 0L;
  7332.  
  7333.     if (!t) t = "";
  7334.     if (!*t)
  7335.       return(-3L);
  7336.     debug(F110,"tod2sec",t,0);
  7337.  
  7338.     if (isdigit(*t))                    /* Get hours from argument */
  7339.       hh = *t++ - '0';
  7340.     else
  7341.       return(-1L);
  7342.     if (isdigit(*t))
  7343.       hh = hh * 10 + *t++ - '0';
  7344. #ifdef COMMENT
  7345.     if (hh > 24L)
  7346.       return(-1L);
  7347. #endif /* COMMENT */
  7348.     if (*t == ':')
  7349.       t++;
  7350.     else if (!*t)
  7351.       goto xtod2sec;
  7352.     else
  7353.       return(-1L);
  7354.  
  7355.     if (isdigit(*t))                    /* Minutes */
  7356.       mm = *t++ - '0';
  7357.     else
  7358.       return(-1L);
  7359.     if (isdigit(*t))
  7360.       mm = mm * 10 + *t++ - '0';
  7361.     if (mm > 60L)
  7362.       return(-1L);
  7363.     if (*t == ':')
  7364.       t++;
  7365.     else if (!*t)
  7366.       goto xtod2sec;
  7367.     else
  7368.       return(-1L);
  7369.  
  7370.     if (isdigit(*t))                    /* Seconds */
  7371.       ss = *t++ - '0';
  7372.     else
  7373.       return(-1L);
  7374.     if (isdigit(*t))
  7375.       ss = ss * 10 + *t++ - '0';
  7376.     if (ss > 60L)
  7377.       return(-1L);
  7378.  
  7379.     if (*t > 32)                        /* No trailing junk allowed */
  7380.       return(-1L);
  7381.  
  7382.   xtod2sec:
  7383.  
  7384.     t2 = hh * 3600L + mm * 60L + ss;    /* Seconds since midnight from arg */
  7385.     debug(F101,"tod2sec t2","",t2);
  7386.  
  7387.     return(t2);
  7388. }
  7389.  
  7390. int waitinterval = 1;
  7391.  
  7392. #ifdef OLDWAIT
  7393. #undef OLDWAIT
  7394. #endif /* OLDWAIT */
  7395.  
  7396. int kbchar = NUL;
  7397.  
  7398. int
  7399. dopaus(cx) int cx; {
  7400.     long zz;
  7401.     extern int sleepcan;
  7402.  
  7403. #ifdef OLDWAIT
  7404.     zz = -1L;
  7405.     x_ifnum = 1;                        /* Turn off internal complaints */
  7406.     if (cx == XXWAI)
  7407.       y = cmnum("seconds to wait, or time of day hh:mm:ss","1",10,&x,xxstring);
  7408.     else if (cx == XXPAU)
  7409.       y = cmnum("seconds to pause, or time of day hh:mm:ss",
  7410.                 "1",10,&x,xxstring);
  7411.     else
  7412.       y = cmnum("milliseconds to sleep, or time of day hh:mm:ss",
  7413.                 "100",10,&x,xxstring);
  7414.     x_ifnum = 0;
  7415.     if (y < 0) {
  7416.         if (y == -2) {                  /* Invalid number or expression */
  7417.             char *p = tmpbuf;           /* Retrieve string from atmbuf */
  7418.             int n = TMPBUFSIZ;
  7419.             *p = NUL;
  7420.             zzstring(atmbuf,&p,&n);     /* Evaluate in case it's a variable */
  7421.             zz = tod2sec(tmpbuf);       /* Convert to secs since midnight */
  7422.             if (zz < 0L) {
  7423.                 printf("?Number, expression, or time of day required\n");
  7424.                 return(-9);
  7425.             } else {
  7426.                 char now[32];           /* Current time */
  7427.                 char *p;
  7428.                 long tnow;
  7429.                 p = now;
  7430.                 ztime(&p);
  7431.                 tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  7432.                 if (zz < tnow)          /* User's time before now */
  7433.                   zz += 86400L;         /* So make it tomorrow */
  7434.                 zz -= tnow;             /* Seconds from now. */
  7435.             }
  7436.         } else
  7437.           return(y);
  7438.     }
  7439.     if (x < 0) x = 0;
  7440.     switch (cx) {
  7441.       case XXPAU:                       /* PAUSE */
  7442.       case XXMSL:                       /* MSLEEP */
  7443.         if ((y = cmcfm()) < 0) return(y);
  7444.         break;
  7445.       case XXWAI:                       /* WAIT */
  7446.         z = 0;                          /* Modem signal mask */
  7447.         while (1) {                     /* Read zero or more signal names */
  7448.             y = cmkey(mstab,nms,"modem signal","",xxstring);
  7449.             if (y == -3) break;         /* -3 means they typed CR */
  7450.             if (y < 0) return(y);       /* Other negatives are errors */
  7451.             z |= y;                     /* OR the bit into the signal mask */
  7452.         }
  7453.         if ((y = cmcfm()) < 0) return(y);
  7454.         break;
  7455.  
  7456.       default:                          /* Shouldn't happen */
  7457.         return(-2);
  7458.     }
  7459.  
  7460. /* Command is entered, now do it. */
  7461.  
  7462.     if (zz > -1L) {                     /* Time of day given? */
  7463.         x = zz;
  7464.         if (zz != (long) x) {
  7465.             printf(
  7466. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7467.                    );
  7468.             return(-9);
  7469.         }
  7470.     }
  7471.     if (cx == XXMSL) {                  /* Millisecond sleep */
  7472.         msleep(zz < 0 ? x : x * 1000);
  7473.         return(success = 1);
  7474.     }
  7475.     if (cx == XXPAU && !sleepcan) {     /* SLEEP CANCELLATION is OFF */
  7476.         sleep(x);
  7477.         return(success = 1);
  7478.     }
  7479.  
  7480.     /* WAIT, or else SLEEP with cancellation allowed... */
  7481.  
  7482.     do {                                /* Sleep loop */
  7483.         int mdmsig;
  7484.         if (sleepcan) {                 /* Keyboard cancellation allowed? */
  7485.             if (y = conchk()) {         /* Did they type something? */
  7486. #ifdef COMMENT
  7487.                 while (y--) coninc(0);  /* Yes, gobble it all up */
  7488. #else
  7489.                 /* There is a debate over whether PAUSE should absorb    */
  7490.                 /* its cancelling character(s).  There are several       */
  7491.                 /* reasons why it should gobble at least one character:  */
  7492.                 /* (1) MS-DOS Kermit does it                             */
  7493.                 /* (2) if not, subsequent PAUSE commands will terminate  */
  7494.                 /*     immediately                                       */
  7495.                 /* (3) if not, subsequent ASK commands will use it as    */
  7496.                 /*     valid input.  If \13, then it will get no input   */
  7497.                 /* (4) if not, then the character appears on the command */
  7498.                 /*     line after all enclosing macros are complete.     */
  7499.                 kbchar = coninc(0);     /* Gobble one up */
  7500. #endif /* COMMENT */
  7501.                 break;                  /* And quit PAUSing or WAITing */
  7502.             }
  7503.         }
  7504.         if (cx == XXWAI) {              /* WAIT (z == modem signal mask) */
  7505.             debug(F101,"WAIT x","",x);
  7506.             if (z > 0) {                /* Looking for any modem signals? */
  7507.                 mdmsig = ttgmdm();      /* Yes, get them */
  7508.                 if (mdmsig < 0)         /* Failed */
  7509.                   return(success = 0);
  7510.                 if ((mdmsig & z) == z)  /* Got what we wanted? */
  7511.                   return(success = 1);  /* Succeed */
  7512.             }
  7513.             if (x == 0)                 /* WAIT 0 and didn't get our signals */
  7514.               break;
  7515.         }
  7516.         sleep(1);                       /* No interrupt, sleep one second */
  7517.     } while (--x > 0);
  7518.  
  7519.     if (cx == XXWAI)                    /* If WAIT and loop exhausted */
  7520.       success = (z == 0);               /* Fail. */
  7521.     else                                /*  */
  7522.       success = (x == 0);               /* Set SUCCESS/FAILURE for PAUSE. */
  7523.     return(success);
  7524.  
  7525. #else  /* New code uses chained FDBs and allows FILE waits... */
  7526.  
  7527.     char * m = "";                      /* Help message */
  7528.     struct FDB nu, fl;                  /* Parse function descriptor blocks */
  7529.     int filewait = 0;
  7530.     int mdmsig = 0, fs = 0;
  7531.     char filedate[32];
  7532.  
  7533.     kbchar = 0;
  7534.  
  7535.     switch (cx) {
  7536.       case XXWAI: m = "seconds to wait, or time of day hh:mm:ss"; break;
  7537.       case XXPAU: m = "seconds to pause, or time of day hh:mm:ss"; break;
  7538.       case XXMSL: m = "milliseconds to sleep, or time of day hh:mm:ss"; break;
  7539.     }
  7540.     zz = -1L;
  7541.     cmfdbi(&nu,
  7542.            _CMNUM,                      /* Number */
  7543.            m,                           /* Help message */
  7544.            (cx == XXMSL) ? "100" : "1", /* Default */
  7545.            "",                          /* N/A */
  7546.            0,                           /* N/A */
  7547.            0,                           /* N/A */
  7548.            xxstring,                    /* Processing function */
  7549.            NULL,                        /* N/A */
  7550.            &fl                          /* Next */
  7551.            );
  7552.     cmfdbi(&fl,                         /* Time of day */
  7553.            _CMFLD,                      /* Field */
  7554.            "",                          /* hlpmsg */
  7555.            "",                          /* default */
  7556.            "",                          /* addtl string data */
  7557.            0,                           /* addtl numeric data 1 */
  7558.            0,                           /* addtl numeric data 2 */
  7559.            xxstring,                    /* processing func */
  7560.            NULL,                        /* N/A */
  7561.            NULL                         /* No next */
  7562.            );
  7563.     x = cmfdb(&nu);                     /* Parse a number or a field */
  7564.     if (x < 0) {
  7565.         if (x == -3)
  7566.           x = -2;
  7567.         return(x);
  7568.     }
  7569.     switch (cmresult.fcode) {
  7570.       case _CMNUM:                      /* Number */
  7571.         x = cmresult.nresult;
  7572.         break;
  7573.       case _CMFLD:                      /* Field */
  7574.         zz = tod2sec(cmresult.sresult); /* Convert to secs since midnight */
  7575.         if (zz < 0L) {
  7576.             printf("?Number, expression, or time of day required\n");
  7577.             return(-9);
  7578.         } else {
  7579.             char now[32];               /* Current time */
  7580.             char *p;
  7581.             long tnow;
  7582.             p = now;
  7583.             ztime(&p);
  7584.             tnow = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  7585.             if (zz < tnow)              /* User's time before now */
  7586.               zz += 86400L;             /* So make it tomorrow */
  7587.             zz -= tnow;         /* Seconds from now. */
  7588.         }
  7589.     }
  7590.     debug(F101,"PAUSE/WAIT/MSLEEP zz","",zz);
  7591.     switch (cx) {
  7592.       case XXPAU:                       /* PAUSE */
  7593.       case XXMSL:                       /* MSLEEP */
  7594.         if ((y = cmcfm()) < 0) return(y);
  7595.         break;
  7596.       case XXWAI:                       /* WAIT */
  7597.         z = 0;                          /* Modem signal mask */
  7598.         y = cmkey(waittab,nwaittab,"","",xxstring);
  7599.         if (y < 0) {
  7600.             if (y == -3) {
  7601.                 if ((y = cmcfm()) < 0)
  7602.                   return(y);
  7603.                 break;
  7604.             } else
  7605.               return(y);
  7606.         }
  7607.         if (y == WAIT_FIL) {            /* FILE */
  7608.             int wild = 0;
  7609.             if ((z = cmkey(wfswi,nwfswi,"event","",xxstring)) < 0)
  7610.               return(z);
  7611.             filewait = z;
  7612.             if (filewait == WF_MOD || filewait == WF_DEL)
  7613.               z = cmifi("Filename","",&s,&wild,xxstring);
  7614.             else
  7615.               z = cmfld("Filename","",&s,xxstring);
  7616.             if (z < 0)
  7617.               return(z);
  7618.             if (wild || ((filewait == WF_CRE) && iswild(s))) {
  7619.                 printf("?Wildcards not valid here\n");
  7620.                 return(-9);
  7621.             }
  7622.             ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7623.             if ((z = cmcfm()) < 0)
  7624.               return(z);
  7625.             break;
  7626.         } else if (y != WAIT_MDM) {     /* A modem signal */
  7627.             z |= y;                     /* OR the bit into the signal mask */
  7628.         }
  7629.         if (!filewait) {                /* Modem signals... */
  7630.             while (1) {                 /* Get zero or more signal names */
  7631.                 y = cmkey(mstab,nms,"modem signal","",xxstring);
  7632.                 if (y == -3) break;     /* -3 means they typed CR */
  7633.                 if (y < 0) return(y);   /* Other negatives are errors */
  7634.                 z |= y;                 /* OR the bit into the signal mask */
  7635.             }
  7636.             if ((y = cmcfm()) < 0) return(y);
  7637.             break;
  7638.         }
  7639.  
  7640.       default:                          /* Shouldn't happen */
  7641.         return(-2);
  7642.     } /* switch (cx) */
  7643.  
  7644. /* Command is entered, now do it. */
  7645.  
  7646.     if (zz > -1L) {                     /* Time of day given? */
  7647.         x = zz;
  7648.         if (zz != (long) x) {
  7649.             printf(
  7650. "Sorry, arithmetic overflow - hh:mm:ss not usable on this platform.\n"
  7651.                    );
  7652.             return(-9);
  7653.         }
  7654.     }
  7655.     if (sleepcan)
  7656.       concb((char)escape);              /* Ensure single-char wakeup */
  7657.  
  7658.     if (cx == XXMSL) {                  /* Millisecond sleep */
  7659.         msleep(zz < 0 ? x : x * 1000);
  7660.         return(success = 1);
  7661.     }
  7662.     if (cx == XXPAU && !sleepcan) {     /* SLEEP CANCELLATION is OFF */
  7663.         sleep(x);
  7664.         return(success = 1);
  7665.     }
  7666.     if (filewait) {                     /* FILE... */
  7667.         fs = zchki(tmpbuf);             /* Check if file exists */
  7668.         switch (filewait) {
  7669.           case WF_DEL:
  7670.             if (fs == -1)
  7671.               return(success = 1);
  7672.             break;
  7673.           case WF_MOD:
  7674.             if (fs == -1) {
  7675.                 printf("?File does not exit: %s\n",tmpbuf);
  7676.                 return(-9);
  7677.             }
  7678.             s = zfcdat(tmpbuf);         /* Get current modification date */
  7679.             if (!s) s = "";
  7680.             if (ckstrncpy(filedate,s,32) != 17) {
  7681.                 printf("?Can't get modification time: %s\n",tmpbuf);
  7682.                 return(-9);
  7683.             }
  7684.             break;
  7685.           case WF_CRE:
  7686.             if (fs > -1)
  7687.               return(success = 1);
  7688.             break;
  7689.         }
  7690.     }
  7691.     do {                                /* Polling loop */
  7692.         if (sleepcan) {                 /* Keyboard cancellation allowed? */
  7693.             if ((y = conchk()) > 0) {   /* Did they type something? */
  7694.                 kbchar = coninc(0);     /* Yes, get first char they typed */
  7695.                 debug(F000,"WAIT kbchar","",kbchar);
  7696. #ifdef COMMENT
  7697.                 while (--y > 0)         /* Gobble the rest up */
  7698.                   coninc(0);
  7699. #endif /* COMMENT */
  7700.                 return(success = 0);    /* And quit PAUSing or WAITing */
  7701.             }
  7702.         }
  7703.         if (filewait == 0) {
  7704.             if (cx == XXWAI) {          /* WAIT for modem signals */
  7705.                 if (z != 0) {
  7706.                     mdmsig = ttgmdm();  /* Get them. */
  7707.                     debug(F101,"WAIT ttgmdm","",mdmsig);
  7708.                     if (mdmsig < 0)     /* Failure to get them? */
  7709.                       return(success = 0); /* Fail. */
  7710.                     if ((mdmsig & z) == z) /* Got desired ones? */
  7711.                       return(success = 1); /* Succeed. */
  7712.                 } else if (x == 0)
  7713.                   return(success = 0);
  7714.             }
  7715.         } else {                        /* FILE... */
  7716.             fs = zchki(tmpbuf);         /* Get file status */
  7717.             if (filewait == WF_MOD) {   /* Wait for modification */
  7718.                 if (fs == -1)           /* Failure to get status */
  7719.                   return(success = 0);  /* so WAIT fails. */
  7720.                 s = zfcdat(tmpbuf);     /* Get current modification time */
  7721.                 if (!s) s = "";         /* And compare with the time */
  7722.                 if (strcmp(s,filedate)) /* when the WAIT started */
  7723.                   return(success = 1);
  7724.             } else if (filewait == WF_DEL) { /* Wait for deletion */
  7725.                 if (fs == -1)           /* If file doesn't exist, */
  7726.                   return(success = 1);  /* succeed. */
  7727.             } else if (filewait == WF_CRE) { /* Wait for creation */
  7728.                 if (fs != -1)           /* If file exists */
  7729.                   return(success = 1);  /* succeed. */
  7730.             }
  7731.         }
  7732.         if (x < 1)                      /* SLEEP/WAIT/PAUSE 0 */
  7733.           break;
  7734.         sleep(waitinterval);            /* No interrupt, sleep */
  7735.         x -= waitinterval;              /* Deduct sleep time */
  7736.     } while (x > 0);
  7737.  
  7738.     if (cx == XXWAI)                    /* WAIT time expired */
  7739.       success = (z == 0);               /* Succeed if no modem signals */
  7740.     else                                /* For SLEEP or PAUSE, success */
  7741.       success = (x == 0);               /* depends on whether it was */
  7742.     return(success);                    /* interrupted from the keyboard. */
  7743. #endif /* OLDWAIT */
  7744. }
  7745. #endif /* NOSPL */
  7746.  
  7747. #ifdef OS2ORUNIX
  7748. _PROTOTYP(int zcmpfn,(char *, char *));
  7749. #endif /* OS2ORUNIX */
  7750.  
  7751. #ifndef NOFRILLS
  7752. #ifdef NT
  7753. int 
  7754. dolink() {
  7755.     /* Parse a file or a directory name */
  7756.     int i, x, z, listing = 0, havename = 0, wild = 0, rc = 1;
  7757.     struct FDB sw, fi;
  7758.  
  7759.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7760.            _CMKEY,                      /* fcode */
  7761.            "Filename or switch",        /* hlpmsg */
  7762.            "",                          /* default */
  7763.            "",                          /* addtl string data */
  7764.            nqvswtab,                    /* addtl numeric data 1: tbl size */
  7765.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7766.            xxstring,                    /* Processing function */
  7767.            qvswtab,                     /* Keyword table */
  7768.            &fi                          /* Pointer to next FDB */
  7769.            );
  7770.  
  7771.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7772.            _CMIFI,                      /* fcode */
  7773.            "",                          /* hlpmsg */
  7774.            "",                          /* default */
  7775.            "",                          /* addtl string data */
  7776.            3,                           /* addtl numeric data 1 */
  7777.            0,                           /* addtl numeric data 2 */
  7778.            xxstring,
  7779.            NULL,
  7780.            NULL
  7781.            );
  7782.  
  7783.     while (!havename) {
  7784.         x = cmfdb(&sw);                 /* Parse something */
  7785.         if (x < 0)                      /* Error */
  7786.           return(x);
  7787.         switch (cmresult.fcode) {
  7788.           case _CMKEY:
  7789.             switch (cmresult.nresult) {
  7790.               case DEL_LIS:
  7791.               case DEL_VRB:
  7792.                 listing = 1;
  7793.                 break;
  7794.               case DEL_NOL:
  7795.               case DEL_QUI:
  7796.                 listing = 0;
  7797.                 break;
  7798.             }
  7799.             break;
  7800.           case _CMIFI:
  7801.             s = cmresult.sresult;
  7802.             havename = 1;
  7803.             break;
  7804.           default:
  7805.             return(-2);
  7806.         }
  7807.     }
  7808.     wild = cmresult.nresult;            /* Source specification wild? */
  7809.  
  7810.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7811.     s = line;
  7812.  
  7813.     if (!wild)
  7814.       wild = iswild(line);
  7815.  
  7816.     p = tmpbuf;                         /* Place for new name */
  7817.     if ((x = cmofi(wild ? "Target directory" : "New name",
  7818.                    "",&s,xxstring)) < 0) { /* Get new name */
  7819.         if (x == -3) {
  7820.             printf("?%s required\n", wild ? "Target directory" : "New name");
  7821.             return(-9);
  7822.         } else return(x);
  7823.     }
  7824.     ckstrncpy(p,s,TMPBUFSIZ);           /* Make a safe copy of the new name */
  7825.     if ((y = cmcfm()) < 0) return(y);
  7826.  
  7827.     if (!wild) {                        /* Just one */
  7828.         if (listing) printf("%s => %s ",line,p);
  7829.         if (zlink(line,p) < 0) {
  7830.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7831.             rc = 0;
  7832.         } else {
  7833.             if (listing) printf("(OK)\n");
  7834.         }
  7835.         return(success = rc);
  7836.     }
  7837.     if (!isdir(p)) {                    /* Multiple */
  7838.         printf(                         /* if target is not a directory */
  7839. "?Multiple source files not allowed if target is not a directory.\n");
  7840.         return(-9);
  7841.     }
  7842. #ifdef COMMENT
  7843.     else {                              /* Show full path of target */
  7844.         char buf[CKMAXPATH];            /* (too much) */
  7845.         if (zfnqfp(p,CKMAXPATH,buf))
  7846.           ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  7847.     }
  7848. #endif /* COMMENT */
  7849.  
  7850. #ifdef VMS
  7851.     conres();                           /* Let Ctrl-C work. */
  7852. #endif /* VMS */
  7853.     debug(F110,"dolink line",line,0);
  7854.  
  7855. #ifdef ZXREWIND
  7856.     z = zxrewind();                     /* Rewind file list */
  7857. #else
  7858.     z = nzxpand(s,0);                   /* Expand file list */
  7859. #endif /* ZXREWIND */
  7860.     debug(F111,"dolink p",p,z);
  7861.  
  7862. #ifdef UNIX
  7863.     if (wild && z > 1)
  7864.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  7865. #endif /* UNIX */
  7866.  
  7867.     while (z-- > 0) {
  7868.         if (!(z == 0 && !wild))
  7869.           znext(line);
  7870.         if (!line[0])
  7871.           break;
  7872.         if (listing) printf("%s => %s ",line,p);
  7873.         if (zlink(line,p) < 0) {
  7874.             if (listing) printf("(FAILED: %s\n",ck_errstr());
  7875.             rc = 0;
  7876.         } else {
  7877.             if (listing) printf("(OK)\n");
  7878.         }
  7879.     }
  7880. #ifdef VMS
  7881.     concb((char)escape);
  7882. #endif /* VMS */
  7883.     return(success = rc);
  7884. }
  7885. #endif /* NT */
  7886.  
  7887. #ifdef ZCOPY
  7888. int
  7889. docopy() {
  7890.     int i, x, listing = 0, nolist = 0, havename = 0, getval;
  7891.     char c;
  7892.     struct FDB sw, fi;
  7893.     int overwrite = OVW_ALWAYS;
  7894.     int targetisdir = 0;
  7895.     int targetlen = 0;
  7896.     int appending = 0;
  7897.     int preserve = 0;
  7898.     int swapping = 0;
  7899.     int fromb64 = 0;
  7900.     int tob64 = 0;
  7901.     int wild = 0;
  7902.     int rc = 1;
  7903.  
  7904.     char newname[CKMAXPATH], * nm;
  7905.     nm = newname;
  7906.  
  7907.     cmfdbi(&sw,                         /* 2nd FDB - optional /PAGE switch */
  7908.            _CMKEY,                      /* fcode */
  7909.            "Filename or switch",        /* hlpmsg */
  7910.            "",                          /* default */
  7911.            "",                          /* addtl string data */
  7912.            ncopytab,                    /* addtl numeric data 1: tbl size */
  7913.            4,                           /* addtl numeric data 2: 4 = cmswi */
  7914.            xxstring,                    /* Processing function */
  7915.            copytab,                     /* Keyword table */
  7916.            &fi                          /* Pointer to next FDB */
  7917.            );
  7918.     cmfdbi(&fi,                         /* 1st FDB - file to type */
  7919.            _CMIFI,                      /* fcode */
  7920.            "",                          /* hlpmsg */
  7921.            "",                          /* default */
  7922.            "",                          /* addtl string data */
  7923.            0,                           /* addtl numeric data 1 */
  7924.            0,                           /* addtl numeric data 2 */
  7925.            xxstring,
  7926.            NULL,
  7927.            NULL
  7928.            );
  7929.  
  7930.     while (!havename) {
  7931.         x = cmfdb(&sw);                 /* Parse something */
  7932.         if (x < 0)                      /* Error */
  7933.           return(x);
  7934.         switch (cmresult.fcode) {
  7935.           case _CMKEY:
  7936.         c = cmgbrk();                   /* Get break character */
  7937.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  7938.         printf("?This switch does not take an argument\n");
  7939.         rc = -9;
  7940.         return(rc);
  7941.         }
  7942.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  7943.         printf("?This switch requires an argument\n");
  7944.         rc = -9;
  7945.         return(rc);
  7946.         }
  7947.             switch (cmresult.nresult) {
  7948.               case DEL_LIS:
  7949.               case DEL_VRB:
  7950.         nolist = 0;
  7951.                 listing = 1;
  7952.                 break;
  7953.               case DEL_NOL:
  7954.               case DEL_QUI:
  7955.         nolist = 1;
  7956.                 listing = 0;
  7957.                 break;
  7958.               case 999:
  7959.                 swapping = 1;
  7960.                 break;
  7961.               case 998:
  7962.                 appending = 1;
  7963.                 break;
  7964.           case 995:
  7965.         preserve = 1;
  7966.                 break;
  7967.           case 994:
  7968.         if ((x = cmkey(ovwtab,novwtab,
  7969.                    "When to overwrite existing destination file",
  7970.                    "",xxstring)) < 0)
  7971.           return(x);
  7972.         overwrite = x;
  7973.                 break;
  7974. #ifndef NOSPL
  7975.               case 997:
  7976.                 fromb64 = 1;
  7977.                 break;
  7978.               case 996:
  7979.                 tob64 = 1;
  7980.                 break;
  7981. #endif /* NOSPL */
  7982.             }
  7983.             break;
  7984.           case _CMIFI:
  7985.             s = cmresult.sresult;
  7986.             havename = 1;
  7987.             break;
  7988.           default:
  7989.             return(-2);
  7990.         }
  7991.     }
  7992.     wild = cmresult.nresult;
  7993.     ckstrncpy(line,s,LINBUFSIZ);        /* Make a safe copy of source name */
  7994.     s = line;
  7995.     p = tmpbuf;                         /* Place for new name */
  7996.  
  7997.     /* Get destination name */
  7998.     if ((x = cmofi("destination name and/or directory",
  7999. #ifdef UNIX
  8000.                    "."
  8001. #else
  8002.                    ""
  8003. #endif /* UNIX */
  8004.                    ,&s,xxstring)) < 0) {
  8005.         if (x == -3) {
  8006.             printf("?Name for destination file required\n");
  8007.             return(-9);
  8008.         } else return(x);
  8009.     }
  8010.     ckstrncpy(p,s,TMPBUFSIZ);           /* Safe copy of destination name */
  8011.     if ((y = cmcfm()) < 0) return(y);
  8012.     if (appending && swapping) {
  8013.         printf("?Sorry, /APPEND and /SWAP conflict\n");
  8014.         return(-9);
  8015.     }
  8016. #ifdef COMMENT
  8017. /*
  8018.   This unreasonably prevented "COPY /APPEND *.* bigfile" from concatenating
  8019.   a bunch of files into one big file.
  8020. */
  8021.     if (appending && wild) {
  8022.         printf("?Sorry, /APPEND can be used only with single files\n");
  8023.         return(-9);
  8024.     }
  8025. #endif /* COMMENT */
  8026.     targetisdir = isdir(p);
  8027.     x = strlen(p);
  8028.     if (targetisdir) {
  8029. #ifdef UNIXOROSK
  8030.         if (p[x-1] != '/') {
  8031.             ckstrncat(p,"/",TMPBUFSIZ);
  8032.             x++;
  8033.         }
  8034. #else
  8035. #ifdef OS2
  8036.         if (p[x-1] != '/') {
  8037.             ckstrncat(p,"/",TMPBUFSIZ);
  8038.             x++;
  8039.         }
  8040. #else
  8041. #ifdef STRATUS
  8042.         if (p[x-1] != '>') {
  8043.             ckstrncat(p,">",TMPBUFSIZ);
  8044.             x++;
  8045.         }
  8046. #else
  8047. #ifdef datageneral
  8048.         if (p[x-1] != ':') {
  8049.             ckstrncat(p,":",TMPBUFSIZ);
  8050.             x++;
  8051.         }
  8052. #else
  8053.         if (p[x-1] != '/') {
  8054.             ckstrncat(p,"/",TMPBUFSIZ);
  8055.             x++;
  8056.         }
  8057. #endif /* datageneral */
  8058. #endif /* STRATUS */
  8059. #endif /* OS2 */
  8060. #endif /* UNIXOROSK */
  8061.     }
  8062.     targetlen = x;
  8063.  
  8064.     if (!appending) {            /* If /APPEND not given */
  8065.     if (wild && !targetisdir) {    /* No wildcards allowed */
  8066.         printf(            /* if target is not a directory */
  8067. "?Multiple source files not allowed if target is not a directory.\n");
  8068.         return(-9);
  8069.     }
  8070.     }
  8071.  
  8072. #ifdef VMS
  8073.     conres();                           /* Let Ctrl-C work. */
  8074. #endif /* VMS */
  8075.     debug(F110,"docopy line",line,0);
  8076.     debug(F110,"docopy p",p,0);
  8077.     debug(F110,"docopy nm",nm,0);
  8078.  
  8079. #ifdef ZXREWIND
  8080.     z = zxrewind();                     /* Rewind file list */
  8081. #else
  8082.     z = nzxpand(s,0);                   /* Expand file list */
  8083. #endif /* ZXREWIND */
  8084.  
  8085. #ifdef UNIX
  8086.     if (wild)
  8087.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  8088. #endif /* UNIX */
  8089.  
  8090. #ifdef IKSD
  8091.     if (!targetisdir && zchki(p) > -1) { /* Destination file exists? */
  8092.         if (inserver && (!ENABLED(en_del)
  8093. #ifdef CK_LOGIN
  8094.                          || isguest
  8095. #endif /* CK_LOGIN */
  8096.                          )) {
  8097.             printf("?Sorry, overwriting existing files is disabled\n");
  8098.             return(-9);
  8099.         }
  8100.     }
  8101. #endif /* IKSD */
  8102.  
  8103.     if (tob64 && fromb64) {             /* To and from B64 = no conversion */
  8104.         tob64 = 0;
  8105.         fromb64 = 0;
  8106.     }
  8107.     debug(F110,"COPY dest",p,0);
  8108.  
  8109.     while (z > 0) {
  8110.  
  8111.         znext(line);
  8112.         if (!line[0])
  8113.           break;
  8114.  
  8115.         errno = 0;                      /* Reset errno */
  8116.  
  8117.     if (targetisdir) {
  8118.         zstrip(line,&nm);
  8119.         ckmakmsg(newname,CKMAXPATH,p,nm,NULL,NULL);
  8120.         nm = newname;
  8121.     } else {
  8122.         nm = p;
  8123.     }
  8124.     if (overwrite) {        /* Overwrite checking? */
  8125.         if (zchki(nm) >= (CK_OFF_T)0) { /* Destination file exists? */
  8126.  
  8127.         char d1[20], * d2;
  8128.         char * n1, * n2;
  8129.         int i, skip = 0;
  8130.  
  8131.         i = strlen(line);    /* Isolate source filename */
  8132.         for (; i >= 0; i--) {
  8133.             if (ISDIRSEP(line[i])) {
  8134.             n1 = &line[i+1];
  8135.             break;
  8136.             }
  8137.         }
  8138.         debug(F110,"COPY n1", n1, 0);
  8139.         i = strlen(nm);        /* And destination filename */
  8140.         for (; i >= 0; i--) {
  8141.             if (ISDIRSEP(nm[i])) {
  8142.             n2 = &nm[i+1];
  8143.             break;
  8144.             }
  8145.         }
  8146.         debug(F110,"COPY n2", n2, 0);
  8147.         if (!strcmp(n1,n2)) {                /* Same name? */
  8148.             if (overwrite == OVW_NEVER) { /* Never overwrite? */
  8149.             if (listing)              /* Skip */
  8150.               if (listing) printf("%s => %s (SKIPPED)\n",line,nm);
  8151.             continue;
  8152.             }
  8153.             ckstrncpy(d1,zfcdat(line),20); /* Source file timestamp */
  8154.             d2 = zfcdat(nm);           /* Timestamp of dest file */
  8155.             x = strcmp(d1,d2);           /* Compare them */
  8156.             if (((overwrite == OVW_NEWER) && (x < 0)) ||
  8157.             ((overwrite == OVW_OLDER) && (x > 0))) {
  8158.             if (listing)
  8159.               if (listing) printf("%s => %s (SKIPPED)\n",line,nm);
  8160.             continue;
  8161.             }
  8162.         }
  8163.         }
  8164.     }
  8165.         if (listing) printf("%s => %s ",line,nm);
  8166.  
  8167.         /* Straight copy */
  8168.         if (!swapping && !appending && !fromb64 && !tob64) {
  8169.             debug(F110,"COPY zcopy",line,0);
  8170.  
  8171.             if ((x = zcopy(line,p)) < 0) { /* Let zcopy() do it. */
  8172.         debug(F111,"COPY not OK",line,x);
  8173.                 switch (x) {
  8174.                   case -2:
  8175.             if (listing)
  8176.               printf("(FAILED: Not a regular file)\n");
  8177.             else if (!nolist)
  8178.               printf("?Not a regular file - %s\n",line);
  8179.                     rc = 0;
  8180.                     break;
  8181.                   case -3:
  8182.             if (listing)
  8183.               printf("(FAILED: Not found or not accessible)\n");
  8184.             else if (!nolist)
  8185.               printf("?Not found or not accessible - %s\n",line);
  8186.                     rc = 0;
  8187.                     break;
  8188.                   case -4:
  8189.             if (listing)
  8190.               printf("(FAILED: Permission denied)\n");
  8191.             else if (!nolist)
  8192.               printf("?Permission denied - %s\n",line);
  8193.                     rc = 0;
  8194.                     break;
  8195.                   case -5:
  8196.             if (listing)
  8197.               printf("(Source and destination are the same file)\n");
  8198.             else if (!nolist)
  8199.               printf(
  8200.               "?Source and destination are the same file - %s\n",
  8201.               line
  8202.               );
  8203.                     break;
  8204.                   case -6:
  8205.             if (listing)
  8206.               printf("(FAILED: Input/Output error)\n");
  8207.             else if (!nolist)
  8208.               printf("?Input/Output error - %s\n",line);
  8209.                     rc = 0;
  8210.                     break;
  8211.                   case -7:
  8212.             if (listing)
  8213.               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  8214.             else if (!nolist)
  8215.               printf("?%s - %s\n",ck_errstr(),p);
  8216.                     rc = 0;
  8217.                     break;
  8218.                   default:
  8219.             if (listing)
  8220.               printf("(FAILED: %s)\n",ck_errstr());
  8221.             else if (!nolist)
  8222.               printf("?%s\n",ck_errstr());
  8223.                     rc = 0;
  8224.                 }
  8225.             } else {            /* Regular copy succeeded */
  8226.         debug(F110,"COPY OK..",newname,0);
  8227. #ifndef NOXFER
  8228.         if (preserve) {        /* Handle /PRESERVE */
  8229.             char * pstr = "";    /* File permissions string */
  8230.             struct zattr xx;    /* File attribute structure */
  8231.             extern char * cksysid;
  8232.  
  8233.             initattr(&xx);    /* Initialize the struct */
  8234.  
  8235.             xx.systemid.val = cksysid; /* Set our system ID */
  8236.             xx.systemid.len = (int)strlen(cksysid);
  8237. #ifdef CK_PERMS
  8238.             pstr = zgperm(line); /* Get source file's permissions */
  8239. #endif /* CK_PERMS */
  8240.             xx.lprotect.val = pstr;
  8241.             xx.lprotect.len = (int)strlen(pstr);
  8242.             xx.date.val = zfcdat(line);    /* Source file's timestamp */
  8243.             xx.date.len = (int)strlen(xx.date.val);
  8244.             if (zstime(nm,&xx,0) < 0) {
  8245.             printf("?COPY /PRESERVE %s: %s\n",nm,ck_errstr());
  8246.             rc = -9;
  8247.             }
  8248.         }
  8249. #endif    /* NOXFER */
  8250.         if (listing && rc > -1)
  8251.           printf("(OK)\n");
  8252.             }
  8253.  
  8254.         } else {                        /* Special options */
  8255.  
  8256.             int prev, y, x = 0;         /* Variables needed for them */
  8257.             int i, t;
  8258.             char ibuf[100];
  8259.             char obuf[200];
  8260.             FILE * in = NULL;
  8261.             FILE * out = NULL;
  8262.  
  8263.             if ((in = fopen(line,"r")) == NULL) { /* Open input file */
  8264.                 if (listing)
  8265.           printf("(FAILED: %s)\n",ck_errstr());
  8266.         else if (!nolist)
  8267.           printf("?%s - %s)\n",ck_errstr(),line);
  8268.                 rc = 0;
  8269.                 continue;
  8270.             }
  8271.             if (targetisdir) {          /* Target is directory */
  8272.                 char * buf = NULL;      /* so append this filename to it */
  8273.                 zstrip(line,&buf);
  8274.                 p[targetlen] = NUL;
  8275.                 if (buf)
  8276.                   ckstrncat(p,buf,TMPBUFSIZ);
  8277.             }
  8278. #ifdef OS2ORUNIX
  8279.             if (zcmpfn(line,p)) {       /* Input and output are same file? */
  8280.                 if (listing)
  8281.                   printf("(FAILED: Source and destination identical)\n");
  8282.         else if (!nolist)
  8283.                   printf("?Source and destination identical - %s\n", line); 
  8284.                 rc = 0;
  8285.                 continue;
  8286.             }
  8287. #endif /* OS2ORUNIX */
  8288.             if ((out = fopen(p, (appending ? "a" : "w"))) == NULL) {
  8289.                 fclose(in);
  8290.                 if (listing)
  8291.           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  8292.         else if (!nolist)
  8293.           printf("?%s - %s\n",p,ck_errstr());
  8294.                 rc = 0;
  8295.                 continue;
  8296.             }
  8297. #ifndef NOSPL
  8298.             if (tob64) {                /* Converting to Base-64 */
  8299.  
  8300.                 debug(F110,"COPY tob64",line,0);
  8301.  
  8302.                 while (1) {             /* Loop... */
  8303.                     prev = x;
  8304.                     if ((x = fread(ibuf,1,54,in)) < 1) { /* EOF */
  8305.                         if (listing)
  8306.                           printf("(OK)\n");
  8307.                         break;
  8308.                     }
  8309.                     if (prev % 3) {
  8310.                         if (listing)
  8311.                           printf("(FAILED: Phase error at %d)\n",prev);
  8312.             else if (!nolist)
  8313.                           printf("?Phase error at %d\n",prev);
  8314.                         rc = 0;
  8315.                         break;
  8316.                     }
  8317.                     if (swapping) {
  8318.                         if (x & 1) {
  8319.                             if (listing)
  8320.                               printf("(FAILED: Swap error)\n");
  8321.                 else if (!nolist)
  8322.                   printf("?Swap error\n");
  8323.                             rc = 0;
  8324.                             break;
  8325.                         }
  8326.                         for (i = 0; i < x; i+=2) {
  8327.                             t = ibuf[i];
  8328.                             ibuf[i] = ibuf[i+1];
  8329.                             ibuf[i+1] = t;
  8330.                         }
  8331.                     }
  8332.                     if ((y = b8tob64(ibuf,x,obuf,180)) < 0) {
  8333.                         if (listing)
  8334.                           printf("(FAILED: Encoding error)\n");
  8335.             else if (!nolist)
  8336.               printf("?Encoding error\n");
  8337.                         rc = 0;
  8338.                         break;
  8339.                     }
  8340.                     fprintf(out,"%s\n",obuf);
  8341.                 }
  8342.  
  8343.             } else if (fromb64) {       /* Converting from Base 64 */
  8344.  
  8345.                 debug(F110,"COPY fromb64",line,0);
  8346.  
  8347.                 if ((out = fopen(p,appending ? "a" : "w")) == NULL) {
  8348.                     fclose(in);
  8349.                     if (listing)
  8350.               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  8351.             else if (!nolist)
  8352.               printf("?%s - %s\n",p,ck_errstr());
  8353.                     rc = 0;
  8354.                     continue;
  8355.                 }
  8356.                 x = 1;
  8357.                 while (x) {
  8358.                     x = fread(ibuf,1,80,in);
  8359.                     if ((y = b64tob8(ibuf,x,obuf,80)) < 0) {
  8360.                         if (listing)
  8361.                           printf("(FAILED: Decoding error)\n");
  8362.             else if (!nolist)
  8363.               printf("?Decoding error\n");
  8364.                         rc = 0;
  8365.                         break;
  8366.                     }
  8367.                     if (swapping) {
  8368.                         if (x & 1) {
  8369.                             if (listing)
  8370.                               printf("(FAILED: Swap error)\n");
  8371.                 else if (!nolist)
  8372.                   printf("?Swap error\n");
  8373.                             rc = 0;
  8374.                             break;
  8375.                         }
  8376.                         for (i = 0; i < y; i+=2) {
  8377.                             t = obuf[i];
  8378.                             obuf[i] = obuf[i+1];
  8379.                             obuf[i+1] = t;
  8380.                         }
  8381.                     }
  8382.                     if (y > 0) {
  8383.                         if (fwrite(obuf,1,y,out) < 1) {
  8384.                             if (listing)
  8385.                               printf("(FAILED: %s - %s)\n",p,ck_errstr());
  8386.                 else if (!nolist)
  8387.                   printf("?%s - %s\n",p,ck_errstr());
  8388.                             rc = 0;
  8389.                             break;
  8390.                         }
  8391.                     }
  8392.                 }
  8393.  
  8394.             } else
  8395. #endif /* NOSPL */
  8396.  
  8397.             if (swapping) {             /* Swapping bytes */
  8398.  
  8399.                 CHAR c[3];
  8400.                 c[2] = NUL;
  8401.  
  8402.                 debug(F110,"COPY swapping",line,0);
  8403.  
  8404.                 while (1) {
  8405.                     x = fread((char *)c,1,2,in);
  8406.                     if (x < 1) {
  8407.                         if (listing)
  8408.                           printf("(OK)\n");
  8409.                         break;
  8410.                     } else if (x == 1) {
  8411.                         c[1] = c[0];
  8412.                         c[0] = NUL;
  8413.                         printf(
  8414.                             "(WARNING: Odd byte count)");
  8415.                         if (!listing) printf("\n");
  8416.                     }
  8417.                     if (fprintf(out,"%c%c",c[1],c[0]) == EOF) {
  8418.                         if (listing)
  8419.                           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  8420.             else if (!nolist)
  8421.                           printf("?%s - %s\n",p,ck_errstr());
  8422.                         rc = 0;
  8423.                         break;
  8424.                     }
  8425.                 }
  8426.  
  8427.             } else if (appending) {     /* Appending to target file */
  8428.  
  8429.                 char c;
  8430.  
  8431.                 debug(F110,"COPY appending",line,0);
  8432.  
  8433.                 while (1) {
  8434.                     x = fread(&c,1,1,in);
  8435.                     if (x < 1) {
  8436.                         if (listing)
  8437.                           printf("(OK)\n");
  8438.                         break;
  8439.                     }
  8440.                     if (fwrite(&c,1,1,out) < 1) {
  8441.                         if (listing)
  8442.                           printf("(FAILED: %s - %s)\n",p,ck_errstr());
  8443.             else if (!nolist)
  8444.                           printf("?%s - %s\n",p,ck_errstr());
  8445.                         rc = 0;
  8446.                         break;
  8447.                     }
  8448.                 }
  8449.             }
  8450.             if (out) fclose(out);
  8451.             if (in) fclose(in);
  8452.         }
  8453. #ifdef VMSORUNIX
  8454.         concb((char)escape);
  8455. #endif /* VMSORUNIX */
  8456.     }
  8457.     if (rc > -1) success = rc;
  8458.     return(rc);
  8459. }
  8460. #endif /* ZCOPY */
  8461. #endif /* NOFRILLS */
  8462.  
  8463. #ifndef NORENAME
  8464. #ifndef NOFRILLS
  8465. #ifdef ZRENAME
  8466.  
  8467. /* The RENAME command - expanded and improved in 8.0.212 April 2006 */
  8468.  
  8469. static char * ren_sub[4] = { NULL,NULL,NULL,NULL }; /* For RENAME /REPLACE */
  8470.  
  8471. int ren_list = 0;                /* Default listing action for RENAME */
  8472. int ren_coll = RENX_OVWR;        /* Default collision action */
  8473.  
  8474. #ifndef NOCSETS
  8475. #ifndef NOUNICODE
  8476. static struct keytab * xfcstab = NULL;    /* For RENAME /CONVERT: */
  8477. static char cvtbufin[CKMAXPATH+8] = { NUL, NUL };
  8478. static char cvtbufout[CKMAXPATH+8] = { NUL, NUL };
  8479. static char * pcvtbufin = NULL;
  8480. static char * pcvtbufout = NULL;
  8481.  
  8482. static int                /* Input function xgnbyte() */
  8483. cvtfnin() {
  8484.     CHAR c;
  8485.     c = *pcvtbufin++;
  8486.     return(c ? c : -1);
  8487. }
  8488.  
  8489. _PROTOTYP(int cvtfnout,(char));        /* Output function for xpnbyte() */
  8490. int
  8491. #ifdef CK_ANSIC
  8492. cvtfnout(char c)
  8493. #else
  8494. cvtfnout(c) char c;
  8495. #endif    /* CK_ANSIC */
  8496. {
  8497.     if (pcvtbufout - cvtbufout >= CKMAXPATH)
  8498.       return(-1);
  8499.     *pcvtbufout++ = c;
  8500.     *pcvtbufout = NUL;
  8501.     return(1);
  8502. }
  8503.  
  8504. /* Convert a string from any charset to any other charset */
  8505.  
  8506. char *
  8507. cvtstring(s,csin,csout) char * s; int csin, csout; {
  8508.     int c;
  8509.     extern CK_OFF_T ffc;
  8510.  
  8511.     ckstrncpy(cvtbufin,s,CKMAXPATH);    /* Put it in a public place */
  8512.     pcvtbufin = cvtbufin;        /* with public pointers */
  8513.     pcvtbufout = cvtbufout;
  8514.     *pcvtbufout = NUL;
  8515.  
  8516.     initxlate(csin,csout);        /* Initialize the translator */
  8517.  
  8518.     while ((c = xgnbyte(FC_UCS2,csin,cvtfnin)) > -1) { /* Loop thru string */
  8519.     if (xpnbyte(c,TC_UCS2,csout,cvtfnout) < 0) {
  8520.         ffc = (CK_OFF_T)0;
  8521.         return("");
  8522.     }
  8523.     }
  8524.     /* ffc is touched by xgnbyte() but this is not file transfer */
  8525.     /* so we have to undo it */
  8526.     ffc = (CK_OFF_T)0;
  8527.     return((char *)cvtbufout);
  8528. }
  8529. #endif /* NOUNICODE */
  8530. #endif /* NOCSETS */
  8531.  
  8532. int
  8533. shorename() {
  8534.     char * s;
  8535.     switch (ren_coll) {
  8536.       case RENX_FAIL: s = "fail"; break;
  8537.       case RENX_OVWR: s = "overwrite"; break;
  8538.       case RENX_SKIP: s = "proceed"; break;
  8539.     }
  8540.     printf(" rename collision: %s\n",s);
  8541.     printf(" rename list:      %s\n",showoff(ren_list));
  8542.     return(1);
  8543. }
  8544.  
  8545. int
  8546. setrename() {                /* Parse SET RENAME options */
  8547.     int x, y;
  8548.     if ((x = cmkey(renamset,nrenamset,"","", xxstring)) < 0)
  8549.       return(x);
  8550.     switch (x) {
  8551.       case REN_OVW:            /* COLLISION */
  8552.     if ((x = cmkey(r_collision,nr_collision,"","", xxstring)) < 0)
  8553.       return(x);
  8554.     if ((y = cmcfm()) < 0)
  8555.       return(y);
  8556.     ren_coll = x;
  8557.     break;
  8558.       case DEL_LIS:            /* LIST */
  8559.     return(seton(&ren_list));
  8560.     }
  8561.     return(success = 1);
  8562. }
  8563.  
  8564. /* Reverse a string - Assumes a single-byte character set */
  8565.  
  8566. int
  8567. gnirts(s1, s2, len) char * s1, * s2; int len; {
  8568.     int n, m = 0;
  8569.     if (!s1)                /* Null source pointer, fail */
  8570.       return(0);
  8571.     n = (int) strlen(s1);
  8572.     if (n > len-1)            /* Source longer than dest, fail */
  8573.       return(0);
  8574.     s2[n--] = NUL;            /* Deposit null byte at end of dest */
  8575.     for (; n >= 0; n--) {        /* Copy the rest backwards */
  8576.     *s2++ = s1[n];
  8577.     m++;
  8578.     }
  8579.     return(m);
  8580. }
  8581.  
  8582. /*
  8583.   r e n a m e o n e
  8584.  
  8585.   Worker function to rename one file for dorenam() (below).
  8586.     old        = name of file or directory to be renamed
  8587.     new        = new name (not required for /UPPER, /LOWER, and /REPLACE)
  8588.     replacing  = 1 if doing string replacement on the name  
  8589.     casing     = 1 if converting name to lowercase, 2 if to uppercase
  8590.     all        = if doing case conversion on all names, not just monocase ones
  8591.     converting = 1 if converting character sets
  8592.     cset1      = character set to convert from (File Character Set index)
  8593.     cset2      = character set to convert to (ditto, see ck?xla.h)
  8594.     listing    = 1 to show results of rename
  8595.     nolist     = 1 to be completely silent (don't even print error messages)
  8596.     op         = 1 means simulate, 2 means check for collision, 0 means rename
  8597.     size       = length of result buffer.
  8598.     collision  = action to take if destination file already exists:
  8599.                  0 = fail
  8600.                  1 = overwrite and succeed
  8601.                  2 = skip and succeed
  8602.   Returns:
  8603.      0: on failure to rename or when a forbidden collision would have occurred.
  8604.      1: on success (file was renamed or did not need to be renamed).
  8605.   Note:
  8606.      If this code is ever built on any platform that is not Unix, Windows,
  8607.      VMS, or OS/2, this routine might need some adjustment.
  8608. */
  8609.  
  8610. /* Opcodes for op... */
  8611. #define REN_OP_SIM 1            /* Simulate */
  8612. #define REN_OP_CHK 2            /* Check for collisions */
  8613.  
  8614. static int
  8615. renameone(old,new,
  8616.       replacing,casing,all,converting,cset1,cset2,
  8617.       listing,nolist,op,size,collision)
  8618.     char * old, * new;
  8619.     int replacing,casing,all,converting,cset1,cset2,
  8620.     listing,nolist,op,size,collision;
  8621. {
  8622.     char buf[CKMAXPATH];        /* Temporary filename buffer */
  8623.     char out[CKMAXPATH];        /* Buffer for new name */
  8624.     char dir[CKMAXPATH];        /* Destination directory */
  8625.     char pat[CKMAXPATH];        /* Path segment on old filename */
  8626.  
  8627.     char * destdir;            /* Destination directory, if any */
  8628.     char * srcpath;            /* Source path, if any */
  8629.     int rc = 1, flag = 0, skip = 0;    /* Control */
  8630.     int honorcase = 0, replaced = 0;
  8631.     int anchor = 0;            /* 1 = beginning, 2 = end */
  8632.     int occur = 0;            /* Occurrence */
  8633.     int minus = 0;            /* Occurrence is negative */
  8634.     int allbut = 0;            /* Occurrence is "all but" */
  8635.     int arg2isfile = 0;            /* Arg2 ("new") is a filename */
  8636.     
  8637.     debug(F110,"RENAME ren_sub[0]",ren_sub[0],0);
  8638.     debug(F110,"RENAME ren_sub[1]",ren_sub[1],0);
  8639.     debug(F110,"RENAME ren_sub[2]",ren_sub[2],0);
  8640.  
  8641.     if (op == REN_OP_SIM && !nolist)    /* For convenience */
  8642.       listing = 1;
  8643. #ifndef NOSPL
  8644.     honorcase = inpcas[cmdlvl];        /* Inherit SET CASE value */
  8645. #else
  8646. #ifdef UNIX
  8647.     honorcase = 1;
  8648. #else
  8649.     honorcase = 0;
  8650. #endif    /* UNIX */
  8651. #endif    /* NOSPL */
  8652.  
  8653.     if (!old) old = "";            /* In case of bad args */
  8654.     if (!new) new = "";
  8655.     if (!*old) return(success = 0);
  8656.     ckstrncpy(out,new,CKMAXPATH);    /* So we don't write into */
  8657.     new = out;                /* our argument... */
  8658.     size = CKMAXPATH;
  8659.  
  8660.     pat[0] = NUL;            /* Assume no path in source file.. */
  8661.     srcpath = pat;
  8662.     {
  8663.     int n;                /* If the old name includes a path */
  8664.     n = (int)strlen(old) - 1;    /* put it in a separate place.     */
  8665.     for (; n >= 0; n--) {        /* We are renaming the file only.  */
  8666.         if (ISDIRSEP(old[n])) {
  8667.         ckstrncpy(pat,old,CKMAXPATH);
  8668.         pat[n+1] = NUL;
  8669.         old = old+n+1;
  8670.         break;
  8671.         }
  8672.     }
  8673.     }
  8674.     debug(F110,"RENAME old",old,0);
  8675.  
  8676.     dir[0] = NUL;            /* Assume no destination directory */
  8677.     destdir = dir;
  8678.     if (*new)                /* If Arg2 given */
  8679.       if (isdir(new) == 0)        /* and it's not a directory */
  8680.     arg2isfile++;            /* it's a filename */
  8681.  
  8682.     if (!casing && !replacing && !converting) {
  8683.     if (!*new)
  8684.       return(success = 0);
  8685.     if (!arg2isfile) {        /* Destination is a directory? */
  8686.         if (!isdir(old)) {        /* and source is not? */
  8687. #ifndef VMS
  8688.         int n, x = 0;        /* Concatenate them */
  8689.         if ((n = strlen(new)) > 0) /* so we can check for */
  8690.           if (ISDIRSEP(new[n-1]))  /* collisions. */
  8691.             x++;
  8692.         ckmakmsg(buf,size,new,x ? "" : "/", old, "");
  8693. #else
  8694.         ckmakmsg(buf,size,new, old, NULL, NULL);
  8695. #endif    /* VMS */
  8696.         new = buf;
  8697.         size = CKMAXPATH;
  8698.         }
  8699.     }
  8700.     } else if (*new) {            /* Directory to move file to */
  8701.     int n, x = 0;            /* after changing its name */
  8702.     if (!isdir(new))
  8703.       return(success = 0);
  8704. #ifndef VMS
  8705.     if ((n = strlen(new)) > 0)
  8706.       if (ISDIRSEP(new[n-1]))
  8707.         x++;
  8708.     ckmakmsg(dir,CKMAXPATH,new,x ? "" : "/", "", "");
  8709. #else
  8710.     ckstrncpy(dir,new,CKMAXPATH);
  8711. #endif    /* VMS */
  8712.     }
  8713.  
  8714. #ifndef NOCSETS
  8715. #ifndef NOUNICODE
  8716.     if (converting) {
  8717.     new = cvtstring(old,cset1,cset2);
  8718.     }
  8719. #endif    /* NOUNICODE */
  8720. #endif    /* NOCSETS */
  8721.  
  8722.     if (replacing) {            /* Replacing strings */
  8723.     int todo = 0;
  8724.     int len0, len1, len2;
  8725.     char c, *p, *s, *bp[3];
  8726.  
  8727.     bp[0] = old;            /* Original name */
  8728.     bp[1] = ren_sub[0];        /* String to be replaced */
  8729.     bp[2] = ren_sub[1];        /* What to replace it with */
  8730.     if (!bp[2]) bp[2] = "";
  8731.  
  8732.     len0 = (int)strlen(bp[0]);    /* length of original filename */
  8733.     len1 = (int)strlen(bp[1]);    /* length of target substring */
  8734.     len2 = (int)strlen(bp[2]);    /* Length of replacement string */
  8735.  
  8736.     if (ren_sub[2]) {        /* Optional options */
  8737.         p = ren_sub[2];
  8738.         while ((c = *p++)) {
  8739.         switch (c) {
  8740.           case '^':
  8741.             anchor = 1; occur = 0; minus = 0; allbut = 0; break;
  8742.           case '$':
  8743.             anchor = 2; occur = 0; minus = 0; allbut = 0; break;
  8744.           case 'A': honorcase = 1; minus = 0; allbut = 0; break;
  8745.           case 'a': honorcase = 0; minus = 0; allbut = 0; break;
  8746.           case '-': minus = 1; break;
  8747.           case '~': allbut = 1; break;
  8748.           default:
  8749.             if (isdigit(c)) {
  8750.             occur = c - '0';
  8751.             if (minus) occur = 0 - occur;
  8752.             anchor = 0;
  8753.             }
  8754.             minus = 0;
  8755.         }
  8756.         }
  8757.     }
  8758.     if (anchor) {            /* Anchored replacement... */
  8759.         y = len0 - len1;
  8760.         if (y > 0) {
  8761.         int x;
  8762.         switch (anchor) {
  8763.           case 1:        /* Anchored at beginning */
  8764.             if (!ckstrcmp(bp[1],bp[0],len1,honorcase)) {
  8765.             x = ckstrncpy(new,bp[2],size);
  8766.             (VOID) ckstrncpy(new+x,bp[0]+len1,size-x);
  8767.             replaced = 1;
  8768.             }
  8769.             break;
  8770.           case 2:        /* Anchored at end */
  8771.             if (!ckstrcmp(bp[1],bp[0]+y,len1,honorcase)) {
  8772.             x = ckstrncpy(new,bp[0],y+1);
  8773.             (VOID) ckstrncpy(new+y,bp[2],size-x);
  8774.             replaced = 1;
  8775.             }
  8776.             break;
  8777.         }
  8778.         }
  8779.         if (!replaced) {
  8780.         ckstrncpy(new,old,size); /* Keep old name */
  8781.         replaced = 1;
  8782.         }
  8783.     } else {            /* Replace all occurrences */
  8784.         int j, n = 0;        /* or a particular occurrence */
  8785.         char c;
  8786.         int x = 0;
  8787.         char * s0 = NULL, * s1 = NULL, * s2 = NULL;
  8788.         p = new;            /* Pointer to new name */
  8789.  
  8790.         if (occur < 0) {        /* nth occurrence from the right */
  8791.         occur = 0 - occur;
  8792.         s0 = (char *)malloc(len0+1); /* Reverse original string */
  8793.         if (s0) {
  8794.             (VOID) gnirts(bp[0],s0,len0+1);
  8795.             bp[0] = s0;
  8796.         } else return(0); 
  8797.         s1 = (char *)malloc(len1+1); /* Reverse target string */
  8798.         if (s1) {
  8799.             (VOID) gnirts(bp[1],s1,len1+1);
  8800.             bp[1] = s1;
  8801.         } else return(0); 
  8802.         s2 = (char *)malloc(len2+1); /* Reverse replacement string */
  8803.         if (s2) {
  8804.             (VOID) gnirts(bp[2],s2,len2+1);
  8805.             bp[2] = s2;
  8806.         } else return(0); 
  8807.         debug(F111,"RENAME s0",s0,len0);
  8808.         debug(F111,"RENAME s1",s1,len1);
  8809.         debug(F111,"RENAME s2",s2,len2);
  8810.         }
  8811.         s = bp[0];            /* Pointer to old name */
  8812.         p = new;            /* Pointer to new name */
  8813.         j = len0 - len1 + 1;    /* How much to scan */
  8814.         while (j-- > 0) {        /* For each character... */
  8815.         if (!ckstrcmp(bp[1],s,len1,honorcase)) { /* Match? */
  8816.             n++;        /* Occurrence counter */
  8817.             todo = (occur == 0) ||
  8818.             (!allbut && n == occur) ||
  8819.             (allbut && n != occur);
  8820.             if (!todo) {    /* Desired occurrence? */
  8821.             size -= ckstrncpy(p,bp[1],size); /* No... */
  8822.             p += len1;    /* Copy target string */
  8823.             s += len1;    /* instead of replacement string */
  8824.             continue;
  8825.             }
  8826.             if (len2) {        /* If replacement string not empty */
  8827.             size -= ckstrncpy(p,bp[2],size); /* Copy it */
  8828.             p += len2;
  8829.             }
  8830.             s += len1;        /* Advance source position */
  8831.         } else {        /* No match */
  8832.             *p++ = *s++;    /* just copy this character */
  8833.             size--;
  8834.         }
  8835.         }
  8836.         while ((*p++ = *s++));    /* Done copy the rest */
  8837.         replaced = 1;        /* Remember we changed the name */
  8838.         if (s0) {            /* Were we doing "all but"? */
  8839.         debug(F110,"RENAME new1",new,0);
  8840.         x = (int)strlen(new);    /* Unreverse the result */
  8841.         if ((p = (char *)malloc(x+2))) {
  8842.             (VOID) gnirts(new,p,x+2);
  8843.             debug(F110,"RENAME new2",new,0);
  8844.             ckstrncpy(new,p,x+2);
  8845.             free(p);
  8846.         }
  8847.         if (s0) free(s0);    /* Free the temporary strings */
  8848.         if (s1) free(s1);
  8849.         if (s2) free(s2);
  8850.         debug(F110,"RENAME new3",new,0);
  8851.         }
  8852.     }
  8853.     }
  8854.     if (casing) {            /* Changing case? */
  8855.     char c, * t;            /* See if mixed case. */
  8856.     if (!replaced)
  8857.       ckstrncpy(new,old,size);    /* Copy old name to new name */
  8858.     t = new;
  8859.     while ((c = *t++)) {
  8860.         if (islower(c)) flag |= 1; /* Have a lowercase letter */
  8861.         else if (isupper(c)) flag |= 2; /* Have an uppercase letter */
  8862.         if (flag == 3) break;    /* Have a mixed-case name */
  8863.     }
  8864.     if (all || flag < 3) {    /* Not skipping or not mixed case */
  8865.         if (casing == 1 && flag != 1) /* Change case to lower */
  8866.           (VOID) cklower(new);
  8867.         else if (casing == 2 && flag != 2) /* Change case to upper */
  8868.           (VOID) ckupper(new);
  8869.     }
  8870.     }
  8871.     debug(F110,"XXX 1 new",new,0);
  8872.     debug(F110,"XXX 1 old",old,0);
  8873.     debug(F110,"XXX 1 srcpath",srcpath,0);
  8874.     debug(F110,"XXX 1 destdir",destdir,0);
  8875.  
  8876.     if (*destdir || *srcpath) {        /* Were there any pathnames? */
  8877.     char tmp[CKMAXPATH];
  8878.     ckmakmsg(tmp,CKMAXPATH,srcpath,old,NULL,NULL);
  8879.     ckstrncpy(old,tmp,CKMAXPATH);
  8880.     if (*destdir) {            /* Directory-to-move-to given? */
  8881.         ckstrncat(destdir,new,CKMAXPATH);
  8882.         new = destdir;
  8883.     } else if (*srcpath && !arg2isfile) { /* Or was there a source path? */
  8884.         ckstrncat(srcpath,new,CKMAXPATH);
  8885.         new = srcpath;
  8886.     }
  8887.     }
  8888.     debug(F110,"XXX 2",new,0);
  8889.  
  8890.     skip = 0;                /* Can we skip this one? */
  8891. #ifdef COMMENT
  8892.     if (casing && !replaced) {
  8893.     skip = (((all == 0) && (flag == 3)) || (flag == casing)) ? 1 : 0;
  8894.     if (!skip && destdir) skip = 0;
  8895.     }
  8896. #endif    /* COMMENT */
  8897.     if (!skip) {
  8898.     if (!ckstrcmp(old,new,-1,1))
  8899.       skip = 1;
  8900.     }
  8901.     if (op == 0 && !skip && (collision != RENX_OVWR)) {
  8902.     if (zchki(new) > (CK_OFF_T)-1) { /* New file already exists?  */
  8903.         switch (collision) {    /* Yes, take specified action */
  8904.           case RENX_SKIP:        /* Skip this one and proceed  */
  8905.         skip = 2;
  8906.         break;
  8907.           case RENX_FAIL:        /* Or fail. */
  8908.         skip = 3;
  8909.         if (!listing && !nolist)
  8910.           printf("?File already exists: %s\n",new);
  8911.         }
  8912.     }
  8913.     }
  8914.     debug(F110,"RENAME new",new,0);
  8915.     debug(F101,"RENAME flag","",flag);    
  8916.     debug(F101,"RENAME skip","",skip);    
  8917.     debug(F100,"RENAME ----------------","",0);
  8918.  
  8919.     if (skip == 3) {
  8920.     if (listing) printf("%s => %s (SKIPPED: %s already exists)\n",
  8921.                 old,new,new);
  8922.     rc = 0;
  8923.     } else if (skip) {            /* Skipping this one */
  8924.     if (listing) printf("%s => %s (%s)\n",
  8925.                 old,new,
  8926.                 (skip == 2) ? "COLLISION: SKIPPED" : "SKIPPED");
  8927.     } else {                /* Have to rename this one */
  8928.     if (op == REN_OP_CHK) {        /* Checking for collisions */
  8929.         return((zchki(new) > (CK_OFF_T)-1) ? 0 : 1 );
  8930.     } else if (op == REN_OP_SIM) {    /* Simulating */
  8931.         if (listing) printf("%s => %s (SIMULATED)\n",old,new);
  8932.     } else {            /* Really renaming */
  8933.         if (listing) printf("%s => %s ",old,new);
  8934.         if (zrename(old,new) < 0) {
  8935.         rc = 0;
  8936.         if (listing)
  8937.           printf("(FAILED: %s)\n",ck_errstr());
  8938.         else if (!nolist)
  8939.           printf("?%s\n",ck_errstr());
  8940.         } else {
  8941.         if (listing) printf("(OK)\n");
  8942.         }
  8943.     }
  8944.     }
  8945.     return(success = rc);  /* Succeeds also if nothing needed to be renamed */
  8946. }
  8947.  
  8948. int
  8949. dorenam() {
  8950. #ifndef NOUNICODE
  8951.     extern int nfilc;
  8952.     extern struct keytab fcstab[];
  8953.     extern struct csinfo fcsinfo[];
  8954. #endif    /* NOUNICODE */
  8955.     int cset1 = 0, cset2 = 0;
  8956.  
  8957.     int i, x, z, fn, listing = 0, havename = 0, wild = 0, rc = 1, noarg = 0;
  8958.     int nolist = 0, all = 0, casing = 0, replacing = 0, getval = 0, sim = 0;
  8959.     int converting = 0, collision = 0;
  8960.  
  8961.     char c;
  8962.     struct FDB sw, fi;
  8963.  
  8964.     collision = ren_coll;        /* Inherit SET RENAME COLLISION */
  8965.     listing = ren_list;            /* Inhereit SET RENAME LIST */
  8966.  
  8967.     if (ren_sub[0]) makestr(&(ren_sub[0]),NULL);
  8968.     if (ren_sub[1]) makestr(&(ren_sub[1]),NULL);
  8969.     if (ren_sub[2]) makestr(&(ren_sub[2]),NULL);
  8970.     line[0] = NUL;
  8971.     tmpbuf[0] = NUL;
  8972.  
  8973.     cmfdbi(&sw,                         /* 1st FDB - switches */
  8974.            _CMKEY,                      /* fcode */
  8975.            "Filename or switch",        /* hlpmsg */
  8976.            "",                          /* default */
  8977.            "",                          /* addtl string data */
  8978.            nrenamsw,            /* addtl numeric data 1: tbl size */
  8979.            4,                           /* addtl numeric data 2: 4 = cmswi */
  8980.            xxstring,                    /* Processing function */
  8981.            renamsw,                     /* Keyword table */
  8982.            &fi                          /* Pointer to next FDB */
  8983.            );
  8984.  
  8985.     cmfdbi(&fi,                         /* 2nd FDB - file or directory name */
  8986.            _CMIFI,                      /* fcode */
  8987.            "",                          /* hlpmsg */
  8988.            "",                          /* default */
  8989.            "",                          /* addtl string data */
  8990.            3,                           /* Flags */
  8991.            0,                           /* 0 = Parse file or directory names */
  8992.            xxstring,
  8993.            NULL,
  8994.            NULL
  8995.            );
  8996.  
  8997.     if (cmflgs == 1) {
  8998.     printf("?File or directory name required\n");
  8999.     return(-9);
  9000.     }
  9001.     while (!havename) {
  9002.     noarg = 0;
  9003.         x = cmfdb(&sw);                 /* Parse something */
  9004.     if (x == -3) {            /* They hit Enter prematurely */
  9005.         printf("?Command incomplete\n");
  9006.         return(-9);
  9007.     } else if (x < 0) {        /* Other error */
  9008.         if (x == -1)
  9009.           return(x);
  9010.             if (iswild(atmbuf) && nzxpand(atmbuf,nzxopts) == 0)
  9011.               printf("?No files match: %s\n",brstrip(atmbuf));
  9012.             else if (zchki(atmbuf) == -1)
  9013.           printf("?File not found: %s\n",brstrip(atmbuf));
  9014.         else
  9015.           printf("?Error with switch or filename: %s\n",brstrip(atmbuf));
  9016.         return(-9);
  9017.     }
  9018.     fn = cmresult.nresult;        /* For brevity */
  9019.         switch (cmresult.fcode) {    /* Handle each kind of field */
  9020.           case _CMKEY:            /* Keyword (switch) */
  9021.             c = cmgbrk();
  9022.             if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  9023.                 printf("?This switch does not take an argument\n");
  9024.                 return(-9);
  9025.             }
  9026.             if (!getval && (cmgkwflgs() & CM_ARG))
  9027.           noarg = 1;        /* Remember arg is missing */
  9028.             switch (cmresult.nresult) {    /* Handle the switch */
  9029.               case DEL_LIS:        /* /LIST */
  9030.               case DEL_VRB:        /* /VERBOSE */
  9031.                 listing = 1;
  9032.                 break;
  9033.               case DEL_NOL:        /* /NOLIST */
  9034.               case DEL_QUI:        /* /QUIET */
  9035.         nolist = 1;
  9036.                 listing = 0;
  9037.                 break;
  9038.               case DEL_SIM:        /* /SIMULATE */
  9039.                 sim = 1;
  9040.                 break;
  9041.           case REN_UPP:        /* /UPPER: */
  9042.           case REN_LOW:        /* /LOWER */
  9043.         all = 1;
  9044.         if (!noarg) {
  9045.             if ((x = cmkey((fn == REN_UPP) ? r_upper : r_lower,
  9046.                    2, "","all",xxstring)) < 0) {
  9047.             if (x == -3)
  9048.               x = 1;
  9049.             else
  9050.               return(x);
  9051.             }
  9052.             all = x;
  9053.         }
  9054.         /* 0 = don't convert; 1 = convert to lower; 2 = to upper */
  9055.         casing = (fn == REN_UPP) ? 2 : 1;
  9056.         converting = 0;
  9057.         break;
  9058.           case REN_OVW:        /* /COLLISION */
  9059.         if (!noarg) {
  9060.             if ((x = cmkey(r_collision,
  9061.                    nr_collision,"","overwrite",xxstring))<0) {
  9062.             if (x == -3)
  9063.               x = RENX_OVWR;
  9064.             else
  9065.               return(x);
  9066.             }
  9067.             collision = x;
  9068.         }
  9069.         break;
  9070.           case REN_RPL:        /* /REPLACE: */
  9071.         if (noarg) {
  9072.             printf("?This switch requires an argument\n");
  9073.             return(-9);
  9074.         }
  9075.         if ((x = cmfld("String to remove, or {{String1}{String2}}",
  9076.                    "",&s,xxstring)) < 0) {
  9077.             if (x == -3) {
  9078.             printf("?Target string required\n");
  9079.             x = -9;
  9080.             }
  9081.             return(x);
  9082.         }
  9083.         if (s[0]) {
  9084.             if (s[0] == '{' && s[1] == '{') /* Get the list */
  9085.               makelist(s,ren_sub,3);
  9086.             else
  9087.               makestr(&(ren_sub[0]),s);
  9088.         }
  9089.         converting = 0;
  9090.         break;
  9091.  
  9092.           case REN_SPA:        /* /FIXSPACES: */
  9093.         if (!noarg)
  9094.           if ((x = cmfld("Character or string to replace spaces with",
  9095.                  "_",&s,xxstring)) < 0)
  9096.             if (x == -3)
  9097.               s = "_";
  9098.             else
  9099.               return(x);
  9100.         makestr(&(ren_sub[0])," ");
  9101.         makestr(&(ren_sub[1]),noarg ? "_" : brstrip(s));
  9102.         makestr(&(ren_sub[3]),NULL);
  9103.         converting = 0;
  9104.         break;
  9105.  
  9106. #ifndef NOCSETS
  9107. #ifndef NOUNICODE
  9108.           case REN_XLA:        /* /CONVERT:cset1:cset2 */
  9109.         if (!xfcstab) {        /* Make a copy of the file charset */
  9110.             int i, x;        /* table with CM_ARG set for each */
  9111.             x = (nfilc + 1) * sizeof(struct keytab);
  9112.             xfcstab = (struct keytab *)malloc(x);
  9113.             if (!xfcstab) {
  9114.             printf("?Memory allocation failure\n");
  9115.             return(-9);
  9116.             }
  9117.             for (i = 0; i < nfilc; i++) {
  9118.             xfcstab[i].kwd = fcstab[i].kwd;
  9119.             xfcstab[i].kwval = fcstab[i].kwval;
  9120.             xfcstab[i].flgs = fcstab[i].flgs | CM_ARG;
  9121.             }
  9122.         }
  9123.         if ((x = cmswi(xfcstab,nfilc,
  9124.                    "Character-set of old name","",xxstring)) < 0) {
  9125.             if (x == -3) {
  9126.             printf("?Pair of character-set names required\n");
  9127.             return(-9);
  9128.             } else
  9129.               return(x);
  9130.         }
  9131.         cset1 = x;
  9132.         c = cmgbrk();
  9133.         if (!getval) {
  9134.             printf("?Secondcharacter-set name required\n");
  9135.             return(-9);
  9136.         }
  9137.         if ((x = cmkey(fcstab,nfilc,
  9138.                    "Character-set of new name","",xxstring)) < 0) {
  9139.             if (x == -3) {
  9140.             printf("?Second character-set name required\n");
  9141.             return(-9);
  9142.             } else
  9143.               return(x);
  9144.         }
  9145.         cset2 = x;
  9146.         if (casing)
  9147.           casing = 0;
  9148.         if (ren_sub[0])
  9149.           makestr(&(ren_sub[0]),NULL);
  9150.         if (ren_sub[1])
  9151.           makestr(&(ren_sub[1]),NULL);
  9152.         if (ren_sub[2])
  9153.           makestr(&(ren_sub[2]),NULL);
  9154.         converting = 1;
  9155.         break;
  9156. #endif    /* NOUNICODE */
  9157. #endif    /* NOCSETS */
  9158.             }
  9159.             break;
  9160.           case _CMIFI:            /* File or directory name */
  9161.             s = cmresult.sresult;
  9162.             havename = 1;
  9163.             break;
  9164.           default:
  9165.         printf("?File or directory name required\n");
  9166.         return(-9);
  9167.         }
  9168.     }
  9169.     if (havename) {
  9170.     ckstrncpy(line,s,LINBUFSIZ);    /* Make a safe copy of source name */
  9171.     } else {
  9172.     printf("?Internal error\n");
  9173.     return(-9);            /* Shouldn't happen */
  9174.     }
  9175.     wild = cmresult.nresult;            /* Source specification wild? */
  9176.     if (!wild)
  9177.       wild = iswild(line);
  9178.  
  9179.     debug(F111,"RENAME WILD",line,wild);
  9180.  
  9181.     p = tmpbuf;                         /* Place for new name */
  9182.     p[0] = NUL;
  9183.     replacing = ren_sub[0] ? 1 : 0;
  9184.  
  9185. #ifdef COMMENT
  9186.     if (!(casing || replacing || converting)) {
  9187.     if ((x = cmofi(wild ? "Target directory" : "New name",
  9188.                "",&s,xxstring)) < 0) { /* Get new name */
  9189.         if (x == -3) {
  9190.         printf("?%s required\n",
  9191.                wild ? "Target directory" : "New name");
  9192.         return(-9);
  9193.         } else return(x);
  9194.     }
  9195.     ckstrncpy(p,s,TMPBUFSIZ);    /* Make a safe copy of the new name */
  9196.     }
  9197. #else
  9198.     if ((x = cmofi(wild ? "Target directory" : "New name",
  9199.            "",&s,xxstring)) < 0) { /* Get new name */
  9200.     if (x == -3) {
  9201.         if (casing || replacing || converting) {
  9202.         s = "";
  9203.         } else {
  9204.         printf("?%s required\n",
  9205.                wild ? "Target directory" : "New name");
  9206.         return(-9);
  9207.         }
  9208.     } else return(x);
  9209.     }
  9210.     ckstrncpy(p,s,TMPBUFSIZ);        /* Make a safe copy of the new name */
  9211. #endif    /* COMMENT */
  9212.  
  9213.     if ((y = cmcfm()) < 0) return(y);    /* Confirm the command */
  9214.  
  9215. #ifdef COMMENT
  9216. #ifndef NOUNICODE
  9217.     if (converting) {
  9218.     printf(" From: %s\n",fcsinfo[cset1].keyword);
  9219.     printf(" To:   %s\n",fcsinfo[cset2].keyword);
  9220.     }
  9221. #endif    /* NOUNICODE */
  9222.     if (casing) {
  9223.     printf("CASING: %s\n", (casing == 1) ? "LOWER" : "UPPER");
  9224.     }
  9225.     if (replacing) {
  9226.     printf("REPLACING: '%s' with '%s'\n",
  9227.            ren_sub[0],
  9228.            ren_sub[1] ? ren_sub[1] : "");
  9229.     }
  9230. #endif    /* COMMENT */
  9231.  
  9232.     s = line;
  9233.     if (!wild)                /* Just one */
  9234.       return(success =
  9235.          renameone(s,p,
  9236.                replacing,casing,all,converting,cset1,cset2,
  9237.                listing,nolist,sim,TMPBUFSIZ,collision));
  9238.  
  9239.     if (!casing && !replacing && !converting) {    /* Multiple files */
  9240.     if (!isdir(p)) {
  9241.         printf(            /* if target is not a directory */
  9242. "?Multiple source files not allowed if target is not a directory.\n");
  9243.         return(-9);
  9244.     }
  9245.     }
  9246. #ifdef COMMENT
  9247.     else {                              /* Show full path of target */
  9248.         char buf[CKMAXPATH];            /* (too much) */
  9249.         if (zfnqfp(p,CKMAXPATH,buf))
  9250.           ckstrncpy(tmpbuf,buf,TMPBUFSIZ);
  9251.     }
  9252. #endif /* COMMENT */
  9253.  
  9254. #ifdef VMS
  9255.     conres();                           /* Let Ctrl-C work. */
  9256. #endif /* VMS */
  9257.     debug(F110,"dorename line",line,0);
  9258.  
  9259. #ifdef ZXREWIND
  9260.     z = zxrewind();                     /* Rewind file list */
  9261. #else
  9262.     z = nzxpand(s,0);                   /* Expand file list */
  9263. #endif /* ZXREWIND */
  9264.     debug(F111,"dorename p",p,z);
  9265.  
  9266. #ifdef UNIX
  9267.     if (wild && z > 1)
  9268.       sh_sort(mtchs,NULL,z,0,0,filecase); /* Alphabetize the filename list */
  9269. #endif /* UNIX */
  9270.  
  9271.     /* For /COLLISION:FAIL make a silent pass to see if there would be any */
  9272.  
  9273.     if (collision == RENX_FAIL) {
  9274.     int n = 0;
  9275.     char line[CKMAXPATH+2];
  9276.     while (z-- > 0) {
  9277.         if (!(z == 0 && !wild))
  9278.           znext(line);
  9279.         if (!line[0])
  9280.           break;
  9281.         if (!renameone((char *)line,p,
  9282.                replacing,casing,all,converting,cset1,cset2,
  9283.                0,1,REN_OP_CHK,TMPBUFSIZ,RENX_FAIL))
  9284.           n++;
  9285.     }
  9286.     if (n > 0) {
  9287.         printf("?Failed: %d file%s would be overwritten\n",
  9288.            n, (n != 1) ? "s" : "");
  9289. #ifdef VMS
  9290.         concb((char)escape);
  9291. #endif /* VMS */
  9292.         return(success = 0);
  9293.     }
  9294.     /* Get the file list back. */
  9295. #ifdef ZXREWIND
  9296.     z = zxrewind();
  9297. #else
  9298.     z = nzxpand(s,0);
  9299. #endif /* ZXREWIND */
  9300.     }
  9301.     while (z-- > 0 && rc > 0) {
  9302.     char line[CKMAXPATH+2];
  9303.         if (!(z == 0 && !wild))
  9304.           znext(line);
  9305.         if (!line[0])
  9306.           break;
  9307.     rc = renameone((char *)line,p, 
  9308.                replacing,casing,all,converting,cset1,cset2,
  9309.                listing,nolist,sim,TMPBUFSIZ,collision);
  9310.     }
  9311. #ifdef VMS
  9312.     concb((char)escape);
  9313. #endif /* VMS */
  9314.     return(success = rc);
  9315. }
  9316. #endif /* ZRENAME */
  9317. #endif /* NOFRILLS */
  9318. #endif /* NORENAME */
  9319.  
  9320. #ifndef NOSPL
  9321.  
  9322. /* Do the RETURN command */
  9323.  
  9324. int
  9325. doreturn(s) char *s; {
  9326.     int x;
  9327.     extern int tra_asg;
  9328.     char * line, * lp;
  9329.  
  9330.     if (cmdlvl < 1) {
  9331.         printf("\n?Can't return from level %d\n",maclvl);
  9332.         return(success = 0);
  9333.     }
  9334.     line = malloc(LINBUFSIZ);
  9335.     if (line == NULL)
  9336.       return(success = 0);
  9337.     lp = line;                          /* Expand return value now */
  9338.     x = LINBUFSIZ-1;
  9339.     if (!s) s = "";
  9340.     debug(F110,"RETURN s",s,0);
  9341.     if (zzstring(s,&lp,&x) > -1) {
  9342.         s = line;
  9343.         debug(F110,"RETURN zzstring",s,0);
  9344.     }
  9345.  
  9346.     /* Pop from all FOR/WHILE/SWITCH/XIFs */
  9347.     while ((maclvl > 0) &&
  9348.            (m_arg[maclvl-1][0]) &&
  9349.            (cmdstk[cmdlvl].src == CMD_MD) &&
  9350.            (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  9351.             !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  9352.             !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  9353.             !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  9354.         debug(F111,"RETURN IF/FOR/WHI/SWI pop",m_arg[maclvl-1][0],maclvl);
  9355.         dogta(XXPTA);                   /* Put args back */
  9356.         popclvl();                      /* Pop up two levels */
  9357.         popclvl();
  9358.     }
  9359.     if (tra_asg) {                      /* If tracing show return value */
  9360.         if (*s)
  9361.           printf("<<< %s: \"%s\"\n", m_arg[maclvl][0], s);
  9362.         else
  9363.           printf("<<< %s: (null)\n", m_arg[maclvl][0]);
  9364.     }
  9365.     popclvl();                          /* Pop from enclosing TAKE or macro */
  9366.     debug(F111,"RETURN tolevel",s,maclvl);
  9367.     if (!s) s = "";
  9368.     if (!*s) s = NULL;
  9369.     makestr(&(mrval[maclvl+1]),s);      /* Set the RETURN value */
  9370.     free(line);
  9371.     return(success = 1);                /* Macro succeeds if we RETURN */
  9372. }
  9373. #endif /* NOSPL */
  9374.  
  9375. #ifndef NOSPL
  9376. /* Do the OPEN command */
  9377.  
  9378. int
  9379. doopen()  {                             /* OPEN { append, read, write } */
  9380.     int x, y, z = 0; char *s;
  9381.     static struct filinfo fcb;          /* (must be static) */
  9382.     if ((x = cmkey(opntab,nopn,"mode","",xxstring)) < 0) {
  9383.         if (x == -3) {
  9384.             printf("?Mode required\n");
  9385.             return(-9);
  9386.         } else return(x);
  9387.     }
  9388.     switch (x) {
  9389.       case OPN_FI_R:                    /* Old file (READ) */
  9390.         if (chkfn(ZRFILE) > 0) {
  9391.             printf("?Read file already open\n");
  9392.             return(-2);
  9393.         }
  9394.         if ((z = cmifi("File to read","",&s,&y,xxstring)) < 0) {
  9395.             if (z == -3) {
  9396.                 printf("?Input filename required\n");
  9397.                 return(-9);
  9398.             } else return(z);
  9399.         }
  9400.         if (y) {                                /* No wildcards allowed */
  9401.             printf("\n?Please specify a single file\n");
  9402.             return(-2);
  9403.         }
  9404.         ckstrncpy(line,s,LINBUFSIZ);
  9405.         if ((int)strlen(line) < 1) return(-2);
  9406.         if ((z = cmnum("buffer size","4096",10,&y,xxstring)) < 0)
  9407.           return(z);
  9408.         if (y < 1) {
  9409.             printf("?Positive number required\n");
  9410.             return(-9);
  9411.         }
  9412.         if ((z = cmcfm()) < 0) return(z);
  9413.         readblock = y;
  9414.         if (readbuf)
  9415.           free((char *)readbuf);
  9416.         if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  9417.             printf("?Can't allocate read buffer\n");
  9418.             return(-9);
  9419.         }
  9420.         return(success = zopeni(ZRFILE,line));
  9421.  
  9422. #ifndef MAC
  9423. #ifndef NOPUSH
  9424.       case OPN_PI_R:                    /* Pipe/Process (!READ) */
  9425.         if (nopush) {
  9426.             printf("?Read from pipe disabled\n");
  9427.             return(success=0);
  9428.         }
  9429.         if (chkfn(ZRFILE) > 0) {
  9430.             printf("?Read file already open\n");
  9431.             return(-2);
  9432.         }
  9433.         if ((y = cmtxt("System command to read from","",&s,xxstring)) < 0) {
  9434.             if (y == -3) {
  9435.                 printf("?Command name required\n");
  9436.                 return(-9);
  9437.             } else return(y);
  9438.         }
  9439.         ckstrncpy(line,brstrip(s),LINBUFSIZ);
  9440.         if (!line[0]) return(-2);
  9441.         if ((y = cmcfm()) < 0) return(y);
  9442.         if (!readbuf) {
  9443.             if (!(readbuf = (CHAR *) malloc(readblock+1))) {
  9444.                 printf("?Can't allocate read buffer\n");
  9445.                 return(-9);
  9446.             }
  9447.         }
  9448.         return(success = zxcmd(ZRFILE,line));
  9449.  
  9450.       case OPN_PI_W:                    /* Write to pipe */
  9451.         if (nopush) {
  9452.             printf("?Write to pipe disabled\n");
  9453.             return(success=0);
  9454.         }
  9455.         if (chkfn(ZWFILE) > 0) {
  9456.             printf("?Write file already open\n");
  9457.             return(-2);
  9458.         }
  9459.         if ((y = cmtxt("System command to write to","",&s,xxstring)) < 0) {
  9460.             if (y == -3) {
  9461.                 printf("?Command name required\n");
  9462.                 return(-9);
  9463.             } else return(y);
  9464.         }
  9465.         ckstrncpy(line,brstrip(s),LINBUFSIZ);
  9466.         if (!line[0]) return(-2);
  9467.         if ((y = cmcfm()) < 0) return(y);
  9468.         success = zxcmd(ZWFILE,line);
  9469.         if (!success && msgflg)
  9470.           printf("Can't open process for writing: %s\n",line);
  9471.         return(success);
  9472. #endif /* NOPUSH */
  9473. #endif /* MAC */
  9474.  
  9475.       case OPN_FI_W:                    /* New file (WRITE) */
  9476.       case OPN_FI_A:                    /* (APPEND) */
  9477.         if ((z = cmofi("Name of local file to create","",&s,xxstring)) < 0) {
  9478.             if (z == -3) {
  9479.                 printf("?Filename required\n");
  9480.                 return(-9);
  9481.             } else return(z);
  9482.         }
  9483.         if (z == 2) {
  9484.             printf("?Sorry, %s is a directory name\n",s);
  9485.             return(-9);
  9486.         }
  9487.         if (chkfn(ZWFILE) > 0) {
  9488.             printf("?Write/Append file already open\n");
  9489.             return(-2);
  9490.         }
  9491.         fcb.bs = fcb.cs = fcb.rl = fcb.fmt = fcb.org = fcb.cc = fcb.typ = 0;
  9492.         fcb.lblopts = 0;
  9493.         fcb.dsp = (x == OPN_FI_W) ? XYFZ_N : XYFZ_A; /* Create or Append */
  9494.         ckstrncpy(line,s,LINBUFSIZ);
  9495.         if ((int)strlen(line) < 1) return(-2);
  9496.         if ((y = cmcfm()) < 0) return(y);
  9497.         return(success = zopeno(ZWFILE,line,NULL,&fcb));
  9498.  
  9499. #ifndef NOLOCAL
  9500.       case OPN_SER:                     /* OPEN PORT or LINE */
  9501.       case OPN_NET: {                   /* OPEN HOST */
  9502.           extern int didsetlin, ttnproto;
  9503.           if (x == OPN_NET) {
  9504.               z = ttnproto;
  9505.               ttnproto = NP_NONE;
  9506.           }
  9507.           if ((y = setlin((x == OPN_SER) ? XYLINE : XYHOST, 1, 0)) < 0) {
  9508.               if (x == OPN_NET)
  9509.                 ttnproto = z;
  9510.               success = 0;
  9511.           }
  9512.           didsetlin++;
  9513.           return(y);
  9514.       }
  9515. #endif /* NOLOCAL */
  9516.  
  9517.       default:
  9518.         printf("?Not implemented");
  9519.         return(-2);
  9520.     }
  9521. }
  9522. #endif /* NOSPL */
  9523.  
  9524. #ifndef NOXFER
  9525. /*  D O X G E T  --  GET command parser with switches  */
  9526.  
  9527. #ifdef CK_LABELED
  9528. int g_lf_opts = -1;
  9529. extern int lf_opts;
  9530. #endif /* CK_LABELED */
  9531.  
  9532. int
  9533. doxget(cx) int cx; {
  9534.     extern int                          /* External variables we need */
  9535. #ifdef RECURSIVE
  9536.       recursive,
  9537. #endif /* RECURSIVE */
  9538.       xfermode, fdispla, protocol, usepipes,
  9539.       g_binary, g_xfermode, g_displa, g_rpath, g_usepipes;
  9540.     extern char * rcv_move;             /* Directory to move new files to */
  9541.     extern char * rcv_rename;           /* What to rename new files to */
  9542.     extern char * rcvexcept[];          /* RECEIVE / GET exception list */
  9543.     int opkt  =  0;                     /* Flag for O-Packet needed */
  9544.  
  9545. #ifdef PIPESEND
  9546.     extern int pipesend;
  9547.     extern char * rcvfilter;
  9548. #endif /* PIPESEND */
  9549.     extern struct keytab rpathtab[];
  9550.     extern int nrpathtab;
  9551.     extern CK_OFF_T calibrate;
  9552.     int asname = 0;                     /* Flag for have as-name */
  9553.     int konly = 0;                      /* Kermit-only function */
  9554.     int c, i, n, confirmed = 0;         /* Workers */
  9555.     int getval = 0;                     /* Whether to get switch value */
  9556.     int rcvcmd = 0;                     /* Whether it is the RECEIVE command */
  9557.     int mget = 0;                       /* Whether it is the MGET command */
  9558.     struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
  9559.     struct FDB sw, fl, cm;              /* FDBs for each parse function */
  9560.     char * cmdstr = "this command";
  9561.  
  9562. #ifdef NEWFTP
  9563.     if (cx == XXGET || cx == XXREGET || cx == XXMGET || cx == XXRETR) {
  9564.         extern int ftpget;
  9565.         extern int ftpisopen();
  9566.         if ((ftpget == 1) || ((ftpget == 2) && ftpisopen())) {
  9567.         int x;
  9568.         x = doftpget(cx,0);
  9569.         debug(F101,"doftpget return","",x);
  9570.         if (x > -1)
  9571.           success = x;
  9572.         debug(F101,"doftpget success","",success);
  9573.         return(x);
  9574.     }
  9575.     }
  9576. #endif /* NEWFTP */
  9577.  
  9578.     debug(F101,"xget cx","",cx);
  9579.  
  9580.     oopts = -1;
  9581.     omode = -1;
  9582.  
  9583.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  9584.         pv[i].sval = NULL;
  9585.         pv[i].ival = -1;
  9586.         pv[i].wval = (CK_OFF_T)-1;
  9587.     }
  9588.     /* Preset switch values based on top-level command that called us */
  9589.  
  9590.     switch (cx) {
  9591.       case XXREC:                       /* RECEIVE */
  9592.         cmdstr = "RECEIVE";
  9593.         rcvcmd = 1; break;
  9594.       case XXGET:                       /* GET */
  9595.         cmdstr = "GET";
  9596.         konly = 1;
  9597.         break;
  9598. #ifdef CK_RESEND
  9599.       case XXREGET:                     /* REGET */
  9600.         cmdstr = "REGET";
  9601.         konly = 1;
  9602.         pv[SND_BIN].ival = 1;           /* Implies /BINARY */
  9603.         pv[SND_RES].ival = 1; break;
  9604. #endif /* CK_RESEND */
  9605.       case XXRETR:                      /* RETRIEVE */
  9606.         cmdstr = "RETRIEVE";
  9607.         konly = 1;
  9608.         pv[SND_DEL].ival = 1; break;
  9609. #ifdef PIPESEND
  9610.       case XXCREC:                      /* CRECEIVE */
  9611.         cmdstr = "CRECEIVE";
  9612.         konly = 1;
  9613.         rcvcmd = 1;
  9614.         pv[SND_CMD].ival = 1; break;
  9615.       case XXCGET:                      /* CGET */
  9616.         cmdstr = "CGET";
  9617.         konly = 1;
  9618.         pv[SND_CMD].ival = 1; break;
  9619. #endif /* PIPESEND */
  9620. #ifndef NOMGET
  9621.       case XXMGET:                      /* MGET */
  9622.         cmdstr = "MGET";
  9623.         konly = 1;
  9624.         mget = 1; break;
  9625. #endif /* NOMGET */
  9626.     }
  9627.     debug(F111,"xget rcvcmd",cmdstr,rcvcmd);
  9628.     debug(F101,"xget konly","",konly);
  9629.  
  9630. #ifdef CK_XYZ
  9631.     if (!rcvcmd && protocol != PROTO_K) {
  9632.         printf("?Sorry, %s works only with Kermit protocol\n",cmdstr);
  9633.         return(-9);
  9634.     }
  9635. #endif /* CK_XYZ */
  9636.  
  9637.     /* Set up chained parse functions... */
  9638.  
  9639.     cmfdbi(&sw,                         /* First FDB - command switches */
  9640.            _CMKEY,                      /* fcode */
  9641.            rcvcmd ?
  9642.            "Optional name/template to store incoming files under, or switch" :
  9643.            "Remote filename, or switch", /* hlpmsg */
  9644.            "",                          /* default */
  9645.            "",                          /* addtl string data */
  9646.            rcvcmd ? nrcvtab : ngettab,  /* addtl numeric data 1: tbl size */
  9647.            4,                           /* addtl numeric data 2: 4 = cmswi */
  9648.            xxstring,                    /* Processing function */
  9649.            rcvcmd ? rcvtab : gettab,    /* Keyword table */
  9650.            &fl                          /* Pointer to next FDB */
  9651.            );
  9652.     if (rcvcmd || mget)                 /* RECEIVE or MGET */
  9653.       cmfdbi(&fl,
  9654.            _CMTXT,                      /* fcode */
  9655.            rcvcmd ?                     /* hlpmsg */
  9656.              "Output filename or Command" : /* Output filename */
  9657.              "File(s) to GET",              /* Files we are asking for */
  9658.            "",                          /* default */
  9659.            "",                          /* addtl string data */
  9660.            0,                           /* addtl numeric data 1 */
  9661.            0,                           /* addtl numeric data 2 */
  9662. #ifdef COMMENT
  9663. #ifdef CK_XYZ
  9664.            (protocol == PROTO_X || protocol == PROTO_XC) ?
  9665.              xxstring :
  9666.              (rcvcmd ? (xx_strp)0  : xxstring)
  9667. #else
  9668.            rcvcmd ? (xx_strp)0  : xxstring /* Processing function */
  9669. #endif /* CK_XYZ */
  9670. #else  /* COMMENT */
  9671.        xxstring            /* Always evaluate - fdc 2006/02/01 */
  9672. #endif    /* COMMENT */
  9673.              ,
  9674.            NULL,
  9675.            &cm
  9676.            );
  9677.     else
  9678.       cmfdbi(&fl,                       /* Remote filename or command */
  9679.            _CMFLD,                      /* fcode */
  9680.            "Remote filename",           /* hlpmsg */
  9681.            "",                          /* default */
  9682.            "",                          /* addtl string data */
  9683.            0,                           /* addtl numeric data 1 */
  9684.            0,                           /* addtl numeric data 2 */
  9685.            xxstring,
  9686.            NULL,
  9687.            &cm
  9688.            );
  9689.     cmfdbi(&cm,                         /* Confirmation */
  9690.            _CMCFM,                      /* fcode */
  9691.            "",                          /* hlpmsg */
  9692.            "",                          /* default */
  9693.            "",                          /* addtl string data */
  9694.            0,                           /* addtl numeric data 1 */
  9695.            0,                           /* addtl numeric data 2 */
  9696.            NULL,
  9697.            NULL,
  9698.            NULL
  9699.            );
  9700.  
  9701.     /* (See doxsend() for fuller commentary) */
  9702.  
  9703.     while (1) {                         /* Parse 0 or more switches */
  9704.         x = cmfdb(&sw);                 /* Parse something */
  9705.         debug(F101,"xget cmfdb","",x);
  9706.         if (x < 0)                      /* Error */
  9707.           goto xgetx;                   /* or reparse needed */
  9708.         if (cmresult.fcode != _CMKEY)   /* Break out if not a switch */
  9709.           break;
  9710.         c = cmgbrk();                   /* Get break character */
  9711.         if ((getval = (c == ':' || c == '=')) && !(cmgkwflgs() & CM_ARG)) {
  9712.             printf("?This switch does not take an argument\n");
  9713.             x = -9;
  9714.             goto xgetx;
  9715.         }
  9716.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  9717.             printf("?This switch requires an argument\n");
  9718.             x = -9;
  9719.             goto xgetx;
  9720.         }
  9721.         n = cmresult.nresult;           /* Numeric result = switch value */
  9722.         debug(F101,"xget switch","",n);
  9723.  
  9724.         switch (n) {                    /* Process the switch */
  9725. #ifdef PIPESEND
  9726.           case SND_CMD:                 /* These take no args */
  9727.             if (nopush) {
  9728.                 printf("?Sorry, system command access is disabled\n");
  9729.                 x = -9;
  9730.                 goto xgetx;
  9731.             } else if (rcvfilter) {
  9732.                 printf(
  9733. "?Sorry, no GET /COMMAND when RECEIVE FILTER selected\n");
  9734.                 x = -9;
  9735.                 goto xgetx;
  9736.             }
  9737.             if (rcvcmd)
  9738.               sw.hlpmsg = "Command, or switch"; /* Change help message */
  9739.             /* Fall thru... */
  9740. #endif /* PIPESEND */
  9741.  
  9742.           case SND_REC:                 /* /RECURSIVE */
  9743.             pv[SND_PTH].ival = PATH_REL; /* Implies relative pathnames */
  9744.             pv[n].ival = 1;             /* Set the recursive flag */
  9745.             break;
  9746.  
  9747.           case SND_RES:                 /* /RECOVER */
  9748.             pv[SND_BIN].ival = 1;       /* Implies /BINARY */
  9749.             pv[n].ival = 1;             /* Set the resend flag */
  9750.             break;
  9751.  
  9752.           case SND_DEL:                 /* /DELETE */
  9753.           case SND_SHH:                 /* /QUIET */
  9754.           case SND_CAL:                 /* /CALIBRATE */
  9755.           case SND_XPA:                 /* /TRANSPARENT */
  9756.             pv[n].ival = 1;             /* Just set the appropriate flag */
  9757.             break;
  9758.  
  9759.           case SND_PIP:                 /* /PIPES:{ON,OFF} */
  9760.             if (!getval) {
  9761.                 pv[n].ival = 1;
  9762.                 break;
  9763.             }
  9764.             if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  9765.               goto xgetx;
  9766.             if (!nopush)
  9767.               pv[n].ival = x;
  9768.             break;
  9769.  
  9770.           /* File transfer modes - each undoes the others */
  9771.  
  9772.           case SND_BIN:                 /* Binary */
  9773.           case SND_TXT:                 /* Text */
  9774.           case SND_IMG:                 /* Image */
  9775.           case SND_LBL:                 /* Labeled */
  9776.             pv[SND_BIN].ival = 0;       /* Unset all */
  9777.             pv[SND_TXT].ival = 0;
  9778.             pv[SND_IMG].ival = 0;
  9779.             pv[SND_LBL].ival = 0;
  9780.             pv[n].ival = 1;             /* Set the requested one */
  9781.             break;
  9782.  
  9783.           case SND_EXC:                 /* Excludes */
  9784.             if (!getval) break;
  9785.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  9786.                 if (x == -3) {
  9787.                     printf("?Pattern required\n");
  9788.                     x = -9;
  9789.                 }
  9790.                 goto xgetx;
  9791.             }
  9792.             if (pv[n].sval) free(pv[n].sval);
  9793.             y = strlen(s);
  9794.             if (y > 256) {
  9795.                 printf("?Pattern too long - 256 max\n");
  9796.                 x = -9;
  9797.                 goto xgetx;
  9798.             }
  9799.             pv[n].sval = malloc(y+1);
  9800.             if (pv[n].sval) {
  9801.                 strcpy(pv[n].sval,s);   /* safe */
  9802.                 pv[n].ival = 1;
  9803.             }
  9804.             break;
  9805.  
  9806. #ifdef COMMENT
  9807.           /* Not implemented */
  9808.           case SND_PRI:                 /* GET to printer */
  9809.             pv[n].ival = 1;
  9810.             if (!getval) break;
  9811.             if ((x = cmfld("Print options","",&s,xxstring)) < 0)
  9812.               goto xgetx;
  9813.             pv[n].sval = malloc((int)strlen(s)+1);
  9814.             if (pv[n].sval)
  9815.               strcpy(pv[n].sval,s);     /* safe */
  9816.             break;
  9817. #endif /* COMMENT */
  9818.  
  9819.           case SND_MOV:                 /* MOVE after */
  9820.           case SND_REN:                 /* RENAME after */
  9821.             if (!getval) break;
  9822.             if ((x = cmfld(n == SND_MOV ?
  9823.            "device and/or directory for source file after sending" :
  9824.            "new name for source file after sending",
  9825.                            "",
  9826.                            &s,
  9827.                            n == SND_MOV ? xxstring : NULL
  9828.                            )) < 0) {
  9829.                 if (x == -3) {
  9830.                     printf("%s\n", n == SND_MOV ?
  9831.                            "?Destination required" :
  9832.                            "?New name required"
  9833.                            );
  9834.                     x = -9;
  9835.                 }
  9836.                 goto xgetx;
  9837.             }
  9838.             if (pv[n].sval) {
  9839.                 free(pv[n].sval);
  9840.                 pv[n].sval = NULL;
  9841.             }
  9842.             s = brstrip(s);
  9843.             y = strlen(s);
  9844.             if (y > 0) {
  9845.                 pv[n].sval = malloc(y+1);
  9846.                 if (pv[n].sval) {
  9847.                     strcpy(pv[n].sval,s); /* safe */
  9848.                     pv[n].ival = 1;
  9849.                 }
  9850.             }
  9851.             break;
  9852.  
  9853.           case SND_ASN:                 /* As-name */
  9854.             if (!getval) break;
  9855.             if (mget) {
  9856.                 printf("?Sorry, as-name not allowed with MGET\n");
  9857.                 x = -9;
  9858.                 goto xgetx;
  9859.             }
  9860.             if (
  9861. #ifdef COMMENT
  9862.         (x = cmfld("Name to store it under","",&s,NULL))
  9863. #else
  9864.         (x = cmfld("Name to store it under","",&s,xxstring))
  9865. #endif    /* COMMENT */
  9866.         < 0)
  9867.               goto xgetx;
  9868.             s = brstrip(s);
  9869.             if ((y = strlen(s)) > 0) {
  9870.                 if (pv[n].sval) free(pv[n].sval);
  9871.                 pv[n].sval = malloc(y+1);
  9872.                 if (pv[n].sval) {
  9873.                     strcpy(pv[n].sval,s); /* safe */
  9874.                     pv[n].ival = 1;
  9875.                 }
  9876.             }
  9877.             break;
  9878.  
  9879. #ifdef PIPESEND
  9880.           case SND_FLT:                 /* Filter */
  9881.             debug(F101,"xget /filter getval","",getval);
  9882.             if (!getval) break;
  9883.             if ((x = cmfld("Filter program to receive through",
  9884.                            "",&s,NULL)) < 0) {
  9885.                 if (x == -3)
  9886.                   s = "";
  9887.                 else
  9888.                   goto xgetx;
  9889.             }
  9890.             if (*s) s = brstrip(s);
  9891.             y = strlen(s);
  9892.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  9893.                 if (s[x] != '\\') continue;
  9894.                 if (s[x+1] == 'v') break;
  9895.             }
  9896.             if (x == y) {
  9897.                 printf(
  9898.                 "?Filter must contain a replacement variable for filename.\n"
  9899.                        );
  9900.                 x = -9;
  9901.                 goto xgetx;
  9902.             }
  9903.             pv[n].ival = 1;
  9904.             if (pv[n].sval) {
  9905.                 free(pv[n].sval);
  9906.                 pv[n].sval = NULL;
  9907.             }
  9908.             if ((y = strlen(s)) > 0) {
  9909.                 if ((pv[n].sval = malloc(y+1)))
  9910.                   strcpy(pv[n].sval,s); /* safe */
  9911.             }
  9912.             break;
  9913. #endif /* PIPESEND */
  9914.  
  9915.           case SND_PTH:                 /* Pathnames */
  9916.             if (!getval) {
  9917.                 pv[n].ival = PATH_REL;
  9918.                 break;
  9919.             }
  9920.             if ((x = cmkey(rpathtab,nrpathtab,"","on",xxstring)) < 0)
  9921.               goto xgetx;
  9922.             pv[n].ival = x;             /* Ditto */
  9923.             break;
  9924.  
  9925.           case SND_NAM:                 /* Filenames */
  9926.             if (!getval) break;
  9927.             if ((x = cmkey(fntab,nfntab,"","converted",xxstring)) < 0)
  9928.               goto xgetx;
  9929.             pv[n].ival = x;
  9930.             break;
  9931.  
  9932.           case SND_PRO:                 /* Protocol to use */
  9933.             if (!getval) break;
  9934.             if ((x = cmkey(protos,nprotos,"File-transfer protocol","",
  9935.                            xxstring)) < 0) {
  9936.                 if (x == -3)
  9937.                   x = 0;
  9938.                 else
  9939.                   goto xgetx;
  9940.             }
  9941.             debug(F111,"xget /proto",atmbuf,x);
  9942.             pv[n].ival = x;
  9943.             if (konly && x != PROTO_K) {
  9944.                 printf(
  9945. "?Sorry, this command works only with Kermit protocol\n"
  9946.                        );
  9947.                 x = -9;
  9948.                 goto xgetx;
  9949.             }
  9950.             break;
  9951.  
  9952.           default:
  9953.             printf("?Unexpected switch value - %d\n",cmresult.nresult);
  9954.             x = -9;
  9955.             goto xgetx;
  9956.         }
  9957.     }
  9958.     debug(F101,"xget cmresult fcode","",cmresult.fcode);
  9959.  
  9960.     cmarg = line;                       /* Initialize string pointers */
  9961.     cmarg2 = tmpbuf;
  9962.     asname = 0;
  9963.     line[0] = NUL;                      /* and buffers. */
  9964.     tmpbuf[0] = NUL;
  9965.  
  9966.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  9967.       case _CMFLD:                      /* (3) Remote filespec */
  9968.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  9969.         break;
  9970.       case _CMTXT:                      /* (4) As-name */
  9971.         if (rcvcmd) {
  9972.             ckstrncpy(tmpbuf,cmresult.sresult,TMPBUFSIZ);
  9973.             if ((int)strlen(tmpbuf) > 0)
  9974.               asname = 1;
  9975.         } else {
  9976.             ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  9977.         }
  9978.       case _CMCFM:                      /* (6) Confirmation */
  9979.         confirmed = 1;
  9980.         break;
  9981.       default:
  9982.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  9983.         x = -9;
  9984.         goto xgetx;
  9985.     }
  9986.     debug(F110,"xget string",cmarg,0);
  9987.     debug(F101,"xget confirmed","",confirmed);
  9988.  
  9989.     cmarg = brstrip(cmarg);             /* Strip any braces */
  9990.  
  9991.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  9992.         if (pv[SND_CMD].ival > 0) {
  9993.             debug(F100,"xget calling cmtxt","",0);
  9994.             x = cmtxt("Local command to pipe into","",&s,NULL);
  9995.             if (x < 0 && x != -3) goto xgetx;
  9996.             if (x != -3) {
  9997.                 ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  9998.                 asname = 1;
  9999.             }
  10000.         } else if (!rcvcmd) {
  10001. #ifdef VMS
  10002.             /* cmofi() fails if you give it a directory name */
  10003.             x = cmfld("Name or directory for incoming file","",&s,NULL);
  10004.             debug(F111,"xget cmfld",s,x);
  10005. #else
  10006.             x = cmofi("Name or directory for incoming file","",&s,NULL);
  10007.             debug(F111,"xget cmofi",s,x);
  10008. #endif /* VMS */
  10009.             if (x < 0 && x != -3) goto xgetx;
  10010.             if (x != -3) {
  10011.                 ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  10012.                 if ((x = cmcfm()) < 0) goto xgetx;
  10013.                 asname = 1;
  10014.             }
  10015.         }
  10016.     }
  10017.     /* Arrive here with cmarg and cmarg2 all set */
  10018.  
  10019.     debug(F111,"xget asname",cmarg2,asname);
  10020.     if (!asname) {
  10021.         if (pv[SND_ASN].sval)
  10022.           ckstrncpy(tmpbuf,pv[SND_ASN].sval,TMPBUFSIZ);
  10023.         else
  10024.           tmpbuf[0] = NUL;
  10025.     }
  10026.     cmarg2 = brstrip(cmarg2);           /* Strip outer braces if any. */
  10027.     debug(F110,"xget cmarg",cmarg,0);
  10028.     debug(F110,"xget cmarg2",cmarg2,0);
  10029.  
  10030.     if (!*cmarg &&
  10031.         (cx == XXGET || cx == XXREGET || cx == XXCGET || cx == XXMGET)) {
  10032.         printf("?A remote file specification is required\n");
  10033.         x = -9;
  10034.         goto xgetx;
  10035.     }
  10036. #ifdef PIPESEND
  10037.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND sets pipesend flag */
  10038.         x = -9;
  10039.         if (!*cmarg2) {
  10040.             printf("?Command required\n");
  10041.             goto xgetx;
  10042.         } else if (nopush) {
  10043.             printf("?Sorry, system command access is disabled\n");
  10044.             goto xgetx;
  10045.         } else if (rcvfilter) {
  10046.             printf("?Sorry, no GET /COMMAND while RECEIVE FILTER selected\n");
  10047.             goto xgetx;
  10048.         } else
  10049.           pipesend = 1;
  10050.     }
  10051.     debug(F101,"xget /COMMAND pipesend","",pipesend);
  10052. #endif /* PIPESEND */
  10053.  
  10054. #ifdef CK_RESEND
  10055.     if (pv[SND_RES].ival > 0) {         /* REGET or GET /RECOVER */
  10056. #ifdef RECURSIVE
  10057.         if (pv[SND_REC].ival > 0) {     /* RECURSIVE */
  10058. #ifdef COMMENT
  10059.             printf("?Unsupported option combination: /RECOVER /RECURSIVE\n");
  10060.             x = -9;
  10061.             goto xgetx;
  10062. #else
  10063.             opkt = 1;
  10064. #endif /* COMMENT */
  10065.         }
  10066. #endif /* RECURSIVE */
  10067.         if (pv[SND_DEL].ival > 0) {     /* /DELETE */
  10068. #ifdef COMMENT
  10069.             printf("?Unsupported option combination: /RECOVER /DELETE\n");
  10070.             x = -9;
  10071.             goto xgetx;
  10072. #else
  10073.             opkt = 1;
  10074. #endif /* COMMENT */
  10075.         }
  10076.     }
  10077. #endif /* CK_RESEND */
  10078.  
  10079.     if (pv[SND_EXC].ival > 0)           /* /EXCEPT */
  10080.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  10081.  
  10082. #ifdef IKS_OPTION
  10083.     if (!rcvcmd
  10084. #ifdef CK_XYZ
  10085.          && protocol == PROTO_K
  10086. #endif /* CK_XYZ */
  10087.          ) {
  10088.         if (!iks_wait(KERMIT_REQ_START,1)) {
  10089.             printf(
  10090.               "?A Kermit Server is not available to process this command\n");
  10091.             x = -9;                     /* correct the return code */
  10092.             goto xgetx;
  10093.         }
  10094.     }
  10095. #endif /* IKS_OPTION */
  10096.  
  10097. #ifdef CK_XYZ
  10098.     {
  10099.         int po, pg;                     /* (for clarity) */
  10100.         po = pv[SND_PRO].ival;          /* /PROTOCOL option */
  10101.         pg = protocol;                  /* Protocol global  */
  10102.         if ((rcvcmd && !*cmarg2) &&     /* If no as-name was given */
  10103.             /* and /PROTOCOL is XMODEM or global protocol is XMODEM... */
  10104.             ((po <  0 && (pg == PROTO_X || pg == PROTO_XC)) ||
  10105.              (po > -1 && (po == PROTO_X || po == PROTO_XC)))
  10106.             ) {
  10107.             printf(
  10108. "Sorry, you must specify a name when receiving a file with XMODEM protocol\n"
  10109.                    );
  10110.             x = -9;
  10111.             goto xgetx;
  10112.         }
  10113.     }
  10114. #endif /* CK_XYZ */
  10115.  
  10116. #ifdef RECURSIVE
  10117.     if (pv[SND_REC].ival > 0) {         /* RECURSIVE */
  10118.         recursive = 1;
  10119.         pv[SND_PTH].ival = PATH_REL;    /* Implies relative pathnames too */
  10120.     }
  10121. #endif /* RECURSIVE */
  10122.  
  10123.     if (pv[SND_PIP].ival > -1) {
  10124.         g_usepipes = usepipes;
  10125.         usepipes = pv[SND_PIP].ival;
  10126.     }
  10127.  
  10128.     /* Save global protocol parameters */
  10129.  
  10130.     g_proto = protocol;
  10131. #ifdef CK_LABELED
  10132.     g_lf_opts = lf_opts;                /* Save labeled transfer options */
  10133. #endif /* CK_LABELED */
  10134.     g_urpsiz = urpsiz;                  /* Receive packet length */
  10135.     g_spsizf = spsizf;                  /* Send packet length flag */
  10136.     g_spsiz = spsiz;                    /* Send packet length */
  10137.     g_spsizr = spsizr;                  /* etc etc */
  10138.     g_spmax = spmax;
  10139.     g_wslotr = wslotr;
  10140.     g_prefixing = prefixing;
  10141.     g_fncact = fncact;
  10142.     g_fncnv = fncnv;
  10143.     g_fnspath = fnspath;
  10144.     g_fnrpath = fnrpath;
  10145.     g_xfrxla = xfrxla;
  10146.  
  10147.     if (pv[SND_PRO].ival > -1) {        /* Change according to switch */
  10148.         protocol = pv[SND_PRO].ival;
  10149.         if (ptab[protocol].rpktlen > -1)   /* copied from initproto() */
  10150.             urpsiz = ptab[protocol].rpktlen;
  10151.         if (ptab[protocol].spktflg > -1)
  10152.             spsizf = ptab[protocol].spktflg;
  10153.         if (ptab[protocol].spktlen > -1) {
  10154.             spsiz = ptab[protocol].spktlen;
  10155.             if (spsizf)
  10156.                 spsizr = spmax = spsiz;
  10157.         }
  10158.         if (ptab[protocol].winsize > -1)
  10159.             wslotr = ptab[protocol].winsize;
  10160.         if (ptab[protocol].prefix > -1)
  10161.             prefixing = ptab[protocol].prefix;
  10162.         if (ptab[protocol].fnca > -1)
  10163.             fncact  = ptab[protocol].fnca;
  10164.         if (ptab[protocol].fncn > -1)
  10165.             fncnv   = ptab[protocol].fncn;
  10166.         if (ptab[protocol].fnsp > -1)
  10167.             fnspath = ptab[protocol].fnsp;
  10168.         if (ptab[protocol].fnrp > -1)
  10169.             fnrpath = ptab[protocol].fnrp;
  10170.     }
  10171.     debug(F101,"xget protocol","",protocol);
  10172.     debug(F111,"xget cmarg2",cmarg2,xfermode);
  10173.  
  10174.     g_xfermode = xfermode;
  10175.     g_binary = binary;
  10176.     if (pv[SND_BIN].ival > 0) {         /* Change according to switch */
  10177.         xfermode = XMODE_M;
  10178.         binary = XYFT_B;                /* FILE TYPE BINARY */
  10179.         omode = GMOD_BIN;               /* O-Packet mode */
  10180.         debug(F101,"doxget /BINARY xfermode","",xfermode);
  10181.     } else if (pv[SND_TXT].ival > 0) {  /* Ditto for /TEXT */
  10182.         xfermode = XMODE_M;
  10183.         binary = XYFT_T;
  10184.         omode = GMOD_TXT;
  10185.         debug(F101,"doxget /TEXT xfermode","",xfermode);
  10186.     } else if (pv[SND_IMG].ival > 0) {
  10187.         xfermode = XMODE_M;
  10188. #ifdef VMS
  10189.         binary = XYFT_I;
  10190. #else
  10191.         binary = XYFT_B;
  10192. #endif /* VMS */
  10193.         omode = GMOD_TXT;
  10194.         debug(F101,"doxget /IMAGE xfermode","",xfermode);
  10195.     }
  10196. #ifdef CK_LABELED
  10197.     else if (pv[SND_LBL].ival > 0) {
  10198.         xfermode = XMODE_M;
  10199.         binary = XYFT_L;
  10200.         omode = GMOD_LBL;
  10201.         debug(F101,"doxget /LABELED xfermode","",xfermode);
  10202.     }
  10203. #endif /* CK_LABELED */
  10204.     debug(F101,"xget binary","",binary);
  10205.     debug(F101,"xget omode","",omode);
  10206.  
  10207.     if (pv[SND_XPA].ival > 0)           /* /TRANSPARENT */
  10208.       xfrxla = 0;                       /* Don't translate character sets */
  10209.  
  10210. #ifdef PIPESEND
  10211.     if (pv[SND_FLT].ival > 0)
  10212.       makestr(&rcvfilter,pv[SND_FLT].sval);
  10213. #endif /* PIPESEND */
  10214.  
  10215. #ifdef CK_TMPDIR
  10216.     if (pv[SND_MOV].ival > 0) {
  10217.         int len;
  10218.         char * p = pv[SND_MOV].sval;
  10219. #ifdef CK_LOGIN
  10220.         if (isguest) {
  10221.             printf("?Sorry, /MOVE-TO not available to guests\n");
  10222.             x = -9;
  10223.             goto xgetx;
  10224.         }
  10225. #endif /* CK_LOGIN */
  10226.         len = strlen(p);
  10227.         if (!isdir(p)) {                /* Check directory */
  10228. #ifdef CK_MKDIR
  10229.             char * s = NULL;
  10230.             s = (char *)malloc(len + 4);
  10231.             if (s) {
  10232.                 strcpy(s,p);            /* safe */
  10233. #ifdef datageneral
  10234.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  10235. #else
  10236.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  10237. #endif /* datageneral */
  10238.                 s[len++] = 'X';
  10239.                 s[len] = NUL;
  10240.                 x = zmkdir(s);
  10241.                 free(s);
  10242.                 if (x < 0) {
  10243.                     printf("?Can't create \"%s\"\n",p);
  10244.                     x = -9;
  10245.                     goto xgetx;
  10246.                 }
  10247.             }
  10248. #else
  10249.             printf("?Directory \"%s\" not found\n",p);
  10250.             x = -9;
  10251.             goto xgetx;
  10252. #endif /* CK_MKDIR */
  10253.         }
  10254.         zfnqfp(p,LINBUFSIZ,line);
  10255.         makestr(&rcv_move,line);
  10256.     }
  10257. #endif /* CK_TMPDIR */
  10258.  
  10259.     if (pv[SND_REN].ival > 0) {         /* /RENAME-TO:name */
  10260.         char * p = pv[SND_REN].sval;
  10261. #ifdef CK_LOGIN
  10262.         if (isguest) {
  10263.             printf("?Sorry, /RENAME-TO not available to guests\n");
  10264.             x = -9;
  10265.             goto xgetx;
  10266.         }
  10267. #endif /* CK_LOGIN */
  10268.         if (!p) p = "";
  10269.         if (!*p) {
  10270.             printf("?New name required for /RENAME\n");
  10271.             x = -9;
  10272.             goto xgetx;
  10273.         }
  10274.         p = brstrip(p);
  10275.         makestr(&rcv_rename,p);
  10276.         debug(F110,"xget rcv_rename","",0);
  10277.     }
  10278.  
  10279. #ifdef CALIBRATE
  10280.     if (pv[SND_CAL].ival > 0)
  10281.       calibrate = (CK_OFF_T)1;
  10282. #endif /* CALIBRATE */
  10283.     g_displa = fdispla;
  10284.     if (pv[SND_SHH].ival > 0)
  10285.       fdispla = 0;
  10286.     debug(F101,"xget display","",fdispla);
  10287.  
  10288.     if (pv[SND_NAM].ival > -1) {        /* /FILENAMES */
  10289.         g_fncnv = fncnv;                /* Save global value */
  10290.         fncnv = pv[SND_NAM].ival;
  10291.         debug(F101,"xsend fncnv","",fncnv);
  10292.         /* We should also handle O packet filename option here */
  10293.         /* but we don't really need to since WHATAMI already handles it */
  10294.     }
  10295.     if (pv[SND_PTH].ival > -1) {        /* PATHNAMES */
  10296.         g_rpath = fnrpath;              /* Save global values */
  10297.         fnrpath = pv[SND_PTH].ival;
  10298.         debug(F101,"xsend fnrpath","",fnrpath);
  10299. #ifndef NZLTOR
  10300.         if (fnrpath != PATH_OFF) {
  10301.             g_fncnv = fncnv;
  10302.             fncnv = XYFN_L;
  10303.             debug(F101,"xsend fncnv","",fncnv);
  10304.         }
  10305.         /* We should also handle O packet pathname option here */
  10306.         /* but we don't really need to since WHATAMI already handles it */
  10307. #endif /* NZLTOR */
  10308.     }
  10309.  
  10310.     /* Set protocol start state */
  10311.  
  10312.     if (opkt) {                         /* Extended GET Options*/
  10313.         sstate = (CHAR) 'o';
  10314.         oopts = 0;
  10315.         if (pv[SND_DEL].ival > 0) oopts |= GOPT_DEL; /* GET /DELETE */
  10316.         if (pv[SND_RES].ival > 0) oopts |= GOPT_RES; /* GET /RECOVER */
  10317.         if (pv[SND_REC].ival > 0) oopts |= GOPT_REC; /* GET /RECURSIVE */
  10318.     } else if (rcvcmd)
  10319.       sstate = (CHAR) 'v';              /* RECEIVE or CRECEIVE */
  10320.     else if (pv[SND_DEL].ival > 0)
  10321.       sstate = (CHAR) 'h';              /* GET /DELETE (= RETRIEVE) */
  10322.     else if (pv[SND_RES].ival > 0)
  10323.       sstate = (CHAR) 'j';              /* GET /RECOVER (= REGET) */
  10324.     else
  10325.       sstate = (CHAR) 'r';              /* Regular GET */
  10326.     getcmd = 1;
  10327.     debug(F000,"xget sstate","",sstate);
  10328. #ifdef MAC
  10329.     what = W_RECV;
  10330.     scrcreate();
  10331. #endif /* MAC */
  10332.     if (local) {
  10333.         if (pv[SND_SHH].ival != 0)
  10334.           displa = 1;
  10335.     if (protocol == PROTO_K)    /* fdc 20070108 */
  10336.       ttflui();
  10337.     }
  10338.     x = 0;
  10339. #ifdef PIPESEND
  10340.     if (pipesend)
  10341.       goto xgetx;
  10342. #endif /* PIPESEND */
  10343.  
  10344. #ifdef CK_TMPDIR
  10345. /*
  10346.   cmarg2 is also allowed to be a device or directory name;
  10347.   even the name of a directory that doesn't exist.
  10348. */
  10349.     y = strlen(cmarg2);
  10350.     debug(F111,"xget strlen(cmarg2)",cmarg2,y);
  10351.     if ((y > 0) &&
  10352. #ifdef OS2
  10353.         ((isalpha(cmarg2[0]) &&
  10354.          cmarg2[1] == ':' &&
  10355.          cmarg2[2] == NUL) ||
  10356.         (cmarg[y-1] == '/' || cmarg[y-1] == '\\') ||
  10357.         isdir(cmarg2))
  10358. #else
  10359. #ifdef UNIXOROSK
  10360.         (cmarg2[y-1] == '/' || isdir(cmarg2))
  10361. #else
  10362. #ifdef VMS
  10363.         (cmarg2[y-1] == ']' || cmarg2[y-1] == '>' || isdir(cmarg2))
  10364. #else
  10365. #ifdef STRATUS
  10366.         (cmarg2[y-1] == '>' || isdir(cmarg2))
  10367. #else
  10368. #ifdef datageneral
  10369.         (cmarg2[y-1] == ':' || cmarg[0] == ':' || isdir(cmarg2))
  10370. #else
  10371.         isdir(cmarg2)
  10372. #endif /* datageneral */
  10373. #endif /* STRATUS */
  10374. #endif /* VMS */
  10375. #endif /* UNIXOROSK */
  10376. #endif /* OS2 */
  10377.         ) {
  10378.         debug(F110,"doxget RECEIVE cmarg2 disk or dir",cmarg2,0);
  10379.         if (!f_tmpdir) {
  10380.             int x;
  10381.             s = zgtdir();
  10382.             if (s) {
  10383.                 ckstrncpy(savdir,s,TMPDIRLEN); /* remember old disk/dir */
  10384.                 f_tmpdir = 1;   /* and that we did this */
  10385.             } else {
  10386.                 printf("?Can't get current directory\n");
  10387.                 cmarg2 = "";
  10388.                 f_tmpdir = 0;
  10389.                 x = -9;
  10390.                 goto xgetx;
  10391.             }
  10392. #ifdef CK_MKDIR
  10393.             x = zchki(cmarg2);          /* Does as-name exist? */
  10394.             if (x == -1) {              /* Doesn't exist */
  10395.                 char * p = NULL;        /* Try to create it */
  10396.                 x = strlen(cmarg2);
  10397.                 if ((p = (char *)malloc(x+4))) {
  10398.                     sprintf(p,"%s%s",cmarg2,"x.x"); /* SAFE (prechecked) */
  10399.                     x = zmkdir(p);
  10400.                     free(p);
  10401.                     if (x < 0) {
  10402.                         printf("?Can't create %s\n",cmarg2);
  10403.                         x = -9;
  10404.                         goto xgetx;
  10405.                     }
  10406.                 }
  10407.             }
  10408. #endif /* CK_MKDIR */
  10409.             if (!zchdir(cmarg2)) {      /* change to given disk/directory, */
  10410.                 printf("?Can't access %s\n",cmarg2);
  10411.                 x = -9;
  10412.                 goto xgetx;
  10413.             }
  10414.             cmarg2 = "";
  10415.         }
  10416.     }
  10417. #endif /* CK_TMPDIR */
  10418.  
  10419.     ckstrncpy(fspec,cmarg,CKMAXPATH);   /* Note - this is a REMOTE filespec */
  10420.     debug(F111,"xget fspec",fspec,fspeclen);
  10421.     debug(F110,"xget cmarg2",cmarg2,0);
  10422.  
  10423.   xgetx:
  10424.     for (i = 0; i < SND_MAX; i++)
  10425.       if (pv[i].sval)
  10426.         free(pv[i].sval);
  10427.     return(x);
  10428. }
  10429. #endif /* NOXFER */
  10430.  
  10431. #ifndef NOSPL
  10432.  
  10433. /*
  10434.   D O G T A  --  Do _GETARGS or _PUTARGS Command.
  10435.  
  10436.   Used by XIF, FOR, WHILE, and SWITCH, each of which are implemented as
  10437.   2-level macros; the first level defines the macro, the second runs it.
  10438.   This routine hides the fact that they are macros by importing the
  10439.   macro arguments (if any) from two levels up, to make them available
  10440.   in the IF, FOR, SWITCH, and WHILE commands themselves; for example as
  10441.   loop indices, etc, and within the IF/FOR/WHILE/SWITCH body itself.
  10442.   _PUTARGS is in case we changed any of these variables or used SHIFT
  10443.   on them, so the new values won't be lost as we pop up the stack.
  10444. */
  10445. int
  10446. dogta(cx) int cx; {
  10447.     int i, n;
  10448.     char c, *p,  mbuf[4];
  10449.     extern int topargc, cmdint;
  10450.     extern char ** topxarg;
  10451.  
  10452.     if ((y = cmcfm()) < 0)
  10453.       return(y);
  10454.     debug(F101,"dogta cx","",cx);
  10455.     debug(F101,"dogta maclvl","",maclvl);
  10456.     if (cx == XXGTA) {
  10457.         debug(F101,"dogta _GETARGS maclvl","",maclvl);
  10458.     } else if (cx == XXPTA) {
  10459.         debug(F101,"dogta _PUTARGS maclvl","",maclvl);
  10460.     } else {
  10461.         return(-2);
  10462.     }
  10463.     if (maclvl < 1)
  10464.       return(success = 0);
  10465.  
  10466.     /* Make new copies of macro arguments /%0..9 */
  10467.  
  10468.     mbuf[0] = '%'; mbuf[1] = '0'; mbuf[2] = NUL; /* Argument name buf */
  10469.  
  10470.     if (cx == XXPTA) {                  /* Go NOINT because _PUTARGS */
  10471.         if (cmdint)                     /* temporarily changes maclvl. */
  10472.           connoi();                     /* Interrupts OFF. */
  10473.     }
  10474.     for (i = 0; i < 10; i++) {          /* For all args */
  10475.         c = (char) (i + '0');           /* Make name */
  10476.         mbuf[1] = (char) c;             /* Insert digit */
  10477.         if (cx == XXGTA) {              /* Get arg from level-minus-2 */
  10478.             if (maclvl == 1) p = g_var[c]; /* If at level 1 use globals 0..9 */
  10479.             else p = m_arg[maclvl-2][i];   /* Otherwise they're on the stack */
  10480.             addmac(mbuf,p);
  10481. #ifdef COMMENT
  10482.             if (maclvl > 1)
  10483.               makestr(&(m_line[maclvl]),m_line[maclvl-2]);
  10484. #endif /* COMMENT */
  10485.         } else if (cx == XXPTA) {       /* Put args level+2 */
  10486.             maclvl -= 2;                /* This is gross, it's because we're */
  10487.             addmac(mbuf,m_arg[maclvl+2][i]); /* adding macros two levels up */
  10488.             maclvl += 2;                     /* and addmac() uses maclvl. */
  10489.             count[cmdlvl - 2]  = count[cmdlvl];
  10490.             intime[cmdlvl - 2] = intime[cmdlvl];
  10491.             inpcas[cmdlvl - 2] = inpcas[cmdlvl];
  10492.             takerr[cmdlvl - 2] = takerr[cmdlvl];
  10493.             merror[cmdlvl - 2] = merror[cmdlvl];
  10494.             xquiet[cmdlvl - 2] = xquiet[cmdlvl];
  10495.             xvarev[cmdlvl - 2] = xvarev[cmdlvl];
  10496.         } else return(success = 0);     /* Bad call to this routine */
  10497.     }
  10498.     if (cx == XXPTA) {                  /* Restore interrupts if we */
  10499.         if (cmdint)                     /* turned them off above. */
  10500.           conint(trap,stptrap);
  10501.     }
  10502.     /* Now take care of the argument vector array \&_[], \v(return), */
  10503.     /* and \v(argc) by just copying the pointers. */
  10504.  
  10505.     if (cx == XXGTA) {                  /* GETARGS from 2 levels up */
  10506.         if (maclvl == 1) {
  10507.             a_ptr[0] = topxarg;         /* \&_[] array */
  10508.             a_dim[0] = topargc - 1;     /* Dimension doesn't include [0] */
  10509.             m_xarg[maclvl] = topxarg;
  10510.             n_xarg[maclvl] = topargc;   /* But \v(argc) does include \%0 */
  10511.             macargc[maclvl] = topargc;
  10512.             makestr(&(mrval[maclvl+1]),mrval[0]); /* (see vnlook()) */
  10513.         } else {
  10514.             a_ptr[0] = m_xarg[maclvl-2];
  10515.             a_dim[0] = n_xarg[maclvl-2];
  10516.             m_xarg[maclvl] = m_xarg[maclvl-2];
  10517.             n_xarg[maclvl] = n_xarg[maclvl-2];
  10518.             macargc[maclvl] = n_xarg[maclvl-2];
  10519.             makestr(&(mrval[maclvl+1]),mrval[maclvl-1]); /* (see vnlook()) */
  10520.  
  10521.         }
  10522.     } else {                            /* PUTARGS 2 levels up */
  10523.         if (maclvl > 1) {
  10524.             a_ptr[0] = m_xarg[maclvl];
  10525.             m_xarg[maclvl-2] = m_xarg[maclvl];
  10526.             a_dim[0] = n_xarg[maclvl];
  10527.             n_xarg[maclvl-2] = n_xarg[maclvl];
  10528.             macargc[maclvl-2] = n_xarg[maclvl];
  10529.         }
  10530.     }
  10531.     return(1);                  /* Internal command - don't change success */
  10532. }
  10533. #endif /* NOSPL */
  10534.  
  10535. #ifndef NOSPL
  10536. /*
  10537.   Do the GOTO and [_]FORWARD commands.
  10538.   s = Label to search for, cx = function code: XXGOTO, XXFWD, or XXXFWD.
  10539. */
  10540.  
  10541. int
  10542. dogoto(s, cx) char *s; int cx; {
  10543.     int i, j, x, y, z, bc;
  10544.     int empty = 0, stopflg = 0;
  10545.     char * cmd;                         /* Name of this command */
  10546.     char tmplbl[LBLSIZ+1], *lp;            /* Current label from command stream */
  10547.     char tmp2[LBLSIZ+1];        /* SWITCH label conversion buffer */
  10548.     char tmp3[LBLSIZ+1];        /* Target label */
  10549.  
  10550.     stopflg = (cx == XXXFWD);           /* _FORWARD (used in SWITCH) */
  10551.     bc = 0;                             /* Brace counter */
  10552.  
  10553.     cmd = (cx == XXGOTO) ? "GOTO" : ((cx == XXFWD) ? "FORWARD" : "_FORWARD");
  10554.     if (!s) s = "";
  10555.     if (!*s) empty = 1;
  10556.  
  10557. #ifdef DEBUG
  10558.     if (deblog) {
  10559.         debug(F111,"GOTO command",cmd,cx);
  10560.         debug(F101,"GOTO cmdlvl","",cmdlvl);
  10561.         debug(F101,"GOTO maclvl","",maclvl);
  10562.         debug(F101,"GOTO tlevel","",tlevel);
  10563.         debug(F111,"GOTO target",s,empty);
  10564.     }
  10565. #endif /* DEBUG */
  10566.     debug(F110,cmd,s,0);
  10567.     x = ckstrncpy(tmp3+1,s,LBLSIZ);
  10568.     debug(F101,"GOTO target len","",x);
  10569.     debug(F101,"GOTO target at x","",s[x]);
  10570.     if (s[x]) {
  10571.     debug(F100,"GOTO target overflow","",0);
  10572.     printf("?GOTO target or SWITCH case label too long\n");
  10573.     if (stopflg) dostop();        /* If in SWITCH return to prompt */
  10574.     return(success = 0);
  10575.     }
  10576.     s = tmp3+1;
  10577.     if (*s != ':') {                    /* Make copy of label */
  10578.         tmp3[0] = ':';                  /* guaranteed to start with ":" */
  10579.         s--;
  10580.     }
  10581.     if (!stopflg && !empty) {
  10582.         if (s[1] == '.' || s[1] == SP || s[1] == NUL) {
  10583.             printf("?Bad label syntax - '%s'\n",s);
  10584.         if (stopflg) dostop();    /* If in SWITCH return to prompt */
  10585.             return(success = 0);
  10586.         }
  10587.     }
  10588.     if (cmdlvl == 0) {
  10589.         printf("?Sorry, %s only works in a command file or macro\n",cmd);
  10590.         return(success = 0);
  10591.     }
  10592.     y = strlen(s);                      /* y = length of target label */
  10593.     debug(F111,cmd,s,y);
  10594.  
  10595.     while (cmdlvl > 0) {                /* As long as not at top level... */
  10596.         if (cmdstk[cmdlvl].src == CMD_MD) { /* GOTO inside macro */
  10597.             int i, m, flag;
  10598.             char *xp, *tp;
  10599.  
  10600.             /* GOTO: rewind the macro; FORWARD: start at current position */
  10601.  
  10602.             lp = (cx == XXGOTO) ? macx[maclvl] : macp[maclvl];
  10603.             m = (int)strlen(lp);
  10604.             debug(F111,"GOTO in macro",lp,m);
  10605.  
  10606.             flag = 1;                   /* flag for valid label position */
  10607.             for (i = 0; i < m; i++,lp++) { /* search for label in macro body */
  10608.                 if (*lp == '{')         /* But only at this level */
  10609.                   bc++;                 /* Anything inside braces is off */
  10610.                 else if (*lp == '}')    /* limits. */
  10611.                   bc--;
  10612.                 if (stopflg && bc > 0)  /* This is good for SWITCH */
  10613.                   continue;             /* but interferes with WHILE, etc. */
  10614.                 if (*lp == ',') {
  10615.                     flag = 1;
  10616.                     continue;
  10617.                 }
  10618.                 if (flag) {             /* If in valid label position */
  10619.                     if (*lp == SP)      /* eat leading spaces */
  10620.                       continue;
  10621.                     if (*lp != ':') {   /* Look for label introducer */
  10622.                         flag = 0;       /* this isn't it */
  10623.                         continue;       /* keep looking */
  10624.                     }
  10625.                 }
  10626.                 if (!flag)              /* We don't have a label */
  10627.                   continue;             /*  so keep looking... */
  10628.                 xp = lp; tp = tmplbl;   /* Copy the label from the macro */
  10629.                 j = 0;                  /* to make it null-terminated */
  10630.                 while ((*tp = *xp)) {
  10631.                     if (j++ > LBLSIZ-1) { /* j = length of word from macro */
  10632.             printf("?GOTO target or SWITCH case label too long\n");
  10633.             if (stopflg) dostop(); /* Return to prompt */
  10634.             return(success = 0);
  10635.             }
  10636.                     if (!*tp || *tp == ',') /* Look for end of word */
  10637.                       break;
  10638.                     else tp++, xp++;    /* Next character */
  10639.                 }
  10640.                 *tp = NUL;              /* In case we stopped early */
  10641.                 /* Now do caseless string comparison, using longest length */
  10642.                 debug(F111,"macro GOTO label",s,y);
  10643.                 debug(F111,"macro target label",tmplbl,j);
  10644.                 if (stopflg) {          /* Allow variables as SWITCH labels */
  10645.                     int n = LBLSIZ - 1;
  10646.                     char * p = tmp2;
  10647.                     zzstring(tmplbl,&p,&n);
  10648.             if (n < 0) {
  10649.             printf("?GOTO target or SWITCH case label too long\n");
  10650.             if (stopflg) dostop(); /* Return to prompt */
  10651.             return(0);
  10652.             }
  10653.                     ckstrncpy(tmplbl,tmp2,LBLSIZ);
  10654.                 }
  10655.                 debug(F111,"GOTO s",s,y);
  10656.                 debug(F111,"GOTO tmplbl",tmplbl,j);
  10657.                 debug(F111,"GOTO empty",ckitoa(stopflg),empty);
  10658.  
  10659.                 if (empty) {           /* Empty target */
  10660.             z = (!strcmp(s,":") && /* String is empty */
  10661.              /* and Label is ":" or ":*"... */
  10662.              (!strcmp(tmplbl,":") || !strcmp(tmplbl,":*")))
  10663.             ? 0 : 1;
  10664.             debug(F111,"GOTO","A",z);
  10665.                 } else if (stopflg) {
  10666.                     z = ckmatch(tmplbl,s,inpcas[cmdlvl],1) ? 0 : 1;
  10667.             debug(F111,"GOTO","B",z);
  10668.                 } else {
  10669.                     z = (stopflg && inpcas[cmdlvl]) ?
  10670.                       strcmp(s,tmplbl) :
  10671.                         ckstrcmp(s,tmplbl,(y > j) ? y : j, 0);
  10672.             debug(F111,"GOTO","C",z);
  10673.                 }
  10674.                 if (!z) {
  10675.                     break;
  10676.                 } else if (stopflg &&
  10677.                          !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j, 0)) {
  10678.                     debug(F100,"GOTO DEFAULT","",0);
  10679.                     break;
  10680.                 } else {
  10681.                     flag = 0;
  10682.                 }
  10683.             }
  10684.             debug(F111,"GOTO macro i",cmd,i);
  10685.             debug(F111,"GOTO macro m",cmd,m);
  10686.             if (i >= m) {               /* Didn't find the label */
  10687.                 debug(F101,"GOTO failed cmdlvl","",cmdlvl);
  10688. #ifdef COMMENT
  10689.         /* MOVED TO AFTER POPCLVL ABOUT 20 LINES DOWN 5 AUG 2002 */
  10690.            if (stopflg)
  10691.                   return(0);
  10692. #endif /* COMMENT */
  10693.                 if ((maclvl > 0) &&
  10694.                        (m_arg[maclvl-1][0]) &&
  10695.                        (cmdstk[cmdlvl].src == CMD_MD) &&
  10696.                        (!strncmp(m_arg[maclvl-1][0],"_xif",4) ||
  10697.                         !strncmp(m_arg[maclvl-1][0],"_for",4) ||
  10698.                         !strncmp(m_arg[maclvl-1][0],"_swi",4) ||
  10699.                         !strncmp(m_arg[maclvl-1][0],"_whi",4))) {
  10700.                     dogta(XXPTA);       /* Restore args */
  10701.                     debug(F101,"GOTO in XIF/FOR/WHI/SWI popping","",cmdlvl);
  10702.                     popclvl();          /* Pop an extra level */
  10703.                 }
  10704.                 debug(F101,"GOTO popping","",cmdlvl);
  10705.                 if (!popclvl()) {       /* pop up to next higher level */
  10706.                     printf("?Label '%s' not found\n",s); /* if none */
  10707.                     return(0);          /* Quit */
  10708.                 } else if (stopflg) {    /* SWITCH no case label match */
  10709.             return(0);        /* and no DEFAULT lable. */
  10710.         } else {
  10711.             continue;        /* otherwise look again */
  10712.         }
  10713.             }
  10714.             debug(F110,"GOTO found macro label",tmplbl,0);
  10715.             macp[maclvl] = lp;          /* set macro buffer pointer */
  10716.             return(1);
  10717.         } else if (cmdstk[cmdlvl].src == CMD_TF) {
  10718.             x = 0;                      /* GOTO issued in take file */
  10719.             debug(F111,"GOTO in TAKE file",cmd,cx);
  10720.             if (cx == XXGOTO) {         /* If GOTO, but not FORWARD, */
  10721.                 rewind(tfile[tlevel]);  /* search file from beginning */
  10722.                 tfline[tlevel] = 0;
  10723.             }
  10724.             while (! feof(tfile[tlevel])) {
  10725. #ifdef COMMENT
  10726. /* This is wrong - it lets us jump to labels inside inferior blocks */
  10727.                 tfline[tlevel]++;
  10728.                 if (fgets(line,LINBUFSIZ,tfile[tlevel]) == NULL) /* Get line */
  10729. #else
  10730.                 if (getnct(line,LINBUFSIZ,tfile[tlevel],0) < 0)
  10731. #endif /* COMMENT */
  10732.                   break;                /* If no more, done, label not found */
  10733.                 lp = line;              /* Got line */
  10734.                 while (*lp == SP || *lp == HT)
  10735.                   lp++;                 /* Strip leading whitespace */
  10736.                 if (*lp != ':') continue; /* Check for label introducer */
  10737.                 while (*(lp+1) == SP) { /* Remove space between : and name */
  10738.                     *(lp+1) = ':';
  10739.                     lp++;               /* Strip leading whitespace */
  10740.                 }
  10741.                 tp = lp;                /* Get end of word */
  10742.                 j = 0;
  10743.                 while (*tp) {           /* And null-terminate it */
  10744.                     if (*tp < 33) {
  10745.                         *tp = NUL;
  10746.                         break;
  10747.                     } else tp++, j++;
  10748.                 }
  10749.                 if (!ckstrcmp(lp,s,(y > j) ? y : j,0)) { /* Caseless compare */
  10750.                     x = 1;              /* Got it */
  10751.                     break;              /* done. */
  10752.                 } else if (stopflg &&
  10753.                            !ckstrcmp(":default",tmplbl,(8 > j) ? 8 : j,0)) {
  10754.                     x = 1;
  10755.                     break;
  10756.                 }
  10757.             }
  10758.             if (x == 0) {               /* If not found, print message */
  10759.                 debug(F101,"GOTO failed at cmdlvl","",cmdlvl);
  10760.                 if (stopflg)
  10761.                   return(0);
  10762.                 if (!popclvl()) {       /* pop up to next higher level */
  10763.                     printf("?Label '%s' not found\n",s); /* if none */
  10764.                     return(0);          /* quit */
  10765.                 } else continue;        /* otherwise look again */
  10766.             }
  10767.             return(x);                  /* Send back return code */
  10768.         }
  10769.     }
  10770.     printf("?Stack problem in GOTO %s\n",s); /* Shouldn't see this */
  10771.     return(0);
  10772. }
  10773. #endif /* NOSPL */
  10774.  
  10775. /* Finish parsing and do the IF, XIF, and WHILE commands */
  10776.  
  10777. #ifndef NOSPL
  10778.  
  10779. /*  C H K V A R  --  Check (if it's a) Variable  */
  10780.  
  10781.  
  10782. #ifdef OLDCHKVAR
  10783. /*
  10784.   Crude and disgusting, but needed for OS/2, DOS, and Windows, where filenames
  10785.   have backslashes in them.  How do we know if a backslash in a filename is a
  10786.   directory separator, or if it's a Kermit backslash?  This routine does a
  10787.   rough syntax check of the next few characters and if it looks like it MIGHT
  10788.   be a variable, then it tries to evaluate it, and if the result is not empty,
  10789.   we say it's a variable, although sometimes it might not be -- some cases are
  10790.   truly ambiguous.  For example there might a DOS directory called \%a, and
  10791.   we also have a variable with the same name.  This is all for the sake of not
  10792.   having to tell PC users that they have to double all backslashes in file
  10793.   and directory names.
  10794. */
  10795. #else
  10796. /*
  10797.   Somewhat less crude & disgusting.  The previous method was nondeterministic
  10798.   and (worse) it interfered with macro argument passing.  So now we only
  10799.   check the syntax of backslash-items to see if they are variables, but we
  10800.   do NOT check their values.
  10801. */
  10802. #endif /* OLDCHKVAR */
  10803. /*
  10804.   Call with a string pointer pointing at the backslash of the purported
  10805.   variable.  Returns 1 if it has the syntax of a variable, 0 if not.
  10806. */
  10807. int
  10808. chkvar(s) char *s; {
  10809.     int z = 0;                          /* Return code - assume failure. */
  10810.     if (!s) s = "";                     /* Watch our for null pointers. */
  10811.     if (!*s) return(0);                 /* Empty arg so not a variable. */
  10812.     if (*s == CMDQ) {                   /* Object begins with backslash. */
  10813.         char c;
  10814.         c = s[1];                       /* Character following backslash. */
  10815.         if (c) {
  10816.             int t = 0;
  10817.             if (c == CMDQ)              /* Quoted backslash */
  10818.               return(1);
  10819.             c = (char) (islower(c) ? toupper(c) : c); /* Otherwise... */
  10820.             if (c == '%') {             /* Simple variable */
  10821. #ifdef OLDCHKVAR
  10822.                 t = 1;
  10823. #else
  10824.                 return(1);
  10825. #endif /* OLDCHKVAR */
  10826.             } else if (c == '&') {      /* Array */
  10827.                 if (!s[2]) return(0);
  10828.                 if (s[3] == '[')
  10829.                   t = ckindex("]",s,4,0,1);
  10830. #ifndef OLDCHKVAR
  10831.                 return((t > 0) ? 1 : 0);
  10832. #endif /* OLDCHKVAR */
  10833.             } else if (c == '$' ||      /* Environment variable */
  10834.                        c == 'V' ||      /* Built-in variable */
  10835.                        c == 'M') {      /* Macro name */
  10836.                 t = (s[2] == '(');
  10837. #ifndef OLDCHKVAR
  10838.                 return((t > 0) ? 1 : 0);
  10839. #endif /* OLDCHKVAR */
  10840.             } else if (c == 'F') {      /* Function reference */
  10841.                 /* Don't actually call it - it might have side effects */
  10842.                 int x;
  10843.                 if ((x = ckindex("(",s,3,0,1))) /* Just check syntax */
  10844.                   if ((x = ckindex(")",s,x,0,1)))
  10845.                     z = 1;
  10846.                 /* Insert a better syntax check here if necessary */
  10847.             }
  10848. #ifdef OLDCHKVAR
  10849.             if (t) {
  10850.                 t = 255;                /* This lets us test \v(xxx) */
  10851.                 lp = line;              /* and even \f...(xxx) */
  10852.                 zzstring(s,&lp,&t);     /* Evaluate it, whatever it is. */
  10853.                 t = strlen(line);       /* Get its length. */
  10854.                 debug(F111,"chkvar",line,t);
  10855.                 z = t > 0;              /* If length > 0, it's defined */
  10856.             }
  10857. #endif /* OLDCHKVAR */
  10858.         }
  10859.     }
  10860.     return(z);
  10861. }
  10862.  
  10863. /*  B O O L E X P  --  Evaluate a Boolean expression  */
  10864.  
  10865. #define BOOLLEN 1024
  10866. static char boolval[BOOLLEN];
  10867.  
  10868. int
  10869. boolexp(cx) int cx; {
  10870.     int x, y, z; char *s, *p;
  10871.     int parens = 0, pcount = 0, ecount = 0;
  10872.     char *q, *bx;
  10873.     struct FDB kw, nu;
  10874. #ifdef FNFLOAT
  10875.     struct FDB fl;
  10876.     CKFLOAT f1 = 0.0, f2 = 0.0;
  10877.     int f1flag = 0, f2flag = 0;
  10878. #endif /* FNFLOAT */
  10879. #ifdef OS2
  10880.     extern int keymac;
  10881. #endif /* OS2 */
  10882.  
  10883.     not = 0;                            /* Flag for whether "NOT" was seen */
  10884.     z = 0;                              /* Initial IF condition */
  10885.     ifargs = 0;                         /* Count of IF condition words */
  10886.     bx = boolval;                       /* Initialize boolean value */
  10887.     *bx = NUL;
  10888.  
  10889.   ifagain:
  10890.     cmfdbi(&kw,                         /* First FDB - command switches */
  10891.            _CMKEY,                      /* fcode */
  10892.            "Number, numeric-valued variable, Boolean expression, or keyword",
  10893.            "",                          /* default */
  10894.            "",                          /* addtl string data */
  10895.            nif,                         /* addtl numeric data 1: tbl size */
  10896.            0,                           /* addtl numeric data 2: 4 = silent */
  10897.            xxstring,                    /* Processing function */
  10898.            iftab,                       /* Keyword table */
  10899.            &nu                          /* Pointer to next FDB */
  10900.            );
  10901.     cmfdbi(&nu,                         /* 2nd FDB - An integer */
  10902.            _CMNUM,                      /* fcode */
  10903.            "",                          /* hlpmsg */
  10904.            "",                          /* Default */
  10905.            "",                          /* addtl string data */
  10906.            0,
  10907.            0,
  10908.            xxstring,
  10909.            NULL,
  10910. #ifdef FNFLOAT
  10911.            &fl
  10912. #else
  10913.            NULL
  10914. #endif /* FNFLOAT */
  10915.            );
  10916. #ifdef FNFLOAT
  10917.     cmfdbi(&fl,                         /* A floating-point number */
  10918.            _CMFLD,                      /* fcode */
  10919.            "",                          /* hlpmsg */
  10920.            "",                          /* default */
  10921.            "",                          /* addtl string data */
  10922.            0,                           /* addtl numeric data 1 */
  10923.            0,                           /* addtl numeric data 2 */
  10924.            xxstring,
  10925.            NULL,
  10926.            NULL
  10927.            );
  10928. #endif /* FNFLOAT */
  10929.     x = cmfdb(&kw);                     /* Parse a keyword or a number */
  10930.     debug(F111,"boolval cmfdb","",x);
  10931.     if (x < 0) {
  10932.         if (x == -3)
  10933.           x = -2;
  10934.         return(x);
  10935.     }
  10936.     debug(F111,"boolval switch","",cmresult.fcode);
  10937.     switch (cmresult.fcode) {           /* What did we get? */
  10938. #ifdef FNFLOAT
  10939.       case _CMFLD:                      /* A "field" */
  10940.         if (isfloat(cmresult.sresult,0)) { /* A floating-point number? */
  10941.             f1 = floatval;              /* Yes, get its value */
  10942.             f1flag = 1;                 /* remember we did this */
  10943.             ifc = 9999;                 /* Set special "if-code" */
  10944.         } else
  10945.           return(-2);
  10946. #endif /* FNFLOAT */
  10947.       case _CMNUM:                      /* A number... */
  10948.         ifc = 9999;                     /* Set special "if-code" */
  10949.         break;
  10950.       case _CMKEY:                      /* A keyword */
  10951.         ifc = cmresult.nresult;         /* Get if-code */
  10952.         break;
  10953.       default:
  10954.         return(-2);
  10955.     }
  10956.     switch (ifc) {                      /* set z = 1 for true, 0 for false */
  10957.       case 9999:                        /* Number */
  10958. #ifdef FNFLOAT
  10959.         if (f1flag) {
  10960.             z = (f1 == 0.0) ? 0 : 1;
  10961.         } else
  10962. #endif /* FNFLOAT */
  10963.         z = (cmresult.nresult == 0) ? 0 : 1;
  10964.         break;
  10965.       case XXIFLP:                      /* Left paren */
  10966.         if (pcount == 0 && ifargs > 0)
  10967.           return(-2);
  10968.         parens = 1;
  10969.         pcount++;
  10970.         ifargs++;
  10971.         *bx++ = '(';
  10972.         goto ifagain;
  10973.       case XXIFRP:                      /* Right paren */
  10974.         if (!parens)
  10975.           return(-2);
  10976.         if (--pcount < 0)
  10977.           return(-2);
  10978.         ifargs++;
  10979.         *bx++ = ')';
  10980.         *bx = NUL;
  10981.         if (pcount == 0)
  10982.           goto ifend;
  10983.         goto ifagain;
  10984.       case XXIFAN:                      /* AND (&&) */
  10985.         ifargs++;
  10986.         if (!ecount)
  10987.           return(-2);
  10988.         *bx++ = '&';
  10989.         goto ifagain;
  10990.       case XXIFOR:                      /* OR (||) */
  10991.         ifargs++;
  10992.         if (!ecount)
  10993.           return(-2);
  10994.         *bx++ = '|';
  10995.         goto ifagain;
  10996.       case XXIFNO:                      /* IF NOT [ NOT [ NOT ... ] ] */
  10997.         if (bx > boolval) {             /* evala() doesn't like cascaded */
  10998.             if (*(bx-1) == '!') {       /* unary operators... */
  10999.                 *(bx-1) = NUL;          /* So here, two wrongs make a right. */
  11000.                 bx--;
  11001.             } else {
  11002.                 *bx++ = '!';
  11003.             }
  11004.         } else {
  11005.             *bx++ = '!';
  11006.         }
  11007.         ifargs++;
  11008.         goto ifagain;
  11009.       case XXIFTR:                      /* IF TRUE */
  11010.         z = 1;
  11011.         debug(F101,"if true","",z);
  11012.         ifargs += 1;
  11013.         break;
  11014.       case XXIFNT:                      /* IF FALSE */
  11015.         z = 0;
  11016.         debug(F101,"if true","",z);
  11017.         ifargs += 1;
  11018.         break;
  11019.       case XXIFSU:                      /* IF SUCCESS */
  11020.         z = ( success != 0 ) ? 1 : 0;
  11021.         debug(F101,"if success","",z);
  11022.         ifargs += 1;
  11023.         break;
  11024.       case XXIFFA:                      /* IF FAILURE */
  11025.         z = ( success == 0 ) ? 1 : 0;
  11026.         debug(F101,"if failure","",z);
  11027.         ifargs += 1;
  11028.         break;
  11029.  
  11030.       case XXIFDE:                      /* IF DEFINED */
  11031.         if ((x = cmfld("Macro or variable name","",&s,NULL)) < 0)
  11032.           return((x == -3) ? -2 : x);
  11033.  
  11034.         if (*s == CMDQ) {
  11035.             char * lp;
  11036.             char line[256];
  11037.             int t, x;
  11038.             if (*(s+1) == 'f' || *(s+1) == 'F') { /* Built-in function */
  11039.                 extern struct keytab fnctab[];
  11040.                 extern int nfuncs;
  11041.                 ckstrncpy(line,s+2,256);
  11042.                 if (line[0]) {
  11043.                     lp = ckstrchr(line,'(');
  11044.                     if (lp) *lp = NUL;
  11045.                     x = lookup(fnctab,line,nfuncs,&t);
  11046.                     z = x > -1;
  11047.                 }
  11048.                 debug(F111,"if defined function",line,z);
  11049.             } else if (*(s+1) == 'v' || *(s+1) == 'V') { /* 8.0.200 */
  11050.                 extern struct keytab vartab[];
  11051.                 extern int nvars;
  11052.                 z = 0;
  11053.                 if (*(s+2) == '(') {
  11054.                     ckstrncpy(line,s+3,256);
  11055.                     if (line[0]) {
  11056.                         lp = ckstrchr(line,')');
  11057.                         if (lp) *lp = NUL;
  11058.                         x = lookup(vartab,line,nvars,&t);
  11059.                         z = x > -1;
  11060.             if (z) {    /* 8.0.203 */
  11061.                 int t;    /* It must have a value to succeed */
  11062.                 t = 255;    /* as in C-Kermit 6.0 and 7.0 */
  11063.                 lp = line;    /* (this was broken in 8.0.200-201) */
  11064.                 zzstring(s,&lp,&t);
  11065.                 t = strlen(line);
  11066.                 z = line[0] ? 1 : 0;
  11067.             }
  11068.                     }
  11069.                 }
  11070.                 debug(F111,"if defined variable",line,z);
  11071.             } else {
  11072.                 z = chkvar(s);          /* Starts with backslash */
  11073.                 if (z > 0) {            /* Yes... */
  11074.                     t = 255;            /* than buffer so if zzstring fails  */
  11075.                     lp = line;          /* check for that -- overflow means */
  11076.                     line[0] = NUL;      /* the quantity is defined. */
  11077.                     x = zzstring(s,&lp,&t);
  11078.                     if ((x < 0 && t != 255) || !line[0])
  11079.                       z = 0;
  11080.                     debug(F111,"if defined zzstring",line,z);
  11081.                     debug(F101,"if defined zzstring t","",t);
  11082.                 }
  11083.             }
  11084.         } else {
  11085.             z = (mxlook(mactab,s,nmac) > -1); /* Look for exact match */
  11086.         }
  11087.         debug(F111,"if defined final",s,z);
  11088.         ifargs += 2;
  11089.         break;
  11090.  
  11091.       case XXIFDC: {                    /* IF DECLARED */
  11092.           char * lp;
  11093.           char line[32];
  11094.           int j, k, t, x;
  11095.           if ((x = cmfld("Array name","",&s,NULL)) < 0)
  11096.             return((x == -3) ? -2 : x);
  11097.           if (*s == CMDQ) {
  11098.               if (*(s+1) != '&') {
  11099.                   t = 31;
  11100.                   lp = line;
  11101.                   line[0] = NUL;
  11102.                   x = zzstring(s,&lp,&t);
  11103.                   s = line;
  11104.               }
  11105.           }
  11106.           z = 0;
  11107.           if ((x = arraybounds(s,&j,&k)) > -1) {
  11108.               if (a_ptr[x]) {
  11109.                   if (j < 1)
  11110.                     z = 1;
  11111.                   else if (j <= a_dim[x])
  11112.                     z = 1;
  11113.                   if (z == 1 && k > a_dim[x])
  11114.                     z = 0;
  11115.               }
  11116.           }
  11117.           break;
  11118.       }
  11119.       case XXIFBG:                      /* IF BACKGROUND */
  11120.       case XXIFFG:                      /* IF FOREGROUND */
  11121.         bgchk();                        /* Check background status */
  11122.         if (ifc == XXIFFG)              /* Foreground */
  11123.           z = pflag ? 1 : 0;
  11124.         else z = pflag ? 0 : 1;         /* Background */
  11125.         ifargs += 1;
  11126.         break;
  11127.  
  11128.       case XXIFCO:                      /* IF COUNT */
  11129.         z = ( --count[cmdlvl] > 0 );
  11130.         if (cx == XXWHI) count[cmdlvl] += 2; /* Don't ask... */
  11131.         debug(F101,"if count","",z);
  11132.         ifargs += 1;
  11133.         break;
  11134.  
  11135.       case XXIFEX:                      /* IF EXIST */
  11136. #ifdef CK_TMPDIR
  11137.       case XXIFDI:                      /* IF DIRECTORY */
  11138. #endif /* CK_TMPDIR */
  11139.       case XXIFAB:                      /* IF ABSOLUTE */
  11140.       case XXIFLN:            /* IF LINK */
  11141.         if ((x = cmfld(
  11142.                        ((ifc == XXIFDI) ? "Directory name" : "File"),
  11143.                        "",&s,
  11144. #ifdef OS2
  11145.                        NULL             /* This allows \'s in filenames */
  11146. #else
  11147.                        xxstring
  11148. #endif /* OS2 */
  11149.                        )) < 0) {
  11150.             if (x == -3) {
  11151.                 extern int cmflgs;
  11152.                 if (cmflgs == 1) {
  11153.                     printf("?File or directory name required\n");
  11154.                     return(-9);
  11155.                 }
  11156.             } else return(x);
  11157.         }
  11158.         s = brstrip(s);
  11159. #ifdef UNIX
  11160.     if (ifc == XXIFLN) {
  11161.         z = isalink(s);
  11162.     } else
  11163. #endif    /* UNIX */
  11164.         if (ifc == XXIFAB) {
  11165.             z = isabsolute(s);
  11166.         } else if (ifc == XXIFEX) {
  11167.             z = (zgetfs(s) > -1L);
  11168.             debug(F101,"if exist 1","",z);
  11169. #ifdef OS2
  11170.             if (!z) {                   /* File not found. */
  11171.                 int t;                  /* Try expanding variables */
  11172.                 t = LINBUFSIZ-1;        /* and looking again. */
  11173.                 lp = line;
  11174.                 zzstring(s,&lp,&t);
  11175.                 s = line;
  11176.                 z = ( zchki(s) > -1L );
  11177.                 debug(F101,"if exist 2","",z);
  11178.             }
  11179. #endif /* OS2 */
  11180. #ifdef CK_TMPDIR
  11181.         } else {
  11182. #ifdef VMS
  11183.             z = (zchki(s) == -2)
  11184. #else
  11185. /* Because this doesn't catch $DISK1:[FOO]BLAH.DIR;1 */
  11186.             z = isdir(s)
  11187. #ifdef OS2
  11188.               || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  11189. #endif /* OS2 */
  11190. #endif /* VMS */
  11191.               ;
  11192.             debug(F101,"if directory 1","",z);
  11193.  
  11194.             if (!z) {            /* File not found. */
  11195.                 int t;                  /* Try expanding variables */
  11196.                 t = LINBUFSIZ-1;        /* and looking again. */
  11197.                 lp = line;
  11198.                 zzstring(s,&lp,&t);
  11199.                 s = line;
  11200.                 z = isdir(s)
  11201. #ifdef OS2
  11202.                   || (isalpha(s[0]) && s[1] == ':' && s[2] == NUL)
  11203. #endif /* OS2 */
  11204.                     ;
  11205.                 debug(F101,"if directory 2","",z);
  11206.             }
  11207. #endif /* CK_TMPDIR */
  11208.         }
  11209.         ifargs += 2;
  11210.         break;
  11211.  
  11212.       case XXIFEQ:                      /* IF EQUAL (string comparison) */
  11213.       case XXIFLL:                      /* IF Lexically Less Than */
  11214.       case XXIFLG:                      /* If Lexically Greater Than */
  11215.         if ((x = cmfld("first word or variable name","",&s,xxstring)) < 0) {
  11216.             if (x == -3) {
  11217.                 printf("?Text required\n");
  11218.                 return(-9);
  11219.             } else return(x);
  11220.         }
  11221.         s = brstrip(s);                 /* Strip braces */
  11222.         x = (int)strlen(s);
  11223.         if (x > LINBUFSIZ-1) {
  11224.             printf("?IF: strings too long\n");
  11225.             return(-2);
  11226.         }
  11227.         lp = line;                      /* lp points to first string */
  11228.         ckstrncpy(line,s,LINBUFSIZ);
  11229.         if ((y = cmfld("second word or variable name","",&s,xxstring)) < 0) {
  11230.             if (y == -3) {
  11231.                 printf("?Text required\n");
  11232.                 return(-9);
  11233.             } else return(y);
  11234.         }
  11235.         s = brstrip(s);
  11236.         y = (int)strlen(s);
  11237.         if (x + y + 2 > LINBUFSIZ) {
  11238.             printf("?IF: strings too long\n");
  11239.             return(-2);
  11240.         }
  11241.         tp = lp + x + 2;                /* tp points to second string */
  11242.         strcpy(tp,s);                   /* safe (checked) */
  11243.         x = ckstrcmp(lp,tp,-1,inpcas[cmdlvl]); /* Use longest length */
  11244.         switch (ifc) {
  11245.           case XXIFEQ:                  /* IF EQUAL (string comparison) */
  11246.             z = (x == 0);
  11247.             break;
  11248.           case XXIFLL:                  /* IF Lexically Less Than */
  11249.             z = (x < 0);
  11250.             break;
  11251.           case XXIFLG:                  /* If Lexically Greater Than */
  11252.             z = (x > 0);
  11253.             break;
  11254.         }
  11255.         debug(F101,"IF EQ result","",z);
  11256.         ifargs += 3;
  11257.         break;
  11258.  
  11259.       case XXIFVE:                      /* IF VERSION */
  11260.       case XXIFAE:                      /* IF (arithmetically) = */
  11261.       case XXIFNQ:                      /* IF != (not arithmetically equal) */
  11262.       case XXIFLT:                      /* IF <  */
  11263.       case XXIFLE:                      /* IF <= */
  11264.       case XXIFGE:                      /* IF >= */
  11265.       case XXIFGT: {                    /* IF >  */
  11266.  
  11267.       /* July 2006 - converted to use CK_OFF_T rather than int to */
  11268.           /* allow long integers on platforms that have ck_off_t > 32 bits */
  11269.       int xx;
  11270.       CK_OFF_T n1 = (CK_OFF_T)0, n2 = (CK_OFF_T)0;
  11271.         if (ifc == XXIFVE) {
  11272.             n1 = (CK_OFF_T) vernum;
  11273.         } else {
  11274.             x = cmfld("first number or variable name","",&s,xxstring);
  11275.             if (x == -3) {
  11276.                 printf("?Quantity required\n");
  11277.                 return(-9);
  11278.             }
  11279.             if (x < 0) return(x);
  11280.             debug(F101,"xxifgt cmfld","",x);
  11281.             ckstrncpy(line,s,LINBUFSIZ);
  11282.             lp = brstrip(line);
  11283.             debug(F110,"xxifgt exp1",lp,0);
  11284.  
  11285. /* The following bit is for compatibility with old versions of MS-DOS Kermit */
  11286.  
  11287.             if (!ckstrcmp(lp,"count",5,0)) {
  11288.                 n1 = (CK_OFF_T)count[cmdlvl];
  11289.             } else if (!ckstrcmp(lp,"version",7,0)) {
  11290.                 n1 = (CK_OFF_T) vernum;
  11291.             } else if (!ckstrcmp(lp,"argc",4,0)) {
  11292.                 n1 = (CK_OFF_T) macargc[maclvl];
  11293.             } else {
  11294.  
  11295. /* End of compatibility bit */
  11296.  
  11297. #ifdef FNFLOAT
  11298.                 if (isfloat(lp,0) > 1) { /* Allow floating-point comparisons */
  11299.                     f1 = floatval;
  11300.                     f1flag = 1;
  11301.                 } else
  11302. #endif /* FNFLOAT */
  11303.                   if (chknum(lp)) {
  11304.                       n1 = ckatofs(lp);
  11305.                   } else {              /* Check for arithmetic expression */
  11306.                       q = evala(lp);    /* cmnum() does this but ... */
  11307.                       if (chknum(q)) {    /* we're not using cmnum(). */
  11308.               n1 = ckatofs(q);
  11309.               } else {
  11310.               printf("?Value not numeric - %s", lp);
  11311.               return(-9);
  11312.               }
  11313.                   }
  11314.             }
  11315.         }
  11316.         y = cmfld("number or variable name","",&s,xxstring);
  11317.         if (y == -3) {
  11318.             printf("?Quantity required\n");
  11319.             return(-9);
  11320.         }
  11321.         if (y < 0) return(y);
  11322.         s = brstrip(s);
  11323.         if (!*s) return(-2);
  11324.         if (ifc == XXIFVE) {
  11325.             tp = line;
  11326.         } else {
  11327.             x = (int)strlen(lp);
  11328.             tp = line + x + 2;
  11329.         }
  11330.         ckstrncpy(tp,s,LINBUFSIZ-x-2);
  11331.         debug(F110,"xxifgt exp2",tp,0);
  11332.         if (!ckstrcmp(tp,"count",5,0)) {
  11333.             n2 = (CK_OFF_T) count[cmdlvl];
  11334.         } else if (!ckstrcmp(tp,"version",7,0)) {
  11335.             n2 = (CK_OFF_T) vernum;
  11336.         } else if (!ckstrcmp(tp,"argc",4,0)) {
  11337.             n2 = (CK_OFF_T) macargc[maclvl];
  11338.         } else {
  11339. #ifdef FNFLOAT
  11340.             if (isfloat(tp,0) > 1) {
  11341.                 f2 = floatval;
  11342.                 f2flag = 1;
  11343.             } else
  11344. #endif /* FNFLOAT */
  11345.             if (chknum(tp)) {
  11346.                 n2 = ckatofs(tp);
  11347.             } else {
  11348.                 q = evala(tp);
  11349.         if (chknum(q)) {    /* we're not using cmnum(). */
  11350.             n2 = ckatofs(q);
  11351.         } else {
  11352.             printf("?Value not numeric - %s", tp);
  11353.             return(-9);
  11354.         }
  11355.             }
  11356.         }
  11357.         xx = (ifc == XXIFVE) ? XXIFGE : ifc;
  11358.  
  11359. #ifdef FNFLOAT
  11360.         if (f1flag && !f2flag) {
  11361.             f2 = (CKFLOAT)n2;
  11362.             f2flag = 1;
  11363.         }
  11364.         if (f2flag && !f1flag)
  11365.           f1 = (CKFLOAT)n1;
  11366.         if (f1flag)
  11367.           z = ((f1 <  f2 && xx == XXIFLT)
  11368.                || (f1 != f2 && xx == XXIFNQ)
  11369.                || (f1 <= f2 && xx == XXIFLE)
  11370.                || (f1 == f2 && xx == XXIFAE)
  11371.                || (f1 >= f2 && xx == XXIFGE)
  11372.                || (f1 >  f2 && xx == XXIFGT));
  11373.         else
  11374. #endif /* FNFLOAT */
  11375.           z = ((n1 <  n2 && xx == XXIFLT)
  11376.                || (n1 != n2 && xx == XXIFNQ)
  11377.                || (n1 <= n2 && xx == XXIFLE)
  11378.                || (n1 == n2 && xx == XXIFAE)
  11379.                || (n1 >= n2 && xx == XXIFGE)
  11380.                || (n1 >  n2 && xx == XXIFGT));
  11381.         debug(F101,"xxifge z","",z);
  11382.         if (ifc == XXIFVE)
  11383.           ifargs += 2;
  11384.         else
  11385.           ifargs += 3;
  11386.         break;
  11387.       }
  11388.  
  11389.       case XXIFNU:                      /* IF NUMERIC */
  11390.         x = cmfld("variable name or constant","",&s,NULL);
  11391.         if (x == -3) {
  11392.             extern int cmflgs;
  11393.             if (cmflgs == 1) {
  11394.                 printf("?Quantity required\n");
  11395.                 return(-9);
  11396.             }
  11397.         } else if (x < 0)
  11398.           return(x);
  11399.         x = LINBUFSIZ-1;
  11400.         lp = line;
  11401.         zzstring(s,&lp,&x);
  11402.         lp = line;
  11403.         debug(F110,"xxifnu quantity",lp,0);
  11404.         z = chknum(lp);
  11405. #ifdef COMMENT
  11406. /*
  11407.   This works, but it's not wise -- IF NUMERIC is mostly used to see if a
  11408.   string really does contain only numeric characters.  If they want to force
  11409.   evaluation, they can use \feval() on the argument string.
  11410. */
  11411.         if (!z) {                       /* Not a number */
  11412.             x_ifnum = 1;                /* Avoid "eval" error messages */
  11413.             q = evala(lp);              /* Maybe it's an expression */
  11414.             z = chknum(q);              /* that evaluates to a number */
  11415.             x_ifnum = 0;                /* Put eval messages back to normal */
  11416.             if (z) debug(F110,"xxifnu exp",lp,0);
  11417.         }
  11418. #endif /* COMMENT */
  11419.         debug(F101,"xxifnu chknum","",z);
  11420.         ifargs += 2;
  11421.         break;
  11422.  
  11423. #ifdef ZFCDAT
  11424.       case XXIFNE: {                    /* IF NEWER */
  11425.         char d1[20], * d2;              /* Buffers for 2 dates */
  11426.         if ((z = cmifi("First file","",&s,&y,xxstring)) < 0)
  11427.           return(z);
  11428.         ckstrncpy(d1,zfcdat(s),20);
  11429.         if ((z = cmifi("Second file","",&s,&y,xxstring)) < 0)
  11430.           return(z);
  11431.         d2 = zfcdat(s);
  11432.         if ((int)strlen(d1) != 17 || (int)strlen(d2) != 17) {
  11433.             printf("?Failure to get file date\n");
  11434.             return(-9);
  11435.         }
  11436.         debug(F110,"xxifnewer d1",d1,0);
  11437.         debug(F110,"xxifnewer d2",d2,0);
  11438.         z = (strcmp(d1,d2) > 0) ? 1 : 0;
  11439.         debug(F101,"xxifnewer","",z);
  11440.         ifargs += 2;
  11441.         break;
  11442.       }
  11443. #endif /* ZFCDAT */
  11444.  
  11445. #ifdef CK_IFRO
  11446.       case XXIFRO:                      /* REMOTE-ONLY advisory */
  11447.         ifargs++;
  11448. #ifdef NOLOCAL
  11449.         z = 1;
  11450. #else
  11451.         z = remonly;
  11452. #endif /* NOLOCAL */
  11453.         break;
  11454. #endif /* CK_IFRO */
  11455.  
  11456.       case XXIFAL:                      /* ALARM */
  11457.         ifargs++;
  11458.         debug(F101,"IF ALARM ck_alarm","",ck_alarm);
  11459.         debug(F110,"IF ALARM alrm_date",alrm_date,0);
  11460.         debug(F110,"IF ALARM alrm_time",alrm_time,0);
  11461.  
  11462.         if (ck_alarm < 1L || alrm_date[0] < '0' || alrm_time[0] < '0') {
  11463.             z = 0;                      /* ALARM not SET */
  11464.             break;                      /* so IF ALARM fails */
  11465.         }
  11466.         /* Get current date and time */
  11467.         ckstrncpy(tmpbuf,ckcvtdate("",1),TMPBUFSIZ);
  11468.         s = tmpbuf;
  11469.         s[8] = NUL;
  11470.         z = (int) strncmp(tmpbuf,alrm_date,8); /* Compare dates */
  11471.         debug(F101,"IF ALARM date z","",z);
  11472.         if (z == 0) {                   /* Dates are the same */
  11473.             /* Compare times */
  11474.             z = (tod2sec(tmpbuf+9) >= atol(alrm_time)) ? 1 : -1;
  11475.             debug(F101,"IF ALARM time z","",z);
  11476.         }
  11477.         tmpbuf[0] = NUL;                /* z >= 0 if alarm is passed */
  11478.         z = ((z >= 0) ? 1 : 0);         /* z <  0 otherwise */
  11479.         debug(F101,"IF ALARM final z","",z);
  11480.         break;
  11481.  
  11482.       case XXIFOP:                      /* IF OPEN */
  11483.         if ((x = cmkey(iotab,niot,"file or log","",xxstring)) < 0)
  11484.           return(x);
  11485.         if (x == 9999 || x == ZSTDIO) {
  11486.             bgchk();                    /* Check background status */
  11487.             z = pflag ? 1 : 0;
  11488.         } else if (x == 8888) {
  11489.             z = local ? ttchk() > -1 : 0;
  11490. #ifdef CKLOGDIAL
  11491.         } else if (x == 7777) {
  11492.             extern int dialog;
  11493.             z = dialog ? 1 : 0;
  11494. #endif /* CKLOGDIAL */
  11495.         } else
  11496.           z = (chkfn(x) > 0) ? 1 : 0;
  11497.         ifargs += 1;
  11498.         break;
  11499.  
  11500.       case XXIFSD:                      /* Started-From-Dialer */
  11501. #ifdef OS2
  11502.         z = StartedFromDialer;
  11503. #else
  11504.         z = 0;
  11505. #endif /* OS2 */
  11506.         break;
  11507.  
  11508.       case XXIFTM:                      /* Terminal-Macro */
  11509. #ifdef OS2
  11510.         z = cmdstk[cmdlvl].ccflgs & CF_KMAC;
  11511. #else
  11512.         z = 0;
  11513. #endif /* OS2 */
  11514.         break;
  11515.  
  11516.       case XXIFEM:                      /* Emulation is active */
  11517. #ifdef OS2
  11518.         z = 1;
  11519. #else
  11520.         z = 0;
  11521. #endif /* OS2 */
  11522.         break;
  11523.  
  11524.       case XXIFIK:                      /* Running as IKSD? */
  11525.         z = inserver;
  11526.         break;
  11527.  
  11528.       case XXIFTA:                      /* Connection is TAPI */
  11529.         z = 0;
  11530. #ifndef NODIAL
  11531. #ifdef CK_TAPI
  11532.         if (local && !network && tttapi)
  11533.           z = 1;
  11534. #endif /* CK_TAPI */
  11535. #endif /* NODIAL */
  11536.         break;
  11537.  
  11538.       case XXIFMA:                      /* IF MATCH */
  11539.         x = cmfld("String or variable","",&s,xxstring);
  11540.         if (x == -3) {
  11541.             extern int cmflgs;
  11542.             if (cmflgs == 1) {
  11543.                 printf("?String required\n");
  11544.                 return(-9);
  11545.             }
  11546.         } else if (x < 0)
  11547.           return(x);
  11548.         ckstrncpy(line,s,LINBUFSIZ);
  11549.         s = brstrip(line);
  11550.         debug(F110,"xxifma string",line,0);
  11551.         x = cmfld("Pattern","",&p,xxstring);
  11552.         if (x == -3) {
  11553.             extern int cmflgs;
  11554.             if (cmflgs == 1) {
  11555.                 printf("?Pattern required\n");
  11556.                 return(-9);
  11557.             }
  11558.         } else if (x < 0)
  11559.           return(x);
  11560.         ckstrncpy(tmpbuf,p,TMPBUFSIZ);
  11561.         p = brstrip(tmpbuf);
  11562.         debug(F110,"xxifma pattern",tmpbuf,0);
  11563.         z = ckmatch(p,s,inpcas[cmdlvl],1);
  11564.         break;
  11565.  
  11566.       case XXIFFL: {                    /* IF FLAG */
  11567.           extern int ooflag;
  11568.           z = ooflag;
  11569.           break;
  11570.       }
  11571.       case XXIFAV: {                    /* IF AVAILABLE */
  11572.           if ((x = cmkey(availtab,availtabn,"","",xxstring)) < 0)
  11573.             return(x);
  11574.           switch (x) {
  11575.             case AV_KRB4:
  11576.               z = ck_krb4_is_installed();
  11577.               break;
  11578.             case AV_KRB5:
  11579.               z = ck_krb5_is_installed();
  11580.               break;
  11581.             case AV_SRP:
  11582.               z = ck_srp_is_installed();
  11583.               break;
  11584.             case AV_SSL:
  11585.               z = ck_ssleay_is_installed();
  11586.               break;
  11587.             case AV_NTLM:
  11588.               z = ck_ntlm_is_installed();
  11589.               break;
  11590.             case AV_CRYPTO:
  11591.               z = ck_crypt_is_installed();
  11592.               break;
  11593.             case AV_SSH:
  11594.               z = ck_ssh_is_installed();
  11595.               break;
  11596.             default:
  11597.               z = 0;
  11598.           }
  11599.           break;
  11600.       }
  11601.       case XXIFAT:                      /* IF ASKTIMEOUT */
  11602.         z = asktimedout;
  11603.         break;
  11604.  
  11605.       case XXIFRD:                      /* IF READABLE */
  11606.       case XXIFWR:                      /* IF WRITEABLE */
  11607.         if ((x = cmfld("File or directory name",
  11608.                        "",
  11609.                        &s,
  11610. #ifdef OS2
  11611.                        NULL             /* This allows \'s in filenames */
  11612. #else
  11613.                        xxstring
  11614. #endif /* OS2 */
  11615.                        )) < 0) {
  11616.             if (x == -3) {
  11617.                 extern int cmflgs;
  11618.                 if (cmflgs == 1) {
  11619.                     printf("?File or directory name required\n");
  11620.                     return(-9);
  11621.                 }
  11622.             } else return(x);
  11623.         }
  11624.         s = brstrip(s);
  11625. /*
  11626.   zchk[io]() do not do what we want here for directories, so we set
  11627.   a global flag telling it to behave specially in this case.  Othewise
  11628.   we'd have to change the API and change all ck?fio.c modules accordingly.
  11629. */
  11630.         y = 0;                          /* Try-again control */
  11631. #ifdef OS2
  11632.   ifrdagain:
  11633. #endif /* OS2 */
  11634.         if (ifc == XXIFRD) {            /* IF READABLE */
  11635.             zchkid = 1;
  11636.             z = zchki(s) > -1;
  11637.             zchkid = 0;
  11638.         } else if (ifc == XXIFWR) {     /* IF WRITEABLE */
  11639.             zchkod = 1;
  11640.             z = zchko(s) > -1;
  11641.             zchkod = 0;
  11642.         }
  11643. #ifdef OS2
  11644.         if (!z && !y) {                 /* File not found. */
  11645.             int t;                      /* Try expanding variables */
  11646.             t = LINBUFSIZ-1;            /* and looking again. */
  11647.             lp = line;
  11648.             zzstring(s,&lp,&t);
  11649.             s = line;
  11650.             z = zchko(s) > -1;
  11651.             y++;
  11652.             goto ifrdagain;
  11653.         }
  11654. #endif /* OS2 */
  11655.         ifargs += 2;
  11656.         break;
  11657.       case XXIFQU:                      /* IF QUIET */
  11658.         z = quiet ? 1 : 0;
  11659.         debug(F101,"if quiet","",z);
  11660.         ifargs += 1;
  11661.         break;
  11662.  
  11663.       case XXIFWI:                      /* WILD */
  11664.         if ((x = cmfld("File specification","",&s,xxstring)) < 0) return(x);
  11665.         z = iswild(s);
  11666.         break;
  11667.  
  11668.       case XXIFCK:                      /* C-KERMIT */
  11669. #ifdef OS2
  11670.         z = 0;
  11671. #else
  11672.         z = 1;
  11673. #endif /* OS2 */
  11674.         break;
  11675.  
  11676.       case XXIFK9:                      /* K-95 */
  11677. #ifdef OS2
  11678.         z = 1;
  11679. #else
  11680.         z = 0;
  11681. #endif /* OS2 */
  11682.         break;
  11683.  
  11684.       case XXIFGU:                      /* GUI */
  11685. #ifdef KUI
  11686.         z = 1;
  11687. #else
  11688.         z = 0;
  11689. #endif /* KUI */
  11690.         break;
  11691.  
  11692.       case XXIFMS:                      /* MS-KERMIT */
  11693.         z = 0;
  11694.         break;
  11695.  
  11696.       case XXIFLO:                      /* IF LOCAL */
  11697.         z = local ? 1 : 0;
  11698.         break;
  11699.  
  11700.       case XXIFCM: {                    /* IF COMMAND */
  11701.           extern struct keytab cmdtab[];
  11702.           extern int ncmd;
  11703.           if ((x = cmfld("Word","",&s,xxstring)) < 0)
  11704.             return(x);
  11705.           z = lookup(cmdtab,s,ncmd,&y);
  11706.           z = (z == -2 || z > -1) ? 1 : 0;
  11707.           break;
  11708.       }
  11709. #ifdef CKFLOAT
  11710.       case XXIFFP:                      /* IF FLOAT */
  11711.         if ((x = cmfld("Number","",&s,xxstring)) < 0)
  11712.           if (x != -3)                  /* e.g. empty variable */
  11713.             return(x);
  11714.         z = isfloat(s,0);
  11715.         break;
  11716. #endif /* CKFLOAT */
  11717.  
  11718.       case XXIFKB:                      /* KBHIT */
  11719.         z = conchk();
  11720.         if (z < 0) z = 0;
  11721.         if (z > 1) z = 1;
  11722.         break;
  11723.  
  11724.       case XXIFKG: {                    /* KERBANG */
  11725.           extern int cfilef;
  11726. #ifdef COMMENT
  11727.           z = (xcmdsrc == 0) ? 0 : (cfilef && cmdlvl == 1);
  11728. #else
  11729.           z = (xcmdsrc == 0) ? 0 : (cfilef && tlevel == 0);
  11730. #endif    /* COMMENT */
  11731.           break;
  11732.       }
  11733.  
  11734.       default:                          /* Shouldn't happen */
  11735.         return(-2);
  11736.     } /* end of switch */
  11737.  
  11738.     if (z)
  11739.       *bx++ = '1';
  11740.     else
  11741.       *bx++ = '0';
  11742.     *bx = NUL;
  11743.     if (bx > boolval + BOOLLEN - 2) {
  11744.         printf("?Boolean expression too long");
  11745.         return(-9);
  11746.     }
  11747.     ecount++;                           /* Expression counter */
  11748.     debug(F101,"boolexp parens","",parens);
  11749.     debug(F101,"boolexp pcount","",pcount);
  11750.     if (parens && pcount > 0)
  11751.       goto ifagain;
  11752.  
  11753.   ifend:                                /* No more - done */
  11754.     *bx = NUL;
  11755.     z = atoi(evalx(boolval));
  11756.     debug(F111,"boolexp boolval",boolval,z);
  11757.     return(z);
  11758. }
  11759.  
  11760. /*  D O I F  --  Do the IF command  */
  11761.  
  11762. int
  11763. doif(cx) int cx; {
  11764.     int x, y, z; char *s, *p;
  11765.     char *q;
  11766. #ifdef OS2
  11767.     extern int keymac;
  11768. #endif /* OS2 */
  11769.  
  11770.     debug(F101,"doif cx","",cx);
  11771.  
  11772.     z = boolexp(cx);                    /* Evaluate the condition(s) */
  11773.     debug(F010,"doif cmdbuf",cmdbuf,0);
  11774.     debug(F101,"doif boolexp","",z);
  11775.     if (z < 0)
  11776.       return(z);
  11777.  
  11778.     if (cx == XXIF) {                   /* Allow IF to have XIF semantics. */
  11779.         char * p;
  11780.         p = cmpeek();
  11781.         if (!p) p = "";
  11782.         while (*p) {
  11783.             if (*p == SP || *p == HT)
  11784.               p++;
  11785.             else
  11786.               break;
  11787.         }
  11788.         if (*p == '{')
  11789.           cx = XXIFX;
  11790.     }
  11791.     switch (cx) {                       /* Separate handling for IF and XIF */
  11792.  
  11793.       case XXASSER:                     /* And ASSERT */
  11794.         if ((x = cmcfm()) < 0)
  11795.           return(x);
  11796.         return(success = z);
  11797.  
  11798.       case XXIF:                        /* This is IF... */
  11799.         ifcmd[cmdlvl] = 1;              /* We just completed an IF command */
  11800.         debug(F101,"doif condition","",z);
  11801.         if (z) {                        /* Condition is true */
  11802.             iftest[cmdlvl] = 1;         /* Remember that IF succeeded */
  11803.             if (maclvl > -1) {          /* In macro, */
  11804.                 pushcmd(NULL);          /* save rest of command. */
  11805.             } else if (tlevel > -1) {   /* In take file, */
  11806.                 debug(F100, "doif: pushing command", "", 0);
  11807.                 pushcmd(NULL);          /* save rest of command. */
  11808.             } else {                    /* If interactive, */
  11809.                 cmini(ckxech);          /* just start a new command */
  11810.                 printf("\n");           /* (like in MS-DOS Kermit) */
  11811.                 if (pflag) prompt(xxstring);
  11812.             }
  11813.         } else {                        /* Condition is false */
  11814.             iftest[cmdlvl] = 0;         /* Remember command failed. */
  11815.             if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)
  11816.               return(y);                /* Gobble up rest of line */
  11817.         }
  11818.         return(0);
  11819.  
  11820.       case XXIFX: {                     /* This is XIF (Extended IF) */
  11821.           char *p;
  11822.           char e[5];
  11823.           int i;
  11824.           if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  11825.             return(y);                  /* Get object command. */
  11826.           p = s;
  11827.           lp = line;
  11828.           debug(F110,"doif THEN part",s,-54);
  11829.           if (litcmd(&p,&lp,LINBUFSIZ - 1) < 0) { /* Quote THEN-part */
  11830.               return(-2);
  11831.           }
  11832.           debug(F111,"doif THEN part 2",line,z);
  11833.  
  11834.           while (*p == SP) p++;         /* Strip trailing spaces */
  11835.           ifcmd[cmdlvl] = 0;            /* Assume ELSE part in same line */
  11836.           iftest[cmdlvl] = z ? 1 : 0;
  11837.           if (*p) {                     /* At end? */
  11838.               if (!z) {                 /* No, use ELSE-part, if any */
  11839.                   for (i = 0; i < 4; i++) e[i] = *p++;
  11840.                   if (ckstrcmp(e,"else",4,0)) /* See if we have an ELSE */
  11841.                     return(-2);         /* Something else - error. */
  11842.                   debug(F010,"doif ELSE line 1",p,0);
  11843.                   while (*p == SP) p++; /* Skip spaces */
  11844.                   if (*p != '{') {      /* Brace ELSE part if necessary */
  11845.                       ckmakmsg(tmpbuf,TMPBUFSIZ,"{",p," }",NULL);
  11846.                       p = tmpbuf;
  11847.                       debug(F010,"doif ELSE line 2",p,0);
  11848.                   }
  11849.                   lp = line;            /* Write over THEN part... */
  11850.                   *lp = NUL;            /* with ELSE part. */
  11851.                   if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) {
  11852.                       return(-2);
  11853.                   }
  11854.                   while (*p == SP) p++; /* Strip trailing spaces */
  11855.                   if (*p) return(-2);   /* Should be nothing here. */
  11856.                   debug(F010,"doif ELSE line 3",line,0);
  11857.               }
  11858.           } else {                      /* At end, treat like an IF command */
  11859.               if (!z) line[0] = NUL;    /* Condition not true and no ELSE */
  11860.               ifcmd[cmdlvl] = 1;        /* Allow ELSE on next line */
  11861.               debug(F101,"IF condition","",z);
  11862.           }
  11863.           if (line[0]) {
  11864.               x = mlook(mactab,"_xif",nmac); /* Get index of "_xif" macro. */
  11865.               if (x < 0) {                      /* Not there? */
  11866.                   addmmac("_xif",xif_def);      /* Put it back. */
  11867.                   if (mlook(mactab,"_xif",nmac) < 0) { /* Look it up again. */
  11868.                       printf("?XIF macro gone!\n");
  11869.                       return(success = 0);
  11870.                   }
  11871.               }
  11872.               dodo(x,line,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  11873.           }
  11874.           return(0);
  11875.       }
  11876.       case XXWHI: {                     /* WHILE Command */
  11877.           p = cmdbuf;                   /* Capture IF condition */
  11878.           ifcond[0] = NUL;              /* from command buffer */
  11879.           while (*p == SP) p++;
  11880.           while (*p != SP) p++;
  11881.           ifcp = ifcond;
  11882.           ifcp += ckstrncpy(ifcp,"{ \\flit(if ( not ",IFCONDLEN);
  11883. #ifdef COMMENT
  11884. /*
  11885.   This doesn't work because it breaks on the first left brace, which does
  11886.   not necessarily start the command list, e.g. "while equal \%a {\35}".
  11887. */
  11888.           while (*p != '{' && *p != NUL) *ifcp++ = *p++;
  11889.           p = " ) goto _..bot) } ";
  11890.           while (*ifcp++ = *p++) ;
  11891. #else
  11892. /*
  11893.   The command parser sets cmbptr to the spot where it left off parsing in
  11894.   the command buffer.
  11895. */
  11896.           {
  11897.               extern char * cmbptr;
  11898.               if (cmbptr) {
  11899.                   while (p < cmbptr && *p != NUL)
  11900.                     *ifcp++ = *p++;
  11901.                   p = " ) goto _..bot) } ";
  11902.                   while ((*ifcp++ = *p++)) ;
  11903.               } else {
  11904.                   printf("?Internal error parsing WHILE condition\n");
  11905.                   return(-9);
  11906.               }
  11907.           }
  11908. #endif /* COMMENT */
  11909.  
  11910.           debug(F110,"WHILE cmd",ifcond,0);
  11911.  
  11912.           if ((y = cmtxt("Object command","",&s,NULL)) < 0)
  11913.             return(y);                  /* Get object command. */
  11914.           p = s;
  11915.           lp = line;
  11916.           if (litcmd(&p,&lp,LINBUFSIZ - 2) < 0) { /* Quote object command */
  11917.               return(-2);
  11918.           }
  11919.           debug(F101,"WHILE body",line,-54);
  11920.           if (line[0]) {
  11921.               char *p;
  11922.               x = mlook(mactab,"_while",nmac); /* index of "_while" macro. */
  11923.               if (x < 0) {              /* Not there? */
  11924.                   addmmac("_while",whil_def); /* Put it back. */
  11925.                   if (mlook(mactab,"_while",nmac) < 0) { /* Look it up again */
  11926.                       printf("?WHILE macro definition gone!\n");
  11927.                       return(success = 0);
  11928.                   }
  11929.               }
  11930.               p = malloc((int)strlen(ifcond) + (int)strlen(line) + 2);
  11931.               if (p) {
  11932.                   strcpy(p,ifcond);     /* safe (prechecked) */
  11933.                   strcat(p,line);       /* safe (prechecked) */
  11934.                   debug(F010,"WHILE dodo",p,0);
  11935.                   dodo(x,p,cmdstk[cmdlvl].ccflgs | CF_IMAC);
  11936.                   free(p);
  11937.                   p = NULL;
  11938.               } else {
  11939.                   printf("?Can't allocate storage for WHILE command");
  11940.                   return(success = 0);
  11941.               }
  11942.           }
  11943.           return(0);
  11944.       }
  11945.       default:
  11946.         return(-2);
  11947.     }
  11948. }
  11949. #endif /* NOSPL */
  11950.  
  11951. /* Set up a TAKE command file */
  11952.  
  11953. int
  11954. dotake(s) char *s; {
  11955. #ifndef NOSPL
  11956.     extern int tra_cmd;
  11957. #endif /* NOSPL */
  11958. #ifndef NOLOCAL
  11959. #ifdef OS2
  11960.     extern int term_io;
  11961.     int term_io_sav = term_io;
  11962. #endif /* OS2 */
  11963. #endif /* NOLOCAL */
  11964.     int slen;
  11965.  
  11966.     debug(F110,"dotake",s,0);
  11967.     if (!s) s = "";
  11968.     if (!*s) return(success = 0);
  11969.     slen = strlen(s);
  11970.     debug(F101,"dotake len","",slen);
  11971.  
  11972.     if ((tfile[++tlevel] = fopen(s,"r")) == NULL) {
  11973.         perror(s);
  11974.         debug(F110,"dotake fail",s,0);
  11975.         tlevel--;
  11976.         return(success = 0);
  11977.     } else {
  11978.         tfline[tlevel] = 0;             /* Line counter */
  11979. #ifdef VMS
  11980.         conres();                       /* So Ctrl-C will work */
  11981. #endif /* VMS */
  11982. #ifndef NOLOCAL
  11983. #ifdef OS2
  11984.         term_io = 0;                    /* Disable Terminal Emulator I/O */
  11985. #endif /* OS2 */
  11986. #endif /* NOLOCAL */
  11987. #ifndef NOSPL
  11988.         cmdlvl++;                       /* Entering a new command level */
  11989.         debug(F111,"CMD +F",s,cmdlvl);
  11990.         debug(F101,"dotake cmdlvl","",cmdlvl);
  11991.         debug(F101,"dotake tlevel","",tlevel);
  11992.         if (cmdlvl > CMDSTKL) {
  11993.             debug(F100,"dotake stack overflow","",0);
  11994.             cmdlvl--;
  11995.             debug(F111,"CMD*-F",s,cmdlvl);
  11996.             fclose(tfile[tlevel--]);
  11997.             printf("?TAKE files and/or DO commands nested too deeply\n");
  11998.             return(success = 0);
  11999.         }
  12000.         if (tfnam[tlevel]) {            /* Copy the filename */
  12001.             free(tfnam[tlevel]);
  12002.             tfnam[tlevel] = NULL;
  12003.         }
  12004.         if ((tfnam[tlevel] = malloc(strlen(s) + 1))) {
  12005.             strcpy(tfnam[tlevel],s);    /* safe */
  12006.         } else {
  12007.             printf("?Memory allocation failure\n");
  12008.             return(success = 0);
  12009.         }
  12010.         ifcmd[cmdlvl] = 0;              /* Set variables for this cmd file */
  12011.         iftest[cmdlvl] = 0;
  12012.         count[cmdlvl]  = count[cmdlvl-1];  /* Inherit this */
  12013.         intime[cmdlvl] = intime[cmdlvl-1]; /* Inherit this */
  12014.         inpcas[cmdlvl] = inpcas[cmdlvl-1]; /* Inherit this */
  12015.         takerr[cmdlvl] = takerr[cmdlvl-1]; /* Inherit this */
  12016.         merror[cmdlvl] = merror[cmdlvl-1]; /* Inherit this */
  12017.         xquiet[cmdlvl] = quiet;
  12018.         xvarev[cmdlvl] = vareval;
  12019.         xcmdsrc = CMD_TF;
  12020.         cmdstk[cmdlvl].src = CMD_TF;    /* Say we're in a TAKE file */
  12021.         cmdstk[cmdlvl].lvl = tlevel;    /* nested at this level */
  12022.         cmdstk[cmdlvl].ccflgs = cmdstk[cmdlvl-1].ccflgs;
  12023. #else
  12024.         takerr[tlevel] = takerr[tlevel-1]; /* Inherit this */
  12025. #endif /* NOSPL */
  12026.     }
  12027. #ifndef NOSPL
  12028.     if (tra_cmd)
  12029.       printf("[%d] +F: \"%s\"\n",cmdlvl,s);
  12030. #endif /* NOSPL */
  12031. #ifndef NOLOCAL
  12032. #ifdef OS2
  12033.     term_io = term_io_sav;
  12034. #endif /* OS2 */
  12035. #endif /* NOLOCAL */
  12036.     return(1);
  12037. }
  12038. #endif /* NOICP */
  12039.