2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3 * Distributed under the terms of the MIT License.
11 #include "external_commands.h"
12 #include "fs_shell_command_beos.h"
15 static port_id sReplyPort
= -1;
21 static port_id port
= -1;
22 static bool initialized
= false;
25 port
= create_port(10, kFSShellCommandPort
);
34 FSShell::get_external_command(char *input
, int len
)
36 // get/create the port
37 port_id port
= get_command_port();
39 fprintf(stderr
, "Error: Failed to create command port: %s\n",
46 char _message
[sizeof(external_command_message
) + kMaxCommandLength
];
47 external_command_message
* message
= (external_command_message
*)_message
;
51 bytesRead
= read_port(port
, &code
, message
, sizeof(_message
));
52 } while (bytesRead
== B_INTERRUPTED
);
55 fprintf(stderr
, "Error: Reading from port failed: %s\n",
60 // get the len of the command
61 int commandLen
= _message
+ bytesRead
- message
->command
;
62 if (commandLen
<= 1) {
63 fprintf(stderr
, "Error: No command given.\n");
66 if (commandLen
> len
) {
67 fprintf(stderr
, "Error: Command too long. Ignored.\n");
72 memcpy(input
, message
->command
, commandLen
);
73 input
[len
- 1] = '\0'; // always NULL-terminate
74 sReplyPort
= message
->reply_port
;
81 FSShell::reply_to_external_command(int result
)
83 if (sReplyPort
>= 0) {
84 // prepare the message
85 external_command_reply reply
;
91 error
= write_port(sReplyPort
, 0, &reply
, sizeof(reply
));
92 } while (error
== B_INTERRUPTED
);
96 fprintf(stderr
, "Error: Failed to send command result to reply "
97 "port: %s\n", strerror(error
));
104 FSShell::external_command_cleanup()
106 // The port will be deleted automatically when the team exits.