-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathqscripts.cpp
More file actions
906 lines (793 loc) · 28.3 KB
/
Copy pathqscripts.cpp
File metadata and controls
906 lines (793 loc) · 28.3 KB
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
// Copyright (c) 2019-2026 Elias Bachaalany
// SPDX-License-Identifier: LicenseRef-Human-Origin-Source-1.0
//
// This file is licensed under the Human-Origin Source License v1.0.
// See LICENSE.
/*
Quick execute script: a plugin to speedup IDA scripts development.
This plugin replaces the regular "Recent scripts" and "Execute Script" dialogs and allows you to develop
scripts in your favorite editor and execute them directly in IDA.
(c) Elias Bachaalany <elias.bachaalany@gmail.com>
*/
#include <unordered_map>
#include <string>
#include <regex>
#include <filesystem>
#include <unordered_set>
#pragma warning(push)
#pragma warning(disable: 4267 4244 4146)
#include <loader.hpp>
#include <idp.hpp>
#include <expr.hpp>
#include <prodir.h>
#include <kernwin.hpp>
#include <diskio.hpp>
#include <registry.hpp>
#include <libidacpp/kernwin/kernwin.hpp>
#include <libidacpp/expr/expr.hpp>
#include <libidacpp/text/text.hpp>
using namespace libidacpp::kernwin;
#pragma warning(pop)
// IDA 8.3
#ifndef CH_NOIDB
#define CH_NOIDB CH_UNUSED
#endif
#include "qscripts_utils.h"
#include "script.hpp"
#include "qscripts_expander.h"
#include "qscripts_deps.h"
#include "qscripts_monitor.h"
//-------------------------------------------------------------------------
// Some constants
static constexpr int IDA_MAX_RECENT_SCRIPTS = 512;
static constexpr char IDAREG_RECENT_SCRIPTS[] = "RecentScripts";
//-------------------------------------------------------------------------
// Non-modal scripts chooser
struct qscripts_chooser_t: public plugmod_t, public chooser_t, public monitor_host_t
{
using chooser_t::operator delete;
using chooser_t::operator new;
private:
action_manager_t am;
bool m_b_filemon_timer_active = false;
qtimer_t m_filemon_timer = nullptr;
int opt_change_interval = 500;
int opt_clear_log = 0;
int opt_show_filename = 0;
int opt_exec_unload_func = 0;
int opt_with_undo = 0;
// The file-monitor engine owns the active script + its dependency graph and the
// "what changed -> what to run" tick. This chooser is its host (see the
// monitor_host_t overrides below): the engine calls back here for the few
// IDA-effectful operations (running a script, running a /reload, refreshing,
// logging), and reads/writes its active-script state via m_monitor.active().
script_monitor_t m_monitor { *this };
script_info_t* action_active_script = nullptr;
inline int normalize_filemon_interval(const int change_interval) const
{
return qmax(300, change_interval);
}
const char *get_selected_script_file()
{
return m_monitor.active().file_path.c_str();
}
void clear_selected_script()
{
m_monitor.clear_selected();
}
const bool has_selected_script()
{
return m_monitor.has_selected();
}
bool is_monitor_active() const { return m_b_filemon_timer_active; }
bool is_filemon_timer_installed() const { return m_filemon_timer != nullptr; }
//
// monitor_host_t: the IDA-effectful operations the engine delegates to us.
//
bool exec_script(script_info_t &si, bool with_undo) override
{
return execute_script(&si, with_undo);
}
bool exec_reload(script_info_t &dep, qstring &err) override
{
return execute_reload_directive(dep, err, false);
}
void refresh_view() override
{
refresh_chooser(QSCRIPTS_TITLE);
}
void log(const char *text) override
{
msg("%s", text);
}
bool monitor_active() const override
{
return m_b_filemon_timer_active;
}
void on_selected_cleared() override
{
action_active_script = nullptr;
// ...and deactivate the monitor
activate_monitor(false);
}
bool execute_reload_directive(
script_info_t &dep_script_file,
qstring &err,
bool silent=true)
{
const char *script_file = dep_script_file.file_path.c_str();
do
{
auto ext = get_file_ext(script_file);
extlang_object_t elang(find_extlang_by_ext(ext == nullptr ? "" : ext));
if (elang == nullptr)
{
err.sprnt("unknown script language detected for '%s'!\n", script_file);
break;
}
expand_ctx_t ctx;
ctx.script_file = script_file;
ctx.pkg_base = dep_script_file.pkg_base;
qstring reload_cmd = m_monitor.expand(dep_script_file.reload_cmd, ctx);
if (!elang->eval_snippet(reload_cmd.c_str(), &err))
{
err.sprnt(
"QScripts failed to reload script file: '%s'\n"
"Reload command used: %s\n"
"Error: %s\n",
script_file, reload_cmd.c_str(), err.c_str());
break;
}
return true;
} while (false);
if (!silent)
msg("%s", err.c_str());
return false;
}
bool execute_script(script_info_t *script_info, bool with_undo)
{
if (with_undo)
{
action_active_script = script_info;
auto r = process_ui_action(ACTION_EXECUTE_SCRIPT_WITH_UNDO_ID);
action_active_script = nullptr;
return r;
}
return execute_script_sync(script_info);
}
// Executes a script file
bool execute_script_sync(script_info_t *script_info)
{
bool exec_ok = false;
// Pause the file monitor timer while executing a script
bool old_state = activate_monitor(false);
do
{
auto script_file = script_info->file_path.c_str();
// First things first: always take the file's modification time-stamp first so not to visit it again in the file monitor timer
if (!get_file_modification_time(script_file, &script_info->modified_time))
{
msg("Script file '%s' not found!\n", script_file);
break;
}
const char *script_ext = get_file_ext(script_file);
extlang_object_t elang(nullptr);
if (script_ext == nullptr || (elang = find_extlang_by_ext(script_ext)) == nullptr)
{
msg("Unknown script language detected for '%s'!\n", script_file);
break;
}
if (opt_clear_log)
msg_clear();
// Silently call the unload script function
qstring errbuf;
if (opt_exec_unload_func)
{
idc_value_t result;
elang->call_func(&result, UNLOAD_SCRIPT_FUNC_NAME, &result, 0, &errbuf);
}
if (opt_show_filename)
msg("QScripts executing %s...\n", script_file);
exec_ok = elang->compile_file(
script_file,
#if IDA_SDK_VERSION >= 850
nullptr, // requested_namespace
#endif
&errbuf);
if (!exec_ok)
{
msg("QScripts failed to compile script file: '%s':\n%s", script_file, errbuf.c_str());
break;
}
// Special case for IDC scripts: we have to call 'main'
if (elang->is_idc())
{
idc_value_t result;
exec_ok = elang->call_func(&result, "main", &result, 0, &errbuf);
if (!exec_ok)
{
msg("QScripts failed to run the IDC main() of file '%s':\n%s", script_file, errbuf.c_str());
break;
}
}
} while (false);
activate_monitor(old_state);
return exec_ok;
}
enum
{
OPTID_INTERVAL = 0x0001,
OPTID_CLEARLOG = 0x0002,
OPTID_SHOWNAME = 0x0004,
OPTID_UNLOADEXEC = 0x0008,
OPTID_SELSCRIPT = 0x0010,
OPTID_WITHUNDO = 0x0020,
OPTID_ONLY_SCRIPT = OPTID_SELSCRIPT,
OPTID_ALL_BUT_SCRIPT = 0xffff & ~OPTID_ONLY_SCRIPT,
OPTID_ALL = 0xffff,
};
// Save or load the options
void saveload_options(bool bsave, int what_ids = OPTID_ALL)
{
enum { QSTR = 1000 };
struct options_t
{
int id;
const char *name;
int vtype;
void *pval;
} int_options [] =
{
{OPTID_INTERVAL, "QScripts_interval", VT_LONG, &opt_change_interval},
{OPTID_CLEARLOG, "QScripts_clearlog", VT_LONG, &opt_clear_log},
{OPTID_SHOWNAME, "QScripts_showscriptname", VT_LONG, &opt_show_filename},
{OPTID_UNLOADEXEC, "QScripts_exec_unload_func", VT_LONG, &opt_exec_unload_func},
{OPTID_SELSCRIPT, "QScripts_selected_script_name", QSTR, &m_monitor.active().file_path},
{OPTID_WITHUNDO, "QScripts_with_undo", VT_LONG, &opt_with_undo}
};
for (auto &opt: int_options)
{
if ((what_ids & opt.id) == 0)
continue;
if (opt.vtype == VT_LONG)
{
if (bsave)
reg_write_int(opt.name, *(int *)opt.pval);
else
*(int *)opt.pval = reg_read_int(opt.name, *(int *)opt.pval);
}
else if (opt.vtype == VT_STR)
{
if (bsave)
reg_write_string(opt.name, ((qstring *)opt.pval)->c_str());
else
reg_read_string(((qstring *)opt.pval), opt.name);
}
else if (opt.vtype == QSTR)
{
if (bsave)
{
reg_write_string(opt.name, ((qstring *)opt.pval)->c_str());
}
else
{
qstring tmp;
reg_read_string(&tmp, opt.name);
*((qstring *)opt.pval) = tmp.c_str();
}
}
}
if (!bsave)
opt_change_interval = normalize_filemon_interval(opt_change_interval);
}
static int idaapi s_filemon_timer_cb(void *ud)
{
auto self = (qscripts_chooser_t *)ud;
return self->m_monitor.tick(self->opt_with_undo != 0, self->opt_change_interval);
}
protected:
static constexpr uint32 flags_ =
CH_KEEP | CH_RESTORE | CH_ATTRS | CH_NOIDB |
CH_CAN_DEL | CH_CAN_EDIT | CH_CAN_INS | CH_CAN_REFRESH;
static constexpr int widths_[2] = { 20, 70 };
static constexpr const char *const header_[2] = { "Script", "Path" };
static constexpr const char *ACTION_DEACTIVATE_MONITOR_ID = "qscripts:deactivatemonitor";
static constexpr const char *ACTION_EXECUTE_SELECTED_SCRIPT_ID = "qscripts:execselscript";
static constexpr const char *ACTION_EXECUTE_SCRIPT_WITH_UNDO_ID = "qscripts:execscriptwithundo";
static constexpr const char *ACTION_EXECUTE_NOTEBOOK_ID = "qscripts:executenotebook";
scripts_info_t m_scripts;
ssize_t m_nselected = NO_SELECTION;
static bool is_correct_widget(action_update_ctx_t* ctx)
{
return ctx->widget_title == QSCRIPTS_TITLE;
}
// Add a new script file and properly populate its script info object
// and returns a borrowed reference
const script_info_t *add_script(
const char *script_file,
bool silent = false,
bool unique = true)
{
if (unique)
{
auto p = m_scripts.find({ script_file });
if (p != m_scripts.end())
return &*p;
}
qtime64_t mtime;
if (!get_file_modification_time(script_file, &mtime))
{
if (!silent)
msg("Script file not found: '%s'\n", script_file);
return nullptr;
}
auto &si = m_scripts.push_back();
si.file_path = script_file;
si.modified_time = mtime;
return &si;
}
bool config_dialog()
{
static const char form[] =
"Options\n"
"\n"
"<#Controls the refresh rate of the script change monitor#Script monitor ~i~nterval:D:100:10::>\n"
"<#Clear the output window before re-running the script#C~l~ear the output window:C>\n"
"<#Display the name of the file that is automatically executed#Show ~f~ile name when execution:C>\n"
"<#Execute a function called '__quick_unload_script' before reloading the script#Execute the u~n~load script function:C>\n"
"<#The executed scripts' side effects can be reverted with IDA's Undo#Allow QScripts execution to be ~u~ndo-able:C>>\n"
"\n"
"\n";
// Copy values to the dialog
union
{
ushort n;
struct
{
ushort b_clear_log : 1;
ushort b_show_filename : 1;
ushort b_exec_unload_func : 1;
ushort b_with_undo : 1;
};
} chk_opts;
// Load previous options first (account for multiple instances of IDA)
saveload_options(false);
chk_opts.n = 0;
chk_opts.b_clear_log = opt_clear_log;
chk_opts.b_show_filename = opt_show_filename;
chk_opts.b_exec_unload_func = opt_exec_unload_func;
chk_opts.b_with_undo = opt_with_undo;
sval_t interval = opt_change_interval;
if (ask_form(form, &interval, &chk_opts.n) > 0)
{
// Copy values from the dialog
opt_change_interval = normalize_filemon_interval(int(interval));
opt_clear_log = chk_opts.b_clear_log;
opt_show_filename = chk_opts.b_show_filename;
opt_exec_unload_func = chk_opts.b_exec_unload_func;
opt_with_undo = chk_opts.b_with_undo;
// Save the options directly
saveload_options(true);
return true;
}
return false;
}
const void *get_obj_id(size_t *len) const override
{
// Allow a single instance
*len = sizeof(this);
return (const void *)this;
}
size_t idaapi get_count() const override
{
return m_scripts.size();
}
void idaapi get_row(
qstrvec_t *cols,
int *icon,
chooser_item_attrs_t *attrs,
size_t n) const override
{
auto si = &m_scripts[n];
auto path = si->file_path.c_str();
auto name = strrchr(path, DIRCHAR);
cols->at(0) = name == nullptr ? path : name + 1;
cols->at(1) = path;
if (n == m_nselected)
{
if (is_monitor_active())
{
attrs->flags = CHITEM_BOLD;
*icon = m_monitor.active().is_notebook() ? IDAICONS::NOTEPAD_1 : IDAICONS::KEYBOARD_GRAY;
}
else
{
attrs->flags = CHITEM_ITALIC;
*icon = IDAICONS::RED_DOT;
}
}
else if (is_monitor_active() && m_monitor.active().has_dep(si->file_path) != nullptr)
{
// Mark as a dependency
*icon = IDAICONS::EYE_GLASSES_EDIT;
}
else
{
// Mark as an inactive file
*icon = IDAICONS::GRAY_X_CIRCLE;
}
}
// Activate a script and execute it
cbret_t idaapi enter(size_t n) override
{
bool exec_ok = false;
m_nselected = n;
// Set as the selected script and execute it
m_monitor.set_selected(m_scripts[n].file_path.c_str());
auto &selected_script = m_monitor.active();
if ( selected_script.is_notebook()
&& selected_script.notebook.activation_action == notebook_ctx_t::act_exec_none)
{
// Notebook, execute nothing on activation
msg("Selected notebook: %s\n",
selected_script.notebook.title.c_str());
}
else if ( selected_script.is_notebook()
&& selected_script.notebook.activation_action == notebook_ctx_t::act_exec_all)
{
// Notebook, execute all scripts on activation
msg("Executing all scripts for notebook: %s\n",
selected_script.notebook.title.c_str());
execute_notebook_cells(&selected_script);
}
else
{
exec_ok = execute_script(&selected_script, opt_with_undo);
}
if (exec_ok)
saveload_options(true, OPTID_ONLY_SCRIPT);
// ...and activate the monitor even if the script fails
activate_monitor();
return cbret_t(n, chooser_base_t::ALL_CHANGED);
}
// Add a new script
cbret_t idaapi ins(ssize_t) override
{
qstring filter;
get_browse_scripts_filter(filter);
const char *script_file = ask_file(false, "", "%s", filter.c_str());
if (script_file == nullptr)
return {};
reg_update_strlist(IDAREG_RECENT_SCRIPTS, script_file, IDA_MAX_RECENT_SCRIPTS);
ssize_t idx = build_scripts_list(script_file);
return cbret_t(qmax(idx, 0), chooser_base_t::ALL_CHANGED);
}
// Remove a script from the list
cbret_t idaapi del(size_t n) override
{
auto &script_file = m_scripts[n].file_path;
reg_update_strlist(IDAREG_RECENT_SCRIPTS, nullptr, IDA_MAX_RECENT_SCRIPTS, script_file.c_str());
build_scripts_list();
// Active script removed?
if (m_nselected == NO_SELECTION)
clear_selected_script();
return adjust_last_item(n);
}
// Use it to show the configuration dialog
cbret_t idaapi edit(size_t n) override
{
config_dialog();
return cbret_t(n, chooser_base_t::NOTHING_CHANGED);
}
void idaapi closed() override
{
saveload_options(true);
}
static void get_browse_scripts_filter(qstring &filter)
{
// Collect all installed external languages
auto langs = libidacpp::expr::collect_extlangs(false);
// Build the filter
filter = "FILTER Script files|";
for (auto lang: langs)
filter.cat_sprnt("*.%s;", lang->fileext);
filter.remove_last();
filter.append("|");
// Language specific filters
for (auto lang: langs)
filter.cat_sprnt("%s scripts|*.%s|", lang->name, lang->fileext);
filter.remove_last();
filter.append("\nSelect script file to load");
}
void setup_ui()
{
am.add_action(
AMAHF_NONE,
ACTION_DEACTIVATE_MONITOR_ID,
"QScripts: Deactivate script monitor",
"Ctrl-D",
FO_ACTION_UPDATE([this],
return this->is_correct_widget(ctx) ? AST_ENABLE_FOR_WIDGET : AST_DISABLE_FOR_WIDGET;
),
FO_ACTION_ACTIVATE([this]) {
if (this->is_monitor_active())
{
this->clear_selected_script();
refresh_chooser(QSCRIPTS_TITLE);
}
return 1;
},
nullptr,
IDAICONS::DISABLED);
am.add_action(
AMAHF_NONE,
ACTION_EXECUTE_SELECTED_SCRIPT_ID,
"QScripts: Execute selected script",
"Shift-Enter",
FO_ACTION_UPDATE([this],
return this->is_correct_widget(ctx) ? AST_ENABLE_FOR_WIDGET : AST_DISABLE_FOR_WIDGET;
),
FO_ACTION_ACTIVATE([this]) {
if (!ctx->chooser_selection.empty())
this->execute_script_at(ctx->chooser_selection.at(0));
return 1;
},
"Execute script without activating it",
IDAICONS::FLASH);
am.add_action(
AMAHF_NONE,
ACTION_EXECUTE_SCRIPT_WITH_UNDO_ID,
"QScripts: Execute last active script",
"Alt-Shift-X",
FO_ACTION_UPDATE([this],
return AST_ENABLE_ALWAYS;
),
FO_ACTION_ACTIVATE([this])
{
if (is_monitor_active())
{
auto &selected_script = m_monitor.active();
script_info_t* work_script = nullptr;
std::unique_ptr<active_script_info_t> cell_script;
if (selected_script.is_notebook())
{
if (!selected_script.notebook.last_active_cell.empty())
{
cell_script.reset(new active_script_info_t(selected_script));
if (work_script = cell_script.get())
work_script->file_path = selected_script.notebook.last_active_cell.c_str();
}
}
else if (action_active_script != nullptr)
work_script = action_active_script;
else if (this->has_selected_script())
work_script = &selected_script;
if (work_script != nullptr)
this->execute_script_sync(work_script);
}
return 1;
},
"An action to programmatically execute the active script",
IDAICONS::FLASH);
am.add_action(
AMAHF_NONE,
ACTION_EXECUTE_NOTEBOOK_ID,
"QScripts: Execute all notebook cells",
"",
FO_ACTION_UPDATE([this],
return AST_ENABLE_ALWAYS;
),
FO_ACTION_ACTIVATE([this])
{
if (this->has_selected_script() && m_monitor.active().is_notebook())
this->execute_notebook_cells(&m_monitor.active());
return 1;
},
"An action to programmatically execute the active script",
IDAICONS::NOTEPAD_1);
}
public:
static constexpr const char *QSCRIPTS_TITLE = "QScripts";
qscripts_chooser_t(const char *title_ = QSCRIPTS_TITLE)
: chooser_t(flags_, qnumber(widths_), widths_, header_, title_), am(this)
{
popup_names[POPUP_EDIT] = "~O~ptions";
setup_ui();
saveload_options(false);
}
bool activate_monitor(bool activate = true)
{
bool old = m_b_filemon_timer_active;
m_b_filemon_timer_active = activate;
return old;
}
// Rebuilds the scripts list and returns the index of the `find_script` if needed
ssize_t build_scripts_list(const char *find_script = nullptr)
{
// Remember active script and invalidate its index
bool b_has_selected_script = has_selected_script();
qstring selected_script;
if (b_has_selected_script)
selected_script = get_selected_script_file();
// De-selected the current script in the hope of finding it again in the list
m_nselected = NO_SELECTION;
// Read all scripts
qstrvec_t scripts_list;
reg_read_strlist(&scripts_list, IDAREG_RECENT_SCRIPTS);
// Rebuild the list
ssize_t idx = 0, find_idx = NO_SELECTION;
m_scripts.qclear();
for (auto &script_file: scripts_list)
{
// Restore active script
if (b_has_selected_script && selected_script == script_file)
m_nselected = idx;
// Optionally, find the index of a script by name
if (find_script != nullptr && streq(script_file.c_str(), find_script))
find_idx = idx;
// We skip non-existent scripts
if (add_script(script_file.c_str(), true) != nullptr)
++idx;
}
return find_idx;
}
void execute_last_selected_script(bool with_undo=false)
{
if (has_selected_script())
execute_script(&m_monitor.active(), with_undo);
}
void execute_script_at(ssize_t n)
{
if (n >=0 && n < ssize_t(m_scripts.size()))
execute_script(&m_scripts[n], opt_with_undo);
}
void execute_notebook_cells(active_script_info_t *script)
{
auto& cell_files = script->notebook.cell_files;
active_script_info_t cell_script = *script;
enumerate_files(
script->notebook.base_path,
script->notebook.cells_re,
[this, &cell_script, &cell_files](const std::string& filename)
{
qtime64_t mtime;
get_file_modification_time(filename, &mtime);
cell_files[filename] = mtime;
cell_script.file_path = filename.c_str();
// Execute script and stop enumeration if one cell fails to execute
return this->execute_script_sync(&cell_script);
}
);
}
void show()
{
build_scripts_list();
auto r = choose(m_nselected);
TWidget *widget;
if (r == 0 && (widget = find_widget(QSCRIPTS_TITLE)) != nullptr)
{
attach_action_to_popup(
widget,
nullptr,
ACTION_DEACTIVATE_MONITOR_ID);
attach_action_to_popup(
widget,
nullptr,
ACTION_EXECUTE_SELECTED_SCRIPT_ID);
attach_action_to_popup(
widget,
nullptr,
ACTION_EXECUTE_SCRIPT_WITH_UNDO_ID);
attach_action_to_popup(
widget,
nullptr,
ACTION_EXECUTE_NOTEBOOK_ID);
}
}
bool install_filemon_timer()
{
m_filemon_timer = register_timer(
opt_change_interval,
s_filemon_timer_cb,
this);
return is_filemon_timer_installed();
}
void uninstall_filemon_timer()
{
if (is_filemon_timer_installed())
{
unregister_timer(m_filemon_timer);
m_filemon_timer = nullptr;
}
activate_monitor(false);
}
bool idaapi run(size_t arg) override
{
switch (arg)
{
// Full UI run
case 0:
{
if (!is_filemon_timer_installed())
{
if (!install_filemon_timer())
msg("QScripts: failed to start the file monitor.\n");
else
msg("QScripts: file monitor started successfully.\n");
}
show();
break;
}
// Execute the selected script
case 1:
{
execute_last_selected_script();
break;
}
// Activate the scripts monitor
case 2:
{
activate_monitor(true);
refresh_chooser(QSCRIPTS_TITLE);
break;
}
// Deactivate the scripts monitor
case 3:
{
activate_monitor(false);
refresh_chooser(QSCRIPTS_TITLE);
break;
}
}
return true;
}
virtual ~qscripts_chooser_t()
{
uninstall_filemon_timer();
}
};
//-------------------------------------------------------------------------
plugmod_t *idaapi init(void)
{
auto plg = new qscripts_chooser_t();
if (!plg->install_filemon_timer())
{
if (::debug & IDA_DEBUG_PLUGIN)
msg("QScripts: failed to install the file monitor on startup. Please invoke the UI once to attempt try again!\n");
}
return plg;
}
//--------------------------------------------------------------------------
static const char help[] =
"An alternative scripts manager that lets you develop in an external editor and run them fast in IDA\n"
"\n"
"Just press ENTER on the script to activate it and then go back to your editor to continue development.\n"
"\n"
"Each time you update your script, it will be automatically invoked in IDA\n\n"
"\n"
"QScripts is developed by Elias Bachaalany. Please see https://github.com/0xeb/ida-qscripts for more information\n"
"\n"
"\0"
__DATE__ " " __TIME__ "\n"
"\n";
//--------------------------------------------------------------------------
//
// PLUGIN DESCRIPTION BLOCK
//
//--------------------------------------------------------------------------
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_MULTI | PLUGIN_FIX,
init,
nullptr,
nullptr,
"QScripts: Develop IDA scripts faster in your favorite text editor",
help,
qscripts_chooser_t::QSCRIPTS_TITLE,
#ifdef _DEBUG
"Alt-Shift-A"
#else
"Alt-Shift-F9"
#endif
};