Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
c35e5343
Commit
c35e5343
authored
May 07, 2014
by
Damien George
Browse files
Merge pull request #582 from dhylands/unix-input
Add input command for unix
parents
1dd46faf
117c46d9
Changes
7
Hide whitespace changes
Inline
Side-by-side
stmhal/input.c
View file @
c35e5343
...
...
@@ -36,7 +36,7 @@
STATIC
mp_obj_t
mp_builtin_input
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
if
(
n_args
==
1
)
{
mp_obj_print
(
args
[
0
],
PRINT_
REP
R
);
mp_obj_print
(
args
[
0
],
PRINT_
ST
R
);
}
vstr_t
line
;
vstr_init
(
&
line
,
16
);
...
...
unix/Makefile
View file @
c35e5343
...
...
@@ -65,6 +65,7 @@ endif
SRC_C
=
\
main.c
\
gccollect.c
\
input.c
\
file.c
\
modsocket.c
\
$(SRC_MOD)
...
...
unix/input.c
0 → 100644
View file @
c35e5343
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include
<stdint.h>
#include
<stdlib.h>
#include
<string.h>
#include
"mpconfig.h"
#include
"nlr.h"
#include
"misc.h"
#include
"qstr.h"
#include
"obj.h"
#include
"input.h"
#if MICROPY_USE_READLINE
#include
<readline/readline.h>
#include
<readline/history.h>
#endif
#define CTRL_D '\x04'
char
*
prompt
(
char
*
p
)
{
#if MICROPY_USE_READLINE
char
*
line
=
readline
(
p
);
if
(
line
)
{
add_history
(
line
);
}
#else
static
char
buf
[
256
];
fputs
(
p
,
stdout
);
char
*
s
=
fgets
(
buf
,
sizeof
(
buf
),
stdin
);
if
(
!
s
)
{
return
NULL
;
}
int
l
=
strlen
(
buf
);
if
(
buf
[
l
-
1
]
==
'\n'
)
{
buf
[
l
-
1
]
=
0
;
}
else
{
l
++
;
}
char
*
line
=
malloc
(
l
);
memcpy
(
line
,
buf
,
l
);
#endif
return
line
;
}
STATIC
mp_obj_t
mp_builtin_input
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
if
(
n_args
==
1
)
{
mp_obj_print
(
args
[
0
],
PRINT_STR
);
}
char
*
line
=
prompt
(
""
);
if
(
line
==
NULL
)
{
nlr_raise
(
mp_obj_new_exception
(
&
mp_type_EOFError
));
}
mp_obj_t
o
=
mp_obj_new_str
((
const
byte
*
)
line
,
strlen
(
line
),
false
);
free
(
line
);
return
o
;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
mp_builtin_input_obj
,
0
,
1
,
mp_builtin_input
);
unix/input.h
0 → 100644
View file @
c35e5343
char
*
prompt
(
char
*
p
);
unix/main.c
View file @
c35e5343
...
...
@@ -49,11 +49,7 @@
#include
"repl.h"
#include
"gc.h"
#include
"genhdr/py-version.h"
#if MICROPY_USE_READLINE
#include
<readline/readline.h>
#include
<readline/history.h>
#endif
#include
"input.h"
// Command line options, with their defaults
bool
compile_only
=
false
;
...
...
@@ -143,31 +139,6 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
return
s
;
}
STATIC
char
*
prompt
(
char
*
p
)
{
#if MICROPY_USE_READLINE
char
*
line
=
readline
(
p
);
if
(
line
)
{
add_history
(
line
);
}
#else
static
char
buf
[
256
];
fputs
(
p
,
stdout
);
char
*
s
=
fgets
(
buf
,
sizeof
(
buf
),
stdin
);
if
(
!
s
)
{
return
NULL
;
}
int
l
=
strlen
(
buf
);
if
(
buf
[
l
-
1
]
==
'\n'
)
{
buf
[
l
-
1
]
=
0
;
}
else
{
l
++
;
}
char
*
line
=
malloc
(
l
);
memcpy
(
line
,
buf
,
l
);
#endif
return
line
;
}
STATIC
void
do_repl
(
void
)
{
printf
(
"Micro Python "
MICROPY_GIT_TAG
" on "
MICROPY_BUILD_DATE
"; UNIX version
\n
"
);
...
...
unix/mpconfigport.h
View file @
c35e5343
...
...
@@ -72,6 +72,8 @@ typedef unsigned int machine_uint_t; // must be pointer size
typedef
void
*
machine_ptr_t
;
// must be of pointer size
typedef
const
void
*
machine_const_ptr_t
;
// must be of pointer size
extern
const
struct
_mp_obj_fun_native_t
mp_builtin_input_obj
;
extern
const
struct
_mp_obj_fun_native_t
mp_builtin_open_obj
;
#define MICROPY_EXTRA_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
unix/qstrdefsport.h
View file @
c35e5343
...
...
@@ -43,6 +43,7 @@ Q(callback)
Q
(
func
)
Q
(
var
)
Q
(
input
)
Q
(
time
)
Q
(
clock
)
Q
(
sleep
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment