"Fossies" - the Fresh Open Source Software Archive

Member "netbiff-0.9.18/gui_text.c" (21 Sep 2003, 3629 Bytes) of package /linux/privat/old/netbiff-0.9.18.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "gui.h"
    2 
    3 #include <stdio.h>
    4 #include <stdarg.h>
    5 #include <unistd.h>
    6 #include <errno.h>
    7 #include <string.h>
    8 #include <time.h>
    9 #include <sys/time.h>
   10 #include <signal.h>
   11 
   12 #include "connection.h"
   13 #include "proto.h"
   14 #include "xlib.h"
   15 #include "conf.h"
   16 
   17 static fd_set rfds;
   18 static fd_set wfds;
   19 static int maxfd;
   20 
   21 static void text_flagup(const Connection *c) {
   22   printf("The flag is going up for connection %s!\n", c->name);
   23 }
   24 
   25 static void text_flagdown(const Connection *c) {
   26   printf("The flag is going down for connection %s!\n", c->name);
   27 }
   28 
   29 static void text_init(int *argc, char ***argv) {
   30   FD_ZERO(&rfds);
   31   FD_ZERO(&wfds);
   32   maxfd = -1;
   33   signal(SIGPIPE, SIG_IGN);
   34 }
   35 
   36 static int text_display_message(const char *fmt, va_list ap) {
   37   char buf[128];
   38   struct tm *tm;
   39   time_t t;
   40 
   41   t = time(NULL);
   42   tm = localtime(&t);
   43   strftime(buf, 128, "[%b %d %H:%M:%S] ", tm);
   44   fputs(buf, stdout);
   45   vfprintf(stdout, fmt, ap);
   46   fputc('\n', stdout);
   47   fflush(stdout);
   48   return 0;
   49 }
   50 
   51 static unsigned text_calc_timeout(time_t next_poll) {
   52   time_t when;
   53   time_t now;
   54   int i;
   55 
   56   when = next_poll;
   57   for(i = 0; i < nconnections; i++) {
   58     if(connections[i].state == CSTATE_PENALTY_BOX) {
   59       if(connections[i].retry_time < when) {
   60         when = connections[i].retry_time;
   61       }
   62     }
   63   }
   64 
   65   now = time(NULL);
   66   return (when < now) ? (0) : (when - now);
   67 }
   68 
   69 static void text_event_loop() {
   70   time_t next_poll;
   71 
   72   next_poll = time(NULL);
   73 
   74   while(1) {
   75     fd_set r, w;
   76     struct timeval tv;
   77 
   78     r = rfds;
   79     w = wfds;
   80     tv.tv_usec = 0;
   81     tv.tv_sec = text_calc_timeout(next_poll);
   82 
   83     if(select(maxfd, &r, &w, NULL, &tv) < 0) {
   84       if(errno != EINTR) {
   85         xerror("select(): %s\n", strerror(errno));
   86         return;
   87       }
   88       continue;
   89     }
   90 
   91     proto_do_retries();
   92     proto_do_io(&r, &w);
   93     
   94     {
   95       time_t now;
   96       now = time(NULL);
   97       if(now >= next_poll) {
   98         proto_do_poll();
   99         next_poll = now + poll_frequency;
  100       }
  101     }
  102   }
  103 }
  104 
  105 static void text_beep() {
  106   xerror("BEEP!");
  107 }
  108 
  109 static void text_add_fd(const Connection *c, int type) {
  110   if(type & GUI_FD_READ) {
  111     FD_SET(c->fd_in, &rfds);
  112     if(c->fd_in >= maxfd)
  113       maxfd = c->fd_in + 1;
  114   }
  115   if(type & GUI_FD_WRITE) {
  116     FD_SET(c->fd_out, &wfds);
  117     if(c->fd_out >= maxfd)
  118       maxfd = c->fd_out + 1;
  119   }
  120 }
  121 
  122 static void text_delete_fd(const Connection *c, int type) {
  123   int i;
  124 
  125   if(type & GUI_FD_READ)
  126     FD_CLR(c->fd_in, &rfds);
  127   if(type & GUI_FD_WRITE)
  128     FD_CLR(c->fd_out, &wfds);
  129 
  130   maxfd = -1;
  131 
  132   for(i = 0; i < nconnections; i++) {
  133     int fd;
  134 
  135     fd = connections[i].fd_in;
  136     if(fd > maxfd && FD_ISSET(fd, &rfds))
  137       maxfd = fd;
  138     fd = connections[i].fd_out;
  139     if(fd > maxfd && FD_ISSET(fd, &wfds))
  140       maxfd = fd;
  141   }
  142 
  143   maxfd++;
  144 }
  145 
  146 static char *text_request_data(const char *prompt, int hidden) {
  147   char *retval;
  148 
  149   if(hidden) {
  150     char buf[1024];
  151     char *pass;
  152 
  153     snprintf(buf, 1024, "A module has requested the data item %s: ", prompt);
  154     pass = getpass(buf);
  155 
  156     retval = strdup(pass);
  157     while(*pass)
  158       *pass++ = '\0';
  159   }
  160   else {
  161     char buf[1024];
  162 
  163     printf("A module has requested the data item %s: ", prompt);
  164     fgets(buf, 1024, stdin);
  165 
  166     retval = strdup(buf);
  167   }
  168   return retval;
  169 }
  170 
  171 static void text_register_connections() { /* NOOP */ }
  172 static void text_schedule_retry(time_t s) { /* NOOP */ }
  173 
  174 GUI gui_text = {
  175   text_flagup,
  176   text_flagdown,
  177   text_init,
  178   text_display_message,
  179   text_event_loop,
  180   text_beep,
  181   text_add_fd,
  182   text_delete_fd,
  183   text_request_data,
  184   text_register_connections,
  185   text_schedule_retry
  186 };
  187 GUI *gui_text_description = &gui_text;