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