Skip to content

Instantly share code, notes, and snippets.

@itfrombit
Created February 16, 2013 22:47
Show Gist options
  • Save itfrombit/4969075 to your computer and use it in GitHub Desktop.
Save itfrombit/4969075 to your computer and use it in GitHub Desktop.
Two quick and dirty ways to capture the output of a system call from Nu.
(set popen (NuBridgedFunction functionWithName:"popen" signature:"l**"))
(set pclose (NuBridgedFunction functionWithName:"pclose" signature:"il"))
(set fgetc (NuBridgedFunction functionWithName:"fgetc" signature:"il"))
(set mktemp (NuBridgedFunction functionWithName:"mktemp" signature:"**"))
(set unlink (NuBridgedFunction functionWithName:"unlink" signature:"i*"))
(function sys (command)
(set tempfile (mktemp "temp.XXXXXX"))
(set c (+ "(" command " 2>&1) > " tempfile))
(system c)
(set o (NSString stringWithContentsOfFile:tempfile))
(unlink tempfile)
o)
(function sysp (command *options)
(set task ((NSTask alloc) init))
(set targs (NSArray arrayWithList:*options))
(set p (NSPipe pipe))
(task setLaunchPath:command)
(task setArguments:targs)
(task setStandardOutput:p)
(set fp (p fileHandleForReading))
(task launch)
(set d (fp readDataToEndOfFile))
(set o ((NSString alloc) initWithData:d encoding:NSUTF8StringEncoding))
o)
(set o (sys "ls"))
(puts o)
(set o (sysp "/bin/ls" "-l" "-a"))
(puts o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment