xref: /titanic_41/usr/src/lib/libshell/common/bltins/cflow.c (revision 7801e5e8b5bc4af34929c54a02cfb78398da08dd)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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[],void *extra){}
45 #endif
46 int	b_return(register int n, register char *argv[],void *extra)
47 {
48 	register char *arg;
49 	register Shell_t *shp = ((Shbltin_t*)extra)->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)&SH_EXITMASK:shp->oldexit));
68 	/* return outside of function, dotscript and profile is exit */
69 	if(shp->fn_depth==0 && shp->dot_depth==0 && !sh_isstate(SH_PROFILE))
70 		pp->mode = SH_JMPEXIT;
71 	sh_exit(shp->savexit=n);
72 	return(1);
73 }
74 
75 
76 /*
77  * break and continue
78  */
79 #if 0
80     /* for the dictionary generator */
81     int	b_continue(int n, register char *argv[],void *extra){}
82 #endif
83 int	b_break(register int n, register char *argv[],void *extra)
84 {
85 	char *arg;
86 	register int cont= **argv=='c';
87 	register Shell_t *shp = ((Shbltin_t*)extra)->shp;
88 	while((n = optget(argv,cont?sh_optcont:sh_optbreak))) switch(n)
89 	{
90 	    case ':':
91 		errormsg(SH_DICT,2, "%s", opt_info.arg);
92 		break;
93 	    case '?':
94 		errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
95 		return(2);
96 	}
97 	if(error_info.errors)
98 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
99 	argv += opt_info.index;
100 	n=1;
101 	if(arg= *argv)
102 	{
103 		n = strtol(arg,&arg,10);
104 		if(n<=0 || *arg)
105 			errormsg(SH_DICT,ERROR_exit(1),e_nolabels,*argv);
106 	}
107 	if(shp->st.loopcnt)
108 	{
109 		shp->st.execbrk = shp->st.breakcnt = n;
110 		if(shp->st.breakcnt > shp->st.loopcnt)
111 			shp->st.breakcnt = shp->st.loopcnt;
112 		if(cont)
113 			shp->st.breakcnt = -shp->st.breakcnt;
114 	}
115 	return(0);
116 }
117 
118