Home | History | Annotate | Line # | Download | only in tests
      1      1.1  christos /*
      2      1.1  christos  * This file is part of flex.
      3      1.1  christos  *
      4      1.1  christos  * Redistribution and use in source and binary forms, with or without
      5      1.1  christos  * modification, are permitted provided that the following conditions
      6      1.1  christos  * are met:
      7      1.1  christos  *
      8      1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  christos  *    documentation and/or other materials provided with the distribution.
     13      1.1  christos  *
     14      1.1  christos  * Neither the name of the University nor the names of its contributors
     15      1.1  christos  * may be used to endorse or promote products derived from this software
     16      1.1  christos  * without specific prior written permission.
     17      1.1  christos  *
     18      1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     19      1.1  christos  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     20      1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21      1.1  christos  * PURPOSE.
     22      1.1  christos  */
     23      1.1  christos 
     24      1.1  christos %{
     25      1.1  christos /* A file to build "scanner.c". */
     26      1.1  christos /* This tests that we can use "yyextra".
     27      1.1  christos    We buffer all input into a growable array, then print it.
     28      1.1  christos    We run diff on the input and output.
     29      1.1  christos */
     30      1.1  christos 
     31      1.1  christos #include <stdio.h>
     32      1.1  christos #include <stdlib.h>
     33      1.1  christos #include "config.h"
     34      1.1  christos 
     35      1.1  christos 
     36      1.1  christos /* We'll store the entire input in this buffer, growing as necessary. */
     37      1.1  christos struct Buffer {
     38      1.1  christos     int  curr_len;
     39      1.1  christos     int  max_len;
     40      1.1  christos     int  grow_len;
     41      1.1  christos     char * data;
     42      1.1  christos };
     43      1.1  christos #define YY_EXTRA_TYPE struct Buffer *
     44      1.1  christos 
     45      1.1  christos /* Save char into junk array at next position. */
     46      1.1  christos static void append_char (char c,  yyscan_t  scanner );
     47      1.1  christos 
     48      1.1  christos %}
     49      1.1  christos 
     50      1.1  christos %option 8bit prefix="test"
     51      1.1  christos %option nounput nomain noyywrap nodefault noinput
     52      1.1  christos %option warn
     53      1.1  christos %option reentrant
     54      1.1  christos 
     55      1.1  christos 
     56      1.1  christos %%
     57      1.1  christos 
     58      1.1  christos .|\r|\n  { append_char (yytext[0],yyscanner); }
     59      1.1  christos 
     60      1.1  christos %%
     61      1.1  christos 
     62      1.1  christos int main(void);
     63      1.1  christos 
     64      1.1  christos int
     65  1.1.1.2  christos main (void)
     66      1.1  christos {
     67      1.1  christos     yyscan_t  scanner;
     68      1.1  christos     struct Buffer * buf;
     69      1.1  christos     int i;
     70      1.1  christos 
     71  1.1.1.2  christos     buf = malloc(sizeof(struct Buffer));
     72      1.1  christos     buf->curr_len =0;
     73      1.1  christos     buf->max_len = 4;
     74      1.1  christos     buf->grow_len = 100;
     75  1.1.1.2  christos     buf->data = malloc((size_t) buf->max_len);
     76      1.1  christos 
     77      1.1  christos     testlex_init(&scanner);
     78      1.1  christos     testset_in( stdin, scanner);
     79      1.1  christos     testset_out( stdout, scanner);
     80      1.1  christos     testset_extra( buf, scanner );
     81      1.1  christos     testlex(scanner);
     82      1.1  christos 
     83      1.1  christos         buf = testget_extra(scanner);
     84      1.1  christos         for(i=0; i < buf->curr_len; i++)
     85      1.1  christos             fputc( buf->data[i], stdout );
     86      1.1  christos         free( buf->data);
     87      1.1  christos         free( buf);
     88      1.1  christos 
     89      1.1  christos     testlex_destroy(scanner);
     90      1.1  christos     return 0;
     91      1.1  christos }
     92      1.1  christos 
     93      1.1  christos /* Save char into junk array at next position. */
     94      1.1  christos static void append_char (char c,  yyscan_t  scanner )
     95      1.1  christos {
     96      1.1  christos     struct Buffer *buf, *new_buf;
     97      1.1  christos     buf = testget_extra(scanner);
     98      1.1  christos 
     99      1.1  christos     /* Grow buffer if necessary. */
    100      1.1  christos 
    101      1.1  christos     if( buf->curr_len >= buf->max_len )
    102      1.1  christos     {
    103  1.1.1.2  christos         new_buf = malloc(sizeof(struct Buffer));
    104      1.1  christos         new_buf->max_len = buf->max_len + buf->grow_len;
    105      1.1  christos         new_buf->grow_len = buf->grow_len;
    106  1.1.1.2  christos         new_buf->data = malloc((size_t) new_buf->max_len);
    107      1.1  christos         for( new_buf->curr_len = 0;
    108      1.1  christos              new_buf->curr_len < buf->curr_len;
    109      1.1  christos              new_buf->curr_len++ )
    110      1.1  christos         {
    111      1.1  christos             new_buf->data[ new_buf->curr_len] = buf->data [ new_buf->curr_len];
    112      1.1  christos         }
    113      1.1  christos         free( buf->data );
    114      1.1  christos         free( buf );
    115      1.1  christos         buf = new_buf;
    116      1.1  christos         testset_extra( buf, scanner );
    117      1.1  christos     }
    118      1.1  christos 
    119      1.1  christos 
    120      1.1  christos     buf->data[ buf->curr_len++ ] = c;
    121      1.1  christos }
    122