## modules/co/constants.c

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

## FUNCTIONS

This source file includes following functions.

1. set_string
2. set_int
3. set_boolean
4. show_string
5. show_int
6. show_boolean
7. CO_get_authenticate
8. CO_get_whois_suspended
9. CO_get_welcome
10. CO_get_prompt
11. CO_get_clear_screen
12. CO_get_accounting
13. CO_get_do_server
14. CO_get_do_update
15. init_constants
16. CO_to_string
17. CO_const_to_string
18. CO_set_const
19. CO_set

       1 /***************************************
       2   $Revision: 1.21 $
       3
       4   Constants module (co) - this _should_ eventually get merged in with the
       5   config module.
       6
       7   Status: NOT REVUED, NOT TESTED
       8
       9   +html+ <DL COMPACT>
      10   +html+ <DT>Online References:
      11   +html+ <DD><UL>
      12   +html+ </UL>
      13   +html+ </DL>
      14   +html+ <PRE>
      15   Instructions for use:
      16
      17     To add a constant:
      18       0. Add a default value for the constant. (string)
      19       1. Add the constant declaration to the _Constants struct.
      20       2. Add a CO_get_function()
      21       3. Add initializing code to init_constants()
      22
      23     To access the constant:
      24       use the CO_get<Constant>() function from your other code.
      25   +html+ </PRE>
      26
      27   ******************/ /******************
      28   Filename            : constants.c
      29   Author              : ottrey@ripe.net
      30   OSs Tested          : Solaris
      31   Related Modules     : Used in conjunction with the properties module.
      32   Problems            :
      33   To Do               : Merge into a "config module"
      34   Comments            :
      35   ******************/ /******************
      36   Copyright (c) 1999,2000,2001,2002               RIPE NCC
      37
      38   All Rights Reserved
      39
      40   Permission to use, copy, modify, and distribute this software and its
      41   documentation for any purpose and without fee is hereby granted,
      42   provided that the above copyright notice appear in all copies and that
      43   both that copyright notice and this permission notice appear in
      44   supporting documentation, and that the name of the author not be
      45   used in advertising or publicity pertaining to distribution of the
      46   software without specific, written prior permission.
      47
      48   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      49   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      50   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      51   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      52   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      53   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      54   ***************************************/
      55
      56 #include "rip.h"
      57
      58 #include <stdio.h>
      59 #include <stdlib.h>
      60 #include <string.h>
      61 #include <glib.h>
      62
      63 /*+ Maximum number of constants. +*/
      64 #define MAX_CONSTS 100
      65
      66 /*+ Default values for constants. +*/
      67
      68 #define DEFLT_AUTHENTICATE    "0"
      69 #define DEFLT_WHOIS_SUSPENDED "0"
      70 #define DEFLT_DO_SERVER       "1"
      71 #define DEFLT_WELCOME         "Welcome to the whois R.I.P. server.\n"
      72 #define DEFLT_PROMPT          "whois R.I.P. config> "
      73 #define DEFLT_CLEAR_SCREEN    "0"
      74 #define DEFLT_ACCOUNTING      "0"
      75 #define DEFLT_CONFIG_FILE     "rip.config"
      76
      77 /*+ Each constant has a +*/
      78 struct _constant {
      79   const char *token;              /*+ Token to be found in properties file. +*/
      80   const char *deflt;                    /*+ Default value for the constant. +*/
      81   int (*set_func)(void *, const char *);  /*+ Function to set the constant. +*/
      82   void *constant_ptr;                     /*+ Pointer to the constant value +*/
      83   char *(*show_func)(const void *);      /*+ Function to show the constant. +*/
      84 };
      85
      86
      87 /*+ The Constants array has a +*/
      88 typedef struct _Constants {
      89   int   authenticate[1];                         /*+ Authenticate users. +*/
      90   int   whois_suspended[1];                /*+ Suspend the whois server. +*/
      91   char  welcome[1024];                  /*+ Welcome for config protocol. +*/
      92   char  prompt[1024];                    /*+ Prompt for config protocol. +*/
      93   int   clear_screen[1];         /*+ Clear screen after config commands. +*/
      94   int   accounting[1];          /*+ Conduct accounting on whois queries. +*/
      95
      96   int   do_server[1]; /*+ turns off execution of the all servers(threads) +*/
      97   int   do_update[1]; /*+ switches on and off the updates +*/
      98
      99 } *Constants;
     100
     101 /*
     102  * Global Variables
     103  */
     104 /*+ The array of Global Constants. +*/
     105 static Constants  Global_constants=NULL;
     106
     107 /*
     108  * Set Functions
     109  */
     110 static int set_string(void *constant, const char *value) {
         /* [<][>][^][v][top][bottom][index][help] */
     111
     112   strcpy((char *)constant, value);
     113
     114   return 0;
     115 } /* set_string() */
     116
     117 static int set_int(void *constant, const char *value) {
         /* [<][>][^][v][top][bottom][index][help] */
     118   int i;
     119
     120   i = atol(value);
     121   ((int *)constant)[0] = i;
     122
     123   return 0;
     124 } /* set_int() */
     125
     126 static int set_boolean(void *constant, const char *value) {
         /* [<][>][^][v][top][bottom][index][help] */
     127   int result=1;
     128   int i;
     129
     130   i = atol(value);
     131
     132   /* If a valid boolean */
     133   if ( (i == 0) || (i == 1)) {
     134     ((int *)constant)[0] = i;
     135     result = 0;
     136   }
     137
     138   return result;
     139 } /* set_boolean() */
     140
     141
     142 /*
     143  * Show Functions
     144  */
     145 /* AR. changed for unification with oter show funcs */
     146 static char *show_string(const void *constant) {
         /* [<][>][^][v][top][bottom][index][help] */
     147   return UT_strdup((char *)constant);
     148 } /* show_string() */
     149
     150 static char *show_int(const void *constant) {
         /* [<][>][^][v][top][bottom][index][help] */
     151   char *tmp;
     152
     153   tmp = UT_malloc(32);
     154
     155   sprintf(tmp, "%d", ((int *)constant)[0]);
     156   return tmp;
     157 } /* show_int() */
     158
     159 static char *show_boolean(const void *constant) {
         /* [<][>][^][v][top][bottom][index][help] */
     160   char *tmp;
     161
     162   tmp =  UT_malloc(32);
     163
     164   sprintf(tmp, "%d", ((int *)constant)[0]);
     165   return tmp;
     166 } /* show_boolean() */
     167
     168
     169 /*
     170  * Get Functions
     171  */
     172
     173 int CO_get_authenticate() {
         /* [<][>][^][v][top][bottom][index][help] */
     174   return Global_constants->authenticate[0];
     175 }
     176
     177 int CO_get_whois_suspended() {
         /* [<][>][^][v][top][bottom][index][help] */
     178   return Global_constants->whois_suspended[0];
     179 }
     180
     181 char *CO_get_welcome() {
         /* [<][>][^][v][top][bottom][index][help] */
     182   return Global_constants->welcome;
     183 }
     184
     185 char *CO_get_prompt() {
         /* [<][>][^][v][top][bottom][index][help] */
     186   return Global_constants->prompt;
     187 }
     188
     189 int CO_get_clear_screen() {
         /* [<][>][^][v][top][bottom][index][help] */
     190   return Global_constants->clear_screen[0];
     191 }
     192
     193 int CO_get_accounting() {
         /* [<][>][^][v][top][bottom][index][help] */
     194   return Global_constants->accounting[0];
     195 }
     196
     197 int CO_get_do_server() {
         /* [<][>][^][v][top][bottom][index][help] */
     198   return Global_constants->do_server[0];
     199 }
     200
     201 int CO_get_do_update() {
         /* [<][>][^][v][top][bottom][index][help] */
     202   return Global_constants->do_update[0];
     203 }
     204
     205 /*+
     206  * Contains the constant definitions for the Token, set_function, show_function.
     207  * (See: _constant)
     208 +*/
     209 static struct _constant constant[MAX_CONSTS];
     210
     211 /* init_constants() */
     212 /*++++++++++++++++++++++++++++++++++++++
     213   Initialize all the constants.
     214
     215   More:
     216   +html+ <PRE>
     217   Authors:
     218         ottrey
     219
     220   +html+ </PRE><DL COMPACT>
     221   +html+ <DT>Online References:
     222   +html+ <DD><UL>
     223   +html+ </UL></DL>
     224
     225   ++++++++++++++++++++++++++++++++++++++*/
     226 static void init_constants(void) {
         /* [<][>][^][v][top][bottom][index][help] */
     227   int n=0;
     228
     229   constant[n].token="SV.authenticate";
     230   constant[n].deflt=DEFLT_AUTHENTICATE;
     231   constant[n].set_func=set_boolean;
     232   constant[n].constant_ptr=Global_constants->authenticate;
     233   constant[n].show_func=show_boolean;
     234   n++;
     235
     236   constant[n].token="SV.whois_suspended";
     237   constant[n].deflt=DEFLT_WHOIS_SUSPENDED;
     238   constant[n].set_func=set_boolean;
     239   constant[n].constant_ptr=Global_constants->whois_suspended;
     240   constant[n].show_func=show_boolean;
     241   n++;
     242
     243   constant[n].token="SV.do_server";
     244   constant[n].deflt=DEFLT_DO_SERVER;
     245   constant[n].set_func=set_boolean;
     246   constant[n].constant_ptr=Global_constants->do_server;
     247   constant[n].show_func=show_boolean;
     248   n++;
     249
     250   constant[n].token="UD.do_update";
     251   constant[n].deflt="1";
     252   constant[n].set_func=set_int;
     253   constant[n].constant_ptr=Global_constants->do_update;
     254   constant[n].show_func=show_int;
     255   n++;
     256
     257   constant[n].token="PC.prompt";
     258   constant[n].deflt=DEFLT_PROMPT;
     259   constant[n].set_func=set_string;
     260   constant[n].constant_ptr=Global_constants->prompt;
     261   constant[n].show_func=show_string;
     262   n++;
     263
     264   constant[n].token="PC.clear_screen";
     265   constant[n].deflt=DEFLT_CLEAR_SCREEN;
     266   constant[n].set_func=set_boolean;
     267   constant[n].constant_ptr=Global_constants->clear_screen;
     268   constant[n].show_func=show_boolean;
     269   n++;
     270
     271   constant[n].token=NULL;
     272
     273 } /* init_constants() */
     274
     275
     276 /* CO_to_string() */
     277 /*++++++++++++++++++++++++++++++++++++++
     278   Returns the constants as a string.
     279
     280   More:
     281   +html+ <PRE>
     282   Authors:
     283         ottrey
     284
     285   +html+ </PRE><DL COMPACT>
     286   +html+ <DT>Online References:
     287   +html+ <DD><UL>
     288   +html+ </UL></DL>
     289
     290   ++++++++++++++++++++++++++++++++++++++*/
     291 char *CO_to_string(void) {
         /* [<][>][^][v][top][bottom][index][help] */
     292   char *consts;
     293   const char *token;
     294   char *value;
     295   GString *tmp;
     296   int i=0;
     297
     298   tmp = g_string_new("Constants = { ");
     299   while(constant[i].token != NULL) {
     300     token = constant[i].token;
     301     value = constant[i].show_func(constant[i].constant_ptr);
     302     g_string_sprintfa(tmp, "\n[%s]=\"%s\"", token, value);
     303     UT_free(value); /* Otherwise we have memory leaks */
     304     i++;
     305   }
     306   g_string_append_c(tmp, '}');
     307
     308   consts = UT_strdup(tmp->str);
     309   g_string_free(tmp, TRUE);
     310
     311   return consts;
     312 } /* CO_to_string() */
     313
     314
     315 char *CO_const_to_string(const char *name) {
         /* [<][>][^][v][top][bottom][index][help] */
     316   char *result=NULL;
     317   int i;
     318
     319   for (i=0; constant[i].token != NULL; i++) {
     320     if (strcmp(constant[i].token, name) == 0) {
     321       result = constant[i].show_func(constant[i].constant_ptr);
     322       break;
     323     }
     324   }
     325
     326   return result;
     327 } /* CO_const_to_string() */
     328
     329  /* CO_set_const() */
     330 /*++++++++++++++++++++++++++++++++++++++
     331   Sets the value of one constant.  Returns 0 if no error.
     332
     333   More:
     334   +html+ <PRE>
     335   Authors:
     336         ottrey
     337
     338   +html+ </PRE><DL COMPACT>
     339   +html+ <DT>Online References:
     340   +html+ <DD><UL>
     341   +html+ </UL></DL>
     342
     343   ++++++++++++++++++++++++++++++++++++++*/
     344 int CO_set_const(const char *name, const char *value) {
         /* [<][>][^][v][top][bottom][index][help] */
     345   int result=1;
     346   int i;
     347
     348   for (i=0; constant[i].token != NULL; i++) {
     349     if (strcmp(constant[i].token, name) == 0) {
     350       result = constant[i].set_func((void *)constant[i].constant_ptr, value);
     351       break;
     352     }
     353   }
     354
     355   return result;
     356 } /* CO_set_const() */
     357
     358
     359 /* CO_set() */
     360 /*++++++++++++++++++++++++++++++++++++++
     361   Sets the constants from the properties module.
     362   Returns the number of constants set.
     363
     364   More:
     365   +html+ <PRE>
     366   Authors:
     367         ottrey
     368   +html+ </PRE><DL COMPACT>
     369   +html+ <DT>Online References:
     370   +html+ <DD><UL>
     371   +html+   <LI><A HREF="../src/.properties">.properties</A>
     372   +html+ </UL></DL>
     373
     374   ++++++++++++++++++++++++++++++++++++++*/
     375 char *CO_set(void) {
         /* [<][>][^][v][top][bottom][index][help] */
     376   int i;
     377   int set_count=0;
     378   int set;
     379   char result_buff[256];
     380   char *result;
     381   char *property;
     382
     383   /* Initialize if necessary */
     384   if (Global_constants == NULL) {
     385     Global_constants = (Constants)UT_calloc(1, sizeof(struct _Constants));
     386
     387     init_constants();
     388   }
     389
     390   for (i=0; constant[i].token != NULL; i++) {
     391     property = PR_get_property(constant[i].token, constant[i].deflt);
     392     set = constant[i].set_func((void *)constant[i].constant_ptr, property);
     393     UT_free(property);
     394     if (set == 0) {
     395       set_count++;
     396     }
     397   }
     398
     399   sprintf(result_buff, "%d out of %d constant(s) set.", set_count, i);
     400
     401   result = (char *)UT_calloc(1, strlen(result_buff)+1);
     402   strcpy(result, result_buff);
     403
     404   return result;
     405 } /* CO_set() */
     406

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