]> git.mjollnir.org Git - cil.git/commitdiff
Remove dependency on Term::CallEditor.
authorAndrew Chilton <andychilton@gmail.com>
Mon, 23 Jun 2008 07:01:53 +0000 (19:01 +1200)
committerAndrew Chilton <andychilton@gmail.com>
Mon, 23 Jun 2008 07:01:53 +0000 (19:01 +1200)
bin/cil
lib/CIL/Utils.pm

diff --git a/bin/cil b/bin/cil
index 66841796b27ebcc5376529bca3d9497844dbc1a0..63d9d26617022495b21e2e0607f76adef5e2b959 100755 (executable)
--- 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 ) {
index b9cd689f1395f61dc10932f834a1481a47364708..a0e165ccf598311c341fdbdd71a903370e34105d 100644 (file)
@@ -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;
 ## ----------------------------------------------------------------------------