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