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