00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include "xdasd_cmdline.h"
00040
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044
00045 #ifdef HAVE_CONFIG_H
00046 # include "config.h"
00047 #endif
00048
00049 #ifndef PACKAGE_VERSION
00050 # error PACKAGE_VERSION not defined
00051 #endif
00052
00053 #ifndef SVNREV
00054 # define SVNREV "0"
00055 #endif
00056
00061 #define XDASD_VERSION PACKAGE_VERSION "." SVNREV
00062
00063 #ifdef _WIN32
00064 # define XDASD_CFGPATH "%SystemRoot%"
00065 # define XDASD_FLTPATH "%SystemRoot%"
00066 # define XDASD_LOGPATH "%SystemRoot%"
00067 # define XDASD_MCFPATH "%SystemRoot%"
00068 # define XDASD_RUNPATH "%SystemRoot%"
00069 #else
00070 # define XDASD_CFGPATH ETCDIR
00071 # define XDASD_FLTPATH ETCDIR
00072 # define XDASD_LOGPATH LSTDIR "/log"
00073 # define XDASD_MCFPATH LSTDIR "/log"
00074 # define XDASD_RUNPATH LSTDIR "/run/xdasd"
00075 #endif
00076
00077 #define XDASD_CFGFNAME "xdasd.conf"
00078 #define XDASD_FLTFNAME "xdasd.filter"
00079 #define XDASD_LOGFNAME "xdasd.log"
00080 #define XDASD_MDIRNAME "xdasd.mcache"
00081
00082
00083 #define XDASD_DEF_CFGFILE XDASD_CFGPATH "/" XDASD_CFGFNAME
00084 #define XDASD_DEF_FLTFILE XDASD_FLTPATH "/" XDASD_FLTFNAME
00085 #define XDASD_DEF_LOGFILE XDASD_LOGPATH "/" XDASD_LOGFNAME
00086 #define XDASD_DEF_MSGFDIR XDASD_MCFPATH "/" XDASD_MDIRNAME
00087 #define XDASD_DEF_RUNPATH XDASD_RUNPATH
00088
00089
00090 #ifdef _WIN32
00091 # define WIN32_LEAN_AND_MEAN
00092 # include <windows.h>
00093 # define strnenv(d,s,n) (ExpandEnvironmentStringsA((LPCSTR)(s),(LPSTR)(d),(n)-1),(d))
00094 #else
00095 # define strnenv(d,s,n) ((strncpy((d),(s),(n)-1)[(n)-1] = 0),(d))
00096 #endif
00097
00098 #ifdef _WIN32
00099 # define XDASD_USAGE_STRING \
00100 "[-cefhklmpv] [[-irsx] | [-d]]\n" \
00101 " Common options:\n" \
00102 " -c <file> use <file> as the configuration file.\n" \
00103 " -e <level> set the logging level to <level>.\n" \
00104 " -f <file> use <file> as the filter specification file.\n" \
00105 " -h display this help screen and terminate.\n" \
00106 " -k <file> write dynamic listen socket number to <file>.\n" \
00107 " -l <file> use <file> as the log file.\n" \
00108 " -m <dir> use <dir> as the message cache directory.\n" \
00109 " -p <dir> use <dir> as the run path (for ipc files).\n" \
00110 " -v display version information and terminate.\n" \
00111 "\n" \
00112 " Windows-specific options:\n" \
00113 " -i [auto] install as a Windows service (auto: start on boot).\n" \
00114 " -r remove the previously installed Windows service.\n" \
00115 " -s start the previously installed Windows service.\n" \
00116 " -x stop the previously installed Windows service."
00117 #else
00118 # define XDASD_USAGE_STRING \
00119 "[-cfhklmpv] [-d]\n" \
00120 " Common options:\n" \
00121 " -c <file> use <file> as the configuration file.\n" \
00122 " -e <level> set the logging level to <level>.\n" \
00123 " -f <file> use <file> as the filter specification file.\n" \
00124 " -h display this help screen and terminate.\n" \
00125 " -k <file> write dynamic listen socket number to <file>.\n" \
00126 " -l <file> use <file> as the log file.\n" \
00127 " -m <dir> use <dir> as the message cache directory.\n" \
00128 " -p <dir> use <dir> as the run path (for pid and socket files).\n" \
00129 " -v display version information and terminate.\n" \
00130 "\n" \
00131 " Unix-specific options:\n" \
00132 " -d detach process from console (daemonize).\n"
00133 #endif
00134
00146 static void xdasd_print_usage(void)
00147 {
00148 fprintf(stderr, "USAGE: xdasd " XDASD_USAGE_STRING "\n");
00149 }
00150
00154
00155
00166 int xdasd_parse_cmdline(int argc, char ** argv, XDASDCmdLine * cmdline)
00167 {
00168 int i;
00169
00170
00171 memset(cmdline, 0, sizeof(*cmdline));
00172 (void)strnenv(cmdline->confile, XDASD_DEF_CFGFILE, sizeof(cmdline->confile));
00173 (void)strnenv(cmdline->fltfile, XDASD_DEF_FLTFILE, sizeof(cmdline->fltfile));
00174 (void)strnenv(cmdline->logfile, XDASD_DEF_LOGFILE, sizeof(cmdline->logfile));
00175 (void)strnenv(cmdline->msgfdir, XDASD_DEF_MSGFDIR, sizeof(cmdline->msgfdir));
00176 (void)strnenv(cmdline->runpath, XDASD_DEF_RUNPATH, sizeof(cmdline->runpath));
00177
00178 strcpy(cmdline->verstr, XDASD_VERSION);
00179
00180 for (i = 1; i < argc; i++)
00181 {
00182 if (strcmp(argv[i], "-c") == 0)
00183 {
00184 i++;
00185 if (i >= argc) goto USAGE;
00186 strncpy(cmdline->confile, argv[i], sizeof(cmdline->confile) - 1);
00187 }
00188 else if (strcmp(argv[i], "-e") == 0)
00189 {
00190 i++;
00191 if (i > argc) goto USAGE;
00192 cmdline->loglevel = atoi(argv[i]);
00193 }
00194 else if (strcmp(argv[i], "-f") == 0)
00195 {
00196 i++;
00197 if (i >= argc) goto USAGE;
00198 strncpy(cmdline->fltfile, argv[i], sizeof(cmdline->fltfile) - 1);
00199 }
00200 else if (strcmp(argv[i], "-l") == 0)
00201 {
00202 i++;
00203 if (i >= argc) goto USAGE;
00204 strncpy(cmdline->logfile, argv[i], sizeof(cmdline->logfile) - 1);
00205 }
00206 else if (strcmp(argv[i], "-m") == 0)
00207 {
00208 int lastch;
00209 i++;
00210 if (i >= argc) goto USAGE;
00211 strncpy(cmdline->msgfdir, argv[i], sizeof(cmdline->msgfdir) - 1);
00212
00213
00214 lastch = (int)(strlen(cmdline->msgfdir) - 1);
00215 if (cmdline->msgfdir[lastch] == '/'
00216 || cmdline->msgfdir[lastch] == '\\')
00217 cmdline->msgfdir[lastch] = 0;
00218 }
00219 else if (strcmp(argv[i], "-k") == 0)
00220 {
00221 i++;
00222 if (i >= argc) goto USAGE;
00223 strncpy(cmdline->sktfile, argv[i], sizeof(cmdline->sktfile) - 1);
00224 }
00225 #ifndef _WIN32
00226 else if (strcmp(argv[i], "-p") == 0)
00227 {
00228 i++;
00229 if (i >= argc) goto USAGE;
00230 strncpy(cmdline->runpath, argv[i], sizeof(cmdline->runpath) - 1);
00231 }
00232 else if (strcmp(argv[i], "-d") == 0)
00233 cmdline->detach = 1;
00234 #else
00235 else if (strcmp(argv[i], "-i") == 0)
00236 {
00237 cmdline->action = XDASD_INSTALL;
00238 if (i + 1 < argc && strcmp(argv[i + 1], "auto") == 0)
00239 {
00240 i++;
00241 cmdline->autostart = 1;
00242 }
00243 }
00244 else if (strcmp(argv[i], "-r") == 0
00245 || strcmp(argv[i], "-u") == 0)
00246 cmdline->action = XDASD_REMOVE;
00247 else if (strcmp(argv[i], "-s") == 0)
00248 cmdline->action = XDASD_START;
00249 else if (strcmp(argv[i], "-x") == 0
00250 || strcmp(argv[i], "--kill") == 0)
00251 cmdline->action = XDASD_STOP;
00252 #endif
00253 else if (strcmp(argv[i], "-v") == 0
00254 || strcmp(argv[i], "-V") == 0
00255 || strcmp(argv[i], "-ver") == 0
00256 || strcmp(argv[i], "--ver") == 0
00257 || strcmp(argv[i], "--version") == 0
00258 || strcmp(argv[i], "--Version") == 0)
00259 {
00260 fprintf(stderr, "OpenXDAS xdasd version: %s\n", cmdline->verstr);
00261 exit(1);
00262 }
00263 else if (strcmp(argv[i], "-h") == 0
00264 || strcmp(argv[i], "-H") == 0
00265 || strcmp(argv[i], "-?") == 0
00266 || strcmp(argv[i], "--help") == 0
00267 || strcmp(argv[i], "--Help") == 0)
00268 {
00269 xdasd_print_usage();
00270 exit(1);
00271 }
00272 else
00273 goto USAGE;
00274 }
00275 return 0;
00276
00277 USAGE:
00278
00279 xdasd_print_usage();
00280 return -1;
00281 }
00282