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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
* libexplain - Explain errno values returned by libc functions
* Copyright (C) 2010, 2011 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/assert.h>
#include <libexplain/ac/ctype.h>
#include <libexplain/ac/errno.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/malloc.h>
#include <libexplain/program_name.h>
#include <libexplain/string_list.h>
#include <libexplain/strndup.h>
#include <libexplain/version_print.h>
static explain_string_list_t view_path;
static explain_string_list_t source_files;
static int error_count;
static char read_lines_path[1000];
static void
read_lines(const char *filename, explain_string_list_t *content)
{
FILE *fp;
size_t j;
fp = 0;
if (0 == strcmp(filename, "-"))
fp = stdin;
else
{
for (j = 0; j < view_path.length; ++j)
{
if (j >= view_path.length)
{
fprintf(stderr, "%s\n", explain_fopen(filename, "r"));
exit(EXIT_FAILURE);
}
snprintf
(
read_lines_path,
sizeof(read_lines_path),
"%s/%s",
view_path.string[j],
filename
);
fp = fopen(read_lines_path, "r");
if (fp)
break;
if (errno != ENOENT)
{
fprintf(stderr, "%s\n", explain_fopen(read_lines_path, "r"));
exit(1);
}
}
if (!fp)
{
fprintf(stderr, "%s\n", explain_fopen(filename, "r"));
exit(1);
}
}
for (;;)
{
size_t n;
char buffer[1000];
if (!explain_fgets_or_die(buffer, sizeof(buffer), fp))
break;
n = strlen(buffer);
while (n > 0 && isspace((unsigned char)buffer[n - 1]))
--n;
explain_string_list_append_n(content, buffer, n);
}
if (fp != stdin)
explain_fclose_or_die(fp);
}
static char *
extract_include_name(const char *line)
{
const char *cp;
const char *start;
cp = line;
while (isspace((unsigned char)*cp))
++cp;
if (*cp++ != '#')
return 0;
while (isspace((unsigned char)*cp))
++cp;
if (0 != memcmp(cp, "include", 7))
return 0;
cp += 7;
while (isspace((unsigned char)*cp))
++cp;
if (*cp++ != '<')
return 0;
start = cp;
while (*cp != '>')
{
if (*cp == '\0')
return 0;
++cp;
}
if (start == cp)
return 0;
return explain_strndup_or_die(start, cp - start);
}
static int
ends_with(const char *haystack, const char *needle)
{
size_t haystack_l;
size_t needle_l;
haystack_l = strlen(haystack);
needle_l = strlen(needle);
return
(
needle_l <= haystack_l
&&
0 == memcmp(haystack + haystack_l - needle_l, needle, needle_l)
);
}
static void
check(const char *filename)
{
explain_string_list_t isource;
explain_string_list_t require;
size_t j;
/*
* Read the file.
*/
explain_string_list_constructor(&isource);
read_lines(filename, &isource);
/*
* Look for system includes in the file's source.
*/
explain_string_list_constructor(&require);
for (j = 0; j < isource.length; ++j)
{
const char *line;
char *name;
line = isource.string[j];
name = extract_include_name(line);
if (name)
{
if (0 != memcmp(name, "libexplain/", 11))
{
char req[100];
snprintf(req, sizeof(req), "libexplain/ac/%s", name);
explain_string_list_append(&require, req);
}
free(name);
}
}
explain_string_list_destructor(&isource);
if (require.length > 0)
{
char *used;
used = explain_malloc_or_die(require.length);
/*
* Examine each project source file.
* If it includes the file passed in,
* it must include all of the "require" files.
*/
for (j = 0; j < source_files.length; ++j)
{
const char *source_file;
explain_string_list_t source;
size_t k;
int relevant;
source_file = source_files.string[j];
if (!ends_with(source_file, ".c"))
continue;
if (0 == strcmp(source_file, "libexplain/libexplain.h"))
continue;
memset(used, 0, require.length);
explain_string_list_constructor(&source);
read_lines(source_file, &source);
/*
* Check each line.
*/
relevant = 0;
for (k = 0; k < source.length; ++k)
{
const char *line;
char *name;
line = source.string[k];
name = extract_include_name(line);
if (name)
{
size_t m;
if (0 == strcmp(name, filename))
{
relevant = k + 1;
break;
}
for (m = 0; m < require.length; ++m)
{
if (0 == strcmp(name, require.string[m]))
{
used[m] = 1;
break;
}
}
free(name);
}
}
explain_string_list_destructor(&source);
if (relevant)
{
/*
* Did they use 'em all?
*/
for (k = 0; k < require.length; ++k)
{
if (!used[k])
{
fprintf
(
stderr,
"%s: %d: portability needs #include <%s>\n",
read_lines_path,
relevant,
require.string[k]
);
++error_count;
}
}
}
}
free(used);
}
explain_string_list_destructor(&require);
}
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(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
explain_string_list_t libexplain_h;
explain_string_list_t public_api;
size_t j;
explain_string_list_append(&view_path, ".");
for (;;)
{
int c = getopt(argc, argv, "F:I:V");
if (c < 0)
break;
switch (c)
{
case 'F':
read_lines(optarg, &source_files);
break;
case 'I':
explain_string_list_append(&view_path, optarg);
break;
case 'V':
explain_version_print();
return EXIT_SUCCESS;
default:
usage();
/* NOTREACHED */
}
}
while (optind < argc)
explain_string_list_append_unique(&source_files, argv[optind++]);
if (source_files.length == 0)
{
fprintf(stderr, "no source files named\n");
exit(EXIT_FAILURE);
}
/*
* Find the libexplain/libexplain.h file.
*/
explain_string_list_constructor(&libexplain_h);
read_lines("libexplain/libexplain.h", &libexplain_h);
/*
* Find all files that constitute the public interface.
*/
explain_string_list_constructor(&public_api);
for (j = 0; j < libexplain_h.length; ++j)
{
const char *line;
char *name;
line = libexplain_h.string[j];
name = extract_include_name(line);
if (name)
{
explain_string_list_append_unique(&public_api, name);
free(name);
}
}
explain_string_list_destructor(&libexplain_h);
/*
* Check each source file in the public API.
*/
for (j = 0; j < public_api.length; ++j)
check(public_api.string[j]);
if (error_count)
{
fprintf
(
stderr,
"Found %d fatal error%s\n",
error_count,
(error_count == 1 ? "" : "s")
);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|