[libc] Add stdin definition.
authorSiva Chandra Reddy <sivachandra@google.com>
Thu, 20 Oct 2022 23:44:13 +0000 (23:44 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Sat, 22 Oct 2022 03:27:31 +0000 (03:27 +0000)
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136398

libc/config/linux/api.td
libc/config/linux/x86_64/entrypoints.txt
libc/spec/stdc.td
libc/src/__support/File/file.h
libc/src/__support/File/linux_file.cpp
libc/src/stdio/CMakeLists.txt
libc/src/stdio/stdin.cpp [new file with mode: 0644]
libc/src/stdio/stdin.h [new file with mode: 0644]

index 8d2e64f..abccc0f 100644 (file)
@@ -149,6 +149,7 @@ def StringAPI : PublicAPI<"string.h"> {
 def StdIOAPI : PublicAPI<"stdio.h"> {
   let Macros = [
     SimpleMacroDef<"stderr", "stderr">,
+    SimpleMacroDef<"stdin", "stdin">,
     SimpleMacroDef<"stdout", "stdout">,
     SimpleMacroDef<"_IOFBF", "0">,
     SimpleMacroDef<"_IOLBF", "1">,
index 2637faf..987e9b2 100644 (file)
@@ -387,6 +387,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putchar
     libc.src.stdio.puts
     libc.src.stdio.stderr
+    libc.src.stdio.stdin
     libc.src.stdio.stdout
 
     # stdlib.h entrypoints
index 4d6c0b1..3108872 100644 (file)
@@ -491,6 +491,7 @@ def StdC : StandardSpec<"stdc"> {
   HeaderSpec StdIO = HeaderSpec<
       "stdio.h",
       [
+          Macro<"stdin">,
           Macro<"stderr">,
           Macro<"stdout">,
           Macro<"_IOFBF">,
@@ -622,6 +623,10 @@ def StdC : StandardSpec<"stdc"> {
       ],
       [
           ObjectSpec<
+              "stdin",
+              "FILE *"
+          >,
+          ObjectSpec<
               "stdout",
               "FILE *"
           >,
index 40956ed..74655b1 100644 (file)
@@ -233,6 +233,7 @@ private:
 // library.
 File *openfile(const char *path, const char *mode);
 
+extern File *stdin;
 extern File *stdout;
 extern File *stderr;
 
index 782cee4..c6c93c8 100644 (file)
@@ -164,6 +164,12 @@ File *openfile(const char *path, const char *mode) {
   return file;
 }
 
+constexpr size_t STDIN_BUFFER_SIZE = 512;
+char stdin_buffer[STDIN_BUFFER_SIZE];
+static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false,
+                       File::ModeFlags(File::OpenMode::READ));
+File *stdin = &StdIn;
+
 constexpr size_t STDOUT_BUFFER_SIZE = 1024;
 char stdout_buffer[STDOUT_BUFFER_SIZE];
 static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,
index 68fdb9a..ea46d6b 100644 (file)
@@ -269,6 +269,18 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  stdin
+  SRCS
+    stdin.cpp
+  HDRS
+    stdin.h
+  DEPENDS
+    libc.include.stdio
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+)
+
+add_entrypoint_object(
   stdout
   SRCS
     stdout.cpp
diff --git a/libc/src/stdio/stdin.cpp b/libc/src/stdio/stdin.cpp
new file mode 100644 (file)
index 0000000..cd34189
--- /dev/null
@@ -0,0 +1,13 @@
+//===-- Definition of the global stdin object -----------------------------===//
+//
+// 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/__support/File/file.h"
+
+#include <stdio.h>
+
+extern "C" FILE *stdin = reinterpret_cast<FILE *>(__llvm_libc::stdin);
diff --git a/libc/src/stdio/stdin.h b/libc/src/stdio/stdin.h
new file mode 100644 (file)
index 0000000..ee4e6bf
--- /dev/null
@@ -0,0 +1,9 @@
+//===------------------------------------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#error "Do not include this file. Instead include __support/File/file.h."