[BACK]Return to test2.c CVS log [TXT][DIR] Up to [local] / OpenXM / doc / oxlib

File: [local] / OpenXM / doc / oxlib / test2.c (download)

Revision 1.2, Thu Mar 16 07:34:37 2000 UTC (24 years, 2 months ago) by noro
Branch: MAIN
CVS Tags: maekawa-ipv6, RELEASE_1_1_3, RELEASE_1_1_2
Changes since 1.1: +3 -5 lines

Makefile : cleanup around the directory names.
test1.c, test2.c : modified the name and the location of the header file.
test3.c : another sample.

% test3
Input>asir 123     <- push 123
Input>pop          <- pop and print
Output>
00 00 00 14 00 00 00 01 00 00 00 7b
Input>push 00 00 00 14 00 00 00 01 00 00 00 0c  <- push 12
Input>push 00 00 00 14 00 00 00 01 00 00 00 08  <- push 8
Input>push 00 00 00 02 00 00 00 02              <- push int32 2
Input>asir "igcd"   <- push a string (function name)
Input>cmd 269       <- executeFunction
Input>pop
Output>
00 00 00 14 00 00 00 01 00 00 00 04     <- 4 = gcd(12,8)
Input>asir quit                         <- execute asir 'quit' command
%

/* $OpenXM: OpenXM/doc/oxlib/test2.c,v 1.2 2000/03/16 07:34:37 noro Exp $ */
#include <asir/ox.h>

char *asir_ox_pop_string();

main() {
  char *s;
  asir_ox_init(1);  /* Use the network byte order */

  /* Loading the file xm */
  /* This part generates a core dump. I'm trying to find a reason. */
  asir_ox_push_string("xm");
  asir_ox_push_int32(1);
  asir_ox_push_string("load");
  asir_ox_push_cmd(SM_executeFunction);
  

  asir_ox_push_string("fctr(x^10-1);");
  asir_ox_push_cmd(SM_executeStringByLocalParser);

  /*
  asir_ox_push_string("x^10-1;");
  asir_ox_push_cmd(SM_executeStringByLocalParser);
  asir_ox_push_int32(1);
  asir_ox_push_string("fctr");
  asir_ox_push_cmd(SM_executeFunction);
  */

  s = asir_ox_pop_string();
  printf("%s\n",s);

  printf("------------------------------------------\n");
  asir_ox_push_object_given_by_a_string("12345;");
  s = asir_ox_pop_string();
  printf("%s\n",s);

}

asir_ox_push_int32(int k) {
  static unsigned char p[8];
  *((int *)p) = htonl((int) CMO_INT32);
  *((int *)(p+4)) = htonl(k);
  debug_packet(p,8);
  asir_ox_push_cmo(p);
}

asir_ox_push_string(char *s) {
  unsigned char *p;
  p = (unsigned char *)GC_malloc(strlen(s)+8);
  if (sizeof(int) != 4) {
	fprintf(stderr,"sizeof(int) is not equal to 4.\n"); exit(1);
  }
  *((int *)p) = htonl((int) CMO_STRING);
  *((int *)(p+4)) = htonl((int ) strlen(s));
  strcpy(p+8,s);
  debug_packet(p,strlen(s)+8);
  asir_ox_push_cmo(p);
}

asir_ox_push_object_given_by_a_string(char *s) {
  asir_ox_push_string(s);
  asir_ox_push_cmd(SM_executeStringByLocalParser);
}

char *asir_ox_pop_string() {
  int size;
  char *s;
  int tag;
  asir_ox_push_int32(1);
  asir_ox_push_string("rtostr");
  asir_ox_push_cmd(SM_executeFunction);

  size = asir_ox_peek_cmo_size();
  s = (char *)GC_malloc(size+1);
  asir_ox_pop_cmo(s,size+1);
  s[size] = 0;
  tag = ntohl(*((int *)s));
  if (tag != CMO_STRING) {
	fprintf(stderr,"asir_ox_pop_string: the cmo object is not a string. Returns the empty string.\n");
	return("");
  }else{
	return(s+8);
  }
}
  
debug_packet(char *p,int size) {
  int i;
  for (i=0; i<size; i++) {
    printf(" %2x ",p[i]);
	if ((p[i] >= ' ') && (p[i] < 0x7f)) {
	  printf("(%c) ",p[i]);
	}
  }
  printf("\n");
}