Commit 1ae9bae2 1ae9bae29836a20784c836b630264b9e15e5af13 by Sergey Poznyakoff

mail: Use new list functions to simplify vartab-related operations.

* mail/mailvar.c (_mailvar_symbol_to_array): Remove.
(_mailvar_symbol_to_list): New function.
(mailvar_make_array): Remove.
(mailvar_list_copy): New function.
(mailvar_iterator)<varptr,varcnt,pos>: Remove.
<varlist,varitr>: New members.
(mailvar_iterate_next): Use subordinate iterator.
(mailvar_iterate_first): Likewise.
(mailvar_print): Use mailvar_make_array.
1 parent 7b2c07f9
...@@ -343,10 +343,10 @@ mailvar_variable_comp (const void *a, const void *b) ...@@ -343,10 +343,10 @@ mailvar_variable_comp (const void *a, const void *b)
343 static int 343 static int
344 mailvar_varptr_comp (const void *a, const void *b) 344 mailvar_varptr_comp (const void *a, const void *b)
345 { 345 {
346 const struct mailvar_variable * const * v1 = a; 346 const struct mailvar_variable const * v1 = a;
347 const struct mailvar_variable * const * v2 = b; 347 const struct mailvar_variable const *v2 = b;
348 348
349 return strcmp ((*v1)->name, (*v2)->name); 349 return strcmp (v1->name, v2->name);
350 } 350 }
351 351
352 /* Find mailvar_list entry VAR. If not found and CREATE is not NULL, then 352 /* Find mailvar_list entry VAR. If not found and CREATE is not NULL, then
...@@ -602,35 +602,34 @@ _mailvar_symbol_count (int set) ...@@ -602,35 +602,34 @@ _mailvar_symbol_count (int set)
602 } 602 }
603 } 603 }
604 604
605 void 605 static int
606 _mailvar_symbol_to_array (int set, struct mailvar_variable **vartab) 606 mailvar_mapper (void **itmv, size_t itmc, void *call_data)
607 {
608 return MU_LIST_MAP_OK;
609 }
610
611 int
612 _mailvar_symbol_to_list (int set, mu_list_t list)
607 { 613 {
608 struct mailvar_symbol *s; 614 struct mailvar_symbol *s;
609 for (s = mailvar_tab; s->var.name; s++) 615 for (s = mailvar_tab; s->var.name; s++)
610 if (!set || s->var.set) 616 if (!set || s->var.set)
611 { 617 mu_list_append (list, &s->var);
612 *vartab = &s->var; 618 return 0;
613 vartab++;
614 }
615 } 619 }
616 620
617 struct mailvar_variable ** 621 mu_list_t
618 mailvar_make_array (int set, size_t *pcount) 622 mailvar_list_copy (int set)
619 { 623 {
620 struct mailvar_variable **vartab; 624 mu_list_t list;
621 size_t count = 0, symcount; 625
622 626 if (mailvar_list)
623 symcount = _mailvar_symbol_count (set); 627 mu_list_map (mailvar_list, mailvar_mapper, NULL, 1, &list);
624 mu_list_count (mailvar_list, &count); 628 else
625 vartab = xcalloc (symcount + count + 1, sizeof *vartab); 629 mu_list_create (&list);
626 mu_list_to_array (mailvar_list, (void**) vartab, count, NULL); 630 _mailvar_symbol_to_list (set, list);
627 _mailvar_symbol_to_array (set, vartab + count); 631 mu_list_sort (list, mailvar_varptr_comp);
628 count += symcount; 632 return list;
629 qsort (vartab, count, sizeof *vartab, mailvar_varptr_comp);
630 vartab[count] = NULL;
631 if (pcount)
632 *pcount = count;
633 return vartab;
634 } 633 }
635 634
636 635
...@@ -638,24 +637,25 @@ struct mailvar_iterator ...@@ -638,24 +637,25 @@ struct mailvar_iterator
638 { 637 {
639 const char *prefix; 638 const char *prefix;
640 int prefixlen; 639 int prefixlen;
641 struct mailvar_variable **varptr; 640 mu_list_t varlist;
642 size_t varcnt; 641 mu_iterator_t varitr;
643 size_t pos;
644 }; 642 };
645 643
646 const char * 644 const char *
647 mailvar_iterate_next (struct mailvar_iterator *itr) 645 mailvar_iterate_next (struct mailvar_iterator *itr)
648 { 646 {
649 struct mailvar_variable *vp; 647 struct mailvar_variable *vp;
650 648
651 while (itr->pos < itr->varcnt) 649 do
652 { 650 {
653 vp = itr->varptr[itr->pos++]; 651 mu_iterator_current (itr->varitr, (void**) &vp);
654 652 mu_iterator_next (itr->varitr);
653
655 if (strlen (vp->name) >= itr->prefixlen 654 if (strlen (vp->name) >= itr->prefixlen
656 && strncmp (vp->name, itr->prefix, itr->prefixlen) == 0) 655 && strncmp (vp->name, itr->prefix, itr->prefixlen) == 0)
657 return vp->name; 656 return vp->name;
658 } 657 }
658 while (!mu_iterator_is_done (itr->varitr));
659 return NULL; 659 return NULL;
660 } 660 }
661 661
...@@ -665,8 +665,9 @@ mailvar_iterate_first (int set, const char *prefix, struct mailvar_iterator **pi ...@@ -665,8 +665,9 @@ mailvar_iterate_first (int set, const char *prefix, struct mailvar_iterator **pi
665 struct mailvar_iterator *itr = xmalloc (sizeof *itr); 665 struct mailvar_iterator *itr = xmalloc (sizeof *itr);
666 itr->prefix = prefix; 666 itr->prefix = prefix;
667 itr->prefixlen = strlen (prefix); 667 itr->prefixlen = strlen (prefix);
668 itr->varptr = mailvar_make_array (set, &itr->varcnt); 668 itr->varlist = mailvar_list_copy (set);
669 itr->pos = 0; 669 mu_list_get_iterator (itr->varlist, &itr->varitr);
670 mu_iterator_first (itr->varitr);
670 *pitr = itr; 671 *pitr = itr;
671 return mailvar_iterate_next (itr); 672 return mailvar_iterate_next (itr);
672 } 673 }
...@@ -677,68 +678,80 @@ mailvar_iterate_end (struct mailvar_iterator **pitr) ...@@ -677,68 +678,80 @@ mailvar_iterate_end (struct mailvar_iterator **pitr)
677 if (pitr && *pitr) 678 if (pitr && *pitr)
678 { 679 {
679 struct mailvar_iterator *itr = *pitr; 680 struct mailvar_iterator *itr = *pitr;
680 free (itr->varptr); 681 mu_iterator_destroy (&itr->varitr);
682 mu_list_destroy (&itr->varlist);
681 free (itr); 683 free (itr);
682 *pitr = NULL; 684 *pitr = NULL;
683 } 685 }
684 } 686 }
685 687
686 void 688 struct mailvar_print_closure
687 mailvar_print (int set)
688 { 689 {
689 struct mailvar_variable **vartab; 690 int prettyprint;
690 mu_stream_t out; 691 mu_stream_t out;
691 size_t i, count; 692 int width;
692 int width = util_getcols (); 693 };
693 int prettyprint = mailvar_get (NULL, "variable-pretty-print",
694 mailvar_type_boolean, 0) == 0;
695
696 vartab = mailvar_make_array (set, &count);
697
698 out = open_pager (count);
699 694
700 for (i = 0; i < count; i++) 695 static int
696 mailvar_printer (void *item, void *data)
697 {
698 struct mailvar_variable *vp = item;
699 struct mailvar_print_closure *clos = data;
700
701 if (clos->prettyprint)
701 { 702 {
702 if (prettyprint) 703 const struct mailvar_symbol *sym = find_mailvar_symbol (vp->name);
703 {
704 const struct mailvar_symbol *sym =
705 find_mailvar_symbol (vartab[i]->name);
706 704
707 if (sym) 705 if (sym)
708 {
709 if (sym->flags & MAILVAR_HIDDEN)
710 continue;
711 if (sym->flags & MAILVAR_RDONLY)
712 mu_stream_printf (out, "# %s:\n", _("Read-only variable"));
713 print_descr (out, gettext (sym->descr), 1, 3, width - 1, "# ");
714 }
715 }
716 switch (vartab[i]->type)
717 { 706 {
718 case mailvar_type_number: 707 if (sym->flags & MAILVAR_HIDDEN)
719 mu_stream_printf (out, "%s=%d", 708 return 0;
720 vartab[i]->name, vartab[i]->value.number); 709 if (sym->flags & MAILVAR_RDONLY)
721 break; 710 mu_stream_printf (clos->out, "# %s:\n", _("Read-only variable"));
711 print_descr (clos->out, gettext (sym->descr), 1, 3,
712 clos->width - 1, "# ");
713 }
714 }
715 switch (vp->type)
716 {
717 case mailvar_type_number:
718 mu_stream_printf (clos->out, "%s=%d", vp->name, vp->value.number);
719 break;
722 720
723 case mailvar_type_string: 721 case mailvar_type_string:
724 mu_stream_printf (out, "%s=\"%s\"", 722 mu_stream_printf (clos->out, "%s=\"%s\"", vp->name, vp->value.string);
725 vartab[i]->name, vartab[i]->value.string); 723 break;
726 break;
727 724
728 case mailvar_type_boolean: 725 case mailvar_type_boolean:
729 if (!vartab[i]->value.bool) 726 if (!vp->value.bool)
730 mu_stream_printf (out, "no"); 727 mu_stream_printf (clos->out, "no");
731 mu_stream_printf (out, "%s", vartab[i]->name); 728 mu_stream_printf (clos->out, "%s", vp->name);
732 break; 729 break;
733 730
734 case mailvar_type_whatever: 731 case mailvar_type_whatever:
735 mu_stream_printf (out, "%s %s", vartab[i]->name, _("oops?")); 732 mu_stream_printf (clos->out, "%s %s", vp->name, _("oops?"));
736 }
737 mu_stream_printf (out, "\n");
738 } 733 }
739 free (vartab); 734 mu_stream_printf (clos->out, "\n");
735 return 0;
736 }
740 737
741 mu_stream_unref (out); 738 void
739 mailvar_print (int set)
740 {
741 mu_list_t varlist;
742 size_t count;
743 struct mailvar_print_closure clos;
744
745 varlist = mailvar_list_copy (set);
746 mu_list_count (varlist, &count);
747 clos.out = open_pager (count);
748 clos.prettyprint = mailvar_get (NULL, "variable-pretty-print",
749 mailvar_type_boolean, 0) == 0;
750 clos.width = util_getcols ();
751
752 mu_list_foreach (varlist, mailvar_printer, &clos);
753 mu_list_destroy (&varlist);
754 mu_stream_unref (clos.out);
742 } 755 }
743 756
744 757
......