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