From 1becdce94dae30bfcab8e29c3f06128b821c1fad Mon Sep 17 00:00:00 2001 From: Araf Al Jami Date: Sun, 5 Jan 2025 18:47:59 +0000 Subject: [PATCH] Create command for contest --- include/announcement.h | 4 +--- include/command.h | 4 +--- include/contest.h | 7 +++++++ include/login.h | 4 +--- src/announcement.c | 37 ++++++++++++++++++++++++++----------- src/command.c | 13 ++++++++----- src/contest.c | 31 +++++++++++++++++++++++++++++++ src/login.c | 22 ++++++++++++++-------- src/main.c | 9 +++++---- 9 files changed, 94 insertions(+), 37 deletions(-) create mode 100644 include/contest.h create mode 100644 src/contest.c diff --git a/include/announcement.h b/include/announcement.h index 36ff190..c30443d 100644 --- a/include/announcement.h +++ b/include/announcement.h @@ -2,8 +2,6 @@ #define ANNOUNCEMENT_H #include -int make_announcement_command_func(struct command *cur, int argc, char **argv); - -struct command make_announcement_command; +struct command* init_announcement_command(void); #endif // ANNOUNCEMENT_H \ No newline at end of file diff --git a/include/command.h b/include/command.h index 3af82ce..8504bcd 100644 --- a/include/command.h +++ b/include/command.h @@ -6,14 +6,12 @@ struct command const char *name; const char *desc; const char *help; - struct command *sub; + struct command **sub; int (*func)(struct command *cur, int argc, char **argv); }; void print_help(struct command *cmd); - int print_help_and_traverse(struct command *cur, int argc, char **argv); - void cleanup_commands(struct command *cmd); #endif // COMMAND_H \ No newline at end of file diff --git a/include/contest.h b/include/contest.h new file mode 100644 index 0000000..5732af0 --- /dev/null +++ b/include/contest.h @@ -0,0 +1,7 @@ +#ifndef CONTEST_H +#define CONTEST_H +#include + +struct command *init_contest_command(void); + +#endif // CONTEST_H \ No newline at end of file diff --git a/include/login.h b/include/login.h index 4312cce..176654c 100644 --- a/include/login.h +++ b/include/login.h @@ -2,8 +2,6 @@ #define LOGIN_H #include -int login_command_func(struct command *cur, int argc, char **argv); - -struct command login_command; +struct command *init_login_command(void); #endif // LOGIN_H \ No newline at end of file diff --git a/src/announcement.c b/src/announcement.c index 641c9a0..9175596 100644 --- a/src/announcement.c +++ b/src/announcement.c @@ -2,18 +2,11 @@ #include #include #include +#include -struct command make_announcement_command = { - .name = "make-announcement", - .desc = "make an announcement", - .help = "make-announcement --message [message]", - .sub = NULL, - .func = make_announcement_command_func, -}; - -int make_announcement_command_func(struct command *cur, int argc, char **argv) +int announcement_create_command_func(struct command *cur, int argc, char **argv) { - static ko_longopt_t make_announcement_longopts[] = { + static ko_longopt_t announcement_create_longopts[] = { {"message", 1, 0}, {"help", 0, 0}, {0, 0, 0}}; @@ -21,7 +14,7 @@ int make_announcement_command_func(struct command *cur, int argc, char **argv) int c; bool help = false; char *message = NULL; - while ((c = ketopt(&opt, argc + 1, argv - 1, 1, "m:h", make_announcement_longopts)) != -1) + while ((c = ketopt(&opt, argc + 1, argv - 1, 1, "m:h", announcement_create_longopts)) != -1) { switch (c) { @@ -73,4 +66,26 @@ int make_announcement_command_func(struct command *cur, int argc, char **argv) printf("make announcement success\n"); } return 0; +} + +struct command announcement_command = { + .name = "announcement", + .desc = "manage announcements", + .help = "\nUsage: socli announcement [command] [options]\n\nCommands:\n", + .sub = NULL, + .func = print_help_and_traverse, +}; + +struct command announcement_create_command = { + .name = "create", + .desc = "create an announcement", + .help = "create --message", + .sub = NULL, + .func = announcement_create_command_func, +}; + +struct command* init_announcement_command(void) +{ + arrpush(announcement_command.sub, &announcement_create_command); + return &announcement_command; } \ No newline at end of file diff --git a/src/command.c b/src/command.c index d8dac3d..c443bd3 100644 --- a/src/command.c +++ b/src/command.c @@ -1,7 +1,10 @@ #include #include #include -#include "command.h" +#include +#ifndef NDEBUG +#include +#endif void print_help(struct command *cmd) { @@ -10,7 +13,7 @@ void print_help(struct command *cmd) printf("%s\n", cmd->help); for (int i = 0; i < arrlen(cmd->sub); ++i) { - printf(" %s\n", cmd->sub[i].name); + printf(" %s\n", cmd->sub[i]->name); } } @@ -23,9 +26,9 @@ int print_help_and_traverse(struct command *cur, int argc, char **argv) } for (int i = 0; i < arrlen(cur->sub); ++i) { - if (strcmp(argv[0], cur->sub[i].name) == 0) + if (strcmp(argv[0], cur->sub[i]->name) == 0) { - return cur->sub[i].func(&cur->sub[i], argc - 1, argv + 1); + return cur->sub[i]->func(cur->sub[i], argc - 1, argv + 1); } } print_help(cur); @@ -36,7 +39,7 @@ void cleanup_commands(struct command *cmd) { for (int i = 0; i < arrlen(cmd->sub); ++i) { - cleanup_commands(&cmd->sub[i]); + cleanup_commands(cmd->sub[i]); } arrfree(cmd->sub); } \ No newline at end of file diff --git a/src/contest.c b/src/contest.c new file mode 100644 index 0000000..29d0980 --- /dev/null +++ b/src/contest.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +int contest_list_command_func(struct command *cur, int argc, char **argv) +{ + printf("list\n"); + return 0; +} + +struct command contest_list_command = { + .name = "list", + .desc = "list all contests", + .help = "\nUsage: socli contest list [--page]\n\n", + .sub = NULL, + .func = print_help_and_traverse}; + +struct command contest_command = { + .name = "contest", + .desc = "manage contests", + .help = "\nUsage: socli contest [command] [options]\n\nCommands:\n", + .sub = NULL, + .func = print_help_and_traverse}; + +struct command *init_contest_command(void) +{ + arrpush(contest_command.sub, &contest_list_command); + return &contest_command; +} diff --git a/src/login.c b/src/login.c index fd1370b..1bc13a8 100644 --- a/src/login.c +++ b/src/login.c @@ -5,14 +5,6 @@ #include #include -struct command login_command = { - .name = "login", - .desc = "login to SeriousOJ", - .help = "login --username [username] --password [password]", - .sub = NULL, - .func = login_command_func, -}; - char *username = NULL, *password = NULL; void free_login_memory(void) @@ -93,4 +85,18 @@ int login_command_func(struct command *cur, int argc, char **argv) save_cookies(); } return 0; +} + +struct command login_command = { + .name = "login", + .desc = "login to SeriousOJ", + .help = "login --username --password", + .sub = NULL, + .func = login_command_func, +}; + + +struct command* init_login_command(void) +{ + return &login_command; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 85f3676..b0d643d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #ifndef NDEBUG #define STB_LEAKCHECK_IMPLEMENTATION @@ -21,7 +22,7 @@ struct command root_command = { .desc = "socli is a command line tool for managing SeriousOJ.", .help = "\nUsage: socli [command] [options]\n\nCommands:\n", .sub = NULL, - .func = NULL}; + .func = print_help_and_traverse}; void cleanup_memory(void) { @@ -35,9 +36,9 @@ int main(int argc, char **argv) init_curl(); - arrpush(root_command.sub, login_command); - arrpush(root_command.sub, make_announcement_command); + arrpush(root_command.sub, init_login_command()); + arrpush(root_command.sub, init_announcement_command()); + arrpush(root_command.sub, init_contest_command()); - root_command.func = print_help_and_traverse; return root_command.func(&root_command, argc - 1, argv + 1); }