| 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 "os.h" |
| 46 | #include "dmxconfig.h" |
| 47 | #include "dmxparse.h" |
| 48 | #include "dmxcompat.h" |
| 49 | #include "parser.h" |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | #include <ctype.h> |
| 54 | |
| 55 | static int |
| 56 | dmxVDLReadLine(FILE * str, char *buf, int len) |
| 57 | { |
| 58 | if (fgets(buf, len, str)) |
| 59 | return strlen(buf); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int |
| 64 | dmxVDLCount(const char *buf) |
| 65 | { |
| 66 | return strtol(buf, NULL, 10); |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | dmxVDLVirtualEntry(const char *buf, char *name, int *len, int *x, int *y) |
| 71 | { |
| 72 | char *end; |
| 73 | const char *s; |
| 74 | char *d; |
| 75 | int start; |
| 76 | |
| 77 | *x = strtol(buf, &end, 10); |
| 78 | *y = strtol(end, &end, 10); |
| 79 | |
| 80 | for (s = end, d = name, start = 1; *s && *s != '['; ++s) { |
| 81 | if (start && isspace(*s)) |
| 82 | continue; |
| 83 | *d++ = *s; |
| 84 | start = 0; |
| 85 | } |
| 86 | *d = '\0'; |
| 87 | while (d > name && isspace(d[-1])) |
| 88 | *--d = '\0'; /* remove trailing space */ |
| 89 | *len = strlen(name); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | dmxVDLDisplayEntry(const char *buf, |
| 94 | char *name, int *len, |
| 95 | int *x, int *y, int *xoff, int *yoff, int *xorig, int *yorig) |
| 96 | { |
| 97 | const char *pt; |
| 98 | char *end; |
| 99 | |
| 100 | pt = strchr(buf, ' '); |
| 101 | strlcpy(name, buf, 1 + pt - buf); |
| 102 | *len = strlen(name); |
| 103 | |
| 104 | *x = strtol(pt, &end, 10); |
| 105 | *y = strtol(end, &end, 10); |
| 106 | *xorig = strtol(end, &end, 10); |
| 107 | *yorig = strtol(end, &end, 10); |
| 108 | *xoff = strtol(end, &end, 10); |
| 109 | *yoff = strtol(end, NULL, 10); |
| 110 | } |
| 111 | |
| 112 | /** Read from the VDL format \a filename and return a newly allocated \a |
| 113 | * DMXConfigEntryPtr */ |
| 114 | DMXConfigEntryPtr |
| 115 | dmxVDLRead(const char *filename) |
| 116 | { |
| 117 | FILE *str; |
| 118 | char buf[2048]; /* RATS: Use ok */ |
| 119 | char *pt; |
| 120 | int lineno = 0; |
| 121 | DMXConfigEntryPtr entry = NULL; |
| 122 | DMXConfigVirtualPtr virtual = NULL; |
| 123 | DMXConfigSubPtr sub = NULL; |
| 124 | DMXConfigDisplayPtr display = NULL; |
| 125 | DMXConfigFullDimPtr fdim = NULL; |
| 126 | int vcount = 0; |
| 127 | int dcount = 0; |
| 128 | int icount = 0; |
| 129 | int x, y, xoff, yoff, xorig, yorig; |
| 130 | char name[2048]; /* RATS: Use ok */ |
| 131 | const char *tmp; |
| 132 | int len; |
| 133 | enum { |
| 134 | simulateFlag, |
| 135 | virtualCount, |
| 136 | virtualEntry, |
| 137 | displayCount, |
| 138 | displayEntry, |
| 139 | ignoreCount, |
| 140 | ignoreEntry |
| 141 | } state = simulateFlag; |
| 142 | |
| 143 | if (!filename) |
| 144 | str = stdin; |
| 145 | else |
| 146 | str = fopen(filename, "r"); |
| 147 | if (!str) |
| 148 | return NULL; |
| 149 | |
| 150 | while (dmxVDLReadLine(str, buf, sizeof(buf))) { |
| 151 | DMXConfigCommentPtr comment = NULL; |
| 152 | |
| 153 | ++lineno; |
| 154 | for (pt = buf; *pt; pt++) |
| 155 | if (*pt == '\r' || *pt == '\n') { |
| 156 | *pt = '\0'; |
| 157 | break; |
| 158 | } |
| 159 | if (buf[0] == '#') { |
| 160 | tmp = dmxConfigCopyString(buf + 1, strlen(buf + 1)); |
| 161 | comment = dmxConfigCreateComment(T_COMMENT, lineno, tmp); |
| 162 | entry = dmxConfigAddEntry(entry, dmxConfigComment, comment, NULL); |
| 163 | continue; |
| 164 | } |
| 165 | switch (state) { |
| 166 | case simulateFlag: |
| 167 | state = virtualCount; |
| 168 | break; |
| 169 | case virtualCount: |
| 170 | vcount = dmxVDLCount(buf); |
| 171 | state = virtualEntry; |
| 172 | break; |
| 173 | case virtualEntry: |
| 174 | len = sizeof(name); |
| 175 | dmxVDLVirtualEntry(buf, name, &len, &x, &y); |
| 176 | tmp = dmxConfigCopyString(name, len); |
| 177 | virtual = dmxConfigCreateVirtual(NULL, |
| 178 | dmxConfigCreateString(T_STRING, |
| 179 | lineno, |
| 180 | NULL, |
| 181 | tmp), |
| 182 | dmxConfigCreatePair(T_DIMENSION, |
| 183 | lineno, |
| 184 | NULL, |
| 185 | x, y, 0, 0), |
| 186 | NULL, NULL, NULL); |
| 187 | state = displayCount; |
| 188 | break; |
| 189 | case displayCount: |
| 190 | dcount = dmxVDLCount(buf); |
| 191 | state = displayEntry; |
| 192 | break; |
| 193 | case displayEntry: |
| 194 | dmxVDLDisplayEntry(buf, name, &len, &x, &y, &xoff, &yoff, |
| 195 | &xorig, &yorig); |
| 196 | tmp = dmxConfigCopyString(name, len); |
| 197 | fdim = |
| 198 | dmxConfigCreateFullDim(dmxConfigCreatePartDim |
| 199 | (dmxConfigCreatePair |
| 200 | (T_DIMENSION, lineno, NULL, x, y, 0, 0), |
| 201 | dmxConfigCreatePair(T_OFFSET, lineno, |
| 202 | NULL, xoff, yoff, |
| 203 | xoff, yoff)), NULL); |
| 204 | display = |
| 205 | dmxConfigCreateDisplay(NULL, |
| 206 | dmxConfigCreateString(T_STRING, lineno, |
| 207 | NULL, tmp), fdim, |
| 208 | dmxConfigCreatePair(T_ORIGIN, lineno, |
| 209 | NULL, xorig, yorig, |
| 210 | 0, 0), NULL); |
| 211 | sub = dmxConfigAddSub(sub, dmxConfigSubDisplay(display)); |
| 212 | if (!--dcount) { |
| 213 | state = ignoreCount; |
| 214 | virtual->subentry = sub; |
| 215 | entry = dmxConfigAddEntry(entry, |
| 216 | dmxConfigVirtual, NULL, virtual); |
| 217 | virtual = NULL; |
| 218 | sub = NULL; |
| 219 | } |
| 220 | break; |
| 221 | case ignoreCount: |
| 222 | icount = dmxVDLCount(buf); |
| 223 | state = ignoreEntry; |
| 224 | break; |
| 225 | case ignoreEntry: |
| 226 | if (!--icount) |
| 227 | state = virtualEntry; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (str != stdin) |
| 233 | fclose(str); |
| 234 | |
| 235 | return entry; |
| 236 | } |