home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / ckermit.arc / ckuusr.c < prev    next >
C/C++ Source or Header  |  1988-02-12  |  34KB  |  1,228 lines

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