File: util.c

package info (click to toggle)
gp2c 0.0.7pl3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,728 kB
  • sloc: ansic: 7,730; sh: 1,420; lex: 336; yacc: 208; makefile: 143
file content (237 lines) | stat: -rw-r--r-- 4,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Copyright (C) 2000-2003  The PARI group.

This file is part of the GP2C package.

PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.

Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "header.h"
int linecount;
static void
msgprefix(int n)
{
  if (n>0)
    fprintf(stderr,"%s:%d: ",nameparse,tree[n].lineno);
  else
  {
    switch(n)
    {
    case err_func:
      fprintf(stderr,"%s:%s: ",nameparse,currfunc==-1?"toplevel":lfunc[currfunc].gpname);
      break;
    case err_parse:
      fprintf(stderr,"%s:%d: ",nameparse,linecount);
      break;
    case err_desc:
      fprintf(stderr,"func.dsc:<loading>: ");
      break;
    }
  }
}

void die(int n, const char *format, ...)
{
  va_list ap;
  va_start(ap, format);
  fprintf(stderr,"Error:");
  msgprefix(n);
  vfprintf(stderr,format,ap);
  fprintf(stderr,"\n");
  printnode(stderr,n);
  fprintf(stderr,"\n");
  exit(1);
}

extern int do_warning;

void warning(int n, const char *format, ...)
{
  va_list ap;
  va_start(ap, format);
  if (do_warning)
  {
    fprintf(stderr,"Warning:");
    msgprefix(n);
    vfprintf(stderr,format,ap);
    fprintf(stderr,"\n");
    printnode(stderr,n);
    fprintf(stderr,"\n");
  }
  va_end(ap);
}

int listtostack(int n, Ffunc f, int *stack, int nbmax, const char *error,int nerr)
{
  int x,i,nb;
  if (n==-1) return 0;
  for(x=n,i=0;tree[x].f==f && i<nbmax;x=tree[x].x,i++);
  if (i==nbmax)
    die(nerr,"Too many args for `%s'",error);
  nb=i+1;
  for(x=n;i>0;stack[i]=tree[x].y,x=tree[x].x,i--);
  stack[0]=x;
  return nb;
}
int listcalltostack(int n, int fnum, int *stack, int nbmax, const char *error,int nerr)
{
  int x=n, i=0, nb;
  if (n==-1) return 0;
  while(1)
  {
    int xx=tree[x].x;
    int xy=tree[x].y;
    if (tree[x].f!=Ffunction || xx!=fnum) break;
    x=tree[xy].x;
    i++;
    if (i>=nbmax)
      die(nerr,"Too many args for `%s'",error);
  }
  nb=i+1;
  for(x=n;i>0;i--)
  {
    int y=tree[x].y;
    x=tree[y].x;
    stack[i]=tree[y].y;
  }
  stack[0]=x;
  return nb;
}
int listtostackparent(int n, Ffunc f, int *stack, int nbmax, const char *error,int nerr)
{
  int x,i,nb;
  if (n==-1) return 0;
  nbmax--;
  for(x=n,i=0;tree[x].f==f && i<nbmax;x=tree[x].x,i++);
  if (i==nbmax)
    die(nerr,"Too many args for `%s'",error);
  nb=i--;
  for(x=n;i>=0;stack[i]=x,x=tree[x].x,i--);
  return nb;
}

int genlistargs(int n,int *stack,int min,int max)
{
  const char *name=value[tree[n].x].val.str;
  int y=tree[n].y;
  int nb=(y==GNOARG)?0:listtostack(y,Flistarg,stack,max,name,n);
  if(nb<min)
    die(n,"Too few args for `%s'",name);
  return nb;
}

void killlistarg(int n, int a)
{
  int x=tree[n].y, p;
  if (x==a)
    tree[n].y = GNOARG;
  else
  {
    for(p=n; tree[x].f==Flistarg; p=x, x=tree[x].x)
      if (tree[x].y==a)
        tree[x].y=GNOARG;
    if (x==a) tree[p].x=GNOARG;
  }
}

int genlistcats(int y,int *stack,int max)
{
  return listcalltostack(y,OPcat,stack,max,"concatenation",y);
}

PPproto
parseproto(char const **q, char *c)
{
  char  const *p=*q;
  long i;
  switch(*p)
  {
  case 0:
  case '\n':
    return PPend;
  case 'D':
    switch(p[1])
    {
    case 0:
      die(-1,"function has incomplete prototype");
    case 'G':
    case '&':
    case 'W':
    case 'V':
    case 'r':
    case 's':
    case 'I':
    case 'E':
    case 'n':
    case 'P':
      *c=p[1];
      *q=p+2;
      return PPdefault;
    default:
      for(i=0;*p && i<2;p++) i+=*p==',';
      if (i<2)
        die(-1,"function has incomplete prototype");
      *c=p[-2];
      *q=p;
      return PPdefaultmulti;
    }
    break;
  case 'C':
  case 'p':
  case 'P':
  case 'f':
    *c=*p;
    *q=p+1;
    return PPauto;
  case '&':
    *c='*';
    *q=p+1;
    return PPstd;
  case 'V':
    if (p[1]=='=')
    {
      if (p[2]!='G')
        die(-1,"function prototype is not supported");
      *c='=';
      p+=2;
    }
    else
      *c=*p;
    *q=p+1;
    return PPstd;
  case '*':
    *c=*p++;
    *q=p+1;
    return PPstar;
  case 's':
    if (p[1]=='*')
    {
      *c=*p++;
      *q=p+1;
      return PPstar;
    }
    /*fall through*/
  }
  *c=*p;
  *q=p+1;
  return PPstd;
}

char *xstrndup(const char *s, size_t n)
{
  char *t=(char*)malloc(n+1);
  size_t i;
  for(i=0;i<n;i++) t[i]=s[i];
  t[i]=0;
  return t;
}