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
|
/*
* 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
* to pretty-print DMX configurations.
*
* 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 "dmxconfig.h"
#include "dmxparse.h"
#include "dmxprint.h"
#include "parser.h"
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
static FILE *str = NULL;
static int indent = 0;
static int pos = 0;
/** Stack of indentation information used for pretty-printing
* configuration information. */
static struct stack {
int base;
int comment;
int step;
struct stack *next;
} *stack, initialStack = {
0, 0, 4, NULL};
static void
dmxConfigIndent(void)
{
int i;
if (indent < 0)
indent = 0;
if (indent > 40)
indent = 40;
for (i = 0; i < indent; i++)
fprintf(str, " ");
}
static void
dmxConfigNewline(void)
{
if (pos)
fprintf(str, "\n");
pos = 0;
}
static void
dmxConfigPushState(int base, int comment, int step)
{
struct stack *new = dmxConfigAlloc(sizeof(*new));
new->base = base;
new->comment = comment;
new->step = step;
new->next = stack;
stack = new;
indent = base;
dmxConfigNewline();
}
static void
dmxConfigPushComment(void)
{
if (stack)
indent = stack->comment;
}
static void
dmxConfigPushStep(void)
{
if (stack)
indent = stack->step;
}
static void
dmxConfigPopState(void)
{
struct stack *old = stack;
if (!stack)
return;
indent = old->base;
stack = old->next;
if (!stack)
dmxConfigLog("Stack underflow\n");
dmxConfigFree(old);
dmxConfigNewline();
}
static void
dmxConfigOutput(int addSpace, int doNewline, const char *comment,
const char *format, ...)
{
va_list args;
if (!pos)
dmxConfigIndent();
else if (addSpace)
fprintf(str, " ");
if (format) {
va_start(args, format);
/* RATS: This hasn't been audited -- it
* could probably result in a buffer
* overflow. */
pos += vfprintf(str, format, args); /* assumes no newlines! */
va_end(args);
}
if (comment) {
if (pos)
fprintf(str, " ");
pos += fprintf(str, "#%s", comment);
dmxConfigNewline();
dmxConfigPushComment();
}
else if (doNewline)
dmxConfigNewline();
}
static void
dmxConfigPrintComment(DMXConfigCommentPtr p)
{
dmxConfigOutput(1, 1, p->comment, NULL);
}
static void
dmxConfigPrintTokenFlag(DMXConfigTokenPtr p, int flag)
{
if (!p)
return;
switch (p->token) {
case T_VIRTUAL:
dmxConfigPushState(0, 4, 4);
dmxConfigOutput(0, 0, p->comment, "virtual");
break;
case T_DISPLAY:
dmxConfigPushState(4, 12, 16);
dmxConfigOutput(0, 0, p->comment, "display");
break;
case T_WALL:
dmxConfigPushState(4, 12, 16);
dmxConfigOutput(0, 0, p->comment, "wall");
break;
case T_OPTION:
dmxConfigPushState(4, 12, 16);
dmxConfigOutput(0, 0, p->comment, "option");
break;
case T_PARAM:
dmxConfigPushState(4, 8, 12);
dmxConfigOutput(0, 0, p->comment, "param");
break;
case ';':
dmxConfigOutput(0, 1, p->comment, ";");
if (flag)
dmxConfigPopState();
break;
case '{':
dmxConfigOutput(1, 1, p->comment, "{");
dmxConfigPushStep();
break;
case '}':
if (flag)
dmxConfigPopState();
dmxConfigOutput(0, 1, p->comment, "}");
break;
case '/':
dmxConfigOutput(1, 0, NULL, "/");
break;
default:
dmxConfigLog("unknown token %d on line %d\n", p->token, p->line);
}
}
static void
dmxConfigPrintToken(DMXConfigTokenPtr p)
{
dmxConfigPrintTokenFlag(p, 1);
}
static void
dmxConfigPrintTokenNopop(DMXConfigTokenPtr p)
{
dmxConfigPrintTokenFlag(p, 0);
}
static int
dmxConfigPrintQuotedString(const char *s)
{
const char *pt;
if (!s || !s[0])
return 1; /* Quote empty string */
for (pt = s; *pt; ++pt)
if (isspace(*pt))
return 1;
return 0;
}
static void
dmxConfigPrintString(DMXConfigStringPtr p, int quote)
{
DMXConfigStringPtr pt;
if (!p)
return;
for (pt = p; pt; pt = pt->next) {
if (quote && dmxConfigPrintQuotedString(pt->string)) {
dmxConfigOutput(1, 0, pt->comment, "\"%s\"",
pt->string ? pt->string : "");
}
else
dmxConfigOutput(1, 0, pt->comment, "%s",
pt->string ? pt->string : "");
}
}
static int
dmxConfigPrintPair(DMXConfigPairPtr p, int addSpace)
{
const char *format = NULL;
if (!p)
return 0;
switch (p->token) {
case T_ORIGIN:
format = "@%dx%d";
break;
case T_DIMENSION:
format = "%dx%d";
break;
case T_OFFSET:
format = "%c%d%c%d";
break;
}
if (p->token == T_OFFSET) {
if (!p->comment && !p->x && !p->y && p->xsign >= 0 && p->ysign >= 0)
return 0;
dmxConfigOutput(addSpace, 0, p->comment, format,
p->xsign < 0 ? '-' : '+', p->x,
p->ysign < 0 ? '-' : '+', p->y);
}
else {
if (!p->comment && !p->x && !p->y)
return 0;
dmxConfigOutput(addSpace, 0, p->comment, format, p->x, p->y);
}
return 1;
}
static void
dmxConfigPrintDisplay(DMXConfigDisplayPtr p)
{
DMXConfigToken dummyStart = { T_DISPLAY, 0, NULL };
DMXConfigToken dummyEnd = { ';', 0, NULL };
DMXConfigToken dummySep = { '/', 0, NULL };
DMXConfigString dummyName = { T_STRING, 0, NULL, NULL, NULL };
DMXConfigPair dummySDim = { T_DIMENSION, 0, NULL, 0, 0, 0, 0 };
DMXConfigPair dummySOffset = { T_OFFSET, 0, NULL, 0, 0, 0, 0 };
DMXConfigPair dummyRDim = { T_DIMENSION, 0, NULL, 0, 0, 0, 0 };
DMXConfigPair dummyROffset = { T_OFFSET, 0, NULL, 0, 0, 0, 0 };
DMXConfigPair dummyOrigin = { T_ORIGIN, 0, NULL, 0, 0, 0, 0 };
int output;
if (p->dname)
p->dname->string = p->name;
else
dummyName.string = p->name;
if (p->dim && p->dim->scrn && p->dim->scrn->dim) {
p->dim->scrn->dim->x = p->scrnWidth;
p->dim->scrn->dim->y = p->scrnHeight;
}
else {
dummySDim.x = p->scrnWidth;
dummySDim.y = p->scrnHeight;
}
if (p->dim && p->dim->scrn && p->dim->scrn->offset) {
p->dim->scrn->offset->x = p->scrnX;
p->dim->scrn->offset->y = p->scrnY;
}
else {
dummySOffset.x = p->scrnX;
dummySOffset.y = p->scrnY;
}
if (p->dim && p->dim->root && p->dim->root->dim) {
p->dim->root->dim->x = p->rootWidth;
p->dim->root->dim->y = p->rootHeight;
}
else {
dummyRDim.x = p->rootWidth;
dummyRDim.y = p->rootHeight;
}
if (p->dim && p->dim->root && p->dim->root->offset) {
p->dim->root->offset->x = p->rootX;
p->dim->root->offset->y = p->rootY;
}
else {
dummyROffset.x = p->rootX;
dummyROffset.y = p->rootY;
}
if (p->origin) {
p->origin->x = p->rootXOrigin, p->origin->y = p->rootYOrigin;
p->origin->xsign = p->rootXSign, p->origin->ysign = p->rootYSign;
}
else {
dummyOrigin.x = p->rootXOrigin, dummyOrigin.y = p->rootYOrigin;
dummyOrigin.xsign = p->rootXSign, dummyOrigin.ysign = p->rootYSign;
}
dmxConfigPrintToken(p->start ? p->start : &dummyStart);
dmxConfigPrintString(p->dname ? p->dname : &dummyName, 1);
if (p->dim && p->dim->scrn && p->dim->scrn->dim)
output = dmxConfigPrintPair(p->dim->scrn->dim, 1);
else
output = dmxConfigPrintPair(&dummySDim, 1);
if (p->dim && p->dim->scrn && p->dim->scrn->offset)
dmxConfigPrintPair(p->dim->scrn->offset, !output);
else
dmxConfigPrintPair(&dummySOffset, !output);
if (p->scrnWidth != p->rootWidth
|| p->scrnHeight != p->rootHeight || p->rootX || p->rootY) {
dmxConfigPrintToken(&dummySep);
if (p->dim && p->dim->root && p->dim->root->dim)
output = dmxConfigPrintPair(p->dim->root->dim, 1);
else
output = dmxConfigPrintPair(&dummyRDim, 1);
if (p->dim && p->dim->root && p->dim->root->offset)
dmxConfigPrintPair(p->dim->root->offset, !output);
else
dmxConfigPrintPair(&dummyROffset, !output);
}
dmxConfigPrintPair(p->origin ? p->origin : &dummyOrigin, 1);
dmxConfigPrintToken(p->end ? p->end : &dummyEnd);
}
static void
dmxConfigPrintWall(DMXConfigWallPtr p)
{
dmxConfigPrintToken(p->start);
dmxConfigPrintPair(p->wallDim, 1);
dmxConfigPrintPair(p->displayDim, 1);
dmxConfigPrintString(p->nameList, 1);
dmxConfigPrintToken(p->end);
}
static void
dmxConfigPrintOption(DMXConfigOptionPtr p)
{
DMXConfigToken dummyStart = { T_OPTION, 0, NULL };
DMXConfigString dummyOption = { T_STRING, 0, NULL, NULL, NULL };
DMXConfigToken dummyEnd = { ';', 0, NULL };
dummyOption.string = p->string;
dmxConfigPrintToken(p->start ? p->start : &dummyStart);
dmxConfigPrintString(&dummyOption, 0);
dmxConfigPrintToken(p->end ? p->end : &dummyEnd);
}
static void
dmxConfigPrintParam(DMXConfigParamPtr p)
{
if (!p)
return;
if (p->start) {
if (p->open && p->close) {
dmxConfigPrintToken(p->start);
dmxConfigPrintToken(p->open);
dmxConfigPrintParam(p->next);
dmxConfigPrintToken(p->close);
}
else if (p->end && p->param) {
dmxConfigPrintToken(p->start);
dmxConfigPrintString(p->param, 1);
dmxConfigPrintToken(p->end);
}
else
dmxConfigLog("dmxConfigPrintParam: cannot handle format (a)\n");
}
else if (p->end && p->param) {
dmxConfigPrintString(p->param, 1);
dmxConfigPrintTokenNopop(p->end);
dmxConfigPrintParam(p->next);
}
else
dmxConfigLog("dmxConfigPrintParam: cannot handle format (b)\n");
}
static void
dmxConfigPrintSub(DMXConfigSubPtr p)
{
DMXConfigSubPtr pt;
if (!p)
return;
for (pt = p; pt; pt = pt->next) {
switch (pt->type) {
case dmxConfigComment:
dmxConfigPrintComment(pt->comment);
break;
case dmxConfigDisplay:
dmxConfigPrintDisplay(pt->display);
break;
case dmxConfigWall:
dmxConfigPrintWall(pt->wall);
break;
case dmxConfigOption:
dmxConfigPrintOption(pt->option);
break;
case dmxConfigParam:
dmxConfigPrintParam(pt->param);
break;
default:
dmxConfigLog("dmxConfigPrintSub:"
" cannot handle type %d in subentry\n", pt->type);
}
}
}
static void
dmxConfigPrintVirtual(DMXConfigVirtualPtr p)
{
DMXConfigToken dummyStart = { T_VIRTUAL, 0, NULL };
DMXConfigToken dummyOpen = { '{', 0, NULL };
DMXConfigToken dummyClose = { '}', 0, NULL };
DMXConfigString dummyName = { T_STRING, 0, NULL, NULL, NULL };
DMXConfigPair dummyDim = { T_DIMENSION, 0, NULL, 0, 0 };
if (p->vname)
p->vname->string = p->name;
else
dummyName.string = p->name;
if (p->dim)
p->dim->x = p->width, p->dim->y = p->height;
else
dummyDim.x = p->width, dummyDim.y = p->height;
dmxConfigPrintToken(p->start ? p->start : &dummyStart);
dmxConfigPrintString(p->vname ? p->vname : &dummyName, 1);
dmxConfigPrintPair(p->dim ? p->dim : &dummyDim, 1);
dmxConfigPrintToken(p->open ? p->open : &dummyOpen);
dmxConfigPrintSub(p->subentry);
dmxConfigPrintToken(p->close ? p->close : &dummyClose);
}
/** The configuration information in \a entry will be pretty-printed to
* the \a stream. If \a stream is NULL, then stdout will be used. */
void
dmxConfigPrint(FILE * stream, DMXConfigEntryPtr entry)
{
DMXConfigEntryPtr pt;
if (!stream)
str = stdout;
else
str = stream;
stack = &initialStack;
for (pt = entry; pt; pt = pt->next) {
switch (pt->type) {
case dmxConfigComment:
dmxConfigPrintComment(pt->comment);
break;
case dmxConfigVirtual:
dmxConfigPrintVirtual(pt->virtual);
break;
default:
dmxConfigLog("dmxConfigPrint: cannot handle type %d in entry\n",
pt->type);
}
}
if (pos)
dmxConfigNewline();
}
/** The configuration information in \a p will be pretty-printed to the
* \a stream. If \a stream is NULL, then stdout will be used. */
void
dmxConfigVirtualPrint(FILE * stream, DMXConfigVirtualPtr p)
{
if (!stream)
str = stdout;
else
str = stream;
stack = &initialStack;
dmxConfigPrintVirtual(p);
if (pos)
dmxConfigNewline();
}
|