GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Readline support for calc program.12Copyright 2000, 2001 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56This program is free software; you can redistribute it and/or modify it under7the terms of the GNU General Public License as published by the Free Software8Foundation; either version 2 of the License, or (at your option) any later9version.1011This program is distributed in the hope that it will be useful, but WITHOUT ANY12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A13PARTICULAR PURPOSE. See the GNU General Public License for more details.1415You should have received a copy of the GNU General Public License along with16this program; if not, write to the Free Software Foundation, Inc., 51 Franklin17Street, Fifth Floor, Boston, MA 02110-1301, USA. */1819#include "calc-common.h"2021#if WITH_READLINE22#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>2829#include "gmp.h"303132/* change this to "#define TRACE(x) x" for a few diagnostics */33#define TRACE(x)343536#define MIN(x,y) ((x) < (y) ? (x) : (y))3738char *39calc_completion_entry (const char *text, int state)40{41static int index, len;42char *name;4344if (!state)45{46index = 0;47len = strlen (text);48}49TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n",50text, state, index, len));51while ((name = calc_keywords[index].name) != NULL)52{53index++;54if (memcmp (name, text, len) == 0)55return (strdup (name));56}57return NULL;58}5960void61calc_init_readline (void)62{63/* By default use readline when the input is a tty. It's a bit contrary64to the GNU interface conventions to make the behaviour depend on where65the input is coming from, but this is pretty convenient. */66if (calc_option_readline == -1)67{68calc_option_readline = isatty (fileno (stdin));69TRACE (printf ("calc_option_readline %d\n", calc_option_readline));70}7172if (calc_option_readline)73{74printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version);75printf ("Type \"help\" for help.\n");76rl_readline_name = "gmp-calc";77rl_completion_entry_function = calc_completion_entry;78}79}808182/* This function is supposed to return YY_NULL to indicate EOF, but that83constant is only in calclex.c and we don't want to clutter calclex.l with84this readline stuff, so instead just hard code 0 for YY_NULL. That's85it's defined value on unix anyway. */8687int88calc_input (char *buf, size_t max_size)89{90if (calc_option_readline)91{92static char *line = NULL;93static size_t line_size = 0;94static size_t upto = 0;95size_t copy_size;9697if (upto >= line_size)98{99if (line != NULL)100free (line);101102line = readline (calc_more_input ? "more> " : "> ");103calc_more_input = 1;104if (line == NULL)105return 0;106TRACE (printf ("readline: %s\n", line));107108if (line[0] != '\0')109add_history (line);110111line_size = strlen (line);112line[line_size] = '\n';113line_size++;114upto = 0;115}116117copy_size = MIN (line_size-upto, max_size);118memcpy (buf, line+upto, copy_size);119upto += copy_size;120return copy_size;121}122else123{124/* not readline */125return fread (buf, 1, max_size, stdin);126}127}128129130/* This redefined input() might let a traditional lex use the readline131support here. Apparently POSIX doesn't specify whether an override like132this will work, so maybe it'll work or maybe it won't. This function is133also not particularly efficient, but don't worry about that, since flex134is the preferred parser. */135136int137input (void)138{139char c;140if (calc_input (&c, 1) != 1)141return EOF;142else143return (int) c;144}145146#endif /* WITH_READLINE */147148149