Lolly 1.4.28
Loading...
Searching...
No Matches
win_sys_utils.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : mingw_sys_utils.cpp
4 * DESCRIPTION: external command handling
5 * COPYRIGHT : (C) 2015 Gregoire LECERF, Denis RAUX
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "win_sys_utils.hpp"
13#include "analyze.hpp"
15#include "tm_timer.hpp"
16#include <windows.h>
17
18using lolly::data::wchar_to_utf8;
19
20SN
24
25#ifdef OS_MINGW
26#include "spawn.hpp"
27
28static void
30
31int
34 // Run command arg[0] with arguments arg[i], i >= 1.
35 // str_in[i] is sent to the file descriptor fd_in[i].
36 // str_out[i] is filled from the file descriptor fd_out[i].
37 // If str_in[i] is -1 then $$i automatically replaced by a valid
38 // file descriptor in arg.
39 if (N (arg) == 0) return 0;
41 int n_in= N (fd_in), n_out= N (fd_out);
42 ASSERT (N (str_in) == n_in, "size mismatch");
43 ASSERT (N (str_out) == n_out, "size mismatch");
45
46 for (int i= 0; i < n_in; i++) {
47 int fd= fd_in[i];
48 if (fd >= 0) ch[i].Init (fd, Channel::CHIN);
49 else ch[i].Init (Channel::CHIN);
50 }
51 for (int i= 0; i < n_out; i++) {
52 int fd= fd_out[i];
53 if (fd >= 0) ch[i + n_in].Init (fd, Channel::CHOUT);
54 else ch[i + n_in].Init (Channel::CHOUT);
55 }
56
58 for (int j= 0; j < N (arg); j++)
59 for (int i= 0; i < n_in; i++)
60 if (fd_in[i] < 0) {
61 arg_[j]= replace (arg_[j], "$%" * as_string (i),
62 as_string (_get_osfhandle (ch[i].getPipe ())));
63 arg_[j]= replace (arg_[j], "$$" * as_string (i),
64 as_string (ch[i].getPipe ()));
65 }
67 for (int j= 0; j < N (arg_); j++)
68 _arg << as_charp (arg_[j]);
69 _arg << (char*) NULL;
70
72 for (int j= 0; j < N (arg_); j++)
74 if (!process.isRunning ()) {
75 return -1;
76 }
77 process.getpid () << LF;
78
79 // receive data from spawn process
80 // class string is not thread safe, use std::string instead
82 for (int i= 0; i < n_out; i++)
83 ch[i + n_in].read (&str[i]);
84
85 // send data to spawn process
87 for (int i= 0; i < n_in; i++)
88 pos_in[i]= 0;
90
91 bool busy;
92 do {
93 busy= false;
94 if (texmacs_time () - last_wait_time > 5000) { // FIXME?
96 _unix_system_warn (process.getpid (), which, "waiting spawn process");
97 }
98 for (int i= 0; i < n_in; i++) {
99 if (N (str_in[i]) > pos_in[i]) {
100 int m= min (ch[i].sz, N (str_in[i]) - pos_in[i]); // do not fill the
101 // pipe
102 int o= ch[i].write (&(str_in[i][pos_in[i]]), m);
103 if (o >= 0) {
104 pos_in[i]+= o;
105 if (N (str_in[i]) == pos_in[i]) ch[i].close ();
106 else busy= true;
107 }
108 }
109 }
110 } while (busy);
111
112 // wait for process
113 int wret= process.wait ();
114 for (int i= 0; i < n_out; ++i)
115 (*(str_out[i])) << ::string (str[i].data (), str[i].length ());
116 return (wret);
117}
118#endif
119
120namespace lolly {
121
122#ifndef SECURITY_WIN32
123#define SECURITY_WIN32
124#endif
125
126#ifdef OS_WIN
127#include <windows.h>
128#endif
129#include <windef.h>
130
131#include <basetsd.h>
132#include <ntsecapi.h>
133#ifdef OS_WIN
134#include <security.h>
135#else
136#include <secext.h>
137#endif
138#include <wtypesbase.h>
139
140string
142 const int MAX_LEN= 100;
143 wchar_t buffer[MAX_LEN]; // it is in UTF-16
144 ULONG len;
145 GetUserNameExW (NameDisplay, buffer, &len);
146 if (len == 0) return string ("");
147 else {
148 return wchar_to_utf8 (buffer);
149 }
150}
151
152} // namespace lolly
bool read(string s, int &i, const char *test)
Definition analyze.cpp:589
string recompose(array< string > a, string sep)
Definition analyze.cpp:963
string replace(string s, string what, string by)
Definition analyze.cpp:899
T * A(array< T > a)
Get a pointer to the first element of the array.
Definition array.hpp:174
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
@ LF
Definition basic.hpp:287
@ CHIN
Definition spawn.hpp:24
@ CHOUT
Definition spawn.hpp:24
The list class represents a linked list.
Definition list.hpp:48
void tm_delete_array(C *Ptr)
SI min(SI i, SI j)
Returns the minimum of two signed integers.
Definition minmax.hpp:27
unsigned int SN
Definition minmax.hpp:11
string win_get_username()
char * as_charp(string s)
Definition string.cpp:256
string as_string(int16_t i)
Definition string.cpp:272
time_t texmacs_time()
Definition tm_timer.cpp:47
static void _unix_system_warn(pid_t pid, string which, string msg)
SN win_get_process_id()