~ctwm/ctwm/trunk

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
436
437
438
439
440
441
442
443
444
445
/*
 *       Copyright 1988 by Evans & Sutherland Computer Corporation,
 *                          Salt Lake City, Utah
 *  Portions Copyright 1989 by the Massachusetts Institute of Technology
 *                        Cambridge, Massachusetts
 *
 * Copyright 1992 Claude Lecommandeur.
 */

/**********************************************************************
 *
 * $XConsortium: list.c,v 1.20 91/01/09 17:13:30 rws Exp $
 *
 * TWM code to deal with the name lists for the NoTitle list and
 * the AutoRaise list
 *
 * 11-Apr-88 Tom LaStrange        Initial Version.
 *
 * Do the necessary modification to be integrated in ctwm.
 * Can no longer be used for the standard twm.
 *
 * 22-April-92 Claude Lecommandeur.
 *
 *
 **********************************************************************/

#include "ctwm.h"

#include <stdio.h>
#include <stdlib.h>

#include "ctwm_shutdown.h"
#include "screen.h"
#include "list.h"
#include "util.h"
#include "parse.h"

#ifdef USE_SYS_REGEX
# include <regex.h>
#endif /* USE_SYS_REGEX */



/***********************************************************************
 *
 *  Procedure:
 *      AddToList - add a window name to the appropriate list
 *
 *  Inputs:
 *      list    - the address of the pointer to the head of a list
 *      name    - a pointer to the name of the window
 *      ptr     - pointer to list dependent data
 *
 *  Special Considerations
 *      If the list does not use the ptr value, a non-null value
 *      should be placed in it.  LookInList returns this ptr value
 *      and procedures calling LookInList will check for a non-null
 *      return value as an indication of success.
 *
 ***********************************************************************
 */

void AddToList(name_list **list_head, const char *name, void *ptr)
{
	name_list *nptr;

	if(!list_head) {
		return;        /* ignore empty inserts */
	}

	nptr = malloc(sizeof(name_list));
	if(nptr == NULL) {
		fprintf(stderr, "unable to allocate %lu bytes for name_list\n",
		        (unsigned long) sizeof(name_list));
		DoShutdown();
	}

	nptr->next = *list_head;
	nptr->name = strdup(name);
	nptr->ptr = (ptr == NULL) ? (char *)1 : ptr;
	*list_head = nptr;
}

/***********************************************************************
 *
 *  Procedure:
 *      LookInList - look through a list for a window name, or class
 *
 *  Returned Value:
 *      the ptr field of the list structure or NULL if the name
 *      or class was not found in the list
 *
 *  Inputs:
 *      list    - a pointer to the head of a list
 *      name    - a pointer to the name to look for
 *      class   - a pointer to the class to look for
 *
 ***********************************************************************
 */

void *LookInList(name_list *list_head, const char *name, XClassHint *class)
{
	name_list *nptr;

	/* look for the name first */
	for(nptr = list_head; nptr != NULL; nptr = nptr->next) {
		if(match(nptr->name, name)) {
			return (nptr->ptr);
		}
	}

	if(class) {
		/* look for the res_name next */
		for(nptr = list_head; nptr != NULL; nptr = nptr->next) {
			if(match(nptr->name, class->res_name)) {
				return (nptr->ptr);
			}
		}

		/* finally look for the res_class */
		for(nptr = list_head; nptr != NULL; nptr = nptr->next) {
			if(match(nptr->name, class->res_class)) {
				return (nptr->ptr);
			}
		}
	}
	return (NULL);
}

void *LookInNameList(name_list *list_head, const char *name)
{
	return (LookInList(list_head, name, NULL));
}

void *LookInListWin(name_list *list_head, TwmWindow *twin)
{
	return LookInList(list_head, twin->name, &(twin->class));
}

bool IsInList(name_list *list_head, TwmWindow *twin)
{
	return (bool)LookInList(list_head, twin->name, &(twin->class));
}

void *LookPatternInList(name_list *list_head, const char *name,
                        XClassHint *class)
{
	name_list *nptr;

	for(nptr = list_head; nptr != NULL; nptr = nptr->next)
		if(match(nptr->name, name)) {
			return (nptr->name);
		}

	if(class) {
		for(nptr = list_head; nptr != NULL; nptr = nptr->next)
			if(match(nptr->name, class->res_name)) {
				return (nptr->name);
			}

		for(nptr = list_head; nptr != NULL; nptr = nptr->next)
			if(match(nptr->name, class->res_class)) {
				return (nptr->name);
			}
	}
	return (NULL);
}

void *LookPatternInNameList(name_list *list_head, const char *name)
{
	return (LookPatternInList(list_head, name, NULL));
}

/***********************************************************************
 *
 *  Procedure:
 *      GetColorFromList - look through a list for a window name, or class
 *
 *  Returned Value:
 *      true  if the name was found
 *      false if the name was not found
 *
 *  Inputs:
 *      list    - a pointer to the head of a list
 *      name    - a pointer to the name to look for
 *      class   - a pointer to the class to look for
 *
 *  Outputs:
 *      ptr     - fill in the list value if the name was found
 *
 ***********************************************************************
 */

bool GetColorFromList(name_list *list_head, char *name,
                      XClassHint *class, Pixel *ptr)
{
	bool save;
	name_list *nptr;

	for(nptr = list_head; nptr != NULL; nptr = nptr->next)
		if(match(nptr->name, name)) {
			save = Scr->FirstTime;
			Scr->FirstTime = true;
			GetColor(Scr->Monochrome, ptr, nptr->ptr);
			Scr->FirstTime = save;
			return true;
		}

	if(class) {
		for(nptr = list_head; nptr != NULL; nptr = nptr->next)
			if(match(nptr->name, class->res_name)) {
				save = Scr->FirstTime;
				Scr->FirstTime = true;
				GetColor(Scr->Monochrome, ptr, nptr->ptr);
				Scr->FirstTime = save;
				return true;
			}

		for(nptr = list_head; nptr != NULL; nptr = nptr->next)
			if(match(nptr->name, class->res_class)) {
				save = Scr->FirstTime;
				Scr->FirstTime = true;
				GetColor(Scr->Monochrome, ptr, nptr->ptr);
				Scr->FirstTime = save;
				return true;
			}
	}
	return false;
}

/***********************************************************************
 *
 *  Procedure:
 *      FreeList - free up a list
 *
 ***********************************************************************
 */

void FreeList(name_list **list)
{
	name_list *nptr;
	name_list *tmp;

	for(nptr = *list; nptr != NULL;) {
		tmp = nptr->next;
		free(nptr->name);
		free(nptr);
		nptr = tmp;
	}
	*list = NULL;
}

#ifdef USE_SYS_REGEX

bool match(const char *pattern, const char *string)
{
	regex_t preg;
	int error;

	if((pattern == NULL) || (string == NULL)) {
		return false;
	}
	error = regcomp(&preg, pattern, REG_EXTENDED | REG_NOSUB);
	if(error != 0) {
		char buf [256];
		regerror(error, &preg, buf, sizeof buf);
		fprintf(stderr, "%s : %s\n", buf, pattern);
		return false;
	}
	error = regexec(&preg, string, 5, 0, 0);
	regfree(&preg);
	if(error == 0) {
		return true;
	}
	return false;
}

#else



int regex_match(const char *p, const char *t);
int regex_match_after_star(const char *p, const char *t);

#if 0                           /* appears not to be used anywhere */
static int is_pattern(char *p)
{
	while(*p) {
		switch(*p++) {
			case '?':
			case '*':
			case '[':
				return TRUE;
			case '\\':
				if(!*p++) {
					return FALSE;
				}
		}
	}
	return FALSE;
}
#endif

#define ABORT 2

int regex_match(const char *p, const char *t)
{
	char range_start, range_end;
	int invert;
	int member_match;
	int loop;

	for(; *p; p++, t++) {
		if(!*t) {
			return (*p == '*' && *++p == '\0') ? TRUE : ABORT;
		}
		switch(*p) {
			case '?':
				break;
			case '*':
				return regex_match_after_star(p, t);
			case '[': {
				p++;
				invert = FALSE;
				if(*p == '!' || *p == '^') {
					invert = TRUE;
					p++;
				}
				if(*p == ']') {
					return ABORT;
				}
				member_match = FALSE;
				loop = TRUE;
				while(loop) {
					if(*p == ']') {
						loop = FALSE;
						continue;
					}
					if(*p == '\\') {
						range_start = range_end = *++p;
					}
					else {
						range_start = range_end = *p;
					}
					if(!range_start) {
						return ABORT;
					}
					if(*++p == '-') {
						range_end = *++p;
						if(range_end == '\0' || range_end == ']') {
							return ABORT;
						}
						if(range_end == '\\') {
							range_end = *++p;
						}
						p++;
					}
					if(range_start < range_end) {
						if(*t >= range_start && *t <= range_end) {
							member_match = TRUE;
							loop = FALSE;
						}
					}
					else {
						if(*t >= range_end && *t <= range_start) {
							member_match = TRUE;
							loop = FALSE;
						}
					}
				}
				if((invert && member_match) || !(invert || member_match)) {
					return (FALSE);
				}
				if(member_match) {
					while(*p != ']') {
						if(!*p) {
							return (ABORT);
						}
						if(*p == '\\') {
							p++;
						}
						p++;
					}
				}
				break;
			}
			case '\\':
				p++;

			default:
				if(*p != *t) {
					return (FALSE);
				}
		}
	}
	return (!*t);
}

int regex_match_after_star(const char *p, const char *t)
{
	int mat;
	int nextp;

	while((*p == '?') || (*p == '*')) {
		if(*p == '?') {
			if(!*t++) {
				return ABORT;
			}
		}
		p++;
	}
	if(!*p) {
		return TRUE;
	}

	nextp = *p;
	if(nextp == '\\') {
		nextp = p[1];
	}

	mat = FALSE;
	while(mat == FALSE) {
		if(nextp == *t || nextp == '[') {
			mat = regex_match(p, t);
		}
		if(!*t++) {
			mat = ABORT;
		}
	}
	return (mat);
}

int match(const char *p, const char *t)
{
	if((p == NULL) || (t == NULL)) {
		return (FALSE);
	}
	return ((regex_match(p, t) == TRUE) ? TRUE : FALSE);
}

#endif