Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

srirupa deadwyler http://31d1.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

wierd little online text editor

careful who gets access to this script. as far as i can tell this can only modify files in its own directory (although i didn't explicitly make it that way). im new at perl so careful anyway. questions or comments? (dj_bidi@fastmail.fm)

   1  
   2  #!/usr/bin/perl
   3  
   4  ##         SLEd (Sort Of Like Ed) Editor
   5  ## USE:
   6  ##  append commands to url after a question mark 
   7  ## COMMANDS:
   8  ##  open (temp) file:                      -f,[file_to_edit]
   9  ##  close (temp)file:                      -d 
  10  ##  delete real file (careful!):           -dd
  11  ##  insert line (0 inserts at beginning):  -[line_number]a,[new_text] 
  12  ##  replace line:                          -[line_number],[new_text]
  13  ##  regex replace in line (like s///g):    -[line_number],/[old]/[new]/
  14  ##  add line:                              [line_to_add]
  15  ##  delete line:                           -[line_number]
  16  ##  save over real file:                   . 
  17  ##  save as new file:                      .[newfile_name]
  18  ## QUIRKS:
  19  ##  use ":lb:" to pass a #
  20  
  21  $in = $ENV{'QUERY_STRING'};
  22  $in =~ s/%3C/</g;
  23  $in =~ s/%3E/>/g;
  24  $in =~ s/:lb:/#/g;
  25  $in =~ s/%20/ /g;
  26  $in =~ s/%22/"/g;
  27  
  28  $url = $ENV{'SCRIPT_URI'};
  29  ($name = $url) =~ s/.+\///;
  30  $file = "~SLEd.pl";
  31  
  32  if ($in =~ /-f,(.*)/) {
  33   if ($file ne "") {
  34    unlink "$file";
  35   }
  36   $new = $1;
  37   $new =~ s/;//g;
  38   $new =~ s/"//g;
  39   open (IN, "$new");
  40   if (length($new) != 0) { 
  41    open (OUT, ">~$new");
  42   }
  43   while ($_ = <IN>) {
  44    print OUT "$_";
  45   }
  46   close IN;
  47   close OUT;
  48   rename "$name", "~bak";
  49   open (IN, "~bak");
  50   open (OUT, ">$name");
  51   while ($_ = <IN>) {
  52    if (length($new) == 0) {
  53     $_ =~ s/\$file = \".*\"/\$file = \"\"/g;
  54    } else {
  55     $_ =~ s/\$file = \".*\"/\$file = \"~$new\"/g;
  56    } 
  57    print OUT "$_";
  58   }
  59   close IN;
  60   close OUT;
  61   unlink "~bak";
  62   print "location: $url\n\n";
  63   die;
  64  }
  65  
  66  if ($in  eq ".") {
  67   ($f = $file) =~ s/^~//;
  68   unlink "$f";
  69   rename("$file", "$f");
  70   print "location: $url?-f,\n\n";
  71   die;
  72  } 
  73  
  74  if ($in eq "-dd") {
  75   ($f = $file) =~ s/^~//;
  76   unlink "$f";
  77   unlink "$file";
  78   print "location: $url?-f,\n\n";
  79   die;
  80  } 
  81  
  82  if ($in =~ /^\.(.+)/) {
  83   $new = $1;
  84   $new =~ s/;//g;
  85   $new =~ s/"//g;
  86   ($f = $file) =~ s/^~//;
  87   rename("$file", "$new");
  88   print "location: $url?-f,\n\n";
  89   die;
  90  } 
  91  
  92  if ($in eq "-d") {
  93   unlink "$file";
  94   print "location: $url?-f,\n\n";
  95   die;
  96  } 
  97  
  98  if ($in =~ /^-([0-9]+)a,(.+)/) {
  99    $i = 1;
 100    if ($1 == 0) {
 101     push(@print, "$2\n");
 102    }
 103    open (F, "$file");
 104    while (<F>) {
 105     if ($i != $1) {
 106      push(@print, $_);
 107     } else {
 108      push(@print, $_);
 109      push(@print, "$2\n");
 110     }
 111     $i++;
 112    }
 113    close F; 
 114    open (F, ">$file");
 115    foreach $line(@print) {
 116     print F "$line";
 117    }
 118    close F;
 119    print "location: $url\n\n";
 120  
 121  } elsif ($in =~ /^-([0-9]+),(.+)/) {
 122    $lino = $1;
 123    $replace = $2;
 124    $i = 1;
 125    open (F, "$file");
 126    while (<F>) {
 127     if ($i != $lino) {
 128      push(@print, $_);
 129     } elsif ($replace =~ m/^\/(.+?)\/(.*)\/$/) {
 130      $ol = $1;
 131      $newe = $2;
 132      ($match = $_) =~ s/$ol/$newe/g;
 133      push(@print, $match);
 134     } else {
 135      push(@print, "$replace\n");
 136     }
 137     $i++;
 138    }
 139    close F;  
 140    open (F, ">$file");
 141    foreach $line(@print) {
 142     print F "$line";
 143    }
 144    close F;
 145    print "location: $url\n\n";
 146  
 147  } elsif ($in =~ /^-([0-9]+)$/) {
 148   $i = 1;
 149   open (F, "$file");
 150   while (<F>) {
 151    if ($i != $1) {
 152     push(@print, $_);
 153    }
 154    $i++;
 155   }
 156   close F;
 157   open (F, ">$file");
 158   foreach $line(@print) {
 159    print F "$line";
 160   }
 161   close F;
 162   print "location: $url\n\n";
 163  
 164  } elsif ($in) {
 165   open (F, ">>$file");
 166   print F "$in\n";
 167   close F;
 168   print "location: $url\n\n";
 169  
 170  } else {
 171   print "Content-type: text/plain\n\n";
 172   open (F, "$file");
 173   $i = 1;
 174   while (<F>) {
 175    print "[$i] $_";
 176    $i++;
 177   }
 178  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS