📄 docmd.c
字号:
srclen = strlen(src); while(*src) { if (((*src == '$') || (*src == '%')) && (*(src-1) != '\\')) { varname = src+1; value = shellsym_chk(*src,varname,&namesize,buf1,sizeof(buf1)); if (value) { if (((srclen - namesize) + strlen(value)) >= CMDLINESIZE) { printf("Cmd line expansion overflow\n"); return(-1); } strcpy(buf,varname+namesize); sprintf(varname-1,"%s%s",value,buf); return(1); } } src++; } return(0);}/* processbraces(): * Look into the incoming string for the deepest set of braces and * substitute that with the value stored in the corresponding shell * variable. Return 1 if a set of braces was processed; else 0 indicating * that all braces have been processed. Return -1 if there is some kind * of processing error (buffer overflow). */intprocessbraces(char *src){ int namesize, srclen, result; char *base, *cp1, *cp2, *varname, *value, type; char buf[CMDLINESIZE], buf1[CMDLINESIZE], buf2[CMDLINESIZE]; type = 0; base = src; varname = src; srclen = strlen(src); while(*src) { if (((*src == '$') || (*src == '%')) && (*(src+1) == '{')) { type = *src; varname = src+2; src++; } else if (*src == '}') { cp1 = varname; cp2 = buf1; while(cp1 < src) *cp2++ = *cp1++; *cp2 = 0; while((result = processprefixes(buf1)) == 1); if (result == -1) return(-1); strcpy(buf,src); sprintf(varname,"%s%s",buf1,buf); value = shellsym_chk(type,varname,&namesize,buf2,sizeof(buf2)); /* If the shellvar or symbol exists, replace it; else remove it. */ if (value) { if (((srclen-(namesize+3))+strlen(value)+1) > CMDLINESIZE) { printf("Cmd line expansion overflow\n"); return(-1); } strcpy(buf1,varname+namesize+1); sprintf(varname-2,"%s%s",value,buf1); } else { strcpy(varname-2,src+1); } return(1); } else if (*src == '\\') { if ((*(src+1) == '$') || (*(src+1) == '%') || (*(src+1) == '\\') || (*(src+1) == '{') || (*(src+1) == '}')) { src++; } } src++; } return(0);}/* expandshellvars(): * Passed a string that is to be expanded with all shell variables converted. * This function supports variables of type $VARNAME and ${VARNAME}. * It also allows variables to be embedded within variables. For example... * ${VAR${NAME}} will be a 2-pass expansion in which ${NAME} is evaluated * and then ${VARXXX} (where XXX is whatever was in variable NAME) is * processed. */intexpandshellvars(char *newstring){ char *cp; int result, cno, ndp; /* Verify that there is a balanced set of braces in the incoming * string... */ ndp = 0; if (braceimbalance(newstring,&cno,&ndp)) { printf("Brace imbalance @ %d%s.\n", cno,ndp ? " ({ missing $ or %)" : ""); return(-1); } /* Process the variable names within braces... */ while((result = processbraces(newstring)) == 1); if (result == -1) return(-1); /* Process dollar signs (left-most first)... */ while((result = processprefixes(newstring)) == 1); if (result == -1) return(-1); /* Cleanup any remaining "\{", "\}" or "\$" strings... */ cp = newstring+1; while(*cp) { if (*cp == '{' || *cp == '}' || *cp == '$' || *cp == '%') { if (*(cp-1) == '\\') { strcpy(cp-1,cp); cp -= 2; } } cp++; } return(0);}/* tokenize(): * Take the incoming string and create an argv[] array from that. The * incoming string is assumed to be writeable. The argv[] array is simple * a set of pointers into that string, where the whitespace delimited * character sets are each NULL terminated. */inttokenize(char *string,char *argv[]){ int argc, done; /* Null out the incoming argv array. */ for(argc=0;argc<ARGCNT;argc++) argv[argc] = (char *)0; argc = 0; while(1) { while ((*string == ' ') || (*string == '\t')) string++; if (*string == 0) break; argv[argc] = string; while ((*string != ' ') && (*string != '\t')) { if ((*string == '\\') && (*(string+1) == '"')) { strcpy(string,string+1); } else if (*string == '"') { strcpy(string,string+1); while(*string != '"') { if ((*string == '\\') && (*(string+1) == '"')) strcpy(string,string+1); if (*string == 0) return(-1); string++; } strcpy(string,string+1); continue; } if (*string == 0) break; string++; } if (*string == 0) done = 1; else { done = 0; *string++ = 0; } argc++; if (done) break; if (argc >= ARGCNT) { argc = -1; break; } } return(argc);}/* Help(): * This command displays each commands help text. * The help text is assumed to be formatted as an array of strings * where... * the first string is the command description; * the second string is command usage; * and all remaining strings up to the NULL are just printable text. */char *HelpHelp[] = { "Display command set", "-[d] [commandname]", "Options:", " -d list commands and descriptions", 0,};intHelp(int argc,char *argv[]){ struct monCommand *cptr, *acptr; char *p; int j, i, foundit, opt, descriptions; descriptions = 0; while((opt=getopt(argc,argv,"d")) != -1) { switch(opt) { case 'd': descriptions = 1; break; default: return(CMD_PARAM_ERROR); } } cptr = cmdlist; if (argc == optind) { foundit = 1; if (descriptions) { while(cptr->name) { showhelp(cmdlist,cptr-cmdlist,1); cptr++; } if (appCmdlist) { acptr = appCmdlist; for(i=0;acptr->name;acptr++,i++) showhelp(appCmdlist,i,1); } putchar('\n'); } else { i = 0; while(1) { if (showhelp(cmdlist,cptr-cmdlist,0)) { if ((++i%6) == 0) putchar('\n'); } cptr++; if (cptr->name == 0L) break; } if (appCmdlist) { acptr = appCmdlist; for(j=0;acptr->name;acptr++,j++) { if (showhelp(appCmdlist,j,0)) { if ((++i%6) == 0) putchar('\n'); } } } putchar('\n'); } } else { foundit = 0; p = argv[1]; while(cptr->name) { if (strcmp(cptr->name,argv[1]) == 0) { foundit = showhelp(cmdlist,cptr-cmdlist,2); break; } cptr++; } if (!cptr->name) { if (appCmdlist) { acptr = appCmdlist; for(i=0;acptr->name;acptr++,i++) { if (strcmp(acptr->name,argv[1]) == 0) { foundit = showhelp(appCmdlist,i,2); break; } } } } } if (!foundit) { printf("\"%s\" not found\n",argv[1]); return(CMD_FAILURE); } return(CMD_SUCCESS);}/* showhelp(): * Called by Help() when it is time to print out some verbosity level of * a command's help text. * if... * verbose == 2, then print all the help text; * verbose == 1, then print the command name and abstract; * verbose == 0, then print only the command name; */intshowhelp(struct monCommand *list,int index,int verbose){ char **hp; int lcnt; struct monCommand *cptr; cptr = &list[index]; /* If the incoming command list is that of the monitor, then check * for user level... */ if ((list == cmdlist) && (cmdUlvl[index] > getUsrLvl())) return(0); if (verbose == 2) { printf("%s\n", cptr->helptxt[0]); showusage(cptr); lcnt = 0; hp = &cptr->helptxt[2]; while(*hp) { printf("%s\n",*hp++); if (++lcnt == 18) { if (!More()) break; lcnt = 0; } } printf("\nRequired user level: "); if (list == cmdlist) printf("%d\n\n",cmdUlvl[index]); else printf("NA\n\n"); } else if (verbose == 1) { if (list == cmdlist) printf(" %-12s %d %s\n", cptr->name, cmdUlvl[index], cptr->helptxt[0]); else printf(" %-12s %s\n", cptr->name,cptr->helptxt[0]); } else { printf("%-12s",cptr->name); } return(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -