Vitaliy Margolen : wineserver: Add support for long command line options.

Alexandre Julliard julliard at winehq.org
Thu May 22 06:27:45 CDT 2008


Module: wine
Branch: master
Commit: 67daa7f7608f4012a9cfcb1e2cdc20340710b526
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=67daa7f7608f4012a9cfcb1e2cdc20340710b526

Author: Vitaliy Margolen <wine-patches at kievinfo.com>
Date:   Wed May 21 17:58:58 2008 -0600

wineserver: Add support for long command line options.

---

 server/main.c            |   67 ++++++++++++++++++++++++++--------------------
 server/wineserver.man.in |   14 +++++-----
 2 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/server/main.c b/server/main.c
index c839c18..a7dfc1c 100644
--- a/server/main.c
+++ b/server/main.c
@@ -28,6 +28,9 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <unistd.h>
+#ifdef HAVE_GETOPT_H
+# include <getopt.h>
+#endif
 
 #include "object.h"
 #include "file.h"
@@ -41,36 +44,48 @@ timeout_t master_socket_timeout = 3 * -TICKS_PER_SEC;  /* master socket timeout,
 const char *server_argv0;
 
 /* parse-line args */
-/* FIXME: should probably use getopt, and add a (more complete?) help option */
 
 static void usage(void)
 {
-    fprintf(stderr, "\nusage: %s [options]\n\n", server_argv0);
-    fprintf(stderr, "options:\n");
-    fprintf(stderr, "   -d<n>  set debug level to <n>\n");
-    fprintf(stderr, "   -f     remain in the foreground for debugging\n");
-    fprintf(stderr, "   -h     display this help message\n");
-    fprintf(stderr, "   -k[n]  kill the current wineserver, optionally with signal n\n");
-    fprintf(stderr, "   -p[n]  make server persistent, optionally for n seconds\n");
-    fprintf(stderr, "   -v     display version information and exit\n");
-    fprintf(stderr, "   -w     wait until the current wineserver terminates\n");
+    fprintf(stderr, "Usage: %s [options]\n\n", server_argv0);
+    fprintf(stderr, "Options:\n");
+    fprintf(stderr, "   -d[n], --debug[=n]       set debug level to n or +1 if n not specified\n");
+    fprintf(stderr, "   -f,    --foreground      remain in the foreground for debugging\n");
+    fprintf(stderr, "   -h,    --help            display this help message\n");
+    fprintf(stderr, "   -k[n], --kill[=n]        kill the current wineserver, optionally with signal n\n");
+    fprintf(stderr, "   -p[n], --persistent[=n]  make server persistent, optionally for n seconds\n");
+    fprintf(stderr, "   -v,    --version         display version information and exit\n");
+    fprintf(stderr, "   -w,    --wait            wait until the current wineserver terminates\n");
     fprintf(stderr, "\n");
 }
 
 static void parse_args( int argc, char *argv[] )
 {
-    int i, ret;
+    int ret, optc;
+
+    static struct option long_options[] =
+    {
+        {"debug",       2, 0, 'd'},
+        {"foreground",  0, 0, 'f'},
+        {"help",        0, 0, 'h'},
+        {"kill",        2, 0, 'k'},
+        {"persistent",  2, 0, 'p'},
+        {"version",     0, 0, 'v'},
+        {"wait",        0, 0, 'w'},
+        { NULL,         0, 0, 0}
+    };
 
     server_argv0 = argv[0];
-    for (i = 1; i < argc; i++)
+
+    while ((optc = getopt_long( argc, argv, "d::fhk::p::vw", long_options, NULL )) != -1)
     {
-        if (argv[i][0] == '-')
+        switch(optc)
         {
-            switch(argv[i][1])
-            {
             case 'd':
-                if (isdigit(argv[i][2])) debug_level = atoi( argv[i] + 2 );
-                else debug_level++;
+                if (optarg && isdigit(*optarg))
+                    debug_level = atoi( optarg );
+                else
+                    debug_level++;
                 break;
             case 'f':
                 foreground = 1;
@@ -80,12 +95,14 @@ static void parse_args( int argc, char *argv[] )
                 exit(0);
                 break;
             case 'k':
-                if (isdigit(argv[i][2])) ret = kill_lock_owner( atoi(argv[i] + 2) );
-                else ret = kill_lock_owner(-1);
+                if (optarg && isdigit(*optarg))
+                    ret = kill_lock_owner( atoi( optarg ) );
+                else
+                    ret = kill_lock_owner(-1);
                 exit( !ret );
             case 'p':
-                if (isdigit(argv[i][2]))
-                    master_socket_timeout = (timeout_t)atoi( argv[i] + 2 ) * -TICKS_PER_SEC;
+                if (optarg && isdigit(*optarg))
+                    master_socket_timeout = (timeout_t)atoi( optarg ) * -TICKS_PER_SEC;
                 else
                     master_socket_timeout = TIMEOUT_INFINITE;
                 break;
@@ -96,16 +113,8 @@ static void parse_args( int argc, char *argv[] )
                 wait_for_lock();
                 exit(0);
             default:
-                fprintf( stderr, "wineserver: unknown option '%s'\n", argv[i] );
                 usage();
                 exit(1);
-            }
-        }
-        else
-        {
-            fprintf( stderr, "wineserver: unknown argument '%s'.\n", argv[i] );
-            usage();
-            exit(1);
         }
     }
 }
diff --git a/server/wineserver.man.in b/server/wineserver.man.in
index 330adbd..ae0e19f 100644
--- a/server/wineserver.man.in
+++ b/server/wineserver.man.in
@@ -16,7 +16,7 @@ useful to start \fBwineserver\fR explicitly with different options, as
 explained below.
 .SH OPTIONS
 .TP
-.BI \-d [n]
+.BI \-d [n] ,\ --debug [=n]
 Set the debug level to
 .I n.
 0 means no debugging information, 1 is the normal level, and 2 is for
@@ -27,14 +27,14 @@ stderr. \fBwine(1)\fR will automatically set the debug level when
 starting \fBwineserver\fR if the +server option is set in the
 WINEDEBUG variable.
 .TP
-.B \-f
+.B \-f, --foreground
 Make the server remain in the foreground for easier debugging, for
 instance when running it under a debugger.
 .TP
-.B \-h
+.B \-h, --help
 Display a help message.
 .TP
-.BI \-k [n]
+.BI \-k [n] ,\ --kill [=n]
 Kill the currently running
 .B wineserver,
 optionally by sending signal \fIn\fR. If no signal is specified, sends
@@ -42,7 +42,7 @@ a SIGINT first and then a SIGKILL.  The instance of \fBwineserver\fR
 that is killed is selected based on the WINEPREFIX environment
 variable.
 .TP
-.BI \-p [n]
+.BI \-p [n] ,\ --persistent [=n]
 Specify the \fBwineserver\fR persistence delay, i.e. the amount of
 time that the server will keep running when all client processes have
 terminated. This avoids the cost of shutting down and starting again
@@ -50,10 +50,10 @@ when programs are launched in quick succession. The timeout \fIn\fR is
 in seconds, the default value is 3 seconds. If \fIn\fR is not
 specified, the server stays around forever.
 .TP
-.B \-v
+.B \-v, --version
 Display version information and exit.
 .TP
-.B \-w
+.B \-w, --wait
 Wait until the currently running
 .B wineserver
 terminates.




More information about the wine-cvs mailing list