2018-12-07 06:47:47 +08:00
|
|
|
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
|
2012-08-01 04:34:13 +08:00
|
|
|
See the COPYRIGHT file for more information. */
|
|
|
|
|
|
|
|
#include "config.h"
|
2012-09-29 07:02:35 +08:00
|
|
|
#ifdef HAVE_STRINGS_H
|
2012-08-01 04:34:13 +08:00
|
|
|
#include <strings.h>
|
2012-09-29 07:02:35 +08:00
|
|
|
#endif
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
#include "dapparselex.h"
|
2015-11-30 11:38:12 +08:00
|
|
|
#include "dapy.h"
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
#undef URLCVT /* NEVER turn this on */
|
2015-10-21 05:44:26 +08:00
|
|
|
|
2018-03-16 22:38:40 +08:00
|
|
|
/* Do we %xx decode all or part of a DAP Identifier: see dapdecode() */
|
2017-08-31 09:13:52 +08:00
|
|
|
#define DECODE_PARTIAL
|
2017-08-30 04:11:15 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
#define DAP2ENCODE
|
2015-10-21 05:44:26 +08:00
|
|
|
#ifdef DAP2ENCODE
|
|
|
|
#define KEEPSLASH
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static void dumptoken(DAPlexstate* lexstate);
|
|
|
|
static void dapaddyytext(DAPlexstate* lex, int c);
|
|
|
|
#ifndef DAP2ENCODE
|
|
|
|
static int tohex(int c);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************/
|
|
|
|
|
|
|
|
#ifdef INFORMATIONAL
|
|
|
|
/* Set of all ascii printable characters */
|
|
|
|
static char ascii[] = " !\"#$%&'()*+,-./:;<=>?@[]\\^_`|{}~";
|
|
|
|
|
|
|
|
/* Define the set of legal nonalphanum characters as specified in the DAP2 spec. */
|
|
|
|
static char* daplegal ="_!~*'-\"";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char* ddsworddelims =
|
|
|
|
"{}[]:;=,";
|
|
|
|
|
|
|
|
/* Define 1 and > 1st legal characters */
|
2017-11-03 03:04:03 +08:00
|
|
|
/* Note: for some reason I added # and removed !~'"
|
|
|
|
what was I thinking?
|
|
|
|
*/
|
2018-10-02 05:51:43 +08:00
|
|
|
static const char* ddswordchars1 =
|
2017-11-03 03:04:03 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
"-+_/%\\.*!~'\"";
|
2018-10-02 05:51:43 +08:00
|
|
|
static const char* ddswordcharsn =
|
2017-11-03 03:04:03 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
"-+_/%\\.*!~'\"";
|
|
|
|
|
|
|
|
/* This includes sharp and colon for historical reasons */
|
2012-08-01 04:34:13 +08:00
|
|
|
static char* daswordcharsn =
|
2017-11-03 03:04:03 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
"-+_/%\\.*#:!~'\"";
|
|
|
|
|
|
|
|
/* Need to remove '.' to allow for fqns */
|
2018-10-02 05:51:43 +08:00
|
|
|
static const char* cewordchars1 =
|
2017-11-03 03:04:03 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
"-+_/%\\*!~'\"";
|
2018-10-02 05:51:43 +08:00
|
|
|
static const char* cewordcharsn =
|
2017-11-03 03:04:03 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
"-+_/%\\*!~'\"";
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Current sets of legal characters */
|
|
|
|
/*
|
|
|
|
static char* wordchars1 = NULL;
|
|
|
|
static char* wordcharsn = NULL;
|
|
|
|
static char* worddelims = NULL;
|
|
|
|
*/
|
|
|
|
|
2018-10-02 05:51:43 +08:00
|
|
|
static const char* keywords[] = {
|
2012-08-01 04:34:13 +08:00
|
|
|
"alias",
|
|
|
|
"array",
|
|
|
|
"attributes",
|
|
|
|
"byte",
|
|
|
|
"dataset",
|
|
|
|
"error",
|
|
|
|
"float32",
|
|
|
|
"float64",
|
|
|
|
"grid",
|
|
|
|
"int16",
|
|
|
|
"int32",
|
|
|
|
"maps",
|
|
|
|
"sequence",
|
|
|
|
"string",
|
|
|
|
"structure",
|
|
|
|
"uint16",
|
|
|
|
"uint32",
|
|
|
|
"url",
|
|
|
|
"code",
|
|
|
|
"message",
|
|
|
|
"program_type",
|
|
|
|
"program",
|
|
|
|
NULL /* mark end of the keywords list */
|
|
|
|
};
|
|
|
|
|
2018-10-02 05:51:43 +08:00
|
|
|
static const int keytokens[] = {
|
2012-08-01 04:34:13 +08:00
|
|
|
SCAN_ALIAS,
|
|
|
|
SCAN_ARRAY,
|
|
|
|
SCAN_ATTR,
|
|
|
|
SCAN_BYTE,
|
|
|
|
SCAN_DATASET,
|
|
|
|
SCAN_ERROR,
|
|
|
|
SCAN_FLOAT32,
|
|
|
|
SCAN_FLOAT64,
|
|
|
|
SCAN_GRID,
|
|
|
|
SCAN_INT16,
|
|
|
|
SCAN_INT32,
|
|
|
|
SCAN_MAPS,
|
|
|
|
SCAN_SEQUENCE,
|
|
|
|
SCAN_STRING,
|
|
|
|
SCAN_STRUCTURE,
|
|
|
|
SCAN_UINT16,
|
|
|
|
SCAN_UINT32,
|
|
|
|
SCAN_URL,
|
|
|
|
SCAN_CODE,
|
|
|
|
SCAN_MESSAGE,
|
|
|
|
SCAN_PTYPE,
|
|
|
|
SCAN_PROG
|
|
|
|
};
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
|
|
|
|
int
|
|
|
|
daplex(YYSTYPE* lvalp, DAPparsestate* state)
|
|
|
|
{
|
|
|
|
DAPlexstate* lexstate = state->lexstate;
|
|
|
|
int token;
|
|
|
|
int c;
|
|
|
|
unsigned int i;
|
|
|
|
char* p;
|
|
|
|
char* tmp;
|
2012-08-20 05:12:35 +08:00
|
|
|
YYSTYPE lval = NULL;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
token = 0;
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytesclear(lexstate->yytext);
|
2012-08-01 04:34:13 +08:00
|
|
|
/* invariant: p always points to current char */
|
|
|
|
for(p=lexstate->next;token==0&&(c=*p);p++) {
|
|
|
|
if(c == '\n') {
|
|
|
|
lexstate->lineno++;
|
|
|
|
} else if(c <= ' ' || c == '\177') {
|
|
|
|
/* whitespace: ignore */
|
|
|
|
} else if(c == '#') {
|
|
|
|
/* single line comment */
|
|
|
|
while((c=*(++p))) {if(c == '\n') break;}
|
|
|
|
} else if(strchr(lexstate->worddelims,c) != NULL) {
|
|
|
|
/* don't put in lexstate->yytext to avoid memory leak */
|
|
|
|
token = c;
|
|
|
|
} else if(c == '"') {
|
|
|
|
int more = 1;
|
|
|
|
/* We have a string token; will be reported as WORD_STRING */
|
|
|
|
while(more && (c=*(++p))) {
|
2015-10-21 05:44:26 +08:00
|
|
|
if(c == '"') {
|
2012-08-01 04:34:13 +08:00
|
|
|
more = 0;
|
2015-10-21 05:44:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef DAP2ENCODE
|
|
|
|
if(c == '\\') {
|
|
|
|
/* Resolve spec ambiguity about handling of \c:
|
|
|
|
1. !KEEPSLASH: convert \c to c for any character c
|
|
|
|
2. KEEPSLASH: convert \c to \c for any character c;
|
|
|
|
that is, keep the backslash.
|
|
|
|
It is clear that the problem being addressed was \".
|
|
|
|
But it is unclear what to to do about \n: convert to
|
|
|
|
Ascii LF or leave as \n.
|
|
|
|
This code will leave as \n and assume higher levels
|
|
|
|
of code will address the issue.
|
|
|
|
*/
|
|
|
|
#ifdef KEEPSLASH
|
|
|
|
dapaddyytext(lexstate,c);
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
c=*(++p);
|
|
|
|
if(c == '\0') more = 0;
|
|
|
|
}
|
|
|
|
#else /*Non-standard*/
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
c=*(++p);
|
|
|
|
switch (c) {
|
|
|
|
case 'r': c = '\r'; break;
|
|
|
|
case 'n': c = '\n'; break;
|
|
|
|
case 'f': c = '\f'; break;
|
|
|
|
case 't': c = '\t'; break;
|
|
|
|
case 'x': {
|
|
|
|
int d1,d2;
|
|
|
|
c = '?';
|
|
|
|
++p;
|
|
|
|
d1 = tohex(*p++);
|
|
|
|
if(d1 < 0) {
|
|
|
|
daperror(state,"Illegal \\xDD in TOKEN_STRING");
|
|
|
|
} else {
|
|
|
|
d2 = tohex(*p++);
|
|
|
|
if(d2 < 0) {
|
|
|
|
daperror(state,"Illegal \\xDD in TOKEN_STRING");
|
|
|
|
} else {
|
|
|
|
c=(((unsigned int)d1)<<4) | (unsigned int)d2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
#endif /*!DAP2ENCODE*/
|
|
|
|
if(more) dapaddyytext(lexstate,c);
|
|
|
|
}
|
|
|
|
token=WORD_STRING;
|
|
|
|
} else if(strchr(lexstate->wordchars1,c) != NULL) {
|
|
|
|
int isdatamark = 0;
|
|
|
|
/* we have a WORD_WORD */
|
|
|
|
dapaddyytext(lexstate,c);
|
|
|
|
while((c=*(++p))) {
|
|
|
|
#ifdef URLCVT
|
|
|
|
if(c == '%' && p[1] != 0 && p[2] != 0
|
|
|
|
&& strchr(hexdigits,p[1]) != NULL
|
|
|
|
&& strchr(hexdigits,p[2]) != NULL) {
|
|
|
|
int d1,d2;
|
|
|
|
d1 = tohex(p[1]);
|
|
|
|
d2 = tohex(p[2]);
|
|
|
|
if(d1 >= 0 || d2 >= 0) {
|
|
|
|
c=(((unsigned int)d1)<<4) | (unsigned int)d2;
|
|
|
|
p+=2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(strchr(lexstate->wordcharsn,c) == NULL) {p--; break;}
|
|
|
|
}
|
|
|
|
dapaddyytext(lexstate,c);
|
|
|
|
#else
|
|
|
|
if(strchr(lexstate->wordcharsn,c) == NULL) {p--; break;}
|
|
|
|
dapaddyytext(lexstate,c);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* Special check for Data: */
|
2017-03-09 08:01:10 +08:00
|
|
|
tmp = ncbytescontents(lexstate->yytext);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(strcmp(tmp,"Data")==0 && *p == ':') {
|
|
|
|
dapaddyytext(lexstate,*p); p++;
|
|
|
|
if(p[0] == '\n') {
|
|
|
|
token = SCAN_DATA;
|
|
|
|
isdatamark = 1;
|
|
|
|
p++;
|
|
|
|
} else if(p[0] == '\r' && p[1] == '\n') {
|
|
|
|
token = SCAN_DATA;
|
|
|
|
isdatamark = 1;
|
|
|
|
p+=2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!isdatamark) {
|
|
|
|
/* check for keyword */
|
|
|
|
token=WORD_WORD; /* assume */
|
|
|
|
for(i=0;;i++) {
|
|
|
|
if(keywords[i] == NULL) break;
|
|
|
|
if(strcasecmp(keywords[i],tmp)==0) {
|
|
|
|
token=keytokens[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { /* illegal */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lexstate->next = p;
|
2017-03-09 08:01:10 +08:00
|
|
|
strncpy(lexstate->lasttokentext,ncbytescontents(lexstate->yytext),MAX_TOKEN_LENGTH);
|
2012-08-01 04:34:13 +08:00
|
|
|
lexstate->lasttoken = token;
|
|
|
|
if(ocdebug >= 2)
|
|
|
|
dumptoken(lexstate);
|
|
|
|
|
|
|
|
/*Put return value onto Bison stack*/
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
if(ncbyteslength(lexstate->yytext) == 0)
|
2012-08-20 05:12:35 +08:00
|
|
|
lval = NULL;
|
2012-08-01 04:34:13 +08:00
|
|
|
else {
|
2017-03-09 08:01:10 +08:00
|
|
|
lval = ncbytesdup(lexstate->yytext);
|
|
|
|
nclistpush(lexstate->reclaim,(void*)lval);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2012-08-20 05:12:35 +08:00
|
|
|
if(lvalp) *lvalp = lval;
|
2012-08-01 04:34:13 +08:00
|
|
|
return token; /* Return the type of the token. */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dapaddyytext(DAPlexstate* lex, int c)
|
|
|
|
{
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytesappend(lex->yytext,c);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DAP2ENCODE
|
|
|
|
static int
|
|
|
|
tohex(int c)
|
|
|
|
{
|
|
|
|
if(c >= 'a' && c <= 'f') return (c - 'a') + 0xa;
|
|
|
|
if(c >= 'A' && c <= 'F') return (c - 'A') + 0xa;
|
|
|
|
if(c >= '0' && c <= '9') return (c - '0');
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumptoken(DAPlexstate* lexstate)
|
|
|
|
{
|
2017-03-09 08:01:10 +08:00
|
|
|
fprintf(stderr,"TOKEN = |%s|\n",ncbytescontents(lexstate->yytext));
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Simple lexer
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
dapsetwordchars(DAPlexstate* lexstate, int kind)
|
|
|
|
{
|
|
|
|
switch (kind) {
|
|
|
|
case 0:
|
|
|
|
lexstate->worddelims = ddsworddelims;
|
|
|
|
lexstate->wordchars1 = ddswordchars1;
|
|
|
|
lexstate->wordcharsn = ddswordcharsn;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
lexstate->worddelims = ddsworddelims;
|
|
|
|
lexstate->wordchars1 = ddswordchars1;
|
|
|
|
lexstate->wordcharsn = daswordcharsn;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
lexstate->worddelims = ddsworddelims;
|
|
|
|
lexstate->wordchars1 = cewordchars1;
|
|
|
|
lexstate->wordcharsn = cewordcharsn;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
daplexinit(char* input, DAPlexstate** lexstatep)
|
|
|
|
{
|
2013-03-16 05:44:59 +08:00
|
|
|
DAPlexstate* lexstate;
|
|
|
|
if(lexstatep == NULL) return; /* no point in building it */
|
|
|
|
lexstate = (DAPlexstate*)malloc(sizeof(DAPlexstate));
|
|
|
|
*lexstatep = lexstate;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(lexstate == NULL) return;
|
|
|
|
memset((void*)lexstate,0,sizeof(DAPlexstate));
|
|
|
|
lexstate->input = strdup(input);
|
|
|
|
lexstate->next = lexstate->input;
|
2017-03-09 08:01:10 +08:00
|
|
|
lexstate->yytext = ncbytesnew();
|
|
|
|
lexstate->reclaim = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
dapsetwordchars(lexstate,0); /* Assume DDS */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
daplexcleanup(DAPlexstate** lexstatep)
|
|
|
|
{
|
|
|
|
DAPlexstate* lexstate = *lexstatep;
|
|
|
|
if(lexstate == NULL) return;
|
|
|
|
if(lexstate->input != NULL) ocfree(lexstate->input);
|
|
|
|
if(lexstate->reclaim != NULL) {
|
2017-03-09 08:01:10 +08:00
|
|
|
while(nclistlength(lexstate->reclaim) > 0) {
|
|
|
|
char* word = (char*)nclistpop(lexstate->reclaim);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(word) free(word);
|
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistfree(lexstate->reclaim);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytesfree(lexstate->yytext);
|
2012-08-01 04:34:13 +08:00
|
|
|
free(lexstate);
|
|
|
|
*lexstatep = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dap identifiers will come to us with some
|
|
|
|
characters escaped using the URL notation of
|
|
|
|
%HH. The assumption here is that any character
|
|
|
|
that is encoded is left encoded, except as follows:
|
|
|
|
1. if the encoded character is in fact a legal DAP2 character
|
|
|
|
(alphanum+"_!~*'-\"") then it is decoded, otherwise not.
|
|
|
|
*/
|
2017-08-31 09:13:52 +08:00
|
|
|
#ifdef DECODE_PARTIAL
|
2018-10-02 05:51:43 +08:00
|
|
|
static const char* decodeset = /* Specify which characters are decoded */
|
2012-08-01 04:34:13 +08:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_!~*'-\"";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char*
|
|
|
|
dapdecode(DAPlexstate* lexstate, char* name)
|
|
|
|
{
|
2012-08-22 07:34:25 +08:00
|
|
|
char* decoded = NULL;
|
2017-08-31 09:13:52 +08:00
|
|
|
#ifdef DECODE_PARTIAL
|
2017-08-30 08:01:26 +08:00
|
|
|
decoded = ncuridecodepartial(name,decodeset); /* Decode selected */
|
2017-08-31 09:13:52 +08:00
|
|
|
#else
|
|
|
|
decoded = ncuridecode(name); /* Decode everything */
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
2017-03-09 08:01:10 +08:00
|
|
|
nclistpush(lexstate->reclaim,(void*)decoded);
|
2012-08-01 04:34:13 +08:00
|
|
|
return decoded;
|
|
|
|
}
|