-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
5844 lines (4950 loc) · 132 KB
/
cli.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
//
// Copyright (C) 2019 David Carpenter
//
// 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.
//
// Everything command line interface via IPC.
// TODO:
// an option to calculate the combined filesize of the search results.
// an option to set error level if no results are found -useful for es.exe sonic -n 1 -expect-result
// es sonic -name -path-column -name-color 13 -highlight // -name-color 13 doesn't work with -highlight !
// improve -pause when the window is resized.
// -v to display everything version?
// -path is not working with -r It is using the <"filter"> (-path is adding brackets and double quotes, neither of which regex understands)
// Gather file information if it is not indexed.
// path ellipsis?
// -get-size <filename> - expand -get-run-count to get other information for a single file? -what happens when the file doesn't exist?
// when in pause:
// w - wrap
// n - number
// / - search
// h - help
// r - write
// default "prompt", ":".
// If you want help, :h.
// Save the stream to a file, :r c:\tmp\out.txt
// Maybe a configurable prompt, so the "current" (bottom-most) line, total count of results.
// [23/72]:
// emulate LESS
// *shouldn't we use environment variables for storing switches? like MORE? -registry does make it easy. -using ini now
// custom date/time format.
// NOTES:
// ScrollConsoleScreenBuffer is unuseable because it fills invalidated areas, which causes unnecessary flickering.
//#define ES_VERSION L"1.0.0.0" // initial release
//#define ES_VERSION L"1.0.0.1" // fixed command line interpreting '-' as a switch inside text.
//#define ES_VERSION L"1.0.0.2" // convert unicode to same code page as console.
//#define ES_VERSION L"1.0.0.3" // removed es_write console because it has no support for piping.
//#define ES_VERSION L"1.0.0.4" // added ChangeWindowMessageFilterEx (if available) for admin/user support.
//#define ES_VERSION L"1.1.0.1a" // Everything 1.4.1 support for size, dates etc.
//#define ES_VERSION L"1.1.0.2a" // -pause, -more, -hide-empty-search-results, -empty-search-help, -save-settings, -clear-settings, -path, -parent-path, -parent -no-*switches*, localized dates
//#define ES_VERSION L"1.1.0.3a" // error levels added, -timeout
//#define ES_VERSION L"1.1.0.4a" // -pause improvements, -set-run-count, -inc-run-count, -get-run-count, allow -name -size -date to re-order columns even when they are specified in settings, with -more ENTER=show one line, Q=Quit, ESC=Escape., column alignment improvements.
//#define ES_VERSION L"1.1.0.5a" // /a[RASH] attributes added, -more/-pause ignored for no results, -sort-size, -sort-size-ascending and -sort-size-descending.
//#define ES_VERSION L"1.1.0.6a" // added -csv, -efu, -txt, -m3u and -m3u8 to set the display format (no exporting), added -export-m3u and -export-m3u8.
//#define ES_VERSION L"1.1.0.7" // Everything 1.4 release.
//#define ES_VERSION L"1.1.0.8" // removed -highligh-name, -highligh-path and -highligh-full-path-name. fixed date widths, switched to ini settings.
//#define ES_VERSION L"1.1.0.9" // fixed an issue with checking if the database is loaded, exporting as efu only exports indexed information now, use -size, -date-modified, -date-created or -attributes to override., folder size is also exported now.
//#define ES_VERSION L"1.1.0.10" // -r now takes a parameter, and Everything handles escaping quotes.
//#define ES_VERSION L"1.1.0.11" // added -date-format
//#define ES_VERSION L"1.1.0.12" // added -get-result-count
//#define ES_VERSION L"1.1.0.13" // added -cd -removed -cd -added -ipc1 -ipc2 -output errors to std_error
//#define ES_VERSION L"1.1.0.14" // updated help (thanks to NotNull) and fixed -? -h -help errors
//#define ES_VERSION L"1.1.0.15" // updated help (thanks to NotNull)
//#define ES_VERSION L"1.1.0.16" // added -no-header, added -double-quote, added -version, added -get-everything-version
//#define ES_VERSION L"1.1.0.17" // added -no-result-error
//#define ES_VERSION L"1.1.0.18" // added -get-total-size
#define ES_VERSION L"1.1.0.18"
// errorlevel returned from ES.
#define ES_ERROR_SUCCESS 0 // no known error, search successful.
#define ES_ERROR_REGISTER_WINDOW_CLASS 1 // failed to register window class
#define ES_ERROR_CREATE_WINDOW 2 // failed to create listening window.
#define ES_ERROR_OUT_OF_MEMORY 3 // out of memory
#define ES_ERROR_EXPECTED_SWITCH_PARAMETER 4 // expected an additional command line option with the specified switch
#define ES_ERROR_CREATE_FILE 5 // failed to create export output file
#define ES_ERROR_UNKNOWN_SWITCH 6 // unknown switch.
#define ES_ERROR_SEND_MESSAGE 7 // failed to send Everything IPC a query.
#define ES_ERROR_IPC 8 // NO Everything IPC window.
#define ES_ERROR_NO_RESULTS 9 // No results found. Only set if -no-result-error is used
// compiler options
#pragma warning(disable : 4996) // deprecation
#include <windows.h>
#include <stdio.h>
// IPC Header from the Everything SDK:
#include "..\..\sdk\ipc\everything_ipc.h"
#include "shlwapi.h" // Path functions
#define ES_COPYDATA_IPCTEST_QUERYCOMPLETEW 0
#define ES_COPYDATA_IPCTEST_QUERYCOMPLETE2W 1
#define ES_EXPORT_NONE 0
#define ES_EXPORT_CSV 1
#define ES_EXPORT_EFU 2
#define ES_EXPORT_TXT 3
#define ES_EXPORT_M3U 4
#define ES_EXPORT_M3U8 5
#define ES_BUF_SIZE MAX_PATH
#define ES_EXPORT_BUF_SIZE 65536
#define ES_SEARCH_BUF_SIZE 65536
#define ES_FILTER_BUF_SIZE 65536
#define ES_PAUSE_TEXT "ESC=Quit; Up,Down,Left,Right,Page Up,Page Down,Home,End=Scroll"
#define ES_BLANK_PAUSE_TEXT " "
enum
{
ES_COLUMN_FILENAME,
ES_COLUMN_NAME,
ES_COLUMN_PATH,
ES_COLUMN_HIGHLIGHTED_FILENAME,
ES_COLUMN_HIGHLIGHTED_NAME,
ES_COLUMN_HIGHLIGHTED_PATH,
ES_COLUMN_EXTENSION,
ES_COLUMN_SIZE,
ES_COLUMN_DATE_CREATED,
ES_COLUMN_DATE_MODIFIED,
ES_COLUMN_DATE_ACCESSED,
ES_COLUMN_ATTRIBUTES,
ES_COLUMN_FILE_LIST_FILENAME,
ES_COLUMN_RUN_COUNT,
ES_COLUMN_DATE_RUN,
ES_COLUMN_DATE_RECENTLY_CHANGED,
ES_COLUMN_TOTAL,
};
const wchar_t *es_column_names[] =
{
L"Filename",
L"Name",
L"Path",
L"Filename", // highlighted
L"Name", // highlighted
L"Path", // highlighted
L"Extension",
L"Size",
L"Date Created",
L"Date Modified",
L"Date Accessed",
L"Attributes",
L"File List Filename",
L"Run Count",
L"Date Run",
L"Date Recently Changed",
};
const wchar_t *es_sort_names[] =
{
L"name",
L"path",
L"size",
L"extension",
L"ext",
L"date-created",
L"dc",
L"date-modified",
L"dm",
L"attributes",
L"attribs",
L"attrib",
L"file-list-file-name",
L"run-count",
L"recent-change",
L"date-recently-changed",
L"rc",
L"drc",
L"date-accessed",
L"da",
L"date-run",
L"daterun",
};
#define ES_SORT_NAME_COUNT (sizeof(es_sort_names) / sizeof(const wchar_t *))
const DWORD es_sort_names_to_ids[] =
{
EVERYTHING_IPC_SORT_NAME_ASCENDING,
EVERYTHING_IPC_SORT_PATH_ASCENDING,
EVERYTHING_IPC_SORT_SIZE_DESCENDING,
EVERYTHING_IPC_SORT_EXTENSION_ASCENDING,
EVERYTHING_IPC_SORT_EXTENSION_ASCENDING,
EVERYTHING_IPC_SORT_DATE_CREATED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_CREATED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_MODIFIED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_MODIFIED_DESCENDING,
EVERYTHING_IPC_SORT_ATTRIBUTES_ASCENDING,
EVERYTHING_IPC_SORT_ATTRIBUTES_ASCENDING,
EVERYTHING_IPC_SORT_ATTRIBUTES_ASCENDING,
EVERYTHING_IPC_SORT_FILE_LIST_FILENAME_ASCENDING,
EVERYTHING_IPC_SORT_RUN_COUNT_DESCENDING,
EVERYTHING_IPC_SORT_DATE_RECENTLY_CHANGED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_RECENTLY_CHANGED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_RECENTLY_CHANGED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_RECENTLY_CHANGED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_ACCESSED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_ACCESSED_DESCENDING,
EVERYTHING_IPC_SORT_DATE_RUN_DESCENDING,
EVERYTHING_IPC_SORT_DATE_RUN_DESCENDING,
};
#define MSGFLT_ALLOW 1
typedef struct tagCHANGEFILTERSTRUCT
{
DWORD cbSize;
DWORD ExtStatus;
}CHANGEFILTERSTRUCT, *PCHANGEFILTERSTRUCT;
typedef unsigned __int64 QWORD;
void DECLSPEC_NORETURN es_fatal(int error_code);
void es_write(const wchar_t *text);
void es_fwrite(const wchar_t *text);
void es_write_highlighted(const wchar_t *text);
void es_write_QWORD(QWORD value);
void es_write_DWORD(DWORD value);
int es_wstring_to_int(const wchar_t *s);
int es_wstring_length(const wchar_t *s);
void es_format_number(wchar_t *buf,QWORD number);
int es_sendquery(HWND hwnd);
int es_sendquery2(HWND hwnd);
int es_compare_list_items(const void *a,const void *b);
void es_write_result_count(DWORD result_count);
void es_listresultsW(EVERYTHING_IPC_LIST *list);
void es_listresults2W(EVERYTHING_IPC_LIST2 *list,int index_start,int count);
void es_write_total_size(EVERYTHING_IPC_LIST2 *list,int count);
LRESULT __stdcall es_window_proc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
void es_help(void);
HWND es_find_ipc_window(void);
void es_wstring_copy(wchar_t *buf,const wchar_t *s);
void es_wstring_cat(wchar_t *buf,const wchar_t *s);
int es_check_param(wchar_t *param,const wchar_t *s);
int es_wstring_compare_param(wchar_t *param,const wchar_t *s);
void es_set_sort_ascending(void);
void es_set_sort_descending(void);
int es_find_column(int type);
void es_add_column(int type);
void *es_get_column_data(EVERYTHING_IPC_LIST2 *list,int index,int type);
void es_format_dir(wchar_t *buf);
void es_format_size(wchar_t *buf,QWORD size);
void es_format_filetime(wchar_t *buf,QWORD filetime);
void es_wstring_print_qword(wchar_t *buf,QWORD number);
void es_format_attributes(wchar_t *buf,DWORD attributes);
int es_filetime_to_localtime(SYSTEMTIME *localst,QWORD ft);
void es_format_leading_space(wchar_t *buf,int size,int ch);
void es_space_to_width(wchar_t *buf,int wide);
void es_format_run_count(wchar_t *buf,DWORD run_count);
int es_check_param_param(wchar_t *param,const wchar_t *s);
void es_flush(void);
void *es_alloc(uintptr_t size);
void es_free(void *ptr);
void es_fwrite_csv_string(const wchar_t *s);
const wchar_t *es_get_command_argv(const wchar_t *command_line);
const wchar_t *es_get_argv(const wchar_t *command_line);
const wchar_t *es_skip_ws(const wchar_t *p);
int es_is_ws(const wchar_t c);
void es_set_color(int id);
void es_set_column_wide(int id);
void es_fill(int count,int utf8ch);
void es_wstring_cat_qword(wchar_t *buf,QWORD qw);
int es_is_valid_key(INPUT_RECORD *ir);
void es_ini_write_int(const char *name,int value,const char *filename);
void es_ini_write_string(const char *name,const wchar_t *value,const char *filename);
void es_save_settings(void);
int es_ini_read_int(const char *name,int *pvalue,const char *filename);
int es_ini_read_string(const char *name,wchar_t *pvalue,const char *filename);
void es_load_settings(void);
void es_remove_column(type);
void es_append_filter(const wchar_t *filter);
void es_wbuf_cat(wchar_t *buf,int max,const wchar_t *s);
wchar_t *es_wstring_alloc(const wchar_t *s);
void es_do_run_history_command(void);
int es_check_sorts(void);
void es_get_ini_filename(char *buf);
int es_sort = EVERYTHING_IPC_SORT_NAME_ASCENDING;
int es_sort_ascending = 0; // 0 = default, >0 = ascending, <0 = descending
EVERYTHING_IPC_LIST *es_sort_list;
HMODULE es_user32_hdll = 0;
wchar_t es_instance[ES_BUF_SIZE] = {0};
BOOL (WINAPI *es_pChangeWindowMessageFilterEx)(HWND hWnd,UINT message,DWORD action,PCHANGEFILTERSTRUCT pChangeFilterStruct) = 0;
int es_highlight_color = FOREGROUND_GREEN|FOREGROUND_INTENSITY;
int es_highlight = 0;
int es_match_whole_word = 0;
int es_match_path = 0;
int es_match_case = 0;
int es_match_diacritics = 0;
int es_export = ES_EXPORT_NONE;
HANDLE es_export_file = INVALID_HANDLE_VALUE;
unsigned char *es_export_buf = 0;
unsigned char *es_export_p;
int es_export_remaining = 0;
int es_numcolumns = 0;
int es_columns[ES_COLUMN_TOTAL];
int es_size_leading_zero = 0;
int es_run_count_leading_zero = 0;
int es_digit_grouping = 1;
DWORD es_offset = 0;
DWORD es_max_results = EVERYTHING_IPC_ALLRESULTS;
DWORD es_ret = ES_ERROR_SUCCESS;
wchar_t *es_connect = 0;
wchar_t *es_search = 0;
wchar_t *es_filter = 0;
wchar_t *es_argv = 0;
WORD es_column_color[ES_COLUMN_TOTAL];
WORD es_column_color_is_valid[ES_COLUMN_TOTAL] = {0};
WORD es_last_color;
int es_is_last_color = 0;
int es_size_format = 1; // 0 = auto, 1=bytes, 2=kb
int es_date_format = 0; // 0 = system format, 1=iso-8601 (as local time), 2=filetime in decimal, 3=iso-8601 (in utc)
CHAR_INFO *es_cibuf = 0;
int es_max_wide = 0;
int es_console_wide = 80;
int es_console_high = 25;
int es_console_size_high = 25;
int es_console_window_x = 0;
int es_console_window_y = 0;
int es_pause = 0;
int es_last_page_start = 0;
int es_cibuf_attributes = 0x07;
int es_cibuf_hscroll = 0;
int es_cibuf_x = 0;
int es_cibuf_y = 0;
int es_empty_search_help = 0;
int es_hide_empty_search_results = 0;
int es_save = 0;
HWND es_everything_hwnd = 0;
int es_timeout = 0;
int es_get_result_count = 0; // 1 = show result count only
int es_get_total_size = 0; // 1 = calculate total result size, only display this total size and exit.
int es_no_result_error = 0; // 1 = set errorlevel if no results found.
HANDLE es_output_handle = 0;
int es_output_is_file = 1; // default to file, unless we can get the console mode.
int es_default_attributes = 0x07;
void *es_run_history_data = 0; // run count command
DWORD es_run_history_count = 0; // run count command
int es_run_history_command = 0;
int es_run_history_size = 0;
DWORD es_ipc_version = 0xffffffff;
int es_header = 1;
int es_double_quote = 0; // always use double quotes for filenames.
int es_get_everything_version = 0;
int es_column_widths[ES_COLUMN_TOTAL] =
{
47, // ES_COLUMN_FILENAME,
30, // ES_COLUMN_NAME,
47, // ES_COLUMN_PATH,
47, // ES_COLUMN_HIGHLIGHTED_FILENAME,
30, // ES_COLUMN_HIGHLIGHTED_NAME,
47, // ES_COLUMN_HIGHLIGHTED_PATH,
4, // ES_COLUMN_EXTENSION,
14, // ES_COLUMN_SIZE,
16, // ES_COLUMN_DATE_CREATED,
16, // ES_COLUMN_DATE_MODIFIED,
16, // ES_COLUMN_DATE_ACCESSED,
4, // ES_COLUMN_ATTRIBUTES,
30, // ES_COLUMN_FILE_LIST_FILENAME,
6, // ES_COLUMN_RUN_COUNT,
16, // ES_COLUMN_DATE_RUN,
16, // ES_COLUMN_DATE_RECENTLY_CHANGED,
};
int es_column_is_right_aligned[ES_COLUMN_TOTAL] =
{
0, // ES_COLUMN_FILENAME,
0, // ES_COLUMN_NAME,
0, // ES_COLUMN_PATH,
0, // ES_COLUMN_HIGHLIGHTED_FILENAME,
0, // ES_COLUMN_HIGHLIGHTED_NAME,
0, // ES_COLUMN_HIGHLIGHTED_PATH,
0, // ES_COLUMN_EXTENSION,
1, // ES_COLUMN_SIZE,
0, // ES_COLUMN_DATE_CREATED,
0, // ES_COLUMN_DATE_MODIFIED,
0, // ES_COLUMN_DATE_ACCESSED,
0, // ES_COLUMN_ATTRIBUTES,
0, // ES_COLUMN_FILE_LIST_FILENAME,
1, // ES_COLUMN_RUN_COUNT,
0, // ES_COLUMN_DATE_RUN,
0, // ES_COLUMN_DATE_RECENTLY_CHANGED,
};
// get the length in wchars from a wchar string.
int es_wstring_length(const wchar_t *s)
{
const wchar_t *p;
p = s;
while(*p)
{
p++;
}
return (int)(p - s);
}
// get the length in wchars from highlighted string
// skips over the '*' parts
int es_wstring_highlighted_length(const wchar_t *s)
{
const wchar_t *p;
int len;
len = 0;
p = s;
while(*p)
{
if (*p == '*')
{
p++;
if (*p == '*')
{
len++;
p++;
}
}
else
{
p++;
len++;
}
}
return len;
}
// query everything with search string
int es_sendquery(HWND hwnd)
{
EVERYTHING_IPC_QUERY *query;
int len;
int size;
COPYDATASTRUCT cds;
int ret;
ret = 0;
len = es_wstring_length(es_search);
size = sizeof(EVERYTHING_IPC_QUERY) - sizeof(WCHAR) + len*sizeof(WCHAR) + sizeof(WCHAR);
query = (EVERYTHING_IPC_QUERY *)es_alloc(size);
if (es_get_result_count)
{
es_max_results = 0;
}
query->max_results = es_max_results;
query->offset = 0;
query->reply_copydata_message = ES_COPYDATA_IPCTEST_QUERYCOMPLETEW;
query->search_flags = (es_match_case?EVERYTHING_IPC_MATCHCASE:0) | (es_match_diacritics?EVERYTHING_IPC_MATCHACCENTS:0) | (es_match_whole_word?EVERYTHING_IPC_MATCHWHOLEWORD:0) | (es_match_path?EVERYTHING_IPC_MATCHPATH:0);
query->reply_hwnd = (DWORD)(uintptr_t)hwnd;
CopyMemory(query->search_string,es_search,(len+1)*sizeof(WCHAR));
cds.cbData = size;
cds.dwData = EVERYTHING_IPC_COPYDATAQUERY;
cds.lpData = query;
if (SendMessage(es_everything_hwnd,WM_COPYDATA,(WPARAM)hwnd,(LPARAM)&cds) == TRUE)
{
ret = 1;
}
es_free(query);
return ret;
}
// query everything with search string
int es_sendquery2(HWND hwnd)
{
EVERYTHING_IPC_QUERY2 *query;
int len;
int size;
COPYDATASTRUCT cds;
int ret;
DWORD request_flags;
ret = 0;
len = es_wstring_length(es_search);
size = sizeof(EVERYTHING_IPC_QUERY2) + ((len + 1) * sizeof(WCHAR));
query = (EVERYTHING_IPC_QUERY2 *)es_alloc(size);
request_flags = 0;
{
int columni;
for(columni=0;columni<es_numcolumns;columni++)
{
switch(es_columns[columni])
{
case ES_COLUMN_FILENAME:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_FULL_PATH_AND_NAME;
break;
case ES_COLUMN_NAME:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_NAME;
break;
case ES_COLUMN_PATH:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_PATH;
break;
case ES_COLUMN_HIGHLIGHTED_FILENAME:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_HIGHLIGHTED_FULL_PATH_AND_NAME;
break;
case ES_COLUMN_HIGHLIGHTED_NAME:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_HIGHLIGHTED_NAME;
break;
case ES_COLUMN_HIGHLIGHTED_PATH:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_HIGHLIGHTED_PATH;
break;
case ES_COLUMN_EXTENSION:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_EXTENSION;
break;
case ES_COLUMN_SIZE:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_SIZE;
break;
case ES_COLUMN_DATE_CREATED:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_DATE_CREATED;
break;
case ES_COLUMN_DATE_MODIFIED:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_DATE_MODIFIED;
break;
case ES_COLUMN_DATE_ACCESSED:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_DATE_ACCESSED;
break;
case ES_COLUMN_ATTRIBUTES:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_ATTRIBUTES;
break;
case ES_COLUMN_FILE_LIST_FILENAME:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_FILE_LIST_FILE_NAME;
break;
case ES_COLUMN_RUN_COUNT:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_RUN_COUNT;
break;
case ES_COLUMN_DATE_RUN:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_DATE_RUN;
break;
case ES_COLUMN_DATE_RECENTLY_CHANGED:
request_flags |= EVERYTHING_IPC_QUERY2_REQUEST_DATE_RECENTLY_CHANGED;
break;
}
}
}
if (es_get_result_count)
{
es_max_results = 0;
request_flags = 0;
}
if (es_get_total_size)
{
// we only want size.
request_flags = EVERYTHING_IPC_QUERY2_REQUEST_SIZE;
es_max_results = EVERYTHING_IPC_ALLRESULTS;
}
query->max_results = es_max_results;
query->offset = es_offset;
query->reply_copydata_message = ES_COPYDATA_IPCTEST_QUERYCOMPLETE2W;
query->search_flags = (es_match_case?EVERYTHING_IPC_MATCHCASE:0) | (es_match_diacritics?EVERYTHING_IPC_MATCHACCENTS:0) | (es_match_whole_word?EVERYTHING_IPC_MATCHWHOLEWORD:0) | (es_match_path?EVERYTHING_IPC_MATCHPATH:0);
query->reply_hwnd = (DWORD)(uintptr_t)hwnd;
query->request_flags = request_flags;
query->sort_type = es_sort;
CopyMemory(query+1,es_search,(len + 1) * sizeof(WCHAR));
cds.cbData = size;
cds.dwData = EVERYTHING_IPC_COPYDATA_QUERY2;
cds.lpData = query;
if (SendMessage(es_everything_hwnd,WM_COPYDATA,(WPARAM)hwnd,(LPARAM)&cds) == TRUE)
{
ret = 1;
}
es_free(query);
return ret;
}
int es_compare_list_items(const void *a,const void *b)
{
int i;
i = wcsicmp(EVERYTHING_IPC_ITEMPATH(es_sort_list,a),EVERYTHING_IPC_ITEMPATH(es_sort_list,b));
if (!i)
{
return wcsicmp(EVERYTHING_IPC_ITEMFILENAME(es_sort_list,a),EVERYTHING_IPC_ITEMFILENAME(es_sort_list,b));
}
else
if (i > 0)
{
return 1;
}
else
{
return -1;
}
}
void DECLSPEC_NORETURN es_fatal(int error_code)
{
const char *msg;
int show_help;
msg = 0;
show_help = 0;
switch(error_code)
{
case ES_ERROR_REGISTER_WINDOW_CLASS:
msg = "Error 1: Failed to register window class.\r\n";
break;
case ES_ERROR_CREATE_WINDOW:
msg = "Error 2: Failed to create window.\r\n";
break;
case ES_ERROR_OUT_OF_MEMORY:
msg = "Error 3: Out of memory.\r\n";
break;
case ES_ERROR_EXPECTED_SWITCH_PARAMETER:
msg = "Error 4: Expected switch parameter.\r\n";
// this error is permanent, show help:
show_help = 1;
break;
case ES_ERROR_CREATE_FILE:
msg = "Error 5: Failed to create output file.\r\n";
break;
case ES_ERROR_UNKNOWN_SWITCH:
msg = "Error 6: Unknown switch.\r\n";
// this error is permanent, show help:
show_help = 1;
break;
case ES_ERROR_SEND_MESSAGE:
msg = "Error 7: Unable to send IPC message.\r\n";
break;
case ES_ERROR_IPC:
msg = "Error 8: Everything IPC window not found. Please make sure Everything is running.\r\n";
break;
case ES_ERROR_NO_RESULTS:
msg = "Error 9: No results found.\r\n";
break;
}
if (msg)
{
DWORD numwritten;
// msg is ASCII only.
WriteFile(GetStdHandle(STD_ERROR_HANDLE),msg,(int)strlen(msg),&numwritten,0);
}
if (show_help)
{
es_help();
}
ExitProcess(error_code);
}
void es_write(const wchar_t *text)
{
int wlen;
wlen = es_wstring_length(text);
if (es_cibuf)
{
int i;
for(i=0;i<wlen;i++)
{
if (i + es_cibuf_x >= es_console_wide)
{
break;
}
if (i + es_cibuf_x >= 0)
{
es_cibuf[i+es_cibuf_x].Attributes = es_cibuf_attributes;
es_cibuf[i+es_cibuf_x].Char.UnicodeChar = text[i];
}
}
es_cibuf_x += wlen;
}
else
{
// pipe?
if (es_output_is_file)
{
int len;
len = WideCharToMultiByte(GetConsoleCP(),0,text,wlen,0,0,0,0);
if (len)
{
DWORD numwritten;
char *buf;
buf = es_alloc(len);
WideCharToMultiByte(GetConsoleCP(),0,text,wlen,buf,len,0,0);
WriteFile(es_output_handle,buf,len,&numwritten,0);
es_free(buf);
}
}
else
{
DWORD numwritten;
WriteConsoleW(es_output_handle,text,wlen,&numwritten,0);
}
}
}
void es_fill(int count,int utf8ch)
{
if (es_cibuf)
{
int i;
for(i=0;i<count;i++)
{
if (i + es_cibuf_x >= es_console_wide)
{
break;
}
if (i + es_cibuf_x >= 0)
{
es_cibuf[i+es_cibuf_x].Attributes = es_cibuf_attributes;
es_cibuf[i+es_cibuf_x].Char.UnicodeChar = utf8ch;
}
}
es_cibuf_x += count;
}
else
{
unsigned char *buf;
unsigned char *p;
DWORD numwritten;
int i;
buf = es_alloc(count);
p = buf;
for(i=0;i<count;i++)
{
*p++ = utf8ch;
}
WriteFile(es_output_handle,buf,count,&numwritten,0);
es_free(buf);
}
}
void es_fwrite(const wchar_t *text)
{
if (es_export_file != INVALID_HANDLE_VALUE)
{
int len;
unsigned char stackbuf[ES_BUF_SIZE];
unsigned char *buf;
DWORD numwritten;
int cp;
cp = CP_UTF8;
if (es_export == ES_EXPORT_M3U)
{
cp = CP_ACP;
}
len = WideCharToMultiByte(cp,0,text,-1,0,0,0,0);
if (len < ES_BUF_SIZE)
{
buf = stackbuf;
}
else
{
buf = es_alloc(len);
}
len = WideCharToMultiByte(cp,0,text,-1,buf,ES_BUF_SIZE,0,0);
// remove null from len.
if (len) len--;
if (len > es_export_remaining)
{
es_flush();
if (len >= ES_EXPORT_BUF_SIZE)
{
WriteFile(es_export_file,buf,len,&numwritten,0);
goto copied;
}
}
es_export_remaining -= len;
CopyMemory(es_export_p,buf,len);
es_export_p += len;
copied:
if (buf != stackbuf)
{
es_free(buf);
}
}
else
{
es_write(text);
}
}
void es_fwrite_csv_string(const wchar_t *s)
{
es_fwrite(L"\"");
while(*s)
{
if (*s == '"')
{
// escape double quotes with double double quotes.
es_fwrite(L"\"");
es_fwrite(L"\"");
}
else
{
wchar_t ch[2];
ch[0] = *s;
ch[1] = 0;
es_fwrite(ch);
}
s++;
}
es_fwrite(L"\"");
}
void es_flush(void)
{
if (es_export_file != INVALID_HANDLE_VALUE)
{
if (es_export_remaining != ES_EXPORT_BUF_SIZE)
{
DWORD numwritten;
WriteFile(es_export_file,es_export_buf,ES_EXPORT_BUF_SIZE - es_export_remaining,&numwritten,0);
es_export_p = es_export_buf;
es_export_remaining = ES_EXPORT_BUF_SIZE;
}
}
}
void es_write_highlighted(const wchar_t *text)
{
if (es_cibuf)
{
const wchar_t *p;
int highlighted;
highlighted = 0;
p = text;
while(*p)
{
int i;
const wchar_t *start;
int toggle_highlight;
int wlen;
start = p;
toggle_highlight = 0;
for(;;)
{
if (!*p)
{
wlen = (int)(p-start);
break;
}
if (*p == '*')
{
if (p[1] == '*')
{
// include one literal *
wlen = (int)(p+1-start);
p+=2;
break;
}
wlen = (int)(p-start);
p++;
toggle_highlight = 1;
break;
}
p++;
}
for(i=0;i<wlen;i++)
{
if (i + es_cibuf_x >= es_console_wide)
{
break;
}
if (i + es_cibuf_x >= 0)
{
es_cibuf[i+es_cibuf_x].Attributes = es_cibuf_attributes;
es_cibuf[i+es_cibuf_x].Char.UnicodeChar = start[i];
}
}
es_cibuf_x += wlen;
// skip *
if (toggle_highlight)
{
highlighted = !highlighted;
if (highlighted)
{
es_cibuf_attributes = es_highlight_color;
}
else
{
if (es_is_last_color)
{
es_cibuf_attributes = es_last_color;
}
else
{
es_cibuf_attributes = es_default_attributes;
}
}
}
}
}