list.c revision 3e747e6d
1/*****************************************************************************/
2/*
3
4Copyright 1989, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
28/**                          Salt Lake City, Utah                           **/
29/**                        Cambridge, Massachusetts                         **/
30/**                                                                         **/
31/**                           All Rights Reserved                           **/
32/**                                                                         **/
33/**    Permission to use, copy, modify, and distribute this software and    **/
34/**    its documentation  for  any  purpose  and  without  fee is hereby    **/
35/**    granted, provided that the above copyright notice appear  in  all    **/
36/**    copies and that both  that  copyright  notice  and  this  permis-    **/
37/**    sion  notice appear in supporting  documentation,  and  that  the    **/
38/**    name of Evans & Sutherland not be used in advertising    **/
39/**    in publicity pertaining to distribution of the  software  without    **/
40/**    specific, written prior permission.                                  **/
41/**                                                                         **/
42/**    EVANS & SUTHERLAND DISCLAIMs ALL WARRANTIES WITH REGARD    **/
43/**    TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES  OF  MERCHANT-    **/
44/**    ABILITY  AND  FITNESS,  IN  NO  EVENT SHALL EVANS & SUTHERLAND    **/
45/**    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL  DAM-    **/
46/**    AGES OR  ANY DAMAGES WHATSOEVER  RESULTING FROM LOSS OF USE, DATA    **/
47/**    OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER    **/
48/**    TORTIOUS ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE    **/
49/**    OR PERFORMANCE OF THIS SOFTWARE.                                     **/
50/*****************************************************************************/
51/* $XFree86: xc/programs/twm/list.c,v 1.8 2002/09/24 21:00:27 tsi Exp $ */
52
53
54/**********************************************************************
55 *
56 * $Xorg: list.c,v 1.4 2001/02/09 02:05:36 xorgcvs Exp $
57 *
58 * TWM code to deal with the name lists for the NoTitle list and
59 * the AutoRaise list
60 *
61 * 11-Apr-88 Tom LaStrange        Initial Version.
62 *
63 **********************************************************************/
64
65#include <stdio.h>
66#include "twm.h"
67#include "screen.h"
68#include "gram.h"
69#include "util.h"
70
71struct name_list_struct
72{
73    name_list *next;    /**< pointer to the next name */
74    char *name;         /**< the name of the window */
75    char *ptr;          /**< list dependent data */
76};
77
78/**
79 * add a window name to the appropriate list.
80 *
81 *	If the list does not use the ptr value, a non-null value
82 *	should be placed in it.  LookInList returns this ptr value
83 *	and procedures calling LookInList will check for a non-null
84 *	return value as an indication of success.
85 *
86 *  \param list  the address of the pointer to the head of a list
87 *  \param name  a pointer to the name of the window
88 *  \param ptr   pointer to list dependent data
89 */
90void
91AddToList(name_list **list_head, char *name, char *ptr)
92{
93    name_list *nptr;
94
95    if (!list_head) return;	/* ignore empty inserts */
96
97    nptr = (name_list *)malloc(sizeof(name_list));
98    if (nptr == NULL)
99    {
100	twmrc_error_prefix();
101	fprintf (stderr, "unable to allocate %ld bytes for name_list\n",
102		 (unsigned long)sizeof(name_list));
103	Done(NULL, NULL);
104    }
105
106    nptr->next = *list_head;
107    nptr->name = name;
108    nptr->ptr = (ptr == NULL) ? (char *)TRUE : ptr;
109    *list_head = nptr;
110}
111
112/**
113 * look through a list for a window name, or class
114 *
115 *  \return the ptr field of the list structure or NULL if the name
116 *	or class was not found in the list
117 *
118 *	\param list   a pointer to the head of a list
119 *	\param name   a pointer to the name to look for
120 *  \param class  a pointer to the class to look for
121 */
122char *
123LookInList(name_list *list_head, char *name, XClassHint *class)
124{
125    name_list *nptr;
126
127    /* look for the name first */
128    for (nptr = list_head; nptr != NULL; nptr = nptr->next)
129	if (strcmp(name, nptr->name) == 0)
130	    return (nptr->ptr);
131
132    if (class)
133    {
134	/* look for the res_name next */
135	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
136	    if (strcmp(class->res_name, nptr->name) == 0)
137		return (nptr->ptr);
138
139	/* finally look for the res_class */
140	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
141	    if (strcmp(class->res_class, nptr->name) == 0)
142		return (nptr->ptr);
143    }
144    return (NULL);
145}
146
147char *
148LookInNameList(name_list *list_head, char *name)
149{
150    return (LookInList(list_head, name, NULL));
151}
152
153/**
154 * look through a list for a window name, or class
155 *
156 *  \return TRUE  if the name was found
157 *  \return FALSE if the name was not found
158 *
159 *  \param      list  a pointer to the head of a list
160 *  \param      name  a pointer to the name to look for
161 *  \param      class a pointer to the class to look for
162 *	\param[out] ptr   fill in the list value if the name was found
163 */
164int GetColorFromList(name_list *list_head, char *name, XClassHint *class,
165                     Pixel *ptr)
166{
167    int save;
168    name_list *nptr;
169
170    for (nptr = list_head; nptr != NULL; nptr = nptr->next)
171	if (strcmp(name, nptr->name) == 0)
172	{
173	    save = Scr->FirstTime;
174	    Scr->FirstTime = TRUE;
175	    GetColor(Scr->Monochrome, ptr, nptr->ptr);
176	    Scr->FirstTime = save;
177	    return (TRUE);
178	}
179
180    if (class)
181    {
182	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
183	    if (strcmp(class->res_name, nptr->name) == 0)
184	    {
185		save = Scr->FirstTime;
186		Scr->FirstTime = TRUE;
187		GetColor(Scr->Monochrome, ptr, nptr->ptr);
188		Scr->FirstTime = save;
189		return (TRUE);
190	    }
191
192	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
193	    if (strcmp(class->res_class, nptr->name) == 0)
194	    {
195		save = Scr->FirstTime;
196		Scr->FirstTime = TRUE;
197		GetColor(Scr->Monochrome, ptr, nptr->ptr);
198		Scr->FirstTime = save;
199		return (TRUE);
200	    }
201    }
202    return (FALSE);
203}
204
205/**
206 * free up a list
207 */
208void FreeList(name_list **list)
209{
210    name_list *nptr;
211    name_list *tmp;
212
213    for (nptr = *list; nptr != NULL; )
214    {
215	tmp = nptr->next;
216	free((char *) nptr);
217	nptr = tmp;
218    }
219    *list = NULL;
220}
221