[CM] Snd: getting a prompt on stdout

Daniel Lopez daniel.lopez999 at gmail.com
Thu Mar 17 17:35:18 PDT 2016


On 17/03/16 11:47, bil at ccrma.Stanford.EDU wrote:
> That output goes through string_to_stdout in
> snd-xen.c, I think (it's watched for via
> g_io_channel_unix_get_fd in snd-gmain.c).
> Snd used to have a print-hook for stuff like
> this, but it didn't get used for years, so
> I removed it.  I could add a hook to
> string_to_stdout to add a prompt, if you like.
> I think you could change string_to_stdout
> to
>
>   fprintf(stdout, "%s\n> ", msg);
>
> or something like that to get a prompt,
> but I haven't tried it.
>
>
>

Thanks Bill - I got the desired effect by editing snd_eval_stdin_str:

void snd_eval_stdin_str(const char *buf)
{
   /* we may get incomplete expressions here */
   /*   (Ilisp always sends a complete expression, but it may be broken 
into two or more pieces from read's point of view) */

   char *str = NULL;
   if (mus_strlen(buf) == 0) return;

   str = stdin_check_for_full_expression(buf);
   if (str)
     {
       Xen result;
       int loc;

       redirect_everything_to(string_to_stdout, NULL);
       result = snd_catch_any(eval_str_wrapper, (void *)str, str);
       redirect_everything_to(NULL, NULL);

       loc = snd_protect(result);
       stdin_free_str();

       str = gl_print(result);
       string_to_stdout(str, NULL);

       /***** DANIEL: Next two lines added *****/
       fprintf(stdout, ">>> ");
       fflush(stdout);

       if (str) free(str);
       snd_unprotect_at(loc);
     }
}

Putting it here makes the new prompt appear only once, after the result 
and after any errors/warnings/snd-print statements that got printed out 
via the redirect_everything_to/snd_catch_any calls.

The fflush() was necessary otherwise the new prompt it doesn't appear 
until after the next console read (that confused me for a while, I can 
tell you).

Also, to make the prompt appear once at program start, I added the same 
two lines (fprintf and fflush) to g_xen_initialize(), right after where 
it calls g_init_listener(). I suppose a more uniform solution would be 
to make a new 'g_init_stdin()' function consisting of the new lines and 
call that instead.

I was going to make a patch file of these changes to proudly present to 
you but there are some other things to round out which I don't quite 
have the tenacity to fully do right now - when starting up with 
-nostdin, my hacked-in prompt spills onto stdout twice (once from 
g_xen_initialize() and once from snd_eval_stdin_str() being called once 
for some reason). And, it would also make sense if there was a built-in 
'stdin-prompt' variable, similar to 'listener-prompt', for customizing 
this. It would default to an empty string, not to disrupt the existing 
default behaviour. I attempted this but my naive copy-pasting didn't 
quite take me 100% of the way there, and to be honest my Scheme aptitude 
is still a bit weak for this kind of thing. I might have another go at a 
later date, unless you reckon these are jolly good ideas and get in 
there first!

Daniel



More information about the Cmdist mailing list