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