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

📄 ckuus4.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 5 页
字号:
	      if (bp[k]) free(bp[k]);	    return("");			/* and return null string. */	}	debug(F111,"fneval arg",bp[i],i);    }#ifdef DEBUG    if (deblog) {	int j;	for (j = 0; j < argn; j++) {	    debug(F111,"fneval arg post eval",argp[j],j);	    debug(F111,"fneval evaluated arg",bp[j],j);	}    }#endif /* DEBUG */    switch (y) {			/* Do function on expanded args */      case FN_DEF:	k = mlook(mactab,bp[0],nmac);	/* def(arg1) - Return a macro def */	p = (k > -1) ? mactab[k].mval : "";	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p ? p : "");      case FN_EVA:			/* eval(arg1) */	p = evala(bp[0]);	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p ? p : "");      case FN_EXE:			/* execute(arg1) */	j = (int)strlen(s = bp[0]);	/* Length of macro invocation */	p = "";				/* Initialize return value to null */	if (j) {			/* If there is a macro to execute */	    while (*s == SP) s++,j--;	/* strip leading spaces */	    p = s;			/* remember beginning of macro name */	    for (i = 0; i < j; i++) {	/* find end of macro name */		if (*s == SP) break;		s++;	    }	    if (*s == SP) {		/* if there was a space after */		*s++ = NUL;		/* terminate the macro name */		while (*s == SP) s++;	/* skip past any extra spaces */	    } else s = "";		/* maybe there are no arguments */	    if (p && *p)	      k = mlook(mactab,p,nmac);	/* Look up the macro name */	    else k = -1;	    p = "";			/* Initialize return value */	    if (k >= 0) {		/* If macro found in table */		if ((j = dodo(k,s)) > 0) { /* Go set it up (like DO cmd) */		    if (cmpush() > -1) { /* Push command parser state */			extern int ifc;			int ifcsav = ifc; /* Push IF condition on stack */			k = parser(1);	/* Call parser to execute the macro */			cmpop();	/* Pop command parser */			ifc = ifcsav;	/* Restore IF condition */			if (k == 0) {	/* No errors, ignore action cmds. */			    p = mrval[maclvl+1]; /* If OK, set return value. */			    if (p == NULL) p = "";			}		    } else {		/* Can't push any more */			debug(F100,"fexec pushed too deep","",0);                        printf("\n?\\fexec() too deeply nested\n");			while (cmpop() > -1) ;			p = "";		    }		}	    }	}	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p ? p : "");      case FN_FC:			/* File count. */	p = fnval;	*p = NUL;	if (argn > 0) {	    k = zxpand(bp[0]);	    sprintf(fnval,"%d",k);	    for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	}	return(p);      case FN_FIL:			/* Next file in list. */	p = fnval;			/* (no args) */	*p = NUL;	znext(p);	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p ? p : "");      case FN_IND:			/* index(arg1,arg2) */	if (argn > 1) {			/* Only works if we have 2 args */	    int start;	    len1 = (int)strlen(bp[0]);	/* length of string to look for */	    len2 = (int)strlen(s = bp[1]); /* length of string to look in */	    if (len1 < 0) return("");	/* paranoia */	    if (len2 < 0) return("");	    j = len2 - len1;		/* length difference */	    start = 0;			/* starting position */	    if (argn > 2) {		if (chknum(bp[2])) {		    start = atoi(bp[2]) - 1;		    if (start < 0) start = 0;		}	    }	    if (j < 0 || start > j) {	/* search string is longer */		p = "0";	    } else {		if (!incase) {		/* input case ignore? */		    lower(bp[0]);		    lower(bp[1]);		}		s = bp[1] + start;	/* Point to beginning of target */		p = "0";		for (i = 0; i <= (j - start); i++) { /* Now compare */		    if (!strncmp(bp[0],s++,len1)) {			sprintf(fnval,"%d",i+1+start);			p = fnval;			break;		    }		}	    }	} else p = "0";	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p);      case FN_CHR:			/* character(arg1) */	if (chknum(bp[0])) {		/* Must be numeric */	    i = atoi(bp[0]);	    if (i >= 0 && i < 256) {	/* Must be an 8-bit value */		p = fnval;		*p++ = i;		*p = NUL;		p = fnval;	    } else p = "";		/* Otherwise return null */	} else p = "";			/* Otherwise return null */	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p);      case FN_COD:			/* code(char) */	p = "";	if ((int)strlen(bp[0]) > 0) {	    p = fnval;	    i = *bp[0];	    sprintf(p,"%d",(i & 0xff));	}	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p);      case FN_LEN:			/* length(arg1) */	p = fnval;	sprintf(p,"%d",(int)strlen(bp[0]));	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p);      case FN_LOW:			/* lower(arg1) */	s = bp[0];	p = fnval;	while (*s) {	    if (isupper(*s))	      *p = tolower(*s);	    else	      *p = *s;	    p++; s++;	}	*p = NUL;	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	p = fnval;	return(p);      case FN_MAX:			/* max(arg1,arg2) */      case FN_MIN:			/* min(arg1,arg2) */      case FN_MOD:			/* mod(arg1,arg2) */	if (chknum(bp[0]) && chknum(bp[1])) {	    i = atoi(bp[0]);	    j = atoi(bp[1]);	    switch (y) {	      case FN_MAX:		if (j < i) j = i;		break;	      case FN_MIN:		if (j > i) j = i;		break;	      case FN_MOD:		j = i % j;		break;	    }	    p = fnval;	    sprintf(p,"%d",j);	} else p = "";	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p);      case FN_SUB:			/* substr(arg1,arg2,arg3) */      case FN_RIG:			/* right(arg1,arg2) */	if (((argn > 1) && (int)strlen(bp[1]) && !rdigits(bp[1])) ||	    ((y == FN_SUB) &&	    ((argn > 2) && (int)strlen(bp[2]) && !rdigits(bp[2])))) {	    p = "";			/* if either, return null */	} else {	    int lx;	    p = fnval;			         /* pointer to result */	    lx = strlen(bp[0]);			 /* length of arg1 */	    if (y == FN_SUB) {			 /* substring */		k = (argn > 2) ? atoi(bp[2]) : 1023; /* length */		j = (argn > 1) ? atoi(bp[1]) : 1; /* start pos for substr */	    } else {				 /* right */		k = (argn > 1) ? atoi(bp[1]) : lx; /* length */		j = lx - k + 1;			 /* start pos for right */		if (j < 1) j = 1;	    }	    if (k > 0 && j <= lx) { 		 /* if start pos in range */		s = bp[0]+j-1;    		 /* point to source string */		for (i = 0; (i < k) && (*p++ = *s++); i++) ;  /* copy */	    }	    *p = NUL;			/* terminate the result */	    p = fnval;			/* and point to it. */	}	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]); /* Free temp mem */	return(p);      case FN_UPP:			/* upper(arg1) */	s = bp[0];	p = fnval;	while (*s) {	    if (islower(*s))	      *p = toupper(*s);	    else	      *p = *s;	    p++; s++;	}	*p = NUL;	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	p = fnval;	return(p);      case FN_REP:			/* Repeat */	p = "";				/* Return value */	if (chknum(bp[1])) {		/* Repeat count */	    n = atoi(bp[1]);	    if (n > 0) {		/* Make n copies */		p = fnval;		*p = '\0';		k = (int)strlen(bp[0]);	/* Make sure string has some length */		if (k > 0) {		    for (i = 0; i < n; i++) {			s = bp[0];			for (j = 0; j < k; j++) {			    if ((p - fnval) >= FNVALL) { /* Protect against */				p = "";	             /* core dumps... */				break;			    } else *p++ = *s++;			}		    }		    *p = NUL;		}	    }	}	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	p = fnval;	return(p);#ifndef NOFRILLS      case FN_REV:	p = fnval;	yystring(bp[0],&p);	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	return(p);#endif /* NOFRILLS */      case FN_RPA:			/* RPAD and LPAD */      case FN_LPA:	*fnval = NUL;			/* Return value */	if (argn == 1) {		/* If a number wasn't given */	    p = fnval;			/* just return the original string */	    strncpy(p,bp[0],FNVALL);	} else if (chknum(bp[1])) {	/* Repeat count */	    char pc;	    n = atoi(bp[1]);	    if (n >= 0) {		/* Pad it out */		p = fnval;		k = (int)strlen(bp[0]);	/* Length of string to be padded */		pc = (argn < 3) ? SP : *bp[2]; /* Padding character */		if (n > FNVALL) n = FNVALL-1; /* protect against overruns */		if (k > FNVALL) k = FNVALL-1; /* and silly args. */                if (k > n) k = n;		if (y == FN_RPA) {	/* RPAD */		    strncpy(p,bp[0],k);		    p += k;		    for (i = k; i < n; i++)		      *p++ = pc;		} else {		/* LPAD */		    n -= k;		    for (i = 0; i < n; i++)		      *p++ = pc;		    strncpy(p,bp[0],k);		    p += k;		}		*p = NUL;	    }	}	for (k = 0; k < argn; k++) if (bp[k]) free(bp[k]);	p = fnval;	return(p);      default:	return("");    }}#endif /* NOSPL */#ifndef NOSPLchar *					/* Evaluate builtin variable */nvlook(s) char *s; {    int x, y;    long z;    char *p;    x = 30;    p = vvbuf;    if (xxstring(s,&p,&x) < 0) {	y = -1;    } else {	s = vvbuf;	if ((y = lookup(vartab,s,nvars,&x)) < 0) return(NULL);    }    switch (y) {      case VN_ARGC:			/* ARGC */	sprintf(vvbuf,"%d",macargc[maclvl]);	return(vvbuf);      case VN_ARGS:			/* ARGS */	sprintf(vvbuf,"%d",xargs);	return(vvbuf);      case VN_COUN:			/* COUNT */	sprintf(vvbuf,"%d",count[cmdlvl]);	return(vvbuf);      case VN_DATE:			/* DATE */	ztime(&p);			/* Get "asctime" string */	if (p == NULL || *p == NUL) return(NULL);	vvbuf[0] = p[8];		/* dd */	vvbuf[1] = p[9];	vvbuf[2] = SP;	vvbuf[3] = p[4];		/* mmm */	vvbuf[4] = p[5];	vvbuf[5] = p[6];	vvbuf[6] = SP;	for (x = 20; x < 24; x++)	/* yyyy */	  vvbuf[x - 13] = p[x];	vvbuf[11] = NUL;	return(vvbuf);      case VN_NDAT:			/* Numeric date */	ztime(&p);			/* Get "asctime" string */	if (p == NULL || *p == NUL) return(NULL);	for (x = 20; x < 24; x++)	/* yyyy */	  vvbuf[x - 20] = p[x];        vvbuf[6] = (p[8] == ' ') ? '0' : p[8]; vvbuf[7] = p[9]; /* dd */	for (x = 0; x < 12; x++)	  /* mm */	  if (!strncmp(p+4,months[x],3)) break;	if (x == 12) {	    vvbuf[4] = vvbuf[5] = '?';	} else {	    x++;	    vvbuf[4] = (x < 10) ? '0' : '1';	    vvbuf[5] = (x % 10) + 48;	}	vvbuf[8] = NUL;        return(vvbuf);      case VN_DIRE:			/* DIRECTORY */	return(zgtdir());      case VN_FILE:			/* filespec */	return(fspec);      case VN_HOST:			/* host name */	if (*myhost) {			/* If known */	    return(myhost);		/* return it. */	} else {			/* Otherwise */	    strcpy(vvbuf,"unknown");	/* just say "unknown" */	    return(vvbuf);	}      case VN_SYST:			/* System type */#ifdef UNIX	strcpy(vvbuf,"UNIX");#else#ifdef VMS	strcpy(vvbuf,"VMS");#else#ifdef OSK	strcpy(vvbuf,"OS9/68K");#else#ifdef AMIGA	strcpy(vvbuf,"Amiga");#else#ifdef MAC	strcpy(vvbuf,"Macintosh");#else#ifdef OS2	strcpy(vvbuf,"OS/2");#else#ifdef datageneral	strcpy(vvbuf,"AOS/VS");#else#ifdef GEMDOS	strcpy(vvbuf,"Atari_ST");#else	strcpy(vvbuf,"unknown");#endif /* GEMDOS */#endif /* datageneral */#endif /* OS2 */#endif /* MAC */#endif /* AMIGA */#endif /* OSK */#endif /* VMS */#endif /* UNIX */	return(vvbuf);      case VN_SYSV:			/* System herald */	for (x = y = 0; x < VVBUFL; x++) {	    if (ckxsys[x] == SP && y == 0) continue;	    vvbuf[y++] = (ckxsys[x] == SP) ? '_' : ckxsys[x];	}	vvbuf[y] = NUL;	return(vvbuf);      case VN_TIME:			/* TIME. Assumes that ztime returns */	ztime(&p);			/* "Thu Feb  8 12:00:00 1990" */	if (p == NULL || *p == NUL)	/* like asctime()! */	  return(NULL);	for (x = 11; x < 19; x++)	/* copy hh:mm:ss */	  vvbuf[x - 11] = p[x];		/* to vvbuf */	vvbuf[8] = NUL;			/* terminate */	return(vvbuf);			/* and return it */      case VN_NTIM:			/* Numeric time */	ztime(&p);			/* "Thu Feb  8 12:00:00 1990" */	if (p == NULL || *p == NUL)	/* like asctime()! */	  return(NULL);	z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);	sprintf(vvbuf,"%ld",z);	return(vvbuf);#ifdef UNIX      case VN_TTYF:			/* TTY file descriptor */	sprintf(vvbuf,"%d",ttyfd);	return(vvbuf);#else#ifdef OS2      case VN_TTYF:			/* TTY file descriptor */	sprintf(vvbuf,"%d",ttyfd);	return(vvbuf);#endif /* OS2 */#endif /* UNIX */      case VN_VERS:			/* Numeric Kermit version number */	sprintf(vvbuf,"%ld",vernum);	return(vvbuf);      case VN_HOME:			/* Home directory */#ifdef UNIX        sprintf(vvbuf,"%s/",zhome());	return(vvbuf);#else	return(zhome());#endif /* UNI

⌨️ 快捷键说明

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