FreeCalypso > hg > ueda-linux
comparison netdiff/flip2pin/mainproc.c @ 146:7ddfb9a67b0c
netdiff: donl-flip2pin utility written, compiles
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 15 Nov 2020 03:32:48 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 145:5e91200bf609 | 146:7ddfb9a67b0c |
|---|---|
| 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 flip_list *flip_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 && !isspace(*cp)) | |
| 51 cp++; | |
| 52 if (*cp) | |
| 53 *cp++ = '\0'; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 static void | |
| 58 process_netpoint() | |
| 59 { | |
| 60 char *cp, *refdes, *pin; | |
| 61 struct flip_list *rp; | |
| 62 | |
| 63 cp = index(fields[1], '.'); | |
| 64 if (!cp) { | |
| 65 fprintf(stderr, "%s line %d: expected '.' not found\n", | |
| 66 infname, lineno); | |
| 67 exit(1); | |
| 68 } | |
| 69 *cp++ = '\0'; | |
| 70 refdes = fields[1]; | |
| 71 pin = cp; | |
| 72 for (rp = flip_list; rp; rp = rp->next) { | |
| 73 if (!strcmp(refdes, rp->refdes)) | |
| 74 break; | |
| 75 } | |
| 76 if (rp) { | |
| 77 if (!strcmp(pin, "1")) | |
| 78 pin = "2"; | |
| 79 else if (!strcmp(pin, "2")) | |
| 80 pin = "1"; | |
| 81 else { | |
| 82 fprintf(stderr, | |
| 83 "%s line %d: flip pin number is not 1 or 2\n", | |
| 84 infname, lineno); | |
| 85 exit(1); | |
| 86 } | |
| 87 } | |
| 88 printf("%s\t%s.%s\n", fields[0], refdes, pin); | |
| 89 } | |
| 90 | |
| 91 main_process(input_filename) | |
| 92 char *input_filename; | |
| 93 { | |
| 94 infname = input_filename; | |
| 95 inf = fopen(infname, "r"); | |
| 96 if (!inf) { | |
| 97 perror(infname); | |
| 98 exit(1); | |
| 99 } | |
| 100 for (;;) { | |
| 101 if (!get_line()) | |
| 102 break; | |
| 103 parse_into_fields(); | |
| 104 if (!nfields) | |
| 105 continue; | |
| 106 if (nfields != 2) { | |
| 107 fprintf(stderr, "%s line %d: expected 2 fields\n", | |
| 108 infname, lineno); | |
| 109 exit(1); | |
| 110 } | |
| 111 process_netpoint(); | |
| 112 } | |
| 113 fclose(inf); | |
| 114 return(0); | |
| 115 } |
