piplib 1.02
[piplib.git] / source / maind.c
blob4e90f3283a8e2374d6a71b76f73d6599498c8205
1 /******************************************************************************
2 * PIP : Parametric Integer Programming *
3 ******************************************************************************
4 * maind.h *
5 ******************************************************************************
6 * *
7 * Copyright Paul Feautrier, 1988, 1993, 1994, 1996, 2002 *
8 * *
9 * This is free software; you can redistribute it and/or modify it under the *
10 * terms of the GNU General Public License as published by the Free Software *
11 * Foundation; either version 2 of the License, or (at your option) any later *
12 * version. *
13 * *
14 * This software is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License along *
20 * with software; if not, write to the Free Software Foundation, Inc., *
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
22 * *
23 * Written by Paul Feautrier *
24 * *
25 ******************************************************************************/
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <stdlib.h>
32 #ifdef __TURBOC__
33 #include <dir.h>
34 #endif
35 #define min(x,y) ((x) < (y)? (x) : (y))
37 #include <piplib/piplib.h>
39 #ifdef UNIX
40 #include <sys/times.h>
41 struct tms chrono;
42 #endif
44 char version[]="Version E.2 $Revision: 1.3 $\n";
46 long int cross_product, limit;
47 int allocation, comptage;
48 int verbose = 0;
49 int profondeur = 0;
50 int compa_count;
52 FILE *dump = NULL;
53 char dump_name[] = "PipXXXXXX";
55 #define INLENGTH 1024
57 char inbuff[INLENGTH];
58 int inptr = 256;
59 int proviso = 0;
62 int dgetc(FILE *foo)
64 char *p;
65 if(inptr >= proviso)
66 {p = fgets(inbuff, INLENGTH, foo);
67 if(p == NULL) return EOF;
68 proviso = min(INLENGTH, strlen(inbuff));
69 inptr = 0;
70 if(verbose > 0) fprintf(dump, "-- %s", inbuff);
72 return inbuff[inptr++];
75 int dscanf(FILE *foo, char *format, Entier *val)
77 char * p;
78 int c;
79 for(;inptr < proviso; inptr++)
80 if(inbuff[inptr] != ' ' && inbuff[inptr] != '\n' && inbuff[inptr] != '\t')
81 break;
82 while(inptr >= proviso)
83 {p = fgets(inbuff, 256, foo);
84 if(p == NULL) return EOF;
85 proviso = strlen(inbuff);
86 if(verbose > 0) {
87 fprintf(dump, ".. %s", inbuff);
88 fflush(dump);
90 for(inptr = 0; inptr < proviso; inptr++)
91 if(inbuff[inptr] != ' '
92 && inbuff[inptr] != '\n'
93 && inbuff[inptr] != '\t') break;
95 if(sscanf(inbuff+inptr, FORMAT, val) != 1) return -1;
97 for(; inptr < proviso; inptr++)
98 if((c = inbuff[inptr]) != '-' && !isdigit(c)) break;
99 return 0;
102 void balance(FILE *foo, FILE *bar)
104 int level = 0;
105 int c;
106 while((c = dgetc(foo)) != EOF)
108 switch(c)
109 {case '(' : level++; break;
110 case ')' : if(--level == 0) return;
112 putc(c, bar);
116 void escape(FILE *foo, FILE *bar, int level)
117 {int c;
118 while((c = dgetc(foo)) != EOF)
119 switch(c)
120 {case '(' : level ++; break;
121 case ')' : if(--level == 0)
122 { fprintf(bar, "\nSyntax error\n)\n");
123 return;
128 char * getenv();
130 int main(int argc, char *argv[])
133 FILE *in, *out;
134 Tableau *ineq, *context, *ctxt;
135 int nvar, nparm, ni, nc, bigparm;
136 int nq; char * g;
137 int simple = 0;
138 struct high_water_mark hq;
139 int c, non_vide;
140 int p, q, xq;
141 long temps;
142 char *date;
143 Entier D, x;
145 in = stdin; out = stdout;
146 p = 1;
147 if(argc > 1)
148 if(strcmp(argv[1], "-s") == 0)
149 {verbose = -1;
150 p = 2;
152 else if(strcmp(argv[1], "-v") == 0)
153 {verbose = 1;
154 p = 2;
155 g = getenv("DEBUG");
156 if(g && *g)
157 {dump = fopen(g, "w");
158 if(dump == NULL)
159 {fprintf(stderr, "%s unaccessible\n", g);
160 verbose = 0;
163 else
165 mkstemp(dump_name);
166 dump = fopen(dump_name, "w");
169 if(verbose >= 0) fprintf(stderr, version);
170 if(argc > p)
171 if(strcmp(argv[p], "-z") == 0) {
172 simple = 1;
173 p++;
175 if(argc>p)
176 {in = fopen(argv[p], "r");
177 if(in == NULL)
178 {fprintf(stderr, "%s unaccessible\n", argv[p]);
179 exit(1);
182 p++;
183 if(argc>p)
184 {out = fopen(argv[p], "w");
185 if(out == NULL)
186 {fprintf(stderr, "%s unaccessible\n", argv[p]);
187 exit(2);
190 limit = 0L;
191 p++;
192 if(argc > p) limit = atol(argv[p]);
193 sol_init();
194 tab_init();
195 while((c = dgetc(in)) != EOF)
196 {if(c != '(') continue;
197 fprintf(out, "(");
198 balance(in, out);
199 if(dscanf(in, FORMAT, &x) < 0){escape(in, out, 1); continue;}
200 else nvar = (int) x;
201 if(dscanf(in, FORMAT, &x) < 0){escape(in, out, 1); continue; }
202 else nparm = (int) x;
203 if(dscanf(in, FORMAT, &x) < 0){escape(in, out, 1); continue; }
204 else ni = (int) x;
205 if(dscanf(in, FORMAT, &x) < 0){escape(in, out, 1); continue; }
206 else nc = (int) x;
207 if(dscanf(in, FORMAT, &x) < 0){escape(in, out, 1); continue; }
208 else bigparm = (int) x;
209 if(dscanf(in, FORMAT, &x) < 0){escape(in, out, 1); continue; }
210 else nq = (int) x;
211 if(verbose > 0) {fprintf(dump, "%d %d %d %d %d %d\n",nvar, nparm, ni, nc,
212 bigparm, nq);
213 fflush(dump);
215 cross_product = 0;
216 hq = tab_hwm();
217 if(verbose > 0) {fprintf(dump, "hwm %x\n", g);
218 fflush(dump);
220 ineq = tab_get(in, ni, nvar+nparm+1, nvar);
221 if(ineq == NULL){escape(in, out, 2); continue;}
222 context = tab_get(in, nc, nparm+1, 0);
223 if(ineq == NULL){escape(in, out, 2); continue;}
224 xq = p = sol_hwm();
225 /* verification de la non-vacuite' du contexte */
226 if(nc)
227 {ctxt = expanser(context, nparm, nc, nparm+1, nparm, 0, 0);
228 traiter(ctxt, NULL, True, UN, nparm, 0, nc, 0, -1);
229 non_vide = is_not_Nil(p);
230 sol_reset(p);
232 else non_vide = True;
233 if(non_vide) {
234 compa_count = 0;
235 D = traiter(ineq, context, nq, UN, nvar, nparm, ni, nc, bigparm);
236 putc(' ',out);
237 fprintf(out, FORMAT, D);
238 fputs(" )",out);
239 if(simple) sol_simplify(xq);
240 q = sol_hwm();
241 while((xq = sol_edit(out, xq)) != q);
242 sol_reset(p);
244 else fprintf(out, "void\n");
245 tab_reset(hq);
246 if(verbose > 0) fflush(dump);
247 fprintf(out, ")\n");
248 fflush(out);
249 if(verbose >= 0) fprintf(stderr,"cross : %ld, alloc : %d, compa : %d\n\r",
250 cross_product, allocation, compa_count);
251 comptage++;
253 #ifdef UNIX
254 times(& chrono);
255 fprintf(stderr, "n %d u %d''' s %d'''\r\n",
256 comptage, chrono.tms_utime, chrono.tms_stime);
257 #endif
258 exit(0);