FreeCalypso > hg > ueda-linux
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 137:6f528e2a9e23 | 138:77acb816727b |
|---|---|
| 1 #include <ctype.h> | |
| 2 #include <string.h> | |
| 3 #include <strings.h> | |
| 4 #include <stdio.h> | |
| 5 #include <stdlib.h> | |
| 6 #include "struct.h" | |
| 7 | |
| 8 extern struct part_rename *part_rename_list; | |
| 9 | |
| 10 #define MAX_FIELDS 2 | |
| 11 | |
| 12 static char *infname; | |
| 13 static FILE *inf; | |
| 14 static char linebuf[512]; | |
| 15 static int lineno; | |
| 16 static char *fields[MAX_FIELDS]; | |
| 17 static unsigned nfields; | |
| 18 | |
| 19 static | |
| 20 get_line() | |
| 21 { | |
| 22 if (!fgets(linebuf, sizeof linebuf, inf)) | |
| 23 return(0); | |
| 24 lineno++; | |
| 25 if (!index(linebuf, '\n')) { | |
| 26 fprintf(stderr, "%s line %d: missing newline\n", | |
| 27 infname, lineno); | |
| 28 exit(1); | |
| 29 } | |
| 30 return(1); | |
| 31 } | |
| 32 | |
| 33 static void | |
| 34 parse_into_fields() | |
| 35 { | |
| 36 char *cp; | |
| 37 | |
| 38 nfields = 0; | |
| 39 for (cp = linebuf; ; ) { | |
| 40 while (isspace(*cp)) | |
| 41 cp++; | |
| 42 if (*cp == '\0' || *cp == '#') | |
| 43 break; | |
| 44 if (nfields >= MAX_FIELDS) { | |
| 45 fprintf(stderr, "%s line %d: too many fields\n", | |
| 46 infname, lineno); | |
| 47 exit(1); | |
| 48 } | |
| 49 fields[nfields++] = cp; | |
| 50 while (*cp) { | |
| 51 if (isspace(*cp)) { | |
| 52 *cp++ = '\0'; | |
| 53 break; | |
| 54 } | |
| 55 if (*cp++ != '\\') | |
| 56 continue; | |
| 57 switch (*cp++) { | |
| 58 case '\\': | |
| 59 case 'n': | |
| 60 case 'r': | |
| 61 case 't': | |
| 62 case ' ': | |
| 63 case '\t': | |
| 64 continue; | |
| 65 default: | |
| 66 fprintf(stderr, "%s line %d: invalid escape\n", | |
| 67 infname, lineno); | |
| 68 exit(1); | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 static void | |
| 75 process_netpoint() | |
| 76 { | |
| 77 char *cp, *refdes; | |
| 78 struct part_rename *rp; | |
| 79 | |
| 80 cp = index(fields[1], '.'); | |
| 81 if (!cp) { | |
| 82 fprintf(stderr, "%s line %d: expected '.' not found\n", | |
| 83 infname, lineno); | |
| 84 exit(1); | |
| 85 } | |
| 86 *cp++ = '\0'; | |
| 87 refdes = fields[1]; | |
| 88 for (rp = part_rename_list; rp; rp = rp->next) { | |
| 89 if (!strcmp(refdes, rp->old)) { | |
| 90 refdes = rp->new; | |
| 91 break; | |
| 92 } | |
| 93 } | |
| 94 printf("%s\t%s.%s\n", fields[0], refdes, cp); | |
| 95 } | |
| 96 | |
| 97 main_process(input_filename) | |
| 98 char *input_filename; | |
| 99 { | |
| 100 infname = input_filename; | |
| 101 inf = fopen(infname, "r"); | |
| 102 if (!inf) { | |
| 103 perror(infname); | |
| 104 exit(1); | |
| 105 } | |
| 106 for (;;) { | |
| 107 if (!get_line()) | |
| 108 break; | |
| 109 parse_into_fields(); | |
| 110 if (!nfields) | |
| 111 continue; | |
| 112 if (nfields != 2) { | |
| 113 fprintf(stderr, "%s line %d: expected 2 fields\n", | |
| 114 infname, lineno); | |
| 115 exit(1); | |
| 116 } | |
| 117 process_netpoint(); | |
| 118 } | |
| 119 fclose(inf); | |
| 120 return(0); | |
| 121 } |
