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
|
/*
* GetOpt functions
*
* Copyright (C) 2011-2020, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* 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 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 <https://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <narrow_string.h>
#include <system_string.h>
#include <types.h>
#include <wide_string.h>
#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
#include <stdlib.h>
#endif
#include "evt_test_getopt.h"
#include "evt_test_libcnotify.h"
#if !defined( HAVE_GETOPT )
/* The option index
* Start with argument 1 (argument 0 is the program name)
*/
int optind = 1;
/* The current option argument
*/
system_character_t *optarg = NULL;
/* Value to indicate the current option
*/
system_integer_t optopt = 0;
/* The next option in a group
*/
system_character_t *next_option = NULL;
/* Get the program options
* Function for platforms that do not have the getopt function
* Returns the option character processed, or -1 on error,
* ? if the option was not in the options string, : if the option argument was missing
*/
system_integer_t evt_test_getopt(
int argument_count,
system_character_t * const argument_values[],
const system_character_t *options_string )
{
system_character_t *argument_value = NULL;
system_character_t *option_value = NULL;
static char *function = "evt_test_getopt";
size_t options_string_length = 0;
if( next_option != NULL )
{
argument_value = next_option;
next_option = NULL;
}
else if( optind >= argument_count )
{
return( (system_integer_t) -1 );
}
else
{
argument_value = argument_values[ optind ];
/* Check if the argument value is not an empty string
*/
if( *argument_value == (system_character_t) '\0' )
{
return( (system_integer_t) -1 );
}
/* Check if the first character is a option marker '-'
*/
if( *argument_value != (system_character_t) '-' )
{
return( (system_integer_t) -1 );
}
argument_value++;
/* Check if long options are provided '--'
*/
if( *argument_value == (system_character_t) '-' )
{
optind++;
return( (system_integer_t) -1 );
}
}
options_string_length = system_string_length(
options_string );
optopt = *argument_value;
option_value = system_string_search_character(
options_string,
optopt,
options_string_length );
argument_value++;
/* Check if an argument was specified or that the option was not found
* in the option string
*/
if( ( optopt == (system_integer_t) ':' )
|| ( option_value == NULL ) )
{
if( *argument_value == (system_character_t) '\0' )
{
optind++;
}
if( ( *options_string != (system_character_t) ':' )
&& ( optopt != (system_integer_t) '?' ) )
{
libcnotify_printf(
"%s: no such option: %" PRIc_SYSTEM ".\n",
function,
optopt );
}
return( (system_integer_t) '?' );
}
option_value++;
/* Check if no option argument is required
*/
if( *option_value != (system_character_t) ':' )
{
optarg = NULL;
if( *argument_value == (system_character_t) '\0' )
{
optind++;
}
else
{
/* Multiple options are grouped
*/
next_option = argument_value;
}
}
/* Check if the argument is right after the option flag with no space in between
*/
else if( *argument_value != (system_character_t) '\0' )
{
optarg = argument_value;
optind++;
}
else
{
optind++;
/* Check if the argument was provided as the next argument value
*/
if( argument_count <= optind )
{
if( *option_value == ':' )
{
return( (system_integer_t) ':' );
}
libcnotify_printf(
"%s: option: %" PRIc_SYSTEM " requires an argument.\n",
function,
optopt );
return( (system_integer_t) '?' );
}
optarg = argument_values[ optind ];
optind++;
}
return( optopt );
}
#endif /* !defined( HAVE_GETOPT ) */
|