On Wed, May 2, 2012 at 10:33 PM, Dan Shelton
<[email protected]> wrote:
> On 2 May 2012 22:18, David Korn <[email protected]> wrote:
>> Subject: Re: [ast-users] Using libshell for emacs line editing
>>> We have a small application which needs to prompt for a text line
>>> (with emacs command line editing) and currently uses libtecla for this
>>> purpose Can we use libshell for the same task? Is there a code example
>>> how to do this for libshell?
>>
>> If you are asking how to do this in ksh, You should be able to do
>>
>> set -o emacs
>> IFS= read var
>>
>> and you will be able to use emacs editing on the input line.
>> You can also continue lines by ending them with a \.
>
> Well, this is close but I want to do this from C code, i.e. have a C
> function int shell_readinputline(const char *prompt, const char
> *histfile, const char *defaultvalue, char **returnstring) which
> accepts a prompt string passed to PS1, a histfile used as history file
> for this application (use /dev/null if value is NULL), defaultvalue
> which is presented as option and return string returns the string the
> user entered.
I don't have time right now to flesh-out all details of your function
but this small demo should help you:
-- snip --
#include <shell.h>
#include <nval.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
int i;
Shell_t *shp;
Namval_t *np;
int exitval;
char *xargv[2];
char script[512]; /* WARNING: Risk of buffer overflow */
xargv[0] = "/usr/bin/sh";
xargv[1] = NULL;
shp = sh_init(2, xargv, 0);
if (!shp)
error(ERROR_exit(1), "shell initialisation failed.");
sprintf(script,
"HISTFILE=%s\n"
"set -o gmacs\n"
"read var?\"%s\" ; (( exitval=$? ))\n",
"/dev/null",
"($(printf '%T' now):input string here:)");
(void) sh_trap(script, 0);
np = nv_open("var", shp->var_tree, 0);
if (!np)
error(ERROR_exit(1), "variable %s not found.", "var");
printf("Returned string is |%s|\n", nv_getval(np));
nv_close(np);
np = nv_open("exitval", shp->var_tree, 0);
if (!np)
error(ERROR_exit(1), "variable %s not found.", "exitval");
exitval = (int)nv_getnum(np);
nv_close(np);
return (exitval);
}
-- snip --
The demo shows how to use libshell to execute a small script inline
which displays a prompt (with the current date+time from $ printf "%T"
now #), reads a line from stdin with gmacs editor mode enabled and
then stores the result in a shell variable. The following part shows
how to access these variables...
>> If you are asking how to use editing in an existing program then you can
>> LD_PRELOAD libedit assuming you have the ast-open package.
>
> Where is libedit.so? I can't find it in my ast-open build.
Uhm... right now libedit is embedded in libshell. AFAIK David Korn
once said he might split it out for other apps to use but until now
this wasn't done yet (well, I am waiting for the new AST bugtracker
coming online and then I'll file an RFE...).
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) [email protected]
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users