Annotation of embedaddon/trafshow/help_page.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  *     Copyright (c) 2004 Rinet Corp., Novosibirsk, Russia
                      3:  *
                      4:  * Redistribution and use in source forms, with and without modification,
                      5:  * are permitted provided that this entire comment appears intact.
                      6:  *
                      7:  * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
                      8:  */
                      9: 
                     10: #ifdef HAVE_CONFIG_H
                     11: #include <config.h>
                     12: #endif
                     13: 
                     14: #ifdef HAVE_SLCURSES
                     15: #include <slcurses.h>
                     16: #elif  HAVE_NCURSES
                     17: #include <ncurses.h>
                     18: #else
                     19: #include <curses.h>
                     20: #endif
                     21: #include <stdio.h>
                     22: #include <string.h>
                     23: 
                     24: #include "trafshow.h"
                     25: #include "help_page.h"
                     26: #include "getkey.h"
                     27: #include "screen.h"
                     28: #include "selector.h"
                     29: 
                     30: static ShowMode help_mode = HelpPage;
                     31: 
                     32: struct help_page_entry {
                     33:        int key;
                     34:        const char *name;
                     35:        const char *descr;
                     36: };
                     37: 
                     38: static struct help_page_entry Interfaces_help[] = {
                     39:  { 'q',                "   Esc",       "Quit the program"      },
                     40:  { K_CR,       "  Enter",      "Use Arrow-Keys to select Interface to show" },
                     41:  { K_CTRL('L'),        "  Ctrl-L",     "Refresh screen from scratch" },
                     42:  { 'r',                "    R",        "Set the screen refresh-period.." },
                     43:  { 'p',                "    P",        "Set the expired data purge-period.." },
                     44:  { 'f',                "    F",        "Set the filter expression (empty to reset).." },
                     45:  { '/',                "    /",        "To search & follow for string in the list.." },
                     46:  { K_CTRL('_'),        "  Ctrl-/",     "Turn off search & follow mode" },
                     47:  { 'a',                "    A",        "To aggregate/summarize flows totally.." },
                     48:  { K_CTRL('R'),        "  Ctrl-R",     "Reset all flow cache totally" },
                     49:  { 'n',                "    N",        "Toggle numeric values to names conversion" },
                     50: 
                     51:  { 0,0,0 }
                     52: };
                     53: 
                     54: static struct help_page_entry NetStat_help[] = {
                     55:  { 'q',                "   Esc",       "Return to previous page" },
                     56:  { K_CR,       "  Enter",      "Use Arrow-Keys to select Flow for detail" },
                     57:  { K_LEFT,     "   Left",      "Rotate show mode left" },
                     58:  { K_RIGHT,    "  Right",      "Rotate show mode right" },
                     59:  { K_TAB,      "   Tab",       "Move cursor to backflow if any" },
                     60:  { K_CTRL('L'),        "  Ctrl-L",     "Refresh screen from scratch" },
                     61:  { 'r',                "    R",        "Set the screen refresh-period.." },
                     62:  { 'p',                "    P",        "Set the expired data purge-period.." },
                     63:  { 'f',                "    F",        "Set the filter expression (empty to reset).." },
                     64:  { '/',                "    /",        "To search & follow for string in the list.." },
                     65:  { K_CTRL('_'),        "  Ctrl-/",     "Turn off search & follow mode" },
                     66:  { 'a',                "    A",        "To aggregate/summarize flows in the list.." },
                     67:  { K_CTRL('R'),        "  Ctrl-R",     "Reset flow cache on the Interface" },
                     68:  { 'n',                "    N",        "Toggle numeric values to names conversion" },
                     69: 
                     70:  { 0,0,0 }
                     71: };
                     72: 
                     73: ShowMode
                     74: help_page_mode()
                     75: {
                     76:        return help_mode;
                     77: }
                     78: 
                     79: static void
                     80: scale_size(name, descr)
                     81:        int *name, *descr;
                     82: {
                     83:        *name   = line_factor * (double)HELP_PAGE_NAME;
                     84:        *descr  = line_factor * (double)HELP_PAGE_DESCR;
                     85: }
                     86: 
                     87: static int
                     88: help_page_header(dst, size, unused)
                     89:        char *dst;
                     90:        int size;
                     91:        const void *unused;
                     92: {
                     93:        int name_sz, desc_sz;
                     94: 
                     95:        /* sanity check */
                     96:        if (!dst || size < 1 || unused)
                     97:                return 0;
                     98: 
                     99:        scale_size(&name_sz, &desc_sz);
                    100: 
                    101:        snprintf(dst, size,
                    102:                 "%-*.*s %-*.*s",
                    103:                 name_sz, name_sz,      " KeyPress",
                    104:                 desc_sz, desc_sz,      "Action");
                    105:        return 0;
                    106: }
                    107: 
                    108: static int
                    109: help_page_line(dst, size, hp, idx)
                    110:        char *dst;
                    111:        int size;
                    112:        const struct help_page_entry *hp;
                    113:        int idx;
                    114: {
                    115:        int name_sz, desc_sz;
                    116: 
                    117:        /* sanity check */
                    118:        if (!dst || size < 1 || !hp)
                    119:                return 0;
                    120: 
                    121:        scale_size(&name_sz, &desc_sz);
                    122:        snprintf(dst, size,
                    123:                 "%-*.*s %-*.*s",
                    124:                 name_sz, name_sz,      hp[idx].name,
                    125:                 desc_sz, desc_sz,      hp[idx].descr);
                    126:        return 0;
                    127: }
                    128: 
                    129: static int
                    130: help_page_footer(dst, size, topic)
                    131:        char *dst;
                    132:        int size;
                    133:        const char *topic;
                    134: {
                    135:        int i, len;
                    136:        SELECTOR *sp = help_page_selector();
                    137: 
                    138:        /* sanity check */
                    139:        if (!dst || size < 1 || !topic || !sp)
                    140:                return 0;
                    141:        i = 0;
                    142:        len = strlen(topic);
                    143:        if (len > 0 && len < sp->COLS) {
                    144:                len = sp->COLS/2 - len/2;
                    145:                while (i < len) dst[i++] = ' ';
                    146:        }
                    147:        (void)strncpy(dst + i, topic, size - i);
                    148:        dst[size-1] = '\0';
                    149:        return 0;
                    150: }
                    151: 
                    152: SELECTOR *
                    153: help_page_selector()
                    154: {
                    155:        static SELECTOR *sp = 0;
                    156:        if (!sp && (sp = selector_init()) != 0) {
                    157:                int name_sz, desc_sz;
                    158:                scale_size(&name_sz, &desc_sz);
                    159: 
                    160:                sp->window_color = A_REVERSE;
                    161:                sp->cursor_color = A_NORMAL;
                    162:                sp->COLS = MIN(name_sz + desc_sz, COLS);
                    163:                sp->LINES = MIN(sp->COLS/3, LINES);
                    164:                sp->COL = COLS/2 - sp->COLS/2;
                    165:                sp->LINE = LINES/2 - sp->LINES/2;
                    166:                sp->get_header = help_page_header;
                    167:                sp->get_line = help_page_line;
                    168:                sp->get_footer = help_page_footer;
                    169:        }
                    170:        return sp;
                    171: }
                    172: 
                    173: int
                    174: help_page_key(idx)
                    175:        int idx;
                    176: {
                    177:        int i;
                    178:        SELECTOR *sp;
                    179:        const struct help_page_entry *hp;
                    180: 
                    181:        if ((sp = help_page_selector()) == 0)
                    182:                return -1;
                    183:        hp = (const struct help_page_entry *)sp->list;
                    184:        for (i = 0; hp; hp++) {
                    185:                if (i++ == idx) break;
                    186:        }
                    187:        return (hp ? hp->key : -1);
                    188: }
                    189: 
                    190: SELECTOR *
                    191: help_page_list(mode)
                    192:        ShowMode mode;
                    193: {
                    194:        struct help_page_entry *hp = 0;
                    195:        char *topic = 0;
                    196:        SELECTOR *sp;
                    197: 
                    198:        switch (mode) {
                    199:        case Interfaces:
                    200:                hp = Interfaces_help;
                    201:                topic = "Interface selection Help";
                    202:                break;
                    203:        case NetStat:
                    204:                hp = NetStat_help;
                    205:                topic = "Network Flow selection Help";
                    206:                break;
                    207:        case FlowDump:  /* no help available */
                    208:        case HelpPage:  /* help on help?? */
                    209:                return 0;
                    210:        }
                    211: 
                    212:        if ((sp = help_page_selector()) != 0) {
                    213:                help_mode = mode;
                    214:                sp->header = 0; /* unused */
                    215:                sp->footer = topic;
                    216:                sp->list = hp;
                    217:                sp->items = 0;
                    218:                for (; hp && hp->name; hp++)
                    219:                        sp->items++;
                    220:        }
                    221:        return sp;
                    222: }
                    223: 

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