void run_console() { initialize_console(); /* Register commands */ esp_console_register_help_command(); const esp_console_cmd_t cmd1 = { .command = "motortest", .help = "Test the motors", .hint = NULL, .func = &motor_test, .argtable = nullptr }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd1)); const esp_console_cmd_t cmd2 = { .command = "i2cscan", .help = "Scan for I2C devices", .hint = NULL, .func = &i2c_scan, .argtable = nullptr }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd2)); /* Prompt to be printed before each line. * This can be customized, made dynamic, etc. */ const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR; printf("\n" "This is an example of ESP-IDF console component.\n" "Type 'help' to get the list of commands.\n" "Use UP/DOWN arrows to navigate through command history.\n" "Press TAB when typing command name to auto-complete.\n"); /* Figure out if the terminal supports escape sequences */ int probe_status = linenoiseProbe(); if (probe_status) { /* zero indicates success */ printf("\n" "Your terminal application does not support escape sequences.\n" "Line editing and history features are disabled.\n" "On Windows, try using Putty instead.\n"); linenoiseSetDumbMode(1); #if CONFIG_LOG_COLORS /* Since the terminal doesn't support escape sequences, * don't use color codes in the prompt. */ prompt = "esp32> "; #endif //CONFIG_LOG_COLORS } /* Main loop */ while(true) { /* Get a line using linenoise. * The line is returned when ENTER is pressed. */ char* line = linenoise(prompt); if (line == NULL) { /* Ignore empty lines */ continue; } /* Add the command to the history */ linenoiseHistoryAdd(line); #if CONFIG_STORE_HISTORY /* Save command history to filesystem */ linenoiseHistorySave(HISTORY_PATH); #endif /* Try to run the command */ int ret; esp_err_t err = esp_console_run(line, &ret); if (err == ESP_ERR_NOT_FOUND) { printf("Unrecognized command\n"); } else if (err == ESP_ERR_INVALID_ARG) { // command was empty } else if (err == ESP_OK && ret != ESP_OK) { printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err)); } else if (err != ESP_OK) { printf("Internal error: %s\n", esp_err_to_name(err)); } /* linenoise allocates line buffer on the heap, so need to free it */ linenoiseFree(line); } }
int main() { char *o, *p, *line; queue *q; double *r, *s; double *pi = malloc(sizeof(double)); double *e = malloc(sizeof(double)); double *ans = malloc(sizeof(double)); *pi = 3.1415926535; *e = 2.7182818285; *ans = 0; hm = hashmap_new(); hashmap_put(hm, "pi", pi); hashmap_put(hm, "e", e); hashmap_put(hm, "ans", ans); /* read input using linenoise */ while((line = linenoise("> ")) != NULL) { /* add the line to history */ linenoiseHistoryAdd(line); /* check if there is an equals sign */ for (p = line; (*p != '\0') && (*p != '='); p++); if (*p == '\0') p = line; else { /* equals sign present */ /* check for spaces */ for (o = line; (*o != ' ') && (*o != '='); o++); *o = '\0'; p++; /* skip equals sign when parsing equation */ } q = syard_run(p); if (q == NULL) continue; #ifdef __DEBUG queue_foreach(q, testfunc, NULL); printf("\n"); #endif r = rpn_calc(q, variable_resolver, func_wl); if (r == NULL) continue; printf("= %g\n", *r); /* store ans */ *ans = *r; if (p != line) { /* equals sign present */ if ((s = hashmap_get(hm, line)) != NULL) { /* existing value, just overwrite it */ *s = *r; free(r); } else { /* new value */ hashmap_put(hm, line, r); } } else { /* no equals sign, so we're done with the number now */ free(r); } /* free storage and stuff */ linenoiseFree(line); } hashmap_iterate(hm, hm_cleaner, NULL); hashmap_destroy(hm); return 0; }
static int preadfd(void) { int nr; char *buf = parsefile->buf; parsefile->nextc = buf; const char* prompt = NULL; retry: #ifdef USE_LINENOISE if (parsefile->fd == 0 && iflag) { if (pending_line == NULL) { // linenoise stashs the prompt buffer away for // the duration of its edit cycle. Because // some edit functionality (in particular, tab // completion allocates the parts of PATH from // dash's stack-based allocator), we need to // properly save the string and then free it, // or it will be clobbered. prompt = savestr(getprompt(NULL)); pending_line = linenoise(prompt); if (pending_line) { pending_line_index = 0u; pending_line_length = strlen(pending_line); pending_line[pending_line_length] = '\n'; pending_line_length += 1; } } if (pending_line == NULL) nr = 0; else { nr = pending_line_length - pending_line_index; if (nr > IBUFSIZ - 1) nr = IBUFSIZ - 1; memcpy(buf, pending_line + pending_line_index, nr); pending_line_index += nr; if (pending_line_index == pending_line_length) { linenoiseFree(pending_line); free(prompt); pending_line = NULL; pending_line_index = 0u; pending_line_length = 0u; } } } else #endif nr = read(parsefile->fd, buf, IBUFSIZ - 1); if (nr < 0) { if (errno == EINTR) goto retry; if (parsefile->fd == 0 && errno == EWOULDBLOCK) { int flags = fcntl(0, F_GETFL, 0); if (flags >= 0 && flags & O_NONBLOCK) { flags &=~ O_NONBLOCK; if (fcntl(0, F_SETFL, flags) >= 0) { out2str("sh: turning off NDELAY mode\n"); goto retry; } } } } return nr; }