Annotation of embedaddon/bmon/src/bindings.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * bindings.c       Key Bindings
                      3:  *
                      4:  * Copyright (c) 2001-2005 Thomas Graf <tgraf@suug.ch>
                      5:  *
                      6:  * Permission is hereby granted, free of charge, to any person obtaining a
                      7:  * copy of this software and associated documentation files (the "Software"),
                      8:  * to deal in the Software without restriction, including without limitation
                      9:  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
                     10:  * and/or sell copies of the Software, and to permit persons to whom the
                     11:  * Software is furnished to do so, subject to the following conditions:
                     12:  *
                     13:  * The above copyright notice and this permission notice shall be included
                     14:  * in all copies or substantial portions of the Software.
                     15:  *
                     16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
                     17:  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
                     19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
                     21:  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
                     22:  * DEALINGS IN THE SOFTWARE.
                     23:  */
                     24: 
                     25: #include <bmon/bmon.h>
                     26: #include <bmon/bindings.h>
                     27: #include <bmon/utils.h>
                     28: 
                     29: static binding_t *bindings;
                     30: 
                     31: void add_binding(binding_t *b)
                     32: {
                     33:        b->next = bindings;
                     34:        bindings = b;
                     35: }
                     36: 
                     37: static void run_binding(binding_t *b, const char *args_)
                     38: {
                     39:        pid_t pid;
                     40:        int i, n;
                     41:        char *s, *args = strdup(args_);
                     42: 
                     43:        for (i = 1; b->args[i]; i++);
                     44:        n = i;
                     45:        
                     46:        if (i < 255)
                     47:        b->args[i++] = args;
                     48:        
                     49:        for (s = args; i < 255; i++) {
                     50:                s = strchr (s, ' ');
                     51:                if (s) {
                     52:                        *s = '\0';
                     53:                        s++;
                     54:                        b->args[i] = s;
                     55:                } else
                     56:                        break;
                     57:        }
                     58: 
                     59:        b->args[i] = NULL;
                     60: 
                     61:        pid = fork();
                     62:        if (pid < 0)
                     63:                quit("fork error: %s\n", strerror(errno));
                     64:        else if (pid) {
                     65:                /* parent */
                     66:                b->args[n] = NULL;
                     67:                return;
                     68:        }
                     69:        
                     70:        /* child */
                     71: 
                     72: #ifdef HAVE_FCLOSEALL
                     73:        fcloseall();
                     74: #else
                     75:        for (i = 0; i < 256; i++)
                     76:                close(i);
                     77: #endif
                     78:        
                     79:        if (execve(b->cmd, b->args, NULL) < 0)
                     80:                quit("execve failed: %s\n", strerror(errno));
                     81:        
                     82:        /* this is only needed in case execve fails */
                     83:        _exit(0);
                     84: }
                     85: 
                     86: int handle_bindings(int ch, const char *args)
                     87: {
                     88:        binding_t *b;
                     89: 
                     90:        for (b = bindings; b; b = b->next) {
                     91:                if (ch == b->ch) {
                     92:                        run_binding(b, args);
                     93:                        return 1;
                     94:                }
                     95:        }
                     96: 
                     97:        return 0;
                     98: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>