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
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_variables_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* Module containing code to deal with description langauge variables: */
26 /****************************************************************************/
28 #include "new_memory.h"
30 #include "string_dictionary_aux.h"
31 #include "variables.h"
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.
39 static char *fields_data;
40 static int fields_data_length = 0;
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.
49 static string_dictionary non_number_variable_dict = NULL;
50 static string_dictionary number_variable_dict = NULL;
55 * int is_digits(string text)
56 * Effects: Returns true iff text matches [0-9]*. ("" matches...)
60 is_digits(string text)
72 * int is_number_variable(string text)
73 * Effects: Returns true iff text matches [0-9]+.
76 #define is_number_variable(x) (isdigit(*(x)) && is_digits((x)))
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 "".
87 var_clear_all_variables(void)
89 if (non_number_variable_dict) {
90 string_dictionary_SafeDestroy(non_number_variable_dict);
91 string_dictionary_SafeDestroy(number_variable_dict);
94 non_number_variable_dict = string_dictionary_Create(101);
95 number_variable_dict = string_dictionary_Create(11);
96 fields_data_length = 0;
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.
109 var_get_variable(string name)
113 static string last_get_field_call_result = NULL;
115 if (is_number_variable(name)) {
116 if (result = string_dictionary_Fetch(number_variable_dict, name))
120 * Convert name to an integer avoiding overflow:
125 field_to_get = 0; /* no way we'll have > 1 trillian fields... */
127 field_to_get = atoi(name);
131 if (last_get_field_call_result)
132 free(last_get_field_call_result);
133 last_get_field_call_result = get_field(fields_data,
136 return(last_get_field_call_result);
139 if (!(result = string_dictionary_Fetch(non_number_variable_dict, name)))
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
150 * Effects: Sets the description langauge variable named name
151 * to have the value value.
155 var_set_variable(string name,
158 string_dictionary_Set(is_number_variable(name) ? number_variable_dict
159 : non_number_variable_dict, name, value);
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
167 * Effects: Sets the description langauge variable named name
168 * to have as its value number's ascii representation.
172 var_set_variable_to_number(string name,
177 sprintf(buffer, "%d", number);
178 var_set_variable(name, buffer);
182 * void var_set_variable_then_free_value(string name, value)
183 * Requires: var_clear_all_variables has been called, value is
185 * Modifies: The value of description langauge variable
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.
195 var_set_variable_then_free_value(string name,
198 string_dictionary_binding *binding;
202 if (!memory__on_heap_p(value))
203 abort(); /* <<<>>> */
206 if (is_number_variable(name)) {
207 var_set_variable(name, value);
212 binding = string_dictionary_Define(non_number_variable_dict, name,
215 free(binding->value);
216 binding->value = value;
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.
234 var_set_number_variables_to_fields(char *data,
238 fields_data_length = length;
240 string_dictionary_SafeDestroy(number_variable_dict);
241 number_variable_dict = string_dictionary_Create(11);