mkexec 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/perl
  2. # make an executable file
  3. use strict;
  4. use warnings;
  5. sub usage; sub mkexec; sub guesstype; sub getcmd; sub launch_editor;
  6. my $DEFAULT_TYPE = 'sh';
  7. my $DEFAULT_MODE = 0755;
  8. my $DEFAULT_FORCE = 0;
  9. my $DEFAULT_EDIT = 0;
  10. ## .... ## . . . . . . . . . . .
  11. ## INIT ## -------------------------------------------------------------------
  12. ## '''' ## ' ' ' ' ' ' ' ' ' ' '
  13. my $force = $DEFAULT_FORCE;
  14. my $name; my $type; my $edit;
  15. foreach (@ARGV) {
  16. if (m/-f|--force/) {
  17. $force++;
  18. } elsif (m/-e|--edit/) {
  19. $edit++;
  20. } elsif (m/-E|--no-edit/) {
  21. $edit = 0;
  22. } elsif (defined $name) {
  23. $type = $_;
  24. } else {
  25. $name = $_;
  26. }
  27. }
  28. usage unless defined $name;
  29. $type = guesstype $name unless defined $type;
  30. my $bangs = {
  31. pl => `which perl`,
  32. sh => '/bin/sh',
  33. py => `which python`,
  34. bash => '/bin/bash',
  35. sed => `which sed`,
  36. bc => `which bc`
  37. };
  38. my $templates = {
  39. pl => "use strict;\nuse warnings;\n",
  40. py => "if __name__ == '__main__':\n"
  41. };
  42. my $editors = [ qw/ vim editor / ];
  43. my $cmds;
  44. $cmds->{vim}->{test} = "vim --version 2>/dev/null";
  45. $cmds->{vim}->{run} = "vim +\"normal G\$\" '%s'";
  46. $cmds->{editor}->{test} = "editor --version 2>/dev/null";
  47. $cmds->{editor}->{run} = "editor '%s'";
  48. ## ... ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  49. ## RUN ## ----------------------------------------------------------------- #
  50. ## ''' ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  51. if (exists $bangs->{$type}) {
  52. $force and -e $name
  53. and (defined `cp "$name" "$name~"` or die $!);
  54. (not -e $name or $force)
  55. and mkexec $name, mkbody($type);
  56. chmod $DEFAULT_MODE, $name;
  57. launch_editor $name if $edit;
  58. } else {
  59. die "unknown type: $type\n";
  60. }
  61. ## .... ## ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''-.
  62. ## SUBZ ## ----------------------------------------------------------------- :
  63. ## '''' ## ..............................................................-'
  64. sub usage {
  65. print STDERR "usage: $0 [-f|-E] filename [type]\n";
  66. exit 0;
  67. }
  68. sub guesstype {
  69. my $name = shift;
  70. my ($ext) = $name =~ m|\.(\w+)$|;
  71. return ( $ext ? $ext : $DEFAULT_TYPE);
  72. }
  73. sub mkbody {
  74. my $type = shift;
  75. my $tmpl = "";
  76. $tmpl .= $templates->{$type} if exists $templates->{$type};
  77. return sprintf "#!%s\n%s\n", $bangs->{$type}, $tmpl;
  78. }
  79. sub mkexec {
  80. my ($name, $body) = @_;
  81. open EXE, ">", $name or die "cannot open $name for writing: $!\n";
  82. -W EXE or die "file $name is not writable\n";
  83. print EXE $body;
  84. close EXE or die "cannot close $name: $!\n";
  85. }
  86. sub get_cmd {
  87. foreach (@$editors) {
  88. return $cmds->{$_}->{run} if `$cmds->{$_}->{test}`
  89. }
  90. warn "no supported editor available\n";
  91. return;
  92. }
  93. sub launch_editor {
  94. my $name = shift;
  95. my $form = get_cmd;
  96. if ($form) {
  97. my $command = sprintf get_cmd, $name;
  98. exec "$command";
  99. } else { return }
  100. warn "failed to launch editor: $form\n";
  101. }