mkexec.pl 2.7KB

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