1/*
2 * Copyright 2005-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "fs_shell_command_beos.h"
7
8#include <stdio.h>
9#include <string.h>
10
11#include <OS.h>
12
13#include "fs_shell_command.h"
14
15
16bool gUsesFifos = false;
17
18
19bool
20send_external_command(const char *command, int *result)
21{
22	int commandLen = strlen(command);
23	if (commandLen > kMaxCommandLength) {
24		fprintf(stderr, "Error: Command line too long.\n");
25		return false;
26	}
27
28	char _message[sizeof(external_command_message) + kMaxCommandLength];
29	external_command_message* message = (external_command_message*)_message;
30	strcpy(message->command, command);
31
32	// find the command port
33	port_id commandPort = find_port(kFSShellCommandPort);
34	if (commandPort < 0) {
35		fprintf(stderr, "Error: Couldn't find fs_shell command port.\n");
36		return false;
37	}
38
39	// create a reply port
40	port_id replyPort = create_port(1, "fs shell reply port");
41	if (replyPort < 0) {
42		fprintf(stderr, "Error: Failed to create a reply port: %s\n",
43			strerror(replyPort));
44		return false;
45	}
46	message->reply_port = replyPort;
47
48	// send the command message
49	status_t error;
50	do {
51		error = write_port(commandPort, 0, message,
52			sizeof(external_command_message) + commandLen);
53	} while (error == B_INTERRUPTED);
54
55	if (error != B_OK) {
56		fprintf(stderr, "Error: Failed to send command: %s\n", strerror(error));
57		return false;
58	}
59
60	// wait for the reply
61	external_command_reply reply;
62	ssize_t bytesRead;
63	do {
64		int32 code;
65		bytesRead = read_port(replyPort, &code, &reply, sizeof(reply));
66	} while (bytesRead == B_INTERRUPTED);
67
68	if (bytesRead < 0) {
69		fprintf(stderr, "Error: Failed to read reply from fs_shell: %s\n",
70			strerror(bytesRead));
71		return false;
72	}
73
74	*result = reply.error;
75	return true;
76}
77
78