1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: misc.c |
2 | * File: misc.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
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 |
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> |
7 | * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> |
8 | * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> |
8 | * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> |
|
|
9 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
9 | * |
10 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation; either version 2 of the License, or |
13 | * the Free Software Foundation; either version 2 of the License, or |
13 | * (at your option) any later version. |
14 | * (at your option) any later version. |
… | |
… | |
26 | #include "rxvt.h" /* NECESSARY */ |
27 | #include "rxvt.h" /* NECESSARY */ |
27 | #include "misc.intpro" /* PROTOS for internal routines */ |
28 | #include "misc.intpro" /* PROTOS for internal routines */ |
28 | |
29 | |
29 | /* EXTPROTO */ |
30 | /* EXTPROTO */ |
30 | char * |
31 | char * |
|
|
32 | rxvt_wcstombs (const wchar_t *str, int len) |
|
|
33 | { |
|
|
34 | mbstate mbs; |
|
|
35 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
|
|
36 | |
|
|
37 | char *dst = r; |
|
|
38 | while (len--) |
|
|
39 | { |
|
|
40 | int l = wcrtomb (dst, *str++, mbs); |
|
|
41 | if (l < 0) |
|
|
42 | *dst++ = '?'; |
|
|
43 | else |
|
|
44 | dst += l; |
|
|
45 | } |
|
|
46 | |
|
|
47 | *dst++ = 0; |
|
|
48 | |
|
|
49 | return r; |
|
|
50 | } |
|
|
51 | |
|
|
52 | /* EXTPROTO */ |
|
|
53 | char * |
31 | rxvt_strdup (const char *str) |
54 | rxvt_strdup (const char *str) |
32 | { |
55 | { |
33 | return str ? strdup (str) : 0; |
56 | return str ? strdup (str) : 0; |
34 | } |
57 | } |
35 | |
58 | |
36 | /* EXTPROTO */ |
59 | /* EXTPROTO */ |
37 | char * |
60 | char * |
38 | rxvt_r_basename (const char *str) |
61 | rxvt_r_basename (const char *str) |
39 | { |
62 | { |
40 | char *base = STRRCHR (str, '/'); |
63 | char *base = STRRCHR (str, '/'); |
41 | |
64 | |
42 | return (char *) (base ? base + 1 : str); |
65 | return (char *) (base ? base + 1 : str); |
43 | } |
66 | } |
44 | |
67 | |
45 | /* |
68 | /* |
46 | * Print an error message |
69 | * Print an error message |
47 | */ |
70 | */ |
48 | /* EXTPROTO */ |
71 | /* EXTPROTO */ |
49 | void |
72 | void |
|
|
73 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
|
|
74 | { |
|
|
75 | char msg[1024]; |
|
|
76 | |
|
|
77 | vsnprintf (msg, sizeof msg, fmt, arg_ptr); |
|
|
78 | |
|
|
79 | if (GET_R && GET_R->log_hook) |
|
|
80 | (*GET_R->log_hook) (msg); |
|
|
81 | else |
|
|
82 | write (STDOUT_FILENO, msg, strlen (msg)); |
|
|
83 | } |
|
|
84 | |
|
|
85 | /* EXTPROTO */ |
|
|
86 | void |
50 | rxvt_print_error (const char *fmt,...) |
87 | rxvt_log (const char *fmt,...) |
51 | { |
88 | { |
52 | va_list arg_ptr; |
89 | va_list arg_ptr; |
53 | |
90 | |
54 | va_start (arg_ptr, fmt); |
91 | va_start (arg_ptr, fmt); |
55 | fprintf (stderr, APL_NAME ": "); |
92 | rxvt_vlog (fmt, arg_ptr); |
56 | vfprintf (stderr, fmt, arg_ptr); |
|
|
57 | fprintf (stderr, "\n"); |
|
|
58 | va_end (arg_ptr); |
93 | va_end (arg_ptr); |
|
|
94 | } |
|
|
95 | |
|
|
96 | /* |
|
|
97 | * Print an error message |
|
|
98 | */ |
|
|
99 | /* EXTPROTO */ |
|
|
100 | void |
|
|
101 | rxvt_warn (const char *fmt,...) |
|
|
102 | { |
|
|
103 | va_list arg_ptr; |
|
|
104 | |
|
|
105 | rxvt_log ("%s: ", RESNAME); |
|
|
106 | |
|
|
107 | va_start (arg_ptr, fmt); |
|
|
108 | rxvt_vlog (fmt, arg_ptr); |
|
|
109 | va_end (arg_ptr); |
|
|
110 | } |
|
|
111 | |
|
|
112 | /* EXTPROTO */ |
|
|
113 | void |
|
|
114 | rxvt_fatal (const char *fmt,...) |
|
|
115 | { |
|
|
116 | va_list arg_ptr; |
|
|
117 | |
|
|
118 | rxvt_log ("%s: ", RESNAME); |
|
|
119 | |
|
|
120 | va_start (arg_ptr, fmt); |
|
|
121 | rxvt_vlog (fmt, arg_ptr); |
|
|
122 | va_end (arg_ptr); |
|
|
123 | |
|
|
124 | rxvt_exit_failure (); |
|
|
125 | } |
|
|
126 | |
|
|
127 | class rxvt_failure_exception rxvt_failure_exception; |
|
|
128 | |
|
|
129 | /* EXTPROTO */ |
|
|
130 | void |
|
|
131 | rxvt_exit_failure () |
|
|
132 | { |
|
|
133 | throw (rxvt_failure_exception); |
59 | } |
134 | } |
60 | |
135 | |
61 | /* |
136 | /* |
62 | * check that the first characters of S1 match S2 |
137 | * check that the first characters of S1 match S2 |
63 | * |
138 | * |
… | |
… | |
68 | */ |
143 | */ |
69 | /* EXTPROTO */ |
144 | /* EXTPROTO */ |
70 | int |
145 | int |
71 | rxvt_Str_match (const char *s1, const char *s2) |
146 | rxvt_Str_match (const char *s1, const char *s2) |
72 | { |
147 | { |
73 | int n = STRLEN (s2); |
148 | int n = STRLEN (s2); |
74 | |
149 | |
75 | return ((STRNCMP (s1, s2, n) == 0) ? n : 0); |
150 | return ((STRNCMP (s1, s2, n) == 0) ? n : 0); |
76 | } |
151 | } |
77 | |
152 | |
78 | /* EXTPROTO */ |
153 | /* EXTPROTO */ |
79 | const char * |
154 | const char * |
80 | rxvt_Str_skip_space (const char *str) |
155 | rxvt_Str_skip_space (const char *str) |
81 | { |
156 | { |
82 | if (str) |
157 | if (str) |
83 | while (*str && isspace (*str)) |
158 | while (*str && isspace (*str)) |
84 | str++; |
159 | str++; |
|
|
160 | |
85 | return str; |
161 | return str; |
86 | } |
162 | } |
87 | |
163 | |
88 | /* |
164 | /* |
89 | * remove leading/trailing space and strip-off leading/trailing quotes. |
165 | * remove leading/trailing space and strip-off leading/trailing quotes. |
… | |
… | |
91 | */ |
167 | */ |
92 | /* EXTPROTO */ |
168 | /* EXTPROTO */ |
93 | char * |
169 | char * |
94 | rxvt_Str_trim (char *str) |
170 | rxvt_Str_trim (char *str) |
95 | { |
171 | { |
96 | char *r, *s; |
172 | char *r, *s; |
97 | int n; |
173 | int n; |
98 | |
174 | |
99 | if (!str || !*str) /* shortcut */ |
175 | if (!str || !*str) /* shortcut */ |
100 | return str; |
176 | return str; |
101 | |
177 | |
102 | /* skip leading spaces */ |
178 | /* skip leading spaces */ |
… | |
… | |
113 | if (*s == '"' && *r == '"' && n > 1) |
189 | if (*s == '"' && *r == '"' && n > 1) |
114 | { |
190 | { |
115 | s++; |
191 | s++; |
116 | n -= 2; |
192 | n -= 2; |
117 | } |
193 | } |
|
|
194 | |
118 | /* copy back over: forwards copy */ |
195 | /* copy back over: forwards copy */ |
119 | for (r = str; n; n--) |
196 | for (r = str; n; n--) |
120 | *r++ = *s++; |
197 | *r++ = *s++; |
121 | *r = '\0'; |
198 | *r = '\0'; |
122 | |
199 | |