Quantcast

[PATCH v4 1/5] saned: parse standalone args in main()

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH v4 1/5] saned: parse standalone args in main()

Matteo Croce
move argument parsing logic from run_standalone() to main()
---
 frontend/saned.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/frontend/saned.c b/frontend/saned.c
index 108512d..e717ed2 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -2923,7 +2923,7 @@ do_bindings (int *nfds, struct pollfd **fds)
 
 
 static void
-run_standalone (int argc, char **argv)
+run_standalone (char *user)
 {
   struct pollfd *fds = NULL;
   struct pollfd *fdp = NULL;
@@ -2944,13 +2944,13 @@ run_standalone (int argc, char **argv)
 
   if (run_mode != SANED_RUN_DEBUG)
     {
-      if (argc > 2)
+      if (user)
  {
-  pwent = getpwnam(argv[2]);
+  pwent = getpwnam(user);
 
   if (pwent == NULL)
     {
-      DBG (DBG_ERR, "FATAL ERROR: user %s not found on system\n", argv[2]);
+      DBG (DBG_ERR, "FATAL ERROR: user %s not found on system\n", user);
       bail_out (1);
     }
 
@@ -2981,7 +2981,7 @@ run_standalone (int argc, char **argv)
 
               while (grp->gr_mem[i])
  {
-                  if (strcmp(grp->gr_mem[i], argv[2]) == 0)
+                  if (strcmp(grp->gr_mem[i], user) == 0)
                     {
                       int need_to_add = 1, j;
 
@@ -3264,6 +3264,7 @@ main (int argc, char *argv[])
 {
   char options[64] = "";
   debug = DBG_WARN;
+  char *user = NULL;
 
   prog_name = strrchr (argv[0], '/');
   if (prog_name)
@@ -3277,7 +3278,11 @@ main (int argc, char *argv[])
   if (argc >= 2)
     {
       if (strncmp (argv[1], "-a", 2) == 0)
- run_mode = SANED_RUN_ALONE;
+ {
+  run_mode = SANED_RUN_ALONE;
+  if (argc >= 3)
+    user = argv[2];
+ }
       else if (strncmp (argv[1], "-d", 2) == 0)
  {
   run_mode = SANED_RUN_DEBUG;
@@ -3342,7 +3347,7 @@ main (int argc, char *argv[])
 
   if ((run_mode == SANED_RUN_ALONE) || (run_mode == SANED_RUN_DEBUG))
     {
-      run_standalone(argc, argv);
+      run_standalone(user);
     }
   else
     {
--
2.5.0


--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH v4 2/5] saned: parse inetd args in main()

Matteo Croce
move argument parsing logic from run_inetd() to main()
---
 frontend/saned.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/frontend/saned.c b/frontend/saned.c
index e717ed2..ab36397 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -3172,7 +3172,7 @@ run_standalone (char *user)
 
 
 static void
-run_inetd (int argc, char **argv)
+run_inetd (char *sock)
 {
   
   int fd = -1;
@@ -3238,18 +3238,13 @@ run_inetd (int argc, char **argv)
 
       close (dave_null);
     }
-#ifndef HAVE_OS2_H
-  /* Unused in this function */
-  argc = argc;
-  argv = argv;
-
-#else
+#ifdef HAVE_OS2_H
   /* under OS/2, the socket handle is passed as argument on the command
      line; the socket handle is relative to IBM TCP/IP, so a call
      to impsockethandle() is required to add it to the EMX runtime */
-  if (argc == 2)
+  if (sock)
     {
-      fd = _impsockhandle (atoi (argv[1]), 0);
+      fd = _impsockhandle (atoi (sock), 0);
       if (fd == -1)
  perror ("impsockhandle");
     }
@@ -3265,6 +3260,7 @@ main (int argc, char *argv[])
   char options[64] = "";
   debug = DBG_WARN;
   char *user = NULL;
+  char *sock = NULL;
 
   prog_name = strrchr (argv[0], '/');
   if (prog_name)
@@ -3351,7 +3347,11 @@ main (int argc, char *argv[])
     }
   else
     {
-      run_inetd(argc, argv);
+#ifdef HAVE_OS2_H
+      if (argc == 2)
+ sock = argv[1];
+#endif
+      run_inetd(sock);
     }
 
   DBG (DBG_WARN, "saned exiting\n");
--
2.5.0


--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH v4 3/5] add usage()

Matteo Croce
In reply to this post by Matteo Croce
move help text in a separate function
---
 frontend/saned.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/frontend/saned.c b/frontend/saned.c
index ab36397..8eff6c7 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -3253,6 +3253,11 @@ run_inetd (char *sock)
   handle_connection(fd);
 }
 
+static void usage(char *me, int err)
+{
+  fprintf (stderr, "Usage: %s -a [ username ] | -d [ n ] | -s [ n ]| -h\n", me);
+  exit(err);
+}
 
 int
 main (int argc, char *argv[])
@@ -3288,12 +3293,11 @@ main (int argc, char *argv[])
  run_mode = SANED_RUN_DEBUG;
       else
         {
-          printf ("Usage: saned [ -a [ username ] | -d [ n ] | -s [ n ] ] | -h\n");
           if ((strncmp (argv[1], "-h", 2) == 0) ||
                (strncmp (argv[1], "--help", 6) == 0))
-            exit (EXIT_SUCCESS);
+            usage (argv[0], EXIT_SUCCESS);
           else
-            exit (EXIT_FAILURE);
+            usage (argv[0], EXIT_FAILURE);
         }
     }
 
--
2.5.0


--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH v4 4/5] getopt support

Matteo Croce
In reply to this post by Matteo Croce
add support for getopt_long, adjust help message
---
 frontend/saned.c | 75 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 30 deletions(-)

diff --git a/frontend/saned.c b/frontend/saned.c
index 8eff6c7..f74b8fa 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -82,6 +82,8 @@
 #include <pwd.h>
 #include <grp.h>
 
+#include <getopt.h>
+
 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
 # include <sys/poll.h>
 #else
@@ -3255,10 +3257,31 @@ run_inetd (char *sock)
 
 static void usage(char *me, int err)
 {
-  fprintf (stderr, "Usage: %s -a [ username ] | -d [ n ] | -s [ n ]| -h\n", me);
+  fprintf (stderr,
+       "Usage: %s [OPTIONS]\n\n"
+       " Options:\n\n"
+       "  -a, --alone[=user] run standalone and fork in background as `user'\n"
+       "  -d, --debug[=level] run foreground with output to stdout\n"
+       " and debug level `level' (default is 2)\n"
+       "  -s, --syslog[=level] run foreground with output to syslog\n"
+       " and debug level `level' (default is 2)\n"
+       "  -h, --help this help message\n", me);
+
   exit(err);
 }
 
+static int debug;
+
+static struct option long_options[] =
+{
+/* These options set a flag. */
+  {"help", no_argument, 0, 'h'},
+  {"alone", optional_argument, 0, 'a'},
+  {"debug", optional_argument, 0, 'd'},
+  {"syslog", optional_argument, 0, 's'},
+  {0}
+};
+
 int
 main (int argc, char *argv[])
 {
@@ -3266,6 +3289,8 @@ main (int argc, char *argv[])
   debug = DBG_WARN;
   char *user = NULL;
   char *sock = NULL;
+  int c;
+  int long_index = 0;
 
   prog_name = strrchr (argv[0], '/');
   if (prog_name)
@@ -3276,37 +3301,27 @@ main (int argc, char *argv[])
   numchildren = 0;
   run_mode = SANED_RUN_INETD;
 
-  if (argc >= 2)
+  while((c = getopt_long(argc, argv,"ha::d::s::", long_options, &long_index )) != -1)
     {
-      if (strncmp (argv[1], "-a", 2) == 0)
- {
-  run_mode = SANED_RUN_ALONE;
-  if (argc >= 3)
-    user = argv[2];
- }
-      else if (strncmp (argv[1], "-d", 2) == 0)
- {
-  run_mode = SANED_RUN_DEBUG;
-  log_to_syslog = SANE_FALSE;
- }
-      else if (strncmp (argv[1], "-s", 2) == 0)
+      switch(c) {
+      case 'a':
+ run_mode = SANED_RUN_ALONE;
+ user = optarg;
+ break;
+      case 'd':
+ log_to_syslog = SANE_FALSE;
+      case 's':
  run_mode = SANED_RUN_DEBUG;
-      else
-        {
-          if ((strncmp (argv[1], "-h", 2) == 0) ||
-               (strncmp (argv[1], "--help", 6) == 0))
-            usage (argv[0], EXIT_SUCCESS);
-          else
-            usage (argv[0], EXIT_FAILURE);
-        }
-    }
-
-  if (run_mode == SANED_RUN_DEBUG)
-    {
-      if (argv[1][2])
- debug = atoi (argv[1] + 2);
-
-      DBG (DBG_WARN, "main: starting debug mode (level %d)\n", debug);
+ if(optarg)
+  debug = atoi(optarg);
+ break;
+      case 'h':
+ usage(argv[0], EXIT_SUCCESS);
+ break;
+      default:
+ usage(argv[0], EXIT_FAILURE);
+ break;
+      }
     }
 
   if (log_to_syslog)
--
2.5.0


--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH v4 5/5] add optional argument '-b' to let saned bind an address

Matteo Croce
In reply to this post by Matteo Croce
---
 frontend/saned.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/frontend/saned.c b/frontend/saned.c
index f74b8fa..d33b031 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -249,6 +249,7 @@ static int num_handles;
 static int debug;
 static int run_mode;
 static Handle *handle;
+static char *bind_addr;
 static union
 {
   int w;
@@ -2809,13 +2810,13 @@ do_bindings (int *nfds, struct pollfd **fds)
   hints.ai_flags = AI_PASSIVE;
   hints.ai_socktype = SOCK_STREAM;
 
-  err = getaddrinfo (NULL, SANED_SERVICE_NAME, &hints, &res);
+  err = getaddrinfo (bind_addr, SANED_SERVICE_NAME, &hints, &res);
   if (err)
     {
       DBG (DBG_WARN, "do_bindings: \" %s \" service unknown on your host; you should add\n", SANED_SERVICE_NAME);
       DBG (DBG_WARN, "do_bindings:      %s %d/tcp saned # SANE network scanner daemon\n", SANED_SERVICE_NAME, SANED_SERVICE_PORT);
       DBG (DBG_WARN, "do_bindings: to your /etc/services file (or equivalent). Proceeding anyway.\n");
-      err = getaddrinfo (NULL, SANED_SERVICE_PORT_S, &hints, &res);
+      err = getaddrinfo (bind_addr, SANED_SERVICE_PORT_S, &hints, &res);
       if (err)
  {
   DBG (DBG_ERR, "do_bindings: getaddrinfo() failed even with numeric port: %s\n", gai_strerror (err));
@@ -2893,7 +2894,10 @@ do_bindings (int *nfds, struct pollfd **fds)
   memset (&sin, 0, sizeof (sin));
 
   sin.sin_family = AF_INET;
-  sin.sin_addr.s_addr = INADDR_ANY;
+  if(bind_addr)
+    sin.sin_addr.s_addr = inet_addr(bind_addr);
+  else
+    sin.sin_addr.s_addr = INADDR_ANY;
   sin.sin_port = port;
 
   DBG (DBG_DBG, "do_bindings: socket ()\n");
@@ -3265,6 +3269,7 @@ static void usage(char *me, int err)
        " and debug level `level' (default is 2)\n"
        "  -s, --syslog[=level] run foreground with output to syslog\n"
        " and debug level `level' (default is 2)\n"
+       "  -b, --bind=addr bind address `addr'\n"
        "  -h, --help this help message\n", me);
 
   exit(err);
@@ -3279,6 +3284,7 @@ static struct option long_options[] =
   {"alone", optional_argument, 0, 'a'},
   {"debug", optional_argument, 0, 'd'},
   {"syslog", optional_argument, 0, 's'},
+  {"bind", required_argument, 0, 'b'},
   {0}
 };
 
@@ -3301,7 +3307,7 @@ main (int argc, char *argv[])
   numchildren = 0;
   run_mode = SANED_RUN_INETD;
 
-  while((c = getopt_long(argc, argv,"ha::d::s::", long_options, &long_index )) != -1)
+  while((c = getopt_long(argc, argv,"ha::d::s::b:", long_options, &long_index )) != -1)
     {
       switch(c) {
       case 'a':
@@ -3315,6 +3321,9 @@ main (int argc, char *argv[])
  if(optarg)
   debug = atoi(optarg);
  break;
+      case 'b':
+ bind_addr = optarg;
+ break;
       case 'h':
  usage(argv[0], EXIT_SUCCESS);
  break;
--
2.5.0


--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

saned option handling changes (was Re: [PATCH v4 1/5] saned: parse standalone args in main())

Olaf Meeuwissen-4
In reply to this post by Matteo Croce
Hi Matteo,

Matteo Croce writes:

I was about to ping you about this ;-)

I've pushed all your changes (plus a few nitpick changes) to Alioth's
master.  The only thing that is still missing now is an updated manual
page.  If you could take care of that, it'll be appreciated.

> [...snip...]

Hope this helps,
--
Olaf Meeuwissen, LPIC-2            FSF Associate Member since 2004-01-27
Support Free Software               Support the Free Software Foundation
https://my.fsf.org/donate                        https://my.fsf.org/join
 GnuPG key: F84A2DD9/B3C0 2F47 EA19 64F4 9F13  F43E B8A4 A88A F84A 2DD9


--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: saned option handling changes (was Re: [PATCH v4 1/5] saned: parse standalone args in main())

Matteo Croce
Hi Olaf,

great, but I think that commit 45e66aee is wrong because it's
shadowing the global variable 'bind_addr'.
Also there is no need to initialize it to NULL because static
variables are always set to 0.

Cheers,
Matteo

2015-10-12 13:57 GMT+02:00 Olaf Meeuwissen <[hidden email]>:

> Hi Matteo,
>
> Matteo Croce writes:
>
> I was about to ping you about this ;-)
>
> I've pushed all your changes (plus a few nitpick changes) to Alioth's
> master.  The only thing that is still missing now is an updated manual
> page.  If you could take care of that, it'll be appreciated.
>
>> [...snip...]
>
> Hope this helps,
> --
> Olaf Meeuwissen, LPIC-2            FSF Associate Member since 2004-01-27
> Support Free Software               Support the Free Software Foundation
> https://my.fsf.org/donate                        https://my.fsf.org/join
>  GnuPG key: F84A2DD9/B3C0 2F47 EA19 64F4 9F13  F43E B8A4 A88A F84A 2DD9



--
Matteo Croce
OpenWrt Developer
  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 CHAOS CALMER
 -----------------------------------------------------
  * 1 1/2 oz Gin            Shake with a glassful
  * 1/4 oz Triple Sec       of broken ice and pour
  * 3/4 oz Lime Juice       unstrained into a goblet.
  * 1 1/2 oz Orange Juice
  * 1 tsp. Grenadine Syrup
 -----------------------------------------------------

--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: saned option handling changes (was Re: [PATCH v4 1/5] saned: parse standalone args in main())

Olaf Meeuwissen-4

Matteo Croce writes:

> Hi Olaf,
>
> great, but I think that commit 45e66aee is wrong because it's
> shadowing the global variable 'bind_addr'.

My bad.

> Also there is no need to initialize it to NULL because static
> variables are always set to 0.

I write a lot of C++.  There static variables are initialized by their
default constructor and I always forget what happens to POD-types.  I
normally prefer to be explicit.

Anyway, I've reverted the offending commit.

Hope this helps,
--
Olaf Meeuwissen, LPIC-2            FSF Associate Member since 2004-01-27
Support Free Software               Support the Free Software Foundation
https://my.fsf.org/donate                        https://my.fsf.org/join
 GnuPG key: F84A2DD9/B3C0 2F47 EA19 64F4 9F13  F43E B8A4 A88A F84A 2DD9

--
sane-devel mailing list: [hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
             to [hidden email]
Loading...