Parse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #include <Dt/DtP.h>
  39. #include <Dt/DbReader.h>
  40. #include <Dt/UserMsg.h>
  41. #include "WmGlobal.h"
  42. #include "WmParse.h"
  43. #include "DataBaseLoad.h"
  44. #include "Parse.h"
  45. /************************************************************************
  46. *
  47. * StringToString
  48. *
  49. ************************************************************************/
  50. Boolean
  51. StringToString (char * parse_source,
  52. void ** parse_return)
  53. {
  54. *parse_return = (void *) strdup (parse_source);
  55. return (True);
  56. }
  57. /************************************************************************
  58. *
  59. * StrintToInt
  60. *
  61. ************************************************************************/
  62. Boolean
  63. StringToInt (char * parse_source,
  64. void ** parse_return)
  65. {
  66. char * source_ptr = parse_source;
  67. long value = 0;
  68. char chr;
  69. while (chr = *source_ptr++)
  70. {
  71. if (chr >= '0' && chr <= '9')
  72. {
  73. value *= 10;
  74. value += chr - '0';
  75. }
  76. else
  77. {
  78. _DtSimpleError (panel.app_name, DtError, NULL,
  79. "Invalid Integer -- %s", parse_source);
  80. return (False);
  81. }
  82. }
  83. *parse_return = (void *) value;
  84. return (True);
  85. }
  86. /************************************************************************
  87. *
  88. * StringToBoolean
  89. *
  90. ************************************************************************/
  91. Boolean
  92. StringToBoolean (char * parse_source,
  93. void ** parse_return)
  94. {
  95. _DtWmParseToLower(parse_source);
  96. if (strcmp (parse_source, "true") == 0)
  97. *parse_return = (void *) True;
  98. else if (strcmp (parse_source, "false") == 0)
  99. *parse_return = (void *) False;
  100. else
  101. {
  102. _DtSimpleError (panel.app_name, DtError, NULL,
  103. "Invalid Boolean -- %s", parse_source);
  104. return (False);
  105. }
  106. return (True);
  107. }
  108. /************************************************************************
  109. *
  110. * StringToResolution
  111. *
  112. ************************************************************************/
  113. Boolean
  114. StringToResolution (char * parse_source,
  115. void ** parse_return)
  116. {
  117. _DtWmParseToLower (parse_source);
  118. if (strcmp (parse_source, resolution_types[HIGH]) == 0)
  119. *parse_return = (void *) HIGH;
  120. else if (strcmp (parse_source, resolution_types[MEDIUM]) == 0)
  121. *parse_return = (void *) MEDIUM;
  122. else if (strcmp (parse_source, resolution_types[LOW]) == 0)
  123. *parse_return = (void *) LOW;
  124. else if (strcmp (parse_source, resolution_types[MATCH_DISPLAY]) == 0)
  125. *parse_return = (void *) MATCH_DISPLAY;
  126. else
  127. {
  128. _DtSimpleError (panel.app_name, DtError, NULL,
  129. "Invalid Resolution -- %s", parse_source);
  130. return (False);
  131. }
  132. return (True);
  133. }
  134. /************************************************************************
  135. *
  136. * StringToControlBehavior
  137. *
  138. ************************************************************************/
  139. Boolean
  140. StringToControlBehavior (char * parse_source,
  141. void ** parse_return)
  142. {
  143. _DtWmParseToLower (parse_source);
  144. if (strcmp (parse_source, "double_click") == 0)
  145. *parse_return = (void *) DOUBLE_CLICK;
  146. else if (strcmp (parse_source, "single_click") == 0)
  147. *parse_return = (void *) SINGLE_CLICK;
  148. else
  149. {
  150. _DtSimpleError (panel.app_name, DtError, NULL,
  151. "Invalid Control Behavior -- %s", parse_source);
  152. return (False);
  153. }
  154. return (True);
  155. }
  156. /************************************************************************
  157. *
  158. * StringToGeometry
  159. *
  160. ************************************************************************/
  161. Boolean
  162. StringToGeometry (char * parse_source,
  163. void ** parse_return)
  164. {
  165. GeometryData *value;
  166. int mask;
  167. int x, y, width, height;
  168. x = y = width = height = 0;
  169. mask = XParseGeometry ((char *) parse_source, &x, &y,
  170. (unsigned int *) &width, (unsigned int *) &height);
  171. if (mask)
  172. {
  173. /* Allocate space for the geometry structure */
  174. value = (GeometryData *) XtMalloc (sizeof (GeometryData));
  175. value->flags = mask;
  176. value->x = x;
  177. value->y = y;
  178. value->width = width;
  179. value->height = height;
  180. *parse_return = (void *) value;
  181. }
  182. else
  183. {
  184. _DtSimpleError (panel.app_name, DtError, NULL,
  185. "Invalid Geometry -- %s", parse_source);
  186. return (False);
  187. }
  188. return (True);
  189. }
  190. /************************************************************************
  191. *
  192. * StringToAction
  193. *
  194. ************************************************************************/
  195. Boolean
  196. StringToAction (char * parse_source,
  197. void ** parse_return)
  198. {
  199. PanelActionData * action_data;
  200. unsigned char * string, * source, * head_ptr;
  201. head_ptr = source = (unsigned char *) strdup (parse_source);
  202. if ((string = _DtWmParseNextTokenC (&source, False)) != NULL)
  203. {
  204. action_data = (PanelActionData *) XtMalloc (sizeof (PanelActionData));
  205. action_data->action_name = strdup ((char *) string);
  206. action_data->action_label = NULL;
  207. action_data->aap = NULL;
  208. action_data->count = 0;
  209. while ((string = _DtWmParseNextTokenC (&source, False)) != NULL)
  210. {
  211. action_data->count++;
  212. action_data->aap =
  213. (DtActionArg *) XtRealloc ((char *) action_data->aap,
  214. (sizeof (DtActionArg) * action_data->count));
  215. action_data->aap[action_data->count-1].argClass = DtACTION_FILE;
  216. action_data->aap[action_data->count-1].u.file.name =
  217. strdup((char *)string);
  218. }
  219. }
  220. else
  221. {
  222. _DtSimpleError (panel.app_name, DtError, NULL,
  223. "Invalid Action -- %s", parse_source);
  224. free (head_ptr);
  225. return (False);
  226. }
  227. free ((char *) head_ptr);
  228. *parse_return = (void *) action_data;
  229. return (True);
  230. }
  231. /************************************************************************
  232. *
  233. * StringToControlType
  234. *
  235. ************************************************************************/
  236. Boolean
  237. StringToControlType (char * parse_source,
  238. void ** parse_return)
  239. {
  240. _DtWmParseToLower (parse_source);
  241. if (strcmp (parse_source, control_types[CONTROL_BLANK]) == 0)
  242. *parse_return = (void *) CONTROL_BLANK;
  243. else if (strcmp (parse_source, control_types[CONTROL_BUSY]) == 0)
  244. *parse_return = (void *) CONTROL_BUSY;
  245. else if (strcmp (parse_source, control_types[CONTROL_ICON]) == 0)
  246. *parse_return = (void *) CONTROL_ICON;
  247. else if (strcmp (parse_source, control_types[CONTROL_CLIENT]) == 0)
  248. *parse_return = (void *) CONTROL_CLIENT;
  249. else if (strcmp (parse_source, control_types[CONTROL_CLOCK]) == 0)
  250. *parse_return = (void *) CONTROL_CLOCK;
  251. else if (strcmp (parse_source, control_types[CONTROL_DATE]) == 0)
  252. *parse_return = (void *) CONTROL_DATE;
  253. else if (strcmp (parse_source, control_types[CONTROL_FILE]) == 0)
  254. *parse_return = (void *) CONTROL_FILE;
  255. else
  256. {
  257. _DtSimpleError (panel.app_name, DtError, NULL,
  258. "Invalid Control Type -- %s", parse_source);
  259. return (False);
  260. }
  261. return (True);
  262. }
  263. /************************************************************************
  264. *
  265. * StringToMonitorType
  266. *
  267. ************************************************************************/
  268. Boolean
  269. StringToMonitorType (char * parse_source,
  270. void ** parse_return)
  271. {
  272. _DtWmParseToLower (parse_source);
  273. if (strcmp (parse_source, monitor_types[MONITOR_NONE]) == 0)
  274. *parse_return = (void *) MONITOR_NONE;
  275. else if (strcmp (parse_source, monitor_types[MONITOR_MAIL]) == 0)
  276. *parse_return = (void *) MONITOR_MAIL;
  277. else if (strcmp (parse_source, monitor_types[MONITOR_FILE]) == 0)
  278. *parse_return = (void *) MONITOR_FILE;
  279. else
  280. {
  281. _DtSimpleError (panel.app_name, DtError, NULL,
  282. "Invalid Monitor Type -- %s", parse_source);
  283. return (False);
  284. }
  285. return (True);
  286. }
  287. /************************************************************************
  288. *
  289. * StringToControlContainerType
  290. *
  291. ************************************************************************/
  292. Boolean
  293. StringToControlContainerType (char * parse_source,
  294. void ** parse_return)
  295. {
  296. if (strcmp (parse_source, entry_types[BOX]) == 0)
  297. *parse_return = (void *) BOX;
  298. else if (strcmp (parse_source, entry_types[SUBPANEL]) == 0)
  299. *parse_return = (void *) SUBPANEL;
  300. else if (strcmp (parse_source, entry_types[SWITCH]) == 0)
  301. *parse_return = (void *) SWITCH;
  302. else
  303. {
  304. _DtSimpleError (panel.app_name, DtError, NULL,
  305. "Invalid Control Container Type -- %s", parse_source);
  306. return (False);
  307. }
  308. return (True);
  309. }
  310. /************************************************************************
  311. *
  312. * StringToPositionHints
  313. *
  314. ************************************************************************/
  315. Boolean
  316. StringToPositionHints (char * parse_source,
  317. void ** parse_return)
  318. {
  319. Boolean status;
  320. _DtWmParseToLower (parse_source);
  321. if (strcmp (parse_source, "first") == 0)
  322. parse_source = "0";
  323. else if (strcmp (parse_source, "last") == 0)
  324. parse_source = "100";
  325. status = StringToInt (parse_source, parse_return);
  326. if ((long) *parse_return < 0 || (long) *parse_return > 100)
  327. {
  328. _DtSimpleError (panel.app_name, DtError, NULL,
  329. "Invalid Position Hints value -- %d",
  330. (long) *parse_return);
  331. return (False);
  332. }
  333. return ( True );
  334. }
  335. /************************************************************************
  336. *
  337. * StringToFileName
  338. * Converts net file format to a file format.
  339. *
  340. ************************************************************************/
  341. Boolean
  342. StringToFileName (char * parse_source,
  343. void ** parse_return)
  344. {
  345. return (StringToString(parse_source, parse_return));
  346. }
  347. /************************************************************************
  348. *
  349. * FreeString
  350. *
  351. ************************************************************************/
  352. void
  353. FreeString (void ** parse_value)
  354. {
  355. XtFree ((char *) *parse_value);
  356. }
  357. /************************************************************************
  358. *
  359. * FreeGeometry
  360. *
  361. ************************************************************************/
  362. void
  363. FreeGeometry (void ** parse_value)
  364. {
  365. XtFree ((char *) *parse_value);
  366. }
  367. /************************************************************************
  368. *
  369. * FreeAction
  370. *
  371. ************************************************************************/
  372. void
  373. FreeAction (void ** parse_value)
  374. {
  375. PanelActionData * actionData = (PanelActionData *) *parse_value;
  376. int i;
  377. XtFree (actionData->action_name);
  378. for (i = 0; i < actionData->count; i++)
  379. XtFree ((char *) actionData->aap[i].u.file.name);
  380. XtFree ((char *) actionData->aap);
  381. XtFree ((char *) *parse_value);
  382. }