--- rxvt-unicode/src/misc.C 2004/02/13 12:16:21 1.7 +++ rxvt-unicode/src/misc.C 2004/03/17 05:15:02 1.12 @@ -6,6 +6,7 @@ * Copyright (c) 1996 mj olesen Queen's Univ at Kingston * Copyright (c) 1997,1998 Oezguer Kesim * Copyright (c) 1998-2000 Geoff Wing + * Copyright (c) 2003-2004 Marc Lehmann * * 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 @@ -28,16 +29,38 @@ /* EXTPROTO */ char * +rxvt_wcstombs (const wchar_t *str, int len) +{ + mbstate mbs; + char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); + + char *dst = r; + while (len--) + { + int l = wcrtomb (dst, *str++, mbs); + if (l < 0) + *dst++ = '?'; + else + dst += l; + } + + *dst++ = 0; + + return r; +} + +/* EXTPROTO */ +char * rxvt_strdup (const char *str) { return str ? strdup (str) : 0; } /* EXTPROTO */ -char * +char * rxvt_r_basename (const char *str) { - char *base = STRRCHR (str, '/'); + char *base = STRRCHR (str, '/'); return (char *) (base ? base + 1 : str); } @@ -47,18 +70,70 @@ */ /* EXTPROTO */ void -rxvt_print_error (const char *fmt,...) +rxvt_vlog (const char *fmt, va_list arg_ptr) { - va_list arg_ptr; + char msg[1024]; + + vsnprintf (msg, sizeof msg, fmt, arg_ptr); + + if (GET_R && GET_R->log_hook) + (*GET_R->log_hook) (msg); + else + write (STDOUT_FILENO, msg, strlen (msg)); +} + +/* EXTPROTO */ +void +rxvt_log (const char *fmt,...) +{ + va_list arg_ptr; va_start (arg_ptr, fmt); - fprintf (stderr, APL_NAME ": "); - vfprintf (stderr, fmt, arg_ptr); - fprintf (stderr, "\n"); + rxvt_vlog (fmt, arg_ptr); va_end (arg_ptr); } /* + * Print an error message + */ +/* EXTPROTO */ +void +rxvt_warn (const char *fmt,...) +{ + va_list arg_ptr; + + rxvt_log ("%s: ", RESNAME); + + va_start (arg_ptr, fmt); + rxvt_vlog (fmt, arg_ptr); + va_end (arg_ptr); +} + +/* EXTPROTO */ +void +rxvt_fatal (const char *fmt,...) +{ + va_list arg_ptr; + + rxvt_log ("%s: ", RESNAME); + + va_start (arg_ptr, fmt); + rxvt_vlog (fmt, arg_ptr); + va_end (arg_ptr); + + rxvt_exit_failure (); +} + +class rxvt_failure_exception rxvt_failure_exception; + +/* EXTPROTO */ +void +rxvt_exit_failure () +{ + throw (rxvt_failure_exception); +} + +/* * check that the first characters of S1 match S2 * * No Match @@ -70,18 +145,19 @@ int rxvt_Str_match (const char *s1, const char *s2) { - int n = STRLEN (s2); + int n = STRLEN (s2); return ((STRNCMP (s1, s2, n) == 0) ? n : 0); } /* EXTPROTO */ -const char * +const char * rxvt_Str_skip_space (const char *str) { if (str) while (*str && isspace (*str)) str++; + return str; } @@ -93,8 +169,8 @@ char * rxvt_Str_trim (char *str) { - char *r, *s; - int n; + char *r, *s; + int n; if (!str || !*str) /* shortcut */ return str; @@ -115,6 +191,7 @@ s++; n -= 2; } + /* copy back over: forwards copy */ for (r = str; n; n--) *r++ = *s++;