Tue Feb 20 23:05:16 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
[glibc/history.git] / sunrpc / rpc_parse.c
blob325f75fbfc8409aa85eb36adf1cede3ad9112a9a
1 /* @(#)rpc_parse.c 2.1 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #ifndef lint
31 static char sccsid[] = "@(#)rpc_parse.c 1.4 87/04/28 (C) 1987 SMI";
32 #endif
35 * rpc_parse.c, Parser for the RPC protocol compiler
36 * Copyright (C) 1987 Sun Microsystems, Inc.
38 #include <stdio.h>
39 #include "rpc_util.h"
40 #include "rpc_scan.h"
41 #include "rpc_parse.h"
44 * return the next definition you see
46 definition *
47 get_definition()
49 definition *defp;
50 token tok;
52 defp = ALLOC(definition);
53 get_token(&tok);
54 switch (tok.kind) {
55 case TOK_STRUCT:
56 def_struct(defp);
57 break;
58 case TOK_UNION:
59 def_union(defp);
60 break;
61 case TOK_TYPEDEF:
62 def_typedef(defp);
63 break;
64 case TOK_ENUM:
65 def_enum(defp);
66 break;
67 case TOK_PROGRAM:
68 def_program(defp);
69 break;
70 case TOK_CONST:
71 def_const(defp);
72 break;
73 case TOK_EOF:
74 return (NULL);
75 break;
76 default:
77 error(_("definition keyword expected"));
79 scan(TOK_SEMICOLON, &tok);
80 isdefined(defp);
81 return (defp);
84 static
85 isdefined(defp)
86 definition *defp;
88 STOREVAL(&defined, defp);
92 static
93 def_struct(defp)
94 definition *defp;
96 token tok;
97 declaration dec;
98 decl_list *decls;
99 decl_list **tailp;
101 defp->def_kind = DEF_STRUCT;
103 scan(TOK_IDENT, &tok);
104 defp->def_name = tok.str;
105 scan(TOK_LBRACE, &tok);
106 tailp = &defp->def.st.decls;
107 do {
108 get_declaration(&dec, DEF_STRUCT);
109 decls = ALLOC(decl_list);
110 decls->decl = dec;
111 *tailp = decls;
112 tailp = &decls->next;
113 scan(TOK_SEMICOLON, &tok);
114 peek(&tok);
115 } while (tok.kind != TOK_RBRACE);
116 get_token(&tok);
117 *tailp = NULL;
120 static
121 def_program(defp)
122 definition *defp;
124 token tok;
125 version_list *vlist;
126 version_list **vtailp;
127 proc_list *plist;
128 proc_list **ptailp;
130 defp->def_kind = DEF_PROGRAM;
131 scan(TOK_IDENT, &tok);
132 defp->def_name = tok.str;
133 scan(TOK_LBRACE, &tok);
134 vtailp = &defp->def.pr.versions;
135 scan(TOK_VERSION, &tok);
136 do {
137 scan(TOK_IDENT, &tok);
138 vlist = ALLOC(version_list);
139 vlist->vers_name = tok.str;
140 scan(TOK_LBRACE, &tok);
141 ptailp = &vlist->procs;
142 do {
143 plist = ALLOC(proc_list);
144 get_type(&plist->res_prefix, &plist->res_type, DEF_PROGRAM);
145 if (streq(plist->res_type, "opaque")) {
146 error(_("illegal result type"));
148 scan(TOK_IDENT, &tok);
149 plist->proc_name = tok.str;
150 scan(TOK_LPAREN, &tok);
151 get_type(&plist->arg_prefix, &plist->arg_type, DEF_PROGRAM);
152 if (streq(plist->arg_type, "opaque")) {
153 error("illegal argument type");
155 scan(TOK_RPAREN, &tok);
156 scan(TOK_EQUAL, &tok);
157 scan_num(&tok);
158 scan(TOK_SEMICOLON, &tok);
159 plist->proc_num = tok.str;
160 *ptailp = plist;
161 ptailp = &plist->next;
162 peek(&tok);
163 } while (tok.kind != TOK_RBRACE);
164 *vtailp = vlist;
165 vtailp = &vlist->next;
166 scan(TOK_RBRACE, &tok);
167 scan(TOK_EQUAL, &tok);
168 scan_num(&tok);
169 vlist->vers_num = tok.str;
170 scan(TOK_SEMICOLON, &tok);
171 scan2(TOK_VERSION, TOK_RBRACE, &tok);
172 } while (tok.kind == TOK_VERSION);
173 scan(TOK_EQUAL, &tok);
174 scan_num(&tok);
175 defp->def.pr.prog_num = tok.str;
176 *vtailp = NULL;
179 static
180 def_enum(defp)
181 definition *defp;
183 token tok;
184 enumval_list *elist;
185 enumval_list **tailp;
187 defp->def_kind = DEF_ENUM;
188 scan(TOK_IDENT, &tok);
189 defp->def_name = tok.str;
190 scan(TOK_LBRACE, &tok);
191 tailp = &defp->def.en.vals;
192 do {
193 scan(TOK_IDENT, &tok);
194 elist = ALLOC(enumval_list);
195 elist->name = tok.str;
196 elist->assignment = NULL;
197 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
198 if (tok.kind == TOK_EQUAL) {
199 scan_num(&tok);
200 elist->assignment = tok.str;
201 scan2(TOK_COMMA, TOK_RBRACE, &tok);
203 *tailp = elist;
204 tailp = &elist->next;
205 } while (tok.kind != TOK_RBRACE);
206 *tailp = NULL;
209 static
210 def_const(defp)
211 definition *defp;
213 token tok;
215 defp->def_kind = DEF_CONST;
216 scan(TOK_IDENT, &tok);
217 defp->def_name = tok.str;
218 scan(TOK_EQUAL, &tok);
219 scan2(TOK_IDENT, TOK_STRCONST, &tok);
220 defp->def.co = tok.str;
223 static
224 def_union(defp)
225 definition *defp;
227 token tok;
228 declaration dec;
229 case_list *cases;
230 case_list **tailp;
232 defp->def_kind = DEF_UNION;
233 scan(TOK_IDENT, &tok);
234 defp->def_name = tok.str;
235 scan(TOK_SWITCH, &tok);
236 scan(TOK_LPAREN, &tok);
237 get_declaration(&dec, DEF_UNION);
238 defp->def.un.enum_decl = dec;
239 tailp = &defp->def.un.cases;
240 scan(TOK_RPAREN, &tok);
241 scan(TOK_LBRACE, &tok);
242 scan(TOK_CASE, &tok);
243 while (tok.kind == TOK_CASE) {
244 scan(TOK_IDENT, &tok);
245 cases = ALLOC(case_list);
246 cases->case_name = tok.str;
247 scan(TOK_COLON, &tok);
248 get_declaration(&dec, DEF_UNION);
249 cases->case_decl = dec;
250 *tailp = cases;
251 tailp = &cases->next;
252 scan(TOK_SEMICOLON, &tok);
253 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
255 *tailp = NULL;
256 if (tok.kind == TOK_DEFAULT) {
257 scan(TOK_COLON, &tok);
258 get_declaration(&dec, DEF_UNION);
259 defp->def.un.default_decl = ALLOC(declaration);
260 *defp->def.un.default_decl = dec;
261 scan(TOK_SEMICOLON, &tok);
262 scan(TOK_RBRACE, &tok);
263 } else {
264 defp->def.un.default_decl = NULL;
269 static
270 def_typedef(defp)
271 definition *defp;
273 declaration dec;
275 defp->def_kind = DEF_TYPEDEF;
276 get_declaration(&dec, DEF_TYPEDEF);
277 defp->def_name = dec.name;
278 defp->def.ty.old_prefix = dec.prefix;
279 defp->def.ty.old_type = dec.type;
280 defp->def.ty.rel = dec.rel;
281 defp->def.ty.array_max = dec.array_max;
285 static
286 get_declaration(dec, dkind)
287 declaration *dec;
288 defkind dkind;
290 token tok;
292 get_type(&dec->prefix, &dec->type, dkind);
293 dec->rel = REL_ALIAS;
294 if (streq(dec->type, "void")) {
295 return;
297 scan2(TOK_STAR, TOK_IDENT, &tok);
298 if (tok.kind == TOK_STAR) {
299 dec->rel = REL_POINTER;
300 scan(TOK_IDENT, &tok);
302 dec->name = tok.str;
303 if (peekscan(TOK_LBRACKET, &tok)) {
304 if (dec->rel == REL_POINTER) {
305 error(_("no array-of-pointer declarations -- use typedef"));
307 dec->rel = REL_VECTOR;
308 scan_num(&tok);
309 dec->array_max = tok.str;
310 scan(TOK_RBRACKET, &tok);
311 } else if (peekscan(TOK_LANGLE, &tok)) {
312 if (dec->rel == REL_POINTER) {
313 error(_("no array-of-pointer declarations -- use typedef"));
315 dec->rel = REL_ARRAY;
316 if (peekscan(TOK_RANGLE, &tok)) {
317 dec->array_max = "~0"; /* unspecified size, use max */
318 } else {
319 scan_num(&tok);
320 dec->array_max = tok.str;
321 scan(TOK_RANGLE, &tok);
324 if (streq(dec->type, "opaque")) {
325 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
326 error(_("array declaration expected"));
328 } else if (streq(dec->type, "string")) {
329 if (dec->rel != REL_ARRAY) {
330 error(_("variable-length array declaration expected"));
336 static
337 get_type(prefixp, typep, dkind)
338 char **prefixp;
339 char **typep;
340 defkind dkind;
342 token tok;
344 *prefixp = NULL;
345 get_token(&tok);
346 switch (tok.kind) {
347 case TOK_IDENT:
348 *typep = tok.str;
349 break;
350 case TOK_STRUCT:
351 case TOK_ENUM:
352 case TOK_UNION:
353 *prefixp = tok.str;
354 scan(TOK_IDENT, &tok);
355 *typep = tok.str;
356 break;
357 case TOK_UNSIGNED:
358 unsigned_dec(typep);
359 break;
360 case TOK_SHORT:
361 *typep = "short";
362 (void) peekscan(TOK_INT, &tok);
363 break;
364 case TOK_LONG:
365 *typep = "long";
366 (void) peekscan(TOK_INT, &tok);
367 break;
368 case TOK_VOID:
369 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
370 error(_("voids allowed only inside union and program definitions"));
372 *typep = tok.str;
373 break;
374 case TOK_STRING:
375 case TOK_OPAQUE:
376 case TOK_CHAR:
377 case TOK_INT:
378 case TOK_FLOAT:
379 case TOK_DOUBLE:
380 case TOK_BOOL:
381 *typep = tok.str;
382 break;
383 default:
384 error(_("expected type specifier"));
389 static
390 unsigned_dec(typep)
391 char **typep;
393 token tok;
395 peek(&tok);
396 switch (tok.kind) {
397 case TOK_CHAR:
398 get_token(&tok);
399 *typep = "u_char";
400 break;
401 case TOK_SHORT:
402 get_token(&tok);
403 *typep = "u_short";
404 (void) peekscan(TOK_INT, &tok);
405 break;
406 case TOK_LONG:
407 get_token(&tok);
408 *typep = "u_long";
409 (void) peekscan(TOK_INT, &tok);
410 break;
411 case TOK_INT:
412 get_token(&tok);
413 *typep = "u_int";
414 break;
415 default:
416 *typep = "u_int";
417 break;