view simtool/pbupd_imm.c @ 16:a6ca422323b9

simtool: saverestore.c split into savebin.c and restorebin.c
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 01:41:21 +0000
parents d4f8c511affe
children b8d27c72747a
line wrap: on
line source

/*
 * This module implements the pb-update-imm command.
 */

#include <sys/types.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include "curfile.h"

extern u_char gsm7_encode_table[256];

static
decode_alphatag_arg(arg, record, maxlen)
	char *arg;
	u_char *record;
	unsigned maxlen;
{
	unsigned acclen, nadd;
	char *cp;
	int c;

	cp = arg;
	for (acclen = 0; *cp; ) {
		c = *cp++;
		if (c == '\\') {
			if (*cp == '\0') {
				fprintf(stderr,
					"error: dangling 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 = encode_phone_number_arg(argv[3], fixp);
	if (rc < 0)
		return(rc);
	if (argv[4]) {
		rc = decode_alphatag_arg(argv[4], record,
					 curfile_record_len - 14);
		if (rc < 0)
			return(rc);
	}
	return update_rec_op(recno, 0x04, record, curfile_record_len);
}