build/ccw_gen: stat the file in C instead of shelling out
[hvf.git] / cp / nucleus / config.l
blob393d4b2185c8a94004954d9d090e8380d86f14cf
1 /*
2  * (C) Copyright 2007-2011  Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
3  *
4  * This file is released under the GPLv2.  See the COPYING file for more
5  * details.
6  */
8 #include <util.h>
9 #include <ebcdic.h>
10 #include <lexer.h>
11 #include <sclp.h>
13 #include "config.tab.h"
15 static int __fill(struct lexer *lexer, int n)
17         char buf[CONFIG_LRECL];
18         int end, i;
19         int ret;
21         if (unlikely(!lexer->init)) {
22                 /* look up the config file */
23                 lexer->file = edf_lookup(lexer->fs, CONFIG_FILE_NAME, CONFIG_FILE_TYPE);
24                 if (IS_ERR(lexer->file))
25                         return PTR_ERR(lexer->file);
27                 if ((lexer->file->FST.LRECL != CONFIG_LRECL) ||
28                     (lexer->file->FST.RECFM != FSTDFIX))
29                         return -EINVAL;
31                 lexer->recoff = 0;
32                 lexer->buflen = sizeof(lexer->buf);
33                 lexer->filllen = 0;
35                 lexer->init = 1;
36         }
38         if (lexer->recoff) {
39                 lexer->filllen -= lexer->recoff;
40                 memmove(lexer->buf, &lexer->buf[lexer->recoff], lexer->filllen);
41                 lexer->recoff = 0;
42         }
44         assert(lexer->filllen < CONFIG_LRECL);
46         ret = edf_read_rec(lexer->file, buf, lexer->recno++);
47         if (ret)
48                 return ret;
50         ebcdic2ascii((u8*) buf, CONFIG_LRECL);
51         ascii2upper((u8*) buf, CONFIG_LRECL);
53         /* find the last non-blank */
54         for(end=CONFIG_LRECL-1; end>=0; end--)
55                 if (buf[end] != ' ')
56                         break;
58         for(i=0; (i<CONFIG_LRECL) && (i<end+1); i++)
59                 lexer->buf[lexer->filllen+i] = buf[i];
61         lexer->buf[lexer->filllen+i] = '\n';
62         lexer->filllen += i+1;
64         return 0;
67 #define YYFILL(n)       do { \
68                                 int ret; \
69                                 int off = cur - &lexer->buf[lexer->recoff]; \
70                                 ret = __fill(lexer, (n)); \
71                                 if (ret && !(lexer->filllen - lexer->recoff)) \
72                                         return ret; \
73                                 cur = &lexer->buf[off]; \
74                         } while(0)
75 #define YYRETURN(x)     do { \
76                                 lexer->recoff = cur - lexer->buf; \
77                                 return (x); \
78                         } while(0)
79 #define YYSKIP()        do { \
80                                 lexer->recoff = cur - lexer->buf; \
81                                 goto next; \
82                         } while(0)
83 int config_lex(void *data, void *yyval)
85         struct lexer *lexer = data;
86         YYSTYPE *val = yyval;
87         char *cur;
89 next:
90         cur = &lexer->buf[lexer->recoff];
92         /*!re2c
93                 re2c:define:YYCTYPE = "char";
94                 re2c:define:YYCURSOR = cur;
95                 re2c:define:YYLIMIT = &lexer->buf[lexer->filllen];
96                 "\n"            { YYRETURN(NLINE); }
97                 "*" [^\n]*      { YYRETURN(COMMENT); }
98                 "DIRECTORY"     { YYRETURN(DIRECTORY); }
99                 "OPERATOR"      { YYRETURN(OPERATOR); }
100                 "RDEV"          { YYRETURN(RDEV); }
101                 "LOGO"          { YYRETURN(LOGO); }
102                 "CONSOLE"       { YYRETURN(CONSOLE); }
103                 "USERID"        { YYRETURN(USERID); }
104                 "LOCAL"         { YYRETURN(LOCAL); }
105                 [0-9A-F]+       { hex(&lexer->buf[lexer->recoff], cur, &val->num);
106                                   YYRETURN(NUM);
107                                 }
108                 [A-Z]+          { int len;
109                                   len = cur-&lexer->buf[lexer->recoff];
110                                   memcpy(lexer->retbuf, &lexer->buf[lexer->recoff], len);
111                                   lexer->retbuf[len] = '\0';
112                                   val->ptr = strdup(lexer->retbuf, ZONE_NORMAL);
113                                   YYRETURN(WORD);
114                                 }
115                 [ \t]+          { YYSKIP(); }
116         */