comparison cmu200/session.c @ 0:bd62be88259d

initial import of rfcal code and docs from freecalypso-tools repository
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 May 2017 18:49:35 +0000
parents
children b552e8d86474
comparison
equal deleted inserted replaced
-1:000000000000 0:bd62be88259d
1 /*
2 * This module contains the code that handles a single local socket
3 * connection session.
4 */
5
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11
12 extern int activesock;
13
14 #define MAX_FIELDS 10
15
16 char client_cmd[256], *client_cmd_fields[MAX_FIELDS+1];
17 int client_cmd_nfields;
18
19 parse_cmd_into_fields()
20 {
21 char *cp;
22
23 client_cmd_nfields = 0;
24 for (cp = client_cmd; ; ) {
25 while (isspace(*cp))
26 cp++;
27 if (*cp == '\0')
28 break;
29 if (client_cmd_nfields >= MAX_FIELDS) {
30 send_socket_response("-Command has too many fields\n");
31 return(1);
32 }
33 client_cmd_fields[client_cmd_nfields++] = cp;
34 while (*cp && !isspace(*cp))
35 cp++;
36 if (*cp)
37 *cp++ = '\0';
38 }
39 client_cmd_fields[client_cmd_nfields] = 0;
40 return(0);
41 }
42
43 handle_command()
44 {
45 char readbuf[256];
46 int cc, pos, rc;
47
48 for (pos = 0; ; ) {
49 cc = read(activesock, readbuf, sizeof readbuf);
50 if (cc <= 0) {
51 printf("Client program closed connection\n");
52 return(1);
53 }
54 if (pos + cc > sizeof client_cmd) {
55 send_socket_response("-Command too long\n");
56 return(1);
57 }
58 bcopy(readbuf, client_cmd + pos, cc);
59 pos += cc;
60 if (client_cmd[pos-1] == '\n')
61 break;
62 }
63 client_cmd[pos-1] = '\0';
64 printf("Client command: %s\n", client_cmd);
65 rc = parse_cmd_into_fields();
66 if (rc)
67 return(0);
68 if (!client_cmd_nfields) {
69 send_socket_response("+Empty command OK\n");
70 return(0);
71 }
72 return dispatch_client_command();
73 }
74
75 handle_session()
76 {
77 int rc;
78
79 send_socket_response("+CMU200 interface daemon ready\n");
80 for (;;) {
81 rc = handle_command();
82 if (rc)
83 break;
84 }
85 close(activesock);
86 }