From patchwork Mon Dec 12 15:23:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 61794 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6044538500B1 for ; Mon, 12 Dec 2022 15:23:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6044538500B1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670858616; bh=K/Y4DPy1GPLx1z72I+ahIx8QFZoBr8d/JD1WGKdQlBw=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=IPXllh2w0/9rJ9XWwZhte7zENvQqegtNPCvNR8O9sEhzo78uJITHlpr7D3DxBgocA rT1LBquc+AqjP9+Bpi68gCC2u7hRZxN3VgJTjgGXB+HMK9kyz1cba8eSR2oSKp2wGB WC8oQCkY26mpn3OxD+XenGGeDnGsl2it+zpWOh40= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 29766385F3F6 for ; Mon, 12 Dec 2022 15:23:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 29766385F3F6 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-663-ff_mxI-XNpGC5Shn45oBfg-1; Mon, 12 Dec 2022 10:23:10 -0500 X-MC-Unique: ff_mxI-XNpGC5Shn45oBfg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D53041C07583 for ; Mon, 12 Dec 2022 15:23:09 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.81]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0923840C2004 for ; Mon, 12 Dec 2022 15:23:08 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v5 03/11] stdio-common: Add __printf_function_invoke In-Reply-To: References: X-From-Line: 8699991d4d2c2a85ee497acb9d2740a2afef0816 Mon Sep 17 00:00:00 2001 Message-Id: <8699991d4d2c2a85ee497acb9d2740a2afef0816.1670858473.git.fweimer@redhat.com> Date: Mon, 12 Dec 2022 16:23:05 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" And __wprintf_function_invoke. These functions will be used to to call registered printf specifier callbacks on printf buffers after vfprintf and vfwprintf have been converted to buffers. The new implementation avoids alloca/variable length arrays. Reviewed-by: Adhemerval Zanella --- include/printf.h | 10 +++++ stdio-common/Makefile | 2 + stdio-common/Xprintf_function_invoke.c | 58 ++++++++++++++++++++++++++ stdio-common/printf_function_invoke.c | 22 ++++++++++ stdio-common/wprintf_function_invoke.c | 22 ++++++++++ 5 files changed, 114 insertions(+) create mode 100644 stdio-common/Xprintf_function_invoke.c create mode 100644 stdio-common/printf_function_invoke.c create mode 100644 stdio-common/wprintf_function_invoke.c diff --git a/include/printf.h b/include/printf.h index 78accaab6d..8f064149d3 100644 --- a/include/printf.h +++ b/include/printf.h @@ -41,6 +41,16 @@ union printf_arg void *pa_user; }; +/* Invoke a registered printf callback. Called from vfprintf and vfwprintf. */ +int __printf_function_invoke (void *, printf_function callback, + union printf_arg *args_value, + size_t ndata_args, + struct printf_info *info) attribute_hidden; +int __wprintf_function_invoke (void *, printf_function callback, + union printf_arg *args_value, + size_t ndata_args, + struct printf_info *info) attribute_hidden; + #include /* Now define the internal interfaces. */ diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 120d66ea93..6e6da091b1 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -63,6 +63,7 @@ routines := \ printf_buffer_write \ printf_fp \ printf_fphex \ + printf_function_invoke \ printf_size \ psiginfo \ psignal \ @@ -101,6 +102,7 @@ routines := \ wprintf_buffer_puts_1 \ wprintf_buffer_to_file \ wprintf_buffer_write \ + wprintf_function_invoke \ # routines aux := \ diff --git a/stdio-common/Xprintf_function_invoke.c b/stdio-common/Xprintf_function_invoke.c new file mode 100644 index 0000000000..908d02bf24 --- /dev/null +++ b/stdio-common/Xprintf_function_invoke.c @@ -0,0 +1,58 @@ +/* Invoke a printf specifier handler. Generic version. + Copyright (C) 1991-2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +int +Xprintf (function_invoke) (void *buf, + printf_function callback, + union printf_arg *args_value, + size_t ndata_args, + struct printf_info *info) +{ + /* Most custom specifiers expect just one argument. Use the heap + for larger argument arrays. */ + const void *onstack_args[4]; + const void **args; + if (ndata_args <= array_length (onstack_args)) + args = onstack_args; + else + { + args = calloc (ndata_args, sizeof (*args)); + if (args == NULL) + return -1; + } + + for (unsigned int i = 0; i < ndata_args; ++i) + args[i] = &args_value[i]; + + struct Xprintf (buffer_as_file) s; + Xprintf (buffer_as_file_init) (&s, buf); + + /* Call the function. */ + int done = callback (Xprintf (buffer_as_file_get) (&s), info, args); + + if (!Xprintf (buffer_as_file_terminate) (&s)) + done = -1; + + if (args != onstack_args) + free (args); + + /* Potential error from the callback function. */ + return done; +} diff --git a/stdio-common/printf_function_invoke.c b/stdio-common/printf_function_invoke.c new file mode 100644 index 0000000000..8919b892ca --- /dev/null +++ b/stdio-common/printf_function_invoke.c @@ -0,0 +1,22 @@ +/* Invoke a printf specifier handler. Multibyte version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include "printf_buffer-char.h" +#include "Xprintf_function_invoke.c" diff --git a/stdio-common/wprintf_function_invoke.c b/stdio-common/wprintf_function_invoke.c new file mode 100644 index 0000000000..29a6b4e073 --- /dev/null +++ b/stdio-common/wprintf_function_invoke.c @@ -0,0 +1,22 @@ +/* Invoke a printf specifier handler. Wide version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include "printf_buffer-wchar_t.h" +#include "Xprintf_function_invoke.c"