/* gobal variables */
char *progname = NULL;
+char *iprompt = "<= ";
int mode = 1;
+char *oprompt = "=> ";
int precision = 6;
char **completion_list = NULL;
FILE *fid = ret ? stderr : stdout;
fprintf (fid, "usage: %s\n", progname);
fprintf (fid, " -h : help message\n");
+ fprintf (fid, " -i : input prompt (%s)\n", iprompt);
fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
+ fprintf (fid, " -o : output prompt (%s)\n", oprompt);
fprintf (fid, " -p : precision (%d)\n", precision);
fprintf (fid, " -v : verbose level (%d)\n", verbose);
mode = 0;
buffer = buffer_static;
break;
+ case 'i':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (ERROR, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1));
+ return 1;
+ }
+ iprompt = arg;
+ break;
+ case 'o':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1));
+ return 1;
+ }
+ oprompt = arg;
+ break;
case 'p':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
}
}
- /* format */
- format[5] = '0' + precision;
+ /* set format */
+ set_format (oprompt, precision);
/* completion list*/
completion_list = generate_completion_list ();
char *line[BUFFER_SIZE] = {0};
if (mode) {
- if ((buffer = readline ("<= ")) == NULL) {
+ if ((buffer = readline (iprompt)) == NULL) {
break;
}
} else if (element != NULL) {
VERBOSE (INFO, print_element (element, 0));
answer = evaluate_element (element, 0);
- fprintf (stdout, format, answer);
- fflush (stdout);
+ print (answer);
delelement (element);
ret = 0;
}
free_completion_list (completion_list);
+ free_format ();
+
return ret;
}
// test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
// test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
// test: calc.exe error 2>&1 | grep -q 'invalid option'
+// test: calc.exe -i 2>&1 | grep -q 'missing input prompt'
+// test: calc.exe -o 2>&1 | grep -q 'missing output prompt'
// test: calc.exe -p 2>&1 | grep -q 'missing precision'
// test: calc.exe -v 2>&1 | grep -q 'missing verbose'
+// test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1'
+// test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2'
// test: echo "1 + 2" | calc.exe | grep -q '=> 3'
// test: echo "1 - 2" | calc.exe | grep -q '=> -1'
// test: echo "2 * 3" | calc.exe | grep -q '=> 6'
#define STORAGE_SIZE 10
double storage[STORAGE_SIZE] = {0};
-char format[9] = "=> %..g\n";
+char *format = NULL;
/* compare codes */
return ret;
}
+/* print function */
+
+void set_format (char *prompt, int precision)
+{
+ char buffer[128] = {0};
+ sprintf (buffer, "%s%%.%dg\n", prompt, precision);
+ free_format ();
+ format = strdup (buffer);
+}
+
+void free_format ()
+{
+ if (format) {
+ free (format);
+ format = NULL;
+ }
+}
+
+double print (double value)
+{
+ fprintf (stdout, format ? format : "=> %.6g\n", value);
+ fflush (stdout);
+ return value;
+}
+
/* quit function */
void quit (void)
}
case While: return while_do (root->ops[0], root->ops[1]);
case Prog: return program_do (root->ops, root->nbops);
- case Print: printf (format, op0); return op0;
+ case Print: return print (op0);
}
return 0;