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