|
|
1.1 root 1: /*
2: scan nm output and detect constructors and destructors for static objects.
3: the name on an nm output line is expected to be in the right hand margin.
4: the name is expected to be on the form __STD*_ or __STI*_ and less than
5: 100 characters long.
6: nm output lines are assumed to be less than 256 characters long.
7: constructors found are called by _main() called from main().
8: destructors found is called by exit().
9: return 0 if no constructor or destructor is found otherwise.
10: The output is ?
11:
12: */
13:
14: #include <stdio.h>
15: //extern int strcpy(char*, char*);
16: //extern char * strtok(char*, char*);
17:
18: struct sbuf {
19: sbuf* next;
20: char str[100];
21: sbuf(sbuf* l, char* p);
22: };
23:
24: sbuf::sbuf(sbuf* l, char* p)
25: {
26: next=l;
27: // strcpy(str,strtok(p," |"));
28: // ``unrolled'' since strtok() is not on bsd systems
29: register char* s = str;
30: for (register char c = *p++; c && c!=' ' && c!='|'; c = *p++) *s++ = c;
31: *s = 0;
32: }
33:
34: sbuf* dtor; // list of constructors
35: sbuf* ctor; // list of destructors
36:
37: void _main() { }; // make munch.c independent of libC.a
38: char* malloc(unsigned);
39: void* operator new(long s) { return malloc(s); }
40:
41: main (int, char* argv[])
42: {
43: char buf[256];
44: register char* p;
45:
46: newline:
47: p = buf;
48: for(;;) {
49: int c;
50: switch (c=getchar()) {
51: case EOF:
52: goto done;
53: case '\n':
54: { if (p == buf) goto newline; // ignore empty lines
55: *p = 0; // terminate string
56: p = buf;
57: while (*p++!='_') if (*p == 0) goto newline;
58: if (p[-2] == '.') goto newline; // RT: ignore ._STI
59: for (p--; *p == '_'; p++) ; // accept _STI and __STI
60: register char* st = p-1;
61: if (st[0]!='_' || st[1]!='S' || st[2]!='T') goto newline;
62: switch (st[3]) {
63: case 'D':
64: dtor = new sbuf(dtor,st);
65: goto newline;
66: case 'I':
67: ctor = new sbuf(ctor,st);
68: default:
69: goto newline;
70: }
71: }
72: default:
73: *p++ = c;
74: }
75: }
76:
77: done:
78: int cond = dtor||ctor;
79:
80: if (cond == 0) exit(0);
81:
82: printf("typedef int (*PFV)();\n"); // "int" to dodge bsd4.2 bug
83: if (ctor) {
84: for (sbuf* p = ctor; p; p=p->next) printf("int %s();\n",p->str);
85: printf("extern PFV _ctors[];\nPFV _ctors[] = {\n");
86: for (sbuf* q = ctor; q; q=q->next) printf("\t%s,\n",q->str);
87: printf("\t0\n};\n");
88: }
89:
90: if (dtor) {
91: for (sbuf* p = dtor; p; p=p->next) printf("int %s();\n",p->str);
92: printf("extern PFV _dtors[];\nPFV _dtors[] = {\n");
93: for (sbuf* q = dtor; q; q=q->next) printf("\t%s,\n",q->str);
94: printf("\t0\n};\n");
95: }
96:
97: exit(1);
98: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.