1 /*
2  * libcomcom.d
3  * Copyright (C) 2019 Victor Porton <porton@narod.ru>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9 
10  * This program 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 General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://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 /// Initialize libcomcom in the usual way.
28 /// For advanced initialization use libcomcom_init_*() functions directly.
29 void libComComInitializer()
30 {
31     int res;
32     res = libcomcom_init_stratum();
33     if (res != 0) {
34         throw new ErrnoException("libcomcom_init_stratum()"); // TODO: Localization
35     }
36     libcomcom_set_default_terminate2();
37     if (res != 0) {
38         throw new ErrnoException("libcomcom_set_default_terminate()"); // TODO: Localization
39     }
40 }
41 
42 /// Deinitialize libcomcom in the usual way.
43 /// For advanced deinitialization use libcomcom_*() functions directly.
44 void libComComDestructor()
45 {
46     cast(void) libcomcom_reset_default_terminate2();
47     cast(void) libcomcom_destroy();
48 }
49 
50 string _runCommand(string file,
51                    const(char[][]) argv,
52                    const char** childEnvp,
53                    const char[] input,
54                    int timeout = -1)
55 {
56     const(char*) output;
57     size_t output_len;
58     const char*[] childArgv = map!toStringz(argv).array ~ null;
59     immutable int res = libcomcom_run_command(input.ptr, input.length,
60                                               &output, &output_len,
61                                               file.toStringz, childArgv.ptr,
62                                               childEnvp,
63                                               timeout);
64     if (res != 0) {
65         throw new ErrnoException("Run command"); // TODO: Localization
66     }
67     import core.stdc.stdlib : free;
68     scope(exit) free(cast(void*) output);
69     return output[0..output_len].idup;
70 }
71 
72 /// Run an OS command `file` with the given `input` and receive its output.
73 /// This functions passes the environment variables without changes.
74 string runCommand(string file, const(char[][]) argv, const char[] input, int timeout = -1) {
75     return _runCommand(file, argv, null, input, timeout);
76 }
77 
78 /// Run an OS command `file` with the given `input` and receive its output.
79 /// This functions allows to pass environment variables.
80 string runCommandWithEnvironment(string file,
81                                  const(char[][]) argv,
82                                  const(char[][]) envp,
83                                  const char[] input,
84                                  int timeout = -1)
85 {
86     const char*[] childEnv = map!toStringz(envp).array ~ null;
87     return _runCommand(file, argv, childEnv.ptr, input, timeout);
88 }
89