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