


# include <stdio.h>
# include <errno.h>
# include <string.h>
# include <stddef.h>
# include <sys/file.h>

# include "serv.h"
# include "../registrar/tables.h"
# include "../registrar/protocol.h"

#define   EOT   "\012.\015\012"
char       *mit_id_ptr;
char       *term_ptr;
char       *sort_columns_ptr;
int        rc;

void
log_aline(line)             /* write  to a  file - for debugging */
        char           *line;
{
        int dfd;
        if ((dfd = open("/mit/net_dev/larissa/serv.log", 
			O_APPEND | O_CREAT | O_WRONLY, 0644)) == 0)
                printf("error logging transaction\n");
        write(dfd, line, strlen(line));
        close(dfd);
        return;
}



/*
 *   init_buf initializes the structure buffer_t *b, and if b_ptr == NULL
 *   allocates b_len bytes. If b_ptr != NULL that means that buffer is
 *   already allocated.
 */

void
init_buf(buffer_t *b, long b_len, char *b_ptr)
{
     b->buflen = b_len;
     if (b_ptr != NULL)
       b->bufptr = b_ptr; /* buffer is already allocated */
       else
	 {
	   /* we have to allocate the buffer */
	   b->bufptr = (char *) malloc ( b->buflen + 1);
           if ( b->bufptr == NULL)
             fprintf (stdout,"Unable to allocate memory for the buffer\n");
	   fflush(stdout);
	 }
     b->buf_offset = b->bufptr;
}

/*
 *  next_field: it returns buf_offset ptr, looks for any of the delimeters,
 *              replaces the found delimeter with '\0', and moves
 *              buf_offset after that '\0'.
 */

char *
next_field (buffer_t *b )
{
     char *field_ptr;

     field_ptr = b->buf_offset;

     while ( *( b->buf_offset) != DLMF && 
             *( b->buf_offset) != LF   &&
             b->buf_offset < (b->bufptr + b->buflen) )

              b->buf_offset = b->buf_offset + 1;

     *( b->buf_offset) = '\0';
     log_aline ("field=");
     log_aline (field_ptr);

     b->buf_offset = b->buf_offset + 1;

     return (field_ptr);
}

/*
 *  int_buf reads symbols from the standard input until it finds the 
 *  delimeter DLMF. Then it converts the number to integer and stores it
 *  in the (*b).buflen, allocates buffer and reads it from stdin.
 */

static void
get_buf(buffer_t *b)
{
  int rc;

  printf("Enter request_type + parameters ");
  printf(" followed by CR,\nor . to QUIT>%s",EOT); 

  fflush(stdout);

  rc = read ( FILE_IN, b->bufptr, b->buflen);

  if (rc < 0)
    perror("trying to read the whole buffer");

  if (*( b->bufptr + rc - 1) == LF)
    rc = rc - 1;

  if (*( b->bufptr + rc - 1) == CR)
    rc = rc - 1;
  b->buflen = rc; /* saved the length of the buffer */
  *( b->bufptr + rc) = '\0';

    fprintf (stdout,"The length of the read buffer is %d\n",b->buflen);
  fflush(stdout);

  log_aline("/nThe buffer=");
  log_aline( b->bufptr);


      
}


/*
 * Append_field appends field + DLMF to the buffer and recalculates 
 * buf_offset. It checks if the buflen allows to do it.
 */

static void
append_field (buffer_t *b, char *field_ptr)
{
  size_t   field_len;
  field_len = strlen(field_ptr);
  if ( b->buf_offset + field_len > b->bufptr + b->buflen)
    printf("There is no space in buffer to append the field\n");
  else
    {
      strncpy( b->buf_offset, field_ptr, field_len);
      b->buf_offset = b->buf_offset + field_len;
      *( b->buf_offset) = DLMF;
      b->buf_offset = b->buf_offset + 1;
    }
  
}

main()
{
  buffer_t   in_head_buffer;
  buffer_t   in_buffer;
  buffer_t   out_buffer;
  int        loc_buflen = 12;
  char       request_type;
  static char   in_buf[300];
  #define       in_buf_len   300

  #define DOT '.'

  char   first_field[6] = "first";
  char   sec_field[4]   = "sec";
  int     rc;

  static char    first_line[80];
  int     line_len = sizeof(first_line);

  
while(TRUE)
    {


      /* We'll initialize our in_buffer descriptor using already described */
      /* in_buf array                                                      */
      init_buf (&in_buffer, in_buf_len, in_buf);

      /* We'll read the whole buffer: */
      get_buf(&in_buffer);
      
      request_type = *( next_field (&in_buffer) );
      
      switch (request_type) {
	
      case BIO_RECORD:
	log_aline( "\nget unique BIO record\n\n");
	break;
	
      case GRADE_HISTORY_TERM_DEF:
	log_aline( "\nget grade history for term(def)\n\n");
	break;
	
      case GRADE_HISTORY_ALL_DEF:
	log_aline( "\nget grade history for all terms(def)\n\n");
	break;
	
      case GRADE_HISTORY_TERM_SORT:
	log_aline( "\nget grade history for term(sort)\n\n");
	break;
	
      case GRADE_HISTORY_ALL_SORT:
	log_aline( "\nget grade history for all terms(sort)\n\n");
	break;
	
      case TERM_SUMMARY_ALL:
	log_aline( "\nget term summary for all terms\n\n");
	break;
	
      case GRADES_TERM:
	log_aline( "\nget grades for term\n\n");
       	break;
	
      case GRADES_ALL:
	log_aline( "\nget grades for all terms\n\n");
	break;
	
      case AUDIT_STRIP:
	log_aline( "\nget audit strip\n\n");
	break;
	
      case SUBJECT_INFO:
	log_aline( "\nget subject info for a subject\n\n");
	break;
	
      case UPDATED_BIO_RECORD:
	log_aline( "\nreceive updated BIO record\n\n");
	break;

      case DOT:
	printf( "\nend of connection\n\n");
	goto dot;
	break;
      
      default:
	printf( "\nunrecognized record type %c\n\n",
		request_type);
	fflush(stdout);
      }

    }  
/* end of the loop till the end of connection - EOC */

 dot: ;;
 /* end of connection */

}









