dtcm: Coverity 88107
[oweals/cde.git] / cde / programs / dtwm / Parse.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 libraries 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  *
25  *   File:         Parse.c
26  *
27  *   Project:       CDE
28  *
29  *   Description:  This file contains the parsing functions for Front Panel
30  *                 file information.
31  *
32  * (c) Copyright 1993, 1994 Hewlett-Packard Company
33  * (c) Copyright 1993, 1994 International Business Machines Corp.
34  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
35  * (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of Novell, Inc.
36  *
37  *****************************************************************************/
38
39 #include <Dt/DtP.h>
40 #include <Dt/DbReader.h>
41 #include <Dt/UserMsg.h>
42
43 #include "WmGlobal.h"
44 #include "WmParse.h"
45 #include "DataBaseLoad.h"
46 #include "Parse.h"
47
48
49 /************************************************************************
50  *
51  *  StringToString
52  *
53  ************************************************************************/
54
55 Boolean
56 StringToString (char * parse_source,
57                 void ** parse_return)
58
59 {
60    *parse_return = (void *) strdup (parse_source);
61
62    return (True);
63 }
64
65
66
67
68 /************************************************************************
69  *
70  *  StrintToInt
71  *
72  ************************************************************************/
73
74 Boolean
75 StringToInt (char * parse_source,
76              void ** parse_return)
77
78 {
79    char * source_ptr = parse_source;
80    long    value = 0;
81    char   chr;
82
83
84    while (chr = *source_ptr++) 
85    {
86       if (chr >= '0' && chr <= '9') 
87       {
88          value *= 10;
89          value += chr - '0';
90       }
91       else
92       {
93          _DtSimpleError (panel.app_name, DtError, NULL, 
94                         "Invalid Integer -- %s", parse_source);
95          return (False);
96       }
97    }
98
99    *parse_return = (void *) value;
100    return (True);
101 }
102
103
104
105
106 /************************************************************************
107  *
108  *  StringToBoolean
109  *
110  ************************************************************************/
111
112 Boolean
113 StringToBoolean (char * parse_source,
114                  void ** parse_return)
115
116 {
117    _DtWmParseToLower(parse_source);
118
119    if (strcmp (parse_source, "true") == 0)
120       *parse_return = (void *) True;
121    else if (strcmp (parse_source, "false") == 0)
122       *parse_return = (void *) False;
123    else
124    {
125       _DtSimpleError (panel.app_name, DtError, NULL, 
126                      "Invalid Boolean -- %s", parse_source);
127       return (False);
128    }
129
130    return (True);
131 }
132
133
134
135
136 /************************************************************************
137  *
138  *  StringToResolution
139  *
140  ************************************************************************/
141
142 Boolean
143 StringToResolution (char * parse_source,
144                     void ** parse_return)
145
146 {
147    _DtWmParseToLower (parse_source);
148    
149    if (strcmp (parse_source, resolution_types[HIGH]) == 0)
150       *parse_return = (void *) HIGH;
151    else if (strcmp (parse_source, resolution_types[MEDIUM]) == 0)
152       *parse_return = (void *) MEDIUM;
153    else if (strcmp (parse_source, resolution_types[LOW]) == 0)
154       *parse_return = (void *) LOW;
155    else if (strcmp (parse_source, resolution_types[MATCH_DISPLAY]) == 0)
156       *parse_return = (void *) MATCH_DISPLAY;
157    else
158    {
159       _DtSimpleError (panel.app_name, DtError, NULL, 
160                      "Invalid Resolution -- %s", parse_source);
161       return (False);
162    }
163
164    return (True);
165 }
166
167
168
169
170 /************************************************************************
171  *
172  *  StringToControlBehavior 
173  *
174  ************************************************************************/
175
176 Boolean
177 StringToControlBehavior (char * parse_source,
178                          void ** parse_return)
179
180 {
181    _DtWmParseToLower (parse_source);
182
183    if (strcmp (parse_source, "double_click") == 0)
184       *parse_return = (void *) DOUBLE_CLICK;
185    else if (strcmp (parse_source, "single_click") == 0)
186       *parse_return = (void *) SINGLE_CLICK;
187    else
188    {
189       _DtSimpleError (panel.app_name, DtError, NULL, 
190                      "Invalid Control Behavior -- %s", parse_source);
191       return (False);
192    }
193
194    return (True);
195 }
196
197
198
199
200 /************************************************************************
201  *
202  *  StringToGeometry
203  *
204  ************************************************************************/
205
206 Boolean
207 StringToGeometry (char * parse_source,
208                   void ** parse_return)
209
210 {
211    GeometryData *value;
212    int mask;
213    int x, y, width, height;
214
215
216    x = y = width = height = 0;
217
218    mask = XParseGeometry ((char *) parse_source, &x, &y, 
219                           (unsigned int *) &width, (unsigned int *) &height);
220
221    if (mask)
222    {
223       /* Allocate space for the geometry structure  */
224
225       value = (GeometryData *) XtMalloc (sizeof (GeometryData));
226
227       value->flags = mask;
228       value->x = x;
229       value->y = y;
230       value->width = width;
231       value->height = height;
232        
233       *parse_return = (void *) value;
234    }
235    else
236    {
237       _DtSimpleError (panel.app_name, DtError, NULL, 
238                      "Invalid Geometry -- %s", parse_source);
239       return (False);
240    }
241
242    return (True);
243 }
244
245
246
247
248 /************************************************************************
249  *
250  *  StringToAction
251  *
252  ************************************************************************/
253
254
255 Boolean
256 StringToAction (char * parse_source,
257                 void ** parse_return)
258
259
260 {
261    PanelActionData * action_data;
262    unsigned char * string, * source, * head_ptr;
263
264
265    head_ptr = source = (unsigned char *) strdup (parse_source);
266
267    if ((string = _DtWmParseNextTokenC (&source, False)) != NULL)
268    {
269       action_data = (PanelActionData *) XtMalloc (sizeof (PanelActionData));
270
271       action_data->action_name = strdup ((char *) string);
272       action_data->action_label = NULL;
273       action_data->aap = NULL;
274       action_data->count = 0;
275
276       while ((string = _DtWmParseNextTokenC (&source, False)) != NULL)
277       {
278          action_data->count++;
279          action_data->aap =
280             (DtActionArg *) XtRealloc ((char *) action_data->aap,
281                                     (sizeof (DtActionArg) * action_data->count));
282          action_data->aap[action_data->count-1].argClass = DtACTION_FILE;
283
284          action_data->aap[action_data->count-1].u.file.name =
285                                                  strdup((char *)string);
286       }
287    }
288    else
289    {
290       _DtSimpleError (panel.app_name, DtError, NULL, 
291                      "Invalid Action -- %s", parse_source);
292       free (head_ptr);
293       return (False);
294    }
295
296    free ((char *) head_ptr);
297    *parse_return = (void *) action_data;
298    return (True);
299 }
300
301
302
303
304 /************************************************************************
305  *
306  *  StringToControlType
307  *
308  ************************************************************************/
309
310 Boolean
311 StringToControlType (char * parse_source,
312                      void ** parse_return)
313
314 {
315    _DtWmParseToLower (parse_source);
316
317    if (strcmp (parse_source, control_types[CONTROL_BLANK]) == 0)
318       *parse_return = (void *) CONTROL_BLANK;
319    else if (strcmp (parse_source, control_types[CONTROL_BUSY]) == 0)
320       *parse_return = (void *) CONTROL_BUSY;
321    else if (strcmp (parse_source, control_types[CONTROL_ICON]) == 0)
322       *parse_return = (void *) CONTROL_ICON;
323    else if (strcmp (parse_source, control_types[CONTROL_CLIENT]) == 0)
324       *parse_return = (void *) CONTROL_CLIENT;
325    else if (strcmp (parse_source, control_types[CONTROL_CLOCK]) == 0)
326       *parse_return = (void *) CONTROL_CLOCK;
327    else if (strcmp (parse_source, control_types[CONTROL_DATE]) == 0)
328       *parse_return = (void *) CONTROL_DATE;
329    else if (strcmp (parse_source, control_types[CONTROL_FILE]) == 0)
330       *parse_return = (void *) CONTROL_FILE;
331    else
332    {
333       _DtSimpleError (panel.app_name, DtError, NULL, 
334                      "Invalid Control Type -- %s", parse_source);
335       return (False);
336    }
337
338    return (True);
339 }
340
341
342
343
344 /************************************************************************
345  *
346  *  StringToMonitorType
347  *
348  ************************************************************************/
349
350 Boolean
351 StringToMonitorType (char * parse_source,
352                      void ** parse_return)
353
354 {
355    _DtWmParseToLower (parse_source);
356
357    if (strcmp (parse_source, monitor_types[MONITOR_NONE]) == 0)
358       *parse_return = (void *) MONITOR_NONE;
359    else if (strcmp (parse_source, monitor_types[MONITOR_MAIL]) == 0)
360       *parse_return = (void *) MONITOR_MAIL;
361    else if (strcmp (parse_source, monitor_types[MONITOR_FILE]) == 0)
362       *parse_return = (void *) MONITOR_FILE;
363    else
364    {
365       _DtSimpleError (panel.app_name, DtError, NULL, 
366                      "Invalid Monitor Type -- %s", parse_source);
367       return (False);
368    }
369
370    return (True);
371 }
372
373
374
375
376 /************************************************************************
377  *
378  *  StringToControlContainerType
379  *
380  ************************************************************************/
381
382 Boolean
383 StringToControlContainerType (char * parse_source,
384                               void ** parse_return)
385
386 {
387    if (strcmp (parse_source, entry_types[BOX]) == 0)
388       *parse_return = (void *) BOX;
389    else if (strcmp (parse_source, entry_types[SUBPANEL]) == 0)
390       *parse_return = (void *) SUBPANEL;
391    else if (strcmp (parse_source, entry_types[SWITCH]) == 0)
392       *parse_return = (void *) SWITCH;
393    else
394    {
395       _DtSimpleError (panel.app_name, DtError, NULL, 
396                      "Invalid Control Container Type -- %s", parse_source);
397       return (False);
398    }
399
400    return (True);
401 }
402
403
404
405
406 /************************************************************************
407  *
408  *  StringToPositionHints
409  *
410  ************************************************************************/
411
412 Boolean
413 StringToPositionHints (char * parse_source,
414                        void ** parse_return)
415
416 {
417    Boolean status;
418
419    _DtWmParseToLower (parse_source);
420
421    if (strcmp (parse_source, "first") == 0)
422       parse_source = "0";
423    else if (strcmp (parse_source, "last") == 0)
424       parse_source = "100";
425
426    status = StringToInt (parse_source, parse_return);
427
428    if ((long) *parse_return < 0 || (long) *parse_return > 100)
429    {
430       
431       _DtSimpleError (panel.app_name, DtError, NULL, 
432                      "Invalid Position Hints value -- %d",
433                      (long) *parse_return);
434       return (False);
435    }
436
437    return ( True );
438 }
439
440
441 /************************************************************************
442  *
443  *  StringToFileName
444  *     Converts net file format to a file format.
445  *
446  ************************************************************************/
447
448 Boolean
449 StringToFileName (char * parse_source,
450                   void ** parse_return)
451
452 {
453    return (StringToString(parse_source, parse_return));
454 }
455
456
457
458
459 /************************************************************************
460  *
461  *  FreeString
462  *
463  ************************************************************************/
464
465 void
466 FreeString (void ** parse_value)
467
468 {
469     XtFree ((char *) *parse_value);
470 }
471
472
473
474
475 /************************************************************************
476  *
477  *  FreeGeometry
478  *
479  ************************************************************************/
480
481 void
482 FreeGeometry (void ** parse_value)
483
484 {
485    XtFree ((char *) *parse_value);
486 }
487
488
489
490
491 /************************************************************************
492  *
493  *  FreeAction
494  *
495  ************************************************************************/
496
497 void
498 FreeAction (void ** parse_value)
499 {
500    PanelActionData * actionData = (PanelActionData *) *parse_value;
501    int i;
502
503    XtFree (actionData->action_name);
504
505    for (i = 0; i < actionData->count; i++)
506      XtFree ((char *) actionData->aap[i].u.file.name);
507
508    XtFree ((char *) actionData->aap);
509
510    XtFree ((char *) *parse_value);
511 }