Home | History | Annotate | Line # | Download | only in fsplit
fsplit.c revision 1.19
      1 /*
      2  * Copyright (c) 1983, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Asa Romberger and Jerry Berkman.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     36  The Regents of the University of California.  All rights reserved.");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "from: @(#)fsplit.c	8.1 (Berkeley) 6/6/93";
     42 #else
     43 __RCSID("$NetBSD: fsplit.c,v 1.19 2008/11/16 04:27:31 dholland Exp $");
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <sys/types.h>
     48 #include <sys/stat.h>
     49 
     50 #include <ctype.h>
     51 #include <err.h>
     52 #include <stdbool.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 /*
     59  *	usage:		fsplit [-e efile] ... [file]
     60  *
     61  *	split single file containing source for several fortran programs
     62  *		and/or subprograms into files each containing one
     63  *		subprogram unit.
     64  *	each separate file will be named using the corresponding subroutine,
     65  *		function, block data or program name if one is found; otherwise
     66  *		the name will be of the form mainNNN.f or blkdtaNNN.f .
     67  *		If a file of that name exists, it is saved in a name of the
     68  *		form zzz000.f .
     69  *	If -e option is used, then only those subprograms named in the -e
     70  *		option are split off; e.g.:
     71  *			fsplit -esub1 -e sub2 prog.f
     72  *		isolates sub1 and sub2 in sub1.f and sub2.f.  The space
     73  *		after -e is optional.
     74  *
     75  *	Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
     76  *		- added comments
     77  *		- more function types: double complex, character*(*), etc.
     78  *		- fixed minor bugs
     79  *		- instead of all unnamed going into zNNN.f, put mains in
     80  *		  mainNNN.f, block datas in blkdtaNNN.f, dups in zzzNNN.f .
     81  */
     82 
     83 #define BSZ 512
     84 static char buf[BSZ];
     85 static FILE *ifp;
     86 
     87 static char x[] = "zzz000.f";
     88 static char mainp[] = "main000.f";
     89 static char blkp[] = "blkdta000.f";
     90 
     91 static void badparms(void);
     92 static const char *functs(const char *);
     93 static int getline(void);
     94 static void get_name(char *, int);
     95 static int lend(void);
     96 static int lname(char *, size_t);
     97 static const char *look(const char *, const char *);
     98 static int saveit(const char *);
     99 static int scan_name(char *, const char *);
    100 static const char *skiplab(const char *);
    101 static const char *skipws(const char *);
    102 
    103 static bool	extr = false;
    104 static int	extrknt = -1;
    105 static bool	extrfnd[100];
    106 static char	extrbuf[1000];
    107 static char	*extrnames[100];
    108 
    109 int
    110 main(int argc, char **argv)
    111 {
    112 	FILE *ofp;	/* output file */
    113 	int rv;		/* 1 if got card in output file, 0 otherwise */
    114 	char *ptr;
    115 	int nflag;	/* 1 if got name of subprog., 0 otherwise */
    116 	int retval, i;
    117 	char name[20], *extrptr = extrbuf;
    118 
    119 	/*  scan -e options */
    120 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') {
    121 		extr = true;
    122 		ptr = argv[1] + 2;
    123 		if (!*ptr) {
    124 			argc--;
    125 			argv++;
    126 			if (argc <= 1) {
    127 				badparms();
    128 			}
    129 			ptr = argv[1];
    130 		}
    131 		extrknt = extrknt + 1;
    132 		extrnames[extrknt] = extrptr;
    133 		extrfnd[extrknt] = false;
    134 		while (*ptr) {
    135 			*extrptr++ = *ptr++;
    136 		}
    137 		*extrptr++ = '\0';
    138 		argc--;
    139 		argv++;
    140 	}
    141 
    142 	if (argc > 2) {
    143 		badparms();
    144 	} else if (argc == 2) {
    145 		if ((ifp = fopen(argv[1], "r")) == NULL) {
    146 			err(1, "%s", argv[1]);
    147 		}
    148 	} else {
    149 		ifp = stdin;
    150 	}
    151 
    152 	for (;;) {
    153 		/*
    154 		 * Look for a temp file that doesn't correspond to an
    155 		 * existing file.
    156 		 */
    157 
    158 		get_name(x, 3);
    159 		ofp = fopen(x, "w");
    160 		if (ofp == NULL) {
    161 			err(1, "%s", x);
    162 		}
    163 		nflag = 0;
    164 		rv = 0;
    165 		while (getline() > 0) {
    166 			rv = 1;
    167 			fprintf(ofp, "%s", buf);
    168 			/* look for an 'end' statement */
    169 			if (lend()) {
    170 				break;
    171 			}
    172 			/* if no name yet, try and find one */
    173 			if (nflag == 0) {
    174 				nflag = lname(name, sizeof(name));
    175 			}
    176 		}
    177 		fclose(ofp);
    178 		if (rv == 0) {
    179 			/* no lines in file, forget the file */
    180 			unlink(x);
    181 			retval = 0;
    182 			for (i = 0; i <= extrknt; i++) {
    183 				if (!extrfnd[i]) {
    184 					retval = 1;
    185 					warnx("%s not found\n", extrnames[i]);
    186 				}
    187 			}
    188 			exit(retval);
    189 		}
    190 		if (nflag) {
    191 			/* rename the file */
    192 			if (saveit(name)) {
    193 				struct stat sbuf;
    194 
    195 				if (stat(name, &sbuf) < 0) {
    196 					link(x, name);
    197 					unlink(x);
    198 					printf("%s\n", name);
    199 					continue;
    200 				} else if (strcmp(name, x) == 0) {
    201 					printf("%s\n", x);
    202 					continue;
    203 				}
    204 				printf("%s already exists, put in %s\n",
    205 				    name, x);
    206 				continue;
    207 			} else {
    208 				unlink(x);
    209 				continue;
    210 			}
    211 		}
    212 		if (!extr) {
    213 			printf("%s\n", x);
    214 		} else {
    215 			unlink(x);
    216 		}
    217 	}
    218 }
    219 
    220 static void
    221 badparms(void)
    222 {
    223 	err(1, "Usage: fsplit [-e efile] ... [file]");
    224 }
    225 
    226 static int
    227 saveit(const char *name)
    228 {
    229 	int i;
    230 	char fname[50];
    231 	char *fptr = fname;
    232 
    233 	if (!extr) {
    234 		return 1;
    235 	}
    236 	while (*name) {
    237 		*fptr++ = *name++;
    238 	}
    239 	*--fptr = '\0';
    240 	*--fptr = '\0';
    241 	for (i = 0; i <= extrknt; i++) {
    242 		if (strcmp(fname, extrnames[i]) == 0) {
    243 			extrfnd[i] = true;
    244 			return 1;
    245 		}
    246 	}
    247 	return 0;
    248 }
    249 
    250 static void
    251 get_name(char *name, int letters)
    252 {
    253 	struct stat sbuf;
    254 	char *ptr;
    255 
    256 	while (stat(name, &sbuf) >= 0) {
    257 		for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
    258 			(*ptr)++;
    259 			if (*ptr <= '9')
    260 				break;
    261 			*ptr = '0';
    262 		}
    263 		if (ptr < name + letters) {
    264 			errx(1, "Ran out of file names.\n");
    265 		}
    266 	}
    267 }
    268 
    269 static int
    270 getline(void)
    271 {
    272 	char *ptr;
    273 
    274 	for (ptr = buf; ptr < &buf[BSZ]; ) {
    275 		*ptr = getc(ifp);
    276 		if (feof(ifp))
    277 			return -1;
    278 		if (*ptr++ == '\n') {
    279 			*ptr = '\0';
    280 			return 1;
    281 		}
    282 	}
    283 	while (getc(ifp) != '\n' && feof(ifp) == 0) {
    284 		/* nothing */
    285 	}
    286 	warnx("Line truncated to %d characters.", BSZ);
    287 	return 1;
    288 }
    289 
    290 /*
    291  * Return 1 for 'end' alone on card (up to col. 72), 0 otherwise.
    292  */
    293 static int
    294 lend(void)
    295 {
    296 	const char *p;
    297 
    298 	if ((p = skiplab(buf)) == 0) {
    299 		return 0;
    300 	}
    301 	p = skipws(p);
    302 	if (*p != 'e' && *p != 'E') {
    303 		return 0;
    304 	}
    305 	p++;
    306 	p = skipws(p);
    307 	if (*p != 'n' && *p != 'N') {
    308 		return 0;
    309 	}
    310 	p++;
    311 	p = skipws(p);
    312 	if (*p != 'd' && *p != 'D') {
    313 		return 0;
    314 	}
    315 	p++;
    316 	p = skipws(p);
    317 	if (p - buf >= 72 || *p == '\n') {
    318 		return 1;
    319 	}
    320 	return 0;
    321 }
    322 
    323 /*
    324  * check for keywords for subprograms
    325  * return 0 if comment card, 1 if found
    326  * name and put in arg string. invent name for unnamed
    327  * block datas and main programs.
    328  */
    329 static int
    330 lname(char *s, size_t l)
    331 {
    332 #define LINESIZE 80
    333 	const char *ptr, *p;
    334 	char line[LINESIZE], *iptr = line;
    335 
    336 	/* first check for comment cards */
    337 	if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') {
    338 		return 0;
    339 	}
    340 	ptr = buf;
    341 	while (*ptr == ' ' || *ptr == '\t') {
    342 		ptr++;
    343 	}
    344 	if (*ptr == '\n') {
    345 		return 0;
    346 	}
    347 
    348 	ptr = skiplab(buf);
    349 	if (ptr == NULL) {
    350 		return 0;
    351 	}
    352 
    353 	/*  copy to buffer and converting to lower case */
    354 	p = ptr;
    355 	while (*p && p <= &buf[71] ) {
    356 	   *iptr = tolower((unsigned char)*p);
    357 	   iptr++;
    358 	   p++;
    359 	}
    360 	*iptr = '\n';
    361 
    362 	if ((ptr = look(line, "subroutine")) != NULL ||
    363 	    (ptr = look(line, "function")) != NULL ||
    364 	    (ptr = functs(line)) != NULL) {
    365 		if (scan_name(s, ptr)) {
    366 			return 1;
    367 		}
    368 		strlcpy(s, x, l);
    369 	} else if ((ptr = look(line, "program")) != NULL) {
    370 		if (scan_name(s, ptr)) {
    371 			return 1;
    372 		}
    373 		get_name(mainp, 4);
    374 		strlcpy(s, mainp, l);
    375 	} else if ((ptr = look(line, "blockdata")) != NULL) {
    376 		if (scan_name(s, ptr)) {
    377 			return 1;
    378 		}
    379 		get_name(blkp, 6);
    380 		strlcpy(s, blkp, l);
    381 	} else if ((ptr = functs(line)) != NULL) {
    382 		if (scan_name(s, ptr)) {
    383 			return 1;
    384 		}
    385 		strlcpy(s, x, l);
    386 	} else {
    387 		get_name(mainp, 4);
    388 		strlcpy(s, mainp, l);
    389 	}
    390 	return 1;
    391 }
    392 
    393 static int
    394 scan_name(char *s, const char *ptr)
    395 {
    396 	char *sptr;
    397 
    398 	/* scan off the name */
    399 	ptr = skipws(ptr);
    400 	sptr = s;
    401 	while (*ptr != '(' && *ptr != '\n') {
    402 		if (*ptr != ' ' && *ptr != '\t')
    403 			*sptr++ = *ptr;
    404 		ptr++;
    405 	}
    406 
    407 	if (sptr == s) {
    408 		return 0;
    409 	}
    410 
    411 	*sptr++ = '.';
    412 	*sptr++ = 'f';
    413 	*sptr++ = '\0';
    414 	return 1;
    415 }
    416 
    417 /*
    418  * look for typed functions such as: real*8 function,
    419  * character*16 function, character*(*) function
    420  */
    421 static const char *
    422 functs(const char *p)
    423 {
    424         const char *ptr;
    425 
    426         if ((ptr = look(p, "character")) != NULL ||
    427 	    (ptr = look(p, "logical")) != NULL ||
    428 	    (ptr = look(p, "real")) != NULL ||
    429 	    (ptr = look(p, "integer")) != NULL ||
    430 	    (ptr = look(p, "doubleprecision")) != NULL ||
    431 	    (ptr = look(p, "complex")) != NULL ||
    432 	    (ptr = look(p, "doublecomplex")) != NULL) {
    433                 while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
    434 		    || (*ptr >= '0' && *ptr <= '9')
    435 		    || *ptr == '(' || *ptr == ')') {
    436 			ptr++;
    437 		}
    438 		ptr = look(ptr, "function");
    439 		return ptr;
    440 	}
    441         else {
    442                 return NULL;
    443 	}
    444 }
    445 
    446 /*
    447  * if first 6 col. blank, return ptr to col. 7,
    448  * if blanks and then tab, return ptr after tab,
    449  * else return NULL (labelled statement, comment or continuation)
    450  */
    451 static const char *
    452 skiplab(const char *p)
    453 {
    454 	const char *ptr;
    455 
    456 	for (ptr = p; ptr < &p[6]; ptr++) {
    457 		if (*ptr == ' ')
    458 			continue;
    459 		if (*ptr == '\t') {
    460 			ptr++;
    461 			break;
    462 		}
    463 		return NULL;
    464 	}
    465 	return ptr;
    466 }
    467 
    468 /*
    469  * return NULL if m doesn't match initial part of s;
    470  * otherwise return ptr to next char after m in s
    471  */
    472 static const char *
    473 look(const char *s, const char *m)
    474 {
    475 	const char *sp, *mp;
    476 
    477 	sp = s; mp = m;
    478 	while (*mp) {
    479 		sp = skipws(sp);
    480 		if (*sp++ != *mp++)
    481 			return NULL;
    482 	}
    483 	return sp;
    484 }
    485 
    486 static const char *
    487 skipws(const char *p)
    488 {
    489 	while (*p == ' ' || *p == '\t') {
    490 		p++;
    491 	}
    492 	return p;
    493 }
    494