diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -CFLAGS = -Wall -g -O2 +CFLAGS = -Wall -Wextra -Wno-unused-parameter -g -O2 headers = $(wildcard src/*.h) sources = $(wildcard src/*.c) diff --git a/src/vcprompt.c b/src/vcprompt.c --- a/src/vcprompt.c +++ b/src/vcprompt.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -13,10 +14,12 @@ #include "bzr.h" */ +#define DEFAULT_FORMAT "[%n:%b%m%u] " + void parse_args(int argc, char** argv, options_t* options) { int opt; - while ((opt = getopt(argc, argv, "f:d")) != -1) { + while ((opt = getopt(argc, argv, "hf:d")) != -1) { switch (opt) { case 'f': options->format = optarg; @@ -24,6 +27,22 @@ case 'd': options->debug = 1; break; + case 'h': + default: + printf("usage: %s [-h] [-d] [-f FORMAT]\n", argv[0]); + printf("FORMAT (default=\"%s\") may contain:\n%s", + DEFAULT_FORMAT, + " %b show branch\n" + " %r show revision\n" + " %u show unknown\n" + " %m show modified\n" + " %n show VC name\n" + " %% show '%'\n" + ); + printf("Environment Variables:\n" + " VCPROMPT_FORMAT\n" + ); + exit(1); } } } @@ -37,7 +56,8 @@ options->show_modified = 0; char* format = options->format; - for (i = 0; i < strlen(format); i++) { + int len = strlen(format); + for (i = 0; i < len; i++) { if (format[i] == '%') { i++; switch (format[i]) { @@ -72,8 +92,9 @@ { int i; char* format = options->format; + int len = strlen(format); - for (i = 0; i < strlen(format); i++) { + for (i = 0; i < len; i++) { if (format[i] == '%') { i++; switch (format[i]) { @@ -149,11 +170,16 @@ int main(int argc, char** argv) { - options_t options = { 0, /* debug */ - "[%n:%b%m%u] ", /* format string */ - 0, /* show branch */ - 0, /* show unknown */ - 0, /* show local changes */ + char* format = getenv("VCPROMPT_FORMAT"); + if (format == NULL) + format = DEFAULT_FORMAT; + options_t options = { + .debug = 0, + .format = format, + .show_branch = 0, + .show_revision = 0, + .show_unknown = 0, + .show_modified = 0, }; parse_args(argc, argv, &options);