diff --git a/examples/c/asyncdemo.c b/examples/c/asyncdemo.c index 9e214e0966f7848e1ea158a50882fa8c4b4739ca..07e61b871ed2bd8e857861d169973f2ff6153a72 100644 --- a/examples/c/asyncdemo.c +++ b/examples/c/asyncdemo.c @@ -45,7 +45,7 @@ typedef struct { void taos_insert_call_back(void *param, TAOS_RES *tres, int code); void taos_select_call_back(void *param, TAOS_RES *tres, int code); -void taos_error(TAOS *taos); +void shellPrintError(TAOS *taos); static void queryDB(TAOS *taos, char *command) { int i; @@ -102,7 +102,7 @@ int main(int argc, char *argv[]) taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); if (taos == NULL) - taos_error(taos); + shellPrintError(taos); printf("success to connect to server\n"); @@ -193,7 +193,7 @@ int main(int argc, char *argv[]) return 0; } -void taos_error(TAOS *con) +void shellPrintError(TAOS *con) { fprintf(stderr, "TDengine error: %s\n", taos_errstr(con)); taos_close(con); diff --git a/tools/shell/inc/shellCommand.h b/tools/shell/inc/shellCommand.h index 82a7cfbf8f338a3fc1b50a22d0c5a73c152f0b52..726264dd5cbad4b7b4424dec71210c7b6c699c1c 100644 --- a/tools/shell/inc/shellCommand.h +++ b/tools/shell/inc/shellCommand.h @@ -23,16 +23,15 @@ #define UP 3 #define DOWN 4 -typedef struct Command Command; -struct Command { - char * buffer; - char * command; +typedef struct { + char *buffer; + char *command; unsigned commandSize; unsigned bufferSize; unsigned cursorOffset; unsigned screenOffset; unsigned endOffset; -}; +} Command; extern void backspaceChar(Command *cmd); extern void clearLineBefore(Command *cmd); @@ -44,10 +43,10 @@ extern void positionCursorHome(Command *cmd); extern void positionCursorEnd(Command *cmd); extern void showOnScreen(Command *cmd); extern void updateBuffer(Command *cmd); -extern int isReadyGo(Command *cmd); +extern int isReadyGo(Command *cmd); extern void resetCommand(Command *cmd, const char s[]); -int countPrefixOnes(unsigned char c); +int countPrefixOnes(unsigned char c); void clearScreen(int ecmd_pos, int cursor_pos); void printChar(char c, int times); void positionCursor(int step, int direction); diff --git a/tools/shell/inc/shellInt.h b/tools/shell/inc/shellInt.h index fe142a4c7d336985f1dbf88faa5470d6bcaa85ca..3bdeed6e2ca0408150143bf9f236530fd09f90be 100644 --- a/tools/shell/inc/shellInt.h +++ b/tools/shell/inc/shellInt.h @@ -13,11 +13,10 @@ * along with this program. If not, see . */ -#ifndef _TD_SHELL_H_ -#define _TD_SHELL_H_ +#ifndef _TD_SHELL_INT_H_ +#define _TD_SHELL_INT_H_ #include "os.h" - #include "taos.h" #include "taosdef.h" @@ -26,64 +25,65 @@ #define HISTORY_FILE ".taos_history" #define DEFAULT_RES_SHOW_NUM 100 -typedef struct SShellHistory { - char* hist[MAX_HISTORY_SIZE]; - int hstart; - int hend; +typedef struct { + char* hist[MAX_HISTORY_SIZE]; + int32_t hstart; + int32_t hend; } SShellHistory; -typedef struct SShellArguments { - char* host; - char* password; - char* user; - char* auth; - char* database; - char* timezone; - bool is_raw_time; - bool is_use_passwd; - bool dump_config; - char file[TSDB_FILENAME_LEN]; - char dir[TSDB_FILENAME_LEN]; - int threadNum; - int check; - bool status; - bool verbose; - char* commands; - int abort; - int port; - int pktLen; - int pktNum; - char* pktType; - char* netTestRole; -} SShellArguments; +typedef struct { + const char* host; + const char* password; + const char* user; + const char* auth; + const char* database; + const char* file; + const char* cfgdir; + const char* commands; + bool is_gen_auth; + bool is_raw_time; + bool is_client; + bool is_server; + bool is_version; + bool is_dump_config; + bool is_check; + bool is_startup; + bool is_help; + uint16_t port; + int32_t pktLen; + int32_t pktNum; + int32_t abort; +} SShellArgs; + +typedef struct { + SShellArgs args; + SShellHistory history; + TAOS* conn; + int64_t result; +} SShellObj; + +int32_t shellParseArgs(int32_t argc, char* argv[]); +int32_t shellInit(); +void shellCleanup(void* arg); +void shellExit(); + +void* shellThreadLoop(void* arg); +void shellPrintError(TAOS_RES* tres, int64_t st); +int32_t shellRegexMatch(const char* s, const char* reg, int32_t cflags); +void shellGetGrantInfo(); +void shellReadHistory(); +void shellWriteHistory(); +void shellHistoryPath(char* history); -/**************** Function declarations ****************/ -extern void shellParseArgument(int argc, char* argv[], SShellArguments* arguments); -extern TAOS* shellInit(SShellArguments* args); -extern void* shellLoopQuery(void* arg); -extern void taos_error(TAOS_RES* tres, int64_t st); -extern int regex_match(const char* s, const char* reg, int cflags); -int32_t shellReadCommand(TAOS* con, char command[]); -int32_t shellRunCommand(TAOS* con, char* command); -void shellRunCommandOnServer(TAOS* con, char command[]); -void read_history(); -void write_history(); -void source_file(TAOS* con, char* fptr); -void source_dir(TAOS* con, SShellArguments* args); -void get_history_path(char* history); -void shellCheck(TAOS* con, SShellArguments* args); -void cleanup_handler(void* arg); -void exitShell(); -int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode); -void shellGetGrantInfo(void *con); -int isCommentLine(char *line); +int32_t shellReadCommand(char command[]); +int32_t shellRunCommand(char* command); +void shellRunCommandImp(char command[]); +void shellSourceFile(TAOS* con, char* fptr); +int32_t shellDumpResult(TAOS_RES* con, char* fname, int32_t* error_no, bool printMode); -/**************** Global variable declarations ****************/ -extern char PROMPT_HEADER[]; -extern char CONTINUE_PROMPT[]; -extern int prompt_size; -extern SShellHistory history; -extern SShellArguments args; -extern int64_t result; +extern char PROMPT_HEADER[]; +extern char CONTINUE_PROMPT[]; +extern int32_t prompt_size; +extern SShellObj shell; -#endif +#endif /*_TD_SHELL_INT_H_*/ diff --git a/tools/shell/inc/syncMsg.h b/tools/shell/inc/syncMsg.h deleted file mode 100644 index 85ac9c78affa5282d5ca703caffc1bc5c24461bb..0000000000000000000000000000000000000000 --- a/tools/shell/inc/syncMsg.h +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef TDENGINE_SYNC_MSG_H -#define TDENGINE_SYNC_MSG_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "tsync.h" - -typedef enum { - TAOS_SMSG_START = 0, - TAOS_SMSG_SYNC_DATA = 1, - TAOS_SMSG_SYNC_DATA_RSP = 2, - TAOS_SMSG_SYNC_FWD = 3, - TAOS_SMSG_SYNC_FWD_RSP = 4, - TAOS_SMSG_SYNC_REQ = 5, - TAOS_SMSG_SYNC_REQ_RSP = 6, - TAOS_SMSG_SYNC_MUST = 7, - TAOS_SMSG_SYNC_MUST_RSP = 8, - TAOS_SMSG_STATUS = 9, - TAOS_SMSG_STATUS_RSP = 10, - TAOS_SMSG_SETUP = 11, - TAOS_SMSG_SETUP_RSP = 12, - TAOS_SMSG_SYNC_FILE = 13, - TAOS_SMSG_SYNC_FILE_RSP = 14, - TAOS_SMSG_TEST = 15, - TAOS_SMSG_END = 16 -} ESyncMsgType; - -typedef enum { - SYNC_STATUS_BROADCAST, - SYNC_STATUS_BROADCAST_RSP, - SYNC_STATUS_SETUP_CONN, - SYNC_STATUS_SETUP_CONN_RSP, - SYNC_STATUS_EXCHANGE_DATA, - SYNC_STATUS_EXCHANGE_DATA_RSP, - SYNC_STATUS_CHECK_ROLE, - SYNC_STATUS_CHECK_ROLE_RSP -} ESyncStatusType; - -#pragma pack(push, 1) - -typedef struct { - int8_t type; // msg type - int8_t protocol; // protocol version - uint16_t signature; // fixed value - int32_t code; // - int32_t cId; // cluster Id - int32_t vgId; // vg ID - int32_t len; // content length, does not include head - uint32_t cksum; -} SSyncHead; - -typedef struct { - SSyncHead head; - uint16_t port; - uint16_t tranId; - int32_t sourceId; // only for arbitrator - char fqdn[TSDB_FQDN_LEN]; -} SSyncMsg; - -typedef struct { - SSyncHead head; - int8_t sync; - int8_t reserved; - uint16_t tranId; - int8_t reserverd[4]; -} SSyncRsp; - -typedef struct { - int8_t role; - uint64_t version; -} SPeerStatus; - -typedef struct { - SSyncHead head; - int8_t role; - int8_t ack; - int8_t type; - int8_t reserved[3]; - uint16_t tranId; - uint64_t version; - SPeerStatus peersStatus[TAOS_SYNC_MAX_REPLICA]; -} SPeersStatus; - -typedef struct { - SSyncHead head; - uint64_t fversion; -} SFileVersion; - -typedef struct { - SSyncHead head; - int8_t ack; -} SFileAck; - -typedef struct { - SSyncHead head; - uint64_t version; - int32_t code; -} SFwdRsp; - -#pragma pack(pop) - -#define SYNC_PROTOCOL_VERSION 1 -#define SYNC_SIGNATURE ((uint16_t)(0xCDEF)) - -extern char *statusType[]; - -uint16_t syncGenTranId(); -int32_t syncCheckHead(SSyncHead *pHead); - -void syncBuildSyncFwdMsg(SSyncHead *pHead, int32_t vgId, int32_t len); -void syncBuildSyncFwdRsp(SFwdRsp *pMsg, int32_t vgId, uint64_t version, int32_t code); -void syncBuildSyncReqMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildSyncDataMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildSyncSetupMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildPeersStatus(SPeersStatus *pMsg, int32_t vgId); -void syncBuildSyncTestMsg(SSyncMsg *pMsg, int32_t vgId); - -void syncBuildFileAck(SFileAck *pMsg, int32_t vgId); -void syncBuildFileVersion(SFileVersion *pMsg, int32_t vgId); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_VNODEPEER_H diff --git a/tools/shell/inc/tsync.h b/tools/shell/inc/tsync.h deleted file mode 100644 index d1b68e3f5a1e27b63dd08a4d1d4862c7b1e68179..0000000000000000000000000000000000000000 --- a/tools/shell/inc/tsync.h +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef TDENGINE_SYNC_H -#define TDENGINE_SYNC_H - -#ifdef __cplusplus -extern "C" { -#endif - -#define TAOS_SYNC_MAX_REPLICA 5 -#define TAOS_SYNC_MAX_INDEX 0x7FFFFFFF - -typedef enum { - TAOS_SYNC_ROLE_OFFLINE = 0, - TAOS_SYNC_ROLE_UNSYNCED = 1, - TAOS_SYNC_ROLE_SYNCING = 2, - TAOS_SYNC_ROLE_SLAVE = 3, - TAOS_SYNC_ROLE_MASTER = 4 -} ESyncRole; - -typedef enum { - TAOS_SYNC_STATUS_INIT = 0, - TAOS_SYNC_STATUS_START = 1, - TAOS_SYNC_STATUS_FILE = 2, - TAOS_SYNC_STATUS_CACHE = 3 -} ESyncStatus; - -typedef struct { - uint32_t nodeId; // node ID assigned by TDengine - uint16_t nodePort; // node sync Port - char nodeFqdn[TSDB_FQDN_LEN]; // node FQDN -} SNodeInfo; - -typedef struct { - int8_t quorum; // number of confirms required, >=1 - int8_t replica; // number of replications, >=1 - SNodeInfo nodeInfo[TAOS_SYNC_MAX_REPLICA]; -} SSyncCfg; - -typedef struct { - int32_t selfIndex; - uint32_t nodeId[TAOS_SYNC_MAX_REPLICA]; - int32_t role[TAOS_SYNC_MAX_REPLICA]; -} SNodesRole; - -// get the wal file from index or after -// return value, -1: error, 1:more wal files, 0:last WAL. if name[0]==0, no WAL file -typedef int32_t (*FGetWalInfo)(int32_t vgId, char *fileName, int64_t *fileId); - -// when a forward pkt is received, call this to handle data -typedef int32_t (*FWriteToCache)(int32_t vgId, void *pHead, int32_t qtype, void *pMsg); - -// when forward is confirmed by peer, master call this API to notify app -typedef void (*FConfirmForward)(int32_t vgId, void *mhandle, int32_t code); - -// when role is changed, call this to notify app -typedef void (*FNotifyRole)(int32_t vgId, int8_t role); - -// if a number of retrieving data failed, call this to start flow control -typedef void (*FNotifyFlowCtrl)(int32_t vgId, int32_t level); - -// when data file is synced successfully, notity app -typedef void (*FStartSyncFile)(int32_t vgId); -typedef void (*FStopSyncFile)(int32_t vgId, uint64_t fversion); - -// get file version -typedef int32_t (*FGetVersion)(int32_t vgId, uint64_t *fver, uint64_t *vver); - -typedef int32_t (*FSendFile)(void *tsdb, SOCKET socketFd); -typedef int32_t (*FRecvFile)(void *tsdb, SOCKET socketFd); - -typedef struct { - int32_t vgId; // vgroup ID - uint64_t version; // initial version - SSyncCfg syncCfg; // configuration from mgmt - char path[TSDB_FILENAME_LEN]; // path to the file - void * pTsdb; - FGetWalInfo getWalInfoFp; - FWriteToCache writeToCacheFp; - FConfirmForward confirmForward; - FNotifyRole notifyRoleFp; - FNotifyFlowCtrl notifyFlowCtrlFp; - FStartSyncFile startSyncFileFp; - FStopSyncFile stopSyncFileFp; - FGetVersion getVersionFp; - FSendFile sendFileFp; - FRecvFile recvFileFp; -} SSyncInfo; - -typedef void *tsync_h; - -int32_t syncInit(); -void syncCleanUp(); - -int64_t syncStart(const SSyncInfo *); -void syncStop(int64_t rid); -int32_t syncReconfig(int64_t rid, const SSyncCfg *); -int32_t syncForwardToPeer(int64_t rid, void *pHead, void *mhandle, int32_t qtype, bool force); -void syncConfirmForward(int64_t rid, uint64_t version, int32_t code, bool force); -void syncRecover(int64_t rid); // recover from other nodes: -int32_t syncGetNodesRole(int64_t rid, SNodesRole *); - -extern char *syncRole[]; - -//global configurable parameters -extern int32_t sDebugFlag; -extern char tsArbitrator[]; -extern uint16_t tsSyncPort; - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_SYNC_H diff --git a/tools/shell/src/backup/shellCheck.c b/tools/shell/src/backup/shellCheck.c deleted file mode 100644 index 9e23e9d02663c6b4f8e34f78c701033c749b054d..0000000000000000000000000000000000000000 --- a/tools/shell/src/backup/shellCheck.c +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _GNU_SOURCE -#define _XOPEN_SOURCE -#define _DEFAULT_SOURCE - -#include "os.h" -#include "shellInt.h" -#include "shellCommand.h" -#include "tglobal.h" -#include "tutil.h" - -#define SHELL_SQL_LEN 1024 -static int32_t tbNum = 0; -static int32_t tbMallocNum = 0; -static char ** tbNames = NULL; -static int32_t checkedNum = 0; -static int32_t errorNum = 0; - -typedef struct { - TdThread threadID; - int threadIndex; - int totalThreads; - void * taos; - char * db; -} ShellThreadObj; - -static int32_t shellUseDb(TAOS *con, char *db) { - if (db == NULL) { - fprintf(stdout, "no dbname input\n"); - return -1; - } - - char sql[SHELL_SQL_LEN] = {0}; - snprintf(sql, SHELL_SQL_LEN, "use %s", db); - - TAOS_RES *pSql = taos_query(con, sql); - int32_t code = taos_errno(pSql); - if (code != 0) { - fprintf(stdout, "failed to execute sql:%s since %s", sql, taos_errstr(pSql)); - } - - taos_free_result(pSql); - return code; -} - -static int32_t shellShowTables(TAOS *con, char *db) { - char sql[SHELL_SQL_LEN] = {0}; - snprintf(sql, SHELL_SQL_LEN, "show %s.tables", db); - - TAOS_RES *pSql = taos_query(con, sql); - int32_t code = taos_errno(pSql); - - if (code != 0) { - fprintf(stdout, "failed to execute sql:%s since %s\n", sql, taos_errstr(pSql)); - } else { - TAOS_ROW row; - while ((row = taos_fetch_row(pSql))) { - int32_t tbIndex = tbNum++; - if (tbMallocNum < tbNum) { - tbMallocNum = (tbMallocNum * 2 + 1); - char** tbNames1 = taosMemoryRealloc(tbNames, tbMallocNum * sizeof(char *)); - if (tbNames1 == NULL) { - fprintf(stdout, "failed to malloc tablenames, num:%d\n", tbMallocNum); - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - break; - } - tbNames = tbNames1; - } - - tbNames[tbIndex] = taosMemoryMalloc(TSDB_TABLE_NAME_LEN); - strncpy(tbNames[tbIndex], (const char *)row[0], TSDB_TABLE_NAME_LEN); - if (tbIndex % 100000 == 0 && tbIndex != 0) { - fprintf(stdout, "%d tablenames fetched\n", tbIndex); - } - } - } - - taos_free_result(pSql); - - fprintf(stdout, "total %d tablenames fetched, over\n", tbNum); - return code; -} - -static void shellFreeTbnames() { - for (int32_t i = 0; i < tbNum; ++i) { - taosMemoryFree(tbNames[i]); - } - taosMemoryFree(tbNames); -} - -static void *shellCheckThreadFp(void *arg) { - ShellThreadObj *pThread = (ShellThreadObj *)arg; - - setThreadName("shellCheckThrd"); - - int32_t interval = tbNum / pThread->totalThreads + 1; - int32_t start = pThread->threadIndex * interval; - int32_t end = (pThread->threadIndex + 1) * interval; - - if (end > tbNum) end = tbNum + 1; - - char file[32] = {0}; - snprintf(file, 32, "tb%d.txt", pThread->threadIndex); - - TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); - if (!fp) { - fprintf(stdout, "failed to open %s, reason:%s", file, strerror(errno)); - return NULL; - } - - char sql[SHELL_SQL_LEN]; - for (int32_t t = start; t < end; ++t) { - char *tbname = tbNames[t]; - if (tbname == NULL) break; - - snprintf(sql, SHELL_SQL_LEN, "select * from %s limit 1", tbname); - - TAOS_RES *pSql = taos_query(pThread->taos, sql); - int32_t code = taos_errno(pSql); - if (code != 0) { - int32_t len = snprintf(sql, SHELL_SQL_LEN, "drop table %s.%s;\n", pThread->db, tbname); - taosWriteFile(pFile, sql, len); - atomic_add_fetch_32(&errorNum, 1); - } - - int32_t cnum = atomic_add_fetch_32(&checkedNum, 1); - if (cnum % 5000 == 0 && cnum != 0) { - fprintf(stdout, "%d tables checked\n", cnum); - } - - taos_free_result(pSql); - } - - taosFsync(pFile); - taosCloseFile(&pFile); - - return NULL; -} - -static void shellRunCheckThreads(TAOS *con, SShellArguments *_args) { - TdThreadAttr thattr; - ShellThreadObj *threadObj = (ShellThreadObj *)taosMemoryCalloc(_args->threadNum, sizeof(ShellThreadObj)); - for (int t = 0; t < _args->threadNum; ++t) { - ShellThreadObj *pThread = threadObj + t; - pThread->threadIndex = t; - pThread->totalThreads = _args->threadNum; - pThread->taos = con; - pThread->db = _args->database; - - taosThreadAttrInit(&thattr); - taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - - if (taosThreadCreate(&(pThread->threadID), &thattr, shellCheckThreadFp, (void *)pThread) != 0) { - fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex); - exit(0); - } - } - - for (int t = 0; t < _args->threadNum; ++t) { - taosThreadJoin(threadObj[t].threadID, NULL); - } - - for (int t = 0; t < _args->threadNum; ++t) { - taos_close(threadObj[t].taos); - } - taosMemoryFree(threadObj); -} - -void shellCheck(TAOS *con, SShellArguments *_args) { - int64_t start = taosGetTimestampMs(); - - if (shellUseDb(con, _args->database) != 0) { - shellFreeTbnames(); - return; - } - - if (shellShowTables(con, _args->database) != 0) { - shellFreeTbnames(); - return; - } - - fprintf(stdout, "total %d tables will be checked by %d threads\n", tbNum, _args->threadNum); - shellRunCheckThreads(con, _args); - - int64_t end = taosGetTimestampMs(); - fprintf(stdout, "total %d tables checked, failed:%d, time spent %.2f seconds\n", checkedNum, errorNum, - (end - start) / 1000.0); -} diff --git a/tools/shell/src/backup/shellDarwin.c b/tools/shell/src/backup/shellDarwin.c index d9807409f7c8e7cca97a41dfffeefe938da6ae1f..9bae306a7017140b14cc5c5c5c447a2809ddc94c 100644 --- a/tools/shell/src/backup/shellDarwin.c +++ b/tools/shell/src/backup/shellDarwin.c @@ -67,7 +67,7 @@ char DARWINCLIENT_VERSION[] = "Welcome to the TDengine shell from %s, Clien "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; char g_password[SHELL_MAX_PASSWORD_LEN]; -void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { +void shellParseArgs(int argc, char *argv[], SShellArgs *arguments) { wordexp_t full_path; for (int i = 1; i < argc; i++) { // for host @@ -238,8 +238,8 @@ int32_t shellReadCommand(TAOS *con, char *command) { printf("\n"); taos_close(con); // write the history - write_history(); - exitShell(); + shellWriteHistory(); + shellExit(); break; case 5: // ctrl E positionCursorEnd(&cmd); @@ -355,7 +355,7 @@ int32_t shellReadCommand(TAOS *con, char *command) { return 0; } -void *shellLoopQuery(void *arg) { +void *shellThreadLoop(void *arg) { if (indicator) { getOldTerminalMode(); indicator = 0; @@ -363,9 +363,9 @@ void *shellLoopQuery(void *arg) { TAOS *con = (TAOS *)arg; - setThreadName("shellLoopQuery"); + setThreadName("shellThreadLoop"); - taosThreadCleanupPush(cleanup_handler, NULL); + taosThreadCleanupPush(shellCleanup, NULL); char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); if (command == NULL){ @@ -387,14 +387,14 @@ void *shellLoopQuery(void *arg) { } while (shellRunCommand(con, command) == 0); taosMemoryFreeClear(command); - exitShell(); + shellExit(); taosThreadCleanupPop(1); return NULL; } -void get_history_path(char *history) { sprintf(history, "%s/%s", getpwuid(getuid())->pw_dir, HISTORY_FILE); } +void shellHistoryPath(char *history) { sprintf(history, "%s/%s", getpwuid(getuid())->pw_dir, HISTORY_FILE); } void clearScreen(int ecmd_pos, int cursor_pos) { struct winsize w; @@ -490,9 +490,9 @@ void showOnScreen(Command *cmd) { fflush(stdout); } -void cleanup_handler(void *arg) { resetTerminalMode(); } +void shellCleanup(void *arg) { resetTerminalMode(); } -void exitShell() { +void shellExit() { resetTerminalMode(); exit(EXIT_SUCCESS); } diff --git a/tools/shell/src/backup/shellImport.c b/tools/shell/src/backup/shellImport.c deleted file mode 100644 index 2eeee616b4e39efd16af9dfe5ed6cfa9c54176b9..0000000000000000000000000000000000000000 --- a/tools/shell/src/backup/shellImport.c +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _GNU_SOURCE -#define _XOPEN_SOURCE -#define _DEFAULT_SOURCE - -#include "os.h" -#include "shellInt.h" -#include "shellCommand.h" -#include "tglobal.h" -#include "tutil.h" - -static char **shellSQLFiles = NULL; -static int32_t shellSQLFileNum = 0; -static char shellTablesSQLFile[TSDB_FILENAME_LEN] = {0}; - -typedef struct { - TdThread threadID; - int threadIndex; - int totalThreads; - void *taos; -} ShellThreadObj; - -static int shellGetFilesNum(const char *directoryName, const char *prefix) -{ - char cmd[1024] = { 0 }; - sprintf(cmd, "ls %s/*.%s | wc -l ", directoryName, prefix); - - char buf[1024] = { 0 }; - if (taosSystem(cmd, buf, sizeof(buf)) < 0) { - fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); - exit(0); - } - - int fileNum = 0; - if (sscanf(buf, "%d", &fileNum) != 1) { - fprintf(stderr, "ERROR: failed to execute:%s, parse result error\n", cmd); - exit(0); - } - - if (fileNum <= 0) { - fprintf(stderr, "ERROR: directory:%s is empry\n", directoryName); - exit(0); - } - - return fileNum; -} - -static void shellParseDirectory(const char *directoryName, const char *prefix, char **fileArray, int totalFiles) -{ - char cmd[1024] = { 0 }; - sprintf(cmd, "ls %s/*.%s | sort", directoryName, prefix); - - char buf[1024] = { 0 }; - if (taosSystem(cmd, buf, sizeof(buf)) < 0) { - fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); - exit(0); - } - - int fileNum = 0; - while (sscanf(buf, "%128s", fileArray[fileNum++])) { - if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) { - fileNum--; - } - if (fileNum >= totalFiles) { - break; - } - } - - if (fileNum != totalFiles) { - fprintf(stderr, "ERROR: directory:%s changed while read\n", directoryName); - exit(0); - } -} - -static void shellCheckTablesSQLFile(const char *directoryName) -{ - sprintf(shellTablesSQLFile, "%s/tables.sql", directoryName); - - if (taosFStatFile(shellTablesSQLFile, NULL, NULL) < 0) { - shellTablesSQLFile[0] = 0; - } -} - -static void shellMallocSQLFiles() -{ - shellSQLFiles = (char**)taosMemoryCalloc(shellSQLFileNum, sizeof(char*)); - for (int i = 0; i < shellSQLFileNum; i++) { - shellSQLFiles[i] = taosMemoryCalloc(1, TSDB_FILENAME_LEN); - } -} - -static void shellGetDirectoryFileList(char *inputDir) -{ - if (!taosDirExist(inputDir)) { - fprintf(stderr, "ERROR: %s not exist\n", inputDir); - exit(0); - } - - if (taosIsDir(inputDir)) { - shellCheckTablesSQLFile(inputDir); - shellSQLFileNum = shellGetFilesNum(inputDir, "sql"); - int totalSQLFileNum = shellSQLFileNum; - if (shellTablesSQLFile[0] != 0) { - shellSQLFileNum--; - } - shellMallocSQLFiles(); - shellParseDirectory(inputDir, "sql", shellSQLFiles, shellSQLFileNum); - fprintf(stdout, "\nstart to dispose %d files in %s\n", totalSQLFileNum, inputDir); - } - else { - fprintf(stderr, "ERROR: %s is not a directory\n", inputDir); - exit(0); - } -} - -static void shellSourceFile(TAOS *con, char *fptr) { - wordexp_t full_path; - int read_len = 0; - char * cmd = taosMemoryMalloc(tsMaxSQLStringLen); - size_t cmd_len = 0; - char * line = NULL; - - if (wordexp(fptr, &full_path, 0) != 0) { - fprintf(stderr, "ERROR: illegal file name\n"); - taosMemoryFree(cmd); - return; - } - - char *fname = full_path.we_wordv[0]; - if (fname == NULL) { - fprintf(stderr, "ERROR: invalid filename\n"); - taosMemoryFree(cmd); - return; - } - - /* - if (access(fname, F_OK) != 0) { - fprintf(stderr, "ERROR: file %s is not exist\n", fptr); - - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - - if (access(fname, R_OK) != 0) { - fprintf(stderr, "ERROR: file %s is not readable\n", fptr); - - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - */ - - // FILE *f = fopen(fname, "r"); - TdFilePtr pFile = taosOpenFile(fname, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - fprintf(stderr, "ERROR: failed to open file %s\n", fname); - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - - fprintf(stdout, "begin import file:%s\n", fname); - - int lineNo = 0; - while ((read_len = taosGetLineFile(pFile, &line)) != -1) { - ++lineNo; - if (read_len >= tsMaxSQLStringLen) continue; - line[--read_len] = '\0'; - - if (read_len == 0 || isCommentLine(line)) { // line starts with # - continue; - } - - if (line[read_len - 1] == '\\') { - line[read_len - 1] = ' '; - memcpy(cmd + cmd_len, line, read_len); - cmd_len += read_len; - continue; - } - - memcpy(cmd + cmd_len, line, read_len); - - TAOS_RES* pSql = taos_query(con, cmd); - int32_t code = taos_errno(pSql); - - if (code != 0) { - fprintf(stderr, "DB error: %s: %s (%d)\n", taos_errstr(pSql), fname, lineNo); - } - - /* free local resouce: allocated memory/metric-meta refcnt */ - taos_free_result(pSql); - - memset(cmd, 0, MAX_COMMAND_SIZE); - cmd_len = 0; - } - - taosMemoryFree(cmd); - if(line != NULL) taosMemoryFree(line); - wordfree(&full_path); - taosCloseFile(&pFile); -} - -void* shellImportThreadFp(void *arg) -{ - ShellThreadObj *pThread = (ShellThreadObj*)arg; - setThreadName("shellImportThrd"); - - for (int f = 0; f < shellSQLFileNum; ++f) { - if (f % pThread->totalThreads == pThread->threadIndex) { - char *SQLFileName = shellSQLFiles[f]; - shellSourceFile(pThread->taos, SQLFileName); - } - } - - return NULL; -} - -static void shellRunImportThreads(SShellArguments* _args) -{ - TdThreadAttr thattr; - ShellThreadObj *threadObj = (ShellThreadObj *)taosMemoryCalloc(_args->threadNum, sizeof(ShellThreadObj)); - for (int t = 0; t < _args->threadNum; ++t) { - ShellThreadObj *pThread = threadObj + t; - pThread->threadIndex = t; - pThread->totalThreads = _args->threadNum; - pThread->taos = taos_connect(_args->host, _args->user, _args->password, _args->database, tsDnodeShellPort); - if (pThread->taos == NULL) { - fprintf(stderr, "ERROR: thread:%d failed connect to TDengine, error:%s\n", pThread->threadIndex, "null taos"/*taos_errstr(pThread->taos)*/); - exit(0); - } - - taosThreadAttrInit(&thattr); - taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - - if (taosThreadCreate(&(pThread->threadID), &thattr, shellImportThreadFp, (void*)pThread) != 0) { - fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex); - exit(0); - } - } - - for (int t = 0; t < _args->threadNum; ++t) { - taosThreadJoin(threadObj[t].threadID, NULL); - } - - for (int t = 0; t < _args->threadNum; ++t) { - taos_close(threadObj[t].taos); - } - taosMemoryFree(threadObj); -} - -void source_dir(TAOS* con, SShellArguments* _args) { - shellGetDirectoryFileList(_args->dir); - int64_t start = taosGetTimestampMs(); - - if (shellTablesSQLFile[0] != 0) { - shellSourceFile(con, shellTablesSQLFile); - int64_t end = taosGetTimestampMs(); - fprintf(stdout, "import %s finished, time spent %.2f seconds\n", shellTablesSQLFile, (end - start) / 1000.0); - } - - shellRunImportThreads(_args); - int64_t end = taosGetTimestampMs(); - fprintf(stdout, "import %s finished, time spent %.2f seconds\n", _args->dir, (end - start) / 1000.0); -} diff --git a/tools/shell/src/backup/shellWindows.c b/tools/shell/src/backup/shellWindows.c index 53bcb84cc8c75b507dc3b2f97c39f2887745af85..bf1c683d94ae7fac76870958925fd0ba4d3c0f85 100644 --- a/tools/shell/src/backup/shellWindows.c +++ b/tools/shell/src/backup/shellWindows.c @@ -70,7 +70,7 @@ void printHelp() { char g_password[SHELL_MAX_PASSWORD_LEN]; -void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { +void shellParseArgs(int argc, char *argv[], SShellArgs *arguments) { for (int i = 1; i < argc; i++) { // for host if (strcmp(argv[i], "-h") == 0) { @@ -231,7 +231,7 @@ void shellPrintContinuePrompt() { printf("%s", CONTINUE_PROMPT); } void shellPrintPrompt() { printf("%s", PROMPT_HEADER); } void updateBuffer(Command *cmd) { - if (regex_match(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); + if (shellRegexMatch(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); strcat(cmd->buffer, cmd->command); memset(cmd->command, 0, MAX_COMMAND_SIZE); @@ -246,7 +246,7 @@ int isReadyGo(Command *cmd) { char *reg_str = "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^" "\\s*clear\\s*$)"; - if (regex_match(total, reg_str, REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(total, reg_str, REG_EXTENDED | REG_ICASE)) { taosMemoryFree(total); return 1; } @@ -299,7 +299,7 @@ int32_t shellReadCommand(TAOS *con, char command[]) { return 0; } -void *shellLoopQuery(void *arg) { +void *shellThreadLoop(void *arg) { TAOS *con = (TAOS *)arg; char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); if (command == NULL) return NULL; @@ -320,6 +320,6 @@ void *shellLoopQuery(void *arg) { return NULL; } -void get_history_path(char *history) { sprintf(history, "C:/TDengine/%s", HISTORY_FILE); } +void shellHistoryPath(char *history) { sprintf(history, "C:/TDengine/%s", HISTORY_FILE); } -void exitShell() { exit(EXIT_SUCCESS); } +void shellExit() { exit(EXIT_SUCCESS); } diff --git a/tools/shell/src/backup/tnettest.h b/tools/shell/src/backup/tnettest.h deleted file mode 100644 index 8a03b67628ffd460a4aa95ad4de8110b71472496..0000000000000000000000000000000000000000 --- a/tools/shell/src/backup/tnettest.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef TDENGINE_TNETTEST_H -#define TDENGINE_TNETTEST_H - -#ifdef __cplusplus -extern "C" { -#endif - -void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, int32_t pkgNum, char *pkgType); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_TNETTEST_H diff --git a/tools/shell/src/shellCommand.c b/tools/shell/src/shellCommand.c index c9cd57fc17c95722cdcb104e5257068c53ba589b..1ec222fcc89fd95eb935c39c54e51d83440ce48e 100644 --- a/tools/shell/src/shellCommand.c +++ b/tools/shell/src/shellCommand.c @@ -214,7 +214,7 @@ void positionCursor(int step, int direction) { void updateBuffer(Command *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); - if (regex_match(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); + if (shellRegexMatch(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); strcat(cmd->buffer, cmd->command); cmd->bufferSize += cmd->commandSize; @@ -236,7 +236,7 @@ int isReadyGo(Command *cmd) { char *reg_str = "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^" "\\s*clear\\s*$)"; - if (regex_match(total, reg_str, REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(total, reg_str, REG_EXTENDED | REG_ICASE)) { taosMemoryFree(total); return 1; } diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index e1a1924b2dd9f51f8f73746d6d1896410f2f643c..9a4ef8b65a9fee2dc2929db7904ff727f7e7c982 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -68,7 +68,7 @@ extern TAOS *taos_connect_auth(const char *ip, const char *user, const char *a /* * FUNCTION: Initialize the shell. */ -TAOS *shellInit(SShellArguments *_args) { +TAOS *shellInit(SShellArgs *_args) { printf("\n"); if (!_args->is_use_passwd) { #ifdef TD_WINDOWS @@ -108,7 +108,7 @@ TAOS *shellInit(SShellArguments *_args) { } /* Read history TODO : release resources here*/ - read_history(); + shellReadHistory(); // Check if it is temperory run if (_args->commands != NULL || _args->file[0] != 0) { @@ -118,29 +118,14 @@ TAOS *shellInit(SShellArguments *_args) { } if (_args->file[0] != 0) { - source_file(con, _args->file); + shellSourceFile(con, _args->file); } taos_close(con); - write_history(); + shellWriteHistory(); exit(EXIT_SUCCESS); } -#if 0 -#ifndef WINDOWS - if (_args->dir[0] != 0) { - source_dir(con, _args); - taos_close(con); - exit(EXIT_SUCCESS); - } - - if (_args->check != 0) { - shellCheck(con, _args); - taos_close(con); - exit(EXIT_SUCCESS); - } -#endif -#endif return con; } @@ -161,22 +146,22 @@ static int32_t shellRunSingleCommand(TAOS *con, char *command) { } // Analyse the command. - if (regex_match(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) { taos_close(con); - write_history(); + shellWriteHistory(); #ifdef WINDOWS exit(EXIT_SUCCESS); #endif return -1; } - if (regex_match(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) { // If clear the screen. system("clear"); return 0; } - if (regex_match(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$", + if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$", REG_EXTENDED | REG_ICASE)) { strtok(command, " \t"); strtok(NULL, " \t"); @@ -189,17 +174,17 @@ static int32_t shellRunSingleCommand(TAOS *con, char *command) { return 0; } - if (regex_match(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) { /* If source file. */ char *c_ptr = strtok(command, " ;"); assert(c_ptr != NULL); c_ptr = strtok(NULL, " ;"); assert(c_ptr != NULL); - source_file(con, c_ptr); + shellSourceFile(con, c_ptr); return 0; } - shellRunCommandOnServer(con, command); + shellRunCommandImp(con, command); return 0; } @@ -294,7 +279,7 @@ void freeResultWithRid(int64_t rid) { #endif } -void shellRunCommandOnServer(TAOS *con, char command[]) { +void shellRunCommandImp(TAOS *con, char command[]) { int64_t st, et; wordexp_t full_path; char *sptr = NULL; @@ -330,13 +315,13 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { TAOS_RES *pSql = taos_query(con, command); if (taos_errno(pSql)) { - taos_error(pSql, st); + shellPrintError(pSql, st); return; } int64_t oresult = atomic_load_64(&result); - if (regex_match(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) { fprintf(stdout, "Database changed.\n\n"); fflush(stdout); @@ -383,14 +368,14 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { } /* Function to do regular expression check */ -int regex_match(const char *s, const char *reg, int cflags) { +int shellRegexMatch(const char *s, const char *reg, int cflags) { regex_t regex; char msgbuf[100] = {0}; /* Compile regular expression */ if (regcomp(®ex, reg, cflags) != 0) { fprintf(stderr, "Fail to compile regex"); - exitShell(); + shellExit(); } /* Execute regular expression */ @@ -405,7 +390,7 @@ int regex_match(const char *s, const char *reg, int cflags) { regerror(reti, ®ex, msgbuf, sizeof(msgbuf)); fprintf(stderr, "Regex match failed: %s\n", msgbuf); regfree(®ex); - exitShell(); + shellExit(); } return 0; @@ -695,7 +680,7 @@ bool isSelectQuery(TAOS_RES *tres) { #if 0 char *sql = tscGetSqlStr(tres); - if (regex_match(sql, "^[\t ]*select[ \t]*", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(sql, "^[\t ]*select[ \t]*", REG_EXTENDED | REG_ICASE)) { return true; } #endif @@ -900,7 +885,7 @@ int shellDumpResult(TAOS_RES *tres, char *fname, int *error_no, bool vertical) { return numOfRows; } -void read_history() { +void shellReadHistory() { // Initialize history memset(history.hist, 0, sizeof(char *) * MAX_HISTORY_SIZE); history.hstart = 0; @@ -909,7 +894,7 @@ void read_history() { int read_size = 0; char f_history[TSDB_FILENAME_LEN]; - get_history_path(f_history); + shellHistoryPath(f_history); // FILE *f = fopen(f_history, "r"); TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_READ | TD_FILE_STREAM); @@ -937,9 +922,9 @@ void read_history() { taosCloseFile(&pFile); } -void write_history() { +void shellWriteHistory() { char f_history[TSDB_FILENAME_LEN]; - get_history_path(f_history); + shellHistoryPath(f_history); // FILE *f = fopen(f_history, "w"); TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM); @@ -960,7 +945,7 @@ void write_history() { taosCloseFile(&pFile); } -void taos_error(TAOS_RES *tres, int64_t st) { +void shellPrintError(TAOS_RES *tres, int64_t st) { int64_t et = taosGetTimestampUs(); atomic_store_ptr(&result, 0); fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6); @@ -970,10 +955,10 @@ void taos_error(TAOS_RES *tres, int64_t st) { int isCommentLine(char *line) { if (line == NULL) return 1; - return regex_match(line, "^\\s*#.*", REG_EXTENDED); + return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED); } -void source_file(TAOS *con, char *fptr) { +void shellSourceFile(TAOS *con, char *fptr) { wordexp_t full_path; int read_len = 0; char *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1); diff --git a/tools/shell/src/shellMain.c b/tools/shell/src/shellMain.c index 83b0f48677734f4329c827487e5ec61bdc718e40..7c7d80cb30fb531fc4505d37dce0275ef383684c 100644 --- a/tools/shell/src/shellMain.c +++ b/tools/shell/src/shellMain.c @@ -64,7 +64,7 @@ static struct argp_option options[] = { static error_t parse_opt(int key, char *arg, struct argp_state *state) { /* Get the input argument from argp_parse, which we know is a pointer to our arguments structure. */ - SShellArguments *arguments = state->input; + SShellArgs *arguments = state->input; wordexp_t full_path; switch (key) { @@ -188,7 +188,7 @@ char LINUXCLIENT_VERSION[] = "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; char g_password[SHELL_MAX_PASSWORD_LEN]; -static void parse_args(int argc, char *argv[], SShellArguments *arguments) { +static void parse_args(int argc, char *argv[], SShellArgs *arguments) { for (int i = 1; i < argc; i++) { if ((strncmp(argv[i], "-p", 2) == 0) || (strncmp(argv[i], "--password", 10) == 0)) { printf(LINUXCLIENT_VERSION, tsOsName, taos_get_client_info()); @@ -212,7 +212,7 @@ static void parse_args(int argc, char *argv[], SShellArguments *arguments) { } } -void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { +int32_t shellParseArgs(int argc, char *argv[], SShellArgs *arguments) { static char verType[32] = {0}; sprintf(verType, "version: %s\n", version); @@ -232,6 +232,8 @@ void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { abort(); #endif } + + return 0; } int32_t shellReadCommand(TAOS *con, char *command) { @@ -275,8 +277,8 @@ int32_t shellReadCommand(TAOS *con, char *command) { printf("\n"); taos_close(con); // write the history - write_history(); - exitShell(); + shellWriteHistory(); + shellExit(); break; case 5: // ctrl E positionCursorEnd(&cmd); @@ -392,7 +394,7 @@ int32_t shellReadCommand(TAOS *con, char *command) { return 0; } -void *shellLoopQuery(void *arg) { +void *shellThreadLoop(void *arg) { if (indicator) { getOldTerminalMode(); indicator = 0; @@ -400,9 +402,9 @@ void *shellLoopQuery(void *arg) { TAOS *con = (TAOS *)arg; - setThreadName("shellLoopQuery"); + setThreadName("shellThreadLoop"); - taosThreadCleanupPush(cleanup_handler, NULL); + taosThreadCleanupPush(shellCleanup, NULL); char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); if (command == NULL) { @@ -424,14 +426,14 @@ void *shellLoopQuery(void *arg) { } while (shellRunCommand(con, command) == 0); taosMemoryFreeClear(command); - exitShell(); + shellExit(); taosThreadCleanupPop(1); return NULL; } -void get_history_path(char *_history) { snprintf(_history, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), HISTORY_FILE); } +void shellHistoryPath(char *_history) { snprintf(_history, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), HISTORY_FILE); } void clearScreen(int ecmd_pos, int cursor_pos) { struct winsize w; @@ -527,9 +529,9 @@ void showOnScreen(Command *cmd) { fflush(stdout); } -void cleanup_handler(void *arg) { resetTerminalMode(); } +void shellCleanup(void *arg) { resetTerminalMode(); } -void exitShell() { +void shellExit() { taos_cleanup(); exit(EXIT_SUCCESS); } @@ -547,7 +549,7 @@ void *cancelHandler(void *arg) { resetTerminalMode(); printf("\nReceive ctrl+c or other signal, quit shell.\n"); - exitShell(); + shellExit(); } return NULL; @@ -574,7 +576,7 @@ int checkVersion() { } // Global configurations -SShellArguments args = { +SShellArgs args = { .host = NULL, .user = NULL, .database = NULL, @@ -604,13 +606,13 @@ void shellDumpConfig() { exit(EXIT_FAILURE); } cfgDumpCfg(pCfg, 0, 1); - exitShell(); + shellExit(); } void shellTestNetWork() { if (args.netTestRole && args.netTestRole[0] != 0) { taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType); - exitShell(); + shellExit(); } } @@ -648,18 +650,18 @@ void shellCheckServerStatus() { } } while (1); - exitShell(); + shellExit(); } void shellExecute() { TAOS *con = shellInit(&args); if (con == NULL) { - exitShell(); + shellExit(); } if (tsem_init(&cancelSem, 0, 0) != 0) { printf("failed to create cancel semphore\n"); - exitShell(); + shellExit(); } TdThread spid; @@ -673,15 +675,15 @@ void shellExecute() { shellGetGrantInfo(con); while (1) { - taosThreadCreate(&pid, NULL, shellLoopQuery, con); + taosThreadCreate(&pid, NULL, shellThreadLoop, con); taosThreadJoin(pid, NULL); } } int main(int argc, char *argv[]) { - if (!checkVersion()) exitShell(); + if (!checkVersion()) shellExit(); - shellParseArgument(argc, argv, &args); + shellParseArgs(argc, argv, &args); taos_init(); shellDumpConfig();