From 2a9a2adee49d1e09425fecfe3f615adc40678042 Mon Sep 17 00:00:00 2001 From: Andrew Chilton Date: Mon, 23 Jun 2008 19:01:53 +1200 Subject: [PATCH] Remove dependency on Term::CallEditor. --- bin/cil | 13 +++++---- lib/CIL/Utils.pm | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/bin/cil b/bin/cil index 6684179..63d9d26 100755 --- a/bin/cil +++ b/bin/cil @@ -28,7 +28,6 @@ use File::Touch; use File::Glob ':glob'; use File::Basename; use File::Slurp qw(read_file write_file); -use Term::CallEditor qw(solicit); use CIL; use CIL::Issue; use CIL::Comment; @@ -227,7 +226,8 @@ sub cmd_add { my ($cil, undef, $issue_name) = @_; # read in the new issue text - my $fh = solicit( $new_issue_text ); + CIL::Utils->ensure_interactive(); + my $fh = CIL::Utils->solicit( $new_issue_text ); my $issue = CIL::Issue->new_from_fh( 'tmp', $fh ); @@ -250,7 +250,8 @@ sub cmd_edit { my $edit_issue_text = $issue->as_output; # read in the new issue text - my $fh = solicit( join('', @$edit_issue_text) ); + CIL::Utils->ensure_interactive(); + my $fh = CIL::Utils->solicit( join('', @$edit_issue_text) ); my $issue_edited = CIL::Issue->new_from_fh( $issue->name, $fh ); unless ( defined $issue_edited ) { @@ -270,7 +271,8 @@ sub cmd_comment { } # read in the new issue text - my $fh = solicit( $add_comment_text ); + CIL::Utils->ensure_interactive(); + my $fh = CIL::Utils->solicit( $add_comment_text ); my $comment = CIL::Comment->new_from_fh( 'tmp', $fh ); unless ( defined $comment ) { @@ -314,7 +316,8 @@ File goes here ... this will be overwritten. EOF # read in the new issue text - my $fh = solicit( $add_attachment_text ); + CIL::Utils->ensure_interactive(); + my $fh = CIL::Utils->solicit( $add_attachment_text ); my $attachment = CIL::Attachment->new_from_fh( 'tmp', $fh ); unless ( defined $attachment ) { diff --git a/lib/CIL/Utils.pm b/lib/CIL/Utils.pm index b9cd689..a0e165c 100644 --- a/lib/CIL/Utils.pm +++ b/lib/CIL/Utils.pm @@ -23,7 +23,18 @@ package CIL::Utils; use strict; use warnings; +use Carp; use File::Slurp; +use File::Temp qw(tempfile); +use POSIX qw(getpgrp tcgetpgrp); +use Fcntl qw(:DEFAULT :flock); + +## ---------------------------------------------------------------------------- +# setup some globals + +my $editor = $ENV{EDITOR} || 'vi'; + +## ---------------------------------------------------------------------------- sub parse_cil_file { my ($class, $filename, $last_field) = @_; @@ -111,6 +122,63 @@ sub write_cil_file { write_file($filename, $lines); } +# this method based on Term::CallEditor(v0.11)'s solicit method +# original: Copyright 2004 by Jeremy Mates +# copied under the terms of the GPL +sub solicit { + my ($class, $message) = @_; + + # when calling this, assume we're already interactive + + File::Temp->safe_level(File::Temp::HIGH); + my ( $fh, $filename ) = tempfile( UNLINK => 1 ); + + # since File::Temp returns both, check both + unless ( $fh and $filename ) { + croak "couldn't create temporary file"; + } + + select( ( select($fh), $|++ )[0] ); + print $fh $message; + + # need to unlock for external editor + flock $fh, LOCK_UN; + + # run the editor + my $status = system($editor, $filename); + + # check its return value + if ( $status != 0 ) { + croak $status != -1 + ? "external editor ($editor) failed: $?" + : "could not launch ($editor) program: $!"; + } + + unless ( seek $fh, 0, 0 ) { + croak "could not seek on temp file: errno=$!"; + } + + return $fh; +} + +# this method based on Recipe 15.2 +sub ensure_interactive { + my $tty; + open($tty, "/dev/tty") + or croak "program not running interactively (can't open /dev/tty): $!"; + + my $tpgrp = tcgetpgrp( fileno($tty) ); + my $pgrp = getpgrp(); + close $tty; + + unless ( $tpgrp == $pgrp ) { + croak "can't get exclusive control of tty: tpgrp=$tpgrp, pgrp=$pgrp"; + } + + # if we are here, then we have ensured what we wanted + return; +} + ## ---------------------------------------------------------------------------- 1; ## ---------------------------------------------------------------------------- -- 2.39.5