summaryrefslogtreecommitdiff
path: root/hw/dmx/config/dmxparse.c
blob: 1acb6d13e6b6d237e67a058f161427e1ab7ceb9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/* $XFree86$ */
/*
 * Copyright 2002 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Authors:
 *   Rickard E. (Rik) Faith <faith@redhat.com>
 *
 */

/** \file
 *
 * This file provides support routines and helper functions to be used
 * by the DMX configuration file parser.
 *
 * Because the DMX configuration file parsing should be capable of being
 * used in a stand-alone fashion (i.e., independent from the DMX server
 * source tree), no dependencies on other DMX routines are made. */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "dmxparse.h"

/** A general error logging routine that does not depend on the dmxLog
 * functions. */
void dmxConfigLog(const char *format, ...)
{
    va_list args;
    
    va_start(args, format);
    vprintf(format, args);      /* RATS: All calls to dmxConfigLog from
                                 * dmxparse.c and dmxprint.c use a
                                 * trusted format. */
    va_end(args);
}

void *dmxConfigAlloc(unsigned long bytes)
{
    void *area = malloc(bytes);
    if (!area) {
        dmxConfigLog("dmxConfigAlloc: out of memory\n");
        return NULL;
    }
    memset(area, 0, bytes);
    return area;
}

void *dmxConfigRealloc(void *orig, unsigned long orig_bytes,
                       unsigned long bytes)
{
    unsigned char *area = realloc(orig, bytes);
    if (!area) {
        dmxConfigLog("dmxConfigRealloc: out of memory\n");
        return NULL;
    }
    memset(area + orig_bytes, 0, bytes - orig_bytes);
    return area;
}

const char *dmxConfigCopyString(const char *string, int length)
{
    char *copy;
    
    if (!length) length = strlen(string);
    copy = dmxConfigAlloc(length + 1);
    if (length) strncpy(copy, string, length);
    copy[length] = '\0';
    return copy;
}

void dmxConfigFree(void *area)
{
    if (area) free(area);
}

DMXConfigTokenPtr dmxConfigCreateToken(int token, int line,
                                       const char *comment)
{
    DMXConfigTokenPtr pToken = dmxConfigAlloc(sizeof(*pToken));
    pToken->token   = token;
    pToken->line    = line;
    pToken->comment = comment;
    return pToken;
}

void dmxConfigFreeToken(DMXConfigTokenPtr p)
{
    if (!p) return;
    dmxConfigFree((void *)p->comment);
    dmxConfigFree(p);
}

DMXConfigStringPtr dmxConfigCreateString(int token, int line,
                                         const char *comment,
                                         const char *string)
{
    DMXConfigStringPtr pString = dmxConfigAlloc(sizeof(*pString));

    pString->token   = token;
    pString->line    = line;
    pString->comment = comment;
    pString->string  = string;
    return pString;
}

void dmxConfigFreeString(DMXConfigStringPtr p)
{
    DMXConfigStringPtr next;

    if (!p) return;
    do {
        next = p->next;
        dmxConfigFree((void *)p->comment);
        dmxConfigFree((void *)p->string);
        dmxConfigFree(p);
    } while ((p = next));
}
 
DMXConfigNumberPtr dmxConfigCreateNumber(int token, int line,
                                         const char *comment,
                                         int number)
{
    DMXConfigNumberPtr pNumber = dmxConfigAlloc(sizeof(*pNumber));

    pNumber->token   = token;
    pNumber->line    = line;
    pNumber->comment = comment;
    pNumber->number  = number;
    return pNumber;
}

void dmxConfigFreeNumber(DMXConfigNumberPtr p)
{
    if (!p) return;
    dmxConfigFree((void *)p->comment);
    dmxConfigFree(p);
}

DMXConfigPairPtr dmxConfigCreatePair(int token, int line,
                                     const char *comment,
                                     int x, int y,
                                     int xsign, int ysign)
{
    DMXConfigPairPtr pPair = dmxConfigAlloc(sizeof(*pPair));

    pPair->token   = token;
    pPair->line    = line;
    pPair->comment = comment;
    pPair->x       = x;
    pPair->y       = y;
    pPair->xsign   = (xsign < 0) ? -1 : 1;
    pPair->ysign   = (ysign < 0) ? -1 : 1;
    return pPair;
}

void dmxConfigFreePair(DMXConfigPairPtr p)
{
    if (!p) return;
    dmxConfigFree((void *)p->comment);
    dmxConfigFree(p);
}

DMXConfigCommentPtr dmxConfigCreateComment(int token, int line,
                                           const char *comment)
{
    DMXConfigCommentPtr pComment = dmxConfigAlloc(sizeof(*pComment));

    pComment->token   = token;
    pComment->line    = line;
    pComment->comment = comment;
    return pComment;
}

void dmxConfigFreeComment(DMXConfigCommentPtr p)
{
    if (!p) return;
    dmxConfigFree((void *)p->comment);
    dmxConfigFree(p);
}

DMXConfigPartDimPtr dmxConfigCreatePartDim(DMXConfigPairPtr pDim,
                                           DMXConfigPairPtr pOffset)
{
    DMXConfigPartDimPtr pPart = dmxConfigAlloc(sizeof(*pPart));
    pPart->dim    = pDim;
    pPart->offset = pOffset;
    return pPart;
}

void dmxConfigFreePartDim(DMXConfigPartDimPtr p)
{
    if (!p) return;
    dmxConfigFreePair(p->dim);
    dmxConfigFreePair(p->offset);
    dmxConfigFree(p);
}

DMXConfigFullDimPtr dmxConfigCreateFullDim(DMXConfigPartDimPtr pScrn,
                                           DMXConfigPartDimPtr pRoot)
{
    DMXConfigFullDimPtr pFull = dmxConfigAlloc(sizeof(*pFull));
    pFull->scrn = pScrn;
    pFull->root = pRoot;
    return pFull;
}

void dmxConfigFreeFullDim(DMXConfigFullDimPtr p)
{
    if (!p) return;
    dmxConfigFreePartDim(p->scrn);
    dmxConfigFreePartDim(p->root);
    dmxConfigFree(p);
}

DMXConfigDisplayPtr dmxConfigCreateDisplay(DMXConfigTokenPtr pStart,
                                           DMXConfigStringPtr pName,
                                           DMXConfigFullDimPtr pDim,
                                           DMXConfigPairPtr pOrigin,
                                           DMXConfigTokenPtr pEnd)
{
    DMXConfigDisplayPtr pDisplay = dmxConfigAlloc(sizeof(*pDisplay));

    memset(pDisplay, 0, sizeof(*pDisplay));

    pDisplay->start          = pStart;
    pDisplay->dname          = pName;
    pDisplay->dim            = pDim;
    pDisplay->origin         = pOrigin;
    pDisplay->end            = pEnd;

    pDisplay->name           = pName ? pName->string : NULL;
    pDisplay->rootXOrigin    = pOrigin ? pOrigin->x : 0;
    pDisplay->rootYOrigin    = pOrigin ? pOrigin->y : 0;

    if (pDim && pDim->scrn && pDim->scrn->dim) {
        pDisplay->scrnWidth  = pDim->scrn->dim->x;
        pDisplay->scrnHeight = pDim->scrn->dim->y;
    }
    if (pDim && pDim->scrn && pDim->scrn->offset) {
        pDisplay->scrnX      = pDim->scrn->offset->x;
        pDisplay->scrnY      = pDim->scrn->offset->y;
        pDisplay->scrnXSign  = pDim->scrn->offset->xsign;
        pDisplay->scrnYSign  = pDim->scrn->offset->ysign;
    }
    
    if (pDim && pDim->root) {
        if (pDim->root->dim) {
            pDisplay->rootWidth  = pDim->root->dim->x;
            pDisplay->rootHeight = pDim->root->dim->y;
        }
        if (pDim->root->offset) {
            pDisplay->rootX      = pDim->root->offset->x;
            pDisplay->rootY      = pDim->root->offset->y;
            pDisplay->rootXSign  = pDim->root->offset->xsign;
            pDisplay->rootYSign  = pDim->root->offset->ysign;
        }
    } else {                    /* If no root specification, copy width
                                 * and height from scrn -- leave offset
                                 * as zero, since it is relative to
                                 * scrn. */
        pDisplay->rootWidth  = pDisplay->scrnWidth;
        pDisplay->rootHeight = pDisplay->scrnHeight;
    }


    return pDisplay;
}

void dmxConfigFreeDisplay(DMXConfigDisplayPtr p)
{
    if (!p) return;
    dmxConfigFreeToken(p->start);
    dmxConfigFreeString(p->dname);
    dmxConfigFreeFullDim(p->dim);
    dmxConfigFreeToken(p->end);
    dmxConfigFree(p);
}

DMXConfigWallPtr dmxConfigCreateWall(DMXConfigTokenPtr pStart,
                                     DMXConfigPairPtr pWallDim,
                                     DMXConfigPairPtr pDisplayDim,
                                     DMXConfigStringPtr pNameList,
                                     DMXConfigTokenPtr pEnd)
{
    DMXConfigWallPtr pWall = dmxConfigAlloc(sizeof(*pWall));

    pWall->start      = pStart;
    pWall->wallDim    = pWallDim;
    pWall->displayDim = pDisplayDim;
    pWall->nameList   = pNameList;
    pWall->end        = pEnd;

    pWall->width      = pDisplayDim ? pDisplayDim->x : 0;
    pWall->height     = pDisplayDim ? pDisplayDim->y : 0;
    pWall->xwall      = pWallDim    ? pWallDim->x    : 0;
    pWall->ywall      = pWallDim    ? pWallDim->y    : 0;

    return pWall;
}

void dmxConfigFreeWall(DMXConfigWallPtr p)
{
    if (!p) return;
    dmxConfigFreeToken(p->start);
    dmxConfigFreePair(p->wallDim);
    dmxConfigFreePair(p->displayDim);
    dmxConfigFreeString(p->nameList);
    dmxConfigFreeToken(p->end);
    dmxConfigFree(p);
}

DMXConfigOptionPtr dmxConfigCreateOption(DMXConfigTokenPtr pStart,
                                         DMXConfigStringPtr pOption,
                                         DMXConfigTokenPtr pEnd)
{
    int                length = 0;
    int                offset = 0;
    DMXConfigStringPtr p;
    DMXConfigOptionPtr option = dmxConfigAlloc(sizeof(*option));

    for (p = pOption; p; p = p->next) {
        if (p->string) length += strlen(p->string) + 1;
    }

    option->string = dmxConfigAlloc(length + 1);
    
    for (p = pOption; p; p = p->next) {
        if (p->string) {
            int len = strlen(p->string);
            strncpy(option->string + offset, p->string, len);
            offset += len;
            if (p->next) option->string[offset++] = ' ';
        }
    }
    option->string[offset] = '\0';

    option->start  = pStart;
    option->option = pOption;
    option->end    = pEnd;

    return option;
}

void dmxConfigFreeOption(DMXConfigOptionPtr p)
{
    if (!p) return;
    if (p->string) free(p->string);
    dmxConfigFreeToken(p->start);
    dmxConfigFreeString(p->option);
    dmxConfigFreeToken(p->end);
    dmxConfigFree(p);
}

const char **dmxConfigLookupParam(DMXConfigParamPtr p, const char *key,
                                  int *argc)
{
    DMXConfigParamPtr pt;

    for (pt = p; pt; pt = pt->next) {
        if (pt->argv && !strcasecmp(pt->argv[0], key)) {
            *argc = pt->argc;
            return pt->argv;
        }
    }
    *argc  = 0;
    return NULL;
}

DMXConfigParamPtr dmxConfigCreateParam(DMXConfigTokenPtr pStart,
                                       DMXConfigTokenPtr pOpen,
                                       DMXConfigStringPtr pParam,
                                       DMXConfigTokenPtr pClose,
                                       DMXConfigTokenPtr pEnd)
{
    DMXConfigParamPtr  param = dmxConfigAlloc(sizeof(*param));
    DMXConfigStringPtr pt;

    param->argc = 0;
    param->argv = NULL;
    for (pt = pParam; pt; pt = pt->next) {
        if (pt->string) {
            param->argv = realloc(param->argv,
                                  (param->argc+2) * sizeof(*param->argv));
            param->argv[param->argc] = pt->string;
            ++param->argc;
        }
    }
    if (param->argv) param->argv[param->argc] = NULL;

    param->start = pStart;
    param->open  = pOpen;
    param->param = pParam;
    param->close = pClose;
    param->end   = pEnd;

    return param;
}

void dmxConfigFreeParam(DMXConfigParamPtr p)
{
    DMXConfigParamPtr next;

    if (!p) return;
    do {
        next = p->next;
        dmxConfigFreeToken(p->start);
        dmxConfigFreeToken(p->open);
        dmxConfigFreeString(p->param);
        dmxConfigFreeToken(p->close);
        dmxConfigFreeToken(p->end);
        dmxConfigFree(p->argv);
        dmxConfigFree(p);
    } while ((p = next));
}

DMXConfigSubPtr dmxConfigCreateSub(DMXConfigType type,
                                   DMXConfigCommentPtr comment,
                                   DMXConfigDisplayPtr display,
                                   DMXConfigWallPtr wall,
                                   DMXConfigOptionPtr option,
                                   DMXConfigParamPtr param)
{
    DMXConfigSubPtr pSub = dmxConfigAlloc(sizeof(*pSub));
    pSub->type = type;
    switch (type) {
    case dmxConfigComment: pSub->comment = comment;                     break;
    case dmxConfigDisplay: pSub->display = display;                     break;
    case dmxConfigWall:    pSub->wall    = wall;                        break;
    case dmxConfigOption:  pSub->option  = option;                      break;
    case dmxConfigParam:   pSub->param   = param;                       break;
    default: dmxConfigLog("Type %d not supported in subentry\n", type); break;
    }
    return pSub;
}

void dmxConfigFreeSub(DMXConfigSubPtr sub)
{
    DMXConfigSubPtr pt;

    for (pt = sub; pt; pt = pt->next) {
        switch (pt->type) {
        case dmxConfigComment: dmxConfigFreeComment(pt->comment); break;
        case dmxConfigDisplay: dmxConfigFreeDisplay(pt->display); break;
        case dmxConfigWall:    dmxConfigFreeWall(pt->wall);       break;
        case dmxConfigOption:  dmxConfigFreeOption(pt->option);   break;
        case dmxConfigParam:   dmxConfigFreeParam(pt->param);     break;
        default:
            dmxConfigLog("Type %d not supported in subentry\n", pt->type);
            break;
        }
    }
    dmxConfigFree(sub);
}

DMXConfigSubPtr dmxConfigSubComment(DMXConfigCommentPtr comment)
{
    return dmxConfigCreateSub(dmxConfigComment, comment, NULL, NULL, NULL,
                              NULL);
}

DMXConfigSubPtr dmxConfigSubDisplay(DMXConfigDisplayPtr display)
{
    return dmxConfigCreateSub(dmxConfigDisplay, NULL, display, NULL, NULL,
                              NULL);
}

DMXConfigSubPtr dmxConfigSubWall(DMXConfigWallPtr wall)
{
    return dmxConfigCreateSub(dmxConfigWall, NULL, NULL, wall, NULL, NULL);
}

DMXConfigSubPtr dmxConfigSubOption(DMXConfigOptionPtr option)
{
    return dmxConfigCreateSub(dmxConfigOption, NULL, NULL, NULL, option, NULL);
}

DMXConfigSubPtr dmxConfigSubParam(DMXConfigParamPtr param)
{
    return dmxConfigCreateSub(dmxConfigParam, NULL, NULL, NULL, NULL, param);
}

extern DMXConfigSubPtr dmxConfigAddSub(DMXConfigSubPtr head,
                                       DMXConfigSubPtr sub)
{
    DMXConfigSubPtr pt;
    
    if (!head) return sub;
    for (pt = head; pt->next; pt = pt->next);
    pt->next = sub;
    return head;
}

DMXConfigVirtualPtr dmxConfigCreateVirtual(DMXConfigTokenPtr pStart,
                                           DMXConfigStringPtr pName,
                                           DMXConfigPairPtr pDim,
                                           DMXConfigTokenPtr pOpen,
                                           DMXConfigSubPtr pSubentry,
                                           DMXConfigTokenPtr pClose)
{
    DMXConfigVirtualPtr pVirtual = dmxConfigAlloc(sizeof(*pVirtual));

    pVirtual->start    = pStart;
    pVirtual->vname    = pName;
    pVirtual->dim      = pDim;
    pVirtual->open     = pOpen;
    pVirtual->subentry = pSubentry;
    pVirtual->close    = pClose;

    pVirtual->name     = pName ? pName->string : NULL;
    pVirtual->width    = pDim ? pDim->x : 0;
    pVirtual->height   = pDim ? pDim->y : 0;
    
    return pVirtual;
}

void dmxConfigFreeVirtual(DMXConfigVirtualPtr virtual)
{
    dmxConfigFreeToken(virtual->start);
    dmxConfigFreeString(virtual->vname);
    dmxConfigFreePair(virtual->dim);
    dmxConfigFreeToken(virtual->open);
    dmxConfigFreeSub(virtual->subentry);
    dmxConfigFreeToken(virtual->close);
    dmxConfigFree(virtual);
}

DMXConfigEntryPtr dmxConfigCreateEntry(DMXConfigType type,
                                       DMXConfigCommentPtr comment,
                                       DMXConfigVirtualPtr virtual)
{
    DMXConfigEntryPtr pEntry = dmxConfigAlloc(sizeof(*pEntry));
    pEntry->type = type;
    switch (type) {
    case dmxConfigComment: pEntry->comment = comment;                break;
    case dmxConfigVirtual: pEntry->virtual = virtual;                break;
    default: dmxConfigLog("Type %d not supported in entry\n", type); break;
    }
    return pEntry;
}

void dmxConfigFreeEntry(DMXConfigEntryPtr entry)
{
    DMXConfigEntryPtr pt;

    for (pt = entry; pt; pt = pt->next) {
        switch (pt->type) {
        case dmxConfigComment: dmxConfigFreeComment(pt->comment); break;
        case dmxConfigVirtual: dmxConfigFreeVirtual(pt->virtual); break;
        default:
            dmxConfigLog("Type %d not supported in entry\n", pt->type);
            break;
        }
    }
    dmxConfigFree(entry);
}

DMXConfigEntryPtr dmxConfigAddEntry(DMXConfigEntryPtr head,
                                    DMXConfigType type,
                                    DMXConfigCommentPtr comment,
                                    DMXConfigVirtualPtr virtual)
{
    DMXConfigEntryPtr child = dmxConfigCreateEntry(type, comment, virtual);
    DMXConfigEntryPtr pt;

    if (!head) return child;

    for (pt = head; pt->next; pt = pt->next);
    pt->next = child;

    return head;
}

DMXConfigEntryPtr dmxConfigEntryComment(DMXConfigCommentPtr comment)
{
    return dmxConfigCreateEntry(dmxConfigComment, comment, NULL);
}

DMXConfigEntryPtr dmxConfigEntryVirtual(DMXConfigVirtualPtr virtual)
{
    return dmxConfigCreateEntry(dmxConfigVirtual, NULL, virtual);
}