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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
|
/**
* Copyright (c) 2015, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstring>
#include "views_vtab.hh"
#include <unistd.h>
#include "base/injector.bind.hh"
#include "base/lnav_log.hh"
#include "base/opt_util.hh"
#include "config.h"
#include "lnav.hh"
#include "sql_util.hh"
#include "vtab_module_json.hh"
#include "yajlpp/yajlpp_def.hh"
using namespace lnav::roles::literals;
template<>
struct from_sqlite<lnav_view_t> {
inline lnav_view_t operator()(int argc, sqlite3_value** val, int argi)
{
const char* view_name = (const char*) sqlite3_value_text(val[argi]);
auto view_index_opt = view_from_string(view_name);
if (!view_index_opt) {
throw from_sqlite_conversion_error("lnav view name", argi);
}
return view_index_opt.value();
}
};
template<>
struct from_sqlite<text_filter::type_t> {
inline text_filter::type_t operator()(int argc,
sqlite3_value** val,
int argi)
{
const char* type_name = (const char*) sqlite3_value_text(val[argi]);
if (strcasecmp(type_name, "in") == 0) {
return text_filter::INCLUDE;
}
if (strcasecmp(type_name, "out") == 0) {
return text_filter::EXCLUDE;
}
throw from_sqlite_conversion_error("value of 'in' or 'out'", argi);
}
};
template<>
struct from_sqlite<filter_lang_t> {
inline filter_lang_t operator()(int argc, sqlite3_value** val, int argi)
{
const char* type_name = (const char*) sqlite3_value_text(val[argi]);
if (strcasecmp(type_name, "regex") == 0) {
return filter_lang_t::REGEX;
}
if (strcasecmp(type_name, "sql") == 0) {
return filter_lang_t::SQL;
}
throw from_sqlite_conversion_error("value of 'regex' or 'sql'", argi);
}
};
template<>
struct from_sqlite<std::shared_ptr<lnav::pcre2pp::code>> {
inline std::shared_ptr<lnav::pcre2pp::code> operator()(int argc,
sqlite3_value** val,
int argi)
{
const char* pattern = (const char*) sqlite3_value_text(val[argi]);
if (pattern == nullptr || pattern[0] == '\0') {
throw sqlite_func_error("Expecting a non-empty pattern value");
}
auto compile_res = lnav::pcre2pp::code::from(
string_fragment::from_c_str(pattern), PCRE2_CASELESS);
if (compile_res.isErr()) {
auto ce = compile_res.unwrapErr();
throw sqlite_func_error(
"Invalid regular expression for pattern: {} at offset {}",
ce.get_message().c_str(),
ce.ce_offset);
}
return compile_res.unwrap().to_shared();
}
};
namespace {
static const typed_json_path_container<breadcrumb::possibility>
breadcrumb_possibility_handlers = {
yajlpp::property_handler("display_value")
.for_field(&breadcrumb::possibility::p_display_value,
&attr_line_t::al_string),
};
struct resolved_crumb {
resolved_crumb() = default;
resolved_crumb(std::string display_value,
std::string search_placeholder,
std::vector<breadcrumb::possibility> possibilities)
: rc_display_value(std::move(display_value)),
rc_search_placeholder(std::move(search_placeholder)),
rc_possibilities(std::move(possibilities))
{
}
std::string rc_display_value;
std::string rc_search_placeholder;
std::vector<breadcrumb::possibility> rc_possibilities;
};
static const typed_json_path_container<resolved_crumb> breadcrumb_crumb_handlers
= {
yajlpp::property_handler("display_value")
.for_field(&resolved_crumb::rc_display_value),
yajlpp::property_handler("search_placeholder")
.for_field(&resolved_crumb::rc_search_placeholder),
yajlpp::property_handler("possibilities#")
.for_field(&resolved_crumb::rc_possibilities)
.with_children(breadcrumb_possibility_handlers),
};
struct top_line_meta {
nonstd::optional<std::string> tlm_time;
nonstd::optional<std::string> tlm_file;
nonstd::optional<std::string> tlm_anchor;
std::vector<resolved_crumb> tlm_crumbs;
};
static const typed_json_path_container<top_line_meta> top_line_meta_handlers = {
yajlpp::property_handler("time").for_field(&top_line_meta::tlm_time),
yajlpp::property_handler("file").for_field(&top_line_meta::tlm_file),
yajlpp::property_handler("anchor").for_field(&top_line_meta::tlm_anchor),
yajlpp::property_handler("breadcrumbs#")
.for_field(&top_line_meta::tlm_crumbs)
.with_children(breadcrumb_crumb_handlers),
};
static const typed_json_path_container<line_range> line_range_handlers = {
yajlpp::property_handler("start").for_field(&line_range::lr_start),
yajlpp::property_handler("end").for_field(&line_range::lr_end),
};
static const typed_json_path_container<textview_curses::selected_text_info>
selected_text_handlers = {
yajlpp::property_handler("line").for_field(
&textview_curses::selected_text_info::sti_line),
yajlpp::property_handler("range")
.for_child(&textview_curses::selected_text_info::sti_range)
.with_children(line_range_handlers),
yajlpp::property_handler("value").for_field(
&textview_curses::selected_text_info::sti_value),
};
enum class row_details_t {
hide,
show,
};
enum class word_wrap_t {
none,
normal,
};
struct view_options {
nonstd::optional<row_details_t> vo_row_details;
nonstd::optional<row_details_t> vo_row_time_offset;
nonstd::optional<int32_t> vo_overlay_focus;
nonstd::optional<word_wrap_t> vo_word_wrap;
nonstd::optional<row_details_t> vo_hidden_fields;
bool empty() const
{
return !this->vo_row_details.has_value()
&& !this->vo_row_time_offset.has_value()
&& !this->vo_overlay_focus.has_value()
&& !this->vo_word_wrap.has_value()
&& !this->vo_hidden_fields.has_value();
}
};
static const json_path_handler_base::enum_value_t ROW_DETAILS_ENUM[] = {
{"hide", row_details_t::hide},
{"show", row_details_t::show},
json_path_handler_base::ENUM_TERMINATOR,
};
static const json_path_handler_base::enum_value_t WORD_WRAP_ENUM[] = {
{"none", word_wrap_t::none},
{"normal", word_wrap_t::normal},
json_path_handler_base::ENUM_TERMINATOR,
};
static const typed_json_path_container<view_options> view_options_handlers = {
yajlpp::property_handler("row-details")
.with_enum_values(ROW_DETAILS_ENUM)
.with_description(
"Show or hide the details overlay for the focused row")
.for_field(&view_options::vo_row_details),
yajlpp::property_handler("row-time-offset")
.with_enum_values(ROW_DETAILS_ENUM)
.with_description(
"Show or hide the time-offset from a row to the previous mark")
.for_field(&view_options::vo_row_time_offset),
yajlpp::property_handler("hidden-fields")
.with_enum_values(ROW_DETAILS_ENUM)
.with_description(
"Show or hide fields that have been hidden by the user")
.for_field(&view_options::vo_hidden_fields),
yajlpp::property_handler("overlay-focused-line")
.with_description("The focused line in an overlay")
.for_field(&view_options::vo_overlay_focus),
yajlpp::property_handler("word-wrap")
.with_enum_values(WORD_WRAP_ENUM)
.with_description("How to break long lines")
.for_field(&view_options::vo_word_wrap),
};
struct lnav_views : public tvt_iterator_cursor<lnav_views> {
static constexpr const char* NAME = "lnav_views";
static constexpr const char* CREATE_STMT = R"(
-- Access lnav's views through this table.
CREATE TABLE lnav_views (
name TEXT PRIMARY KEY, -- The name of the view.
top INTEGER, -- The number of the line at the top of the view, starting from zero.
left INTEGER, -- The left position of the viewport.
height INTEGER, -- The height of the viewport.
inner_height INTEGER, -- The number of lines in the view.
top_time DATETIME, -- The time of the top line in the view, if the content is time-based.
top_file TEXT, -- The file the top line is from.
paused INTEGER, -- Indicates if the view is paused and will not load new data.
search TEXT, -- The text to search for in the view.
filtering INTEGER, -- Indicates if the view is applying filters.
movement TEXT, -- The movement mode, either 'top' or 'cursor'.
top_meta TEXT, -- A JSON object that contains metadata related to the top line in the view.
selection INTEGER, -- The number of the line that is focused for selection.
options TEXT, -- A JSON object that contains optional settings for this view.
selected_text TEXT -- A JSON object that contains information about the text selected by the mouse in the view.
);
)";
using iterator = textview_curses*;
iterator begin() { return std::begin(lnav_data.ld_views); }
iterator end() { return std::end(lnav_data.ld_views); }
int get_column(cursor& vc, sqlite3_context* ctx, int col)
{
lnav_view_t view_index = (lnav_view_t) std::distance(
std::begin(lnav_data.ld_views), vc.iter);
const auto& tc = *vc.iter;
unsigned long width;
vis_line_t height;
tc.get_dimensions(height, width);
switch (col) {
case 0:
sqlite3_result_text(
ctx, lnav_view_strings[view_index], -1, SQLITE_STATIC);
break;
case 1:
sqlite3_result_int(ctx, (int) tc.get_top());
break;
case 2:
sqlite3_result_int(ctx, tc.get_left());
break;
case 3:
sqlite3_result_int(ctx, height);
break;
case 4:
sqlite3_result_int(ctx, tc.get_inner_height());
break;
case 5: {
auto* time_source
= dynamic_cast<text_time_translator*>(tc.get_sub_source());
if (time_source != nullptr && tc.get_inner_height() > 0) {
auto top_ri_opt
= time_source->time_for_row(tc.get_selection());
if (top_ri_opt) {
char timestamp[64];
sql_strftime(timestamp,
sizeof(timestamp),
top_ri_opt->ri_time,
' ');
sqlite3_result_text(
ctx, timestamp, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_null(ctx);
}
} else {
sqlite3_result_null(ctx);
}
break;
}
case 6: {
to_sqlite(ctx, tc.map_top_row([](const auto& al) {
return get_string_attr(al.get_attrs(), logline::L_FILE) |
[](const auto wrapper) {
auto lf = wrapper.get();
return nonstd::make_optional(lf->get_filename());
};
}));
break;
}
case 7:
sqlite3_result_int(ctx, tc.is_paused());
break;
case 8:
to_sqlite(ctx, tc.get_current_search());
break;
case 9: {
auto* tss = tc.get_sub_source();
if (tss != nullptr && tss->tss_supports_filtering) {
sqlite3_result_int(ctx, tss->tss_apply_filters);
} else {
sqlite3_result_int(ctx, 0);
}
break;
}
case 10: {
sqlite3_result_text(ctx,
tc.is_selectable() ? "cursor" : "top",
-1,
SQLITE_STATIC);
break;
}
case 11: {
static const size_t MAX_POSSIBILITIES = 128;
if (sqlite3_vtab_nochange(ctx)) {
return SQLITE_OK;
}
auto* tss = tc.get_sub_source();
if (tss != nullptr && tss->text_line_count() > 0) {
auto* time_source = dynamic_cast<text_time_translator*>(
tc.get_sub_source());
auto* ta = dynamic_cast<text_anchors*>(tc.get_sub_source());
std::vector<breadcrumb::crumb> crumbs;
tss->text_crumbs_for_line(tc.get_top(), crumbs);
top_line_meta tlm;
if (time_source != nullptr) {
auto top_ri_opt
= time_source->time_for_row(tc.get_selection());
if (top_ri_opt) {
char timestamp[64];
sql_strftime(timestamp,
sizeof(timestamp),
top_ri_opt->ri_time,
' ');
tlm.tlm_time = timestamp;
}
}
if (ta != nullptr) {
tlm.tlm_anchor = ta->anchor_for_row(tc.get_top());
}
tlm.tlm_file = tc.map_top_row([](const auto& al) {
return get_string_attr(al.get_attrs(), logline::L_FILE)
| [](const auto wrapper) {
auto lf = wrapper.get();
return nonstd::make_optional(
lf->get_filename());
};
});
for (const auto& crumb : crumbs) {
auto poss = crumb.c_possibility_provider();
if (poss.size() > MAX_POSSIBILITIES) {
poss.resize(MAX_POSSIBILITIES);
}
tlm.tlm_crumbs.emplace_back(
crumb.c_display_value.get_string(),
crumb.c_search_placeholder,
std::move(poss));
}
to_sqlite(ctx, top_line_meta_handlers.to_json_string(tlm));
} else {
sqlite3_result_null(ctx);
}
break;
}
case 12:
sqlite3_result_int(ctx, (int) tc.get_selection());
break;
case 13: {
if (sqlite3_vtab_nochange(ctx)) {
return SQLITE_OK;
}
auto* text_accel_p
= dynamic_cast<text_accel_source*>(tc.get_sub_source());
auto vo = view_options{};
vo.vo_word_wrap = tc.get_word_wrap() ? word_wrap_t::normal
: word_wrap_t::none;
vo.vo_hidden_fields = tc.get_hide_fields()
? row_details_t::hide
: row_details_t::show;
if (tc.get_overlay_source()) {
auto ov_sel = tc.get_overlay_selection();
vo.vo_row_details
= tc.get_overlay_source()->get_show_details_in_overlay()
? row_details_t::show
: row_details_t::hide;
if (ov_sel) {
vo.vo_overlay_focus = ov_sel.value();
}
}
if (text_accel_p != nullptr) {
vo.vo_row_time_offset
= text_accel_p->is_time_offset_enabled()
? row_details_t::show
: row_details_t::hide;
}
if (vo.empty()) {
sqlite3_result_null(ctx);
} else {
to_sqlite(ctx, view_options_handlers.to_json_string(vo));
}
break;
}
case 14: {
if (tc.tc_selected_text) {
to_sqlite(ctx,
selected_text_handlers.to_json_string(
tc.tc_selected_text.value()));
} else {
sqlite3_result_null(ctx);
}
break;
}
}
return SQLITE_OK;
}
int delete_row(sqlite3_vtab* tab, sqlite3_int64 rowid)
{
tab->zErrMsg = sqlite3_mprintf(
"Rows cannot be deleted from the lnav_views table");
return SQLITE_ERROR;
}
int insert_row(sqlite3_vtab* tab, sqlite3_int64& rowid_out)
{
tab->zErrMsg = sqlite3_mprintf(
"Rows cannot be inserted into the lnav_views table");
return SQLITE_ERROR;
}
int update_row(sqlite3_vtab* tab,
sqlite3_int64& index,
const char* name,
int64_t top_row,
int64_t left,
int64_t height,
int64_t inner_height,
const char* top_time,
const char* top_file,
bool is_paused,
const char* search,
bool do_filtering,
string_fragment movement,
const char* top_meta,
int64_t selection,
nonstd::optional<string_fragment> options,
nonstd::optional<string_fragment> selected_text)
{
auto& tc = lnav_data.ld_views[index];
auto* time_source
= dynamic_cast<text_time_translator*>(tc.get_sub_source());
auto* text_accel_p
= dynamic_cast<text_accel_source*>(tc.get_sub_source());
view_options vo;
if (options) {
static const intern_string_t OPTIONS_SRC
= intern_string::lookup("options");
auto parse_res = view_options_handlers.parser_for(OPTIONS_SRC)
.of(options.value());
if (parse_res.isErr()) {
auto errmsg = parse_res.unwrapErr();
set_vtable_errmsg(tab, errmsg[0]);
return SQLITE_ERROR;
}
vo = parse_res.unwrap();
}
if (tc.get_top() != top_row) {
log_debug(
"setting top for %s to %d", tc.get_title().c_str(), top_row);
tc.set_top(vis_line_t(top_row));
if (!tc.is_selectable()) {
selection = top_row;
}
} else if (top_time != nullptr && time_source != nullptr) {
date_time_scanner dts;
struct timeval tv;
log_debug("setting top time for %s to %s",
tc.get_title().c_str(),
top_time);
if (dts.convert_to_timeval(top_time, -1, nullptr, tv)) {
auto last_ri_opt
= time_source->time_for_row(tc.get_selection());
if (last_ri_opt) {
auto last_time = last_ri_opt->ri_time;
if (tv != last_time) {
time_source->row_for_time(tv) |
[&tc, &selection](auto row) {
log_debug("setting top for %s to %d from time",
tc.get_title().c_str(),
row);
selection = row;
tc.set_selection(row);
};
if (!tc.is_selectable()) {
selection = tc.get_top();
}
}
} else {
log_warning(" could not get for time top row of %s",
tc.get_title().c_str());
}
} else {
auto um = lnav::console::user_message::error(
attr_line_t("Invalid ")
.append_quoted("top_time"_symbol)
.append(" value"))
.with_reason(
attr_line_t("Unrecognized time value: ")
.append(lnav::roles::string(top_time)));
set_vtable_errmsg(tab, um);
return SQLITE_ERROR;
}
}
if (tc.get_selection() != selection) {
tc.set_selection(vis_line_t(selection));
}
if (top_meta != nullptr) {
static const intern_string_t SQL_SRC
= intern_string::lookup("top_meta");
auto parse_res = top_line_meta_handlers.parser_for(SQL_SRC).of(
string_fragment::from_c_str(top_meta));
if (parse_res.isErr()) {
auto errmsg = parse_res.unwrapErr();
set_vtable_errmsg(tab, errmsg[0]);
return SQLITE_ERROR;
}
auto tlm = parse_res.unwrap();
if (index == LNV_TEXT && tlm.tlm_file) {
if (!lnav_data.ld_text_source.to_front(tlm.tlm_file.value())) {
auto um
= lnav::console::user_message::error(
attr_line_t("Invalid ")
.append_quoted("top_meta.file"_symbol)
.append(" value"))
.with_reason(attr_line_t("Unknown text file: ")
.append(lnav::roles::file(
tlm.tlm_file.value())));
set_vtable_errmsg(tab, um);
return SQLITE_ERROR;
}
}
auto* ta = dynamic_cast<text_anchors*>(tc.get_sub_source());
if (ta != nullptr && tlm.tlm_anchor
&& !tlm.tlm_anchor.value().empty())
{
auto req_anchor = tlm.tlm_anchor.value();
auto req_anchor_top = ta->row_for_anchor(req_anchor);
if (req_anchor_top) {
auto curr_anchor = ta->anchor_for_row(tc.get_top());
if (!curr_anchor || curr_anchor.value() != req_anchor) {
tc.set_selection(req_anchor_top.value());
}
} else {
auto um
= lnav::console::user_message::error(
attr_line_t("Invalid ")
.append_quoted("top_meta.anchor"_symbol)
.append(" value"))
.with_reason(
attr_line_t("Unknown anchor: ")
.append(lnav::roles::symbol(req_anchor)));
set_vtable_errmsg(tab, um);
return SQLITE_ERROR;
}
}
}
if (movement == "top" && tc.is_selectable()) {
tc.set_selectable(false);
} else if (movement == "cursor" && !tc.is_selectable()) {
// First, toggle modes, otherwise get_selection() returns top
tc.set_selectable(true);
auto cur_sel = tc.get_selection();
auto cur_top = tc.get_top();
auto cur_bot = tc.get_bottom() - tc.get_tail_space();
if (cur_sel < cur_top) {
tc.set_selection(cur_top);
} else if (cur_sel > cur_bot) {
tc.set_selection(cur_bot);
}
}
if (vo.vo_row_details && tc.get_overlay_source()) {
tc.get_overlay_source()->set_show_details_in_overlay(
vo.vo_row_details.value() == row_details_t::show);
tc.set_needs_update();
}
if (vo.vo_overlay_focus && tc.get_overlay_source()) {
tc.set_overlay_selection(vis_line_t(vo.vo_overlay_focus.value()));
}
if (vo.vo_word_wrap) {
tc.set_word_wrap(vo.vo_word_wrap.value() == word_wrap_t::normal);
}
if (vo.vo_hidden_fields) {
tc.set_hide_fields(vo.vo_hidden_fields.value()
== row_details_t::hide);
}
if (text_accel_p != nullptr && vo.vo_row_time_offset) {
switch (vo.vo_row_time_offset.value()) {
case row_details_t::show:
text_accel_p->set_time_offset(true);
break;
case row_details_t::hide:
text_accel_p->set_time_offset(false);
break;
}
}
tc.set_left(left);
tc.set_paused(is_paused);
tc.execute_search(search);
auto* tss = tc.get_sub_source();
if (tss != nullptr && tss->tss_supports_filtering
&& tss->tss_apply_filters != do_filtering)
{
tss->tss_apply_filters = do_filtering;
tss->text_filters_changed();
}
return SQLITE_OK;
};
};
struct lnav_view_stack : public tvt_iterator_cursor<lnav_view_stack> {
using iterator = std::vector<textview_curses*>::iterator;
static constexpr const char* NAME = "lnav_view_stack";
static constexpr const char* CREATE_STMT = R"(
-- Access lnav's view stack through this table.
CREATE TABLE lnav_view_stack (
name TEXT
);
)";
iterator begin() { return lnav_data.ld_view_stack.begin(); }
iterator end() { return lnav_data.ld_view_stack.end(); }
int get_column(cursor& vc, sqlite3_context* ctx, int col)
{
textview_curses* tc = *vc.iter;
auto view = lnav_view_t(tc - lnav_data.ld_views);
switch (col) {
case 0:
sqlite3_result_text(
ctx, lnav_view_strings[view], -1, SQLITE_STATIC);
break;
}
return SQLITE_OK;
}
int delete_row(sqlite3_vtab* tab, sqlite3_int64 rowid)
{
if ((size_t) rowid != lnav_data.ld_view_stack.size() - 1) {
tab->zErrMsg = sqlite3_mprintf(
"Only the top view in the stack can be deleted");
return SQLITE_ERROR;
}
lnav_data.ld_last_view = *lnav_data.ld_view_stack.top();
lnav_data.ld_view_stack.pop_back();
clear_preview();
return SQLITE_OK;
}
int insert_row(sqlite3_vtab* tab,
sqlite3_int64& rowid_out,
lnav_view_t view_index)
{
textview_curses* tc = &lnav_data.ld_views[view_index];
ensure_view(tc);
rowid_out = lnav_data.ld_view_stack.size() - 1;
return SQLITE_OK;
}
int update_row(sqlite3_vtab* tab, sqlite3_int64& index)
{
tab->zErrMsg
= sqlite3_mprintf("The lnav_view_stack table cannot be updated");
return SQLITE_ERROR;
}
};
struct lnav_view_filter_base {
struct iterator {
using difference_type = int;
using value_type = text_filter;
using pointer = text_filter*;
using reference = text_filter&;
using iterator_category = std::forward_iterator_tag;
lnav_view_t i_view_index;
int i_filter_index;
iterator(lnav_view_t view = LNV_LOG, int filter = -1)
: i_view_index(view), i_filter_index(filter)
{
}
iterator& operator++()
{
while (this->i_view_index < LNV__MAX) {
const auto& tc = lnav_data.ld_views[this->i_view_index];
auto* tss = tc.get_sub_source();
if (tss == nullptr) {
this->i_view_index = lnav_view_t(this->i_view_index + 1);
continue;
}
const auto& fs = tss->get_filters();
this->i_filter_index += 1;
if (this->i_filter_index >= (ssize_t) fs.size()) {
this->i_filter_index = -1;
this->i_view_index = lnav_view_t(this->i_view_index + 1);
} else {
break;
}
}
return *this;
}
bool operator==(const iterator& other) const
{
return this->i_view_index == other.i_view_index
&& this->i_filter_index == other.i_filter_index;
}
bool operator!=(const iterator& other) const
{
return !(*this == other);
}
};
iterator begin()
{
iterator retval = iterator();
return ++retval;
}
iterator end() { return {LNV__MAX, -1}; }
sqlite_int64 get_rowid(iterator iter)
{
textview_curses& tc = lnav_data.ld_views[iter.i_view_index];
text_sub_source* tss = tc.get_sub_source();
filter_stack& fs = tss->get_filters();
auto& tf = *(fs.begin() + iter.i_filter_index);
sqlite_int64 retval = iter.i_view_index;
retval = retval << 32;
retval = retval | tf->get_index();
return retval;
}
};
struct lnav_view_filters
: public tvt_iterator_cursor<lnav_view_filters>
, public lnav_view_filter_base {
static constexpr const char* NAME = "lnav_view_filters";
static constexpr const char* CREATE_STMT = R"(
-- Access lnav's filters through this table.
CREATE TABLE lnav_view_filters (
view_name TEXT, -- The name of the view.
filter_id INTEGER DEFAULT 0, -- The filter identifier.
enabled INTEGER DEFAULT 1, -- Indicates if the filter is enabled/disabled.
type TEXT DEFAULT 'out', -- The type of filter (i.e. in/out).
language TEXT DEFAULT 'regex', -- The filter language.
pattern TEXT -- The filter pattern.
);
)";
int get_column(cursor& vc, sqlite3_context* ctx, int col)
{
textview_curses& tc = lnav_data.ld_views[vc.iter.i_view_index];
text_sub_source* tss = tc.get_sub_source();
filter_stack& fs = tss->get_filters();
auto tf = *(fs.begin() + vc.iter.i_filter_index);
switch (col) {
case 0:
sqlite3_result_text(ctx,
lnav_view_strings[vc.iter.i_view_index],
-1,
SQLITE_STATIC);
break;
case 1:
to_sqlite(ctx, tf->get_index());
break;
case 2:
sqlite3_result_int(ctx, tf->is_enabled());
break;
case 3:
switch (tf->get_type()) {
case text_filter::INCLUDE:
sqlite3_result_text(ctx, "in", 2, SQLITE_STATIC);
break;
case text_filter::EXCLUDE:
sqlite3_result_text(ctx, "out", 3, SQLITE_STATIC);
break;
default:
ensure(0);
}
break;
case 4:
switch (tf->get_lang()) {
case filter_lang_t::REGEX:
sqlite3_result_text(ctx, "regex", 5, SQLITE_STATIC);
break;
case filter_lang_t::SQL:
sqlite3_result_text(ctx, "sql", 3, SQLITE_STATIC);
break;
default:
ensure(0);
}
break;
case 5:
sqlite3_result_text(
ctx, tf->get_id().c_str(), -1, SQLITE_TRANSIENT);
break;
}
return SQLITE_OK;
}
int insert_row(sqlite3_vtab* tab,
sqlite3_int64& rowid_out,
lnav_view_t view_index,
nonstd::optional<int64_t> _filter_id,
nonstd::optional<bool> enabled,
nonstd::optional<text_filter::type_t> type,
nonstd::optional<filter_lang_t> lang,
sqlite3_value* pattern_str)
{
auto* mod_vt = (vtab_module<lnav_view_filters>::vtab*) tab;
auto& tc = lnav_data.ld_views[view_index];
auto* tss = tc.get_sub_source();
auto& fs = tss->get_filters();
auto filter_index
= lang.value_or(filter_lang_t::REGEX) == filter_lang_t::REGEX
? fs.next_index()
: nonstd::make_optional(size_t{0});
if (!filter_index) {
throw sqlite_func_error("Too many filters");
}
auto conflict_mode = sqlite3_vtab_on_conflict(mod_vt->v_db);
std::shared_ptr<text_filter> tf;
switch (lang.value_or(filter_lang_t::REGEX)) {
case filter_lang_t::REGEX: {
auto pattern
= from_sqlite<std::shared_ptr<lnav::pcre2pp::code>>()(
1, &pattern_str, 0);
auto pf = std::make_shared<pcre_filter>(
type.value_or(text_filter::type_t::EXCLUDE),
pattern->get_pattern(),
*filter_index,
pattern);
auto new_cmd = pf->to_command();
for (auto& filter : fs) {
if (filter->to_command() == new_cmd) {
switch (conflict_mode) {
case SQLITE_FAIL:
case SQLITE_ABORT:
tab->zErrMsg = sqlite3_mprintf(
"filter already exists -- :%s",
new_cmd.c_str());
return conflict_mode;
case SQLITE_IGNORE:
return SQLITE_OK;
case SQLITE_REPLACE:
if (filter->is_enabled() != pf->is_enabled()) {
filter->set_enabled(pf->is_enabled());
tss->text_filters_changed();
tc.set_needs_update();
}
return SQLITE_OK;
default:
break;
}
}
}
fs.add_filter(pf);
tf = pf;
break;
}
case filter_lang_t::SQL: {
if (view_index != LNV_LOG) {
throw sqlite_func_error(
"SQL filters are only supported in the log view");
}
if (lnav_data.ld_log_source.get_sql_filter_text() != "") {
switch (conflict_mode) {
case SQLITE_FAIL:
case SQLITE_ABORT:
tab->zErrMsg = sqlite3_mprintf(
"A SQL expression filter already exists");
return conflict_mode;
case SQLITE_IGNORE:
return SQLITE_OK;
default:
break;
}
}
auto clause = from_sqlite<std::string>()(1, &pattern_str, 0);
auto expr
= fmt::format(FMT_STRING("SELECT 1 WHERE {}"), clause);
auto_mem<sqlite3_stmt> stmt(sqlite3_finalize);
#ifdef SQLITE_PREPARE_PERSISTENT
auto retcode = sqlite3_prepare_v3(lnav_data.ld_db.in(),
expr.c_str(),
expr.size(),
SQLITE_PREPARE_PERSISTENT,
stmt.out(),
nullptr);
#else
auto retcode = sqlite3_prepare_v2(lnav_data.ld_db.in(),
expr.c_str(),
expr.size(),
stmt.out(),
nullptr);
#endif
if (retcode != SQLITE_OK) {
const char* errmsg = sqlite3_errmsg(lnav_data.ld_db);
throw sqlite_func_error("Invalid SQL: {}", errmsg);
}
auto set_res = lnav_data.ld_log_source.set_sql_filter(
clause, stmt.release());
if (set_res.isErr()) {
set_vtable_errmsg(tab, set_res.unwrapErr());
return SQLITE_ERROR;
}
tf = lnav_data.ld_log_source.get_sql_filter().value();
break;
}
default:
ensure(0);
}
if (!enabled.value_or(true)) {
tf->disable();
}
tss->text_filters_changed();
tc.set_needs_update();
return SQLITE_OK;
}
int delete_row(sqlite3_vtab* tab, sqlite3_int64 rowid)
{
auto view_index = lnav_view_t(rowid >> 32);
size_t filter_index = rowid & 0xffffffffLL;
textview_curses& tc = lnav_data.ld_views[view_index];
text_sub_source* tss = tc.get_sub_source();
filter_stack& fs = tss->get_filters();
for (const auto& iter : fs) {
if (iter->get_index() == filter_index) {
fs.delete_filter(iter->get_id());
tss->text_filters_changed();
break;
}
}
tc.set_needs_update();
return SQLITE_OK;
}
int update_row(sqlite3_vtab* tab,
sqlite3_int64& rowid,
lnav_view_t new_view_index,
int64_t new_filter_id,
bool enabled,
text_filter::type_t type,
filter_lang_t lang,
sqlite3_value* pattern_val)
{
auto* mod_vt = (vtab_module<lnav_view_filters>::vtab*) tab;
auto view_index = lnav_view_t(rowid >> 32);
auto filter_index = rowid & 0xffffffffLL;
auto& tc = lnav_data.ld_views[view_index];
auto* tss = tc.get_sub_source();
auto& fs = tss->get_filters();
auto iter = fs.begin();
for (; iter != fs.end(); ++iter) {
if ((*iter)->get_index() == (size_t) filter_index) {
break;
}
}
auto tf = *iter;
if (new_view_index != view_index) {
tab->zErrMsg
= sqlite3_mprintf("The view for a filter cannot be changed");
return SQLITE_ERROR;
}
if (lang == filter_lang_t::SQL && tf->get_index() == 0) {
if (view_index != LNV_LOG) {
throw sqlite_func_error(
"SQL filters are only supported in the log view");
}
auto clause = from_sqlite<std::string>()(1, &pattern_val, 0);
auto expr = fmt::format(FMT_STRING("SELECT 1 WHERE {}"), clause);
auto_mem<sqlite3_stmt> stmt(sqlite3_finalize);
#ifdef SQLITE_PREPARE_PERSISTENT
auto retcode = sqlite3_prepare_v3(lnav_data.ld_db.in(),
expr.c_str(),
expr.size(),
SQLITE_PREPARE_PERSISTENT,
stmt.out(),
nullptr);
#else
auto retcode = sqlite3_prepare_v2(lnav_data.ld_db.in(),
expr.c_str(),
expr.size(),
stmt.out(),
nullptr);
#endif
if (retcode != SQLITE_OK) {
const char* errmsg = sqlite3_errmsg(lnav_data.ld_db);
throw sqlite_func_error("Invalid SQL: {}", errmsg);
}
auto set_res = lnav_data.ld_log_source.set_sql_filter(
clause, stmt.release());
if (set_res.isErr()) {
set_vtable_errmsg(tab, set_res.unwrapErr());
return SQLITE_ERROR;
}
*iter = lnav_data.ld_log_source.get_sql_filter().value();
} else {
tf->lf_deleted = true;
tss->text_filters_changed();
auto pattern = from_sqlite<std::shared_ptr<lnav::pcre2pp::code>>()(
1, &pattern_val, 0);
auto pf = std::make_shared<pcre_filter>(
type, pattern->get_pattern(), tf->get_index(), pattern);
auto conflict_mode = sqlite3_vtab_on_conflict(mod_vt->v_db);
auto new_cmd = pf->to_command();
for (auto& filter : fs) {
if (filter->to_command() == new_cmd) {
switch (conflict_mode) {
case SQLITE_FAIL:
case SQLITE_ABORT:
tab->zErrMsg = sqlite3_mprintf(
"filter already exists -- :%s",
new_cmd.c_str());
return conflict_mode;
case SQLITE_IGNORE:
return SQLITE_OK;
case SQLITE_REPLACE:
if (filter->is_enabled() != pf->is_enabled()) {
filter->set_enabled(pf->is_enabled());
tss->text_filters_changed();
tc.set_needs_update();
}
return SQLITE_OK;
default:
break;
}
}
}
*iter = pf;
}
if (!enabled) {
(*iter)->disable();
}
tss->text_filters_changed();
tc.set_needs_update();
return SQLITE_OK;
}
};
struct lnav_view_filter_stats
: public tvt_iterator_cursor<lnav_view_filter_stats>
, public lnav_view_filter_base {
static constexpr const char* NAME = "lnav_view_filter_stats";
static constexpr const char* CREATE_STMT = R"(
-- Access statistics for filters through this table.
CREATE TABLE lnav_view_filter_stats (
view_name TEXT, -- The name of the view.
filter_id INTEGER, -- The filter identifier.
hits INTEGER -- The number of lines that matched this filter.
);
)";
int get_column(cursor& vc, sqlite3_context* ctx, int col)
{
textview_curses& tc = lnav_data.ld_views[vc.iter.i_view_index];
text_sub_source* tss = tc.get_sub_source();
filter_stack& fs = tss->get_filters();
auto tf = *(fs.begin() + vc.iter.i_filter_index);
switch (col) {
case 0:
sqlite3_result_text(ctx,
lnav_view_strings[vc.iter.i_view_index],
-1,
SQLITE_STATIC);
break;
case 1:
to_sqlite(ctx, tf->get_index());
break;
case 2:
to_sqlite(ctx, tss->get_filtered_count_for(tf->get_index()));
break;
}
return SQLITE_OK;
}
};
struct lnav_view_files : public tvt_iterator_cursor<lnav_view_files> {
static constexpr const char* NAME = "lnav_view_files";
static constexpr const char* CREATE_STMT = R"(
--
CREATE TABLE lnav_view_files (
view_name TEXT, -- The name of the view.
filepath TEXT, -- The path to the file.
visible INTEGER -- Indicates whether or not the file is shown.
);
)";
using iterator = logfile_sub_source::iterator;
struct cursor : public tvt_iterator_cursor<lnav_view_files>::cursor {
explicit cursor(sqlite3_vtab* vt)
: tvt_iterator_cursor<lnav_view_files>::cursor(vt)
{
}
int next()
{
if (this->iter != get_handler().end()) {
do {
++this->iter;
} while (this->iter != get_handler().end()
&& (*this->iter)->get_file_ptr() == nullptr);
}
return SQLITE_OK;
}
};
iterator begin() { return lnav_data.ld_log_source.begin(); }
iterator end() { return lnav_data.ld_log_source.end(); }
int get_column(cursor& vc, sqlite3_context* ctx, int col)
{
auto& ld = *vc.iter;
switch (col) {
case 0:
sqlite3_result_text(
ctx, lnav_view_strings[LNV_LOG], -1, SQLITE_STATIC);
break;
case 1:
to_sqlite(ctx,
ld->ld_filter_state.lfo_filter_state.tfs_logfile
->get_filename());
break;
case 2:
to_sqlite(ctx, ld->ld_visible);
break;
}
return SQLITE_OK;
}
int delete_row(sqlite3_vtab* tab, sqlite3_int64 rowid)
{
tab->zErrMsg = sqlite3_mprintf(
"Rows cannot be deleted from the lnav_view_files table");
return SQLITE_ERROR;
}
int insert_row(sqlite3_vtab* tab, sqlite3_int64& rowid_out)
{
tab->zErrMsg = sqlite3_mprintf(
"Rows cannot be inserted into the lnav_view_files table");
return SQLITE_ERROR;
}
int update_row(sqlite3_vtab* tab,
sqlite3_int64& rowid,
const char* view_name,
const char* file_path,
bool visible)
{
auto& lss = lnav_data.ld_log_source;
auto iter = this->begin();
std::advance(iter, rowid);
auto& ld = *iter;
if (ld->ld_visible != visible) {
ld->get_file_ptr()->set_indexing(visible);
ld->set_visibility(visible);
lss.text_filters_changed();
}
return SQLITE_OK;
}
};
static auto a = injector::bind_multiple<vtab_module_base>()
.add<vtab_module<lnav_views>>()
.add<vtab_module<lnav_view_stack>>()
.add<vtab_module<lnav_view_filters>>()
.add<vtab_module<tvt_no_update<lnav_view_filter_stats>>>()
.add<vtab_module<lnav_view_files>>();
} // namespace
int
register_views_vtab(sqlite3* db)
{
static const char* CREATE_FILTER_VIEW = R"(
CREATE VIEW lnav_view_filters_and_stats AS
SELECT * FROM lnav_view_filters LEFT NATURAL JOIN lnav_view_filter_stats
)";
auto_mem<char> errmsg(sqlite3_free);
if (sqlite3_exec(db, CREATE_FILTER_VIEW, nullptr, nullptr, errmsg.out())
!= SQLITE_OK)
{
log_error("Unable to create filter view: %s", errmsg.in());
}
return 0;
}
|