FreeCalypso > hg > ueda-linux
diff netdiff/renpart/mainproc.c @ 138:77acb816727b
netdiff: donl-rename-parts put together
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 07 Sep 2020 04:02:14 +0000 |
parents | |
children | bf188727e606 |
line wrap: on
line diff
--- /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); +}