Minimal changes to get it to compile (a few taken from David Given ack-6.0pre5)
[Ack-5.5.git] / util / ack / svars.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  */
6
7 #include "ack.h"
8
9 #ifndef NORCSID
10 static char rcs_id[] = "$Id: svars.c,v 2.3 1994/06/24 10:13:12 ceriel Exp $" ;
11 #endif
12
13 /*      The processing of string valued variables,
14         this is an almost self contained module.
15
16         Five externally visible routines:
17
18         setsvar(name,result)
19                 Associate the name with the result.
20
21                 name    a string pointer
22                 result  a string pointer
23
24         setpvar(name,routine)
25                 Associate the name with the routine.
26
27                 name    a string pointer
28                 routine a routine id
29
30            The parameters name and result are supposed to be pointing to
31            non-volatile string storage used only for this call.
32
33         char *getvar(name)
34                 returns the pointer to a string associated with name,
35                 the pointer is produced by returning result or the
36                 value returned by calling the routine.
37
38                 name    a string pointer
39
40         Other routines called
41
42         getcore(size)   Core allocation
43
44 */
45
46 extern  char    *getcore();
47
48 struct vars {
49         char                            *v_name;
50         enum { routine, string }        v_type;
51
52         union {
53                 char    *v_string;
54                 char    *(*v_routine)();
55         }                               v_value ;
56         struct vars                     *v_next ;
57 };
58
59 static struct vars *v_first ;
60
61 static struct vars *newvar(name) char *name; {
62         register struct vars *new ;
63
64         for ( new=v_first ; new ; new= new->v_next ) {
65                 if ( strcmp(name,new->v_name)==0 ) {
66                         throws(name) ;
67                         if ( new->v_type== string ) {
68                                 throws(new->v_value.v_string) ;
69                         }
70                         return new ;
71                 }
72         }
73         new= (struct vars *)getcore( (unsigned)sizeof (struct vars));
74         new->v_name= name ;
75         new->v_next= v_first ;
76         v_first= new ;
77         return new ;
78 }
79
80 setsvar(name,str) char *name, *str ; {
81         register struct vars *new ;
82
83         new= newvar(name);
84 #ifdef DEBUG
85         if ( debug>=2 ) vprint("%s=%s\n", name, str) ;
86 #endif
87         new->v_type= string;
88         new->v_value.v_string= str;
89 }
90
91 setpvar(name,rout) char *name, *(*rout)() ; {
92         register struct vars *new ;
93
94         new= newvar(name);
95 #ifdef DEBUG
96         if ( debug>=2 ) vprint("%s= (*%o)()\n",name,rout) ;
97 #endif
98         new->v_type= routine;
99         new->v_value.v_routine= rout;
100 }
101
102 char *getvar(name) char *name ; {
103         register struct vars *scan ;
104
105         for ( scan=v_first ; scan ; scan= scan->v_next ) {
106                 if ( strcmp(name,scan->v_name)==0 ) {
107                         switch ( scan->v_type ) {
108                         case string:
109                                 return scan->v_value.v_string ;
110                         case routine:
111                                 return (*scan->v_value.v_routine)() ;
112                         }
113                 }
114         }
115         return (char *)0 ;
116 }