From cf44e5709816f67ce11db19c3b0ea9f3b08ac4e9 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 25 Mar 2008 17:09:40 +0000 Subject: [PATCH] Join multiple xcowsay arguments together git-svn-id: http://svn.nickg.me.uk/work/xcowsay@334 a97b1542-0b21-0410-a459-e47997c36f34 --- configure.ac | 4 ++-- src/xcowsay.c | 35 ++++++++++++++++++++++++++++++----- xcowsay.6 | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index a7d23f4..be74533 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ -AC_INIT([xcowsay], [0.6.1], - [Nick Gasson ], +AC_INIT([xcowsay], [0.7], + [Nick Gasson ], [xcowsay]) AM_INIT_AUTOMAKE([-Wall]) AC_PROG_CC diff --git a/src/xcowsay.c b/src/xcowsay.c index e8e31b3..fc2abeb 100644 --- a/src/xcowsay.c +++ b/src/xcowsay.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "display_cow.h" #include "settings.h" @@ -73,7 +75,7 @@ static void read_from_stdin(void) static void usage() { static const char *usage_message = - "Usage: xcowsay [OPTION]... [MESSAGE]\n" + "Usage: xcowsay [OPTION]... [MESSAGE]...\n" "Display a cow on your desktop with MESSAGE or standard input.\n\n" "Options:\n" " -h, --help\t\tDisplay this message and exit.\n" @@ -119,6 +121,30 @@ static int parse_int_option(const char *optarg) } } +/* + * Join all the strings in argv from ind to argc-1 into one big + * string with spaces between the words. + */ +static char *cat_from_index(int ind, int argc, char **argv) +{ + size_t len = 0, i; + for (i = ind; i < argc; i++) + len += strlen(argv[i]) + (i < argc - 1 ? 1 : 0); + + char *buf = malloc(len+1); + assert(buf); + + char *p = buf; + for (i = ind; i < argc; i++) { + strcpy(p, argv[i]); + p += strlen(argv[i]); + if (i < argc - 1) // No space at the end + *p++ = ' '; + } + + return buf; +} + int main(int argc, char **argv) { add_int_option("lead_in_time", DEF_LEAD_IN_TIME); @@ -181,11 +207,10 @@ int main(int argc, char **argv) if (optind == argc) { read_from_stdin(); } - else if (optind == argc - 1) { - display_cow_or_invoke_daemon(debug, argv[optind]); - } else { - fprintf(stderr, "Error: Too many arguments\n"); + char *str = cat_from_index(optind, argc, argv); + display_cow_or_invoke_daemon(debug, str); + free(str); } } diff --git a/xcowsay.6 b/xcowsay.6 index 0ac5aea..afc9804 100644 --- a/xcowsay.6 +++ b/xcowsay.6 @@ -12,7 +12,7 @@ xcowsay \- Display a cute cow and speech bubble. .RB [ "-d" ] .RB [ "-f" .RI \| font \|] -.RI [\| text \|] +.RI [\| text \|]... .SH DESCRIPTION Display a cow with a speech bubble containing some text. If .I text -- 2.39.2