Source File modules/ca/ca_sourceLoader.c

    1    | /***************************************
    2    |   $Revision:
    3    |
    4    |   CA module: definitions of functions that read a file of databases and
    5    |              sources.
    6    |
    7    |   Status: NOT REVIEWED, NOT TESTED
    8    |
    9    |   Author(s):       Ambrose Magee
    10   |
    11   | ******************/ /******************
    12   | Modification History:
    13   |
    14   | ******************/
    15   |
    16   | /************************************
    17   | Copyright (c) 2000                              RIPE NCC
    18   |
    19   | All Rights Reserved
    20   |
    21   | Permission to use, copy, modify, and distribute this software and its
    22   | documentation for any purpose and without fee is hereby granted,
    23   | provided that the above copyright notice appear in all copies and that
    24   | both that copyright notice and this permission notice appear in
    25   | supporting documentation, and that the name of the author not be
    26   | used in advertising or publicity pertaining to distribution of the
    27   | software without specific, written prior permission.
    28   |
    29   | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
    30   | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
    31   | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
    32   | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
    33   | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    34   | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    35   | ***************************************/
    36   |
    37   | #include <stdio.h>
    38   | #include <stdlib.h>
    39   | #include <glib.h>
    40   | #include <string.h>
    41   | #include <stubs.h>
    42   | #include <unistd.h>
    43   | #include "ca_configFns.h"
    44   |
    45   | /* #define DEBUG */	/* Swich OFF Debugging. */
    46   |
    47   | /*******************************************************
    48   | 	* This file contains the definitions of functions 		*
    49   |  * that read a file of databases and sources.				*
    50   |  *******************************************************/
    51   |
    52   |
    53   | void ca_readSources(const char *sourcesDefFile, values_t confVars[])
    54   | /*******************************************************************
    55   |  * 																						*
    56   |  * ca_readSources -- parses the Sources file and writes the values	*
    57   |  *						  into memory.												*
    58   |  *																						*
    59   |  * Parameters																		*
    60   | 	*		sourcesFile	-- the file of databases, mirrors, sources		*
    61   | 	*		confVars[]	--	the array of values structures					*
    62   |  *																						*
    63   |  * Returns																			*
    64   | 	*		Nothing -- perhaps make this return 0 on successful exit ?	*
    65   |  *																						*
    66   |  *******************************************************************/
    67   | {
    68   | FILE *sourcesFilePtr;				/* Pointer to Source file. */
    69   | char line[80];					/* The current line of input. */
    70   | const char *comment = "#";     /* Declared as a string. */
    71   | #ifdef DEBUG
    72   | char name[STRLENGTH_M];     /* The name of the config variable */
    73   | char value[STRLENGTH_XXL];        /* The value of the variable */
    74   | 												/* 640 characters */
    75   |
    76   | int location;				/* Storage Location of the variable's value. */
    77   | int type;		/* Data type of the variable, represented by an integer. */
    78   |
    79   | const char *blankLine = "\n";  /* Declared as a string, not a character. */
    80   |
    81   | char dbcomp[16];									/* Component of a databse. */
    82   | char nrtmcomp[16];									/* Component of an nrtm. */
    83   | int mode;                      /* The mode of operation of the src */
    84   | char srcOptions[16];           /* The options of a source. */
    85   | char nrtMirror[STRLENGTH_M];         /* The elements of a NRTM */
    86   | int updatePort;                /* The update port of the source */
    87   | 											/* N. B.  This is not the same as the */
    88   | 											/* updPort in the UPDSOURCE variables. */
    89   | #endif /* DEBUG */
    90   | char source[16];                           /* The name of a source. */
    91   | char database[16];       						/* The name of a database. */
    92   | char mirror[16];									/* The name of a mirror. */
    93   | char varName[16];									/* Temporary variable */
    94   |
    95   |
    96   |
    97   | ca_database_t *newDbPtr;    /* A pointer to a new instance of */
    98   | 										/* ca_database_t.                 */
    99   |
    100  | ca_mirror_t *newMirrPtr;    /* A pointer to a new instance of */
    101  | 										/* ca_mirror_t.                   */
    102  |
    103  | ca_dbSource_t *newSrcPtr;   /* A pointer to a new instance of */
    104  | 										/* ca_database_list_t.            */
    105  |
    106  | int in_DATABASE_def, in_NRTM_def, in_SOURCE_def;
    107  | 									 	/* When we are reading the definition */
    108  | 										/* of a database, nrtm, source, etc.  */
    109  | 										/* this is set to 1.  Otherwise, it */
    110  | 										/* is 0. */
    111  |
    112  | /*
    113  | 	* Function prototypes of ca_parseDbLine(), ca_parseNrtmLine()
    114  |  * and ca_parseSrcLine().  We put them here so that it can only
    115  |  * be called from within the ca_readSources() function.
    116  |  */
    117  | void ca_parseDbLine(char *, ca_database_t *);
    118  | void ca_parseNrtmLine(char *, ca_mirror_t *);
    119  | void ca_parseSrcLine(char *, ca_dbSource_t *);
    120  |
    121  |
    122  | #ifdef DEBUG
    123  | printf("\nInside ca_readSources() function.\n");
    124  | printf("Sources file is: %s\n", sourcesDefFile);
    125  | #endif	/* DEBUG */
    126  |
    127  | /*
    128  |  * Open the sources file for reading .....
    129  |  */
    130  | if ( (sourcesFilePtr = fopen(sourcesDefFile, "r")) == NULL)
    131  | 		{
    132  | 		printf("Error: the file %s could not be opened.\n", sourcesDefFile);
    133  | 		die;
    134  | 		}
    135  |
    136  | /* Before reading the file, we initialise all flags to 0. */
    137  | in_DATABASE_def = 0;
    138  | in_NRTM_def = 0;
    139  | in_SOURCE_def = 0;
    140  |
    141  | /*
    142  |  * Read the first line of the file.
    143  |  * Remove leading and trailing blank-space characters.
    144  |  * if the first character of the line is a comment or if it is a
    145  |  * blank-line, continue.
    146  |  *
    147  |  */
    148  |
    149  | fgets(line, sizeof(line), sourcesFilePtr);
    150  | g_strstrip(line);
    151  |
    152  | /*
    153  | 	* While there are lines to be read in the sources file,
    154  |  * process the current line and
    155  |  * read the next line.
    156  |  */
    157  |
    158  | while (!feof(sourcesFilePtr) )
    159  | 		{
    160  | 		#ifdef DEBUG
    161  | 		printf("line:%s:End of line\n", line);
    162  | 		printf("Length of line: %d\n", strlen(line));
    163  | 		#endif	/* DEBUG */
    164  |
    165  | 		/*
    166  | 		 * Ignore comments and empty lines.
    167  |      */
    168  | 		if ( (strncmp(line, comment, 1) == 0) || (strlen(line) == 0) )
    169  | 			{
    170  | #ifdef DEBUG
    171  | printf("We are reading a comment or an empty line ..... \n");
    172  | #endif /* DEBUG */
    173  | 			fgets(line, sizeof(line), sourcesFilePtr);
    174  | 			g_strstrip(line);
    175  | 			continue;
    176  | 		   }
    177  |
    178  | /* Testing */
    179  | #ifdef DEBUG
    180  | printf("LINE >>>%sEND_OF_LINE\n", line);
    181  | #endif	/* DEBUG */
    182  |
    183  | 			/* if we are in a DATABASE definition
    184  | 			 * then have we read all of the definition ?
    185  | 			 * if yes, then commit the definition, reset the 'Database'
    186  |         * flag and read the next line.
    187  | 			 * Otherwise parse the line and store the details in temporary
    188  | 			 * variables.
    189  | 			 */
    190  |
    191  | 			/* If we are in a DATABASE definition */
    192  | 			if (in_DATABASE_def)
    193  | 				{
    194  | 					/* If we have reached the end of a DATABASE definition,
    195  | 					 * commit the definition.
    196  | 					 */
    197  |
    198  | 					if (strcmp(line, "/DATABASE")== 0)
    199  | 						{
    200  | 						/* Commit the definition */
    201  | 						/* Some code. */
    202  | #ifdef DEBUG
    203  | puts("We have reached the end of a DATABASE definition");
    204  | puts("Testing the population of the db structure:");
    205  | printf("\n%s::%d::%s::%s::%s\n", newDbPtr->host, newDbPtr->port, newDbPtr->user, newDbPtr->password, newDbPtr->dbName);
    206  | #endif	/* DEBUG */
    207  |
    208  | 						/* Commit the definition to the linked list of Databases. */
    209  |
    210  | 						dbList = g_slist_append(dbList, newDbPtr);
    211  |
    212  | 						/* We have reached the end of the DATABASE definition */
    213  | 						/* Thus, reset the flag and free some memory. */
    214  | 						in_DATABASE_def = 0;
    215  |
    216  |
    217  | 						/* Read the next line and do the conditional test. */
    218  | 						fgets(line, sizeof(line), sourcesFilePtr);
    219  | 						g_strstrip(line);
    220  | 						continue;
    221  | 						}
    222  |
    223  | 				/*
    224  |            * Otherwise, parse the line and fill in the structure of
    225  | 				 * the Database.
    226  | 				 */
    227  | 				ca_parseDbLine(line, newDbPtr);
    228  |
    229  | 				}
    230  |
    231  | 			/* If we have found the _beginning_ of a Database definition,
    232  | 			 * then set the in_DATABASE_def flag and allocate space, etc.
    233  | 			 * for the database.
    234  | 			 */
    235  |
    236  | 			if ( (!in_DATABASE_def) && (strncmp(line, DATABASE_KEY, strlen(DATABASE_KEY) )== 0 ) )
    237  | 				{
    238  | 					in_DATABASE_def = 1;
    239  |
    240  | 					/* Allocate space for the database */
    241  | 					/* Current_db = fscanf etc.) */
    242  | 					/* Fill in the defaults. */
    243  | #ifdef DEBUG
    244  | puts("Beginning of a database defintion ..... ");
    245  | #endif	/* DEBUG */
    246  |
    247  | 					sscanf(line, "%s %s", varName, database);
    248  | 					g_strstrip(database);
    249  |
    250  | #ifdef DEBUG
    251  | printf("Database name is: %s\n", database);
    252  | #endif	/* DEBUG */
    253  |
    254  | 					/*
    255  | 					 * Create a structure for the database.
    256  | 					 */
    257  | 					newDbPtr = calloc(1,sizeof(ca_database_t));
    258  | 					if (newDbPtr == NULL)
    259  | 						{
    260  | 						fprintf(stderr, "Cannot allocate memory to new db structure\n");
    261  | 						die;
    262  | 						}
    263  |
    264  | 					/* Assign the name of the database */
    265  | 					strcpy(newDbPtr->dbName, database);
    266  |
    267  | 				}
    268  |
    269  |
    270  | 		/********************
    271  | 		 * NRTM definition  *
    272  | 		 ********************/
    273  |
    274  | 			/* if we are in a NRTM definition
    275  | 			 * then have we read all of the definition ?
    276  | 			 * if yes, then commit the definition and read the next line.
    277  | 			 * otherwise parse the line and store the details in temporary
    278  | 			 * variables.
    279  | 			 */
    280  |
    281  | 			/* If we are in a NRTM definition */
    282  | 			if (in_NRTM_def)
    283  | 				{
    284  | 					/* If we have reached the end of a NRTM definition,
    285  | 					 * commit the definition.
    286  | 					 */
    287  | 					if (strcmp(line, "/NRTM")== 0)
    288  | 						{
    289  | 						/* Commit the definition */
    290  | 						/* Some code. */
    291  | #ifdef DEBUG
    292  | puts("We have reached the end of a NRTM definition");
    293  | puts("Testing the population of the mirror structure:");
    294  | printf("\n%s::%d::%d::%d\n", newMirrPtr->host, newMirrPtr->port, newMirrPtr->delay, newMirrPtr->protocolVer);
    295  | #endif	/* DEBUG */
    296  |
    297  | 						/* Commit the definition to the linked list of nrt-mirrors. */
    298  |
    299  | 						nrtmList = g_slist_append(nrtmList, newMirrPtr);
    300  |
    301  | 						/* We have reached the end of the NRTM definition */
    302  | 						/* Thus, reset the NRTM flag. */
    303  | 						in_NRTM_def = 0;
    304  |
    305  | 						/* Read the next line and do the conditional test. */
    306  | 						fgets(line, sizeof(line), sourcesFilePtr);
    307  | 						g_strstrip(line);
    308  | 						continue;
    309  | 						}
    310  |
    311  | 				/*
    312  |            * Otherwise, parse the line and fill in the structure of
    313  | 				 * the NRMT.
    314  | 				 */
    315  |
    316  | 				ca_parseNrtmLine(line, newMirrPtr);
    317  | 				}
    318  |
    319  | 				/* If we have found the beginning of a Near-Real-Time-Mirror
    320  | 				 * definition, then set the in_NRTM_def flag and allocate
    321  | 				 *	space, etc. for the Near-Real-Time-Mirror.
    322  | 				 */
    323  |
    324  | 				if ( (!in_NRTM_def) && (strncmp(line, NRTM_KEY, strlen(NRTM_KEY) )== 0 ) )
    325  | 					{
    326  | 					in_NRTM_def = 1;
    327  | 					/* Allocate space for the Near-Real-Time-Mirror. */
    328  | 					/* Current_db = fscanf etc.) */
    329  | 					/* Fill in the defaults. */
    330  | #ifdef DEBUG
    331  | puts("Beginning of a Near-Real-Time-Mirror defintion ..... ");
    332  | #endif	/* DEBUG */
    333  |
    334  | 					sscanf(line, "%s %s", varName, mirror);
    335  |
    336  | 					/*
    337  | 					 * Create a structure for the mirror.
    338  | 					 */
    339  | 					newMirrPtr = calloc(1,sizeof(ca_mirror_t));
    340  | 					if (newMirrPtr == NULL)
    341  | 						{
    342  | 						fprintf(stderr, "Cannot allocate memory to new nrtm structure\n");
    343  | 						die;
    344  | 						}
    345  | 					/* Assign the name of the mirror ? */
    346  | 					strcpy(newMirrPtr->mrName, mirror);
    347  |
    348  | 					}
    349  |
    350  | 		/*********************
    351  | 		 * SOURCE Definition *
    352  | 		 *********************/
    353  |
    354  | 			/* if we are in a SOURCE definition
    355  | 			 * then have we read all of the definition ?
    356  | 			 * if yes, then commit the definition, reset the 'Database'
    357  |         * flag and read the next line.
    358  | 			 * Otherwise parse the line and store the details in temporary
    359  | 			 * variables.
    360  | 			 */
    361  |
    362  | 			/* If we are in a SOURCE definition */
    363  | 			if (in_SOURCE_def)
    364  | 				{
    365  | 					/* If we have reached the end of a SOURCE definition,
    366  | 					 * commit the definition.
    367  | 					 */
    368  |
    369  | 					if (strcmp(line, "/SOURCE")== 0)
    370  | 						{
    371  | 						/* Commit the definition */
    372  | 						/* Some code. */
    373  | #ifdef DEBUG
    374  | puts("We have reached the end of a SOURCE definition");
    375  | puts("Testing the population of the new Source structure:");
    376  | printf("Source name: %s\n", newSrcPtr->name);
    377  | printf("\nDB == %s::%d::%s::%s::%s\n", (newSrcPtr->db).host, (newSrcPtr->db).port, (newSrcPtr->db).user, (newSrcPtr->db).password, (newSrcPtr->db).dbName);
    378  | printf("Mode: %d\n", newSrcPtr->opMode);
    379  | printf("NRTM == %s::%d::%d:%d\n", (newSrcPtr->nrtm).host, (newSrcPtr->nrtm).port, (newSrcPtr->nrtm).delay, (newSrcPtr->nrtm).protocolVer);
    380  | printf("UpdPort: %d\n", newSrcPtr->updPort);
    381  | printf("New Source Options == %s::%s\n", newSrcPtr->canupd, newSrcPtr->deflook);
    382  | #endif	/* DEBUG */
    383  |
    384  | 						/* Commit the definition to the linked list of Databases. */
    385  |
    386  | 						sourceList = g_slist_append(sourceList, newSrcPtr);
    387  |
    388  | 						/* We have reached the end of the DATABASE definition */
    389  | 						/* Thus, reset the flag and free some memory. */
    390  | 						in_SOURCE_def = 0;
    391  |
    392  |
    393  | 						/* Read the next line and do the conditional test. */
    394  | 						fgets(line, sizeof(line), sourcesFilePtr);
    395  | 						g_strstrip(line);
    396  | 						continue;
    397  | 						}
    398  |
    399  | 				/*
    400  |            * Otherwise, parse the line and fill in the structure of
    401  | 				 * the Database.
    402  | 				 */
    403  | 				ca_parseSrcLine(line, newSrcPtr);
    404  |
    405  | 				}
    406  |
    407  | 			/* If we have found the _beginning_ of a SOURCE definition,
    408  | 			 * then set the in_SOURCE_def flag and allocate space, etc.
    409  | 			 * for the database.
    410  | 			 */
    411  |
    412  | 			if ( (!in_SOURCE_def) && (strncmp(line, SOURCE_KEY, strlen(SOURCE_KEY) )== 0 ) )
    413  | 				{
    414  | 					in_SOURCE_def = 1;
    415  |
    416  | 					/* Allocate space for the Source */
    417  | 					/* Current_source = fscanf etc.) */
    418  | 					/* Fill in the defaults. */
    419  | #ifdef DEBUG
    420  | puts("Beginning of a Source defintion ..... ");
    421  | #endif	/* DEBUG */
    422  |
    423  | 					sscanf(line, "%s %s", varName, source);
    424  | 					g_strstrip(source);
    425  |
    426  | #ifdef DEBUG
    427  | printf("Source name is: %s\n", source);
    428  | #endif	/* DEBUG */
    429  |
    430  | 					/*
    431  | 					 * Create a structure for the source.
    432  | 					 *
    433  | 					 */
    434  | 					newSrcPtr = calloc(1,sizeof(ca_dbSource_t));
    435  | 					if (newSrcPtr == NULL)
    436  | 						{
    437  | 						fprintf(stderr, "Cannot allocate memory to new Source structure\n");
    438  | 						die;
    439  | 						}
    440  |
    441  | 					/* Assign the name of the Source */
    442  | 					strcpy(newSrcPtr->name, source);
    443  |
    444  | 				}
    445  |
    446  | 		/* Read the next line. */
    447  | 		fgets(line, sizeof(line), sourcesFilePtr);
    448  | 		g_strstrip(line);
    449  |
    450  | 		/* End of while loop; i.e. end of processing a line. */
    451  | 		}
    452  |
    453  | /* Close the sources definition file. */
    454  | fclose(sourcesFilePtr);
    455  |
    456  | /* End of ca_readSources() function */
    457  |
    458  | }
    459  |
    460  | void ca_getAllDatabases(GSList *databases)
    461  | {
    462  | GSList *currentPtr;   /* Pointer to the structure at which we look. */
    463  |
    464  | /*
    465  | 	* Look at the first member of the linked-list of sources.
    466  |  */
    467  | currentPtr = databases;
    468  |
    469  | /*
    470  | 	* Look at each data component of the source list,
    471  |  * untill we reach the end of the list.
    472  |  */
    473  | while(currentPtr != NULL)
    474  | 	{
    475  |  ca_database_t *dbPtr = currentPtr->data;
    476  |  printf("\n%s,%d,%s,%s,%s\n", dbPtr->host, dbPtr->port, dbPtr->user, dbPtr->password,  dbPtr->dbName);
    477  | currentPtr = currentPtr->next;
    478  | 	}
    479  | }
    480  |
    481  |
    482  | void ca_getAllMirrors(GSList *mirrors)
    483  | {
    484  | GSList *currentPtr;   /* Pointer to the structure at which we look. */
    485  |
    486  | /*
    487  | 	* Look at the first member of the linked-list of sources.
    488  |  */
    489  | currentPtr = mirrors;
    490  |
    491  | /*
    492  | 	* Look at each data component of the source list,
    493  |  * untill we reach the end of the list.
    494  |  */
    495  | while(currentPtr != NULL)
    496  | 	{
    497  |  ca_mirror_t *nrtmPtr = currentPtr->data;
    498  |  printf("\n%s,%d,%d,%d, %s\n", nrtmPtr->host, nrtmPtr->port, nrtmPtr->delay,  nrtmPtr->protocolVer, nrtmPtr->mrName);
    499  | currentPtr = currentPtr->next;
    500  | 	}
    501  | }
    502  |
    503  | void ca_parseDbLine(char *lineStr, ca_database_t *dbStructPtr)
    504  | /*******************************************************************
    505  |  * 																						*
    506  |  * ca_parseLine  -- parses the a line in the Sources file and 		*
    507  |  *						 writes the values into temporary variables.		*
    508  |  *																						*
    509  |  * Parameters																		*
    510  | 	*		lineStr 		-- the current line of the Sources file 			*
    511  |  * 							-- a NULL terminated string						*
    512  | 	*		dbStructPtr	-- the db we are filling 								*
    513  | 	*							-- a pointer to a ca_database_t structure.	*
    514  |  *																						*
    515  |  * Returns																			*
    516  | 	*		Nothing -- perhaps make this return 0 on successful exit ?	*
    517  |  *																						*
    518  |  *******************************************************************/
    519  | {
    520  | char dbComp[64];						/* Component of a database. */
    521  | char varName[16];						/* The name of the variable. */
    522  |
    523  | gchar **tokens;                   /* Pointer to an array of strings. */
    524  |
    525  | #ifdef DEBUG
    526  | int i;										/* A counting variable. */
    527  | #endif /* DEBUG */
    528  |
    529  | /*
    530  | 	* Split the line on the ':' character.
    531  |  * Then, for both the name of the variable and its value,
    532  |  * remove leading and trailing blank-space characters.
    533  |  */
    534  | tokens = g_strsplit(lineStr, ":", 0);
    535  |
    536  | #ifdef DEBUG
    537  | for (i = 0; tokens[i] != NULL; i++)
    538  | 		printf("tokens[%d] = %s\n", i, tokens[i]);
    539  | #endif	/* DEBUG */
    540  |
    541  | strcpy(varName, tokens[0]);
    542  | strcpy(dbComp, tokens[1]);
    543  |
    544  | /* Free the memory used by the tokens array. */
    545  | g_strfreev(tokens);
    546  |
    547  | /* Remove leading and trailing blank-space characters. */
    548  | g_strstrip(varName);
    549  | g_strstrip(dbComp);
    550  |
    551  | #ifdef DEBUG
    552  | printf("VarName: %s; dbComp: %s\n", varName, dbComp);
    553  | #endif	/* DEBUG */
    554  |
    555  | 				if (strcmp(varName, "host") == 0)
    556  | 					{
    557  | 					strcpy(dbStructPtr->host, dbComp);
    558  | 					}
    559  |  				else
    560  |  					{
    561  |  					if (strcmp(varName, "port") == 0)
    562  |  						{
    563  |  						dbStructPtr->port = atoi(dbComp);
    564  |  						}
    565  |  					else
    566  |  						{
    567  |  						if (strcmp(varName, "user") == 0)
    568  |  							{
    569  |  							strcpy(dbStructPtr->user, dbComp);
    570  |  							}
    571  |  						else
    572  |  							{
    573  |  							if (strcmp(varName, "password") == 0)
    574  |  								{
    575  |  								strcpy(dbStructPtr->password, dbComp);
    576  |  								}
    577  | 								else
    578  | 									{
    579  | 									fprintf(stderr, "Unknown database component \"%s\".\n", dbComp);
    580  | 									die;
    581  | 									}
    582  |  							}
    583  |  						}
    584  |  					}
    585  | }
    586  |
    587  |
    588  |
    589  | void ca_parseNrtmLine(char *lineStr, ca_mirror_t *mrStructPtr)
    590  | /*
    591  |  */
    592  | {
    593  | char nrtmComp[64];						/* Component of a NRTM. */
    594  | char varName[16];						/* The name of the variable. */
    595  |
    596  | gchar **tokens;                   /* Pointer to an array of strings. */
    597  |
    598  | #ifdef DEBUG
    599  | int i;										/* A counting variable. */
    600  | #endif	/* DEBUG */
    601  |
    602  | /*
    603  | 	* Split the line on the ':' character.
    604  |  * Then, for both the name of the variable and its value,
    605  |  * remove leading and trailing blank-space characters.
    606  |  */
    607  | tokens = g_strsplit(lineStr, ":", 0);
    608  |
    609  | #ifdef DEBUG
    610  | for (i = 0; tokens[i] != NULL; i++)
    611  | 		printf("tokens[%d] = %s\n", i, tokens[i]);
    612  | #endif	/* DEBUG */
    613  |
    614  | strcpy(varName, tokens[0]);
    615  | strcpy(nrtmComp, tokens[1]);
    616  |
    617  | /* Free the memory used by the tokens array. */
    618  | g_strfreev(tokens);
    619  |
    620  | /* Remove leading and trailing blank-space characters. */
    621  | g_strstrip(varName);
    622  | g_strstrip(nrtmComp);
    623  |
    624  | #ifdef DEBUG
    625  | printf("VarName: %s; nrtmComp: %s\n", varName, nrtmComp);
    626  | #endif	/* DEBUG */
    627  |
    628  |
    629  | if (strcmp(varName, "host") == 0)
    630  | 		{
    631  |  	strcpy(mrStructPtr->host, nrtmComp);
    632  |  	}
    633  | else
    634  | 		{
    635  | 		if (strcmp(varName, "port") == 0)
    636  | 			{
    637  | 			mrStructPtr->port = atoi(nrtmComp);
    638  | 			}
    639  | 		else
    640  | 			{
    641  | 			if (strcmp(varName, "delay") == 0)
    642  | 				{
    643  | 				mrStructPtr->delay = atoi(nrtmComp);
    644  | 				}
    645  | 				else
    646  | 					{
    647  | 					if (strcmp(varName, "protocolVersion") == 0)
    648  | 						{
    649  | 						mrStructPtr->protocolVer = atoi(nrtmComp);
    650  | 						}
    651  | 					else
    652  | 						{
    653  | 						fprintf(stderr, "Unknown mirror component \"%s\".\n", nrtmComp);
    654  | 						die;
    655  | 						}
    656  | 					}
    657  | 				}
    658  |  		}
    659  | 		}
    660  |
    661  |
    662  |
    663  |
    664  | void ca_parseSrcLine(char *lineStr, ca_dbSource_t *srcStructPtr)
    665  | /*
    666  | 	* ca_parseSrcLine() function.
    667  |  */
    668  | {
    669  | char srcComp[64];						/* Component of a database. */
    670  | char varName[16];						/* The name of the variable. */
    671  |
    672  | gchar **tokens;                   /* Pointer to an array of strings. */
    673  |
    674  | #ifdef DEBUG
    675  | int i;										/* A counting variable. */
    676  | #endif /* DEBUG */
    677  |
    678  | /*
    679  | 	* Split the line on the ':' character.
    680  |  * Then, for both the name of the variable and its value,
    681  |  * remove leading and trailing blank-space characters.
    682  |  */
    683  | tokens = g_strsplit(lineStr, ":", 0);
    684  |
    685  | #ifdef DEBUG
    686  | for (i = 0; tokens[i] != NULL; i++)
    687  | 		printf("tokens[%d] = %s\n", i, tokens[i]);
    688  | #endif	/* DEBUG */
    689  |
    690  | strcpy(varName, tokens[0]);
    691  | strcpy(srcComp, tokens[1]);
    692  |
    693  | /* Free the memory used by the tokens array. */
    694  | g_strfreev(tokens);
    695  |
    696  | /* Remove leading and trailing blank-space characters. */
    697  | g_strstrip(varName);
    698  | g_strstrip(srcComp);
    699  |
    700  | #ifdef DEBUG
    701  | printf("VarName: %s; srcComp: %s\n", varName, srcComp);
    702  | #endif	/* DEBUG */
    703  |
    704  | 				/*
    705  | 				 * Parse each line of the SOURCE definition.
    706  | 				 * If we find a database or a mirror, search for it in
    707  | 				 * the appropriate linked list and make this source
    708  |   			 * point to it.
    709  | 				 */
    710  | 				if (strcmp(varName, "database") == 0)
    711  | 					{
    712  | 					/* Search for the appropriate database. */
    713  | 					/* Make this source point to it. */
    714  | 					/* Use ca_getDbHandleByName(). */
    715  | 					srcStructPtr->db = *ca_getDbHandleByName(srcComp);
    716  | 					}
    717  |  				else
    718  |  					{
    719  |  					if (strcmp(varName, "opMode") == 0)
    720  |  						{
    721  |  						srcStructPtr->opMode = atoi(srcComp);
    722  |  						}
    723  |  					else
    724  |  						{
    725  |  						if (strcmp(varName, "updPort") == 0)
    726  |  							{
    727  |  							srcStructPtr->updPort = atoi(srcComp);
    728  |  							}
    729  |  						else
    730  |  							{
    731  |  							if (strcmp(varName, "canupd") == 0)
    732  |  								{
    733  |  								strcpy(srcStructPtr->canupd, srcComp);
    734  |  								}
    735  | 								else
    736  | 									{
    737  | 										if (strcmp(varName, "deflook") == 0)
    738  | 											{
    739  | 										 	strcpy(srcStructPtr->deflook, srcComp);
    740  | 											}
    741  | 										else
    742  | 											{
    743  | 											if (strcmp(varName, "nrtm") == 0)
    744  | 												{
    745  | 												/* Get Mirror Handle by Name */
    746  | 												/* Assign this mirror to */
    747  | 												/* srcStructPtr->nrtm. */
    748  | 												srcStructPtr->nrtm = *ca_getNrtmHandleByName(srcComp);
    749  | 												}
    750  | 											else
    751  | 												{
    752  | 											fprintf(stderr, "Unknown SOURCE component \"%s\".\n", srcComp);
    753  | 									die;
    754  | 												}
    755  | 											}
    756  | 									}
    757  |  							}
    758  |  						}
    759  |  					}
    760  | }
    761  |
    762  |
    763  | ca_database_t *ca_getDbHandleByName(char *databaseNameStr)
    764  | /*******************************************************************
    765  |  * ca_getDbHandleByName															*
    766  |  * 	-- A function that compares each 'name' component of every     *
    767  |  *		element in the linked-list of databases with the name of 	*
    768  |  *		the database to be found.  If the required database is 		*
    769  |  *		found, a pointer to the structure representing this 			*
    770  |  * 		database is	returned.													*
    771  |  *																						*
    772  |  * 	Parameters																		*
    773  |  *	-- databaseNameStr - the name of the required database				*
    774  |  *																						*
    775  |  *	Returns																			*
    776  | 	*	-- dbasePtr	- a pointer to the structure representing the 		*
    777  | 	*					  database or a pointer to NULL, if we cannot 		*
    778  |  *					  find the database.											*
    779  |  *																						*
    780  |  *******************************************************************/
    781  | {
    782  | /*
    783  |  * Define a pointer to the current element in the linked list.
    784  |  * Initialise it to the start of the list.
    785  |  */
    786  | GSList *currentPtr = dbList;
    787  |
    788  | /*
    789  | 	* Define and initialise a pointer that points to the 'data'
    790  |  * component of the GSList struct; i.e. a pointer to a
    791  |  * variable of type ca_database_t.
    792  |  */
    793  | ca_database_t *dbasePtr = currentPtr->data;
    794  |
    795  | /*
    796  | 	* Look at each data component of the list of databases;
    797  |  * (each data component is a structure of type ca_database_t).
    798  |  * Compare the 'name' component of each ca_database_t structure
    799  |  * with the value of databaseName untill we get a match or we
    800  |  * reach the end of the list.
    801  |  */
    802  |
    803  |    /*
    804  |     * We first check if currentPtr is pointing to NULL;
    805  |     *    if yes, we exit the while loop;
    806  |     *    if no, we make dbasePtr point to the data component
    807  |     *    of the current ca_database_t structure;
    808  |     *       then, we check if this is the database name that we want;
    809  |     *       if yes, we _break_ from the while loop.
    810  |  */
    811  |    while (currentPtr != NULL)
    812  |       {
    813  |       dbasePtr= currentPtr->data;
    814  |    if (strcmp(dbasePtr->dbName, databaseNameStr) == 0 )
    815  |          break;
    816  |       currentPtr = currentPtr->next;
    817  |       }
    818  |
    819  |    /*
    820  |     * We return a pointer.  If we found the database, this pointer
    821  |     * points to the ca_database_t structure which represents the
    822  |     * database.
    823  |     * If we did not find the database, we return a pointer to NULL.
    824  |     */
    825  |    if (currentPtr == NULL)
    826  |       {
    827  |       dbasePtr = NULL;
    828  |       return(dbasePtr);
    829  |       }
    830  |  else
    831  |       {
    832  |       return(dbasePtr);
    833  |       }
    834  |
    835  | }
    836  |
    837  |
    838  |
    839  | ca_mirror_t *ca_getNrtmHandleByName(char *nrtmNameStr)
    840  | /*******************************************************************
    841  |  * ca_NrtmHandleByName															*
    842  |  * 	-- A function that compares each 'name' component of every     *
    843  |  *		element in the linked-list of databases with the name of 	*
    844  |  *		the database to be found.  If the required database is 		*
    845  |  *		found, a pointer to the structure representing this 			*
    846  |  * 		database is	returned.													*
    847  |  *																						*
    848  |  * 	Parameters																		*
    849  |  *	-- nrtmNameStr - the name of the required database				*
    850  |  *																						*
    851  |  *	Returns																			*
    852  | 	*	-- nrtmPtr	- a pointer to the structure representing the 		*
    853  | 	*					  database or a pointer to NULL, if we cannot 		*
    854  |  *					  find the database.											*
    855  |  *																						*
    856  |  *******************************************************************/
    857  | {
    858  | /*
    859  |  * Define a pointer to the current element in the linked list.
    860  |  * Initialise it to the start of the list.
    861  |  */
    862  | GSList *currentPtr = nrtmList;
    863  |
    864  | /*
    865  | 	* Define and initialise a pointer that points to the 'data'
    866  |  * component of the GSList struct; i.e. a pointer to a
    867  |  * variable of type ca_database_t.
    868  |  */
    869  | ca_mirror_t *nrtmPtr = currentPtr->data;
    870  |
    871  | /*
    872  | 	* Look at each data component of the list of databases;
    873  |  * (each data component is a structure of type ca_database_t).
    874  |  * Compare the 'name' component of each ca_database_t structure
    875  |  * with the value of databaseName untill we get a match or we
    876  |  * reach the end of the list.
    877  |  */
    878  |
    879  |    /*
    880  |     * We first check if currentPtr is pointing to NULL;
    881  |     *    if yes, we exit the while loop;
    882  |     *    if no, we make nrtmPtr point to the data component
    883  |     *    of the current ca_database_t structure;
    884  |     *       then, we check if this is the database name that we want;
    885  |     *       if yes, we _break_ from the while loop.
    886  |  */
    887  |    while (currentPtr != NULL)
    888  |       {
    889  |       nrtmPtr= currentPtr->data;
    890  |    if (strcmp(nrtmPtr->mrName, nrtmNameStr) == 0 )
    891  |          break;
    892  |       currentPtr = currentPtr->next;
    893  |       }
    894  |
    895  |    /*
    896  |     * We return a pointer.  If we found the database, this pointer
    897  |     * points to the ca_database_t structure which represents the
    898  |     * database.
    899  |     * If we did not find the database, we return a pointer to NULL.
    900  |     */
    901  |    if (currentPtr == NULL)
    902  |       {
    903  |       nrtmPtr = NULL;
    904  |       return(nrtmPtr);
    905  |       }
    906  |  else
    907  |       {
    908  |       return(nrtmPtr);
    909  |       }
    910  |
    911  | }