00001 // -*- Mode: C++; -*- 00002 // Package : omniEvents 00003 // getopt.cc Created : 1/4/98 00004 // Author : Paul Nader (pwn) 00005 // 00006 // Copyright (C) 1998 Paul Nader. 00007 // 00008 // This file is part of the omniEvents application. 00009 // 00010 // omniEvents is free software; you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public 00012 // License as published by the Free Software Foundation; either 00013 // version 2.1 of the License, or (at your option) any later version. 00014 // 00015 // omniEvents is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 // 00024 // Description: 00025 // 00026 // getopt implementation for WIN32 platforms. 00027 // 00028 00029 /* 00030 $Log: getopt.cc,v $ 00031 Revision 1.4 2004/08/04 08:13:44 alextingle 00032 Unix daemon & Windows service now both working. Accessed through interface class Daemon (in daemon.h). 00033 00034 Revision 1.3 2004/07/15 14:34:31 alextingle 00035 Global variables are now declared extern "C". 00036 00037 Revision 1.2 2004/02/22 00:37:35 alextingle 00038 Now does nothing if it isn't needed. Fixes link errors 00039 00040 Revision 1.1 2003/12/21 16:19:49 alextingle 00041 Moved into 'src' directory as part of the change to POA implementation. 00042 00043 Revision 1.2 2003/11/03 22:46:54 alextingle 00044 This implementation of getopt() seems to be needed on AIX as well as 00045 windows. It therefore needs to be able to compile on Unix. Changed 00046 _tzset() to tzset() in order to achieve this. 00047 00048 Revision 1.1.1.1 2002/09/25 19:00:35 shamus13 00049 Import of OmniEvents source tree from release 2.1.1 00050 00051 Revision 1.3 1999/11/01 16:59:38 naderp 00052 *** empty log message *** 00053 00054 Revision 1.2 99/04/23 12:11:18 12:11:18 naderp (Paul Nader) 00055 *** empty log message *** 00056 00057 Revision 1.1 99/04/23 09:33:35 09:33:35 naderp (Paul Nader) 00058 Initial revision 00059 */ 00060 00061 #ifdef HAVE_CONFIG_H 00062 # include "config.h" 00063 #endif 00064 00065 #ifdef HAVE_GETOPT 00066 void omniEventsDummyGetopt(){} 00067 #else 00068 00069 #include "getopt.h" 00070 #include <errno.h> 00071 #include <stdio.h> 00072 #include <string.h> 00073 00074 static char* letP =NULL; // Speichert den Ort des Zeichens der 00075 // naechsten Option 00076 static char SW ='-'; // DOS-Schalter, entweder '-' oder '/' 00077 00078 // -------------------------------------------------------------- exports ---- 00079 00080 extern "C" 00081 { 00082 int optind = 1; // Index: welches Argument ist das naechste 00083 char* optarg; // Zeiger auf das Argument der akt. Option 00084 int opterr = 1; // erlaubt Fehlermeldungen 00085 } 00086 00087 // =========================================================================== 00088 int getopt(int argc, char *argv[], const char *optionS) 00089 { 00090 unsigned char ch; 00091 char *optP; 00092 00093 if(argc>optind) 00094 { 00095 if(letP==NULL) 00096 { 00097 // Initialize letP 00098 if( (letP=argv[optind])==NULL || *(letP++)!=SW ) 00099 goto gopEOF; 00100 if(*letP == SW) 00101 { 00102 // "--" is end of options. 00103 optind++; 00104 goto gopEOF; 00105 } 00106 } 00107 00108 if((ch=*(letP++))== '\0') 00109 { 00110 // "-" is end of options. 00111 optind++; 00112 goto gopEOF; 00113 } 00114 if(':'==ch || (optP=(char*)strchr(optionS,ch)) == NULL) 00115 { 00116 goto gopError; 00117 } 00118 // 'ch' is a valid option 00119 // 'optP' points to the optoin char in optionS 00120 if(':'==*(++optP)) 00121 { 00122 // Option needs a parameter. 00123 optind++; 00124 if('\0'==*letP) 00125 { 00126 // parameter is in next argument 00127 if(argc <= optind) 00128 goto gopError; 00129 letP = argv[optind++]; 00130 } 00131 optarg = letP; 00132 letP = NULL; 00133 } 00134 else 00135 { 00136 // Option needs no parameter. 00137 if('\0'==*letP) 00138 { 00139 // Move on to next argument. 00140 optind++; 00141 letP = NULL; 00142 } 00143 optarg = NULL; 00144 } 00145 return ch; 00146 } 00147 gopEOF: 00148 optarg=letP=NULL; 00149 return EOF; 00150 00151 gopError: 00152 optarg = NULL; 00153 errno = EINVAL; 00154 if(opterr) 00155 perror ("error in command line"); 00156 return ('?'); 00157 } 00158 // =========================================================================== 00159 // Ende von getopt () 00160 // =========================================================================== 00161 00162 #endif // HAVE_GETOPT 00163