xref: /OK3568_Linux_fs/external/xserver/hw/dmx/config/scanner.l (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* $XFree86$ */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2002 Red Hat Inc., Durham, North Carolina.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * All Rights Reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining
8*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
9*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
10*4882a593Smuzhiyun  * without limitation on the rights to use, copy, modify, merge,
11*4882a593Smuzhiyun  * publish, distribute, sublicense, and/or sell copies of the Software,
12*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so,
13*4882a593Smuzhiyun  * subject to the following conditions:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
16*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial
17*4882a593Smuzhiyun  * portions of the Software.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22*4882a593Smuzhiyun  * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
23*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26*4882a593Smuzhiyun  * SOFTWARE.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * Authors:
31*4882a593Smuzhiyun  *   Rickard E. (Rik) Faith <faith@redhat.com>
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun %{
36*4882a593Smuzhiyun #ifdef HAVE_DMX_CONFIG_H
37*4882a593Smuzhiyun #include <dmx-config.h>
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include "dmxparse.h"
41*4882a593Smuzhiyun #include "parser.h"
42*4882a593Smuzhiyun #include "os.h"
43*4882a593Smuzhiyun #include <string.h>
44*4882a593Smuzhiyun #include <stdlib.h>
45*4882a593Smuzhiyun #include <ctype.h>
46*4882a593Smuzhiyun static int getdimension(int token, const char *text, int leng);
47*4882a593Smuzhiyun static int getstring(int token, const char *text, int leng);
48*4882a593Smuzhiyun static int gettoken(int token, const char *text, int leng);
49*4882a593Smuzhiyun static int getcomment(int token, const char *text, int leng);
50*4882a593Smuzhiyun static int lineno = 1;
51*4882a593Smuzhiyun %}
52*4882a593Smuzhiyun %s OTHER
53*4882a593Smuzhiyun comment         #.*
54*4882a593Smuzhiyun word            ([[:alpha:]_/:\-\+\.\*][[:alnum:]_/:\-\+\.\*]+)
55*4882a593Smuzhiyun string          \"(([^\"\n])|\"\")*\"
56*4882a593Smuzhiyun badstring       \"(([^\"\n])|\"\")*
57*4882a593Smuzhiyun number          [[:digit:]x]+
58*4882a593Smuzhiyun dimension       [[:digit:]]+[[:blank:]]*x[[:blank:]]*[[:digit:]]+
59*4882a593Smuzhiyun offset          [+-][[:digit:]]+[[:blank:]]*[+-][[:blank:]]*[[:digit:]]+
60*4882a593Smuzhiyun origin          @[[:blank:]]*[[:digit:]]+[[:blank:]]*[[:blank:]]*x[[:digit:]]+
61*4882a593Smuzhiyun NL              \n
62*4882a593Smuzhiyun WS              [[:blank:]]+
63*4882a593Smuzhiyun %%
64*4882a593Smuzhiyun virtual         return gettoken(T_VIRTUAL, yytext, yyleng);
65*4882a593Smuzhiyun display         return gettoken(T_DISPLAY, yytext, yyleng);
66*4882a593Smuzhiyun wall            return gettoken(T_WALL, yytext, yyleng);
67*4882a593Smuzhiyun option          return gettoken(T_OPTION, yytext, yyleng);
68*4882a593Smuzhiyun param           return gettoken(T_PARAM, yytext, yyleng);
69*4882a593Smuzhiyun {dimension}     return getdimension(T_DIMENSION, yytext, yyleng);
70*4882a593Smuzhiyun {offset}        return getdimension(T_OFFSET, yytext+1, yyleng-1);
71*4882a593Smuzhiyun {origin}        return getdimension(T_ORIGIN, yytext+1, yyleng-1);
72*4882a593Smuzhiyun {word}          return getstring(T_STRING, yytext, yyleng);
73*4882a593Smuzhiyun {string}        return getstring(T_STRING, yytext+1, yyleng-2);
74*4882a593Smuzhiyun {NL}            ++lineno;
75*4882a593Smuzhiyun {WS}
76*4882a593Smuzhiyun \{              return gettoken(yytext[0], yytext, yyleng);
77*4882a593Smuzhiyun \}              return gettoken(yytext[0], yytext, yyleng);
78*4882a593Smuzhiyun \;              return gettoken(yytext[0], yytext, yyleng);
79*4882a593Smuzhiyun \/              return gettoken(yytext[0], yytext, yyleng);
80*4882a593Smuzhiyun ^{comment}      return getcomment(T_LINE_COMMENT, yytext, yyleng);
81*4882a593Smuzhiyun {comment}       return getcomment(T_COMMENT, yytext, yyleng);
82*4882a593Smuzhiyun .               return getstring(T_STRING, yytext, yyleng);
83*4882a593Smuzhiyun <<EOF>>         return 0;
84*4882a593Smuzhiyun %%
85*4882a593Smuzhiyun int yywrap(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun     (void) &yyunput;
88*4882a593Smuzhiyun     (void) &input;
89*4882a593Smuzhiyun     return 1;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun _X_NORETURN void yyerror(const char *message)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun     const char *pt, *end;
95*4882a593Smuzhiyun     struct _entry {
96*4882a593Smuzhiyun         const char *from;
97*4882a593Smuzhiyun         const char *to;
98*4882a593Smuzhiyun     } *entry, list[] = {
99*4882a593Smuzhiyun         { "T_VIRTUAL",      "\"virtual\"" },
100*4882a593Smuzhiyun         { "T_DISPLAY",      "\"display\"" },
101*4882a593Smuzhiyun         { "T_WALL",         "\"wall\"" },
102*4882a593Smuzhiyun         { "T_OPTION",       "\"option\"" },
103*4882a593Smuzhiyun         { "T_PARAM",        "\"param\"" },
104*4882a593Smuzhiyun         { "T_DIMENSION",    "dimension (e.g., 2x2 or 1024x768)" },
105*4882a593Smuzhiyun         { "T_OFFSET",       "display offset (e.g., +10-10)" },
106*4882a593Smuzhiyun         { "T_ORIGIN",       "tile origin (e.g., @1280x1024)" },
107*4882a593Smuzhiyun         { "T_STRING",       "string" },
108*4882a593Smuzhiyun         { "T_COMMENT",      "comment (e.g., #...)" },
109*4882a593Smuzhiyun         { "T_LINE_COMMENT", "comment (e.g., #...)" },
110*4882a593Smuzhiyun         { NULL, NULL }
111*4882a593Smuzhiyun     };
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun     fprintf(stderr, "parse error on line %d at token \"%*.*s\"\n",
114*4882a593Smuzhiyun             lineno, (int)yyleng, (int)yyleng, yytext);
115*4882a593Smuzhiyun     end = message + strlen(message);
116*4882a593Smuzhiyun     for (pt = message; *pt; pt++) {
117*4882a593Smuzhiyun         if (pt[0] == 'T' && pt[1] == '_') {
118*4882a593Smuzhiyun             const char *next = strchr(pt, ' ');
119*4882a593Smuzhiyun             if (!next || !*next) next = strchr(pt, '\0');
120*4882a593Smuzhiyun             if (!next) goto bail;
121*4882a593Smuzhiyun             --next;
122*4882a593Smuzhiyun             if (next-pt == 1 && next[1]
123*4882a593Smuzhiyun                 && next[2] == '\'' && next[3] == '\'') {
124*4882a593Smuzhiyun                 fprintf(stderr, "\"%c\"", next[1]);
125*4882a593Smuzhiyun                 pt += 4;
126*4882a593Smuzhiyun                 goto cnt;
127*4882a593Smuzhiyun             }
128*4882a593Smuzhiyun             for (entry = list; entry->from; ++entry) {
129*4882a593Smuzhiyun                 if (!strncmp(entry->from, pt, strlen(entry->from))) {
130*4882a593Smuzhiyun                     fprintf(stderr, "%s", entry->to);
131*4882a593Smuzhiyun                     pt = next;
132*4882a593Smuzhiyun                     goto cnt;
133*4882a593Smuzhiyun                 }
134*4882a593Smuzhiyun             }
135*4882a593Smuzhiyun         } else if (end-pt >= 5 && pt[0] == '\'' && pt[1] == '\'' && pt[3]
136*4882a593Smuzhiyun                    && pt[4] == '\'' && pt[5] == '\'') {
137*4882a593Smuzhiyun             fprintf(stderr, "\"%c\"", pt[3]);
138*4882a593Smuzhiyun             pt += 5;
139*4882a593Smuzhiyun         } else if (end-pt >= 3 && pt[0] == '\'' && pt[1] && pt[2] == '\'') {
140*4882a593Smuzhiyun             fprintf(stderr, "\"%c\"", pt[1]);
141*4882a593Smuzhiyun             pt += 3;
142*4882a593Smuzhiyun         }
143*4882a593Smuzhiyun       bail:
144*4882a593Smuzhiyun         putc(*pt, stderr);
145*4882a593Smuzhiyun       cnt:
146*4882a593Smuzhiyun         ;
147*4882a593Smuzhiyun     }
148*4882a593Smuzhiyun     fprintf(stderr, "\n");
149*4882a593Smuzhiyun     exit( 1 );
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static int getdimension(int token, const char *text, int leng)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun     char *endptr;
155*4882a593Smuzhiyun     char *tmp = dmxConfigAlloc(leng+1);
156*4882a593Smuzhiyun     int  x, y;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun     strlcpy(tmp, text, leng+1);
159*4882a593Smuzhiyun     x = strtol(tmp, &endptr, 10);
160*4882a593Smuzhiyun     while (*endptr && !isdigit(*endptr)) ++endptr;
161*4882a593Smuzhiyun     y = strtol(endptr, NULL, 10);
162*4882a593Smuzhiyun     dmxConfigFree(tmp);
163*4882a593Smuzhiyun     yylval.pair = dmxConfigCreatePair(token, lineno, NULL, x, y, 1, 1);
164*4882a593Smuzhiyun     return token;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun static int getstring(int token, const char *text, int leng)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun     yylval.string = dmxConfigCreateString(token, lineno, NULL,
170*4882a593Smuzhiyun                                           dmxConfigCopyString(leng ? text : "",
171*4882a593Smuzhiyun                                                               leng));
172*4882a593Smuzhiyun     return token;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun static int gettoken(int token, const char *text, int leng)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun     yylval.token = dmxConfigCreateToken(token, lineno, NULL);
178*4882a593Smuzhiyun     return token;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun static int getcomment(int token, const char *text, int leng)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun     yylval.comment = dmxConfigCreateComment(token, lineno,
184*4882a593Smuzhiyun                                             dmxConfigCopyString(text + 1,
185*4882a593Smuzhiyun                                                                 leng - 1));
186*4882a593Smuzhiyun     return token;
187*4882a593Smuzhiyun }
188