home *** CD-ROM | disk | FTP | other *** search
/ WordPerfect for Linux Bible / WP4LinuxBible.iso / sdk / wpx / code / uoapi.c next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  6.3 KB  |  273 lines

  1. /* name    uoapi.c
  2.    title    Unix WP UOAPInterface Sample Program
  3.  
  4.    Copyright (C) 1992 WordPerfect Corp., All Rights Reserved
  5.  ------------------------------------------------------------*/
  6.  
  7. #include "uoapi.h"
  8.  
  9. #define READ  0
  10. #define WRITE 1
  11.  
  12. static struct {         /* structure to store pipe file descriptors */
  13.     int out[2];
  14.     int in[2];
  15. } pipes;
  16.  
  17. static char fds[9];        /* temp for converting fds to strings */
  18.  
  19. char *params[5];        /* parameter data for exec call */
  20.  
  21. char ppath[] = "wpbin/xwp";    /* path and name of WordPerfect executable */
  22.  
  23. char thirds[] = "-third";    /* command line parameter to send to WordPerfect */
  24.  
  25. char tmp1[] = "-L";
  26. char tmp2[] = "Q:NHVEAG";
  27.  
  28. int cpid;                /* pid of WordPerfect process */
  29.  
  30. /* Utility routine */
  31. /* convert 4 bytes in Intel order into an unsigned int (32 bits) */
  32. static unsigned long BtoI(b)
  33. unsigned char *b;
  34. {
  35.     unsigned long val;
  36.  
  37.     val = *b++;
  38.     val += (unsigned int)*b++ << 8;
  39.     val += (unsigned int)*b++ << 16;
  40.     val += (unsigned int)*b << 24;
  41.     return val;
  42. }
  43.  
  44. /* Utility routine */
  45. /* convert 2 bytes in Intel order into an unsigned int (32 bits) */
  46. static unsigned long BtoS(b)
  47. unsigned char *b;
  48. {
  49.     unsigned long val;
  50.  
  51.     val = *b++;
  52.     val += (unsigned int)*b++ << 8;
  53.     return val;
  54. }
  55.  
  56. /* Utility routine */
  57. /* convert an integer into "cnt" bytes in Intel order */
  58. IVALtoB(ival,b,cnt)
  59. unsigned int ival;
  60. unsigned char *b;
  61. int cnt;
  62. {
  63.     int i;
  64.  
  65.     for (i=0;i<cnt;i++) {
  66.         b[i] = ival & 0xff;
  67.         ival >>= 8;
  68.     }
  69. }
  70.  
  71. /* Utility routine */
  72. /* convert an integer into 4 bytes in Intel order */
  73. void ItoB(ival,b)
  74. unsigned int ival;
  75. unsigned char *b;
  76. {
  77.     IVALtoB(ival,b,4);
  78. }
  79.  
  80. /* Utility routine */
  81. /* convert an integer into 2 bytes in Intel order */
  82. void StoB(ival,b)
  83. unsigned int ival;
  84. unsigned char *b;
  85. {
  86.     IVALtoB(ival,b,2);
  87. }
  88.  
  89. /* Utility routine */
  90. /* convert a byte string to a word string */
  91. int btowstr(char *s, char *b)
  92. {
  93.     int i = 0;
  94.  
  95.     while (*s) {
  96.         b[i++] = *s++;
  97.         b[i++] = 0;
  98.     }
  99.     b[i++] = 0;
  100.     b[i++] = 0;
  101.     return i;        /* return the length of the word string */
  102. }
  103.  
  104. /* Utility routine */
  105. /* read cnt bytes from file decriptor fd */
  106. static bread(fd,buf,cnt)
  107. int fd,cnt;
  108. char *buf;
  109. {
  110.     int br = 0;
  111.  
  112.     for (;;) {
  113.         br += read(fd,buf+br,cnt-br);    /* read bytes */
  114.         if (br == cnt) {                /* did we get them all ? */
  115.             break;                        /* yes - return */
  116.         }                                /* no - loop for another read */
  117.     }
  118. }
  119.  
  120.  
  121. /* A rudimentary Third Party program to inteface with WP60 UNIX */
  122. main(argc,argv)
  123. char *argv[];
  124. {
  125.     char inbuf[256];
  126.     char tmpbuf[256];
  127.     char *bufptr;
  128.  
  129.     /* variables to save data from the initialization packet */
  130.     int in_len;
  131.     int total_len;
  132.     int token;
  133.     int name_len;
  134.     char name[128];
  135.     int product;
  136.     int major;
  137.     int minor;
  138.     char language[3];
  139.  
  140.     int i;
  141.  
  142.     /* open pipe to use in writting to WP */
  143.     if (pipe(pipes.out) == -1) {
  144.         exit (1);
  145.     }
  146.  
  147.     /* open pipe to use in reading from WP */
  148.     if (pipe(pipes.in) == -1 ) {
  149.         exit (1);
  150.     }
  151.  
  152.     /* convert pipe fds to strings to pass on command line to WP */
  153.     sprintf(fds,"%d",pipes.out[READ]);
  154.     strcat(fds,",");
  155.     sprintf(fds+strlen(fds),"%d",pipes.in[WRITE]);
  156.  
  157.     /* set up command line parameter list to pass to WP */
  158.     params[0] = (char *) ppath;
  159.     params[1] = (char *) thirds;
  160.     params[2] = (char *) fds;
  161.     params[3] = tmp1;
  162.     params[4] = tmp2;
  163.     params[5] = 0;
  164.  
  165.     /* start WP */
  166.     if (cpid = fork()) {
  167.         if (cpid == -1) {
  168.             exit (1);
  169.         }
  170.     }
  171.     else {
  172.         if (execv(ppath,params)) {
  173.             exit (1);
  174.         }
  175.     }
  176.  
  177.     /* Read the initiaialization packet from WP */
  178.     /* and save the initial data */
  179.     bread(pipes.in[READ],inbuf,2);            /* read the length of the packet */
  180.     total_len = BtoS(inbuf);                /* save packet length */
  181.     bread(pipes.in[READ],inbuf+2,total_len);/* read remainder of packet */
  182.     bufptr = inbuf+2;
  183.     token = BtoS(bufptr);                    /* extract the init token */
  184.     bufptr += 2;
  185.     name_len = BtoS(bufptr);                /* extract the name lenth */
  186.     bufptr += 2;
  187.     strcpy(name,bufptr);                    /* extract the logical name */
  188.     bufptr += name_len;
  189.     product = *bufptr++;                    /* extract the product code */
  190.     major = *bufptr++;                        /* extract the major version # */
  191.     minor = *bufptr++;                        /* extract the minor version # */
  192.     language[0] = *bufptr++;                /* extract the language string */
  193.     language[1] = *bufptr;
  194.     language[2] = 0;
  195.     
  196.  
  197.     /* return a packet to let WP know we are here */
  198.     StoB(4,inbuf);                        /* length of return packet (4) */
  199.     StoB(0,inbuf+2);                    /* token lower bound */
  200.     StoB(0xffff,inbuf+4);                /* token upper bound */
  201.     write(pipes.out[WRITE],inbuf,6);    /* send the packet */
  202.  
  203.     for (;;) {        /* handle tokens */
  204.         bread(pipes.in[READ],inbuf,2);                /* get the packet size */
  205.         in_len = BtoS(inbuf);
  206.         bread(pipes.in[READ],inbuf+2,in_len);    /* read the packet */
  207.  
  208.         token = BtoS(inbuf+2);                        /* get the token value */
  209.  
  210.         if (token == THIRD_EXIT_TOK) {    /* Is WP exiting ? */
  211.             printf("EXITING\n");        /* yes - so we exit also */
  212.             exit(0);
  213.         }
  214.  
  215.         if (token == Type) {            /* a regular character ? */
  216.             int cnt;                    /* yes */
  217.             token = BtoS(inbuf+9);        /* get the character */
  218.             if (0 && token >= 'a' && token <= 'z') {
  219.  
  220.                 /*    THIS CODE SETS AND THEN GETS A MERGE VARIABLE */
  221.                 cnt = 2;
  222.                 StoB(THIRD_SET_MERGE_TOK,tmpbuf+2);
  223.                 cnt += 2;
  224.                 cnt += btowstr("abc",tmpbuf+cnt);/* set the variable "abc" */
  225.                 cnt += btowstr("xyz",tmpbuf+cnt);/* to have the value "xyz" */
  226.                 StoB(cnt-2,tmpbuf);
  227.                 write(pipes.out[WRITE],tmpbuf,cnt);    /* set merge variable */
  228.  
  229.                 bread(pipes.in[READ],tmpbuf,2);        /* get result */
  230.                 total_len = BtoS(tmpbuf);
  231.                 bread(pipes.in[READ],tmpbuf+2,total_len);
  232.  
  233.                 cnt = 2;
  234.                 StoB(THIRD_GET_MERGE_TOK,tmpbuf+2);
  235.                 cnt += 2;
  236.                 cnt += btowstr("abc",tmpbuf+cnt);/* get the variable "abc" */
  237.                 StoB(cnt-2,tmpbuf);
  238.                 write(pipes.out[WRITE],tmpbuf,cnt);    /* get merge variable */
  239.  
  240.                 bread(pipes.in[READ],tmpbuf,2);        /* get result */
  241.                 total_len = BtoS(tmpbuf);
  242.                 bread(pipes.in[READ],tmpbuf+2,total_len);
  243.  
  244.  
  245.     
  246.                 /*    THIS CODE GETS A SYSTEM VARIABLE */
  247.                 StoB(4,tmpbuf);
  248.                 StoB(THIRD_GET_SYSTEM_TOK,tmpbuf+2);
  249.                 StoB(QMarginRight,tmpbuf+4);        /* get the right margin value */
  250.                 write(pipes.out[WRITE],tmpbuf,6);
  251.                 bread(pipes.in[READ],tmpbuf,2);
  252.                 total_len = BtoS(tmpbuf);
  253.                 bread(pipes.in[READ],tmpbuf+2,total_len);
  254.  
  255.             }
  256.  
  257.                 /* This code increment the character that was typed */
  258.                 /* and sends it back to WP to be handled */
  259.                 token++;
  260.                 if (token > 'z') {
  261.                     token = 'a';
  262.                 }
  263.                 StoB(token,inbuf+9);
  264.                 write(pipes.out[WRITE],inbuf,in_len+2);
  265.         }
  266.         else {
  267.             /* send token back unchanged */
  268.             write(pipes.out[WRITE],inbuf,in_len+2);
  269.         }
  270.     }
  271. }
  272.  
  273.