aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-10 23:03:30 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-10 23:03:30 +0000
commit80d14beae9ebe64d3be1e6c2771f292977cf6d2c (patch)
treed0dd803ca8b6c70521895fd5ec71ef532861b8e4
parent89054964443b5cb14cba673b86306f534810404a (diff)
downloadbusybox-w32-80d14beae9ebe64d3be1e6c2771f292977cf6d2c.tar.gz
busybox-w32-80d14beae9ebe64d3be1e6c2771f292977cf6d2c.tar.bz2
busybox-w32-80d14beae9ebe64d3be1e6c2771f292977cf6d2c.zip
Rename two config options:
FEATURE_SH_STANDALONE_SHELL => FEATURE_SH_STANDALONE FEATURE_EXEC_PREFER_APPLETS => FEATURE_PREFER_APPLETS Make SH_STANDALONE depend on PREFER_APPLETS. getopt.c: more randomconfig-induced fixes
-rw-r--r--Config.in6
-rw-r--r--TODO_config_nommu4
-rw-r--r--include/applets.h4
-rw-r--r--include/busybox.h2
-rw-r--r--include/libbb.h5
-rw-r--r--libbb/default_error_retval.c1
-rw-r--r--libbb/error_msg_and_die.c4
-rw-r--r--libbb/execable.c2
-rw-r--r--libbb/fflush_stdout_and_exit.c2
-rw-r--r--libbb/vfork_daemon_rexec.c14
-rw-r--r--scripts/defconfig4
-rw-r--r--shell/Config.in4
-rw-r--r--shell/ash.c8
-rw-r--r--shell/hush.c2
-rw-r--r--shell/lash.c2
-rw-r--r--shell/msh.c6
-rw-r--r--util-linux/getopt.c37
17 files changed, 67 insertions, 40 deletions
diff --git a/Config.in b/Config.in
index 59d7d6731..94ed109cb 100644
--- a/Config.in
+++ b/Config.in
@@ -238,7 +238,7 @@ config SELINUX
238 238
239 Most people will leave this set to 'N'. 239 Most people will leave this set to 'N'.
240 240
241config FEATURE_EXEC_PREFER_APPLETS 241config FEATURE_PREFER_APPLETS
242 bool "exec prefers applets" 242 bool "exec prefers applets"
243 default n 243 default n
244 help 244 help
@@ -462,10 +462,10 @@ config INSTALL_APPLET_HARDLINKS
462 462
463config INSTALL_APPLET_DONT 463config INSTALL_APPLET_DONT
464 bool "not installed" 464 bool "not installed"
465 depends on FEATURE_INSTALLER || FEATURE_SH_STANDALONE_SHELL || FEATURE_EXEC_PREFER_APPLETS 465 depends on FEATURE_INSTALLER || FEATURE_SH_STANDALONE || FEATURE_PREFER_APPLETS
466 help 466 help
467 Do not install applet links. Useful when using the -install feature 467 Do not install applet links. Useful when using the -install feature
468 or a standalone shell for rescue pruposes. 468 or a standalone shell for rescue purposes.
469 469
470endchoice 470endchoice
471 471
diff --git a/TODO_config_nommu b/TODO_config_nommu
index 695ac1114..e6f6d9314 100644
--- a/TODO_config_nommu
+++ b/TODO_config_nommu
@@ -35,7 +35,7 @@ CONFIG_FEATURE_SYSLOG=y
35# CONFIG_FEATURE_SUID_CONFIG_QUIET is not set 35# CONFIG_FEATURE_SUID_CONFIG_QUIET is not set
36# CONFIG_FEATURE_HAVE_RPC is not set 36# CONFIG_FEATURE_HAVE_RPC is not set
37# CONFIG_SELINUX is not set 37# CONFIG_SELINUX is not set
38# CONFIG_FEATURE_EXEC_PREFER_APPLETS is not set 38# CONFIG_FEATURE_PREFER_APPLETS is not set
39CONFIG_BUSYBOX_EXEC_PATH="/proc/self/exe" 39CONFIG_BUSYBOX_EXEC_PATH="/proc/self/exe"
40 40
41# 41#
@@ -657,7 +657,7 @@ CONFIG_MSH=y
657# Bourne Shell Options 657# Bourne Shell Options
658# 658#
659CONFIG_FEATURE_SH_EXTRA_QUIET=y 659CONFIG_FEATURE_SH_EXTRA_QUIET=y
660# CONFIG_FEATURE_SH_STANDALONE_SHELL is not set 660# CONFIG_FEATURE_SH_STANDALONE is not set
661 661
662# 662#
663# System Logging Utilities 663# System Logging Utilities
diff --git a/include/applets.h b/include/applets.h
index d05299b69..67f7db4a6 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -56,8 +56,8 @@ s - suid type:
56# define APPLET(name,l,s) { #name, name##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) }, 56# define APPLET(name,l,s) { #name, name##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) },
57# define APPLET_NOUSAGE(name,main,l,s) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) }, 57# define APPLET_NOUSAGE(name,main,l,s) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) },
58# define APPLET_ODDNAME(name,main,l,s,name2) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) }, 58# define APPLET_ODDNAME(name,main,l,s,name2) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) },
59# define APPLET_NOEXEC(name,main,l,s,name2) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) USE_FEATURE_EXEC_PREFER_APPLETS(,1) }, 59# define APPLET_NOEXEC(name,main,l,s,name2) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) USE_FEATURE_PREFER_APPLETS(,1) },
60# define APPLET_NOFORK(name,main,l,s,name2) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) USE_FEATURE_EXEC_PREFER_APPLETS(,1 ,1) }, 60# define APPLET_NOFORK(name,main,l,s,name2) { #name, main##_main USE_FEATURE_INSTALLER(,l) USE_FEATURE_SUID(,s) USE_FEATURE_PREFER_APPLETS(,1 ,1) },
61#endif 61#endif
62 62
63#if ENABLE_INSTALL_NO_USR 63#if ENABLE_INSTALL_NO_USR
diff --git a/include/busybox.h b/include/busybox.h
index 380de9ab8..290ad17f2 100644
--- a/include/busybox.h
+++ b/include/busybox.h
@@ -37,7 +37,7 @@ struct bb_applet {
37#if ENABLE_FEATURE_SUID 37#if ENABLE_FEATURE_SUID
38 __extension__ enum bb_suid_t need_suid:8; 38 __extension__ enum bb_suid_t need_suid:8;
39#endif 39#endif
40#if ENABLE_FEATURE_EXEC_PREFER_APPLETS 40#if ENABLE_FEATURE_PREFER_APPLETS
41 /* true if instead if fork(); exec("applet"); waitpid(); 41 /* true if instead if fork(); exec("applet"); waitpid();
42 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */ 42 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
43 unsigned char noexec; 43 unsigned char noexec;
diff --git a/include/libbb.h b/include/libbb.h
index 2be13891c..04bf6a5cd 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -502,7 +502,7 @@ int execable_file(const char *name);
502char *find_execable(const char *filename); 502char *find_execable(const char *filename);
503int exists_execable(const char *filename); 503int exists_execable(const char *filename);
504 504
505#if ENABLE_FEATURE_EXEC_PREFER_APPLETS 505#if ENABLE_FEATURE_PREFER_APPLETS
506int bb_execvp(const char *file, char *const argv[]); 506int bb_execvp(const char *file, char *const argv[]);
507#define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd) 507#define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd)
508#define BB_EXECLP(prog,cmd,...) \ 508#define BB_EXECLP(prog,cmd,...) \
@@ -609,7 +609,8 @@ llist_t *llist_rev(llist_t *list);
609int write_pidfile(const char *path); 609int write_pidfile(const char *path);
610#define remove_pidfile(f) ((void)unlink(f)) 610#define remove_pidfile(f) ((void)unlink(f))
611#else 611#else
612#define write_pidfile(f) TRUE 612/* Why? #defining it to 1 gives "warning: statement with no effect"... */
613static ATTRIBUTE_ALWAYS_INLINE int write_pidfile(const char *path) { return 1; }
613#define remove_pidfile(f) ((void)0) 614#define remove_pidfile(f) ((void)0)
614#endif 615#endif
615 616
diff --git a/libbb/default_error_retval.c b/libbb/default_error_retval.c
index f4e46a4b5..0b19f2163 100644
--- a/libbb/default_error_retval.c
+++ b/libbb/default_error_retval.c
@@ -13,7 +13,6 @@
13 * that too seems silly. 13 * that too seems silly.
14 */ 14 */
15 15
16#include <stdlib.h>
17#include "libbb.h" 16#include "libbb.h"
18 17
19int xfunc_error_retval = EXIT_FAILURE; 18int xfunc_error_retval = EXIT_FAILURE;
diff --git a/libbb/error_msg_and_die.c b/libbb/error_msg_and_die.c
index 4a9049364..4115046c2 100644
--- a/libbb/error_msg_and_die.c
+++ b/libbb/error_msg_and_die.c
@@ -10,14 +10,14 @@
10#include "libbb.h" 10#include "libbb.h"
11 11
12int die_sleep; 12int die_sleep;
13#if ENABLE_FEATURE_EXEC_PREFER_APPLETS 13#if ENABLE_FEATURE_PREFER_APPLETS
14jmp_buf die_jmp; 14jmp_buf die_jmp;
15#endif 15#endif
16 16
17void xfunc_die(void) 17void xfunc_die(void)
18{ 18{
19 if (die_sleep) { 19 if (die_sleep) {
20 if (ENABLE_FEATURE_EXEC_PREFER_APPLETS && die_sleep < 0) { 20 if (ENABLE_FEATURE_PREFER_APPLETS && die_sleep < 0) {
21 /* Special case. We arrive here if NOFORK applet 21 /* Special case. We arrive here if NOFORK applet
22 * calls xfunc, which then decides to die. 22 * calls xfunc, which then decides to die.
23 * We don't die, but jump instead back to caller. 23 * We don't die, but jump instead back to caller.
diff --git a/libbb/execable.c b/libbb/execable.c
index ee10c6123..d84364753 100644
--- a/libbb/execable.c
+++ b/libbb/execable.c
@@ -60,7 +60,7 @@ int exists_execable(const char *filename)
60 return 0; 60 return 0;
61} 61}
62 62
63#if ENABLE_FEATURE_EXEC_PREFER_APPLETS 63#if ENABLE_FEATURE_PREFER_APPLETS
64/* just like the real execvp, but try to launch an applet named 'file' first 64/* just like the real execvp, but try to launch an applet named 'file' first
65 */ 65 */
66int bb_execvp(const char *file, char *const argv[]) 66int bb_execvp(const char *file, char *const argv[])
diff --git a/libbb/fflush_stdout_and_exit.c b/libbb/fflush_stdout_and_exit.c
index d79827f45..48889dae8 100644
--- a/libbb/fflush_stdout_and_exit.c
+++ b/libbb/fflush_stdout_and_exit.c
@@ -18,7 +18,7 @@ void fflush_stdout_and_exit(int retval)
18 if (fflush(stdout)) 18 if (fflush(stdout))
19 xfunc_die(); 19 xfunc_die();
20 20
21 if (ENABLE_FEATURE_EXEC_PREFER_APPLETS && die_sleep < 0) { 21 if (ENABLE_FEATURE_PREFER_APPLETS && die_sleep < 0) {
22 /* We are in NOFORK applet. Do not exit() directly, 22 /* We are in NOFORK applet. Do not exit() directly,
23 * but use xfunc_die() */ 23 * but use xfunc_die() */
24 xfunc_error_retval = retval; 24 xfunc_error_retval = retval;
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index cf88a2b28..214b645c5 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -102,7 +102,7 @@ int wait_pid(int *wstat, int pid)
102 102
103int spawn_and_wait(char **argv) 103int spawn_and_wait(char **argv)
104{ 104{
105#if ENABLE_FEATURE_EXEC_PREFER_APPLETS 105#if ENABLE_FEATURE_PREFER_APPLETS
106 int rc; 106 int rc;
107 const struct bb_applet *a = find_applet_by_name(argv[0]); 107 const struct bb_applet *a = find_applet_by_name(argv[0]);
108 108
@@ -121,8 +121,13 @@ int spawn_and_wait(char **argv)
121 { 121 {
122 int old_sleep = die_sleep; 122 int old_sleep = die_sleep;
123 int old_x = xfunc_error_retval; 123 int old_x = xfunc_error_retval;
124 die_sleep = -1; /* special flag */ 124 uint32_t old_m = option_mask32;
125 /* xfunc_die() checks for it */ 125
126 xfunc_error_retval = EXIT_FAILURE;
127 /* special flag for xfunc_die(). If xfunc will "die"
128 * in NOFORK applet, xfunc_die() sees negative
129 * die_sleep and longjmp here instead. */
130 die_sleep = -1;
126 131
127 rc = setjmp(die_jmp); 132 rc = setjmp(die_jmp);
128 if (!rc) { 133 if (!rc) {
@@ -144,6 +149,7 @@ int spawn_and_wait(char **argv)
144 149
145 die_sleep = old_sleep; 150 die_sleep = old_sleep;
146 xfunc_error_retval = old_x; 151 xfunc_error_retval = old_x;
152 option_mask32 = old_m;
147 return rc; 153 return rc;
148 } 154 }
149#ifndef BB_NOMMU /* MMU only */ 155#ifndef BB_NOMMU /* MMU only */
@@ -159,7 +165,7 @@ int spawn_and_wait(char **argv)
159 rc = spawn(argv); 165 rc = spawn(argv);
160 w: 166 w:
161 return wait4pid(rc); 167 return wait4pid(rc);
162#else /* !FEATURE_EXEC_PREFER_APPLETS */ 168#else /* !FEATURE_PREFER_APPLETS */
163 return wait4pid(spawn(argv)); 169 return wait4pid(spawn(argv));
164#endif 170#endif
165} 171}
diff --git a/scripts/defconfig b/scripts/defconfig
index 6864b2fa6..009026e5a 100644
--- a/scripts/defconfig
+++ b/scripts/defconfig
@@ -29,7 +29,7 @@ CONFIG_FEATURE_SUID_CONFIG=y
29CONFIG_FEATURE_SUID_CONFIG_QUIET=y 29CONFIG_FEATURE_SUID_CONFIG_QUIET=y
30# CONFIG_FEATURE_HAVE_RPC is not set 30# CONFIG_FEATURE_HAVE_RPC is not set
31# CONFIG_SELINUX is not set 31# CONFIG_SELINUX is not set
32# CONFIG_FEATURE_EXEC_PREFER_APPLETS is not set 32# CONFIG_FEATURE_PREFER_APPLETS is not set
33CONFIG_BUSYBOX_EXEC_PATH="/proc/self/exe" 33CONFIG_BUSYBOX_EXEC_PATH="/proc/self/exe"
34 34
35# 35#
@@ -658,7 +658,7 @@ CONFIG_ASH_RANDOM_SUPPORT=y
658# Bourne Shell Options 658# Bourne Shell Options
659# 659#
660CONFIG_FEATURE_SH_EXTRA_QUIET=y 660CONFIG_FEATURE_SH_EXTRA_QUIET=y
661# CONFIG_FEATURE_SH_STANDALONE_SHELL is not set 661# CONFIG_FEATURE_SH_STANDALONE is not set
662 662
663# 663#
664# System Logging Utilities 664# System Logging Utilities
diff --git a/shell/Config.in b/shell/Config.in
index 4b7270efd..9479e3fed 100644
--- a/shell/Config.in
+++ b/shell/Config.in
@@ -216,10 +216,10 @@ config FEATURE_SH_EXTRA_QUIET
216 help 216 help
217 Remove the busybox introduction when starting a shell. 217 Remove the busybox introduction when starting a shell.
218 218
219config FEATURE_SH_STANDALONE_SHELL 219config FEATURE_SH_STANDALONE
220 bool "Standalone shell" 220 bool "Standalone shell"
221 default n 221 default n
222 depends on MSH || LASH || HUSH || ASH 222 depends on (MSH || LASH || HUSH || ASH) && FEATURE_PREFER_APPLETS
223 help 223 help
224 This option causes the selected busybox shell to use busybox applets 224 This option causes the selected busybox shell to use busybox applets
225 in preference to executables in the PATH whenever possible. For 225 in preference to executables in the PATH whenever possible. For
diff --git a/shell/ash.c b/shell/ash.c
index b4278424a..bebfec8f5 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -6531,7 +6531,7 @@ tryexec(char *cmd, char **argv, char **envp)
6531{ 6531{
6532 int repeated = 0; 6532 int repeated = 0;
6533 6533
6534#if ENABLE_FEATURE_SH_STANDALONE_SHELL 6534#if ENABLE_FEATURE_SH_STANDALONE
6535 if (strchr(cmd, '/') == NULL) { 6535 if (strchr(cmd, '/') == NULL) {
6536 const struct bb_applet *a; 6536 const struct bb_applet *a;
6537 6537
@@ -6596,7 +6596,7 @@ shellexec(char **argv, const char *path, int idx)
6596 clearredir(1); 6596 clearredir(1);
6597 envp = environment(); 6597 envp = environment();
6598 if (strchr(argv[0], '/') 6598 if (strchr(argv[0], '/')
6599#if ENABLE_FEATURE_SH_STANDALONE_SHELL 6599#if ENABLE_FEATURE_SH_STANDALONE
6600 || find_applet_by_name(argv[0]) 6600 || find_applet_by_name(argv[0])
6601#endif 6601#endif
6602 ) { 6602 ) {
@@ -11116,7 +11116,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
11116 return; 11116 return;
11117 } 11117 }
11118 11118
11119#if ENABLE_FEATURE_SH_STANDALONE_SHELL 11119#if ENABLE_FEATURE_SH_STANDALONE
11120 if (find_applet_by_name(name)) { 11120 if (find_applet_by_name(name)) {
11121 entry->cmdtype = CMDNORMAL; 11121 entry->cmdtype = CMDNORMAL;
11122 entry->u.index = -1; 11122 entry->u.index = -1;
@@ -11341,7 +11341,7 @@ helpcmd(int argc, char **argv)
11341 col = 0; 11341 col = 0;
11342 } 11342 }
11343 } 11343 }
11344#if ENABLE_FEATURE_SH_STANDALONE_SHELL 11344#if ENABLE_FEATURE_SH_STANDALONE
11345 for (i = 0; i < NUM_APPLETS; i++) { 11345 for (i = 0; i < NUM_APPLETS; i++) {
11346 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name); 11346 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name);
11347 if (col > 60) { 11347 if (col > 60) {
diff --git a/shell/hush.c b/shell/hush.c
index 666604dd9..f6c69a221 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -1112,7 +1112,7 @@ static void pseudo_exec(struct child_prog *child)
1112 * really dislike relying on /proc for things. We could exec ourself 1112 * really dislike relying on /proc for things. We could exec ourself
1113 * from global_argv[0], but if we are in a chroot, we may not be able 1113 * from global_argv[0], but if we are in a chroot, we may not be able
1114 * to find ourself... */ 1114 * to find ourself... */
1115#if ENABLE_FEATURE_SH_STANDALONE_SHELL 1115#if ENABLE_FEATURE_SH_STANDALONE
1116 { 1116 {
1117 int argc_l; 1117 int argc_l;
1118 char** argv_l = child->argv; 1118 char** argv_l = child->argv;
diff --git a/shell/lash.c b/shell/lash.c
index f9d9deb62..99e2b1f06 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -1164,7 +1164,7 @@ static int pseudo_exec(struct child_prog *child)
1164 * /bin/foo invocation will fork and exec /bin/foo, even if 1164 * /bin/foo invocation will fork and exec /bin/foo, even if
1165 * /bin/foo is a symlink to busybox. 1165 * /bin/foo is a symlink to busybox.
1166 */ 1166 */
1167 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) { 1167 if (ENABLE_FEATURE_SH_STANDALONE) {
1168 char **argv_l = child->argv; 1168 char **argv_l = child->argv;
1169 int argc_l; 1169 int argc_l;
1170 1170
diff --git a/shell/msh.c b/shell/msh.c
index 23a7c0498..0337a4f06 100644
--- a/shell/msh.c
+++ b/shell/msh.c
@@ -37,7 +37,7 @@
37# define DEFAULT_SHELL "/proc/self/exe" 37# define DEFAULT_SHELL "/proc/self/exe"
38# define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe" 38# define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe"
39# define BB_BANNER "busybox standalone" 39# define BB_BANNER "busybox standalone"
40# define ENABLE_FEATURE_SH_STANDALONE_SHELL 0 40# define ENABLE_FEATURE_SH_STANDALONE 0
41# define bb_msg_memory_exhausted "memory exhausted" 41# define bb_msg_memory_exhausted "memory exhausted"
42# define xmalloc(size) malloc(size) 42# define xmalloc(size) malloc(size)
43# define msh_main(argc,argv) main(argc,argv) 43# define msh_main(argc,argv) main(argc,argv)
@@ -3064,7 +3064,7 @@ static const char *rexecve(char *c, char **v, char **envp)
3064 int eacces = 0, asis = 0; 3064 int eacces = 0, asis = 0;
3065 char *name = c; 3065 char *name = c;
3066 3066
3067 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) { 3067 if (ENABLE_FEATURE_SH_STANDALONE) {
3068 optind = 1; 3068 optind = 1;
3069 if (find_applet_by_name(name)) { 3069 if (find_applet_by_name(name)) {
3070 /* We have to exec here since we vforked. Running 3070 /* We have to exec here since we vforked. Running
@@ -3195,7 +3195,7 @@ static int dohelp(struct op *t)
3195 } 3195 }
3196 x++; 3196 x++;
3197 } 3197 }
3198#if ENABLE_FEATURE_SH_STANDALONE_SHELL 3198#if ENABLE_FEATURE_SH_STANDALONE
3199 { 3199 {
3200 const struct bb_applet *applet = applets; 3200 const struct bb_applet *applet = applets;
3201 3201
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index 85a1d4410..68e69de5c 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -38,8 +38,10 @@
38 mode */ 38 mode */
39enum { 39enum {
40 NON_OPT = 1, 40 NON_OPT = 1,
41#if ENABLE_GETOPT_LONG
41/* LONG_OPT is the code that is returned when a long option is found. */ 42/* LONG_OPT is the code that is returned when a long option is found. */
42 LONG_OPT = 2 43 LONG_OPT = 2
44#endif
43}; 45};
44 46
45/* For finding activated option flags. Must match getopt32 call! */ 47/* For finding activated option flags. Must match getopt32 call! */
@@ -51,8 +53,10 @@ enum {
51 OPT_s = 0x10, // -s 53 OPT_s = 0x10, // -s
52 OPT_T = 0x20, // -T 54 OPT_T = 0x20, // -T
53 OPT_u = 0x40, // -u 55 OPT_u = 0x40, // -u
56#if ENABLE_GETOPT_LONG
54 OPT_a = 0x80, // -a 57 OPT_a = 0x80, // -a
55 OPT_l = 0x100, // -l 58 OPT_l = 0x100, // -l
59#endif
56 SHELL_IS_TCSH = 0x8000, /* hijack this bit for other purposes */ 60 SHELL_IS_TCSH = 0x8000, /* hijack this bit for other purposes */
57}; 61};
58 62
@@ -137,31 +141,45 @@ static const char *normalize(const char *arg)
137 * optstr must contain the short options, and longopts the long options. 141 * optstr must contain the short options, and longopts the long options.
138 * Other settings are found in global variables. 142 * Other settings are found in global variables.
139 */ 143 */
140static int generate_output(char * argv[],int argc,const char *optstr, 144#if !ENABLE_GETOPT_LONG
141 const struct option *longopts) 145#define generate_output(argv,argc,optstr,longopts) generate_output(argv,argc,optstr)
146#endif
147static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts)
142{ 148{
143 int exit_code = 0; /* We assume everything will be OK */ 149 int exit_code = 0; /* We assume everything will be OK */
144 unsigned opt; 150 unsigned opt;
151#if ENABLE_GETOPT_LONG
145 int longindex; 152 int longindex;
153#endif
146 const char *charptr; 154 const char *charptr;
147 155
148 if (quiet_errors) /* No error reporting from getopt(3) */ 156 if (quiet_errors) /* No error reporting from getopt(3) */
149 opterr = 0; 157 opterr = 0;
150 optind = 0; /* Reset getopt(3) */ 158 optind = 0; /* Reset getopt(3) */
151 159
152 while ((opt = (alternative ? 160 while (1) {
153 getopt_long_only(argc,argv,optstr,longopts,&longindex) : 161 opt =
154 getopt_long(argc,argv,optstr,longopts,&longindex))) 162#if ENABLE_GETOPT_LONG
155 != EOF) 163 alternative ?
164 getopt_long_only(argc, argv, optstr, longopts, &longindex) :
165 getopt_long(argc, argv, optstr, longopts, &longindex);
166#else
167 getopt(argc, argv, optstr);
168#endif
169 if (opt == EOF)
170 break;
156 if (opt == '?' || opt == ':' ) 171 if (opt == '?' || opt == ':' )
157 exit_code = 1; 172 exit_code = 1;
158 else if (!quiet_output) { 173 else if (!quiet_output) {
174#if ENABLE_GETOPT_LONG
159 if (opt == LONG_OPT) { 175 if (opt == LONG_OPT) {
160 printf(" --%s", longopts[longindex].name); 176 printf(" --%s", longopts[longindex].name);
161 if (longopts[longindex].has_arg) 177 if (longopts[longindex].has_arg)
162 printf(" %s", 178 printf(" %s",
163 normalize(optarg ? optarg : "")); 179 normalize(optarg ? optarg : ""));
164 } else if (opt == NON_OPT) 180 } else
181#endif
182 if (opt == NON_OPT)
165 printf(" %s", normalize(optarg)); 183 printf(" %s", normalize(optarg));
166 else { 184 else {
167 printf(" -%c", opt); 185 printf(" -%c", opt);
@@ -171,6 +189,7 @@ static int generate_output(char * argv[],int argc,const char *optstr,
171 normalize(optarg ? optarg : "")); 189 normalize(optarg ? optarg : ""));
172 } 190 }
173 } 191 }
192 }
174 193
175 if (!quiet_output) { 194 if (!quiet_output) {
176 printf(" --"); 195 printf(" --");
@@ -181,6 +200,7 @@ static int generate_output(char * argv[],int argc,const char *optstr,
181 return exit_code; 200 return exit_code;
182} 201}
183 202
203#if ENABLE_GETOPT_LONG
184/* 204/*
185 * Register several long options. options is a string of long options, 205 * Register several long options. options is a string of long options,
186 * separated by commas or whitespace. 206 * separated by commas or whitespace.
@@ -224,6 +244,7 @@ static struct option *add_long_options(struct option *long_options, char *option
224 } 244 }
225 return long_options; 245 return long_options;
226} 246}
247#endif
227 248
228static void set_shell(const char *new_shell) 249static void set_shell(const char *new_shell)
229{ 250{
@@ -262,13 +283,13 @@ static const struct option longopts[] = {
262int getopt_main(int argc, char *argv[]); 283int getopt_main(int argc, char *argv[]);
263int getopt_main(int argc, char *argv[]) 284int getopt_main(int argc, char *argv[])
264{ 285{
265 struct option *long_options = NULL;
266 char *optstr = NULL; 286 char *optstr = NULL;
267 char *name = NULL; 287 char *name = NULL;
268 unsigned opt; 288 unsigned opt;
269 const char *compatible; 289 const char *compatible;
270 char *s_arg; 290 char *s_arg;
271#if ENABLE_GETOPT_LONG 291#if ENABLE_GETOPT_LONG
292 struct option *long_options = NULL;
272 llist_t *l_arg = NULL; 293 llist_t *l_arg = NULL;
273#endif 294#endif
274 295