Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
/*
2
 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3
 * unrestricted use provided that this legend is included on all tape
4
 * media and as a part of the software program in whole or part.  Users
5
 * may copy or modify Sun RPC without charge, but are not authorized
6
 * to license or distribute it to anyone else except as part of a product or
7
 * program developed by the user or with the express written consent of
8
 * Sun Microsystems, Inc.
9
 *
10
 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13
 *
14
 * Sun RPC is provided with no support and without any obligation on the
15
 * part of Sun Microsystems, Inc. to assist in its use, correction,
16
 * modification or enhancement.
17
 *
18
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20
 * OR ANY PART THEREOF.
21
 *
22
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23
 * or profits or other special, indirect and consequential damages, even if
24
 * Sun has been advised of the possibility of such damages.
25
 *
26
 * Sun Microsystems, Inc.
27
 * 2550 Garcia Avenue
28
 * Mountain View, California  94043
29
 */
30
 
31
#ifndef lint
32
static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
33
#endif
34
 
35
/*
36
 * rpc_util.c, Utility routines for the RPC protocol compiler 
37
 */
38
#include <stdio.h>
39
#include <ctype.h>
40
#include "rpc_scan.h"
41
#include "rpc_parse.h"
42
#include "rpc_util.h"
43
 
44
#define ARGEXT "argument"
45
 
46
char curline[MAXLINESIZE];	/* current read line */
47
char *where = curline;		/* current point in line */
48
int linenum = 0;		/* current line number */
49
 
50
char *infilename;		/* input filename */
51
 
52
#define NFILES 7
53
char *outfiles[NFILES];		/* output file names */
54
int nfiles;
55
 
56
FILE *fout;			/* file pointer of current output */
57
FILE *fin;			/* file pointer of current input */
58
 
59
list *defined;			/* list of defined things */
60
 
61
/*
62
 * Reinitialize the world 
63
 */
64
reinitialize()
65
{
66
	memset(curline, 0, MAXLINESIZE);
67
	where = curline;
68
	linenum = 0;
69
   defined = NULL;
70
#if defined(__WATCOMC__)
71
   if (fin && fin != stdin)
72
      fclose(fin);
73
   fin = NULL;
74
   if (fout && fout != stdout)
75
      fclose(fout);
76
   fout = NULL;
77
#endif
78
}
79
 
80
/*
81
 * string equality 
82
 */
83
streq(a, b)
84
	char *a;
85
	char *b;
86
{
87
	return (strcmp(a, b) == 0);
88
}
89
 
90
/*
91
 * find a value in a list 
92
 */
93
definition *
94
findval(lst, val, cmp)
95
	list *lst;
96
	char *val;
97
	int (*cmp) ();
98
 
99
{
100
 
101
	for (; lst != NULL; lst = lst->next) {
102
		if ((*cmp) (lst->val, val)) {
103
			return (lst->val);
104
		}
105
	}
106
	return (NULL);
107
}
108
 
109
/*
110
 * store a value in a list 
111
 */
112
void
113
storeval(lstp, val)
114
	list **lstp;
115
	definition *val;
116
{
117
	list **l;
118
	list *lst;
119
 
120
 
121
	for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
122
	lst = ALLOC(list);
123
	lst->val = val;
124
	lst->next = NULL;
125
	*l = lst;
126
}
127
 
128
static
129
findit(def, type)
130
	definition *def;
131
	char *type;
132
{
133
	return (streq(def->def_name, type));
134
}
135
 
136
static char *
137
fixit(type, orig)
138
	char *type;
139
	char *orig;
140
{
141
	definition *def;
142
 
143
	def = (definition *) FINDVAL(defined, type, findit);
144
	if (def == NULL || def->def_kind != DEF_TYPEDEF) {
145
		return (orig);
146
	}
147
	switch (def->def.ty.rel) {
148
	case REL_VECTOR:
149
		return (def->def.ty.old_type);
150
	case REL_ALIAS:
151
		return (fixit(def->def.ty.old_type, orig));
152
	default:
153
		return (orig);
154
	}
155
}
156
 
157
char *
158
fixtype(type)
159
	char *type;
160
{
161
	return (fixit(type, type));
162
}
163
 
164
char *
165
stringfix(type)
166
	char *type;
167
{
168
	if (streq(type, "string")) {
169
		return ("wrapstring");
170
	} else {
171
		return (type);
172
	}
173
}
174
 
175
void
176
ptype(prefix, type, follow)
177
	char *prefix;
178
	char *type;
179
	int follow;
180
{
181
	if (prefix != NULL) {
182
		if (streq(prefix, "enum")) {
183
			f_print(fout, "enum ");
184
		} else {
185
			f_print(fout, "struct ");
186
		}
187
	}
188
	if (streq(type, "bool")) {
189
		f_print(fout, "bool_t ");
190
	} else if (streq(type, "string")) {
191
		f_print(fout, "char *");
192
	} else {
193
		f_print(fout, "%s ", follow ? fixtype(type) : type);
194
	}
195
}
196
 
197
static
198
typedefed(def, type)
199
	definition *def;
200
	char *type;
201
{
202
	if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
203
		return (0);
204
	} else {
205
		return (streq(def->def_name, type));
206
	}
207
}
208
 
209
isvectordef(type, rel)
210
	char *type;
211
	relation rel;
212
{
213
	definition *def;
214
 
215
	for (;;) {
216
		switch (rel) {
217
		case REL_VECTOR:
218
			return (!streq(type, "string"));
219
		case REL_ARRAY:
220
			return (0);
221
		case REL_POINTER:
222
			return (0);
223
		case REL_ALIAS:
224
			def = (definition *) FINDVAL(defined, type, typedefed);
225
			if (def == NULL) {
226
				return (0);
227
			}
228
			type = def->def.ty.old_type;
229
			rel = def->def.ty.rel;
230
		}
231
	}
232
}
233
 
234
char *
235
locase(str)
236
	char *str;
237
{
238
	char c;
239
	static char buf[100];
240
	char *p = buf;
241
 
242
	while (c = *str++) {
243
		*p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
244
	}
245
	*p = 0;
246
	return (buf);
247
}
248
 
249
void
250
pvname_svc(pname, vnum)
251
	char *pname;
252
	char *vnum;
253
{
254
	f_print(fout, "%s_%s_svc", locase(pname), vnum);
255
}
256
 
257
void
258
pvname(pname, vnum)
259
	char *pname;
260
	char *vnum;
261
{
262
	f_print(fout, "%s_%s", locase(pname), vnum);
263
}
264
 
265
/*
266
 * print a useful (?) error message, and then die 
267
 */
268
void
269
error(msg)
270
	char *msg;
271
{
272
	printwhere();
273
	f_print(stderr, "%s, line %d: ", infilename, linenum);
274
	f_print(stderr, "%s\n", msg);
275
	crash();
276
}
277
 
278
/*
279
 * Something went wrong, unlink any files that we may have created and then
280
 * die. 
281
 */
282
crash()
283
{
284
	int i;
285
 
286
	for (i = 0; i < nfiles; i++) {
287
		(void) unlink(outfiles[i]);
288
	}
289
	exit(1);
290
}
291
 
292
void
293
record_open(file)
294
	char *file;
295
{
296
	if (nfiles < NFILES) {
297
		outfiles[nfiles++] = file;
298
	} else {
299
		f_print(stderr, "too many files!\n");
300
		crash();
301
	}
302
}
303
 
304
static char expectbuf[100];
305
static char *toktostr();
306
 
307
/*
308
 * error, token encountered was not the expected one 
309
 */
310
void
311
expected1(exp1)
312
	tok_kind exp1;
313
{
314
	s_print(expectbuf, "expected '%s'",
315
		toktostr(exp1));
316
	error(expectbuf);
317
}
318
 
319
/*
320
 * error, token encountered was not one of two expected ones 
321
 */
322
void
323
expected2(exp1, exp2)
324
	tok_kind exp1, exp2;
325
{
326
	s_print(expectbuf, "expected '%s' or '%s'",
327
		toktostr(exp1),
328
		toktostr(exp2));
329
	error(expectbuf);
330
}
331
 
332
/*
333
 * error, token encountered was not one of 3 expected ones 
334
 */
335
void
336
expected3(exp1, exp2, exp3)
337
	tok_kind exp1, exp2, exp3;
338
{
339
	s_print(expectbuf, "expected '%s', '%s' or '%s'",
340
		toktostr(exp1),
341
		toktostr(exp2),
342
		toktostr(exp3));
343
	error(expectbuf);
344
}
345
 
346
void
347
tabify(f, tab)
348
	FILE *f;
349
	int tab;
350
{
351
	while (tab--) {
352
		(void) fputc('\t', f);
353
	}
354
}
355
 
356
 
357
static token tokstrings[] = {
358
			     {TOK_IDENT, "identifier"},
359
			     {TOK_CONST, "const"},
360
			     {TOK_RPAREN, ")"},
361
			     {TOK_LPAREN, "("},
362
			     {TOK_RBRACE, "}"},
363
			     {TOK_LBRACE, "{"},
364
			     {TOK_LBRACKET, "["},
365
			     {TOK_RBRACKET, "]"},
366
			     {TOK_STAR, "*"},
367
			     {TOK_COMMA, ","},
368
			     {TOK_EQUAL, "="},
369
			     {TOK_COLON, ":"},
370
			     {TOK_SEMICOLON, ";"},
371
			     {TOK_UNION, "union"},
372
			     {TOK_STRUCT, "struct"},
373
			     {TOK_SWITCH, "switch"},
374
			     {TOK_CASE, "case"},
375
			     {TOK_DEFAULT, "default"},
376
			     {TOK_ENUM, "enum"},
377
			     {TOK_TYPEDEF, "typedef"},
378
			     {TOK_INT, "int"},
379
			     {TOK_SHORT, "short"},
380
			     {TOK_LONG, "long"},
381
			     {TOK_UNSIGNED, "unsigned"},
382
			     {TOK_DOUBLE, "double"},
383
			     {TOK_FLOAT, "float"},
384
			     {TOK_CHAR, "char"},
385
			     {TOK_STRING, "string"},
386
			     {TOK_OPAQUE, "opaque"},
387
			     {TOK_BOOL, "bool"},
388
			     {TOK_VOID, "void"},
389
			     {TOK_PROGRAM, "program"},
390
			     {TOK_VERSION, "version"},
391
			     {TOK_EOF, "??????"}
392
};
393
 
394
static char *
395
toktostr(kind)
396
	tok_kind kind;
397
{
398
	token *sp;
399
 
400
	for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
401
	return (sp->str);
402
}
403
 
404
static
405
printbuf()
406
{
407
	char c;
408
	int i;
409
	int cnt;
410
 
411
#	define TABSIZE 4
412
 
413
	for (i = 0; c = curline[i]; i++) {
414
		if (c == '\t') {
415
			cnt = 8 - (i % TABSIZE);
416
			c = ' ';
417
		} else {
418
			cnt = 1;
419
		}
420
		while (cnt--) {
421
			(void) fputc(c, stderr);
422
		}
423
	}
424
}
425
 
426
static
427
printwhere()
428
{
429
	int i;
430
	char c;
431
	int cnt;
432
 
433
	printbuf();
434
	for (i = 0; i < where - curline; i++) {
435
		c = curline[i];
436
		if (c == '\t') {
437
			cnt = 8 - (i % TABSIZE);
438
		} else {
439
			cnt = 1;
440
		}
441
		while (cnt--) {
442
			(void) fputc('^', stderr);
443
		}
444
	}
445
	(void) fputc('\n', stderr);
446
}
447
 
448
char * 
449
make_argname(pname, vname) 
450
    char *pname;
451
    char *vname;
452
{
453
	char *name;
454
 
455
	name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
456
	if (!name) {
457
		fprintf(stderr, "failed in malloc");
458
		exit(1);
459
	}
460
	sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
461
	return(name);
462
}
463
 
464
bas_type *typ_list_h;
465
bas_type *typ_list_t;
466
 
467
add_type(len,type)
468
int len;
469
char *type;
470
{
471
  bas_type *ptr;
472
 
473
 
474
  if((ptr = (bas_type *) malloc(sizeof(bas_type)))== (bas_type *)NULL)
475
     {
476
	fprintf(stderr, "failed in malloc");
477
	exit(1);
478
	}
479
 
480
  ptr->name=type;
481
  ptr->length=len;
482
  ptr->next=NULL;
483
  if(typ_list_t == NULL)
484
    {
485
 
486
      typ_list_t=ptr;
487
      typ_list_h=ptr;
488
    }
489
  else
490
    {
491
 
492
    typ_list_t->next=ptr;
493
    typ_list_t=ptr;
494
    }
495
 
496
}
497
 
498
 
499
bas_type *find_type(type)
500
char *type;
501
{
502
  bas_type * ptr;
503
 
504
  ptr=typ_list_h;
505
 
506
 
507
  while(ptr != NULL)
508
    {
509
    if(strcmp(ptr->name,type) == 0)
510
           return(ptr);
511
    else
512
      ptr=ptr->next;
513
    };
514
return(NULL);
515
}
516