aboutsummaryrefslogtreecommitdiff
path: root/shell/hush.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-08-11 01:32:46 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-08-11 01:32:46 +0200
commit74d40589288890fffea437ed2f38ad1f2dc740e5 (patch)
treefafc1724b3b2c11280c067eaa4eaabb57e5f34cd /shell/hush.c
parent4628945cd8d4679912f126d5f18f954210abb7d0 (diff)
downloadbusybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.gz
busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.bz2
busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.zip
hush: getopts builtin
function old new delta builtin_getopts - 271 +271 bltins1 372 384 +12 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/0 up/down: 283/0) Total: 283 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/hush.c')
-rw-r--r--shell/hush.c77
1 files changed, 75 insertions, 2 deletions
diff --git a/shell/hush.c b/shell/hush.c
index b53d1dcfb..dba12c12e 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -48,7 +48,7 @@
48 * tilde expansion 48 * tilde expansion
49 * aliases 49 * aliases
50 * builtins mandated by standards we don't support: 50 * builtins mandated by standards we don't support:
51 * [un]alias, command, fc, getopts: 51 * [un]alias, command, fc:
52 * command -v CMD: print "/path/to/CMD" 52 * command -v CMD: print "/path/to/CMD"
53 * prints "CMD" for builtins 53 * prints "CMD" for builtins
54 * prints "alias ALIAS='EXPANSION'" for aliases 54 * prints "alias ALIAS='EXPANSION'" for aliases
@@ -58,7 +58,6 @@
58 * (can use this to override standalone shell as well) 58 * (can use this to override standalone shell as well)
59 * -p: use default $PATH 59 * -p: use default $PATH
60 * command BLTIN: disables special-ness (e.g. errors do not abort) 60 * command BLTIN: disables special-ness (e.g. errors do not abort)
61 * getopts: getopt() for shells
62 * fc -l[nr] [BEG] [END]: list range of commands in history 61 * fc -l[nr] [BEG] [END]: list range of commands in history
63 * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands 62 * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands
64 * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP 63 * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP
@@ -294,6 +293,11 @@
294//config: default y 293//config: default y
295//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 294//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
296//config: 295//config:
296//config:config HUSH_GETOPTS
297//config: bool "getopts builtin"
298//config: default y
299//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
300//config:
297//config:config HUSH_MEMLEAK 301//config:config HUSH_MEMLEAK
298//config: bool "memleak builtin (debugging)" 302//config: bool "memleak builtin (debugging)"
299//config: default n 303//config: default n
@@ -983,6 +987,9 @@ static int builtin_readonly(char **argv) FAST_FUNC;
983static int builtin_fg_bg(char **argv) FAST_FUNC; 987static int builtin_fg_bg(char **argv) FAST_FUNC;
984static int builtin_jobs(char **argv) FAST_FUNC; 988static int builtin_jobs(char **argv) FAST_FUNC;
985#endif 989#endif
990#if ENABLE_HUSH_GETOPTS
991static int builtin_getopts(char **argv) FAST_FUNC;
992#endif
986#if ENABLE_HUSH_HELP 993#if ENABLE_HUSH_HELP
987static int builtin_help(char **argv) FAST_FUNC; 994static int builtin_help(char **argv) FAST_FUNC;
988#endif 995#endif
@@ -1079,6 +1086,9 @@ static const struct built_in_command bltins1[] = {
1079#if ENABLE_HUSH_JOB 1086#if ENABLE_HUSH_JOB
1080 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), 1087 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"),
1081#endif 1088#endif
1089#if ENABLE_HUSH_GETOPTS
1090 BLTIN("getopts" , builtin_getopts , NULL),
1091#endif
1082#if ENABLE_HUSH_HELP 1092#if ENABLE_HUSH_HELP
1083 BLTIN("help" , builtin_help , NULL), 1093 BLTIN("help" , builtin_help , NULL),
1084#endif 1094#endif
@@ -9859,6 +9869,69 @@ static int FAST_FUNC builtin_shift(char **argv)
9859 return EXIT_FAILURE; 9869 return EXIT_FAILURE;
9860} 9870}
9861 9871
9872#if ENABLE_HUSH_GETOPTS
9873static int FAST_FUNC builtin_getopts(char **argv)
9874{
9875/*
9876TODO:
9877if a character is followed by a colon, the option is expected to have
9878an argument, which should be separated from it by white space.
9879When an option requires an argument, getopts places that argument into
9880the variable OPTARG.
9881
9882If an invalid option is seen, getopts places ? into VAR and, if
9883not silent, prints an error message and unsets OPTARG. If
9884getopts is silent, the option character found is placed in
9885OPTARG and no diagnostic message is printed.
9886
9887If a required argument is not found, and getopts is not silent,
9888a question mark (?) is placed in VAR, OPTARG is unset, and a
9889diagnostic message is printed. If getopts is silent, then a
9890colon (:) is placed in VAR and OPTARG is set to the option
9891character found.
9892
9893Test that VAR is a valid variable name?
9894*/
9895 char cbuf[2];
9896 const char *cp, *optstring, *var;
9897 int c, exitcode;
9898
9899 optstring = *++argv;
9900 if (!optstring || !(var = *++argv)) {
9901 bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]");
9902 return EXIT_FAILURE;
9903 }
9904
9905 cp = get_local_var_value("OPTERR");
9906 opterr = cp ? atoi(cp) : 1;
9907 cp = get_local_var_value("OPTIND");
9908 optind = cp ? atoi(cp) : 0;
9909
9910 /* getopts stops on first non-option. Add "+" to force that */
9911 /*if (optstring[0] != '+')*/ {
9912 char *s = alloca(strlen(optstring) + 2);
9913 sprintf(s, "+%s", optstring);
9914 optstring = s;
9915 }
9916
9917 if (argv[1])
9918 argv[0] = G.global_argv[0]; /* for error messages */
9919 else
9920 argv = G.global_argv;
9921 c = getopt(string_array_len(argv), argv, optstring);
9922 exitcode = EXIT_SUCCESS;
9923 if (c < 0) { /* -1: end of options */
9924 exitcode = EXIT_FAILURE;
9925 c = '?';
9926 }
9927 cbuf[0] = c;
9928 cbuf[1] = '\0';
9929 set_local_var_from_halves(var, cbuf);
9930 set_local_var_from_halves("OPTIND", utoa(optind));
9931 return exitcode;
9932}
9933#endif
9934
9862static int FAST_FUNC builtin_source(char **argv) 9935static int FAST_FUNC builtin_source(char **argv)
9863{ 9936{
9864 char *arg_path, *filename; 9937 char *arg_path, *filename;