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

📄 ckucmd.c

📁 linux终端仿真程序
💻 C
📖 第 1 页 / 共 5 页
字号:
#include "ckcsym.h"char *cmdv = "Command package 6.0.088, 6 Sep 96";#ifdef OS2int cmdmsk = 255; /* Command-terminal-to-C-Kermit character mask */#elseint cmdmsk = 127; /* Command-terminal-to-C-Kermit character mask */#endif /* OS2 */#ifdef BS_DIRSEP			/* Directory separator is backslash */#undef BS_DIRSEP#endif /* BS_DIRSEP */#ifdef OS2#define BS_DIRSEP#endif /* BS_DIRSEP */#include "ckcdeb.h"                     /* Formats for debug(), etc. */#include "ckcker.h"			/* Needed for BIGBUFOK definition */#include "ckucmd.h"			/* Needed for xx_strp prototype */#include "ckuusr.h"                     /* Needed for prompt length */_PROTOTYP( int unhex, (char) );_PROTOTYP( static int filhelp, (int, char *, char *, int) );_PROTOTYP( VOID sh_sort, (char **, int) );_PROTOTYP( static VOID cmdclrscn, (void) );#ifndef NOICP     /* The rest only if interactive command parsing selected */#ifndef NOSPL_PROTOTYP( int chkvar, (char *) );#endif /* NOSPL */static int blocklvl = 0;              /* "Block" level */static int linebegin = 0;             /* Flag for at start of a line */static int quoting = 1;#ifdef BS_DIRSEPstatic int cmdirflg = 0;#endif /* BS_DIRSEP *//*  C K U C M D  --  Interactive command package for Unix  *//*  Author: Frank da Cruz (fdc@columbia.edu),  Columbia University Academic Information Systems, New York City.  Copyright (C) 1985, 1996, Trustees of Columbia University in the City of New  York.  The C-Kermit software may not be, in whole or in part, licensed or  sold for profit as a software product itself, nor may it be included in or  distributed with commercial products or otherwise distributed by commercial  concerns to their clients or customers without written permission of the  Office of Kermit Development and Distribution, Columbia University.  This  copyright notice must not be removed, altered, or obscured.*//*Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP. Features:. parses and verifies keywords, filenames, text strings, numbers, other data. displays appropriate menu or help message when user types "?". does keyword and filename completion when user types ESC or TAB. does partial filename completion. accepts any unique abbreviation for a keyword. allows keywords to have attributes, like "invisible" and "abbreviation". can supply defaults for fields omitted by user. provides command retry and recall. provides command line editing (character, word, and line deletion). accepts input from keyboard, command files, macros, or redirected stdin. allows for full or half duplex operation, character or line input. allows \-escapes for hard-to-type characters. allows specification of a user exit to expand variables, etc.. settable prompt, protected from deletion, dynamically re-evaluated each time.Functions: cmsetp - Set prompt (cmprom is prompt string) cmsavp - Save current prompt prompt - Issue prompt cmini - Clear the command buffer (before parsing a new command) cmres - Reset command buffer pointers (before reparsing) cmkey - Parse a keyword or token cmnum - Parse a number cmifi - Parse an input file name cmofi - Parse an output file name cmdir - Parse a directory name cmfld - Parse an arbitrary field cmtxt - Parse a text string cmcfm - Parse command confirmation (end of line)Return codes: -9: like -2 except this module already printed the error message -3: no input provided when required -2: input was invalid (e.g. not a number when a number was required) -1: reparse required (user deleted into a preceding field)  0 or greater: successSee individual functions for greater detail.Before using these routines, the caller should #include ckucmd.h, and set theprogram's prompt by calling cmsetp().  If the file parsing functions cmifi,cmofi, or cmdir are to be used, this module must be linked with a ck?fio filesystem support module for the appropriate system, e.g. ckufio for Unix.  Ifthe caller puts the terminal in character wakeup ("cbreak") mode with no echo,then these functions will provide line editing -- character, word, and linedeletion, as well as keyword and filename completion upon ESC and helpstrings, keyword, or file menus upon '?'.  If the caller puts the terminalinto character wakeup/noecho mode, care should be taken to restore it beforeexit from or interruption of the program.  If the character wakeup mode is notset, the system's own line editor may be used.NOTE: Contrary to expectations, many #ifdef's have been added to this module.Any operation requiring an #ifdef (like clear screen, get character fromkeyboard, erase character from screen, etc) should eventually be turned into acall to a function that is defined in ck?tio.c, but then all the ck?tio.cmodules would have to be changed...*//* Includes */#include "ckcker.h"#include "ckcasc.h"			/* ASCII character symbols */#include "ckucmd.h"                     /* Command parsing definitions */#include <errno.h>			/* Error number symbols */#ifdef OS2#ifndef NT#define INCL_NOPM#define INCL_VIO			/* Needed for ckocon.h */#include <os2.h> #undef COMMENT#else #define APIRET ULONG#include <windows.h>#endif /* NT */#include "ckocon.h"#include <io.h>#endif /* OS2 */#ifdef NT#define stricmp _stricmp #endif /* NT */#ifdef OSK#define cc ccount			/* OS-9/68K compiler bug */#endif /* OSK */#ifdef GEMDOS				/* Atari ST */#ifdef putchar#undef putchar#endif /* putchar */#define putchar(x) conoc(x)#endif /* GEMDOS *//* Local variables */staticint psetf = 0,                          /* Flag that prompt has been set */    cc = 0,                             /* Character count */    dpx = 0,                            /* Duplex (0 = full) */    inword = 0;				/* In the middle of getting a word */staticint hw = HLPLW,                         /* Help line width */    hc = HLPCW,                         /* Help line column width */    hh,                                 /* Current help column number */    hx;                                 /* Current help line position */char *dfprom = "Command? ";             /* Default prompt */int cmflgs;                             /* Command flags */int cmfsav;				/* A saved version of them */#ifdef DCMDBUFchar *cmdbuf;				/* Command buffer */char *savbuf;				/* Buffer to save copy of command */char *atmbuf;				/* Atom buffer - for current field */char *atxbuf;				/* For expanding the atom buffer */#ifdef OLDHELPstatic char *hlpbuf;			/* Help string buffer */#endif /* OLDHELP */static char *atybuf;			/* For copying atom buffer */static char *filbuf;			/* File name buffer */static char *cmprom;			/* Program's prompt */static char *cmprxx;			/* Program's prompt, unevaluated */#ifdef CK_RECALL/*  Command recall is available only if we can make profligate use of malloc().*/#define R_MAX 10			/* Max number of commands to save */int cm_recall = R_MAX;			/* Size of command recall buffer */int on_recall = 1;			/* Recall feature is ON */int in_recall = 0;			/* Recall buffers are init'd */static int  current = -1,				/* Pointer to current command */  rlast = -1;				/* Index of last command in buffer */static char **recall = NULL;		/* Array of recall buffer pointers */#endif /* CK_RECALL */#elsechar cmdbuf[CMDBL+4];                   /* Command buffer */char savbuf[CMDBL+4];                   /* Buffer to save copy of command */char atmbuf[ATMBL+4];                   /* Atom buffer */char atxbuf[CMDBL+4];                   /* For expanding the atom buffer */#ifdef OLDHELPstatic char hlpbuf[HLPBL+4];		/* Help string buffer */#endif /* OLDHELP */static char atybuf[ATMBL+4];		/* For copying atom buffer */static char filbuf[ATMBL+4];		/* File name buffer */static char cmprom[PROMPTL+1];		/* Program's prompt */static char cmprxx[PROMPTL+1];		/* Program's prompt, unevaluated */#endif /* DCMDBUF *//* Command buffer pointers */static char *bp,                        /* Current command buffer position */    *pp,                                /* Start of current field */    *np;                                /* Start of next field */static int ungw,			/* For ungetting words */    atxn;				/* Expansion buffer (atxbuf) length */#ifdef OS2extern int wideresult;#endif /* OS2 */extern int cmd_cols, cmd_rows;#ifdef OLDHELP_PROTOTYP( static VOID addhlp, (char *) );_PROTOTYP( static VOID clrhlp, (void) );_PROTOTYP( static VOID dmphlp, (void) );#endif /* OLDHELP */_PROTOTYP( static int gtword, (void) );_PROTOTYP( static int addbuf, (char *) );_PROTOTYP( static int setatm, (char *, int) );_PROTOTYP( static int cmdgetc, (void) );_PROTOTYP( static VOID cmdnewl, (char) );_PROTOTYP( static VOID cmdchardel, (void) );_PROTOTYP( static VOID cmdecho, (char, int) );_PROTOTYP( static int test, (int, int) );#ifdef GEMDOS_PROTOTYP( extern char *strchr, (char *, int) );#endif /* GEMDOS *//*  T E S T  --  Bit test  */static inttest(x,m) int x, m; { /*  Returns 1 if any bits from m are on in x, else 0  */    return((x & m) ? 1 : 0);}/*  K W D H E L P  --  Given a keyword table, print keywords in columns.  *//*  Call with:    s     - keyword table    n     - number of entries    pat   - pattern (left substring) that must match for each keyword    pre   - prefix to add to each keyword    post  - suffix to add to each keyword    off   - offset on first screenful, allowing room for introductory text  Arranges keywords in columns with width based on longest keyword.  Does "more?" prompting at end of screen.    Uses global cmd_rows and cmd_cols for screen size.*/VOIDkwdhelp(s,n,pat,pre,post,off)    struct keytab s[]; int n, off; char *pat, *pre, *post;/* kwdhelp */ {    int width = 0;    int cc;    int cols, height, i, j, k, lc, n2 = 0;    char *b = NULL, *p, *q;    char *pa, *px;    char **s2 = NULL;        cc = strlen(pat);    if (!s) return;			/* Nothing to do */    if (n < 1) return;			/* Ditto */    if (off < 0) off = 0;		/* Offset for first page */    if (!pre) pre = "";			/* Handle null string pointers */    if (!post) post = "";    lc = off;				/* Screen-line counter */    if (s2 = (char **) malloc(n * sizeof(char *))) {	for (i = 0; i < n; i++) {	/* Find longest keyword */	    s2[i] = NULL;	    if (/* (cc > 0) && */		(xxstrcmp(s[i].kwd,pat,cc)) || (s[i].flgs & CM_INV)		)	      continue;	    s2[n2++] = s[i].kwd;	/* Copy pointers to visible ones */	    j = strlen(s[i].kwd);	    if (j > width)	      width = j;	}	/* Column width */	n = n2;    }    if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer   */	char * bx;	bx = b + cmd_cols;	width += (int)strlen(pre) + (int)strlen(post) + 2; 	cols = cmd_cols / width;	/* How many columns? */	if (cols < 1) cols = 1;	height = n / cols;		/* How long is each column? */	if (n % cols) height++;		/* Add one for remainder, if any */	for (i = 0; i < height; i++) {	    /* Loop for each row */	    for (j = 0; j < cmd_cols; j++)  /* First fill row with blanks */	      b[j] = SP;	    for (j = 0; j < cols; j++) {    /* Loop for each column in row */		k = i + (j * height);       /* Index of next keyword */		if (k < n) {		    /* In range? */		    pa = pre;		    px = post;		    p = s2[k];		    /* Point to verb name */		    q = b + (j * width) + 1; /* Where to copy it to */		    while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */		    q--;		                 /* Back up over NUL */		    while ((q < bx) && (*q++ = *p++)) ;	 /* Copy filename */		    q--;		                 /* Back up over NUL */		    while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */		    if (j < cols - 1) {			q--;			*q = SP;	/* Replace the space */		    }		}	    }	    p = b + cmd_cols - 1;	/* Last char in line */	    while (*p-- == SP) ;	/* Trim */	    *(p+2) = NUL;	    printf("%s\n",b);		/* Print the line */	    if (++lc > (cmd_rows - 2)) { /* Screen full? */		if (!askmore())		/* Do more-prompting... */		  goto xkwdhelp;		else		  lc = 0;	    }	} 	/* printf("\n"); */		/* Blank line at end of report */    } else {				/* Malloc failure, no columns */	for (i = 0; i < n; i++) {	    if (s[i].flgs & CM_INV)	/* Use original keyword table */	      continue;			/* skipping invisible entries */	    printf("%s%s%s\n",pre,s[i].kwd,post);	    if (++lc > (cmd_rows - 2)) { /* Screen full? */		if (!askmore())		/* Do more-prompting... */		  goto xkwdhelp;		else		  lc = 0;	    }	    	}    }  xkwdhelp:    if (s2) free(s2);			/* Free array copy */    if (b) free(b);			/* Free line buffer */    return;}VOIDsh_sort(s,n) char **s; int n; {		/* Shell sort */    int m, i, j;			/* Much faster than bubble sort */    char *t;				/* Not as fast as Quicksort */    if (n < 2)				/* but less code, and fast enough */      return;    m = n;				/* Initial group size is whole array */    while (1) {	m = m / 2;			/* Divide group size in half */	if (m < 1)			/* Small as can be, so done */	  break;	for (j = 0; j < n-m; j++) {	/* Sort each group */	    t = s[j+m];	    for (i = j; i >= 0; i -= m) {		if (strcmp(s[i],t) < 0)		  break;		s[i+m] = s[i];	    }	    s[i+m] = t;	}    }}/*  F I L H E L P  --  Given a file list, print names in columns.  *//*  Call with:    s     - file list    n     - number of entries    pat   - pattern (left substring) that must match for each filename    pre   - prefix to add to each filename    post  - suffix to add to each filename    off   - offset on first screenful, allowing room for introductory text  Arranges filenames in columns with width based on longest filename.  Does "more?" prompting at end of screen.    Uses global cmd_rows and cmd_cols for screen size.*/#ifndef MAXPATHLEN#define MAXPATHLEN 1024#endif /* MAXPATHLEN */static intfilhelp(n,pre,post,off) int n, off; char *pre, *post; {    char filbuf[MAXPATHLEN + 1];	/* Temp buffer for one filename */    int width = 0;    int cols, height, i, j, k, lc, n2 = 0, rc = 0;    char *b = NULL, *p, *q;    char *pa, *px;    char **s2 = NULL;    if (n < 1) return(0);    if (off < 0) off = 0;		/* Offset for first page */    if (!pre) pre = "";			/* Handle null string pointers */    if (!post) post = "";    lc = off;				/* Screen-line counter */    if (s2 = (char **) malloc(n * sizeof(char *))) {	for (i = 0; i < n; i++) {	/* Loop through filenames */	    s2[i] = NULL;		/* Initialize each pointer to NULL */	    znext(filbuf);		/* Get next filename */	    if (!filbuf[0])		/* Shouldn't happen */	      break;	    if (!(s2[n2] = malloc((j = (int) strlen(filbuf)) + 1))) {		printf("?Memory allocation failure\n");		rc = -9;		goto xfilhelp;	    }	    if (j <= MAXPATHLEN) {		strcpy(s2[n2],filbuf);		n2++;	    } else {		printf("?Name too long - %s\n", filbuf);		rc = -9;		goto xfilhelp;	    }			    if (j > width)		/* Get width of widest one */	      width = j;	}	n = n2;				/* How many we actually got */    }    sh_sort(s2,n);			/* Alphabetize the list */    if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */	char * bx;	bx = b + cmd_cols;	width += (int)strlen(pre) + (int)strlen(post) + 2; 	cols = cmd_cols / width;	/* How many columns? */	if (cols < 1) cols = 1;

⌨️ 快捷键说明

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