refactor: rename shell functions

This commit is contained in:
Shengliang Guan 2022-04-20 19:44:25 +08:00
parent 0f22cf1d2e
commit 9e287ce29f
13 changed files with 135 additions and 927 deletions

View File

@ -45,7 +45,7 @@ typedef struct {
void taos_insert_call_back(void *param, TAOS_RES *tres, int code); 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_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) { static void queryDB(TAOS *taos, char *command) {
int i; int i;
@ -102,7 +102,7 @@ int main(int argc, char *argv[])
taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
if (taos == NULL) if (taos == NULL)
taos_error(taos); shellPrintError(taos);
printf("success to connect to server\n"); printf("success to connect to server\n");
@ -193,7 +193,7 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
void taos_error(TAOS *con) void shellPrintError(TAOS *con)
{ {
fprintf(stderr, "TDengine error: %s\n", taos_errstr(con)); fprintf(stderr, "TDengine error: %s\n", taos_errstr(con));
taos_close(con); taos_close(con);

View File

@ -23,8 +23,7 @@
#define UP 3 #define UP 3
#define DOWN 4 #define DOWN 4
typedef struct Command Command; typedef struct {
struct Command {
char *buffer; char *buffer;
char *command; char *command;
unsigned commandSize; unsigned commandSize;
@ -32,7 +31,7 @@ struct Command {
unsigned cursorOffset; unsigned cursorOffset;
unsigned screenOffset; unsigned screenOffset;
unsigned endOffset; unsigned endOffset;
}; } Command;
extern void backspaceChar(Command *cmd); extern void backspaceChar(Command *cmd);
extern void clearLineBefore(Command *cmd); extern void clearLineBefore(Command *cmd);

View File

@ -13,11 +13,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_SHELL_H_ #ifndef _TD_SHELL_INT_H_
#define _TD_SHELL_H_ #define _TD_SHELL_INT_H_
#include "os.h" #include "os.h"
#include "taos.h" #include "taos.h"
#include "taosdef.h" #include "taosdef.h"
@ -26,64 +25,65 @@
#define HISTORY_FILE ".taos_history" #define HISTORY_FILE ".taos_history"
#define DEFAULT_RES_SHOW_NUM 100 #define DEFAULT_RES_SHOW_NUM 100
typedef struct SShellHistory { typedef struct {
char* hist[MAX_HISTORY_SIZE]; char* hist[MAX_HISTORY_SIZE];
int hstart; int32_t hstart;
int hend; int32_t hend;
} SShellHistory; } SShellHistory;
typedef struct SShellArguments { typedef struct {
char* host; const char* host;
char* password; const char* password;
char* user; const char* user;
char* auth; const char* auth;
char* database; const char* database;
char* timezone; const char* file;
const char* cfgdir;
const char* commands;
bool is_gen_auth;
bool is_raw_time; bool is_raw_time;
bool is_use_passwd; bool is_client;
bool dump_config; bool is_server;
char file[TSDB_FILENAME_LEN]; bool is_version;
char dir[TSDB_FILENAME_LEN]; bool is_dump_config;
int threadNum; bool is_check;
int check; bool is_startup;
bool status; bool is_help;
bool verbose; uint16_t port;
char* commands; int32_t pktLen;
int abort; int32_t pktNum;
int port; int32_t abort;
int pktLen; } SShellArgs;
int pktNum;
char* pktType;
char* netTestRole;
} SShellArguments;
/**************** Function declarations ****************/ typedef struct {
extern void shellParseArgument(int argc, char* argv[], SShellArguments* arguments); SShellArgs args;
extern TAOS* shellInit(SShellArguments* args); SShellHistory history;
extern void* shellLoopQuery(void* arg); TAOS* conn;
extern void taos_error(TAOS_RES* tres, int64_t st); int64_t result;
extern int regex_match(const char* s, const char* reg, int cflags); } SShellObj;
int32_t shellReadCommand(TAOS* con, char command[]);
int32_t shellRunCommand(TAOS* con, char* command); int32_t shellParseArgs(int32_t argc, char* argv[]);
void shellRunCommandOnServer(TAOS* con, char command[]); int32_t shellInit();
void read_history(); void shellCleanup(void* arg);
void write_history(); void shellExit();
void source_file(TAOS* con, char* fptr);
void source_dir(TAOS* con, SShellArguments* args); void* shellThreadLoop(void* arg);
void get_history_path(char* history); void shellPrintError(TAOS_RES* tres, int64_t st);
void shellCheck(TAOS* con, SShellArguments* args); int32_t shellRegexMatch(const char* s, const char* reg, int32_t cflags);
void cleanup_handler(void* arg); void shellGetGrantInfo();
void exitShell(); void shellReadHistory();
int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode); void shellWriteHistory();
void shellGetGrantInfo(void *con); void shellHistoryPath(char* history);
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 PROMPT_HEADER[];
extern char CONTINUE_PROMPT[]; extern char CONTINUE_PROMPT[];
extern int prompt_size; extern int32_t prompt_size;
extern SShellHistory history; extern SShellObj shell;
extern SShellArguments args;
extern int64_t result;
#endif #endif /*_TD_SHELL_INT_H_*/

View File

@ -1,141 +0,0 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -1,127 +0,0 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -1,202 +0,0 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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);
}

View File

@ -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"; "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n";
char g_password[SHELL_MAX_PASSWORD_LEN]; 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; wordexp_t full_path;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
// for host // for host
@ -238,8 +238,8 @@ int32_t shellReadCommand(TAOS *con, char *command) {
printf("\n"); printf("\n");
taos_close(con); taos_close(con);
// write the history // write the history
write_history(); shellWriteHistory();
exitShell(); shellExit();
break; break;
case 5: // ctrl E case 5: // ctrl E
positionCursorEnd(&cmd); positionCursorEnd(&cmd);
@ -355,7 +355,7 @@ int32_t shellReadCommand(TAOS *con, char *command) {
return 0; return 0;
} }
void *shellLoopQuery(void *arg) { void *shellThreadLoop(void *arg) {
if (indicator) { if (indicator) {
getOldTerminalMode(); getOldTerminalMode();
indicator = 0; indicator = 0;
@ -363,9 +363,9 @@ void *shellLoopQuery(void *arg) {
TAOS *con = (TAOS *)arg; TAOS *con = (TAOS *)arg;
setThreadName("shellLoopQuery"); setThreadName("shellThreadLoop");
taosThreadCleanupPush(cleanup_handler, NULL); taosThreadCleanupPush(shellCleanup, NULL);
char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); char *command = taosMemoryMalloc(MAX_COMMAND_SIZE);
if (command == NULL){ if (command == NULL){
@ -387,14 +387,14 @@ void *shellLoopQuery(void *arg) {
} while (shellRunCommand(con, command) == 0); } while (shellRunCommand(con, command) == 0);
taosMemoryFreeClear(command); taosMemoryFreeClear(command);
exitShell(); shellExit();
taosThreadCleanupPop(1); taosThreadCleanupPop(1);
return NULL; 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) { void clearScreen(int ecmd_pos, int cursor_pos) {
struct winsize w; struct winsize w;
@ -490,9 +490,9 @@ void showOnScreen(Command *cmd) {
fflush(stdout); fflush(stdout);
} }
void cleanup_handler(void *arg) { resetTerminalMode(); } void shellCleanup(void *arg) { resetTerminalMode(); }
void exitShell() { void shellExit() {
resetTerminalMode(); resetTerminalMode();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -1,279 +0,0 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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);
}

View File

@ -70,7 +70,7 @@ void printHelp() {
char g_password[SHELL_MAX_PASSWORD_LEN]; 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 (int i = 1; i < argc; i++) {
// for host // for host
if (strcmp(argv[i], "-h") == 0) { if (strcmp(argv[i], "-h") == 0) {
@ -231,7 +231,7 @@ void shellPrintContinuePrompt() { printf("%s", CONTINUE_PROMPT); }
void shellPrintPrompt() { printf("%s", PROMPT_HEADER); } void shellPrintPrompt() { printf("%s", PROMPT_HEADER); }
void updateBuffer(Command *cmd) { 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); strcat(cmd->buffer, cmd->command);
memset(cmd->command, 0, MAX_COMMAND_SIZE); memset(cmd->command, 0, MAX_COMMAND_SIZE);
@ -246,7 +246,7 @@ int isReadyGo(Command *cmd) {
char *reg_str = char *reg_str =
"(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^" "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^"
"\\s*clear\\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); taosMemoryFree(total);
return 1; return 1;
} }
@ -299,7 +299,7 @@ int32_t shellReadCommand(TAOS *con, char command[]) {
return 0; return 0;
} }
void *shellLoopQuery(void *arg) { void *shellThreadLoop(void *arg) {
TAOS *con = (TAOS *)arg; TAOS *con = (TAOS *)arg;
char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); char *command = taosMemoryMalloc(MAX_COMMAND_SIZE);
if (command == NULL) return NULL; if (command == NULL) return NULL;
@ -320,6 +320,6 @@ void *shellLoopQuery(void *arg) {
return NULL; 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); }

View File

@ -1,29 +0,0 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -214,7 +214,7 @@ void positionCursor(int step, int direction) {
void updateBuffer(Command *cmd) { void updateBuffer(Command *cmd) {
assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); 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); strcat(cmd->buffer, cmd->command);
cmd->bufferSize += cmd->commandSize; cmd->bufferSize += cmd->commandSize;
@ -236,7 +236,7 @@ int isReadyGo(Command *cmd) {
char *reg_str = char *reg_str =
"(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^" "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^"
"\\s*clear\\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); taosMemoryFree(total);
return 1; return 1;
} }

View File

@ -68,7 +68,7 @@ extern TAOS *taos_connect_auth(const char *ip, const char *user, const char *a
/* /*
* FUNCTION: Initialize the shell. * FUNCTION: Initialize the shell.
*/ */
TAOS *shellInit(SShellArguments *_args) { TAOS *shellInit(SShellArgs *_args) {
printf("\n"); printf("\n");
if (!_args->is_use_passwd) { if (!_args->is_use_passwd) {
#ifdef TD_WINDOWS #ifdef TD_WINDOWS
@ -108,7 +108,7 @@ TAOS *shellInit(SShellArguments *_args) {
} }
/* Read history TODO : release resources here*/ /* Read history TODO : release resources here*/
read_history(); shellReadHistory();
// Check if it is temperory run // Check if it is temperory run
if (_args->commands != NULL || _args->file[0] != 0) { if (_args->commands != NULL || _args->file[0] != 0) {
@ -118,29 +118,14 @@ TAOS *shellInit(SShellArguments *_args) {
} }
if (_args->file[0] != 0) { if (_args->file[0] != 0) {
source_file(con, _args->file); shellSourceFile(con, _args->file);
} }
taos_close(con); taos_close(con);
write_history(); shellWriteHistory();
exit(EXIT_SUCCESS); 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; return con;
} }
@ -161,22 +146,22 @@ static int32_t shellRunSingleCommand(TAOS *con, char *command) {
} }
// Analyse the 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); taos_close(con);
write_history(); shellWriteHistory();
#ifdef WINDOWS #ifdef WINDOWS
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
#endif #endif
return -1; 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. // If clear the screen.
system("clear"); system("clear");
return 0; 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)) { REG_EXTENDED | REG_ICASE)) {
strtok(command, " \t"); strtok(command, " \t");
strtok(NULL, " \t"); strtok(NULL, " \t");
@ -189,17 +174,17 @@ static int32_t shellRunSingleCommand(TAOS *con, char *command) {
return 0; 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. */ /* If source file. */
char *c_ptr = strtok(command, " ;"); char *c_ptr = strtok(command, " ;");
assert(c_ptr != NULL); assert(c_ptr != NULL);
c_ptr = strtok(NULL, " ;"); c_ptr = strtok(NULL, " ;");
assert(c_ptr != NULL); assert(c_ptr != NULL);
source_file(con, c_ptr); shellSourceFile(con, c_ptr);
return 0; return 0;
} }
shellRunCommandOnServer(con, command); shellRunCommandImp(con, command);
return 0; return 0;
} }
@ -294,7 +279,7 @@ void freeResultWithRid(int64_t rid) {
#endif #endif
} }
void shellRunCommandOnServer(TAOS *con, char command[]) { void shellRunCommandImp(TAOS *con, char command[]) {
int64_t st, et; int64_t st, et;
wordexp_t full_path; wordexp_t full_path;
char *sptr = NULL; char *sptr = NULL;
@ -330,13 +315,13 @@ void shellRunCommandOnServer(TAOS *con, char command[]) {
TAOS_RES *pSql = taos_query(con, command); TAOS_RES *pSql = taos_query(con, command);
if (taos_errno(pSql)) { if (taos_errno(pSql)) {
taos_error(pSql, st); shellPrintError(pSql, st);
return; return;
} }
int64_t oresult = atomic_load_64(&result); 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"); fprintf(stdout, "Database changed.\n\n");
fflush(stdout); fflush(stdout);
@ -383,14 +368,14 @@ void shellRunCommandOnServer(TAOS *con, char command[]) {
} }
/* Function to do regular expression check */ /* 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; regex_t regex;
char msgbuf[100] = {0}; char msgbuf[100] = {0};
/* Compile regular expression */ /* Compile regular expression */
if (regcomp(&regex, reg, cflags) != 0) { if (regcomp(&regex, reg, cflags) != 0) {
fprintf(stderr, "Fail to compile regex"); fprintf(stderr, "Fail to compile regex");
exitShell(); shellExit();
} }
/* Execute regular expression */ /* Execute regular expression */
@ -405,7 +390,7 @@ int regex_match(const char *s, const char *reg, int cflags) {
regerror(reti, &regex, msgbuf, sizeof(msgbuf)); regerror(reti, &regex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf); fprintf(stderr, "Regex match failed: %s\n", msgbuf);
regfree(&regex); regfree(&regex);
exitShell(); shellExit();
} }
return 0; return 0;
@ -695,7 +680,7 @@ bool isSelectQuery(TAOS_RES *tres) {
#if 0 #if 0
char *sql = tscGetSqlStr(tres); 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; return true;
} }
#endif #endif
@ -900,7 +885,7 @@ int shellDumpResult(TAOS_RES *tres, char *fname, int *error_no, bool vertical) {
return numOfRows; return numOfRows;
} }
void read_history() { void shellReadHistory() {
// Initialize history // Initialize history
memset(history.hist, 0, sizeof(char *) * MAX_HISTORY_SIZE); memset(history.hist, 0, sizeof(char *) * MAX_HISTORY_SIZE);
history.hstart = 0; history.hstart = 0;
@ -909,7 +894,7 @@ void read_history() {
int read_size = 0; int read_size = 0;
char f_history[TSDB_FILENAME_LEN]; char f_history[TSDB_FILENAME_LEN];
get_history_path(f_history); shellHistoryPath(f_history);
// FILE *f = fopen(f_history, "r"); // FILE *f = fopen(f_history, "r");
TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_READ | TD_FILE_STREAM); TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_READ | TD_FILE_STREAM);
@ -937,9 +922,9 @@ void read_history() {
taosCloseFile(&pFile); taosCloseFile(&pFile);
} }
void write_history() { void shellWriteHistory() {
char f_history[TSDB_FILENAME_LEN]; char f_history[TSDB_FILENAME_LEN];
get_history_path(f_history); shellHistoryPath(f_history);
// FILE *f = fopen(f_history, "w"); // FILE *f = fopen(f_history, "w");
TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM); 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); taosCloseFile(&pFile);
} }
void taos_error(TAOS_RES *tres, int64_t st) { void shellPrintError(TAOS_RES *tres, int64_t st) {
int64_t et = taosGetTimestampUs(); int64_t et = taosGetTimestampUs();
atomic_store_ptr(&result, 0); atomic_store_ptr(&result, 0);
fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6); 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) { int isCommentLine(char *line) {
if (line == NULL) return 1; 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; wordexp_t full_path;
int read_len = 0; int read_len = 0;
char *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1); char *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1);

View File

@ -64,7 +64,7 @@ static struct argp_option options[] = {
static error_t parse_opt(int key, char *arg, struct argp_state *state) { static error_t parse_opt(int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we /* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */ know is a pointer to our arguments structure. */
SShellArguments *arguments = state->input; SShellArgs *arguments = state->input;
wordexp_t full_path; wordexp_t full_path;
switch (key) { switch (key) {
@ -188,7 +188,7 @@ char LINUXCLIENT_VERSION[] =
"Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n";
char g_password[SHELL_MAX_PASSWORD_LEN]; 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++) { for (int i = 1; i < argc; i++) {
if ((strncmp(argv[i], "-p", 2) == 0) || (strncmp(argv[i], "--password", 10) == 0)) { if ((strncmp(argv[i], "-p", 2) == 0) || (strncmp(argv[i], "--password", 10) == 0)) {
printf(LINUXCLIENT_VERSION, tsOsName, taos_get_client_info()); 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}; static char verType[32] = {0};
sprintf(verType, "version: %s\n", version); sprintf(verType, "version: %s\n", version);
@ -232,6 +232,8 @@ void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) {
abort(); abort();
#endif #endif
} }
return 0;
} }
int32_t shellReadCommand(TAOS *con, char *command) { int32_t shellReadCommand(TAOS *con, char *command) {
@ -275,8 +277,8 @@ int32_t shellReadCommand(TAOS *con, char *command) {
printf("\n"); printf("\n");
taos_close(con); taos_close(con);
// write the history // write the history
write_history(); shellWriteHistory();
exitShell(); shellExit();
break; break;
case 5: // ctrl E case 5: // ctrl E
positionCursorEnd(&cmd); positionCursorEnd(&cmd);
@ -392,7 +394,7 @@ int32_t shellReadCommand(TAOS *con, char *command) {
return 0; return 0;
} }
void *shellLoopQuery(void *arg) { void *shellThreadLoop(void *arg) {
if (indicator) { if (indicator) {
getOldTerminalMode(); getOldTerminalMode();
indicator = 0; indicator = 0;
@ -400,9 +402,9 @@ void *shellLoopQuery(void *arg) {
TAOS *con = (TAOS *)arg; TAOS *con = (TAOS *)arg;
setThreadName("shellLoopQuery"); setThreadName("shellThreadLoop");
taosThreadCleanupPush(cleanup_handler, NULL); taosThreadCleanupPush(shellCleanup, NULL);
char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); char *command = taosMemoryMalloc(MAX_COMMAND_SIZE);
if (command == NULL) { if (command == NULL) {
@ -424,14 +426,14 @@ void *shellLoopQuery(void *arg) {
} while (shellRunCommand(con, command) == 0); } while (shellRunCommand(con, command) == 0);
taosMemoryFreeClear(command); taosMemoryFreeClear(command);
exitShell(); shellExit();
taosThreadCleanupPop(1); taosThreadCleanupPop(1);
return NULL; 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) { void clearScreen(int ecmd_pos, int cursor_pos) {
struct winsize w; struct winsize w;
@ -527,9 +529,9 @@ void showOnScreen(Command *cmd) {
fflush(stdout); fflush(stdout);
} }
void cleanup_handler(void *arg) { resetTerminalMode(); } void shellCleanup(void *arg) { resetTerminalMode(); }
void exitShell() { void shellExit() {
taos_cleanup(); taos_cleanup();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -547,7 +549,7 @@ void *cancelHandler(void *arg) {
resetTerminalMode(); resetTerminalMode();
printf("\nReceive ctrl+c or other signal, quit shell.\n"); printf("\nReceive ctrl+c or other signal, quit shell.\n");
exitShell(); shellExit();
} }
return NULL; return NULL;
@ -574,7 +576,7 @@ int checkVersion() {
} }
// Global configurations // Global configurations
SShellArguments args = { SShellArgs args = {
.host = NULL, .host = NULL,
.user = NULL, .user = NULL,
.database = NULL, .database = NULL,
@ -604,13 +606,13 @@ void shellDumpConfig() {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
cfgDumpCfg(pCfg, 0, 1); cfgDumpCfg(pCfg, 0, 1);
exitShell(); shellExit();
} }
void shellTestNetWork() { void shellTestNetWork() {
if (args.netTestRole && args.netTestRole[0] != 0) { if (args.netTestRole && args.netTestRole[0] != 0) {
taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType); taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType);
exitShell(); shellExit();
} }
} }
@ -648,18 +650,18 @@ void shellCheckServerStatus() {
} }
} while (1); } while (1);
exitShell(); shellExit();
} }
void shellExecute() { void shellExecute() {
TAOS *con = shellInit(&args); TAOS *con = shellInit(&args);
if (con == NULL) { if (con == NULL) {
exitShell(); shellExit();
} }
if (tsem_init(&cancelSem, 0, 0) != 0) { if (tsem_init(&cancelSem, 0, 0) != 0) {
printf("failed to create cancel semphore\n"); printf("failed to create cancel semphore\n");
exitShell(); shellExit();
} }
TdThread spid; TdThread spid;
@ -673,15 +675,15 @@ void shellExecute() {
shellGetGrantInfo(con); shellGetGrantInfo(con);
while (1) { while (1) {
taosThreadCreate(&pid, NULL, shellLoopQuery, con); taosThreadCreate(&pid, NULL, shellThreadLoop, con);
taosThreadJoin(pid, NULL); taosThreadJoin(pid, NULL);
} }
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (!checkVersion()) exitShell(); if (!checkVersion()) shellExit();
shellParseArgument(argc, argv, &args); shellParseArgs(argc, argv, &args);
taos_init(); taos_init();
shellDumpConfig(); shellDumpConfig();