mkexec.pl 2.8KB

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