4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.6 2008/02/20 17:18:33 james
14 * *** empty log message ***
16 * Revision 1.5 2008/02/20 15:50:14 james
17 * *** empty log message ***
19 * Revision 1.4 2008/02/20 02:11:35 james
20 * *** empty log message ***
22 * Revision 1.3 2008/02/14 02:46:44 james
23 * *** empty log message ***
25 * Revision 1.2 2008/02/14 00:57:58 james
26 * *** empty log message ***
28 * Revision 1.1 2008/02/05 14:25:49 james
29 * *** empty log message ***
37 char *fatal_moan(char *fmt,...)
43 n = vfprintf (stderr,fmt,ap);
52 /*make the path in fmt from home (hence the name) */
54 char *mome(char *fmt,...)
64 if (!home) return NULL;
66 homelen=strlen(home)+1;
73 fatal_moan("malloc failed");
82 n = vsnprintf (buf+homelen, size-homelen, fmt, ap);
85 if (n > -1 && n < len)
88 if (n > -1) /* glibc 2.1 */
91 size *= 2; /* twice the old size */
93 buf=realloc(buf,size);
96 fatal_moan("malloc failed");
104 main (int argc, char *argv[])
108 extern int optind, opterr, optopt;
113 Socket *server_socket = NULL, *client_socket = NULL;
117 memset (oflags[128], 0, sizeof (oflags));
118 memset (oargs[128], 0, sizeof (oargs));
120 while ((c = getopt (argc, argv, "tscr:d:pb:fL:Fk:n:")) != EOF)
129 if ((c > 64) && (c < 91))
134 else if ((c > 96) && (c < 123))
141 moan ("unknown option %c", c);
148 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r'])
150 /*If no mode is specified behave like screen */
155 if ((oflags['t'] && ((oflags['s'] || oflags['c']) || oflags['r'])) ||
156 (oflags['r'] && ((oflags['s'] || oflags['c']) || oflags['t'])) ||
157 ((oflags['s'] || oflags['c']) && (oflags['r'] || oflags['t'])))
158 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t and -r");
160 if (oflags['r'] && oflags['k'])
161 fatal_moan ("-k is incompatible with -r");
167 int id = safe_atoi (oargs['r']);
169 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
172 oargs['k'] = mome ("/.sympathy/%d", id);
177 if (oflags['c'] && !oflags['k'])
178 fatal_moan ("-c requires a socket to be specified with -s or -k");
180 if (oflags['p'] && oflags['d'])
181 fatal_moan ("-p incompatible with -d");
183 /*implement server and client by opening the server socket to prevent */
184 /*a race condition, and then forking and munging the cmd line options */
185 /*in the parent and child so that the child is the server and the */
186 /*parent becomes its client */
188 if (oflags['c'] && oflags['s'] && oflags['F'])
189 fatal_moan ("-F is incompatible with -c -s");
191 if (oflags['s'] && !oflags['k'])
194 path = mome ("/.sympathy");
198 oargs['k'] = mome ("/.sympathy/%d", getpid ());
204 server_socket = socket_listen (oargs['k']);
206 fatal_moan ("failed to create socket %s for listening", oargs['k']);
211 case 0: /*child becomes the server */
216 fatal_moan ("fork failed");
229 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
230 || oflags['L']) && oflags['c'])
231 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
233 if (oflags['t'] || oflags['s'])
235 if (!oflags['p'] && !oflags['d'])
240 if (oflags['s'] || oflags['t'])
245 log = file_log_new (oargs['L']);
247 fatal_moan ("unable to access log file %s", oargs['L']);
252 tty = ptty_open (NULL, NULL);
257 serial_open (oargs['d'],
258 oflags['K'] ? SERIAL_LOCK_ACTIVE :
259 SERIAL_LOCK_PASSIVE);
261 fatal_moan ("unable to open serial port %s", oargs['d']);
266 int baud = safe_atoi (oargs['b']);
269 fatal_maon ("Unable to parse baudrate %s", oargs['b']);
271 tty_set_baud (tty, baud);
275 tty_set_flow (tty, oflags['f'] ? 1 : 0);
282 client_socket = socket_connect (oargs['k']);
284 fatal_moan ("failed to connect to socket %s", oargs['k']);
289 if (oflags['c'] || oflags['t'])
294 fatal_moan ("fix a bug in HTML dispatcher before this works");
298 ansi = (ANSI *) malloc (sizeof (ANSI));
299 memset (ansi, 0, sizeof (ANSI));
301 terminal_register_handlers ();
302 ansi->terminal = terminal_open (0, 1);
303 ansi_reset (ansi, NULL);
307 if (oflags['s'] && !oflags['F']) {
308 /*FIXME become a daemon*/
311 mainloop (tty, server_socket, client_socket, ansi, log);
315 ansi_terminal_reset (ansi);