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

📄 ckuus6.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 4 页
字号:
	    for (i = 0; i < m; i++,lp++) { /* search for label in macro body */		if (*lp == ',') {	/* Really should also watch out */		    flag = 1;		/* for braces here...  Commas in */		    continue;		/* in braces are not really commas */		}		if (flag) {		/* If in valid label position */		    if (*lp == SP)	/* eat leading spaces */		      continue;		    if (*lp != ':') {	/* Look for label introducer */			flag = 0;	/* this isn't it */			continue;	/* keep looking */		    }		}		if (!flag)		/* We don't have a label */		  continue;		/*  so keep looking... */		xp = lp; tp = tmplbl;	/* Copy the label from the macro */		j = 0;			/* to make it null-terminated */		while (*tp = *xp) {		    if (j++ > 50) break;  /* j = length of word from macro */		    if (*tp < 33 || *tp == ',')	/* Look for end of word */		      break;		    else tp++, xp++;	/* Next character */		}		*tp = '\0';		/* In case we stopped early */		/* Now do caseless string comparison, using longest length */		debug(F111,"macro GOTO label",s,y);		debug(F111,"macro target label",tmplbl,j);		if (!xxstrcmp(s,tmplbl,(y > j) ? y : j))		  break;		else flag = 0;	    }	    if (i == m) {		/* didn't find the label */		debug(F101,"goto failed at cmdlvl","",cmdlvl);		if (!popclvl()) {	/* pop up to next higher level */		    printf("?Label '%s' not found\n",s); /* if none */		    return(0);		/* quit */		} else continue;	/* otherwise look again */	    }	    debug(F110,"goto found macro label",lp,0);	    macp[maclvl] = lp;		/* set macro buffer pointer */	    return(1);	} else if (cmdstk[cmdlvl].src == CMD_TF) {	    x = 0;			/* GOTO issued in take file */	    rewind(tfile[tlevel]);	/* Search file from beginning */	    while (! feof(tfile[tlevel])) {		if (fgets(line,LINBUFSIZ,tfile[tlevel]) == NULL) /* Get line */		  break;		/* If no more, done, label not found */		lp = line;		/* Got line */		while (*lp == SP || *lp == HT)		  lp++;			/* Strip leading whitespace */		if (*lp != ':') continue; /* Check for label introducer */		tp = lp;		/* Get end of word */		j = 0;		while (*tp) {		/* And null-terminate it */		    if (*tp < 33) {			*tp = '\0';			break;		    } else tp++, j++;		}		if (!xxstrcmp(lp,s,(y > j) ? y : j)) { /* Caseless compare */		    x = 1;		/* Got it */		    break;		/* done. */		}	    }	    if (x == 0) {		/* If not found, print message */		debug(F101,"goto failed at cmdlvl","",cmdlvl);		if (!popclvl()) {	/* pop up to next higher level */		    printf("?Label '%s' not found\n",s);	/* if none */		    return(0);		/* quit */		} else continue;	/* otherwise look again */	    }	    return(x);			/* Send back return code */	}    }    printf("?Stack problem in GOTO %s\n",s); /* Shouldn't see this */    return(0);}#endif /* NOSPL */#ifndef NOSPL/* Finish parsing and do the IF, XIF, and WHILE commands */intdoif(cx) int cx; {    int x, y, z; char *s, *p;    not = 0;				/* Flag for whether "NOT" was seen */    z = 0;				/* Initial IF condition */    ifargs = 0;				/* Count of IF condition words */ifagain:    if ((ifc = cmkey(iftab,nif,"","",xxstring)) < 0) { /* If what?... */	if (ifc == -3) {	    printf("?Condition required\n");	    return(-9);	} else return(ifc);    }    switch (ifc) {			/* set z = 1 for true, 0 for false */      case XXIFNO:			/* IF NOT */	not ^= 1;			/* So NOT NOT NOT ... will work */	ifargs++;	goto ifagain;      case XXIFSU:			/* IF SUCCESS */	z = ( success != 0 );	debug(F101,"if success","",z);	ifargs += 1;	break;      case XXIFFA:			/* IF FAILURE */	z = ( success == 0 );	debug(F101,"if failure","",z);	ifargs += 1;	break;      case XXIFDE:			/* IF DEFINED */	if ((x = cmfld("Macro or variable name","",&s,NULL)) < 0) {	    if (x == -3) return(-2);	    else return(x);	}#ifdef COMMENT	strcpy(line,s);			/* Make a copy */        if ((int)strlen(line) < 1) return(-2);	lp = line;	if (line[0] == CMDQ && (line[1] == '%' || line[1] == '&')) lp++;	if (*lp == '%')	{		/* Is it a variable? */	    x = *(lp + 1);		/* Fold case */	    if (isupper(x)) *(lp + 1) = tolower(x);	    if (x >= '0' && x <= '9' && maclvl > -1) /* Digit is macro arg */	      z = ( (m_arg[maclvl][x - '0'] != (char *)0)		   && (int)strlen(m_arg[maclvl][x - '0']) != 0);	    else			/* Otherwise it's a global variable */	      z = ( (g_var[x] != (char *)0)		   &&  (int)strlen(g_var[x]) != 0 );	} else if (*lp == '&') {	/* Array reference */	    int cc, nn;	    if (arraynam(lp,&cc,&nn) < 0)	      z = 0;	    else z = (arrayval(cc,nn) == NULL ? 0 : 1);	}#else        if ((int)strlen(s) < 1) return(-2);	z = 0;				/* Assume failure. */	if (*s == CMDQ) {		/* Object begins with backslash. */	    char c;	    c = s[1];			/* Character following backslash */	    if (c) {		c = islower(c) ? toupper(c) : c;		if (c == '%' ||		/* Simple variable */		    c == '&' ||		/* Array element */		    c == '$' ||		/* Environment variable */		    c == 'V' ||		/* Builtin named variable */		    c == 'M' ||		/* Macro name */		    c == 'F') {		/* Builtin function */		    int t;		/* Let xxstring() evaluate it */		    t = LINBUFSIZ-1;	/* This lets us test \v(xxx) */		    lp = line;		/* and even \f...(xxx) */		    xxstring(s,&lp,&t);		    t = strlen(line);		    debug(F111,"IF DEF",line,t);		    z = t > 0;		}	    } 	} #endif /* COMMENT */	else {			/* Otherwise it's a macro name */	    z = ( mxlook(mactab,s,nmac) > -1 ); /* Look for exact match */	}	debug(F111,"if defined",s,z);	ifargs += 2;	break;      case XXIFBG:			/* IF BACKGROUND */      case XXIFFG:			/* IF FOREGROUND */    	bgchk();			/* Check background status */	if (ifc == XXIFFG)		/* Foreground */	  z = pflag ? 1 : 0;        else z = pflag ? 0 : 1;		/* Background */	ifargs += 1;	break;      case XXIFCO:			/* IF COUNT */	z = ( --count[cmdlvl] > 0 );	debug(F101,"if count","",z);	ifargs += 1;	break;      case XXIFEX:			/* IF EXIST */	if ((x = cmfld("File","",&s,xxstring)) < 0) {	    if (x == -3) {		printf("?Filename required\n");		return(-9);	    } else return(x);	}	z = ( zchki(s) > -1L );	debug(F101,"if exist","",z);	ifargs += 2;	break;      case XXIFEQ: 			/* IF EQUAL (string comparison) */      case XXIFLL:			/* IF Lexically Less Than */      case XXIFLG:			/* If Lexically Greater Than */	if ((x = cmfld("first word or variable name","",&s,xxstring)) < 0) {	    if (x == -3) {		printf("?Text required\n");		return(-9);	    } else return(x);	}	x = (int)strlen(s);	if (x > LINBUFSIZ-1) {	    printf("?IF: strings too long\n");	    return(-2);	}	lp = line;			/* lp points to first string */	strcpy(lp,s);	if ((y = cmfld("second word or variable name","",&s,xxstring)) < 0) {	    if (y == -3) {		printf("?Text required\n");		return(-9);	    } else return(y);	}	y = (int)strlen(s);	if (x + y + 2 > LINBUFSIZ) {	    printf("?IF: strings too long\n");	    return(-2);	}	tp = lp + y + 2;		/* tp points to second string */	strcpy(tp,s);	if (incase)			/* INPUT CASE OBSERVE */	  x = strcmp(lp,tp);	else				/* INPUT CASE IGNORE */	  x = xxstrcmp(lp,tp,(y > x) ? y : x); /* Use longest length */	debug(F101,"IF comparison","",x);	switch (ifc) {	  case XXIFEQ: 			/* IF EQUAL (string comparison) */	    z = (x == 0);	    break;	  case XXIFLL:			/* IF Lexically Less Than */	    z = (x < 0);	    break;	  case XXIFLG:			/* If Lexically Greater Than */	    z = (x > 0);	    break;	}	ifargs += 3;	break;      case XXIFAE:			/* IF (arithmetically) = */      case XXIFLT:			/* IF (arithmetically) < */      case XXIFGT: {			/* IF (arithmetically) > */	/* Really should use longs here... */	/* But cmnum parses ints. */	int n1, n2;	x = cmfld("first number or variable name","",&s,xxstring);	if (x == -3) {	    printf("?Quantity required\n");	    return(-9);	}	if (x < 0) return(x);	debug(F101,"xxifgt cmfld","",x);	lp = line;	strcpy(lp,s);	debug(F110,"xxifgt exp1",lp,0);	if (!xxstrcmp(lp,"count",5)) {	    n1 = count[cmdlvl];	} else if (!xxstrcmp(lp,"version",7)) {	    n1 = (int) vernum;	} else if (!xxstrcmp(lp,"argc",4)) {	    n1 = (int) macargc[maclvl];	} else {	    if (!chknum(lp)) return(-2);	    n1 = atoi(lp);	}	y = cmfld("second number or variable name","",&s,xxstring);	if (y == -3) {	    printf("?Quantity required\n");	    return(-9);	}	if (y < 0) return(y);        if ((int)strlen(s) < 1) return(-2);	x = (int)strlen(lp);	tp = line + x + 2;	strcpy(tp,s);	debug(F110,"xxifgt exp2",tp,0);	if (!xxstrcmp(tp,"count",5)) {	    n2 = count[cmdlvl];	} else if (!xxstrcmp(tp,"version",7)) {	    n2 = (int) vernum;	} else if (!xxstrcmp(tp,"argc",4)) {	    n2 = (int) macargc[maclvl];	} else {	    if (!chknum(tp)) return(-2);	    n2 = atoi(tp);	}	debug(F101,"xxifft ifc","",ifc);	z = ((n1 <  n2 && ifc == XXIFLT)	  || (n1 == n2 && ifc == XXIFAE)	  || (n1 >  n2 && ifc == XXIFGT));	debug(F101,"xxifft n1","",n1);	debug(F101,"xxifft n2","",n2);	debug(F101,"xxifft z","",z);	ifargs += 3;	break; }      case XXIFNU:			/* IF NUMERIC */	x = cmfld("variable name or constant","",&s,xxstring);	if (x == -3) {	    printf("?Quantity required\n");	    return(-9);	}	if (x < 0) return(x);	debug(F111,"xxifnu cmfld",s,x);	lp = line;	strcpy(lp,s);	debug(F110,"xxifnu quantity",lp,0);        z = chknum(lp);        debug(F101,"xxifnu chknum","",z);	ifargs += 2;	break;      default:				/* Shouldn't happen */	return(-2);    }    switch (cx) {			/* Separate handling for IF and XIF */      case XXIF:			/* This is IF... */	ifcmd[cmdlvl] = 1;		/* We just completed an IF command */	if (not) z = !z;		/* Handle NOT here */	if (z) {			/* Condition is true */	    iftest[cmdlvl] = 1;		/* Remember that IF succeeded */	    if (maclvl > -1) {		/* In macro, */		pushcmd();		/* save rest of command. */	    } else if (tlevel > -1) {	/* In take file, */		debug(F100, "doif: pushing command", "", 0);		pushcmd();		/* save rest of command. */	    } else {			/* If interactive, */		cmini(ckxech);		/* just start a new command */		printf("\n");		/* (like in MS-DOS Kermit) */		if (pflag) prompt(xxstring);	    }	} else {			/* Condition is false */	    iftest[cmdlvl] = 0;		/* Remember command failed. */	    if ((y = cmtxt("command to be ignored","",&s,NULL)) < 0)	      return(y);		/* Gobble up rest of line */	}	return(0);      case XXIFX: {			/* This is XIF (Extended IF) */	  char *p;	  char e[5];	  int i;	  if ((y = cmtxt("Object command","",&s,NULL)) < 0)	    return(y);			/* Get object command. */	  p = s;	  lp = line;	  if (litcmd(&p,&lp) < 0) {	/* Insert quotes in THEN-part */	      return(-2);	  }	  if (!z) {			/* Use ELSE-part, if any */	      lp = line;		/* Write over THEN part. */	      *lp = NUL;	      while (*p == SP) p++;	/* Strip trailing spaces */	      if (*p) {			/* At end? */		  for (i = 0; i < 4; i++) e[i] = *p++; /* No, check for ELSE */		  if (xxstrcmp(e,"else",4)) return(-2);	/* No, syntax error */		  if (litcmd(&p,&lp) < 0) { /* Insert quotes */		      return(-2);		  }		  while (*p == SP) p++;	/* Strip trailing spaces */		  if (*p) return(-2);	/* Should be nothing here. */	      }	  }	  if (line[0]) {	      x = mlook(mactab,"_xif",nmac); /* get index of "_xif" macro. */	      if (x < 0) {			/* Not there? */		  addmmac("_xif",xif_def);	/* Put it back. */		  if (mlook(mactab,"_xif",nmac) < 0) { /* Look it up again. */		      printf("?XIF macro gone!\n");		      return(success = 0);		  }	      }	      dodo(x,line);		/* Do the XIF macro */	  }	  return(0);      }      case XXWHI: {			/* WHILE Command */	  p = cmdbuf;			/* Capture IF condition */	  ifcond[0] = NUL;		/* from command buffer */	  while (*p == SP) p++;	  while (*p != SP) p++;	  ifcp = ifcond;	  strcpy(ifcp,"{ \\flit(if not ");	  ifcp += (int)strlen(ifcp);	  while (*p != '{' && *p != NUL) *ifcp++ = *p++;	  p = " goto wbot) } ";	  while (*ifcp++ = *p++) ;	  debug(F110,"WHILE cmd",ifcond,0);	  if ((y = cmtxt("Object command","",&s,NULL)) < 0)	    return(y);			/* Get object command. */	  p = s;	  lp = line;	  if (litcmd(&p,&lp) < 0) {	/* Insert quotes in object command */	      return(-2);	  }	  debug(F110,"WHILE body",line,0);	  if (line[0]) {	      char *p;	      x = mlook(mactab,"_while",nmac); /* index of "_while" macro. */	      if (x < 0) {		/* Not there? */		  addmmac("_while",whil_def); /* Put it back. */		  if (mlook(mactab,"_while",nmac) < 0) { /* Look it up again */		      printf("?WHILE macro definition gone!\n");		      return(success = 0);		  }	      }	      p = malloc((int)strlen(ifcond) + (int)strlen(line) + 2);	      if (p) {		  strcpy(p,ifcond);		  strcat(p,line);		  debug(F110,"WHILE dodo",p,0);		  dodo(x,p);		  free(p);	      } else {		  printf("?Can't allocate storage for WHILE command");		  return(success = 0);	      }	  }	  return(0);      }      default:	return(-2);    }}#endif /* NOSPL *//* Set up a TAKE command file */intdotake(s) char *s; {    if ((tfile[++tlevel] = fopen(s,"r")) == NULL) {	perror(s);	debug(F110,"Failure to open",s,0);	success = 0;	tlevel--;    } else {#ifdef VMS	conres();			/* So Ctrl-C will work */#endif /* VMS */#ifndef NOSPL	cmdlvl++;			/* Entering a new command level */	if (cmdlvl > CMDSTKL) {	    cmdlvl--;	    printf("?TAKE files and/or DO commands nested too deeply\n");	    return(success = 0);	}	if (tfnam[tlevel]) free(tfnam[tlevel]);	/* Copy the filename */	if (tfnam[tlevel] = malloc(strlen(s) + 1))	  strcpy(tfnam[tlevel],s);	ifcmd[cmdlvl] = 0;		/* Set variables for this cmd file */	iftest[cmdlvl] = 0;	count[cmdlvl] = 0;	cmdstk[cmdlvl].src = CMD_TF;	/* Say we're in a TAKE file */	cmdstk[cmdlvl].lvl = tlevel;	/* nested at this level */#endif /* NOSPL */    }    return(1);}#endif /* NOICP */

⌨️ 快捷键说明

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