shell.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /* shell.c -- GNU's idea of the POSIX shell specification. */
  2. /* Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Bash, the Bourne Again SHell.
  4. Bash is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Bash is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bash. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Birthdate:
  17. Sunday, January 10th, 1988.
  18. Initial author: Brian Fox
  19. */
  20. #define INSTALL_DEBUG_MODE
  21. #include "config.h"
  22. #include "bashtypes.h"
  23. #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
  24. # include <sys/file.h>
  25. #endif
  26. #include "posixstat.h"
  27. #include "posixtime.h"
  28. #include "bashansi.h"
  29. #include <stdio.h>
  30. #include <signal.h>
  31. #include <errno.h>
  32. #include "filecntl.h"
  33. #if defined (HAVE_PWD_H)
  34. # include <pwd.h>
  35. #endif
  36. #if defined (HAVE_UNISTD_H)
  37. # include <unistd.h>
  38. #endif
  39. #include "bashintl.h"
  40. #define NEED_SH_SETLINEBUF_DECL /* used in externs.h */
  41. #include "shell.h"
  42. #include "flags.h"
  43. #include "trap.h"
  44. #include "mailcheck.h"
  45. #include "builtins.h"
  46. #include "builtins/common.h"
  47. #if defined (JOB_CONTROL)
  48. #include "jobs.h"
  49. #else
  50. extern int initialize_job_control __P((int));
  51. extern int get_tty_state __P((void));
  52. #endif /* JOB_CONTROL */
  53. #include "input.h"
  54. #include "execute_cmd.h"
  55. #include "findcmd.h"
  56. #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
  57. # include <malloc/shmalloc.h>
  58. #endif
  59. #if defined (HISTORY)
  60. # include "bashhist.h"
  61. # include <readline/history.h>
  62. #endif
  63. #if defined (READLINE)
  64. # include <readline/readline.h>
  65. # include "bashline.h"
  66. #endif
  67. #include <tilde/tilde.h>
  68. #include <glob/strmatch.h>
  69. #if defined (__OPENNT)
  70. # include <opennt/opennt.h>
  71. #endif
  72. #if !defined (HAVE_GETPW_DECLS)
  73. extern struct passwd *getpwuid ();
  74. #endif /* !HAVE_GETPW_DECLS */
  75. #if !defined (errno)
  76. extern int errno;
  77. #endif
  78. #if defined (NO_MAIN_ENV_ARG)
  79. extern char **environ; /* used if no third argument to main() */
  80. #endif
  81. extern char *dist_version, *release_status;
  82. extern int patch_level, build_version;
  83. extern int shell_level;
  84. extern int subshell_environment;
  85. extern int running_in_background;
  86. extern int last_command_exit_value;
  87. extern int line_number;
  88. extern int expand_aliases;
  89. extern int array_needs_making;
  90. extern int gnu_error_format;
  91. extern char *primary_prompt, *secondary_prompt;
  92. extern char *this_command_name;
  93. /* Non-zero means that this shell has already been run; i.e. you should
  94. call shell_reinitialize () if you need to start afresh. */
  95. int shell_initialized = 0;
  96. COMMAND *global_command = (COMMAND *)NULL;
  97. /* Information about the current user. */
  98. struct user_info current_user =
  99. {
  100. (uid_t)-1, (uid_t)-1, (gid_t)-1, (gid_t)-1,
  101. (char *)NULL, (char *)NULL, (char *)NULL
  102. };
  103. /* The current host's name. */
  104. char *current_host_name = (char *)NULL;
  105. /* Non-zero means that this shell is a login shell.
  106. Specifically:
  107. 0 = not login shell.
  108. 1 = login shell from getty (or equivalent fake out)
  109. -1 = login shell from "--login" (or -l) flag.
  110. -2 = both from getty, and from flag.
  111. */
  112. int login_shell = 0;
  113. /* Non-zero means that at this moment, the shell is interactive. In
  114. general, this means that the shell is at this moment reading input
  115. from the keyboard. */
  116. int interactive = 0;
  117. /* Non-zero means that the shell was started as an interactive shell. */
  118. int interactive_shell = 0;
  119. /* Non-zero means to send a SIGHUP to all jobs when an interactive login
  120. shell exits. */
  121. int hup_on_exit = 0;
  122. /* Non-zero means to list status of running and stopped jobs at shell exit */
  123. int check_jobs_at_exit = 0;
  124. /* Non-zero means to change to a directory name supplied as a command name */
  125. int autocd = 0;
  126. /* Tells what state the shell was in when it started:
  127. 0 = non-interactive shell script
  128. 1 = interactive
  129. 2 = -c command
  130. 3 = wordexp evaluation
  131. This is a superset of the information provided by interactive_shell.
  132. */
  133. int startup_state = 0;
  134. int reading_shell_script = 0;
  135. /* Special debugging helper. */
  136. int debugging_login_shell = 0;
  137. /* The environment that the shell passes to other commands. */
  138. char **shell_environment;
  139. /* Non-zero when we are executing a top-level command. */
  140. int executing = 0;
  141. /* The number of commands executed so far. */
  142. int current_command_number = 1;
  143. /* Non-zero is the recursion depth for commands. */
  144. int indirection_level = 0;
  145. /* The name of this shell, as taken from argv[0]. */
  146. char *shell_name = (char *)NULL;
  147. /* time in seconds when the shell was started */
  148. time_t shell_start_time;
  149. /* Are we running in an emacs shell window? */
  150. int running_under_emacs;
  151. /* Do we have /dev/fd? */
  152. #ifdef HAVE_DEV_FD
  153. int have_devfd = HAVE_DEV_FD;
  154. #else
  155. int have_devfd = 0;
  156. #endif
  157. /* The name of the .(shell)rc file. */
  158. static char *bashrc_file = DEFAULT_BASHRC;
  159. /* Non-zero means to act more like the Bourne shell on startup. */
  160. static int act_like_sh;
  161. /* Non-zero if this shell is being run by `su'. */
  162. static int su_shell;
  163. /* Non-zero if we have already expanded and sourced $ENV. */
  164. static int sourced_env;
  165. /* Is this shell running setuid? */
  166. static int running_setuid;
  167. /* Values for the long-winded argument names. */
  168. static int debugging; /* Do debugging things. */
  169. static int no_rc; /* Don't execute ~/.bashrc */
  170. static int no_profile; /* Don't execute .profile */
  171. static int do_version; /* Display interesting version info. */
  172. static int make_login_shell; /* Make this shell be a `-bash' shell. */
  173. static int want_initial_help; /* --help option */
  174. int debugging_mode = 0; /* In debugging mode with --debugger */
  175. #if defined (READLINE)
  176. int no_line_editing = 0; /* non-zero -> don't do fancy line editing. */
  177. #else
  178. int no_line_editing = 1; /* can't have line editing without readline */
  179. #endif
  180. int dump_translatable_strings; /* Dump strings in $"...", don't execute. */
  181. int dump_po_strings; /* Dump strings in $"..." in po format */
  182. int wordexp_only = 0; /* Do word expansion only */
  183. int protected_mode = 0; /* No command substitution with --wordexp */
  184. #if defined (STRICT_POSIX)
  185. int posixly_correct = 1; /* Non-zero means posix.2 superset. */
  186. #else
  187. int posixly_correct = 0; /* Non-zero means posix.2 superset. */
  188. #endif
  189. /* Some long-winded argument names. These are obviously new. */
  190. #define Int 1
  191. #define Charp 2
  192. static const struct {
  193. const char *name;
  194. int type;
  195. int *int_value;
  196. char **char_value;
  197. } long_args[] = {
  198. { "debug", Int, &debugging, (char **)0x0 },
  199. #if defined (DEBUGGER)
  200. { "debugger", Int, &debugging_mode, (char **)0x0 },
  201. #endif
  202. { "dump-po-strings", Int, &dump_po_strings, (char **)0x0 },
  203. { "dump-strings", Int, &dump_translatable_strings, (char **)0x0 },
  204. { "help", Int, &want_initial_help, (char **)0x0 },
  205. { "init-file", Charp, (int *)0x0, &bashrc_file },
  206. { "login", Int, &make_login_shell, (char **)0x0 },
  207. { "noediting", Int, &no_line_editing, (char **)0x0 },
  208. { "noprofile", Int, &no_profile, (char **)0x0 },
  209. { "norc", Int, &no_rc, (char **)0x0 },
  210. { "posix", Int, &posixly_correct, (char **)0x0 },
  211. #if defined (WORDEXP_OPTION)
  212. { "protected", Int, &protected_mode, (char **)0x0 },
  213. #endif
  214. { "rcfile", Charp, (int *)0x0, &bashrc_file },
  215. #if defined (RESTRICTED_SHELL)
  216. { "restricted", Int, &restricted, (char **)0x0 },
  217. #endif
  218. { "verbose", Int, &verbose_flag, (char **)0x0 },
  219. { "version", Int, &do_version, (char **)0x0 },
  220. #if defined (WORDEXP_OPTION)
  221. { "wordexp", Int, &wordexp_only, (char **)0x0 },
  222. #endif
  223. { (char *)0x0, Int, (int *)0x0, (char **)0x0 }
  224. };
  225. /* These are extern so execute_simple_command can set them, and then
  226. longjmp back to main to execute a shell script, instead of calling
  227. main () again and resulting in indefinite, possibly fatal, stack
  228. growth. */
  229. procenv_t subshell_top_level;
  230. int subshell_argc;
  231. char **subshell_argv;
  232. char **subshell_envp;
  233. char *exec_argv0;
  234. #if defined (BUFFERED_INPUT)
  235. /* The file descriptor from which the shell is reading input. */
  236. int default_buffered_input = -1;
  237. #endif
  238. /* The following two variables are not static so they can show up in $-. */
  239. int read_from_stdin; /* -s flag supplied */
  240. int want_pending_command; /* -c flag supplied */
  241. /* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */
  242. char *command_execution_string; /* argument to -c option */
  243. int malloc_trace_at_exit = 0;
  244. static int shell_reinitialized = 0;
  245. static FILE *default_input;
  246. static STRING_INT_ALIST *shopt_alist;
  247. static int shopt_ind = 0, shopt_len = 0;
  248. static int parse_long_options __P((char **, int, int));
  249. static int parse_shell_options __P((char **, int, int));
  250. static int bind_args __P((char **, int, int, int));
  251. static void start_debugger __P((void));
  252. static void add_shopt_to_alist __P((char *, int));
  253. static void run_shopt_alist __P((void));
  254. static void execute_env_file __P((char *));
  255. static void run_startup_files __P((void));
  256. static int open_shell_script __P((char *));
  257. static void set_bash_input __P((void));
  258. static int run_one_command __P((char *));
  259. #if defined (WORDEXP_OPTION)
  260. static int run_wordexp __P((char *));
  261. #endif
  262. static int uidget __P((void));
  263. static void init_interactive __P((void));
  264. static void init_noninteractive __P((void));
  265. static void init_interactive_script __P((void));
  266. static void set_shell_name __P((char *));
  267. static void shell_initialize __P((void));
  268. static void shell_reinitialize __P((void));
  269. static void show_shell_usage __P((FILE *, int));
  270. #ifdef __CYGWIN__
  271. static void
  272. _cygwin32_check_tmp ()
  273. {
  274. struct stat sb;
  275. if (stat ("/tmp", &sb) < 0)
  276. internal_warning (_("could not find /tmp, please create!"));
  277. else
  278. {
  279. if (S_ISDIR (sb.st_mode) == 0)
  280. internal_warning (_("/tmp must be a valid directory name"));
  281. }
  282. }
  283. #endif /* __CYGWIN__ */
  284. #if defined (NO_MAIN_ENV_ARG)
  285. /* systems without third argument to main() */
  286. int
  287. main (argc, argv)
  288. int argc;
  289. char **argv;
  290. #else /* !NO_MAIN_ENV_ARG */
  291. int
  292. main (argc, argv, env)
  293. int argc;
  294. char **argv, **env;
  295. #endif /* !NO_MAIN_ENV_ARG */
  296. {
  297. register int i;
  298. int code, old_errexit_flag;
  299. #if defined (RESTRICTED_SHELL)
  300. int saverst;
  301. #endif
  302. volatile int locally_skip_execution;
  303. volatile int arg_index, top_level_arg_index;
  304. #ifdef __OPENNT
  305. char **env;
  306. env = environ;
  307. #endif /* __OPENNT */
  308. USE_VAR(argc);
  309. USE_VAR(argv);
  310. USE_VAR(env);
  311. USE_VAR(code);
  312. USE_VAR(old_errexit_flag);
  313. #if defined (RESTRICTED_SHELL)
  314. USE_VAR(saverst);
  315. #endif
  316. /* Catch early SIGINTs. */
  317. code = setjmp_nosigs (top_level);
  318. if (code)
  319. exit (2);
  320. xtrace_init ();
  321. #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
  322. malloc_set_register (1); /* XXX - change to 1 for malloc debugging */
  323. #endif
  324. check_dev_tty ();
  325. #ifdef __CYGWIN__
  326. _cygwin32_check_tmp ();
  327. #endif /* __CYGWIN__ */
  328. /* Wait forever if we are debugging a login shell. */
  329. while (debugging_login_shell) sleep (3);
  330. set_default_locale ();
  331. running_setuid = uidget ();
  332. if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
  333. posixly_correct = 1;
  334. #if defined (USE_GNU_MALLOC_LIBRARY)
  335. mcheck (programming_error, (void (*) ())0);
  336. #endif /* USE_GNU_MALLOC_LIBRARY */
  337. if (setjmp_sigs (subshell_top_level))
  338. {
  339. argc = subshell_argc;
  340. argv = subshell_argv;
  341. env = subshell_envp;
  342. sourced_env = 0;
  343. }
  344. shell_reinitialized = 0;
  345. /* Initialize `local' variables for all `invocations' of main (). */
  346. arg_index = 1;
  347. if (arg_index > argc)
  348. arg_index = argc;
  349. command_execution_string = (char *)NULL;
  350. want_pending_command = locally_skip_execution = read_from_stdin = 0;
  351. default_input = stdin;
  352. #if defined (BUFFERED_INPUT)
  353. default_buffered_input = -1;
  354. #endif
  355. /* Fix for the `infinite process creation' bug when running shell scripts
  356. from startup files on System V. */
  357. login_shell = make_login_shell = 0;
  358. /* If this shell has already been run, then reinitialize it to a
  359. vanilla state. */
  360. if (shell_initialized || shell_name)
  361. {
  362. /* Make sure that we do not infinitely recurse as a login shell. */
  363. if (*shell_name == '-')
  364. shell_name++;
  365. shell_reinitialize ();
  366. if (setjmp_nosigs (top_level))
  367. exit (2);
  368. }
  369. shell_environment = env;
  370. set_shell_name (argv[0]);
  371. shell_start_time = NOW; /* NOW now defined in general.h */
  372. /* Parse argument flags from the input line. */
  373. /* Find full word arguments first. */
  374. arg_index = parse_long_options (argv, arg_index, argc);
  375. if (want_initial_help)
  376. {
  377. show_shell_usage (stdout, 1);
  378. exit (EXECUTION_SUCCESS);
  379. }
  380. if (do_version)
  381. {
  382. show_shell_version (1);
  383. exit (EXECUTION_SUCCESS);
  384. }
  385. echo_input_at_read = verbose_flag; /* --verbose given */
  386. /* All done with full word options; do standard shell option parsing.*/
  387. this_command_name = shell_name; /* for error reporting */
  388. arg_index = parse_shell_options (argv, arg_index, argc);
  389. /* If user supplied the "--login" (or -l) flag, then set and invert
  390. LOGIN_SHELL. */
  391. if (make_login_shell)
  392. {
  393. login_shell++;
  394. login_shell = -login_shell;
  395. }
  396. set_login_shell ("login_shell", login_shell != 0);
  397. if (dump_po_strings)
  398. dump_translatable_strings = 1;
  399. if (dump_translatable_strings)
  400. read_but_dont_execute = 1;
  401. if (running_setuid && privileged_mode == 0)
  402. disable_priv_mode ();
  403. /* Need to get the argument to a -c option processed in the
  404. above loop. The next arg is a command to execute, and the
  405. following args are $0...$n respectively. */
  406. if (want_pending_command)
  407. {
  408. command_execution_string = argv[arg_index];
  409. if (command_execution_string == 0)
  410. {
  411. report_error (_("%s: option requires an argument"), "-c");
  412. exit (EX_BADUSAGE);
  413. }
  414. arg_index++;
  415. }
  416. this_command_name = (char *)NULL;
  417. /* First, let the outside world know about our interactive status.
  418. A shell is interactive if the `-i' flag was given, or if all of
  419. the following conditions are met:
  420. no -c command
  421. no arguments remaining or the -s flag given
  422. standard input is a terminal
  423. standard error is a terminal
  424. Refer to Posix.2, the description of the `sh' utility. */
  425. if (forced_interactive || /* -i flag */
  426. (!command_execution_string && /* No -c command and ... */
  427. wordexp_only == 0 && /* No --wordexp and ... */
  428. ((arg_index == argc) || /* no remaining args or... */
  429. read_from_stdin) && /* -s flag with args, and */
  430. isatty (fileno (stdin)) && /* Input is a terminal and */
  431. isatty (fileno (stderr)))) /* error output is a terminal. */
  432. init_interactive ();
  433. else
  434. init_noninteractive ();
  435. /*
  436. * Some systems have the bad habit of starting login shells with lots of open
  437. * file descriptors. For instance, most systems that have picked up the
  438. * pre-4.0 Sun YP code leave a file descriptor open each time you call one
  439. * of the getpw* functions, and it's set to be open across execs. That
  440. * means one for login, one for xterm, one for shelltool, etc. There are
  441. * also systems that open persistent FDs to other agents or files as part
  442. * of process startup; these need to be set to be close-on-exec.
  443. */
  444. if (login_shell && interactive_shell)
  445. {
  446. for (i = 3; i < 20; i++)
  447. SET_CLOSE_ON_EXEC (i);
  448. }
  449. /* If we're in a strict Posix.2 mode, turn on interactive comments,
  450. alias expansion in non-interactive shells, and other Posix.2 things. */
  451. if (posixly_correct)
  452. {
  453. bind_variable ("POSIXLY_CORRECT", "y", 0);
  454. sv_strict_posix ("POSIXLY_CORRECT");
  455. }
  456. /* Now we run the shopt_alist and process the options. */
  457. if (shopt_alist)
  458. run_shopt_alist ();
  459. /* From here on in, the shell must be a normal functioning shell.
  460. Variables from the environment are expected to be set, etc. */
  461. shell_initialize ();
  462. set_default_lang ();
  463. set_default_locale_vars ();
  464. /*
  465. * M-x term -> TERM=eterm-color INSIDE_EMACS='251,term:0.96' (eterm)
  466. * M-x shell -> TERM='dumb' INSIDE_EMACS='25.1,comint' (no line editing)
  467. *
  468. * Older versions of Emacs may set EMACS to 't' or to something like
  469. * '22.1 (term:0.96)' instead of (or in addition to) setting INSIDE_EMACS.
  470. * They may set TERM to 'eterm' instead of 'eterm-color'. They may have
  471. * a now-obsolete command that sets neither EMACS nor INSIDE_EMACS:
  472. * M-x terminal -> TERM='emacs-em7955' (line editing)
  473. */
  474. if (interactive_shell)
  475. {
  476. char *term, *emacs, *inside_emacs;;
  477. int emacs_term, in_emacs;
  478. term = get_string_value ("TERM");
  479. emacs = get_string_value ("EMACS");
  480. inside_emacs = get_string_value ("INSIDE_EMACS");
  481. if (inside_emacs)
  482. {
  483. emacs_term = strstr (inside_emacs, ",term:") != 0;
  484. in_emacs = 1;
  485. }
  486. else if (emacs)
  487. {
  488. /* Infer whether we are in an older Emacs. */
  489. emacs_term = strstr (emacs, " (term:") != 0;
  490. in_emacs = emacs_term || STREQ (emacs, "t");
  491. }
  492. else
  493. in_emacs = emacs_term = 0;
  494. /* Not sure any emacs terminal emulator sets TERM=emacs any more */
  495. no_line_editing |= STREQ (term, "emacs");
  496. no_line_editing |= in_emacs && STREQ (term, "dumb");
  497. /* running_under_emacs == 2 for `eterm' */
  498. running_under_emacs = in_emacs || STREQN (term, "emacs", 5);
  499. running_under_emacs += emacs_term && STREQN (term, "eterm", 5);
  500. if (running_under_emacs)
  501. gnu_error_format = 1;
  502. }
  503. top_level_arg_index = arg_index;
  504. old_errexit_flag = exit_immediately_on_error;
  505. /* Give this shell a place to longjmp to before executing the
  506. startup files. This allows users to press C-c to abort the
  507. lengthy startup. */
  508. code = setjmp_sigs (top_level);
  509. if (code)
  510. {
  511. if (code == EXITPROG || code == ERREXIT)
  512. exit_shell (last_command_exit_value);
  513. else
  514. {
  515. #if defined (JOB_CONTROL)
  516. /* Reset job control, since run_startup_files turned it off. */
  517. set_job_control (interactive_shell);
  518. #endif
  519. /* Reset value of `set -e', since it's turned off before running
  520. the startup files. */
  521. exit_immediately_on_error += old_errexit_flag;
  522. locally_skip_execution++;
  523. }
  524. }
  525. arg_index = top_level_arg_index;
  526. /* Execute the start-up scripts. */
  527. if (interactive_shell == 0)
  528. {
  529. unbind_variable ("PS1");
  530. unbind_variable ("PS2");
  531. interactive = 0;
  532. #if 0
  533. /* This has already been done by init_noninteractive */
  534. expand_aliases = posixly_correct;
  535. #endif
  536. }
  537. else
  538. {
  539. change_flag ('i', FLAG_ON);
  540. interactive = 1;
  541. }
  542. #if defined (RESTRICTED_SHELL)
  543. /* Set restricted_shell based on whether the basename of $0 indicates that
  544. the shell should be restricted or if the `-r' option was supplied at
  545. startup. */
  546. restricted_shell = shell_is_restricted (shell_name);
  547. /* If the `-r' option is supplied at invocation, make sure that the shell
  548. is not in restricted mode when running the startup files. */
  549. saverst = restricted;
  550. restricted = 0;
  551. #endif
  552. /* The startup files are run with `set -e' temporarily disabled. */
  553. if (locally_skip_execution == 0 && running_setuid == 0)
  554. {
  555. old_errexit_flag = exit_immediately_on_error;
  556. exit_immediately_on_error = 0;
  557. run_startup_files ();
  558. exit_immediately_on_error += old_errexit_flag;
  559. }
  560. /* If we are invoked as `sh', turn on Posix mode. */
  561. if (act_like_sh)
  562. {
  563. bind_variable ("POSIXLY_CORRECT", "y", 0);
  564. sv_strict_posix ("POSIXLY_CORRECT");
  565. }
  566. #if defined (RESTRICTED_SHELL)
  567. /* Turn on the restrictions after executing the startup files. This
  568. means that `bash -r' or `set -r' invoked from a startup file will
  569. turn on the restrictions after the startup files are executed. */
  570. restricted = saverst || restricted;
  571. if (shell_reinitialized == 0)
  572. maybe_make_restricted (shell_name);
  573. #endif /* RESTRICTED_SHELL */
  574. #if defined (WORDEXP_OPTION)
  575. if (wordexp_only)
  576. {
  577. startup_state = 3;
  578. last_command_exit_value = run_wordexp (argv[arg_index]);
  579. exit_shell (last_command_exit_value);
  580. }
  581. #endif
  582. cmd_init (); /* initialize the command object caches */
  583. uwp_init ();
  584. if (command_execution_string)
  585. {
  586. arg_index = bind_args (argv, arg_index, argc, 0);
  587. startup_state = 2;
  588. if (debugging_mode)
  589. start_debugger ();
  590. #if defined (ONESHOT)
  591. executing = 1;
  592. run_one_command (command_execution_string);
  593. exit_shell (last_command_exit_value);
  594. #else /* ONESHOT */
  595. with_input_from_string (command_execution_string, "-c");
  596. goto read_and_execute;
  597. #endif /* !ONESHOT */
  598. }
  599. /* Get possible input filename and set up default_buffered_input or
  600. default_input as appropriate. */
  601. if (arg_index != argc && read_from_stdin == 0)
  602. {
  603. open_shell_script (argv[arg_index]);
  604. arg_index++;
  605. }
  606. else if (interactive == 0)
  607. {
  608. /* In this mode, bash is reading a script from stdin, which is a
  609. pipe or redirected file. */
  610. #if defined (BUFFERED_INPUT)
  611. default_buffered_input = fileno (stdin); /* == 0 */
  612. #else
  613. setbuf (default_input, (char *)NULL);
  614. #endif /* !BUFFERED_INPUT */
  615. read_from_stdin = 1;
  616. }
  617. else if (arg_index == argc)
  618. /* "If there are no operands and the -c option is not specified, the -s
  619. option shall be assumed." */
  620. read_from_stdin = 1;
  621. set_bash_input ();
  622. /* Bind remaining args to $1 ... $n */
  623. arg_index = bind_args (argv, arg_index, argc, 1);
  624. if (debugging_mode && locally_skip_execution == 0 && running_setuid == 0 && (reading_shell_script || interactive_shell == 0))
  625. start_debugger ();
  626. /* Do the things that should be done only for interactive shells. */
  627. if (interactive_shell)
  628. {
  629. /* Set up for checking for presence of mail. */
  630. reset_mail_timer ();
  631. init_mail_dates ();
  632. #if defined (HISTORY)
  633. /* Initialize the interactive history stuff. */
  634. bash_initialize_history ();
  635. /* Don't load the history from the history file if we've already
  636. saved some lines in this session (e.g., by putting `history -s xx'
  637. into one of the startup files). */
  638. if (shell_initialized == 0 && history_lines_this_session == 0)
  639. load_history ();
  640. #endif /* HISTORY */
  641. /* Initialize terminal state for interactive shells after the
  642. .bash_profile and .bashrc are interpreted. */
  643. get_tty_state ();
  644. }
  645. #if !defined (ONESHOT)
  646. read_and_execute:
  647. #endif /* !ONESHOT */
  648. shell_initialized = 1;
  649. /* Read commands until exit condition. */
  650. reader_loop ();
  651. exit_shell (last_command_exit_value);
  652. }
  653. static int
  654. parse_long_options (argv, arg_start, arg_end)
  655. char **argv;
  656. int arg_start, arg_end;
  657. {
  658. int arg_index, longarg, i;
  659. char *arg_string;
  660. arg_index = arg_start;
  661. while ((arg_index != arg_end) && (arg_string = argv[arg_index]) &&
  662. (*arg_string == '-'))
  663. {
  664. longarg = 0;
  665. /* Make --login equivalent to -login. */
  666. if (arg_string[1] == '-' && arg_string[2])
  667. {
  668. longarg = 1;
  669. arg_string++;
  670. }
  671. for (i = 0; long_args[i].name; i++)
  672. {
  673. if (STREQ (arg_string + 1, long_args[i].name))
  674. {
  675. if (long_args[i].type == Int)
  676. *long_args[i].int_value = 1;
  677. else if (argv[++arg_index] == 0)
  678. {
  679. report_error (_("%s: option requires an argument"), long_args[i].name);
  680. exit (EX_BADUSAGE);
  681. }
  682. else
  683. *long_args[i].char_value = argv[arg_index];
  684. break;
  685. }
  686. }
  687. if (long_args[i].name == 0)
  688. {
  689. if (longarg)
  690. {
  691. report_error (_("%s: invalid option"), argv[arg_index]);
  692. show_shell_usage (stderr, 0);
  693. exit (EX_BADUSAGE);
  694. }
  695. break; /* No such argument. Maybe flag arg. */
  696. }
  697. arg_index++;
  698. }
  699. return (arg_index);
  700. }
  701. static int
  702. parse_shell_options (argv, arg_start, arg_end)
  703. char **argv;
  704. int arg_start, arg_end;
  705. {
  706. int arg_index;
  707. int arg_character, on_or_off, next_arg, i;
  708. char *o_option, *arg_string;
  709. arg_index = arg_start;
  710. while (arg_index != arg_end && (arg_string = argv[arg_index]) &&
  711. (*arg_string == '-' || *arg_string == '+'))
  712. {
  713. /* There are flag arguments, so parse them. */
  714. next_arg = arg_index + 1;
  715. /* A single `-' signals the end of options. From the 4.3 BSD sh.
  716. An option `--' means the same thing; this is the standard
  717. getopt(3) meaning. */
  718. if (arg_string[0] == '-' &&
  719. (arg_string[1] == '\0' ||
  720. (arg_string[1] == '-' && arg_string[2] == '\0')))
  721. return (next_arg);
  722. i = 1;
  723. on_or_off = arg_string[0];
  724. while (arg_character = arg_string[i++])
  725. {
  726. switch (arg_character)
  727. {
  728. case 'c':
  729. want_pending_command = 1;
  730. break;
  731. case 'l':
  732. make_login_shell = 1;
  733. break;
  734. case 's':
  735. read_from_stdin = 1;
  736. break;
  737. case 'o':
  738. o_option = argv[next_arg];
  739. if (o_option == 0)
  740. {
  741. list_minus_o_opts (-1, (on_or_off == '-') ? 0 : 1);
  742. break;
  743. }
  744. if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
  745. exit (EX_BADUSAGE);
  746. next_arg++;
  747. break;
  748. case 'O':
  749. /* Since some of these can be overridden by the normal
  750. interactive/non-interactive shell initialization or
  751. initializing posix mode, we save the options and process
  752. them after initialization. */
  753. o_option = argv[next_arg];
  754. if (o_option == 0)
  755. {
  756. shopt_listopt (o_option, (on_or_off == '-') ? 0 : 1);
  757. break;
  758. }
  759. add_shopt_to_alist (o_option, on_or_off);
  760. next_arg++;
  761. break;
  762. case 'D':
  763. dump_translatable_strings = 1;
  764. break;
  765. default:
  766. if (change_flag (arg_character, on_or_off) == FLAG_ERROR)
  767. {
  768. report_error (_("%c%c: invalid option"), on_or_off, arg_character);
  769. show_shell_usage (stderr, 0);
  770. exit (EX_BADUSAGE);
  771. }
  772. }
  773. }
  774. /* Can't do just a simple increment anymore -- what about
  775. "bash -abouo emacs ignoreeof -hP"? */
  776. arg_index = next_arg;
  777. }
  778. return (arg_index);
  779. }
  780. /* Exit the shell with status S. */
  781. void
  782. exit_shell (s)
  783. int s;
  784. {
  785. fflush (stdout); /* XXX */
  786. fflush (stderr);
  787. /* Clean up the terminal if we are in a state where it's been modified. */
  788. #if defined (READLINE)
  789. if (RL_ISSTATE (RL_STATE_TERMPREPPED) && rl_deprep_term_function)
  790. (*rl_deprep_term_function) ();
  791. #endif
  792. if (read_tty_modified ())
  793. read_tty_cleanup ();
  794. /* Do trap[0] if defined. Allow it to override the exit status
  795. passed to us. */
  796. if (signal_is_trapped (0))
  797. s = run_exit_trap ();
  798. #if defined (PROCESS_SUBSTITUTION)
  799. unlink_fifo_list ();
  800. #endif /* PROCESS_SUBSTITUTION */
  801. #if defined (HISTORY)
  802. if (remember_on_history)
  803. maybe_save_shell_history ();
  804. #endif /* HISTORY */
  805. #if defined (COPROCESS_SUPPORT)
  806. coproc_flush ();
  807. #endif
  808. #if defined (JOB_CONTROL)
  809. /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
  810. an interactive login shell. ksh does this unconditionally. */
  811. if (interactive_shell && login_shell && hup_on_exit)
  812. hangup_all_jobs ();
  813. /* If this shell is interactive, or job control is active, terminate all
  814. stopped jobs and restore the original terminal process group. Don't do
  815. this if we're in a subshell and calling exit_shell after, for example,
  816. a failed word expansion. We want to do this even if the shell is not
  817. interactive because we set the terminal's process group when job control
  818. is enabled regardless of the interactive status. */
  819. if (subshell_environment == 0)
  820. end_job_control ();
  821. #endif /* JOB_CONTROL */
  822. /* Always return the exit status of the last command to our parent. */
  823. sh_exit (s);
  824. }
  825. /* A wrapper for exit that (optionally) can do other things, like malloc
  826. statistics tracing. */
  827. void
  828. sh_exit (s)
  829. int s;
  830. {
  831. #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
  832. if (malloc_trace_at_exit)
  833. trace_malloc_stats (get_name_for_error (), (char *)NULL);
  834. /* mlocation_write_table (); */
  835. #endif
  836. exit (s);
  837. }
  838. /* Exit a subshell, which includes calling the exit trap. We don't want to
  839. do any more cleanup, since a subshell is created as an exact copy of its
  840. parent. */
  841. void
  842. subshell_exit (s)
  843. int s;
  844. {
  845. fflush (stdout);
  846. fflush (stderr);
  847. /* Do trap[0] if defined. Allow it to override the exit status
  848. passed to us. */
  849. if (signal_is_trapped (0))
  850. s = run_exit_trap ();
  851. sh_exit (s);
  852. }
  853. /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
  854. the Posix.2 startup file rules: $ENV is expanded, and if the file it
  855. names exists, that file is sourced. The Posix.2 rules are in effect
  856. for interactive shells only. (section 4.56.5.3) */
  857. /* Execute ~/.bashrc for most shells. Never execute it if
  858. ACT_LIKE_SH is set, or if NO_RC is set.
  859. If the executable file "/usr/gnu/src/bash/foo" contains:
  860. #!/usr/gnu/bin/bash
  861. echo hello
  862. then:
  863. COMMAND EXECUTE BASHRC
  864. --------------------------------
  865. bash -c foo NO
  866. bash foo NO
  867. foo NO
  868. rsh machine ls YES (for rsh, which calls `bash -c')
  869. rsh machine foo YES (for shell started by rsh) NO (for foo!)
  870. echo ls | bash NO
  871. login NO
  872. bash YES
  873. */
  874. static void
  875. execute_env_file (env_file)
  876. char *env_file;
  877. {
  878. char *fn;
  879. if (env_file && *env_file)
  880. {
  881. fn = expand_string_unsplit_to_string (env_file, Q_DOUBLE_QUOTES);
  882. if (fn && *fn)
  883. maybe_execute_file (fn, 1);
  884. FREE (fn);
  885. }
  886. }
  887. static void
  888. run_startup_files ()
  889. {
  890. #if defined (JOB_CONTROL)
  891. int old_job_control;
  892. #endif
  893. int sourced_login, run_by_ssh;
  894. /* get the rshd/sshd case out of the way first. */
  895. if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
  896. act_like_sh == 0 && command_execution_string)
  897. {
  898. #ifdef SSH_SOURCE_BASHRC
  899. run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) ||
  900. (find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0);
  901. #else
  902. run_by_ssh = 0;
  903. #endif
  904. /* If we were run by sshd or we think we were run by rshd, execute
  905. ~/.bashrc if we are a top-level shell. */
  906. if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2)
  907. {
  908. #ifdef SYS_BASHRC
  909. # if defined (__OPENNT)
  910. maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
  911. # else
  912. maybe_execute_file (SYS_BASHRC, 1);
  913. # endif
  914. #endif
  915. maybe_execute_file (bashrc_file, 1);
  916. return;
  917. }
  918. }
  919. #if defined (JOB_CONTROL)
  920. /* Startup files should be run without job control enabled. */
  921. old_job_control = interactive_shell ? set_job_control (0) : 0;
  922. #endif
  923. sourced_login = 0;
  924. /* A shell begun with the --login (or -l) flag that is not in posix mode
  925. runs the login shell startup files, no matter whether or not it is
  926. interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
  927. startup files if argv[0][0] == '-' as well. */
  928. #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
  929. if (login_shell && posixly_correct == 0)
  930. #else
  931. if (login_shell < 0 && posixly_correct == 0)
  932. #endif
  933. {
  934. /* We don't execute .bashrc for login shells. */
  935. no_rc++;
  936. /* Execute /etc/profile and one of the personal login shell
  937. initialization files. */
  938. if (no_profile == 0)
  939. {
  940. maybe_execute_file (SYS_PROFILE, 1);
  941. if (act_like_sh) /* sh */
  942. maybe_execute_file ("~/.profile", 1);
  943. else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
  944. (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
  945. maybe_execute_file ("~/.profile", 1);
  946. }
  947. sourced_login = 1;
  948. }
  949. /* A non-interactive shell not named `sh' and not in posix mode reads and
  950. executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
  951. and `-su' as the name of the shell, we want to read the startup files.
  952. No other non-interactive shells read any startup files. */
  953. if (interactive_shell == 0 && !(su_shell && login_shell))
  954. {
  955. if (posixly_correct == 0 && act_like_sh == 0 && privileged_mode == 0 &&
  956. sourced_env++ == 0)
  957. execute_env_file (get_string_value ("BASH_ENV"));
  958. return;
  959. }
  960. /* Interactive shell or `-su' shell. */
  961. if (posixly_correct == 0) /* bash, sh */
  962. {
  963. if (login_shell && sourced_login++ == 0)
  964. {
  965. /* We don't execute .bashrc for login shells. */
  966. no_rc++;
  967. /* Execute /etc/profile and one of the personal login shell
  968. initialization files. */
  969. if (no_profile == 0)
  970. {
  971. maybe_execute_file (SYS_PROFILE, 1);
  972. if (act_like_sh) /* sh */
  973. maybe_execute_file ("~/.profile", 1);
  974. else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
  975. (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
  976. maybe_execute_file ("~/.profile", 1);
  977. }
  978. }
  979. /* bash */
  980. if (act_like_sh == 0 && no_rc == 0)
  981. {
  982. #ifdef SYS_BASHRC
  983. # if defined (__OPENNT)
  984. maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
  985. # else
  986. maybe_execute_file (SYS_BASHRC, 1);
  987. # endif
  988. #endif
  989. maybe_execute_file (bashrc_file, 1);
  990. }
  991. /* sh */
  992. else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
  993. execute_env_file (get_string_value ("ENV"));
  994. }
  995. else /* bash --posix, sh --posix */
  996. {
  997. /* bash and sh */
  998. if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0)
  999. execute_env_file (get_string_value ("ENV"));
  1000. }
  1001. #if defined (JOB_CONTROL)
  1002. set_job_control (old_job_control);
  1003. #endif
  1004. }
  1005. #if defined (RESTRICTED_SHELL)
  1006. /* Return 1 if the shell should be a restricted one based on NAME or the
  1007. value of `restricted'. Don't actually do anything, just return a
  1008. boolean value. */
  1009. int
  1010. shell_is_restricted (name)
  1011. char *name;
  1012. {
  1013. char *temp;
  1014. if (restricted)
  1015. return 1;
  1016. temp = base_pathname (name);
  1017. if (*temp == '-')
  1018. temp++;
  1019. return (STREQ (temp, RESTRICTED_SHELL_NAME));
  1020. }
  1021. /* Perhaps make this shell a `restricted' one, based on NAME. If the
  1022. basename of NAME is "rbash", then this shell is restricted. The
  1023. name of the restricted shell is a configurable option, see config.h.
  1024. In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
  1025. and non-unsettable.
  1026. Do this also if `restricted' is already set to 1; maybe the shell was
  1027. started with -r. */
  1028. int
  1029. maybe_make_restricted (name)
  1030. char *name;
  1031. {
  1032. char *temp;
  1033. temp = base_pathname (name);
  1034. if (*temp == '-')
  1035. temp++;
  1036. if (restricted || (STREQ (temp, RESTRICTED_SHELL_NAME)))
  1037. {
  1038. set_var_read_only ("PATH");
  1039. set_var_read_only ("SHELL");
  1040. set_var_read_only ("ENV");
  1041. set_var_read_only ("BASH_ENV");
  1042. restricted = 1;
  1043. }
  1044. return (restricted);
  1045. }
  1046. #endif /* RESTRICTED_SHELL */
  1047. /* Fetch the current set of uids and gids and return 1 if we're running
  1048. setuid or setgid. */
  1049. static int
  1050. uidget ()
  1051. {
  1052. uid_t u;
  1053. u = getuid ();
  1054. if (current_user.uid != u)
  1055. {
  1056. FREE (current_user.user_name);
  1057. FREE (current_user.shell);
  1058. FREE (current_user.home_dir);
  1059. current_user.user_name = current_user.shell = current_user.home_dir = (char *)NULL;
  1060. }
  1061. current_user.uid = u;
  1062. current_user.gid = getgid ();
  1063. current_user.euid = geteuid ();
  1064. current_user.egid = getegid ();
  1065. /* See whether or not we are running setuid or setgid. */
  1066. return (current_user.uid != current_user.euid) ||
  1067. (current_user.gid != current_user.egid);
  1068. }
  1069. void
  1070. disable_priv_mode ()
  1071. {
  1072. int e;
  1073. if (setuid (current_user.uid) < 0)
  1074. {
  1075. e = errno;
  1076. sys_error (_("cannot set uid to %d: effective uid %d"), current_user.uid, current_user.euid);
  1077. #if defined (EXIT_ON_SETUID_FAILURE)
  1078. if (e == EAGAIN)
  1079. exit (e);
  1080. #endif
  1081. }
  1082. if (setgid (current_user.gid) < 0)
  1083. sys_error (_("cannot set gid to %d: effective gid %d"), current_user.gid, current_user.egid);
  1084. current_user.euid = current_user.uid;
  1085. current_user.egid = current_user.gid;
  1086. }
  1087. #if defined (WORDEXP_OPTION)
  1088. static int
  1089. run_wordexp (words)
  1090. char *words;
  1091. {
  1092. int code, nw, nb;
  1093. WORD_LIST *wl, *tl, *result;
  1094. code = setjmp_nosigs (top_level);
  1095. if (code != NOT_JUMPED)
  1096. {
  1097. switch (code)
  1098. {
  1099. /* Some kind of throw to top_level has occurred. */
  1100. case FORCE_EOF:
  1101. return last_command_exit_value = 127;
  1102. case ERREXIT:
  1103. case EXITPROG:
  1104. return last_command_exit_value;
  1105. case DISCARD:
  1106. return last_command_exit_value = 1;
  1107. default:
  1108. command_error ("run_wordexp", CMDERR_BADJUMP, code, 0);
  1109. }
  1110. }
  1111. /* Run it through the parser to get a list of words and expand them */
  1112. if (words && *words)
  1113. {
  1114. with_input_from_string (words, "--wordexp");
  1115. if (parse_command () != 0)
  1116. return (126);
  1117. if (global_command == 0)
  1118. {
  1119. printf ("0\n0\n");
  1120. return (0);
  1121. }
  1122. if (global_command->type != cm_simple)
  1123. return (126);
  1124. wl = global_command->value.Simple->words;
  1125. if (protected_mode)
  1126. for (tl = wl; tl; tl = tl->next)
  1127. tl->word->flags |= W_NOCOMSUB|W_NOPROCSUB;
  1128. result = wl ? expand_words_no_vars (wl) : (WORD_LIST *)0;
  1129. }
  1130. else
  1131. result = (WORD_LIST *)0;
  1132. last_command_exit_value = 0;
  1133. if (result == 0)
  1134. {
  1135. printf ("0\n0\n");
  1136. return (0);
  1137. }
  1138. /* Count up the number of words and bytes, and print them. Don't count
  1139. the trailing NUL byte. */
  1140. for (nw = nb = 0, wl = result; wl; wl = wl->next)
  1141. {
  1142. nw++;
  1143. nb += strlen (wl->word->word);
  1144. }
  1145. printf ("%u\n%u\n", nw, nb);
  1146. /* Print each word on a separate line. This will have to be changed when
  1147. the interface to glibc is completed. */
  1148. for (wl = result; wl; wl = wl->next)
  1149. printf ("%s\n", wl->word->word);
  1150. return (0);
  1151. }
  1152. #endif
  1153. #if defined (ONESHOT)
  1154. /* Run one command, given as the argument to the -c option. Tell
  1155. parse_and_execute not to fork for a simple command. */
  1156. static int
  1157. run_one_command (command)
  1158. char *command;
  1159. {
  1160. int code;
  1161. code = setjmp_nosigs (top_level);
  1162. if (code != NOT_JUMPED)
  1163. {
  1164. #if defined (PROCESS_SUBSTITUTION)
  1165. unlink_fifo_list ();
  1166. #endif /* PROCESS_SUBSTITUTION */
  1167. switch (code)
  1168. {
  1169. /* Some kind of throw to top_level has occurred. */
  1170. case FORCE_EOF:
  1171. return last_command_exit_value = 127;
  1172. case ERREXIT:
  1173. case EXITPROG:
  1174. return last_command_exit_value;
  1175. case DISCARD:
  1176. return last_command_exit_value = 1;
  1177. default:
  1178. command_error ("run_one_command", CMDERR_BADJUMP, code, 0);
  1179. }
  1180. }
  1181. return (parse_and_execute (savestring (command), "-c", SEVAL_NOHIST));
  1182. }
  1183. #endif /* ONESHOT */
  1184. static int
  1185. bind_args (argv, arg_start, arg_end, start_index)
  1186. char **argv;
  1187. int arg_start, arg_end, start_index;
  1188. {
  1189. register int i;
  1190. WORD_LIST *args;
  1191. for (i = arg_start, args = (WORD_LIST *)NULL; i < arg_end; i++)
  1192. args = make_word_list (make_word (argv[i]), args);
  1193. if (args)
  1194. {
  1195. args = REVERSE_LIST (args, WORD_LIST *);
  1196. if (start_index == 0) /* bind to $0...$n for sh -c command */
  1197. {
  1198. /* Posix.2 4.56.3 says that the first argument after sh -c command
  1199. becomes $0, and the rest of the arguments become $1...$n */
  1200. shell_name = savestring (args->word->word);
  1201. FREE (dollar_vars[0]);
  1202. dollar_vars[0] = savestring (args->word->word);
  1203. remember_args (args->next, 1);
  1204. push_args (args->next); /* BASH_ARGV and BASH_ARGC */
  1205. }
  1206. else /* bind to $1...$n for shell script */
  1207. {
  1208. remember_args (args, 1);
  1209. push_args (args); /* BASH_ARGV and BASH_ARGC */
  1210. }
  1211. dispose_words (args);
  1212. }
  1213. return (i);
  1214. }
  1215. void
  1216. unbind_args ()
  1217. {
  1218. remember_args ((WORD_LIST *)NULL, 1);
  1219. pop_args (); /* Reset BASH_ARGV and BASH_ARGC */
  1220. }
  1221. static void
  1222. start_debugger ()
  1223. {
  1224. #if defined (DEBUGGER) && defined (DEBUGGER_START_FILE)
  1225. int old_errexit;
  1226. int r;
  1227. old_errexit = exit_immediately_on_error;
  1228. exit_immediately_on_error = 0;
  1229. r = force_execute_file (DEBUGGER_START_FILE, 1);
  1230. if (r < 0)
  1231. {
  1232. internal_warning (_("cannot start debugger; debugging mode disabled"));
  1233. debugging_mode = 0;
  1234. }
  1235. error_trace_mode = function_trace_mode = debugging_mode;
  1236. set_shellopts ();
  1237. set_bashopts ();
  1238. exit_immediately_on_error += old_errexit;
  1239. #endif
  1240. }
  1241. static int
  1242. open_shell_script (script_name)
  1243. char *script_name;
  1244. {
  1245. int fd, e, fd_is_tty;
  1246. char *filename, *path_filename, *t;
  1247. char sample[80];
  1248. int sample_len;
  1249. struct stat sb;
  1250. #if defined (ARRAY_VARS)
  1251. SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
  1252. ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
  1253. #endif
  1254. filename = savestring (script_name);
  1255. fd = open (filename, O_RDONLY);
  1256. if ((fd < 0) && (errno == ENOENT) && (absolute_program (filename) == 0))
  1257. {
  1258. e = errno;
  1259. /* If it's not in the current directory, try looking through PATH
  1260. for it. */
  1261. path_filename = find_path_file (script_name);
  1262. if (path_filename)
  1263. {
  1264. free (filename);
  1265. filename = path_filename;
  1266. fd = open (filename, O_RDONLY);
  1267. }
  1268. else
  1269. errno = e;
  1270. }
  1271. if (fd < 0)
  1272. {
  1273. e = errno;
  1274. file_error (filename);
  1275. sh_exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
  1276. }
  1277. free (dollar_vars[0]);
  1278. dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (script_name);
  1279. if (exec_argv0)
  1280. {
  1281. free (exec_argv0);
  1282. exec_argv0 = (char *)NULL;
  1283. }
  1284. if (file_isdir (filename))
  1285. {
  1286. #if defined (EISDIR)
  1287. errno = EISDIR;
  1288. #else
  1289. errno = EINVAL;
  1290. #endif
  1291. file_error (filename);
  1292. sh_exit (EX_NOINPUT);
  1293. }
  1294. #if defined (ARRAY_VARS)
  1295. GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
  1296. GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
  1297. GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
  1298. array_push (bash_source_a, filename);
  1299. if (bash_lineno_a)
  1300. {
  1301. t = itos (executing_line_number ());
  1302. array_push (bash_lineno_a, t);
  1303. free (t);
  1304. }
  1305. array_push (funcname_a, "main");
  1306. #endif
  1307. #ifdef HAVE_DEV_FD
  1308. fd_is_tty = isatty (fd);
  1309. #else
  1310. fd_is_tty = 0;
  1311. #endif
  1312. /* Only do this with non-tty file descriptors we can seek on. */
  1313. if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
  1314. {
  1315. /* Check to see if the `file' in `bash file' is a binary file
  1316. according to the same tests done by execute_simple_command (),
  1317. and report an error and exit if it is. */
  1318. sample_len = read (fd, sample, sizeof (sample));
  1319. if (sample_len < 0)
  1320. {
  1321. e = errno;
  1322. if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
  1323. {
  1324. #if defined (EISDIR)
  1325. errno = EISDIR;
  1326. file_error (filename);
  1327. #else
  1328. internal_error (_("%s: Is a directory"), filename);
  1329. #endif
  1330. }
  1331. else
  1332. {
  1333. errno = e;
  1334. file_error (filename);
  1335. }
  1336. exit (EX_NOEXEC);
  1337. }
  1338. else if (sample_len > 0 && (check_binary_file (sample, sample_len)))
  1339. {
  1340. internal_error (_("%s: cannot execute binary file"), filename);
  1341. exit (EX_BINARY_FILE);
  1342. }
  1343. /* Now rewind the file back to the beginning. */
  1344. lseek (fd, 0L, 0);
  1345. }
  1346. /* Open the script. But try to move the file descriptor to a randomly
  1347. large one, in the hopes that any descriptors used by the script will
  1348. not match with ours. */
  1349. fd = move_to_high_fd (fd, 1, -1);
  1350. #if defined (BUFFERED_INPUT)
  1351. default_buffered_input = fd;
  1352. SET_CLOSE_ON_EXEC (default_buffered_input);
  1353. #else /* !BUFFERED_INPUT */
  1354. default_input = fdopen (fd, "r");
  1355. if (default_input == 0)
  1356. {
  1357. file_error (filename);
  1358. exit (EX_NOTFOUND);
  1359. }
  1360. SET_CLOSE_ON_EXEC (fd);
  1361. if (fileno (default_input) != fd)
  1362. SET_CLOSE_ON_EXEC (fileno (default_input));
  1363. #endif /* !BUFFERED_INPUT */
  1364. /* Just about the only way for this code to be executed is if something
  1365. like `bash -i /dev/stdin' is executed. */
  1366. if (interactive_shell && fd_is_tty)
  1367. {
  1368. dup2 (fd, 0);
  1369. close (fd);
  1370. fd = 0;
  1371. #if defined (BUFFERED_INPUT)
  1372. default_buffered_input = 0;
  1373. #else
  1374. fclose (default_input);
  1375. default_input = stdin;
  1376. #endif
  1377. }
  1378. else if (forced_interactive && fd_is_tty == 0)
  1379. /* But if a script is called with something like `bash -i scriptname',
  1380. we need to do a non-interactive setup here, since we didn't do it
  1381. before. */
  1382. init_interactive_script ();
  1383. free (filename);
  1384. reading_shell_script = 1;
  1385. return (fd);
  1386. }
  1387. /* Initialize the input routines for the parser. */
  1388. static void
  1389. set_bash_input ()
  1390. {
  1391. /* Make sure the fd from which we are reading input is not in
  1392. no-delay mode. */
  1393. #if defined (BUFFERED_INPUT)
  1394. if (interactive == 0)
  1395. sh_unset_nodelay_mode (default_buffered_input);
  1396. else
  1397. #endif /* !BUFFERED_INPUT */
  1398. sh_unset_nodelay_mode (fileno (stdin));
  1399. /* with_input_from_stdin really means `with_input_from_readline' */
  1400. if (interactive && no_line_editing == 0)
  1401. with_input_from_stdin ();
  1402. #if defined (BUFFERED_INPUT)
  1403. else if (interactive == 0)
  1404. with_input_from_buffered_stream (default_buffered_input, dollar_vars[0]);
  1405. #endif /* BUFFERED_INPUT */
  1406. else
  1407. with_input_from_stream (default_input, dollar_vars[0]);
  1408. }
  1409. /* Close the current shell script input source and forget about it. This is
  1410. extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
  1411. is non-zero, we close default_buffered_input even if it's the standard
  1412. input (fd 0). */
  1413. void
  1414. unset_bash_input (check_zero)
  1415. int check_zero;
  1416. {
  1417. #if defined (BUFFERED_INPUT)
  1418. if ((check_zero && default_buffered_input >= 0) ||
  1419. (check_zero == 0 && default_buffered_input > 0))
  1420. {
  1421. close_buffered_fd (default_buffered_input);
  1422. default_buffered_input = bash_input.location.buffered_fd = -1;
  1423. bash_input.type = st_none; /* XXX */
  1424. }
  1425. #else /* !BUFFERED_INPUT */
  1426. if (default_input)
  1427. {
  1428. fclose (default_input);
  1429. default_input = (FILE *)NULL;
  1430. }
  1431. #endif /* !BUFFERED_INPUT */
  1432. }
  1433. #if !defined (PROGRAM)
  1434. # define PROGRAM "bash"
  1435. #endif
  1436. static void
  1437. set_shell_name (argv0)
  1438. char *argv0;
  1439. {
  1440. /* Here's a hack. If the name of this shell is "sh", then don't do
  1441. any startup files; just try to be more like /bin/sh. */
  1442. shell_name = argv0 ? base_pathname (argv0) : PROGRAM;
  1443. if (argv0 && *argv0 == '-')
  1444. {
  1445. if (*shell_name == '-')
  1446. shell_name++;
  1447. login_shell = 1;
  1448. }
  1449. if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0')
  1450. act_like_sh++;
  1451. if (shell_name[0] == 's' && shell_name[1] == 'u' && shell_name[2] == '\0')
  1452. su_shell++;
  1453. shell_name = argv0 ? argv0 : PROGRAM;
  1454. FREE (dollar_vars[0]);
  1455. dollar_vars[0] = savestring (shell_name);
  1456. /* A program may start an interactive shell with
  1457. "execl ("/bin/bash", "-", NULL)".
  1458. If so, default the name of this shell to our name. */
  1459. if (!shell_name || !*shell_name || (shell_name[0] == '-' && !shell_name[1]))
  1460. shell_name = PROGRAM;
  1461. }
  1462. static void
  1463. init_interactive ()
  1464. {
  1465. expand_aliases = interactive_shell = startup_state = 1;
  1466. interactive = 1;
  1467. #if defined (HISTORY)
  1468. remember_on_history = enable_history_list = 1; /* XXX */
  1469. #endif
  1470. }
  1471. static void
  1472. init_noninteractive ()
  1473. {
  1474. #if defined (HISTORY)
  1475. bash_history_reinit (0);
  1476. #endif /* HISTORY */
  1477. interactive_shell = startup_state = interactive = 0;
  1478. expand_aliases = posixly_correct; /* XXX - was 0 not posixly_correct */
  1479. no_line_editing = 1;
  1480. #if defined (JOB_CONTROL)
  1481. /* Even if the shell is not interactive, enable job control if the -i or
  1482. -m option is supplied at startup. */
  1483. set_job_control (forced_interactive||jobs_m_flag);
  1484. #endif /* JOB_CONTROL */
  1485. }
  1486. static void
  1487. init_interactive_script ()
  1488. {
  1489. init_noninteractive ();
  1490. expand_aliases = interactive_shell = startup_state = 1;
  1491. #if defined (HISTORY)
  1492. remember_on_history = enable_history_list = 1; /* XXX */
  1493. #endif
  1494. }
  1495. void
  1496. get_current_user_info ()
  1497. {
  1498. struct passwd *entry;
  1499. /* Don't fetch this more than once. */
  1500. if (current_user.user_name == 0)
  1501. {
  1502. #if defined (__TANDEM)
  1503. entry = getpwnam (getlogin ());
  1504. #else
  1505. entry = getpwuid (current_user.uid);
  1506. #endif
  1507. if (entry)
  1508. {
  1509. current_user.user_name = savestring (entry->pw_name);
  1510. current_user.shell = (entry->pw_shell && entry->pw_shell[0])
  1511. ? savestring (entry->pw_shell)
  1512. : savestring ("/bin/sh");
  1513. current_user.home_dir = savestring (entry->pw_dir);
  1514. }
  1515. else
  1516. {
  1517. current_user.user_name = _("I have no name!");
  1518. current_user.user_name = savestring (current_user.user_name);
  1519. current_user.shell = savestring ("/bin/sh");
  1520. current_user.home_dir = savestring ("/");
  1521. }
  1522. #if defined (HAVE_GETPWENT)
  1523. endpwent ();
  1524. #endif
  1525. }
  1526. }
  1527. /* Do whatever is necessary to initialize the shell.
  1528. Put new initializations in here. */
  1529. static void
  1530. shell_initialize ()
  1531. {
  1532. char hostname[256];
  1533. int should_be_restricted;
  1534. /* Line buffer output for stderr and stdout. */
  1535. if (shell_initialized == 0)
  1536. {
  1537. sh_setlinebuf (stderr);
  1538. sh_setlinebuf (stdout);
  1539. }
  1540. /* Sort the array of shell builtins so that the binary search in
  1541. find_shell_builtin () works correctly. */
  1542. initialize_shell_builtins ();
  1543. /* Initialize the trap signal handlers before installing our own
  1544. signal handlers. traps.c:restore_original_signals () is responsible
  1545. for restoring the original default signal handlers. That function
  1546. is called when we make a new child. */
  1547. initialize_traps ();
  1548. initialize_signals (0);
  1549. /* It's highly unlikely that this will change. */
  1550. if (current_host_name == 0)
  1551. {
  1552. /* Initialize current_host_name. */
  1553. if (gethostname (hostname, 255) < 0)
  1554. current_host_name = "??host??";
  1555. else
  1556. current_host_name = savestring (hostname);
  1557. }
  1558. /* Initialize the stuff in current_user that comes from the password
  1559. file. We don't need to do this right away if the shell is not
  1560. interactive. */
  1561. if (interactive_shell)
  1562. get_current_user_info ();
  1563. /* Initialize our interface to the tilde expander. */
  1564. tilde_initialize ();
  1565. #if defined (RESTRICTED_SHELL)
  1566. should_be_restricted = shell_is_restricted (shell_name);
  1567. #endif
  1568. /* Initialize internal and environment variables. Don't import shell
  1569. functions from the environment if we are running in privileged or
  1570. restricted mode or if the shell is running setuid. */
  1571. #if defined (RESTRICTED_SHELL)
  1572. initialize_shell_variables (shell_environment, privileged_mode||restricted||should_be_restricted||running_setuid);
  1573. #else
  1574. initialize_shell_variables (shell_environment, privileged_mode||running_setuid);
  1575. #endif
  1576. /* Initialize the data structures for storing and running jobs. */
  1577. initialize_job_control (jobs_m_flag);
  1578. /* Initialize input streams to null. */
  1579. initialize_bash_input ();
  1580. initialize_flags ();
  1581. /* Initialize the shell options. Don't import the shell options
  1582. from the environment variables $SHELLOPTS or $BASHOPTS if we are
  1583. running in privileged or restricted mode or if the shell is running
  1584. setuid. */
  1585. #if defined (RESTRICTED_SHELL)
  1586. initialize_shell_options (privileged_mode||restricted||should_be_restricted||running_setuid);
  1587. initialize_bashopts (privileged_mode||restricted||should_be_restricted||running_setuid);
  1588. #else
  1589. initialize_shell_options (privileged_mode||running_setuid);
  1590. initialize_bashopts (privileged_mode||running_setuid);
  1591. #endif
  1592. }
  1593. /* Function called by main () when it appears that the shell has already
  1594. had some initialization performed. This is supposed to reset the world
  1595. back to a pristine state, as if we had been exec'ed. */
  1596. static void
  1597. shell_reinitialize ()
  1598. {
  1599. /* The default shell prompts. */
  1600. primary_prompt = PPROMPT;
  1601. secondary_prompt = SPROMPT;
  1602. /* Things that get 1. */
  1603. current_command_number = 1;
  1604. /* We have decided that the ~/.bashrc file should not be executed
  1605. for the invocation of each shell script. If the variable $ENV
  1606. (or $BASH_ENV) is set, its value is used as the name of a file
  1607. to source. */
  1608. no_rc = no_profile = 1;
  1609. /* Things that get 0. */
  1610. login_shell = make_login_shell = interactive = executing = 0;
  1611. debugging = do_version = line_number = last_command_exit_value = 0;
  1612. forced_interactive = interactive_shell = 0;
  1613. subshell_environment = running_in_background = 0;
  1614. expand_aliases = 0;
  1615. /* XXX - should we set jobs_m_flag to 0 here? */
  1616. #if defined (HISTORY)
  1617. bash_history_reinit (enable_history_list = 0);
  1618. #endif /* HISTORY */
  1619. #if defined (RESTRICTED_SHELL)
  1620. restricted = 0;
  1621. #endif /* RESTRICTED_SHELL */
  1622. /* Ensure that the default startup file is used. (Except that we don't
  1623. execute this file for reinitialized shells). */
  1624. bashrc_file = DEFAULT_BASHRC;
  1625. /* Delete all variables and functions. They will be reinitialized when
  1626. the environment is parsed. */
  1627. delete_all_contexts (shell_variables);
  1628. delete_all_variables (shell_functions);
  1629. reinit_special_variables ();
  1630. #if defined (READLINE)
  1631. bashline_reinitialize ();
  1632. #endif
  1633. shell_reinitialized = 1;
  1634. }
  1635. static void
  1636. show_shell_usage (fp, extra)
  1637. FILE *fp;
  1638. int extra;
  1639. {
  1640. int i;
  1641. char *set_opts, *s, *t;
  1642. if (extra)
  1643. fprintf (fp, _("GNU bash, version %s-(%s)\n"), shell_version_string (), MACHTYPE);
  1644. fprintf (fp, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"),
  1645. shell_name, shell_name);
  1646. fputs (_("GNU long options:\n"), fp);
  1647. for (i = 0; long_args[i].name; i++)
  1648. fprintf (fp, "\t--%s\n", long_args[i].name);
  1649. fputs (_("Shell options:\n"), fp);
  1650. fputs (_("\t-ilrsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp);
  1651. for (i = 0, set_opts = 0; shell_builtins[i].name; i++)
  1652. if (STREQ (shell_builtins[i].name, "set"))
  1653. set_opts = savestring (shell_builtins[i].short_doc);
  1654. if (set_opts)
  1655. {
  1656. s = strchr (set_opts, '[');
  1657. if (s == 0)
  1658. s = set_opts;
  1659. while (*++s == '-')
  1660. ;
  1661. t = strchr (s, ']');
  1662. if (t)
  1663. *t = '\0';
  1664. fprintf (fp, _("\t-%s or -o option\n"), s);
  1665. free (set_opts);
  1666. }
  1667. if (extra)
  1668. {
  1669. fprintf (fp, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name);
  1670. fprintf (fp, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name);
  1671. fprintf (fp, _("Use the `bashbug' command to report bugs.\n"));
  1672. fprintf (fp, "\n");
  1673. fprintf (fp, _("bash home page: <http://www.gnu.org/software/bash>\n"));
  1674. fprintf (fp, _("General help using GNU software: <http://www.gnu.org/gethelp/>\n"));
  1675. }
  1676. }
  1677. static void
  1678. add_shopt_to_alist (opt, on_or_off)
  1679. char *opt;
  1680. int on_or_off;
  1681. {
  1682. if (shopt_ind >= shopt_len)
  1683. {
  1684. shopt_len += 8;
  1685. shopt_alist = (STRING_INT_ALIST *)xrealloc (shopt_alist, shopt_len * sizeof (shopt_alist[0]));
  1686. }
  1687. shopt_alist[shopt_ind].word = opt;
  1688. shopt_alist[shopt_ind].token = on_or_off;
  1689. shopt_ind++;
  1690. }
  1691. static void
  1692. run_shopt_alist ()
  1693. {
  1694. register int i;
  1695. for (i = 0; i < shopt_ind; i++)
  1696. if (shopt_setopt (shopt_alist[i].word, (shopt_alist[i].token == '-')) != EXECUTION_SUCCESS)
  1697. exit (EX_BADUSAGE);
  1698. free (shopt_alist);
  1699. shopt_alist = 0;
  1700. shopt_ind = shopt_len = 0;
  1701. }