view loadtools/buzplay.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents 684eddecbc62
children
line wrap: on
line source

/*
 * The actual functionality of fc-buzplay is implemented here.
 */

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

cmd_play(argc, argv)
	char **argv;
{
	FILE *f;
	char linebuf[256], *cp, *num1, *num2;
	int lineno;
	char *targv[4];
	u_long n1, n2, total_ms;
	int rc, timeout;

	f = fopen(argv[1], "r");
	if (!f) {
		perror(argv[1]);
		return(-1);
	}
	printf("Uploading the melody to the target\n");
	targv[0] = "I";
	targv[1] = 0;
	tpinterf_make_cmd(targv);
	if (tpinterf_send_cmd() < 0) {
		fclose(f);
		return(-1);
	}
	rc = tpinterf_pass_output(1);
	if (rc) {
		fclose(f);
		return(rc);
	}
	targv[0] = "E";
	targv[3] = 0;
	total_ms = 0;
	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
		cp = index(linebuf, '\n');
		if (!cp) {
			fprintf(stderr, "%s line %d: missing newline\n",
				argv[1], lineno);
			fclose(f);
			return(-1);
		}
		for (cp = linebuf; isspace(*cp); cp++)
			;
		if (*cp == '\0' || *cp == '#')
			continue;
		if (!isdigit(*cp)) {
inv:			fprintf(stderr, "%s line %d: unexpected content\n",
				argv[1], lineno);
			fclose(f);
			return(-1);
		}
		for (num1 = cp; *cp && !isspace(*cp); cp++)
			if (!isdigit(*cp))
				goto inv;
		if (isspace(*cp))
			*cp++ = '\0';
		while (isspace(*cp))
			cp++;
		if (!isdigit(*cp))
			goto inv;
		for (num2 = cp; *cp && !isspace(*cp); cp++)
			if (!isdigit(*cp))
				goto inv;
		if (isspace(*cp))
			*cp++ = '\0';
		while (isspace(*cp))
			cp++;
		if (*cp != '\0' && *cp != '#')
			goto inv;
		n1 = strtoul(num1, 0, 10);
		n2 = strtoul(num2, 0, 10);
		if (n1 > 255) {
			fprintf(stderr,
				"%s line %d: the tone number is out of range\n",
				argv[1], lineno);
			fclose(f);
			return(-1);
		}
		if (n2 < 1 || n2 > 0xFFFF) {
			fprintf(stderr,
			"%s line %d: the duration number is out of range\n",
				argv[1], lineno);
			fclose(f);
			return(-1);
		}
		/* send it to the target */
		targv[1] = num1;
		targv[2] = num2;
		tpinterf_make_cmd(targv);
		if (tpinterf_send_cmd() < 0) {
			fclose(f);
			return(-1);
		}
		rc = tpinterf_pass_output(1);
		if (rc) {
			fclose(f);
			return(rc);
		}
		/* account for the duration */
		total_ms += n2 * 5;
	}
	fclose(f);
	if (!total_ms) {
		fprintf(stderr, "%s is empty!\n", argv[1]);
		return(-1);
	}
	printf("Requesting play of the uploaded melody on the target\n");
	targv[0] = "P";
	targv[1] = 0;
	tpinterf_make_cmd(targv);
	if (tpinterf_send_cmd() < 0)
		return(-1);
	timeout = total_ms / 1000 + 2;
	return tpinterf_pass_output(timeout);
}