Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | #include <ucommon/ucommon.h> |
| 19 | |
| 20 | using namespace UCOMMON_NAMESPACE; |
| 21 | |
| 22 | static shell::flagopt helpflag('h',"--help", _TEXT("display this list")); |
| 23 | static shell::flagopt althelp('?', NULL, NULL); |
| 24 | static shell::stringopt error('e', "--error", _TEXT("stderr path to use"), "filename"); |
| 25 | static shell::stringopt input('i', "--input", _TEXT("stdin path to use"), "filename"); |
| 26 | static shell::stringopt output('o', "--output", _TEXT("stdout path to use"), "filename"); |
| 27 | |
| 28 | PROGRAM_MAIN(argc, argv) |
| 29 | { |
| 30 | fd_t stdio[3] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}; |
| 31 | const char *argv0; |
| 32 | |
| 33 | shell::bind("pdetach"); |
| 34 | shell args(argc, argv); |
| 35 | |
| 36 | if(is(helpflag) || is(althelp)) { |
| 37 | printf("%s\n", _TEXT("Usage: pdetach [stdio options] command [arguments...]")); |
| 38 | printf("%s\n\n", _TEXT("Create detached process")); |
| 39 | printf("%s\n", _TEXT("Options:")); |
| 40 | shell::help(); |
| 41 | printf("\n%s\n", _TEXT("Report bugs to dyfet@gnu.org")); |
| 42 | PROGRAM_EXIT(0); |
| 43 | } |
| 44 | |
| 45 | if(!args()) |
| 46 | shell::errexit(10, "*** pdetach %s", _TEXT("no command specified")); |
| 47 | |
| 48 | argv = args.argv(); |
| 49 | argv0 = *argv; |
| 50 | |
| 51 | if(is(input)) { |
| 52 | stdio[0] = fsys::input(*input); |
| 53 | if(stdio[0] == INVALID_HANDLE_VALUE) |
| 54 | shell::errexit(1, "*** pdetach: %s: %s", |
| 55 | _TEXT("cannot access"), *input); |
| 56 | } |
| 57 | |
| 58 | if(is(output)) { |
| 59 | stdio[1] = fsys::output(*output); |
| 60 | if(stdio[1] == INVALID_HANDLE_VALUE) |
| 61 | shell::errexit(2, "*** pdetach: %s: %s", |
| 62 | _TEXT("cannot access"), *output); |
| 63 | } |
| 64 | |
| 65 | if(is(error)) { |
| 66 | stdio[2] = fsys::output(*error); |
| 67 | if(stdio[2] == INVALID_HANDLE_VALUE) |
| 68 | shell::errexit(3, "*** pdetach: %s: %s", |
| 69 | _TEXT("cannot access"), *error); |
| 70 | } |
| 71 | |
| 72 | if(shell::detach(argv0, argv, NULL, stdio)) |
| 73 | shell::errexit(-1, "*** pdetach: %s: %s", |
| 74 | argv0, _TEXT("failed to execute")); |
| 75 | |
| 76 | PROGRAM_EXIT(0); |
| 77 | } |
| 78 | |