Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - language - gplib.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.2 lcov report (development 29115-f22e516b23) Lines: 522 963 54.2 %
Date: 2024-04-22 08:08:03 Functions: 63 97 64.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /*******************************************************************/
      16             : /**                                                               **/
      17             : /**            LIBRARY ROUTINES FOR PARI CALCULATOR               **/
      18             : /**                                                               **/
      19             : /*******************************************************************/
      20             : #ifdef _WIN32
      21             : #  include "../systems/mingw/pwinver.h"
      22             : #  include <windows.h>
      23             : #  include "../systems/mingw/mingw.h"
      24             : #  include <process.h>
      25             : #endif
      26             : 
      27             : #include "pari.h"
      28             : #include "paripriv.h"
      29             : #ifdef __EMSCRIPTEN__
      30             : #include "../systems/emscripten/emscripten.h"
      31             : #endif
      32             : 
      33             : /********************************************************************/
      34             : /**                                                                **/
      35             : /**                            STRINGS                             **/
      36             : /**                                                                **/
      37             : /********************************************************************/
      38             : 
      39             : void
      40          28 : pari_skip_space(char **s) {
      41          28 :   char *t = *s;
      42          28 :   while (isspace((unsigned char)*t)) t++;
      43          28 :   *s = t;
      44          28 : }
      45             : void
      46           0 : pari_skip_alpha(char **s) {
      47           0 :   char *t = *s;
      48           0 :   while (isalpha((unsigned char)*t)) t++;
      49           0 :   *s = t;
      50           0 : }
      51             : 
      52             : /*******************************************************************/
      53             : /**                                                               **/
      54             : /**                          BUFFERS                              **/
      55             : /**                                                               **/
      56             : /*******************************************************************/
      57             : static Buffer **bufstack;
      58             : static pari_stack s_bufstack;
      59             : void
      60        1830 : pari_init_buffers(void)
      61        1830 : { pari_stack_init(&s_bufstack, sizeof(Buffer*), (void**)&bufstack); }
      62             : 
      63             : void
      64        1894 : pop_buffer(void)
      65             : {
      66        1894 :   if (s_bufstack.n)
      67        1894 :     delete_buffer( bufstack[ --s_bufstack.n ] );
      68        1894 : }
      69             : 
      70             : /* kill all buffers until B is met or nothing is left */
      71             : void
      72       14382 : kill_buffers_upto(Buffer *B)
      73             : {
      74       16209 :   while (s_bufstack.n) {
      75       14389 :     if (bufstack[ s_bufstack.n-1 ] == B) break;
      76        1827 :     pop_buffer();
      77             :   }
      78       14382 : }
      79             : void
      80           0 : kill_buffers_upto_including(Buffer *B)
      81             : {
      82           0 :   while (s_bufstack.n) {
      83           0 :     if (bufstack[ s_bufstack.n-1 ] == B) { pop_buffer(); break; }
      84           0 :     pop_buffer();
      85             :   }
      86           0 : }
      87             : 
      88             : static int disable_exception_handler = 0;
      89             : #define BLOCK_EH_START                \
      90             : {                                     \
      91             :   int block=disable_exception_handler;\
      92             :   disable_exception_handler = 1;
      93             : 
      94             : #define BLOCK_EH_END                \
      95             :   disable_exception_handler = block;\
      96             : }
      97             : /* numerr < 0: from SIGINT */
      98             : int
      99       12159 : gp_handle_exception(long numerr)
     100             : {
     101       12159 :   if (disable_exception_handler)
     102           0 :     disable_exception_handler = 0;
     103       12159 :   else if (GP_DATA->breakloop && cb_pari_break_loop
     104          56 :                               && cb_pari_break_loop(numerr))
     105           0 :     return 1;
     106       12152 :   return 0;
     107             : }
     108             : 
     109             : /********************************************************************/
     110             : /**                                                                **/
     111             : /**                             HELP                               **/
     112             : /**                                                                **/
     113             : /********************************************************************/
     114             : void
     115           0 : pari_hit_return(void)
     116             : {
     117             :   int c;
     118           0 :   if (GP_DATA->flags & (gpd_EMACS|gpd_TEXMACS)) return;
     119           0 :   BLOCK_EH_START
     120           0 :   pari_puts("/*-- (type RETURN to continue) --*/");
     121           0 :   pari_flush();
     122             :   /* if called from a readline callback, may be in a funny TTY mode */
     123           0 :   do c = fgetc(stdin); while (c >= 0 && c != '\n' && c != '\r');
     124           0 :   pari_putc('\n');
     125           0 :   BLOCK_EH_END
     126             : }
     127             : 
     128             : static int
     129          13 : has_ext_help(void) { return (GP_DATA->help && *GP_DATA->help); }
     130             : 
     131             : static int
     132         173 : compare_str(char **s1, char **s2) { return strcmp(*s1, *s2); }
     133             : 
     134             : /* Print all elements of list in columns, pausing every nbli lines
     135             :  * if nbli is nonzero. list is a NULL terminated list of function names */
     136             : void
     137           7 : print_fun_list(char **list, long nbli)
     138             : {
     139           7 :   long i=0, j=0, maxlen=0, nbcol,len, w = term_width();
     140             :   char **l;
     141             : 
     142          77 :   while (list[i]) i++;
     143           7 :   qsort (list, i, sizeof(char *), (QSCOMP)compare_str);
     144             : 
     145          77 :   for (l=list; *l; l++)
     146             :   {
     147          70 :     len = strlen(*l);
     148          70 :     if (len > maxlen) maxlen=len;
     149             :   }
     150           7 :   maxlen++; nbcol= w / maxlen;
     151           7 :   if (nbcol * maxlen == w) nbcol--;
     152           7 :   if (!nbcol) nbcol = 1;
     153             : 
     154           7 :   pari_putc('\n'); i=0;
     155          77 :   for (l=list; *l; l++)
     156             :   {
     157          70 :     pari_puts(*l); i++;
     158          70 :     if (i >= nbcol)
     159             :     {
     160           7 :       i=0; pari_putc('\n');
     161           7 :       if (nbli && j++ > nbli) { j = 0; pari_hit_return(); }
     162           7 :       continue;
     163             :     }
     164          63 :     len = maxlen - strlen(*l);
     165         329 :     while (len--) pari_putc(' ');
     166             :   }
     167           7 :   if (i) pari_putc('\n');
     168           7 : }
     169             : 
     170             : static const long MAX_SECTION = 17;
     171             : static void
     172           7 : commands(long n)
     173             : {
     174             :   long i;
     175             :   entree *ep;
     176             :   char **t_L;
     177             :   pari_stack s_L;
     178             : 
     179           7 :   pari_stack_init(&s_L, sizeof(*t_L), (void**)&t_L);
     180         952 :   for (i = 0; i < functions_tblsz; i++)
     181       10465 :     for (ep = functions_hash[i]; ep; ep = ep->next)
     182             :     {
     183             :       long m;
     184        9520 :       switch (EpVALENCE(ep))
     185             :       {
     186          21 :         case EpVAR:
     187          21 :           if (typ((GEN)ep->value) == t_CLOSURE) break;
     188             :           /* fall through */
     189          28 :         case EpNEW: continue;
     190             :       }
     191        9492 :       m = ep->menu;
     192        9492 :       if (m == n || (n < 0 && m && m <= MAX_SECTION))
     193          70 :         pari_stack_pushp(&s_L, (void*)ep->name);
     194             :     }
     195           7 :   pari_stack_pushp(&s_L, NULL);
     196           7 :   print_fun_list(t_L, term_height()-4);
     197           7 :   pari_stack_delete(&s_L);
     198           7 : }
     199             : 
     200             : void
     201          32 : pari_center(const char *s)
     202             : {
     203          32 :   pari_sp av = avma;
     204          32 :   long i, l = strlen(s), pad = term_width() - l;
     205             :   char *buf, *u;
     206             : 
     207          32 :   if (pad<0) pad=0; else pad >>= 1;
     208          32 :   u = buf = stack_malloc(l + pad + 2);
     209         468 :   for (i=0; i<pad; i++) *u++ = ' ';
     210        1714 :   while (*s) *u++ = *s++;
     211          32 :   *u++ = '\n'; *u = 0;
     212          32 :   pari_puts(buf); set_avma(av);
     213          32 : }
     214             : 
     215             : static void
     216           0 : community(void)
     217             : {
     218             :   const char *pari_docdir;
     219             : #if defined(_WIN32)
     220             :   /* for some reason, the documentation on windows is not in datadir */
     221             :   if (paricfg_datadir[0]=='@' && paricfg_datadir[1]==0)
     222             :     pari_docdir = win32_basedir();
     223             :   else
     224             : #endif
     225           0 :     pari_docdir = pari_datadir;
     226             : 
     227           0 :   print_text("The PARI/GP distribution includes a reference manual, a \
     228             : tutorial, a reference card and quite a few examples. They have been installed \
     229             : in the directory ");
     230           0 :   pari_puts("  ");
     231           0 :   pari_puts(pari_docdir);
     232           0 :   pari_puts("\nYou can also download them from http://pari.math.u-bordeaux.fr/.\
     233             : \n\nThree mailing lists are devoted to PARI:\n\
     234             :   - pari-announce (moderated) to announce major version changes.\n\
     235             :   - pari-dev for everything related to the development of PARI, including\n\
     236             :     suggestions, technical questions, bug reports and patch submissions.\n\
     237             :   - pari-users for everything else!\n\
     238             : To subscribe, send an empty message to\n\
     239             :   <pari_list_name>-request@pari.math.u-bordeaux.fr\n\
     240             : with a Subject: field containing the word 'subscribe'.\n\n");
     241           0 :   print_text("An archive is kept at the WWW site mentioned above. You can also \
     242           0 : reach the authors at pari@math.u-bordeaux.fr (answer not guaranteed)."); }
     243             : 
     244             : static void
     245           7 : gentypes(void)
     246             : {
     247           7 :   pari_puts("List of the PARI types:\n\
     248             :   t_INT    : long integers     [ cod1 ] [ cod2 ] [ man_1 ] ... [ man_k ]\n\
     249             :   t_REAL   : long real numbers [ cod1 ] [ cod2 ] [ man_1 ] ... [ man_k ]\n\
     250             :   t_INTMOD : integermods       [ code ] [ mod  ] [ integer ]\n\
     251             :   t_FRAC   : irred. rationals  [ code ] [ num. ] [ den. ]\n\
     252             :   t_FFELT  : finite field elt. [ code ] [ cod2 ] [ elt ] [ mod ] [ p ]\n\
     253             :   t_COMPLEX: complex numbers   [ code ] [ real ] [ imag ]\n\
     254             :   t_PADIC  : p-adic numbers    [ cod1 ] [ cod2 ] [ p ] [ p^r ] [ int ]\n\
     255             :   t_QUAD   : quadratic numbers [ cod1 ] [ mod  ] [ real ] [ imag ]\n\
     256             :   t_POLMOD : poly mod          [ code ] [ mod  ] [ polynomial ]\n\
     257             :   -------------------------------------------------------------\n\
     258             :   t_POL    : polynomials       [ cod1 ] [ cod2 ] [ man_1 ] ... [ man_k ]\n\
     259             :   t_SER    : power series      [ cod1 ] [ cod2 ] [ man_1 ] ... [ man_k ]\n\
     260             :   t_RFRAC  : irred. rat. func. [ code ] [ num. ] [ den. ]\n\
     261             :   t_QFB    : qfb               [ code ] [ a ] [ b ] [ c ] [ disc ]\n\
     262             :   t_VEC    : row vector        [ code ] [  x_1  ] ... [  x_k  ]\n\
     263             :   t_COL    : column vector     [ code ] [  x_1  ] ... [  x_k  ]\n\
     264             :   t_MAT    : matrix            [ code ] [ col_1 ] ... [ col_k ]\n\
     265             :   t_LIST   : list              [ cod1 ] [ cod2 ] [ vec ]\n\
     266             :   t_STR    : string            [ code ] [ man_1 ] ... [ man_k ]\n\
     267             :   t_VECSMALL: vec. small ints  [ code ] [ x_1 ] ... [ x_k ]\n\
     268             :   t_CLOSURE: functions         [ code ] [ arity ] [ proto ] [ operand ] ... \n\
     269             :   t_ERROR  : error context     [ code ] [ errnum ] [ dat_1 ] ... [ dat_k ]\n\
     270             :   t_INFINITY: a*infinity       [ code ] [ a ]\n\
     271             : \n");
     272           7 : }
     273             : 
     274             : static void
     275           7 : menu_commands(void)
     276             : {
     277             :   ulong i;
     278           7 :   const char *s[] = {
     279             :   "user-defined functions (aliases, installed and user functions)",
     280             :   "PROGRAMMING under GP",
     281             :   "Standard monadic or dyadic OPERATORS",
     282             :   "CONVERSIONS and similar elementary functions",
     283             :   "functions related to COMBINATORICS",
     284             :   "NUMBER THEORETICAL functions",
     285             :   "POLYNOMIALS and power series",
     286             :   "Vectors, matrices, LINEAR ALGEBRA and sets",
     287             :   "TRANSCENDENTAL functions",
     288             :   "SUMS, products, integrals and similar functions",
     289             :   "General NUMBER FIELDS",
     290             :   "Associative and central simple ALGEBRAS",
     291             :   "ELLIPTIC CURVES",
     292             :   "L-FUNCTIONS",
     293             :   "HYPERGEOMETRIC MOTIVES",
     294             :   "MODULAR FORMS",
     295             :   "MODULAR SYMBOLS",
     296             :   "GRAPHIC functions",
     297             :   "The PARI community"
     298             :   };
     299           7 :   pari_puts("Help topics: for a list of relevant subtopics, type ?n for n in\n");
     300         140 :   for (i = 0; i < numberof(s); i++) pari_printf("  %2lu: %s\n", i, s[i]);
     301           7 :   pari_puts("Also:\n\
     302             :   ? functionname (short on-line help)\n\
     303             :   ?\\             (keyboard shortcuts)\n\
     304             :   ?.             (member functions)\n");
     305           7 :   if (has_ext_help()) pari_puts("\
     306             : Extended help (if available):\n\
     307             :   ??             (opens the full user's manual in a dvi previewer)\n\
     308             :   ??  tutorial / refcard / libpari (tutorial/reference card/libpari manual)\n\
     309             :   ??  refcard-ell (or -lfun/-mf/-nf: specialized reference card)\n\
     310             :   ??  keyword    (long help text about \"keyword\" from the user's manual)\n\
     311             :   ??? keyword    (a propos: list of related functions).");
     312           7 : }
     313             : 
     314             : static void
     315           7 : slash_commands(void)
     316             : {
     317           7 :   pari_puts("#       : enable/disable timer\n\
     318             : ##      : print time for last result\n\
     319             : \\\\      : comment up to end of line\n\
     320             : \\a {n}  : print result in raw format (readable by PARI)\n\
     321             : \\B {n}  : print result in beautified format\n\
     322             : \\c      : list all commands (same effect as ?*)\n\
     323             : \\d      : print all defaults\n\
     324             : \\e {n}  : enable/disable echo (set echo=n)\n\
     325             : \\g {n}  : set debugging level\n\
     326             : \\gf{n}  : set file debugging level\n\
     327             : \\gm{n}  : set memory debugging level\n\
     328             : \\h {m-n}: hashtable information\n\
     329             : \\l {f}  : enable/disable logfile (set logfile=f)\n\
     330             : \\m {n}  : print result in prettymatrix format\n\
     331             : \\o {n}  : set output method (0=raw, 1=prettymatrix, 2=prettyprint, 3=2-dim)\n\
     332             : \\p {n}  : change real precision\n\
     333             : \\pb{n}  : change real bit precision\n\
     334             : \\ps{n}  : change series precision\n\
     335             : \\q      : quit completely this GP session\n\
     336             : \\r {f}  : read in a file\n\
     337             : \\s      : print stack information\n\
     338             : \\t      : print the list of PARI types\n\
     339             : \\u      : print the list of user-defined functions\n\
     340             : \\um     : print the list of user-defined member functions\n\
     341             : \\uv     : print the list of user-defined variables, excluding closures\n\
     342             : \\v      : print current version of GP\n\
     343             : \\w {nf} : write to a file\n\
     344             : \\x {n}  : print complete inner structure of result\n\
     345             : \\y {n}  : disable/enable automatic simplification (set simplify=n)\n\
     346             : \n\
     347             : {f}=optional filename. {n}=optional integer\n");
     348           7 : }
     349             : 
     350             : static void
     351           7 : member_commands(void)
     352             : {
     353           7 :   pari_puts("\
     354             : Member functions, followed by relevant objects\n\n\
     355             : a1-a6, b2-b8, c4-c6 : coeff. of the curve.         ell\n\
     356             : area : area                                        ell\n\
     357             : bid  : big ideal                     bid,                     bnr\n\
     358             : bnf  : big number field                                   bnf,bnr\n\
     359             : clgp : class group              quad,bid,                 bnf,bnr\n\
     360             : cyc  : cyclic decomposition     quad,bid,     clgp,ell,   bnf,bnr\n\
     361             : diff, codiff: different and codifferent                nf,bnf,bnr\n\
     362             : disc : discriminant                                ell,nf,bnf,bnr,rnf\n\
     363             : e, f : inertia/residue  degree           prid\n\
     364             : fu   : fundamental units                                  bnf\n\
     365             : gen  : generators                    bid,prid,clgp,ell,   bnf,bnr,    gal\n\
     366             : group: group                                       ell,               gal\n\
     367             : index: index                                           nf,bnf,bnr\n\
     368             : j    : j-invariant                                 ell\n");
     369             : /* split: some compilers can't handle long constant strings */
     370           7 :   pari_puts("\
     371             : mod  : modulus                       bid,                     bnr,    gal\n\
     372             : nf   : number field                                    nf,bnf,bnr,rnf\n\
     373             : no   : number of elements       quad,bid,     clgp,ell,   bnf,bnr\n\
     374             : normfu:                         quad\n\
     375             : omega, eta: [w1,w2] and [eta1, eta2]               ell\n\
     376             : orders: relative orders of generators                                 gal\n\
     377             : p    : rational prime                    prid,     ell,nf,bnf,bnr,rnf,gal\n\
     378             : pol  : defining polynomial                             nf,bnf,bnr,    gal\n\
     379             : polabs: defining polynomial over Q                                rnf\n\
     380             : reg  : regulator                quad,                     bnf\n\
     381             : roots: roots                                       ell,nf,bnf,bnr,    gal\n\
     382             : sign,r1,r2 : signature                                 nf,bnf,bnr\n\
     383             : t2   : t2 matrix                                       nf,bnf,bnr\n\
     384             : tate : Tate's [u^2, u, q, [a,b], L, Ei]            ell\n\
     385             : tu   : torsion unit and its order                         bnf\n\
     386             : zk   : integral basis                                  nf,bnf,bnr,rnf\n\
     387             : zkst : structure of (Z_K/m)*         bid,                     bnr\n");
     388           7 : }
     389             : 
     390             : #define QUOTE "_QUOTE"
     391             : #define DOUBQUOTE "_DOUBQUOTE"
     392             : #define BACKQUOTE "_BACKQUOTE"
     393             : 
     394             : static char *
     395           0 : _cat(char *s, const char *t)
     396             : {
     397           0 :   *s = 0; strcat(s,t); return s + strlen(t);
     398             : }
     399             : 
     400             : static char *
     401           0 : filter_quotes(const char *s)
     402             : {
     403           0 :   int i, l = strlen(s);
     404           0 :   int quote = 0;
     405           0 :   int backquote = 0;
     406           0 :   int doubquote = 0;
     407             :   char *str, *t;
     408             : 
     409           0 :   for (i=0; i < l; i++)
     410           0 :     switch(s[i])
     411             :     {
     412           0 :       case '\'': quote++; break;
     413           0 :       case '`' : backquote++; break;
     414           0 :       case '"' : doubquote++;
     415             :     }
     416           0 :   str = (char*)pari_malloc(l + quote * (strlen(QUOTE)-1)
     417           0 :                           + doubquote * (strlen(DOUBQUOTE)-1)
     418           0 :                           + backquote * (strlen(BACKQUOTE)-1) + 1);
     419           0 :   t = str;
     420           0 :   for (i=0; i < l; i++)
     421           0 :     switch(s[i])
     422             :     {
     423           0 :       case '\'': t = _cat(t, QUOTE); break;
     424           0 :       case '`' : t = _cat(t, BACKQUOTE); break;
     425           0 :       case '"' : t = _cat(t, DOUBQUOTE); break;
     426           0 :       default: *t++ = s[i];
     427             :     }
     428           0 :   *t = 0; return str;
     429             : }
     430             : 
     431             : static int
     432           0 : nl_read(char *s) { size_t l = strlen(s); return s[l-1] == '\n'; }
     433             : 
     434             : /* query external help program for s. num < 0 [keyword] or chapter number */
     435             : static void
     436           0 : external_help(const char *s, long num)
     437             : {
     438           0 :   long nbli = term_height()-3, li = 0;
     439             :   char buf[256], *str;
     440           0 :   const char *opt = "", *ar = "";
     441           0 :   char *t, *help = GP_DATA->help;
     442             :   pariFILE *z;
     443             :   FILE *f;
     444           0 :   if (cb_pari_long_help) { cb_pari_long_help(s, num); return; }
     445             : 
     446           0 :   if (!has_ext_help()) pari_err(e_MISC,"no external help program");
     447           0 :   t = filter_quotes(s);
     448           0 :   if (num < 0)
     449           0 :     opt = "-k";
     450           0 :   else if (t[strlen(t)-1] != '@')
     451           0 :     ar = stack_sprintf("@%d",num);
     452             : #ifdef _WIN32
     453             :   if (*help == '@')
     454             :   {
     455             :     const char *basedir = win32_basedir();
     456             :     help = stack_sprintf("%c:& cd %s & %s", *basedir, basedir, help+1);
     457             :   }
     458             : #endif
     459           0 :   str = stack_sprintf("%s -fromgp %s %c%s%s%c",
     460             :                       help, opt, SHELL_Q, t, ar, SHELL_Q);
     461           0 :   z = try_pipe(str,0); f = z->file;
     462           0 :   pari_free(t);
     463           0 :   while (fgets(buf, numberof(buf), f))
     464             :   {
     465           0 :     if (!strncmp("ugly_kludge_done",buf,16)) break;
     466           0 :     pari_puts(buf);
     467           0 :     if (nl_read(buf) && ++li > nbli) { pari_hit_return(); li = 0; }
     468             :   }
     469           0 :   pari_fclose(z);
     470             : }
     471             : 
     472             : const char **
     473           0 : gphelp_keyword_list(void)
     474             : {
     475             :   static const char *L[]={
     476             :   "operator",
     477             :   "libpari",
     478             :   "member",
     479             :   "integer",
     480             :   "real",
     481             :   "readline",
     482             :   "refcard",
     483             :   "refcard-nf",
     484             :   "refcard-ell",
     485             :   "refcard-mf",
     486             :   "refcard-lfun",
     487             :   "tutorial",
     488             :   "tutorial-mf",
     489             :   "mf",
     490             :   "nf",
     491             :   "bnf",
     492             :   "bnr",
     493             :   "ell",
     494             :   "rnf",
     495             :   "hgm",
     496             :   "HGM",
     497             :   "ideal",
     498             :   "idele",
     499             :   "CFT",
     500             :   "bid",
     501             :   "modulus",
     502             :   "prototype",
     503             :   "Lmath",
     504             :   "Ldata",
     505             :   "Linit",
     506             :   "character",
     507             :   "sums",
     508             :   "products",
     509             :   "integrals",
     510             :   "gchar",
     511             :   "grossencharacter",
     512             :   "Grossencharacter",
     513             :   NULL};
     514           0 :   return L;
     515             : }
     516             : 
     517             : static int
     518           0 : ok_external_help(char **s)
     519             : {
     520             :   const char **L;
     521             :   long n;
     522           0 :   if (!**s) return 1;
     523           0 :   if (!isalpha((unsigned char)**s)) return 3; /* operator or section number */
     524           0 :   if (!strncmp(*s,"t_",2)) { *s += 2; return 2; } /* type name */
     525             : 
     526           0 :   L = gphelp_keyword_list();
     527           0 :   for (n=0; L[n]; n++)
     528           0 :     if (!strcmp(*s,L[n])) return 3;
     529           0 :   return 0;
     530             : }
     531             : 
     532             : static void
     533         113 : cut_trailing_garbage(char *s)
     534             : {
     535             :   char c;
     536         573 :   while ( (c = *s++) )
     537             :   {
     538         474 :     if (c == '\\' && ! *s++) return; /* gobble next char, return if none. */
     539         474 :     if (!is_keyword_char(c) && c != '@') { s[-1] = 0; return; }
     540             :   }
     541             : }
     542             : 
     543             : static void
     544           7 : digit_help(char *s, long flag)
     545             : {
     546           7 :   long n = atoi(s);
     547           7 :   if (n < 0 || n > MAX_SECTION+4)
     548           0 :     pari_err(e_SYNTAX,"no such section in help: ?",s,s);
     549           7 :   if (n == MAX_SECTION+1)
     550           0 :     community();
     551           7 :   else if (flag & h_LONG)
     552           0 :     external_help(s,3);
     553             :   else
     554           7 :     commands(n);
     555           7 :   return;
     556             : }
     557             : 
     558             : long
     559           2 : pari_community(void)
     560             : {
     561           2 :   return MAX_SECTION+1;
     562             : }
     563             : 
     564             : static void
     565          39 : simple_help(const char *s1, const char *s2) { pari_printf("%s: %s\n", s1, s2); }
     566             : 
     567             : static void
     568          21 : default_help(char *s, long flag)
     569             : {
     570          21 :   if (flag & h_LONG)
     571           0 :     external_help(stack_strcat("se:def,",s),3);
     572             :   else
     573          21 :     simple_help(s,"default");
     574          21 : }
     575             : 
     576             : static void
     577         155 : help(const char *s0, int flag)
     578             : {
     579         155 :   const long long_help = flag & h_LONG;
     580             :   long n;
     581             :   entree *ep;
     582         155 :   char *s = get_sep(s0);
     583             : 
     584         229 :   if (isdigit((unsigned char)*s)) { digit_help(s,flag); return; }
     585         148 :   if (flag & h_APROPOS) { external_help(s,-1); return; }
     586             :   /* Get meaningful answer on '\ps 5' (e.g. from <F1>) */
     587         148 :   if (*s == '\\' && isalpha((unsigned char)*(s+1)))
     588           0 :   { char *t = s+1; pari_skip_alpha(&t); *t = '\0'; }
     589         148 :   if (isalpha((unsigned char)*s))
     590             :   {
     591         113 :     char *t = s;
     592         113 :     if (!strncmp(s, "default", 7))
     593             :     { /* special-case ?default(dft_name), e.g. default(log) */
     594          14 :       t += 7; pari_skip_space(&t);
     595          14 :       if (*t == '(')
     596             :       {
     597          14 :         t++; pari_skip_space(&t);
     598          14 :         cut_trailing_garbage(t);
     599          14 :         if (pari_is_default(t)) { default_help(t,flag); return; }
     600             :       }
     601             :     }
     602          99 :     if (!strncmp(s, "refcard-", 8)) t += 8;
     603          99 :     else if (!strncmp(s, "tutorial-", 9)) t += 9;
     604          99 :     if (strncmp(s, "se:", 3)) cut_trailing_garbage(t);
     605             :   }
     606             : 
     607         134 :   if (long_help && (n = ok_external_help(&s))) { external_help(s,n); return; }
     608         134 :   switch (*s)
     609             :   {
     610           0 :     case '*' : commands(-1); return;
     611           7 :     case '\0': menu_commands(); return;
     612           7 :     case '\\': slash_commands(); return;
     613           7 :     case '.' : member_commands(); return;
     614             :   }
     615         113 :   ep = is_entry(s);
     616         113 :   if (!ep)
     617             :   {
     618          14 :     if (pari_is_default(s))
     619           7 :       default_help(s,flag);
     620           7 :     else if (long_help)
     621           0 :       external_help(s,3);
     622           7 :     else if (!cb_pari_whatnow || !cb_pari_whatnow(pariOut, s,1))
     623           7 :       simple_help(s,"unknown identifier");
     624          14 :     return;
     625             :   }
     626             : 
     627          99 :   if (EpVALENCE(ep) == EpALIAS)
     628             :   {
     629          14 :     pari_printf("%s is aliased to:\n\n",s);
     630          14 :     ep = do_alias(ep);
     631             :   }
     632          99 :   switch(EpVALENCE(ep))
     633             :   {
     634          35 :     case EpVAR:
     635          35 :       if (!ep->help)
     636             :       {
     637          21 :         if (typ((GEN)ep->value)!=t_CLOSURE)
     638           7 :           simple_help(s, "user defined variable");
     639             :         else
     640             :         {
     641          14 :           GEN str = closure_get_text((GEN)ep->value);
     642          14 :           if (typ(str) == t_VEC)
     643          14 :             pari_printf("%s =\n  %Ps\n", ep->name, ep->value);
     644             :         }
     645          21 :         return;
     646             :       }
     647          14 :       break;
     648             : 
     649           4 :     case EpINSTALL:
     650           4 :       if (!ep->help) { simple_help(s, "installed function"); return; }
     651           4 :       break;
     652             : 
     653          18 :     case EpNEW:
     654          18 :       if (!ep->help) { simple_help(s, "new identifier"); return; };
     655          14 :       break;
     656             : 
     657          42 :     default: /* built-in function */
     658          42 :       if (!ep->help) pari_err_BUG("gp_help (no help found)"); /*paranoia*/
     659          42 :       if (long_help) { external_help(ep->name,3); return; }
     660             :   }
     661          74 :   print_text(ep->help);
     662             : }
     663             : 
     664             : void
     665         155 : gp_help(const char *s, long flag)
     666             : {
     667         155 :   pari_sp av = avma;
     668         155 :   if ((flag & h_RL) == 0)
     669             :   {
     670         155 :     if (*s == '?') { flag |= h_LONG; s++; }
     671         155 :     if (*s == '?') { flag |= h_APROPOS; s++; }
     672             :   }
     673         155 :   term_color(c_HELP); help(s,flag); term_color(c_NONE);
     674         155 :   if ((flag & h_RL) == 0) pari_putc('\n');
     675         155 :   set_avma(av);
     676         155 : }
     677             : 
     678             : /********************************************************************/
     679             : /**                                                                **/
     680             : /**                         GP HEADER                              **/
     681             : /**                                                                **/
     682             : /********************************************************************/
     683             : static char *
     684           6 : what_readline(void)
     685             : {
     686             : #ifdef READLINE
     687           6 :   const char *v = READLINE;
     688           6 :   char *s = stack_malloc(3 + strlen(v) + 8);
     689           6 :   (void)sprintf(s, "v%s %s", v, GP_DATA->use_readline? "enabled": "disabled");
     690           6 :   return s;
     691             : #else
     692             :   return (char*)"not compiled in";
     693             : #endif
     694             : }
     695             : 
     696             : static char *
     697           6 : what_cc(void)
     698             : {
     699             :   char *s;
     700             : #ifdef GCC_VERSION
     701             : #  ifdef __cplusplus
     702             :   s = stack_malloc(6 + strlen(GCC_VERSION) + 1);
     703             :   (void)sprintf(s, "(C++) %s", GCC_VERSION);
     704             : #  else
     705           6 :   s = stack_strdup(GCC_VERSION);
     706             : #  endif
     707             : #else
     708             : #  ifdef _MSC_VER
     709             :   s = stack_malloc(32);
     710             :   (void)sprintf(s, "MSVC-%i", _MSC_VER);
     711             : #  else
     712             :   s = NULL;
     713             : #  endif
     714             : #endif
     715           6 :   return s;
     716             : }
     717             : 
     718             : static char *
     719          13 : convert_time(char *s, long delay)
     720             : {
     721          13 :   if (delay >= 3600000)
     722             :   {
     723           7 :     sprintf(s, "%ldh, ", delay / 3600000); s+=strlen(s);
     724           7 :     delay %= 3600000;
     725             :   }
     726          13 :   if (delay >= 60000)
     727             :   {
     728           7 :     sprintf(s, "%ldmin, ", delay / 60000); s+=strlen(s);
     729           7 :     delay %= 60000;
     730             :   }
     731          13 :   if (delay >= 1000)
     732             :   {
     733          13 :     sprintf(s, "%ld,", delay / 1000); s+=strlen(s);
     734          13 :     delay %= 1000;
     735          13 :     if (delay < 100)
     736             :     {
     737           4 :       sprintf(s, "%s", (delay<10)? "00": "0");
     738           4 :       s+=strlen(s);
     739             :     }
     740             :   }
     741          13 :   sprintf(s, "%ld ms", delay); s+=strlen(s);
     742          13 :   return s;
     743             : }
     744             : 
     745             : /* Format a time of 'delay' ms */
     746             : const char *
     747           0 : gp_format_time(long delay)
     748             : {
     749           0 :   char *buf = stack_malloc(64), *s = buf;
     750           0 :   term_get_color(s, c_TIME);
     751           0 :   s = convert_time(s + strlen(s), delay);
     752           0 :   term_get_color(s, c_NONE); return buf;
     753             : }
     754             : 
     755             : GEN
     756           7 : strtime(long delay)
     757             : {
     758           7 :   long n = nchar2nlong(64);
     759           7 :   GEN x = cgetg(n+1, t_STR);
     760           7 :   char *buf = GSTR(x), *t = buf + 64, *s = convert_time(buf, delay);
     761         308 :   s++; while (s < t) *s++ = 0; /* pacify valgrind */
     762           7 :   return x;
     763             : }
     764             : 
     765             : /********************************************************************/
     766             : /*                                                                  */
     767             : /*                              GPRC                                */
     768             : /*                                                                  */
     769             : /********************************************************************/
     770             : /* LOCATE GPRC */
     771             : static void
     772           0 : err_gprc(const char *s, char *t, char *u)
     773             : {
     774           0 :   err_printf("\n");
     775           0 :   pari_err(e_SYNTAX,s,t,u);
     776           0 : }
     777             : 
     778             : /* return $HOME or the closest we can find */
     779             : static const char *
     780           4 : get_home(int *free_it)
     781             : {
     782           4 :   char *drv, *pth = os_getenv("HOME");
     783           4 :   if (pth) return pth;
     784           0 :   if ((drv = os_getenv("HOMEDRIVE"))
     785           0 :    && (pth = os_getenv("HOMEPATH")))
     786             :   { /* looks like WinNT */
     787           0 :     char *buf = (char*)pari_malloc(strlen(pth) + strlen(drv) + 1);
     788           0 :     sprintf(buf, "%s%s",drv,pth);
     789           0 :     *free_it = 1; return buf;
     790             :   }
     791           0 :   pth = pari_get_homedir("");
     792           0 :   return pth? pth: ".";
     793             : }
     794             : 
     795             : static FILE *
     796          12 : gprc_chk(const char *s)
     797             : {
     798          12 :   FILE *f = fopen(s, "r");
     799          12 :   if (f && !(GP_DATA->flags & gpd_QUIET)) err_printf("Reading GPRC: %s\n", s);
     800          12 :   return f;
     801             : }
     802             : 
     803             : /* Look for [._]gprc: $GPRC, then in $HOME, ., /etc, pari_datadir */
     804             : static FILE *
     805           4 : gprc_get(void)
     806             : {
     807           4 :   FILE *f = NULL;
     808           4 :   const char *gprc = os_getenv("GPRC");
     809           4 :   if (gprc) f = gprc_chk(gprc);
     810           4 :   if (!f)
     811             :   {
     812           4 :     int free_it = 0;
     813           4 :     const char *home = get_home(&free_it);
     814             :     char *str, *s, c;
     815             :     long l;
     816           4 :     l = strlen(home); c = home[l-1];
     817             :     /* + "/gprc.txt" + \0*/
     818           4 :     str = strcpy((char*)pari_malloc(l+10), home);
     819           4 :     if (free_it) pari_free((void*)home);
     820           4 :     s = str + l;
     821           4 :     if (c != '/' && c != '\\') *s++ = '/';
     822             : #ifndef _WIN32
     823           4 :     strcpy(s, ".gprc");
     824             : #else
     825             :     strcpy(s, "gprc.txt");
     826             : #endif
     827           4 :     f = gprc_chk(str); /* in $HOME */
     828           4 :     if (!f) f = gprc_chk(s); /* in . */
     829             : #ifndef _WIN32
     830           4 :     if (!f) f = gprc_chk("/etc/gprc");
     831             : #else
     832             :     if (!f)  /* in basedir */
     833             :     {
     834             :       const char *basedir = win32_basedir();
     835             :       char *t = (char *) pari_malloc(strlen(basedir)+strlen(s)+2);
     836             :       sprintf(t, "%s/%s", basedir, s);
     837             :       f = gprc_chk(t); free(t);
     838             :     }
     839             : #endif
     840           4 :     pari_free(str);
     841             :   }
     842           4 :   return f;
     843             : }
     844             : 
     845             : /* PREPROCESSOR */
     846             : 
     847             : static ulong
     848           0 : read_uint(char **s)
     849             : {
     850           0 :   long v = atol(*s);
     851           0 :   if (!isdigit((unsigned char)**s)) err_gprc("not an integer", *s, *s);
     852           0 :   while (isdigit((unsigned char)**s)) (*s)++;
     853           0 :   return v;
     854             : }
     855             : static ulong
     856           0 : read_dot_uint(char **s)
     857             : {
     858           0 :   if (**s != '.') return 0;
     859           0 :   (*s)++; return read_uint(s);
     860             : }
     861             : /* read a.b.c */
     862             : static long
     863           0 : read_version(char **s)
     864             : {
     865             :   long a, b, c;
     866           0 :   a = read_uint(s);
     867           0 :   b = read_dot_uint(s);
     868           0 :   c = read_dot_uint(s);
     869           0 :   return PARI_VERSION(a,b,c);
     870             : }
     871             : 
     872             : static int
     873           4 : get_preproc_value(char **s)
     874             : {
     875           4 :   if (!strncmp(*s,"EMACS",5)) {
     876           4 :     *s += 5;
     877           4 :     return GP_DATA->flags & (gpd_EMACS|gpd_TEXMACS);
     878             :   }
     879           0 :   if (!strncmp(*s,"READL",5)) {
     880           0 :     *s += 5;
     881           0 :     return GP_DATA->use_readline;
     882             :   }
     883           0 :   if (!strncmp(*s,"VERSION",7)) {
     884           0 :     int less = 0, orequal = 0;
     885             :     long d;
     886           0 :     *s += 7;
     887           0 :     switch(**s)
     888             :     {
     889           0 :       case '<': (*s)++; less = 1; break;
     890           0 :       case '>': (*s)++; less = 0; break;
     891           0 :       default: return -1;
     892             :     }
     893           0 :     if (**s == '=') { (*s)++; orequal = 1; }
     894           0 :     d = paricfg_version_code - read_version(s);
     895           0 :     if (!d) return orequal;
     896           0 :     return less? (d < 0): (d > 0);
     897             :   }
     898           0 :   if (!strncmp(*s,"BITS_IN_LONG",12)) {
     899           0 :     *s += 12;
     900           0 :     if ((*s)[0] == '=' && (*s)[1] == '=')
     901             :     {
     902           0 :       *s += 2;
     903           0 :       return BITS_IN_LONG == read_uint(s);
     904             :     }
     905             :   }
     906           0 :   return -1;
     907             : }
     908             : 
     909             : /* PARSE GPRC */
     910             : 
     911             : /* 1) replace next separator by '\0' (t must be writable)
     912             :  * 2) return the next expression ("" if none)
     913             :  * see get_sep() */
     914             : static char *
     915          12 : next_expr(char *t)
     916             : {
     917          12 :   int outer = 1;
     918          12 :   char *s = t;
     919             : 
     920             :   for(;;)
     921         184 :   {
     922             :     char c;
     923         196 :     switch ((c = *s++))
     924             :     {
     925           8 :       case '"':
     926           8 :         if (outer || (s >= t+2 && s[-2] != '\\')) outer = !outer;
     927           8 :         break;
     928          12 :       case '\0':
     929          12 :         return (char*)"";
     930         176 :       default:
     931         176 :         if (outer && c == ';') { s[-1] = 0; return s; }
     932             :     }
     933             :   }
     934             : }
     935             : 
     936             : Buffer *
     937        1894 : filtered_buffer(filtre_t *F)
     938             : {
     939        1894 :   Buffer *b = new_buffer();
     940        1894 :   init_filtre(F, b);
     941        1894 :   pari_stack_pushp(&s_bufstack, (void*)b);
     942        1894 :   return b;
     943             : }
     944             : 
     945             : /* parse src of the form s=t (or s="t"), set *ps to s, and *pt to t.
     946             :  * modifies src (replaces = by \0) */
     947             : void
     948          18 : parse_key_val(char *src, char **ps, char **pt)
     949             : {
     950             :   char *s_end, *t;
     951         130 :   t = src; while (*t && *t != '=') t++;
     952          18 :   if (*t != '=') err_gprc("missing '='",t,src);
     953          18 :   s_end = t;
     954          18 :   t++;
     955          18 :   if (*t == '"') (void)pari_translate_string(t, t, src);
     956          18 :   *s_end = 0; *ps = src; *pt = t;
     957          18 : }
     958             : /* parse src of the form (s,t) (or "s", or "t"), set *ps to s, and *pt to t. */
     959             : static void
     960           0 : parse_key_val_paren(char *src, char **ps, char **pt)
     961             : {
     962             :   char *s, *t, *s_end, *t_end;
     963           0 :   s = t = src + 1; while (*t && *t != ',') t++;
     964           0 :   if (*t != ',') err_gprc("missing ','",t,src);
     965           0 :   s_end = t;
     966           0 :   t++; while (*t && *t != ')') t++;
     967           0 :   if (*t != ')') err_gprc("missing ')'",t,src);
     968           0 :   if (t[1])  err_gprc("unexpected character",t+1,src);
     969           0 :   t_end = t; t = s_end + 1;
     970           0 :   if (*t == '"') (void)pari_translate_string(t, t, src);
     971           0 :   if (*s == '"') (void)pari_translate_string(s, s, src);
     972           0 :   *s_end = 0; *t_end = 0; *ps = s; *pt = t;
     973           0 : }
     974             : 
     975             : void
     976           4 : gp_initrc(pari_stack *p_A)
     977             : {
     978           4 :   FILE *file = gprc_get();
     979             :   Buffer *b;
     980             :   filtre_t F;
     981           4 :   VOLATILE long c = 0;
     982             :   jmp_buf *env;
     983             :   pari_stack s_env;
     984             : 
     985           4 :   if (!file) return;
     986           4 :   b = filtered_buffer(&F);
     987           4 :   pari_stack_init(&s_env, sizeof(*env), (void**)&env);
     988           4 :   (void)pari_stack_new(&s_env);
     989             :   for(;;)
     990         172 :   {
     991             :     char *nexts, *s, *t;
     992         176 :     if (setjmp(env[s_env.n-1])) err_printf("...skipping line %ld.\n", c);
     993         176 :     c++;
     994         176 :     if (!get_line_from_file(NULL,&F,file)) break;
     995         172 :     s = b->buf;
     996         172 :     if (*s == '#')
     997             :     { /* preprocessor directive */
     998           4 :       int z, NOT = 0;
     999           4 :       s++;
    1000           4 :       if (strncmp(s,"if",2)) err_gprc("unknown directive",s,b->buf);
    1001           4 :       s += 2;
    1002           4 :       if (!strncmp(s,"not",3)) { NOT = !NOT; s += 3; }
    1003           4 :       if (*s == '!')           { NOT = !NOT; s++; }
    1004           4 :       t = s;
    1005           4 :       z = get_preproc_value(&s);
    1006           4 :       if (z < 0) err_gprc("unknown preprocessor variable",t,b->buf);
    1007           4 :       if (NOT) z = !z;
    1008           4 :       if (!*s)
    1009             :       { /* make sure at least an expr follows the directive */
    1010           0 :         if (!get_line_from_file(NULL,&F,file)) break;
    1011           0 :         s = b->buf;
    1012             :       }
    1013           4 :       if (!z) continue; /* dump current line */
    1014             :     }
    1015             :     /* parse line */
    1016         184 :     for ( ; *s; s = nexts)
    1017             :     {
    1018          12 :       nexts = next_expr(s);
    1019          12 :       if (!strncmp(s,"read",4) && (s[4] == ' ' || s[4] == '\t' || s[4] == '"'))
    1020             :       { /* read file */
    1021           0 :         s += 4;
    1022           0 :         t = (char*)pari_malloc(strlen(s) + 1);
    1023           0 :         if (*s == '"') (void)pari_translate_string(s, t, s-4); else strcpy(t,s);
    1024           0 :         pari_stack_pushp(p_A,t);
    1025             :       }
    1026          12 :       else if (!strncmp(s, "default(", 8))
    1027             :       {
    1028           0 :         s += 7; parse_key_val_paren(s, &s,&t);
    1029           0 :         (void)setdefault(s,t,d_INITRC);
    1030             :       }
    1031          12 :       else if (!strncmp(s, "setdebug(", 9))
    1032             :       {
    1033           0 :         s += 8; parse_key_val_paren(s, &s,&t);
    1034           0 :         setdebug(s, atol(t));
    1035             :       }
    1036             :       else
    1037             :       { /* set default */
    1038          12 :         parse_key_val(s, &s,&t);
    1039          12 :         (void)setdefault(s,t,d_INITRC);
    1040             :       }
    1041             :     }
    1042             :   }
    1043           4 :   pari_stack_delete(&s_env);
    1044           4 :   pop_buffer();
    1045           4 :   if (!(GP_DATA->flags & gpd_QUIET)) err_printf("GPRC Done.\n\n");
    1046           4 :   fclose(file);
    1047             : }
    1048             : 
    1049             : void
    1050           0 : gp_load_gprc(void)
    1051             : {
    1052             :   pari_stack sA;
    1053             :   char **A;
    1054             :   long i;
    1055           0 :   pari_stack_init(&sA,sizeof(*A),(void**)&A);
    1056           0 :   gp_initrc(&sA);
    1057           0 :   for (i = 0; i < sA.n; pari_free(A[i]),i++)
    1058             :   {
    1059           0 :     pari_CATCH(CATCH_ALL) { err_printf("... skipping file '%s'\n", A[i]); }
    1060           0 :     pari_TRY { gp_read_file(A[i]); } pari_ENDCATCH;
    1061             :   }
    1062           0 :   pari_stack_delete(&sA);
    1063           0 : }
    1064             : 
    1065             : /********************************************************************/
    1066             : /*                                                                  */
    1067             : /*                             PROMPTS                              */
    1068             : /*                                                                  */
    1069             : /********************************************************************/
    1070             : /* if prompt is coloured, tell readline to ignore the ANSI escape sequences */
    1071             : /* s must be able to store 14 chars (including final \0) */
    1072             : #ifdef READLINE
    1073             : static void
    1074           0 : readline_prompt_color(char *s, int c)
    1075             : {
    1076             : #ifdef _WIN32
    1077             :   (void)s; (void)c;
    1078             : #else
    1079           0 :   *s++ = '\001'; /*RL_PROMPT_START_IGNORE*/
    1080           0 :   term_get_color(s, c);
    1081           0 :   s += strlen(s);
    1082           0 :   *s++ = '\002'; /*RL_PROMPT_END_IGNORE*/
    1083           0 :   *s = 0;
    1084             : #endif
    1085           0 : }
    1086             : #endif
    1087             : /* s must be able to store 14 chars (including final \0) */
    1088             : static void
    1089           0 : brace_color(char *s, int c, int force)
    1090             : {
    1091           0 :   if (disable_color || (gp_colors[c] == c_NONE && !force)) return;
    1092             : #ifdef READLINE
    1093           0 :   if (GP_DATA->use_readline)
    1094           0 :     readline_prompt_color(s, c);
    1095             :   else
    1096             : #endif
    1097           0 :     term_get_color(s, c);
    1098             : }
    1099             : 
    1100             : /* strlen(prompt) + 28 chars */
    1101             : static const char *
    1102           0 : color_prompt(const char *prompt)
    1103             : {
    1104           0 :   long n = strlen(prompt);
    1105           0 :   char *t = stack_malloc(n + 28), *s = t;
    1106           0 :   *s = 0;
    1107             :   /* escape sequences bug readline, so use special bracing (if available) */
    1108           0 :   brace_color(s, c_PROMPT, 0);
    1109           0 :   s += strlen(s); memcpy(s, prompt, n);
    1110           0 :   s += n; *s = 0;
    1111           0 :   brace_color(s, c_INPUT, 1);
    1112           0 :   return t;
    1113             : }
    1114             : 
    1115             : const char *
    1116        7559 : gp_format_prompt(const char *prompt)
    1117             : {
    1118        7559 :   if (GP_DATA->flags & gpd_TEST)
    1119        7559 :     return prompt;
    1120             :   else
    1121             :   {
    1122             :     char b[256]; /* longer is truncated */
    1123           0 :     strftime_expand(prompt, b, sizeof(b));
    1124           0 :     return color_prompt(b);
    1125             :   }
    1126             : }
    1127             : 
    1128             : /********************************************************************/
    1129             : /*                                                                  */
    1130             : /*                           GP MAIN LOOP                           */
    1131             : /*                                                                  */
    1132             : /********************************************************************/
    1133             : static int
    1134      247382 : is_interactive(void)
    1135      247382 : { return cb_pari_is_interactive? cb_pari_is_interactive(): 0; }
    1136             : 
    1137             : static char *
    1138           0 : strip_prompt(const char *s)
    1139             : {
    1140           0 :   long l = strlen(s);
    1141           0 :   char *t, *t0 = stack_malloc(l+1);
    1142           0 :   t = t0;
    1143           0 :   for (; *s; s++)
    1144             :   {
    1145             :     /* RL_PROMPT_START_IGNORE / RL_PROMPT_END_IGNORE */
    1146           0 :     if (*s == 1 || *s == 2) continue;
    1147           0 :     if (*s == '\x1b') /* skip ANSI color escape sequence */
    1148             :     {
    1149           0 :       while (*++s != 'm')
    1150           0 :         if (!*s) goto end;
    1151           0 :       continue;
    1152             :     }
    1153           0 :     *t = *s; t++;
    1154             :   }
    1155           0 : end:
    1156           0 :   *t = 0; return t0;
    1157             : }
    1158             : static void
    1159        6773 : update_logfile(const char *prompt, const char *s)
    1160             : {
    1161             :   pari_sp av;
    1162             :   const char *p;
    1163        6773 :   if (!pari_logfile) return;
    1164           0 :   av = avma;
    1165           0 :   p = strip_prompt(prompt); /* raw prompt */
    1166             : 
    1167           0 :   switch (pari_logstyle) {
    1168           0 :     case logstyle_TeX:
    1169           0 :       fprintf(pari_logfile,
    1170             :               "\\PARIpromptSTART|%s\\PARIpromptEND|%s\\PARIinputEND|%%\n",
    1171             :               p, s);
    1172           0 :     break;
    1173           0 :     case logstyle_plain:
    1174           0 :       fprintf(pari_logfile,"%s%s\n",p, s);
    1175           0 :     break;
    1176           0 :     case logstyle_color:
    1177           0 :       fprintf(pari_logfile,"%s%s%s%s%s\n",term_get_color(NULL,c_PROMPT), p,
    1178             :                                           term_get_color(NULL,c_INPUT), s,
    1179             :                                           term_get_color(NULL,c_NONE));
    1180           0 :       break;
    1181             :   }
    1182           0 :   set_avma(av);
    1183             : }
    1184             : 
    1185             : void
    1186      112929 : gp_echo_and_log(const char *prompt, const char *s)
    1187             : {
    1188      112929 :   if (!is_interactive())
    1189             :   {
    1190      112929 :     if (!GP_DATA->echo) return;
    1191             :     /* not pari_puts(): would duplicate in logfile */
    1192        6773 :     fputs(prompt, pari_outfile);
    1193        6773 :     fputs(s,      pari_outfile);
    1194        6773 :     fputc('\n',   pari_outfile);
    1195        6773 :     pari_set_last_newline(1);
    1196             :   }
    1197        6773 :   update_logfile(prompt, s);
    1198        6773 :   pari_flush();
    1199             : }
    1200             : 
    1201             : /* prompt = NULL --> from gprc. Return 1 if new input, and 0 if EOF */
    1202             : int
    1203      134636 : get_line_from_file(const char *prompt, filtre_t *F, FILE *file)
    1204             : {
    1205             :   char *s;
    1206             :   input_method IM;
    1207             : 
    1208      134636 :   IM.file = (void*)file;
    1209      134636 :   if (file==stdin && cb_pari_fgets_interactive)
    1210           0 :     IM.myfgets = (fgets_t)cb_pari_fgets_interactive;
    1211             :   else
    1212      134636 :     IM.myfgets = (fgets_t)&fgets;
    1213      134636 :   IM.getline = &file_input;
    1214      134636 :   IM.free = 0;
    1215      134636 :   if (! input_loop(F,&IM))
    1216             :   {
    1217        1824 :     if (file==stdin && cb_pari_start_output) cb_pari_start_output();
    1218        1824 :     return 0;
    1219             :   }
    1220      132812 :   s = F->buf->buf;
    1221             :   /* don't log if from gprc or empty input */
    1222      132812 :   if (*s && prompt && GP_DATA->echo != 2) gp_echo_and_log(prompt, s);
    1223      132812 :   return 1;
    1224             : }
    1225             : 
    1226             : /* return 0 if no line could be read (EOF). If PROMPT = NULL, expand and
    1227             :  * color default prompt; otherwise, use PROMPT as-is. */
    1228             : int
    1229      134453 : gp_read_line(filtre_t *F, const char *PROMPT)
    1230             : {
    1231             :   static const char *DFT_PROMPT = "? ";
    1232      134453 :   Buffer *b = (Buffer*)F->buf;
    1233             :   const char *p;
    1234             :   int res, interactive;
    1235      134453 :   if (b->len > 100000) fix_buffer(b, 100000);
    1236      134453 :   interactive = is_interactive();
    1237      134453 :   if (interactive || pari_logfile || GP_DATA->echo)
    1238             :   {
    1239        7678 :     p = PROMPT;
    1240        7678 :     if (!p) {
    1241        7496 :       p = F->in_comment? GP_DATA->prompt_comment: GP_DATA->prompt;
    1242        7496 :       p = gp_format_prompt(p);
    1243             :     }
    1244             :   }
    1245             :   else
    1246      126775 :     p = DFT_PROMPT;
    1247             : 
    1248      134453 :   if (interactive)
    1249             :   {
    1250           0 :     BLOCK_EH_START
    1251           0 :     if (!pari_last_was_newline()) pari_putc('\n');
    1252           0 :     if (cb_pari_get_line_interactive)
    1253           0 :       res = cb_pari_get_line_interactive(p, GP_DATA->prompt_cont, F);
    1254             :     else {
    1255           0 :       pari_puts(p); pari_flush();
    1256           0 :       res = get_line_from_file(p, F, pari_infile);
    1257             :     }
    1258           0 :     BLOCK_EH_END
    1259             :   }
    1260             :   else
    1261             :   { /* in case UI fakes noninteractivity, e.g. TeXmacs */
    1262      134453 :     if (cb_pari_start_output && cb_pari_get_line_interactive)
    1263           0 :       res = cb_pari_get_line_interactive(p, GP_DATA->prompt_cont, F);
    1264             :     else
    1265      134453 :       res = get_line_from_file(p, F, pari_infile);
    1266             :   }
    1267             : 
    1268      134453 :   if (!disable_color && p != DFT_PROMPT &&
    1269           0 :       (gp_colors[c_PROMPT] != c_NONE || gp_colors[c_INPUT] != c_NONE))
    1270             :   {
    1271           0 :     term_color(c_NONE); pari_flush();
    1272             :   }
    1273      134453 :   return res;
    1274             : }
    1275             : 
    1276             : /********************************************************************/
    1277             : /*                                                                  */
    1278             : /*                      EXCEPTION HANDLER                           */
    1279             : /*                                                                  */
    1280             : /********************************************************************/
    1281             : static THREAD pari_timer ti_alarm;
    1282             : 
    1283             : #if defined(_WIN32) || defined(SIGALRM)
    1284             : static void
    1285           6 : gp_alarm_fun(void) {
    1286             :   char buf[64];
    1287           6 :   if (cb_pari_start_output) cb_pari_start_output();
    1288           6 :   convert_time(buf, timer_get(&ti_alarm));
    1289           6 :   pari_err(e_ALARM, buf);
    1290           0 : }
    1291             : #endif /* SIGALRM */
    1292             : 
    1293             : void
    1294           0 : gp_sigint_fun(void) {
    1295             :   char buf[150];
    1296             : #if defined(_WIN32)
    1297             :   if (win32alrm) { win32alrm = 0; gp_alarm_fun(); return;}
    1298             : #endif
    1299           0 :   if (cb_pari_start_output) cb_pari_start_output();
    1300           0 :   convert_time(buf, timer_get(GP_DATA->T));
    1301           0 :   if (pari_mt_nbthreads > 1)
    1302             :   {
    1303           0 :     sprintf(buf + strlen(buf), " cpu time, ");
    1304           0 :     convert_time(buf + strlen(buf), walltimer_get(GP_DATA->Tw));
    1305           0 :     sprintf(buf + strlen(buf), " real time");
    1306             :   }
    1307           0 :   pari_sigint(buf);
    1308           0 : }
    1309             : 
    1310             : #ifdef SIGALRM
    1311             : void
    1312           8 : gp_alarm_handler(int sig)
    1313             : {
    1314             : #ifndef HAS_SIGACTION
    1315             :   /*SYSV reset the signal handler in the handler*/
    1316             :   (void)os_signal(sig,gp_alarm_handler);
    1317             : #endif
    1318           8 :   if (PARI_SIGINT_block) PARI_SIGINT_pending=sig;
    1319           6 :   else gp_alarm_fun();
    1320           2 :   return;
    1321             : }
    1322             : #endif /* SIGALRM */
    1323             : 
    1324             : /********************************************************************/
    1325             : /*                                                                  */
    1326             : /*                      GP-SPECIFIC ROUTINES                        */
    1327             : /*                                                                  */
    1328             : /********************************************************************/
    1329             : void
    1330          84 : gp_allocatemem(GEN z)
    1331             : {
    1332             :   ulong newsize;
    1333          84 :   if (!z) newsize = 0;
    1334             :   else {
    1335          84 :     if (typ(z) != t_INT) pari_err_TYPE("allocatemem",z);
    1336          84 :     newsize = itou(z);
    1337          84 :     if (signe(z) < 0) pari_err_DOMAIN("allocatemem","size","<",gen_0,z);
    1338             :   }
    1339          84 :   if (pari_mainstack->vsize)
    1340           0 :     paristack_resize(newsize);
    1341             :   else
    1342          84 :     paristack_newrsize(newsize);
    1343           0 : }
    1344             : 
    1345             : GEN
    1346           7 : gp_input(void)
    1347             : {
    1348             :   filtre_t F;
    1349           7 :   Buffer *b = filtered_buffer(&F);
    1350             :   GEN x;
    1351             : 
    1352           7 :   while (! get_line_from_file("",&F,pari_infile))
    1353           0 :     if (popinfile()) { err_printf("no input ???"); cb_pari_quit(1); }
    1354           7 :   x = readseq(b->buf);
    1355           7 :   pop_buffer(); return x;
    1356             : }
    1357             : 
    1358             : static GEN
    1359         121 : closure_alarmer(GEN C, long s)
    1360             : {
    1361             :   struct pari_evalstate state;
    1362             :   VOLATILE GEN x;
    1363         121 :   if (!s) { pari_alarm(0); return closure_evalgen(C); }
    1364         121 :   evalstate_save(&state);
    1365             : #if !defined(HAS_ALARM) && !defined(_WIN32)
    1366             :   pari_err(e_ARCH,"alarm");
    1367             : #endif
    1368         121 :   pari_CATCH(CATCH_ALL) /* We need to stop the timer after any error */
    1369             :   {
    1370           6 :     GEN E = pari_err_last();
    1371           6 :     if (err_get_num(E) != e_ALARM) { pari_alarm(0); pari_err(0, E); }
    1372           6 :     x = evalstate_restore_err(&state);
    1373             :   }
    1374         121 :   pari_TRY { pari_alarm(s); x = closure_evalgen(C); pari_alarm(0); } pari_ENDCATCH;
    1375         121 :   return x;
    1376             : }
    1377             : 
    1378             : void
    1379      112379 : pari_alarm(long s)
    1380             : {
    1381      112379 :   if (s < 0) pari_err_DOMAIN("alarm","delay","<",gen_0,stoi(s));
    1382      112379 :   if (s) timer_start(&ti_alarm);
    1383             : #ifdef _WIN32
    1384             :   win32_alarm(s);
    1385             : #elif defined(HAS_ALARM)
    1386      112379 :   alarm(s);
    1387             : #else
    1388             :   if (s) pari_err(e_ARCH,"alarm");
    1389             : #endif
    1390      112379 : }
    1391             : 
    1392             : GEN
    1393         121 : gp_alarm(long s, GEN code)
    1394             : {
    1395         121 :   if (!code) { pari_alarm(s); return gnil; }
    1396         121 :   return closure_alarmer(code,s);
    1397             : }
    1398             : 
    1399             : /*******************************************************************/
    1400             : /**                                                               **/
    1401             : /**                    EXTERNAL PRETTYPRINTER                     **/
    1402             : /**                                                               **/
    1403             : /*******************************************************************/
    1404             : /* Wait for prettinprinter to finish, to prevent new prompt from overwriting
    1405             :  * the output.  Fill the output buffer, wait until it is read.
    1406             :  * Better than sleep(2): give possibility to print */
    1407             : static void
    1408           0 : prettyp_wait(FILE *out)
    1409             : {
    1410           0 :   const char *s = "                                                     \n";
    1411           0 :   long i = 2000;
    1412             : 
    1413           0 :   fputs("\n\n", out); fflush(out); /* start translation */
    1414           0 :   while (--i) fputs(s, out);
    1415           0 :   fputs("\n", out); fflush(out);
    1416           0 : }
    1417             : 
    1418             : /* initialise external prettyprinter (tex2mail) */
    1419             : static int
    1420           0 : prettyp_init(void)
    1421             : {
    1422           0 :   gp_pp *pp = GP_DATA->pp;
    1423           0 :   if (!pp->cmd) return 0;
    1424           0 :   if (pp->file || (pp->file = try_pipe(pp->cmd, mf_OUT))) return 1;
    1425             : 
    1426           0 :   pari_warn(warner,"broken prettyprinter: '%s'",pp->cmd);
    1427           0 :   pari_free(pp->cmd); pp->cmd = NULL;
    1428           0 :   sd_output("1", d_SILENT);
    1429           0 :   return 0;
    1430             : }
    1431             : 
    1432             : /* n = history number. if n = 0 no history */
    1433             : int
    1434           0 : tex2mail_output(GEN z, long n)
    1435             : {
    1436           0 :   pariout_t T = *(GP_DATA->fmt); /* copy */
    1437           0 :   FILE *log = pari_logfile, *out;
    1438             : 
    1439           0 :   if (!prettyp_init()) return 0;
    1440           0 :   out = GP_DATA->pp->file->file;
    1441             :   /* Emit first: there may be lines before the prompt */
    1442           0 :   if (n) term_color(c_OUTPUT);
    1443           0 :   pari_flush();
    1444           0 :   T.prettyp = f_TEX;
    1445             :   /* history number */
    1446           0 :   if (n)
    1447             :   {
    1448           0 :     pari_sp av = avma;
    1449           0 :     const char *c_hist = term_get_color(NULL, c_HIST);
    1450           0 :     const char *c_out = term_get_color(NULL, c_OUTPUT);
    1451           0 :     if (!(GP_DATA->flags & gpd_QUIET))
    1452             :     {
    1453           0 :       if (*c_hist || *c_out)
    1454           0 :         fprintf(out, "\\LITERALnoLENGTH{%s}\\%%%ld =\\LITERALnoLENGTH{%s} ",
    1455             :                      c_hist, n, c_out);
    1456             :       else
    1457           0 :         fprintf(out, "\\%%%ld = ", n);
    1458             :     }
    1459           0 :     if (log) {
    1460           0 :       switch (pari_logstyle) {
    1461           0 :       case logstyle_plain:
    1462           0 :         fprintf(log, "%%%ld = ", n);
    1463           0 :         break;
    1464           0 :       case logstyle_color:
    1465           0 :         fprintf(log, "%s%%%ld = %s", c_hist, n, c_out);
    1466           0 :         break;
    1467           0 :       case logstyle_TeX:
    1468           0 :         fprintf(log, "\\PARIout{%ld}", n);
    1469           0 :         break;
    1470             :       }
    1471           0 :     }
    1472           0 :     set_avma(av);
    1473             :   }
    1474             :   /* output */
    1475           0 :   fputGEN_pariout(z, &T, out);
    1476             :   /* flush and restore, output to logfile */
    1477           0 :   prettyp_wait(out);
    1478           0 :   if (log) {
    1479           0 :     if (pari_logstyle == logstyle_TeX) {
    1480           0 :       T.TeXstyle |= TEXSTYLE_BREAK;
    1481           0 :       fputGEN_pariout(z, &T, log);
    1482           0 :       fputc('%', log);
    1483             :     } else {
    1484           0 :       T.prettyp = f_RAW;
    1485           0 :       fputGEN_pariout(z, &T, log);
    1486             :     }
    1487           0 :     fputc('\n', log); fflush(log);
    1488             :   }
    1489           0 :   if (n) term_color(c_NONE);
    1490           0 :   pari_flush(); return 1;
    1491             : }
    1492             : 
    1493             : /*******************************************************************/
    1494             : /**                                                               **/
    1495             : /**                     GP-SPECIFIC DEFAULTS                      **/
    1496             : /**                                                               **/
    1497             : /*******************************************************************/
    1498             : 
    1499             : static long
    1500           0 : atocolor(const char *s)
    1501             : {
    1502           0 :   long l = atol(s);
    1503           0 :   if (l & ~0xff) pari_err(e_MISC, "invalid 8bit RGB code: %ld", l);
    1504           0 :   return l;
    1505             : }
    1506             : 
    1507             : GEN
    1508           4 : sd_graphcolormap(const char *v, long flag)
    1509             : {
    1510             :   char *p, *q;
    1511             :   long i, j, l, a, s, *lp;
    1512             : 
    1513           4 :   if (v)
    1514             :   {
    1515           4 :     pari_sp av = avma;
    1516           4 :     char *t = gp_filter(v);
    1517           4 :     if (*t != '[' || t[strlen(t)-1] != ']')
    1518           0 :       pari_err(e_SYNTAX, "incorrect value for graphcolormap", t, t);
    1519          76 :     for (s = 0, p = t+1, l = 2, a=0; *p; p++)
    1520          72 :       if (*p == '[')
    1521             :       {
    1522           0 :         a++;
    1523           0 :         while (*++p != ']')
    1524           0 :           if (!*p || *p == '[')
    1525           0 :             pari_err(e_SYNTAX, "incorrect value for graphcolormap", p, t);
    1526             :       }
    1527          72 :       else if (*p == '"')
    1528             :       {
    1529          36 :         s += sizeof(long)+1;
    1530         236 :         while (*p && *++p != '"') s++;
    1531          36 :         if (!*p) pari_err(e_SYNTAX, "incorrect value for graphcolormap", p, t);
    1532          36 :         s = (s+sizeof(long)-1) & ~(sizeof(long)-1);
    1533             :       }
    1534          36 :       else if (*p == ',')
    1535          32 :         l++;
    1536           4 :     if (l < 4)
    1537           0 :       pari_err(e_MISC, "too few colors (< 4) in graphcolormap");
    1538           4 :     if (GP_DATA->colormap) pari_free(GP_DATA->colormap);
    1539           4 :     GP_DATA->colormap = (GEN)pari_malloc((l+4*a)*sizeof(long) + s);
    1540           4 :     GP_DATA->colormap[0] = evaltyp(t_VEC)|evallg(l);
    1541          76 :     for (p = t+1, i = 1, lp = GP_DATA->colormap+l; i < l; p++)
    1542          72 :       switch(*p)
    1543             :       {
    1544          36 :       case '"':
    1545          36 :         gel(GP_DATA->colormap, i) = lp;
    1546         236 :         q = ++p; while (*q != '"') q++;
    1547          36 :         *q = 0;
    1548          36 :         j = 1 + nchar2nlong(q-p+1);
    1549          36 :         lp[0] = evaltyp(t_STR)|evallg(j);
    1550          36 :         strncpy(GSTR(lp), p, q-p+1);
    1551          36 :         lp += j; p = q;
    1552          36 :         break;
    1553           0 :       case '[': {
    1554             :         const char *ap[3];
    1555           0 :         gel(GP_DATA->colormap, i) = lp;
    1556           0 :         lp[0] = evaltyp(t_VECSMALL)|_evallg(4);
    1557           0 :         for (ap[0] = ++p, j=0; *p && *p != ']'; p++)
    1558           0 :           if (*p == ',' && j<2) { *p++ = 0; ap[++j] = p; }
    1559           0 :         while (j<2) ap[++j] = "0";
    1560           0 :         if (j>2 || *p != ']')
    1561             :         {
    1562             :           char buf[100];
    1563           0 :           sprintf(buf, "incorrect value for graphcolormap[%ld]: ", i);
    1564           0 :           pari_err(e_SYNTAX, buf, p, t);
    1565             :         }
    1566           0 :         *p = '\0';
    1567           0 :         lp[1] = atocolor(ap[0]);
    1568           0 :         lp[2] = atocolor(ap[1]);
    1569           0 :         lp[3] = atocolor(ap[2]);
    1570           0 :         lp += 4;
    1571           0 :         break;
    1572             :       }
    1573          36 :       case ',':
    1574             :       case ']':
    1575          36 :         i++;
    1576          36 :         break;
    1577           0 :       default:
    1578           0 :         pari_err(e_SYNTAX, "incorrect value for graphcolormap", p, t);
    1579             :       }
    1580           4 :     set_avma(av);
    1581             :   }
    1582           4 :   if (flag == d_RETURN || flag == d_ACKNOWLEDGE)
    1583             :   {
    1584           0 :     GEN C = cgetg(lg(GP_DATA->colormap), t_VEC);
    1585           0 :     long i, l = lg(C);
    1586           0 :     for (i = 1; i < l; i++)
    1587             :     {
    1588           0 :       GEN c = gel(GP_DATA->colormap, i);
    1589           0 :       gel(C, i) = (typ(c) == t_STR)? gcopy(c): zv_to_ZV(c);
    1590             :     }
    1591           0 :     if (flag == d_RETURN) return C;
    1592           0 :     pari_printf("   graphcolormap = %Ps\n", C);
    1593             :   }
    1594           4 :   return gnil;
    1595             : }
    1596             : 
    1597             : GEN
    1598           4 : sd_graphcolors(const char *v, long flag)
    1599           4 : { return sd_intarray(v, flag, &(GP_DATA->graphcolors), "graphcolors"); }
    1600             : GEN
    1601          35 : sd_plothsizes(const char *v, long flag)
    1602          35 : { return sd_intarray(v, flag, &(GP_DATA->plothsizes), "plothsizes"); }
    1603             : 
    1604             : GEN
    1605           0 : sd_help(const char *v, long flag)
    1606             : {
    1607             :   const char *str;
    1608           0 :   if (v)
    1609             :   {
    1610           0 :     if (GP_DATA->secure)
    1611           0 :       pari_err(e_MISC,"[secure mode]: can't modify 'help' default (to %s)",v);
    1612           0 :     if (GP_DATA->help) pari_free((void*)GP_DATA->help);
    1613             : #ifndef _WIN32
    1614           0 :     GP_DATA->help = path_expand(v);
    1615             : #else
    1616             :     GP_DATA->help = pari_strdup(v);
    1617             : #endif
    1618             :   }
    1619           0 :   str = GP_DATA->help? GP_DATA->help: "none";
    1620           0 :   if (flag == d_RETURN) return strtoGENstr(str);
    1621           0 :   if (flag == d_ACKNOWLEDGE)
    1622           0 :     pari_printf("   help = \"%s\"\n", str);
    1623           0 :   return gnil;
    1624             : }
    1625             : 
    1626             : static GEN
    1627           0 : sd_prompt_set(const char *v, long flag, const char *how, char **p)
    1628             : {
    1629           0 :   if (v) {
    1630           0 :     if (*p) free(*p);
    1631           0 :     *p = pari_strdup(v);
    1632             :   }
    1633           0 :   if (flag == d_RETURN) return strtoGENstr(*p);
    1634           0 :   if (flag == d_ACKNOWLEDGE)
    1635           0 :     pari_printf("   prompt%s = \"%s\"\n", how, *p);
    1636           0 :   return gnil;
    1637             : }
    1638             : GEN
    1639           0 : sd_prompt(const char *v, long flag)
    1640           0 : { return sd_prompt_set(v, flag, "", &(GP_DATA->prompt)); }
    1641             : GEN
    1642           0 : sd_prompt_cont(const char *v, long flag)
    1643           0 : { return sd_prompt_set(v, flag, "_cont", &(GP_DATA->prompt_cont)); }
    1644             : 
    1645             : GEN
    1646           7 : sd_breakloop(const char *v, long flag)
    1647           7 : { return sd_toggle(v,flag,"breakloop", &(GP_DATA->breakloop)); }
    1648             : GEN
    1649         186 : sd_echo(const char *v, long flag)
    1650         186 : { return sd_ulong(v,flag,"echo", &(GP_DATA->echo), 0,2,NULL); }
    1651             : GEN
    1652           2 : sd_timer(const char *v, long flag)
    1653           2 : { return sd_toggle(v,flag,"timer", &(GP_DATA->chrono)); }
    1654             : GEN
    1655           0 : sd_recover(const char *v, long flag)
    1656           0 : { return sd_toggle(v,flag,"recover", &(GP_DATA->recover)); }
    1657             : 
    1658             : GEN
    1659           0 : sd_psfile(const char *v, long flag)
    1660           0 : { return sd_string(v, flag, "psfile", &current_psfile); }
    1661             : 
    1662             : GEN
    1663           6 : sd_lines(const char *v, long flag)
    1664           6 : { return sd_ulong(v,flag,"lines",&(GP_DATA->lim_lines), 0,LONG_MAX,NULL); }
    1665             : GEN
    1666           0 : sd_linewrap(const char *v, long flag)
    1667             : {
    1668           0 :   ulong old = GP_DATA->linewrap, n = GP_DATA->linewrap;
    1669           0 :   GEN z = sd_ulong(v,flag,"linewrap",&n, 0,LONG_MAX,NULL);
    1670           0 :   if (old)
    1671           0 :   { if (!n) resetout(1); }
    1672             :   else
    1673           0 :   { if (n) init_linewrap(n); }
    1674           0 :   GP_DATA->linewrap = n; return z;
    1675             : }
    1676             : 
    1677             : /* readline-specific defaults */
    1678             : GEN
    1679           0 : sd_readline(const char *v, long flag)
    1680             : {
    1681           0 :   const char *msg[] = {
    1682             :     "(bits 0x2/0x4 control matched-insert/arg-complete)", NULL};
    1683           0 :   ulong state = GP_DATA->readline_state;
    1684           0 :   GEN res = sd_ulong(v,flag,"readline", &GP_DATA->readline_state, 0, 7, msg);
    1685             : 
    1686           0 :   if (state != GP_DATA->readline_state)
    1687           0 :     (void)sd_toggle(GP_DATA->readline_state? "1": "0", d_SILENT, "readline", &(GP_DATA->use_readline));
    1688           0 :   return res;
    1689             : }
    1690             : GEN
    1691           4 : sd_histfile(const char *v, long flag)
    1692             : {
    1693           4 :   char *old = GP_DATA->histfile;
    1694           4 :   GEN r = sd_string(v, flag, "histfile", &GP_DATA->histfile);
    1695           4 :   if (v && !*v)
    1696             :   {
    1697           0 :     free(GP_DATA->histfile);
    1698           0 :     GP_DATA->histfile = NULL;
    1699             :   }
    1700           4 :   else if (GP_DATA->histfile != old && (!old || strcmp(old,GP_DATA->histfile)))
    1701             :   {
    1702           4 :     if (cb_pari_init_histfile) cb_pari_init_histfile();
    1703             :   }
    1704           4 :   return r;
    1705             : }
    1706             : 
    1707             : /********************************************************************/
    1708             : /**                                                                **/
    1709             : /**                         METACOMMANDS                           **/
    1710             : /**                                                                **/
    1711             : /********************************************************************/
    1712             : void
    1713           6 : pari_print_version(void)
    1714             : {
    1715           6 :   pari_sp av = avma;
    1716           6 :   char *buf, *ver = what_cc();
    1717           6 :   const char *kver = pari_kernel_version();
    1718           6 :   const char *date = paricfg_compiledate;
    1719             : 
    1720           6 :   pari_center(paricfg_version);
    1721           6 :   buf = stack_malloc(strlen(paricfg_buildinfo) + 2 + strlen(kver));
    1722           6 :   (void)sprintf(buf, paricfg_buildinfo, kver);
    1723           6 :   pari_center(buf);
    1724           6 :   buf = stack_malloc(strlen(date) + 32 + (ver? strlen(ver): 0));
    1725           6 :   if (ver) (void)sprintf(buf, "compiled: %s, %s", date, ver);
    1726           0 :   else     (void)sprintf(buf, "compiled: %s", date);
    1727           6 :   pari_center(buf);
    1728           6 :   sprintf(buf, "threading engine: %s",paricfg_mt_engine);
    1729           6 :   pari_center(buf);
    1730           6 :   ver = what_readline();
    1731           6 :   buf = stack_malloc(strlen(ver) + 64);
    1732           6 :   (void)sprintf(buf, "(readline %s, extended help%s enabled)", ver,
    1733           6 :                 has_ext_help()? "": " not");
    1734           6 :   pari_center(buf); set_avma(av);
    1735           6 : }
    1736             : 
    1737             : static int
    1738           7 : cmp_epname(void *E, GEN e, GEN f)
    1739             : {
    1740             :   (void)E;
    1741           7 :   return strcmp(((entree*)e)->name, ((entree*)f)->name);
    1742             : }
    1743             : /* if fun is set print only closures, else only non-closures
    1744             :  * if member is set print only member functions, else only non-members */
    1745             : static void
    1746           7 : print_all_user_obj(int fun, int member)
    1747             : {
    1748           7 :   pari_sp av = avma;
    1749           7 :   long i, iL = 0, lL = 1024;
    1750           7 :   GEN L = cgetg(lL+1, t_VECSMALL);
    1751             :   entree *ep;
    1752         952 :   for (i = 0; i < functions_tblsz; i++)
    1753       10472 :     for (ep = functions_hash[i]; ep; ep = ep->next)
    1754        9527 :       if (EpVALENCE(ep) == EpVAR && fun == (typ((GEN)ep->value) == t_CLOSURE))
    1755             :       {
    1756          14 :         const char *f = ep->name;
    1757          14 :         if (member == (f[0] == '_' && f[1] == '.'))
    1758             :         {
    1759          14 :           if (iL >= lL) { lL *= 2; L = vecsmall_lengthen(L, lL); }
    1760          14 :           L[++iL] = (long)ep;
    1761             :         }
    1762             :       }
    1763           7 :   if (iL)
    1764             :   {
    1765           7 :     setlg(L, iL+1);
    1766           7 :     gen_sort_inplace(L, NULL, &cmp_epname, NULL);
    1767          21 :     for (i = 1; i <= iL; i++)
    1768             :     {
    1769          14 :       ep = (entree*)L[i];
    1770          14 :       pari_printf("%s =\n  %Ps\n\n", ep->name, ep->value);
    1771             :     }
    1772             :   }
    1773           7 :   set_avma(av);
    1774           7 : }
    1775             : 
    1776             : /* get_sep, removing enclosing quotes */
    1777             : static char *
    1778         133 : get_name(const char *s)
    1779             : {
    1780         133 :   char *t = get_sep(s);
    1781         133 :   if (*t == '"')
    1782             :   {
    1783          56 :     long n = strlen(t)-1;
    1784          56 :     if (t[n] == '"') { t[n] = 0; t++; }
    1785             :   }
    1786         133 :   return t;
    1787             : }
    1788             : static void
    1789          56 : ack_debug(const char *s, long d) {pari_printf("   debug(\"%s\") = %ld\n",s,d);}
    1790             : static void
    1791          42 : ack_setdebug(const char *s, long d) {setdebug(s, d); ack_debug(s, d);}
    1792             : 
    1793             : static void
    1794         470 : escape(const char *tch, int ismain)
    1795             : {
    1796         470 :   const char *s = tch;
    1797             :   long d;
    1798             :   char c;
    1799             :   GEN x;
    1800         470 :   switch ((c = *s++))
    1801             :   {
    1802           0 :     case 'w': case 'x': case 'a': case 'b': case 'B': case 'm':
    1803             :     { /* history things */
    1804           0 :       if (c != 'w' && c != 'x') d = get_int(s,0);
    1805             :       else
    1806             :       {
    1807           0 :         d = atol(s); if (*s == '-') s++;
    1808           0 :         while (isdigit((unsigned char)*s)) s++;
    1809             :       }
    1810           0 :       x = pari_get_hist(d);
    1811           0 :       switch (c)
    1812             :       {
    1813           0 :         case 'B': /* prettyprinter */
    1814           0 :           if (tex2mail_output(x,0)) break;
    1815             :         case 'b': /* fall through */
    1816           0 :         case 'm': matbrute(x, GP_DATA->fmt->format, -1); break;
    1817           0 :         case 'a': brute(x, GP_DATA->fmt->format, -1); break;
    1818           0 :         case 'x': dbgGEN(x, get_int(s, -1)); break;
    1819           0 :         case 'w':
    1820           0 :           s = get_name(s); if (!*s) s = current_logfile;
    1821           0 :           write0(s, mkvec(x)); return;
    1822             :       }
    1823           0 :       pari_putc('\n'); return;
    1824             :     }
    1825             : 
    1826           0 :     case 'c': commands(-1); break;
    1827           0 :     case 'd': (void)setdefault(NULL,NULL,d_SILENT); break;
    1828         109 :     case 'e':
    1829         109 :       s = get_sep(s);
    1830         109 :       if (!*s) s = (GP_DATA->echo)? "0": "1";
    1831         109 :       (void)sd_echo(s,d_ACKNOWLEDGE); break;
    1832         112 :     case 'g':
    1833         112 :         if (isdigit((unsigned char)*s))
    1834             :         {
    1835          35 :           const char *t = s + 1;
    1836          35 :           if (isdigit((unsigned char)*t)) t++; /* atol(s) < 99 */
    1837          35 :           t = get_name(t);
    1838          35 :           if (*t) { d = atol(s); ack_setdebug(t, d); break; }
    1839             :         }
    1840          77 :         else if (*s == '"' || isalpha((unsigned char)*s))
    1841             :         {
    1842          77 :           char *t = get_name(s);
    1843          77 :           if (t[1] && !isdigit((unsigned char)t[1]))
    1844          42 :           {
    1845          56 :             char *T = t + strlen(t) - 1;
    1846          56 :             if (isdigit((unsigned char)*T))
    1847             :             {
    1848          21 :               if (isdigit((unsigned char)T[-1])) T--; /* < 99 */
    1849          21 :               d = atol(T); *T = 0;
    1850          21 :               ack_setdebug(get_name(t), d); /* get_name in case of ".." */
    1851             :             }
    1852             :             else
    1853             :             {
    1854          35 :               x = setdebug(t, -1); ack_debug(t, itos(x));
    1855             :             }
    1856             :           }
    1857          21 :           else switch (*t)
    1858             :           {
    1859           0 :             case 'm':
    1860           0 :               s++; (void)sd_debugmem(*s? s: NULL,d_ACKNOWLEDGE); break;
    1861          21 :             case 'f':
    1862          21 :               s++; (void)sd_debugfiles(*s? s: NULL,d_ACKNOWLEDGE); break;
    1863             :           }
    1864          63 :           break;
    1865             :         }
    1866          14 :         (void)sd_debug(*s? s: NULL,d_ACKNOWLEDGE); break;
    1867             :       break;
    1868           0 :     case 'h': print_functions_hash(s); break;
    1869           0 :     case 'l':
    1870           0 :       s = get_name(s);
    1871           0 :       if (*s)
    1872             :       {
    1873           0 :         if (pari_logfile) { (void)sd_logfile(s,d_ACKNOWLEDGE);break; }
    1874           0 :         (void)sd_logfile(s,d_SILENT);
    1875             :       }
    1876           0 :       (void)sd_log(pari_logfile?"0":"1",d_ACKNOWLEDGE);
    1877           0 :       break;
    1878           0 :     case 'o': (void)sd_output(*s? s: NULL,d_ACKNOWLEDGE); break;
    1879         235 :     case 'p':
    1880         235 :       switch (*s)
    1881             :       {
    1882           7 :         case 's': s++;
    1883           7 :           (void)sd_seriesprecision(*s? s: NULL,d_ACKNOWLEDGE); break;
    1884          14 :         case 'b' : s++;
    1885          14 :           (void)sd_realbitprecision(*s? s: NULL,d_ACKNOWLEDGE); break;
    1886         214 :         default :
    1887         214 :           (void)sd_realprecision(*s? s: NULL,d_ACKNOWLEDGE); break;
    1888             :       }
    1889         235 :       break;
    1890           0 :     case 'q': cb_pari_quit(0); break;
    1891           0 :     case 'r':
    1892           0 :       s = get_name(s);
    1893           0 :       if (!ismain) { (void)gp_read_file(s); break; }
    1894           0 :       switchin(s);
    1895           0 :       if (file_is_binary(pari_infile))
    1896             :       {
    1897           0 :         pari_sp av = avma;
    1898             :         int vector;
    1899           0 :         GEN x = readbin(s,pari_infile, &vector);
    1900           0 :         popinfile();
    1901           0 :         if (!x) pari_err_FILE("input file",s);
    1902           0 :         if (vector) /* many BIN_GEN */
    1903             :         {
    1904           0 :           long i, l = lg(x);
    1905           0 :           pari_warn(warner,"setting %ld history entries", l-1);
    1906           0 :           for (i=1; i<l; i++) pari_add_hist(gel(x,i), 0, 0);
    1907             :         }
    1908           0 :         set_avma(av);
    1909             :       }
    1910           0 :       break;
    1911           0 :     case 's': dbg_pari_heap(); break;
    1912           7 :     case 't': gentypes(); break;
    1913           7 :     case 'u':
    1914           7 :       switch(*s)
    1915             :       {
    1916           0 :         case 'v':
    1917           0 :           if (*++s) break;
    1918           0 :           print_all_user_obj(0, 0); return;
    1919           0 :         case 'm':
    1920           0 :           if (*++s) break;
    1921           0 :           print_all_user_obj(1, 1); return;
    1922           7 :         case '\0':
    1923           7 :           print_all_user_obj(1, 0); return;
    1924             :       }
    1925           0 :       pari_err(e_SYNTAX,"unexpected character", s,tch-1); break;
    1926           0 :     case 'v':
    1927           0 :       if (*s) pari_err(e_SYNTAX,"unexpected character", s,tch-1);
    1928           0 :       pari_print_version(); break;
    1929           0 :     case 'y':
    1930           0 :       s = get_sep(s);
    1931           0 :       if (!*s) s = (GP_DATA->simplify)? "0": "1";
    1932           0 :       (void)sd_simplify(s,d_ACKNOWLEDGE); break;
    1933           0 :     default: pari_err(e_SYNTAX,"unexpected character", tch,tch-1);
    1934             :   }
    1935             : }
    1936             : 
    1937             : static int
    1938         487 : chron(const char *s)
    1939             : {
    1940         487 :   if (*s)
    1941             :   { /* if "#" or "##" timer metacommand. Otherwise let the parser get it */
    1942             :     const char *t;
    1943         487 :     if (*s == '#') s++;
    1944         487 :     if (*s) return 0;
    1945           0 :     t = gp_format_time(pari_get_histtime(0));
    1946           0 :     if (pari_mt_nbthreads==1)
    1947           0 :       pari_printf("  ***   last result computed in %s.\n", t);
    1948             :     else
    1949             :     {
    1950           0 :       const char *r = gp_format_time(pari_get_histrtime(0));
    1951           0 :       pari_printf("  ***   last result: cpu time %s, real time %s.\n", t,r);
    1952             :     }
    1953             :   }
    1954           0 :   else { GP_DATA->chrono ^= 1; (void)sd_timer(NULL,d_ACKNOWLEDGE); }
    1955           0 :   return 1;
    1956             : }
    1957             : 
    1958             : /* return 0: can't interpret *buf as a metacommand
    1959             :  *        1: did interpret *buf as a metacommand or empty command */
    1960             : int
    1961      936406 : gp_meta(const char *buf, int ismain)
    1962             : {
    1963      936406 :   switch(*buf++)
    1964             :   {
    1965         155 :     case '?': gp_help(buf, h_REGULAR); break;
    1966         487 :     case '#': return chron(buf);
    1967         470 :     case '\\': escape(buf, ismain); break;
    1968       19704 :     case '\0': break;
    1969      915590 :     default: return 0;
    1970             :   }
    1971       20308 :   return 1;
    1972             : }

Generated by: LCOV version 1.14