view smpp-send/input.c @ 267:81958b35f74d

NANP validation: allow made-up area codes of N9X form
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 13 Nov 2023 15:28:09 -0800
parents 62184b971c8e
children
line wrap: on
line source

/*
 * This module implements input handling for smpp-send.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "error.h"

extern char input_line[];
extern int input_lineno;

extern void set_src_addr();
extern void set_src_ton();
extern void set_src_npi();
extern void set_dest_addr();
extern void set_dest_ton();
extern void set_dest_npi();
extern void set_pid_byte();
extern void set_dcs_byte();
extern void set_esm_class();
extern void set_priority();
extern void set_reg_deliv();
extern void set_replace_flag();
extern void cmd_msg_plain();
extern void cmd_msg_udh();

static struct cmdtab {
	char *cmd;
	int minargs;
	int maxargs;
	void (*func)();
} cmdtab[] = {
	{"src", 1, 1, set_src_addr},
	{"src-ton", 1, 1, set_src_ton},
	{"src-npi", 1, 1, set_src_npi},
	{"dest", 1, 1, set_dest_addr},
	{"dest-ton", 1, 1, set_dest_ton},
	{"dest-npi", 1, 1, set_dest_npi},
	{"pid", 1, 1, set_pid_byte},
	{"dcs", 1, 2, set_dcs_byte},
	{"esm-class", 1, 1, set_esm_class},
	{"priority", 1, 1, set_priority},
	{"reg-deliv", 1, 1, set_reg_deliv},
	{"replace-flag", 1, 1, set_replace_flag},
	{"msg", 1, 1, cmd_msg_plain},
	{"msg-udh", 1, 1, cmd_msg_udh},
	/* table search terminator */
	{0, 0, 0, 0}
};

void
process_input_line()
{
	char *argv[10];
	char *cp, **ap;
	struct cmdtab *tp;

	cp = index(input_line, '\n');
	if (!cp) {
		fprintf(stderr, ERR_PREFIX "missing newline\n", input_lineno);
		exit(1);
	}
	for (cp = input_line; isspace(*cp); cp++)
		;
	if (*cp == '\0' || *cp == '#')
		return;
	argv[0] = cp;
	while (*cp && !isspace(*cp))
		cp++;
	if (*cp)
		*cp++ = '\0';
	for (tp = cmdtab; tp->cmd; tp++)
		if (!strcmp(tp->cmd, argv[0]))
			break;
	if (!tp->func) {
		fprintf(stderr, ERR_PREFIX "invalid command \"%s\"\n",
			input_lineno, argv[0]);
		exit(1);
	}
	for (ap = argv + 1; ; ) {
		while (isspace(*cp))
			cp++;
		if (!*cp || *cp == '#')
			break;
		if (ap - argv - 1 >= tp->maxargs) {
			fprintf(stderr, ERR_PREFIX "too many arguments\n",
				input_lineno);
			exit(1);
		}
		if (*cp == '"') {
			*ap++ = ++cp;
			for (;;) {
				if (!*cp) {
unterm_qstring:				fprintf(stderr, ERR_PREFIX
						"unterminated quoted string\n",
						input_lineno);
					exit(1);
				}
				if (*cp == '"')
					break;
				if (*cp++ == '\\') {
					if (!*cp)
						goto unterm_qstring;
					cp++;
				}
			}
			*cp++ = '\0';
		} else {
			*ap++ = cp;
			while (*cp && !isspace(*cp))
				cp++;
			if (*cp)
				*cp++ = '\0';
		}
	}
	if (ap - argv - 1 < tp->minargs) {
		fprintf(stderr, ERR_PREFIX "too few arguments\n", input_lineno);
		exit(1);
	}
	*ap = 0;
	tp->func(ap - argv, argv);
}