Back to index

wims  3.65+svn20090927
cmd.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 /* daemon command processing */
00019 
00020        /* testing routine. Don't touch! */
00021 void cmd_test(char *p)
00022 {
00023 }
00024 
00025 void cmd_ping(char *p)
00026 {
00027     char *p1;
00028     int qtest;
00029     p1=find_word_start(p); *find_word_end(p1)=0;
00030     if(*p1!=0 && qbuf[0]!=0 && wordchr(qbuf,p1)!=NULL) qtest=1; else qtest=0;
00031     snprintf(textbuf+3,MAX_LINELEN,"%d %s\n%s",qtest,loadavg,ipbuf);
00032 }
00033 
00034 void cmd_forklist(char *p)
00035 {
00036     snprintf(textbuf+3,MAX_LINELEN,"%d",forkcnt);
00037 }
00038 
00039 void cmd_readfile(char *p)
00040 {
00041     char *cut[2];
00042     int l;
00043     cutwords(p,cut,2);
00044     if(cut[1][0]) l=evalue(cut[1]); else l=MAX_LINELEN-16;
00045     if(l<=0 || cut[0][0]==0) return;
00046     if(l>BUFFERLEN-16) l=BUFFERLEN-16;
00047     readfile(cut[0],textbuf+3,l);
00048 }
00049 
00050 void cmd_record(char *p)
00051 {
00052     char *cut[2];
00053     cutwords(p,cut,2);
00054     if(cut[1][0]==0) return;
00055     datafile_fnd_record(cut[1],atoi(cut[0]),textbuf+3);
00056 }
00057 
00058 void cmd_recordcnt(char *p)
00059 {
00060     char *p1;
00061     p1=find_word_start(p);
00062     if(*p1==0) return;
00063     *find_word_end(p1)=0;
00064     snprintf(textbuf+3,16,"%d",datafile_recordnum(p1));
00065 }
00066 
00067 struct {
00068     char *name;
00069     void (*routine) (char *p);
00070 } cmdlist[]={
00071         {"forkcnt",  cmd_forklist},
00072         {"forkcount",       cmd_forklist},
00073         {"forklist", cmd_forklist},
00074         {"getscore", cmd_getscore},
00075        {"ping",      cmd_ping},
00076        {"readfile",  cmd_readfile},
00077        {"record",    cmd_record},
00078        {"recordcnt", cmd_recordcnt},
00079        {"recordcount",      cmd_recordcnt},
00080        {"recordno",  cmd_recordcnt},
00081        {"recordnum", cmd_recordcnt},
00082         {"scorelog", cmd_scorelog},
00083         {"test",     cmd_test},
00084 };
00085 #define cmdcnt (sizeof(cmdlist)/sizeof(cmdlist[0]))
00086 
00087 void cmd(void)
00088 {
00089     char *p1, *p2, cmdline[MAX_LINELEN+1];
00090     int i;
00091     
00092     errno=0;
00093     p1=find_word_start(textptr); p2=find_word_end(p1);
00094     if(*p2) *p2++=0;
00095     i=search_list(cmdlist,cmdcnt,sizeof(cmdlist[0]),p1);
00096     if(i<0) {sockerror(2,"bad_cmd %s",p1); return;}
00097     mystrncpy(cmdline,find_word_start(p2),sizeof(cmdline));
00098     memmove(textbuf,"OK\n",4);
00099     cmdlist[i].routine(cmdline);
00100 }
00101