diff 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
line wrap: on
line diff
--- a/rfcal/cmu200/session.c	Mon Apr 24 01:43:02 2017 +0000
+++ b/rfcal/cmu200/session.c	Mon Apr 24 02:17:04 2017 +0000
@@ -11,10 +11,39 @@
 
 extern int activesock;
 
+#define	MAX_FIELDS	10
+
+char client_cmd[256], *client_cmd_fields[MAX_FIELDS+1];
+int client_cmd_nfields;
+
+parse_cmd_into_fields()
+{
+	char *cp;
+
+	client_cmd_nfields = 0;
+	for (cp = client_cmd; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (*cp == '\0')
+			break;
+		if (client_cmd_nfields >= MAX_FIELDS) {
+			send_socket_response("-Command has too many fields\n");
+			return(1);
+		}
+		client_cmd_fields[client_cmd_nfields++] = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	client_cmd_fields[client_cmd_nfields] = 0;
+	return(0);
+}
+
 handle_command()
 {
-	char readbuf[256], linebuf[256];
-	int cc, pos;
+	char readbuf[256];
+	int cc, pos, rc;
 
 	for (pos = 0; ; ) {
 		cc = read(activesock, readbuf, sizeof readbuf);
@@ -22,19 +51,25 @@
 			printf("Client program closed connection\n");
 			return(1);
 		}
-		if (pos + cc > sizeof linebuf) {
+		if (pos + cc > sizeof client_cmd) {
 			send_socket_response("-Command too long\n");
 			return(1);
 		}
-		bcopy(readbuf, linebuf + pos, cc);
+		bcopy(readbuf, client_cmd + pos, cc);
 		pos += cc;
-		if (linebuf[pos-1] == '\n')
+		if (client_cmd[pos-1] == '\n')
 			break;
 	}
-	linebuf[pos-1] = '\0';
-	printf("Client command: %s\n", linebuf);
-	/* actual command handling will go here */
-	return(0);
+	client_cmd[pos-1] = '\0';
+	printf("Client command: %s\n", client_cmd);
+	rc = parse_cmd_into_fields();
+	if (rc)
+		return(0);
+	if (!client_cmd_nfields) {
+		send_socket_response("+Empty command OK\n");
+		return(0);
+	}
+	return dispatch_client_command();
 }
 
 handle_session()