FreeCalypso > hg > freecalypso-tools
comparison rfcal/cmu200/session.c @ 196:47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 24 Apr 2017 02:17:04 +0000 |
parents | db9ee7745cdd |
children |
comparison
equal
deleted
inserted
replaced
195:db9ee7745cdd | 196:47d56330609d |
---|---|
9 #include <string.h> | 9 #include <string.h> |
10 #include <strings.h> | 10 #include <strings.h> |
11 | 11 |
12 extern int activesock; | 12 extern int activesock; |
13 | 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 | |
14 handle_command() | 43 handle_command() |
15 { | 44 { |
16 char readbuf[256], linebuf[256]; | 45 char readbuf[256]; |
17 int cc, pos; | 46 int cc, pos, rc; |
18 | 47 |
19 for (pos = 0; ; ) { | 48 for (pos = 0; ; ) { |
20 cc = read(activesock, readbuf, sizeof readbuf); | 49 cc = read(activesock, readbuf, sizeof readbuf); |
21 if (cc <= 0) { | 50 if (cc <= 0) { |
22 printf("Client program closed connection\n"); | 51 printf("Client program closed connection\n"); |
23 return(1); | 52 return(1); |
24 } | 53 } |
25 if (pos + cc > sizeof linebuf) { | 54 if (pos + cc > sizeof client_cmd) { |
26 send_socket_response("-Command too long\n"); | 55 send_socket_response("-Command too long\n"); |
27 return(1); | 56 return(1); |
28 } | 57 } |
29 bcopy(readbuf, linebuf + pos, cc); | 58 bcopy(readbuf, client_cmd + pos, cc); |
30 pos += cc; | 59 pos += cc; |
31 if (linebuf[pos-1] == '\n') | 60 if (client_cmd[pos-1] == '\n') |
32 break; | 61 break; |
33 } | 62 } |
34 linebuf[pos-1] = '\0'; | 63 client_cmd[pos-1] = '\0'; |
35 printf("Client command: %s\n", linebuf); | 64 printf("Client command: %s\n", client_cmd); |
36 /* actual command handling will go here */ | 65 rc = parse_cmd_into_fields(); |
37 return(0); | 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(); | |
38 } | 73 } |
39 | 74 |
40 handle_session() | 75 handle_session() |
41 { | 76 { |
42 int rc; | 77 int rc; |