diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-03-07 23:13:44 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-03-07 23:13:44 +0800 |
commit | ae31e19f92e717919ac8e3db9039eb38d2b89aae (patch) | |
tree | c70164d6a1852344f44b04a653ae2815043512af /innbbsd/daemon.c | |
download | pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.gz pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.bz2 pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.lz pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.xz pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.zst pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.zip |
Initial revision
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@1 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'innbbsd/daemon.c')
-rw-r--r-- | innbbsd/daemon.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/innbbsd/daemon.c b/innbbsd/daemon.c new file mode 100644 index 00000000..973a96c8 --- /dev/null +++ b/innbbsd/daemon.c @@ -0,0 +1,173 @@ +#include "daemon.h" +/* +typedef struct daemoncmd { + char *cmdname; + char *usage; + int argc; + int (*main) ARG((FILE*,FILE*,int,char**,char*)); +} daemoncmd_t; + +*/ + +void deargify ARG((char ***)); +static daemoncmd_t *dcmdp=NULL; +static char *startupmessage=NULL; +static int startupcode=100; +static FILE *DIN,*DOUT,*DIO; +typedef int (*F)(); + +void installdaemon(cmds,code,startupmsg) +daemoncmd_t *cmds; +int code; +char *startupmsg; +{ + dcmdp = cmds; + startupcode = code; + startupmessage = startupmsg; +} + +daemoncmd_t *searchcmd(cmd) +char *cmd; +{ + daemoncmd_t *p; + for (p=dcmdp;p->name != NULL ; p++) { +#ifdef DEBUGCMD + printf("searching name %s for cmd %s\n",p->name,cmd); +#endif + if (!strncasecmp(p->name,cmd,1024)) + return p; + } + return NULL; +} + +#if 0 +int daemon(dfd) +int dfd; +{ + static char BUF[1024]; + /*hash_init();*/ + if (dfd > 0) { + DIO = fdopen(dfd,"rw"); + DIN = fdopen(dfd,"r"); + DOUT = fdopen(dfd,"w"); + if (DIO == NULL || DIN == NULL || DOUT == NULL) { + perror("fdopen"); + return -1; + } + } + if (startupmessage) { + fprintf(DOUT,"%d %s\n",startupcode,startupmessage); + fflush(DOUT); + } + while (fgets(BUF,1024,DIN) != NULL) { + int i; + int (*Main)(); + daemoncmd_t *dp; + argv_t Argv; + + char *p=(char*)strchr(BUF,'\r'); + if (p == NULL) p=(char*)strchr(BUF,'\n'); + if (p == NULL) continue; + *p='\0'; + if (p==BUF) continue; + + Argv.argc = 0, Argv.argv = NULL, Argv.inputline=BUF; + Argv.in = DIN, Argv.out = DOUT; + printf("command entered: %s\n",BUF); +#ifdef DEBUGSERVER + fprintf(DOUT,"BUF in client %s\n",BUF); + fprintf(stdout,"BUF in server %s\n",BUF); + fflush(DOUT); +#endif + Argv.argc = argify(BUF,&Argv.argv); +#ifdef DEBUGSERVER + fprintf(stdout,"argc %d argv ",Argv.argc); + for (i=0;i<Argv.argc;++i) + fprintf(stdout,"%s ",Argv.argv[i]); + fprintf(stdout,"\n"); +#endif + dp = searchcmd(Argv.argv[0]); + Argv.dc = dp; + if (dp) { +#ifdef DEBUGSERVER + printf("find cmd %s by %s\n",dp->name,dp->usage); +#endif + if (Argv.argc < dp->argc) { + fprintf(DOUT,"%d Usage: %s\n",dp->errorcode,dp->usage); + fflush(DOUT); + goto cont; + } + if (dp->argno != 0 && Argv.argc > dp->argno) { + fprintf(DOUT,"%d Usage: %s\n",dp->errorcode,dp->usage); + fflush(DOUT); + goto cont; + } + Main=dp->main; + if (Main) { + fflush(stdout); + (*Main)(&Argv); + } + } + else { + fprintf(DOUT,"99 command %s not available\n",Argv.argv[0]); + fflush(DOUT); + } +cont: + deargify(&Argv.argv); + } + /*hash_reclaim();*/ +} +#endif + +#define MAX_ARG 32 +#define MAX_ARG_SIZE 16384 + +int argify(line, argvp) +char *line, ***argvp; +{ + static char *argvbuffer[MAX_ARG+2]; + char **argv = argvbuffer; + int i; + static char argifybuffer[MAX_ARG_SIZE]; + char *p; + while (strchr("\t\n\r ",*line)) line++; + i=strlen(line); + /*p=(char*) mymalloc(i+1);*/ + p = argifybuffer; + strncpy(p,line, sizeof argifybuffer); + for (*argvp = argv, i=0 ;*p && i < MAX_ARG;){ + for (*argv++=p;*p && !strchr("\t\r\n ",*p);p++); + if (*p=='\0') break; + for (*p++='\0'; strchr("\t\r\n ",*p) && *p;p++); + } + *argv = NULL; + return argv - *argvp; +} + +void deargify (argv) +char ***argv; +{ + return; + /*if (*argv != NULL) { + if (*argv[0] != NULL){ + free(*argv[0]); + *argv[0] = NULL; + } + free(*argv); + *argv = NULL; + }*/ +} + +int daemonprintf(format) +char *format; +{ + fprintf(DOUT,format); + fflush(DOUT); +} + +int daemonputs(output) +char* output; +{ + fputs(output, DOUT); + fflush(DOUT); +} |