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 */
28 #include "misc.intpro" /* PROTOS for internal routines */
31 rxvt_wcstombs (const wchar_t *str, int len)
33 if (len < 0) len = wcslen (str);
36 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
41 int l = wcrtomb (dst, *str++, mbs);
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 (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);
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);
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 && s[0] >= 0xc2 && s[0] <= 0xdf
119 && (s[1] & 0xc0) == 0x80)
121 *p++ = ((s[0] & 0x1f) << 6)
126 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
127 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
128 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
129 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
131 && (s[2] & 0xc0) == 0x80)
133 *p++ = ((s[0] & 0x0f) << 12)
134 | ((s[1] & 0x3f) << 6)
139 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
140 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
141 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
143 && (s[2] & 0xc0) == 0x80
144 && (s[3] & 0xc0) == 0x80)
146 *p++ = ((s[0] & 0x07) << 18)
147 | ((s[1] & 0x3f) << 12)
148 | ((s[2] & 0x3f) << 6)
165 rxvt_strdup (const char *str)
167 return str ? strdup (str) : 0;
172 rxvt_r_basename (const char *str)
174 char *base = strrchr (str, '/');
176 return (char *) (base ? base + 1 : str);
180 * Print an error message
184 rxvt_vlog (const char *fmt, va_list arg_ptr)
188 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
190 if (GET_R && GET_R->log_hook)
191 (*GET_R->log_hook) (msg);
193 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
212 rxvt_warn (const char *fmt,...)
216 rxvt_log ("%s: ", RESNAME);
218 va_start (arg_ptr, fmt);
219 rxvt_vlog (fmt, arg_ptr);
225 rxvt_fatal (const char *fmt,...)
229 rxvt_log ("%s: ", RESNAME);
231 va_start (arg_ptr, fmt);
232 rxvt_vlog (fmt, arg_ptr);
235 rxvt_exit_failure ();
238 class rxvt_failure_exception rxvt_failure_exception;
244 throw (rxvt_failure_exception);
248 * check that the first characters of S1 match S2
253 * return: strlen (S2)
257 rxvt_Str_match (const char *s1, const char *s2)
261 return ((strncmp (s1, s2, n) == 0) ? n : 0);
266 rxvt_Str_skip_space (const char *str)
269 while (*str && isspace (*str))
276 * remove leading/trailing space and strip-off leading/trailing quotes.
281 rxvt_Str_trim (char *str)
286 if (!str || !*str) /* shortcut */
289 /* skip leading spaces */
290 for (s = str; *s && isspace (*s); s++) ;
291 /* goto end of string */
292 for (n = 0, r = s; *r++; n++) ;
295 if (n > 0 && *r == '\n')
297 /* backtrack along trailing spaces */
298 for (; n > 0 && isspace (*r); r--, n--) ;
299 /* skip matching leading/trailing quotes */
300 if (*s == '"' && *r == '"' && n > 1)
306 /* copy back over: forwards copy */
307 for (r = str; n; n--)
315 * in-place interpretation of string:
317 * backslash-escaped: "\a\b\E\e\n\r\t", "\octal"
318 * Ctrl chars: ^@ .. ^_, ^?
320 * Emacs-style: "M-" prefix
323 * "M-x" prefixed strings, append "\r" if needed
324 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
326 * returns the converted string length
330 rxvt_Str_escaped (char *str)
333 int i, num, append = 0;
340 if (*s == 'M' && s[1] == '-')
342 /* Emacs convenience, replace leading `M-..' with `\E..' */
345 if (toupper (*s) == 'X')
346 /* append carriage-return for `M-xcommand' */
347 for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ;
354 if (ch >= '0' && ch <= '7')
357 for (i = 0; i < 2; i++, s++)
360 if (ch < '0' || ch > '7')
362 num = num * 8 + ch - '0';
367 ch = C0_BEL; /* bell */
369 ch = C0_BS; /* backspace */
370 else if (ch == 'E' || ch == 'e')
371 ch = C0_ESC; /* escape */
373 ch = '\n'; /* newline */
375 ch = '\r'; /* carriage-return */
377 ch = C0_HT; /* tab */
383 ch = (ch == '?' ? 127 : (ch - '@'));
388 /* ESC] is an XTerm escape sequence, must be terminated */
389 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']')
392 /* add trailing character as required */
393 if (append && d[-1] != append)
401 * Split a comma-separated string into an array, stripping leading and
402 * trailing spaces (and paired quotes) from each entry. Empty strings
403 * are properly returned
404 * Caller should free each entry and array when done
408 rxvt_splitcommastring (const char *cs)
414 if ((s = cs) == NULL)
417 for (n = 1, t = s; *t; t++)
420 ret = (char **)malloc ((n + 1) * sizeof (char *));
423 for (l = 0, t = s; l < n; l++)
425 for ( ; *t && *t != ','; t++) ;
427 ret[l] = (char *)malloc (p + 1);
428 strncpy (ret[l], s, p);
430 rxvt_Str_trim (ret[l]);
437 rxvt_freecommastring (char **cs)
439 for (int i = 0; cs[i]; ++i)
445 /*----------------------------------------------------------------------*
449 /* #define DEBUG_SEARCH_PATH */
451 #if defined (XPM_BACKGROUND) || (MENUBAR_MAX)
453 * search for FILE in the current working directory, and within the
454 * colon-delimited PATHLIST, adding the file extension EXT if required.
456 * FILE is either semi-colon or zero terminated
460 rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
463 const char *p, *path;
466 if (!access (file, R_OK)) /* found (plain name) in current directory */
467 return strdup (file);
469 /* semi-colon delimited */
470 if ((p = strchr (file, ';')))
475 #ifdef DEBUG_SEARCH_PATH
476 getcwd (name, sizeof (name));
477 fprintf (stderr, "pwd: \"%s\"\n", name);
478 fprintf (stderr, "find: \"%.*s\"\n", len, file);
481 /* leave room for an extra '/' and trailing '\0' */
482 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
486 /* check if we can find it now */
487 strncpy (name, file, len);
490 if (!access (name, R_OK))
491 return strdup (name);
495 if (!access (name, R_OK))
496 return strdup (name);
498 for (path = pathlist; path != NULL && *path != '\0'; path = p)
502 /* colon delimited */
503 if ((p = strchr (path, ':')) == NULL)
504 p = strchr (path, '\0');
510 if (n > 0 && n <= maxpath)
512 strncpy (name, path, n);
513 if (name[n - 1] != '/')
516 strncat (name, file, len);
518 if (!access (name, R_OK))
519 return strdup (name);
523 if (!access (name, R_OK))
524 return strdup (name);
533 rxvt_File_find (const char *file, const char *ext, const char *path)
537 if (file == NULL || *file == '\0')
540 /* search environment variables here too */
541 if ((f = rxvt_File_search_path (path, file, ext)) == NULL)
543 if ((f = rxvt_File_search_path (getenv (PATH_ENV), file, ext)) == NULL)
545 f = rxvt_File_search_path (getenv ("PATH"), file, ext);
547 #ifdef DEBUG_SEARCH_PATH
549 fprintf (stderr, "found: \"%s\"\n", f);
554 #endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */
556 /*----------------------------------------------------------------------*
557 * miscellaneous drawing routines
561 * Draw top/left and bottom/right border shadows around windows
563 #if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
566 rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
570 shadow = (w == 0 || h == 0) ? 1 : SHADOW;
573 for (; shadow-- > 0; x++, y++, w--, h--)
575 XDrawLine (display, win, topShadow, x, y, w, y);
576 XDrawLine (display, win, topShadow, x, y, x, h);
577 XDrawLine (display, win, botShadow, w, h, w, y + 1);
578 XDrawLine (display, win, botShadow, w, h, x + 1, h);
587 rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
591 case 'r': /* right triangle */
592 XDrawLine (display, win, topShadow, x, y, x, y + w);
593 XDrawLine (display, win, topShadow, x, y, x + w, y + w / 2);
594 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w / 2);
597 case 'l': /* left triangle */
598 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
599 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w / 2);
600 XDrawLine (display, win, topShadow, x, y + w / 2, x + w, y);
603 case 'd': /* down triangle */
604 XDrawLine (display, win, topShadow, x, y, x + w / 2, y + w);
605 XDrawLine (display, win, topShadow, x, y, x + w, y);
606 XDrawLine (display, win, botShadow, x + w, y, x + w / 2, y + w);
609 case 'u': /* up triangle */
610 XDrawLine (display, win, botShadow, x + w, y + w, x + w / 2, y);
611 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w);
612 XDrawLine (display, win, topShadow, x, y + w, x + w / 2, y);
615 case 's': /* square */
616 XDrawLine (display, win, topShadow, x + w, y, x, y);
617 XDrawLine (display, win, topShadow, x, y, x, y + w);
618 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w);
619 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
626 /*----------------------- end-of-file (C source) -----------------------*/