git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dario Rodriguez <soft.d4rio@gmail.com>
To: peff@peff.net, jrnieder@uchicago.edu
Cc: Dario Rodriguez <soft.d4rio@gmail.com>, git@vger.kernel.org
Subject: [PATCH/RFC] Fix for default pager
Date: Mon,  7 Jun 2010 20:58:08 -0300	[thread overview]
Message-ID: <1275955088-32750-1-git-send-email-soft.d4rio@gmail.com> (raw)
In-Reply-To: <Installing on AIX fails>

All commands using pager:

Default pager was 'less' even when some systems such AIX and other basic
or old systems do NOT have 'less' installed. In such case, git just
does not display anything in pager-enabled functionalities such as 'git log'
or 'git show', exiting with status 0.

With this patch, git will not use DEFAULT_PAGER macro anymore, instead,
git will look for 'less' and 'more' in the most common paths.
If there is no pager, returns NULL as if it's 'cat'.

Obviously, the commit message needs to be rewritten :p

Signed-off-by: Dario Rodriguez <soft.d4rio@gmail.com>
---
 pager.c |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 73 insertions(+), 7 deletions(-)

diff --git a/pager.c b/pager.c
index dac358f..3cfbd73 100644
--- a/pager.c
+++ b/pager.c
@@ -2,10 +2,19 @@
 #include "run-command.h"
 #include "sigchain.h"
 
-#ifndef DEFAULT_PAGER
-#define DEFAULT_PAGER "less"
+#ifndef GIT_PAGER_ENVIRONMENT
+#define GIT_PAGER_ENVIRONMENT "GIT_PAGER"
 #endif
 
+#ifndef GIT_PGR_LOOKUP_DBG_ENVIRONMENT
+#define GIT_PGR_LOOKUP_DBG_ENVIRONMENT "GIT_PAGER_LOOKUP_DEBUG"
+#endif
+
+#ifndef PAGER_ENVIRONMENT
+#define PAGER_ENVIRONMENT "PAGER"
+#endif
+
+
 /*
  * This is split up from the rest of git so that we can do
  * something different on Windows.
@@ -48,23 +57,80 @@ static void wait_for_pager_signal(int signo)
 	raise(signo);
 }
 
-const char *git_pager(int stdout_is_tty)
+static int is_executable(const char *name)
+{
+	struct stat st;
+
+	if (stat(name, &st) ||
+	    !S_ISREG(st.st_mode))
+		return 0;
+
+#ifdef WIN32
+{	/* cannot trust the executable bit, peek into the file instead */
+	char buf[3] = { 0 };
+	int n;
+	int fd = open(name, O_RDONLY);
+	st.st_mode &= ~S_IXUSR;
+	if (fd >= 0) {
+		n = read(fd, buf, 2);
+		if (n == 2)
+			/* DOS executables start with "MZ" */
+			if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
+				st.st_mode |= S_IXUSR;
+		close(fd);
+	}
+}
+#endif
+	return st.st_mode & S_IXUSR;
+}
+
+const char *git_pager(int stdout_is_tty) 
 {
+	static const char *pager_bins[] =
+		{ "less", "more", NULL };
+	static const char *common_binary_paths[] =
+		{ "/bin/","/usr/bin/","/usr/local/bin/",NULL };
+
 	const char *pager;
+	char **p1,**p2,*pager_heap;
+	char exe_name[255];
+	int pager_lkp_dbg=0;
 
 	if (!stdout_is_tty)
 		return NULL;
 
-	pager = getenv("GIT_PAGER");
+	if(getenv(GIT_PGR_LOOKUP_DBG_ENVIRONMENT))
+		pager_lkp_dbg=1;
+
+	memset( exe_name,0,255 );
+	pager = getenv(GIT_PAGER_ENVIRONMENT);
 	if (!pager) {
 		if (!pager_program)
 			git_config(git_default_config, NULL);
 		pager = pager_program;
 	}
 	if (!pager)
-		pager = getenv("PAGER");
-	if (!pager)
-		pager = DEFAULT_PAGER;
+		pager = getenv(PAGER_ENVIRONMENT);
+	if (!pager) {
+
+		for (p1=(char**)pager_bins; (*p1)&&(!pager)
+			;p1++)
+			for (p2=(char**)common_binary_paths; (*p2)&&(!pager)
+				;p2++) {
+				sprintf( exe_name,"%s%s",
+					 *p2,*p1 );
+				if (is_executable(exe_name)) {
+					pager_heap = (char*) malloc(sizeof(exe_name));
+					strcpy(pager_heap,exe_name);
+					pager = pager_heap;
+				}
+			}
+
+		if (pager_lkp_dbg)
+			fprintf(stderr, "Debug: Lookup for existent pagers, got [%s]\n",
+				(pager==NULL)?"(null)":pager);
+
+	}
 	else if (!*pager || !strcmp(pager, "cat"))
 		pager = NULL;
 
-- 
1.7.1.245.g7c42e.dirty

       reply	other threads:[~2010-06-07 23:58 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Installing on AIX fails>
2010-06-07 23:58 ` Dario Rodriguez [this message]
2010-06-08  0:04   ` [PATCH/RFC] Fix for default pager Ben Walton
2010-06-08  2:14     ` Dario Rodriguez
2010-06-08  5:35       ` Jeff King
2010-06-08 13:49         ` Dario Rodriguez
2010-06-08 14:17           ` Johannes Sixt
2010-06-08 14:39             ` Dario Rodriguez
2010-06-08 15:56               ` Johannes Sixt
2010-06-08 17:28                 ` Dario Rodriguez
2010-06-08 18:59                   ` Johannes Sixt
2010-06-08 20:44                     ` Dario Rodriguez
2010-06-08 21:33                       ` Andreas Ericsson
2010-06-09  9:08                         ` Tor Arntsen
2010-06-09  9:29                           ` Miles Bader
2010-06-10  8:29                           ` Jeff King
2010-06-10  8:48                             ` Tor Arntsen
2010-06-10  8:59                               ` Jeff King
2010-06-10  9:24                                 ` Tor Arntsen
2010-06-10 11:31                                   ` Dario Rodriguez
2010-06-10 14:55                                 ` Junio C Hamano
2010-06-15 16:11                                 ` Brandon Casey
2010-06-15 16:32                                   ` Tor Arntsen
2010-06-16  1:34                                   ` Nazri Ramliy
2010-06-16  6:28                                   ` Jeff King
2010-06-09 18:57                         ` Ævar Arnfjörð Bjarmason
2010-06-08  5:29   ` Jeff King
2010-06-08  6:12     ` Johannes Sixt
2010-06-08 12:24     ` Dario Rodriguez
2010-06-08 14:04       ` Erik Faye-Lund

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=1275955088-32750-1-git-send-email-soft.d4rio@gmail.com \
    --to=soft.d4rio@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@uchicago.edu \
    --cc=peff@peff.net \
    /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).