#ifndef __TBL_QCMD_H #define __TBL_QCMD_H #include #include #include #include #include "debug.h" #include "util.h" #include "irc_parse.h" #define EXEC_CURRENT 0 //executes in current thread #define EXEC_SEPERATE 1 //executes in seperate thread /* Fields: */ typedef struct tble_exec { int id; char *name; /*module name, can be used just for note*/ char *cmd; /*command name*/ int type; void *(*callback)(void *); //callback } tble_exec; #define TBL_PF_EXEC_ID 1<<1 #define TBL_PF_EXEC_NAME 1<<2 #define TBL_PF_EXEC_CMD 1<<3 #define TBL_PF_EXEC_TYPE 1<<4 #define TBL_PF_EXEC_ALL 0x0fffffff /* list of registed commands Fields: */ typedef struct tbl_exec { int id; int size; int max_size; tble_exec **cmd; } tbl_exec; #define QCMD_NONE 0 //nothing happening with this command #define QCMD_TIMEOUT 1 //command running to long without result #define QCMD_DONE 2 //comand finsihed execution #define QCMD_RUNNING 3 //command still in execution state #define QCMD_PREPARE 4 //something need to be done before make it to run #define QCMD_REPLY 5 //there is replay for command #define QCMD_NOTFOUND 6 //command cant find executor #define QCMD_READY 7 //command ready to start execution #define QCMD_INIT 8 //command is set, need to find executor /* Fields: */ typedef struct tble_qcmd { int id; //command index in table int cid; //caller id, who called this command int timestamp; //when command started to execute int state; //command execution state int timeout; //timeout time for command char *cmd; //cmd name ALLOC char *param; //params ALLOC mqd_t out_q; //UNUSED mqd_t in_q; //UNUSED int tid; //table id int tidx; //table unique id of executor, check if still there ;] irc_token *tok; //parsed irc command, needed only by irc consumer thread } tble_qcmd; //list of print fields #define TBL_PF_QCMD_ID 1<<1 #define TBL_PF_QCMD_CID 1<<2 #define TBL_PF_QCMD_TIMESTAMP 1<<3 #define TBL_PF_QCMD_STATE 1<<4 #define TBL_PF_QCMD_TIMEOUT 1<<5 #define TBL_PF_QCMD_CMD 1<<6 #define TBL_PF_QCMD_PARAM 1<<7 #define TBL_PF_QCMD_OUT_Q 1<<8 #define TBL_PF_QCMD_IN_Q 1<<9 #define TBL_PF_QCMD_TID 1<<10 #define TBL_PF_QCMD_TIDX 1<<11 #define TBL_PF_QCMD_ALL 0x0fffffff /* table of commands executing Fields: */ typedef struct tbl_qcmd { int id; int size; int max_size; tble_qcmd **cmd; /*list of command executing and waiting for replay*/ } tbl_qcmd; /* use this structure to give params to command execution thread. used more or less like proxy type that prepare all needed info to pass to executor. Could have some info for execution or from executor. Fields: */ typedef struct tble_cmd_param { int id; //uniq id for cmd_param int qid; //command id that created this param mqd_t out_q; char *cmd; char *param; } tble_cmd_param; /* Fields: */ typedef struct tble_cmd_resp { int id; //response id int qid; //parametr id, matches with pid id int type; int ret_code; char *resp; } tble_cmd_resp; #define TBL_RSP_NONE 0 //default value #define TBL_RSP_NORESP 1 //no response #define TBL_RSP_ERR 2 //error happened with thread #define TBL_RSP_OK 4 //response /* create exec command, used to add to table Input: Output: !NULL - if everything whent ok NULL - if there was some kind of mistake */ tble_exec *tbl_exec_c(); /* create array where to put commands Input: size - size of array that will be created Output: !NULL - if everything whent ok NULL - if there was some kind of mistake */ tbl_exec *tbl_exec_list_c(int size); /* add new command to array Input: tbl - table of executed commands cmd - command that shoule be added Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd); /* search for command in da list Input: tbl - table of executed commands cmd - command name to search in the list Output: >=0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_exec_in_s(tbl_exec *tbl, char *cmd); /* if command is found then return pointer to it if you passs pointer further then if its will be freed or deleted from the list your programm will segfault so enjoy ;] Input: tbl - table of executed commands cmd - command name to search in the list Output: !NULL - if everything whent ok, NOFREE NULL - if there was some kind of mistake */ tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd); int tbl_exec_print(tble_exec *tble, int flags); int tbl_exec_tbl_free(tbl_exec *tbl); int tbl_exec_tel_free(tble_exec *tble); /* print all entries in table Input: tbl - table of executed commands Output: >=0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_exec_print_tbl(tbl_exec *tbl, int flags); /* create qcmd commands Input: Output: !NULL - if everything whent ok ALLOC, NOFREE NULL - if there was some kind of mistake */ tble_qcmd *tbl_qcmd_cmd_c(); /* create array of commands in processing Input: Output: !NULL - if everything whent ok NULL - if there was some kind of mistake */ tbl_qcmd* tbl_qcmd_c(int size); /* Match command with execution table. Set id if its match some command. There could be multiple tables Input: Output: */ int tbl_qcmd_exec(tbl_qcmd *tcmd, tbl_exec *texec); /* add command to executed array Input: tbl - array of executed commands cmd - command to be added to execution list ALLOC,NOFREE Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *qcmd); /* check if there any event on commands Input: Output: >=0 - if everything whent ok, returns index in the table (ret-1) -1 - if there was some kind of mistake, or nothing happened */ int tbl_qcmd_chk(tbl_qcmd *tbl); /* delete command from the list Input: tbl - array of executed commands idx - index in array that should be removed Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_del(tbl_qcmd *tbl, int idx); /* add parsed token to command list Input: tbl - executed command tk - token, its going to be copies, so fill free to FREE Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_add_tok(tble_qcmd *tbl, irc_token *tk); /* Return command if there is ont that matching this id Return !NULL - pointer to table elemnt NONFREE NULL - nothing found or error */ tble_qcmd* tbl_qcmd_match_id(tbl_qcmd *tbl, int idx); /* if there is response then try to match response and return code to executed command requester Input: tbl - array of executed cmds resp - response recieved, match it to table values Output: >0 - command -1 - if there was some kind of mistake */ int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp); /* create cmd param. take command and create parametrs to pass to executor. command should be in right state to do so. It should be READY Input: Output: !NULL - if everything whent ok NULL - if there was some kind of mistake */ tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd); /* free param structure Input: Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_cmd_param_free(tble_cmd_param *param); int tbl_cmd_param_print(tble_cmd_param *param); /* should be used in executor. takes param and creates response structure. after response structure should be filled with response result Input: Output: !NULL - if everything whent ok NULL - if there was some kind of mistake */ tble_cmd_resp* tbl_cmd_resp_c(tble_cmd_param *param); /* free response structure Input: Output: !NULL - if everything whent ok NULL - if there was some kind of mistake */ int tbl_cmd_resp_free(tble_cmd_resp *resp); int tbl_cmd_resp_print(tble_cmd_resp *resp); /* print table of commands in execution Input: Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags); /* print table of commands in execution Input: Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_print_tbl(tbl_qcmd *tbl, int flags); /* Input: Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_destroy_cmd(tble_qcmd *tbl); /* Input: Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ int tbl_qcmd_destroy_table(tbl_qcmd *tbl); /*mvar things*/ //shitty macro #define MVAR_ALLOC_STRC(VNAME,VTYPE,VRET)\ VTYPE *VNAME;\ VNAME=malloc(sizeof(VTYPE));\ if ((VNAME)==NULL){\ VRET\ }\ memset(VNAME,0,sizeof(VTYPE)); #define MVAR_ALLOC_ARR(VNAME,VTYPE,VSZ,VRET)\ VTYPE *VNAME;\ VNAME=malloc(sizeof(VTYPE)*(VSZ));\ if ((VNAME)==NULL){\ VRET\ }\ memset(VNAME,0,sizeof(VTYPE)*(VSZ)); #endif