File: repwhere.c

package info (click to toggle)
libamplsolver 0~20190702-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: ansic: 40,472; asm: 76; sh: 72; fortran: 51; makefile: 16
file content (477 lines) | stat: -rw-r--r-- 10,282 bytes parent folder | download | duplicates (4)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* The following Lucent copyright notice applies to report_where()
   and the original introuble* routines (moved here in 2011 from
   rops.c and rops2.c). */

/****************************************************************
Copyright (C) 1997 Lucent Technologies
All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/

/* The following AMPL copyright notice applies to material added in 2011,
   identified by #ifndef ASL_OLD_DERIV_CHECK . */

/*******************************************************************
Copyright (C) 2017 AMPL Optimization, Inc.; written by David M. Gay.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that the copyright notice and this permission notice and warranty
disclaimer appear in supporting documentation.

The author and AMPL Optimization, Inc. disclaim all warranties with
regard to this software, including all implied warranties of
merchantability and fitness.  In no event shall the author be liable
for any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether in an
action of contract, negligence or other tortious action, arising out
of or in connection with the use or performance of this software.
*******************************************************************/

#include "asl.h"
#include "errchk.h"

 void
report_where(ASL *asl)
{
	int i, j, k, k1;
	static const char *what[2] = { "constraint", "objective" };
	static const char *nfmt[2] = { "%d: ", "function: " };
	char *b, buf[512];
	FILE *f;

	fflush(stdout);
	need_nl = 0;
	fprintf(Stderr, "Error evaluating ");

#define next_line fgets(buf,sizeof(buf),f)

	if ((i = cv_index)) {
		strcpy(stub_end, ".fix");
		j = 0;
		if ((f = fopen(filename, "r"))) {
			for(;;) {
				if (!next_line)
					goto eof;
				for(b = buf; *b; b++)
					if (*b == '=') {
						while(++j < i)
							if (!next_line)
								goto eof;
						b = buf;
						while(*b && *b != '=')
							b++;
						if (*b != '=' || b < buf + 2)
							j = 0;
						else
							b[-1] = 0;
						goto eof;
						}
				}
 eof:
			fclose(f);
			}
		if (j == i)
			fprintf(Stderr, "var %s: ", buf);
		else
			fprintf(Stderr, "\"var =\" definition %d: ", i);
		goto ret;
		}

	k = k1 = 0;
	if ((i = co_index) < 0) {
		k = 1;
		i = asl->i.n_con0 -i - 1;
		if (n_obj <= 1)
			k1 = 1;
		}
	fprintf(Stderr, "%s ", what[k]);
	if (maxrownamelen) {
		strcpy(stub_end, ".row");
		if ((f = fopen(filename, "r"))) {
			for(j = 0; j <= i; j++)
				if (!next_line)
					break;
			fclose(f);
			if (j > i) {
				for(b = buf; *b; b++)
					if (*b == '\n') {
						*b = 0;
						break;
						}
				fprintf(Stderr, "%s: ", buf);
				goto ret;
				}
			}
		}
	fprintf(Stderr, nfmt[k1], i + 1);
 ret:
	errno = 0;	/* in case it was set by fopen */
	fflush(Stderr);
	}

 static void
jmp_check(Jmp_buf *J, int jv)
{
	if (J)
		longjmp(J->jb, jv);
	}

 static void
Errprint(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
#ifndef NO_PERROR
	if (errno)
		fprintf(Stderr, "\n%s: ", strerror(errno));
#endif
	vfprintf(Stderr, fmt, ap);
	va_end(ap);
	fflush(Stderr);
	}

#ifdef ASL_OLD_DERIV_CHECK /*{{*/
 void deriv_errclear_ASL(Edaginfo *I) {}
 void deriv_errchk_ASL(ASL *asl, fint *nerror, int coi, int n) {}
#else /*}{*/
 typedef struct DerrRecord DerrRecord;
 typedef void (*DerrPrint)(ASL*, DerrRecord*);

 struct
DerrRecord {
	DerrPrint errprint;
	const char *fmt, *who;
	real a;
	union { const char *s; real b; } u;
	int jv;
	int dv;
	};

 static void
derrprint1(ASL *asl, DerrRecord *R)
{
	fprintf(Stderr, R->fmt, R->who, R->a);
	}

 static void
derrprint2(ASL *asl, DerrRecord *R)
{
	fprintf(Stderr, R->fmt, R->who, R->a, R->u.b);
	}

 static void
derrprintf(ASL *asl, DerrRecord *R)
{
	fprintf(Stderr, R->fmt, R->who, R->u.s);
	}

 typedef struct DerrMblock DerrMblock;
 struct
DerrMblock {
	DerrMblock *next;
	size_t len;
	real align[1]; /* would prefer align[0], but some older compilers would complain */
	};

 struct
DerivErrInfo {
	DerrMblock *curmb, *freemb;
	char *mbnext, *mblast;
	DerrRecord **R;
	int *busy;
	int nbusy;
	};

 void
deriv_errchk_ASL(ASL *asl, fint *nerror, int coi, int n)
{
	DerivErrInfo *D;
	DerrRecord *R, **Rp, **Rpe;
	int k;

	D = asl->i.Derrs;
	if ((k = coi) < 0) {
		k = -(k + 1);
		if (k >= nlo)
			return;
		k += nlc;
		}
	else if (k >= nlc)
		return;
	for(Rp = D->R + k, Rpe = Rp + n; Rp < Rpe; ++Rp, ++coi)
		if ((R = *Rp)) {
			jmp_check(err_jmp, R->jv);
			co_index = coi;
			cv_index = R->dv;
			report_where(asl);
			R->errprint(asl,R);
			fflush(Stderr);
			jmp_check(err_jmp1, R->jv);
			exit(1);
			}
	}

 static DerivErrInfo *
new_DerrMblock(Edaginfo *I, size_t len)
{
	DerivErrInfo *D;
	DerrMblock *M, **Mp;
	char *s;
	int nlco;
	size_t L, L1;

	len = len < 4096
		? 4096
		: (len + sizeof(real) - 1) & ~(sizeof(real) - 1);
	if (!(D = I->Derrs)) {
		if ((D = I->Derrs0)) {
			I->Derrs = D;
			M = D->curmb;
			if (M->len >= len)
				return D;
			}
		else {
			nlco = I->nlc_ + I->nlo_;
			L = sizeof(DerivErrInfo)
				+ nlco*(sizeof(int) + sizeof(DerrRecord*));
			L = (L + sizeof(real) - 1) & ~(sizeof(real) - 1);
			L1 = L + (sizeof(DerrMblock) - sizeof(real)) + len;
			D = (DerivErrInfo*)M1alloc_ASL(I, L1);
			memset(D, 0, L);
			I->Derrs = I->Derrs0 = D;
			D->R = (DerrRecord**)(D+1);
			D->busy = (int*)(D->R + nlco);
			M = (DerrMblock*)((char*)D + L);
			M->len = len;
			goto have_M;
			}
		}
	for(Mp = &D->freemb;; Mp = &M->next) {
		if (!(M = *Mp)) {
			M = (DerrMblock*)M1alloc_ASL(I, (sizeof(DerrMblock) - sizeof(real)) + len);
			M->len = len;
			break;
			}
		if (M->len >= len) {
			*Mp = M->next;
			break;
			}
		}
 have_M:
	M->next = D->curmb;
	D->curmb = M;
	D->mbnext = s = (char*)M->align;
	D->mblast = s + M->len;
	return D;
	}

 static DerrRecord *
getDR(ASL *asl)
{
	DerivErrInfo *D;
	DerrRecord *R;
	int i, j, je, k;
	size_t L;

	if ((k = co_index) < 0) {
		k = -(k + 1);
		if (k >= nlo)
			return 0;
		k += nlc;
		}
	else if (k >= nlc)
		return 0;
	L = (sizeof(DerrRecord) + sizeof(real)-1) & ~(sizeof(real)-1);
	if ((D = asl->i.Derrs)) {
		if (D->R[k])
			return 0;
		if (L <= D->mblast - D->mbnext)
			goto have_D;
		}
	D = new_DerrMblock(&asl->i, L);
 have_D:
	D->R[k] = R = (DerrRecord*)(D->mblast - L);
	D->mblast = (char*)R;
	D->busy[D->nbusy++] = k;
	if ((R->dv = i = cv_index)) {
		j = 0;
		je = nlc + nlo;
		if (i > comb) {
			if (i <= combc)
				je = nlc;
			else if (i <= ncom0)
				j = combc;
			}
		for(; j < je; ++j) {
			if (!D->R[j]) {
				D->R[j] = R;
				D->busy[D->nbusy++] = j;
				}
			}
		}
	return R;
	}

 void
deriv_errclear_ASL(Edaginfo *I)
{
	DerivErrInfo *D;
	DerrMblock *M, *M0, *M1;
	DerrRecord **R;
	char *s;
	int *b, *be;

	D = I->Derrs;
	I->Derrs = 0;
	R = D->R;
	for(b = D->busy, be = b + D->nbusy; b < be; ++b)
		R[*b] = 0;
	D->nbusy = 0;
	M0 = D->freemb;
	for(M = D->curmb; M; M0 = M, M = M1) {
		M1 = M->next;
		M->next = M0;
		}
	D->freemb = M0->next;
	M0->next = 0;
	D->curmb = M0;
	D->mbnext = s = (char*)M0->align;
	D->mblast = s + M0->len;
	}

#endif /*}} ASL_OLD_DERIV_CHECK*/

 void
introuble_ASL(ASL *asl, const char *who, real a, int jv)
{
	static const char fmt[] = "can't evaluate %s(%g).\n";
#ifndef ASL_OLD_DERIV_CHECK /*{*/
	DerrRecord *R;

	if (jv > 1 && !(want_deriv & 2)) {
		if ((R = getDR(asl))) {
			R->errprint = derrprint1;
			R->a = a;
			R->jv = jv;
			R->fmt = fmt;
			R->who = who;
			}
		return;
		}
#endif /*}*/
	jmp_check(err_jmp, jv);
	report_where(asl);
	Errprint(fmt, who, a);
	jmp_check(err_jmp1, jv);
	exit(1);
	}

 void
introuble2_ASL(ASL *asl, const char *who, real a, real b, int jv)
{
	static const char fmt[] = "can't evaluate %s(%g,%g).\n";
#ifndef ASL_OLD_DERIV_CHECK /*{*/
	DerrRecord *R;

	if (jv > 1 && !(want_deriv & 2)) {
		if ((R = getDR(asl))) {
			R->errprint = derrprint2;
			R->a = a;
			R->u.b = b;
			R->jv = jv;
			R->fmt = fmt;
			R->who = who;
			}
		return;
		}
#endif /*}*/
	jmp_check(err_jmp, jv);
	report_where(asl);
	Errprint(fmt, who, a, b);
	jmp_check(err_jmp1, jv);
	exit(1);
	}

 void
zero_div_ASL(ASL *asl, real L, const char *op)
{
	errno_set(EDOM);
	jmp_check(err_jmp, 1);
	report_where(asl);
	fprintf(Stderr, "can't compute %g%s0.\n", L, op);
	fflush(Stderr);
	jmp_check(err_jmp1, 1);
	exit(1);
	}

 void
fintrouble_ASL(ASL *asl, func_info *fi, const char *s, TMInfo *T)
{
	TMInfo *T1, *T1prev;
	int jv;
	static const char fmt[] = "Error in function %s:\n\t%s\n";

	jv = 1;
	switch(*s) {
	 case '\'':
		jv = 2;
		goto inc_s;
	 case '"':
		jv = 3;
 inc_s:
		++s;
	 }
#ifndef ASL_OLD_DERIV_CHECK /*{*/
	if (jv > 1 && !(want_deriv & 2)) {
		DerivErrInfo *D;
		DerrRecord *R;
		size_t L;

		if ((R = getDR(asl))) {
			D = asl->i.Derrs;
			L = strlen(s) + 1;
			if (L > D->mblast - D->mbnext)
				D = new_DerrMblock(&asl->i, L);
			memcpy(D->mbnext, s, L);
			R->u.s = D->mbnext;
			D->mbnext += L;
			R->errprint = derrprintf;
			R->jv = jv;
			R->fmt = fmt;
			R->who = fi->name;
			}
		return;
		}
#endif /*}*/
	jmp_check(err_jmp, jv);
	report_where(asl);
	fprintf(Stderr, fmt, fi->name, s);
	fflush(Stderr);
	for(T1 = T->u.prev; T1; T1 = T1prev) {
		T1prev = T1->u.prev;
		free(T1);
		}
	jmp_check(err_jmp1,jv);
	exit(1);
	}