diff --git a/Makefile.am b/Makefile.am index ce7a41a89..f7adf265f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = 1.5 -SUBDIRS = intl po m4 vfs slang mhl edit src lib doc syntax +SUBDIRS = intl po m4 vfs slang edit src lib doc syntax EXTRA_DIST = FAQ HACKING INSTALL.FAST MAINTAINERS README.QNX TODO pkginfo.in prototype.in mc.qpg mc.spec diff --git a/configure.ac b/configure.ac index b35fdd44d..0066dbfbb 100644 --- a/configure.ac +++ b/configure.ac @@ -25,21 +25,6 @@ dnl Keep this check close to the beginning, so that the users dnl without any glib won't have their time wasted by other checks. dnl -AC_DEFINE(WITH_GLIB, 1, [Use Glib functions]) - -dnl TODO: make option --with-glib like this: -dnl -dnl AC_ARG_WITH(glib, -dnl [AS_HELP_STRING([--with-glib], [Use glib (default is yes)])], -dnl [with_glib=$enableval], -dnl [with_glib=yes]) -dnl -dnl if test x${with_glib} = xyes; then -dnl dnl some checking stuff -dnl AC_DEFINE(WITH_GLIB, 1, [Use Glib functions]) -dnl fi - - AC_ARG_WITH(glib12, [ --with-glib12 Force using glib 1.2.x [[no]]]) @@ -606,7 +591,6 @@ slang/Makefile edit/Makefile syntax/Makefile m4/Makefile -mhl/Makefile lib/mc.ext vfs/extfs/a vfs/extfs/apt diff --git a/mhl/Makefile.am b/mhl/Makefile.am deleted file mode 100644 index e8580f513..000000000 --- a/mhl/Makefile.am +++ /dev/null @@ -1,13 +0,0 @@ -AM_CFLAGS = $(GLIB_CFLAGS) - -noinst_LIBRARIES = libmhl.a - -libmhl_a_SOURCES = \ - env.h \ - memory.h \ - shell_escape.h \ - strhash.h \ - mhl_string.c mhl_string.h \ - mhl_recode.c mhl_recode.h - -EXTRA_DIST = README diff --git a/mhl/README b/mhl/README index 7eb6c5ec2..83fb3d7a4 100644 --- a/mhl/README +++ b/mhl/README @@ -36,8 +36,6 @@ memory.h: Memory management functions string.h: String helpers -TODO: need to support multibyte codepages. - * mhl_str_dup(const char*s) -> char* [MACRO-FUNC] Safe version of strdup(), when NULL passed, returns strdup("") diff --git a/mhl/env.h b/mhl/env.h index 15ab2dc0d..4d0255487 100644 --- a/mhl/env.h +++ b/mhl/env.h @@ -3,4 +3,4 @@ #define mhl_getenv_dup(name) (mhl_str_dup(name ? getenv(name) : "")) -#endif // __MHL_ENV_H +#endif diff --git a/mhl/memory.h b/mhl/memory.h index 5b3ed3725..b00617700 100644 --- a/mhl/memory.h +++ b/mhl/memory.h @@ -1,102 +1,28 @@ #ifndef __MHL_MEM #define __MHL_MEM -#if defined _AIX - #pragma alloca -#endif - -#include #include #include -#ifdef __GNUC__ -# ifndef alloca -# define alloca __builtin_alloca -# endif -#else -# ifdef _MSC_VER -# include -# define alloca _alloca -# else -# if HAVE_ALLOCA_H -# include -# else -# ifdef _AIX -#pragma alloca -# else -# ifndef alloca /* predefined by HP cc +Olibcalls */ -char *alloca (); -# endif -# endif -# endif -# endif -#endif +/* allocate a chunk of stack memory, uninitialized */ +#define mhl_mem_alloc_u(sz) (malloc(sz)) -#ifndef HAVE_ALLOCA -# include -# if !defined(STDC_HEADERS) && defined(HAVE_MALLOC_H) -# include -# endif -# define alloca malloc -#endif - -#ifdef WITH_GLIB -# include - -# if GLIB_MAJOR_VERSION >= 2 - /* allocate a chunk of stack memory, uninitialized */ -# define mhl_mem_alloc_u(sz) (g_try_malloc(sz)) - - /* allocate a chunk of stack memory, zeroed */ -# define mhl_mem_alloc_z(sz) (g_try_malloc0(sz)) - - /* re-alloc memory chunk */ -# define mhl_mem_realloc(ptr,sz) (g_try_realloc(ptr,sz)) - -# else - - /* allocate a chunk of stack memory, uninitialized */ -# define mhl_mem_alloc_u(sz) (g_malloc(sz)) - - /* allocate a chunk of stack memory, zeroed */ -# define mhl_mem_alloc_z(sz) (g_malloc0(sz)) - - /* re-alloc memory chunk */ -# define mhl_mem_realloc(ptr,sz) (g_realloc(ptr,sz)) - -# endif - - /* allocate a chunk on stack - automatically free'd on function exit */ -# define mhl_stack_alloc(sz) (g_alloca(sz)) - -#else - - /* allocate a chunk of stack memory, uninitialized */ -# define mhl_mem_alloc_u(sz) (malloc(sz)) - - /* allocate a chunk of stack memory, zeroed */ -# define mhl_mem_alloc_z(sz) (calloc(1,sz)) - - /* re-alloc memory chunk */ -# define mhl_mem_realloc(ptr,sz) (realloc(ptr,sz)) - - /* allocate a chunk on stack - automatically free'd on function exit */ -# define mhl_stack_alloc(sz) (alloca(sz)) - -#endif +/* allocate a chunk of stack memory, zeroed */ +#define mhl_mem_alloc_z(sz) (calloc(1,sz)) /* free a chunk of memory from stack, passing NULL does no harm */ static inline void mhl_mem_free(void* ptr) { - if (ptr) -#ifdef WITH_GLIB - g_free(ptr); -#else - free(ptr); -#endif + if (ptr) free(ptr); } /* free an ptr and NULL it */ #define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0); -#endif // __MHL_MEM +/* allocate a chunk on stack - automatically free'd on function exit */ +#define mhl_stack_alloc(sz) (alloca(sz)) + +/* re-alloc memory chunk */ +#define mhl_mem_realloc(ptr,sz) (realloc(ptr,sz)) + +#endif diff --git a/mhl/mhl_recode.c b/mhl/mhl_recode.c deleted file mode 100644 index d35f95c14..000000000 --- a/mhl/mhl_recode.c +++ /dev/null @@ -1,201 +0,0 @@ -/* - Micro helper library - support recode for strings between charsets - - Copyright (C) 2009 The Free Software Foundation, Inc. - - Written by: - Slava Zanko , 2009. - - This file is part of the Midnight Commander. - - The Midnight Commander is free software; you can redistribute it - and/or modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Midnight Commander 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 - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - MA 02110-1301, USA. - */ - -#include - -#ifndef WITH_GLIB -# include -# include -#endif - -#include "src/global.h" -#include "mhl_recode.h" - -/*** global variables **************************************************/ -struct mhl_charset terminal_encoding; - -/*** file scope macro definitions **************************************/ - -/*** file scope type declarations **************************************/ - -/*** file scope variables **********************************************/ -static int is_terminal_in_utf8_mode; - -/** - names, that are used for utf-8 - - \private -*/ -static const char *str_utf8_encodings[] = { - "utf-8", - "utf8", - NULL -}; - -/** - standard 8bit encodings, no wide or multibytes characters - - \private -*/ -static const char *str_8bit_encodings[] = { - "cp-1251", - "cp1251", - "cp-1250", - "cp1250", - "cp-866", - "cp866", - "cp-850", - "cp850", - "cp-852", - "cp852", - "iso-8859", - "iso8859", - "koi8", - NULL -}; - -/*** file scope functions **********************************************/ -/* ---------------------------------------------------------------------------- */ -/** - Search charset name in charsets list. - - \param const char *_charset_name - name of charset - - \param const char **_charset_list - point to list of charsets - - \return - function return zero if _charset_name don't found in _charset_list. - function return non-zero if _charset_name found in _charset_list. - - \private -*/ -static int -mhl_recode_charset_test_class (const char *_charset_name, const char **_charset_list) -{ - int loop; - int result = 0; - - for (loop = 0; _charset_list[loop] != NULL; loop++) - { - result += (strncasecmp (_charset_name, _charset_list[loop], - strlen (_charset_list[loop])) == 0); - } - - return result; -} - -/* ---------------------------------------------------------------------------- */ -/** - Determine recode type: - \arg \c utf-8; - \arg \c 8-bits one-byte; - \arg \c 7-bits one-byte. - - \param const char *_charset_name - name of charset - - \return - function return type of charset (see declaration of enum mhl_charset_types - in mhl/mhl_recode.h) - - \todo determine multibyte charsets; - - \private -*/ - -static int -mhl_recode_get_charset_type (const char *_charset_name) -{ - if (mhl_recode_charset_test_class (_charset_name, str_utf8_encodings)) - return MHL_CHARSET_UTF8; - - if (mhl_recode_charset_test_class (_charset_name, str_8bit_encodings)) - return MHL_CHARSET_8BIT; - - return MHL_CHARSET_7BIT; -} - -/* ---------------------------------------------------------------------------- */ -/*** public functions **************************************************/ -/* ---------------------------------------------------------------------------- */ -/** - Init MHL recode subsystem. - Determine system charset and initialize it. - - \param void - function don't need any params. - \return - function don't return any data. - - \ingroup recode -*/ -void -mhl_recode_init (void) -{ -#ifdef WITH_GLIB - is_terminal_in_utf8_mode = g_get_charset (&(terminal_encoding.name)); -#else - const char *local_lang_name = getenv ("LANG"); - is_terminal_in_utf8_mode = FALSE; - - if (local_lang_name) - { - terminal_encoding.name = strchr (local_lang_name, '.') + 1; - - if (!strncasecmp (terminal_encoding.name, "UTF-8", 5)) - is_terminal_in_utf8_mode = TRUE; - } -#endif - mhl_recode_charset_init (&terminal_encoding); -} - -/* ---------------------------------------------------------------------------- */ -/** - Init recode structure. - - \param struct mhl_charset *_charset - point to structure mhl_charset - - \return - function don't return any data. - - \ingroup recode -*/ - -void -mhl_recode_charset_init (struct mhl_charset *_charset) -{ - if (_charset == NULL || _charset->name == NULL) - return; - - _charset->type = mhl_recode_get_charset_type (_charset->name); - - return; -} - -/* ---------------------------------------------------------------------------- */ diff --git a/mhl/mhl_recode.h b/mhl/mhl_recode.h deleted file mode 100644 index 3c825162d..000000000 --- a/mhl/mhl_recode.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef __MHL_RECODE_H -#define __MHL_RECODE_H - -#ifdef WITH_GLIB -# include -typedef GIConv mhl_iconv; -#else -# include -typedef iconv_t mhl_iconv; -#endif -#include "mhl/memory.h" - -/** - List of charsets types - - \todo support for multibyte charsets (except utf-8) -*/ -enum mhl_charset_types -{ - /** multibyte chatsets. Ex: utf-16, KANJI, ... - Reserved for future use */ - MHL_CHARSET_MULTIBYTE, - - /** UTF-8 */ - MHL_CHARSET_UTF8, - - /** 8-bit charset */ - MHL_CHARSET_8BIT, - - /** others handle as 7-bit charsets */ - MHL_CHARSET_7BIT -}; - -struct mhl_charset -{ - const char *name; - int type; -}; - -void mhl_recode_init (void); - -void mhl_recode_charset_init (struct mhl_charset *); - - -#define mhl_recode_charset_new() mhl_mem_alloc_z ( sizeof ( struct mhl_charset ) ) -#define mhl_recode_charset_stack_new() mhl_stack_alloc ( sizeof ( struct mhl_charset ) ) -#define mhl_recode_charset_delete(sz) mhl_mem_free ( sz ) - - -#endif // __MHL_RECODE_H diff --git a/mhl/mhl_string.c b/mhl/mhl_string.c deleted file mode 100644 index 0973b32b5..000000000 --- a/mhl/mhl_string.c +++ /dev/null @@ -1,168 +0,0 @@ -/* Micro helper library - strings - - Copyright (C) 2009 Free Software Foundation, Inc. - - Written by: - 2009 Enrico Weigelt - 2009 Slava Zanko - 2009 Patrick Winnertz - - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include -#include "../mhl/mhl_string.h" - -/* ---------------------------------------------------------------------------- */ -inline char * -mhl_str_reverse (char *ptr) -{ - if (!ptr) - return NULL; // missing string - if (!(ptr[0] && ptr[1])) - return ptr; // empty or 1-ch string - - size_t _sz = strlen (ptr); - char *start = ptr; - char *end = ptr + _sz - 1; - - while (start < end) - { - char c = *start; - *start = *end; - *end = c; - start++; - end--; - } - - return ptr; -} - -/* ---------------------------------------------------------------------------- */ -inline char * -__mhl_str_concat_hlp (const char *base, ...) -{ - static const char *arg_ptr[__STR_CONCAT_MAX]; - static size_t arg_sz[__STR_CONCAT_MAX]; - int count = 0; - size_t totalsize = 0; - - // first pass: scan through the params and count string sizes - va_list par; - - if (base) - { - arg_ptr[0] = base; - arg_sz[0] = totalsize = strlen (base); - count = 1; - } - - va_list args; - va_start (args, base); - char *a; - // note: we use ((char*)(1)) as terminator - NULL is a valid argument ! - while ((a = va_arg (args, char *)) != (char *) 1) - { - // printf("a=%u\n", a); - if (a) - { - arg_ptr[count] = a; - arg_sz[count] = strlen (a); - totalsize += arg_sz[count]; - count++; - } - } - - if (!count) - return mhl_str_dup (""); - - // now as we know how much to copy, allocate the buffer - char *buffer = (char *) mhl_mem_alloc_u (totalsize + 2); - char *current = buffer; - int x = 0; - for (x = 0; x < count; x++) - { - memcpy (current, arg_ptr[x], arg_sz[x]); - current += arg_sz[x]; - } - - *current = 0; - return buffer; -} - -/* ---------------------------------------------------------------------------- */ -inline void -mhl_str_toupper (char *str) -{ - /* - FIXME: need to upper miltibyte strings... utf-8 for example. - And after UPPER byte-lenght of wide string may change. - This mean, need to create new string. - */ - if (str) - for (; *str; str++) - *str = toupper (*str); -} - -/* ---------------------------------------------------------------------------- */ -inline char * -mhl_str_trim (char *str) -{ - if (!str) - return NULL; // NULL string ?! bail out. - - // find the first non-space - char *start; - for (start = str; ((*str) && (!isspace (*str))); str++); - - // only spaces ? - if (!(*str)) - { - *str = 0; - return str; - } - - // get the size (cannot be empty - catched above) - size_t _sz = strlen (str); - - // find the proper end - char *end; - for (end = (str + _sz - 1); ((end > str) && (isspace (*end))); end--); - end[1] = 0; // terminate, just to be sure - - // if we have no leading spaces, just trucate - if (start == str) - { - end++; - *end = 0; - return str; - } - - - // if it' only one char, dont need memmove for that - if (start == end) - { - str[0] = *start; - str[1] = 0; - return str; - } - - // by here we have a (non-empty) region between start end end - memmove (str, start, (end - start + 1)); - return str; -} - -/* ---------------------------------------------------------------------------- */ diff --git a/mhl/mhl_string.h b/mhl/mhl_string.h deleted file mode 100644 index 236fad2ae..000000000 --- a/mhl/mhl_string.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef __MHL_STRING_H -#define __MHL_STRING_H - -#include -#include -#include "../mhl/memory.h" - -#define mhl_str_dup(str) ((str ? strdup(str) : strdup(""))) -#define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup(""))) -#define mhl_str_len(str) ((str ? strlen(str) : 0)) - -inline char* mhl_str_trim(char*); - -inline void mhl_str_toupper(char*); - -#define __STR_CONCAT_MAX 32 - -/* _NEVER_ call this function directly ! */ -inline char* __mhl_str_concat_hlp(const char*, ...); - -#define mhl_str_concat(...) (__mhl_str_concat_hlp(__VA_ARGS__, (char*)(1))) - -inline char* mhl_str_reverse(char*); - -#endif // __MHL_STRING_H diff --git a/mhl/shell_escape.h b/mhl/shell_escape.h index 2e2fa898b..253338842 100644 --- a/mhl/shell_escape.h +++ b/mhl/shell_escape.h @@ -109,4 +109,4 @@ static inline char* mhl_shell_unescape_buf(char* text) return text; } -#endif // __MHL_SHELL_ESCAPE_H +#endif diff --git a/mhl/strhash.h b/mhl/strhash.h index 4b5279729..76c14e8b9 100644 --- a/mhl/strhash.h +++ b/mhl/strhash.h @@ -46,4 +46,4 @@ static inline void* mhl_strhash_lookup(MHL_STRHASH* ht, const char* key) return NULL; } -#endif // __MHL_STRHASH_H +#endif diff --git a/mhl/string.h b/mhl/string.h new file mode 100644 index 000000000..fa99a7b92 --- /dev/null +++ b/mhl/string.h @@ -0,0 +1,124 @@ +#ifndef __MHL_STRING_H +#define __MHL_STRING_H + +#include +#include +#include "../mhl/memory.h" + +#define mhl_str_dup(str) ((str ? strdup(str) : strdup(""))) +#define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup(""))) +#define mhl_str_len(str) ((str ? strlen(str) : 0)) + +static inline char* mhl_str_trim(char* str) +{ + if (!str) return NULL; // NULL string ?! bail out. + + // find the first non-space + char* start; for (start=str; ((*str) && (!isspace(*str))); str++); + + // only spaces ? + if (!(*str)) { *str = 0; return str; } + + // get the size (cannot be empty - catched above) + size_t _sz = strlen(str); + + // find the proper end + char* end; + for (end=(str+_sz-1); ((end>str) && (isspace(*end))); end--); + end[1] = 0; // terminate, just to be sure + + // if we have no leading spaces, just trucate + if (start==str) { end++; *end = 0; return str; } + + + // if it' only one char, dont need memmove for that + if (start==end) { str[0]=*start; str[1]=0; return str; } + + // by here we have a (non-empty) region between start end end + memmove(str,start,(end-start+1)); + return str; +} + +static inline void mhl_str_toupper(char* str) +{ + if (str) + for (;*str;str++) + *str = toupper(*str); +} + +#define __STR_CONCAT_MAX 32 +/* _NEVER_ call this function directly ! */ +static inline char* __mhl_str_concat_hlp(const char* base, ...) +{ + static const char* arg_ptr[__STR_CONCAT_MAX]; + static size_t arg_sz[__STR_CONCAT_MAX]; + int count = 0; + size_t totalsize = 0; + + // first pass: scan through the params and count string sizes + va_list par; + + if (base) + { + arg_ptr[0] = base; + arg_sz[0] = totalsize = strlen(base); + count = 1; + } + + va_list args; + va_start(args,base); + char* a; + // note: we use ((char*)(1)) as terminator - NULL is a valid argument ! + while ((a = va_arg(args, char*))!=(char*)1) + { +// printf("a=%u\n", a); + if (a) + { + arg_ptr[count] = a; + arg_sz[count] = strlen(a); + totalsize += arg_sz[count]; + count++; + } + } + + if (!count) + return mhl_str_dup(""); + + // now as we know how much to copy, allocate the buffer + char* buffer = (char*)mhl_mem_alloc_u(totalsize+2); + char* current = buffer; + int x=0; + for (x=0; x #endif -#ifndef WITH_GLIB -# define TRUE (int)1 -# define FALSE (int)0 -#endif - #endif