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 <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 "jobs.h"
28 #include "optget_long.h"
29 #include "sfio.h"
30 #include "shcmd.h"
31 
32 #ifdef JOBS
33 static const char *short_options = "";
34 static const struct optget_option long_options[] = {
35     {"help", optget_no_arg, NULL, 1},  // all builtins support --help
36     {NULL, 0, NULL, 0}};
37 
38 //
39 // Builtin `disown` command.
40 //
b_disown(int argc,char * argv[],Shbltin_t * context)41 int b_disown(int argc, char *argv[], Shbltin_t *context) {
42     int opt;
43     Shell_t *shp = context->shp;
44     char *cmd = argv[0];
45 
46     optget_ind = 0;
47     while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
48         switch (opt) {
49             case 1: {
50                 builtin_print_help(shp, cmd);
51                 return 0;
52             }
53             case ':': {
54                 builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
55                 return 2;
56             }
57             case '?': {
58                 builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
59                 return 2;
60             }
61             default: { abort(); }
62         }
63     }
64 
65     argv += optget_ind;
66     if (!sh_isoption(shp, SH_MONITOR) || !job.jobcontrol) {
67         if (sh_isstate(shp, SH_INTERACTIVE)) {
68             errormsg(SH_DICT, ERROR_exit(1), e_no_jctl);
69             __builtin_unreachable();
70         }
71         return 1;
72     }
73     if (*argv == 0) argv = NULL;
74     if (job_walk(shp, sfstdout, job_switch, 'd', argv)) {
75         errormsg(SH_DICT, ERROR_exit(1), e_no_job);
76         __builtin_unreachable();
77     }
78     return shp->exitval;
79 }
80 #endif  // JOBS
81