| 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #include "py/mpstate.h" |
| 34 | #include "py/mphal.h" |
| 35 | #include "input.h" |
| 36 | |
| 37 | #if MICROPY_USE_READLINE == 1 |
| 38 | #include "lib/mp-readline/readline.h" |
| 39 | #endif |
| 40 | |
| 41 | #if MICROPY_USE_READLINE == 0 |
| 42 | char *prompt(char *p) { |
| 43 | // simple read string |
| 44 | static char buf[256]; |
| 45 | fputs(p, stdout); |
| 46 | char *s = fgets(buf, sizeof(buf), stdin); |
| 47 | if (!s) { |
| 48 | return NULL; |
| 49 | } |
| 50 | int l = strlen(buf); |
| 51 | if (buf[l - 1] == '\n') { |
| 52 | buf[l - 1] = 0; |
| 53 | } else { |
| 54 | l++; |
| 55 | } |
| 56 | char *line = malloc(l); |
| 57 | memcpy(line, buf, l); |
| 58 | return line; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | void prompt_read_history(void) { |
| 63 | #if MICROPY_USE_READLINE_HISTORY |
| 64 | #if MICROPY_USE_READLINE == 1 |
| 65 | readline_init0(); // will clear history pointers |
| 66 | char *home = getenv("HOME" ); |
| 67 | if (home != NULL) { |
| 68 | vstr_t vstr; |
| 69 | vstr_init(&vstr, 50); |
| 70 | vstr_printf(&vstr, "%s/.micropython.history" , home); |
| 71 | int fd = open(vstr_null_terminated_str(&vstr), O_RDONLY); |
| 72 | if (fd != -1) { |
| 73 | vstr_reset(&vstr); |
| 74 | for (;;) { |
| 75 | char c; |
| 76 | int sz = read(fd, &c, 1); |
| 77 | if (sz < 0) { |
| 78 | if (errno == EINTR) { |
| 79 | continue; |
| 80 | } |
| 81 | break; |
| 82 | } |
| 83 | if (sz == 0 || c == '\n') { |
| 84 | readline_push_history(vstr_null_terminated_str(&vstr)); |
| 85 | if (sz == 0) { |
| 86 | break; |
| 87 | } |
| 88 | vstr_reset(&vstr); |
| 89 | } else { |
| 90 | vstr_add_byte(&vstr, c); |
| 91 | } |
| 92 | } |
| 93 | close(fd); |
| 94 | } |
| 95 | vstr_clear(&vstr); |
| 96 | } |
| 97 | #endif |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | void prompt_write_history(void) { |
| 102 | #if MICROPY_USE_READLINE_HISTORY |
| 103 | #if MICROPY_USE_READLINE == 1 |
| 104 | char *home = getenv("HOME" ); |
| 105 | if (home != NULL) { |
| 106 | vstr_t vstr; |
| 107 | vstr_init(&vstr, 50); |
| 108 | vstr_printf(&vstr, "%s/.micropython.history" , home); |
| 109 | int fd = open(vstr_null_terminated_str(&vstr), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 110 | if (fd != -1) { |
| 111 | for (int i = MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist)) - 1; i >= 0; i--) { |
| 112 | const char *line = MP_STATE_PORT(readline_hist)[i]; |
| 113 | if (line != NULL) { |
| 114 | while (write(fd, line, strlen(line)) == -1 && errno == EINTR) { |
| 115 | } |
| 116 | while (write(fd, "\n" , 1) == -1 && errno == EINTR) { |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | close(fd); |
| 121 | } |
| 122 | } |
| 123 | #endif |
| 124 | #endif |
| 125 | } |
| 126 | |