2018-12-07 06:47:47 +08:00
|
|
|
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
|
2013-07-28 04:54:50 +08:00
|
|
|
See the COPYRIGHT file for more information.
|
|
|
|
*/
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "dapparselex.h"
|
2015-11-30 11:38:12 +08:00
|
|
|
#include "dapy.h"
|
2023-11-27 19:36:03 +08:00
|
|
|
#include <stddef.h>
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
|
|
|
|
static void addedges(OCnode* node);
|
2017-03-09 08:01:10 +08:00
|
|
|
static void setroot(OCnode*,NClist*);
|
2012-08-01 04:34:13 +08:00
|
|
|
static int isglobalname(const char* name);
|
|
|
|
static int isdodsname(const char* name);
|
|
|
|
static OCnode* newocnode(char* name, OCtype octype, DAPparsestate* state);
|
|
|
|
static OCtype octypefor(Object etype);
|
2017-03-09 08:01:10 +08:00
|
|
|
static NClist* scopeduplicates(NClist* list);
|
2012-08-01 04:34:13 +08:00
|
|
|
static int check_int32(char* val, long* value);
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************/
|
|
|
|
|
|
|
|
/* Switch to DAS parsing SCAN_WORD definition */
|
|
|
|
|
|
|
|
/* Use the initial keyword to indicate what we are parsing */
|
|
|
|
void
|
|
|
|
dap_tagparse(DAPparsestate* state, int kind)
|
|
|
|
{
|
|
|
|
switch (kind) {
|
|
|
|
case SCAN_DATASET:
|
|
|
|
case SCAN_ERROR:
|
|
|
|
break;
|
|
|
|
case SCAN_ATTR:
|
|
|
|
dapsetwordchars(state->lexstate,1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,"tagparse: Unknown tag argument: %d\n",kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_datasetbody(DAPparsestate* state, Object name, Object decls)
|
|
|
|
{
|
|
|
|
OCnode* root = newocnode((char*)name,OC_Dataset,state);
|
2012-11-22 07:57:42 +08:00
|
|
|
char* dupname = NULL;
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* dups = scopeduplicates((NClist*)decls);
|
2013-07-28 04:20:13 +08:00
|
|
|
if(dups != NULL) {
|
2012-11-22 07:57:42 +08:00
|
|
|
/* Sometimes, some servers (i.e. Thredds)
|
|
|
|
return a dds with duplicate field names
|
|
|
|
at the dataset level; simulate an errorbody response
|
|
|
|
*/
|
2013-07-28 04:20:13 +08:00
|
|
|
ocnodes_free(dups);
|
2020-06-29 09:07:08 +08:00
|
|
|
dap_parse_error(state,"Duplicate dataset field names: %s",(char*)name,dupname);
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = OC_ENAMEINUSE;
|
|
|
|
return (Object)NULL;
|
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
root->subnodes = (NClist*)decls;
|
2012-08-01 04:34:13 +08:00
|
|
|
OCASSERT((state->root == NULL));
|
|
|
|
state->root = root;
|
|
|
|
state->root->root = state->root; /* make sure to cross link */
|
|
|
|
addedges(root);
|
|
|
|
setroot(root,state->ocnodes);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_attributebody(DAPparsestate* state, Object attrlist)
|
|
|
|
{
|
2013-07-28 04:20:13 +08:00
|
|
|
OCnode* node;
|
|
|
|
/* Check for and remove attribute duplicates */
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* dups = scopeduplicates((NClist*)attrlist);
|
2013-07-28 04:20:13 +08:00
|
|
|
if(dups != NULL) {
|
|
|
|
ocnodes_free(dups);
|
|
|
|
dap_parse_error(state,"Duplicate attribute names in same scope");
|
|
|
|
state->error = OC_ENAMEINUSE; /* semantic error */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
node = newocnode(NULL,OC_Attributeset,state);
|
2012-08-01 04:34:13 +08:00
|
|
|
OCASSERT((state->root == NULL));
|
|
|
|
state->root = node;
|
|
|
|
/* make sure to cross link */
|
|
|
|
state->root->root = state->root;
|
2017-03-09 08:01:10 +08:00
|
|
|
node->subnodes = (NClist*)attrlist;
|
2012-08-01 04:34:13 +08:00
|
|
|
addedges(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dap_errorbody(DAPparsestate* state,
|
|
|
|
Object code, Object msg, Object ptype, Object prog)
|
|
|
|
{
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = OC_EDAPSVC;
|
2012-08-01 04:34:13 +08:00
|
|
|
state->code = nulldup((char*)code);
|
|
|
|
state->message = nulldup((char*)msg);
|
|
|
|
/* Ignore ptype and prog for now */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dap_unrecognizedresponse(DAPparsestate* state)
|
|
|
|
{
|
|
|
|
/* see if this is an HTTP error */
|
|
|
|
unsigned int httperr = 0;
|
|
|
|
int i;
|
|
|
|
char iv[32];
|
2018-08-05 03:22:29 +08:00
|
|
|
(void)sscanf(state->lexstate->input,"%u ",&httperr);
|
2012-08-01 04:34:13 +08:00
|
|
|
sprintf(iv,"%u",httperr);
|
|
|
|
state->lexstate->next = state->lexstate->input;
|
|
|
|
/* Limit the amount of input to prevent runaway */
|
|
|
|
for(i=0;i<4096;i++) {if(state->lexstate->input[i] == '\0') break;}
|
|
|
|
state->lexstate->input[i] = '\0';
|
|
|
|
dap_errorbody(state,iv,state->lexstate->input,NULL,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_declarations(DAPparsestate* state, Object decls, Object decl)
|
|
|
|
{
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* alist = (NClist*)decls;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(alist == NULL)
|
2017-03-09 08:01:10 +08:00
|
|
|
alist = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
else
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistpush(alist,(void*)decl);
|
2012-08-01 04:34:13 +08:00
|
|
|
return alist;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_arraydecls(DAPparsestate* state, Object arraydecls, Object arraydecl)
|
|
|
|
{
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* alist = (NClist*)arraydecls;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(alist == NULL)
|
2017-03-09 08:01:10 +08:00
|
|
|
alist = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
else
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistpush(alist,(void*)arraydecl);
|
2012-08-01 04:34:13 +08:00
|
|
|
return alist;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_arraydecl(DAPparsestate* state, Object name, Object size)
|
|
|
|
{
|
|
|
|
long value;
|
|
|
|
OCnode* dim;
|
2012-11-22 07:57:42 +08:00
|
|
|
if(!check_int32(size,&value)) {
|
2012-08-01 04:34:13 +08:00
|
|
|
dap_parse_error(state,"Dimension not an integer");
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = OC_EDIMSIZE; /* signal semantic error */
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
if(name != NULL)
|
|
|
|
dim = newocnode((char*)name,OC_Dimension,state);
|
|
|
|
else
|
|
|
|
dim = newocnode(NULL,OC_Dimension,state);
|
|
|
|
dim->dim.declsize = value;
|
|
|
|
return dim;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_attrlist(DAPparsestate* state, Object attrlist, Object attrtuple)
|
|
|
|
{
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* alist = (NClist*)attrlist;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(alist == NULL)
|
2017-03-09 08:01:10 +08:00
|
|
|
alist = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
else {
|
|
|
|
if(attrtuple != NULL) {/* NULL=>alias encountered, ignore */
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistpush(alist,(void*)attrtuple);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return alist;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_attrvalue(DAPparsestate* state, Object valuelist, Object value, Object etype)
|
|
|
|
{
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* alist = (NClist*)valuelist;
|
|
|
|
if(alist == NULL) alist = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Watch out for null values */
|
|
|
|
if(value == NULL) value = "";
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistpush(alist,(void*)strdup(value));
|
2012-08-01 04:34:13 +08:00
|
|
|
return alist;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_attribute(DAPparsestate* state, Object name, Object values, Object etype)
|
|
|
|
{
|
|
|
|
OCnode* att;
|
|
|
|
att = newocnode((char*)name,OC_Attribute,state);
|
|
|
|
att->etype = octypefor(etype);
|
2017-03-09 08:01:10 +08:00
|
|
|
att->att.values = (NClist*)values;
|
2012-08-01 04:34:13 +08:00
|
|
|
return att;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_attrset(DAPparsestate* state, Object name, Object attributes)
|
|
|
|
{
|
|
|
|
OCnode* attset;
|
|
|
|
attset = newocnode((char*)name,OC_Attributeset,state);
|
|
|
|
/* Check var set vs global set */
|
|
|
|
attset->att.isglobal = isglobalname(name);
|
|
|
|
attset->att.isdods = isdodsname(name);
|
2017-03-09 08:01:10 +08:00
|
|
|
attset->subnodes = (NClist*)attributes;
|
2012-08-01 04:34:13 +08:00
|
|
|
addedges(attset);
|
|
|
|
return attset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
isglobalname(const char* name)
|
|
|
|
{
|
|
|
|
int len = strlen(name);
|
|
|
|
int glen = strlen("global");
|
|
|
|
const char* p;
|
|
|
|
if(len < glen) return 0;
|
|
|
|
p = name + (len - glen);
|
|
|
|
if(strcasecmp(p,"global") != 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
isdodsname(const char* name)
|
|
|
|
{
|
2014-03-09 11:41:30 +08:00
|
|
|
size_t len = strlen(name);
|
|
|
|
size_t glen = strlen("DODS");
|
2012-08-01 04:34:13 +08:00
|
|
|
if(len < glen) return 0;
|
|
|
|
if(ocstrncmp(name,"DODS",glen) != 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int
|
|
|
|
isnumber(const char* text)
|
|
|
|
{
|
|
|
|
for(;*text;text++) {if(!isdigit(*text)) return 0;}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2017-03-09 08:01:10 +08:00
|
|
|
dimension(OCnode* node, NClist* dimensions)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2017-03-09 08:01:10 +08:00
|
|
|
unsigned int rank = nclistlength(dimensions);
|
|
|
|
node->array.dimensions = (NClist*)dimensions;
|
2012-08-01 04:34:13 +08:00
|
|
|
node->array.rank = rank;
|
|
|
|
for(i=0;i<rank;i++) {
|
2017-03-09 08:01:10 +08:00
|
|
|
OCnode* dim = (OCnode*)nclistget(node->array.dimensions,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
dim->dim.array = node;
|
|
|
|
dim->dim.arrayindex = i;
|
|
|
|
#if 0
|
|
|
|
if(dim->name == NULL) {
|
|
|
|
dim->dim.anonymous = 1;
|
|
|
|
dim->name = dimnameanon(node->name,i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
dimnameanon(char* basename, unsigned int index)
|
|
|
|
{
|
|
|
|
char name[64];
|
|
|
|
sprintf(name,"%s_%d",basename,index);
|
|
|
|
return strdup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_makebase(DAPparsestate* state, Object name, Object etype, Object dimensions)
|
|
|
|
{
|
|
|
|
OCnode* node;
|
|
|
|
node = newocnode((char*)name,OC_Atomic,state);
|
|
|
|
node->etype = octypefor(etype);
|
2017-03-09 08:01:10 +08:00
|
|
|
dimension(node,(NClist*)dimensions);
|
2012-08-01 04:34:13 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_makestructure(DAPparsestate* state, Object name, Object dimensions, Object fields)
|
|
|
|
{
|
|
|
|
OCnode* node;
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* dups = scopeduplicates((NClist*)fields);
|
2013-07-28 04:20:13 +08:00
|
|
|
if(dups != NULL) {
|
|
|
|
ocnodes_free(dups);
|
|
|
|
dap_parse_error(state,"Duplicate structure field names in same structure: %s",(char*)name);
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = OC_ENAMEINUSE; /* semantic error */
|
2012-08-01 04:34:13 +08:00
|
|
|
return (Object)NULL;
|
|
|
|
}
|
|
|
|
node = newocnode(name,OC_Structure,state);
|
|
|
|
node->subnodes = fields;
|
2017-03-09 08:01:10 +08:00
|
|
|
dimension(node,(NClist*)dimensions);
|
2012-08-01 04:34:13 +08:00
|
|
|
addedges(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_makesequence(DAPparsestate* state, Object name, Object members)
|
|
|
|
{
|
|
|
|
OCnode* node;
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* dups = scopeduplicates((NClist*)members);
|
2013-07-28 04:20:13 +08:00
|
|
|
if(dups != NULL) {
|
|
|
|
ocnodes_free(dups);
|
|
|
|
dap_parse_error(state,"Duplicate sequence member names in same sequence: %s",(char*)name);
|
2012-08-01 04:34:13 +08:00
|
|
|
return (Object)NULL;
|
|
|
|
}
|
|
|
|
node = newocnode(name,OC_Sequence,state);
|
|
|
|
node->subnodes = members;
|
|
|
|
addedges(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object
|
|
|
|
dap_makegrid(DAPparsestate* state, Object name, Object arraydecl, Object mapdecls)
|
|
|
|
{
|
|
|
|
OCnode* node;
|
|
|
|
/* Check for duplicate map names */
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* dups = scopeduplicates((NClist*)mapdecls);
|
2013-07-28 04:20:13 +08:00
|
|
|
if(dups != NULL) {
|
|
|
|
ocnodes_free(dups);
|
|
|
|
dap_parse_error(state,"Duplicate grid map names in same grid: %s",(char*)name);
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = OC_ENAMEINUSE; /* semantic error */
|
2012-08-01 04:34:13 +08:00
|
|
|
return (Object)NULL;
|
|
|
|
}
|
|
|
|
node = newocnode(name,OC_Grid,state);
|
2017-03-09 08:01:10 +08:00
|
|
|
node->subnodes = (NClist*)mapdecls;
|
|
|
|
nclistinsert(node->subnodes,0,(void*)arraydecl);
|
2012-08-01 04:34:13 +08:00
|
|
|
addedges(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addedges(OCnode* node)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
if(node->subnodes == NULL) return;
|
2017-03-09 08:01:10 +08:00
|
|
|
for(i=0;i<nclistlength(node->subnodes);i++) {
|
|
|
|
OCnode* subnode = (OCnode*)nclistget(node->subnodes,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
subnode->container = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-09 08:01:10 +08:00
|
|
|
setroot(OCnode* root, NClist* ocnodes)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-03-09 11:41:30 +08:00
|
|
|
size_t i;
|
2017-03-09 08:01:10 +08:00
|
|
|
for(i=0;i<nclistlength(ocnodes);i++) {
|
|
|
|
OCnode* node = (OCnode*)nclistget(ocnodes,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
node->root = root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
daperror(DAPparsestate* state, const char* msg)
|
2012-11-22 07:57:42 +08:00
|
|
|
{
|
|
|
|
return dapsemanticerror(state,OC_EINVAL,msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dapsemanticerror(DAPparsestate* state, OCerror err, const char* msg)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
dap_parse_error(state,msg);
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = err; /* semantic error */
|
2012-08-01 04:34:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2014-03-09 11:41:30 +08:00
|
|
|
flatten(char* s, char* tmp, size_t tlen)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char* p,*q;
|
|
|
|
strncpy(tmp,s,tlen);
|
|
|
|
tmp[tlen] = '\0';
|
|
|
|
p = (q = tmp);
|
|
|
|
while((c=*p++)) {
|
|
|
|
switch (c) {
|
|
|
|
case '\r': case '\n': break;
|
|
|
|
case '\t': *q++ = ' '; break;
|
|
|
|
case ' ': if(*p != ' ') *q++ = c; break;
|
|
|
|
default: *q++ = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an ocnode and capture in the state->ocnode list */
|
|
|
|
static OCnode*
|
|
|
|
newocnode(char* name, OCtype octype, DAPparsestate* state)
|
|
|
|
{
|
|
|
|
OCnode* node = ocnode_new(name,octype,state->root);
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistpush(state->ocnodes,(void*)node);
|
2012-08-01 04:34:13 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_int32(char* val, long* value)
|
|
|
|
{
|
|
|
|
char* ptr;
|
|
|
|
int ok = 1;
|
|
|
|
long iv = strtol(val,&ptr,0); /* 0=>auto determine base */
|
|
|
|
if((iv == 0 && val == ptr) || *ptr != '\0') {ok=0; iv=1;}
|
|
|
|
else if(iv > OC_INT32_MAX || iv < OC_INT32_MIN) ok=0;
|
|
|
|
if(value != NULL) *value = iv;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
static NClist*
|
|
|
|
scopeduplicates(NClist* list)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
unsigned int i,j;
|
2023-11-27 19:36:03 +08:00
|
|
|
size_t len = nclistlength(list);
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* dups = NULL;
|
2013-07-28 04:20:13 +08:00
|
|
|
for(i=0;i<len;i++) {
|
2017-03-09 08:01:10 +08:00
|
|
|
OCnode* io = (OCnode*)nclistget(list,i);
|
2013-07-28 04:20:13 +08:00
|
|
|
retry:
|
|
|
|
for(j=i+1;j<len;j++) {
|
2017-03-09 08:01:10 +08:00
|
|
|
OCnode* jo = (OCnode*)nclistget(list,j);
|
2013-07-28 04:20:13 +08:00
|
|
|
if(strcmp(io->name,jo->name)==0) {
|
2017-03-09 08:01:10 +08:00
|
|
|
if(dups == NULL) dups = nclistnew();
|
|
|
|
nclistpush(dups,jo);
|
|
|
|
nclistremove(list,j);
|
2013-07-28 04:20:13 +08:00
|
|
|
len--;
|
|
|
|
goto retry;
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-28 04:20:13 +08:00
|
|
|
return dups;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static OCtype
|
|
|
|
octypefor(Object etype)
|
|
|
|
{
|
2021-12-24 13:18:56 +08:00
|
|
|
switch ((uintptr_t)etype) {
|
2012-08-01 04:34:13 +08:00
|
|
|
case SCAN_BYTE: return OC_Byte;
|
|
|
|
case SCAN_INT16: return OC_Int16;
|
|
|
|
case SCAN_UINT16: return OC_UInt16;
|
|
|
|
case SCAN_INT32: return OC_Int32;
|
|
|
|
case SCAN_UINT32: return OC_UInt32;
|
|
|
|
case SCAN_FLOAT32: return OC_Float32;
|
|
|
|
case SCAN_FLOAT64: return OC_Float64;
|
|
|
|
case SCAN_URL: return OC_URL;
|
|
|
|
case SCAN_STRING: return OC_String;
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
return OC_NAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dap_parse_error(DAPparsestate* state, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
size_t len, suffixlen, prefixlen;
|
|
|
|
va_list argv;
|
|
|
|
char* tmp = NULL;
|
|
|
|
va_start(argv,fmt);
|
|
|
|
(void) vfprintf(stderr,fmt,argv) ;
|
|
|
|
(void) fputc('\n',stderr) ;
|
|
|
|
len = strlen(state->lexstate->input);
|
|
|
|
suffixlen = strlen(state->lexstate->next);
|
|
|
|
prefixlen = (len - suffixlen);
|
|
|
|
tmp = (char*)ocmalloc(len+1);
|
|
|
|
flatten(state->lexstate->input,tmp,prefixlen);
|
|
|
|
(void) fprintf(stderr,"context: %s",tmp);
|
|
|
|
flatten(state->lexstate->next,tmp,suffixlen);
|
|
|
|
(void) fprintf(stderr,"^%s\n",tmp);
|
|
|
|
(void) fflush(stderr); /* to ensure log files are current */
|
|
|
|
ocfree(tmp);
|
2012-08-03 02:43:21 +08:00
|
|
|
va_end(argv);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dap_parse_cleanup(DAPparsestate* state)
|
|
|
|
{
|
|
|
|
daplexcleanup(&state->lexstate);
|
|
|
|
if(state->ocnodes != NULL) ocnodes_free(state->ocnodes);
|
|
|
|
state->ocnodes = NULL;
|
2018-10-31 10:48:12 +08:00
|
|
|
nullfree(state->code);
|
|
|
|
nullfree(state->message);
|
2012-08-01 04:34:13 +08:00
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DAPparsestate*
|
|
|
|
dap_parse_init(char* buf)
|
|
|
|
{
|
|
|
|
DAPparsestate* state = (DAPparsestate*)ocmalloc(sizeof(DAPparsestate)); /*ocmalloc zeros*/
|
|
|
|
MEMCHECK(state,NULL);
|
|
|
|
if(buf==NULL) {
|
|
|
|
dap_parse_error(state,"dap_parse_init: no input buffer");
|
2012-11-22 07:57:42 +08:00
|
|
|
state->error = OC_EINVAL; /* semantic error */
|
2012-08-01 04:34:13 +08:00
|
|
|
dap_parse_cleanup(state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
daplexinit(buf,&state->lexstate);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapper for dapparse */
|
|
|
|
OCerror
|
|
|
|
DAPparse(OCstate* conn, OCtree* tree, char* parsestring)
|
|
|
|
{
|
|
|
|
DAPparsestate* state = dap_parse_init(parsestring);
|
|
|
|
int parseresult;
|
|
|
|
OCerror ocerr = OC_NOERR;
|
2017-03-09 08:01:10 +08:00
|
|
|
state->ocnodes = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
state->conn = conn;
|
|
|
|
if(ocdebug >= 2)
|
|
|
|
dapdebug = 1;
|
|
|
|
parseresult = dapparse(state);
|
|
|
|
if(parseresult == 0) {/* 0 => parse ok */
|
2012-11-22 07:57:42 +08:00
|
|
|
if(state->error == OC_EDAPSVC) {
|
|
|
|
/* we ended up parsing an error message from server */
|
2012-08-01 04:34:13 +08:00
|
|
|
conn->error.code = nulldup(state->code);
|
|
|
|
conn->error.message = nulldup(state->message);
|
|
|
|
tree->root = NULL;
|
|
|
|
/* Attempt to further decipher the error code */
|
|
|
|
if(state->code != NULL
|
|
|
|
&& (strcmp(state->code,"404") == 0 /* tds returns 404 */
|
|
|
|
|| strcmp(state->code,"5") == 0)) /* hyrax returns 5 */
|
|
|
|
ocerr = OC_ENOFILE;
|
|
|
|
else
|
|
|
|
ocerr = OC_EDAPSVC;
|
2012-11-22 07:57:42 +08:00
|
|
|
} else if(state->error != OC_NOERR) {
|
|
|
|
/* Parse failed for semantic reasons */
|
|
|
|
ocerr = state->error;
|
2012-08-01 04:34:13 +08:00
|
|
|
} else {
|
|
|
|
tree->root = state->root;
|
|
|
|
state->root = NULL; /* avoid reclaim */
|
|
|
|
tree->nodes = state->ocnodes;
|
|
|
|
state->ocnodes = NULL; /* avoid reclaim */
|
|
|
|
tree->root->tree = tree;
|
|
|
|
ocerr = OC_NOERR;
|
|
|
|
}
|
|
|
|
} else { /* Parse failed */
|
|
|
|
switch (tree->dxdclass) {
|
|
|
|
case OCDAS: ocerr = OC_EDAS; break;
|
|
|
|
case OCDDS: ocerr = OC_EDDS; break;
|
|
|
|
case OCDATADDS: ocerr = OC_EDATADDS; break;
|
|
|
|
default: ocerr = OC_EDAPSVC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dap_parse_cleanup(state);
|
|
|
|
return ocerr;
|
|
|
|
}
|
|
|
|
|