]> dxcluster.net Git - spider.git/blob - src/client.c
3cd88068d54aa65f282f9230c936b85681a3282b
[spider.git] / src / client.c
1 /*
2  * C Client for the DX Spider cluster program
3  *
4  * Eventually this program will be a complete replacement
5  * for the perl version.
6  *
7  * This program provides the glue necessary to talk between
8  * an input (eg from telnet or ax25) and the perl DXSpider
9  * node.
10  *
11  * Currently, this program connects STDIN/STDOUT to the
12  * message system used by cluster.pl
13  *
14  * Copyright (c) 2000 Dirk Koopman G1TLH
15  *
16  * $Id$
17  */
18
19 #include <stdio.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <regex.h>
34
35 #include "sel.h"
36 #include "cmsg.h"
37 #include "debug.h"
38
39 #define TEXT 1
40 #define MSG 2
41 #define MAXBUFL 1024
42
43 #ifndef MAXPATHLEN 
44 #define MAXPATHLEN 256
45 #endif
46
47 #define DEFPACLEN 128
48 #define MAXPACLEN 236
49 #define MAXCALLSIGN 9
50
51 #define DBUF 1
52 #define DMSG 2
53
54 typedef struct 
55 {
56         int cnum;                                       /* the connection number */
57         int sort;                                       /* the type of connection either text or msg */
58         cmsg_t *in;                                     /* current input message being built up */
59         cmsg_t *out;                            /* current output message being sent */
60         cmsg_t *obuf;                           /* current output being buffered */
61         reft *inq;                                      /* input queue */
62         reft *outq;                                     /* output queue */
63         sel_t *sp;                                      /* my select fcb address */
64         struct termios t;                       /* any termios associated with this cnum */
65         char echo;                                      /* echo characters back to this cnum */
66         char t_set;                                     /* the termios structure is valid */
67         char buffer_it;                         /* buffer outgoing packets for paclen */
68 } fcb_t;
69
70 typedef struct 
71 {
72         char *in;
73         regex_t *regex;
74 } myregex_t;
75
76
77 char *node_addr = "localhost";  /* the node tcp address, can be overridden by DXSPIDER_HOST */
78 int node_port = 27754;                  /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
79 char *call;                                             /* the caller's callsign */
80 char *connsort;                                 /* the type of connection */
81 fcb_t *in;                                              /* the fcb of 'stdin' that I shall use */
82 fcb_t *node;                                    /* the fcb of the msg system */
83 char nl = '\n';                                 /* line end character */
84 char ending = 0;                                /* set this to end the program */
85 char send_Z = 1;                                /* set a Z record to the node on termination */
86 char echo = 1;                                  /* echo characters on stdout from stdin */
87 char int_tabs = 0;                              /* interpret tabs -> spaces */
88 char *root = "/spider";         /* root of data tree, can be overridden by DXSPIDER_ROOT  */
89 int timeout = 60;                               /* default timeout for logins and things */
90 int paclen = DEFPACLEN;                 /* default buffer size for outgoing packets */
91 int tabsize = 8;                                /* default tabsize for text messages */
92
93 myregex_t iscallreg[] = {               /* regexes to determine whether this is a reasonable callsign */
94         {
95                 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0                /* G1TLH G1TLH1 */
96         },
97         {
98                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0          /* 2E0AAA 2E0AAA1 */
99         },
100         {
101                 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0                /* G1TLH-2 */
102         },
103         {
104                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0          /* 2E0AAA-2 */
105         },
106         {
107                 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0               /* G1TLH-11 */
108         },
109         {
110                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0         /* 2E0AAA-11 */
111         },
112         {
113                 0, 0
114         }
115 };
116
117 void terminate(int);
118
119 /*
120  * utility routines - various
121  */
122
123 void die(char *s, ...)
124 {
125         char buf[2000];
126         
127         va_list ap;
128         va_start(ap, s);
129         vsnprintf(buf, sizeof(buf)-1, s, ap);
130         va_end(ap);
131         fprintf(stderr,"%s\n", buf);
132         terminate(-1);
133 }
134
135 char *strupper(char *s)
136 {
137         char *d = malloc(strlen(s)+1);
138         char *p = d;
139         
140         if (!d)
141                 die("out of room in strupper");
142         while (*p++ = toupper(*s++)) ;
143         return d;
144 }
145
146 char *strlower(char *s)
147 {
148         char *d = malloc(strlen(s)+1);
149         char *p = d;
150         
151         if (!d)
152                 die("out of room in strlower");
153         while (*p++ = tolower(*s++)) ;
154         return d;
155 }
156
157 int eq(char *a, char *b)
158 {
159         return (strcmp(a, b) == 0);
160 }
161
162 int xopen(char *dir, char *name, int mode)
163 {
164         char fn[MAXPATHLEN+1];
165         snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
166         return open(fn, mode);
167 }
168
169 int iscallsign(char *s)
170 {
171         myregex_t *rp;
172
173         if (strlen(s) > MAXCALLSIGN)
174                 return 0;
175         
176         for (rp = iscallreg; rp->in; ++rp) {
177                 if (regexec(rp->regex, s, 0, 0, 0) == 0)
178                         return 1;
179         }
180         return 0;
181 }
182
183 /*
184  * higher level send and receive routines
185  */
186
187 fcb_t *fcb_new(int cnum, int sort)
188 {
189         fcb_t *f = malloc(sizeof(fcb_t));
190         if (!f)
191                 die("no room in fcb_new");
192         memset (f, 0, sizeof(fcb_t));
193         f->cnum = cnum;
194         f->sort = sort;
195         f->inq = chain_new();
196         f->outq = chain_new();
197         return f;
198 }
199
200 void flush_text(fcb_t *f)
201 {
202         if (f->obuf) {
203                 cmsg_send(f->outq, f->obuf, 0);
204                 f->sp->flags |= SEL_OUTPUT;
205                 f->obuf = 0;
206         }
207 }
208
209 void send_text(fcb_t *f, char *s, int l)
210 {
211         cmsg_t *mp;
212         char *p;
213         
214         if (f->buffer_it && f->obuf) {
215                 mp = f->obuf;
216         } else {
217                 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
218         }
219
220         /* remove trailing spaces  */
221         while (l > 0 &&isspace(s[l-1]))
222                 --l;
223
224         for (p = s; p < s+l; ) {
225                 if (mp->inp >= mp->data + paclen) {
226                         flush_text(f);
227                         f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
228                 }
229                 *mp->inp++ = *p++;
230         }
231         if (mp->inp >= mp->data + paclen) {
232                 flush_text(f);
233                 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
234         }
235         *mp->inp++ = '\n';
236         if (!f->buffer_it)
237                 flush_text(f);
238 }
239
240 void send_msg(fcb_t *f, char let, unsigned char *s, int l)
241 {
242         cmsg_t *mp;
243         int ln;
244         int myl = strlen(call)+2+l;
245
246         mp = cmsg_new(myl+4+1, f->sort, f);
247         *mp->inp++ = let;
248         strcpy(mp->inp, call);
249         mp->inp += strlen(call);
250         *mp->inp++ = '|';
251         if (l > 0) {
252                 unsigned char *p;
253                 for (p = s; p < s+l; ++p) {
254                         if (mp->inp >= mp->data + (myl - 4)) {
255                                 int off = mp->inp - mp->data;
256                                 myl += 256;
257                                 mp = realloc(mp, myl);
258                                 mp->inp = mp->data + off;
259                         }
260                         
261                         if (*p < 0x20 || *p > 0x7e || *p == '%') {
262                                 sprintf(mp->inp, "%%%02X", *p & 0xff);
263                                 mp->inp += strlen(mp->inp);
264                         } else 
265                                 *mp->inp++ = *p;
266                 }
267         } 
268         *mp->inp++ = '\n';
269         *mp->inp = 0;
270         cmsg_send(f->outq, mp, 0);
271         f->sp->flags |= SEL_OUTPUT;
272 }
273
274 /*
275  * the callback (called by sel_run) that handles all the inputs and outputs
276  */
277
278 int fcb_handler(sel_t *sp, int in, int out, int err)
279 {
280         fcb_t *f = sp->fcb;
281         cmsg_t *mp, *omp;
282         unsigned char c;
283         
284         /* input modes */
285         if (in) {
286                 char *p, buf[MAXBUFL];
287                 int r;
288
289                 /* read what we have into a buffer */
290                 r = read(f->cnum, buf, MAXBUFL);
291                 if (r < 0) {
292                         switch (errno) {
293                         case EINTR:
294                         case EINPROGRESS:
295                         case EAGAIN:
296                                 goto lout;
297                         default:
298 /*                              if (f->sort == MSG)
299                                 send_Z = 0; */
300                                 ending++;
301                                 return 0;
302                         }
303                 } else if (r == 0) {
304 /*                      if (f->sort == MSG)
305                         send_Z = 0; */
306                         ending++;
307                         return 0;
308                 }
309
310                 dbgdump(DBUF, "in ->", buf, r);
311                 
312                 /* create a new message buffer if required */
313                 if (!f->in)
314                         f->in = cmsg_new(MAXBUFL+1, f->sort, f);
315                 mp = f->in;
316
317                 switch (f->sort) {
318                 case TEXT:
319                         p = buf;
320                         if (f->echo)
321                                 omp = cmsg_new(3*r+1, f->sort, f);
322                         while (r > 0 && p < &buf[r]) {
323
324                                 /* echo processing */
325                                 if (f->echo) {
326                                         switch (*p) {
327                                         case '\b':
328                                         case 0x7f:
329                                                 strcpy(omp->inp, "\b \b");
330                                                 omp->inp += strlen(omp->inp);
331                                                 break;
332                                         default:
333                                                 *omp->inp++ = *p;
334                                         }
335                                 }
336                                 
337                                 /* character processing */
338                                 switch (*p) {
339                                 case '\t':
340                                         if (int_tabs) {
341                                                 memset(mp->inp, ' ', tabsize);
342                                                 mp->inp += tabsize;
343                                                 ++p;
344                                         } else {
345                                                 *mp->inp++ = *p++;
346                                         }
347                                         break;
348                                 case '\b':
349                                 case 0x7f:
350                                         if (mp->inp > mp->data)
351                                                 mp->inp--;
352                                         ++p;
353                                         break;
354                                 default:
355                                         if (nl == '\n' && *p == '\r') {   /* ignore \r in telnet mode (ugh) */
356                                                 p++;
357                                         } else if (*p == nl) {
358                                                 if (mp->inp == mp->data)
359                                                         *mp->inp++ = ' ';
360                                                 *mp->inp = 0;              /* zero terminate it, but don't include it in the length */
361                                                 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
362                                                 cmsg_send(f->inq, mp, 0);
363                                                 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
364                                                 ++p;
365                                         } else {
366                                                 if (mp->inp < &mp->data[MAXBUFL-8])
367                                                         *mp->inp++ = *p++;
368                                                 else {
369                                                         mp->inp = mp->data;
370                                                 }
371                                         }
372                                 }
373                         }
374                         
375                         /* queue any echo text */
376                         if (f->echo) {
377                                 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
378                                 cmsg_send(f->outq, omp, 0);
379                                 f->sp->flags |= SEL_OUTPUT;
380                         }
381                         
382                         break;
383
384                 case MSG:
385                         p = buf;
386                         while (r > 0 && p < &buf[r]) {
387                                 unsigned char ch = *p++;
388                                 
389                                 if (mp->inp >= mp->data + (MAXBUFL-1)) {
390                                         mp->state = 0;
391                                         mp->inp = mp->data;
392                                         dbg(DMSG, "Message longer than %d received", MAXBUFL);
393                                 }
394
395                                 switch (mp->state) {
396                                 case 0: 
397                                         if (ch == '%') {
398                                                 c = 0;
399                                                 mp->state = 1;
400                                         } else if (ch == '\n') {
401                                                 /* kick it upstairs */
402                                                 *mp->inp = 0;
403                                                 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
404                                                 cmsg_send(f->inq, mp, 0);
405                                                 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
406                                         } else if (ch < 0x20 || ch > 0x7e) {
407                                                 dbg(DMSG, "Illegal character (0x%02X) received", *p);
408                                                 mp->inp = mp->data;
409                                         } else {
410                                                 *mp->inp++ = ch;
411                                         }
412                                         break;
413
414                                 case 1:
415                                         mp->state = 2;
416                                         if (ch >= '0' && ch <= '9') 
417                                                 c = (ch - '0') << 4;
418                                         else if (ch >= 'A' && ch <= 'F')
419                                                 c = (ch - 'A' + 10) << 4;
420                                         else if (ch >= 'a' && ch <= 'a')
421                                                 c = (ch - 'a' + 10) << 4;
422                                         else {
423                                                 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
424                                                 mp->inp = mp->data;
425                                                 mp->state = 0;
426                                         }
427                                         break;
428                                         
429                                 case 2:
430                                         if (ch >= '0' && ch <= '9') 
431                                                 *mp->inp++ = c | (ch - '0');
432                                         else if (ch >= 'A' && ch <= 'F')
433                                                 *mp->inp++ = c | (ch - 'A' + 10);
434                                         else if (ch >= 'a' && ch <= 'a')
435                                                 *mp->inp++ = c | (ch - 'a' + 10);
436                                         else {
437                                                 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
438                                                 mp->inp = mp->data;
439                                         }
440                                         mp->state = 0;
441                                 }
442                         }
443                         break;
444                         
445                 default:
446                         die("invalid sort (%d) in input handler", f->sort);
447                 }
448         }
449         
450         /* output modes */
451 lout:;
452         if (out) {
453                 int l, r;
454                 
455                 if (!f->out) {
456                         mp = f->out = cmsg_next(f->outq);
457                         if (!mp) {
458                                 sp->flags &= ~SEL_OUTPUT;
459                                 return 0;
460                         }
461                         mp->inp = mp->data;
462                 }
463                 l = mp->size - (mp->inp - mp->data);
464                 if (l > 0) {
465                         
466                         dbgdump(DBUF, "<-out", mp->inp, l);
467                         
468                         r = write(f->cnum, mp->inp, l);
469                         if (r < 0) {
470                                 switch (errno) {
471                                 case EINTR:
472                                 case EINPROGRESS:
473                                 case EAGAIN:
474                                         goto lend;
475                                 default:
476 /*                                      if (f->sort == MSG)
477                                         send_Z = 0; */
478                                         ending++;
479                                         return;
480                                 }
481                         } else if (r > 0) {
482                                 mp->inp += r;
483                         }
484                 } else if (l < 0) 
485                         die("got negative length in handler on node");
486                 if (mp->inp - mp->data >= mp->size) {
487                         cmsg_callback(mp, 0);
488                         f->out = 0;
489                 }
490         }
491 lend:;
492         return 0;
493 }
494
495 /*
496  * things to do with initialisation
497  */
498
499 void initargs(int argc, char *argv[])
500 {
501         int i, c, err = 0;
502
503         while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
504                 switch (c) {
505                 case 'h':
506                         node_addr = optarg;
507                         break;
508                 case 'l':
509                         paclen = atoi(optarg);
510                         if (paclen < 80)
511                                 paclen = 80;
512                         if (paclen > MAXPACLEN)
513                                 paclen = MAXPACLEN;
514                         break;
515                 case 'p':
516                         node_port = atoi(optarg);
517                         break;
518                 case 'x':
519                         dbginit("client");
520                         dbgset(atoi(optarg));
521                         break;
522                 default:
523                         ++err;
524                         goto lerr;
525                 }
526         }
527
528 lerr:
529         if (err) {
530                 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
531         }
532         
533         if (optind < argc) {
534                 call = strupper(argv[optind]);
535                 ++optind;
536         }
537         if (!call)
538                 die("Must have at least a callsign (for now)");
539
540         if (optind < argc) {
541                 connsort = strlower(argv[optind]);
542                 if (eq(connsort, "telnet") || eq(connsort, "local")) {
543                         nl = '\n';
544                         echo = 1;
545                 } else if (eq(connsort, "ax25")) {
546                         nl = '\r';
547                         echo = 0;
548                 } else {
549                         die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
550                 }
551         } else {
552                 connsort = "local";
553                 nl = '\n';
554                 echo = 1;
555         }
556
557         /* this is kludgy, but hey so is the rest of this! */
558         if (!eq(connsort, "ax25") && paclen == DEFPACLEN) {
559                 paclen = MAXPACLEN;
560         }
561 }
562
563 void connect_to_node()
564 {
565         struct hostent *hp, *gethostbyname();
566         struct sockaddr_in server;
567         int nodef;
568         sel_t *sp;
569                                 
570         if ((hp = gethostbyname(node_addr)) == 0) 
571                 die("Unknown host tcp host %s for printer", node_addr);
572
573         memset(&server, 0, sizeof server);
574         server.sin_family = AF_INET;
575         memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
576         server.sin_port = htons(node_port);
577                                                 
578         nodef = socket(AF_INET, SOCK_STREAM, 0);
579         if (nodef < 0) 
580                 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
581
582         if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
583                 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
584         }
585         node = fcb_new(nodef, MSG);
586         node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
587         
588 }
589
590 /*
591  * things to do with going away
592  */
593
594 void term_timeout(int i)
595 {
596         /* none of this is going to be reused so don't bother cleaning up properly */
597         if (in && in->t_set)
598                 tcsetattr(0, TCSANOW, &in->t);
599         if (node) {
600                 shutdown(node->cnum, 3);
601                 close(node->cnum);
602         }
603         exit(i);
604 }
605
606 void terminate(int i)
607 {
608         if (node && send_Z && call) {
609                 send_msg(node, 'Z', "bye", 3);
610         }
611         
612         signal(SIGALRM, term_timeout);
613         alarm(10);
614         
615         while ((in && !is_chain_empty(in->outq)) ||
616                    (node && !is_chain_empty(node->outq))) {
617                 sel_run();
618         }
619         if (in && in->t_set)
620                 tcsetattr(0, TCSADRAIN, &in->t);
621         if (node) {
622                 shutdown(node->cnum, 3);
623                 close(node->cnum);
624         }
625         exit(i);
626 }
627
628 void login_timeout(int i)
629 {
630         write(0, "Timed Out", 10);
631         write(0, &nl, 1);
632         sel_run();                                      /* force a coordination */
633         if (in && in->t_set)
634                 tcsetattr(0, TCSANOW, &in->t);
635         exit(i);
636 }
637
638 /*
639  * things to do with ongoing processing of inputs
640  */
641
642 void process_stdin()
643 {
644         cmsg_t *mp = cmsg_next(in->inq);
645         if (mp) {
646                 dbg(DMSG, "MSG size: %d", mp->size);
647         
648                 if (mp->size > 0 && mp->inp > mp->data) {
649                         send_msg(node, 'I', mp->data, mp->size);
650                 }
651                 cmsg_callback(mp, 0);
652         }
653 }
654
655 void process_node()
656 {
657         cmsg_t *mp = cmsg_next(node->inq);
658         if (mp) {
659                 dbg(DMSG, "MSG size: %d", mp->size);
660         
661                 if (mp->size > 0 && mp->inp > mp->data) {
662                         char *p = strchr(mp->data, '|');
663                         if (p)
664                                 p++;
665                         switch (mp->data[0]) {
666                         case 'Z':
667                                 send_Z = 0;
668                                 ending++;
669                                 return;
670                         case 'E':
671                                 if (isdigit(*p))
672                                         in->echo = *p - '0';
673                                 break;
674                         case 'B':
675                                 if (isdigit(*p))
676                                         in->buffer_it = *p - '0';
677                                 break;
678                         case 'D':
679                                 if (p) {
680                                         int l = mp->inp - (unsigned char *) p;
681                                         send_text(in, p, l);
682                                 }
683                                 break;
684                         default:
685                                 break;
686                         }
687                 }
688                 cmsg_callback(mp, 0);
689         } else {
690                 flush_text(in);
691         }
692 }
693
694 /*
695  * the program itself....
696  */
697
698 main(int argc, char *argv[])
699 {
700         /* set up environment */
701         {
702                 char *p = getenv("DXSPIDER_ROOT");
703                 if (p)
704                         root = p;
705                 p = getenv("DXSPIDER_HOST");
706                 if (p)
707                         node_addr = p;
708                 p = getenv("DXSPIDER_PORT");
709                 if (p)
710                         node_port = atoi(p);
711                 p = getenv("DXSPIDER_PACLEN");
712                 if (p) {
713                         paclen = atoi(p);
714                         if (paclen < 80)
715                                 paclen = 80;
716                         if (paclen > MAXPACLEN)
717                                 paclen = MAXPACLEN;
718                 }
719         }
720         
721         /* get program arguments, initialise stuff */
722         initargs(argc, argv);
723         sel_init(10, 0, 10000);
724
725         /* trap signals */
726         signal(SIGHUP, SIG_IGN);
727         signal(SIGINT, terminate);
728         signal(SIGQUIT, terminate);
729         signal(SIGTERM, terminate);
730 #ifdef SIGPWR
731         signal(SIGPWR, terminate);
732 #endif
733
734         /* compile regexes for iscallsign */
735         {
736                 myregex_t *rp;
737                 for (rp = iscallreg; rp->in; ++rp) {
738                         regex_t reg;
739                         int r = regcomp(&reg, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
740                         if (r)
741                                 die("regcomp returned %d for '%s'", r, rp->in);
742                         rp->regex = malloc(sizeof(regex_t));
743                         if (!rp->regex)
744                                 die("out of room - compiling regexes");
745                         *rp->regex = reg;
746                 }
747         }
748         
749         /* is this a login? */
750         if (eq(call, "LOGIN")) {
751                 char buf[MAXPACLEN+1];
752                 int r;
753                 int f = xopen("data", "issue", 0);
754                 if (f > 0) {
755                         while ((r = read(f, buf, paclen)) > 0) {
756                                 if (nl != '\n') {
757                                         char *p;
758                                         for (p = buf; p < &buf[r]; ++p) {
759                                                 if (*p == '\n')
760                                                         *p = nl;
761                                         }
762                                 }
763                                 write(0, buf, r);
764                         }
765                         close(f);
766                 }
767                 signal(SIGALRM, login_timeout);
768                 alarm(timeout);
769                 write(0, "login: ", 7);
770                 r = read(0, buf, 20);
771                 if (r <= 0)
772                         die("No login or error (%d)", errno);
773                 signal(SIGALRM, SIG_IGN);
774                 alarm(0);
775                 while (r > 0) {
776                         if (buf[r-1] == ' ' || buf[r-1] == '\r' || buf[r-1] == '\n')
777                                 --r;
778                         else
779                                 break;
780                 }
781                 buf[r] = 0;
782                 call = strupper(buf);
783         }
784
785         /* check the callsign */
786         if (!iscallsign(call)) {
787                 die("Sorry, %s isn't a valid callsign", call);
788         }
789         
790         /* connect up stdin */
791         in = fcb_new(0, TEXT);
792         in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
793         if (tcgetattr(0, &in->t) < 0) {
794                 echo = 0;
795                 in->t_set = 0;
796         } else {
797                 struct termios t = in->t;
798                 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
799                 if (tcsetattr(0, TCSANOW, &t) < 0) 
800                         die("tcsetattr (%d)", errno);
801                 in->echo = echo;
802                 in->t_set = 1;
803         }
804         in->buffer_it = 1;
805
806         /* connect up node */
807         connect_to_node();
808
809         /* tell the cluster who I am */
810         send_msg(node, 'A', connsort, strlen(connsort));
811         
812         /* main processing loop */
813         while (!ending) {
814                 sel_run();
815                 if (!ending) {
816                         process_stdin();
817                         process_node();
818                 }
819         }
820         terminate(0);
821 }
822
823
824
825
826
827