xref: /minix3/usr.bin/fsplit/fsplit.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
197b7aef1SThomas Cort /*
297b7aef1SThomas Cort  * Copyright (c) 1983, 1993
397b7aef1SThomas Cort  *	The Regents of the University of California.  All rights reserved.
497b7aef1SThomas Cort  *
597b7aef1SThomas Cort  * This code is derived from software contributed to Berkeley by
697b7aef1SThomas Cort  * Asa Romberger and Jerry Berkman.
797b7aef1SThomas Cort  *
897b7aef1SThomas Cort  * Redistribution and use in source and binary forms, with or without
997b7aef1SThomas Cort  * modification, are permitted provided that the following conditions
1097b7aef1SThomas Cort  * are met:
1197b7aef1SThomas Cort  * 1. Redistributions of source code must retain the above copyright
1297b7aef1SThomas Cort  *    notice, this list of conditions and the following disclaimer.
1397b7aef1SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
1497b7aef1SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
1597b7aef1SThomas Cort  *    documentation and/or other materials provided with the distribution.
1697b7aef1SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
1797b7aef1SThomas Cort  *    may be used to endorse or promote products derived from this software
1897b7aef1SThomas Cort  *    without specific prior written permission.
1997b7aef1SThomas Cort  *
2097b7aef1SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2197b7aef1SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2297b7aef1SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2397b7aef1SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2497b7aef1SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2597b7aef1SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2697b7aef1SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2797b7aef1SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2897b7aef1SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2997b7aef1SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3097b7aef1SThomas Cort  * SUCH DAMAGE.
3197b7aef1SThomas Cort  */
3297b7aef1SThomas Cort 
3397b7aef1SThomas Cort #include <sys/cdefs.h>
3497b7aef1SThomas Cort #ifndef lint
3597b7aef1SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
3697b7aef1SThomas Cort  The Regents of the University of California.  All rights reserved.");
3797b7aef1SThomas Cort #endif /* not lint */
3897b7aef1SThomas Cort 
3997b7aef1SThomas Cort #ifndef lint
4097b7aef1SThomas Cort #if 0
4197b7aef1SThomas Cort static char sccsid[] = "from: @(#)fsplit.c	8.1 (Berkeley) 6/6/93";
4297b7aef1SThomas Cort #else
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fsplit.c,v 1.30 2015/06/16 22:54:10 christos Exp $");
4497b7aef1SThomas Cort #endif
4597b7aef1SThomas Cort #endif /* not lint */
4697b7aef1SThomas Cort 
4797b7aef1SThomas Cort #include <sys/types.h>
4897b7aef1SThomas Cort #include <sys/stat.h>
4997b7aef1SThomas Cort 
5097b7aef1SThomas Cort #include <assert.h>
5197b7aef1SThomas Cort #include <ctype.h>
5297b7aef1SThomas Cort #include <err.h>
5397b7aef1SThomas Cort #include <stdbool.h>
5497b7aef1SThomas Cort #include <stdio.h>
5597b7aef1SThomas Cort #include <stdlib.h>
5697b7aef1SThomas Cort #include <string.h>
5797b7aef1SThomas Cort #include <unistd.h>
5897b7aef1SThomas Cort 
5997b7aef1SThomas Cort /*
6097b7aef1SThomas Cort  *	usage:		fsplit [-e efile] ... [file]
6197b7aef1SThomas Cort  *
6297b7aef1SThomas Cort  *	split single file containing source for several fortran programs
6397b7aef1SThomas Cort  *		and/or subprograms into files each containing one
6497b7aef1SThomas Cort  *		subprogram unit.
6597b7aef1SThomas Cort  *	each separate file will be named using the corresponding subroutine,
6697b7aef1SThomas Cort  *		function, block data or program name if one is found; otherwise
6797b7aef1SThomas Cort  *		the name will be of the form mainNNN.f or blkdtaNNN.f .
6897b7aef1SThomas Cort  *		If a file of that name exists, it is saved in a name of the
6997b7aef1SThomas Cort  *		form zzz000.f .
7097b7aef1SThomas Cort  *	If -e option is used, then only those subprograms named in the -e
7197b7aef1SThomas Cort  *		option are split off; e.g.:
7297b7aef1SThomas Cort  *			fsplit -esub1 -e sub2 prog.f
7397b7aef1SThomas Cort  *		isolates sub1 and sub2 in sub1.f and sub2.f.  The space
7497b7aef1SThomas Cort  *		after -e is optional.
7597b7aef1SThomas Cort  *
7697b7aef1SThomas Cort  *	Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
7797b7aef1SThomas Cort  *		- added comments
7897b7aef1SThomas Cort  *		- more function types: double complex, character*(*), etc.
7997b7aef1SThomas Cort  *		- fixed minor bugs
8097b7aef1SThomas Cort  *		- instead of all unnamed going into zNNN.f, put mains in
8197b7aef1SThomas Cort  *		  mainNNN.f, block datas in blkdtaNNN.f, dups in zzzNNN.f .
8297b7aef1SThomas Cort  */
8397b7aef1SThomas Cort 
8497b7aef1SThomas Cort #define BSZ 512
8597b7aef1SThomas Cort static char buf[BSZ];
8697b7aef1SThomas Cort static FILE *ifp;
8797b7aef1SThomas Cort 
8897b7aef1SThomas Cort static char x[] = "zzz000.f";
8997b7aef1SThomas Cort static char mainp[] = "main000.f";
9097b7aef1SThomas Cort static char blkp[] = "blkdta000.f";
9197b7aef1SThomas Cort 
9297b7aef1SThomas Cort __dead static void badparms(void);
9397b7aef1SThomas Cort static const char *functs(const char *);
9497b7aef1SThomas Cort static int get_line(void);
9597b7aef1SThomas Cort static void get_name(char *, int);
9697b7aef1SThomas Cort static int lend(void);
9797b7aef1SThomas Cort static int lname(char *, size_t);
9897b7aef1SThomas Cort static const char *look(const char *, const char *);
9997b7aef1SThomas Cort static int saveit(const char *);
10097b7aef1SThomas Cort static int scan_name(char *, size_t, const char *);
10197b7aef1SThomas Cort static const char *skiplab(const char *);
10297b7aef1SThomas Cort static const char *skipws(const char *);
10397b7aef1SThomas Cort 
10497b7aef1SThomas Cort struct extract {
10597b7aef1SThomas Cort 	bool found;
10697b7aef1SThomas Cort 	char *name;
10797b7aef1SThomas Cort };
10897b7aef1SThomas Cort 
10997b7aef1SThomas Cort #define MAXEXTONLY 100
11097b7aef1SThomas Cort static struct extract extonly[MAXEXTONLY];
11197b7aef1SThomas Cort static int numextonly = 0;
11297b7aef1SThomas Cort 
11397b7aef1SThomas Cort int
main(int argc,char ** argv)11497b7aef1SThomas Cort main(int argc, char **argv)
11597b7aef1SThomas Cort {
11697b7aef1SThomas Cort 	FILE *ofp;	/* output file */
11797b7aef1SThomas Cort 	int rv;		/* 1 if got card in output file, 0 otherwise */
11897b7aef1SThomas Cort 	int nflag;	/* 1 if got name of subprog., 0 otherwise */
11997b7aef1SThomas Cort 	int retval, i, ch;
12097b7aef1SThomas Cort 	char name[80];
12197b7aef1SThomas Cort 
12297b7aef1SThomas Cort 	while ((ch = getopt(argc, argv, "e:")) != -1) {
12397b7aef1SThomas Cort 		switch (ch) {
12497b7aef1SThomas Cort 		    case 'e':
12597b7aef1SThomas Cort 			if (numextonly >= MAXEXTONLY) {
12697b7aef1SThomas Cort 				errx(1, "Too many -e options");
12797b7aef1SThomas Cort 			}
12897b7aef1SThomas Cort 			extonly[numextonly].name = optarg;
12997b7aef1SThomas Cort 			extonly[numextonly].found = false;
13097b7aef1SThomas Cort 			numextonly++;
13197b7aef1SThomas Cort 			break;
13297b7aef1SThomas Cort 		    default:
13397b7aef1SThomas Cort 			badparms();
13497b7aef1SThomas Cort 			break;
13597b7aef1SThomas Cort 		}
13697b7aef1SThomas Cort 	}
13797b7aef1SThomas Cort 
13897b7aef1SThomas Cort 	if (argc > 2) {
13997b7aef1SThomas Cort 		badparms();
14097b7aef1SThomas Cort 	} else if (argc == 2) {
14197b7aef1SThomas Cort 		if ((ifp = fopen(argv[1], "r")) == NULL) {
14297b7aef1SThomas Cort 			err(1, "%s", argv[1]);
14397b7aef1SThomas Cort 		}
14497b7aef1SThomas Cort 	} else {
14597b7aef1SThomas Cort 		ifp = stdin;
14697b7aef1SThomas Cort 	}
14797b7aef1SThomas Cort 
14897b7aef1SThomas Cort 	for (;;) {
14997b7aef1SThomas Cort 		/*
15097b7aef1SThomas Cort 		 * Look for a temp file that doesn't correspond to an
15197b7aef1SThomas Cort 		 * existing file.
15297b7aef1SThomas Cort 		 */
15397b7aef1SThomas Cort 
15497b7aef1SThomas Cort 		get_name(x, 3);
15597b7aef1SThomas Cort 		ofp = fopen(x, "w");
15697b7aef1SThomas Cort 		if (ofp == NULL) {
15797b7aef1SThomas Cort 			err(1, "%s", x);
15897b7aef1SThomas Cort 		}
15997b7aef1SThomas Cort 		nflag = 0;
16097b7aef1SThomas Cort 		rv = 0;
16197b7aef1SThomas Cort 		while (get_line() > 0) {
16297b7aef1SThomas Cort 			rv = 1;
16397b7aef1SThomas Cort 			fprintf(ofp, "%s", buf);
16497b7aef1SThomas Cort 			/* look for an 'end' statement */
16597b7aef1SThomas Cort 			if (lend()) {
16697b7aef1SThomas Cort 				break;
16797b7aef1SThomas Cort 			}
16897b7aef1SThomas Cort 			/* if no name yet, try and find one */
16997b7aef1SThomas Cort 			if (nflag == 0) {
17097b7aef1SThomas Cort 				nflag = lname(name, sizeof(name));
17197b7aef1SThomas Cort 			}
17297b7aef1SThomas Cort 		}
17397b7aef1SThomas Cort 		fclose(ofp);
17497b7aef1SThomas Cort 		if (rv == 0) {
17597b7aef1SThomas Cort 			/* no lines in file, forget the file */
17697b7aef1SThomas Cort 			unlink(x);
17797b7aef1SThomas Cort 			retval = 0;
17897b7aef1SThomas Cort 			for (i = 0; i < numextonly; i++) {
17997b7aef1SThomas Cort 				if (!extonly[i].found) {
18097b7aef1SThomas Cort 					retval = 1;
18197b7aef1SThomas Cort 					warnx("%s not found", extonly[i].name);
18297b7aef1SThomas Cort 				}
18397b7aef1SThomas Cort 			}
18497b7aef1SThomas Cort 			exit(retval);
18597b7aef1SThomas Cort 		}
18697b7aef1SThomas Cort 		if (nflag) {
18797b7aef1SThomas Cort 			/* rename the file */
18897b7aef1SThomas Cort 			if (saveit(name)) {
18997b7aef1SThomas Cort 				struct stat sbuf;
19097b7aef1SThomas Cort 
19197b7aef1SThomas Cort 				if (stat(name, &sbuf) < 0) {
19297b7aef1SThomas Cort 					if (rename(x, name) < 0) {
19397b7aef1SThomas Cort 						warn("%s: rename", x);
19497b7aef1SThomas Cort 						printf("%s left in %s\n",
19597b7aef1SThomas Cort 						    name, x);
19697b7aef1SThomas Cort 					} else {
19797b7aef1SThomas Cort 						printf("%s\n", name);
19897b7aef1SThomas Cort 					}
19997b7aef1SThomas Cort 					continue;
20097b7aef1SThomas Cort 				} else if (strcmp(name, x) == 0) {
20197b7aef1SThomas Cort 					printf("%s\n", x);
20297b7aef1SThomas Cort 					continue;
20397b7aef1SThomas Cort 				}
20497b7aef1SThomas Cort 				printf("%s already exists, put in %s\n",
20597b7aef1SThomas Cort 				    name, x);
20697b7aef1SThomas Cort 				continue;
20797b7aef1SThomas Cort 			} else {
20897b7aef1SThomas Cort 				unlink(x);
20997b7aef1SThomas Cort 				continue;
21097b7aef1SThomas Cort 			}
21197b7aef1SThomas Cort 		}
21297b7aef1SThomas Cort 		if (numextonly == 0) {
21397b7aef1SThomas Cort 			printf("%s\n", x);
21497b7aef1SThomas Cort 		} else {
21597b7aef1SThomas Cort 			unlink(x);
21697b7aef1SThomas Cort 		}
21797b7aef1SThomas Cort 	}
21897b7aef1SThomas Cort }
21997b7aef1SThomas Cort 
22097b7aef1SThomas Cort static void
badparms(void)22197b7aef1SThomas Cort badparms(void)
22297b7aef1SThomas Cort {
22397b7aef1SThomas Cort 	err(1, "Usage: fsplit [-e efile] ... [file]");
22497b7aef1SThomas Cort }
22597b7aef1SThomas Cort 
22697b7aef1SThomas Cort static int
saveit(const char * name)22797b7aef1SThomas Cort saveit(const char *name)
22897b7aef1SThomas Cort {
22997b7aef1SThomas Cort 	int i;
23097b7aef1SThomas Cort 	char fname[50];
23197b7aef1SThomas Cort 	size_t fnamelen;
23297b7aef1SThomas Cort 
23397b7aef1SThomas Cort 	if (numextonly == 0) {
23497b7aef1SThomas Cort 		return 1;
23597b7aef1SThomas Cort 	}
23697b7aef1SThomas Cort 	strlcpy(fname, name, sizeof(fname));
23797b7aef1SThomas Cort 	fnamelen = strlen(fname);
23884d9c625SLionel Sambuc         /* Guaranteed by scan_name.  */
23997b7aef1SThomas Cort 	assert(fnamelen > 2);
24084d9c625SLionel Sambuc 	assert(fname[fnamelen-2] == '.');
24184d9c625SLionel Sambuc 	assert(fname[fnamelen-1] == 'f');
24297b7aef1SThomas Cort 	fname[fnamelen-2] = '\0';
24397b7aef1SThomas Cort 
24497b7aef1SThomas Cort 	for (i = 0; i < numextonly; i++) {
24597b7aef1SThomas Cort 		if (strcmp(fname, extonly[i].name) == 0) {
24697b7aef1SThomas Cort 			extonly[i].found = true;
24797b7aef1SThomas Cort 			return 1;
24897b7aef1SThomas Cort 		}
24997b7aef1SThomas Cort 	}
25097b7aef1SThomas Cort 	return 0;
25197b7aef1SThomas Cort }
25297b7aef1SThomas Cort 
25397b7aef1SThomas Cort static void
get_name(char * name,int letters)25497b7aef1SThomas Cort get_name(char *name, int letters)
25597b7aef1SThomas Cort {
25697b7aef1SThomas Cort 	struct stat sbuf;
25797b7aef1SThomas Cort 	char *ptr;
25897b7aef1SThomas Cort 
25997b7aef1SThomas Cort 	while (stat(name, &sbuf) >= 0) {
26097b7aef1SThomas Cort 		for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
26197b7aef1SThomas Cort 			(*ptr)++;
26297b7aef1SThomas Cort 			if (*ptr <= '9')
26397b7aef1SThomas Cort 				break;
26497b7aef1SThomas Cort 			*ptr = '0';
26597b7aef1SThomas Cort 		}
26697b7aef1SThomas Cort 		if (ptr < name + letters) {
267*0a6a1f1dSLionel Sambuc 			errx(1, "Ran out of file names.");
26897b7aef1SThomas Cort 		}
26997b7aef1SThomas Cort 	}
27097b7aef1SThomas Cort }
27197b7aef1SThomas Cort 
27297b7aef1SThomas Cort static int
get_line(void)27397b7aef1SThomas Cort get_line(void)
27497b7aef1SThomas Cort {
27597b7aef1SThomas Cort 	char *ptr;
27697b7aef1SThomas Cort 
27797b7aef1SThomas Cort 	for (ptr = buf; ptr < &buf[BSZ]; ) {
27897b7aef1SThomas Cort 		*ptr = getc(ifp);
27997b7aef1SThomas Cort 		if (feof(ifp))
28097b7aef1SThomas Cort 			return -1;
28197b7aef1SThomas Cort 		if (*ptr++ == '\n') {
28297b7aef1SThomas Cort 			*ptr = '\0';
28397b7aef1SThomas Cort 			return 1;
28497b7aef1SThomas Cort 		}
28597b7aef1SThomas Cort 	}
28697b7aef1SThomas Cort 	while (getc(ifp) != '\n' && feof(ifp) == 0) {
28797b7aef1SThomas Cort 		/* nothing */
28897b7aef1SThomas Cort 	}
28997b7aef1SThomas Cort 	warnx("Line truncated to %d characters.", BSZ);
29097b7aef1SThomas Cort 	return 1;
29197b7aef1SThomas Cort }
29297b7aef1SThomas Cort 
29397b7aef1SThomas Cort /*
29497b7aef1SThomas Cort  * Return 1 for 'end' alone on card (up to col. 72), 0 otherwise.
29597b7aef1SThomas Cort  */
29697b7aef1SThomas Cort static int
lend(void)29797b7aef1SThomas Cort lend(void)
29897b7aef1SThomas Cort {
29997b7aef1SThomas Cort 	const char *p;
30097b7aef1SThomas Cort 
30197b7aef1SThomas Cort 	if ((p = skiplab(buf)) == 0) {
30297b7aef1SThomas Cort 		return 0;
30397b7aef1SThomas Cort 	}
30497b7aef1SThomas Cort 	p = skipws(p);
30597b7aef1SThomas Cort 	if (*p != 'e' && *p != 'E') {
30697b7aef1SThomas Cort 		return 0;
30797b7aef1SThomas Cort 	}
30897b7aef1SThomas Cort 	p++;
30997b7aef1SThomas Cort 	p = skipws(p);
31097b7aef1SThomas Cort 	if (*p != 'n' && *p != 'N') {
31197b7aef1SThomas Cort 		return 0;
31297b7aef1SThomas Cort 	}
31397b7aef1SThomas Cort 	p++;
31497b7aef1SThomas Cort 	p = skipws(p);
31597b7aef1SThomas Cort 	if (*p != 'd' && *p != 'D') {
31697b7aef1SThomas Cort 		return 0;
31797b7aef1SThomas Cort 	}
31897b7aef1SThomas Cort 	p++;
31997b7aef1SThomas Cort 	p = skipws(p);
32097b7aef1SThomas Cort 	if (p - buf >= 72 || *p == '\n') {
32197b7aef1SThomas Cort 		return 1;
32297b7aef1SThomas Cort 	}
32397b7aef1SThomas Cort 	return 0;
32497b7aef1SThomas Cort }
32597b7aef1SThomas Cort 
32697b7aef1SThomas Cort /*
32797b7aef1SThomas Cort  * check for keywords for subprograms
32897b7aef1SThomas Cort  * return 0 if comment card, 1 if found
32997b7aef1SThomas Cort  * name and put in arg string. invent name for unnamed
33097b7aef1SThomas Cort  * block datas and main programs.
33197b7aef1SThomas Cort  */
33297b7aef1SThomas Cort static int
lname(char * s,size_t l)33397b7aef1SThomas Cort lname(char *s, size_t l)
33497b7aef1SThomas Cort {
33597b7aef1SThomas Cort #define LINESIZE 80
33697b7aef1SThomas Cort 	const char *ptr, *p;
33797b7aef1SThomas Cort 	char line[LINESIZE], *iptr = line;
33897b7aef1SThomas Cort 
33997b7aef1SThomas Cort 	/* first check for comment cards */
34097b7aef1SThomas Cort 	if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') {
34197b7aef1SThomas Cort 		return 0;
34297b7aef1SThomas Cort 	}
34397b7aef1SThomas Cort 	ptr = skipws(buf);
34497b7aef1SThomas Cort 	if (*ptr == '\n') {
34597b7aef1SThomas Cort 		return 0;
34697b7aef1SThomas Cort 	}
34797b7aef1SThomas Cort 
34897b7aef1SThomas Cort 	ptr = skiplab(buf);
34997b7aef1SThomas Cort 	if (ptr == NULL) {
35097b7aef1SThomas Cort 		return 0;
35197b7aef1SThomas Cort 	}
35297b7aef1SThomas Cort 
35397b7aef1SThomas Cort 	/*  copy to buffer and converting to lower case */
35497b7aef1SThomas Cort 	p = ptr;
35597b7aef1SThomas Cort 	while (*p && p <= &buf[71] ) {
35697b7aef1SThomas Cort 	   *iptr = tolower((unsigned char)*p);
35797b7aef1SThomas Cort 	   iptr++;
35897b7aef1SThomas Cort 	   p++;
35997b7aef1SThomas Cort 	}
36097b7aef1SThomas Cort 	*iptr = '\n';
36197b7aef1SThomas Cort 
36297b7aef1SThomas Cort 	if ((ptr = look(line, "subroutine")) != NULL ||
36397b7aef1SThomas Cort 	    (ptr = look(line, "function")) != NULL ||
36497b7aef1SThomas Cort 	    (ptr = functs(line)) != NULL) {
36597b7aef1SThomas Cort 		if (scan_name(s, l, ptr)) {
36697b7aef1SThomas Cort 			return 1;
36797b7aef1SThomas Cort 		}
36897b7aef1SThomas Cort 		strlcpy(s, x, l);
36997b7aef1SThomas Cort 	} else if ((ptr = look(line, "program")) != NULL) {
37097b7aef1SThomas Cort 		if (scan_name(s, l, ptr)) {
37197b7aef1SThomas Cort 			return 1;
37297b7aef1SThomas Cort 		}
37397b7aef1SThomas Cort 		get_name(mainp, 4);
37497b7aef1SThomas Cort 		strlcpy(s, mainp, l);
37597b7aef1SThomas Cort 	} else if ((ptr = look(line, "blockdata")) != NULL) {
37697b7aef1SThomas Cort 		if (scan_name(s, l, ptr)) {
37797b7aef1SThomas Cort 			return 1;
37897b7aef1SThomas Cort 		}
37997b7aef1SThomas Cort 		get_name(blkp, 6);
38097b7aef1SThomas Cort 		strlcpy(s, blkp, l);
38197b7aef1SThomas Cort 	} else if ((ptr = functs(line)) != NULL) {
38297b7aef1SThomas Cort 		if (scan_name(s, l, ptr)) {
38397b7aef1SThomas Cort 			return 1;
38497b7aef1SThomas Cort 		}
38597b7aef1SThomas Cort 		strlcpy(s, x, l);
38697b7aef1SThomas Cort 	} else {
38797b7aef1SThomas Cort 		get_name(mainp, 4);
38897b7aef1SThomas Cort 		strlcpy(s, mainp, l);
38997b7aef1SThomas Cort 	}
39097b7aef1SThomas Cort 	return 1;
39197b7aef1SThomas Cort }
39297b7aef1SThomas Cort 
39397b7aef1SThomas Cort static int
scan_name(char * s,size_t smax,const char * ptr)39497b7aef1SThomas Cort scan_name(char *s, size_t smax, const char *ptr)
39597b7aef1SThomas Cort {
39697b7aef1SThomas Cort 	char *sptr;
39797b7aef1SThomas Cort 	size_t sptrmax;
39897b7aef1SThomas Cort 
39997b7aef1SThomas Cort 	/* scan off the name */
40097b7aef1SThomas Cort 	ptr = skipws(ptr);
40197b7aef1SThomas Cort 	sptr = s;
40297b7aef1SThomas Cort 	sptrmax = smax - 3;
40397b7aef1SThomas Cort 	while (*ptr != '(' && *ptr != '\n') {
40497b7aef1SThomas Cort 		if (*ptr != ' ' && *ptr != '\t' && *ptr != '/') {
40597b7aef1SThomas Cort 			if (sptrmax == 0) {
40697b7aef1SThomas Cort 				/* Not sure this is the right thing, so warn */
40797b7aef1SThomas Cort 				warnx("Output name too long; truncated");
40897b7aef1SThomas Cort 				break;
40997b7aef1SThomas Cort 			}
41097b7aef1SThomas Cort 			*sptr++ = *ptr;
41197b7aef1SThomas Cort 			sptrmax--;
41297b7aef1SThomas Cort 		}
41397b7aef1SThomas Cort 		ptr++;
41497b7aef1SThomas Cort 	}
41597b7aef1SThomas Cort 
41697b7aef1SThomas Cort 	if (sptr == s) {
41797b7aef1SThomas Cort 		return 0;
41897b7aef1SThomas Cort 	}
41997b7aef1SThomas Cort 
42097b7aef1SThomas Cort 	*sptr++ = '.';
42197b7aef1SThomas Cort 	*sptr++ = 'f';
42297b7aef1SThomas Cort 	*sptr++ = '\0';
42397b7aef1SThomas Cort 	return 1;
42497b7aef1SThomas Cort }
42597b7aef1SThomas Cort 
42697b7aef1SThomas Cort /*
42797b7aef1SThomas Cort  * look for typed functions such as: real*8 function,
42897b7aef1SThomas Cort  * character*16 function, character*(*) function
42997b7aef1SThomas Cort  */
43097b7aef1SThomas Cort static const char *
functs(const char * p)43197b7aef1SThomas Cort functs(const char *p)
43297b7aef1SThomas Cort {
43397b7aef1SThomas Cort         const char *ptr;
43497b7aef1SThomas Cort 
43597b7aef1SThomas Cort         if ((ptr = look(p, "character")) != NULL ||
43697b7aef1SThomas Cort 	    (ptr = look(p, "logical")) != NULL ||
43797b7aef1SThomas Cort 	    (ptr = look(p, "real")) != NULL ||
43897b7aef1SThomas Cort 	    (ptr = look(p, "integer")) != NULL ||
43997b7aef1SThomas Cort 	    (ptr = look(p, "doubleprecision")) != NULL ||
44097b7aef1SThomas Cort 	    (ptr = look(p, "complex")) != NULL ||
44197b7aef1SThomas Cort 	    (ptr = look(p, "doublecomplex")) != NULL) {
44297b7aef1SThomas Cort                 while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
44397b7aef1SThomas Cort 		    || (*ptr >= '0' && *ptr <= '9')
44497b7aef1SThomas Cort 		    || *ptr == '(' || *ptr == ')') {
44597b7aef1SThomas Cort 			ptr++;
44697b7aef1SThomas Cort 		}
44797b7aef1SThomas Cort 		ptr = look(ptr, "function");
44897b7aef1SThomas Cort 		return ptr;
44997b7aef1SThomas Cort 	}
45097b7aef1SThomas Cort         else {
45197b7aef1SThomas Cort                 return NULL;
45297b7aef1SThomas Cort 	}
45397b7aef1SThomas Cort }
45497b7aef1SThomas Cort 
45597b7aef1SThomas Cort /*
45697b7aef1SThomas Cort  * if first 6 col. blank, return ptr to col. 7,
45797b7aef1SThomas Cort  * if blanks and then tab, return ptr after tab,
45897b7aef1SThomas Cort  * else return NULL (labelled statement, comment or continuation)
45997b7aef1SThomas Cort  */
46097b7aef1SThomas Cort static const char *
skiplab(const char * p)46197b7aef1SThomas Cort skiplab(const char *p)
46297b7aef1SThomas Cort {
46397b7aef1SThomas Cort 	const char *ptr;
46497b7aef1SThomas Cort 
46597b7aef1SThomas Cort 	for (ptr = p; ptr < &p[6]; ptr++) {
46697b7aef1SThomas Cort 		if (*ptr == ' ')
46797b7aef1SThomas Cort 			continue;
46897b7aef1SThomas Cort 		if (*ptr == '\t') {
46997b7aef1SThomas Cort 			ptr++;
47097b7aef1SThomas Cort 			break;
47197b7aef1SThomas Cort 		}
47297b7aef1SThomas Cort 		return NULL;
47397b7aef1SThomas Cort 	}
47497b7aef1SThomas Cort 	return ptr;
47597b7aef1SThomas Cort }
47697b7aef1SThomas Cort 
47797b7aef1SThomas Cort /*
47897b7aef1SThomas Cort  * return NULL if m doesn't match initial part of s;
47997b7aef1SThomas Cort  * otherwise return ptr to next char after m in s
48097b7aef1SThomas Cort  */
48197b7aef1SThomas Cort static const char *
look(const char * s,const char * m)48297b7aef1SThomas Cort look(const char *s, const char *m)
48397b7aef1SThomas Cort {
48497b7aef1SThomas Cort 	const char *sp, *mp;
48597b7aef1SThomas Cort 
48697b7aef1SThomas Cort 	sp = s; mp = m;
48797b7aef1SThomas Cort 	while (*mp) {
48897b7aef1SThomas Cort 		sp = skipws(sp);
48997b7aef1SThomas Cort 		if (*sp++ != *mp++)
49097b7aef1SThomas Cort 			return NULL;
49197b7aef1SThomas Cort 	}
49297b7aef1SThomas Cort 	return sp;
49397b7aef1SThomas Cort }
49497b7aef1SThomas Cort 
49597b7aef1SThomas Cort static const char *
skipws(const char * p)49697b7aef1SThomas Cort skipws(const char *p)
49797b7aef1SThomas Cort {
49897b7aef1SThomas Cort 	while (*p == ' ' || *p == '\t') {
49997b7aef1SThomas Cort 		p++;
50097b7aef1SThomas Cort 	}
50197b7aef1SThomas Cort 	return p;
50297b7aef1SThomas Cort }
503