File: misc.c

package info (click to toggle)
eterm 0.7-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,024 kB
  • ctags: 1,205
  • sloc: ansic: 14,536; sh: 308; makefile: 294
file content (337 lines) | stat: -rw-r--r-- 7,377 bytes parent folder | download
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
/*--------------------------------*-C-*---------------------------------*
 * File:	misc.c
 *
 * miscellaneous service routines
 *
 * Copyright 1996,97
 * mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston
 *
 * You can do what you like with this source code provided you don't make
 * money from it and you include an unaltered copy of this message
 * (including the copyright).  As usual, the author accepts no
 * responsibility for anything, nor does he guarantee anything whatsoever.
 *----------------------------------------------------------------------*/
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

#include "main.h"
#include "misc.h"
#include "debug.h"
#include "mem.h"
#include "options.h"

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

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

/*
 * Print an error message
 */
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)
 */
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
 */
char           *
Str_skip_space(char *str)
{
    if (str && *str) {
	while (*str && isspace(*str))
	    str++;
    }
    return str;
}

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
 */
int
Str_escaped(char *str) {

    register char  *p = str;
    int             i = 0, len, n, append = 0;

    dprintf(("Str_escaped(\"%s\")\n", str));

/* 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';

    dprintf(("  New value is:  \"%s\"\n", str));

    return len;
}

const char *
find_file(const char *file, const char *ext) {

  const char *f;

#ifdef PATH_ENV
  if ((f = search_path(getenv(PATH_ENV), file, ext)) != NULL)
    return (f);
  else
#endif
    return (search_path(getenv("PATH"), file, ext));
}

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

/*
 * draw bottomShadow/highlight along top/left sides of the window
 */
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
 */
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);
    }
}

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 */
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) -----------------------*/