-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[libc] Support for scanf on baremetal #131043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
dee8274
4ebcb48
cdad78e
c9d8ccc
8f461ff
3cffce6
062804f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //===-- Implementation of scanf ---------------------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/stdio/scanf.h" | ||
|
|
||
| #include "hdr/stdio_macros.h" | ||
| #include "src/__support/OSUtil/io.h" | ||
| #include "src/__support/arg_list.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/stdio/scanf_core/reader.h" | ||
| #include "src/stdio/scanf_core/scanf_main.h" | ||
|
|
||
| #include <stdarg.h> | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| namespace { | ||
|
|
||
| struct StreamReader : scanf_core::Reader<StreamReader> { | ||
| LIBC_INLINE char getc() { | ||
| char buf[1]; | ||
| auto result = read_from_stdin(buf, sizeof(buf)); | ||
| if (result <= 0) | ||
| return EOF; | ||
| return buf[0]; | ||
| } | ||
| LIBC_INLINE void ungetc(int) {} | ||
| }; | ||
|
||
|
|
||
| } // namespace | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) { | ||
| va_list vlist; | ||
| va_start(vlist, format); | ||
| internal::ArgList args(vlist); // This holder class allows for easier copying | ||
| // and pointer semantics, as well as handling | ||
| // destruction automatically. | ||
| va_end(vlist); | ||
|
|
||
| StreamReader reader; | ||
| int retval = scanf_core::scanf_main(&reader, format, args); | ||
| // This is done to avoid including stdio.h in the internals. On most systems | ||
| // EOF is -1, so this will be transformed into just "return retval". | ||
| return (retval == -1) ? EOF : retval; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| //===-- Implementation of vscanf --------------------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/stdio/vscanf.h" | ||
|
|
||
| #include "hdr/stdio_macros.h" | ||
| #include "src/__support/OSUtil/io.h" | ||
| #include "src/__support/arg_list.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/stdio/scanf_core/reader.h" | ||
| #include "src/stdio/scanf_core/scanf_main.h" | ||
|
|
||
| #include <stdarg.h> | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| namespace { | ||
|
|
||
| struct StreamReader : scanf_core::Reader<StreamReader> { | ||
|
||
| LIBC_INLINE char getc() { | ||
| char buf[1]; | ||
| auto result = read_from_stdin(buf, sizeof(buf)); | ||
| if (result <= 0) | ||
| return EOF; | ||
| return buf[0]; | ||
| } | ||
| LIBC_INLINE void ungetc(int) {} | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, vscanf, | ||
| (const char *__restrict format, va_list vlist)) { | ||
| internal::ArgList args(vlist); // This holder class allows for easier copying | ||
| // and pointer semantics, as well as handling | ||
| // destruction automatically. | ||
| va_end(vlist); | ||
|
|
||
| StreamReader reader; | ||
| int retval = scanf_core::scanf_main(&reader, format, args); | ||
| // This is done to avoid including stdio.h in the internals. On most systems | ||
| // EOF is -1, so this will be transformed into just "return retval". | ||
| return (retval == -1) ? EOF : retval; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not needed anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was moved to
libc/src/stdio/generic/CMakeLists.txt.