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