-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstylecheck.c
137 lines (130 loc) · 2.76 KB
/
stylecheck.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <stdlib.h>
static void
check_file(int *had_err, const char *fn, int dos, int trailing, int firstspace, int maxlen)
{
FILE *fp;
int ln_nbr;
char ln[4096];
int msgs = 0;
int max_msgs = 20;
int length;
int position = 2;
int pos;
if ((fp = fopen (fn, "r")) == NULL) {
perror (fn);
*had_err = 1;
return;
}
ln_nbr = 1;
while (fgets (ln, sizeof(ln), fp) != NULL) {
length = strlen(ln);
if(ln[length-1] != '\n') {
fprintf(stderr, "error: %s:%d: line is missing LF:\n%s\n", fn, ln_nbr, ln);
*had_err = 1;
++msgs;
continue;
}
/* max line lenght */
pos = 0;
for(int i=0;i<length-1;i++) {
++pos;
if(ln[i]=='\t') {
pos += (8 - pos % 8) % 8;
}
}
if(pos > maxlen) {
fprintf(stderr, "error: %s:%d: line too long (%d):\n%s\n",
fn, ln_nbr, pos, ln);
*had_err = 1;
++msgs;
}
/* space at the beginning of line */
if(firstspace) {
if (ln[0] == ' ' && ln[1] != '*') {
fprintf(stderr, "error: %s:%d: invalid indention (SP at column 1):"
"\n%s\n", fn, ln_nbr, ln);
*had_err = 1;
++msgs;
}
}
/* whitespace at the end of the line */
if(trailing) {
if(dos) {
if(length > position && ln[length-1]=='\n' && ln[length-2]=='\r') {
position = 3;
}
else {
position = 2;
}
}
if(length > position && isspace(ln[length-position])) {
fprintf(stderr, "error: %s:%d: trailing whitespace at end of line:"
"\n%s\n", fn, ln_nbr, ln);
*had_err = 1;
++msgs;
}
}
/* line number + msg limit */
++ln_nbr;
if(msgs == max_msgs) {
fprintf(stderr, "reached max %d messages, not reporting more\n",
max_msgs);
goto done;
}
}
done:
fclose (fp);
}
int
main(int argc, char *argv[])
{
int had_err = 0;
int opt = 0;
int dos = 0;
int trailing = 1;
int firstspace = 1;
int maxlen = 120;
char *ignore = NULL;
static struct option long_options[] = {
{"ignore", required_argument, 0, 'i'},
{"permit-dos-format", no_argument, 0, 'd'},
{"disable-trailing-whitespace", no_argument, 0, 'w'},
{"disable-first-space", no_argument, 0, 'f'},
{"set-maxlength", required_argument, 0, 'l'},
{0, 0, 0, 0}
};
int long_index = 0;
while((opt = getopt_long(argc, argv, "i:dwfl:", long_options, &long_index)) != -1) {
switch(opt) {
case 'i':
ignore = optarg;
break;
case 'd':
dos = 1;
break;
case 'w':
trailing = 0;
break;
case 'f':
firstspace = 0;
break;
case 'l':
maxlen = atoi(optarg);
break;
default:
break;
}
}
for(int i = optind; i < argc ; ++i) {
if(ignore == NULL || strcmp(basename(argv[i]), ignore) != 0) {
check_file(&had_err, argv[i], dos, trailing, firstspace, maxlen);
}
}
return had_err;
}