/* Main function. */ int main(int argc, char *argv[]) { cli_t cli; char *hostname = DEFAULT_HOST; char history_file[4096]; ybool_t interactive_mode = YTRUE; bzero(&cli, sizeof(cli_t)); cli.autocheck = YTRUE; if (argc == 2 && argv[1][0] != '-') hostname = argv[1]; if (argc == 3 && !strcmp(argv[2], "-")) interactive_mode = YFALSE; // init database connection if ((cli.finedb = finedb_create(hostname, 11138)) == NULL) { printf_color("red", "Memory error."); printf("\n"); exit(1); } if (finedb_connect(cli.finedb) != FINEDB_OK) { printf_color("red", "Unable to connect to server '%s' on port '%d'.", argv[1], 11138); printf("\n"); exit(2); } // interactive mode init if (interactive_mode) { char *home = NULL; if ((home = getenv("HOME")) != NULL) { FILE *hist; snprintf(history_file, sizeof(history_file), "%s/%s", home, HISTORY_FILE); if ((hist = fopen(history_file, "w+")) != NULL) { fclose(hist); linenoiseHistoryLoad(HISTORY_FILE); } linenoiseSetCompletionCallback(cli_completion); } } // main loop for (; ; ) { char buff[4096], *line = NULL, *pt, *cmd; if (!interactive_mode) { ssize_t bufsz, linesz = 0; while ((bufsz = read(0, buff, sizeof(buff))) > 0) { pt = (char*)malloc(linesz + bufsz + 1); memcpy(pt, line, linesz); memcpy((void*)((size_t)pt + linesz), buff, bufsz); linesz += bufsz; pt[linesz] = '\0'; if (line) free(line); line = pt; } } else { snprintf(buff, sizeof(buff), "%s > ", (cli.dbname ? cli.dbname : "default")); if ((line = linenoise(buff)) == NULL) break; } pt = line; LTRIM(pt); cmd = pt; // add command line to history linenoiseHistoryAdd(cmd); linenoiseHistorySave(history_file); // extract the command while (*pt && !IS_SPACE(*pt)) ++pt; *pt++ = '\0'; LTRIM(pt); /* command management */ if (cmd[0] == '\0') goto reloop; //continue; // local commands, no need for a running connection if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit")) exit(0); if (!strcasecmp(cmd, "help") || cmd[0] == '?') { command_help(); goto reloop; //continue; } else if (!strcasecmp(cmd, "sync")) { command_sync(&cli); goto reloop; //continue; } else if (!strcasecmp(cmd, "async")) { command_async(&cli); goto reloop; //continue; } // commands that need a running connection if (!strcasecmp(cmd, "use")) command_use(&cli, pt); else if (!strcasecmp(cmd, "get")) command_get(&cli, pt); else if (!strcasecmp(cmd, "del")) command_del(&cli, pt); else if (!strcasecmp(cmd, "put")) command_send_data(&cli, pt, YFALSE, YFALSE); else if (!strcasecmp(cmd, "add")) command_send_data(&cli, pt, YTRUE, YFALSE); else if (!strcasecmp(cmd, "update")) command_send_data(&cli, pt, YFALSE, YTRUE); else if (!strcasecmp(cmd, "inc")) command_inc(&cli, pt); else if (!strcasecmp(cmd, "dec")) command_dec(&cli, pt); else if (!strcasecmp(cmd, "start")) command_start(&cli); else if (!strcasecmp(cmd, "stop")) command_stop(&cli); #if 0 else if (!strcasecmp(cmd, "list")) command_list(&cli, pt); #endif else if (!strcasecmp(cmd, "ping")) command_ping(&cli); else if (!strcasecmp(cmd, "autocheck")) command_autocheck(&cli, pt); else { printf_color("red", "Bad command."); printf("\n"); } reloop: free(line); } return (0); }
void command_sync(Queue queue, ThreadBarrier b, CommandFunction function, void* data) { command_async(queue, function, data); command_async(queue, (CommandFunction)command_sync_function, b); threadbarrier_wait(b); }