PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.0.beta2/openslp/common/slp_attr_y.y

#
Happy | 384 lines | 350 code | 34 blank | 0 comment | 0 complexity | 7568fc22c66d7a580758c437e766ce4c MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*-------------------------------------------------------------------------
  2. * Copyright (C) 2000-2005 Mike Day <ncmike@ncultra.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * Neither the name of Caldera Systems nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALDERA
  24. * SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *-------------------------------------------------------------------------*/
  32. /** Bison parser input file for attribute parser.
  33. *
  34. * @file slp_attr_y.y
  35. * @date 03-06-2000
  36. * @author Matthew Peterson, Michael Day (md@soft-hackle.net),
  37. * John Calcote (jcalcote@novell.com)
  38. * @attention Please submit patches to http://www.openslp.org
  39. * @ingroup CommonCodeAttrs
  40. */
  41. %{
  42. #include "slp_types.h"
  43. #include "slp_attr.h"
  44. #include "slp_linkedlist.h"
  45. /* prototypes and globals go here */
  46. #ifndef FALSE
  47. # define FALSE 0
  48. #endif
  49. #ifndef TRUE
  50. # define TRUE (!FALSE)
  51. #endif
  52. #define yymaxdepth slp_attr_maxdepth
  53. #define yyparse slp_attr_parse
  54. #define yylex slp_attr_lex
  55. #define yyerror slp_attr_error
  56. #define yylval slp_attr_lval
  57. #define yychar slp_attr_char
  58. #define yydebug slp_attr_debug
  59. #define yypact slp_attr_pact
  60. #define yyr1 slp_attr_r1
  61. #define yyr2 slp_attr_r2
  62. #define yydef slp_attr_def
  63. #define yychk slp_attr_chk
  64. #define yypgo slp_attr_pgo
  65. #define yyact slp_attr_act
  66. #define yyexca slp_attr_exca
  67. #define yyerrflag slp_attr_errflag
  68. #define yynerrs slp_attr_nerrs
  69. #define yyps slp_attr_ps
  70. #define yypv slp_attr_pv
  71. #define yys slp_attr_s
  72. #define yy_yys slp_attr_yys
  73. #define yystate slp_attr_state
  74. #define yytmp slp_attr_tmp
  75. #define yyv slp_attr_v
  76. #define yy_yyv slp_attr_yyv
  77. #define yyval slp_attr_val
  78. #define yylloc slp_attr_lloc
  79. #define yyreds slp_attr_reds
  80. #define yytoks slp_attr_toks
  81. #define yylhs slp_attr_yylhs
  82. #define yylen slp_attr_yylen
  83. #define yydefred slp_attr_yydefred
  84. #define yydgoto slp_attr_yydgoto
  85. #define yysindex slp_attr_yysindex
  86. #define yyrindex slp_attr_yyrindex
  87. #define yygindex slp_attr_yygindex
  88. #define yytable slp_attr_yytable
  89. #define yycheck slp_attr_yycheck
  90. #define yyname slp_attr_yyname
  91. #define yyrule slp_attr_yyrule
  92. static int bt = TRUE;
  93. static int bf = FALSE;
  94. static SLPAttrList attrHead = {&attrHead, &attrHead, TRUE, 0, head, {0L}};
  95. static SLPAttrList inProcessAttr = {&inProcessAttr, &inProcessAttr, TRUE, 0, head, {0L}};
  96. static SLPAttrList inProcessTag = {&inProcessTag, &inProcessTag, TRUE, 0, head, {0L}};
  97. int slp_attr_parse(void);
  98. void slp_attr_error(char *, ...);
  99. int slp_attr_wrap(void);
  100. int slp_attr_lex(void);
  101. void slp_attr_close_lexer(unsigned int handle);
  102. unsigned int slp_attr_init_lexer(char *s);
  103. %}
  104. /* definitions for ytab.h */
  105. %union
  106. {
  107. int _i;
  108. char *_s;
  109. SLPAttrList *_atl;
  110. }
  111. %token<_i> _TRUE _FALSE _MULTIVAL _INT
  112. %token<_s> _ESCAPED _TAG _STRING
  113. /* typecast the non-terminals */
  114. /* %type <_i> */
  115. %type <_atl> attr_list attr attr_val_list attr_val
  116. %%
  117. attr_list: attr
  118. {
  119. while ( !SLP_IS_HEAD(inProcessAttr.next) )
  120. {
  121. $$ = inProcessAttr.next;
  122. SLP_UNLINK($$);
  123. SLP_INSERT_BEFORE($$, &attrHead);
  124. }
  125. /* all we really want to do here is link each attribute */
  126. /* to the global list head. */
  127. }
  128. | attr_list ',' attr
  129. {
  130. /* both of these non-terminals are really lists */
  131. /* ignore the first non-terminal */
  132. while ( !SLP_IS_HEAD(inProcessAttr.next) )
  133. {
  134. $$ = inProcessAttr.next;
  135. SLP_UNLINK($$);
  136. SLP_INSERT_BEFORE($$, &attrHead);
  137. }
  138. };
  139. attr: _TAG
  140. {
  141. $$ = SLPAllocAttr($1, tag, 0, 0);
  142. if ( 0 != $$ )
  143. {
  144. SLP_INSERT_BEFORE($$, &inProcessAttr);
  145. }
  146. }
  147. | '(' _TAG ')'
  148. {
  149. $$ = SLPAllocAttr($2, tag, 0, 0);
  150. if (0 != $$)
  151. {
  152. SLP_INSERT_BEFORE($$, &inProcessAttr);
  153. }
  154. }
  155. | '(' _TAG '=' attr_val_list ')'
  156. {
  157. $$ = inProcessTag.next;
  158. while (!SLP_IS_HEAD($$))
  159. {
  160. $$->name = strdup($2);
  161. SLP_UNLINK($$);
  162. SLP_INSERT_BEFORE($$, &inProcessAttr);
  163. $$ = inProcessTag.next;
  164. }
  165. };
  166. attr_val_list: attr_val
  167. {
  168. if(0 != $1)
  169. {
  170. SLP_INSERT($1, &inProcessTag);
  171. }
  172. }
  173. | attr_val_list _MULTIVAL attr_val
  174. {
  175. if (0 != $3)
  176. {
  177. SLP_INSERT_BEFORE($3, &inProcessTag);
  178. }
  179. };
  180. attr_val: _TRUE
  181. {
  182. $$ = SLPAllocAttr(0, boolean, &bt, sizeof(int));
  183. }
  184. | _FALSE
  185. {
  186. $$ = SLPAllocAttr(0, boolean, &bf, sizeof(int));
  187. }
  188. | _ESCAPED
  189. {
  190. /* treat it as a string because it is already encoded */
  191. $$ = SLPAllocAttr(0, string, $1, strlen($1) + 1);
  192. }
  193. | _STRING
  194. {
  195. $$ = SLPAllocAttr(0, string, $1, strlen($1) + 1);
  196. }
  197. | _INT
  198. {
  199. $$ = SLPAllocAttr(0, integer, &($1), sizeof(int));
  200. };
  201. %%
  202. SLPAttrList *SLPAllocAttr(char *name, SLPTypes type, void *val, int len)
  203. {
  204. SLPAttrList *attr;
  205. if ( 0 != (attr = (SLPAttrList *)calloc(1, sizeof(SLPAttrList))) )
  206. {
  207. if ( name != 0 )
  208. {
  209. if ( 0 == (attr->name = strdup(name)) )
  210. {
  211. free(attr);
  212. return(0);
  213. }
  214. }
  215. attr->type = type;
  216. if ( type == head ) /* listhead */
  217. return(attr);
  218. if ( val != 0 )
  219. {
  220. switch ( type )
  221. {
  222. case string:
  223. if ( 0 != (attr->val.stringVal = strdup((char *)val)) )
  224. return(attr);
  225. break;
  226. case integer:
  227. attr->val.intVal = *(unsigned int *)val;
  228. break;
  229. case boolean:
  230. attr->val.boolVal = *(int *)val;
  231. break;
  232. case opaque:
  233. #if 0
  234. if ( len > 0 )
  235. {
  236. int encLen;
  237. opq_EncodeOpaque(val, len, (char **)&(attr->val.opaqueVal), &encLen);
  238. if ( 0 != attr->val.opaqueVal )
  239. {
  240. /* first two bytes contain length of attribute */
  241. SLP_SETSHORT(((char *)attr->val.opaqueVal), encLen, 0 );
  242. }
  243. }
  244. #endif
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. }
  251. return(attr);
  252. }
  253. SLPAttrList *SLPAllocAttrList(void)
  254. {
  255. SLPAttrList *temp;
  256. if ( 0 != (temp = SLPAllocAttr(0, head, 0, 0)) )
  257. {
  258. temp->next = temp->prev = temp;
  259. temp->isHead = TRUE;
  260. }
  261. return(temp);
  262. }
  263. /* attr MUST be unlinked from its list ! */
  264. void SLPFreeAttr(SLPAttrList *attr)
  265. {
  266. if ( attr->name != 0 )
  267. {
  268. free(attr->name);
  269. }
  270. if ( attr->type == string && attr->val.stringVal != 0 )
  271. {
  272. free(attr->val.stringVal);
  273. }
  274. else if ( attr->type == opaque && attr->val.opaqueVal != 0 )
  275. {
  276. free(attr->val.opaqueVal);
  277. }
  278. free(attr);
  279. }
  280. void SLPFreeAttrList(SLPAttrList *list, int staticFlag)
  281. {
  282. SLPAttrList *temp;
  283. while ( ! (SLP_IS_EMPTY(list)) )
  284. {
  285. temp = list->next;
  286. SLP_UNLINK(temp);
  287. SLPFreeAttr(temp);
  288. }
  289. if ( staticFlag == TRUE )
  290. {
  291. SLPFreeAttr(list);
  292. }
  293. return;
  294. }
  295. void SLPInitInternalAttrList(void)
  296. {
  297. attrHead.next = attrHead.prev = &attrHead;
  298. attrHead.isHead = TRUE;
  299. inProcessAttr.next = inProcessAttr.prev = &inProcessAttr;
  300. inProcessAttr.isHead = TRUE;
  301. inProcessTag.next = inProcessTag.prev = &inProcessTag;
  302. inProcessTag.isHead = TRUE;
  303. return;
  304. }
  305. SLPAttrList *_SLPDecodeAttrString(char *s)
  306. {
  307. unsigned int lexer = 0;
  308. SLPAttrList *temp = 0;
  309. SLPInitInternalAttrList();
  310. if ( s != 0 )
  311. {
  312. if ( 0 != (temp = SLPAllocAttrList()) )
  313. {
  314. if ((0 != (lexer = slp_attr_init_lexer(s))) && yyparse() )
  315. {
  316. SLPFreeAttrList(temp,0);
  317. while ( !SLP_IS_HEAD(inProcessTag.next) )
  318. {
  319. temp = inProcessTag.next;
  320. SLP_UNLINK(temp);
  321. SLPFreeAttr(temp);
  322. }
  323. while ( !SLP_IS_HEAD(inProcessAttr.next) )
  324. {
  325. temp = inProcessAttr.next;
  326. SLP_UNLINK(temp);
  327. SLPFreeAttr(temp);
  328. }
  329. while ( !SLP_IS_HEAD(attrHead.next) )
  330. {
  331. temp = attrHead.next;
  332. SLP_UNLINK(temp);
  333. SLPFreeAttr(temp);
  334. }
  335. slp_attr_close_lexer(lexer);
  336. return 0;
  337. }
  338. if ( !SLP_IS_EMPTY(&attrHead) )
  339. {
  340. SLP_LINK_HEAD(temp, &attrHead);
  341. }
  342. if ( lexer != 0 )
  343. {
  344. slp_attr_close_lexer(lexer);
  345. }
  346. }
  347. }
  348. return temp;
  349. }
  350. /*=========================================================================*/