1 /* Readline support for calc program.
3 Copyright 2000, 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
12 This program is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see http://www.gnu.org/licenses/. */
19 #include "calc-common.h"
22 #include <stdio.h> /* for FILE for old versions of readline/readline.h */
23 #include <stdlib.h> /* for free */
24 #include <string.h> /* for strdup */
25 #include <unistd.h> /* for isatty */
26 #include <readline/readline.h>
27 #include <readline/history.h>
32 /* change this to "#define TRACE(x) x" for a few diagnostics */
36 #define MIN(x,y) ((x) < (y) ? (x) : (y))
39 calc_completion_entry (const char *text, int state)
41 static int index, len;
49 TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n",
50 text, state, index, len));
51 while ((name = calc_keywords[index].name) != NULL)
54 if (memcmp (name, text, len) == 0)
55 return (strdup (name));
61 calc_init_readline (void)
63 /* By default use readline when the input is a tty. It's a bit contrary
64 to the GNU interface conventions to make the behaviour depend on where
65 the input is coming from, but this is pretty convenient. */
66 if (calc_option_readline == -1)
68 calc_option_readline = isatty (fileno (stdin));
69 TRACE (printf ("calc_option_readline %d\n", calc_option_readline));
72 if (calc_option_readline)
74 printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version);
75 printf ("Type \"help\" for help.\n");
76 rl_readline_name = "gmp-calc";
77 rl_completion_entry_function = calc_completion_entry;
82 /* This function is supposed to return YY_NULL to indicate EOF, but that
83 constant is only in calclex.c and we don't want to clutter calclex.l with
84 this readline stuff, so instead just hard code 0 for YY_NULL. That's
85 it's defined value on unix anyway. */
88 calc_input (char *buf, size_t max_size)
90 if (calc_option_readline)
92 static char *line = NULL;
93 static size_t line_size = 0;
94 static size_t upto = 0;
97 if (upto >= line_size)
102 line = readline (calc_more_input ? "more> " : "> ");
106 TRACE (printf ("readline: %s\n", line));
111 line_size = strlen (line);
112 line[line_size] = '\n';
117 copy_size = MIN (line_size-upto, max_size);
118 memcpy (buf, line+upto, copy_size);
125 return fread (buf, 1, max_size, stdin);
130 /* This redefined input() might let a traditional lex use the readline
131 support here. Apparently POSIX doesn't specify whether an override like
132 this will work, so maybe it'll work or maybe it won't. This function is
133 also not particularly efficient, but don't worry about that, since flex
134 is the preferred parser. */
140 if (calc_input (&c, 1) != 1)
146 #endif /* WITH_READLINE */