2 * Copyright 2002 Red Hat Inc., Durham, North Carolina.
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:
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.
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
30 * Rickard E. (Rik) Faith <faith@redhat.com>
36 * This file provides support routines and helper functions to be used
37 * by the DMX configuration file parser.
39 * Because the DMX configuration file parsing should be capable of being
40 * used in a stand-alone fashion (i.e., independent from the DMX server
41 * source tree), no dependencies on other DMX routines are made. */
43 #ifdef HAVE_DMX_CONFIG_H
44 #include <dmx-config.h>
53 /** A general error logging routine that does not depend on the dmxLog
56 dmxConfigLog(const char *format
, ...)
60 va_start(args
, format
);
61 vprintf(format
, args
); /* RATS: All calls to dmxConfigLog from
62 * dmxparse.c and dmxprint.c use a
68 dmxConfigAlloc(unsigned long bytes
)
70 void *area
= calloc(1, bytes
);
73 dmxConfigLog("dmxConfigAlloc: out of memory\n");
80 dmxConfigRealloc(void *orig
, unsigned long orig_bytes
, unsigned long bytes
)
82 unsigned char *area
= realloc(orig
, bytes
);
85 dmxConfigLog("dmxConfigRealloc: out of memory\n");
88 memset(area
+ orig_bytes
, 0, bytes
- orig_bytes
);
93 dmxConfigCopyString(const char *string
, int length
)
98 length
= strlen(string
);
99 copy
= dmxConfigAlloc(length
+ 1);
101 strncpy(copy
, string
, length
);
107 dmxConfigFree(void *area
)
113 dmxConfigCreateToken(int token
, int line
, const char *comment
)
115 DMXConfigTokenPtr pToken
= dmxConfigAlloc(sizeof(*pToken
));
117 pToken
->token
= token
;
119 pToken
->comment
= comment
;
124 dmxConfigFreeToken(DMXConfigTokenPtr p
)
128 dmxConfigFree((void *) p
->comment
);
133 dmxConfigCreateString(int token
, int line
,
134 const char *comment
, const char *string
)
136 DMXConfigStringPtr pString
= dmxConfigAlloc(sizeof(*pString
));
138 pString
->token
= token
;
139 pString
->line
= line
;
140 pString
->comment
= comment
;
141 pString
->string
= string
;
146 dmxConfigFreeString(DMXConfigStringPtr p
)
148 DMXConfigStringPtr next
;
154 dmxConfigFree((void *) p
->comment
);
155 dmxConfigFree((void *) p
->string
);
157 } while ((p
= next
));
161 dmxConfigCreateNumber(int token
, int line
, const char *comment
, int number
)
163 DMXConfigNumberPtr pNumber
= dmxConfigAlloc(sizeof(*pNumber
));
165 pNumber
->token
= token
;
166 pNumber
->line
= line
;
167 pNumber
->comment
= comment
;
168 pNumber
->number
= number
;
173 dmxConfigFreeNumber(DMXConfigNumberPtr p
)
177 dmxConfigFree((void *) p
->comment
);
182 dmxConfigCreatePair(int token
, int line
,
183 const char *comment
, int x
, int y
, int xsign
, int ysign
)
185 DMXConfigPairPtr pPair
= dmxConfigAlloc(sizeof(*pPair
));
187 pPair
->token
= token
;
189 pPair
->comment
= comment
;
192 pPair
->xsign
= (xsign
< 0) ? -1 : 1;
193 pPair
->ysign
= (ysign
< 0) ? -1 : 1;
198 dmxConfigFreePair(DMXConfigPairPtr p
)
202 dmxConfigFree((void *) p
->comment
);
207 dmxConfigCreateComment(int token
, int line
, const char *comment
)
209 DMXConfigCommentPtr pComment
= dmxConfigAlloc(sizeof(*pComment
));
211 pComment
->token
= token
;
212 pComment
->line
= line
;
213 pComment
->comment
= comment
;
218 dmxConfigFreeComment(DMXConfigCommentPtr p
)
222 dmxConfigFree((void *) p
->comment
);
227 dmxConfigCreatePartDim(DMXConfigPairPtr pDim
, DMXConfigPairPtr pOffset
)
229 DMXConfigPartDimPtr pPart
= dmxConfigAlloc(sizeof(*pPart
));
232 pPart
->offset
= pOffset
;
237 dmxConfigFreePartDim(DMXConfigPartDimPtr p
)
241 dmxConfigFreePair(p
->dim
);
242 dmxConfigFreePair(p
->offset
);
247 dmxConfigCreateFullDim(DMXConfigPartDimPtr pScrn
, DMXConfigPartDimPtr pRoot
)
249 DMXConfigFullDimPtr pFull
= dmxConfigAlloc(sizeof(*pFull
));
257 dmxConfigFreeFullDim(DMXConfigFullDimPtr p
)
261 dmxConfigFreePartDim(p
->scrn
);
262 dmxConfigFreePartDim(p
->root
);
267 dmxConfigCreateDisplay(DMXConfigTokenPtr pStart
,
268 DMXConfigStringPtr pName
,
269 DMXConfigFullDimPtr pDim
,
270 DMXConfigPairPtr pOrigin
, DMXConfigTokenPtr pEnd
)
272 DMXConfigDisplayPtr pDisplay
= dmxConfigAlloc(sizeof(*pDisplay
));
274 pDisplay
->start
= pStart
;
275 pDisplay
->dname
= pName
;
276 pDisplay
->dim
= pDim
;
277 pDisplay
->origin
= pOrigin
;
278 pDisplay
->end
= pEnd
;
280 pDisplay
->name
= pName
? pName
->string
: NULL
;
281 pDisplay
->rootXOrigin
= pOrigin
? pOrigin
->x
: 0;
282 pDisplay
->rootYOrigin
= pOrigin
? pOrigin
->y
: 0;
284 if (pDim
&& pDim
->scrn
&& pDim
->scrn
->dim
) {
285 pDisplay
->scrnWidth
= pDim
->scrn
->dim
->x
;
286 pDisplay
->scrnHeight
= pDim
->scrn
->dim
->y
;
288 if (pDim
&& pDim
->scrn
&& pDim
->scrn
->offset
) {
289 pDisplay
->scrnX
= pDim
->scrn
->offset
->x
;
290 pDisplay
->scrnY
= pDim
->scrn
->offset
->y
;
291 pDisplay
->scrnXSign
= pDim
->scrn
->offset
->xsign
;
292 pDisplay
->scrnYSign
= pDim
->scrn
->offset
->ysign
;
295 if (pDim
&& pDim
->root
) {
296 if (pDim
->root
->dim
) {
297 pDisplay
->rootWidth
= pDim
->root
->dim
->x
;
298 pDisplay
->rootHeight
= pDim
->root
->dim
->y
;
300 if (pDim
->root
->offset
) {
301 pDisplay
->rootX
= pDim
->root
->offset
->x
;
302 pDisplay
->rootY
= pDim
->root
->offset
->y
;
303 pDisplay
->rootXSign
= pDim
->root
->offset
->xsign
;
304 pDisplay
->rootYSign
= pDim
->root
->offset
->ysign
;
307 else { /* If no root specification, copy width
308 * and height from scrn -- leave offset
309 * as zero, since it is relative to
311 pDisplay
->rootWidth
= pDisplay
->scrnWidth
;
312 pDisplay
->rootHeight
= pDisplay
->scrnHeight
;
319 dmxConfigFreeDisplay(DMXConfigDisplayPtr p
)
323 dmxConfigFreeToken(p
->start
);
324 dmxConfigFreeString(p
->dname
);
325 dmxConfigFreeFullDim(p
->dim
);
326 dmxConfigFreeToken(p
->end
);
331 dmxConfigCreateWall(DMXConfigTokenPtr pStart
,
332 DMXConfigPairPtr pWallDim
,
333 DMXConfigPairPtr pDisplayDim
,
334 DMXConfigStringPtr pNameList
, DMXConfigTokenPtr pEnd
)
336 DMXConfigWallPtr pWall
= dmxConfigAlloc(sizeof(*pWall
));
338 pWall
->start
= pStart
;
339 pWall
->wallDim
= pWallDim
;
340 pWall
->displayDim
= pDisplayDim
;
341 pWall
->nameList
= pNameList
;
344 pWall
->width
= pDisplayDim
? pDisplayDim
->x
: 0;
345 pWall
->height
= pDisplayDim
? pDisplayDim
->y
: 0;
346 pWall
->xwall
= pWallDim
? pWallDim
->x
: 0;
347 pWall
->ywall
= pWallDim
? pWallDim
->y
: 0;
353 dmxConfigFreeWall(DMXConfigWallPtr p
)
357 dmxConfigFreeToken(p
->start
);
358 dmxConfigFreePair(p
->wallDim
);
359 dmxConfigFreePair(p
->displayDim
);
360 dmxConfigFreeString(p
->nameList
);
361 dmxConfigFreeToken(p
->end
);
366 dmxConfigCreateOption(DMXConfigTokenPtr pStart
,
367 DMXConfigStringPtr pOption
, DMXConfigTokenPtr pEnd
)
371 DMXConfigStringPtr p
;
372 DMXConfigOptionPtr option
= dmxConfigAlloc(sizeof(*option
));
374 for (p
= pOption
; p
; p
= p
->next
) {
376 length
+= strlen(p
->string
) + 1;
379 option
->string
= dmxConfigAlloc(length
+ 1);
381 for (p
= pOption
; p
; p
= p
->next
) {
383 int len
= strlen(p
->string
);
385 strncpy(option
->string
+ offset
, p
->string
, len
);
388 option
->string
[offset
++] = ' ';
391 option
->string
[offset
] = '\0';
393 option
->start
= pStart
;
394 option
->option
= pOption
;
401 dmxConfigFreeOption(DMXConfigOptionPtr p
)
406 dmxConfigFreeToken(p
->start
);
407 dmxConfigFreeString(p
->option
);
408 dmxConfigFreeToken(p
->end
);
413 dmxConfigLookupParam(DMXConfigParamPtr p
, const char *key
, int *argc
)
415 DMXConfigParamPtr pt
;
417 for (pt
= p
; pt
; pt
= pt
->next
) {
418 if (pt
->argv
&& !strcasecmp(pt
->argv
[0], key
)) {
428 dmxConfigCreateParam(DMXConfigTokenPtr pStart
,
429 DMXConfigTokenPtr pOpen
,
430 DMXConfigStringPtr pParam
,
431 DMXConfigTokenPtr pClose
, DMXConfigTokenPtr pEnd
)
433 DMXConfigParamPtr param
= dmxConfigAlloc(sizeof(*param
));
434 DMXConfigStringPtr pt
;
438 for (pt
= pParam
; pt
; pt
= pt
->next
) {
440 param
->argv
= realloc(param
->argv
,
441 (param
->argc
+ 2) * sizeof(*param
->argv
));
442 param
->argv
[param
->argc
] = pt
->string
;
447 param
->argv
[param
->argc
] = NULL
;
449 param
->start
= pStart
;
451 param
->param
= pParam
;
452 param
->close
= pClose
;
459 dmxConfigFreeParam(DMXConfigParamPtr p
)
461 DMXConfigParamPtr next
;
467 dmxConfigFreeToken(p
->start
);
468 dmxConfigFreeToken(p
->open
);
469 dmxConfigFreeString(p
->param
);
470 dmxConfigFreeToken(p
->close
);
471 dmxConfigFreeToken(p
->end
);
472 dmxConfigFree(p
->argv
);
474 } while ((p
= next
));
478 dmxConfigCreateSub(DMXConfigType type
,
479 DMXConfigCommentPtr comment
,
480 DMXConfigDisplayPtr display
,
481 DMXConfigWallPtr wall
,
482 DMXConfigOptionPtr option
, DMXConfigParamPtr param
)
484 DMXConfigSubPtr pSub
= dmxConfigAlloc(sizeof(*pSub
));
488 case dmxConfigComment
:
489 pSub
->comment
= comment
;
491 case dmxConfigDisplay
:
492 pSub
->display
= display
;
497 case dmxConfigOption
:
498 pSub
->option
= option
;
504 dmxConfigLog("Type %d not supported in subentry\n", type
);
511 dmxConfigFreeSub(DMXConfigSubPtr sub
)
515 for (pt
= sub
; pt
; pt
= pt
->next
) {
517 case dmxConfigComment
:
518 dmxConfigFreeComment(pt
->comment
);
520 case dmxConfigDisplay
:
521 dmxConfigFreeDisplay(pt
->display
);
524 dmxConfigFreeWall(pt
->wall
);
526 case dmxConfigOption
:
527 dmxConfigFreeOption(pt
->option
);
530 dmxConfigFreeParam(pt
->param
);
533 dmxConfigLog("Type %d not supported in subentry\n", pt
->type
);
541 dmxConfigSubComment(DMXConfigCommentPtr comment
)
543 return dmxConfigCreateSub(dmxConfigComment
, comment
, NULL
, NULL
, NULL
,
548 dmxConfigSubDisplay(DMXConfigDisplayPtr display
)
550 return dmxConfigCreateSub(dmxConfigDisplay
, NULL
, display
, NULL
, NULL
,
555 dmxConfigSubWall(DMXConfigWallPtr wall
)
557 return dmxConfigCreateSub(dmxConfigWall
, NULL
, NULL
, wall
, NULL
, NULL
);
561 dmxConfigSubOption(DMXConfigOptionPtr option
)
563 return dmxConfigCreateSub(dmxConfigOption
, NULL
, NULL
, NULL
, option
, NULL
);
567 dmxConfigSubParam(DMXConfigParamPtr param
)
569 return dmxConfigCreateSub(dmxConfigParam
, NULL
, NULL
, NULL
, NULL
, param
);
572 extern DMXConfigSubPtr
573 dmxConfigAddSub(DMXConfigSubPtr head
, DMXConfigSubPtr sub
)
579 for (pt
= head
; pt
->next
; pt
= pt
->next
);
585 dmxConfigCreateVirtual(DMXConfigTokenPtr pStart
,
586 DMXConfigStringPtr pName
,
587 DMXConfigPairPtr pDim
,
588 DMXConfigTokenPtr pOpen
,
589 DMXConfigSubPtr pSubentry
, DMXConfigTokenPtr pClose
)
591 DMXConfigVirtualPtr pVirtual
= dmxConfigAlloc(sizeof(*pVirtual
));
593 pVirtual
->start
= pStart
;
594 pVirtual
->vname
= pName
;
595 pVirtual
->dim
= pDim
;
596 pVirtual
->open
= pOpen
;
597 pVirtual
->subentry
= pSubentry
;
598 pVirtual
->close
= pClose
;
600 pVirtual
->name
= pName
? pName
->string
: NULL
;
601 pVirtual
->width
= pDim
? pDim
->x
: 0;
602 pVirtual
->height
= pDim
? pDim
->y
: 0;
608 dmxConfigFreeVirtual(DMXConfigVirtualPtr
virtual)
610 dmxConfigFreeToken(virtual->start
);
611 dmxConfigFreeString(virtual->vname
);
612 dmxConfigFreePair(virtual->dim
);
613 dmxConfigFreeToken(virtual->open
);
614 dmxConfigFreeSub(virtual->subentry
);
615 dmxConfigFreeToken(virtual->close
);
616 dmxConfigFree(virtual);
620 dmxConfigCreateEntry(DMXConfigType type
,
621 DMXConfigCommentPtr comment
, DMXConfigVirtualPtr
virtual)
623 DMXConfigEntryPtr pEntry
= dmxConfigAlloc(sizeof(*pEntry
));
627 case dmxConfigComment
:
628 pEntry
->comment
= comment
;
630 case dmxConfigVirtual
:
631 pEntry
->virtual = virtual;
634 dmxConfigLog("Type %d not supported in entry\n", type
);
641 dmxConfigFreeEntry(DMXConfigEntryPtr entry
)
643 DMXConfigEntryPtr pt
;
645 for (pt
= entry
; pt
; pt
= pt
->next
) {
647 case dmxConfigComment
:
648 dmxConfigFreeComment(pt
->comment
);
650 case dmxConfigVirtual
:
651 dmxConfigFreeVirtual(pt
->virtual);
654 dmxConfigLog("Type %d not supported in entry\n", pt
->type
);
658 dmxConfigFree(entry
);
662 dmxConfigAddEntry(DMXConfigEntryPtr head
,
664 DMXConfigCommentPtr comment
, DMXConfigVirtualPtr
virtual)
666 DMXConfigEntryPtr child
= dmxConfigCreateEntry(type
, comment
, virtual);
667 DMXConfigEntryPtr pt
;
672 for (pt
= head
; pt
->next
; pt
= pt
->next
);
679 dmxConfigEntryComment(DMXConfigCommentPtr comment
)
681 return dmxConfigCreateEntry(dmxConfigComment
, comment
, NULL
);
685 dmxConfigEntryVirtual(DMXConfigVirtualPtr
virtual)
687 return dmxConfigCreateEntry(dmxConfigVirtual
, NULL
, virtual);