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
  35. ca_sanityChk
  36. ca_mandVarChk

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

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