Home | History | Annotate | Line # | Download | only in fsplit
fsplit.c revision 1.21
      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.21 2008/11/16 04:51:27 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 struct extract {
    104 	bool found;
    105 	char *name;
    106 };
    107 
    108 static int	numextonly = 0;
    109 static struct extract extonly[100];
    110 static char	extrbuf[1000];
    111 
    112 int
    113 main(int argc, char **argv)
    114 {
    115 	FILE *ofp;	/* output file */
    116 	int rv;		/* 1 if got card in output file, 0 otherwise */
    117 	char *ptr;
    118 	int nflag;	/* 1 if got name of subprog., 0 otherwise */
    119 	int retval, i;
    120 	char name[20], *extrptr = extrbuf;
    121 
    122 	/*  scan -e options */
    123 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') {
    124 		ptr = argv[1] + 2;
    125 		if (!*ptr) {
    126 			argc--;
    127 			argv++;
    128 			if (argc <= 1) {
    129 				badparms();
    130 			}
    131 			ptr = argv[1];
    132 		}
    133 		extonly[numextonly].name = extrptr;
    134 		extonly[numextonly].found = false;
    135 		numextonly++;
    136 		while (*ptr) {
    137 			*extrptr++ = *ptr++;
    138 		}
    139 		*extrptr++ = '\0';
    140 		argc--;
    141 		argv++;
    142 	}
    143 
    144 	if (argc > 2) {
    145 		badparms();
    146 	} else if (argc == 2) {
    147 		if ((ifp = fopen(argv[1], "r")) == NULL) {
    148 			err(1, "%s", argv[1]);
    149 		}
    150 	} else {
    151 		ifp = stdin;
    152 	}
    153 
    154 	for (;;) {
    155 		/*
    156 		 * Look for a temp file that doesn't correspond to an
    157 		 * existing file.
    158 		 */
    159 
    160 		get_name(x, 3);
    161 		ofp = fopen(x, "w");
    162 		if (ofp == NULL) {
    163 			err(1, "%s", x);
    164 		}
    165 		nflag = 0;
    166 		rv = 0;
    167 		while (getline() > 0) {
    168 			rv = 1;
    169 			fprintf(ofp, "%s", buf);
    170 			/* look for an 'end' statement */
    171 			if (lend()) {
    172 				break;
    173 			}
    174 			/* if no name yet, try and find one */
    175 			if (nflag == 0) {
    176 				nflag = lname(name, sizeof(name));
    177 			}
    178 		}
    179 		fclose(ofp);
    180 		if (rv == 0) {
    181 			/* no lines in file, forget the file */
    182 			unlink(x);
    183 			retval = 0;
    184 			for (i = 0; i < numextonly; i++) {
    185 				if (!extonly[i].found) {
    186 					retval = 1;
    187 					warnx("%s not found", extonly[i].name);
    188 				}
    189 			}
    190 			exit(retval);
    191 		}
    192 		if (nflag) {
    193 			/* rename the file */
    194 			if (saveit(name)) {
    195 				struct stat sbuf;
    196 
    197 				if (stat(name, &sbuf) < 0) {
    198 					link(x, name);
    199 					unlink(x);
    200 					printf("%s\n", name);
    201 					continue;
    202 				} else if (strcmp(name, x) == 0) {
    203 					printf("%s\n", x);
    204 					continue;
    205 				}
    206 				printf("%s already exists, put in %s\n",
    207 				    name, x);
    208 				continue;
    209 			} else {
    210 				unlink(x);
    211 				continue;
    212 			}
    213 		}
    214 		if (numextonly == 0) {
    215 			printf("%s\n", x);
    216 		} else {
    217 			unlink(x);
    218 		}
    219 	}
    220 }
    221 
    222 static void
    223 badparms(void)
    224 {
    225 	err(1, "Usage: fsplit [-e efile] ... [file]");
    226 }
    227 
    228 static int
    229 saveit(const char *name)
    230 {
    231 	int i;
    232 	char fname[50];
    233 	char *fptr = fname;
    234 
    235 	if (numextonly == 0) {
    236 		return 1;
    237 	}
    238 	while (*name) {
    239 		*fptr++ = *name++;
    240 	}
    241 	*--fptr = '\0';
    242 	*--fptr = '\0';
    243 	for (i = 0; i < numextonly; i++) {
    244 		if (strcmp(fname, extonly[i].name) == 0) {
    245 			extonly[i].found = true;
    246 			return 1;
    247 		}
    248 	}
    249 	return 0;
    250 }
    251 
    252 static void
    253 get_name(char *name, int letters)
    254 {
    255 	struct stat sbuf;
    256 	char *ptr;
    257 
    258 	while (stat(name, &sbuf) >= 0) {
    259 		for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
    260 			(*ptr)++;
    261 			if (*ptr <= '9')
    262 				break;
    263 			*ptr = '0';
    264 		}
    265 		if (ptr < name + letters) {
    266 			errx(1, "Ran out of file names.\n");
    267 		}
    268 	}
    269 }
    270 
    271 static int
    272 getline(void)
    273 {
    274 	char *ptr;
    275 
    276 	for (ptr = buf; ptr < &buf[BSZ]; ) {
    277 		*ptr = getc(ifp);
    278 		if (feof(ifp))
    279 			return -1;
    280 		if (*ptr++ == '\n') {
    281 			*ptr = '\0';
    282 			return 1;
    283 		}
    284 	}
    285 	while (getc(ifp) != '\n' && feof(ifp) == 0) {
    286 		/* nothing */
    287 	}
    288 	warnx("Line truncated to %d characters.", BSZ);
    289 	return 1;
    290 }
    291 
    292 /*
    293  * Return 1 for 'end' alone on card (up to col. 72), 0 otherwise.
    294  */
    295 static int
    296 lend(void)
    297 {
    298 	const char *p;
    299 
    300 	if ((p = skiplab(buf)) == 0) {
    301 		return 0;
    302 	}
    303 	p = skipws(p);
    304 	if (*p != 'e' && *p != 'E') {
    305 		return 0;
    306 	}
    307 	p++;
    308 	p = skipws(p);
    309 	if (*p != 'n' && *p != 'N') {
    310 		return 0;
    311 	}
    312 	p++;
    313 	p = skipws(p);
    314 	if (*p != 'd' && *p != 'D') {
    315 		return 0;
    316 	}
    317 	p++;
    318 	p = skipws(p);
    319 	if (p - buf >= 72 || *p == '\n') {
    320 		return 1;
    321 	}
    322 	return 0;
    323 }
    324 
    325 /*
    326  * check for keywords for subprograms
    327  * return 0 if comment card, 1 if found
    328  * name and put in arg string. invent name for unnamed
    329  * block datas and main programs.
    330  */
    331 static int
    332 lname(char *s, size_t l)
    333 {
    334 #define LINESIZE 80
    335 	const char *ptr, *p;
    336 	char line[LINESIZE], *iptr = line;
    337 
    338 	/* first check for comment cards */
    339 	if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') {
    340 		return 0;
    341 	}
    342 	ptr = buf;
    343 	while (*ptr == ' ' || *ptr == '\t') {
    344 		ptr++;
    345 	}
    346 	if (*ptr == '\n') {
    347 		return 0;
    348 	}
    349 
    350 	ptr = skiplab(buf);
    351 	if (ptr == NULL) {
    352 		return 0;
    353 	}
    354 
    355 	/*  copy to buffer and converting to lower case */
    356 	p = ptr;
    357 	while (*p && p <= &buf[71] ) {
    358 	   *iptr = tolower((unsigned char)*p);
    359 	   iptr++;
    360 	   p++;
    361 	}
    362 	*iptr = '\n';
    363 
    364 	if ((ptr = look(line, "subroutine")) != NULL ||
    365 	    (ptr = look(line, "function")) != NULL ||
    366 	    (ptr = functs(line)) != NULL) {
    367 		if (scan_name(s, ptr)) {
    368 			return 1;
    369 		}
    370 		strlcpy(s, x, l);
    371 	} else if ((ptr = look(line, "program")) != NULL) {
    372 		if (scan_name(s, ptr)) {
    373 			return 1;
    374 		}
    375 		get_name(mainp, 4);
    376 		strlcpy(s, mainp, l);
    377 	} else if ((ptr = look(line, "blockdata")) != NULL) {
    378 		if (scan_name(s, ptr)) {
    379 			return 1;
    380 		}
    381 		get_name(blkp, 6);
    382 		strlcpy(s, blkp, l);
    383 	} else if ((ptr = functs(line)) != NULL) {
    384 		if (scan_name(s, ptr)) {
    385 			return 1;
    386 		}
    387 		strlcpy(s, x, l);
    388 	} else {
    389 		get_name(mainp, 4);
    390 		strlcpy(s, mainp, l);
    391 	}
    392 	return 1;
    393 }
    394 
    395 static int
    396 scan_name(char *s, const char *ptr)
    397 {
    398 	char *sptr;
    399 
    400 	/* scan off the name */
    401 	ptr = skipws(ptr);
    402 	sptr = s;
    403 	while (*ptr != '(' && *ptr != '\n') {
    404 		if (*ptr != ' ' && *ptr != '\t')
    405 			*sptr++ = *ptr;
    406 		ptr++;
    407 	}
    408 
    409 	if (sptr == s) {
    410 		return 0;
    411 	}
    412 
    413 	*sptr++ = '.';
    414 	*sptr++ = 'f';
    415 	*sptr++ = '\0';
    416 	return 1;
    417 }
    418 
    419 /*
    420  * look for typed functions such as: real*8 function,
    421  * character*16 function, character*(*) function
    422  */
    423 static const char *
    424 functs(const char *p)
    425 {
    426         const char *ptr;
    427 
    428         if ((ptr = look(p, "character")) != NULL ||
    429 	    (ptr = look(p, "logical")) != NULL ||
    430 	    (ptr = look(p, "real")) != NULL ||
    431 	    (ptr = look(p, "integer")) != NULL ||
    432 	    (ptr = look(p, "doubleprecision")) != NULL ||
    433 	    (ptr = look(p, "complex")) != NULL ||
    434 	    (ptr = look(p, "doublecomplex")) != NULL) {
    435                 while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
    436 		    || (*ptr >= '0' && *ptr <= '9')
    437 		    || *ptr == '(' || *ptr == ')') {
    438 			ptr++;
    439 		}
    440 		ptr = look(ptr, "function");
    441 		return ptr;
    442 	}
    443         else {
    444                 return NULL;
    445 	}
    446 }
    447 
    448 /*
    449  * if first 6 col. blank, return ptr to col. 7,
    450  * if blanks and then tab, return ptr after tab,
    451  * else return NULL (labelled statement, comment or continuation)
    452  */
    453 static const char *
    454 skiplab(const char *p)
    455 {
    456 	const char *ptr;
    457 
    458 	for (ptr = p; ptr < &p[6]; ptr++) {
    459 		if (*ptr == ' ')
    460 			continue;
    461 		if (*ptr == '\t') {
    462 			ptr++;
    463 			break;
    464 		}
    465 		return NULL;
    466 	}
    467 	return ptr;
    468 }
    469 
    470 /*
    471  * return NULL if m doesn't match initial part of s;
    472  * otherwise return ptr to next char after m in s
    473  */
    474 static const char *
    475 look(const char *s, const char *m)
    476 {
    477 	const char *sp, *mp;
    478 
    479 	sp = s; mp = m;
    480 	while (*mp) {
    481 		sp = skipws(sp);
    482 		if (*sp++ != *mp++)
    483 			return NULL;
    484 	}
    485 	return sp;
    486 }
    487 
    488 static const char *
    489 skipws(const char *p)
    490 {
    491 	while (*p == ' ' || *p == '\t') {
    492 		p++;
    493 	}
    494 	return p;
    495 }
    496