Annotation of src/usr.bin/fsplit/fsplit.c, Revision 1.12
1.12 ! millert 1: /* $OpenBSD: fsplit.c,v 1.11 2003/04/05 17:18:46 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.
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.12 ! millert 43: static char rcsid[] = "$OpenBSD: fsplit.c,v 1.11 2003/04/05 17:18:46 deraadt 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.1 deraadt 107: main(argc, argv)
1.5 deraadt 108: char **argv;
1.1 deraadt 109: {
1.8 mpech 110: FILE *ofp; /* output file */
111: int rv; /* 1 if got card in output file, 0 otherwise */
112: char *ptr;
1.5 deraadt 113: int nflag, /* 1 if got name of subprog., 0 otherwise */
114: retval, i;
1.6 espie 115: /* must be as large as max(sizeof(x), sizeof(mainp), sizeof(blockp)) */
116: char name[20];
1.1 deraadt 117:
1.6 espie 118: maxextrknt = 100;
119: extrnames = malloc(sizeof(char *) * maxextrknt);
120: if (extrnames == NULL)
121: errx(1, "out of memory");
1.5 deraadt 122: /* scan -e options */
123: while (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') {
1.1 deraadt 124: extr = TRUE;
125: ptr = argv[1] + 2;
1.5 deraadt 126: if (!*ptr) {
1.1 deraadt 127: argc--;
128: argv++;
1.5 deraadt 129: if (argc <= 1)
130: badparms();
1.1 deraadt 131: ptr = argv[1];
132: }
133: extrknt = extrknt + 1;
1.6 espie 134: if (extrknt >= maxextrknt) {
135: extrnames = realloc(extrnames,
136: sizeof(char *) * maxextrknt);
137: if (extrnames == NULL)
138: errx(1, "too many -e arguments");
139: }
140: if ((extrnames[extrknt] = strdup(ptr)) == NULL)
141: errx(1, "out of memory");
1.1 deraadt 142: argc--;
143: argv++;
144: }
145:
1.6 espie 146: extrfnd = calloc(extrknt+1, sizeof(int));
147: if (extrfnd == NULL)
148: errx(1, "out of memory");
149:
1.1 deraadt 150: if (argc > 2)
151: badparms();
152: else
1.5 deraadt 153: if (argc == 2) {
154: if ((ifp = fopen(argv[1], "r")) == NULL)
1.6 espie 155: err(1, "%s", argv[1]);
1.5 deraadt 156: } else
157: ifp = stdin;
158: for (;;) {
1.6 espie 159: int fd;
160:
1.5 deraadt 161: /* look for a temp file that doesn't correspond to an existing
162: * file */
163: get_name(x, 3);
1.6 espie 164:
165: fd = open(x, O_CREAT|O_EXCL|O_RDWR, 0666);
166: if (fd == -1)
1.7 millert 167: err(1, "%s", x);
1.6 espie 168: ofp = fdopen(fd, "w");
169: if (ofp == NULL) {
170: close(fd);
171: unlink(x);
1.7 millert 172: err(1, "%s", x);
1.6 espie 173: }
1.5 deraadt 174: nflag = 0;
175: rv = 0;
176: while (getline() > 0) {
177: rv = 1;
178: fprintf(ofp, "%s", buf);
179: if (lend()) /* look for an 'end' statement */
180: break;
181: if (nflag == 0) /* if no name yet, try and find one */
1.11 deraadt 182: nflag = lname(name, sizeof name);
1.5 deraadt 183: }
184: fclose(ofp);
185: if (rv == 0) { /* no lines in file, forget the file */
186: unlink(x);
187: retval = 0;
188: for (i = 0; i <= extrknt; i++)
189: if (!extrfnd[i]) {
190: retval = 1;
191: warnx("%s not found", extrnames[i]);
192: }
193: exit(retval);
194: }
195: if (nflag) { /* rename the file */
196: if (saveit(name)) {
197: if (stat(name, &sbuf) < 0) {
198: link(x, name);
199: unlink(x);
200: printf("%s\n", name);
201: continue;
202: } else
203: if (strcmp(name, x) == 0) {
204: printf("%s\n", x);
205: continue;
206: }
207: printf("%s already exists, put in %s\n", name, x);
208: continue;
209: } else
1.1 deraadt 210: unlink(x);
211: continue;
1.5 deraadt 212: }
213: if (!extr)
214: printf("%s\n", x);
215: else
1.1 deraadt 216: unlink(x);
217: }
218: }
219:
1.4 deraadt 220: void
1.1 deraadt 221: badparms()
222: {
1.6 espie 223: fprintf(stderr, "usage: fsplit [-e efile] ... [file]\n");
224: exit(1);
1.1 deraadt 225: }
226:
1.4 deraadt 227: int
1.1 deraadt 228: saveit(name)
1.5 deraadt 229: char *name;
1.1 deraadt 230: {
1.5 deraadt 231: int i;
1.6 espie 232: size_t n;
1.1 deraadt 233:
1.5 deraadt 234: if (!extr)
235: return (1);
1.6 espie 236:
237: n = strlen(name);
238: if (n < 2)
239: return (0);
240:
1.5 deraadt 241: for (i = 0; i <= extrknt; i++)
1.6 espie 242: if (strncmp(name, extrnames[i], n - 2) == 0 &&
243: extrnames[i][n-2] == '\0') {
1.1 deraadt 244: extrfnd[i] = TRUE;
1.5 deraadt 245: return (1);
1.1 deraadt 246: }
1.5 deraadt 247: return (0);
1.1 deraadt 248: }
249:
1.4 deraadt 250: void
1.1 deraadt 251: get_name(name, letters)
1.5 deraadt 252: char *name;
253: int letters;
1.1 deraadt 254: {
1.8 mpech 255: char *ptr;
1.1 deraadt 256:
257: while (stat(name, &sbuf) >= 0) {
258: for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
259: (*ptr)++;
260: if (*ptr <= '9')
261: break;
262: *ptr = '0';
263: }
1.5 deraadt 264: if (ptr < name + letters)
1.3 mickey 265: errx(1, "ran out of file names");
1.1 deraadt 266: }
267: }
268:
1.4 deraadt 269: int
1.1 deraadt 270: getline()
271: {
1.10 deraadt 272: int c;
1.8 mpech 273: char *ptr;
1.1 deraadt 274:
1.5 deraadt 275: for (ptr = buf; ptr < &buf[BSZ];) {
1.10 deraadt 276: c = getc(ifp);
277: *ptr = c;
1.1 deraadt 278: if (feof(ifp))
279: return (-1);
280: if (*ptr++ == '\n') {
281: *ptr = 0;
282: return (1);
283: }
284: }
1.5 deraadt 285: while (getc(ifp) != '\n' && feof(ifp) == 0);
1.3 mickey 286: warnx("line truncated to %d characters", BSZ);
1.1 deraadt 287: return (1);
288: }
289: /* return 1 for 'end' alone on card (up to col. 72), 0 otherwise */
1.4 deraadt 290: int
1.1 deraadt 291: lend()
292: {
1.8 mpech 293: char *p;
1.1 deraadt 294:
295: if ((p = skiplab(buf)) == 0)
296: return (0);
297: trim(p);
1.5 deraadt 298: if (*p != 'e' && *p != 'E')
299: return (0);
1.1 deraadt 300: p++;
301: trim(p);
1.5 deraadt 302: if (*p != 'n' && *p != 'N')
303: return (0);
1.1 deraadt 304: p++;
305: trim(p);
1.5 deraadt 306: if (*p != 'd' && *p != 'D')
307: return (0);
1.1 deraadt 308: p++;
309: trim(p);
310: if (p - buf >= 72 || *p == '\n')
311: return (1);
312: return (0);
313: }
1.5 deraadt 314: /* check for keywords for subprograms
1.1 deraadt 315: return 0 if comment card, 1 if found
316: name and put in arg string. invent name for unnamed
317: block datas and main programs. */
1.4 deraadt 318: int
1.11 deraadt 319: lname(s, len)
1.5 deraadt 320: char *s;
1.11 deraadt 321: size_t len;
1.1 deraadt 322: {
1.5 deraadt 323: #define LINESIZE 80
1.8 mpech 324: char *ptr, *p;
1.5 deraadt 325: char line[LINESIZE], *iptr = line;
1.1 deraadt 326:
327: /* first check for comment cards */
1.5 deraadt 328: if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*')
329: return (0);
1.1 deraadt 330: ptr = buf;
1.5 deraadt 331: while (*ptr == ' ' || *ptr == '\t')
332: ptr++;
333: if (*ptr == '\n')
334: return (0);
1.1 deraadt 335:
336:
337: ptr = skiplab(buf);
338: if (ptr == 0)
339: return (0);
340:
341:
1.5 deraadt 342: /* copy to buffer and converting to lower case */
1.1 deraadt 343: p = ptr;
1.5 deraadt 344: while (*p && p <= &buf[71]) {
1.6 espie 345: *iptr = tolower(*p);
1.5 deraadt 346: iptr++;
347: p++;
1.1 deraadt 348: }
349: *iptr = '\n';
350:
351: if ((ptr = look(line, "subroutine")) != 0 ||
352: (ptr = look(line, "function")) != 0 ||
353: (ptr = functs(line)) != 0) {
1.5 deraadt 354: if (scan_name(s, ptr))
355: return (1);
1.11 deraadt 356: strlcpy(s, x, len);
357: } else if ((ptr = look(line, "program")) != 0) {
358: if (scan_name(s, ptr))
359: return (1);
360: get_name(mainp, 4);
361: strlcpy(s, mainp, len);
362: } else if ((ptr = look(line, "blockdata")) != 0) {
363: if (scan_name(s, ptr))
364: return (1);
365: get_name(blkp, 6);
366: strlcpy(s, blkp, len);
367: } else if ((ptr = functs(line)) != 0) {
368: if (scan_name(s, ptr))
369: return (1);
370: strlcpy(s, x, len);
371: } else {
372: get_name(mainp, 4);
373: strlcpy(s, mainp, len);
374: }
1.5 deraadt 375: return (1);
1.1 deraadt 376: }
377:
1.4 deraadt 378: int
1.1 deraadt 379: scan_name(s, ptr)
1.5 deraadt 380: char *s, *ptr;
1.1 deraadt 381: {
1.5 deraadt 382: char *sptr;
1.1 deraadt 383:
384: /* scan off the name */
385: trim(ptr);
386: sptr = s;
387: while (*ptr != '(' && *ptr != '\n') {
388: if (*ptr != ' ' && *ptr != '\t')
389: *sptr++ = *ptr;
390: ptr++;
391: }
392:
1.5 deraadt 393: if (sptr == s)
394: return (0);
1.1 deraadt 395:
396: *sptr++ = '.';
397: *sptr++ = 'f';
398: *sptr++ = 0;
1.5 deraadt 399: return (1);
1.1 deraadt 400: }
401:
1.5 deraadt 402: char *
403: functs(p)
404: char *p;
1.1 deraadt 405: {
1.8 mpech 406: char *ptr;
1.1 deraadt 407:
408: /* look for typed functions such as: real*8 function,
409: character*16 function, character*(*) function */
410:
1.5 deraadt 411: if ((ptr = look(p, "character")) != 0 ||
412: (ptr = look(p, "logical")) != 0 ||
413: (ptr = look(p, "real")) != 0 ||
414: (ptr = look(p, "integer")) != 0 ||
415: (ptr = look(p, "doubleprecision")) != 0 ||
416: (ptr = look(p, "complex")) != 0 ||
417: (ptr = look(p, "doublecomplex")) != 0) {
418: while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
419: || (*ptr >= '0' && *ptr <= '9')
420: || *ptr == '(' || *ptr == ')')
421: ptr++;
422: ptr = look(ptr, "function");
423: return (ptr);
424: } else
425: return (0);
1.1 deraadt 426: }
427: /* if first 6 col. blank, return ptr to col. 7,
428: if blanks and then tab, return ptr after tab,
429: else return 0 (labelled statement, comment or continuation */
1.5 deraadt 430: char *
431: skiplab(p)
432: char *p;
1.1 deraadt 433: {
1.8 mpech 434: char *ptr;
1.1 deraadt 435:
436: for (ptr = p; ptr < &p[6]; ptr++) {
437: if (*ptr == ' ')
438: continue;
439: if (*ptr == '\t') {
440: ptr++;
441: break;
442: }
443: return (0);
444: }
445: return (ptr);
446: }
447: /* return 0 if m doesn't match initial part of s;
448: otherwise return ptr to next char after m in s */
1.5 deraadt 449: char *
450: look(s, m)
451: char *s, *m;
1.1 deraadt 452: {
1.8 mpech 453: char *sp, *mp;
1.1 deraadt 454:
1.5 deraadt 455: sp = s;
456: mp = m;
1.1 deraadt 457: while (*mp) {
458: trim(sp);
459: if (*sp++ != *mp++)
460: return (0);
461: }
462: return (sp);
463: }