convert all Imakefile LinuxDistribution to LinuxArchitecture.
[oweals/cde.git] / cde / programs / dtksh / define.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: define.c /main/4 1995/11/01 15:51:03 rswiston $ */
24 /*      Copyright (c) 1991, 1992 UNIX System Laboratories, Inc. */
25 /*      All Rights Reserved     */
26
27 /*      THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF    */
28 /*      UNIX System Laboratories, Inc.                  */
29 /*      The copyright notice above does not evidence any       */
30 /*      actual or intended publication of such source code.    */
31
32 #include "stdio.h"
33 #include "exksh.h" /* which includes sys/types.h */
34 #include <sys/param.h>
35 #include <string.h>
36 #include <search.h>
37 #include <ctype.h>
38 #include "misc.h"
39 #include "docall.h"
40 #include "basetbl.h"
41 #include "msgs.h"
42
43
44
45 static growdef( void ) ;
46 static int add_deflist( 
47                         struct symarray *defptr,
48                         char *prefix) ;
49 static def_init( void ) ;
50
51
52
53 static struct symarray *Dyndef = NULL;
54 static int Ndyndef = 0;
55 static int Sdyndef = 0;
56 static char defInited = 0;
57
58 static char use[] = "0x%x";
59 static char use2[] = "%s=0x%x";
60
61 struct deflist {
62         char *prefix;
63         int size;
64         struct symarray *defs;
65 };
66
67 struct deflist *Deflist = NULL;
68 int Ndeflist;
69
70
71 static
72 growdef( void )
73 {
74    int i;
75
76    if (!defInited)
77       def_init();
78
79    if (!(Dyndef = (struct symarray *) realloc(Dyndef, (Sdyndef + 20) * 
80                                               sizeof(struct symarray))))
81    {
82       return(SH_FAIL);
83    }
84    Deflist->defs = Dyndef;
85    memset(((char *) Dyndef) + Sdyndef * sizeof(struct symarray), '\0', 
86                20 * sizeof(struct symarray));
87    Sdyndef += 20;
88 }
89
90 int
91 do_define(
92         int argc,
93         char **argv )
94 {
95    int i, argstart, redo;
96    char *name;
97    struct symarray *found, dummy;
98
99    if (!defInited)
100       def_init();
101
102    if (argc > 1 && C_PAIR(argv[1], '-', 'R')) 
103    {
104       redo = 0;
105       argstart = 2;
106    }
107    else 
108    {
109       argstart = 1;
110       redo = 1;
111    }
112
113    if ((argstart +1) >= argc)
114       XK_USAGE(argv[0]);
115
116    name = argv[argstart++];
117    dummy.str = name;
118    found = (struct symarray *) bsearch((char *) &dummy, Dyndef, Ndyndef, 
119            sizeof(struct symarray), symcomp);
120
121    if (found) 
122    {
123        if (!redo)
124           return(SH_SUCC);
125        i = found - Dyndef;
126    }
127    else 
128    {
129       if (Sdyndef == Ndyndef)
130          growdef();
131       Ndyndef++;
132       if (Ndyndef > 1)
133          for (i = Ndyndef - 1; i > 0; i--) 
134          {
135             if (strcmp(name, Dyndef[i - 1].str) >= 0)
136                break;
137             Dyndef[i] = Dyndef[i - 1];
138          }
139       else
140          i = 0;
141       Dyndef[i].str = strdup(name);
142       Deflist->size++;
143    }
144    RIF(xk_par_int(argv + argstart, &Dyndef[i].addr, NULL));
145    return(SH_SUCC);
146 }
147
148 int
149 fdef(
150         char *str,
151         unsigned long *val )
152 {
153    struct symarray *found, dummy;
154    int i;
155
156    dummy.str = str;
157    if (!Deflist)
158       return(0);
159
160    for (i = 0; i < Ndeflist; i++) 
161    {
162       if (Deflist[i].defs) 
163       {
164          if (Deflist[i].size < 0)
165          {
166             found = (struct symarray *) lfind((char *) &dummy, Deflist[i].defs,
167                     (unsigned int *) &Deflist[i].size, sizeof(struct symarray),
168                      symcomp);
169          }
170          else
171          {
172             found = (struct symarray *) bsearch((char *) &dummy, 
173                     Deflist[i].defs, Deflist[i].size, sizeof(struct symarray), 
174                     symcomp);
175          }
176
177          if (found != NULL) 
178          {
179             *val = found->addr;
180             return(1);
181          }
182       }
183    }
184    return(0);
185 }
186
187 int
188 do_deflist(
189         int argc,
190         char **argv )
191 {
192    int i, j;
193    char *prefix = NULL;
194    struct symarray *defptr = NULL;
195    char * errmsg;
196
197    for (i = 1; (i < argc) && argv[i]; i++) 
198    {
199       if (argv[i][0] == '-') 
200       {
201          for (j = 1; argv[i][j]; j++) 
202          {
203             switch(argv[i][j]) 
204             {
205                case 'p': 
206                {
207                   if (argv[i][j + 1]) 
208                   {
209                      prefix = argv[i] + j;
210                      j += strlen(prefix) - 2;
211                   }
212                   else 
213                   {
214                      prefix = argv[++i];
215                      j = strlen(prefix) - 1;
216                   }
217                }
218             }
219          }
220       }
221       else 
222       {
223          if ((defptr = (struct symarray *) getaddr(argv[i])) == NULL) 
224          {
225             errmsg=strdup(GETMESSAGE(3,1, 
226                           "Unable to locate the definition list '%s'"));
227             printerrf(argv[0], errmsg, argv[i], NULL, NULL,
228                       NULL, NULL, NULL, NULL, NULL);
229             free(errmsg);
230             return(SH_FAIL);
231          }
232       }
233    }
234
235    if (defptr == NULL)
236    {
237       XK_USAGE(argv[0]);
238    }
239
240    for (i = 0; i < Ndeflist; i++)
241       if ((Deflist[i].defs == defptr) && 
242           (!prefix || (strcmp(Deflist[i].prefix, prefix) == 0)))
243       {
244          return(SH_SUCC);
245       }
246
247    return(add_deflist(defptr, prefix));
248 }
249
250 static int
251 add_deflist(
252         struct symarray *defptr,
253         char *prefix )
254 {
255    int i;
256
257    if (!Deflist)
258    {
259       Deflist = (struct deflist *) malloc((Ndeflist + 1) * 
260                                            sizeof(struct deflist));
261    }
262    else
263    {
264       Deflist = (struct deflist *) realloc(Deflist, (Ndeflist + 1) * 
265                                            sizeof(struct deflist));
266    }
267
268    if (!Deflist)
269       return(SH_FAIL);
270
271    Deflist[Ndeflist].defs = defptr;
272    Deflist[Ndeflist].prefix = strdup(prefix);
273    if (!defptr[0].str)
274       Deflist[Ndeflist].size = 0;
275    else 
276    {
277       for (i = 1; defptr[i].str && defptr[i].str[0]; i++)
278          if (symcomp((void *) (defptr + i), (void *) (defptr + i - 1)) < 0)
279             break;
280
281       if (!(defptr[i].str && defptr[i].str[0]))
282          Deflist[Ndeflist].size = i;
283       else
284          Deflist[Ndeflist].size = -1;
285    }
286    Ndeflist++;
287    return(SH_SUCC);
288 }
289
290 int
291 do_finddef(
292         int argc,
293         char **argv )
294 {
295    unsigned long found;
296    struct symarray dummy;
297    char * errmsg;
298
299    if (argc < 2) 
300       XK_USAGE(argv[0]);
301
302    if (fdef(argv[1], &found)) 
303    {
304       if (argc >= 3) 
305       {
306          char buf[50];
307
308          sprintf(buf, use2, argv[2], found);
309          env_set(buf);
310       }
311       else 
312       {
313          sprintf(xk_ret_buffer, use, found);
314          xk_ret_buf = xk_ret_buffer;
315       }
316       return(SH_SUCC);
317    }
318    errmsg = strdup(GETMESSAGE(3, 2, "Unable to locate the define '%s'"));
319    printerrf(argv[0], errmsg, argv[1], NULL, NULL, NULL,
320              NULL, NULL, NULL, NULL);
321    free(errmsg);
322    return(SH_FAIL);
323 }
324
325 static
326 def_init( void )
327 {
328    char * errhdr;
329    char * errmsg;
330
331    defInited = 1;
332    if (!(Dyndef = (struct symarray *) malloc(20 * sizeof(struct symarray)))) 
333    {
334       errhdr = strdup(GetSharedMsg(DT_ERROR));
335       errmsg =  strdup(GetSharedMsg(DT_ALLOC_FAILURE));
336       printerr(errhdr, errmsg, NULL);
337       free(errhdr);
338       free(errmsg);
339       exit(1);
340    }
341    Dyndef[0].str = NULL;
342    Sdyndef = 20;
343    Ndyndef = 0;
344    add_deflist(Dyndef, "dynamic");
345    add_deflist(basedefs, "base");
346 }