Home | History | Annotate | Line # | Download | only in fsplit
fsplit.c revision 1.13
      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.13 2008/07/21 14:19:22 lukem 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 <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 #include <err.h>
     56 
     57 /*
     58  *	usage:		fsplit [-e efile] ... [file]
     59  *
     60  *	split single file containing source for several fortran programs
     61  *		and/or subprograms into files each containing one
     62  *		subprogram unit.
     63  *	each separate file will be named using the corresponding subroutine,
     64  *		function, block data or program name if one is found; otherwise
     65  *		the name will be of the form mainNNN.f or blkdtaNNN.f .
     66  *		If a file of that name exists, it is saved in a name of the
     67  *		form zzz000.f .
     68  *	If -e option is used, then only those subprograms named in the -e
     69  *		option are split off; e.g.:
     70  *			fsplit -esub1 -e sub2 prog.f
     71  *		isolates sub1 and sub2 in sub1.f and sub2.f.  The space
     72  *		after -e is optional.
     73  *
     74  *	Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
     75  *		- added comments
     76  *		- more function types: double complex, character*(*), etc.
     77  *		- fixed minor bugs
     78  *		- instead of all unnamed going into zNNN.f, put mains in
     79  *		  mainNNN.f, block datas in blkdtaNNN.f, dups in zzzNNN.f .
     80  */
     81 
     82 #define BSZ 512
     83 char buf[BSZ];
     84 FILE *ifp;
     85 char 	x[]="zzz000.f",
     86 	mainp[]="main000.f",
     87 	blkp[]="blkdta000.f";
     88 
     89 void	badparms __P((void));
     90 char   *functs __P((char *));
     91 int	getline __P((void));
     92 void	get_name __P((char *, int));
     93 int	main __P((int, char **));
     94 int	lend __P((void));
     95 int	lname __P((char *, size_t));
     96 char   *look __P((char *, char *));
     97 int	saveit __P((char *));
     98 int	scan_name __P((char *, char *));
     99 char   *skiplab __P((char *));
    100 
    101 #define TRUE 1
    102 #define FALSE 0
    103 int	extr = FALSE,
    104 	extrknt = -1,
    105 	extrfnd[100];
    106 char	extrbuf[1000],
    107 	*extrnames[100];
    108 struct stat sbuf;
    109 
    110 #define trim(p)	while (*p == ' ' || *p == '\t') p++
    111 
    112 int
    113 main(argc, argv)
    114 	int argc;
    115 	char **argv;
    116 {
    117 	FILE *ofp;	/* output file */
    118 	int rv;		/* 1 if got card in output file, 0 otherwise */
    119 	char *ptr;
    120 	int nflag;	/* 1 if got name of subprog., 0 otherwise */
    121 	int retval, i;
    122 	char name[20], *extrptr = extrbuf;
    123 
    124 	/*  scan -e options */
    125 	while ( argc > 1  && argv[1][0] == '-' && argv[1][1] == 'e') {
    126 		extr = TRUE;
    127 		ptr = argv[1] + 2;
    128 		if(!*ptr) {
    129 			argc--;
    130 			argv++;
    131 			if(argc <= 1) badparms();
    132 			ptr = argv[1];
    133 		}
    134 		extrknt = extrknt + 1;
    135 		extrnames[extrknt] = extrptr;
    136 		extrfnd[extrknt] = FALSE;
    137 		while(*ptr) *extrptr++ = *ptr++;
    138 		*extrptr++ = 0;
    139 		argc--;
    140 		argv++;
    141 	}
    142 
    143 	if (argc > 2)
    144 		badparms();
    145 	else if (argc == 2) {
    146 		if ((ifp = fopen(argv[1], "r")) == NULL) {
    147 			fprintf(stderr, "fsplit: cannot open %s\n", argv[1]);
    148 			exit(1);
    149 		}
    150 	}
    151 	else
    152 		ifp = stdin;
    153     for(;;) {
    154 	/* look for a temp file that doesn't correspond to an existing file */
    155 	get_name(x, 3);
    156 	ofp = fopen(x, "w");
    157 	if (ofp == NULL) {
    158 		err(1, "%s", x);
    159 	}
    160 	nflag = 0;
    161 	rv = 0;
    162 	while (getline() > 0) {
    163 		rv = 1;
    164 		fprintf(ofp, "%s", buf);
    165 		if (lend())		/* look for an 'end' statement */
    166 			break;
    167 		if (nflag == 0)		/* if no name yet, try and find one */
    168 			nflag = lname(name, sizeof(name));
    169 	}
    170 	fclose(ofp);
    171 	if (rv == 0) {			/* no lines in file, forget the file */
    172 		unlink(x);
    173 		retval = 0;
    174 		for ( i = 0; i <= extrknt; i++ )
    175 			if(!extrfnd[i]) {
    176 				retval = 1;
    177 				fprintf( stderr, "fsplit: %s not found\n",
    178 					extrnames[i]);
    179 			}
    180 		exit( retval );
    181 	}
    182 	if (nflag) {			/* rename the file */
    183 		if(saveit(name)) {
    184 			if (stat(name, &sbuf) < 0 ) {
    185 				link(x, name);
    186 				unlink(x);
    187 				printf("%s\n", name);
    188 				continue;
    189 			} else if (strcmp(name, x) == 0) {
    190 				printf("%s\n", x);
    191 				continue;
    192 			}
    193 			printf("%s already exists, put in %s\n", name, x);
    194 			continue;
    195 		} else
    196 			unlink(x);
    197 			continue;
    198 	}
    199 	if(!extr)
    200 		printf("%s\n", x);
    201 	else
    202 		unlink(x);
    203     }
    204 }
    205 
    206 void
    207 badparms()
    208 {
    209 	fprintf(stderr, "fsplit: usage:  fsplit [-e efile] ... [file] \n");
    210 	exit(1);
    211 }
    212 
    213 int
    214 saveit(name)
    215 	char *name;
    216 {
    217 	int i;
    218 	char	fname[50],
    219 		*fptr = fname;
    220 
    221 	if(!extr) return(1);
    222 	while(*name) *fptr++ = *name++;
    223 	*--fptr = 0;
    224 	*--fptr = 0;
    225 	for ( i=0 ; i<=extrknt; i++ )
    226 		if( strcmp(fname, extrnames[i]) == 0 ) {
    227 			extrfnd[i] = TRUE;
    228 			return(1);
    229 		}
    230 	return(0);
    231 }
    232 
    233 void
    234 get_name(name, letters)
    235 	char *name;
    236 	int letters;
    237 {
    238 	char *ptr;
    239 
    240 	while (stat(name, &sbuf) >= 0) {
    241 		for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
    242 			(*ptr)++;
    243 			if (*ptr <= '9')
    244 				break;
    245 			*ptr = '0';
    246 		}
    247 		if(ptr < name + letters) {
    248 			fprintf( stderr, "fsplit: ran out of file names\n");
    249 			exit(1);
    250 		}
    251 	}
    252 }
    253 
    254 int
    255 getline()
    256 {
    257 	char *ptr;
    258 
    259 	for (ptr = buf; ptr < &buf[BSZ]; ) {
    260 		*ptr = getc(ifp);
    261 		if (feof(ifp))
    262 			return (-1);
    263 		if (*ptr++ == '\n') {
    264 			*ptr = 0;
    265 			return (1);
    266 		}
    267 	}
    268 	while (getc(ifp) != '\n' && feof(ifp) == 0) ;
    269 	fprintf(stderr, "line truncated to %d characters\n", BSZ);
    270 	return (1);
    271 }
    272 
    273 /* return 1 for 'end' alone on card (up to col. 72),  0 otherwise */
    274 int
    275 lend()
    276 {
    277 	char *p;
    278 
    279 	if ((p = skiplab(buf)) == 0)
    280 		return (0);
    281 	trim(p);
    282 	if (*p != 'e' && *p != 'E') return(0);
    283 	p++;
    284 	trim(p);
    285 	if (*p != 'n' && *p != 'N') return(0);
    286 	p++;
    287 	trim(p);
    288 	if (*p != 'd' && *p != 'D') return(0);
    289 	p++;
    290 	trim(p);
    291 	if (p - buf >= 72 || *p == '\n')
    292 		return (1);
    293 	return (0);
    294 }
    295 
    296 /*		check for keywords for subprograms
    297 		return 0 if comment card, 1 if found
    298 		name and put in arg string. invent name for unnamed
    299 		block datas and main programs.		*/
    300 
    301 int
    302 lname(s, l)
    303 	char *s;
    304 	size_t l;
    305 {
    306 #	define LINESIZE 80
    307 	char *ptr, *p;
    308 	char	line[LINESIZE], *iptr = line;
    309 
    310 	/* first check for comment cards */
    311 	if(buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') return(0);
    312 	ptr = buf;
    313 	while (*ptr == ' ' || *ptr == '\t') ptr++;
    314 	if(*ptr == '\n') return(0);
    315 
    316 
    317 	ptr = skiplab(buf);
    318 	if (ptr == 0)
    319 		return (0);
    320 
    321 
    322 	/*  copy to buffer and converting to lower case */
    323 	p = ptr;
    324 	while (*p && p <= &buf[71] ) {
    325 	   *iptr = tolower((unsigned char)*p);
    326 	   iptr++;
    327 	   p++;
    328 	}
    329 	*iptr = '\n';
    330 
    331 	if ((ptr = look(line, "subroutine")) != 0 ||
    332 	    (ptr = look(line, "function")) != 0 ||
    333 	    (ptr = functs(line)) != 0) {
    334 		if(scan_name(s, ptr)) return(1);
    335 		strlcpy(s, x, l);
    336 	} else if((ptr = look(line, "program")) != 0) {
    337 		if(scan_name(s, ptr)) return(1);
    338 		get_name(mainp, 4);
    339 		strlcpy(s, mainp, l);
    340 	} else if((ptr = look(line, "blockdata")) != 0) {
    341 		if(scan_name(s, ptr)) return(1);
    342 		get_name( blkp, 6);
    343 		strlcpy(s, blkp, l);
    344 	} else if((ptr = functs(line)) != 0) {
    345 		if(scan_name(s, ptr)) return(1);
    346 		strlcpy(s, x, l);
    347 	} else {
    348 		get_name(mainp, 4);
    349 		strlcpy(s, mainp, l);
    350 	}
    351 	return(1);
    352 }
    353 
    354 int
    355 scan_name(s, ptr)
    356 	char *s, *ptr;
    357 {
    358 	char *sptr;
    359 
    360 	/* scan off the name */
    361 	trim(ptr);
    362 	sptr = s;
    363 	while (*ptr != '(' && *ptr != '\n') {
    364 		if (*ptr != ' ' && *ptr != '\t')
    365 			*sptr++ = *ptr;
    366 		ptr++;
    367 	}
    368 
    369 	if (sptr == s) return(0);
    370 
    371 	*sptr++ = '.';
    372 	*sptr++ = 'f';
    373 	*sptr++ = 0;
    374 	return(1);
    375 }
    376 
    377 char *
    378 functs(p)
    379 	char *p;
    380 {
    381         char *ptr;
    382 
    383 /*      look for typed functions such as: real*8 function,
    384                 character*16 function, character*(*) function  */
    385 
    386         if((ptr = look(p,"character")) != 0 ||
    387            (ptr = look(p,"logical")) != 0 ||
    388            (ptr = look(p,"real")) != 0 ||
    389            (ptr = look(p,"integer")) != 0 ||
    390            (ptr = look(p,"doubleprecision")) != 0 ||
    391            (ptr = look(p,"complex")) != 0 ||
    392            (ptr = look(p,"doublecomplex")) != 0 ) {
    393                 while ( *ptr == ' ' || *ptr == '\t' || *ptr == '*'
    394 			|| (*ptr >= '0' && *ptr <= '9')
    395 			|| *ptr == '(' || *ptr == ')') ptr++;
    396 		ptr = look(ptr,"function");
    397 		return(ptr);
    398 	}
    399         else
    400                 return(0);
    401 }
    402 
    403 /* 	if first 6 col. blank, return ptr to col. 7,
    404 	if blanks and then tab, return ptr after tab,
    405 	else return 0 (labelled statement, comment or continuation */
    406 
    407 char *
    408 skiplab(p)
    409 	char *p;
    410 {
    411 	char *ptr;
    412 
    413 	for (ptr = p; ptr < &p[6]; ptr++) {
    414 		if (*ptr == ' ')
    415 			continue;
    416 		if (*ptr == '\t') {
    417 			ptr++;
    418 			break;
    419 		}
    420 		return (0);
    421 	}
    422 	return (ptr);
    423 }
    424 
    425 /* 	return 0 if m doesn't match initial part of s;
    426 	otherwise return ptr to next char after m in s */
    427 
    428 char *
    429 look(s, m)
    430 	char *s, *m;
    431 {
    432 	char *sp, *mp;
    433 
    434 	sp = s; mp = m;
    435 	while (*mp) {
    436 		trim(sp);
    437 		if (*sp++ != *mp++)
    438 			return (0);
    439 	}
    440 	return (sp);
    441 }
    442