view rvinterf/rvtat/main.c @ 396:519d914e08a2

CHANGES: fc-lcdemu move-out and rvinterf -X output inversion fix
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 15 Mar 2018 01:16:17 +0000
parents 99471c57155a
children e40bb5a6c6b9
line wrap: on
line source

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "pktmux.h"
#include "limits.h"
#include "localtypes.h"
#include "exitcodes.h"

extern char *socket_pathname;
extern char *rvinterf_ttyport, *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt;

extern u_char rvi_msg[];
extern int rvi_msg_len;

char command[256], message[256];
int cmd_with_msg;

read_command_input(buf)
	char *buf;
{
	char *nl;

	if (!fgets(buf, 256, stdin))
		return(0);
	nl = index(buf, '\n');
	if (!nl) {
		printf("Ecommand or message is too long\n");
		exit(1);
	}
	*nl = '\0';
	return(1);
}

send_to_target(str)
	char *str;
{
	unsigned len;
	u_char sendpkt[MAX_PKT_TO_TARGET+1];

	len = strlen(str);
	/* fill out the packet */
	sendpkt[0] = RVT_AT_HEADER;
	strcpy(sendpkt + 1, str);
	/* send it! */
	send_pkt_to_target(sendpkt, len + 1);
	return(0);
}

execute_command()
{
	send_to_target(command);
	if (cmd_with_msg) {
		collect_pkt_from_target();
		if (rvi_msg_len != 4 || rvi_msg[2] != '>' || rvi_msg[3] != ' '){
			printf("F%.*s\n", rvi_msg_len - 2, rvi_msg + 2);
			return;
		}
		send_to_target(message);
	}
	for (;;) {
		collect_pkt_from_target();
		if (rvi_msg_len == 4 && !strncmp(rvi_msg + 2, "OK", 2) ||
		    rvi_msg_len == 7 && !strncmp(rvi_msg + 2, "ERROR", 5) ||
		    rvi_msg_len == 6 && !strncmp(rvi_msg + 2, "BUSY", 4) ||
		    rvi_msg_len == 12 &&
			!strncmp(rvi_msg + 2, "NO CARRIER", 10) ||
		    rvi_msg_len >= 12 &&
			!strncmp(rvi_msg + 2, "+CME ERROR", 10) ||
		    rvi_msg_len >= 12 &&
			!strncmp(rvi_msg + 2, "+CMS ERROR", 10)) {
			printf("F%.*s\n", rvi_msg_len - 2, rvi_msg + 2);
			return;
		}
		printf("I%.*s\n", rvi_msg_len - 2, rvi_msg + 2);
	}
}

main(argc, argv)
	char **argv;
{
	extern int optind;
	extern char *optarg;
	int c, sopt = 0;

	while ((c = getopt(argc, argv, "B:l:p:s:w:")) != EOF)
		switch (c) {
		case 'B':
			rvinterf_Bopt = optarg;
			continue;
		case 'l':
			rvinterf_lopt = optarg;
			continue;
		case 'p':
			rvinterf_ttyport = optarg;
			continue;
		case 's':
			socket_pathname = optarg;
			sopt++;
			continue;
		case 'w':
			rvinterf_wopt = optarg;
			continue;
		case '?':
		default:
			/* error msg already printed */
			exit(ERROR_USAGE);
		}
	if (rvinterf_ttyport) {
		if (sopt) {
			fprintf(stderr,
			"%s error: -p and -s options are mutually exclusive\n",
				argv[0]);
			exit(ERROR_USAGE);
		}
		launch_rvinterf();
	} else {
		if (rvinterf_Bopt || rvinterf_lopt || rvinterf_wopt) {
			fprintf(stderr,
"%s error: -B, -l and -w options are meaningful only when launching rvinterf\n",
				argv[0]);
			exit(ERROR_USAGE);
		}
		connect_local_socket();
	}

	while (read_command_input(command)) {
		if (!strcasecmp(command, "c+m")) {
			cmd_with_msg = 1;
			if (!read_command_input(command))
				break;
			if (!read_command_input(message))
				break;
		} else
			cmd_with_msg = 0;
		rx_control(1);
		execute_command();
		fflush(stdout);
	}
	exit(0);
}