121610Sdist /* 2*38105Sbostic * Copyright (c) 1983 The Regents of the University of California. 3*38105Sbostic * All rights reserved. 4*38105Sbostic * 5*38105Sbostic * Redistribution and use in source and binary forms are permitted 6*38105Sbostic * provided that the above copyright notice and this paragraph are 7*38105Sbostic * duplicated in all such forms and that any documentation, 8*38105Sbostic * advertising materials, and other materials related to such 9*38105Sbostic * distribution and use acknowledge that the software was developed 10*38105Sbostic * by the University of California, Berkeley. The name of the 11*38105Sbostic * University may not be used to endorse or promote products derived 12*38105Sbostic * from this software without specific prior written permission. 13*38105Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*38105Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*38105Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621610Sdist */ 179670Slinton 1821610Sdist #ifndef lint 1921610Sdist char copyright[] = 20*38105Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 2121610Sdist All rights reserved.\n"; 22*38105Sbostic #endif /* not lint */ 239670Slinton 2421610Sdist #ifndef lint 25*38105Sbostic static char sccsid[] = "@(#)makedefs.c 5.5 (Berkeley) 05/23/89"; 26*38105Sbostic #endif /* not lint */ 2721610Sdist 289670Slinton /* 299670Slinton * Create a definitions file (e.g. .h) from an implementation file (e.g. .c). 309670Slinton * 319670Slinton * Usage is "makedefs source.c source.h" where source.h is to be created. 329670Slinton * 339670Slinton * Lines beginning with "public" or within a "#ifndef public ... #endif" 349670Slinton * block are copied to the new file. Initializations (e.g. "int x = 3") are 359670Slinton * omitted ("int x;" is output). 369670Slinton * 379670Slinton * Normally a temporary definitions file is created and compared to 389670Slinton * the given destination. If they are different, the temporary file 399670Slinton * is copied on top of the destination. This is so that dependencies 409670Slinton * when using "make" are not triggered. 419670Slinton * 429670Slinton * The "-f" option overrides this and forces the destination file to be created. 439670Slinton */ 449670Slinton 459670Slinton #include "defs.h" 469670Slinton #include <signal.h> 4737790Sbostic #include "pathnames.h" 489670Slinton 499670Slinton #define procedure void 509670Slinton 5133323Sdonn #define streqn(s1, s2, n) (strncmp(s1, s2, n) == 0) 5233323Sdonn 539670Slinton Boolean force; 549670Slinton Boolean copytext; 559670Slinton 569670Slinton String tmpname; 579670Slinton String modulename(); 589670Slinton procedure abnorm(); 599670Slinton 609670Slinton main(argc, argv) 619670Slinton int argc; 629670Slinton String argv[]; 639670Slinton { 649670Slinton extern String mktemp(); 659670Slinton String name; 669670Slinton File tmp; 679670Slinton Integer r; 689670Slinton Integer index; 699670Slinton 709670Slinton if (streq(argv[1], "-f")) { 719670Slinton force = true; 729670Slinton index = 2; 739670Slinton } else { 749670Slinton force = false; 759670Slinton index = 1; 769670Slinton } 779670Slinton if (argc - index > 2) { 789670Slinton fatal("usage: makedefs [ -f ] file.c [ file.h ]\n"); 799670Slinton } 809670Slinton tmp = nil; 819670Slinton if (freopen(argv[index], "r", stdin) == NULL) { 829670Slinton fatal("can't read %s", argv[index]); 839670Slinton } 849670Slinton signal(SIGINT, abnorm); 859670Slinton signal(SIGQUIT, abnorm); 869670Slinton if (index + 1 < argc) { 879670Slinton if (force) { 889670Slinton tmpname = argv[index + 1]; 899670Slinton } else { 9037790Sbostic tmpname = mktemp(_PATH_TMP); 919670Slinton } 929670Slinton tmp = freopen(tmpname, "w", stdout); 939670Slinton if (tmp == nil) { 949670Slinton fatal("can't write %s", tmpname); 959670Slinton } 969670Slinton } 979670Slinton copytext = false; 989670Slinton name = modulename(argv[index]); 999670Slinton printf("#ifndef %s\n", name); 1009670Slinton printf("#define %s\n", name); 1019670Slinton copy(); 1029670Slinton printf("#endif\n"); 1039670Slinton if (tmp != NULL and not force) { 1049670Slinton fclose(tmp); 1059670Slinton r = call("cmp", stdin, stderr, "-s", tmpname, argv[2], nil); 1069670Slinton if (r != 0) { 1079670Slinton r = call("cp", stdin, stderr, tmpname, argv[2], nil); 1089670Slinton if (r != 0) { 1099670Slinton fprintf(stderr, "can't create %s\n", argv[2]); 1109670Slinton } 1119670Slinton } 1129670Slinton unlink(tmpname); 1139670Slinton } 1149670Slinton quit(0); 1159670Slinton } 1169670Slinton 1179670Slinton String modulename(s) 1189670Slinton String s; 1199670Slinton { 1209670Slinton String r, i, j; 1219670Slinton static char buf[256]; 1229670Slinton 1239670Slinton strcpy(buf, s); 1249670Slinton i = rindex(buf, '/'); 1259670Slinton if (i == nil) { 1269670Slinton i = buf; 12733383Sdonn } else { 12833383Sdonn ++i; 1299670Slinton } 13033383Sdonn for (j = i; *j; j++) { 13133383Sdonn if (*j == '.') { 13233383Sdonn *j = '_'; 13333383Sdonn } 13433383Sdonn } 13533383Sdonn if (j > i && *--j == 'c') { 13633383Sdonn *j = 'h'; 13733383Sdonn } 13833383Sdonn return i; 1399670Slinton } 1409670Slinton 1419670Slinton copy() 1429670Slinton { 1439670Slinton register char *p; 14433323Sdonn integer nesting; 1459670Slinton char line[1024]; 1469670Slinton 1479670Slinton while (gets(line) != NULL) { 14833323Sdonn if (streqn(line, "#ifndef public", 14)) { 1499670Slinton copytext = true; 15033323Sdonn nesting = 1; 15133323Sdonn } else if (streqn(line, "public", 6)) { 1529670Slinton copydef(line); 1539670Slinton } else if (copytext) { 15433323Sdonn if (streqn(line, "#ifdef", 6) or streqn(line, "#ifndef", 7)) { 15533323Sdonn ++nesting; 15633323Sdonn printf("%s\n", line); 15733323Sdonn } else if (streqn(line, "#endif", 6)) { 15833323Sdonn --nesting; 15933323Sdonn if (nesting <= 0) { 16033323Sdonn copytext = false; 16133323Sdonn } else { 16233323Sdonn printf("%s\n", line); 16333323Sdonn } 16433323Sdonn } else { 16533323Sdonn printf("%s\n", line); 16633323Sdonn } 16733323Sdonn } else if ( 16833323Sdonn streqn(line, "#ifdef", 6) or 16933323Sdonn streqn(line, "#ifndef", 7) or 17033323Sdonn streqn(line, "#else", 5) or 17133323Sdonn streqn(line, "#endif", 6) 17233323Sdonn ) { 1739670Slinton printf("%s\n", line); 1749670Slinton } 1759670Slinton } 1769670Slinton } 1779670Slinton 1789670Slinton copydef(s) 1799670Slinton String s; 1809670Slinton { 1819670Slinton register char *p; 1829670Slinton register Boolean isproc; 1839670Slinton 1849670Slinton isproc = false; 1859670Slinton for (p = &s[7]; *p != '\0' and *p != '='; p++) { 1869670Slinton if (*p == '(') { 1879670Slinton isproc = true; 1889670Slinton printf("(/* "); 1899670Slinton } else if (*p == ')' and isproc and *(p+1) == '\0') { 1909670Slinton printf(" */)"); 1919670Slinton } else { 1929670Slinton putchar(*p); 1939670Slinton } 1949670Slinton } 1959670Slinton if (isproc or *p == '=') { 1969670Slinton putchar(';'); 1979670Slinton } 1989670Slinton putchar('\n'); 1999670Slinton } 2009670Slinton 2019670Slinton /* 2029670Slinton * Terminate program. 2039670Slinton */ 2049670Slinton 2059670Slinton procedure abnorm(signo) 2069670Slinton int signo; 2079670Slinton { 2089670Slinton unlink(tmpname); 2099670Slinton quit(signo); 2109670Slinton } 2119670Slinton 2129670Slinton quit(r) 2139670Slinton int r; 2149670Slinton { 2159670Slinton exit(r); 2169670Slinton } 2179670Slinton 2189670Slinton /* 2199670Slinton * No special error recovery strategy. 2209670Slinton */ 2219670Slinton 2229670Slinton erecover() 2239670Slinton { 2249670Slinton } 225