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