void Console::registerAlias(const std::string &name, const std::string &cmd) { AliasRecord *a = findAlias(name); if (a == nullptr) { verifyRegistration(CON_Lang("Alias registration"), name, false); AliasRecord newAlias(name, cmd); // Zaradi novy alias tak, aby byly setridene podle abecedy size_t position = 0; for (const AliasRecord &alias : aliases) { if (alias.getName().compare(name) > 0) { break; } ++position; } aliases.insert(aliases.begin() + position, newAlias); } else { a->setCommand(cmd); } if (hasFlag(RegInfoFlag)) { print(CON_Format(CON_Lang("Alias registration: \"{0}\" as \"{1}\" has been successful\n")) << name << cmd); } }
HRESULT BusAssignmentManager::State::autoAssign(const char* pszName, PCIBusAddress& Address) { PCIRulesList matchingRules; addMatchingRules(pszName, matchingRules); const char* pszAlias = findAlias(pszName); if (pszAlias) addMatchingRules(pszAlias, matchingRules); AssertMsg(matchingRules.size() > 0, ("No rule for %s(%s)\n", pszName, pszAlias)); stable_sort(matchingRules.begin(), matchingRules.end(), RuleComparator); for (size_t iRule = 0; iRule < matchingRules.size(); iRule++) { const DeviceAssignmentRule* rule = matchingRules[iRule]; Address.miBus = rule->iBus; Address.miDevice = rule->iDevice; Address.miFn = rule->iFn; if (checkAvailable(Address)) return S_OK; } AssertMsgFailed(("All possible candidate positions for %s exhausted\n", pszName)); return E_INVALIDARG; }
void checkAlias() { int i,j,n,t,u; char path[100]; int count=0; for(i=0;i<countCmd;++i) { for(j=0;j<cmds[i].numArg;++j) { int ind = findAlias(tokens[cmds[i].args[j]]); if(ind!=-1) { //printf("meet alias : %s\n",tokens[cmds[i].args[j]]); count++; for(u=j;u<cmds[i].numArg-1;++u) { cmds[i].args[u]=cmds[i].args[u+1]; } cmds[i].numArg--; t=0; n=0; int l=strlen(alias[ind][1]); while(1) { t=0; while(t+n<l&&alias[ind][1][t+n]!=' ') { path[t]=alias[ind][1][t+n]; t++; } path[t]='\0'; //printf("%s\n",path); strcpy(tokens[countToken++],path); int u=cmds[i].numArg; while(u>j) { cmds[i].args[u]=cmds[i].args[u-1]; u--; } cmds[i].args[j]= countToken-1; j++; cmds[i].numArg++; n+=t+1; if(n>=l) { j--; break; } } } } } if(count>0) checkAlias(); }
/* * LookupAlias - return the string associated with an identifier or NULL if * no alias exists */ char *LookupAlias( AliasHdl hdl, unsigned long id ) { AnAlias *cur; cur = findAlias( hdl, id ); if( cur == NULL ) { return( NULL ); } return( cur->name ); } /* LookupAlias */
void Console::verifyRegistration(const std::string &proc, const std::string &name, bool isNull) { if (name.empty()) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : cannot register empty name")) << proc); } if (findCommand(name) != nullptr || findVar(name) != nullptr || findAlias(name) != nullptr) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : name \"{1}\" is already registered")) << proc << name); } if (isNull) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : attempt to register \"{1}\" with null pointer")) << proc << name); } }
void do_unalias(int argc, const char ** argv) { Alias * alias; while (--argc > 0) { alias = findAlias(*++argv); if (alias == NULL) continue; free(alias->name); free(alias->value); aliasCount--; alias->name = aliasTable[aliasCount].name; alias->value = aliasTable[aliasCount].value; } }
void runAlias(Node * head, char * s, char * path){ char command[1000]; strcpy(command, findAlias(&head, s)); char ** argv; int argc; argc = makeargs(command, &argv); #if TESTER printf("command = %s\n", command); printf("ARGC = %d<----\n", argc); printargs(argc, argv); #endif runCommand(argv, argc, path); }
/* * AddAlias - add an alias to an alias list * - if an alias already exists for this identifier replace it */ void AddAlias( AliasHdl hdl, char *text, unsigned long id ) { AnAlias *cur; size_t len; cur = findAlias( hdl, id ); if( cur == NULL ) { cur = MemAlloc( sizeof( AnAlias ) ); cur->id = id; insertAlias( hdl, cur ); if( hdl->updatefn != NULL ) { hdl->updatefn( id, text, NULL, hdl->userdata ); } } else { if( hdl->updatefn != NULL ) { hdl->updatefn( id, text, cur->name, hdl->userdata ); } MemFree( cur->name ); } len = strlen( text ) + 1; cur->name = MemAlloc( len ); strcpy( cur->name, text ); } /* AddAlias */
int buildInCmd() { if(countCmd!=1) return 0; if(strcmp(tokens[cmds[0].args[0]],"setenv")==0) { if(cmds[0].numArg!=3) { int i; for(i=3;i<cmds[0].numArg;++i) { strcat(tokens[cmds[0].args[2]]," "); strcat(tokens[cmds[0].args[2]],tokens[cmds[0].args[i]]); } cmds[0].numArg=3; } int ind = findVar(tokens[cmds[0].args[1]]); if(ind==-1) { strcpy(ShellVariable[countVar][0],tokens[cmds[0].args[1]]); strcpy(ShellVariable[countVar++][1],tokens[cmds[0].args[2]]); } else { strcpy(ShellVariable[ind][0],tokens[cmds[0].args[1]]); strcpy(ShellVariable[ind][1],tokens[cmds[0].args[2]]); } return 1; } if(strcmp(tokens[cmds[0].args[0]],"printenv")==0) { if(cmds[0].numArg!=1) { printf("Error : No parameters for printenv \n"); return 1; } int i=0; for(i=0;i<countVar;++i) printf("%s : %s \n",ShellVariable[i][0],ShellVariable[i][1]); return 1; } if(strcmp(tokens[cmds[0].args[0]],"unsetenv")==0) { if(cmds[0].numArg!=2) { return 1; } int i=0; for(i=0;i<countVar;++i) if(strcmp(tokens[cmds[0].args[1]],ShellVariable[i][0])==0) { countVar--; strcpy(ShellVariable[i][0],ShellVariable[countVar][0]); strcpy(ShellVariable[i][1],ShellVariable[countVar][1]); } return 1; } if(strcmp(tokens[cmds[0].args[0]],"cd")==0) { char buf[200]; strcpy(buf,currentPath); if(cmds[0].numArg==2) { int i=0; if(tokens[cmds[0].args[1]][0]=='/') strcpy(currentPath,tokens[cmds[0].args[1]]); else if(tokens[cmds[0].args[1]][0]=='~') { int ind = cmds[0].args[1]; if(strlen(tokens[ind])>1&&tokens[ind][1]=='/') { strcpy(currentPath,getenv("HOME")); strcat(currentPath,&tokens[cmds[0].args[1]][1]); } else{ char userName[100]; int u; for(u=1;u<strlen(tokens[ind]);++u) if(tokens[ind][u]=='/') break; else userName[u-1]=tokens[ind][u]; userName[u-1]='\0'; struct passwd *pw; uid_t uid; uid = geteuid (); pw = getpwuid (uid); //printf("user name %s -> %s\n",userName,pw->pw_name); if(strcmp(userName,pw->pw_name)!=0) { printf("File does not exist\n"); strcpy(currentPath,buf); return 1; } strcpy(currentPath,getenv("HOME")); if(u!=strlen(tokens[ind])) strcat(currentPath,&tokens[ind][u]); } } else { strcat(currentPath,"/"); strcat(currentPath,tokens[cmds[0].args[1]]); } } else strcpy(currentPath,getenv("HOME")); //printf("current path %s\n",currentPath); if(chdir (currentPath)!=0) { printf("File does not exist\n"); strcpy(currentPath,buf); chdir(currentPath); return 1; } return 1; } if(strcmp(tokens[cmds[0].args[0]],"alias")==0) { if(cmds[0].numArg>3) { int u; for(u=3;u<cmds[0].numArg;++u) { strcat(tokens[cmds[0].args[2]]," "); strcat(tokens[cmds[0].args[2]],tokens[cmds[0].args[u]]); } cmds[0].numArg=3; } if(cmds[0].numArg==1) { int i; for(i=0;i<countAlias;++i) printf("%s : %s\n",alias[i][0],alias[i][1]); return 1; } int ind = findAlias(tokens[cmds[0].args[1]]); if(ind!=-1) { strcpy(alias[ind][1],tokens[cmds[0].args[1]]); } else { strcpy(alias[countAlias][0],tokens[cmds[0].args[1]]); strcpy(alias[countAlias++][1],tokens[cmds[0].args[2]]); } return 1; } if(strcmp(tokens[cmds[0].args[0]],"unalias")==0) { if(cmds[0].numArg!=2) { printf("Not an alias name\n"); return 1; } int ind = findAlias(tokens[cmds[0].args[1]]); //printf("%d %s \n",ind,tokens[cmds[0].args[1]]); // now can you compile and use alias and unalias ok i saved it if(ind!=-1) { countAlias--; strcpy(alias[ind][0],alias[countAlias][0]); strcpy(alias[ind][1],alias[countAlias][1]); } return 1; } if(strcmp(tokens[cmds[0].args[0]],"bye")==0) { exit(0); return 1; } return 0; }
void do_alias(int argc, const char ** argv) { const char * name; char * value; Alias * alias; int count; char buf[CMD_LEN]; if (argc < 2) { count = aliasCount; for (alias = aliasTable; count-- > 0; alias++) printf("%s\t%s\n", alias->name, alias->value); return; } name = argv[1]; if (argc == 2) { alias = findAlias(name); if (alias) printf("%s\n", alias->value); else fprintf(stderr, "Alias \"%s\" is not defined\n", name); return; } if (strcmp(name, "alias") == 0) { fprintf(stderr, "Cannot alias \"alias\"\n"); return; } if (!makeString(argc - 2, argv + 2, buf, CMD_LEN)) return; value = malloc(strlen(buf) + 1); if (value == NULL) { fprintf(stderr, "No memory for alias value\n"); return; } strcpy(value, buf); alias = findAlias(name); if (alias) { free(alias->value); alias->value = value; return; } if ((aliasCount % ALIAS_ALLOC) == 0) { count = aliasCount + ALIAS_ALLOC; if (aliasTable) { alias = (Alias *) realloc(aliasTable, sizeof(Alias) * count); } else alias = (Alias *) malloc(sizeof(Alias) * count); if (alias == NULL) { free(value); fprintf(stderr, "No memory for alias table\n"); return; } aliasTable = alias; } alias = &aliasTable[aliasCount]; alias->name = malloc(strlen(name) + 1); if (alias->name == NULL) { free(value); fprintf(stderr, "No memory for alias name\n"); return; } strcpy(alias->name, name); alias->value = value; aliasCount++; }
/* * Parse and execute one null-terminated command line string. * This breaks the command line up into words, checks to see if the * command is an alias, and expands wildcards. */ static void command(const char * cmd) { const char * endCmd; const Alias * alias; char newCommand[CMD_LEN]; char cmdName[CMD_LEN]; /* * Rest the interrupt flag and free any memory chunks that * were allocated by the previous command. */ intFlag = FALSE; freeChunks(); /* * Skip leading blanks. */ while (isBlank(*cmd)) cmd++; /* * If the command is empty or is a comment then ignore it. */ if ((*cmd == '\0') || (*cmd == '#')) return; /* * Look for the end of the command name and then copy the * command name to a buffer so we can null terminate it. */ endCmd = cmd; while (*endCmd && !isBlank(*endCmd)) endCmd++; memcpy(cmdName, cmd, endCmd - cmd); cmdName[endCmd - cmd] = '\0'; /* * Search for the command name in the alias table. * If it is found, then replace the command name with * the alias value, and append the current command * line arguments to that. */ alias = findAlias(cmdName); if (alias) { strcpy(newCommand, alias->value); strcat(newCommand, endCmd); cmd = newCommand; } /* * Now look for the command in the builtin table, and execute * the command if found. */ if (tryBuiltIn(cmd)) return; /* * The command is not a built-in, so run the program along * the PATH list. */ runCmd(cmd); }