makedefs.c 3.3 KB

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