1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:28:10 +00:00

Utilities: Make syscall(1) use Core::ArgsParser

Note that this should not change the behavior at all.
This commit is contained in:
Ben Wiederhake 2021-02-11 21:08:46 +01:00 committed by Andreas Kling
parent b5e5e43d4b
commit 244c81bcf2

View file

@ -24,71 +24,42 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <errno.h> #include <LibCore/ArgsParser.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <syscall.h> #include <syscall.h>
#include <unistd.h>
#if !defined __ENUMERATE_SYSCALL
# define __ENUMERATE_SYSCALL(x) SC_##x,
#endif
#define SC_NARG 4 #define SC_NARG 4
Syscall::Function syscall_table[] = {
ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
};
FlatPtr arg[SC_NARG]; FlatPtr arg[SC_NARG];
char buf[BUFSIZ]; char buf[BUFSIZ];
FlatPtr parse(char* s); static FlatPtr parse(const char* s);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int oflag; bool output_buffer = false;
int opt; bool list_syscalls = false;
while ((opt = getopt(argc, argv, "olh")) != -1) { Vector<const char*> arguments;
switch (opt) {
case 'o': Core::ArgsParser args_parser;
oflag = 1; args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
break; args_parser.add_option(list_syscalls, "List all existing syscalls", "list-syscalls", 'l');
case 'l': args_parser.add_positional_argument(arguments, "Syscall arguments (can be strings, 'buf' for the output buffer, or numbers like 1234 or 0xffffffff)", "syscall-arguments");
for (auto sc : syscall_table) { args_parser.parse(argc, argv);
fprintf(stdout, "%s ", Syscall::to_string(sc));
} for (size_t i = 0; i < arguments.size(); i++) {
return EXIT_SUCCESS; arg[i] = parse(arguments[i]);
case 'h':
fprintf(stderr, "usage: \tsyscall [-o] [-l] [-h] <syscall-name> <args...> [buf==BUFSIZ buffer]\n");
fprintf(stderr, "\tsyscall write 1 hello 5\n");
fprintf(stderr, "\tsyscall -o read 0 buf 5\n");
fprintf(stderr, "\tsyscall sleep 3\n");
break;
default:
exit(EXIT_FAILURE);
}
} }
if (optind >= argc) { for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
fprintf(stderr, "No entry specified\n"); if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
return -1;
}
for (int i = 0; i < argc - optind; i++) {
arg[i] = parse(argv[i + optind]);
}
for (auto sc : syscall_table) {
if (strcmp(Syscall::to_string(sc), (char*)arg[0]) == 0) {
int rc = syscall(sc, arg[1], arg[2], arg[3]); int rc = syscall(sc, arg[1], arg[2], arg[3]);
if (rc == -1) { if (rc == -1) {
perror("syscall"); perror("syscall");
} else { } else {
if (oflag) if (output_buffer)
fwrite(buf, 1, sizeof(buf), stdout); fwrite(buf, 1, sizeof(buf), stdout);
} }
@ -101,7 +72,7 @@ int main(int argc, char** argv)
return -1; return -1;
} }
FlatPtr parse(char* s) FlatPtr parse(const char* s)
{ {
char* t; char* t;
FlatPtr l; FlatPtr l;