Skip to content

Commit 117c46d

Browse files
committed
Add input command for unix
1 parent 0ef015b commit 117c46d

File tree

7 files changed

+92
-31
lines changed

7 files changed

+92
-31
lines changed

stmhal/input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
3838
if (n_args == 1) {
39-
mp_obj_print(args[0], PRINT_REPR);
39+
mp_obj_print(args[0], PRINT_STR);
4040
}
4141
vstr_t line;
4242
vstr_init(&line, 16);

unix/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ endif
6565
SRC_C = \
6666
main.c \
6767
gccollect.c \
68+
input.c \
6869
file.c \
6970
modsocket.c \
7071
$(SRC_MOD)

unix/input.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of the Micro Python 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 <stdint.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
31+
#include "mpconfig.h"
32+
#include "nlr.h"
33+
#include "misc.h"
34+
#include "qstr.h"
35+
#include "obj.h"
36+
#include "input.h"
37+
38+
#if MICROPY_USE_READLINE
39+
#include <readline/readline.h>
40+
#include <readline/history.h>
41+
#endif
42+
43+
#define CTRL_D '\x04'
44+
45+
char *prompt(char *p) {
46+
#if MICROPY_USE_READLINE
47+
char *line = readline(p);
48+
if (line) {
49+
add_history(line);
50+
}
51+
#else
52+
static char buf[256];
53+
fputs(p, stdout);
54+
char *s = fgets(buf, sizeof(buf), stdin);
55+
if (!s) {
56+
return NULL;
57+
}
58+
int l = strlen(buf);
59+
if (buf[l - 1] == '\n') {
60+
buf[l - 1] = 0;
61+
} else {
62+
l++;
63+
}
64+
char *line = malloc(l);
65+
memcpy(line, buf, l);
66+
#endif
67+
return line;
68+
}
69+
70+
STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
71+
if (n_args == 1) {
72+
mp_obj_print(args[0], PRINT_STR);
73+
}
74+
75+
char *line = prompt("");
76+
if (line == NULL) {
77+
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
78+
}
79+
mp_obj_t o = mp_obj_new_str((const byte*)line, strlen(line), false);
80+
free(line);
81+
return o;
82+
}
83+
84+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

unix/input.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
char *prompt(char *p);
2+

unix/main.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@
4949
#include "repl.h"
5050
#include "gc.h"
5151
#include "genhdr/py-version.h"
52-
53-
#if MICROPY_USE_READLINE
54-
#include <readline/readline.h>
55-
#include <readline/history.h>
56-
#endif
52+
#include "input.h"
5753

5854
// Command line options, with their defaults
5955
bool compile_only = false;
@@ -143,31 +139,6 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
143139
return s;
144140
}
145141

146-
STATIC char *prompt(char *p) {
147-
#if MICROPY_USE_READLINE
148-
char *line = readline(p);
149-
if (line) {
150-
add_history(line);
151-
}
152-
#else
153-
static char buf[256];
154-
fputs(p, stdout);
155-
char *s = fgets(buf, sizeof(buf), stdin);
156-
if (!s) {
157-
return NULL;
158-
}
159-
int l = strlen(buf);
160-
if (buf[l - 1] == '\n') {
161-
buf[l - 1] = 0;
162-
} else {
163-
l++;
164-
}
165-
char *line = malloc(l);
166-
memcpy(line, buf, l);
167-
#endif
168-
return line;
169-
}
170-
171142
STATIC void do_repl(void) {
172143
printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; UNIX version\n");
173144

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ typedef unsigned int machine_uint_t; // must be pointer size
7272
typedef void *machine_ptr_t; // must be of pointer size
7373
typedef const void *machine_const_ptr_t; // must be of pointer size
7474

75+
extern const struct _mp_obj_fun_native_t mp_builtin_input_obj;
7576
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
7677
#define MICROPY_EXTRA_BUILTINS \
78+
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
7779
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },

unix/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Q(callback)
4343
Q(func)
4444
Q(var)
4545

46+
Q(input)
4647
Q(time)
4748
Q(clock)
4849
Q(sleep)

0 commit comments

Comments
 (0)