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 3 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, see https://www.gnu.org/licenses/. */1718#include "calc-common.h"1920#if WITH_READLINE21#include <stdio.h> /* for FILE for old versions of readline/readline.h */22#include <stdlib.h> /* for free */23#include <string.h> /* for strdup */24#include <unistd.h> /* for isatty */25#include <readline/readline.h>26#include <readline/history.h>2728#include "gmp.h"293031/* change this to "#define TRACE(x) x" for a few diagnostics */32#define TRACE(x)333435#define MIN(x,y) ((x) < (y) ? (x) : (y))3637char *38calc_completion_entry (const char *text, int state)39{40static int index, len;41char *name;4243if (!state)44{45index = 0;46len = strlen (text);47}48TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n",49text, state, index, len));50while ((name = calc_keywords[index].name) != NULL)51{52index++;53if (memcmp (name, text, len) == 0)54return (strdup (name));55}56return NULL;57}5859void60calc_init_readline (void)61{62/* By default use readline when the input is a tty. It's a bit contrary63to the GNU interface conventions to make the behaviour depend on where64the input is coming from, but this is pretty convenient. */65if (calc_option_readline == -1)66{67calc_option_readline = isatty (fileno (stdin));68TRACE (printf ("calc_option_readline %d\n", calc_option_readline));69}7071if (calc_option_readline)72{73printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version);74printf ("Type \"help\" for help.\n");75rl_readline_name = "gmp-calc";76rl_completion_entry_function = calc_completion_entry;77}78}798081/* This function is supposed to return YY_NULL to indicate EOF, but that82constant is only in calclex.c and we don't want to clutter calclex.l with83this readline stuff, so instead just hard code 0 for YY_NULL. That's84it's defined value on unix anyway. */8586int87calc_input (char *buf, size_t max_size)88{89if (calc_option_readline)90{91static char *line = NULL;92static size_t line_size = 0;93static size_t upto = 0;94size_t copy_size;9596if (upto >= line_size)97{98if (line != NULL)99free (line);100101line = readline (calc_more_input ? "more> " : "> ");102calc_more_input = 1;103if (line == NULL)104return 0;105TRACE (printf ("readline: %s\n", line));106107if (line[0] != '\0')108add_history (line);109110line_size = strlen (line);111line[line_size] = '\n';112line_size++;113upto = 0;114}115116copy_size = MIN (line_size-upto, max_size);117memcpy (buf, line+upto, copy_size);118upto += copy_size;119return copy_size;120}121else122{123/* not readline */124return fread (buf, 1, max_size, stdin);125}126}127128129/* This redefined input() might let a traditional lex use the readline130support here. Apparently POSIX doesn't specify whether an override like131this will work, so maybe it'll work or maybe it won't. This function is132also not particularly efficient, but don't worry about that, since flex133is the preferred parser. */134135int136input (void)137{138char c;139if (calc_input (&c, 1) != 1)140return EOF;141else142return (int) c;143}144145#endif /* WITH_READLINE */146147148