diff --git a/src/command.c b/src/command.c index 5f2ad6eef..90686b7e3 100644 --- a/src/command.c +++ b/src/command.c @@ -31,6 +31,7 @@ #include "permission.h" #include "audio.h" #include "buffer2array.h" +#include "log.h" #include #include @@ -708,25 +709,59 @@ CommandEntry * getCommandEntryFromString(char * string, int * permission) { return cmd; } -int processCommand(FILE * fp, unsigned int * permission, int argArrayLength, - char ** argArray, ListNode * commandNode) +int processCommandInternal(FILE * fp, unsigned int * permission, + char * commandString, + ListNode * commandNode) { + int argArrayLength; + char ** argArray; CommandEntry * cmd; + int ret = -1; + + argArrayLength = buffer2array(commandString,&argArray); if(argArrayLength == 0) return 0; - if(NULL==(cmd = getCommandEntryAndCheckArgcAndPermission(fp,permission, + if((cmd = getCommandEntryAndCheckArgcAndPermission(fp,permission, argArrayLength,argArray))) { - return -1; + if(NULL==commandNode || NULL==cmd->listHandler) { + ret = cmd->handler(fp, permission, argArrayLength, + argArray); + } + else { + ret = cmd->listHandler(fp, permission, argArrayLength, + argArray, commandNode, cmd); + } } - if(NULL==commandNode || NULL==cmd->listHandler) { - return cmd->handler(fp, permission, argArrayLength, argArray); - } - else { - return cmd->listHandler(fp, permission, argArrayLength, - argArray, commandNode, cmd); - } + freeArgArray(argArray,argArrayLength); + + return ret; } +int proccessListOfCommands(FILE * fp, int * permission, int * expired, + List * list) +{ + ListNode * node = list->firstNode; + ListNode * tempNode; + int ret = 0; + + while(node!=NULL) { + DEBUG("proccesListOfCommands: process command \"%s\"\n", + node->data); + ret = processCommandInternal(fp,permission,(char *)node->data, + node); + DEBUG("proccessListOfCommands: command returned %i\n",ret); + tempNode = node->nextNode; + deleteNodeFromList(list,node); + node = tempNode; + if(ret!=0 || (*expired)!=0) node = NULL; + } + + return ret; +} + +int processCommand(FILE * fp, unsigned int * permission, char * commandString) { + return processCommandInternal(fp,permission,commandString,NULL); +} diff --git a/src/command.h b/src/command.h index 247bce573..68d6f4a36 100644 --- a/src/command.h +++ b/src/command.h @@ -31,8 +31,10 @@ #define COMMAND_RESPOND_ERROR "ACK" #define COMMAND_RESPOND_OK "OK" -int processCommand(FILE * fp, unsigned int * permission, int argArrayLength, - char ** argArray, ListNode * commandNode); +int proccessListOfCommands(FILE * fp, int * permission, int * expired, + List * list); + +int processCommand(FILE * fp, unsigned int * permission, char * commandString); void initCommands(); diff --git a/src/interface.c b/src/interface.c index 747ac4097..9499d1049 100644 --- a/src/interface.c +++ b/src/interface.c @@ -17,7 +17,6 @@ */ #include "interface.h" -#include "buffer2array.h" #include "command.h" #include "conf.h" #include "list.h" @@ -222,40 +221,28 @@ int interfaceReadInput(Interface * interface) { closeInterface(interface); } else if(interface->buffer[interface->bufferLength-1]=='\n') { - char ** argArray; - int argArrayLength; - interface->buffer[interface->bufferLength-1] = '\0'; interface->bufferLength = 0; - argArrayLength = buffer2array(interface->buffer,&argArray); if(interface->commandList) { - if(argArrayLength==0); - else if(strcmp(argArray[0],INTERFACE_LIST_MODE_END)==0) { - ListNode * node = interface->commandList->firstNode; - ListNode * tempNode; - ret = 0; - - while(node!=NULL) { - char ** argArray; - int argArrayLength; - argArrayLength = buffer2array((char *)node->data,&argArray); - DEBUG("interface %i: process command \"%s\"\n",interface->num,node->data); - ret = processCommand(interface->fp,&(interface->permission),argArrayLength,argArray,node); - DEBUG("interface %i: command returned %i\n",interface->num,ret); - freeArgArray(argArray,argArrayLength); - tempNode = node->nextNode; - deleteNodeFromList( - interface->commandList, - node); - node = tempNode; - if(ret!=0 || interface->expired) - { - node = NULL; - } - } + if(strcmp(interface->buffer, + INTERFACE_LIST_MODE_END)==0) + { + DEBUG("interface %i: process command " + "list\n",interface->num); + ret = proccessListOfCommands( + interface->fp, + &(interface->permission), + &(interface->expired), + interface->commandList); + DEBUG("interface %i: process command " + "list returned %i\n", + interface->num, + ret); if(ret==0) { - myfprintf(interface->fp,"%s\n",COMMAND_RESPOND_OK); + myfprintf(interface->fp, + "%s\n", + COMMAND_RESPOND_OK); } else if(ret==COMMAND_RETURN_CLOSE || interface->expired) { @@ -267,21 +254,34 @@ int interfaceReadInput(Interface * interface) { interface->commandList = NULL; } else { - interface->commandListSize+=sizeof(ListNode); - interface->commandListSize+=strlen(interface->buffer)+1; - if(interface->commandListSize>interface_max_command_list_size) { - ERROR("interface %i: command list size (%lli) is larger than the max (%lli)\n",interface->num,interface->commandListSize,interface_max_command_list_size); + interface->commandListSize+= + sizeof(ListNode); + interface->commandListSize+= + strlen(interface->buffer)+1; + if(interface->commandListSize > + interface_max_command_list_size) + { + ERROR("interface %i: command " + "list size (%lli) is " + "larger than the max " + "(%lli)\n", + interface->num, + interface-> + commandListSize, + interface_max_command_list_size); closeInterface(interface); } else { - insertInListWithoutKey(interface->commandList,strdup(interface->buffer)); + insertInListWithoutKey( + interface->commandList, + strdup(interface-> + buffer)); } } } else { - if(argArrayLength && - strcmp(argArray[0], + if(strcmp(interface->buffer, INTERFACE_LIST_MODE_BEGIN)==0) { interface->commandList = makeList(free); @@ -290,8 +290,7 @@ int interfaceReadInput(Interface * interface) { ret = 1; } else { - if(argArrayLength==0) ret = 0; - else if(strcmp(argArray[0], + if(strcmp(interface->buffer, INTERFACE_LIST_MODE_END) ==0) { @@ -300,7 +299,11 @@ int interfaceReadInput(Interface * interface) { } else { DEBUG("interface %i: process command \"%s\"\n",interface->num,interface->buffer); - ret = processCommand(interface->fp,&(interface->permission),argArrayLength,argArray,NULL); + ret = processCommand( + interface->fp, + &(interface-> + permission), + interface->buffer); DEBUG("interface %i: command returned %i\n",interface->num,ret); } if(ret==0) { @@ -313,7 +316,6 @@ int interfaceReadInput(Interface * interface) { printInterfaceOutBuffer(interface); } } - freeArgArray(argArray,argArrayLength); } return ret; }