fsplit.c revision 1.23 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.23 2008/11/16 05:20:11 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 *, size_t, const char *);
100 static const char *skiplab(const char *);
101 static const char *skipws(const char *);
102
103 struct extract {
104 bool found;
105 char *name;
106 };
107
108 #define MAXEXTONLY 100
109 static struct extract extonly[MAXEXTONLY];
110 static int numextonly = 0;
111
112 int
113 main(int argc, char **argv)
114 {
115 FILE *ofp; /* output file */
116 int rv; /* 1 if got card in output file, 0 otherwise */
117 int nflag; /* 1 if got name of subprog., 0 otherwise */
118 int retval, i, ch;
119 char name[20];
120
121 while ((ch = getopt(argc, argv, "e:")) != -1) {
122 switch (ch) {
123 case 'e':
124 if (numextonly >= MAXEXTONLY) {
125 errx(1, "Too many -e options");
126 }
127 extonly[numextonly].name = optarg;
128 extonly[numextonly].found = false;
129 numextonly++;
130 break;
131 default:
132 badparms();
133 break;
134 }
135 }
136
137 if (argc > 2) {
138 badparms();
139 } else if (argc == 2) {
140 if ((ifp = fopen(argv[1], "r")) == NULL) {
141 err(1, "%s", argv[1]);
142 }
143 } else {
144 ifp = stdin;
145 }
146
147 for (;;) {
148 /*
149 * Look for a temp file that doesn't correspond to an
150 * existing file.
151 */
152
153 get_name(x, 3);
154 ofp = fopen(x, "w");
155 if (ofp == NULL) {
156 err(1, "%s", x);
157 }
158 nflag = 0;
159 rv = 0;
160 while (getline() > 0) {
161 rv = 1;
162 fprintf(ofp, "%s", buf);
163 /* look for an 'end' statement */
164 if (lend()) {
165 break;
166 }
167 /* if no name yet, try and find one */
168 if (nflag == 0) {
169 nflag = lname(name, sizeof(name));
170 }
171 }
172 fclose(ofp);
173 if (rv == 0) {
174 /* no lines in file, forget the file */
175 unlink(x);
176 retval = 0;
177 for (i = 0; i < numextonly; i++) {
178 if (!extonly[i].found) {
179 retval = 1;
180 warnx("%s not found", extonly[i].name);
181 }
182 }
183 exit(retval);
184 }
185 if (nflag) {
186 /* rename the file */
187 if (saveit(name)) {
188 struct stat sbuf;
189
190 if (stat(name, &sbuf) < 0) {
191 link(x, name);
192 unlink(x);
193 printf("%s\n", name);
194 continue;
195 } else if (strcmp(name, x) == 0) {
196 printf("%s\n", x);
197 continue;
198 }
199 printf("%s already exists, put in %s\n",
200 name, x);
201 continue;
202 } else {
203 unlink(x);
204 continue;
205 }
206 }
207 if (numextonly == 0) {
208 printf("%s\n", x);
209 } else {
210 unlink(x);
211 }
212 }
213 }
214
215 static void
216 badparms(void)
217 {
218 err(1, "Usage: fsplit [-e efile] ... [file]");
219 }
220
221 static int
222 saveit(const char *name)
223 {
224 int i;
225 char fname[50];
226 char *fptr = fname;
227
228 if (numextonly == 0) {
229 return 1;
230 }
231 while (*name) {
232 *fptr++ = *name++;
233 }
234 *--fptr = '\0';
235 *--fptr = '\0';
236 for (i = 0; i < numextonly; i++) {
237 if (strcmp(fname, extonly[i].name) == 0) {
238 extonly[i].found = true;
239 return 1;
240 }
241 }
242 return 0;
243 }
244
245 static void
246 get_name(char *name, int letters)
247 {
248 struct stat sbuf;
249 char *ptr;
250
251 while (stat(name, &sbuf) >= 0) {
252 for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
253 (*ptr)++;
254 if (*ptr <= '9')
255 break;
256 *ptr = '0';
257 }
258 if (ptr < name + letters) {
259 errx(1, "Ran out of file names.\n");
260 }
261 }
262 }
263
264 static int
265 getline(void)
266 {
267 char *ptr;
268
269 for (ptr = buf; ptr < &buf[BSZ]; ) {
270 *ptr = getc(ifp);
271 if (feof(ifp))
272 return -1;
273 if (*ptr++ == '\n') {
274 *ptr = '\0';
275 return 1;
276 }
277 }
278 while (getc(ifp) != '\n' && feof(ifp) == 0) {
279 /* nothing */
280 }
281 warnx("Line truncated to %d characters.", BSZ);
282 return 1;
283 }
284
285 /*
286 * Return 1 for 'end' alone on card (up to col. 72), 0 otherwise.
287 */
288 static int
289 lend(void)
290 {
291 const char *p;
292
293 if ((p = skiplab(buf)) == 0) {
294 return 0;
295 }
296 p = skipws(p);
297 if (*p != 'e' && *p != 'E') {
298 return 0;
299 }
300 p++;
301 p = skipws(p);
302 if (*p != 'n' && *p != 'N') {
303 return 0;
304 }
305 p++;
306 p = skipws(p);
307 if (*p != 'd' && *p != 'D') {
308 return 0;
309 }
310 p++;
311 p = skipws(p);
312 if (p - buf >= 72 || *p == '\n') {
313 return 1;
314 }
315 return 0;
316 }
317
318 /*
319 * check for keywords for subprograms
320 * return 0 if comment card, 1 if found
321 * name and put in arg string. invent name for unnamed
322 * block datas and main programs.
323 */
324 static int
325 lname(char *s, size_t l)
326 {
327 #define LINESIZE 80
328 const char *ptr, *p;
329 char line[LINESIZE], *iptr = line;
330
331 /* first check for comment cards */
332 if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') {
333 return 0;
334 }
335 ptr = buf;
336 while (*ptr == ' ' || *ptr == '\t') {
337 ptr++;
338 }
339 if (*ptr == '\n') {
340 return 0;
341 }
342
343 ptr = skiplab(buf);
344 if (ptr == NULL) {
345 return 0;
346 }
347
348 /* copy to buffer and converting to lower case */
349 p = ptr;
350 while (*p && p <= &buf[71] ) {
351 *iptr = tolower((unsigned char)*p);
352 iptr++;
353 p++;
354 }
355 *iptr = '\n';
356
357 if ((ptr = look(line, "subroutine")) != NULL ||
358 (ptr = look(line, "function")) != NULL ||
359 (ptr = functs(line)) != NULL) {
360 if (scan_name(s, l, ptr)) {
361 return 1;
362 }
363 strlcpy(s, x, l);
364 } else if ((ptr = look(line, "program")) != NULL) {
365 if (scan_name(s, l, ptr)) {
366 return 1;
367 }
368 get_name(mainp, 4);
369 strlcpy(s, mainp, l);
370 } else if ((ptr = look(line, "blockdata")) != NULL) {
371 if (scan_name(s, l, ptr)) {
372 return 1;
373 }
374 get_name(blkp, 6);
375 strlcpy(s, blkp, l);
376 } else if ((ptr = functs(line)) != NULL) {
377 if (scan_name(s, l, ptr)) {
378 return 1;
379 }
380 strlcpy(s, x, l);
381 } else {
382 get_name(mainp, 4);
383 strlcpy(s, mainp, l);
384 }
385 return 1;
386 }
387
388 static int
389 scan_name(char *s, size_t smax, const char *ptr)
390 {
391 char *sptr;
392 size_t sptrmax;
393
394 /* scan off the name */
395 ptr = skipws(ptr);
396 sptr = s;
397 sptrmax = smax - 3;
398 while (*ptr != '(' && *ptr != '\n') {
399 if (*ptr != ' ' && *ptr != '\t') {
400 if (sptrmax == 0) {
401 /* Not sure this is the right thing, so warn */
402 warnx("Output name too long; truncated");
403 break;
404 }
405 *sptr++ = *ptr;
406 sptrmax--;
407 }
408 ptr++;
409 }
410
411 if (sptr == s) {
412 return 0;
413 }
414
415 *sptr++ = '.';
416 *sptr++ = 'f';
417 *sptr++ = '\0';
418 return 1;
419 }
420
421 /*
422 * look for typed functions such as: real*8 function,
423 * character*16 function, character*(*) function
424 */
425 static const char *
426 functs(const char *p)
427 {
428 const char *ptr;
429
430 if ((ptr = look(p, "character")) != NULL ||
431 (ptr = look(p, "logical")) != NULL ||
432 (ptr = look(p, "real")) != NULL ||
433 (ptr = look(p, "integer")) != NULL ||
434 (ptr = look(p, "doubleprecision")) != NULL ||
435 (ptr = look(p, "complex")) != NULL ||
436 (ptr = look(p, "doublecomplex")) != NULL) {
437 while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
438 || (*ptr >= '0' && *ptr <= '9')
439 || *ptr == '(' || *ptr == ')') {
440 ptr++;
441 }
442 ptr = look(ptr, "function");
443 return ptr;
444 }
445 else {
446 return NULL;
447 }
448 }
449
450 /*
451 * if first 6 col. blank, return ptr to col. 7,
452 * if blanks and then tab, return ptr after tab,
453 * else return NULL (labelled statement, comment or continuation)
454 */
455 static const char *
456 skiplab(const char *p)
457 {
458 const char *ptr;
459
460 for (ptr = p; ptr < &p[6]; ptr++) {
461 if (*ptr == ' ')
462 continue;
463 if (*ptr == '\t') {
464 ptr++;
465 break;
466 }
467 return NULL;
468 }
469 return ptr;
470 }
471
472 /*
473 * return NULL if m doesn't match initial part of s;
474 * otherwise return ptr to next char after m in s
475 */
476 static const char *
477 look(const char *s, const char *m)
478 {
479 const char *sp, *mp;
480
481 sp = s; mp = m;
482 while (*mp) {
483 sp = skipws(sp);
484 if (*sp++ != *mp++)
485 return NULL;
486 }
487 return sp;
488 }
489
490 static const char *
491 skipws(const char *p)
492 {
493 while (*p == ' ' || *p == '\t') {
494 p++;
495 }
496 return p;
497 }
498