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