void runline(const char *cmd, char *err) { // this function takes an instruction for execution. // it removes all comments and passes the statement to exec void exec(const char *str, char *err); char impurity[2] = {'\0','\0'}; impurity[0] = purity_check(cmd); if(*cmd==';') /* if statement is a comment. */ return; if(!impurity[0]) /* if cmd is pure */ { strcpy(err,""); char *statement = NULL; if((statement=(char *)(malloc(sizeof(char)* (strlen(cmd)+1))))==NULL) allocerr(); *statement='\0'; char *spointer = statement; int l = 0; /* to store length of spointer */ l = strlen(cmd); int i = 0; /* loop counter */ char qt=0; for(i=0;i<l;i++,cmd++,spointer++) { if(*cmd==34||*cmd==39) /* char["] and char['] */ { if(qt==0) qt = 1; /* special symbol starts */ else qt = 0; /* special symbol ends */ } if(qt==0) if(*cmd=='#') /* remove comments from the statement */ break; *spointer = *cmd; /* transfer cmd to str */ } *spointer='\0'; /* set termination point for string */ strtrm(statement,' ','s'); /* now run the statement */ exec(statement,err); if(!strlen(err)) /* if eval was succesfull. */ { free(statement); statement = NULL; return; } else /* if eval was un-succesfull. */ { free(statement); statement = NULL; return; } } else /* if cmd is impure */ { strcpy(err,"Character ["); strcat(err,impurity); strcat(err,"] is not allowed."); return; } }
void shell(void) { printf("\n%s\n",eval_version); /* shell constants */ const char _shell_arrow[] =">>"; const char _shell_exit[]="exit"; /* shell variables */ char *cmd = NULL; char *str = NULL; char *err = NULL; void runline(const char *cmd, char *err); /* shell system */ if((cmd=(char *)(malloc(1)))==NULL) allocerr(); if((err=(char *)(malloc(1)))==NULL) allocerr(); while(strcmp(cmd,_shell_exit)) { strcpy(err,""); /* empty err string */ /* allocate (max_str_length) bytes to str for input */ stralloc: if((cp=(char *)(realloc(str,sizeof(char)* (max_str_length))))!=NULL) { str = cp; } else { /* if the system is very very low on memory */ if(max_str_length>=255) { max_str_length -= 1; goto stralloc; } else { allocerr(); } } printf("\n%s",_shell_arrow); gets(str); /* re-allocate cmd for reuse */ if((cp=(char *)(realloc(cmd,sizeof(char)*(strlen(str)+1))))!=NULL) { cmd = cp; } else { allocerr(); } strcpy(cmd,str); /* destroy str to free (max_str_length) bytes of memory space */ free(str); str = NULL; /* truncuate the string cmd */ strtrm(cmd,' ','s'); if(*cmd==0) continue; /* if cmd is empty then continue */ if(!strlen(cmd)) continue; /* allocate variable to store error information */ if((cp=(char *)(realloc(err,sizeof(char)*(strlen(cmd)+256))))!=NULL) { err = cp; } else { allocerr(); } /* cmd should not be exit */ if(strcmp(cmd,_shell_exit)) /* now run line string cmd */ runline(cmd,err); /* check for any errors */ if(strlen(err)) printf("\nError: %s\n",err); } /* while exiting, free used memeory */ free(cmd); cmd = NULL; free(err); err = NULL; }
void exec(const char *str, char *err) { // this function executes a given instruction. // all important functions are done from here. int eval(const char *str); void * evals(const char *str, const char data_type); strcpy(err,""); /* empty error container string */ char *exp = NULL; /* string containing evaluation expression */ char *asgn = NULL; /* string containing assignment variable name */ char data_type = '\0'; /* data type of variable to evaluate */ /* assignment analysis & error handling */ if((asgn=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *asgn='\0'; if(prechar(asgn,str,"=")=='=') /* check for = operator */ { /* ok, it is a variable assignment */ strtrm(asgn,' ','S'); if((*asgn==0)||(strlen(asgn)==0)) { /* error: assignment string is empty */ strcpy(err,"Assignment variable is not specified."); free(asgn); return; } else /* assignment string is NOT empty */ { char isid[2] = {'\0','\0'}; /* check identifier validity*/ isid[0] = id_check(asgn); if(isid[0]!=0) /* error: is not an identifier */ { if(isid[0]==1) /* error: var name starts with a number */ { strcpy(err,"Variable name cannot start with a number."); } else if(isid[0]==32) /* error: var name contains spaces */ { strcpy(err,"Variable name cannot contain spaces."); } else /* error: var name has illegal chars*/ { strcpy(err,"Variable name cannot contain ["); strcat(err,isid); strcat(err,"] character."); } free(asgn); return; } else /* success: assignment string is valid */ { /* allocate string exp */ if((exp=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *exp='\0'; /* now split the equation to get the exp. */ postchar(exp,str,"="); strtrm(exp,' ','S'); // find out the data type of the assignment // variable & store it in char data_type } } } else /* there is no assigment involved */ { /* allocate string exp */ if((exp=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *exp='\0'; strcpy(exp,str); strtrm(exp,' ','S'); /* de-allocate asgn */ free(asgn); asgn = NULL; } /* variable assignment */ if(asgn!=NULL) { /* possible variable data types */ long *i = NULL; // <- (int) short *n = NULL; // <- (int-) char *c = NULL; // <- (chr) double *d = NULL; // <- (real) float *f = NULL; // <- (real-) unsigned long *j = NULL; // <- (int+) unsigned short *m = NULL; // <- (int-+) unsigned char *a = NULL; // <- (chr+) switch(data_type) { case 'i': break; default: strcpy(err,"Fatal Data Transfer Error on ["); strcat(err,asgn); strcat(err,"] variable."); } /* de-allocate memory of data type */ switch(data_type) { case 'i': free(i); i = NULL; break; case 'n': free(n); n = NULL; break; case 'c': free(c); c = NULL; break; case 'd': free(d); d = NULL; break; case 'f': free(f); f = NULL; break; case 'j': free(j); j = NULL; break; case 'm': free(m); m = NULL; break; case 'a': free(a); a = NULL; break; } /* de-allocate asgn */ free(asgn); asgn = NULL; } else /* no assigment */ { // if there is no assignment then integer is considered // to be the default assignment data type for evaluation. puts(""); int _i=0; _i = eval(exp); printf("eval: %d\n",_i); data_type = 'i'; long *i = NULL; i=(long *)(evals(exp,data_type)); printf("evals: %ld\n",*i); free(i); i = NULL; void evali(const char *str, char *err, int *eval); evali(exp,err,&_i); printf("evali: %d\n",_i); printf("evali was invoked %d time(s).\n",evali_count); evali_count = 0; } free(exp); exp = NULL; }
void strtrm(char *str, const char trim, const char mode) { // STRING TRIMMING FUNCTION: // --------------------------- // This function removes a certain character from a string, // then truncuates the string. <const char trim> is removed. // The <const char mode> defines the mode of operation: // if mode is 'r' or 'R' then right side of str is trimmed. // if mode is 'l' or 'L' then left side of str is trimmed. // if mode is 's' or 'S' then both sides of str is trimmed. // if mode is 'i' or 'I' then the entire string str is trimmed. // if mode is anything else, then str is not modified // Finally, if capital letter is used for mode, then the // string str is re-allocated to the new string size, // in order to reduce memory usage. Warning: Use capital // char mode *only* for dynamically allocated strings. if(trim=='\0') return; /* if trim is null then return */ if((strlen(str)==0)||(*str=='\0')) return; /* str is empty */ if(mode=='i'||mode=='I') // inside { // an extensive trimming operation. this method omits all // the trim chars from str and squeezes str to close holes. char ch; /* temporary character storage variable */ unsigned int i, j; /* loop counters */ for(;*str!='\0';str++) { if(*str==trim) { for(i=0;*str!='\0';i++,str++) { str++; ch = *str; str--; *str=ch; } for(j=0;j<=i;j++) str--; } } *str='\0'; /* set termination point for string */ if(!strlen(str)) *str = '\0'; } else if(mode=='r'||mode=='R') // right { // this method removes all char trim selectively from str // starting from right, it removes trim until a non-trim char. // if modified str is '\0', then return is 1, otherwise is 0 const unsigned int len = strlen(str); unsigned int i = 0; /* loop counter */ for(i=0;i<len;i++) str++; /* push str poiner to the right end */ unsigned char wfound=0; for(i=0;i<len;i++) { str--; /* move pointer backward by one block */ if(*str!=trim) wfound=1; /* reached end of trim chars */ if(wfound) /* now reached the end of actual string*/ { str++; /* move pointer forward by one block */ *str='\0'; /* set termination point for string */ goto end; } } if(i==len) /* check is str is full of trims */ *str = '\0'; } else if(mode=='l'||mode=='L') // left { // this method removes all char trim selectively from str // starting from left, it removes trim until a non-trim char. // if modified str is '\0', then return is 1, otherwise is 0 const unsigned int len = strlen(str); unsigned int w = 0; /* to store no.of trim chars */ unsigned int l = 0; /* to store real length */ unsigned int i = 0; /* loop counter */ unsigned char wfound=0; /* boolean to verify if w is found */ for(;*str!='\0';str++) { if(*str!=trim) wfound=1; /* now w is found */ if(!wfound) if(*str==trim) w++; /* find out the no. of trim chars. */ } if(w==len) /* check is str is full of trims */ { for(i=0;i<len;i++) str--; /* move str pointer to the starting*/ *str = '\0'; goto end; } l=(len - w); /* find out real length */ for(i=0;i<len;i++) str--; /* move str pointer to the starting*/ char ch; /* temporary character storage variable */ unsigned int u = 0; /* loop counter */ for(i=0;i<l;i++) { for(u=0;u<w;u++) str++; /* moves pointer w block forward */ ch = *str; /* stores (w+u)th block of str into ch */ for(u=0;u<w;u++) str--; /* moves pointer w block backwards */ *str = ch; /* stores ch into (u)th block of str */ str++; /* increments str pointer to the next block */ } /* set termination point for string */ *str='\0'; } else if(mode=='s'||mode=='S') // sides { // the function is recursed to remove // char trim from left and right sides. strtrm(str,trim,'r'); strtrm(str,trim,'l'); } end: if(mode=='I'||mode=='L'||mode=='R'||mode=='S') { // re-alocate str to new size. int newsize = strlen(str)+1; if((cp=(char *)(realloc(str,sizeof(char)*newsize)))!=NULL) { str = cp; } else { allocerr(); } } return; }
int main(int argc, char **argv) { bool hidden = false; DWORD dwCmpFlags = SORT_STRINGSORT; char file[MAX_PATH] = ""; while (file[0]++ < 2) if (argc - 1) if ((argv[1][0] | 0x02) == '/') if ((argv[1][1] | 0x20) == 'a') { hidden = true; if ((argv[1][2] | 0x20) == 'i') dwCmpFlags |= NORM_IGNORECASE; ++argv; --argc; } else if ((argv[1][1] | 0x20) == 'i') { dwCmpFlags |= NORM_IGNORECASE; if ((argv[1][2] | 0x20) == 'a') hidden = true; ++argv; --argc; } else { puts("REPTXT32, freeware by Olof Lagerkvist.\r\n" "http://www.ltr-data.se [email protected]\r\n" "More info, including distribution permissions and\r\n" "source code are available on the website.\r\n" "\n" "Command line syntax:\r\n" "REPTXT32 [/A][/I] filename \"text1\" \"text2\"\r\n" "\n" "The program replaces text1 with text2 in the files. The /A switch includes\r\n" "hidden files and the /I switch ignores lower/uppercase when searching for\r\n" "text1 in the files.\r\n" "Please note that the length of text1 and text2 must be exactly the same."); return 0; } if (argc - 1) { if (argv[1][0] == '\"') strncpy(file, argv[1] + 1, sizeof(file)); else strncpy(file, argv[1], sizeof(file)); if (file[strlen(file) - 1] == '\"') file[strlen(file) - 1] = 0; } else { printf("Filename: "); file[0] = 0; fgets(file, sizeof file, stdin); file[strlen(file) - 1] = 0; } strncpy(file, strtrm(file), sizeof file); file[sizeof(file) - 1] = 0; if (file[0] == 0) return 0; char cmpstr[MAX_COMPARE_LENGTH]; if (argc > 2) { if (argv[2][0] == '\"') strncpy(cmpstr, argv[2] + 1, sizeof cmpstr); else strncpy(cmpstr, argv[2], sizeof cmpstr); if (cmpstr[strlen(cmpstr) - 1] == '\"') cmpstr[strlen(cmpstr) - 1] = 0; } else { printf("Type the text string to be replaced: "); cmpstr[0] = 0; fgets(cmpstr, sizeof cmpstr, stdin); cmpstr[strlen(cmpstr) - 1] = 0; } if (cmpstr[0] == 0) return 0; UINT txtlen = (UINT) strlen(cmpstr); char newstr[MAX_COMPARE_LENGTH]; FillMemory(newstr, txtlen, ' '); if (argc > 3) { if (argv[3][0] == '\"') strncpy(newstr, argv[3] + 1, txtlen + 1); else strncpy(newstr, argv[3], txtlen + 1); if (newstr[strlen(newstr) - 1] == '\"') newstr[strlen(newstr) - 1] = 0; } else { printf("Type the replacement text string : "); fgets(newstr, txtlen + 1, stdin); } newstr[strlen(newstr)] = ' '; newstr[txtlen] = 0; char *fptr = strrchr(file, '\\'); if (fptr == NULL) fptr = file; else fptr++; WIN32_FIND_DATA ft; HANDLE hFFF; if ((hFFF = FindFirstFile(file, &ft)) == INVALID_HANDLE_VALUE) { win_perror(file); return EOF; } do { if (ft.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; if ((ft.nFileSizeHigh == 0) && (ft.nFileSizeLow < txtlen)) { printf("File \"%s\" ignored: Too short to contain \"%s\".", ft.cFileName, cmpstr); clreol(); puts(""); continue; } strncpy(fptr, ft.cFileName, sizeof(file) - (fptr - file)); if (hidden) SetFileAttributes(ft.cFileName, FILE_ATTRIBUTE_NORMAL); else if (ft.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) continue; DWORD dwReplaces = process(file, cmpstr, newstr, txtlen, dwCmpFlags); if (dwReplaces) { printf("\r\"%s\" replaced with \"%s\" in %u place%s " "in \"%s\".", cmpstr, newstr, dwReplaces, (dwReplaces > 1 ? "s" : ""), file); clreol(); puts(""); } if (hidden) SetFileAttributes(ft.cFileName, ft.dwFileAttributes); } while (FindNextFile(hFFF, &ft)); FindClose(hFFF); fputc('\r', stdout); clreol(); return 0; }