changeset 117:06e2d5c60cbd

fc-simtool: pb-update-imm command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Jan 2021 03:41:06 +0000
parents c8685074c845
children b563ff1c1a2a
files simtool/dispatch.c simtool/pbupdate.c
diffstat 2 files changed, 136 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/dispatch.c	Thu Jan 28 03:14:32 2021 +0000
+++ b/simtool/dispatch.c	Thu Jan 28 03:41:06 2021 +0000
@@ -22,6 +22,7 @@
 extern int cmd_pb_erase();
 extern int cmd_pb_erase_one();
 extern int cmd_pb_update();
+extern int cmd_pb_update_imm();
 extern int cmd_readbin();
 extern int cmd_readef();
 extern int cmd_readrec();
@@ -67,6 +68,7 @@
 	{"pb-erase", 1, 1, cmd_pb_erase},
 	{"pb-erase-one", 2, 2, cmd_pb_erase_one},
 	{"pb-update", 2, 2, cmd_pb_update},
+	{"pb-update-imm", 3, 4, cmd_pb_update_imm},
 	{"quit", 0, 0, cmd_exit},
 	{"readbin", 2, 2, cmd_readbin},
 	{"readef", 1, 1, cmd_readef},
--- a/simtool/pbupdate.c	Thu Jan 28 03:14:32 2021 +0000
+++ b/simtool/pbupdate.c	Thu Jan 28 03:41:06 2021 +0000
@@ -313,3 +313,137 @@
 	fclose(inf);
 	return(0);
 }
+
+static
+decode_number_arg(arg, fixp)
+	char *arg;
+	u_char *fixp;
+{
+	u_char digits[20];
+	unsigned ndigits, num_digit_bytes;
+	char *cp, *endp;
+	int c;
+
+	cp = arg;
+	if (*cp == '+') {
+		fixp[1] = 0x91;
+		cp++;
+	} else
+		fixp[1] = 0x81;
+	if (digit_char_to_gsm(*cp) < 0) {
+inv_arg:	fprintf(stderr, "error: invalid phone number argument\n");
+		return(-1);
+	}
+	for (ndigits = 0; ; ndigits++) {
+		c = digit_char_to_gsm(*cp);
+		if (c < 0)
+			break;
+		cp++;
+		if (ndigits >= 20) {
+			fprintf(stderr, "error: too many number digits\n");
+			return(-1);
+		}
+		digits[ndigits] = c;
+	}
+	if (ndigits & 1)
+		digits[ndigits++] = 0xF;
+	num_digit_bytes = ndigits >> 1;
+	fixp[0] = num_digit_bytes + 1;
+	pack_digit_bytes(digits, fixp + 2, num_digit_bytes);
+	if (*cp == ',') {
+		cp++;
+		if (!isdigit(*cp))
+			goto inv_arg;
+		fixp[1] = strtoul(cp, &endp, 0);
+		if (*endp)
+			goto inv_arg;
+	} else if (*cp)
+		goto inv_arg;
+	return(0);
+}
+
+static
+decode_alphatag_arg(arg, record)
+	char *arg;
+	u_char *record;
+{
+	unsigned maxlen, acclen, nadd;
+	char *cp;
+	int c;
+
+	maxlen = curfile_record_len - 14;
+	cp = arg;
+	for (acclen = 0; *cp; ) {
+		c = *cp++;
+		if (c == '\\') {
+			if (*cp == '\0') {
+				fprintf(stderr,
+					"error: danging backslash escape\n");
+				return(-1);
+			}
+			c = *cp++;
+			switch (c) {
+			case 'n':
+				c = '\n';
+				break;
+			case 'r':
+				c = '\r';
+				break;
+			case '"':
+			case '\\':
+				break;
+			default:
+				fprintf(stderr,
+				"error: non-understood backslash escape\n");
+				return(-1);
+			}
+		}
+		c = gsm7_encode_table[c];
+		if (c == 0xFF) {
+			fprintf(stderr,
+	"error: character in alpha tag string cannot be encoded in GSM7\n");
+			return(-1);
+		}
+		if (c & 0x80)
+			nadd = 2;
+		else
+			nadd = 1;
+		if (acclen + nadd > maxlen) {
+			fprintf(stderr,
+			"error: alpha tag string is longer than SIM limit\n");
+			return(-1);
+		}
+		if (c & 0x80)
+			record[acclen++] = 0x1B;
+		record[acclen++] = c & 0x7F;
+	}
+	return(0);
+}
+
+cmd_pb_update_imm(argc, argv)
+	char **argv;
+{
+	int rc;
+	unsigned recno;
+	u_char record[255], *fixp;
+
+	rc = phonebook_op_common(argv[1]);
+	if (rc < 0)
+		return(rc);
+	recno = strtoul(argv[2], 0, 0);
+	if (recno < 1 || recno > curfile_record_count) {
+		fprintf(stderr, "error: specified record number is invalid\n");
+		return(-1);
+	}
+	memset(record, 0xFF, curfile_record_len);
+	fixp = record + curfile_record_len - 14;
+	rc = decode_number_arg(argv[3], fixp);
+	if (rc < 0)
+		return(rc);
+	if (argv[4]) {
+		rc = decode_alphatag_arg(argv[4], record);
+		if (rc < 0)
+			return(rc);
+	}
+	return update_rec_op(recno, 0x04, record, curfile_record_len);
+}