home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / watch.c < prev    next >
C/C++ Source or Header  |  1998-04-28  |  2KB  |  96 lines

  1. /*
  2.  * watch.c    -- generic data structures and routines for watching
  3.  *           file descriptors and timers (eventually)
  4.  */
  5.  
  6. #include "estruct.h"
  7. #include "edef.h"
  8.  
  9. typedef struct {
  10.     char     *callback;    /* a vile command to run... */
  11.     long      otherid;    /* e.g, the XtInputId is stored here for x11. */
  12.     WATCHTYPE type;    /* one of WATCHINPUT, WATCHOUTPUT, or WATCHERROR */
  13. } watchrec;
  14.  
  15. #define NWATCHFDS 256
  16.  
  17. static watchrec *watchfds[NWATCHFDS];
  18.  
  19.  
  20. static void unwatch_dealloc(int fd);
  21. static void unwatch_free_callback(char *callback);
  22.  
  23. int
  24. watchfd(int fd, WATCHTYPE type, char *callback)
  25. {
  26.     long otherid;
  27.     int status;
  28.  
  29.     if (watchfds[fd]) {
  30.     /* Already allocated/watched, so deallocate/unwatch */
  31.     unwatchfd(fd);
  32.     }
  33.  
  34.     watchfds[fd] = typealloc(watchrec);
  35.  
  36.     if (watchfds[fd] == NULL) {
  37.     unwatch_free_callback(callback);
  38.     return FALSE;
  39.     }
  40.  
  41.     status = TTwatchfd(fd, type, &otherid);
  42.  
  43.     watchfds[fd]->callback = callback;
  44.     watchfds[fd]->type     = type;
  45.     watchfds[fd]->otherid  = otherid;
  46.  
  47.     if (status != TRUE) {
  48.     unwatch_dealloc(fd);
  49.     }
  50.  
  51.     return status;
  52. }
  53.  
  54. void
  55. unwatchfd(int fd)
  56. {
  57.     if (watchfds[fd] == NULL)
  58.     return;
  59.  
  60.     TTunwatchfd(fd, watchfds[fd]->otherid);
  61.     unwatch_dealloc(fd);
  62. }
  63.  
  64. void
  65. dowatchcallback(int fd)
  66. {
  67.     if (watchfds[fd] == NULL || watchfds[fd]->callback == NULL)
  68.     return;
  69.  
  70.     (void) docmd(watchfds[fd]->callback, TRUE, FALSE, 1);
  71. }
  72.  
  73. static void
  74. unwatch_dealloc(int fd)
  75. {
  76.     if (watchfds[fd] == NULL)
  77.     return;
  78.  
  79.     unwatch_free_callback(watchfds[fd]->callback);
  80.     FreeAndNull(watchfds[fd]);
  81. }
  82.  
  83. static void
  84. unwatch_free_callback(char *callback)
  85. {
  86.     if (callback == NULL)
  87.     return;
  88.  
  89. #ifdef OPT_PERL
  90.     if (strncmp("perl", callback, 4) == 0)
  91.     perl_free_callback(callback);
  92. #endif
  93.  
  94.     free(callback);
  95. }
  96.