]> Pileus Git - vpaste/blobdiff - index.cgi
limits, title
[vpaste] / index.cgi
index 0c5eeba32858e1bd7a46c4484729443ed8a09db8..1fdb8adfa75965b0d68acaccda419a07828bbfcd 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -30,19 +30,14 @@ function cut_file {
                st==2  {print \$0};
                /$1--/ {st=0};
                /^\\r$/ && st==1 {st=2};
-       " | head -c -2
+       " | head -c -2 | head -c $((128*1024))
        # Remove trailing ^M's that come with CGI
+       # Limit size to 128K
 }
 
 # Print out a generic header
-function start_html {
-       echo "Content-Type: text/html; charset=UTF-8"
-       echo
-}
-
-# Print out a generic header
-function start_text {
-       echo "Content-Type: text/plain; charset=UTF-8"
+function header {
+       echo "Content-Type: $1; charset=UTF-8"
        echo
 }
 
@@ -54,8 +49,8 @@ function do_print {
                input="db/$1"
                trim='1d' # sed command to remove cruft
        else
-               echo "Status: 404"
-               start_text
+               echo "Status: 404 Not Found"
+               header text/plain
                echo "File '$1' not found"
                return
        fi
@@ -65,26 +60,26 @@ function do_print {
                # Create a temp file with the provided modeline
                output="$(mktemp)"
                tmp="$(mktemp)"
-               cat  "$input" >> "$tmp"
-               get_modeline  >> "$tmp"
+               sed "1a$(get_modeline)" "$input" > "$tmp"
 
                # - I have some plugins in ~/.vim
                # - Run ex in screen to trick it into thinking that it
                #   has a real terminal, not that we also have to set
                #   term=xterm-256color in vimrc
                HOME=/home/andy \
-               screen -D -m ex -u vimrc \
-                       '+$d|'$trim     \
+               screen -D -m ex -Z -u vimrc \
+                       '+2d|'$trim     \
+                       '+%s/\r//g'     \
                        '+TOhtml'       \
                        "+sav! $output" \
                        '+qall!'        \
                        "$tmp"
 
-               start_html
+               header text/html
                cat "$output" 
                rm "$tmp" "$output"
        else
-               start_text
+               header text/plain
                sed "$trim" "$input"
        fi
 }
@@ -93,10 +88,12 @@ function do_print {
 # Upload handler
 function do_upload {
        output="$(mktemp db/XXXXX)"
-       uri="$SCRIPT_URI$(basename "$output")"
+       uri="$url$(basename "$output")${QUERY_STRING:+"?"}"
        (get_modeline; cut_file "$1") > "$output"
-       start_text
-       echo "$uri${QUERY_STRING:+"?"}"
+       echo "Status: 302 Found"
+       echo "Location: $uri"
+       header text/plain
+       echo "$uri"
 }
 
 # Default index page
@@ -105,12 +102,13 @@ filetypes=$(
        ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ |
        sed -n 's/.vim$//p' | sort | uniq
 )
-uploads=$(ls -t db | head -n 5 | sed "s!^!$SCRIPT_URI!")
+uploads=$(ls -t db | head -n 5 | sed "s!^!$url!")
 
-start_html
+header text/html
 cat - <<EOF
 <html>
        <head>
+               <title>vpaste.net - Vim based pastebin</title>
                <style>
                * { margin:0; padding:0; }
                body { margin:1em; }
@@ -121,14 +119,32 @@ cat - <<EOF
                </style>
        </head>
        <body>
+               <form id="form" method="post" action="?" enctype="multipart/form-data">
+               <textarea name="text" style="width:100%; height:20em;"></textarea>
+               <br>
+               <select onchange="document.getElementById('form').action =
+                       document.location + '?ft=' + this.value;">
+               <option value="" selected="selected" disabled="disabled">Filetype</option>
+               <option value="">None</option>
+               $(for ft in $filetypes; do
+                       echo "<option>$ft</option>"
+               done)
+               </select>
+               <input type="submit" value="Paste">
+               </form>
+               <br>
+
                <h4>NAME</h4>
-               <p>vpaste: Vim enabled pastebin</p>
+               <p>vpaste: Vim based pastebin</p>
 
                <h4>SYNOPSIS</h4>
                <pre> vpaste file [option=value,..]</pre>
                <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
                <br>
-               <pre> &lt;command&gt; | curl -F 'x=<-' $SCRIPT_URI[?option=value,..]</pre>
+               <pre> &lt;command&gt; | curl -F 'x=<-' $url[?option=value,..]</pre>
+               <br>
+               <pre>:map vp :exec "w !vpaste ft=".&ft&lt;CR&gt;</pre>
+               <pre>:vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&ft&lt;CR&gt;</pre>
 
                <h4>DESCRIPTION</h4>
                <p>Add <b>?[option[=value],..]</b> to make your text a rainbow.</p>
@@ -137,7 +153,7 @@ cat - <<EOF
                <h4>OPTIONS</h4>
                <dl>
                <dt>ft, filetype={filetype}</dt>
-               <dd>A filetype to use for highlighting, see FILETYPES</dd>
+               <dd>A filetype to use for highlighting, see above menu for supported types</dd>
                <dt>bg, background={light|dark}</dt>
                <dd>Background color to use for the page</dd>
                <dt>et, expandtab</dt>
@@ -155,28 +171,27 @@ cat - <<EOF
                    <a href="vimrc?ft=vim">vimrc</a>
                    <a href="htaccess?ft=apache">htaccess</a>
                <li><a href="2html-et.patch?ft=diff">2html-et.patch</a>
+               <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a>
                </ul>
 
                <h4>LATEST UPLOADS</h4>
                <ul>$(for uri in ${uploads[@]}; do
                        echo "<li><a href='$uri'>$uri</a>"
                done)</ul>
-
-               <h4>FILETYPES</h4>
-               <blockquote>$filetypes</blockquote>
        </body>
 </html>
 EOF
 }
 
 # Main
-pathinfo="${SCRIPT_URL/*vpaste\/}"
-boundary="${CONTENT_TYPE/*boundary\=/}"
+url="http://$HTTP_HOST/$SCRIPT_URI"
+pathinfo="${REQUEST_URI/*\/}"
+pathinfo="${pathinfo/\?*}"
 
 if [ "$pathinfo" ]; then
        do_print "$pathinfo"
 elif [ "$CONTENT_TYPE" ]; then
-       do_upload "$boundary"
+       do_upload ${CONTENT_TYPE/*boundary\=/}
 else
        do_help
 fi