-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurses.c
880 lines (775 loc) · 21.9 KB
/
curses.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/*
mtr -- a network diagnostic tool
Copyright (C) 1997,1998 Matt Kimball
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include <strings.h>
#include <unistd.h>
#ifndef NO_CURSES
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
/* MacOSX may need this before scoket.h...*/
#if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h>
#else
/* If a system doesn't have sys/types.h, lets hope that time_t is an int */
#define time_t int
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined(UNICODE)
#define _XOPEN_SOURCE_EXTENDED
#if defined (HAVE_NCURSESW_NCURSES_H)
# include <ncursesw/ncurses.h>
#elif defined (HAVE_NCURSESW_CURSES_H)
# include <ncursesw/curses.h>
#elif defined (HAVE_CURSES_H)
# include <curses.h>
#else
# error No ncursesw header file available
#endif
#if defined(HAVE_WCHAR_H)
# include <wchar.h>
#endif
#ifdef NETBSD_CURSES
#define CCHAR_attr attributes
#define CCHAR_chars vals
/*
#elif defined OPENSOLARIS_CURSES
#define CCHAR_attr _at
#define CCHAR_chars _wc
*/
#else
#define CCHAR_attr attr
#define CCHAR_chars chars
#endif
#else
#if defined(HAVE_NCURSES_H)
# include <ncurses.h>
#elif defined(HAVE_NCURSES_CURSES_H)
# include <ncurses/curses.h>
#elif defined(HAVE_CURSES_H)
# include <curses.h>
#elif defined(HAVE_CURSESX_H)
# include <cursesX.h>
#else
# error No curses header file available
#endif
#endif
#ifndef HAVE_ATTRON
#define attron(x)
#define attroff(x)
#endif
#ifndef getmaxyx
# define getmaxyx(win,y,x) ((y) = (win)->_maxy + 1, (x) = (win)->_maxx + 1)
#endif
#include "mtr.h"
#include "mtr-curses.h"
#include "net.h"
#include "dns.h"
#include "asn.h"
#include "display.h"
#include "version.h"
#endif
#include <time.h>
extern char LocalHostname[];
extern int fstTTL;
extern int maxTTL;
extern int cpacketsize;
extern int bitpattern;
extern int tos;
extern float WaitTime;
extern int af;
extern int mtrtype;
extern int color_mode;
static int __unused_int;
void pwcenter(char *str)
{
int maxx;
int cx;
getmaxyx(stdscr, __unused_int, maxx);
cx = (signed)(maxx - strlen(str)) / 2;
while(cx-- > 0)
addch(' ');
printw(str);
}
void mtr_fill_buf(int y, char *buf) {
move(2, y);
int curs = curs_set(1);
refresh();
int c;
int i = 0;
while ((c=getch()) != '\n' && i < MAXFLD) {
addch(c | A_BOLD); refresh();
buf[i++] = c; /* need more checking on 'c' */
}
if (curs != ERR)
curs_set(curs);
buf[i] = '\0';
}
int mtr_curses_keyaction(void)
{
int c = getch();
float f = 0.0;
char buf[MAXFLD+1];
if(c == 'q')
return ActionQuit;
if(c==3)
return ActionQuit;
if (c==12)
return ActionClear;
if ((c==19) || (tolower (c) == 'p'))
return ActionPause;
if ((c==17) || (c == ' '))
return ActionResume;
if(tolower(c) == 'r')
return ActionReset;
if (tolower(c) == 'd')
return ActionDisplay;
if (tolower(c) == 'e')
return ActionMPLS;
if (tolower(c) == 'n')
return ActionDNS;
#ifdef IPINFO
if (tolower(c) == 'y')
return ActionII;
if (tolower(c) == 'z')
return ActionAS;
#endif
if (c == '+')
return ActionScrollDown;
if (c == '-')
return ActionScrollUp;
if (tolower(c) == 's') {
mvprintw(2, 0, "Change Packet Size: %d\n", cpacketsize );
mvprintw(3, 0, "Size Range: %d-%d, < 0:random.\n", MINPACKET, MAXPACKET);
mtr_fill_buf(20, buf);
cpacketsize = atoi(buf);
return ActionNone;
}
if (tolower(c) == 'b') {
mvprintw(2, 0, "Ping Bit Pattern: %d\n", bitpattern );
mvprintw(3, 0, "Pattern Range: 0(0x00)-255(0xff), <0 random.\n");
mtr_fill_buf(18, buf);
bitpattern = atoi(buf);
if( bitpattern > 255 ) { bitpattern = -1; }
return ActionNone;
}
if ( c == 'Q') { /* can not be tolower(c) */
mvprintw(2, 0, "Type of Service(tos): %d\n", tos );
mvprintw(3, 0, "default 0x00, min cost 0x02, rel 0x04,, thr 0x08, low del 0x10...\n");
mtr_fill_buf(22, buf);
tos = atoi(buf);
if( tos > 255 || tos <0 ) {
tos = 0;
}
return ActionNone;
}
if (tolower(c) == 'i') {
mvprintw(2, 0, "Interval : %0.0f\n\n", WaitTime );
mtr_fill_buf(11, buf);
f = atof(buf);
if (f <= 0.0) return ActionNone;
if (getuid() != 0 && f < 1.0)
return ActionNone;
WaitTime = f;
return ActionNone;
}
if (tolower(c) == 'f') {
mvprintw(2, 0, "First TTL: %d\n\n", fstTTL );
mtr_fill_buf(11, buf);
int i = atoi(buf);
if ( i < 1 || i> maxTTL ) return ActionNone;
fstTTL = i;
return ActionNone;
}
if (tolower(c) == 'm') {
mvprintw(2, 0, "Max TTL: %d\n\n", maxTTL );
mtr_fill_buf(9, buf);
int i = atoi(buf);
if ( i < fstTTL || i>(MaxHost-1) ) return ActionNone;
maxTTL = i;
return ActionNone;
}
/* fields to display & their ordering */
if (tolower(c) == 'o') {
mvprintw(2, 0, "Fields: %s\n\n", fld_active );
int i;
for( i=0; i<MAXFLD; i++ ){
if( data_fields[i].descr != NULL )
printw(" %s\n", data_fields[i].descr);
}
addch('\n');
move(2,8); /* length of "Fields: " */
refresh();
i = 0;
int curs = curs_set(1);
while ( (c=getch ()) != '\n' && i < MAXFLD ) {
if( strchr(available_options, c) ) {
addch(c | A_BOLD); refresh();
buf[i++] = c; /* Only permit values in "available_options" be entered */
} else {
printf("\a"); /* Illegal character. Beep, ring the bell. */
}
}
if (curs != ERR)
curs_set(curs);
buf[i] = '\0';
if ( strlen( buf ) > 0 ) strcpy( fld_active, buf );
return ActionNone;
}
if (tolower(c) == 'j') {
if( index(fld_active, 'N') ) {
strcpy(fld_active, "DR AGJMXI"); /* GeoMean and jitter */
} else {
strcpy(fld_active, "LS NABWV"); /* default */
}
return ActionNone;
}
if (tolower(c) == 'u') {
switch ( mtrtype ) {
case IPPROTO_ICMP:
case IPPROTO_TCP:
mtrtype = IPPROTO_UDP;
break;
case IPPROTO_UDP:
mtrtype = IPPROTO_ICMP;
break;
}
return ActionNone;
}
if (tolower(c) == 't') {
switch ( mtrtype ) {
case IPPROTO_ICMP:
case IPPROTO_UDP:
mtrtype = IPPROTO_TCP;
break;
case IPPROTO_TCP:
mtrtype = IPPROTO_ICMP;
break;
}
return ActionNone;
}
/* reserve to display help message -Min */
if (tolower(c) == '?'|| tolower(c) == 'h') {
erase();
int pressanykey_row = 20;
mvprintw(2, 0, "Command:\n" );
printw(" ?|h help\n" );
printw(" p pause (SPACE to resume)\n" );
printw(" d switching display mode\n" );
printw(" e toggle MPLS information on/off\n" );
printw(" n toggle DNS on/off\n" );
printw(" r reset all counters\n" );
printw(" o str set the columns to display, default str='LRS N BAWV'\n" );
printw(" j toggle latency(LS NABWV)/jitter(DR AGJMXI) stats\n" );
printw(" c <n> report cycle n, default n=infinite\n" );
printw(" i <n> set the ping interval to n seconds, default n=1\n" );
printw(" f <n> set the initial time-to-live(ttl), default n=1\n" );
printw(" m <n> set the max time-to-live, default n= # of hops\n" );
printw(" s <n> set the packet size to n or random(n<0)\n" );
printw(" b <c> set ping bit pattern to c(0..255) or random(c<0)\n" );
printw(" Q <t> set ping packet's TOS to t\n" );
printw(" u switch between ICMP ECHO and UDP datagrams\n" );
#ifdef IPINFO
printw(" y switching IP info\n");
printw(" z toggle ASN info on/off\n");
pressanykey_row += 2;
#endif
addch('\n');
mvprintw(pressanykey_row, 0, " press any key to go back..." );
getch(); /* get any key */
return ActionNone;
}
return ActionNone; /* ignore unknown input */
}
void mtr_fill_data(int at, char *buf) {
int i, j;
int hd_len;
/* net_xxx returns times in usecs. Just display millisecs */
hd_len = 0;
for( i=0; i<MAXFLD; i++ ) {
/* Ignore options that don't exist */
/* On the other hand, we now check the input side. Shouldn't happen,
can't be careful enough. */
j = fld_index[fld_active[i]];
if (j == -1) continue;
/* temporay hack for stats usec to ms... */
if( index( data_fields[j].format, 'f' ) ) {
sprintf(buf + hd_len, data_fields[j].format,
data_fields[j].net_xxx(at) /1000.0 );
} else {
sprintf(buf + hd_len, data_fields[j].format,
data_fields[j].net_xxx(at) );
}
hd_len += data_fields[j].length;
}
buf[hd_len] = 0;
}
void mtr_curses_hosts(int startstat)
{
int max;
int at;
struct mplslen *mpls, *mplss;
ip_t *addr, *addrs;
int y;
char *name;
int i, k;
char buf[1024];
max = net_max();
for(at = net_min () + display_offset; at < max; at++) {
printw("%2d. ", at + 1);
addr = net_addr(at);
mpls = net_mpls(at);
if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) {
name = dns_lookup(addr);
if (! net_up(at))
attron(A_BOLD);
#ifdef IPINFO
if (enable_ipinfo)
printw(fmt_ipinfo(addr));
#endif
if(name != NULL) {
if (show_ips) printw("%s (%s)", name, strlongip(addr));
else printw("%s", name);
} else {
printw("%s", strlongip( addr ) );
}
attroff(A_BOLD);
getyx(stdscr, y, __unused_int);
move(y, startstat);
mtr_fill_data(at, buf);
printw("%s", buf);
for (k=0; k < mpls->labels && enablempls; k++) {
if((k+1 < mpls->labels) || (mpls->labels == 1)) {
/* if we have more labels */
printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]);
} else {
/* bottom label */
printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]);
}
}
/* Multi path */
for (i=0; i < MAXPATH; i++ ) {
addrs = net_addrs(at, i);
mplss = net_mplss(at, i);
if ( addrcmp( (void *) addrs, (void *) addr, af ) == 0 ) continue;
if ( addrcmp( (void *) addrs, (void *) &unspec_addr, af ) == 0 ) break;
name = dns_lookup(addrs);
if (! net_up(at)) attron(A_BOLD);
printw("\n ");
#ifdef IPINFO
if (enable_ipinfo)
printw(fmt_ipinfo(addrs));
#endif
if (name != NULL) {
if (show_ips) printw("%s (%s)", name, strlongip(addrs));
else printw("%s", name);
} else {
printw("%s", strlongip( addrs ) );
}
for (k=0; k < mplss->labels && enablempls; k++) {
printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mplss->label[k], mplss->exp[k], mplss->s[k], mplss->ttl[k]);
}
attroff(A_BOLD);
}
} else {
printw("???");
}
addch('\n');
}
move(2, 0);
}
static int low_ms, high_ms;
static chtype map1[] = {'.' | A_NORMAL, '>' | COLOR_PAIR(1)};
#define NUM_FACTORS2 8
static chtype map2[NUM_FACTORS2];
static double factors2[NUM_FACTORS2];
static int scale2[NUM_FACTORS2];
static int dm2_color_base;
static chtype map_na2[] = { ' ', '?', '>' | A_BOLD};
#ifdef UNICODE
#define NUM_FACTORS3_MONO 7 // without trailing char
#define NUM_FACTORS3 22
static cchar_t map3[NUM_FACTORS3];
static double factors3[NUM_FACTORS3];
static int scale3[NUM_FACTORS3];
static int dm3_color_base;
static cchar_t map_na3[3];
#endif
void mtr_gen_scale(int num_factors, int *scale, double *factors)
{
int *saved, i, max, at;
int range;
low_ms = 1000000;
high_ms = -1;
for (i = 0; i < num_factors; i++) {
scale[i] = 0;
}
max = net_max();
for (at = display_offset; at < max; at++) {
saved = net_saved_pings(at);
for (i = 0; i < SAVED_PINGS; i++) {
if (saved[i] < 0) continue;
if (saved[i] < low_ms) {
low_ms = saved[i];
}
if (saved[i] > high_ms) {
high_ms = saved[i];
}
}
}
range = high_ms - low_ms;
for (i = 0; i < num_factors; i++) {
scale[i] = low_ms + ((double)range * factors[i]);
}
}
void mtr_gen_scale_gc(void) {
#ifdef UNICODE
if (display_mode == 3)
mtr_gen_scale(color_mode ? NUM_FACTORS3 : (NUM_FACTORS3_MONO + 1), scale3, factors3);
else
#endif
mtr_gen_scale(NUM_FACTORS2, scale2, factors2);
}
void mtr_curses_factors_init(int num_factors, double *factors) {
/* Initialize factors to a log scale. */
int i;
for (i = 0; i < num_factors; i++) {
factors[i] = ((double)1 / num_factors) * (i + 1);
factors[i] *= factors[i]; /* Squared. */
}
}
void mtr_curses_init(void) {
// display mode 2
mtr_curses_factors_init(NUM_FACTORS2, factors2);
#ifdef UNICODE
// display mode 3
mtr_curses_factors_init(color_mode ? NUM_FACTORS3 : (NUM_FACTORS3_MONO + 1), factors3);
#endif
/* Initialize block_map. */
int block_split;
block_split = (NUM_FACTORS2 - 2) / 2;
if (block_split > 9) {
block_split = 9;
}
int i;
for (i = 1; i <= block_split; i++) {
map2[i] = '0' + i;
}
for (i = block_split + 1; i < NUM_FACTORS2 - 1; i++) {
map2[i] = 'a' + i - block_split - 1;
}
map2[0] = map1[0];
for (i = 1; i < NUM_FACTORS2 - 1; i++)
map2[i] |= COLOR_PAIR(dm2_color_base + i - 1);
map2[NUM_FACTORS2 - 1] = map1[1] & A_CHARTEXT;
map2[NUM_FACTORS2 - 1] |= (map2[NUM_FACTORS2 - 2] & A_ATTRIBUTES) | A_BOLD;
map_na2[1] |= color_mode ? (map2[NUM_FACTORS2 - 1] & A_ATTRIBUTES) : A_BOLD;
#ifdef UNICODE
for (i = 0; i < NUM_FACTORS3_MONO; i++)
map3[i].CCHAR_chars[0] = L'▁' + i;
if (color_mode) {
for (i = 0; i < NUM_FACTORS3 - 1; i++) {
int base = i / NUM_FACTORS3_MONO;
map3[i].CCHAR_attr = COLOR_PAIR(dm3_color_base + base);
if (i >= NUM_FACTORS3_MONO)
map3[i].CCHAR_chars[0] = map3[i % NUM_FACTORS3_MONO].CCHAR_chars[0];
}
map3[NUM_FACTORS3 - 1].CCHAR_chars[0] = map1[1] & A_CHARTEXT;
map3[NUM_FACTORS3 - 1].CCHAR_attr = map3[NUM_FACTORS3 - 2].CCHAR_attr | A_BOLD;
} else
map3[NUM_FACTORS3_MONO].CCHAR_chars[0] = map1[1] & A_CHARTEXT;
for (i = 0; i < (sizeof(map_na2) / sizeof(map_na2[0])); i++)
map_na3[i].CCHAR_chars[0] = map_na2[i] & A_CHARTEXT;
map_na3[1].CCHAR_attr = color_mode ? map3[NUM_FACTORS3 - 1].CCHAR_attr : A_BOLD;
map_na3[2].CCHAR_attr = A_BOLD;
#endif
}
chtype mtr_saved_ch(int saved_int) {
if (saved_int == -2)
return map_na2[0]; // unsent
if (saved_int == -1)
return map_na2[1]; // has not responded
if (display_mode == 1) {
return map1[(saved_int <= scale2[NUM_FACTORS2 - 2]) ? 0 : 1];
} else if (display_mode == 2) {
int i;
for (i = 0; i < NUM_FACTORS2; i++)
if (saved_int <= scale2[i])
return map2[i];
}
return map_na2[2]; // ???
}
#ifdef UNICODE
cchar_t* mtr_saved_cc(int saved_int) {
if (saved_int == -2)
return &map_na3[0];
if (saved_int == -1)
return &map_na3[1];
int num_factors = color_mode ? NUM_FACTORS3 : (NUM_FACTORS3_MONO + 1);
int i;
for (i = 0; i < num_factors; i++)
if (saved_int <= scale3[i])
return &map3[i];
return &map_na3[2]; // ???
}
#endif
void mtr_curses_graph(int startstat, int cols)
{
int max, at, y;
ip_t * addr;
char* name;
max = net_max();
for (at = net_min() + display_offset; at < max; at++) {
printw("%2d. ", at+1);
addr = net_addr(at);
if (!addr) {
printw("???\n");
continue;
}
if (! net_up(at))
attron(A_BOLD);
if (addrcmp((void *) addr, (void *) &unspec_addr, af)) {
#ifdef IPINFO
if (enable_ipinfo)
printw(fmt_ipinfo(addr));
#endif
name = dns_lookup(addr);
printw("%s", name?name:strlongip(addr));
} else
printw("???");
attroff(A_BOLD);
getyx(stdscr, y, __unused_int);
move(y, startstat);
addch(' ');
int *saved = net_saved_pings(at);
int i;
#ifdef UNICODE
if (display_mode == 3)
for (i = SAVED_PINGS - cols; i < SAVED_PINGS; i++)
add_wch(mtr_saved_cc(saved[i]));
else
#endif
for (i = SAVED_PINGS - cols; i < SAVED_PINGS; i++)
addch(mtr_saved_ch(saved[i]));
addch('\n');
}
}
int mtr_curses_data_fields(char *buf) {
int i, j;
int hd_len = 0;
char fmt[16];
for (i=0; i < MAXFLD; i++ ) {
j = fld_index[fld_active[i]];
if (j < 0) continue;
sprintf( fmt, "%%%ds", data_fields[j].length );
sprintf( buf + hd_len, fmt, data_fields[j].title );
hd_len += data_fields[j].length;
}
return hd_len;
}
#ifdef UNICODE
void mtr_print_scale3(int num_factors, int i0, int di) {
int i;
for (i = i0; i < num_factors; i += di) {
// printw(" %lc:%dms", map3[i].CCHAR_chars[0], scale3[i] / 1000);
addstr(" ");
add_wch(&map3[i]);
int sc = scale3[i] / 1000;
if (sc)
printw(":%dms", sc);
else
printw(":%.1fms", (double)scale3[i] / 1000);
}
addstr(" ");
add_wch(&map3[num_factors]);
}
#endif
void mtr_curses_redraw(void)
{
int maxx;
int startstat;
int rowstat;
time_t t;
int i;
int hd_len;
char buf[1024];
erase();
getmaxyx(stdscr, __unused_int, maxx);
rowstat = 5;
move(0, 0);
attron(A_BOLD);
pwcenter("My traceroute [v" MTR_VERSION "]");
attroff(A_BOLD);
mvprintw(1, 0, "%s (%s)", LocalHostname, net_localaddr());
time(&t);
mvprintw(1, maxx-25, ctime(&t));
printw("Keys: ");
attron(A_BOLD); addch('H'); attroff(A_BOLD); printw("elp ");
attron(A_BOLD); addch('D'); attroff(A_BOLD); printw("isplay mode ");
attron(A_BOLD); addch('R'); attroff(A_BOLD); printw("estart statistics ");
attron(A_BOLD); addch('O'); attroff(A_BOLD); printw("rder of fields ");
attron(A_BOLD); addch('q'); attroff(A_BOLD); printw("uit\n");
if (display_mode == 0) {
hd_len = mtr_curses_data_fields(buf);
attron(A_BOLD);
mvprintw(rowstat - 1, 0, " Host");
mvprintw(rowstat - 1, maxx-hd_len-1, "%s", buf);
mvprintw(rowstat - 2, maxx-hd_len-1, " Packets Pings");
attroff(A_BOLD);
move(rowstat, 0);
mtr_curses_hosts(maxx-hd_len-1);
} else {
char msg[80];
startstat = STARTSTAT;
#ifdef IPINFO
if (enable_ipinfo)
startstat += ii_getwidth();
#endif
int max_cols = maxx<=SAVED_PINGS+startstat ? maxx-startstat : SAVED_PINGS;
startstat -= 2;
sprintf(msg, " Last %3d pings", max_cols);
mvprintw(rowstat - 1, startstat, msg);
attroff(A_BOLD);
move(rowstat, 0);
mtr_gen_scale_gc();
mtr_curses_graph(startstat, max_cols);
addch('\n');
attron(A_BOLD);
printw("Scale:");
attroff(A_BOLD);
if (display_mode == 1) {
addstr(" ");
addch(map1[0] | A_BOLD);
printw(" less than %dms ", scale2[NUM_FACTORS2 - 2] / 1000);
addch(map1[1] | (color_mode ? 0 : A_BOLD));
addstr(" greater than ");
addch(map1[0] | A_BOLD);
addstr(" ");
addch('?' | (color_mode ? (map2[NUM_FACTORS2 - 1] & A_ATTRIBUTES) : A_BOLD));
addstr(" Unknown");
} else if (display_mode == 2) {
if (color_mode) {
for (i = 0; i < NUM_FACTORS2 - 1; i++) {
addstr(" ");
addch(map2[i]);
printw(":%dms", scale2[i] / 1000);
}
addstr(" ");
addch(map2[NUM_FACTORS2 - 1]);
} else {
for (i = 0; i < NUM_FACTORS2 - 1; i++)
printw(" %c:%dms", map2[i] & A_CHARTEXT, scale2[i] / 1000);
printw(" %c", map2[NUM_FACTORS2 - 1] & A_CHARTEXT);
}
}
#ifdef UNICODE
else if (display_mode == 3) {
if (color_mode)
mtr_print_scale3(NUM_FACTORS3 - 1, 1, 2);
else
mtr_print_scale3(NUM_FACTORS3_MONO, 0, 1);
}
#endif
}
refresh();
}
void mtr_curses_open(void)
{
initscr();
raw();
noecho();
if (color_mode)
if (!has_colors())
color_mode = 0;
if (color_mode) {
start_color();
int bg_col = 0;
#ifdef HAVE_USE_DEFAULT_COLORS
use_default_colors();
if (use_default_colors() == OK)
bg_col = -1;
#endif
int i = 1;
// display_mode 1
init_pair(i++, COLOR_YELLOW, bg_col);
// display_mode 2
dm2_color_base = i;
init_pair(i++, COLOR_GREEN, bg_col);
init_pair(i++, COLOR_CYAN, bg_col);
init_pair(i++, COLOR_BLUE, bg_col);
init_pair(i++, COLOR_YELLOW, bg_col);
init_pair(i++, COLOR_MAGENTA, bg_col);
init_pair(i++, COLOR_RED, bg_col);
#ifdef UNICODE
// display mode 3
dm3_color_base = i;
init_pair(i++, COLOR_GREEN, bg_col);
// init_pair(i++, COLOR_YELLOW, COLOR_GREEN);
init_pair(i++, COLOR_YELLOW, bg_col);
// init_pair(i++, COLOR_RED, COLOR_YELLOW);
init_pair(i++, COLOR_RED, bg_col);
init_pair(i++, COLOR_RED, bg_col);
#endif
}
mtr_curses_init();
mtr_curses_redraw();
curs_set(0);
}
void mtr_curses_close(void)
{
addch('\n');
endwin();
}
void mtr_curses_clear(void)
{
mtr_curses_close();
mtr_curses_open();
}
#ifdef GRAPHCAIRO
void mtr_curses_scale_desc(char *buf) {
static char scale_hs[] = "Scale:";
if (display_mode == 1)
sprintf(buf, "%s %c less than %dms %c greater than %c %c Unknown", scale_hs,
(char)(map1[0] & A_CHARTEXT), scale2[NUM_FACTORS2 - 2] / 1000, (char)(map1[1] & A_CHARTEXT),
(char)(map1[0] & A_CHARTEXT), (char)(map_na2[1] & A_CHARTEXT));
else if (display_mode == 2) {
int len = sprintf(buf, "%s", scale_hs);
int i;
for (i = 0; i < NUM_FACTORS2 - 1; i++)
len += sprintf(buf + len, " %c:%dms", (char)(map2[i] & A_CHARTEXT), scale2[i] / 1000);
sprintf(buf + len, " %c", (char)(map2[NUM_FACTORS2 - 1] & A_CHARTEXT));
}
#ifdef UNICODE
else if (display_mode == 3) {
int len = swprintf((wchar_t*)buf, 16, L"%s", scale_hs);
int i;
for (i = 0; i < NUM_FACTORS3_MONO; i++)
len += swprintf(((wchar_t*)buf) + len, 16, L" %lc:%dms", map3[i].CCHAR_chars[0], scale3[i] / 1000);
swprintf(((wchar_t*)buf) + len, 4, L" %c", map3[NUM_FACTORS3_MONO].CCHAR_chars[0]);
}
#endif
}
#ifdef UNICODE
wchar_t mtr_curses_saved_wch(int saved_int) {
cchar_t *cc = mtr_saved_cc(saved_int);
return cc->CCHAR_chars[0];
}
#endif
char mtr_curses_saved_ch(int saved_int) {
return (char)(mtr_saved_ch(saved_int) & A_CHARTEXT);
}
#endif