From 657a5d2cdad645ea94a0970262efa5bfa864af25 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Mon, 10 Nov 2014 23:02:05 +0100 Subject: Handle all arguments, split out int parsing function --- src/CMakeLists.txt | 2 +- src/utils.c | 34 +++++++++++++++++++ src/utils.h | 7 ++++ src/yawa.c | 97 ++++++++++++++++++++++++++++++++---------------------- src/yawa.h | 44 ++++++++++++++++--------- 5 files changed, 128 insertions(+), 56 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 544cb87..33ed42d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(yawa yawa.c) +add_executable(yawa yawa.c utils.c) target_link_libraries (yawa X11) target_link_libraries (yawa Imlib2) target_link_libraries (yawa bsd) diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..2425576 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,34 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "yawa.h" +#include "utils.h" + +int +parse_int(char *string, char *arg) +{ + errno = 0; + char *endptr; + long val = strtol(string, &endptr, 10); + if (errno != 0) + { + char *errormsg; + asprintf(&errormsg, "parse_int: failed to parse %s", arg); + + perror(errormsg); + free(errormsg); + exit(-2); + } + + if (endptr == string) + { + fprintf(stderr, "Valid %s not found\n", arg); + exit(-2); + } + return (int)val; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..25e51ee --- /dev/null +++ b/src/utils.h @@ -0,0 +1,7 @@ +#ifndef UTILS_H +#define UTILS_H +#include + +int parse_int(char *string, char *arg); + +#endif diff --git a/src/yawa.c b/src/yawa.c index 1a669a5..4d783a5 100644 --- a/src/yawa.c +++ b/src/yawa.c @@ -29,17 +29,14 @@ #include #include "yawa.h" +#include "utils.h" #include "config.h" - /* Order of parameters: KEY, ARG, STATE. */ static error_t parse_opt (int key, char *arg, struct argp_state *state) { struct arguments *arguments = state->input; - char *endptr; - long val; - errno = 0; switch (key) { case 'a': @@ -48,78 +45,97 @@ parse_opt (int key, char *arg, struct argp_state *state) sizeof(arguments->add_color[0]) - 1); num_add_colors += 1; break; + case 'g': arguments->gradient = true; - val = strtol(arg, &endptr, 10); - arguments->angle = (int)val; - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) - || (errno != 0 && val == 0)) - { - perror("strtol"); - exit(-2); - } - - if (endptr == arg) - { - fprintf(stderr, "Valid gradiant angle not found\n"); - exit(-2); - } + arguments->angle = parse_int(arg, "gradient angle"); break; + case 'c': arguments->clear = true; break; + + case 's': arguments->solid = true; - arguments->color = arg; + arguments->solid_color = arg; break; + + case 'C': arguments->center = true; arguments->image = arg; break; + case 't': arguments->tile = true; arguments->image = arg; break; + case 'f': arguments->full = true; arguments->image = arg; break; + case 'F': arguments->fill = true; arguments->image = arg; break; + + case 'T': arguments->tint = true; - arguments->color = arg; + arguments->tint_color = arg; + break; + + case 'b': + arguments->blur = true; + arguments->blur_radius = parse_int(arg, "blur radius"); + break; + + case 'S': + arguments->sharpen = true; + arguments->sharpen_radius = parse_int(arg, "sharpen radius"); + break; + + case 'o': + arguments->contrast = true; + arguments->contrast_amount = parse_int(arg, "contrast amount"); + break; + + case 'B': + arguments->brightness = true; + arguments->brightness_amount = parse_int(arg, "brightness amount"); + break; + + case 'G': + arguments->gamma = true; + arguments->gamma_amount = parse_int(arg, "gamma amount"); break; case 'v': arguments->flipv = true; break; + case 'h': arguments->fliph = true; break; + case 'd': arguments->flipd = true; break; + case 'A': arguments->alpha = true; - val = strtol(arg, &endptr, 10); - arguments->alpha_amount = (int)val; - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) - || (errno != 0 && val == 0)) { - perror("strtol"); - exit(-2); - } + arguments->alpha_amount = parse_int(arg, "alpha amount"); + break; - if (endptr == arg) { - fprintf(stderr, "Valid alpha amount not found\n"); - exit(-2); - } + case 'w': + arguments->write = true; + arguments->write_file = arg; break; + default: return ARGP_ERR_UNKNOWN; } @@ -311,10 +327,11 @@ int main (int argc, char **argv) { struct arguments arguments = { - "", "", 0, 0, false, false, false, false, false, - false, false, false, false, false, false, false, - false, false, false, false, false, false, false, - {{0}} // add_color array + "", "", {{0}}, "", "", 0, 0, 0, 0, 0, 0, 0, + false, false, false, false, false, false, + false, false, false, false, false, false, + false, false, false, false, false, false, + false, }; /* Where the magic happens */ @@ -380,9 +397,9 @@ main (int argc, char **argv) if (arguments.solid) { Color c; - if (parse_color(arguments.color, &c, alpha) == 0) + if (parse_color(arguments.solid_color, &c, alpha) == 0) { - fprintf(stderr, "Bad color (%s)\n", arguments.color); + fprintf(stderr, "Bad color (%s)\n", arguments.solid_color); exit(-2); } imlib_context_set_color(c.r, c.g, c.b, c.a); @@ -451,7 +468,7 @@ main (int argc, char **argv) DATA8 r[256], g[256], b[256], a[256]; int j; - if (parse_color(arguments.color, &c, 255) == 0) + if (parse_color(arguments.tint_color, &c, 255) == 0) { fprintf(stderr, "Bad color\n"); exit(-2); diff --git a/src/yawa.h b/src/yawa.h index 38f15a4..7d4a1e7 100644 --- a/src/yawa.h +++ b/src/yawa.h @@ -1,5 +1,8 @@ #ifndef YAWA_H #define YAWA_H +#include +#include + #include "config.h" typedef struct @@ -8,12 +11,14 @@ typedef struct } Color; typedef enum -{ Full, Fill, Center, Tile } ImageMode; +{ + Full, Fill, Center, Tile +} ImageMode; -int set_root_atoms (Pixmap pixmap); +int set_root_atoms(Pixmap pixmap); bool parse_color (char *arg, Color *c, int a); -int load_image (ImageMode mode, const char *arg, int rootW, int rootH, int alpha, - Imlib_Image rootimg); +int load_image(ImageMode mode, const char *arg, int rootW, int rootH, int alpha, + Imlib_Image rootimg); static char doc[] = "yawa -- Yet Another Wallpaper Application"; const char *argp_program_version = PACKAGE_STRING; @@ -22,8 +27,18 @@ const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; static int num_add_colors; struct arguments { char *image; - char *color; + char *write_file; + + char add_color[8][8]; + char *solid_color; + char *tint_color; + int angle; + int blur_radius; + int sharpen_radius; + int contrast_amount; + int brightness_amount; + int gamma_amount; int alpha_amount; bool add: 1; @@ -34,18 +49,17 @@ struct arguments { bool tile: 1; bool full: 1; bool fill: 1; - bool tint: 1; - bool blur: 1; - bool sharpen: 1; - bool contrast: 1; - bool brightness: 1; - bool gamma: 1; - bool flipv: 1; - bool fliph: 1; + bool tint; + bool blur; + bool sharpen; + bool contrast; + bool brightness; + bool gamma; + bool flipv; + bool fliph; bool flipd; bool alpha; bool write; - char add_color[9][9]; }; /* Order of fields: {NAME, KEY, ARG, FLAGS, DOC, GROUP}. */ @@ -71,7 +85,7 @@ static struct argp_option options[] = {"blur", 'b', "RADIUS", 0, "Blur the current image", 4}, // Not implemented {"sharpen", 'S', "RADIUS", 0, "Sharpen the current image", 4}, // Not implemented {"contrast", 'o', "AMOUNT", 0, "Adjust the contrast of the current image", 4}, // Not implemented - {"brightness", 'G', "AMOUNT", 0, "Adjust the bightness of the current image", 4}, // Not implemented + {"brightness", 'B', "AMOUNT", 0, "Adjust the bightness of the current image", 4}, // Not implemented {"gamma", 'G', "AMOUNT", 0, "Adjust the gamma level of the current image", 4}, // Not implemented {"flipv", 'v', 0, 0, "Flip the current image vertically", 4}, // Not implemented {"fliph", 'h', 0, 0, "Flip the current image horizontally", 4}, // Not implemented -- cgit v1.2.3-70-g09d2