eesh: Add prompt if interactive
This commit is contained in:
parent
1bfc29c007
commit
d659c3036a
40
eesh/main.c
40
eesh/main.c
|
@ -22,6 +22,7 @@
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -30,13 +31,20 @@
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include "E.h"
|
#include "E.h"
|
||||||
|
|
||||||
|
#define PROMPT "eesh> "
|
||||||
|
|
||||||
Display *disp;
|
Display *disp;
|
||||||
|
|
||||||
static Client *e;
|
static Client *e;
|
||||||
|
static bool use_prompt;
|
||||||
|
static bool input_pending;
|
||||||
|
static bool reply_pending;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
process_line(char *line)
|
process_line(char *line)
|
||||||
{
|
{
|
||||||
|
input_pending = false;
|
||||||
|
|
||||||
if (!line)
|
if (!line)
|
||||||
exit(0);
|
exit(0);
|
||||||
if (*line == '\0')
|
if (*line == '\0')
|
||||||
|
@ -44,6 +52,7 @@ process_line(char *line)
|
||||||
|
|
||||||
CommsSend(e, line);
|
CommsSend(e, line);
|
||||||
XSync(disp, False);
|
XSync(disp, False);
|
||||||
|
reply_pending = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -54,6 +63,8 @@ stdin_read(void)
|
||||||
int nr;
|
int nr;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
|
input_pending = true;
|
||||||
|
|
||||||
nr = read(STDIN_FILENO, buf + nin, sizeof(buf) - 1 - nin);
|
nr = read(STDIN_FILENO, buf + nin, sizeof(buf) - 1 - nin);
|
||||||
if (nr <= 0)
|
if (nr <= 0)
|
||||||
exit(0);
|
exit(0);
|
||||||
|
@ -102,11 +113,9 @@ main(int argc, char **argv)
|
||||||
struct pollfd pfd[2];
|
struct pollfd pfd[2];
|
||||||
int nfd, timeout;
|
int nfd, timeout;
|
||||||
char *command, *s;
|
char *command, *s;
|
||||||
char mode;
|
|
||||||
int len, l;
|
int len, l;
|
||||||
const char *space;
|
const char *space;
|
||||||
|
bool interactive;
|
||||||
mode = 0;
|
|
||||||
|
|
||||||
for (i = 1; i < argc; i++)
|
for (i = 1; i < argc; i++)
|
||||||
{
|
{
|
||||||
|
@ -150,7 +159,6 @@ main(int argc, char **argv)
|
||||||
space = "";
|
space = "";
|
||||||
if (i < argc)
|
if (i < argc)
|
||||||
{
|
{
|
||||||
mode = 1;
|
|
||||||
len = 0;
|
len = 0;
|
||||||
for (; i < argc; i++)
|
for (; i < argc; i++)
|
||||||
{
|
{
|
||||||
|
@ -167,19 +175,22 @@ main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input_pending = false;
|
||||||
|
reply_pending = false;
|
||||||
|
|
||||||
if (command)
|
if (command)
|
||||||
{
|
{
|
||||||
/* Non-interactive */
|
/* Non-interactive */
|
||||||
|
interactive = use_prompt = false;
|
||||||
CommsSend(e, command);
|
CommsSend(e, command);
|
||||||
XSync(disp, False);
|
XSync(disp, False);
|
||||||
#if 0 /* No - Wait for ack */
|
reply_pending = true;
|
||||||
if (mode <= 0)
|
|
||||||
goto done;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Interactive */
|
/* Interactive */
|
||||||
|
interactive = true;
|
||||||
|
use_prompt = isatty(STDIN_FILENO);
|
||||||
stdin_state_setup();
|
stdin_state_setup();
|
||||||
atexit(stdin_state_restore);
|
atexit(stdin_state_restore);
|
||||||
}
|
}
|
||||||
|
@ -204,11 +215,14 @@ main(int argc, char **argv)
|
||||||
s = CommsGet(me, &ev);
|
s = CommsGet(me, &ev);
|
||||||
if (!s)
|
if (!s)
|
||||||
break;
|
break;
|
||||||
|
reply_pending = false;
|
||||||
if (*s)
|
if (*s)
|
||||||
|
{
|
||||||
printf("%s", s);
|
printf("%s", s);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
}
|
||||||
Efree(s);
|
Efree(s);
|
||||||
if (mode)
|
if (!interactive)
|
||||||
goto done;
|
goto done;
|
||||||
break;
|
break;
|
||||||
case DestroyNotify:
|
case DestroyNotify:
|
||||||
|
@ -216,6 +230,12 @@ main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (use_prompt && !input_pending && !reply_pending)
|
||||||
|
{
|
||||||
|
printf(PROMPT);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
if (poll(pfd, nfd, timeout) < 0)
|
if (poll(pfd, nfd, timeout) < 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue