Cocoa shell/libreadline like framework - does it exist?

Status
Not open for further replies.
Hello,

I'd be interested to hear if any of you know of a framework providing a console/terminal functionality that could be used as a (more or less) drop-in replacement for the Terminal.app or at least Console.app? I guess I'm not the only one who's ever wanted to turned a command line app using *printf to a GUI app with minimal rewriting of at least the output code. A bit like AquaTerm, but focussing on text.

Googling returns of course an overkill of hits concerning the use of Cocoa in command line tools; the most relevant hit I found to date is on adapting a component from F-Script ... with a link to more information that's gone stale.

I know this used to be possible with Tk (used it back in the 90s in an editor called Point), so if there's no off-the-shelf Cocoa or Carbon solution, I'd also appreciate some pointers to refresh my memory on this aspect of Tcl/Tk.
 
I reckon that boils down to startng them up from a minimal terminal app?
You won't get a terminal window like you might see on Windows, if that's what you mean, because Unix's console model is different.

NSTask provides methods for setting and retrieveing the standard i/o streams for the subprocess. So you create some pipes, make the task use them, launch the task, and then your program can read/write to the sub task's input output and error streams using the pipes. You can display the task's output in a text widget or whatever and you'll have the beginings of a basic terminal-like UI.
 
Ah, right. And that's indeed what I was hoping to find ready to be used. XCode has the thing built-in, it would be nice if it were usable like, indeed, one can on MSWin. The goal here is really to compile unixy shell apps as Cocoa apps quickly and without hassle. Although I could also argue that Apple has already provided the necessary things in Xcode and if I need to run said tools outside Xcode, I have Terminal.app and my trusty old xterms.

I hate how Xcode 3 always puts up a debugger window, though!

I've grabbed code for an output-only console from the GNUStep distribution - I reckon I could adapt that for directing output elsewhere than to a disk file to be opened in Console.app . Or maybe something like

Code:
  if( (logFp = freopen("theLogFile.log", "a", stdout)) ){
     system( "open theLogFile.log" ); // should open theLogFile.log in Console.app
     fputs( "Opened theLogFile.log from %s::%d\n", __FILE__, __LINE __ );
     unlink( "theLogFile.log" ); // poor-man's implementation of a pipe
  }

although I guess Console.app will detect the disappearance of the file immediately... Maybe it's time to get some very old code of mine dusted off and see if it's still possible to hand off a pair of file descriptors to a separate process attached to a terminal :)
 
I've been thinking this over a bit more. What I'd need is not so much a pseudo-terminal as a pseudo-tty: a framework that can read (and if possible, write) on POSIX file descriptors or pointers, and that redirects the output/input to a window.

In fact, this existed in MPW (via files on disk) and even nicer in AmigaOS where something like fopen("con:/width/height/X/Y/title", "rw") would open a bidirectional console (memories, memories!!)
 
I don't know that a pre-made framework exists yet.

Perhaps someone with more Cocoa experience can comment, but it seems like it wouldn't be hard to subclass NSTextStorage to do this, and then it's trivial to hook that to a Cocoa GUI.

So you'd have an NSTask running, hooked up to NSPipes where you can get file descriptors for the task's input and output, grand central dispatch to watch the task's output file descriptor for output to direct into the text storage, a subclass of NSTextStorage to manage all that as well as redirect user input into the task's input, and then an NSTextView hooked up as the view for your NSTextStorage subclass's model.

Or alternatively you can make your app bundle launch a helper app which opens Terminal and runs the the real application from inside there. Or you can skip the app bundle altogether and have a flat executable. I think OS X opens a terminal window to handle stdin and stdout when the Finder launches one of those. The issue with that is that you don't get to hide your application's resources (such as the UI description) inside a bundle that way.
 
I'm currently looking at the PCLogController from GNUStep's ProjectCenter. It uses an NSTextView. I could see a simple app that is called with a bidirectional pipe, reads from one end displaying in the NSTextView ... and possibly sending back through the pipe's other end the text it receives from the window. (Which would mean using something else but an NSTextVew?)

Here's the dusted-off code that uses a similar principle to open a "log window" under X11. It still works, but not with Terminal.app (either I can't pass arguments to 'xpipe', or the pipe descriptors I pass it don't work.

Code:
/* Xfopenw(): opens an xterm with geometry and title as specified,
 * executing the utility program 'xpipe' in it, which will read
 * from the pipe-file, and display what it reads. Xfopenw uses
 * the program 'system' to spawn the new command; this ensures (?)
 * complete independancy (system calls setsid(), and then calls
 * system() with its arguments).
 */
FILE *Xfopenw( char *geometry, char *windowname, long bufsize, FILE **pipefp, int pipefd[2], pid_t *pid)
{  FILE *fp= NULL;
   char command[1024];
   double timer1;
   Time_Struct t1;
   pid_t ourpid= getpid();

	/* first check if we can open an xterm. Assume that
	 * xterm -help will only work if xterm will work, and
	 * else complain.
	 */
	errno= 0;
	Elapsed_Since( &t1);
#if !defined(__APPLE_CC__) && !defined(__MACH__)
	if( system( "xterm -help >/dev/null 2>&1") || errno ){
		if( !errno)
			errno= ENOXTERM;
		*pid= 0L;
		*pipefp= NULL;
		XPipefp= NULL;
		XPipefd= NULL;
		return( NULL);
	}
#endif
	Elapsed_Since( &t1);
	timer1= t1.Tot_Time;

	XPipefd= pipefd;
	  /* create the necessary pipe:	*/
	pipe( pipefd);
	errno= 0;
	fp= fdopen( pipefd[1], "w");
	if( fp ){
	  int ret;
		*pipefp= fp;
		XPipefp= pipefp;
		  /* construct the command to be executed:	*/
		sprintf( command, "system </dev/null >/dev/null 2>&1 \"xterm -sb -xrm 'XTerm*waitForMap:False' ");
		if( geometry ){
			sprintf( command, "%s -geometry %s ", command, geometry);
		}
		if( windowname ){
			sprintf( command, "%s -T '%s' -e xpipe -bufsize %d -fildes %d,%d &\"",
				command, windowname, bufsize, pipefd[0], pipefd[1]
			);
		}
		else{
			sprintf( command, "%s -e xpipe -bufsize %d -fildes %d,%d &\"",
				command, bufsize, pipefd[0], pipefd[1]
			);
		}
		if( strlen(command)> sizeof(command)/sizeof(char) ){
			fprintf( stderr, "Xfopenw(): potentially fatal error: wrote %d bytes in command string\n",
				strlen(command)
			);
		}

		logwindow_pid= pid;
		*pid= 0;
		  /* execute it, and, if possible, read the xpipe process id
		   * as written by xpipe to pipefd[1]
		   */
		if( !(ret= system(command)) ){
			sigusr2_handler = signal( SIGUSR2, xpipe_exitted);
			fprintf( stderr, "cx::Xfopenw(): reading child PID");
			fflush( stderr);
			read( pipefd[0], pid, sizeof(pid_t) );
			kill( *pid, SIGCONT );
			CXSleep( 0.5 );
			kill( *pid, SIGCONT );
			CXSleep( 0.5 );
			fprintf( stderr, "=%d; sending our PID", *pid);
			fflush( stderr);
			  /* Send our pid multiple times: on my O2 under Irix 6.3, it gets it the 3rd or 4th time... */
			write( pipefd[1], &ourpid, sizeof(pid_t) );
			write( pipefd[1], &ourpid, sizeof(pid_t) );
			write( pipefd[1], &ourpid, sizeof(pid_t) );
			write( pipefd[1], &ourpid, sizeof(pid_t) );
			fputc( '\n', stderr);
			fflush( stderr);
			set_alarm_call( 1800, Check_PipeProcess );
			return( fp);
		}
		else{
			Xclosew();
			if( !errno ){
				errno = ENOXTERM;
			}
			return( NULL);
		}
	}
	else{
		fprintf( cx_stderr, "Xfopenw(%s,%s): can't attach filepointer to pipe (%s)\n", serror() );
	}
	return( NULL);
}
 
Status
Not open for further replies.