File: main.c

package info (click to toggle)
libexplain 1.4.D001-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,228 kB
  • sloc: ansic: 156,043; makefile: 47,892; sh: 16,304; yacc: 1,898; awk: 245
file content (224 lines) | stat: -rw-r--r-- 5,395 bytes parent folder | download | duplicates (6)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * libexplain - Explain errno values returned by libc functions
 * Copyright (C) 2009 Peter Miller
 * Written by Peter Miller <pmiller@opensource.org.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libexplain/ac/ctype.h>
#include <libexplain/ac/stdio.h>
#include <libexplain/ac/stdlib.h>
#include <libexplain/ac/string.h>
#include <libexplain/ac/unistd.h>

#include <libexplain/fclose.h>
#include <libexplain/fgets.h>
#include <libexplain/fopen.h>
#include <libexplain/freopen.h>
#include <libexplain/program_name.h>
#include <libexplain/version_print.h>


static int      initialised;
static int      fixme_count;


static void
begin(void)
{
    if (initialised)
        return;
    initialised = 1;

    printf("<html>\n");
    printf("<body>\n");
    printf("<h1> Fix Me, Please! </h1>\n");
    printf("The following notes were automatically extracted from\n");
    printf("the source files, at places where the code is marked\n");
    printf("&ldquo;<tt>FIXME:</tt>&rdquo;.\n");
    printf("<table>\n");
    printf("<tr><th>Filename</th><th>Line</th><th>Comment</th></tr>\n");
}


static void
process(const char *filename)
{
    FILE            *fp;
    int             line_number;

    begin();
    fp = explain_fopen_or_die(filename, "r");
    line_number = 0;
    for (;;)
    {
        char            line[1000];
        char            *cp;

        if (!explain_fgets_or_die(line, sizeof(line), fp))
            break;
        ++line_number;

        cp = strstr(line, "FIXME:");
        if (cp)
        {
            char            *ep;
            const char      *fn;
            const char      *bp;

            cp += 6;
            while (*cp && isspace((unsigned char)*cp))
                ++cp;
            ep = cp + strlen(cp);
            while (ep > cp)
            {
                if (isspace((unsigned char)ep[-1]))
                {
                    --ep;
                    continue;
                }
                if (ep - cp >= 2 && ep[-2] == '*' && ep[-1] == '/')
                {
                    ep -= 2;
                    continue;
                }
                break;
            }

            fn = filename;
            bp = fn;
            while (bp[0] == 'b' && bp[1] == 'l')
                bp += 2;
            if (*bp == '/')
                fn = bp + 1;

            if
            (
                0 != strcmp(fn, "fixme-html/main.c")
            &&
                0 != strcmp(fn, "web-src/index.html")
            )
            {
                printf("<tr><td valign=\"top\" align=\"right\" >%s</td>\n", fn);
                printf
                (
                    "    <td valign=\"top\" align=\"right\" >%d:</td>\n",
                    line_number
                );
                printf
                (
                    "    <td valign=\"top\" >%.*s</td></tr>\n",
                    (int)(ep - cp),
                    cp
                );
                printf("    </tr>\n");
                ++fixme_count;
            }
        }
    }
    explain_fclose_or_die(fp);
}


static void
end(void)
{
    printf("</table>\n");
    printf("<p>\n");
    printf
    (
        "Found %d places in the code marked &ldquo;FIXME:&rdquo;.\n",
        fixme_count
    );
    printf("</body>\n");
    printf("</html>\n");
}


static void
process_list(const char *filename)
{
    FILE            *fp;

    fp = stdin;
    if (0 != strcmp(filename, "-"))
        fp = explain_fopen_or_die(filename, "r");
    for (;;)
    {
        size_t          len;
        char            line[1000];

        if (!explain_fgets_or_die(line, sizeof(line), fp))
            break;
        len = strlen(line);
        if (line[len - 1] == '\n')
            line[len - 1] = '\0';
        process(line);
    }
    if (fp != stdin)
        explain_fclose_or_die(fp);
}


static void
usage(void)
{
    const char      *prog;

    prog = explain_program_name_get();
    fprintf(stderr, "Usage: %s [ <option>... ] <filename>...\n", prog);
    fprintf(stderr, "       %s -V\n", prog);
    exit(1);
}


int
main(int argc, char **argv)
{
    for (;;)
    {
        int c = getopt(argc, argv, "F:o:V");
        if (c == EOF)
            break;
        switch (c)
        {
        case 'o':
            explain_freopen_or_die(optarg, "w", stdout);
            break;

        case 'F':
            process_list(optarg);
            break;

        case 'V':
            explain_version_print();
            return 0;

        default:
            usage();
        }
    }

    while (optind < argc)
        process(argv[optind++]);
    if (!initialised)
    {
        fprintf(stderr, "%s: no files named\n", argv[0]);
        exit(1);
    }
    end();
    explain_fclose_or_die(stdout);
    return 0;
}