home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc072.zip / ckuusr.c < prev    next >
C/C++ Source or Header  |  1989-02-02  |  36KB  |  1,236 lines

  1. char *userv = "User Interface 4E(060), 13 Jan 89";
  2.  
  3. /*  C K U U S R --  "User Interface" for Unix Kermit (Part 1)  */
  4.  
  5. /*
  6.  4E, support for Apollo Aegis, Data General added, July 87.
  7. */
  8. /*
  9.  Author: Frank da Cruz (fdc@cunixc.cc.columbia.edu, FDCCU@CUVMA.BITNET),
  10.  Columbia University Center for Computing Activities.
  11.  First released January 1985.
  12.  Copyright (C) 1985, 1989, Trustees of Columbia University in the City of New 
  13.  York.  Permission is granted to any individual or institution to use, copy, or
  14.  redistribute this software so long as it is not sold for profit, provided this
  15.  copyright notice is retained. 
  16. */
  17.  
  18. /*
  19.  The ckuusr module contains the terminal input and output functions for Unix
  20.  Kermit.  It includes a simple Unix-style command line parser as well as
  21.  an interactive prompting keyword command parser.  It depends on the existence
  22.  of Unix facilities like fopen, fgets, feof, (f)printf, argv/argc, etc.  Other
  23.  functions that are likely to vary among Unix implementations -- like setting
  24.  terminal modes or interrupts -- are invoked via calls to functions that are
  25.  defined in the system-dependent modules, ck?[ft]io.c.
  26.  
  27.  The command line parser processes any arguments found on the command line,
  28.  as passed to main() via argv/argc.  The interactive parser uses the facilities
  29.  of the cmd package (developed for this program, but usable by any program).
  30.  
  31.  Any command parser may be substituted for this one.  The only requirements
  32.  for the Kermit command parser are these:
  33.  
  34.  1. Set parameters via global variables like duplex, speed, ttname, etc.
  35.     See ckmain.c for the declarations and descriptions of these variables.
  36.  
  37.  2. If a command can be executed without the use of Kermit protocol, then
  38.     execute the command directly and set the variable sstate to 0. Examples
  39.     include 'set' commands, local directory listings, the 'connect' command.
  40.  
  41.  3. If a command requires the Kermit protocol, set the following variables:
  42.  
  43.     sstate                             string data
  44.       'x' (enter server mode)            (none)
  45.       'r' (send a 'get' command)         cmarg, cmarg2
  46.       'v' (enter receive mode)           cmarg2
  47.       'g' (send a generic command)       cmarg
  48.       's' (send files)                   nfils, cmarg & cmarg2 OR cmlist
  49.       'c' (send a remote host command)   cmarg
  50.  
  51.     cmlist is an array of pointers to strings.
  52.     cmarg, cmarg2 are pointers to strings.
  53.     nfils is an integer.    
  54.  
  55.     cmarg can be a filename string (possibly wild), or
  56.        a pointer to a prefabricated generic command string, or
  57.        a pointer to a host command string.
  58.     cmarg2 is the name to send a single file under, or
  59.        the name under which to store an incoming file; must not be wild.
  60.     cmlist is a list of nonwild filenames, such as passed via argv.
  61.     nfils is an integer, interpreted as follows:
  62.       -1: argument string is in cmarg, and should be expanded internally.
  63.        0: stdin.
  64.       >0: number of files to send, from cmlist.
  65.  
  66.  The screen() function is used to update the screen during file transfer.
  67.  The tlog() function maintains a transaction log.
  68.  The debug() function maintains a debugging log.
  69.  The intmsg() and chkint() functions provide the user i/o for interrupting
  70.    file transfers.
  71. */
  72.  
  73. /* Includes */
  74.  
  75. #include "ckcdeb.h"
  76. #include <stdio.h>
  77. #include <ctype.h>
  78. #ifndef AMIGA
  79. #include <signal.h>
  80. #endif
  81. #include "ckcker.h"
  82. #include "ckucmd.h"
  83. #include "ckuusr.h"
  84.  
  85. #ifdef datageneral
  86. #define fgets(stringbuf,max,fd) dg_fgets(stringbuf,max,fd) 
  87. #define fork() vfork()
  88. /* DG version 3.21 of C has bugs in the following routines, since they
  89.  * depend on /etc/passwd.  In the context where the routines are used,
  90.  * we don't need them anyway.
  91.  */
  92. #define getgid() -1
  93. #define getuid() -1
  94. #define geteuid() -1
  95. #endif
  96.  
  97. /* External Kermit Variables, see ckmain.c for description. */
  98.  
  99. extern int size, rpsiz, urpsiz, speed, local, 
  100.   server, displa, binary, parity, deblog, escape, xargc, flow,
  101.   turn, duplex, nfils, ckxech, pktlog, seslog, tralog, stdouf,
  102.   turnch, dfloc, keep, maxrps, warn, quiet, cnflg, tlevel;
  103.  
  104. extern char *versio, *protv, *ckxv, *ckzv, *fnsv, *connv, *dftty, *cmdv;
  105. extern char *dialv, *loginv;
  106. extern char *ckxsys, *ckzsys, *cmarg, *cmarg2, **xargv, **cmlist;
  107. extern char *DIRCMD, *PWDCMD, cmerrp[];
  108. extern CHAR sstate, ttname[];
  109. char *strcpy(), *getenv();
  110. #ifdef AMIGA
  111. char *getcwd();
  112. #endif
  113.  
  114. /* Declarations from cmd package */
  115.  
  116. extern char cmdbuf[];            /* Command buffer */
  117.  
  118. /* Declarations from ck?fio.c module */
  119.  
  120. extern char *SPACMD, *zhome();        /* Space command, home directory. */
  121. extern int backgrd;            /* Kermit executing in background */
  122.  
  123. /* The background flag is set by ckutio.c (via conint() ) to note whether */
  124. /* this kermit is executing in background ('&' on shell command line).    */
  125.  
  126.  
  127. /* Variables and symbols local to this module */
  128.  
  129. char line[CMDBL+10], *lp;        /* Character buffer for anything */
  130. char debfil[50];            /* Debugging log file name */
  131. char pktfil[50];            /* Packet log file name */
  132. char sesfil[50];            /* Session log file name */
  133. char trafil[50];            /* Transaction log file name */
  134.  
  135. int n,                    /* General purpose int */
  136.     cflg,                /* Command-line connect cmd given */
  137.     action,                /* Action selected on command line*/
  138.     repars,                /* Reparse needed */
  139.     cwdf = 0;                /* CWD has been done */
  140.  
  141. #define MAXTAKE 20            /* Maximum nesting of TAKE files */
  142. FILE *tfile[MAXTAKE];            /* File pointers for TAKE command */
  143.  
  144. char *homdir;                /* Pointer to home directory string */
  145. char cmdstr[100];            /* Place to build generic command */
  146.  
  147. /*  C M D L I N  --  Get arguments from command line  */
  148. /*
  149.  Simple Unix-style command line parser, conforming with 'A Proposed Command
  150.  Syntax Standard for Unix Systems', Hemenway & Armitage, Unix/World, Vol.1,
  151.  No.3, 1984.
  152. */
  153. cmdlin() {
  154.     char x;                /* Local general-purpose int */
  155.     cmarg = "";                /* Initialize globals */
  156.     cmarg2 = "";
  157.     action = cflg = 0;
  158.  
  159.     while (--xargc > 0) {        /* Go through command line words */
  160.     xargv++;
  161.     debug(F111,"xargv",*xargv,xargc);
  162.         if (**xargv == '-') {        /* Got an option (begins with dash) */
  163.         x = *(*xargv+1);        /* Get the option letter */
  164.         x = doarg(x);        /* Go handle the option */
  165.         if (x < 0) doexit(BAD_EXIT);
  166.         } else {            /* No dash where expected */
  167.         usage();
  168.         doexit(BAD_EXIT);
  169.     }
  170.     }
  171.     debug(F101,"action","",action);
  172.     if (!local) {
  173.     if ((action == 'g') || (action == 'r') ||
  174.         (action == 'c') || (cflg != 0))
  175.         fatal("-l and -b required");
  176.     }
  177.     if (*cmarg2 != 0) {
  178.     if ((action != 's') && (action != 'r') &&
  179.         (action != 'v'))
  180.         fatal("-a without -s, -r, or -g");
  181.     }
  182.     if ((action == 'v') && (stdouf) && (!local)) {
  183.         if (isatty(1))
  184.         fatal("unredirected -k can only be used in local mode");
  185.     }
  186.     if ((action == 's') || (action == 'v') ||
  187.         (action == 'r') || (action == 'x')) {
  188.     if (local) displa = 1;
  189.     if (stdouf) { displa = 0; quiet = 1; }
  190.     }
  191.  
  192.     if (quiet) displa = 0;        /* No display if quiet requested */
  193.  
  194.     if (cflg) {
  195.     conect();            /* Connect if requested */
  196.     if (action == 0) {
  197.         if (cnflg) conect();    /* And again if requested */
  198.         doexit(GOOD_EXIT);        /* Then exit indicating success */
  199.     }
  200.     }
  201.     if (displa) concb(escape);        /* (for console "interrupts") */
  202.     return(action);            /* Then do any requested protocol */
  203. }
  204.  
  205. /*  D O A R G  --  Do a command-line argument.  */
  206.  
  207. doarg(x) char x; {
  208.     int z; char *xp;
  209.  
  210.     xp = *xargv+1;            /* Pointer for bundled args */
  211.     while (x) {
  212.     switch (x) {
  213.  
  214. case 'x':                /* server */
  215.     if (action) fatal("conflicting actions");
  216.     action = 'x';
  217.     break;
  218.  
  219. case 'f':
  220.     if (action) fatal("conflicting actions");
  221.     action = setgen('F',"","","");
  222.     break;
  223.  
  224. case 'r':                /* receive */
  225.     if (action) fatal("conflicting actions");
  226.     action = 'v';
  227.     break;
  228.  
  229. case 'k':                /* receive to stdout */
  230.     if (action) fatal("conflicting actions");
  231.     stdouf = 1;
  232.     action = 'v';
  233.     break;
  234.  
  235. case 's':                 /* send */
  236.     if (action) fatal("conflicting actions");
  237.     if (*(xp+1)) fatal("invalid argument bundling after -s");
  238.     z = nfils = 0;            /* Initialize file counter, flag */
  239.     cmlist = xargv+1;            /* Remember this pointer */
  240.     while (--xargc > 0) {        /* Traverse the list */    
  241.     *xargv++;
  242.     if (**xargv == '-') {        /* Check for sending stdin */
  243.         if (strcmp(*xargv,"-") != 0) break;
  244.         z++;
  245.         }
  246.     nfils++;            /* Bump file counter */
  247.     }
  248.     xargc++, *xargv--;            /* Adjust argv/argc */
  249.     if (nfils < 1) fatal("missing filename for -s");
  250.     if (z > 1) fatal("-s: too many -'s");
  251.     if (z == 1) {
  252.     if (nfils == 1) nfils = 0;
  253.     else fatal("invalid mixture of filenames and '-' in -s");
  254.     }
  255.     if (nfils == 0) {
  256.     if (isatty(0)) fatal("sending from terminal not allowed");
  257.     }
  258.     debug(F101,*xargv,"",nfils);
  259.     action = 's';
  260.     break;
  261.  
  262. /* cont'd... */
  263.  
  264. /* ...doarg(), cont'd */
  265.  
  266. case 'g':                /* get */
  267.     if (action) fatal("conflicting actions");
  268.     if (*(xp+1)) fatal("invalid argument bundling after -g");
  269.     *xargv++, xargc--;
  270.     if ((xargc == 0) || (**xargv == '-'))
  271.         fatal("missing filename for -g");
  272.     cmarg = *xargv;
  273.     action = 'r';
  274.     break;
  275.  
  276. case 'c':                /* connect before */
  277.     cflg = 1;
  278.     break;
  279.  
  280. case 'n':                /* connect after */
  281.     cnflg = 1;
  282.     break;
  283.  
  284. case 'h':                /* help */
  285.     usage();
  286.     return(-1);
  287.  
  288. case 'a':                /* "as" */
  289.     if (*(xp+1)) fatal("invalid argument bundling after -a");
  290.     *xargv++, xargc--;
  291.     if ((xargc < 1) || (**xargv == '-'))
  292.         fatal("missing name in -a");
  293.     cmarg2 = *xargv;
  294.     break;
  295.  
  296. case 'l':                /* set line */
  297.     if (*(xp+1)) fatal("invalid argument bundling after -l");
  298.     *xargv++, xargc--;
  299.     if ((xargc < 1) || (**xargv == '-'))
  300.         fatal("communication line device name missing");
  301.     strcpy(ttname,*xargv);
  302. /*  if (strcmp(ttname,dftty) == 0) local = dfloc; else local = 1;  */
  303.     local = (strcmp(ttname,CTTNAM) != 0); /* (better than old way) */
  304.     debug(F101,"local","",local);
  305.     ttopen(ttname,&local,0);
  306.     break;
  307.  
  308. case 'b':                       /* set baud */
  309.     if (*(xp+1)) fatal("invalid argument bundling");
  310.     *xargv++, xargc--;
  311.     if ((xargc < 1) || (**xargv == '-'))
  312.         fatal("missing baud");
  313.     z = atoi(*xargv);            /* Convert to number */
  314.     if (chkspd(z) > -1) speed = z;    /* Check it */
  315.         else fatal("unsupported baud rate");
  316.     break;
  317.  
  318. case 'e':                /* Extended packet length */
  319.     if (*(xp+1)) fatal("invalid argument bundling");
  320.     *xargv++, xargc--;
  321.     if ((xargc < 1) || (**xargv == '-'))
  322.         fatal("missing length");
  323.     z = atoi(*xargv);            /* Convert to number */
  324.     if (z > 10 && z < maxrps) {
  325.         rpsiz = urpsiz = z;
  326.     if (z > 94) rpsiz = 94;        /* Fallback if other Kermit can't */
  327.     } else fatal("Unsupported packet length");
  328.     break;
  329.  
  330. case 'i':                /* Treat files as binary */
  331.     binary = 1;
  332.     break;
  333.  
  334. /* cont'd... */
  335.  
  336. /* ...doarg(), cont'd */
  337.  
  338.  
  339. case 'w':                /* File warning */
  340.     warn = 1;
  341.     break;
  342.  
  343. case 'q':                /* Quiet */
  344.     quiet = 1;
  345.     break;
  346.  
  347. case 'd':                /* debug */
  348.     debopn("debug.log");
  349.     break;
  350.  
  351. case 'p':                /* set parity */
  352.     if (*(xp+1)) fatal("invalid argument bundling");
  353.     *xargv++, xargc--;
  354.     if ((xargc < 1) || (**xargv == '-'))
  355.         fatal("missing parity");
  356.     switch(x = **xargv) {
  357.     case 'e':
  358.     case 'o':
  359.     case 'm':
  360.     case 's': parity = x; break;
  361.     case 'n': parity = 0; break;
  362.     default:  fatal("invalid parity");
  363.         }
  364.     break;
  365.  
  366. case 't':
  367.     turn = 1;                /* Line turnaround handshake */
  368.     turnch = XON;            /* XON is turnaround character */
  369.     duplex = 1;                /* Half duplex */
  370.     flow = 0;                /* No flow control */
  371.     break;
  372.  
  373. default:
  374.     fatal("invalid argument, type 'kermit -h' for help");
  375.         }
  376.  
  377.     x = *++xp;                /* See if options are bundled */
  378.     }
  379.     return(0);
  380. }
  381.  
  382. /* Misc */
  383.  
  384. fatal(msg) char *msg; {            /* Fatal error message */
  385.     fprintf(stderr,"\r\nFatal: %s\n",msg);
  386.     tlog(F110,"Fatal:",msg,0l);
  387.     doexit(BAD_EXIT);            /* Exit indicating failure */
  388. }
  389.  
  390.  
  391. ermsg(msg) char *msg; {            /* Print error message */
  392.     if (!quiet) fprintf(stderr,"\r\n%s - %s\n",cmerrp,msg);
  393.     tlog(F110,"Error -",msg,0l);
  394. }
  395.  
  396. /* Interactive command parser */ 
  397.  
  398.  
  399. /* Top-Level Keyword Table */
  400.  
  401. struct keytab cmdtab[] = {
  402.     "!",       XXSHE, 0,
  403.     "%",           XXCOM, CM_INV,
  404.     "bye",         XXBYE, 0,
  405.     "c",           XXCON, CM_INV,
  406.     "cd",          XXCWD, 0,
  407.     "close",       XXCLO, 0,
  408.     "connect",     XXCON, 0,
  409.     "cwd",       XXCWD, 0,
  410.     "dial",       XXDIAL, 0,
  411.     "directory",   XXDIR, 0,
  412.     "echo",        XXECH, 0,
  413.     "exit",       XXEXI, 0,
  414.     "finish",       XXFIN, 0,
  415.     "get",       XXGET, 0,
  416.     "hangup",      XXHAN, 0,
  417.     "help",       XXHLP, 0,
  418.     "log",         XXLOG, 0,
  419.     "quit",       XXQUI, 0,
  420.     "r",           XXREC, CM_INV,
  421.     "receive",       XXREC, 0,
  422.     "remote",       XXREM, 0,
  423.     "s",           XXSEN, CM_INV,
  424.     "script",       XXLOGI, 0,
  425.     "send",       XXSEN, 0,
  426.     "server",       XXSER, 0,
  427.     "set",       XXSET, 0,
  428.     "show",        XXSHO, 0,
  429.     "space",       XXSPA, 0,
  430.     "statistics",  XXSTA, 0,
  431.     "take",       XXTAK, 0
  432. };
  433. int ncmd = (sizeof(cmdtab) / sizeof(struct keytab));
  434.  
  435. /* Parameter keyword table */
  436.  
  437. struct keytab prmtab[] = {
  438.     "baud",            XYSPEE,  CM_INV,
  439.     "block-check",      XYCHKT,  0,
  440.     "delay",            XYDELA,  0,
  441.     "duplex",            XYDUPL,  0,
  442.     "end-of-packet",    XYEOL,   CM_INV,    /* moved to send/receive */
  443.     "escape-character", XYESC,   0,
  444.     "file",           XYFILE,  0,
  445.     "flow-control",     XYFLOW,  0,
  446.     "handshake",        XYHAND,  0,
  447.     "incomplete",       XYIFD,   0,
  448.     "line",             XYLINE,  0,
  449.     "modem-dialer",    XYMODM,     0,
  450.     "packet-length",    XYLEN,   CM_INV,    /* moved to send/receive */
  451.     "pad-character",    XYPADC,  CM_INV,    /* moved to send/receive */
  452.     "padding",          XYNPAD,  CM_INV,    /* moved to send/receive */
  453.     "parity",            XYPARI,  0,
  454.     "prompt",            XYPROM,  0,
  455.     "receive",          XYRECV,  0,
  456.     "retry",            XYRETR,  0,
  457.     "send",             XYSEND,  0,
  458.     "speed",            XYSPEE,  0,
  459.     "start-of-packet",  XYMARK,  CM_INV,    /* moved to send/receive */
  460.     "terminal",         XYTERM,  0,
  461.     "timeout",            XYTIMO,  CM_INV     /* moved to send/receive */
  462. };
  463. int nprm = (sizeof(prmtab) / sizeof(struct keytab)); /* How many parameters */
  464.  
  465.  
  466. /* Remote Command Table */
  467.  
  468. struct keytab remcmd[] = {
  469.     "cd",        XZCWD, CM_INV,
  470.     "cwd",       XZCWD, 0,
  471.     "delete",    XZDEL, 0,
  472.     "directory", XZDIR, 0,
  473.     "help",      XZHLP, 0,
  474.     "host",      XZHOS, 0,
  475.     "space",     XZSPA, 0,
  476.     "type",      XZTYP, 0,
  477.     "who",       XZWHO, 0
  478. };
  479. int nrmt = (sizeof(remcmd) / sizeof(struct keytab));
  480.  
  481. struct keytab logtab[] = {
  482.     "debugging",    LOGD, 0,
  483.     "packets",        LOGP, 0,
  484.     "session",      LOGS, 0,
  485.     "transactions", LOGT, 0
  486. };
  487. int nlog = (sizeof(logtab) / sizeof(struct keytab));
  488.  
  489. /* Show command arguments */
  490.  
  491. #define SHPAR 0                /* Parameters */
  492. #define SHVER 1                /* Versions */
  493.  
  494. struct keytab shotab[] = {
  495.     "parameters", SHPAR, 0,
  496.     "versions",   SHVER, 0
  497. };
  498.  
  499. /*  C M D I N I  --  Initialize the interactive command parser  */
  500.  
  501. cmdini() {
  502.  
  503. #ifdef AMIGA
  504.     congm();
  505.     concb(escape);
  506. #endif 
  507.     tlevel = -1;            /* Take file level */
  508.     cmsetp("C-Kermit>");        /* Set default prompt */
  509.  
  510. /* Look for init file in home or current directory. */
  511.  
  512.     homdir = zhome();
  513.     lp = line;
  514.     lp[0] = '\0';
  515.     if (homdir) {
  516.     strcpy(lp,homdir);
  517.     if (lp[0] == '/') strcat(lp,"/");
  518.     }
  519.     strcat(lp,KERMRC);
  520. #ifdef AMIGA
  521.     reqoff();            /* disable requestors */
  522. #endif
  523.     if ((tfile[0] = fopen(line,"r")) != NULL) {
  524.     tlevel = 0;
  525.     debug(F110,"init file",line,0);
  526.     }
  527.     if (homdir && (tlevel < 0)) {
  528.         strcpy(lp,KERMRC);
  529.     if ((tfile[0] = fopen(line,"r")) != NULL) {
  530.         tlevel = 0;
  531.         debug(F110,"init file",line,0);
  532.     } else {
  533.         debug(F100,"no init file","",0);
  534.         }
  535.     }
  536. #ifdef AMIGA
  537.     reqpop();                /* restore requestors */
  538. #else
  539.     congm();                /* Get console tty modes */
  540. #endif
  541. }
  542.  
  543. /* Display version herald and initial prompt */
  544.  
  545. herald() {
  546.     if (!backgrd) printf("%s,%s\nType ? for help\n",versio,ckxsys);
  547. }
  548.  
  549.  
  550. /*  T R A P  --  Terminal interrupt handler */
  551.  
  552. trap() {
  553.     debug(F100,"terminal interrupt...","",0);
  554.     doexit(GOOD_EXIT);            /* Exit indicating success */
  555. }
  556.  
  557. /*  S T P T R A P -- Handle SIGTSTP signals */
  558.  
  559. stptrap() {
  560.     conres();                /* Reset the console */
  561. #ifdef SIGTSTP
  562.     kill(0, SIGSTOP);            /* If job control, suspend the job */
  563. #else
  564.     doexit(GOOD_EXIT);            /* Probably won't happen otherwise */
  565. #endif
  566.     concb();                /* Put console back in Kermit mode */
  567. }
  568.  
  569. /*  P A R S E R  --  Top-level interactive command parser.  */
  570.  
  571. parser() {
  572.     int xx, cbn;
  573.     char *cbp;
  574.  
  575. #ifdef AMIGA
  576.     reqres();            /* restore AmigaDOS requestors */
  577. #endif
  578.     concb(escape);        /* Put console in cbreak mode. */
  579.     conint(trap);        /* Turn on console terminal interrupts. */
  580. /*
  581.  sstate becomes nonzero when a command has been parsed that requires some
  582.  action from the protocol module.  Any non-protocol actions, such as local
  583.  directory listing or terminal emulation, are invoked directly from below.
  584. */
  585.     if (local && !backgrd) printf("\n"); /*** Temporary kludge ***/
  586.     sstate = 0;                /* Start with no start state. */
  587.     while (sstate == 0) {        /* Parse cmds until action requested */
  588.     while ((tlevel > -1) && feof(tfile[tlevel])) { /* If end of take */
  589.         fclose(tfile[tlevel--]); /* file, close it. */
  590.         cmini(ckxech);        /* and clear the cmd buffer. */
  591.         if (tlevel < 0) {    /* Just popped out of cmd files? */
  592.             conint(trap);    /* Check background stuff again. */
  593.             return(0);        /* End of init file or whatever. */
  594.         }
  595.      }
  596. debug(F101,"tlevel","",tlevel);
  597.     if (tlevel > -1) {        /* If in take file */
  598.         cbp = cmdbuf;        /* Get the next line. */
  599.         cbn = CMDBL;
  600.  
  601. /* Loop to get next command line and all continuation lines from take file. */
  602.  
  603. again:        if (fgets(line,cbn,tfile[tlevel]) == NULL) continue;
  604.         lp = line;            /* Got one, copy it. */
  605.         while (*cbp++ = *lp++)
  606.             if (--cbn < 1) fatal("Command too long for internal buffer");
  607.         if (*(cbp - 3) == '\\') {    /* Continued on next line? */
  608.         cbp -= 3;        /* If so, back up pointer, */
  609.         goto again;        /* go back, get next line. */
  610.         }
  611.         stripq(cmdbuf);        /* Strip any quotes from cmd buffer. */
  612.  
  613.     } else {            /* No take file, get typein. */
  614.  
  615.         if (!backgrd) prompt();    /* Issue interactive prompt. */
  616.         cmini(ckxech);
  617.         }
  618.     repars = 1;
  619.     displa = 0;
  620.     while (repars) {
  621.         cmres();            /* Reset buffer pointers. */
  622.         xx = cmkey(cmdtab,ncmd,"Command","");
  623.         debug(F101,"top-level cmkey","",xx);
  624.         switch (docmd(xx)) {
  625.         case -4:        /* EOF */
  626.             doexit(GOOD_EXIT);    /* ...exit successfully */
  627.             case -1:        /* Reparse needed */
  628.             repars = 1;
  629.             continue;
  630.             case -2:        /* Invalid command given */
  631.             if (backgrd)     /* if in background, terminate */
  632.             fatal("Kermit command error in background execution");
  633.             if (tlevel > -1) {    /* If in take file, quit */
  634.             ermsg("Kermit command error: take file terminated.");
  635.             fclose(tfile[tlevel]);
  636.             tlevel--;
  637.             }
  638.             cmini(ckxech);    /* (fall thru) */
  639.              case -3:        /* Empty command OK at top level */
  640.         default:        /* Anything else (fall thru) */
  641.             repars = 0;        /* No reparse, get new command. */
  642.             continue;
  643.             }
  644.         }
  645.     }
  646. /* Got an action command; disable terminal interrupts and return start state */
  647.  
  648.     if (!local) connoi();        /* Interrupts off only if remote */
  649.     return(sstate);
  650. }
  651.  
  652. /*  D O E X I T  --  Exit from the program.  */
  653.  
  654. doexit(exitstat) int exitstat; {
  655.     
  656.     ttclos();                /* Close external line, if any */
  657.     if (local) {
  658.     strcpy(ttname,dftty);        /* Restore default tty */
  659.     local = dfloc;            /* And default remote/local status */
  660.     }
  661.     if (!quiet) conres();        /* Restore console terminal. */
  662.     if (!quiet) connoi();        /* Turn off console interrupt traps. */
  663.  
  664.     if (deblog) {            /* Close any open logs. */
  665.     debug(F100,"Debug Log Closed","",0);
  666.     *debfil = '\0';
  667.     deblog = 0;
  668.     zclose(ZDFILE);
  669.     }
  670.     if (pktlog) {
  671.     *pktfil = '\0';
  672.     pktlog = 0;
  673.     zclose(ZPFILE);
  674.     }
  675.     if (seslog) {
  676.         *sesfil = '\0';
  677.     seslog = 0;
  678.     zclose(ZSFILE);
  679.     }
  680.     if (tralog) {
  681.     tlog(F100,"Transaction Log Closed","",0l);
  682.     *trafil = '\0';
  683.     tralog = 0;
  684.     zclose(ZTFILE);
  685.     }
  686.     syscleanup();
  687.     exit(exitstat);                /* Exit from the program. */
  688. }
  689.  
  690. /*  B L D L E N  --  Make length-encoded copy of string  */
  691.  
  692. char *
  693. bldlen(str,dest) char *str, *dest; {
  694.     int len;
  695.     len = strlen(str);
  696.     *dest = tochar(len);
  697.     strcpy(dest+1,str);
  698.     return(dest+len+1);
  699. }
  700.  
  701.  
  702. /*  S E T G E N  --  Construct a generic command  */
  703.  
  704. setgen(type,arg1,arg2,arg3) char type, *arg1, *arg2, *arg3; {
  705.     char *upstr, *cp;
  706.  
  707.     cp = cmdstr;
  708.     *cp++ = type;
  709.     *cp = NUL;
  710.     if (*arg1 != NUL) {
  711.     upstr = bldlen(arg1,cp);
  712.     if (*arg2 != NUL) {
  713.         upstr = bldlen(arg2,upstr);
  714.         if (*arg3 != NUL) bldlen(arg3,upstr);
  715.     }
  716.     }
  717.     cmarg = cmdstr;
  718.     debug(F110,"setgen",cmarg,0);
  719.  
  720.     return('g');
  721. }
  722.  
  723. /*  D O C M D  --  Do a command  */
  724.  
  725. /*
  726.  Returns:
  727.    -2: user typed an illegal command
  728.    -1: reparse needed
  729.     0: parse was successful (even tho command may have failed).
  730. */ 
  731.  
  732. docmd(cx) int cx; {
  733.     int x, y;
  734.     char *s;
  735.  
  736.     switch (cx) {
  737.  
  738. case -4:                /* EOF */
  739.     if (!quiet && !backgrd) printf("\r\n");
  740.     doexit(GOOD_EXIT);
  741. case -3:                /* Null command */
  742.     return(0);
  743. case -2:                /* Error */
  744. case -1:                /* Reparse needed */
  745.     return(cx);
  746.  
  747. case XXBYE:                /* bye */
  748.     if ((x = cmcfm()) < 0) return(x);
  749.     if (!local) {
  750.     printf("You have to 'set line' first\n");
  751.     return(0);
  752.     }
  753.     sstate = setgen('L',"","","");
  754.     return(0);
  755.  
  756. case XXCOM:                /* comment */
  757.     if ((x = cmtxt("Text of comment line","",&s)) < 0) return(x);
  758.     return(0);
  759.  
  760. case XXCON:                         /* connect */
  761.     if ((x = cmcfm()) < 0) return(x);
  762.     return(doconect());
  763.  
  764. case XXCWD:
  765. #ifdef AMIGA
  766.     if (cmtxt("Name of local directory, or carriage return","",&s) < 0)
  767.         return(-1);
  768.     /* if no name, just print directory name */
  769.     if (*s) {
  770.     if (chdir(s)) perror(s);
  771.     cwdf = 1;
  772.     }
  773.     if (getcwd(line, sizeof(line)) == NULL)
  774.     printf("Current directory name not available.\n");
  775.     else
  776.     if (!backgrd) printf("%s\n", line);
  777. #else
  778.     if (cmtxt("Name of local directory, or carriage return",homdir,&s) < 0)
  779.         return(-1);    
  780.     if (chdir(s)) perror(s);
  781.     cwdf = 1;
  782.     system(PWDCMD);
  783. #endif
  784.     return(0);
  785.  
  786. case XXCLO:
  787.     x = cmkey(logtab,nlog,"Which log to close","");
  788.     if (x == -3) {
  789.     printf("?You must tell which log\n");
  790.     return(-2);
  791.     }
  792.     if (x < 0) return(x);
  793.     if ((y = cmcfm()) < 0) return(y);
  794.     switch (x) {
  795.  
  796.     case LOGD:
  797.         if (deblog == 0) {
  798.         printf("?Debugging log wasn't open\n");
  799.         return(0);
  800.         }
  801.         *debfil = '\0';
  802.         deblog = 0;
  803.         return(zclose(ZDFILE));
  804.  
  805.     case LOGP:
  806.         if (pktlog == 0) {
  807.         printf("?Packet log wasn't open\n");
  808.         return(0);
  809.         }
  810.         *pktfil = '\0';
  811.         pktlog = 0;
  812.         return(zclose(ZPFILE));
  813.  
  814.     case LOGS:
  815.         if (seslog == 0) {
  816.         printf("?Session log wasn't open\n");
  817.         return(0);
  818.         }
  819.         *sesfil = '\0';
  820.         seslog = 0;
  821.         return(zclose(ZSFILE));
  822.  
  823.         case LOGT:
  824.         if (tralog == 0) {
  825.         printf("?Transaction log wasn't open\n");
  826.         return(0);
  827.         }
  828.         *trafil = '\0';
  829.         tralog = 0;
  830.         return(zclose(ZTFILE));
  831.  
  832.     default:
  833.         printf("\n?Unexpected log designator - %ld\n", x);
  834.         return(0);
  835.     }
  836.  
  837. case XXDIAL:                /* dial number */
  838.     if ((x = cmtxt("Number to be dialed","",&s)) < 0) return(x);
  839.     return(ckdial(s));
  840.  
  841. case XXDIR:                /* directory */
  842. #ifdef AMIGA
  843.     if ((x = cmtxt("Directory/file specification","",&s)) < 0) return(x);
  844. #else
  845. #ifdef datageneral
  846.     if ((x = cmtxt("Directory/file specification","+",&s)) < 0) return(x);
  847. #else
  848.     if ((x = cmtxt("Directory/file specification",".",&s)) < 0) return(x);
  849. #endif
  850. #endif
  851.     lp = line;
  852.     sprintf(lp,"%s %s",DIRCMD,s);
  853.     system(line);
  854.     return(0);
  855.  
  856.  
  857. case XXECH:                 /* echo */
  858.     if ((x = cmtxt("Material to be echoed","",&s)) < 0) return(x);
  859.     for ( ; *s; s++) {
  860.     if ((x = *s) == 0134) {        /* Convert octal escapes */
  861.         s++;            /* up to 3 digits */
  862.         for (x = y = 0; *s >= '0' && *s <= '7' && y < 3; s++,y++) {
  863.             x = x * 8 + (int) *s - 48;
  864.         }
  865.         s--;
  866.         }
  867.     putchar(x);
  868.     }
  869.     printf("\n");
  870.     return(0);
  871.  
  872. case XXQUI:                /* quit, exit */
  873. case XXEXI:
  874.     if ((x = cmcfm()) > -1) doexit(GOOD_EXIT);
  875.     else return(x);
  876.  
  877. case XXFIN:                /* finish */
  878.     if ((x = cmcfm()) < 0) return(x);
  879.     if (!local) {
  880.     printf("You have to 'set line' first\n");
  881.     return(0);
  882.     }
  883.     sstate = setgen('F',"","","");
  884.     return(0);
  885.  
  886. case XXGET:                /* get */
  887.     if (!local) {
  888.     printf("\nYou have to 'set line' first\n");
  889.     return(0);
  890.     }
  891.     x = cmtxt("Name of remote file(s), or carriage return","",&cmarg);
  892.     if ((x == -2) || (x == -1)) return(x);
  893.  
  894. /* If foreign file name omitted, get foreign and local names separately */
  895.  
  896.     x = 0;                /* For some reason cmtxt returns 1 */
  897.     if (*cmarg == NUL) {
  898.  
  899.     if (tlevel > -1) {        /* Input is from take file */
  900.  
  901.         if (fgets(line,100,tfile[tlevel]) == NULL)
  902.             fatal("take file ends prematurely in 'get'");
  903. debug(F110,"take-get 2nd line",line,0);
  904.         stripq(line);
  905.         for (x = strlen(line);
  906.               x > 0 && (line[x-1] == '\n' || line[x-1] == '\r');
  907.          x--)
  908.         line[x-1] = '\0';
  909.         cmarg = line;
  910.         if (fgets(cmdbuf,CMDBL,tfile[tlevel]) == NULL)
  911.             fatal("take file ends prematurely in 'get'");
  912.         stripq(cmdbuf);
  913.         for (x = strlen(cmdbuf);
  914.               x > 0 && (cmdbuf[x-1] == '\n' || cmdbuf[x-1] == '\r');
  915.          x--)
  916.         cmdbuf[x-1] = '\0';
  917.         if (*cmdbuf == NUL) cmarg2 = line; else cmarg2 = cmdbuf;
  918.             x = 0;            /* Return code */
  919.  
  920.         } else {            /* Input is from terminal */
  921.  
  922.         char psave[40];        /* Save old prompt */
  923.         cmsavp(psave,40);
  924.         cmsetp(" Remote file specification: "); /* Make new one */
  925.         cmini(ckxech);
  926.         x = -1;
  927.         if (!backgrd) prompt();
  928.         while (x == -1) {        /* Prompt till they answer */
  929.             x = cmtxt("Name of remote file(s)","",&cmarg);
  930.         debug(F111," cmtxt",cmarg,x);
  931.         }
  932.         if (x < 0) {
  933.         cmsetp(psave);
  934.         return(x);
  935.         }
  936.         if (*cmarg == NUL) {     /* If user types a bare CR, */
  937.         printf("(cancelled)\n"); /* Forget about this. */
  938.             cmsetp(psave);        /* Restore old prompt, */
  939.         return(0);        /* and return. */
  940.         }
  941.         strcpy(line,cmarg);        /* Make a safe copy */
  942.         cmarg = line;
  943.         cmsetp(" Local name to store it under: ");    /* New prompt */
  944.         cmini(ckxech);
  945.         x = -1;
  946.         if (!backgrd) prompt();
  947.         while (x == -1) {        /* Again, parse till answered */
  948.             x = cmofi("Local file name","",&cmarg2);
  949.         }
  950.         if (x == -3) {                    /* If bare CR, */
  951.         printf("(cancelled)\n");    /* escape from this... */
  952.             cmsetp(psave);                /* Restore old prompt, */
  953.         return(0);                /* and return. */
  954.         } else if (x < 0) return(x);        /* Handle parse errors. */
  955.         
  956.         x = -1;            /* Get confirmation. */
  957.         while (x == -1) x = cmcfm();
  958.         cmsetp(psave);        /* Restore old prompt. */
  959.         }
  960.     }
  961.     if (x == 0) {            /* Good return from cmtxt or cmcfm, */
  962.     sstate = 'r';            /* set start state. */
  963.     if (local) displa = 1;
  964.     }
  965.     return(x);
  966.  
  967. case XXHLP:                /* Help */
  968.     x = cmkey(cmdtab,ncmd,"C-Kermit command","help");
  969.     return(dohlp(x));
  970.  
  971. case XXHAN:                /* Hangup */
  972.     if ((x = cmcfm()) > -1) return(tthang());
  973.  
  974. case XXLOG:                /* Log */
  975.     x = cmkey(logtab,nlog,"What to log","");
  976.     if (x == -3) {
  977.     printf("?You must specify what is to be logged\n");
  978.     return(-2);
  979.     }
  980.     if (x < 0) return(x);
  981.     return(dolog(x));
  982.  
  983. case XXLOGI:                /* Send script remote system */
  984.     if ((x = cmtxt("Text of login script","",&s)) < 0) return(x);
  985.     return( login(s) );            /* Return 0=completed, -2=failed */
  986.  
  987. case XXREC:                /* Receive */
  988.     cmarg2 = "";
  989.     x = cmofi("Name under which to store the file, or CR","",&cmarg2);
  990.     if ((x == -1) || (x == -2)) return(x);
  991.     debug(F111,"cmofi cmarg2",cmarg2,x);
  992.     if ((x = cmcfm()) < 0) return(x);
  993.     sstate = 'v';
  994.     if (local) displa = 1;
  995.     return(0);
  996.  
  997. case XXREM:                /* Remote */
  998.     if (!local) {
  999.     printf("\nYou have to 'set line' first\n");
  1000.     return(-2);
  1001.     }
  1002.     x = cmkey(remcmd,nrmt,"Remote Kermit server command","");
  1003.     if (x == -3) {
  1004.     printf("?You must specify a command for the remote server\n");
  1005.     return(-2);
  1006.     }
  1007.     return(dormt(x));
  1008.  
  1009. case XXSEN:                /* Send */
  1010.     cmarg = cmarg2 = "";
  1011.     if ((x = cmifi("File(s) to send","",&s,&y)) < 0) {
  1012.     if (x == -3) {
  1013.         printf("?A file specification is required\n");
  1014.         return(-2);
  1015.     }
  1016.     return(x);
  1017.     }
  1018.     nfils = -1;                /* Files come from internal list. */
  1019.     strcpy(line,s);            /* Save copy of string just parsed. */
  1020.     debug(F101,"Send: wild","",y);
  1021.     *cmarg2 = '\0';            /* Initialize send-as name */
  1022.     if (y == 0) {
  1023.     if ((x = cmtxt("Name to send it with","",&cmarg2)) < 0) return(x);
  1024.     } else {
  1025.     if ((x = cmcfm()) < 0) return(x);
  1026.     }
  1027.     cmarg = line;            /* File to send */
  1028.     debug(F110,"Sending:",cmarg,0);
  1029.     if (*cmarg2 != '\0') debug(F110," as:",cmarg2,0);
  1030.     sstate = 's';            /* Set start state */
  1031.     if (local) displa = 1;
  1032.     return(0);
  1033.  
  1034. case XXSER:                /* Server */
  1035.     if ((x = cmcfm()) < 0) return(x);
  1036.     sstate = 'x';
  1037.     if (local) displa = 1;
  1038. #ifdef AMIGA
  1039.     reqoff();                /* no DOS requestors while server */
  1040. #endif
  1041.     return(0);
  1042.  
  1043. case XXSET:                /* Set */
  1044.     x = cmkey(prmtab,nprm,"Parameter","");
  1045.     if (x == -3) {
  1046.     printf("?You must specify a parameter to set\n");
  1047.     return(-2);
  1048.     }
  1049.     if (x < 0) return(x);
  1050.     return(doprm(x));
  1051.     
  1052. /* XXSHE code by H. Fischer; copyright rights assigned to Columbia Univ */
  1053. /*
  1054.  Adapted to use getpwuid to find login shell because many systems do not
  1055.  have SHELL in environment, and to use direct calling of shell rather
  1056.  than intermediate system() call. -- H. Fischer
  1057. */
  1058. case XXSHE:                /* Local shell command */
  1059.     {
  1060.     int pid;
  1061. #ifdef AMIGA
  1062.     if (cmtxt("Command to execute","",&s) < 0) return(-1);
  1063. #else
  1064.     if (cmtxt("Unix shell command to execute","",&s) < 0) return(-1);
  1065. #endif
  1066.     conres();                /* Make console normal  */
  1067. #ifdef AMIGA
  1068.     system(s);
  1069. #else
  1070. #ifdef MSDOS
  1071.     zxcmd(s);
  1072. #else
  1073. #ifdef vax11c
  1074.  
  1075.     system(s);                /* Best we can do for VMS? */
  1076. #else                    /* All Unix systems... */
  1077. #ifdef datageneral
  1078.     if (*s == NUL)            /* Interactive shell requested? */
  1079. #ifdef mvux
  1080.     system("/bin/sh ");
  1081. #else
  1082.         system("x :cli prefix Kermit_Baby:");
  1083. #endif
  1084.     else                /* Otherwise, */
  1085.         system(s);            /* Best for aos/vs?? */
  1086.  
  1087. #else                    /* All Unix systems... */
  1088. #ifdef apollo
  1089.     if ((pid = vfork()) == 0) {        /* Make child quickly */
  1090.     char *shpath, *shname, *shptr;    /* For finding desired shell */
  1091.  
  1092.         if ((shpath = getenv("SHELL")) == NULL) shpath = "/com/sh";
  1093. #else
  1094.  
  1095.     if ((pid = fork()) == 0) {        /* Make child */
  1096.     char *shpath, *shname, *shptr;    /* For finding desired shell */
  1097.     struct passwd *p;
  1098.     extern struct passwd * getpwuid();
  1099.     extern int getuid();
  1100.     char *defShel = "/bin/sh";    /* Default */
  1101.  
  1102.     p = getpwuid( getuid() );    /* Get login data */
  1103.     if ( p == (struct passwd *) NULL || !*(p->pw_shell) )
  1104.         shpath = defShel;
  1105.     else
  1106.         shpath = p->pw_shell;
  1107. #endif
  1108.     shptr = shname = shpath;
  1109.     while (*shptr != '\0')
  1110.         if (*shptr++ == '/') shname = shptr;
  1111.  
  1112. /* Remove following uid calls if they cause trouble */
  1113. #ifdef BSD4
  1114.     setegid(getgid());        /* Override 4.3BSD csh security */
  1115.     seteuid(getuid());        /*  checks. */
  1116. #endif
  1117.  
  1118.     if (*s == NUL)            /* Interactive shell requested? */
  1119.         execl(shpath,shname,"-i",NULL);    /* Yes, do that */
  1120.     else                /* Otherwise, */
  1121.         execl(shpath,shname,"-c",s,NULL); /* exec the given command */
  1122.     exit(BAD_EXIT); }        /* Just punt if it didn't work */
  1123.  
  1124.     else {                /* Parent */
  1125.  
  1126.         int wstat;            /* Kermit must wait for child */
  1127.     SIGTYP (*istat)(), (*qstat)();
  1128.  
  1129.     istat = signal(SIGINT,SIG_IGN);    /* Let the fork handle keyboard */
  1130.     qstat = signal(SIGQUIT,SIG_IGN); /* interrupts itself... */
  1131.  
  1132.         while (((wstat = wait((int *)0)) != pid) && (wstat != -1)) ;
  1133.                                     /* Wait for fork */
  1134.     signal(SIGINT,istat);        /* Restore interrupts */
  1135.     signal(SIGQUIT,qstat);
  1136.     }
  1137. #endif
  1138. #endif
  1139. #endif
  1140. #endif
  1141.     concb(escape);            /* Console back in cbreak mode */
  1142.     return(0);
  1143. }
  1144.  
  1145. case XXSHO:                /* Show */
  1146.     x = cmkey(shotab,2,"","parameters");
  1147.     if (x < 0) return(x);
  1148.     if ((y = cmcfm()) < 0) return(y);
  1149.     switch (x) {
  1150.  
  1151.     case SHPAR:
  1152.         shopar();
  1153.         break;
  1154.  
  1155.     case SHVER:
  1156.         printf("\nVersions:\n %s\n %s\n",versio,protv);
  1157.         printf(" %s\n",fnsv);
  1158.         printf(" %s\n %s\n",cmdv,userv);
  1159.         printf(" %s for%s\n",ckxv,ckxsys);
  1160.         printf(" %s for%s\n",ckzv,ckzsys);
  1161.         printf(" %s\n",connv);
  1162.         printf(" %s\n %s\n\n",dialv,loginv);
  1163.         break;
  1164.  
  1165.     default:
  1166.         printf("\nNothing to show...\n");
  1167.         break;
  1168.     }
  1169.     return(0);
  1170.  
  1171. case XXSPA:                /* space */
  1172. #ifdef datageneral
  1173.     /* The DG can take an argument after its "space" command. */
  1174.     if ((x = cmtxt("Confirm, or local directory name","",&s)) < 0) return(x);
  1175.     if (*s == NULL) system(SPACMD);
  1176.     else {
  1177.         char *cp;
  1178.         cp = alloc(strlen(s) + 7);      /* For "space *s" */
  1179.         strcpy(cp,"space "), strcat(cp,s);
  1180.         system(cp);
  1181.         free(cp);
  1182.     }
  1183. #else
  1184.     if ((x = cmcfm()) < 0) return(x);
  1185.     system(SPACMD);
  1186. #endif
  1187.     return(0);
  1188.  
  1189. case XXSTA:                /* statistics */
  1190.     if ((x = cmcfm()) < 0) return(x);
  1191.     return(dostat());
  1192.  
  1193. case XXTAK:                /* take */
  1194.     if (tlevel > MAXTAKE-1) {
  1195.     printf("?Take files nested too deeply\n");
  1196.     return(-2);
  1197.     }
  1198.     if ((y = cmifi("C-Kermit command file","",&s,&x)) < 0) { 
  1199.     if (y == -3) {
  1200.         printf("?A file specification is required\n");
  1201.         return(-2);
  1202.     } else return(y);
  1203.     }
  1204.     if (x != 0) {
  1205.     printf("?Wildcards not allowed in command file name\n");
  1206.     return(-2);
  1207.     }
  1208.     strcpy(line,s);            /* Make a safe copy of the string */
  1209.     if ((y = cmcfm()) < 0) return(y);
  1210.     if ((tfile[++tlevel] = fopen(line,"r")) == NULL) {
  1211.     perror(line);
  1212.     debug(F110,"Failure to open",line,0);
  1213.     tlevel--;
  1214.     }
  1215.     return(0);
  1216.  
  1217. default:
  1218.     printf("Not available - %s\n",cmdbuf);
  1219.     return(-2);
  1220.     }
  1221. }
  1222.  
  1223. /*  D O C O N E C T  --  Do the connect command  */
  1224.  
  1225. /*  Note, we don't call this directly from dial, because we need to give */
  1226. /*  the user a chance to change parameters (e.g. parity) after the */
  1227. /*  connection is made. */
  1228.  
  1229. doconect() {
  1230.     int x;
  1231.     conres();                /* Put console back to normal */
  1232.     x = conect();            /* Connect */
  1233.     concb(escape);            /* Put console into cbreak mode, */
  1234.     return(x);                /* for more command parsing. */
  1235. }
  1236.