blob: ce38b08ce4cc163bb69720269399c3d9dfd2df68 [file] [log] [blame]
drh36ce6d22012-08-20 15:46:08 +00001/*
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
drha6b0a9c2012-08-20 16:23:36 +000014#define CR_OK 0x001
15#define WSEOL_OK 0x002
16
17static void checkSpacing(const char *zFile, unsigned flags){
drh36ce6d22012-08-20 15:46:08 +000018 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' ){
drha6b0a9c2012-08-20 16:23:36 +000038 if( (flags & CR_OK)==0 ){
drh36ce6d22012-08-20 15:46:08 +000039 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 }
drha6b0a9c2012-08-20 16:23:36 +000048 if( seenSpace && (flags & WSEOL_OK)==0 ){
drh36ce6d22012-08-20 15:46:08 +000049 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
59int main(int argc, char **argv){
60 int i;
drha6b0a9c2012-08-20 16:23:36 +000061 unsigned flags = WSEOL_OK;
drh36ce6d22012-08-20 15:46:08 +000062 for(i=1; i<argc; i++){
drha6b0a9c2012-08-20 16:23:36 +000063 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 }
drh36ce6d22012-08-20 15:46:08 +000079 }else{
drha6b0a9c2012-08-20 16:23:36 +000080 checkSpacing(argv[i], flags);
drh36ce6d22012-08-20 15:46:08 +000081 }
82 }
83 return 0;
84}