view rvinterf/asyncshell/at.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents 10f4062e049b
children
line wrap: on
line source

/*
 * Functions for the AT-over-RVTMUX interface
 */

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

extern char *rvinterf_ttyport;
extern u_char rvi_msg[];
extern int rvi_msg_len;
extern int oneshot_nowait;
extern int sock;

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

	len = strlen(str);
	if (len + 1 > MAX_PKT_TO_TARGET) {
		printf("error: max pkt to target limit exceeded\n");
		return(ERROR_USAGE);
	}
	/* 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_unterm_string_to_ati(str)
	char *str;
{
	unsigned len;
	u_char sendpkt[MAX_PKT_TO_TARGET+1];

	len = strlen(str);
	if (len + 2 > MAX_PKT_TO_TARGET) {
		printf("error: max pkt to target limit exceeded\n");
		return(ERROR_USAGE);
	}
	/* fill out the packet */
	sendpkt[0] = RVT_AT_HEADER;
	sendpkt[1] = 0x01;
	strcpy(sendpkt + 2, str);
	/* send it! */
	send_pkt_to_target(sendpkt, len + 2);
	return(0);
}

void
cmd_sendat(arg)
	char *arg;
{
	while (isspace(*arg))
		arg++;
	if (!*arg) {
		printf("error: missing string argument\n");
		return;
	}
	ati_rx_control(1);
	send_string_to_ati(arg);
}

void
cmd_unterm(arg)
	char *arg;
{
	while (isspace(*arg))
		arg++;
	if (!*arg) {
		printf("error: missing string argument\n");
		return;
	}
	ati_rx_control(1);
	send_unterm_string_to_ati(arg);
}

oneshot_at_command(cmd)
	char *cmd;
{
	fd_set fds;
	int rc;

	if (!oneshot_nowait) {
		if (rvinterf_ttyport)
			usleep(30000);
		init();		/* to catch the response properly */
		ati_rx_control(1);
	}
	rc = send_string_to_ati(cmd);
	if (rc)
		exit(rc);
	if (oneshot_nowait)
		exit(0);
	/* wait for response */
	for (;;) {
		FD_ZERO(&fds);
		FD_SET(sock, &fds);
		rc = select(sock+1, &fds, 0, 0, 0);
		if (rc < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			exit(ERROR_UNIX);
		}
		if (FD_ISSET(sock, &fds))
			handle_rvinterf_input();
	}
}

cmd_str_oneshot(argc, argv)
	char **argv;
{
	return oneshot_at_command(argv[1]);
}

cmd_unterm_oneshot(argc, argv)
	char **argv;
{
	return send_unterm_string_to_ati(argv[1]);
}

void
oneshot_at_check_response()
{
	if (rvi_msg_len == 4 && !strncmp(rvi_msg + 2, "OK", 2))
		exit(0);
	if (rvi_msg_len == 4 && !strncmp(rvi_msg + 2, "> ", 2))
		exit(0);
	if (rvi_msg_len == 7 && !strncmp(rvi_msg + 2, "ERROR", 5))
		exit(ERROR_TARGET);
	if (rvi_msg_len == 6 && !strncmp(rvi_msg + 2, "BUSY", 4))
		exit(ERROR_TARGET);
	if (rvi_msg_len == 12 && !strncmp(rvi_msg + 2, "NO CARRIER", 10))
		exit(ERROR_TARGET);
	if (rvi_msg_len >= 12 && !strncmp(rvi_msg + 2, "+CME ERROR", 10))
		exit(ERROR_TARGET);
	if (rvi_msg_len >= 12 && !strncmp(rvi_msg + 2, "+CMS ERROR", 10))
		exit(ERROR_TARGET);
}