Mercurial > hg
annotate mcabber/src/fifo.c @ 1642:cd67b29e04e3
Fix not execution of HOOK_INTERNAL
author | Myhailo Danylenko <isbear@ukrpost.net> |
---|---|
date | Mon, 02 Nov 2009 19:11:29 +0200 |
parents | dcd5d4c75199 |
children | fca9a4c17432 |
rev | line source |
---|---|
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 * |
1599 | 4 * Copyright (C) 2008,2009 Mikael Berthe <mikael@lilotux.net> |
1426
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> |
1598 | 23 #include <stdlib.h> |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 #include <glib.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 #include <sys/types.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 #include <sys/stat.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 #include <fcntl.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 #include <unistd.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 #include <sys/time.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 #include <errno.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 #include <fcntl.h> |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 #include "commands.h" |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 #include "logprint.h" |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 #include "utils.h" |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
36 #include "settings.h" |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 #include "hbuf.h" // For HBB_BLOCKSIZE |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 static FILE *sfd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 static char *fifo_name; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
1572
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
43 static const char *FIFO_ENV_NAME = "MCABBER_FIFO"; |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 // fifo_init(fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 // Create and open the FIFO file. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 // If fifo_path is NULL, reopen the current pipe. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 // Return 0 (success) or -1 (failure). |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 int fifo_init(const char *fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 struct stat buf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 int fd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 char *fifo_path_xp; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 if (!sfd && !fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 return -1; // Nothing to do... |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 if (sfd && !fifo_path) { // We want to reinitialize the pipe |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 fclose(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 if (fifo_name) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 goto fifo_init_open; |
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 sfd = NULL; |
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 fifo_path_xp = expand_filename(fifo_path); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 if (!stat(fifo_path_xp, &buf)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 if (!S_ISFIFO(buf.st_mode)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. " |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 "%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
|
72 g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 } |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 if (unlink(fifo_path_xp)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 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
|
78 fifo_path_xp, g_strerror(errno)); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 return -1; |
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 } |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 if (mkfifo(fifo_path_xp, S_IWUSR | S_IRUSR)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO [%s]", |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 g_strerror(errno)); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 } |
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_name = fifo_path_xp; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 fifo_init_open: |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 fd = open(fifo_name, O_RDONLY | O_NONBLOCK); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 if (!fd) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
1572
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
98 setenv(FIFO_ENV_NAME, fifo_name, 1); |
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
99 |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 sfd = fdopen(fd, "r"); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 if (fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 return 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 } |
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 // fifo_deinit() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 // Close the current FIFO pipe and delete it. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 void fifo_deinit(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 { |
1572
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
110 unsetenv(FIFO_ENV_NAME); |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 if (sfd) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 fclose(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 sfd = 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 if (fifo_name) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 unlink(fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 g_free(fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 fifo_name = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 } |
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 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 // fifo_read() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 // 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
|
124 void fifo_read(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 struct timeval tv; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 fd_set fds; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 char *getbuf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 char buf[HBB_BLOCKSIZE+1]; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 int fd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 if (!sfd) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 return; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
134 } |
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 tv.tv_sec = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 tv.tv_usec = 0; |
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 fd = fileno(sfd); |
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 FD_ZERO(&fds); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 FD_SET(fd, &fds); |
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 select(fd + 1, &fds, NULL, NULL, &tv); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
145 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 if (!FD_ISSET(fd, &fds)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 return; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
148 } |
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 getbuf = fgets(buf, HBB_BLOCKSIZE, sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
151 if (getbuf) { |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
152 guint logflag; |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
153 char *eol = buf; |
1526
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
154 guint fifo_ignore = settings_opt_get_int("fifo_ignore"); |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 // Strip trailing newlines |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 for ( ; *eol ; eol++) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
158 ; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
159 if (eol > buf) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
160 eol--; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
161 while (eol > buf && *eol == '\n') |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 *eol-- = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
164 if (settings_opt_get_int("fifo_hide_commands")) |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
165 logflag = LPRINT_LOG; |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
166 else |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
167 logflag = LPRINT_LOGNORM; |
1526
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
168 scr_LogPrint(logflag, "%s FIFO command: %s", |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
169 (fifo_ignore ? "Ignoring" : "Executing"), buf); |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
170 if (!fifo_ignore) { |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
171 if (process_command(buf, TRUE) == 255) |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
172 mcabber_set_terminate_ui(); |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
173 } |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
174 } else { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
175 if (feof(sfd)) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
176 fifo_init(NULL); // Reopen the FIFO on EOF |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
177 } |
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 // fifo_get_fd() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
181 // Return the FIFO file descriptor (-1 if none). |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
182 int fifo_get_fd(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
183 { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
184 if (sfd) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
185 return fileno(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
186 return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
187 } |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
188 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
189 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |