PPL Logo

libs/ck-libs/metis/GKlib/getopt.c File Reference

Command line parsing. More...

Go to the source code of this file.

Enumerations

enum  { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }

Functions

static void exchange (char **)
static char * gk_getopt_initialize (int, char **, char *)
static int gk_getopt_internal (int argc, char **argv, char *optstring, struct gk_option *longopts, int *longind, int long_only)
int gk_getopt (int argc, char **argv, char *options)
 Parse command-line arguments.
int gk_getopt_long (int argc, char **argv, char *options, struct gk_option *long_options, int *opt_index)
 Parse command-line arguments with long options.
int gk_getopt_long_only (int argc, char **argv, char *options, struct gk_option *long_options, int *opt_index)
 Parse command-line arguments with only long options.

Variables

CMK_THREADLOCAL char * gk_optarg
 For communication arguments to the caller.
CMK_THREADLOCAL int gk_optind = 1
 Index in ARGV of the next element to be scanned.
CMK_THREADLOCAL int gk_opterr = 1
 Controls error reporting for unrecognized options.
CMK_THREADLOCAL int gk_optopt = '?'
 Stores unknown option characters.
CMK_THREADLOCAL int gk_getopt_initialized
static CMK_THREADLOCAL char * nextchar
static CMK_THREADLOCAL char * posixly_correct
static enum { ... }  ordering
static CMK_THREADLOCAL int first_nonopt
static CMK_THREADLOCAL int last_nonopt


Detailed Description

Command line parsing.

This file contains a implementation of GNU's Getopt facility. The purpose for including it here is to ensure portability across different unix- and windows-based systems.

Warning:
The implementation provided here uses the gk_ prefix for all variables used by the standard Getopt facility to communicate with the program. So, do read the documentation here.
   Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
   Free Software Foundation, Inc. This file is part of the GNU C Library.

   The GNU C Library 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 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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 the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  

This file in metis was modified by Kavitha Chandrasekar at UIUC

Definition in file getopt.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
REQUIRE_ORDER 
PERMUTE 
RETURN_IN_ORDER 

Definition at line 156 of file getopt.c.


Function Documentation

static void exchange ( char **  argv  )  [static]

Definition at line 191 of file getopt.c.

References first_nonopt, gk_optind, last_nonopt, and len.

static char * gk_getopt_initialize ( int  argc,
char **  argv,
char *  optstring 
) [static]

Definition at line 247 of file getopt.c.

References first_nonopt, gk_optind, last_nonopt, nextchar, ordering, PERMUTE, posixly_correct, REQUIRE_ORDER, and RETURN_IN_ORDER.

Referenced by gk_getopt_internal().

Here is the caller graph for this function:

static int gk_getopt_internal ( int  argc,
char **  argv,
char *  optstring,
struct gk_option longopts,
int longind,
int  long_only 
) [static]

Definition at line 332 of file getopt.c.

References c, exchange(), first_nonopt, gk_option::flag, gk_getopt_initialize(), gk_getopt_initialized, gk_optarg, gk_opterr, gk_optind, gk_optopt, gk_option::has_arg, last_nonopt, gk_option::name, nextchar, ordering, p, PERMUTE, posixly_correct, REQUIRE_ORDER, and gk_option::val.

Referenced by gk_getopt(), gk_getopt_long(), and gk_getopt_long_only().

Here is the call graph for this function:

Here is the caller graph for this function:

int gk_getopt ( int  argc,
char **  argv,
char *  options 
)

Parse command-line arguments.

The gk_getopt() function gets the next option argument from the argument list specified by the argv and argc arguments. Normally these values come directly from the arguments received by main().

Parameters:
argc is the number of command line arguments passed to main().
argv is an array of strings storing the above command line arguments.
options is a string that specifies the option characters that are valid for this program. An option character in this string can be followed by a colon (`:') to indicate that it takes a required argument. If an option character is followed by two colons (`::'), its argument is optional; this is a GNU extension.
Returns:
It returns the option character for the next command line option. When no more option arguments are available, it returns -1. There may still be more non-option arguments; you must compare the external variable gk_optind against the argc parameter to check this.

If the option has an argument, gk_getopt() returns the argument by storing it in the variable gk_optarg. You don't ordinarily need to copy the gk_optarg string, since it is a pointer into the original argv array, not into a static area that might be overwritten.

If gk_getopt() finds an option character in argv that was not included in options, or a missing option argument, it returns `?' and sets the external variable gk_optopt to the actual option character. If the first character of options is a colon (`:'), then gk_getopt() returns `:' instead of `?' to indicate a missing option argument. In addition, if the external variable gk_opterr is nonzero (which is the default), gk_getopt() prints an error message. This variable is set by gk_getopt() to point at the value of the option argument, for those options that accept arguments.

gk_getopt() has three ways to deal with options that follow non-options argv elements. The special argument `--' forces in all cases the end of option scanning.
  • The default is to permute the contents of argv while scanning it so that eventually all the non-options are at the end. This allows options to be given in any order, even with programs that were not written to expect this.
  • If the options argument string begins with a hyphen (`-'), this is treated specially. It permits arguments that are not options to be returned as if they were associated with option character `\1'.
  • POSIX demands the following behavior: The first non-option stops option processing. This mode is selected by either setting the environment variable POSIXLY_CORRECT or beginning the options argument string with a plus sign (`+').

Definition at line 771 of file getopt.c.

References gk_getopt_internal().

Here is the call graph for this function:

int gk_getopt_long ( int  argc,
char **  argv,
char *  options,
struct gk_option long_options,
int opt_index 
)

Parse command-line arguments with long options.

This function accepts GNU-style long options as well as single-character options.

Parameters:
argc is the number of command line arguments passed to main().
argv is an array of strings storing the above command line arguments.
options describes the short options to accept, just as it does in gk_getopt().
long_options describes the long options to accept. See the defintion of gk_option for more information.
opt_index this is a returned variable. For any long option, gk_getopt_long() tells you the index in the array long_options of the options definition, by storing it into *opt_index. You can get the name of the option with longopts[*opt_index].name. So you can distinguish among long options either by the values in their val fields or by their indices. You can also distinguish in this way among long options that set flags.
Returns:
When gk_getopt_long() encounters a short option, it does the same thing that gk_getopt() would do: it returns the character code for the option, and stores the options argument (if it has one) in gk_optarg.

When gk_getopt_long() encounters a long option, it takes actions based on the flag and val fields of the definition of that option.

If flag is a null pointer, then gk_getopt_long() returns the contents of val to indicate which option it found. You should arrange distinct values in the val field for options with different meanings, so you can decode these values after gk_getopt_long() returns. If the long option is equivalent to a short option, you can use the short option's character code in val.

If flag is not a null pointer, that means this option should just set a flag in the program. The flag is a variable of type int that you define. Put the address of the flag in the flag field. Put in the val field the value you would like this option to store in the flag. In this case, gk_getopt_long() returns 0.

When a long option has an argument, gk_getopt_long() puts the argument value in the variable gk_optarg before returning. When the option has no argument, the value in gk_optarg is a null pointer. This is how you can tell whether an optional argument was supplied.

When gk_getopt_long() has no more options to handle, it returns -1, and leaves in the variable gk_optind the index in argv of the next remaining argument.

Definition at line 835 of file getopt.c.

References gk_getopt_internal().

Here is the call graph for this function:

int gk_getopt_long_only ( int  argc,
char **  argv,
char *  options,
struct gk_option long_options,
int opt_index 
)

Parse command-line arguments with only long options.

Like gk_getopt_long(), but '-' as well as '--' can indicate a long option. If an option that starts with '-' (not '--') doesn't match a long option, but does match a short option, it is parsed as a short option instead.

Definition at line 851 of file getopt.c.

References gk_getopt_internal().

Referenced by parse_cmdline().

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

CMK_THREADLOCAL char* gk_optarg

For communication arguments to the caller.

This variable is set by getopt to point at the value of the option argument, for those options that accept arguments.

Definition at line 58 of file getopt.c.

Referenced by gk_getopt_internal(), and parse_cmdline().

CMK_THREADLOCAL int gk_optind = 1

Index in ARGV of the next element to be scanned.

This variable is set by getopt to the index of the next element of the argv array to be processed. Once getopt has found all of the option arguments, you can use this variable to determine where the remaining non-option arguments begin.

Definition at line 70 of file getopt.c.

Referenced by exchange(), gk_getopt_initialize(), gk_getopt_internal(), and parse_cmdline().

CMK_THREADLOCAL int gk_opterr = 1

Controls error reporting for unrecognized options.

If the value of this variable is nonzero, then getopt prints an error message to the standard error stream if it encounters an unknown option character or an option with a missing required argument. This is the default behavior. If you set this variable to zero, getopt does not print any messages, but it still returns the character ? to indicate an error.

Definition at line 83 of file getopt.c.

Referenced by gk_getopt_internal().

CMK_THREADLOCAL int gk_optopt = '?'

Stores unknown option characters.

When getopt encounters an unknown option character or an option with a missing required argument, it stores that option character in this variable. You can use this for providing your own diagnostic messages.

Definition at line 94 of file getopt.c.

Referenced by gk_getopt_internal().

CMK_THREADLOCAL int gk_getopt_initialized

Definition at line 102 of file getopt.c.

Referenced by gk_getopt_internal().

CMK_THREADLOCAL char* nextchar [static]

Definition at line 115 of file getopt.c.

Referenced by gk_getopt_initialize(), and gk_getopt_internal().

CMK_THREADLOCAL char* posixly_correct [static]

Definition at line 123 of file getopt.c.

Referenced by gk_getopt_initialize(), and gk_getopt_internal().

enum { ... } ordering [static]

CMK_THREADLOCAL int first_nonopt [static]

Definition at line 170 of file getopt.c.

Referenced by exchange(), gk_getopt_initialize(), and gk_getopt_internal().

CMK_THREADLOCAL int last_nonopt [static]

Definition at line 171 of file getopt.c.

Referenced by exchange(), gk_getopt_initialize(), and gk_getopt_internal().


Generated on Mon Sep 21 08:08:02 2020 for Charm++ by  doxygen 1.5.5