Skip to content

Commit

Permalink
Create command for contest
Browse files Browse the repository at this point in the history
  • Loading branch information
ajami1331 committed Jan 5, 2025
1 parent 15eb7d5 commit 1becdce
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 37 deletions.
4 changes: 1 addition & 3 deletions include/announcement.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define ANNOUNCEMENT_H
#include <command.h>

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
4 changes: 1 addition & 3 deletions include/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions include/contest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef CONTEST_H
#define CONTEST_H
#include <command.h>

struct command *init_contest_command(void);

#endif // CONTEST_H
4 changes: 1 addition & 3 deletions include/login.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define LOGIN_H
#include <command.h>

int login_command_func(struct command *cur, int argc, char **argv);

struct command login_command;
struct command *init_login_command(void);

#endif // LOGIN_H
37 changes: 26 additions & 11 deletions src/announcement.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@
#include <client.h>
#include <stdbool.h>
#include <ketopt.h>
#include <stb_ds.h>

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}};
ketopt_t opt = KETOPT_INIT;
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)
{
Expand Down Expand Up @@ -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;
}
13 changes: 8 additions & 5 deletions src/command.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <stb_ds.h>
#include "command.h"
#include <command.h>
#ifndef NDEBUG
#include <stb_leakcheck.h>
#endif

void print_help(struct command *cmd)
{
Expand All @@ -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);
}
}

Expand All @@ -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);
Expand All @@ -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);
}
31 changes: 31 additions & 0 deletions src/contest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <contest.h>
#include <stdlib.h>
#include <stb_ds.h>
#include <stdio.h>
#include <client.h>

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;
}
22 changes: 14 additions & 8 deletions src/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
#include <stdlib.h>
#include <client.h>

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)
Expand Down Expand Up @@ -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;
}
9 changes: 5 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <command.h>
#include <login.h>
#include <announcement.h>
#include <contest.h>

#ifndef NDEBUG
#define STB_LEAKCHECK_IMPLEMENTATION
Expand All @@ -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)
{
Expand All @@ -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);
}

0 comments on commit 1becdce

Please sign in to comment.