| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <getopt.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <assert.h> |
| 26 |
|
| 27 |
#include "argv-iter.h" |
| 28 |
#include "closeout.h" |
| 29 |
#include "error.h" |
| 30 |
#include "pathmax.h" |
| 31 |
#include "progname.h" |
| 32 |
#include "quote.h" |
| 33 |
#include "quotearg.h" |
| 34 |
#include "xalloc.h" |
| 35 |
|
| 36 |
#include "xnls.h" |
| 37 |
#include "scanners.h" |
| 38 |
#include "idfile.h" |
| 39 |
#include "iduglobal.h" |
| 40 |
|
| 41 |
static void scan_files (struct idhead *idhp); |
| 42 |
static void scan_member_file (struct member_file const *member); |
| 43 |
void usage (void) __attribute__((__noreturn__)); |
| 44 |
|
| 45 |
static char *lang_map_file_name = 0; |
| 46 |
static int show_version = 0; |
| 47 |
static int show_help = 0; |
| 48 |
struct idhead idh; |
| 49 |
static struct file_link *cw_dlink; |
| 50 |
|
| 51 |
void |
| 52 |
usage (void) |
| 53 |
{ |
| 54 |
fprintf (stderr, _("Try `%s --help' for more information.\n"), |
| 55 |
program_name); |
| 56 |
exit (EXIT_FAILURE); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
enum |
| 62 |
{ |
| 63 |
FILES0_FROM_OPTION = CHAR_MAX +1, |
| 64 |
}; |
| 65 |
|
| 66 |
static struct option const long_options[] = |
| 67 |
{ |
| 68 |
{ "include", required_argument, 0, 'i' }, |
| 69 |
{ "exclude", required_argument, 0, 'x' }, |
| 70 |
{ "lang-option", required_argument, 0, 'l' }, |
| 71 |
{ "lang-map", required_argument, 0, 'm' }, |
| 72 |
{ "default-lang", required_argument, 0, 'd' }, |
| 73 |
{ "prune", required_argument, 0, 'p' }, |
| 74 |
{ "help", no_argument, &show_help, 1 }, |
| 75 |
{ "version", no_argument, &show_version, 1 }, |
| 76 |
{ "files0-from", required_argument, NULL, FILES0_FROM_OPTION }, |
| 77 |
{NULL, 0, NULL, 0} |
| 78 |
}; |
| 79 |
|
| 80 |
static void __attribute__((__noreturn__)) |
| 81 |
help_me (void) |
| 82 |
{ |
| 83 |
printf (_("\ |
| 84 |
Usage: %s [OPTION]... [FILE]...\n\ |
| 85 |
"), program_name); |
| 86 |
|
| 87 |
printf (_("\ |
| 88 |
Print all tokens found in a source file.\n\ |
| 89 |
-i, --include=LANGS include languages in LANGS (default: \"C C++ asm\")\n\ |
| 90 |
-x, --exclude=LANGS exclude languages in LANGS\n\ |
| 91 |
-l, --lang-option=L:OPT pass OPT as a default for language L (see below)\n\ |
| 92 |
-m, --lang-map=MAPFILE use MAPFILE to map file names onto source language\n\ |
| 93 |
-d, --default-lang=LANG make LANG the default source language\n\ |
| 94 |
-p, --prune=NAMES exclude the named files and/or directories\n\ |
| 95 |
\n\ |
| 96 |
--files0-from=F tokenize only the files specified by\n\ |
| 97 |
NUL-terminated names in file F\n\ |
| 98 |
\n\ |
| 99 |
--help display this help and exit\n \ |
| 100 |
--version output version information and exit\n\ |
| 101 |
\n\ |
| 102 |
The following arguments apply to the language-specific scanners:\n\ |
| 103 |
")); |
| 104 |
language_help_me (); |
| 105 |
printf (_("\nReport bugs to " PACKAGE_BUGREPORT "\n\n")); |
| 106 |
exit (EXIT_SUCCESS); |
| 107 |
} |
| 108 |
|
| 109 |
int |
| 110 |
main (int argc, char **argv) |
| 111 |
{ |
| 112 |
bool ok; |
| 113 |
int nfiles; |
| 114 |
char *files_from = NULL; |
| 115 |
|
| 116 |
set_program_name (argv[0]); |
| 117 |
|
| 118 |
#if ENABLE_NLS |
| 119 |
|
| 120 |
setlocale (LC_ALL, ""); |
| 121 |
|
| 122 |
|
| 123 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 124 |
textdomain (PACKAGE); |
| 125 |
#endif |
| 126 |
|
| 127 |
atexit (close_stdout); |
| 128 |
|
| 129 |
for (;;) |
| 130 |
{ |
| 131 |
int optc = getopt_long (argc, argv, "i:x:l:m:d:p:", |
| 132 |
long_options, (int *) 0); |
| 133 |
if (optc < 0) |
| 134 |
break; |
| 135 |
switch (optc) |
| 136 |
{ |
| 137 |
case 0: |
| 138 |
break; |
| 139 |
|
| 140 |
case 'i': |
| 141 |
include_languages (optarg); |
| 142 |
break; |
| 143 |
|
| 144 |
case 'x': |
| 145 |
exclude_languages (optarg); |
| 146 |
break; |
| 147 |
|
| 148 |
case 'l': |
| 149 |
language_save_arg (optarg); |
| 150 |
break; |
| 151 |
|
| 152 |
case 'm': |
| 153 |
lang_map_file_name = optarg; |
| 154 |
break; |
| 155 |
|
| 156 |
case 'd': |
| 157 |
set_default_language (optarg); |
| 158 |
break; |
| 159 |
|
| 160 |
case 'p': |
| 161 |
if (cw_dlink == 0) |
| 162 |
cw_dlink = init_walker (&idh); |
| 163 |
prune_file_names (optarg, cw_dlink); |
| 164 |
break; |
| 165 |
|
| 166 |
case FILES0_FROM_OPTION: |
| 167 |
files_from = optarg; |
| 168 |
break; |
| 169 |
|
| 170 |
default: |
| 171 |
usage (); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (show_version) |
| 176 |
{ |
| 177 |
printf ("%s - %s\n", program_name, PACKAGE_VERSION); |
| 178 |
exit (EXIT_SUCCESS); |
| 179 |
} |
| 180 |
|
| 181 |
if (show_help) |
| 182 |
help_me (); |
| 183 |
|
| 184 |
nfiles = argc - optind; |
| 185 |
|
| 186 |
struct argv_iterator *ai; |
| 187 |
if (files_from) |
| 188 |
{ |
| 189 |
|
| 190 |
|
| 191 |
if (nfiles != 0) |
| 192 |
{ |
| 193 |
error (0, 0, _("extra operand %s"), quote (argv[optind])); |
| 194 |
fprintf (stderr, "%s\n", |
| 195 |
_("file operands cannot be combined with --files0-from")); |
| 196 |
usage(); |
| 197 |
} |
| 198 |
|
| 199 |
if (! (strequ (files_from, "-") || freopen (files_from, "r", stdin))) |
| 200 |
error (EXIT_FAILURE, errno, _("cannot open %s for reading"), |
| 201 |
quote (files_from)); |
| 202 |
|
| 203 |
ai = argv_iter_init_stream (stdin); |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
static char *cwd_only[2]; |
| 208 |
cwd_only[0] = bad_cast ("."); |
| 209 |
cwd_only[1] = NULL; |
| 210 |
char **files = (optind < argc ? argv + optind : cwd_only); |
| 211 |
ai = argv_iter_init_argv (files); |
| 212 |
} |
| 213 |
|
| 214 |
language_getopt (); |
| 215 |
if (cw_dlink == 0) |
| 216 |
cw_dlink = init_walker (&idh); |
| 217 |
parse_language_map (lang_map_file_name); |
| 218 |
|
| 219 |
|
| 220 |
ok = true; |
| 221 |
while (true) |
| 222 |
{ |
| 223 |
bool skip_file = false; |
| 224 |
enum argv_iter_err ai_err; |
| 225 |
char *file_name = argv_iter (ai, &ai_err); |
| 226 |
if (ai_err == AI_ERR_EOF) |
| 227 |
break; |
| 228 |
if (!file_name) |
| 229 |
{ |
| 230 |
switch (ai_err) |
| 231 |
{ |
| 232 |
case AI_ERR_READ: |
| 233 |
error (0, errno, _("%s: read error"), quote (files_from)); |
| 234 |
continue; |
| 235 |
|
| 236 |
case AI_ERR_MEM: |
| 237 |
xalloc_die (); |
| 238 |
|
| 239 |
default: |
| 240 |
assert (!"unexpected error code from argv_iter"); |
| 241 |
} |
| 242 |
} |
| 243 |
if (files_from && STREQ (files_from, "-") && STREQ (file_name, "-")) |
| 244 |
{ |
| 245 |
|
| 246 |
|
| 247 |
error (0, 0, _("when reading file names from stdin, " |
| 248 |
"no file name of %s allowed"), |
| 249 |
quote (file_name)); |
| 250 |
skip_file = true; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
if (!file_name[0]) |
| 255 |
{ |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
if (files_from == NULL) |
| 261 |
error (0, 0, "%s", _("invalid zero-length file name")); |
| 262 |
else |
| 263 |
{ |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
unsigned long int file_number = argv_iter_n_args (ai); |
| 268 |
error (0, 0, "%s:%lu: %s", quotearg_colon (files_from), |
| 269 |
file_number, _("invalid zero-length file name")); |
| 270 |
} |
| 271 |
skip_file = true; |
| 272 |
} |
| 273 |
|
| 274 |
if (skip_file) |
| 275 |
ok = false; |
| 276 |
else |
| 277 |
{ |
| 278 |
struct file_link *flink = parse_file_name (file_name, cw_dlink); |
| 279 |
if (flink) |
| 280 |
walk_flink (flink, 0); |
| 281 |
|
| 282 |
|
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
argv_iter_free (ai); |
| 287 |
mark_member_file_links (&idh); |
| 288 |
obstack_init (&tokens_obstack); |
| 289 |
scan_files (&idh); |
| 290 |
|
| 291 |
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 292 |
} |
| 293 |
|
| 294 |
static void |
| 295 |
scan_files (struct idhead *idhp) |
| 296 |
{ |
| 297 |
struct member_file **members_0 |
| 298 |
= (struct member_file **) hash_dump (&idhp->idh_member_file_table, |
| 299 |
0, member_file_qsort_compare); |
| 300 |
struct member_file **end = &members_0[idhp->idh_member_file_table.ht_fill]; |
| 301 |
struct member_file **members; |
| 302 |
|
| 303 |
if (largest_member_file > MAX_LARGEST_MEMBER_FILE) |
| 304 |
largest_member_file = MAX_LARGEST_MEMBER_FILE; |
| 305 |
scanner_buffer = xmalloc (largest_member_file + 1); |
| 306 |
|
| 307 |
for (members = members_0; members < end; members++) |
| 308 |
scan_member_file (*members); |
| 309 |
|
| 310 |
free (scanner_buffer); |
| 311 |
free (members_0); |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
scan_member_file (struct member_file const *member) |
| 316 |
{ |
| 317 |
struct lang_args const *lang_args = member->mf_lang_args; |
| 318 |
struct language const *lang = lang_args->la_language; |
| 319 |
get_token_func_t get_token = lang->lg_get_token; |
| 320 |
struct file_link *flink = member->mf_link; |
| 321 |
FILE *source_FILE; |
| 322 |
|
| 323 |
chdir_to_link (flink->fl_parent); |
| 324 |
source_FILE = fopen (flink->fl_name, "r"); |
| 325 |
if (source_FILE) |
| 326 |
{ |
| 327 |
void const *args = lang_args->la_args_digested; |
| 328 |
int flags; |
| 329 |
struct token *token; |
| 330 |
|
| 331 |
while ((token = (*get_token) (source_FILE, args, &flags)) != NULL) |
| 332 |
{ |
| 333 |
puts (TOKEN_NAME (token)); |
| 334 |
obstack_free (&tokens_obstack, token); |
| 335 |
} |
| 336 |
fclose (source_FILE); |
| 337 |
} |
| 338 |
else |
| 339 |
error (0, errno, _("can't open `%s'"), flink->fl_name); |
| 340 |
} |