1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1982-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                  David Korn <dgk@research.att.com>                   *
18*4887Schin *                                                                      *
19*4887Schin ***********************************************************************/
20*4887Schin #pragma prototyped
21*4887Schin /*
22*4887Schin  * break [n]
23*4887Schin  * continue [n]
24*4887Schin  * return [n]
25*4887Schin  * exit [n]
26*4887Schin  *
27*4887Schin  *   David Korn
28*4887Schin  *   AT&T Labs
29*4887Schin  *   dgk@research.att.com
30*4887Schin  *
31*4887Schin  */
32*4887Schin 
33*4887Schin #include	"defs.h"
34*4887Schin #include	<ast.h>
35*4887Schin #include	<error.h>
36*4887Schin #include	<ctype.h>
37*4887Schin #include	"shnodes.h"
38*4887Schin #include	"builtins.h"
39*4887Schin 
40*4887Schin /*
41*4887Schin  * return and exit
42*4887Schin  */
43*4887Schin #if 0
44*4887Schin     /* for the dictionary generator */
45*4887Schin     int	b_exit(int n, register char *argv[],void *extra){}
46*4887Schin #endif
47*4887Schin int	b_return(register int n, register char *argv[],void *extra)
48*4887Schin {
49*4887Schin 	register char *arg;
50*4887Schin 	register Shell_t *shp = (Shell_t*)extra;
51*4887Schin 	struct checkpt *pp = (struct checkpt*)shp->jmplist;
52*4887Schin 	const char *options = (**argv=='r'?sh_optreturn:sh_optexit);
53*4887Schin 	while((n = optget(argv,options))) switch(n)
54*4887Schin 	{
55*4887Schin 	    case ':':
56*4887Schin 		if(!strmatch(argv[opt_info.index],"[+-]+([0-9])"))
57*4887Schin 			errormsg(SH_DICT,2, "%s", opt_info.arg);
58*4887Schin 		goto done;
59*4887Schin 	    case '?':
60*4887Schin 		errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
61*4887Schin 		return(2);
62*4887Schin 	}
63*4887Schin done:
64*4887Schin 	if(error_info.errors)
65*4887Schin 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
66*4887Schin 	pp->mode = (**argv=='e'?SH_JMPEXIT:SH_JMPFUN);
67*4887Schin 	argv += opt_info.index;
68*4887Schin 	n = (((arg= *argv)?(int)strtol(arg, (char**)0, 10):shp->oldexit)&SH_EXITMASK);
69*4887Schin 	/* return outside of function, dotscript and profile is exit */
70*4887Schin 	if(shp->fn_depth==0 && shp->dot_depth==0 && !sh_isstate(SH_PROFILE))
71*4887Schin 		pp->mode = SH_JMPEXIT;
72*4887Schin 	sh_exit(shp->savexit=n);
73*4887Schin 	return(1);
74*4887Schin }
75*4887Schin 
76*4887Schin 
77*4887Schin /*
78*4887Schin  * break and continue
79*4887Schin  */
80*4887Schin #if 0
81*4887Schin     /* for the dictionary generator */
82*4887Schin     int	b_continue(int n, register char *argv[],void *extra){}
83*4887Schin #endif
84*4887Schin int	b_break(register int n, register char *argv[],void *extra)
85*4887Schin {
86*4887Schin 	char *arg;
87*4887Schin 	register int cont= **argv=='c';
88*4887Schin 	register Shell_t *shp = (Shell_t*)extra;
89*4887Schin 	while((n = optget(argv,cont?sh_optcont:sh_optbreak))) switch(n)
90*4887Schin 	{
91*4887Schin 	    case ':':
92*4887Schin 		errormsg(SH_DICT,2, "%s", opt_info.arg);
93*4887Schin 		break;
94*4887Schin 	    case '?':
95*4887Schin 		errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
96*4887Schin 		return(2);
97*4887Schin 	}
98*4887Schin 	if(error_info.errors)
99*4887Schin 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
100*4887Schin 	argv += opt_info.index;
101*4887Schin 	n=1;
102*4887Schin 	if(arg= *argv)
103*4887Schin 	{
104*4887Schin 		n = strtol(arg,&arg,10);
105*4887Schin 		if(n<=0 || *arg)
106*4887Schin 			errormsg(SH_DICT,ERROR_exit(1),e_nolabels,*argv);
107*4887Schin 	}
108*4887Schin 	if(shp->st.loopcnt)
109*4887Schin 	{
110*4887Schin 		shp->st.execbrk = shp->st.breakcnt = n;
111*4887Schin 		if(shp->st.breakcnt > shp->st.loopcnt)
112*4887Schin 			shp->st.breakcnt = shp->st.loopcnt;
113*4887Schin 		if(cont)
114*4887Schin 			shp->st.breakcnt = -shp->st.breakcnt;
115*4887Schin 	}
116*4887Schin 	return(0);
117*4887Schin }
118*4887Schin 
119