summaryrefslogtreecommitdiff
path: root/demos
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2001-12-21 17:34:18 +0100
committerKevin Ryde <user42@zip.com.au>2001-12-21 17:34:18 +0100
commit2fbd7eddd1d87c9584949be1c78901af699a8129 (patch)
treeaa73eeabdb373bb7b66490ba7efa608dfc48da7b /demos
parent1016a8e3a3613142fead4ff3462aaf4d52f0a356 (diff)
downloadgmp-2fbd7eddd1d87c9584949be1c78901af699a8129.tar.gz
* demos/calc/calcread.c, demos/calc/calc-common.h,
demos/calc/calc-config-h.in, demos/calc/README: New files.
Diffstat (limited to 'demos')
-rw-r--r--demos/calc/README66
-rw-r--r--demos/calc/calcread.c147
2 files changed, 213 insertions, 0 deletions
diff --git a/demos/calc/README b/demos/calc/README
new file mode 100644
index 000000000..c90326d02
--- /dev/null
+++ b/demos/calc/README
@@ -0,0 +1,66 @@
+Copyright 2001 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+This program is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free Software
+Foundation; either version 2 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+
+
+ DEMONSTRATION CALCULATOR PROGRAM
+
+
+This is a simple program, meant only to show one way to use GMP with yacc
+and lex to make a calculator. Usage and comments on the implementation can
+be found in calc.y.
+
+Within a GMP build tree, the generated Makefile can be used to build the
+program,
+
+ make calc
+
+(or on a DOS system, "make calc.exe").
+
+Elsewhere, once GMP has been installed, the program can be compiled with for
+instance
+
+ gcc calc.c calclex.c -lgmp -o calc
+
+Or if GNU readline is used then
+
+ gcc calc.c calclex.c calcread.c -lgmp -lreadline -o calc
+
+(again, on a DOS system "-o calc.exe").
+
+Readline support can be enabled or disabled in calc-config.h. That file is
+created by the GMP ./configure based on the --with-readline option. The
+default is --with-readline=detect, which means to use readline if available.
+"yes" can be used to force it to be used, or "no" to not use it.
+
+The supplied calc.c was generated by GNU bison, but a standard yacc should
+work too.
+
+The supplied calclex.c was generated by GNU flex, but a standard lex should
+work too. The readline support may or may not work with a standard lex (see
+comments with input() in calcread.c). Note also that a standard lex will
+require its library "-ll" on the compile command line. "./configure" sets
+this up in the GMP build tree Makefile.
+
+
+
+----------------
+Local variables:
+mode: text
+fill-column: 76
+End:
diff --git a/demos/calc/calcread.c b/demos/calc/calcread.c
new file mode 100644
index 000000000..0afb66e9f
--- /dev/null
+++ b/demos/calc/calcread.c
@@ -0,0 +1,147 @@
+/* Readline support for calc program.
+
+Copyright 2000, 2001 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+This program is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free Software
+Foundation; either version 2 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#include "calc-common.h"
+
+#if WITH_READLINE
+#include <stdio.h> /* for FILE for old versions of readline/readline.h */
+#include <stdlib.h> /* for free */
+#include <string.h> /* for strdup */
+#include <unistd.h> /* for isatty */
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include "gmp.h"
+
+
+/* change this to "#define TRACE(x) x" for a few diagnostics */
+#define TRACE(x)
+
+
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+
+char *
+calc_completion_entry (const char *text, int state)
+{
+ static int index, len;
+ char *name;
+
+ if (!state)
+ {
+ index = 0;
+ len = strlen (text);
+ }
+ TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n",
+ text, state, index, len));
+ while ((name = calc_keywords[index].name) != NULL)
+ {
+ index++;
+ if (memcmp (name, text, len) == 0)
+ return (strdup (name));
+ }
+ return NULL;
+}
+
+void
+calc_init_readline (void)
+{
+ /* By default use readline when the input is a tty. It's a bit contrary
+ to the GNU interface conventions to make the behaviour depend on where
+ the input is coming from, but this is pretty convenient. */
+ if (calc_option_readline == -1)
+ {
+ calc_option_readline = isatty (fileno (stdin));
+ TRACE (printf ("calc_option_readline %d\n", calc_option_readline));
+ }
+
+ if (calc_option_readline)
+ {
+ printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version);
+ printf ("Type \"help\" for help.\n");
+ rl_readline_name = "gmp-calc";
+ rl_completion_entry_function = calc_completion_entry;
+ }
+}
+
+
+/* This function is supposed to return YY_NULL to indicate EOF, but that
+ constant is only in calclex.c and we don't want to clutter calclex.l with
+ this readline stuff, so instead just hard code 0 for YY_NULL. That's
+ it's defined value on unix anyway. */
+
+int
+calc_input (char *buf, size_t max_size)
+{
+ if (calc_option_readline)
+ {
+ static char *line = NULL;
+ static size_t line_size = 0;
+ static size_t upto = 0;
+ size_t copy_size;
+
+ if (upto >= line_size)
+ {
+ if (line != NULL)
+ free (line);
+
+ line = readline (calc_more_input ? "more> " : "> ");
+ calc_more_input = 1;
+ if (line == NULL)
+ return 0;
+ TRACE (printf ("readline: %s\n", line));
+
+ if (line[0] != '\0')
+ add_history (line);
+
+ line_size = strlen (line);
+ line[line_size] = '\n';
+ line_size++;
+ upto = 0;
+ }
+
+ copy_size = MIN (line_size-upto, max_size);
+ memcpy (buf, line+upto, copy_size);
+ upto += copy_size;
+ return copy_size;
+ }
+ else
+ {
+ /* not readline */
+ return fread (buf, 1, max_size, stdin);
+ }
+}
+
+
+/* This redefined input() might let a traditional lex use the readline
+ support here. Apparently POSIX doesn't specify whether an override like
+ this will work, so maybe it'll work or maybe it won't. This function is
+ also not particularly efficient, but don't worry about that, since flex
+ is the preferred parser. */
+
+int
+input (void)
+{
+ char c;
+ if (calc_input (&c, 1) != 1)
+ return EOF;
+ else
+ return (int) c;
+}
+
+#endif /* WITH_READLINE */