|
|
1.1 root 1: /*ident "@(#)ctrans:lib/static/munch.c 1.4.1.2" */
2: /**************************************************************************
3: Copyright (c) 1984 AT&T
4: All Rights Reserved
5:
6: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
7:
8: The copyright notice above does not evidence any
9: actual or intended publication of such source code.
10:
11: *****************************************************************************/
12: /*
13: scan nm output and detect constructors and destructors for static objects.
14: the name on an nm output line is expected to be in the right hand margin.
15: the name is expected to be on the form __std__ or __sti__ and less than
16: 100 characters long.
17: nm output lines are assumed to be less than 256 characters long.
18: constructors found are called by _main() called from main().
19: destructors found are called by exit().
20: return 1 if no constructor or destructor is found;
21: otherwise, returns 0
22: The output is redirected by CC into _ctdt.c
23:
24: */
25:
26: #include <stdio.h>
27: #include <ctype.h>
28: #include <stdlib.h>
29: //extern int strcpy(char*, char*);
30: //extern char * strtok(char*, char*);
31:
32: struct sbuf {
33: sbuf* next;
34: char str[100];
35: sbuf(sbuf* l, char* p);
36: };
37:
38: sbuf::sbuf(sbuf* l, char* p)
39: {
40: next=l;
41: // strcpy(str,strtok(p," |"));
42: // ``unrolled'' since strtok() is not on bsd systems
43: register char* s = str;
44: for (register char c = *p++; c && c!=' ' && c!='|'; c = *p++) *s++ = c;
45: *s = 0;
46: }
47:
48: sbuf* dtor; // list of constructors
49: sbuf* ctor; // list of destructors
50:
51: main (int, char* argv[])
52: {
53: char buf[256];
54: register char* p;
55:
56: newline:
57: p = buf;
58: for(;;) {
59: int c;
60: switch (c=getchar()) {
61: case EOF:
62: goto done;
63: case '\n':
64: { *p = 0; // terminate string
65: p = buf;
66: while (*p!='_') if (*p++ == 0) goto newline;
67: if (p!=buf && !isspace( *(p-1))) goto newline; // '_' not first character
68: // accept __st and ___st
69: if (*++p != '_') goto newline; // need '__'
70: if (*++p != '_')
71: { p--;
72: p--;
73: }
74: else
75: p--;
76:
77: register char* st = p;
78: if (st[0]!='_' || st[1]!='_' ||
79: st[2]!='s' || st[3]!='t' ||
80: st[5]!='_' || st[6]!='_' )
81: goto newline;
82: switch (st[4]) {
83: case 'd':
84: dtor = new sbuf(dtor,st);
85: goto newline;
86: case 'i':
87: ctor = new sbuf(ctor,st);
88: default:
89: goto newline;
90: }
91: }
92: default:
93: *p++ = c;
94: }
95: }
96:
97: done:
98: int cond = dtor||ctor;
99: if (cond == 0) exit(1);
100:
101: printf("typedef int (*PFV)();\n"); // "int" to dodge bsd4.2 bug
102: if (ctor) {
103: for (sbuf* p = ctor; p; p=p->next) printf("int %s();\n",p->str);
104: printf("extern PFV _ctors[];\nPFV _ctors[] = {\n");
105: for (sbuf* q = ctor; q; q=q->next) printf("\t%s,\n",q->str);
106: printf("\t0\n};\n");
107: }
108:
109: if (dtor) {
110: for (sbuf* p = dtor; p; p=p->next) printf("int %s();\n",p->str);
111: printf("extern PFV _dtors[];\nPFV _dtors[] = {\n");
112: for (sbuf* q = dtor; q; q=q->next) printf("\t%s,\n",q->str);
113: printf("\t0\n};\n");
114: }
115:
116: exit(0);
117: }
118:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.