-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrmate
executable file
·201 lines (163 loc) · 4.72 KB
/
rmate
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
#!/usr/bin/env perl
use 5.8.8;
use strict;
use warnings;
use Cwd qw[abs_path];
use POSIX qw(setsid);
use Getopt::Long;
use IO::Socket;
use Pod::Usage;
use Sys::Hostname;
our $VERSION = "1.1.0";
$SIG{HUP} = "IGNORE";
# Setup defaults
my $action_man = 0;
my $action_help = 0;
my $action_version = 0;
my $host = 'localhost';
my $port = 52698;
my $force = 0;
my $wait = 0;
my $verbose = 0;
my $new_window = 1;
my $line = 0;
my $filetype = '';
# Read configuration from file
my $config = {};
foreach my $configfile ( '/etc/rmate.rc', "$ENV{HOME}/.rmate.rc" ) {
if ( -e $configfile ) {
open(my $fh, '<', $configfile);
while ( my $line = <$fh> ) {
if ( my ($key,$value) = $line =~ /(\w+):\s*(.+)/ ) {
$config->{$key} = $value;
}
}
close($configfile);
}
$host = $config->{host} if $config->{host};
$port = $config->{port} if $config->{port};
}
# Override with options from the environment
$host = $ENV{RMATE_HOST} if $ENV{RMATE_HOST};
$port = $ENV{RMATE_PORT} if $ENV{RMATE_PORT};
# Override with options from commandline
GetOptions(
'help' => \$action_help,
'man' => \$action_man,
'version' => \$action_version,
'H|host' => \$host,
'p|port=i' => \$port,
'w|wait!' => \$wait,
'l|line=i' => \$line,
't|type=s' => \$filetype,
'f|force' => \$force,
'v|verbose+' => \$verbose,
'n|new!' => \$new_window,
) or pod2usage(2);
pod2usage(1) if $action_help;
pod2usage(-exitval => 0, -verbose => 2) if $action_man;
if ( $action_version ) {
print "$0 $VERSION\n";
exit 0;
}
# Get filename from args
my $filename = shift @ARGV;
pod2usage(1) unless $filename;
# Open socket
my $socket = IO::Socket::INET->new(
PeerHost => $host,
PeerPort => $port,
Proto => 'tcp',
) or die $!;
# Read server into (which is always the first line)
my $server_info = $socket->getline;
chomp($server_info);
print "$server_info\n" if $verbose;
# Compute what is shown in the tab, and the absolute path to the file
my $displayname = sprintf("%s:%s", hostname, $filename);
my $abspath = abs_path($filename);
# Initialize editor with contents from file
$socket->print("open\n");
$socket->print("display-name: ${displayname}\n");
$socket->print("real-path: ${abspath}\n");
$socket->print("data-on-save: yes\n");
$socket->print("re-activate: yes\n");
$socket->print("token: ${filename}\n");
if ( $new_window ) {
$socket->print("new: yes\n");
}
if ( $line ) {
$socket->print("selection: ${line}\n");
}
if ( $filetype ) {
$socket->print("file-type: ${filetype}\n");
}
# Fetch file contents if the file exists
my $filesize = -s $filename || 0;
$socket->print("data: ${filesize}\n");
if ( $filesize > 0 ) {
open(my $fh, '<', $abspath);
while ( my $line = <$fh> ) {
$socket->print($line);
}
close($fh);
}
$socket->print("\n.\n");
# Detatch from terminal
unless ( $wait ) {
open(STDIN, "<", "/dev/null") || die "Unable to read /dev/null: $!";
open(STDOUT, ">", "/dev/null") || die "Unable to write to /dev/null: $!";
defined(my $child_pid = fork()) || die "Unable to fork: $!";
# Exit parent process
exit if $child_pid;
(setsid() != -1) || die "Unable to start a new session: $!";
open(STDERR, ">&STDOUT") || die "Unable to dup stdout: $!";
}
# Enter save loop until buffer is closed in editor
while ( my $command = $socket->getline ) {
chomp($command);
# Read commands
my $data = "";
my $options = {};
while ( my $line = $socket->getline ) {
chomp($line);
last unless $line;
my ($name, $value) = split(/:\s*/, $line, 2);
$options->{$name} = $value;
if ( $name eq 'data' ) {
$socket->read($data, $value);
}
}
# Process commands
if ( $command eq 'save' ) {
print "Saveing $filename\n" if $verbose;
my $mode = ( -f $filename ) ? (stat($filename))[2] & 07777 : undef;
open(my $fh, '>', $filename.'-'.$$);
$fh->print($data);
close($fh);
rename($filename.'-'.$$, $filename);
chmod($mode, $filename) if $mode;
}
elsif ( $command eq 'close' ) {
print "Closing $filename\n" if $verbose;
}
}
__END__
=head1 NAME
sample - Using Getopt::Long and Pod::Usage
=head1 SYNOPSIS
rmate [options] [file ...]
Options:
-help brief help message
-man full documentation
=head1 OPTIONS
=over 8
=item B<-help>
Print a brief help message and exits.
=item B<-man>
Prints the manual page and exits.
=back
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do something
useful with the contents thereof.
=cut