rpc_parse.c revision 1.14 1 1.14 elad /* $NetBSD: rpc_parse.c,v 1.14 2006/03/20 17:03:08 elad Exp $ */
2 1.1 glass /*
3 1.1 glass * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 1.1 glass * unrestricted use provided that this legend is included on all tape
5 1.1 glass * media and as a part of the software program in whole or part. Users
6 1.1 glass * may copy or modify Sun RPC without charge, but are not authorized
7 1.1 glass * to license or distribute it to anyone else except as part of a product or
8 1.4 pk * program developed by the user or with the express written consent of
9 1.4 pk * Sun Microsystems, Inc.
10 1.4 pk *
11 1.1 glass * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 glass * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 glass * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.4 pk *
15 1.1 glass * Sun RPC is provided with no support and without any obligation on the
16 1.1 glass * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 glass * modification or enhancement.
18 1.4 pk *
19 1.1 glass * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 glass * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 glass * OR ANY PART THEREOF.
22 1.4 pk *
23 1.1 glass * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 glass * or profits or other special, indirect and consequential damages, even if
25 1.1 glass * Sun has been advised of the possibility of such damages.
26 1.4 pk *
27 1.1 glass * Sun Microsystems, Inc.
28 1.1 glass * 2550 Garcia Avenue
29 1.1 glass * Mountain View, California 94043
30 1.1 glass */
31 1.4 pk
32 1.13 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.13 jmc #include "nbtool_config.h"
34 1.13 jmc #endif
35 1.13 jmc
36 1.6 christos #include <sys/cdefs.h>
37 1.11 tv #if defined(__RCSID) && !defined(lint)
38 1.6 christos #if 0
39 1.4 pk static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
40 1.6 christos #else
41 1.14 elad __RCSID("$NetBSD: rpc_parse.c,v 1.14 2006/03/20 17:03:08 elad Exp $");
42 1.6 christos #endif
43 1.1 glass #endif
44 1.1 glass
45 1.1 glass /*
46 1.7 lukem * rpc_parse.c, Parser for the RPC protocol compiler
47 1.1 glass * Copyright (C) 1987 Sun Microsystems, Inc.
48 1.1 glass */
49 1.1 glass #include <stdio.h>
50 1.5 cgd #include <stdlib.h>
51 1.5 cgd #include <string.h>
52 1.4 pk #include "rpc/types.h"
53 1.1 glass #include "rpc_scan.h"
54 1.1 glass #include "rpc_parse.h"
55 1.4 pk #include "rpc_util.h"
56 1.4 pk
57 1.4 pk #define ARGNAME "arg"
58 1.4 pk
59 1.6 christos static void isdefined __P((definition *));
60 1.6 christos static void def_struct __P((definition *));
61 1.6 christos static void def_program __P((definition *));
62 1.6 christos static void def_enum __P((definition *));
63 1.6 christos static void def_const __P((definition *));
64 1.6 christos static void def_union __P((definition *));
65 1.6 christos static void check_type_name __P((char *, int));
66 1.6 christos static void def_typedef __P((definition *));
67 1.6 christos static void get_declaration __P((declaration *, defkind));
68 1.6 christos static void get_prog_declaration __P((declaration *, defkind, int));
69 1.6 christos static void get_type __P((char **, char **, defkind));
70 1.6 christos static void unsigned_dec __P((char **));
71 1.1 glass
72 1.1 glass /*
73 1.1 glass * return the next definition you see
74 1.1 glass */
75 1.1 glass definition *
76 1.1 glass get_definition()
77 1.1 glass {
78 1.1 glass definition *defp;
79 1.7 lukem token tok;
80 1.1 glass
81 1.1 glass defp = ALLOC(definition);
82 1.1 glass get_token(&tok);
83 1.1 glass switch (tok.kind) {
84 1.1 glass case TOK_STRUCT:
85 1.1 glass def_struct(defp);
86 1.1 glass break;
87 1.1 glass case TOK_UNION:
88 1.1 glass def_union(defp);
89 1.1 glass break;
90 1.1 glass case TOK_TYPEDEF:
91 1.1 glass def_typedef(defp);
92 1.1 glass break;
93 1.1 glass case TOK_ENUM:
94 1.1 glass def_enum(defp);
95 1.1 glass break;
96 1.1 glass case TOK_PROGRAM:
97 1.1 glass def_program(defp);
98 1.1 glass break;
99 1.1 glass case TOK_CONST:
100 1.1 glass def_const(defp);
101 1.1 glass break;
102 1.1 glass case TOK_EOF:
103 1.14 elad free(defp);
104 1.1 glass return (NULL);
105 1.1 glass default:
106 1.1 glass error("definition keyword expected");
107 1.1 glass }
108 1.1 glass scan(TOK_SEMICOLON, &tok);
109 1.1 glass isdefined(defp);
110 1.1 glass return (defp);
111 1.1 glass }
112 1.1 glass
113 1.6 christos static void
114 1.1 glass isdefined(defp)
115 1.1 glass definition *defp;
116 1.1 glass {
117 1.1 glass STOREVAL(&defined, defp);
118 1.1 glass }
119 1.1 glass
120 1.6 christos static void
121 1.1 glass def_struct(defp)
122 1.1 glass definition *defp;
123 1.1 glass {
124 1.7 lukem token tok;
125 1.1 glass declaration dec;
126 1.1 glass decl_list *decls;
127 1.1 glass decl_list **tailp;
128 1.1 glass
129 1.1 glass defp->def_kind = DEF_STRUCT;
130 1.1 glass
131 1.1 glass scan(TOK_IDENT, &tok);
132 1.1 glass defp->def_name = tok.str;
133 1.1 glass scan(TOK_LBRACE, &tok);
134 1.1 glass tailp = &defp->def.st.decls;
135 1.1 glass do {
136 1.1 glass get_declaration(&dec, DEF_STRUCT);
137 1.1 glass decls = ALLOC(decl_list);
138 1.1 glass decls->decl = dec;
139 1.1 glass *tailp = decls;
140 1.1 glass tailp = &decls->next;
141 1.1 glass scan(TOK_SEMICOLON, &tok);
142 1.1 glass peek(&tok);
143 1.1 glass } while (tok.kind != TOK_RBRACE);
144 1.1 glass get_token(&tok);
145 1.1 glass *tailp = NULL;
146 1.1 glass }
147 1.1 glass
148 1.6 christos static void
149 1.1 glass def_program(defp)
150 1.1 glass definition *defp;
151 1.1 glass {
152 1.7 lukem token tok;
153 1.4 pk declaration dec;
154 1.4 pk decl_list *decls;
155 1.4 pk decl_list **tailp;
156 1.1 glass version_list *vlist;
157 1.1 glass version_list **vtailp;
158 1.1 glass proc_list *plist;
159 1.1 glass proc_list **ptailp;
160 1.7 lukem int num_args;
161 1.7 lukem bool_t isvoid = FALSE; /* whether first argument is void */
162 1.1 glass defp->def_kind = DEF_PROGRAM;
163 1.1 glass scan(TOK_IDENT, &tok);
164 1.1 glass defp->def_name = tok.str;
165 1.1 glass scan(TOK_LBRACE, &tok);
166 1.1 glass vtailp = &defp->def.pr.versions;
167 1.4 pk tailp = &defp->def.st.decls;
168 1.1 glass scan(TOK_VERSION, &tok);
169 1.1 glass do {
170 1.1 glass scan(TOK_IDENT, &tok);
171 1.1 glass vlist = ALLOC(version_list);
172 1.1 glass vlist->vers_name = tok.str;
173 1.1 glass scan(TOK_LBRACE, &tok);
174 1.1 glass ptailp = &vlist->procs;
175 1.1 glass do {
176 1.4 pk /* get result type */
177 1.1 glass plist = ALLOC(proc_list);
178 1.7 lukem get_type(&plist->res_prefix, &plist->res_type,
179 1.7 lukem DEF_PROGRAM);
180 1.1 glass if (streq(plist->res_type, "opaque")) {
181 1.1 glass error("illegal result type");
182 1.1 glass }
183 1.1 glass scan(TOK_IDENT, &tok);
184 1.1 glass plist->proc_name = tok.str;
185 1.1 glass scan(TOK_LPAREN, &tok);
186 1.7 lukem /* get args - first one */
187 1.4 pk num_args = 1;
188 1.4 pk isvoid = FALSE;
189 1.7 lukem /* type of DEF_PROGRAM in the first
190 1.4 pk * get_prog_declaration and DEF_STURCT in the next
191 1.7 lukem * allows void as argument if it is the only argument */
192 1.4 pk get_prog_declaration(&dec, DEF_PROGRAM, num_args);
193 1.4 pk if (streq(dec.type, "void"))
194 1.7 lukem isvoid = TRUE;
195 1.4 pk decls = ALLOC(decl_list);
196 1.4 pk plist->args.decls = decls;
197 1.4 pk decls->decl = dec;
198 1.4 pk tailp = &decls->next;
199 1.4 pk /* get args */
200 1.7 lukem while (peekscan(TOK_COMMA, &tok)) {
201 1.7 lukem num_args++;
202 1.7 lukem get_prog_declaration(&dec, DEF_STRUCT,
203 1.7 lukem num_args);
204 1.7 lukem decls = ALLOC(decl_list);
205 1.7 lukem decls->decl = dec;
206 1.7 lukem *tailp = decls;
207 1.7 lukem if (streq(dec.type, "void"))
208 1.7 lukem isvoid = TRUE;
209 1.7 lukem tailp = &decls->next;
210 1.1 glass }
211 1.4 pk /* multiple arguments are only allowed in newstyle */
212 1.7 lukem if (!newstyle && num_args > 1) {
213 1.7 lukem error("only one argument is allowed");
214 1.4 pk }
215 1.7 lukem if (isvoid && num_args > 1) {
216 1.7 lukem error("illegal use of void in program definition");
217 1.4 pk }
218 1.4 pk *tailp = NULL;
219 1.1 glass scan(TOK_RPAREN, &tok);
220 1.1 glass scan(TOK_EQUAL, &tok);
221 1.1 glass scan_num(&tok);
222 1.1 glass scan(TOK_SEMICOLON, &tok);
223 1.1 glass plist->proc_num = tok.str;
224 1.4 pk plist->arg_num = num_args;
225 1.1 glass *ptailp = plist;
226 1.1 glass ptailp = &plist->next;
227 1.1 glass peek(&tok);
228 1.1 glass } while (tok.kind != TOK_RBRACE);
229 1.4 pk *ptailp = NULL;
230 1.1 glass *vtailp = vlist;
231 1.1 glass vtailp = &vlist->next;
232 1.1 glass scan(TOK_RBRACE, &tok);
233 1.1 glass scan(TOK_EQUAL, &tok);
234 1.1 glass scan_num(&tok);
235 1.1 glass vlist->vers_num = tok.str;
236 1.7 lukem /* make the argument structure name for each arg */
237 1.7 lukem for (plist = vlist->procs; plist != NULL;
238 1.4 pk plist = plist->next) {
239 1.4 pk plist->args.argname = make_argname(plist->proc_name,
240 1.7 lukem vlist->vers_num);
241 1.7 lukem /* free the memory ?? */
242 1.4 pk }
243 1.1 glass scan(TOK_SEMICOLON, &tok);
244 1.1 glass scan2(TOK_VERSION, TOK_RBRACE, &tok);
245 1.1 glass } while (tok.kind == TOK_VERSION);
246 1.1 glass scan(TOK_EQUAL, &tok);
247 1.1 glass scan_num(&tok);
248 1.1 glass defp->def.pr.prog_num = tok.str;
249 1.1 glass *vtailp = NULL;
250 1.1 glass }
251 1.1 glass
252 1.4 pk
253 1.6 christos static void
254 1.1 glass def_enum(defp)
255 1.1 glass definition *defp;
256 1.1 glass {
257 1.7 lukem token tok;
258 1.1 glass enumval_list *elist;
259 1.1 glass enumval_list **tailp;
260 1.1 glass
261 1.1 glass defp->def_kind = DEF_ENUM;
262 1.1 glass scan(TOK_IDENT, &tok);
263 1.1 glass defp->def_name = tok.str;
264 1.1 glass scan(TOK_LBRACE, &tok);
265 1.1 glass tailp = &defp->def.en.vals;
266 1.1 glass do {
267 1.1 glass scan(TOK_IDENT, &tok);
268 1.1 glass elist = ALLOC(enumval_list);
269 1.1 glass elist->name = tok.str;
270 1.1 glass elist->assignment = NULL;
271 1.1 glass scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
272 1.1 glass if (tok.kind == TOK_EQUAL) {
273 1.1 glass scan_num(&tok);
274 1.1 glass elist->assignment = tok.str;
275 1.1 glass scan2(TOK_COMMA, TOK_RBRACE, &tok);
276 1.1 glass }
277 1.1 glass *tailp = elist;
278 1.1 glass tailp = &elist->next;
279 1.1 glass } while (tok.kind != TOK_RBRACE);
280 1.1 glass *tailp = NULL;
281 1.1 glass }
282 1.1 glass
283 1.6 christos static void
284 1.1 glass def_const(defp)
285 1.1 glass definition *defp;
286 1.1 glass {
287 1.7 lukem token tok;
288 1.1 glass
289 1.1 glass defp->def_kind = DEF_CONST;
290 1.1 glass scan(TOK_IDENT, &tok);
291 1.1 glass defp->def_name = tok.str;
292 1.1 glass scan(TOK_EQUAL, &tok);
293 1.1 glass scan2(TOK_IDENT, TOK_STRCONST, &tok);
294 1.1 glass defp->def.co = tok.str;
295 1.1 glass }
296 1.1 glass
297 1.6 christos static void
298 1.1 glass def_union(defp)
299 1.1 glass definition *defp;
300 1.1 glass {
301 1.7 lukem token tok;
302 1.7 lukem declaration dec;
303 1.7 lukem case_list *cases;
304 1.7 lukem case_list **tailp;
305 1.7 lukem int flag;
306 1.7 lukem
307 1.7 lukem defp->def_kind = DEF_UNION;
308 1.7 lukem scan(TOK_IDENT, &tok);
309 1.7 lukem defp->def_name = tok.str;
310 1.7 lukem scan(TOK_SWITCH, &tok);
311 1.7 lukem scan(TOK_LPAREN, &tok);
312 1.7 lukem get_declaration(&dec, DEF_UNION);
313 1.7 lukem defp->def.un.enum_decl = dec;
314 1.7 lukem tailp = &defp->def.un.cases;
315 1.7 lukem scan(TOK_RPAREN, &tok);
316 1.7 lukem scan(TOK_LBRACE, &tok);
317 1.7 lukem scan(TOK_CASE, &tok);
318 1.7 lukem while (tok.kind == TOK_CASE) {
319 1.7 lukem scan2(TOK_IDENT, TOK_CHARCONST, &tok);
320 1.7 lukem cases = ALLOC(case_list);
321 1.7 lukem cases->case_name = tok.str;
322 1.7 lukem scan(TOK_COLON, &tok);
323 1.7 lukem /* now peek at next token */
324 1.7 lukem flag = 0;
325 1.7 lukem if (peekscan(TOK_CASE, &tok)) {
326 1.7 lukem
327 1.7 lukem do {
328 1.7 lukem scan2(TOK_IDENT, TOK_CHARCONST, &tok);
329 1.7 lukem cases->contflag = 1; /* continued case
330 1.7 lukem * statement */
331 1.7 lukem *tailp = cases;
332 1.7 lukem tailp = &cases->next;
333 1.7 lukem cases = ALLOC(case_list);
334 1.7 lukem cases->case_name = tok.str;
335 1.7 lukem scan(TOK_COLON, &tok);
336 1.7 lukem
337 1.7 lukem } while (peekscan(TOK_CASE, &tok));
338 1.7 lukem } else
339 1.7 lukem if (flag) {
340 1.7 lukem
341 1.7 lukem *tailp = cases;
342 1.7 lukem tailp = &cases->next;
343 1.7 lukem cases = ALLOC(case_list);
344 1.7 lukem };
345 1.7 lukem
346 1.7 lukem get_declaration(&dec, DEF_UNION);
347 1.7 lukem cases->case_decl = dec;
348 1.7 lukem cases->contflag = 0; /* no continued case statement */
349 1.7 lukem *tailp = cases;
350 1.7 lukem tailp = &cases->next;
351 1.7 lukem scan(TOK_SEMICOLON, &tok);
352 1.7 lukem
353 1.7 lukem scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
354 1.7 lukem }
355 1.7 lukem *tailp = NULL;
356 1.7 lukem if (tok.kind == TOK_DEFAULT) {
357 1.7 lukem scan(TOK_COLON, &tok);
358 1.7 lukem get_declaration(&dec, DEF_UNION);
359 1.7 lukem defp->def.un.default_decl = ALLOC(declaration);
360 1.7 lukem *defp->def.un.default_decl = dec;
361 1.7 lukem scan(TOK_SEMICOLON, &tok);
362 1.7 lukem scan(TOK_RBRACE, &tok);
363 1.7 lukem } else {
364 1.7 lukem defp->def.un.default_decl = NULL;
365 1.7 lukem }
366 1.4 pk }
367 1.4 pk
368 1.7 lukem static char *reserved_words[] = {
369 1.4 pk "array",
370 1.4 pk "bytes",
371 1.4 pk "destroy",
372 1.4 pk "free",
373 1.4 pk "getpos",
374 1.4 pk "inline",
375 1.4 pk "pointer",
376 1.4 pk "reference",
377 1.4 pk "setpos",
378 1.4 pk "sizeof",
379 1.4 pk "union",
380 1.4 pk "vector",
381 1.4 pk NULL
382 1.4 pk };
383 1.4 pk
384 1.7 lukem static char *reserved_types[] = {
385 1.4 pk "opaque",
386 1.4 pk "string",
387 1.4 pk NULL
388 1.4 pk };
389 1.4 pk /* check that the given name is not one that would eventually result in
390 1.4 pk xdr routines that would conflict with internal XDR routines. */
391 1.6 christos static void
392 1.7 lukem check_type_name(name, new_type)
393 1.7 lukem int new_type;
394 1.7 lukem char *name;
395 1.7 lukem {
396 1.7 lukem int i;
397 1.7 lukem char tmp[100];
398 1.7 lukem
399 1.7 lukem for (i = 0; reserved_words[i] != NULL; i++) {
400 1.7 lukem if (strcmp(name, reserved_words[i]) == 0) {
401 1.7 lukem sprintf(tmp,
402 1.7 lukem "illegal (reserved) name :\'%s\' in type definition", name);
403 1.7 lukem error(tmp);
404 1.7 lukem }
405 1.7 lukem }
406 1.7 lukem if (new_type) {
407 1.7 lukem for (i = 0; reserved_types[i] != NULL; i++) {
408 1.7 lukem if (strcmp(name, reserved_types[i]) == 0) {
409 1.7 lukem sprintf(tmp,
410 1.7 lukem "illegal (reserved) name :\'%s\' in type definition", name);
411 1.7 lukem error(tmp);
412 1.7 lukem }
413 1.7 lukem }
414 1.7 lukem }
415 1.1 glass }
416 1.1 glass
417 1.6 christos static void
418 1.1 glass def_typedef(defp)
419 1.1 glass definition *defp;
420 1.1 glass {
421 1.1 glass declaration dec;
422 1.1 glass
423 1.1 glass defp->def_kind = DEF_TYPEDEF;
424 1.1 glass get_declaration(&dec, DEF_TYPEDEF);
425 1.1 glass defp->def_name = dec.name;
426 1.7 lukem check_type_name(dec.name, 1);
427 1.1 glass defp->def.ty.old_prefix = dec.prefix;
428 1.1 glass defp->def.ty.old_type = dec.type;
429 1.1 glass defp->def.ty.rel = dec.rel;
430 1.1 glass defp->def.ty.array_max = dec.array_max;
431 1.1 glass }
432 1.1 glass
433 1.6 christos static void
434 1.1 glass get_declaration(dec, dkind)
435 1.1 glass declaration *dec;
436 1.1 glass defkind dkind;
437 1.1 glass {
438 1.7 lukem token tok;
439 1.1 glass
440 1.1 glass get_type(&dec->prefix, &dec->type, dkind);
441 1.1 glass dec->rel = REL_ALIAS;
442 1.1 glass if (streq(dec->type, "void")) {
443 1.1 glass return;
444 1.1 glass }
445 1.7 lukem check_type_name(dec->type, 0);
446 1.4 pk
447 1.1 glass scan2(TOK_STAR, TOK_IDENT, &tok);
448 1.1 glass if (tok.kind == TOK_STAR) {
449 1.1 glass dec->rel = REL_POINTER;
450 1.1 glass scan(TOK_IDENT, &tok);
451 1.1 glass }
452 1.1 glass dec->name = tok.str;
453 1.1 glass if (peekscan(TOK_LBRACKET, &tok)) {
454 1.1 glass if (dec->rel == REL_POINTER) {
455 1.1 glass error("no array-of-pointer declarations -- use typedef");
456 1.1 glass }
457 1.1 glass dec->rel = REL_VECTOR;
458 1.1 glass scan_num(&tok);
459 1.1 glass dec->array_max = tok.str;
460 1.1 glass scan(TOK_RBRACKET, &tok);
461 1.7 lukem } else
462 1.7 lukem if (peekscan(TOK_LANGLE, &tok)) {
463 1.7 lukem if (dec->rel == REL_POINTER) {
464 1.7 lukem error("no array-of-pointer declarations -- use typedef");
465 1.7 lukem }
466 1.7 lukem dec->rel = REL_ARRAY;
467 1.7 lukem if (peekscan(TOK_RANGLE, &tok)) {
468 1.12 christos dec->array_max = "(u_int)~0";
469 1.12 christos /* unspecified size, use * max */
470 1.7 lukem } else {
471 1.7 lukem scan_num(&tok);
472 1.7 lukem dec->array_max = tok.str;
473 1.7 lukem scan(TOK_RANGLE, &tok);
474 1.7 lukem }
475 1.1 glass }
476 1.1 glass if (streq(dec->type, "opaque")) {
477 1.1 glass if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
478 1.1 glass error("array declaration expected");
479 1.1 glass }
480 1.7 lukem } else
481 1.7 lukem if (streq(dec->type, "string")) {
482 1.7 lukem if (dec->rel != REL_ARRAY) {
483 1.7 lukem error("variable-length array declaration expected");
484 1.7 lukem }
485 1.1 glass }
486 1.1 glass }
487 1.1 glass
488 1.6 christos static void
489 1.4 pk get_prog_declaration(dec, dkind, num)
490 1.4 pk declaration *dec;
491 1.4 pk defkind dkind;
492 1.7 lukem int num; /* arg number */
493 1.4 pk {
494 1.7 lukem token tok;
495 1.10 bouyer char name[255]; /* argument name */
496 1.4 pk
497 1.7 lukem if (dkind == DEF_PROGRAM) {
498 1.7 lukem peek(&tok);
499 1.7 lukem if (tok.kind == TOK_RPAREN) { /* no arguments */
500 1.7 lukem dec->rel = REL_ALIAS;
501 1.7 lukem dec->type = "void";
502 1.7 lukem dec->prefix = NULL;
503 1.7 lukem dec->name = NULL;
504 1.7 lukem return;
505 1.7 lukem }
506 1.4 pk }
507 1.4 pk get_type(&dec->prefix, &dec->type, dkind);
508 1.4 pk dec->rel = REL_ALIAS;
509 1.7 lukem if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
510 1.4 pk strcpy(name, tok.str);
511 1.7 lukem else
512 1.7 lukem sprintf(name, "%s%d", ARGNAME, num); /* default name of
513 1.7 lukem * argument */
514 1.7 lukem
515 1.7 lukem dec->name = (char *) strdup(name);
516 1.4 pk
517 1.4 pk if (streq(dec->type, "void")) {
518 1.4 pk return;
519 1.4 pk }
520 1.4 pk if (streq(dec->type, "opaque")) {
521 1.4 pk error("opaque -- illegal argument type");
522 1.4 pk }
523 1.7 lukem if (peekscan(TOK_STAR, &tok)) {
524 1.7 lukem if (streq(dec->type, "string")) {
525 1.7 lukem error("pointer to string not allowed in program arguments\n");
526 1.7 lukem }
527 1.4 pk dec->rel = REL_POINTER;
528 1.7 lukem if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
529 1.7 lukem dec->name = (char *) strdup(tok.str);
530 1.7 lukem }
531 1.7 lukem if (peekscan(TOK_LANGLE, &tok)) {
532 1.7 lukem if (!streq(dec->type, "string")) {
533 1.7 lukem error("arrays cannot be declared as arguments to procedures -- use typedef");
534 1.7 lukem }
535 1.4 pk dec->rel = REL_ARRAY;
536 1.4 pk if (peekscan(TOK_RANGLE, &tok)) {
537 1.12 christos dec->array_max = "(u_int)~0";
538 1.12 christos /* unspecified size, use max */
539 1.4 pk } else {
540 1.4 pk scan_num(&tok);
541 1.4 pk dec->array_max = tok.str;
542 1.4 pk scan(TOK_RANGLE, &tok);
543 1.4 pk }
544 1.4 pk }
545 1.4 pk if (streq(dec->type, "string")) {
546 1.7 lukem if (dec->rel != REL_ARRAY) { /* .x specifies just string as
547 1.7 lukem * type of argument - make it
548 1.7 lukem * string<> */
549 1.4 pk dec->rel = REL_ARRAY;
550 1.12 christos dec->array_max = "(u_int)~0";
551 1.12 christos /* unspecified size, use max */
552 1.4 pk }
553 1.4 pk }
554 1.4 pk }
555 1.4 pk
556 1.4 pk
557 1.1 glass
558 1.6 christos static void
559 1.1 glass get_type(prefixp, typep, dkind)
560 1.7 lukem char **prefixp;
561 1.7 lukem char **typep;
562 1.1 glass defkind dkind;
563 1.1 glass {
564 1.7 lukem token tok;
565 1.1 glass
566 1.1 glass *prefixp = NULL;
567 1.1 glass get_token(&tok);
568 1.1 glass switch (tok.kind) {
569 1.1 glass case TOK_IDENT:
570 1.1 glass *typep = tok.str;
571 1.1 glass break;
572 1.1 glass case TOK_STRUCT:
573 1.1 glass case TOK_ENUM:
574 1.1 glass case TOK_UNION:
575 1.1 glass *prefixp = tok.str;
576 1.1 glass scan(TOK_IDENT, &tok);
577 1.1 glass *typep = tok.str;
578 1.1 glass break;
579 1.1 glass case TOK_UNSIGNED:
580 1.1 glass unsigned_dec(typep);
581 1.1 glass break;
582 1.1 glass case TOK_SHORT:
583 1.9 lukem *typep = "short";
584 1.1 glass (void) peekscan(TOK_INT, &tok);
585 1.1 glass break;
586 1.1 glass case TOK_LONG:
587 1.9 lukem *typep = "long";
588 1.1 glass (void) peekscan(TOK_INT, &tok);
589 1.1 glass break;
590 1.1 glass case TOK_VOID:
591 1.1 glass if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
592 1.4 pk error("voids allowed only inside union and program definitions with one argument");
593 1.1 glass }
594 1.1 glass *typep = tok.str;
595 1.1 glass break;
596 1.1 glass case TOK_STRING:
597 1.1 glass case TOK_OPAQUE:
598 1.1 glass case TOK_CHAR:
599 1.9 lukem case TOK_INT:
600 1.1 glass case TOK_FLOAT:
601 1.1 glass case TOK_DOUBLE:
602 1.1 glass case TOK_BOOL:
603 1.1 glass *typep = tok.str;
604 1.1 glass break;
605 1.1 glass default:
606 1.1 glass error("expected type specifier");
607 1.1 glass }
608 1.1 glass }
609 1.1 glass
610 1.6 christos static void
611 1.1 glass unsigned_dec(typep)
612 1.7 lukem char **typep;
613 1.1 glass {
614 1.7 lukem token tok;
615 1.1 glass
616 1.1 glass peek(&tok);
617 1.1 glass switch (tok.kind) {
618 1.1 glass case TOK_CHAR:
619 1.1 glass get_token(&tok);
620 1.1 glass *typep = "u_char";
621 1.1 glass break;
622 1.1 glass case TOK_SHORT:
623 1.1 glass get_token(&tok);
624 1.9 lukem *typep = "u_short";
625 1.1 glass (void) peekscan(TOK_INT, &tok);
626 1.1 glass break;
627 1.1 glass case TOK_LONG:
628 1.1 glass get_token(&tok);
629 1.9 lukem *typep = "u_long";
630 1.1 glass (void) peekscan(TOK_INT, &tok);
631 1.1 glass break;
632 1.1 glass case TOK_INT:
633 1.1 glass get_token(&tok);
634 1.9 lukem *typep = "u_int";
635 1.1 glass break;
636 1.1 glass default:
637 1.9 lukem *typep = "u_int";
638 1.1 glass break;
639 1.1 glass }
640 1.1 glass }
641