File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / mtr / split.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jul 21 23:43:42 2013 UTC (10 years, 11 months ago) by misho
Branches: mtr, elwix, MAIN
CVS tags: v0_85, HEAD
0.85

    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: 
   23: #include <config.h>
   24: #include <ctype.h>
   25: #include <stdlib.h>
   26: #include <stdio.h>
   27: #include <string.h>
   28: #include <sys/types.h>
   29: 
   30: #include "mtr.h"
   31: #include "display.h"
   32: #include "dns.h"
   33: 
   34: #include "net.h"
   35: #include "split.h"
   36: 
   37: #ifdef NO_CURSES
   38: #include <sys/time.h>
   39: #include <sys/types.h>
   40: #include <unistd.h>
   41: #else
   42: /* Use the curses variant */
   43: 
   44: #if defined(HAVE_NCURSES_H)
   45: #  include <ncurses.h>
   46: #elif defined(HAVE_NCURSES_CURSES_H)
   47: #  include <ncurses/curses.h>
   48: #elif defined(HAVE_CURSES_H)
   49: #  include <curses.h>
   50: #else
   51: #  error No curses header file available
   52: #endif
   53: 
   54: #endif
   55: 
   56: 
   57: extern char *Hostname;
   58: extern int WaitTime;
   59: extern int af;
   60: 
   61: /* There is 256 hops max in the IP header (coded with a byte) */
   62: #define MAX_LINE_COUNT 256
   63: #define MAX_LINE_SIZE  256
   64: 
   65: char Lines[MAX_LINE_COUNT][MAX_LINE_SIZE];
   66: int  LineCount;
   67: 
   68: 
   69: #define DEBUG 0
   70: 
   71: 
   72: void split_redraw(void) 
   73: {
   74:   int   max;
   75:   int   at;
   76:   ip_t *addr;
   77:   char  newLine[MAX_LINE_SIZE];
   78:   int   i;
   79: 
   80: #if DEBUG
   81:   fprintf(stderr, "split_redraw()\n"); 
   82: #endif
   83: 
   84:   /* 
   85:    * If there is less lines than last time, we delete them
   86:    * TEST THIS PLEASE
   87:    */
   88:   max = net_max();
   89:   for (i=LineCount; i>max; i--) {
   90:     printf("-%d\n", i);
   91:     LineCount--;
   92:   }
   93: 
   94:   /*
   95:    * For each line, we compute the new one and we compare it to the old one
   96:    */
   97:   for(at = 0; at < max; at++) {
   98:     addr = net_addr(at);
   99:     if(addrcmp((void*)addr, (void*)&unspec_addr, af)) {
  100:       char str[256], *name;
  101:       if (!(name = dns_lookup(addr)))
  102:         name = strlongip(addr);
  103:       if (show_ips) {
  104:         snprintf(str, sizeof(str), "%s %s", name, strlongip(addr));
  105:         name = str;
  106:       }
  107:       /* May be we should test name's length */
  108:       snprintf(newLine, sizeof(newLine), "%s %d %d %d %d %d %d", name,
  109:                net_loss(at),
  110:                net_returned(at), net_xmit(at),
  111:                net_best(at) /1000, net_avg(at)/1000,
  112:                net_worst(at)/1000);
  113:     } else {
  114:       sprintf(newLine, "???");
  115:     }
  116: 
  117:     if (strcmp(newLine, Lines[at]) == 0) {
  118:       /* The same, so do nothing */
  119: #if DEBUG
  120:       printf("SAME LINE\n");
  121: #endif
  122:     } else {
  123:       printf("%d %s\n", at+1, newLine);
  124:       fflush(stdout);
  125:       strcpy(Lines[at], newLine);
  126:       if (LineCount < (at+1)) {
  127: 	LineCount = at+1;
  128:       }
  129:     }
  130:   }
  131: }
  132: 
  133: 
  134: void split_open(void)
  135: {
  136:   int i;
  137: #if DEBUG
  138:   printf("split_open()\n");
  139: #endif
  140:   LineCount = -1;
  141:   for (i=0; i<MAX_LINE_COUNT; i++) {
  142:     strcpy(Lines[i], "???");
  143:   }
  144: }
  145: 
  146: 
  147: void split_close(void)
  148: {
  149: #if DEBUG
  150:   printf("split_close()\n");
  151: #endif
  152: }
  153: 
  154: 
  155: int split_keyaction(void) 
  156: {
  157: #ifdef NO_CURSES
  158:   fd_set readfds;
  159:   struct timeval tv;
  160:   char c;
  161: 
  162:   FD_ZERO (&readfds);
  163:   FD_SET (0, &readfds);
  164:   tv.tv_sec = 0;
  165:   tv.tv_usec = 0;
  166: 
  167:   if (select (1, &readfds, NULL, NULL, &tv) > 0) {
  168:     read (0, &c, 1);
  169:   } else 
  170:     return 0;
  171: #else
  172:   char c = getch();
  173: #endif
  174: 
  175: #if DEBUG
  176:   printf("split_keyaction()\n");
  177: #endif
  178:   if(tolower(c) == 'q')
  179:     return ActionQuit;
  180:   if(c==3)
  181:     return ActionQuit;
  182:   if(tolower(c) == 'r')
  183:     return ActionReset;
  184:   
  185:   return 0;
  186: }

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