aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/f2c/src/sysdep.c
blob: 93a1e5b3b93533b9279ad91371ea655a5e6992d5 (plain) (blame)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/****************************************************************
Copyright 1990 - 1994, 2000 by AT&T, Lucent Technologies and Bellcore.

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 names of AT&T, Bell Laboratories,
Lucent or Bellcore or any of their entities not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

AT&T, Lucent and Bellcore disclaim all warranties with regard to
this software, including all implied warranties of
merchantability and fitness.  In no event shall AT&T, Lucent or
Bellcore 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 "defs.h"
#include "usignal.h"

char binread[] = "rb", textread[] = "r";
char binwrite[] = "wb", textwrite[] = "w";
char *c_functions	= "c_functions";
char *coutput		= "c_output";
char *initfname		= "raw_data";
char *initbname		= "raw_data.b";
char *blkdfname		= "block_data";
char *p1_file		= "p1_file";
char *p1_bakfile	= "p1_file.BAK";
char *sortfname		= "init_file";
char *proto_fname	= "proto_file";

char link_msg[]	= "on Microsoft Windows system, link with libf2c.lib;\n\
	on Linux or Unix systems, link with .../path/to/libf2c.a -lm\n\
	or, if you install libf2c.a in a standard place, with -lf2c -lm\n\
	-- in that order, at the end of the command line, as in\n\
		cc *.o -lf2c -lm\n\
	Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,\n\n\
		http://www.netlib.org/f2c/libf2c.zip";

char *outbuf = "", *outbtail;

#undef WANT_spawnvp
#ifdef MSDOS
#ifndef NO_spawnvp
#define WANT_spawnvp
#endif
#endif

#ifdef _WIN32
#include <windows.h>	/* for GetVolumeInformation */
#undef WANT_spawnvp
#define WANT_spawnvp
#undef  MSDOS
#define MSDOS
#endif

#ifdef WANT_spawnvp
#include <process.h>
#ifndef _P_WAIT
#define _P_WAIT P_WAIT	/* Symantec C/C++ */
#endif
static char **spargv, **pfname;
#endif

char *tmpdir = "";

#ifdef __cplusplus
#define Cextern extern "C"
extern "C" {
 static void flovflo(int), killed(int);
 static int compare(const void *a, const void *b);
}
#else
#define Cextern extern
#endif

Cextern int unlink Argdcl((const char *));
Cextern int fork Argdcl((void)), getpid Argdcl((void)), wait Argdcl((int*));

 void
#ifdef KR_headers
Un_link_all(cdelete)
	int cdelete;
#else
Un_link_all(int cdelete)
#endif
{
	if (!debugflag) {
		unlink(c_functions);
		unlink(initfname);
		unlink(p1_file);
		unlink(sortfname);
		unlink(blkdfname);
		if (cdelete && coutput)
			unlink(coutput);
		}
	}

#ifndef MSDOS
#include "sysdep.hd"
#include <unistd.h> /* for mkdtemp and rmdir */
#endif

#ifndef NO_TEMPDIR
 static void
rmtdir(Void)
{
	char *s;
	if (*(s = tmpdir)) {
		tmpdir = "";
		rmdir(s);
		}
	}
#endif /*NO_TEMPDIR*/

 static void
alloc_names(Void)
{
	int k = strlen(tmpdir) + 24;
	c_functions = (char *)ckalloc(7*k);
	initfname = c_functions + k;
	initbname = initfname + k;
	blkdfname = initbname + k;
	p1_file = blkdfname + k;
	p1_bakfile = p1_file + k;
	sortfname = p1_bakfile + k;
	}

 void
set_tmp_names(Void)
{
#ifdef MSDOS
	char buf[64], *s, *t;
#ifdef _WIN32
	DWORD flags, maxlen, volser;
	char volname[512], f2c[24], fsname[512], *name1;
	int i;

	if (debugflag == 1)
		return;
	i = sprintf(f2c, "%x", _getpid());
	if (!GetVolumeInformation(NULL, volname, sizeof(volname), &volser, &maxlen,
			&flags, fsname, sizeof(fsname))
	 || maxlen < i+8) /* FAT16 */
		strcpy(f2c, "f2c_");
#else
	static char f2c[] = "f2c_";
	if (debugflag == 1)
		return;
#endif

	if (!*tmpdir || *tmpdir == '.' && !tmpdir[1])
		t = "";
	else {
		/* substitute \ for / to avoid confusion with a
		 * switch indicator in the system("sort ...")
		 * call in formatdata.c
		 */
		for(s = tmpdir, t = buf; *s; s++, t++)
			if ((*t = *s) == '/')
				*t = '\\';
		if (t[-1] != '\\')
			*t++ = '\\';
		*t = 0;
		t = buf;
		}
	alloc_names();
	sprintf(c_functions, "%s%sfunc", t, f2c);
	sprintf(initfname, "%s%srd", t, f2c);
	sprintf(blkdfname, "%s%sblkd", t, f2c);
	sprintf(p1_file, "%s%sp1f", t, f2c);
	sprintf(p1_bakfile, "%s%sp1fb", t, f2c);
	sprintf(sortfname, "%s%ssort", t, f2c);
#else /*!MSDOS*/
	long pid;

#define L_TDNAME 20
#ifdef NO_MKDTEMP
#ifdef NO_MKSTEMP
#undef  L_TDNAME
#define L_TDNAME L_tmpnam
#endif
#endif
	static char tdbuf[L_TDNAME];

	if (debugflag == 1)
		return;
	pid = getpid();
	if (!*tmpdir) {
#ifdef NO_TEMPDIR
		tmpdir = "/tmp";
#else
#ifdef NO_MKDTEMP
#ifdef NO_MKSTEMP
		if (!(tmpdir = tmpnam(tdbuf))) {
			fprintf(stderr, "tmpnam failed (for -T)\n");
			exit(1);
			}
#else
		int f;
		strcpy(tdbuf, "/tmp/f2ctd_XXXXXX");
		f = mkstemp(tdbuf);
		if (f >= 0) {
			close(f);
			remove(tmpdir = tdbuf);
			}
		else {
			fprintf(stderr, "mkstemp failed (for -T)\n");
			exit(1);
			}
#endif /*NO_MKSTEMP*/
		if (mkdir(tdbuf,0700)) {
			fprintf(stderr, "mkdir failed (for -T)\n");
			exit(1);
			}
#else /*!NO_MKDTEMP*/
		strcpy(tdbuf, "/tmp/f2ctd_XXXXXX");
		if (!(tmpdir = mkdtemp(tdbuf))) {
			fprintf(stderr, "mkdtemp failed (for -T)\n");
			exit(1);
			}
#endif /*NO_MKDTEMP*/
		if (!debugflag)
			atexit(rmtdir);
#endif /*NO_TEMPDIR*/
		}
	alloc_names();
	sprintf(c_functions, "%s/f2c%ld_func", tmpdir, pid);
	sprintf(initfname, "%s/f2c%ld_rd", tmpdir, pid);
	sprintf(blkdfname, "%s/f2c%ld_blkd", tmpdir, pid);
	sprintf(p1_file, "%s/f2c%ld_p1f", tmpdir, pid);
	sprintf(p1_bakfile, "%s/f2c%ld_p1fb", tmpdir, pid);
	sprintf(sortfname, "%s/f2c%ld_sort", tmpdir, pid);
#endif /*MSDOS*/
	sprintf(initbname, "%s.b", initfname);
	if (debugflag)
		fprintf(diagfile, "%s %s %s %s %s %s\n", c_functions,
			initfname, blkdfname, p1_file, p1_bakfile, sortfname);
	}

 char *
#ifdef KR_headers
c_name(s, ft)
	char *s;
	int ft;
#else
c_name(char *s, int ft)
#endif
{
	char *b, *s0;
	int c;

	b = s0 = s;
	while(c = *s++)
		if (c == '/')
			b = s;
	if (--s < s0 + 3 || s[-2] != '.'
			 || ((c = *--s) != 'f' && c != 'F')) {
		infname = s0;
		Fatal("file name must end in .f or .F");
		}
	strcpy(outbtail, b);
	outbtail[s-b] = ft;
	b = copys(outbuf);
	return b;
	}

 static void
#ifdef KR_headers
killed(sig)
	int sig;
#else
killed(int sig)
#endif
{
	sig = sig;	/* shut up warning */
	signal(SIGINT, SIG_IGN);
#ifdef SIGQUIT
	signal(SIGQUIT, SIG_IGN);
#endif
#ifdef SIGHUP
	signal(SIGHUP, SIG_IGN);
#endif
	signal(SIGTERM, SIG_IGN);
	Un_link_all(1);
	exit(126);
	}

 static void
#ifdef KR_headers
sig1catch(sig)
	int sig;
#else
sig1catch(int sig)
#endif
{
	sig = sig;	/* shut up warning */
	if (signal(sig, SIG_IGN) != SIG_IGN)
		signal(sig, killed);
	}

 static void
#ifdef KR_headers
flovflo(sig)
	int sig;
#else
flovflo(int sig)
#endif
{
	sig = sig;	/* shut up warning */
	Fatal("floating exception during constant evaluation; cannot recover");
	/* vax returns a reserved operand that generates
	   an illegal operand fault on next instruction,
	   which if ignored causes an infinite loop.
	*/
	signal(SIGFPE, flovflo);
}

 void
#ifdef KR_headers
sigcatch(sig)
	int sig;
#else
sigcatch(int sig)
#endif
{
	sig = sig;	/* shut up warning */
	sig1catch(SIGINT);
#ifdef SIGQUIT
	sig1catch(SIGQUIT);
#endif
#ifdef SIGHUP
	sig1catch(SIGHUP);
#endif
	sig1catch(SIGTERM);
	signal(SIGFPE, flovflo);  /* catch overflows */
	}

/* argkludge permits wild-card expansion and caching of the original or expanded */
/* argv to kludge around the lack of fork() and exec() when necessary. */

 void
#ifdef KR_headers
argkludge(pargc, pargv) int *pargc; char ***pargv;
#else
argkludge(int *pargc, char ***pargv)
#endif
{
#ifdef WANT_spawnvp
	size_t L, L1;
	int argc, i, nf;
	char **a, **argv, *s, *t, *t0;

	/* Assume wild-card expansion has been done by Microsoft's setargv.obj */

	/* Count Fortran input files. */

	L = argc = *pargc;
	argv = *pargv;
	for(i = nf = 0; i < argc; i++) {
		L += L1 = strlen(s = argv[i]);
		if (L1 > 2 && s[L1-2] == '.')
			switch(s[L1-1]) {
			  case 'f':
			  case 'F':
				nf++;
			  }
		}
	if (nf <= 1)
		return;

	/* Cache inputs */

	i = argc - nf + 2;
	a = spargv = (char**)Alloc(i*sizeof(char*) + L);
	t = (char*)(a + i);
	for(i = 0; i < argc; i++) {
		*a++ = t0 = t;
		for(s = argv[i]; *t++ = *s; s++);
		if (t-t0 > 3 && s[-2] == '.')
			switch(s[-1]) {
			  case 'f':
			  case 'F':
				--a;
				t = t0;
			  }
		}
	pfname = a++;
	*a = 0;
#endif
	}

 int
#ifdef KR_headers
dofork(fname) char *fname;
#else
dofork(char *fname)
#endif
{
	extern int retcode;
#ifdef MSDOS
#ifdef WANT_spawnvp
	*pfname = fname;
	retcode |= _spawnvp(_P_WAIT, spargv[0], (char const*const*)spargv);
#else /*_WIN32*/
	Fatal("Only one Fortran input file allowed under MS-DOS");
#endif /*_WIN32*/
#else
	int pid, status, w;

	if (!(pid = fork()))
		return 1;
	if (pid == -1)
		Fatal("bad fork");
	while((w = wait(&status)) != pid)
		if (w == -1)
			Fatal("bad wait code");
	retcode |= status >> 8;
#endif
	return 0;
	}

/* Initialization of tables that change with the character set... */

char escapes[Table_size];

#ifdef non_ASCII
char *str_fmt[Table_size];
static char *str0fmt[127] = { /*}*/
#else
char *str_fmt[Table_size] = {
#endif
 "\\000", "\\001", "\\002", "\\003", "\\004", "\\005", "\\006", "\\007",
   "\\b",   "\\t",   "\\n", "\\013",   "\\f",   "\\r", "\\016", "\\017",
 "\\020", "\\021", "\\022", "\\023", "\\024", "\\025", "\\026", "\\027",
 "\\030", "\\031", "\\032", "\\033", "\\034", "\\035", "\\036", "\\037",
     " ",     "!",  "\\\"",     "#",     "$",     "%%",    "&",     "'",
     "(",     ")",     "*",     "+",     ",",     "-",     ".",     "/",
     "0",     "1",     "2",     "3",     "4",     "5",     "6",     "7",
     "8",     "9",     ":",     ";",     "<",     "=",     ">",     "?",
     "@",     "A",     "B",     "C",     "D",     "E",     "F",     "G",
     "H",     "I",     "J",     "K",     "L",     "M",     "N",     "O",
     "P",     "Q",     "R",     "S",     "T",     "U",     "V",     "W",
     "X",     "Y",     "Z",     "[",  "\\\\",     "]",     "^",     "_",
     "`",     "a",     "b",     "c",     "d",     "e",     "f",     "g",
     "h",     "i",     "j",     "k",     "l",     "m",     "n",     "o",
     "p",     "q",     "r",     "s",     "t",     "u",     "v",     "w",
     "x",     "y",     "z",     "{",     "|",     "}",     "~"
     };

#ifdef non_ASCII
char *chr_fmt[Table_size];
static char *chr0fmt[127] = {	/*}*/
#else
char *chr_fmt[Table_size] = {
#endif
   "\\0",   "\\1",   "\\2",   "\\3",   "\\4",   "\\5",   "\\6",   "\\7",
   "\\b",   "\\t",   "\\n",  "\\13",   "\\f",   "\\r",  "\\16",  "\\17",
  "\\20",  "\\21",  "\\22",  "\\23",  "\\24",  "\\25",  "\\26",  "\\27",
  "\\30",  "\\31",  "\\32",  "\\33",  "\\34",  "\\35",  "\\36",  "\\37",
     " ",     "!",    "\"",     "#",     "$",     "%%",    "&",   "\\'",
     "(",     ")",     "*",     "+",     ",",     "-",     ".",     "/",
     "0",     "1",     "2",     "3",     "4",     "5",     "6",     "7",
     "8",     "9",     ":",     ";",     "<",     "=",     ">",     "?",
     "@",     "A",     "B",     "C",     "D",     "E",     "F",     "G",
     "H",     "I",     "J",     "K",     "L",     "M",     "N",     "O",
     "P",     "Q",     "R",     "S",     "T",     "U",     "V",     "W",
     "X",     "Y",     "Z",     "[",  "\\\\",     "]",     "^",     "_",
     "`",     "a",     "b",     "c",     "d",     "e",     "f",     "g",
     "h",     "i",     "j",     "k",     "l",     "m",     "n",     "o",
     "p",     "q",     "r",     "s",     "t",     "u",     "v",     "w",
     "x",     "y",     "z",     "{",     "|",     "}",     "~"
     };

 void
fmt_init(Void)
{
	static char *str1fmt[6] =
		{ "\\b", "\\t", "\\n", "\\f", "\\r", "\\013" };
	register int i, j;
	register char *s;

	/* str_fmt */

#ifdef non_ASCII
	i = 0;
#else
	i = 127;
#endif
	s = Alloc(5*(Table_size - i));
	for(; i < Table_size; i++) {
		sprintf(str_fmt[i] = s, "\\%03o", i);
		s += 5;
		}
#ifdef non_ASCII
	for(i = 32; i < 127; i++) {
		s = str0fmt[i];
		str_fmt[*(unsigned char *)s] = s;
		}
	str_fmt['"'] = "\\\"";
#else
	if (Ansi == 1)
		str_fmt[7] = chr_fmt[7] = "\\a";
#endif

	/* chr_fmt */

#ifdef non_ASCII
	for(i = 0; i < 32; i++)
		chr_fmt[i] = chr0fmt[i];
#else
	i = 127;
#endif
	for(; i < Table_size; i++)
		chr_fmt[i] = "\\%o";
#ifdef non_ASCII
	for(i = 32; i < 127; i++) {
		s = chr0fmt[i];
		j = *(unsigned char *)s;
		if (j == '\\')
			j = *(unsigned char *)(s+1);
		chr_fmt[j] = s;
		}
#endif

	/* escapes (used in lex.c) */

	for(i = 0; i < Table_size; i++)
		escapes[i] = i;
	for(s = "btnfr0", i = 0; i < 6; i++)
		escapes[*(unsigned char *)s++] = "\b\t\n\f\r"[i];
	/* finish str_fmt and chr_fmt */

	if (Ansi)
		str1fmt[5] = "\\v";
	if ('\v' == 'v') { /* ancient C compiler */
		str1fmt[5] = "v";
#ifndef non_ASCII
		escapes['v'] = 11;
#endif
		}
	else
		escapes['v'] = '\v';
	for(s = "\b\t\n\f\r\v", i = 0; j = *(unsigned char *)s++;)
		str_fmt[j] = chr_fmt[j] = str1fmt[i++];
	/* '\v' = 11 for both EBCDIC and ASCII... */
	chr_fmt[11] = (char*)(Ansi ? "\\v" : "\\13");
	}

 void
outbuf_adjust(Void)
{
	int n, n1;
	char *s;

	n = n1 = strlen(outbuf);
	if (*outbuf && outbuf[n-1] != '/')
		n1++;
	s = Alloc(n+64);
	outbtail = s + n1;
	strcpy(s, outbuf);
	if (n != n1)
		strcpy(s+n, "/");
	outbuf = s;
	}


/* Unless SYSTEM_SORT is defined, the following gives a simple
 * in-core version of dsort().  On Fortran source with huge DATA
 * statements, the in-core version may exhaust the available memory,
 * in which case you might either recompile this source file with
 * SYSTEM_SORT defined (if that's reasonable on your system), or
 * replace the dsort below with a more elaborate version that
 * does a merging sort with the help of auxiliary files.
 */

#ifdef SYSTEM_SORT

 int
#ifdef KR_headers
dsort(from, to)
	char *from;
	char *to;
#else
dsort(char *from, char *to)
#endif
{
	char buf[200];
	sprintf(buf, "sort <%s >%s", from, to);
	return system(buf) >> 8;
	}
#else

 static int
#ifdef KR_headers
 compare(a,b)
  char *a, *b;
#else
 compare(const void *a, const void *b)
#endif
{ return strcmp(*(char **)a, *(char **)b); }

 int
#ifdef KR_headers
dsort(from, to)
	char *from;
	char *to;
#else
dsort(char *from, char *to)
#endif
{
	struct Memb {
		struct Memb *next;
		int n;
		char buf[32000];
		};
	typedef struct Memb memb;
	memb *mb, *mb1;
	register char *x, *x0, *xe;
	register int c, n;
	FILE *f;
	char **z, **z0;
	int nn = 0;

	f = opf(from, textread);
	mb = (memb *)Alloc(sizeof(memb));
	mb->next = 0;
	x0 = x = mb->buf;
	xe = x + sizeof(mb->buf);
	n = 0;
	for(;;) {
		c = getc(f);
		if (x >= xe && (c != EOF || x != x0)) {
			if (!n)
				return 126;
			nn += n;
			mb->n = n;
			mb1 = (memb *)Alloc(sizeof(memb));
			mb1->next = mb;
			mb = mb1;
			memcpy(mb->buf, x0, n = x-x0);
			x0 = mb->buf;
			x = x0 + n;
			xe = x0 + sizeof(mb->buf);
			n = 0;
			}
		if (c == EOF)
			break;
		if (c == '\n') {
			++n;
			*x++ = 0;
			x0 = x;
			}
		else
			*x++ = c;
		}
	clf(&f, from, 1);
	f = opf(to, textwrite);
	if (x > x0) { /* shouldn't happen */
		*x = 0;
		++n;
		}
	mb->n = n;
	nn += n;
	if (!nn) /* shouldn't happen */
		goto done;
	z = z0 = (char **)Alloc(nn*sizeof(char *));
	for(mb1 = mb; mb1; mb1 = mb1->next) {
		x = mb1->buf;
		n = mb1->n;
		for(;;) {
			*z++ = x;
			if (--n <= 0)
				break;
			while(*x++);
			}
		}
	qsort((char *)z0, nn, sizeof(char *), compare);
	for(n = nn, z = z0; n > 0; n--)
		fprintf(f, "%s\n", *z++);
	free((char *)z0);
 done:
	clf(&f, to, 1);
	do {
		mb1 = mb->next;
		free((char *)mb);
		}
		while(mb = mb1);
	return 0;
	}
#endif