Commit | Line | Data |
---|---|---|
d57a8d8e ML |
1 | /* Copyright (c) 1982 Regents of the University of California */ |
2 | ||
0022c355 ML |
3 | static char sccsid[] = "@(#)makedefs.c 1.4 (Berkeley) %G%"; |
4 | ||
5 | static char rcsid[] = "$Header: makedefs.c,v 1.4 84/12/26 10:40:22 linton Exp $"; | |
d57a8d8e ML |
6 | |
7 | /* | |
8 | * Create a definitions file (e.g. .h) from an implementation file (e.g. .c). | |
9 | * | |
10 | * Usage is "makedefs source.c source.h" where source.h is to be created. | |
11 | * | |
12 | * Lines beginning with "public" or within a "#ifndef public ... #endif" | |
13 | * block are copied to the new file. Initializations (e.g. "int x = 3") are | |
14 | * omitted ("int x;" is output). | |
15 | * | |
16 | * Normally a temporary definitions file is created and compared to | |
17 | * the given destination. If they are different, the temporary file | |
18 | * is copied on top of the destination. This is so that dependencies | |
19 | * when using "make" are not triggered. | |
20 | * | |
21 | * The "-f" option overrides this and forces the destination file to be created. | |
22 | */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include <signal.h> | |
26 | ||
27 | #define procedure void | |
28 | ||
29 | Boolean force; | |
30 | Boolean copytext; | |
31 | ||
32 | String tmpname; | |
33 | String modulename(); | |
34 | procedure abnorm(); | |
35 | ||
36 | main(argc, argv) | |
37 | int argc; | |
38 | String argv[]; | |
39 | { | |
40 | extern String mktemp(); | |
41 | String name; | |
42 | File tmp; | |
43 | Integer r; | |
44 | Integer index; | |
45 | ||
46 | if (streq(argv[1], "-f")) { | |
47 | force = true; | |
48 | index = 2; | |
49 | } else { | |
50 | force = false; | |
51 | index = 1; | |
52 | } | |
53 | if (argc - index > 2) { | |
54 | fatal("usage: makedefs [ -f ] file.c [ file.h ]\n"); | |
55 | } | |
56 | tmp = nil; | |
57 | if (freopen(argv[index], "r", stdin) == NULL) { | |
58 | fatal("can't read %s", argv[index]); | |
59 | } | |
60 | signal(SIGINT, abnorm); | |
61 | signal(SIGQUIT, abnorm); | |
62 | if (index + 1 < argc) { | |
63 | if (force) { | |
64 | tmpname = argv[index + 1]; | |
65 | } else { | |
66 | tmpname = mktemp("/tmp/makedefsXXXXXX"); | |
67 | } | |
68 | tmp = freopen(tmpname, "w", stdout); | |
69 | if (tmp == nil) { | |
70 | fatal("can't write %s", tmpname); | |
71 | } | |
72 | } | |
73 | copytext = false; | |
74 | name = modulename(argv[index]); | |
75 | printf("#ifndef %s\n", name); | |
76 | printf("#define %s\n", name); | |
77 | copy(); | |
78 | printf("#endif\n"); | |
79 | if (tmp != NULL and not force) { | |
80 | fclose(tmp); | |
81 | r = call("cmp", stdin, stderr, "-s", tmpname, argv[2], nil); | |
82 | if (r != 0) { | |
83 | r = call("cp", stdin, stderr, tmpname, argv[2], nil); | |
84 | if (r != 0) { | |
85 | fprintf(stderr, "can't create %s\n", argv[2]); | |
86 | } | |
87 | } | |
88 | unlink(tmpname); | |
89 | } | |
90 | quit(0); | |
91 | } | |
92 | ||
93 | String modulename(s) | |
94 | String s; | |
95 | { | |
96 | String r, i, j; | |
97 | static char buf[256]; | |
98 | ||
99 | strcpy(buf, s); | |
100 | i = rindex(buf, '/'); | |
101 | if (i == nil) { | |
102 | i = buf; | |
103 | } | |
104 | for (j = i; *j != '.'; j++); | |
105 | *j++ = '_'; | |
106 | *j++ = 'h'; | |
107 | *j = '\0'; | |
108 | return buf; | |
109 | } | |
110 | ||
111 | copy() | |
112 | { | |
113 | register char *p; | |
114 | char line[1024]; | |
115 | ||
116 | while (gets(line) != NULL) { | |
117 | if (strncmp(line, "#ifndef public", 14) == 0) { | |
118 | copytext = true; | |
119 | } else if (strncmp(line, "#endif", 6) == 0) { | |
120 | copytext = false; | |
121 | } else if (strncmp(line, "public", 6) == 0) { | |
122 | copydef(line); | |
123 | } else if (copytext) { | |
124 | printf("%s\n", line); | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | copydef(s) | |
130 | String s; | |
131 | { | |
132 | register char *p; | |
133 | register Boolean isproc; | |
134 | ||
135 | isproc = false; | |
136 | for (p = &s[7]; *p != '\0' and *p != '='; p++) { | |
137 | if (*p == '(') { | |
138 | isproc = true; | |
139 | printf("(/* "); | |
140 | } else if (*p == ')' and isproc and *(p+1) == '\0') { | |
141 | printf(" */)"); | |
142 | } else { | |
143 | putchar(*p); | |
144 | } | |
145 | } | |
146 | if (isproc or *p == '=') { | |
147 | putchar(';'); | |
148 | } | |
149 | putchar('\n'); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Terminate program. | |
154 | */ | |
155 | ||
156 | procedure abnorm(signo) | |
157 | int signo; | |
158 | { | |
159 | unlink(tmpname); | |
160 | quit(signo); | |
161 | } | |
162 | ||
163 | quit(r) | |
164 | int r; | |
165 | { | |
166 | exit(r); | |
167 | } | |
168 | ||
169 | /* | |
170 | * No special error recovery strategy. | |
171 | */ | |
172 | ||
173 | erecover() | |
174 | { | |
175 | } |