From: "Ramsay Jones" <ramsay@ramsay1.demon.co.uk>
To: <git@vger.kernel.org>
Subject: [PATCH 9/10] Remove cmd_usage() routine and re-organize the help/usage code.
Date: Wed, 2 Aug 2006 02:03:44 +0100 [thread overview]
Message-ID: <00c201c6b5cf$8089a580$c47eedc1@ramsay1.demon.co.uk> (raw)
[-- Attachment #1: Type: text/plain, Size: 4146 bytes --]
The cmd_usage() routine was causing warning messages due to a NULL
format parameter being passed in three out of four calls. This is a
problem if you want to compile with -Werror. A simple solution is to
simply remove the GNU __attribute__ format pragma from the cmd_usage()
declaration in the header file. The function interface was somewhat
muddled anyway, so re-write the code to finesse the problem.
Signed-off-by: Ramsay Allan Jones <ramsay@ramsay1.demon.co.uk>
---
builtin-help.c | 54
+++++++++++++++++++++++-------------------------------
builtin.h | 7 ++-----
git.c | 7 +++++--
3 files changed, 30 insertions(+), 38 deletions(-)
diff --git a/builtin-help.c b/builtin-help.c
index 7470faa..006da05 100644
--- a/builtin-help.c
+++ b/builtin-help.c
@@ -9,8 +9,6 @@ #include "builtin.h"
#include "exec_cmd.h"
#include "common-cmds.h"
-static const char git_usage[] =
- "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND
ARGS ]";
/* most gui terms set COLUMNS (although some don't export it) */
static int term_columns(void)
@@ -178,31 +176,6 @@ static void list_common_cmds_help(void)
puts("(use 'git help -a' to get a list of all installed git commands)");
}
-void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
-{
- if (fmt) {
- va_list ap;
-
- va_start(ap, fmt);
- printf("git: ");
- vprintf(fmt, ap);
- va_end(ap);
- putchar('\n');
- }
- else
- puts(git_usage);
-
- if (exec_path) {
- putchar('\n');
- if (show_all)
- list_commands(exec_path, "git-*");
- else
- list_common_cmds_help();
- }
-
- exit(1);
-}
-
static void show_man_page(const char *git_cmd)
{
const char *page;
@@ -221,6 +194,13 @@ static void show_man_page(const char *gi
execlp("man", "man", page, NULL);
}
+void help_unknown_cmd(const char *cmd)
+{
+ printf("git: '%s' is not a git-command\n\n", cmd);
+ list_common_cmds_help();
+ exit(1);
+}
+
int cmd_version(int argc, const char **argv, char **envp)
{
printf("git version %s\n", git_version_string);
@@ -230,12 +210,24 @@ int cmd_version(int argc, const char **a
int cmd_help(int argc, const char **argv, char **envp)
{
const char *help_cmd = argv[1];
- if (!help_cmd)
- cmd_usage(0, git_exec_path(), NULL);
- else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
- cmd_usage(1, git_exec_path(), NULL);
+ const char *exec_path = git_exec_path();
+
+ if (!help_cmd) {
+ printf("usage: %s\n\n", git_usage_string);
+ list_common_cmds_help();
+ exit(1);
+ }
+
+ else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
+ printf("usage: %s\n\n", git_usage_string);
+ if(exec_path)
+ list_commands(exec_path, "git-*");
+ exit(1);
+ }
+
else
show_man_page(help_cmd);
+
return 0;
}
diff --git a/builtin.h b/builtin.h
index 7bfff11..b6cf5be 100644
--- a/builtin.h
+++ b/builtin.h
@@ -5,12 +5,9 @@ #include <stdio.h>
#include <limits.h>
extern const char git_version_string[];
+extern const char git_usage_string[];
-void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
-#ifdef __GNUC__
- __attribute__((__format__(__printf__, 3, 4), __noreturn__))
-#endif
- ;
+extern void help_unknown_cmd(const char *cmd);
extern int cmd_help(int argc, const char **argv, char **envp);
extern int cmd_version(int argc, const char **argv, char **envp);
diff --git a/git.c b/git.c
index ca8961f..f414df9 100644
--- a/git.c
+++ b/git.c
@@ -14,6 +14,9 @@ #include "cache.h"
#include "builtin.h"
+const char git_usage_string[] =
+ "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
+
static void prepend_to_path(const char *dir, int len)
{
const char *old_path = getenv("PATH");
@@ -279,7 +282,7 @@ int main(int argc, const char **argv, ch
puts(git_exec_path());
exit(0);
}
- cmd_usage(0, NULL, NULL);
+ usage(git_usage_string);
}
argv[0] = cmd;
@@ -312,7 +315,7 @@ int main(int argc, const char **argv, ch
}
if (errno == ENOENT)
- cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
+ help_unknown_cmd(cmd);
fprintf(stderr, "Failed to run command '%s': %s\n",
cmd, strerror(errno));
--
1.4.1
[-- Attachment #2: P0009.TXT --]
[-- Type: text/plain, Size: 4412 bytes --]
From 1ce42e2b5b65b03657b3ca9a3b06dc97cc66573c Mon Sep 17 00:00:00 2001
From: Ramsay Allan Jones <ramsay@ramsay1.demon.co.uk>
Date: Sun, 30 Jul 2006 22:42:25 +0100
Subject: [PATCH 9/10] Remove cmd_usage() routine and re-organize the help/usage code.
The cmd_usage() routine was causing warning messages due to a NULL
format parameter being passed in three out of four calls. This is a
problem if you want to compile with -Werror. A simple solution is to
simply remove the GNU __attribute__ format pragma from the cmd_usage()
declaration in the header file. The function interface was somewhat
muddled anyway, so re-write the code to finesse the problem.
Signed-off-by: Ramsay Allan Jones <ramsay@ramsay1.demon.co.uk>
---
builtin-help.c | 54 +++++++++++++++++++++++-------------------------------
builtin.h | 7 ++-----
git.c | 7 +++++--
3 files changed, 30 insertions(+), 38 deletions(-)
diff --git a/builtin-help.c b/builtin-help.c
index 7470faa..006da05 100644
--- a/builtin-help.c
+++ b/builtin-help.c
@@ -9,8 +9,6 @@ #include "builtin.h"
#include "exec_cmd.h"
#include "common-cmds.h"
-static const char git_usage[] =
- "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
/* most gui terms set COLUMNS (although some don't export it) */
static int term_columns(void)
@@ -178,31 +176,6 @@ static void list_common_cmds_help(void)
puts("(use 'git help -a' to get a list of all installed git commands)");
}
-void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
-{
- if (fmt) {
- va_list ap;
-
- va_start(ap, fmt);
- printf("git: ");
- vprintf(fmt, ap);
- va_end(ap);
- putchar('\n');
- }
- else
- puts(git_usage);
-
- if (exec_path) {
- putchar('\n');
- if (show_all)
- list_commands(exec_path, "git-*");
- else
- list_common_cmds_help();
- }
-
- exit(1);
-}
-
static void show_man_page(const char *git_cmd)
{
const char *page;
@@ -221,6 +194,13 @@ static void show_man_page(const char *gi
execlp("man", "man", page, NULL);
}
+void help_unknown_cmd(const char *cmd)
+{
+ printf("git: '%s' is not a git-command\n\n", cmd);
+ list_common_cmds_help();
+ exit(1);
+}
+
int cmd_version(int argc, const char **argv, char **envp)
{
printf("git version %s\n", git_version_string);
@@ -230,12 +210,24 @@ int cmd_version(int argc, const char **a
int cmd_help(int argc, const char **argv, char **envp)
{
const char *help_cmd = argv[1];
- if (!help_cmd)
- cmd_usage(0, git_exec_path(), NULL);
- else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
- cmd_usage(1, git_exec_path(), NULL);
+ const char *exec_path = git_exec_path();
+
+ if (!help_cmd) {
+ printf("usage: %s\n\n", git_usage_string);
+ list_common_cmds_help();
+ exit(1);
+ }
+
+ else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
+ printf("usage: %s\n\n", git_usage_string);
+ if(exec_path)
+ list_commands(exec_path, "git-*");
+ exit(1);
+ }
+
else
show_man_page(help_cmd);
+
return 0;
}
diff --git a/builtin.h b/builtin.h
index 7bfff11..b6cf5be 100644
--- a/builtin.h
+++ b/builtin.h
@@ -5,12 +5,9 @@ #include <stdio.h>
#include <limits.h>
extern const char git_version_string[];
+extern const char git_usage_string[];
-void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
-#ifdef __GNUC__
- __attribute__((__format__(__printf__, 3, 4), __noreturn__))
-#endif
- ;
+extern void help_unknown_cmd(const char *cmd);
extern int cmd_help(int argc, const char **argv, char **envp);
extern int cmd_version(int argc, const char **argv, char **envp);
diff --git a/git.c b/git.c
index ca8961f..f414df9 100644
--- a/git.c
+++ b/git.c
@@ -14,6 +14,9 @@ #include "cache.h"
#include "builtin.h"
+const char git_usage_string[] =
+ "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
+
static void prepend_to_path(const char *dir, int len)
{
const char *old_path = getenv("PATH");
@@ -279,7 +282,7 @@ int main(int argc, const char **argv, ch
puts(git_exec_path());
exit(0);
}
- cmd_usage(0, NULL, NULL);
+ usage(git_usage_string);
}
argv[0] = cmd;
@@ -312,7 +315,7 @@ int main(int argc, const char **argv, ch
}
if (errno == ENOENT)
- cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
+ help_unknown_cmd(cmd);
fprintf(stderr, "Failed to run command '%s': %s\n",
cmd, strerror(errno));
--
1.4.1
next reply other threads:[~2006-08-02 1:03 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-08-02 1:03 Ramsay Jones [this message]
2006-08-02 13:21 ` [PATCH 9/10] Remove cmd_usage() routine and re-organize the help/usage code Martin Waitz
2006-08-02 18:18 ` Junio C Hamano
2006-08-03 18:26 ` Ramsay Jones
2006-08-03 19:30 ` Junio C Hamano
2006-08-04 4:33 ` Junio C Hamano
2006-08-02 18:47 ` Ramsay Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='00c201c6b5cf$8089a580$c47eedc1@ramsay1.demon.co.uk' \
--to=ramsay@ramsay1.demon.co.uk \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).