At this stage, GILD is selecting the write handler and calling it, and

passing the output back to the client; it isn't, however, passing the first
line of input to the client because it has eaten that.
This commit is contained in:
simon 1997-10-10 15:34:56 +00:00
parent b5ed179649
commit b81ad4ac28
4 changed files with 51 additions and 55 deletions

View file

@ -18,12 +18,12 @@ extern char errorBuff[]; /* where I assemble logging messages */
handler * handlers = ( handler *) null;
/* the list of handlers I handle */
void parse_config( char * path)
int parse_config( char * path)
/* parse the config file and identify the handlers I handle */
{
FILE * configFile; /* the file handle from which to load config */
char * line[ 1024]; /* a buffer to read lines into */
int n = 0; /* number of handlers we find */
sprintf( errorBuff, "Loading configuration from %s", path);
error( NOTICE);
@ -54,6 +54,10 @@ void parse_config( char * path)
handler * newhandler =
( struct handler *) malloc( sizeof( struct handler));
/* create a handler */
struct re_pattern_buffer * patternBuff =
( struct re_pattern_buffer *)
malloc( sizeof( struct re_pattern_buffer));
/* and reserve space for a compiled regex */
if ( newhandler == ( handler *) null)
{ /* unlikely, but... best check */
@ -62,15 +66,19 @@ void parse_config( char * path)
error( FATAL_ERROR);
}
regcomp( patternBuff, pattern,
REG_ICASE | REG_NEWLINE);
/* and load it with the values we found */
newhandler->port = port;
newhandler->protocol = strdup( protocol);
newhandler->pattern = regcomp( pattern);
newhandler->pattern = patternBuff;
newhandler->command = strdup( command);
/* then splice it into the handler chain */
newhandler->next = handlers;
handlers = newhandler;
n ++; /* increment the counter */
/* and log it. */
sprintf( errorBuff,
"registering handler [%s] for protocol %s",
@ -93,6 +101,7 @@ void parse_config( char * path)
}
}
}
return ( n); /* say how many we found */
}
char * get_handler_command( char * match)
@ -100,12 +109,13 @@ char * get_handler_command( char * match)
{
handler * h;
char * command = null;
regmatch_t pmatch[ 2];
for ( h = handlers; ( command == null) && ( h != null); h = h->next)
/* scan down the list of handlers looking
for a match... */
{
if ( 0) /* ( regexec( h->pattern, match)) */
if ( regexec( h->pattern, match, 2, pmatch, 0) == 0)
{
command = h->command;
}

13
gild.c
View file

@ -74,7 +74,16 @@ int main( int argc, char * argv[])
}
}
parse_config( configPath);
if ( parse_config( configPath) == 0)
{
sprintf( errorBuff, "failed to load any handlers");
error( FATAL_ERROR);
}
port = handlers->port; /* for now we'll listen on the port of
the last registered handler --
after all, we bomb out if this is
different from any of the others */
keyhole = socket( AF_INET, SOCK_STREAM, 0);
if ( keyhole == -1)
@ -114,7 +123,7 @@ int main( int argc, char * argv[])
sprintf( errorBuff, "failed in listen()?");
error( FATAL_ERROR);
}
sprintf( errorBuff, "GILD: awaiting requests");
sprintf( errorBuff, "GILD: awaiting requests on port %d", port);
error( NOTICE);
for ever

17
gild.h
View file

@ -12,14 +12,10 @@
* *
\**************************************************************************/
#define _POSIX_SOURCE
/* this is a fudge. The whole thing depends on fdopen, and I'm having
great difficulty making it work */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <regexp.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -30,7 +26,10 @@
#define DEFAULT_PORT_NO 1984
#define MAX_PENDING_REQUESTS 5
#define null (((void *) 0))
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define FATAL_ERROR 1
#define NOTICE 0
@ -38,13 +37,15 @@
#define DEBUG 1
#define ever (;;)
#define null (((void *) 0))
typedef struct handler
{
struct handler * next; /* next one down the chain */
int port; /* the port on which I listen */
char * protocol; /* a name for the protocol handled */
regexp * pattern; /* the pattern to match to select this
regex_t * pattern; /* the pattern to match to select this
handler */
char * command; /* the command to invoke this handler */
} handler;
@ -55,7 +56,7 @@ void error( int severity);
char * get_handler_command( char * match);
/* find a handler whose pattern matches match, and return it's command */
void parse_config( char * path);
int parse_config( char * path);
/* parse the config file and identify the handlers I handle */
void wrapper( int conversation);

View file

@ -34,53 +34,20 @@ void wrapper( int conversation)
printf( "wrapper started with fdes [%d]\n",
conversation);
#ifdef glubba
hear = dup( conversation); /* creat two handles on conversation */
say = dup( conversation); /* one for reading and one for writing */
#ifdef globba
for( i = 0; ( i < 1024 ) && ( ( i == 0) || ( firstln[ i - 1] > ' ')); i++)
{
read( hear, ( char *)firstln + i, 1);
/* read a control-terminated line from the
conversation, one byte at a time
(prolly a better way of doing this) */
puts( firstln);
}
firstln[ i] = '\0'; /* terminate the string */
#endif
recv( hear, firstln, 80, MSG_PEEK);
printf( "looking for command to match [%s]\n", firstln);
command = get_handler_command( firstln);
/* and find the appropriate handler */
printf( "got command [%s]\n", command);
if ( ! command) /* didn't find one */
{
sprintf( errorBuff, "no handler registered for %s", firstln);
error( FATAL_ERROR);
}
else /* did find one */
{
sprintf( errorBuff, "using handler %s for protocol %s",
command, firstln);
error( FATAL_ERROR);
}
recv( hear, firstln, 1024, 0);
exit( 0);
if ( dup2( hear, 0) == -1)
if ( dup2( conversation, STDIN_FILENO) == -1)
{
sprintf( errorBuff,
"failed to duplicate conversation [%d] onto stdin: %s",
hear, strerror( errno));
error( FATAL_ERROR);
}
dup2( say, 1);
if ( dup2( conversation, STDOUT_FILENO) == -1)
{
sprintf( errorBuff,
"failed to duplicate conversation [%d] onto stdout: %s",
@ -88,11 +55,20 @@ void wrapper( int conversation)
error( FATAL_ERROR);
}
/* gets( firstln); */
puts( "hello!\n");
/* puts( firstln); */
gets( firstln);
command = get_handler_command( firstln);
/* and find the appropriate handler */
if ( ! command) /* didn't find one */
{
sprintf( errorBuff, "no handler registered for %s", firstln);
error( FATAL_ERROR);
}
else /* did find one */
{
system( command);
}
puts("\tClosing...");
exit( 0);
}