Go to the first, previous, next, last section, table of contents.

## Library interfaces

It is possible to link an Asir library to use the functionalities of Asir from other programs. The necessary libraries are included in the OpenXM distribution (OpenXM ) (`http://www.math.kobe-u.ac.jp/OpenXM`). At present only the OpenXM interfaces are available. Here we assume that OpenXM is already installed. In the following `$OpenXM_HOME` denotes the OpenXM root directory. All the library files are placed in ``$OpenXM_HOME/lib'`. There are three kinds of libraries as follows.

- ``libasir.a'`
  It does not contain the functionalities related to PARI and X11. Only ``libasir-gc.a'` is necessary for linking.
- ``libasir_pari.a'`
  It does not contain the functionalities related to X11. ``libasir-gc.a'`, ``libpari.a'` are necessary for linking.
- ``libasir_pari_X.a'`
  All the functionalities are included. ``libasir-gc.a'`, ``libpari.a'` and libraries related to X11 are necessary for linking.

- `int asir_ox_init(int byteorder)`
  It initializes the library. byteorder specifies the format of binary CMO data on the memory. If byteorder is 0, the byteorder native to the machine is used. If byteorder is 1, the network byteorder is used. It returns 0 if the initialization is successful, -1 otherwise.
- `void asir_ox_push_cmo(void *cmo)`
- `int asir_ox_peek_cmo_size()`
  It returns the size of the object at the top of the stack as CMO object. It returns -1 if the object cannot be converted into CMO object.
- `int asir_ox_pop_cmo(void *cmo, int limit)`
  It pops an Asir object at the top of the stack and it converts the object into CMO data. If the size of the CMO data is not greater than limit, then the data is written in cmo and the size is returned. Otherwise -1 is returned. The size of the array pointed by cmo must be at least limit. In order to know the size of converted CMO data in advance `asir_ox_peek_cmo_size` is called.
- `void asir_ox_push_cmd(int cmd)`
  It executes a stack machine command cmd.
- `void asir_ox_execute_string(char *str)`
  It evaluates str as a string written in the Asir user language. The result is pushed onto the stack.

A program calling the above functions should include ``$OpenXM_HOME/include/asir/ox.h'`. In this file all the definitions of OpenXM tags and commands. The following example (``$OpenXM_HOME/doc/oxlib/test3.c'`) illustrates the usage of the above functions.

    #include <asir/ox.h>
    #include <signal.h>

    main(int argc, char **argv)
    {
      char buf[BUFSIZ+1];
      int c;
      unsigned char sendbuf[BUFSIZ+10];
      unsigned char *result;
      unsigned char h[3];
      int len,i,j;
      static int result_len = 0;
      char *kwd,*bdy;
      unsigned int cmd;

      signal(SIGINT,SIG_IGN);
      asir_ox_init(1); /* 1: network byte order; 0: native byte order */
      result_len = BUFSIZ;
      result = (void *)malloc(BUFSIZ);
      while ( 1 ) {
        printf("Input>"); fflush(stdout);
        fgets(buf,BUFSIZ,stdin);
        for ( i = 0; buf[i] && isspace(buf[i]); i++ );
        if ( !buf[i] )
          continue;
        kwd = buf+i;
        for ( ; buf[i] && !isspace(buf[i]); i++ );
        buf[i] = 0;
        bdy = buf+i+1;
        if ( !strcmp(kwd,"asir") ) {
          sprintf(sendbuf,"%s;",bdy);
          asir_ox_execute_string(sendbuf);
        } else if ( !strcmp(kwd,"push") ) {
          h[0] = 0;
          h[2] = 0;
          j = 0;
          while ( 1 ) {
            for ( ; (c= *bdy) && isspace(c); bdy++ );
            if ( !c )
              break;
            else if ( h[0] ) {
              h[1] = c;
              sendbuf[j++] = strtoul(h,0,16);
              h[0] = 0;
            } else
              h[0] = c;
            bdy++;
          }
          if ( h[0] )
            fprintf(stderr,"Number of characters is odd.\n");
          else {
            sendbuf[j] = 0;
            asir_ox_push_cmo(sendbuf);
          }
        } else if ( !strcmp(kwd,"cmd") ) {
          cmd = atoi(bdy);
          asir_ox_push_cmd(cmd);
        } else if ( !strcmp(kwd,"pop") ) {
          len = asir_ox_peek_cmo_size();
          if ( !len )
            continue;
          if ( len > result_len ) {
            result = (char *)realloc(result,len);
            result_len = len;
          }
          asir_ox_pop_cmo(result,len);
          printf("Output>"); fflush(stdout);
          printf("\n");
          for ( i = 0; i < len; ) {
            printf("%02x ",result[i]);
            i++;
            if ( !(i%16) )
              printf("\n");
          }
          printf("\n");
        }
      }
    }

This program receives a line in the form of keyword body as an input and it executes the following operations according to keyword.

- `asir` body
  body is regarded as an expression written in the Asir user language. The expression is evaluated and the result is pushed onto the stack. `asir_ox_execute_string()` is called.
- `push` body
  body is regarded as a CMO object in the hexadecimal form. The CMO object is converted into an Asir object and is pushed onto the stack. `asir_ox_push_cmo()` is called.
- `pop`
  The object at the top of the stack is converted into a CMO object and it is displayed in the hexadecimal form. `asir_ox_peek_cmo_size()` and `asir_ox_pop_cmo()` are called.
- `cmd` body
  body is regarded as an SM command and the command is executed. `asir_ox_push_cmd()` is called.

Go to the first, previous, next, last section, table of contents.