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
|
/* $Xorg: PParse.c,v 1.4 2001/02/09 02:05:58 xorgcvs Exp $ */
/*
Copyright 1996, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice 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 MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
ITY, 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.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
The Open Group.
*/
#include "RxI.h"
#define RX_DEFAULT_VERSION 1
#define RX_DEFAULT_REVISION 0
typedef struct {
char *string;
int length;
int index;
} Token;
/* macros to make token tables */
#define TOKEN(s, i) { s, sizeof(s) - 1, i }
#define NULLTOKEN { NULL, 0, 0 }
/* tables of tokens,
each token is made of a string and its associated enum value.
*/
static Token RxServices[] = {
TOKEN(RX_UI, UI),
TOKEN(RX_PRINT, Print),
NULLTOKEN
};
static Token RxUIs[] = {
TOKEN("X", XUI),
NULLTOKEN
};
static Token RxPrints[] = {
TOKEN("XPrint", XPrint),
NULLTOKEN
};
static Token RxXAuthentications[] = {
TOKEN("MIT-MAGIC-COOKIE-1", MitMagicCookie1),
NULLTOKEN
};
#ifdef NEED_STRCASECMP
/*
* in case str[n]casecmp are not provided by the system here are some
* which do the trick
*/
int
_RxStrcasecmp(register const char *s1, register const char *s2)
{
register int c1, c2;
while (*s1 && *s2) {
c1 = tolower(*s1);
c2 = tolower(*s2);
if (c1 != c2)
return (c1 - c2);
s1++;
s2++;
}
return (int) (*s1 - *s2);
}
int
_RxStrncasecmp(register const char *s1, register const char *s2, size_t n)
{
register int c1, c2;
const char *l1 = s1 + n;
const char *l2 = s2 + n;
while (*s1 && *s2 && s1 < l1 && s2 < l2) {
c1 = tolower(*s1);
c2 = tolower(*s2);
if (c1 != c2)
return (c1 - c2);
s1++;
s2++;
}
if (s1 < l1 && s2 < l2)
return (int) (*s1 - *s2);
else if (s1 < l1)
return (int) *s1;
else if (s2 < l2)
return (int) *s2;
return 0;
}
#endif
/* string copy functions */
static char *
strcopy(char *src)
{
char *cpy = (char *)Malloc(strlen(src) + 1);
if (cpy)
strcpy(cpy, src);
return cpy;
}
static char *
strncopy(char *src, int n)
{
char *cpy = (char *)Malloc(n + 1);
if (cpy) {
strncpy(cpy, src, n);
cpy[n] = '\0';
}
return cpy;
}
/* print out warning message */
#define WARNING(m, p) if (debug != 0) Warning(m, p)
static void
Warning(char *message, char *param)
{
fprintf(stderr, "Warning: %s%s\n", message, param);
}
#define WARNINGN(m, p, n) if (debug != 0) WarningN(m, p, n)
static void
WarningN(char *message, char *param, int param_len)
{
fprintf(stderr, "Warning: %s", message);
fwrite((void *)param, sizeof(char), param_len, stderr);
putc('\n', stderr);
}
/* print out error message */
static void
Error(char *message, char *param)
{
fprintf(stderr, "Error: %s%s\n", message, param);
}
/* look for a known token and return its index or 0 */
static int
LookForToken(char *string, Token *token)
{
for (; token->string != NULL; token++)
if (Strncasecmp(string, token->string, token->length) == 0)
return token->index;
return 0;
}
/* parse a comma separated list string: a,b,c
for each element look for a known token,
when found store the associated index in the given index table,
when not found print out a WARNING and skip,
terminate the index table with 0,
return number of recognized tokens.
Note that no array bound verification is done!
*/
static int
ParseList(char *value, Token *tokens, int *indices, int debug)
{
char *next;
int token;
int n = 0;
do {
token = LookForToken(value, tokens);
next = strchr(value, ',');
/* if unknown token print out WARNING */
if (token == 0) {
if (next != NULL) {
/* not a NULL terminated string, so make one */
char buf[BUFSIZ];
int len = (next - value < BUFSIZ) ? next - value : BUFSIZ;
strncpy(buf, value, len);
buf[len] = '\0';
WARNING("unknown parameter value: ", buf);
} else
WARNING("unknown parameter value: ", value);
} else
indices[n++] = token;
if (next != NULL)
value = next + 1;
} while (next);
indices[n] = 0;
return n;
}
/* Parse a comma separated list of pairs name[:value] such as: A:a,B,C:c
for each element look for a known token,
when found store the associated index in the given index table and copy
the associated value,
when not found print out a WARNING and skip,
terminate the index table with 0,
return number of recognized tokens.
Note that no array bound verification is done!
*/
static int
ParseAuthList(char *value, Token *tokens, int *indices, char **values,
int debug)
{
char *next, *ptr;
int token;
int n = 0;
do {
token = LookForToken(value, tokens);
ptr = strchr(value, ':');
next = strchr(((ptr != NULL) ? ptr : value), ',');
/* if unknown token print out WARNING */
if (token == 0) {
if (next != NULL) {
/* not a NULL terminated string, so make one */
char buf[BUFSIZ];
int len = (next - value < BUFSIZ) ? next - value : BUFSIZ;
strncpy(buf, value, len);
buf[len] = '\0';
WARNING("unknown parameter value: ", buf);
} else
WARNING("unknown parameter value: ", value);
} else {
indices[n] = token;
if (ptr != NULL) { /* there is an associated data */
if (next != NULL)
values[n++] = strncopy(ptr + 1, next - ptr);
else
values[n++] = strcopy(ptr + 1);
} else
values[n++] = NULL;
}
if (next != NULL)
value = next + 1;
} while (next);
indices[n] = 0;
return n;
}
/* parse a boolean string value returning 0 on success, 1 otherwise */
static int
ParseBoolean(char *strvalue, RxBool *value_ret)
{
if (Strcasecmp(strvalue, RX_YES) == 0) {
*value_ret = RxTrue;
return 0;
} else if (Strcasecmp(strvalue, RX_NO) == 0) {
*value_ret = RxFalse;
return 0;
}
return 1;
}
/* parse an X-UI-INPUT-METHOD parameter value */
static int
ParseXInputMethod(char *value,
RxBool *input_method_ret, char **input_method_url_ret,
int debug)
{
char *url;
url = strchr(value, ';');
if (url != NULL) {
if (strncmp(value, RX_YES, sizeof(RX_YES) - 1) == 0) {
*input_method_ret = RxTrue;
*input_method_url_ret = strcopy(url + 1);
} else if (strncmp(value, RX_YES, sizeof(RX_YES) - 1) == 0)
*input_method_ret = RxFalse;
else
WARNINGN("not a boolean value: ", value, url - value);
} else {
if (ParseBoolean(value, input_method_ret) != 0)
WARNING("not a boolean value: ", value);
}
return 0;
}
/* parse an RX parameter */
static int
ParseParam(char *name, char *value, RxParams *params, int debug)
{
if (Strcasecmp(name, RX_ACTION) == 0) {
Free(params->action);
params->action = strcopy(value);
} else if (Strcasecmp(name, RX_EMBEDDED) == 0) {
if (ParseBoolean(value, ¶ms->embedded) != 0)
WARNING("not a boolean value: ", value);
} else if (Strcasecmp(name, RX_AUTO_START) == 0) {
if (ParseBoolean(value, ¶ms->auto_start) != 0)
WARNING("not a boolean value: ", value);
} else if (Strcasecmp(name, RX_WIDTH) == 0)
params->width = atoi(value);
else if (Strcasecmp(name, RX_HEIGHT) == 0)
params->height = atoi(value);
else if (Strcasecmp(name, RX_APP_GROUP) == 0) {
Free(params->app_group);
params->app_group = strcopy(value);
} else if (Strcasecmp(name, RX_REQUIRED_SERVICES) == 0)
ParseList(value, RxServices, (int*)params->required_services, debug);
else if (Strcasecmp(name, RX_UI) == 0)
ParseList(value, RxUIs, (int*)params->ui, debug);
else if (Strcasecmp(name, RX_PRINT) == 0)
ParseList(value, RxPrints, (int*)params->print, debug);
else if (Strcasecmp(name, RX_X_UI_INPUT_METHOD) == 0)
ParseXInputMethod(value, ¶ms->x_ui_input_method,
¶ms->x_ui_input_method_url, debug);
else if (Strcasecmp(name, RX_X_AUTH) == 0)
ParseAuthList(value, RxXAuthentications,
(int*)params->x_auth, params->x_auth_data, debug);
else if (Strcasecmp(name, RX_X_UI_AUTH) == 0)
ParseAuthList(value, RxXAuthentications,
(int*)params->x_ui_auth, params->x_ui_auth_data, debug);
else if (Strcasecmp(name, RX_X_PRINT_AUTH) == 0)
ParseAuthList(value, RxXAuthentications,
(int*)params->x_print_auth, params->x_print_auth_data,
debug);
else if (Strcasecmp(name, RX_X_UI_LBX_AUTH) == 0)
ParseAuthList(value, RxXAuthentications,
(int*)params->x_ui_lbx_auth,
params->x_ui_lbx_auth_data, debug);
else if (Strcasecmp(name, RX_X_PRINT_LBX_AUTH) == 0)
ParseAuthList(value, RxXAuthentications,
(int*)params->x_print_lbx_auth,
params->x_print_lbx_auth_data,
debug);
else if (Strcasecmp(name, RX_X_UI_LBX) == 0) {
if (ParseBoolean(value, ¶ms->x_ui_lbx) != 0)
WARNING("not a boolean value: ", value);
} else if (Strcasecmp(name, RX_X_PRINT_LBX) == 0) {
if (ParseBoolean(value, ¶ms->x_print_lbx) != 0)
WARNING("not a boolean value: ", value);
} else /* unknown parameter */
WARNING("unknown parameter name: ", name);
return 0;
}
/* parse a list of RX arguments storing result in the given RxParams struct,
* since this function might be called several times using the same structure,
* it is assumed that the structure is correctly initialized.
*/
int
RxParseParams(char *argn[], char *argv[], int argc, RxParams *params,
int debug)
{
int i;
int version, revision;
if (argc == 0)
return 0;
/* the first param should be the Version number */
if (Strcasecmp(argn[0], RX_VERSION) == 0) {
if (sscanf(argv[0], "%d.%d", &version, &revision) == 2) {
params->version = version;
params->revision = revision;
} else {
Error("invalid version identifier: ", argv[0]);
return 1;
}
argn++;
argv++;
i = 1;
} else {
/* no version identifier assume 1.0 */
params->version = RX_DEFAULT_VERSION;
params->revision = RX_DEFAULT_REVISION;
i = 0;
}
/* parse given params */
for (; i < argc; i++, argn++, argv++)
if (ParseParam(*argn, *argv, params, debug))
return 1;
return 0;
}
/* Initialize RxParams structure */
void
RxInitializeParams(RxParams *params)
{
memset(params, 0, sizeof(RxParams));
params->embedded = RxUndef;
params->auto_start = RxUndef;
params->width = RxUndef;
params->height = RxUndef;
/* init X params */
params->x_ui_input_method = RxUndef;
params->x_ui_input_method_url = NULL;
params->x_ui_lbx = RxUndef;
params->x_print_lbx = RxUndef;
}
static void
FreeAuthListData(char **list)
{
while (*list != NULL)
Free(*list++);
}
/* Free data stored in RxParams structure */
int
RxFreeParams(RxParams *params)
{
Free(params->action);
Free(params->app_group);
Free(params->x_ui_input_method_url);
FreeAuthListData(params->x_auth_data);
FreeAuthListData(params->x_ui_auth_data);
FreeAuthListData(params->x_print_auth_data);
FreeAuthListData(params->x_ui_lbx_auth_data);
FreeAuthListData(params->x_print_lbx_auth_data);
return 0;
}
/* Free data stored in RxReturnParams structure */
int
RxFreeReturnParams(RxReturnParams *params)
{
/* action is only a reference - do not free */
Free(params->ui);
Free(params->print);
Free(params->x_ui_lbx_auth);
Free(params->x_print_lbx_auth);
return 0;
}
|