OmniEvents
getopt.cc
Go to the documentation of this file.
1 // -*- Mode: C++; -*-
2 // Package : omniEvents
3 // getopt.cc Created : 1/4/98
4 // Author : Paul Nader (pwn)
5 //
6 // Copyright (C) 1998 Paul Nader.
7 //
8 // This file is part of the omniEvents application.
9 //
10 // omniEvents is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // omniEvents is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // Description:
25 //
26 // getopt implementation for WIN32 platforms.
27 //
28 
29 /*
30  $Log: getopt.cc,v $
31  Revision 1.4 2004/08/04 08:13:44 alextingle
32  Unix daemon & Windows service now both working. Accessed through interface class Daemon (in daemon.h).
33 
34  Revision 1.3 2004/07/15 14:34:31 alextingle
35  Global variables are now declared extern "C".
36 
37  Revision 1.2 2004/02/22 00:37:35 alextingle
38  Now does nothing if it isn't needed. Fixes link errors
39 
40  Revision 1.1 2003/12/21 16:19:49 alextingle
41  Moved into 'src' directory as part of the change to POA implementation.
42 
43  Revision 1.2 2003/11/03 22:46:54 alextingle
44  This implementation of getopt() seems to be needed on AIX as well as
45  windows. It therefore needs to be able to compile on Unix. Changed
46  _tzset() to tzset() in order to achieve this.
47 
48  Revision 1.1.1.1 2002/09/25 19:00:35 shamus13
49  Import of OmniEvents source tree from release 2.1.1
50 
51  Revision 1.3 1999/11/01 16:59:38 naderp
52  *** empty log message ***
53 
54 Revision 1.2 99/04/23 12:11:18 12:11:18 naderp (Paul Nader)
55 *** empty log message ***
56 
57 Revision 1.1 99/04/23 09:33:35 09:33:35 naderp (Paul Nader)
58 Initial revision
59 */
60 
61 #ifdef HAVE_CONFIG_H
62 # include "config.h"
63 #endif
64 
65 #ifdef HAVE_GETOPT
66 void omniEventsDummyGetopt(){}
67 #else
68 
69 #include "getopt.h"
70 #include <errno.h>
71 #include <stdio.h>
72 #include <string.h>
73 
74 static char* letP =NULL; // Speichert den Ort des Zeichens der
75  // naechsten Option
76 static char SW ='-'; // DOS-Schalter, entweder '-' oder '/'
77 
78 // -------------------------------------------------------------- exports ----
79 
80 extern "C"
81 {
82  int optind = 1; // Index: welches Argument ist das naechste
83  char* optarg; // Zeiger auf das Argument der akt. Option
84  int opterr = 1; // erlaubt Fehlermeldungen
85 }
86 
87 // ===========================================================================
88 int getopt(int argc, char *argv[], const char *optionS)
89 {
90  unsigned char ch;
91  char *optP;
92 
93  if(argc>optind)
94  {
95  if(letP==NULL)
96  {
97  // Initialize letP
98  if( (letP=argv[optind])==NULL || *(letP++)!=SW )
99  goto gopEOF;
100  if(*letP == SW)
101  {
102  // "--" is end of options.
103  optind++;
104  goto gopEOF;
105  }
106  }
107 
108  if((ch=*(letP++))== '\0')
109  {
110  // "-" is end of options.
111  optind++;
112  goto gopEOF;
113  }
114  if(':'==ch || (optP=(char*)strchr(optionS,ch)) == NULL)
115  {
116  goto gopError;
117  }
118  // 'ch' is a valid option
119  // 'optP' points to the optoin char in optionS
120  if(':'==*(++optP))
121  {
122  // Option needs a parameter.
123  optind++;
124  if('\0'==*letP)
125  {
126  // parameter is in next argument
127  if(argc <= optind)
128  goto gopError;
129  letP = argv[optind++];
130  }
131  optarg = letP;
132  letP = NULL;
133  }
134  else
135  {
136  // Option needs no parameter.
137  if('\0'==*letP)
138  {
139  // Move on to next argument.
140  optind++;
141  letP = NULL;
142  }
143  optarg = NULL;
144  }
145  return ch;
146  }
147 gopEOF:
148  optarg=letP=NULL;
149  return EOF;
150 
151 gopError:
152  optarg = NULL;
153  errno = EINVAL;
154  if(opterr)
155  perror ("error in command line");
156  return ('?');
157 }
158 // ===========================================================================
159 // Ende von getopt ()
160 // ===========================================================================
161 
162 #endif // HAVE_GETOPT
163 
static char SW
Definition: getopt.cc:76
int optind
Definition: getopt.cc:82
char * optarg
Definition: getopt.cc:83
static char * letP
Definition: getopt.cc:74
int opterr
Definition: getopt.cc:84
int getopt(int argc, char *argv[], const char *optionS)
Definition: getopt.cc:88