Urdu language locale for India.
[glibc.git] / sunrpc / rpc_util.c
blob6a6c0e42e2bd6319ee27b92b7682a4e13f95a981
1 /*
2 * From: @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_util.c, Utility routines for the RPC protocol compiler
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include "rpc_scan.h"
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 #include "proto.h"
44 #define ARGEXT "argument"
46 char curline[MAXLINESIZE]; /* current read line */
47 const char *where = curline; /* current point in line */
48 int linenum = 0; /* current line number */
50 const char *infilename; /* input filename */
52 #define NFILES 7
53 const char *outfiles[NFILES]; /* output file names */
54 int nfiles;
56 FILE *fout; /* file pointer of current output */
57 FILE *fin; /* file pointer of current input */
59 list *defined; /* list of defined things */
61 static int findit (const definition * def, const char *type);
62 static const char *fixit (const char *type, const char *orig);
63 static int typedefed (const definition * def, const char *type);
64 static const char *toktostr (tok_kind kind);
65 static void printbuf (void);
66 static void printwhere (void);
69 * Reinitialize the world
71 void
72 reinitialize (void)
74 memset (curline, 0, MAXLINESIZE);
75 where = curline;
76 linenum = 0;
77 defined = NULL;
81 * string equality
83 int
84 streq (const char *a, const char *b)
86 return strcmp (a, b) == 0;
90 * find a value in a list
92 definition *
93 findval (list *lst, const char *val,
94 int (*cmp) (const definition *, const char *))
97 for (; lst != NULL; lst = lst->next)
99 if (cmp (lst->val, val))
101 return lst->val;
104 return NULL;
108 * store a value in a list
110 void
111 storeval (list **lstp, definition *val)
113 list **l;
114 list *lst;
117 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
118 lst = ALLOC (list);
119 lst->val = val;
120 lst->next = NULL;
121 *l = lst;
124 static int
125 findit (const definition * def, const char *type)
127 return streq (def->def_name, type);
130 static const char *
131 fixit (const char *type, const char *orig)
133 definition *def;
135 def = findval (defined, type, findit);
136 if (def == NULL || def->def_kind != DEF_TYPEDEF)
138 return orig;
140 switch (def->def.ty.rel)
142 case REL_VECTOR:
143 if (streq (def->def.ty.old_type, "opaque"))
144 return ("char");
145 else
146 return (def->def.ty.old_type);
147 case REL_ALIAS:
148 return (fixit (def->def.ty.old_type, orig));
149 default:
150 return orig;
154 const char *
155 fixtype (const char *type)
157 return fixit (type, type);
160 const char *
161 stringfix (const char *type)
163 if (streq (type, "string"))
165 return "wrapstring";
167 else
169 return type;
173 void
174 ptype (const char *prefix, const char *type, int follow)
176 if (prefix != NULL)
178 if (streq (prefix, "enum"))
180 f_print (fout, "enum ");
182 else
184 f_print (fout, "struct ");
187 if (streq (type, "bool"))
189 f_print (fout, "bool_t ");
191 else if (streq (type, "string"))
193 f_print (fout, "char *");
195 else
197 f_print (fout, "%s ", follow ? fixtype (type) : type);
201 static int
202 typedefed (const definition * def, const char *type)
204 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
206 return 0;
208 else
210 return streq (def->def_name, type);
215 isvectordef (const char *type, relation rel)
217 definition *def;
219 for (;;)
221 switch (rel)
223 case REL_VECTOR:
224 return !streq (type, "string");
225 case REL_ARRAY:
226 return 0;
227 case REL_POINTER:
228 return 0;
229 case REL_ALIAS:
230 def = findval (defined, type, typedefed);
231 if (def == NULL)
233 return 0;
235 type = def->def.ty.old_type;
236 rel = def->def.ty.rel;
241 char *
242 locase (const char *str)
244 char c;
245 static char buf[100];
246 char *p = buf;
248 while ((c = *str++) != 0)
250 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
252 *p = 0;
253 return buf;
256 void
257 pvname_svc (const char *pname, const char *vnum)
259 f_print (fout, "%s_%s_svc", locase (pname), vnum);
262 void
263 pvname (const char *pname, const char *vnum)
265 f_print (fout, "%s_%s", locase (pname), vnum);
269 * print a useful (?) error message, and then die
271 void
272 error (const char *msg)
274 printwhere ();
275 f_print (stderr, "%s, line %d: ", infilename, linenum);
276 f_print (stderr, "%s\n", msg);
277 crash ();
281 * Something went wrong, unlink any files that we may have created and then
282 * die.
284 void
285 crash (void)
287 int i;
289 for (i = 0; i < nfiles; i++)
291 unlink (outfiles[i]);
293 exit (1);
296 void
297 record_open (const char *file)
299 if (nfiles < NFILES)
301 outfiles[nfiles++] = file;
303 else
305 f_print (stderr, "too many files!\n");
306 crash ();
310 static char expectbuf[100];
313 * error, token encountered was not the expected one
315 void
316 expected1 (tok_kind exp1)
318 s_print (expectbuf, "expected '%s'",
319 toktostr (exp1));
320 error (expectbuf);
324 * error, token encountered was not one of two expected ones
326 void
327 expected2 (tok_kind exp1, tok_kind exp2)
329 s_print (expectbuf, "expected '%s' or '%s'",
330 toktostr (exp1),
331 toktostr (exp2));
332 error (expectbuf);
336 * error, token encountered was not one of 3 expected ones
338 void
339 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
341 s_print (expectbuf, "expected '%s', '%s' or '%s'",
342 toktostr (exp1),
343 toktostr (exp2),
344 toktostr (exp3));
345 error (expectbuf);
348 void
349 tabify (FILE * f, int tab)
351 while (tab--)
353 (void) fputc ('\t', f);
358 static const token tokstrings[] =
360 {TOK_IDENT, "identifier"},
361 {TOK_CONST, "const"},
362 {TOK_RPAREN, ")"},
363 {TOK_LPAREN, "("},
364 {TOK_RBRACE, "}"},
365 {TOK_LBRACE, "{"},
366 {TOK_LBRACKET, "["},
367 {TOK_RBRACKET, "]"},
368 {TOK_STAR, "*"},
369 {TOK_COMMA, ","},
370 {TOK_EQUAL, "="},
371 {TOK_COLON, ":"},
372 {TOK_SEMICOLON, ";"},
373 {TOK_UNION, "union"},
374 {TOK_STRUCT, "struct"},
375 {TOK_SWITCH, "switch"},
376 {TOK_CASE, "case"},
377 {TOK_DEFAULT, "default"},
378 {TOK_ENUM, "enum"},
379 {TOK_TYPEDEF, "typedef"},
380 {TOK_INT, "int"},
381 {TOK_SHORT, "short"},
382 {TOK_LONG, "long"},
383 {TOK_UNSIGNED, "unsigned"},
384 {TOK_DOUBLE, "double"},
385 {TOK_FLOAT, "float"},
386 {TOK_CHAR, "char"},
387 {TOK_STRING, "string"},
388 {TOK_OPAQUE, "opaque"},
389 {TOK_BOOL, "bool"},
390 {TOK_VOID, "void"},
391 {TOK_PROGRAM, "program"},
392 {TOK_VERSION, "version"},
393 {TOK_EOF, "??????"}
396 static const char *
397 toktostr (tok_kind kind)
399 const token *sp;
401 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
402 return sp->str;
405 static void
406 printbuf (void)
408 char c;
409 int i;
410 int cnt;
412 #define TABSIZE 4
414 for (i = 0; (c = curline[i]) != 0; i++)
416 if (c == '\t')
418 cnt = 8 - (i % TABSIZE);
419 c = ' ';
421 else
423 cnt = 1;
425 while (cnt--)
427 (void) fputc (c, stderr);
432 static void
433 printwhere (void)
435 int i;
436 char c;
437 int cnt;
439 printbuf ();
440 for (i = 0; i < where - curline; i++)
442 c = curline[i];
443 if (c == '\t')
445 cnt = 8 - (i % TABSIZE);
447 else
449 cnt = 1;
451 while (cnt--)
453 (void) fputc ('^', stderr);
456 (void) fputc ('\n', stderr);
459 char *
460 make_argname (const char *pname, const char *vname)
462 char *name;
464 name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
465 if (!name)
467 fprintf (stderr, "failed in malloc");
468 exit (1);
470 sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
471 return name;
474 bas_type *typ_list_h;
475 bas_type *typ_list_t;
477 void
478 add_type (int len, const char *type)
480 bas_type *ptr;
483 if ((ptr = malloc (sizeof (bas_type))) == NULL)
485 fprintf (stderr, "failed in malloc");
486 exit (1);
489 ptr->name = type;
490 ptr->length = len;
491 ptr->next = NULL;
492 if (typ_list_t == NULL)
495 typ_list_t = ptr;
496 typ_list_h = ptr;
498 else
501 typ_list_t->next = ptr;
502 typ_list_t = ptr;
508 bas_type *
509 find_type (const char *type)
511 bas_type *ptr;
513 ptr = typ_list_h;
516 while (ptr != NULL)
518 if (strcmp (ptr->name, type) == 0)
519 return ptr;
520 else
521 ptr = ptr->next;
523 return NULL;