/* depend: */
/* cflags: */
-/* linker: alloc.o argument.o color.o debug.o element.o format.o parser.o program.o stack.o storage.o tabular.o workspace.o -lm -lreadline */
+/* linker: alloc.o argument.o color.o debug.o element.o format.o parser.o program.o readline.o stack.o storage.o tabular.o workspace.o -lm -lreadline */
#include <malloc.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
-#include <readline/readline.h>
-#include <readline/history.h>
-
#include "debug.h"
#include "element.h"
#include "format.h"
#include "parser.h"
+#include "readline.h"
/* constants */
#define BUFFER_SIZE 4096
-#define HISTORY_LEN 10
/* macros */
-#define CEIL(x, y) (((x) + (y) - 1) / (y))
-#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-#define MAX(x, y) (((x) > (y)) ? (x) : (y))
-
/* gobal variables */
char *progname = NULL;
int mode = 1;
char *oprompt = "=> ";
int precision = 6;
-char **completion_list = NULL;
/* help function */
return ret;
}
-/* completion function */
-
-char *generator (const char *text, int state);
-
-char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end)
-{
- rl_attempted_completion_over = 1;
- return rl_completion_matches (text, generator);
-}
-
-char *generator (const char *text, int state)
-{
- static int index, len;
- char *name;
-
- if (!state) {
- index = 0;
- len = strlen(text);
- }
-
- while ((name = completion_list[index++])) {
- if (strncmp (name, text, len) == 0) {
- return strdup (name);
- }
- }
-
- return NULL;
-}
-
-/* edit line */
-char *edit_line = NULL;
-int edit_hook ()
-{
- static int state = 0;
- if (edit_line) {
- if (state == 0) {
- state = 1;
- } else {
- state = 0;
- free (edit_line);
- edit_line = NULL;
- }
- }
- return rl_insert_text (edit_line);
-}
-
-/* history management */
-
-void history ()
-{
- HIST_ENTRY **entries = history_list ();
- if (entries == NULL) {
- VERBOSE (WARNING, fprintf (stdout, "no history\n"));
- } else {
- int i = 0;
- while (entries[i] != NULL) {
- printf ("%d: %s\n", history_length - i, entries[i]->line);
- i++;
- }
- }
-}
-
/* main function */
int main (int argc, char *argv[])
/* set format */
set_format ();
- /* completion list*/
- completion_list = generate_completion_list ();
- rl_attempted_completion_function = completion;
-
- /* readline parameters */
- rl_startup_hook = edit_hook;
- rl_variable_bind ("blink-matching-paren", "On");
- //rl_set_screen_size (50, 40);
+ /* init readline */
+ if (mode) {
+ init_read_line ();
+ }
/* read from input stream */
char *line[BUFFER_SIZE] = {0};
if (mode) {
- if ((buffer = readline (iprompt)) == NULL) {
+ if (read_line (&buffer, iprompt)) {
break;
}
/* check empty line */
- if (strlen (buffer) == 0) {
- free (buffer);
+ if (buffer == NULL) {
continue;
- } else if (strcmp (buffer, ".") == 0) {
- free (buffer);
- break;
}
/* add line into history */
- add_history (buffer);
- VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n",
- where_history (), history_length, buffer));
- if (history_length > HISTORY_LEN) {
- HIST_ENTRY *last = remove_history (0);
- if (last) {
- free_history_entry (last);
- }
- }
+ manage_history (buffer);
+
} else {
printf ("%s", iprompt);
if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
break;
}
- VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
+ }
+
+ if (strcmp (buffer, ".") == 0) {
+ break;
}
/* pre-process buffer */
fflush (stdout);
}
- free_completion_list (completion_list);
+ clean_read_line (buffer);
free_format ();
// 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 + 1" | calc.exe -o '# ' -o 'x ' | grep -q 'x 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'
// test: echo -e 'clr\nsto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
// test: echo -e 'mem (3)\nclr\nquit' | calc.exe -v 3 | grep -q Clear
// test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (1, {cos (arg (1)^2)})\ncall (1, pi/6)\nprog (2, {arg (1) * 3})\ncall (2, 1, 2)\nls' | calc.exe | grep -q 'programs: 2 1'
- // test: echo -e 'prog (1, {arg (2) - arg (1)})\ncall (1, 2, 3)\nls\nedit (1)\nprog (1, {arg (2) + arg (1)})\nedit (1)\ndel (1)\nquit' | calc.exe -v 3 | grep -q bye
- // test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ncall (1, 2, 3)\ncall (2, 1)\nls\nedit (1)\ndel (1)\ndel (3)\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 5 =
+ // test: echo -e 'prog (1, {arg (2) - arg (1)})\ncall (1, 2, 3)\nls\nedit (1)\n\nprog (1, {arg (2) + arg (1)})\nedit (1)\n\ndel (1)\nquit' | calc.exe -v 3 | grep -q bye
+ // test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ncall (1, 2, 3)\ncall (2, 1)\nls\nedit (1)\n\ndel (1)\ndel (3)\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 5 =
// test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ndel (2)\ndel (3)\nls' | calc.exe | grep -q '^programs:$'
// test: echo -e 'erf (1)\nerfc (1)\nquit' | calc.exe -v 3 | grep -q bye
// test: echo -e 'erf ()\nerfc ()' | calc.exe | grep -c error | xargs test 2 =
// test: echo -e '255' | calc.exe -b 10,16 | grep -q '=> ff'
// test: echo -e 'base (-2)\nbase (16, 0)' | calc.exe | grep -c error | xargs test 2 =
// test: echo -e 'base (10, 16)\n255' | calc.exe | grep -q '=> ff'
+ // test: echo -e 'base (10, 16)\nsto (2, 255)\ndisp' | calc.exe | grep -q 'storage: 0 ff 0 0 0 0 0 0 0 0'
// test: echo -e 'base' | calc.exe | grep -q 'base (I/O): 10/10'
// test: echo -e 'deg\nacos (-1)\ngrad\nacos (-1)\nrad\nacos (-1)' | calc.exe | awk 'BEGIN { split("180 200 3.14159", v) } /=>/ { for (i in v) if ($2 == v[i]) n++ } END { exit n != 3 }'
// test: echo -e 'format\nbase\ndeg\ngrad\nrad\nquit' | calc.exe -v 3 | grep -q bye