-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlint.c
372 lines (308 loc) · 7.39 KB
/
rlint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <stdio.h>
//#include <err.h>
#include <stdlib.h>
//#include <unistd.h>
#include <string.h>
#include <gsos.h>
#include <resources.h>
#include <memory.h>
#include "rlint.h"
struct {
ResType type;
ResID id;
} history[10];
unsigned level;
unsigned error_count;
unsigned flag_v = 0;
struct node {
struct node *next;
unsigned status;
ResType type;
ResID id;
};
struct node *processed_map[256];
void free_processed_map(void) {
struct node *e;
unsigned i;
for (i = 0; i < 256; ++i) {
e = processed_map[i];
while (e) {
struct node *next = e->next;
free(e);
e = next;
}
processed_map[i] = 0;
}
}
unsigned *processed(ResType type, ResID id) {
unsigned hash = 0xaaaa;
struct node *e;
hash ^= type;
hash ^= (unsigned)id;
hash ^= id >> 16;
hash ^= hash >> 8;
hash &= 0xff;
e = processed_map[hash];
while(e) {
if (e->type == type && e->id == id) return &e->status;
e = e->next;
}
e = malloc(sizeof(struct node));
e->next = processed_map[hash];
e->status = 0;
e->type = type;
e->id = id;
processed_map[hash] = e;
return &e->status;
}
char *ResName(ResType type) {
static char buffer[6] = { '$', 'x', 'x', 'x', 'x', 0 };
static char hex[] = "0123456789abcdef";
switch(type) {
case rIcon: return "rIcon";
case rPicture: return "rPicture";
case rControlList: return "rControlList";
case rControlTemplate: return "rControlTemplate";
case rC1InputString: return "rC1InputString";
case rPString: return "rPString";
case rStringList: return "rStringList";
case rMenuBar: return "rMenuBar";
case rMenu: return "rMenu";
case rMenuItem: return "rMenuItem";
case rTextForLETextBox2: return "rTextForLETextBox2";
case rCtlDefProc: return "rCtlDefProc";
case rCtlColorTbl: return "rCtlColorTbl";
case rWindParam1: return "rWindParam1";
case rWindParam2: return "rWindParam2";
case rWindColor: return "rWindColor";
case rTextBlock: return "rTextBlock";
case rStyleBlock: return "rStyleBlock";
case rToolStartup: return "rToolStartup";
case rResName: return "rResName";
case rAlertString: return "rAlertString";
case rText: return "rText";
case rCodeResource: return "rCodeResource";
case rCDEVCode: return "rCDEVCode";
case rCDEVFlags: return "rCDEVFlags";
case rTwoRects: return "rTwoRects";
case rFileType: return "rFileType";
case rListRef: return "rListRef";
case rCString: return "rCString";
case rXCMD: return "rXCMD";
case rXFCN: return "rXFCN";
case rErrorString: return "rErrorString";
case rKTransTable: return "rKTransTable";
case rWString: return "rWString";
case rC1OutputString: return "rC1OutputString";
case rSoundSample: return "rSoundSample";
case rTERuler: return "rTERuler";
case rFSequence: return "rFSequence";
case rCursor: return "rCursor";
case rItemStruct: return "rItemStruct";
case rVersion: return "rVersion";
case rComment: return "rComment";
case rBundle: return "rBundle";
case rFinderPath: return "rFinderPath";
case rPaletteWindow: return "rPaletteWindow";
case rTaggedStrings: return "rTaggedStrings";
case rPatternList: return "rPatternList";
case rRectList: return "rRectList";
case rPrintRecord: return "rPrintRecord";
case rFont: return "rFont";
}
buffer[1] = hex[(type >> 12) & 0x0f];
buffer[2] = hex[(type >> 8) & 0x0f];
buffer[3] = hex[(type >> 4) & 0x0f];
buffer[4] = hex[(type >> 0) & 0x0f];
return buffer;
}
void error(const char *msg) {
unsigned i;
for (i = 0; i < level; ++i) {
if (i > 0) fputs(" > ", stderr);
fprintf(stderr, "%s:%08lx", ResName(history[i].type), history[i].id);
}
fputs("\n ", stderr);
fputs(msg, stderr);
fputs("\n", stderr);
++error_count;
}
void check_rStringList(Handle h){
typedef struct StringList {
unsigned count;
Ref strings[1];
} StringList;
StringList *ptr = *(StringList **)h;
unsigned i;
for (i = 0; i < ptr->count; ++i) {
Ref ref = ptr->strings[i];
if (ref) check(rPString, ref);
}
}
void check(ResType type, ResID id) {
Handle h;
void (*callback)(Handle);
unsigned *status;
if (flag_v) {
fprintf(stdout," %s:%08lx\n", ResName(type), id);
}
history[level].type = type;
history[level].id = id;
++level;
if (id == 0) {
error("Invalid resource ID");
--level;
return;
}
status = processed(type, id);
if (*status == 2) {
/* missing */
error("Unable to load Resource");
--level;
return;
}
if (*status == 1) {
--level;
return;
}
switch(type) {
case rMenu: callback = check_rMenu; break;
case rMenuItem: callback = check_rMenuItem; break;
case rMenuBar: callback = check_rMenuBar; break;
case rItemStruct: callback = check_rItemStruct; break;
case rControlList: callback = check_rControlList; break;
case rControlTemplate: callback = check_rControlTemplate; break;
case rWindParam1: callback = check_rWindParam1; break;
case rStringList: callback = check_rStringList; break;
case rBundle: callback = check_rBundle; break;
case rAlertString:
case rErrorString:
callback = check_rAlertString;
break;
default: callback = 0;
}
h = LoadResource(type, id);
if (_toolErr) {
*status = 2;
error("Unable to load Resource");
--level;
return;
}
*status = 1;
if (callback) {
HLock(h);
callback(h);
HUnlock(h);
}
ReleaseResource(-1, type, id);
--level;
}
void usage(int rv) {
fputs("rlint [-v] file [...]\n", stderr);
exit(rv);
}
GSString255Ptr c2gs(const char *cp) {
GSString255Ptr gs;
int l = strlen(cp);
gs = malloc(l + 3);
if (gs) {
gs->length = l;
strcpy(gs->text, cp);
}
return gs;
}
void one_file(const char *name) {
GSString255Ptr gname;
unsigned rfd;
unsigned depth;
unsigned ti;
unsigned long ri;
if (flag_v) fprintf(stdout, "Checking %s\n", name);
gname = c2gs(name);
rfd = OpenResourceFile(0x8000 | readEnable, NULL, (Pointer)gname);
if (_toolErr) {
++error_count;
fprintf(stderr, "%s: OpenResourceFile: $%04x\n", name, _toolErr);
free(gname);
return;
}
depth = SetResourceFileDepth(1);
/* now iterate through all resource types... */
for (ti = 1; ; ++ti) {
ResType type = GetIndType(ti);
if (_toolErr == resIndexRange) break;
if (_toolErr) {
fprintf(stderr, "%s: GetIndType: $%04x\n", name, _toolErr);
continue;
}
switch(type) {
case rMenu:
case rMenuItem:
case rMenuBar:
case rItemStruct:
case rControlList:
case rControlTemplate:
case rWindParam1:
case rStringList:
case rBundle:
case rAlertString:
case rErrorString:
break;
default:
continue;
}
for (ri = 1; ; ++ri) {
ResID id = GetIndResource(type, ri);
if (_toolErr == resIndexRange) break;
if (_toolErr) {
fprintf(stderr, "%s: GetIndResource: $%04x\n", name, _toolErr);
continue;
}
check(type, id);
}
}
SetResourceFileDepth(depth);
CloseResourceFile(rfd);
free_processed_map();
free(gname);
}
int main(int argc, char **argv) {
int c;
unsigned i;
flag_v = 0;
error_count = 0;
level = 0;
memset(processed_map, 0, sizeof(processed_map));
memset(history, 0, sizeof(history));
for (i = 1; i < argc; ++i) {
char *cp = argv[i];
if (cp[0] != '-') break;
if (cp[1] == '-' && cp[2] == 0) {++i; break; }
if (cp[1] == 'v') flag_v = 1;
else usage(1);
}
argv += i;
argc -= i;
/*
for((c = getopt(argc, argv, "hv")) != -1) {
switch(c) {
case 'v': flag_v = 1;
case 'h':
usage(0);
case '?'
case ':'
usage(1);
}
}
argv += optind;
argc -= optind;
*/
if (argc == 0) usage(0);
ResourceStartUp(MMStartUp());
for (i = 0; i < argc; ++i) {
one_file(argv[i]);
}
ResourceShutDown();
if (error_count) return 1;
return 0;
}