shellMain.c 4.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
slguan 已提交
16
#include "os.h"
H
hzcheng 已提交
17
#include "shell.h"
S
Shengliang Guan 已提交
18
#include "tglobal.h"
H
hzcheng 已提交
19 20

pthread_t pid;
D
dapan1121 已提交
21
static tsem_t cancelSem;
H
hzcheng 已提交
22

S
TD-1207  
Shengliang Guan 已提交
23
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) {
D
dapan1121 已提交
24 25 26 27
  tsem_post(&cancelSem);
}

void *cancelHandler(void *arg) {
28 29
  setThreadName("cancelHandler");

S
Shengliang Guan 已提交
30
  while (1) {
D
dapan1121 已提交
31 32 33 34 35
    if (tsem_wait(&cancelSem) != 0) {
      taosMsleep(10);
      continue;
    }

S
slguan 已提交
36
#ifdef LINUX
S
Shengliang Guan 已提交
37
#if 0
D
dapan1121 已提交
38 39 40 41
    int64_t rid = atomic_val_compare_exchange_64(&result, result, 0);
    SSqlObj* pSql = taosAcquireRef(tscObjRef, rid);
    taos_stop_query(pSql);
    taosReleaseRef(tscObjRef, rid);
S
Shengliang Guan 已提交
42
#endif    
S
slguan 已提交
43
#else
X
Xiaoyu Wang 已提交
44
    reset_terminal_mode();
D
dapan1121 已提交
45 46
    printf("\nReceive ctrl+c or other signal, quit shell.\n");
    exit(0);
S
slguan 已提交
47
#endif
X
Xiaoyu Wang 已提交
48
    reset_terminal_mode();
S
Shengliang Guan 已提交
49 50
    printf("\nReceive ctrl+c or other signal, quit shell.\n");
    exit(0);
D
dapan1121 已提交
51
  }
S
Shengliang Guan 已提交
52

D
dapan1121 已提交
53
  return NULL;
H
hzcheng 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

int checkVersion() {
  if (sizeof(int8_t) != 1) {
    printf("taos int8 size is %d(!= 1)", (int)sizeof(int8_t));
    return 0;
  }
  if (sizeof(int16_t) != 2) {
    printf("taos int16 size is %d(!= 2)", (int)sizeof(int16_t));
    return 0;
  }
  if (sizeof(int32_t) != 4) {
    printf("taos int32 size is %d(!= 4)", (int)sizeof(int32_t));
    return 0;
  }
  if (sizeof(int64_t) != 8) {
    printf("taos int64 size is %d(!= 8)", (int)sizeof(int64_t));
    return 0;
  }
  return 1;
}

// Global configurations
S
Shengliang Guan 已提交
77
SShellArguments args = {.host = NULL,
78
#ifndef TD_WINDOWS
S
Shengliang Guan 已提交
79
                        .password = NULL,
80
#endif
S
Shengliang Guan 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
                        .user = NULL,
                        .database = NULL,
                        .timezone = NULL,
                        .is_raw_time = false,
                        .is_use_passwd = false,
                        .dump_config = false,
                        .file = "\0",
                        .dir = "\0",
                        .threadNum = 5,
                        .commands = NULL,
                        .pktLen = 1000,
                        .pktNum = 100,
                        .pktType = "TCP",
                        .netTestRole = NULL};
H
hzcheng 已提交
95 96 97 98

/*
 * Main function.
 */
S
Shengliang Guan 已提交
99
int main(int argc, char *argv[]) {
H
hzcheng 已提交
100
  /*setlocale(LC_ALL, "en_US.UTF-8"); */
S
slguan 已提交
101

H
hzcheng 已提交
102 103 104 105 106 107
  if (!checkVersion()) {
    exit(EXIT_FAILURE);
  }

  shellParseArgument(argc, argv, &args);

S
Shengliang Guan 已提交
108
#if 0
109 110 111 112
  if (args.dump_config) {
    taosInitGlobalCfg();
    taosReadGlobalLogCfg();

113
    if (taosReadGlobalCfg() ! =0) {
114 115 116 117 118 119 120 121
      printf("TDengine read global config failed");
      exit(EXIT_FAILURE);
    }

    taosDumpGlobalCfg();
    exit(0);
  }

H
Hui Li 已提交
122
  if (args.netTestRole && args.netTestRole[0] != 0) {
D
fix bug  
dapan1121 已提交
123 124 125 126
    if (taos_init()) {
      printf("Failed to init taos");
      exit(EXIT_FAILURE);
    }
127
    taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType);
H
Hui Li 已提交
128 129
    exit(0);
  }
S
Shengliang Guan 已提交
130
#endif
H
Hui Li 已提交
131

H
hzcheng 已提交
132
  /* Initialize the shell */
S
Shengliang Guan 已提交
133
  TAOS *con = shellInit(&args);
H
hzcheng 已提交
134 135 136 137
  if (con == NULL) {
    exit(EXIT_FAILURE);
  }

D
dapan1121 已提交
138 139 140 141 142 143 144 145
  if (tsem_init(&cancelSem, 0, 0) != 0) {
    printf("failed to create cancel semphore\n");
    exit(EXIT_FAILURE);
  }

  pthread_t spid;
  pthread_create(&spid, NULL, cancelHandler, NULL);

146
  /* Interrupt handler. */
S
TD-1207  
Shengliang Guan 已提交
147 148 149 150
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
  taosSetSignal(SIGABRT, shellQueryInterruptHandler);
H
hzcheng 已提交
151

S
slguan 已提交
152 153 154
  /* Get grant information */
  shellGetGrantInfo(con);

H
hzcheng 已提交
155 156 157 158 159 160
  /* Loop to query the input. */
  while (1) {
    pthread_create(&pid, NULL, shellLoopQuery, con);
    pthread_join(pid, NULL);
  }
}