User:GreenC/awb/cygwin/demon-lin


 * 1) !/usr/local/bin/awk -f


 * 1) Demon to pass data back and forth between Windows host and Linux guest OS under VirtualBox
 * 2) See also demon-win.awk for the version running on the Windows host.
 * 3) Purposes is to run AWB on windows, and unix scripts on Linux.

@load "filefuncs" @load "readfile"

BEGIN {


 * 1) Begin configuration

rm = "/bin/rm"                    # Path to rm   sleep = "/bin/sleep"               # Path to sleep

script = "/home/script/script.bsh" # The unix script. It takes as argv[1] the title of a Wikipedia article, process it and # deposits the updated wikisource as "article.txt" in the directory defined in next line. dir = "/mnt/ramdisk/"             # Shared directory with Windows (include a trailing "/"). RAM disk recommended. fname = dir "name.txt"            # File containing name passed *from* AWB to unix script farticle = dir "article.txt"      # File containing article passed *to* AWB from unix script fabort = dir "abort.txt"          # File flagging abort


 * 1) End configuration

removefile(farticle)              # Clear out old files removefile(fabort)

print("demon-lin.awk: Waiting for " fname " ...")

while(1) { sleep(2) if( exists(fname) ) { name = strip( readfile(fname) ) print("demon-lin.awk: New job request: " name "\n") removefile(fname) removefile(farticle) if(length(name) > 0) { com = script " \"" name "\""                 # eg. /usr/home/script.bsh "Charles Dickens" print sys2var(com)                           # Run script and print any debug output close(com)                                   # Flush script's disk writes } else { abort("demon-lin.awk: error retrieving name") # Pass control back to AWB with no article.txt generated }     }  } }

function abort(msg, filen) { if( length(msg) > 0 ) print(msg) printf "0" > fabort close(fabort) }
 * 1) Create abort.txt
 * 1) Create abort.txt

function removefile(str) {
 * 1) Delete a file

if( exists(str) ) sys2var(rm " -- " str) if( exists(str) ) { print("demon-lin.awk: Unable to delete " str ", aborting.") exit }     system("") # Flush }

function exists(name   ,fd) { if ( stat(name, fd) == -1) return 0 else return 1 }
 * 1) Check for file existence. Return 1 if exists, 0 otherwise.
 * 2)  Requires GNU Awk 4.1:
 * 3)     @load "filefuncs"
 * 1)     @load "filefuncs"

function sys2var(command       ,fish, scale, ship) {
 * 1) Run a system command and store result in a variable
 * 2)   eg. googlepage = sys2var("wget -q -O- http://google.com")
 * 3) Supports pipes inside command string. Stderr is sent to null.
 * 4) If command fails (errno) return null
 * 1) If command fails (errno) return null

command = command " 2>/dev/null" while ( (command | getline fish) > 0 ) { if ( ++scale == 1 ) ship = fish else ship = ship "\n" fish }        close(command) return ship }

function sleep(seconds) { sys2var(sleep " " seconds) }
 * 1) Sleep
 * 1) Sleep

function strip(str){ gsub(/^space:+|space:+$/,"",str) return str }
 * 1) Strip leading/trailing whitespace
 * 1) Strip leading/trailing whitespace