Fix compiler warnings in dtsession
[oweals/cde.git] / cde / programs / dtsession / SmProp.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * (c) Copyright 1995 Digital Equipment Corporation.
25  * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
26  * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
27  * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
28  * (c) Copyright 1993, 1994, 1995 Novell, Inc. 
29  * (c) Copyright 1995 FUJITSU LIMITED.
30  * (c) Copyright 1995 Hitachi.
31  *
32  * $XConsortium: SmProp.c /main/4 1996/10/07 10:29:51 drk $
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <X11/Intrinsic.h>
38 #include <X11/SM/SMlib.h>
39 #include "SmXSMP.h"
40
41 /*
42  * Forward declarations
43  */
44 static SmPropValue * CopyPropValues (
45         int                     numPropValues,
46         SmPropValue             * thePropValues);
47
48
49 void
50 SetPropertiesProc (
51         SmsConn                 smsConn,
52         SmPointer               managerData,
53         int                     numProps,
54         SmProp                  **theProps)
55 {
56         ClientRecPtr            pClient = (ClientRecPtr) managerData;
57         PropertyRecPtr          pProp;
58         int                     i, j;
59         Boolean                 found;
60
61 #ifdef DEBUG
62         (void) printf ("Received SET PROPERTIES [%p]\n", smsConn);
63 #endif /* DEBUG */
64
65         if (numProps == 0)
66                 return;
67
68         for (i = 0; i < numProps; i++) {
69
70                 found = False;
71
72                 if (theProps[i]->num_vals == 0)
73                         continue;
74
75                 for (pProp = pClient->props; 
76                      pProp != NULL; 
77                      pProp = pProp->next) {
78                         
79                         if (!strcmp (theProps[i]->name, pProp->prop.name) &&
80                             !strcmp (theProps[i]->type, pProp->prop.type)) {
81                                 /* 
82                                  * Reuse this property but replace its
83                                  * property values
84                                  */
85
86                                 for (j = 0; j < pProp->prop.num_vals; j++) {
87                                         XtFree (pProp->prop.vals[j].value);
88                                 }
89
90                                 if (pProp->prop.num_vals > 0)
91                                         XtFree ((char *) pProp->prop.vals);
92
93                                 pProp->prop.vals = CopyPropValues (
94                                         theProps[i]->num_vals, 
95                                         theProps[i]->vals);
96
97                                 pProp->prop.num_vals = theProps[i]->num_vals;
98
99                                 found = True;
100                                 break;
101                         }       
102                 }
103
104                 if (!found) {
105
106                         /*
107                          * Put it at the end of the list (or the beginning
108                          * if this client has no properties)
109                          */
110                         PropertyRecPtr          trail = pClient->props;
111                         for (pProp = pClient->props; 
112                              pProp != NULL; 
113                              trail = pProp, pProp = pProp->next);
114
115
116                         pProp = (PropertyRecPtr)XtMalloc (sizeof (PropertyRec));
117                         if (!pProp)
118                                 return;
119
120                         if (trail)
121                                 trail->next = pProp;
122                         else
123                                 pClient->props = pProp;
124
125                         pProp->prop.name = XtNewString (theProps[i]->name);
126                         pProp->prop.type = XtNewString (theProps[i]->type);
127                         pProp->prop.num_vals = theProps[i]->num_vals;
128                         pProp->next = NULL;
129
130                         pProp->prop.vals = CopyPropValues (
131                                 theProps[i]->num_vals, theProps[i]->vals);
132                 }
133
134                 SmFreeProperty (theProps[i]);
135         }
136
137         if (theProps)
138                 free ((char *) theProps);
139
140 #ifdef DEBUG
141         for (i = 0, pProp = pClient->props; 
142              pProp != NULL; 
143              i++, pProp = pProp->next) {
144                 (void) printf ("\t[%2d] name = %s\n", i + 1, 
145                         pProp->prop.name);
146                 (void) printf ("\t[%2d] type = %s\n", i + 1, 
147                         pProp->prop.type);
148                 (void) printf ("\t[%2d] num props = %d\n", i + 1, 
149                         pProp->prop.num_vals);
150                 for (j = 0; j < pProp->prop.num_vals; j++) {
151                         (void) printf ("\t\t [%2d] (%3d bytes) = %s\n", j + 1,
152                                 pProp->prop.vals[j].length,
153                                 pProp->prop.vals[j].value);
154                 }
155         }
156 #endif /* DEBUG */
157 }
158
159
160 void 
161 DeletePropertiesProc (
162         SmsConn                 smsConn,
163         SmPointer               managerData,
164         int                     numProps,
165         char                    **propNames)
166 {
167         ClientRecPtr            pClient = (ClientRecPtr) managerData;
168         int                     i;
169
170 #ifdef DEBUG
171         (void) printf ("Received DELETE PROPERTIES [%p] - %d properties\n", 
172                         smsConn, numProps);
173         for (i = 0; i < numProps; i++)
174                 (void) printf ("\t%s\n", propNames[i]);
175 #endif /* DEBUG */
176
177         if (numProps == 0)
178                 return;
179
180         for (i = 0; i < numProps; i++) {
181
182                 PropertyRecPtr          pProp;
183                 PropertyRecPtr          tmp;
184                 PropertyRecPtr          trail;
185
186                 for (pProp = pClient->props, trail = pClient->props ; 
187                      pProp != NULL;
188                      trail = pProp, pProp = pProp->next) {
189
190                         if (!strcmp (pProp->prop.name, propNames[i])) {
191
192                                 int             j;
193
194                                 XtFree (pProp->prop.name);
195                                 XtFree (pProp->prop.type);
196
197                                 for (j = 0; j < pProp->prop.num_vals; j++) {
198                                         XtFree (pProp->prop.vals[j].value);
199                                 }
200
201                                 if (pProp->prop.num_vals > 0)
202                                         XtFree ((char *) pProp->prop.vals);
203                                 /*
204                                  * Remove the record from the list and then
205                                  * free the record
206                                  */
207                                 trail->next = pProp->next;
208                                 XtFree ((char *) pProp);
209                                 pProp = trail;
210                         }
211                 }
212                 
213                 free (propNames[i]);
214         }
215
216         free ((char *) propNames);
217 }
218
219
220 void
221 GetPropertiesProc (
222         SmsConn                 smsConn,
223         SmPointer               managerData)
224 {
225         ClientRecPtr            pClient = (ClientRecPtr) managerData;
226         PropertyRecPtr          pProp;
227         PropertyRecPtr          trail;
228         int                     numProps;
229         int                     i, j;
230         SmProp                  **pPropsRet;
231
232 #ifdef DEBUG
233         (void) printf ("Received GET PROPERTIES [%p]\n", smsConn);
234 #endif /* DEBUG */
235
236         if (!pClient->props) {
237                 SmsReturnProperties (smsConn, 0, NULL);
238                 return;
239         }
240
241         for (pProp = pClient->props, numProps = 0; 
242              pProp != NULL;
243              pProp = pProp->next, numProps++);
244
245         pPropsRet = (SmProp **) XtMalloc (numProps * sizeof (SmProp *));
246
247         if (!pPropsRet) {
248                 SmsReturnProperties (smsConn, 0, NULL);
249                 return;
250         }
251
252         /*
253          * This function must return an array of pointers to SmProp
254          * structures and since the properties are actually stored
255          * in a linked list, a transformation is required.
256          */
257         for (i=0, pProp = pClient->props; 
258              pProp != NULL; 
259              i++, pProp = pProp->next) {
260
261                 pPropsRet[i] = (SmProp *) XtMalloc (sizeof (SmProp));
262                 if (!pPropsRet[i]) {
263                         SmsReturnProperties (smsConn, 0, NULL);
264                         return;
265                 }
266
267                 pPropsRet[i]->name = XtNewString (pProp->prop.name);
268                 pPropsRet[i]->type = XtNewString (pProp->prop.type);
269                 pPropsRet[i]->num_vals = pProp->prop.num_vals;
270                 pPropsRet[i]->vals = (SmPropValue *) XtMalloc (
271                         pProp->prop.num_vals * sizeof (SmPropValue));
272
273                 if (!pPropsRet[i]->vals) {
274                         SmsReturnProperties (smsConn, 0, NULL);
275                         return;
276                 }
277
278                 for (j = 0; j < pProp->prop.num_vals; j++) {
279
280                         pPropsRet[i]->vals[j].length = 
281                                 pProp->prop.vals[j].length;
282                         pPropsRet[i]->vals[j].value = (SmPointer)
283                                 XtMalloc (pProp->prop.vals[j].length);
284
285                         if (!pPropsRet[i]->vals[j].value) {
286                                 SmsReturnProperties (smsConn, 0, NULL);
287                                 return;
288                         }
289
290                         memcpy (pPropsRet[i]->vals[j].value,
291                                 pProp->prop.vals[j].value,
292                                 pProp->prop.vals[j].length);
293                 }
294         }
295
296         SmsReturnProperties (smsConn, numProps, pPropsRet);
297
298 #ifdef DEBUG
299         for (i = 0; i < numProps; i++) {
300                 (void) printf ("\t[%2d] name = %s\n", i + 1, 
301                         pPropsRet[i]->name);
302                 (void) printf ("\t[%2d] type = %s\n", i + 1, 
303                         pPropsRet[i]->type);
304                 (void) printf ("\t[%2d] num props = %d\n", i + 1, 
305                         pPropsRet[i]->num_vals);
306                 for (j = 0; j < pPropsRet[i]->num_vals; j++) {
307                         (void) printf ("\t\t [%2d] (%3d bytes) = %s\n", j + 1,
308                                 pPropsRet[i]->vals[j].length,
309                                 pPropsRet[i]->vals[j].value);
310                 }
311         }
312 #endif /* DEBUG */
313
314         for (i = 0; i < numProps; i++)
315                 SmFreeProperty (pPropsRet[i]);
316
317         XtFree ((char *) pPropsRet);
318 }
319
320
321 static SmPropValue *
322 CopyPropValues (
323         int                     numPropValues,
324         SmPropValue             * thePropValues)
325 {
326         SmPropValue             * pPropValueRet;
327         int                     i;
328
329         pPropValueRet = (SmPropValue *) XtMalloc (numPropValues *
330                   sizeof (SmPropValue));
331
332         if (!pPropValueRet)
333                 return (NULL);
334
335         for (i = 0; i < numPropValues; i++) {
336
337                 pPropValueRet[i].length = thePropValues[i].length;
338                 pPropValueRet[i].value = (SmPointer) 
339                         XtMalloc(thePropValues[i].length + 1);
340
341                 if (!pPropValueRet[i].value)
342                         return (NULL);
343
344                 memcpy (pPropValueRet[i].value,
345                         thePropValues[i].value,
346                         thePropValues[i].length);
347
348                 ((char *) pPropValueRet[i].value)[pPropValueRet[i].length]='\0';
349         }
350
351         return (pPropValueRet);
352 }
353
354
355 /*
356  * Returns a ptr to the property record list for the given
357  * property.
358  */
359 PropertyRecPtr
360 GetPropertyRec (
361         ClientRecPtr            pClientRec,
362         char                    * propName)
363 {
364         PropertyRecPtr          pPropRec;
365
366         for (pPropRec = pClientRec->props; pPropRec; 
367                         pPropRec = pPropRec->next) {
368
369                 if (!strcmp (propName, pPropRec->prop.name))
370                         return (pPropRec);
371
372         }
373
374         return (pPropRec);
375 }
376
377
378 /*
379  * If the given property exists, it returns a ptr to the first
380  * value of the property if the type of the property is SmARRAY8
381  * (char *).
382  */
383 char *
384 GetArrayPropertyValue (
385         ClientRecPtr            pClientRec,
386         char                    * propName)
387 {
388         PropertyRecPtr          pPropRec;
389
390         if ((pPropRec = GetPropertyRec (pClientRec, propName)) == NULL)
391                 return (NULL);
392
393         if ((!strcmp (pPropRec->prop.type, SmARRAY8)) &&
394             (pPropRec->prop.num_vals >= 1))
395                 return (pPropRec->prop.vals[0].value);
396
397         return (NULL);
398 }
399
400
401 /*************************************<->*************************************
402  *
403  *  GetListOfArrayPropertyValue -
404  *
405  *  Description: If the specified property exits and its type is
406  *      a LISTofARRAY8, then a NULL-terminated array of pointers
407  *      to the values is returned.
408  *
409  *  Returns: NULL-terminated array of values or NULL
410  *
411  *  Comments: caller is responsible for using XtFree to free the returned
412  *      pointer if it is not NULL
413  *
414  *************************************<->***********************************/
415 char **
416 GetListOfArrayPropertyValue (
417         ClientRecPtr            pClientRec,
418         char                    * propName)
419 {
420         PropertyRecPtr          pPropRec;
421         char                    ** ppchar;
422         int                     i;
423
424         if ((pPropRec = GetPropertyRec (pClientRec, propName)) == NULL)
425                 return (NULL);
426
427         if ((strcmp (pPropRec->prop.type, SmLISTofARRAY8)) ||
428                         (pPropRec->prop.num_vals <= 0))
429                 return (NULL);
430
431         ppchar = (char **) XtMalloc ((pPropRec->prop.num_vals + 1) *
432                                    sizeof (char *));
433         if (!ppchar)
434                 return (NULL);
435
436         for (i = 0; i < pPropRec->prop.num_vals; i++)
437                 ppchar[i] = pPropRec->prop.vals[i].value;
438
439         ppchar[pPropRec->prop.num_vals] = NULL;
440
441         return (ppchar);
442 }
443
444
445 /*************************************<->*************************************
446  *
447  *  GetCardPropertyValue -
448  *
449  *  Description: If the specified property exits and its type is
450  *      a CARD8, then return its value.
451  *
452  *  Returns: True if the property is found; False otherwise
453  *
454  *************************************<->***********************************/
455 Boolean
456 GetCardPropertyValue (
457         ClientRecPtr            pClientRec,
458         char                    * propName,
459         int                     * propValue)            /* MODIFIED */
460 {
461         PropertyRecPtr          pPropRec;
462
463         if ((pPropRec = GetPropertyRec (pClientRec, propName)) == NULL)
464                 return (False);
465
466         if ((!strcmp (pPropRec->prop.type, SmCARD8)) &&
467             (pPropRec->prop.num_vals >= 1)) {
468                 int     hint =  (int) *((char *) (pPropRec->prop.vals[0].value));
469                 *propValue = hint;
470                 return (True);
471         }
472
473         return (False);
474 }