Skip to content
Permalink
Browse files

Switch b_break() from optget() to getopt_long()

  • Loading branch information...
krader1961 committed Sep 11, 2019
1 parent 0101139 commit d49d8deab9152f80fadb782398455101bcb2d32a
@@ -19,45 +19,52 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdlib.h>

#include "builtins.h"
#include "defs.h"
#include "error.h"
#include "option.h"
#include "shcmd.h"

static const char *short_options = ":";
static const struct option long_options[] = {{"help", 0, NULL, 1}, // all builtins supports --help
{NULL, 0, NULL, 0}};

//
// Builtin `break`.
// See also the continue.c module.
// Builtin `break` command. See also the continue.c module.
//
int b_break(int n, char *argv[], Shbltin_t *context) {
char *arg;
int b_break(int argc, char *argv[], Shbltin_t *context) {
UNUSED(argc);
int opt;
Shell_t *shp = context->shp;
while ((n = optget(argv, sh_optbreak))) {
switch (n) { //!OCLINT(MissingDefaultStatement)
char *cmd = argv[0];

optind = 0;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case ':': {
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
builtin_missing_argument(shp, cmd, argv[optind - 1]);
return 2;
}
case '?': {
errormsg(SH_DICT, ERROR_usage(0), "%s", opt_info.arg);
builtin_unknown_option(shp, cmd, argv[optind - 1]);
return 2;
}
default: { abort(); }
}
}

if (error_info.errors) {
errormsg(SH_DICT, ERROR_usage(2), "%s", optusage(NULL));
__builtin_unreachable();
}

argv += opt_info.index;
n = 1;
arg = *argv;
argv += optind;

long n = 1;
char *arg = *argv;
if (arg) {
n = (int)strtol(arg, &arg, 10);
n = strtol(arg, &arg, 10);
if (n <= 0 || *arg) {
errormsg(SH_DICT, ERROR_exit(1), e_nolabels, *argv);
__builtin_unreachable();
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,54 @@
.\" Man page generated from reStructuredText.
.
.TH "BREAK" "1" "Sep 10, 2019" "" "Korn Shell"
.SH NAME
break \- break out of loop
.
.nr rst2man-indent-level 0
.
.de1 rstReportMargin
\\$1 \\n[an-margin]
level \\n[rst2man-indent-level]
level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
-
\\n[rst2man-indent0]
\\n[rst2man-indent1]
\\n[rst2man-indent2]
..
.de1 INDENT
.\" .rstReportMargin pre:
. RS \\$1
. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
. nr rst2man-indent-level +1
.\" .rstReportMargin post:
..
.de UNINDENT
. RE
.\" indent \\n[an-margin]
.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
.nr rst2man-indent-level -1
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.SH SYNOPSIS
.nf
break [n]
.fi
.sp
.SH DESCRIPTION
.sp
\fBfg\fP is a shell special built\-in that exits the smallest enclosing for, select, while, or until loop, or the n\-th enclosing loop if n is specified. Execution continues at the command following the loop(s).
.sp
If \fBn\fP is given it must be a positive integer >= 1. If n is larger than the number of enclosing loops the last enclosing loop will be exited.
.SH EXIT STATUS
.sp
0
.SH SEE ALSO
.sp
\fBcontinue\fP(1), \fBreturn\fP(1)
.SH AUTHOR
David J. Korn, et. al.
.SH COPYRIGHT
2019, David J. Korn, et.al.
.\" Generated by docutils manpage writer.
.

This file was deleted.

@@ -0,0 +1,22 @@
.. default-role:: code

:index:`break` -- break out of loop
===================================

Synopsis
--------
| break [n]

Description
-----------
`fg` is a shell special built-in that exits the smallest enclosing for, select, while, or until loop, or the n-th enclosing loop if n is specified. Execution continues at the command following the loop(s).

If `n` is given it must be a positive integer >= 1. If n is larger than the number of enclosing loops the last enclosing loop will be exited.

Exit Status
-----------
0

See Also
--------
`continue`\(1), `return`\(1)
@@ -62,6 +62,8 @@
'David J. Korn, et. al.', '1'),
('bg', 'bg', 'resume jobs in the background',
'David J. Korn, et. al.', '1'),
('break', 'break', 'break out of loop',
'David J. Korn, et. al.', '1'),
('fg', 'fg', 'move jobs to the foreground',
'David J. Korn, et. al.', '1'),
# External commands available as builtins.
@@ -13,6 +13,7 @@ Welcome to the Korn Shell

alias
bg
break
fg

.. toctree::

0 comments on commit d49d8de

Please sign in to comment.
You can’t perform that action at this time.