]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/variables.c
cda09d6e6ac2e11db2887d91a11d578944d0bc78
[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 is_digits(text)
60      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 var_clear_all_variables()
87 {
88     if (non_number_variable_dict) {
89         string_dictionary_SafeDestroy(non_number_variable_dict);
90         string_dictionary_SafeDestroy(number_variable_dict);
91     }
92
93     non_number_variable_dict = string_dictionary_Create(101);
94     number_variable_dict = string_dictionary_Create(11);
95     fields_data_length = 0;
96 }
97
98 /*
99  *    string var_get_variable(string name)
100  *        Requires: var_clear_all_variables has been called
101  *        Effects: Returns the value of the description langauge variable
102  *                 named name.  The returned string is read-only and is
103  *                 guarenteed to last only until the next var module
104  *                 call.  DO NOT FREE THIS STRING.
105  */
106
107 string var_get_variable(name)
108      string name;
109 {
110     char *result;
111     int field_to_get;
112     static string last_get_field_call_result = NULL;
113
114     if (is_number_variable(name)) {
115         if (result = string_dictionary_Fetch(number_variable_dict, name))
116           return(result);
117
118         /*
119          * Convert name to an integer avoiding overflow:
120          */
121         while (*name=='0')
122           name++;
123         if (strlen(name)>12)
124           field_to_get = 0; /* no way we'll have > 1 trillian fields... */
125         else
126           field_to_get = atoi(name);
127
128         if (!field_to_get)
129           return("");
130         if (last_get_field_call_result)
131           free(last_get_field_call_result);
132         last_get_field_call_result = get_field(fields_data,
133                                                fields_data_length,
134                                                field_to_get);
135         return(last_get_field_call_result);
136     }
137
138     if (!(result = string_dictionary_Fetch(non_number_variable_dict, name)))
139       result = "";
140
141     return(result);
142 }
143
144 /*
145  *    void var_set_variable(string name, value)
146  *        Requires: var_clear_all_variables has been called
147  *        Modifies: The value of description langauge variable
148  *                  named name.
149  *        Effects: Sets the description langauge variable named name
150  *                 to have the value value.
151  */
152
153 void var_set_variable(name, value)
154      string name;
155      string value;
156 {
157     string_dictionary_Set(is_number_variable(name) ? number_variable_dict
158                           : non_number_variable_dict, name, value);
159 }
160
161 /*
162  *    void var_set_variable_to_number(string name; int number)
163  *        Requires: var_clear_all_variables has been called
164  *        Modifies: The value of description langauge variable
165  *                  named name.
166  *        Effects: Sets the description langauge variable named name
167  *                 to have as its value number's ascii representation.
168  */
169
170 void var_set_variable_to_number(name, number)
171      string name;
172      int number;
173 {
174     char buffer[20];
175
176     sprintf(buffer, "%d", number);
177     var_set_variable(name, buffer);
178 }
179
180 /*
181  *    void var_set_variable_then_free_value(string name, value)
182  *        Requires: var_clear_all_variables has been called, value is
183  *                  on the heap.
184  *        Modifies: The value of description langauge variable
185  *                  named name, value
186  *        Effects: Sets the description langauge variable named name
187  *                 to have the value value then frees value.  This
188  *                 routine is slightly faster than calling var_set_variable
189  *                 then freeing value.  It is provided mainly for
190  *                 convenience reasons.
191  */
192
193 void var_set_variable_then_free_value(name, value)
194      string name;
195      string value;
196 {
197     string_dictionary_binding *binding;
198     int exists;
199
200 #ifdef DEBUG_MEMORY
201     if (!memory__on_heap_p(value))
202       abort(); /* <<<>>> */
203 #endif
204
205     if (is_number_variable(name)) {
206         var_set_variable(name, value);
207         free(value);
208         return;
209     }
210
211     binding = string_dictionary_Define(non_number_variable_dict, name,
212                                        &exists);
213     if (exists)
214       free(binding->value);
215     binding->value = value;
216 }
217
218 /*
219  *    void var_set_number_variables_to_fields(char *data, int length)
220  *        Requires: var_clear_all_variables has been called
221  *        Modifies: All numeric description language variables
222  *        Effects: Treats data[0]..data[length-1] as a series of
223  *                 null-seperated fields.  Sets $<number> (<number>
224  *                 here means [0-9]+ to field # <number> in data.
225  *                 Field 0 is defined to be "" as are all field #'s
226  *                 greater than the number of fields in data.
227  *                 Data[0]..data[length-1] must not be changed (or freed)
228  *                 until either this call is made again with different
229  *                 data or var_clear_all_variables is called.
230  */
231
232 void var_set_number_variables_to_fields(data, length)
233      char *data;
234      int length;
235 {
236     fields_data = data;
237     fields_data_length = length;
238
239     string_dictionary_SafeDestroy(number_variable_dict);
240     number_variable_dict = string_dictionary_Create(11);
241 }