view uptools/atcmd/smdump.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 b88a37d4f148
children dc2fd8e6f42c
line wrap: on
line source

/*
 * This utility retrieves a dump of all stored SMS records in PDU mode.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../../rvinterf/include/exitcodes.h"
#include "resp_parse.h"

extern char at_response[];

struct delafter {
	unsigned	msgid;
	struct delafter	*next;
};

int delete_after_flag;
struct delafter *delafter_head, **delafter_tail = &delafter_head;

int pdu_state;
char *msgtype;
u_char pbname_gsm[40];
unsigned pbname_len, header_len;
u_char pdu_bin[176];

add_delafter_record(msgid)
	unsigned msgid;
{
	struct delafter *rec;

	rec = malloc(sizeof(struct delafter));
	if (!rec)
		return;
	rec->msgid = msgid;
	rec->next = 0;
	*delafter_tail = rec;
	delafter_tail = &rec->next;
}

validate_pbname_7bit()
{
	unsigned n;

	for (n = 0; n < pbname_len; n++)
		if (pbname_gsm[n] & 0x80)
			return(-1);
	return(0);
}

cmgl_header()
{
	struct resp_field fields[4];
	int cc;

	/* skip empty lines */
	if (!at_response[1])
		return;
	/* if not empty, it MUST be +CMGL */
	if (strncmp(at_response+1, "+CMGL: ", 7)) {
		fprintf(stderr, "error: response from target is not +CMGL\n");
		exit(ERROR_TARGET);
	}
	if (parse_structured_response(at_response+8, fields, 4) != 4) {
malformed:	fprintf(stderr, "error: malformed +CMGL response\n");
		exit(ERROR_TARGET);
	}
	if (fields[0].type != RESP_FIELD_NUMBER ||
	    fields[1].type != RESP_FIELD_NUMBER ||
	    fields[3].type != RESP_FIELD_NUMBER)
		goto malformed;
	if (fields[2].type != RESP_FIELD_STRING &&
	    fields[2].type != RESP_FIELD_EMPTY)
		goto malformed;
	if (delete_after_flag)
		add_delafter_record(fields[0].num);
	switch (fields[1].num) {
	case 0:
	case 1:
		msgtype = "Received";
		break;
	case 2:
		msgtype = "Stored unsent";
		break;
	case 3:
		msgtype = "Sent";
		break;
	default:
		fprintf(stderr,
		"error: invalid message status code in +CMGL response\n");
		exit(ERROR_TARGET);
	}
	if (fields[2].type == RESP_FIELD_STRING) {
		cc = decode_hex_line(fields[2].str, pbname_gsm,
					sizeof pbname_gsm);
		if (cc >= 1) {
			pbname_len = cc;
			if (validate_pbname_7bit() < 0)
				pbname_len = 0;
		} else
			pbname_len = 0;
	} else
		pbname_len = 0;
	header_len = fields[3].num;
	pdu_state = 1;
}

emit_pb_name()
{
	u_char decoded_name[81];

	gsm7_to_ascii_or_ext(pbname_gsm, pbname_len, decoded_name, 0, 0, 0, 0);
	printf("Phonebook-Name: %s\n", decoded_name);
}

cmgl_pdu()
{
	int cc;
	unsigned sca_len;

	cc = decode_hex_line(at_response+1, pdu_bin, sizeof pdu_bin);
	if (cc < 1) {
		fprintf(stderr, "error: expected PDU not received\n");
		exit(ERROR_TARGET);
	}
	sca_len = pdu_bin[0];
	if (1 + sca_len + header_len != cc) {
		fprintf(stderr, "error: PDU length mismatch\n");
		exit(ERROR_TARGET);
	}
	printf("%s message:\n", msgtype);
	if (pbname_len)
		emit_pb_name();
	puts(at_response+1);
	putchar('\n');
	pdu_state = 0;
}

cmgl_callback()
{
	if (pdu_state)
		cmgl_pdu();
	else
		cmgl_header();
}

delete_after_process()
{
	struct delafter *rec;
	char cmgd_cmd[32];

	for (rec = delafter_head; rec; rec = rec->next) {
		sprintf(cmgd_cmd, "AT+CMGD=%u", rec->msgid);
		atinterf_exec_cmd_needok(cmgd_cmd, 0, 0);
	}
}

main(argc, argv)
	char **argv;
{
	int c;
	extern int optind;

	while ((c = getopt(argc, argv, "B:dnp:RX:")) != EOF) {
		if (atinterf_cmdline_opt(c))
			continue;
		else if (c == 'd')
			delete_after_flag = 1;
		else {
			/* error msg already printed */
			exit(ERROR_USAGE);
		}
	}
	if (argc != optind) {
		fprintf(stderr, "usage: %s [options]\n", argv[0]);
		exit(ERROR_USAGE);
	}
	atinterf_init();
	/* enable verbose error messages */
	atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0);
	/* put the 07.05 modem in PDU mode */
	atinterf_exec_cmd_needok("AT+CMGF=0", 0, 0);
	/* HEX charset for phonebook names */
	atinterf_exec_cmd_needok("AT+CSCS=\"HEX\"", 0, 0);
	/* main command */
	atinterf_exec_cmd_needok("AT+CMGL=4", 0, cmgl_callback);
	if (pdu_state) {
		fprintf(stderr, "error: wrong state at the end of +CMGL\n");
		exit(ERROR_TARGET);
	}
	if (delete_after_flag)
		delete_after_process();
	/* be nice and restore IRA charset for manual AT command users */
	atinterf_exec_cmd_needok("AT+CSCS=\"IRA\"", 0, 0);
	exit(0);
}