
#define TRACE_STRING_LENGTH 64

char trace_string_1[TRACE_STRING_LENGTH+1],
     trace_string_2[TRACE_STRING_LENGTH+1];
char trace_string_1_position=0,trace_string_2_position=TRACE_STRING_LENGTH / 2;

struct trace_list_type  {
  char trace_string[TRACE_STRING_LENGTH+1]; /* The string we're looking for.*/
  char fired_flag;   /* Set to non-zero if the string has been received. */
  struct trace_list_type *next;    /* Pointer to next entry in this list.*/
  }  *trace_list_head_ptr = NULL,
     *trace_list_current_ptr, *trace_list_previous_ptr,
     *trace_list_saved_ptr,   *trace_list_traversing_ptr;

void trace_process_received_character(char the_character)
{
if (!the_character)  /* Did we receive a null? */
  return;         /*  If so, just skip it.  */
trace_string_1[trace_string_1_position++] = the_character;
if (trace_string_1_position == TRACE_STRING_LENGTH)
  trace_string_1_position -= TRACE_STRING_LENGTH;
trace_string_2[trace_string_2_position++] = the_character;
if (trace_string_2_position == TRACE_STRING_LENGTH)
  trace_string_2_position -= TRACE_STRING_LENGTH;
trace_list_current_ptr = trace_list_head_ptr;
while (trace_list_current_ptr != NULL)
  {
  if (strstr(trace_string_1,trace_list_current_ptr->trace_string) != NULL)  /* String there? */
    trace_list_current_ptr->fired_flag = 1;  /* String was found! */
  if (strstr(trace_string_2,trace_list_current_ptr->trace_string) != NULL)
    trace_list_current_ptr->fired_flag = 1;
  trace_list_current_ptr = trace_list_current_ptr->next;  /* Go to next. */
  }
}

int trace_start(char *the_string)  /* Returns 0 if successful. */
{
if (trace_list_head_ptr == NULL)
  {
  memset(trace_string_1,' ',TRACE_STRING_LENGTH);
  trace_string_1[TRACE_STRING_LENGTH] = 0;
  memset(trace_string_2,' ',TRACE_STRING_LENGTH);
  trace_string_2[TRACE_STRING_LENGTH] = 0;
  }
trace_list_current_ptr = (struct trace_list_type *)malloc(sizeof(struct trace_list_type));
if (trace_list_current_ptr == NULL)   /* Out of memory? */
    return(1);
strcpy(trace_list_current_ptr->trace_string,the_string); /* String we're tracing. */
trace_list_current_ptr->fired_flag = 0;  /* Init to "unfired". */
trace_list_current_ptr->next = trace_list_head_ptr; /* Insert at start of list.*/
trace_list_head_ptr = trace_list_current_ptr;
return(0);
}

void trace_stop(char *the_string,char debug_flag)
{
trace_list_previous_ptr = NULL;
trace_list_current_ptr = trace_list_head_ptr;
while (trace_list_current_ptr != NULL)
  {
  if (!strcmp(trace_list_current_ptr->trace_string,the_string))
    {
    if (trace_list_current_ptr == trace_list_head_ptr)
      trace_list_head_ptr = trace_list_current_ptr->next;
    else
      trace_list_previous_ptr->next = trace_list_current_ptr->next;
    if (debug_flag)
      printf("Stopping trace for %s\n",trace_list_current_ptr->trace_string);
    free(trace_list_current_ptr);
    return;
    }
  trace_list_previous_ptr = trace_list_current_ptr;
  trace_list_current_ptr = trace_list_current_ptr->next;
  }
}

void trace_count()
{
printf("Traces enabled at this time:\n");
trace_list_current_ptr = trace_list_head_ptr;
while (trace_list_current_ptr != NULL)
  {
  printf("  %s\n",trace_list_current_ptr->trace_string);
  trace_list_current_ptr = trace_list_current_ptr->next;
  }
printf("  -----\n");
}

void trace_stop_all()
{
while (trace_list_head_ptr != NULL)
  {
  trace_list_saved_ptr = trace_list_head_ptr; /* Save ptr to 1st item in list.*/
  trace_list_head_ptr = trace_list_head_ptr->next;
  free(trace_list_saved_ptr);
  }
}

int trace_fired(char *the_string)  /* Return non-zero if trace fired. */
{
trace_list_current_ptr = trace_list_head_ptr;
while (trace_list_current_ptr != NULL)
  {
  if (!strcmp(trace_list_current_ptr->trace_string,the_string))
    return(trace_list_current_ptr->fired_flag);
  trace_list_current_ptr = trace_list_current_ptr->next;  /* Go to next. */
  }
return(0);
}


