xref: /haiku/src/apps/codycam/SpawningUploadClient.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 #include <errno.h>
2 #include <image.h>
3 #include <unistd.h>
4 #include <signal.h>
5 
6 #include <String.h>
7 
8 #include "SpawningUploadClient.h"
9 
10 SpawningUploadClient::SpawningUploadClient()
11 	: FileUploadClient()
12 	, fCommand()
13 	, fCommandPid(-1)
14 	, fPty(-1)
15 {
16 }
17 
18 
19 SpawningUploadClient::~SpawningUploadClient()
20 {
21 	close(fPty);
22 	kill(fCommandPid, SIGTERM);
23 	kill(fCommandPid, SIGKILL);
24 	status_t ret;
25 	wait_for_thread(fCommandPid, &ret);
26 }
27 
28 
29 bool
30 SpawningUploadClient::Connect(const string& server, const string& login, const string& passwd)
31 {
32 	bool rc = false;
33 //	fCommand += " ";
34 //	fCommand += server;
35 	rc = SpawnCommand();
36 	return rc;
37 }
38 
39 
40 bool
41 SpawningUploadClient::SpawnCommand()
42 {
43 	char ptypath[20];
44 	char ttypath[20];
45 	//XXX: should use a system-provided TerminalCommand class
46 	BString shellcmd = "exec";
47 	const char *args[] = { "/bin/sh", "-c", NULL, NULL };
48 	fPty = getpty(ptypath, ttypath);
49 	if (fPty < 0)
50 		return B_ERROR;
51 
52 	shellcmd << " 0<" << ttypath;
53 	shellcmd << " 1>" << ttypath;
54 	shellcmd += " 2>&1";
55 	shellcmd << " ; ";
56 	shellcmd << "export TTY=" << ttypath << "; "; // BeOS hack
57 	shellcmd << "export LC_ALL=C; export LANG=C; ";
58 	shellcmd << "exec ";
59 	shellcmd << fCommand.c_str();
60 printf("spawning: '%s'\n", shellcmd.String());
61 	args[2] = shellcmd.String();
62 	fCommandPid = load_image(3, args, (const char **)environ);
63 	if (fCommandPid < 0)
64 		return false;
65 	resume_thread(fCommandPid);
66 	return true;
67 }
68 
69 
70 status_t
71 SpawningUploadClient::SetCommandLine(const char *command)
72 {
73 	fCommand = command;
74 	return B_OK;
75 }
76 
77 
78 ssize_t
79 SpawningUploadClient::SendCommand(const char *cmd)
80 {
81 	return write(InputPipe(), cmd, strlen(cmd));
82 }
83 
84 
85 ssize_t
86 SpawningUploadClient::ReadReply(BString *to)
87 {
88 	char buff[1024];
89 	ssize_t len;
90 	to->Truncate(0);
91 	len = read(OutputPipe(), buff, 1024);
92 	if (len < 0)
93 		return errno;
94 	to->Append(buff, len);
95 	return len;
96 }
97 
98 
99 status_t
100 SpawningUploadClient::ParseReply()
101 {
102 	return B_ERROR;
103 }
104 
105 
106 int
107 SpawningUploadClient::getpty(char *pty, char *tty)
108 {
109 	static const char major[] = "pqrs";
110 	static const char minor[] = "0123456789abcdef";
111 	uint32 i, j;
112 	int32 fd = -1;
113 
114 	for (i = 0; i < sizeof(major); i++)
115 	{
116 		for (j = 0; j < sizeof(minor); j++)
117 		{
118 			sprintf(pty, "/dev/pt/%c%c", major[i], minor[j]);
119 			sprintf(tty, "/dev/tt/%c%c", major[i], minor[j]);
120 			fd = open(pty, O_RDWR|O_NOCTTY);
121 			if (fd >= 0)
122 			{
123 				return fd;
124 			}
125 		}
126 	}
127 
128 	return fd;
129 }
130 
131 
132 /* the rest is empty */
133 
134 
135 bool
136 SpawningUploadClient::ChangeDir(const string& dir)
137 {
138 	bool rc = false;
139 	return rc;
140 }
141 
142 
143 bool
144 SpawningUploadClient::ListDirContents(string& listing)
145 {
146 	bool rc = false;
147 	return rc;
148 }
149 
150 
151 bool
152 SpawningUploadClient::PrintWorkingDir(string& dir)
153 {
154 	bool rc = false;
155 	return rc;
156 }
157 
158 
159 bool
160 SpawningUploadClient::PutFile(const string& local, const string& remote, ftp_mode mode)
161 {
162 	bool rc = false;
163 	return rc;
164 }
165 
166 
167 bool
168 SpawningUploadClient::GetFile(const string& remote, const string& local, ftp_mode mode)
169 {
170 	bool rc = false;
171 	return rc;
172 }
173 
174 
175 // Note: this only works for local remote moves, cross filesystem moves
176 // will not work
177 bool
178 SpawningUploadClient::MoveFile(const string& oldPath, const string& newPath)
179 {
180 	bool rc = false;
181 	return rc;
182 }
183 
184 
185 bool
186 SpawningUploadClient::Chmod(const string& path, const string& mod)
187 {
188 	bool rc = false;
189 	return rc;
190 }
191 
192 
193 void
194 SpawningUploadClient::SetPassive(bool on)
195 {
196 }
197 
198 
199