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