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