Code coverage tests

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

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

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

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

Generated by: LCOV version 1.16