1    | #include <stdio.h>
2    | #include <stdlib.h>
3    | #include <glib.h>
4    | #include "ca_defs.h"
5    | #define DEBUG
6    | 
7    | void stringPack(char *dest, const char *source)
8    | {
9    | #ifdef DEBUG
10   | printf("\nInside stringPack function\n");
11   | #endif 	/* DEBUG */
12   | 
13   | /*----------------------------------------------------------------------*\
14   | 
15   | *  Function to rewrite a line of text with only one blankspace between  *
16   | *  each word.
17   | *
18   | 
19   | \*----------------------------------------------------------------------*/
20   | 
21   | 
22   | /*
23   |  * This while loop continues until the NULL character is copied into
24   |  * the destination string.  If a tab character is copied into the 
25   |  * destination string, it is replaced with a blank-space character.
26   |  *
27   |  * Multiple blank-space and/or tab characters are skipped in the source
28   |  * string until any other character is found.
29   |  */
30   | 
31   | 	while (1)
32   | 		{
33   | 		*dest = *source;
34   | 
35   | 		if (*dest == '\t')
36   | 			(*dest = ' ');
37   | 	
38   | 		/* Exit if have copied the end of the string. */
39   | 		if (*dest == '\0')
40   | 			return;
41   | 
42   | /*
43   |  * If the source character was a blank-space or a tab, move to the next 
44   |  * source character.  While the source character is a blank-space or a
45   |  * tab, move to the next character (i.e. ignore these characters).  When
46   |  * any other character is found in the source string, move to the next
47   |  * element of the destination string.
48   |  *
49   |  * Otherwise, simultaneously, move to the next elements of the destination
50   |  * and the source strings.
51   |  */
52   | 
53   | 
54   | 		
55   | 		if ( (*source == ' ') || (*source == '\t') )
56   | 			{
57   | 			++source;
58   | 			while ( (*source == ' ') || (*source == '\t') )
59   | 				{
60   | 				++source;
61   | 				}
62   | 
63   | 			++dest;
64   | 			}
65   | 		else
66   | 			{
67   | 			++dest;
68   | 			++source;
69   | 			}
70   | 		}
71   | }
72   | 
73   | 
74   | void ca_populateDictionary(dict_t woordenboek[], int size)
75   | 
76   | /*******************************************************************
77   |  * ca_populateDictionary -- Parses dictionary file, initializes    *
78   |  *									the dictionary structure and writes    *
79   |  *									the file of dictionary symbols, 			*
80   | 	*									ca_dictSyms.h									*
81   | 	*																						*
82   | 	* Parameters																		*
83   | 	*		woordenboek -- the dictionary to be populated					*
84   | 	*		size -- the total number of variables i.e. the size of the  *
85   |  *	 			  array of dict_t structures.  See D. & D., p.276		*
86   |  *																						*
87   |  * Returns																			*
88   | 	*		Nothing ?  (may change this later)									*
89   |  *																						*
90   |  *******************************************************************/
91   | 
92   | {
93   | const char *blankLine = "\n";
94   | const char *comment = "#";
95   | char line[120];
96   | char input[120];
97   | char test[120];
98   | int lineNo = 0;
99   | int i;
100  | int entry = 0;
101  | FILE *dictPtr, *defnPtr;
102  | 
103  | gchar **tokens;                         /* Pointer to an array of strings. */
104  | 		
105  | /*
106  | 	* Try to open the dictionary file for reading.  If it cannot be
107  |  * opened, exit with an error.
108  |  */
109  | if ( (dictPtr = fopen("dictionary.txt", "r")) == NULL)
110  | 		{
111  | 		fprintf(stderr, "Error: Unable to open 'dictionary.txt'\n");
112  | 		exit (51);
113  | 		}
114  | 		
115  | 
116  | 	/*
117  | 	 *Try to open the definitions file for writing.  If it cannot be
118  |   * opened,exit with an error
119  |   */
120  | if ( (defnPtr = fopen("defs.txt", "w")) == NULL)
121  | 		{
122  | 		fprintf(stderr, "Error: Unable to open 'defs.txt'\n");
123  | 		exit (51);
124  | 		}
125  | 	
126  | 		/*
127  | 		 * Read the file one line at a time;
128  | 		 * if the line begins with a comment, ignore it;
129  | 		 * otherwise, split each line into tokens;
130  | 		 * print each token.
131  | 		 * Assign each token to the appropriate member of
132  | 		 * the appropriate element of the dictionary array.
133  | 		 */
134  | 		
135  | 		fgets(input, sizeof(input), dictPtr);
136  | 	
137  | 		if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0) )
138  | 
139  | 			{
140  | 			/*
141  | 			 * First remove the newline character.
142  | 			 * Then replace multiple tab and space
143  | 			 * characters with single space characters.
144  | 			 */
145  | 
146  | 			/* Remove the newline character, if present. 
147  | 			 * Replace the last character of the string
148  | 			 * array with with '\0'.
149  | 			 */
150  | 
151  | 			input[strlen(input) - 1] = '\0';
152  | 
153  | 			/* Now, remove the multiple space and tab
154  | 			 * characters.
155  | 			 */
156  | 
157  | 			stringPack(line, input);
158  | 			
159  | 			g_strchomp(line); /* Remove trailing w-space. */
160  | #ifdef DEBUG
161  | puts(line);
162  | #endif	/*DEBUG */
163  | 
164  | 			tokens = g_strsplit(line, " ", 0);
165  | 
166  | #ifdef DEBUG						
167  | 			for (i = 0; tokens[i] != NULL; i++)
168  | 				printf("tokens[%d] = %s\n", i, tokens[i]);
169  | #endif	/* DEBUG */
170  | 
171  | 			/* We no longer need a variable for scope
172  | 			 * woordenboek[entry].varScope = atoi(tokens[1]);
173  | 			 */
174  | 
175  | 			strcpy(woordenboek[entry].varName, tokens[0]);
176  | 			strcpy(woordenboek[entry].varSym, tokens[1]);
177  | 			strcpy(woordenboek[entry].varType, tokens[2]);
178  | 			woordenboek[entry].varNum = entry;
179  | 			
180  |        /*
181  | 			 * Write the dictionary symbol and the entry number 
182  | 			 * to the definitions file.
183  |         */
184  | 			fprintf(defnPtr, "%s\t%d\n", tokens[1], entry);
185  | 
186  | 			++entry; 
187  | 			}
188  | 	/*
189  | 	 * Get the 2nd and subsequent line of the file.
190  | 	 */
191  | 
192  | 	fgets(input, sizeof(input), dictPtr);
193  | 
194  | 	while(!feof(dictPtr) )
195  | 	{
196  | 		/*
197  | 		 * Process the line if it is not a comment.
198  | 		 */
199  | 
200  | 		if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0 ) )
201  | 		{
202  | 			/*
203  | 			 * First remove the newline character.
204  | 			 * Then replace multiple tab and space
205  | 			 * characters with single space characters.
206  | 			 */
207  | 
208  | 			/* Remove the newline character, if present. 
209  | 			 * Replace the last character of the string
210  | 			 * array with with '\0'.
211  | 			 */
212  | 
213  | 			input[strlen(input) - 1] = '\0';
214  | 
215  | 			/* Now, remove the multiple space and tab
216  | 			 * characters.
217  | 			 */
218  | 
219  | 			stringPack(line, input);
220  | 			
221  | 			g_strchomp(line); /* Remove trailing w/space. */
222  | #ifdef	DEBUG
223  | puts(line);
224  | #endif	/* DEBUG */
225  | 			tokens = g_strsplit(line, " ", 0);
226  | 			
227  | #ifdef DEBUG
228  | 			for (i = 0; tokens[i] != NULL; i++)
229  | 				printf("tokens[%d] = %s\n", i, tokens[i]);
230  | #endif	/* DEBUG */
231  | 
232  | 			/*
233  | 			 * We no longer need to know the scope of a variable
234  | 			 * woordenboek[entry].varScope = atoi(tokens[1]);
235  |         */
236  | 
237  | 			strcpy(woordenboek[entry].varName, tokens[0]);
238  | 			strcpy(woordenboek[entry].varSym, tokens[1]);
239  | 			strcpy(woordenboek[entry].varType, tokens[2]);
240  | 			woordenboek[entry].varNum = entry;
241  | 			fprintf(defnPtr, "%s\t%d\n", tokens[1], entry);
242  | 			++entry;
243  | 		}
244  | 		fgets(input, sizeof(input), dictPtr);
245  | 	}		
246  | 
247  | fclose(dictPtr);
248  | fclose(defnPtr);
249  | 
250  | }	/* End of ca_populateDictionary() function. */
251  | 
252  | 
253  | void opSplitsen (FILE *filePtr, gchar **tokenArray)
254  | {
255  | /*
256  |  * Declaring character constants is safer than using #define.
257  |  * See Oualline's book, p.145.
258  |  *
259  |  */
260  | 
261  | const char *blankLine = "\n";		/* Declared as a string, not a character. */
262  | const char *comment = "#";			/* Declared as a string. */
263  | char line[99];
264  | char input[99];
265  | int lineNo = 0;
266  | int j;
267  | 
268  | 
269  | 	fgets(input, sizeof(input), filePtr); /* Get the (first) line from the */
270  | 					 /* file to which filePtr points. */
271  | 	
272  | #ifdef DEBUG
273  | 	printf("\nFIRST INPUT >>> %s\n", input);
274  | #endif	/* DEBUG */
275  | 
276  | 	/* Compare the first character of the input */
277  | 	/* to the comment and the newline strings. */
278  | 
279  | 	if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0) )
280  | 					
281  | 				
282  | 
283  | 		{
284  | 		/* Remove the newline character, if present. */
285  | 		/* Replace the last character */
286  | 		/* of the string array with '\0'. */
287  | 
288  | 		input[strlen(input) - 1] = '\0';	
289  | #ifdef DEBUG
290  | printf("First Input >>> %s\n", input);
291  | #endif /* DEBUG */
292  | 
293  | 		strcpy(line, input);
294  | #ifdef DEBUG
295  | printf("First Line after copy >>> %s\n", line);
296  | #endif 	/* DEBUG */
297  | 
298  | 		stringPack(line, input);     
299  | #ifdef DEBUG
300  | printf("Line: %s\n", line);
301  | #endif	/* DEBUG */
302  | 
303  | 		g_strchomp(line);
304  | /*		g_strdelimit(line, " ", ':');
305  |  *		g_strdelimit(line, "\t", '*');
306  | */
307  | 
308  | 		printf("%3d> %s\n", ++lineNo, line);
309  | 
310  | 		/*
311  | 		 * g_strsplit() is a GLib function;
312  | 		 * it returns an array of strings.
313  | 		 * 
314  | 		 * Here, we split on two spaces, "  ".
315  | 		 * We set max_tokenArray to be 0.  We want the 
316  | 		 * first token to be the name of the variable
317  | 		 * and the other tokens to be the value of the variable,
318  | 		 * qualifiers, etc.
319  | 		 */
320  | 
321  | 		tokenArray = g_strsplit(line, " ", 0);	
322  | 
323  | 		for (j = 0; tokenArray[j] != NULL; j++)
324  | 			printf("token[%d] = %s\n", j, tokenArray[j]);
325  | 		} /* End of processing the first line, if not commented. */
326  | 
327  | 		/* End of getting the first line. */
328  | 
329  | 
330  | 	/*Get the 2nd line of the file. */
331  | 	fgets(input, sizeof(input), filePtr);
332  | 
333  | 	while(!feof(filePtr) )
334  | 		{
335  | 			
336  | 			/* Process the line if it is not commented. */
337  | 			if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0 ) )
338  | 			{
339  | 	   	/* Remove the newline character, if present. */ 
340  | 			input[strlen(input) -1] = '\0';
341  | #ifdef DEBUG
342  | printf("Subsequent Input >>> %s\n", input);
343  | #endif	/* DEBUG */
344  | 
345  | 			strcpy(line, input);
346  | #ifdef DEBUG
347  | printf("Subsequent Line after copy >>> %s\n", line);
348  | #endif	/* DEBUG */
349  | 
350  | 			stringPack(line, input);
351  | #ifdef DEBUG
352  | printf("Line: %s\n", line);
353  | #endif	/* DEBUG */
354  | 
355  | 			g_strchomp(line);
356  | /*			g_strdelimit(line, " ", ':');
357  |  *			g_strdelimit(line, "\t", '*');
358  |  */
359  | 			printf("%3d> %s\n", ++lineNo, line);
360  | 
361  | 			/*
362  | 			 * See the comment above about the maximum 
363  | 			 * number of tokens being set to 0.
364  | 			 */
365  | 
366  | 			tokenArray = g_strsplit(line, " ", 0);
367  | 			for (j = 0; tokenArray[j] != NULL; j++)
368  | 				{	
369  | 				printf("token[%d] = %s\n", j, tokenArray[j]);
370  | 				/* Can also use puts(tokenArray[j]) here. */
371  | 				}
372  | 			} /* Processed uncommented lines. */
373  | 
374  | 		fgets(input, sizeof(input), filePtr);
375  | 		} /* Processed the 2nd & subsequent lines of the file. */
376  | 
377  | } /* End of processing the opened file. */
378  | 
379  | 
380  | void ca_readConfig(const char *configFile, values_t confVars[], int size)
381  | /*******************************************************************
382  |  *																						*
383  |  * ca_readConfig -- parses the config file and writes the values   *
384  |  * 						 into memory.												*
385  |  *																						*
386  |  * Parameters																		*
387  |  *		configFile -- the configuration file
388  | 	*		confVars[] -- the array of values structures						*
389  | 	*		size -- the number of configuration variables					*
390  |  * 																						*
391  |  * Returns																			*
392  |  *		Nothing -- perhaps make this return 0 on successful exit ?	*
393  |  *																						*
394  |  * Note: 	Should we make the name of the config file a global		*
395  | 	*			variable ?																*	
396  |  *******************************************************************/
397  | {
398  | FILE *confPtr;			/* Pointer to config file. */
399  | char name[STRLENGTH];		/* The name of the config variable */
400  | char value[STRLENGTH];			/* The value of the variable */
401  | int location;			/* Storage Location of the variable's value. */
402  | int type;	 /* Data type of the variable, represented by an integer. */
403  | 
404  | 
405  | const char *blankLine = "\n";  /* Declared as a string, not a character. */
406  | const char *comment = "#"; 		/* Declared as a string. */
407  | 
408  | char source[16];									/* The name of a source. */
409  | char database[STRLENGTH]; 		/* The elements of a database. */
410  | 
411  | gchar **dbcomps;	/* Pointer to an array of strings that represents */
412  | 							/* the components of a db. */
413  | 
414  | int i;					/* A counting variable. */
415  | 
416  | ca_database_t *newDbPtr; 	/* A pointer to a new instance of */
417  | 										/* ca_database_t.						 */
418  | 
419  | ca_database_list_t *newSrc;	/* A pointer to a new instance of */
420  | 										/* ca_database_list_t.				 */
421  | 
422  | /* 
423  |  * Function Prototype for ca_getStorageLocation()
424  |  * We put it here; thus it can only be called from 
425  |  * within ca_readConfig()
426  |  *
427  |  * This function finds the location in the values_t array
428  |  * where we store pointers to the string value and the actual
429  |  * value of the variable.  It returns this location as an 
430  |  * integer.
431  |  *
432  |  */
433  | int ca_getStorageLocation(char [], dict_t [], int);
434  | 
435  | /*
436  | 	* Function Prototype for ca_getType()
437  | 	* We put it here so that it can only be called from
438  | 	* within ca_readConfig()
439  |  *
440  |  * This function returns the type of the configuration
441  |  * variable.  It returns it as a string.
442  |  *
443  |  */
444  | int ca_getType(char [], dict_t [], int);
445  | 
446  | 
447  | #ifdef	DEBUG
448  | printf("\nInside readConfig() function.\n");
449  | printf("Configuration file is: %s\n", configFile);
450  | #endif	/* DEBUG */
451  | 
452  | /*
453  | 	* Open the configuration file for reading .....
454  |  */
455  | if ( (confPtr = fopen(configFile, "r")) == NULL)
456  | 		{
457  | 		printf("Error: file %s could not be opened.\n", configFile);
458  | 		exit (51);
459  | 		}
460  | 
461  | /*
462  | 	* Read the first record in the configuration file .....
463  |  * We read the _name_ of the variable using fscanf into a
464  |  * string array.  We read the _value_ of the variable
465  |  * using fgets into an array; thus, we can handle values of
466  |  * variables with qualifiers (e.g. SPLIT after DBLIST) and
467  |  * values with blank characters (e.g. REPLYBANNER).
468  |  */
469  | fscanf(confPtr, "%s", name);
470  | fgets(value, sizeof(value), confPtr);
471  | 
472  | 
473  | /* 
474  | 	* 		While there are records to be read in the config file.
475  | 	*		write the current record into memory,
476  |  *     read the next record in the config file
477  |  */
478  | 
479  | 
480  | while (!feof(confPtr) )
481  | 	{
482  | 
483  | /*
484  | 	* From the variable name, find the dictionary number.
485  |  * The dictionary number is defined as the place in the 
486  |  * values array in which to store the value of the variable.
487  |  * 
488  |  */
489  | 
490  | 		/*
491  | 		 * Process the line only when/if it is not a comment or 
492  | 		 * a blankline.
493  |      */
494  | 		if ( (strncmp(name, comment, 1) != 0) && (strncmp(name, blankLine, 1) != 0) )
495  | 			{
496  | 			/*
497  |  		 * If the last character of "value" is '\n',
498  |         * replace it with '\0'.
499  |         */
500  | 			if ( value[strlen(value) - 1] == '\n')
501  | 				{
502  | 				value[strlen(value) - 1] = '\0';
503  | 				}
504  | 
505  | 			/*
506  | 			 * From the variable name, find the element of the values
507  | 			 * array in which to store the value of the variable.
508  |         *
509  | 			 */
510  | 			location = ca_getStorageLocation(name, dictionary, VARS);
511  | 			printf("The location is: %d\n", location);
512  | 
513  | 			/*
514  | 			 * Store a pointer to the string that contains the value
515  | 			 * This is not necessarily the actual value itself.
516  | 			 * First, we must allocate some memory.
517  | 			 */
518  | 			confVars[location].strPtr = (char *)malloc(STRLENGTH);
519  | 			/*
520  | 			 * We check the return value of the malloc function .....
521  |         */	
522  | 			if (confVars[location].strPtr == NULL)
523  | 				{
524  | 				fprintf(stderr, "Cannot allocate memory for confVars[location].strPtr\n");
525  | 				exit (8);
526  | 				}
527  | 
528  | 			strcpy(confVars[location].strPtr, value);
529  | 
530  | 			/*
531  | 			 * Now, store a pointer to the _value_ of the variable.  
532  | 			 * Do this as follows:
533  | 			 * (a) get the _type_ of the variable
534  | 			 * (b) store a pointer to the value of the variable in 
535  | 			 *     a way that depends on the _type_ of the variable.
536  |         */
537  | #ifdef DEBUG
538  | printf("Variable \"%s\" is data-type \"%d\"\n", name, ca_getType(name, dictionary, VARS) );
539  | #endif /* DEBUG */
540  | 
541  | 
542  | type = ca_getType(name, dictionary, VARS);
543  | 
544  | 			/*
545  | 			 * Given the _type_ of the variable, store the value of the
546  |   		 * variable in the appropriate way.
547  | 			 */		 
548  | 			switch(type)	
549  | 				{
550  | 				case 11:
551  | 				puts("Data type is Integer");
552  | 				confVars[location].valPtr = malloc(sizeof(int) );
553  | 				if (confVars[location].valPtr == NULL)
554  | 					{
555  | 					fprintf(stderr, "Cannot allocate memory !!!\n");
556  | 					exit (8);
557  | 					}
558  | 				sscanf(value, "%d", (int *) confVars[location].valPtr);
559  | 				break;
560  | 
561  | 				case 12:
562  | 				puts("Data type is String");
563  | 				confVars[location].valPtr = (char *)malloc(STRLENGTH);
564  | 				if (confVars[location].valPtr == NULL)
565  | 					{
566  | 					fprintf(stderr, "Cannot allocate memory !!!\n");
567  | 					exit (8);
568  | 					}
569  | 				 strcpy(confVars[location].valPtr, value);
570  | 				break;
571  | 
572  | 				case 13:
573  | 				puts("Data type is Dirlist");
574  | 				confVars[location].valPtr = (char *)malloc(STRLENGTH);
575  | 				if (confVars[location].valPtr == NULL)
576  | 					{
577  | 					fprintf(stderr, "Cannot allocate memory !!!\n");
578  | 					exit (8);
579  | 					}
580  | 				 strcpy(confVars[location].valPtr, value);
581  | 				break;
582  | 
583  | 				case 15:
584  | 				puts("Data type is Source !!!");
585  | 				/*
586  | 				 * Split the value into "source" and "database"
587  |            * Use blankspace as the delimiter between the
588  | 				 * "source" and "database".
589  | 				 */
590  | 				sscanf(value, "%s %s", source, database);
591  | #ifdef DEBUG
592  | puts(source);
593  | puts(database);
594  | #endif	/* DEBUG */
595  | 
596  | 				/*
597  | 				 * Using the values in "database".
598  | 				 * populate a ca_database_t structure.
599  | 				 * Give this variable a name.
600  | 				 *
601  | 				 */
602  | 
603  | 	          	/* First, separate the values in "database", using "," as 
604  | 					 * as a delimiting  character.
605  | 					 */
606  | 				dbcomps = g_strsplit(database, ",", 0);
607  | 
608  | #ifdef DEBUG                                            
609  | for (i = 0; dbcomps[i] != NULL; i++)
610  | 		printf("dbcomps[%d] = %s\n", i, dbcomps[i]);
611  | #endif  /* DEBUG */
612  | 
613  | 
614  | 					/*
615  | 					 * Create a structure for this database.
616  | 					 */
617  | 				newDbPtr = malloc(sizeof(ca_database_t));
618  | 				if (newDbPtr == NULL)
619  | 					{
620  | 					fprintf(stderr, "Cannot allocate memory to new db structure\n");
621  | 					exit (8);
622  | 					}
623  | 
624  | 				strcpy(newDbPtr->host, dbcomps[0]);
625  | 				strcpy(newDbPtr->port, dbcomps[1]);
626  | 				strcpy(newDbPtr->user, dbcomps[2]);
627  | 				strcpy(newDbPtr->password, dbcomps[3]);
628  | 				strcpy(newDbPtr->dbName, dbcomps[4]);
629  | #ifdef DEBUG
630  | puts("Testing the population of the db structure:");
631  | printf("\n%s::%s::%s::%s::%s\n", newDbPtr->host, newDbPtr->port, newDbPtr->user, newDbPtr->password, newDbPtr->dbName);
632  | #endif /* DEBUG */
633  | 
634  | 				/*
635  | 				 * Using the above ca_database_t structure
636  | 				 * and the "source" value, 
637  | 				 * populate the ca_src_t structure.
638  | 				 */
639  | 			
640  | 					/*
641  | 					 * Create a new structure for this source.
642  | 					 */
643  | 				newSrc = malloc(sizeof(ca_database_list_t));
644  | 
645  | 				if (newSrc == NULL)
646  | 					{
647  | 					fprintf(stderr, "Cannot allocate memory to new source structure\n");
648  | 					exit (8);
649  | 					}
650  | 
651  | 				strcpy(newSrc->name, source);
652  | 				newSrc->db = *newDbPtr;
653  | 
654  | #ifdef DEBUG
655  | puts("Testing the population of the ca_database_list_t structure:");
656  | printf("\n%s::%s::%s::%s::%s::%s\n", newSrc->name, (newSrc->db).host, (newSrc->db).port, (newSrc->db).user, (newSrc->db).password, (newSrc->db).dbName);
657  | #endif /* DEBUG */
658  | 
659  | 				/*
660  | 				 * Append this ca_src_t structure to the sourceList,
661  | 				 * which is a singly-linked list if type GSList.
662  | 				 */
663  | 
664  | sourceList = g_slist_append(sourceList, newSrc);
665  | 
666  | 				break;
667  | 
668  | 				default:
669  | 				printf("Data type not found for variable \"%s\".\n", name);
670  | 				break;
671  | 				}
672  | 	 		}
673  | 
674  | fscanf(confPtr, "%s", name);
675  | fgets(value, sizeof(value), confPtr);
676  | 
677  | 	}	/* End of processing the config file. */
678  | 
679  | }	/* End of readConfig() function */
680  | 
681  | 
682  | /*
683  |  * void ca_populateDictionary(dictionary_t woordenboek[], int size, FILE *fiPtr)
684  |  * {
685  |  * int j;
686  |  * char input[99];
687  |  * 
688  |  * for (j=0; (j < size) && !feof(fiPtr); j++)
689  |  * 	j = 0;
690  |  * 	while ((j < size) && !feof(fiPtr) )
691  |  * 	{
692  |  * 	printf("\n%d\n", j);
693  |  * 	
694  |  * 	fgets(input, sizeof(input), filePtr);
695  |  * 
696  |  * 	if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) 
697  |  * != 0) )
698  |  * 	{
699  |  * 	fscanf(fiPtr, "%s %s %s %s", woordenboek[j].varName, woordenboek[j].varScope, woordenboek[j].varSym, woordenboek[j].varType);
700  |  * 	}
701  |  * 
702  |  * 	fgets(input, sizeof(input), filePtr);
703  |  * 	printf("%s\n", woordenboek[j].varName);
704  |  * 	}
705  |  * }
706  |  * 
707  |  */
708  | 
709  | 
710  | void ca_getDictionary(dict_t woordenboek[], int size)
711  | {
712  | int k;
713  | 
714  | for (k = 0; k <= size; k++)
715  | 	{
716  | 	printf("\nj = %d\n", k);
717  |  /*
718  | 	 * printf("%s\t%d\t%s\n", woordenboek[k].varName, woordenboek[k].varScope, woordenboek[k].varType);
719  | 	 */
720  | 	 printf("%s\t%s\t%s\t%d\n", woordenboek[k].varName, woordenboek[k].varSym, woordenboek[k].varType, woordenboek[k].varNum);
721  | 
722  | 	}
723  | }
724  | 
725  | 
726  | int ca_get_int(int symbol)
727  | {
728  | int *xPtr;
729  | 
730  | 		/*
731  | 		 * First print a message saying that the ca_get_int()
732  | 		 * function is being called.
733  | 		 */
734  | #ifdef DEBUG
735  | 	printf("\nDEBUG: ca_get_int() function is called .....\n");
736  | printf("DEBUG: New value of StringPtr: %s\n", globals[symbol].strPtr);
737  | #endif 	/* DEBUG */
738  | 
739  | 		/*
740  | 		 * Look at the appropriate place in the dictionary;
741  | 		 * e.g. C_BINDPORT => the first element, index = 0.
742  | 		 *
743  | 		 * if the varType is not an integer, exit with an error;
744  | 		 *
745  | 		 * otherwise, 
746  | 		 *		if the varScope is global, look for the value in the
747  | 		 *		appropriate place in memory in the global values array;
748  | 		 *    otherwise, look for the value in the appropriate place in 
749  | 		 * 	memory in the local values array;
750  | 		 *
751  | 		 *
752  | 		 */
753  | 
754  | 		/* Look at the appropriate place in the dictionary. */
755  | 
756  | #ifdef DEBUG
757  | 		printf("\nDEBUG: Variable type: %s\n", dictionary[symbol].varType);
758  | #endif	/* DEBUG */
759  | 
760  | 		/* If the variable type is not an integer, exit with an error. */
761  | 		if ( strcmp(dictionary[symbol].varType, "CA_INT") != 0)
762  | 			{
763  | 			fprintf(stderr, "Error: unexpected variable type.\n");
764  | 			exit (51);
765  | 			}
766  | 		else
767  | 			{
768  | 			/*
769  | 			 * If the variable has global scope, look for it in 
770  | 			 * the globals array.  Otherwise, look for it in the
771  | 			 * locals array.
772  | 			 *
773  | 			 */
774  | 			
775  | 			/*
776  | 			 * Lock the value of the variable before reading it.
777  | 		 	 */
778  | 
779  | 			mutex_lock(&Lock);
780  | 
781  | 				switch(dictionary[symbol].varScope)
782  | 					{
783  | 					case 1:
784  | #ifdef	DEBUG
785  | 					printf("\nThis variable has global scope.\n");
786  | 					printf("The string is: %s\n", globals[symbol].strPtr);
787  | 					printf("String2Value: %d\n", atoi(globals[symbol].strPtr));
788  | #endif	/* DEBUG */
789  | 
790  | 					xPtr = globals[symbol].valPtr;
791  | 					printf("Value: %d\n", *xPtr);
792  | 					return(*xPtr);
793  | 					break;
794  | 
795  | 					case 99:
796  | #ifdef	DEBUG
797  | 					printf("\nThis variable has local scope.\n");
798  | 					printf("The string is %s\n", locals[symbol].strPtr);
799  | 					printf("String2Value: %d\n", atoi(locals[symbol].strPtr));
800  | #endif	/* DEBUG */
801  | 					xPtr = locals[symbol].valPtr;
802  | 					printf("Value: %d\n", *xPtr);
803  | 					return(*xPtr);
804  | 					break;
805  | 
806  | 					default:
807  | 					printf("\nAaaargh !!!  This variable has unwelcome scope.\n");			
808  | 					break;
809  | 					}
810  | 			/* 
811  | 			 * Unlock the value of the variable after reading it.
812  | 			 */
813  | 			mutex_unlock(&Lock);
814  | 			
815  | 			}
816  | 	
817  | }
818  | 
819  | char *ca_get_dirlist(int symbol)
820  | {
821  | /*
822  |  * This function returns a pointer to a character array.  Thus,
823  |  * we need to declare such a pointer.
824  |  *
825  |  */
826  | 
827  | char *xPtr;
828  | #ifdef	DEBUG
829  | 	printf("\nca_get_dirlist() function is called .....\n");
830  | #endif	/* DEBUG */
831  | 
832  | 
833  | 		/*
834  | 		 * Look at the appropriate place in the dictionary;
835  | 		 * e.g. CA_HELP => the second element, index = 1.
836  | 		 *
837  | 		 * if the varType is not CA_DIRLIST, exit with an error;
838  | 		 *
839  | 		 * otherwise, 
840  | 		 *		if the varScope is global, look for the value in the
841  | 		 *		appropriate place in memory in the global values array;
842  | 		 *    otherwise, look for the value in the appropriate place in 
843  | 		 * 	memory in the local values array;
844  | 		 *
845  | 		 *
846  | 		 */
847  | 
848  | 		/* Look at the appropriate place in the dictionary. */
849  | 		
850  | 		printf("\nVariable type: %s\n", dictionary[symbol].varType);
851  | 	
852  | 		/* If the variable type is not CA_DIRLIST, exit with an error. */
853  | 		if ( strcmp(dictionary[symbol].varType, "CA_DIRLIST") != 0)
854  | 			{
855  | 			fprintf(stderr, "Error: unexpected variable type.\n");
856  | 			exit (51);
857  | 			}
858  | 		else
859  | 			{
860  | 			/*
861  | 			 * If the variable has global scope, look for it in 
862  | 			 * the globals array.  Otherwise, look for it in the
863  | 			 * locals array.
864  | 			 *
865  | 			 */
866  | 
867  | 				switch(dictionary[symbol].varScope)
868  | 					{
869  | 					case 1:
870  | 					printf("\nThis variable has global scope.\n");
871  | 					printf("The string is: %s\n", globals[symbol].strPtr);
872  | 					xPtr = globals[symbol].valPtr;
873  | 					printf("Value: %s\n", xPtr);
874  | 					return(xPtr);
875  | 					break;
876  | 
877  | 					case 99:
878  | 					printf("\nThis variable has local scope.\n");
879  | 					printf("The string is %s\n", locals[symbol].strPtr);
880  | 					xPtr = locals[symbol].valPtr;
881  | 					printf("Value: %s\n", xPtr);
882  | 					return(xPtr);
883  | 					break;
884  | 
885  | 					default:
886  | 					printf("\nAaaargh !!!  This variable has unwelcome scope.\n");			
887  | 					break;
888  | 					}
889  | 			
890  | 			}
891  | 	
892  | }
893  | 
894  | 
895  | char *ca_get_string(int symbol)
896  | {
897  | /*
898  |  * This function returns a pointer to a character array.  Thus,
899  |  * we need to declare such a pointer.
900  |  *
901  |  */
902  | 
903  | char *xPtr;
904  | #ifdef	DEBUG
905  | 	printf("\nca_get_text() function is called .....\n");
906  | #endif	/* DEBUG */
907  | 
908  | 
909  | 		/*
910  | 		 * Look at the appropriate place in the dictionary;
911  | 		 * e.g. CA_REPLYBANNER => the third element, index = 2.
912  | 		 *
913  | 		 * if the varType is not CA_STRING, exit with an error;
914  | 		 *
915  | 		 * otherwise, 
916  | 		 *		if the varScope is global, look for the value in the
917  | 		 *		appropriate place in memory in the global values array;
918  | 		 *    otherwise, look for the value in the appropriate place in 
919  | 		 * 	memory in the local values array;
920  | 		 *
921  | 		 *
922  | 		 */
923  | 
924  | 		/* Look at the appropriate place in the dictionary. */
925  | 		
926  | 		printf("\nVariable type: %s\n", dictionary[symbol].varType);
927  | 	
928  | 		/* If the variable type is not CA_STRING, exit with an error. */
929  | 		if ( strcmp(dictionary[symbol].varType, "CA_STRING") != 0)
930  | 			{
931  | 			fprintf(stderr, "Error: unexpected variable type.\n");
932  | 			exit (51);
933  | 			}
934  | 		else
935  | 			{
936  | 			/*
937  | 			 * If the variable has global scope, look for it in 
938  | 			 * the globals array.  Otherwise, look for it in the
939  | 			 * locals array.
940  | 			 *
941  | 			 */
942  | 
943  | 				switch(dictionary[symbol].varScope)
944  | 					{
945  | 					case 1:
946  | 					printf("\nThis variable has global scope.\n");
947  | 					printf("The string is: %s\n", globals[symbol].strPtr);
948  | 					xPtr = globals[symbol].valPtr;
949  | 					printf("Value: %s\n", xPtr);
950  | 					return(xPtr);
951  | 					break;
952  | 
953  | 					case 99:
954  | 					printf("\nThis variable has local scope.\n");
955  | 					printf("The string is %s\n", locals[symbol].strPtr);
956  | 					xPtr = locals[symbol].valPtr;
957  | 					printf("Value: %s\n", xPtr);
958  | 					return(xPtr);
959  | 					break;
960  | 
961  | 					default:
962  | 					printf("\nAaaargh !!!  This variable has unwelcome scope.\n");			
963  | 					break;
964  | 					}
965  | 			
966  | 			}
967  | }
968  | 
969  | 
970  | int ca_get_boolean(int symbol)
971  | {
972  | /**********************************************
973  |  * ca_get_boolean()									*
974  | 	* 															*
975  | 	*															*
976  | 	* Parameters											*
977  | 	*															*
978  | 	*	symbol -- the symbol for the variable		*
979  |  *															*
980  | 	*															*
981  | 	* Returns												*
982  | 	*															*
983  | 	*	1 if true, 0 if false.							*
984  |  *															*
985  |  * Remarks												*
986  | 	*															*
987  |  *   Is there a better way to implement 		*
988  | 	*   Boolean values in C ?							*
989  |  *															*
990  | 	*********************************************/
991  | 
992  | int *xPtr;
993  | 
994  | /*
995  |  * Print this message if in debug mode.
996  |  *
997  |  */
998  | #ifdef DEBUG
999  | 	printf("\nca_get_boolean() function is called .....\n");
1000 | printf("DEBUG 5: New value of StringPtr: %s\n", globals[symbol].strPtr);
1001 | #endif	/* DEBUG	*/
1002 | 
1003 | /**********************************************\
1004 |  *															*
1005 | 	* Here is how this works:							*
1006 |  * 															*
1007 |  * (a) Check that the type of variable whose 	*
1008 |  *     value is being read is CA_BOOLEAN.		*
1009 |  *															*
1010 |  * (b) Lock the value of the variable before	*
1011 |  * 		reading it.										*
1012 |  *															*
1013 |  * (c) Depending on the scope of the variable	*
1014 |  *     look for it in the appropriate array.	*
1015 |  *															*
1016 | 	* (d) Read the value of the variable.			*
1017 |  *															*
1018 | 	* (e) Unlock the value of the variable after *
1019 |  *		reading it.										*
1020 | 	*															*
1021 |  *															*
1022 |  * Returns												*
1023 | 	*
1024 | 	*	an integer value as follows:					*
1025 | 	*		1 if the db is in testmode (true),							*
1026 | 	*		0 if the db is not in testmode (false).					*
1027 | \*********************************************/
1028 | 
1029 | 
1030 | /*
1031 | 	* Look at the appropriate place in the dictionary; 
1032 | 	* e.g. CA_BOOLEAN = the fifth element of the dict_t array,
1033 |  * => index = 4.
1034 |  *
1035 |  * If the varType is not Boolean, exit with an error
1036 | 	* 
1037 |  * Otherwise,
1038 | 	*	if the varScope is global, look for the value in the
1039 | 	* 	appropriate place in the global values array;
1040 | 	*
1041 | 	*	otherwise, look for the value in the appropriate place in the
1042 | 	*	locals array.
1043 | 	*
1044 |  */
1045 | 
1046 | #ifdef DEBUG
1047 | /* Look in the appropriate place in the dictionary. */
1048 | printf("\nVariable type: %s\n", dictionary[symbol].varType);
1049 | #endif	/* DEBUG */
1050 | 
1051 | /* If the variable type is not Boolean, exit with an error. */
1052 | 
1053 | 	if ( strcmp(dictionary[symbol].varType, "CA_BOOLEAN") != 0)
1054 | 		{
1055 | 		fprintf(stderr, "Error: Boolean type expected.\n");
1056 | 		exit (51);
1057 | 		}
1058 | 
1059 | 	else
1060 | 		{
1061 | 		/*
1062 | 		 * Lock the value of the variable before reading it.
1063 | 		 *
1064 | 		 */
1065 | 		
1066 | 		mutex_lock(&Lock);
1067 | 		
1068 | 		/*
1069 | 		 * If the variable has global scope, look for it in the globals
1070 | 		 * array.  Otherwise, look for it in the locals array.
1071 | 		 *
1072 |      */
1073 | 
1074 | 		switch(dictionary[symbol].varScope)
1075 | 			{
1076 | 			case 1:
1077 | 			printf("\nThis variable has global scope.\n");
1078 | 			printf("The string is: %s\n", globals[symbol].strPtr);
1079 | 			printf("String2Value: %d\n", atoi(globals[symbol].strPtr) );
1080 | 			xPtr = globals[symbol].valPtr;
1081 | 			printf("Value: %d\n", *xPtr);
1082 | 			return (*xPtr);
1083 | 			break;
1084 | 
1085 | 			case 99:
1086 | 			printf("\nThis variable has local scope.\n");
1087 | 			printf("The string is %s\n", locals[symbol].strPtr);
1088 | 			printf("String2Value: %d\n", atoi(locals[symbol].strPtr) );
1089 | 			xPtr = locals[symbol].valPtr;
1090 | 			printf("Value: %d\n", *xPtr);
1091 | 			return(*xPtr);
1092 | 			break;
1093 | 
1094 | 			default:
1095 | 			printf("\nError: This variable has unknown scope.\n");
1096 | 			break;
1097 | 			}
1098 | 
1099 | 		/*
1100 | 		 * Unlock the value of the variable after reading it.
1101 | 		 */
1102 | 		mutex_unlock(&Lock);
1103 | 		
1104 | 		}	
1105 | 
1106 | }
1107 | 
1108 | 
1109 | 
1110 | void ca_set_int(int symbol)
1111 | {
1112 | 	/*********************************************
1113 | 	 * ca_set_int()										*
1114 |   *  														*
1115 | 	 * Parameters											*
1116 | 	 *		symbol -- the symbol for the variable.	*
1117 | 	 *															*
1118 | 	 * Returns												*
1119 | 	 *		1 if successful 0 if not ?					*
1120 | 	 *															*
1121 | 	 * Remarks												*
1122 | 	 * 	Needs a better way to check for valid  *
1123 |   *    values from the keyboard.					*
1124 | 	 *															*
1125 | 	 *********************************************/
1126 | 
1127 | 	void *tempPtr;		/* Temp pointer to point to the value pointer
1128 | 								in the appropriate values array. */
1129 |  char newPort[16];
1130 |  int invalid;
1131 |  int portNr;
1132 | 
1133 |  /* Function to change the value in a given values array.
1134 |   * This function can only be called from within ca_set_int().
1135 | 	 */
1136 |  int *ca_change_int_value(char []); 
1137 |  void testFunction(values_t values[]);
1138 | 
1139 | 	/*
1140 |   * Using the symbol, look at the appropriate place in the
1141 | 	 * dictionary.
1142 | 	 */
1143 | #ifdef DEBUG
1144 |  printf("\nca_set_int() function called .....\n");
1145 | 	printf("Variable type: %s\n", dictionary[symbol].varType);
1146 | #endif	/* DEBUG */
1147 | 
1148 | 
1149 | /*
1150 |  * Make sure that a reasonable, sensible value of bind-port has
1151 |  * been read from the keyboard.
1152 |  */
1153 | 
1154 | do	{
1155 | 
1156 | 		/*
1157 | 		 * First, flush input stream.
1158 |      */
1159 | 		fflush(stdin);
1160 | 
1161 | 		/*
1162 | 		 * Prompt for the new value of the bind-port.
1163 | 		 */
1164 | 		
1165 | 		printf("\nNew value of bind-port (non-zero positive integer) >>> ");
1166 | 		scanf("%s", newPort);
1167 | 		/*
1168 | 		 * gets(newPort);                                  
1169 | 		 */
1170 | #ifdef DEBUG
1171 | 		printf("\nDEBUG: Value of newPort variable: %s\n", newPort);
1172 | #endif	/* DEBUG */
1173 | 
1174 | 		sscanf(newPort, "%d", &portNr);
1175 | 
1176 | #ifdef DEBUG
1177 | 		printf("\nDEBUG: Value of integer variable, portNr: %d\n", portNr);
1178 | #endif	/* DEBUG */
1179 | 		
1180 | 			if (portNr < 0)
1181 | 				{
1182 | 				invalid = 1;
1183 | 				puts("Only non-zero positive integer values accepted for bind-port");
1184 | 				}
1185 | 			else
1186 | 				{
1187 | 				invalid = 0;
1188 | 				}
1189 | 
1190 | 		} while(invalid);
1191 | 
1192 |  /*
1193 | 	 * Check that the function is attempting to set the correct type
1194 | 	 * of value.  If not, do not set the value and exit.
1195 | 	 */
1196 | 
1197 | 	if (strcmp(dictionary[symbol].varType, "CA_INT") != 0)
1198 | 		{
1199 | 		fprintf(stderr, "Error: unexpected variable type.\n");
1200 | 		exit (51);
1201 | 		}
1202 | 
1203 | 	/*
1204 | 	 * Choose the appropriate values array.  
1205 | 	 */
1206 | 	switch(dictionary[symbol].varScope)
1207 | 		{
1208 | 		/* If the variable has global scope, 
1209 | 		 * write it into the globals array.
1210 | 		 * If it has local scope,
1211 | 		 * write it into the local array.
1212 | 		 * If the scope cannot be found, then report an error.
1213 | 		 */
1214 | 		case 1:
1215 | 		globals[symbol].valPtr = ca_change_int_value(newPort);
1216 | 		globals[symbol].strPtr = newPort;
1217 | 		
1218 | 		globals[symbol].strPtr = (char *)malloc(sizeof(newPort) );
1219 | 		
1220 | 		/* Check the return value of malloc() to make sure that we 
1221 | 		 * actually got the memory.
1222 | 		 */
1223 | 		if (globals[symbol].strPtr == NULL)
1224 | 			{	
1225 | 			fprintf(stderr, "Cannot allocate memory for globals[symbol].strPtr.\n");
1226 | 			exit (8);
1227 | 			}
1228 | #ifdef DEBUG
1229 | 		printf("DEBUG: New value of StringPtr: %s\n", globals[symbol].strPtr);
1230 | #endif	/* DEBUG */
1231 | 
1232 | 		strcpy(globals[symbol].strPtr, newPort);
1233 | 
1234 | #ifdef DEBUG
1235 | 		printf("DEBUG 2: New value of StringPtr: %s\n", globals[symbol].strPtr);
1236 | #endif	/* DEBUG */
1237 | 		break;
1238 | 
1239 | 		case 99:
1240 | 		locals[symbol].valPtr = ca_change_int_value(newPort);
1241 | 		/*
1242 | 		 * First allocate some memory and then copy the value of the new 
1243 | 		 * Port into it.
1244 |      */
1245 | 		locals[symbol].strPtr = (char *)malloc(sizeof(newPort) );
1246 | 		/* 
1247 | 		 * Now, check that the memory was actually allocated.
1248 | 		 */
1249 | 		if (locals[symbol].strPtr == NULL)
1250 | 			{
1251 | 			fprintf(stderr, "Cannot allocate memory for locals[symbol].strPtr\n");
1252 | 			exit(8);
1253 | 			}
1254 | 		
1255 | 		strcpy(locals[symbol].strPtr, newPort);
1256 | 		/*
1257 | 		 * locals[symbol].strPtr = newPort;
1258 | 		 */	
1259 | 		break;
1260 | 
1261 | 		default:
1262 | 		fprintf(stderr, "Error; unknown scope: %d\n", dictionary[symbol].varScope);
1263 | 		break;
1264 | 		}
1265 | 
1266 |  /*
1267 |   * Write the new value of the variable to the correct place in 
1268 |   * this array.  (First, set a mutex lock ???).
1269 | 	 */
1270 | 
1271 |  /*
1272 | 	 * Write the new value of this variable back to the config. file
1273 | 	 */
1274 | 
1275 | ca_writeNewValue(symbol, newPort);
1276 | 
1277 | 		printf("DEBUG 3: New value of StringPtr: %s\n", globals[symbol].strPtr);
1278 | 
1279 | }
1280 | 
1281 | int *ca_change_int_value(char value[])
1282 | {
1283 | void *tempPtr;
1284 | 
1285 | /*
1286 |  * Check the return value of malloc() in case we did not actually get
1287 |  * the memory.
1288 |  */
1289 | tempPtr = malloc(sizeof(int) );
1290 | if (tempPtr == NULL)
1291 | 		{
1292 | 		fprintf(stderr, "Cannot allocate memory for tempPtr\n");
1293 | 		exit (8);
1294 | 		}
1295 | 
1296 | sscanf(value, "%d", (int *) tempPtr);
1297 | return(tempPtr);
1298 | }
1299 | 
1300 | 
1301 | 
1302 | void testFunction(values_t array[])
1303 | {
1304 | 	printf("\nInside the Test function.\n");
1305 | 	}
1306 | 
1307 | 
1308 | void ca_getDatabase(ca_database_t db)
1309 | {
1310 | printf("\n%s\t%s\t%s\t%s\t%s\n", db.host, db.port, db.user, db.password, db.dbName);
1311 | }
1312 | 
1313 | void ca_getSource(ca_database_list_t src)
1314 | {
1315 | printf("\n%s\t%s\t%s\t%s\t%s\t%s\n", src.name, (src.db).host, (src.db).port, (src.db).user, (src.db).password, (src.db).dbName);
1316 | }
1317 | 
1318 | 
1319 | void ca_getAllSources(GSList *sources)
1320 | {
1321 | /*
1322 |  * Function Prototype of getSource().
1323 |  * getSource can only be called from within getAllSources().
1324 |  */
1325 | void ca_getAsource(ca_database_list_t *);
1326 | 	 
1327 | GSList *currentPtr;	/* Pointer to the structure at which we look. */
1328 | 
1329 | /*
1330 |  * Look at the first member of the linked-list of sources.
1331 |  */
1332 | currentPtr = sources;
1333 | 
1334 | /*
1335 |  * Look at each data component of the source list,
1336 |  * untill we reach the end of the list.
1337 |  */
1338 | while(currentPtr != NULL)
1339 | 	{
1340 |  ca_database_list_t *srcPtr = currentPtr->data;
1341 | printf("\n%s\t%s\t%s\t%s\t%s\t%s\n", srcPtr->name, (srcPtr->db).host, (srcPtr->db).port, (srcPtr->db).user, (srcPtr->db).password, (srcPtr->db).dbName);
1342 |  currentPtr = currentPtr->next;	
1343 | 	}
1344 | }
1345 | 
1346 | void ca_getAsource(ca_database_list_t *srcPtr)
1347 | {
1348 | printf("\n%s\t%s\t%s\t%s\t%s\t%s\n", srcPtr->name, (srcPtr->db).host, (srcPtr->db).port, (srcPtr->db).user, (srcPtr->db).password, (srcPtr->db).dbName);
1349 | }
1350 | 
1351 | 
1352 | void ca_set_boolean(int symbol)
1353 | {
1354 | /*************************************************************
1355 |  *																				*
1356 |  * ca_set_boolean()														*
1357 |  * 																				*
1358 | 	*																				*
1359 |  * Parameters																*
1360 |  *																				*
1361 | 	* 	symbol -- the symbol for the variable.							*
1362 |  *																				*
1363 | 	*																				*
1364 | 	* Returns																	*
1365 | 	*																				*
1366 | 	* 		nothing																*
1367 |  *																				*
1368 | 	*																				*
1369 |  * Remarks																	*
1370 | 	*																				*
1371 | 	* 	Must check that a sensible value is given as input.		*
1372 | 	*																				*
1373 | 	*																				*
1374 | 	*************************************************************/
1375 | 
1376 | 
1377 | char newTestmodeStr[2];
1378 | int newTestmodeVal;	/* The new value of the testmode variable. */
1379 | int invalid;				/* Flag to indicate an invalid new value.  */
1380 | 
1381 | FILE *testPtr, *tempPtr;			/* The pointer to the files. */
1382 | char name[STRLENGTH];				/* The name of the variable. */
1383 | char value[STRLENGTH];			/* The value of the variable. */
1384 | 
1385 | /*
1386 |  * Function to change the value in a given values array.
1387 |  * This function can only be called from within ca_set_boolean().
1388 |  */
1389 | int *ca_change_int_value(char []);
1390 | 
1391 | 
1392 | /*
1393 | 	* Using the symbol, look at the appropriate place in the 
1394 |  * dictionary.
1395 | 	*/
1396 | #ifdef DEBUG
1397 | printf("\nca_set_int() function called .....\n");
1398 | printf("Variable type: %s\n", dictionary[symbol].varType);
1399 | #endif		/* DEBUG */
1400 | 
1401 | /*
1402 |  * Check that the function is attempting to set the correct type of 
1403 |  * value.  If not, do not set the value, but exit instead.
1404 |  */
1405 | 
1406 | if (strcmp(dictionary[symbol].varType, "CA_BOOLEAN") != 0)
1407 | 		{
1408 | 		fprintf(stderr, "Error: CA_BOOLEAN data type expected.\n");
1409 | 		exit (51);
1410 | 		}
1411 | 
1412 | /*
1413 |  * First, flush the input stream.
1414 |  */
1415 | fflush(stdin);
1416 | 
1417 | 
1418 | /*
1419 |  * Make sure that a reasonable, sensible value of bind-port has
1420 |  * been read from the keyboard.
1421 |  */
1422 | 
1423 | do	{
1424 | 			/*
1425 | 			 * Prompt for the new value of the testmode.
1426 | 			 */
1427 | 
1428 | 			printf("\nNew value of testmode (0 or 1) >>> ");
1429 | 			scanf("%s", newTestmodeStr);
1430 | 
1431 | 			/*
1432 | 			 * We scanf() the value as a string, but we want it to be an
1433 | 			 * integer.  Thus, we use sscanf() to scanf the value from the
1434 |     	 * string-variable and store it as an integer in an integer
1435 | 			 * variable.
1436 | 			 */ 
1437 | 			sscanf(newTestmodeStr, "%d", &newTestmodeVal);
1438 | 
1439 | 			/*
1440 |         * We only change the testmode when the user is absolutely sure
1441 | 			 * that they want to change.  Thus, we only accept two possible
1442 | 			 * values for testmode.
1443 | 			 */
1444 | 
1445 | 			if ( (newTestmodeVal < 0) || (newTestmodeVal > 1) )
1446 | 				{
1447 | 				invalid = 1;
1448 | 				puts("Only '0' or '1' accepted as value for testmode.");
1449 | 				}
1450 | 			else
1451 | 				{
1452 | 				invalid = 0;
1453 | 				}	
1454 | 		} while(invalid);
1455 | 	
1456 | 
1457 | /*
1458 | 	* Lock the value of the variable before changing it.
1459 |  */
1460 | 
1461 | mutex_lock(&Lock);
1462 | 
1463 | 
1464 | /*
1465 |  * Choose the appropriate values array.
1466 |  */
1467 | 
1468 | switch(dictionary[symbol].varScope)
1469 | 		{
1470 | 		/*
1471 | 		 * If the variable has global scope, 
1472 | 		 * write it into the globals array.
1473 | 		 * If it has local scope, 
1474 | 		 * write it into the local array.
1475 | 		 * If the scope cannot be found, then report an error.
1476 |      */
1477 | 		case 1:
1478 | 		globals[symbol].valPtr = ca_change_int_value(newTestmodeStr);
1479 | 		globals[symbol].strPtr = newTestmodeStr;
1480 | 		break;
1481 | 
1482 | 		case 99:
1483 | 		locals[symbol].valPtr = ca_change_int_value(newTestmodeStr);
1484 | 		locals[symbol].strPtr = newTestmodeStr;
1485 | 		break;
1486 | 
1487 | 		default:
1488 | 		fprintf(stderr, "Error: unknown scope: %d\n", dictionary[symbol].varScope);
1489 | 		break;
1490 | 		}
1491 | 
1492 | /*
1493 | 	* Write the new value of this variable back to the config file.
1494 |  *
1495 |  * To be implemented.
1496 |  */
1497 | 
1498 | /*
1499 |  * Find the actual name of the variable from the dictionary
1500 |  * structure (use the variable symbol as an index into the
1501 |  * array of dictionary structures.
1502 |  */
1503 |  
1504 | 	printf("Name of variable to be changed: %s\n", dictionary[symbol].varName);
1505 | 	printf("Type of variable to be changed: %s\n", dictionary[symbol].varType);
1506 | 
1507 | /*
1508 | 	* Open the test config file for reading .....
1509 |  */
1510 | if ( (testPtr = fopen(testFile, "r")) == NULL)
1511 | 	{
1512 | 	printf("File \"%s\" could not be opened.\n", testFile);
1513 | 	exit (51);
1514 | 	}
1515 | 
1516 | /*
1517 | 	* Open the temporary file for writing .....
1518 |  */
1519 | if ((tempPtr = fopen(tempFile, "w")) == NULL)
1520 | 	{
1521 | 	printf("File \"%s\" could not be opened.\n", tempFile);
1522 |  exit (51);
1523 |  }
1524 | 
1525 | /*
1526 |  * Read the first record in the test config file.
1527 |  */
1528 |  
1529 |  fscanf(testPtr, "%s", name);
1530 |  fgets(value, sizeof(value), testPtr);
1531 |  
1532 |  /*
1533 |   * If the last character of "value" is '\n',
1534 |   * replace it with '\0'.
1535 |   */
1536 | 	if (value[strlen(value) - 1] == '\n')
1537 | 		{
1538 | 		printf("The value string is %s", value);
1539 | 		printf("Replacing last character of \"%s\" with the NULL character\n", name);
1540 | 		value[strlen(value) - 1] = '\0';
1541 | 		printf("The new value string is %s", value);
1542 | 		}
1543 | 
1544 | 
1545 | /*
1546 | 	* While there are records to be read in the test config file:
1547 | 	* 		Write the current record into the temporary file.
1548 |  * 		Read the next record in the config file.
1549 |  * Repeat untill the EOF has been reached.
1550 |  */
1551 | 	
1552 | while(!feof(testPtr) )
1553 | 	{
1554 |  fprintf(tempPtr, "%s %s\n", name, value);
1555 |  fscanf(testPtr, "%s", name);
1556 | 	fgets(value, sizeof(value), testPtr);
1557 | 
1558 |  /*
1559 |   * If the last character of "value" is '\n',
1560 |   * replace it with '\0'.
1561 |   */
1562 | 	if (value[strlen(value) - 1] == '\n')
1563 | 		{
1564 | 		printf("The last character of the value string is %c", value[strlen(value) - 1]);
1565 | 		printf("The value string is %s", value);
1566 | 		printf("Replacing last character of \"%s\" with the NULL character\n",name);
1567 | 		value[strlen(value) - 1] = '\0';
1568 | 		printf("The new value string is %s", value);
1569 | 		}
1570 | 
1571 |  
1572 |  /*
1573 |   * if we read the variable that we want to change,
1574 |   * stop reading this file and print only the name
1575 |   * of this variable to the temporary file.
1576 |   */
1577 | 
1578 | 	/*
1579 | 	 * If we read the variable that we want to change,
1580 |   * replace the value of this variable in the config
1581 |   * file with the value supplied from the keyboard.
1582 |   *
1583 |   */
1584 | 	if ( strcmp(name, dictionary[symbol].varName) == 0)
1585 | 		{
1586 | 		strcpy(value, newTestmodeStr);
1587 | 		printf("The replacement string is %s", value);
1588 | 		}
1589 | 	/*
1590 | 	 * Flush the pointer to the test config file.
1591 | 	 */
1592 | 	fflush(testPtr);
1593 | 
1594 | 	}				
1595 |  /* 
1596 | 	 * Here ends the loop that writes the config file, with the
1597 |   * new variable, to the temporary file.
1598 | 	 */
1599 | 
1600 | /*
1601 |  *
1602 |  * While !(the record to be updated)
1603 | 	*	BEGIN
1604 | 	*  Write the record to the temporary file
1605 |  *  Read the next record in the config file
1606 |  *	END
1607 |  *
1608 |  * Write the new value to the temporary file
1609 |  * Read the next record in the config file
1610 |  * COMMENT: this is the record to be updated.
1611 |  * COMMENT: discard this record.
1612 |  * 
1613 |  * Read the next record in the config file
1614 | 	*
1615 |  * While !(EOF)
1616 | 	*	BEGIN
1617 | 	*  write the record to the temporary file
1618 |  *  read the next record in the config file
1619 |  *  END
1620 |  *
1621 |  * Close Config file
1622 |  * Close Temporary file
1623 |  *
1624 |  * Open Temporary file for reading
1625 |  * Open Config file for writing
1626 |  *
1627 |  * Read the next record of the Temporary file
1628 |  *
1629 |  * While (!EOF of Temporary file)
1630 | 	*	BEGIN
1631 | 	*  write the record into the Config file
1632 | 	*  read the next record of the Temporary file
1633 |  *  END
1634 |  * 
1635 | 	*	Close Temporary file
1636 |  *  Close Config file
1637 |  *
1638 |  */
1639 | 
1640 | fclose(testPtr);
1641 | fclose(tempPtr);
1642 | 
1643 | /*
1644 | 	* Now, flush the file pointers
1645 |  */
1646 | 	fflush(testPtr);
1647 | 	fflush(tempPtr);
1648 | 
1649 | /*
1650 |  * Open the temporary file for reading.
1651 | 	* Open the config file for writing.
1652 |  * Write the contents of the temporary file
1653 |  * into the config file.
1654 |  */
1655 | 
1656 | /*
1657 | 	* Open the temporary file for reading .....
1658 |  */
1659 | if ((tempPtr = fopen(tempFile, "r")) == NULL)
1660 | 	{
1661 | 	printf("File \"%s\" could not be opened for reading.\n", tempFile);
1662 |  exit (51);
1663 |  }
1664 | 
1665 | /*
1666 | 	* Open the config file for writing .....
1667 |  */
1668 | if ((testPtr = fopen(testFile, "w")) == NULL)
1669 | 	{
1670 | 	printf("File \"%s\" could not be opened for writing.\n", testFile);
1671 |  exit (51);
1672 |  }
1673 | 
1674 | /*
1675 |  * Read the first record in the temporary file.
1676 |  */
1677 |  
1678 |  fscanf(tempPtr, "%s", name);
1679 |  fgets(value, sizeof(value), tempPtr);
1680 | 	printf("\nFIRST LINE: %s %s", name, value);
1681 |  
1682 |  
1683 | /*
1684 | 	* While there are records to be read in the temporary file:
1685 | 	* 		Write the current record into the test config file.
1686 |  * 		Read the next record in the temporary file.
1687 |  * Repeat untill the EOF has been reached.
1688 |  */
1689 | 	
1690 | while(!feof(tempPtr) )
1691 | 	{
1692 |  fprintf(testPtr, "%s %s", name, value);
1693 |  fscanf(tempPtr, "%s", name);
1694 | 	fgets(value, sizeof(value), tempPtr);
1695 |  }
1696 | 
1697 | fclose(testPtr);
1698 | fclose(tempPtr);
1699 | 
1700 | /*
1701 | 	* Unlock the value of the variable after setting it and writing the
1702 |  * new value back to the configuration (and the dictionary) file.
1703 | 	*
1704 |  */
1705 | 	mutex_unlock(&Lock);
1706 | 
1707 | }
1708 | 
1709 | 
1710 | void ca_set_dirlist(int symbol)
1711 | {
1712 | /****************************************************************
1713 | 	* ca_set_dirlist()															*
1714 |  *																					*
1715 |  * Parameters																  	*
1716 | 	*		symbol -- the symbol of the variable.							*
1717 |  *																					*
1718 |  * Returns																		*
1719 | 	*		1 if successful, 0 if not successful.							*
1720 | 	*																					*
1721 | 	* Remarks																		*
1722 |  *		Writing the new value back to the config file has yet to *
1723 | 	*		be implemented.														*
1724 |  *																					*
1725 | 	****************************************************************/
1726 | 
1727 |  char newDir[80];
1728 |  /*
1729 |   * Declare a pointer to a values_t variable.
1730 | 	 * Later, we shall assign this pointer to the first element
1731 |   * of either the globals or the locals array, as appropriate.
1732 |   */
1733 |  values_t *hereValues;
1734 | 
1735 | 	/*
1736 | 	 * Using the symbol, look in the appropriate place in the dictionary.
1737 | 	 */
1738 | #ifdef DEBUG
1739 | 	printf("\nca_set_dirlist() function called ..... \n");
1740 |  printf("Variable type: %s\n", dictionary[symbol].varType);
1741 | #endif
1742 | 
1743 | 	/*
1744 | 	 * First, flush the input stream.
1745 | 	 */
1746 | 	fflush(stdin);
1747 | 
1748 | 	/* 
1749 | 	 * Prompt for the new value of the directory.
1750 | 	 */
1751 | 	printf("\nNew value of %s [80 characters, maximum] >>> ", dictionary[symbol].varName);
1752 |  scanf("%s", newDir);
1753 | 	
1754 | /*
1755 |  * Make sure that a reasonable, sensible value of the directory 
1756 |  * value has been read from the keyboard.
1757 |  *
1758 |  * How do we implement this ???
1759 |  *
1760 |  */
1761 | 
1762 | 
1763 |  /*
1764 |   * Make sure that the function is attempting to set the correct type
1765 |   * of value.  If not, do not set the value - and exit.
1766 |   */
1767 | 
1768 | if (strcmp(dictionary[symbol].varType, "CA_DIRLIST") != 0)
1769 | 		{
1770 | 		fprintf(stderr, "Error: unexpected variable type.\n");
1771 | 		exit(51);
1772 | 		}	
1773 | 
1774 | 	/*
1775 |   * Choose the appropriate values array.
1776 | 	 * Assign a temporary pointer to this array.
1777 | 	 */
1778 | 
1779 | 	switch(dictionary[symbol].varScope)
1780 | 		{
1781 | 		/* If the variable has global scope,
1782 | 		 * write it into the globals array.
1783 | 		 * If it has local scope, 
1784 | 		 * write it into the locals array.
1785 |  	 * If the scope cannot be found, report an error.
1786 | 		 */
1787 | 		case 1:
1788 | 		hereValues = globals;
1789 | 		break;
1790 | 
1791 | 		case 99:
1792 | 		hereValues = locals;
1793 | 		break;
1794 | 
1795 | 		default:
1796 | 		fprintf(stderr, "Error: Unknown scope: %d\n", dictionary[symbol].varScope);
1797 | 		break;
1798 | 		}
1799 | 
1800 | 
1801 | 	/*
1802 |   * Check for the presence of the mutex lock:
1803 | 	 *		if present,
1804 | 	 * 		wait until it is available;
1805 | 	 *		else
1806 |   *			get the lock and proceed with the change of value.
1807 | 	 */
1808 | 	
1809 | 	/*
1810 |   * Write the new value of the variable to the correct place
1811 | 	 * in the [appropriate] values array.
1812 | 	 *
1813 |   * Note that there is a check to see if malloc() actually worked .....
1814 | 	 */
1815 | 
1816 | 		hereValues[symbol].valPtr = (char *)malloc(80);
1817 | 		if (hereValues[symbol].valPtr == NULL)
1818 | 			{
1819 | 			fprintf(stderr, "Cannot alllocate memory for hereValuesvlPtr\n");
1820 | 			exit (8);
1821 | 			}
1822 | 		strcpy(hereValues[symbol].valPtr,newDir); 
1823 | 
1824 | 
1825 | 		hereValues[symbol].strPtr = (char *)malloc(sizeof(newDir) );
1826 | 		if (hereValues[symbol].strPtr == NULL)
1827 | 			{
1828 | 			fprintf(stderr, "Cannot alllocate memory for hereValuestPtr\n");
1829 | 			exit (8);
1830 | 			}
1831 | 		strcpy(hereValues[symbol].strPtr, newDir);
1832 | 
1833 | 	/*
1834 | 	 * Free the temporary pointer, hereValues.
1835 |   *
1836 |   */
1837 | 	free(hereValues);
1838 | 	hereValues = NULL;
1839 | 		
1840 | 	/*
1841 | 	 * Release the mutex lock.
1842 | 	 */
1843 | 
1844 | 	/*
1845 | 	 * Write the new value of this variable back to the config file.
1846 | 	 */
1847 | 
1848 | }
1849 | 
1850 | 
1851 | void ca_set_string(int symbol)
1852 | {
1853 | 
1854 | /****************************************************************
1855 | 	* ca_set_string()															*
1856 |  *																					*
1857 |  * Parameters																  	*
1858 | 	*		symbol -- the symbol of the variable.							*
1859 |  *																					*
1860 |  * Returns																		*
1861 | 	*		1 if successful, 0 if not successful ?							*
1862 | 	*																					*
1863 | 	* Remarks																		*
1864 |  *		Writing the new value back to the config file has yet to *
1865 | 	*		be implemented.														*
1866 |  *																					*
1867 | 	****************************************************************/
1868 | 
1869 |  char newString[80];	/* May need to make this bigger. */
1870 | 
1871 |  /*
1872 |   * Declare a pointer to a values_t variable.
1873 | 	 * Later, we shall assign this pointer to the first element
1874 |   * of either the globals or the locals array, as appropriate.
1875 |   */
1876 |  values_t *hereValues;
1877 | 
1878 | 	/*
1879 | 	 * Using the symbol, look in the appropriate place in the dictionary.
1880 | 	 */
1881 | #ifdef DEBUG
1882 | 	printf("\nca_set_string() function called ..... \n");
1883 |  printf("Variable type: %s\n", dictionary[symbol].varType);
1884 | #endif
1885 | 
1886 | 	/*
1887 | 	 * First, flush the input stream.
1888 | 	 */
1889 | 	fflush(stdin);
1890 | 
1891 | 	/* 
1892 | 	 * Prompt for the new value of the string.
1893 | 	 */
1894 | 	printf("\nNew value of %s [80 characters, maximum] >>> ", dictionary[symbol].varName);
1895 | gets(newString);
1896 | 	
1897 | /*
1898 |  * Make sure that a reasonable, sensible value of the string
1899 |  * value has been read from the keyboard.
1900 |  *
1901 |  * How do we implement this ???
1902 |  *
1903 |  */
1904 | 
1905 | 
1906 |  /*
1907 |   * Make sure that the function is attempting to set the correct type
1908 |   * of value.  If not, do not set the value - and exit.
1909 |   */
1910 | 
1911 | if (strcmp(dictionary[symbol].varType, "CA_STRING") != 0)
1912 | 		{
1913 | 		fprintf(stderr, "Error: unexpected variable type.\n");
1914 | 		exit(51);
1915 | 		}	
1916 | 
1917 | 	/*
1918 |   * Choose the appropriate values array.
1919 | 	 * Assign a temporary pointer to this array.
1920 | 	 */
1921 | 
1922 | 	switch(dictionary[symbol].varScope)
1923 | 		{
1924 | 		/* If the variable has global scope,
1925 | 		 * write it into the globals array.
1926 | 		 * If it has local scope, 
1927 | 		 * write it into the locals array.
1928 |  	 * If the scope cannot be found, report an error.
1929 | 		 */
1930 | 		case 1:
1931 | 		hereValues = globals;
1932 | 		break;
1933 | 
1934 | 		case 99:
1935 | 		hereValues = locals;
1936 | 		break;
1937 | 
1938 | 		default:
1939 | 		fprintf(stderr, "Error: Unknown scope: %d\n", dictionary[symbol].varScope);
1940 | 		break;
1941 | 		}
1942 | 
1943 | 
1944 | 	/*
1945 |   * Check for the presence of the mutex lock:
1946 | 	 *		if present,
1947 | 	 * 		wait until it is available;
1948 | 	 *		else
1949 |   *			get the lock and proceed with the change of value.
1950 | 	 */
1951 | 	mutex_lock(&Lock);
1952 | 	
1953 | 	/*
1954 |   * Write the new value of the variable to the correct place
1955 | 	 * in the [appropriate] values array.
1956 |   * Note the check to the return value of malloc() to see if the
1957 | 	 * memory was actually obtained.
1958 | 	 */
1959 | 
1960 | 		hereValues[symbol].valPtr = (char *)malloc(80);
1961 | 		if (hereValues[symbol].valPtr == NULL)
1962 | 			{
1963 | 			fprintf(stderr, "Cannot allocate memory for hereValues[symbol].valPtr\n");
1964 | 			exit (8);
1965 | 			}
1966 | 		strcpy(hereValues[symbol].valPtr, newString); 
1967 | 
1968 | 
1969 | 		hereValues[symbol].strPtr = (char *)malloc(sizeof(newString) );
1970 | 		if (hereValues[symbol].strPtr == NULL)
1971 | 			{
1972 | 			fprintf(stderr, "Cannot allocate memory for hereValues[symbol].strPtr\n");
1973 | 			exit (8);
1974 | 			}
1975 | 		strcpy(hereValues[symbol].strPtr, newString);
1976 | 
1977 | 	/*
1978 | 	 * Free the temporary pointer, hereValues.
1979 | 	 *
1980 |   */
1981 | 	free(hereValues);
1982 |  hereValues = NULL;
1983 | 
1984 | 	/*
1985 | 	 * Release the mutex lock.
1986 | 	 */
1987 | 	mutex_unlock(&Lock);
1988 | 
1989 | 	/*
1990 | 	 * Write the new value of this variable back to the config file.
1991 | 	 * Implement this later ?
1992 | 	 */
1993 | 
1994 | }
1995 | 
1996 | 
1997 | int ca_writeNewValue(int dictSymbol, char *newValue)
1998 | {
1999 | 
2000 | FILE *confPtr;						/* Pointer to config file */
2001 | FILE *tempPtr;						/* The pointer to temp file. */
2002 | char name[STRLENGTH];				/* The name of the variable. */
2003 | char value[STRLENGTH];			/* The value of the variable. */
2004 | 
2005 | 
2006 | /*
2007 |  * Find the actual name of the variable from the dictionary
2008 |  * structure (use the variable symbol as an index into the
2009 |  * array of dictionary structures.
2010 |  */
2011 | #ifdef DEBUG 
2012 | 	printf("Name of variable to be changed: %s\n", dictionary[dictSymbol].varName);
2013 | 	printf("Type of variable to be changed: %s\n", dictionary[dictSymbol].varType);
2014 | #endif	/* DEBUG */
2015 | 
2016 | /*
2017 | 	* Open the test config file for reading .....
2018 |  */
2019 | if ( (confPtr = fopen(testFile, "r")) == NULL)
2020 | 	{
2021 | 	printf("File \"%s\" could not be opened.\n", testFile);
2022 | 	exit (51);
2023 | 	}
2024 | 
2025 | /*
2026 | 	* Open the temporary file for writing .....
2027 |  */
2028 | if ((tempPtr = fopen(tempFile, "w")) == NULL)
2029 | 	{
2030 | 	printf("File \"%s\" could not be opened.\n", tempFile);
2031 |  exit (51);
2032 |  }
2033 | 
2034 | /*
2035 |  * Read the first record in the test config file.
2036 |  */
2037 |  
2038 |  fscanf(confPtr, "%s", name);
2039 |  fgets(value, sizeof(value), confPtr);
2040 |  
2041 |  /*
2042 |   * If the last character of "value" is '\n',
2043 |   * replace it with '\0'.
2044 |   */
2045 | 	if (value[strlen(value) - 1] == '\n')
2046 | 		{
2047 | #ifdef DEBUG
2048 | 		printf("The value string is %s", value);
2049 | 		printf("Replacing last character of \"%s\" with the NULL character\n", name);
2050 | #endif	/* DEBUG */
2051 | 
2052 | 		value[strlen(value) - 1] = '\0';
2053 | 
2054 | #ifdef DEBUG
2055 | 		printf("The new value string is %s", value);
2056 | #endif	/* DEBUG */
2057 | 		}
2058 | 
2059 | 	/*
2060 | 	 * If we read the variable that we want to change,
2061 |   * replace the value of this variable in the config
2062 |   * file with the value supplied from the keyboard.
2063 |   *
2064 |   */
2065 | 	if ( strcmp(name, dictionary[dictSymbol].varName) == 0)
2066 | 		{
2067 | 		strcpy(value, newValue);
2068 | 
2069 | #ifdef DEBUG
2070 | 		printf("The replacement string is %s", value);
2071 | #endif	/* DEBUG */
2072 | }
2073 | 
2074 | /*
2075 | 	* While there are records to be read in the test config file:
2076 | 	* 		Write the current record into the temporary file.
2077 |  * 		Read the next record in the config file.
2078 |  * Repeat untill the EOF has been reached.
2079 |  */
2080 | 	
2081 | while(!feof(confPtr) )
2082 | 	{
2083 |  fprintf(tempPtr, "%s %s\n", name, value);
2084 |  fscanf(confPtr, "%s", name);
2085 | 	fgets(value, sizeof(value), confPtr);
2086 | 
2087 |  /*
2088 |   * If the last character of "value" is '\n',
2089 |   * replace it with '\0'.
2090 |   */
2091 | 	if (value[strlen(value) - 1] == '\n')
2092 | 		{
2093 | #ifdef DEBUG
2094 | 		printf("The last character of the value string is %c", value[strlen(value) - 1]);
2095 | 		printf("The value string is %s", value);
2096 | 		printf("Replacing last character of \"%s\" with the NULL character\n",name);
2097 | #endif	/* DEBUG */
2098 | 
2099 | 		value[strlen(value) - 1] = '\0';
2100 | #ifdef DEBUG
2101 | 		printf("The new value string is %s", value);
2102 | #endif	/* DEBUG */
2103 | 		}
2104 | 
2105 | 
2106 | 	/*
2107 | 	 * If we read the variable that we want to change,
2108 |   * replace the value of this variable in the config
2109 |   * file with the value supplied from the keyboard.
2110 |   *
2111 |   */
2112 | 	if ( strcmp(name, dictionary[dictSymbol].varName) == 0)
2113 | 		{
2114 | 		strcpy(value, newValue);
2115 | 
2116 | #ifdef DEBUG
2117 | 		printf("The replacement string is %s", value);
2118 | #endif	/* DEBUG */
2119 | 		}
2120 | 
2121 | 	/*
2122 | 	 * Flush the pointer to the test config file.
2123 | 	 */
2124 | 	fflush(confPtr);
2125 | 
2126 | 	}				
2127 |  /* 
2128 | 	 * Here ends the loop that writes the config file, with the
2129 |   * new variable, to the temporary file.
2130 | 	 */
2131 | 
2132 | /*
2133 |  *
2134 |  * While !(the record to be updated)
2135 | 	*	BEGIN
2136 | 	*  Write the record to the temporary file
2137 |  *  Read the next record in the config file
2138 |  *	END
2139 |  *
2140 |  * Write the new value to the temporary file
2141 |  * Read the next record in the config file
2142 |  * COMMENT: this is the record to be updated.
2143 |  * COMMENT: discard this record.
2144 |  * 
2145 |  * Read the next record in the config file
2146 | 	*
2147 |  * While !(EOF)
2148 | 	*	BEGIN
2149 | 	*  write the record to the temporary file
2150 |  *  read the next record in the config file
2151 |  *  END
2152 |  *
2153 |  * Close Config file
2154 |  * Close Temporary file
2155 |  *
2156 |  * Open Temporary file for reading
2157 |  * Open Config file for writing
2158 |  *
2159 |  * Read the next record of the Temporary file
2160 |  *
2161 |  * While (!EOF of Temporary file)
2162 | 	*	BEGIN
2163 | 	*  write the record into the Config file
2164 | 	*  read the next record of the Temporary file
2165 |  *  END
2166 |  * 
2167 | 	*	Close Temporary file
2168 |  *  Close Config file
2169 |  *
2170 |  */
2171 | 
2172 | fclose(confPtr);
2173 | fclose(tempPtr);
2174 | 
2175 | /*
2176 | 	* Now, flush the file pointers
2177 |  */
2178 | 	fflush(confPtr);
2179 | 	fflush(tempPtr);
2180 | 
2181 | /*
2182 |  * Open the temporary file for reading.
2183 | 	* Open the config file for writing.
2184 |  * Write the contents of the temporary file
2185 |  * into the config file.
2186 |  */
2187 | 
2188 | /*
2189 | 	* Open the temporary file for reading .....
2190 |  */
2191 | if ((tempPtr = fopen(tempFile, "r")) == NULL)
2192 | 	{
2193 | 	printf("File \"%s\" could not be opened for reading.\n", tempFile);
2194 |  exit (51);
2195 |  }
2196 | 
2197 | /*
2198 | 	* Open the config file for writing .....
2199 |  */
2200 | if ((confPtr = fopen(testFile, "w")) == NULL)
2201 | 	{
2202 | 	printf("File \"%s\" could not be opened for writing.\n", testFile);
2203 |  exit (51);
2204 |  }
2205 | 
2206 | /*
2207 |  * Read the first record in the temporary file.
2208 |  */
2209 |  
2210 |  fscanf(tempPtr, "%s", name);
2211 |  fgets(value, sizeof(value), tempPtr);
2212 | #ifdef DEBUG
2213 | 	printf("\nFIRST LINE: %s %s", name, value);
2214 | #endif /* DEBUG */
2215 |  
2216 | /*
2217 | 	* While there are records to be read in the temporary file:
2218 | 	* 		Write the current record into the test config file.
2219 |  * 		Read the next record in the temporary file.
2220 |  * Repeat untill the EOF has been reached.
2221 |  */
2222 | 	
2223 | while(!feof(tempPtr) )
2224 | 	{
2225 |  fprintf(confPtr, "%s %s", name, value);
2226 |  fscanf(tempPtr, "%s", name);
2227 | 	fgets(value, sizeof(value), tempPtr);
2228 |  }
2229 | 
2230 | fclose(confPtr);
2231 | fclose(tempPtr);
2232 | unlink(tempFile);
2233 | 
2234 | return(0);
2235 | }
2236 | 
2237 | 
2238 | int ca_getStorageLocation(char *confVar, dict_t woordenboek[], int size)
2239 | /*************************************************************
2240 |  * ca_getStorageLocation()												*
2241 | 	*	- takes the name of a config variable and searches the  	*
2242 |  *    dictionary structure for the storage location for this *
2243 |  * 	  variable.																*
2244 |  *																				*
2245 |  * Parameters																*
2246 | 	*	confVar -- the string variable that contains the name    *
2247 |  *        	  of the variable.										*	
2248 |  *  woordenboek -- the dictionary structure to be searched	*
2249 | 	*	size			-- the size of the dictionary structure to	*
2250 |  *                 searched.											*
2251 |  *																				*
2252 |  * Returns																	*
2253 | 	*	the location (integer) in the values array.					*
2254 |  *																				*
2255 | 	*************************************************************/
2256 | {
2257 | int i, 
2258 | where,
2259 | found = 0	;		/* Whether or not the symbol has been found. */
2260 | 
2261 | 
2262 | #ifdef DEBUG
2263 | printf("The variable name in ca_getStorageLocation is: %s\n", confVar);
2264 | #endif /* DEBUG */
2265 | 
2266 | /*
2267 |  * Compares each name in the dictionary with the one for which
2268 |  * we are looking.
2269 |  */
2270 | i = 0;
2271 | while (!found && i <= size)
2272 | 	{
2273 | 	if (strcmp(woordenboek[i].varName, confVar) == 0)
2274 | 		{
2275 | 		found = 1;
2276 | 		}
2277 | 	else
2278 | 		{
2279 | 		++i;
2280 | 		}
2281 | 	}
2282 | 
2283 | /*
2284 | 	* Returns the storage location for the given variable name
2285 |  * or else returns NOT_FOUND
2286 |  */
2287 | if (found)
2288 | 		{
2289 | 		/*	mySymbol = atoi(woordenboek[i].varSym);	*/
2290 |     printf("Symbol is %s\n", woordenboek[i].varSym);
2291 | 		printf("Storage Location is: %d\n", woordenboek[i].varNum);
2292 | 	   where = woordenboek[i].varNum; 
2293 | 		}
2294 | else
2295 | 		{
2296 | 		fprintf(stderr, "Error: cannot find storage location for variable %s\n", confVar);	
2297 | 		where = NOT_FOUND;
2298 | 		}
2299 | 	return (where);
2300 | 
2301 | }
2302 | 
2303 | 
2304 | void ca_getConfig(values_t confVars[], int size)
2305 | /*************************************************************
2306 |  * ca_getConfig -- prints the strings representing the 		*
2307 |  * 					   values of the configuration variables		*
2308 |  *																				*
2309 |  * Parameters																*
2310 | 	*		confVars -- the values_t array which stores the 		*
2311 | 	*						values of the configuration variables.	   *
2312 | 	*		size -- the number of configuration variables,			*
2313 | 	*		        the number of elements in the confVars array  *
2314 |  *																				*
2315 |  *																				*
2316 |  *************************************************************/
2317 | {
2318 | int i = 0; 	/* A counting variable. */
2319 | 
2320 | puts("A dump of the strings of the values of the Config Vars:");
2321 | puts("Number\t\tString");
2322 | puts("----------");
2323 | 
2324 | while (i < size)
2325 | 	{
2326 | 	printf("%d\t\t%s\n", i, confVars[i].strPtr);
2327 |  ++i;
2328 | 	}
2329 | 
2330 | }
2331 | 
2332 | 
2333 | int ca_getType(char *confVar, dict_t woordenboek[], int size)
2334 | /****************************************************************
2335 | 	* ca_getType -- returns the data type of the variable.			*
2336 |  *																					*
2337 |  * Parameters																	*
2338 |  *		confVar -- the name of the configuration variable.			*
2339 | 	*		woordenboek -- the array of dict_t structures.				*
2340 | 	*		size -- the number of configuration variables.				*
2341 |  *																					*
2342 |  * Returns																		*
2343 |  *		an integer representing the data type of the variable		*
2344 |  *																					*
2345 |  ****************************************************************/
2346 | {
2347 | int i = 0,		/* Counter variable. */
2348 | 		found = 0;	/* Set this == 1 when we find the variable.  */
2349 | int myType;	/* Integer representing the type of the config variable. */
2350 | 
2351 | /*
2352 |  * Compare each name in the dictionary with the one for which we
2353 |  * are looking.
2354 |  */
2355 |  
2356 | myType = 0;
2357 | 
2358 | #ifdef DEBUG
2359 | printf("ca_getType function called for variable: %s\n", confVar);
2360 | #endif	/* DEBUG */
2361 | 
2362 | while (!found && i <= size)
2363 | 		{
2364 | 		if (strcmp(woordenboek[i].varName, confVar) == 0)
2365 | 			{
2366 | 			found = 1;
2367 | #ifdef DEBUG	
2368 | printf("ca_getType function: %s, %s matched.\n", woordenboek[i].varName, confVar);
2369 | #endif	/* DEBUG */
2370 | 			}
2371 | 		else
2372 | 			{
2373 | 			++i;
2374 | 			}
2375 | 		}			
2376 | 
2377 | /*
2378 | 	* Return the type of the config variable or
2379 | 	* else return "NOT FOUND".
2380 |  */
2381 | if (found)
2382 | 		{
2383 | 			if(strcmp(woordenboek[i].varType, "CA_INT") == 0)
2384 | 				{
2385 | #ifdef DEBUG
2386 | printf("ca_getType function: %s variable of type %s is Integer type\n", woordenboek[i].varName, woordenboek[i].varType);
2387 | 
2388 | printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
2389 | #endif /* DEBUG */
2390 | 				myType = 11;
2391 | 				printf("For type CA_INT, myType is %d\n", myType);
2392 | #ifdef DEBUG
2393 | printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
2394 | #endif /* DEBUG */
2395 | 				}
2396 | 			else
2397 | 				{
2398 | 					if(strcmp(woordenboek[i].varType, "CA_STRING") == 0)
2399 | 						{
2400 | #ifdef DEBUG
2401 | printf("ca_getType function: %s variable of type %s is String type\n", woordenboek[i].varName, woordenboek[i].varType);
2402 | printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
2403 | #endif /* DEBUG */
2404 | 						myType = 12;
2405 | #ifdef DEBUG
2406 | printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
2407 | #endif /* DEBUG */
2408 | 						}
2409 | 					else
2410 | 						{
2411 | 						if (strcmp(woordenboek[i].varType, "CA_DIRLIST") == 0 )
2412 | 							{
2413 | #ifdef DEBUG
2414 | printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
2415 | #endif /* DEBUG */
2416 | 							myType = 13;
2417 | #ifdef DEBUG
2418 | printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
2419 | #endif /* DEBUG */
2420 | 							}
2421 | 						else
2422 | 							{
2423 | 							if (strcmp(woordenboek[i].varType, "CA_BOOLEAN") == 0)
2424 | 								{
2425 | #ifdef DEBUG
2426 | printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
2427 | #endif /* DEBUG */
2428 | 								myType = 14;
2429 | #ifdef DEBUG
2430 | printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
2431 | #endif /* DEBUG */
2432 | 								}
2433 | 							else
2434 | 								{
2435 | 								if (strcmp(woordenboek[i].varType, "CA_SOURCETYPE") == 0)
2436 | 									{	
2437 | #ifdef DEBUG
2438 | printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
2439 | #endif /* DEBUG */
2440 | 									myType = 15;
2441 | #ifdef DEBUG
2442 | printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
2443 | #endif /* DEBUG */
2444 | 									}
2445 | 								}
2446 | 							}
2447 | 						}
2448 | 				}
2449 | 		}
2450 | 	else
2451 | 		{
2452 | 		myType = NOT_FOUND;
2453 | 		}
2454 | 	return(myType);
2455 |  }