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;
case PEER_AUTH:
/* 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_DEBUG("Accepted authenticated panel %p\n", peer);

View File

@ -55,30 +55,6 @@
name##_COUNT += numElem; \
} 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);
#endif

View File

@ -2,6 +2,7 @@
#define SHELLCLIENT_H
#include "hashmap.h"
#include "lmem.h"
#include "lpeer.h"
#include "lsodium.h"
#include "ltask.h"
@ -15,9 +16,7 @@ typedef struct sShell_client
struct sLaika_peer *peer;
tShell_peer *openShell; /* if not NULL, shell is open on peer */
struct hashmap *peers;
tShell_peer **peerTbl;
int peerTblCount;
int peerTblCap;
laikaM_newVector(tShell_peer *, peerTbl);
} tShell_client;
#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,
shell_ElemCompare, NULL, NULL);
client->openShell = NULL;
client->peerTbl = NULL;
client->peerTblCap = 4;
client->peerTblCount = 0;
laikaM_initVector(client->peerTbl, 4);
laikaT_initTaskService(&client->tService);
laikaT_newTask(&client->tService, LAIKA_PING_INTERVAL, shell_pingTask, client);
@ -254,7 +252,7 @@ void shellC_cleanup(tShell_client *client)
laikaT_cleanTaskService(&client->tService);
/* free peers */
for (i = 0; i < client->peerTblCount; i++) {
for (i = 0; i < laikaM_countVector(client->peerTbl); i++) {
if (client->peerTbl[i])
shellP_freePeer(client->peerTbl[i]);
}
@ -346,16 +344,15 @@ int shellC_addPeer(tShell_client *client, tShell_peer *newPeer)
{
/* find empty 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! */
break;
}
/* if we didn't find an empty id, grow the array */
if (id == client->peerTblCount) {
laikaM_growarray(tShell_peer *, client->peerTbl, 1, client->peerTblCount,
client->peerTblCap);
client->peerTblCount++;
/* if we didn't find an empty id, grow the array (ID is already set to the correct index) */
if (id == laikaM_countVector(client->peerTbl)) {
laikaM_growVector(tShell_peer *, client->peerTbl, 1);
laikaM_countVector(client->peerTbl)++;
}
/* 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)
{
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);
return client->peerTbl[id];
@ -48,7 +48,7 @@ void listPeersCMD(tShell_client *client, int argc, char *argv[])
{
int i;
for (i = 0; i < client->peerTblCount; i++) {
for (i = 0; i < laikaM_countVector(client->peerTbl); i++) {
if (client->peerTbl[i]) {
shellT_printf("%04d ", i);
shellP_printInfo(client->peerTbl[i]);
@ -172,11 +172,10 @@ void shellS_cleanupCmds(void)
char **shellS_splitCmd(char *cmd, int *argSize)
{
int argCount = 0;
int argCap = 4;
char *temp;
char **args = NULL;
char *arg = cmd;
laikaM_newVector(char *, args);
laikaM_initVector(args, 4);
do {
/* replace space with NULL terminator */
@ -192,12 +191,12 @@ char **shellS_splitCmd(char *cmd, int *argSize)
*arg++ = '\0';
}
/* insert into our 'args' array */
laikaM_growarray(char *, args, 1, argCount, argCap);
args[argCount++] = arg;
/* insert into our 'args' vector */
laikaM_growVector(char *, args, 1);
args[laikaM_countVector(args)++] = arg;
} while ((arg = strchr(arg, ' ')) != NULL); /* while we still have a delimiter */
*argSize = argCount;
*argSize = laikaM_countVector(args);
return args;
}

View File

@ -15,8 +15,42 @@
#define cursorBackward(x) printf("\033[%dD", (x))
#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;
char *cmd, *prompt = "$> ";
char *cmd = NULL, *prompt = "$> ";
int cmdCount = 0, cmdCap = 4, cmdCursor = 0;
void shellT_conioTerm(void)