1 /*
2  * libcomcom.d
3  * Copyright (C) 2019 Victor Porton <porton@narod.ru>
4  *
5  * libcomcom is free software: you can redistribute it and/or modify it
6  * 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  * libcomcom is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the 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 this program.  If not, see <http://www.gnu.org/licenses/>.";
17  */
18 
19 module libcomcom_wrapper;
20 
21 import std.string;
22 import std.algorithm.iteration : map;
23 import std.array : array;
24 import std.exception : ErrnoException;
25 import libcomcom;
26 
27 void libComComInitializer()
28 {
29     int res;
30     res = libcomcom_init_stratum();
31     if (res != 0) {
32         throw new ErrnoException("libcomcom_init_stratum()"); // TODO: Localization
33     }
34     libcomcom_set_default_terminate2();
35     if (res != 0) {
36         throw new ErrnoException("libcomcom_set_default_terminate()"); // TODO: Localization
37     }
38 }
39 
40 void libComComDestructor()
41 {
42     cast(void) libcomcom_reset_default_terminate2();
43     cast(void) libcomcom_destroy();
44 }
45 
46 string _runCommand(string file,
47                    const(char[][]) argv,
48                    const char** childEnvp,
49                    const char[] input,
50                    int timeout = -1)
51 {
52     const(char*) output;
53     size_t output_len;
54     const char*[] childArgv = map!toStringz(argv).array ~ null;
55     immutable int res = libcomcom_run_command(input.ptr, input.length,
56                                               &output, &output_len,
57                                               file.toStringz, childArgv.ptr,
58                                               childEnvp,
59                                               timeout);
60     if (res != 0) {
61         throw new ErrnoException("Run command"); // TODO: Localization
62     }
63     import core.stdc.stdlib : free;
64     scope(exit) free(cast(void*) output);
65     return output[0..output_len].idup;
66 }
67 
68 string runCommand(string file, const(char[][]) argv, const char[] input, int timeout = -1) {
69     return _runCommand(file, argv, null, input, timeout);
70 }
71 
72 string runCommandWithEnvironment(string file,
73                                  const(char[][]) argv,
74                                  const(char[][]) envp,
75                                  const char[] input,
76                                  int timeout = -1)
77 {
78     const char*[] childEnv = map!toStringz(envp).array ~ null;
79     return _runCommand(file, argv, childEnv.ptr, input, timeout);
80 }
81