linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Richard Weinberger <richard@nod.at>
To: linux-kernel@vger.kernel.org
Cc: rostedt@goodmis.org, pantelis.antoniou@konsulko.com,
	mark.rutland@arm.com, pmladek@suse.com,
	andriy.shevchenko@linux.intel.com, joe@perches.com,
	corbet@lwn.net, me@tobin.cc, sergey.senozhatsky@gmail.com,
	Richard Weinberger <richard@nod.at>
Subject: [PATCH 1/2] lib: vsprintf: Implement %pCOW
Date: Sun,  1 Apr 2018 10:56:21 +0200	[thread overview]
Message-ID: <20180401085622.7754-2-richard@nod.at> (raw)
In-Reply-To: <20180401085622.7754-1-richard@nod.at>

Add a new format string to print in cowsay format.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 lib/vsprintf.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index d7a708f82559..a48df6f1c3f0 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1693,6 +1693,55 @@ static int __init initialize_ptr_random(void)
 }
 early_initcall(initialize_ptr_random);
 
+static char *cowsay(char *buf, char *end, void *ptr)
+{
+	static char dashes[] = {[0 ... 256] = '-'};
+	static char unders[] = {[0 ... 256] = '_'};
+	static char spaces[] = {[0 ... 256] = ' '};
+	static struct cow_type {
+		int num_lines;
+		char *cow_lines[];
+	} default_cow = {
+		.num_lines = 5,
+		.cow_lines = {
+			"\\   ^__^",
+			" \\  (oo)\\_______",
+			"    (__)\\       )\\/\\",
+			"        ||----w |",
+			"        ||     ||",
+		},
+	};
+
+	int i, n;
+	char *orig_buf = buf;
+	char *str = ptr;
+	int len = strlen(str);
+
+	n = snprintf(buf, end - buf, "  %.*s\n< %s >\n  %.*s\n", len, unders,
+		     str, len, dashes);
+	if (n < 0 || buf + n >= end)
+		goto cow_too_fat;
+
+	buf += n;
+
+	for (i = 0; i < default_cow.num_lines; i++) {
+		n = snprintf(buf, end - buf, "%.*s%s\n", len / 2, spaces,
+			     default_cow.cow_lines[i]);
+		if (n < 0 || buf + n >= end)
+			goto cow_too_fat;
+
+		buf += n;
+	}
+
+	return buf;
+
+cow_too_fat:
+	n = snprintf(orig_buf, end - orig_buf, "%s\n", str);
+	if (n > 0)
+		orig_buf += n;
+	return orig_buf;
+}
+
 /* Maps a pointer to a 32 bit unique identifier. */
 static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
 {
@@ -1941,6 +1990,9 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 	case 'd':
 		return dentry_name(buf, end, ptr, spec, fmt);
 	case 'C':
+		if (fmt[1] == 'O' && fmt[2] == 'W')
+			return cowsay(buf, end, ptr);
+
 		return clock(buf, end, ptr, spec, fmt);
 	case 'D':
 		return dentry_name(buf, end,
-- 
2.13.6

  reply	other threads:[~2018-04-01  8:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-01  8:56 [PATCH 0/2] cowsay support Richard Weinberger
2018-04-01  8:56 ` Richard Weinberger [this message]
2018-04-01  9:15   ` [PATCH 1/2] lib: vsprintf: Implement %pCOW Adam Borowski
2018-04-01  9:18     ` Richard Weinberger
2018-04-02 14:18   ` Andy Shevchenko
2018-04-03  9:15     ` Petr Mladek
2018-04-01  8:56 ` [PATCH 2/2] printk: Use %pCOW for kmsg Richard Weinberger
2018-04-02  1:57 ` [PATCH 0/2] cowsay support Sergey Senozhatsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180401085622.7754-2-richard@nod.at \
    --to=richard@nod.at \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=corbet@lwn.net \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=me@tobin.cc \
    --cc=pantelis.antoniou@konsulko.com \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=sergey.senozhatsky@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).