index.cgi 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/perl
  2. # Run Sidef code inside the browser
  3. use utf8;
  4. use 5.018;
  5. use strict;
  6. #use autodie;
  7. use CGI qw(:standard -utf8);
  8. #use CGI::Carp qw(fatalsToBrowser);
  9. use Capture::Tiny qw(capture);
  10. use HTML::Entities qw(encode_entities);
  11. # Path where Sidef exists (when not installed)
  12. #use lib qw(/home/user/Sidef/lib);
  13. # Limit the size of Sidef scripts to 500KB
  14. $CGI::POST_MAX = 1024 * 500;
  15. use Sidef;
  16. binmode(STDOUT, ':utf8');
  17. print header(
  18. -charset => 'UTF-8',
  19. 'Referrer-Policy' => 'no-referrer',
  20. 'X-Frame-Options' => 'DENY',
  21. 'X-Xss-Protection' => '1; mode=block',
  22. 'X-Content-Type-Options' => 'nosniff',
  23. ),
  24. start_html(
  25. -lang => 'en',
  26. -title => 'Sidef Programming Language',
  27. -base => 'true',
  28. -meta => {
  29. 'keywords' => 'sidef programming language web interface',
  30. 'viewport' => 'width=device-width, initial-scale=1.0',
  31. },
  32. -style => [{-src => 'css/main.css'}],
  33. -script => [
  34. {
  35. -src => 'js/jquery-3.6.0.min.js',
  36. },
  37. {
  38. -src => 'js/tabby.js',
  39. },
  40. {
  41. -src => 'js/main.js',
  42. },
  43. ],
  44. );
  45. print h1("Sidef");
  46. print start_form(
  47. -method => 'POST',
  48. -action => 'index.cgi',
  49. 'accept-charset' => "UTF-8",
  50. ),
  51. textarea(
  52. -name => 'code',
  53. -default => 'Write your code here...',
  54. -rows => 10,
  55. -columns => 80,
  56. -onfocus => 'clearContents(this);',
  57. ),
  58. br, submit(-name => "Run!"), end_form;
  59. sub compile {
  60. my ($sidef, $code) = @_;
  61. my $errors = '';
  62. local $SIG{__WARN__} = sub {
  63. $errors .= join("\n", @_);
  64. };
  65. local $SIG{__DIE__} = sub {
  66. $errors .= join("\n", @_);
  67. };
  68. my $ccode = eval { $sidef->compile_code($code, 'Perl') };
  69. return ($ccode, $errors);
  70. }
  71. sub execute {
  72. my ($sidef, $ccode) = @_;
  73. my $errors = '';
  74. local $SIG{__WARN__} = sub {
  75. $errors .= join("\n", @_);
  76. };
  77. local $SIG{__DIE__} = sub {
  78. $errors .= join("\n", @_);
  79. };
  80. my ($stdout, $stderr) = capture {
  81. alarm 5;
  82. $sidef->execute_perl($ccode);
  83. alarm(0);
  84. };
  85. return ($stdout, $errors . $stderr);
  86. }
  87. if (param) {
  88. if (defined(my $code = param('code'))) {
  89. # Replace any newline characters with "\n"
  90. $code =~ s/\R/\n/g;
  91. my $sidef = Sidef->new(name => '-');
  92. my ($ccode, $errors) = compile($sidef, $code);
  93. if ($errors ne '') {
  94. chomp($errors);
  95. print pre(encode_entities($errors));
  96. print hr;
  97. $errors = '';
  98. }
  99. if (defined($ccode)) {
  100. my ($output, $errors) = execute($sidef, $ccode);
  101. if ($errors ne "") {
  102. chomp($errors);
  103. print pre(encode_entities($errors));
  104. print hr;
  105. }
  106. if (defined $output and $output ne '') {
  107. print pre(encode_entities($output));
  108. }
  109. }
  110. }
  111. }
  112. print end_html;