# HG changeset patch # User Mychaela Falconia # Date 1615408443 0 # Node ID 0f96b513654131fcd434256494458cb9c301ca7f # Parent 208ae1633f6cc137d2dcdf7eb47dca9d8d29ec58 sws-email2db program written diff -r 208ae1633f6c -r 0f96b5136541 .hgignore --- a/.hgignore Wed Mar 10 19:39:33 2021 +0000 +++ b/.hgignore Wed Mar 10 20:34:03 2021 +0000 @@ -6,6 +6,7 @@ ^misc/fc-pcsc-list$ ^offline/sim-iccid-mkfull$ +^offline/sws-email2db$ ^simtool/fc-simtool$ diff -r 208ae1633f6c -r 0f96b5136541 offline/Makefile --- a/offline/Makefile Wed Mar 10 19:39:33 2021 +0000 +++ b/offline/Makefile Wed Mar 10 20:34:03 2021 +0000 @@ -1,6 +1,6 @@ CC= gcc CFLAGS= -O2 -PROGS= sim-iccid-mkfull +PROGS= sim-iccid-mkfull sws-email2db LIBS= ../libutil/libutil.a INSTBIN=/opt/freecalypso/bin @@ -9,6 +9,9 @@ sim-iccid-mkfull: sim-iccid-mkfull.o ${LIBS} ${CC} ${CFLAGS} -o $@ $@.o ${LIBS} +sws-email2db: sws-email2db.c + ${CC} ${CFLAGS} -o $@ $@.c + install: mkdir -p ${INSTBIN} install -c ${PROGS} ${INSTBIN} diff -r 208ae1633f6c -r 0f96b5136541 offline/sws-email2db.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/offline/sws-email2db.c Wed Mar 10 20:34:03 2021 +0000 @@ -0,0 +1,117 @@ +/* + * This program reads extracts from Sysmocom webshop emails containing + * card provisioning data and converts these bits into our sws-card-db + * format. + */ + +#include +#include +#include +#include + +#define MAX_FIELDS 32 +#define MAX_FIELD_KW 7 + +char *infname; +FILE *inf; +char linebuf[128]; +int lineno; + +char field_names[MAX_FIELDS][MAX_FIELD_KW+1]; +unsigned nfields; + +get_input_line() +{ + char *cp; + + if (!fgets(linebuf, sizeof linebuf, inf)) + return(0); + lineno++; + cp = index(linebuf, '\n'); + if (!cp) { + fprintf(stderr, "%s line %d: too long or missing newline\n", + infname, lineno); + exit(1); + } + *cp = '\0'; + return(1); +} + +read_field_names() +{ + for (;;) { + if (!get_input_line()) { + fprintf(stderr, "error: %s EOFs in field name list\n", + infname); + exit(1); + } + if (!linebuf[0]) + break; + if (nfields >= MAX_FIELDS) { + fprintf(stderr, "%s line %d: too many fields\n", + infname, lineno); + exit(1); + } + if (strlen(linebuf) > MAX_FIELD_KW) { + fprintf(stderr, "%s line %d: field name is too long\n", + infname, lineno); + exit(1); + } + strcpy(field_names[nfields], linebuf); + nfields++; + } + if (!nfields) { + fprintf(stderr, "error: %s header defines 0 fields\n", infname); + exit(1); + } +} + +process_one_card() +{ + unsigned nf; + int rc; + + for (nf = 0; nf < nfields; nf++) { + for (;;) { + rc = get_input_line(); + if (!rc) { + if (!nf) + exit(0); + fprintf(stderr, + "error: %s EOFs in the middle of a card data block\n", + infname); + exit(1); + } + if (linebuf[0]) + break; + if (nf) { + fprintf(stderr, + "%s line %d: empty line in the middle of a card data block\n", + infname, lineno); + exit(1); + } + } + if (nf) + putchar(' '); + printf("%s=%s", field_names[nf], linebuf); + } + putchar('\n'); +} + +main(argc, argv) + char **argv; +{ + if (argc != 2) { + fprintf(stderr, "usage: %s email-extract-file\n", argv[0]); + exit(1); + } + infname = argv[1]; + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + read_field_names(); + for (;;) + process_one_card(); +}