diff --git a/libgphoto2_port/ChangeLog b/libgphoto2_port/ChangeLog index eeb3daa29..9cc70e5f3 100644 --- a/libgphoto2_port/ChangeLog +++ b/libgphoto2_port/ChangeLog @@ -1,3 +1,11 @@ +2001-11-29 Lutz Müller + + * configure.in: Check for GNU regex + * acconfig.h: HAVE_GNU_REGEX + * serial: + * libgphoto2_port: First try to implement a regex match. Doesn't work + yet. + 2001-11-29 Hans Ulrich Niedermann * libgphoto2_port/m4/packaging.m4: added packaging macro diff --git a/libgphoto2_port/acconfig.h b/libgphoto2_port/acconfig.h index 5ece5b9eb..8eabef2f5 100644 --- a/libgphoto2_port/acconfig.h +++ b/libgphoto2_port/acconfig.h @@ -17,3 +17,5 @@ #undef HAVE_BAUDBOY #undef HAVE_TTYLOCK #undef HAVE_LOCKDEV + +#undef HAVE_GNU_REGEX diff --git a/libgphoto2_port/configure.in b/libgphoto2_port/configure.in index 7a16c08f3..94e0e7ebe 100644 --- a/libgphoto2_port/configure.in +++ b/libgphoto2_port/configure.in @@ -211,6 +211,15 @@ AC_SUBST(USB_LIBS) AC_SUBST(IOLIB_SUBDIRS) +dnl ********** +dnl GNU regex? +dnl ********** +AC_CHECK_FUNC(re_compile_pattern, AC_DEFINE(HAVE_GNU_REGEX)) + +dnl **************************************** +dnl What libraries do other programs need if +dnl they want to use libgphoto2_port? +dnl **************************************** GPPORT_LIBDIR='-L${libdir}' GPPORT_INCLUDEDIR='-I${includedir}/gphoto2' GPPORT_LIBS="-lgphoto2_port" diff --git a/libgphoto2_port/libgphoto2_port/gphoto2-port-info-list.c b/libgphoto2_port/libgphoto2_port/gphoto2-port-info-list.c index 52ec4c90a..b150c3916 100644 --- a/libgphoto2_port/libgphoto2_port/gphoto2-port-info-list.c +++ b/libgphoto2_port/libgphoto2_port/gphoto2-port-info-list.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -59,6 +60,7 @@ struct _GPPortInfoList { }; #define CHECK_NULL(x) {if (!(x)) return (GP_ERROR_BAD_PARAMETERS);} +#define CR(x) {int r=(x);if (r<0) return (r);} /** * gp_port_info_list_new: @@ -112,7 +114,7 @@ gp_port_info_list_free (GPPortInfoList *list) * Appends an entry to the @list. This function is typically called by * an io-driver on #gp_port_library_list. * - * Return value: a gphoto2 error code + * Return value: The index of the new entry or a gphoto2 error code **/ int gp_port_info_list_append (GPPortInfoList *list, GPPortInfo info) @@ -134,7 +136,7 @@ gp_port_info_list_append (GPPortInfoList *list, GPPortInfo info) memcpy (&(list->info[list->count - 1]), &info, sizeof (GPPortInfo)); - return (GP_OK); + return (list->count - 1); } /** @@ -262,21 +264,86 @@ gp_port_info_list_count (GPPortInfoList *list) * @list: a #GPPortInfoList * @path: a path * - * Looks for an entry in the list with the supplied @path. + * Looks for an entry in the list with the supplied @path. If no exact match + * can be found, a regex search will be performed in the hope some driver + * claimed ports like "serial:*". * * Return value: The index of the entry or a gphoto2 error code **/ int gp_port_info_list_lookup_path (GPPortInfoList *list, const char *path) { - int i; + int i, result; + regex_t reb; +#if HAVE_GNU_REGEX + const char *rv; +#else + regmatch_t match; +#endif CHECK_NULL (list && path); + /* Exact match? */ for (i = 0; i < list->count; i++) if (!strcmp (list->info[i].path, path)) return (i); - + + /* Regex match? */ + gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list", + "Starting regex search for '%s'...", path); + for (i = 0; i < list->count; i++) { + gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list", + "Trying '%s'...", list->info[i].path); + + /* Compile the pattern */ +#if HAVE_GNU_REGEX + reb.allocated = 0; + reb.buffer = NULL; + reb.fastmap = NULL; + reb.translate = 0; + reb.no_sub = 0; + rv = re_compile_pattern (list->info[i].path, + sizeof (list->info[i].path), &reb); + if (rv) { + gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list", + "%s", rv); + continue; + } +#else + result = regcomp (&reb, list->info[i].path, REG_ICASE); + if (result) { + char buf[1024]; + if (regerror (result, &reb, buf, sizeof (buf))) + gp_log (GP_LOG_ERROR, "gphoto2-port-info-list", + "%s", buf); + else + gp_log (GP_LOG_ERROR, "gphoto2-port-info-list", + "regcomp failed"); + return (GP_ERROR_UNKNOWN_PORT); + } +#endif + + /* Try to match */ +#if HAVE_GNU_REGEX + result = re_match (&reb, path, strlen (path), 0, NULL); + if (result < 0) { + gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list", + "re_match failed (%i)", result); + continue; + } +#else + if (!regexec (&reb, path, 1, &match, 0)) { + gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list", + "regexec failed"); + continue; + } +#endif + CR (result = gp_port_info_list_append (list, list->info[i])); + strncpy (list->info[result].path, path, + sizeof (list->info[result].path)); + return (result); + } + return (GP_ERROR_UNKNOWN_PORT); } diff --git a/libgphoto2_port/serial/unix.c b/libgphoto2_port/serial/unix.c index 7a6be22f9..93b340e37 100644 --- a/libgphoto2_port/serial/unix.c +++ b/libgphoto2_port/serial/unix.c @@ -293,6 +293,12 @@ gp_port_library_list (GPPortInfoList *list) CHECK (gp_port_info_list_append (list, info)); } + /* Generic support */ + info.type = GP_PORT_SERIAL; + strncpy (info.path, "^serial", sizeof (info.path)); + strncpy (info.name, "Generic Serial Port", sizeof (info.name)); + CHECK (gp_port_info_list_append (list, info)); + return (GP_OK); }