]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/variables.c
r4264@bucket (orig r254): kcr | 2008-01-20 22:11:44 -0500
[1ts-debian.git] / zephyr / zwgc / variables.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It is one of the source files comprising zwgc, the Zephyr WindowGram
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #include <sysdep.h>
15
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_variables_c[] = "$Id$";
18 #endif
19
20 #include <zephyr/mit-copyright.h>
21
22 /****************************************************************************/
23 /*                                                                          */
24 /*   Module containing code to deal with description langauge variables:    */
25 /*                                                                          */
26 /****************************************************************************/
27
28 #include "new_memory.h"
29 #include "notice.h"
30 #include "string_dictionary_aux.h"
31 #include "variables.h"
32
33 /*
34  * fields_data[_length] - these point to the field data that the number
35  *                        variables were last set to using 
36  *                        var_set_number_variables_to_fields.
37  */
38
39 static char *fields_data;
40 static int fields_data_length = 0;
41
42 /*
43  * [non_]number_variable_dict - contains the values of all the [non-]number
44  *                              variables that have been set since the last
45  *                              var_clear_all_variables call or (for numbers
46  *                              only) var_set_number_variables_to_fields call.
47  */
48
49 static string_dictionary non_number_variable_dict = NULL;
50 static string_dictionary number_variable_dict = NULL;
51
52 /*
53  *  Internal Routine:
54  *
55  *    int is_digits(string text)
56  *        Effects: Returns true iff text matches [0-9]*.  ("" matches...)
57  */
58
59 static int
60 is_digits(string text)
61 {
62     for (; *text; text++)
63       if (!isdigit(*text))
64         return(0);
65
66     return(1);
67 }
68
69 /*
70  *  Internal Routine:
71  *
72  *    int is_number_variable(string text)
73  *        Effects: Returns true iff text matches [0-9]+.
74  */
75
76 #define  is_number_variable(x)      (isdigit(*(x)) && is_digits((x)))
77
78 /*
79  *    void var_clear_all_variables()
80  *        Requires: This routine must be called before any other
81  *                  var module routine is called.
82  *        Modifies: All description language variables
83  *        Effects: Sets all description langauge variables to "".
84  */
85
86 void
87 var_clear_all_variables(void)
88 {
89     if (non_number_variable_dict) {
90         string_dictionary_SafeDestroy(non_number_variable_dict);
91         string_dictionary_SafeDestroy(number_variable_dict);
92     }
93
94     non_number_variable_dict = string_dictionary_Create(101);
95     number_variable_dict = string_dictionary_Create(11);
96     fields_data_length = 0;
97 }
98
99 /*
100  *    string var_get_variable(string name)
101  *        Requires: var_clear_all_variables has been called
102  *        Effects: Returns the value of the description langauge variable
103  *                 named name.  The returned string is read-only and is
104  *                 guarenteed to last only until the next var module
105  *                 call.  DO NOT FREE THIS STRING.
106  */
107
108 string
109 var_get_variable(string name)
110 {
111     char *result;
112     int field_to_get;
113     static string last_get_field_call_result = NULL;
114
115     if (is_number_variable(name)) {
116         if (result = string_dictionary_Fetch(number_variable_dict, name))
117           return(result);
118
119         /*
120          * Convert name to an integer avoiding overflow:
121          */
122         while (*name=='0')
123           name++;
124         if (strlen(name)>12)
125           field_to_get = 0; /* no way we'll have > 1 trillian fields... */
126         else
127           field_to_get = atoi(name);
128
129         if (!field_to_get)
130           return("");
131         if (last_get_field_call_result)
132           free(last_get_field_call_result);
133         last_get_field_call_result = get_field(fields_data,
134                                                fields_data_length,
135                                                field_to_get);
136         return(last_get_field_call_result);
137     }
138
139     if (!(result = string_dictionary_Fetch(non_number_variable_dict, name)))
140       result = "";
141
142     return(result);
143 }
144
145 /*
146  *    void var_set_variable(string name, value)
147  *        Requires: var_clear_all_variables has been called
148  *        Modifies: The value of description langauge variable
149  *                  named name.
150  *        Effects: Sets the description langauge variable named name
151  *                 to have the value value.
152  */
153
154 void
155 var_set_variable(string name,
156                  string value)
157 {
158     string_dictionary_Set(is_number_variable(name) ? number_variable_dict
159                           : non_number_variable_dict, name, value);
160 }
161
162 /*
163  *    void var_set_variable_to_number(string name; int number)
164  *        Requires: var_clear_all_variables has been called
165  *        Modifies: The value of description langauge variable
166  *                  named name.
167  *        Effects: Sets the description langauge variable named name
168  *                 to have as its value number's ascii representation.
169  */
170
171 void
172 var_set_variable_to_number(string name,
173                            int number)
174 {
175     char buffer[20];
176
177     sprintf(buffer, "%d", number);
178     var_set_variable(name, buffer);
179 }
180
181 /*
182  *    void var_set_variable_then_free_value(string name, value)
183  *        Requires: var_clear_all_variables has been called, value is
184  *                  on the heap.
185  *        Modifies: The value of description langauge variable
186  *                  named name, value
187  *        Effects: Sets the description langauge variable named name
188  *                 to have the value value then frees value.  This
189  *                 routine is slightly faster than calling var_set_variable
190  *                 then freeing value.  It is provided mainly for
191  *                 convenience reasons.
192  */
193
194 void
195 var_set_variable_then_free_value(string name,
196                                  string value)
197 {
198     string_dictionary_binding *binding;
199     int exists;
200
201 #ifdef DEBUG_MEMORY
202     if (!memory__on_heap_p(value))
203       abort(); /* <<<>>> */
204 #endif
205
206     if (is_number_variable(name)) {
207         var_set_variable(name, value);
208         free(value);
209         return;
210     }
211
212     binding = string_dictionary_Define(non_number_variable_dict, name,
213                                        &exists);
214     if (exists)
215       free(binding->value);
216     binding->value = value;
217 }
218
219 /*
220  *    void var_set_number_variables_to_fields(char *data, int length)
221  *        Requires: var_clear_all_variables has been called
222  *        Modifies: All numeric description language variables
223  *        Effects: Treats data[0]..data[length-1] as a series of
224  *                 null-seperated fields.  Sets $<number> (<number>
225  *                 here means [0-9]+ to field # <number> in data.
226  *                 Field 0 is defined to be "" as are all field #'s
227  *                 greater than the number of fields in data.
228  *                 Data[0]..data[length-1] must not be changed (or freed)
229  *                 until either this call is made again with different
230  *                 data or var_clear_all_variables is called.
231  */
232
233 void
234 var_set_number_variables_to_fields(char *data,
235                                    int length)
236 {
237     fields_data = data;
238     fields_data_length = length;
239
240     string_dictionary_SafeDestroy(number_variable_dict);
241     number_variable_dict = string_dictionary_Create(11);
242 }