2018-02-18  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* cmdbuf.c (histfile_name) [DJGPPC]: If LFN support is available load
	.lesshst else _lesshst.  If LFN is not available load _lesshst.
	(histfile_name): Use CANONICALIZE_PATH to canonicalize HOME and
	LESSHISTFILE.

	* decode.c (init_cmds) [MSDOS_COMPILER, DJGPPC]: If BINDIR exists and
	LFN support is available load .sysless else _sysless.
	If SYSDIR exists load sysless as system wide lesskey file.  If SYSDIR
	does not exist load "c:\\_sysless".
	If LFN support ia available load .less else _less.  If LFN is not
	available load _less.

	* forwback.c (forw): Ignore any line shift that may still exist from
	the last line of the previous screenful.

	* less.h [MSDOS_COMPILER, DJGPPC]: For DJGPP define HAVE_LFN_SUPPORT,
	DIR_EXISTS, FILE_EXISTS, CANONICALIZE_PATH and STRIP_FULL_PATH_AND_EXTENSION.
	For all others the macros are no-ops.

	* lesskey.c (main): If LFN support available used ".less" else "_less".
	(parse_args): If LFN support available used ".lesskey" else "_lesskey".

	* lesskey.nro: Add DJGPP specific informations.

	* less.nro: Add DJGPP specific informations.

	* line.c [MSDOS_COMPILER, DJGPPC]: For DJGPP make (current shift)
	cshift public.
	(pdone): If the current line is finished, reset the current shift.

	* main.c (main): If edit is the used editor change the default prompt.
	(main): Use STRIP_FULL_PATH_AND_EXTENSION for argv[0].  Use CANONICALIZE_PATH
	to canonicalize all filenames from command line.

	* screen.c [DJGPPC]: New function clreol_maybe.  Clear to EOL, but only
	if in the last display line.





diff -aprNU5 less-530.orig/cmdbuf.c less-530/cmdbuf.c
--- less-530.orig/cmdbuf.c	2017-11-23 12:49:34 +0000
+++ less-530/cmdbuf.c	2018-02-24 16:34:10 +0000
@@ -1363,11 +1363,11 @@ histfile_name()
 	char *home;
 	char *name;
 	int len;
 	
 	/* See if filename is explicitly specified by $LESSHISTFILE. */
-	name = lgetenv("LESSHISTFILE");
+	name = CANONICALIZE_PATH(lgetenv("LESSHISTFILE"));
 	if (name != NULL && *name != '\0')
 	{
 		if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
 			/* $LESSHISTFILE == "-" means don't use a history file. */
 			return (NULL);
@@ -1377,11 +1377,11 @@ histfile_name()
 	/* See if history file is disabled in the build. */
 	if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
 		return (NULL);
 
 	/* Otherwise, file is in $HOME. */
-	home = lgetenv("HOME");
+	home = CANONICALIZE_PATH(lgetenv("HOME"));
 	if (home == NULL || *home == '\0')
 	{
 #if OS2
 		home = lgetenv("INIT");
 		if (home == NULL || *home == '\0')
@@ -1389,10 +1389,14 @@ histfile_name()
 			return (NULL);
 	}
 	len = (int) (strlen(home) + strlen(LESSHISTFILE) + 2);
 	name = (char *) ecalloc(len, sizeof(char));
 	SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
+#if defined(MSDOS_COMPILER) && MSDOS_COMPILER == DJGPPC
+	if (!HAVE_LFN_SUPPORT(name) || (HAVE_LFN_SUPPORT(name) && !FILE_EXISTS(name)))
+		SNPRINTF2(name, len, "%s/%s", home, "_lesshst");
+#endif
 	return (name);
 }
 
 /*
  * Read a .lesshst file and call a callback for each line in the file.
diff -aprNU5 less-530.orig/decode.c less-530/decode.c
--- less-530.orig/decode.c	2017-11-23 12:49:34 +0000
+++ less-530/decode.c	2018-02-24 16:34:10 +0000
@@ -334,21 +334,64 @@ init_cmds()
 	/*
 	 * For backwards compatibility,
 	 * try to add tables in the OLD system lesskey file.
 	 */
 #ifdef BINDIR
+#if defined(MSDOS_COMPILER) && MSDOS_COMPILER == DJGPPC
+	if (HAVE_LFN_SUPPORT(".sysless"))
+	{
+#if 0
+	  char file_name[FILENAME_MAX];
+	  strcpy(file_name, BINDIR);
+	  strcat(file_name, "/.sysless");
+	  add_hometable(NULL, FILE_EXISTS(file_name) ? file_name : BINDIR "/_sysless", 1);
+#else
+	  add_hometable(NULL, FILE_EXISTS(BINDIR "/.sysless") ? BINDIR "/.sysless" : BINDIR "/_sysless", 1);
+#endif
+	}
+	else
+	  add_hometable(NULL, BINDIR "/_sysless", 1);
+#else
 	add_hometable(NULL, BINDIR "/.sysless", 1);
 #endif
+#endif
 	/*
 	 * Try to add the tables in the system lesskey file.
 	 */
+#if defined(MSDOS_COMPILER) && MSDOS_COMPILER == DJGPPC
+	if (FILE_EXISTS(LESSKEYFILE_SYS))
+	  add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
+	else
+	  add_hometable("LESSKEY_SYSTEM", "c:\\_sysless", 1);
+#else
 	add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
+#endif
 	/*
 	 * Try to add the tables in the standard lesskey file "$HOME/.less".
 	 */
+#if defined(MSDOS_COMPILER) && MSDOS_COMPILER == DJGPPC
+	if (HAVE_LFN_SUPPORT(LESSKEYFILE))
+	{
+	  char file_name[FILENAME_MAX];
+	  char *home_path = getenv("HOME");
+	  if (home_path)
+	  {
+	    unsigned int i;
+	    for (i = 0; file_name[i] = home_path[i]; i++)
+	      ;
+	    if (i)
+	      file_name[++i] = '/';
+	  }
+	  strcat(file_name, LESSKEYFILE);
+	  add_hometable("LESSKEY", FILE_EXISTS(file_name) ? LESSKEYFILE : "_less", 0);
+	}
+	else
+	  add_hometable("LESSKEY", "_less", 0);
+#else
 	add_hometable("LESSKEY", LESSKEYFILE, 0);
 #endif
+#endif
 }
 
 /*
  * Add a command table.
  */
diff -aprNU5 less-530.orig/forwback.c less-530/forwback.c
--- less-530.orig/forwback.c	2017-11-23 12:49:34 +0000
+++ less-530/forwback.c	2018-02-24 16:34:10 +0000
@@ -155,10 +155,17 @@ forw(n, pos, force, only_last, nblank)
 	}
 #endif
 
 	if (!do_repaint)
 	{
+		/*
+		 * Forget any current line shift we might have (from
+		 * the last line of the previous screenful).
+		 */
+		extern int cshift;
+		cshift = 0;
+
 		if (top_scroll && n >= sc_height - 1 && pos != ch_length())
 		{
 			/*
 			 * Start a new screen.
 			 * {{ This is not really desirable if we happen
diff -aprNU5 less-530.orig/less.h less-530/less.h
--- less-530.orig/less.h	2017-11-23 12:49:34 +0000
+++ less-530/less.h	2018-02-24 16:34:10 +0000
@@ -539,5 +539,54 @@ struct hilite_tree;
 
 /* Functions not included in funcs.h */
 void postoa LESSPARAMS ((POSITION, char*));
 void linenumtoa LESSPARAMS ((LINENUM, char*));
 void inttoa LESSPARAMS ((int, char*));
+
+#if defined(MSDOS_COMPILER) && MSDOS_COMPILER == DJGPPC
+# undef  IS_DIR_SEPARATOR
+# define IS_DIR_SEPARATOR(c)  ((c) == '/' || (c) == '\\' || (c) == ':')
+# include <libc/unconst.h>
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#  define __gnuc_extension__  __extension__
+# else
+#  define __gnuc_extension__
+# endif
+# define STRIP_FULL_PATH_AND_EXTENSION(file_name)    \
+  (__gnuc_extension__                                \
+    ({                                               \
+      char *_dst, *_src;                             \
+      _dst = _src = unconst((file_name), char *);    \
+      while (*_src++)                                \
+        ;                                            \
+      while ((_src - _dst) && (*--_src != '.'))      \
+        ;                                            \
+      for (*_src = '\0'; (_src - _dst); _src--)      \
+        if (IS_DIR_SEPARATOR(*_src))                 \
+          break;                                     \
+      if (_src - _dst)                               \
+        while ((*_dst++ = *++_src))                  \
+          ;                                          \
+      (file_name);                                   \
+    })                                               \
+  )
+# define CANONICALIZE_PATH(path)                     \
+  (__gnuc_extension__                                \
+    ({                                               \
+      if ((path))                                    \
+      {                                              \
+        char *_p = unconst((path), char *);          \
+        for (; *_p; _p++)                            \
+          if (*_p == '\\')                           \
+            *_p = '/';                               \
+      }                                              \
+      (path);                                        \
+    })                                               \
+  )
+# define HAVE_LFN_SUPPORT(name)   ((pathconf((name), _PC_NAME_MAX) > 12) ? TRUE : FALSE)
+# define DIR_EXISTS(path)         (access((path), D_OK) == 0)
+# define FILE_EXISTS(name)        (access((name), F_OK) == 0)
+#else
+# define HAVE_LFN_SUPPORT(name)                    (TRUE)
+# define STRIP_FULL_PATH_AND_EXTENSION(file_name)  (file_name)
+# define CANONICALIZE_PATH(path)                   (path)
+#endif
diff -aprNU5 less-530.orig/less.nro less-530/less.nro
--- less-530.orig/less.nro	2017-11-23 12:49:34 +0000
+++ less-530/less.nro	2018-02-24 16:34:10 +0000
@@ -1061,10 +1061,16 @@ Otherwise,
 .I less
 looks in a standard place for the lesskey file:
 On Unix systems,
 .I less
 looks for a lesskey file called "$HOME/.less".
+On DJGPP systems,
+.I less
+looks for a lesskey file called "$HOME/.less" if LFS support is available,
+and if it is not found there, then looks for a lesskey file called "$HOME/_less".
+If none of both are found there, then looks for a lesskey file called "_less"
+in any directory specified in the PATH environment variable.
 On MS-DOS and Windows systems,
 .I less
 looks for a lesskey file called "$HOME/_less", and if it is not found there,
 then looks for a lesskey file called "_less" in any directory specified
 in the PATH environment variable.
@@ -1092,10 +1098,12 @@ looks in a standard place for the system
 On Unix systems, the system-wide lesskey file is /usr/local/etc/sysless.
 (However, if
 .I less
 was built with a different sysconf directory than /usr/local/etc,
 that directory is where the sysless file is found.)
+On DJGPP systems, the system-wide lesskey file is /dev/env/DJDIR/etc/sysless.
+If it is not found there then c:\sysless is used as the system-wide lesskey file.
 On MS-DOS and Windows systems, the system-wide lesskey file is c:\e_sysless.
 On OS/2 systems, the system-wide lesskey file is c:\esysless.ini.
 
 .SH "INPUT PREPROCESSOR"
 You may define an "input preprocessor" for
diff -aprNU5 less-530.orig/lesskey.c less-530/lesskey.c
--- less-530.orig/lesskey.c	2017-11-23 12:49:34 +0000
+++ less-530/lesskey.c	2018-02-24 16:34:10 +0000
@@ -334,11 +334,11 @@ parse_args(argc, argv)
 	 * Open the input file, or use DEF_LESSKEYINFILE if none specified.
 	 */
 	if (argc > 0)
 		infile = *argv;
 	else
-		infile = homefile(DEF_LESSKEYINFILE);
+		infile = homefile(HAVE_LFN_SUPPORT(DEF_LESSKEYINFILE) ? DEF_LESSKEYINFILE : "_lesskey");
 }
 
 /*
  * Initialize data structures.
  */
@@ -843,11 +843,11 @@ main(argc, argv)
 	}
 
 	if (outfile == NULL)
 		outfile = getenv("LESSKEY");
 	if (outfile == NULL)
-		outfile = homefile(LESSKEYFILE);
+		outfile = homefile(HAVE_LFN_SUPPORT(LESSKEYFILE) ? LESSKEYFILE : "_less");
 	if ((out = fopen(outfile, "wb")) == NULL)
 	{
 #if HAVE_PERROR
 		perror(outfile);
 #else
diff -aprNU5 less-530.orig/lesskey.nro less-530/lesskey.nro
--- less-530.orig/lesskey.nro	2017-11-23 12:49:34 +0000
+++ less-530/lesskey.nro	2018-02-24 16:34:10 +0000
@@ -16,10 +16,12 @@ is used to specify a set of key bindings
 The input file is a text file which describes the key bindings.
 If the input file is "-", standard input is read.
 If no input file is specified, a standard filename is used
 as the name of the input file, which depends on the system being used:
 On Unix systems, $HOME/.lesskey is used;
+on DJGPP systems, $HOME/.lesskey is used
+and if this fails $HOME/_lesskey is used;
 on MS-DOS systems, $HOME/_lesskey is used;
 and on OS/2 systems $HOME/lesskey.ini is used,
 or $INIT/lesskey.ini if $HOME is undefined.
 The output file is a binary file which is used by 
 .I less.
@@ -27,10 +29,12 @@ If no output file is specified,
 and the environment variable LESSKEY is set,
 the value of LESSKEY is used as the name of the output file.
 Otherwise, a standard filename is used as the name of the output file,
 which depends on the system being used:
 On Unix and OS-9 systems, $HOME/.less is used;
+on DJGPP systems, $HOME/.less is used
+and if this fails $HOME/_less is used;
 on MS-DOS systems, $HOME/_less is used;
 and on OS/2 systems, $HOME/less.ini is used,
 or $INIT/less.ini if $HOME is undefined.
 If the output file already exists,
 .I lesskey
diff -aprNU5 less-530.orig/line.c less-530/line.c
--- less-530.orig/line.c	2017-11-23 12:49:34 +0000
+++ less-530/line.c	2018-02-24 16:40:06 +0000
@@ -24,11 +24,15 @@
 
 static char *linebuf = NULL;	/* Buffer which holds the current output line */
 static char *attr = NULL;	/* Extension of linebuf to hold attributes */
 public int size_linebuf = 0;	/* Size of line buffer (and attr buffer) */
 
+#if defined(MSDOS_COMPILER) && MSDOS_COMPILER == DJGPPC
+public int cshift;		/* Current left-shift of output line buffer */
+#else
 static int cshift;		/* Current left-shift of output line buffer */
+#endif
 public int hshift;		/* Desired left-shift of output line buffer */
 public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
 public int ntabstops = 1;	/* Number of tabstops */
 public int tabdefault = 8;	/* Default repeated tabstops */
 public POSITION highest_hilite;	/* Pos of last hilite in file found so far */
@@ -160,11 +164,10 @@ prewind()
 {
 	curr = 0;
 	column = 0;
 	right_curr = 0;
 	right_column = 0;
-	cshift = 0;
 	overstrike = 0;
 	last_overstrike = AT_NORMAL;
 	mbc_buf_len = 0;
 	is_null_line = 0;
 	pendc = '\0';
@@ -1163,10 +1166,15 @@ pdone(endline, chopped, forw)
 		 */
 		add_linebuf(' ', AT_NORMAL, 1);
 		add_linebuf('\b', AT_NORMAL, -1);
 	}
 	set_linebuf(curr, '\0', AT_NORMAL);
+	/*
+	 * If we are done with this line, reset the current shift.
+	 */
+	if (endline)
+		cshift = 0;
 }
 
 /*
  *
  */
diff -aprNU5 less-530.orig/main.c less-530/main.c
--- less-530.orig/main.c	2017-11-23 12:49:34 +0000
+++ less-530/main.c	2018-02-24 16:34:10 +0000
@@ -76,10 +76,11 @@ main(argc, argv)
 	_response(&argc, &argv);
 	_wildcard(&argc, &argv);
 #endif
 
 	progname = *argv++;
+	progname = STRIP_FULL_PATH_AND_EXTENSION(progname);
 	argc--;
 
 	secure = 0;
 	s = lgetenv("LESSSECURE");
 	if (s != NULL && *s != '\0')
@@ -168,12 +169,16 @@ main(argc, argv)
 		if (editor == NULL || *editor == '\0')
 			editor = EDIT_PGM;
 	}
 	editproto = lgetenv("LESSEDIT");
 	if (editproto == NULL || *editproto == '\0')
+#if (MSDOS_COMPILER && MSDOS_COMPILER == DJGPPC)
+		editproto = (stricmp(editor, "edit") == 0) ? "%E %f" : "%E ?lm+%lm. %f";
+#else
 		editproto = "%E ?lm+%lm. %f";
 #endif
+#endif
 
 	/*
 	 * Call get_ifile with all the command line filenames
 	 * to "register" them with the ifile system.
 	 */
@@ -198,11 +203,11 @@ main(argc, argv)
 		init_textlist(&tlist, gfilename);
 		filename = NULL;
 		while ((filename = forw_textlist(&tlist, filename)) != NULL)
 		{
 			qfilename = shell_unquote(filename);
-			(void) get_ifile(qfilename, ifile);
+			(void) get_ifile(CANONICALIZE_PATH(qfilename), ifile);
 			free(qfilename);
 			ifile = prev_ifile(NULL_IFILE);
 		}
 		free(gfilename);
 #else
diff -aprNU5 less-530.orig/screen.c less-530/screen.c
--- less-530.orig/screen.c	2017-11-23 12:49:34 +0000
+++ less-530/screen.c	2018-02-24 16:34:10 +0000
@@ -101,14 +101,33 @@ static int flash_created = 0;
 #if MSDOS_COMPILER==BORLANDC
 static unsigned short *whitescreen;
 static int flash_created = 0;
 #endif
 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
+extern int top_scroll;
+	static void
+clreol_maybe()
+{
+	/* Clear to EOL, but only if in the last display line.  This
+	   is a kludgey way to work around display problems when color
+	   is switched at the rightmost column of the last display
+	   line.  The problem is that when the display is scrolled,
+	   the empty line added from below inherits the colors of the
+	   last character on the previous line.  */
+	if (top_scroll != OPT_ON) {
+		int x, y;
+		extern int sc_height;
+		ScreenGetCursor(&x, &y);
+		if (x == sc_height - 1)
+			clreol();
+	}
+}
 #define _settextposition(y,x)   gotoxy(x,y)
 #define _clearscreen(m)         clrscr()
 #define _outtext(s)             cputs(s)
-#define	SETCOLORS(fg,bg)	{ textcolor(fg); textbackground(bg); }
+#define	SETCOLORS(fg,bg)	{ textcolor(fg); textbackground(bg); \
+				  if (bg == nm_bg_color) clreol_maybe(); }
 extern int sc_height;
 #endif
 
 #if MSDOS_COMPILER==WIN32C
 struct keyRecord
