src/config.c
changeset 11 a769385a59c6
parent 10 311ea5fa60dd
child 13 d3554afaa768
equal deleted inserted replaced
10:311ea5fa60dd 11:a769385a59c6
    11 #include <config.h>
    11 #include <config.h>
    12 
    12 
    13 const
    13 const
    14 char *CONFIG_OPTIONS[] = {
    14 char *CONFIG_OPTIONS[] = {
    15     "NICK", "PASSWORD", "SERVER", "PORT",
    15     "NICK", "PASSWORD", "SERVER", "PORT",
    16     "CHANNEL", "TOPIC",
    16     "CHANNEL", "TOPIC", NULL
    17 };
    17 };
    18 
    18 
    19 int config (UC * uc, char *fname)
    19 int config (UC * uc, char *fname)
    20 {
    20 {
    21     FILE *f;
    21     FILE *f;
    29         return -1;
    29         return -1;
    30 
    30 
    31     while (CONFIG_OPTIONS[i] != NULL)
    31     while (CONFIG_OPTIONS[i] != NULL)
    32         i++;
    32         i++;
    33 
    33 
    34     line = calloc (i, sizeof (char *));
    34     line = calloc ((size_t) (i + 1), sizeof (char *));
    35 
    35 
    36     /*
    36     /*
    37      * We can easily provide default values ...
    37      * We can easily provide default values ...
    38      */
    38      */
    39     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    39     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    40     uc->port = 6667;
    40     uc->port = 6667;
    41 
    41 
    42     *buffer = 0;
    42     while (!feof (f)) {
    43     fgets (buffer, 512, f);
    43         *buffer = '\0';
       
    44         (void)fgets (buffer, 512, f);
       
    45         token = buffer;
    44 
    46 
    45     token = buffer;
    47         while (*token == '\t')  /* Eat trailing tabs */
    46 
       
    47     while (!feof (f)) {
       
    48         /*
       
    49          * eat trailing tabs
       
    50          */
       
    51         while (*token == '\t')
       
    52             token++;
    48             token++;
    53 
    49         while (*token == ' ')   /* Eat trailing whitespaces */
    54         /*
       
    55          * eat trailing whitespaces
       
    56          */
       
    57         while (*token == ' ')
       
    58             token++;
    50             token++;
    59 
    51 
    60         token = strtok (token, ":");
    52         token = strtok (token, ":");
    61 
    53 
    62         if (token != NULL) {
    54         if (token != NULL) {
    63             value = strtok (NULL, "\n");
    55             value = strtok (NULL, "\n");
    64             i = 0;
    56             i = 0;
    65             while (CONFIG_OPTIONS[i] != NULL) {
    57             while (CONFIG_OPTIONS[i] != NULL) {
    66                 if (!strcmp (token, CONFIG_OPTIONS[i])) {
    58                 if (!strcmp (CONFIG_OPTIONS[i], token)) {
    67                     line[i] = malloc (strlen (value) + 1);
    59                     if (value)
    68                     strncpy (line[i], value, strlen (value));
    60                         line[i] = strdup (value);
    69                 }
    61                 }
    70                 i++;
    62                 i++;
    71             }
    63             }
    72         }
    64         }
    73         *buffer = 0;
       
    74         fgets (buffer, 512, f);
       
    75     }
    65     }
    76 
    66 
    77     if (fclose (f) != 0)
    67     if (fclose (f) != 0)
    78         return -1;
    68         return -1;
    79 
    69 
    80     i = 0;
    70     i = 0;
    81     while (CONFIG_OPTIONS[i] != NULL) {
    71     while (CONFIG_OPTIONS[i] != NULL) {
    82         if (line[i] != NULL)
    72         if (line[i] != NULL)
    83             switch (i) {
    73             switch (i) {
    84             case 0:            /* NICK */
    74             case 0:            /* NICK */
    85                 uc->nick = line[i];
    75                 uc->nick = strdup (line[i]);
       
    76                 free (line[i]);
    86                 break;
    77                 break;
    87             case 1:            /* PASSWORD */
    78             case 1:            /* PASSWORD */
    88                 uc->pass = line[i];
    79                 uc->pass = strdup (line[i]);
       
    80                 free (line[i]);
    89                 break;
    81                 break;
    90             case 2:            /* SERVER */
    82             case 2:            /* SERVER */
    91                 uc->server = line[i];
    83                 uc->server = strdup (line[i]);
       
    84                 free (line[i]);
    92                 break;
    85                 break;
    93             case 3:            /* PORT */
    86             case 3:            /* PORT */
    94                 uc->port = atoi (line[i]);
    87                 uc->port = atoi (line[i]);
       
    88                 free (line[i]);
    95                 break;
    89                 break;
    96             case 4:            /* CHANNEL */
    90             case 4:            /* CHANNEL */
    97                 uc->channel = line[i];
    91                 uc->channel = strdup (line[i]);
       
    92                 free (line[i]);
    98                 break;
    93                 break;
    99             case 5:            /* TOPIC */
    94             case 5:            /* TOPIC */
   100                 uc->topic = line[i];
    95                 uc->topic = strdup (line[i]);
       
    96                 free (line[i]);
   101                 break;
    97                 break;
   102             }
    98             }
   103         i++;
    99         i++;
   104     }
   100     }
       
   101 
       
   102     if (line != NULL)
       
   103         free (line);
   105 
   104 
   106     if (!(uc->nick && uc->pass && uc->server && uc->channel))
   105     if (!(uc->nick && uc->pass && uc->server && uc->channel))
   107         return -2;
   106         return -2;
   108 
   107 
   109     return 0;
   108     return 0;