14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*8462SApril.Chin@Sun.COM *          Copyright (c) 1982-2008 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
7*8462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                  David Korn <dgk@research.att.com>                   *
184887Schin *                                                                      *
194887Schin ***********************************************************************/
204887Schin #pragma prototyped
214887Schin /*
224887Schin  * break [n]
234887Schin  * continue [n]
244887Schin  * return [n]
254887Schin  * exit [n]
264887Schin  *
274887Schin  *   David Korn
284887Schin  *   AT&T Labs
294887Schin  *   dgk@research.att.com
304887Schin  *
314887Schin  */
324887Schin 
334887Schin #include	"defs.h"
344887Schin #include	<ast.h>
354887Schin #include	<error.h>
364887Schin #include	<ctype.h>
374887Schin #include	"shnodes.h"
384887Schin #include	"builtins.h"
394887Schin 
404887Schin /*
414887Schin  * return and exit
424887Schin  */
434887Schin #if 0
444887Schin     /* for the dictionary generator */
454887Schin     int	b_exit(int n, register char *argv[],void *extra){}
464887Schin #endif
474887Schin int	b_return(register int n, register char *argv[],void *extra)
484887Schin {
494887Schin 	register char *arg;
50*8462SApril.Chin@Sun.COM 	register Shell_t *shp = ((Shbltin_t*)extra)->shp;
514887Schin 	struct checkpt *pp = (struct checkpt*)shp->jmplist;
524887Schin 	const char *options = (**argv=='r'?sh_optreturn:sh_optexit);
534887Schin 	while((n = optget(argv,options))) switch(n)
544887Schin 	{
554887Schin 	    case ':':
564887Schin 		if(!strmatch(argv[opt_info.index],"[+-]+([0-9])"))
574887Schin 			errormsg(SH_DICT,2, "%s", opt_info.arg);
584887Schin 		goto done;
594887Schin 	    case '?':
604887Schin 		errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
614887Schin 		return(2);
624887Schin 	}
634887Schin done:
644887Schin 	if(error_info.errors)
654887Schin 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
664887Schin 	pp->mode = (**argv=='e'?SH_JMPEXIT:SH_JMPFUN);
674887Schin 	argv += opt_info.index;
684887Schin 	n = (((arg= *argv)?(int)strtol(arg, (char**)0, 10):shp->oldexit)&SH_EXITMASK);
694887Schin 	/* return outside of function, dotscript and profile is exit */
704887Schin 	if(shp->fn_depth==0 && shp->dot_depth==0 && !sh_isstate(SH_PROFILE))
714887Schin 		pp->mode = SH_JMPEXIT;
724887Schin 	sh_exit(shp->savexit=n);
734887Schin 	return(1);
744887Schin }
754887Schin 
764887Schin 
774887Schin /*
784887Schin  * break and continue
794887Schin  */
804887Schin #if 0
814887Schin     /* for the dictionary generator */
824887Schin     int	b_continue(int n, register char *argv[],void *extra){}
834887Schin #endif
844887Schin int	b_break(register int n, register char *argv[],void *extra)
854887Schin {
864887Schin 	char *arg;
874887Schin 	register int cont= **argv=='c';
88*8462SApril.Chin@Sun.COM 	register Shell_t *shp = ((Shbltin_t*)extra)->shp;
894887Schin 	while((n = optget(argv,cont?sh_optcont:sh_optbreak))) switch(n)
904887Schin 	{
914887Schin 	    case ':':
924887Schin 		errormsg(SH_DICT,2, "%s", opt_info.arg);
934887Schin 		break;
944887Schin 	    case '?':
954887Schin 		errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
964887Schin 		return(2);
974887Schin 	}
984887Schin 	if(error_info.errors)
994887Schin 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
1004887Schin 	argv += opt_info.index;
1014887Schin 	n=1;
1024887Schin 	if(arg= *argv)
1034887Schin 	{
1044887Schin 		n = strtol(arg,&arg,10);
1054887Schin 		if(n<=0 || *arg)
1064887Schin 			errormsg(SH_DICT,ERROR_exit(1),e_nolabels,*argv);
1074887Schin 	}
1084887Schin 	if(shp->st.loopcnt)
1094887Schin 	{
1104887Schin 		shp->st.execbrk = shp->st.breakcnt = n;
1114887Schin 		if(shp->st.breakcnt > shp->st.loopcnt)
1124887Schin 			shp->st.breakcnt = shp->st.loopcnt;
1134887Schin 		if(cont)
1144887Schin 			shp->st.breakcnt = -shp->st.breakcnt;
1154887Schin 	}
1164887Schin 	return(0);
1174887Schin }
1184887Schin 
119