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