list.c revision c2535118
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
52
53/**********************************************************************
54 *
55 * TWM code to deal with the name lists for the NoTitle list and
56 * the AutoRaise list
57 *
58 * 11-Apr-88 Tom LaStrange        Initial Version.
59 *
60 **********************************************************************/
61
62#include <stdio.h>
63#include "twm.h"
64#include "screen.h"
65#include "gram.h"
66#include "util.h"
67
68struct name_list_struct
69{
70    name_list *next;    /**< pointer to the next name */
71    char *name;         /**< the name of the window */
72    char *ptr;          /**< list dependent data */
73};
74
75/**
76 * add a window name to the appropriate list.
77 *
78 *	If the list does not use the ptr value, a non-null value
79 *	should be placed in it.  LookInList returns this ptr value
80 *	and procedures calling LookInList will check for a non-null
81 *	return value as an indication of success.
82 *
83 *  \param list  the address of the pointer to the head of a list
84 *  \param name  a pointer to the name of the window
85 *  \param ptr   pointer to list dependent data
86 */
87void
88AddToList(name_list **list_head, char *name, char *ptr)
89{
90    name_list *nptr;
91
92    if (!list_head) return;	/* ignore empty inserts */
93
94    nptr = malloc(sizeof(name_list));
95    if (nptr == NULL)
96    {
97	twmrc_error_prefix();
98	fprintf (stderr, "unable to allocate %ld bytes for name_list\n",
99		 (unsigned long)sizeof(name_list));
100	Done(NULL, NULL);
101    }
102
103    nptr->next = *list_head;
104    nptr->name = name;
105    nptr->ptr = (ptr == NULL) ? (char *)TRUE : ptr;
106    *list_head = nptr;
107}
108
109/**
110 * look through a list for a window name, or class
111 *
112 *  \return the ptr field of the list structure or NULL if the name
113 *	or class was not found in the list
114 *
115 *	\param list   a pointer to the head of a list
116 *	\param name   a pointer to the name to look for
117 *  \param class  a pointer to the class to look for
118 */
119char *
120LookInList(name_list *list_head, char *name, XClassHint *class)
121{
122    name_list *nptr;
123
124    /* look for the name first */
125    for (nptr = list_head; nptr != NULL; nptr = nptr->next)
126	if (strcmp(name, nptr->name) == 0)
127	    return (nptr->ptr);
128
129    if (class)
130    {
131	/* look for the res_name next */
132	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
133	    if (strcmp(class->res_name, nptr->name) == 0)
134		return (nptr->ptr);
135
136	/* finally look for the res_class */
137	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
138	    if (strcmp(class->res_class, nptr->name) == 0)
139		return (nptr->ptr);
140    }
141    return (NULL);
142}
143
144char *
145LookInNameList(name_list *list_head, char *name)
146{
147    return (LookInList(list_head, name, NULL));
148}
149
150/**
151 * look through a list for a window name, or class
152 *
153 *  \return TRUE  if the name was found
154 *  \return FALSE if the name was not found
155 *
156 *  \param      list  a pointer to the head of a list
157 *  \param      name  a pointer to the name to look for
158 *  \param      class a pointer to the class to look for
159 *	\param[out] ptr   fill in the list value if the name was found
160 */
161int GetColorFromList(name_list *list_head, char *name, XClassHint *class,
162                     Pixel *ptr)
163{
164    int save;
165    name_list *nptr;
166
167    for (nptr = list_head; nptr != NULL; nptr = nptr->next)
168	if (strcmp(name, nptr->name) == 0)
169	{
170	    save = Scr->FirstTime;
171	    Scr->FirstTime = TRUE;
172	    GetColor(Scr->Monochrome, ptr, nptr->ptr);
173	    Scr->FirstTime = save;
174	    return (TRUE);
175	}
176
177    if (class)
178    {
179	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
180	    if (strcmp(class->res_name, nptr->name) == 0)
181	    {
182		save = Scr->FirstTime;
183		Scr->FirstTime = TRUE;
184		GetColor(Scr->Monochrome, ptr, nptr->ptr);
185		Scr->FirstTime = save;
186		return (TRUE);
187	    }
188
189	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
190	    if (strcmp(class->res_class, nptr->name) == 0)
191	    {
192		save = Scr->FirstTime;
193		Scr->FirstTime = TRUE;
194		GetColor(Scr->Monochrome, ptr, nptr->ptr);
195		Scr->FirstTime = save;
196		return (TRUE);
197	    }
198    }
199    return (FALSE);
200}
201
202/**
203 * free up a list
204 */
205void FreeList(name_list **list)
206{
207    name_list *nptr;
208    name_list *tmp;
209
210    for (nptr = *list; nptr != NULL; )
211    {
212	tmp = nptr->next;
213	free(nptr);
214	nptr = tmp;
215    }
216    *list = NULL;
217}
218