return(ptr);
}
+char* xstrdup(const char* s)
+{
+ char* ptr = strdup(s);
+ if (ptr == NULL)
+ {
+ fprintf(stderr, "Out of memory!\n");
+ exit(EXIT_FAILURE);
+ }
+ return(ptr);
+}
+
void packed_to_token(long packed, char token[6])
{
+ // Unpack and map back to ASCII.
for (int i = 0; i < 5; ++i)
{
char advent = (packed >> i * 6) & 63;
- token[i] = advent_to_ascii[advent];
+ token[4 - i] = advent_to_ascii[advent];
}
+
+ // Ensure the last character is \0.
token[5] = '\0';
+
+ // Replace trailing whitespace with \0.
+ for (int i = 4; i >= 0; --i)
+ {
+ if (token[i] == ' ' || token[i] == '\t')
+ token[i] = '\0';
+ else
+ break;
+ }
}
/* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
printf("\n");
// Create a copy of our string, so we can edit it.
- char* copy = (char*) xmalloc(strlen(msg) + 1);
- strncpy(copy, msg, strlen(msg) + 1);
+ char* copy = xstrdup(msg);
// Staging area for stringified parameters.
char parameters[5][100]; // FIXME: to be replaced with dynamic allocation
if (msg[i + 1] == 'd')
{
copy[i + 1] = 's';
- sprintf(parameters[pi], "%d", PARMS[pi]);
+ sprintf(parameters[pi], "%ld", PARMS[pi]);
}
// Unmodified string specifier.
// Render the final string.
char rendered[2000]; // FIXME: to be replaced with dynamic allocation
- sprintf(&rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]); // FIXME: to be replaced with vsprintf()
+ sprintf(rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]); // FIXME: to be replaced with vsprintf()
// Print the message.
printf("%s\n", rendered);
* and is not changed thereafter unless the routines on this page choose
* to do so. */
- if (!oldstyle && !isatty(1))
+ if (prompt)
fputs("> ", stdout);
do {
- if (oldstyle) {
+ if (!editline) {
IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
eof = (feof(fp));
} else {
- char *cp = linenoise("> ");
+ char *cp = linenoise(prompt ? "> ": "");
eof = (cp == NULL);
if (!eof) {
strncpy(rawbuf, cp, sizeof(rawbuf)-1);
*t = (long) tv.tv_usec;
}
-long MOD(long n, long m)
-{
- return(n%m);
-}
+/* end */