Objective c terminal application - objective-c

Is there such thing? I need to make an application in Xcode to basically do what the terminal app does. With just an nstextfield as the input, a label for the terminal output, and a send button. All this needs to be done without terminal accually being open.
Is this possible? If so, can someone post a website or sample code?

It's certainly possible. The Terminal basically just runs a shell (bash by default). You could just launch an app that forwards entered text onto bash, and let bash do the work. Or you could interpret the input yourself. Bash is pretty simple, for the most part: you type in a program and arguments, it finds the program in the $PATH, and launches it with the given arguments. (Of course, bash gets a bit fancier, which pipes, input/output redirection, scripting, background tasks, etc., but if you don't need that in your application, you could ignore those features.) You can use NSTask, system(3), or the exec family of tasks to launch processes (probably NSTask is your best bet).

Related

Perl 6: automated testing of terminal-based programs

How could I automate interactions with command line programs that expose a text terminal interface with Perl 6 for testing purposes?
If you want to use Perl 6 to automate execution or testing of console applications, I think you're going to use NativeCall to interact with the expect library. Once expect is installed, man libexpect will show its API documentation, though the way of accessing the documentation (such as the manpage name) may differ per package distribution.
Expect has APIs to launch a program, wait for text to appear on the (emulated) console (to "expect" text), and send text to the console (to emulate typing). The most common use case is to automate programs which require password input. Expect is often scripted--it is an interpreter--but there's no reason not to use it from a higher level programming language.
Edit: I somewhat answered the wrong question. The OP is interested in testing Perl 6 modules with Perl 6. That said, using expect to launch a second Perl 6 interpreter which uses the module is still the strongest, most strict way to test the application. You don't need to know what type of terminal library the module uses, because expect should be compatible with nearly all of them. You can send text to the STDIN pipe of a subprocess, but that's not as strong as the subprocess (console) communication you can get from expect. I don't know if there's a way to hijack whichever terminal library the module uses and communicate with it directly.
If it's just a plain interface, you could just run the program and collect output. The currently-experimental Testo module has is-run routine. You could use that directly, or if experimental status is bothersome, copy the guts of it into your own helper routine.
Take a look at Sparrow6 Task Check Language - Perl6 based DSL to verify text output. I've done a lot terminal apps testing using it.

NSTask vs System - pros and cons?

I'm at a point in a project where I need to call system commands. I originally started looking at NSTask (as that seems to be the most popular approach) but recently i just came across the system command. It looks like a far easier setup that NSTask. I've seen some questions/answers that say NSTask is the better approach, but I don't see
What are the advantages/disadvantages between the two
In what cases would one more likely to be used than the other
Any help/links/thoughts/ideas? (and yes.. i did a google search)
NSTask:
Can run his task in the background. Allows you to send interrupts and kills to the underlying process, and allows you to suspend or resume the underlying process without setting up threads yourself. Can also run synchronously if that's what you want.
Let's you work back and forth with Cocoa classes, like NSStrings without having to do a buncha conversions.
Let's you set I/O streams for the underlying process that differ from the caller's.
Better supported across all Apple platforms (like iOS) than system(3) -- I don't think system even works on iOS.
Requires Cocoa and Objective-C.
Doesn't interpret shell arguments or do path expansions of arguments.
system(3):
Better supported across all Unix-like platforms.
Can run a task with a one-liner.
Only requires C.
Runs in a shell and will interpret working directory and arguments like /bin/sh would.
For a Cocoa app I always use NSTask; I only use system if I'm doing something that must be C-only or I know will have to run under non-Mac environments. As it is, system is pretty brittle and the more robust solution is doing a fork-exec, because it allows you more control over streams and concurrent operation.
There are some differences. For some of them it is probably har to say in general, whether it is an advantage or not.
system() starts a shell. NSTask don't.
system() blocks. NSTask run asynchronously.
system() only takes args. NSTask works with pipes.
system() has only an integer exit code. NSTask works with pipes. (Yes, mentioned again. This is for output.)
system() takes a complete command line. To NSTask args can be passed in an array.
system() runs on the current directory. To NSTask you can pass a working directory.
This are some differences out of my mind without rechecking the documentation. It is an overview.

See when an executable opens and closes

How can I get a callback within my application when an executable (such as pbs, cp, etc) launches and then exits? This would need to work only knowing the path to the executable.
You could move the original executable aside, and replace it with a wrapper that runs the original, reporting when it runs and exits.
You could look at the accton and lastcomm commands, which record the start and exit of every process on the system.
You could look into using dtrace, which can definitely do what you're asking but it's rather complicated to use. You'd probably have to do a fair amount of learning to do this. I don't know much about writing dtrace scripts, but I'd probably start with execsnoop as my model.

Auto completion by typing TAB in TCL

How can I achieve auto-completion of key words and directories by typing TAB (or something else) in TCL shell , tclsh.
You need to find a copy of tclreadline, or switch to using tkcon (a graphical terminal written in Tcl that acts as a console) if using a GUI app is acceptable at all. FWIW, I can definitely recommend tkcon; the only reason I don't use it that much is because I'm often working with a custom build of Tcl that's hooked up to other things.

Putting a 'terminal' in my application?

Are there any frameworks for say, putting a display like in Terminal.app in MY app, and then displaying text on it like usual output to STDOUT? Complete with scrollback and etc.?
You may want to look at iTerm, an open-source terminal emulator written in Cocoa. If you really want terminal emulation, you might be able to lift from that framework.
This thread has a couple suggestions. The first is very UNIX'y - you use pipe() to map stdout to a new location. Then you'd need another process or a thread that reads that pipe and displays it into an NSTextView. The other approach that I liked as it seems cleaner and less resource intensive is to replace the File_writer_t _write proc in the stdout() FILE pointer with the hook that you want, which write the output into an NSTextView.