home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / texview / ipc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  5.0 KB  |  206 lines

  1. /* ipc for TeX->TeXview communications. */
  2. #define TEXVIEW
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <sys/time.h>
  6. #include <fcntl.h>
  7. #include <strings.h>
  8.  
  9. extern int unlink(), fcntl(), close(), read() ;
  10. extern void checkmatch() ;
  11.  
  12. #define IPC_PIPE_NAME "/.TeXview_Pipe"
  13.  
  14. struct msg {
  15.    short namelength ; /* length of auxilliary data */
  16.    int eof ;   /* new eof for dvi file */
  17.    char more_data[0] ; /* where the rest of the stuff goes */ 
  18. } ;
  19.  
  20. /* common routines */
  21. extern char *getenv(), *malloc() ;
  22. static char *ipc_name ;
  23. static struct sockaddr *ipc_addr ;
  24. static int ipc_addr_len ;
  25. static int ipc_make_name() {
  26.    char *s ;
  27.    if (ipc_addr_len == 0) {
  28.       if ((s = getenv("HOME")) &&
  29.           (ipc_addr = (struct sockaddr *)malloc(strlen(s) + 40))) {
  30.          ipc_addr->sa_family = 0 ;
  31.          ipc_name = ipc_addr->sa_data ;
  32.          strcpy(ipc_name, s) ;
  33.          strcat(ipc_name, IPC_PIPE_NAME) ;
  34.          ipc_addr_len = strlen(ipc_name) + 3 ;
  35.       }
  36.    }
  37.    return ipc_addr_len ;
  38. }
  39.  
  40. static int sock = -1 ;
  41.  
  42. int ipc_is_open() {
  43.    return sock >= 0 ;
  44. }
  45.  
  46. #ifdef TEXVIEW
  47. extern void add_fd(), delete_fd() ;
  48. /* routines for input (for TeXview) */
  49. #include "errno.h"
  50. extern int dviOpenFile() ;
  51. extern int fakeeof ;
  52. static int ns = -1 ;
  53. void ipc_open_in() {
  54. #ifdef IPCDEBUG
  55.  printf("Starting up ipc.\n") ;
  56. #endif
  57.    if (ipc_make_name()) {
  58.       unlink(ipc_name) ;
  59.       if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) >= 0) {
  60.          if ((bind(sock, ipc_addr, ipc_addr_len) >= 0) &&
  61.              (listen(sock, 1) >= 0) &&
  62.              (fcntl(sock, F_SETFL, FNDELAY) != -1)) {
  63.             add_fd(sock) ;
  64. #ifdef IPCDEBUG
  65.  printf("IPC successfully started.\n") ;
  66. #endif
  67.          } else {
  68.             close(sock) ;
  69.             sock = -1 ;
  70.          }
  71.       }
  72.    }
  73. }
  74.  
  75. void ipc_shutdown() {
  76. #ifdef IPCDEBUG
  77.    printf("Shutting down ipc\n") ;
  78. #endif
  79.    if (ipc_is_open()) {
  80.       if (ns >= 0) {
  81.          delete_fd(ns) ;
  82.          close(ns) ;
  83.          ns = -1 ;
  84.       } else
  85.          delete_fd(sock) ;
  86.       close(sock) ;
  87.       sock = -1 ;
  88.    }
  89. }
  90.  
  91. static void ipc_tryagain() {
  92.    ipc_shutdown() ;
  93.    ipc_open_in() ;
  94. }
  95.  
  96. int fromipc ;
  97.  
  98. void ipc_msg_handler() {
  99.    struct msg msg ;
  100.    int stuff_to_do = 0 ;
  101.    int dmyint ;
  102.    char dummy[1024] ;
  103.    char other_dummy[1024] ;
  104.  
  105. #ifdef IPCDEBUG
  106.  printf("Entered message handler.\n") ;
  107. #endif
  108.    dummy[0] = 0 ;
  109.    if (! ipc_is_open())
  110.       return ;
  111.    while (1) {
  112. #ifdef IPCDEBUG
  113.  printf("Top of the loop; sock=%d ns=%d\n", sock, ns) ;
  114. #endif
  115.       if (ns >= 0) {
  116.          while ((dmyint = read(ns, &msg, sizeof(struct msg)))
  117.                                              == sizeof(struct msg)) {
  118. #ifdef IPCDEBUG
  119.  printf("Read a message; length=%d\n", msg.namelength) ;
  120. #endif
  121.             if (msg.namelength > 0) {
  122.                if ((dmyint = read(ns, &dummy, msg.namelength))
  123.                                                       != msg.namelength)
  124.                   break ;
  125.                dummy[msg.namelength] = 0 ; /* end the string */
  126. #ifdef IPCDEBUG
  127.  printf("Successfully read a name: <%s>\n", dummy) ;
  128. #endif
  129.             }
  130.             stuff_to_do = 1 ;
  131.          }
  132.          if (dmyint == 0) { /* read eof */
  133. #ifdef IPCDEBUG
  134.  printf("Read EOF on input stream.\n") ;
  135. #endif
  136.             close(ns) ;
  137.             delete_fd(ns) ;
  138.             ns = -1 ;
  139.             add_fd(sock) ;
  140. /* we need to return here, to get around a bug in DPS. */
  141.             break ;
  142.          } else if (dmyint > 0 || errno != EWOULDBLOCK) {
  143. #ifdef IPCDEBUG
  144.  printf("Error? dmyint=%d errno=%d\n", dmyint, errno) ;
  145. #endif
  146.             ipc_tryagain() ;
  147.             return ;
  148.          } else {
  149. #ifdef IPCDEBUG
  150.  printf("Assuming wouldblock: dmyint=%d errno=%d\n", dmyint, errno) ;
  151. #endif
  152.             break ;
  153.          }
  154.       } else {
  155.          dmyint = sizeof(other_dummy) ;
  156. #ifdef IPCDEBUG
  157.  printf("Trying to accept a connection.\n") ;
  158. #endif
  159.          if ((ns = accept(sock, (struct sockaddr *)other_dummy, &dmyint))
  160.                                                              > 0) {
  161. #ifdef IPCDEBUG
  162.  printf("Got a connection!\n") ;
  163. #endif
  164.             delete_fd(sock) ;
  165.             if (fcntl(ns, F_SETFL, FNDELAY) == -1) {
  166.                ipc_tryagain() ;
  167.                return ;
  168.             } else {
  169. #ifdef IPCDEBUG
  170.  printf("Got a new socket under fcntl\n") ;
  171. #endif
  172.                add_fd(ns) ;
  173.             }
  174. /* we need to return here, to get around a bug in DPS. */
  175.             break ;
  176.          } else if (errno == EWOULDBLOCK) {
  177. #ifdef IPCDEBUG
  178.  printf("Got a would block on accept; continuing\n") ;
  179. #endif
  180.             ns = -1 ;
  181.             break ;
  182.          } else {
  183.             ipc_tryagain() ;
  184.             return ;
  185.          }
  186.       }
  187.    }
  188.    if (stuff_to_do) {
  189.       fakeeof = msg.eof ;
  190.       if (dummy[0] != 0) { /* got a file name!  close the current. */
  191.          fromipc = 1 ;
  192.          dviOpenFile(dummy) ;
  193.          fromipc = 0 ;
  194.       }
  195.       fakeeof = msg.eof ;
  196.       checkmatch() ;
  197. #ifdef IPCDEBUG
  198.  printf("name=<%s> fakeeof=%d\n", dummy, fakeeof) ;
  199. #endif
  200.    }
  201. #ifdef IPCDEBUG
  202.  printf("Returning from message handler.\n") ;
  203. #endif
  204. }
  205. #endif
  206.