FreeCalypso > hg > ueda-linux
changeset 138:77acb816727b
netdiff: donl-rename-parts put together
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 07 Sep 2020 04:02:14 +0000 |
parents | 6f528e2a9e23 |
children | bf188727e606 |
files | .hgignore netdiff/renpart/Makefile netdiff/renpart/main.c netdiff/renpart/mainproc.c netdiff/renpart/rename.c netdiff/renpart/struct.h |
diffstat | 6 files changed, 252 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Mon Sep 07 03:20:50 2020 +0000 +++ b/.hgignore Mon Sep 07 04:02:14 2020 +0000 @@ -19,5 +19,6 @@ ^netdiff/convert/pads2donl$ ^netdiff/convert/protel2donl$ ^netdiff/convert/tedax2donl$ +^netdiff/renpart/donl-rename-parts$ ^pads2gpcb/pads2gpcb$
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netdiff/renpart/Makefile Mon Sep 07 04:02:14 2020 +0000 @@ -0,0 +1,16 @@ +CC= gcc +CFLAGS= -O2 +PROG= donl-rename-parts +OBJS= main.o mainproc.o rename.o +BINDIR= /usr/local/bin + +all: ${PROG} + +${PROG}: ${OBJS} + ${CC} ${CFLAGS} -o $@ ${OBJS} + +install: + install -c -o bin -g bin -m 755 ${PROG} ${BINDIR} + +clean: + rm -f *.[ao] a.out core errs ${PROG}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netdiff/renpart/main.c Mon Sep 07 04:02:14 2020 +0000 @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <stdlib.h> +#include "struct.h" + +struct part_rename *part_rename_list; + +main(argc, argv) + char **argv; +{ + int i; + + if (argc != 3) { + fprintf(stderr, "usage: %s input-donl rename-parts-file\n", + argv[0]); + exit(1); + } + process_rename_file(argv[2]); + main_process(argv[1]); + exit(0); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netdiff/renpart/mainproc.c Mon Sep 07 04:02:14 2020 +0000 @@ -0,0 +1,121 @@ +#include <ctype.h> +#include <string.h> +#include <strings.h> +#include <stdio.h> +#include <stdlib.h> +#include "struct.h" + +extern struct part_rename *part_rename_list; + +#define MAX_FIELDS 2 + +static char *infname; +static FILE *inf; +static char linebuf[512]; +static int lineno; +static char *fields[MAX_FIELDS]; +static unsigned nfields; + +static +get_line() +{ + if (!fgets(linebuf, sizeof linebuf, inf)) + return(0); + lineno++; + if (!index(linebuf, '\n')) { + fprintf(stderr, "%s line %d: missing newline\n", + infname, lineno); + exit(1); + } + return(1); +} + +static void +parse_into_fields() +{ + char *cp; + + nfields = 0; + for (cp = linebuf; ; ) { + while (isspace(*cp)) + cp++; + if (*cp == '\0' || *cp == '#') + break; + if (nfields >= MAX_FIELDS) { + fprintf(stderr, "%s line %d: too many fields\n", + infname, lineno); + exit(1); + } + fields[nfields++] = cp; + while (*cp) { + if (isspace(*cp)) { + *cp++ = '\0'; + break; + } + if (*cp++ != '\\') + continue; + switch (*cp++) { + case '\\': + case 'n': + case 'r': + case 't': + case ' ': + case '\t': + continue; + default: + fprintf(stderr, "%s line %d: invalid escape\n", + infname, lineno); + exit(1); + } + } + } +} + +static void +process_netpoint() +{ + char *cp, *refdes; + struct part_rename *rp; + + cp = index(fields[1], '.'); + if (!cp) { + fprintf(stderr, "%s line %d: expected '.' not found\n", + infname, lineno); + exit(1); + } + *cp++ = '\0'; + refdes = fields[1]; + for (rp = part_rename_list; rp; rp = rp->next) { + if (!strcmp(refdes, rp->old)) { + refdes = rp->new; + break; + } + } + printf("%s\t%s.%s\n", fields[0], refdes, cp); +} + +main_process(input_filename) + char *input_filename; +{ + infname = input_filename; + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + for (;;) { + if (!get_line()) + break; + parse_into_fields(); + if (!nfields) + continue; + if (nfields != 2) { + fprintf(stderr, "%s line %d: expected 2 fields\n", + infname, lineno); + exit(1); + } + process_netpoint(); + } + fclose(inf); + return(0); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netdiff/renpart/rename.c Mon Sep 07 04:02:14 2020 +0000 @@ -0,0 +1,89 @@ +#include <ctype.h> +#include <string.h> +#include <strings.h> +#include <stdio.h> +#include <stdlib.h> +#include "struct.h" + +extern struct part_rename *part_rename_list; + +static char *infname; +static FILE *inf; +static char linebuf[80]; +static int lineno; + +static void +rename_entry(old, new) + char *old, *new; +{ + struct part_rename *rp, **rpp; + char *dp; + + for (rpp = &part_rename_list; rp = *rpp; rpp = &rp->next) { + if (!strcmp(rp->old, old)) { + fprintf(stderr, "%s line %d: old name %s given twice\n", + infname, lineno, old); + exit(1); + } + } + rp = malloc(sizeof(struct part_rename) + strlen(old) + strlen(new) + 2); + dp = (char *)(rp + 1); + rp->old = dp; + strcpy(dp, old); + dp += strlen(old) + 1; + rp->new = dp; + strcpy(dp, new); + rp->next = 0; + *rpp = rp; +} + +static void +process_line() +{ + char *cp, *old, *new; + + for (cp = linebuf; isspace(*cp); cp++) + ; + if (*cp == '\0' || *cp == '#') + return; + for (old = cp; *cp && !isspace(*cp); cp++) + ; + if (*cp) + *cp++ = '\0'; + while (isspace(*cp)) + cp++; + if (*cp == '\0' || *cp == '#') { + fprintf(stderr, "%s line %d: too few fields\n", + infname, lineno); + exit(1); + } + for (new = cp; *cp && !isspace(*cp); cp++) + ; + if (*cp) + *cp++ = '\0'; + while (isspace(*cp)) + cp++; + if (*cp != '\0' && *cp != '#') { + fprintf(stderr, "%s line %d: too many fields\n", + infname, lineno); + exit(1); + } + rename_entry(old, new); +} + +process_rename_file(input_filename) + char *input_filename; +{ + infname = input_filename; + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + while (fgets(linebuf, sizeof linebuf, inf)) { + lineno++; + process_line(); + } + fclose(inf); + return(0); +}