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

Generated by: LCOV version 1.16