1706f2543Smrg/*
2706f2543Smrg * Copyright 2002 Red Hat Inc., Durham, North Carolina.
3706f2543Smrg *
4706f2543Smrg * All Rights Reserved.
5706f2543Smrg *
6706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining
7706f2543Smrg * a copy of this software and associated documentation files (the
8706f2543Smrg * "Software"), to deal in the Software without restriction, including
9706f2543Smrg * without limitation on the rights to use, copy, modify, merge,
10706f2543Smrg * publish, distribute, sublicense, and/or sell copies of the Software,
11706f2543Smrg * and to permit persons to whom the Software is furnished to do so,
12706f2543Smrg * subject to the following conditions:
13706f2543Smrg *
14706f2543Smrg * The above copyright notice and this permission notice (including the
15706f2543Smrg * next paragraph) shall be included in all copies or substantial
16706f2543Smrg * portions of the Software.
17706f2543Smrg *
18706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19706f2543Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20706f2543Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21706f2543Smrg * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22706f2543Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23706f2543Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24706f2543Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25706f2543Smrg * SOFTWARE.
26706f2543Smrg */
27706f2543Smrg
28706f2543Smrg/*
29706f2543Smrg * Authors:
30706f2543Smrg *   Rickard E. (Rik) Faith <faith@redhat.com>
31706f2543Smrg */
32706f2543Smrg
33706f2543Smrg/** \file
34706f2543Smrg * This file provides some compatibility support for reading VDL files
35706f2543Smrg * that are used by xmovie
36706f2543Smrg * (http://www.llnl.gov/icc/sdd/img/xmovie/xmovie.shtml).
37706f2543Smrg *
38706f2543Smrg * This file is not used by the DMX server.
39706f2543Smrg */
40706f2543Smrg
41706f2543Smrg#ifdef HAVE_DMX_CONFIG_H
42706f2543Smrg#include <dmx-config.h>
43706f2543Smrg#endif
44706f2543Smrg
45706f2543Smrg#include "dmxconfig.h"
46706f2543Smrg#include "dmxparse.h"
47706f2543Smrg#include "dmxcompat.h"
48706f2543Smrg#include "parser.h"
49706f2543Smrg#include <stdio.h>
50706f2543Smrg#include <stdlib.h>
51706f2543Smrg#include <string.h>
52706f2543Smrg#include <ctype.h>
53706f2543Smrg
54706f2543Smrgstatic int dmxVDLReadLine(FILE *str, char *buf, int len)
55706f2543Smrg{
56706f2543Smrg    if (fgets(buf, len, str)) return strlen(buf);
57706f2543Smrg    return 0;
58706f2543Smrg}
59706f2543Smrg
60706f2543Smrgstatic int dmxVDLCount(const char *buf)
61706f2543Smrg{
62706f2543Smrg    return strtol(buf, NULL, 10);
63706f2543Smrg}
64706f2543Smrg
65706f2543Smrgstatic void dmxVDLVirtualEntry(const char *buf,
66706f2543Smrg                               char *name, int *len,
67706f2543Smrg                               int *x, int *y)
68706f2543Smrg{
69706f2543Smrg    char       *end;
70706f2543Smrg    const char *s;
71706f2543Smrg    char       *d;
72706f2543Smrg    int        start;
73706f2543Smrg
74706f2543Smrg    *x = strtol(buf, &end, 10);
75706f2543Smrg    *y = strtol(end, &end, 10);
76706f2543Smrg
77706f2543Smrg    for (s = end, d = name, start = 1; *s && *s != '['; ++s) {
78706f2543Smrg        if (start && isspace(*s)) continue;
79706f2543Smrg        *d++  = *s;
80706f2543Smrg        start = 0;
81706f2543Smrg    }
82706f2543Smrg    *d = '\0';
83706f2543Smrg    while (d > name && isspace(d[-1])) *--d = '\0'; /* remove trailing space */
84706f2543Smrg    *len = strlen(name);
85706f2543Smrg}
86706f2543Smrg
87706f2543Smrgstatic void dmxVDLDisplayEntry(const char *buf,
88706f2543Smrg                               char *name, int *len,
89706f2543Smrg                               int *x, int *y,
90706f2543Smrg                               int *xoff, int *yoff,
91706f2543Smrg                               int *xorig, int *yorig)
92706f2543Smrg{
93706f2543Smrg    const char *pt;
94706f2543Smrg    char       *end;
95706f2543Smrg
96706f2543Smrg    pt   = strchr(buf, ' ');
97706f2543Smrg    strncpy(name, buf, pt-buf);
98706f2543Smrg    name[pt-buf] = '\0';
99706f2543Smrg    *len  = strlen(name);
100706f2543Smrg
101706f2543Smrg    *x     = strtol(pt, &end, 10);
102706f2543Smrg    *y     = strtol(end, &end, 10);
103706f2543Smrg    *xorig = strtol(end, &end, 10);
104706f2543Smrg    *yorig = strtol(end, &end, 10);
105706f2543Smrg    *xoff  = strtol(end, &end, 10);
106706f2543Smrg    *yoff  = strtol(end, NULL, 10);
107706f2543Smrg}
108706f2543Smrg
109706f2543Smrg/** Read from the VDL format \a filename and return a newly allocated \a
110706f2543Smrg * DMXConfigEntryPtr */
111706f2543SmrgDMXConfigEntryPtr dmxVDLRead(const char *filename)
112706f2543Smrg{
113706f2543Smrg    FILE                *str;
114706f2543Smrg    char                buf[2048]; /* RATS: Use ok */
115706f2543Smrg    char                *pt;
116706f2543Smrg    int                 lineno  = 0;
117706f2543Smrg    DMXConfigEntryPtr   entry   = NULL;
118706f2543Smrg    DMXConfigVirtualPtr virtual = NULL;
119706f2543Smrg    DMXConfigSubPtr     sub     = NULL;
120706f2543Smrg    DMXConfigDisplayPtr display = NULL;
121706f2543Smrg    DMXConfigFullDimPtr fdim    = NULL;
122706f2543Smrg    int                 vcount  = 0;
123706f2543Smrg    int                 dcount  = 0;
124706f2543Smrg    int                 icount  = 0;
125706f2543Smrg    int                 x, y, xoff, yoff, xorig, yorig;
126706f2543Smrg    char                name[2048]; /* RATS: Use ok */
127706f2543Smrg    const char          *tmp;
128706f2543Smrg    int                 len;
129706f2543Smrg    enum {
130706f2543Smrg        simulateFlag,
131706f2543Smrg        virtualCount,
132706f2543Smrg        virtualEntry,
133706f2543Smrg        displayCount,
134706f2543Smrg        displayEntry,
135706f2543Smrg        ignoreCount,
136706f2543Smrg        ignoreEntry
137706f2543Smrg    }                 state = simulateFlag;
138706f2543Smrg
139706f2543Smrg    if (!filename) str = stdin;
140706f2543Smrg    else           str = fopen(filename, "r");
141706f2543Smrg    if (!str) return NULL;
142706f2543Smrg
143706f2543Smrg    while (dmxVDLReadLine(str, buf, sizeof(buf))) {
144706f2543Smrg        DMXConfigCommentPtr comment = NULL;
145706f2543Smrg
146706f2543Smrg        ++lineno;
147706f2543Smrg        for (pt = buf; *pt; pt++)
148706f2543Smrg            if (*pt == '\r' || *pt == '\n') {
149706f2543Smrg                *pt = '\0';
150706f2543Smrg                break;
151706f2543Smrg            }
152706f2543Smrg        if (buf[0] == '#') {
153706f2543Smrg            tmp = dmxConfigCopyString(buf + 1, strlen(buf + 1));
154706f2543Smrg            comment = dmxConfigCreateComment(T_COMMENT, lineno, tmp);
155706f2543Smrg            entry = dmxConfigAddEntry(entry, dmxConfigComment, comment, NULL);
156706f2543Smrg            continue;
157706f2543Smrg        }
158706f2543Smrg        switch (state) {
159706f2543Smrg        case simulateFlag:
160706f2543Smrg            state = virtualCount;
161706f2543Smrg            break;
162706f2543Smrg        case virtualCount:
163706f2543Smrg            vcount = dmxVDLCount(buf);
164706f2543Smrg            state = virtualEntry;
165706f2543Smrg            break;
166706f2543Smrg        case virtualEntry:
167706f2543Smrg            len = sizeof(name);
168706f2543Smrg            dmxVDLVirtualEntry(buf, name, &len, &x, &y);
169706f2543Smrg            tmp     = dmxConfigCopyString(name, len);
170706f2543Smrg            virtual = dmxConfigCreateVirtual(NULL,
171706f2543Smrg                                             dmxConfigCreateString(T_STRING,
172706f2543Smrg                                                                   lineno,
173706f2543Smrg                                                                   NULL,
174706f2543Smrg                                                                   tmp),
175706f2543Smrg                                             dmxConfigCreatePair(T_DIMENSION,
176706f2543Smrg                                                                 lineno,
177706f2543Smrg                                                                 NULL,
178706f2543Smrg                                                                 x, y, 0, 0),
179706f2543Smrg                                             NULL, NULL, NULL);
180706f2543Smrg            state = displayCount;
181706f2543Smrg            break;
182706f2543Smrg        case displayCount:
183706f2543Smrg            dcount = dmxVDLCount(buf);
184706f2543Smrg            state = displayEntry;
185706f2543Smrg            break;
186706f2543Smrg        case displayEntry:
187706f2543Smrg            dmxVDLDisplayEntry(buf, name, &len, &x, &y, &xoff, &yoff,
188706f2543Smrg                               &xorig, &yorig);
189706f2543Smrg            tmp     = dmxConfigCopyString(name, len);
190706f2543Smrg            fdim    = dmxConfigCreateFullDim(
191706f2543Smrg                dmxConfigCreatePartDim(
192706f2543Smrg                    dmxConfigCreatePair(T_DIMENSION,
193706f2543Smrg                                        lineno,
194706f2543Smrg                                        NULL,
195706f2543Smrg                                        x, y, 0, 0),
196706f2543Smrg                    dmxConfigCreatePair(T_OFFSET,
197706f2543Smrg                                        lineno,
198706f2543Smrg                                        NULL,
199706f2543Smrg                                        xoff, yoff,
200706f2543Smrg                                        xoff, yoff)),
201706f2543Smrg                NULL);
202706f2543Smrg            display = dmxConfigCreateDisplay(NULL,
203706f2543Smrg                                             dmxConfigCreateString(T_STRING,
204706f2543Smrg                                                                   lineno,
205706f2543Smrg                                                                   NULL,
206706f2543Smrg                                                                   tmp),
207706f2543Smrg                                             fdim,
208706f2543Smrg                                             dmxConfigCreatePair(T_ORIGIN,
209706f2543Smrg                                                                 lineno,
210706f2543Smrg                                                                 NULL,
211706f2543Smrg                                                                 xorig, yorig,
212706f2543Smrg                                                                 0, 0),
213706f2543Smrg                                             NULL);
214706f2543Smrg            sub = dmxConfigAddSub(sub, dmxConfigSubDisplay(display));
215706f2543Smrg            if (!--dcount) {
216706f2543Smrg                state             = ignoreCount;
217706f2543Smrg                virtual->subentry = sub;
218706f2543Smrg                entry             = dmxConfigAddEntry(entry,
219706f2543Smrg                                                      dmxConfigVirtual,
220706f2543Smrg                                                      NULL,
221706f2543Smrg                                                      virtual);
222706f2543Smrg                virtual           = NULL;
223706f2543Smrg                sub               = NULL;
224706f2543Smrg            }
225706f2543Smrg            break;
226706f2543Smrg        case ignoreCount:
227706f2543Smrg            icount = dmxVDLCount(buf);
228706f2543Smrg            state = ignoreEntry;
229706f2543Smrg            break;
230706f2543Smrg        case ignoreEntry:
231706f2543Smrg            if (!--icount) state = virtualEntry;
232706f2543Smrg            break;
233706f2543Smrg        }
234706f2543Smrg    }
235706f2543Smrg    return entry;
236706f2543Smrg}
237