aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/AdminPanel/Module/Firewall.pm204
1 files changed, 134 insertions, 70 deletions
diff --git a/lib/AdminPanel/Module/Firewall.pm b/lib/AdminPanel/Module/Firewall.pm
index 94613c05..9226667c 100644
--- a/lib/AdminPanel/Module/Firewall.pm
+++ b/lib/AdminPanel/Module/Firewall.pm
@@ -30,6 +30,11 @@ use AdminPanel::Shared qw(trim);
use AdminPanel::Shared::GUI;
use AdminPanel::Shared::Firewall;
+use List::Util qw(any);
+use List::MoreUtils qw(uniq);
+
+use MDK::Common::Func qw(if_);
+
extends qw( AdminPanel::Module );
has '+icon' => (
@@ -72,6 +77,13 @@ has 'all_servers' => (
isa => 'ArrayRef',
);
+has 'net' => (
+ is => 'rw',
+ init_arg => undef,
+ isa => 'HashRef',
+ builder => '_initNet',
+);
+
sub _localeInitialize {
my $self = shift();
@@ -201,6 +213,13 @@ sub _initAllServers {
return \@all_servers;
}
+sub _initNet {
+ my $self = shift();
+ my $net = {};
+ network::network::read_net_conf($net);
+ return $net;
+}
+
#=============================================================
=head2 port2server
@@ -222,9 +241,14 @@ sub _initAllServers {
sub port2server {
my $self = shift();
my ($port) = @_;
- find {
- any { $port eq $_ } split(' ', $_->{ports});
- } $self->all_servers();
+ for my $service(@{$self->all_servers()})
+ {
+ if(any { $port eq $_ } split(' ', $service->{ports}))
+ {
+ return $service;
+ }
+ }
+ return 0;
}
#=============================================================
@@ -294,14 +318,20 @@ sub get_conf {
# cause it can't read the interfaces file
return ($shorewall->{disabled}, $self->from_ports($shorewall->{ports}), $shorewall->{log_net_drop});
} else {
- $self->sh_gui->ask_OkCancel({title => $self->loc->N("Firewall configuration"), text => $self->loc->N("drakfirewall configurator
-
-This configures a personal firewall for this Mageia machine."), richtext => 1}) or return;
-
- $self->sh_gui->ask_OkCancel({title => $self->loc->N("Firewall configuration"), text => $self->loc->N("drakfirewall configurator
-
+ $self->sh_gui->ask_OkCancel({
+ title => $self->loc->N("Firewall configuration"),
+ text => $self->loc->N("drakfirewall configurator
+ This configures a personal firewall for this Mageia machine."),
+ richtext => 1
+ }) or return;
+
+ $self->sh_gui->ask_OkCancel({
+ title => $self->loc->N("Firewall configuration"),
+ text => $self->loc->N("drakfirewall configurator
Make sure you have configured your Network/Internet access with
-drakconnect before going any further."), richtext => 1}) or return;
+drakconnect before going any further."),
+ richtext => 1
+ }) or return;
return($disabled, $possible_servers, '');
}
@@ -309,6 +339,73 @@ drakconnect before going any further."), richtext => 1}) or return;
#=============================================================
+=head2 choose_allowed_services
+
+=head3 INPUT
+
+ $self: this object
+
+ $disabled: boolean
+
+ $servers: array of hashes representing servers
+
+ $unlisted: array of hashes with the port not listed (???)
+
+ $log_net_drop: network::shorewall log_net_drop attribute
+
+=head3 DESCRIPTION
+
+ This method shows the main dialog to let users choose the allowed services
+
+=cut
+
+#=============================================================
+
+sub choose_allowed_services {
+ my ($self, $disabled, $servers, $unlisted, $log_net_drop) = @_;
+
+ $_->{on} = 0 foreach @{$self->all_servers()};
+ $_->{on} = 1 foreach @$servers;
+ my @l = grep { $_->{on} || !$_->{hide} } @{$self->all_servers()};
+
+ my $dialog_data = {
+ title => $self->loc->N("Firewall"),
+ icon => $network::shorewall::firewall_icon,
+ # if_(!$::isEmbedded, banner_title => $self->loc->N("Firewall")),
+ banner_title => $self->loc->N("Firewall"),
+ advanced_messages => $self->loc->N("You can enter miscellaneous ports.
+Valid examples are: 139/tcp 139/udp 600:610/tcp 600:610/udp.
+Have a look at /etc/services for information."),
+# callbacks => {
+# complete => sub {
+# if (my $invalid_port = check_ports_syntax($unlisted)) {
+# $in->ask_warn('', $self->loc->N("Invalid port given: %s.
+# The proper format is \"port/tcp\" or \"port/udp\",
+# where port is between 1 and 65535.
+#
+# You can also give a range of ports (eg: 24300:24350/udp)", $invalid_port));
+# return 1;
+# }
+# },
+# }
+ };
+
+ my $items = [
+ { label => $self->loc->N("Which services would you like to allow the Internet to connect to?"), title => 1 },
+ if_($self->net()->{PROFILE} && network::network::netprofile_count() > 0, { label => $self->loc->N("Those settings will be saved for the network profile <b>%s</b>", $self->net()->{PROFILE}) }),
+ { text => $self->loc->N("Everything (no firewall)"), val => \$disabled, type => 'bool' },
+ (map { { text => $_->{name}, val => \$_->{on}, type => 'bool', disabled => sub { $disabled } } } @l),
+ { label => $self->loc->N("Other ports"), val => \$unlisted, advanced => 1, disabled => sub { $disabled } },
+ { text => $self->loc->N("Log firewall messages in system logs"), val => \$log_net_drop, type => 'bool', advanced => 1, disabled => sub { $disabled } },
+ ];
+
+ $self->ask_AllowedServices($dialog_data, $items) or return;
+
+ return ($disabled, [ grep { $_->{on} } @l ], $unlisted, $log_net_drop);
+}
+
+#=============================================================
+
=head2 start
=head3 INPUT
@@ -329,27 +426,29 @@ sub start {
$self->all_servers($self->_initAllServers());
+
+
my ($disabled, $servers, $unlisted, $log_net_drop) = $self->get_conf(undef) or return;
+ ($disabled, $servers, $unlisted, $log_net_drop) = $self->choose_allowed_services($disabled, $servers, $unlisted, $log_net_drop) or return;
- $self->_manageFirewallDialog();
};
#=============================================================
-sub _manageFirewallDialog {
+sub ask_AllowedServices {
my $self = shift;
- ## TODO fix for adminpanel
- my $appTitle = yui::YUI::app()->applicationTitle();
- my $appIcon = yui::YUI::app()->applicationIcon();
+ my ($dlg_data,
+ $items) = @_;
+
+ my $old_title = yui::YUI::app()->applicationTitle();
+
## set new title to get it in dialog
- my $newTitle = $self->loc->N("Manage firewall rules");
- yui::YUI::app()->setApplicationTitle($newTitle);
+ yui::YUI::app()->setApplicationTitle($dlg_data->{title});
my $factory = yui::YUI::widgetFactory;
my $optional = yui::YUI::optionalWidgetFactory;
-
$self->dialog($factory->createMainDialog());
my $layout = $factory->createVBox($self->dialog);
@@ -357,34 +456,26 @@ sub _manageFirewallDialog {
my $headLeft = $factory->createHBox($factory->createLeft($hbox_header));
my $headRight = $factory->createHBox($factory->createRight($hbox_header));
- my $logoImage = $factory->createImage($headLeft, $appIcon);
- my $labelAppDescription = $factory->createLabel($headRight,$newTitle);
+ my $logoImage = $factory->createImage($headLeft, $dlg_data->{icon});
+ my $labelAppDescription = $factory->createLabel($headRight,$dlg_data->{title});
$logoImage->setWeight($yui::YD_HORIZ,0);
$labelAppDescription->setWeight($yui::YD_HORIZ,3);
my $hbox_content = $factory->createHBox($layout);
- my $leftContent = $factory->createLeft($hbox_content);
- $leftContent->setWeight($yui::YD_HORIZ,45);
+ my $widgetContainer = $factory->createVBox($hbox_content);
- for my $v(@{$self->all_servers()})
+ foreach my $item(@{$items})
{
- #use Data::Dumper;
- #print Dumper($v);
+ if(defined($item->{label}))
+ {
+ $factory->createLabel($widgetContainer, $item->{label});
+ }
+ elsif(defined($item->{text}))
+ {
+ $factory->createLabel($widgetContainer, $item->{text} . " - ". $item->{val} . " - " . $item->{type});
+ }
}
-
- my $rightContent = $factory->createRight($hbox_content);
- $rightContent->setWeight($yui::YD_HORIZ,10);
- my $topContent = $factory->createTop($rightContent);
- my $vbox_commands = $factory->createVBox($topContent);
- my $addButton = $factory->createPushButton($factory->createHBox($vbox_commands),$self->loc->N("Add"));
- my $edtButton = $factory->createPushButton($factory->createHBox($vbox_commands),$self->loc->N("Edit"));
- my $remButton = $factory->createPushButton($factory->createHBox($vbox_commands),$self->loc->N("Remove"));
- my $hnButton = $factory->createPushButton($factory->createHBox($vbox_commands),$self->loc->N("Hostname"));
- $addButton->setWeight($yui::YD_HORIZ,1);
- $edtButton->setWeight($yui::YD_HORIZ,1);
- $remButton->setWeight($yui::YD_HORIZ,1);
- $hnButton->setWeight($yui::YD_HORIZ,1);
my $hbox_foot = $factory->createHBox($layout);
my $vbox_foot_left = $factory->createVBox($factory->createLeft($hbox_foot));
@@ -407,46 +498,19 @@ sub _manageFirewallDialog {
my $widget = $event->widget();
if ($widget == $cancelButton) {
last;
- }
- elsif ($widget == $addButton) {
- $self->_addHostDialog();
- $self->setupTable();
- }
- elsif ($widget == $edtButton) {
- my $tblItem = yui::toYTableItem($self->table->selectedItem());
- if($tblItem->cellCount() >= 3){
- $self->_edtHostDialog($tblItem->cell(0)->label(),$tblItem->cell(1)->label(),$tblItem->cell(2)->label());
- }else{
- $self->_edtHostDialog($tblItem->cell(0)->label(),$tblItem->cell(1)->label(),"");
- }
- $self->setupTable();
- }
- elsif ($widget == $remButton) {
- # implement deletion dialog
- if($self->sh_gui->ask_YesOrNo({title => $self->loc->N("Confirmation"), text => $self->loc->N("Are you sure to drop this host?")}) == 1){
- my $tblItem = yui::toYTableItem($self->table->selectedItem());
- # drop the host using the ip
- $self->cfgHosts->_dropHost($tblItem->cell(0)->label());
- # write changes
- $self->cfgHosts->_writeHosts();
- $self->setupTable();
- }
- }elsif ($widget == $hnButton) {
- $self->_changeHostNameDialog("Change the HostName FQDN");
- $self->setupTable();
}elsif ($widget == $aboutButton) {
$self->sh_gui->AboutDialog({
- name => $appTitle,
+ name => $dlg_data->{title},
version => $VERSION,
- credits => "Copyright (c) 2013-2014 by Matteo Pasotti",
+ credits => "Copyright (c) 2013-2015 by Matteo Pasotti",
license => "GPLv2",
- description => $self->loc->N("Graphical manager for hosts definitions"),
+ description => $self->loc->N("Graphical manager for firewall rules"),
authors => "Matteo Pasotti &lt;matteo.pasotti\@gmail.com&gt;"
}
);
}elsif ($widget == $okButton) {
# write changes
- $self->cfgHosts->_writeHosts();
+ return
last;
}
}
@@ -455,7 +519,7 @@ sub _manageFirewallDialog {
$self->dialog->destroy() ;
#restore old application title
- yui::YUI::app()->setApplicationTitle($appTitle);
+ yui::YUI::app()->setApplicationTitle($old_title);
}
1;
'>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
/* SLang_read_line interface --- uses SLang tty stuff */
/* Copyright (c) 1992, 1999, 2001 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

#include "slinclud.h"

#include "slang.h"
#include "_slang.h"

#ifdef REAL_UNIX_SYSTEM
int SLang_RL_EOF_Char = 4;
#else
int SLang_RL_EOF_Char = 26;
#endif

int SLang_Rline_Quit;
static SLang_RLine_Info_Type *This_RLI;

static unsigned char Char_Widths[256];
static void position_cursor (int);

static void rl_beep (void)
{
   putc(7, stdout);
   fflush (stdout);
}

/* editing functions */
static int rl_bol (void)
{
   if (This_RLI->point == 0) return 0;
   This_RLI->point = 0;
   return 1;
}

static int rl_eol (void)
{
   if (This_RLI->point == This_RLI->len) return 0;
   This_RLI->point = This_RLI->len;
   return 1;
}

static int rl_right (void)
{
   if (This_RLI->point == This_RLI->len) return 0;
   This_RLI->point++;
   return 1;
}

static int rl_left (void)
{
   if (This_RLI->point == 0) return 0;
   This_RLI->point--;
   return 1;
}

static int rl_self_insert (void)
{
   unsigned char *pmin, *p;

   if (This_RLI->len == This_RLI->buf_len)
     {
	rl_beep ();
	return 0;
     }

   pmin = This_RLI->buf + This_RLI->point;
   p = This_RLI->buf + This_RLI->len;
   while (p > pmin)
     {
	*p = *(p - 1);
	p--;
     }
   *pmin = SLang_Last_Key_Char;

   This_RLI->len++;
   This_RLI->point++;
   if ((This_RLI->curs_pos + 2 >= This_RLI->edit_width)
       || (This_RLI->tt_insert == NULL)
       || (Char_Widths[SLang_Last_Key_Char] != 1)) return 1;

   (*This_RLI->tt_insert)((char) SLang_Last_Key_Char);
   /* update screen buf */
   p = This_RLI->old_upd + (This_RLI->len - 1);
   pmin = This_RLI->old_upd + (This_RLI->point - 1);
   while (p > pmin)
     {
	*p = *(p - 1);
	p--;
     }
   *pmin = SLang_Last_Key_Char;
   return 0;
}

int SLang_rline_insert (char *s)
{
   unsigned char *pmin, *p;
   int n;

   n = strlen (s);
   if (n > This_RLI->buf_len - This_RLI->len)
     n = This_RLI->buf_len - This_RLI->len;

   if (n == 0) return 0;

   pmin = This_RLI->buf + This_RLI->point;
   p = This_RLI->buf + (This_RLI->len - 1);

   while (p >= pmin)
     {
	*(p + n) = *p;
	p--;
     }
   SLMEMCPY ((char *) pmin, s, n);

   This_RLI->len += n;
   This_RLI->point += n;
   return n;
}

static int rl_deln (int n)
{
   unsigned char *pmax, *p;

   p = This_RLI->buf + This_RLI->point;
   pmax = This_RLI->buf + This_RLI->len;

   if (p + n > pmax) n = (int) (pmax - p);
   while (p < pmax)
     {
	*p = *(p + n);
	p++;
     }
   This_RLI->len -= n;
   return n;
}

static int rl_del (void)
{
   return rl_deln(1);
}

static int rl_quote_insert (void)
{
   int err = SLang_Error;
   SLang_Error = 0;
   SLang_Last_Key_Char = (*This_RLI->getkey)();
   rl_self_insert ();
   if (SLang_Error == SL_USER_BREAK) SLang_Error = 0;
   else SLang_Error = err;
   return 1;
}

static int rl_trim (void)
{
   unsigned char *p, *pmax, *p1;
   p = This_RLI->buf + This_RLI->point;
   pmax = This_RLI->buf + This_RLI->len;

   if (p == pmax)
     {
	if (p == This_RLI->buf) return 0;
	p--;
     }

   if ((*p != ' ') && (*p != '\t')) return 0;
   p1 = p;
   while ((p1 < pmax) && ((*p1 == ' ') || (*p1 == '\t'))) p1++;
   pmax = p1;
   p1 = This_RLI->buf;

   while ((p >= p1) && ((*p == ' ') || (*p == '\t'))) p--;
   if (p == pmax) return 0;
   p++;

   This_RLI->point = (int) (p - p1);
   return rl_deln ((int) (pmax - p));
}

static int rl_bdel (void)
{
   if (rl_left()) return rl_del();
   return 0;
}

static int rl_deleol (void)
{
   if (This_RLI->point == This_RLI->len) return 0;
   *(This_RLI->buf + This_RLI->point) = 0;
   This_RLI->len = This_RLI->point;
   return 1;
}

static int rl_delete_line (void)
{
   This_RLI->point = 0;
   *(This_RLI->buf + This_RLI->point) = 0;
   This_RLI->len = 0;
   return 1;
}

static int rl_enter (void)
{
   *(This_RLI->buf + This_RLI->len) = 0;
   SLang_Rline_Quit = 1;
   return 1;
}

static SLKeyMap_List_Type *RL_Keymap;

/* This update is designed for dumb terminals.  It assumes only that the
 * terminal can backspace via ^H, and move cursor to start of line via ^M.
 * There is a hook so the user can provide a more sophisticated update if
 * necessary.
 */

static void position_cursor (int col)
{
   unsigned char *p, *pmax;
   int dc;

   if (col == This_RLI->curs_pos)
     {
	fflush (stdout);
	return;
     }

   if (This_RLI->tt_goto_column != NULL)
     {
	(*This_RLI->tt_goto_column)(col);
	This_RLI->curs_pos = col;
	fflush (stdout);
	return;
     }

   dc = This_RLI->curs_pos - col;
   if (dc < 0)
     {
	p = This_RLI->new_upd + This_RLI->curs_pos;
	pmax = This_RLI->new_upd + col;
	while (p < pmax) putc((char) *p++, stdout);
     }
   else
     {
	if (dc < col)
	  {
	     while (dc--) putc(8, stdout);
	  }
	else
	  {
	     putc('\r', stdout);
	     p = This_RLI->new_upd;
	     pmax = This_RLI->new_upd + col;
	     while (p < pmax) putc((char) *p++, stdout);
	  }
     }
   This_RLI->curs_pos = col;
   fflush (stdout);
}

static void erase_eol (SLang_RLine_Info_Type *rli)
{
   unsigned char *p, *pmax;

   p = rli->old_upd + rli->curs_pos;
   pmax = rli->old_upd + rli->old_upd_len;

   while (p++ < pmax) putc(' ', stdout);

   rli->curs_pos = rli->old_upd_len;
}

static unsigned char *spit_out(SLang_RLine_Info_Type *rli, unsigned char *p)
{
   unsigned char *pmax;
   position_cursor ((int) (p - rli->new_upd));
   pmax = rli->new_upd + rli->new_upd_len;
   while (p < pmax) putc((char) *p++, stdout);
   rli->curs_pos = rli->new_upd_len;
   return pmax;
}

static void really_update (SLang_RLine_Info_Type *rli, int new_curs_position)
{
   unsigned char *b = rli->old_upd, *p = rli->new_upd, chb, chp;
   unsigned char *pmax;

   if (rli->update_hook != NULL)
     {
	(*rli->update_hook)(p, rli->edit_width, new_curs_position);
     }
   else
     {
	pmax = p + rli->edit_width;
	while (p < pmax)
	  {
	     chb = *b++; chp = *p++;
	     if (chb == chp) continue;

	     if (rli->old_upd_len <= rli->new_upd_len)
	       {
		  /* easy one */
		  (void) spit_out (rli, p - 1);
		  break;
	       }
	     spit_out(rli, p - 1);
	     erase_eol (rli);
	     break;
	  }
	position_cursor (new_curs_position);
     }

   /* update finished, so swap */

   rli->old_upd_len = rli->new_upd_len;
   p = rli->old_upd;
   rli->old_upd = rli->new_upd;
   rli->new_upd = p;
}

static void RLupdate (SLang_RLine_Info_Type *rli)
{
   int len, dlen, start_len = 0, prompt_len = 0, tw = 0, count;
   int want_cursor_pos;
   unsigned char *b, chb, *b_point, *p;
   int no_echo;

   no_echo = rli->flags & SL_RLINE_NO_ECHO;

   b_point = (unsigned char *) (rli->buf + rli->point);
   *(rli->buf + rli->len) = 0;

   /* expand characters for output buffer --- handle prompt first.
    * Do two passes --- first to find out where to begin upon horiz
    * scroll and the second to actually fill the buffer. */
   len = 0;
   count = 2;			       /* once for prompt and once for buf */

   b = (unsigned char *) rli->prompt;
   while (count--)
     {
	if ((count == 0) && no_echo)
	  break;

	/* The prompt could be NULL */
	if (b != NULL) while ((chb = *b) != 0)
	  {
	     /* This will ensure that the screen is scrolled a third of the edit
	      * width each time */
	     if (b_point == b) break;
	     dlen = Char_Widths[chb];
	     if ((chb == '\t') && tw)
	       {
		  dlen = tw * ((len - prompt_len) / tw + 1) - (len - prompt_len);
	       }
	     len += dlen;
	     b++;
	  }
	tw = rli->tab;
	b = (unsigned char *) rli->buf;
	if (count == 1) want_cursor_pos = prompt_len = len;
     }

   if (len < rli->edit_width - rli->dhscroll) start_len = 0;
   else if ((rli->start_column > len)
	    || (rli->start_column + rli->edit_width <= len))
     {
	start_len = len - (rli->edit_width - rli->dhscroll);
	if (start_len < 0) start_len = 0;
     }
   else start_len = rli->start_column;
   rli->start_column = start_len;

   want_cursor_pos = len - start_len;

   /* second pass */
   p = rli->new_upd;

   len = 0;
   count = 2;
   b = (unsigned char *) rli->prompt;
   if (b == NULL) b = (unsigned char *) "";

   while ((len < start_len) && (*b))
     {
	len += Char_Widths[*b++];
     }

   tw = 0;
   if (*b == 0)
     {
	b = (unsigned char *) rli->buf;
	while (len < start_len)
	  {
	     len += Char_Widths[*b++];
	  }
	tw = rli->tab;
	count--;
     }

   len = 0;
   while (count--)
     {
	if ((count == 0) && (no_echo))
	  break;

	while ((len < rli->edit_width) && ((chb = *b++) != 0))
	  {
	     dlen = Char_Widths[chb];
	     if (dlen == 1) *p++ = chb;
	     else
	       {
		  if ((chb == '\t') && tw)
		    {
		       dlen = tw * ((len + start_len - prompt_len) / tw + 1) - (len + start_len - prompt_len);
		       len += dlen;	       /* ok since dlen comes out 0  */
		       if (len > rli->edit_width) dlen = len - rli->edit_width;
		       while (dlen--) *p++ = ' ';
		       dlen = 0;
		    }
		  else
		    {
		       if (dlen == 3)
			 {
			    chb &= 0x7F;
			    *p++ = '~';
			 }

		       *p++ = '^';
		       if (chb == 127)  *p++ = '?';
		       else *p++ = chb + '@';
		    }
	       }
	     len += dlen;
	  }
	/* if (start_len > prompt_len) break; */
	tw = rli->tab;
	b = (unsigned char *) rli->buf;
     }

   rli->new_upd_len = (int) (p - rli->new_upd);
   while (p < rli->new_upd + rli->edit_width) *p++ = ' ';
   really_update (rli, want_cursor_pos);
}

void SLrline_redraw (SLang_RLine_Info_Type *rli)
{
   unsigned char *p = rli->new_upd;
   unsigned char *pmax = p + rli->edit_width;
   while (p < pmax) *p++ = ' ';
   rli->new_upd_len = rli->edit_width;
   really_update (rli, 0);
   RLupdate (rli);
}

static int rl_eof_insert (void)
{
   if (This_RLI->len == 0)
     {
	SLang_Last_Key_Char = SLang_RL_EOF_Char;
	/* rl_self_insert (); */
	return rl_enter ();
     }
   return 0;
}

/* This is very naive.  It knows very little about nesting and nothing
 * about quoting.
 */
static void blink_match (SLang_RLine_Info_Type *rli)
{
   unsigned char bra, ket;
   unsigned int delta_column;
   unsigned char *p, *pmin;
   int dq_level, sq_level;
   int level;

   pmin = rli->buf;
   p = pmin + rli->point;
   if (pmin == p)
     return;

   ket = SLang_Last_Key_Char;
   switch (ket)
     {
      case ')':
	bra = '(';
	break;
      case ']':
	bra = '[';
	break;
      case '}':
	bra = '{';
	break;
      default:
	return;
     }

   level = 0;
   sq_level = dq_level = 0;

   delta_column = 0;
   while (p > pmin)
     {
	char ch;

	p--;
	delta_column++;
	ch = *p;

	if (ch == ket)
	  {
	     if ((dq_level == 0) && (sq_level == 0))
	       level++;
	  }
	else if (ch == bra)
	  {
	     if ((dq_level != 0) || (sq_level != 0))
	       continue;

	     level--;
	     if (level == 0)
	       {
		  rli->point -= delta_column;
		  RLupdate (rli);
		  (*rli->input_pending)(10);
		  rli->point += delta_column;
		  RLupdate (rli);
		  break;
	       }
	     if (level < 0)
	       break;
	  }
	else if (ch == '"') dq_level = !dq_level;
	else if (ch == '\'') sq_level = !sq_level;
     }
}

int SLang_read_line (SLang_RLine_Info_Type *rli)
{
   unsigned char *p, *pmax;
   SLang_Key_Type *key;

   SLang_Rline_Quit = 0;
   This_RLI = rli;
   p = rli->old_upd; pmax = p + rli->edit_width;
   while (p < pmax) *p++ = ' ';

   /* Sanity checking */
   rli->len = strlen ((char *) rli->buf);
   if (rli->len >= rli->buf_len)
     {
	rli->len = 0;
	*rli->buf = 0;
     }
   if (rli->point > rli->len) rli->point = rli->len;
   if (rli->point < 0) rli->point = 0;

   rli->curs_pos = rli->start_column = 0;
   rli->new_upd_len = rli->old_upd_len = 0;

   This_RLI->last_fun = NULL;
   if (rli->update_hook == NULL)
     putc ('\r', stdout);

   RLupdate (rli);

   while (1)
     {
	key = SLang_do_key (RL_Keymap, (int (*)(void)) rli->getkey);

	if ((key == NULL) || (key->f.f == NULL))
	  rl_beep ();
	else
	  {
	     if ((SLang_Last_Key_Char == SLang_RL_EOF_Char)
		 && (*key->str == 2)
		 && (This_RLI->len == 0))
	       rl_eof_insert ();
	     else if (key->type == SLKEY_F_INTRINSIC)
	       {
		  if ((key->f.f)())
		    RLupdate (rli);

		  if ((rli->flags & SL_RLINE_BLINK_MATCH)
		      && (rli->input_pending != NULL))
		    blink_match (rli);
	       }

	     if (SLang_Rline_Quit)
	       {
		  This_RLI->buf[This_RLI->len] = 0;
		  if (SLang_Error == SL_USER_BREAK)
		    {
		       SLang_Error = 0;
		       return -1;
		    }
		  return This_RLI->len;
	       }
	  }
	if (key != NULL)
	  This_RLI->last_fun = key->f.f;
     }
}

static int rl_abort (void)
{
   rl_delete_line ();
   return rl_enter ();
}

/* TTY interface --- ANSI */

static void ansi_goto_column (int n)
{
   putc('\r', stdout);
   if (n) fprintf(stdout, "\033[%dC", n);
}

static void rl_select_line (SLang_Read_Line_Type *p)
{
   This_RLI->last = p;
   strcpy ((char *) This_RLI->buf, (char *) p->buf);
   This_RLI->point = This_RLI->len = strlen((char *) p->buf);
}
static int rl_next_line (void);
static int rl_prev_line (void)
{
   SLang_Read_Line_Type *prev;

   if (((This_RLI->last_fun != (FVOID_STAR) rl_prev_line)
	&& (This_RLI->last_fun != (FVOID_STAR) rl_next_line))
       || (This_RLI->last == NULL))
     {
	prev = This_RLI->tail;
     }
   else prev = This_RLI->last->prev;

   if (prev == NULL)
     {
	rl_beep ();
	return 0;
     }

   rl_select_line (prev);
   return 1;
}
static int rl_redraw (void)
{
   SLrline_redraw (This_RLI);
   return 1;
}

static int rl_next_line (void)
{
   SLang_Read_Line_Type *next;

   if (((This_RLI->last_fun != (FVOID_STAR) rl_prev_line)
	&& (This_RLI->last_fun != (FVOID_STAR) rl_next_line))
       || (This_RLI->last == NULL))
      {
	 rl_beep ();
	 return 0;
      }

   next = This_RLI->last->next;

   if (next == NULL)
     {
	This_RLI->len = This_RLI->point = 0;
	*This_RLI->buf = 0;
	This_RLI->last = NULL;
     }
   else rl_select_line (next);
   return 1;
}

static SLKeymap_Function_Type SLReadLine_Functions[] =
{
   {"up", rl_prev_line},
   {"down", rl_next_line},
   {"bol", rl_bol},
   {"eol", rl_eol},
   {"right", rl_right},
   {"left", rl_left},
   {"self_insert", rl_self_insert},
   {"bdel", rl_bdel},
   {"del", rl_del},
   {"deleol", rl_deleol},
   {"enter", rl_enter},
   {"trim", rl_trim},
   {"quoted_insert", rl_quote_insert},
   {(char *) NULL, NULL}
};

int SLang_init_readline (SLang_RLine_Info_Type *rli)
{
   int ch;
   char simple[2];

   if (RL_Keymap == NULL)
     {
	simple[1] = 0;
	if (NULL == (RL_Keymap = SLang_create_keymap ("ReadLine", NULL)))
	  return -1;

	RL_Keymap->functions = SLReadLine_Functions;

	/* This breaks under some DEC ALPHA compilers (scary!) */
#ifndef __DECC
	for (ch = ' '; ch < 256; ch++)
	  {
	     simple[0] = (char) ch;
	     SLkm_define_key (simple, (FVOID_STAR) rl_self_insert, RL_Keymap);
	  }
#else
	ch = ' ';
	while (1)
	  {
	     simple[0] = (char) ch;
	     SLkm_define_key (simple, (FVOID_STAR) rl_self_insert, RL_Keymap);
	     ch = ch + 1;
	     if (ch == 256) break;
	  }
#endif				       /* NOT __DECC */

	simple[0] = SLang_Abort_Char;
	SLkm_define_key (simple, (FVOID_STAR) rl_abort, RL_Keymap);
	simple[0] = SLang_RL_EOF_Char;
	SLkm_define_key (simple, (FVOID_STAR) rl_eof_insert, RL_Keymap);

#ifndef IBMPC_SYSTEM
	SLkm_define_key  ("^[[A", (FVOID_STAR) rl_prev_line, RL_Keymap);
	SLkm_define_key  ("^[[B", (FVOID_STAR) rl_next_line, RL_Keymap);
	SLkm_define_key  ("^[[C", (FVOID_STAR) rl_right, RL_Keymap);
	SLkm_define_key  ("^[[D", (FVOID_STAR) rl_left, RL_Keymap);
	SLkm_define_key  ("^[OA", (FVOID_STAR) rl_prev_line, RL_Keymap);
	SLkm_define_key  ("^[OB", (FVOID_STAR) rl_next_line, RL_Keymap);
	SLkm_define_key  ("^[OC", (FVOID_STAR) rl_right, RL_Keymap);
	SLkm_define_key  ("^[OD", (FVOID_STAR) rl_left, RL_Keymap);
#else
	SLkm_define_key  ("^@H", (FVOID_STAR) rl_prev_line, RL_Keymap);
	SLkm_define_key  ("^@P", (FVOID_STAR) rl_next_line, RL_Keymap);
	SLkm_define_key  ("^@M", (FVOID_STAR) rl_right, RL_Keymap);
	SLkm_define_key  ("^@K", (FVOID_STAR) rl_left, RL_Keymap);
	SLkm_define_key  ("^@S", (FVOID_STAR) rl_del, RL_Keymap);
	SLkm_define_key  ("^@O", (FVOID_STAR) rl_eol, RL_Keymap);
	SLkm_define_key  ("^@G", (FVOID_STAR) rl_bol, RL_Keymap);

	SLkm_define_key  ("\xE0H", (FVOID_STAR) rl_prev_line, RL_Keymap);
	SLkm_define_key  ("\xE0P", (FVOID_STAR) rl_next_line, RL_Keymap);
	SLkm_define_key  ("\xE0M", (FVOID_STAR) rl_right, RL_Keymap);