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