From 6bf79945201265dccf62fd1e393f4e062912704c Mon Sep 17 00:00:00 2001 From: Tamas TEVESZ Date: Wed, 31 Mar 2010 04:24:17 +0200 Subject: [PATCH] Style stuff up - convert hand-rolled arg parsers to getopt_long - sort headers, get rid of some duplicates in the process - in fontconv, replace magic numbers with constants (there's little practical value to this here, but it's much nicer) - slightly redo fontconv.c:strToInt() --- util/convertfonts.c | 115 ++++++++++++++++++++++++++------------------ util/fontconv.c | 57 ++++++++++++++-------- util/geticonset.c | 63 ++++++++++++++++-------- util/getstyle.c | 107 ++++++++++++++++++++++++----------------- util/seticons.c | 74 ++++++++++++++++------------ 5 files changed, 257 insertions(+), 159 deletions(-) diff --git a/util/convertfonts.c b/util/convertfonts.c index 2aa79c75..7f93a93c 100644 --- a/util/convertfonts.c +++ b/util/convertfonts.c @@ -22,11 +22,18 @@ #define PROG_VERSION "convertfonts (Window Maker) 1.0" -#include -#include +#ifdef __GLIBC__ +#define _GNU_SOURCE /* getopt_long */ +#endif + #include -#include + +#include #include +#include +#include +#include + #include #include "../src/wconfig.h" @@ -48,73 +55,89 @@ extern char *__progname; extern char *convertFont(char *font, Bool keepXLFD); -void print_help() +void print_help(int print_usage, int exitval) { - printf("\nUsage: %s \n\n", __progname); - puts("Converts fonts in a style file into fontconfig format"); - puts(""); - puts(" -h, --help display this help and exit"); - puts(" -v, --version output version information and exit"); - puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=' hint"); - puts(" to the font name. This property is not used by the fontconfig"); - puts(" matching engine to find the font, but it is useful as a hint"); - puts(" about what the original font was to allow hand tuning the"); - puts(" result or restoring the xlfd. The default is to not add it"); - puts(" as it results in long, unreadable and confusing names."); - puts(""); + printf("Usage: %s [-h] [-v] [--keep-xlfd] \n", __progname); + if (print_usage) { + puts("Converts fonts in a style file into fontconfig format"); + puts(""); + puts(" -h, --help display this help and exit"); + puts(" -v, --version output version information and exit"); + puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=' hint"); + puts(" to the font name. This property is not used by the fontconfig"); + puts(" matching engine to find the font, but it is useful as a hint"); + puts(" about what the original font was to allow hand tuning the"); + puts(" result or restoring the xlfd. The default is to not add it"); + puts(" as it results in long, unreadable and confusing names."); + } + exit(exitval); } int main(int argc, char **argv) { WMPropList *style, *key, *val; char *file = NULL, *oldfont, *newfont; - struct stat statbuf; + struct stat st; Bool keepXLFD = False; - int i; + int i, ch; - if (argc < 2) { - print_help(); - exit(0); + struct option longopts[] = { + { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { "keep-xlfd", no_argument, &keepXLFD, True }, + { NULL, 0, NULL, 0 } + }; + + while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) + switch(ch) { + case 'v': + puts(PROG_VERSION); + return 0; + /* NOTREACHED */ + case 'h': + print_help(1, 0); + /* NOTREACHED */ + case 0: + break; + default: + print_help(0, 1); + /* NOTREACHED */ + } + + argc -= optind; + argv += optind; + + if (argc != 1) + print_help(0, 1); + + file = argv[0]; + + if (stat(file, &st) != 0) { + perror(file); + return 1; } - for (i = 1; i < argc; i++) { - if (strcmp("-v", argv[i]) == 0 || strcmp("--version", argv[i]) == 0) { - puts(PROG_VERSION); - exit(0); - } else if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) { - print_help(); - exit(0); - } else if (strcmp("--keep-xlfd", argv[i]) == 0) { - keepXLFD = True;; - } else if (argv[i][0] == '-') { - printf("%s: invalid argument '%s'\n", __progname, argv[i]); - printf("Try '%s --help' for more information\n", __progname); - exit(1); - } else { - file = argv[i]; - } + if (!S_ISREG(st.st_mode)) { /* maybe symlink too? */ + fprintf(stderr, "%s: `%s' is not a regular file\n", __progname, file); + return 1; } /* we need this in order for MB_CUR_MAX to work */ + /* this contradicts big time with getstyle */ setlocale(LC_ALL, ""); WMPLSetCaseSensitive(False); - if (stat(file, &statbuf) < 0) { - perror(file); - exit(1); - } - style = WMReadPropListFromFile(file); if (!style) { perror(file); - printf("%s: could not load style file.\n", __progname); - exit(1); + printf("%s: could not load style file\n", __progname); + return 1; } if (!WMIsPLDictionary(style)) { printf("%s: '%s' is not a well formatted style file\n", __progname, file); - exit(1); + return 1; } for (i = 0; FontOptions[i] != NULL; i++) { @@ -135,5 +158,5 @@ int main(int argc, char **argv) WMWritePropListToFile(style, file); - exit(0); + return 0; } diff --git a/util/fontconv.c b/util/fontconv.c index e4709981..e3263b4e 100644 --- a/util/fontconv.c +++ b/util/fontconv.c @@ -1,13 +1,32 @@ -#include #include #include +#include + #include #include "../src/wconfig.h" #define DEFAULT_FONT "sans serif:pixelsize=12" +/* X Font Name Suffix field names */ +enum { + FOUNDRY, + FAMILY_NAME, + WEIGHT_NAME, + SLANT, + SETWIDTH_NAME, + ADD_STYLE_NAME, + PIXEL_SIZE, + POINT_SIZE, + RESOLUTION_X, + RESOLUTION_Y, + SPACING, + AVERAGE_WIDTH, + CHARSET_REGISTRY, + CHARSET_ENCODING +}; + static int countChar(char *str, char c) { int count = 0; @@ -26,7 +45,7 @@ static int countChar(char *str, char c) typedef struct str { char *str; - int len; + size_t len; } str; #define XLFD_TOKENS 14 @@ -37,6 +56,7 @@ static str *getXLFDTokens(char *xlfd) int i, len, size; char *ptr; + /* XXX: why does this assume there can't ever be XFNextPrefix? */ if (!xlfd || *xlfd != '-' || countChar(xlfd, '-') != XLFD_TOKENS) return NULL; @@ -62,26 +82,25 @@ static str *getXLFDTokens(char *xlfd) static int strToInt(str * token) { - int res = 0, pos, c; + static char buf[32]; /* enough for an Incredibly Big Number */ - if (token->len == 0 || token->str[0] == '*') { + if (token->len == 0 || + token->str[0] == '*' || + token->len >= sizeof(buf)) return -1; - } else { - for (res = 0, pos = 0; pos < token->len; pos++) { - c = token->str[pos] - '0'; - if (c < 0 || c > 9) - break; - res = res * 10 + c; - } - } - return res; + + memset(buf, 0, sizeof(buf)); + strncpy(buf, token->str, token->len); + + /* the code using this will gracefully handle overflows */ + return (int)strtol(buf, NULL, 10); } static char *mapWeightToName(str * weight) { char *normalNames[] = { "medium", "normal", "regular" }; static char buf[32]; - int i; + size_t i; if (weight->len == 0) return ""; @@ -123,11 +142,11 @@ char *xlfdToFc(char *xlfd, char *useFamily, Bool keepXLFD) if (!tokens) return wstrdup(DEFAULT_FONT); - family = &(tokens[1]); - weight = &(tokens[2]); - slant = &(tokens[3]); - pixelsize = strToInt(&tokens[6]); - size = strToInt(&tokens[7]); + family = &(tokens[FAMILY_NAME]); + weight = &(tokens[WEIGHT_NAME]); + slant = &(tokens[SLANT]); + pixelsize = strToInt(&tokens[PIXEL_SIZE]); + size = strToInt(&tokens[POINT_SIZE]); if (useFamily) { name = wstrdup(useFamily); diff --git a/util/geticonset.c b/util/geticonset.c index e21994b0..2b793d5d 100644 --- a/util/geticonset.c +++ b/util/geticonset.c @@ -22,8 +22,13 @@ #define PROG_VERSION "geticonset (Window Maker) 0.1" -#include +#ifdef __GLIBC__ +#define _GNU_SOURCE /* getopt_long */ +#endif + +#include #include +#include #include #include @@ -32,13 +37,16 @@ extern char *__progname; -void print_help() +void print_help(int print_usage, int exitval) { - printf("Usage: %s [OPTIONS] [FILE]\n", __progname); - puts("Retrieves program icon configuration and output to FILE or to stdout"); - puts(""); - puts(" -h, --help display this help and exit"); - puts(" -v, --version output version information and exit"); + printf("Usage: %s [-h] [-v] [file]\n", __progname); + if (print_usage) { + puts("Retrieves program icon configuration and output to FILE or to stdout"); + puts(""); + puts(" -h, --help display this help and exit"); + puts(" -v, --version output version information and exit"); + } + exit(exitval); } int main(int argc, char **argv) @@ -46,24 +54,39 @@ int main(int argc, char **argv) WMPropList *window_name, *icon_key, *window_attrs, *icon_value; WMPropList *all_windows, *iconset, *keylist; char *path; - int i; + int i, ch; - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { - print_help(); - exit(0); - } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { - puts(PROG_VERSION); - exit(0); + struct option longopts[] = { + { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } + }; + + while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) + switch(ch) { + case 'v': + puts(PROG_VERSION); + return 0; + /* NOTREACHED */ + case 'h': + print_help(1, 0); + /* NOTREACHED */ + case 0: + break; + default: + print_help(0, 1); + /* NOTREACHED */ } - } + + argc -= optind; + argv += optind; path = wdefaultspathfordomain("WMWindowAttributes"); all_windows = WMReadPropListFromFile(path); if (!all_windows) { printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path); - exit(1); + return 1; } iconset = WMCreatePLDictionary(NULL, NULL); @@ -88,10 +111,10 @@ int main(int argc, char **argv) } } - if (argc == 2) { - WMWritePropListToFile(iconset, argv[1]); + if (argc == 1) { + WMWritePropListToFile(iconset, argv[0]); } else { puts(WMGetPropListDescription(iconset, True)); } - exit(0); + return 0; } diff --git a/util/getstyle.c b/util/getstyle.c index 78f9efcd..ab3a1be7 100644 --- a/util/getstyle.c +++ b/util/getstyle.c @@ -22,20 +22,25 @@ #define PROG_VERSION "getstyle (Window Maker) 0.6" +#ifdef __GLIBC__ +#define _GNU_SOURCE /* getopt_long */ +#endif + #include #include +#include #include #include -#include +#include +#include +#include +#include #include +#include #include #include -#include -#include -#include -#include -#include + #include #define RETRY( x ) do { \ @@ -132,15 +137,18 @@ char *ThemePath = NULL; extern char *convertFont(char *font, Bool keepXLFD); -void print_help() +void print_help(int print_usage, int exitval) { - printf("Usage: %s [OPTIONS] [FILE]\n", __progname); - puts("Retrieves style/theme configuration and output to FILE or to stdout"); - puts(""); - puts(" -t, --theme-options output theme related options when producing a style file"); - puts(" -p, --pack produce output as a theme pack"); - puts(" -h, --help display this help and exit"); - puts(" -v, --version output version information and exit"); + printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname); + if (print_usage) { + puts("Retrieves style/theme configuration and output to FILE or to stdout"); + puts(""); + puts(" -h, --help display this help and exit"); + puts(" -v, --version output version information and exit"); + puts(" -t, --theme-options output theme related options when producing a style file"); + puts(" -p, --pack produce output as a theme pack"); + } + exit(exitval); } void abortar(char *reason) @@ -342,44 +350,55 @@ int main(int argc, char **argv) { WMPropList *prop, *style, *key, *val; char *path, *p; - int i, theme_too = 0, make_pack = 0; + int i, ch, theme_too = 0, make_pack = 0; char *style_file = NULL; - if (argc > 1) { - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--pack") == 0) { + struct option longopts[] = { + { "pack", no_argument, NULL, 'p' }, + { "theme-options", no_argument, NULL, 't' }, + { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } + }; + + while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1) + switch(ch) { + case 'v': + puts(PROG_VERSION); + return 0; + /* NOTREACHED */ + case 'h': + print_help(1, 0); + /* NOTREACHED */ + case 'p': make_pack = 1; theme_too = 1; - } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--theme-options") == 0) { + break; + case 't': theme_too = 1; - } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { - print_help(); - exit(0); - } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { - puts(PROG_VERSION); - exit(0); - } else { - if (style_file != NULL) { - printf("%s: invalid argument '%s'\n", __progname, - style_file[0] == '-' ? style_file : argv[i]); - printf("Try '%s --help' for more information\n", __progname); - exit(1); - } - style_file = argv[i]; - while ((p = strchr(style_file, '/')) != NULL) - *p = '_'; - } + case 0: + break; + default: + print_help(0, 1); + /* NOTREACHED */ } - } - if (style_file && !make_pack) { - print_help(); - exit(1); - } + argc -= optind; + argv += optind; + + if (argc != 1) + print_help(0, 1); + + style_file = argv[0]; + while ((p = strchr(style_file, '/')) != NULL) + *p = '_'; + + if (style_file && !make_pack) /* what's this? */ + print_help(0, 1); if (make_pack && !style_file) { printf("%s: you must supply a name for the theme pack\n", __progname); - exit(1); + return 1; } WMPLSetCaseSensitive(False); @@ -389,7 +408,7 @@ int main(int argc, char **argv) prop = WMReadPropListFromFile(path); if (!prop) { printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path); - exit(1); + return 1; } /* get global value */ @@ -459,5 +478,5 @@ int main(int argc, char **argv) puts(WMGetPropListDescription(style, True)); } } - exit(0); + return 0; } diff --git a/util/seticons.c b/util/seticons.c index 96baf984..ffd38f4a 100644 --- a/util/seticons.c +++ b/util/seticons.c @@ -22,53 +22,67 @@ #define PROG_VERSION "seticons (Window Maker) 0.1" -#include +#ifdef __GLIBC__ +#define _GNU_SOURCE /* getopt_long */ +#endif + +#include #include +#include #include + #include #include "../src/wconfig.h" extern char *__progname; -void print_help() +void print_help(int print_usage, int exitval) { - printf("Usage: %s [OPTIONS] FILE\n", __progname); - puts("Reads icon configuration from FILE and updates Window Maker."); - puts(""); - puts(" -h, --help display this help and exit"); - puts(" -v, --version output version information and exit"); + printf("Usage: %s [-h] [-v] [file]\n", __progname); + if (print_usage) { + puts("Reads icon configuration from FILE and updates Window Maker."); + puts(""); + puts(" -h, --help display this help and exit"); + puts(" -v, --version output version information and exit"); + } + exit(exitval); } int main(int argc, char **argv) { WMPropList *window_name, *icon_key, *window_attrs, *icon_value; WMPropList *all_windows, *iconset, *keylist; - int i; + int i, ch; char *path = NULL; - if (argc < 2) { - printf("%s: missing argument\n", __progname); - printf("Try '%s --help' for more information\n", __progname); - exit(1); - } + struct option longopts[] = { + { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } + }; - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { - print_help(); - exit(0); - } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { - puts(PROG_VERSION); - exit(0); - } else { - if (path) { - printf("%s: invalid argument '%s'\n", __progname, argv[i]); - printf("Try '%s --help' for more information\n", __progname); - exit(1); - } - path = argv[i]; + while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) + switch(ch) { + case 'v': + puts(PROG_VERSION); + return 0; + /* NOTREACHED */ + case 'h': + print_help(1, 0); + /* NOTREACHED */ + case 0: + break; + default: + print_help(0, 1); + /* NOTREACHED */ } - } + + argc -= optind; + argv += optind; + + if (argc != 1) + print_help(0, 1); path = wdefaultspathfordomain("WMWindowAttributes"); @@ -78,9 +92,9 @@ int main(int argc, char **argv) exit(1); } - iconset = WMReadPropListFromFile(argv[1]); + iconset = WMReadPropListFromFile(argv[0]); if (!iconset) { - printf("%s: could not load icon set file \"%s\".\n", __progname, argv[1]); + printf("%s: could not load icon set file \"%s\".\n", __progname, argv[0]); exit(1); }