view rfcal/cmu200/session.c @ 234:4cee70d429bf

CHANGES: fc-fsio format change documented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 12 Aug 2017 17:37:02 +0000
parents 47d56330609d
children
line wrap: on
line source

/*
 * This module contains the code that handles a single local socket
 * connection session.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

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];
	int cc, pos, rc;

	for (pos = 0; ; ) {
		cc = read(activesock, readbuf, sizeof readbuf);
		if (cc <= 0) {
			printf("Client program closed connection\n");
			return(1);
		}
		if (pos + cc > sizeof client_cmd) {
			send_socket_response("-Command too long\n");
			return(1);
		}
		bcopy(readbuf, client_cmd + pos, cc);
		pos += cc;
		if (client_cmd[pos-1] == '\n')
			break;
	}
	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()
{
	int rc;

	send_socket_response("+CMU200 interface daemon ready\n");
	for (;;) {
		rc = handle_command();
		if (rc)
			break;
	}
	close(activesock);
}