summaryrefslogtreecommitdiff
path: root/dbush
blob: 8c6ec801666fa1e5935adea07437448d0868854a (plain)
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
#!/usr/bin/perl

use strict;
use warnings;
use Net::DBus;
use Net::DBus::Reactor;
use Term::ReadLine;
use Getopt::Long;

# TODO: Loading a dbushrc with commands to execute on startup
# TODO: Ctrl+C handling
# TODO: Tab completion
# TODO: Listing interfaces/methods/signals/properties
# TODO: Method invocation
# TODO: Convenient property handling?
# TODO: Capturing/displaying signals
# TODO: Displaying NameOwnerChanged notifications
# TODO: DOCUMENTATION!

# XXX: This script makes use of several undocumented and/or internal features
# of Net::DBus. I'll have to figure out how portable those are. For now I've
# marked all these occurences with "XXX: Undocumented".


die "This command requires the Term::ReadLine::Gnu module, please install it!"
  if Term::ReadLine->ReadLine() ne 'Term::ReadLine::Gnu';


my($conf_system, $conf_session);
GetOptions(
  system => \$conf_system,
  session => \$conf_session,
);

my $histfile = "$ENV{HOME}/.dbush_history";

my $bus = $conf_session ? Net::DBus->session : $conf_system ? Net::DBus->system : Net::DBus->find;
my $term = Term::ReadLine->new('dbush');
my $reactor = Net::DBus::Reactor->main();
my $curobj; # Current Net::DBus::RemoteObject that we're cd'ed to
my $objalive; # Whether the service providing $curobj is alive


# Comparison function for connection names, in order to nicely sort stuff like ":1.10".
sub name_cmp {
  my($fa, $fb) = ($a, $b);
  return $a cmp $b if !($fa =~ s/^://) || !($fb =~ s/^://);
  my @ea = split /\./, $fa;
  my @eb = split /\./, $fb;
  while(@ea) {
    my($ea, $eb) = (shift(@ea)||'', shift(@eb)||'');
    my $c = $ea =~ /^[1-9][0-9]*$/ && $eb =~ /^[1-9][0-9]*$/ ? $ea <=> $eb : $ea cmp $eb;
    return $c if $c;
  }
  return 0;
}


sub getprompt {
  return '[dbush] ' if !$curobj;
  sprintf '[%s %s%s] ',
    $curobj->get_service->get_service_name,
    $curobj->get_object_path,
    $objalive ? '' : ' (dead)';
}


# Resolve a path relative to $curobj or '/'
sub resolvepath {
  my($path, $obj) = @_;
  my @path = split /\//, $path;
  $obj ||= $curobj;

  # If the given path is relative, get a sane root.
  my @root;
  if(!(@path && $path[0] eq '') && $obj) {
    @root = split /\//, $obj->get_object_path;
    shift @root; # Remove first empty element to get rid of the / prefix
  }

  for (@path) {
    next if $_ eq '' || $_ eq '.';
    if($_ eq '..') {
      pop @root;
    } else {
      push @root, $_;
    }
  }
  return '/'.join '/', @root;
}


sub resolveobj {
  my($path, $obj) = @_;
  return ($obj||$curobj)->get_service()->get_object(resolvepath $path, $obj);
}


sub asyncprint(&) {
  # Ugly hack to erase the readline prompt for a bit so that we can output
  # async notification lines. Doesn't work well if the prompt consumes multiple
  # lines on the terminal.
  print "\r\x1B[K";

  shift->();

  $term->rl_set_prompt(getprompt);
  $term->rl_on_new_line;
  $term->rl_redisplay;
}




my %cmd;
sub cmd($&) {
  my $n = shift;
  $cmd{$n} = shift;
}

cmd names => sub {
  my $o = $bus->get_bus_object;
  my %names;
  for(@{$o->ListNames()}) {
    next if $_ eq 'org.freedesktop.DBus';
    my $n = /^:/? $_ : $o->GetNameOwner($_);
    $names{$n} ||= [];
    push @{$names{$n}}, $_ if !/^:/;
  }
  for my $n (sort name_cmp keys %names) {
    printf "  %-8s%s\n", $n, join '', map "  $_", sort @{$names{$n}};
  }
};


cmd sw => sub {
  my($name, $path) = split /\s+/, shift;
  my $o = $bus->get_bus_object;
  return undef $curobj if !$name;
  my @n = grep $_ ne 'org.freedesktop.DBus' && /\Q$name/i, @{$o->ListNames()};
  return print "No connection found that matches '$name'.\n" if !@n;

  # If there are multiple matches, pick the shortest one (e.g. '1.1' matches
  # both '1.1' and '1.10', assume we meant the first).
  my $n = [sort { length($a) <=> length($b) } @n]->[0];
  my $un = $o->GetNameOwner($n);
  printf "%s is owned by %s.\n", $n, $un if $un ne $n;
  $objalive = 1;

  $path ||= '/';
  ($path = $n) =~ s/\./\//g if $path eq '-';
  $curobj = resolveobj $path, $bus->get_service($n)->get_object('/');
};


cmd ls => sub {
  my @args = split /\s+/, shift;
  my($path, $level) = ('', 1);
  for(@args) {
    /^-[rR]$/ and ($level = 999), next;
    $path = $_;
  }
  return print "Not associated with a connection, use 'sw <name>' first\n" if !$curobj;
  return print "Current name is not associated with a connection\n" if !$objalive;

  my $t; $t = sub {
    my($depth, $obj, $name) = @_;
    my @r = ($name);
    return @r if $depth <= 0;
    my $ins = eval { $obj->_introspector() }; # XXX: Undocumented
    my @l = $ins ? $ins->list_children() : ();
    printf '%s: %s', $obj->get_object_path, $@ if !$ins;
    for my $i (0..$#l) {
      my @n = $t->($depth-1, resolveobj($l[$i], $obj), $l[$i]);
      push @r, ($i == $#l ? '└' : '├').'── '.shift(@n);
      push @r, map +($i == $#l ? ' ' : '│').'   '.$_, @n;
    }
    @r;
  };

  $path = resolvepath $path;
  print "$_\n" for ($t->($level, resolveobj($path), $path));
};


cmd cd => sub {
  my $path = shift;
  # TODO: Verify that the object exists? (Ping() or Introspect())
  # TODO: What should a 'cd' without arguments go to?
  return print "Not associated with a connection, use 'sw <name>' first\n" if !$curobj;
  $curobj = resolveobj $path;
};



$term->ReadHistory($histfile);
$term->callback_handler_install(getprompt, sub {
  my $msg = shift;
  exit if !defined $msg;
  my($cmd, $args) = $msg =~ /^\s*([a-z]+)(.*)$/;
  return printf "Unknown command '%s'.\n", $cmd||''
    if !$cmd || !$cmd{$cmd};
  $args =~ s/^\s+//;
  $args =~ s/\s+$//;
  $cmd{$cmd}->($args);
  $term->addhistory($msg);
  $term->rl_set_prompt(getprompt);

  # Flush any object caching in Net::DBus after each command to ensure that we
  # have fresh data on the next command.
  # TODO: Caching is completely disabled this way, even while it certainly
  # isn't such a bad idea in many situations. We probably want to make this
  # caching behaviour configurable.
  if($curobj) {
    # XXX: Undocumented
    $curobj->get_service()->{objects} = {};
    $curobj->{introspected} = 0;
  }
});
$reactor->add_read(0, Net::DBus::Callback->new(method => sub { $term->callback_read_char }));


$bus->get_bus_object()->connect_to_signal(NameOwnerChanged => sub {
  my($name, $old, $new) = @_;
  return if !$curobj || $name ne $curobj->get_service()->get_service_name;
  asyncprint {
    # Message strings inspired by gdbus
    if($new) {
      $objalive = 1;
      # Unique bus names are never re-used, so $name and $new are always
      # different strings if we assume that the $name has been available
      # earlier.
      print "** The name $name is now owned by $new\n";
    } else {
      $objalive = 0;
      print "** The name $name does not have an owner\n";
    }
  };
});


$reactor->run();

END {
  $term->WriteHistory($histfile);
  print "\n";
}