chiark / gitweb /
*** empty log message ***
[sympathy.git] / apps / sympathyd.c
1 /*
2  * sympathy.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.6  2008/02/14 00:57:58  james
14  * *** empty log message ***
15  *
16  * Revision 1.5  2008/02/13 18:05:06  james
17  * *** empty log message ***
18  *
19  * Revision 1.4  2008/02/13 17:21:55  james
20  * *** empty log message ***
21  *
22  * Revision 1.3  2008/02/08 15:06:52  james
23  * *** empty log message ***
24  *
25  * Revision 1.2  2008/02/07 15:42:49  james
26  * *** empty log message ***
27  *
28  * Revision 1.1  2008/02/05 14:25:49  james
29  * *** empty log message ***
30  *
31  */
32
33 #include <sympathy.h>
34
35 #include "client.h"
36 #include "clients.h"
37
38
39 static void send_history(History *h,Client *c)
40 {
41 int rptr=h->wptr;
42
43 HISTORY_INC(h,rptr);
44
45 HISTORY_INC(h,rptr);
46 while (rptr!=h->wptr)
47 {
48 History_ent *l=&h->lines[rptr];
49 if (l->valid) { 
50
51 if (ipc_msg_send_history(c->s,l))
52         c->dead++;
53
54 }
55 HISTORY_INC(h,rptr);
56 }
57 }
58
59 int main (int argc,char *argv[])
60 {
61   fd_set rfds, wfds;
62   ANSI a = { 0 };
63   Context c;
64   Socket *s, *cs;
65   Clients *clients;
66
67
68   s = socket_listen ("socket");
69
70   c.t = ptty_open (NULL, NULL);
71   c.v = vt102_new ();
72   c.h = history_new (200);
73   c.l = file_log_new ("log");
74
75   terminal_register_handlers ();
76   a.terminal = terminal_open (0, 1);
77
78   ansi_reset (&a, NULL);
79
80   clients=clients_new();
81
82   for (;;)
83     {
84       struct timeval tv = { 0, 100000 };
85
86
87       FD_ZERO (&rfds);
88       FD_ZERO (&wfds);
89
90       tty_pre_select (c.t, &rfds,&wfds);
91
92       FD_SET(s->fd,&rfds);
93
94       socket_pre_select (s, &rfds, &wfds);
95
96       clients_pre_select (clients,&rfds,&wfds);
97
98       select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
99
100       if (FD_ISSET (s->fd, &rfds) && ((cs=socket_accept (s)))) {
101         {
102           Client *cl;
103           /*New client connexion */
104           cl=clients_new_client (clients, cs, &c);
105
106           send_history(c.h,cl);
107           
108         }
109       }
110
111
112       clients_post_select (clients, &c, &rfds, &wfds);
113
114       if (FD_ISSET (c.t->rfd, &rfds))
115         {
116           char buf[1024];
117           int red;
118
119           red = c.t->recv (c.t, buf, sizeof (buf));
120
121           if (red < 0)
122             break;
123
124           if (red)
125             {
126               clients_output (clients, buf, red);
127               vt102_parse (&c, buf, red);
128             }
129         }
130
131       ansi_dispatch (&a, &c);
132       ansi_update (&a, &c);
133
134       
135     }
136
137   clients_shutdown (clients);
138   ansi_terminal_reset (&a);
139   terminal_atexit ();
140   printf ("QUAT\n");
141 }