cleanup interface between interface.[ch] and command.[ch]

primarily move all buffer2array stuff from interface.c to command.c
and write a new command function explicitly for dealing with commandLists

git-svn-id: https://svn.musicpd.org/mpd/trunk@701 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2004-04-12 01:44:52 +00:00
parent f74991b9fd
commit b68417a643
3 changed files with 93 additions and 54 deletions

View File

@ -31,6 +31,7 @@
#include "permission.h" #include "permission.h"
#include "audio.h" #include "audio.h"
#include "buffer2array.h" #include "buffer2array.h"
#include "log.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -708,25 +709,59 @@ CommandEntry * getCommandEntryFromString(char * string, int * permission) {
return cmd; return cmd;
} }
int processCommand(FILE * fp, unsigned int * permission, int argArrayLength, int processCommandInternal(FILE * fp, unsigned int * permission,
char ** argArray, ListNode * commandNode) char * commandString,
ListNode * commandNode)
{ {
int argArrayLength;
char ** argArray;
CommandEntry * cmd; CommandEntry * cmd;
int ret = -1;
argArrayLength = buffer2array(commandString,&argArray);
if(argArrayLength == 0) return 0; if(argArrayLength == 0) return 0;
if(NULL==(cmd = getCommandEntryAndCheckArgcAndPermission(fp,permission, if((cmd = getCommandEntryAndCheckArgcAndPermission(fp,permission,
argArrayLength,argArray))) 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) { freeArgArray(argArray,argArrayLength);
return cmd->handler(fp, permission, argArrayLength, argArray);
} return ret;
else {
return cmd->listHandler(fp, permission, argArrayLength,
argArray, commandNode, cmd);
}
} }
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);
}

View File

@ -31,8 +31,10 @@
#define COMMAND_RESPOND_ERROR "ACK" #define COMMAND_RESPOND_ERROR "ACK"
#define COMMAND_RESPOND_OK "OK" #define COMMAND_RESPOND_OK "OK"
int processCommand(FILE * fp, unsigned int * permission, int argArrayLength, int proccessListOfCommands(FILE * fp, int * permission, int * expired,
char ** argArray, ListNode * commandNode); List * list);
int processCommand(FILE * fp, unsigned int * permission, char * commandString);
void initCommands(); void initCommands();

View File

@ -17,7 +17,6 @@
*/ */
#include "interface.h" #include "interface.h"
#include "buffer2array.h"
#include "command.h" #include "command.h"
#include "conf.h" #include "conf.h"
#include "list.h" #include "list.h"
@ -222,40 +221,28 @@ int interfaceReadInput(Interface * interface) {
closeInterface(interface); closeInterface(interface);
} }
else if(interface->buffer[interface->bufferLength-1]=='\n') { else if(interface->buffer[interface->bufferLength-1]=='\n') {
char ** argArray;
int argArrayLength;
interface->buffer[interface->bufferLength-1] = '\0'; interface->buffer[interface->bufferLength-1] = '\0';
interface->bufferLength = 0; interface->bufferLength = 0;
argArrayLength = buffer2array(interface->buffer,&argArray);
if(interface->commandList) { if(interface->commandList) {
if(argArrayLength==0); if(strcmp(interface->buffer,
else if(strcmp(argArray[0],INTERFACE_LIST_MODE_END)==0) { INTERFACE_LIST_MODE_END)==0)
ListNode * node = interface->commandList->firstNode; {
ListNode * tempNode; DEBUG("interface %i: process command "
ret = 0; "list\n",interface->num);
ret = proccessListOfCommands(
while(node!=NULL) { interface->fp,
char ** argArray; &(interface->permission),
int argArrayLength; &(interface->expired),
argArrayLength = buffer2array((char *)node->data,&argArray); interface->commandList);
DEBUG("interface %i: process command \"%s\"\n",interface->num,node->data); DEBUG("interface %i: process command "
ret = processCommand(interface->fp,&(interface->permission),argArrayLength,argArray,node); "list returned %i\n",
DEBUG("interface %i: command returned %i\n",interface->num,ret); interface->num,
freeArgArray(argArray,argArrayLength); ret);
tempNode = node->nextNode;
deleteNodeFromList(
interface->commandList,
node);
node = tempNode;
if(ret!=0 || interface->expired)
{
node = NULL;
}
}
if(ret==0) { 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 || else if(ret==COMMAND_RETURN_CLOSE ||
interface->expired) { interface->expired) {
@ -267,21 +254,34 @@ int interfaceReadInput(Interface * interface) {
interface->commandList = NULL; interface->commandList = NULL;
} }
else { else {
interface->commandListSize+=sizeof(ListNode); interface->commandListSize+=
interface->commandListSize+=strlen(interface->buffer)+1; sizeof(ListNode);
if(interface->commandListSize>interface_max_command_list_size) { interface->commandListSize+=
ERROR("interface %i: command list size (%lli) is larger than the max (%lli)\n",interface->num,interface->commandListSize,interface_max_command_list_size); 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); closeInterface(interface);
} }
else { else {
insertInListWithoutKey(interface->commandList,strdup(interface->buffer)); insertInListWithoutKey(
interface->commandList,
strdup(interface->
buffer));
} }
} }
} }
else { else {
if(argArrayLength && if(strcmp(interface->buffer,
strcmp(argArray[0],
INTERFACE_LIST_MODE_BEGIN)==0) INTERFACE_LIST_MODE_BEGIN)==0)
{ {
interface->commandList = makeList(free); interface->commandList = makeList(free);
@ -290,8 +290,7 @@ int interfaceReadInput(Interface * interface) {
ret = 1; ret = 1;
} }
else { else {
if(argArrayLength==0) ret = 0; if(strcmp(interface->buffer,
else if(strcmp(argArray[0],
INTERFACE_LIST_MODE_END) INTERFACE_LIST_MODE_END)
==0) ==0)
{ {
@ -300,7 +299,11 @@ int interfaceReadInput(Interface * interface) {
} }
else { else {
DEBUG("interface %i: process command \"%s\"\n",interface->num,interface->buffer); 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); DEBUG("interface %i: command returned %i\n",interface->num,ret);
} }
if(ret==0) { if(ret==0) {
@ -313,7 +316,6 @@ int interfaceReadInput(Interface * interface) {
printInterfaceOutBuffer(interface); printInterfaceOutBuffer(interface);
} }
} }
freeArgArray(argArray,argArrayLength);
} }
return ret; return ret;
} }