|
|
1.1 root 1: /*
2: * Convert old as format to new asm format.
3: */
4: #include <stdio.h>
5: #include <ctype.h>
6: #define MAXL 100
7:
8: #define START 0
9: #define INTOKEN 1
10: #define BSL 2
11: #define DQUOTE 3
12: #define SQUOTE 4
13: #define WHITE 5
14: #define ACOMM 6
15:
16: typedef struct locsym locsym;
17: struct locsym { /* one for local symbol */
18: int number; /* old local symbol number */
19: int def; /* 0 if undefined 1 if defined */
20: };
21: locsym locs[MAXL];
22: int top; /* highest defined symbol */
23: int lineno = 1; /* line number */
24: int rc; /* return code */
25: int octdig; /* octal digit ct */
26:
27: /*
28: * Report an error on stderr.
29: */
30: error(s)
31: char *s;
32: {
33: fprintf(stderr, "%d: %s\n", lineno, s);
34: rc = 1;
35: }
36: /*
37: * Find a local symbol in the right defined state.
38: */
39: locsym *
40: scanLoc(n, at)
41: {
42: register locsym *s;
43:
44: for (s = locs + (top - 1); s >= locs; s--)
45: if (n == s->number && at == s->def)
46: return (s);
47: return(NULL);
48: }
49:
50: /*
51: * A token has been read process it looking for local symbols.
52: * These are in the form [0-9]+[:bf]
53: * Where [:] requires a pstate of START and [bf] a pstate of white
54: */
55: static void
56: crunch(token, pstate)
57: char *token;
58: {
59: register locsym *s;
60: register i, c;
61: register char *p;
62: int sw;
63:
64: for (sw = i = 0, p = token; (c = *p++) && isdigit(c); ) {
65: sw = 1;
66: i = (i * 10) + (c - '0');
67: }
68: if (!sw || *p) /* mark nomatch */
69: c = 0;
70:
71: s = NULL;
72: switch (c) {
73: case ':':
74: if (START != pstate)
75: error(": local symbol after start of line");
76: /*
77: * if first use of this symbol build table entry.
78: * else mark it defined.
79: */
80: if (NULL == (s = scanLoc(i, 0))) {
81: s = locs + top++;
82: s->number = i;
83: break;
84: }
85: s->def = 1;
86: break;
87: case 'b':
88: if (WHITE != pstate)
89: error("b local symbol after start of line");
90: if (NULL == (s = scanLoc(i, 1)))
91: error("Unresolved local symbol");
92: break;
93: case 'f':
94: if (WHITE != pstate)
95: error("f local symbol after start of line");
96: /*
97: * If first use of this symbol build it
98: * else use it.
99: */
100: if (NULL == (s = scanLoc(i, 0))) {
101: s = locs + top++;
102: s->number = i;
103: s->def = 0;
104: break;
105: }
106: break;
107: }
108: if (NULL == s)
109: printf("%s", token);
110: else
111: printf("?%03d", (int)(s - locs));
112: }
113:
114: /*
115: * Read input to output finding tokens.
116: */
117: main()
118: {
119: register c, state, pstate;
120: char *tp, token[20];
121:
122: for (state = pstate = START; EOF != (c = getchar()); ) {
123: switch (state) {
124: case START:
125: if (isspace(c)) {
126: state = WHITE;
127: break;
128: }
129: case WHITE:
130: if (isalnum(c)) {
131: pstate = state;
132: state = INTOKEN;
133: tp = token;
134: *tp++ = c;
135: continue;
136: }
137: switch (c) {
138: case '"':
139: state = DQUOTE;
140: break;
141: case '\'':
142: state = SQUOTE;
143: octdig = 0;
144: break;
145: case '/':
146: state = ACOMM;
147: }
148: case ACOMM:
149: break;
150: case INTOKEN:
151: if ((':' == c) || isalnum(c)) {
152: *tp++ = c;
153: continue;
154: }
155: *tp = '\0';
156: crunch(token, pstate);
157: state = WHITE;
158: break;
159: case DQUOTE:
160: switch (c) {
161: case '\'':
162: pstate = state;
163: state = BSL;
164: break;
165: case '"':
166: state = WHITE;
167: break;
168: }
169: break;
170: case BSL:
171: if (SQUOTE != (state = pstate))
172: break;
173: if ('\\' == c) {
174: putchar(c);
175: c = '\'';
176: state = WHITE;
177: break;
178: }
179: octdig = 2; /* fall through */
180:
181: case SQUOTE:
182: if ('\\' == c) {
183: pstate = state;
184: state = BSL;
185: break;
186: }
187: if (isdigit(c) && octdig--)
188: break;
189: putchar(c); /* turn 'x to 'x' */
190: c = '\'';
191: state = WHITE;
192: }
193: putchar(c);
194: if ('\n' == c) {
195: lineno++;
196: state = START;
197: }
198: }
199: exit(rc);
200: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.