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

Generated by: LCOV version 1.16