-static int unescape(char *input, char *output, size_t buflen) {
- char *ch, *ch2;
- int e=0;
-
- if (*input=='\"') {
- for (ch=input;*ch;ch++) {
- if(ch - input >= buflen) {
- parseError="Buffer overflow";
- return 0;
- }
- }
-
- if (*(ch-1) != '\"') {
- parseError="Quote mismatch";
- return 0;
- }
-
- *(ch-1)='\0';
- input++;
- }
-
- ch2=output;
- for (ch=input;*ch;ch++) {
- if(ch - input >= buflen) {
- parseError="Buffer overflow";
- return 0;
- }
-
- if (e) {
- e=0;
- *ch2++=*ch;
- } else if (*ch=='\\') {
- e=1;
- } else {
- *ch2++=*ch;
- }
- }
- *ch2='\0';
-
- return 1;
-}
-
-struct searchNode *search_parse(searchCtx *ctx, char *cinput) {
- /* OK, we need to split the input into chunks on spaces and brackets.. */
- char *argvector[100];
- char inputb[1024];
- char *input;
- char thestring[500];
- int i,j,q=0,e=0;
- char *ch;
- struct Command *cmd;
- struct searchNode *thenode;
-
- strlcpy(inputb, cinput, sizeof(inputb));
- input = inputb;
-
- /* If it starts with a bracket, it's a function call.. */
- if (*input=='(') {
- /* Skip past string */
- for (ch=input;*ch;ch++);
- if (*(ch-1) != ')') {
- parseError = "Bracket mismatch!";
- return NULL;
- }
- input++;
- *(ch-1)='\0';
-
- /* Split further args */
- i=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */
- j=0; /* j = current arg */
- e=0;
- q=0;
- argvector[0]="";
- for (ch=input;*ch;ch++) {
- /*printf("i: %d j: %d e: %d q: %d ch: '%c'\n", i, j, e, q, *ch);*/
- if (i==-1) {
- argvector[j]=ch;
- if (*ch=='(') {
- i=1;
- } else if (*ch != ' ') {
- i=0;
- if (*ch=='\\') {
- e=1;
- } else if (*ch=='\"') {
- q=1;
- }
- }
- } else if (e==1) {
- e=0;
- } else if (q==1) {
- if (*ch=='\\') {
- e=1;
- } else if (*ch=='\"') {
- q=0;
- }
- } else if (i==0) {
- if (*ch=='\\') {
- e=1;
- } else if (*ch=='\"') {
- q=1;
- } else if (*ch==' ') {
- *ch='\0';
- j++;
- if(j >= (sizeof(argvector) / sizeof(*argvector))) {
- parseError = "Too many arguments";
- return NULL;
- }
- i=-1;
- }
- } else {
- if (*ch=='\\') {
- e=1;
- } else if (*ch=='\"') {
- q=1;
- } else if (*ch=='(') {
- i++;
- } else if (*ch==')') {
- i--;
- }
- }
- }
-
- if (i>0) {
- parseError = "Bracket mismatch!";
- return NULL;
- }
-
- if (*(ch-1) == 0) /* if the last character was a space */
- j--; /* remove an argument */
-
-/* for(k=1;k<=j;k++)
- if(!unescape(argvector[k], argvector[k], sizeof(inputb)))
- return NULL;
-*/
-
- if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
- parseError = "Unknown command (for valid command list, see help <searchcmd>)";
- return NULL;
- } else {
- if (!controlpermitted(cmd->level, ctx->sender)) {
- parseError = "Access denied (for valid command list, see help <searchcmd>)";
- return NULL;
- }
- return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
- }
- } else {
- /* Literal */
-
- /* slug: disabled now we unescape during the main parse stage */
- if(!unescape(input, thestring, sizeof(thestring)))
- return NULL;
-
- if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
- parseError = "malloc: could not allocate memory for this search.";
- return NULL;
- }
-
- thenode->localdata = getsstring(thestring,512);
- thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING;
- thenode->exe = literal_exe;
- thenode->free = literal_free;
-
- return thenode;
- }
-}
-