xref: /OK3568_Linux_fs/external/xserver/hw/dmx/config/dmxcompat.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2002 Red Hat Inc., Durham, North Carolina.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining
7*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
8*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun  * without limitation on the rights to use, copy, modify, merge,
10*4882a593Smuzhiyun  * publish, distribute, sublicense, and/or sell copies of the Software,
11*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so,
12*4882a593Smuzhiyun  * subject to the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial
16*4882a593Smuzhiyun  * portions of the Software.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21*4882a593Smuzhiyun  * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25*4882a593Smuzhiyun  * SOFTWARE.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Authors:
30*4882a593Smuzhiyun  *   Rickard E. (Rik) Faith <faith@redhat.com>
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /** \file
34*4882a593Smuzhiyun  * This file provides some compatibility support for reading VDL files
35*4882a593Smuzhiyun  * that are used by xmovie
36*4882a593Smuzhiyun  * (http://www.llnl.gov/icc/sdd/img/xmovie/xmovie.shtml).
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * This file is not used by the DMX server.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #ifdef HAVE_DMX_CONFIG_H
42*4882a593Smuzhiyun #include <dmx-config.h>
43*4882a593Smuzhiyun #endif
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include "os.h"
46*4882a593Smuzhiyun #include "dmxconfig.h"
47*4882a593Smuzhiyun #include "dmxparse.h"
48*4882a593Smuzhiyun #include "dmxcompat.h"
49*4882a593Smuzhiyun #include "parser.h"
50*4882a593Smuzhiyun #include <stdio.h>
51*4882a593Smuzhiyun #include <stdlib.h>
52*4882a593Smuzhiyun #include <string.h>
53*4882a593Smuzhiyun #include <ctype.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static int
dmxVDLReadLine(FILE * str,char * buf,int len)56*4882a593Smuzhiyun dmxVDLReadLine(FILE * str, char *buf, int len)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun     if (fgets(buf, len, str))
59*4882a593Smuzhiyun         return strlen(buf);
60*4882a593Smuzhiyun     return 0;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static int
dmxVDLCount(const char * buf)64*4882a593Smuzhiyun dmxVDLCount(const char *buf)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun     return strtol(buf, NULL, 10);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static void
dmxVDLVirtualEntry(const char * buf,char * name,int * len,int * x,int * y)70*4882a593Smuzhiyun dmxVDLVirtualEntry(const char *buf, char *name, int *len, int *x, int *y)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun     char *end;
73*4882a593Smuzhiyun     const char *s;
74*4882a593Smuzhiyun     char *d;
75*4882a593Smuzhiyun     int start;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun     *x = strtol(buf, &end, 10);
78*4882a593Smuzhiyun     *y = strtol(end, &end, 10);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun     for (s = end, d = name, start = 1; *s && *s != '['; ++s) {
81*4882a593Smuzhiyun         if (start && isspace(*s))
82*4882a593Smuzhiyun             continue;
83*4882a593Smuzhiyun         *d++ = *s;
84*4882a593Smuzhiyun         start = 0;
85*4882a593Smuzhiyun     }
86*4882a593Smuzhiyun     *d = '\0';
87*4882a593Smuzhiyun     while (d > name && isspace(d[-1]))
88*4882a593Smuzhiyun         *--d = '\0';            /* remove trailing space */
89*4882a593Smuzhiyun     *len = strlen(name);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun static void
dmxVDLDisplayEntry(const char * buf,char * name,int * len,int * x,int * y,int * xoff,int * yoff,int * xorig,int * yorig)93*4882a593Smuzhiyun dmxVDLDisplayEntry(const char *buf,
94*4882a593Smuzhiyun                    char *name, int *len,
95*4882a593Smuzhiyun                    int *x, int *y, int *xoff, int *yoff, int *xorig, int *yorig)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun     const char *pt;
98*4882a593Smuzhiyun     char *end;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun     pt = strchr(buf, ' ');
101*4882a593Smuzhiyun     strlcpy(name, buf, 1 + pt - buf);
102*4882a593Smuzhiyun     *len = strlen(name);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun     *x = strtol(pt, &end, 10);
105*4882a593Smuzhiyun     *y = strtol(end, &end, 10);
106*4882a593Smuzhiyun     *xorig = strtol(end, &end, 10);
107*4882a593Smuzhiyun     *yorig = strtol(end, &end, 10);
108*4882a593Smuzhiyun     *xoff = strtol(end, &end, 10);
109*4882a593Smuzhiyun     *yoff = strtol(end, NULL, 10);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /** Read from the VDL format \a filename and return a newly allocated \a
113*4882a593Smuzhiyun  * DMXConfigEntryPtr */
114*4882a593Smuzhiyun DMXConfigEntryPtr
dmxVDLRead(const char * filename)115*4882a593Smuzhiyun dmxVDLRead(const char *filename)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun     FILE *str;
118*4882a593Smuzhiyun     char buf[2048];             /* RATS: Use ok */
119*4882a593Smuzhiyun     char *pt;
120*4882a593Smuzhiyun     int lineno = 0;
121*4882a593Smuzhiyun     DMXConfigEntryPtr entry = NULL;
122*4882a593Smuzhiyun     DMXConfigVirtualPtr virtual = NULL;
123*4882a593Smuzhiyun     DMXConfigSubPtr sub = NULL;
124*4882a593Smuzhiyun     DMXConfigDisplayPtr display = NULL;
125*4882a593Smuzhiyun     DMXConfigFullDimPtr fdim = NULL;
126*4882a593Smuzhiyun     int dcount = 0;
127*4882a593Smuzhiyun     int icount = 0;
128*4882a593Smuzhiyun     int x, y, xoff, yoff, xorig, yorig;
129*4882a593Smuzhiyun     char name[2048];            /* RATS: Use ok */
130*4882a593Smuzhiyun     const char *tmp;
131*4882a593Smuzhiyun     int len;
132*4882a593Smuzhiyun     enum {
133*4882a593Smuzhiyun         simulateFlag,
134*4882a593Smuzhiyun         virtualCount,
135*4882a593Smuzhiyun         virtualEntry,
136*4882a593Smuzhiyun         displayCount,
137*4882a593Smuzhiyun         displayEntry,
138*4882a593Smuzhiyun         ignoreCount,
139*4882a593Smuzhiyun         ignoreEntry
140*4882a593Smuzhiyun     } state = simulateFlag;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun     if (!filename)
143*4882a593Smuzhiyun         str = stdin;
144*4882a593Smuzhiyun     else
145*4882a593Smuzhiyun         str = fopen(filename, "r");
146*4882a593Smuzhiyun     if (!str)
147*4882a593Smuzhiyun         return NULL;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun     while (dmxVDLReadLine(str, buf, sizeof(buf))) {
150*4882a593Smuzhiyun         DMXConfigCommentPtr comment = NULL;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun         ++lineno;
153*4882a593Smuzhiyun         for (pt = buf; *pt; pt++)
154*4882a593Smuzhiyun             if (*pt == '\r' || *pt == '\n') {
155*4882a593Smuzhiyun                 *pt = '\0';
156*4882a593Smuzhiyun                 break;
157*4882a593Smuzhiyun             }
158*4882a593Smuzhiyun         if (buf[0] == '#') {
159*4882a593Smuzhiyun             tmp = dmxConfigCopyString(buf + 1, strlen(buf + 1));
160*4882a593Smuzhiyun             comment = dmxConfigCreateComment(T_COMMENT, lineno, tmp);
161*4882a593Smuzhiyun             entry = dmxConfigAddEntry(entry, dmxConfigComment, comment, NULL);
162*4882a593Smuzhiyun             continue;
163*4882a593Smuzhiyun         }
164*4882a593Smuzhiyun         switch (state) {
165*4882a593Smuzhiyun         case simulateFlag:
166*4882a593Smuzhiyun             state = virtualCount;
167*4882a593Smuzhiyun             break;
168*4882a593Smuzhiyun         case virtualCount:
169*4882a593Smuzhiyun             state = virtualEntry;
170*4882a593Smuzhiyun             break;
171*4882a593Smuzhiyun         case virtualEntry:
172*4882a593Smuzhiyun             len = sizeof(name);
173*4882a593Smuzhiyun             dmxVDLVirtualEntry(buf, name, &len, &x, &y);
174*4882a593Smuzhiyun             tmp = dmxConfigCopyString(name, len);
175*4882a593Smuzhiyun             virtual = dmxConfigCreateVirtual(NULL,
176*4882a593Smuzhiyun                                              dmxConfigCreateString(T_STRING,
177*4882a593Smuzhiyun                                                                    lineno,
178*4882a593Smuzhiyun                                                                    NULL,
179*4882a593Smuzhiyun                                                                    tmp),
180*4882a593Smuzhiyun                                              dmxConfigCreatePair(T_DIMENSION,
181*4882a593Smuzhiyun                                                                  lineno,
182*4882a593Smuzhiyun                                                                  NULL,
183*4882a593Smuzhiyun                                                                  x, y, 0, 0),
184*4882a593Smuzhiyun                                              NULL, NULL, NULL);
185*4882a593Smuzhiyun             state = displayCount;
186*4882a593Smuzhiyun             break;
187*4882a593Smuzhiyun         case displayCount:
188*4882a593Smuzhiyun             dcount = dmxVDLCount(buf);
189*4882a593Smuzhiyun             state = displayEntry;
190*4882a593Smuzhiyun             break;
191*4882a593Smuzhiyun         case displayEntry:
192*4882a593Smuzhiyun             dmxVDLDisplayEntry(buf, name, &len, &x, &y, &xoff, &yoff,
193*4882a593Smuzhiyun                                &xorig, &yorig);
194*4882a593Smuzhiyun             tmp = dmxConfigCopyString(name, len);
195*4882a593Smuzhiyun             fdim =
196*4882a593Smuzhiyun                 dmxConfigCreateFullDim(dmxConfigCreatePartDim
197*4882a593Smuzhiyun                                        (dmxConfigCreatePair
198*4882a593Smuzhiyun                                         (T_DIMENSION, lineno, NULL, x, y, 0, 0),
199*4882a593Smuzhiyun                                         dmxConfigCreatePair(T_OFFSET, lineno,
200*4882a593Smuzhiyun                                                             NULL, xoff, yoff,
201*4882a593Smuzhiyun                                                             xoff, yoff)), NULL);
202*4882a593Smuzhiyun             display =
203*4882a593Smuzhiyun                 dmxConfigCreateDisplay(NULL,
204*4882a593Smuzhiyun                                        dmxConfigCreateString(T_STRING, lineno,
205*4882a593Smuzhiyun                                                              NULL, tmp), fdim,
206*4882a593Smuzhiyun                                        dmxConfigCreatePair(T_ORIGIN, lineno,
207*4882a593Smuzhiyun                                                            NULL, xorig, yorig,
208*4882a593Smuzhiyun                                                            0, 0), NULL);
209*4882a593Smuzhiyun             sub = dmxConfigAddSub(sub, dmxConfigSubDisplay(display));
210*4882a593Smuzhiyun             if (!--dcount) {
211*4882a593Smuzhiyun                 state = ignoreCount;
212*4882a593Smuzhiyun                 virtual->subentry = sub;
213*4882a593Smuzhiyun                 entry = dmxConfigAddEntry(entry,
214*4882a593Smuzhiyun                                           dmxConfigVirtual, NULL, virtual);
215*4882a593Smuzhiyun                 virtual = NULL;
216*4882a593Smuzhiyun                 sub = NULL;
217*4882a593Smuzhiyun             }
218*4882a593Smuzhiyun             break;
219*4882a593Smuzhiyun         case ignoreCount:
220*4882a593Smuzhiyun             icount = dmxVDLCount(buf);
221*4882a593Smuzhiyun             state = ignoreEntry;
222*4882a593Smuzhiyun             break;
223*4882a593Smuzhiyun         case ignoreEntry:
224*4882a593Smuzhiyun             if (!--icount)
225*4882a593Smuzhiyun                 state = virtualEntry;
226*4882a593Smuzhiyun             break;
227*4882a593Smuzhiyun         }
228*4882a593Smuzhiyun     }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun     if (str != stdin)
231*4882a593Smuzhiyun         fclose(str);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun     return entry;
234*4882a593Smuzhiyun }
235