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

Generated by: LCOV version 1.11