summaryrefslogtreecommitdiff
path: root/tbl_qcmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'tbl_qcmd.c')
-rw-r--r--tbl_qcmd.c307
1 files changed, 307 insertions, 0 deletions
diff --git a/tbl_qcmd.c b/tbl_qcmd.c
new file mode 100644
index 0000000..fb45c5a
--- /dev/null
+++ b/tbl_qcmd.c
@@ -0,0 +1,307 @@
+#include "tbl_qcmd.h"
+
+tble_exec *tbl_exec_c()
+{
+ MVAR_ALLOC_STRC(ret,tble_exec,{
+ return NULL;});
+ return ret;
+}
+
+
+tbl_exec *tbl_exec_list_c(int size)
+{
+ MVAR_ALLOC_STRC(ret,tbl_exec,{
+ return NULL;});
+
+ ret->size = 0;
+ ret->max_size = size;
+
+ MVAR_ALLOC_ARR(reg_cmd,tble_exec*,size,{
+ free(ret);
+ return NULL;
+ })
+
+ ret->reg_cmd = reg_cmd;
+
+ return ret;
+}
+
+//TODO check if command allready excists
+int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd)
+{
+ if (tbl == NULL || cmd == NULL)
+ {
+ return -1;
+ }
+
+ tbl->reg_cmd[tbl->size] = cmd;
+ if (tbl->size+1<tbl->max_size)
+ {
+ tbl->size += 1;
+ } else
+ {
+ return -1;
+ }
+
+ return 0;
+}
+
+int tbl_exec_in_s(tbl_exec *tbl, char *cmd)
+{
+ return -1;
+}
+
+tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd)
+{
+ tble_exec *ret = NULL;
+ tble_exec *cur_cmd = NULL;
+ int i;
+
+ if ((tbl == NULL)||(cmd == NULL))
+ {
+ return NULL;
+ }
+
+ for (i=0;i<tbl->max_size;i++)
+ {
+ cur_cmd = tbl->reg_cmd[i];
+ if ((strncmp(cur_cmd->cmd,cmd,strlen(cmd)) == 0) &&
+ (strlen(cmd) == strlen(cur_cmd->cmd)))
+ {
+ ret = cur_cmd;
+ break;
+ }
+ }
+
+ return ret;
+}
+
+int tbl_exec_print(tbl_exec *tbl)
+{
+ int i;
+ tble_exec *cmd=NULL;
+
+ if (tbl == NULL)
+ {
+ return -1;
+ }
+
+ for (i=0;i<tbl->size;i++)
+ {
+ cmd = tbl->reg_cmd[i];
+ if (cmd != NULL)
+ {
+ printf("->%d<-\n",i);
+ printf("ID: %d\n", cmd->id);
+ printf("TYPE: %d\n", cmd->type);
+ if (cmd->name)
+ {
+ printf("%s\n", cmd->name);
+ }
+ if (cmd->cmd)
+ {
+ printf("%s\n", cmd->cmd);
+ }
+ }
+ }
+ return 0;
+}
+
+tble_qcmd *tbl_qcmd_c()
+{
+ MVAR_ALLOC_STRC(ret,tble_qcmd,{
+ return NULL;});
+ return ret;
+}
+
+
+tbl_qcmd* tbl_qcmd_list_c(int size)
+{
+ MVAR_ALLOC_STRC(ret,tbl_qcmd,{
+ return NULL;});
+
+ ret->size = 0;
+ ret->max_size = size;
+
+ //alloc array of (tble_qcmd*) pointers
+ MVAR_ALLOC_ARR(cmd,tble_qcmd*,size,{
+ free(ret);
+ return NULL;
+ })
+
+ ret->cmd = cmd;
+
+ return ret;
+}
+
+int tbl_qcmd_set_exec(tble_qcmd *qcmd, tble_exec *exec)
+{
+ int ret = -1;
+
+ if ((qcmd == NULL) || (exec == NULL))
+ return -1;
+
+ //set executed command
+ qcmd->cmd = alloc_new_str(exec->cmd);
+ qcmd->state = QCMD_PREPARE;
+ qcmd->idx_exec = exec->id;
+
+ ret = 0;
+
+ return ret;
+}
+
+
+int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *cmd)
+{
+ if (tbl == NULL || cmd == NULL)
+ return -1;
+ tbl->cmd[tbl->size] = cmd;
+ if (tbl->size+1<tbl->max_size)
+ {
+ tbl->size += 1;
+ } else
+ {
+ return -1;
+ }
+
+ return 0;
+}
+
+
+int tbl_qcmd_chk(tbl_qcmd *tbl)
+{
+ int ret=-1;
+ int i;
+
+ if (tbl == NULL)
+ {
+ return -1;
+ }
+
+ for (i=0;i<tbl->max_size;i++)
+ {
+ //if command in some state like segfaulted or
+ // finished running then lets check
+ if ((tbl->cmd[i]->state == QCMD_TIMEOUT)||
+ (tbl->cmd[i]->state == QCMD_DONE))
+ {
+ return i;
+ }
+ }
+
+ return ret;
+}
+
+
+int tbl_qcmd_del(tbl_qcmd *tbl, int idx)
+{
+ int ret=-1;
+ tble_qcmd *replace_cmd=NULL;
+
+ if (tbl == NULL || idx < 0)
+ {
+ return -1;
+ }
+
+ if (idx >= tbl->size)
+ {
+ return -1;
+ }
+
+ if (tbl->size = 1)
+ {
+ tbl->size = 0;
+ return 0;
+ }
+
+ //if last idx = size then will overwrite itself and decrease command counter
+ replace_cmd = tbl->cmd[tbl->size];
+ tbl->size -= 1;
+ tbl->cmd[idx] = replace_cmd;
+
+ //looks like no errors
+ ret = 0;
+
+ return ret;
+}
+
+int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp)
+{
+ int ret = -1;
+ tble_qcmd *single_cmd=NULL;
+
+ int i;
+
+ if ((tbl == NULL) || (resp == NULL))
+ {
+ return -1;
+ }
+
+ for (i=0;i<tbl->size;i++)
+ {
+ single_cmd = tbl->cmd[i];
+ if ((single_cmd->id == resp->id))
+ {
+ return i;
+ }
+ }
+
+ return ret;
+}
+
+tble_cmd_param* tbl_cmd_param_c()
+{
+
+ MVAR_ALLOC_STRC(ret,tble_cmd_param,{
+ return NULL;});
+
+ return ret;
+}
+
+int tbl_cmd_param_set(tble_cmd_param *cmd_param, tble_qcmd *cmd)
+{
+
+ if ((cmd_param == NULL) || (cmd == NULL))
+ {
+ return -1;
+ }
+ cmd_param->id = cmd->id;
+ cmd_param->cmd = alloc_new_str(cmd->cmd);
+ cmd_param->param = alloc_new_str(cmd->param);
+ cmd_param->out_q = cmd->out_q;
+
+ return 0;
+}
+
+
+char *alloc_new_str_s(char *str, size_t size)
+{
+ char *ret = NULL;
+
+ if (str == NULL)
+ {
+ return NULL;
+ }
+
+ //1MB is enought
+ if (size > (1024*1024))
+ {
+ return NULL;
+ }
+
+ ret = malloc(size);
+ if (ret == NULL)
+ {
+ return NULL;
+ }
+
+ memcpy(ret, str, size);
+
+ return ret;
+}
+
+char *alloc_new_str(char *str)
+{
+ return alloc_new_str_s(str, strlen(str));
+} \ No newline at end of file