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

Generated by: LCOV version 1.11