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 */
32 rxvt_wcstombs (const wchar_t *str, int len)
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
40 int l = wcrtomb (dst, *str++, mbs);
54 rxvt_strdup (const char *str)
56 return str ? strdup (str) : 0;
61 rxvt_r_basename (const char *str)
63 char *base = STRRCHR (str, '/');
65 return (char *) (base ? base + 1 : str);
69 * Print an error message
73 rxvt_vlog (const char *fmt, va_list arg_ptr)
77 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
79 if (GET_R && GET_R->log_hook)
80 (*GET_R->log_hook) (msg);
82 write (STDOUT_FILENO, msg, strlen (msg));
87 rxvt_log (const char *fmt,...)
91 va_start (arg_ptr, fmt);
92 rxvt_vlog (fmt, arg_ptr);
97 * Print an error message
101 rxvt_warn (const char *fmt,...)
105 rxvt_log ("%s: ", RESNAME);
107 va_start (arg_ptr, fmt);
108 rxvt_vlog (fmt, arg_ptr);
114 rxvt_fatal (const char *fmt,...)
118 rxvt_log ("%s: ", RESNAME);
120 va_start (arg_ptr, fmt);
121 rxvt_vlog (fmt, arg_ptr);
124 rxvt_exit_failure ();
127 class rxvt_failure_exception rxvt_failure_exception;
133 throw (rxvt_failure_exception);
137 * check that the first characters of S1 match S2
142 * return: STRLEN (S2)
146 rxvt_Str_match (const char *s1, const char *s2)
150 return ((STRNCMP (s1, s2, n) == 0) ? n : 0);
155 rxvt_Str_skip_space (const char *str)
158 while (*str && isspace (*str))
165 * remove leading/trailing space and strip-off leading/trailing quotes.
170 rxvt_Str_trim (char *str)
175 if (!str || !*str) /* shortcut */
178 /* skip leading spaces */
179 for (s = str; *s && isspace (*s); s++) ;
180 /* goto end of string */
181 for (n = 0, r = s; *r++; n++) ;
184 if (n > 0 && *r == '\n')
186 /* backtrack along trailing spaces */
187 for (; n > 0 && isspace (*r); r--, n--) ;
188 /* skip matching leading/trailing quotes */
189 if (*s == '"' && *r == '"' && n > 1)
195 /* copy back over: forwards copy */
196 for (r = str; n; n--)
204 * in-place interpretation of string:
206 * backslash-escaped: "\a\b\E\e\n\r\t", "\octal"
207 * Ctrl chars: ^@ .. ^_, ^?
209 * Emacs-style: "M-" prefix
212 * "M-x" prefixed strings, append "\r" if needed
213 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
215 * returns the converted string length
219 rxvt_Str_escaped (char *str)
222 int i, num, append = 0;
229 if (*s == 'M' && s[1] == '-')
231 /* Emacs convenience, replace leading `M-..' with `\E..' */
234 if (toupper (*s) == 'X')
235 /* append carriage-return for `M-xcommand' */
236 for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ;
243 if (ch >= '0' && ch <= '7')
246 for (i = 0; i < 2; i++, s++)
249 if (ch < '0' || ch > '7')
251 num = num * 8 + ch - '0';
256 ch = C0_BEL; /* bell */
258 ch = C0_BS; /* backspace */
259 else if (ch == 'E' || ch == 'e')
260 ch = C0_ESC; /* escape */
262 ch = '\n'; /* newline */
264 ch = '\r'; /* carriage-return */
266 ch = C0_HT; /* tab */
272 ch = (ch == '?' ? 127 : (ch - '@'));
277 /* ESC] is an XTerm escape sequence, must be terminated */
278 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']')
281 /* add trailing character as required */
282 if (append && d[-1] != append)
290 * Split a comma-separated string into an array, stripping leading and
291 * trailing spaces (and paired quotes) from each entry. Empty strings
292 * are properly returned
293 * Caller should free each entry and array when done
297 rxvt_splitcommastring (const char *cs)
303 if ((s = cs) == NULL)
306 for (n = 1, t = s; *t; t++)
309 ret = (char **)malloc ((n + 1) * sizeof (char *));
312 for (l = 0, t = s; l < n; l++)
314 for ( ; *t && *t != ','; t++) ;
316 ret[l] = (char *)malloc (p + 1);
317 strncpy (ret[l], s, p);
319 rxvt_Str_trim (ret[l]);
325 /*----------------------------------------------------------------------*
329 /* #define DEBUG_SEARCH_PATH */
331 #if defined (XPM_BACKGROUND) || (MENUBAR_MAX)
333 * search for FILE in the current working directory, and within the
334 * colon-delimited PATHLIST, adding the file extension EXT if required.
336 * FILE is either semi-colon or zero terminated
340 rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
343 const char *p, *path;
346 if (!access (file, R_OK)) /* found (plain name) in current directory */
347 return STRDUP (file);
349 /* semi-colon delimited */
350 if ((p = STRCHR (file, ';')))
355 #ifdef DEBUG_SEARCH_PATH
356 getcwd (name, sizeof (name));
357 fprintf (stderr, "pwd: \"%s\"\n", name);
358 fprintf (stderr, "find: \"%.*s\"\n", len, file);
361 /* leave room for an extra '/' and trailing '\0' */
362 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2);
366 /* check if we can find it now */
367 STRNCPY (name, file, len);
370 if (!access (name, R_OK))
371 return STRDUP (name);
375 if (!access (name, R_OK))
376 return STRDUP (name);
378 for (path = pathlist; path != NULL && *path != '\0'; path = p)
382 /* colon delimited */
383 if ((p = STRCHR (path, ':')) == NULL)
384 p = STRCHR (path, '\0');
390 if (n > 0 && n <= maxpath)
392 STRNCPY (name, path, n);
393 if (name[n - 1] != '/')
396 STRNCAT (name, file, len);
398 if (!access (name, R_OK))
399 return STRDUP (name);
403 if (!access (name, R_OK))
404 return STRDUP (name);
413 rxvt_File_find (const char *file, const char *ext, const char *path)
417 if (file == NULL || *file == '\0')
420 /* search environment variables here too */
421 if ((f = rxvt_File_search_path (path, file, ext)) == NULL)
423 if ((f = rxvt_File_search_path (getenv (PATH_ENV), file, ext)) == NULL)
425 f = rxvt_File_search_path (getenv ("PATH"), file, ext);
427 #ifdef DEBUG_SEARCH_PATH
429 fprintf (stderr, "found: \"%s\"\n", f);
434 #endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */
436 /*----------------------------------------------------------------------*
437 * miscellaneous drawing routines
441 * Draw top/left and bottom/right border shadows around windows
443 #if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
446 rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
450 shadow = (w == 0 || h == 0) ? 1 : SHADOW;
453 for (; shadow-- > 0; x++, y++, w--, h--)
455 XDrawLine (display, win, topShadow, x, y, w, y);
456 XDrawLine (display, win, topShadow, x, y, x, h);
457 XDrawLine (display, win, botShadow, w, h, w, y + 1);
458 XDrawLine (display, win, botShadow, w, h, x + 1, h);
467 rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
471 case 'r': /* right triangle */
472 XDrawLine (display, win, topShadow, x, y, x, y + w);
473 XDrawLine (display, win, topShadow, x, y, x + w, y + w / 2);
474 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w / 2);
477 case 'l': /* left triangle */
478 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
479 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w / 2);
480 XDrawLine (display, win, topShadow, x, y + w / 2, x + w, y);
483 case 'd': /* down triangle */
484 XDrawLine (display, win, topShadow, x, y, x + w / 2, y + w);
485 XDrawLine (display, win, topShadow, x, y, x + w, y);
486 XDrawLine (display, win, botShadow, x + w, y, x + w / 2, y + w);
489 case 'u': /* up triangle */
490 XDrawLine (display, win, botShadow, x + w, y + w, x + w / 2, y);
491 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w);
492 XDrawLine (display, win, topShadow, x, y + w, x + w / 2, y);
495 case 's': /* square */
496 XDrawLine (display, win, topShadow, x + w, y, x, y);
497 XDrawLine (display, win, topShadow, x, y, x, y + w);
498 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w);
499 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
506 /*----------------------- end-of-file (C source) -----------------------*/