kernel - Fix improper BUF_UNLOCK() with spinlock held
[dragonfly.git] / usr.bin / yacc / skeleton.c
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Paul Corbett.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#)skeleton.c       5.8 (Berkeley) 4/29/95
37  *
38  * $FreeBSD: src/usr.bin/yacc/skeleton.c,v 1.28.2.1 2001/07/19 05:46:39 peter Exp $
39  */
40
41 #include "defs.h"
42
43 /*  The definition of yysccsid in the banner should be replaced with    */
44 /*  a #pragma ident directive if the target C compiler supports         */
45 /*  #pragma ident directives.                                           */
46 /*                                                                      */
47 /*  If the skeleton is changed, the banner should be changed so that    */
48 /*  the altered version can be easily distinguished from the original.  */
49 /*                                                                      */
50 /*  The #defines included with the banner are there because they are    */
51 /*  useful in subsequent code.  The macros #defined in the header or    */
52 /*  the body either are not useful outside of semantic actions or       */
53 /*  are conditional.                                                    */
54
55 const char *banner[] = {
56     "#include <stdlib.h>",
57     "#include <string.h>",
58     "#define YYBYACC 1",
59     "#define YYMAJOR 1",
60     "#define YYMINOR 9",
61     "#define YYLEX yylex()",
62     "#define YYEMPTY -1",
63     "#define yyclearin (yychar=(YYEMPTY))",
64     "#define yyerrok (yyerrflag=0)",
65     "#define YYRECOVERING() (yyerrflag!=0)",
66     "static int yygrowstack(void);",
67     NULL
68 };
69
70 const char *tables[] = {
71     "extern const short yylhs[];",
72     "extern const short yylen[];",
73     "extern const short yydefred[];",
74     "extern const short yydgoto[];",
75     "extern const short yysindex[];",
76     "extern const short yyrindex[];",
77     "extern const short yygindex[];",
78     "extern const short yytable[];",
79     "extern const short yycheck[];",
80     "#if YYDEBUG",
81     "extern char *yyname[];",
82     "extern char *yyrule[];",
83     "#endif",
84     NULL
85 };
86
87 const char *header[] = {
88     "#if YYDEBUG",
89     "#include <stdio.h>",
90     "#endif",
91     "#ifdef YYSTACKSIZE",
92     "#undef YYMAXDEPTH",
93     "#define YYMAXDEPTH YYSTACKSIZE",
94     "#else",
95     "#ifdef YYMAXDEPTH",
96     "#define YYSTACKSIZE YYMAXDEPTH",
97     "#else",
98     "#define YYSTACKSIZE 10000",
99     "#define YYMAXDEPTH 10000",
100     "#endif",
101     "#endif",
102     "#define YYINITSTACKSIZE 200",
103     "int yydebug;",
104     "int yynerrs;",
105     "int yyerrflag;",
106     "int yychar;",
107     "short *yyssp;",
108     "YYSTYPE *yyvsp;",
109     "YYSTYPE yyval;",
110     "YYSTYPE yylval;",
111     "short *yyss;",
112     "short *yysslim;",
113     "YYSTYPE *yyvs;",
114     "int yystacksize;",
115     NULL
116 };
117
118 const char *body[] = {
119     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
120     "static int yygrowstack(void)",
121     "{",
122     "    int newsize, i;",
123     "    short *newss;",
124     "    YYSTYPE *newvs;",
125     "",
126     "    if ((newsize = yystacksize) == 0)",
127     "        newsize = YYINITSTACKSIZE;",
128     "    else if (newsize >= YYMAXDEPTH)",
129     "        return -1;",
130     "    else if ((newsize *= 2) > YYMAXDEPTH)",
131     "        newsize = YYMAXDEPTH;",
132     "    i = yyssp - yyss;",
133     "    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :",
134     "      (short *)malloc(newsize * sizeof *newss);",
135     "    if (newss == NULL)",
136     "        return -1;",
137     "    yyss = newss;",
138     "    yyssp = newss + i;",
139     "    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :",
140     "      (YYSTYPE *)malloc(newsize * sizeof *newvs);",
141     "    if (newvs == NULL)",
142     "        return -1;",
143     "    yyvs = newvs;",
144     "    yyvsp = newvs + i;",
145     "    yystacksize = newsize;",
146     "    yysslim = yyss + newsize - 1;",
147     "    return 0;",
148     "}",
149     "",
150     "#define YYABORT goto yyabort",
151     "#define YYREJECT goto yyabort",
152     "#define YYACCEPT goto yyaccept",
153     "#define YYERROR goto yyerrlab",
154     "",
155     "#ifndef YYPARSE_PARAM",
156     "#define YYPARSE_PARAM_ARG void",
157     "#define YYPARSE_PARAM_DECL",
158     "#else      /* YYPARSE_PARAM */",
159     "#ifndef YYPARSE_PARAM_TYPE",
160     "#define YYPARSE_PARAM_TYPE void *",
161     "#endif",
162     "#define YYPARSE_PARAM_ARG YYPARSE_PARAM_TYPE YYPARSE_PARAM",
163     "#define YYPARSE_PARAM_DECL",
164     "#endif     /* ! YYPARSE_PARAM */",
165     "",
166     "int",
167     "yyparse (YYPARSE_PARAM_ARG)",
168     "    YYPARSE_PARAM_DECL",
169     "{",
170     "    int yym, yyn, yystate;",
171     "#if YYDEBUG",
172     "    const char *yys;",
173     "",
174     "    if ((yys = getenv(\"YYDEBUG\")))",
175     "    {",
176     "        yyn = *yys;",
177     "        if (yyn >= '0' && yyn <= '9')",
178     "            yydebug = yyn - '0';",
179     "    }",
180     "#endif",
181     "",
182     "    yynerrs = 0;",
183     "    yyerrflag = 0;",
184     "    yychar = (-1);",
185     "",
186     "    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
187     "    yyssp = yyss;",
188     "    yyvsp = yyvs;",
189     "    *yyssp = yystate = 0;",
190     "",
191     "yyloop:",
192     "    if ((yyn = yydefred[yystate])) goto yyreduce;",
193     "    if (yychar < 0)",
194     "    {",
195     "        if ((yychar = yylex()) < 0) yychar = 0;",
196     "#if YYDEBUG",
197     "        if (yydebug)",
198     "        {",
199     "            yys = 0;",
200     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
201     "            if (!yys) yys = \"illegal-symbol\";",
202     "            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
203     "                    YYPREFIX, yystate, yychar, yys);",
204     "        }",
205     "#endif",
206     "    }",
207     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
208     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
209     "    {",
210     "#if YYDEBUG",
211     "        if (yydebug)",
212     "            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
213     "                    YYPREFIX, yystate, yytable[yyn]);",
214     "#endif",
215     "        if (yyssp >= yysslim && yygrowstack())",
216     "        {",
217     "            goto yyoverflow;",
218     "        }",
219     "        *++yyssp = yystate = yytable[yyn];",
220     "        *++yyvsp = yylval;",
221     "        yychar = (-1);",
222     "        if (yyerrflag > 0)  --yyerrflag;",
223     "        goto yyloop;",
224     "    }",
225     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
226     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
227     "    {",
228     "        yyn = yytable[yyn];",
229     "        goto yyreduce;",
230     "    }",
231     "    if (yyerrflag) goto yyinrecovery;",
232     "#if defined(lint) || defined(__GNUC__)",
233     "    goto yynewerror;",
234     "#endif",
235     "yynewerror:",
236     "    yyerror(\"syntax error\");",
237     "#if defined(lint) || defined(__GNUC__)",
238     "    goto yyerrlab;",
239     "#endif",
240     "yyerrlab:",
241     "    ++yynerrs;",
242     "yyinrecovery:",
243     "    if (yyerrflag < 3)",
244     "    {",
245     "        yyerrflag = 3;",
246     "        for (;;)",
247     "        {",
248     "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
249     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
250     "            {",
251     "#if YYDEBUG",
252     "                if (yydebug)",
253     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
254     " to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
255     "#endif",
256     "                if (yyssp >= yysslim && yygrowstack())",
257     "                {",
258     "                    goto yyoverflow;",
259     "                }",
260     "                *++yyssp = yystate = yytable[yyn];",
261     "                *++yyvsp = yylval;",
262     "                goto yyloop;",
263     "            }",
264     "            else",
265     "            {",
266     "#if YYDEBUG",
267     "                if (yydebug)",
268     "                    printf(\"%sdebug: error recovery discarding state %d\
269 \\n\",",
270     "                            YYPREFIX, *yyssp);",
271     "#endif",
272     "                if (yyssp <= yyss) goto yyabort;",
273     "                --yyssp;",
274     "                --yyvsp;",
275     "            }",
276     "        }",
277     "    }",
278     "    else",
279     "    {",
280     "        if (yychar == 0) goto yyabort;",
281     "#if YYDEBUG",
282     "        if (yydebug)",
283     "        {",
284     "            yys = 0;",
285     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
286     "            if (!yys) yys = \"illegal-symbol\";",
287     "            printf(\"%sdebug: state %d, error recovery discards token %d\
288  (%s)\\n\",",
289     "                    YYPREFIX, yystate, yychar, yys);",
290     "        }",
291     "#endif",
292     "        yychar = (-1);",
293     "        goto yyloop;",
294     "    }",
295     "yyreduce:",
296     "#if YYDEBUG",
297     "    if (yydebug)",
298     "        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
299     "                YYPREFIX, yystate, yyn, yyrule[yyn]);",
300     "#endif",
301     "    yym = yylen[yyn];",
302     "    if (yym)",
303     "        yyval = yyvsp[1-yym];",
304     "    else",
305     "        memset(&yyval, 0, sizeof yyval);",
306     "    yyval = yyvsp[1-yym];",
307     "    switch (yyn)",
308     "    {",
309     NULL
310 };
311
312 const char *trailer[] = {
313     "    }",
314     "    yyssp -= yym;",
315     "    yystate = *yyssp;",
316     "    yyvsp -= yym;",
317     "    yym = yylhs[yyn];",
318     "    if (yystate == 0 && yym == 0)",
319     "    {",
320     "#if YYDEBUG",
321     "        if (yydebug)",
322     "            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
323     " state %d\\n\", YYPREFIX, YYFINAL);",
324     "#endif",
325     "        yystate = YYFINAL;",
326     "        *++yyssp = YYFINAL;",
327     "        *++yyvsp = yyval;",
328     "        if (yychar < 0)",
329     "        {",
330     "            if ((yychar = yylex()) < 0) yychar = 0;",
331     "#if YYDEBUG",
332     "            if (yydebug)",
333     "            {",
334     "                yys = 0;",
335     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
336     "                if (!yys) yys = \"illegal-symbol\";",
337     "                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
338     "                        YYPREFIX, YYFINAL, yychar, yys);",
339     "            }",
340     "#endif",
341     "        }",
342     "        if (yychar == 0) goto yyaccept;",
343     "        goto yyloop;",
344     "    }",
345     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
346     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
347     "        yystate = yytable[yyn];",
348     "    else",
349     "        yystate = yydgoto[yym];",
350     "#if YYDEBUG",
351     "    if (yydebug)",
352     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
353     "to state %d\\n\", YYPREFIX, *yyssp, yystate);",
354     "#endif",
355     "    if (yyssp >= yysslim && yygrowstack())",
356     "    {",
357     "        goto yyoverflow;",
358     "    }",
359     "    *++yyssp = yystate;",
360     "    *++yyvsp = yyval;",
361     "    goto yyloop;",
362     "yyoverflow:",
363     "    yyerror(\"yacc stack overflow\");",
364     "yyabort:",
365     "    return (1);",
366     "yyaccept:",
367     "    return (0);",
368     "}",
369     NULL
370 };
371
372
373 void
374 write_section(const char **section)
375 {
376     int c;
377     int i;
378     const char *s;
379     FILE *f;
380
381     f = code_file;
382     for (i = 0; (s = section[i]); ++i)
383     {
384         ++outline;
385         while ((c = *s))
386         {
387             putc(c, f);
388             ++s;
389         }
390         putc('\n', f);
391     }
392 }