main() { char s[100], wt[100], *nt; long i; do { queryn("template: ", wt, 100); if (has_wildcards(wt)) printf("template has wildcards\n"); chop_nl(wt); if (is_blank(wt)) break; nt = expand_ranges(wt); printf("expanded template: %s\n", nt); do { queryn("string: ", s, 100); chop_nl(s); if (s[0]=='q') break; if ((i=wild_match(s, nt))) printf("%s is matched by %s--return value %ld\n", s, wt, i); else printf("%s is not matched by %s\n", s, wt); } while (1); } while (1); }
char *fgets_mc_skip( char *s, /* buffer for lines */ long slen, /* length of lines to read */ FILE *fp, /* file to read from */ char skip_char /* ignore lines that begin with this character */ ) { do { if (!fgets(s, slen, fp)) return(NULL); chop_nl(s); if (s[0]!=skip_char) break; } while (1); return(s); }
/** * read data from stdin, remove new line. * strdup it and return pointer to it. NULL otherwise * if not NULL, caller must free the memory */ char *read_data(char *prompt) { char *data, *l, buf[BUFSIZ]; (void) fprintf(stdout,"%s",prompt); (void) fflush(stdout); l=fgets(buf,sizeof(buf)-1,stdin); if (l && *l != '\n') { data=strdup(l); chop_nl(data); return(data); } return(NULL); }
void rpn_execs() { char *string; #if !defined(vxWorks) char buffer[1024]; FILE *fp; #endif if (!(string = pop_string())) return; #if defined(vxWorks) fprintf(stderr, "popen is not supported in vxWorks\n"); exit(1); #else if (!(fp = popen(string, "r"))) { fprintf(stderr, "error: invalid command: %s\n", string); stop(); return; } if (feof(fp)) { fprintf(stderr, "error: command %s returns EOF\n", string); stop(); return; } if (!fgets(buffer, 1024, fp)) { fprintf(stderr, "error: command %s returns NULL\n", string); stop(); return; } do { chop_nl(buffer); push_string(buffer); } while (fgets(buffer, 1024, fp)); #endif }
double rpn(char *expression) { static long i, return_code; static char *ptr; static char *input, *rpn_defns; static long initial_call = 1; #if defined(LINUX) struct stat sts; #endif if (initial_call) { initial_call = 0; #ifdef VAX_VMS /* initialize collection of computer usage statistics--required by * user-callable function 'rs' */ init_stats(); #endif /* sort the command table for faster access */ qsort(funcRPN, NFUNCS, sizeof(struct FUNCTION), func_compare); /* initialize stack pointers--empty stacks */ stackptr = 0; dstackptr = 0; sstackptr = 0; lstackptr = 0; astackptr = 0; udf_stackptr = 0; max_udf_stackptr = 0; astack = NULL; udf_stack = NULL; udf_id = NULL; udf_unknown = NULL; /* The first item on the command input stack is the standard input. * Input from this source is echoed to the screen. */ istackptr = 1; input_stack[0].fp = stdin; input_stack[0].filemode = ECHO; /* Initialize variables use in keeping track of what 'code' is being * executed. code_ptr is a global pointer to the currently used * code structure. The code is kept track of in a linked list of * code structures. */ code_ptr = &code; input = code_ptr->text = tmalloc(sizeof(*(code_ptr->text))*CODE_LEN); code_ptr->position = 0; code_ptr->token = NULL; code_ptr->storage_mode = STATIC; code_ptr->buffer = tmalloc(sizeof(*(code_ptr->buffer))*LBUFFER); code_ptr->pred = code_ptr->succ = NULL; code_lev = 1; /* Initialize array of IO file structures. Element 0 is for terminal * input, while element 1 is for terminal output. */ for (i=0; i<FILESTACKSIZE; i++) io_file[i].fp = NULL; io_file[0].fp = stdin; cp_str(&(io_file[0].name), "stdin"); io_file[0].mode = INPUT; io_file[1].fp = stdout; cp_str(&(io_file[1].name), "stdout"); io_file[1].mode = OUTPUT; /* initialize variables for UDF storage */ udf_changed = num_udfs = max_udfs = 0; udf_list = NULL; /* Initialize flags for user memories */ n_memories = memory_added = 0; /* If there was an argument (filename), push it onto the input stack * so that it will be run to set up the program. */ if (expression) { if ((input_stack[istackptr].fp = fopen_e(expression, "r", 1))==NULL) { fprintf(stderr, "ensure the RPN_DEFNS environment variable is set\n"); exit(1); } input_stack[istackptr++].filemode = NO_ECHO; } else { /*add default setting for a linux system, G. Shen, Dec 31, 2009 */ rpn_defns=getenv("RPN_DEFNS"); if(!rpn_defns) { #if defined(LINUX) if (!(stat(rpn_default, &sts) == -1 && errno == ENOENT)) { /* check whether default file exists */ rpn_defns = rpn_default; } #endif } if (rpn_defns) { /* check environment variable RPN_DEFNS for setup file */ /*cp_str(&rpn_defns, getenv("RPN_DEFNS"));*/ if (strlen(rpn_defns)) { input_stack[istackptr].fp = fopen_e(rpn_defns, "r", 0); input_stack[istackptr++].filemode = NO_ECHO; } } } expression = NULL; /* end of initialization section */ } else istackptr = 1; /* check the stacks for overflows */ if (stackptr>=STACKSIZE-1) { fprintf(stderr, "error: numeric stack size overflow (rpn).\n"); abort(); } /* if (astackptr>=STACKSIZE-1) { fprintf(stderr, "error: array stack size overflow (rpn).\n"); abort(); } */ if (sstackptr>=STACKSIZE-1) { fprintf(stderr, "error: string stack size overflow (rpn).\n"); abort(); } if (lstackptr>=LOGICSTACKSIZE-1) { fprintf(stderr, "error: logic stack size overflow (rpn).\n"); abort(); } /* This is the main loop. Code is read in and executed here. */ while (istackptr!=0) { /* istackptr-1 gives index of most recently pushed input file. */ /* This loop implements the command input file stacking. */ while (istackptr>0 && (ptr=((istackptr-1)?fgets((code_ptr->text=input), CODE_LEN, input_stack[istackptr-1].fp) :(expression?strcpy(code_ptr->text,expression):NULL) )) ) { /* Loop while there's still data in the (istackptr-1)th file. * * istackptr=1 corresponds to the expression passed. * * The data is put in the code list. */ /* If we are at the terminal input level and a UDF has been changed * or a memory added, relink the udfs to get any references to the * new udf or memory translated into 'pcode'. */ if ((istackptr==1 && udf_changed) || memory_added) { link_udfs(); udf_changed = memory_added = 0; } code_ptr->position = 0; /* Get rid of new-lines in data from files */ if (istackptr!=1 && ptr!=NULL) { chop_nl(ptr); } /* Check for and ignore comment lines. */ if (strncmp(ptr, "/*", 2)==0) continue; /* Finally, push input line onto the code stack & execute it. */ return_code = execute_code(); if (code_lev!=1) { fputs("error: code level on return from execute_code is not 1\n\n", stderr); exit(1); } /* Reset pointers in the current code structure to indicate that the * stuff has been executed. */ *(code_ptr->text) = 0; code_ptr->position = 0; expression = NULL; } /* Close the current input file and go to the one below it on the * * stack. This constitutes popping the command input stack. * */ if (istackptr>1) fclose(input_stack[--istackptr].fp); else istackptr--; } /* check the stacks for overflows */ if (stackptr>=STACKSIZE-1) { fprintf(stderr, "error: numeric stack size overflow (rpn).\n"); abort(); } /* if (astackptr>=STACKSIZE-1) { fprintf(stderr, "error: array stack size overflow (rpn).\n"); abort(); } */ if (sstackptr>=STACKSIZE-1) { fprintf(stderr, "error: string stack size overflow (rpn).\n"); abort(); } if (lstackptr>=LOGICSTACKSIZE-1) { fprintf(stderr, "error: logic stack size overflow (rpn).\n"); abort(); } if (stackptr>0) return(stack[stackptr-1]); return(0.0); }
int main(int argc, char **argv) { long i, return_code; char *ptr; static char *input; static char *rpn_defns; #if defined(LINUX) struct stat sts; #endif #ifdef VAX_VMS /* initialize collection of computer usage statistics--required by * user-callable function 'rs' */ init_stats(); #endif puts("Welcome to rpn version 6, by Michael Borland and Robert Soliday (June 1999)."); /* sort the command table for faster access */ /*qsort(func, NFUNCS, sizeof(struct FUNCTION), func_compare); */ qsort(funcRPN, sizeof(funcRPN)/sizeof(funcRPN[0]), sizeof(struct FUNCTION), func_compare); /* initialize stack pointers--empty stacks */ stackptr = 0; sstackptr = 0; lstackptr = 0; astackptr = 0; dstackptr = 0; astack = NULL; udf_stackptr = 0; max_udf_stackptr = 0; udf_stack = NULL; udf_cond_stackptr = 0; max_udf_cond_stackptr = 0; udf_cond_stack = NULL; udf_id = NULL; udf_unknown = NULL; /* The first item on the command input stack is the standard input. * Input from this source is echoed to the screen. */ istackptr = 1; input_stack[0].fp = stdin; input_stack[0].filemode = ECHO; /* Initialize variables use in keeping track of what 'code' is being * executed. code_ptr is a global pointer to the currently used * code structure. The code is kept track of in a linked list of * code structures. */ code_ptr = &code; input = code_ptr->text = tmalloc(sizeof(*(code_ptr->text))*CODE_LEN); code_ptr->position = 0; code_ptr->token = NULL; code_ptr->storage_mode = STATIC; code_ptr->buffer = tmalloc(sizeof(*(code_ptr->buffer))*LBUFFER); code_ptr->pred = code_ptr->succ = NULL; code_lev = 1; /* Initialize array of IO file structures. Element 0 is for terminal * input, while element 1 is for terminal output. */ for (i=0; i<FILESTACKSIZE; i++) io_file[i].fp = NULL; io_file[0].fp = stdin; cp_str(&(io_file[0].name), "stdin"); io_file[0].mode = INPUT; io_file[1].fp = stdout; cp_str(&(io_file[1].name), "stdout"); io_file[1].mode = OUTPUT; /* initialize variables for UDF storage */ udf_changed = num_udfs = max_udfs = 0; udf_list = NULL; /* Initialize flags for user memories */ n_memories = memory_added = 0; /* If there are arguments push them onto the input stack * so that it will be run to set up the program. */ while (argc-- >= 2) { input_stack[istackptr].fp = fopen_e(argv[argc], "r", 0); input_stack[istackptr++].filemode = NO_ECHO; } /*add default setting for a linux system, G. Shen, Dec 31, 2009 */ rpn_defns=getenv("RPN_DEFNS"); if(!rpn_defns) { #if defined(LINUX) if (!(stat(rpn_default, &sts) == -1 && errno == ENOENT)) { /* check whether default file exists */ rpn_defns = rpn_default; } #endif } if (rpn_defns && (long)strlen(rpn_defns)>0 ) { /* push rpn definitions file onto top of the stack */ input_stack[istackptr].fp = fopen_e(rpn_defns, "r", 0); input_stack[istackptr++].filemode = NO_ECHO; } /* This is the main loop. Code is read in and executed here. */ while (istackptr!=0) { /* istackptr-1 gives index of most recently pushed input file. */ /* This loop implements the command input file stacking. */ #ifdef DEBUG fprintf(stderr, "istackptr = %ld\n", istackptr); #endif while (prompt("rpn> ", !(istackptr-1)), ptr=fgets((code_ptr->text=input), CODE_LEN, input_stack[istackptr-1].fp)) { /* Loop while there's still data in the (istackptr-1)th file. * * The data is put in the code list. */ #ifdef DEBUG fprintf(stderr, "input string: >%s<\n", ptr); #endif /* If we are at the terminal input level and a UDF has been changed * or a memory added, relink the udfs to get any references to the * new udf or memory translated into 'pcode'. */ if ((udf_changed) || memory_added) { #ifdef DEBUG fputs("re-linking udfs", stderr); #endif link_udfs(); udf_changed = memory_added = 0; } code_ptr->position = 0; /* Get rid of new-lines in data from files, and echo data to * * screen if appropriate. */ if (istackptr!=1 && ptr!=NULL) { #ifdef DEBUG fputs("truncating input line", stderr); #endif chop_nl(ptr); if (input_stack[istackptr-1].filemode==ECHO) puts(ptr); } /* Check for and ignore comment lines. */ #ifdef DEBUG fputs("checking for comment line", stderr); #endif if (strncmp(ptr, "/*", 2)==0) continue; /* Finally, push input line onto the code stack & execute it. */ #ifdef DEBUG fputs("pushing onto stack and executing", stderr); #endif return_code = execute_code(); cycle_counter = 0; if (code_lev!=1) { fputs("error: code level on return from execute_code is not 1\n", stderr); exit(1); } /* Reset pointers in the current code structure to indicate that the * stuff has been executed. */ #ifdef DEBUG fputs("reseting pointers", stderr); #endif *(code_ptr->text) = 0; code_ptr->position = 0; /* If it's appropriate to print the top of the numeric or logical * * stacks, do so here. */ if (stackptr>=1 && return_code==NUMERIC_FUNC ) printf(choose_format(format_flag, stack[stackptr-1]), ' ', stack[stackptr-1], '\n'); if (lstackptr>=1 && return_code==LOGICAL_FUNC) printf("%s\n", (logicstack[lstackptr-1])?"true":"false"); } /* Close the current input file and go to the one below it on the * * stack. This constitutes popping the command input stack. * */ #ifdef DEBUG fputs("closing input file", stderr); #endif fclose(input_stack[--istackptr].fp); } return(0); }