home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / src / xtokid.c < prev   
C/C++ Source or Header  |  1996-09-28  |  5KB  |  216 lines

  1. /* idx.c -- simple interface for testing scanners scanners
  2.    Copyright (C) 1986, 1995, 1996 Free Software Foundation, Inc.
  3.    Written by Greg McGary <gkm@gnu.ai.mit.edu>
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  18.  
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. #include "xstring.h"
  23. #include "xnls.h"
  24. #include "scanners.h"
  25. #include "idfile.h"
  26. #include "pathmax.h"
  27. #include "error.h"
  28.  
  29. void scan_files __P((struct idhead *idhp));
  30. void scan_member_file __P((struct member_file const *member));
  31. void usage __P((void));
  32.  
  33. char const *program_name;
  34. char *lang_map_file_name = 0;
  35. int show_version = 0;
  36. int show_help = 0;
  37. struct idhead idh;
  38. struct file_link *cw_dlink;
  39.  
  40. void
  41. usage (void)
  42. {
  43.   fprintf (stderr, _("Try `%s --help' for more information.\n"),
  44.        program_name);
  45.   exit (1);
  46. }
  47.  
  48. static struct option const long_options[] =
  49. {
  50.   { "include", required_argument, 0, 'i' },
  51.   { "exclude", required_argument, 0, 'x' },
  52.   { "lang-option", required_argument, 0, 'l' },
  53.   { "lang-map", required_argument, 0, 'm' },
  54.   { "default-lang", required_argument, 0, 'd' },
  55.   { "prune", required_argument, 0, 'p' },
  56.   { "help", no_argument, &show_help, 1 },
  57.   { "version", no_argument, &show_version, 1 },
  58.   { 0 }
  59. };
  60.  
  61. static void
  62. help_me (void)
  63. {
  64.   printf (_("\
  65. Usage: %s [OPTION]... [FILE]...\n\
  66. "), program_name);
  67.  
  68.   printf (_("\
  69. Print all tokens found in a source file.\n\
  70.   -i, --include=LANGS     include languages in LANGS (default: \"C C++ asm\")\n\
  71.   -x, --exclude=LANGS     exclude languages in LANGS\n\
  72.   -l, --lang-option=L:OPT pass OPT as a default for language L (see below)\n\
  73.   -m, --lang-map=MAPFILE  use MAPFILE to map file names onto source language\n\
  74.   -d, --default-lang=LANG make LANG the default source language\n\
  75.   -p, --prune=NAMES       exclude the named files and/or directories\n\
  76.       --help              display this help and exit\n\
  77.       --version           output version information and exit\n\
  78. \n\
  79. The following arguments apply to the language-specific scanners:\n\
  80. "));
  81.   language_help_me ();
  82.   exit (0);
  83. }
  84.  
  85. int
  86. main (int argc, char **argv)
  87. {
  88.   program_name = argv[0];
  89.  
  90.   /* Set locale according to user's wishes.  */
  91.   setlocale (LC_ALL, "");
  92.  
  93.   /* Tell program which translations to use and where to find.  */
  94.   bindtextdomain (PACKAGE, LOCALEDIR);
  95.   textdomain (PACKAGE);
  96.  
  97.   for (;;)
  98.     {
  99.       int optc = getopt_long (argc, argv, "i:x:l:m:d:p:",
  100.                   long_options, (int *) 0);
  101.       if (optc < 0)
  102.     break;
  103.       switch (optc)
  104.     {
  105.     case 0:
  106.       break;
  107.  
  108.     case 'i':
  109.       include_languages (optarg);
  110.       break;
  111.  
  112.     case 'x':
  113.       exclude_languages (optarg);
  114.       break;
  115.  
  116.     case 'l':
  117.       language_save_arg (optarg);
  118.       break;
  119.  
  120.     case 'm':
  121.       lang_map_file_name = optarg;
  122.       break;
  123.  
  124.     case 'd':
  125.       set_default_language (optarg);
  126.       break;
  127.  
  128.     case 'p':
  129.       if (cw_dlink == 0)
  130.         cw_dlink = init_walker (&idh);
  131.       prune_file_names (optarg, cw_dlink);
  132.       break;
  133.  
  134.     default:
  135.       usage ();
  136.     }
  137.     }
  138.  
  139.   if (show_version)
  140.     {
  141.       printf ("%s - %s\n", program_name, PACKAGE_VERSION);
  142.       exit (0);
  143.     }
  144.  
  145.   if (show_help)
  146.     help_me ();
  147.  
  148.   argc -= optind;
  149.   argv += optind;
  150.   if (argc == 0)
  151.     {
  152.       static char *dot = (char *) ".";
  153.       argc = 1;
  154.       argv = ˙
  155.     }
  156.  
  157.   language_getopt ();
  158.   if (cw_dlink == 0)
  159.     cw_dlink = init_walker (&idh);
  160.   parse_language_map (lang_map_file_name);
  161.  
  162.   while (argc--)
  163.     {
  164.       struct file_link *flink = parse_file_name (*argv++, cw_dlink);
  165.       if (flink)
  166.     walk_flink (flink, 0);
  167.     }
  168.   mark_member_file_links (&idh);
  169.   obstack_init (&tokens_obstack);
  170.   scan_files (&idh);
  171.  
  172.   return 0;
  173. }
  174.  
  175. void
  176. scan_files (struct idhead *idhp)
  177. {
  178.   struct member_file **members_0
  179.     = (struct member_file **) hash_dump (&idhp->idh_member_file_table,
  180.                      0, member_file_qsort_compare);
  181.   struct member_file **end = &members_0[idhp->idh_member_file_table.ht_fill];
  182.   struct member_file **members;
  183.  
  184.   for (members = members_0; members < end; members++)
  185.     scan_member_file (*members);
  186.   free (members_0);
  187. }
  188.  
  189. void
  190. scan_member_file (struct member_file const *member)
  191. {
  192.   struct lang_args const *lang_args = member->mf_lang_args;
  193.   struct language const *lang = lang_args->la_language;
  194.   get_token_func_t get_token = lang->lg_get_token;
  195.   struct file_link *flink = member->mf_link;
  196.   FILE *source_FILE;
  197.  
  198.   chdir_to_link (flink->fl_parent);
  199.   source_FILE = fopen (flink->fl_name, "r");
  200.   if (source_FILE)
  201.     {
  202.       void const *args = lang_args->la_args_digested;
  203.       int flags;
  204.       struct token *token;
  205.  
  206.       while ((token = (*get_token) (source_FILE, args, &flags)) != NULL)
  207.     {
  208.       puts (token->tok_name);
  209.       obstack_free (&tokens_obstack, token);
  210.     }
  211.       fclose (source_FILE);
  212.     }
  213.   else
  214.     error (0, errno, _("can't open `%s'"), flink->fl_name);
  215. }
  216.