input.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <fcntl.h>
  30. #include "py/mpstate.h"
  31. #include "py/mphal.h"
  32. #include "input.h"
  33. #if MICROPY_USE_READLINE == 1
  34. #include "lib/mp-readline/readline.h"
  35. #endif
  36. #if MICROPY_USE_READLINE == 0
  37. char *prompt(char *p) {
  38. // simple read string
  39. static char buf[256];
  40. fputs(p, stdout);
  41. char *s = fgets(buf, sizeof(buf), stdin);
  42. if (!s) {
  43. return NULL;
  44. }
  45. int l = strlen(buf);
  46. if (buf[l - 1] == '\n') {
  47. buf[l - 1] = 0;
  48. } else {
  49. l++;
  50. }
  51. char *line = malloc(l);
  52. memcpy(line, buf, l);
  53. return line;
  54. }
  55. #endif
  56. void prompt_read_history(void) {
  57. #if MICROPY_USE_READLINE_HISTORY
  58. #if MICROPY_USE_READLINE == 1
  59. readline_init0(); // will clear history pointers
  60. char *home = getenv("HOME");
  61. if (home != NULL) {
  62. vstr_t vstr;
  63. vstr_init(&vstr, 50);
  64. vstr_printf(&vstr, "%s/.micropython.history", home);
  65. int fd = open(vstr_null_terminated_str(&vstr), O_RDONLY);
  66. if (fd != -1) {
  67. vstr_reset(&vstr);
  68. for (;;) {
  69. char c;
  70. int sz = read(fd, &c, 1);
  71. if (sz < 0) {
  72. break;
  73. }
  74. if (sz == 0 || c == '\n') {
  75. readline_push_history(vstr_null_terminated_str(&vstr));
  76. if (sz == 0) {
  77. break;
  78. }
  79. vstr_reset(&vstr);
  80. } else {
  81. vstr_add_byte(&vstr, c);
  82. }
  83. }
  84. close(fd);
  85. }
  86. vstr_clear(&vstr);
  87. }
  88. #endif
  89. #endif
  90. }
  91. void prompt_write_history(void) {
  92. #if MICROPY_USE_READLINE_HISTORY
  93. #if MICROPY_USE_READLINE == 1
  94. char *home = getenv("HOME");
  95. if (home != NULL) {
  96. vstr_t vstr;
  97. vstr_init(&vstr, 50);
  98. vstr_printf(&vstr, "%s/.micropython.history", home);
  99. int fd = open(vstr_null_terminated_str(&vstr), O_CREAT | O_TRUNC | O_WRONLY, 0644);
  100. if (fd != -1) {
  101. for (int i = MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist)) - 1; i >= 0; i--) {
  102. const char *line = MP_STATE_PORT(readline_hist)[i];
  103. if (line != NULL) {
  104. int res;
  105. res = write(fd, line, strlen(line));
  106. res = write(fd, "\n", 1);
  107. (void)res;
  108. }
  109. }
  110. close(fd);
  111. }
  112. }
  113. #endif
  114. #endif
  115. }