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