]> git.jsancho.org Git - gacela.git/blob - gacela.c
(no commit message)
[gacela.git] / gacela.c
1 #include <stdio.h>
2 #include <readline/readline.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5
6 /* Read-Send-Print-Loop */
7 void rspl(int pin, int pout)
8 {
9   static char *line = (char *)NULL;
10   int exit = 0;
11
12   while (!exit) {
13     if (line) {
14       free(line);
15       line = (char *)NULL;
16     }
17     
18     line = readline("gacela>");
19     
20     if (line && *line) {
21       add_history(line);
22       if (strcmp(line, "(quit)") == 0)
23         exit = 1;
24       else {
25         write(pout, line, strlen(line));
26         write(pout, "\n", 1);
27       }
28     }
29   }
30 }
31
32 int main (int argc, char *argv[])
33 {
34   pid_t cpid;
35   int pfd[2];
36
37   pipe(pfd);
38   cpid = fork();
39   if (cpid != 0) {
40     rspl(pfd[0], pfd[1]);
41     return 0;
42   }
43   else {
44     char buf;
45
46     while (1) {
47       while (read(pfd[0], &buf, 1) > 0) {
48         if (buf == '\n')
49           write(STDOUT_FILENO, "-\n", 2);
50         else
51           write(STDOUT_FILENO, &buf, 1);
52       }
53     }
54   }
55 }