changeset 874:f442156d31ad

ringtools: fc-ringlist-comp program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 29 Mar 2022 19:38:28 +0000
parents 2b5f4736079c
children 8ff9bce1b56e
files .hgignore ringtools/Makefile ringtools/fc-ringlist-comp.c
diffstat 3 files changed, 152 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Mar 29 07:49:05 2022 +0000
+++ b/.hgignore	Tue Mar 29 19:38:28 2022 +0000
@@ -51,6 +51,7 @@
 ^ringtools/fc-e1decode$
 ^ringtools/fc-e1gen$
 ^ringtools/fc-pwt-comp$
+^ringtools/fc-ringlist-comp$
 
 ^rvinterf/asyncshell/fc-shell$
 ^rvinterf/ctracedec/ctracedec$
--- a/ringtools/Makefile	Tue Mar 29 07:49:05 2022 +0000
+++ b/ringtools/Makefile	Tue Mar 29 19:38:28 2022 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	fc-e1decode fc-e1gen fc-pwt-comp
+PROGS=	fc-e1decode fc-e1gen fc-pwt-comp fc-ringlist-comp
 
 INSTALL_PREFIX=	/opt/freecalypso
 
@@ -17,6 +17,9 @@
 fc-pwt-comp:	fc-pwt-comp.c
 	${CC} ${CFLAGS} -o $@ $@.c
 
+fc-ringlist-comp:	fc-ringlist-comp.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
 install:
 	mkdir -p ${INSTBIN}
 	install -c ${PROGS} ${INSTBIN}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ringtools/fc-ringlist-comp.c	Tue Mar 29 19:38:28 2022 +0000
@@ -0,0 +1,147 @@
+/*
+ * 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);
+}