genversion.sh: rename GIT_HEAD_ID to PIPLIB_HEAD and drop "piplib-" prefix
[piplib.git] / source / maind.c
blob26cec0a6126a63d756d3a608300dd5a30d358b50
1 /******************************************************************************
2 * PIP : Parametric Integer Programming *
3 ******************************************************************************
4 * maind.h *
5 ******************************************************************************
6 * *
7 * Copyright Paul Feautrier, 1988-2005 *
8 * *
9 * This library is free software; you can redistribute it and/or modify it *
10 * under the terms of the GNU Lesser General Public License as published by *
11 * the Free Software Foundation; either version 2.1 of the License, or (at *
12 * your option) any later 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 Lesser General Public License *
20 * along with this library; if not, write to the Free Software Foundation, *
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "pip.h"
38 #include "version.h"
40 #ifdef UNIX
41 #include <sys/times.h>
42 struct tms chrono;
43 #endif
45 static char version[] = "Version "PIPLIB_HEAD"\n";
48 extern long int cross_product, limit ;
49 extern int allocation, comptage, verbose ;
50 extern FILE * dump ;
51 extern int compa_count ;
52 extern int deepest_cut;
54 void balance(FILE *foo, FILE *bar)
56 int level = 0;
57 int c;
58 while((c = dgetc(foo)) != EOF)
60 switch(c)
61 {case '(' : level++; break;
62 case ')' : if(--level == 0) return;
64 putc(c, bar);
68 void escape(FILE *foo, FILE *bar, int level)
69 {int c;
70 while((c = dgetc(foo)) != EOF)
71 switch(c)
72 {case '(' : level ++; break;
73 case ')' : if(--level == 0)
74 { fprintf(bar, "\nSyntax error\n)\n");
75 return;
80 char * getenv();
82 int main(int argc, char *argv[])
84 FILE *in, *out;
85 Tableau *ineq, *context, *ctxt;
86 int nvar, nparm, ni, nc, bigparm;
87 int nq; char * g;
88 int simple = 0;
89 struct high_water_mark hq;
90 int c, non_vide;
91 int p, q, xq;
92 long temps;
93 char *date;
94 Entier x ;
95 #if defined(LINEAR_VALUE_IS_MP)
96 mpz_init(x);
97 #else
98 Entier i;
99 #endif
101 #if defined(LINEAR_VALUE_IS_MP)
102 mpz_init_set_si(UN, 1);
103 mpz_init_set_si(ZERO, 0);
104 #else
105 UN = VAL_UN ;
106 ZERO = VAL_ZERO ;
107 #endif
109 in = stdin; out = stdout;
110 p = 1;
111 if(argc > 1)
112 { if(strcmp(argv[1], "-s") == 0)
113 { verbose = -1;
114 p = 2;
116 /* the number of 'v' in the verbose option control the amount of debug
117 * information generated by Pip.
119 else
120 if(strncmp(argv[1], "-v", 2) == 0)
121 { verbose = 1;
122 g = argv[1]+2;
123 while(*g++ == 'v') verbose++;
125 p = 2;
126 dump = pip_create_dump_file();
127 if (!dump)
128 verbose = 0;
130 if(argc>p && strcmp(argv[p], "-d") == 0)
131 { deepest_cut = 1;
132 p++;
136 if(verbose >= 0) fprintf(stderr, version);
137 if(argc > p)
138 { if(strcmp(argv[p], "-z") == 0)
139 { simple = 1;
140 p++;
143 in = fopen(argv[p], "r");
144 if(in == NULL)
145 { fprintf(stderr, "%s unaccessible\n", argv[p]);
146 exit(1);
149 p++;
150 if(argc>p)
151 { out = fopen(argv[p], "w");
152 if(out == NULL)
153 { fprintf(stderr, "%s unaccessible\n", argv[p]);
154 exit(2);
158 limit = 0L;
159 p++;
160 if(argc > p) limit = atol(argv[p]);
161 sol_init();
162 tab_init();
163 while((c = dgetc(in)) != EOF)
164 {if(c != '(') continue;
165 fprintf(out, "(");
166 balance(in, out);
167 #if defined(LINEAR_VALUE_IS_MP)
168 if(dscanf(in, x) < 0){escape(in, out, 1); continue;}
169 else
170 nvar = mpz_get_si(x);
171 if(dscanf(in, x) < 0){escape(in, out, 1); continue;}
172 else
173 nparm = mpz_get_si(x);
174 if(dscanf(in, x) < 0){escape(in, out, 1); continue;}
175 else
176 ni = mpz_get_si(x);
177 if(dscanf(in, x) < 0){escape(in, out, 1); continue;}
178 else
179 nc = mpz_get_si(x);
180 if(dscanf(in, x) < 0){escape(in, out, 1); continue;}
181 else
182 bigparm = mpz_get_si(x);
183 if(dscanf(in, x) < 0){escape(in, out, 1); continue;}
184 else
185 nq = mpz_get_si(x);
186 #else
187 if(dscanf(in, &x) < 0){escape(in, out, 1); continue;}
188 else
189 nvar = (int) x;
190 if(dscanf(in, &x) < 0){escape(in, out, 1); continue;}
191 else
192 nparm = (int) x;
193 if(dscanf(in, &x) < 0){escape(in, out, 1); continue;}
194 else
195 ni = (int) x;
196 if(dscanf(in, &x) < 0){escape(in, out, 1); continue;}
197 else
198 nc = (int) x;
199 if(dscanf(in, &x) < 0){escape(in, out, 1); continue;}
200 else
201 bigparm = (int) x;
202 if(dscanf(in, &x) < 0){escape(in, out, 1); continue;}
203 else
204 nq = (int) x;
205 #endif
207 if(verbose > 0) {fprintf(dump, "%d %d %d %d %d %d\n",nvar, nparm, ni, nc,
208 bigparm, nq);
209 fflush(dump);
211 cross_product = 0;
212 hq = tab_hwm();
213 if(verbose > 0) {fprintf(dump, "hwm %x\n", g);
214 fflush(dump);
216 ineq = tab_get(in, ni, nvar+nparm+1, nvar);
217 if (nq)
218 tab_simplify(ineq, nvar);
219 if(ineq == NULL){escape(in, out, 2); continue;}
220 context = tab_get(in, nc, nparm+1, 0);
221 if (nq)
222 tab_simplify(context, nparm);
223 if(context == 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, nparm, 0, nc, 0, -1, TRAITER_INT);
229 non_vide = is_not_Nil(p);
230 sol_reset(p);
232 else non_vide = Pip_True;
233 if(non_vide) {
234 compa_count = 0;
235 traiter(ineq, context, nvar, nparm, ni, nc, bigparm, nq ? TRAITER_INT : 0);
236 if (verbose > 0) {
237 fprintf(dump, "det: ");
238 #if defined(LINEAR_VALUE_IS_MP)
239 mpz_out_str(dump, 10, ineq->determinant);
240 #else
241 for (i=0; i<ineq->l_determinant; i++) {
242 fprintf(dump, FORMAT, ineq->determinant[i]);
243 fprintf(dump, " ");
245 #endif
246 fprintf(dump, "\n");
248 fputs(")\n",out);
249 if(simple) sol_simplify(xq);
250 q = sol_hwm();
251 while((xq = sol_edit(out, xq)) != q);
252 sol_reset(p);
254 else fprintf(out, "void\n");
255 tab_reset(hq);
256 if(verbose > 0) fflush(dump);
257 /* add a right parenthesis in order to keep the output in balance */
258 fprintf(out, ")\n");
259 fflush(out);
260 if(verbose >= 0)
261 fprintf(stderr,"cross : %ld, alloc : %d, compa : %d\n\r",
262 cross_product, allocation, compa_count);
263 comptage++;
265 #ifdef UNIX
266 times(& chrono);
267 fprintf(stderr, "n %d u %d''' s %d'''\r\n",
268 comptage, chrono.tms_utime, chrono.tms_stime);
269 #endif
271 #if defined(LINEAR_VALUE_IS_MP)
272 mpz_clear(x);
273 #endif
274 pip_close();
275 exit(0);