xref: /minix3/external/bsd/flex/dist/tests/test-mem-r/scanner.l (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*357f1050SThomas Veerman /*
2*357f1050SThomas Veerman  * This file is part of flex.
3*357f1050SThomas Veerman  *
4*357f1050SThomas Veerman  * Redistribution and use in source and binary forms, with or without
5*357f1050SThomas Veerman  * modification, are permitted provided that the following conditions
6*357f1050SThomas Veerman  * are met:
7*357f1050SThomas Veerman  *
8*357f1050SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
9*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
10*357f1050SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
11*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
12*357f1050SThomas Veerman  *    documentation and/or other materials provided with the distribution.
13*357f1050SThomas Veerman  *
14*357f1050SThomas Veerman  * Neither the name of the University nor the names of its contributors
15*357f1050SThomas Veerman  * may be used to endorse or promote products derived from this software
16*357f1050SThomas Veerman  * without specific prior written permission.
17*357f1050SThomas Veerman  *
18*357f1050SThomas Veerman  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19*357f1050SThomas Veerman  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20*357f1050SThomas Veerman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*357f1050SThomas Veerman  * PURPOSE.
22*357f1050SThomas Veerman  */
23*357f1050SThomas Veerman 
24*357f1050SThomas Veerman %{
25*357f1050SThomas Veerman /* A template scanner file to build "scanner.c".
26*357f1050SThomas Veerman  * The whole idea is to cause memory realloc by
27*357f1050SThomas Veerman  *  1. pushing a lot on the condition stack, and
28*357f1050SThomas Veerman  *  2. eating input greater than YY_BUF_SIZE
29*357f1050SThomas Veerman  */
30*357f1050SThomas Veerman #include <stdio.h>
31*357f1050SThomas Veerman #include <stdlib.h>
32*357f1050SThomas Veerman #include "config.h"
33*357f1050SThomas Veerman 
34*357f1050SThomas Veerman /* Insanely small read buffer. This pretty much guarantees at least one realloc. */
35*357f1050SThomas Veerman #ifdef YY_BUF_SIZE
36*357f1050SThomas Veerman #undef YY_BUF_SIZE
37*357f1050SThomas Veerman #endif
38*357f1050SThomas Veerman #define YY_BUF_SIZE 8
39*357f1050SThomas Veerman 
40*357f1050SThomas Veerman %}
41*357f1050SThomas Veerman 
42*357f1050SThomas Veerman %option 8bit outfile="scanner.c" prefix="test"
43*357f1050SThomas Veerman %option nounput nomain noyywrap
44*357f1050SThomas Veerman %option warn stack nodefault reentrant
45*357f1050SThomas Veerman %option noyyalloc noyyrealloc noyyfree
46*357f1050SThomas Veerman 
47*357f1050SThomas Veerman %x parens
48*357f1050SThomas Veerman 
49*357f1050SThomas Veerman %%
50*357f1050SThomas Veerman 
51*357f1050SThomas Veerman <INITIAL>{
52*357f1050SThomas Veerman "("         { printf("yy_push_state(parens)\n"); yy_push_state(parens,yyscanner); }
53*357f1050SThomas Veerman len=[0-9]+  { printf("About read token where %s\n",yytext); }
54*357f1050SThomas Veerman 0+          { }
55*357f1050SThomas Veerman .|\n        { }
56*357f1050SThomas Veerman }
57*357f1050SThomas Veerman 
58*357f1050SThomas Veerman <parens>{
59*357f1050SThomas Veerman "("         { printf("yy_push_state(parens)\n"); yy_push_state(parens,yyscanner); }
60*357f1050SThomas Veerman ")"         { printf("yy_pop_state()\n");yy_pop_state(yyscanner);}
61*357f1050SThomas Veerman [^()\n]+     { }
62*357f1050SThomas Veerman .|\n        { }
63*357f1050SThomas Veerman }
64*357f1050SThomas Veerman 
65*357f1050SThomas Veerman %%
66*357f1050SThomas Veerman /* total memory allocated */
67*357f1050SThomas Veerman static size_t total_mem=0;
68*357f1050SThomas Veerman 
69*357f1050SThomas Veerman /* track the amount of memory for ptr. */
70*357f1050SThomas Veerman struct memsz {
71*357f1050SThomas Veerman     void* p;
72*357f1050SThomas Veerman     size_t sz;
73*357f1050SThomas Veerman };
74*357f1050SThomas Veerman 
75*357f1050SThomas Veerman static struct memsz * ptrs=0;  /* Array of pairs. */
76*357f1050SThomas Veerman static int nptrs=0; /* Number of pairs in array. */
77*357f1050SThomas Veerman static int arrsz=0; /* Capacity of array. */
78*357f1050SThomas Veerman 
dump_mem(FILE * fp)79*357f1050SThomas Veerman static void dump_mem(FILE* fp){
80*357f1050SThomas Veerman     int i;
81*357f1050SThomas Veerman     fprintf(fp,"\tptrs[%d] = {", nptrs);
82*357f1050SThomas Veerman     for (i=0; i < arrsz; i++)
83*357f1050SThomas Veerman         fprintf(fp," {%#lx,%ld},", (long)ptrs[i].p, (long)ptrs[i].sz);
84*357f1050SThomas Veerman 
85*357f1050SThomas Veerman     fprintf(fp,"}\n");
86*357f1050SThomas Veerman }
87*357f1050SThomas Veerman 
yyalloc(yy_size_t n,void * yyscanner)88*357f1050SThomas Veerman void * yyalloc(yy_size_t n , void* yyscanner)
89*357f1050SThomas Veerman {
90*357f1050SThomas Veerman     void * p;
91*357f1050SThomas Veerman     int i;
92*357f1050SThomas Veerman 
93*357f1050SThomas Veerman     total_mem += n;
94*357f1050SThomas Veerman     p = (void*)malloc(n);
95*357f1050SThomas Veerman 
96*357f1050SThomas Veerman     if( nptrs >= arrsz){
97*357f1050SThomas Veerman         /* increase array size by 1 */
98*357f1050SThomas Veerman         arrsz++;
99*357f1050SThomas Veerman         ptrs = (struct memsz*)realloc( ptrs, arrsz * sizeof(struct memsz));
100*357f1050SThomas Veerman         ptrs[nptrs].p = 0;
101*357f1050SThomas Veerman         ptrs[nptrs].sz = 0;
102*357f1050SThomas Veerman     }
103*357f1050SThomas Veerman 
104*357f1050SThomas Veerman     /* find a null slot */
105*357f1050SThomas Veerman     for(i=0; i < arrsz ; i++)
106*357f1050SThomas Veerman         if (ptrs[i].p == 0) {
107*357f1050SThomas Veerman             ptrs[i].p = p;
108*357f1050SThomas Veerman             ptrs[i].sz = n;
109*357f1050SThomas Veerman         }
110*357f1050SThomas Veerman 
111*357f1050SThomas Veerman     nptrs++;
112*357f1050SThomas Veerman     printf("yyflex_alloc(%8ld) total=%8ld return=%#10lx\n",(long)n,(long)total_mem,(long)p);
113*357f1050SThomas Veerman     dump_mem(stdout);
114*357f1050SThomas Veerman     return p;
115*357f1050SThomas Veerman }
116*357f1050SThomas Veerman 
yyrealloc(void * p,yy_size_t n,void * yyscanner)117*357f1050SThomas Veerman void * yyrealloc(void* p, yy_size_t n , void* yyscanner)
118*357f1050SThomas Veerman {
119*357f1050SThomas Veerman     int i;
120*357f1050SThomas Veerman     for (i=0; i < arrsz; i++)
121*357f1050SThomas Veerman         if ( ptrs[i].p == p){
122*357f1050SThomas Veerman             total_mem -= ptrs[i].sz;
123*357f1050SThomas Veerman             total_mem += n;
124*357f1050SThomas Veerman             ptrs[i].p = (void*)realloc(p,n);
125*357f1050SThomas Veerman             ptrs[i].sz = n;
126*357f1050SThomas Veerman 
127*357f1050SThomas Veerman             printf("yyflex_realloc(%#10lx,%8ld) total=%8ld return=%8lx\n",
128*357f1050SThomas Veerman                     (long)p,(long)n,(long)total_mem,(long)ptrs[i].p);
129*357f1050SThomas Veerman             dump_mem(stdout);
130*357f1050SThomas Veerman             return ptrs[i].p;
131*357f1050SThomas Veerman         }
132*357f1050SThomas Veerman 
133*357f1050SThomas Veerman     fprintf(stderr,"ERROR: yyflex_realloc could not locate pointer %#lx.\n",(long)p);
134*357f1050SThomas Veerman     dump_mem(stdout);
135*357f1050SThomas Veerman     exit(1);
136*357f1050SThomas Veerman }
137*357f1050SThomas Veerman 
yyfree(void * p,void * yyscanner)138*357f1050SThomas Veerman void yyfree(void* p , void* yyscanner)
139*357f1050SThomas Veerman {
140*357f1050SThomas Veerman     int i;
141*357f1050SThomas Veerman     for (i=0; i < arrsz; i++)
142*357f1050SThomas Veerman         if ( ptrs[i].p == p){
143*357f1050SThomas Veerman             total_mem -= ptrs[i].sz;
144*357f1050SThomas Veerman             free(p);
145*357f1050SThomas Veerman             ptrs[i].p = 0;
146*357f1050SThomas Veerman             ptrs[i].sz = 0;
147*357f1050SThomas Veerman             nptrs--;
148*357f1050SThomas Veerman             printf("yyflex_free(%#10lx) total=%8ld\n",(long)p,(long)total_mem);
149*357f1050SThomas Veerman             dump_mem(stdout);
150*357f1050SThomas Veerman             return;
151*357f1050SThomas Veerman         }
152*357f1050SThomas Veerman 
153*357f1050SThomas Veerman     fprintf(stderr,"ERROR: yyflex_free could not locate pointer %#lx.\n",(long)p);
154*357f1050SThomas Veerman     dump_mem(stdout);
155*357f1050SThomas Veerman     exit(1);
156*357f1050SThomas Veerman }
157*357f1050SThomas Veerman 
158*357f1050SThomas Veerman int main(void);
159*357f1050SThomas Veerman 
160*357f1050SThomas Veerman int
main()161*357f1050SThomas Veerman main ()
162*357f1050SThomas Veerman {
163*357f1050SThomas Veerman     yyscan_t scanner;
164*357f1050SThomas Veerman     arrsz = 1;
165*357f1050SThomas Veerman     ptrs  = (struct memsz*)calloc(1,sizeof(struct memsz));
166*357f1050SThomas Veerman     nptrs = 0;
167*357f1050SThomas Veerman 
168*357f1050SThomas Veerman     yylex_init(&scanner);
169*357f1050SThomas Veerman     yyset_in(stdin,scanner);
170*357f1050SThomas Veerman     yyset_out(stdout,scanner);
171*357f1050SThomas Veerman     yylex(scanner);
172*357f1050SThomas Veerman     yylex_destroy(scanner);
173*357f1050SThomas Veerman     free(ptrs);
174*357f1050SThomas Veerman 
175*357f1050SThomas Veerman     if ( nptrs > 0 || total_mem > 0){
176*357f1050SThomas Veerman         fprintf(stderr,"Oops. Looks like a memory leak: nptrs=%d, unfreed memory=%ld\n",nptrs,(long)total_mem);
177*357f1050SThomas Veerman         exit(1);
178*357f1050SThomas Veerman     }
179*357f1050SThomas Veerman     printf("TEST RETURNING OK.\n");
180*357f1050SThomas Veerman     return 0;
181*357f1050SThomas Veerman }
182