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

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

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

int sms_write_mode, text_mode, utf8_input, ucs2_mode;
u_char dest_addr[12];
int dest_addr_global;
char input_line[21+5+1+320+2], *msgtext;
int lineno;
int initdone;

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

	while ((c = getopt(argc, argv, "B:np:RtuUwWX:")) != EOF) {
		if (atinterf_cmdline_opt(c))
			continue;
		switch (c) {
		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;
			continue;
		default:
			/* error msg already printed */
			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 + 1) {
		fprintf(stderr, "usage: %s [options] [dest-addr]\n",
			argv[0]);
		exit(ERROR_USAGE);
	}
	if (argv[optind]) {
		dest_addr_global = 1;
		if (parse_and_encode_dest_addr(argv[optind], dest_addr) < 0) {
			fprintf(stderr,
			"error: destination address argument is invalid\n");
			exit(ERROR_USAGE);
		}
	} else
		dest_addr_global = 0;
	return(0);
}

preen_input_line()
{
	char *cp;

	cp = index(input_line, '\n');
	if (!cp) {
		fprintf(stderr, "input line %d: too long or unterminated\n",
			lineno);
		exit(ERROR_USAGE);
	}
	*cp = '\0';
	if (dest_addr_global) {
		msgtext = input_line;
		return(0);
	}
	if (!input_line[0] || isspace(input_line[0])) {
inv:		fprintf(stderr, "input line %d: invalid syntax\n", lineno);
		exit(ERROR_USAGE);
	}
	for (cp = input_line; *cp && !isspace(*cp); cp++)
		;
	if (!*cp)
		goto inv;
	*cp++ = '\0';
	if (parse_and_encode_dest_addr(input_line, dest_addr) < 0)
		goto inv;
	while (isspace(*cp))
		cp++;
	msgtext = cp;
	return(1);
}

init_send_process()
{
	if (initdone)
		return(0);
	atinterf_init();
	/* enable verbose error messages */
	atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0);
	if (text_mode)
		prep_for_text_mode();
	else
		prep_for_pdu_mode();
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=1", 0, 0);
	initdone = 1;
	return(1);
}

process_msg_gsm7()
{
	u_char msgtext_gsm7[160];
	unsigned msgtext_gsmlen;
	int rc;

	if (utf8_input && utf8_to_latin1(msgtext) < 0) {
		fprintf(stderr, "input line %d: invalid UTF-8 message\n",
			lineno);
		exit(ERROR_USAGE);
	}
	if (text_mode) {
		if (strlen(msgtext) > 160) {
toolong:		fprintf(stderr,
				"input line %d: message exceeds 160 chars\n",
				lineno);
			exit(ERROR_USAGE);
		}
		init_send_process();
		send_in_text_mode(dest_addr, msgtext);
		return(0);
	}
	rc = latin1_to_gsm7(msgtext, msgtext_gsm7, 160, &msgtext_gsmlen);
	if (rc == -1) {
		fprintf(stderr,
			"input line %d: message not valid for GSM7 charset\n",
			lineno);
		exit(ERROR_USAGE);
	}
	if (rc == -2)
		goto toolong;
	init_send_process();
	send_in_pdu_mode(dest_addr, msgtext_gsm7, msgtext_gsmlen, 0, 0);
	return(0);
}

process_msg_ucs2()
{
	u_short msgtext_uni[70];
	unsigned msgtext_unilen;
	int rc;

	rc = utf8_to_ucs2(msgtext, msgtext_uni, 70, &msgtext_unilen);
	if (rc == -1) {
		fprintf(stderr, "input line %d: invalid UTF-8 message\n",
			lineno);
		exit(ERROR_USAGE);
	}
	if (rc == -2) {
		fprintf(stderr,
			"input line %d: message exceeds 70 UCS-2 chars\n",
			lineno);
		exit(ERROR_USAGE);
	}
	init_send_process();
	send_pdu_ucs2(dest_addr, msgtext_uni, msgtext_unilen, 0, 0);
	return(0);
}

main(argc, argv)
	char **argv;
{
	process_cmdline(argc, argv);
	for (lineno = 1; fgets(input_line, sizeof input_line, stdin); lineno++){
		preen_input_line();
		if (ucs2_mode)
			process_msg_ucs2();
		else
			process_msg_gsm7();
	}
	if (!initdone)
		exit(0);
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=0", 0, 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	exit(0);
}