|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +const char * yes[] = { "CH32V003" }; |
| 7 | +const char * no[] = { "CH32V20x", "CH32V30x", "CH32X03x", "CH32V10x" }; |
| 8 | + |
| 9 | +char * WhitePull( const char ** sti ) |
| 10 | +{ |
| 11 | + const char * st = *sti; |
| 12 | + int len = 0; |
| 13 | + while( ( *st == ' ' || *st == '\t' || *st == '(' ) && *st ) { st++; } |
| 14 | + const char * sts = st; |
| 15 | + while( *st != ' ' && *st != '\t' && *st != '\n' && *st != ')' && *st != '(' && *st != 0 ) { st++; len++; } |
| 16 | + if( *st == ')' ) { st++; } |
| 17 | + char * ret = malloc( len + 1 ); |
| 18 | + memcpy( ret, sts, len ); |
| 19 | + ret[len] = 0; |
| 20 | + *sti = st; |
| 21 | + return ret; |
| 22 | +} |
| 23 | + |
| 24 | +int NYI( const char * s ) |
| 25 | +{ |
| 26 | + int ret = 2; |
| 27 | + char * wp = WhitePull( &s ); |
| 28 | + int i; |
| 29 | + for( i = 0; i < sizeof(yes)/sizeof(yes[0]); i++ ) |
| 30 | + if( strcmp( yes[i], wp ) == 0 ) ret = 1; |
| 31 | + for( i = 0; i < sizeof(no)/sizeof(no[0]); i++ ) |
| 32 | + if( strcmp( no[i], wp ) == 0 ) ret = 0; |
| 33 | + free( wp ); |
| 34 | + return ret; |
| 35 | +} |
| 36 | + |
| 37 | +int EvalSpec( const char * spl ) |
| 38 | +{ |
| 39 | + int rsofar = 0; |
| 40 | + int i; |
| 41 | + int lastv = 0; |
| 42 | + int lasto = -1; |
| 43 | + int ret = 0; |
| 44 | + |
| 45 | + char * wp = WhitePull( &spl ); |
| 46 | + int def = -1; |
| 47 | + if( strcmp( wp, "defined" ) == 0 ) def = 1; |
| 48 | + if( strcmp( wp, "!defined" ) == 0 ) def = 2; |
| 49 | + if( def < 0 ) return 2; |
| 50 | + char * wpn = WhitePull( &spl ); |
| 51 | + i = NYI( wpn ); |
| 52 | +//printf( "SPIN: %s/%s/%d/%d\n", wp, wpn, i, def ); |
| 53 | + if( i == 2 ) return 2; |
| 54 | + |
| 55 | + if( def == 2 ) i = !i; |
| 56 | + |
| 57 | + if( lasto == 1 ) |
| 58 | + ret = lastv || i; |
| 59 | + if( lasto == 2 ) |
| 60 | + ret = lastv && i; |
| 61 | + else |
| 62 | + ret = i; |
| 63 | + |
| 64 | + char * wpa = WhitePull( &spl ); |
| 65 | + lastv = ret; |
| 66 | + lasto = -1; |
| 67 | + if( strcmp( wpa, "||" ) ) { lasto = 1; } |
| 68 | + else if( strcmp( wpa, "&&" ) ) { lasto = 2; } |
| 69 | + else return ret; |
| 70 | +} |
| 71 | + |
| 72 | +// 0 for no |
| 73 | +// 1 for yes |
| 74 | +// 2 for indeterminate |
| 75 | +int NoYesInd( const char * preprocc ) |
| 76 | +{ |
| 77 | + int ret; |
| 78 | + int ofs = 0; |
| 79 | + if( strncmp( preprocc, "#if ", 4 ) == 0 ) ofs = 4; |
| 80 | + if( strncmp( preprocc, "#elif ", 6 ) == 0 ) ofs = 6; |
| 81 | + if( ofs ) |
| 82 | + { |
| 83 | + ret = EvalSpec( preprocc + ofs ); |
| 84 | + //printf( "SPEC: %d\n", ret ); |
| 85 | + } |
| 86 | + else if( strncmp( preprocc, "#ifdef ", 7 ) == 0 ) |
| 87 | + { |
| 88 | + char * ep = preprocc + 6; |
| 89 | + char * wp = WhitePull( &ep ); |
| 90 | + ret = NYI( wp ); |
| 91 | + free( wp ); |
| 92 | + } |
| 93 | + else if( strncmp( preprocc, "#ifndef ", 8 ) == 0 ) |
| 94 | + { |
| 95 | + char * ep = preprocc + 6; |
| 96 | + char * wp = WhitePull( &ep ); |
| 97 | + ret = NYI( wp ); |
| 98 | + if( ret < 2 ) ret = !ret; |
| 99 | + free( wp ); |
| 100 | + } |
| 101 | + else |
| 102 | + ret = 2; |
| 103 | + //printf( "%d-> %s\n", ret, preprocc ); |
| 104 | + return ret; |
| 105 | +} |
| 106 | + |
| 107 | +const char * sslineis( const char * line, const char * match ) |
| 108 | +{ |
| 109 | + while( *line == ' ' || *line == '\t' ) line++; |
| 110 | + const char * linestart = line; |
| 111 | + while( *line && *match == *line ) { line++; match++; } |
| 112 | + if( *match == 0 ) |
| 113 | + return linestart; |
| 114 | + else |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +int main() |
| 119 | +{ |
| 120 | + FILE * f = fopen( "hardware.c", "r" ); |
| 121 | + char line[1024]; |
| 122 | + char * l; |
| 123 | + |
| 124 | + |
| 125 | + int depth = 0; |
| 126 | + |
| 127 | + // 0 = no |
| 128 | + // 1 = yes |
| 129 | + // 2 = indeterminate |
| 130 | + // 3 = super no. (I.e. after a true #if clause) |
| 131 | + int yesnoind[1024]; |
| 132 | + yesnoind[0] = 1; |
| 133 | + |
| 134 | + while( l = fgets( line, sizeof(line)-1, f ) ) |
| 135 | + { |
| 136 | + char * ss = 0; |
| 137 | + int nyi = yesnoind[depth]; |
| 138 | + int waspre = 0; |
| 139 | + |
| 140 | + if( (ss = sslineis( line, "#if " ) ) || (ss = sslineis( line, "#ifdef " ) ) || (ss = sslineis( line, "#ifndef " ) ) ) |
| 141 | + { |
| 142 | + waspre = 1; |
| 143 | + //printf( "CHECK: %d/%s\n", depth, l ); |
| 144 | + nyi = NoYesInd( ss ); |
| 145 | + depth++; |
| 146 | + yesnoind[depth] = nyi; |
| 147 | + } |
| 148 | + else if( (ss = sslineis( line, "#elif " ) ) ) |
| 149 | + { |
| 150 | + waspre = 1; |
| 151 | + if( nyi == 1 ) |
| 152 | + { |
| 153 | + nyi = 3; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + nyi = NoYesInd( ss ); |
| 158 | + } |
| 159 | + //printf( "ELIF check: %s %d\n", ss, nyi ); |
| 160 | + yesnoind[depth] = nyi; |
| 161 | + } |
| 162 | + else if( (ss = sslineis( line, "#else" ) ) ) |
| 163 | + { |
| 164 | + waspre = 1; |
| 165 | + if( yesnoind[depth] == 1 ) |
| 166 | + nyi = 3; |
| 167 | + else |
| 168 | + nyi = !yesnoind[depth]; |
| 169 | + yesnoind[depth] = nyi; |
| 170 | + } |
| 171 | + else if( (ss = sslineis( line, "#endif" ) ) ) |
| 172 | + { |
| 173 | + waspre = 1; |
| 174 | + depth--; |
| 175 | + } |
| 176 | + |
| 177 | + int thisv = nyi; |
| 178 | + int i; |
| 179 | + for( i = 0; i <= depth; i++ ) |
| 180 | + { |
| 181 | + //printf( "%d", yesnoind[i] ); |
| 182 | + if( yesnoind[i] == 0 || yesnoind[i] == 3 ) thisv = 0; |
| 183 | + } |
| 184 | + //printf( ">>%s", l ); |
| 185 | + |
| 186 | + if( thisv != 0 && thisv != 3 && ( thisv != 1 || !waspre ) ) |
| 187 | + { |
| 188 | + printf( "%s", l ); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | + |
0 commit comments