Dan Pasanen | c6e3786 | 2014-10-02 14:08:59 -0500 | [diff] [blame] | 1 | /* break.c, created from break.def. */ |
| 2 | #line 22 "./break.def" |
| 3 | |
| 4 | #line 34 "./break.def" |
| 5 | #include <config.h> |
| 6 | |
| 7 | #if defined (HAVE_UNISTD_H) |
| 8 | # ifdef _MINIX |
| 9 | # include <sys/types.h> |
| 10 | # endif |
| 11 | # include <unistd.h> |
| 12 | #endif |
| 13 | |
| 14 | #include "../bashintl.h" |
| 15 | |
| 16 | #include "../shell.h" |
| 17 | #include "common.h" |
| 18 | |
| 19 | extern char *this_command_name; |
| 20 | extern int posixly_correct; |
| 21 | |
| 22 | static int check_loop_level __P((void)); |
| 23 | |
| 24 | /* The depth of while's and until's. */ |
| 25 | int loop_level = 0; |
| 26 | |
| 27 | /* Non-zero when a "break" instruction is encountered. */ |
| 28 | int breaking = 0; |
| 29 | |
| 30 | /* Non-zero when we have encountered a continue instruction. */ |
| 31 | int continuing = 0; |
| 32 | |
| 33 | /* Set up to break x levels, where x defaults to 1, but can be specified |
| 34 | as the first argument. */ |
| 35 | int |
| 36 | break_builtin (list) |
| 37 | WORD_LIST *list; |
| 38 | { |
| 39 | intmax_t newbreak; |
| 40 | |
| 41 | if (check_loop_level () == 0) |
| 42 | return (EXECUTION_SUCCESS); |
| 43 | |
| 44 | (void)get_numeric_arg (list, 1, &newbreak); |
| 45 | |
| 46 | if (newbreak <= 0) |
| 47 | { |
| 48 | sh_erange (list->word->word, _("loop count")); |
| 49 | breaking = loop_level; |
| 50 | return (EXECUTION_FAILURE); |
| 51 | } |
| 52 | |
| 53 | if (newbreak > loop_level) |
| 54 | newbreak = loop_level; |
| 55 | |
| 56 | breaking = newbreak; |
| 57 | |
| 58 | return (EXECUTION_SUCCESS); |
| 59 | } |
| 60 | |
| 61 | #line 101 "./break.def" |
| 62 | |
| 63 | /* Set up to continue x levels, where x defaults to 1, but can be specified |
| 64 | as the first argument. */ |
| 65 | int |
| 66 | continue_builtin (list) |
| 67 | WORD_LIST *list; |
| 68 | { |
| 69 | intmax_t newcont; |
| 70 | |
| 71 | if (check_loop_level () == 0) |
| 72 | return (EXECUTION_SUCCESS); |
| 73 | |
| 74 | (void)get_numeric_arg (list, 1, &newcont); |
| 75 | |
| 76 | if (newcont <= 0) |
| 77 | { |
| 78 | sh_erange (list->word->word, _("loop count")); |
| 79 | breaking = loop_level; |
| 80 | return (EXECUTION_FAILURE); |
| 81 | } |
| 82 | |
| 83 | if (newcont > loop_level) |
| 84 | newcont = loop_level; |
| 85 | |
| 86 | continuing = newcont; |
| 87 | |
| 88 | return (EXECUTION_SUCCESS); |
| 89 | } |
| 90 | |
| 91 | /* Return non-zero if a break or continue command would be okay. |
| 92 | Print an error message if break or continue is meaningless here. */ |
| 93 | static int |
| 94 | check_loop_level () |
| 95 | { |
| 96 | #if defined (BREAK_COMPLAINS) |
| 97 | if (loop_level == 0 && posixly_correct == 0) |
| 98 | builtin_error (_("only meaningful in a `for', `while', or `until' loop")); |
| 99 | #endif /* BREAK_COMPLAINS */ |
| 100 | |
| 101 | return (loop_level); |
| 102 | } |