Annotation of src/usr.bin/fsplit/fsplit.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: fsplit.c,v 1.14 2003/06/25 21:19:19 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.15 ! deraadt 43: static char rcsid[] = "$OpenBSD: fsplit.c,v 1.14 2003/06/25 21:19:19 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.15 ! deraadt 56: void badparms(void);
1.9 millert 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";
1.14 deraadt 93: char *look(char *, char *), *skiplab(char *), *functs(char *);
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: }
1.14 deraadt 285:
1.1 deraadt 286: /* return 1 for 'end' alone on card (up to col. 72), 0 otherwise */
1.4 deraadt 287: int
1.13 deraadt 288: lend(void)
1.1 deraadt 289: {
1.8 mpech 290: char *p;
1.1 deraadt 291:
292: if ((p = skiplab(buf)) == 0)
293: return (0);
294: trim(p);
1.5 deraadt 295: if (*p != 'e' && *p != 'E')
296: return (0);
1.1 deraadt 297: p++;
298: trim(p);
1.5 deraadt 299: if (*p != 'n' && *p != 'N')
300: return (0);
1.1 deraadt 301: p++;
302: trim(p);
1.5 deraadt 303: if (*p != 'd' && *p != 'D')
304: return (0);
1.1 deraadt 305: p++;
306: trim(p);
307: if (p - buf >= 72 || *p == '\n')
308: return (1);
309: return (0);
310: }
1.14 deraadt 311:
312: /* check for keywords for subprograms
313: * return 0 if comment card, 1 if found
314: * name and put in arg string. invent name for unnamed
315: * block datas and main programs.
316: */
1.4 deraadt 317: int
1.13 deraadt 318: lname(char *s, size_t len)
1.1 deraadt 319: {
1.5 deraadt 320: #define LINESIZE 80
1.8 mpech 321: char *ptr, *p;
1.5 deraadt 322: char line[LINESIZE], *iptr = line;
1.1 deraadt 323:
324: /* first check for comment cards */
1.5 deraadt 325: if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*')
326: return (0);
1.1 deraadt 327: ptr = buf;
1.5 deraadt 328: while (*ptr == ' ' || *ptr == '\t')
329: ptr++;
330: if (*ptr == '\n')
331: return (0);
1.1 deraadt 332:
333:
334: ptr = skiplab(buf);
335: if (ptr == 0)
336: return (0);
337:
338:
1.5 deraadt 339: /* copy to buffer and converting to lower case */
1.1 deraadt 340: p = ptr;
1.5 deraadt 341: while (*p && p <= &buf[71]) {
1.6 espie 342: *iptr = tolower(*p);
1.5 deraadt 343: iptr++;
344: p++;
1.1 deraadt 345: }
346: *iptr = '\n';
347:
348: if ((ptr = look(line, "subroutine")) != 0 ||
349: (ptr = look(line, "function")) != 0 ||
350: (ptr = functs(line)) != 0) {
1.5 deraadt 351: if (scan_name(s, ptr))
352: return (1);
1.11 deraadt 353: strlcpy(s, x, len);
354: } else if ((ptr = look(line, "program")) != 0) {
355: if (scan_name(s, ptr))
356: return (1);
357: get_name(mainp, 4);
358: strlcpy(s, mainp, len);
359: } else if ((ptr = look(line, "blockdata")) != 0) {
360: if (scan_name(s, ptr))
361: return (1);
362: get_name(blkp, 6);
363: strlcpy(s, blkp, len);
364: } else if ((ptr = functs(line)) != 0) {
365: if (scan_name(s, ptr))
366: return (1);
367: strlcpy(s, x, len);
368: } else {
369: get_name(mainp, 4);
370: strlcpy(s, mainp, len);
371: }
1.5 deraadt 372: return (1);
1.1 deraadt 373: }
374:
1.4 deraadt 375: int
1.13 deraadt 376: scan_name(char *s, char *ptr)
1.1 deraadt 377: {
1.5 deraadt 378: char *sptr;
1.1 deraadt 379:
380: /* scan off the name */
381: trim(ptr);
382: sptr = s;
383: while (*ptr != '(' && *ptr != '\n') {
384: if (*ptr != ' ' && *ptr != '\t')
385: *sptr++ = *ptr;
386: ptr++;
387: }
388:
1.5 deraadt 389: if (sptr == s)
390: return (0);
1.1 deraadt 391:
392: *sptr++ = '.';
393: *sptr++ = 'f';
394: *sptr++ = 0;
1.5 deraadt 395: return (1);
1.1 deraadt 396: }
397:
1.5 deraadt 398: char *
1.13 deraadt 399: functs(char *p)
1.1 deraadt 400: {
1.8 mpech 401: char *ptr;
1.1 deraadt 402:
403: /* look for typed functions such as: real*8 function,
404: character*16 function, character*(*) function */
405:
1.5 deraadt 406: if ((ptr = look(p, "character")) != 0 ||
407: (ptr = look(p, "logical")) != 0 ||
408: (ptr = look(p, "real")) != 0 ||
409: (ptr = look(p, "integer")) != 0 ||
410: (ptr = look(p, "doubleprecision")) != 0 ||
411: (ptr = look(p, "complex")) != 0 ||
412: (ptr = look(p, "doublecomplex")) != 0) {
413: while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
414: || (*ptr >= '0' && *ptr <= '9')
415: || *ptr == '(' || *ptr == ')')
416: ptr++;
417: ptr = look(ptr, "function");
418: return (ptr);
419: } else
420: return (0);
1.1 deraadt 421: }
1.14 deraadt 422:
423: /*
424: * if first 6 col. blank, return ptr to col. 7,
425: * if blanks and then tab, return ptr after tab,
426: * else return 0 (labelled statement, comment or continuation
427: */
1.5 deraadt 428: char *
1.13 deraadt 429: skiplab(char *p)
1.1 deraadt 430: {
1.8 mpech 431: char *ptr;
1.1 deraadt 432:
433: for (ptr = p; ptr < &p[6]; ptr++) {
434: if (*ptr == ' ')
435: continue;
436: if (*ptr == '\t') {
437: ptr++;
438: break;
439: }
440: return (0);
441: }
442: return (ptr);
443: }
1.14 deraadt 444:
445: /*
446: * return 0 if m doesn't match initial part of s;
447: * otherwise return ptr to next char after m in s
448: */
1.5 deraadt 449: char *
1.13 deraadt 450: look(char *s, char *m)
1.1 deraadt 451: {
1.8 mpech 452: char *sp, *mp;
1.1 deraadt 453:
1.5 deraadt 454: sp = s;
455: mp = m;
1.1 deraadt 456: while (*mp) {
457: trim(sp);
458: if (*sp++ != *mp++)
459: return (0);
460: }
461: return (sp);
462: }