Commit | Line | Data |
---|---|---|
9e8e5516 C |
1 | /* posy.c - PEPY optional structure-generator (yacc-based) */ |
2 | ||
3 | /* OPEN QUESTIONS: | |
4 | ||
5 | How to do smarter DEFAULT determination for the other types and NULLs | |
6 | ||
7 | Perhaps pull-up primitive IDentifiers | |
8 | ||
9 | Abort a CHOICE encoding if the structure is empty | |
10 | ||
11 | ||
12 | HEURISTICS | |
13 | ||
14 | 1. LANGUAGE SIMPLIFICATIONS: | |
15 | ||
16 | ||
17 | Pull-up uni-member SEQUENCEs/SETs/CHOICEs | |
18 | ||
19 | ||
20 | 2. LANGUAGE ASSUMPTIONS: | |
21 | ||
22 | Unique tags to avoid conflicts for internal structures (-h1 option) | |
23 | ||
24 | ||
25 | 3. STYLE ISSUES: | |
26 | ||
27 | SEQUENCE/SET OF Type should have Type be an ID for nicer naming | |
28 | */ | |
29 | ||
30 | #ifndef lint | |
31 | static char *rcsid = "$Header: /f/osi/pepy/RCS/posy.c,v 7.6 91/02/22 09:35:13 mrose Interim $"; | |
32 | #endif | |
33 | ||
34 | /* | |
35 | * $Header: /f/osi/pepy/RCS/posy.c,v 7.6 91/02/22 09:35:13 mrose Interim $ | |
36 | * | |
37 | * | |
38 | * $Log: posy.c,v $ | |
39 | * Revision 7.6 91/02/22 09:35:13 mrose | |
40 | * Interim 6.8 | |
41 | * | |
42 | * Revision 7.5 90/10/17 11:51:24 mrose | |
43 | * sync | |
44 | * | |
45 | * Revision 7.4 90/09/07 17:35:09 mrose | |
46 | * touch-up | |
47 | * | |
48 | * Revision 7.3 90/02/23 17:50:09 mrose | |
49 | * update | |
50 | * | |
51 | * Revision 7.2 90/02/19 13:09:35 mrose | |
52 | * update | |
53 | * | |
54 | * Revision 7.1 90/01/11 18:37:05 mrose | |
55 | * real-sync | |
56 | * | |
57 | * Revision 7.0 89/11/23 22:11:59 mrose | |
58 | * Release 6.0 | |
59 | * | |
60 | */ | |
61 | ||
62 | /* | |
63 | * NOTICE | |
64 | * | |
65 | * Acquisition, use, and distribution of this module and related | |
66 | * materials are subject to the restrictions of a license agreement. | |
67 | * Consult the Preface in the User's Manual for the full terms of | |
68 | * this agreement. | |
69 | * | |
70 | */ | |
71 | ||
72 | ||
73 | #include <ctype.h> | |
74 | #include <stdio.h> | |
75 | #include <varargs.h> | |
76 | #include "pepy.h" | |
77 | ||
78 | ||
79 | #define SVAL(s) ((s) ? (s) : "") | |
80 | #define PARVAL(s) ((s) ? (s) : "parm") | |
81 | ||
82 | /* \f DATA */ | |
83 | ||
84 | static int aflag = 0; | |
85 | int Cflag = 0; /* posy */ | |
86 | int dflag = 0; | |
87 | int Pflag = 0; /* pepy compat ... */ | |
88 | char *bflag = NULL; /* .. */ | |
89 | char *module_actions = NULL; | |
90 | int pepydebug = 0; | |
91 | int doexternals = 1; | |
92 | static int fflag = 0; | |
93 | static int linepos = 0; | |
94 | static int mflag = 0; | |
95 | static int sflag = 0; | |
96 | ||
97 | #define hflag (options[0]) | |
98 | #define Hflag (options[1]) | |
99 | #define h2flag (options[2]) | |
100 | #define NOPTIONS 3 | |
101 | ||
102 | static int options[NOPTIONS]; | |
103 | ||
104 | static char *eval = NULLCP; | |
105 | ||
106 | char *mymodule = ""; | |
107 | OID mymoduleid = NULLOID; | |
108 | static char modulename[BUFSIZ]; | |
109 | ||
110 | int yysection = YP_DECODER; | |
111 | char *yyencpref = "encode"; | |
112 | char *yydecpref = "decode"; | |
113 | char *yyprfpref = "print"; | |
114 | char *yyencdflt = "encode"; | |
115 | char *yydecdflt = "decode"; | |
116 | char *yyprfdflt = "print"; | |
117 | ||
118 | static char *classes[] = { | |
119 | "UNIVERSAL ", | |
120 | "APPLICATION ", | |
121 | "", | |
122 | "PRIVATE " | |
123 | }; | |
124 | ||
125 | static char *tags[] = { | |
126 | "", "BOOLEAN", "INTEGER", "INTEGER", "BIT STRING", "BIT STRING", | |
127 | "OCTET STRING", "NULL", "SEQUENCE", "SEQUENCE OF", "SEQUENCE", "SET", | |
128 | "SET OF", "SET", "CHOICE", "ANY", "OBJECT IDENTIFIER", "", "ENUMERATED", | |
129 | "REAL", | |
130 | ||
131 | NULL | |
132 | }; | |
133 | ||
134 | static char autogen[BUFSIZ]; | |
135 | ||
136 | char *sysin = NULLCP; | |
137 | static char sysout[BUFSIZ]; | |
138 | static char sysdef[BUFSIZ]; | |
139 | static char sysact[BUFSIZ]; | |
140 | ||
141 | static FILE *fact; | |
142 | static FILE *fdef; | |
143 | ||
144 | typedef struct modlist { | |
145 | char *md_module; | |
146 | ||
147 | struct modlist *md_next; | |
148 | } modlist, *MD; | |
149 | #define NULLMD ((MD) 0) | |
150 | ||
151 | static MD mymodules = NULLMD; | |
152 | ||
153 | typedef struct symlist { | |
154 | char *sy_encpref; | |
155 | char *sy_decpref; | |
156 | char *sy_prfpref; | |
157 | char *sy_module; | |
158 | char *sy_name; | |
159 | ||
160 | YP sy_type; | |
161 | ||
162 | struct symlist *sy_next; | |
163 | } symlist, *SY; | |
164 | #define NULLSY ((SY) 0) | |
165 | ||
166 | static SY mysymbols = NULLSY; | |
167 | ||
168 | ||
169 | char *gensym (), *modsym (), *array (); | |
170 | MD lookup_module (); | |
171 | SY new_symbol (), add_symbol (); | |
172 | static double val2real (); | |
173 | static void prime_default (); | |
174 | YP lookup_type (); | |
175 | ||
176 | /* \f MAIN */ | |
177 | ||
178 | /* ARGSUSED */ | |
179 | ||
180 | main (argc, argv, envp) | |
181 | int argc; | |
182 | char **argv, | |
183 | **envp; | |
184 | { | |
185 | int i; | |
186 | register char *cp, | |
187 | *dp; | |
188 | ||
189 | dp = pepyversion + strlen ("pepy "); | |
190 | fprintf (stderr, "posy %s\n", dp); | |
191 | ||
192 | sysout[0] = sysdef[0] = sysact[0] = NULL; | |
193 | for (argc--, argv++; argc > 0; argc--, argv++) { | |
194 | cp = *argv; | |
195 | ||
196 | if (strcmp (cp, "-a") == 0) { | |
197 | aflag++; | |
198 | continue; | |
199 | } | |
200 | if (strcmp (cp, "-d") == 0) { | |
201 | dflag++; | |
202 | continue; | |
203 | } | |
204 | if (strcmp (cp, "-f") == 0) { | |
205 | dflag++, fflag++; | |
206 | continue; | |
207 | } | |
208 | if (strncmp (cp, "-h", 2) == 0) { | |
209 | if (cp[2] == NULL) { | |
210 | hflag++; | |
211 | continue; | |
212 | } | |
213 | if (sscanf (cp + 2, "%d", &i) != 1 || i >= NOPTIONS) | |
214 | goto usage; | |
215 | hflag++, options[i]++; | |
216 | continue; | |
217 | } | |
218 | if (strcmp (cp, "-m") == 0) { | |
219 | mflag++; | |
220 | continue; | |
221 | } | |
222 | if (strcmp (cp, "-o") == 0) { | |
223 | if (sysout[0]) { | |
224 | fprintf (stderr, "too many output files\n"); | |
225 | exit (1); | |
226 | } | |
227 | argc--, argv++; | |
228 | if ((cp = *argv) == NULL || (*cp == '-' && cp[1] != NULL)) | |
229 | goto usage; | |
230 | (void) strcpy (sysout, cp); | |
231 | ||
232 | continue; | |
233 | } | |
234 | if (strcmp (cp, "-s") == 0) { | |
235 | sflag++; | |
236 | continue; | |
237 | } | |
238 | ||
239 | if (sysin) { | |
240 | usage: ; | |
241 | fprintf (stderr, | |
242 | "usage: posy [-a] [-d] [-f] [-Hh] [-o newmodule.py] [-s] module.py\n"); | |
243 | exit (1); | |
244 | } | |
245 | ||
246 | if (*cp == '-') { | |
247 | if (*++cp != NULL) | |
248 | goto usage; | |
249 | sysin = ""; | |
250 | } | |
251 | sysin = cp; | |
252 | } | |
253 | ||
254 | switch (pepydebug = (cp = getenv ("POSYTEST")) && *cp ? atoi (cp) : 0) { | |
255 | case 2: | |
256 | yydebug++; /* fall */ | |
257 | case 1: | |
258 | sflag++; /* .. */ | |
259 | case 0: | |
260 | break; | |
261 | } | |
262 | ||
263 | if (sysin == NULLCP) | |
264 | sysin = ""; | |
265 | ||
266 | if (*sysin && freopen (sysin, "r", stdin) == NULL) { | |
267 | fprintf (stderr, "unable to read "), perror (sysin); | |
268 | exit (1); | |
269 | } | |
270 | ||
271 | if (strcmp (sysout, "-") == 0) | |
272 | sysout[0] = NULL; | |
273 | if (*sysout && freopen (sysout, "w", stdout) == NULL) { | |
274 | fprintf (stderr, "unable to write "), perror (sysout); | |
275 | exit (1); | |
276 | } | |
277 | ||
278 | if (cp = index (dp, ')')) { | |
279 | for (cp++; *cp != ' '; cp++) | |
280 | if (*cp == NULL) | |
281 | break; | |
282 | if (*cp == NULL) | |
283 | cp = NULL; | |
284 | } | |
285 | if (cp == NULL) | |
286 | cp = dp + strlen (dp); | |
287 | (void) sprintf (autogen, "posy %*.*s", cp - dp, cp - dp, dp); | |
288 | printf ("-- automatically generated by %s, do not edit!\n\n", autogen); | |
289 | ||
290 | initoidtbl (); | |
291 | ||
292 | exit (yyparse ()); /* NOTREACHED */ | |
293 | } | |
294 | ||
295 | /* \f ERRORS */ | |
296 | ||
297 | yyerror (s) | |
298 | register char *s; | |
299 | { | |
300 | yyerror_aux (s); | |
301 | ||
302 | if (*sysout) | |
303 | (void) unlink (sysout); | |
304 | if (*sysdef) | |
305 | (void) unlink (sysdef); | |
306 | if (*sysact) | |
307 | (void) unlink (sysact); | |
308 | ||
309 | exit (1); | |
310 | } | |
311 | ||
312 | #ifndef lint | |
313 | warning (va_alist) | |
314 | va_dcl | |
315 | { | |
316 | char buffer[BUFSIZ]; | |
317 | char buffer2[BUFSIZ]; | |
318 | char *cp; | |
319 | va_list ap; | |
320 | ||
321 | va_start (ap); | |
322 | ||
323 | _asprintf (buffer, NULLCP, ap); | |
324 | ||
325 | va_end (ap); | |
326 | ||
327 | (void) sprintf (buffer2, "Warning: %s", buffer); | |
328 | yyerror_aux (buffer2); | |
329 | } | |
330 | ||
331 | #else | |
332 | ||
333 | /* VARARGS1 */ | |
334 | warning (fmt) | |
335 | char *fmt; | |
336 | { | |
337 | warning (fmt); | |
338 | } | |
339 | #endif | |
340 | ||
341 | static yyerror_aux (s) | |
342 | register char *s; | |
343 | { | |
344 | if (linepos) | |
345 | fprintf (stderr, "\n"), linepos = 0; | |
346 | ||
347 | if (eval) | |
348 | fprintf (stderr, "type %s: ", eval); | |
349 | else | |
350 | fprintf (stderr, "line %d: ", yylineno); | |
351 | fprintf (stderr, "%s\n", s); | |
352 | if (!eval) | |
353 | fprintf (stderr, "last token read was \"%s\"\n", yytext); | |
354 | } | |
355 | ||
356 | /* \f */ | |
357 | ||
358 | #ifndef lint | |
359 | myyerror (va_alist) | |
360 | va_dcl | |
361 | { | |
362 | char buffer[BUFSIZ]; | |
363 | va_list ap; | |
364 | ||
365 | va_start (ap); | |
366 | ||
367 | _asprintf (buffer, NULLCP, ap); | |
368 | ||
369 | va_end (ap); | |
370 | ||
371 | yyerror (buffer); | |
372 | } | |
373 | #else | |
374 | /* VARARGS */ | |
375 | ||
376 | myyerror (fmt) | |
377 | char *fmt; | |
378 | { | |
379 | myyerror (fmt); | |
380 | } | |
381 | #endif | |
382 | ||
383 | ||
384 | #ifndef lint | |
385 | static pyyerror (va_alist) | |
386 | va_dcl | |
387 | { | |
388 | char buffer[BUFSIZ]; | |
389 | register YP yp; | |
390 | va_list ap; | |
391 | ||
392 | va_start (ap); | |
393 | ||
394 | yp = va_arg (ap, YP); | |
395 | ||
396 | _asprintf (buffer, NULLCP, ap); | |
397 | ||
398 | va_end (ap); | |
399 | ||
400 | yyerror_aux (buffer); | |
401 | print_type (yp, 0); | |
402 | ||
403 | ||
404 | if (*sysout) | |
405 | (void) unlink (sysout); | |
406 | if (*sysdef) | |
407 | (void) unlink (sysdef); | |
408 | if (*sysact) | |
409 | (void) unlink (sysact); | |
410 | ||
411 | exit (1); | |
412 | } | |
413 | #else | |
414 | /* VARARGS */ | |
415 | ||
416 | static pyyerror (yp, fmt) | |
417 | YP yp; | |
418 | char *fmt; | |
419 | { | |
420 | pyyerror (yp, fmt); | |
421 | } | |
422 | #endif | |
423 | ||
424 | /* \f */ | |
425 | ||
426 | yywrap () { | |
427 | if (linepos) | |
428 | fprintf (stderr, "\n"), linepos = 0; | |
429 | ||
430 | return 1; | |
431 | } | |
432 | ||
433 | /* \f */ | |
434 | ||
435 | /* ARGSUSED */ | |
436 | ||
437 | yyprint (s, f, top) | |
438 | char *s; | |
439 | int f; | |
440 | int top; | |
441 | { | |
442 | int len; | |
443 | static int nameoutput = 0; | |
444 | static int outputlinelen = 79; | |
445 | ||
446 | if (sflag || !s) | |
447 | return; | |
448 | ||
449 | if (!nameoutput) { | |
450 | if (linepos) | |
451 | fprintf (stderr, "\n\n"); | |
452 | ||
453 | fprintf (stderr, "%s", mymodule); | |
454 | nameoutput = (linepos = strlen (mymodule)) + 1; | |
455 | ||
456 | fprintf (stderr, " types:"); | |
457 | linepos += 7; | |
458 | ||
459 | if (top) | |
460 | return; | |
461 | } | |
462 | ||
463 | len = strlen (s); | |
464 | if (linepos != nameoutput) | |
465 | if (len + linepos + 1 > outputlinelen) | |
466 | fprintf (stderr, "\n%*s", linepos = nameoutput, ""); | |
467 | else | |
468 | fprintf (stderr, " "), linepos++; | |
469 | fprintf (stderr, "%s", s); | |
470 | linepos += len; | |
471 | } | |
472 | ||
473 | /* \f PASS1 */ | |
474 | ||
475 | pass1 () | |
476 | { | |
477 | printf ("%s ", mymodule); | |
478 | if (mymoduleid) { | |
479 | printf ("%s ", oidprint(mymoduleid)); | |
480 | } | |
481 | printf ("DEFINITIONS ::=\n\n"); | |
482 | } | |
483 | ||
484 | /* \f */ | |
485 | ||
486 | pass1_type (encpref, decpref, prfpref, mod, id, yp) | |
487 | register char *encpref, | |
488 | *decpref, | |
489 | *prfpref, | |
490 | *mod, | |
491 | *id; | |
492 | register YP yp; | |
493 | { | |
494 | register SY sy; | |
495 | ||
496 | if (lookup_type (mod, id)) /* no duplicate entries, please... */ | |
497 | return; | |
498 | ||
499 | if (pepydebug) { | |
500 | if (linepos) | |
501 | fprintf (stderr, "\n"), linepos = 0; | |
502 | ||
503 | fprintf (stderr, "%s.%s\n", mod ? mod : mymodule, id); | |
504 | print_type (yp, 0); | |
505 | fprintf (stderr, "--------\n"); | |
506 | } | |
507 | else | |
508 | if (!(yp -> yp_flags & YP_IMPORTED)) | |
509 | yyprint (id, 0, 0); | |
510 | ||
511 | sy = new_symbol (encpref, decpref, prfpref, mod, id, yp); | |
512 | mysymbols = add_symbol (mysymbols, sy); | |
513 | } | |
514 | ||
515 | /* \f PASS2 */ | |
516 | ||
517 | pass2 () { | |
518 | int first; | |
519 | register SY sy; | |
520 | register YP yp, | |
521 | y; | |
522 | ||
523 | if (!sflag) | |
524 | (void) fflush (stderr); | |
525 | ||
526 | modsym_aux (mymodule, modulename); | |
527 | ||
528 | (void) sprintf (sysdef, "%s-types.h", mymodule); | |
529 | if ((fdef = fopen (sysdef, "w")) == NULL) | |
530 | myyerror ("unable to write %s", sysdef); | |
531 | fprintf (fdef, "/* automatically generated by %s, do not edit! */\n\n", | |
532 | autogen); | |
533 | fprintf (fdef, "#ifndef\t_module_%s_defined_\n", modulename); | |
534 | fprintf (fdef, "#define\t_module_%s_defined_\n\n", modulename); | |
535 | ||
536 | fprintf (fdef, "#ifndef PEPYPATH\n"); | |
537 | fprintf (fdef, "#include <isode/psap.h>\n"); | |
538 | if (strcmp (mymodule, "UNIV")) | |
539 | fprintf (fdef, "#include <isode/pepy/UNIV-types.h>\n"); | |
540 | fprintf (fdef, "#else\n"); | |
541 | fprintf (fdef, "#include \"psap.h\"\n"); | |
542 | if (strcmp (mymodule, "UNIV")) | |
543 | fprintf (fdef, "#include \"../pepy/UNIV-types.h\"\n"); | |
544 | fprintf (fdef, "#endif\n"); | |
545 | ||
546 | fprintf (fdef, "\n\n"); | |
547 | ||
548 | if (fflag) { | |
549 | (void) sprintf (sysact, "%s-types.tmp", mymodule); | |
550 | if ((fact = fopen (sysact, "w+")) == NULL) | |
551 | myyerror ("unable to write %s", sysact); | |
552 | } | |
553 | ||
554 | for (sy = mysymbols; sy; sy = sy -> sy_next) { | |
555 | eval = sy -> sy_name; | |
556 | yp = sy -> sy_type; | |
557 | if (sy -> sy_module == NULLCP) | |
558 | yyerror ("no module name associated with symbol"); | |
559 | if (yp -> yp_flags & YP_IMPORTED) | |
560 | continue; | |
561 | ||
562 | do_struct0 (yp, eval); | |
563 | if (ferror (stdout) || ferror (fdef) || (fflag && ferror (fact))) | |
564 | myyerror ("write error - %s", sys_errname (errno)); | |
565 | } | |
566 | ||
567 | for (sy = mysymbols; sy; sy = sy -> sy_next) { | |
568 | eval = sy -> sy_name; | |
569 | yp = sy -> sy_type; | |
570 | if (yp -> yp_flags & YP_IMPORTED) | |
571 | continue; | |
572 | ||
573 | do_struct1 (yp, eval, NULLCP); | |
574 | if (ferror (stdout) || ferror (fdef) || (fflag && ferror (fact))) | |
575 | myyerror ("write error - %s", sys_errname (errno)); | |
576 | } | |
577 | ||
578 | for (sy = mysymbols; sy; sy = sy -> sy_next) { | |
579 | eval = sy -> sy_name; | |
580 | yp = sy -> sy_type; | |
581 | if (yp -> yp_flags & YP_IMPORTED) | |
582 | continue; | |
583 | ||
584 | do_struct2 (yp, eval, NULLCP); | |
585 | if (ferror (stdout) || ferror (fdef) || (fflag && ferror (fact))) | |
586 | myyerror ("write error - %s", sys_errname (errno)); | |
587 | } | |
588 | ||
589 | if (Cflag == 0) | |
590 | printf ("%%{\n#include <stdio.h>\n#include \"%s\"\n%%}\n", sysdef); | |
591 | ||
592 | printf ("\nPREFIXES %s %s %s\n", yyencdflt, yydecdflt, yyprfdflt); | |
593 | ||
594 | printf ("\nBEGIN\n"); | |
595 | ||
596 | print_expimp (); | |
597 | ||
598 | first = 1; | |
599 | for (sy = mysymbols; sy; sy = sy -> sy_next) { | |
600 | eval = sy -> sy_name; | |
601 | yp = sy -> sy_type; | |
602 | if (sy -> sy_module == NULLCP) | |
603 | yyerror ("no module name associated with symbol"); | |
604 | if (yp -> yp_flags & YP_IMPORTED) | |
605 | continue; | |
606 | ||
607 | if (first) { | |
608 | printf ("\nENCODER %s\n", yyencpref); | |
609 | first = 0; | |
610 | } | |
611 | ||
612 | printf ("\n%s", sy -> sy_name); | |
613 | printf (" [[P struct %s *]]", | |
614 | modsym (mymodule, sy -> sy_name, "type")); | |
615 | switch (yp -> yp_code) { | |
616 | case YP_SEQTYPE: | |
617 | case YP_SEQLIST: | |
618 | case YP_SETTYPE: | |
619 | case YP_SETLIST: | |
620 | case YP_CHOICE: | |
621 | if (yp -> yp_declexp || yp -> yp_type) | |
622 | do_type0 (yp, YP_ENCODER); | |
623 | break; | |
624 | ||
625 | case YP_BIT: | |
626 | case YP_BITLIST: | |
627 | do_type0 (yp, YP_ENCODER); | |
628 | break; | |
629 | ||
630 | default: | |
631 | if (!yp -> yp_declexp) | |
632 | break; | |
633 | do_type0 (yp, YP_ENCODER); | |
634 | break; | |
635 | } | |
636 | printf (" ::=\n"); | |
637 | do_type1 (yp, 1, (yp -> yp_flags & YP_TAG) ? 1 : 2, eval, "parm", | |
638 | NULLCP, YP_ENCODER); | |
639 | printf ("\n"); | |
640 | if (ferror (stdout) || ferror (fdef) || (fflag && ferror (fact))) | |
641 | myyerror ("write error - %s", sys_errname (errno)); | |
642 | } | |
643 | ||
644 | first = 1; | |
645 | for (sy = mysymbols; sy; sy = sy -> sy_next) { | |
646 | eval = sy -> sy_name; | |
647 | yp = sy -> sy_type; | |
648 | if (sy -> sy_module == NULLCP) | |
649 | yyerror ("no module name associated with symbol"); | |
650 | if (yp -> yp_flags & YP_IMPORTED) | |
651 | continue; | |
652 | ||
653 | if (first) { | |
654 | printf ("\nDECODER %s\n", yydecpref); | |
655 | first = 0; | |
656 | } | |
657 | ||
658 | printf ("\n%s", sy -> sy_name); | |
659 | printf (" [[P struct %s **]]", | |
660 | modsym (mymodule, sy -> sy_name, "type")); | |
661 | switch (yp -> yp_code) { | |
662 | case YP_SEQTYPE: | |
663 | case YP_SEQLIST: | |
664 | case YP_SETTYPE: | |
665 | case YP_SETLIST: | |
666 | case YP_CHOICE: | |
667 | if (yp -> yp_declexp || yp -> yp_type) | |
668 | do_type0 (yp, YP_DECODER); | |
669 | break; | |
670 | ||
671 | default: | |
672 | if (!yp -> yp_declexp) | |
673 | break; | |
674 | do_type0 (yp, YP_DECODER); | |
675 | break; | |
676 | } | |
677 | printf (" ::=\n"); | |
678 | ||
679 | y = yp; | |
680 | again: ; | |
681 | switch (y -> yp_code) { | |
682 | case YP_SEQTYPE: | |
683 | case YP_SETTYPE: | |
684 | if (h2flag) | |
685 | xalloc (y, 0, 1, "parm", | |
686 | modsym (mymodule, sy -> sy_name, "type"), 1); | |
687 | break; | |
688 | ||
689 | case YP_BIT: | |
690 | case YP_BITLIST: | |
691 | case YP_SEQ: | |
692 | case YP_SET: | |
693 | case YP_ANY: | |
694 | case YP_OCT: | |
695 | case YP_OID: | |
696 | case YP_IDEFINED: | |
697 | case YP_REAL: | |
698 | break; | |
699 | ||
700 | case YP_SEQLIST: | |
701 | case YP_SETLIST: | |
702 | case YP_CHOICE: | |
703 | if (hflag && y -> yp_type && !y -> yp_type -> yp_next) { | |
704 | y = y -> yp_type; | |
705 | goto again; | |
706 | } | |
707 | /* else fall */ | |
708 | ||
709 | default: | |
710 | xalloc (y, 1, 1, "parm", | |
711 | modsym (mymodule, sy -> sy_name, "type"), 1); | |
712 | break; | |
713 | } | |
714 | do_type1 (yp, 1, (yp -> yp_flags & YP_TAG) ? 1 : 2, eval, "(*parm)", | |
715 | NULLCP, YP_DECODER); | |
716 | printf ("\n"); | |
717 | if (ferror (stdout) || ferror (fdef) || (fflag && ferror (fact))) | |
718 | myyerror ("write error - %s", sys_errname (errno)); | |
719 | } | |
720 | ||
721 | printf ("\nEND\n"); | |
722 | ||
723 | if (fflag) { | |
724 | register int c; | |
725 | ||
726 | (void) fflush (fact); | |
727 | (void) fseek (fact, 0L, 0); | |
728 | ||
729 | printf ("\n%%{\n"); | |
730 | while ((c = getc (fact)) != EOF) | |
731 | putchar (c); | |
732 | printf ("\n%%}\n"); | |
733 | ||
734 | (void) fclose (fact); | |
735 | if (*sysact) | |
736 | (void) unlink (sysact); | |
737 | } | |
738 | ||
739 | fprintf (fdef, "#endif\n"); | |
740 | (void) fflush (fdef); | |
741 | (void) fflush (stdout); | |
742 | if (ferror (stdout) || ferror (fdef)) | |
743 | myyerror ("write error - %s", sys_errname (errno)); | |
744 | (void) fclose (fdef); | |
745 | } | |
746 | ||
747 | /* \f */ | |
748 | ||
749 | /* ARGSUSED */ | |
750 | ||
751 | static do_struct0 (yp, id) | |
752 | register YP yp; | |
753 | char *id; | |
754 | { | |
755 | register YP y; | |
756 | ||
757 | switch (yp -> yp_code) { | |
758 | case YP_SEQLIST: | |
759 | case YP_SETLIST: | |
760 | components_pullup (yp); | |
761 | break; | |
762 | ||
763 | default: | |
764 | break; | |
765 | } | |
766 | ||
767 | switch (yp -> yp_code) { | |
768 | case YP_SEQTYPE: | |
769 | case YP_SETTYPE: | |
770 | do_struct0 (yp -> yp_type, id); | |
771 | break; | |
772 | ||
773 | case YP_CHOICE: | |
774 | case YP_SETLIST: | |
775 | choice_pullup (yp, yp -> yp_code == YP_CHOICE ? CH_FULLY | |
776 | : CH_PARTIAL); | |
777 | /* and fall */ | |
778 | case YP_SEQLIST: | |
779 | for (y = yp -> yp_type; y; y = y -> yp_next) | |
780 | do_struct0 (y, id); | |
781 | break; | |
782 | ||
783 | case YP_IDEFINED: | |
784 | if (yp -> yp_module | |
785 | && strcmp (yp -> yp_module, mymodule) | |
786 | && !lookup_module (yp -> yp_module)) | |
787 | fprintf (fdef, "#include \"%s-types.h\"\n", yp -> yp_module); | |
788 | break; | |
789 | ||
790 | default: | |
791 | break; | |
792 | } | |
793 | } | |
794 | ||
795 | /* \f */ | |
796 | ||
797 | static do_struct1 (yp, id, pullup) | |
798 | register YP yp; | |
799 | char *id, | |
800 | *pullup; | |
801 | { | |
802 | register int i, | |
803 | j; | |
804 | char buf1[BUFSIZ]; | |
805 | register YP y; | |
806 | register YV yv; | |
807 | ||
808 | switch (yp -> yp_code) { | |
809 | case YP_BIT: | |
810 | case YP_BITLIST: | |
811 | case YP_SEQ: | |
812 | case YP_SET: | |
813 | case YP_ANY: | |
814 | fprintf (fdef, "\n"); | |
815 | if (aflag) | |
816 | printag (yp, 4, pullup); | |
817 | fprintf (fdef, "#define\t%s\tPElement\n", | |
818 | modsym (mymodule, id, "type")); | |
819 | if (yp -> yp_code == YP_BITLIST) { | |
820 | i = -1; | |
821 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) | |
822 | if ((j = val2int (yv)) < 0) | |
823 | pyyerror (yp, "invalid bit number in BIT STRING"); | |
824 | else | |
825 | if (j > i) | |
826 | i = j; | |
827 | if (i < sizeof (int) * 8) { /* NBBY */ | |
828 | fprintf (fdef, "#define\t%s\t\"\\020", | |
829 | modsym (mymodule, eval, "bits")); | |
830 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) | |
831 | if (yv -> yv_flags & YV_NAMED) | |
832 | fprintf (fdef, "\\0%o%s", | |
833 | val2int (yv) + 1, yv -> yv_named); | |
834 | else | |
835 | fprintf (fdef, "\\0%oBIT%d", | |
836 | val2int (yv) + 1, val2int (yv)); | |
837 | fprintf (fdef, "\"\n"); | |
838 | } | |
839 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) { | |
840 | modsym_aux (yv -> yv_named, buf1); | |
841 | fprintf (fdef, "#define\t%s_%s\t%d\n", | |
842 | modsym (mymodule, eval, "bit"), | |
843 | buf1, val2int (yv)); | |
844 | } | |
845 | } | |
846 | if (fflag) | |
847 | fprintf (fdef, "#define\t%s\tpe_free\n", | |
848 | modsym (mymodule, id, "free")); | |
849 | break; | |
850 | ||
851 | case YP_OCT: | |
852 | fprintf (fdef, "\n"); | |
853 | if (aflag) | |
854 | printag (yp, 4, pullup); | |
855 | fprintf (fdef, "#define\t%s\tqbuf\n", | |
856 | modsym (mymodule, id, "type")); | |
857 | if (fflag) | |
858 | fprintf (fdef, "#define\t%s\tqb_free\n", | |
859 | modsym (mymodule, id, "free")); | |
860 | break; | |
861 | ||
862 | case YP_OID: | |
863 | fprintf (fdef, "\n"); | |
864 | if (aflag) | |
865 | printag (yp, 4, pullup); | |
866 | fprintf (fdef, "#define\t%s\tOIDentifier\n", | |
867 | modsym (mymodule, id, "type")); | |
868 | if (fflag) | |
869 | fprintf (fdef, "#define\t%s\toid_free\n", | |
870 | modsym (mymodule, id, "free")); | |
871 | break; | |
872 | ||
873 | case YP_IDEFINED: | |
874 | fprintf (fdef, "\n"); | |
875 | if (aflag) | |
876 | printag (yp, 4, pullup); | |
877 | fprintf (fdef, "#define\t%s\t", | |
878 | modsym (mymodule, id, "type")); | |
879 | fprintf (fdef, "%s\n", | |
880 | modsym (yp -> yp_module, yp -> yp_identifier, "type")); | |
881 | if (fflag) { | |
882 | fprintf (fdef, "#define\t%s\t", | |
883 | modsym (mymodule, id, "free")); | |
884 | fprintf (fdef, "%s\n", | |
885 | modsym (yp -> yp_module, yp -> yp_identifier, "free")); | |
886 | } | |
887 | break; | |
888 | ||
889 | case YP_SEQLIST: | |
890 | case YP_SETLIST: | |
891 | case YP_CHOICE: | |
892 | if (hflag && (y = yp -> yp_type) && !y -> yp_next) { | |
893 | do_struct1 (y, id, tags[yp -> yp_code]); | |
894 | break; | |
895 | } | |
896 | /* else fall */ | |
897 | ||
898 | default: | |
899 | break; | |
900 | } | |
901 | } | |
902 | ||
903 | /* \f */ | |
904 | ||
905 | static do_struct2 (yp, id, pullup) | |
906 | register YP yp; | |
907 | char *id, | |
908 | *pullup; | |
909 | { | |
910 | register YP y; | |
911 | int flg = (yp -> yp_code == YP_SEQTYPE || yp -> yp_code == YP_SETTYPE); | |
912 | ||
913 | switch (yp -> yp_code) { | |
914 | case YP_BIT: | |
915 | case YP_BITLIST: | |
916 | case YP_SEQ: | |
917 | case YP_SET: | |
918 | case YP_ANY: | |
919 | case YP_OCT: | |
920 | case YP_OID: | |
921 | case YP_IDEFINED: | |
922 | break; | |
923 | ||
924 | case YP_SEQLIST: | |
925 | case YP_SETLIST: | |
926 | case YP_CHOICE: | |
927 | if (hflag && (y = yp -> yp_type) && !y -> yp_next) { | |
928 | do_struct2 (y, id, tags[yp -> yp_code]); | |
929 | break; | |
930 | } | |
931 | /* else fall */ | |
932 | ||
933 | default: | |
934 | fprintf (fdef, "\n"); | |
935 | if (aflag) | |
936 | printag (yp, 4, pullup); | |
937 | fprintf (fdef, "struct %s {\n", modsym (mymodule, id, "type")); | |
938 | if (fflag) { | |
939 | fprintf (fact, "\n%s (arg)\n", modsym (mymodule, id, "free")); | |
940 | fprintf (fact, "struct %s *arg;\n{\n", | |
941 | modsym (mymodule, id, "type")); | |
942 | fprintf (fact, " struct %s *parm = arg;\n", | |
943 | modsym (mymodule, id, "type")); | |
944 | if (h2flag && flg) | |
945 | fprintf (fact, " int\tn_parm;\n"); | |
946 | fprintf (fact, "\n if (parm == NULL)\n\treturn;\n\n"); | |
947 | } | |
948 | posy (yp, 1, 1, "parm", id, "parm", flg && h2flag); | |
949 | fprintf (fdef, "};\n"); | |
950 | fprintf (fdef, "int\t%s ();\n", modsym (mymodule, id, "free")); | |
951 | if (fflag) { | |
952 | if (yp -> yp_code != YP_SEQTYPE && | |
953 | yp -> yp_code != YP_SETTYPE) | |
954 | fprintf (fact, "\n free ((char *) arg);"); | |
955 | fprintf (fact, "\n}\n"); | |
956 | } | |
957 | ||
958 | break; | |
959 | } | |
960 | } | |
961 | ||
962 | /* \f */ | |
963 | ||
964 | static int type0_brackets; | |
965 | static int type0_bit; | |
966 | ||
967 | static do_type0 (yp, direction) | |
968 | register YP yp; | |
969 | int direction; | |
970 | { | |
971 | type0_brackets = type0_bit = 0; | |
972 | do_type0_aux (yp, direction); | |
973 | if (type0_brackets) | |
974 | printf (" %%}\n "); | |
975 | } | |
976 | ||
977 | static do_type0_aux (yp, direction) | |
978 | register YP yp; | |
979 | int direction; | |
980 | { | |
981 | register YP y; | |
982 | ||
983 | if (yp -> yp_declexp) { | |
984 | if (type0_brackets++ == 0) | |
985 | printf ("\n %%{\n"); | |
986 | printf ("\tstruct %s *%s%s;\n", yp -> yp_declexp, | |
987 | direction == YP_DECODER ? "*" : "", yp -> yp_declexp); | |
988 | } | |
989 | ||
990 | switch (yp -> yp_code) { | |
991 | case YP_SEQTYPE: | |
992 | case YP_SETTYPE: | |
993 | if (h2flag) { | |
994 | if (type0_brackets++ == 0) | |
995 | printf ("\n %%{\n"); | |
996 | printf ("\tint n_%s = 0;\n", yp -> yp_declexp ? | |
997 | yp -> yp_declexp : "parm"); | |
998 | } | |
999 | do_type0_aux (yp -> yp_type, direction); | |
1000 | break; | |
1001 | ||
1002 | case YP_SEQLIST: | |
1003 | case YP_SETLIST: | |
1004 | case YP_CHOICE: | |
1005 | for (y = yp -> yp_type; y; y = y -> yp_next) | |
1006 | do_type0_aux (y, direction); | |
1007 | break; | |
1008 | ||
1009 | case YP_BIT: | |
1010 | case YP_BITLIST: | |
1011 | if (direction == YP_ENCODER) { | |
1012 | if (type0_brackets++ == 0) | |
1013 | printf ("\n %%{\n"); | |
1014 | if (type0_bit++ == 0) | |
1015 | printf ("\tchar *bit_parm;\n"); | |
1016 | } | |
1017 | break; | |
1018 | ||
1019 | default: | |
1020 | break; | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | /* \f */ | |
1025 | ||
1026 | /* ARGSUSED */ | |
1027 | ||
1028 | static do_type1 (yp, top, level, id, var, action2, direction) | |
1029 | register YP yp; | |
1030 | int top, | |
1031 | level; | |
1032 | char *id, | |
1033 | *var, | |
1034 | *action2; | |
1035 | int direction; | |
1036 | { | |
1037 | int i; | |
1038 | char *cp, | |
1039 | *ep, | |
1040 | buffer[BUFSIZ], | |
1041 | varbuf[BUFSIZ]; | |
1042 | register YP y; | |
1043 | register YV yv; | |
1044 | register YT yt; | |
1045 | ||
1046 | printf ("%*s", level * 4, ""); | |
1047 | ||
1048 | if (yp -> yp_flags & YP_ID) { | |
1049 | printf ("%s", yp -> yp_id); | |
1050 | if (!(yp -> yp_flags & YP_TAG)) | |
1051 | printf ("\n%*s", ++level * 4, ""); | |
1052 | } | |
1053 | ||
1054 | if (yp -> yp_flags & YP_TAG) { | |
1055 | yt = yp -> yp_tag; | |
1056 | printf ("[%s%d]\n", classes[yt -> yt_class], val2int (yt -> yt_value)); | |
1057 | level++; | |
1058 | printf ("%*s", level * 4, ""); | |
1059 | } | |
1060 | if (yp -> yp_flags & YP_OPTIONAL && yp -> yp_varexp) { | |
1061 | if ((ep = index (yp -> yp_varexp, ' ')) == NULL) | |
1062 | yyerror ("Bug in varexp!"); | |
1063 | ||
1064 | (void) sprintf (varbuf, "%*.*s", ep - yp -> yp_varexp, | |
1065 | ep - yp -> yp_varexp, yp -> yp_varexp); | |
1066 | } | |
1067 | ||
1068 | switch (yp -> yp_code) { | |
1069 | case YP_BOOL: | |
1070 | if ((yp -> yp_flags & (YP_OPTIONAL|YP_DEFAULT)) && | |
1071 | direction == YP_DECODER) { | |
1072 | if (!top && !(yp -> yp_flags & (YP_ID | YP_TAG))) | |
1073 | printf ("dummy-for-default\n%*s", ++level * 4, ""); | |
1074 | if (yp -> yp_flags & YP_OPTIONAL) | |
1075 | printf ("%%{ %s -> optionals |= %s; %%}\n%*s", | |
1076 | varbuf, yp -> yp_optcontrol, level * 4, ""); | |
1077 | else | |
1078 | printf ("%%{ %s%s = %d; %%}\n%*s", | |
1079 | var, SVAL (yp -> yp_varexp), | |
1080 | val2int (yp -> yp_default) ? 1 : 0, level * 4, ""); | |
1081 | } | |
1082 | break; | |
1083 | ||
1084 | case YP_INT: | |
1085 | if ((yp -> yp_flags & (YP_OPTIONAL|YP_DEFAULT)) && | |
1086 | direction == YP_DECODER) { | |
1087 | if (!top && !(yp -> yp_flags & (YP_ID | YP_TAG))) | |
1088 | printf ("dummy-for-default\n%*s", ++level * 4, ""); | |
1089 | if (yp -> yp_flags & YP_OPTIONAL) | |
1090 | printf ("%%{ %s -> optionals |= %s; %%}\n%*s", | |
1091 | varbuf, yp -> yp_optcontrol, level * 4, ""); | |
1092 | else | |
1093 | printf ("%%{ %s%s = %d; %%}\n%*s", | |
1094 | var, SVAL (yp -> yp_varexp), | |
1095 | val2int (yp -> yp_default), level * 4, ""); | |
1096 | } | |
1097 | break; | |
1098 | ||
1099 | case YP_INTLIST: | |
1100 | case YP_ENUMLIST: | |
1101 | if ((yp -> yp_flags & (YP_OPTIONAL|YP_DEFAULT)) && | |
1102 | direction == YP_DECODER) { | |
1103 | if (!top && !(yp -> yp_flags & (YP_ID | YP_TAG))) | |
1104 | printf ("dummy-for-default\n%*s", ++level * 4, ""); | |
1105 | if (yp -> yp_flags & YP_OPTIONAL) | |
1106 | printf ("%%{ %s -> optionals |= %s; %%}\n%*s", | |
1107 | varbuf, yp -> yp_optcontrol, level * 4, ""); | |
1108 | else | |
1109 | printf ("%%{ %s%s = %d; %%}\n%*s", | |
1110 | var, SVAL (yp -> yp_varexp), dfl2int (yp), | |
1111 | level * 4, ""); | |
1112 | } | |
1113 | break; | |
1114 | ||
1115 | case YP_REAL: | |
1116 | if ((yp -> yp_flags & (YP_OPTIONAL|YP_DEFAULT)) && | |
1117 | direction == YP_DECODER) { | |
1118 | if (!top && !(yp -> yp_flags & (YP_ID | YP_TAG))) | |
1119 | printf ("dummy-for-default\n%*s", ++level * 4, ""); | |
1120 | if (yp -> yp_flags & YP_OPTIONAL) | |
1121 | printf ("%%{ %s -> optionals |= %s; %%}\n%*s", | |
1122 | varbuf, yp -> yp_optcontrol, level * 4, ""); | |
1123 | else | |
1124 | printf ("%%{ %s%s = %g; %%}\n%*s", | |
1125 | var, SVAL (yp -> yp_varexp), | |
1126 | val2real (yp -> yp_default), level * 4, ""); | |
1127 | } | |
1128 | break; | |
1129 | ||
1130 | case YP_NULL: | |
1131 | if ((yp -> yp_flags & YP_OPTIONAL) && direction == YP_DECODER) { | |
1132 | if (!top && !(yp -> yp_flags & (YP_ID | YP_TAG))) | |
1133 | printf ("dummy-for-default\n%*s", ++level * 4, ""); | |
1134 | printf ("%%{ %s -> optionals |= %s; %%}\n%*s", | |
1135 | varbuf, yp -> yp_optcontrol, level * 4, ""); | |
1136 | } | |
1137 | break; | |
1138 | } | |
1139 | ||
1140 | if ((yp -> yp_flags & (YP_TAG | YP_IMPLICIT)) == (YP_TAG | YP_IMPLICIT)) | |
1141 | printf ("IMPLICIT "); | |
1142 | if (yp -> yp_flags & YP_BOUND) | |
1143 | printf ("%s < ", yp -> yp_bound); | |
1144 | if (yp -> yp_flags & YP_COMPONENTS) | |
1145 | printf ("COMPONENTS OF "); | |
1146 | if (yp -> yp_flags & YP_ENCRYPTED) | |
1147 | printf ("ENCRYPTED "); | |
1148 | ||
1149 | switch (yp -> yp_code) { | |
1150 | case YP_BOOL: | |
1151 | printf ("BOOLEAN"); | |
1152 | switch (direction) { | |
1153 | case YP_ENCODER: | |
1154 | case YP_DECODER: | |
1155 | printf (top ? "\n%*s[[b %s -> %s ]]" : "\n%*s[[b %s%s ]]", | |
1156 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1157 | break; | |
1158 | } | |
1159 | break; | |
1160 | ||
1161 | case YP_INT: | |
1162 | printf ("INTEGER"); | |
1163 | switch (direction) { | |
1164 | case YP_ENCODER: | |
1165 | case YP_DECODER: | |
1166 | printf (top ? "\n%*s[[i %s -> %s ]]" : "\n%*s[[i %s%s ]]", | |
1167 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1168 | break; | |
1169 | } | |
1170 | break; | |
1171 | ||
1172 | case YP_INTLIST: | |
1173 | case YP_ENUMLIST: | |
1174 | if (yp -> yp_code == YP_ENUMLIST) | |
1175 | printf ("ENUMERATED"); | |
1176 | else | |
1177 | printf ("INTEGER"); | |
1178 | switch (direction) { | |
1179 | case YP_ENCODER: | |
1180 | case YP_DECODER: | |
1181 | printf (top ? "\n%*s[[i %s -> %s ]]\n%*s{\n" | |
1182 | : "\n%*s[[i %s%s ]]\n%*s{\n", | |
1183 | level * 4, "", var, SVAL (yp -> yp_varexp), | |
1184 | level * 4, ""); | |
1185 | break; | |
1186 | ||
1187 | default: | |
1188 | printf (" {\n"); | |
1189 | break; | |
1190 | } | |
1191 | level++; | |
1192 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) | |
1193 | printf ("%*s%s(%d)%s\n", level * 4, "", yv -> yv_named, | |
1194 | val2int (yv), yv -> yv_next ? "," : ""); | |
1195 | level--; | |
1196 | printf ("%*s}", level * 4, ""); | |
1197 | break; | |
1198 | ||
1199 | case YP_BIT: | |
1200 | printf ("BIT STRING"); | |
1201 | switch (direction) { | |
1202 | case YP_ENCODER: | |
1203 | printf ("\n%*s[[x bit_parm = bitstr2strb (%s%s, &len) $ len]]", | |
1204 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1205 | printf ("\n%*s%%{\n%*sfree (bit_parm);\n", level * 4, "", | |
1206 | (level + 1) * 4, ""); | |
1207 | if (action2) | |
1208 | printf ("%*s%s\n", (level + 1) * 4, "", action2); | |
1209 | printf ("%*s%%}\n", level * 4, ""); | |
1210 | break; | |
1211 | ||
1212 | case YP_DECODER: | |
1213 | balloc (yp, var, action2, level); | |
1214 | break; | |
1215 | } | |
1216 | break; | |
1217 | ||
1218 | case YP_BITLIST: | |
1219 | printf ("BIT STRING"); | |
1220 | switch (direction) { | |
1221 | case YP_ENCODER: | |
1222 | printf ("\n%*s[[x bit_parm = bitstr2strb (%s%s, &len) $ len]]\n%*s{\n", | |
1223 | level * 4, "", var, SVAL (yp -> yp_varexp), | |
1224 | level * 4, ""); | |
1225 | break; | |
1226 | ||
1227 | case YP_DECODER: | |
1228 | default: | |
1229 | printf (" {\n"); | |
1230 | break; | |
1231 | } | |
1232 | level++; | |
1233 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) | |
1234 | printf ("%*s%s(%d)%s\n", level * 4, "", yv -> yv_named, | |
1235 | val2int (yv), yv -> yv_next ? "," : ""); | |
1236 | level--; | |
1237 | printf ("%*s}", level * 4, ""); | |
1238 | switch (direction) { | |
1239 | case YP_DECODER: | |
1240 | balloc (yp, var, action2, level); | |
1241 | break; | |
1242 | ||
1243 | case YP_ENCODER: | |
1244 | printf ("\n%*s%%{\n%*sfree (bit_parm);\n", level * 4, "", | |
1245 | (level + 1) * 4, ""); | |
1246 | if (action2) | |
1247 | printf ("%*s%s\n", (level + 1) * 4, "", action2); | |
1248 | printf ("%*s%%}\n", level * 4, ""); | |
1249 | break; | |
1250 | } | |
1251 | break; | |
1252 | ||
1253 | case YP_OCT: | |
1254 | printf ("OCTET STRING"); | |
1255 | switch (direction) { | |
1256 | case YP_ENCODER: | |
1257 | printf ("\n%*s[[q %s%s ]]", level * 4, "", | |
1258 | var, SVAL (yp -> yp_varexp)); | |
1259 | break; | |
1260 | ||
1261 | case YP_DECODER: | |
1262 | printf ("\n%*s[[q %s%s ]]", level * 4, "", | |
1263 | var, SVAL (yp -> yp_varexp)); | |
1264 | break; | |
1265 | } | |
1266 | break; | |
1267 | ||
1268 | case YP_REAL: | |
1269 | printf ("REAL"); | |
1270 | printf (top ? "\n%*s[[r %s -> %s ]]" : "\n%*s[[r %s%s ]]", | |
1271 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1272 | break; | |
1273 | ||
1274 | case YP_NULL: | |
1275 | printf ("NULL"); | |
1276 | break; | |
1277 | ||
1278 | case YP_SEQ: | |
1279 | case YP_SET: | |
1280 | case YP_ANY: | |
1281 | printf ("%s", tags[yp -> yp_code]); | |
1282 | switch (direction) { | |
1283 | case YP_ENCODER: | |
1284 | case YP_DECODER: | |
1285 | printf ("\n%*s[[a %s%s ]]", | |
1286 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1287 | break; | |
1288 | } | |
1289 | break; | |
1290 | ||
1291 | case YP_SEQTYPE: | |
1292 | case YP_SETTYPE: | |
1293 | ep = yp -> yp_code != YP_SETTYPE ? "element" : "member"; | |
1294 | printf ("%s\n", tags [yp -> yp_code]); | |
1295 | switch (direction) { | |
1296 | case YP_ENCODER: | |
1297 | if ((y = yp -> yp_type) -> yp_declexp) { | |
1298 | printf ("%*s%%{ %s = %s; %%}\n", | |
1299 | (level + 1) * 4, "", y -> yp_declexp, | |
1300 | SVAL (y -> yp_varexp)); | |
1301 | } | |
1302 | if (h2flag) { | |
1303 | if (top) { | |
1304 | printf ("%*s<<n_parm = 0; ", (level + 1) * 4, ""); | |
1305 | printf ("n_parm < parm -> nelem; n_parm++>>\n"); | |
1306 | } | |
1307 | else { | |
1308 | printf ("%*s<<n_%s = 0;\n%*sn_%s < %s -> nelem;\n", | |
1309 | (level + 1) * 4, "", yp -> yp_declexp, | |
1310 | (level + 3) * 4, "", yp -> yp_declexp, | |
1311 | yp -> yp_declexp); | |
1312 | printf ("%*sn_%s++>>\n", | |
1313 | (level + 3) * 4, "", yp -> yp_declexp); | |
1314 | } | |
1315 | } | |
1316 | else { | |
1317 | if (top) | |
1318 | printf ("%*s<<; parm; parm = parm -> next>>\n", | |
1319 | (level + 1) * 4, ""); | |
1320 | else | |
1321 | printf ("%*s<<%s = %s%s;\n%*s%s;\n%*s%s = %s -> next>>\n", | |
1322 | (level + 1) * 4, "", yp -> yp_declexp, | |
1323 | var, SVAL (yp -> yp_varexp), | |
1324 | (level + 3) * 4, "", yp -> yp_declexp, | |
1325 | (level + 3) * 4, "", yp -> yp_declexp, | |
1326 | yp -> yp_declexp); | |
1327 | } | |
1328 | break; | |
1329 | ||
1330 | case YP_DECODER: | |
1331 | if (h2flag) { | |
1332 | y = yp -> yp_type; | |
1333 | xalloc (y, 0, level + 2, y -> yp_declexp, | |
1334 | y -> yp_declexp, 1); | |
1335 | } | |
1336 | else | |
1337 | xalloc (yp, 0, level + 1, | |
1338 | top ? "parm" : yp -> yp_declexp, | |
1339 | top ? modsym (mymodule, eval, "type") | |
1340 | : yp -> yp_declexp, 1); | |
1341 | break; | |
1342 | } | |
1343 | do_type1 (yp -> yp_type, 0, level + 1, ep, "", NULLCP, direction); | |
1344 | switch (direction) { | |
1345 | case YP_DECODER: | |
1346 | printf ("\n%*s%%{ ", (level + 1) * 4, ""); | |
1347 | if (h2flag) { | |
1348 | ||
1349 | printf ("n_%s++;", top ? "parm" : yp -> yp_declexp); | |
1350 | ||
1351 | if ((yp -> yp_type) && (yp -> yp_type -> yp_declexp)) | |
1352 | printf(" %s ++;", yp -> yp_type -> yp_declexp); | |
1353 | ||
1354 | } | |
1355 | else | |
1356 | if (top) | |
1357 | printf ("parm = &((*parm) -> next);"); | |
1358 | else | |
1359 | printf ("%s = &((*%s) -> next);", | |
1360 | yp -> yp_declexp, yp -> yp_declexp); | |
1361 | if (action2) | |
1362 | printf (" %s", action2); | |
1363 | printf (" %%}"); | |
1364 | break; | |
1365 | } | |
1366 | break; | |
1367 | ||
1368 | case YP_SEQLIST: | |
1369 | case YP_SETLIST: | |
1370 | ep = yp -> yp_code != YP_SETLIST ? "element" : "member"; | |
1371 | printf ("%s", tags [yp -> yp_code]); | |
1372 | printf ("\n%*s%%{\n", (level + 1) * 4, ""); | |
1373 | if (direction == YP_DECODER) | |
1374 | xalloc (yp, 1, level + 2, yp -> yp_declexp, | |
1375 | yp -> yp_declexp, 0); | |
1376 | for (y = yp -> yp_type; y; y = y -> yp_next) { | |
1377 | if (y -> yp_declexp) | |
1378 | switch (direction) { | |
1379 | case YP_ENCODER: | |
1380 | printf ("%*s%s = %s;\n", | |
1381 | (level + 2) * 4, "", | |
1382 | y -> yp_declexp, y -> yp_varexp); | |
1383 | break; | |
1384 | ||
1385 | case YP_DECODER: | |
1386 | printf ("%*s%s = &(%s);\n", | |
1387 | (level + 2) * 4, "", | |
1388 | y -> yp_declexp, y -> yp_varexp); | |
1389 | break; | |
1390 | } | |
1391 | if (direction == YP_DECODER && | |
1392 | y -> yp_flags & YP_DEFAULT) { | |
1393 | prime_default (y, level + 2); | |
1394 | } | |
1395 | } | |
1396 | printf ("%*s%%}\n%*s{\n", (level + 1) * 4, "", | |
1397 | level * 4, ""); | |
1398 | ||
1399 | if (!hflag || !(y = yp -> yp_type) || y -> yp_next) { | |
1400 | var = ""; | |
1401 | top = 0; | |
1402 | } | |
1403 | for (y = yp -> yp_type; y; y = y -> yp_next) { | |
1404 | do_type1 (y, top, | |
1405 | level + ((y -> yp_flags & (YP_ID | YP_TAG)) ? 1 : 2), | |
1406 | ep, var, NULLCP, direction); | |
1407 | printf ("%s\n", y -> yp_next ? ",\n" : ""); | |
1408 | } | |
1409 | printf ("%*s}", level * 4, ""); | |
1410 | break; | |
1411 | ||
1412 | case YP_CHOICE: | |
1413 | printf ("CHOICE"); | |
1414 | if (!hflag || !(y = yp -> yp_type) || y -> yp_next) | |
1415 | var = ""; | |
1416 | i = 0; | |
1417 | for (y = yp -> yp_type; y; y = y -> yp_next) | |
1418 | if (y -> yp_declexp) | |
1419 | i++; | |
1420 | switch (direction) { | |
1421 | case YP_ENCODER: | |
1422 | if (i) { | |
1423 | printf ("\n%*s%%{\n", (level + 1) * 4, ""); | |
1424 | for (y = yp -> yp_type; y; y = y -> yp_next) | |
1425 | if (y -> yp_declexp) | |
1426 | printf ("%*s%s = %s;\n", (level + 2) * 4, "", | |
1427 | y -> yp_declexp, y -> yp_varexp); | |
1428 | printf ("%*s%%}\n%*s", (level + 1) * 4, "", | |
1429 | (level + 1) * 4 - 1, "" ); | |
1430 | } | |
1431 | if (*var) | |
1432 | printf (" <<1>>"); | |
1433 | else | |
1434 | if (top) | |
1435 | printf (" <<parm -> offset>>"); | |
1436 | else | |
1437 | printf (" <<%s -> offset>>", | |
1438 | yp -> yp_declexp); | |
1439 | printf (i ? "\n%*s{\n" : " {\n", level * 4, ""); | |
1440 | break; | |
1441 | ||
1442 | case YP_DECODER: | |
1443 | printf ("\n"); | |
1444 | xalloc (yp, 0, level + 1, yp -> yp_declexp, | |
1445 | yp -> yp_declexp, 1); | |
1446 | printf ("%*s{\n", level * 4, ""); | |
1447 | break; | |
1448 | ||
1449 | default: | |
1450 | printf (" {\n"); | |
1451 | break; | |
1452 | } | |
1453 | if (direction == YP_DECODER) { | |
1454 | (void) sprintf (cp = buffer, "(*(%s)) -> offset = ", | |
1455 | top ? "parm" : yp -> yp_declexp); | |
1456 | cp += strlen (cp); | |
1457 | } | |
1458 | else | |
1459 | cp = NULL; | |
1460 | if (!hflag || !(y = yp -> yp_type) || y -> yp_next) | |
1461 | top = 0; | |
1462 | else | |
1463 | if (top) | |
1464 | cp = NULL; | |
1465 | for (y = yp -> yp_type; y; y = y -> yp_next) { | |
1466 | if (cp) | |
1467 | (void) sprintf (cp, "%s;", y -> yp_offset); | |
1468 | do_type1 (y, top, level + 1, "choice", var, | |
1469 | cp ? buffer : NULLCP, direction); | |
1470 | printf ("%s\n", y -> yp_next ? ",\n" : ""); | |
1471 | } | |
1472 | printf ("%*s}", level * 4, ""); | |
1473 | break; | |
1474 | ||
1475 | case YP_OID: | |
1476 | printf ("OBJECT IDENTIFIER"); | |
1477 | switch (direction) { | |
1478 | case YP_ENCODER: | |
1479 | case YP_DECODER: | |
1480 | printf ("\n%*s[[O %s%s ]]", | |
1481 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1482 | break; | |
1483 | } | |
1484 | break; | |
1485 | ||
1486 | case YP_IDEFINED: | |
1487 | if (yp -> yp_module && strcmp (yp -> yp_module, mymodule)) | |
1488 | printf ("%s.", yp -> yp_module); | |
1489 | printf ("%s", yp -> yp_identifier); | |
1490 | switch (direction) { | |
1491 | case YP_ENCODER: | |
1492 | printf ("\n%*s[[p %s%s ]]", | |
1493 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1494 | break; | |
1495 | ||
1496 | case YP_DECODER: | |
1497 | printf ("\n%*s[[p &(%s%s)]]", | |
1498 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
1499 | break; | |
1500 | } | |
1501 | break; | |
1502 | ||
1503 | default: | |
1504 | myyerror ("unknown type: %d", yp -> yp_code); | |
1505 | } | |
1506 | ||
1507 | if (action2) | |
1508 | switch (yp -> yp_code) { | |
1509 | case YP_BIT: | |
1510 | case YP_BITLIST: | |
1511 | if (direction == YP_ENCODER) | |
1512 | break; | |
1513 | case YP_SEQTYPE: | |
1514 | case YP_SETTYPE: | |
1515 | if (direction == YP_DECODER) | |
1516 | break; | |
1517 | /* else fall */ | |
1518 | ||
1519 | default: | |
1520 | printf ("\n%*s%%{ %s %%}", level * 4, "", action2); | |
1521 | break; | |
1522 | } | |
1523 | ||
1524 | if (yp -> yp_flags & YP_OPTIONAL) { | |
1525 | printf ("\n%*sOPTIONAL", level * 4, ""); | |
1526 | ||
1527 | if (direction == YP_ENCODER) | |
1528 | switch (yp -> yp_code) { | |
1529 | case YP_BOOL: | |
1530 | case YP_INT: | |
1531 | case YP_INTLIST: | |
1532 | case YP_ENUMLIST: | |
1533 | case YP_NULL: | |
1534 | case YP_REAL: | |
1535 | printf (" <<%s -> optionals & %s >>", | |
1536 | varbuf, yp -> yp_optcontrol); | |
1537 | default: | |
1538 | break; | |
1539 | ||
1540 | case YP_BIT: | |
1541 | case YP_BITLIST: | |
1542 | case YP_OCT: | |
1543 | case YP_SEQ: | |
1544 | case YP_SEQTYPE: | |
1545 | case YP_SEQLIST: | |
1546 | case YP_SET: | |
1547 | case YP_SETTYPE: | |
1548 | case YP_SETLIST: | |
1549 | case YP_CHOICE: | |
1550 | case YP_ANY: | |
1551 | case YP_OID: | |
1552 | case YP_IDEFINED: | |
1553 | printf (" <<%s%s>>", var, SVAL (yp -> yp_varexp)); | |
1554 | break; | |
1555 | } | |
1556 | } | |
1557 | else | |
1558 | if (yp -> yp_flags & YP_DEFAULT) { | |
1559 | printf ("\n%*sDEFAULT ", level * 4, ""); | |
1560 | val2prf (yp -> yp_default, level + 2); | |
1561 | ||
1562 | if (direction == YP_ENCODER) | |
1563 | switch (yp -> yp_code) { | |
1564 | case YP_BOOL: | |
1565 | printf (" <<%s%s%s>>", | |
1566 | val2int (yp -> yp_default) ? "!" : "", | |
1567 | var, SVAL (yp -> yp_varexp)); | |
1568 | break; | |
1569 | ||
1570 | case YP_INT: | |
1571 | case YP_INTLIST: | |
1572 | case YP_ENUMLIST: | |
1573 | printf (" <<%s%s != %d>>", var, SVAL (yp -> yp_varexp), | |
1574 | dfl2int (yp)); | |
1575 | break; | |
1576 | ||
1577 | case YP_REAL: | |
1578 | printf (" << %s%s != %g >>", | |
1579 | var, SVAL (yp -> yp_varexp), | |
1580 | val2real (yp -> yp_default)); | |
1581 | break; | |
1582 | ||
1583 | case YP_NULL: | |
1584 | default: | |
1585 | break; | |
1586 | ||
1587 | case YP_BIT: | |
1588 | case YP_BITLIST: | |
1589 | case YP_OCT: | |
1590 | case YP_SEQ: | |
1591 | case YP_SEQTYPE: | |
1592 | case YP_SEQLIST: | |
1593 | case YP_SET: | |
1594 | case YP_SETTYPE: | |
1595 | case YP_SETLIST: | |
1596 | case YP_CHOICE: | |
1597 | case YP_ANY: | |
1598 | case YP_OID: | |
1599 | case YP_IDEFINED: | |
1600 | printf (" <<%s%s>>", var, SVAL (yp -> yp_varexp)); | |
1601 | break; | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | if (direction == YP_ENCODER | |
1606 | && yp -> yp_varexp | |
1607 | && (cp = index (yp -> yp_varexp, ' ')) | |
1608 | && strncmp (cp + 1, "-> ", 3) == 0) { | |
1609 | *cp = NULL; | |
1610 | (void) sprintf (buffer, "(*%s) -> %s", yp -> yp_varexp, cp + 4); | |
1611 | yp -> yp_varexp = new_string (buffer); | |
1612 | } | |
1613 | } | |
1614 | ||
1615 | /* \f TYPE HANDLING */ | |
1616 | ||
1617 | static YP lookup_type (mod, id) | |
1618 | register char *mod, | |
1619 | *id; | |
1620 | { | |
1621 | register SY sy; | |
1622 | ||
1623 | for (sy = mysymbols; sy; sy = sy -> sy_next) { | |
1624 | if (mod) { | |
1625 | if (strcmp (sy -> sy_module, mod)) | |
1626 | continue; | |
1627 | } | |
1628 | else | |
1629 | if (strcmp (sy -> sy_module, mymodule) | |
1630 | && strcmp (sy -> sy_module, "UNIV")) | |
1631 | continue; | |
1632 | ||
1633 | if (strcmp (sy -> sy_name, id) == 0) | |
1634 | return sy -> sy_type; | |
1635 | } | |
1636 | ||
1637 | return NULLYP; | |
1638 | } | |
1639 | ||
1640 | /* \f */ | |
1641 | ||
1642 | static posy (yp, top, level, id, val, var, arrayflg) | |
1643 | register YP yp; | |
1644 | int top, | |
1645 | level, | |
1646 | arrayflg; | |
1647 | char *id, | |
1648 | *val, | |
1649 | *var; | |
1650 | { | |
1651 | register int i, | |
1652 | j; | |
1653 | register char *bp; | |
1654 | char *cp, | |
1655 | *dp, | |
1656 | *ep, | |
1657 | *newid, | |
1658 | buf1[BUFSIZ], | |
1659 | buf2[BUFSIZ], | |
1660 | buf3[BUFSIZ]; | |
1661 | register YP y; | |
1662 | register YV yv; | |
1663 | ||
1664 | (void) strcpy (bp = buf2, var); | |
1665 | bp += strlen (bp); | |
1666 | ||
1667 | switch (yp -> yp_code) { | |
1668 | case YP_BOOL: | |
1669 | if (aflag) | |
1670 | printag (yp, level + 4, NULLCP); | |
1671 | fprintf (fdef, "%*schar %s;\n", level * 4, "", | |
1672 | array(id, arrayflg)); | |
1673 | yp -> yp_varexp = new_string (buf2); | |
1674 | break; | |
1675 | ||
1676 | case YP_INT: | |
1677 | case YP_INTLIST: | |
1678 | case YP_ENUMLIST: | |
1679 | if (aflag) | |
1680 | printag (yp, level + 4, NULLCP); | |
1681 | fprintf (fdef, "%*sinteger %s;\n", level * 4, "", | |
1682 | array(id, arrayflg)); | |
1683 | yp -> yp_varexp = new_string (buf2); | |
1684 | if (yp -> yp_code == YP_INT) | |
1685 | break; | |
1686 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) { | |
1687 | modsym_aux (yv -> yv_named, buf1); | |
1688 | fprintf (fdef, "#define\t%s_%s\t%d\n", | |
1689 | modsym (mymodule, top ? eval : id, "int"), | |
1690 | buf1, val2int (yv)); | |
1691 | } | |
1692 | break; | |
1693 | ||
1694 | case YP_BIT: | |
1695 | case YP_BITLIST: | |
1696 | if (!top) { | |
1697 | if (aflag) | |
1698 | printag (yp, level + 4, NULLCP); | |
1699 | fprintf (fdef, "%*sPE %s;\n", level * 4, "", | |
1700 | array(id, arrayflg)); | |
1701 | } | |
1702 | if (fflag) { | |
1703 | fprintf (fact, "%*sif (%s)\n", level * 4, "", buf2); | |
1704 | fprintf (fact, | |
1705 | "%*spe_free (%s),\n%*s%s = NULLPE;\n", | |
1706 | (level + 1) * 4, "", buf2, | |
1707 | (level + 2) * 4, "", buf2); | |
1708 | } | |
1709 | yp -> yp_varexp = new_string (buf2); | |
1710 | if (yp -> yp_code != YP_BITLIST) | |
1711 | break; | |
1712 | i = -1; | |
1713 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) | |
1714 | if ((j = val2int (yv)) < 0) | |
1715 | pyyerror (yp, "invalid bit number in BIT STRING"); | |
1716 | else | |
1717 | if (j > i) | |
1718 | i = j; | |
1719 | if (i < sizeof (int) * 8) { /* NBBY */ | |
1720 | fprintf (fdef, "#define\t%s\t\"\\020", | |
1721 | modsym (mymodule, top ? eval : id, "bits")); | |
1722 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) | |
1723 | if (yv -> yv_flags & YV_NAMED) | |
1724 | fprintf (fdef, "\\0%o%s", | |
1725 | val2int (yv) + 1, yv -> yv_named); | |
1726 | else | |
1727 | fprintf (fdef, "\\0%oBIT%d", | |
1728 | val2int (yv) + 1, val2int (yv)); | |
1729 | fprintf (fdef, "\"\n"); | |
1730 | } | |
1731 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) { | |
1732 | modsym_aux (yv -> yv_named, buf1); | |
1733 | fprintf (fdef, "#define\t%s_%s\t%d\n", | |
1734 | modsym (mymodule, top ? eval : id, "bit"), | |
1735 | buf1, val2int (yv)); | |
1736 | } | |
1737 | break; | |
1738 | ||
1739 | case YP_REAL: | |
1740 | if (aflag) | |
1741 | printag (yp, level + 4, NULLCP); | |
1742 | fprintf (fdef, "%*sdouble %s;\n", level * 4, "", | |
1743 | array(id, arrayflg)); | |
1744 | yp -> yp_varexp = new_string (buf2); | |
1745 | break; | |
1746 | ||
1747 | case YP_OCT: | |
1748 | if (!top) { | |
1749 | if (aflag) | |
1750 | printag (yp, level + 4, NULLCP); | |
1751 | fprintf (fdef, "%*sstruct qbuf *%s;\n", level * 4, "", | |
1752 | array(id, arrayflg)); | |
1753 | } | |
1754 | if (fflag) { | |
1755 | fprintf (fact, "%*sif (%s)\n", level * 4, "", buf2); | |
1756 | fprintf (fact, | |
1757 | "%*sqb_free (%s),\n%*s%s = NULL;\n", | |
1758 | (level + 1) * 4, "", buf2, | |
1759 | (level + 2) * 4, "", buf2); | |
1760 | } | |
1761 | yp -> yp_varexp = new_string (buf2); | |
1762 | break; | |
1763 | ||
1764 | case YP_NULL: | |
1765 | if (aflag) | |
1766 | printag (yp, level + 4, NULLCP); | |
1767 | fprintf (fdef, "%*schar %s;\n", level * 4, "", | |
1768 | array(id, arrayflg)); | |
1769 | if (yp -> yp_flags & YP_OPTIONAL) | |
1770 | yp -> yp_varexp = new_string (buf2); | |
1771 | break; | |
1772 | ||
1773 | case YP_SEQ: | |
1774 | case YP_SET: | |
1775 | case YP_ANY: | |
1776 | if (!top) { | |
1777 | if (aflag) | |
1778 | printag (yp, level + 4, NULLCP); | |
1779 | fprintf (fdef, "%*sPE %s;\n", level * 4, "", | |
1780 | array(id, arrayflg)); | |
1781 | } | |
1782 | if (fflag) { | |
1783 | fprintf (fact, "%*sif (%s)\n", level * 4, "", buf2); | |
1784 | fprintf (fact, | |
1785 | "%*spe_free (%s),\n%*s%s = NULLPE;\n", | |
1786 | (level + 1) * 4, "", buf2, | |
1787 | (level + 2) * 4, "", buf2); | |
1788 | } | |
1789 | yp -> yp_varexp = new_string (buf2); | |
1790 | break; | |
1791 | ||
1792 | case YP_SEQTYPE: | |
1793 | case YP_SETTYPE: | |
1794 | ep = yp -> yp_code != YP_SETTYPE ? "element" : "member"; | |
1795 | if ((cp = rindex (buf2, ' ')) && *++cp) { | |
1796 | if ((dp = rindex (cp, '.')) && *++dp) | |
1797 | cp = dp; | |
1798 | (void) sprintf (dp = buf1, "%*.*s", | |
1799 | cp - buf2, cp - buf2, buf2); | |
1800 | dp += strlen (dp); | |
1801 | } | |
1802 | else { | |
1803 | (void) strcpy (buf1, buf2); | |
1804 | dp = NULL; | |
1805 | } | |
1806 | newid = yp -> yp_ptrname ? yp -> yp_ptrname : id; | |
1807 | if (h2flag && top) | |
1808 | fprintf (fdef, "%*sint\tnelem;\n", level * 4, ""); | |
1809 | if (!top) { | |
1810 | if (yp -> yp_structname) | |
1811 | id = yp -> yp_structname; | |
1812 | else if (!Hflag) | |
1813 | id = gensym (ep, NULLCP); | |
1814 | if (aflag) | |
1815 | printag (yp, level + 4, NULLCP); | |
1816 | fprintf (fdef, "%*sstruct %s {\n", level * 4, "", id); | |
1817 | if (h2flag) | |
1818 | fprintf (fdef, "%*sint\tnelem;\n", (level + 1) * 4, ""); | |
1819 | } | |
1820 | if (dp) | |
1821 | (void) strcpy (dp, newid); | |
1822 | (void) strcpy (bp = buf2, id); | |
1823 | bp += strlen (bp); | |
1824 | ||
1825 | if (!top) | |
1826 | yp -> yp_declexp = new_string (id); | |
1827 | ||
1828 | if (dp) | |
1829 | (void) strcpy (dp, newid); | |
1830 | yp -> yp_varexp = new_string (buf1); | |
1831 | if ((y = yp -> yp_type) -> yp_code == YP_IDEFINED && hflag) { | |
1832 | modsym_aux (y -> yp_identifier, cp = buf3); | |
1833 | if (h2flag) { | |
1834 | cp += strlen(cp); | |
1835 | (void) sprintf (cp, "[n_%s]", PARVAL (yp->yp_declexp)); | |
1836 | cp = buf3; | |
1837 | } | |
1838 | } | |
1839 | else { | |
1840 | switch (y -> yp_code) { | |
1841 | case YP_SEQLIST: | |
1842 | case YP_SETLIST: | |
1843 | case YP_SETTYPE: | |
1844 | case YP_SEQTYPE: | |
1845 | case YP_CHOICE: | |
1846 | case YP_IDEFINED: | |
1847 | cp = gensym (ep, h2flag ? PARVAL(yp->yp_declexp) : NULLCP); | |
1848 | break; | |
1849 | default: | |
1850 | cp = gensym (ep, NULLCP); | |
1851 | break; | |
1852 | } | |
1853 | } | |
1854 | (void) sprintf (bp, " -> %s", cp); | |
1855 | if (fflag) { | |
1856 | if (!top) { | |
1857 | fprintf (fact, "%*s{\n", level * 4, ""); | |
1858 | level++; | |
1859 | if (h2flag) { | |
1860 | fprintf (fact, "%*sint n_%s;\n", | |
1861 | level * 4, "", PARVAL (yp->yp_declexp)); | |
1862 | fprintf (fact, "%*sstruct %s *%s = %s;\n\n", | |
1863 | level * 4, "", id, id, var); | |
1864 | } | |
1865 | else | |
1866 | fprintf (fact, "%*sstruct %s *%s;\n\n", | |
1867 | level * 4, "", id, id); | |
1868 | } | |
1869 | if (h2flag) { | |
1870 | fprintf (fact, "%*sfor (n_%s = 0;\n", | |
1871 | level * 4, "", PARVAL(yp -> yp_declexp)); | |
1872 | fprintf (fact, "%*sn_%s < %s -> nelem;\n", | |
1873 | (level + 2) * 4, "", PARVAL(yp->yp_declexp), id); | |
1874 | fprintf (fact, "%*sn_%s++) {\n", | |
1875 | (level + 2) * 4, "", PARVAL(yp->yp_declexp)); | |
1876 | } | |
1877 | else { | |
1878 | fprintf (fact, | |
1879 | "%*sfor (%s = %s; %s;) {\n", | |
1880 | level * 4, "", id, buf1, id); | |
1881 | fprintf (fact, "%*sstruct %s *f_%s = %s -> next;\n\n", | |
1882 | (level + 1) * 4, "", | |
1883 | top ? modsym (mymodule, val, "type") : id, | |
1884 | id, id); | |
1885 | } | |
1886 | } | |
1887 | level++; | |
1888 | posy (y, 0, level, cp, ep, buf2, h2flag); | |
1889 | *bp = NULL; | |
1890 | if (y -> yp_code != YP_IDEFINED) | |
1891 | free (cp); | |
1892 | if (!h2flag) | |
1893 | fprintf (fdef, "\n%*sstruct %s *next;\n", level * 4, "", | |
1894 | top ? modsym (mymodule, val, "type") : id); | |
1895 | ||
1896 | level--; | |
1897 | ||
1898 | (void) strcpy (bp = buf2, var); | |
1899 | bp += strlen (bp); | |
1900 | if (fflag) { | |
1901 | if (!h2flag) { | |
1902 | fprintf (fact, "\n%*sif (%s)\n%*sfree ((char *) %s);", | |
1903 | (level + 1) * 4, "", id, | |
1904 | (level + 2) * 4, "", id); | |
1905 | fprintf (fact, "\n%*s%s = f_%s;", (level + 1) * 4, "", | |
1906 | id, id); | |
1907 | } | |
1908 | fprintf (fact, "\n%*s}\n", level * 4, ""); | |
1909 | ||
1910 | if (!top) { | |
1911 | if (h2flag) | |
1912 | fprintf (fact, "\n%*s%s = NULL;\n", | |
1913 | level * 4, "", var); | |
1914 | else | |
1915 | fprintf (fact, "\n%*s%s = NULL;\n", | |
1916 | level * 4, "", yp -> yp_varexp); | |
1917 | ||
1918 | level--; | |
1919 | fprintf (fact, "%*s}\n", level * 4, ""); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | if (!top) { | |
1924 | fprintf (fdef, "%*s} *%s;\n", level * 4, "", | |
1925 | array(newid, arrayflg)); | |
1926 | if (!Hflag) | |
1927 | free (id); | |
1928 | } | |
1929 | break; | |
1930 | ||
1931 | case YP_SEQLIST: | |
1932 | case YP_SETLIST: | |
1933 | ep = yp -> yp_code != YP_SETLIST ? "element" : "member"; | |
1934 | if ((cp = rindex (buf2, ' ')) && *++cp) { | |
1935 | if ((dp = rindex (cp, '.')) && *++dp) | |
1936 | cp = dp; | |
1937 | (void) sprintf (dp = buf1, "%*.*s", | |
1938 | cp - buf2, cp - buf2, buf2); | |
1939 | dp += strlen (dp); | |
1940 | } | |
1941 | else { | |
1942 | (void) strcpy (buf1, buf2); | |
1943 | dp = NULL; | |
1944 | } | |
1945 | newid = yp -> yp_ptrname ? yp -> yp_ptrname : id; | |
1946 | if (!top) { | |
1947 | if (yp -> yp_structname) | |
1948 | id = yp -> yp_structname; | |
1949 | else if (!Hflag) | |
1950 | id = gensym (ep, NULLCP); | |
1951 | if (aflag) | |
1952 | printag (yp, level + 4, NULLCP); | |
1953 | fprintf (fdef, "%*sstruct %s {\n", level * 4, "", id); | |
1954 | ||
1955 | if (dp) | |
1956 | (void) strcpy (dp, newid); | |
1957 | ||
1958 | if (fflag) { | |
1959 | fprintf (fact, "%*sif (%s) {\n", | |
1960 | level * 4, "", buf1); | |
1961 | i = 0; | |
1962 | for (y = yp -> yp_type; y; y = y -> yp_next) { | |
1963 | switch (y -> yp_code) { | |
1964 | case YP_BOOL: | |
1965 | case YP_INT: | |
1966 | case YP_INTLIST: | |
1967 | case YP_ENUMLIST: | |
1968 | case YP_REAL: | |
1969 | case YP_NULL: | |
1970 | continue; | |
1971 | ||
1972 | default: | |
1973 | i = 1; | |
1974 | break; | |
1975 | } | |
1976 | break; | |
1977 | } | |
1978 | if (i) | |
1979 | fprintf (fact, "%*sstruct %s *%s = %s;\n\n", | |
1980 | (level + 1) * 4, "", id, id, buf1); | |
1981 | } | |
1982 | (void) strcpy (bp = buf2, id); | |
1983 | bp += strlen (bp); | |
1984 | yp -> yp_declexp = new_string (id); | |
1985 | ||
1986 | level++; | |
1987 | } | |
1988 | if (dp) | |
1989 | (void) strcpy (dp, newid); | |
1990 | yp -> yp_varexp = new_string (buf1); | |
1991 | for (y = yp -> yp_type, i = 0; y; y = y -> yp_next) { | |
1992 | if (y -> yp_flags & YP_OPTIONAL) | |
1993 | switch (y -> yp_code) { | |
1994 | case YP_BOOL: | |
1995 | case YP_INT: | |
1996 | case YP_INTLIST: | |
1997 | case YP_ENUMLIST: | |
1998 | case YP_REAL: | |
1999 | case YP_NULL: | |
2000 | { | |
2001 | char obuf[BUFSIZ]; | |
2002 | ||
2003 | if (i == 0) | |
2004 | fprintf (fdef, "%*sint optionals;\n", | |
2005 | level * 4, ""); | |
2006 | if (y -> yp_flags & YP_ID) | |
2007 | modsym_aux (y -> yp_id, cp = buf1); | |
2008 | else { | |
2009 | cp = gensym (ep, NULLCP); | |
2010 | (void) strcpy (buf1, cp); | |
2011 | free (cp); | |
2012 | cp = buf1; | |
2013 | } | |
2014 | (void) sprintf (obuf, "%s_%s", | |
2015 | modsym (mymodule, | |
2016 | top ? eval : id, | |
2017 | "opt"), cp); | |
2018 | fprintf (fdef, "#define\t%s (0%08o)\n", obuf, | |
2019 | 1 << i); | |
2020 | y -> yp_optcontrol = new_string (obuf); | |
2021 | y -> yp_flags |= YP_OPTCONTROL; | |
2022 | ||
2023 | i ++; | |
2024 | if (i >= 8 * sizeof (int)) | |
2025 | yyerror ("too many optionals in structure"); | |
2026 | } | |
2027 | break; | |
2028 | } | |
2029 | } | |
2030 | if (i > 0) fprintf (fdef, "\n"); | |
2031 | ||
2032 | for (y = yp -> yp_type, i = 1; y; y = y -> yp_next, i++) { | |
2033 | if (y -> yp_flags & YP_ID) | |
2034 | modsym_aux (y -> yp_id, cp = buf1); | |
2035 | else | |
2036 | cp = gensym (ep, NULLCP); | |
2037 | (void) sprintf (bp, " -> %s", cp); | |
2038 | posy (y, 0, level, cp, ep, buf2, 0); | |
2039 | *bp = NULL; | |
2040 | if (!(y -> yp_flags & YP_ID)) | |
2041 | free (cp); | |
2042 | if (y -> yp_next) | |
2043 | fprintf (fdef, "\n"); | |
2044 | } | |
2045 | if (i == 1) | |
2046 | fprintf (fdef, "%*schar dummy;\n", level * 4, ""); | |
2047 | if (!top) { | |
2048 | level--; | |
2049 | ||
2050 | (void) strcpy (bp = buf2, var); | |
2051 | bp += strlen (bp); | |
2052 | if (fflag) { | |
2053 | fprintf (fact, "\n%*sif (%s)\n%*sfree ((char *) %s);\n%*s%s = NULL;\n", | |
2054 | (level + 1) * 4, "", yp -> yp_varexp, | |
2055 | (level + 2) * 4, "", yp -> yp_varexp, | |
2056 | (level + 1) * 4, "", yp -> yp_varexp); | |
2057 | fprintf (fact, "%*s}\n", level * 4, ""); | |
2058 | } | |
2059 | ||
2060 | fprintf (fdef, "%*s} *%s;\n", level * 4, "", | |
2061 | array(newid, arrayflg)); | |
2062 | if (!Hflag) | |
2063 | free (id); | |
2064 | } | |
2065 | break; | |
2066 | ||
2067 | case YP_CHOICE: | |
2068 | if ((cp = rindex (buf2, ' ')) && *++cp) { | |
2069 | if ((dp = rindex (cp, '.')) && *++dp) | |
2070 | cp = dp; | |
2071 | (void) sprintf (dp = buf1, "%*.*s", | |
2072 | cp - buf2, cp - buf2, buf2); | |
2073 | dp += strlen (dp); | |
2074 | } | |
2075 | else { | |
2076 | (void) strcpy (buf1, buf2); | |
2077 | dp = NULL; | |
2078 | } | |
2079 | newid = yp -> yp_ptrname ? yp -> yp_ptrname : id; | |
2080 | if (!top) { | |
2081 | if (yp -> yp_structname) | |
2082 | id = yp -> yp_structname; | |
2083 | else if (!Hflag) | |
2084 | id = gensym ("choice", NULLCP); | |
2085 | if (aflag) | |
2086 | printag (yp, level + 4, NULLCP); | |
2087 | fprintf (fdef, "%*sstruct %s {\n", level * 4, "", id); | |
2088 | ||
2089 | if (dp) | |
2090 | (void) strcpy (dp, newid); | |
2091 | if (fflag) { | |
2092 | fprintf (fact, "%*sif (%s) {\n%*sstruct %s *%s = %s;\n\n", | |
2093 | level * 4, "", buf1, | |
2094 | (level + 1) * 4, "", id, id, buf1); | |
2095 | } | |
2096 | (void) strcpy (bp = buf2, id); | |
2097 | bp += strlen (bp); | |
2098 | yp -> yp_declexp = new_string (id); | |
2099 | ||
2100 | level++; | |
2101 | } | |
2102 | if (dp) | |
2103 | (void) strcpy (dp, newid); | |
2104 | yp -> yp_varexp = new_string (buf1); | |
2105 | fprintf (fdef, "%*sint offset;\n", level * 4, ""); | |
2106 | if (fflag) | |
2107 | fprintf (fact, "%*sswitch (%s -> offset) {\n", | |
2108 | level * 4, "", id); | |
2109 | if (top) | |
2110 | cp = modsym (mymodule, val, "type"); | |
2111 | else | |
2112 | cp = id; | |
2113 | (void) sprintf (ep = buf1, "%s_", cp); | |
2114 | ep += strlen (ep); | |
2115 | for (y = yp -> yp_type, i = 1; y; y = y -> yp_next, i++) { | |
2116 | if (y -> yp_flags & YP_ID) | |
2117 | modsym_aux (y -> yp_id, ep); | |
2118 | else | |
2119 | (void) sprintf (ep, "%d", i); | |
2120 | y -> yp_offset = new_string (buf1); | |
2121 | fprintf (fdef, "#define\t%s\t%d\n", y -> yp_offset, i); | |
2122 | } | |
2123 | fprintf (fdef, "\n%*sunion {\n", level * 4, ""); | |
2124 | level++; | |
2125 | for (y = yp -> yp_type; y; y = y -> yp_next) { | |
2126 | if (y -> yp_flags & YP_ID) | |
2127 | modsym_aux (y -> yp_id, cp = buf1); | |
2128 | else | |
2129 | cp = gensym ("choice", NULLCP); | |
2130 | if (fflag) { | |
2131 | fprintf (fact, "%*scase %s:\n", level * 4, "", | |
2132 | y -> yp_offset); | |
2133 | level++; | |
2134 | } | |
2135 | (void) sprintf (bp, " -> un.%s", cp); | |
2136 | posy (y, 0, level, cp, "choice", buf2, 0); | |
2137 | *bp = NULL; | |
2138 | if (fflag) { | |
2139 | fprintf (fact, "%*sbreak;\n", level * 4, ""); | |
2140 | if (y -> yp_next) | |
2141 | fprintf (fact, "\n"); | |
2142 | level--; | |
2143 | } | |
2144 | if (!(y -> yp_flags & YP_ID)) | |
2145 | free (cp); | |
2146 | if (y -> yp_next) | |
2147 | fprintf (fdef, "\n"); | |
2148 | } | |
2149 | level--; | |
2150 | fprintf (fdef, "%*s} un;\n", level * 4, ""); | |
2151 | if (fflag) | |
2152 | fprintf (fact, "%*s}\n", level * 4, ""); | |
2153 | if (!top) { | |
2154 | level--; | |
2155 | ||
2156 | (void) strcpy (bp = buf2, var); | |
2157 | bp += strlen (bp); | |
2158 | if (fflag) { | |
2159 | fprintf (fact, "\n%*sif (%s)\n%*sfree ((char *) %s);\n%*s%s = NULL;\n", | |
2160 | (level + 1) * 4, "", yp -> yp_varexp, | |
2161 | (level + 2) * 4, "", yp -> yp_varexp, | |
2162 | (level + 1) * 4, "", yp -> yp_varexp); | |
2163 | fprintf (fact, "%*s}\n", level * 4, ""); | |
2164 | } | |
2165 | ||
2166 | fprintf (fdef, "%*s} *%s;\n", level * 4, "", | |
2167 | array(newid, arrayflg)); | |
2168 | if (!Hflag) | |
2169 | free (id); | |
2170 | } | |
2171 | break; | |
2172 | ||
2173 | case YP_OID: | |
2174 | if (!top) { | |
2175 | if (aflag) | |
2176 | printag (yp, level + 4, NULLCP); | |
2177 | fprintf (fdef, "%*sOID %s;\n", level * 4, "", | |
2178 | array(id, arrayflg)); | |
2179 | } | |
2180 | if (fflag) { | |
2181 | fprintf (fact, "%*sif (%s)\n", level * 4, "", buf2); | |
2182 | fprintf (fact, | |
2183 | "%*soid_free (%s),\n%*s%s = NULLOID;\n", | |
2184 | (level + 1) * 4, "", buf2, | |
2185 | (level + 2) * 4, "", buf2); | |
2186 | } | |
2187 | yp -> yp_varexp = new_string (buf2); | |
2188 | break; | |
2189 | ||
2190 | case YP_IDEFINED: | |
2191 | if (aflag) | |
2192 | printag (yp, level + 4, NULLCP); | |
2193 | fprintf (fdef, "%*sstruct %s *%s;\n", level * 4, "", | |
2194 | modsym (yp -> yp_module, yp -> yp_identifier, "type"), | |
2195 | array(id, arrayflg)); | |
2196 | if (fflag) { | |
2197 | fprintf (fact, "%*sif (%s)\n", level * 4, "", buf2); | |
2198 | fprintf (fact, | |
2199 | "%*s%s (%s),\n%*s%s = NULL;\n", | |
2200 | (level + 1) * 4, "", | |
2201 | modsym (yp -> yp_module, yp -> yp_identifier, "free"), | |
2202 | buf2, (level + 2) * 4, "", buf2); | |
2203 | } | |
2204 | yp -> yp_varexp = new_string (buf2); | |
2205 | break; | |
2206 | ||
2207 | default: | |
2208 | myyerror ("unknown type: %d", yp -> yp_code); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | /* \f */ | |
2213 | ||
2214 | static printag (yp, level, pullup) | |
2215 | register YP yp; | |
2216 | int level; | |
2217 | char *pullup; | |
2218 | { | |
2219 | fprintf (fdef, "%*s/* ", level * 4, ""); | |
2220 | switch (yp -> yp_code) { | |
2221 | case YP_IDEFINED: | |
2222 | if (yp -> yp_module && strcmp (yp -> yp_module, mymodule)) | |
2223 | fprintf (fdef, "%s.", yp -> yp_module); | |
2224 | fprintf (fdef, "%s", yp -> yp_identifier); | |
2225 | break; | |
2226 | ||
2227 | default: | |
2228 | fprintf (fdef, "%s", tags[yp -> yp_code]); | |
2229 | break; | |
2230 | } | |
2231 | if (pullup) | |
2232 | fprintf (fdef, " pulled up from %s", pullup); | |
2233 | fprintf (fdef, " */\n"); | |
2234 | } | |
2235 | ||
2236 | /* \f */ | |
2237 | ||
2238 | static xalloc (yp, top, level, arg, type, brackets) | |
2239 | register YP yp; | |
2240 | int top, | |
2241 | level, | |
2242 | brackets; | |
2243 | char *arg, | |
2244 | *type; | |
2245 | { | |
2246 | int didone; | |
2247 | register YP y; | |
2248 | ||
2249 | if (hflag && !arg && !type) | |
2250 | return; | |
2251 | ||
2252 | didone = 0; | |
2253 | ||
2254 | if (arg && type) { | |
2255 | if (brackets && !didone) { | |
2256 | printf ("%*s%%{\n", level * 4, ""); | |
2257 | level++, didone++; | |
2258 | } | |
2259 | ||
2260 | if (h2flag && (yp -> yp_code == YP_SEQTYPE || | |
2261 | yp -> yp_code == YP_SETTYPE)) { | |
2262 | printf ("%*s{\n%*sPE xx_pe = prim2%s ($$);\n\n", | |
2263 | level * 4, "", (level +1) * 4, "", | |
2264 | yp -> yp_code == YP_SEQTYPE ? "seq" : "set"); | |
2265 | printf ("%*sn_%s = xx_pe -> pe_cardinal > 0 ", | |
2266 | (level + 1) * 4, "", arg); | |
2267 | printf ("? xx_pe -> pe_cardinal : 0;\n%*s}\n", level * 4, ""); | |
2268 | printf ("%*sif ((*(%s) = (struct %s *)\n", | |
2269 | level * 4, "", arg, type); | |
2270 | printf ("%*scalloc (1 + (unsigned) n_%s, sizeof **(%s)", | |
2271 | (level + 2) * 4, "", arg, arg); | |
2272 | printf (")) == ((struct %s *) 0)) {\n", type); | |
2273 | printf ("%*sadvise (NULLCP, \"%%s\", PEPY_ERR_NOMEM);\n", | |
2274 | (level + 1) * 4, ""); | |
2275 | printf ("%*sreturn NOTOK;\n%*s}\n", (level + 1) * 4, "", | |
2276 | level * 4, ""); | |
2277 | printf ("%*s(*%s) -> nelem = n_%s;\n", level * 4, "", arg, arg); | |
2278 | printf ("%*sn_%s = 0;\n", level * 4, "", arg); | |
2279 | } else { | |
2280 | printf ("%*sif ((*(%s) = (struct %s *)\n", | |
2281 | level * 4, "", arg, type); | |
2282 | printf ("%*scalloc (1, sizeof **(%s))) == ((struct %s *) 0)) {\n", | |
2283 | (level + 2) * 4, "", arg, type); | |
2284 | printf ("%*sadvise (NULLCP, \"%%s\", PEPY_ERR_NOMEM);\n", | |
2285 | (level + 1) * 4, ""); | |
2286 | printf ("%*sreturn NOTOK;\n%*s}\n", (level + 1) * 4, "", | |
2287 | level * 4, ""); | |
2288 | } | |
2289 | } | |
2290 | switch (yp -> yp_code) { | |
2291 | case YP_SEQTYPE: | |
2292 | case YP_SETTYPE: | |
2293 | if (top) break; | |
2294 | case YP_CHOICE: | |
2295 | case YP_SEQLIST: | |
2296 | case YP_SETLIST: | |
2297 | for (y = yp -> yp_type; y; y = y -> yp_next) | |
2298 | switch (y -> yp_code) { | |
2299 | case YP_SEQTYPE: | |
2300 | case YP_SETTYPE: | |
2301 | if (h2flag && (yp -> yp_code == YP_SETLIST || | |
2302 | yp -> yp_code == YP_SEQLIST)) { | |
2303 | /* include allocation here - no chance later */ | |
2304 | if (brackets && !didone) { | |
2305 | printf ("%*s%%{\n", level * 4, ""); | |
2306 | level++, didone++; | |
2307 | } | |
2308 | if (y -> yp_declexp) | |
2309 | printf ("%*s%s = &(%s);\n", level * 4, "", | |
2310 | y -> yp_declexp, | |
2311 | y -> yp_varexp); | |
2312 | xalloc (y, top, level, y -> yp_declexp, | |
2313 | y -> yp_declexp, 0); | |
2314 | } | |
2315 | /* and continue ... */ | |
2316 | case YP_SEQLIST: | |
2317 | case YP_SETLIST: | |
2318 | case YP_CHOICE: | |
2319 | if (brackets && !didone) { | |
2320 | printf ("%*s%%{\n", level * 4, ""); | |
2321 | level++, didone++; | |
2322 | } | |
2323 | printf ("%*s%s = &(%s);\n", | |
2324 | level * 4, "", y -> yp_declexp, | |
2325 | y -> yp_varexp); | |
2326 | break; | |
2327 | } | |
2328 | break; | |
2329 | } | |
2330 | ||
2331 | if (brackets && didone) { | |
2332 | level--; | |
2333 | printf ("%*s%%}\n", level * 4, ""); | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | ||
2338 | static balloc (yp, var, action2, level) | |
2339 | register YP yp; | |
2340 | char *var, *action2; | |
2341 | int level; | |
2342 | { | |
2343 | printf ("\n%*s%%{\n", level * 4, ""); | |
2344 | level++; | |
2345 | ||
2346 | printf ("%*sif ((%s%s = prim2bit (pe_cpy ($$))) == NULLPE) {\n", | |
2347 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
2348 | printf ("%*sadvise (NULLCP, \"%%s\", PEPY_ERR_NOMEM);\n", (level + 1) * 4, ""); | |
2349 | printf ("%*sreturn NOTOK;\n%*s}\n", (level + 1) * 4, "", level * 4, ""); | |
2350 | ||
2351 | if (action2) | |
2352 | printf ("\n%*s%s\n", level * 4, "", action2); | |
2353 | ||
2354 | level--; | |
2355 | printf ("%*s%%}", level * 4, ""); | |
2356 | } | |
2357 | ||
2358 | #ifdef notdef | |
2359 | static qalloc (yp, var, action2, level) | |
2360 | register YP yp; | |
2361 | char *var, | |
2362 | *action2; | |
2363 | int level; | |
2364 | { | |
2365 | printf ("\n%*s%%{\n", level * 4, ""); | |
2366 | level++; | |
2367 | ||
2368 | printf ("%*sif ((%s%s = str2qb ($$, $$_len, 1)) == ((struct qbuf *) 0)) {\n", | |
2369 | level * 4, "", var, SVAL (yp -> yp_varexp)); | |
2370 | printf ("%*sadvise (NULLCP, \"%%s\", PEPY_ERR_NOMEM);\n", (level + 1) * 4, ""); | |
2371 | printf ("%*sreturn NOTOK;\n%*s}\n", (level + 1) * 4, "", level * 4, ""); | |
2372 | ||
2373 | if (action2) | |
2374 | printf ("\n%*s%s\n", level * 4, "", action2); | |
2375 | ||
2376 | level--; | |
2377 | printf ("%*s%%}", level * 4, ""); | |
2378 | } | |
2379 | #endif | |
2380 | ||
2381 | /* \f */ | |
2382 | ||
2383 | static choice_pullup (yp, partial) | |
2384 | register YP yp; | |
2385 | int partial; | |
2386 | { | |
2387 | register YP *x, | |
2388 | y, | |
2389 | z, | |
2390 | *z1, | |
2391 | z2, | |
2392 | z3; | |
2393 | ||
2394 | for (x = &yp -> yp_type; y = *x; x = &y -> yp_next) { | |
2395 | if (y -> yp_flags & (YP_TAG | YP_BOUND)) | |
2396 | continue; | |
2397 | ||
2398 | switch (y -> yp_code) { | |
2399 | case YP_IDEFINED: | |
2400 | if ((z = lookup_type (y -> yp_module, y -> yp_identifier)) | |
2401 | == NULLYP | |
2402 | || z -> yp_code != YP_CHOICE) | |
2403 | continue; | |
2404 | choice_pullup (z2 = copy_type (z), CH_FULLY); | |
2405 | goto patch; | |
2406 | ||
2407 | case YP_CHOICE: | |
2408 | choice_pullup (z2 = copy_type (y), CH_FULLY); | |
2409 | patch: ; | |
2410 | if (partial) { | |
2411 | *x = z2; | |
2412 | z2 -> yp_next = y -> yp_next; | |
2413 | continue; | |
2414 | } | |
2415 | break; | |
2416 | ||
2417 | default: | |
2418 | continue; | |
2419 | } | |
2420 | z = z3 = z2 -> yp_type; | |
2421 | for (z1 = &z -> yp_next; z2 = *z1; z1 = &z2 -> yp_next) | |
2422 | z3 = z2; | |
2423 | *z1 = y -> yp_next; | |
2424 | *x = z; | |
2425 | y = z3; | |
2426 | } | |
2427 | } | |
2428 | ||
2429 | /* \f */ | |
2430 | ||
2431 | static components_pullup (yp) | |
2432 | register YP yp; | |
2433 | { | |
2434 | register YP *x, | |
2435 | y, | |
2436 | z, | |
2437 | z1, | |
2438 | z2; | |
2439 | ||
2440 | for (x = &yp -> yp_type; y = *x; x = &y -> yp_next) { | |
2441 | if (!(y -> yp_flags & YP_COMPONENTS)) | |
2442 | continue; | |
2443 | ||
2444 | switch (y -> yp_code) { | |
2445 | case YP_SEQLIST: | |
2446 | case YP_SETLIST: | |
2447 | z = y; | |
2448 | break; | |
2449 | ||
2450 | case YP_IDEFINED: | |
2451 | if ((z = lookup_type (y -> yp_module, y -> yp_identifier)) | |
2452 | == NULLYP) { | |
2453 | warning ("COMPONENTS OF target \"%s\" is undefined", | |
2454 | y -> yp_identifier); | |
2455 | continue; | |
2456 | } | |
2457 | break; | |
2458 | } | |
2459 | if (yp -> yp_code != z -> yp_code) { | |
2460 | warning ("COMPONENTS OF target \"%s\" is wrong type, should be %s", | |
2461 | y -> yp_code == YP_IDEFINED ? y -> yp_identifier | |
2462 | : y -> yp_id ? y -> yp_id | |
2463 | : "", | |
2464 | yp -> yp_code == YP_SEQLIST ? "SEQUENCE" : "SET"); | |
2465 | continue; | |
2466 | } | |
2467 | if (z -> yp_type == NULLYP) | |
2468 | continue; | |
2469 | components_pullup (z = copy_type (z)); | |
2470 | *x = z2 = z -> yp_type; | |
2471 | for (x = &z -> yp_type; z1 = *x; x = &z1 -> yp_next) | |
2472 | z2 = z1; | |
2473 | *x = y -> yp_next; | |
2474 | y = z2; | |
2475 | } | |
2476 | } | |
2477 | ||
2478 | /* \f VALUE HANDLING */ | |
2479 | ||
2480 | static int val2int (yv) | |
2481 | register YV yv; | |
2482 | { | |
2483 | switch (yv -> yv_code) { | |
2484 | case YV_BOOL: | |
2485 | case YV_NUMBER: | |
2486 | return yv -> yv_number; | |
2487 | ||
2488 | case YV_STRING: | |
2489 | yyerror ("need an integer, not a string"); | |
2490 | ||
2491 | case YV_IDEFINED: | |
2492 | case YV_IDLIST: | |
2493 | yyerror ("haven't written symbol table for values yet"); | |
2494 | ||
2495 | case YV_NULL: | |
2496 | yyerror ("need an integer, not NULL"); | |
2497 | ||
2498 | default: | |
2499 | myyerror ("unknown value: %d", yv -> yv_code); | |
2500 | } | |
2501 | /* NOTREACHED */ | |
2502 | } | |
2503 | ||
2504 | static double val2real (yv) | |
2505 | register YV yv; | |
2506 | { | |
2507 | switch (yv -> yv_code) { | |
2508 | case YV_NUMBER: | |
2509 | return yv -> yv_number; | |
2510 | ||
2511 | case YV_REAL: | |
2512 | return yv -> yv_real; | |
2513 | ||
2514 | case YV_STRING: | |
2515 | yyerror ("need an integer, not a string"); | |
2516 | ||
2517 | case YV_IDEFINED: | |
2518 | case YV_IDLIST: | |
2519 | yyerror ("haven't written symbol table for values yet"); | |
2520 | ||
2521 | case YV_NULL: | |
2522 | yyerror ("need an integer, not NULL"); | |
2523 | ||
2524 | default: | |
2525 | myyerror ("unknown value: %d", yv -> yv_code); | |
2526 | } | |
2527 | /* NOTREACHED */ | |
2528 | } | |
2529 | ||
2530 | /* \f */ | |
2531 | ||
2532 | static val2prf (yv, level) | |
2533 | register YV yv; | |
2534 | int level; | |
2535 | { | |
2536 | register YV y; | |
2537 | ||
2538 | if (yv -> yv_flags & YV_ID) | |
2539 | printf ("%s ", yv -> yv_id); | |
2540 | ||
2541 | if (yv -> yv_flags & YV_TYPE) /* will this REALLY work??? */ | |
2542 | do_type1 (yv -> yv_type, 0, level, NULLCP, NULLCP, NULLCP, NULL); | |
2543 | ||
2544 | switch (yv -> yv_code) { | |
2545 | case YV_BOOL: | |
2546 | printf (yv -> yv_number ? "TRUE" : "FALSE"); | |
2547 | break; | |
2548 | ||
2549 | case YV_NUMBER: | |
2550 | if (yv -> yv_named) | |
2551 | printf ("%s", yv -> yv_named); | |
2552 | else | |
2553 | printf ("%d", yv -> yv_number); | |
2554 | break; | |
2555 | ||
2556 | case YV_REAL: | |
2557 | dump_real (yv -> yv_real); | |
2558 | break; | |
2559 | ||
2560 | case YV_STRING: | |
2561 | printf ("\"%s\"", yv -> yv_string); | |
2562 | break; | |
2563 | ||
2564 | case YV_IDEFINED: | |
2565 | if (yv -> yv_module) | |
2566 | printf ("%s.", yv -> yv_module); | |
2567 | printf ("%s", yv -> yv_identifier); | |
2568 | break; | |
2569 | ||
2570 | case YV_IDLIST: | |
2571 | case YV_VALIST: | |
2572 | printf ("{"); | |
2573 | for (y = yv -> yv_idlist; y; y = y -> yv_next) { | |
2574 | printf (" "); | |
2575 | val2prf (y, level + 1); | |
2576 | printf (y -> yv_next ? ", " : " "); | |
2577 | } | |
2578 | printf ("}"); | |
2579 | break; | |
2580 | ||
2581 | case YV_NULL: | |
2582 | printf ("NULL"); | |
2583 | break; | |
2584 | ||
2585 | default: | |
2586 | myyerror ("unknown value: %d", yv -> yv_code); | |
2587 | /* NOTREACHED */ | |
2588 | } | |
2589 | } | |
2590 | static dump_real (r) | |
2591 | double r; | |
2592 | { | |
2593 | #ifndef BSD44 | |
2594 | extern char *ecvt (); | |
2595 | char *cp; | |
2596 | char sbuf[128]; | |
2597 | int decpt, sign; | |
2598 | ||
2599 | cp = ecvt (r, 20, &decpt, &sign); | |
2600 | (void) strcpy (sbuf, cp); /* cp gets overwritten by printf */ | |
2601 | printf ("{ %s%s, 10, %d }", sign ? "-" : "", sbuf, | |
2602 | decpt - strlen (sbuf)); | |
2603 | #else | |
2604 | register char *cp, | |
2605 | *dp, | |
2606 | *sp; | |
2607 | char sbuf[128]; | |
2608 | ||
2609 | (void) sprintf (sbuf, "%.19e", r); | |
2610 | if (*(dp = sbuf) == '-') | |
2611 | sp = "-", dp++; | |
2612 | else | |
2613 | sp = ""; | |
2614 | ||
2615 | if (dp[1] != '.' || (cp = index (dp, 'e')) == NULL) { | |
2616 | printf ("{ 0, 10, 0 } -- %s --", sbuf); | |
2617 | return; | |
2618 | } | |
2619 | *cp++ = NULL; | |
2620 | printf ("{ %s%c%s, 10, %d }", | |
2621 | sp, *dp, dp + 2, atoi (cp) - strlen (dp + 2)); | |
2622 | #endif | |
2623 | } | |
2624 | ||
2625 | /* \f */ | |
2626 | ||
2627 | static int dfl2int (yp) | |
2628 | register YP yp; | |
2629 | { | |
2630 | register YV yv, | |
2631 | y; | |
2632 | ||
2633 | yv = yp -> yp_default; | |
2634 | switch (yv -> yv_code) { | |
2635 | case YV_BOOL: | |
2636 | case YV_NUMBER: | |
2637 | return yv -> yv_number; | |
2638 | ||
2639 | case YV_STRING: | |
2640 | yyerror ("need an integer, not a string"); | |
2641 | ||
2642 | case YV_REAL: | |
2643 | yyerror ("need an integer, not a real"); | |
2644 | ||
2645 | case YV_IDEFINED: | |
2646 | for (y = yp -> yp_value; y; y = y -> yv_next) | |
2647 | if (y -> yv_code == YV_NUMBER | |
2648 | && (y -> yv_flags & YV_NAMED) | |
2649 | && strcmp (yv -> yv_identifier, y -> yv_named) == 0) | |
2650 | return y -> yv_number; | |
2651 | /* and fall */ | |
2652 | ||
2653 | case YV_IDLIST: | |
2654 | yyerror ("haven't written symbol table for values yet"); | |
2655 | ||
2656 | case YV_NULL: | |
2657 | yyerror ("need an integer, not NULL"); | |
2658 | ||
2659 | default: | |
2660 | myyerror ("unknown value: %d", yv -> yv_code); | |
2661 | } | |
2662 | /* NOTREACHED */ | |
2663 | } | |
2664 | ||
2665 | /* \f DEBUG */ | |
2666 | ||
2667 | print_type (yp, level) | |
2668 | register YP yp; | |
2669 | register int level; | |
2670 | { | |
2671 | register YP y; | |
2672 | register YV yv; | |
2673 | ||
2674 | if (yp == NULLYP) | |
2675 | return; | |
2676 | ||
2677 | fprintf (stderr, "%*scode=0x%x flags=%s direction=0x%x\n", level * 4, "", | |
2678 | yp -> yp_code, sprintb (yp -> yp_flags, YPBITS), | |
2679 | yp -> yp_direction); | |
2680 | fprintf (stderr, | |
2681 | "%*sintexp=\"%s\" strexp=\"%s\" prfexp=0%o declexp=\"%s\" varexp=\"%s\"\n", | |
2682 | level * 4, "", yp -> yp_intexp, yp -> yp_strexp, yp -> yp_prfexp, | |
2683 | yp -> yp_declexp, yp -> yp_varexp); | |
2684 | if (yp -> yp_param_type) | |
2685 | fprintf (stderr, "%*sparameter type=\"%s\"\n", level * 4, "", | |
2686 | yp -> yp_param_type); | |
2687 | if (yp -> yp_action0) | |
2688 | fprintf (stderr, "%*saction0 at line %d=\"%s\"\n", level * 4, "", | |
2689 | yp -> yp_act0_lineno, yp -> yp_action0); | |
2690 | if (yp -> yp_action05) | |
2691 | fprintf (stderr, "%*saction05 at line %d=\"%s\"\n", level * 4, "", | |
2692 | yp -> yp_act05_lineno, yp -> yp_action05); | |
2693 | if (yp -> yp_action1) | |
2694 | fprintf (stderr, "%*saction1 at line %d=\"%s\"\n", level * 4, "", | |
2695 | yp -> yp_act1_lineno, yp -> yp_action1); | |
2696 | if (yp -> yp_action2) | |
2697 | fprintf (stderr, "%*saction2 at line %d=\"%s\"\n", level * 4, "", | |
2698 | yp -> yp_act2_lineno, yp -> yp_action2); | |
2699 | if (yp -> yp_action3) | |
2700 | fprintf (stderr, "%*saction3 at line %d=\"%s\"\n", level * 4, "", | |
2701 | yp -> yp_act3_lineno, yp -> yp_action3); | |
2702 | ||
2703 | if (yp -> yp_flags & YP_TAG) { | |
2704 | fprintf (stderr, "%*stag class=0x%x value=0x%x\n", level * 4, "", | |
2705 | yp -> yp_tag -> yt_class, yp -> yp_tag -> yt_value); | |
2706 | print_value (yp -> yp_tag -> yt_value, level + 1); | |
2707 | } | |
2708 | ||
2709 | if (yp -> yp_flags & YP_DEFAULT) { | |
2710 | fprintf (stderr, "%*sdefault=0x%x\n", level * 4, "", yp -> yp_default); | |
2711 | print_value (yp -> yp_default, level + 1); | |
2712 | } | |
2713 | ||
2714 | if (yp -> yp_flags & YP_ID) | |
2715 | fprintf (stderr, "%*sid=\"%s\"\n", level * 4, "", yp -> yp_id); | |
2716 | ||
2717 | if (yp -> yp_flags & YP_BOUND) | |
2718 | fprintf (stderr, "%*sbound=\"%s\"\n", level * 4, "", yp -> yp_bound); | |
2719 | ||
2720 | if (yp -> yp_offset) | |
2721 | fprintf (stderr, "%*soffset=\"%s\"\n", level * 4, "", yp -> yp_offset); | |
2722 | ||
2723 | switch (yp -> yp_code) { | |
2724 | case YP_INTLIST: | |
2725 | case YP_ENUMLIST: | |
2726 | case YP_BITLIST: | |
2727 | fprintf (stderr, "%*svalue=0x%x\n", level * 4, "", yp -> yp_value); | |
2728 | for (yv = yp -> yp_value; yv; yv = yv -> yv_next) { | |
2729 | print_value (yv, level + 1); | |
2730 | fprintf (stderr, "%*s----\n", (level + 1) * 4, ""); | |
2731 | } | |
2732 | break; | |
2733 | ||
2734 | case YP_SEQTYPE: | |
2735 | case YP_SEQLIST: | |
2736 | case YP_SETTYPE: | |
2737 | case YP_SETLIST: | |
2738 | case YP_CHOICE: | |
2739 | fprintf (stderr, "%*stype=0x%x\n", level * 4, "", yp -> yp_type); | |
2740 | for (y = yp -> yp_type; y; y = y -> yp_next) { | |
2741 | print_type (y, level + 1); | |
2742 | fprintf (stderr, "%*s----\n", (level + 1) * 4, ""); | |
2743 | } | |
2744 | break; | |
2745 | ||
2746 | case YP_IDEFINED: | |
2747 | fprintf (stderr, "%*smodule=\"%s\" identifier=\"%s\"\n", | |
2748 | level * 4, "", yp -> yp_module ? yp -> yp_module : "", | |
2749 | yp -> yp_identifier); | |
2750 | break; | |
2751 | ||
2752 | default: | |
2753 | break; | |
2754 | } | |
2755 | } | |
2756 | ||
2757 | /* \f */ | |
2758 | ||
2759 | static print_value (yv, level) | |
2760 | register YV yv; | |
2761 | register int level; | |
2762 | { | |
2763 | register YV y; | |
2764 | ||
2765 | if (yv == NULLYV) | |
2766 | return; | |
2767 | ||
2768 | fprintf (stderr, "%*scode=0x%x flags=%s\n", level * 4, "", | |
2769 | yv -> yv_code, sprintb (yv -> yv_flags, YVBITS)); | |
2770 | ||
2771 | if (yv -> yv_action) | |
2772 | fprintf (stderr, "%*saction at line %d=\"%s\"\n", level * 4, "", | |
2773 | yv -> yv_act_lineno, yv -> yv_action); | |
2774 | ||
2775 | if (yv -> yv_flags & YV_ID) | |
2776 | fprintf (stderr, "%*sid=\"%s\"\n", level * 4, "", yv -> yv_id); | |
2777 | ||
2778 | if (yv -> yv_flags & YV_NAMED) | |
2779 | fprintf (stderr, "%*snamed=\"%s\"\n", level * 4, "", yv -> yv_named); | |
2780 | ||
2781 | if (yv -> yv_flags & YV_TYPE) { | |
2782 | fprintf (stderr, "%*stype=0x%x\n", level * 4, "", yv -> yv_type); | |
2783 | print_type (yv -> yv_type, level + 1); | |
2784 | } | |
2785 | ||
2786 | switch (yv -> yv_code) { | |
2787 | case YV_NUMBER: | |
2788 | case YV_BOOL: | |
2789 | fprintf (stderr, "%*snumber=0x%x\n", level * 4, "", | |
2790 | yv -> yv_number); | |
2791 | break; | |
2792 | ||
2793 | case YV_STRING: | |
2794 | fprintf (stderr, "%*sstring=\"%s\"\n", level * 4, "", | |
2795 | yv -> yv_string); | |
2796 | break; | |
2797 | ||
2798 | case YV_IDEFINED: | |
2799 | if (yv -> yv_flags & YV_BOUND) | |
2800 | fprintf (stderr, "%*smodule=\"%s\" identifier=\"%s\"\n", | |
2801 | level * 4, "", yv -> yv_module, yv -> yv_identifier); | |
2802 | else | |
2803 | fprintf (stderr, "%*sbound identifier=\"%s\"\n", | |
2804 | level * 4, "", yv -> yv_identifier); | |
2805 | break; | |
2806 | ||
2807 | case YV_IDLIST: | |
2808 | case YV_VALIST: | |
2809 | for (y = yv -> yv_idlist; y; y = y -> yv_next) { | |
2810 | print_value (y, level + 1); | |
2811 | fprintf (stderr, "%*s----\n", (level + 1) * 4, ""); | |
2812 | } | |
2813 | break; | |
2814 | ||
2815 | default: | |
2816 | break; | |
2817 | } | |
2818 | } | |
2819 | ||
2820 | /* \f SYMBOLS */ | |
2821 | ||
2822 | static SY new_symbol (encpref, decpref, prfpref, mod, id, type) | |
2823 | register char *encpref, | |
2824 | *decpref, | |
2825 | *prfpref, | |
2826 | *mod, | |
2827 | *id; | |
2828 | register YP type; | |
2829 | { | |
2830 | register SY sy; | |
2831 | ||
2832 | if ((sy = (SY) calloc (1, sizeof *sy)) == NULLSY) | |
2833 | yyerror ("out of memory"); | |
2834 | sy -> sy_encpref = encpref; | |
2835 | sy -> sy_decpref = decpref; | |
2836 | sy -> sy_prfpref = prfpref; | |
2837 | sy -> sy_module = mod; | |
2838 | sy -> sy_name = id; | |
2839 | sy -> sy_type = type; | |
2840 | ||
2841 | return sy; | |
2842 | } | |
2843 | ||
2844 | ||
2845 | static SY add_symbol (s1, s2) | |
2846 | register SY s1, | |
2847 | s2; | |
2848 | { | |
2849 | register SY sy; | |
2850 | ||
2851 | if (s1 == NULLSY) | |
2852 | return s2; | |
2853 | ||
2854 | for (sy = s1; sy -> sy_next; sy = sy -> sy_next) | |
2855 | continue; | |
2856 | sy -> sy_next = s2; | |
2857 | ||
2858 | return s1; | |
2859 | } | |
2860 | ||
2861 | /* \f MODULES */ | |
2862 | ||
2863 | static MD lookup_module (module) | |
2864 | char *module; | |
2865 | { | |
2866 | register MD md; | |
2867 | ||
2868 | for (md = mymodules; md; md = md -> md_next) | |
2869 | if (strcmp (md -> md_module, module) == 0) | |
2870 | return md; | |
2871 | ||
2872 | if ((md = (MD) calloc (1, sizeof *md)) == NULLMD) | |
2873 | yyerror ("out of memory"); | |
2874 | md -> md_module = new_string (module); | |
2875 | ||
2876 | if (mymodules != NULLMD) | |
2877 | md -> md_next = mymodules; | |
2878 | mymodules = md; | |
2879 | ||
2880 | return NULLMD; | |
2881 | } | |
2882 | ||
2883 | /* \f TYPES */ | |
2884 | ||
2885 | YP new_type (code) | |
2886 | int code; | |
2887 | { | |
2888 | register YP yp; | |
2889 | ||
2890 | if ((yp = (YP) calloc (1, sizeof *yp)) == NULLYP) | |
2891 | yyerror ("out of memory"); | |
2892 | yp -> yp_code = code; | |
2893 | ||
2894 | return yp; | |
2895 | } | |
2896 | ||
2897 | ||
2898 | YP add_type (y, z) | |
2899 | register YP y, | |
2900 | z; | |
2901 | { | |
2902 | register YP yp; | |
2903 | ||
2904 | for (yp = y; yp -> yp_next; yp = yp -> yp_next) | |
2905 | continue; | |
2906 | yp -> yp_next = z; | |
2907 | ||
2908 | return y; | |
2909 | } | |
2910 | ||
2911 | /* \f */ | |
2912 | ||
2913 | YP copy_type (yp) | |
2914 | register YP yp; | |
2915 | { | |
2916 | register YP y; | |
2917 | ||
2918 | if (yp == NULLYP) | |
2919 | return NULLYP; | |
2920 | ||
2921 | y = new_type (yp -> yp_code); | |
2922 | y -> yp_direction = yp -> yp_direction; | |
2923 | ||
2924 | switch (yp -> yp_code) { | |
2925 | case YP_IDEFINED: | |
2926 | if (yp -> yp_module) | |
2927 | y -> yp_module = new_string (yp -> yp_module); | |
2928 | y -> yp_identifier = new_string (yp -> yp_identifier); | |
2929 | y -> yp_modid = oid_cpy (yp -> yp_modid); | |
2930 | break; | |
2931 | ||
2932 | case YP_SEQTYPE: | |
2933 | case YP_SEQLIST: | |
2934 | case YP_SETTYPE: | |
2935 | case YP_SETLIST: | |
2936 | case YP_CHOICE: | |
2937 | y -> yp_type = copy_type (yp -> yp_type); | |
2938 | break; | |
2939 | ||
2940 | case YP_INTLIST: | |
2941 | case YP_ENUMLIST: | |
2942 | case YP_BITLIST: | |
2943 | y -> yp_value = copy_value (yp -> yp_value); | |
2944 | break; | |
2945 | ||
2946 | default: | |
2947 | break; | |
2948 | } | |
2949 | ||
2950 | y -> yp_intexp = yp -> yp_intexp; | |
2951 | y -> yp_strexp = yp -> yp_strexp; | |
2952 | y -> yp_prfexp = yp -> yp_prfexp; | |
2953 | ||
2954 | y -> yp_declexp = yp -> yp_declexp; | |
2955 | y -> yp_varexp = yp -> yp_varexp; | |
2956 | ||
2957 | if (yp -> yp_structname) | |
2958 | y -> yp_structname = new_string (yp -> yp_structname); | |
2959 | if (yp -> yp_ptrname) | |
2960 | y -> yp_ptrname = new_string (yp -> yp_ptrname); | |
2961 | ||
2962 | if (yp -> yp_param_type) | |
2963 | y -> yp_param_type = new_string (yp -> yp_param_type); | |
2964 | ||
2965 | if (yp -> yp_action0) { | |
2966 | y -> yp_action0 = new_string (yp -> yp_action0); | |
2967 | y -> yp_act0_lineno = yp -> yp_act0_lineno; | |
2968 | } | |
2969 | ||
2970 | if (yp -> yp_action05) { | |
2971 | y -> yp_action05 = new_string (yp -> yp_action05); | |
2972 | y -> yp_act05_lineno = yp -> yp_act05_lineno; | |
2973 | } | |
2974 | ||
2975 | if (yp -> yp_action1) { | |
2976 | y -> yp_action1 = new_string (yp -> yp_action1); | |
2977 | y -> yp_act1_lineno = yp -> yp_act1_lineno; | |
2978 | } | |
2979 | ||
2980 | if (yp -> yp_action2) { | |
2981 | y -> yp_action2 = new_string (yp -> yp_action2); | |
2982 | y -> yp_act2_lineno = yp -> yp_act2_lineno; | |
2983 | } | |
2984 | ||
2985 | if (yp -> yp_action3) { | |
2986 | y -> yp_action3 = new_string (yp -> yp_action3); | |
2987 | y -> yp_act3_lineno = yp -> yp_act3_lineno; | |
2988 | } | |
2989 | ||
2990 | y -> yp_flags = yp -> yp_flags; | |
2991 | ||
2992 | if (yp -> yp_flags & YP_DEFAULT) | |
2993 | y -> yp_default = copy_value (yp -> yp_default); | |
2994 | ||
2995 | if (yp -> yp_flags & YP_ID) | |
2996 | y -> yp_id = new_string (yp -> yp_id); | |
2997 | ||
2998 | if (yp -> yp_flags & YP_TAG) | |
2999 | y -> yp_tag = copy_tag (yp -> yp_tag); | |
3000 | ||
3001 | if (yp -> yp_flags & YP_BOUND) | |
3002 | y -> yp_bound = new_string (yp -> yp_bound); | |
3003 | ||
3004 | if (yp -> yp_flags & YP_PARMVAL) | |
3005 | y -> yp_parm = new_string (yp -> yp_parm); | |
3006 | ||
3007 | if (yp -> yp_flags & YP_CONTROLLED) | |
3008 | y -> yp_control = new_string (yp -> yp_control); | |
3009 | ||
3010 | if (yp -> yp_flags & YP_OPTCONTROL) | |
3011 | y -> yp_optcontrol = new_string (yp -> yp_optcontrol); | |
3012 | ||
3013 | if (yp -> yp_offset) | |
3014 | y -> yp_offset = new_string (yp -> yp_offset); | |
3015 | ||
3016 | if (yp -> yp_next) | |
3017 | y -> yp_next = copy_type (yp -> yp_next); | |
3018 | ||
3019 | return y; | |
3020 | } | |
3021 | ||
3022 | /* \f VALUES */ | |
3023 | ||
3024 | YV new_value (code) | |
3025 | int code; | |
3026 | { | |
3027 | register YV yv; | |
3028 | ||
3029 | if ((yv = (YV) calloc (1, sizeof *yv)) == NULLYV) | |
3030 | yyerror ("out of memory"); | |
3031 | yv -> yv_code = code; | |
3032 | ||
3033 | return yv; | |
3034 | } | |
3035 | ||
3036 | ||
3037 | YV add_value (y, z) | |
3038 | register YV y, | |
3039 | z; | |
3040 | { | |
3041 | register YV yv; | |
3042 | ||
3043 | for (yv = y; yv -> yv_next; yv = yv -> yv_next) | |
3044 | continue; | |
3045 | yv -> yv_next = z; | |
3046 | ||
3047 | return y; | |
3048 | } | |
3049 | ||
3050 | /* \f */ | |
3051 | ||
3052 | YV copy_value (yv) | |
3053 | register YV yv; | |
3054 | { | |
3055 | register YV y; | |
3056 | ||
3057 | if (yv == NULLYV) | |
3058 | return NULLYV; | |
3059 | ||
3060 | y = new_value (yv -> yv_code); | |
3061 | y -> yv_flags = yv -> yv_flags; | |
3062 | ||
3063 | if (yv -> yv_action) { | |
3064 | y -> yv_action = new_string (yv -> yv_action); | |
3065 | y -> yv_act_lineno = yv -> yv_act_lineno; | |
3066 | } | |
3067 | ||
3068 | if (yv -> yv_flags & YV_ID) | |
3069 | y -> yv_id = new_string (yv -> yv_id); | |
3070 | ||
3071 | if (yv -> yv_flags & YV_NAMED) | |
3072 | y -> yv_named = new_string (yv -> yv_named); | |
3073 | ||
3074 | if (yv -> yv_flags & YV_TYPE) | |
3075 | y -> yv_type = copy_type (yv -> yv_type); | |
3076 | ||
3077 | switch (yv -> yv_code) { | |
3078 | case YV_NUMBER: | |
3079 | case YV_BOOL: | |
3080 | y -> yv_number = yv -> yv_number; | |
3081 | break; | |
3082 | ||
3083 | case YV_STRING: | |
3084 | y -> yv_string = new_string (yv -> yv_string); | |
3085 | break; | |
3086 | ||
3087 | case YV_IDEFINED: | |
3088 | if (yv -> yv_module) | |
3089 | y -> yv_module = new_string (yv -> yv_module); | |
3090 | y -> yv_identifier = new_string (yv -> yv_identifier); | |
3091 | break; | |
3092 | ||
3093 | case YV_IDLIST: | |
3094 | case YV_VALIST: | |
3095 | y -> yv_idlist = copy_value (yv -> yv_idlist); | |
3096 | break; | |
3097 | ||
3098 | default: | |
3099 | break; | |
3100 | } | |
3101 | ||
3102 | if (yv -> yv_next) | |
3103 | y -> yv_next = copy_value (yv -> yv_next); | |
3104 | ||
3105 | return y; | |
3106 | } | |
3107 | ||
3108 | /* \f TAGS */ | |
3109 | ||
3110 | YT new_tag (class) | |
3111 | PElementClass class; | |
3112 | { | |
3113 | register YT yt; | |
3114 | ||
3115 | if ((yt = (YT) calloc (1, sizeof *yt)) == NULLYT) | |
3116 | yyerror ("out of memory"); | |
3117 | yt -> yt_class = class; | |
3118 | ||
3119 | return yt; | |
3120 | } | |
3121 | ||
3122 | /* \f */ | |
3123 | ||
3124 | YT copy_tag (yt) | |
3125 | register YT yt; | |
3126 | { | |
3127 | register YT y; | |
3128 | ||
3129 | if (yt == NULLYT) | |
3130 | return NULLYT; | |
3131 | ||
3132 | y = new_tag (yt -> yt_class); | |
3133 | ||
3134 | y -> yt_value = copy_value (yt -> yt_value); | |
3135 | ||
3136 | return y; | |
3137 | } | |
3138 | ||
3139 | /* \f STRINGS */ | |
3140 | ||
3141 | char *new_string (s) | |
3142 | register char *s; | |
3143 | { | |
3144 | register char *p; | |
3145 | ||
3146 | if ((p = malloc ((unsigned) (strlen (s) + 1))) == NULLCP) | |
3147 | yyerror ("out of memory"); | |
3148 | ||
3149 | (void) strcpy (p, s); | |
3150 | return p; | |
3151 | } | |
3152 | ||
3153 | /* \f SYMBOLS */ | |
3154 | ||
3155 | static struct triple { | |
3156 | char *t_name; | |
3157 | PElementClass t_class; | |
3158 | PElementID t_id; | |
3159 | } triples[] = { | |
3160 | "IA5String", PE_CLASS_UNIV, PE_DEFN_IA5S, | |
3161 | "ISO646String", PE_CLASS_UNIV, PE_DEFN_IA5S, | |
3162 | "NumericString", PE_CLASS_UNIV, PE_DEFN_NUMS, | |
3163 | "PrintableString", PE_CLASS_UNIV, PE_DEFN_PRTS, | |
3164 | "T61String", PE_CLASS_UNIV, PE_DEFN_T61S, | |
3165 | "TeletexString", PE_CLASS_UNIV, PE_DEFN_T61S, | |
3166 | "VideotexString", PE_CLASS_UNIV, PE_DEFN_VTXS, | |
3167 | "GeneralizedTime", PE_CLASS_UNIV, PE_DEFN_GENT, | |
3168 | "GeneralisedTime", PE_CLASS_UNIV, PE_DEFN_GENT, | |
3169 | "UTCTime", PE_CLASS_UNIV, PE_DEFN_UTCT, | |
3170 | "UniversalTime", PE_CLASS_UNIV, PE_DEFN_UTCT, | |
3171 | "GraphicString", PE_CLASS_UNIV, PE_DEFN_GFXS, | |
3172 | "VisibleString", PE_CLASS_UNIV, PE_DEFN_VISS, | |
3173 | "GeneralString", PE_CLASS_UNIV, PE_DEFN_GENS, | |
3174 | "EXTERNAL", PE_CLASS_UNIV, PE_CONS_EXTN, | |
3175 | "ObjectDescriptor", PE_CLASS_UNIV, PE_PRIM_ODE, | |
3176 | ||
3177 | NULL | |
3178 | }; | |
3179 | ||
3180 | /* \f */ | |
3181 | ||
3182 | static char *modsym (module, id, prefix) | |
3183 | register char *module, | |
3184 | *id; | |
3185 | char *prefix; | |
3186 | { | |
3187 | char buf1[BUFSIZ], | |
3188 | buf2[BUFSIZ], | |
3189 | buf3[BUFSIZ]; | |
3190 | register struct triple *t; | |
3191 | static char buffer[BUFSIZ]; | |
3192 | ||
3193 | if (module == NULLCP) | |
3194 | for (t = triples; t -> t_name; t++) | |
3195 | if (strcmp (t -> t_name, id) == 0) { | |
3196 | module = "UNIV"; | |
3197 | break; | |
3198 | } | |
3199 | ||
3200 | if (prefix) | |
3201 | modsym_aux (prefix, buf1); | |
3202 | modsym_aux (module ? module : mymodule, buf2); | |
3203 | modsym_aux (id, buf3); | |
3204 | if (prefix) | |
3205 | (void) sprintf (buffer, "%s_%s_%s", buf1, buf2, buf3); | |
3206 | else | |
3207 | (void) sprintf (buffer, "%s_%s", buf2, buf3); | |
3208 | ||
3209 | return buffer; | |
3210 | } | |
3211 | ||
3212 | ||
3213 | static modsym_aux (name, bp) | |
3214 | register char *name, | |
3215 | *bp; | |
3216 | { | |
3217 | register char c; | |
3218 | ||
3219 | while (c = *name++) | |
3220 | switch (c) { | |
3221 | case '-': | |
3222 | *bp++ = '_'; | |
3223 | *bp++ = '_'; | |
3224 | break; | |
3225 | ||
3226 | default: | |
3227 | *bp++ = c; | |
3228 | break; | |
3229 | } | |
3230 | ||
3231 | *bp = NULL; | |
3232 | } | |
3233 | ||
3234 | /* \f */ | |
3235 | ||
3236 | static char *gensym (s, a) | |
3237 | char *s, *a; | |
3238 | { | |
3239 | int i; | |
3240 | register char *p; | |
3241 | char buffer[BUFSIZ]; | |
3242 | static int cP = 0; | |
3243 | static int eP = 0; | |
3244 | static int mP = 0; | |
3245 | ||
3246 | switch (*s) { | |
3247 | case 'c': | |
3248 | i = cP++; | |
3249 | break; | |
3250 | case 'e': | |
3251 | i = eP++; | |
3252 | break; | |
3253 | case 'm': | |
3254 | i = mP++; | |
3255 | break; | |
3256 | ||
3257 | default: | |
3258 | myyerror ("unknown gensym argument \"%s\"", s); | |
3259 | /* NOTREACHED */ | |
3260 | } | |
3261 | if (a) | |
3262 | (void) sprintf (buffer, "%s_%s_%d[n_%s]", s, modulename, i, a); | |
3263 | else | |
3264 | (void) sprintf (buffer, "%s_%s_%d", s, modulename, i); | |
3265 | ||
3266 | if ((p = malloc ((unsigned) (strlen (buffer) + 11))) == NULLCP) | |
3267 | yyerror ("out of memory"); | |
3268 | ||
3269 | (void) strcpy (p, buffer); | |
3270 | return p; | |
3271 | } | |
3272 | ||
3273 | /* pepy compatible routines - you know how it is ... */ | |
3274 | init_new_file () | |
3275 | { | |
3276 | ; | |
3277 | } | |
3278 | ||
3279 | end_file () | |
3280 | { | |
3281 | ; | |
3282 | } | |
3283 | ||
3284 | static char *array (s, flg) | |
3285 | char *s; | |
3286 | int flg; | |
3287 | { | |
3288 | static char buf[BUFSIZ]; | |
3289 | char *p; | |
3290 | ||
3291 | if (!flg) return s; | |
3292 | ||
3293 | if (p = index (s, '[')) { | |
3294 | (void) sprintf (buf, "%*.*s[1]", p - s, p - s, s); | |
3295 | return buf; | |
3296 | } | |
3297 | return s; | |
3298 | } | |
3299 | ||
3300 | static void prime_default (yp, level) | |
3301 | YP yp; | |
3302 | int level; | |
3303 | { | |
3304 | switch (yp -> yp_code) { | |
3305 | case YP_BOOL: | |
3306 | printf ("%*s%s = %d;\n", level * 4, "", | |
3307 | SVAL (yp->yp_varexp), | |
3308 | val2int (yp -> yp_default) ? 1 : 0); | |
3309 | break; | |
3310 | ||
3311 | case YP_INT: | |
3312 | printf ("%*s%s = %d;\n", level * 4, "", | |
3313 | SVAL (yp -> yp_varexp), val2int (yp -> yp_default)); | |
3314 | break; | |
3315 | ||
3316 | case YP_INTLIST: | |
3317 | case YP_ENUMLIST: | |
3318 | printf ("%*s%s = %d;\n", level * 4, "", | |
3319 | SVAL (yp -> yp_varexp), dfl2int (yp)); | |
3320 | break; | |
3321 | ||
3322 | case YP_REAL: | |
3323 | printf ("%*s%s = %g;\n", level * 4, "", | |
3324 | SVAL (yp -> yp_varexp), | |
3325 | val2real (yp -> yp_default)); | |
3326 | ||
3327 | default: | |
3328 | break; | |
3329 | } | |
3330 | } |