author | Mikael Berthe <mikael@lilotux.net> |
Fri, 29 Feb 2008 14:55:55 +0100 | |
changeset 1443 | 0623d694a77f |
parent 1427 | a8eb9aba2ed2 |
child 1526 | eefa0ae248d8 |
permissions | -rw-r--r-- |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
/* |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
2 |
* fifo.c -- Read commands from a named pipe |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
3 |
* |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
4 |
* Copyright (C) 2008 Mikael Berthe <mikael@lilotux.net> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
5 |
* |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
6 |
* This program is free software; you can redistribute it and/or modify |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
* your option) any later version. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
* |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, but |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
* General Public License for more details. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
* |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
* along with this program; if not, write to the Free Software |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
* USA |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
*/ |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
#include <stdio.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
#include <glib.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
#include <sys/types.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 |
#include <sys/stat.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 |
#include <fcntl.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 |
#include <unistd.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 |
#include <sys/time.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 |
#include <errno.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 |
#include <fcntl.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
#include "commands.h" |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
#include "logprint.h" |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
#include "utils.h" |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
35 |
#include "settings.h" |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
#include "hbuf.h" // For HBB_BLOCKSIZE |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
static FILE *sfd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
static char *fifo_name; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
// fifo_init(fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
// Create and open the FIFO file. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
// If fifo_path is NULL, reopen the current pipe. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
// Return 0 (success) or -1 (failure). |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
int fifo_init(const char *fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
{ |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 |
struct stat buf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 |
int fd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
char *fifo_path_xp; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
if (!sfd && !fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
return -1; // Nothing to do... |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
if (sfd && !fifo_path) { // We want to reinitialize the pipe |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
fclose(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 |
sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
if (fifo_name) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
goto fifo_init_open; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
fifo_path_xp = expand_filename(fifo_path); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
if (!stat(fifo_path_xp, &buf)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
if (!S_ISFIFO(buf.st_mode)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. " |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
"%s already exists and is not a pipe", fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 |
if (unlink(fifo_path_xp)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
scr_LogPrint(LPRINT_LOGNORM, "WARNING: Unable to unlink FIFO %s [%s]", |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 |
fifo_path_xp, g_strerror(errno)); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
82 |
if (mkfifo(fifo_path_xp, S_IWUSR | S_IRUSR)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 |
scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO [%s]", |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
g_strerror(errno)); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 |
g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 |
return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
fifo_name = fifo_path_xp; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
fifo_init_open: |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
fd = open(fifo_name, O_RDONLY | O_NONBLOCK); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 |
if (!fd) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 |
return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
sfd = fdopen(fd, "r"); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
if (fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
98 |
scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 |
return 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 |
// fifo_deinit() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 |
// Close the current FIFO pipe and delete it. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 |
void fifo_deinit(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 |
{ |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 |
if (sfd) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 |
fclose(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 |
sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 |
if (fifo_name) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 |
unlink(fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 |
g_free(fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 |
fifo_name = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 |
// fifo_read() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 |
// Read a line from the FIFO pipe (if available), and execute it. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 |
void fifo_read(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
120 |
{ |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 |
struct timeval tv; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 |
fd_set fds; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 |
char *getbuf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 |
char buf[HBB_BLOCKSIZE+1]; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 |
int fd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 |
if (!sfd) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 |
return; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
tv.tv_sec = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 |
tv.tv_usec = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
134 |
fd = fileno(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
135 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 |
FD_ZERO(&fds); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 |
FD_SET(fd, &fds); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
139 |
select(fd + 1, &fds, NULL, NULL, &tv); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
141 |
if (!FD_ISSET(fd, &fds)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 |
return; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
144 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
145 |
getbuf = fgets(buf, HBB_BLOCKSIZE, sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 |
if (getbuf) { |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
147 |
guint logflag; |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
148 |
char *eol = buf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
149 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 |
// Strip trailing newlines |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
151 |
for ( ; *eol ; eol++) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
152 |
; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
153 |
if (eol > buf) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
154 |
eol--; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 |
while (eol > buf && *eol == '\n') |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 |
*eol-- = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 |
|
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
158 |
if (settings_opt_get_int("fifo_hide_commands")) |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
159 |
logflag = LPRINT_LOG; |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
160 |
else |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
161 |
logflag = LPRINT_LOGNORM; |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
162 |
scr_LogPrint(logflag, "Executing FIFO command: %s", buf); |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 |
if (process_command(buf, TRUE) == 255) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
164 |
mcabber_set_terminate_ui(); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
} else { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
166 |
if (feof(sfd)) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
167 |
fifo_init(NULL); // Reopen the FIFO on EOF |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
168 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
169 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
170 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
171 |
// fifo_get_fd() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
172 |
// Return the FIFO file descriptor (-1 if none). |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
173 |
int fifo_get_fd(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
174 |
{ |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
175 |
if (sfd) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
176 |
return fileno(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
177 |
return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
178 |
} |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
179 |
|
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
180 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |