Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is break.def, from which is created break.c. |
| 2 | It implements the builtins "break" and "continue" in Bash. |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES break.c |
| 22 | |
| 23 | $BUILTIN break |
| 24 | $FUNCTION break_builtin |
| 25 | $SHORT_DOC break [n] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Exit for, while, or until loops. |
| 27 | |
| 28 | Exit a FOR, WHILE or UNTIL loop. If N is specified, break N enclosing |
| 29 | loops. |
| 30 | |
| 31 | Exit Status: |
| 32 | The exit status is 0 unless N is not greater than or equal to 1. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 33 | $END |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 34 | #include <config.h> |
| 35 | |
| 36 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 37 | # ifdef _MINIX |
| 38 | # include <sys/types.h> |
| 39 | # endif |
| 40 | # include <unistd.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 41 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 43 | #include "../bashintl.h" |
| 44 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 45 | #include "../shell.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | #include "common.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 47 | |
| 48 | extern char *this_command_name; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 49 | extern int posixly_correct; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 51 | static int check_loop_level __P((void)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | |
| 53 | /* The depth of while's and until's. */ |
| 54 | int loop_level = 0; |
| 55 | |
| 56 | /* Non-zero when a "break" instruction is encountered. */ |
| 57 | int breaking = 0; |
| 58 | |
| 59 | /* Non-zero when we have encountered a continue instruction. */ |
| 60 | int continuing = 0; |
| 61 | |
| 62 | /* Set up to break x levels, where x defaults to 1, but can be specified |
| 63 | as the first argument. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 65 | break_builtin (list) |
| 66 | WORD_LIST *list; |
| 67 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 68 | intmax_t newbreak; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 69 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 70 | if (check_loop_level () == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 71 | return (EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 72 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 73 | (void)get_numeric_arg (list, 1, &newbreak); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 74 | |
| 75 | if (newbreak <= 0) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 76 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 77 | sh_erange (list->word->word, _("loop count")); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 78 | breaking = loop_level; |
| 79 | return (EXECUTION_FAILURE); |
| 80 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 81 | |
| 82 | if (newbreak > loop_level) |
| 83 | newbreak = loop_level; |
| 84 | |
| 85 | breaking = newbreak; |
| 86 | |
| 87 | return (EXECUTION_SUCCESS); |
| 88 | } |
| 89 | |
| 90 | $BUILTIN continue |
| 91 | $FUNCTION continue_builtin |
| 92 | $SHORT_DOC continue [n] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 93 | Resume for, while, or until loops. |
| 94 | |
| 95 | Resumes the next iteration of the enclosing FOR, WHILE or UNTIL loop. |
| 96 | If N is specified, resumes the Nth enclosing loop. |
| 97 | |
| 98 | Exit Status: |
| 99 | The exit status is 0 unless N is not greater than or equal to 1. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 100 | $END |
| 101 | |
| 102 | /* Set up to continue x levels, where x defaults to 1, but can be specified |
| 103 | as the first argument. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 104 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 105 | continue_builtin (list) |
| 106 | WORD_LIST *list; |
| 107 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 108 | intmax_t newcont; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 109 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 110 | if (check_loop_level () == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 111 | return (EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 112 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 113 | (void)get_numeric_arg (list, 1, &newcont); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | |
| 115 | if (newcont <= 0) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 116 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 117 | sh_erange (list->word->word, _("loop count")); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 118 | breaking = loop_level; |
| 119 | return (EXECUTION_FAILURE); |
| 120 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 121 | |
| 122 | if (newcont > loop_level) |
| 123 | newcont = loop_level; |
| 124 | |
| 125 | continuing = newcont; |
| 126 | |
| 127 | return (EXECUTION_SUCCESS); |
| 128 | } |
| 129 | |
| 130 | /* Return non-zero if a break or continue command would be okay. |
| 131 | Print an error message if break or continue is meaningless here. */ |
| 132 | static int |
| 133 | check_loop_level () |
| 134 | { |
| 135 | #if defined (BREAK_COMPLAINS) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 136 | if (loop_level == 0 && posixly_correct == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 137 | builtin_error (_("only meaningful in a `for', `while', or `until' loop")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 138 | #endif /* BREAK_COMPLAINS */ |
| 139 | |
| 140 | return (loop_level); |
| 141 | } |