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

Generated by: LCOV version 1.16