Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp125818iof; Sun, 5 Jun 2022 23:08:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOJ3nrqhgbHRvNH6vBQTDVpPcozfbkluvo5kNAm8Wu8kpx4feFCNNf4iBLG+cplHiXdZki X-Received: by 2002:a17:902:aa85:b0:155:ceb9:3710 with SMTP id d5-20020a170902aa8500b00155ceb93710mr23029705plr.59.1654495687222; Sun, 05 Jun 2022 23:08:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495687; cv=none; d=google.com; s=arc-20160816; b=MHr/2P2sZQyBCErDpfdZno9bURhTiDSxvimNMymE2uzL3beD11Pu6JG/lSmyJh+cSm LTKpky41zEjDlZNt+K77/oqLQMRw/N6lxBRiwqQp8uauSsYx7wfm8siGK0lcHGKRw982 yppHVkFZAtlySGOZO0FrsqjF/tCGlikA071i8/Cs2mSc7fhenVsIeYpPI4QOfKsV2G1n LVy6hHNSmUn3w2VX9gnFbTuo5ZZSIQYD6KssLd2ncBLbF8UmnuxrYwpedPlncgp1o8gU iG8q0B3mFvJ1nIs/AJgdf+wu1z3X4lrjqe+58Ko4qdZAJ9ZcwgQgXbsFBVKmY16rDfa7 v5Qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=k/S9RyrnAxoxBJABrQh6w4keA3WMxhL6CB9ATTh+2iM=; b=HCdrMC0DBcj8ZcCCUIeXwYZa0CjgD0Ro9p2e7PthqzvmdUDVzyRnXfJgwoqcNuzfos mhUjFjOz3qlLuBLR+XvSSuGVkUJ0Y4SZVnakhDwtbGQEQPQIu0LtAgXXz6PrAoLtVQRA by6+igevO05HU1IDKmladk9H3U4uQ5C/7w5z1SkETvwnuMiJsS6u55Eo3FC/lA19JgDU WtoaYn4kqcmJTW0jJ6aUdkTd+17k/Ax1pg1Kyouhtl4kl80novmbZ+SEMiWan6Ipn9rM z+AtrjOny850x80p0y7ApMqqTQleSGWUvSkUsE3fUOm/0UwsHUz4MVhqEZZ053d1kljW OS2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kX5pVQfS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t17-20020a170902e1d100b0015cf4f37e4bsi17009602pla.610.2022.06.05.23.08.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:08:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kX5pVQfS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0DDF7F749A; Sun, 5 Jun 2022 21:58:04 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240281AbiFDTb2 (ORCPT + 99 others); Sat, 4 Jun 2022 15:31:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240091AbiFDTbI (ORCPT ); Sat, 4 Jun 2022 15:31:08 -0400 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84B4F4F9DD for ; Sat, 4 Jun 2022 12:31:03 -0700 (PDT) Received: by mail-qt1-x834.google.com with SMTP id j8so5184965qtn.13 for ; Sat, 04 Jun 2022 12:31:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=k/S9RyrnAxoxBJABrQh6w4keA3WMxhL6CB9ATTh+2iM=; b=kX5pVQfSHOnNdg7M682y9YLafSoDvPyLol0a+J1ubVd6S/w3RxV8ZLbMUiaKYIADtx 6N/IUYDkEigm2PS+DxW5vQTLVgkuN39sabY1YsKYYjea73WUgpl6eWngEpuIa2xofdH6 cTq/tfvO+3hDSFgclGTJ3wi1dDSIqoZZ1xnyrduucSaFT985PZAHnpUzyQLyEqc6XMwh 18L76hF4rQthxkC7KOkXsDJLlVttEyPIPIA6J5yjTibyxxq9yIIu009oNYu/l9w1Zppy 4MTEWb+nV0Gsqxia8SG4dwY2bCV7pPLpWHXapGQ8vOaGLeKCtBzbPV/OQGhw35g+EnmN 2xow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=k/S9RyrnAxoxBJABrQh6w4keA3WMxhL6CB9ATTh+2iM=; b=W1Yt7GV30bvICpRkGkDtSBSoftGQN7K+wLzyCj2/2A7ntooeXkWc+XLCRB5LTCd3cA TzLGZemoD64Kikc7IlQbtqEW7V6zLIxKXhTk+etQ6C9xdoPnqyN4lCrz3bBO6Wnvpoxc zgG06qmh4b2xmFjVGG/PqRLK7LRVuuyMi91sP/oVPdaHiywlTe5G7MEcR/y0yBUvTxQm /T6mflTBTAGBcC32zbpl9Z8gmMlSOFeq07Bt8uqu+EIA/s9MB//ynCi5pmEKgKcvcjeV ngU2lHEfInJeuJeG8WMtgICbhJ8h92j7jcS5i2l6who+9RzIVLpdNeYuDuX+FNCJ3qdh AFHw== X-Gm-Message-State: AOAM531j/6wxbg3BrlonX0igOvWILL9RjdiNJRz5/Ls2p+k8isbt7FoC MDOwWU1a1JDHm4G+Nmo/dpnPM6O2N8NU X-Received: by 2002:a05:622a:292:b0:304:3b6e:3f49 with SMTP id z18-20020a05622a029200b003043b6e3f49mr13106649qtw.613.1654371061997; Sat, 04 Jun 2022 12:31:01 -0700 (PDT) Received: from moria.home.lan (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id o17-20020ac84291000000b00304defdb1b3sm3537426qtl.85.2022.06.04.12.31.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jun 2022 12:31:01 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org Cc: Kent Overstreet , pmladek@suse.com, rostedt@goodmis.org Subject: [PATCH v3 08/33] lib/printbuf: Tabstops, indenting Date: Sat, 4 Jun 2022 15:30:17 -0400 Message-Id: <20220604193042.1674951-9-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220604193042.1674951-1-kent.overstreet@gmail.com> References: <20220604193042.1674951-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds two new features to printbuf for structured formatting: - Indent level: the indent level, as a number of spaces, may be increased with pr_indent_add() and decreased with pr_indent_sub(). Subsequent lines, when started with pr_newline() (not "\n", although that may change) will then be intended according to the current indent level. This helps with pretty-printers that structure a large amonut of data across multiple lines and multiple functions. - Tabstops: Tabstops may be set by assigning to the printbuf->tabstops array. Then, pr_tab() may be used to advance to the next tabstop, printing as many spaces as required - leaving previous output left justified to the previous tabstop. pr_tab_rjust() advances to the next tabstop but inserts the spaces just after the previous tabstop - right justifying the previously-outputted text to the next tabstop. Signed-off-by: Kent Overstreet --- include/linux/printbuf.h | 28 +++++++++ lib/printbuf.c | 125 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h index 35236f774a..8fb074eb67 100644 --- a/include/linux/printbuf.h +++ b/include/linux/printbuf.h @@ -36,6 +36,23 @@ * memory allocation failure we usually don't want to bail out and unwind - we * want to print what we've got, on a best-effort basis. But code that does want * to return -ENOMEM may check printbuf.allocation_failure. + * + * Indenting, tabstops: + * + * To aid is writing multi-line pretty printers spread across multiple + * functions, printbufs track the current indent level. + * + * printbuf_indent_push() and printbuf_indent_pop() increase and decrease the current indent + * level, respectively. + * + * To use tabstops, set printbuf->tabstops[]; they are in units of spaces, from + * start of line. Once set, prt_tab() will output spaces up to the next tabstop. + * prt_tab_rjust() will also advance the current line of text up to the next + * tabstop, but it does so by shifting text since the previous tabstop up to the + * next tabstop - right justifying it. + * + * Make sure you use prt_newline() instead of \n in the format string for indent + * level and tabstops to work corretly. */ #include @@ -44,18 +61,29 @@ struct printbuf { char *buf; unsigned size; unsigned pos; + unsigned last_newline; + unsigned last_field; + unsigned indent; /* * If nonzero, allocations will be done with GFP_ATOMIC: */ u8 atomic; bool allocation_failure:1; bool heap_allocated:1; + u8 tabstop; + u8 tabstops[4]; }; int printbuf_make_room(struct printbuf *, unsigned); const char *printbuf_str(const struct printbuf *); void printbuf_exit(struct printbuf *); +void prt_newline(struct printbuf *); +void printbuf_indent_add(struct printbuf *, unsigned); +void printbuf_indent_sub(struct printbuf *, unsigned); +void prt_tab(struct printbuf *); +void prt_tab_rjust(struct printbuf *); + /* Initializer for a heap allocated printbuf: */ #define PRINTBUF ((struct printbuf) { .heap_allocated = true }) diff --git a/lib/printbuf.c b/lib/printbuf.c index 0093b34158..e11a504abf 100644 --- a/lib/printbuf.c +++ b/lib/printbuf.c @@ -11,6 +11,11 @@ #include #include +static inline size_t printbuf_linelen(struct printbuf *buf) +{ + return buf->pos - buf->last_newline; +} + int printbuf_make_room(struct printbuf *out, unsigned extra) { unsigned new_size; @@ -68,3 +73,123 @@ void printbuf_exit(struct printbuf *buf) } } EXPORT_SYMBOL(printbuf_exit); + +void prt_newline(struct printbuf *buf) +{ + unsigned i; + + printbuf_make_room(buf, 1 + buf->indent); + + __prt_char(buf, '\n'); + + buf->last_newline = buf->pos; + + for (i = 0; i < buf->indent; i++) + __prt_char(buf, ' '); + + printbuf_nul_terminate(buf); + + buf->last_field = buf->pos; + buf->tabstop = 0; +} +EXPORT_SYMBOL(prt_newline); + +/** + * printbuf_indent_add - add to the current indent level + * + * @buf: printbuf to control + * @spaces: number of spaces to add to the current indent level + * + * Subsequent lines, and the current line if the output position is at the start + * of the current line, will be indented by @spaces more spaces. + */ +void printbuf_indent_add(struct printbuf *buf, unsigned spaces) +{ + if (WARN_ON_ONCE(buf->indent + spaces < buf->indent)) + spaces = 0; + + buf->indent += spaces; + while (spaces--) + prt_char(buf, ' '); +} +EXPORT_SYMBOL(printbuf_indent_add); + +/** + * printbuf_indent_sub - subtract from the current indent level + * + * @buf: printbuf to control + * @spaces: number of spaces to subtract from the current indent level + * + * Subsequent lines, and the current line if the output position is at the start + * of the current line, will be indented by @spaces less spaces. + */ +void printbuf_indent_sub(struct printbuf *buf, unsigned spaces) +{ + if (WARN_ON_ONCE(spaces > buf->indent)) + spaces = buf->indent; + + if (buf->last_newline + buf->indent == buf->pos) { + buf->pos -= spaces; + printbuf_nul_terminate(buf); + } + buf->indent -= spaces; +} +EXPORT_SYMBOL(printbuf_indent_sub); + +/** + * prt_tab - Advance printbuf to the next tabstop + * + * @buf: printbuf to control + * + * Advance output to the next tabstop by printing spaces. + */ +void prt_tab(struct printbuf *out) +{ + int spaces = max_t(int, 0, out->tabstops[out->tabstop] - printbuf_linelen(out)); + + BUG_ON(out->tabstop > ARRAY_SIZE(out->tabstops)); + + prt_chars(out, ' ', spaces); + + out->last_field = out->pos; + out->tabstop++; +} +EXPORT_SYMBOL(prt_tab); + +/** + * prt_tab_rjust - Advance printbuf to the next tabstop, right justifying + * previous output + * + * @buf: printbuf to control + * + * Advance output to the next tabstop by inserting spaces immediately after the + * previous tabstop, right justifying previously outputted text. + */ +void prt_tab_rjust(struct printbuf *buf) +{ + BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops)); + + if (printbuf_linelen(buf) < buf->tabstops[buf->tabstop]) { + unsigned move = buf->pos - buf->last_field; + unsigned shift = buf->tabstops[buf->tabstop] - + printbuf_linelen(buf); + + printbuf_make_room(buf, shift); + + if (buf->last_field + shift < buf->size) + memmove(buf->buf + buf->last_field + shift, + buf->buf + buf->last_field, + min(move, buf->size - 1 - buf->last_field - shift)); + + if (buf->last_field < buf->size) + memset(buf->buf + buf->last_field, ' ', + min(shift, buf->size - buf->last_field)); + + buf->pos += shift; + printbuf_nul_terminate(buf); + } + + buf->last_field = buf->pos; + buf->tabstop++; +} +EXPORT_SYMBOL(prt_tab_rjust); -- 2.36.0