Skip to content

Commit 37286ff

Browse files
committed
Add clang-format changes.
This is to be squashed to previous changes later on.
1 parent a173604 commit 37286ff

File tree

3 files changed

+231
-218
lines changed

3 files changed

+231
-218
lines changed

src/util/piped_process.cpp

Lines changed: 133 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -2,156 +2,162 @@
22
// Windows includes go here
33
#else
44
#include <cstring>
5-
#include <iostream>
6-
#include <unistd.h>
75
#include <errno.h>
86
#include <fcntl.h> // library for fcntl function
7+
#include <iostream>
8+
#include <unistd.h>
99
#endif
1010

1111
#include "invariant.h"
1212
#include "piped_process.h"
1313

14-
1514
piped_processt::piped_processt(const std::string &command)
1615
{
17-
#ifdef _WIN32
18-
// This should use the new error state from PR #6131 once that is done
19-
INVARIANT(false, "New SMT2 backend WIP: Windows piped_process constructor.");
20-
#else
21-
22-
if (pipe(pipe_input) == -1) {
23-
throw std::runtime_error("Input pipe creation failed");
24-
}
25-
26-
if (pipe(pipe_output) == -1) {
27-
throw std::runtime_error("Output pipe creation failed");
28-
}
29-
30-
// Default state
31-
process_state = process_statet::NOT_CREATED;
32-
33-
if (fcntl(pipe_output[0], F_SETFL, O_NONBLOCK) < 0) {
34-
throw std::runtime_error("Setting pipe non-blocking failed");
35-
}
16+
#ifdef _WIN32
17+
// This should use the new error state from PR #6131 once that is done
18+
INVARIANT(false, "New SMT2 backend WIP: Windows piped_process constructor.");
19+
#else
3620

37-
// Create a new process for the child that will execute the
38-
// command and receive information via pipes.
39-
pid_t pid = fork();
40-
if (pid == 0) {
41-
// child process here
42-
43-
// Close pipes that will be used by the parent so we do
44-
// not have our own copies and conflicts.
45-
close(pipe_input[1]);
46-
close(pipe_output[0]);
47-
48-
// Duplicate pipes so we have the ones we need.
49-
dup2(pipe_input[0], STDIN_FILENO);
50-
dup2(pipe_output[1], STDOUT_FILENO);
51-
dup2(pipe_output[1], STDERR_FILENO);
52-
53-
// Create the arguments to execvp from the construction string
54-
char **args = split_command_args(command);
55-
// Execute the command
56-
execvp(args[0], args);
57-
// Only reachable if execvp failed
58-
throw std::runtime_error("Launching \"" + command +
59-
"\" failed with error: " + strerror(errno));
60-
} else {
61-
// parent process here
62-
// Close pipes to be used by the child process
63-
close(pipe_input[0]);
64-
close(pipe_output[1]);
65-
66-
// Get stream for sending to the child process
67-
command_stream = fdopen(pipe_input[1], "w");
68-
process_state = process_statet::CREATED;
69-
}
70-
#endif
21+
if(pipe(pipe_input) == -1)
22+
{
23+
throw std::runtime_error("Input pipe creation failed");
24+
}
25+
26+
if(pipe(pipe_output) == -1)
27+
{
28+
throw std::runtime_error("Output pipe creation failed");
29+
}
30+
31+
// Default state
32+
process_state = process_statet::NOT_CREATED;
33+
34+
if(fcntl(pipe_output[0], F_SETFL, O_NONBLOCK) < 0)
35+
{
36+
throw std::runtime_error("Setting pipe non-blocking failed");
37+
}
38+
39+
// Create a new process for the child that will execute the
40+
// command and receive information via pipes.
41+
pid_t pid = fork();
42+
if(pid == 0)
43+
{
44+
// child process here
45+
46+
// Close pipes that will be used by the parent so we do
47+
// not have our own copies and conflicts.
48+
close(pipe_input[1]);
49+
close(pipe_output[0]);
50+
51+
// Duplicate pipes so we have the ones we need.
52+
dup2(pipe_input[0], STDIN_FILENO);
53+
dup2(pipe_output[1], STDOUT_FILENO);
54+
dup2(pipe_output[1], STDERR_FILENO);
55+
56+
// Create the arguments to execvp from the construction string
57+
char **args = split_command_args(command);
58+
// Execute the command
59+
execvp(args[0], args);
60+
// Only reachable if execvp failed
61+
throw std::runtime_error(
62+
"Launching \"" + command + "\" failed with error: " + strerror(errno));
63+
}
64+
else
65+
{
66+
// parent process here
67+
// Close pipes to be used by the child process
68+
close(pipe_input[0]);
69+
close(pipe_output[1]);
70+
71+
// Get stream for sending to the child process
72+
command_stream = fdopen(pipe_input[1], "w");
73+
process_state = process_statet::CREATED;
74+
}
75+
#endif
7176
}
7277

7378
bool piped_processt::send(const std::string &message)
7479
{
75-
#ifdef _WIN32
76-
// This should use the new error state from PR #6131 once that is done
77-
INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::send.");
78-
#else
79-
80-
if(process_state != process_statet::CREATED)
81-
{
82-
return false;
83-
}
84-
85-
// send message to solver process
86-
int send_status = fputs(message.c_str(), command_stream);
87-
fflush(command_stream);
88-
89-
if(send_status == EOF)
90-
{
91-
// Some kind of error occured, maybe we should update the
92-
// solver status here?
93-
return false;
94-
}
80+
#ifdef _WIN32
81+
// This should use the new error state from PR #6131 once that is done
82+
INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::send.");
83+
#else
84+
85+
if(process_state != process_statet::CREATED)
86+
{
87+
return false;
88+
}
89+
90+
// send message to solver process
91+
int send_status = fputs(message.c_str(), command_stream);
92+
fflush(command_stream);
9593

96-
return true;
97-
#endif
94+
if(send_status == EOF)
95+
{
96+
// Some kind of error occured, maybe we should update the
97+
// solver status here?
98+
return false;
99+
}
100+
101+
return true;
102+
#endif
98103
}
99104

100105
std::string piped_processt::receive()
101106
{
102-
#ifdef _WIN32
103-
// This should use the new error state from PR #6131 once that is done
104-
INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::receive.");
105-
#else
106-
107-
if(process_state != process_statet::CREATED)
108-
return NULL;
109-
110-
std::string response = std::string("");
111-
int nbytes;
112-
char buff[BUFSIZE];
113-
114-
while (true)
115-
{
116-
nbytes = read(pipe_output[0], buff, BUFSIZE);
117-
switch (nbytes) {
118-
case -1:
119-
// Nothing more to read in the pipe
120-
return response;
121-
case 0:
122-
// Pipe is closed.
123-
process_state = process_statet::STOPPED;
124-
if (response == std::string("")) {
125-
return NULL;
126-
}
127-
return response;
128-
default:
129-
// Read some bytes, append them to the response and continue
130-
response.append(buff, nbytes);
131-
}
132-
}
133-
134-
UNREACHABLE;
135-
#endif
136-
}
107+
#ifdef _WIN32
108+
// This should use the new error state from PR #6131 once that is done
109+
INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::receive.");
110+
#else
137111

112+
if(process_state != process_statet::CREATED)
113+
return NULL;
138114

139-
char ** piped_processt::split_command_args(const std::string &command)
140-
{
141-
char ** res = NULL;
142-
int n_spaces = 0;
143-
char *p = strtok(strdup(command.c_str()), " ");
115+
std::string response = std::string("");
116+
int nbytes;
117+
char buff[BUFSIZE];
144118

145-
while(p)
119+
while(true)
120+
{
121+
nbytes = read(pipe_output[0], buff, BUFSIZE);
122+
switch(nbytes)
146123
{
147-
res = (char **)realloc(res, sizeof (char*) * ++n_spaces);
148-
if (res == NULL)
149-
exit (-1); /* memory allocation failed */
150-
res[n_spaces-1] = p;
151-
p = strtok (NULL, " ");
124+
case -1:
125+
// Nothing more to read in the pipe
126+
return response;
127+
case 0:
128+
// Pipe is closed.
129+
process_state = process_statet::STOPPED;
130+
if(response == std::string(""))
131+
{
132+
return NULL;
133+
}
134+
return response;
135+
default:
136+
// Read some bytes, append them to the response and continue
137+
response.append(buff, nbytes);
152138
}
139+
}
153140

154-
res = (char **)realloc (res, sizeof (char*) * (n_spaces+1));
155-
res[n_spaces] = 0;
156-
return res;
141+
UNREACHABLE;
142+
#endif
143+
}
144+
145+
char **piped_processt::split_command_args(const std::string &command)
146+
{
147+
char **res = NULL;
148+
int n_spaces = 0;
149+
char *p = strtok(strdup(command.c_str()), " ");
150+
151+
while(p)
152+
{
153+
res = (char **)realloc(res, sizeof(char *) * ++n_spaces);
154+
if(res == NULL)
155+
exit(-1); /* memory allocation failed */
156+
res[n_spaces - 1] = p;
157+
p = strtok(NULL, " ");
158+
}
159+
160+
res = (char **)realloc(res, sizeof(char *) * (n_spaces + 1));
161+
res[n_spaces] = 0;
162+
return res;
157163
}

src/util/piped_process.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,34 @@
66
// by the pipe, to check/fix.
77
#define BUFSIZE 2048
88

9-
class piped_processt {
10-
enum class process_statet {
11-
NOT_CREATED,
12-
CREATED,
13-
STOPPED
14-
};
9+
class piped_processt
10+
{
11+
enum class process_statet
12+
{
13+
NOT_CREATED,
14+
CREATED,
15+
STOPPED
16+
};
1517

16-
char **split_command_args(const std::string &command);
18+
char **split_command_args(const std::string &command);
1719

1820
protected:
19-
FILE *response_stream;
20-
FILE *command_stream;
21-
int pipe_input[2];
22-
int pipe_output[2];
23-
process_statet process_state;
21+
FILE *response_stream;
22+
FILE *command_stream;
23+
int pipe_input[2];
24+
int pipe_output[2];
25+
process_statet process_state;
26+
2427
public:
25-
bool send(const std::string &message);
26-
std::string receive();
27-
28-
process_statet get_status()
29-
{
30-
return process_state;
31-
}
32-
33-
piped_processt(const std::string &command);
34-
// ~piped_processt();
28+
bool send(const std::string &message);
29+
std::string receive();
30+
31+
process_statet get_status()
32+
{
33+
return process_state;
34+
}
35+
36+
piped_processt(const std::string &command);
3537
};
3638

3739
#endif

0 commit comments

Comments
 (0)