]> mj.ucw.cz Git - eval.git/blob - src/pedant.c
Added pedantic check of file syntax.
[eval.git] / src / pedant.c
1 /*
2  *      A Pedantic Check of Text Input/Output File Syntax
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdio.h>
8
9 int main(void)
10 {
11   int line = 1;
12   int pos = 0;
13   int maxlen = 0;
14   int lastlen = -1;
15   int space = 0;
16   int c;
17   while ((c = getchar()) >= 0)
18     {
19       if (c == '\n')
20         {
21           if (space)
22             printf("Line %d: Trailing spaces\n", line);
23           if (line == 1 && !pos)
24             printf("Line %d: Leading empty line\n", line);
25           if (maxlen < pos)
26             maxlen = pos;
27           if (!lastlen && !pos)
28             printf("Line %d: Consecutive empty lines\n", line);
29           lastlen = pos;
30           line++;
31           pos = space = 0;
32         }
33       else
34         {
35           if (c == ' ')
36             {
37               if (!pos)
38                 printf("Line %d: Leading spaces\n", line);
39               if (space)
40                 printf("Line %d: Consecutive spaces\n", line);
41               space = 1;
42             }
43           else
44             {
45               space = 0;
46               if (c < ' ' || c >= 0x7f)
47                 printf("Line %d: Invalid character 0x%02x\n", line, c);
48             }
49           pos++;
50         }
51     }
52   if (pos)
53     printf("Line %d: Incomplete line at the end of file\n", line);
54   else if (!lastlen)
55     printf("Line %d: Trailing empty line\n", line-1);
56   printf("Found %d lines, the longest has %d chars\n", line-1, maxlen);
57   return 0;
58 }