1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston
7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
8 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com>
9 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *----------------------------------------------------------------------*/
26 #include "../config.h" /* NECESSARY */
27 #include "rxvt.h" /* NECESSARY */
30 rxvt_wcstombs (const wchar_t *str, int len)
32 if (len < 0) len = wcslen (str);
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
40 ssize_t l = wcrtomb (dst, *str++, mbs);
50 return (char *)rxvt_realloc (r, dst - r);
54 rxvt_mbstowcs (const char *str, int len)
56 if (len < 0) len = strlen (str);
58 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
60 if ((ssize_t)mbstowcs (r, str, len + 1) < 0)
67 rxvt_wcstoutf8 (const wchar_t *str, int len)
69 if (len < 0) len = wcslen (str);
71 char *r = (char *)rxvt_malloc (len * 4 + 1);
76 unicode_t w = *str++ & UNICODE_MASK;
80 else if (w < 0x000800)
81 *p++ = 0xc0 | ( w >> 6),
82 *p++ = 0x80 | ( w & 0x3f);
83 else if (w < 0x010000)
84 *p++ = 0xe0 | ( w >> 12 ),
85 *p++ = 0x80 | ((w >> 6) & 0x3f),
86 *p++ = 0x80 | ( w & 0x3f);
87 else if (w < 0x110000)
88 *p++ = 0xf0 | ( w >> 18),
89 *p++ = 0x80 | ((w >> 12) & 0x3f),
90 *p++ = 0x80 | ((w >> 6) & 0x3f),
91 *p++ = 0x80 | ( w & 0x3f);
98 return (char *)rxvt_realloc (r, p - r);
102 rxvt_utf8towcs (const char *str, int len)
104 if (len < 0) len = strlen (str);
106 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)),
109 unsigned char *s = (unsigned char *)str,
118 else if (s[0] < 0x80)
121 && s[0] >= 0xc2 && s[0] <= 0xdf
122 && (s[1] & 0xc0) == 0x80)
124 *p++ = ((s[0] & 0x1f) << 6)
129 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
130 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
131 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
132 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
134 && (s[2] & 0xc0) == 0x80)
136 *p++ = ((s[0] & 0x0f) << 12)
137 | ((s[1] & 0x3f) << 6)
142 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
143 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
144 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
146 && (s[2] & 0xc0) == 0x80
147 && (s[3] & 0xc0) == 0x80)
149 *p++ = ((s[0] & 0x07) << 18)
150 | ((s[1] & 0x3f) << 12)
151 | ((s[2] & 0x3f) << 6)
168 rxvt_strdup (const char *str)
170 return str ? strdup (str) : 0;
174 rxvt_r_basename (const char *str)
176 char *base = strrchr (str, '/');
178 return (char *) (base ? base + 1 : str);
182 * Print an error message
185 rxvt_vlog (const char *fmt, va_list arg_ptr)
189 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
191 if (GET_R && GET_R->log_hook)
192 (*GET_R->log_hook) (msg);
194 write (STDOUT_FILENO, msg, strlen (msg));
198 rxvt_log (const char *fmt,...)
202 va_start (arg_ptr, fmt);
203 rxvt_vlog (fmt, arg_ptr);
208 * Print an error message
211 rxvt_warn (const char *fmt,...)
215 rxvt_log ("%s: ", RESNAME);
217 va_start (arg_ptr, fmt);
218 rxvt_vlog (fmt, arg_ptr);
223 rxvt_fatal (const char *fmt,...)
227 rxvt_log ("%s: ", RESNAME);
229 va_start (arg_ptr, fmt);
230 rxvt_vlog (fmt, arg_ptr);
233 rxvt_exit_failure ();
236 class rxvt_failure_exception rxvt_failure_exception;
241 throw (rxvt_failure_exception);
245 * check that the first characters of S1 match S2
250 * return: strlen (S2)
253 rxvt_Str_match (const char *s1, const char *s2)
257 return ((strncmp (s1, s2, n) == 0) ? n : 0);
261 rxvt_Str_skip_space (const char *str)
264 while (*str && isspace (*str))
271 * remove leading/trailing space and strip-off leading/trailing quotes.
275 rxvt_Str_trim (char *str)
279 if (!str || !*str) /* shortcut */
282 /* skip leading spaces */
283 for (s = str; *s && isspace (*s); s++) ;
285 /* goto end of string */
286 r = s + strlen (s) - 1;
288 /* dump return and other trailing whitespace */
289 while (r > s && isspace (*r))
293 /* skip matching leading/trailing quotes */
294 if (*s == '"' && *r == '"' && n > 1)
301 memmove (str, s, r + 1 - s);
308 * in-place interpretation of string:
310 * backslash-escaped: "\a\b\E\e\n\r\t", "\octal"
311 * Ctrl chars: ^@ .. ^_, ^?
313 * Emacs-style: "M-" prefix
316 * "M-x" prefixed strings, append "\r" if needed
317 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
319 * returns the converted string length
322 rxvt_Str_escaped (char *str)
325 int i, num, append = 0;
332 if (*s == 'M' && s[1] == '-')
334 /* Emacs convenience, replace leading `M-..' with `\E..' */
337 if (toupper (*s) == 'X')
338 /* append carriage-return for `M-xcommand' */
339 for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ;
346 if (ch >= '0' && ch <= '7')
349 for (i = 0; i < 2; i++, s++)
352 if (ch < '0' || ch > '7')
354 num = num * 8 + ch - '0';
359 ch = C0_BEL; /* bell */
361 ch = C0_BS; /* backspace */
362 else if (ch == 'E' || ch == 'e')
363 ch = C0_ESC; /* escape */
365 ch = '\n'; /* newline */
367 ch = '\r'; /* carriage-return */
369 ch = C0_HT; /* tab */
375 ch = (ch == '?' ? 127 : (ch - '@'));
380 /* ESC] is an XTerm escape sequence, must be terminated */
381 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']')
384 /* add trailing character as required */
385 if (append && d[-1] != append)
393 * Split a comma-separated string into an array, stripping leading and
394 * trailing spaces from each entry. Empty strings are properly returned
395 * Caller should free each entry and array when done
398 rxvt_splitcommastring (const char *cs)
404 if ((s = cs) == NULL)
407 for (n = 1, t = s; *t; t++)
411 ret = (char **)malloc ((n + 1) * sizeof (char *));
414 for (l = 0, t = s; l < n; l++)
416 for ( ; *t && *t != ','; t++) ;
418 ret[l] = (char *)malloc (p + 1);
419 strncpy (ret[l], s, p);
421 rxvt_Str_trim (ret[l]);
429 rxvt_freecommastring (char **cs)
431 for (int i = 0; cs[i]; ++i)
437 /*----------------------------------------------------------------------*
441 /* #define DEBUG_SEARCH_PATH */
443 #ifdef XPM_BACKGROUND
445 * search for FILE in the current working directory, and within the
446 * colon-delimited PATHLIST, adding the file extension EXT if required.
448 * FILE is either semi-colon or zero terminated
451 rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
454 const char *p, *path;
457 if (!access (file, R_OK)) /* found (plain name) in current directory */
458 return strdup (file);
460 /* semi-colon delimited */
461 if ((p = strchr (file, ';')))
466 #ifdef DEBUG_SEARCH_PATH
467 getcwd (name, sizeof (name));
468 fprintf (stderr, "pwd: \"%s\"\n", name);
469 fprintf (stderr, "find: \"%.*s\"\n", len, file);
472 /* leave room for an extra '/' and trailing '\0' */
473 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
477 /* check if we can find it now */
478 strncpy (name, file, len);
481 if (!access (name, R_OK))
482 return strdup (name);
486 if (!access (name, R_OK))
487 return strdup (name);
489 for (path = pathlist; path != NULL && *path != '\0'; path = p)
493 /* colon delimited */
494 if ((p = strchr (path, ':')) == NULL)
495 p = strchr (path, '\0');
501 if (n > 0 && n <= maxpath)
503 strncpy (name, path, n);
504 if (name[n - 1] != '/')
507 strncat (name, file, len);
509 if (!access (name, R_OK))
510 return strdup (name);
514 if (!access (name, R_OK))
515 return strdup (name);
523 rxvt_File_find (const char *file, const char *ext, const char *path)
527 if (file == NULL || *file == '\0')
530 f = rxvt_File_search_path (path, file, ext);
532 #ifdef DEBUG_SEARCH_PATH
534 fprintf (stderr, "found: \"%s\"\n", f);
541 // should not be used in interactive programs, for obvious reasons
542 void rxvt_usleep (int usecs)
548 ts.tv_nsec = usecs * 1000;
549 nanosleep (&ts, NULL);
551 /* use select for timing */
556 select (0, NULL, NULL, NULL, &tv);
560 /*----------------------- end-of-file (C source) -----------------------*/