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

/*
 * This is the main module for the basic fcup-smsend utility.
 */

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

#define	MAX_MSG_CHARS	(153*255)
#define	MAX_MSG_UNI	(67*255)

int sms_write_mode, text_mode, utf8_input, ucs2_mode;
int concat_enable, concat_refno_set, concat_quiet;
u_char dest_addr[12];
char msgtext[MAX_MSG_CHARS*2+2];
u_char concat_refno;

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

	while ((c = getopt(argc, argv, "B:cC:np:qRtuUwWX:")) != EOF) {
		if (atinterf_cmdline_opt(c))
			continue;
		switch (c) {
		case 'c':
			concat_enable = 1;
			continue;
		case 'C':
			concat_enable = 1;
			concat_refno = strtoul(optarg, 0, 0);
			concat_refno_set = 1;
			continue;
		case 'q':
			concat_quiet = 1;
			continue;
		case 't':
			text_mode = 1;
			continue;
		case 'u':
			utf8_input = 1;
			continue;
		case 'U':
			ucs2_mode = 1;
			continue;
		case 'w':
			sms_write_mode = 1;
			continue;
		case 'W':
			sms_write_mode = 2;
			concat_quiet = 1;
			continue;
		default:
			/* error msg already printed */
			exit(ERROR_USAGE);
		}
	}
	if (concat_enable && text_mode) {
		fprintf(stderr,
			"%s error: cannot send concat SMS in text mode\n",
			argv[0]);
		exit(ERROR_USAGE);
	}
	if (ucs2_mode && text_mode) {
		fprintf(stderr, "%s error: UCS-2 not supported in text mode\n",
			argv[0]);
		exit(ERROR_USAGE);
	}
	if (argc > optind + 2) {
		fprintf(stderr, "usage: %s [options] dest-addr [message]\n",
			argv[0]);
		exit(ERROR_USAGE);
	}
	if (!argv[optind] || !argv[optind][0]) {
		if (sms_write_mode == 2) {
			dest_addr[0] = 0;
			dest_addr[1] = 0x80;
		} else {
			fprintf(stderr,
			"error: destination address must be specified\n");
			exit(ERROR_USAGE);
		}
	} else if (parse_and_encode_dest_addr(argv[optind], dest_addr) < 0) {
		fprintf(stderr,
			"error: destination address argument is invalid\n");
		exit(ERROR_USAGE);
	}
	if (argc < optind + 2)
		return(0);
	if (strlen(argv[optind+1]) > MAX_MSG_CHARS*2) {
		fprintf(stderr, "error: message argument is too long\n");
		exit(ERROR_USAGE);
	}
	strcpy(msgtext, argv[optind+1]);
	return(1);
}

read_msgtext_from_stdin()
{
	unsigned pos, remain;
	int cc;

	pos = 0;
	remain = sizeof(msgtext);
	for (;;) {
		if (!remain) {
			fprintf(stderr,
				"error: message on stdin is too long\n");
			exit(ERROR_USAGE);
		}
		cc = read(0, msgtext + pos, remain);
		if (cc < 0) {
			fprintf(stderr, "error reading message from stdin\n");
			exit(ERROR_USAGE);
		}
		if (cc == 0)
			break;
		pos += cc;
		remain -= cc;
	}
	msgtext[pos] = '\0';
}

trim_trailing_newlines()
{
	char *cp;

	cp = index(msgtext, '\0');
	while (cp > msgtext && cp[-1] == '\n')
		cp--;
	*cp = '\0';
}

common_init()
{
	atinterf_init();
	/* enable verbose error messages */
	atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0);
}

gsm7_mode_main()
{
	u_char msgtext_gsm7[MAX_MSG_CHARS];
	unsigned msgtext_gsmlen;
	int rc;
	unsigned nparts, n;
	u_char udh[5];
	unsigned pos, remain, chunk;

	if (utf8_input && utf8_to_latin1(msgtext) < 0) {
		fprintf(stderr, "error: invalid UTF-8 message\n");
		exit(ERROR_USAGE);
	}
	if (text_mode) {
		if (index(msgtext, '\n')) {
			fprintf(stderr,
		"error: multiline messages not supported in text mode\n");
			exit(ERROR_USAGE);
		}
		if (strlen(msgtext) > 160) {
too_long_for_one_sms:	fprintf(stderr, "error: message exceeds 160 chars\n");
			exit(ERROR_USAGE);
		}
		common_init();
		prep_for_text_mode();
		send_in_text_mode(dest_addr, msgtext);
		if (sms_write_mode == 1)
			sendafterwr_process();
		exit(0);
	}
	rc = latin1_to_gsm7(msgtext, msgtext_gsm7, MAX_MSG_CHARS,
				&msgtext_gsmlen);
	if (rc == -1) {
		fprintf(stderr, "error: message not valid for GSM7 charset\n");
		exit(ERROR_USAGE);
	}
	if (rc == -2) {
		fprintf(stderr, "error: message too long for max concat SMS\n");
		exit(ERROR_USAGE);
	}
	if (msgtext_gsmlen <= 160) {
		common_init();
		prep_for_pdu_mode();
		send_in_pdu_mode(dest_addr, msgtext_gsm7, msgtext_gsmlen, 0, 0);
		if (sms_write_mode == 1)
			sendafterwr_process();
		if (concat_enable && !concat_quiet)
			printf("Message sent as single SMS\n");
		exit(0);
	}
	if (!concat_enable)
		goto too_long_for_one_sms;
	if (!concat_refno_set)
		concat_refno = get_concsms_refno_from_host_fs();
	nparts = (msgtext_gsmlen + 152) / 153;
	udh[0] = 0x00;
	udh[1] = 0x03;
	udh[2] = concat_refno;
	udh[3] = nparts;
	common_init();
	prep_for_pdu_mode();
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=1", 0, 0);
	pos = 0;
	remain = msgtext_gsmlen;
	for (n = 1; n <= nparts; n++) {
		udh[4] = n;
		chunk = 153;
		if (chunk > remain)
			chunk = remain;
		send_in_pdu_mode(dest_addr, msgtext_gsm7 + pos, chunk, udh, 5);
		pos += chunk;
		remain -= chunk;
	}
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=0", 0, 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	if (!concat_quiet)
		printf("Message sent as %u SMS segments\n", nparts);
	exit(0);
}

ucs2_mode_main()
{
	u_short msgtext_uni[MAX_MSG_UNI];
	unsigned msgtext_unilen;
	int rc;
	unsigned nparts, n;
	u_char udh[5];
	unsigned pos, remain, chunk;

	rc = utf8_to_ucs2(msgtext, msgtext_uni, MAX_MSG_UNI, &msgtext_unilen);
	if (rc == -1) {
		fprintf(stderr, "error: invalid UTF-8 message\n");
		exit(ERROR_USAGE);
	}
	if (rc == -2) {
		fprintf(stderr, "error: message too long for max concat SMS\n");
		exit(ERROR_USAGE);
	}
	if (msgtext_unilen <= 70) {
		common_init();
		prep_for_pdu_mode();
		send_pdu_ucs2(dest_addr, msgtext_uni, msgtext_unilen, 0, 0);
		if (sms_write_mode == 1)
			sendafterwr_process();
		if (concat_enable && !concat_quiet)
			printf("Message sent as single SMS\n");
		exit(0);
	}
	if (!concat_enable) {
		fprintf(stderr, "error: message exceeds 70 UCS-2 chars\n");
		exit(ERROR_USAGE);
	}
	if (!concat_refno_set)
		concat_refno = get_concsms_refno_from_host_fs();
	nparts = (msgtext_unilen + 66) / 67;
	udh[0] = 0x00;
	udh[1] = 0x03;
	udh[2] = concat_refno;
	udh[3] = nparts;
	common_init();
	prep_for_pdu_mode();
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=1", 0, 0);
	pos = 0;
	remain = msgtext_unilen;
	for (n = 1; n <= nparts; n++) {
		udh[4] = n;
		chunk = 67;
		if (chunk > remain)
			chunk = remain;
		send_pdu_ucs2(dest_addr, msgtext_uni + pos, chunk, udh, 5);
		pos += chunk;
		remain -= chunk;
	}
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=0", 0, 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	if (!concat_quiet)
		printf("Message sent as %u SMS segments\n", nparts);
	exit(0);
}

main(argc, argv)
	char **argv;
{
	if (!process_cmdline(argc, argv))
		read_msgtext_from_stdin();
	trim_trailing_newlines();
	if (ucs2_mode)
		ucs2_mode_main();
	else
		gsm7_mode_main();
}