|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1983 The Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: char copyright[] = ! 22: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ ! 23: All rights reserved.\n"; ! 24: #endif /* not lint */ ! 25: ! 26: #ifndef lint ! 27: static char sccsid[] = "@(#)makedefs.c 5.6 (Berkeley) 6/1/90"; ! 28: #endif /* not lint */ ! 29: ! 30: /* ! 31: * Create a definitions file (e.g. .h) from an implementation file (e.g. .c). ! 32: * ! 33: * Usage is "makedefs source.c source.h" where source.h is to be created. ! 34: * ! 35: * Lines beginning with "public" or within a "#ifndef public ... #endif" ! 36: * block are copied to the new file. Initializations (e.g. "int x = 3") are ! 37: * omitted ("int x;" is output). ! 38: * ! 39: * Normally a temporary definitions file is created and compared to ! 40: * the given destination. If they are different, the temporary file ! 41: * is copied on top of the destination. This is so that dependencies ! 42: * when using "make" are not triggered. ! 43: * ! 44: * The "-f" option overrides this and forces the destination file to be created. ! 45: */ ! 46: ! 47: #include "defs.h" ! 48: #include <signal.h> ! 49: #include "pathnames.h" ! 50: ! 51: #define procedure void ! 52: ! 53: #define streqn(s1, s2, n) (strncmp(s1, s2, n) == 0) ! 54: ! 55: Boolean force; ! 56: Boolean copytext; ! 57: ! 58: String tmpname; ! 59: String modulename(); ! 60: procedure abnorm(); ! 61: ! 62: main(argc, argv) ! 63: int argc; ! 64: String argv[]; ! 65: { ! 66: extern String mktemp(); ! 67: String name; ! 68: File tmp; ! 69: Integer r; ! 70: Integer index; ! 71: ! 72: if (streq(argv[1], "-f")) { ! 73: force = true; ! 74: index = 2; ! 75: } else { ! 76: force = false; ! 77: index = 1; ! 78: } ! 79: if (argc - index > 2) { ! 80: fatal("usage: makedefs [ -f ] file.c [ file.h ]\n"); ! 81: } ! 82: tmp = nil; ! 83: if (freopen(argv[index], "r", stdin) == NULL) { ! 84: fatal("can't read %s", argv[index]); ! 85: } ! 86: signal(SIGINT, abnorm); ! 87: signal(SIGQUIT, abnorm); ! 88: if (index + 1 < argc) { ! 89: if (force) { ! 90: tmpname = argv[index + 1]; ! 91: } else { ! 92: tmpname = mktemp(_PATH_TMP); ! 93: } ! 94: tmp = freopen(tmpname, "w", stdout); ! 95: if (tmp == nil) { ! 96: fatal("can't write %s", tmpname); ! 97: } ! 98: } ! 99: copytext = false; ! 100: name = modulename(argv[index]); ! 101: printf("#ifndef %s\n", name); ! 102: printf("#define %s\n", name); ! 103: copy(); ! 104: printf("#endif\n"); ! 105: if (tmp != NULL and not force) { ! 106: fclose(tmp); ! 107: r = call("cmp", stdin, stderr, "-s", tmpname, argv[2], nil); ! 108: if (r != 0) { ! 109: r = call("cp", stdin, stderr, tmpname, argv[2], nil); ! 110: if (r != 0) { ! 111: fprintf(stderr, "can't create %s\n", argv[2]); ! 112: } ! 113: } ! 114: unlink(tmpname); ! 115: } ! 116: quit(0); ! 117: } ! 118: ! 119: String modulename(s) ! 120: String s; ! 121: { ! 122: String r, i, j; ! 123: static char buf[256]; ! 124: ! 125: strcpy(buf, s); ! 126: i = rindex(buf, '/'); ! 127: if (i == nil) { ! 128: i = buf; ! 129: } else { ! 130: ++i; ! 131: } ! 132: for (j = i; *j; j++) { ! 133: if (*j == '.') { ! 134: *j = '_'; ! 135: } ! 136: } ! 137: if (j > i && *--j == 'c') { ! 138: *j = 'h'; ! 139: } ! 140: return i; ! 141: } ! 142: ! 143: copy() ! 144: { ! 145: register char *p; ! 146: integer nesting; ! 147: char line[1024]; ! 148: ! 149: while (gets(line) != NULL) { ! 150: if (streqn(line, "#ifndef public", 14)) { ! 151: copytext = true; ! 152: nesting = 1; ! 153: } else if (streqn(line, "public", 6)) { ! 154: copydef(line); ! 155: } else if (copytext) { ! 156: if (streqn(line, "#ifdef", 6) or streqn(line, "#ifndef", 7)) { ! 157: ++nesting; ! 158: printf("%s\n", line); ! 159: } else if (streqn(line, "#endif", 6)) { ! 160: --nesting; ! 161: if (nesting <= 0) { ! 162: copytext = false; ! 163: } else { ! 164: printf("%s\n", line); ! 165: } ! 166: } else { ! 167: printf("%s\n", line); ! 168: } ! 169: } else if ( ! 170: streqn(line, "#ifdef", 6) or ! 171: streqn(line, "#ifndef", 7) or ! 172: streqn(line, "#else", 5) or ! 173: streqn(line, "#endif", 6) ! 174: ) { ! 175: printf("%s\n", line); ! 176: } ! 177: } ! 178: } ! 179: ! 180: copydef(s) ! 181: String s; ! 182: { ! 183: register char *p; ! 184: register Boolean isproc; ! 185: ! 186: isproc = false; ! 187: for (p = &s[7]; *p != '\0' and *p != '='; p++) { ! 188: if (*p == '(') { ! 189: isproc = true; ! 190: printf("(/* "); ! 191: } else if (*p == ')' and isproc and *(p+1) == '\0') { ! 192: printf(" */)"); ! 193: } else { ! 194: putchar(*p); ! 195: } ! 196: } ! 197: if (isproc or *p == '=') { ! 198: putchar(';'); ! 199: } ! 200: putchar('\n'); ! 201: } ! 202: ! 203: /* ! 204: * Terminate program. ! 205: */ ! 206: ! 207: procedure abnorm(signo) ! 208: int signo; ! 209: { ! 210: unlink(tmpname); ! 211: quit(signo); ! 212: } ! 213: ! 214: quit(r) ! 215: int r; ! 216: { ! 217: exit(r); ! 218: } ! 219: ! 220: /* ! 221: * No special error recovery strategy. ! 222: */ ! 223: ! 224: erecover() ! 225: { ! 226: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.