1
0
mirror of git://projects.qi-hardware.com/ben-blinkenlights.git synced 2024-11-27 16:57:31 +02:00

ubb-patgen/ubb-patgen.c: accept file (- for standard input) as pattern source

This commit is contained in:
Werner Almesberger 2013-01-15 17:05:33 -03:00
parent 653910b64b
commit 69aba6ae19

View File

@ -14,6 +14,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
@ -194,6 +195,45 @@ static void *parse_pattern(const char *s, int *nibbles)
}
static const char *load_pattern(const char *s)
{
static char buf[20000]; /* more than enough :) */
FILE *file;
char *p = buf;
int comment = 0;
int c;
if (!strcmp(s, "-")) {
file = stdin;
} else {
file = fopen(s, "r");
if (!file)
return s;
}
while ((c = fgetc(file)) != EOF) {
if (comment) {
comment = c != '\n';
continue;
}
if (c == '#') {
comment = 1;
continue;
}
if (isspace(c))
continue;
if (buf+sizeof(buf)-1 == p) {
fprintf(stderr, "%s: file is too big\n", s);
exit(1);
}
*p++ = c;
}
if (file != stdin)
fclose(file);
*p = 0;
return buf;
}
/* ----- DMA control ------------------------------------------------------- */
@ -449,7 +489,7 @@ static void usage(const char *name)
"usage: %s\n"
" %s [-q] -f freq_hz\n"
" %s [-q] [-f freq_hz] -c [active_s]\n"
" %s [-q] [-f freq_hz] [-C] pattern\n\n"
" %s [-q] [-f freq_hz] [-C] file|pattern\n\n"
" -c output bus clock on CLK without sending a pattern\n"
" -C temporarily output bus clock on CLK (for debugging)\n"
" -f freq_hz set bus clock to the specified frequency (default: 1 MHz)\n"
@ -457,6 +497,7 @@ static void usage(const char *name)
" differences.\n\n"
" active_s keep running that many seconds after setting the clock\n"
" (default: exit immediately but leave the clock on)\n"
" file file containing the pattern\n"
" pattern send the specified pattern on DAT0 through DAT3\n\n"
"Frequency: the frequency in Hz, optionally followed by \"M\" or \"k\",\n"
" optionally followed by \"Hz\", optionally followed by \"+\" or \"-\".\n"
@ -538,6 +579,9 @@ int main(int argc, char **argv)
usage(*argv);
}
if (pattern)
pattern = load_pattern(pattern);
ubb_open(UBB_ALL);
PDFUNS = UBB_CMD;