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

Generated by: LCOV version 1.11