void runs(const char *str) { if(*str=='#'||*str==' '||*str=='\t') return; linenum += 1; /* * here str is trimmed of all spaces * and tabs and stored in string and. */ char *and = clones(str); trims(and,' '); trims(and,'\t'); /* eliminate the comments */ register int32 loop = 0; for(;and[loop]!='\0';loop++) { if(and[loop]==34||and[loop]==39) bits.qt += 1; else if(and[loop]=='#'&&!bits.qt) and[loop] = '\0'; } reallocstr(and); /* now execute "and" */ lisp_error[0] = '\0'; exec(and); /* now runs() returns */ if(strlen(lisp_error)) msg(lisp_error,2); free(and); }
void runs(const char *str) { if(*str==';'||*str==' '||*str=='\t') return; /* * here str is trimmed of all spaces * and tabs and stored in string and. */ char *and = clones(str); trims(and,' '); trims(and,'\t'); register int loop = 0; for(;and[loop]!='\0';loop++) { if(and[loop]==34||and[loop]==39) bits.qt += 1; else if(and[loop]==';'&&!bits.qt) and[loop] = '\0'; } /* now execute "and" */ PROTOTYPE (void exec, (char *)); reallocstr(and); exec(and); free(and); }
void shell(char *name) { setaddress("i"); setaddress("am"); setaddress("arjun"); getaddress("yes"); deladdress("am"); deladdress("i"); setaddress("menon"); getaddress("arjun"); char str[STR_MAX]; puts(name); while(1) { printf("\n$ "); fflush(stdin); fgets(str,STR_MAX,stdin); str[strlen(str)-1] = '\0'; trims(str,' '); trims(str,'\t'); if(!strlen(str)) continue; if(!stricmp(str,"exit")) break; runs(str); } }
void runs(const char *str) { if(*str==';'||*str==' '||*str=='\t') return; linenum += 1; /* * here str is trimmed of all spaces * and tabs and stored in string and. */ char *and = clones(str); trims(and,' '); trims(and,'\t'); /* eliminate the comments */ register long int loop = 0; for(;and[loop]!='\0';loop++) { if(and[loop]==34||and[loop]==39) bits.qt += 1; else if(and[loop]==';'&&!bits.qt) and[loop] = '\0'; } /* now execute "and" */ void exec(const char *); reallocstr(and); exec(and); free(and); }
void exchange_vars(char *lhs, char *rhs, char *err) { /* * Variable Exchange Function, swaps the value * stored in two variable lhs & rhs. If they * don't exist error message is stored in *err, * and function is returned. * ------------------------------------------------ * WARNING: Strings *rhs and *lhs are subject to * changes, forget them after calling this function. */ rhs++; /* to avoid '>' */ trims(rhs,' '); /* now exchange */ int32 *value = mygetint32(rhs); if(value!=NULL) { /* okay, rhs is valid */ int32 rvalue = *value; value = mygetint32(lhs); if(value!=NULL) { /* now swap the values */ * mygetint32(rhs) = * value; * value = rvalue; printf("%s => %s\n",lhs,rhs); } else sprintf(err,"LHS Variable '%s' does not exist.",lhs); } else sprintf(err,"RHS Variable '%s' does not exist.",rhs); }
void shell(const char *prog) { char str[STR_MAX]; puts(prog); forever { printf("\n$ "); fflush(stdin); fgets(str,STR_MAX,stdin); str[strlen(str)-1] = '\0'; trims(str,' '); trims(str,'\t'); if(!strlen(str)) continue; if(!stricmp(str,"exit")) break; runs(str); } }
void shell(char *name) { PROTOTYPE (void runs,(const char *str)); char str[KB_SIZE]; puts(name); forever { printf("\n$ "); fflush(stdin); fgets(str,KB_SIZE,stdin); str[strlen(str)-1] = '\0'; trims(str,' '); trims(str,'\t'); if(!strlen(str)) continue; if(!stricmp(str,"exit")) break; runs(str); } }
void exec(const char *str) { int32 strn = 0; /* check validity of equation in number of brackets */ int32 brackets = 0; for(;*str!='\0';str++,strn++) { if(*str=='(') brackets++; else if(*str==')') brackets--; } if(brackets!=0) { strcpy(lisp_error,"Invalid Syntax, incorrect number of parentheses."); return; } str -= strn; char *lhs = allocs(strn); char *rhs = allocs(strn); /* here the rhs & lhs are obtained */ { char *sptr = NULL; /* sptr points at const *str */ for(sptr=lhs;*str!='\0'&&*str!='=';str++) *sptr++ = *str; *sptr = '\0'; if(*str=='=') { for(sptr=rhs,str++;*str!='\0';str++) { *sptr++ = *str; *sptr = '\0'; } } else { *rhs = '\0'; } trims(lhs,' '); } if(*rhs=='\0') { /* <- empty rhs, so no assignment involved. */ /* check if rhs is a variable */ int32 *value = NULL; value = getint32(lhs); if(value==NULL) { /* rhs is NOT a variable */ int32 val=0; eval_int32(lhs,&val); printf("\n%ld\n",val); } else printf("%s = %ld\n",lhs,*value); } else { /* rhs exists */ /* check for [=>] exchange directive */ if(*rhs=='>') exchange_vars(lhs,rhs,lisp_error); else { /* variable creation/assignment */ void asgn(char *rhs, char *lhs); asgn(rhs,lhs); } } /* free memeory */ free(lhs); free(rhs); }
void shell(const char *prog) { lex_cmd("hello hi 45.67 hoo 0x234 hee 67 !@#$%^&* ",0); lex_cmd("punch ( 423, gfs, sfds, 55664, BRR, %*@$@$ ) ",0); char str[STR_MAX]; puts(prog); while(TRUE) { printf("\n$ "); fflush(stdin); fgets(str,STR_MAX,stdin); str[strlen(str)-1] = '\0'; trims(str,' '); trims(str,'\t'); if(!strlen(str)) continue; if(!stricmp(str,"exit")) break; runs(str); } }
bool KTrashOnekeyTask::SplitStrTask( /*[in]*/ std::wstring str, /*[out]*/ std::vector <std::wstring>& vcResult, /*[in]*/ char delim ) { size_t nIter = 0; size_t nLast = 0; std::wstring v; while (true) { nIter = str.find(delim, nIter); trims(str.substr(nLast, nIter - nLast), vcResult, ' '); if( nIter == std::wstring::npos ) break; nLast = ++nIter; } return true; }
void eval_int32(char *str, int32 *value) { /* Expression Evaluation Function (int32) * ======================================== * This function evaluates an infix expression * for an int32 result. First, it translates * the given expression to the postfix * form, and then evaluates the postfix * expression using the peval function. * The result is stored in int32 *value. * -------------------------------------------- * WARNING: *str is subject to slight changes, * so transfer expression to temporary string * before passing it here. */ trims(str,' '); register num32 cur=0; /* count the number of operators */ if(*str=='-') { str++; cur++; } num32 stack_size=0; for(;*str!='\0';str++,cur++) if(*str=='+'||*str=='-'|| *str=='^'||*str=='%'|| *str=='*'||*str=='/'|| *str=='<'||*str=='>'|| *str=='&'||*str=='|'|| *str==')'||*str=='('||( *str=='='&&str[1]=='=')) stack_size++; str -= cur; /* no operators */ if(!stack_size) { sval_int32(str,value); return; } /* stacks for storing postfix expression */ char * const ostack = (char *) malloc ((stack_size*2 + 1) * sizeof(char)); int32 * const values = (int32 *) malloc ((stack_size + 1) * sizeof(int32)); if(ostack==NULL||values==NULL) msg(system_no_mem,3); /* Rules: ostack contains operators(+,-,*..), wheras * values contains numbers. if char *ostack = 0, then * int32 *values is holds a valid number of the expression, * otherwise char *ostack contains a valid math operator. * Based on these rules the expression is designed. */ /* important loop counters... */ register num32 top=0,ival=0,ops=0; /***********************************. | ( infix => postfix ) translation: | `***********************************/ { /* temporary character array */ char * const tmp = allocs(strlen(str)); /* temporary stack to hold operators (+,-,*,..) */ uchar * const cstack = (uchar *) malloc ((stack_size + 1) * sizeof(uchar)+11); if(cstack==NULL) msg(system_no_mem,3); *cstack = '\0'; char prevOp = '\0', negative=0; /* beware: mathematics ahead... */ for(cur=0;;str++) { if(*str=='('&&cur) { /* to handle substring baraces */ register int32 pars = 0; for(;*str!='\0';str++) { if(*str=='(') pars++; if(*str==')') pars--; if(!pars) break; tmp[cur++] = *str; } if(*str=='\0') break; else tmp[cur++] = ')'; continue; } if(*str=='+'||*str=='-'|| *str=='*'||*str=='/'|| *str=='^'||*str=='%'|| *str=='<'||*str=='>'|| *str=='('||*str==')'|| *str=='&'||*str=='|'|| *str=='='||*str=='\0') { /* finalise tmp */ tmp[cur] = '\0'; trims(tmp,' '); for(cur=0;tmp[cur]!='\0';cur++); /* skip vals for opening braces */ if(*str=='(') goto after_vals; if(!cur) { /* skip the translation in case of repition of operators */ if(prevOp==')'||prevOp=='(') goto after_vals; else { if(*str=='-') negative++; prevOp = *str; if(*str=='\0') break; continue; } } /* doubly operators */ if(*str=='='&&str[1]=='=') /* logical == */ *++str='E'; else if(*str=='>'&&str[1]=='=') /* logical >= */ *++str='F'; else if(*str=='<'&&str[1]=='=') /* logical <= */ *++str='G'; else if(*str=='&'&&str[1]=='&') /* logical AND */ *++str='A'; else if(*str=='|'&&str[1]=='|') /* logical OR */ *++str='O'; else if(*str=='*'&&str[1]=='*') /* power */ *++str='P'; else if(*str=='>'&&str[1]=='>') /* bitwise right shift (>>) */ *++str='R'; else if(*str=='<'&&str[1]=='<') /* bitwise left shift (<<) */ *++str='L'; else if(*str=='>'&&str[1]=='<') /* maximum value */ *++str='M'; else if(*str=='+'&&str[1]=='&') /* concatenate */ *++str='C'; /* number insertion: */ sval_int32(tmp,&values[ival++]); if(negative) { values[ival-1]*=-1; negative = 0; } ostack[ops++] = 0; cur = *tmp = 0; after_vals: /* operand insertion: */ if(eval_prior(cstack[top])<eval_prior(*str) ||*str=='(') cstack[++top] = *str; else { for(;top&&cstack[top]!='(';top--) ostack[ops++] = cstack[top]; if(*str!=')') cstack[++top] = *str; else if(cstack[top]=='(') cstack[top--] = 0; } if(*str=='\0') break; prevOp = *str; } else tmp[cur++] = *str; } /* free memory: */ free(cstack); free(tmp); } // printf("\n---[%ld]---\n",ops); // for(cur=0,top=0;cur<ops;cur++) // if(!ostack[cur]) printf(" %ld ",values[top++]); // else printf(" %c ",ostack[cur]); // puts("\n-----------"); /*******************************. | Postfix Expression Evaluator: | `*******************************/ { /* temporary stack to evaluate expression */ int32 * const stack = (int32 *) malloc ((stack_size + 1) * sizeof(int32)); if(stack==NULL) msg(system_no_mem,3); for(cur=0,top=0,ival=0;cur<ops;cur++) { if(!ostack[cur]) /* an operand */ stack[ival++] = values[top++]; else { /* an operator (+,-,*,..)[15] */ switch(ostack[cur]) { /* do the math */ case '+': stack[--ival-1] += stack[ival]; break; case '-': stack[--ival-1] -= stack[ival]; break; case '*': stack[--ival-1] *= stack[ival]; break; case '/': stack[--ival-1] /= stack[ival]; break; case '%': stack[--ival-1] %= stack[ival]; break; /* the logical operators (==,>=,&&,||,..) */ case 'E': stack[ival---2] = stack[ival-1] == stack[ival-2] ? 1 : 0; break; /* == */ case '>': stack[ival---2] = stack[ival-1] < stack[ival-2] ? 1 : 0; break; /* > */ case 'F': stack[ival---2] = stack[ival-1] >= stack[ival-2] ? 1 : 0; break; /* >= */ case '<': stack[ival---2] = stack[ival-1] > stack[ival-2] ? 1 : 0; break; /* < */ case 'G': stack[ival---2] = stack[ival-1] >= stack[ival-2] ? 1 : 0; break; /* <= */ case 'A': stack[ival---2] =(stack[ival-1]>0)&&( stack[ival-2]) ? 1 : 0; break; /* && */ case 'O': stack[ival---2] =(stack[ival-1]>0)||( stack[ival-2]) ? 1 : 0; break; /* || */ /* the bitwise operators (&,|,^^,>>,<<,..) */ case '&': stack[--ival-1] &= stack[ival]; break; case '|': stack[--ival-1] |= stack[ival]; break; case '^': stack[--ival-1] ^= stack[ival]; break; case 'L': stack[ival-1] = /* bitwise left shift */ stack[ival-1] << stack[--ival]; break; case 'R': stack[ival-1] = /* bitwise right shift */ stack[ival-1] >> stack[--ival]; break; /* the special operators (^,+&,><)... */ case 'P': stack[--ival-1] = (int32) pow (stack[ival-2],stack[ival-1]); break; case 'C': concatenate(&stack[ival-1],stack[--ival]); break; case 'M': stack[ival---2] = stack[ival-1] > stack[ival-2] ? stack[ival-1] : stack[ival-2]; break; default: stack[--ival-1] = stack[ival]; } } } /* set value */ *value = stack[0]; free(stack); } /* free memory: */ free(ostack); free(values); }
void eval_long(char *str, long int *value) { /* Expression Evaluation Function (long) * ======================================== * This function evaluates an infix expression * for a long int result. First, it translates * the given expression to the postfix * form, and then evaluates the postfix * expression using the peval function. * The result is stored in int *value. * -------------------------------------------- * WARNING: *str is subject to slight changes, * so transfer expression to temporary string * before passing it here. */ trims(str,' '); register unsigned long int cur=0; /* count the number of operators */ if(*str=='-') { str++; cur++; } unsigned long int stack_size=0; for(;*str!='\0';str++,cur++) if(*str=='+'||*str=='-'|| *str=='^'||*str=='%'|| *str=='*'||*str=='/'|| *str=='<'||*str=='>'|| *str=='&'||*str=='|'|| *str==')'||*str=='(') stack_size++; str -= cur; /* no operators */ if(!stack_size) { vals_long(str,value); return; } /* stacks for storing postfix expression */ char * const ostack = (char *) malloc ((stack_size*2 + 1) * sizeof(char)); long int * const values = (long int *) malloc ((stack_size + 1) * sizeof(long int)); if(ostack==NULL||values==NULL) msg(no_mem,3); /* Rules: ostack contains operators(+,-,*..), wheras * values contains numbers. if char *ostack = 0, then * int *values is holds a valid number of the expression, * otherwise char *ostack contains a valid math operator. * Based on these rules the expression is designed. */ /* important loop counters... */ register unsigned long int top=0,ival=0,ops=0; /***********************************. | ( infix => postfix ) translation: | `***********************************/ { /* temporary character array */ char * const tmp = allocs(strlen(str)); /* temporary stack to hold operators (+,-,*,..) */ unsigned char * const cstack = (unsigned char *) malloc ((stack_size + 1) * sizeof(unsigned char)+11); if(cstack==NULL) msg(no_mem,3); *cstack = '\0'; char prevOp = '\0', negative=0; for(cur=0;;str++) { if(*str=='('&&cur) { register int pars = 0; for(;*str!='\0';str++) { if(*str=='(') pars++; if(*str==')') pars--; if(!pars) break; tmp[cur++] = *str; } if(*str=='\0') break; else tmp[cur++] = ')'; continue; } if(*str=='+'||*str=='-'|| *str=='*'||*str=='/'|| *str=='^'||*str=='%'|| *str=='<'||*str=='>'|| *str=='('||*str==')'|| *str=='&'||*str=='|'|| *str=='\0') { /* finalise tmp */ tmp[cur] = '\0'; trims(tmp,' '); for(cur=0;tmp[cur]!='\0';cur++); /* skip vals for opening braces */ if(*str=='(') goto after_vals; if(!cur) { /* skip the translation in case of repition of operators */ if(prevOp==')'||prevOp=='(') goto after_vals; else { if(prevOp=='-') negative++; prevOp = *str; if(*str=='\0') break; continue; } } /* bitwise and (&) */ if(*str=='&'&&str[1]=='&') *++str='A'; /* bitwise or (|) */ if(*str=='|'&&str[1]=='|') *++str='O'; /* number insertion: */ vals_long(tmp,&values[ival++]); if(negative) { values[ival-1]*=-1; negative = 0; } ostack[ops++] = 0; cur = *tmp = 0; after_vals: /* opernad insertion: */ if(eval_prior(cstack[top])<eval_prior(*str) ||*str=='(') cstack[++top] = *str; else { for(;top&&cstack[top]!='(';top--) ostack[ops++] = cstack[top]; if(*str!=')') cstack[++top] = *str; else if(cstack[top]=='(') cstack[top--] = 0; } if(*str=='\0') break; prevOp = *str; } else tmp[cur++] = *str; } /* free memory: */ free(cstack); free(tmp); } printf("\n---[%ld]---\n",ops); for(cur=0,top=0;cur<ops;cur++) if(!ostack[cur]) printf(" %ld ",values[top++]); else printf(" %c ",ostack[cur]); puts("\n-----------"); /*******************************. | Postfix Expression Evaluator: | `*******************************/ { /* temporary stack to evaluate expression */ long int * const stack = (long int *) malloc ((stack_size + 1) * sizeof(long int)); if(stack==NULL) msg(no_mem,3); for(cur=0,top=0,ival=0;cur<ops;cur++) { if(!ostack[cur]) /* an operand */ stack[ival++] = values[top++]; else { /* an operator (+,-,*,..) */ switch(ostack[cur]) { /* do the math */ case '+': stack[--ival-1] += stack[ival]; break; case '-': stack[--ival-1] -= stack[ival]; break; case '*': stack[--ival-1] *= stack[ival]; break; case '/': stack[--ival-1] /= stack[ival]; break; case '%': stack[--ival-1] %= stack[ival]; break; case 'A': stack[--ival-1] &= stack[ival]; break; case 'O': stack[--ival-1] |= stack[ival]; break; case '<': stack[ival-1] = /* bitwise left shift */ stack[ival-1] << stack[--ival]; break; case '>': stack[ival-1] = /* bitwise right shift */ stack[ival-1] >> stack[--ival]; break; case '^': stack[--ival-1] = (long int) pow (stack[ival-2],stack[ival-1]); break; case '&': concatenate(&stack[ival-1],stack[--ival]); break; default: stack[--ival-1] = stack[ival]; } } } /* set value */ *value = stack[0]; free(stack); } /* free memory: */ free(ostack); free(values); }
void exec(const char *str) { long int strn = 0; /* check validity of equation in number of brackets */ long int brackets = 0; for(;*str!='\0';str++,strn++) { if(*str=='(') brackets++; else if(*str==')') brackets--; } if(brackets!=0) { printf("\nError: Invalid Syntax, incorrect number of parentheses.\n"); return; } str -= strn; char *lhs = allocs(strn); char *rhs = allocs(strn); char *sptr = NULL; /* here, sptr points at const *str */ /* here the rhs & lhs are obtained */ for(sptr=lhs;*str!='\0'&&*str!='=';str++) *sptr++ = *str; *sptr = '\0'; if(*str=='=') { for(sptr=rhs,str++;*str!='\0';str++) { *sptr++ = *str; *sptr = '\0'; } } else { *rhs = '\0'; } trims(lhs,' '); /* here onwards, sptr is used as an error descrptor string. */ sptr = (char *) allocs(strn+64); *sptr = '\0'; if(*rhs=='\0') { /* <- empty rhs, so no assignment involved. */ /* check if rhs is a variable */ long int *value = NULL; value = getint(lhs); if(value==NULL) { /* rhs is NOT a variable */ long val=0; eval_long(lhs,&val); printf("\n%ld\n",val); } else printf("%s = %ld\n",lhs,*value); } else { /* rhs exists */ /* check for [=>] exchange directive */ if(*rhs=='>') { exchange_vars(lhs,rhs,sptr); if(strlen(sptr)) printf("\nError: %s\n",sptr); } else /* variable creation/assignment */ { void asgn(char *rhs, char *lhs, char *err); asgn(rhs,lhs,sptr); if(strlen(sptr)) printf("\nError: %s\n",sptr); } } /* free memeory */ free(sptr); free(lhs); free(rhs); }