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 8 char copyright[] = 9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)makedefs.c 5.1 (Berkeley) 05/31/85"; 15 #endif not lint 16 17 static char rcsid[] = "$Header: makedefs.c,v 1.4 84/12/26 10:40:22 linton Exp $"; 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> 38 39 #define procedure void 40 41 Boolean force; 42 Boolean copytext; 43 44 String tmpname; 45 String modulename(); 46 procedure abnorm(); 47 48 main(argc, argv) 49 int argc; 50 String argv[]; 51 { 52 extern String mktemp(); 53 String name; 54 File tmp; 55 Integer r; 56 Integer index; 57 58 if (streq(argv[1], "-f")) { 59 force = true; 60 index = 2; 61 } else { 62 force = false; 63 index = 1; 64 } 65 if (argc - index > 2) { 66 fatal("usage: makedefs [ -f ] file.c [ file.h ]\n"); 67 } 68 tmp = nil; 69 if (freopen(argv[index], "r", stdin) == NULL) { 70 fatal("can't read %s", argv[index]); 71 } 72 signal(SIGINT, abnorm); 73 signal(SIGQUIT, abnorm); 74 if (index + 1 < argc) { 75 if (force) { 76 tmpname = argv[index + 1]; 77 } else { 78 tmpname = mktemp("/tmp/makedefsXXXXXX"); 79 } 80 tmp = freopen(tmpname, "w", stdout); 81 if (tmp == nil) { 82 fatal("can't write %s", tmpname); 83 } 84 } 85 copytext = false; 86 name = modulename(argv[index]); 87 printf("#ifndef %s\n", name); 88 printf("#define %s\n", name); 89 copy(); 90 printf("#endif\n"); 91 if (tmp != NULL and not force) { 92 fclose(tmp); 93 r = call("cmp", stdin, stderr, "-s", tmpname, argv[2], nil); 94 if (r != 0) { 95 r = call("cp", stdin, stderr, tmpname, argv[2], nil); 96 if (r != 0) { 97 fprintf(stderr, "can't create %s\n", argv[2]); 98 } 99 } 100 unlink(tmpname); 101 } 102 quit(0); 103 } 104 105 String modulename(s) 106 String s; 107 { 108 String r, i, j; 109 static char buf[256]; 110 111 strcpy(buf, s); 112 i = rindex(buf, '/'); 113 if (i == nil) { 114 i = buf; 115 } 116 for (j = i; *j != '.'; j++); 117 *j++ = '_'; 118 *j++ = 'h'; 119 *j = '\0'; 120 return buf; 121 } 122 123 copy() 124 { 125 register char *p; 126 char line[1024]; 127 128 while (gets(line) != NULL) { 129 if (strncmp(line, "#ifndef public", 14) == 0) { 130 copytext = true; 131 } else if (strncmp(line, "#endif", 6) == 0) { 132 copytext = false; 133 } else if (strncmp(line, "public", 6) == 0) { 134 copydef(line); 135 } else if (copytext) { 136 printf("%s\n", line); 137 } 138 } 139 } 140 141 copydef(s) 142 String s; 143 { 144 register char *p; 145 register Boolean isproc; 146 147 isproc = false; 148 for (p = &s[7]; *p != '\0' and *p != '='; p++) { 149 if (*p == '(') { 150 isproc = true; 151 printf("(/* "); 152 } else if (*p == ')' and isproc and *(p+1) == '\0') { 153 printf(" */)"); 154 } else { 155 putchar(*p); 156 } 157 } 158 if (isproc or *p == '=') { 159 putchar(';'); 160 } 161 putchar('\n'); 162 } 163 164 /* 165 * Terminate program. 166 */ 167 168 procedure abnorm(signo) 169 int signo; 170 { 171 unlink(tmpname); 172 quit(signo); 173 } 174 175 quit(r) 176 int r; 177 { 178 exit(r); 179 } 180 181 /* 182 * No special error recovery strategy. 183 */ 184 185 erecover() 186 { 187 } 188