Ejemplo n.º 1
0
int lisp_encode(unsigned char *vector, char *sexp){
  int vptr=0, sptr, length=0;
  // maximum length for sexp is 1 + 2 + (16+2)*7 + 7
  // #, parens, seven 64bit hex numbers, prefixed by #x, spaces
  // grand total of 135, plus a null character, so 137
  // vector is SYSREG_BYTES long.
  memset(sexp, 0, SEXP_LENGTH);
    
  length += sprintf(sexp+length, "#(");
  
  for (vptr = 0; vptr < SYSREG_BYTES; vptr += sizeof(long int)) {
    length += sprintf(sexp+length, "#x%llx ",
                      bytes_to_integer(vector + vptr));
  }
  length --;
  length += sprintf(sexp+length, ")\n\0");

  printf("SEXP: %s\n", sexp);
  
  return length;
}
Ejemplo n.º 2
0
Ipc_Status_t
ipc_transport_get_line (
     char               *str,    /* returns the result, null terminated    */
     int                *len,    /* length of str passed IN and passed OUT */
     Ipc_Wait_t         wait )   /* IN - wait or dont wait on incoming msg */
{
  int count = 0;                     /* number of bytes read                   */
  int message_length;            /* extracted from message header          */

  if (sock_state == IPC_SOCK_UNINITIALIZED) {
    fprintf (stderr,
	    "ERROR: IPC: Attempted to read from uninitialized socket\n");
    return IPC_STATUS_ERROR;
  }
  
  assert ((sock_state == IPC_SOCK_CONNECTED_TO_CLIENT) ||
	  (sock_state == IPC_SOCK_INITIALIZED));

  if (sock_state == IPC_SOCK_INITIALIZED) {
    /* We have an open socket but have not connected to a client.          */
    /* Accept a connection from a client.                                  */
    msg_stream = accept (sock_desc, (struct sockaddr *)0, (unsigned int *)0);

    if (msg_stream == -1) {
      fprintf (stderr, "ERROR: IPC: Server accepting request\n");
      perror ("ERROR: IPC");
      return IPC_STATUS_ERROR;
    }
    sock_state = IPC_SOCK_CONNECTED_TO_CLIENT;
  }
  /*-----------------------------------------------------------------------*/
  /* First read in the message header.                                     */
  {
    int flags;
    flags = fcntl(msg_stream, F_GETFL, NULL);    /* Blocking read mode     */

    if (wait == IPC_WAIT) {
      /* Block here and wait for the next message                          */
      count = read_sock (msg_stream, str, SOCK_MSG_HDR_LEN, wait, flags);
      if (count == 0) {
	/* EOF, will this ever happen?                                     */
	/* fprintf (stderr, "WARNING: IPC: Reached eof on socket\n");  */
	return handle_socket_eof ();
      }
    } else if (wait == IPC_NO_WAIT) {
      /* Read message, but do not wait if none available.                  */

      fcntl (msg_stream, F_SETFL, flags | O_NDELAY);
      count = read_sock (msg_stream, str, SOCK_MSG_HDR_LEN, wait, flags);

      if (count == 0) {
	/* EOF, will this ever happen?                                     */
	/* fprintf (stderr, "WARNING: IPC: Reached eof on socket\n");  */
	return handle_socket_eof ();
      } else if (count == -1) {
	if (errno == EWOULDBLOCK) {
	  return IPC_STATUS_NO_DATA;
	}
      }

    } else {
      /* Serious problem, since it is not reading anything.                */
      fprintf (stderr,
	       "ERROR: IPC: invalid wait arg to ipc_transport_get_line\n");
    }
  }
  
  /* Do more error checking on the read in values of the message header:   */
  if (count == -1) {
    fprintf (stderr, "ERROR: IPC: Reading from socket\n");
    perror ("ERROR: IPC");
    return IPC_STATUS_ERROR;
  } else if (str[0] != BOL_CHAR) {
    fprintf (stderr,
	     "ERROR: IPC: Did not find beginning of message header (%c)\n",
	     str[0]);
    return IPC_STATUS_ERROR;
  } else if ((message_length = (int) bytes_to_integer (str, 1)) == -1) {
    /* fprintf (stderr, "WARNING: IPC: Reached eof on socket\n");      */
    return handle_socket_eof ();
  } else if (message_length == 0) {
    *len = 0;
    return IPC_STATUS_NO_DATA;

/*  Invalid test... delete - wbk
  } else if (message_length > *len) {
    fprintf (stderr,
	     "ERROR: IPC: Buffer (%d) is too short for message (%d)\n",
	     *len, message_length);
    return IPC_STATUS_ERROR;
*/

  }

  /*-----------------------------------------------------------------------*/
  /* Now read in the message body.                                         */
  /* Always block here since the message header was already read and       */
  /* we must get the body.                                                 */

  *len = message_length;
  count = read_sock (msg_stream, str, message_length, IPC_WAIT, 0);
  if (count == 0) {
    /* EOF, will this ever happen? */
    /* fprintf (stderr,                                                    */
    /*          "WARNING: IPC: Reached eof in message body on socket\n");*/
    return handle_socket_eof ();
  } else if (count == -1) {
    fprintf (stderr, "ERROR: IPC: reading message body from socket\n");
    perror ("ERROR: IPC");
    return IPC_STATUS_ERROR;
  }
  
  /* Looks like we have a valid message here. Put in the string terminator. */
  *len = count;
  str[count] = '\0';

  return IPC_STATUS_OK;

}   /* end ipc_transport_get_line */