cfg_parser.y 23.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
%{
/* cfg_parser.y -- general-purpose configuration file parser 
   Copyright (C) 2007 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
  
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>	
#include <string.h>
#include <netdb.h>
#include "intprops.h"
#include <mailutils/nls.h>
#include <mailutils/cfg.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>  

int mu_cfg_parser_verbose;
static mu_cfg_node_t *parse_tree;
mu_cfg_locus_t mu_cfg_locus;
int mu_cfg_tie_in;
 
static int _mu_cfg_errcnt;
static mu_cfg_lexer_t _mu_cfg_lexer;
mu_cfg_perror_t mu_cfg_perror; 
static void *_mu_cfg_lexer_data;
mu_cfg_alloc_t _mu_cfg_alloc;
mu_cfg_free_t _mu_cfg_free;

static int
yyerror (char *s)
{
  mu_cfg_perror (_mu_cfg_lexer_data, &mu_cfg_locus, "%s", s);
  return 0;
}
 
static int
yylex ()
{
  return _mu_cfg_lexer (_mu_cfg_lexer_data);
}

static mu_cfg_node_t *
mu_cfg_alloc_node (enum mu_cfg_node_type type, mu_cfg_locus_t *loc,
		   char *tag, char *label, mu_cfg_node_t *node)
{
  char *p;
  mu_cfg_node_t *np;
  size_t size = sizeof *np + strlen (tag) + 1
                + (label ? (strlen (label) + 1) : 0);
  np = _mu_cfg_alloc (size);
  if (!np)
    {
      mu_cfg_perror (_mu_cfg_lexer_data, &mu_cfg_locus,
		     _("Not enough memory"));
      abort();
    }
  np->type = type;
  np->locus = *loc;
  p = (char*) (np + 1);
  np->tag_name = p;
  strcpy (p, tag);
  p += strlen (p) + 1;
  if (label)
    {
      np->tag_label = p;
      strcpy (p, label);
    }
  else
    np->tag_label = label;
  np->node = node;
  np->next = NULL;
  return np;
}

static void
_mu_cfg_default_perror (void *ptr, const mu_cfg_locus_t *loc,
			const char *fmt, ...)
{
  va_list ap;
  
  fprintf (stderr, "%s:", loc->file ? loc->file : _("unknown file"));
  if (loc->line > 0)
    fprintf (stderr, "%lu", (unsigned long) loc->line);
  else
    fprintf (stderr, "%s", _("unknown line"));
  fprintf (stderr, ": ");
  va_start (ap, fmt);
  vfprintf (stderr, fmt, ap);
  va_end (ap);
  fprintf (stderr, "\n");
}

%}

%token MU_CFG_EOL_TOKEN
%token <node> MU_CFG_START_TOKEN MU_CFG_END_TOKEN
%token <string> MU_CFG_STRING_TOKEN

%type <pnode> tag
%type <nodelist> taglist

%union
{
  mu_cfg_node_t node;
  mu_cfg_node_t *pnode;
  struct
  {
    mu_cfg_node_t *head, *tail;
  } nodelist;
  char *string;
};

%%

input   : taglist
          {
	    parse_tree = $1.head;
          }
        ;

taglist : tag
          {
	    $$.head = $$.tail = $1;
	  }
        | taglist tag
          {
	    $$ = $1;
	    $$.tail->next = $2;
	    $$.tail = $2;
	  }
        ;

opt_eol : /* empty */
        | eol
        ;

eol     : MU_CFG_EOL_TOKEN
        | eol MU_CFG_EOL_TOKEN
        ;

tag     : MU_CFG_START_TOKEN opt_eol taglist MU_CFG_END_TOKEN MU_CFG_EOL_TOKEN
          {
	    if ($4.tag_name && strcmp ($4.tag_name, $1.tag_name))
	      {
		mu_cfg_perror (_mu_cfg_lexer_data,
			       &$1.locus,
			       _("Tag %s not closed"),
			       $1.tag_name);
		mu_cfg_perror (_mu_cfg_lexer_data,
			       &$4.locus,
			       _("Found closing %s tag instead"),
			       $4.tag_name);
		_mu_cfg_errcnt++;
	      }
	    $$ = mu_cfg_alloc_node (mu_cfg_node_tag, &$1.locus,
				    $1.tag_name, $1.tag_label, $3.head);
	  }
        | MU_CFG_STRING_TOKEN { mu_cfg_tie_in++; }
          MU_CFG_STRING_TOKEN { mu_cfg_tie_in = 0; } MU_CFG_EOL_TOKEN
          {
	    $$ = mu_cfg_alloc_node (mu_cfg_node_param, &mu_cfg_locus, $1, $3,
				    NULL);
	  }
        ;

%%

int
mu_cfg_parse (mu_cfg_node_t **ptree,
	      void *data, mu_cfg_lexer_t lexer,
	      mu_cfg_perror_t perror,
	      mu_cfg_alloc_t palloc, mu_cfg_free_t pfree)
{
  int rc;
	
  _mu_cfg_lexer = lexer;
  _mu_cfg_lexer_data = data;
  mu_cfg_perror = perror ? perror : _mu_cfg_default_perror;
  _mu_cfg_alloc = palloc ? palloc : malloc;
  _mu_cfg_free = pfree ? pfree : free;
  _mu_cfg_errcnt = 0;
  mu_cfg_tie_in = 0;
  rc = yyparse ();
  if (rc == 0 && _mu_cfg_errcnt)
    rc = 1;
  /* FIXME if (rc) free_memory; else */
  *ptree = parse_tree;
  parse_tree = NULL;
  return rc;
}
	


static void
print_level (FILE *fp, int level)
{
  while (level--)
    {
      fputc (' ', fp);
      fputc (' ', fp);
    }
}

struct tree_print
{
  unsigned level;
  FILE *fp;
};

int
print_node (const mu_cfg_node_t *node, void *data)
{
  struct tree_print *tp = data;
  
  print_level (tp->fp, tp->level);
  switch (node->type)
    {
    case mu_cfg_node_undefined:
      fprintf (tp->fp, _("<UNDEFINED>"));
      break;

    case mu_cfg_node_tag:
      fprintf (tp->fp, "<%s", node->tag_name);
      if (node->tag_label)
	fprintf (tp->fp, " %s", node->tag_label);
      fprintf (tp->fp, ">");
      tp->level++;
      break;

    case mu_cfg_node_param:
      fprintf (tp->fp, "%s", node->tag_name);
      if (node->tag_label)
	fprintf (tp->fp, " %s", node->tag_label);
      break;
    }
  fprintf (tp->fp, "\n");
  return MU_CFG_ITER_OK;
}

int
print_node_end (const mu_cfg_node_t *node, void *data)
{
  struct tree_print *tp = data;
  tp->level--;
  print_level (tp->fp, tp->level);
  fprintf (tp->fp, "</%s>\n", node->tag_name);
  return MU_CFG_ITER_OK;
}

void
mu_cfg_print_tree (FILE *fp, mu_cfg_node_t *node)
{
  struct tree_print t;
  t.level = 0;
  t.fp = fp;
  mu_cfg_preorder (node, print_node, print_node_end, &t);
}



static int
_mu_cfg_preorder_recursive (mu_cfg_node_t *node,
			    mu_cfg_iter_func_t beg, mu_cfg_iter_func_t end,
			    void *data)
{
  switch (node->type)
    {
    case mu_cfg_node_undefined:
      abort ();

    case mu_cfg_node_tag:
      switch (beg (node, data))
	{
	case MU_CFG_ITER_OK:
	  if (mu_cfg_preorder (node->node, beg, end, data))
	    return MU_CFG_ITER_STOP;
	  if (end && end (node, data) == MU_CFG_ITER_STOP)
	    return MU_CFG_ITER_STOP;
	  break;
	  
	case MU_CFG_ITER_SKIP:
	  break;
	  
	case MU_CFG_ITER_STOP:
	  return MU_CFG_ITER_STOP;
	}
      break;

    case mu_cfg_node_param:
      return beg (node, data);
    }
  return MU_CFG_ITER_OK;
}

int
mu_cfg_preorder(mu_cfg_node_t *node,
		mu_cfg_iter_func_t beg, mu_cfg_iter_func_t end, void *data)
{
  for (; node; node = node->next)
    if (_mu_cfg_preorder_recursive(node, beg, end, data)  == MU_CFG_ITER_STOP)
      return 1;
  return 0;
}

static int
_mu_cfg_postorder_recursive(mu_cfg_node_t *node,
			    mu_cfg_iter_func_t beg, mu_cfg_iter_func_t end,
			    void *data)
{
  switch (node->type)
    {
    case mu_cfg_node_undefined:
      abort ();

    case mu_cfg_node_tag:
      switch (beg (node, data))
	{
	case MU_CFG_ITER_OK:
	  if (mu_cfg_postorder (node->node, beg, end, data))
	    return MU_CFG_ITER_STOP;
	  if (end && end (node, data) == MU_CFG_ITER_STOP)
	    return MU_CFG_ITER_STOP;
	  break;
	  
	case MU_CFG_ITER_SKIP:
	  break;
	  
	case MU_CFG_ITER_STOP:
	  return MU_CFG_ITER_STOP;
	}
      break;
      
    case mu_cfg_node_param:
      return beg (node, data);
    }
  return 0;
}

int
mu_cfg_postorder (mu_cfg_node_t *node,
		  mu_cfg_iter_func_t beg, mu_cfg_iter_func_t end, void *data)
{
  if (node->next
      && mu_cfg_postorder (node->next, beg, end, data) == MU_CFG_ITER_STOP)
    return 1;
  return _mu_cfg_postorder_recursive (node, beg, end, data)
                == MU_CFG_ITER_STOP;
}


static int
free_section (const mu_cfg_node_t *node, void *data)
{
  mu_cfg_free_t free_fn = data;
  if (node->type == mu_cfg_node_tag)
    free_fn ((void *) node);
  return MU_CFG_ITER_OK;
}

static int
free_param (const mu_cfg_node_t *node, void *data)
{
  mu_cfg_free_t free_fn = data;
  if (node->type == mu_cfg_node_param)
    free_fn ((void*) node);
  return MU_CFG_ITER_OK;
}

void
mu_cfg_destroy_tree (mu_cfg_node_t **tree)
{
  if (tree && *tree)
    {
      mu_cfg_postorder (*tree, free_param, free_section, _mu_cfg_free);
      *tree = NULL;
    }
}



struct find_data
{
  char *tag;
  char *label;
  char *next;
  const mu_cfg_node_t *node;
};

static void
parse_tag (struct find_data *fptr)
{
  char *p = strchr (fptr->tag, '=');
  if (p)
    {
      *p++ = 0;
      fptr->label = p;
      fptr->next = p + strlen (p) + 1;
    }
  else
    {
      fptr->label = NULL;
      fptr->next = fptr->tag + strlen (fptr->tag) + 1;
    }
}

static int
node_finder (const mu_cfg_node_t *node, void *data)
{
  struct find_data *fdptr = data;
  if (strcmp (fdptr->tag, node->tag_name) == 0
      && (!fdptr->label || strcmp (fdptr->label, node->tag_label) == 0))
    {
      fdptr->tag = fdptr->next;
      parse_tag (fdptr);
      if (fdptr->tag[0] == 0)
	{
	  fdptr->node = node;
	  return MU_CFG_ITER_STOP;
	}
    }
  return MU_CFG_ITER_OK;
}

int	    
mu_cfg_find_node (mu_cfg_node_t *tree, const char *path, mu_cfg_node_t **pval)
{
  int rc;
  char *p;
  char *xpath;
  size_t len;
  struct find_data data;
  
  len = strlen (path) + 1;
  xpath = calloc (1, len + 1);
  if (!xpath)
    return 1;
  strcpy (xpath, path);
  xpath[len-1] = '/';
  data.tag = xpath;
  for (p = data.tag; *p; p++)
    if (*p == '/')
      *p = 0;
  parse_tag (&data);
  rc = mu_cfg_preorder (tree, node_finder, NULL, &data);
  free (xpath);
  if (rc)
    {
      *pval = (mu_cfg_node_t *) data.node;
      return 0;
    }
  return MU_ERR_NOENT;
}

int	    
mu_cfg_find_node_label (mu_cfg_node_t *tree, const char *path,
			const char **pval)
{
  mu_cfg_node_t *node;
  int rc = mu_cfg_find_node (tree, path, &node);
  if (rc)
    *pval = node->tag_label;
  return rc;
}


struct mu_cfg_section_list
{
  struct mu_cfg_section_list *next;
  struct mu_cfg_section *sec;
};

struct scan_tree_data
{
  struct mu_cfg_section_list *list;
  void *call_data;
  int error;
};

static struct mu_cfg_cont *
find_container (mu_list_t list, const char *ident, size_t len)
{
  mu_iterator_t iter;
  struct mu_cfg_cont *ret = NULL;
      
  if (len == 0)
    len = strlen (ident);

  mu_list_get_iterator (list, &iter);
  for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
       mu_iterator_next (iter))
    {
      struct mu_cfg_cont *cont;
      mu_iterator_current (iter, (void**) &cont);

      if (strlen (cont->v.ident) == len
	  && memcmp (cont->v.ident, ident, len) == 0)
	{
	  ret = cont;
	  break;
	}
    }
  mu_iterator_destroy (&iter);
  return ret;
}

static struct mu_cfg_section *
find_subsection (struct mu_cfg_section *sec, const char *ident, size_t len)
{
  if (sec)
    {
      if (sec->subsec)
	{
	  struct mu_cfg_cont *cont = find_container (sec->subsec, ident, len);
	  if (cont)
	    return &cont->v.section;
	}
    }
  return NULL;
}

static struct mu_cfg_param *
find_param (struct mu_cfg_section *sec, const char *ident, size_t len)
{
  if (sec)
    {
      if (sec->param)
	{
	  struct mu_cfg_cont *cont = find_container (sec->param, ident, len);
	  if (cont)
	    return &cont->v.param;
	}
    }
  return NULL;
}

static int
push_section (struct scan_tree_data *dat, struct mu_cfg_section *sec)
{
  struct mu_cfg_section_list *p = _mu_cfg_alloc (sizeof *p);
  if (!p)
    {
      mu_cfg_perror (dat->call_data, NULL, _("not enough memory"));
      return 1;
    }
  p->sec = sec;
  p->next = dat->list;
  dat->list = p;
  return 0;
}

static struct mu_cfg_section *
pop_section (struct scan_tree_data *dat)
{
  struct mu_cfg_section_list *p = dat->list;
  struct mu_cfg_section *sec = p->sec;
  dat->list = p->next;
  _mu_cfg_free (p);
  return sec;
}

#define STRTONUM(s, type, base, res, limit)				      \
{									      \
  type sum = 0;							      	      \
									      \
  while (*s)								      \
    {							      		      \
      type x;							      	      \
      									      \
      if ('0' <= *s && *s <= '9')				      	      \
	x = sum * base + *s - '0';			      		      \
      else if (base == 16 && 'a' <= *s && *s <= 'f')		      	      \
	x = sum * base + *s - 'a';			      		      \
      else if (base == 16 && 'A' <= *s && *s <= 'F')		      	      \
	x = sum * base + *s - 'A';			      		      \
      else							      	      \
	break;						      		      \
      if (x <= sum)							      \
	{						      		      \
	  mu_cfg_perror (sdata->call_data,                         	      \
			 &node->locus,                           	      \
			 _("numeric overflow"));                 	      \
	  return 1;					      		      \
	}								      \
      else if (limit && x > limit)					      \
	{			      					      \
	  mu_cfg_perror (sdata->call_data,                         	      \
			 &node->locus,                           	      \
			 _("value out of allowed range"));       	      \
	  return 1;					      		      \
	}							      	      \
      sum = x;						      		      \
      *s++;							      	      \
    }								      	      \
  res = sum;                                                                  \
}

#define STRxTONUM(s, type, res, limit)				              \
{								              \
  int base;						              	      \
  if (*s == '0')							      \
    {					              			      \
      s++;						              	      \
      if (*s == 0)					              	      \
	base = 10;				              		      \
      else if (*s == 'x' || *s == 'X')					      \
	{		              					      \
	  s++;					              		      \
	  base = 16;				              		      \
	}								      \
      else						              	      \
	base = 8;				              		      \
    } else							              \
      base = 10;					              	      \
  STRTONUM (s, type, base, res, limit);			                      \
}

#define GETUNUM(str, type, res)						      \
{									      \
  type tmpres;							      	      \
  const char *s = str;                                                        \
  STRxTONUM (s, type, tmpres, 0);					      \
  if (*s)								      \
    {							      		      \
      mu_cfg_perror (sdata->call_data,                                 	      \
		     &node->locus,                                   	      \
		     _("not a number (stopped near `%s')"),          	      \
		     s);    					      	      \
      return 1;					      	      		      \
    }								      	      \
  res = tmpres;							              \
}

#define GETSNUM(str, type, res)	               				      \
{									      \
  unsigned type tmpres;						      	      \
  const char *s = str;						      	      \
  int sign;							      	      \
  unsigned type limit;						      	      \
									      \
  if (*s == '-')							      \
    {						      			      \
      sign++;							      	      \
      s++;							      	      \
      limit = TYPE_MINIMUM (type);				      	      \
      limit = - limit;                                                        \
    }									      \
  else									      \
    {							      		      \
      sign = 0;						      		      \
      limit = TYPE_MAXIMUM (type);				      	      \
    }								      	      \
									      \
  STRxTONUM (s, unsigned type, tmpres, limit);	      		      	      \
  if (*s)								      \
    {							      		      \
      mu_cfg_perror (sdata->call_data,                                 	      \
		     &node->locus,                                   	      \
		     _("not a number (stopped near `%s')"),          	      \
		     s);    					      	      \
      return 1;					      	      		      \
    }								      	      \
  res = sign ? - tmpres : tmpres;					      \
}

static int
parse_ipv4 (struct scan_tree_data *sdata, const mu_cfg_node_t *node,
	    struct in_addr *res)
{
  struct in_addr addr;
  if (inet_aton (node->tag_label, &addr) == 0)
    {
      mu_cfg_perror (sdata->call_data, 
		     &node->locus,
		     _("not an IPv4"));
      return 1;
    }
  addr.s_addr = ntohl (addr.s_addr);
  *res = addr;
  return 0;
}		

static int
parse_host (struct scan_tree_data *sdata, const mu_cfg_node_t *node,
	    struct in_addr *res)
{
  struct in_addr addr;
  struct hostent *hp = gethostbyname (node->tag_label);
  if (hp)
    {
      addr.s_addr = *(unsigned long *)hp->h_addr;
    }
  else if (inet_aton(node->tag_label, &addr) == 0)
    {
      mu_cfg_perror (sdata->call_data, 
		     &node->locus,
		     _("cannot resolve hostname `%s'"),
		     node->tag_label);
      return 1;
    } 
  addr.s_addr = ntohl (addr.s_addr);
  *res = addr;
  return 0;
}		

static int
parse_cidr (struct scan_tree_data *sdata, const mu_cfg_node_t *node,
	    mu_cfg_cidr_t *res)
{
  struct in_addr addr;
  unsigned long mask;
  char astr[16], *p, *s;
  
  p = strchr (node->tag_label, '/');
  if (*p)
    {
      int len = p - node->tag_label;
      if (len > sizeof astr - 1) {
	mu_cfg_perror (sdata->call_data, 
		       &node->locus,
		       _("not a valid IPv4 address in CIDR"));
	return 1;
      }
      memcpy (astr, node->tag_label, len);
      astr[len] = 0;
      if (inet_aton (astr, &addr) == 0)
	{
	  mu_cfg_perror (sdata->call_data, 
			 &node->locus,
			 _("not a valid IPv4 address in CIDR"));
	  return 1;
	}
      addr.s_addr = ntohl (addr.s_addr);

      p++;
      s = p;
      STRxTONUM (s, unsigned long, mask, 0);
      if (*s == '.')
	{
	  struct in_addr a;
	  if (inet_aton (p, &a) == 0)
	    {
	      mu_cfg_perror (sdata->call_data, 
			     &node->locus,
			     _("not a valid network in CIDR"));
	      return 1;
	    }
	  a.s_addr = ntohl (a.s_addr);
	  for (mask = 0; (a.s_addr & 1) == 0 && mask < 32; )
	    {
	      mask++;
	      a.s_addr >>= 1;
	    }
	  mask = 32 - mask;
	}
      else if (mask > 32)
	{
	  mu_cfg_perror (sdata->call_data, 
			 &node->locus,
			 _("not a valid network mask in CIDR"));
	  return 1;
	}
    }
  else
    {
      int i;
      unsigned short x;
      addr.s_addr = 0;
      
      for (i = 0; i < 3; i++)
	{
	  STRxTONUM(p, unsigned short, x, 255);
	  if (*p != '.')
	    break;
	  addr.s_addr = (addr.s_addr << 8) + x;
	}
		
      if (*p)
	{
	  mu_cfg_perror (sdata->call_data,
			 &node->locus,
			 _("not a CIDR (stopped near `%s')"),
			 p);
	  return 1;
	}

      mask = i * 8;
      
      addr.s_addr <<= (4 - i) * 8;
    }
			
  res->addr = addr;
  res->mask = mask;
  return 0;
}	

int
mu_cfg_parse_boolean (const char *str, int *res)
{
  if (strcmp (str, "yes") == 0
      || strcmp (str, "on") == 0
      || strcmp (str, "t") == 0
      || strcmp (str, "true") == 0
      || strcmp (str, "1") == 0)
    *res = 1;
  else if (strcmp (str, "no") == 0
	   || strcmp (str, "off") == 0
	   || strcmp (str, "nil") == 0
	   || strcmp (str, "false") == 0
	   || strcmp (str, "0") == 0)
    *res = 0;
  else
    return 1;
  return 0;
}

static int
parse_bool (struct scan_tree_data *sdata, const mu_cfg_node_t *node, int *res)
{
  if (mu_cfg_parse_boolean (node->tag_label, res))
    {
      mu_cfg_perror (sdata->call_data, &node->locus, _("not a boolean"));
      return 1;
    }
  return 0;
}

static int
parse_param (struct scan_tree_data *sdata, const mu_cfg_node_t *node)
{
  struct mu_cfg_param *param = find_param (sdata->list->sec, node->tag_name,
					   0);
  if (!param)
    {
      mu_cfg_perror (sdata->call_data,
		     &node->locus,
		     _("unknown keyword `%s'"),
		     node->tag_name);
      return 1;
    }

  switch (param->type)
    {
    case mu_cfg_string:
      {
	size_t len = strlen (node->tag_label);
	char *s = _mu_cfg_alloc (len + 1);
	if (!s)
	  {
	    mu_cfg_perror (sdata->call_data,
			   &node->locus,
			   _("not enough memory"));
	    return 1;
	  }
	strcpy (s, node->tag_label);
	/* FIXME: free param->data? */
	*(char**)param->data = s;
	break;
      }
		
    case mu_cfg_short:
      GETSNUM (node->tag_label, short, *(short*)param->data);
      break;
		
    case mu_cfg_ushort:
      GETUNUM (node->tag_label, unsigned short, *(unsigned short*)param->data);
      break;
		
    case mu_cfg_int:
      GETSNUM (node->tag_label, int, *(int*)param->data);
      break;
		
    case mu_cfg_uint:
      GETUNUM (node->tag_label, unsigned int, *(unsigned int*)param->data);
      break;
            
    case mu_cfg_long:
      GETSNUM (node->tag_label, long, *(long*)param->data);
      break;
      
    case mu_cfg_ulong:
      GETUNUM (node->tag_label, unsigned long, *(unsigned long*)param->data);
      break;
		
    case mu_cfg_size:
      GETUNUM (node->tag_label, size_t, *(size_t*)param->data);
      break;
		
    case mu_cfg_off:
      mu_cfg_perror (sdata->call_data, &node->locus, _("not implemented yet"));
      /* GETSNUM(node->tag_label, off_t, *(off_t*)param->data); */
      return 1;

    case mu_cfg_time:
      GETUNUM (node->tag_label, time_t, *(time_t*)param->data);
      break;
      
    case mu_cfg_bool:
      if (parse_bool (sdata, node, (int*) param->data))
	return 1;
      break;
		
    case mu_cfg_ipv4: 
      if (parse_ipv4 (sdata, node, (struct in_addr *)param->data))
	return 1;
      break;
      
    case mu_cfg_cidr:
      if (parse_cidr (sdata, node, (mu_cfg_cidr_t *)param->data))
	return 1;
      break;
      
    case mu_cfg_host:
      if (parse_host (sdata, node, (struct in_addr *)param->data))
	return 1;
      break;

    case mu_cfg_callback:
      if (param->callback (&node->locus, param->data, node->tag_label))
	  return 1;
      break;
      
    default:
      abort ();
    }
  return 0;
}


static int
_scan_tree_helper (const mu_cfg_node_t *node, void *data)
{
  struct scan_tree_data *sdata = data;
  struct mu_cfg_section *sec;
  
  switch (node->type)
    {
    case mu_cfg_node_undefined:
      abort ();
		
    case mu_cfg_node_tag:
      sec = find_subsection (sdata->list->sec, node->tag_name, 0);
      if (!sec)
	{
	  if (mu_cfg_parser_verbose)
	    {
	      mu_cfg_perror (sdata->call_data,
			     &node->locus,
			     _("unknown section `%s'"),
			     node->tag_name);
	    }
	  return MU_CFG_ITER_SKIP;
	}
      if (!sec->subsec && !sec->param)
	return MU_CFG_ITER_SKIP;
      if (sec->parser &&
	  sec->parser (mu_cfg_section_start, node,
		       sec->data, sdata->call_data))
	{
	  sdata->error++;
	  return MU_CFG_ITER_SKIP;
	}
      push_section(sdata, sec);
      break;
		
    case mu_cfg_node_param:
      if (parse_param (sdata, node))
	{
	  sdata->error++;
	  return MU_CFG_ITER_SKIP;
	}
      break;
    }
  return MU_CFG_ITER_OK;
}

static int
_scan_tree_end_helper (const mu_cfg_node_t *node, void *data)
{
  struct scan_tree_data *sdata = data;
  struct mu_cfg_section *sec;
  
  switch (node->type)
    {
    default:
      abort ();
		
    case mu_cfg_node_tag:
      sec = pop_section (sdata);
      if (sec && sec->parser)
	{
	  if (sec->parser (mu_cfg_section_end, node, sec->data,
			   sdata->call_data))
	    {
	      sdata->error++;
	      return MU_CFG_ITER_SKIP;
	    }
	}
    }
  return MU_CFG_ITER_OK;
}

int
mu_cfg_scan_tree (mu_cfg_node_t *node,
		  struct mu_cfg_section *sections,
		  void *data, mu_cfg_perror_t perror,
		  mu_cfg_alloc_t palloc, mu_cfg_free_t pfree)
{
  struct scan_tree_data dat;
  dat.list = NULL;
  mu_cfg_perror = perror ? perror : _mu_cfg_default_perror;
  _mu_cfg_alloc = palloc ? palloc : malloc;
  _mu_cfg_free = pfree ? pfree : free;
  dat.call_data = data;
  dat.error = 0;
  if (push_section (&dat, sections))
    return 1;
  mu_cfg_preorder (node, _scan_tree_helper, _scan_tree_end_helper, &dat);
  pop_section (&dat);
  return dat.error;
}

int
mu_cfg_find_section (struct mu_cfg_section *root_sec,
		     const char *path, struct mu_cfg_section **retval)
{
  while (path[0])
    {
      struct mu_cfg_section *sec;
      size_t len;
      const char *p;
      
      while (*path == '/')
	path++;

      if (*path == 0)
	return MU_ERR_NOENT;
      
      p = strchr (path, '/');
      if (p)
	len = p - path;
      else
	len = strlen (path);
      
      sec = find_subsection (root_sec, path, len);
      if (!sec)
	return MU_ERR_NOENT;
      root_sec = sec;
      path += len;
    }
  if (retval)
    *retval = root_sec;
  return 0;
}