


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

# include "serv.h"

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

#define   EOT   "\012.\015\012"

int        rc;

void
log_aline(line)             /* write  to a  file - for debugging */
        char           *line;
{
        int dfd;
        if ((dfd = open("/mit/sis_dev/serv.log", 
			O_APPEND | O_CREAT | O_WRONLY, 0644)) == -1)
	  {
	        perror("in log_aline open");
                printf("error logging transaction\n");
		fflush(stdout);
	      }
        rc = write(dfd, line, strlen(line));
	if (rc < 0)
	  perror("in log_aline write");

        close(dfd);
        return;
}

void
log_int(num)             /* write  to a  file - for debugging */
        int           num;
{
        char          line[50];
        int dfd;
        if ((dfd = open("/mit/sis_dev/serv.log", 
			O_APPEND | O_CREAT | O_WRONLY, 0644)) == -1)
                printf("error logging transaction\n");
	sprintf(line, " %d \n", num);
        rc = write(dfd, line, strlen(line));
	if (rc < 0)
	  perror("in log_int write");
        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) != EOL   &&
             b->buf_offset < (b->bufptr + b->buflen) )

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

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

     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.
 */

void
get_request_buf(buffer_t *b)
{
  int rc;

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

  /* fflush(stdout);                                     */          

  log_aline("Server, get_request_buf, starting to read request\n");

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

  if (rc < 0)
    {
      log_aline("Get_request_buf, error in read, rc= ");
      log_int(rc);
      log_aline("\n");
      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';

  log_aline("Get_request_buffer: the length of the read buffer is ");
  log_int ( b->buflen);

  log_aline("Get_request_buffer: the buffer=");
  log_aline( b->bufptr);


      
}
void
get_server_buf(buffer_t *b, int sock)
{
  int rc;

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

  /* fflush(stdout);                                     */          

  log_aline("Get_server_buf, get_request_buf, starting to read request\n");

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

  if (rc < 0)
    {
      log_aline("Get_server_buf, error in read, rc= ");
      log_int(rc);
      log_aline("\n");
      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';

  log_aline("Get_server_buf: the length of the read buffer is ");
  log_int ( b->buflen);

  log_aline("Get_server_buf: the 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.
 */

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;
      /* printf("Append_field: buffer after append=");   */
      /* write(FILE_OUT, b->bufptr, 100);                */
      /* fflush (stdout);                                */
    }
  
}
/*
 * Append_header appends message_header + EOL to the buffer and recalculates 
 * buf_offset. It checks if the buflen allows to do it.
 */

void
append_header (buffer_t *b, message_header_t *m)
{
  int header_len;

  /* It has to be taken out: */
  if ( b->buf_offset + MSG_HEADER_LEN > b->bufptr + b->buflen)
    printf("There is no space in buffer to append the field\n");
  else
    {
      header_len = sprintf( b->buf_offset, "%d%c%d%c%c%c%d%c", m->buflen,
	      DLMF, m->return_code, DLMF, m->record_type, DLMF,
	      m->num_records, DLMF );

      b->buf_offset = b->buf_offset + header_len;
      /* For now: *( b->buf_offset) = EOL;                 */
      /* For now: b->buf_offset = b->buf_offset + EOL_LEN; */
      /* printf("Append_header: buffer after append=");   */
      /* write(FILE_OUT, b->bufptr, 100);                 */
      /* fflush (stdout);                                 */
    }
  
}
/*
 * Append_eom appends EOM to the buffer and recalculates 
 * buf_offset. It checks if the buflen allows to do it.
 */

void
append_eom (buffer_t *b )
{

  if ( b->buf_offset + EOM_LEN > b->bufptr + b->buflen)
    printf("There is no space in buffer to append the EOM\n");
  else
    {
      sprintf( b->buf_offset, "%s", EOM);

      b->buf_offset = b->buf_offset + EOM_LEN;
      /*     printf("Append_eom: buffer after append=");     */
      /*    write(FILE_OUT, b->bufptr, 100);                 */
      /*    fflush (stdout);                                 */
    }
  
}











