PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/App/win-bash_0_6/v01/bash-1.14.2/flags.c

https://bitbucket.org/dabomb69/bash-portable
C | 270 lines | 132 code | 48 blank | 90 comment | 20 complexity | 5220e8a92d6eb256c37ad67666b767fe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, Unlicense, AGPL-1.0
  1. /* flags.c -- Everything about flags except the `set' command. That
  2. is in builtins.c */
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4. This file is part of GNU Bash, the Bourne Again SHell.
  5. Bash is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 1, or (at your option) any later
  8. version.
  9. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with Bash; see the file COPYING. If not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  16. /* Flags hacking. */
  17. #include "shell.h"
  18. #include "flags.h"
  19. /* **************************************************************** */
  20. /* */
  21. /* The Standard Sh Flags. */
  22. /* */
  23. /* **************************************************************** */
  24. /* Non-zero means automatically mark variables which are modified or created
  25. as auto export variables. */
  26. int mark_modified_vars = 0;
  27. /* Non-zero causes asynchronous job notification. Otherwise, job state
  28. notification only takes place just before a primary prompt is printed. */
  29. int asynchronous_notification = 0;
  30. /* Non-zero means exit immediately if a command exits with a non-zero
  31. exit status. */
  32. int exit_immediately_on_error = 0;
  33. /* Non-zero means disable filename globbing. */
  34. int disallow_filename_globbing = 0;
  35. /* Non-zero means to locate and remember function commands as functions are
  36. defined. Function commands are normally located when the function is
  37. executed. */
  38. int locate_commands_in_functions = 0;
  39. /* Non-zero means that all keyword arguments are placed into the environment
  40. for a command, not just those that appear on the line before the command
  41. name. */
  42. int place_keywords_in_env = 0;
  43. /* Non-zero means read commands, but don't execute tham. This is useful
  44. for debugging shell scripts that should do something hairy and possibly
  45. desctructive. */
  46. int read_but_dont_execute = 0;
  47. /* Non-zero means end of file is after one command. */
  48. int just_one_command = 0;
  49. /* Non-zero means don't overwrite existing files while doing redirections. */
  50. int noclobber = 0;
  51. /* Non-zero means trying to get the value of $i where $i is undefined
  52. causes an error, instead of a null substitution. */
  53. int unbound_vars_is_error = 0;
  54. /* Non-zero means type out input lines after you read them. */
  55. int echo_input_at_read = 0;
  56. /* Non-zero means type out the command definition after reading, but
  57. before executing. */
  58. int echo_command_at_execute = 0;
  59. /* Non-zero means turn on the job control features. */
  60. int jobs_m_flag = 0;
  61. /* Non-zero means this shell is interactive, even if running under a
  62. pipe. */
  63. int forced_interactive = 0;
  64. /* By default, follow the symbolic links as if they were real directories
  65. while hacking the `cd' command. This means that `cd ..' moves up in
  66. the string of symbolic links that make up the current directory, instead
  67. of the absolute directory. The shell variable `nolinks' also controls
  68. this flag. */
  69. int no_symbolic_links = 0;
  70. /* **************************************************************** */
  71. /* */
  72. /* Non-Standard Flags Follow Here. */
  73. /* */
  74. /* **************************************************************** */
  75. /* Non-zero means do lexical scoping in the body of a FOR command. */
  76. int lexical_scoping = 0;
  77. /* Non-zero means no such thing as invisible variables. */
  78. int no_invisible_vars = 0;
  79. /* Non-zero means don't look up or remember command names in a hash table, */
  80. int hashing_disabled = 0;
  81. #if defined (HISTORY)
  82. /* Non-zero means that we are doing history expansion. The default.
  83. This means !22 gets the 22nd line of history. */
  84. int history_expansion = 1;
  85. #endif /* HISTORY */
  86. /* Non-zero means that we allow comments to appear in interactive commands. */
  87. #if defined (INTERACTIVE_COMMENTS)
  88. int interactive_comments = 1;
  89. #else
  90. int interactive_comments = 0;
  91. #endif /* INTERACTIVE_COMMENTS */
  92. #if defined (RESTRICTED_SHELL)
  93. /* Non-zero means that this shell is `restricted'. A restricted shell
  94. disallows: changing directories, command or path names containing `/',
  95. unsetting or resetting the values of $PATH and $SHELL, and any type of
  96. output redirection. */
  97. int restricted = 0;
  98. #endif /* RESTRICTED_SHELL */
  99. /* Non-zero means that this shell is running in `privileged' mode. This
  100. mode is entered on startup if the real and effective uids or gids
  101. differ. */
  102. int privileged_mode = 0;
  103. /* **************************************************************** */
  104. /* */
  105. /* The Flags ALIST. */
  106. /* */
  107. /* **************************************************************** */
  108. struct flags_alist shell_flags[] = {
  109. /* Standard sh flags. */
  110. { 'a', &mark_modified_vars },
  111. #if defined (JOB_CONTROL)
  112. { 'b', &asynchronous_notification },
  113. #endif /* JOB_CONTROL */
  114. { 'e', &exit_immediately_on_error },
  115. { 'f', &disallow_filename_globbing },
  116. { 'h', &locate_commands_in_functions }, /* Oh, yeah, good mnemonic. */
  117. { 'i', &forced_interactive },
  118. { 'k', &place_keywords_in_env },
  119. #if defined (JOB_CONTROL)
  120. { 'm', &jobs_m_flag },
  121. #endif /* JOB_CONTROL */
  122. { 'n', &read_but_dont_execute },
  123. { 'p', &privileged_mode },
  124. #if defined (RESTRICTED_SHELL)
  125. { 'r', &restricted },
  126. #endif /* RESTRICTED_SHELL */
  127. { 't', &just_one_command },
  128. { 'u', &unbound_vars_is_error },
  129. { 'v', &echo_input_at_read },
  130. { 'x', &echo_command_at_execute },
  131. { 'C', &noclobber },
  132. /* New flags that control non-standard things. */
  133. { 'l', &lexical_scoping },
  134. { 'I', &no_invisible_vars },
  135. /* I want `h', but locate_commands_in_functions has it. Great. */
  136. { 'd', &hashing_disabled },
  137. { 'P', &no_symbolic_links },
  138. #if defined (HISTORY)
  139. /* Once again, we don't have the right mnemonic. */
  140. { 'H', &history_expansion },
  141. #endif /* HISTORY */
  142. {0, (int *)NULL}
  143. };
  144. #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
  145. int *
  146. find_flag (name)
  147. int name;
  148. {
  149. int i = 0;
  150. while (shell_flags[i].name)
  151. {
  152. if (shell_flags[i].name == name)
  153. return (shell_flags[i].value);
  154. i++;
  155. }
  156. return (FLAG_UNKNOWN);
  157. }
  158. /* Change the state of a flag, and return it's original value, or return
  159. FLAG_ERROR if there is no flag called NAME. ON_OR_OFF should be one
  160. of FLAG_ON or FLAG_OFF. */
  161. int
  162. change_flag (flag, on_or_off)
  163. int flag;
  164. int on_or_off;
  165. {
  166. int *value = find_flag (flag);
  167. int old_value;
  168. #if defined (RESTRICTED_SHELL)
  169. /* Don't allow "set +r" in a shell which is `restricted'. */
  170. if (restricted && flag == 'r' && on_or_off == FLAG_OFF)
  171. return (FLAG_ERROR);
  172. #endif /* RESTRICTED_SHELL */
  173. if (value == (int *)FLAG_UNKNOWN)
  174. return (FLAG_ERROR);
  175. else
  176. old_value = *value;
  177. if (on_or_off == FLAG_ON)
  178. *value = 1;
  179. else
  180. {
  181. if (on_or_off == FLAG_OFF)
  182. *value = 0;
  183. else
  184. return (FLAG_ERROR);
  185. }
  186. /* Special cases for a few flags. */
  187. switch (flag)
  188. {
  189. #if defined (JOB_CONTROL)
  190. case 'm':
  191. set_job_control (on_or_off == '-');
  192. break;
  193. #endif /* JOB_CONTROL */
  194. case 'p':
  195. if (on_or_off == '+')
  196. {
  197. setuid (current_user.uid);
  198. setgid (current_user.gid);
  199. current_user.euid = current_user.uid;
  200. current_user.egid = current_user.gid;
  201. }
  202. break;
  203. }
  204. return (old_value);
  205. }
  206. /* Return a string which is the names of all the currently
  207. set shell flags. */
  208. char *
  209. which_set_flags ()
  210. {
  211. char *temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS);
  212. int i, string_index = 0;
  213. for (i = 0; shell_flags[i].name; i++)
  214. if (*(shell_flags[i].value))
  215. temp[string_index++] = shell_flags[i].name;
  216. temp[string_index] = '\0';
  217. return (temp);
  218. }