drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** This program checks for formatting problems in source code: |
| 3 | ** |
| 4 | ** * Any use of tab characters |
| 5 | ** * White space at the end of a line |
| 6 | ** * Blank lines at the end of a file |
| 7 | ** |
| 8 | ** Any violations are reported. |
| 9 | */ |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
drh | a6b0a9c | 2012-08-20 16:23:36 +0000 | [diff] [blame] | 14 | #define CR_OK 0x001 |
| 15 | #define WSEOL_OK 0x002 |
| 16 | |
| 17 | static void checkSpacing(const char *zFile, unsigned flags){ |
drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 18 | FILE *in = fopen(zFile, "rb"); |
| 19 | int i; |
| 20 | int seenSpace; |
| 21 | int seenTab; |
| 22 | int ln = 0; |
| 23 | int lastNonspace = 0; |
| 24 | char zLine[2000]; |
| 25 | if( in==0 ){ |
| 26 | printf("cannot open %s\n", zFile); |
| 27 | return; |
| 28 | } |
| 29 | while( fgets(zLine, sizeof(zLine), in) ){ |
| 30 | seenSpace = 0; |
| 31 | seenTab = 0; |
| 32 | ln++; |
| 33 | for(i=0; zLine[i]; i++){ |
| 34 | if( zLine[i]=='\t' && seenTab==0 ){ |
| 35 | printf("%s:%d: tab (\\t) character\n", zFile, ln); |
| 36 | seenTab = 1; |
| 37 | }else if( zLine[i]=='\r' ){ |
drh | a6b0a9c | 2012-08-20 16:23:36 +0000 | [diff] [blame] | 38 | if( (flags & CR_OK)==0 ){ |
drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 39 | printf("%s:%d: carriage-return (\\r) character\n", zFile, ln); |
| 40 | } |
| 41 | }else if( zLine[i]==' ' ){ |
| 42 | seenSpace = 1; |
| 43 | }else if( zLine[i]!='\n' ){ |
| 44 | lastNonspace = ln; |
| 45 | seenSpace = 0; |
| 46 | } |
| 47 | } |
drh | a6b0a9c | 2012-08-20 16:23:36 +0000 | [diff] [blame] | 48 | if( seenSpace && (flags & WSEOL_OK)==0 ){ |
drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 49 | printf("%s:%d: whitespace at end-of-line\n", zFile, ln); |
| 50 | } |
| 51 | } |
| 52 | fclose(in); |
| 53 | if( lastNonspace<ln ){ |
| 54 | printf("%s:%d: blank lines at end of file (%d)\n", |
| 55 | zFile, ln, ln - lastNonspace); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char **argv){ |
| 60 | int i; |
drh | a6b0a9c | 2012-08-20 16:23:36 +0000 | [diff] [blame] | 61 | unsigned flags = WSEOL_OK; |
drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 62 | for(i=1; i<argc; i++){ |
drh | a6b0a9c | 2012-08-20 16:23:36 +0000 | [diff] [blame] | 63 | const char *z = argv[i]; |
| 64 | if( z[0]=='-' ){ |
| 65 | while( z[0]=='-' ) z++; |
| 66 | if( strcmp(z,"crok")==0 ){ |
| 67 | flags |= CR_OK; |
| 68 | }else if( strcmp(z, "wseol")==0 ){ |
| 69 | flags &= ~WSEOL_OK; |
| 70 | }else if( strcmp(z, "help")==0 ){ |
| 71 | printf("Usage: %s [options] FILE ...\n", argv[0]); |
| 72 | printf(" --crok Do not report on carriage-returns\n"); |
| 73 | printf(" --wseol Complain about whitespace at end-of-line\n"); |
| 74 | printf(" --help This message\n"); |
| 75 | }else{ |
| 76 | printf("unknown command-line option: [%s]\n", argv[i]); |
| 77 | printf("use --help for additional information\n"); |
| 78 | } |
drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 79 | }else{ |
drh | a6b0a9c | 2012-08-20 16:23:36 +0000 | [diff] [blame] | 80 | checkSpacing(argv[i], flags); |
drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | return 0; |
| 84 | } |