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

📄 ckucmd.c

📁 linux终端仿真程序
💻 C
📖 第 1 页 / 共 5 页
字号:
	free(cmp_b[cmddep][4]);	cmp_b[cmddep][4] = NULL;    }    if (cmp_b[cmddep][5]) {	strncpy(atybuf,cmp_b[cmddep][5],ATMBL); /* 5: Atom buffer copY */	free(cmp_b[cmddep][5]);	cmp_b[cmddep][5] = NULL;    }    if (cmp_b[cmddep][6]) {	strncpy(filbuf,cmp_b[cmddep][6],ATMBL); /* 6: Filename buffer */	free(cmp_b[cmddep][6]);	cmp_b[cmddep][6] = NULL;    }#endif /* DCMDBUF */    cmddep--;				/* Rise, rise */    debug(F101,"&cmpop","",cmddep);    return(cmddep);}#endif /* NOSPL */#ifdef COMMENTVOIDstripq(s) char *s; {                    /* Function to strip '\' quotes */    char *t;    while (*s) {        if (*s == CMDQ) {            for (t = s; *t != '\0'; t++) *t = *(t+1);        }        s++;    }}#endif /* COMMENT *//* Convert tabs to spaces, one for one */VOIDuntab(s) char *s; {    while (*s) {	if (*s == HT) *s = SP;	s++;    }}/*  C M N U M  --  Parse a number in the indicated radix  *//* The only radix allowed in unquoted numbers is 10. Parses unquoted numeric strings in base 10. Parses backslash-quoted numbers in the radix indicated by the quote:   \nnn = \dnnn = decimal, \onnn = octal, \xnn = Hexadecimal. If these fail, then if a preprocessing function is supplied, that is applied and then a second attempt is made to parse an unquoted decimal string. And if that fails, the preprocessed string is passed to an arithmetic expression evaluator.  Returns:   -3 if no input present when required,   -2 if user typed an illegal number,   -1 if reparse needed,    0 otherwise, with argument n set to the number that was parsed*/intcmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; xx_strp f; {    int x; char *s, *zp, *zq;    if (radix != 10) {                  /* Just do base 10 */        printf("cmnum: illegal radix - %d\n",radix);        return(-1);    } /* Easy to add others but there has never been a need for it. */    x = cmfld(xhlp,xdef,&s,(xx_strp)0);    debug(F101,"cmnum: cmfld","",x);    if (x < 0) return(x);		/* Parse a field */    zp = atmbuf;/*      Edit 192 - Allow any number field to be braced.  This lets us included  spaces in expressions, but perhaps more important lets us have user-defined  functions in numeric fields, since these always have spaces in them.*/    if (*zp == '{') {			/* Braced field, strip braces */	x = (int) strlen(atmbuf);	if (x > 0) {			/* The "if" is to shut up optimizers */	    *(atmbuf+x-1) = NUL;	/* that complain about a possible */	    zp++;			/* reference to atbmbuf[-1] even */	}				/* though we know that x > 0. */    }    if (chknum(zp)) {			/* Check for decimal number */        *n = atoi(zp);			/* Got one, we're done. */	debug(F101,"cmnum 1st chknum ok","",*n);        return(0);    } else if ((x = xxesc(&zp)) > -1) {	/* Check for backslash escape */#ifndef OS2	*n = x;#else	*n = wideresult;#endif /* OS2 */	debug(F101,"cmnum xxesc ok","",*n);	return(*zp ? -2 : 0);    } else if (f) {			/* If conversion function given */	zq = atxbuf;			/* Try that */	atxn = CMDBL;	(*f)(zp,&zq,&atxn);		/* Convert */	zp = atxbuf;    }    debug(F110,"cmnum zp",zp,0);    if (chknum(zp)) {			/* Check again for decimal number */        *n = atoi(zp);			/* Got one, we're done. */	debug(F101,"cmnum 2nd chknum ok","",*n);        return(0);#ifndef NOSPL    }  else if ((x = xxesc(&zp)) > -1) { /* Check for backslash escape */#ifndef OS2	*n = x;#else	*n = wideresult;#endif /* OS2 */	debug(F101,"cmnum xxesc 2 ok","",*n);	return(*zp ? -2 : 0);    } else if (f) {			/* Not numeric, maybe an expression */	char *p; _PROTOTYP(char * evala, (char *));	p = evala(zp);	if (chknum(p)) {	    *n = atoi(p);	    debug(F101,"cmnum exp eval ok","",*n);	    return(0);	} else return(-2);#endif /* NOSPL */    } else {				/* Not numeric */	return(-2);    }}/*  C M O F I  --  Parse the name of an output file  *//* Depends on the external function zchko(); if zchko() not available, use cmfld() to parse output file names. Returns:   -9 like -2, except message already printed,   -3 if no input present when required,   -2 if permission would be denied to create the file,   -1 if reparse needed,    0 or 1 if file can be created, with xp pointing to name.    2 if given the name of an existing directory.*/intcmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {    int x; char *s, *zq;#ifdef OS2    int tries;#endif /* OS2 */#ifdef DTILDE    _PROTOTYP( char * tilde_expand, (char *) );    char *dirp;#endif /* DTILDE */    if (*xhlp == NUL) xhlp = "Output file";    *xp = "";    if ((x = cmfld(xhlp,xdef,&s,(xx_strp)0)) < 0) return(x);    debug(F111,"cmofi 1",s,x);    if (*s == '{') {			/* Strip enclosing braces */	int n;	n = strlen(s);	if (s[n-1] == '}') {	    s[n-1] = NUL;	    s++;	}    }    debug(F110,"cmofi 1.5",s,0);#ifdef OS2    tries = 0;    {	char *p = s, q;    /*      This is really ugly.  If we skip conversion the first time through,      then variable names like \%a will be used as filenames (e.g. creating      a file called %A in the root directory).  If we DON'T skip conversion      the first time through, then single backslashes used as directory      separators in filenames will be misinterpreted as variable lead-ins.      So we prescan to see if it has any variable references.  But this      module is not supposed to know anything about variables, functions,      etc, so this code does not really belong here, but rather it should      be at the same level as zzstring().    */	while ( (tries == 0) && (p = strchr(p,CMDQ)) ) {	    q = *(p+1);			/* Char after backslash */	    if (!q)			/* None, quit */	      break;	    if (isupper(q))		/* If letter, convert to lowercase */	      q = tolower(q);	    if (isdigit(q)) {		/* If it's a digit, */		tries = 1;		/* assume it's a backslash code  */		break;	    }	    switch (q) {	      case CMDQ:		/* Double backslash */		tries = 1;		/* so call the conversion function */		break;	      case '%':			/* Variable or array reference */	      case '&':			/* must be followed by letter */		if (isalpha(*(p+2)) || (*(p+2) >= '0' && *(p+2) <= '9'))		  tries = 1;		break;	      case 'm': case 'v': case '$': /* \m(), \v(), \$() */		if (*(p+2) == '(')		  if (strchr(p+2,')'))		    tries = 1;		break;	      case 'f':			/* \Fname() */		if (strchr(p+2,'('))		  if (strchr(p+2,')'))		      tries = 1;		break;	      case '{':			/* \{...} */		if (strchr(p+2,'}'))		  tries = 1;		break;	      case 'd': case 'o':	/* Decimal or Octal number */	        if (isdigit(*(p+2)))		  tries = 1;		break;	      case 'x':			/* Hex number */		if (isdigit(*(p+2)) ||		    ((*(p+2) >= 'a' && *(p+2) <= 'f') ||		     ((*(p+2) >= 'A' && *(p+2) <= 'F'))))		  tries = 1;	      default:		break;	    }	    p++;	}    }o_again:    if (tries == 1)#endif /* OS2 */    if (f) {				/* If a conversion function is given */	zq = atxbuf;			/* do the conversion. */	atxn = CMDBL;	if ((x = (*f)(s,&zq,&atxn)) < 0)	  return(-2);	s = atxbuf;    }    debug(F111,"cmofi 2",s,x);#ifdef DTILDE    dirp = tilde_expand(s);		/* Expand tilde, if any, */    if (*dirp != '\0') {		/* right in the atom buffer. */	if (setatm(dirp,1) < 0) {	    printf("?Name too long\n");	    return(-9);	}    }    s = atmbuf;    debug(F110,"cmofi 3",s,0);#endif /* DTILDE */    if (iswild(s)) {        printf("?Wildcards not allowed - %s\n",s);        return(-2);    }    debug(F110,"cmofi 4",s,0);#ifdef CK_TMPDIR    /* isdir() function required for this! */    if (isdir(s)) {	debug(F110,"cmofi 5: is directory",s,0);        *xp = s;	return(2);    }#endif /* CK_TMPDIR */    if (strcmp(s,CTTNAM) && (zchko(s) < 0)) { /* OK to write to console */#ifdef COMMENT#ifdef OS2/*  We don't try again because we already prescanned the string to see if  if contained anything that could be used by zzstring().*/	if (tries++ < 1)	  goto o_again;#endif /* OS2 */#endif /* COMMENT */	debug(F110,"cmofi 6: failure",s,0);        printf("?Write permission denied - %s\n",s);        return(-9);    } else {	debug(F110,"cmofi 7: ok",s,0);        *xp = s;        return(x);    }}/*  C M I F I  --  Parse the name of an existing file  *//* This function depends on the external functions:   zchki()  - Check if input file exists and is readable.   zxpand() - Expand a wild file specification into a list.   znext()  - Return next file name from list. If these functions aren't available, then use cmfld() to parse filenames.*//* Returns   -4 EOF   -3 if no input present when required,   -2 if file does not exist or is not readable,   -1 if reparse needed,    0 or 1 otherwise, with:        xp pointing to name,        wild = 1 if name contains '*' or '?', 0 otherwise.*/intcmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {    return(cmifi2(xhlp,xdef,xp,wild,0,NULL,f));}/*  cmifip() is called when we want to supply a path or path list to search  in case the filename that the user gives is (a) not absolute, and (b) can't  be found as given.  The path string can be the name of a single directory,  or a list of directories separated by the PATHSEP character, defined in  ckucmd.h.  Look in ckuusr.c and ckuus3.c for examples of usage.*/intcmifip(xhlp,xdef,xp,wild,d,path,f)    char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; {    return(cmifi2(xhlp,xdef,xp,wild,0,path,f));}/*  cmifi2() is for use when you also want to parse a directory or device  name as an input file, as in the DIRECTORY command.*/intcmifi2(xhlp,xdef,xp,wild,d,path,f)    char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; {#define NEWCMIFI#ifdef NEWCMIFI    /* New version... */    int i, x, xc; long y; char *sp, *zq;    char *sv = NULL;#ifdef DTILDE    char *tilde_expand(), *dirp;#endif /* DTILDE */#ifndef NOPARTIAL#ifndef OS2#ifdef OSK    /* This large array is dynamic for OS-9 -- should do for others too... */    extern char **mtchs;#else#ifdef UNIX    /* OK, for UNIX too */    extern char **mtchs;#else    extern char *mtchs[];#endif /* UNIX */#endif /* OSK */#endif /* OS2 */#endif /* NOPARTIAL */    inword = 0;				/* Initialize counts & pointers */    cc = 0;    xc = 0;    *xp = "";    if ((x = cmflgs) != 1) {            /* Already confirmed? */#ifdef BS_DIRSEP	cmdirflg = 1;        x = gtword();                   /* No, get a word */	cmdirflg = 0;#else        x = gtword();                   /* No, get a word */#endif /* BS_DIRSEP */    } else {				/* If so, use default, if any. */        if (setatm(xdef,0) < 0) {	    printf("?Default input filename too long\n");	    return(-9);	}    }  i_path:    *xp = atmbuf;                       /* Point to result. */    while (1) {        xc += cc;                       /* Count this character. */        debug(F111,"cmifi gtword",atmbuf,xc);	debug(F101,"cmifi switch x","",x);        switch (x) {	    case -9:	       printf("Command or field too long\n");            case -4:                    /* EOF */            case -2:                    /* Out of space. */            case -1:                    /* Reparse needed */                return(x);            case 0:                     /* SP or NL */            case 1:                if (xc == 0) *xp = xdef;     /* If no input, return default. */                if (**xp == NUL) return(-3); /* If field empty, return -3. */		if (**xp == '{') {	/* Strip enclosing braces first  */		    char *s = *xp;	/* which might have been used if */		    int n;		/* a filespec included spaces... */		    n = strlen(s);		    if (s[n-1] == '}') {			s[n-1] = NUL;			s++;			*xp = s;		    }		}#ifdef OS2/* In OS/2 and Windows, if we're parsing directory name, allow a disk name */		if (d && ((int)strlen(*xp) == 2))		  if (isalpha(**xp) && *(*xp + 1) == ':')		    return(x);#endif /* OS2 */#ifndef NOSPL		if (f) {		/* If a conversion function is given */		    char *s = *xp;	/* See if there are any variables in */		    while (*s) {	/* the string and if so, expand them */			if (chkvar(s)) {			    zq = atxbuf;			    atxn = CMDBL;			    if ((y = (*f)(*xp,&zq,&atxn)) < 0)			      return(-2);			    if ((int) strlen(atxbuf) > 0) {				*xp = atxbuf;				break;			    }			}			s++;		    }		}#endif /* NOSPL */		debug(F110,"cmifi atxbuf",atxbuf,0);		if (!sv) {			     /* Only do this once */		    sv = malloc((int)strlen(*xp)+1); /* Make a safe copy */		    if (!sv) {			printf("?malloc error 73, cmifi\n");			return(-9);		    }		    strcpy(sv,*xp);		    debug(F110,"cmifi sv",sv,0);		}		y = zxpand(*xp);		*wild = (y > 1);

⌨️ 快捷键说明

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