/* JW 20051126: * Wrapper around const_express() called by try_to_get_string(). * Disallows top level + and - operators. * This enables things like set xtics ('-\pi' -pi, '-\pi/2' -pi/2.) */ struct value * const_string_express(struct value *valptr) { string_result_only = TRUE; const_express(valptr); string_result_only = FALSE; return (valptr); }
double real_expression() { double result; struct value a; result = real(const_express(&a)); gpfree_string(&a); return result; }
void define() { register int start_token; /* the 1st token in the function definition */ register struct udvt_entry *udv; register struct udft_entry *udf; if (equals(c_token + 1, "(")) { /* function ! */ int dummy_num = 0; struct at_type *at_tmp; char save_dummy[MAX_NUM_VAR][MAX_ID_LEN + 1]; memcpy(save_dummy, c_dummy_var, sizeof(save_dummy)); start_token = c_token; do { c_token += 2; /* skip to the next dummy */ copy_str(c_dummy_var[dummy_num++], c_token, MAX_ID_LEN); } while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR)); if (equals(c_token + 1, ",")) int_error("function contains too many parameters", c_token + 2); c_token += 3; /* skip (, dummy, ) and = */ if (END_OF_COMMAND) int_error("function definition expected", c_token); udf = dummy_func = add_udf(start_token); if ((at_tmp = perm_at()) == (struct at_type *) NULL) int_error("not enough memory for function", start_token); if (udf->at) /* already a dynamic a.t. there */ free((char *) udf->at); /* so free it first */ udf->at = at_tmp; /* before re-assigning it. */ memcpy(c_dummy_var, save_dummy, sizeof(save_dummy)); m_capture(&(udf->definition), start_token, c_token - 1); dummy_func = NULL; /* dont let anyone else use our workspace */ } else { /* variable ! */ start_token = c_token; c_token += 2; udv = add_udv(start_token); (void) const_express(&(udv->udv_value)); udv->udv_undef = FALSE; } }
static void prepare_call(int calltype) { struct udvt_entry *udv; int argindex; if (calltype == 2) { call_argc = 0; while (!END_OF_COMMAND && call_argc <= 9) { call_args[call_argc] = try_to_get_string(); if (!call_args[call_argc]) { int save_token = c_token; /* This catches call "file" STRINGVAR (expression) */ if (type_udv(c_token) == STRING) { call_args[call_argc] = gp_strdup(add_udv(c_token)->udv_value.v.string_val); c_token++; /* Evaluates a parenthesized expression and store the result in a string */ } else if (equals(c_token, "(")) { char val_as_string[32]; struct value a; const_express(&a); switch(a.type) { case CMPLX: /* FIXME: More precision? Some way to provide a format? */ sprintf(val_as_string, "%g", a.v.cmplx_val.real); call_args[call_argc] = gp_strdup(val_as_string); break; default: int_error(save_token, "Unrecognized argument type"); break; case INTGR: sprintf(val_as_string, "%d", a.v.int_val); call_args[call_argc] = gp_strdup(val_as_string); break; } /* old (pre version 5) style wrapping of bare tokens as strings */ /* is still useful for passing unquoted numbers */ } else { m_capture(&call_args[call_argc], c_token, c_token); c_token++; } } call_argc++; } lf_head->c_token = c_token; if (!END_OF_COMMAND) int_error(++c_token, "too many arguments for 'call <file>'"); } else if (calltype == 5) { /* lf_push() moved our call arguments from call_args[] to lf->call_args[] */ /* call_argc was determined at program entry */ for (argindex = 0; argindex < 10; argindex++) { call_args[argindex] = lf_head->call_args[argindex]; lf_head->call_args[argindex] = NULL; /* just to be safe */ } } else { /* "load" command has no arguments */ call_argc = 0; } /* Old-style "call" arguments were referenced as $0 ... $9 and $# */ /* New-style has ARG0 = script-name, ARG1 ... ARG9 and ARGC */ /* FIXME: If we defined these on entry, we could use get_udv* here */ udv = add_udv_by_name("ARGC"); Ginteger(&(udv->udv_value), call_argc); udv->udv_undef = FALSE; udv = add_udv_by_name("ARG0"); gpfree_string(&(udv->udv_value)); Gstring(&(udv->udv_value), gp_strdup(lf_head->name)); udv->udv_undef = FALSE; for (argindex = 1; argindex <= 9; argindex++) { char *arg = gp_strdup(call_args[argindex-1]); udv = add_udv_by_name(argname[argindex]); gpfree_string(&(udv->udv_value)); Gstring(&(udv->udv_value), arg ? arg : gp_strdup("")); udv->udv_undef = FALSE; } }
static int command() { FILE *fp; int i; /* string holding name of save or load file */ char sv_file[MAX_LINE_LEN + 1]; for (i = 0; i < MAX_NUM_VAR; i++) c_dummy_var[i][0] = NUL; /* no dummy variables */ if (is_definition(c_token)) define(); else if (almost_equals(c_token, "h$elp") || equals(c_token, "?")) { c_token++; do_help(1); } else if (equals(c_token, "testtime")) { /* given a format and a time string, exercise the time code */ char format[160], string[160]; struct tm tm; double secs; if (isstring(++c_token)) { quote_str(format, c_token, 159); if (isstring(++c_token)) { quote_str(string, c_token++, 159); memset(&tm, 0, sizeof(tm)); gstrptime(string, format, &tm); secs = gtimegm(&tm); fprintf(stderr, "internal = %f - %d/%d/%d::%d:%d:%d , wday=%d, yday=%d\n", secs, tm.tm_mday, tm.tm_mon + 1, tm.tm_year % 100, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday, tm.tm_yday); memset(&tm, 0, sizeof(tm)); ggmtime(&tm, secs); gstrftime(string, 159, format, secs); fprintf(stderr, "convert back \"%s\" - %d/%d/%d::%d:%d:%d , wday=%d, yday=%d\n", string, tm.tm_mday, tm.tm_mon + 1, tm.tm_year % 100, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday, tm.tm_yday); } } } else if (almost_equals(c_token, "test")) { c_token++; test_term(); } else if (almost_equals(c_token, "scr$eendump")) { c_token++; #ifdef _Windows screen_dump(); #else fputs("screendump not implemented\n", stderr); #endif } else if (almost_equals(c_token, "pa$use")) { struct value a; int sleep_time, text = 0; char buf[MAX_LINE_LEN + 1]; c_token++; sleep_time = (int) real(const_express(&a)); buf[0] = NUL; if (!(END_OF_COMMAND)) { if (!isstring(c_token)) int_error("expecting string", c_token); else { quote_str(buf, c_token, MAX_LINE_LEN); ++c_token; #ifdef _Windows if (sleep_time >= 0) #else # ifdef OS2 if (strcmp(term->name, "pm") != 0 || sleep_time >= 0) # else # ifdef MTOS if (strcmp(term->name, "mtos") != 0 || sleep_time >= 0) # endif /* MTOS */ # endif /* OS2 */ #endif /* _Windows */ fputs(buf, stderr); text = 1; } } if (sleep_time < 0) { #ifdef _Windows if (!Pause(buf)) bail_to_command_line(); #else # ifdef OS2 if (strcmp(term->name, "pm") == 0 && sleep_time < 0) { int rc; if ((rc = PM_pause(buf)) == 0) bail_to_command_line(); else if (rc == 2) { fputs(buf, stderr); text = 1; (void) fgets(buf, MAX_LINE_LEN, stdin); } } # else /* !OS2 */ # ifdef _Macintosh if (strcmp(term->name, "macintosh") == 0 && sleep_time < 0) Pause(sleep_time); # else /* !_Macintosh */ # ifdef MTOS if (strcmp(term->name, "mtos") == 0) { int MTOS_pause(char *buf); int rc; if ((rc = MTOS_pause(buf)) == 0) bail_to_command_line(); else if (rc == 2) { fputs(buf, stderr); text = 1; (void) fgets(buf, MAX_LINE_LEN, stdin); } } else if (strcmp(term->name, "atari") == 0) { char *readline(char *); char *line = readline(""); if (line) free(line); } else (void) fgets(buf, MAX_LINE_LEN, stdin); # else /* !MTOS */ # ifdef ATARI if (strcmp(term->name, "atari") == 0) { char *readline(char *); char *line = readline(""); if (line) free(line); } else (void) fgets(buf, MAX_LINE_LEN, stdin); # else /* !ATARI */ (void) fgets(buf, MAX_LINE_LEN, stdin); /* Hold until CR hit. */ # endif /* !ATARI */ # endif /* !MTOS */ # endif /* !_Macintosh */ # endif /* !OS2 */ #endif } if (sleep_time > 0) GP_SLEEP(sleep_time); if (text != 0 && sleep_time >= 0) fputc('\n', stderr); screen_ok = FALSE; } else if (almost_equals(c_token, "pr$int")) { int need_space = 0; /* space printed between two expressions only */ screen_ok = FALSE; do { ++c_token; if (isstring(c_token)) { char s[MAX_LINE_LEN]; quote_str(s, c_token, MAX_LINE_LEN); fputs(s, stderr); need_space = 0; ++c_token; } else { struct value a; (void) const_express(&a); if (need_space) putc(' ', stderr); need_space = 1; disp_value(stderr, &a); } } while (!END_OF_COMMAND && equals(c_token, ",")); (void) putc('\n', stderr); } else if (almost_equals(c_token, "fit")) { ++c_token; do_fit(); } else if (almost_equals(c_token, "up$date")) { char tmps[80]; char tmps2[80]; /* Have to initialise tmps2, otherwise * update() cannot decide whether a valid * filename was given. lh */ tmps2[0] = NUL; if (!isstring(++c_token)) int_error("Parameter filename expected", c_token); quote_str(tmps, c_token++, 80); if (!(END_OF_COMMAND)) { if (!isstring(c_token)) int_error("New parameter filename expected", c_token); else quote_str(tmps2, c_token++, 80); } update(tmps, tmps2); } else if (almost_equals(c_token, "p$lot")) { plot_token = c_token++; SET_CURSOR_WAIT; plotrequest(); SET_CURSOR_ARROW; } else if (almost_equals(c_token, "sp$lot")) { plot_token = c_token++; SET_CURSOR_WAIT; plot3drequest(); SET_CURSOR_ARROW; } else if (almost_equals(c_token, "rep$lot")) { if (replot_line[0] == NUL) int_error("no previous plot", c_token); c_token++; SET_CURSOR_WAIT; replotrequest(); SET_CURSOR_ARROW; } else if (almost_equals(c_token, "se$t")) set_command(); else if (almost_equals(c_token, "res$et")) reset_command(); else if (almost_equals(c_token, "sh$ow")) show_command(); else if (almost_equals(c_token, "cl$ear")) { term_start_plot(); if (multiplot && term->fillbox) { unsigned int x1 = (unsigned int) (xoffset * term->xmax); unsigned int y1 = (unsigned int) (yoffset * term->ymax); unsigned int width = (unsigned int) (xsize * term->xmax); unsigned int height = (unsigned int) (ysize * term->ymax); (*term->fillbox) (0, x1, y1, width, height); } term_end_plot(); screen_ok = FALSE; c_token++; } else if (almost_equals(c_token, "she$ll")) { do_shell(); screen_ok = FALSE; c_token++; } else if (almost_equals(c_token, "sa$ve")) { if (almost_equals(++c_token, "f$unctions")) { if (!isstring(++c_token)) int_error("expecting filename", c_token); else { quote_str(sv_file, c_token, MAX_LINE_LEN); save_functions(fopen(sv_file, "w")); } } else if (almost_equals(c_token, "v$ariables")) { if (!isstring(++c_token)) int_error("expecting filename", c_token); else { quote_str(sv_file, c_token, MAX_LINE_LEN); save_variables(fopen(sv_file, "w")); } } else if (almost_equals(c_token, "s$et")) { if (!isstring(++c_token)) int_error("expecting filename", c_token); else { quote_str(sv_file, c_token, MAX_LINE_LEN); save_set(fopen(sv_file, "w")); } } else if (isstring(c_token)) { quote_str(sv_file, c_token, MAX_LINE_LEN); save_all(fopen(sv_file, "w")); } else { int_error("filename or keyword 'functions', 'variables', or 'set' expected", c_token); } c_token++; } else if (almost_equals(c_token, "l$oad")) { if (!isstring(++c_token)) int_error("expecting filename", c_token); else { quote_str(sv_file, c_token, MAX_LINE_LEN); /* load_file(fp=fopen(sv_file, "r"), sv_file, FALSE); OLD * DBT 10/6/98 handle stdin as special case * passes it on to load_file() so that it gets * pushed on the stack and recusion will work, etc */ fp = strcmp(sv_file, "-") ? fopen(sv_file, "r") : stdin; load_file(fp, sv_file, FALSE); /* input_line[] and token[] now destroyed! */ c_token = num_tokens = 0; } } else if (almost_equals(c_token, "ca$ll")) { if (!isstring(++c_token)) int_error("expecting filename", c_token); else { quote_str(sv_file, c_token, MAX_LINE_LEN); load_file(fopen(sv_file, "r"), sv_file, TRUE); /* Argument list follows filename */ /* input_line[] and token[] now destroyed! */ c_token = num_tokens = 0; } } else if (almost_equals(c_token, "if")) { double exprval; struct value t; if (!equals(++c_token, "(")) /* no expression */ int_error("expecting (expression)", c_token); exprval = real(const_express(&t)); if (exprval != 0.0) { /* fake the condition of a ';' between commands */ int eolpos = token[num_tokens - 1].start_index + token[num_tokens - 1].length; --c_token; token[c_token].length = 1; token[c_token].start_index = eolpos + 2; input_line[eolpos + 2] = ';'; input_line[eolpos + 3] = NUL; } else c_token = num_tokens = 0; } else if (almost_equals(c_token, "rer$ead")) { fp = lf_top(); if (fp != (FILE *) NULL) rewind(fp); c_token++; } else if (almost_equals(c_token, "cd")) { if (!isstring(++c_token)) int_error("expecting directory name", c_token); else { quote_str(sv_file, c_token, MAX_LINE_LEN); if (changedir(sv_file)) { int_error("Can't change to this directory", c_token); } c_token++; } } else if (almost_equals(c_token, "pwd")) { GP_GETCWD(sv_file, sizeof(sv_file)); fprintf(stderr, "%s\n", sv_file); c_token++; } else if (almost_equals(c_token, "ex$it") || almost_equals(c_token, "q$uit")) { /* graphics will be tidied up in main */ return (1); } else if (!equals(c_token, ";")) { /* null statement */ #ifdef OS2 if (_osmode == OS2_MODE) { if (token[c_token].is_token) { int rc; rc = ExecuteMacro(input_line + token[c_token].start_index, token[c_token].length); if (rc == 0) { c_token = num_tokens = 0; return (0); } } } #endif int_error("invalid command", c_token); } return (0); }
/* return TRUE if a command match, FALSE if not */ static TBOOLEAN show_one() { if (almost_equals(c_token, "ac$tion_table") || equals(c_token, "at")) { c_token++; show_at(); c_token++; } else if (almost_equals(c_token, "ar$row")) { struct value a; int tag = 0; c_token++; if (!END_OF_COMMAND) { tag = (int) real(const_express(&a)); if (tag <= 0) int_error("tag must be > zero", c_token); } (void) putc('\n', stderr); show_arrow(tag); } else if (almost_equals(c_token, "au$toscale")) { (void) putc('\n', stderr); show_autoscale(); c_token++; } else if (almost_equals(c_token, "b$ars")) { (void) putc('\n', stderr); show_bars(); c_token++; } else if (almost_equals(c_token, "bor$der")) { (void) putc('\n', stderr); show_border(); c_token++; } else if (almost_equals(c_token, "box$width")) { (void) putc('\n', stderr); show_boxwidth(); c_token++; } else if (almost_equals(c_token, "c$lip")) { (void) putc('\n', stderr); show_clip(); c_token++; } else if (almost_equals(c_token, "ma$pping")) { (void) putc('\n', stderr); show_mapping(); c_token++; } else if (almost_equals(c_token, "co$ntour") || almost_equals(c_token, "cn$trparam")) { (void) putc('\n', stderr); show_contour(); c_token++; } else if (almost_equals(c_token, "da$ta")) { c_token++; if (!almost_equals(c_token, "s$tyle")) int_error("expecting keyword 'style'", c_token); (void) putc('\n', stderr); show_style("data", data_style); c_token++; } else if (almost_equals(c_token, "dg$rid3d")) { (void) putc('\n', stderr); show_dgrid3d(); c_token++; } else if (almost_equals(c_token, "du$mmy")) { (void) fprintf(stderr, "\n\tdummy variables are \"%s\" and \"%s\"\n", dummy_var[0], dummy_var[1]); c_token++; } else if (almost_equals(c_token, "fo$rmat")) { show_format(); c_token++; } else if (almost_equals(c_token, "fu$nctions")) { c_token++; if (almost_equals(c_token, "s$tyle")) { (void) putc('\n', stderr); show_style("functions", func_style); c_token++; } else show_functions(); } else if (almost_equals(c_token, "lo$gscale")) { (void) putc('\n', stderr); show_logscale(); c_token++; } else if (almost_equals(c_token, "of$fsets")) { (void) putc('\n', stderr); show_offsets(); c_token++; } else if (almost_equals(c_token, "ma$rgin")) { (void) putc('\n', stderr); show_margin(); c_token++; } else if (almost_equals(c_token, "o$utput")) { (void) putc('\n', stderr); show_output(); c_token++; } else if (almost_equals(c_token, "tit$le")) { (void) putc('\n', stderr); show_xyzlabel("title", &title); c_token++; } else if (almost_equals(c_token, "mis$sing")) { (void) putc('\n', stderr); show_missing(); c_token++; } else if (almost_equals(c_token, "xl$abel")) { (void) putc('\n', stderr); show_xyzlabel("xlabel", &xlabel); c_token++; } else if (almost_equals(c_token, "x2l$abel")) { (void) putc('\n', stderr); show_xyzlabel("x2label", &x2label); c_token++; } else if (almost_equals(c_token, "yl$abel")) { (void) putc('\n', stderr); show_xyzlabel("ylabel", &ylabel); c_token++; } else if (almost_equals(c_token, "y2l$abel")) { (void) putc('\n', stderr); show_xyzlabel("y2label", &y2label); c_token++; } else if (almost_equals(c_token, "zl$abel")) { (void) putc('\n', stderr); show_xyzlabel("zlabel", &zlabel); c_token++; } else if (almost_equals(c_token, "keyt$itle")) { (void) putc('\n', stderr); show_keytitle(); c_token++; } else if (almost_equals(c_token, "xda$ta")) { (void) putc('\n', stderr); show_datatype("xdata", FIRST_X_AXIS); c_token++; } else if (almost_equals(c_token, "yda$ta")) { (void) putc('\n', stderr); show_datatype("ydata", FIRST_Y_AXIS); c_token++; } else if (almost_equals(c_token, "x2da$ta")) { (void) putc('\n', stderr); show_datatype("x2data", SECOND_X_AXIS); c_token++; } else if (almost_equals(c_token, "y2da$ta")) { (void) putc('\n', stderr); show_datatype("y2data", SECOND_Y_AXIS); c_token++; } else if (almost_equals(c_token, "zda$ta")) { (void) putc('\n', stderr); show_datatype("zdata", FIRST_Z_AXIS); c_token++; } else if (almost_equals(c_token, "timef$mt")) { (void) putc('\n', stderr); show_timefmt(); c_token++; } else if (almost_equals(c_token, "loca$le")) { (void) putc('\n', stderr); show_locale(); c_token++; } else if (almost_equals(c_token, "xzero$axis")) { (void) putc('\n', stderr); show_xzeroaxis(); c_token++; } else if (almost_equals(c_token, "yzero$axis")) { (void) putc('\n', stderr); show_yzeroaxis(); c_token++; } else if (almost_equals(c_token, "zeroa$xis")) { (void) putc('\n', stderr); show_xzeroaxis(); show_yzeroaxis(); c_token++; } else if (almost_equals(c_token, "la$bel")) { struct value a; int tag = 0; c_token++; if (!END_OF_COMMAND) { tag = (int) real(const_express(&a)); if (tag <= 0) int_error("tag must be > zero", c_token); } (void) putc('\n', stderr); show_label(tag); } else if (almost_equals(c_token, "li$nestyle") || equals(c_token, "ls")) { struct value a; int tag = 0; c_token++; if (!END_OF_COMMAND) { tag = (int) real(const_express(&a)); if (tag <= 0) int_error("tag must be > zero", c_token); } (void) putc('\n', stderr); show_linestyle(tag); } else if (almost_equals(c_token, "g$rid")) { (void) putc('\n', stderr); show_grid(); c_token++; } else if (almost_equals(c_token, "mxt$ics")) { (void) putc('\n', stderr); show_mtics(mxtics, mxtfreq, "x"); c_token++; } else if (almost_equals(c_token, "myt$ics")) { (void) putc('\n', stderr); show_mtics(mytics, mytfreq, "y"); c_token++; } else if (almost_equals(c_token, "mzt$ics")) { (void) putc('\n', stderr); show_mtics(mztics, mztfreq, "z"); c_token++; } else if (almost_equals(c_token, "mx2t$ics")) { (void) putc('\n', stderr); show_mtics(mx2tics, mx2tfreq, "x2"); c_token++; } else if (almost_equals(c_token, "my2t$ics")) { (void) putc('\n', stderr); show_mtics(my2tics, my2tfreq, "y2"); c_token++; } else if (almost_equals(c_token, "k$ey")) { (void) putc('\n', stderr); show_key(); c_token++; } else return (FALSE); return TRUE; }