forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.c
1829 lines (1612 loc) · 47.3 KB
/
ui.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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ui.c -- various curses interfaces
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#define STDIN_FILENO 0
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* include stuff from 4.3 BSD */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <pthread.h>
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "ui.h"
#ifdef HAVE_LIBTOKYOCABINET
#include "tcabdb.h"
#else
#include "gkhash.h"
#endif
#include "color.h"
#include "error.h"
#include "gmenu.h"
#include "goaccess.h"
#include "util.h"
#include "xmalloc.h"
/* *INDENT-OFF* */
/* Determine which metrics should be displayed per module/panel */
static GOutput outputting[] = {
{VISITORS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1} ,
{REQUESTS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0} ,
{REQUESTS_STATIC , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0} ,
{NOT_FOUND , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0} ,
{HOSTS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0} ,
{OS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1} ,
{BROWSERS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1} ,
{VISIT_TIMES , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1} ,
{VIRTUAL_HOSTS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0} ,
{REFERRERS , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0} ,
{REFERRING_SITES , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0} ,
{KEYPHRASES , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0} ,
#ifdef HAVE_LIBGEOIP
{GEO_LOCATION , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0} ,
#endif
{STATUS_CODES , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0} ,
};
/* *INDENT-ON* */
/* Structure to display overall statistics */
typedef struct Field_
{
const char *field;
/* char due to log, bw, log_file */
char *value;
GColors *(*colorlbl) (void);
GColors *(*colorval) (void);
short oneliner;
} Field;
/* Determine which metrics to output given a module
*
* On error, or if not found, NULL is returned.
* On success, the panel value is returned. */
GOutput *
output_lookup (GModule module)
{
int i, num_panels = ARRAY_SIZE (outputting);
for (i = 0; i < num_panels; i++) {
if (outputting[i].module == module)
return &outputting[i];
}
return NULL;
}
/* Initialize curses colors */
void
init_colors (int force)
{
/* use default foreground/background colors */
use_default_colors ();
/* first set a default normal color */
set_normal_color ();
/* then parse custom colors and initialize them */
set_colors (force);
}
/* Ncurses' window handling */
void
set_input_opts (void)
{
initscr ();
clear ();
noecho ();
halfdelay (10);
nonl ();
intrflush (stdscr, FALSE);
keypad (stdscr, TRUE);
if (curs_set (0) == ERR)
LOG_DEBUG (("Unable to change cursor: %s\n", strerror (errno)));
if (conf.mouse_support)
mousemask (BUTTON1_CLICKED, NULL);
}
/* Deletes the given window, freeing all memory associated with it. */
void
close_win (WINDOW * w)
{
if (w == NULL)
return;
wclear (w);
wrefresh (w);
delwin (w);
}
/* Get the current calendar time as a value of type time_t and convert
* time_t to tm as local time */
void
generate_time (void)
{
timestamp = time (NULL);
now_tm = localtime (×tamp);
}
/* Set the loading spinner as ended and manage the mutex locking. */
void
end_spinner (void)
{
pthread_mutex_lock (&parsing_spinner->mutex);
parsing_spinner->state = SPN_END;
pthread_mutex_unlock (&parsing_spinner->mutex);
}
/* Set background colors to all windows. */
void
set_wbkgd (WINDOW * main_win, WINDOW * header_win)
{
GColors *color = get_color (COLOR_BG);
/* background colors */
wbkgd (main_win, COLOR_PAIR (color->pair->idx));
wbkgd (header_win, COLOR_PAIR (color->pair->idx));
wbkgd (stdscr, COLOR_PAIR (color->pair->idx));
wrefresh (main_win);
}
/* Creates and the new terminal windows and set basic properties to
* each of them. e.g., background color, enable the reading of
* function keys. */
void
init_windows (WINDOW ** header_win, WINDOW ** main_win)
{
int row = 0, col = 0;
/* init standard screen */
getmaxyx (stdscr, row, col);
if (row < MIN_HEIGHT || col < MIN_WIDTH)
FATAL ("Minimum screen size - 0 columns by 7 lines");
/* init header screen */
*header_win = newwin (6, col, 0, 0);
keypad (*header_win, TRUE);
if (*header_win == NULL)
FATAL ("Unable to allocate memory for header_win.");
/* init main screen */
*main_win = newwin (row - 8, col, 7, 0);
keypad (*main_win, TRUE);
if (*main_win == NULL)
FATAL ("Unable to allocate memory for main_win.");
set_wbkgd (*main_win, *header_win);
}
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
/* Draw a generic header with the ability to set a custom text to it. */
void
draw_header (WINDOW * win, const char *s, const char *fmt, int y, int x, int w,
GColors * (*func) (void))
{
GColors *color = (*func) ();
char *buf;
buf = xmalloc (snprintf (NULL, 0, fmt, s) + 1);
sprintf (buf, fmt, s);
wattron (win, color->attr | COLOR_PAIR (color->pair->idx));
mvwhline (win, y, x, ' ', w);
mvwaddnstr (win, y, x, buf, w);
wattroff (win, color->attr | COLOR_PAIR (color->pair->idx));
free (buf);
}
#pragma GCC diagnostic warning "-Wformat-nonliteral"
/* Determine the actual size of the main window. */
void
term_size (WINDOW * main_win, int *main_win_height)
{
int term_h = 0, term_w = 0;
getmaxyx (stdscr, term_h, term_w);
*main_win_height = term_h - (MAX_HEIGHT_HEADER + MAX_HEIGHT_FOOTER);
wresize (main_win, *main_win_height, term_w);
wmove (main_win, *main_win_height, 0);
}
/* Get the module/panel label name for the given module enum value.
*
* On success, a string containing the label name is returned. */
const char *
module_to_label (GModule module)
{
static const char *modules[] = {
VISIT_LABEL,
REQUE_LABEL,
STATI_LABEL,
FOUND_LABEL,
HOSTS_LABEL,
OPERA_LABEL,
BROWS_LABEL,
VTIME_LABEL,
VHOST_LABEL,
REFER_LABEL,
SITES_LABEL,
KEYPH_LABEL,
#ifdef HAVE_LIBGEOIP
GEOLO_LABEL,
#endif
CODES_LABEL,
};
return modules[module];
}
/* Get the module/panel label id for the given module enum value.
*
* On success, a string containing the label id is returned. */
const char *
module_to_id (GModule module)
{
static const char *modules[] = {
VISIT_ID,
REQUE_ID,
STATI_ID,
FOUND_ID,
HOSTS_ID,
OPERA_ID,
BROWS_ID,
VTIME_ID,
VHOST_ID,
REFER_ID,
SITES_ID,
KEYPH_ID,
#ifdef HAVE_LIBGEOIP
GEOLO_ID,
#endif
CODES_ID,
};
return modules[module];
}
/* Get the module/panel label header for the given module enum value.
*
* On success, a string containing the label header is returned. */
const char *
module_to_head (GModule module)
{
static const char *modules[] = {
VISIT_HEAD,
REQUE_HEAD,
STATI_HEAD,
FOUND_HEAD,
HOSTS_HEAD,
OPERA_HEAD,
BROWS_HEAD,
VTIME_HEAD,
VHOST_HEAD,
REFER_HEAD,
SITES_HEAD,
KEYPH_HEAD,
#ifdef HAVE_LIBGEOIP
GEOLO_HEAD,
#endif
CODES_HEAD,
};
if (!conf.ignore_crawlers)
modules[VISITORS] = VISIT_HEAD INCLUDE_BOTS;
return modules[module];
}
/* Get the module/panel label description for the given module enum
* value.
*
* On success, a string containing the label description is returned. */
const char *
module_to_desc (GModule module)
{
static const char *modules[] = {
VISIT_DESC,
REQUE_DESC,
STATI_DESC,
FOUND_DESC,
HOSTS_DESC,
OPERA_DESC,
BROWS_DESC,
VTIME_DESC,
VHOST_DESC,
REFER_DESC,
SITES_DESC,
KEYPH_DESC,
#ifdef HAVE_LIBGEOIP
GEOLO_DESC,
#endif
CODES_DESC,
};
return modules[module];
}
/* Rerender the header window to reflect active module. */
void
update_active_module (WINDOW * header_win, GModule current)
{
GColors *color = get_color (COLOR_ACTIVE_LABEL);
const char *module = module_to_label (current);
int col = getmaxx (stdscr);
char *lbl = xmalloc (snprintf (NULL, 0, "[Active Panel: %s]", module) + 1);
sprintf (lbl, "[Active Panel: %s]", module);
wmove (header_win, 0, 30);
wattron (header_win, color->attr | COLOR_PAIR (color->pair->idx));
mvwprintw (header_win, 0, col - strlen (lbl) - 1, "%s", lbl);
wattroff (header_win, color->attr | COLOR_PAIR (color->pair->idx));
wrefresh (header_win);
free (lbl);
}
/* Print out (terminal) an overall field label. e.g., 'Processed Time' */
static void
render_overall_field (WINDOW * win, const char *s, int y, int x,
GColors * color)
{
wattron (win, color->attr | COLOR_PAIR (color->pair->idx));
mvwprintw (win, y, x, "%s", s);
wattroff (win, color->attr | COLOR_PAIR (color->pair->idx));
}
/* Print out (terminal) an overall field value. e.g., '120 secs' */
static void
render_overall_value (WINDOW * win, const char *s, int y, int x,
GColors * color)
{
wattron (win, color->attr | COLOR_PAIR (color->pair->idx));
mvwprintw (win, y, x, "%s", s);
wattroff (win, color->attr | COLOR_PAIR (color->pair->idx));
}
/* Convert the number of excluded ips to a string.
*
* On success, the number of excluded ips as a string is returned. */
static char *
get_str_excluded_ips (GLog * glog)
{
return int2str (glog->excluded_ip, 0);
}
/* Convert the number of failed requests to a string.
*
* On success, the number of failed requests as a string is returned. */
static char *
get_str_failed_reqs (GLog * glog)
{
return int2str (glog->invalid, 0);
}
/* Convert the number of processed requests to a string.
*
* On success, the number of processed requests as a string is returned. */
static char *
get_str_processed_reqs (GLog * glog)
{
return int2str (glog->processed, 0);
}
/* Convert the number of valid requests to a string.
*
* On success, the number of valid requests as a string is returned. */
static char *
get_str_valid_reqs (GLog * glog)
{
return int2str (glog->valid, 0);
}
/* Convert the number of not found requests to a string.
*
* On success, the number of not found requests as a string is
* returned. */
static char *
get_str_notfound_reqs (void)
{
return int2str (ht_get_size_datamap (NOT_FOUND), 0);
}
/* Convert the number of referrers to a string.
*
* On success, the number of referrers as a string is returned. */
static char *
get_str_ref_reqs (void)
{
return int2str (ht_get_size_datamap (REFERRERS), 0);
}
/* Convert the number of requests to a string.
*
* On success, the number of requests as a string is returned. */
static char *
get_str_reqs (void)
{
return int2str (ht_get_size_datamap (REQUESTS), 0);
}
/* Convert the number of static requests to a string.
*
* On success, the number of static requests as a string is returned. */
static char *
get_str_static_reqs (void)
{
return int2str (ht_get_size_datamap (REQUESTS_STATIC), 0);
}
/* Convert the number of unique visitors to a string.
*
* On success, the number of unique visitors as a string is returned. */
static char *
get_str_visitors (void)
{
return int2str (ht_get_size_uniqmap (VISITORS), 0);
}
/* Convert the time taken to process the log to a string.
*
* On success, the time taken to process the log as a string is
* returned. */
static char *
get_str_proctime (void)
{
return int2str (((long long) end_proc - start_proc), 0);
}
/* Get the log file size in a human readable format.
*
* On success, the log file size as a string is returned. */
static char *
get_str_filesize (GLog * glog, const char *ifile)
{
if (!glog->piping && ifile != NULL)
return filesize_str (file_size (ifile));
else
return alloc_string ("N/A");
}
/* Get the log file path.
*
* On success, the log file path as a string is returned. */
static char *
get_str_logfile (GLog * glog, const char *ifile)
{
if (!glog->piping && ifile != NULL)
return alloc_string (ifile);
else
return alloc_string ("STDIN");
}
/* Get the bandwidth in a human readable format.
*
* On success, the bandwidth as a string is returned. */
static char *
get_str_bandwidth (GLog * glog)
{
return filesize_str ((float) glog->resp_size);
}
/* Iterate over the visitors module and sort date in an ascending
* order.
*
* On success, an array of sorted dates is returned. */
static char **
get_visitors_dates (GHolder * h)
{
char **dates = malloc (sizeof (char *) * h->holder_size);
int i;
for (i = 0; i < h->idx; i++) {
dates[i] = h->items[i].metrics->data;
}
qsort (dates, h->holder_size, sizeof (char *), strcmp_asc);
return dates;
}
/* Get the overall statistics header (label).
*
* On success, an string containing the overall header is returned. */
char *
get_overall_header (GHolder * h)
{
const char *sndfmt = conf.spec_date_time_num_format;
const char *head = conf.output_stdout ? T_HEAD : T_DASH " - " T_HEAD;
char *hd = NULL, *start = NULL, *end = NULL, **dates = NULL;
if (h->idx == 0)
return xstrdup (head);
dates = get_visitors_dates (h + VISITORS);
/* just display the actual dates - no specificity */
start = get_visitors_date (dates[0], sndfmt, "%d/%b/%Y");
end = get_visitors_date (dates[h->idx - 1], sndfmt, "%d/%b/%Y");
hd = xmalloc (snprintf (NULL, 0, "%s (%s - %s)", head, start, end) + 1);
sprintf (hd, "%s (%s - %s)", head, start, end);
free (dates);
free (end);
free (start);
return hd;
}
/* Print out (terminal dashboard) the overall statistics header. */
static void
render_overall_header (WINDOW * win, GHolder * h)
{
char *hd = get_overall_header (h);
int col = getmaxx (stdscr);
draw_header (win, hd, " %s", 0, 0, col, color_panel_header);
free (hd);
}
/* Render the overall statistics. This will attempt to determine the
* right X and Y position given the current values. */
static void
render_overall_statistics (WINDOW * win, Field fields[], size_t n)
{
GColors *color = NULL;
int x_field = 2, x_value = 0;
size_t i, j, k, max_field = 0, max_value = 0, mod_val, y;
for (i = 0, k = 0, y = 2; i < n; i++) {
/* new line every OVERALL_NUM_COLS */
mod_val = k % OVERALL_NUM_COLS;
/* reset position & length and increment row */
if (k > 0 && mod_val == 0) {
max_value = max_field = 0;
x_value = x_field = 2;
y++;
}
/* x pos = max length of field */
x_field += max_field;
color = (*fields[i].colorlbl) ();
render_overall_field (win, fields[i].field, y, x_field, color);
/* get max length of field in the same column */
max_field = 0;
for (j = 0; j < n; j++) {
size_t len = strlen (fields[j].field);
if (j % OVERALL_NUM_COLS == mod_val && len > max_field &&
!fields[j].oneliner)
max_field = len;
}
/* get max length of value in the same column */
max_value = 0;
for (j = 0; j < n; j++) {
size_t len = strlen (fields[j].value);
if (j % OVERALL_NUM_COLS == mod_val && len > max_value &&
!fields[j].oneliner)
max_value = len;
}
/* spacers */
x_value = max_field + x_field + 1;
max_field += max_value + 2;
color = (*fields[i].colorval) ();
render_overall_value (win, fields[i].value, y, x_value, color);
k += fields[i].oneliner ? OVERALL_NUM_COLS : 1;
}
}
/* The entry point to render the overall statistics and free its data. */
void
display_general (WINDOW * win, GLog * glog, GHolder * h)
{
GColors *(*colorlbl) (void) = color_overall_lbls;
GColors *(*colorpth) (void) = color_overall_path;
GColors *(*colorval) (void) = color_overall_vals;
char *ifile = conf.ifile;
size_t n, i;
/* *INDENT-OFF* */
Field fields[] = {
{T_REQUESTS , get_str_processed_reqs (glog) , colorlbl , colorval , 0} ,
{T_UNIQUE_VIS , get_str_visitors () , colorlbl , colorval , 0} ,
{T_UNIQUE_FIL , get_str_reqs () , colorlbl , colorval , 0} ,
{T_REFERRER , get_str_ref_reqs () , colorlbl , colorval , 0} ,
{T_VALID , get_str_valid_reqs (glog) , colorlbl , colorval , 0} ,
{T_GEN_TIME , get_str_proctime () , colorlbl , colorval , 0} ,
{T_STATIC_FIL , get_str_static_reqs () , colorlbl , colorval , 0} ,
{T_LOG , get_str_filesize (glog, ifile) , colorlbl , colorval , 0} ,
{T_FAILED , get_str_failed_reqs (glog) , colorlbl , colorval , 0} ,
{T_EXCLUDE_IP , get_str_excluded_ips (glog) , colorlbl , colorval , 0} ,
{T_UNIQUE404 , get_str_notfound_reqs () , colorlbl , colorval , 0} ,
{T_BW , get_str_bandwidth (glog) , colorlbl , colorval , 0} ,
{T_LOG_PATH , get_str_logfile (glog, ifile) , colorlbl , colorpth , 1}
};
/* *INDENT-ON* */
werase (win);
render_overall_header (win, h);
n = ARRAY_SIZE (fields);
render_overall_statistics (win, fields, n);
for (i = 0; i < n; i++) {
free (fields[i].value);
}
}
/* Implement a basic framework to build a field input.
*
* On success, the inputted string is returned. */
char *
input_string (WINDOW * win, int pos_y, int pos_x, size_t max_width,
const char *str, int enable_case, int *toggle_case)
{
char *s = xmalloc (max_width + 1), *tmp;
size_t i, c, pos = 0, x = 0, quit = 1, len = 0, size_x = 0, size_y = 0;
getmaxyx (win, size_y, size_x);
size_x -= 4;
/* are we setting a default string */
if (str) {
len = MIN (max_width, strlen (str));
memcpy (s, str, len);
s[len] = '\0';
x = pos = 0;
/* is the default str length greater than input field? */
if (strlen (s) > size_x) {
tmp = xstrdup (&s[0]);
tmp[size_x] = '\0';
mvwprintw (win, pos_y, pos_x, "%s", tmp);
free (tmp);
} else {
mvwprintw (win, pos_y, pos_x, "%s", s);
}
} else {
s[0] = '\0';
}
if (enable_case)
mvwprintw (win, size_y - 2, 1, " %s", CSENSITIVE);
wmove (win, pos_y, pos_x + x);
wrefresh (win);
curs_set (1);
while (quit) {
c = wgetch (stdscr);
switch (c) {
case 1: /* ^a */
case 262: /* HOME */
pos = x = 0;
break;
case 5:
case 360: /* END of line */
if (strlen (s) > size_x) {
x = size_x;
pos = strlen (s) - size_x;
} else {
pos = 0;
x = strlen (s);
}
break;
case 7: /* ^g */
case 27: /* ESC */
pos = x = 0;
if (str && *str == '\0')
s[0] = '\0';
quit = 0;
break;
case 9: /* TAB */
if (!enable_case)
break;
*toggle_case = *toggle_case == 0 ? 1 : 0;
if (*toggle_case)
mvwprintw (win, size_y - 2, 1, " %s", CISENSITIVE);
else if (!*toggle_case)
mvwprintw (win, size_y - 2, 1, " %s", CSENSITIVE);
break;
case 21: /* ^u */
s[0] = '\0';
pos = x = 0;
break;
case 8: /* xterm-256color */
case 127:
case KEY_BACKSPACE:
if (pos + x > 0) {
memmove (&s[(pos + x) - 1], &s[pos + x], (max_width - (pos + x)) + 1);
if (pos <= 0)
x--;
else
pos--;
}
break;
case KEY_LEFT:
if (x > 0)
x--;
else if (pos > 0)
pos--;
break;
case KEY_RIGHT:
if ((x + pos) < strlen (s)) {
if (x < size_x)
x++;
else
pos++;
}
break;
case 0x0a:
case 0x0d:
case KEY_ENTER:
quit = 0;
break;
default:
if (strlen (s) == max_width)
break;
if (!isprint (c))
break;
if (strlen (s) == pos) {
s[pos + x] = c;
s[pos + x + 1] = '\0';
waddch (win, c);
} else {
memmove (&s[pos + x + 1], &s[pos + x], strlen (&s[pos + x]) + 1);
s[pos + x] = c;
}
if ((x + pos) < max_width) {
if (x < size_x)
x++;
else
pos++;
}
}
tmp = xstrdup (&s[pos > 0 ? pos : 0]);
tmp[MIN (strlen (tmp), size_x)] = '\0';
for (i = strlen (tmp); i < size_x; i++)
mvwprintw (win, pos_y, pos_x + i, "%s", " ");
mvwprintw (win, pos_y, pos_x, "%s", tmp);
free (tmp);
wmove (win, pos_y, pos_x + x);
wrefresh (win);
}
curs_set (0);
return s;
}
/* Fill the given terminal dashboard menu with user agent data.
*
* On error, the 1 is returned.
* On success, 0 is returned. */
static int
fill_host_agents_gmenu (void *val, void *user_data)
{
GMenu *menu = user_data;
char *agent = ht_get_host_agent_val ((*(int *) val));
if (agent == NULL)
return 1;
menu->items[menu->size].name = agent;
menu->items[menu->size].checked = 0;
menu->size++;
return 0;
}
/* Iterate over the linked-list of user agents for the given host and
* load its data into the given menu. */
static void
load_host_agents_gmenu (void *list, void *user_data, int count)
{
GSLList *lst = list;
GMenu *menu = user_data;
menu->items = (GItem *) xcalloc (count, sizeof (GItem));
list_foreach (lst, fill_host_agents_gmenu, menu);
}
/* Set host data from a linked-list and load its data into a GMenu
* structure.
*
* On error, the 1 is returned.
* On success, 0 is returned. */
#ifdef TCB_BTREE
int
set_host_agents (const char *addr, void (*func) (void *, void *, int),
void *arr)
{
TCLIST *tclist;
GSLList *list;
int key, count = 0;
key = ht_get_keymap (HOSTS, addr);
if (key == 0)
return 1;
tclist = ht_get_host_agent_tclist (HOSTS, key);
if (!tclist)
return 1;
list = tclist_to_gsllist (tclist);
if ((count = list_count (list)) == 0) {
free (list);
return 1;
}
func (list, arr, count);
list_remove_nodes (list);
tclistdel (tclist);
return 0;
}
#endif
/* Set host data from a linked-list and load its data into a GMenu
* structure.
*
* On error, the 1 is returned.
* On success, 0 is returned. */
#ifndef TCB_BTREE
int
set_host_agents (const char *addr, void (*func) (void *, void *, int),
void *arr)
{
GSLList *list;
int data_nkey, count = 0;
data_nkey = ht_get_keymap (HOSTS, addr);
if (data_nkey == 0)
return 1;
list = ht_get_host_agent_list (HOSTS, data_nkey);
if (!list)
return 1;
if ((count = list_count (list)) == 0) {
free (list);
return 1;
}
func (list, arr, count);
#ifdef TCB_MEMHASH
free (list);
#endif
return 0;
}
#endif
/* Render a list of agents if available for the selected host/IP. */
void
load_agent_list (WINDOW * main_win, char *addr)
{
GMenu *menu;
WINDOW *win;
char buf[256];
int c, quit = 1, i;
int y, x, list_h, list_w, menu_w, menu_h;
if (!conf.list_agents)
return;
getmaxyx (stdscr, y, x);
list_h = y / 2; /* list window - height */
list_w = x - 4; /* list window - width */
menu_h = list_h - AGENTS_MENU_Y - 1; /* menu window - height */
menu_w = list_w - AGENTS_MENU_X - AGENTS_MENU_X; /* menu window - width */
win = newwin (list_h, list_w, (y - list_h) / 2, (x - list_w) / 2);
keypad (win, TRUE);
wborder (win, '|', '|', '-', '-', '+', '+', '+', '+');
/* create a new instance of GMenu and make it selectable */
menu = new_gmenu (win, menu_h, menu_w, AGENTS_MENU_Y, AGENTS_MENU_X);
if (set_host_agents (addr, load_host_agents_gmenu, menu) == 1)
goto out;
post_gmenu (menu);
snprintf (buf, sizeof buf, "User Agents for %s", addr);
draw_header (win, buf, " %s", 1, 1, list_w - 2, color_panel_header);
mvwprintw (win, 2, 2, "[UP/DOWN] to scroll - [q] to close window");
wrefresh (win);
while (quit) {
c = wgetch (stdscr);
switch (c) {
case KEY_DOWN:
gmenu_driver (menu, REQ_DOWN);
break;
case KEY_UP:
gmenu_driver (menu, REQ_UP);
break;
case KEY_RESIZE:
case 'q':
quit = 0;
break;
}
wrefresh (win);
}
touchwin (main_win);
close_win (win);
wrefresh (main_win);
out:
/* clean stuff up */
for (i = 0; i < menu->size; ++i)
free (menu->items[i].name);
if (menu->items)
free (menu->items);