File: Symbols.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (531 lines) | stat: -rw-r--r-- 13,006 bytes parent folder | download | duplicates (3)
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
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.2  2006/10/02 14:24:58  andre
 * AA-2006-10-02-0: Enhancem:  o jgdi qlimit
 *                             o sge_error_verror()
 *                             o japi segv
 *                             o copyright comments
 *                  Review:    RH
 *
 * Revision 1.1.1.1  2001/07/18 11:06:03  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <Xmt/Xmt.h>
#include <Xmt/Symbols.h>
#include <Xmt/Hash.h>
#include <Xmt/QuarksP.h>
#include <X11/IntrinsicP.h>

#if NeedFunctionPrototypes
        void _XtCopyToArg(XtPointer, XtArgVal *, Cardinal);
        void _XtCopyFromArg(XtArgVal, XtPointer, Cardinal);
#else
        void _XtCopyToArg();
        void _XtCopyFromArg();
#endif

/*
 * values for the XmtSymbolRec mode field.
 */
#define XmtSymbolABSOLUTE   1
#define XmtSymbolIMMEDIATE  2
#define XmtSymbolRESOURCE   3

static XmtHashTable symbol_table = NULL;


#if NeedFunctionPrototypes
static void GetResourceTypeAndSize(Widget w, String resource_name,
				   XrmQuark *type, Cardinal *size)
#else
static void GetResourceTypeAndSize(w, resource_name, type, size)
Widget w;
String resource_name;
XrmQuark *type;
Cardinal *size;
#endif
{
    int i;
    Boolean found = False;
    WidgetClass c = XtClass(w);
    XtResourceList *list = (XtResourceList *) c->core_class.resources;
    XrmQuark name = XrmStringToQuark(resource_name);
    
    /* check normal resources first */
    /* Note that we assume knowledge of the compiled resource format */
    for(i=0; i < c->core_class.num_resources; i++) {
	if ((list[i] != NULL) &&
	    ((XrmName)list[i]->resource_name == name)) {
	    found = True;
	    break;
	}		
    }
    
    /* check constraint resources if parent is constraint */
    if (!found && XtParent(w) && XtIsConstraint(XtParent(w))) {
	ConstraintWidgetClass c =
	    (ConstraintWidgetClass) XtClass(XtParent(w));
	list = (XtResourceList *) c->constraint_class.resources;
	for(i = 0; i < c->constraint_class.num_resources; i++) {
	    if ((list[i] != NULL) &&
		((XrmName)list[i]->resource_name == name)) {
		found = True;
		break;
	    }
	}
    }

    if (found) {
	*type = (XrmQuark)list[i]->resource_type;
	*size = list[i]->resource_size;
    }
}

/*
 * s must be in static memory, or at least in memory that will never
 * be freed.  s->name must be the same.  For statically declared arrays
 * of symbols this is the usual case.  For dynamically created symbols,
 * the programmer must make a copy of the name in allocated memory.
 */
#if NeedFunctionPrototypes
void XmtRegisterSymbol(XmtSymbol s)
#else
void XmtRegisterSymbol(s)
XmtSymbol s;
#endif
{
    XrmName name;

    if (symbol_table == NULL)
	symbol_table = XmtHashTableCreate(3);

    if (s->addr == 0) {  /* symbol is an immediate value */
	/*
	 * if size > sizeof(XtPointer), an immediate value won't
	 * actually fit, so issue error msg. and exit.
	 */
	if (s->size > sizeof(s->addr))
	    XmtErrorMsg("XmtSymbol", "size",
			"Symbol '%s' has size too big for immediate mode.",
			s->name);
	s->mode = XmtSymbolIMMEDIATE;
	s->type = (String)XrmStringToQuark(s->type);
    }
    else if (s->resource_name) {  /* symbol is a widget resource */
	GetResourceTypeAndSize((Widget)s->addr, s->resource_name,
			       (XrmQuark *) &s->type, &s->size);
	s->mode = XmtSymbolRESOURCE;
    }
    else {
	s->mode = XmtSymbolABSOLUTE;
	s->type = (String)XrmStringToQuark(s->type);
    }

    name = XrmPermStringToQuark(s->name);
    XmtHashTableStore(symbol_table, (XtPointer)name, (XtPointer)s);
}


/*
 * The symbol records and the string within must be permanently allocated
 */
#if NeedFunctionPrototypes
void XmtRegisterSymbols(XmtSymbolRec *syms, Cardinal num_syms)
#else
void XmtRegisterSymbols(syms, num_syms)
XmtSymbolRec *syms;
Cardinal num_syms;
#endif
{
    int i;
    for(i=0; i < num_syms; i++)	XmtRegisterSymbol(&syms[i]);
}

/*
 * All strings must be permanently allocated, and may not change
 */
#if NeedVarargsPrototypes
void XmtVaRegisterSymbols(StringConst name, ...)
#else
void XmtVaRegisterSymbols(name, va_alist)
StringConst name;
va_dcl
#endif
{
    va_list var;
    XmtSymbol sym;

    Va_start(var, name);
    
    while(name) {
	sym = (XmtSymbol) XtCalloc(1, sizeof(XmtSymbolRec));
	sym->name = (String) name;
	sym->type = va_arg(var, String);
	sym->size = va_arg(var, int);
	sym->addr = va_arg(var, XtPointer);
	XmtRegisterSymbol(sym);
	name = va_arg(var, String);
    }

    va_end(var);
}

#if NeedFunctionPrototypes
void XmtRegisterResourceList(XtPointer base,
			     XtResourceList resources, Cardinal num_resources)
#else
void XmtRegisterResourceList(base, resources, num_resources)
XtPointer base;
XtResourceList resources;
Cardinal num_resources;
#endif
{
    int i;
    XmtSymbolRec *syms = (XmtSymbolRec *)XtCalloc(num_resources,
						  sizeof(XmtSymbolRec));

    /*
     * This procedure knows about the Intrinsics compiled resource format.
     * This is a little dangerous.
     */
    if (((int) resources[0].resource_offset) >= 0) { /* uncompiled */
	for(i=0; i < num_resources; i++) {
	    syms[i].name = resources[i].resource_name;
	    syms[i].type = resources[i].resource_type;
	    syms[i].size = resources[i].resource_size;
	    syms[i].addr = (XtPointer) 
		(resources[i].resource_offset + (char *)base);
	}
    }
    else {  /* compiled */
#define tostring(s) XrmQuarkToString((XrmQuark)s)	
	for(i=0; i < num_resources; i++) {
	    syms[i].name = tostring(resources[i].resource_name);
	    syms[i].type = tostring(resources[i].resource_type);
	    syms[i].size = resources[i].resource_size;
	    syms[i].addr = (XtPointer)
		(-resources[i].resource_offset - 1 + (char *)base);
	}
#undef tostring
    }

    XmtRegisterSymbols(syms, num_resources);
}

#if NeedFunctionPrototypes
void XmtRegisterWidgetResource(StringConst name, Widget w, StringConst res)
#else
void XmtRegisterWidgetResource(name, w, res)
StringConst name;
Widget w;
StringConst res;
#endif
{
    XmtSymbol s = (XmtSymbol) XtCalloc(1, sizeof(XmtSymbolRec));

    s->name = (String) name;
    s->addr = (XtPointer) w;
    s->resource_name = (String) res;
    XmtRegisterSymbol(s);
}


#if NeedFunctionPrototypes
XmtSymbol XmtLookupSymbol(StringConst name)
#else
XmtSymbol XmtLookupSymbol(name)
StringConst name;
#endif
{
    XmtSymbol s;
    XrmQuark q = XrmStringToQuark(name);

    if (!symbol_table) return NULL;
    if (XmtHashTableLookup(symbol_table,(XtPointer)q, (XtPointer *)&s))
	return s;
    else
	return NULL;
}

#if NeedFunctionPrototypes
void XmtSymbolSetValue(XmtSymbol s, XtArgVal value)
#else
void XmtSymbolSetValue(s, value)
XmtSymbol s;
XtArgVal value;
#endif
{
    XmtSymbolCallbackRec *p;
    int index;

    /*
     * Special case for XmtRBuffer type symbols.
     * we copy the NULL-terminated string, and warn if it is too
     * large for the buffer.
     * XXX
     * we should do sanity checking on buffer symbols.  They can only
     * have ABSOLUTE addressing mode.
     */
    if (s->type == (String) XmtQBuffer) {
	int len = strlen((char *)value);
	if (len < s->size)
	    strcpy((char *)s->addr, (char *) value);
	else {
	    strncpy((char *)s->addr, (char *)value, s->size-1);
	    ((char *)s->addr)[s->size] = '\0';
	    XmtWarningMsg("XmtSymbolSetValue", "overflow",
		       "Symbol '%s': buffer overflow.\n\t%d characters lost.",
			  s->name, len - s->size +1);
	}
    }
    else {  /* anything other than buffer symbols */
	/*
	 * if this is an enumerated symbol, then the new value is the
	 * index into the supplied array of values, not the value itself.
	 */
	if (s->num_values > 0) {
	    index = (unsigned int) value;
	    if ((index >= s->num_values) || (index < 0)) {
		XmtWarningMsg("XmtSymbolSetValue", "badValue",
			      "symbol '%s' has no enumerated value for %d.",
			      s->name, index);
		return;
	    }
	    s->index = (unsigned short)index;
	    value = (XtArgVal)s->values[index];
	}
	
	/* XXX
	 * machine dependencies?  Does this work on a Sparc?
	 */
	switch(s->mode) {
	case XmtSymbolABSOLUTE:
	    _XtCopyFromArg(value, s->addr, s->size);
	    break;
	case XmtSymbolIMMEDIATE:
	    _XtCopyFromArg(value, &s->addr, s->size);
	    break;
	case XmtSymbolRESOURCE:
	    XtVaSetValues((Widget)s->addr, s->resource_name, value, NULL);
	    break;
	}
    }

    /* 
     * call notify procs.
     */
    for(p = s->callbacks; p; p = p->next)
	(*p->proc)(s, p->closure, value);
}

#if NeedFunctionPrototypes
Boolean XmtSymbolSetTypedValue(Widget w, XmtSymbol s, String type,
			       XtArgVal value, Cardinal size)
#else
Boolean XmtSymbolSetTypedValue(w, s, type, value, size)
Widget w;
XmtSymbol s;
String type;
XtArgVal value;
Cardinal size;
#endif
{
    XrmQuark typeq = XrmStringToQuark(type);
    XrmValue from, to;
    XtArgVal converted_value;
    Boolean status;

    /*
     * if the types match, just call XmtSymbolSetValue().
     * if the symbol is a buffer, and the value a string, that is a match.
     */
    if ((typeq == (XrmQuark) s->type) ||
	((typeq == XmtQString) && (s->type == (String) XmtQBuffer))) {
	XmtSymbolSetValue(s, value);
	return True;
    }

    /*
     * otherwise attempt to convert the value.
     */
    from.size = size;
    if ((size > sizeof(XtArgVal)) ||
	(typeq == XmtQString) || (typeq == XmtQBuffer))
	from.addr = (XPointer) value;
    else
	from.addr = (XPointer) &value;
    /* let the converter return a value of the right type. */
    to.addr = NULL;  
    to.size = s->size;
    status = XtConvertAndStore(w, type, &from,
			       XrmQuarkToString((XrmQuark)s->type), &to);

    if (status) {
	converted_value = 0;  /* make func below store directly into us */
	_XtCopyToArg(to.addr, &converted_value, to.size);
	XmtSymbolSetValue(s, converted_value);
    }
    else {
	XmtWarningMsg("XmtSymbolSetTypedValue", "convert",
		      "Couldn't set symbol '%s'.", s->name);
    }

    return status;
}

#if NeedFunctionPrototypes
void XmtSymbolGetValue(XmtSymbol s, XtArgVal *valuep)
#else
void XmtSymbolGetValue(s, valuep)
XmtSymbol s;
XtArgVal *valuep;
#endif
{

    /*
     * Special case for XmtRBuffer type symbols.
     * Just return the address.
     */
    if (s->type == (String) XmtQBuffer) {
	*(char **)valuep = s->addr;
	return;
    }
    
    switch(s->mode) {
    case XmtSymbolABSOLUTE:
	_XtCopyToArg(s->addr, (XtArgVal *) &valuep, s->size);
	break;
    case XmtSymbolIMMEDIATE:
	_XtCopyToArg(&s->addr, (XtArgVal *) &valuep, s->size);
	break;
    case XmtSymbolRESOURCE:
	XtVaGetValues((Widget)s->addr, s->resource_name, valuep, NULL);
	break;
    }
}

#if NeedFunctionPrototypes
int XmtSymbolGetScalarValue(XmtSymbol s)
#else
int XmtSymbolGetScalarValue(s)
XmtSymbol s;
#endif
{
    if (s->num_values > 0) return (int)s->index;
    else 
	XmtWarningMsg("XmtSymbolGetScalarValue", "badSymbol",
		      "symbol '%s' is not an enumerated value.",
		      s->name);
    return -1;
}
	    

#if NeedFunctionPrototypes
void XmtSymbolAddCallback(XmtSymbol s, XmtSymbolCallbackProc proc,
			  XtPointer closure)
#else
void XmtSymbolAddCallback(s, proc, closure)
XmtSymbol s;
XmtSymbolCallbackProc proc;
XtPointer closure;
#endif
{
    XmtSymbolCallbackRec *cb = XtNew(XmtSymbolCallbackRec);
    XmtSymbolCallbackRec *p;

    cb->proc = proc;
    cb->closure = closure;
    cb->next = NULL;
    if (s->callbacks == NULL) s->callbacks = cb;
    else {
	for(p = s->callbacks; p->next; p = p->next);
	p->next = cb;
    }
}

#if NeedFunctionPrototypes
void XmtSymbolRemoveCallback(XmtSymbol s, XmtSymbolCallbackProc proc,
			     XtPointer closure)
#else
void XmtSymbolRemoveCallback(s, proc, closure)
XmtSymbol s;
XmtSymbolCallbackProc proc;
XtPointer closure;
#endif
{
    XmtSymbolCallbackRec *p, *q;

    for(q=NULL, p = s->callbacks; p; q=p, p = p->next)
	if ((p->proc == proc) && (p->closure == closure)) break;

    if (p) {
	if (!q) s->callbacks = p->next;
	else q->next = p->next;
	XtFree((char *)p);
    }
}

#if NeedFunctionPrototypes
String XmtSymbolName(XmtSymbol s)
#else
String XmtSymbolName(s)
XmtSymbol s;
#endif
{
    return s->name;
}

#if NeedFunctionPrototypes
int XmtSymbolSize(XmtSymbol s)
#else
int XmtSymbolSize(s)
XmtSymbol s;
#endif
{
    return s->size;
}
    
#if NeedFunctionPrototypes
XrmQuark XmtSymbolTypeQuark(XmtSymbol s)
#else
XrmQuark XmtSymbolTypeQuark(s)
XmtSymbol s;
#endif
{
    return (XrmQuark) s->type;
}

#if NeedFunctionPrototypes
String XmtSymbolType(XmtSymbol s)
#else
String XmtSymbolType(s)
XmtSymbol s;
#endif
{
    return XrmQuarkToString((XrmQuark)s->type);
}

#if NeedFunctionPrototypes
Boolean XmtSymbolIsEnumerated(XmtSymbol s)
#else
Boolean XmtSymbolIsEnumerated(s)
XmtSymbol s;
#endif
{
    return (s->num_values > 0);
}