

/* This program can either (1) mark the current time or (2) check to */
/*  see if more than x seconds have passed since that time.          */


#define ERRORLEVEL_SUCCESS       0
#define ERRORLEVEL_SCAN_FAILED   1
#define ERRORLEVEL_BAD_PARAMETER 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <conio.h>
#include <time.h>
#include <dos.h>  /* Dos-specific functions are used to get date/time. */

#include "common.h"


/*------------------------- Subroutines ------------------------------*/

int mark_time()
{
mark_holding_file = fopen(mark_holding_filespec,"wb");
if (mark_holding_file == NULL)
	return(ERRORLEVEL_BAD_PARAMETER);
log_file_to_check = fopen(file_name,"rb");
if (log_file_to_check == NULL)             /* Log file gone? */
  {                                        /*  Write out 0 as the filesize, */
  strcpy(temp_string,file_name);           /*  in case user is checking.... */
  strcat(temp_string,CRLF_string);
  fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
  strcpy(temp_string,"0");
  strcat(temp_string,CRLF_string);
  fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
  fclose(mark_holding_file);
  return(ERRORLEVEL_BAD_PARAMETER);
  }
fseek(log_file_to_check,0,SEEK_END);
file_size_marked = ftell(log_file_to_check);
fclose(log_file_to_check);
strcpy(temp_string,file_name);      /* Write the filespec as the 1st line. */
strcat(temp_string,CRLF_string);
fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
ltoa(file_size_marked,temp_string,10);  /* Now write out current filesize. */
strcat(temp_string,CRLF_string);
fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
fclose(mark_holding_file);
return(ERRORLEVEL_SUCCESS);
}

int scan_file_for_string(char string_to_scan_for[])
{
char the_char;
mark_holding_file = fopen(mark_holding_filespec,"rt");
if (mark_holding_file == NULL)
	return(ERRORLEVEL_BAD_PARAMETER);
fgets(log_file_spec,sizeof(log_file_spec),mark_holding_file);
log_file_spec[strlen(log_file_spec)-1] = 0;
fgets(temp_string,sizeof(temp_string),mark_holding_file);
temp_string[strlen(temp_string)-1] = 0;  /* Cut off LF. */
file_size_marked = atol(temp_string);
fclose(mark_holding_file);
log_file_to_check = fopen(log_file_spec,"rb");
if (log_file_to_check == NULL)
	return(ERRORLEVEL_BAD_PARAMETER);
fseek(log_file_to_check,0,SEEK_END);
file_size_current = ftell(log_file_to_check);
if (file_size_current <= file_size_marked)
	return(ERRORLEVEL_SCAN_FAILED);
fseek(log_file_to_check,file_size_marked,SEEK_SET);
trace_start(string_to_scan_for);
while (ftell(log_file_to_check) < file_size_current)
	{
	fread(&the_char,1,1,log_file_to_check);  /* Read 1 character. */
	trace_process_received_character(the_char);
	if (trace_fired(string_to_scan_for))
		{
		trace_stop(string_to_scan_for,0);
		fclose(log_file_to_check);
		return(ERRORLEVEL_SUCCESS);
    }
  /* Go read next character. */
  }
fclose(log_file_to_check);
return(ERRORLEVEL_SCAN_FAILED);
}

int print_new_part_of_file(char wait_argument[])
{
char the_char;
int wait_time;
mark_holding_file = fopen(mark_holding_filespec,"rt");
if (mark_holding_file == NULL)
  return(ERRORLEVEL_BAD_PARAMETER);
fgets(log_file_spec,sizeof(log_file_spec),mark_holding_file);
log_file_spec[strlen(log_file_spec)-1] = 0;
fgets(temp_string,sizeof(temp_string),mark_holding_file);
temp_string[strlen(temp_string)-1] = 0;  /* Cut off LF. */
file_size_marked = atol(temp_string);
fclose(mark_holding_file);
log_file_to_check = fopen(log_file_spec,"rb");
if (log_file_to_check == NULL)
  {
  printf("?Log file %s not found.\n");
  return(ERRORLEVEL_BAD_PARAMETER);
  }
fseek(log_file_to_check,0,SEEK_END);
file_size_current = ftell(log_file_to_check);
if (file_size_current <= file_size_marked)
  return(ERRORLEVEL_SCAN_FAILED);
fseek(log_file_to_check,file_size_marked,SEEK_SET);
while (ftell(log_file_to_check) < file_size_current)
  {
  fread(&the_char,1,1,log_file_to_check);  /* Read 1 character. */
  printf("%c",the_char);
  }
fclose(log_file_to_check);
wait_time = atoi(wait_argument);  /* How long to wait after showing log. */
if (wait_time)
  wait(wait_time);
return(ERRORLEVEL_SUCCESS);
}


/*---------------------------- The program! ----------------------------*/
int main(int argc, char *argv[])
{
char arg_function[64],arg_seconds[64];
time_t ending_time;
int the_char,seconds_to_wait;

printf("TIMECHEK -- Alton Moore -- Fidonet 1:397/5264, WWIVnet 1@1042\n");
if (argc < 3)
  {
  printf("?Illegal number of arguments.%c\n",BELL_string[0]);
  printf("\nSpecify in the command line:\n");
  printf("  Function and number of seconds (if needed)\n\n");
  printf("  TIMECHEK [function] [seconds]\n");
  printf("  If function is -m then the time is marked and seconds are ignored.\n");
  printf("  If function is -c then the program checks to see if [seconds] seconds have\n");
  printf("   passed since the time was last marked.\n\n");
  printf("Error levels returned:  0 -- SUCCESS)\n");
  printf("                        1 -- [seconds] seconds have passed\n");
  printf("                        9 -- Parameter or program's error\n");
  printf("\nExiting with errorlevel 9....\n");
  return(9);
  }
if (!strcmp(argv[1],"-m"))   /* Mark time? */
  {
  mark_time();
  return(mark_file_size(argv[2]));
  }
if (!strcmp(argv[1],"-s"))
  {
  print_header();
  return(scan_file_for_string(argv[2]));
  }
if (!strcmp(argv[1],"-p"))
  return(print_new_part_of_file(argv[2]));
return(ERRORLEVEL_BAD_PARAMETER);
strcpy(arg_function,argv[1]);
strcpy(arg_baud,argv[2]);
modem_port  = atoi(arg_port);
modem_speed = atoi(arg_baud);
if (argc >= 5)   /* Were the seconds arguments included (both!)? */
  {
  strcpy(arg_minimum_seconds,argv[3]);
  strcpy(arg_maximum_random_seconds,argv[4]);
  minimum_seconds = atoi(arg_minimum_seconds);
  maximum_random_seconds = atoi(arg_maximum_random_seconds);
  }
if (initSerial(modem_port,modem_speed,modem_parity,modem_bits,modem_stopbits))
  {
  printf("?Couldn't initialize serial port; exiting with errorlevel 9!\n");
  return(9);
  }
fossil_set_dtr(modem_port,1);
port_set_handshaking(1);  /* Hardware handshaking. */
send_to_modem(modem_port,init_string);
randomize();
seconds_to_wait = minimum_seconds + (rand() % (maximum_random_seconds+1));
printf("Waiting %d seconds for RING from modem.  Hit space bar to\n",seconds_to_wait);
printf("hurry up or press any other key to abort next operation.\n");

trace_start("RING");
ending_time = time(NULL) + seconds_to_wait;
while (time(NULL) < ending_time)
  {
  if (kbhit())
    {
    deinitSerial(0);
    trace_stop_all();
    if (getch() == ' ')
      {
      printf("Quitting and exiting with errorlevel 0!\n");
      return(0);
      }
    else
      {
      printf("Key pressed; aborting and exiting with errorlevel 2!\n");
      return(2);
      }
    }
  the_char = port_in();             /* Anything from the modem? */
  if (the_char != -1)
    trace_process_received_character(the_char);
  if (trace_fired("RING"))
    {
    printf("RING detected from modem; exiting with errorlevel 1!\n");
    trace_stop_all();
    trash_modem_output(modem_port,0);
    deinitSerial(0);
    return(1);
    }
  }
printf("Nothing detected; exiting with errorlevel 0.\n");
trace_stop_all();
deinitSerial(0);
return(0);
}




