| 49 | | #endif |
| 50 | | |
| 51 | | |
| 52 | | |
| 53 | | /* sample function 'mul2' */ |
| 54 | | /* takes one argument, a number */ |
| 55 | | /* multiplies the number by 2.0 */ |
| 56 | | /* Note: all local functions should be declared 'static' */ |
| 57 | | |
| 58 | | static void my_mul2(RESULT * result, RESULT * arg1) |
| 59 | | { |
| 60 | | double param; |
| 61 | | double value; |
| 62 | | |
| 63 | | /* Get Parameter */ |
| 64 | | /* R2N stands for 'Result to Number' */ |
| 65 | | param = R2N(arg1); |
| 66 | | |
| 67 | | /* calculate value */ |
| 68 | | value = param * 2.0; |
| 69 | | |
| 70 | | /* store result */ |
| 71 | | /* when called with R_NUMBER, it assumes the */ |
| 72 | | /* next parameter to be a pointer to double */ |
| 73 | | SetResult(&result, R_NUMBER, &value); |
| 74 | | } |
| 75 | | |
| 76 | | |
| 77 | | /* sample function 'mul3' */ |
| 78 | | /* takes one argument, a number */ |
| 79 | | /* multiplies the number by 3.0 */ |
| 80 | | /* same as 'mul2', but shorter */ |
| 81 | | |
| 82 | | static void my_mul3(RESULT * result, RESULT * arg1) |
| 83 | | { |
| 84 | | /* do it all in one line */ |
| 85 | | double value = R2N(arg1) * 3.0; |
| 86 | | |
| 87 | | /* store result */ |
| 88 | | SetResult(&result, R_NUMBER, &value); |
| 89 | | } |
| 90 | | |
| 91 | | |
| 92 | | /* sample function 'diff' */ |
| 93 | | /* takes two arguments, both numbers */ |
| 94 | | /* returns |a-b| */ |
| 95 | | |
| 96 | | static void my_diff(RESULT * result, RESULT * arg1, RESULT * arg2) |
| 97 | | { |
| 98 | | /* do it all in one line */ |
| 99 | | double value = R2N(arg1) - R2N(arg2); |
| 100 | | |
| 101 | | /* some more calculations... */ |
| 102 | | if (value < 0) |
| 103 | | value = -value; |
| 104 | | |
| 105 | | /* store result */ |
| 106 | | SetResult(&result, R_NUMBER, &value); |
| 107 | | } |
| 108 | | |
| 109 | | |
| 110 | | /* sample function 'answer' */ |
| 111 | | /* takes no argument! */ |
| 112 | | /* returns the answer to all questions */ |
| 113 | | |
| 114 | | static void my_answer(RESULT * result) |
| 115 | | { |
| 116 | | /* we have to declare a variable because */ |
| 117 | | /* SetResult needs a pointer */ |
| 118 | | double value = 42; |
| 119 | | |
| 120 | | /* store result */ |
| 121 | | SetResult(&result, R_NUMBER, &value); |
| 122 | | } |
| 123 | | |
| 124 | | |
| 125 | | /* sample function 'length' */ |
| 126 | | /* takes one argument, a string */ |
| 127 | | /* returns the string length */ |
| 128 | | |
| 129 | | static void my_length(RESULT * result, RESULT * arg1) |
| 130 | | { |
| 131 | | /* Note #1: value *must* be double! */ |
| 132 | | /* Note #2: R2S stands for 'Result to String' */ |
| 133 | | double value = strlen(R2S(arg1)); |
| 134 | | |
| 135 | | /* store result */ |
| 136 | | SetResult(&result, R_NUMBER, &value); |
| 137 | | } |
| 138 | | |
| 139 | | |
| 140 | | |
| 141 | | /* sample function 'upcase' */ |
| 142 | | /* takes one argument, a string */ |
| 143 | | /* returns the string in upper case letters */ |
| 144 | | |
| 145 | | static void my_upcase(RESULT * result, RESULT * arg1) |
| 146 | | { |
| 147 | | char *value, *p; |
| 148 | | |
| 149 | | /* create a local copy of the argument */ |
| 150 | | /* Do *NOT* try to modify the original string! */ |
| 151 | | value = strdup(R2S(arg1)); |
| 152 | | |
| 153 | | /* process the string */ |
| 154 | | for (p = value; *p != '\0'; p++) |
| 155 | | *p = toupper(*p); |
| 156 | | |
| 157 | | /* store result */ |
| 158 | | /* when called with R_STRING, it assumes the */ |
| 159 | | /* next parameter to be a pointer to a string */ |
| 160 | | /* 'value' is already a char*, so use 'value', not '&value' */ |
| 161 | | SetResult(&result, R_STRING, value); |
| 162 | | |
| 163 | | /* free local copy again */ |
| 164 | | /* Note that SetResult() makes its own string copy */ |
| 165 | | free(value); |
| 166 | | } |
| 167 | | |
| 168 | | |
| 169 | | /* sample function 'cat' */ |
| 170 | | /* takes variable number of arguments, all strings */ |
| 171 | | /* returns all prameters concatenated */ |
| 172 | | |
| 173 | | static void my_concat(RESULT * result, int argc, RESULT * argv[]) |
| 174 | | { |
| 175 | | int i, len; |
| 176 | | char *value, *part; |
| 177 | | |
| 178 | | /* start with a empty string */ |
| 179 | | value = strdup(""); |
| 180 | | |
| 181 | | /* process all arguments */ |
| 182 | | for (i = 0; i < argc; i++) { |
| 183 | | part = R2S(argv[i]); |
| 184 | | len = strlen(value) + strlen(part); |
| 185 | | value = realloc(value, len + 1); |
| 186 | | strcat(value, part); |
| 187 | | } |
| 188 | | |
| 189 | | /* store result */ |
| 190 | | SetResult(&result, R_STRING, value); |
| 191 | | |
| 192 | | /* free local string */ |
| 193 | | free(value); |
| 194 | | } |
| 195 | | |
| 196 | | |
| 197 | | /* plugin initialization */ |
| 198 | | /* MUST NOT be declared 'static'! */ |
| 199 | | int plugin_init_sample(void) |
| 200 | | { |
| 201 | | |
| 202 | | /* register all our cool functions */ |
| 203 | | /* the second parameter is the number of arguments */ |
| 204 | | /* -1 stands for variable argument list */ |
| 205 | | AddFunction("sample::mul2", 1, my_mul2); |
| 206 | | AddFunction("sample::mul3", 1, my_mul3); |
| 207 | | AddFunction("sample::answer", 0, my_answer); |
| 208 | | AddFunction("sample::diff", 2, my_diff); |
| 209 | | AddFunction("sample::length", 1, my_length); |
| 210 | | AddFunction("sample::upcase", 1, my_upcase); |
| 211 | | AddFunction("sample::concat", -1, my_concat); |
| 212 | | |
| 213 | | return 0; |
| 214 | | } |
| 215 | | |
| 216 | | void plugin_exit_sample(void) |
| 217 | | { |
| 218 | | /* free any allocated memory */ |
| 219 | | /* close filedescriptors */ |
| 220 | | } |
| | 53 | #endif /* |
| | 54 | */ |
| | 55 | |
| | 56 | #define FIFO_BUFFER_SIZE 80 |
| | 57 | |
| | 58 | typedef struct _FifoData { |
| | 59 | |
| | 60 | char *path; |
| | 61 | |
| | 62 | int input; |
| | 63 | |
| | 64 | int created; |
| | 65 | |
| | 66 | |
| | 67 | } FifoData; |
| | 68 | |
| | 69 | |
| | 70 | static char Section[] = "Plugin:FIFO"; |
| | 71 | |
| | 72 | |
| | 73 | static FifoData fd; |
| | 74 | |
| | 75 | static char msg[FIFO_BUFFER_SIZE]; |
| | 76 | |
| | 77 | static char fifopath[1024]; |
| | 78 | |
| | 79 | |
| | 80 | static void configure_fifo(void) |
| | 81 | { |
| | 82 | |
| | 83 | char *s; |
| | 84 | |
| | 85 | s = cfg_get(Section, "fifopath", "/tmp/lcd4linux.fifo"); |
| | 86 | |
| | 87 | if (*s == '\0') { |
| | 88 | |
| | 89 | info("[FIFO] empty '%s.fifopath' entry from %s, assuming '/tmp/lcd4linux.fifo'", Section, cfg_source()); |
| | 90 | |
| | 91 | strcpy(fifopath, "/tmp/lcd4linux.fifo"); |
| | 92 | |
| | 93 | } else |
| | 94 | |
| | 95 | strcpy(fifopath, s); |
| | 96 | |
| | 97 | |
| | 98 | free(s); |
| | 99 | |
| | 100 | } |
| | 101 | |
| | 102 | |
| | 103 | |
| | 104 | static void removeFifo() |
| | 105 | { |
| | 106 | |
| | 107 | debug("Removing FIFO \"%s\"\n", fd.path); |
| | 108 | |
| | 109 | |
| | 110 | if (unlink(fd.path) < 0) { |
| | 111 | |
| | 112 | error("Could not remove FIFO \"%s\": %s\n", fd.path, strerror(errno)); |
| | 113 | |
| | 114 | return; |
| | 115 | |
| | 116 | } |
| | 117 | |
| | 118 | |
| | 119 | fd.created = 0; |
| | 120 | |
| | 121 | } |
| | 122 | |
| | 123 | |
| | 124 | static void closeFifo() |
| | 125 | { |
| | 126 | |
| | 127 | struct stat st; |
| | 128 | |
| | 129 | |
| | 130 | if (fd.input >= 0) { |
| | 131 | |
| | 132 | close(fd.input); |
| | 133 | |
| | 134 | fd.input = -1; |
| | 135 | |
| | 136 | } |
| | 137 | |
| | 138 | |
| | 139 | if (fd.created && (stat(fd.path, &st) == 0)) |
| | 140 | |
| | 141 | removeFifo(fd); |
| | 142 | |
| | 143 | } |
| | 144 | |
| | 145 | |
| | 146 | static int makeFifo() |
| | 147 | { |
| | 148 | |
| | 149 | if (mkfifo(fd.path, 0666) < 0) { |
| | 150 | |
| | 151 | error("Couldn't create FIFO \"%s\": %s\n", fd.path, strerror(errno)); |
| | 152 | |
| | 153 | return -1; |
| | 154 | |
| | 155 | } |
| | 156 | |
| | 157 | |
| | 158 | fd.created = 1; |
| | 159 | |
| | 160 | |
| | 161 | return 0; |
| | 162 | |
| | 163 | } |
| | 164 | |
| | 165 | |
| | 166 | |
| | 167 | static int checkFifo() |
| | 168 | { |
| | 169 | |
| | 170 | struct stat st; |
| | 171 | |
| | 172 | if (stat(fd.path, &st) < 0) { |
| | 173 | |
| | 174 | if (errno == ENOENT) { |
| | 175 | |
| | 176 | /* Path doesn't exist */ |
| | 177 | return makeFifo(fd); |
| | 178 | |
| | 179 | } |
| | 180 | |
| | 181 | |
| | 182 | error("Failed to stat FIFO \"%s\": %s\n", fd.path, strerror(errno)); |
| | 183 | |
| | 184 | return -1; |
| | 185 | |
| | 186 | } |
| | 187 | |
| | 188 | |
| | 189 | if (!S_ISFIFO(st.st_mode)) { |
| | 190 | |
| | 191 | error("\"%s\" already exists, but is not a FIFO\n", fd.path); |
| | 192 | |
| | 193 | return -1; |
| | 194 | |
| | 195 | } |
| | 196 | |
| | 197 | |
| | 198 | return 0; |
| | 199 | |
| | 200 | } |
| | 201 | |
| | 202 | |
| | 203 | static int openFifo() |
| | 204 | { |
| | 205 | |
| | 206 | if (checkFifo() < 0) |
| | 207 | |
| | 208 | return -1; |
| | 209 | |
| | 210 | |
| | 211 | fd.input = open(fd.path, O_RDONLY | O_NONBLOCK); |
| | 212 | |
| | 213 | if (fd.input < 0) { |
| | 214 | |
| | 215 | error("Could not open FIFO \"%s\" for reading: %s\n", fd.path, strerror(errno)); |
| | 216 | |
| | 217 | closeFifo(); |
| | 218 | |
| | 219 | return -1; |
| | 220 | |
| | 221 | } |
| | 222 | |
| | 223 | |
| | 224 | return 0; |
| | 225 | |
| | 226 | } |
| | 227 | |
| | 228 | |
| | 229 | static void fiforead(RESULT * result) |
| | 230 | { |
| | 231 | |
| | 232 | char buf[FIFO_BUFFER_SIZE]; |
| | 233 | |
| | 234 | int i, bytes = 1; |
| | 235 | |
| | 236 | memset(buf, 0, FIFO_BUFFER_SIZE); |
| | 237 | |
| | 238 | |
| | 239 | strcat(buf, "ERROR"); |
| | 240 | |
| | 241 | if (checkFifo() == 0) { |
| | 242 | |
| | 243 | memset(buf, 0, FIFO_BUFFER_SIZE); |
| | 244 | |
| | 245 | while (bytes > 0 && errno != EINTR) |
| | 246 | |
| | 247 | bytes = read(fd.input, buf, FIFO_BUFFER_SIZE); |
| | 248 | |
| | 249 | |
| | 250 | if (bytes < 0) { |
| | 251 | |
| | 252 | error("[FIFO] Error %i: %s\n", errno, strerror(errno)); |
| | 253 | |
| | 254 | } else { |
| | 255 | |
| | 256 | |
| | 257 | if (strlen(buf) > 0) { |
| | 258 | |
| | 259 | strcpy(msg, buf); |
| | 260 | |
| | 261 | } |
| | 262 | |
| | 263 | |
| | 264 | for (i = 0; i < strlen(buf); i++) { |
| | 265 | |
| | 266 | if (msg[i] < 0x20) |
| | 267 | |
| | 268 | msg[i] = ' '; |
| | 269 | |
| | 270 | } |
| | 271 | |
| | 272 | } |
| | 273 | |
| | 274 | } |
| | 275 | |
| | 276 | |
| | 277 | /* store result */ |
| | 278 | SetResult(&result, R_STRING, msg); |
| | 279 | |
| | 280 | } |
| | 281 | |
| | 282 | |
| | 283 | |
| | 284 | /* plugin initialization */ |
| | 285 | int plugin_init_fifo(void) |
| | 286 | { |
| | 287 | |
| | 288 | configure_fifo(); |
| | 289 | |
| | 290 | |
| | 291 | fd.path = fifopath; |
| | 292 | |
| | 293 | fd.input = -1; |
| | 294 | |
| | 295 | fd.created = 0; |
| | 296 | |
| | 297 | |
| | 298 | if (openFifo() < 0) { |
| | 299 | |
| | 300 | return -1; |
| | 301 | |
| | 302 | } |
| | 303 | |
| | 304 | |
| | 305 | memset(msg, 0, FIFO_BUFFER_SIZE); |
| | 306 | |
| | 307 | AddFunction("fifo::read", 0, fiforead); |
| | 308 | |
| | 309 | |
| | 310 | return 0; |
| | 311 | |
| | 312 | } |
| | 313 | |
| | 314 | |
| | 315 | void plugin_exit_fifo(void) |
| | 316 | { |
| | 317 | |
| | 318 | /* close filedescriptors */ |
| | 319 | closeFifo(); |
| | 320 | |
| | 321 | } |