Mercurial > hg
annotate mcabber/src/fifo.c @ 1572:8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 11 Apr 2009 12:30:51 +0200 |
parents | eefa0ae248d8 |
children | a087125d8fc8 |
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 * |
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 |
1572
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
42 static const char *FIFO_ENV_NAME = "MCABBER_FIFO"; |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 // fifo_init(fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 // Create and open the FIFO file. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 // If fifo_path is NULL, reopen the current pipe. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 // Return 0 (success) or -1 (failure). |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 int fifo_init(const char *fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 struct stat buf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 int fd; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 char *fifo_path_xp; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 if (!sfd && !fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 return -1; // Nothing to do... |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 if (sfd && !fifo_path) { // We want to reinitialize the pipe |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 fclose(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 if (fifo_name) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 goto fifo_init_open; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 } |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 fifo_path_xp = expand_filename(fifo_path); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 if (!stat(fifo_path_xp, &buf)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 if (!S_ISFIFO(buf.st_mode)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. " |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 "%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
|
71 g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 return -1; |
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 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 if (unlink(fifo_path_xp)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 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
|
77 fifo_path_xp, g_strerror(errno)); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 return -1; |
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 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 if (mkfifo(fifo_path_xp, S_IWUSR | S_IRUSR)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO [%s]", |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 g_strerror(errno)); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 g_free(fifo_path_xp); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 return -1; |
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 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 fifo_name = fifo_path_xp; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 fifo_init_open: |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 fd = open(fifo_name, O_RDONLY | O_NONBLOCK); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 if (!fd) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
1572
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
97 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
|
98 |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 sfd = fdopen(fd, "r"); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 if (fifo_path) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 return 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 } |
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 // fifo_deinit() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 // Close the current FIFO pipe and delete it. |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 void fifo_deinit(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 { |
1572
8c0237c8c186
Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents:
1526
diff
changeset
|
109 unsetenv(FIFO_ENV_NAME); |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 if (sfd) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 fclose(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 sfd = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 } |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 if (fifo_name) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 unlink(fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 g_free(fifo_name); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 fifo_name = NULL; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 } |
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 // fifo_read() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 // 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
|
123 void fifo_read(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 struct timeval tv; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 fd_set fds; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 char *getbuf; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 char buf[HBB_BLOCKSIZE+1]; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 int fd; |
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 if (!sfd) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 return; |
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 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
135 tv.tv_sec = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 tv.tv_usec = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 fd = fileno(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
139 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 FD_ZERO(&fds); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
141 FD_SET(fd, &fds); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 select(fd + 1, &fds, NULL, NULL, &tv); |
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 if (!FD_ISSET(fd, &fds)) { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 return; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 } |
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 getbuf = fgets(buf, HBB_BLOCKSIZE, sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 if (getbuf) { |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
151 guint logflag; |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
152 char *eol = buf; |
1526
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
153 guint fifo_ignore = settings_opt_get_int("fifo_ignore"); |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
154 |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 // Strip trailing newlines |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 for ( ; *eol ; eol++) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 ; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
158 if (eol > buf) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
159 eol--; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
160 while (eol > buf && *eol == '\n') |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
161 *eol-- = 0; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 |
1427
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
163 if (settings_opt_get_int("fifo_hide_commands")) |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
164 logflag = LPRINT_LOG; |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
165 else |
a8eb9aba2ed2
Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents:
1426
diff
changeset
|
166 logflag = LPRINT_LOGNORM; |
1526
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
167 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
|
168 (fifo_ignore ? "Ignoring" : "Executing"), buf); |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
169 if (!fifo_ignore) { |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
170 if (process_command(buf, TRUE) == 255) |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
171 mcabber_set_terminate_ui(); |
eefa0ae248d8
Add option "fifo_ignore" (Suggested by T. Glaser)
Mikael Berthe <mikael@lilotux.net>
parents:
1427
diff
changeset
|
172 } |
1426
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
173 } else { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
174 if (feof(sfd)) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
175 fifo_init(NULL); // Reopen the FIFO on EOF |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
176 } |
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 // fifo_get_fd() |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
180 // Return the FIFO file descriptor (-1 if none). |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
181 int fifo_get_fd(void) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
182 { |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
183 if (sfd) |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
184 return fileno(sfd); |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
185 return -1; |
a64778f5f26b
Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
186 } |
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 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |