Changeset 237
- Timestamp:
- Feb 26, 2006 11:17:28 PM (19 years ago)
- Location:
- pjproject/trunk/pjsip/src/pjsua
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip/src/pjsua/getopt.c
r184 r237 18 18 */ 19 19 20 #ifdef _MSC_VER21 /* in VC this file will generate a lot of warning about old style function22 * declarations.23 */24 # pragma warning(push, 3)25 #endif26 27 20 /* 28 21 * getopt entry points … … 33 26 */ 34 27 35 #ifdef HAVE_CONFIG_H 36 #include "config.h" 37 #endif 38 28 #include "getopt.h" 39 29 #include <pj/string.h> 40 30 41 #ifndef HAVE_GETOPT_LONG 31 /* Internal only. Users should not call this directly. */ 32 static 33 int _getopt_internal (int argc, char *const *argv, 34 const char *shortopts, 35 const struct option *longopts, int *longind, 36 int long_only); 42 37 43 38 /* getopt_long and getopt_long_only entry points for GNU getopt. … … 60 55 Boston, MA 02111-1307, USA. */ 61 56 62 #include "getopt.h"63 64 65 #include <stdio.h>66 57 67 58 /* Comment out all this code if we are using the GNU C Library, and are not … … 73 64 it is simpler to just do this in the source for each such file. */ 74 65 75 #define GETOPT_INTERFACE_VERSION 2 76 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 77 #include <gnu-versions.h> 78 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 79 #define ELIDE_CODE 80 #endif 81 #endif 82 83 #ifndef ELIDE_CODE 84 85 86 /* This needs to come after some library #include 87 to get __GNU_LIBRARY__ defined. */ 88 #ifdef __GNU_LIBRARY__ 89 #include <stdlib.h> 90 #endif 91 92 #ifndef NULL 93 #define NULL 0 94 #endif 66 # define GETOPT_INTERFACE_VERSION 2 67 95 68 96 69 int … … 106 79 instead. */ 107 80 108 int109 getopt_long_only (argc, argv, options, long_options, opt_index)110 int argc;111 char *const *argv;112 const char *options;113 const struct option *long_options;114 int *opt_index;115 {116 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);117 }118 119 81 int 120 82 getopt (int argc, char * const * argv, const char * optstring) … … 126 88 } 127 89 128 #endif /* Not ELIDE_CODE. */ 129 130 131 #ifndef _NO_PROTO 132 #define _NO_PROTO 133 #endif 134 135 136 //#include <strings.h> 137 138 /* Comment out all this code if we are using the GNU C Library, and are not 139 actually compiling the library itself. This code is part of the GNU C 140 Library, but also included in many other GNU distributions. Compiling 141 and linking in this code is a waste when using the GNU C library 142 (especially if it is a shared library). Rather than having every GNU 143 program understand `configure --with-gnu-libc' and omit the object files, 144 it is simpler to just do this in the source for each such file. */ 145 146 #define GETOPT_INTERFACE_VERSION 2 147 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 148 #include <gnu-versions.h> 149 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 150 #define ELIDE_CODE 151 #endif 152 #endif 153 154 #ifndef ELIDE_CODE 155 156 157 /* This needs to come after some library #include 158 to get __GNU_LIBRARY__ defined. */ 159 #ifdef __GNU_LIBRARY__ 160 /* Don't include stdlib.h for non-GNU C libraries because some of them 161 contain conflicting prototypes for getopt. */ 162 #include <stdlib.h> 163 #include <unistd.h> 164 #endif /* GNU C library. */ 165 166 #ifdef VMS 167 #include <unixlib.h> 168 #if HAVE_STRING_H - 0 169 #include <string.h> 170 #endif 171 #endif 172 173 #if defined (WIN32) && !defined (__CYGWIN32__) 174 /* It's not Unix, really. See? Capital letters. */ 175 #include <windows.h> 176 #define getpid() GetCurrentProcessId() 177 #endif 178 179 #ifndef _ 180 /* This is for other GNU distributions with internationalized messages. 181 When compiling libc, the _ macro is predefined. */ 182 #ifdef HAVE_LIBINTL_H 183 # include <libintl.h> 184 # define _(msgid) gettext (msgid) 185 #else 186 # define _(msgid) (msgid) 187 #endif 188 #endif 90 91 #define _(msgid) (msgid) 189 92 190 93 /* This version of `getopt' appears to the caller like standard Unix `getopt' … … 201 104 GNU application programs can use a third alternative mode in which 202 105 they can distinguish the relative order of options and other arguments. */ 203 204 #include "getopt.h"205 106 206 107 /* For communication from `getopt' to the caller. … … 241 142 242 143 static char *nextchar; 243 244 /* Callers store zero here to inhibit the error message245 for unrecognized options. */246 247 int opterr = 1;248 144 249 145 /* Set to an option character which was unrecognized. … … 289 185 /* Value of POSIXLY_CORRECT environment variable. */ 290 186 static char *posixly_correct; 291 292 #ifdef __GNU_LIBRARY__293 /* We want to avoid inclusion of string.h with non-GNU libraries294 because there are many ways it can cause trouble.295 On some systems, it contains special magic macros that don't work296 in GCC. */297 #include <string.h>298 #define my_index pj_ansi_strchr299 #else300 187 301 188 static char * … … 311 198 } 312 199 313 /* If using GCC, we can safely declare strlen this way.314 If not using GCC, it is ok not to declare it. */315 #ifdef __GNUC__316 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.317 That was relevant to code that was here before. */318 #if !defined (__STDC__) || !__STDC__319 /* gcc with -traditional declares the built-in strlen to return int,320 and has done so at least since version 2.4.5. -- rms. */321 extern int strlen (const char *);322 #endif /* not __STDC__ */323 #endif /* __GNUC__ */324 325 #endif /* not __GNU_LIBRARY__ */326 200 327 201 /* Handle permutation of arguments. */ … … 334 208 static int last_nonopt; 335 209 336 #ifdef _LIBC 337 /* Bash 2.0 gives us an environment variable containing flags 338 indicating ARGV elements that should not be considered arguments. */ 339 340 /* Defined in getopt_init.c */ 341 extern char *__getopt_nonoption_flags; 342 343 static int nonoption_flags_max_len; 344 static int nonoption_flags_len; 345 346 static int original_argc; 347 static char *const *original_argv; 348 349 extern pid_t __libc_pid; 350 351 /* Make sure the environment variable bash 2.0 puts in the environment 352 is valid for the getopt call we must make sure that the ARGV passed 353 to getopt is that one passed to the process. */ 354 static void 355 __attribute__ ((unused)) 356 store_args_and_env (int argc, char *const *argv) 357 { 358 /* XXX This is no good solution. We should rather copy the args so 359 that we can compare them later. But we must not use malloc(3). */ 360 original_argc = argc; 361 original_argv = argv; 362 } 363 text_set_element (__libc_subinit, store_args_and_env); 364 365 # define SWAP_FLAGS(ch1, ch2) \ 366 if (nonoption_flags_len > 0) \ 367 { \ 368 char __tmp = __getopt_nonoption_flags[ch1]; \ 369 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 370 __getopt_nonoption_flags[ch2] = __tmp; \ 371 } 372 #else /* !_LIBC */ 373 # define SWAP_FLAGS(ch1, ch2) 374 #endif /* _LIBC */ 210 # define SWAP_FLAGS(ch1, ch2) 375 211 376 212 /* Exchange two adjacent subsequences of ARGV. … … 383 219 the new indices of the non-options in ARGV after they are moved. */ 384 220 385 #if defined (__STDC__) && __STDC__386 static void exchange (char **);387 #endif388 389 221 static void 390 exchange (argv) 391 char **argv; 222 exchange (char **argv) 392 223 { 393 224 int bottom = first_nonopt; … … 400 231 It leaves the longer segment in the right place overall, 401 232 but it consists of two parts that need to be swapped next. */ 402 403 #ifdef _LIBC404 /* First make sure the handling of the `__getopt_nonoption_flags'405 string can work normally. Our top argument must be in the range406 of the string. */407 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)408 {409 /* We must extend the array. The user plays games with us and410 presents new arguments. */411 char *new_str = malloc (top + 1);412 if (new_str == NULL)413 nonoption_flags_len = nonoption_flags_max_len = 0;414 else415 {416 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);417 memset (&new_str[nonoption_flags_max_len], '\0',418 top + 1 - nonoption_flags_max_len);419 nonoption_flags_max_len = top + 1;420 __getopt_nonoption_flags = new_str;421 }422 }423 #endif424 233 425 234 while (top > middle && middle > bottom) … … 469 278 /* Initialize the internal data when the first call is made. */ 470 279 471 #if defined (__STDC__) && __STDC__ 472 static const char *_getopt_initialize (int, char *const *, const char *); 473 #endif 474 static const char * 475 _getopt_initialize (argc, argv, optstring) 476 int argc; 477 char *const *argv; 478 const char *optstring; 280 static const char *_getopt_initialize (int argc, char *const *argv, 281 const char *optstring) 479 282 { 283 PJ_UNUSED_ARG(argc); 284 PJ_UNUSED_ARG(argv); 285 480 286 /* Start processing options with ARGV-element 1 (since ARGV-element 0 481 287 is the program name); the sequence of previously skipped … … 506 312 ordering = PERMUTE; 507 313 508 #ifdef _LIBC509 if (posixly_correct == NULL510 && argc == original_argc && argv == original_argv)511 {512 if (nonoption_flags_max_len == 0)513 {514 if (__getopt_nonoption_flags == NULL515 || __getopt_nonoption_flags[0] == '\0')516 nonoption_flags_max_len = -1;517 else518 {519 const char *orig_str = __getopt_nonoption_flags;520 int len = nonoption_flags_max_len = strlen (orig_str);521 if (nonoption_flags_max_len < argc)522 nonoption_flags_max_len = argc;523 __getopt_nonoption_flags =524 (char *) malloc (nonoption_flags_max_len);525 if (__getopt_nonoption_flags == NULL)526 nonoption_flags_max_len = -1;527 else528 {529 memcpy (__getopt_nonoption_flags, orig_str, len);530 memset (&__getopt_nonoption_flags[len], '\0',531 nonoption_flags_max_len - len);532 }533 }534 }535 nonoption_flags_len = nonoption_flags_max_len;536 }537 else538 nonoption_flags_len = 0;539 #endif540 541 314 return optstring; 542 315 } … … 598 371 long-named options. */ 599 372 600 int 601 _getopt_internal (argc, argv, optstring, longopts, longind, long_only) 602 int argc; 603 char *const *argv; 604 const char *optstring; 605 const struct option *longopts; 606 int *longind; 607 int long_only; 373 static int 374 _getopt_internal (int argc, char *const *argv, const char *optstring, 375 const struct option *longopts, int *longind, 376 int long_only) 608 377 { 609 378 optarg = NULL; … … 621 390 from the shell indicating it is not an option. The later information 622 391 is only used when the used in the GNU libc. */ 623 #ifdef _LIBC624 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \625 || (optind < nonoption_flags_len \626 && __getopt_nonoption_flags[optind] == '1'))627 #else628 392 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 629 #endif630 393 631 394 if (nextchar == NULL || *nextchar == '\0') … … 763 526 if (ambig && !exact) 764 527 { 765 if (opterr)766 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),767 argv[0], argv[optind]);768 528 nextchar += strlen (nextchar); 769 529 optind++; … … 784 544 else 785 545 { 786 if (opterr)787 {788 if (argv[optind - 1][1] == '-')789 /* --option */790 fprintf (stderr,791 _("%s: option `--%s' doesn't allow an argument\n"),792 argv[0], pfound->name);793 else794 {795 /* +option or -option */796 fprintf (stderr,797 _("%s: option `%c%s' doesn't allow an argument\n"),798 argv[0], argv[optind - 1][0], pfound->name);799 }800 }801 546 nextchar += strlen (nextchar); 802 547 … … 811 556 else 812 557 { 813 if (opterr)814 fprintf (stderr,815 _("%s: option `%s' requires an argument\n"),816 argv[0], argv[optind - 1]);817 558 nextchar += strlen (nextchar); 818 559 optopt = pfound->val; … … 838 579 || my_index (optstring, *nextchar) == NULL) 839 580 { 840 if (opterr)841 {842 if (argv[optind][1] == '-')843 /* --option */844 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),845 argv[0], nextchar);846 else847 /* +option or -option */848 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),849 argv[0], argv[optind][0], nextchar);850 }851 581 nextchar = (char *) ""; 852 582 optind++; … … 868 598 if (temp == NULL || c == ':') 869 599 { 870 if (opterr)871 {872 if (posixly_correct)873 /* 1003.2 specifies the format of this message. */874 fprintf (stderr, _("%s: illegal option -- %c\n"),875 argv[0], c);876 else877 fprintf (stderr, _("%s: invalid option -- %c\n"),878 argv[0], c);879 }880 600 optopt = c; 881 601 return '?'; … … 902 622 else if (optind == argc) 903 623 { 904 if (opterr)905 {906 /* 1003.2 specifies the format of this message. */907 fprintf (stderr, _("%s: option requires an argument -- %c\n"),908 argv[0], c);909 }910 624 optopt = c; 911 625 if (optstring[0] == ':') … … 951 665 if (ambig && !exact) 952 666 { 953 if (opterr)954 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),955 argv[0], argv[optind]);956 667 nextchar += strlen (nextchar); 957 668 optind++; … … 969 680 else 970 681 { 971 if (opterr)972 fprintf (stderr, _("\973 %s: option `-W %s' doesn't allow an argument\n"),974 argv[0], pfound->name);975 976 682 nextchar += strlen (nextchar); 977 683 return '?'; … … 984 690 else 985 691 { 986 if (opterr)987 fprintf (stderr,988 _("%s: option `%s' requires an argument\n"),989 argv[0], argv[optind - 1]);990 692 nextchar += strlen (nextchar); 991 693 return optstring[0] == ':' ? ':' : '?'; … … 1031 733 else if (optind == argc) 1032 734 { 1033 if (opterr)1034 {1035 /* 1003.2 specifies the format of this message. */1036 fprintf (stderr,1037 _("%s: option requires an argument -- %c\n"),1038 argv[0], c);1039 }1040 735 optopt = c; 1041 736 if (optstring[0] == ':') … … 1055 750 } 1056 751 1057 #endif /* Not ELIDE_CODE. */1058 1059 #endif1060 1061 #ifdef _MSC_VER1062 # pragma warning(pop)1063 #endif1064 -
pjproject/trunk/pjsip/src/pjsua/getopt.h
r167 r237 128 128 const struct option *longopts, int *longind); 129 129 130 /* Internal only. Users should not call this directly. */131 int _getopt_internal (int argc, char *const *argv,132 const char *shortopts,133 const struct option *longopts, int *longind,134 int long_only);135 130 136 131 #ifdef __cplusplus
Note: See TracChangeset
for help on using the changeset viewer.