view ringtools/fc-ringlist-comp.c @ 967:6bf473f77fc4

fcup-smsend: support backslash escapes in UCS-2 mode too
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 01 Sep 2023 16:43:35 +0000
parents f442156d31ad
children
line wrap: on
line source

/*
 * This program compiles a list of ringing tone or message alert tone
 * melodies from ASCII source into the binary format (.mls) that will
 * be uploaded into FreeCalypso device FFS and read by the UI layer
 * of FC phone firmwares.
 */

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

#define	MAX_FFS_PATHNAME	19
#define	MAX_FFS_PREFIX		5
#define	MAX_NAME_FIELD		59

struct bin_record {
	char	ffs_pathname[MAX_FFS_PATHNAME+1];
	char	ui_name[MAX_NAME_FIELD+1];
};

char *infname, *outfname;
FILE *inf, *outf;
char ffs_prefix[MAX_FFS_PREFIX+2];
unsigned ffs_prefix_len;
char linebuf[256];
int lineno;

set_ffs_prefix(prefix_arg)
	char *prefix_arg;
{
	char *cp;

	if (prefix_arg[0] != '/') {
		fprintf(stderr,
			"error: given FFS prefix does not begin with \'/\'\n");
		exit(1);
	}
	ffs_prefix_len = strlen(prefix_arg);
	if (ffs_prefix_len > MAX_FFS_PREFIX) {
		fprintf(stderr, "error: given FFS prefix is too long\n");
		exit(1);
	}
	strcpy(ffs_prefix, prefix_arg);
	cp = ffs_prefix + ffs_prefix_len;
	if (cp[-1] != '/') {
		cp[0] = '/';
		cp[1] = '\0';
		ffs_prefix_len++;
	}
}

enforce_and_strip_newline()
{
	char *cp;

	cp = index(linebuf, '\n');
	if (!cp) {
		fprintf(stderr, "%s line %d: too long or missing newline\n",
			infname, lineno);
		exit(1);
	}
	*cp = '\0';
}

emit_record(fname, uname)
	char *fname, *uname;
{
	struct bin_record rec;

	strcpy(rec.ffs_pathname, ffs_prefix);
	strncpy(rec.ffs_pathname + ffs_prefix_len, fname,
		MAX_FFS_PATHNAME + 1 - ffs_prefix_len);
	strncpy(rec.ui_name, uname, MAX_NAME_FIELD + 1);
	fwrite(&rec, sizeof rec, 1, outf);
}

process_line()
{
	char *cp, *fname, *uname;

	cp = linebuf;
	while (isspace(*cp))
		cp++;
	if (*cp == '\0' || *cp == '#')
		return;
	fname = cp;
	while (*cp && !isspace(*cp))
		cp++;
	if (!*cp) {
inv:		fprintf(stderr, "%s line %d: invalid syntax\n",
			infname, lineno);
		exit(1);
	}
	*cp++ = '\0';
	while (isspace(*cp))
		cp++;
	if (*cp == '\0' || *cp == '#')
		goto inv;
	uname = cp;
	if (strlen(fname) + ffs_prefix_len > MAX_FFS_PATHNAME) {
		fprintf(stderr, "%s line %d: melody filename exceeds limit\n",
			infname, lineno);
		exit(1);
	}
	if (strlen(uname) > MAX_NAME_FIELD) {
		fprintf(stderr, "%s line %d: melody UI name exceeds limit\n",
			infname, lineno);
		exit(1);
	}
	emit_record(fname, uname);
}

main(argc, argv)
	char **argv;
{
	if (argc != 4) {
		fprintf(stderr, "usage: %s src-file bin-file ffs-prefix\n",
			argv[0]);
		exit(1);
	}
	if (strcmp(argv[1], "-")) {
		infname = argv[1];
		inf = fopen(infname, "r");
		if (!inf) {
			perror(infname);
			exit(1);
		}
	} else {
		infname = "stdin";
		inf = stdin;
	}
	set_ffs_prefix(argv[3]);
	outfname = argv[2];
	outf = fopen(outfname, "w");
	if (!outf) {
		perror(outfname);
		exit(1);
	}

	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
		enforce_and_strip_newline();
		process_line();
	}
	exit(0);
}