PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/OpenSLP_1-1-5/openslp/common/slp_attr_y.y

#
Happy | 371 lines | 333 code | 38 blank | 0 comment | 0 complexity | 63e8f21125a0e2075181f86a29e5cc8a MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*******************************************************************
  2. * Description: encode/decode attribute lists
  3. *
  4. * Originated: 03-06-2000
  5. * Original Author: Mike Day - md@soft-hackle.net
  6. *
  7. * Copyright (C) Michael Day, 2000-2001
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *******************************************************************/
  23. %{
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include "slp_attr.h"
  28. #include "slp_linkedlist.h"
  29. /* prototypes and globals go here */
  30. #ifndef FALSE
  31. #define FALSE 0
  32. #endif
  33. #ifndef TRUE
  34. #define TRUE (!FALSE)
  35. #endif
  36. #define yymaxdepth slp_attr_maxdepth
  37. #define yyparse slp_attr_parse
  38. #define yylex slp_attr_lex
  39. #define yyerror slp_attr_error
  40. #define yylval slp_attr_lval
  41. #define yychar slp_attr_char
  42. #define yydebug slp_attr_debug
  43. #define yypact slp_attr_pact
  44. #define yyr1 slp_attr_r1
  45. #define yyr2 slp_attr_r2
  46. #define yydef slp_attr_def
  47. #define yychk slp_attr_chk
  48. #define yypgo slp_attr_pgo
  49. #define yyact slp_attr_act
  50. #define yyexca slp_attr_exca
  51. #define yyerrflag slp_attr_errflag
  52. #define yynerrs slp_attr_nerrs
  53. #define yyps slp_attr_ps
  54. #define yypv slp_attr_pv
  55. #define yys slp_attr_s
  56. #define yy_yys slp_attr_yys
  57. #define yystate slp_attr_state
  58. #define yytmp slp_attr_tmp
  59. #define yyv slp_attr_v
  60. #define yy_yyv slp_attr_yyv
  61. #define yyval slp_attr_val
  62. #define yylloc slp_attr_lloc
  63. #define yyreds slp_attr_reds
  64. #define yytoks slp_attr_toks
  65. #define yylhs slp_attr_yylhs
  66. #define yylen slp_attr_yylen
  67. #define yydefred slp_attr_yydefred
  68. #define yydgoto slp_attr_yydgoto
  69. #define yysindex slp_attr_yysindex
  70. #define yyrindex slp_attr_yyrindex
  71. #define yygindex slp_attr_yygindex
  72. #define yytable slp_attr_yytable
  73. #define yycheck slp_attr_yycheck
  74. #define yyname slp_attr_yyname
  75. #define yyrule slp_attr_yyrule
  76. static int bt = TRUE;
  77. static int bf = FALSE;
  78. static SLPAttrList attrHead = {&attrHead, &attrHead, TRUE, NULL, head, {0L}};
  79. static SLPAttrList inProcessAttr = {&inProcessAttr, &inProcessAttr, TRUE, NULL, head, {0L}};
  80. static SLPAttrList inProcessTag = {&inProcessTag, &inProcessTag, TRUE, NULL, head, {0L}};
  81. int slp_attr_parse(void);
  82. void slp_attr_error(char *, ...);
  83. int slp_attr_wrap(void);
  84. int slp_attr_lex(void);
  85. void slp_attr_close_lexer(unsigned int handle);
  86. unsigned int slp_attr_init_lexer(char *s);
  87. %}
  88. /* definitions for ytab.h */
  89. %union
  90. {
  91. int _i;
  92. char *_s;
  93. SLPAttrList *_atl;
  94. }
  95. %token<_i> _TRUE _FALSE _MULTIVAL _INT
  96. %token<_s> _ESCAPED _TAG _STRING
  97. /* typecast the non-terminals */
  98. /* %type <_i> */
  99. %type <_atl> attr_list attr attr_val_list attr_val
  100. %%
  101. attr_list: attr
  102. {
  103. while ( !SLP_IS_HEAD(inProcessAttr.next) )
  104. {
  105. $$ = inProcessAttr.next;
  106. SLP_UNLINK($$);
  107. SLP_INSERT_BEFORE($$, &attrHead);
  108. }
  109. /* all we really want to do here is link each attribute */
  110. /* to the global list head. */
  111. }
  112. | attr_list ',' attr
  113. {
  114. /* both of these non-terminals are really lists */
  115. /* ignore the first non-terminal */
  116. while ( !SLP_IS_HEAD(inProcessAttr.next) )
  117. {
  118. $$ = inProcessAttr.next;
  119. SLP_UNLINK($$);
  120. SLP_INSERT_BEFORE($$, &attrHead);
  121. }
  122. };
  123. attr: _TAG
  124. {
  125. $$ = SLPAllocAttr($1, tag, NULL, 0);
  126. if ( NULL != $$ )
  127. {
  128. SLP_INSERT_BEFORE($$, &inProcessAttr);
  129. }
  130. }
  131. | '(' _TAG ')'
  132. {
  133. $$ = SLPAllocAttr($2, tag, NULL, 0);
  134. if (NULL != $$)
  135. {
  136. SLP_INSERT_BEFORE($$, &inProcessAttr);
  137. }
  138. }
  139. | '(' _TAG '=' attr_val_list ')'
  140. {
  141. $$ = inProcessTag.next;
  142. while (!SLP_IS_HEAD($$))
  143. {
  144. $$->name = strdup($2);
  145. SLP_UNLINK($$);
  146. SLP_INSERT_BEFORE($$, &inProcessAttr);
  147. $$ = inProcessTag.next;
  148. }
  149. };
  150. attr_val_list: attr_val
  151. {
  152. if(NULL != $1)
  153. {
  154. SLP_INSERT($1, &inProcessTag);
  155. }
  156. }
  157. | attr_val_list _MULTIVAL attr_val
  158. {
  159. if (NULL != $3)
  160. {
  161. SLP_INSERT_BEFORE($3, &inProcessTag);
  162. }
  163. };
  164. attr_val: _TRUE
  165. {
  166. $$ = SLPAllocAttr(NULL, boolean, &bt, sizeof(int));
  167. }
  168. | _FALSE
  169. {
  170. $$ = SLPAllocAttr(NULL, boolean, &bf, sizeof(int));
  171. }
  172. | _ESCAPED
  173. {
  174. /* treat it as a string because it is already encoded */
  175. $$ = SLPAllocAttr(NULL, string, $1, strlen($1) + 1);
  176. }
  177. | _STRING
  178. {
  179. $$ = SLPAllocAttr(NULL, string, $1, strlen($1) + 1);
  180. }
  181. | _INT
  182. {
  183. $$ = SLPAllocAttr(NULL, integer, &($1), sizeof(int));
  184. };
  185. %%
  186. SLPAttrList *SLPAllocAttr(char *name, SLPTypes type, void *val, int len)
  187. {
  188. SLPAttrList *attr;
  189. if ( NULL != (attr = (SLPAttrList *)calloc(1, sizeof(SLPAttrList))) )
  190. {
  191. if ( name != NULL )
  192. {
  193. if ( NULL == (attr->name = strdup(name)) )
  194. {
  195. free(attr);
  196. return(NULL);
  197. }
  198. }
  199. attr->type = type;
  200. if ( type == head ) /* listhead */
  201. return(attr);
  202. if ( val != NULL )
  203. {
  204. switch ( type )
  205. {
  206. case string:
  207. if ( NULL != (attr->val.stringVal = strdup((unsigned char *)val)) )
  208. return(attr);
  209. break;
  210. case integer:
  211. attr->val.intVal = *(unsigned int *)val;
  212. break;
  213. case boolean:
  214. attr->val.boolVal = *(int *)val;
  215. break;
  216. case opaque:
  217. #if 0
  218. if ( len > 0 )
  219. {
  220. int encLen;
  221. opq_EncodeOpaque(val, len, (char **)&(attr->val.opaqueVal), &encLen);
  222. if ( NULL != attr->val.opaqueVal )
  223. {
  224. /* first two bytes contain length of attribute */
  225. SLP_SETSHORT(((char *)attr->val.opaqueVal), encLen, 0 );
  226. }
  227. }
  228. #endif
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. }
  235. return(attr);
  236. }
  237. SLPAttrList *SLPAllocAttrList(void)
  238. {
  239. SLPAttrList *temp;
  240. if ( NULL != (temp = SLPAllocAttr(NULL, head, NULL, 0)) )
  241. {
  242. temp->next = temp->prev = temp;
  243. temp->isHead = TRUE;
  244. }
  245. return(temp);
  246. }
  247. /* attr MUST be unlinked from its list ! */
  248. void SLPFreeAttr(SLPAttrList *attr)
  249. {
  250. if ( attr->name != NULL )
  251. {
  252. free(attr->name);
  253. }
  254. if ( attr->type == string && attr->val.stringVal != NULL )
  255. {
  256. free(attr->val.stringVal);
  257. }
  258. else if ( attr->type == opaque && attr->val.opaqueVal != NULL )
  259. {
  260. free(attr->val.opaqueVal);
  261. }
  262. free(attr);
  263. }
  264. void SLPFreeAttrList(SLPAttrList *list, int staticFlag)
  265. {
  266. SLPAttrList *temp;
  267. while ( ! (SLP_IS_EMPTY(list)) )
  268. {
  269. temp = list->next;
  270. SLP_UNLINK(temp);
  271. SLPFreeAttr(temp);
  272. }
  273. if ( staticFlag == TRUE )
  274. {
  275. SLPFreeAttr(list);
  276. }
  277. return;
  278. }
  279. void SLPInitInternalAttrList(void)
  280. {
  281. attrHead.next = attrHead.prev = &attrHead;
  282. attrHead.isHead = TRUE;
  283. inProcessAttr.next = inProcessAttr.prev = &inProcessAttr;
  284. inProcessAttr.isHead = TRUE;
  285. inProcessTag.next = inProcessTag.prev = &inProcessTag;
  286. inProcessTag.isHead = TRUE;
  287. return;
  288. }
  289. SLPAttrList *_SLPDecodeAttrString(char *s)
  290. {
  291. unsigned int lexer = 0;
  292. SLPAttrList *temp = NULL;
  293. SLPInitInternalAttrList();
  294. if ( s != NULL )
  295. {
  296. if ( NULL != (temp = SLPAllocAttrList()) )
  297. {
  298. if ((0 != (lexer = slp_attr_init_lexer(s))) && yyparse() )
  299. {
  300. SLPFreeAttrList(temp,0);
  301. while ( !SLP_IS_HEAD(inProcessTag.next) )
  302. {
  303. temp = inProcessTag.next;
  304. SLP_UNLINK(temp);
  305. SLPFreeAttr(temp);
  306. }
  307. while ( !SLP_IS_HEAD(inProcessAttr.next) )
  308. {
  309. temp = inProcessAttr.next;
  310. SLP_UNLINK(temp);
  311. SLPFreeAttr(temp);
  312. }
  313. while ( !SLP_IS_HEAD(attrHead.next) )
  314. {
  315. temp = attrHead.next;
  316. SLP_UNLINK(temp);
  317. SLPFreeAttr(temp);
  318. }
  319. slp_attr_close_lexer(lexer);
  320. return(NULL);
  321. }
  322. if ( !SLP_IS_EMPTY(&attrHead) )
  323. {
  324. SLP_LINK_HEAD(temp, &attrHead);
  325. }
  326. if ( lexer != 0 )
  327. {
  328. slp_attr_close_lexer(lexer);
  329. }
  330. }
  331. }
  332. return(temp);
  333. }