forked from textmate/dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm_dialog2.mm
167 lines (142 loc) · 3.97 KB
/
tm_dialog2.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// client.mm
// Created by Allan Odgaard on 2007-09-22.
//
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <map>
#include <vector>
#include <string.h>
#include <poll.h>
#include <stdlib.h>
#import <Foundation/Foundation.h>
#import "Dialog2.h"
static double const AppVersion = 2.0;
static size_t const AppRevision = APP_REVISION;
id connect ()
{
NSString* portName = DialogServerConnectionName;
if(char const* var = getenv("DIALOG_PORT_NAME"))
portName = [NSString stringWithUTF8String:var];
id proxy = [NSConnection rootProxyForConnectionWithRegisteredName:portName host:nil];
[proxy setProtocolForProxy:@protocol(DialogServerProtocol)];
return proxy;
}
char const* create_pipe (char const* name)
{
char* filename;
asprintf(&filename, "%s/dialog_fifo_%d_%s", getenv("TMPDIR") ?: "/tmp", getpid(), name);
int res = mkfifo(filename, 0666);
if((res == -1) && (errno != EEXIST))
{
perror("Error creating the named pipe");
exit(1);
}
return filename;
}
int open_pipe (char const* name, int oflag)
{
int fd = open(name, oflag);
if(fd == -1)
{
perror("Error opening the named pipe");
exit(1);
}
return fd;
}
int main (int argc, char const* argv[])
{
if(argc == 2 && strcmp(argv[1], "--version") == 0)
{
fprintf(stderr, "%1$s %2$.1f (" COMPILE_DATE " revision %3$zu)\n", getprogname(), AppVersion, AppRevision);
return 0;
}
// If the argument list starts with a switch then assume it’s meant for trunk dialog
// and pass it off
if(argc > 1 && *argv[1] == '-')
execv(getenv("DIALOG_1"), (char* const*)argv);
NSAutoreleasePool* pool = [NSAutoreleasePool new];
id<DialogServerProtocol> proxy = connect();
if(!proxy)
{
fprintf(stderr, "error reaching server\n");
exit(1);
}
char const* stdin_name = create_pipe("stdin");
char const* stdout_name = create_pipe("stdout");
char const* stderr_name = create_pipe("stderr");
NSMutableArray* args = [NSMutableArray array];
for(size_t i = 0; i < argc; ++i)
[args addObject:[NSString stringWithUTF8String:argv[i]]];
NSMutableDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithUTF8String:stdin_name], @"stdin",
[NSString stringWithUTF8String:stdout_name], @"stdout",
[NSString stringWithUTF8String:stderr_name], @"stderr",
[NSString stringWithUTF8String:getcwd(NULL, 0)], @"cwd",
[[NSProcessInfo processInfo] environment], @"environment",
args, @"arguments",
nil
];
[proxy connectFromClientWithOptions:dict];
int stdin_fd = open_pipe(stdin_name, O_WRONLY);
int stdout_fd = open_pipe(stdout_name, O_RDONLY);
int stderr_fd = open_pipe(stderr_name, O_RDONLY);
std::map<int, int> fd_map;
fd_map[STDIN_FILENO] = stdin_fd;
fd_map[stdout_fd] = STDOUT_FILENO;
fd_map[stderr_fd] = STDERR_FILENO;
if(isatty(STDIN_FILENO) != 0)
{
fd_map.erase(fd_map.find(STDIN_FILENO));
close(stdin_fd);
}
while(fd_map.size() > 1 || (fd_map.size() == 1 && fd_map.find(STDIN_FILENO) == fd_map.end()))
{
fd_set readfds, writefds;
FD_ZERO(&readfds); FD_ZERO(&writefds);
int num_fds = 0;
iterate(it, fd_map)
{
FD_SET(it->first, &readfds);
num_fds = std::max(num_fds, it->first + 1);
}
int i = select(num_fds, &readfds, &writefds, NULL, NULL);
if(i == -1)
{
perror("Error from select");
continue;
}
std::vector<std::map<int, int>::iterator> to_remove;
iterate(it, fd_map)
{
if(FD_ISSET(it->first, &readfds))
{
char buf[1024];
ssize_t len = read(it->first, buf, sizeof(buf));
if(len == 0)
to_remove.push_back(it); // we can’t remove as long as we need the iterator for the ++
else write(it->second, buf, len);
}
}
iterate(it, to_remove)
{
if((*it)->second == stdin_fd)
close((*it)->second);
fd_map.erase(*it);
}
}
close(stdout_fd);
close(stderr_fd);
unlink(stdin_name);
unlink(stdout_name);
unlink(stderr_name);
[pool release];
return 0;
}