File: func_add.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 (550 lines) | stat: -rw-r--r-- 11,444 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/****************************************************************
Copyright (C) 1997-2000 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.
****************************************************************/

#include "asl.h"

#ifdef __cplusplus
extern "C" {
#endif
extern ASLhead ASLhead_ASL;

const char *i_option_ASL;
unsigned long randseed_ASL;
static int n_added;

 void
addrandinit_ASL(AmplExports *ae, RandSeedSetter rss, void *v)
{
	char *s, *se;
	unsigned long x;

	/* Something more elaborate will be needed if we add a "reset" facility to ASL. */

	if (!randseed_ASL) {
		randseed_ASL = 1;
		if ((s = getenv("randseed"))
		 && (x = (unsigned long)strtol(s,&se,10))
		 && !se)
			randseed_ASL = x;
		}
	(*rss)(v, randseed_ASL);
	}

 func_info *
func_lookup(ASL *asl, register const char *s, int add)
{
	register unsigned x = 0;
	func_info *fi, **finext;
	Const char *s0 = s;

	while(*s)
		x = 31*x + *s++;
	finext = &fhash[x % NFHASH];
	for(fi = *finext; fi; fi = fi->next)
		if (!strcmp(s0, fi->name)) {
			if (add) {
				fprintf(Stderr,
				"addfunc: duplicate function %s\n", s0);
				fi = 0;
				}
			return fi;
			}
	if (add) {
		fi = (func_info *)mem_ASL(asl, sizeof(func_info));
		fi->next = *finext;
		*finext = fi;
		fi->name = s0;
		}
	return fi;
	}

 void
addfunc_ASL(const char *fname, ufunc *f, int ftype, int nargs, void *funcinfo, AmplExports *ae)
{
	register func_info *fi;
	ASL *asl = (ASL*)ae->asl;
	if (ftype && ftype != 1) {
#ifndef COMPLAIN_AT_BAD_FTYPE
		if (ftype < 0 || ftype > 6)
#endif
		{
		fprintf(Stderr, "function %s: ftype = %d; expected 0 or 1\n",
			fname, ftype);
		exit(1);
		}
#ifndef COMPLAIN_AT_BAD_FTYPE
		return;
#endif
		}
	if ((fi = func_lookup(asl, fname, 1))) {
		n_added++;
		fi->funcp = f;
		fi->ftype = ftype;
		fi->nargs = nargs;
		fi->funcinfo = funcinfo;
		if (!funcsfirst)
			funcsfirst = fi;
		else
			funcslast->fnext = fi;
		funcslast = fi;
		fi->fnext = 0;
		}
	}

enum { NEFB = 5, NEFB0 = 2 };

 static Exitcall a_e_info[NEFB0];
 static Exitcall *a_e_next = a_e_info;
 static Exitcall *a_e_last = a_e_info + NEFB0;
 static Exitcall *a_e_prev;

 typedef struct
ExitCallInfo { Exitcall *cur, **curp, *last, **lastp; } ExitCallInfo;

 static void
AtReset1(AmplExports *ae, Exitfunc *ef, void *v, ExitCallInfo *eci)
{
	Exitcall *ec;
	ASL *asl = (ASL*)ae->asl;
	if (eci) {
		eci->cur = asl->i.arprev;
		eci->curp = &asl->i.arprev;
		eci->last = asl->i.arlast;
		eci->lastp = &asl->i.arlast;
		}
	if (asl->i.arnext >= asl->i.arlast) {
		asl->i.arnext = (Exitcall*)M1alloc(NEFB*sizeof(Exitcall));
		asl->i.arlast = asl->i.arnext + NEFB;
		}
	asl->i.arnext->prev = asl->i.arprev;
	asl->i.arprev = ec = asl->i.arnext++;
	ec->ef = ef;
	ec->v = v;
	}

 static void
AtReset(AmplExports *ae, Exitfunc *ef, void *v)
{ AtReset1(ae, ef, v, 0); }


 void
at_end_ASL(Exitcall *ec)
{
	while(ec) {
		(*ec->ef)(ec->v);
		ec = ec->prev;
		}
	}

 void
at_exit_ASL(VOID)
{
	Exitcall *ec;
	ASLhead *h, *h0;

	h0 = &ASLhead_ASL;
	h = ASLhead_ASL.next;
	h0->next = h0->prev = h0;
	for(; h != h0; h = h->next)
		if ((ec = ((ASL*)h)->i.arprev))
			at_end_ASL(ec);
	if ((ec = a_e_prev)) {
		a_e_prev = 0;
		at_end_ASL(ec);
		}
	}

 static void
AtExit1(AmplExports *ae, Exitfunc *ef, void *v, ExitCallInfo *eci)
{
	Exitcall *ec;
	Not_Used(ae);
#ifndef NO_ONEXIT
	if (!a_e_prev)
		atexit(at_exit_ASL); /* in case mainexit() is bypassed */
#endif
	if (eci) {
		eci->cur = a_e_prev;
		eci->curp = &a_e_prev;
		eci->last = a_e_last;
		eci->lastp = &a_e_last;
		}
	if (a_e_next >= a_e_last) {
		a_e_next = (Exitcall*)mymalloc(NEFB*sizeof(Exitcall));
		a_e_last = a_e_next + NEFB;
		}
	a_e_next->prev = a_e_prev;
	a_e_prev = ec = a_e_next++;
	ec->ef = ef;
	ec->v = v;
	}

 static void
AtExit(AmplExports *ae, Exitfunc *ef, void *v)
{ AtExit1(ae, ef, v, 0); }

 static Char *
Tempmem(TMInfo *T, size_t L)
{
	TMInfo *T1 = (TMInfo *)mymalloc(L + sizeof(TMInfo));
	T1->u.prev = T->u.prev;
	T->u.prev = T1;
	return (Char*)(T1+1);
	}

 static void
No_table_handler(
	int (*Dbread)(AmplExports*, TableInfo*),
	int (*Dbwrite)(AmplExports*, TableInfo*),
	char *hname,
	int flags,
	void *vinfo)
{}

 static cryptblock*
No_crypto(char *key, size_t scrbytes)
{ return 0; }

typedef void Funcadd ANSI((AmplExports*));

#ifndef CLOSE_AT_RESET
static Funcadd *Fa0[4], **Fa = Fa0;
static int nFa = 0, nFamax = 4;
#endif

#ifdef SYMANTEC
#define No_popen_or_pclose
typedef char *(*Tempnamtype)(const char*, const char*);
#define Tempnam_cast (Tempnamtype)
#endif

#ifdef WATCOM
#define tempnam _tempnam
#endif

#ifdef NO_tempnam

/* If the system does not provide a true tempnam function */
/* the AMPL/solver interface library will not do so either. */

 static char *
tempnam(const char *dir, const char *pfx)
{ return 0; }
#endif /* NO_tempnam */

#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#else
#ifdef MSDOS
#undef  No_popen_or_pclose
#define No_popen_or_pclose
#endif
#endif

#ifdef No_popen_or_pclose
#undef popen
#define popen no_popen
#undef pclose
#define pclose no_pclose

 static int
no_pclose(FILE*f) { return 1; }

 static FILE*
no_popen(const char*cmd, const char*type) { return 0; }
#endif

 static AmplExports AE;

#ifdef clearerr
 static void
myclearerr(FILE *f)
{ clearerr(f); }
#undef clearerr
#define clearerr myclearerr
#endif /*clearerr*/

#ifdef feof
 static int
myfeof(FILE *f)
{ return feof(f); }
#undef feof
#define feof myfeof
#endif /*feof*/

#ifdef ferror
 static int
myferror(FILE *f)
{ return ferror(f); }
#undef ferror
#define ferror myferror
#endif /*ferror*/

#ifdef _fileno
#undef fileno
#define fileno _fileno
#endif

#ifdef fileno
 static int
myfileno(FILE *f)
{ return fileno(f); }
#undef fileno
#define fileno myfileno
#endif /* fileno */

#ifndef Tempnam_cast
#define Tempnam_cast /*nothing*/
#endif

#ifdef __linux__
#define USE_MKSTEMP
#endif
#ifdef USE_MKSTEMP
 /* Shut up warnings about tempnam and tmpnam. */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

 static int
isdir(const char *s)
{
	struct stat sbuf;
	if (stat(s, &sbuf))
		return 0;
	return S_ISDIR(sbuf.st_mode);
	}

 static char *
my_tempnam(const char *dir, const char *pfx, char *s)
{
	const char *c;
	int i;
	size_t Ld, Lp;

	if ((c = getenv("TMPDIR")) && isdir(c))
		dir = c;
	else if (!dir || !isdir(dir))
		dir = "/tmp";
	if (!pfx)
		pfx = "";
	Ld = strlen(dir);
	Lp = strlen(pfx);
	if (!s)
		s = (char*)Malloc(Ld + Lp + 8);
	strcpy(s, dir);
	if (s[Ld-1] != '/')
		s[Ld++] = '/';
	strcpy(s+Ld, pfx);
	strcpy(s + Ld + Lp, "XXXXXX");
	if ((i = mkstemp(s)))
		close(i);
	else {
		free(s);
		s = 0;
		}
	return s;
	}

 static char *
Tempnam(const char *dir, const char *pfx)
{ return my_tempnam(dir,pfx,0); }

 static char *
Tmpnam(char *s)
{
	static char *s0;
	if (s)
		return my_tempnam(0,"Temp_",s);
	if (s0)
		free(s0);
	return s0 = my_tempnam(0,"Temp_",0);
	}
#undef tempnam
#define tempnam Tempnam
#undef tmpnam
#define tmpnam Tmpnam
#endif /* USE_MKSTEMP */

 void (*breakfunc_ASL) ANSI((int,void*)), *breakarg_ASL;

 void
func_add(ASL *asl)
{
	AmplExports *ae;

	if (need_funcadd) {
		if (!i_option_ASL
		 && !(i_option_ASL = getenv("ampl_funclibs")))
		      i_option_ASL = getenv("AMPLFUNC");
		if (!AE.PrintF) {
			AE.StdIn = stdin;
			AE.StdOut = stdout;
			AE.StdErr = Stderr;
			AE.ASLdate = ASLdate_ASL;
			AE.Addfunc = addfunc_ASL;
			AE.PrintF = printf;
			AE.FprintF = fprintf;
			AE.SprintF = sprintf;
			AE.SnprintF = snprintf;
			AE.VfprintF = vfprintf;
			AE.VsprintF = vsprintf;
			AE.VsnprintF = vsnprintf;
			AE.Strtod = strtod;
			AE.AtExit = AtExit;
			AE.AtReset = AtReset;
			AE.Tempmem = Tempmem;
			AE.Add_table_handler = No_table_handler;
			AE.Crypto = No_crypto;
			AE.Qsortv = qsortv;
			AE.Clearerr = clearerr;
			AE.Fclose = fclose;
			AE.Fdopen = fdopen;
			AE.Feof = feof;
			AE.Ferror = ferror;
			AE.Fflush = fflush;
			AE.Fgetc = fgetc;
			AE.Fgets = fgets;
			AE.Fileno = fileno;
			AE.Fopen = fopen;
			AE.Fputc = fputc;
			AE.Fputs = fputs;
			AE.Fread = fread;
			AE.Freopen = freopen;
			AE.Fscanf = fscanf;
			AE.Fseek = fseek;
			AE.Ftell = ftell;
			AE.Fwrite = fwrite;
			AE.Pclose = pclose;
			AE.Perror = perror;
			AE.Popen = popen;
			AE.Puts = puts;
			AE.Rewind = rewind;
			AE.Scanf = scanf;
			AE.Setbuf = setbuf;
			AE.Setvbuf = setvbuf;
			AE.Sscanf = sscanf;
			AE.Tempnam = Tempnam_cast tempnam;
			AE.Tmpfile = tmpfile;
			AE.Tmpnam = tmpnam;
			AE.Ungetc = ungetc;
			AE.Getenv = getenv_ASL;
			AE.Breakfunc = breakfunc_ASL;
			AE.Breakarg = breakarg_ASL;
			AE.Addrandinit = addrandinit_ASL;
			}
		if (AE.asl)
			memcpy(ae = (AmplExports*)M1alloc(sizeof(AmplExports)),
				&AE, sizeof(AmplExports));
		else
			ae = &AE;
		asl->i.ae = ae;
		ae->asl = (Char*)asl;
		auxinfo_ASL(ae);
#ifndef CLOSE_AT_RESET
		if (nFa > 0) {
			/* not the first nl_reader call */
			int i = 0;
			while(i < nFa)
				(*Fa[i++])(ae);
			}
		else
#endif
			funcadd(ae);
		need_funcadd = 0;
		}
	}

 void
show_funcs_ASL(ASL *asl)
{
	func_info *fi;
	int nargs;
	const char *atleast;

	func_add(asl);
	fprintf(Stderr, "Available nonstandard functions:%s\n",
		(fi = funcsfirst) ? "" : " none");
	for(; fi; fi = fi->fnext) {
		if ((nargs = fi->nargs) >= 0)
			atleast = "";
		else {
			nargs = -(1 + nargs);
			atleast = "at least ";
			}
		fprintf(Stderr, "\t%s(%s%d %sarg%s)\n", fi->name,
			atleast, nargs, fi->ftype ? "" : "real ",
			nargs == 1 ? "" : "s");
		}
	fflush(Stderr);
	}

 void
note_libuse_ASL(void)
{ ++n_added; }

 int
aflibname_ASL(AmplExports *ae, const char *fullname, const char *name, int nlen,
	Funcadd *fa, int save_fa, void (*dl_close)(void*), void *h)
{
	Exitcall *ec;
	ExitCallInfo eci;

	af_libnamesave_ASL(ae, fullname, name, nlen);
	n_added = 0;
	if (save_fa)
		AtExit1( ae, dl_close, h, &eci);
	else
		AtReset1(ae, dl_close, h, &eci);
	(*fa)(ae);
	if (!n_added) {
		for(ec = *eci.curp; ec != eci.cur; ec = ec->prev)
			(*ec->ef)(ec->v);
		*eci.curp = ec;
		*eci.lastp = eci.last;
		/* A small storage leak is possible if a new block of */
		/* Exitcalls was allocated, but since the present !n_added */
		/* case is unlikely, this leak should be of little concern. */
		}
#ifndef CLOSE_AT_RESET
	else if (save_fa) {
		if (++nFa >= nFamax) {
			Funcadd **Fa1;
			nFamax <<= 1;
			Fa1 = (Funcadd**)Malloc(nFamax * sizeof(Funcadd*));
			memcpy(Fa1, Fa, nFa*sizeof(Funcadd*));
			if (Fa != Fa0)
				free(Fa);
			Fa = Fa1;
			}
		Fa[nFa-1] = fa;
		}
#endif /* !CLOSE_AT_RESET */
	return n_added;
	}
#ifdef __cplusplus
}
#endif
/* Last relevant change to asl.h: 19991013. */