Annotation of OpenXM_contrib/gmp/demos/calc/calcread.c, Revision 1.1.1.1
1.1 ohara 1: /* Readline support for calc program.
2:
3: Copyright 2000, 2001 Free Software Foundation, Inc.
4:
5: This file is part of the GNU MP Library.
6:
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 2 of the License, or (at your option) any later
10: version.
11:
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.
15:
16: You should have received a copy of the GNU General Public License along with
17: this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18: Place - Suite 330, Boston, MA 02111-1307, USA. */
19:
20: #include "calc-common.h"
21:
22: #if WITH_READLINE
23: #include <stdio.h> /* for FILE for old versions of readline/readline.h */
24: #include <stdlib.h> /* for free */
25: #include <string.h> /* for strdup */
26: #include <unistd.h> /* for isatty */
27: #include <readline/readline.h>
28: #include <readline/history.h>
29:
30: #include "gmp.h"
31:
32:
33: /* change this to "#define TRACE(x) x" for a few diagnostics */
34: #define TRACE(x)
35:
36:
37: #define MIN(x,y) ((x) < (y) ? (x) : (y))
38:
39: char *
40: calc_completion_entry (const char *text, int state)
41: {
42: static int index, len;
43: char *name;
44:
45: if (!state)
46: {
47: index = 0;
48: len = strlen (text);
49: }
50: TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n",
51: text, state, index, len));
52: while ((name = calc_keywords[index].name) != NULL)
53: {
54: index++;
55: if (memcmp (name, text, len) == 0)
56: return (strdup (name));
57: }
58: return NULL;
59: }
60:
61: void
62: calc_init_readline (void)
63: {
64: /* By default use readline when the input is a tty. It's a bit contrary
65: to the GNU interface conventions to make the behaviour depend on where
66: the input is coming from, but this is pretty convenient. */
67: if (calc_option_readline == -1)
68: {
69: calc_option_readline = isatty (fileno (stdin));
70: TRACE (printf ("calc_option_readline %d\n", calc_option_readline));
71: }
72:
73: if (calc_option_readline)
74: {
75: printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version);
76: printf ("Type \"help\" for help.\n");
77: rl_readline_name = "gmp-calc";
78: rl_completion_entry_function = calc_completion_entry;
79: }
80: }
81:
82:
83: /* This function is supposed to return YY_NULL to indicate EOF, but that
84: constant is only in calclex.c and we don't want to clutter calclex.l with
85: this readline stuff, so instead just hard code 0 for YY_NULL. That's
86: it's defined value on unix anyway. */
87:
88: int
89: calc_input (char *buf, size_t max_size)
90: {
91: if (calc_option_readline)
92: {
93: static char *line = NULL;
94: static size_t line_size = 0;
95: static size_t upto = 0;
96: size_t copy_size;
97:
98: if (upto >= line_size)
99: {
100: if (line != NULL)
101: free (line);
102:
103: line = readline (calc_more_input ? "more> " : "> ");
104: calc_more_input = 1;
105: if (line == NULL)
106: return 0;
107: TRACE (printf ("readline: %s\n", line));
108:
109: if (line[0] != '\0')
110: add_history (line);
111:
112: line_size = strlen (line);
113: line[line_size] = '\n';
114: line_size++;
115: upto = 0;
116: }
117:
118: copy_size = MIN (line_size-upto, max_size);
119: memcpy (buf, line+upto, copy_size);
120: upto += copy_size;
121: return copy_size;
122: }
123: else
124: {
125: /* not readline */
126: return fread (buf, 1, max_size, stdin);
127: }
128: }
129:
130:
131: /* This redefined input() might let a traditional lex use the readline
132: support here. Apparently POSIX doesn't specify whether an override like
133: this will work, so maybe it'll work or maybe it won't. This function is
134: also not particularly efficient, but don't worry about that, since flex
135: is the preferred parser. */
136:
137: int
138: input (void)
139: {
140: char c;
141: if (calc_input (&c, 1) != 1)
142: return EOF;
143: else
144: return (int) c;
145: }
146:
147: #endif /* WITH_READLINE */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>