todo : chdir-long.c exclude.c

2016-02-06  Juan Manuel Guerrero <juan.guerrero@gmx.de>

	* lib/chdir-long.c: Include dosname.h.
	(find_non_slash): Use OS specific DIR_SEPARATOR_CHARS instead of
	hardcoded posix slash.
	(chdir_long): Acount for possible drive letter prefix.  Use OS specific
	DIR_SEPARATOR_CHARS instead of hardcoded posix slash.

	* lib/dosname.h: New macro DIR_SEPARATOR_CHARS defines charactes to be
	used by strspn calls.

	* lib/fchdir.c (_gl_register_fd) [__DJGPP__]: For speed reasons call
	access instead of stat.

	* lib/fdopendir.c (fdopendir_with_dup) [__DJGPP__]: DJGPP has no method
	to get the file desscriptor of an directory open with opendir.  So do
	not close the file descriptor associated to the directory.

	* lib/fts.c: Include dosname.h for ISSLASH nor IS_ABSOLUTE_FILE_NAME
	definition.
	(link_count_optimize_ok): Use ISSLASH instead of '/'.
	(fts_children): Use IS_ABSOLUTE_FILE_NAME instead of '/'.

	* lib/open.c: Include dosname.h for ISSLASH definition.
	(open): Check with ISSLASH.
	(rpl_open) [__DJGPP__]: Use access to determinate if given path is
	an existing directory and ISSLASH to allow for mixed backslash and
        slash as directory separator.

	* lib/openat-proc.c (openat_proc_name) [__DJGPP__]: DJGPP does not
	support openning of "/proc/self/fd".  Return always -1 even if
	"/proc/self/fd" is passed as filename to open; this may be a valid
	DOS file name in some application.

	* lib/progname.c [MSDOS]: Define new macro GET_LAST_SLASH to find
	the last directory separator character.  On DOS-like systems these
	are slash, backslash or colon.  For POSIX this is simple a slash.

	* src/dfa.c [__DJGPP__]:  Provide wctype, iswctype prototypes.

	* src/dosbuf.c:  No longer include config.h.
	[__DJGPP__]: Include pc.h for ScreenGetCursor prototype.
	(restore_blink_bit, screen_puts, msdos_screen_write, djgpp_grep_startup)
	[__DJGPP__]: New functions to support colorization without requiring
	ANSI.SYS driver (or its work-alike) to be loaded.  
	(msdos_screen_write): Added support to decode and execute erase part
	of the line to the right escape sequence (ESC-[K), that is used to
	initialize and finish SGR escape sequences.

	* src/grep.c [__DJGPP__]: New macro IS_TERMINAL defined.  If compiled
	with DJGPP do not check for the term environment variable to decided
	which value should color_option have.
	(main): Use CANONICALIZE_PATH, STRIP_FULL_PATH_AND_EXTENSION and
	IS_TERMINAL.
	Call setmatcher with appropriate argument if file name is [e|f]grep.

	* src/system.h [HAVE_DOS_FILE_NAMES]:  New macro CANONICALIZE_PATH.
	New macro STRIP_FULL_PATH_AND_EXTENSION to strip extension and full
	path from argv[0].  Are no-op for all other systems.












diff -aprNU5 grep-2.23.orig/lib/chdir-long.c grep-2.23/lib/chdir-long.c
--- grep-2.23.orig/lib/chdir-long.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/chdir-long.c	2016-02-06 02:49:22 +0100
@@ -25,10 +25,12 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
 #include <stdio.h>
 
+#include "dosname.h"
+
 #include "assure.h"
 
 #ifndef PATH_MAX
 # error "compile this file only if your system defines PATH_MAX"
 #endif
@@ -85,11 +87,11 @@ cdb_advance_fd (struct cd_buf *cdb, char
 
 /* Return a pointer to the first non-slash in S.  */
 static char * _GL_ATTRIBUTE_PURE
 find_non_slash (char const *s)
 {
-  size_t n_slash = strspn (s, "/");
+  size_t n_slash = strspn (s, DIR_SEPARATOR_CHARS);
   return (char *) s + n_slash;
 }
 
 /* This is a function much like chdir, but without the PATH_MAX limitation
    on the length of the directory name.  A significant difference is that
@@ -111,10 +113,11 @@ chdir_long (char *dir)
 {
   int e = chdir (dir);
   if (e == 0 || errno != ENAMETOOLONG)
     return e;
 
+  dir = dir + FILE_SYSTEM_PREFIX_LEN(dir);
   {
     size_t len = strlen (dir);
     char *dir_end = dir + len;
     struct cd_buf cdb;
     size_t n_leading_slash;
@@ -125,11 +128,11 @@ chdir_long (char *dir)
        must have failed and set errno to ENOENT.  */
     assure (0 < len);
     assure (PATH_MAX <= len);
 
     /* Count leading slashes.  */
-    n_leading_slash = strspn (dir, "/");
+    n_leading_slash = strspn (dir, DIR_SEPARATOR_CHARS);
 
     /* Handle any leading slashes as well as any name that matches
        the regular expression, m!^//hostname[/]*! .  Handling this
        prefix separately usually results in a single additional
        cdb_advance_fd call, but it's worthwhile, since it makes the
diff -aprNU5 grep-2.23.orig/lib/dosname.h grep-2.23/lib/dosname.h
--- grep-2.23.orig/lib/dosname.h	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/dosname.h	2016-02-06 02:49:22 +0100
@@ -31,13 +31,15 @@
           (_IS_DRIVE_LETTER ((Filename)[0]) && (Filename)[1] == ':' ? 2 : 0)
 # ifndef __CYGWIN__
 #  define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 1
 # endif
 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
+# define DIR_SEPARATOR_CHARS  "/\\"
 #else
 # define FILE_SYSTEM_PREFIX_LEN(Filename) 0
 # define ISSLASH(C) ((C) == '/')
+# define DIR_SEPARATOR_CHARS  "/"
 #endif
 
 #ifndef FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
 # define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0
 #endif
diff -aprNU5 grep-2.23.orig/lib/fchdir.c grep-2.23/lib/fchdir.c
--- grep-2.23.orig/lib/fchdir.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/fchdir.c	2016-02-06 02:49:22 +0100
@@ -132,11 +132,21 @@ _gl_register_fd (int fd, const char *fil
 {
   struct stat statbuf;
 
   assure (0 <= fd);
   if (REPLACE_OPEN_DIRECTORY
+#ifdef __DJGPP__
+      /*
+       *  Computation of stat is very expensive.
+       *  To determinate if PATH is an existing
+       *  directory, access on DOS platforms is
+       *  mostly a fast system call.
+       */
+      || (access (filename, D_OK) == 0))
+#else
       || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
+#endif
     {
       if (!ensure_dirs_slot (fd)
           || (dirs[fd].name = get_name (filename)) == NULL)
         {
           int saved_errno = errno;
diff -aprNU5 grep-2.23.orig/lib/fdopendir.c grep-2.23/lib/fdopendir.c
--- grep-2.23.orig/lib/fdopendir.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/fdopendir.c	2016-02-06 02:52:16 +0100
@@ -149,11 +149,21 @@ fdopendir_with_dup (int fd, int older_du
           dir = fdopendir_with_dup (fd, dupfd, cwd);
           saved_errno = errno;
         }
       else
         {
-          close (fd);
+#ifdef __DJGPP__
+          /*
+           *  DJGPP has no method to get the
+           *  the file descriptor of an open
+           *  directory, so do not close it.
+           *  If CWD != NULL we are doing
+           *  fchdir/opendir(".")/restore_cwd(CWD).
+           */
+          if (!cwd)
+#endif
+            close (fd);
           dir = fd_clone_opendir (dupfd, cwd);
           saved_errno = errno;
           if (! dir)
             {
               int fd1 = dup (dupfd);
diff -aprNU5 grep-2.23.orig/lib/fts.c grep-2.23/lib/fts.c
--- grep-2.23.orig/lib/fts.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/fts.c	2016-02-06 02:56:18 +0100
@@ -50,10 +50,14 @@
 static char sccsid[] = "@(#)fts.c       8.6 (Berkeley) 8/14/94";
 #endif /* LIBC_SCCS and not lint */
 
 #include "fts_.h"
 
+#if !defined(ISSLASH) || !defined(IS_ABSOLUTE_FILE_NAME)
+# include "dosname.h"
+#endif
+
 #if HAVE_SYS_PARAM_H || defined _LIBC
 # include <sys/param.h>
 #endif
 #ifdef _LIBC
 # include <include/sys/stat.h>
@@ -833,11 +837,11 @@ link_count_optimize_ok (FTSENT const *p)
 /*
  * Special case of "/" at the end of the file name so that slashes aren't
  * appended which would cause file names to be written as "....//foo".
  */
 #define NAPPEND(p)                                                      \
-        (p->fts_path[p->fts_pathlen - 1] == '/'                         \
+        (ISSLASH(p->fts_path[p->fts_pathlen - 1])                       \
             ? p->fts_pathlen - 1 : p->fts_pathlen)
 
 FTSENT *
 fts_read (register FTS *sp)
 {
@@ -1180,11 +1184,11 @@ fts_children (register FTS *sp, int inst
          * does its chdir to the root of a traversal, we can lose -- we need to
          * chdir into the subdirectory, and we don't know where the current
          * directory is, so we can't get back so that the upcoming chdir by
          * fts_read will work.
          */
-        if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
+        if (p->fts_level != FTS_ROOTLEVEL || IS_ABSOLUTE_FILE_NAME(p->fts_accpath) ||
             ISSET(FTS_NOCHDIR))
                 return (sp->fts_child = fts_build(sp, instr));
 
         if ((fd = diropen (sp, ".")) < 0)
                 return (sp->fts_child = NULL);
diff -aprNU5 grep-2.23.orig/lib/open.c grep-2.23/lib/open.c
--- grep-2.23.orig/lib/open.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/open.c	2016-02-06 03:03:30 +0100
@@ -25,10 +25,14 @@
 /* Get the original definition of open.  It might be defined as a macro.  */
 #include <fcntl.h>
 #include <sys/types.h>
 #undef __need_system_fcntl_h
 
+#ifndef ISSLASH
+# include "dosname.h"
+#endif
+
 static int
 orig_open (const char *filename, int flags, mode_t mode)
 {
   return open (filename, flags, mode);
 }
@@ -105,11 +109,11 @@ open (const char *filename, int flags, .
        - if O_WRONLY or O_RDWR is specified, open() must fail because the
          file does not contain a '.' directory.  */
   if (flags & (O_CREAT | O_WRONLY | O_RDWR))
     {
       size_t len = strlen (filename);
-      if (len > 0 && filename[len - 1] == '/')
+      if (len > 0 && ISSLASH(filename[len - 1]))
         {
           errno = EISDIR;
           return -1;
         }
     }
@@ -126,12 +130,22 @@ open (const char *filename, int flags, .
      dummy.  */
   if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
       && ((flags & O_ACCMODE) == O_RDONLY
           || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
     {
+#ifdef __DJGPP__
+      /*
+       *  Computation of stat is very expensive.
+       *  To determinate if PATH is an existing
+       *  directory, access on DOS platforms is
+       *  mostly a fast system call.
+       */
+      if (access (filename, D_OK) == 0)
+#else
       struct stat statbuf;
       if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
+#endif
         {
           /* Maximum recursion depth of 1.  */
           fd = open ("/dev/null", flags, mode);
           if (0 <= fd)
             fd = _gl_register_fd (fd, filename);
@@ -156,15 +170,25 @@ open (const char *filename, int flags, .
      with ENOTDIR.  */
   if (fd >= 0)
     {
       /* We know len is positive, since open did not fail with ENOENT.  */
       size_t len = strlen (filename);
-      if (filename[len - 1] == '/')
+      if (ISSLASH(filename[len - 1]))
         {
+#ifdef __DJGPP__
+          /*
+           *  Computation of stat is very expensive.
+           *  To determinate if PATH is an existing
+           *  directory, access on DOS platforms is
+           *  mostly a fast system call.
+           */
+          if (access (filename, D_OK) == 0)
+#else
           struct stat statbuf;
 
           if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
+#endif
             {
               close (fd);
               errno = ENOTDIR;
               return -1;
             }
diff -aprNU5 grep-2.23.orig/lib/openat-proc.c grep-2.23/lib/openat-proc.c
--- grep-2.23.orig/lib/openat-proc.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/openat-proc.c	2016-02-06 03:04:56 +0100
@@ -63,20 +63,24 @@ openat_proc_name (char buf[OPENAT_BUFFER
     };
 
     static int proc_status = 0;
     if (! proc_status)
       {
+#ifdef __DJGPP__
+        int proc_self_fd = -1;
+#else
         /* Set PROC_STATUS to a positive value if /proc/self/fd is
            reliable, and a negative value otherwise.  Solaris 10
            /proc/self/fd mishandles "..", and any file name might expand
            to ".." after symbolic link expansion, so avoid /proc/self/fd
            if it mishandles "..".  Solaris 10 has openat, but this
            problem is exhibited on code that built on Solaris 8 and
            running on Solaris 10.  */
 
         int proc_self_fd = open ("/proc/self/fd",
                                  O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
+#endif
         if (proc_self_fd < 0)
           proc_status = -1;
         else
           {
             /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the
diff -aprNU5 grep-2.23.orig/lib/progname.c grep-2.23/lib/progname.c
--- grep-2.23.orig/lib/progname.c	2016-02-03 06:46:26 +0100
+++ grep-2.23/lib/progname.c	2016-02-06 03:10:46 +0100
@@ -25,10 +25,44 @@
 #include <errno.h> /* get program_invocation_name declaration */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+/* MS-DOS and similar non-Posix systems have some peculiarities:
+    - they use both `/' and `\\' as directory separator in file names;
+    - they can have a drive letter X: prepended to a file name;
+   These are all parameterized here.  */
+
+#ifdef MSDOS
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#  define __gnuc_extension__  __extension__
+# else
+#  define __gnuc_extension__
+# endif
+# include <libc/unconst.h>
+# undef  IS_SLASH
+# define IS_SLASH(c)  ((c) == '/' || (c) == '\\' || (c) == ':')
+# define GET_LAST_SLASH(filename)              \
+  (__gnuc_extension__                          \
+    ({                                         \
+      char *_slash = NULL;                     \
+      if ((filename))                          \
+      {                                        \
+        _slash = unconst((filename), char *);  \
+        while (*_slash++)                      \
+          ;                                    \
+        while ((--_slash - (filename)))        \
+          if (IS_SLASH(*_slash))               \
+            break;                             \
+      }                                        \
+      _slash;                                  \
+    })                                         \
+  )
+#else
+# define GET_LAST_SLASH(filename)  (strrchr((filename), '/'))
+#endif
+
 
 /* String containing name the program is called with.
    To be initialized by main().  */
 const char *program_name = NULL;
 
@@ -54,13 +88,21 @@ set_program_name (const char *argv0)
       fputs ("A NULL argv[0] was passed through an exec system call.\n",
              stderr);
       abort ();
     }
 
-  slash = strrchr (argv0, '/');
+  slash = GET_LAST_SLASH (argv0);
   base = (slash != NULL ? slash + 1 : argv0);
-  if (base - argv0 >= 7 && strncmp (base - 7, "/.libs/", 7) == 0)
+  if (base - argv0 >= 7 && (strncmp (base - 7, "/.libs/", 7) == 0
+#ifdef MSDOS
+     || strncmp (base - 7, "\\.libs/", 7) == 0
+     || strncmp (base - 7, "\\.libs\\", 7) == 0
+     || strncmp (base - 7, "/_libs/", 7) == 0
+     || strncmp (base - 7, "\\_libs/", 7) == 0
+     || strncmp (base - 7, "\\_libs\\", 7) == 0
+#endif
+     ))
     {
       argv0 = base;
       if (strncmp (base, "lt-", 3) == 0)
         {
           argv0 = base + 3;
diff -aprNU5 grep-2.23.orig/src/dfa.c grep-2.23/src/dfa.c
--- grep-2.23.orig/src/dfa.c	2016-01-01 23:45:40 +0100
+++ grep-2.23/src/dfa.c	2016-02-06 03:12:08 +0100
@@ -48,10 +48,19 @@
 #define _(str) gettext (str)
 
 #include <wchar.h>
 #include <wctype.h>
 
+#ifdef __DJGPP__
+/*
+ *  Provided by the port because DJGPP still lacks multi-byte character support.
+ *  Prototypes for wctype, iswtype.
+ */
+wctype_t wctype(const char *property);
+int iswctype(wint_t wc, wctype_t charclass);
+#endif
+
 /* HPUX defines these as macros in sys/param.h.  */
 #ifdef setbit
 # undef setbit
 #endif
 #ifdef clrbit
diff -aprNU5 grep-2.23.orig/src/dosbuf.c grep-2.23/src/dosbuf.c
--- grep-2.23.orig/src/dosbuf.c	2016-01-01 23:45:40 +0100
+++ grep-2.23/src/dosbuf.c	2016-02-06 03:13:58 +0100
@@ -15,23 +15,24 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
    02110-1301, USA.  */
 
 /* Messy DOS-specific code for correctly treating binary, Unix text
-   and DOS text files.
+   and DOS text files, and for emulating a Posix terminal driver
+   wrt SGR (a.k.a. ANSI) color escape sequences.
 
    This has several aspects:
 
      * Guessing the file type (unless the user tells us);
      * Stripping CR characters from DOS text files (otherwise regex
        functions won't work correctly);
      * Reporting correct byte count with -b for any kind of file.
+     * Redirecting output with ANSI color commands to direct screen
+       writes.
 
 */
 
-#include <config.h>
-
 typedef enum {
   UNKNOWN, DOS_BINARY, DOS_TEXT, UNIX_TEXT
 } File_type;
 
 struct dos_map {
@@ -218,5 +219,347 @@ dossified_pos (off_t byteno)
         out_map_idx--;
     }
 
   return byteno + dos_pos_map[out_map_idx].add;
 }
+
+#ifdef __DJGPP__
+/*  Screen write redirection.  We need this to support colorization
+    without requiring ANSI.SYS driver (or its work-alike) to be loaded.
+
+    This function uses the DJGPP filesystem extensions mechanism.  It is
+    installed as a handler for handle-based functions (read/write/close)
+    for the standard output (but actually only handles writes, only if
+    the standard output is connected to the terminal, and only if user
+    asked for colorization).  When a buffer is written to the screen by
+    low-level functions of the DJGPP C library, our handler will be
+    called.  For any request that doesn't require colored screen writes
+    we return a zero to the caller, in which case the caller will handle
+    the output in the usual way (by eventually calling DOS).
+
+    When colorization *is* required, the buffer is written directly to
+    the screen while converting the ANSI escape sequences into calls to
+    DJGPP conio functions which change text attributes.  A non-zero value is
+    then returned to the caller to signal that the output has been handled.
+*/
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <unistd.h>
+#include <conio.h>
+#include <sys/fsext.h>
+#include <go32.h> /* for `_dos_ds' */
+#include <sys/farptr.h>
+#include <pc.h> /* for `ScreenGetCursor' */
+
+static int norm_blink = -1, cur_blink = -1;
+static unsigned char norm_attr = 0, cur_attr = 0;
+static int isatty_stdout = -1;
+static size_t leftover = 0;
+
+/* Restore the BIOS blinking bit to its original value.  Called at exit.  */
+static void
+restore_blink_bit (void)
+{
+  if (cur_blink != norm_blink)
+    {
+      if (norm_blink > 0)
+        blinkvideo ();
+      else
+        intensevideo ();
+    }
+}
+
+/* Write a buffer to the screen video memory.  This expands the TAB
+   characters to the appropriate number of spaces, and also does TRT
+   with null characters and other non-printable characters, if
+   any.  */
+static void
+screen_puts (char *buf, char *buf_end)
+{
+  register char *p = buf, *q = p;
+  int row, col;
+  unsigned char c;
+
+  while (p < buf_end)
+    {
+      if (*p < ' ')
+        {
+          switch (*p)
+            {
+              case '\b':
+              case '\r':
+              case '\n':
+                /* nothing: cputs already does TRT with these */
+                break;
+              case '\t':
+                *p = '\0';
+                cputs (q);
+                ScreenGetCursor (&row, &col);
+                for (cputs (" "), col += 1; col % 8; col++)
+                  cputs (" ");
+                q = p + 1;
+                *p = '\t';
+                break;
+              default:
+                c = *p;
+                *p = '\0';
+                cputs (q);
+                cputs ("^");
+                putch (c | 0x40);
+                q = p + 1;
+                *p = c;
+                break;
+            }
+        }
+      p++;
+    }
+  /* Output whatever is left.  */
+  cputs (q);
+}
+
+#define ESC '\033'
+#define IS_SGR(s) (((s)[1] == '[') && ((s)[2] == 'm'))
+#define IS_EL(s)  (((s)[1] == '[') && ((s)[2] == 'K'))
+
+/* Screen writes redirector function.  */
+static int
+msdos_screen_write (__FSEXT_Fnumber func, int *retval, va_list rest_args)
+{
+  static char *cbuf = NULL;
+  static size_t cbuf_len = 0;
+  /* Only dark colors mentioned here, so that bold has visible effect.  */
+  static enum COLORS screen_color[] = {
+    BLACK,
+    RED,
+    GREEN,
+    BROWN,
+    BLUE,
+    MAGENTA,
+    CYAN,
+    LIGHTGRAY
+  };
+  char *anchor, *p_next;
+  unsigned char fg, bg;
+
+  int handle;
+  char *buf, *buf_end;
+  size_t buflen;
+
+  /* Avoid direct screen writes unless colorization was actually requested.
+     Otherwise, we will break programs that catch I/O from their children.  */
+  handle = va_arg (rest_args, int);
+  if (!color_option || func != __FSEXT_write ||
+       !(handle == STDOUT_FILENO ? isatty_stdout : isatty (handle)))
+    return 0;
+
+  buf = va_arg (rest_args, char *);
+  if (!buf)
+    {
+      errno = EINVAL;
+      *retval = -1;
+      return 1;
+    }
+
+  /* Allocate a sufficiently large buffer to hold the output.  */
+  buflen = va_arg (rest_args, size_t);
+  if (!cbuf)
+    {
+      struct text_info txtinfo;
+
+      cbuf_len = buflen + 1;
+      cbuf = (char *)xmalloc (cbuf_len);
+      gettextinfo (&txtinfo);
+      norm_attr = txtinfo.attribute; /* save the original text attribute */
+      cur_attr = norm_attr;
+      /* Does it normally blink when bg has its 3rd bit set?  */
+      norm_blink = (_farpeekb (_dos_ds, 0x465) & 0x20) ? 1 : 0;
+      cur_blink = norm_blink;
+    }
+  else if (buflen >= cbuf_len)
+    {
+      cbuf_len = buflen + 1 + leftover;
+      cbuf = (char *)xrealloc (cbuf, cbuf_len);
+    }
+  memcpy (cbuf + leftover, buf, buflen);
+  buf_end = cbuf + buflen + leftover;
+  *buf_end = '\0';
+
+  /* Current text attributes are used as baseline.  */
+  fg = cur_attr & 15;
+  bg = (cur_attr >> 4) & 15;
+
+  /* Walk the buffer, writing text directly to video RAM,
+     changing color attributes when an escape sequence is seen.  */
+  for (anchor = p_next = cbuf;
+       (p_next = memchr (p_next, ESC, buflen - (p_next - cbuf))) != 0; )
+    {
+      char *p = p_next;
+
+      /* If some chars seen since the last escape sequence,
+         write it out to the screen using current text attributes.  */
+      if (p > anchor)
+        {
+          *p = '\0'; /* `cputs' needs ASCIIZ string */
+          screen_puts (anchor, p);
+          *p = ESC; /* restore the ESC character */
+          anchor = p;
+        }
+
+      /* Handle the null escape sequence (ESC-[m), which is used to
+         restore the original color. */
+      if (IS_SGR(p))
+        {
+          textattr (norm_attr);
+          p += 3;
+          anchor = p_next = p;
+          continue;
+        }
+
+      /* Handle the erase in line to the right escape sequence (ESC-[K). */
+      if (IS_EL(p))
+        {
+          clreol();
+          p += 3;
+          anchor = p_next = p;
+          continue;
+        }
+
+      if (p[1] == '[') /* "Esc-[" sequence */
+        {
+          p += 2; /* get past "Esc-[" sequence */
+          p_next = p;
+          while (*p != 'm') /* `m' ends the escape sequence */
+            {
+              char *q;
+              long code = strtol (p, &q, 10);
+
+              if (!*q)
+                {
+                  /* Incomplete escape sequence.  Remember the part
+                     we've seen for the next time.  */
+                  leftover = q - anchor;
+                  if (leftover >= cbuf_len)
+                    {
+                      cbuf_len += 1 + leftover;
+                      cbuf = (char *)xrealloc (cbuf, cbuf_len);
+                    }
+                  strcpy (cbuf, anchor);
+                  *retval = buflen; /* that's a lie, but we have to! */
+                  return 1;
+                }
+
+              /* 
+                 Sanity checks:
+
+                 q > p unless p doesn't point to a number;
+                 SGR codes supported by ANSI.SYS are between 0 and 49;
+                 Each SGR code ends with a `;' or an `m'.
+
+                 If any of the above is violated, we just ignore the bogon.
+              */
+              if (q == p || code > 49 || code < 0 || (*q != 'm' && *q != ';'))
+                {
+                  p_next = q;
+                  break;
+                }
+              if (*q == ';') /* more codes to follow */
+                q++;
+
+              /* Convert ANSI codes to color fore- and background.  */
+              switch (code)
+                {
+                  case 0: /* all attributes off */
+                    fg = norm_attr & 15;
+                    bg = (norm_attr >> 4) & 15;
+                    break;
+                  case 1: /* intensity on */
+                    fg |= 8;
+                    break;
+                  case 4: /* underline on */
+                    fg |= 8; /* we can't, so make it bold instead */
+                    break;
+                  case 5: /* blink */
+                    if (cur_blink != 1)
+                      {
+                        blinkvideo (); /* ensure we are'nt in bright bg mode */
+                        cur_blink = 1;
+                      }
+                    bg |= 8;
+                    break;
+                  case 7: /* reverse video */
+                    {
+                      unsigned char t = fg;
+                      fg = bg;
+                      bg = t;
+
+                      /* If it was blinking before, let it blink after.  */
+                      if (fg & 8)
+                        bg |= 8;
+
+                      /* If the fg was bold, let the background be bold.  */
+                      if ((t & 8) && cur_blink != 0)
+                        {
+                          intensevideo ();
+                          cur_blink = 0;
+                        }
+                    }
+                    break;
+                  case 8: /* concealed on */
+                    fg = (bg & 7) | 8; /* make fg be like bg, only bright */
+                    break;
+                  case 30: case 31: case 32: case 33: /* foreground color */
+                  case 34: case 35: case 36: case 37:
+                    fg = (fg & 8) | (screen_color[code - 30] & 15);
+                    break;
+                  case 40: case 41: case 42: case 43: /* background color */
+                  case 44: case 45: case 46: case 47:
+                    bg = (bg & 8) | (screen_color[code - 40] & 15);
+                    break;
+                  case 39: /* default fg */
+                    fg = norm_attr & 15;
+                    break;
+                  case 49:
+                    bg = (norm_attr >> 4) & 15;
+                    break;
+                  default:
+                    p_next = q; /* ignore unknown codes */
+                    break;
+                }
+              p = q;
+            } /* while loop */
+
+          if (*p == 'm' && p > p_next)
+            {
+              /* They don't *really* want it invisible, do they?  */
+              if (fg == (bg & 7))
+                fg |= 8; /* make it concealed instead */
+
+              /* Construct the text attribute and set it.  */
+              cur_attr = (bg << 4) | fg;
+              textattr (cur_attr);
+              p_next = anchor = p + 1;
+            }
+          else
+            break;
+        }
+      else
+        p_next++;
+    }  /* for loop */
+
+  /* Output what's left in the buffer.  */
+  screen_puts (anchor, buf_end);
+  leftover = 0;
+  *retval = buflen;
+  return 1;
+}
+
+/* This is called before `main' to install our STDOUT redirector.  */
+static void __attribute__((constructor))
+djgpp_grep_startup (void)
+{
+  __FSEXT_set_function (STDOUT_FILENO, msdos_screen_write);
+  isatty_stdout = isatty (STDOUT_FILENO);
+  atexit (restore_blink_bit);
+}
+#endif  /* __DJGPP__ */
diff -aprNU5 grep-2.23.orig/src/grep.c grep-2.23/src/grep.c
--- grep-2.23.orig/src/grep.c	2016-02-02 06:59:58 +0100
+++ grep-2.23/src/grep.c	2016-02-06 03:23:36 +0100
@@ -51,10 +51,16 @@
 
 #define SEP_CHAR_SELECTED ':'
 #define SEP_CHAR_REJECTED '-'
 #define SEP_STR_GROUP    "--"
 
+#ifdef __DJGPP__
+# define IS_TERMINAL     (isatty(STDOUT_FILENO))
+# define IS_EGREP(name)  (name[0] == 'e' && name[1] == 'g' && name[2] == 'r' && name[3] == 'e' && name[4] == 'p' && name[5] == '\0')
+# define IS_FGREP(name)  (name[0] == 'f' && name[1] == 'g' && name[2] == 'r' && name[3] == 'e' && name[4] == 'p' && name[5] == '\0')
+#endif
+
 #define AUTHORS \
   proper_name ("Mike Haertel"), \
   _("others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>")
 
 /* When stdout is connected to a regular file, save its stat
@@ -1782,12 +1788,12 @@ grep_command_line_arg (char const *arg)
       filename = label ? label : _("(standard input)");
       return grepdesc (STDIN_FILENO, true);
     }
   else
     {
-      filename = arg;
-      return grepfile (AT_FDCWD, arg, true, true);
+      filename = CANONICALIZE_PATH(arg);
+      return grepfile (AT_FDCWD, filename, true, true);
     }
 }
 
 _Noreturn void usage (int);
 void
@@ -2214,11 +2220,23 @@ main (int argc, char **argv)
   intmax_t default_context;
   FILE *fp;
   exit_failure = EXIT_TROUBLE;
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
-  program_name = argv[0];
+  program_name = STRIP_FULL_PATH_AND_EXTENSION(argv[0]);
+
+#ifdef __DJGPP__
+  /*
+   *  The shell scripts [e|f]grep will usually not work in DOS environments.
+   *  The scripts have been replaced by soft links that will call grep with
+   *  egrep or fgrep as argv[0].
+   */
+  if (IS_EGREP(program_name))
+    setmatcher ("egrep");
+  else if (IS_FGREP(program_name))
+    setmatcher ("fgrep");
+#endif
 
   keys = NULL;
   keycc = 0;
   with_filenames = false;
   eolbyte = '\n';
@@ -2353,11 +2371,11 @@ main (int argc, char **argv)
         keycc += cc;
         keys[keycc++] = '\n';
         break;
 
       case 'f':
-        fp = STREQ (optarg, "-") ? stdin : fopen (optarg, O_TEXT ? "rt" : "r");
+        fp = STREQ (optarg, "-") ? stdin : fopen (CANONICALIZE_PATH(optarg), O_TEXT ? "rt" : "r");
         if (!fp)
           error (EXIT_TROUBLE, errno, "%s", optarg);
         for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
           ;
         keys = xrealloc (keys, keyalloc);
@@ -2540,11 +2558,15 @@ main (int argc, char **argv)
         break;
 
       }
 
   if (color_option == 2)
+#ifdef IS_TERMINAL
+    color_option = (IS_TERMINAL) ? 1 : 0;
+#else
     color_option = isatty (STDOUT_FILENO) && should_colorize ();
+#endif
   init_colorize ();
 
   /* POSIX says that -q overrides -l, which in turn overrides the
      other output options.  */
   if (exit_on_match)
diff -aprNU5 grep-2.23.orig/src/system.h grep-2.23/src/system.h
--- grep-2.23.orig/src/system.h	2016-01-01 23:45:40 +0100
+++ grep-2.23/src/system.h	2016-02-06 03:17:44 +0100
@@ -34,10 +34,76 @@
 #include <stddef.h>
 #include <limits.h>
 #include <string.h>
 #include <ctype.h>
 
+#if O_BINARY
+# ifdef __DJGPP__
+/* MS-DOS and similar non-Posix systems have some peculiarities:
+    - they use both `/' and `\\' as directory separator in file names;
+    - they can have a drive letter X: prepended to a file name;
+   These are all parameterized here.  */
+
+#  if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#   define __gnuc_extension__  __extension__
+#  else
+#   define __gnuc_extension__
+#  endif
+#  include <libc/unconst.h>
+#  define IS_DIR_SEPARATOR(c)  ((c) == '/' || (c) == '\\' || (c) == ':')
+#  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 STRIP_EXTENSION(file_name)                   \
+   (__gnuc_extension__                                 \
+     ({                                                \
+        char *_begin, *_end;                           \
+        _begin = _end = unconst((file_name), char *);  \
+        while (*_end++)                                \
+          ;                                            \
+        while ((_end - _begin) && (*--_end != '.'))    \
+          ;                                            \
+        if (*_end == '.')                              \
+          *_end = '\0';                                \
+        (file_name);                                   \
+     })                                                \
+   )
+#  define CANONICALIZE_PATH(path)                      \
+   (__gnuc_extension__                                 \
+     ({                                                \
+        if ((path))                                    \
+        {                                              \
+          char *_p = unconst((path), char *);          \
+          for (; *_p; _p++)                            \
+            if (*_p == '\\')                           \
+              *_p = '/';                               \
+        }                                              \
+        (path);                                        \
+     })                                                \
+   )
+# else  /* !__DJGPP__  */
+#  define STRIP_FULL_PATH_AND_EXTENSION(file_name)  (file_name)
+#  define STRIP_EXTENSION(file_name)                (file_name)
+#  define CANONICALIZE_PATH(path)                   (path)
+# endif  /* !__DJGPP__  */
+#endif
+
 enum { EXIT_TROUBLE = 2 };
 
 #include <gettext.h>
 #define N_(String) gettext_noop(String)
 #define _(String) gettext(String)
