Parser moved.

This commit is contained in:
dcp1990 2005-06-13 21:00:07 +00:00
parent 7fdb393ada
commit d8eeaa1f3c
3 changed files with 131 additions and 22 deletions

View File

@ -1,6 +1,6 @@
# cnd Makefile
# (C)2005, Dan Ponte
# $Amigan: phoned/phoned/Makefile,v 1.9 2005/06/12 15:22:56 dcp1990 Exp $
# $Amigan: phoned/phoned/Makefile,v 1.10 2005/06/13 21:00:07 dcp1990 Exp $
include ../global.mk
# basic stuff. we append for a reason.
CPPFLAGS+=-I../include -DDEBUG -DYY_NO_UNPUT
@ -8,8 +8,8 @@ CFLAGS+=-g -Wall -W -ansi ${CPPFLAGS} -pthread
LDFLAGS+=-lutil -lpcre
# keep these up to date.
MAINBIN=phoned
SRCS=main.c init.c log.c cfg.c socket.c y.tab.c lex.yy.c signals.c cid.c modem.c notify.c filters.c
OBJS=main.o init.o log.o cfg.o socket.o y.tab.o lex.yy.o signals.o cid.o modem.o notify.o filters.o
SRCS=main.c init.c log.c cfg.c socket.c y.tab.c lex.yy.c signals.c cid.c modem.c notify.c filters.c remote.c
OBJS=main.o init.o log.o cfg.o socket.o y.tab.o lex.yy.o signals.o cid.o modem.o notify.o filters.o remote.o
OHDRS=y.tab.h
CLEANFILES=y.tab.c y.tab.h lex.yy.c
LEX=lex

128
phoned/remote.c Normal file
View File

@ -0,0 +1,128 @@
/*
* Copyright (c) 2005, Dan Ponte
*
* remote.c - parser for use with clients.
* MAKE IT THREAD SAFE, FOR PETE'S SAKE!
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Amigan: phoned/phoned/remote.c,v 1.1 2005/06/13 21:00:07 dcp1990 Exp $ */
/* system includes */
#include <string.h>
#include <stdio.h>
#include <pthread.h>
/* us */
#include <phoned.h>
#define MAXBUFSIZE 512
#define MAXARGS 15
#define CHK(m) strcasecmp(m, argvect[cpos]) == 0
#define RNF(m) s->freeit = 0; free(is); return(m)
typedef enum stat {
init = 0,
login,
pass
} states_t;
typedef struct si_t {
states_t st;
short freeit;
FILE* fpo;
} state_info_t;
char *parse_command(cmd, cont, s)
const char *cmd;
short *cont;
state_info_t *s;
{
char **ap, *is, *argvect[MAXARGS];
int cpos = 0;
memset(argvect, 0, sizeof(argvect));
is = strdup(cmd);
*cont = 0x1;
for(ap = argvect; (*ap = strsep(&is, " \t")) != NULL;)
if(**ap != '\0')
if(++ap >= &argvect[MAXARGS])
break;
/* begin checking */
switch(s->st) {
case init:
if(CHK("login")) {
++cpos;
if(argvect[cpos] != NULL) {
/* TODO: put login stuff here */
RNF("100 PASSWORD: Please give me a password.\n");
} else {
RNF("513 ERROR: Syntax error: Needs usernamd as argument.\n");
}
} else if(CHK("tnotify")) {
cid_t c;
c.number = "7823020300";
c.name = "BUSH,GEORGE W.";
c.day = 2;
c.hour = 3;
c.month = 4;
c.minute = 20;
cid_notify(&c);
cid_log(&c);
RNF("500 OK: Notify tested.\n");
} else if(CHK("tparse")) {
cid_t* rc;
rc = parse_cid("802701083132323130383234070F5354414E444953482048454154494E020A343031333937333337325C\n");
lprintf(info, "nam=%s;month=%d\n", rc->name, rc->month);
cid_notify(rc);
cid_log(rc);
RNF("500 OK: Parser tested.\n");
} else if(CHK("gmm")) {
give_me_modem(argvect[1] != NULL ? argvect[1] : "AT\r\n");
RNF("500 OK: Give Me Modem tested.\n");
}
break;
case login:
break;
case pass:
break;
}
return NULL;
}
void begin_dialogue(fp)
FILE* fp;
{
char buffer[MAXBUFSIZE]; /* molto importante */
char *rcode, *c;
short keep_going = 0x1;
state_info_t si;
memset(&si, 0, sizeof(state_info_t));
si.fpo = fp; /* this is JUST for data, not real commands */
while(keep_going == 0x1 && !feof(fp)) {
if(fgets(buffer, MAXBUFSIZE, fp) == 0x0) break;
if((c = strrchr(buffer, '\n')) != NULL)
*c = '\0';
rcode = parse_command(buffer, &keep_going, &si);
if(rcode != NULL) {
fputs(rcode, fp);
if(si.freeit) free(rcode);
}
}
lprintf(info, "Connection closed in begin_dialogue\n");
}

View File

@ -58,25 +58,6 @@ void *handclient(k)
int rlen;
rlen = recv(sk, buffer, sizeof(buffer), 0);
lprintf(debug, "Client said %s.", buffer);
if(strcmp(buffer, "tnotify") == 0) {
cid_t c;
c.number = "7823020300";
c.name = "BUSH,GEORGE W.";
c.day = 2;
c.hour = 3;
c.month = 4;
c.minute = 20;
cid_notify(&c);
cid_log(&c);
} else if(strcmp(buffer, "tparse") == 0) {
cid_t* rc;
rc = parse_cid("802701083132323130383234070F5354414E444953482048454154494E020A343031333937333337325C\n");
lprintf(info, "nam=%s;month=%d\n", rc->name, rc->month);
cid_notify(rc);
cid_log(rc);
} else if(strcmp(buffer, "gmm") == 0) {
give_me_modem("AT\r\n");
}
close(sk);
pthread_exit(NULL);
return 0;