2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Asa Romberger and Jerry Berkman.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char copyright[] =
39 "@(#) Copyright (c) 1983, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
44 static char sccsid[] = "@(#)fsplit.c 8.1 (Berkeley) 6/6/93";
49 #include <sys/types.h>
53 * usage: fsplit [-e efile] ... [file]
55 * split single file containing source for several fortran programs
56 * and/or subprograms into files each containing one
58 * each separate file will be named using the corresponding subroutine,
59 * function, block data or program name if one is found; otherwise
60 * the name will be of the form mainNNN.f or blkdtaNNN.f .
61 * If a file of that name exists, it is saved in a name of the
63 * If -e option is used, then only those subprograms named in the -e
64 * option are split off; e.g.:
65 * fsplit -esub1 -e sub2 prog.f
66 * isolates sub1 and sub2 in sub1.f and sub2.f. The space
67 * after -e is optional.
69 * Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
71 * - more function types: double complex, character*(*), etc.
73 * - instead of all unnamed going into zNNN.f, put mains in
74 * mainNNN.f, block datas in blkdtaNNN.f, dups in zzzNNN.f .
83 char *look(), *skiplab(), *functs();
94 #define trim(p) while (*p == ' ' || *p == '\t') p++
99 register FILE *ofp; /* output file */
100 register rv; /* 1 if got card in output file, 0 otherwise */
102 int nflag, /* 1 if got name of subprog., 0 otherwise */
108 /* scan -e options */
109 while ( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') {
115 if(argc <= 1) badparms();
118 extrknt = extrknt + 1;
119 extrnames[extrknt] = extrptr;
120 extrfnd[extrknt] = FALSE;
121 while(*ptr) *extrptr++ = *ptr++;
129 else if (argc == 2) {
130 if ((ifp = fopen(argv[1], "r")) == NULL) {
131 fprintf(stderr, "fsplit: cannot open %s\n", argv[1]);
138 /* look for a temp file that doesn't correspond to an existing file */
143 while (getline() > 0) {
145 fprintf(ofp, "%s", buf);
146 if (lend()) /* look for an 'end' statement */
148 if (nflag == 0) /* if no name yet, try and find one */
152 if (rv == 0) { /* no lines in file, forget the file */
155 for ( i = 0; i <= extrknt; i++ )
158 fprintf( stderr, "fsplit: %s not found\n",
163 if (nflag) { /* rename the file */
165 if (stat(name, &sbuf) < 0 ) {
168 printf("%s\n", name);
170 } else if (strcmp(name, x) == 0) {
174 printf("%s already exists, put in %s\n", name, x);
189 fprintf(stderr, "fsplit: usage: fsplit [-e efile] ... [file] \n");
201 while(*name) *fptr++ = *name++;
204 for ( i=0 ; i<=extrknt; i++ )
205 if( strcmp(fname, extrnames[i]) == 0 ) {
212 get_name(name, letters)
218 while (stat(name, &sbuf) >= 0) {
219 for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
225 if(ptr < name + letters) {
226 fprintf( stderr, "fsplit: ran out of file names\n");
236 for (ptr = buf; ptr < &buf[BSZ]; ) {
240 if (*ptr++ == '\n') {
245 while (getc(ifp) != '\n' && feof(ifp) == 0) ;
246 fprintf(stderr, "line truncated to %d characters\n", BSZ);
250 /* return 1 for 'end' alone on card (up to col. 72), 0 otherwise */
255 if ((p = skiplab(buf)) == 0)
258 if (*p != 'e' && *p != 'E') return(0);
261 if (*p != 'n' && *p != 'N') return(0);
264 if (*p != 'd' && *p != 'D') return(0);
267 if (p - buf >= 72 || *p == '\n')
272 /* check for keywords for subprograms
273 return 0 if comment card, 1 if found
274 name and put in arg string. invent name for unnamed
275 block datas and main programs. */
280 register char *ptr, *p, *sptr;
281 char line[LINESIZE], *iptr = line;
283 /* first check for comment cards */
284 if(buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') return(0);
286 while (*ptr == ' ' || *ptr == '\t') ptr++;
287 if(*ptr == '\n') return(0);
295 /* copy to buffer and converting to lower case */
297 while (*p && p <= &buf[71] ) {
298 *iptr = isupper(*p) ? tolower(*p) : *p;
304 if ((ptr = look(line, "subroutine")) != 0 ||
305 (ptr = look(line, "function")) != 0 ||
306 (ptr = functs(line)) != 0) {
307 if(scan_name(s, ptr)) return(1);
309 } else if((ptr = look(line, "program")) != 0) {
310 if(scan_name(s, ptr)) return(1);
313 } else if((ptr = look(line, "blockdata")) != 0) {
314 if(scan_name(s, ptr)) return(1);
317 } else if((ptr = functs(line)) != 0) {
318 if(scan_name(s, ptr)) return(1);
332 /* scan off the name */
335 while (*ptr != '(' && *ptr != '\n') {
336 if (*ptr != ' ' && *ptr != '\t')
341 if (sptr == s) return(0);
354 /* look for typed functions such as: real*8 function,
355 character*16 function, character*(*) function */
357 if((ptr = look(p,"character")) != 0 ||
358 (ptr = look(p,"logical")) != 0 ||
359 (ptr = look(p,"real")) != 0 ||
360 (ptr = look(p,"integer")) != 0 ||
361 (ptr = look(p,"doubleprecision")) != 0 ||
362 (ptr = look(p,"complex")) != 0 ||
363 (ptr = look(p,"doublecomplex")) != 0 ) {
364 while ( *ptr == ' ' || *ptr == '\t' || *ptr == '*'
365 || (*ptr >= '0' && *ptr <= '9')
366 || *ptr == '(' || *ptr == ')') ptr++;
367 ptr = look(ptr,"function");
374 /* if first 6 col. blank, return ptr to col. 7,
375 if blanks and then tab, return ptr after tab,
376 else return 0 (labelled statement, comment or continuation */
382 for (ptr = p; ptr < &p[6]; ptr++) {
394 /* return 0 if m doesn't match initial part of s;
395 otherwise return ptr to next char after m in s */
399 register char *sp, *mp;