Annotation of embedaddon/mtr/split.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:     mtr  --  a network diagnostic tool
                      3:     Copyright (C) 1997  Matt Kimball
                      4: 
                      5:     split.c -- raw output (for inclusion in KDE Network Utilities or others
                      6:                          GUI based tools)
                      7:     Copyright (C) 1998  Bertrand Leconte <B.Leconte@mail.dotcom.fr>
                      8: 
                      9:     This program is free software; you can redistribute it and/or modify
                     10:     it under the terms of the GNU General Public License version 2 as 
                     11:     published by the Free Software Foundation.
                     12: 
                     13:     This program is distributed in the hope that it will be useful,
                     14:     but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15:     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16:     GNU General Public License for more details.
                     17: 
                     18:     You should have received a copy of the GNU General Public License
                     19:     along with this program; if not, write to the Free Software
                     20:     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     21: */
                     22: 
1.1.1.2 ! misho      23: #include "config.h"
        !            24: 
1.1       misho      25: #include <ctype.h>
                     26: #include <stdlib.h>
                     27: #include <stdio.h>
                     28: #include <string.h>
                     29: #include <sys/types.h>
                     30: 
                     31: #include "mtr.h"
                     32: #include "display.h"
                     33: #include "dns.h"
                     34: 
                     35: #include "net.h"
                     36: #include "split.h"
                     37: 
                     38: #ifdef NO_CURSES
                     39: #include <sys/time.h>
                     40: #include <sys/types.h>
                     41: #include <unistd.h>
                     42: #else
                     43: /* Use the curses variant */
                     44: 
                     45: #if defined(HAVE_NCURSES_H)
                     46: #  include <ncurses.h>
                     47: #elif defined(HAVE_NCURSES_CURSES_H)
                     48: #  include <ncurses/curses.h>
                     49: #elif defined(HAVE_CURSES_H)
                     50: #  include <curses.h>
                     51: #else
                     52: #  error No curses header file available
                     53: #endif
                     54: 
                     55: #endif
                     56: 
                     57: 
                     58: extern char *Hostname;
                     59: extern int WaitTime;
                     60: extern int af;
                     61: 
                     62: /* There is 256 hops max in the IP header (coded with a byte) */
                     63: #define MAX_LINE_COUNT 256
                     64: #define MAX_LINE_SIZE  256
                     65: 
                     66: char Lines[MAX_LINE_COUNT][MAX_LINE_SIZE];
                     67: int  LineCount;
                     68: 
                     69: 
                     70: #define DEBUG 0
                     71: 
                     72: 
                     73: void split_redraw(void) 
                     74: {
                     75:   int   max;
                     76:   int   at;
                     77:   ip_t *addr;
                     78:   char  newLine[MAX_LINE_SIZE];
                     79:   int   i;
                     80: 
                     81: #if DEBUG
                     82:   fprintf(stderr, "split_redraw()\n"); 
                     83: #endif
                     84: 
                     85:   /* 
                     86:    * If there is less lines than last time, we delete them
                     87:    * TEST THIS PLEASE
                     88:    */
                     89:   max = net_max();
                     90:   for (i=LineCount; i>max; i--) {
                     91:     printf("-%d\n", i);
                     92:     LineCount--;
                     93:   }
                     94: 
                     95:   /*
                     96:    * For each line, we compute the new one and we compare it to the old one
                     97:    */
                     98:   for(at = 0; at < max; at++) {
                     99:     addr = net_addr(at);
                    100:     if(addrcmp((void*)addr, (void*)&unspec_addr, af)) {
                    101:       char str[256], *name;
                    102:       if (!(name = dns_lookup(addr)))
                    103:         name = strlongip(addr);
                    104:       if (show_ips) {
                    105:         snprintf(str, sizeof(str), "%s %s", name, strlongip(addr));
                    106:         name = str;
                    107:       }
                    108:       /* May be we should test name's length */
                    109:       snprintf(newLine, sizeof(newLine), "%s %d %d %d %d %d %d", name,
                    110:                net_loss(at),
                    111:                net_returned(at), net_xmit(at),
                    112:                net_best(at) /1000, net_avg(at)/1000,
                    113:                net_worst(at)/1000);
                    114:     } else {
                    115:       sprintf(newLine, "???");
                    116:     }
                    117: 
                    118:     if (strcmp(newLine, Lines[at]) == 0) {
                    119:       /* The same, so do nothing */
                    120: #if DEBUG
                    121:       printf("SAME LINE\n");
                    122: #endif
                    123:     } else {
                    124:       printf("%d %s\n", at+1, newLine);
                    125:       fflush(stdout);
                    126:       strcpy(Lines[at], newLine);
                    127:       if (LineCount < (at+1)) {
                    128:        LineCount = at+1;
                    129:       }
                    130:     }
                    131:   }
                    132: }
                    133: 
                    134: 
                    135: void split_open(void)
                    136: {
                    137:   int i;
                    138: #if DEBUG
                    139:   printf("split_open()\n");
                    140: #endif
                    141:   LineCount = -1;
                    142:   for (i=0; i<MAX_LINE_COUNT; i++) {
                    143:     strcpy(Lines[i], "???");
                    144:   }
                    145: }
                    146: 
                    147: 
                    148: void split_close(void)
                    149: {
                    150: #if DEBUG
                    151:   printf("split_close()\n");
                    152: #endif
                    153: }
                    154: 
                    155: 
                    156: int split_keyaction(void) 
                    157: {
                    158: #ifdef NO_CURSES
                    159:   fd_set readfds;
                    160:   struct timeval tv;
                    161:   char c;
                    162: 
                    163:   FD_ZERO (&readfds);
                    164:   FD_SET (0, &readfds);
                    165:   tv.tv_sec = 0;
                    166:   tv.tv_usec = 0;
                    167: 
                    168:   if (select (1, &readfds, NULL, NULL, &tv) > 0) {
                    169:     read (0, &c, 1);
                    170:   } else 
                    171:     return 0;
                    172: #else
                    173:   char c = getch();
                    174: #endif
                    175: 
                    176: #if DEBUG
                    177:   printf("split_keyaction()\n");
                    178: #endif
                    179:   if(tolower(c) == 'q')
                    180:     return ActionQuit;
                    181:   if(c==3)
                    182:     return ActionQuit;
                    183:   if(tolower(c) == 'r')
                    184:     return ActionReset;
                    185:   
                    186:   return 0;
                    187: }

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