view rvinterf/rvtat/main.c @ 746:f19c347d0a80

tiffs-mkfile utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 19 Oct 2020 17:39:19 +0000
parents 261c1b6d936e
children
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[508], message[508];
int cmd_with_msg;

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

	if (!fgets(buf, 508, 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_prefix(str)
	char *str;
{
	u_char sendpkt[MAX_PKT_TO_TARGET];

	/* fill out the packet */
	sendpkt[0] = RVT_AT_HEADER;
	sendpkt[1] = 0x01;
	bcopy(str, sendpkt + 2, 252);
	/* send it! */
	send_pkt_to_target(sendpkt, 254);
	return(0);
}

send_to_target_plain(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);
}

send_to_target(str)
	char *str;
{
	unsigned len;

	len = strlen(str);
	if (len <= 254)
		send_to_target_plain(str);
	else {
		send_to_target_prefix(str);
		send_to_target_plain(str + 252);
	}
	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(1);
	} 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);
}