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