/*
 *
 * fowcb.c -- robot front end for fowc.c
 *
 * This code is copyright 1995 by James A. Cherry, jac@doe.carleton.ca.
 * It is not to be redistributed or modified without his permission.
 *
 */

#include <varargs.h>
#include "funddefs.h"
#include "globals.h"
#include "socket.h"
#include "botutil.h"
#include "fowc.h"
#include "proto.h"


void ProcessWordsCommand( name )
    char *name;
/* Deal with a `words' command from someone on the DOoM. */
{
    char *pe, cmn[10];
    int cmtype, pagearg;
    
    pe = NULL;
    if( SType == WORDS_ROBOT ) {
	if( strstr( name, "connector" ) != NULL ) return;
	if( CurrState == CONSULT_MODE )
	    ChangeState( CONSULT_MODE );
	pe = InRecH( "\n" ) - 1;
	strcpy( RName, name );
    }
    pagearg = 0;
    if( Command == CMD_WORDS ) {
	if( ParseInputArgs( CmdPtr, pe ) != 0 ) return;
	WMatches = -1;
	CTok = 0;
	FreeEntry = 0;
	TreeHead = BuildExpressionTree();
	if( InterpretArgs() != 0 ) {
	    FreeArgv();
	    return;
	}
	SayExpressionTree();
	ApplyDemorgan( TreeHead );
	cmtype = WC_WORDS;
    } else {
	for( pe = CmdPtr; *pe == ' '; pe++ );
	if( *pe == '-' ) pe++;
	for( ; *pe >= '0' && *pe <= '9'; pe++ );
	if( *pe != ' ' && *pe != '"' && *pe != '\0' ) {
	    if( Command == CMD_MORE ) strcpy( cmn, "more" );
	    if( Command == CMD_FORWARD ) strcpy( cmn, "forward" );
	    if( Command == CMD_BACK ) strcpy( cmn, "back" );
	    if( Command == CMD_GOTO ) strcpy( cmn, "goto" );
	    if( Command == CMD_LINES ) strcpy( cmn, "lines" );
	    sprintf( WriteBuf,
		     "-%s Error: invalid argument to `%s' Command.",
		     RName, cmn );
	    QueueCommand( WriteBuf, 0 );
	    return;
	}
	if( *pe == '\0' ) {
	    if( Command == CMD_MORE || Command == CMD_FORWARD
		|| Command == CMD_BACK || Command == CMD_GOTO ) pagearg = 1;
	    if( Command == CMD_LINES ) {
		if( MWYSize < 10 ) {
		    sprintf( WriteBuf,
			     "-%s I am set to display %s line%s of matches.",
			     RName, NumNames[MWYSize],
			     MWYSize == 1 ? "" : "s" );
		} else {
		    sprintf( WriteBuf,
			     "-%s I am set to display %d lines of matches.",
			     RName, MWYSize );
		}
		QueueCommand( WriteBuf, 0 );
		return;
	    }
	} else sscanf( CmdPtr, "%d", &pagearg );
	if( Command == CMD_LINES ) {
	    MWYSize = pagearg;
	    if( MWYSize < 1 ) MWYSize = 1;
	    if( MWYSize > 20 ) MWYSize = 20;
	    if( MWYSize < 10 )
		sprintf( WriteBuf, "-%s Now displaying %s line%s of matches.",
			 RName, NumNames[MWYSize], ( MWYSize == 1 ? "" : "s" )
		    );
	    else
		sprintf( WriteBuf, "-%s Now displaying %d lines of matches.",
			 RName, MWYSize );
	    QueueCommand( WriteBuf, 0 );
	    return;
	}
	if( Command == CMD_BACK ) pagearg = -pagearg;
	if( Command == CMD_GOTO ) cmtype = WC_GOTO;
	else cmtype = WC_PAGE;
    }

    if( SType == WORDS_ROBOT ) {
	sprintf( WriteBuf, "%s %d %d PERFORM_SEARCH",
		 name, cmtype, pagearg );
	QueueCommand( WriteBuf, 0 );
    } else if( SType == WORDS_CRITIQUE ) {
	PerformSearch( cmtype, pagearg );
    }
}

void LPrint( const char *str, ... )
/* Print something to the logfile. */
{
    va_list args;

    if( Logging == LOG_NO ) return;
    va_start( args );
    vfprintf( LogFile, str, args );
    va_end( args );
    fflush( LogFile );
}

/* Stubs for critique.c */

void OPrint( const char *str, ... )
/* Say something to the person who did the last `words' command. */
{
    char fstr[200];
    va_list args;

    va_start( args );
    sprintf( fstr, "-%s %s", RName, str );
    vsprintf( WriteBuf, fstr, args );
    QueueCommand( WriteBuf, 0 );
    va_end( args );
}

void CritGridShow()
{
}

void WHeaderLine()
{
}

