⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckuusr.c

📁 Kermit Kermit文件运输协议 Kermit文件运输协议是用于在PC之间交换文件的一种简单的文件运输协议。该协议是哥仑比亚大学开发的
💻 C
📖 第 1 页 / 共 3 页
字号:
    "padding",          XYNPAD,  CM_INV,    /* moved to send/receive */
    "parity",           XYPARI,  0,
    "prompt",           XYPROM,  0,
    "receive",          XYRECV,  0,
    "send",             XYSEND,  0,
    "speed",            XYSPEE,  0,
    "start-of-packet",  XYMARK,  CM_INV,    /* moved to send/receive */
    "timeout",          XYTIMO,  CM_INV     /* moved to send/receive */
};
int nprm = (sizeof(prmtab) / sizeof(struct keytab)); /* How many parameters */


/* Remote Command Table */

struct keytab remcmd[] = {
    "cwd",       XZCWD, 0,
    "delete",    XZDEL, 0,
    "directory", XZDIR, 0,
    "help",      XZHLP, 0,
    "host",      XZHOS, 0,
    "space",     XZSPA, 0,
    "type",      XZTYP, 0,
    "who",       XZWHO, 0
};
int nrmt = (sizeof(remcmd) / sizeof(struct keytab));

struct keytab logtab[] = {
    "debugging",    LOGD, 0,
    "packets",      LOGP, 0,
    "session",      LOGS, 0,
    "transactions", LOGT, 0
};
int nlog = (sizeof(logtab) / sizeof(struct keytab));

/* Show command arguments */

#define SHPAR 0                         /* Parameters */
#define SHVER 1                         /* Versions */

struct keytab shotab[] = {
    "parameters", SHPAR, 0,
    "versions",   SHVER, 0
};

/* Form Feed */
/*  C M D I N I  --  Initialize the interactive command parser  */

cmdini() {

    printf3("%s,%s\nType ? for help\n",versio,ckxsys);
    cmsetp("C-Kermit>");                /* Set default prompt. */

    tlevel = -1;                        /* Take file level */

/* Look for init file in home or current directory. */

    homdir = zhome();
    lp = line;
    lp[0] = '\0';
    if (homdir) {
        strcpy(lp,homdir);
        if (lp[0] == '/') strcat(lp,"/");
    }
    strcat(lp,KERMRC);
    if ((tfile[0] = fopen(line,"r")) != NULL) {
        tlevel = 0;
        debug(F110,"init file",line,0);
    }
    if (homdir && (tlevel < 0)) {
        strcpy(lp,KERMRC);
        if ((tfile[0] = fopen(line,"r")) != NULL) {
            tlevel = 0;
            debug(F110,"init file",line,0);
        } else {
            debug(F100,"no init file","",0);
        }
    }

    congm();                            /* Get console tty modes */
}


/*  T R A P  --  Terminal interrupt handler */

trap() {
    debug(F100,"terminal interrupt...","",0);
    doexit(GOOD_EXIT);                  /* Exit indicating success */
}

/* Form Feed */
/*  P A R S E R  --  Top-level interactive command parser.  */

parser() {
    int xx, cbn;
    char *cbp;

    concb(escape);              /* Put console in cbreak mode. */
    conint(trap);               /* Turn on console terminal interrupts. */
/*
 sstate becomes nonzero when a command has been parsed that requires some
 action from the protocol module.  Any non-protocol actions, such as local
 directory listing or terminal emulation, are invoked directly from below.
*/
    if (local) printf("\n");            /*** Temporary kludge ***/
    sstate = 0;                         /* Start with no start state. */
    while (sstate == 0) {               /* Parse cmds until action requested */
        while ((tlevel > -1) && feof(tfile[tlevel])) { /* If end of take */
                fclose(tfile[tlevel]);  /* file, close it */
                tlevel--;               /* and forget about it. */
                cmini(ckxech);          /* and clear the cmd buffer. */
        }
        if (tlevel > -1) {              /* If in take file */
            cbp = cmdbuf;               /* Get the next line. */
            cbn = CMDBL;

/* Loop to get next command line and all continuation lines from take file. */

again:      if (fgets(line,cbn,tfile[tlevel]) == NULL) continue;
            lp = line;                  /* Got one, copy it. */
            while (*cbp++ = *lp++)
                if (--cbn < 1) fatal("Command too long for internal buffer");
            if (*(cbp - 3) == '\\') {   /* Continued on next line? */
                cbp -= 3;               /* If so, back up pointer, */
                goto again;             /* go back, get next line. */
            }
            stripq(cmdbuf);             /* Strip any quotes from cmd buffer. */

        } else {                        /* No take file, get typein. */

            prompt();                   /* Issue interactive prompt. */
            cmini(ckxech);
        }
        repars = 1;
        displa = 0;
        while (repars) {
            cmres();                    /* Reset buffer pointers. */
            xx = cmkey(cmdtab,ncmd,"Command","");
            debug(F101,"top-level cmkey","",xx);
            switch (docmd(xx)) {
                case -4:                /* EOF */
                    doexit(GOOD_EXIT);  /* ...exit successfully */
                case -1:                /* Reparse needed */
                    repars = 1;
                    continue;
                case -2:                /* Invalid command given */
                    if (backgrd)        /* if in background, terminate */
                        fatal("Kermit command error in background execution");
                    if (tlevel > -1) {  /* If in take file, quit */
                        ermsg("Kermit command error: take file terminated.");
                        fclose(tfile[tlevel]);
                        tlevel--;
                    }
                    cmini(ckxech);      /* (fall thru) */
                case -3:                /* Empty command OK at top level */
                default:                /* Anything else (fall thru) */
                    repars = 0;         /* No reparse, get new command. */
                    continue;
            }
        }
    }
/* Got an action command; disable terminal interrupts and return start state */

    if (!local) connoi();               /* Interrupts off only if remote */
    return(sstate);
}

/* Form Feed */
/*  D O E X I T  --  Exit from the program.  */

doexit(exitstat) int exitstat; {

    ttclos();                           /* Close external line, if any */
    if (local) {
        strcpy(ttname,dftty);           /* Restore default tty */
        local = dfloc;                  /* And default remote/local status */
    }
    if (!quiet) conres();               /* Restore console terminal. */
    if (!quiet) connoi();               /* Turn off console interrupt traps. */

    if (deblog) {                       /* Close any open logs. */
        debug(F100,"Debug Log Closed","",0);
        *debfil = '\0';
        deblog = 0;
        zclose(ZDFILE);
    }
    if (pktlog) {
        *pktfil = '\0';
        pktlog = 0;
        zclose(ZPFILE);
    }
    if (seslog) {
        *sesfil = '\0';
        seslog = 0;
        zclose(ZSFILE);
    }
    if (tralog) {
        tlog(F100,"Transaction Log Closed","",0l);
        *trafil = '\0';
        tralog = 0;
        zclose(ZTFILE);
    }
#ifdef AMIGA
    sysclnup();
#endif
    exit(exitstat);                             /* Exit from the program. */
}

/* Form Feed */
/*  B L D L E N  --  Make length-encoded copy of string  */

char *
bldlen(str,dest) char *str, *dest; {
    int len;
    len = strlen(str);
    *dest = tochar(len);
    strcpy(dest+1,str);
    return(dest+len+1);
}


/*  S E T G E N  --  Construct a generic command  */

setgen(type,arg1,arg2,arg3) char type, *arg1, *arg2, *arg3; {
    char *upstr, *cp;

    cp = cmdstr;
    *cp++ = type;
    *cp = NUL;
    if (*arg1 != NUL) {
        upstr = bldlen(arg1,cp);
        if (*arg2 != NUL) {
            upstr = bldlen(arg2,upstr);
            if (*arg3 != NUL) bldlen(arg3,upstr);
        }
    }
    cmarg = cmdstr;
    debug(F110,"setgen",cmarg,0);

    return('g');
}

/* Form Feed */
/*  D O C M D  --  Do a command  */

/*
 Returns:
   -2: user typed an illegal command
   -1: reparse needed
    0: parse was successful (even tho command may have failed).
*/

docmd(cx) int cx; {
    int x, y;
    char *s;

    switch (cx) {

case -4:                                /* EOF */
    if (!quiet) printf("\r\n");
    doexit(GOOD_EXIT);
case -3:                                /* Null command */
    return(0);
case -2:                                /* Error */
case -1:                                /* Reparse needed */
    return(cx);

case XXBYE:                             /* bye */
    if ((x = cmcfm()) < 0) return(x);
    if (!local) {
        printf("You have to 'set line' first\n");
        return(0);
    }
    sstate = setgen('L',"","","");
    return(0);

case XXCOM:                             /* comment */
    if ((x = cmtxt("Text of comment line","",&s)) < 0) return(x);
    return(0);

case XXCON:                             /* connect */
    if ((x = cmcfm()) < 0) return(x);
    return(doconect());

case XXCWD:
    if (cmtxt("Name of local directory, or carriage return",homdir,&s) < 0)
        return(-1);
    if (chdir(s)) perror(s);
    cwdf = 1;
    system(PWDCMD);
    return(0);

/* Form Feed */
case XXCLO:
    x = cmkey(logtab,nlog,"Which log to close","");
    if (x == -3) {
        printf("?You must tell which log\n");
        return(-2);
    }
    if (x < 0) return(x);
    if ((y = cmcfm()) < 0) return(y);
    switch (x) {

        case LOGD:
            if (deblog == 0) {
                printf("?Debugging log wasn't open\n");
                return(0);
            }
            *debfil = '\0';
            deblog = 0;
            return(zclose(ZDFILE));

        case LOGP:
            if (pktlog == 0) {
                printf("?Packet log wasn't open\n");
                return(0);
            }
            *pktfil = '\0';
            pktlog = 0;
            return(zclose(ZPFILE));

        case LOGS:
            if (seslog == 0) {
                printf("?Session log wasn't open\n");
                return(0);
            }
            *sesfil = '\0';
            seslog = 0;
            return(zclose(ZSFILE));

        case LOGT:
            if (tralog == 0) {
                printf("?Transaction log wasn't open\n");
                return(0);
            }
            *trafil = '\0';
            tralog = 0;
            return(zclose(ZTFILE));

        default:
            printf2("\n?Unexpected log designator - %ld\n", x);
            return(0);
    }

/* Form Feed */
case XXDIAL:                            /* dial number */
    if ((x = cmtxt("Number to be dialed","",&s)) < 0) return(x);
    return(dial(s));

case XXDIR:                             /* directory */
    if ((x = cmtxt("Directory/file specification",".",&s)) < 0) return(x);
    lp = line;
    sprintf(lp,"%s %s",DIRCMD,s);
    system(line);
    return(0);


case XXECH:                             /* echo */
    if ((x = cmtxt("Material to be echoed","",&s)) < 0) return(x);
    for ( ; *s; s++) {
        if ((x = *s) == 0134) {         /* Convert octal escapes */
            s++;                        /* up to 3 digits */
            for (x = y = 0; *s >= '0' && *s <= '7' && y < 3; s++,y++) {
                x = x * 8 + (int) *s - 48;
            }
            s--;
        }
        putchar(x);
    }
    printf("\n");
    return(0);

case XXQUI:                             /* quit, exit */
case XXEXI:
    if ((x = cmcfm()) > -1) doexit(GOOD_EXIT);
    else return(x);

case XXFIN:                             /* finish */
    if ((x = cmcfm()) < 0) return(x);
    if (!local) {
        printf("You have to 'set line' first\n");
        return(0);
    }
    sstate = setgen('F',"","","");
    return(0);

/* Form Feed */
case XXGET:                             /* get */
    if (!local) {
        printf("\nYou have to 'set line' first\n");
        return(0);
    }
    x = cmtxt("Name of remote file(s), or carriage return","",&cmarg);
    if ((x == -2) || (x == -1)) return(x);

/* If foreign file name omitted, get foreign and local names separately */

    if (*cmarg == NUL) {

        if (tlevel > -1) {              /* Input is from take file */

            if (fgets(line,100,tfile[tlevel]) == NULL)
                fatal("take file ends prematurely in 'get'");
            stripq(line);
	    for (x = strlen(line);
	    	x > 0 && (line[x-1] == '\n' || line[x-1] == '\r');
		x--)
		line[x-1] = '\0';
            cmarg = line;
            if (fgets(cmdbuf,CMDBL,tfile[tlevel]) == NULL)
                fatal("take file ends prematurely in 'get'");
                stripq(cmdbuf);
            if (*cmdbuf == NUL) cmarg2 = line; else cmarg2 = cmdbuf;

        } else {                        /* Input is from terminal */

            char psave[40];             /* Save old prompt */
            cmsavp(psave,40);
            cmsetp(" Remote file specification: "); /* Make new one */
            cmini(ckxech);
            x = -1;
            prompt();
            while (x == -1) {           /* Prompt till they answer */
                x = cmtxt("Name of remote file(s)","",&cmarg);
                debug(F111," cmtxt",cmarg,x);
            }
            if (x < 0) {
                cmsetp(psave);
                return(x);
            }
            if (*cmarg == NUL) {        /* If user types a bare CR, */
                printf("(cancelled)\n"); /* Forget about this. */
                cmsetp(psave);          /* Restore old prompt, */
                return(0);              /* and return. */
            }
            strcpy(line,cmarg);         /* Make a safe copy */
            cmarg = line;
            cmsetp(" Local name to store it under: ");  /* New prompt */
            cmini(ckxech);
            x = -1;
            prompt();                   /* Prompt */
            while (x < 0) {             /* Again, parse till answered */
                x = cmofi("Local file name","",&cmarg2);
                if (x == -2) return(x);
                if (x == -3) {                  /* If bare CR, */
                    printf("(cancelled)\n");    /* escape from this... */
                    cmsetp(psave);              /* restore old prompt, */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -