Extended fs_shell by an interface for external scripting.
This is the first step towards getting rid of the UserlandFS dependency for building a HD image. Which is in turn the first step to being able to build one under Linux. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11541 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2005, Ingo Weinhold, [email protected].
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "external_commands.h"
|
||||||
|
|
||||||
|
#ifdef __BEOS__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <OS.h>
|
||||||
|
|
||||||
|
#include "fs_shell_command_beos.h"
|
||||||
|
|
||||||
|
static port_id sReplyPort = -1;
|
||||||
|
|
||||||
|
static port_id
|
||||||
|
get_command_port()
|
||||||
|
{
|
||||||
|
static port_id port = -1;
|
||||||
|
static bool initialized = false;
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
port = create_port(10, kFSShellCommandPort);
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *
|
||||||
|
get_external_command(const char *prompt, char *input, int len)
|
||||||
|
{
|
||||||
|
// get/create the port
|
||||||
|
port_id port = get_command_port();
|
||||||
|
if (port < 0) {
|
||||||
|
fprintf(stderr, "Failed to create command port: %s\n", strerror(port));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// read a message
|
||||||
|
external_command_message message;
|
||||||
|
ssize_t bytesRead;
|
||||||
|
do {
|
||||||
|
int32 code;
|
||||||
|
bytesRead = read_port(port, &code, &message, sizeof(message));
|
||||||
|
} while (bytesRead == B_INTERRUPTED);
|
||||||
|
|
||||||
|
if (bytesRead < 0) {
|
||||||
|
fprintf(stderr, "Reading from port failed: %s\n",
|
||||||
|
strerror(bytesRead));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the len of the command
|
||||||
|
int commandLen = (char*)&message + bytesRead - message.command;
|
||||||
|
if (commandLen <= 1) {
|
||||||
|
fprintf(stderr, "No command given.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (commandLen > len) {
|
||||||
|
fprintf(stderr, "Command too long. Ignored.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy the command
|
||||||
|
memcpy(input, message.command, commandLen);
|
||||||
|
input[len - 1] = '\0'; // always NULL-terminate
|
||||||
|
sReplyPort = message.reply_port;
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
reply_to_external_command(int result)
|
||||||
|
{
|
||||||
|
if (sReplyPort >= 0) {
|
||||||
|
// prepare the message
|
||||||
|
external_command_reply reply;
|
||||||
|
reply.error = result;
|
||||||
|
|
||||||
|
// send the reply
|
||||||
|
status_t error;
|
||||||
|
do {
|
||||||
|
error = write_port(sReplyPort, 0, &reply, sizeof(reply));
|
||||||
|
} while (error == B_INTERRUPTED);
|
||||||
|
sReplyPort = -1;
|
||||||
|
|
||||||
|
if (error != B_OK) {
|
||||||
|
fprintf(stderr, "Failed to send command result to reply port: %s\n",
|
||||||
|
strerror(error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // __BEOS__
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2005, Haiku Inc. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
#ifndef FS_SHELL_EXTERNAL_COMMANDS_H
|
||||||
|
#define FS_SHELL_EXTERNAL_COMMANDS_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
char *get_external_command(const char *prompt, char *input, int len);
|
||||||
|
void reply_to_external_command(int result);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // FS_SHELL_EXTERNAL_COMMANDS_H
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user