## modules/ca/ca_configFns.c

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. stringPack
2. ca_populateDictionary
3. opSplitsen
4. ca_readConfig
5. ca_getDictionary
6. ca_get_int
7. ca_get_dirlist
8. ca_get_string
9. ca_get_boolean
10. ca_set_int
11. ca_change_int_value
12. testFunction
13. ca_getDatabase
14. ca_getSource
15. ca_getAllSources
16. ca_getAsource
17. ca_getSourceDetails
18. ca_get_SourceHandleByPosition
19. ca_get_SourceHandleByName
20. ca_srchandle2Strelement
21. ca_srchandle2Intelement
22. ca_get_adminStrElement
23. ca_get_adminIntElement
24. ca_malloc
25. ca_set_boolean
26. ca_set_dirlist
27. ca_set_string
28. ca_writeNewValue
29. ca_getStorageLocation
30. ca_getConfig
31. ca_getType
32. ca_get_UpdSourceHandle
33. ca_UpdSrcHandle2StrElement
34. ca_UpdSrcHandle2IntElement

       1 /***************************************
       2   $Revision:
       3
       4   CA module: definitions of most functions.
       5
       6   Status: NOT REVIEWED, NOT TESTED
       7
       8   Author(s):       Ambrose Magee
       9
      10   ******************/ /******************
      11   Modification History:
      12
      13   ******************/
      14
      15 /************************************
      16  Copyright (c) 2000                              RIPE NCC
      17
      18 All Rights Reserved
      19
      20 Permission to use, copy, modify, and distribute this software and its
      21 documentation for any purpose and without fee is hereby granted,
      22 provided that the above copyright notice appear in all copies and that
      23 both that copyright notice and this permission notice appear in
      24 supporting documentation, and that the name of the author not be
      25 used in advertising or publicity pertaining to distribution of the
      26 software without specific, written prior permission.
      27
      28 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      29 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      30 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      31 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      32 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      33 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      34 ***************************************/
      35
      36 #include <stdio.h>
      37 #include <stdlib.h>
      38 #include <glib.h>
      39 #include <string.h>
      40 #include <stubs.h>
      41 #include "ca_defs.h"
      42 #include "ca_configFns.h"
      43 #include <unistd.h>
      44
      45 /* #define DEBUG */
      46
      47 /**********************************************
      48  * This file contains the definitions of all    *
      49  * the functions.                                                                               *
      50         **********************************************/
      51
      52 void stringPack(char *dest, const char *source)
         /* [<][>][^][v][top][bottom][index][help] */
      53 /****************************************************************
      54         * stringPack -- function to rewrite a line of text with only    *
      55  *                                       one blankspace between each word.                                      *
      56  *                                                                                                                                                                      *
      57         * Parameters                                                                                                                                    *
      58         *       dest -- destination character, the character to be                      *
      59  *                        outputted                                                                                                                     *
      60         *       source -- the 'source' character, the original character.       *
      61  *                                                                                                                                                                      *
      62  * Returns                                                                                                                                              *
      63         *               Nothing (may change this to the number of characters            *
      64         *               read or copied).                                                                                                                *
      65  *                                                                                                                                                                      *
      66  ****************************************************************/
      67 {
      68 #ifdef DEBUG
      69 printf("\nInside stringPack function\n");
      70 #endif  /* DEBUG */
      71
      72 /*
      73  * This while loop continues until the NULL character is copied into
      74  * the destination string.  If a tab character is copied into the
      75  * destination string, it is replaced with a blank-space character.
      76  *
      77  * Multiple blank-space and/or tab characters are skipped in the source
      78  * string until any other character is found.
      79  */
      80
      81         while (1)
      82                 {
      83                 *dest = *source;
      84
      85                 if (*dest == '\t')
      86                         (*dest = ' ');
      87
      88                 /* Exit if have copied the end of the string. */
      89                 if (*dest == '\0')
      90                         return;
      91
      92 /*
      93  * If the source character was a blank-space or a tab, move to the next
      94  * source character.  While the source character is a blank-space or a
      95  * tab, move to the next character (i.e. ignore these characters).  When
      96  * any other character is found in the source string, move to the next
      97  * element of the destination string.
      98  *
      99  * Otherwise, simultaneously, move to the next elements of the destination
     100  * and the source strings.
     101  */
     102
     103
     104
     105                 if ( (*source == ' ') || (*source == '\t') )
     106                         {
     107                         ++source;
     108                         while ( (*source == ' ') || (*source == '\t') )
     109                                 {
     110                                 ++source;
     111                                 }
     112
     113                         ++dest;
     114                         }
     115                 else
     116                         {
     117                         ++dest;
     118                         ++source;
     119                         }
     120                 }
     121 }
     122
     123
     124 void ca_populateDictionary(dict_t woordenboek[], int size)
         /* [<][>][^][v][top][bottom][index][help] */
     125
     126 /*******************************************************************
     127  * ca_populateDictionary -- Parses dictionary file, initializes    *
     128  *                                                                      the dictionary structure and writes    *
     129  *                                                                      the file of dictionary symbols,                         *
     130         *                                                                       ca_dictSyms.h                                                                   *
     131         *                                                                                                                                                                               *
     132         * Parameters                                                                                                                                            *
     133         *               woordenboek -- the dictionary to be populated                                   *
     134         *               size -- the total number of variables i.e. the size of the  *
     135  *                                array of dict_t structures.  See D. & D., p.276               *
     136  *                                                                                                                                                                              *
     137  * Returns                                                                                                                                                      *
     138         *               Nothing ?  (may change this later)                                                                      *
     139  *                                                                                                                                                                              *
     140  *******************************************************************/
     141
     142 {
     143 const char *blankLine = "\n";
     144 const char *comment = "#";
     145 char line[120];
     146 char input[120];
     147 int entry = 0;
     148 FILE *dictPtr;
     149 #ifdef DEBUG
     150 int i;
     151 FILE *defnPtr;
     152 #endif /* DEBUG */
     153
     154 gchar **tokens;                         /* Pointer to an array of strings. */
     155
     156 /*
     157         * Try to open the dictionary file for reading.  If it cannot be
     158  * opened, exit with an error.
     159  */
     160 if ( (dictPtr = fopen("dictionary.txt", "r")) == NULL)
     161                 {
     162                 fprintf(stderr, "Error: Unable to open 'dictionary.txt'\n");
     163                 die;
     164                 }
     165
     166
     167         /*
     168          * DEBUG mode only.
     169          *Try to open the definitions file for writing.  If it cannot be
     170   * opened,exit with an error
     171   */
     172 #ifdef DEBUG
     173 if ( (defnPtr = fopen("defs.txt", "w")) == NULL)
     174                 {
     175                 fprintf(stderr, "Error: Unable to open 'defs.txt'\n");
     176                 die;
     177                 }
     178 #endif /* DEBUG */
     179
     180                 /*
     181                  * Read the file one line at a time;
     182                  * if the line begins with a comment, ignore it;
     183                  * otherwise, split each line into tokens;
     184                  * print each token.
     185                  * Assign each token to the appropriate member of
     186                  * the appropriate element of the dictionary array.
     187                  */
     188
     189                 fgets(input, sizeof(input), dictPtr);
     190
     191                 if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0) )
     192
     193                         {
     194                         /*
     195                          * First remove the newline character.
     196                          * Then replace multiple tab and space
     197                          * characters with single space characters.
     198                          */
     199
     200                         /* Remove the newline character, if present.
     201                          * Replace the last character of the string
     202                          * array with with '\0'.
     203                          */
     204
     205                         input[strlen(input) - 1] = '\0';
     206
     207                         /* Now, remove the multiple space and tab
     208                          * characters.
     209                          */
     210
     211                         stringPack(line, input);
     212
     213                         g_strchomp(line); /* Remove trailing w-space. */
     214 #ifdef DEBUG
     215 puts(line);
     216 #endif  /*DEBUG */
     217
     218                         tokens = g_strsplit(line, " ", 0);
     219
     220 #ifdef DEBUG
     221                         for (i = 0; tokens[i] != NULL; i++)
     222                                 printf("tokens[%d] = %s\n", i, tokens[i]);
     223 #endif  /* DEBUG */
     224
     225                         /* We no longer need a variable for scope
     226                          * woordenboek[entry].varScope = atoi(tokens[1]);
     227                          */
     228
     229                         strcpy(woordenboek[entry].varName, tokens[0]);
     230                         strcpy(woordenboek[entry].varSym, tokens[1]);
     231                         strcpy(woordenboek[entry].varType, tokens[2]);
     232                         woordenboek[entry].varNum = entry;
     233
     234        /*
     235                          * DEBUG mode only.
     236                          * Write the dictionary symbol and the entry number
     237                          * to the definitions file.
     238         */
     239                         #ifdef DEBUG
     240                         fprintf(defnPtr, "%s\t%d\n", tokens[1], entry);
     241                         #endif /* DEBUG */
     242
     243                         ++entry;
     244                         g_strfreev( tokens );
     245                         }
     246         /*
     247          * Get the 2nd and subsequent line of the file.
     248          */
     249
     250         fgets(input, sizeof(input), dictPtr);
     251
     252         while(!feof(dictPtr) )
     253         {
     254                 /*
     255                  * Process the line if it is not a comment.
     256                  */
     257
     258                 if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0 ) )
     259                 {
     260                         /*
     261                          * First remove the newline character.
     262                          * Then replace multiple tab and space
     263                          * characters with single space characters.
     264                          */
     265
     266                         /* Remove the newline character, if present.
     267                          * Replace the last character of the string
     268                          * array with with '\0'.
     269                          */
     270
     271                         input[strlen(input) - 1] = '\0';
     272
     273                         /* Now, remove the multiple space and tab
     274                          * characters.
     275                          */
     276
     277                         stringPack(line, input);
     278
     279                         g_strchomp(line); /* Remove trailing w/space. */
     280
     281 #ifdef  DEBUG
     282 puts(line);
     283 #endif  /* DEBUG */
     284                         tokens = g_strsplit(line, " ", 0);
     285
     286 #ifdef DEBUG
     287                         for (i = 0; tokens[i] != NULL; i++)
     288                                 printf("tokens[%d] = %s\n", i, tokens[i]);
     289 #endif  /* DEBUG */
     290
     291                         /*
     292                          * We no longer need to know the scope of a variable
     293                          * woordenboek[entry].varScope = atoi(tokens[1]);
     294         */
     295
     296                         strcpy(woordenboek[entry].varName, tokens[0]);
     297                         strcpy(woordenboek[entry].varSym, tokens[1]);
     298                         strcpy(woordenboek[entry].varType, tokens[2]);
     299                         woordenboek[entry].varNum = entry;
     300
     301                         #ifdef DEBUG
     302                         fprintf(defnPtr, "%s\t%d\n", tokens[1], entry);
     303                         #endif  /* DEBUG */
     304
     305                         ++entry;
     306
     307                         g_strfreev( tokens );
     308                 }
     309                 fgets(input, sizeof(input), dictPtr);
     310         }
     311
     312 fclose(dictPtr);
     313
     314 #ifdef DEBUG
     315 fclose(defnPtr);
     316 #endif /* DEBUG */
     317
     318 }       /* End of ca_populateDictionary() function. */
     319
     320
     321 void opSplitsen (FILE *filePtr, gchar **tokenArray)
         /* [<][>][^][v][top][bottom][index][help] */
     322 /*******************************************************************
     323  * opSplitsen -- reads a file and splits it into  tokens.                               *
     324  *                                                                                                                                                                              *
     325  * Parameters                                                                                                                                           *
     326         *       filePtr -- a text file                                                                                                          *
     327  *  tokenArray -- pointer to an array of strings                                                        *
     328  *                                                                                                                                                                              *
     329  * Returns                                                                                                                                                      *
     330  *      Nothing                                                                                                                                                 *
     331  *                                                                                                                                                                              *
     332  *******************************************************************/
     333 {
     334 /*
     335  * Declaring character constants is safer than using #define.
     336  */
     337
     338 const char *blankLine = "\n";           /* Declared as a string, not a character. */
     339 const char *comment = "#";                      /* Declared as a string. */
     340 char line[99];
     341 char input[99];
     342 #ifdef DEBUG
     343 int lineNo = 0;
     344 int j;
     345 #endif  /* DEBUG */
     346
     347
     348         fgets(input, sizeof(input), filePtr); /* Get the (first) line from the */
     349                                          /* file to which filePtr points. */
     350
     351 #ifdef DEBUG
     352         printf("\nFIRST INPUT >>> %s\n", input);
     353 #endif  /* DEBUG */
     354
     355         /* Compare the first character of the input */
     356         /* to the comment and the newline strings. */
     357
     358         if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0) )
     359
     360
     361
     362                 {
     363                 /* Remove the newline character, if present. */
     364                 /* Replace the last character */
     365                 /* of the string array with '\0'. */
     366
     367                 input[strlen(input) - 1] = '\0';
     368 #ifdef DEBUG
     369 printf("First Input >>> %s\n", input);
     370 #endif /* DEBUG */
     371
     372                 strcpy(line, input);
     373 #ifdef DEBUG
     374 printf("First Line after copy >>> %s\n", line);
     375 #endif  /* DEBUG */
     376
     377                 stringPack(line, input);
     378 #ifdef DEBUG
     379 printf("Line: %s\n", line);
     380 #endif  /* DEBUG */
     381
     382                 g_strchomp(line);
     383 /*              g_strdelimit(line, " ", ':');
     384  *              g_strdelimit(line, "\t", '*');
     385 */
     386
     387 #ifdef DEBUG
     388                 printf("%3d> %s\n", ++lineNo, line);
     389 #endif  /* DEBUG */
     390
     391                 /*
     392                  * g_strsplit() is a GLib function;
     393                  * it returns an array of strings.
     394                  *
     395                  * Here, we split on two spaces, "  ".
     396                  * We set max_tokenArray to be 0.  We want the
     397                  * first token to be the name of the variable
     398                  * and the other tokens to be the value of the variable,
     399                  * qualifiers, etc.
     400                  */
     401
     402                 tokenArray = g_strsplit(line, " ", 0);
     403
     404 #ifdef DEBUG
     405                 for (j = 0; tokenArray[j] != NULL; j++)
     406                         printf("token[%d] = %s\n", j, tokenArray[j]);
     407 #endif  /* DEBUG */
     408
     409                 } /* End of processing the first line, if not commented. */
     410
     411                 /* End of getting the first line. */
     412
     413
     414         /*Get the 2nd line of the file. */
     415         fgets(input, sizeof(input), filePtr);
     416
     417         while(!feof(filePtr) )
     418                 {
     419
     420                         /* Process the line if it is not commented. */
     421                         if ( (strncmp(input, comment, 1) != 0) && (strncmp(input, blankLine, 1) != 0 ) )
     422                         {
     423                 /* Remove the newline character, if present. */
     424                         input[strlen(input) -1] = '\0';
     425 #ifdef DEBUG
     426 printf("Subsequent Input >>> %s\n", input);
     427 #endif  /* DEBUG */
     428
     429                         strcpy(line, input);
     430 #ifdef DEBUG
     431 printf("Subsequent Line after copy >>> %s\n", line);
     432 #endif  /* DEBUG */
     433
     434                         stringPack(line, input);
     435 #ifdef DEBUG
     436 printf("Line: %s\n", line);
     437 #endif  /* DEBUG */
     438
     439                         g_strchomp(line);
     440
     441 #ifdef DEBUG
     442                         printf("%3d> %s\n", ++lineNo, line);
     443 #endif  /* DEBUG */
     444
     445                         /*
     446                          * See the comment above about the maximum
     447                          * number of tokens being set to 0.
     448                          */
     449
     450                         tokenArray = g_strsplit(line, " ", 0);
     451
     452 #ifdef DEBUG
     453                         for (j = 0; tokenArray[j] != NULL; j++)
     454                                 {
     455                                 printf("token[%d] = %s\n", j, tokenArray[j]);
     456                                 /* Can also use puts(tokenArray[j]) here. */
     457                                 }
     458 #endif /* DEBUG */
     459                         } /* Processed uncommented lines. */
     460
     461                 fgets(input, sizeof(input), filePtr);
     462                 } /* Processed the 2nd & subsequent lines of the file. */
     463
     464 } /* End of processing the opened file. */
     465
     466
     467 void ca_readConfig(const char *configFile, values_t confVars[], int size)
         /* [<][>][^][v][top][bottom][index][help] */
     468 /*******************************************************************
     469  *                                                                                                                                                                              *
     470  * ca_readConfig -- parses the config file and writes the values   *
     471  *                                               into memory.                                                                                           *
     472  *                                                                                                                                                                              *
     473  * Parameters                                                                                                                                           *
     474  *              configFile -- the configuration file
     475         *               confVars[] -- the array of values structures                                            *
     476         *               size -- the number of configuration variables                                   *
     477  *                                                                                                                                                                              *
     478  * Returns                                                                                                                                                      *
     479  *              Nothing -- perhaps make this return 0 on successful exit ?      *
     480  *                                                                                                                                                                              *
     481  * Note:        Should we make the name of the config file a global             *
     482         *                       variable ?                                                                                                                              *
     483  *******************************************************************/
     484 {
     485 FILE *confPtr;                  /* Pointer to config file. */
     486 char name[STRLENGTH_M];         /* The name of the config variable */
     487                                                                                 /* 80 characters */
     488 char value[STRLENGTH_XXL];                      /* The value of the variable */
     489                                                                                                 /* 640 characters */
     490 int location;                   /* Storage Location of the variable's value. */
     491 int type;        /* Data type of the variable, represented by an integer. */
     492
     493
     494 const char *blankLine = "\n";  /* Declared as a string, not a character. */
     495 const char *comment = "#";              /* Declared as a string. */
     496
     497 char source[16];                                                                        /* The name of a source. */
     498 char database[STRLENGTH_M];             /* The elements of a database. */
     499                                                                                                 /* 80 characters */
     500
     501 /*
     502         * UPDSOURCE variables: whoisd host, query-port, update-port.
     503  */
     504 char updDetails[STRLENGTH_M];   /* The details of the update host: */
     505                                                                                         /* the name of the qry & upd machine; */
     506                                                                                         /* the query port; */
     507                                                                                         /* the update port. */
     508
     509
     510 gchar **dbcomps;        /* Pointer to an array of strings that represents */
     511                                                         /* the components of a db. */
     512
     513
     514 gchar **updDbcomps;     /* Pointer to an array of strings that */
     515                                                                 /* represents the components of an UPD Source. */
     516
     517 ca_ripadmin_t    *newAdminPtr;  /* A pointer to a new instance of */
     518                                                                                         /* a ca_ripadmin_t variable.    */
     519
     520 ca_database_t *newUpdDbPtr;             /* A pointer to a new instance of */
     521                                                                                         /* ca_database_t, for UPDSOURCE. */
     522
     523 ca_updDbSource_t *newUpdSrc;    /* A pointer to a new instance of */
     524                                                                                         /* ca_updDbSource_t structure. */
     525
     526 /*
     527  * Function Prototype for ca_getStorageLocation()
     528  * We put it here; thus it can only be called from
     529  * within ca_readConfig()
     530  *
     531  * This function finds the location in the values_t array
     532  * where we store pointers to the string value and the actual
     533  * value of the variable.  It returns this location as an
     534  * integer.
     535  *
     536  */
     537 int ca_getStorageLocation(char [], dict_t [], int);
     538
     539 /*
     540         * Function Prototype for ca_getType()
     541         * We put it here so that it can only be called from
     542         * within ca_readConfig()
     543  *
     544  * This function returns the type of the configuration
     545  * variable.  It returns it as a string.
     546  *
     547  */
     548 int ca_getType(char [], dict_t [], int);
     549
     550
     551 #ifdef  DEBUG
     552 printf("\nInside readConfig() function.\n");
     553 printf("Configuration file is: %s\n", configFile);
     554 #endif  /* DEBUG */
     555
     556 /*
     557         * Open the configuration file for reading .....
     558  */
     559 if ( (confPtr = fopen(configFile, "r")) == NULL)
     560                 {
     561                 printf("Error: file %s could not be opened.\n", configFile);
     562                 die;
     563                 }
     564
     565 /*
     566         * Read the first record in the configuration file .....
     567  * We read the _name_ of the variable using fscanf into a
     568  * string array.  We read the _value_ of the variable
     569  * using fgets into an array; thus, we can handle values of
     570  * variables with qualifiers (e.g. SPLIT after DBLIST) and
     571  * values with blank characters (e.g. REPLYBANNER).
     572  */
     573 fscanf(confPtr, "%s", name);
     574 fgets(value, sizeof(value), confPtr);
     575 g_strstrip(value);
     576
     577
     578 /*
     579         *               While there are records to be read in the config file.
     580         *               write the current record into memory,
     581  *     read the next record in the config file
     582  */
     583
     584
     585 while (!feof(confPtr) )
     586         {
     587
     588 /*
     589         * From the variable name, find the dictionary number.
     590  * The dictionary number is defined as the place in the
     591  * values array in which to store the value of the variable.
     592  *
     593  */
     594
     595                 /*
     596                  * Process the line only when/if it is not a comment or
     597                  * a blankline.
     598      */
     599                 if ( (strncmp(name, comment, 1) != 0) && (strncmp(name, blankLine, 1) != 0) )
     600                         {
     601                         /*
     602                  * If the last character of "value" is '\n',
     603         * replace it with '\0'.
     604         */
     605                         if ( value[strlen(value) - 1] == '\n')
     606                                 {
     607                                 value[strlen(value) - 1] = '\0';
     608                                 }
     609
     610                         /*
     611                          * From the variable name, find the element of the values
     612                          * array in which to store the value of the variable.
     613         *
     614                          */
     615                         location = ca_getStorageLocation(name, dictionary, VARS);
     616
     617 #ifdef DEBUG
     618                         printf("The location is: %d\n", location);
     619 #endif  /* DEBUG */
     620
     621                         /*
     622                          * See if the string value has already been stored;
     623                          * if it has, then concatenate the new value to it;
     624                          * if not, then allocate some memory and copy the
     625                          * string into it.
     626                          */
     627
     628                         /*
     629                          * If this variable already exists, it has a non-zero
     630         * value and this 'if' statement returns a "true" value.
     631                          * Otherwise, it returns a "zero" or "false" value.
     632                          */
     633                         if (confVars[location].strPtr)
     634                                 {
     635                                 strcat(confVars[location].strPtr, "\n");
     636                                 strcat(confVars[location].strPtr, value);
     637                                 }
     638                         else
     639                                 {
     640                         /*
     641                          * Store a pointer to the string that contains the value
     642                          * This is not necessarily the actual value itself.
     643                          * First, we must allocate some memory.
     644                          */
     645                         confVars[location].strPtr = (char *)malloc(STRLENGTH_XXL);
     646                         /*
     647                          * We check the return value of the malloc function .....
     648         */
     649                         if (confVars[location].strPtr == NULL)
     650                                 {
     651                                 fprintf(stderr, "Cannot allocate memory for confVars[location].strPtr\n");
     652                                 die;
     653                                 }
     654                         strcpy(confVars[location].strPtr, value);
     655                                 }
     656
     657                         /*
     658                          * Now, store a pointer to the _value_ of the variable.
     659                          * Do this as follows:
     660                          * (a) get the _type_ of the variable
     661                          * (b) store a pointer to the value of the variable in
     662                          *     a way that depends on the _type_ of the variable.
     663         */
     664 #ifdef DEBUG
     665 printf("Variable \"%s\" is data-type \"%d\"\n", name, ca_getType(name, dictionary, VARS) );
     666 #endif /* DEBUG */
     667
     668
     669 type = ca_getType(name, dictionary, VARS);
     670
     671                         /*
     672                          * Given the _type_ of the variable, store the value of the
     673                  * variable in the appropriate way.
     674                          */
     675                         switch(type)
     676                                 {
     677                                 case 11:
     678
     679 #ifdef DEBUG
     680                                 puts("Data type is Integer");
     681 #endif  /* DEBUG */
     682
     683                                 confVars[location].valPtr = malloc(sizeof(int) );
     684                                 if (confVars[location].valPtr == NULL)
     685                                         {
     686                                         fprintf(stderr, "Cannot allocate memory !!!\n");
     687                                         die;
     688                                         }
     689                                 sscanf(value, "%d", (int *) confVars[location].valPtr);
     690                                 break;
     691
     692                                 case 12:
     693
     694 #ifdef DEBUG
     695                                 puts("Data type is String !!! *** !!!");
     696 #endif  /* DEBUG */
     697
     698
     699                                 /*
     700                                  * Test if this variable has already been created.
     701                                  * Look for a non-zero i.e. true value.
     702                                  *
     703                                  * First put a '\n' character at the end of the existing
     704            * string.
     705                                  * Then, concatenate the additional string.
     706                                  */
     707                                 if (confVars[location].valPtr)
     708                                         {
     709 #ifdef DEBUG
     710                                         printf("\n%s variable already exists\n", name);
     711 #endif /* DEBUG */
     712                                         g_string_append(confVars[location].valPtr, value);
     713                                         g_string_append(confVars[location].valPtr, "\n");
     714                                         }
     715                                 else
     716                                         {
     717                                         /*
     718                                          * If the variable has not already been created,
     719                                          * then create it.
     720                                          */
     721 #ifdef DEBUG
     722                                         printf("\n%s variable does not exist\n", name);
     723 #endif  /* DEBUG */
     724
     725                                         /*
     726                                          * We use g_string_new() to create a new GString.
     727                                          * This is a _structure_ of str and len.  The actual
     728                                          * string is stored in the str component.  Thus, when
     729                                          * we want to access the string, we must look into
     730                                          * structure.
     731                                  */
     732                                         confVars[location].valPtr = g_string_new(value);
     733                                         g_string_append(confVars[location].valPtr, "\n");
     734                                         }
     735
     736                                 break;
     737
     738                                 case 13:
     739 #ifdef DEBUG
     740                                 puts("Data type is Dirlist");
     741 #endif  /* DEBUG */
     742                                 confVars[location].valPtr = (char *)malloc(STRLENGTH);
     743                                 if (confVars[location].valPtr == NULL)
     744                                         {
     745                                         fprintf(stderr, "Cannot allocate memory !!!\n");
     746                                         die;
     747                                         }
     748                                  strcpy(confVars[location].valPtr, value);
     749                                 break;
     750
     751                                 case 14:
     752 #ifdef DEBUG
     753                                 puts("Data type is Boolean");
     754 #endif  /* DEBUG */
     755
     756                                 /*
     757                                  * confVars[location].valPtr = (char *)malloc(2);
     758            */
     759
     760                                 confVars[location].valPtr = malloc(sizeof(int) );
     761                                 if (confVars[location].valPtr == NULL)
     762                                         {
     763                                         fprintf(stderr, "Cannot allocate memory !!!\n");
     764                                         die;
     765                                         }
     766                                  /*
     767                                   * strcpy(confVars[location].valPtr, value);
     768                                   */
     769                                  sscanf(value, "%d", (int *) confVars[location].valPtr);
     770                                 break;
     771
     772
     773                                 case 16:
     774 #ifdef DEBUG
     775 puts("Found the CA_ADMIN stuff !!!");
     776 #endif  /* DEBUG */
     777                                 /* The elements of the Admin-DB have already been read in. */
     778                                 /* Now, split up the elements and assign them to the */
     779                         /* components of the Admin-DB structure. */
     780                                 /* First, separate the values in "value", using ',' as a */
     781                                 /* delimiting character.        */
     782                                 dbcomps = g_strsplit(value, ",", 0);
     783
     784 #ifdef DEBUG
     785 for (i = 0; dbcomps[i] != NULL; i++)
     786                 printf("dbcomps[%d] = %s\n", i, dbcomps[i]);
     787 #endif  /* DEBUG */
     788
     789                                 /*
     790                                  * Now, allocate some memory to the newAdminPtr.
     791                                  */
     792                            newAdminPtr = calloc(1, sizeof(ca_ripadmin_t) );
     793
     794                                 /*
     795                                  * Check that we actually got the memory.
     796                                  */
     797                                 if (newAdminPtr ==NULL)
     798                                         {
     799                                         fprintf(stderr, "Cannot allocate memory to new admin-db structure\n");
     800                                         die;
     801                                         }
     802
     803                                 /*
     804                                  * Now, assign the elements of the dbcomps array to the
     805                                  * appropriate components of the structure to which
     806                                  * newAdminPtr points.
     807                                  */
     808
     809                                 /*
     810                                  * Strip leading and trailing whitespace from dbcomps[0]
     811            */
     812                                 /*
     813                             * g_strstrip( dbcomps[0] );
     814                                  */
     815
     816                                 strcpy(newAdminPtr->host, dbcomps[0]);
     817                                 newAdminPtr->port = atoi(dbcomps[1]);
     818                                 strcpy(newAdminPtr->user, dbcomps[2]);
     819                                 strcpy(newAdminPtr->password, dbcomps[3]);
     820                                 strcpy(newAdminPtr->tableName, dbcomps[4]);
     821
     822                                 g_strfreev(dbcomps);
     823
     824 #ifdef DEBUG
     825 puts("Testing the population of the rip-admin db structure:");
     826 printf("\n%s::%d::%s::%s::%s\n", newAdminPtr->host, newAdminPtr->port, newAdminPtr->user, newAdminPtr->password, newAdminPtr->tableName);
     827 #endif /* DEBUG */
     828
     829                                 /*
     830                                  * Now, assign these values into the correct long-term
     831                                  * storage.
     832                                  */
     833
     834
     835                                 confVars[location].valPtr = (ca_ripadmin_t *)calloc(1, sizeof(ca_ripadmin_t) );
     836
     837
     838                                 /*
     839                                  * Check that we actually got the memory.
     840                                  */
     841                                 if (confVars[location].valPtr == NULL)
     842                                         {
     843                                         fprintf(stderr, "Cannot allocate memory to new admin-db structure\n");
     844                                         die;
     845                                         }
     846
     847                                 memcpy(confVars[location].valPtr, newAdminPtr, sizeof(ca_ripadmin_t));
     848                                 /*
     849                                 strcpy( ((ca_ripadmin_t *)confVars[location].valPtr)->host, newAdminPtr->host);
     850                                 (confVars[location].valPtr)->port = newAdminPtr->port;
     851                                 strcpy( (confVars[location].valPtr)->user, newAdminPtr->user);
     852                                 strcpy( (confVars[location].valPtr)->password, newAdminPtr->password);
     853                                 strcpy( (confVars[location].valPtr)->tableName, newAdminPtr->tableName);
     854                                 */
     855
     856           free(newAdminPtr);
     857 #ifdef DEBUG
     858                                 printf("The ripadmin machine is: %s\n", ((ca_ripadmin_t *)confVars[location].valPtr)->host);
     859 #endif  /* DEBUG */
     860
     861                                 break;
     862
     863                                 case 17:
     864                                 /*
     865                                  Found Update_Source variable.
     866                  */
     867 #ifdef DEBUG
     868                                 printf("Found Update_Source variable !!!\n");
     869 #endif  /* DEBUG */
     870
     871 #ifdef DEBUG
     872 puts(name);
     873 puts(value);
     874 #endif  /* DEBUG */
     875
     876                                 /*
     877                                  * Split the value into
     878                                  * DB-name, DB-details, updDetails.
     879                                  * Use blankspace as the delimiter between each of these
     880                                  * variables.
     881                                  */
     882                                 sscanf(value, "%s %s %s", source, database, updDetails);
     883 #ifdef  DEBUG
     884 puts(source);
     885 puts(database);
     886 puts(updDetails);
     887 #endif  /* DEBUG */
     888
     889                                 /*
     890                                  * Using the values in "database",
     891                                  * populate a ca_database_t structure.
     892                                  * Give this variable a name.
     893                                  */
     894
     895                                         /* First, separate the values in "database", using "," as
     896                                          * as a delimiting  character.
     897                                          */
     898                                  dbcomps = g_strsplit(database, ",", 0);
     899
     900 #ifdef DEBUG
     901 for (i = 0; dbcomps[i] != NULL; i++)
     902                 printf("dbcomps[%d] = %s\n", i, dbcomps[i]);
     903 #endif  /* DEBUG */
     904
     905                                         /*
     906                                          * Create a structure for this database.
     907                                          */
     908                                 newUpdDbPtr= calloc(1, sizeof(ca_database_t));
     909
     910                                 /*
     911                                  * Did we get the memory ?  We make some checks.
     912                                  */
     913                                 if (newUpdDbPtr == NULL)
     914                                         {
     915                                         fprintf(stderr, "Cannot allocate memory to new UPD DB structure.\n");
     916                                         die;
     917                                         }
     918
     919                                 strcpy(newUpdDbPtr->host, dbcomps[0]);
     920                                 newUpdDbPtr->port = atoi(dbcomps[1]);
     921                                 strcpy(newUpdDbPtr->user, dbcomps[2]);
     922                                 strcpy(newUpdDbPtr->password, dbcomps[3]);
     923                                 strcpy(newUpdDbPtr->dbName, dbcomps[4]);
     924
     925                                 g_strfreev(dbcomps);
     926
     927 #ifdef DEBUG
     928 puts("Testing the population of the UPD db structure:");
     929 printf("\n%s::%d::%s::%s::%s\n", newUpdDbPtr->host, newUpdDbPtr->port, newUpdDbPtr->user, newUpdDbPtr->password, newUpdDbPtr->dbName);
     930 #endif  /* DEBUG */
     931
     932                         /*
     933                          * Now, store the values contained in the updDetails string.
     934                          */
     935
     936                         /*
     937                          * First, separate the values in the 'updDetails' string, using
     938                          * "," as a delimiting character.
     939                          */
     940                         updDbcomps = g_strsplit(updDetails, ",", 0);
     941
     942 #ifdef DEBUG
     943 for (i = 0; updDbcomps[i] != NULL; i++)
     944                 printf("updDbcomps[%d] = %s\n", i, updDbcomps[i]);
     945 #endif  /* DEBUG */
     946
     947                         /*
     948                          * Using the above ca_database_t structure, the "source"
     949                          * value and the values of updDbcomps, populate the
     950                          * ca_updDbSource_t structure.
     951                          *
     952                          */
     953
     954                                 /*
     955                                  * Create a new structure for this UPD Source.
     956                                  */
     957                         newUpdSrc = calloc(1,sizeof(ca_updDbSource_t));
     958
     959                         if (newUpdSrc == NULL)
     960                                 {
     961                                 fprintf(stderr, "Cannot allocate memory to new source structure\n");
     962                                 die;
     963                                 }
     964
     965 #ifdef DEBUG
     966 puts("Created a structure for the UPD Source variable");
     967 #endif  /* DEBUG */
     968
     969                         /*
     970                          * Now, populate this structure.
     971                          */
     972
     973                         strcpy(newUpdSrc->name, source);
     974                         newUpdSrc->updDb = *newUpdDbPtr;
     975                         strcpy(newUpdSrc->whoisd_host, updDbcomps[0]);
     976                         newUpdSrc->qryPort = atoi(updDbcomps[1]);
     977                         newUpdSrc->updPort = atoi(updDbcomps[2]);
     978
     979                         free(newUpdDbPtr);      /* Was copied */
     980                         g_strfreev(updDbcomps);
     981
     982 #ifdef DEBUG
     983 puts("Testing the population of the ca_updDbSource_t structure:");
     984 printf("Update Source name: %s\n", newUpdSrc->name);
     985 printf("\nUPD-DB == %s::%d::%s::%s::%s\n", (newUpdSrc->updDb).host, (newUpdSrc->updDb).port, (newUpdSrc->updDb).user, (newUpdSrc->updDb).password, (newUpdSrc->updDb).dbName);
     986 printf("\nUpdate Source Machine Details: %s::%d::%d\n", newUpdSrc->whoisd_host, newUpdSrc->qryPort, newUpdSrc->updPort);
     987 #endif  /* DEBUG */
     988
     989                                 /*
     990                                  * Now, assign these values into the correct long-term
     991                                  * storage.
     992                                  */
     993
     994                                 confVars[location].valPtr = (ca_updDbSource_t *)calloc(1, sizeof(ca_updDbSource_t) );
     995
     996                                 /*
     997                                  * Check that we actually got the memory.
     998                                  */
     999                                 if (confVars[location].valPtr == NULL)
    1000                                         {
    1001                                         fprintf(stderr, "Cannot allocate memory to new admin-db structure\n");
    1002                                         die;
    1003                                         }
    1004
    1005                                 memcpy(confVars[location].valPtr, newUpdSrc, sizeof(ca_updDbSource_t) );
    1006
    1007                                 /* No longer needed. */
    1008                                 free(newUpdSrc);
    1009
    1010 #ifdef DEBUG
    1011 printf("UPD-Source/DB-details/user: %s\n", ( ( (ca_updDbSource_t *)confVars[location].valPtr)->updDb).user  );
    1012 #endif  /* DEBUG */
    1013
    1014                                 break;
    1015
    1016                                 default:
    1017                                 fprintf(stderr, "Data type not found for variable \"%s\".\n", name);
    1018                                 die;
    1019                                 break;
    1020                                 }
    1021                         }
    1022
    1023 fscanf(confPtr, "%s", name);
    1024 fgets(value, sizeof(value), confPtr);
    1025 g_strstrip(value);
    1026
    1027         }       /* End of processing the config file. */
    1028
    1029 }       /* End of readConfig() function */
    1030
    1031
    1032
    1033
    1034 void ca_getDictionary(dict_t woordenboek[], int size)
         /* [<][>][^][v][top][bottom][index][help] */
    1035 {
    1036 int k;
    1037
    1038 for (k = 0; k < size; k++)
    1039         {
    1040         printf("\nj = %d\n", k);
    1041  /*
    1042          * printf("%s\t%d\t%s\n", woordenboek[k].varName, woordenboek[k].varScope, woordenboek[k].varType);
    1043          */
    1044          printf("%s\t%s\t%s\t%d\n", woordenboek[k].varName, woordenboek[k].varSym, woordenboek[k].varType, woordenboek[k].varNum);
    1045
    1046         }
    1047 }
    1048
    1049
    1050 int ca_get_int(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    1051 {
    1052 int *xPtr;
    1053
    1054                 /*
    1055                  * First print a message saying that the ca_get_int()
    1056                  * function is being called.
    1057                  */
    1058 #ifdef DEBUG
    1059         printf("\nDEBUG: ca_get_int() function is called .....\n");
    1060 printf("DEBUG: New value of StringPtr: %s\n", confVars[symbol].strPtr);
    1061 #endif  /* DEBUG */
    1062
    1063                 /*
    1064                  * Look at the appropriate place in the dictionary;
    1065                  * e.g. C_BINDPORT => the first element, index = 0.
    1066                  *
    1067                  * if the varType is not an integer, exit with an error;
    1068                  *
    1069                  * otherwise, return an integer.
    1070                  *
    1071                  */
    1072
    1073                 /* Look at the appropriate place in the dictionary. */
    1074
    1075 #ifdef DEBUG
    1076                 printf("\nDEBUG: Variable type: %s\n", dictionary[symbol].varType);
    1077 #endif  /* DEBUG */
    1078
    1079                 /* If the variable type is not an integer, exit with an error. */
    1080                 if ( strcmp(dictionary[symbol].varType, "CA_INT") != 0)
    1081                         {
    1082                         fprintf(stderr, "Error: unexpected variable type.\n");
    1083                         die;
    1084                         }
    1085                 else
    1086                         {
    1087                         /*
    1088                          * Lock the value of the variable before reading it.
    1089                          */
    1090
    1091                         pthread_mutex_lock(&Lock);
    1092
    1093                         xPtr = confVars[symbol].valPtr;
    1094                         /*
    1095                          * Unlock the value of the variable after reading it.
    1096                          */
    1097                         pthread_mutex_unlock(&Lock);
    1098                         }
    1099
    1100                         if (xPtr == NULL)
    1101                                 {
    1102                                 printf("Error: undefined integer variable: %s\n ", dictionary[symbol].varName);
    1103
    1104                                 die;
    1105                                 }
    1106                         return(*xPtr);
    1107 }
    1108
    1109 char *ca_get_dirlist(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    1110 {
    1111 /*
    1112  * This function returns a pointer to a character array.  Thus,
    1113  * we need to declare such a pointer.
    1114  *
    1115  */
    1116
    1117 char *xPtr;
    1118 #ifdef  DEBUG
    1119         printf("\nca_get_dirlist() function is called .....\n");
    1120 #endif  /* DEBUG */
    1121
    1122
    1123                 /*
    1124                  * Look at the appropriate place in the dictionary;
    1125                  * e.g. CA_HELP => the second element, index = 1.
    1126                  *
    1127                  * if the varType is not CA_DIRLIST, exit with an error;
    1128                  *
    1129                  * otherwise, return a pointer to the value.
    1130                  *
    1131                  */
    1132
    1133                 /* Look at the appropriate place in the dictionary. */
    1134                 #ifdef DEBUG
    1135                 printf("\nVariable type: %s\n", dictionary[symbol].varType);
    1136                 #endif  /* DEBUG */
    1137
    1138                 /* If the variable type is not CA_DIRLIST, exit with an error. */
    1139                 if ( strcmp(dictionary[symbol].varType, "CA_DIRLIST") != 0)
    1140                         {
    1141                         fprintf(stderr, "Error: unexpected variable type.\n");
    1142                         die;
    1143                         }
    1144                 else
    1145                         {
    1146                                 pthread_mutex_lock(&Lock);
    1147                                 /*
    1148                                  * Test if a value for this variable has been defined.  If
    1149                                  * yes, return a copy of it.  If not, print an error message
    1150                                  * and die.
    1151                                  */
    1152                                 if (confVars[symbol].valPtr)
    1153                                         {
    1154                                         xPtr = (strdup(confVars[symbol].valPtr));
    1155                                         #ifdef DEBUG
    1156                                         printf("Value: %s\n", xPtr);
    1157                                 #       endif   /* DEBUG */
    1158                                         }
    1159                                 else
    1160                                         {
    1161                                         printf("Error: undefined DIRLIST variable: %s\n", dictionary[symbol].varName);
    1162                                         die;
    1163                                         }
    1164                                 pthread_mutex_unlock(&Lock);
    1165                         }
    1166                 return(xPtr);
    1167 }
    1168
    1169
    1170 char *ca_get_string(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    1171 {
    1172 /*
    1173  * This function returns a pointer to a character array.  Thus,
    1174  * we need to declare such a pointer.
    1175  *
    1176  */
    1177
    1178 char *xPtr;
    1179 #ifdef  DEBUG
    1180         printf("\nca_get_text() function is called .....\n");
    1181 #endif  /* DEBUG */
    1182
    1183
    1184                 /*
    1185                  * Look at the appropriate place in the dictionary;
    1186                  * e.g. CA_REPLYBANNER => the third element, index = 2.
    1187                  *
    1188                  * if the varType is not CA_STRING, exit with an error;
    1189                  *
    1190                  * otherwise, return the value.
    1191                  *
    1192                  */
    1193
    1194                 /* Look at the appropriate place in the dictionary. */
    1195
    1196 #ifdef DEBUG
    1197                 printf("\nVariable type: %s\n", dictionary[symbol].varType);
    1198 #endif  /* DEBUG */
    1199
    1200                 /* If the variable type is not CA_STRING, exit with an error. */
    1201                 if ( strcmp(dictionary[symbol].varType, "CA_STRING") != 0)
    1202                         {
    1203                         fprintf(stderr, "Error: unexpected variable type.\n");
    1204                         die;
    1205                         }
    1206                 else
    1207                         {
    1208                                 pthread_mutex_lock(&Lock);
    1209
    1210                                 /*
    1211                                  * Test if a value for this variable has been defined.  If
    1212                                  * yes, return a copy of it.  If not, return a NULL pointer.
    1213                                  */
    1214                                 if (( (GString *)confVars[symbol].valPtr) )
    1215                                         {
    1216                                         xPtr = (strdup(( (GString *)confVars[symbol].valPtr)->str) );
    1217                                         #ifdef DEBUG
    1218                                         printf("Value: %s\n", xPtr);
    1219                                         #endif  /* DEBUG */
    1220                                         }
    1221                                 else
    1222                                         {
    1223                                         #ifdef DEBUG
    1224                                         printf("STRING value is undefined !!!\n");
    1225                                         #endif /* DEBUG */
    1226                                         xPtr = NULL;
    1227                                         }
    1228                                 pthread_mutex_unlock(&Lock);
    1229                         }
    1230                 return(xPtr);
    1231 }
    1232
    1233
    1234 int ca_get_boolean(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    1235 {
    1236 /**********************************************
    1237  * ca_get_boolean()                                                                     *
    1238         *                                                                                                                       *
    1239         *                                                                                                                       *
    1240         * Parameters                                                                                    *
    1241         *                                                                                                                       *
    1242         *       symbol -- the symbol for the variable           *
    1243  *                                                                                                                      *
    1244         *                                                                                                                       *
    1245         * Returns                                                                                               *
    1246         *                                                                                                                       *
    1247         *       1 if true, 0 if false.                                                  *
    1248  *                                                                                                                      *
    1249  * Remarks                                                                                              *
    1250         *                                                                                                                       *
    1251  *   Is there a better way to implement                 *
    1252         *   Boolean values in C ?                                                       *
    1253  *                                                                                                                      *
    1254         *********************************************/
    1255
    1256 int *xPtr;
    1257
    1258 /*
    1259  * Print this message if in debug mode.
    1260  *
    1261  */
    1262 #ifdef DEBUG
    1263         printf("\nca_get_boolean() function is called .....\n");
    1264 printf("DEBUG 5: New value of StringPtr: %s\n", globals[symbol].strPtr);
    1265 #endif  /* DEBUG        */
    1266
    1267 /**********************************************\
    1268  *                                                                                                                      *
    1269         * Here is how this works:                                                       *
    1270  *                                                                                                                      *
    1271  * (a) Check that the type of variable whose    *
    1272  *     value is being read is CA_BOOLEAN.               *
    1273  *                                                                                                                      *
    1274  * (b) Lock the value of the variable before    *
    1275  *              reading it.                                                                             *
    1276  *                                                                                                                      *
    1277  * (c) Depending on the scope of the variable   *
    1278  *     look for it in the appropriate array.    *
    1279  *                                                                                                                      *
    1280         * (d) Read the value of the variable.                   *
    1281  *                                                                                                                      *
    1282         * (e) Unlock the value of the variable after *
    1283  *              reading it.                                                                             *
    1284         *                                                                                                                       *
    1285  *                                                                                                                      *
    1286  * Returns                                                                                              *
    1287         *
    1288         *       an integer value as follows:                                    *
    1289         *               1 if the db is in testmode (true),                                                      *
    1290         *               0 if the db is not in testmode (false).                                 *
    1291 \*********************************************/
    1292
    1293
    1294 /*
    1295         * Look at the appropriate place in the dictionary;
    1296         * e.g. CA_BOOLEAN = the fifth element of the dict_t array,
    1297  * => index = 4.
    1298  *
    1299  * If the varType is not Boolean, exit with an error
    1300         *
    1301  * Otherwise,
    1302         *
    1303  */
    1304
    1305 #ifdef DEBUG
    1306 /* Look in the appropriate place in the dictionary. */
    1307 printf("\nVariable type: %s\n", dictionary[symbol].varType);
    1308 #endif  /* DEBUG */
    1309
    1310 /* If the variable type is not Boolean, exit with an error. */
    1311
    1312         if ( strcmp(dictionary[symbol].varType, "CA_BOOLEAN") != 0)
    1313                 {
    1314                 fprintf(stderr, "Error: Boolean type expected.\n");
    1315                 die;
    1316                 }
    1317
    1318         else
    1319                 {
    1320
    1321                 /*
    1322                  * Otherwise, return an integer value.
    1323                  *
    1324      */
    1325
    1326                 /*
    1327                  * Lock the value of the variable before reading it.
    1328                  *
    1329                  */
    1330
    1331                 pthread_mutex_lock(&Lock);
    1332                 xPtr = confVars[symbol].valPtr;
    1333                 /*
    1334                  * Unlock the value of the variable after reading it.
    1335                  */
    1336                 pthread_mutex_unlock(&Lock);
    1337
    1338         }
    1339 if (xPtr == NULL)
    1340         {
    1341         printf("Undefined Boolean variable: %s\n", dictionary[symbol].varName);
    1342         die;
    1343         }
    1344 return(*xPtr);
    1345 }
    1346
    1347
    1348
    1349 void ca_set_int(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    1350 {
    1351         /*********************************************
    1352          * ca_set_int()                                                                         *
    1353   *                                                                                                             *
    1354          * Parameters                                                                                   *
    1355          *              symbol -- the symbol for the variable.  *
    1356          *                                                                                                                      *
    1357          * Returns                                                                                              *
    1358          *              1 if successful 0 if not ?                                      *
    1359          *                                                                                                                      *
    1360          * Remarks                                                                                              *
    1361          *      Needs a better way to check for valid  *
    1362   *    values from the keyboard.                                        *
    1363          *                                                                                                                      *
    1364          *********************************************/
    1365
    1366         /* void *tempPtr; */            /* Temp pointer to point to the value pointer
    1367                                                                 in the appropriate values array. */
    1368  char newPort[16];
    1369  int invalid;
    1370  int portNr;
    1371
    1372  /* Function to change the value in a given values array.
    1373   * This function can only be called from within ca_set_int().
    1374          */
    1375  int *ca_change_int_value(char []);
    1376  void testFunction(values_t values[]);
    1377
    1378         /*
    1379   * Using the symbol, look at the appropriate place in the
    1380          * dictionary.
    1381          */
    1382 #ifdef DEBUG
    1383  printf("\nca_set_int() function called .....\n");
    1384         printf("Variable type: %s\n", dictionary[symbol].varType);
    1385 #endif  /* DEBUG */
    1386
    1387
    1388 /*
    1389  * Make sure that a reasonable, sensible value of bind-port has
    1390  * been read from the keyboard.
    1391  */
    1392
    1393 do      {
    1394
    1395                 /*
    1396                  * First, flush input stream.
    1397      */
    1398                 fflush(stdin);
    1399
    1400                 /*
    1401                  * Prompt for the new value of the bind-port.
    1402                  */
    1403
    1404                 printf("\nNew value of bind-port (non-zero positive integer) >>> ");
    1405                 scanf("%s", newPort);
    1406                 /*
    1407                  * gets(newPort);
    1408                  */
    1409 #ifdef DEBUG
    1410                 printf("\nDEBUG: Value of newPort variable: %s\n", newPort);
    1411 #endif  /* DEBUG */
    1412
    1413                 sscanf(newPort, "%d", &portNr);
    1414
    1415 #ifdef DEBUG
    1416                 printf("\nDEBUG: Value of integer variable, portNr: %d\n", portNr);
    1417 #endif  /* DEBUG */
    1418
    1419                         if (portNr < 0)
    1420                                 {
    1421                                 invalid = 1;
    1422                                 puts("Only non-zero positive integer values accepted for bind-port");
    1423                                 }
    1424                         else
    1425                                 {
    1426                                 invalid = 0;
    1427                                 }
    1428
    1429                 } while(invalid);
    1430
    1431  /*
    1432          * Check that the function is attempting to set the correct type
    1433          * of value.  If not, do not set the value and exit.
    1434          */
    1435
    1436         if (strcmp(dictionary[symbol].varType, "CA_INT") != 0)
    1437                 {
    1438                 fprintf(stderr, "Error: unexpected variable type.\n");
    1439                 die;
    1440                 }
    1441
    1442         /*
    1443          * Choose the appropriate values array.
    1444          */
    1445         switch(dictionary[symbol].varScope)
    1446                 {
    1447                 /* If the variable has global scope,
    1448                  * write it into the globals array.
    1449                  * If it has local scope,
    1450                  * write it into the local array.
    1451                  * If the scope cannot be found, then report an error.
    1452                  */
    1453                 case 1:
    1454                 globals[symbol].valPtr = ca_change_int_value(newPort);
    1455                 globals[symbol].strPtr = newPort;
    1456
    1457                 globals[symbol].strPtr = (char *)calloc(1,sizeof(newPort) );
    1458
    1459                 /* Check the return value of malloc() to make sure that we
    1460                  * actually got the memory.
    1461                  */
    1462                 if (globals[symbol].strPtr == NULL)
    1463                         {
    1464                         fprintf(stderr, "Cannot allocate memory for globals[symbol].strPtr.\n");
    1465                         die;
    1466                         }
    1467 #ifdef DEBUG
    1468                 printf("DEBUG: New value of StringPtr: %s\n", globals[symbol].strPtr);
    1469 #endif  /* DEBUG */
    1470
    1471                 strcpy(globals[symbol].strPtr, newPort);
    1472
    1473 #ifdef DEBUG
    1474                 printf("DEBUG 2: New value of StringPtr: %s\n", globals[symbol].strPtr);
    1475 #endif  /* DEBUG */
    1476                 break;
    1477
    1478                 case 99:
    1479                 locals[symbol].valPtr = ca_change_int_value(newPort);
    1480                 /*
    1481                  * First allocate some memory and then copy the value of the new
    1482                  * Port into it.
    1483      */
    1484                 locals[symbol].strPtr = (char *)calloc(1,sizeof(newPort) );
    1485                 /*
    1486                  * Now, check that the memory was actually allocated.
    1487                  */
    1488                 if (locals[symbol].strPtr == NULL)
    1489                         {
    1490                         fprintf(stderr, "Cannot allocate memory for locals[symbol].strPtr\n");
    1491                         exit(8);
    1492                         }
    1493
    1494                 strcpy(locals[symbol].strPtr, newPort);
    1495                 /*
    1496                  * locals[symbol].strPtr = newPort;
    1497                  */
    1498                 break;
    1499
    1500                 default:
    1501                 fprintf(stderr, "Error; unknown scope: %d\n", dictionary[symbol].varScope);
    1502                 break;
    1503                 }
    1504
    1505  /*
    1506   * Write the new value of the variable to the correct place in
    1507   * this array.  (First, set a mutex lock ???).
    1508          */
    1509
    1510  /*
    1511          * Write the new value of this variable back to the config. file
    1512          */
    1513
    1514 ca_writeNewValue(symbol, newPort);
    1515
    1516                 printf("DEBUG 3: New value of StringPtr: %s\n", globals[symbol].strPtr);
    1517
    1518 }
    1519
    1520 int *ca_change_int_value(char value[])
         /* [<][>][^][v][top][bottom][index][help] */
    1521 {
    1522 void *tempPtr;
    1523
    1524 /*
    1525  * Check the return value of malloc() in case we did not actually get
    1526  * the memory.
    1527  */
    1528 tempPtr = malloc(sizeof(int) );
    1529 if (tempPtr == NULL)
    1530                 {
    1531                 fprintf(stderr, "Cannot allocate memory for tempPtr\n");
    1532                 die;
    1533                 }
    1534
    1535 sscanf(value, "%d", (int *) tempPtr);
    1536 return(tempPtr);
    1537 }
    1538
    1539
    1540
    1541 void testFunction(values_t array[])
         /* [<][>][^][v][top][bottom][index][help] */
    1542 {
    1543         printf("\nInside the Test function.\n");
    1544         }
    1545
    1546
    1547 void ca_getDatabase(ca_database_t db)
         /* [<][>][^][v][top][bottom][index][help] */
    1548 {
    1549 printf("\n%s\t%d\t%s\t%s\t%s\n", db.host, db.port, db.user, db.password, db.dbName);
    1550 }
    1551
    1552 void ca_getSource(ca_database_list_t src)
         /* [<][>][^][v][top][bottom][index][help] */
    1553 {
    1554 printf("\n%s\t%s\t%d\t%s\t%s\t%s\n", src.name, (src.db).host, (src.db).port, (src.db).user, (src.db).password, (src.db).dbName);
    1555 }
    1556
    1557
    1558 void ca_getAllSources(GSList *sources)
         /* [<][>][^][v][top][bottom][index][help] */
    1559 {
    1560
    1561 GSList *currentPtr;     /* Pointer to the structure at which we look. */
    1562
    1563 /*
    1564  * Look at the first member of the linked-list of sources.
    1565  */
    1566 currentPtr = sources;
    1567
    1568 /*
    1569  * Look at each data component of the source list,
    1570  * untill we reach the end of the list.
    1571  */
    1572 while(currentPtr != NULL)
    1573         {
    1574  ca_database_list_t *srcPtr = currentPtr->data;
    1575 printf("\n%s\t%s\t%d\t%s\t%s\t%s\n", srcPtr->name, (srcPtr->db).host, (srcPtr->db).port, (srcPtr->db).user, (srcPtr->db).password, (srcPtr->db).dbName);
    1576  currentPtr = currentPtr->next;
    1577         }
    1578 }
    1579
    1580 void ca_getAsource(char *sourceName, GSList *sources)
         /* [<][>][^][v][top][bottom][index][help] */
    1581 /*******************************************************************
    1582  * ca_getAsource -- looks for a source in the linked list                               *
    1583  *                                                                                                                                                                              *
    1584  * Parameters                                                                                                                                           *
    1585         *       sourceName -- the name of a source for which to look                            *
    1586         *  sources -- the list of sources in which to look                                              *
    1587  *                                                                                                                                                                              *
    1588  * Returns                                                                                                                                                      *
    1589  *  nothing, so far.                                                                                                                            *
    1590  *                                                                                                                                                                              *
    1591  *******************************************************************/
    1592 {
    1593
    1594 GSList *currentPtr = sources;
    1595
    1596 #ifdef DEBUG
    1597 printf("\nLooking for source: %s\n", sourceName);
    1598 #endif  /* DEBUG */
    1599
    1600 /*
    1601  * Look at each data component of the source list,
    1602         * compare the name of the source with the sourceName
    1603  * untill we find the source o we reach the end of the list
    1604  */
    1605 {       /* Begin special block
    1606                  * I got a syntax error when I defined
    1607                  * "ca_database_list_t *srcPtr = currentPtr->data;"
    1608                  * in the usual way, with all the other local variables.
    1609                  *
    1610      * However, if I define it inside this block, I do not
    1611                  * get any syntax errors.
    1612             *
    1613          */
    1614
    1615
    1616 ca_database_list_t *srcPtr = currentPtr->data;
    1617 #ifdef DEBUG
    1618 printf("FirstSource is: %s\n", srcPtr->name);
    1619 #endif  /* DEBUG */
    1620 while( (currentPtr != NULL) && ( strcmp(srcPtr->name, sourceName) != 0 ) )
    1621         {
    1622 #ifdef DEBUG
    1623  puts("Now printing the current source .....");
    1624  printf("CurrentSource is: %s\n", srcPtr->name);
    1625  printf("%d\n", strcmp(srcPtr->name, sourceName) );
    1626  if ( strcmp(srcPtr->name, sourceName) == 0 )
    1627                 {
    1628                 printf("Found it !!! Source: %s\n", srcPtr->name);
    1629                 }
    1630 #endif  /* DEBUG */
    1631  currentPtr = currentPtr->next;
    1632         puts("currentPtr = currentPtr->next");
    1633         if (currentPtr != NULL)
    1634         {
    1635         srcPtr = currentPtr->data;
    1636         puts("srcPtr = currentPtr->data");
    1637  }
    1638  #ifdef DEBUG
    1639                 puts("At the end of the while loop inside ca_getAsource function .....");
    1640         printf("The NewSource is: %s\n", srcPtr->name);
    1641  #endif /* DEBUG */
    1642         }
    1643 #ifdef DEBUG
    1644 puts("Exited from while loop in ca_getAsource function .....");
    1645 #endif /* DEBUG */
    1646
    1647 if (currentPtr != NULL)
    1648         {
    1649  printf("\nFound the source: %s\n", srcPtr->name);
    1650 /* printf("\n%s\t%s\t%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).canupd, (srcPtr->db).deflook, (srcPtr->db).dbName);
    1651 */
    1652  }
    1653 else
    1654         {
    1655         printf("\nCould not find source: %s\n", sourceName);
    1656         }
    1657 }       /* End special block */
    1658
    1659 }
    1660
    1661
    1662 ca_dbSource_t *ca_getSourceDetails(char *sourceName, GSList *sources)
         /* [<][>][^][v][top][bottom][index][help] */
    1663 /*******************************************************************
    1664  * ca_getSourceDetails                                                                                                                  *
    1665  *      -- A function that compares each 'name' component of every              *
    1666  *              ca_database_list_t element in the linked-list of sources                *
    1667         *               (the first element of which is a parameter of this function)*
    1668  *              with the name of the source to be found.  If the required       *
    1669         *               source is found, a pointer to the structure representing        *
    1670  *              this source is returned.                                                                                                *
    1671         *                                                                                                                                                                               *
    1672         *       Parameters                                                                                                                                              *
    1673         *       --      sourceName - the name of the required source                                            *
    1674         *       --      sources - the list of sources in which to look                                  *
    1675         *                                                                                                                                                                               *
    1676  *      Returns                                                                                                                                                 *
    1677         *       -- srcPtr - a pointer to the structure representing the source  *
    1678         *            - or a pointer to NULL, if we cannot find the source *
    1679  *                                                                                                                                                                              *
    1680  *******************************************************************/
    1681 {
    1682 /*
    1683         * Define a pointer to the current element in the linked list.
    1684  * Initialise it to the start of the list;
    1685  */
    1686 GSList *currentPtr = sources;
    1687
    1688         /*
    1689                 * Define and initialise a pointer that points to the 'data'
    1690          * component of the GSList struct; i.e. a pointer to a
    1691          * variable of type ca_dbSource_t.
    1692          */
    1693 ca_dbSource_t *srcPtr = currentPtr->data;
    1694
    1695
    1696         /*
    1697                 * Look at each data component of list of sources;
    1698                 * (each data component is a structure of type ca_dbSource_t
    1699                 * i.e. ca_database_list_t).  Compare the 'name' component of
    1700          * of each ca_dbSource_t structure with the value of sourceName
    1701          * untill we get a match or we reach the end of the list.
    1702                 */
    1703         /*
    1704          * We first check if currentPtr is pointing to NULL;
    1705          *              if yes, we exit the while loop;
    1706          *      if no, we make srcPtr point to the data component
    1707          *    of the current dbSource structure;
    1708          *                      then, we check if this is the source name that we want;
    1709          *                      if yes, we _break_ from the while loop.
    1710  */
    1711         while (currentPtr != NULL)
    1712                 {
    1713                 srcPtr = currentPtr->data;
    1714         if (strcmp(srcPtr->name, sourceName) == 0 )
    1715                         break;
    1716                 currentPtr = currentPtr->next;
    1717                 }
    1718
    1719         /*
    1720                 * We return a pointer.  If we found the source, this pointer points
    1721          * to the ca_dbSource_t structure which represents the source.
    1722          * If we did not find the source, we return a pointer to NULL.
    1723          */
    1724         if (currentPtr == NULL)
    1725                 {
    1726                 srcPtr = NULL;
    1727                 return(srcPtr);
    1728                 }
    1729  else
    1730                 {
    1731                 return(srcPtr);
    1732                 }
    1733
    1734 }       /* End of ca_getSourceDetails function */
    1735
    1736
    1737 ca_SrcHdl_t *ca_get_SourceHandleByPosition(int position)
         /* [<][>][^][v][top][bottom][index][help] */
    1738 /*******************************************************************
    1739  * ca_get_SourceHandleByPosition                                                                                                *
    1740  *      -- retrieves the a handle to a Source                                                                   *
    1741  *                                                                                                                                                                              *
    1742  * Parameters                                                                                                                                           *
    1743  *      -- the position in the linked list of sources                                           *
    1744  *                                                                                                                                                                              *
    1745  *                                                                                                                                                                              *
    1746         * Returns                                                                                                                                                       *
    1747         *       -- a pointer to the source or NULL                                                                              *
    1748  *              i.e. a pointer to the data component of the appropriate         *
    1749  *              element in the linked list of sources.                                                          *
    1750  *******************************************************************/
    1751 {
    1752 ca_dbSource_t * mySource;
    1753
    1754 mySource = g_slist_nth_data(sourceList, position);
    1755 return(mySource);
    1756 }
    1757
    1758 ca_SrcHdl_t *ca_get_SourceHandleByName(char *srcName)
         /* [<][>][^][v][top][bottom][index][help] */
    1759 /*******************************************************************
    1760  * ca_get_SourceHandleByName                                                                                                    *
    1761  *      -- retrieves the a handle to a source                                                                   *
    1762  *                                                                                                                                                                              *
    1763  * Parameters                                                                                                                                           *
    1764  *      -- the name of the required source
    1765  *                                                                                                                                                                              *
    1766  *                                                                                                                                                                              *
    1767         * Returns                                                                                                                                                       *
    1768         *       -- a pointer to the source or NULL                                                                              *
    1769  *              i.e. a pointer to the data component of the appropriate         *
    1770  *              element in the linked list of sources.                                                          *
    1771  *******************************************************************/
    1772
    1773 {
    1774 ca_dbSource_t * mySource;
    1775
    1776 mySource = ca_getSourceDetails(srcName, sourceList);
    1777 return(mySource);
    1778 }
    1779
    1780 char *ca_srchandle2Strelement(ca_SrcHdl_t *ah, int srcAttrib)
         /* [<][>][^][v][top][bottom][index][help] */
    1781 /*******************************************************************
    1782         * ca_srchandle2Strelement                                                                                                                       *
    1783         *       -- returns a string which represents the attribute of a source *
    1784         *               e.g. returns the name of a source                                                                       *
    1785         *               It allocates the required memory;                                                                       *
    1786         *               but it returns NULL if the required memory cannot be                    *
    1787         *               allocated.
    1788  *                                                                                                                                                                              *
    1789         * Parameters                                                                                                                                            *
    1790         *       --  source name - the name of the source
    1791         *                ca_get_SourceHandleByName or ca_get_SourceHandleByPosition *
    1792         *                                                                                                                                                                               *
    1793         *       -- srcAttrib - an integer which represents the required                         *
    1794         *               attribute of the source.  We use #define statments to make      *
    1795         *               a mapping between the attributes and the integers.                              *
    1796  *                                                                                                                                                                              *
    1797         * Returns                                                                                                                                                       *
    1798         * -- a string or NULL                                                                                                                   *
    1799  *******************************************************************/
    1800 {
    1801 char *myStr;
    1802 void ca_malloc(char *, int);
    1803
    1804 if(ah == NULL)
    1805                 {
    1806                 fprintf(stderr, "ca_srchandle2Strelement(): Cannot dereference NULL pointer\n");
    1807                 die;
    1808                 }
    1809
    1810 pthread_mutex_lock(&Lock);
    1811 switch(srcAttrib)
    1812                 {
    1813                 case 0:
    1814                 /* source name */
    1815                 myStr = strdup(ah->name);
    1816                 break;
    1817
    1818                 case 1:
    1819                 /* canupd */
    1820                 myStr =  strdup(ah->canupd);
    1821                 break;
    1822
    1823                 case 2:
    1824                 /* deflook */
    1825                 /*
    1826                  * ca_malloc(myStr, 2);
    1827                  * strcpy(myStr, (ah->db).deflook);
    1828                  */
    1829                 myStr = strdup(ah->deflook);
    1830                 break;
    1831
    1832                 case 3:
    1833                 /* machine */
    1834                 myStr = strdup((ah->db).host);
    1835                 break;
    1836
    1837                 case 5:
    1838                 /* user */
    1839                 myStr = strdup((ah->db).user);
    1840                 break;
    1841
    1842                 case 6:
    1843                 /* password */
    1844                 myStr = strdup((ah->db).password);
    1845                 break;
    1846
    1847                 case 7:
    1848                 /* dbName */
    1849                 myStr = strdup((ah->db).dbName);
    1850                 break;
    1851
    1852                 case 9:
    1853                 /* Near-Real-Time Mirror host */
    1854                 myStr = strdup((ah->nrtm).host);
    1855                 break;
    1856
    1857                 case 11:
    1858                 /* NRTM Log */
    1859                 myStr = strdup((ah->nrtm).log);
    1860                 break;
    1861
    1862                 default:
    1863                 puts("Cannot find this source attribute");
    1864                 break;
    1865                 }
    1866 pthread_mutex_unlock(&Lock);
    1867
    1868 return(myStr);
    1869 }
    1870
    1871 int ca_srchandle2Intelement(ca_SrcHdl_t *ah, int srcAttrib)
         /* [<][>][^][v][top][bottom][index][help] */
    1872 /*******************************************************************
    1873         * ca_srchandle2Intelement                                                                                                               *
    1874  *      -- a function that returns the integer value of the requested   *
    1875  *     attribute of the given source.                                                                           *
    1876  *                                                                                                                                                                              *
    1877  * Parameters                                                                                                                                           *
    1878         *       --  source name - the name of the source
    1879         *                ca_get_SourceHandleByName or ca_get_SourceHandleByPosition *
    1880         *                                                                                                                                                                               *
    1881         *       -- srcAttrib - an integer which represents the required                         *
    1882         *               attribute of the source.  We use #define statments to make      *
    1883         *               a mapping between the attributes and the integers.                              *
    1884  *                                                                                                                                                                              *
    1885  * Returns                                                                                                                                                      *
    1886         *  -- an integer.
    1887         *******************************************************************/
    1888 {
    1889 int myInt;      /* The value of this integer is returned. */
    1890
    1891 if(ah == NULL)
    1892                 {
    1893                 fprintf(stderr, "ca_srchandle2Intelement(): Cannot dereference NULL pointer\n");
    1894                 die;
    1895                 }
    1896
    1897 pthread_mutex_lock(&Lock);
    1898         switch(srcAttrib)
    1899                 {
    1900
    1901                 case 4:
    1902                 /* DB Port */
    1903                 myInt = (ah->db).port;
    1904                 break;
    1905
    1906                 case 8:
    1907                 /* Mode of Operation of the Source. */
    1908                 myInt = ah->opMode;
    1909                 break;
    1910
    1911                 case 10:
    1912                 /* Near-Real-Time Mirror port */
    1913                 myInt = (ah->nrtm).port;
    1914                 break;
    1915
    1916                 case 12:
    1917                 /* NRTM Delay */
    1918                 myInt = (ah->nrtm).delay;
    1919                 break;
    1920
    1921                 case 13:
    1922                 /* NRTM Protocol Version. */
    1923                 myInt = (ah->nrtm).protocolVer;
    1924                 break;
    1925
    1926                 case 14:
    1927                 /* Source Update Port */
    1928                 myInt = ah->updPort;
    1929                 break;
    1930
    1931                 default:
    1932                 fprintf(stderr, "Could not find source-attribute %d\n", srcAttrib);
    1933                 die;
    1934     break;
    1935                 }
    1936
    1937 pthread_mutex_unlock(&Lock);
    1938 return (myInt);
    1939 }
    1940
    1941
    1942 char *ca_get_adminStrElement(int symbol, int adminAttrib)
         /* [<][>][^][v][top][bottom][index][help] */
    1943 /*******************************************************************
    1944         * ca_adminStrElement
    1945         *       -- returns a string which represents the attribute of a admin  *
    1946  *     db
    1947         *               e.g. returns the name of a host machine.                                                   *
    1948         *               It allocates the required memory;                                                                       *
    1949         *               but it returns NULL if the required memory cannot be                    *
    1950         *               allocated.
    1951  *                                                                                                                                                                              *
    1952         * Parameters                                                                                                                                            *
    1953         *  -- symbol - the symbol of the variable
    1954         *                                                                                                                                                                               *
    1955         *       -- adminAttrib - an integer which represents the required                       *
    1956         *               attribute of the Admin db.  We use #define statements to        *
    1957         *               make a mapping between the attributes and the integers.         *
    1958  *                                                                                                                                                                              *
    1959         * Returns                                                                                                                                                       *
    1960         * -- a string or NULL                                                                                                                   *
    1961  *******************************************************************/
    1962 {
    1963 char *myStr;
    1964 void ca_malloc(char *, int);
    1965
    1966 /*
    1967         * Make sure that we are calling the correct function.
    1968  */
    1969 if ( strcmp(dictionary[symbol].varType, "CA_ADMIN") != 0)
    1970                 {
    1971                 fprintf(stderr, "Error: unexpected variable type.\n");
    1972                 die;
    1973                 }
    1974 else
    1975                 {
    1976  pthread_mutex_lock(&Lock);
    1977  switch(adminAttrib)
    1978                 {
    1979                 case 0:
    1980                 /* admin host */
    1981                 myStr = strdup( ((ca_ripadmin_t *)confVars[symbol].valPtr)->host);
    1982                 break;
    1983
    1984                 case 2:
    1985                 /* User */
    1986                 myStr =  strdup( ((ca_ripadmin_t *)confVars[symbol].valPtr)->user);
    1987                 break;
    1988
    1989                 case 3:
    1990                 /* password */
    1991                 myStr = strdup( ((ca_ripadmin_t *)confVars[symbol].valPtr)->password);
    1992                 break;
    1993
    1994                 case 4:
    1995                 /* tableName */
    1996                 myStr = strdup( ((ca_ripadmin_t *)confVars[symbol].valPtr)->tableName);
    1997                 break;
    1998
    1999                 default:
    2000                 puts("Cannot find this admin attribute");
    2001                 die;
    2002                 break;
    2003                 }
    2004  pthread_mutex_unlock(&Lock);
    2005
    2006  }
    2007 return(myStr);
    2008 }
    2009
    2010 int ca_get_adminIntElement(int symbol, int adminAttrib)
         /* [<][>][^][v][top][bottom][index][help] */
    2011 /*
    2012         * Returns an int element of the admin db structure.
    2013  */
    2014 {
    2015 int myInt;      /* The value of this integer is returned. */
    2016
    2017 pthread_mutex_lock(&Lock);
    2018 switch(adminAttrib)
    2019         {
    2020         case 1:
    2021         /* Port number */
    2022         myInt = ((ca_ripadmin_t *)confVars[symbol].valPtr)->port;
    2023         break;
    2024
    2025  default:
    2026         puts("Cannot find this admin attribute");
    2027         die;
    2028         break;
    2029         }
    2030 pthread_mutex_unlock(&Lock);
    2031
    2032 return(myInt);
    2033 }
    2034
    2035 void ca_malloc(char *someStr, int memSize)
         /* [<][>][^][v][top][bottom][index][help] */
    2036 /*******************************************************************
    2037         * ca_malloc                                                                                                                                                     *
    2038         *       -- a function that allocates memory for a string                                        *
    2039         *                                                                                                                                                                               *
    2040  * Parameters                                                                                                                                           *
    2041         * --someStr     - the string that is to be created                                                      *
    2042         *        memSize- required amount of memory in bytes                                                    *
    2043         *                                                                                                                                                                               *
    2044         * Returns                                                                                                                                                       *
    2045         * -- nothing; it assigns the allocated memory to the pointer            *
    2046  *   that was passed to it.                                                                                                             *
    2047         *                                                                                                                                                                               *
    2048         *******************************************************************/
    2049 {
    2050         someStr = malloc(memSize);
    2051
    2052         /*
    2053          * Check that we actually did get the memory ....
    2054   */
    2055         if (someStr == NULL)
    2056                 {
    2057                 fprintf(stderr, "ca_malloc(): cannot allocate memory !!!\n");
    2058                 exit(8);
    2059                 }
    2060 }
    2061
    2062 void ca_set_boolean(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    2063 {
    2064 /*************************************************************
    2065  *                                                                                                                                                              *
    2066  * ca_set_boolean()                                                                                                             *
    2067  *                                                                                                                                                              *
    2068         *                                                                                                                                                               *
    2069  * Parameters                                                                                                                           *
    2070  *                                                                                                                                                              *
    2071         *       symbol -- the symbol for the variable.                                                  *
    2072  *                                                                                                                                                              *
    2073         *                                                                                                                                                               *
    2074         * Returns                                                                                                                                       *
    2075         *                                                                                                                                                               *
    2076         *               nothing                                                                                                                         *
    2077  *                                                                                                                                                              *
    2078         *                                                                                                                                                               *
    2079  * Remarks                                                                                                                                      *
    2080         *                                                                                                                                                               *
    2081         *       Must check that a sensible value is given as input.             *
    2082         *                                                                                                                                                               *
    2083         *                                                                                                                                                               *
    2084         *************************************************************/
    2085
    2086
    2087 char newTestmodeStr[2];
    2088 int newTestmodeVal;     /* The new value of the testmode variable. */
    2089 int invalid;                            /* Flag to indicate an invalid new value.  */
    2090
    2091 FILE *testPtr, *tempPtr;                        /* The pointer to the files. */
    2092 char name[STRLENGTH];                           /* The name of the variable. */
    2093 char value[STRLENGTH];                  /* The value of the variable. */
    2094
    2095 /*
    2096  * Function to change the value in a given values array.
    2097  * This function can only be called from within ca_set_boolean().
    2098  */
    2099 int *ca_change_int_value(char []);
    2100
    2101
    2102 /*
    2103         * Using the symbol, look at the appropriate place in the
    2104  * dictionary.
    2105         */
    2106 #ifdef DEBUG
    2107 printf("\nca_set_int() function called .....\n");
    2108 printf("Variable type: %s\n", dictionary[symbol].varType);
    2109 #endif          /* DEBUG */
    2110
    2111 /*
    2112  * Check that the function is attempting to set the correct type of
    2113  * value.  If not, do not set the value, but exit instead.
    2114  */
    2115
    2116 if (strcmp(dictionary[symbol].varType, "CA_BOOLEAN") != 0)
    2117                 {
    2118                 fprintf(stderr, "Error: CA_BOOLEAN data type expected.\n");
    2119                 die;
    2120                 }
    2121
    2122 /*
    2123  * First, flush the input stream.
    2124  */
    2125 fflush(stdin);
    2126
    2127
    2128 /*
    2129  * Make sure that a reasonable, sensible value of bind-port has
    2130  * been read from the keyboard.
    2131  */
    2132
    2133 do      {
    2134                         /*
    2135                          * Prompt for the new value of the testmode.
    2136                          */
    2137
    2138                         printf("\nNew value of testmode (0 or 1) >>> ");
    2139                         scanf("%s", newTestmodeStr);
    2140
    2141                         /*
    2142                          * We scanf() the value as a string, but we want it to be an
    2143                          * integer.  Thus, we use sscanf() to scanf the value from the
    2144          * string-variable and store it as an integer in an integer
    2145                          * variable.
    2146                          */
    2147                         sscanf(newTestmodeStr, "%d", &newTestmodeVal);
    2148
    2149                         /*
    2150         * We only change the testmode when the user is absolutely sure
    2151                          * that they want to change.  Thus, we only accept two possible
    2152                          * values for testmode.
    2153                          */
    2154
    2155                         if ( (newTestmodeVal < 0) || (newTestmodeVal > 1) )
    2156                                 {
    2157                                 invalid = 1;
    2158                                 puts("Only '0' or '1' accepted as value for testmode.");
    2159                                 }
    2160                         else
    2161                                 {
    2162                                 invalid = 0;
    2163                                 }
    2164                 } while(invalid);
    2165
    2166
    2167 /*
    2168         * Lock the value of the variable before changing it.
    2169  */
    2170
    2171 pthread_mutex_lock(&Lock);
    2172
    2173
    2174 /*
    2175  * Choose the appropriate values array.
    2176  */
    2177
    2178 switch(dictionary[symbol].varScope)
    2179                 {
    2180                 /*
    2181                  * If the variable has global scope,
    2182                  * write it into the globals array.
    2183                  * If it has local scope,
    2184                  * write it into the local array.
    2185                  * If the scope cannot be found, then report an error.
    2186      */
    2187                 case 1:
    2188                 globals[symbol].valPtr = ca_change_int_value(newTestmodeStr);
    2189                 globals[symbol].strPtr = newTestmodeStr;
    2190                 break;
    2191
    2192                 case 99:
    2193                 locals[symbol].valPtr = ca_change_int_value(newTestmodeStr);
    2194                 locals[symbol].strPtr = newTestmodeStr;
    2195                 break;
    2196
    2197                 default:
    2198                 fprintf(stderr, "Error: unknown scope: %d\n", dictionary[symbol].varScope);
    2199                 break;
    2200                 }
    2201
    2202 /*
    2203         * Write the new value of this variable back to the config file.
    2204  *
    2205  * To be implemented.
    2206  */
    2207
    2208 /*
    2209  * Find the actual name of the variable from the dictionary
    2210  * structure (use the variable symbol as an index into the
    2211  * array of dictionary structures.
    2212  */
    2213
    2214         printf("Name of variable to be changed: %s\n", dictionary[symbol].varName);
    2215         printf("Type of variable to be changed: %s\n", dictionary[symbol].varType);
    2216
    2217 /*
    2218         * Open the test config file for reading .....
    2219  */
    2220 if ( (testPtr = fopen(testFile, "r")) == NULL)
    2221         {
    2222         printf("File \"%s\" could not be opened.\n", testFile);
    2223         die;
    2224         }
    2225
    2226 /*
    2227         * Open the temporary file for writing .....
    2228  */
    2229 if ((tempPtr = fopen(tempFile, "w")) == NULL)
    2230         {
    2231         printf("File \"%s\" could not be opened.\n", tempFile);
    2232  die;
    2233  }
    2234
    2235 /*
    2236  * Read the first record in the test config file.
    2237  */
    2238
    2239  fscanf(testPtr, "%s", name);
    2240  fgets(value, sizeof(value), testPtr);
    2241
    2242  /*
    2243   * If the last character of "value" is '\n',
    2244   * replace it with '\0'.
    2245   */
    2246         if (value[strlen(value) - 1] == '\n')
    2247                 {
    2248                 printf("The value string is %s", value);
    2249                 printf("Replacing last character of \"%s\" with the NULL character\n", name);
    2250                 value[strlen(value) - 1] = '\0';
    2251                 printf("The new value string is %s", value);
    2252                 }
    2253
    2254
    2255 /*
    2256         * While there are records to be read in the test config file:
    2257         *               Write the current record into the temporary file.
    2258  *              Read the next record in the config file.
    2259  * Repeat untill the EOF has been reached.
    2260  */
    2261
    2262 while(!feof(testPtr) )
    2263         {
    2264  fprintf(tempPtr, "%s %s\n", name, value);
    2265  fscanf(testPtr, "%s", name);
    2266         fgets(value, sizeof(value), testPtr);
    2267
    2268  /*
    2269   * If the last character of "value" is '\n',
    2270   * replace it with '\0'.
    2271   */
    2272         if (value[strlen(value) - 1] == '\n')
    2273                 {
    2274                 printf("The last character of the value string is %c", value[strlen(value) - 1]);
    2275                 printf("The value string is %s", value);
    2276                 printf("Replacing last character of \"%s\" with the NULL character\n",name);
    2277                 value[strlen(value) - 1] = '\0';
    2278                 printf("The new value string is %s", value);
    2279                 }
    2280
    2281
    2282  /*
    2283   * if we read the variable that we want to change,
    2284   * stop reading this file and print only the name
    2285   * of this variable to the temporary file.
    2286   */
    2287
    2288         /*
    2289          * If we read the variable that we want to change,
    2290   * replace the value of this variable in the config
    2291   * file with the value supplied from the keyboard.
    2292   *
    2293   */
    2294         if ( strcmp(name, dictionary[symbol].varName) == 0)
    2295                 {
    2296                 strcpy(value, newTestmodeStr);
    2297                 printf("The replacement string is %s", value);
    2298                 }
    2299         /*
    2300          * Flush the pointer to the test config file.
    2301          */
    2302         fflush(testPtr);
    2303
    2304         }
    2305  /*
    2306          * Here ends the loop that writes the config file, with the
    2307   * new variable, to the temporary file.
    2308          */
    2309
    2310 /*
    2311  *
    2312  * While !(the record to be updated)
    2313         *       BEGIN
    2314         *  Write the record to the temporary file
    2315  *  Read the next record in the config file
    2316  *      END
    2317  *
    2318  * Write the new value to the temporary file
    2319  * Read the next record in the config file
    2320  * COMMENT: this is the record to be updated.
    2321  * COMMENT: discard this record.
    2322  *
    2323  * Read the next record in the config file
    2324         *
    2325  * While !(EOF)
    2326         *       BEGIN
    2327         *  write the record to the temporary file
    2328  *  read the next record in the config file
    2329  *  END
    2330  *
    2331  * Close Config file
    2332  * Close Temporary file
    2333  *
    2334  * Open Temporary file for reading
    2335  * Open Config file for writing
    2336  *
    2337  * Read the next record of the Temporary file
    2338  *
    2339  * While (!EOF of Temporary file)
    2340         *       BEGIN
    2341         *  write the record into the Config file
    2342         *  read the next record of the Temporary file
    2343  *  END
    2344  *
    2345         *       Close Temporary file
    2346  *  Close Config file
    2347  *
    2348  */
    2349
    2350 fclose(testPtr);
    2351 fclose(tempPtr);
    2352
    2353 /*
    2354         * Now, flush the file pointers
    2355  */
    2356         fflush(testPtr);
    2357         fflush(tempPtr);
    2358
    2359 /*
    2360  * Open the temporary file for reading.
    2361         * Open the config file for writing.
    2362  * Write the contents of the temporary file
    2363  * into the config file.
    2364  */
    2365
    2366 /*
    2367         * Open the temporary file for reading .....
    2368  */
    2369 if ((tempPtr = fopen(tempFile, "r")) == NULL)
    2370         {
    2371         printf("File \"%s\" could not be opened for reading.\n", tempFile);
    2372  die;
    2373  }
    2374
    2375 /*
    2376         * Open the config file for writing .....
    2377  */
    2378 if ((testPtr = fopen(testFile, "w")) == NULL)
    2379         {
    2380         printf("File \"%s\" could not be opened for writing.\n", testFile);
    2381  die;
    2382  }
    2383
    2384 /*
    2385  * Read the first record in the temporary file.
    2386  */
    2387
    2388  fscanf(tempPtr, "%s", name);
    2389  fgets(value, sizeof(value), tempPtr);
    2390         printf("\nFIRST LINE: %s %s", name, value);
    2391
    2392
    2393 /*
    2394         * While there are records to be read in the temporary file:
    2395         *               Write the current record into the test config file.
    2396  *              Read the next record in the temporary file.
    2397  * Repeat untill the EOF has been reached.
    2398  */
    2399
    2400 while(!feof(tempPtr) )
    2401         {
    2402  fprintf(testPtr, "%s %s", name, value);
    2403  fscanf(tempPtr, "%s", name);
    2404         fgets(value, sizeof(value), tempPtr);
    2405  }
    2406
    2407 fclose(testPtr);
    2408 fclose(tempPtr);
    2409
    2410 /*
    2411         * Unlock the value of the variable after setting it and writing the
    2412  * new value back to the configuration (and the dictionary) file.
    2413         *
    2414  */
    2415         pthread_mutex_unlock(&Lock);
    2416
    2417 }
    2418
    2419
    2420 void ca_set_dirlist(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    2421 {
    2422 /****************************************************************
    2423         * ca_set_dirlist()                                                                                                                      *
    2424  *                                                                                                                                                                      *
    2425  * Parameters                                                                                                                                   *
    2426         *               symbol -- the symbol of the variable.                                                   *
    2427  *                                                                                                                                                                      *
    2428  * Returns                                                                                                                                              *
    2429         *               1 if successful, 0 if not successful.                                                   *
    2430         *                                                                                                                                                                       *
    2431         * Remarks                                                                                                                                               *
    2432  *              Writing the new value back to the config file has yet to *
    2433         *               be implemented.                                                                                                         *
    2434  *                                                                                                                                                                      *
    2435         ****************************************************************/
    2436
    2437  char newDir[80];
    2438  /*
    2439   * Declare a pointer to a values_t variable.
    2440          * Later, we shall assign this pointer to the first element
    2441   * of either the globals or the locals array, as appropriate.
    2442   */
    2443  values_t *hereValues;
    2444
    2445         /*
    2446          * Using the symbol, look in the appropriate place in the dictionary.
    2447          */
    2448 #ifdef DEBUG
    2449         printf("\nca_set_dirlist() function called ..... \n");
    2450  printf("Variable type: %s\n", dictionary[symbol].varType);
    2451 #endif
    2452
    2453         /*
    2454          * First, flush the input stream.
    2455          */
    2456         fflush(stdin);
    2457
    2458         /*
    2459          * Prompt for the new value of the directory.
    2460          */
    2461         printf("\nNew value of %s [80 characters, maximum] >>> ", dictionary[symbol].varName);
    2462  scanf("%s", newDir);
    2463
    2464 /*
    2465  * Make sure that a reasonable, sensible value of the directory
    2466  * value has been read from the keyboard.
    2467  *
    2468  * How do we implement this ???
    2469  *
    2470  */
    2471
    2472
    2473  /*
    2474   * Make sure that the function is attempting to set the correct type
    2475   * of value.  If not, do not set the value - and exit.
    2476   */
    2477
    2478 if (strcmp(dictionary[symbol].varType, "CA_DIRLIST") != 0)
    2479                 {
    2480                 fprintf(stderr, "Error: unexpected variable type.\n");
    2481                 exit(51);
    2482                 }
    2483
    2484         /*
    2485   * Choose the appropriate values array.
    2486          * Assign a temporary pointer to this array.
    2487          */
    2488
    2489         switch(dictionary[symbol].varScope)
    2490                 {
    2491                 /* If the variable has global scope,
    2492                  * write it into the globals array.
    2493                  * If it has local scope,
    2494                  * write it into the locals array.
    2495          * If the scope cannot be found, report an error.
    2496                  */
    2497                 case 1:
    2498                 hereValues = globals;
    2499                 break;
    2500
    2501                 case 99:
    2502                 hereValues = locals;
    2503                 break;
    2504
    2505                 default:
    2506                 fprintf(stderr, "Error: Unknown scope: %d\n", dictionary[symbol].varScope);
    2507                 break;
    2508                 }
    2509
    2510
    2511         /*
    2512   * Check for the presence of the mutex lock:
    2513          *              if present,
    2514          *              wait until it is available;
    2515          *              else
    2516   *                     get the lock and proceed with the change of value.
    2517          */
    2518
    2519         /*
    2520   * Write the new value of the variable to the correct place
    2521          * in the [appropriate] values array.
    2522          *
    2523   * Note that there is a check to see if malloc() actually worked .....
    2524          */
    2525
    2526                 hereValues[symbol].valPtr = (char *)malloc(80);
    2527                 if (hereValues[symbol].valPtr == NULL)
    2528                         {
    2529                         fprintf(stderr, "Cannot alllocate memory for hereValuesvlPtr\n");
    2530                         die;
    2531                         }
    2532                 strcpy(hereValues[symbol].valPtr,newDir);
    2533
    2534
    2535                 hereValues[symbol].strPtr = (char *)malloc(sizeof(newDir) );
    2536                 if (hereValues[symbol].strPtr == NULL)
    2537                         {
    2538                         fprintf(stderr, "Cannot alllocate memory for hereValuestPtr\n");
    2539                         die;
    2540                         }
    2541                 strcpy(hereValues[symbol].strPtr, newDir);
    2542
    2543         /*
    2544          * Free the temporary pointer, hereValues.
    2545   *
    2546   */
    2547         free(hereValues);
    2548         hereValues = NULL;
    2549
    2550         /*
    2551          * Release the mutex lock.
    2552          */
    2553
    2554         /*
    2555          * Write the new value of this variable back to the config file.
    2556          */
    2557
    2558 }
    2559
    2560
    2561 void ca_set_string(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    2562 {
    2563
    2564 /****************************************************************
    2565         * ca_set_string()                                                                                                                       *
    2566  *                                                                                                                                                                      *
    2567  * Parameters                                                                                                                                   *
    2568         *               symbol -- the symbol of the variable.                                                   *
    2569  *                                                                                                                                                                      *
    2570  * Returns                                                                                                                                              *
    2571         *               1 if successful, 0 if not successful ?                                                  *
    2572         *                                                                                                                                                                       *
    2573         * Remarks                                                                                                                                               *
    2574  *              Writing the new value back to the config file has yet to *
    2575         *               be implemented.                                                                                                         *
    2576  *                                                                                                                                                                      *
    2577         ****************************************************************/
    2578
    2579  char newString[80];    /* May need to make this bigger. */
    2580
    2581  /*
    2582   * Declare a pointer to a values_t variable.
    2583          * Later, we shall assign this pointer to the first element
    2584   * of either the globals or the locals array, as appropriate.
    2585   */
    2586  values_t *hereValues;
    2587
    2588         /*
    2589          * Using the symbol, look in the appropriate place in the dictionary.
    2590          */
    2591 #ifdef DEBUG
    2592         printf("\nca_set_string() function called ..... \n");
    2593  printf("Variable type: %s\n", dictionary[symbol].varType);
    2594 #endif
    2595
    2596         /*
    2597          * First, flush the input stream.
    2598          */
    2599         fflush(stdin);
    2600
    2601         /*
    2602          * Prompt for the new value of the string.
    2603          */
    2604         printf("\nNew value of %s [80 characters, maximum] >>> ", dictionary[symbol].varName);
    2605 gets(newString);
    2606
    2607 /*
    2608  * Make sure that a reasonable, sensible value of the string
    2609  * value has been read from the keyboard.
    2610  *
    2611  * How do we implement this ???
    2612  *
    2613  */
    2614
    2615
    2616  /*
    2617   * Make sure that the function is attempting to set the correct type
    2618   * of value.  If not, do not set the value - and exit.
    2619   */
    2620
    2621 if (strcmp(dictionary[symbol].varType, "CA_STRING") != 0)
    2622                 {
    2623                 fprintf(stderr, "Error: unexpected variable type.\n");
    2624                 exit(51);
    2625                 }
    2626
    2627         /*
    2628   * Choose the appropriate values array.
    2629          * Assign a temporary pointer to this array.
    2630          */
    2631
    2632         switch(dictionary[symbol].varScope)
    2633                 {
    2634                 /* If the variable has global scope,
    2635                  * write it into the globals array.
    2636                  * If it has local scope,
    2637                  * write it into the locals array.
    2638          * If the scope cannot be found, report an error.
    2639                  */
    2640                 case 1:
    2641                 hereValues = globals;
    2642                 break;
    2643
    2644                 case 99:
    2645                 hereValues = locals;
    2646                 break;
    2647
    2648                 default:
    2649                 fprintf(stderr, "Error: Unknown scope: %d\n", dictionary[symbol].varScope);
    2650                 break;
    2651                 }
    2652
    2653
    2654         /*
    2655   * Check for the presence of the mutex lock:
    2656          *              if present,
    2657          *              wait until it is available;
    2658          *              else
    2659   *                     get the lock and proceed with the change of value.
    2660          */
    2661         pthread_mutex_lock(&Lock);
    2662
    2663         /*
    2664   * Write the new value of the variable to the correct place
    2665          * in the [appropriate] values array.
    2666   * Note the check to the return value of malloc() to see if the
    2667          * memory was actually obtained.
    2668          */
    2669
    2670                 hereValues[symbol].valPtr = (char *)malloc(80);
    2671                 if (hereValues[symbol].valPtr == NULL)
    2672                         {
    2673                         fprintf(stderr, "Cannot allocate memory for hereValues[symbol].valPtr\n");
    2674                         die;
    2675                         }
    2676                 strcpy(hereValues[symbol].valPtr, newString);
    2677
    2678
    2679                 hereValues[symbol].strPtr = (char *)malloc(sizeof(newString) );
    2680                 if (hereValues[symbol].strPtr == NULL)
    2681                         {
    2682                         fprintf(stderr, "Cannot allocate memory for hereValues[symbol].strPtr\n");
    2683                         die;
    2684                         }
    2685                 strcpy(hereValues[symbol].strPtr, newString);
    2686
    2687         /*
    2688          * Free the temporary pointer, hereValues.
    2689          *
    2690   */
    2691         free(hereValues);
    2692  hereValues = NULL;
    2693
    2694         /*
    2695          * Release the mutex lock.
    2696          */
    2697         pthread_mutex_unlock(&Lock);
    2698
    2699         /*
    2700          * Write the new value of this variable back to the config file.
    2701          * Implement this later ?
    2702          */
    2703
    2704 }
    2705
    2706
    2707 int ca_writeNewValue(int dictSymbol, char *newValue)
         /* [<][>][^][v][top][bottom][index][help] */
    2708 {
    2709
    2710 FILE *confPtr;                                          /* Pointer to config file */
    2711 FILE *tempPtr;                                          /* The pointer to temp file. */
    2712 char name[STRLENGTH];                           /* The name of the variable. */
    2713 char value[STRLENGTH];                  /* The value of the variable. */
    2714
    2715
    2716 /*
    2717  * Find the actual name of the variable from the dictionary
    2718  * structure (use the variable symbol as an index into the
    2719  * array of dictionary structures.
    2720  */
    2721 #ifdef DEBUG
    2722         printf("Name of variable to be changed: %s\n", dictionary[dictSymbol].varName);
    2723         printf("Type of variable to be changed: %s\n", dictionary[dictSymbol].varType);
    2724 #endif  /* DEBUG */
    2725
    2726 /*
    2727         * Open the test config file for reading .....
    2728  */
    2729 if ( (confPtr = fopen(testFile, "r")) == NULL)
    2730         {
    2731         printf("File \"%s\" could not be opened.\n", testFile);
    2732         die;
    2733         }
    2734
    2735 /*
    2736         * Open the temporary file for writing .....
    2737  */
    2738 if ((tempPtr = fopen(tempFile, "w")) == NULL)
    2739         {
    2740         printf("File \"%s\" could not be opened.\n", tempFile);
    2741  die;
    2742  }
    2743
    2744 /*
    2745  * Read the first record in the test config file.
    2746  */
    2747
    2748  fscanf(confPtr, "%s", name);
    2749  fgets(value, sizeof(value), confPtr);
    2750
    2751  /*
    2752   * If the last character of "value" is '\n',
    2753   * replace it with '\0'.
    2754   */
    2755         if (value[strlen(value) - 1] == '\n')
    2756                 {
    2757 #ifdef DEBUG
    2758                 printf("The value string is %s", value);
    2759                 printf("Replacing last character of \"%s\" with the NULL character\n", name);
    2760 #endif  /* DEBUG */
    2761
    2762                 value[strlen(value) - 1] = '\0';
    2763
    2764 #ifdef DEBUG
    2765                 printf("The new value string is %s", value);
    2766 #endif  /* DEBUG */
    2767                 }
    2768
    2769         /*
    2770          * If we read the variable that we want to change,
    2771   * replace the value of this variable in the config
    2772   * file with the value supplied from the keyboard.
    2773   *
    2774   */
    2775         if ( strcmp(name, dictionary[dictSymbol].varName) == 0)
    2776                 {
    2777                 strcpy(value, newValue);
    2778
    2779 #ifdef DEBUG
    2780                 printf("The replacement string is %s", value);
    2781 #endif  /* DEBUG */
    2782 }
    2783
    2784 /*
    2785         * While there are records to be read in the test config file:
    2786         *               Write the current record into the temporary file.
    2787  *              Read the next record in the config file.
    2788  * Repeat untill the EOF has been reached.
    2789  */
    2790
    2791 while(!feof(confPtr) )
    2792         {
    2793  fprintf(tempPtr, "%s %s\n", name, value);
    2794  fscanf(confPtr, "%s", name);
    2795         fgets(value, sizeof(value), confPtr);
    2796
    2797  /*
    2798   * If the last character of "value" is '\n',
    2799   * replace it with '\0'.
    2800   */
    2801         if (value[strlen(value) - 1] == '\n')
    2802                 {
    2803 #ifdef DEBUG
    2804                 printf("The last character of the value string is %c", value[strlen(value) - 1]);
    2805                 printf("The value string is %s", value);
    2806                 printf("Replacing last character of \"%s\" with the NULL character\n",name);
    2807 #endif  /* DEBUG */
    2808
    2809                 value[strlen(value) - 1] = '\0';
    2810 #ifdef DEBUG
    2811                 printf("The new value string is %s", value);
    2812 #endif  /* DEBUG */
    2813                 }
    2814
    2815
    2816         /*
    2817          * If we read the variable that we want to change,
    2818   * replace the value of this variable in the config
    2819   * file with the value supplied from the keyboard.
    2820   *
    2821   */
    2822         if ( strcmp(name, dictionary[dictSymbol].varName) == 0)
    2823                 {
    2824                 strcpy(value, newValue);
    2825
    2826 #ifdef DEBUG
    2827                 printf("The replacement string is %s", value);
    2828 #endif  /* DEBUG */
    2829                 }
    2830
    2831         /*
    2832          * Flush the pointer to the test config file.
    2833          */
    2834         fflush(confPtr);
    2835
    2836         }
    2837  /*
    2838          * Here ends the loop that writes the config file, with the
    2839   * new variable, to the temporary file.
    2840          */
    2841
    2842 /*
    2843  *
    2844  * While !(the record to be updated)
    2845         *       BEGIN
    2846         *  Write the record to the temporary file
    2847  *  Read the next record in the config file
    2848  *      END
    2849  *
    2850  * Write the new value to the temporary file
    2851  * Read the next record in the config file
    2852  * COMMENT: this is the record to be updated.
    2853  * COMMENT: discard this record.
    2854  *
    2855  * Read the next record in the config file
    2856         *
    2857  * While !(EOF)
    2858         *       BEGIN
    2859         *  write the record to the temporary file
    2860  *  read the next record in the config file
    2861  *  END
    2862  *
    2863  * Close Config file
    2864  * Close Temporary file
    2865  *
    2866  * Open Temporary file for reading
    2867  * Open Config file for writing
    2868  *
    2869  * Read the next record of the Temporary file
    2870  *
    2871  * While (!EOF of Temporary file)
    2872         *       BEGIN
    2873         *  write the record into the Config file
    2874         *  read the next record of the Temporary file
    2875  *  END
    2876  *
    2877         *       Close Temporary file
    2878  *  Close Config file
    2879  *
    2880  */
    2881
    2882 fclose(confPtr);
    2883 fclose(tempPtr);
    2884
    2885 /*
    2886         * Now, flush the file pointers
    2887  */
    2888         fflush(confPtr);
    2889         fflush(tempPtr);
    2890
    2891 /*
    2892  * Open the temporary file for reading.
    2893         * Open the config file for writing.
    2894  * Write the contents of the temporary file
    2895  * into the config file.
    2896  */
    2897
    2898 /*
    2899         * Open the temporary file for reading .....
    2900  */
    2901 if ((tempPtr = fopen(tempFile, "r")) == NULL)
    2902         {
    2903         printf("File \"%s\" could not be opened for reading.\n", tempFile);
    2904  die;
    2905  }
    2906
    2907 /*
    2908         * Open the config file for writing .....
    2909  */
    2910 if ((confPtr = fopen(testFile, "w")) == NULL)
    2911         {
    2912         printf("File \"%s\" could not be opened for writing.\n", testFile);
    2913  die;
    2914  }
    2915
    2916 /*
    2917  * Read the first record in the temporary file.
    2918  */
    2919
    2920  fscanf(tempPtr, "%s", name);
    2921  fgets(value, sizeof(value), tempPtr);
    2922 #ifdef DEBUG
    2923         printf("\nFIRST LINE: %s %s", name, value);
    2924 #endif /* DEBUG */
    2925
    2926 /*
    2927         * While there are records to be read in the temporary file:
    2928         *               Write the current record into the test config file.
    2929  *              Read the next record in the temporary file.
    2930  * Repeat untill the EOF has been reached.
    2931  */
    2932
    2933 while(!feof(tempPtr) )
    2934         {
    2935  fprintf(confPtr, "%s %s", name, value);
    2936  fscanf(tempPtr, "%s", name);
    2937         fgets(value, sizeof(value), tempPtr);
    2938  }
    2939
    2940 fclose(confPtr);
    2941 fclose(tempPtr);
    2942 unlink(tempFile);
    2943
    2944 return(0);
    2945 }
    2946
    2947
    2948 int ca_getStorageLocation(char *confVar, dict_t woordenboek[], int size)
         /* [<][>][^][v][top][bottom][index][help] */
    2949 /*************************************************************
    2950  * ca_getStorageLocation()                                                                                              *
    2951         *       - takes the name of a config variable and searches the          *
    2952  *    dictionary structure for the storage location for this *
    2953  *        variable.                                                                                                                             *
    2954  *                                                                                                                                                              *
    2955  * Parameters                                                                                                                           *
    2956         *       confVar -- the string variable that contains the name    *
    2957  *                of the variable.                                                                              *
    2958  *  woordenboek -- the dictionary structure to be searched      *
    2959         *       size                    -- the size of the dictionary structure to      *
    2960  *                 searched.                                                                                    *
    2961  *                                                                                                                                                              *
    2962  * Returns                                                                                                                                      *
    2963         *       the location (integer) in the values array.                                     *
    2964  *                                                                                                                                                              *
    2965         *************************************************************/
    2966 {
    2967 int i,
    2968 where,
    2969 found = 0       ;               /* Whether or not the symbol has been found. */
    2970
    2971
    2972 #ifdef DEBUG
    2973 printf("The variable name in ca_getStorageLocation is: %s\n", confVar);
    2974 #endif /* DEBUG */
    2975
    2976 /*
    2977  * Compares each name in the dictionary with the one for which
    2978  * we are looking.
    2979  */
    2980 i = 0;
    2981 while (!found && i < size)
    2982         {
    2983         if (strcmp(woordenboek[i].varName, confVar) == 0)
    2984                 {
    2985                 found = 1;
    2986                 }
    2987         else
    2988                 {
    2989                 ++i;
    2990                 }
    2991         }
    2992
    2993 /*
    2994         * Returns the storage location for the given variable name
    2995  * or else returns NOT_FOUND
    2996  */
    2997 if (found)
    2998                 {
    2999                 /*      mySymbol = atoi(woordenboek[i].varSym); */
    3000 #ifdef DEBUG
    3001     printf("Symbol is %s\n", woordenboek[i].varSym);
    3002                 printf("Storage Location is: %d\n", woordenboek[i].varNum);
    3003 #endif  /* DEBUG */
    3004            where = woordenboek[i].varNum;
    3005                 }
    3006 else
    3007                 {
    3008                 fprintf(stderr, "Error: cannot find storage location for variable %s\n", confVar);
    3009                 where = NOT_FOUND;
    3010                 }
    3011         return (where);
    3012
    3013 }
    3014
    3015
    3016 void ca_getConfig(values_t confVars[], int size)
         /* [<][>][^][v][top][bottom][index][help] */
    3017 /*************************************************************
    3018  * ca_getConfig -- prints the strings representing the          *
    3019  *                                         values of the configuration variables                *
    3020  *                                                                                                                                                              *
    3021  * Parameters                                                                                                                           *
    3022         *               confVars -- the values_t array which stores the                 *
    3023         *                                               values of the configuration variables.     *
    3024         *               size -- the number of configuration variables,                  *
    3025         *                       the number of elements in the confVars array  *
    3026  *                                                                                                                                                              *
    3027  *                                                                                                                                                              *
    3028  *************************************************************/
    3029 {
    3030 int i = 0;      /* A counting variable. */
    3031
    3032 puts("A dump of the strings of the values of the Config Vars:");
    3033 puts("Number\t\tString");
    3034 puts("----------");
    3035
    3036 while (i < size)
    3037         {
    3038         printf("%d\t\t%s\n", i, confVars[i].strPtr);
    3039  ++i;
    3040         }
    3041
    3042 }
    3043
    3044
    3045 int ca_getType(char *confVar, dict_t woordenboek[], int size)
         /* [<][>][^][v][top][bottom][index][help] */
    3046 /****************************************************************
    3047         * ca_getType -- returns the data type of the variable.                  *
    3048  *                                                                                                                                                                      *
    3049  * Parameters                                                                                                                                   *
    3050  *              confVar -- the name of the configuration variable.                      *
    3051         *               woordenboek -- the array of dict_t structures.                          *
    3052         *               size -- the number of configuration variables.                          *
    3053  *                                                                                                                                                                      *
    3054  * Returns                                                                                                                                              *
    3055  *              an integer representing the data type of the variable           *
    3056  *                                                                                                                                                                      *
    3057  ****************************************************************/
    3058 {
    3059 int i = 0,              /* Counter variable. */
    3060                 found = 0;      /* Set this == 1 when we find the variable.  */
    3061 int myType;     /* Integer representing the type of the config variable. */
    3062
    3063 /*
    3064  * Compare each name in the dictionary with the one for which we
    3065  * are looking.
    3066  */
    3067
    3068 myType = 0;
    3069
    3070 #ifdef DEBUG
    3071 printf("ca_getType function called for variable: %s\n", confVar);
    3072 #endif  /* DEBUG */
    3073
    3074 while (!found && i <= size)
    3075                 {
    3076                 if (strcmp(woordenboek[i].varName, confVar) == 0)
    3077                         {
    3078                         found = 1;
    3079 #ifdef DEBUG
    3080 printf("ca_getType function: %s, %s matched.\n", woordenboek[i].varName, confVar);
    3081 #endif  /* DEBUG */
    3082                         }
    3083                 else
    3084                         {
    3085                         ++i;
    3086                         }
    3087                 }
    3088
    3089 /*
    3090         * Return the type of the config variable or
    3091         * else return "NOT FOUND".
    3092  */
    3093 if (found)
    3094                 {
    3095                         if(strcmp(woordenboek[i].varType, "CA_INT") == 0)
    3096                                 {
    3097 #ifdef DEBUG
    3098 printf("ca_getType function: %s variable of type %s is Integer type\n", woordenboek[i].varName, woordenboek[i].varType);
    3099
    3100 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3101 #endif /* DEBUG */
    3102                                 myType = 11;
    3103 #ifdef DEBUG
    3104                                 printf("For type CA_INT, myType is %d\n", myType);
    3105 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3106 #endif /* DEBUG */
    3107                                 }
    3108                         else
    3109                                 {
    3110                                         if(strcmp(woordenboek[i].varType, "CA_STRING") == 0)
    3111                                                 {
    3112 #ifdef DEBUG
    3113 printf("ca_getType function: %s variable of type %s is String type\n", woordenboek[i].varName, woordenboek[i].varType);
    3114 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3115 #endif /* DEBUG */
    3116                                                 myType = 12;
    3117 #ifdef DEBUG
    3118 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3119 #endif /* DEBUG */
    3120                                                 }
    3121                                         else
    3122                                                 {
    3123                                                 if (strcmp(woordenboek[i].varType, "CA_DIRLIST") == 0 )
    3124                                                         {
    3125 #ifdef DEBUG
    3126 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3127 #endif /* DEBUG */
    3128                                                         myType = 13;
    3129 #ifdef DEBUG
    3130 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3131 #endif /* DEBUG */
    3132                                                         }
    3133                                                 else
    3134                                                         {
    3135                                                         if (strcmp(woordenboek[i].varType, "CA_BOOLEAN") == 0)
    3136                                                                 {
    3137 #ifdef DEBUG
    3138 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3139 #endif /* DEBUG */
    3140                                                                 myType = 14;
    3141 #ifdef DEBUG
    3142 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3143 #endif /* DEBUG */
    3144                                                                 }
    3145                                                         else
    3146                                                                 {
    3147                                                                 if (strcmp(woordenboek[i].varType, "CA_SOURCETYPE") == 0)
    3148                                                                         {
    3149 #ifdef DEBUG
    3150 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3151 #endif /* DEBUG */
    3152                                                                         myType = 15;
    3153 #ifdef DEBUG
    3154 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3155 #endif /* DEBUG */
    3156                                                                         }
    3157                                                                 else
    3158                                                                         {
    3159                                                                         if (strcmp(woordenboek[i].varType, "CA_ADMIN") == 0)
    3160                                                                                 {
    3161 #ifdef DEBUG
    3162 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3163 #endif /* DEBUG */
    3164                                                                         myType = 16;
    3165 #ifdef DEBUG
    3166 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3167 #endif /* DEBUG */
    3168
    3169                                                                                 }
    3170                                                                         else
    3171                                                                                 {
    3172                                                                                 if (strcmp(woordenboek[i].varType, "CA_UPDSOURCE") == 0)
    3173                                                                                         {
    3174 #ifdef  DEBUG
    3175 printf("ca_getType function: %s, %s\n", woordenboek[i].varName, woordenboek[i].varType);
    3176 #endif  /* DEBUG */
    3177                                                                                 myType = 17;
    3178 #ifdef DEBUG
    3179 printf("ca_getType function: %s, %s, %d\n", woordenboek[i].varName, woordenboek[i].varType, myType);
    3180 #endif /* DEBUG */
    3181                                                                                         }
    3182                                                                                 }
    3183                                                                         }
    3184                                                                 }
    3185                                                         }
    3186                                                 }
    3187                                 }
    3188                 }
    3189         else
    3190                 {
    3191                 myType = NOT_FOUND;
    3192                 }
    3193         return(myType);
    3194  }
    3195
    3196
    3197 ca_updDbSource_t *ca_get_UpdSourceHandle(int symbol)
         /* [<][>][^][v][top][bottom][index][help] */
    3198 /*******************************************************************
    3199  *ca_get_UpdSourceHandle                                                                                                                        *
    3200  * -- returns the handle to the Update source                                                           *
    3201         *                                                                                                                                                                               *
    3202         * Parameters                                                                                                                                            *
    3203         *       -- none; there is only one Update Source in the Configuration  *
    3204         *     file because a single DBupdate process cannot update more         *
    3205  *              than one source.                                                                                                                        *
    3206  *                                                                                                                                                                              *
    3207  * Returns                                                                                                                                                      *
    3208         *       -- a pointer to the Update Source structure (type                                       *
    3209  *              ca_updDbSource_t) or NULL.                                                                                              *
    3210  *                                                                                                                                                                              *
    3211  *******************************************************************/
    3212  {
    3213         ca_updDbSource_t *myUpdSourcePtr;
    3214
    3215  /*
    3216          * Make sure that we are calling the correct function.
    3217          */
    3218         if (strcmp(dictionary[symbol].varType, "CA_UPDSOURCE") != 0)
    3219                 {
    3220                 fprintf(stderr, "Error: unexpected variable type.\n");
    3221                 die;
    3222                 }
    3223         else
    3224                 {
    3225                  myUpdSourcePtr = (ca_updDbSource_t *)confVars[symbol].valPtr;
    3226                 }
    3227  return(myUpdSourcePtr);
    3228  }
    3229
    3230
    3231 char *ca_UpdSrcHandle2StrElement(ca_updDbSource_t *uh, int srcAttrib)
         /* [<][>][^][v][top][bottom][index][help] */
    3232 /*******************************************************************
    3233  * ca_UpdSrcHandle2StrElement                                                                                                   *
    3234  *      -- returns a string which represents the attribute of an                *
    3235  *              update source e.g. the name, the user, etc.                                             *
    3236  *              It allocates the required memory, but it returns NULL if    *
    3237  *              the required memory cannot be allocated.                                                        *
    3238  *                                                                                                                                                                              *
    3239  *                                                                                                                                                                              *
    3240  * Parameters                                                                                                                                           *
    3241  *      -- the Update Source Handle, i.e. a pointer to the structure    *
    3242  *              which contains the data about the Update Source.                                *
    3243         *                                                                                                                                                                               *
    3244  *      -- srcAttrib - an integer which represents the required                 *
    3245  *              attribute of the source.  This is also used in the                      *
    3246  *              ca_srchandle2Strelement() function.                                                                     *
    3247  *                                                                                                                                                                              *
    3248         * Returns                                                                                                                                                       *
    3249         *       -- a string or NULL                                                                                                                     *
    3250  *                                                                                                                                                                              *
    3251  *******************************************************************/
    3252 {
    3253 char *myStr;
    3254
    3255 if (uh == NULL)
    3256                 {
    3257                 fprintf(stderr, "ca_UpdSrcHandle2StrElement(): Cannot dereference NULL pointer.\n");
    3258                 die;
    3259                 }
    3260
    3261         switch(srcAttrib)
    3262                 {
    3263                 case 0:
    3264                 /* Update Source Name */
    3265                 myStr = strdup(uh->name);
    3266                 break;
    3267
    3268                 case 3:
    3269                 /* Machine */
    3270                 myStr = strdup((uh->updDb).host );
    3271                 break;
    3272
    3273                 case 5:
    3274                 /* User */
    3275                 myStr = strdup((uh->updDb).user );
    3276                 break;
    3277
    3278                 case 6:
    3279                 /* Password */
    3280                 myStr = strdup((uh->updDb).password);
    3281                 break;
    3282
    3283                 case 7:
    3284                 /* Update DB Name */
    3285                 myStr = strdup((uh->updDb).dbName);
    3286                 break;
    3287
    3288                 case 15:
    3289                 /* Update Source Whois Machine */
    3290                 myStr = strdup((uh->whoisd_host));
    3291                 break;
    3292
    3293                 default:
    3294                 puts("Cannot find this Update source attribute");
    3295                 break;
    3296                 }
    3297
    3298 return(myStr);
    3299 }
    3300
    3301
    3302 int ca_UpdSrcHandle2IntElement(ca_updDbSource_t *uh, int srcAttrib)
         /* [<][>][^][v][top][bottom][index][help] */
    3303 /*******************************************************************
    3304  * ca_UpdSrcHandle2IntElement                                                                                                   *
    3305  *      -- a function that returns the integer value of the requested   *
    3306         *               attribute of the given source.                                                                          *
    3307         *                                                                                                                                                                               *
    3308         * Parameters                                                                                                                                            *
    3309         *       -- the Update Source Handle, i.e. a pointer to the structure    *
    3310         *               which contains the data about the Update Source.            *
    3311  *
    3312  *      -- srcAttrib - an integer which represents the required                 *
    3313  *              attribute of the source.  This is also used in the                      *
    3314  *              ca_srchandle2Strelement() function.                                                                     *
    3315  *                                                                                                                                                                              *
    3316  * Returns                                                                                                                                                      *
    3317  *      -- an integer.
    3318  *******************************************************************/
    3319 {
    3320
    3321 int myInt;      /* The value of this integer is returned. */
    3322
    3323 if(uh == NULL)
    3324                 {
    3325                 fprintf(stderr, "ca_srchandle2Intelement(): Cannot dereference NULL pointer\n");
    3326                 die;
    3327                 }
    3328
    3329         switch(srcAttrib)
    3330                 {
    3331
    3332                 case 4:
    3333                 /* Update Source DB Port */
    3334                 myInt = (uh->updDb).port;
    3335                 break;
    3336
    3337                 case 16:
    3338                 /* Update Source QRY Port */
    3339                 myInt = (uh->qryPort);
    3340                 break;
    3341
    3342                 case 17:
    3343                 /* Update Source UPD Port */
    3344                 myInt = (uh->updPort);
    3345                 break;
    3346
    3347                 default:
    3348                 fprintf(stderr, "Could not find source-attribute %d\n", srcAttrib);
    3349                 die;
    3350     break;
    3351                 }
    3352
    3353 return (myInt);
    3354
    3355 }
    3356
    3357 /* void ca_init(dict_t theDict[], const char *configFile, values_t configVars[], int varNo) */
    3358 /*
    3359  * ca_init() -- Initialisation function.
    3360  */
    3361 /*
    3362  * {
    3363  * char sourcesFile[80];
    3364  *
    3365  * ca_populateDictionary(theDict, varNo);
    3366  * ca_readConfig(configFile, configVars, varNo);
    3367  *
    3368  * sourcesFile = ca_get_dirlist(CA_SOURCEFILE);
    3369  * ca_readSources(sourcesFile, confVars);
    3370  * }
    3371  */
    3372

/* [<][>][^][v][top][bottom][index][help] */