]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
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, 1989, 1991 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 | |
bb70624e | 10 | Software Foundation; either version 2, or (at your option) any later |
726f6388 JA |
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 | |
bb70624e | 20 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
726f6388 JA |
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 | |
ccc6cda3 JA |
30 | #include <config.h> |
31 | ||
32 | #if defined (HAVE_UNISTD_H) | |
cce855bc JA |
33 | # ifdef _MINIX |
34 | # include <sys/types.h> | |
35 | # endif | |
36 | # include <unistd.h> | |
ccc6cda3 | 37 | #endif |
726f6388 JA |
38 | |
39 | #include "../shell.h" | |
ccc6cda3 | 40 | #include "common.h" |
726f6388 JA |
41 | |
42 | extern char *this_command_name; | |
43 | ||
44 | static int check_loop_level (); | |
45 | ||
46 | /* The depth of while's and until's. */ | |
47 | int loop_level = 0; | |
48 | ||
49 | /* Non-zero when a "break" instruction is encountered. */ | |
50 | int breaking = 0; | |
51 | ||
52 | /* Non-zero when we have encountered a continue instruction. */ | |
53 | int continuing = 0; | |
54 | ||
55 | /* Set up to break x levels, where x defaults to 1, but can be specified | |
56 | as the first argument. */ | |
ccc6cda3 | 57 | int |
726f6388 JA |
58 | break_builtin (list) |
59 | WORD_LIST *list; | |
60 | { | |
61 | int newbreak; | |
62 | ||
d166f048 | 63 | if (check_loop_level () == 0) |
726f6388 JA |
64 | return (EXECUTION_FAILURE); |
65 | ||
d166f048 | 66 | newbreak = get_numeric_arg (list, 1); |
726f6388 JA |
67 | |
68 | if (newbreak <= 0) | |
d166f048 JA |
69 | { |
70 | builtin_error ("loop count must be > 0"); | |
71 | breaking = loop_level; | |
72 | return (EXECUTION_FAILURE); | |
73 | } | |
726f6388 JA |
74 | |
75 | if (newbreak > loop_level) | |
76 | newbreak = loop_level; | |
77 | ||
78 | breaking = newbreak; | |
79 | ||
80 | return (EXECUTION_SUCCESS); | |
81 | } | |
82 | ||
83 | $BUILTIN continue | |
84 | $FUNCTION continue_builtin | |
85 | $SHORT_DOC continue [n] | |
86 | Resume the next iteration of the enclosing FOR, WHILE or UNTIL loop. | |
87 | If N is specified, resume at the N-th enclosing loop. | |
88 | $END | |
89 | ||
90 | /* Set up to continue x levels, where x defaults to 1, but can be specified | |
91 | as the first argument. */ | |
ccc6cda3 | 92 | int |
726f6388 JA |
93 | continue_builtin (list) |
94 | WORD_LIST *list; | |
95 | { | |
96 | int newcont; | |
97 | ||
d166f048 | 98 | if (check_loop_level () == 0) |
726f6388 JA |
99 | return (EXECUTION_FAILURE); |
100 | ||
d166f048 | 101 | newcont = get_numeric_arg (list, 1); |
726f6388 JA |
102 | |
103 | if (newcont <= 0) | |
d166f048 JA |
104 | { |
105 | builtin_error ("loop count must be > 0"); | |
106 | breaking = loop_level; | |
107 | return (EXECUTION_FAILURE); | |
108 | } | |
726f6388 JA |
109 | |
110 | if (newcont > loop_level) | |
111 | newcont = loop_level; | |
112 | ||
113 | continuing = newcont; | |
114 | ||
115 | return (EXECUTION_SUCCESS); | |
116 | } | |
117 | ||
118 | /* Return non-zero if a break or continue command would be okay. | |
119 | Print an error message if break or continue is meaningless here. */ | |
120 | static int | |
121 | check_loop_level () | |
122 | { | |
123 | #if defined (BREAK_COMPLAINS) | |
d166f048 | 124 | if (loop_level == 0) |
ccc6cda3 | 125 | builtin_error ("only meaningful in a `for', `while', or `until' loop"); |
726f6388 JA |
126 | #endif /* BREAK_COMPLAINS */ |
127 | ||
128 | return (loop_level); | |
129 | } |