File: misc.c

package info (click to toggle)
rxvt 1%3A2.4.5-16
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,128 kB
  • ctags: 1,296
  • sloc: ansic: 12,852; makefile: 521; sh: 411
file content (435 lines) | stat: -rw-r--r-- 9,873 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*--------------------------------*-C-*---------------------------------*
 * File:	misc.c
 *----------------------------------------------------------------------*
 * Copyright (C) 1997 1998 Oezguer Kesim <kesim@math.fu-berlin.de>
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *----------------------------------------------------------------------*/

#include "rxvt.h"		/* NECESSARY */

/*----------------------------------------------------------------------*/
/* PROTO */
const char     *
my_basename(const char *str)
{
    const char     *base = strrchr(str, '/');

    return (base ? base + 1 : str);
}

/*
 * Print an error message
 */
/* PROTO */
void
print_error(const char *fmt,...)
{
    va_list         arg_ptr;

    va_start(arg_ptr, fmt);
    fprintf(stderr, APL_NAME ": ");
    vfprintf(stderr, fmt, arg_ptr);
    fprintf(stderr, "\n");
    va_end(arg_ptr);
}

/*
 * check that the first characters of S1 match S2
 *
 * No Match
 *      return: 0
 * Match
 *      return: strlen (N)
 */
/* PROTO */
int
Str_match(const char *s1, const char *s2)
{
    int             n = 0;

    if (s1 == NULL || s2 == NULL)
	return 0;

    while (*s2) {
	if (*s1++ != *s2++)
	    return 0;
	n++;
    }

    return n;
}

/*
 * remove leading/trailing space and strip-off leading/trailing quotes
 * Do it in-place
 */
/* PROTO */
char           *
Str_skip_space(char *str)
{
    if (str && *str) {
	while (*str && isspace(*str))
	    str++;
    }
    return str;
}

/* PROTO */
char           *
Str_trim(char *str)
{
    if (str && *str) {
	char           *src;
	int             n;

	src = Str_skip_space(str);
	n = strlen(src) - 1;

	while (n > 0 && isspace(src[n]))
	    n--;
	src[n + 1] = '\0';

    /* strip leading/trailing quotes */
	if (src[0] == '"') {
	    src++;
	    n--;
	    if (src[n] == '"')
		src[n--] = '\0';
	}
	if (n < 0)
	    *str = '\0';
	else if (src != str) {
	    char           *dst = str;

	/* copy back in-place */
	    do {
		*dst++ = *src;
	    }
	    while (*src++ != '\0');
	}
    }
    return str;
}

/*
 * in-place interpretation of string:
 *
 *      backslash-escaped:      "\a\b\E\e\n\r\t", "\octal"
 *      Ctrl chars:     ^@ .. ^_, ^?
 *
 *      Emacs-style:    "M-" prefix
 *
 * Also,
 *      "M-x" prefixed strings, append "\r" if needed
 *      "\E]" prefixed strings (XTerm escape sequence) append "\a" if needed
 *
 * returns the converted string length
 */
/* PROTO */
int
Str_escaped(char *str)
{
    register char  *p = str;
    int             i = 0, len, n, append = 0;

/* use 'i' to increment through destination and p through source */

    if (str == NULL || (len = strlen(str)) == 0)
	return 0;

/* Emacs convenience, replace leading `M-..' with `\E..' */
    if ((n = Str_match(p, "M-")) != 0) {
	str[i++] = '\033';	/* destination */
	len--;
	p += n;
	if (toupper(*p) == 'X') {
	/* append carriage-return for `M-xcommand' */
	    append = '\r';
	    str[i++] = 'x';	/* destination */
	    p++;
	    while (isspace(*p)) {
		p++;
		len--;
	    }
	}
    }
    for ( /*nil */ ; i < len; i++) {
	register char   ch = *p++;

	if (ch == '\\') {
	    ch = *p;
	    if (ch >= '0' && ch <= '7') {	/* octal */
		int             j, num = 0;

		for (j = 0; j < 3 && (ch >= '0' && ch <= '7'); j++) {
		    num = num * 010 + (ch - '0');
		    p++;
		    len--;
		    ch = *p;
		}
		ch = (unsigned char)num;
	    } else {
		p++;
		len--;
		switch (ch) {
		case 'a':
		    ch = 007;
		    break;	/* bell */
		case 'b':
		    ch = '\b';
		    break;	/* backspace */
		case 'E':
		case 'e':
		    ch = 033;
		    break;	/* escape */
		case 'n':
		    ch = '\n';
		    break;	/* newline */
		case 'r':
		    ch = '\r';
		    break;	/* carriage-return */
		case 't':
		    ch = '\t';
		    break;	/* tab */
		}
	    }
	} else if (ch == '^') {
	    ch = *p;
	    p++;
	    len--;
	    ch = toupper(ch);
	    ch = (ch == '?' ? 127 : (ch - '@'));
	}
	str[i] = ch;
    }

/* ESC] is an XTerm escape sequence, must be ^G terminated */
    if (str[0] == '\0' && str[1] == '\033' && str[2] == ']')
	append = 007;

/* add trailing character as required */
    if (append && str[len - 1] != append)
	str[len++] = append;

    str[len] = '\0';

    return len;
}

/*----------------------------------------------------------------------*
 * file searching
 */

/* #define DEBUG_SEARCH_PATH */

#if defined (XPM_BACKGROUND) || (MENUBAR_MAX)
/*
 * search for FILE in the current working directory, and within the
 * colon-delimited PATHLIST, adding the file extension EXT if required.
 *
 * FILE is either semi-colon or zero terminated
 */
/* PROTO */
const char     *
File_search_path(const char *pathlist, const char *file, const char *ext)
{
    static char     name[256];
    int             maxpath, len;
    char           *p;
    char           *path = (char *)pathlist;	/* remove const */

    if (!access(file, R_OK))
	return file;

/* semi-colon delimited */
    if ((p = strchr(file, ';')) == NULL)
	p = strchr(file, '\0');
    len = (p - file);

#ifdef DEBUG_SEARCH_PATH
    getcwd(name, sizeof(name));
    fprintf(stderr, "pwd: \"%s\"\n", name);
    fprintf(stderr, "find: \"%.*s\"\n", len, file);
#endif

/* leave room for an extra '/' and trailing '\0' */
    maxpath = sizeof(name) - (len + (ext ? strlen(ext) : 0) + 2);
    if (maxpath <= 0)
	return NULL;

/* check if we can find it now */
    strncpy(name, file, len);
    name[len] = '\0';

    if (!access(name, R_OK))
	return name;
    if (ext) {
	strcat(name, ext);
	if (!access(name, R_OK))
	    return name;
    }
    for ( /*nil */ ; path != NULL && *path != '\0'; path = p) {
	int             n;

    /* colon delimited */
	if ((p = strchr(path, ':')) == NULL)
	    p = strchr(path, '\0');

	n = (p - path);
	if (*p != '\0')
	    p++;

	if (n > 0 && n <= maxpath) {
	    strncpy(name, path, n);
	    if (name[n - 1] != '/')
		name[n++] = '/';
	    name[n] = '\0';
	    strncat(name, file, len);

	    if (!access(name, R_OK))
		return name;
	    if (ext) {
		strcat(name, ext);
		if (!access(name, R_OK))
		    return name;
	    }
	}
    }
    return NULL;
}

/* PROTO */
const char     *
File_find(const char *file, const char *ext)
{
    const char     *f;

    if (file == NULL || *file == '\0')
	return NULL;

/* search environment variables here too */
    if ((f = File_search_path(rs_path, file, ext)) == NULL)
#ifdef PATH_ENV
	if ((f = File_search_path(getenv(PATH_ENV), file, ext)) == NULL)
#endif
	    f = File_search_path(getenv("PATH"), file, ext);

#ifdef DEBUG_SEARCH_PATH
    if (f)
	fprintf(stderr, "found: \"%s\"\n", f);
#endif

    return f;
}
#endif				/* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */

/*----------------------------------------------------------------------*
 * miscellaneous drawing routines
 */

/*
 * draw bottomShadow/highlight along top/left sides of the window
 */
/* PROTO */
void
Draw_tl(Window win, GC gc, int x, int y, int w, int h)
{
    int             shadow = SHADOW;

    if (w == 0 || h == 0)
	shadow = 1;

    w += (x - 1);
    h += (y - 1);

    for ( /*nil */ ; shadow > 0; shadow--, x++, y++, w--, h--) {
	XDrawLine(Xdisplay, win, gc, x, y, w, y);
	XDrawLine(Xdisplay, win, gc, x, y, x, h);
    }
}

/*
 * draw bottomShadow/highlight along the bottom/right sides of the window
 */
/* PROTO */
void
Draw_br(Window win, GC gc, int x, int y, int w, int h)
{
    int             shadow = SHADOW;

    if (w == 0 || h == 0)
	shadow = 1;

    w += (x - 1);
    h += (y - 1);

    x++;
    y++;
    for ( /*nil */ ; shadow > 0; shadow--, x++, y++, w--, h--) {
	XDrawLine(Xdisplay, win, gc, w, h, w, y);
	XDrawLine(Xdisplay, win, gc, w, h, x, h);
    }
}

/* PROTO */
void
Draw_Shadow(Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
{
    Draw_tl(win, topShadow, x, y, w, h);
    Draw_br(win, botShadow, x, y, w, h);
}

/* button shapes */
/* PROTO */
void
Draw_Triangle(Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
{
    switch (type) {
    case 'r':			/* right triangle */
	XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w);
	XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y + w / 2);
	XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w / 2);
	break;

    case 'l':			/* right triangle */
	XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y);
	XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w / 2);
	XDrawLine(Xdisplay, win, topShadow, x, y + w / 2, x + w, y);
	break;

    case 'd':			/* down triangle */
	XDrawLine(Xdisplay, win, topShadow, x, y, x + w / 2, y + w);
	XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y);
	XDrawLine(Xdisplay, win, botShadow, x + w, y, x + w / 2, y + w);
	break;

    case 'u':			/* up triangle */
	XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w / 2, y);
	XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w);
	XDrawLine(Xdisplay, win, topShadow, x, y + w, x + w / 2, y);
	break;
#if 0
    case 's':			/* square */
	XDrawLine(Xdisplay, win, topShadow, x + w, y, x, y);
	XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w);
	XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w);
	XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y);
	break;
#endif
    }
}
/*----------------------- end-of-file (C source) -----------------------*/