📄 if.c
字号:
* Basically, the items can be thought of as a table. The value of idx * (starting with 1) is used to index into the list of items and place * that item in the shell variable "var". * Syntax: * item {idx} {var} {item1} {item2} {item3} .... */char *ItemHelp[] = { "Extract an item from a list", "{idx} {stor_var} [item1] [item2] ...", "Note: idx=1 retrieves item1", 0,};intItem(int argc, char *argv[]){ int idx; char *varname; if (argc < 3) return(CMD_PARAM_ERROR); idx = atoi(argv[1]); varname = argv[2]; if ((idx < 1) || (idx > (argc-3))) { setenv(varname,0); return(CMD_SUCCESS); } idx += 2; setenv(varname,argv[idx]); return(CMD_SUCCESS);}/* Read(): * Simple interactive shell variable entry. * Syntax: * read [options] [var1] [var2] [...] * Options: * -c wait for any character to be received. * -twww timeout after 'www' milliseconds (approximate) of * waiting for input. * */char *ReadHelp[] = { "Interactive shellvar entry", "-[ct:] {var1} [var2] ... ", " -c wait for any input", " -t ### initial msec timeout", " -T ### msec timeout per char", 0,};intRead(argc,argv)int argc;char *argv[];{ int i, reached_eol, opt, waitfor1, waitfor2; char buf[64], *space, *bp; waitfor1 = waitfor2 = 0; while((opt=getopt(argc,argv,"ct:T:")) != -1) { switch(opt) { case 'c': /* Wait for any character entered (like hitakey). */ while(!gotachar()) #if INCLUDE_ETHERNET pollethernet()#endif ; getchar(); /* Flush it from the input buffer, once received. */ break; case 't': waitfor1 = strtol(optarg,(char **)0,0); waitfor1 *= LoopsPerSecond/1000; break; case 'T': waitfor2 = strtol(optarg,(char **)0,0); waitfor2 *= LoopsPerSecond/1000; break; default: return(CMD_PARAM_ERROR); } } if (argc == optind) return(CMD_SUCCESS); /* Timeout waiting for first character... */ if (waitfor1) { while(!gotachar()) { if (waitfor1-- <= 0) return(CMD_FAILURE);#if INCLUDE_ETHERNET pollethernet();#endif } } /* Timeout waiting for every character... */ if (getline_t(buf,sizeof(buf)-1,waitfor2) == 0) return(CMD_FAILURE); bp = buf; reached_eol = 0; for(i=optind;i<argc;i++) { space=strpbrk(bp," \t"); if (space) { *space = 0; setenv(argv[i],bp); bp = space+1; } else { if (!reached_eol) { setenv(argv[i],bp); reached_eol = 1; } else { setenv(argv[i],0); } } } return(CMD_SUCCESS);}/* gototag(): * Used with tfsscript to allow a command to adjust the pointer into the * script that is currently being executed. It simply populates the * "ScriptGotoTag" pointer with the tag that should be branched to next. */voidgototag(char *tag){ if (InAScript()) { if (ScriptGotoTag) free(ScriptGotoTag); ScriptGotoTag = malloc(strlen(tag)+8); sprintf(ScriptGotoTag,"# %s",tag); }}/* gosubtag(): * Similar in basic use to gototag(), except that we keep a copy of the * current position in the active script file so that it can be returned * to later. */voidgosubtag(char *tag){ if (InAScript()) { if (ReturnToDepth >= MAXGOSUBDEPTH) { printf("Max return-to depth reached\n"); return; } ReturnToTbl[ReturnToDepth] = tfstell(ReturnToTfd); ReturnToDepth++; gototag(tag); }}voidgosubret(char *ignored){ if (InAScript()) { if (ReturnToDepth <= 0) printf("Nothing to return to\n"); else { ReturnToDepth--; tfsseek(ReturnToTfd, ReturnToTbl[ReturnToDepth], TFS_BEGIN); } }}/* tfsscript(): * Treat the file as a list of commands that should be executed * as monitor commands. Step through each line and execute it. * The tfsDocommand() function pointer is used so that the application * can assign a different command interpreter to the script capbilities * of the monitor. To really take advantage of this, the command interpreter * that is reassigned, should allow monitor commands to run if the command * does not exist in the application's command list. * * Scripts can call other scripts that call other scripts (etc...) and * that works fine because tfsscript() will just use more stack space but * it eventually returns through the same function call tree. A script can * also call a COFF, ELF or AOUT file and expect it to return as long as * the executable returns through the same point into which it was started * (the entrypoint). If the executable uses mon_appexit() to terminate, * then the calling script will not regain control. */inttfsscript(TFILE *fp,int verbose){ char lcpy[CMDLINESIZE], *sv; int tfd, lnsize, lno, verbosity, rslt; /* TFS does not support calling a script from within a subroutine. */ if (ReturnToDepth != 0) return(TFSERR_SCRIPTINSUB); tfd = tfsopen(fp->name,TFS_RDONLY,0); if (tfd < 0) return(tfd); lno = 0; ReturnToTfd = tfd; ScriptIsRunning++; while(1) { lno++; lnsize = tfsgetline(tfd,lcpy,CMDLINESIZE); if (lnsize == 0) /* end of file? */ break; if (lnsize < 0) { printf("tfsscript(): %s\n",tfserrmsg(lnsize)); break; } if ((lcpy[0] == '\r') || (lcpy[0] == '\n')) /* empty line? */ continue; lcpy[lnsize-1] = 0; /* Remove the newline */ /* Just in case the goto tag was set outside a script, */ /* clear it now. */ if (ScriptGotoTag) { free(ScriptGotoTag); ScriptGotoTag = (char *)0; } ScriptExitFlag = 0; /* Execute the command line. * If the shell variable "SCRIPTVERBOSE" is set, then enable * verbosity for this command; else use what was passed in * the parameter list of the function. Note that the variable * is tested for each command so that verbosity can be enabled * or disabled within a script. * If the command returns a status that indicates that there was * some parameter error, then exit the script. */ sv = getenv("SCRIPTVERBOSE"); if (sv) verbosity = atoi(sv); else verbosity = verbose; rslt = tfsDocommand(lcpy,verbosity); switch(rslt) { case CMD_PARAM_ERROR: case CMD_NOT_FOUND: printf("Terminating script '%s' at line %d\n", TFS_NAME(fp),lno); ScriptExitFlag = 1; break; } /* Check for exit flag. If set, then in addition to terminating the * script, clear the return depth here so that the "missing return" * warning is not printed. This is done because there is likely * to be a subroutine with an exit in it and this should not * cause a warning. */ if (ScriptExitFlag) { ReturnToDepth = 0; break; } /* If ScriptGotoTag is set, then attempt to reposition the line * pointer to the line that contains the tag. */ if (ScriptGotoTag) { int tlen; tlen = strlen(ScriptGotoTag); tfsseek(tfd,0,TFS_BEGIN); while(1) { lnsize = tfsgetline(tfd,lcpy,CMDLINESIZE); if (lnsize == 0) { printf("Tag '%s' not found\n",ScriptGotoTag+2); free(ScriptGotoTag); ReturnToDepth = 0; ScriptGotoTag = (char *)0; tfsclose(tfd,0); return(TFS_OKAY); } if (!strncmp(lcpy,ScriptGotoTag,tlen)) { free(ScriptGotoTag); ScriptGotoTag = (char *)0; break; } } }#if INCLUDE_ETHERNET /* After each line, poll ethernet interface. */ pollethernet();#endif } tfsclose(tfd,0); if (ScriptExitFlag & REMOVE_SCRIPT) tfsunlink(fp->name); if (ReturnToDepth != 0) { printf("Warning: '%s' missing return.\n",fp->name); ReturnToDepth = 0; } ScriptIsRunning--; return(TFS_OKAY);}#else /* INCLUDE_TFSSCRIPT */inttfsscript(TFILE *fp,int verbose){ return(TFSERR_NOTAVAILABLE);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -