xdasd_cmdline.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 /* default cfg, log, msg, run paths */
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 /* expand environment strings or copy them, zero terminate, return d */
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    /* Set defaults */
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          /* remove any trailing slashes in order to normalize the path */
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 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6