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