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 - default.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27783-affec94c65) Lines: 358 569 62.9 %
Date: 2022-07-07 07:34:25 Functions: 42 67 62.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             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : #ifdef _WIN32
      18             : #  include "../systems/mingw/mingw.h"
      19             : #endif
      20             : 
      21             : /* Return all chars, up to next separator
      22             :  * [as strtok but must handle verbatim character string] */
      23             : char*
      24        2294 : get_sep(const char *t)
      25             : {
      26        2294 :   char *buf = stack_malloc(strlen(t)+1);
      27        2294 :   char *s = buf;
      28        2294 :   int outer = 1;
      29             : 
      30             :   for(;;)
      31             :   {
      32        8194 :     switch(*s++ = *t++)
      33             :     {
      34         112 :       case '"':
      35         112 :         outer = !outer; break;
      36        2287 :       case '\0':
      37        2287 :         return buf;
      38           0 :       case ';':
      39           0 :         if (outer) { s[-1] = 0; return buf; }
      40           0 :         break;
      41           7 :       case '\\': /* gobble next char */
      42           7 :         if (! (*s++ = *t++) ) return buf;
      43             :     }
      44             :   }
      45             : }
      46             : 
      47             : /* "atoul" + optional [kmg] suffix */
      48             : static ulong
      49        1897 : my_int(char *s)
      50             : {
      51        1897 :   ulong n = 0;
      52        1897 :   char *p = s;
      53             : 
      54        5989 :   while (isdigit((int)*p)) {
      55             :     ulong m;
      56        4092 :     if (n > (~0UL / 10)) pari_err(e_SYNTAX,"integer too large",s,s);
      57        4092 :     n *= 10; m = n;
      58        4092 :     n += *p++ - '0';
      59        4092 :     if (n < m) pari_err(e_SYNTAX,"integer too large",s,s);
      60             :   }
      61        1897 :   if (n)
      62             :   {
      63        1848 :     switch(*p)
      64             :     {
      65           0 :       case 'k': case 'K': n = umuluu_or_0(n,1000UL);       p++; break;
      66         368 :       case 'm': case 'M': n = umuluu_or_0(n,1000000UL);    p++; break;
      67           0 :       case 'g': case 'G': n = umuluu_or_0(n,1000000000UL); p++; break;
      68             : #ifdef LONG_IS_64BIT
      69           0 :       case 't': case 'T': n = umuluu_or_0(n,1000000000000UL); p++; break;
      70             : #endif
      71             :     }
      72        1848 :     if (!n) pari_err(e_SYNTAX,"integer too large",s,s);
      73             :   }
      74        1897 :   if (*p) pari_err(e_SYNTAX,"I was expecting an integer here", s, s);
      75        1897 :   return n;
      76             : }
      77             : 
      78             : long
      79          44 : get_int(const char *s, long dflt)
      80             : {
      81          44 :   pari_sp av = avma;
      82          44 :   char *p = get_sep(s);
      83             :   long n;
      84          44 :   int minus = 0;
      85             : 
      86          44 :   if (*p == '-') { minus = 1; p++; }
      87          44 :   if (!isdigit((int)*p)) return gc_long(av, dflt);
      88             : 
      89          44 :   n = (long)my_int(p);
      90          44 :   if (n < 0) pari_err(e_SYNTAX,"integer too large",s,s);
      91          44 :   return gc_long(av, minus? -n: n);
      92             : }
      93             : 
      94             : ulong
      95        1853 : get_uint(const char *s)
      96             : {
      97        1853 :   pari_sp av = avma;
      98        1853 :   char *p = get_sep(s);
      99        1853 :   if (*p == '-') pari_err(e_SYNTAX,"arguments must be positive integers",s,s);
     100        1853 :   return gc_ulong(av, my_int(p));
     101             : }
     102             : 
     103             : #if defined(__EMX__) || defined(_WIN32) || defined(__CYGWIN32__)
     104             : #  define PATH_SEPARATOR ';' /* beware DOSish 'C:' disk drives */
     105             : #else
     106             : #  define PATH_SEPARATOR ':'
     107             : #endif
     108             : 
     109             : static const char *
     110        1802 : pari_default_path(void) {
     111             : #if PATH_SEPARATOR == ';'
     112             :   return ".;C:;C:/gp";
     113             : #elif defined(UNIX)
     114        1802 :   return ".:~:~/gp";
     115             : #else
     116             :   return ".";
     117             : #endif
     118             : }
     119             : 
     120             : static void
     121        7168 : delete_dirs(gp_path *p)
     122             : {
     123        7168 :   char **v = p->dirs, **dirs;
     124        7168 :   if (v)
     125             :   {
     126        3584 :     p->dirs = NULL; /* in case of error */
     127        8960 :     for (dirs = v; *dirs; dirs++) pari_free(*dirs);
     128        3584 :     pari_free(v);
     129             :   }
     130        7168 : }
     131             : 
     132             : static void
     133        3584 : expand_path(gp_path *p)
     134             : {
     135        3584 :   char **dirs, *s, *v = p->PATH;
     136        3584 :   int i, n = 0;
     137             : 
     138        3584 :   delete_dirs(p);
     139        3584 :   if (*v)
     140             :   {
     141        1792 :     v = pari_strdup(v);
     142       16128 :     for (s=v; *s; s++)
     143       14336 :       if (*s == PATH_SEPARATOR) {
     144        3584 :         *s = 0;
     145        3584 :         if (s == v || s[-1] != 0) n++; /* ignore empty path components */
     146             :       }
     147        1792 :     dirs = (char**) pari_malloc((n + 2)*sizeof(char *));
     148             : 
     149        7168 :     for (s=v, i=0; i<=n; i++)
     150             :     {
     151             :       char *end, *f;
     152        5376 :       while (!*s) s++; /* skip empty path components */
     153        5376 :       f = end = s + strlen(s);
     154        5376 :       while (f > s && *--f == '/') *f = 0; /* skip trailing '/' */
     155        5376 :       dirs[i] = path_expand(s);
     156        5376 :       s = end + 1; /* next path component */
     157             :     }
     158        1792 :     pari_free((void*)v);
     159             :   }
     160             :   else
     161             :   {
     162        1792 :     dirs = (char**) pari_malloc(sizeof(char *));
     163        1792 :     i = 0;
     164             :   }
     165        3584 :   dirs[i] = NULL; p->dirs = dirs;
     166        3584 : }
     167             : void
     168        1792 : pari_init_paths(void)
     169             : {
     170        1792 :   expand_path(GP_DATA->path);
     171        1792 :   expand_path(GP_DATA->sopath);
     172        1792 : }
     173             : 
     174             : static void
     175        3584 : delete_path(gp_path *p) { delete_dirs(p); free(p->PATH); }
     176             : void
     177        1792 : pari_close_paths(void)
     178             : {
     179        1792 :   delete_path(GP_DATA->path);
     180        1792 :   delete_path(GP_DATA->sopath);
     181        1792 : }
     182             : 
     183             : /********************************************************************/
     184             : /*                                                                  */
     185             : /*                            DEFAULTS                              */
     186             : /*                                                                  */
     187             : /********************************************************************/
     188             : 
     189             : long
     190           0 : getrealprecision(void)
     191             : {
     192           0 :   return GP_DATA->fmt->sigd;
     193             : }
     194             : 
     195             : long
     196           0 : setrealprecision(long n, long *prec)
     197             : {
     198           0 :   GP_DATA->fmt->sigd = n;
     199           0 :   *prec = ndec2prec(n);
     200           0 :   precreal = prec2nbits(*prec);
     201           0 :   return n;
     202             : }
     203             : 
     204             : GEN
     205          44 : sd_toggle(const char *v, long flag, const char *s, int *ptn)
     206             : {
     207          44 :   int state = *ptn;
     208          44 :   if (v)
     209             :   {
     210          44 :     int n = (int)get_int(v,0);
     211          44 :     if (n == state) return gnil;
     212          44 :     if (n != !state)
     213             :     {
     214           0 :       char *t = stack_malloc(64 + strlen(s));
     215           0 :       (void)sprintf(t, "default: incorrect value for %s [0:off / 1:on]", s);
     216           0 :       pari_err(e_SYNTAX, t, v,v);
     217             :     }
     218          44 :     state = *ptn = n;
     219             :   }
     220          44 :   switch(flag)
     221             :   {
     222           0 :     case d_RETURN: return utoi(state);
     223           0 :     case d_ACKNOWLEDGE:
     224           0 :       if (state) pari_printf("   %s = 1 (on)\n", s);
     225           0 :       else       pari_printf("   %s = 0 (off)\n", s);
     226           0 :       break;
     227             :   }
     228          44 :   return gnil;
     229             : }
     230             : 
     231             : static void
     232        1853 : sd_ulong_init(const char *v, const char *s, ulong *ptn, ulong Min, ulong Max)
     233             : {
     234        1853 :   if (v)
     235             :   {
     236        1853 :     ulong n = get_uint(v);
     237        1853 :     if (n > Max || n < Min)
     238             :     {
     239           2 :       char *buf = stack_malloc(strlen(s) + 2 * 20 + 40);
     240           2 :       (void)sprintf(buf, "default: incorrect value for %s [%lu-%lu]",
     241             :                     s, Min, Max);
     242           2 :       pari_err(e_SYNTAX, buf, v,v);
     243             :     }
     244        1851 :     *ptn = n;
     245             :   }
     246        1851 : }
     247             : 
     248             : /* msg is NULL or NULL-terminated array with msg[0] != NULL. */
     249             : GEN
     250         723 : sd_ulong(const char *v, long flag, const char *s, ulong *ptn, ulong Min, ulong Max,
     251             :          const char **msg)
     252             : {
     253         723 :   ulong n = *ptn;
     254         723 :   sd_ulong_init(v, s, ptn, Min, Max);
     255         721 :   switch(flag)
     256             :   {
     257           0 :     case d_RETURN:
     258           0 :       return utoi(*ptn);
     259         151 :     case d_ACKNOWLEDGE:
     260         151 :       if (!v || *ptn != n) {
     261         151 :         if (!msg)         /* no specific message */
     262         144 :           pari_printf("   %s = %lu\n", s, *ptn);
     263           7 :         else if (!msg[1]) /* single message, always printed */
     264           7 :           pari_printf("   %s = %lu %s\n", s, *ptn, msg[0]);
     265             :         else              /* print (new)-n-th message */
     266           0 :           pari_printf("   %s = %lu %s\n", s, *ptn, msg[*ptn]);
     267             :       }
     268         151 :       break;
     269             :   }
     270         721 :   return gnil;
     271             : }
     272             : 
     273             : static void
     274          21 : err_intarray(char *t, char *p, const char *s)
     275             : {
     276          21 :   char *b = stack_malloc(64 + strlen(s));
     277          21 :   sprintf(b, "incorrect value for %s", s);
     278          21 :   pari_err(e_SYNTAX, b, p, t);
     279           0 : }
     280             : static GEN
     281          39 : parse_intarray(const char *v, const char *s)
     282             : {
     283          39 :   pari_sp av = avma;
     284          39 :   char *p, *t = gp_filter(v);
     285             :   long i, l;
     286             :   GEN w;
     287          39 :   if (*t != '[') err_intarray(t, t, s);
     288          32 :   if (t[1] == ']') return gc_const(av, cgetalloc(t_VECSMALL, 1));
     289         125 :   for (p = t+1, l=2; *p; p++)
     290         111 :     if (*p == ',') l++;
     291          70 :     else if (*p < '0' || *p > '9') break;
     292          32 :   if (*p != ']') err_intarray(t, p, s);
     293          18 :   w = cgetalloc(t_VECSMALL, l);
     294          70 :   for (p = t+1, i=0; *p; p++)
     295             :   {
     296          52 :     long n = 0;
     297          97 :     while (*p >= '0' && *p <= '9') n = 10*n + (*p++ -'0');
     298          52 :     w[++i] = n;
     299             :   }
     300          18 :   return gc_const(av, w);
     301             : }
     302             : GEN
     303          39 : sd_intarray(const char *v, long flag, GEN *pz, const char *s)
     304             : {
     305          39 :   if (v) { GEN z = *pz; *pz = parse_intarray(v, s); pari_free(z); }
     306          18 :   switch(flag)
     307             :   {
     308           0 :     case d_RETURN: return zv_to_ZV(*pz);
     309           0 :     case d_ACKNOWLEDGE: pari_printf("   %s = %Ps\n", s, zv_to_ZV(*pz));
     310             :   }
     311          18 :   return gnil;
     312             : }
     313             : 
     314             : GEN
     315        1095 : sd_realprecision(const char *v, long flag)
     316             : {
     317        1095 :   pariout_t *fmt = GP_DATA->fmt;
     318        1095 :   if (v)
     319             :   {
     320        1095 :     ulong newnb = fmt->sigd;
     321             :     long prec;
     322        1095 :     sd_ulong_init(v, "realprecision", &newnb, 1, prec2ndec(LGBITS));
     323        1109 :     if (fmt->sigd == (long)newnb) return gnil;
     324         541 :     if (fmt->sigd >= 0) fmt->sigd = newnb;
     325         541 :     prec = ndec2nbits(newnb);
     326         541 :     if (prec == precreal) return gnil;
     327         527 :     precreal = prec;
     328             :   }
     329         527 :   if (flag == d_RETURN) return stoi(nbits2ndec(precreal));
     330         527 :   if (flag == d_ACKNOWLEDGE)
     331             :   {
     332         179 :     long n = nbits2ndec(precreal);
     333         179 :     pari_printf("   realprecision = %ld significant digits", n);
     334         179 :     if (fmt->sigd < 0)
     335           0 :       pari_puts(" (all digits displayed)");
     336         179 :     else if (n != fmt->sigd)
     337          21 :       pari_printf(" (%ld digits displayed)", fmt->sigd);
     338         179 :     pari_putc('\n');
     339             :   }
     340         527 :   return gnil;
     341             : }
     342             : 
     343             : GEN
     344          35 : sd_realbitprecision(const char *v, long flag)
     345             : {
     346          35 :   pariout_t *fmt = GP_DATA->fmt;
     347          35 :   if (v)
     348             :   {
     349          35 :     ulong newnb = precreal;
     350             :     long n;
     351          35 :     sd_ulong_init(v, "realbitprecision", &newnb, 1, prec2nbits(LGBITS));
     352          35 :     if ((long)newnb == precreal) return gnil;
     353          35 :     n = nbits2ndec(newnb);
     354          35 :     if (!n) n = 1;
     355          35 :     if (fmt->sigd >= 0) fmt->sigd = n;
     356          35 :     precreal = (long) newnb;
     357             :   }
     358          35 :   if (flag == d_RETURN) return stoi(precreal);
     359          35 :   if (flag == d_ACKNOWLEDGE)
     360             :   {
     361          14 :     pari_printf("   realbitprecision = %ld significant bits", precreal);
     362          14 :     if (fmt->sigd < 0)
     363           0 :       pari_puts(" (all digits displayed)");
     364             :     else
     365          14 :       pari_printf(" (%ld decimal digits displayed)", fmt->sigd);
     366          14 :     pari_putc('\n');
     367             :   }
     368          35 :   return gnil;
     369             : }
     370             : 
     371             : GEN
     372          35 : sd_seriesprecision(const char *v, long flag)
     373             : {
     374          35 :   const char *msg[] = {"significant terms", NULL};
     375          35 :   return sd_ulong(v,flag,"seriesprecision",&precdl, 1,LGBITS,msg);
     376             : }
     377             : 
     378             : static long
     379          28 : gp_get_color(char **st)
     380             : {
     381          28 :   char *s, *v = *st;
     382             :   int trans;
     383             :   long c;
     384          28 :   if (isdigit((int)*v))
     385          28 :     { c = atol(v); trans = 1; } /* color on transparent background */
     386             :   else
     387             :   {
     388           0 :     if (*v == '[')
     389             :     {
     390             :       const char *a[3];
     391           0 :       long i = 0;
     392           0 :       for (a[0] = s = ++v; *s && *s != ']'; s++)
     393           0 :         if (*s == ',') { *s = 0; a[++i] = s+1; }
     394           0 :       if (*s != ']') pari_err(e_SYNTAX,"expected character: ']'",s, *st);
     395           0 :       *s = 0; for (i++; i<3; i++) a[i] = "";
     396             :       /*    properties    |   color    | background */
     397           0 :       c = (atoi(a[2])<<8) | atoi(a[0]) | (atoi(a[1])<<4);
     398           0 :       trans = (*(a[1]) == 0);
     399           0 :       v = s + 1;
     400             :     }
     401           0 :     else { c = c_NONE; trans = 0; }
     402             :   }
     403          28 :   if (trans) c = c | (1L<<12);
     404          56 :   while (*v && *v++ != ',') /* empty */;
     405          28 :   if (c != c_NONE) disable_color = 0;
     406          28 :   *st = v; return c;
     407             : }
     408             : 
     409             : /* 1: error, 2: history, 3: prompt, 4: input, 5: output, 6: help, 7: timer */
     410             : GEN
     411           4 : sd_colors(const char *v, long flag)
     412             : {
     413             :   long c,l;
     414           4 :   if (v && !(GP_DATA->flags & (gpd_EMACS|gpd_TEXMACS)))
     415             :   {
     416           4 :     pari_sp av = avma;
     417             :     char *s;
     418           4 :     disable_color=1;
     419           4 :     l = strlen(v);
     420           4 :     if (l <= 2 && strncmp(v, "no", l) == 0)
     421           0 :       v = "";
     422           4 :     if (l <= 6 && strncmp(v, "darkbg", l) == 0)
     423           0 :       v = "1, 5, 3, 7, 6, 2, 3"; /* Assume recent ReadLine. */
     424           4 :     if (l <= 7 && strncmp(v, "lightbg", l) == 0)
     425           4 :       v = "1, 6, 3, 4, 5, 2, 3"; /* Assume recent ReadLine. */
     426           4 :     if (l <= 8 && strncmp(v, "brightfg", l) == 0)      /* Good for windows consoles */
     427           0 :       v = "9, 13, 11, 15, 14, 10, 11";
     428           4 :     if (l <= 6 && strncmp(v, "boldfg", l) == 0)        /* Good for darkbg consoles */
     429           0 :       v = "[1,,1], [5,,1], [3,,1], [7,,1], [6,,1], , [2,,1]";
     430           4 :     s = gp_filter(v);
     431          32 :     for (c=c_ERR; c < c_LAST; c++) gp_colors[c] = gp_get_color(&s);
     432           4 :     set_avma(av);
     433             :   }
     434           4 :   if (flag == d_ACKNOWLEDGE || flag == d_RETURN)
     435             :   {
     436           0 :     char s[128], *t = s;
     437             :     long col[3], n;
     438           0 :     for (*t=0,c=c_ERR; c < c_LAST; c++)
     439             :     {
     440           0 :       n = gp_colors[c];
     441           0 :       if (n == c_NONE)
     442           0 :         sprintf(t,"no");
     443             :       else
     444             :       {
     445           0 :         decode_color(n,col);
     446           0 :         if (n & (1L<<12))
     447             :         {
     448           0 :           if (col[0])
     449           0 :             sprintf(t,"[%ld,,%ld]",col[1],col[0]);
     450             :           else
     451           0 :             sprintf(t,"%ld",col[1]);
     452             :         }
     453             :         else
     454           0 :           sprintf(t,"[%ld,%ld,%ld]",col[1],col[2],col[0]);
     455             :       }
     456           0 :       t += strlen(t);
     457           0 :       if (c < c_LAST - 1) { *t++=','; *t++=' '; }
     458             :     }
     459           0 :     if (flag==d_RETURN) return strtoGENstr(s);
     460           0 :     pari_printf("   colors = \"%s\"\n",s);
     461             :   }
     462           4 :   return gnil;
     463             : }
     464             : 
     465             : GEN
     466           7 : sd_format(const char *v, long flag)
     467             : {
     468           7 :   pariout_t *fmt = GP_DATA->fmt;
     469           7 :   if (v)
     470             :   {
     471           7 :     char c = *v;
     472           7 :     if (c!='e' && c!='f' && c!='g')
     473           0 :       pari_err(e_SYNTAX,"default: inexistent format",v,v);
     474           7 :     fmt->format = c; v++;
     475             : 
     476           7 :     if (isdigit((int)*v))
     477           0 :       { while (isdigit((int)*v)) v++; } /* FIXME: skip obsolete field width */
     478           7 :     if (*v++ == '.')
     479             :     {
     480           7 :       if (*v == '-') fmt->sigd = -1;
     481             :       else
     482           7 :         if (isdigit((int)*v)) fmt->sigd=atol(v);
     483             :     }
     484             :   }
     485           7 :   if (flag == d_RETURN)
     486             :   {
     487           0 :     char *s = stack_malloc(64);
     488           0 :     (void)sprintf(s, "%c.%ld", fmt->format, fmt->sigd);
     489           0 :     return strtoGENstr(s);
     490             :   }
     491           7 :   if (flag == d_ACKNOWLEDGE)
     492           0 :     pari_printf("   format = %c.%ld\n", fmt->format, fmt->sigd);
     493           7 :   return gnil;
     494             : }
     495             : 
     496             : GEN
     497           0 : sd_compatible(const char *v, long flag)
     498             : {
     499           0 :   const char *msg[] = {
     500             :     "(no backward compatibility)",
     501             :     "(no backward compatibility)",
     502             :     "(no backward compatibility)",
     503             :     "(no backward compatibility)", NULL
     504             :   };
     505           0 :   ulong junk = 0;
     506           0 :   return sd_ulong(v,flag,"compatible",&junk, 0,3,msg);
     507             : }
     508             : 
     509             : GEN
     510           0 : sd_secure(const char *v, long flag)
     511             : {
     512           0 :   if (v && GP_DATA->secure)
     513           0 :     pari_ask_confirm("[secure mode]: About to modify the 'secure' flag");
     514           0 :   return sd_toggle(v,flag,"secure", &(GP_DATA->secure));
     515             : }
     516             : 
     517             : GEN
     518          28 : sd_debug(const char *v, long flag)
     519             : {
     520          28 :   GEN r = sd_ulong(v,flag,"debug",&DEBUGLEVEL, 0,20,NULL);
     521          28 :   if (v) setalldebug(DEBUGLEVEL);
     522          28 :   return r;
     523             : }
     524             : 
     525             : GEN
     526          21 : sd_debugfiles(const char *v, long flag)
     527          21 : { return sd_ulong(v,flag,"debugfiles",&DEBUGLEVEL_io, 0,20,NULL); }
     528             : 
     529             : GEN
     530           0 : sd_debugmem(const char *v, long flag)
     531           0 : { return sd_ulong(v,flag,"debugmem",&DEBUGMEM, 0,20,NULL); }
     532             : 
     533             : /* set D->hist to size = s / total = t */
     534             : static void
     535        1816 : init_hist(gp_data *D, size_t s, ulong t)
     536             : {
     537        1816 :   gp_hist *H = D->hist;
     538        1816 :   H->total = t;
     539        1816 :   H->size = s;
     540        1816 :   H->v = (gp_hist_cell*)pari_calloc(s * sizeof(gp_hist_cell));
     541        1816 : }
     542             : GEN
     543          14 : sd_histsize(const char *s, long flag)
     544             : {
     545          14 :   gp_hist *H = GP_DATA->hist;
     546          14 :   ulong n = H->size;
     547          14 :   GEN r = sd_ulong(s,flag,"histsize",&n, 1,
     548             :                      (LONG_MAX / sizeof(long)) - 1,NULL);
     549          14 :   if (n != H->size)
     550             :   {
     551          14 :     const ulong total = H->total;
     552             :     long g, h, k, kmin;
     553          14 :     gp_hist_cell *v = H->v, *w; /* v = old data, w = new one */
     554          14 :     size_t sv = H->size, sw;
     555             : 
     556          14 :     init_hist(GP_DATA, n, total);
     557          14 :     if (!total) return r;
     558             : 
     559          14 :     w = H->v;
     560          14 :     sw= H->size;
     561             :     /* copy relevant history entries */
     562          14 :     g     = (total-1) % sv;
     563          14 :     h = k = (total-1) % sw;
     564          14 :     kmin = k - minss(sw, sv);
     565          28 :     for ( ; k > kmin; k--, g--, h--)
     566             :     {
     567          14 :       w[h]   = v[g];
     568          14 :       v[g].z = NULL;
     569          14 :       if (!g) g = sv;
     570          14 :       if (!h) h = sw;
     571             :     }
     572             :     /* clean up */
     573          84 :     for ( ; v[g].z; g--)
     574             :     {
     575          70 :       gunclone(v[g].z);
     576          70 :       if (!g) g = sv;
     577             :     }
     578          14 :     pari_free((void*)v);
     579             :   }
     580          14 :   return r;
     581             : }
     582             : 
     583             : static void
     584           0 : TeX_define(const char *s, const char *def) {
     585           0 :   fprintf(pari_logfile, "\\ifx\\%s\\undefined\n  \\def\\%s{%s}\\fi\n", s,s,def);
     586           0 : }
     587             : static void
     588           0 : TeX_define2(const char *s, const char *def) {
     589           0 :   fprintf(pari_logfile, "\\ifx\\%s\\undefined\n  \\def\\%s#1#2{%s}\\fi\n", s,s,def);
     590           0 : }
     591             : 
     592             : static FILE *
     593           0 : open_logfile(const char *s) {
     594           0 :   FILE *log = fopen(s, "a");
     595           0 :   if (!log) pari_err_FILE("logfile",s);
     596           0 :   setbuf(log,(char *)NULL);
     597           0 :   return log;
     598             : }
     599             : 
     600             : GEN
     601           0 : sd_log(const char *v, long flag)
     602             : {
     603           0 :   const char *msg[] = {
     604             :       "(off)",
     605             :       "(on)",
     606             :       "(on with colors)",
     607             :       "(TeX output)", NULL
     608             :   };
     609           0 :   ulong s = pari_logstyle;
     610           0 :   GEN res = sd_ulong(v,flag,"log", &s, 0, 3, msg);
     611             : 
     612           0 :   if (!s != !pari_logstyle) /* Compare converts to boolean */
     613             :   { /* toggled LOG */
     614           0 :     if (pari_logstyle)
     615             :     { /* close log */
     616           0 :       if (flag == d_ACKNOWLEDGE)
     617           0 :         pari_printf("   [logfile was \"%s\"]\n", current_logfile);
     618           0 :       if (pari_logfile) { fclose(pari_logfile); pari_logfile = NULL; }
     619             :     }
     620             :     else
     621             :     {
     622           0 :       pari_logfile = open_logfile(current_logfile);
     623           0 :       if (flag == d_ACKNOWLEDGE)
     624           0 :         pari_printf("   [logfile is \"%s\"]\n", current_logfile);
     625           0 :       else if (flag == d_INITRC)
     626           0 :         pari_printf("Logging to %s\n", current_logfile);
     627             :     }
     628             :   }
     629           0 :   if (pari_logfile && s != pari_logstyle && s == logstyle_TeX)
     630             :   {
     631           0 :     TeX_define("PARIbreak",
     632             :                "\\hskip 0pt plus \\hsize\\relax\\discretionary{}{}{}");
     633           0 :     TeX_define("PARIpromptSTART", "\\vskip\\medskipamount\\bgroup\\bf");
     634           0 :     TeX_define("PARIpromptEND", "\\egroup\\bgroup\\tt");
     635           0 :     TeX_define("PARIinputEND", "\\egroup");
     636           0 :     TeX_define2("PARIout",
     637             :                 "\\vskip\\smallskipamount$\\displaystyle{\\tt\\%#1} = #2$");
     638             :   }
     639             :   /* don't record new value until we are sure everything is fine */
     640           0 :   pari_logstyle = s; return res;
     641             : }
     642             : 
     643             : GEN
     644           0 : sd_TeXstyle(const char *v, long flag)
     645             : {
     646           0 :   const char *msg[] = { "(bits 0x2/0x4 control output of \\left/\\PARIbreak)",
     647             :                         NULL };
     648           0 :   ulong n = GP_DATA->fmt->TeXstyle;
     649           0 :   GEN z = sd_ulong(v,flag,"TeXstyle", &n, 0, 7, msg);
     650           0 :   GP_DATA->fmt->TeXstyle = n; return z;
     651             : }
     652             : 
     653             : GEN
     654           7 : sd_nbthreads(const char *v, long flag)
     655           7 : { return sd_ulong(v,flag,"nbthreads",&pari_mt_nbthreads, 1,LONG_MAX,NULL); }
     656             : 
     657             : GEN
     658           0 : sd_output(const char *v, long flag)
     659             : {
     660           0 :   const char *msg[] = {"(raw)", "(prettymatrix)", "(prettyprint)",
     661             :                  "(external prettyprint)", NULL};
     662           0 :   ulong n = GP_DATA->fmt->prettyp;
     663           0 :   GEN z = sd_ulong(v,flag,"output", &n, 0,3,msg);
     664           0 :   GP_DATA->fmt->prettyp = n;
     665           0 :   GP_DATA->fmt->sp = (n != f_RAW);
     666           0 :   return z;
     667             : }
     668             : 
     669             : GEN
     670           0 : sd_parisizemax(const char *v, long flag)
     671             : {
     672           0 :   ulong size = pari_mainstack->vsize, n = size;
     673           0 :   GEN r = sd_ulong(v,flag,"parisizemax",&n, 0,LONG_MAX,NULL);
     674           0 :   if (n != size) {
     675           0 :     if (flag == d_INITRC)
     676           0 :       paristack_setsize(pari_mainstack->rsize, n);
     677             :     else
     678           0 :       parivstack_resize(n);
     679             :   }
     680           0 :   return r;
     681             : }
     682             : 
     683             : GEN
     684         412 : sd_parisize(const char *v, long flag)
     685             : {
     686         412 :   ulong rsize = pari_mainstack->rsize, n = rsize;
     687         412 :   GEN r = sd_ulong(v,flag,"parisize",&n, 10000,LONG_MAX,NULL);
     688         410 :   if (n != rsize) {
     689         403 :     if (flag == d_INITRC)
     690           0 :       paristack_setsize(n, pari_mainstack->vsize);
     691             :     else
     692         403 :       paristack_newrsize(n);
     693             :   }
     694           7 :   return r;
     695             : }
     696             : 
     697             : GEN
     698           0 : sd_threadsizemax(const char *v, long flag)
     699             : {
     700           0 :   ulong size = GP_DATA->threadsizemax, n = size;
     701           0 :   GEN r = sd_ulong(v,flag,"threadsizemax",&n, 0,LONG_MAX,NULL);
     702           0 :   if (n != size)
     703           0 :     GP_DATA->threadsizemax = n;
     704           0 :   return r;
     705             : }
     706             : 
     707             : GEN
     708           0 : sd_threadsize(const char *v, long flag)
     709             : {
     710           0 :   ulong size = GP_DATA->threadsize, n = size;
     711           0 :   GEN r = sd_ulong(v,flag,"threadsize",&n, 0,LONG_MAX,NULL);
     712           0 :   if (n != size)
     713           0 :     GP_DATA->threadsize = n;
     714           0 :   return r;
     715             : }
     716             : 
     717             : GEN
     718          14 : sd_primelimit(const char *v, long flag)
     719          14 : { return sd_ulong(v,flag,"primelimit",&(GP_DATA->primelimit),
     720             :                   0,2*(ulong)(LONG_MAX-1024) + 1,NULL); }
     721             : 
     722             : GEN
     723           0 : sd_simplify(const char *v, long flag)
     724           0 : { return sd_toggle(v,flag,"simplify", &(GP_DATA->simplify)); }
     725             : 
     726             : GEN
     727           0 : sd_strictmatch(const char *v, long flag)
     728           0 : { return sd_toggle(v,flag,"strictmatch", &(GP_DATA->strictmatch)); }
     729             : 
     730             : GEN
     731           7 : sd_strictargs(const char *v, long flag)
     732           7 : { return sd_toggle(v,flag,"strictargs", &(GP_DATA->strictargs)); }
     733             : 
     734             : GEN
     735           4 : sd_string(const char *v, long flag, const char *s, char **pstr)
     736             : {
     737           4 :   char *old = *pstr;
     738           4 :   if (v)
     739             :   {
     740           4 :     char *str, *ev = path_expand(v);
     741           4 :     long l = strlen(ev) + 256;
     742           4 :     str = (char *) pari_malloc(l);
     743           4 :     strftime_expand(ev,str, l-1); pari_free(ev);
     744           4 :     if (GP_DATA->secure)
     745             :     {
     746           0 :       char *msg=pari_sprintf("[secure mode]: About to change %s to '%s'",s,str);
     747           0 :       pari_ask_confirm(msg);
     748           0 :       pari_free(msg);
     749             :     }
     750           4 :     if (old) pari_free(old);
     751           4 :     *pstr = old = pari_strdup(str);
     752           4 :     pari_free(str);
     753             :   }
     754           0 :   else if (!old) old = (char*)"<undefined>";
     755           4 :   if (flag == d_RETURN) return strtoGENstr(old);
     756           4 :   if (flag == d_ACKNOWLEDGE) pari_printf("   %s = \"%s\"\n",s,old);
     757           4 :   return gnil;
     758             : }
     759             : 
     760             : GEN
     761           0 : sd_logfile(const char *v, long flag)
     762             : {
     763           0 :   GEN r = sd_string(v, flag, "logfile", &current_logfile);
     764           0 :   if (v && pari_logfile)
     765             :   {
     766           0 :     FILE *log = open_logfile(current_logfile);
     767           0 :     fclose(pari_logfile); pari_logfile = log;
     768             :   }
     769           0 :   return r;
     770             : }
     771             : 
     772             : GEN
     773           0 : sd_factor_add_primes(const char *v, long flag)
     774           0 : { return sd_toggle(v,flag,"factor_add_primes", &factor_add_primes); }
     775             : 
     776             : GEN
     777           0 : sd_factor_proven(const char *v, long flag)
     778           0 : { return sd_toggle(v,flag,"factor_proven", &factor_proven); }
     779             : 
     780             : GEN
     781          28 : sd_new_galois_format(const char *v, long flag)
     782          28 : { return sd_toggle(v,flag,"new_galois_format", &new_galois_format); }
     783             : 
     784             : GEN
     785          40 : sd_datadir(const char *v, long flag)
     786             : {
     787             :   const char *str;
     788          40 :   if (v)
     789             :   {
     790           7 :     mt_broadcast(snm_closure(is_entry("default"),
     791             :                  mkvec2(strtoGENstr("datadir"), strtoGENstr(v))));
     792           7 :     if (pari_datadir) pari_free(pari_datadir);
     793           7 :     pari_datadir = path_expand(v);
     794             :   }
     795          40 :   str = pari_datadir? pari_datadir: "none";
     796          40 :   if (flag == d_RETURN) return strtoGENstr(str);
     797           7 :   if (flag == d_ACKNOWLEDGE)
     798           0 :     pari_printf("   datadir = \"%s\"\n", str);
     799           7 :   return gnil;
     800             : }
     801             : 
     802             : static GEN
     803           0 : sd_PATH(const char *v, long flag, const char* s, gp_path *p)
     804             : {
     805           0 :   if (v)
     806             :   {
     807           0 :     mt_broadcast(snm_closure(is_entry("default"),
     808             :                  mkvec2(strtoGENstr(s), strtoGENstr(v))));
     809           0 :     pari_free((void*)p->PATH);
     810           0 :     p->PATH = pari_strdup(v);
     811           0 :     if (flag == d_INITRC) return gnil;
     812           0 :     expand_path(p);
     813             :   }
     814           0 :   if (flag == d_RETURN) return strtoGENstr(p->PATH);
     815           0 :   if (flag == d_ACKNOWLEDGE)
     816           0 :     pari_printf("   %s = \"%s\"\n", s, p->PATH);
     817           0 :   return gnil;
     818             : }
     819             : GEN
     820           0 : sd_path(const char *v, long flag)
     821           0 : { return sd_PATH(v, flag, "path", GP_DATA->path); }
     822             : GEN
     823           0 : sd_sopath(char *v, int flag)
     824           0 : { return sd_PATH(v, flag, "sopath", GP_DATA->sopath); }
     825             : 
     826             : static const char *DFT_PRETTYPRINTER = "tex2mail -TeX -noindent -ragged -by_par";
     827             : GEN
     828           0 : sd_prettyprinter(const char *v, long flag)
     829             : {
     830           0 :   gp_pp *pp = GP_DATA->pp;
     831           0 :   if (v && !(GP_DATA->flags & gpd_TEXMACS))
     832             :   {
     833           0 :     char *old = pp->cmd;
     834           0 :     int cancel = (!strcmp(v,"no"));
     835             : 
     836           0 :     if (GP_DATA->secure)
     837           0 :       pari_err(e_MISC,"[secure mode]: can't modify 'prettyprinter' default (to %s)",v);
     838           0 :     if (!strcmp(v,"yes")) v = DFT_PRETTYPRINTER;
     839           0 :     if (old && strcmp(old,v) && pp->file)
     840             :     {
     841             :       pariFILE *f;
     842           0 :       if (cancel) f = NULL;
     843             :       else
     844             :       {
     845           0 :         f = try_pipe(v, mf_OUT);
     846           0 :         if (!f)
     847             :         {
     848           0 :           pari_warn(warner,"broken prettyprinter: '%s'",v);
     849           0 :           return gnil;
     850             :         }
     851             :       }
     852           0 :       pari_fclose(pp->file);
     853           0 :       pp->file = f;
     854             :     }
     855           0 :     pp->cmd = cancel? NULL: pari_strdup(v);
     856           0 :     if (old) pari_free(old);
     857           0 :     if (flag == d_INITRC) return gnil;
     858             :   }
     859           0 :   if (flag == d_RETURN)
     860           0 :     return strtoGENstr(pp->cmd? pp->cmd: "");
     861           0 :   if (flag == d_ACKNOWLEDGE)
     862           0 :     pari_printf("   prettyprinter = \"%s\"\n",pp->cmd? pp->cmd: "");
     863           0 :   return gnil;
     864             : }
     865             : 
     866             : /* compare entrees s1 s2 according to the attached function name */
     867             : static int
     868           0 : compare_name(const void *s1, const void *s2) {
     869           0 :   entree *e1 = *(entree**)s1, *e2 = *(entree**)s2;
     870           0 :   return strcmp(e1->name, e2->name);
     871             : }
     872             : static void
     873           0 : defaults_list(pari_stack *s)
     874             : {
     875             :   entree *ep;
     876             :   long i;
     877           0 :   for (i = 0; i < functions_tblsz; i++)
     878           0 :     for (ep = defaults_hash[i]; ep; ep = ep->next) pari_stack_pushp(s, ep);
     879           0 : }
     880             : /* ep attached to function f of arity 2. Call f(v,flag) */
     881             : static GEN
     882        1623 : call_f2(entree *ep, const char *v, long flag)
     883        1623 : { return ((GEN (*)(const char*,long))ep->value)(v, flag); }
     884             : GEN
     885        1623 : setdefault(const char *s, const char *v, long flag)
     886             : {
     887             :   entree *ep;
     888        1623 :   if (!s)
     889             :   { /* list all defaults */
     890             :     pari_stack st;
     891             :     entree **L;
     892             :     long i;
     893           0 :     pari_stack_init(&st, sizeof(*L), (void**)&L);
     894           0 :     defaults_list(&st);
     895           0 :     qsort (L, st.n, sizeof(*L), compare_name);
     896           0 :     for (i = 0; i < st.n; i++) (void)call_f2(L[i], NULL, d_ACKNOWLEDGE);
     897           0 :     pari_stack_delete(&st);
     898           0 :     return gnil;
     899             :   }
     900        1623 :   ep = pari_is_default(s);
     901        1623 :   if (!ep)
     902             :   {
     903           0 :     pari_err(e_MISC,"unknown default: %s",s);
     904             :     return NULL; /* LCOV_EXCL_LINE */
     905             :   }
     906        1623 :   return call_f2(ep, v, flag);
     907             : }
     908             : 
     909             : GEN
     910        1605 : default0(const char *a, const char *b) { return setdefault(a,b, b? d_SILENT: d_RETURN); }
     911             : 
     912             : /********************************************************************/
     913             : /*                                                                  */
     914             : /*                     INITIALIZE GP_DATA                           */
     915             : /*                                                                  */
     916             : /********************************************************************/
     917             : /* initialize path */
     918             : static void
     919        3604 : init_path(gp_path *path, const char *v)
     920             : {
     921        3604 :   path->PATH = pari_strdup(v);
     922        3604 :   path->dirs = NULL;
     923        3604 : }
     924             : 
     925             : /* initialize D->fmt */
     926             : static void
     927        1802 : init_fmt(gp_data *D)
     928             : {
     929             : #ifdef LONG_IS_64BIT
     930             :   static pariout_t DFLT_OUTPUT = { 'g', 38, 1, f_PRETTYMAT, 0 };
     931             : #else
     932             :   static pariout_t DFLT_OUTPUT = { 'g', 28, 1, f_PRETTYMAT, 0 };
     933             : #endif
     934        1802 :   D->fmt = &DFLT_OUTPUT;
     935        1802 : }
     936             : 
     937             : /* initialize D->pp */
     938             : static void
     939        1802 : init_pp(gp_data *D)
     940             : {
     941        1802 :   gp_pp *p = D->pp;
     942        1802 :   p->cmd = pari_strdup(DFT_PRETTYPRINTER);
     943        1802 :   p->file = NULL;
     944        1802 : }
     945             : 
     946             : static char *
     947        1802 : init_help(void)
     948             : {
     949        1802 :   char *h = os_getenv("GPHELP");
     950        1802 :   if (!h) h = (char*)paricfg_gphelp;
     951             : #ifdef _WIN32
     952             :   win32_set_pdf_viewer();
     953             : #endif
     954        1802 :   if (h) h = pari_strdup(h);
     955        1802 :   return h;
     956             : }
     957             : 
     958             : static void
     959        1802 : init_graphs(gp_data *D)
     960             : {
     961        1802 :   const char *cols[] = { "",
     962             :     "white","black","blue","violetred","red","green","grey","gainsboro"
     963             :   };
     964        1802 :   const long N = 8;
     965        1802 :   GEN c = cgetalloc(t_VECSMALL, 3), s;
     966             :   long i;
     967        1802 :   c[1] = 4;
     968        1802 :   c[2] = 5;
     969        1802 :   D->graphcolors = c;
     970        1802 :   c = (GEN)pari_malloc((N+1 + 4*N)*sizeof(long));
     971        1802 :   c[0] = evaltyp(t_VEC)|evallg(N+1);
     972       16218 :   for (i = 1, s = c+N+1; i <= N; i++, s += 4)
     973             :   {
     974       14416 :     GEN lp = s;
     975       14416 :     lp[0] = evaltyp(t_STR)|evallg(4);
     976       14416 :     strcpy(GSTR(lp), cols[i]);
     977       14416 :     gel(c,i) = lp;
     978             :   }
     979        1802 :   D->colormap = c;
     980        1802 : }
     981             : 
     982             : gp_data *
     983        1802 : default_gp_data(void)
     984             : {
     985             :   static gp_data __GPDATA, *D = &__GPDATA;
     986             :   static gp_hist __HIST;
     987             :   static gp_pp   __PP;
     988             :   static gp_path __PATH, __SOPATH;
     989             :   static pari_timer __T, __Tw;
     990             : 
     991        1802 :   D->flags       = 0;
     992        1802 :   D->primelimit  = 500000;
     993             : 
     994             :   /* GP-specific */
     995        1802 :   D->breakloop   = 1;
     996        1802 :   D->echo        = 0;
     997        1802 :   D->lim_lines   = 0;
     998        1802 :   D->linewrap    = 0;
     999        1802 :   D->recover     = 1;
    1000        1802 :   D->chrono      = 0;
    1001             : 
    1002        1802 :   D->strictargs  = 0;
    1003        1802 :   D->strictmatch = 1;
    1004        1802 :   D->simplify    = 1;
    1005        1802 :   D->secure      = 0;
    1006        1802 :   D->use_readline= 0;
    1007        1802 :   D->T    = &__T;
    1008        1802 :   D->Tw   = &__Tw;
    1009        1802 :   D->hist = &__HIST;
    1010        1802 :   D->pp   = &__PP;
    1011        1802 :   D->path = &__PATH;
    1012        1802 :   D->sopath=&__SOPATH;
    1013        1802 :   init_fmt(D);
    1014        1802 :   init_hist(D, 5000, 0);
    1015        1802 :   init_path(D->path, pari_default_path());
    1016        1802 :   init_path(D->sopath, "");
    1017        1802 :   init_pp(D);
    1018        1802 :   init_graphs(D);
    1019        1802 :   D->plothsizes = cgetalloc(t_VECSMALL, 1);
    1020        1802 :   D->prompt_comment = (char*)"comment> ";
    1021        1802 :   D->prompt = pari_strdup("? ");
    1022        1802 :   D->prompt_cont = pari_strdup("");
    1023        1802 :   D->help = init_help();
    1024        1802 :   D->readline_state = DO_ARGS_COMPLETE;
    1025        1802 :   D->histfile = NULL;
    1026        1802 :   return D;
    1027             : }

Generated by: LCOV version 1.13