1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1982-2014 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 <dgkorn@gmail.com> *
18 * *
19 ***********************************************************************/
20 #include "config_ast.h" // IWYU pragma: keep
21
22 #include <stdlib.h>
23
24 #include "builtins.h"
25 #include "defs.h"
26 #include "error.h"
27 #include "optget_long.h"
28 #include "shcmd.h"
29
30 static const char *short_options = "";
31 static const struct optget_option long_options[] = {
32 {"help", optget_no_arg, NULL, 1}, // all builtins support --help
33 {NULL, 0, NULL, 0}};
34
35 //
36 // Builtin `continue`.
37 // See also the break.c module.
38 //
b_continue(int argc,char * argv[],Shbltin_t * context)39 int b_continue(int argc, char *argv[], Shbltin_t *context) {
40 char *arg;
41 int opt, n;
42 Shell_t *shp = context->shp;
43 char *cmd = argv[0];
44
45 optget_ind = 0;
46 while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
47 switch (opt) {
48 case 1: {
49 builtin_print_help(shp, cmd);
50 return 0;
51 }
52 case ':': {
53 builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
54 return 2;
55 }
56 case '?': {
57 builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
58 return 2;
59 }
60 default: { abort(); }
61 }
62 }
63
64 argv += optget_ind;
65 n = 1;
66 arg = *argv;
67
68 if (arg) {
69 n = (int)strtol(arg, &arg, 10);
70 if (n <= 0 || *arg) {
71 errormsg(SH_DICT, ERROR_exit(1), e_nolabels, *argv);
72 __builtin_unreachable();
73 }
74 }
75
76 if (shp->st.loopcnt) {
77 shp->st.execbrk = shp->st.breakcnt = n;
78 if (shp->st.breakcnt > shp->st.loopcnt) shp->st.breakcnt = shp->st.loopcnt;
79 shp->st.breakcnt = -shp->st.breakcnt;
80 }
81
82 return 0;
83 }
84