home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / cperf-2.1 / src / options.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-11  |  18.1 KB  |  445 lines

  1. /* Handles parsing the Options provided to the user.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  4.  
  5. This file is part of GNU GPERF.
  6.  
  7. GNU GPERF is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU GPERF is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU GPERF; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #include "options.h"
  24. #include "iterator.h"
  25. #include "stderr.h"
  26.  
  27. /* Current program version. */
  28. extern char *version_string;
  29.  
  30. /* Size to jump on a collision. */
  31. #define DEFAULT_JUMP_VALUE 5
  32.  
  33. /* Default name for generated lookup function. */
  34. #define DEFAULT_NAME "in_word_set"
  35.  
  36. /* Default name for the key component. */
  37. #define DEFAULT_KEY "name"
  38.  
  39. /* Default name for generated hash function. */
  40. #define DEFAULT_HASH_NAME "hash"
  41.  
  42. /* Globally visible OPTIONS object. */
  43. OPTIONS option;
  44.  
  45. /* Default delimiters that separate keywords from their attributes. */
  46. #define DEFAULT_DELIMITERS ",\n"
  47.  
  48. /* Prints program usage to standard error stream. */
  49.  
  50. void 
  51. usage ()
  52.   report_error ("usage: %n [-acCdDef[num]gGhH<hashname>i<init>jk<keys>\
  53. K<keyname>lnN<name>oprs<size>S<switches>tTv].\n(type %n -h for help)\n");
  54. }
  55.  
  56. /* Sorts the key positions *IN REVERSE ORDER!!*
  57.    This makes further routines more efficient.  Especially when generating code.
  58.    Uses a simple Insertion Sort since the set is probably ordered.
  59.    Returns 1 if there are no duplicates, 0 otherwise. */
  60.  
  61. static int 
  62. key_sort (base, len)
  63.      char *base;
  64.      int   len;
  65. {
  66.   int i, j;
  67.  
  68.   for (i = 0, j = len - 1; i < j; i++) 
  69.     {
  70.       int curr, tmp;
  71.       
  72.       for (curr = i + 1,tmp = base[curr]; curr > 0 && tmp >= base[curr - 1]; curr--) 
  73.         if ((base[curr] = base[curr - 1]) == tmp) /* oh no, a duplicate!!! */
  74.           return 0;
  75.  
  76.       base[curr] = tmp;
  77.     }
  78.  
  79.   return 1;
  80. }
  81.  
  82. /* Dumps option status when debug is set. */
  83.  
  84. void
  85. options_destroy ()
  86.   if (OPTION_ENABLED (option, DEBUG))
  87.     {
  88.       char *ptr;
  89.  
  90.       fprintf (stderr, "\ndumping Options:\nDEBUG is.......: %s\nORDER is.......: %s\
  91. \nANSI is........: %s\nTYPE is........: %s\nGNU is.........: %s\nRANDOM is......: %s\
  92. \nDEFAULTCHARS is: %s\nSWITCH is......: %s\nPOINTER is.....: %s\nNOLENGTH is....: %s\
  93. \nLENTABLE is....: %s\nDUP is.........: %s\nCOMP is........: %s\nFAST is........: %s\
  94. \nNOTYPE is......: %s\nGLOBAL is......: %s\nCONST is.......: %s\niterations = %d\
  95. \nlookup function name = %s\nhash function name = %s\nkey name = %s\
  96. \njump value = %d\nmax associcated value = %d\ninitial associated value = %d\
  97. \ndelimiters = %s\nnumber of switch statements = %d\napproximate switch statement size = %d\n",
  98.                OPTION_ENABLED (option, DEBUG) ? "enabled" : "disabled", 
  99.                OPTION_ENABLED (option, ORDER) ? "enabled" : "disabled",
  100.                OPTION_ENABLED (option, ANSI) ? "enabled" : "disabled",
  101.                OPTION_ENABLED (option, TYPE) ? "enabled" : "disabled",
  102.                OPTION_ENABLED (option, GNU) ? "enabled" : "disabled",
  103.                OPTION_ENABLED (option, RANDOM) ? "enabled" : "disabled",
  104.                OPTION_ENABLED (option, DEFAULTCHARS) ? "enabled" : "disabled",
  105.                OPTION_ENABLED (option, SWITCH) ? "enabled" : "disabled",
  106.                OPTION_ENABLED (option, POINTER) ? "enabled" : "disabled",
  107.                OPTION_ENABLED (option, NOLENGTH) ? "enabled" : "disabled",
  108.                OPTION_ENABLED (option, LENTABLE) ? "enabled" : "disabled",
  109.                OPTION_ENABLED (option, DUP) ? "enabled" : "disabled",
  110.                OPTION_ENABLED (option, COMP) ? "enabled" : "disabled",
  111.                OPTION_ENABLED (option, FAST) ? "enabled" : "disabled",
  112.                OPTION_ENABLED (option, NOTYPE) ? "enabled" : "disabled",
  113.                OPTION_ENABLED (option, GLOBAL) ? "enabled" : "disabled",
  114.                OPTION_ENABLED (option, CONST) ? "enabled" : "disabled",
  115.                option.iterations, option.function_name, option.hash_name,
  116.                option.key_name, option.jump, option.size - 1, 
  117.                option.initial_asso_value, option.delimiters, option.total_switches,
  118.                keyword_list_length () / option.total_switches);
  119.  
  120.       if (OPTION_ENABLED (option, ALLCHARS)) 
  121.         fprintf (stderr, "all characters are used in the hash function\n");
  122.       fprintf (stderr, "maximum charset size = %d\nkey positions are: \n", 
  123.                option.total_charset_size);
  124.  
  125.       for (ptr = option.key_positions; *ptr != EOS; ptr++) 
  126.         if (*ptr == WORD_END) 
  127.           fprintf (stderr, "$\n");
  128.         else 
  129.           fprintf (stderr, "%d\n", *ptr);
  130.  
  131.       fprintf (stderr, "finished dumping Options\n");
  132.     }
  133. }
  134.  
  135. /* Parses the command line Options and sets appropriate flags in option.option_word. */
  136.  
  137. void
  138. options_init (argc, argv)
  139.      int argc;
  140.      char *argv[];
  141.   extern int   optind;
  142.   extern char *optarg;
  143.   int   option_char;
  144.   
  145.   option.key_positions[0]   = WORD_START;
  146.   option.key_positions[1]   = WORD_END;
  147.   option.key_positions[2]   = EOS;
  148.   option.total_charset_size = 2;
  149.   option.jump               = DEFAULT_JUMP_VALUE;
  150.   option.option_word        = (int) DEFAULTCHARS;
  151.   option.function_name      = DEFAULT_NAME;
  152.   option.hash_name          = DEFAULT_HASH_NAME;
  153.   option.key_name           = DEFAULT_KEY;
  154.   option.delimiters         = DEFAULT_DELIMITERS;
  155.   option.initial_asso_value = option.size = option.iterations = 0;
  156.   option.total_switches     = 1;
  157.   option.argument_count     = argc;
  158.   option.argument_vector    = argv;
  159.   set_program_name (argv[0]);
  160.   
  161.   while ((option_char = getopt (argc, argv, "adcCDe:f:gGhH:i:j:k:K:lnN:oprs:S:tTv")) != EOF)
  162.     {
  163.       switch (option_char)
  164.         {
  165.         case 'a':               /* Generated coded uses the ANSI prototype format. */
  166.           { 
  167.             SET_OPTION (option, ANSI);
  168.             break;
  169.           }
  170.         case 'c':               /* Generate strncmp rather than strcmp. */
  171.           {
  172.             SET_OPTION (option, COMP);
  173.             break;
  174.           }
  175.         case 'C':               /* Make the generated tables readonly (const). */
  176.           {
  177.             SET_OPTION (option, CONST);
  178.             break;
  179.           }
  180.         case 'd':               /* Enable debugging option. */
  181.           { 
  182.             SET_OPTION (option, DEBUG);
  183.             report_error ("starting program %n, version %s, with debuggin on.\n",
  184.                           version_string);
  185.             break;
  186.           }   
  187.         case 'D':               /* Enable duplicate option. */
  188.           { 
  189.             SET_OPTION (option, DUP);
  190.             break;
  191.           }   
  192.         case 'e': /* Allows user to provide keyword/attribute separator */
  193.           {
  194.             SET_DELIMITERS (option, optarg);
  195.             break;
  196.           }
  197.         case 'f':               /* Generate the hash table ``fast.'' */
  198.           {
  199.             SET_OPTION (option, FAST);
  200.             if ((option.iterations = atoi (optarg)) < 0) 
  201.               {
  202.                 report_error ("iterations value must not be negative, assuming 0\n");
  203.                 option.iterations = 0;
  204.               }
  205.             break;
  206.           }
  207.         case 'g':               /* Use the ``inline'' keyword for generated sub-routines. */
  208.           { 
  209.             SET_OPTION (option, GNU);
  210.             break;
  211.           }
  212.         case 'G':               /* Make the keyword table a global variable. */
  213.           { 
  214.             SET_OPTION (option, GLOBAL);
  215.             break;
  216.           }
  217.         case 'h':               /* Displays a list of helpful Options to the user. */
  218.           { 
  219.             report_error (
  220. "-a\tGenerate ANSI standard C output code, i.e., function prototypes.\n\
  221. -c\tGenerate comparison code using strncmp rather than strcmp.\n\
  222. -C\tMake the contents of generated lookup tables constant, i.e., readonly.\n\
  223. -d\tEnables the debugging option (produces verbose output to Std_Err).\n\
  224. -D\tHandle keywords that hash to duplicate values.  This is useful\n\
  225. \tfor certain highly redundant keyword sets.  It enables the -S option.\n\
  226. -e\tAllow user to provide a string containing delimiters used to separate\n\
  227. \tkeywords from their attributes.  Default is \",\\n\"\n\
  228. -f\tGenerate the perfect hash function ``fast.''  This decreases GPERF's\n\
  229. \trunning time at the cost of minimizing generated table-size.\n\
  230. \tThe numeric argument represents the number of times to iterate when\n\
  231. \tresolving a collision.  `0' means ``iterate by the number of keywords''.\n\
  232. -g\tAssume a GNU compiler, e.g., g++ or gcc.  This makes all generated\n\
  233. \troutines use the ``inline'' keyword to remove cost of function calls.\n\
  234. -G\tGenerate the static table of keywords as a static global variable,\n\
  235. \trather than hiding it inside of the lookup function (which is the\n\
  236. \tdefault behavior).\n\
  237. -h\tPrints this mesage.\n");
  238.               report_error (
  239. "-H\tAllow user to specify name of generated hash function. Default is `hash'.\n\
  240. -i\tProvide an initial value for the associate values array.  Default is 0.\n\
  241. \tSetting this value larger helps inflate the size of the final table.\n\
  242. -j\tAffects the ``jump value,'' i.e., how far to advance the associated\n\
  243. \tcharacter value upon collisions.  Must be an odd number, default is %d.\n\
  244. -k\tAllows selection of the key positions used in the hash function.\n\
  245. \tThe allowable choices range between 1-%d, inclusive.  The positions\n\
  246. \tare separated by commas, ranges may be used, and key positions may\n\
  247. \toccur in any order.  Also, the meta-character '*' causes the generated\n\
  248. \thash function to consider ALL key positions, and $ indicates the\n\
  249. \t``final character'' of a key, e.g., $,1,2,4,6-10.\n\
  250. -K\tAllow user to select name of the keyword component in the keyword structure.\n\
  251. -l\tCompare key lengths before trying a string comparison.  This helps\n\
  252. \tcut down on the number of string comparisons made during the lookup.\n\
  253. -n\tDo not include the length of the keyword when computing the hash function\n\
  254. -N\tAllow user to specify name of generated lookup function.  Default\n\
  255. \tname is `in_word_set.'\n\
  256. -o\tReorders input keys by frequency of occurrence of the key sets.\n\
  257. \tThis should decrease the search time dramatically.\n\
  258. -p\tChanges the return value of the generated function ``in_word_set''\n\
  259. \tfrom its default boolean value (i.e., 0 or 1), to type ``pointer\n\
  260. \tto wordlist array''  This is most useful when the -t option, allowing\n\
  261. \tuser-defined structs, is used.\n",
  262.                           DEFAULT_JUMP_VALUE, MAX_KEY_POS - 1);
  263.             report_error (
  264. "-r\tUtilizes randomness to initialize the associated values table.\n\
  265. -s\tAffects the size of the generated hash table.  The numeric argument\n\
  266. \tfor this option indicates ``how many times larger'' the table range\n\
  267. \tshould be, in relationship to the number of keys, e.g. a value of 3\n\
  268. \tmeans ``make the table about 3 times larger than the number of input\n\
  269. \tkeys.''  A larger table should decrease the time required for an\n\
  270. \tunsuccessful search, at the expense of extra table space.  Default\n\
  271. \tvalue is 1.  This actual table size may vary somewhat.\n\
  272. -S\tCauses the generated C code to use a switch statement scheme, rather\n\
  273. \tthan an array lookup table.  This can lead to a reduction in both\n\
  274. \ttime and space requirements for some keyfiles.  The argument to\n\
  275. \tthis option determines how many switch statements are generated.\n\
  276. \tA value of 1 generates 1 switch containing all the elements, a value of 2\n\
  277. \tgenerates 2 tables with 1/2 the elements in each table, etc.  This\n\
  278. \tis useful since many C compilers cannot correctly generate code for\n\
  279. \tlarge switch statements.\n\
  280. \tthe expense of longer time for each lookup.  Mostly important for\n\
  281. \t*large* input sets, i.e., greater than around 100 items or so.\n\
  282. -t\tAllows the user to include a structured type declaration for \n\
  283. \tgenerated code. Any text before %%%% is consider part of the type\n\
  284. \tdeclaration.  Key words and additional fields may follow this, one\n\
  285. \tgroup of fields per line.\n\
  286. -T\tPrevents the transfer of the type declaration to the output file.\n\
  287. \tUse this option if the type is already defined elsewhere.\n\
  288. -v\tPrints out the current version number\n%e%a\n",
  289.                           usage);
  290.           }
  291.         case 'H':               /* Sets the name for the hash function */
  292.           {
  293.             option.hash_name = optarg;
  294.             break;
  295.           }
  296.         case 'i':               /* Sets the initial value for the associated values array. */
  297.           { 
  298.             if ((option.initial_asso_value = atoi (optarg)) < 0) 
  299.               report_error ("initial value %d must be non-zero, ignoring and continuing\n", 
  300.                             option.initial_asso_value);
  301.             if (OPTION_ENABLED (option, RANDOM))
  302.               report_error ("warning, -r option superceeds -i, ignoring -i option and continuing\n");
  303.             break;
  304.           }
  305.         case 'j':               /* Sets the jump value, must be odd for later algorithms. */
  306.           { 
  307.             if ((option.jump = atoi (optarg)) < 0)
  308.               report_error ("jump value %d must be a positive number\n%e%a", 
  309.               option.jump, usage);
  310.             else if (option.jump && EVEN (option.jump)) 
  311.               report_error ("jump value %d should be odd, adding 1 and continuing...\n", 
  312.               option.jump++);
  313.             break;
  314.           }
  315.         case 'k':               /* Sets key positions used for hash function. */
  316.           { 
  317.             int BAD_VALUE = -1;
  318.             int value;
  319.  
  320.             iterator_init (optarg, 1, MAX_KEY_POS - 1, WORD_END, BAD_VALUE, EOS);
  321.             
  322.             if (*optarg == '*') /* Use all the characters for hashing!!!! */
  323.               {
  324.                 UNSET_OPTION (option, DEFAULTCHARS);
  325.                 SET_OPTION (option, ALLCHARS);
  326.               }
  327.             else 
  328.               {
  329.                 char *key_pos;
  330.  
  331.                 for (key_pos = option.key_positions; (value = next ()) != EOS; key_pos++)
  332.                   if (value == BAD_VALUE) 
  333.                     report_error ("illegal key value or range, use 1,2,3-%d,'$' or '*'.\n%e%a", 
  334.                     (MAX_KEY_POS - 1),usage);
  335.                   else 
  336.                     *key_pos = value;;
  337.  
  338.                 *key_pos = EOS;
  339.  
  340.                 if (! (option.total_charset_size = (key_pos - option.key_positions))) 
  341.                   report_error ("no keys selected\n%e%a", usage);
  342.                 else if (! key_sort (option.key_positions, option.total_charset_size))
  343.                   report_error ("duplicate keys selected\n%e%a", usage);
  344.  
  345.                 if (option.total_charset_size != 2 
  346.                     || (option.key_positions[0] != 1 || option.key_positions[1] != WORD_END))
  347.                   UNSET_OPTION (option, DEFAULTCHARS);
  348.               }
  349.             break;
  350.           }
  351.         case 'K':               /* Make this the keyname for the keyword component field. */
  352.           {
  353.             option.key_name = optarg;
  354.             break;
  355.           }
  356.         case 'l':               /* Create length table to avoid extra string compares. */
  357.           { 
  358.             SET_OPTION (option, LENTABLE);
  359.             break;
  360.           }
  361.         case 'n':               /* Don't include the length when computing hash function. */
  362.           { 
  363.             SET_OPTION (option, NOLENGTH);
  364.             break; 
  365.           }
  366.         case 'N':               /* Make generated lookup function name be optarg */
  367.           { 
  368.             option.function_name = optarg;
  369.             break;
  370.           }
  371.         case 'o':               /* Order input by frequency of key set occurrence. */
  372.           { 
  373.             SET_OPTION (option, ORDER);
  374.             break;
  375.           }   
  376.         case 'p':               /* Generated lookup function now a pointer instead of int. */
  377.           { 
  378.             SET_OPTION (option, POINTER);
  379.             break;
  380.           }
  381.         case 'r':               /* Utilize randomness to initialize the associated values table. */
  382.           { 
  383.             SET_OPTION (option, RANDOM);
  384.             if (option.initial_asso_value != 0)
  385.               report_error ("warning, -r option superceeds -i, disabling -i option and continuing\n");
  386.             break;
  387.           }
  388.         case 's':               /* Range of associated values, determines size of final table. */
  389.           { 
  390.             if ((option.size = atoi (optarg)) <= 0) 
  391.               report_error ("improper range argument %s\n%e%a", optarg, usage);
  392.             else if (option.size > 50) 
  393.               report_error ("%d is excessive, did you really mean this?! (type %n -h for help)\n", 
  394.               option.size);
  395.             break; 
  396.           }       
  397.         case 'S':               /* Generate switch statement output, rather than lookup table. */
  398.           { 
  399.             SET_OPTION (option, SWITCH);
  400.             if ((option.total_switches = atoi (optarg)) <= 0)
  401.               report_error ("number of switches %s must be a positive number\n%e%a", optarg, usage);
  402.             break;
  403.           }
  404.         case 't':               /* Enable the TYPE mode, allowing arbitrary user structures. */
  405.           { 
  406.             SET_OPTION (option, TYPE);
  407.             break;
  408.           }
  409.         case 'T':               /* Don't print structure definition. */
  410.           {
  411.             SET_OPTION (option, NOTYPE);
  412.             break;
  413.           }
  414.         case 'v':               /* Print out the version and quit. */
  415.           report_error ("%n: version %s\n%e%a\n", version_string, usage);
  416.         default: 
  417.           report_error ("%e%a", usage);
  418.         }
  419.     }
  420.  
  421.   if (argv[optind] && ! freopen (argv[optind], "r", stdin))
  422.     report_error ("unable to read key word file %s\n%e%a", argv[optind], usage);
  423.   
  424.   if (++optind < argc) 
  425.     report_error ("extra trailing arguments to %n\n%e%a", usage);
  426. }
  427.  
  428. /* Output command-line Options. */
  429. void 
  430. print_options ()
  431.   int i;
  432.  
  433.   printf ("/* Command-line: ");
  434.  
  435.   for (i = 0; i < option.argument_count; i++) 
  436.     printf ("%s ", option.argument_vector[i]);
  437.    
  438.   printf (" */\n\n");
  439. }
  440.  
  441.