int set_alias(dbref target, const char *alias, int rotate) { PData pdat; const char *p = alias; /* is the new alias legal? */ while (*p && *p != ':' && *p != PROPDIR_DELIMITER) p++; if (*p || !ok_player_name(alias)) { return NOTHING; } /* is the new alias actually available? */ if (lookup_alias(alias, 0) != NOTHING) { return AMBIGUOUS; } /* set the new alias */ sprintf(abuf, ALIASDIR_CUR "%s", alias); pdat.flags = PROP_REFTYP; pdat.data.ref = target; set_property(0, abuf, &pdat); if (rotate) { rotate_alias(target, 0); /* set the "last alias" hint */ sprintf(abuf, ALIASDIR_LAST "%d", (int) target); add_property(0, abuf, alias, 0); } return 0; }
std::string configcontainer::get_configvalue(const std::string& key) { std::string retval = config_data[lookup_alias(key)].value; if (config_data[key].type == configdata_t::PATH) { retval = utils::resolve_tilde(retval); } return retval; }
Obis::Obis(const char *strClear) { if (parse(strClear) != SUCCESS) { // check alias if (lookup_alias(strClear) == SUCCESS) { } else { throw vz::VZException("Parse ObisString failed."); } } }
/* * Call specified function */ SC_FUNC void ffcall(symbol *sym,const char *label,int numargs) { char symname[2*sNAMEMAX+16]; char aliasname[sNAMEMAX+1]; int wasAlias = 0; assert(sym!=NULL); assert(sym->ident==iFUNCTN); if (sc_asmfile) funcdisplayname(symname,sym->name); if ((sym->usage & uNATIVE)!=0) { /* reserve a SYSREQ id if called for the first time */ assert(label==NULL); stgwrite("\tsysreq.c "); if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; /* Look for an alias */ if (lookup_alias(aliasname, sym->name)) { symbol *asym = findglb(aliasname, sGLOBAL); if (asym && asym->ident==iFUNCTN && ((sym->usage & uNATIVE) != 0)) { sym = asym; if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0) { sym->addr=ntv_funcid++; markusage(sym, uREAD); } } } outval(sym->addr,FALSE); if (sc_asmfile) { stgwrite("\t; "); stgwrite(symname); } /* if */ stgwrite("\n"); /* write on a separate line, to mark a sequence point for the peephole optimizer */ stgwrite("\tstack "); outval((numargs+1)*sizeof(cell), TRUE); code_idx+=opcodes(2)+opargs(2); } else { /* normal function */ stgwrite("\tcall "); if (label!=NULL) { stgwrite("l."); stgwrite(label); } else { stgwrite(sym->name); } /* if */ if (sc_asmfile && (label!=NULL || (!isalpha(sym->name[0]) && sym->name[0]!='_' && sym->name[0]!=sc_ctrlchar))) { stgwrite("\t; "); stgwrite(symname); } /* if */ stgwrite("\n"); code_idx+=opcodes(1)+opargs(1); } /* if */ }
void configcontainer::handle_action(const std::string& action, const std::vector<std::string>& params) { std::string resolved_action = lookup_alias(action); configdata& cfgdata = config_data[resolved_action]; // configdata_t::INVALID indicates that the action didn't exist, and that the returned object was created ad-hoc. if (cfgdata.type == configdata_t::INVALID) { LOG(level::WARN, "configcontainer::handler_action: unknown action %s", action); throw confighandlerexception(action_handler_status::INVALID_COMMAND); } LOG(level::DEBUG, "configcontainer::handle_action: action = %s, type = %u", action, cfgdata.type); if (params.size() < 1) { throw confighandlerexception(action_handler_status::TOO_FEW_PARAMS); } switch (cfgdata.type) { case configdata_t::BOOL: if (!is_bool(params[0])) throw confighandlerexception(strprintf::fmt(_("expected boolean value, found `%s' instead"), params[0])); cfgdata.value = params[0]; break; case configdata_t::INT: if (!is_int(params[0])) throw confighandlerexception(strprintf::fmt(_("expected integer value, found `%s' instead"), params[0])); cfgdata.value = params[0]; break; case configdata_t::ENUM: if (cfgdata.enum_values.find(params[0]) == cfgdata.enum_values.end()) throw confighandlerexception(strprintf::fmt(_("invalid configuration value `%s'"), params[0])); // fall-through case configdata_t::STR: case configdata_t::PATH: if (cfgdata.multi_option) cfgdata.value = utils::join(params, " "); else cfgdata.value = params[0]; break; default: // should not happen throw confighandlerexception(action_handler_status::INVALID_COMMAND); } }
/* * execute walk if word is valid [speed]walk sequence - * return 1 if walked, 0 if not */ int map_walk(char *word, int silent, int maponly) { char buf[16]; int n = strlen(word); int is_main = (tcp_fd == tcp_main_fd); if (!is_main && !maponly && !opt_speedwalk) return 0; if (!n || (n > 1 && !opt_speedwalk && !maponly) || !strchr("neswud", word[n - 1]) || (int)strspn(word, "neswud0123456789") != n) return 0; if (maponly) silent = 1; buf[1] = '\0'; while (*word) { if (!silent) { status(1); tty_putc('['); } if (isdigit(*word)) { n = strtol(word, &word, 10); if (!silent) tty_printf("%d", n); } else n = 1; if (!silent) tty_putc(*word); while (n--) { *buf = *word; if (!maponly) { if (*lookup_alias(buf)) parse_instruction(buf, 1, 0, 0); // we want to execute aliases n,e,s,w,u,d else tcp_write(tcp_fd, buf); } if (is_main || maponly) map_add_dir(*word); } if (!silent) tty_puts("] "); word++; } if (!silent) tty_putc('\n'); return !maponly; }
dbref lookup_player(const char *name) { hash_data *hd; if (*name == '#') { name++; if (!OkObj(atoi(name)) || Typeof(atoi(name)) != TYPE_PLAYER) return NOTHING; else return atoi(name); } else { if (*name == '*') name++; if ((hd = find_hash(name, player_list, PLAYER_HASH_SIZE)) == NULL) { /* secondary check - is there an exact @alias? this will return * NOTHING if no alias was found. */ return lookup_alias(name, 0); } else { return (hd->dbval); } } }
SC_FUNC int assemble(FILE *fout,FILE *fin) { AMX_HEADER hdr; AMX_FUNCSTUB func; int numpublics,numnatives,numoverlays,numlibraries,numpubvars,numtags; int padding; long nametablesize,nameofs; char line[512]; char *instr,*params; int i,pass,size; int16_t count; symbol *sym; symbol **nativelist; constvalue *constptr; cell mainaddr; char nullchar; #if !defined NDEBUG /* verify that the opcode list is sorted (skip entry 1; it is reserved * for a non-existant opcode) */ { #define MAX_OPCODE 176 unsigned char opcodearray[MAX_OPCODE+1]; assert(opcodelist[1].name!=NULL); memset(opcodearray,0,sizeof opcodearray); for (i=2; i<(sizeof opcodelist / sizeof opcodelist[0]); i++) { assert(opcodelist[i].name!=NULL); assert(stricmp(opcodelist[i].name,opcodelist[i-1].name)>0); /* also verify that no opcode number appears twice */ assert((int)opcodelist[i].opcode<=MAX_OPCODE); assert(opcodelist[i].opcode==0 || opcodearray[(int)opcodelist[i].opcode]==0); opcodearray[(int)opcodelist[i].opcode] += 1; } /* for */ } #endif writeerror=FALSE; nametablesize=sizeof(int16_t); numpublics=0; numnatives=0; numpubvars=0; numoverlays=0; mainaddr=-1; /* count number of public and native functions and public variables */ for (sym=glbtab.next; sym!=NULL; sym=sym->next) { int match=0; if (sym->ident==iFUNCTN) { if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)!=0 && sym->index>=0) match=++numnatives; if ((sym->usage & uPUBLIC)!=0 && (sym->usage & uDEFINE)!=0) match=++numpublics; if (pc_overlays>0 && (sym->usage & uNATIVE)==0 && (sym->usage & (uREAD | uPUBLIC))!=0 && (sym->usage & uDEFINE)!=0) { if (strcmp(sym->name,uENTRYFUNC)!=0) ++numoverlays; /* there is no stub function for state entry functions */ if (sym->states!=NULL) { /* for functions with states, write an overlay block for every implementation */ statelist *stlist; for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) ++numoverlays; } /* if */ } /* if */ if (strcmp(sym->name,uMAINFUNC)==0) { assert(sym->vclass==sGLOBAL); mainaddr=(pc_overlays>0) ? sym->index : sym->addr; } /* if */ } else if (sym->ident==iVARIABLE) { if ((sym->usage & uPUBLIC)!=0 && (sym->usage & (uREAD | uWRITTEN))!=0) match=++numpubvars; } /* if */ if (match) { char alias[sNAMEMAX+1]; assert(sym!=NULL); if ((sym->usage & uNATIVE)==0 || !lookup_alias(alias,sym->name)) { assert(strlen(sym->name)<=sNAMEMAX); strcpy(alias,sym->name); } /* if */ nametablesize+=(int)strlen(alias)+1; } /* if */ } /* for */ assert(numnatives==ntv_funcid); /* count number of libraries */ numlibraries=0; if (pc_addlibtable) { for (constptr=libname_tab.next; constptr!=NULL; constptr=constptr->next) { if (constptr->value>0) { assert(strlen(constptr->name)>0); numlibraries++; nametablesize+=(int)strlen(constptr->name)+1; } /* if */ } /* for */ } /* if */ /* count number of public tags */ numtags=0; for (constptr=tagname_tab.next; constptr!=NULL; constptr=constptr->next) { if ((constptr->value & PUBLICTAG)!=0) { assert(strlen(constptr->name)>0); numtags++; nametablesize+=(int)strlen(constptr->name)+1; } /* if */ } /* for */ /* adjust the number of overlays by the special overlays */ if (pc_overlays>0) for (i=0; i<ovlFIRST; i++) if (pc_ovl0size[i][1]!=0) numoverlays++; /* pad the header to sc_dataalign * => thereby the code segment is aligned * => since the code segment is padded to a sc_dataalign boundary, the data segment is aligned * => and thereby the stack top is aligned too */ assert(sc_dataalign!=0); padding= (int)(sc_dataalign - (sizeof hdr + nametablesize) % sc_dataalign); if (padding==sc_dataalign) padding=0; /* write the abstract machine header */ memset(&hdr, 0, sizeof hdr); if (pc_cellsize==2) hdr.magic=(unsigned short)AMX_MAGIC_16; else if (pc_cellsize==4) hdr.magic=(unsigned short)AMX_MAGIC_32; else if (pc_cellsize==8) hdr.magic=(unsigned short)AMX_MAGIC_64; hdr.file_version=CUR_FILE_VERSION; hdr.amx_version=MIN_AMX_VERSION; hdr.flags=(short)(sc_debug & sSYMBOLIC); if (sc_debug==0) hdr.flags|=AMX_FLAG_NOCHECKS; if (pc_memflags & suSLEEP_INSTR) hdr.flags|=AMX_FLAG_SLEEP; if (pc_overlays>0) hdr.flags|=AMX_FLAG_OVERLAY; if (pc_cryptkey!=0) hdr.flags|=AMX_FLAG_CRYPT; hdr.defsize=sizeof(AMX_FUNCSTUB); hdr.publics=sizeof hdr; /* public table starts right after the header */ hdr.natives=hdr.publics + numpublics*sizeof(AMX_FUNCSTUB); hdr.libraries=hdr.natives + numnatives*sizeof(AMX_FUNCSTUB); hdr.pubvars=hdr.libraries + numlibraries*sizeof(AMX_FUNCSTUB); hdr.tags=hdr.pubvars + numpubvars*sizeof(AMX_FUNCSTUB); hdr.overlays=hdr.tags + numtags*sizeof(AMX_FUNCSTUB); hdr.nametable=hdr.overlays + numoverlays*sizeof(AMX_OVERLAYINFO); hdr.cod=hdr.nametable + nametablesize + padding; hdr.dat=(int32_t)(hdr.cod + code_idx); hdr.hea=(int32_t)(hdr.dat + glb_declared*pc_cellsize); hdr.stp=(int32_t)(hdr.hea + pc_stksize*pc_cellsize); hdr.cip=(int32_t)(mainaddr); hdr.size=hdr.hea; pc_writebin(fout,&hdr,sizeof hdr); /* dump zeros up to the rest of the header, so that we can easily "seek" */ nullchar='\0'; for (nameofs=sizeof hdr; nameofs<hdr.cod; nameofs++) pc_writebin(fout,&nullchar,1); nameofs=hdr.nametable+sizeof(int16_t); /* write the public functions table */ count=0; for (sym=glbtab.next; sym!=NULL; sym=sym->next) { if (sym->ident==iFUNCTN && (sym->usage & uPUBLIC)!=0 && (sym->usage & uDEFINE)!=0) { assert(sym->vclass==sGLOBAL); /* in the case of overlays, write the overlay index rather than the address */ func.address=(uint32_t)((pc_overlays>0) ? sym->index : sym->addr); func.nameofs=nameofs; #if BYTE_ORDER==BIG_ENDIAN align32(&func.address); align32(&func.nameofs); #endif pc_resetbin(fout,hdr.publics+count*sizeof(AMX_FUNCSTUB)); pc_writebin(fout,&func,sizeof func); pc_resetbin(fout,nameofs); pc_writebin(fout,sym->name,(int)strlen(sym->name)+1); nameofs+=(int)strlen(sym->name)+1; count++; } /* if */ } /* for */ /* write the natives table */ /* The native functions must be written in sorted order. (They are * sorted on their "id", not on their name). A nested loop to find * each successive function would be an O(n^2) operation. But we * do not really need to sort, because the native function id's * are sequential and there are no duplicates. So we first walk * through the complete symbol list and store a pointer to every * native function of interest in a temporary table, where its id * serves as the index in the table. Now we can walk the table and * have all native functions in sorted order. */ if (numnatives>0) { nativelist=(symbol **)malloc(numnatives*sizeof(symbol *)); if (nativelist==NULL) error(103); /* insufficient memory */ #if !defined NDEBUG memset(nativelist,0,numnatives*sizeof(symbol *)); /* for NULL checking */ #endif for (sym=glbtab.next; sym!=NULL; sym=sym->next) { if (sym->ident==iFUNCTN && (sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)!=0 && sym->index>=0) { assert(sym->index < numnatives); nativelist[(int)sym->index]=sym; } /* if */ } /* for */ count=0; for (i=0; i<numnatives; i++) { char alias[sNAMEMAX+1]; sym=nativelist[i]; assert(sym!=NULL); if (!lookup_alias(alias,sym->name)) { assert(strlen(sym->name)<=sNAMEMAX); strcpy(alias,sym->name); } /* if */ assert(sym->vclass==sGLOBAL); func.address=0; func.nameofs=nameofs; #if BYTE_ORDER==BIG_ENDIAN align32(&func.address); align32(&func.nameofs); #endif pc_resetbin(fout,hdr.natives+count*sizeof(AMX_FUNCSTUB)); pc_writebin(fout,&func,sizeof func); pc_resetbin(fout,nameofs); pc_writebin(fout,alias,(int)strlen(alias)+1); nameofs+=(int)strlen(alias)+1; count++; } /* for */ free(nativelist); } /* if */ /* write the libraries table */ if (pc_addlibtable) { count=0; for (constptr=libname_tab.next; constptr!=NULL; constptr=constptr->next) { if (constptr->value>0) { assert(strlen(constptr->name)>0); func.address=0; func.nameofs=nameofs; #if BYTE_ORDER==BIG_ENDIAN align32(&func.address); align32(&func.nameofs); #endif pc_resetbin(fout,hdr.libraries+count*sizeof(AMX_FUNCSTUB)); pc_writebin(fout,&func,sizeof func); pc_resetbin(fout,nameofs); pc_writebin(fout,constptr->name,(int)strlen(constptr->name)+1); nameofs+=(int)strlen(constptr->name)+1; count++; } /* if */ } /* for */ } /* if */ /* write the public variables table */ count=0; for (sym=glbtab.next; sym!=NULL; sym=sym->next) { if (sym->ident==iVARIABLE && (sym->usage & uPUBLIC)!=0 && (sym->usage & (uREAD | uWRITTEN))!=0) { assert((sym->usage & uDEFINE)!=0); assert(sym->vclass==sGLOBAL); func.address=(uint32_t)sym->addr; func.nameofs=nameofs; #if BYTE_ORDER==BIG_ENDIAN align32(&func.address); align32(&func.nameofs); #endif pc_resetbin(fout,hdr.pubvars+count*sizeof(AMX_FUNCSTUB)); pc_writebin(fout,&func,sizeof func); pc_resetbin(fout,nameofs); pc_writebin(fout,sym->name,(int)strlen(sym->name)+1); nameofs+=(int)strlen(sym->name)+1; count++; } /* if */ } /* for */ /* write the public tagnames table */ count=0; for (constptr=tagname_tab.next; constptr!=NULL; constptr=constptr->next) { if ((constptr->value & PUBLICTAG)!=0) { assert(strlen(constptr->name)>0); func.address=(uint32_t)(constptr->value & TAGMASK); func.nameofs=nameofs; #if BYTE_ORDER==BIG_ENDIAN align32(&func.address); align32(&func.nameofs); #endif pc_resetbin(fout,hdr.tags+count*sizeof(AMX_FUNCSTUB)); pc_writebin(fout,&func,sizeof func); pc_resetbin(fout,nameofs); pc_writebin(fout,constptr->name,(int)strlen(constptr->name)+1); nameofs+=(int)strlen(constptr->name)+1; count++; } /* if */ } /* for */ /* write the "maximum name length" field in the name table */ assert(nameofs==hdr.nametable+nametablesize); pc_resetbin(fout,hdr.nametable); count=sNAMEMAX; #if BYTE_ORDER==BIG_ENDIAN align16(&count); #endif pc_writebin(fout,&count,sizeof count); /* write the overlay table */ if (pc_overlays>0) { AMX_OVERLAYINFO info; #if !defined NDEBUG int count=0; #endif pc_resetbin(fout,hdr.overlays); /* first the special overlay(s) for the return point(s) */ for (i=0; i<ovlFIRST; i++) { if (pc_ovl0size[i][1]!=0) { info.offset=pc_ovl0size[i][0]; info.size=pc_ovl0size[i][1]; #if BYTE_ORDER==BIG_ENDIAN align32(&info.offset); align32(&info.size); #endif pc_writebin(fout,&info,sizeof info); #if !defined NDEBUG count++; #endif } /* if */ } /* for */ /* now all real overlay functions */ for (sym=glbtab.next; sym!=NULL; sym=sym->next) { if (sym->ident==iFUNCTN && (sym->usage & uNATIVE)==0 && (sym->usage & (uREAD | uPUBLIC))!=0 && (sym->usage & uDEFINE)!=0) { assert(sym->vclass==sGLOBAL); assert(strcmp(sym->name,uENTRYFUNC)==0 || sym->index==count++);/* overlay indices must be in sequential order */ assert(strcmp(sym->name,uENTRYFUNC)==0 || sym->addr<sym->codeaddr); /* write the overlay for the stub function first */ if (strcmp(sym->name,uENTRYFUNC)!=0) { /* there is no stub function for state entry functions */ info.offset=(int32_t)sym->addr; info.size=(uint32_t)(sym->codeaddr - sym->addr); #if BYTE_ORDER==BIG_ENDIAN align32(&info.offset); align32(&info.size); #endif pc_writebin(fout,&info,sizeof info); } /* if */ if (sym->states!=NULL) { /* for functions with states, write an overlay block for every implementation */ statelist *stlist; for (stlist=sym->states->next; stlist!=NULL; stlist=stlist->next) { assert(stlist->label==count++); info.offset=(int32_t)stlist->addr; info.size=(int32_t)(stlist->endaddr - stlist->addr); #if BYTE_ORDER==BIG_ENDIAN align32(&info.offset); align32(&info.size); #endif pc_writebin(fout,&info,sizeof info); } /* for */ } /* if */ } /* if */ } /* for */ } /* if */ pc_resetbin(fout,hdr.cod); /* First pass: relocate all labels */ /* This pass is necessary because the code addresses of labels is only known * after the peephole optimization flag. Labels can occur inside expressions * (e.g. the conditional operator), which are optimized. */ lbltab=NULL; if (sc_labnum>0) { cell codeindex=0; /* address of the current opcode similar to "code_idx" */ /* only very short programs have zero labels; no first pass is needed * if there are no labels */ lbltab=(cell *)malloc(sc_labnum*sizeof(cell)); if (lbltab==NULL) error(103); /* insufficient memory */ memset(lbltab,0,sc_labnum*sizeof(cell)); pc_resetasm(fin); while (pc_readasm(fin,line,sizeof line)!=NULL) { stripcomment(line); instr=skipwhitespace(line); /* ignore empty lines */ if (*instr=='\0') continue; if (tolower(*instr)=='l' && *(instr+1)=='.') { int lindex=(int)hex2ucell(instr+2,NULL); assert(lindex>=0 && lindex<sc_labnum); assert(lbltab[lindex]==0); /* should not already be declared */ lbltab[lindex]=codeindex; } else { /* get to the end of the instruction (make use of the '\n' that fgets() * added at the end of the line; this way we will *always* drop on a * whitespace character) */ for (params=instr; *params!='\0' && !isspace(*params); params++) /* nothing */; assert(params>instr); i=findopcode(instr,(int)(params-instr)); assert(opcodelist[i].name!=NULL); assert(opcodelist[i].opt_level<=pc_optimize || pc_optimize==0 && opcodelist[i].opt_level<=1); if (opcodelist[i].segment==sIN_CSEG) codeindex+=opcodelist[i].func(NULL,skipwhitespace(params),opcodelist[i].opcode,codeindex); } /* if */ } /* while */ } /* if */ /* Second pass (actually 2 more passes, one for all code and one for all data) */ for (pass=sIN_CSEG; pass<=sIN_DSEG; pass++) { cell codeindex=0; /* address of the current opcode similar to "code_idx" */ pc_resetasm(fin); while (pc_readasm(fin,line,sizeof line)!=NULL) { stripcomment(line); instr=skipwhitespace(line); /* ignore empty lines and labels (labels have a special syntax, so these * must be parsed separately) */ if (*instr=='\0' || tolower(*instr)=='l' && *(instr+1)=='.') continue; /* get to the end of the instruction (make use of the '\n' that fgets() * added at the end of the line; this way we will *always* drop on a * whitespace character) */ for (params=instr; *params!='\0' && !isspace(*params); params++) /* nothing */; assert(params>instr); i=findopcode(instr,(int)(params-instr)); assert(opcodelist[i].name!=NULL); assert(opcodelist[i].opt_level<=pc_optimize || pc_optimize==0 && opcodelist[i].opt_level<=1); if (opcodelist[i].segment==pass) codeindex+=opcodelist[i].func(fout,skipwhitespace(params),opcodelist[i].opcode,codeindex); } /* while */ } /* for */ if (lbltab!=NULL) { free(lbltab); #if !defined NDEBUG lbltab=NULL; #endif } /* if */ assert(hdr.size==pc_lengthbin(fout)); if (!writeerror && (sc_debug & sSYMBOLIC)!=0) append_dbginfo(fout); /* optionally append debug file */ if (writeerror) error(101,"disk full"); /* adjust the header (for Big Endian architectures) */ size=(int)hdr.cod; /* save, the value in the header may need to be swapped */ #if BYTE_ORDER==BIG_ENDIAN align32(&hdr.size); align16(&hdr.magic); align16(&hdr.flags); align16(&hdr.defsize); align32(&hdr.publics); align32(&hdr.natives); align32(&hdr.libraries); align32(&hdr.pubvars); align32(&hdr.tags); align32(&hdr.nametable); align32(&hdr.cod); align32(&hdr.dat); align32(&hdr.hea); align32(&hdr.stp); align32(&hdr.cip); pc_resetbin(fout,0); pc_writebin(fout,&hdr,sizeof hdr); #endif /* return the size of the header (including name tables, but excluding code * or data sections) */ return size; }
char *expand_aliases( char *command, int depth, Alias *aliases ) { int ends_with_space; size_t length; char *result; char *cmd; char *args; int i; List *words; char *alias_value; char *aliased_command; char *prev_cmd; List *new_command_list; List *new_words; int new_command_list_length; // fprintf( stderr, "expand_aliases (%s),%d ... \n", command, depth ); /* If tcsh detects a loop in the aliases then it prints "Alias loop", but we simply stop expanding aliases at a certain depth. This is unlikely to affect any real alias expansion. */ if ( depth >= 20 ) { result = strdup( command ); } else if ( is_empty( command ) ) { result = strdup( command ); } else { /* If the command ends with a space, then we will eventually make sure that the final result also ends with a space. */ length = strlen( command ); if ( (length > 0) && (command[length-1] == ' ') ) { ends_with_space = 1; } else { ends_with_space = 0; } /* Split the string at the first white space. The first entry on the list is then the "command" and the second contains all the arguments. */ words = split_after_first_word( command ); if ( list_length( words ) > 0 ) { cmd = get_nth_word( words, 0 ); args = get_nth_word( words, 1 ); free_list( words ); /* If the command matches a defined alias, get the text which is is supposed to expand to. */ alias_value = lookup_alias( cmd, aliases ); if ( alias_value != NULL ) { prev_cmd = strdup( cmd ); /* Replace refernces to the "history" with values from the original command and arguments. */ aliased_command = replace_history( alias_value, cmd, args ); free( alias_value ); free( cmd ); /* Expanding the alias may very well have generated a number of sub-commands, so we must again split into simple commands. */ new_command_list = split_into_simple_commands( aliased_command ); new_command_list_length = list_length( new_command_list ); result = get_nth_word( new_command_list, 0 ); /* If the process so far has changed the command, then we need to see whether this new command is itself an alias. The simplest way to do this is to recursively call resolve_alias, but we include an explicit "depth" parameter so that we can avoid an infinite loop. */ new_words = split_after_first_word( aliased_command ); cmd = get_nth_word( new_words, 0 ); if ( strcmp( cmd, prev_cmd ) != 0 ) { result = expand_aliases( result, depth+1, aliases ); } free_list( new_words ); free( aliased_command ); free( cmd ); free( prev_cmd ); /* Recursively expand any aliases in the rest of the simple commands. */ for ( i = 1; i < new_command_list_length; i++ ) { result = append_dup_string( result, " " ); cmd = expand_aliases( get_nth_word( new_command_list, i ), depth+1, aliases ); result = append_dup_string( result, cmd ); free( cmd ); } free_list( new_command_list ); /* If the original command ended with a space, make sure that the result does as well. */ if ( ends_with_space ) { result = append_dup_string( result, " " ); } } else { /* The first word wasn't actually an alias. */ free( cmd ); result = strdup( command ); } } else { result = strdup( "" ); } } free( command ); // fprintf( stderr, "expand_aliases returns (%s)\n", result ); return result; }
void configcontainer::toggle(const std::string& key) { std::string resolved_key = lookup_alias(key); if (config_data[resolved_key].type == configdata_t::BOOL) { set_configvalue(resolved_key, std::string(get_configvalue_as_bool(resolved_key) ? "false" : "true")); } }
void configcontainer::reset_to_default(const std::string& key) { std::string resolved_key = lookup_alias(key); config_data[resolved_key].value = config_data[resolved_key].default_value; }
void configcontainer::set_configvalue(const std::string& key, const std::string& value) { LOG(level::DEBUG,"configcontainer::set_configvalue(%s [resolved: %s],%s) called", key, lookup_alias(key), value); config_data[lookup_alias(key)].value = value; }
bool configcontainer::get_configvalue_as_bool(const std::string& key) { std::string value = config_data[lookup_alias(key)].value; if (value == "true" || value == "yes") return true; return false; }
int configcontainer::get_configvalue_as_int(const std::string& key) { std::istringstream is(config_data[lookup_alias(key)].value); int i; is >> i; return i; }
// Expand aliases, this is recursive. ie if one alias contains other // aliases, these will also be expanded from the insertion point // onwards. // // If 'iter' is zero, then quoted strings are not expanded // static bool _expand_aliases(char *line, int len, int iter) { char *lp = line; char *ms, *me, *ep; char *alias; char c; int offset, line_len, alias_len; char alias_buf[MAX_STRING_LENGTH]; bool macro_found = false; if ((line_len = strlen(line)) != 0) { while (*lp) { c = *lp++; if ((c == '%') && (*lp == '{')) { // Found a macro/alias to expand ms = lp+1; lp += 2; while (*lp && (*lp != '}')) lp++; if (!*lp) { diag_printf("Invalid macro/alias '%s'\n", ms); line[0] = '\0'; // Destroy line return false; } me = lp; *me = '\0'; lp++; if ((alias = lookup_alias(ms,alias_buf)) != (char *)NULL) { alias_len = strlen(alias); // See if there is room in the line to expand this macro/alias if ((line_len+alias_len) < len) { // Make a hole by moving data within the line offset = alias_len-strlen(ms)-2; // Number of bytes being inserted ep = &lp[strlen(lp)-1]; if (offset > 1) { ep[offset] = '\0'; while (ep != (lp-1)) { ep[offset-1] = *ep; ep--; } } else { if (offset <=0) { while ((lp-1) != ep) { lp[offset-1] = *lp; lp++; } lp[offset-1]='\0'; } } // Insert the macro/alias data lp = ms-2; while (*alias) { if ((alias[0] == '%') && (alias[1] == '{')) macro_found = true; *lp++ = *alias++; } line_len = strlen(line); lp = lp - alias_len; } else { diag_printf("No room to expand '%s'\n", ms); line[0] = '\0'; // Destroy line return false; } } else { diag_printf("Alias '%s' not defined\n", ms); *me = '|'; } } else if ((c == '"') && (iter == 0)) { // Skip quoted strings while (*lp && (*lp != '"')) lp++; } } } return macro_found; }
/* * Looks up a pkgname from the index and fills a vector of index entries * with the result. * * Globbing is supported, and the alias list is consulted. The calling code * is responsible for freeing the memory allocated. See * mport_index_entry_free_vec() */ MPORT_PUBLIC_API int mport_index_lookup_pkgname(mportInstance *mport, const char *pkgname, mportIndexEntry ***entry_vec) { char *lookup; int count, i, step; sqlite3_stmt *stmt; int ret = MPORT_OK; mportIndexEntry **e; MPORT_CHECK_FOR_INDEX(mport, "mport_index_lookup_pkgname()") if (lookup_alias(mport, pkgname, &lookup) != MPORT_OK) RETURN_CURRENT_ERROR; if (mport_db_prepare(mport->db, &stmt, "SELECT COUNT(*) FROM index.packages WHERE pkg GLOB %Q", lookup) != MPORT_OK) RETURN_CURRENT_ERROR; switch (sqlite3_step(stmt)) { case SQLITE_ROW: count = sqlite3_column_int(stmt, 0); break; case SQLITE_DONE: ret = SET_ERROR(MPORT_ERR_FATAL, "No rows returned from a 'SELECT COUNT(*)' query."); goto DONE; break; default: ret = SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db)); goto DONE; break; } sqlite3_finalize(stmt); e = (mportIndexEntry **)calloc(count + 1, sizeof(mportIndexEntry *)); *entry_vec = e; if (count == 0) return MPORT_OK; if (mport_db_prepare(mport->db, &stmt, "SELECT pkg, version, comment, www, bundlefile FROM index.packages WHERE pkg GLOB %Q", lookup) != MPORT_OK) { ret = mport_err_code(); goto DONE; } while (1) { step = sqlite3_step(stmt); if (step == SQLITE_ROW) { if ((e[i] = (mportIndexEntry *)malloc(sizeof(mportIndexEntry))) == NULL) { ret = MPORT_ERR_FATAL; goto DONE; } e[i]->pkgname = strdup(sqlite3_column_text(stmt, 0)); e[i]->version = strdup(sqlite3_column_text(stmt, 1)); e[i]->comment = strdup(sqlite3_column_text(stmt, 2)); e[i]->www = strdup(sqlite3_column_text(stmt, 3)); e[i]->bundlefile = strdup(sqlite3_column_text(stmt, 4)); if (e[i]->pkgname == NULL || e[i]->version == NULL || e[i]->comment == NULL || e[i]->www == NULL || e[i]->bundlefile == NULL) { ret = MPORT_ERR_FATAL; goto DONE; } i++; } else if (step == SQLITE_DONE) { e[i] = NULL; goto DONE; } else { ret = SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db)); goto DONE; } } DONE: free(lookup); sqlite3_finalize(stmt); return ret; }