[sane-devel] [PATCH v3 1/2] use getopt
Matteo Croce
matteo at openwrt.org
Tue Sep 15 10:06:26 UTC 2015
Implement command line parsing using getopt,
replace the help string with a nicer one.
---
NEWS | 7 +++++
doc/saned.man | 15 +++++----
frontend/saned.c | 94 +++++++++++++++++++++++++++++++-------------------------
3 files changed, 66 insertions(+), 50 deletions(-)
diff --git a/NEWS b/NEWS
index c7a7035..42e1930 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,11 @@
-*-Mode: outline-*-
+New with 1.0.25, unreleased yet:
+
+* optional argument to `saned -a' requires no space
+ between `-a' and the username, eg. `saned -auser'
+* saned can bind a specific address with the new `-b' option
+
+
New with 1.0.24, released 2013-09-30:
* Significant enhancements to pixma, genesys, kodakaio, fujitsu, canon_dr.
diff --git a/doc/saned.man b/doc/saned.man
index 8721b15..0264c01 100644
--- a/doc/saned.man
+++ b/doc/saned.man
@@ -4,14 +4,13 @@
saned \- SANE network daemon
.SH SYNOPSIS
.B saned
-.B [ \-a
-.I [ username ]
-.B | \-d
-.I [ n ]
-.B | \-s
-.I [ n ]
-.B | \-h
-.B ]
+.B \-ausername
+|
+.B \-dn
+|
+.B \-sn
+|
+.B \-h
.SH DESCRIPTION
.B saned
is the SANE (Scanner Access Now Easy) daemon that allows remote clients
diff --git a/frontend/saned.c b/frontend/saned.c
index 108512d..64cd076 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;
@@ -3172,7 +3172,7 @@ run_standalone (int argc, char **argv)
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");
}
@@ -3258,12 +3253,21 @@ run_inetd (int argc, char **argv)
handle_connection(fd);
}
+void usage(char *me, int err)
+{
+ fprintf (stderr, "Usage: %s -ausername | -dn | -sn | -h\n", me);
+ exit(err);
+}
int
main (int argc, char *argv[])
{
char options[64] = "";
debug = DBG_WARN;
+ int c;
+ char *user = NULL;
+ char *sock = NULL;
+ int opt_given = 0;
prog_name = strrchr (argv[0], '/');
if (prog_name)
@@ -3274,35 +3278,37 @@ main (int argc, char *argv[])
numchildren = 0;
run_mode = SANED_RUN_INETD;
- if (argc >= 2)
- {
- if (strncmp (argv[1], "-a", 2) == 0)
- run_mode = SANED_RUN_ALONE;
- 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)
- 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);
- else
- exit (EXIT_FAILURE);
- }
+ while((c = getopt(argc, argv, "a::d::s::h")) != -1)
+ {
+ switch(c) {
+ case 'a':
+ if(opt_given++)
+ usage(argv[0], EXIT_FAILURE);
+ run_mode = SANED_RUN_ALONE;
+ user = optarg;
+ break;
+ case 'd':
+ log_to_syslog = SANE_FALSE;
+ case 's':
+ if(opt_given++)
+ usage(argv[0], EXIT_FAILURE);
+ run_mode = SANED_RUN_DEBUG;
+ if(optarg)
+ debug = atoi(optarg);
+ break;
+ case 'h':
+ usage(argv[0], EXIT_SUCCESS);
+ default:
+ usage(argv[0], EXIT_FAILURE);
}
-
+ opt_given = 1;
+ }
+#ifndef HAVE_OS2_H
+ if(optind != argc)
+ usage(argv[0], EXIT_FAILURE);
+#endif
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 (log_to_syslog)
openlog ("saned", LOG_PID | LOG_CONS, LOG_DAEMON);
@@ -3342,11 +3348,15 @@ main (int argc, char *argv[])
if ((run_mode == SANED_RUN_ALONE) || (run_mode == SANED_RUN_DEBUG))
{
- run_standalone(argc, argv);
+ run_standalone(user);
}
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.1.4
More information about the sane-devel
mailing list