cfg_lexer.c 19.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
/* cfg_lexer.c -- default lexer for Mailutils configuration files
   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/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <mailutils/argcv.h>
#include <mailutils/nls.h>
#include <mailutils/cfg.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/mutil.h>
#include <mailutils/monitor.h>
#include <mailutils/refcount.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/stream.h>

#include "cfg_parser.h"

struct lexer_data
{
  char *buffer;
  char *curp;
  mu_list_t mpool;
  char *cbuf;
  size_t cbufsize;
  size_t cbuflevel;
};

#define CBUFINCR 256

static void
cbuf_grow (struct lexer_data *datp, const char *str, size_t len)
{
  if (datp->cbufsize - datp->cbuflevel < len)
    {
      size_t n = ((datp->cbuflevel + len + CBUFINCR - 1) / CBUFINCR);
      datp->cbufsize = n * CBUFINCR;
      datp->cbuf = realloc (datp->cbuf, datp->cbufsize);
      if (!datp->cbuf)
	{
	  mu_error ("%s", mu_strerror (ENOMEM));
	  abort ();
	}
    }
  memcpy (datp->cbuf + datp->cbuflevel, str, len);
  datp->cbuflevel += len;
}

static void
cbuf_1grow (struct lexer_data *datp, char c)
{
  cbuf_grow (datp, &c, 1);
}

static void
_mpool_destroy_item (void *p)
{
  free (p);
}

static char *
cbuf_finish (struct lexer_data *datp)
{
  char *p = malloc (datp->cbuflevel);
  if (!p)
    {
      mu_error ("%s", mu_strerror (ENOMEM));
      abort ();
    }
  memcpy (p, datp->cbuf, datp->cbuflevel);
  datp->cbuflevel = 0;
  if (!datp->mpool)
    {
      mu_list_create (&datp->mpool);
      mu_list_set_destroy_item (datp->mpool, _mpool_destroy_item);
    }
  mu_list_append (datp->mpool, p);
  return p;
}

static void
skipws (struct lexer_data *p)
{
  while (*p->curp && isspace (*p->curp))
    {
      if (*p->curp == '\n')
	mu_cfg_locus.line++;
      p->curp++;
    }
}

static void
skipline (struct lexer_data *p)
{
  while (*p->curp && *p->curp != '\n')
    p->curp++;
}

static int
isword (int c)
{
  if (mu_cfg_tie_in)
    return c && c != ';';
  
  return isalnum (c) || c == '_' || c == '-' || c == '.';
}

static char *
copy_alpha (struct lexer_data *p)
{
  do
    {
      if (*p->curp == '\n')
	mu_cfg_locus.line++;
      cbuf_1grow (p, *p->curp);
      p->curp++;
    } while (*p->curp && isword (*p->curp));
  cbuf_1grow (p, 0);
  return cbuf_finish (p);
}

static int
continuation_line_p (struct lexer_data *p, int quote)
{
  skipws (p);
  return *p->curp == quote;
}

static char *
copy_string (struct lexer_data *p)
{
  int quote;
  do
    {
      quote = *p->curp++;

      while (*p->curp)
	{
	  if (*p->curp == '\\')
	    {
	      char c;
	      if (*++p->curp == 0)
		{
		  cbuf_1grow (p, '\\');
		  break;
		}
	      if (*p->curp == '\n')
		{
		  p->curp++;
		  continue;
		}
	      c = mu_argcv_unquote_char (*p->curp);
	      if (c == *p->curp)
		{
		  cbuf_1grow (p, '\\');
		  cbuf_1grow (p, *p->curp);
		}
	      else
		cbuf_1grow (p, c);
	      p->curp++;
	    }
	  else if (*p->curp == quote)
	    {
	      p->curp++;
	      break;
	    }
	  else
	    {
	      cbuf_1grow (p, *p->curp);
	      p->curp++;
	    }
	}
    }
  while (continuation_line_p (p, quote));
  
  cbuf_1grow (p, 0);
  return cbuf_finish (p);
}

#define LEX_DEBUG(tok, arg1, arg2) \
 do \
   { \
     if (mu_debug_check_level (dbg, MU_DEBUG_TRACE2)) \
       { \
	 mu_debug_set_locus (dbg, \
			     mu_cfg_locus.file, mu_cfg_locus.line); \
	 mu_cfg_format_error (dbg, MU_DEBUG_TRACE2, "TOKEN %s \"%s\" \"%s\"", \
			      tok, \
			      arg1 ? arg1 : "", \
			      arg2 ? arg2 : ""); \
       } \
   } \
 while (0)

int
default_lexer (void *dp, mu_debug_t dbg)
{
  struct lexer_data *p = dp;
  char *save_start;
  char *tag, *label;
  extern int mu_cfg_yydebug;
  
again:
  skipws (p);

  if (*p->curp == '#')
    {
      const char *start = ++p->curp;
      skipline (p);
      if (strncmp (start, "debug=", 6) == 0)
	{
	  mu_log_level_t lev;
	  if (p->curp[0] == '\n')
	    {
	      mu_cfg_locus.line++;
	      *p->curp++ = 0;
	    }
	  if (mu_debug_level_from_string (start + 6, &lev, dbg) == 0)
	    {
	      mu_debug_set_level (dbg, lev);
	      mu_cfg_yydebug = lev & MU_DEBUG_LEVEL_MASK (MU_DEBUG_TRACE1);
	    }
	}
      goto again;
    }
    
  if (*p->curp == '/' && p->curp[1] == '/')
    {
      skipline (p);
      goto again;
    }
	
  if (*p->curp == '/' && p->curp[1] == '*')
    {
      int keep_line = mu_cfg_locus.line;

      p->curp += 2;
      do
	{
	  while (*p->curp != '*')
	    {
	      if (*p->curp == 0)
		{
		  mu_cfg_perror (&mu_cfg_locus,
                       _("unexpected EOF in comment started at line %d"),
				 keep_line);
		  return 0;
		}
	      else if (*p->curp == '\n')
		mu_cfg_locus.line++;
	      ++p->curp;
	    }
	} while (*++p->curp != '/');
      ++p->curp;
      goto again;
    }

  if (*p->curp == 0)
    {
      LEX_DEBUG ("EOF", NULL, NULL);
      return 0;
    }

  if (*p->curp == '"' || *p->curp == '\'')
    {
      mu_cfg_yylval.string = copy_string (p);
      LEX_DEBUG ("STRING", mu_cfg_yylval.string, NULL); 
      return MU_CFG_STRING_TOKEN;
    }

  if (mu_cfg_tie_in)
    {
      mu_cfg_yylval.string = copy_alpha (p);
      LEX_DEBUG ("STRING", mu_cfg_yylval.string, NULL); 
      return MU_CFG_STRING_TOKEN;
    }
	
  if (*p->curp == '}')
    {
      p->curp++;
      memset (&mu_cfg_yylval.node, 0, sizeof mu_cfg_yylval.node);
      mu_cfg_yylval.node.locus = mu_cfg_locus;
      LEX_DEBUG ("END", NULL, NULL);
      return MU_CFG_END_TOKEN;
    }

  if (*p->curp == ';')
    {
      p->curp++;
      LEX_DEBUG ("EOL", NULL, NULL);
      return MU_CFG_EOL_TOKEN;
    }
	
  tag = copy_alpha (p);
  skipws (p);
  if (*p->curp == '"')
    {
      mu_cfg_yylval.string = tag;
      LEX_DEBUG ("STRING", mu_cfg_yylval.string, NULL); 
      return MU_CFG_STRING_TOKEN;
    }
	
  save_start = p->curp;
  if (*p->curp != '{')
    {
      label = copy_alpha (p);
      skipws (p);
    }
  else
    label = NULL;
  if (*p->curp == '{')
    {
      p->curp++;
      mu_cfg_yylval.node.tag_name = tag;
      mu_cfg_yylval.node.locus = mu_cfg_locus;
      mu_cfg_yylval.node.tag_label = label;
      LEX_DEBUG ("START", tag, label); 
      return MU_CFG_START_TOKEN;
    }
  else
    {
      p->curp = save_start;
      mu_cfg_yylval.string = tag;
    }
  LEX_DEBUG ("STRING", mu_cfg_yylval.string, NULL); 
  return MU_CFG_STRING_TOKEN; 
}


static struct mu_cfg_cont *root_container;

int
mu_config_create_container (struct mu_cfg_cont **pcont,
			    enum mu_cfg_cont_type type)
{
  struct mu_cfg_cont *cont;
  int rc;
  
  cont = malloc (sizeof (*cont));
  if (!cont)
    return ENOMEM;
  rc = mu_refcount_create (&cont->refcount);
  if (rc)
    free (cont);
  else
    {
      cont->type = type;
      *pcont = cont;
    }
  return rc; 
}  


struct dup_data
{
  struct mu_cfg_cont *cont;
};

static int dup_container (struct mu_cfg_cont **pcont);

static int
_dup_section_action (void *item, void *cbdata)
{
  int rc;
  struct mu_cfg_cont *cont = item;
  struct dup_data *pdd = cbdata;

  rc = dup_container (&cont);
  if (rc)
    return rc;

  if (!pdd->cont->v.section.subsec)
    {
      int rc = mu_list_create (&pdd->cont->v.section.subsec);
      if (rc)
	return rc;
    }
  return mu_list_append (pdd->cont->v.section.subsec, cont);
}

static int
_dup_param_action (void *item, void *cbdata)
{
  int rc;
  struct mu_cfg_cont *cont = item;
  struct dup_data *pdd = cbdata;
  rc = dup_container (&cont);
  if (rc)
    return rc;
  if (!pdd->cont->v.section.param)
    {
      rc = mu_list_create (&pdd->cont->v.section.param);
      if (rc)
	return rc;
    }
  return mu_list_append (pdd->cont->v.section.param, cont);
}
    
static int
dup_container (struct mu_cfg_cont **pcont)
{
  int rc;
  struct mu_cfg_cont *newcont, *oldcont = *pcont;
  struct dup_data dd;

  rc = mu_config_create_container (&newcont, oldcont->type);
  if (rc)
    return rc;

  dd.cont = newcont;
  switch (oldcont->type)
    {
    case mu_cfg_cont_section:
      newcont->v.section.ident = oldcont->v.section.ident;
      newcont->v.section.parser = oldcont->v.section.parser;
      newcont->v.section.data = oldcont->v.section.data;
      newcont->v.section.subsec = NULL;
      newcont->v.section.param = NULL;
      mu_list_do (oldcont->v.section.subsec, _dup_section_action, &dd);
      mu_list_do (oldcont->v.section.param, _dup_param_action, &dd);
      break;

    case mu_cfg_cont_param:
      newcont->v.param = oldcont->v.param;
      break;
    }
  *pcont = newcont;
  return 0;
}


static void
destroy_list (mu_list_t *plist)
{
  mu_list_t list = *plist;
  mu_iterator_t itr = NULL;
  
  if (!list)
    return;

  mu_list_get_iterator (list, &itr);
  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      struct mu_cfg_cont *cont, *p;
      mu_iterator_current (itr, (void**)&cont);
      p = cont;
      mu_config_destroy_container (&p);
      if (!p)
	mu_list_remove (list, cont);
    }
  mu_iterator_destroy (&itr);
  if (mu_list_is_empty (list))
    mu_list_destroy (plist);
}

void
mu_config_destroy_container (struct mu_cfg_cont **pcont)
{
  struct mu_cfg_cont *cont = *pcont;
  unsigned refcount = mu_refcount_dec (cont->refcount);
  /* printf ("destr %p-%s: %d\n", cont, cont->v.section.ident, refcount); */
  switch (cont->type)
    {
    case mu_cfg_cont_section:
      destroy_list (&cont->v.section.subsec);
      destroy_list (&cont->v.section.param);
      break;

    case mu_cfg_cont_param:
      break;
    }

  if (refcount == 0)
    {
      free (cont);
      *pcont = 0;
    }
}
     

static int
add_parameters (struct mu_cfg_section *sect, struct mu_cfg_param *param)
{
  if (!param)
    return 0;
  if (!sect->param)
    mu_list_create (&sect->param);
  for (; param->ident; param++)
    {
      int rc;
      struct mu_cfg_cont *container;

      rc = mu_config_create_container (&container, mu_cfg_cont_param);
      if (rc)
	return rc;
      container->v.param = *param;
      mu_list_append (sect->param, container);
    }
  return 0;
}

static int
_clone_action (void *item, void *cbdata)
{
  struct mu_cfg_cont *cont = item;
  return mu_config_clone_container (cont);
}

int
mu_config_clone_container (struct mu_cfg_cont *cont)
{
  mu_refcount_inc (cont->refcount);
  /* printf("clone %p-%s: %d\n", cont, cont->v.section.ident, n); */
  switch (cont->type)
    {
    case mu_cfg_cont_section:
      mu_list_do (cont->v.section.subsec, _clone_action, NULL);
      mu_list_do (cont->v.section.param, _clone_action, NULL);
      break;

    case mu_cfg_cont_param:
      break;
    }
  return 0;
}  


int
_mu_config_register_section (struct mu_cfg_cont **proot,
			     const char *parent_path,
			     const char *ident,
			     mu_cfg_section_fp parser,
			     void *data,
			     struct mu_cfg_param *param,
			     struct mu_cfg_section **psection)
{
  int rc;
  struct mu_cfg_section *root_section;
  struct mu_cfg_section *parent;
  
  if (!*proot)
    {
      rc = mu_config_create_container (proot, mu_cfg_cont_section);
      if (rc)
	return rc;
      memset (&(*proot)->v.section, 0, sizeof (*proot)->v.section);
    }
  
  root_section = &(*proot)->v.section;
  
  if (parent_path)
    {
      if (mu_cfg_find_section (root_section, parent_path, &parent))
	return MU_ERR_NOENT;
    }
  else  
    parent = root_section;

  if (mu_refcount_value ((*proot)->refcount) > 1)
    {
      /* It is a clone, do copy-on-write */
      rc = dup_container (proot);
      if (rc)
	return rc;

      root_section = &(*proot)->v.section;
      
      if (parent_path)
	{
	  if (mu_cfg_find_section (root_section, parent_path, &parent))
	    return MU_ERR_NOENT;
	}
      else  
	parent = root_section;
    }

  if (ident)
    {
      struct mu_cfg_cont *container;
      struct mu_cfg_section *s;
      
      if (!parent->subsec)
	mu_list_create (&parent->subsec);
      mu_config_create_container (&container, mu_cfg_cont_section);
      mu_list_append (parent->subsec, container); 
      s = &container->v.section;

      s->ident = strdup (ident);
      s->parser = parser;
      s->data = data;
      s->subsec = NULL;
      s->param = NULL;
      add_parameters (s, param);
      if (psection)
	*psection = s;
    }
  else
    {
      add_parameters (parent, param);
      if (!parent->parser)
	parent->parser = parser;
      if (!parent->data)
	parent->data = data;
      if (psection)
	*psection = parent;
    }
  return 0;
}
  
int
mu_config_register_section (const char *parent_path,
			    const char *ident,
			    mu_cfg_section_fp parser,
			    void *data,
			    struct mu_cfg_param *param)
{
  return _mu_config_register_section (&root_container,
				      parent_path,
				      ident, parser, data, param, NULL);
}

int
mu_config_register_plain_section (const char *parent_path, const char *ident,
				  struct mu_cfg_param *params)
{
  return mu_config_register_section (parent_path, ident, NULL, NULL, params);
}

static int
prog_parser (enum mu_cfg_section_stage stage,
	     const mu_cfg_node_t *node,
	     void *section_data, void *call_data,
	     mu_cfg_tree_t *tree)
{
  if (stage == mu_cfg_section_start)
    {
      return strcmp (node->tag_label, call_data);
    }
  return 0;
}

static char *
make_file_name (const char *dir, const char *file)
{
  char *tmp;
  size_t len = strlen (dir) + 1 + strlen (file);
  tmp = malloc (len + 1);
  if (!tmp)
    {
      mu_error ("%s", mu_strerror (errno));
      exit (1);
    }
  strcpy (tmp, dir);
  strcat (tmp, "/");
  strcat (tmp, file);
  return tmp;
}

struct include_data
{
  const char *progname;
  struct mu_cfg_param *progparam;
  int flags;
};

static int _mu_parse_config (const char *file, const char *progname,
			     struct mu_cfg_param *progparam, int flags);

static int
_cb_include (mu_debug_t debug, void *data, char *arg)
{
  int ret = 0;
  struct stat sb;
  char *dirname = arg;
  struct include_data *idp = data;
  char *tmp = NULL;
  
  if (dirname[0] != '/')
    dirname = tmp = make_file_name (SYSCONFDIR, dirname);
  
  if (stat (dirname, &sb) == 0)
    {
      if (S_ISDIR (sb.st_mode))
	{
	  char *file = make_file_name (dirname, idp->progname);
	  ret = _mu_parse_config (file, idp->progname, idp->progparam, 0);
	}
      else
	ret = _mu_parse_config (dirname, idp->progname, idp->progparam,
				idp->flags);
    }
  else if (errno == ENOENT)
    {
      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			   _("include file or directory does not exist"));
      ret = 1;
    }
  else
    {
      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			   _("cannot stat include file or directory: %s"),
			   mu_strerror (errno));
      ret = 1;
    }
  free (tmp);
  return ret;
}

struct mu_cfg_cont *
mu_build_container (const char *progname,
		    struct mu_cfg_param *progparam, int flags)
{
  struct mu_cfg_cont *cont = root_container;
  struct include_data idata;
  struct mu_cfg_param mu_include_param[] = {
    { "include", mu_cfg_callback, &idata, _cb_include },
    { NULL }
  };
      
  mu_config_clone_container (cont);
  idata.progname = progname;
  idata.progparam = progparam;
  idata.flags = flags & MU_PARSE_CONFIG_GLOBAL;
  _mu_config_register_section (&cont, NULL, NULL, NULL,
			       (void*) progname, mu_include_param, NULL);
  if (flags & MU_PARSE_CONFIG_GLOBAL)
    {
      mu_iterator_t iter;
      struct mu_cfg_section *prog_sect;
      struct mu_cfg_cont *old_root = root_container;
      static struct mu_cfg_param empty_param = { NULL };
      if (!progparam)
	progparam = &empty_param;
      
      _mu_config_register_section (&cont, NULL, "program", prog_parser,
				   (void*) progname,
				   progparam, &prog_sect);
      
      if (old_root->v.section.subsec)
	{
	  if (!prog_sect->subsec)
	    mu_list_create (&prog_sect->subsec);
	  mu_list_get_iterator (old_root->v.section.subsec, &iter);
	  for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
	       mu_iterator_next (iter))
	    {
	      struct mu_cfg_cont *c;
	      mu_iterator_current (iter, (void**)&c);
	      mu_list_append (prog_sect->subsec, c);
	    }
	  mu_iterator_destroy (&iter);
	}
      
      if (old_root->v.section.param)
	{
	  if (!prog_sect->param)
	    mu_list_create (&prog_sect->param);
	  mu_list_get_iterator (old_root->v.section.param, &iter);
	  for (mu_iterator_first (iter); !mu_iterator_is_done (iter);
	       mu_iterator_next (iter))
	    {
	      struct mu_cfg_cont *c;
	      mu_iterator_current (iter, (void**)&c);
	      mu_list_append (prog_sect->param, c);
	    }
	  mu_iterator_destroy (&iter);
	}
    }
  else if (progparam)
    _mu_config_register_section (&cont, NULL, NULL, NULL, NULL,
				 progparam, NULL);
  return cont;
}

int
mu_parse_config_tree (mu_cfg_tree_t *parse_tree, const char *progname,
		      struct mu_cfg_param *progparam, int flags)
{
  int rc = 1;
  
  if (root_container)
    {
      struct mu_cfg_cont *cont = mu_build_container (progname, progparam,
						     flags);
      rc = mu_cfg_scan_tree (parse_tree, &cont->v.section, (void*) progname);
      mu_config_destroy_container (&cont);
    }

  if (flags & MU_PARSE_CONFIG_DUMP)
    {
      mu_stream_t stream;
      mu_stdio_stream_create (&stream, stderr,
			      MU_STREAM_NO_CHECK|MU_STREAM_NO_CLOSE);
      mu_stream_open (stream);
      mu_cfg_format_parse_tree (stream, parse_tree);
      mu_stream_destroy (&stream, NULL);
    }

  return rc;
}

void
mu_format_config_tree (mu_stream_t stream, const char *progname,
		       struct mu_cfg_param *progparam, int flags)
{
  struct mu_cfg_cont *cont = mu_build_container (progname, progparam, flags);
  mu_cfg_format_container (stream, cont);
  mu_config_destroy_container (&cont);
}

static int
_mu_parse_config (const char *file, const char *progname,
		  struct mu_cfg_param *progparam, int flags)
{
  struct lexer_data data;
  struct stat st;
  int fd;
  int rc;
  mu_cfg_tree_t *parse_tree;
  
  if (stat (file, &st))
    {
      if (errno != ENOENT)
	mu_error (_("can't stat `%s'"), file);
      return -1;
    }
  fd = open (file, O_RDONLY);
  if (fd == -1)
    {
      mu_error (_("cannot open config file `%s'"), file);
      return -1;
    }

  if (flags & MU_PARSE_CONFIG_VERBOSE)
    mu_error (_("Info: parsing file `%s'"), file);
  
  memset (&data, 0, sizeof data);
  data.buffer = malloc (st.st_size+1);
        
  read (fd, data.buffer, st.st_size);
  data.buffer[st.st_size] = 0;
  close (fd);
  data.curp = data.buffer;

  /* Parse configuration */
  mu_cfg_locus.file = (char*) file;
  mu_cfg_locus.line = 1;
  rc = mu_cfg_parse (&parse_tree,
		     &data,
		     default_lexer,
		     NULL,
		     NULL,
		     NULL);

  if (rc == 0)
    rc = mu_parse_config_tree (parse_tree, progname, progparam, flags);

  mu_cfg_destroy_tree (&parse_tree);
  mu_list_destroy (&data.mpool);
  free (data.cbuf);
  free (data.buffer);

  if (flags & MU_PARSE_CONFIG_VERBOSE)
    mu_error (_("Info: finished parsing file `%s'"), file);
  
  return rc;
}

int
mu_parse_config (const char *file, const char *progname,
		 struct mu_cfg_param *progparam, int flags)
{
  int rc;
  char *full_name = mu_tilde_expansion (file, "/", NULL);
  if (full_name)
    {
      if (access (full_name, R_OK) == 0)
	{
	  rc = _mu_parse_config (full_name, progname, progparam, flags);
	  free (full_name);
	}
      else
	rc = ENOENT;
    }
  else
    rc = ENOMEM;
  return rc;
}