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