summaryrefslogtreecommitdiff
path: root/zwgc/variables.c
blob: 3368c1fda41bbc6ad77a4edddccf189e67d743db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_variables_c[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

/****************************************************************************/
/*                                                                          */
/*   Module containing code to deal with description langauge variables:    */
/*                                                                          */
/****************************************************************************/

#include "new_memory.h"
#include "notice.h"
#include "string_dictionary_aux.h"
#include "variables.h"

/*
 * fields_data[_length] - these point to the field data that the number
 *                        variables were last set to using 
 *                        var_set_number_variables_to_fields.
 */

static char *fields_data;
static int fields_data_length = 0;

/*
 * [non_]number_variable_dict - contains the values of all the [non-]number
 *                              variables that have been set since the last
 *                              var_clear_all_variables call or (for numbers
 *                              only) var_set_number_variables_to_fields call.
 */

static string_dictionary non_number_variable_dict = NULL;
static string_dictionary number_variable_dict = NULL;

/*
 *  Internal Routine:
 *
 *    int is_digits(string text)
 *        Effects: Returns true iff text matches [0-9]*.  ("" matches...)
 */

static int
is_digits(string text)
{
    for (; *text; text++)
      if (!isdigit(*text))
	return(0);

    return(1);
}

/*
 *  Internal Routine:
 *
 *    int is_number_variable(string text)
 *        Effects: Returns true iff text matches [0-9]+.
 */

#define  is_number_variable(x)      (isdigit(*(x)) && is_digits((x)))

/*
 *    void var_clear_all_variables()
 *        Requires: This routine must be called before any other
 *                  var module routine is called.
 *        Modifies: All description language variables
 *        Effects: Sets all description langauge variables to "".
 */

void
var_clear_all_variables(void)
{
    if (non_number_variable_dict) {
	string_dictionary_SafeDestroy(non_number_variable_dict);
	string_dictionary_SafeDestroy(number_variable_dict);
    }

    non_number_variable_dict = string_dictionary_Create(101);
    number_variable_dict = string_dictionary_Create(11);
    fields_data_length = 0;
}

/*
 *    string var_get_variable(string name)
 *        Requires: var_clear_all_variables has been called
 *        Effects: Returns the value of the description langauge variable
 *                 named name.  The returned string is read-only and is
 *                 guarenteed to last only until the next var module
 *                 call.  DO NOT FREE THIS STRING.
 */

string
var_get_variable(string name)
{
    char *result;
    int field_to_get;
    static string last_get_field_call_result = NULL;

    if (is_number_variable(name)) {
	result = string_dictionary_Fetch(number_variable_dict, name);
	if (result)
	  return(result);

	/*
	 * Convert name to an integer avoiding overflow:
	 */
	while (*name=='0')
	  name++;
	if (strlen(name)>12)
	  field_to_get = 0; /* no way we'll have > 1 trillian fields... */
	else
	  field_to_get = atoi(name);

	if (!field_to_get)
	  return("");
	if (last_get_field_call_result)
	  free(last_get_field_call_result);
	last_get_field_call_result = get_field(fields_data,
					       fields_data_length,
					       field_to_get);
	return(last_get_field_call_result);
    }

    if (!(result = string_dictionary_Fetch(non_number_variable_dict, name)))
      result = "";

    return(result);
}

/*
 *    void var_set_variable(string name, value)
 *        Requires: var_clear_all_variables has been called
 *        Modifies: The value of description langauge variable
 *                  named name.
 *        Effects: Sets the description langauge variable named name
 *                 to have the value value.
 */

void
var_set_variable(string name,
		 string value)
{
    string_dictionary_Set(is_number_variable(name) ? number_variable_dict
			  : non_number_variable_dict, name, value);
}

/*
 *    void var_set_variable_to_number(string name; int number)
 *        Requires: var_clear_all_variables has been called
 *        Modifies: The value of description langauge variable
 *                  named name.
 *        Effects: Sets the description langauge variable named name
 *                 to have as its value number's ascii representation.
 */

void
var_set_variable_to_number(string name,
			   int number)
{
    char buffer[20];

    sprintf(buffer, "%d", number);
    var_set_variable(name, buffer);
}

/*
 *    void var_set_variable_then_free_value(string name, value)
 *        Requires: var_clear_all_variables has been called, value is
 *                  on the heap.
 *        Modifies: The value of description langauge variable
 *                  named name, value
 *        Effects: Sets the description langauge variable named name
 *                 to have the value value then frees value.  This
 *                 routine is slightly faster than calling var_set_variable
 *                 then freeing value.  It is provided mainly for
 *                 convenience reasons.
 */

void
var_set_variable_then_free_value(string name,
				 string value)
{
    string_dictionary_binding *binding;
    int exists;

#ifdef DEBUG_MEMORY
    if (!memory__on_heap_p(value))
      abort(); /* <<<>>> */
#endif

    if (is_number_variable(name)) {
	var_set_variable(name, value);
	free(value);
	return;
    }

    binding = string_dictionary_Define(non_number_variable_dict, name,
				       &exists);
    if (exists)
      free(binding->value);
    binding->value = value;
}

/*
 *    void var_set_number_variables_to_fields(char *data, int length)
 *        Requires: var_clear_all_variables has been called
 *        Modifies: All numeric description language variables
 *        Effects: Treats data[0]..data[length-1] as a series of
 *                 null-seperated fields.  Sets $<number> (<number>
 *                 here means [0-9]+ to field # <number> in data.
 *                 Field 0 is defined to be "" as are all field #'s
 *                 greater than the number of fields in data.
 *                 Data[0]..data[length-1] must not be changed (or freed)
 *                 until either this call is made again with different
 *                 data or var_clear_all_variables is called.
 */

void
var_set_number_variables_to_fields(char *data,
				   int length)
{
    fields_data = data;
    fields_data_length = length;

    string_dictionary_SafeDestroy(number_variable_dict);
    number_variable_dict = string_dictionary_Create(11);
}