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>
7 * $Id: variables.c,v 1.5 1999/01/22 23:20:42 ghudson Exp $
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: variables.c,v 1.5 1999/01/22 23:20:42 ghudson Exp $";
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...)
59 static int is_digits(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 "".
86 void var_clear_all_variables()
88 if (non_number_variable_dict) {
89 string_dictionary_SafeDestroy(non_number_variable_dict);
90 string_dictionary_SafeDestroy(number_variable_dict);
93 non_number_variable_dict = string_dictionary_Create(101);
94 number_variable_dict = string_dictionary_Create(11);
95 fields_data_length = 0;
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.
107 string var_get_variable(name)
112 static string last_get_field_call_result = NULL;
114 if (is_number_variable(name)) {
115 if (result = string_dictionary_Fetch(number_variable_dict, name))
119 * Convert name to an integer avoiding overflow:
124 field_to_get = 0; /* no way we'll have > 1 trillian fields... */
126 field_to_get = atoi(name);
130 if (last_get_field_call_result)
131 free(last_get_field_call_result);
132 last_get_field_call_result = get_field(fields_data,
135 return(last_get_field_call_result);
138 if (!(result = string_dictionary_Fetch(non_number_variable_dict, name)))
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
149 * Effects: Sets the description langauge variable named name
150 * to have the value value.
153 void var_set_variable(name, value)
157 string_dictionary_Set(is_number_variable(name) ? number_variable_dict
158 : non_number_variable_dict, name, value);
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
166 * Effects: Sets the description langauge variable named name
167 * to have as its value number's ascii representation.
170 void var_set_variable_to_number(name, number)
176 sprintf(buffer, "%d", number);
177 var_set_variable(name, buffer);
181 * void var_set_variable_then_free_value(string name, value)
182 * Requires: var_clear_all_variables has been called, value is
184 * Modifies: The value of description langauge variable
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.
193 void var_set_variable_then_free_value(name, value)
197 string_dictionary_binding *binding;
201 if (!memory__on_heap_p(value))
202 abort(); /* <<<>>> */
205 if (is_number_variable(name)) {
206 var_set_variable(name, value);
211 binding = string_dictionary_Define(non_number_variable_dict, name,
214 free(binding->value);
215 binding->value = value;
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.
232 void var_set_number_variables_to_fields(data, length)
237 fields_data_length = length;
239 string_dictionary_SafeDestroy(number_variable_dict);
240 number_variable_dict = string_dictionary_Create(11);