chiark / gitweb /
New option: movres.ignore_transience.
[e16] / eesh / main.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2007 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "E.h"
25
26 /* Global vars */
27 Display            *disp;
28
29 static char         buf[10240];
30 static int          stdin_state;
31 static char        *display_name;
32 static Client      *e;
33 static Window       my_win, comms_win;
34
35 static void
36 process_line(char *line)
37 {
38    if (line == NULL)
39       exit(0);
40    if (*line == '\0')
41       return;
42
43    CommsSend(e, line);
44    XSync(disp, False);
45 }
46
47 static void
48 stdin_state_setup(void)
49 {
50    stdin_state = fcntl(0, F_GETFL, 0);
51    fcntl(0, F_SETFL, O_NONBLOCK);
52 }
53
54 static void
55 stdin_state_restore(void)
56 {
57    fcntl(0, F_SETFL, stdin_state);
58 }
59
60 static void
61 stdin_read(void)
62 {
63    static int          j = 0;
64    int                 k, ret;
65
66    k = 0;
67    while ((ret = read(0, &(buf[j]), 1) > 0))
68      {
69         k = 1;
70         if (buf[j] == '\n')
71           {
72              buf[j] = 0;
73              if (strlen(buf) > 0)
74                 process_line(buf);
75              j = -1;
76           }
77         j++;
78      }
79    if ((ret < 0) || ((k == 0) && (ret == 0)))
80       exit(0);
81 }
82
83 int
84 main(int argc, char **argv)
85 {
86    XEvent              ev;
87    Client             *me;
88    int                 i;
89    fd_set              fd;
90    char               *command, *s;
91    char                mode;
92    int                 len, l;
93
94    mode = 0;
95    display_name = NULL;
96    command = NULL;
97
98    for (i = 1; i < argc; i++)
99      {
100         s = argv[i];
101         if (*s != '-')
102            break;
103
104         if (!strcmp(argv[i], "-e"))
105           {
106              mode = -1;
107              if (i != (argc - 1))
108                {
109                   command = argv[++i];
110                }
111           }
112         else if (!strcmp(argv[i], "-ewait"))
113           {
114              mode = 1;
115              if (i != (argc - 1))
116                 command = argv[++i];
117           }
118         else if (!strcmp(argv[i], "-display"))
119           {
120              if (i != (argc - 1))
121                {
122                   display_name = argv[++i];
123                   display_name = Estrdup(display_name);
124                }
125           }
126         else if ((!strcmp(argv[i], "-h")) ||
127                  (!strcmp(argv[i], "-help")) || (!strcmp(argv[i], "--help")))
128           {
129              printf
130                 ("eesh sends commands to E\n\n"
131                  "Examples:\n"
132                  "  eesh Command to Send to E then wait for a reply then exit\n"
133                  "  eesh -ewait \"Command to Send to E then wait for a reply then exit\"\n"
134                  "  eesh -e \"Command to Send to Enlightenment then exit\"\n\n");
135              printf("Use eesh by itself to enter the \"interactive mode\"\n"
136                     "  Ctrl-D will exit interactive mode\n"
137                     "  Use \"help\" from inside interactive mode for further assistance\n");
138              exit(0);
139           }
140      }
141
142    /* Open a connection to the diplay nominated by the DISPLAY variable */
143    /* Or set with the -display option */
144    disp = XOpenDisplay(display_name);
145    if (!disp)
146      {
147         Alert("Failed to connect to X server\n");
148         exit(1);
149      }
150
151    CommsInit();
152    comms_win = CommsFindCommsWindow();
153    my_win = CommsSetup(comms_win);
154
155    e = ClientCreate(comms_win);
156    me = ClientCreate(my_win);
157
158    CommsSend(e, "set clientname eesh");
159    CommsSend(e, "set version 0.2");
160 #if 0                           /* Speed it up */
161    CommsSend(e, "set author The Rasterman");
162    CommsSend(e, "set email raster@rasterman.com");
163    CommsSend(e, "set web http://www.enlightenment.org");
164 /* CommsSend(e, "set address NONE"); */
165    CommsSend(e, "set info Enlightenment IPC Shell - talk to E direct");
166 /* CommsSend(e, "set pixmap 0"); */
167 #endif
168
169    if (command == NULL && i < argc)
170      {
171         mode = 1;
172         len = 0;
173         for (; i < argc; i++)
174           {
175              l = strlen(argv[i]);
176              command = EREALLOC(char, command, len + l + 2);
177
178              if (len)
179                 command[len++] = ' ';
180              strcpy(command + len, argv[i]);
181              len += l;
182           }
183      }
184
185    if (command)
186      {
187         /* Non-interactive */
188         CommsSend(e, command);
189         XSync(disp, False);
190 #if 0                           /* No - Wait for ack */
191         if (mode <= 0)
192            goto done;
193 #endif
194      }
195    else
196      {
197         /* Interactive */
198         stdin_state_setup();
199         atexit(stdin_state_restore);
200      }
201
202    for (;;)
203      {
204         XSync(disp, False);
205         while (XPending(disp))
206           {
207              XNextEvent(disp, &ev);
208              switch (ev.type)
209                {
210                case ClientMessage:
211                   s = CommsGet(me, &ev);
212                   if (!s)
213                      break;
214                   if (*s)
215                      printf("%s", s);
216                   fflush(stdout);
217                   Efree(s);
218                   if (mode)
219                      goto done;
220                   break;
221                case DestroyNotify:
222                   goto done;
223                }
224           }
225
226         FD_ZERO(&fd);
227         if (!command)
228            FD_SET(0, &fd);
229         FD_SET(ConnectionNumber(disp), &fd);
230
231         if (select(ConnectionNumber(disp) + 1, &fd, NULL, NULL, NULL) < 0)
232            break;
233
234         if (FD_ISSET(0, &fd))
235           {
236              stdin_read();
237           }
238      }
239
240  done:
241    ClientDestroy(e);
242    ClientDestroy(me);
243
244    return 0;
245 }
246
247 void
248 Alert(const char *fmt, ...)
249 {
250    va_list             ap;
251
252    va_start(ap, fmt);
253    vfprintf(stderr, fmt, ap);
254    va_end(ap);
255 }
256
257 #if !USE_LIBC_STRDUP
258 char               *
259 Estrdup(const char *s)
260 {
261    char               *ss;
262    int                 sz;
263
264    if (!s)
265       return NULL;
266    sz = strlen(s);
267    ss = EMALLOC(char, sz + 1);
268    strncpy(ss, s, sz + 1);
269
270    return ss;
271 }
272 #endif