Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / fsplit / fsplit.c
blobfc6d65b7019c656bd516396d8f227e220cadf4b1
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Asa Romberger and Jerry Berkman.
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.
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.
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 */
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.26 2008/11/23 09:13:20 dholland Exp $");
44 #endif
45 #endif /* not lint */
47 #include <sys/types.h>
48 #include <sys/stat.h>
50 #include <assert.h>
51 #include <ctype.h>
52 #include <err.h>
53 #include <stdbool.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
60 * usage: fsplit [-e efile] ... [file]
62 * split single file containing source for several fortran programs
63 * and/or subprograms into files each containing one
64 * subprogram unit.
65 * each separate file will be named using the corresponding subroutine,
66 * function, block data or program name if one is found; otherwise
67 * the name will be of the form mainNNN.f or blkdtaNNN.f .
68 * If a file of that name exists, it is saved in a name of the
69 * form zzz000.f .
70 * If -e option is used, then only those subprograms named in the -e
71 * option are split off; e.g.:
72 * fsplit -esub1 -e sub2 prog.f
73 * isolates sub1 and sub2 in sub1.f and sub2.f. The space
74 * after -e is optional.
76 * Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
77 * - added comments
78 * - more function types: double complex, character*(*), etc.
79 * - fixed minor bugs
80 * - instead of all unnamed going into zNNN.f, put mains in
81 * mainNNN.f, block datas in blkdtaNNN.f, dups in zzzNNN.f .
84 #define BSZ 512
85 static char buf[BSZ];
86 static FILE *ifp;
88 static char x[] = "zzz000.f";
89 static char mainp[] = "main000.f";
90 static char blkp[] = "blkdta000.f";
92 static void badparms(void);
93 static const char *functs(const char *);
94 static int get_line(void);
95 static void get_name(char *, int);
96 static int lend(void);
97 static int lname(char *, size_t);
98 static const char *look(const char *, const char *);
99 static int saveit(const char *);
100 static int scan_name(char *, size_t, const char *);
101 static const char *skiplab(const char *);
102 static const char *skipws(const char *);
104 struct extract {
105 bool found;
106 char *name;
109 #define MAXEXTONLY 100
110 static struct extract extonly[MAXEXTONLY];
111 static int numextonly = 0;
114 main(int argc, char **argv)
116 FILE *ofp; /* output file */
117 int rv; /* 1 if got card in output file, 0 otherwise */
118 int nflag; /* 1 if got name of subprog., 0 otherwise */
119 int retval, i, ch;
120 char name[80];
122 while ((ch = getopt(argc, argv, "e:")) != -1) {
123 switch (ch) {
124 case 'e':
125 if (numextonly >= MAXEXTONLY) {
126 errx(1, "Too many -e options");
128 extonly[numextonly].name = optarg;
129 extonly[numextonly].found = false;
130 numextonly++;
131 break;
132 default:
133 badparms();
134 break;
138 if (argc > 2) {
139 badparms();
140 } else if (argc == 2) {
141 if ((ifp = fopen(argv[1], "r")) == NULL) {
142 err(1, "%s", argv[1]);
144 } else {
145 ifp = stdin;
148 for (;;) {
150 * Look for a temp file that doesn't correspond to an
151 * existing file.
154 get_name(x, 3);
155 ofp = fopen(x, "w");
156 if (ofp == NULL) {
157 err(1, "%s", x);
159 nflag = 0;
160 rv = 0;
161 while (get_line() > 0) {
162 rv = 1;
163 fprintf(ofp, "%s", buf);
164 /* look for an 'end' statement */
165 if (lend()) {
166 break;
168 /* if no name yet, try and find one */
169 if (nflag == 0) {
170 nflag = lname(name, sizeof(name));
173 fclose(ofp);
174 if (rv == 0) {
175 /* no lines in file, forget the file */
176 unlink(x);
177 retval = 0;
178 for (i = 0; i < numextonly; i++) {
179 if (!extonly[i].found) {
180 retval = 1;
181 warnx("%s not found", extonly[i].name);
184 exit(retval);
186 if (nflag) {
187 /* rename the file */
188 if (saveit(name)) {
189 struct stat sbuf;
191 if (stat(name, &sbuf) < 0) {
192 if (rename(x, name) < 0) {
193 warn("%s: rename", x);
194 printf("%s left in %s\n",
195 name, x);
196 } else {
197 printf("%s\n", name);
199 continue;
200 } else if (strcmp(name, x) == 0) {
201 printf("%s\n", x);
202 continue;
204 printf("%s already exists, put in %s\n",
205 name, x);
206 continue;
207 } else {
208 unlink(x);
209 continue;
212 if (numextonly == 0) {
213 printf("%s\n", x);
214 } else {
215 unlink(x);
220 static void
221 badparms(void)
223 err(1, "Usage: fsplit [-e efile] ... [file]");
226 static int
227 saveit(const char *name)
229 int i;
230 char fname[50];
231 size_t fnamelen;
233 if (numextonly == 0) {
234 return 1;
236 strlcpy(fname, name, sizeof(fname));
237 fnamelen = strlen(fname);
238 assert(fnamelen > 2);
239 assert(fname[fnamelen-2] = '.');
240 assert(fname[fnamelen-1] = 'f');
241 fname[fnamelen-2] = '\0';
243 for (i = 0; i < numextonly; i++) {
244 if (strcmp(fname, extonly[i].name) == 0) {
245 extonly[i].found = true;
246 return 1;
249 return 0;
252 static void
253 get_name(char *name, int letters)
255 struct stat sbuf;
256 char *ptr;
258 while (stat(name, &sbuf) >= 0) {
259 for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
260 (*ptr)++;
261 if (*ptr <= '9')
262 break;
263 *ptr = '0';
265 if (ptr < name + letters) {
266 errx(1, "Ran out of file names.\n");
271 static int
272 get_line(void)
274 char *ptr;
276 for (ptr = buf; ptr < &buf[BSZ]; ) {
277 *ptr = getc(ifp);
278 if (feof(ifp))
279 return -1;
280 if (*ptr++ == '\n') {
281 *ptr = '\0';
282 return 1;
285 while (getc(ifp) != '\n' && feof(ifp) == 0) {
286 /* nothing */
288 warnx("Line truncated to %d characters.", BSZ);
289 return 1;
293 * Return 1 for 'end' alone on card (up to col. 72), 0 otherwise.
295 static int
296 lend(void)
298 const char *p;
300 if ((p = skiplab(buf)) == 0) {
301 return 0;
303 p = skipws(p);
304 if (*p != 'e' && *p != 'E') {
305 return 0;
307 p++;
308 p = skipws(p);
309 if (*p != 'n' && *p != 'N') {
310 return 0;
312 p++;
313 p = skipws(p);
314 if (*p != 'd' && *p != 'D') {
315 return 0;
317 p++;
318 p = skipws(p);
319 if (p - buf >= 72 || *p == '\n') {
320 return 1;
322 return 0;
326 * check for keywords for subprograms
327 * return 0 if comment card, 1 if found
328 * name and put in arg string. invent name for unnamed
329 * block datas and main programs.
331 static int
332 lname(char *s, size_t l)
334 #define LINESIZE 80
335 const char *ptr, *p;
336 char line[LINESIZE], *iptr = line;
338 /* first check for comment cards */
339 if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') {
340 return 0;
342 ptr = skipws(buf);
343 if (*ptr == '\n') {
344 return 0;
347 ptr = skiplab(buf);
348 if (ptr == NULL) {
349 return 0;
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++;
359 *iptr = '\n';
361 if ((ptr = look(line, "subroutine")) != NULL ||
362 (ptr = look(line, "function")) != NULL ||
363 (ptr = functs(line)) != NULL) {
364 if (scan_name(s, l, ptr)) {
365 return 1;
367 strlcpy(s, x, l);
368 } else if ((ptr = look(line, "program")) != NULL) {
369 if (scan_name(s, l, ptr)) {
370 return 1;
372 get_name(mainp, 4);
373 strlcpy(s, mainp, l);
374 } else if ((ptr = look(line, "blockdata")) != NULL) {
375 if (scan_name(s, l, ptr)) {
376 return 1;
378 get_name(blkp, 6);
379 strlcpy(s, blkp, l);
380 } else if ((ptr = functs(line)) != NULL) {
381 if (scan_name(s, l, ptr)) {
382 return 1;
384 strlcpy(s, x, l);
385 } else {
386 get_name(mainp, 4);
387 strlcpy(s, mainp, l);
389 return 1;
392 static int
393 scan_name(char *s, size_t smax, const char *ptr)
395 char *sptr;
396 size_t sptrmax;
398 /* scan off the name */
399 ptr = skipws(ptr);
400 sptr = s;
401 sptrmax = smax - 3;
402 while (*ptr != '(' && *ptr != '\n') {
403 if (*ptr != ' ' && *ptr != '\t' && *ptr != '/') {
404 if (sptrmax == 0) {
405 /* Not sure this is the right thing, so warn */
406 warnx("Output name too long; truncated");
407 break;
409 *sptr++ = *ptr;
410 sptrmax--;
412 ptr++;
415 if (sptr == s) {
416 return 0;
419 *sptr++ = '.';
420 *sptr++ = 'f';
421 *sptr++ = '\0';
422 return 1;
426 * look for typed functions such as: real*8 function,
427 * character*16 function, character*(*) function
429 static const char *
430 functs(const char *p)
432 const char *ptr;
434 if ((ptr = look(p, "character")) != NULL ||
435 (ptr = look(p, "logical")) != NULL ||
436 (ptr = look(p, "real")) != NULL ||
437 (ptr = look(p, "integer")) != NULL ||
438 (ptr = look(p, "doubleprecision")) != NULL ||
439 (ptr = look(p, "complex")) != NULL ||
440 (ptr = look(p, "doublecomplex")) != NULL) {
441 while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
442 || (*ptr >= '0' && *ptr <= '9')
443 || *ptr == '(' || *ptr == ')') {
444 ptr++;
446 ptr = look(ptr, "function");
447 return ptr;
449 else {
450 return NULL;
455 * if first 6 col. blank, return ptr to col. 7,
456 * if blanks and then tab, return ptr after tab,
457 * else return NULL (labelled statement, comment or continuation)
459 static const char *
460 skiplab(const char *p)
462 const char *ptr;
464 for (ptr = p; ptr < &p[6]; ptr++) {
465 if (*ptr == ' ')
466 continue;
467 if (*ptr == '\t') {
468 ptr++;
469 break;
471 return NULL;
473 return ptr;
477 * return NULL if m doesn't match initial part of s;
478 * otherwise return ptr to next char after m in s
480 static const char *
481 look(const char *s, const char *m)
483 const char *sp, *mp;
485 sp = s; mp = m;
486 while (*mp) {
487 sp = skipws(sp);
488 if (*sp++ != *mp++)
489 return NULL;
491 return sp;
494 static const char *
495 skipws(const char *p)
497 while (*p == ' ' || *p == '\t') {
498 p++;
500 return p;