mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h
@ 2023-11-09 12:25 Ahmad Fatoum
  2023-11-09 12:25 ` [PATCH v2 2/3] complete: add support for spaces in completions Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2023-11-09 12:25 UTC (permalink / raw)
  To: barebox; +Cc: Christian Melki, Ahmad Fatoum

The implementation of the glob and fnmatch functions is taken from glibc
and it shows in the amount of #ifdefs. We don't need that in barebox and
can even get the glob_t size down by dropping unused members, so let's
do that.

Cc: Christian Melki <christian.melki@t2data.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 include/fnmatch.h |  58 ++-----------
 include/glob.h    | 201 +++++-----------------------------------------
 lib/Makefile      |   2 +-
 lib/fnmatch.c     |  45 +----------
 lib/glob.c        | 105 ++++--------------------
 5 files changed, 45 insertions(+), 366 deletions(-)

diff --git a/include/fnmatch.h b/include/fnmatch.h
index c13beb9011fb..1bc2cf87391a 100644
--- a/include/fnmatch.h
+++ b/include/fnmatch.h
@@ -4,69 +4,21 @@
 */
 
 #ifndef	_FNMATCH_H
-#define	_FNMATCH_H	1
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-#if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
-# if !defined __GLIBC__ || !defined __P
-#  undef	__P
-#  define __P(protos)	protos
-# endif
-#else /* Not C++ or ANSI C.  */
-# undef	__P
-# define __P(protos)	()
-/* We can get away without defining `const' here only because in this file
-   it is used only inside the prototype for `fnmatch', which is elided in
-   non-ANSI C where `const' is problematical.  */
-#endif /* C++ or ANSI C.  */
-
-#ifndef const
-# if (defined __STDC__ && __STDC__) || defined __cplusplus
-#  define __const	const
-# else
-#  define __const
-# endif
-#endif
-
-/* We #undef these before defining them because some losing systems
-   (HP-UX A.08.07 for example) define these in <unistd.h>.  */
-#undef	FNM_PATHNAME
-#undef	FNM_NOESCAPE
-#undef	FNM_PERIOD
+#define	_FNMATCH_H
 
 /* Bits set in the FLAGS argument to `fnmatch'.  */
 #define	FNM_PATHNAME	(1 << 0) /* No wildcard can ever match `/'.  */
 #define	FNM_NOESCAPE	(1 << 1) /* Backslashes don't quote special chars.  */
 #define	FNM_PERIOD	(1 << 2) /* Leading `.' is matched only explicitly.  */
-
-#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 2 || defined _GNU_SOURCE
-# define FNM_FILE_NAME	 FNM_PATHNAME	/* Preferred GNU name.  */
-# define FNM_LEADING_DIR (1 << 3)	/* Ignore `/...' after a match.  */
-# define FNM_CASEFOLD	 (1 << 4)	/* Compare without regard to case.  */
-# define FNM_EXTMATCH	 (1 << 5)	/* Use ksh-like extended matching. */
-#endif
+#define FNM_FILE_NAME	 FNM_PATHNAME	/* Preferred GNU name.  */
+#define FNM_LEADING_DIR	(1 << 3)	/* Ignore `/...' after a match.  */
+#define FNM_CASEFOLD	(1 << 4)	/* Compare without regard to case.  */
 
 /* Value returned by `fnmatch' if STRING does not match PATTERN.  */
 #define	FNM_NOMATCH	1
 
-/* This value is returned if the implementation does not support
-   `fnmatch'.  Since this is not the case here it will never be
-   returned but the conformance test suites still require the symbol
-   to be defined.  */
-#ifdef _XOPEN_SOURCE
-# define FNM_NOSYS	(-1)
-#endif
-
 /* Match NAME against the filename pattern PATTERN,
    returning zero if it matches, FNM_NOMATCH if not.  */
-extern int fnmatch __P ((__const char *__pattern, __const char *__name,
-			 int __flags));
-
-#ifdef	__cplusplus
-}
-#endif
+extern int fnmatch(const char *pattern, const char *name, int flags);
 
 #endif /* fnmatch.h */
diff --git a/include/glob.h b/include/glob.h
index ec0ac66f8765..67816c9cf0ad 100644
--- a/include/glob.h
+++ b/include/glob.h
@@ -4,54 +4,9 @@
 */
 
 #ifndef	_GLOB_H
-#define	_GLOB_H	1
+#define	_GLOB_H
 
-#define _FILE_OFFSET_BITS 32
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-#undef	__ptr_t
-#if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
-# if !defined __GLIBC__ || !defined __P
-#  undef __P
-#  undef __PMT
-#  define __P(protos)	protos
-#  define __PMT(protos)	protos
-#  if !defined __GNUC__ || __GNUC__ < 2
-#   undef __const
-#   define __const const
-#  endif
-# endif
-# define __ptr_t	void *
-#else /* Not C++ or ANSI C.  */
-# undef	__P
-# undef __PMT
-# define __P(protos)	()
-# define __PMT(protos)	()
-# undef	__const
-# define __const
-# define __ptr_t	char *
-#endif /* C++ or ANSI C.  */
-
-/* We need `size_t' for the following definitions.  */
-#ifndef __size_t
-# if defined __GNUC__ && __GNUC__ >= 2
-typedef __SIZE_TYPE__ __size_t;
-#  ifdef _XOPEN_SOURCE
-typedef __SIZE_TYPE__ size_t;
-#  endif
-# else
-/* This is a guess.  */
-typedef unsigned long int __size_t;
-# endif
-#else
-/* The GNU CC stddef.h version defines __size_t as empty.  We need a real
-   definition.  */
-# undef __size_t
-# define __size_t size_t
-#endif
+#include <linux/types.h>
 
 /* Bits set in the FLAGS argument to `glob'.  */
 #define	GLOB_ERR	(1 << 0)/* Return on read errors.  */
@@ -62,163 +17,49 @@ typedef unsigned long int __size_t;
 #define	GLOB_APPEND	(1 << 5)/* Append to results of a previous call.  */
 #define	GLOB_NOESCAPE	(1 << 6)/* Backslashes don't quote metacharacters.  */
 #define	GLOB_PERIOD	(1 << 7)/* Leading `.' can be matched by metachars.  */
-
-#if (!defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 2 || defined _BSD_SOURCE \
-     || defined _GNU_SOURCE)
-# define GLOB_MAGCHAR	 (1 << 8)/* Set in gl_flags if any metachars seen.  */
-# define GLOB_ALTDIRFUNC (1 << 9)/* Use gl_opendir et al functions.  */
-# define GLOB_BRACE	 (1 << 10)/* Expand "{a,b}" to "a" "b".  */
-# define GLOB_NOMAGIC	 (1 << 11)/* If no magic chars, return the pattern.  */
-# define GLOB_TILDE	 (1 << 12)/* Expand ~user and ~ to home directories. */
-# define GLOB_ONLYDIR	 (1 << 13)/* Match only directories.  */
-# define GLOB_TILDE_CHECK (1 << 14)/* Like GLOB_TILDE but return an error
-				      if the user name is not available.  */
-# define __GLOB_FLAGS	(GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_DOOFFS| \
-			 GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_APPEND|     \
-			 GLOB_PERIOD|GLOB_ALTDIRFUNC|GLOB_BRACE|     \
-			 GLOB_NOMAGIC|GLOB_TILDE|GLOB_ONLYDIR|GLOB_TILDE_CHECK)
-#else
+#define	GLOB_MAGCHAR	 (1 << 8)/* Set in gl_flags if any metachars seen.  */
 # define __GLOB_FLAGS	(GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_DOOFFS| \
 			 GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_APPEND|     \
 			 GLOB_PERIOD)
-#endif
 
 /* Error returns from `glob'.  */
 #define	GLOB_NOSPACE	1	/* Ran out of memory.  */
 #define	GLOB_ABORTED	2	/* Read error.  */
 #define	GLOB_NOMATCH	3	/* No matches found.  */
 #define GLOB_NOSYS	4	/* Not implemented.  */
-#ifdef _GNU_SOURCE
-/* Previous versions of this file defined GLOB_ABEND instead of
-   GLOB_ABORTED.  Provide a compatibility definition here.  */
-# define GLOB_ABEND GLOB_ABORTED
-#endif
+#define GLOB_ABEND	GLOB_ABORTED
 
-/* Structure describing a globbing run.  */
-#if !defined _AMIGA && !defined VMS /* Buggy compiler.   */
-# ifdef _GNU_SOURCE
-struct stat;
-# endif
-#endif
-typedef struct
-  {
-    __size_t gl_pathc;		/* Count of paths matched by the pattern.  */
-    char **gl_pathv;		/* List of matched pathnames.  */
-    __size_t gl_offs;		/* Slots to reserve in `gl_pathv'.  */
-    int gl_flags;		/* Set to FLAGS, maybe | GLOB_MAGCHAR.  */
+typedef struct {
+	size_t gl_pathc;		/* Count of paths matched by the pattern.  */
+	char **gl_pathv;		/* List of matched pathnames.  */
+	size_t gl_offs;		/* Slots to reserve in `gl_pathv'.  */
+	int gl_flags;		/* Set to FLAGS, maybe | GLOB_MAGCHAR.  */
+} glob_t;
 
-    /* If the GLOB_ALTDIRFUNC flag is set, the following functions
-       are used instead of the normal file access functions.  */
-    void (*gl_closedir) __PMT ((void *));
-#ifdef _GNU_SOURCE
-    struct dirent *(*gl_readdir) __PMT ((void *));
+#ifdef CONFIG_GLOB
+extern int glob (const char *__restrict pattern, int flags,
+		      int (*errfunc) (const char *, int),
+		      glob_t *__restrict pglob);
+
+extern void globfree(glob_t *pglob);
 #else
-    void *(*gl_readdir) __PMT ((void *));
-#endif
-    __ptr_t (*gl_opendir) __PMT ((__const char *));
-#ifdef _GNU_SOURCE
-    int (*gl_lstat) __PMT ((__const char *__restrict,
-			    struct stat *__restrict));
-    int (*gl_stat) __PMT ((__const char *__restrict, struct stat *__restrict));
-#else
-    int (*gl_lstat) __PMT ((__const char *__restrict, void *__restrict));
-    int (*gl_stat) __PMT ((__const char *__restrict, void *__restrict));
-#endif
-  } glob_t;
-
-#ifdef _LARGEFILE64_SOURCE
-# ifdef _GNU_SOURCE
-struct stat64;
-# endif
-typedef struct
-  {
-    __size_t gl_pathc;
-    char **gl_pathv;
-    __size_t gl_offs;
-    int gl_flags;
-
-    /* If the GLOB_ALTDIRFUNC flag is set, the following functions
-       are used instead of the normal file access functions.  */
-    void (*gl_closedir) __PMT ((void *));
-# ifdef _GNU_SOURCE
-    struct dirent64 *(*gl_readdir) __PMT ((void *));
-# else
-    void *(*gl_readdir) __PMT ((void *));
-# endif
-    __ptr_t (*gl_opendir) __PMT ((__const char *));
-# ifdef _GNU_SOURCE
-    int (*gl_lstat) __PMT ((__const char *__restrict,
-			    struct stat64 *__restrict));
-    int (*gl_stat) __PMT ((__const char *__restrict,
-			   struct stat64 *__restrict));
-# else
-    int (*gl_lstat) __PMT ((__const char *__restrict, void *__restrict));
-    int (*gl_stat) __PMT ((__const char *__restrict, void *__restrict));
-# endif
-  } glob64_t;
-#endif
-
-#if _FILE_OFFSET_BITS == 64 && __GNUC__ < 2
-# define glob glob64
-# define globfree globfree64
-#endif
-
-/* Do glob searching for PATTERN, placing results in PGLOB.
-   The bits defined above may be set in FLAGS.
-   If a directory cannot be opened or read and ERRFUNC is not nil,
-   it is called with the pathname that caused the error, and the
-   `errno' value from the failing call; if it returns non-zero
-   `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
-   If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
-   Otherwise, `glob' returns zero.  */
-#if _FILE_OFFSET_BITS != 64 || __GNUC__ < 2
-#if defined CONFIG_GLOB || defined CONFIG_FAKE_GLOB
-extern int glob __P ((__const char *__restrict __pattern, int __flags,
-		      int (*__errfunc) (__const char *, int),
-		      glob_t *__restrict __pglob));
-
-extern void globfree __P ((glob_t *__pglob));
-#else
-static inline int glob __P ((__const char *__restrict __pattern, int __flags,
-		      int (*__errfunc) (__const char *, int),
-		      glob_t *__restrict __pglob))
+static inline int glob(const char *__restrict pattern, int flags,
+		      int (*errfunc) (const char *, int),
+		      glob_t *__restrict pglob)
 {
 	return GLOB_ABORTED;
 }
 
-static inline void globfree __P ((glob_t *__pglob))
+static inline void globfree(glob_t *pglob)
 {
 }
 #endif
-/* Free storage allocated in PGLOB by a previous `glob' call.  */
-#else
-extern int glob __P ((__const char *__restrict __pattern, int __flags,
-		      int (*__errfunc) (__const char *, int),
-		      glob_t *__restrict __pglob)) __asm__ ("glob64");
 
-extern void globfree __P ((glob_t *__pglob)) __asm__ ("globfree64");
-#endif
-
-#ifdef _LARGEFILE64_SOURCE
-extern int glob64 __P ((__const char *__restrict __pattern, int __flags,
-			int (*__errfunc) (__const char *, int),
-			glob64_t *__restrict __pglob));
-
-extern void globfree64 __P ((glob64_t *__pglob));
-#endif
-
-
-#ifdef _GNU_SOURCE
 /* Return nonzero if PATTERN contains any metacharacters.
    Metacharacters can be quoted with backslashes if QUOTE is nonzero.
 
    This function is not part of the interface specified by POSIX.2
    but several programs want to use it.  */
-extern int glob_pattern_p __P ((__const char *__pattern, int __quote));
-#endif
-
-#ifdef	__cplusplus
-}
-#endif
+extern int glob_pattern_p(const char *pattern, int quote);
 
 #endif /* glob.h  */
diff --git a/lib/Makefile b/lib/Makefile
index 8817b5f47bac..9bb871f94f71 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -38,7 +38,7 @@ obj-$(CONFIG_CMDLINE_EDITING)	+= readline.o
 obj-$(CONFIG_SIMPLE_READLINE)	+= readline_simple.o
 obj-$(CONFIG_FNMATCH)		+= fnmatch.o
 obj-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
-obj-y			+= glob.o
+obj-$(CONFIG_GLOB)	+= glob.o
 obj-y			+= notifier.o
 obj-y			+= random.o
 obj-$(CONFIG_IMAGE_SPARSE) += image-sparse.o
diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 0ab530d3b182..ac87ba76219c 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -1,50 +1,11 @@
-/* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
-
-/* Enable GNU extensions in fnmatch.h.  */
-#ifndef _GNU_SOURCE
-# define _GNU_SOURCE	1
-#endif
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// SPDX-FileCopyrightText: (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
 
 #include <errno.h>
 #include <fnmatch.h>
 #include <linux/ctype.h>
 
-
-/* Comment out all this code if we are using the GNU C Library, and are not
-   actually compiling the library itself.  This code is part of the GNU C
-   Library, but also included in many other GNU distributions.  Compiling
-   and linking in this code is a waste when using the GNU C library
-   (especially if it is a shared library).  Rather than having every GNU
-   program understand `configure --with-gnu-libc' and omit the object files,
-   it is simpler to just do this in the source for each such file.  */
-
-# if defined (STDC_HEADERS) || !defined (isascii)
-#  define ISASCII(c) 1
-# else
-#  define ISASCII(c) isascii(c)
-# endif
-
-# define ISUPPER(c) (ISASCII (c) && isupper (c))
-
-
-# ifndef errno
-extern int errno;
-# endif
+# define ISUPPER(c) (isascii(c) && isupper(c))
 
 /* Match STRING against the filename pattern PATTERN, returning zero if
    it matches, nonzero if not.  */
diff --git a/lib/glob.c b/lib/glob.c
index 8523bad9a7ef..389580b0ed79 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -1,19 +1,5 @@
-/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// SPDX-FileCopyrightText: (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
 
 #include <common.h>
 #include <errno.h>
@@ -23,25 +9,15 @@ Cambridge, MA 02139, USA.  */
 #include <xfuncs.h>
 #include <fnmatch.h>
 #include <qsort.h>
-#define _GNU_SOURCE
 #include <glob.h>
 
-#ifdef CONFIG_GLOB
-
-extern __ptr_t(*__glob_opendir_hook) __P((const char *directory));
-extern void (*__glob_closedir_hook) __P((__ptr_t stream));
-extern const char *(*__glob_readdir_hook) __P((__ptr_t stream));
-
-static int glob_in_dir __P((const char *pattern, const char *directory,
+static int glob_in_dir (const char *pattern, const char *directory,
 			    int flags,
-			    int (*errfunc) __P((const char *, int)),
-			    glob_t * pglob));
-static int prefix_array __P((const char *prefix, char **array, size_t n,
-			     int add_slash));
+			    int (*errfunc) (const char *, int),
+			    glob_t * pglob);
+static int prefix_array (const char *prefix, char **array, size_t n,
+			     int add_slash);
 
-#ifdef __GLOB64
-extern int glob_pattern_p(const char *pattern, int quote);
-#else
 /* Return nonzero if PATTERN contains any metacharacters.
    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
 int glob_pattern_p(const char *pattern, int quote)
@@ -72,7 +48,6 @@ int glob_pattern_p(const char *pattern, int quote)
 
 	return 0;
 }
-#endif
 
 #ifdef CONFIG_GLOB_SORT
 /* Do a collated comparison of A and B.  */
@@ -100,7 +75,7 @@ static int collated_compare(const void *a, const void *b)
    If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
    Otherwise, `glob' returns zero.  */
 int glob(const char *pattern, int flags,
-		int (*errfunc) __P((const char *, int)), glob_t *pglob)
+		int (*errfunc) (const char *, int), glob_t *pglob)
 {
 	const char *filename;
 	char *dirname = NULL;
@@ -169,19 +144,6 @@ int glob(const char *pattern, int flags,
 		for (i = 0; i < dirs.gl_pathc; ++i) {
 			int oldcount1;
 
-#ifdef	SHELL
-			{
-				/* Make globbing interruptible in the bash shell. */
-				extern int interrupt_state;
-
-				if (interrupt_state) {
-					globfree(&dirs);
-					globfree(&files);
-					status = GLOB_ABEND goto out;
-				}
-			}
-#endif				/* SHELL.  */
-
 			oldcount1 = pglob->gl_pathc;
 			status = glob_in_dir(filename, dirs.gl_pathv[i],
 					     (flags | GLOB_APPEND) &
@@ -263,7 +225,7 @@ int glob(const char *pattern, int flags,
 #ifdef CONFIG_GLOB_SORT
 	if (!(flags & GLOB_NOSORT))
 		/* Sort the vector.  */
-		qsort((__ptr_t) & pglob->gl_pathv[oldcount],
+		qsort(&pglob->gl_pathv[oldcount],
 		      pglob->gl_pathc - oldcount,
 		      sizeof(char *), collated_compare);
 #endif
@@ -299,7 +261,7 @@ static int prefix_array(const char *dirname, char **array, size_t n,
 		memcpy(new, dirname, dirlen);
 		new[dirlen] = '/';
 		memcpy(&new[dirlen + 1], array[i], eltlen);
-		free((__ptr_t) array[i]);
+		free(array[i]);
 		array[i] = new;
 	}
 
@@ -311,9 +273,9 @@ static int prefix_array(const char *dirname, char **array, size_t n,
    The GLOB_NOSORT bit in FLAGS is ignored.  No sorting is ever done.
    The GLOB_APPEND flag is assumed to be set (always appends).  */
 static int glob_in_dir(const char *pattern, const char *directory,
-		int flags, int (*errfunc) __P((const char *, int)), glob_t *pglob)
+		int flags, int (*errfunc) (const char *, int), glob_t *pglob)
 {
-	__ptr_t stream = NULL;
+	void *stream = NULL;
 
 	struct globlink {
 		struct globlink *next;
@@ -356,7 +318,7 @@ static int glob_in_dir(const char *pattern, const char *directory,
 			    (struct globlink *)xmalloc(sizeof(struct globlink));
 			len = strlen(name);
 			new->name = xmalloc(len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
-			memcpy((__ptr_t) new->name, name, len);
+			memcpy(new->name, name, len);
 			new->name[len] = '\0';
 			new->next = names;
 			names = new;
@@ -414,44 +376,7 @@ void globfree(glob_t *pglob)
 		int i = pglob->gl_flags & GLOB_DOOFFS ? pglob->gl_offs : 0;
 		for (; i < pglob->gl_pathc; ++i)
 			if (pglob->gl_pathv[i] != NULL)
-				free((__ptr_t) pglob->gl_pathv[i]);
-		free((__ptr_t) pglob->gl_pathv);
+				free(pglob->gl_pathv[i]);
+		free(pglob->gl_pathv);
 	}
 }
-#endif /* CONFIG_GLOB */
-
-#ifdef CONFIG_FAKE_GLOB
-/* Fake version of glob. We simply put the input string into
- * the gl_pathv array. Currently we don't need it as hush.c won't
- * call us if no glob support is available.
- */
-int glob(pattern, flags, errfunc, pglob)
-const char *pattern;
-int flags;
-int (*errfunc) __P((const char *, int));
-glob_t *pglob;
-{
-	int elems, i;
-
-	if (!(flags & GLOB_APPEND)) {
-		pglob->gl_pathc = 0;
-		pglob->gl_pathv = NULL;
-	}
-
-	elems = pglob->gl_pathc + 2;
-	if (flags & GLOB_DOOFFS)
-		elems += pglob->gl_offs;
-
-	pglob->gl_pathv = xrealloc(pglob->gl_pathv, elems * sizeof(char *));
-
-	if (flags & GLOB_DOOFFS)
-		for (i = 0; i < pglob->gl_offs; i++)
-			pglob->gl_pathv[i] = NULL;
-
-	pglob->gl_pathv[pglob->gl_pathc] = strdup(pattern);
-	pglob->gl_pathc++;
-	pglob->gl_pathv[pglob->gl_pathc] = NULL;
-
-	return 0;
-}
-#endif /* CONFIG_FAKE_GLOB */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] complete: add support for spaces in completions
  2023-11-09 12:25 [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Ahmad Fatoum
@ 2023-11-09 12:25 ` Ahmad Fatoum
  2023-11-09 12:25 ` [PATCH v2 3/3] commands: drvinfo: support filtering by driver Ahmad Fatoum
  2023-11-13 12:51 ` [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2023-11-09 12:25 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Drivers have spaces inside their names, which makes tab completion
tricky as it restarts for every separate argument.

Support for doing this in readline and hush landed in commits
1498093ccd11 ("readline: Complete strings containing whitespaces correctly")
and 70e0885229d2 ("hush: Fix handling '\ '") respectively.

This only goes one way though, escape a completion suggestion. The
suggestion needs to be unescaped again, so repeated <Tab> usage after
modifying the prompt is understood.

For this to work, skip backslashes in the argument being completed.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch to prepare completing driver names with spaces
---
 common/complete.c | 70 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 52 insertions(+), 18 deletions(-)

diff --git a/common/complete.c b/common/complete.c
index 4137bb3084fc..ef31a36faf5f 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -14,6 +14,27 @@
 #include <command.h>
 #include <environment.h>
 
+static bool is_valid_escape(const char *str)
+{
+	return str[0] == '\\' && (str[1] == ' ' || str[1] == '\\');
+}
+
+static bool strstarts_escaped(const char *whole, const char *prefix_escaped)
+{
+	if (!prefix_escaped)
+		return true;
+
+	while (*prefix_escaped) {
+		if (is_valid_escape(prefix_escaped))
+			prefix_escaped++;
+
+		if (*whole++ != *prefix_escaped++)
+			return false;
+	}
+
+	return true;
+}
+
 static int file_complete(struct string_list *sl, char *instr,
 			 const char *dirn, int exec)
 {
@@ -35,7 +56,7 @@ static int file_complete(struct string_list *sl, char *instr,
 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 			continue;
 
-		if (strncmp(base, d->d_name, strlen(base)))
+		if (!strstarts_escaped(d->d_name, base))
 			continue;
 
 		strcpy(tmp, instr);
@@ -94,7 +115,7 @@ static int path_command_complete(struct string_list *sl, char *instr)
 					!strcmp(d->d_name, ".."))
 				continue;
 
-			if (!strncmp(instr, d->d_name, strlen(instr))) {
+			if (strstarts_escaped(d->d_name, instr)) {
 				strcpy(tmp, d->d_name);
 				if (!stat(tmp, &s) &&
 						S_ISDIR(s.st_mode))
@@ -136,15 +157,9 @@ EXPORT_SYMBOL(command_complete);
 int device_complete(struct string_list *sl, char *instr)
 {
 	struct device *dev;
-	int len;
-
-	if (!instr)
-		instr = "";
-
-	len = strlen(instr);
 
 	for_each_device(dev) {
-		if (strncmp(instr, dev_name(dev), len))
+		if (!strstarts_escaped(dev_name(dev), instr))
 			continue;
 
 		string_list_add_asprintf(sl, "%s ", dev_name(dev));
@@ -158,12 +173,9 @@ static int device_param_complete(struct device *dev, const char *devname,
 				 struct string_list *sl, char *instr, int eval)
 {
 	struct param_d *param;
-	int len;
-
-	len = strlen(instr);
 
 	list_for_each_entry(param, &dev->parameters, list) {
-		if (strncmp(instr, param->name, len))
+		if (!strstarts_escaped(param->name, instr))
 			continue;
 
 		string_list_add_asprintf(sl, "%s%s.%s%c",
@@ -340,21 +352,43 @@ void complete_reset(void)
 	tab_pressed = 0;
 }
 
+static char *skip_to_last_unescaped_space(char *instr)
+{
+	char *t;
+
+	t = strrchr(instr, ' ');
+	if (t && (instr == t || t[-1] != '\\'))
+		return t + 1;
+
+	return instr;
+}
+
+static size_t strlen_escaped(char *instr)
+{
+	size_t count = 0;
+
+	for (; *instr; instr++) {
+		if (is_valid_escape(instr))
+			instr++;
+
+		count++;
+	}
+
+	return count;
+}
+
 static char* cmd_complete_lookup(struct string_list *sl, char *instr)
 {
 	struct command *cmdtp;
 	int len;
 	int ret = COMPLETE_END;
 	char *res = NULL;
-	char *t;
 
 	for_each_command(cmdtp) {
 		len = strlen(cmdtp->name);
 		if (!strncmp(instr, cmdtp->name, len) && instr[len] == ' ') {
 			instr += len + 1;
-			t = strrchr(instr, ' ');
-			if (t)
-				instr = t + 1;
+			instr = skip_to_last_unescaped_space(instr);
 
 			if (cmdtp->complete) {
 				ret = cmdtp->complete(sl, instr);
@@ -414,7 +448,7 @@ int complete(char *instr, char **outstr)
 			env_param_complete(&sl, instr + 1, 1);
 	}
 
-	pos = strlen(instr);
+	pos = strlen_escaped(instr);
 
 	*outstr = "";
 	if (list_empty(&sl.list))
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] commands: drvinfo: support filtering by driver
  2023-11-09 12:25 [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Ahmad Fatoum
  2023-11-09 12:25 ` [PATCH v2 2/3] complete: add support for spaces in completions Ahmad Fatoum
@ 2023-11-09 12:25 ` Ahmad Fatoum
  2023-11-13 12:51 ` [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2023-11-09 12:25 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

drvinfo can be very long especially for the in-tree defconfigs.

Make it more convenient to use by add optional filtering support:

  barebox@board:/ drvinfo '*imx7d*'
  Driver  Device(s)
  --------------------
  imx7d-src
          30390000.reset-controller@30390000.of
  imx7d_adc
          30610000.adc@30610000.of
          30620000.adc@30620000.of

  Use 'devinfo DEVICE' for more information

Furthermore, tab completion for driver names is now supported as well.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - use fnmatch to support glob patterns for driver names (Sascha)
  - add functional autocomplete despite spaces (Sascha)

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/drvinfo.c |  8 ++++++++
 common/complete.c  | 15 +++++++++++++++
 include/complete.h |  1 +
 3 files changed, 24 insertions(+)

diff --git a/commands/drvinfo.c b/commands/drvinfo.c
index b984b9472585..e13b04870ee4 100644
--- a/commands/drvinfo.c
+++ b/commands/drvinfo.c
@@ -5,15 +5,21 @@
 #include <common.h>
 #include <command.h>
 #include <driver.h>
+#include <complete.h>
+#include <fnmatch.h>
 
 static int do_drvinfo(int argc, char *argv[])
 {
+	char *pattern = argv[1];
 	struct driver *drv;
 	struct device *dev;
 
 	printf("Driver\tDevice(s)\n");
 	printf("--------------------\n");
 	for_each_driver(drv) {
+		if (pattern && fnmatch(pattern, drv->name, 0))
+			continue;
+
 		printf("%s\n",drv->name);
 		for_each_device(dev) {
 			if (dev->driver == drv)
@@ -31,5 +37,7 @@ static int do_drvinfo(int argc, char *argv[])
 BAREBOX_CMD_START(drvinfo)
 	.cmd		= do_drvinfo,
 	BAREBOX_CMD_DESC("list compiled-in device drivers")
+	BAREBOX_CMD_OPTS("[DRIVER]")
 	BAREBOX_CMD_GROUP(CMD_GRP_INFO)
+	BAREBOX_CMD_COMPLETE(driver_complete)
 BAREBOX_CMD_END
diff --git a/common/complete.c b/common/complete.c
index ef31a36faf5f..3911535621b1 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -186,6 +186,21 @@ static int device_param_complete(struct device *dev, const char *devname,
 	return 0;
 }
 
+int driver_complete(struct string_list *sl, char *instr)
+{
+	struct driver_d *drv;
+
+	for_each_driver(drv) {
+		if (!strstarts_escaped(drv->name, instr))
+			continue;
+
+		string_list_add_asprintf(sl, "%s ", drv->name);
+	}
+
+	return COMPLETE_CONTINUE;
+}
+EXPORT_SYMBOL(driver_complete);
+
 int empty_complete(struct string_list *sl, char *instr)
 {
 	return COMPLETE_END;
diff --git a/include/complete.h b/include/complete.h
index b0e675b5599f..2068760ac235 100644
--- a/include/complete.h
+++ b/include/complete.h
@@ -14,6 +14,7 @@ void complete_reset(void);
 
 int command_complete(struct string_list *sl, char *instr);
 int device_complete(struct string_list *sl, char *instr);
+int driver_complete(struct string_list *sl, char *instr);
 int empty_complete(struct string_list *sl, char *instr);
 int eth_complete(struct string_list *sl, char *instr);
 int command_var_complete(struct string_list *sl, char *instr);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h
  2023-11-09 12:25 [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Ahmad Fatoum
  2023-11-09 12:25 ` [PATCH v2 2/3] complete: add support for spaces in completions Ahmad Fatoum
  2023-11-09 12:25 ` [PATCH v2 3/3] commands: drvinfo: support filtering by driver Ahmad Fatoum
@ 2023-11-13 12:51 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2023-11-13 12:51 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, Christian Melki

On Thu, Nov 09, 2023 at 01:25:49PM +0100, Ahmad Fatoum wrote:
> The implementation of the glob and fnmatch functions is taken from glibc
> and it shows in the amount of #ifdefs. We don't need that in barebox and
> can even get the glob_t size down by dropping unused members, so let's
> do that.
> 
> Cc: Christian Melki <christian.melki@t2data.com>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> v1 -> v2:
>   - new patch
> ---
>  include/fnmatch.h |  58 ++-----------
>  include/glob.h    | 201 +++++-----------------------------------------
>  lib/Makefile      |   2 +-
>  lib/fnmatch.c     |  45 +----------
>  lib/glob.c        | 105 ++++--------------------
>  5 files changed, 45 insertions(+), 366 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/include/fnmatch.h b/include/fnmatch.h
> index c13beb9011fb..1bc2cf87391a 100644
> --- a/include/fnmatch.h
> +++ b/include/fnmatch.h
> @@ -4,69 +4,21 @@
>  */
>  
>  #ifndef	_FNMATCH_H
> -#define	_FNMATCH_H	1
> -
> -#ifdef	__cplusplus
> -extern "C" {
> -#endif
> -
> -#if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
> -# if !defined __GLIBC__ || !defined __P
> -#  undef	__P
> -#  define __P(protos)	protos
> -# endif
> -#else /* Not C++ or ANSI C.  */
> -# undef	__P
> -# define __P(protos)	()
> -/* We can get away without defining `const' here only because in this file
> -   it is used only inside the prototype for `fnmatch', which is elided in
> -   non-ANSI C where `const' is problematical.  */
> -#endif /* C++ or ANSI C.  */
> -
> -#ifndef const
> -# if (defined __STDC__ && __STDC__) || defined __cplusplus
> -#  define __const	const
> -# else
> -#  define __const
> -# endif
> -#endif
> -
> -/* We #undef these before defining them because some losing systems
> -   (HP-UX A.08.07 for example) define these in <unistd.h>.  */
> -#undef	FNM_PATHNAME
> -#undef	FNM_NOESCAPE
> -#undef	FNM_PERIOD
> +#define	_FNMATCH_H
>  
>  /* Bits set in the FLAGS argument to `fnmatch'.  */
>  #define	FNM_PATHNAME	(1 << 0) /* No wildcard can ever match `/'.  */
>  #define	FNM_NOESCAPE	(1 << 1) /* Backslashes don't quote special chars.  */
>  #define	FNM_PERIOD	(1 << 2) /* Leading `.' is matched only explicitly.  */
> -
> -#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 2 || defined _GNU_SOURCE
> -# define FNM_FILE_NAME	 FNM_PATHNAME	/* Preferred GNU name.  */
> -# define FNM_LEADING_DIR (1 << 3)	/* Ignore `/...' after a match.  */
> -# define FNM_CASEFOLD	 (1 << 4)	/* Compare without regard to case.  */
> -# define FNM_EXTMATCH	 (1 << 5)	/* Use ksh-like extended matching. */
> -#endif
> +#define FNM_FILE_NAME	 FNM_PATHNAME	/* Preferred GNU name.  */
> +#define FNM_LEADING_DIR	(1 << 3)	/* Ignore `/...' after a match.  */
> +#define FNM_CASEFOLD	(1 << 4)	/* Compare without regard to case.  */
>  
>  /* Value returned by `fnmatch' if STRING does not match PATTERN.  */
>  #define	FNM_NOMATCH	1
>  
> -/* This value is returned if the implementation does not support
> -   `fnmatch'.  Since this is not the case here it will never be
> -   returned but the conformance test suites still require the symbol
> -   to be defined.  */
> -#ifdef _XOPEN_SOURCE
> -# define FNM_NOSYS	(-1)
> -#endif
> -
>  /* Match NAME against the filename pattern PATTERN,
>     returning zero if it matches, FNM_NOMATCH if not.  */
> -extern int fnmatch __P ((__const char *__pattern, __const char *__name,
> -			 int __flags));
> -
> -#ifdef	__cplusplus
> -}
> -#endif
> +extern int fnmatch(const char *pattern, const char *name, int flags);
>  
>  #endif /* fnmatch.h */
> diff --git a/include/glob.h b/include/glob.h
> index ec0ac66f8765..67816c9cf0ad 100644
> --- a/include/glob.h
> +++ b/include/glob.h
> @@ -4,54 +4,9 @@
>  */
>  
>  #ifndef	_GLOB_H
> -#define	_GLOB_H	1
> +#define	_GLOB_H
>  
> -#define _FILE_OFFSET_BITS 32
> -
> -#ifdef	__cplusplus
> -extern "C" {
> -#endif
> -
> -#undef	__ptr_t
> -#if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
> -# if !defined __GLIBC__ || !defined __P
> -#  undef __P
> -#  undef __PMT
> -#  define __P(protos)	protos
> -#  define __PMT(protos)	protos
> -#  if !defined __GNUC__ || __GNUC__ < 2
> -#   undef __const
> -#   define __const const
> -#  endif
> -# endif
> -# define __ptr_t	void *
> -#else /* Not C++ or ANSI C.  */
> -# undef	__P
> -# undef __PMT
> -# define __P(protos)	()
> -# define __PMT(protos)	()
> -# undef	__const
> -# define __const
> -# define __ptr_t	char *
> -#endif /* C++ or ANSI C.  */
> -
> -/* We need `size_t' for the following definitions.  */
> -#ifndef __size_t
> -# if defined __GNUC__ && __GNUC__ >= 2
> -typedef __SIZE_TYPE__ __size_t;
> -#  ifdef _XOPEN_SOURCE
> -typedef __SIZE_TYPE__ size_t;
> -#  endif
> -# else
> -/* This is a guess.  */
> -typedef unsigned long int __size_t;
> -# endif
> -#else
> -/* The GNU CC stddef.h version defines __size_t as empty.  We need a real
> -   definition.  */
> -# undef __size_t
> -# define __size_t size_t
> -#endif
> +#include <linux/types.h>
>  
>  /* Bits set in the FLAGS argument to `glob'.  */
>  #define	GLOB_ERR	(1 << 0)/* Return on read errors.  */
> @@ -62,163 +17,49 @@ typedef unsigned long int __size_t;
>  #define	GLOB_APPEND	(1 << 5)/* Append to results of a previous call.  */
>  #define	GLOB_NOESCAPE	(1 << 6)/* Backslashes don't quote metacharacters.  */
>  #define	GLOB_PERIOD	(1 << 7)/* Leading `.' can be matched by metachars.  */
> -
> -#if (!defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 2 || defined _BSD_SOURCE \
> -     || defined _GNU_SOURCE)
> -# define GLOB_MAGCHAR	 (1 << 8)/* Set in gl_flags if any metachars seen.  */
> -# define GLOB_ALTDIRFUNC (1 << 9)/* Use gl_opendir et al functions.  */
> -# define GLOB_BRACE	 (1 << 10)/* Expand "{a,b}" to "a" "b".  */
> -# define GLOB_NOMAGIC	 (1 << 11)/* If no magic chars, return the pattern.  */
> -# define GLOB_TILDE	 (1 << 12)/* Expand ~user and ~ to home directories. */
> -# define GLOB_ONLYDIR	 (1 << 13)/* Match only directories.  */
> -# define GLOB_TILDE_CHECK (1 << 14)/* Like GLOB_TILDE but return an error
> -				      if the user name is not available.  */
> -# define __GLOB_FLAGS	(GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_DOOFFS| \
> -			 GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_APPEND|     \
> -			 GLOB_PERIOD|GLOB_ALTDIRFUNC|GLOB_BRACE|     \
> -			 GLOB_NOMAGIC|GLOB_TILDE|GLOB_ONLYDIR|GLOB_TILDE_CHECK)
> -#else
> +#define	GLOB_MAGCHAR	 (1 << 8)/* Set in gl_flags if any metachars seen.  */
>  # define __GLOB_FLAGS	(GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_DOOFFS| \
>  			 GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_APPEND|     \
>  			 GLOB_PERIOD)
> -#endif
>  
>  /* Error returns from `glob'.  */
>  #define	GLOB_NOSPACE	1	/* Ran out of memory.  */
>  #define	GLOB_ABORTED	2	/* Read error.  */
>  #define	GLOB_NOMATCH	3	/* No matches found.  */
>  #define GLOB_NOSYS	4	/* Not implemented.  */
> -#ifdef _GNU_SOURCE
> -/* Previous versions of this file defined GLOB_ABEND instead of
> -   GLOB_ABORTED.  Provide a compatibility definition here.  */
> -# define GLOB_ABEND GLOB_ABORTED
> -#endif
> +#define GLOB_ABEND	GLOB_ABORTED
>  
> -/* Structure describing a globbing run.  */
> -#if !defined _AMIGA && !defined VMS /* Buggy compiler.   */
> -# ifdef _GNU_SOURCE
> -struct stat;
> -# endif
> -#endif
> -typedef struct
> -  {
> -    __size_t gl_pathc;		/* Count of paths matched by the pattern.  */
> -    char **gl_pathv;		/* List of matched pathnames.  */
> -    __size_t gl_offs;		/* Slots to reserve in `gl_pathv'.  */
> -    int gl_flags;		/* Set to FLAGS, maybe | GLOB_MAGCHAR.  */
> +typedef struct {
> +	size_t gl_pathc;		/* Count of paths matched by the pattern.  */
> +	char **gl_pathv;		/* List of matched pathnames.  */
> +	size_t gl_offs;		/* Slots to reserve in `gl_pathv'.  */
> +	int gl_flags;		/* Set to FLAGS, maybe | GLOB_MAGCHAR.  */
> +} glob_t;
>  
> -    /* If the GLOB_ALTDIRFUNC flag is set, the following functions
> -       are used instead of the normal file access functions.  */
> -    void (*gl_closedir) __PMT ((void *));
> -#ifdef _GNU_SOURCE
> -    struct dirent *(*gl_readdir) __PMT ((void *));
> +#ifdef CONFIG_GLOB
> +extern int glob (const char *__restrict pattern, int flags,
> +		      int (*errfunc) (const char *, int),
> +		      glob_t *__restrict pglob);
> +
> +extern void globfree(glob_t *pglob);
>  #else
> -    void *(*gl_readdir) __PMT ((void *));
> -#endif
> -    __ptr_t (*gl_opendir) __PMT ((__const char *));
> -#ifdef _GNU_SOURCE
> -    int (*gl_lstat) __PMT ((__const char *__restrict,
> -			    struct stat *__restrict));
> -    int (*gl_stat) __PMT ((__const char *__restrict, struct stat *__restrict));
> -#else
> -    int (*gl_lstat) __PMT ((__const char *__restrict, void *__restrict));
> -    int (*gl_stat) __PMT ((__const char *__restrict, void *__restrict));
> -#endif
> -  } glob_t;
> -
> -#ifdef _LARGEFILE64_SOURCE
> -# ifdef _GNU_SOURCE
> -struct stat64;
> -# endif
> -typedef struct
> -  {
> -    __size_t gl_pathc;
> -    char **gl_pathv;
> -    __size_t gl_offs;
> -    int gl_flags;
> -
> -    /* If the GLOB_ALTDIRFUNC flag is set, the following functions
> -       are used instead of the normal file access functions.  */
> -    void (*gl_closedir) __PMT ((void *));
> -# ifdef _GNU_SOURCE
> -    struct dirent64 *(*gl_readdir) __PMT ((void *));
> -# else
> -    void *(*gl_readdir) __PMT ((void *));
> -# endif
> -    __ptr_t (*gl_opendir) __PMT ((__const char *));
> -# ifdef _GNU_SOURCE
> -    int (*gl_lstat) __PMT ((__const char *__restrict,
> -			    struct stat64 *__restrict));
> -    int (*gl_stat) __PMT ((__const char *__restrict,
> -			   struct stat64 *__restrict));
> -# else
> -    int (*gl_lstat) __PMT ((__const char *__restrict, void *__restrict));
> -    int (*gl_stat) __PMT ((__const char *__restrict, void *__restrict));
> -# endif
> -  } glob64_t;
> -#endif
> -
> -#if _FILE_OFFSET_BITS == 64 && __GNUC__ < 2
> -# define glob glob64
> -# define globfree globfree64
> -#endif
> -
> -/* Do glob searching for PATTERN, placing results in PGLOB.
> -   The bits defined above may be set in FLAGS.
> -   If a directory cannot be opened or read and ERRFUNC is not nil,
> -   it is called with the pathname that caused the error, and the
> -   `errno' value from the failing call; if it returns non-zero
> -   `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
> -   If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
> -   Otherwise, `glob' returns zero.  */
> -#if _FILE_OFFSET_BITS != 64 || __GNUC__ < 2
> -#if defined CONFIG_GLOB || defined CONFIG_FAKE_GLOB
> -extern int glob __P ((__const char *__restrict __pattern, int __flags,
> -		      int (*__errfunc) (__const char *, int),
> -		      glob_t *__restrict __pglob));
> -
> -extern void globfree __P ((glob_t *__pglob));
> -#else
> -static inline int glob __P ((__const char *__restrict __pattern, int __flags,
> -		      int (*__errfunc) (__const char *, int),
> -		      glob_t *__restrict __pglob))
> +static inline int glob(const char *__restrict pattern, int flags,
> +		      int (*errfunc) (const char *, int),
> +		      glob_t *__restrict pglob)
>  {
>  	return GLOB_ABORTED;
>  }
>  
> -static inline void globfree __P ((glob_t *__pglob))
> +static inline void globfree(glob_t *pglob)
>  {
>  }
>  #endif
> -/* Free storage allocated in PGLOB by a previous `glob' call.  */
> -#else
> -extern int glob __P ((__const char *__restrict __pattern, int __flags,
> -		      int (*__errfunc) (__const char *, int),
> -		      glob_t *__restrict __pglob)) __asm__ ("glob64");
>  
> -extern void globfree __P ((glob_t *__pglob)) __asm__ ("globfree64");
> -#endif
> -
> -#ifdef _LARGEFILE64_SOURCE
> -extern int glob64 __P ((__const char *__restrict __pattern, int __flags,
> -			int (*__errfunc) (__const char *, int),
> -			glob64_t *__restrict __pglob));
> -
> -extern void globfree64 __P ((glob64_t *__pglob));
> -#endif
> -
> -
> -#ifdef _GNU_SOURCE
>  /* Return nonzero if PATTERN contains any metacharacters.
>     Metacharacters can be quoted with backslashes if QUOTE is nonzero.
>  
>     This function is not part of the interface specified by POSIX.2
>     but several programs want to use it.  */
> -extern int glob_pattern_p __P ((__const char *__pattern, int __quote));
> -#endif
> -
> -#ifdef	__cplusplus
> -}
> -#endif
> +extern int glob_pattern_p(const char *pattern, int quote);
>  
>  #endif /* glob.h  */
> diff --git a/lib/Makefile b/lib/Makefile
> index 8817b5f47bac..9bb871f94f71 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -38,7 +38,7 @@ obj-$(CONFIG_CMDLINE_EDITING)	+= readline.o
>  obj-$(CONFIG_SIMPLE_READLINE)	+= readline_simple.o
>  obj-$(CONFIG_FNMATCH)		+= fnmatch.o
>  obj-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
> -obj-y			+= glob.o
> +obj-$(CONFIG_GLOB)	+= glob.o
>  obj-y			+= notifier.o
>  obj-y			+= random.o
>  obj-$(CONFIG_IMAGE_SPARSE) += image-sparse.o
> diff --git a/lib/fnmatch.c b/lib/fnmatch.c
> index 0ab530d3b182..ac87ba76219c 100644
> --- a/lib/fnmatch.c
> +++ b/lib/fnmatch.c
> @@ -1,50 +1,11 @@
> -/* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
> -
> -This library is free software; you can redistribute it and/or
> -modify it under the terms of the GNU Library General Public License as
> -published by the Free Software Foundation; either version 2 of the
> -License, or (at your option) any later version.
> -
> -This library is distributed in the hope that it will be useful,
> -but WITHOUT ANY WARRANTY; without even the implied warranty of
> -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> -Library General Public License for more details.
> -
> -You should have received a copy of the GNU Library General Public
> -License along with this library; see the file COPYING.LIB.  If
> -not, write to the Free Software Foundation, Inc., 675 Mass Ave,
> -Cambridge, MA 02139, USA.  */
> -
> -/* Enable GNU extensions in fnmatch.h.  */
> -#ifndef _GNU_SOURCE
> -# define _GNU_SOURCE	1
> -#endif
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +// SPDX-FileCopyrightText: (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
>  
>  #include <errno.h>
>  #include <fnmatch.h>
>  #include <linux/ctype.h>
>  
> -
> -/* Comment out all this code if we are using the GNU C Library, and are not
> -   actually compiling the library itself.  This code is part of the GNU C
> -   Library, but also included in many other GNU distributions.  Compiling
> -   and linking in this code is a waste when using the GNU C library
> -   (especially if it is a shared library).  Rather than having every GNU
> -   program understand `configure --with-gnu-libc' and omit the object files,
> -   it is simpler to just do this in the source for each such file.  */
> -
> -# if defined (STDC_HEADERS) || !defined (isascii)
> -#  define ISASCII(c) 1
> -# else
> -#  define ISASCII(c) isascii(c)
> -# endif
> -
> -# define ISUPPER(c) (ISASCII (c) && isupper (c))
> -
> -
> -# ifndef errno
> -extern int errno;
> -# endif
> +# define ISUPPER(c) (isascii(c) && isupper(c))
>  
>  /* Match STRING against the filename pattern PATTERN, returning zero if
>     it matches, nonzero if not.  */
> diff --git a/lib/glob.c b/lib/glob.c
> index 8523bad9a7ef..389580b0ed79 100644
> --- a/lib/glob.c
> +++ b/lib/glob.c
> @@ -1,19 +1,5 @@
> -/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
> -
> -This library is free software; you can redistribute it and/or
> -modify it under the terms of the GNU Library General Public License as
> -published by the Free Software Foundation; either version 2 of the
> -License, or (at your option) any later version.
> -
> -This library is distributed in the hope that it will be useful,
> -but WITHOUT ANY WARRANTY; without even the implied warranty of
> -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> -Library General Public License for more details.
> -
> -You should have received a copy of the GNU Library General Public
> -License along with this library; see the file COPYING.LIB.  If
> -not, write to the Free Software Foundation, Inc., 675 Mass Ave,
> -Cambridge, MA 02139, USA.  */
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +// SPDX-FileCopyrightText: (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
>  
>  #include <common.h>
>  #include <errno.h>
> @@ -23,25 +9,15 @@ Cambridge, MA 02139, USA.  */
>  #include <xfuncs.h>
>  #include <fnmatch.h>
>  #include <qsort.h>
> -#define _GNU_SOURCE
>  #include <glob.h>
>  
> -#ifdef CONFIG_GLOB
> -
> -extern __ptr_t(*__glob_opendir_hook) __P((const char *directory));
> -extern void (*__glob_closedir_hook) __P((__ptr_t stream));
> -extern const char *(*__glob_readdir_hook) __P((__ptr_t stream));
> -
> -static int glob_in_dir __P((const char *pattern, const char *directory,
> +static int glob_in_dir (const char *pattern, const char *directory,
>  			    int flags,
> -			    int (*errfunc) __P((const char *, int)),
> -			    glob_t * pglob));
> -static int prefix_array __P((const char *prefix, char **array, size_t n,
> -			     int add_slash));
> +			    int (*errfunc) (const char *, int),
> +			    glob_t * pglob);
> +static int prefix_array (const char *prefix, char **array, size_t n,
> +			     int add_slash);
>  
> -#ifdef __GLOB64
> -extern int glob_pattern_p(const char *pattern, int quote);
> -#else
>  /* Return nonzero if PATTERN contains any metacharacters.
>     Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
>  int glob_pattern_p(const char *pattern, int quote)
> @@ -72,7 +48,6 @@ int glob_pattern_p(const char *pattern, int quote)
>  
>  	return 0;
>  }
> -#endif
>  
>  #ifdef CONFIG_GLOB_SORT
>  /* Do a collated comparison of A and B.  */
> @@ -100,7 +75,7 @@ static int collated_compare(const void *a, const void *b)
>     If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
>     Otherwise, `glob' returns zero.  */
>  int glob(const char *pattern, int flags,
> -		int (*errfunc) __P((const char *, int)), glob_t *pglob)
> +		int (*errfunc) (const char *, int), glob_t *pglob)
>  {
>  	const char *filename;
>  	char *dirname = NULL;
> @@ -169,19 +144,6 @@ int glob(const char *pattern, int flags,
>  		for (i = 0; i < dirs.gl_pathc; ++i) {
>  			int oldcount1;
>  
> -#ifdef	SHELL
> -			{
> -				/* Make globbing interruptible in the bash shell. */
> -				extern int interrupt_state;
> -
> -				if (interrupt_state) {
> -					globfree(&dirs);
> -					globfree(&files);
> -					status = GLOB_ABEND goto out;
> -				}
> -			}
> -#endif				/* SHELL.  */
> -
>  			oldcount1 = pglob->gl_pathc;
>  			status = glob_in_dir(filename, dirs.gl_pathv[i],
>  					     (flags | GLOB_APPEND) &
> @@ -263,7 +225,7 @@ int glob(const char *pattern, int flags,
>  #ifdef CONFIG_GLOB_SORT
>  	if (!(flags & GLOB_NOSORT))
>  		/* Sort the vector.  */
> -		qsort((__ptr_t) & pglob->gl_pathv[oldcount],
> +		qsort(&pglob->gl_pathv[oldcount],
>  		      pglob->gl_pathc - oldcount,
>  		      sizeof(char *), collated_compare);
>  #endif
> @@ -299,7 +261,7 @@ static int prefix_array(const char *dirname, char **array, size_t n,
>  		memcpy(new, dirname, dirlen);
>  		new[dirlen] = '/';
>  		memcpy(&new[dirlen + 1], array[i], eltlen);
> -		free((__ptr_t) array[i]);
> +		free(array[i]);
>  		array[i] = new;
>  	}
>  
> @@ -311,9 +273,9 @@ static int prefix_array(const char *dirname, char **array, size_t n,
>     The GLOB_NOSORT bit in FLAGS is ignored.  No sorting is ever done.
>     The GLOB_APPEND flag is assumed to be set (always appends).  */
>  static int glob_in_dir(const char *pattern, const char *directory,
> -		int flags, int (*errfunc) __P((const char *, int)), glob_t *pglob)
> +		int flags, int (*errfunc) (const char *, int), glob_t *pglob)
>  {
> -	__ptr_t stream = NULL;
> +	void *stream = NULL;
>  
>  	struct globlink {
>  		struct globlink *next;
> @@ -356,7 +318,7 @@ static int glob_in_dir(const char *pattern, const char *directory,
>  			    (struct globlink *)xmalloc(sizeof(struct globlink));
>  			len = strlen(name);
>  			new->name = xmalloc(len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
> -			memcpy((__ptr_t) new->name, name, len);
> +			memcpy(new->name, name, len);
>  			new->name[len] = '\0';
>  			new->next = names;
>  			names = new;
> @@ -414,44 +376,7 @@ void globfree(glob_t *pglob)
>  		int i = pglob->gl_flags & GLOB_DOOFFS ? pglob->gl_offs : 0;
>  		for (; i < pglob->gl_pathc; ++i)
>  			if (pglob->gl_pathv[i] != NULL)
> -				free((__ptr_t) pglob->gl_pathv[i]);
> -		free((__ptr_t) pglob->gl_pathv);
> +				free(pglob->gl_pathv[i]);
> +		free(pglob->gl_pathv);
>  	}
>  }
> -#endif /* CONFIG_GLOB */
> -
> -#ifdef CONFIG_FAKE_GLOB
> -/* Fake version of glob. We simply put the input string into
> - * the gl_pathv array. Currently we don't need it as hush.c won't
> - * call us if no glob support is available.
> - */
> -int glob(pattern, flags, errfunc, pglob)
> -const char *pattern;
> -int flags;
> -int (*errfunc) __P((const char *, int));
> -glob_t *pglob;
> -{
> -	int elems, i;
> -
> -	if (!(flags & GLOB_APPEND)) {
> -		pglob->gl_pathc = 0;
> -		pglob->gl_pathv = NULL;
> -	}
> -
> -	elems = pglob->gl_pathc + 2;
> -	if (flags & GLOB_DOOFFS)
> -		elems += pglob->gl_offs;
> -
> -	pglob->gl_pathv = xrealloc(pglob->gl_pathv, elems * sizeof(char *));
> -
> -	if (flags & GLOB_DOOFFS)
> -		for (i = 0; i < pglob->gl_offs; i++)
> -			pglob->gl_pathv[i] = NULL;
> -
> -	pglob->gl_pathv[pglob->gl_pathc] = strdup(pattern);
> -	pglob->gl_pathc++;
> -	pglob->gl_pathv[pglob->gl_pathc] = NULL;
> -
> -	return 0;
> -}
> -#endif /* CONFIG_FAKE_GLOB */
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-11-13 12:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-09 12:25 [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Ahmad Fatoum
2023-11-09 12:25 ` [PATCH v2 2/3] complete: add support for spaces in completions Ahmad Fatoum
2023-11-09 12:25 ` [PATCH v2 3/3] commands: drvinfo: support filtering by driver Ahmad Fatoum
2023-11-13 12:51 ` [PATCH v2 1/3] glob: drop needless ifdeffery in {glob,fnmatch}.h Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox