dapparse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #include "dapparselex.h"
  5. /* Forward */
  6. static void addedges(OCnode* node);
  7. static void setroot(OCnode*,OClist*);
  8. static int isglobalname(char* name);
  9. static OCnode* newocnode(char* name, OCtype octype, DAPparsestate* state);
  10. static OCtype octypefor(Object etype);
  11. static char* scopeduplicates(OClist* list);
  12. static int check_int32(char* val, long* value);
  13. /****************************************************/
  14. /* Switch to DAS parsing SCAN_WORD definition */
  15. /* Use the initial keyword to indicate what we are parsing */
  16. void
  17. dap_tagparse(DAPparsestate* state, int kind)
  18. {
  19. switch (kind) {
  20. case SCAN_DATASET:
  21. case SCAN_ERROR:
  22. break;
  23. case SCAN_ATTR:
  24. dapsetwordchars(state->lexstate,1);
  25. break;
  26. default:
  27. fprintf(stderr,"tagparse: Unknown tag argument: %d\n",kind);
  28. }
  29. }
  30. Object
  31. dap_datasetbody(DAPparsestate* state, Object name, Object decls)
  32. {
  33. OCnode* root = newocnode((char*)name,OC_Dataset,state);
  34. root->subnodes = (OClist*)decls;
  35. OCASSERT((state->root == NULL));
  36. state->root = root;
  37. state->root->root = state->root; /* make sure to cross link */
  38. addedges(root);
  39. setroot(root,state->ocnodes);
  40. return NULL;
  41. }
  42. Object
  43. dap_attributebody(DAPparsestate* state, Object attrlist)
  44. {
  45. OCnode* node = newocnode(NULL,OC_Attributeset,state);
  46. OCASSERT((state->root == NULL));
  47. state->root = node;
  48. /* make sure to cross link */
  49. state->root->root = state->root;
  50. node->subnodes = (OClist*)attrlist;
  51. addedges(node);
  52. return NULL;
  53. }
  54. void
  55. dap_errorbody(DAPparsestate* state,
  56. Object code, Object msg, Object ptype, Object prog)
  57. {
  58. state->svcerror = 1;
  59. state->code = nulldup((char*)code);
  60. state->message = nulldup((char*)msg);
  61. /* Ignore ptype and prog for now */
  62. }
  63. void
  64. dap_unrecognizedresponse(DAPparsestate* state)
  65. {
  66. /* see if this is an HTTP error */
  67. unsigned int httperr = 0;
  68. int i;
  69. char iv[32];
  70. sscanf(state->lexstate->input,"%u ",&httperr);
  71. sprintf(iv,"%u",httperr);
  72. state->lexstate->next = state->lexstate->input;
  73. /* Limit the amount of input to prevent runaway */
  74. for(i=0;i<4096;i++) {if(state->lexstate->input[i] == '\0') break;}
  75. state->lexstate->input[i] = '\0';
  76. dap_errorbody(state,iv,state->lexstate->input,NULL,NULL);
  77. }
  78. Object
  79. dap_declarations(DAPparsestate* state, Object decls, Object decl)
  80. {
  81. OClist* alist = (OClist*)decls;
  82. if(alist == NULL)
  83. alist = oclistnew();
  84. else
  85. oclistpush(alist,(ocelem)decl);
  86. return alist;
  87. }
  88. Object
  89. dap_arraydecls(DAPparsestate* state, Object arraydecls, Object arraydecl)
  90. {
  91. OClist* alist = (OClist*)arraydecls;
  92. if(alist == NULL)
  93. alist = oclistnew();
  94. else
  95. oclistpush(alist,(ocelem)arraydecl);
  96. return alist;
  97. }
  98. Object
  99. dap_arraydecl(DAPparsestate* state, Object name, Object size)
  100. {
  101. long value;
  102. OCnode* dim;
  103. if(!check_int32(size,&value))
  104. dap_parse_error(state,"Dimension not an integer");
  105. if(name != NULL)
  106. dim = newocnode((char*)name,OC_Dimension,state);
  107. else
  108. dim = newocnode(NULL,OC_Dimension,state);
  109. dim->dim.declsize = value;
  110. return dim;
  111. }
  112. Object
  113. dap_attrlist(DAPparsestate* state, Object attrlist, Object attrtuple)
  114. {
  115. OClist* alist = (OClist*)attrlist;
  116. if(alist == NULL)
  117. alist = oclistnew();
  118. else {
  119. char* dupname;
  120. if(attrtuple != NULL) {/* NULL=>alias encountered, ignore */
  121. oclistpush(alist,(ocelem)attrtuple);
  122. if((dupname=scopeduplicates(alist))!=NULL) {
  123. dap_parse_error(state,"Duplicate attribute names in same scope: %s",dupname);
  124. /* Remove this attribute */
  125. oclistpop(alist);
  126. }
  127. }
  128. }
  129. return alist;
  130. }
  131. Object
  132. dap_attrvalue(DAPparsestate* state, Object valuelist, Object value, Object etype)
  133. {
  134. OClist* alist = (OClist*)valuelist;
  135. if(alist == NULL) alist = oclistnew();
  136. /* Watch out for null values */
  137. if(value == NULL) value = "";
  138. oclistpush(alist,(ocelem)strdup(value));
  139. return alist;
  140. }
  141. Object
  142. dap_attribute(DAPparsestate* state, Object name, Object values, Object etype)
  143. {
  144. OCnode* att;
  145. att = newocnode((char*)name,OC_Attribute,state);
  146. att->etype = octypefor(etype);
  147. att->att.values = (OClist*)values;
  148. return att;
  149. }
  150. Object
  151. dap_attrset(DAPparsestate* state, Object name, Object attributes)
  152. {
  153. OCnode* attset;
  154. attset = newocnode((char*)name,OC_Attributeset,state);
  155. /* Check var set vs global set */
  156. attset->att.isglobal = isglobalname(name);
  157. attset->subnodes = (OClist*)attributes;
  158. addedges(attset);
  159. return attset;
  160. }
  161. static int
  162. isglobalname(char* name)
  163. {
  164. int len = strlen(name);
  165. int glen = strlen("global");
  166. char* p;
  167. if(len < glen) return 0;
  168. p = name + (len - glen);
  169. if(strcasecmp(p,"global") != 0)
  170. return 0;
  171. return 1;
  172. }
  173. #if 0
  174. static int
  175. isnumber(const char* text)
  176. {
  177. for(;*text;text++) {if(!isdigit(*text)) return 0;}
  178. return 1;
  179. }
  180. #endif
  181. static void
  182. dimension(OCnode* node, OClist* dimensions)
  183. {
  184. unsigned int i;
  185. unsigned int rank = oclistlength(dimensions);
  186. node->array.dimensions = (OClist*)dimensions;
  187. node->array.rank = rank;
  188. for(i=0;i<rank;i++) {
  189. OCnode* dim = (OCnode*)oclistget(node->array.dimensions,i);
  190. dim->dim.array = node;
  191. dim->dim.arrayindex = i;
  192. #if 0
  193. if(dim->name == NULL) {
  194. dim->dim.anonymous = 1;
  195. dim->name = dimnameanon(node->name,i);
  196. }
  197. #endif
  198. }
  199. }
  200. char*
  201. dimnameanon(char* basename, unsigned int index)
  202. {
  203. char name[64];
  204. sprintf(name,"%s_%d",basename,index);
  205. return strdup(name);
  206. }
  207. Object
  208. dap_makebase(DAPparsestate* state, Object name, Object etype, Object dimensions)
  209. {
  210. OCnode* node;
  211. node = newocnode((char*)name,OC_Primitive,state);
  212. node->etype = octypefor(etype);
  213. dimension(node,(OClist*)dimensions);
  214. return node;
  215. }
  216. Object
  217. dap_makestructure(DAPparsestate* state, Object name, Object dimensions, Object fields)
  218. {
  219. OCnode* node;
  220. char* dupname;
  221. if((dupname=scopeduplicates((OClist*)fields))!= NULL) {
  222. dap_parse_error(state,"Duplicate structure field names in same scope: %s.%s",(char*)name,dupname);
  223. return (Object)NULL;
  224. }
  225. node = newocnode(name,OC_Structure,state);
  226. node->subnodes = fields;
  227. dimension(node,(OClist*)dimensions);
  228. addedges(node);
  229. return node;
  230. }
  231. Object
  232. dap_makesequence(DAPparsestate* state, Object name, Object members)
  233. {
  234. OCnode* node;
  235. char* dupname;
  236. if((dupname=scopeduplicates((OClist*)members)) != NULL) {
  237. dap_parse_error(state,"Duplicate sequence member names in same scope: %s.%s",(char*)name,dupname);
  238. return (Object)NULL;
  239. }
  240. node = newocnode(name,OC_Sequence,state);
  241. node->subnodes = members;
  242. addedges(node);
  243. return node;
  244. }
  245. Object
  246. dap_makegrid(DAPparsestate* state, Object name, Object arraydecl, Object mapdecls)
  247. {
  248. OCnode* node;
  249. /* Check for duplicate map names */
  250. char* dupname;
  251. if((dupname=scopeduplicates((OClist*)mapdecls)) != NULL) {
  252. dap_parse_error(state,"Duplicate grid map names in same scope: %s.%s",(char*)name,dupname);
  253. return (Object)NULL;
  254. }
  255. node = newocnode(name,OC_Grid,state);
  256. node->subnodes = (OClist*)mapdecls;
  257. oclistinsert(node->subnodes,0,(ocelem)arraydecl);
  258. addedges(node);
  259. return node;
  260. }
  261. static void
  262. addedges(OCnode* node)
  263. {
  264. unsigned int i;
  265. if(node->subnodes == NULL) return;
  266. for(i=0;i<oclistlength(node->subnodes);i++) {
  267. OCnode* subnode = (OCnode*)oclistget(node->subnodes,i);
  268. subnode->container = node;
  269. }
  270. }
  271. static void
  272. setroot(OCnode* root, OClist* ocnodes)
  273. {
  274. int i;
  275. for(i=0;i<oclistlength(ocnodes);i++) {
  276. OCnode* node = (OCnode*)oclistget(ocnodes,i);
  277. node->root = root;
  278. }
  279. }
  280. int
  281. daperror(DAPparsestate* state, const char* msg)
  282. {
  283. dap_parse_error(state,msg);
  284. return 0;
  285. }
  286. static char*
  287. flatten(char* s, char* tmp, int tlen)
  288. {
  289. int c;
  290. char* p,*q;
  291. strncpy(tmp,s,tlen);
  292. tmp[tlen] = '\0';
  293. p = (q = tmp);
  294. while((c=*p++)) {
  295. switch (c) {
  296. case '\r': case '\n': break;
  297. case '\t': *q++ = ' '; break;
  298. case ' ': if(*p != ' ') *q++ = c; break;
  299. default: *q++ = c;
  300. }
  301. }
  302. *q = '\0';
  303. return tmp;
  304. }
  305. /* Create an ocnode and capture in the state->ocnode list */
  306. static OCnode*
  307. newocnode(char* name, OCtype octype, DAPparsestate* state)
  308. {
  309. OCnode* node = ocmakenode(name,octype,state->root);
  310. oclistpush(state->ocnodes,(ocelem)node);
  311. return node;
  312. }
  313. static int
  314. check_int32(char* val, long* value)
  315. {
  316. char* ptr;
  317. int ok = 1;
  318. long iv = strtol(val,&ptr,0); /* 0=>auto determine base */
  319. if((iv == 0 && val == ptr) || *ptr != '\0') {ok=0; iv=1;}
  320. else if(iv > OC_INT32_MAX || iv < OC_INT32_MIN) ok=0;
  321. if(value != NULL) *value = iv;
  322. return ok;
  323. }
  324. static char*
  325. scopeduplicates(OClist* list)
  326. {
  327. unsigned int i,j;
  328. for(i=0;i<oclistlength(list);i++) {
  329. OCnode* io = (OCnode*)oclistget(list,i);
  330. for(j=i+1;j<oclistlength(list);j++) {
  331. OCnode* jo = (OCnode*)oclistget(list,j);
  332. if(strcmp(io->name,jo->name)==0)
  333. return io->name;
  334. }
  335. }
  336. return NULL;
  337. }
  338. static OCtype
  339. octypefor(Object etype)
  340. {
  341. switch ((long)etype) {
  342. case SCAN_BYTE: return OC_Byte;
  343. case SCAN_INT16: return OC_Int16;
  344. case SCAN_UINT16: return OC_UInt16;
  345. case SCAN_INT32: return OC_Int32;
  346. case SCAN_UINT32: return OC_UInt32;
  347. case SCAN_FLOAT32: return OC_Float32;
  348. case SCAN_FLOAT64: return OC_Float64;
  349. case SCAN_URL: return OC_URL;
  350. case SCAN_STRING: return OC_String;
  351. default: abort();
  352. }
  353. return OC_NAT;
  354. }
  355. void
  356. dap_parse_error(DAPparsestate* state, const char *fmt, ...)
  357. {
  358. size_t len, suffixlen, prefixlen;
  359. va_list argv;
  360. char* tmp = NULL;
  361. va_start(argv,fmt);
  362. (void) vfprintf(stderr,fmt,argv) ;
  363. (void) fputc('\n',stderr) ;
  364. len = strlen(state->lexstate->input);
  365. suffixlen = strlen(state->lexstate->next);
  366. prefixlen = (len - suffixlen);
  367. tmp = (char*)ocmalloc(len+1);
  368. flatten(state->lexstate->input,tmp,prefixlen);
  369. (void) fprintf(stderr,"context: %s",tmp);
  370. flatten(state->lexstate->next,tmp,suffixlen);
  371. (void) fprintf(stderr,"^%s\n",tmp);
  372. (void) fflush(stderr); /* to ensure log files are current */
  373. ocfree(tmp);
  374. }
  375. static void
  376. dap_parse_cleanup(DAPparsestate* state)
  377. {
  378. daplexcleanup(&state->lexstate);
  379. if(state->ocnodes != NULL) ocfreenodes(state->ocnodes);
  380. state->ocnodes = NULL;
  381. free(state);
  382. }
  383. static DAPparsestate*
  384. dap_parse_init(char* buf)
  385. {
  386. DAPparsestate* state = (DAPparsestate*)ocmalloc(sizeof(DAPparsestate)); /*ocmalloc zeros*/
  387. MEMCHECK(state,(DAPparsestate*)NULL);
  388. if(buf==NULL) {
  389. dap_parse_error(state,"dap_parse_init: no input buffer");
  390. dap_parse_cleanup(state);
  391. return NULL;
  392. }
  393. daplexinit(buf,&state->lexstate);
  394. return state;
  395. }
  396. /* Wrapper for dapparse */
  397. OCerror
  398. DAPparse(OCstate* conn, OCtree* tree, char* parsestring)
  399. {
  400. DAPparsestate* state = dap_parse_init(parsestring);
  401. int parseresult;
  402. OCerror ocerr = OC_NOERR;
  403. state->ocnodes = oclistnew();
  404. state->conn = conn;
  405. if(ocdebug >= 2)
  406. dapdebug = 1;
  407. parseresult = dapparse(state);
  408. if(parseresult == 0) {/* 0 => parse ok */
  409. /* Check to see if we ended up parsing an error message */
  410. if(state->svcerror) {
  411. conn->error.code = nulldup(state->code);
  412. conn->error.message = nulldup(state->message);
  413. tree->root = NULL;
  414. /* Attempt to further decipher the error code */
  415. if(state->code != NULL
  416. && (strcmp(state->code,"404") == 0 /* tds returns 404 */
  417. || strcmp(state->code,"5") == 0)) /* hyrax returns 5 */
  418. ocerr = OC_ENOFILE;
  419. else
  420. ocerr = OC_EDAPSVC;
  421. } else {
  422. OCASSERT((state->root != NULL));
  423. tree->root = state->root;
  424. state->root = NULL; /* avoid reclaim */
  425. tree->nodes = state->ocnodes;
  426. state->ocnodes = NULL; /* avoid reclaim */
  427. tree->root->tree = tree;
  428. ocerr = OC_NOERR;
  429. }
  430. } else { /* Parse failed */
  431. switch (tree->dxdclass) {
  432. case OCDAS: ocerr = OC_EDAS; break;
  433. case OCDDS: ocerr = OC_EDDS; break;
  434. case OCDATADDS: ocerr = OC_EDATADDS; break;
  435. default: ocerr = OC_EDAPSVC;
  436. }
  437. }
  438. dap_parse_cleanup(state);
  439. return ocerr;
  440. }