On Mon, 23 Apr 2007 11:20:51 +0200
Stefan Seyfried <[EMAIL PROTECTED]> wrote:

> On Sat, Apr 21, 2007 at 12:25:24AM +0200, Rafael J. Wysocki wrote:
> > On Saturday, 21 April 2007 00:14, Tim Dijkstra wrote:
> > > Hi,
> > > 
> > > S2ram could also use the usage() function from config.c to display its
> > > message. The only problem is that to replicate its current output we
> > > need to print comments too. To make this possible I made a little
> > > patch. The patch only works for s2disk currently, I didn't feel like
> > > adding the comments for s2ram/s2both if nobody likes the idea...
> > 
> > I like it. :-)
> 
> <AOL/> ;-)
> 
> me, too.

Because everybody is so enthusiastic, I updated the patch so it
works for s2ram/s2both too;) 

I also added an -r,--resume_device option to s2disk/s2both/resume. The
device as the last element on the cli still, works and overrides the
option, but it is no longer showed in usage(), because that would be
inappropriate for s2ram.

Now the output looks like below, (we could loose the '[' and ']' IMHO). It's
not perfect, it is hard to align properly, especially because of the options
with arguments.

$ ./s2both -h
Usage: s2both [options]
  [-h|--help]                           this message.
  [-f|--config <config>]                alternative configuration file
  [-s|--image_size <image_size>]        desired size of the image
  [-r|--resume_device <resume_device>]  device that contains swap area
  [-o|--resume_offset <resume_offset>]  offset of swap file in resume device
  [--force]             force suspending, even on unknown machines.

The following options are only available with --force:
  [--vbe_save]          save VBE state before suspending and restore after 
resume.
  [--vbe_post]          VBE POST the graphics card after resume.
  [--vbe_mode]          get VBE mode before suspend and set it after resume.
  [--radeontool]        turn off the backlight on radeons before suspending.
  [--pci_save]          save the PCI config space for the VGA card.
  [--acpi_sleep <acpi_sleep>]   set the acpi_sleep parameter before suspend
                        1=s3_bios, 2=s3_mode, 3=both


Index: Makefile
===================================================================
RCS file: /cvsroot/suspend/suspend/Makefile,v
retrieving revision 1.54
diff -u -r1.54 Makefile
--- Makefile    26 Mar 2007 21:44:04 -0000      1.54
+++ Makefile    23 Apr 2007 20:09:17 -0000
@@ -22,7 +22,7 @@
 BINARIES=s2disk s2both s2ram swap-offset resume
 BINARIES_MIN=s2disk swap-offset
 
-S2RAM_OBJ=vt.o vbetool/vbetool.o radeontool.o dmidecode.o
+S2RAM_OBJ=vt.o config.o vbetool/vbetool.o radeontool.o dmidecode.o
 SWSUSP_OBJ=vt.o md5.o encrypt.o config.o loglevel.o splash.o bootsplash.o 
 
 S2RAM_LD_FLAGS = $(LD_FLAGS) -lpci -lz -lx86
Index: config.c
===================================================================
RCS file: /cvsroot/suspend/suspend/config.c,v
retrieving revision 1.8
diff -u -r1.8 config.c
--- config.c    1 Apr 2007 22:03:29 -0000       1.8
+++ config.c    23 Apr 2007 20:09:17 -0000
@@ -104,23 +104,26 @@
        return error;
 }
 
-void usage(char *my_name, struct option *options, const char *short_options)
+void usage(char *my_name, struct option_descr *options, const char 
*short_options)
 {
-       struct option *opt;
+       struct option_descr *opt;
 
-       printf("Usage: %s\t", my_name);
-       for (opt = options; opt->name; opt++) 
+       printf("Usage: %s [options]", my_name);
+       for (opt = options; opt->o.name; opt++) 
        {
-               if (strchr(short_options,opt->val))
-                       printf("[-%c|--%s", opt->val, opt->name);
+               if (strchr(short_options,opt->o.val))
+                       printf("\n  [-%c|--%s", opt->o.val, opt->o.name);
                else
-                       printf("[--%s", opt->name);
+                       printf("\n  [--%s", opt->o.name);
 
-               if (opt->has_arg)
-                       printf(" <%s>]\n\t\t", opt->name);
+               if (opt->o.has_arg)
+                       printf(" <%s>]", opt->o.name);
                else
-                       printf("]\n\t\t");
+                       printf("]");
+
+               if (strlen(opt->descr))
+                       printf("\t%s",opt->descr);
        }
 
-       printf("[<resume_device>]\n");
+       printf("\n");
 }
Index: config.h
===================================================================
RCS file: /cvsroot/suspend/suspend/config.h,v
retrieving revision 1.5
diff -u -r1.5 config.h
--- config.h    1 Apr 2007 22:03:29 -0000       1.5
+++ config.h    23 Apr 2007 20:09:17 -0000
@@ -10,6 +10,8 @@
  *
  */
 
+#include <getopt.h>
+
 #define MAX_STR_LEN    256
 
 struct config_par {
@@ -19,8 +21,13 @@
        unsigned int len;
 };
 
+struct option_descr {
+       struct option o;
+       const char *descr;
+};
+
 int parse(char *my_name, char *file_name, int parc, struct config_par *parv);
 
-void usage(char *my_name, struct option options[], const char *short_options);
+void usage(char *my_name, struct option_descr options[], const char 
*short_options);
 
 #define CONFIG_FILE    "/etc/suspend.conf"
Index: resume.c
===================================================================
RCS file: /cvsroot/suspend/suspend/resume.c,v
retrieving revision 1.42
diff -u -r1.42 resume.c
--- resume.c    1 Apr 2007 22:03:29 -0000       1.42
+++ resume.c    23 Apr 2007 20:09:19 -0000
@@ -24,7 +24,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <getopt.h>
 #include <errno.h>
 #ifdef CONFIG_COMPRESS
 #include <lzf.h>
@@ -736,19 +735,26 @@
 /* Parse the command line and/or configuration file */
 static inline int get_config(int argc, char *argv[])
 {
-       static struct option options[] = {
-               { "help",               no_argument,            NULL, 'h'},
-               { "config",             required_argument,      NULL, 'f'},
-               { "resume_offset",      required_argument,      NULL, 'o'},
-               { NULL,                 0,                      NULL,  0 }
+       static struct option_descr options[] = {
+               {   { "help",           no_argument,            NULL, 'h'},
+                   "this text." },
+               {   { "config",         required_argument,      NULL, 'f'},
+                   "alternative configuration file." },
+               {   { "resume_device",  required_argument,      NULL, 'r'},
+                   "device that contains swap area"},
+               {   { "resume_offset",  required_argument,      NULL, 'o'},
+                   "offset of swap file in resume device."},
+               {   { NULL,             0,                      NULL,  0 }, ""}
        };
        int i, error;
        char *conf_name = CONFIG_FILE;
        int set_off = 0;
        unsigned long long int off = 0;
-       const char *optstring = "hf:o:";
+       int set_rdev = 0;
+       char *rdev = NULL;
+       const char *optstring = "hf:o:r:";
 
-       while ((i = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
+       while ((i = getopt_long(argc, argv, optstring, (struct option 
*)options, NULL)) != -1) {
                switch (i) {
                case 'h':
                        usage("resume", options, optstring);
@@ -760,6 +766,10 @@
                        off = atoll(optarg);
                        set_off = 1;
                        break;
+               case 'r':
+                       rdev  = optarg;
+                       set_rdev = 1;
+                       break;
                default:
                        usage("resume", options, optstring);
                        return -EINVAL;
@@ -770,6 +780,9 @@
                fprintf(stderr, "resume: Could not parse config file\n");
                return error;
        }
+       if (set_rdev)
+               strncpy(resume_dev_name, rdev, MAX_STR_LEN -1);
+
        if (set_off)
                resume_offset = off;
 
Index: s2ram.c
===================================================================
RCS file: /cvsroot/suspend/suspend/s2ram.c,v
retrieving revision 1.52
diff -u -r1.52 s2ram.c
--- s2ram.c     2 Apr 2007 19:57:15 -0000       1.52
+++ s2ram.c     23 Apr 2007 20:09:19 -0000
@@ -19,6 +19,7 @@
 #include "vbetool/vbetool.h"
 #include "vt.h"
 #include "s2ram.h"
+#include "config.h"
 
 static void *vbe_buffer;
 static unsigned char vga_pci_state[256];
@@ -340,48 +341,28 @@
 }
 
 #ifndef CONFIG_BOTH
-static void usage(void)
-{
-       printf("Usage: s2ram [-nhi] [-fspmrav]\n"
-              "\n"
-              "Options:\n"
-              "    -h, --help:       this text.\n"
-              "    -n, --test:       test if the machine is in the database.\n"
-              "                      returns 0 if known and supported\n"
-              "    -i, --identify:   prints a string that identifies the 
machine.\n"
-              "    -f, --force:      force suspending, even on unknown 
machines.\n"
-              "\n"
-              "the following options are only available with --force:\n"
-              "    -s, --vbe_save:   save VBE state before suspending and "
-                                      "restore after resume.\n"
-              "    -p, --vbe_post:   VBE POST the graphics card after resume\n"
-              "    -m, --vbe_mode:   get VBE mode before suspend and set it 
after resume\n"
-              "    -r, --radeontool: turn off the backlight on radeons "
-                                      "before suspending.\n"
-              "    -a, --acpi_sleep: set the acpi_sleep parameter before "
-                                      "suspend\n"
-              "                      1=s3_bios, 2=s3_mode, 3=both\n"
-              "    -v, --pci_save:   save the PCI config space for the VGA 
card.\n"
-              "\n");
-       exit(1);
-}
-
 int main(int argc, char *argv[])
 {
        int i, id = -1, ret = 0, test_mode = 0;
        int active_console = -1;
-       struct option options[] = {
-               { "test",       no_argument,            NULL, 'n'},
-               { "help",       no_argument,            NULL, 'h'},
-               { "identify",   no_argument,            NULL, 'i'},
-               HACKS_LONG_OPTS
-               { NULL,         0,                      NULL,  0 }
+       struct option_descr options[] = {
+           {   { "help",       no_argument,            NULL, 'h'},
+               "\tthis text." },
+           {   { "test",       no_argument,            NULL, 'n'},
+               "\ttest if the machine is in the database." },
+           {   { "identify",   no_argument,            NULL, 'i'},
+               "prints a string that identifies the machine." },
+           HACKS_LONG_OPTS,
+           {   { NULL,         0,                      NULL,  0 },
+               "" }
        };
+       const char *optstring = "hni" "fspmrva:";
 
-       while ((i = getopt_long(argc, argv, "nhi" "fspmrva:", options, NULL)) 
!= -1) {
+       while ((i = getopt_long(argc, argv, optstring, (struct option 
*)options, NULL)) != -1) {
                switch (i) {
                case 'h':
-                       usage();
+                       usage("s2ram", options, optstring);
+                       exit(0);
                        break;
                case 'i':
                        dmi_scan();
@@ -391,7 +372,8 @@
                        test_mode = 1;
                        break;
                case '?':
-                       usage();
+                       usage("s2ram", options, optstring);
+                       exit(1);
                        break;
                default:
                        s2ram_add_flag(i,optarg);
@@ -401,7 +383,8 @@
 
        if (force && test_mode) {
                printf("force and test mode do not make sense together.\n\n");
-               usage();
+               usage("s2ram", options, optstring);
+               exit(1);
        }
 
        if (test_mode) {
Index: s2ram.h
===================================================================
RCS file: /cvsroot/suspend/suspend/s2ram.h,v
retrieving revision 1.5
diff -u -r1.5 s2ram.h
--- s2ram.h     2 Apr 2007 19:57:15 -0000       1.5
+++ s2ram.h     23 Apr 2007 20:09:19 -0000
@@ -21,12 +21,18 @@
 void s2ram_add_flag(int opt, const char *arg);
 
 #define HACKS_LONG_OPTS \
-               { "force",      no_argument,            NULL, 1}, \
-               { "vbe_save",   no_argument,            NULL, 2}, \
-               { "vbe_post",   no_argument,            NULL, 3}, \
-               { "vbe_mode",   no_argument,            NULL, 4}, \
-               { "radeontool", no_argument,            NULL, 5}, \
-               { "pci_save",   no_argument,            NULL, 6}, \
-               { "acpi_sleep", required_argument,      NULL, 7},
-
+    {  { "force",      no_argument,            NULL, 1}, \
+       "\tforce suspending, even on unknown machines.\n\nThe following options 
are only available with --force:" }, \
+    {  { "vbe_save",   no_argument,            NULL, 2}, \
+       "\tsave VBE state before suspending and restore after resume."}, \
+    {  { "vbe_post",   no_argument,            NULL, 3}, \
+       "\tVBE POST the graphics card after resume."}, \
+    {  { "vbe_mode",   no_argument,            NULL, 4}, \
+       "\tget VBE mode before suspend and set it after resume."}, \
+    {  { "radeontool", no_argument,            NULL, 5}, \
+       "turn off the backlight on radeons before suspending."}, \
+    {  { "pci_save",   no_argument,            NULL, 6}, \
+       "\tsave the PCI config space for the VGA card."}, \
+    {  { "acpi_sleep", required_argument,      NULL, 7}, \
+       "set the acpi_sleep parameter before suspend\n\t\t\t1=s3_bios, 
2=s3_mode, 3=both" }
 
Index: suspend.c
===================================================================
RCS file: /cvsroot/suspend/suspend/suspend.c,v
retrieving revision 1.76
diff -u -r1.76 suspend.c
--- suspend.c   3 Apr 2007 19:32:34 -0000       1.76
+++ suspend.c   23 Apr 2007 20:09:20 -0000
@@ -32,7 +32,6 @@
 #include <errno.h>
 #include <signal.h>
 #include <termios.h>
-#include <getopt.h>
 #ifdef CONFIG_COMPRESS
 #include <lzf.h>
 #else
@@ -1169,15 +1168,22 @@
 /* Parse the command line and/or configuration file */
 static inline int get_config(int argc, char *argv[])
 {
-       static struct option options[] = {
-               { "help",               no_argument,            NULL, 'h'},
-               { "config",             required_argument,      NULL, 'f'},
-               { "image_size",         required_argument,      NULL, 's'},
-               { "resume_offset",      required_argument,      NULL, 'o'},
+       static struct option_descr options[] = {
+           {   { "help",               no_argument,            NULL, 'h'}, 
+               "\t\t\tthis message."},
+           {   { "config",             required_argument,      NULL, 'f'},
+               "\talternative configuration file"},
+           {   { "image_size",         required_argument,      NULL, 's'},
+               "desired size of the image"},
+           {   { "resume_device",      required_argument,      NULL, 'r'},
+               "device that contains swap area"},
+           {   { "resume_offset",      required_argument,      NULL, 'o'},
+               "offset of swap file in resume device"},
 #ifdef CONFIG_BOTH
-               HACKS_LONG_OPTS
+           HACKS_LONG_OPTS,
 #endif
-               { NULL,                 0,                      NULL,  0 }
+           {   { NULL,                 0,                      NULL,  0 },
+               ""}
        };
        int i, error;
        char *conf_name = CONFIG_FILE;
@@ -1185,9 +1191,11 @@
        unsigned long long int off = 0;
        int set_size = 0;
        unsigned long int im_size = 0;
-       const char *optstring = "hf:s:o:";
+       char *rdev = NULL;
+       int set_rdev = 0;
+       const char *optstring = "hf:s:o:r:";
 
-       while ((i = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
+       while ((i = getopt_long(argc, argv, optstring, (struct option *) 
options, NULL)) != -1) {
                switch (i) {
                case 'h':
                        usage(my_name, options, optstring);
@@ -1203,6 +1211,10 @@
                        off = atoll(optarg);
                        set_off = 1;
                        break;
+               case 'r':
+                       rdev  = optarg;
+                       set_rdev = 1;
+                       break;
                case '?':
                        usage(my_name, options, optstring);
                        return -EINVAL;
@@ -1231,6 +1243,9 @@
                fprintf(stderr, "%s: Could not parse config file\n", my_name);
                return error;
        }
+       if (set_rdev)
+               strncpy(resume_dev_name, rdev, MAX_STR_LEN -1);
+
        if (set_off)
                resume_offset = off;
 

Attachment: signature.asc
Description: PGP signature

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Suspend-devel mailing list
Suspend-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/suspend-devel

Reply via email to