Changeset 930
- Timestamp:
- Jan 2, 2004, 8:37:04 PM (22 years ago)
- Location:
- trunk/src/emx
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/emx/include/emx/getopt.h
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.2
r929 r930 8 8 #endif 9 9 10 extern char * optarg; /* argument of current option */11 extern int optind; /* index of next argument; default=0: initialize */12 extern int opterr; /* 0=disable error messages; default=1: enable */13 extern int optopt; /* option character which caused the error */14 extern char * optswchar; /* characters introducing options; default="-" */10 extern char *_emx_optarg; /* argument of current option */ 11 extern int _emx_optind; /* index of next argument; default=0: initialize */ 12 extern int _emx_opterr; /* 0=disable error messages; default=1: enable */ 13 extern int _emx_optopt; /* option character which caused the error */ 14 extern char *_emx_optswchar; /* characters introducing options; default="-" */ 15 15 16 16 extern enum _optmode … … 19 19 GETOPT_ANY, /* move non-options to the end */ 20 20 GETOPT_KEEP /* return options in order */ 21 } optmode;21 } _emx_optmode; 22 22 23 23 … … 25 25 array pointed to. */ 26 26 27 int getopt (int, char **, __const__ char *);27 int _emx_getopt (int, char **, __const__ char *); 28 28 29 29 #if defined (__cplusplus) -
Property cvs2svn:cvs-rev
changed from
-
trunk/src/emx/src/lib/misc/getopt.c
-
Property cvs2svn:cvs-rev
changed from
1.6
to1.7
r929 r930 8 8 #include <emx/umalloc.h> 9 9 10 char * optarg = NULL;11 int optind = 1; /* Default: first call */12 int opterr = 1; /* Default: error messages enabled */13 char * optswchar = "-"; /* Default: '-' starts options */14 enum _optmode optmode = GETOPT_UNIX;15 int optopt;10 char *_emx_optarg = NULL; 11 int _emx_optind = 1; /* Default: first call */ 12 int _emx_opterr = 1; /* Default: error messages enabled */ 13 char *_emx_optswchar = "-"; /* Default: '-' starts options */ 14 enum _optmode _emx_optmode = GETOPT_UNIX; 15 int _emx_optopt; 16 16 17 17 static char * next_opt; /* Next character in cluster of options */ … … 30 30 31 31 #define PUT(dst) BEGIN \ 32 if ( optmode == GETOPT_ANY) \33 dst[dst##_count++] = argv[ optind]; \32 if (_emx_optmode == GETOPT_ANY) \ 33 dst[dst##_count++] = argv[_emx_optind]; \ 34 34 END 35 35 36 36 /* Note: `argv' is not const as GETOPT_ANY reorders argv[]. */ 37 37 38 int _ STD(getopt)(int argc, char * argv[], const char *opt_str)38 int _emx_getopt (int argc, char * argv[], const char *opt_str) 39 39 { 40 40 char c, *q; 41 41 int i, j; 42 42 43 if (!init || optind == 0)43 if (!init || _emx_optind == 0) 44 44 { 45 if ( optind == 0)optind = 1;45 if (_emx_optind == 0) _emx_optind = 1; 46 46 done = 0; init = 1; 47 47 next_opt = ""; 48 if ( optmode == GETOPT_ANY)48 if (_emx_optmode == GETOPT_ANY) 49 49 { 50 50 options = (char **)_hmalloc (argc * sizeof (char *)); … … 61 61 return -1; 62 62 restart: 63 optarg = NULL;63 _emx_optarg = NULL; 64 64 if (*next_opt == 0) 65 65 { 66 if ( optind >= argc)66 if (_emx_optind >= argc) 67 67 { 68 if ( optmode == GETOPT_ANY)68 if (_emx_optmode == GETOPT_ANY) 69 69 { 70 70 j = 1; … … 73 73 for (i = 0; i < non_options_count; ++i) 74 74 argv[j++] = non_options[i]; 75 optind = options_count+1;75 _emx_optind = options_count+1; 76 76 free (options); free (non_options); 77 77 } … … 79 79 return -1; 80 80 } 81 else if (!strchr ( optswchar, argv[optind][0]) || argv[optind][1] == 0)81 else if (!strchr (_emx_optswchar, argv[_emx_optind][0]) || argv[_emx_optind][1] == 0) 82 82 { 83 if ( optmode == GETOPT_UNIX)83 if (_emx_optmode == GETOPT_UNIX) 84 84 { 85 85 done = 1; … … 87 87 } 88 88 PUT (non_options); 89 optarg = argv[optind++];90 if ( optmode == GETOPT_ANY)89 _emx_optarg = argv[_emx_optind++]; 90 if (_emx_optmode == GETOPT_ANY) 91 91 goto restart; 92 /* optmode==GETOPT_KEEP */92 /* _emx_optmode==GETOPT_KEEP */ 93 93 return 0; 94 94 } 95 else if (argv[ optind][0] == argv[optind][1] && argv[optind][2] == 0)95 else if (argv[_emx_optind][0] == argv[_emx_optind][1] && argv[_emx_optind][2] == 0) 96 96 { 97 if ( optmode == GETOPT_ANY)97 if (_emx_optmode == GETOPT_ANY) 98 98 { 99 99 j = 1; 100 100 for (i = 0; i < options_count; ++i) 101 101 argv[j++] = options[i]; 102 argv[j++] = argv[ optind];102 argv[j++] = argv[_emx_optind]; 103 103 for (i = 0; i < non_options_count; ++i) 104 104 argv[j++] = non_options[i]; 105 for (i = optind+1; i < argc; ++i)105 for (i = _emx_optind+1; i < argc; ++i) 106 106 argv[j++] = argv[i]; 107 optind = options_count+2;107 _emx_optind = options_count+2; 108 108 free (options); free (non_options); 109 109 } 110 ++ optind;110 ++_emx_optind; 111 111 done = 1; 112 112 return -1; … … 115 115 { 116 116 PUT (options); 117 sw_char = argv[ optind][0];118 next_opt = argv[ optind]+1;117 sw_char = argv[_emx_optind][0]; 118 next_opt = argv[_emx_optind]+1; 119 119 } 120 120 } 121 121 c = *next_opt++; 122 122 if (*next_opt == 0) /* Move to next argument if end of argument reached */ 123 ++ optind;123 ++_emx_optind; 124 124 if (c == ':' || (q = strchr (opt_str, c)) == NULL) 125 125 { 126 if ( opterr && opt_str[0] != ':')126 if (_emx_opterr && opt_str[0] != ':') 127 127 { 128 128 if (c < ' ' || c >= 127) … … 133 133 argv[0], sw_char, c); 134 134 } 135 optopt = c;135 _emx_optopt = c; 136 136 return '?'; 137 137 } … … 140 140 if (*next_opt != 0) /* Argument given */ 141 141 { 142 optarg = next_opt;142 _emx_optarg = next_opt; 143 143 next_opt = ""; 144 ++ optind;144 ++_emx_optind; 145 145 } 146 146 else if (q[2] == ':') 147 optarg = NULL; /* Optional argument missing */148 else if ( optind >= argc)147 _emx_optarg = NULL; /* Optional argument missing */ 148 else if (_emx_optind >= argc) 149 149 { /* Required argument missing */ 150 if ( opterr && opt_str[0] != ':')150 if (_emx_opterr && opt_str[0] != ':') 151 151 fprintf (stderr, "%s: no argument for `%c%c' option\n", 152 152 argv[0], sw_char, c); 153 optopt = c;153 _emx_optopt = c; 154 154 return (opt_str[0] == ':' ? ':' : '?'); 155 155 } … … 157 157 { 158 158 PUT (options); 159 optarg = argv[optind++];159 _emx_optarg = argv[_emx_optind++]; 160 160 } 161 161 } -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.