From e3a1934ababe450b20e5f25e5cedc041c182e590 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 21 Nov 2009 21:11:04 +0000 Subject: [PATCH] Add --image option to specify a non-cow image --- src/config_file.c | 3 +- src/display_cow.c | 17 +++++++--- src/xcowsay.c | 86 ++++++++++++++++++++++++++--------------------- xcowsay.6 | 5 +++ 4 files changed, 66 insertions(+), 45 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index 9e3fde5..ee1c7fc 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -91,7 +91,8 @@ static token_t next_token(FILE *f, strbuf_t* sbuf, int *lineno, jmp_buf *escape) return tEQUALS; else if ('#' == next) skip_to_eol = true; - else if (isalpha(next) || isdigit(next) || '_' == next) + else if (isalpha(next) || isdigit(next) || '_' == next + || '/' == next || '.' == next || '-' == next) push_char(sbuf, next); else { fprintf(stderr, "Illegal character in xcowsayrc: %c\n", next); diff --git a/src/display_cow.c b/src/display_cow.c index 1acdda8..f66d440 100644 --- a/src/display_cow.c +++ b/src/display_cow.c @@ -74,19 +74,26 @@ static cowstate_t next_state(cowstate_t state) } } -// TODO: Change this to use the actual max path length... -#define MAX_COW_PATH 256 static GdkPixbuf *load_cow() { - char cow_path[MAX_COW_PATH]; - snprintf(cow_path, MAX_COW_PATH, "%s/%s_%s.png", DATADIR, - get_string_option("image_base"), get_string_option("cow_size")); + char *cow_path; + + const char *alt_image = get_string_option("alt_image"); + + if (*alt_image) + cow_path = strdup(alt_image); + else + asprintf(&cow_path, "%s/%s_%s.png", DATADIR, + get_string_option("image_base"), + get_string_option("cow_size")); GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(cow_path, NULL); if (NULL == pixbuf) { fprintf(stderr, i18n("Failed to load cow image: %s\n"), cow_path); exit(EXIT_FAILURE); } + + free(cow_path); return pixbuf; } diff --git a/src/xcowsay.c b/src/xcowsay.c index a3c5151..6bc1ce1 100644 --- a/src/xcowsay.c +++ b/src/xcowsay.c @@ -43,6 +43,7 @@ #define DEF_COW_SIZE "med" #define DEF_IMAGE_BASE "cow" #define DEF_DREAM_TIME 10000 +#define DEF_ALT_IMAGE "" #define MAX_STDIN 4096 // Maximum chars to read from stdin @@ -60,6 +61,7 @@ static struct option long_options[] = { {"cow-size", required_argument, 0, 'c'}, {"reading-speed", required_argument, 0, 'r'}, {"daemon", no_argument, &daemon_flag, 1}, + {"image", required_argument, 0, 'i'}, {"debug", no_argument, &debug, 1}, {0, 0, 0, 0} }; @@ -80,49 +82,51 @@ static void read_from_stdin(cowmode_t mode) static void usage() { - printf - ("%s: xcowsay [OPTION]... [MESSAGE]...\n" - "%s\n\n" - "%s:\n" - " -h, --help\t\t%s\n" - " -v, --version\t\t%s\n" - " -t, --time=SECONDS\t%s\n" - " -r, --reading-speed=N\t%s\n" - " -f, --font=FONT\t%s\n" - " -d, --dream=FILE\t%s\n" - " --think\t\t%s\n" - " --daemon\t\t%s\n" - " --cow-size=SIZE\t%s\n" - " --debug\t\t%s\n\n" - "%s\n\n" - "%s\n\n" - "%s\n", - i18n("Usage"), - i18n("Display a cow on your desktop with MESSAGE or standard input."), - i18n("Options"), - i18n("Display this message and exit."), - i18n("Print version information."), - i18n("Number of seconds to display message for"), - i18n("Number of milliseconds to delay per word."), - i18n("Set message font (Pango format)."), - i18n("Display an image instead of text."), - i18n("Display a thought bubble rather than a speech bubble."), - i18n("Run xcowsay in daemon mode."), - i18n("Size of the cow (small, med, large)."), - i18n("Keep daemon attached to terminal."), - i18n("Default values for these options can be specified in the " - "xcowsay config\nfile. See the man page for more information."), - i18n("If the display_time option is not set the display time will " - "be calcuated\nfrom the reading_speed parameter multiplied by " - "the word count."), - i18n("Report bugs to nick@nickg.me.uk") - ); + printf( + "%s: xcowsay [OPTION]... [MESSAGE]...\n" + "%s\n\n" + "%s:\n" + " -h, --help\t\t%s\n" + " -v, --version\t\t%s\n" + " -t, --time=SECONDS\t%s\n" + " -r, --reading-speed=N\t%s\n" + " -f, --font=FONT\t%s\n" + " -d, --dream=FILE\t%s\n" + " --think\t\t%s\n" + " --daemon\t\t%s\n" + " --cow-size=SIZE\t%s\n" + " --image=FILE\t%s\n" + " --debug\t\t%s\n\n" + "%s\n\n" + "%s\n\n" + "%s\n", + i18n("Usage"), + i18n("Display a cow on your desktop with MESSAGE or standard input."), + i18n("Options"), + i18n("Display this message and exit."), + i18n("Print version information."), + i18n("Number of seconds to display message for"), + i18n("Number of milliseconds to delay per word."), + i18n("Set message font (Pango format)."), + i18n("Display an image instead of text."), + i18n("Display a thought bubble rather than a speech bubble."), + i18n("Run xcowsay in daemon mode."), + i18n("Size of the cow (small, med, large)."), + i18n("Use a different image instead of the cow."), + i18n("Keep daemon attached to terminal."), + i18n("Default values for these options can be specified in the " + "xcowsay config\nfile. See the man page for more information."), + i18n("If the display_time option is not set the display time will " + "be calcuated\nfrom the reading_speed parameter multiplied by " + "the word count. Set the\ndisplay time to zero to display the " + " cow indefinitely until it is clicked\non."), + i18n("Report bugs to nick@nickg.me.uk")); } static void version() { static const char *copy = - "Copyright (C) 2008-2009 Nick Gasson\n" + "Copyright (C) 2008-2009 Nick Gasson\n" "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and\n" "you are welcome to redistribute it under certain conditions. See the GNU\n" "General Public Licence for details."; @@ -186,6 +190,7 @@ int main(int argc, char **argv) add_string_option("font", DEF_FONT); add_string_option("cow_size", DEF_COW_SIZE); add_string_option("image_base", DEF_IMAGE_BASE); + add_string_option("alt_image", DEF_ALT_IMAGE); parse_config_file(); @@ -217,7 +222,10 @@ int main(int argc, char **argv) break; case 'f': set_string_option("font", optarg); - break; + break; + case 'i': + set_string_option("alt_image", optarg); + break; case '?': // getopt_long already printed an error message failure = 1; diff --git a/xcowsay.6 b/xcowsay.6 index bc1f126..a4f496f 100644 --- a/xcowsay.6 +++ b/xcowsay.6 @@ -108,6 +108,11 @@ Size of the cow image. Current choices are The corresponding config file option is .IR cow_size . .TP +.BI "--image=" file +Use a different image instead of the cow. The corresponding config file +option is +.IR alt_image . +.TP .B "--debug" Print messages about what .B xcowsay -- 2.39.2