File: getopt_w32.c

package info (click to toggle)
poker-eval 134.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,544 kB
  • ctags: 1,587
  • sloc: ansic: 15,340; sh: 8,928; cs: 785; makefile: 276
file content (184 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download | duplicates (2)
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
#include "getopt_w32.h"

/*
*  g e t o p t . c
* 
*  This file contains the following routines:
* 
*       getopt -- get command line option letter
* 
*++ 
*  NAME
*       getopt -- get command line option letter
* 
*  SYNOPSIS
*       int getopt(argc, argv, optstring)
*       int argc;
*       char *argv[];
*       char *optstring;
*
*       extern char *optarg;
*       extern int optind, opterr;
*       extern char switch_char;
*       
*  DESCRIPTION
*       The function getopt is a command-line parser.  It returns the 
*       next option letter in argv that matches a letter in optstring.  
*
*       getopt places in the external variable optind the argv index 
*       of the next argument to be processed.  optind is initialized 
*       to 1 before the first call to getopt.
*
*       optstring is a string of recognized option letters;  if a letter 
*       is followed by a colon, the option is expected to have an argument 
*       that must be separated from it by white space.  optarg is set to 
*       point to the start of the option argument on return from getopt.
*
*       getopt places in optind the argv index of the next argument
*       to be processed.
*
*       When all options have been processed (i.e., up to the first
*       non-option argument), getopt returns EOF (-1), optarg will 
*       point to the argument, and optind will be set to the argv index
*       of the argument.  If there are no non-option arguments, optarg 
*       will be set to NULL.
*
*       The special option "//" may be used to delimit the end of the 
*       options;  EOF will be returned, and "//" (and everything after it) 
*       will be skipped.
* 
*  RETURN VALUE
*       For option letters contained in the string optstring, getopt 
*       will return the option letter.  getopt returns a question mark (?) 
*       when it encounters an option letter not included in optstring.  
*
*-- 
*/ 

#include <stdio.h>
#include <string.h>

int optind  = 1;
char *optarg  = NULL;
int opterr  = 1;

int getopt (int argc, char *argv[], const char *optstring)
{
	char *cptr;
	static char scan_char   = 0;
	static int letter_index = 0;

	optarg = NULL;

	/*
	*  check if at the beginning of a group of option letters
	*/
	if (letter_index == 0)
	{
		if (optind >= argc)
			return -1;
		/*
		*  skip to next arg unless this is first
		*/
		if (optind != 1)
			optind++;
	}

	while ((optind < argc) || (letter_index != 0))
	{
		optarg = argv[optind];

		/*
		*  check if at the beginning of a group of option letters
		*/
		if (letter_index == 0)
		{
			if (optind >= argc)
			{
				optarg = NULL;
				return -1;
			}
			if ((argv[optind][0] == '/') || (argv[optind][0] == '-'))
			{
				/*
				*  check if "//"
				*/
				if (argv[optind][1] == argv[optind][0])
				{
					return -1;
				}
				/*
				*  only one / -- just skip it
				*/
				letter_index++;
			}
			else
				return -1;
		}

		scan_char = optarg[letter_index++];

		/*
		*  check if end of option letter group
		*/
		if (scan_char == 0)
		{
			letter_index = 0;
			optind++;
			optarg = NULL;
			continue;
		}

		/*
		*  check if argument is a "dbug" command
		*/
		if ((optarg[0] == '/') || (optarg[0] == '-'))
		{
			if (optarg[1] == '#')
			{
				letter_index = 0;
				optind++;
				optarg = NULL;

				continue;
			}
			else
				break;
		} 
	}

	if (scan_char == 0)
		return -1;

	if (optind >= argc)
	{
		optarg = NULL;
		return -1;
	}

	cptr = strchr(optstring, scan_char);  
	/*
	*  check if it's a valid command letter
	*/
	if (cptr == NULL || scan_char == ':')
		return ('?');

	cptr++;

	/*
	*  check if this command takes an argument
	*/
	if (*cptr == ':')
	{
		if (optind < argc)
			optarg = argv[++optind];
		else
			optarg = NULL;

		letter_index = 0;
	}
	else
		optarg = NULL;

	return scan_char;
}