# HG changeset patch # User Mychaela Falconia # Date 1600569623 0 # Node ID ffadaa339478dc4a9fec81d57d696d3e67d7ed36 # Parent 7c0fd80782c8d9b43d6749a51aa0a85fda32e96b protel-parts-condense misc utility written diff -r 7c0fd80782c8 -r ffadaa339478 .hgignore --- a/.hgignore Sat Sep 19 23:46:46 2020 +0000 +++ b/.hgignore Sun Sep 20 02:40:23 2020 +0000 @@ -18,6 +18,8 @@ ^ueda/utils/cutelements$ ^ueda/utils/instfileelem$ +^miscutil/protel-parts-condense$ + ^netdiff/convert/pads2donl$ ^netdiff/convert/protel2donl$ ^netdiff/convert/tedax2donl$ diff -r 7c0fd80782c8 -r ffadaa339478 miscutil/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/miscutil/Makefile Sun Sep 20 02:40:23 2020 +0000 @@ -0,0 +1,17 @@ +CC= gcc +CFLAGS= -O2 +PROGS= protel-parts-condense +BINDIR= /usr/local/bin + +all: ${PROGS} + +${PROGS}: + ${CC} ${CFLAGS} -o $@ $@.c + +install: + install -c -o bin -g bin -m 755 ${PROGS} ${BINDIR} + +clean: + rm -f *.[ao] a.out core errs ${PROGS} + +protel-parts-condense: protel-parts-condense.c diff -r 7c0fd80782c8 -r ffadaa339478 miscutil/protel-parts-condense.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/miscutil/protel-parts-condense.c Sun Sep 20 02:40:23 2020 +0000 @@ -0,0 +1,202 @@ +/* + * This program reads a Protel netlist (exported from Altium), extracts + * the component list portion and emits it in a condensed form. + */ + +#include +#include +#include +#include + +static char *infname; +static FILE *inf; +static char linebuf[80], savebuf[6][80]; +static int lineno; + +static +get_line() +{ + char *cp; + + if (!fgets(linebuf, sizeof linebuf, inf)) + return(0); + lineno++; + cp = index(linebuf, '\n'); + if (!cp) { + fprintf(stderr, "%s line %d: missing newline\n", + infname, lineno); + exit(1); + } + *cp = '\0'; + if (cp > linebuf && cp[-1] == '\r') + *--cp = '\0'; + return(1); +} + +static void +get_line_notfirst() +{ + if (!get_line()) { + fprintf(stderr, "error: unexpected EOF in input\n"); + exit(1); + } + if (!strcmp(linebuf, "[")) { + fprintf(stderr, "%s line %d: [ NOT expected\n", + infname, lineno); + exit(1); + } + if (!strcmp(linebuf, "(")) { + fprintf(stderr, "%s line %d: ( NOT expected\n", + infname, lineno); + exit(1); + } +} + +static void +check_line_for_nonprint_chars() +{ + char *cp; + int c; + + for (cp = linebuf; *cp; cp++) { + c = *cp; + if ((c < ' ' || c > '~') && c != '\t') { + fprintf(stderr, "%s line %d: non-printable char\n", + infname, lineno); + exit(1); + } + } +} + +static int +item_needs_quoting(str) + char *str; +{ + char *cp; + int c; + + if (!str[0]) + return(1); + for (cp = str; *cp; cp++) { + c = *cp; + if (c == '\t' || c == ' ' || c == '\"' || c == '\\') + return(1); + } + return(0); +} + +static void +print_item_quoted(str) + char *str; +{ + char *cp; + int c; + + putchar('\"'); + for (cp = str; *cp; cp++) { + c = *cp; + if (c == '\t') { + putchar('\\'); + putchar('t'); + continue; + } + if (c == '\"' || c == '\\') + putchar('\\'); + putchar(c); + } + putchar('\"'); +} + +static void +print_item(str) + char *str; +{ + if (item_needs_quoting(str)) + print_item_quoted(str); + else + fputs(str, stdout); +} + +static void +process_component_block() +{ + int i; + + for (i = 0; i < 6; i++) { + get_line_notfirst(); + if (!strcmp(linebuf, "]")) { + fprintf(stderr, "%s line %d: ] NOT expected\n", + infname, lineno); + exit(1); + } + if (!strcmp(linebuf, ")")) { + fprintf(stderr, "%s line %d: ) NOT expected\n", + infname, lineno); + exit(1); + } + check_line_for_nonprint_chars(); + strcpy(savebuf[i], linebuf); + } + get_line_notfirst(); + if (strcmp(linebuf, "]")) { + fprintf(stderr, "%s line %d: expected ]\n", infname, lineno); + exit(1); + } + print_item(savebuf[0]); + putchar(' '); + print_item(savebuf[1]); + putchar(' '); + print_item(savebuf[2]); + putchar(' '); + print_item(savebuf[3]); + putchar(' '); + print_item(savebuf[4]); + putchar(' '); + print_item(savebuf[5]); + putchar('\n'); +} + +static void +skip_net_block() +{ + for (;;) { + get_line_notfirst(); + if (!strcmp(linebuf, "]")) { + fprintf(stderr, "%s line %d: ] NOT expected\n", + infname, lineno); + exit(1); + } + if (!strcmp(linebuf, ")")) + break; + } +} + +main(argc, argv) + char **argv; +{ + if (argc != 2) { + fprintf(stderr, "usage: %s protel-netlist-file\n", argv[0]); + exit(1); + } + infname = argv[1]; + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + for (;;) { + if (!get_line()) + break; + if (!strcmp(linebuf, "[")) + process_component_block(); + else if (!strcmp(linebuf, "(")) + skip_net_block(); + else { + fprintf(stderr, + "%s line %d: expected beginning of block\n", + infname, lineno); + exit(1); + } + } + exit(0); +}