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
|
//
// Shell Command database code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
#include "app/shell_command.h"
#include "Fluid.h"
#include "Project.h"
#include "io/Project_Reader.h"
#include "io/Project_Writer.h"
#include "panels/settings_panel.h"
#include "widgets/App_Menu_Bar.h"
#include <FL/Fl_Double_Window.H>
#include <FL/fl_message.H>
#include <FL/fl_string_functions.h>
#include <errno.h>
#include <string.h>
static Fl_Process s_proc;
/**
See if shell command is running (public)
*/
int shell_command_running() {
return s_proc.desc() ? 1 : 0;
}
/** \class Fl_Process
Launch an external shell command.
*/
/**
Create a process manager
*/
Fl_Process::Fl_Process() {
_fpt = 0;
}
/**
Destroy the project manager.
*/
Fl_Process::~Fl_Process() {
if (_fpt) close();
}
/**
Open a process.
\param[in] cmd the shell command that we want to run
\param[in] mode "r" or "w" for creating a stream that can read or write
\return a stream that is redirected from the shell command stdout
*/
FILE * Fl_Process::popen(const char *cmd, const char *mode) {
#if defined(_WIN32) && !defined(__CYGWIN__)
// PRECONDITIONS
if (!mode || !*mode || (*mode!='r' && *mode!='w') ) return 0;
if (_fpt) close(); // close first before reuse
ptmode = *mode;
pin[0] = pin[1] = pout[0] = pout[1] = perr[0] = perr[1] = INVALID_HANDLE_VALUE;
// stderr to stdout wanted ?
int fusion = (strstr(cmd,"2>&1") != 0);
// Create windows pipes
if (!createPipe(pin) || !createPipe(pout) || (!fusion && !createPipe(perr) ) )
return freeHandles(); // error
// Initialize Startup Info
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = pin[0];
si.hStdOutput = pout[1];
si.hStdError = fusion ? pout[1] : perr [1];
if ( CreateProcess(0, (LPTSTR) cmd, 0, 0, TRUE,
DETACHED_PROCESS, 0, 0, &si, &pi)) {
// don't need theses handles inherited by child process:
clean_close(pin[0]); clean_close(pout[1]); clean_close(perr[1]);
HANDLE & h = *mode == 'r' ? pout[0] : pin[1];
_fpt = _fdopen(_open_osfhandle((fl_intptr_t) h,_O_BINARY),mode);
h= INVALID_HANDLE_VALUE; // reset the handle pointer that is shared
// with _fpt so we don't free it twice
}
if (!_fpt) freeHandles();
return _fpt;
#else
_fpt=::popen(cmd,mode);
return _fpt;
#endif
}
/**
Close the current process.
*/
int Fl_Process::close() {
#if defined(_WIN32) && !defined(__CYGWIN__)
if (_fpt) {
fclose(_fpt);
clean_close(perr[0]);
clean_close(pin[1]);
clean_close(pout[0]);
_fpt = 0;
return 0;
}
return -1;
#else
int ret = ::pclose(_fpt);
_fpt = 0;
return ret;
#endif
}
/**
non-null if file is open.
\return the current file descriptor of the process' stdout
*/
FILE *Fl_Process::desc() const {
return _fpt;
}
/**
Receive a single line from the current process.
\param[out] line buffer to receive the line
\param[in] s size of the provided buffer
\return 0 if an error occurred, otherwise a pointer to the string
*/
char *Fl_Process::get_line(char * line, size_t s) const {
return _fpt ? fgets(line, (int)s, _fpt) : 0;
}
// returns fileno(FILE*):
// (file must be open, i.e. _fpt must be non-null)
int Fl_Process::get_fileno() const {
#ifdef _MSC_VER
return _fileno(_fpt); // suppress MSVC warning
#else
return fileno(_fpt);
#endif
}
#if defined(_WIN32) && !defined(__CYGWIN__)
int Fl_Process::createPipe(HANDLE * h, BOOL bInheritHnd) {
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = 0;
sa.bInheritHandle = bInheritHnd;
return CreatePipe (&h[0],&h[1],&sa,0) ? 1 : 0;
}
FILE *Fl_Process::freeHandles() {
clean_close(pin[0]); clean_close(pin[1]);
clean_close(pout[0]); clean_close(pout[1]);
clean_close(perr[0]); clean_close(perr[1]);
return 0; // convenient for error management
}
void Fl_Process::clean_close(HANDLE& h) {
if (h!= INVALID_HANDLE_VALUE) CloseHandle(h);
h = INVALID_HANDLE_VALUE;
}
#endif
/**
Prepare FLUID for running a shell command according to the command flags.
\param[in] flags set various flags to save the project, code, and string before running the command
\return 0 if the previous command is still running
*/
static int prepare_shell_command(int flags) {
if (s_proc.desc()) {
fl_alert("Previous shell command still running!");
return 0;
}
if (flags & Fd_Shell_Command::SAVE_PROJECT) {
Fluid.save_project_file(0);
}
if (flags & Fd_Shell_Command::SAVE_SOURCECODE) {
Fluid.write_code_files(1);
}
if (flags & Fd_Shell_Command::SAVE_STRINGS) {
Fluid.proj.write_strings();
}
return 1;
}
/**
Called by the file handler when the command is finished.
*/
void shell_proc_done() {
shell_run_terminal->append("... END SHELL COMMAND ...\n");
shell_run_button->activate();
shell_run_window->label("FLUID Shell");
fl_beep();
}
void shell_timer_cb(void*) {
if (!s_proc.desc()) {
shell_proc_done();
} else {
Fl::add_timeout(0.25, shell_timer_cb);
}
}
// Support the full piped shell command...
void shell_pipe_cb(FL_SOCKET, void*) {
char line[1024]=""; // Line from command output...
if (s_proc.get_line(line, sizeof(line)) != 0) {
// Add the line to the output list...
shell_run_terminal->append(line);
} else {
// End of file; tell the parent...
Fl::remove_timeout(shell_timer_cb);
Fl::remove_fd(s_proc.get_fileno());
s_proc.close();
shell_proc_done();
}
}
/**
Replace all occurrences of a macro with the given content.
\param[inout] cmd buffer holding the command (must have enough space)
\param[in] cmd_size size of the command buffer
\param[in] macro the macro string to replace (e.g., "@BASENAME@")
\param[in] content the replacement string
*/
static void expand_macro(char *cmd, int cmd_size, const char *macro, const char *content) {
char *pos;
int macro_len;
int content_len;
int tail_len;
if (!cmd || !macro || !content) return;
macro_len = (int)strlen(macro);
content_len = (int)strlen(content);
while ((pos = strstr(cmd, macro)) != 0) {
tail_len = (int)strlen(pos + macro_len);
if ((pos - cmd) + content_len + tail_len >= cmd_size - 1) {
// Not enough space, truncate
break;
}
memmove(pos + content_len, pos + macro_len, tail_len + 1);
memcpy(pos, content, content_len);
}
}
/**
Expand all known macros in the command string.
\param[inout] cmd buffer holding the command
\param[in] cmd_size size of the command buffer
*/
static void expand_macros(char *cmd, int cmd_size) {
char buf[FL_PATH_MAX];
Fluid.proj.basename(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@BASENAME@", buf);
Fluid.proj.projectfile_path(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@PROJECTFILE_PATH@", buf);
{
const char *name = Fluid.proj.projectfile_name();
expand_macro(cmd, cmd_size, "@PROJECTFILE_NAME@", name ? name : "");
}
Fluid.proj.codefile_path(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@CODEFILE_PATH@", buf);
Fluid.proj.codefile_name(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@CODEFILE_NAME@", buf);
Fluid.proj.headerfile_path(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@HEADERFILE_PATH@", buf);
Fluid.proj.headerfile_name(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@HEADERFILE_NAME@", buf);
Fluid.proj.stringsfile_path(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@TEXTFILE_PATH@", buf);
Fluid.proj.stringsfile_name(buf, FL_PATH_MAX);
expand_macro(cmd, cmd_size, "@TEXTFILE_NAME@", buf);
if (strstr(cmd, "@TMPDIR@") != 0) {
const char *tmpdir = Fluid.get_tmpdir();
expand_macro(cmd, cmd_size, "@TMPDIR@", tmpdir ? tmpdir : "");
}
}
/**
Show the terminal window where it was last positioned.
*/
void show_terminal_window() {
Fl_Preferences pos(Fluid.preferences, "shell_run_Window_pos");
int x, y, w, h;
pos.get("x", x, -1);
pos.get("y", y, 0);
pos.get("w", w, 640);
pos.get("h", h, 480);
if (x!=-1) {
shell_run_window->resize(x, y, w, h);
}
shell_run_window->show();
}
#define SHELL_CMD_BUF_SIZE 4096
/**
Prepare for and run a shell command.
\param[in] cmd the command that is sent to `/bin/sh -c ...` or `cmd.exe` on Windows machines
\param[in] flags various flags in preparation of the command
*/
void run_shell_command(const char *cmd, int flags) {
char expanded_cmd[SHELL_CMD_BUF_SIZE];
if (!cmd || !cmd[0]) {
fl_alert("No shell command entered!");
return;
}
if (!prepare_shell_command(flags)) return;
strlcpy(expanded_cmd, cmd, sizeof(expanded_cmd));
expand_macros(expanded_cmd, sizeof(expanded_cmd));
if ( ((flags & Fd_Shell_Command::DONT_SHOW_TERMINAL) == 0)
&& (!shell_run_window->visible()))
{
show_terminal_window();
}
// Show the output window and clear things...
if (flags & Fd_Shell_Command::CLEAR_TERMINAL)
shell_run_terminal->printf("\033[2J\033[H");
if (flags & Fd_Shell_Command::CLEAR_HISTORY)
shell_run_terminal->printf("\033[3J");
shell_run_terminal->scrollbar->value(0);
shell_run_terminal->printf("\033[0;32m%s\033[0m\n", expanded_cmd);
shell_run_window->label(expanded_cmd);
if (s_proc.popen(expanded_cmd, "r") == 0) {
shell_run_terminal->printf("\033[1;31mUnable to run shell command: %s\033[0m\n",
strerror(errno));
shell_run_window->label("FLUID Shell");
return;
}
shell_run_button->deactivate();
// if the function below does not for some reason, we will check periodically
// to see if the command is done
Fl::add_timeout(0.25, shell_timer_cb);
// this will tell us when the shell command is done
Fl::add_fd(s_proc.get_fileno(), shell_pipe_cb);
}
// Helper to duplicate a string safely
static char *dup_str(const char *s) {
return s ? strdup(s) : 0;
}
/**
Create an empty shell command structure.
*/
Fd_Shell_Command::Fd_Shell_Command()
: name(0),
label(0),
shortcut(0),
storage(FLD_TOOL_STORE_USER),
condition(0),
condition_data(0),
command(0),
flags(0),
shell_menu_item_(0)
{
}
/**
Copy the aspects of a shell command dataset into a new shell command.
\param[in] rhs copy from this prototype
*/
Fd_Shell_Command::Fd_Shell_Command(const Fd_Shell_Command *rhs)
: name(dup_str(rhs->name)),
label(dup_str(rhs->label)),
shortcut(rhs->shortcut),
storage(rhs->storage),
condition(rhs->condition),
condition_data(dup_str(rhs->condition_data)),
command(dup_str(rhs->command)),
flags(rhs->flags),
shell_menu_item_(0)
{
}
/**
Create a default storage for a shell command and how it is accessible in FLUID.
\param[in] in_name is used as a stand-in for the command name and label
*/
Fd_Shell_Command::Fd_Shell_Command(const char *in_name)
: name(dup_str(in_name)),
label(dup_str(in_name)),
shortcut(0),
storage(FLD_TOOL_STORE_USER),
condition(Fd_Shell_Command::ALWAYS),
condition_data(0),
command(strdup("echo \"Hello, FLUID!\"")),
flags(Fd_Shell_Command::SAVE_PROJECT|Fd_Shell_Command::SAVE_SOURCECODE),
shell_menu_item_(0)
{
}
/**
Create a storage for a shell command and how it is accessible in FLUID.
*/
Fd_Shell_Command::Fd_Shell_Command(const char *in_name,
const char *in_label,
Fl_Shortcut in_shortcut,
fld::Tool_Store in_storage,
int in_condition,
const char *in_condition_data,
const char *in_command,
int in_flags)
: name(dup_str(in_name)),
label(dup_str(in_label)),
shortcut(in_shortcut),
storage(in_storage),
condition(in_condition),
condition_data(dup_str(in_condition_data)),
command(dup_str(in_command)),
flags(in_flags),
shell_menu_item_(0)
{
}
/**
Destructor - free all allocated strings.
*/
Fd_Shell_Command::~Fd_Shell_Command() {
if (name) free(name);
if (label) free(label);
if (condition_data) free(condition_data);
if (command) free(command);
}
void Fd_Shell_Command::set_name(const char *s) {
if (name) free(name);
name = dup_str(s);
}
void Fd_Shell_Command::set_label(const char *s) {
if (label) free(label);
label = dup_str(s);
}
void Fd_Shell_Command::set_condition_data(const char *s) {
if (condition_data) free(condition_data);
condition_data = dup_str(s);
}
void Fd_Shell_Command::set_command(const char *s) {
if (command) free(command);
command = dup_str(s);
}
/**
Run this command now.
*/
void Fd_Shell_Command::run() {
if (command && command[0])
run_shell_command(command, flags);
}
/**
Update the shell submenu in main menu with the shortcut and a copy of the label.
*/
void Fd_Shell_Command::update_shell_menu() {
if (shell_menu_item_) {
const char *old_label = shell_menu_item_->label(); // can be 0
const char *new_label = label ? label : "";
if (!old_label || strcmp(old_label, new_label)) {
if (old_label) ::free((void*)old_label);
shell_menu_item_->label(fl_strdup(new_label));
}
shell_menu_item_->shortcut(shortcut);
}
}
/**
Check if the set condition is met.
\return 1 if this command appears in the main menu
*/
int Fd_Shell_Command::is_active() {
switch (condition) {
case ALWAYS: return 1;
case NEVER: return 0;
#ifdef _WIN32
case MAC_ONLY: return 0;
case UX_ONLY: return 0;
case WIN_ONLY: return 1;
case MAC_AND_UX_ONLY: return 0;
#elif defined(__APPLE__)
case MAC_ONLY: return 1;
case UX_ONLY: return 0;
case WIN_ONLY: return 0;
case MAC_AND_UX_ONLY: return 1;
#else
case MAC_ONLY: return 0;
case UX_ONLY: return 1;
case WIN_ONLY: return 0;
case MAC_AND_UX_ONLY: return 1;
#endif
case USER_ONLY: return 0; // TODO: get user name
case HOST_ONLY: return 0; // TODO: get host name
case ENV_ONLY: {
const char *value = condition_data ? fl_getenv(condition_data) : 0;
if (value && *value) return 1;
return 0;
}
}
return 0;
}
void Fd_Shell_Command::read(Fl_Preferences &prefs) {
int tmp;
char *str_ptr = 0;
prefs.get("name", str_ptr, "<unnamed>");
set_name(str_ptr);
if (str_ptr) { free(str_ptr); str_ptr = 0; }
prefs.get("label", str_ptr, "<no label>");
set_label(str_ptr);
if (str_ptr) { free(str_ptr); str_ptr = 0; }
prefs.get("shortcut", tmp, 0);
shortcut = (Fl_Shortcut)tmp;
prefs.get("storage", tmp, -1);
if (tmp != -1) storage = (fld::Tool_Store)tmp;
prefs.get("condition", condition, ALWAYS);
prefs.get("condition_data", str_ptr, "");
set_condition_data(str_ptr);
if (str_ptr) { free(str_ptr); str_ptr = 0; }
prefs.get("command", str_ptr, "");
set_command(str_ptr);
if (str_ptr) { free(str_ptr); str_ptr = 0; }
prefs.get("flags", flags, 0);
}
void Fd_Shell_Command::write(Fl_Preferences &prefs, int save_location) {
prefs.set("name", name ? name : "");
prefs.set("label", label ? label : "");
if (shortcut != 0) prefs.set("shortcut", (int)shortcut);
if (save_location) prefs.set("storage", (int)storage);
if (condition != ALWAYS) prefs.set("condition", condition);
if (condition_data && condition_data[0]) prefs.set("condition_data", condition_data);
if (command && command[0]) prefs.set("command", command);
if (flags != 0) prefs.set("flags", flags);
}
void Fd_Shell_Command::read(class fld::io::Project_Reader *in) {
const char *c = in->read_word(1);
if (strcmp(c, "{")!=0) return; // expecting start of group
storage = FLD_TOOL_STORE_PROJECT;
for (;;) {
c = in->read_word(1);
if (strcmp(c, "}")==0) break; // end of command list
else if (strcmp(c, "name")==0)
set_name(in->read_word());
else if (strcmp(c, "label")==0)
set_label(in->read_word());
else if (strcmp(c, "shortcut")==0)
shortcut = in->read_int();
else if (strcmp(c, "condition")==0)
condition = in->read_int();
else if (strcmp(c, "condition_data")==0)
set_condition_data(in->read_word());
else if (strcmp(c, "command")==0)
set_command(in->read_word());
else if (strcmp(c, "flags")==0)
flags = in->read_int();
else
in->read_word(); // skip an unknown word
}
}
void Fd_Shell_Command::write(class fld::io::Project_Writer *out) {
out->write_string("\n command {");
out->write_string("\n name "); out->write_word(name ? name : "");
out->write_string("\n label "); out->write_word(label ? label : "");
if (shortcut) out->write_string("\n shortcut %d", shortcut);
if (condition) out->write_string("\n condition %d", condition);
if (condition_data && condition_data[0]) {
out->write_string("\n condition_data "); out->write_word(condition_data);
}
if (command && command[0]) {
out->write_string("\n command "); out->write_word(command);
}
if (flags) out->write_string("\n flags %d", flags);
out->write_string("\n }");
}
/**
Manage a list of shell commands and their parameters.
*/
Fd_Shell_Command_List::Fd_Shell_Command_List()
: list(0),
list_size(0),
list_capacity(0),
shell_menu_(0)
{
}
/**
Release all shell commands and destroy this class.
*/
Fd_Shell_Command_List::~Fd_Shell_Command_List() {
clear();
}
/**
Return the shell command at the given index.
\param[in] index must be between 0 and list_size-1
\return a pointer to the shell command data
*/
Fd_Shell_Command *Fd_Shell_Command_List::at(int index) const {
return list[index];
}
/**
Clear all shell commands.
*/
void Fd_Shell_Command_List::clear() {
if (list) {
int i;
for (i=0; i<list_size; i++) {
delete list[i];
}
::free(list);
list_size = 0;
list_capacity = 0;
list = 0;
}
}
/**
remove all shell commands of the given storage location from the list.
*/
void Fd_Shell_Command_List::clear(fld::Tool_Store storage) {
int i;
for (i=list_size-1; i>=0; i--) {
if (list[i]->storage == storage) {
remove(i);
}
}
}
/**
Read shell configuration from a preferences group.
*/
void Fd_Shell_Command_List::read(Fl_Preferences &prefs, fld::Tool_Store storage) {
int i, n;
// import the old shell commands from previous user settings
if (&Fluid.preferences == &prefs) {
int version;
prefs.get("shell_commands_version", version, 0);
if (version == 0) {
int save_fl, save_code, save_strings;
Fd_Shell_Command *cmd = new Fd_Shell_Command();
cmd->storage = FLD_TOOL_STORE_USER;
cmd->set_name("Sample Shell Command");
cmd->set_label("Sample Shell Command");
cmd->shortcut = FL_ALT+'g';
char *cmd_str = 0;
Fluid.preferences.get("shell_command", cmd_str, "echo \"Sample Shell Command\"");
if (cmd_str) { cmd->set_command(cmd_str); free(cmd_str); }
Fluid.preferences.get("shell_savefl", save_fl, 1);
Fluid.preferences.get("shell_writecode", save_code, 1);
Fluid.preferences.get("shell_writemsgs", save_strings, 0);
if (save_fl) cmd->flags |= Fd_Shell_Command::SAVE_PROJECT;
if (save_code) cmd->flags |= Fd_Shell_Command::SAVE_SOURCECODE;
if (save_strings) cmd->flags |= Fd_Shell_Command::SAVE_STRINGS;
add(cmd);
}
version = 1;
prefs.set("shell_commands_version", version);
}
Fl_Preferences shell_commands(prefs, "shell_commands");
n = shell_commands.groups();
for (i=0; i<n; i++) {
Fl_Preferences cmd_prefs(shell_commands, Fl_Preferences::Name(i));
Fd_Shell_Command *cmd = new Fd_Shell_Command();
cmd->storage = FLD_TOOL_STORE_USER;
cmd->read(cmd_prefs);
add(cmd);
}
}
/**
Write shell configuration to a preferences group.
*/
void Fd_Shell_Command_List::write(Fl_Preferences &prefs, fld::Tool_Store storage) {
int i, index;
Fl_Preferences shell_commands(prefs, "shell_commands");
shell_commands.delete_all_groups();
index = 0;
for (i=0; i<list_size; i++) {
if (list[i]->storage == FLD_TOOL_STORE_USER) {
Fl_Preferences cmd(shell_commands, Fl_Preferences::Name(index++));
list[i]->write(cmd, 0);
}
}
}
/**
Read shell configuration from a project file.
*/
void Fd_Shell_Command_List::read(fld::io::Project_Reader *in) {
const char *c = in->read_word(1);
if (strcmp(c, "{")!=0) return; // expecting start of group
clear(FLD_TOOL_STORE_PROJECT);
for (;;) {
c = in->read_word(1);
if (strcmp(c, "}")==0) break; // end of command list
else if (strcmp(c, "command")==0) {
Fd_Shell_Command *cmd = new Fd_Shell_Command();
add(cmd);
cmd->read(in);
} else {
in->read_word(); // skip an unknown group
}
}
}
/**
Write shell configuration to a project file.
*/
void Fd_Shell_Command_List::write(fld::io::Project_Writer *out) {
int i, n_in_project_file = 0;
for (i=0; i<list_size; i++) {
if (list[i]->storage == FLD_TOOL_STORE_PROJECT)
n_in_project_file++;
}
if (n_in_project_file > 0) {
out->write_string("\nshell_commands {");
for (i=0; i<list_size; i++) {
if (list[i]->storage == FLD_TOOL_STORE_PROJECT)
list[i]->write(out);
}
out->write_string("\n}");
}
}
/**
Add a previously created shell command to the end of the list.
\param[in] cmd a pointer to the command that we want to add
*/
void Fd_Shell_Command_List::add(Fd_Shell_Command *cmd) {
if (list_size == list_capacity) {
list_capacity += 16;
list = (Fd_Shell_Command**)::realloc(list, list_capacity * sizeof(Fd_Shell_Command*));
}
list[list_size++] = cmd;
}
/**
Insert a newly created shell command at the given position in the list.
\param[in] index must be between 0 and list_size-1
\param[in] cmd a pointer to the command that we want to add
*/
void Fd_Shell_Command_List::insert(int index, Fd_Shell_Command *cmd) {
if (list_size == list_capacity) {
list_capacity += 16;
list = (Fd_Shell_Command**)::realloc(list, list_capacity * sizeof(Fd_Shell_Command*));
}
::memmove(list+index+1, list+index, (list_size-index)*sizeof(Fd_Shell_Command**));
list_size++;
list[index] = cmd;
}
/**
Remove and delete the command at the given index.
\param[in] index must be between 0 and list_size-1
*/
void Fd_Shell_Command_List::remove(int index) {
delete list[index];
list_size--;
::memmove(list+index, list+index+1, (list_size-index)*sizeof(Fd_Shell_Command**));
}
/**
This is called whenever the user clicks a shell command menu in the main menu.
\param[in] u cast tp long to get the index of the shell command
*/
void menu_shell_cmd_cb(Fl_Widget*, void *u) {
long index = (long)(fl_intptr_t)u;
g_shell_config->list[index]->run();
}
/**
This is called when the user selects the menu to edit the shell commands.
It pops up the setting panel at the shell settings tab.
*/
void menu_shell_customize_cb(Fl_Widget*, void*) {
settings_window->show();
w_settings_tabs->value(w_settings_shell_tab);
}
/**
Rebuild the entire shell submenu from scratch and replace the old menu.
*/
void Fd_Shell_Command_List::rebuild_shell_menu() {
static Fl_Menu_Item *shell_submenu = 0;
int i, j, num_active_items;
if (!shell_submenu)
shell_submenu = (Fl_Menu_Item*)Fluid.main_menubar->find_item(menu_marker);
// count the active commands
num_active_items = 0;
for (i=0; i<list_size; i++) {
if (list[i]->is_active()) num_active_items++;
}
// allocate a menu item array
Fl_Menu_Item *mi = (Fl_Menu_Item*)::calloc(num_active_items+2, sizeof(Fl_Menu_Item));
// set the menu item pointer for all active commands
for (i=j=0; i<list_size; i++) {
Fd_Shell_Command *cmd = list[i];
if (cmd->is_active()) {
cmd->shell_menu_item_ = mi + j;
mi[j].callback(menu_shell_cmd_cb);
mi[j].argument(i);
cmd->update_shell_menu();
j++;
}
}
if (j>0) mi[j-1].flags |= FL_MENU_DIVIDER;
mi[j].label(fl_strdup("Customize..."));
mi[j].shortcut(FL_ALT+'x');
mi[j].callback(menu_shell_customize_cb);
// replace the old menu array with the new one
Fl_Menu_Item *mi_old = shell_menu_;
shell_menu_ = mi;
shell_submenu->user_data(shell_menu_);
// free all resources from the old menu
if (mi_old && (mi_old != default_menu)) {
for (i=0; ; i++) {
const char *lbl = mi_old[i].label();
if (!lbl) break;
::free((void*)lbl);
}
::free(mi_old);
}
}
/**
Tell the settings dialog to query this list and update its GUI elements.
*/
void Fd_Shell_Command_List::update_settings_dialog() {
if (w_settings_shell_tab)
w_settings_shell_tab->do_callback(w_settings_shell_tab, LOAD);
}
/**
The default shell submenu in batch mode.
*/
Fl_Menu_Item Fd_Shell_Command_List::default_menu[] = {
{ "Customize...", FL_ALT+'x', menu_shell_customize_cb },
{ 0 }
};
/**
Used to find the shell submenu within the main menu tree.
*/
void Fd_Shell_Command_List::menu_marker(Fl_Widget*, void*) {
// intentionally left empty
}
/**
Export all selected shell commands to an external file.
*/
void Fd_Shell_Command_List::export_selected() {
char path[FL_PATH_MAX];
char preset[FL_PATH_MAX];
if (!g_shell_config || (g_shell_config->list_size == 0)) return;
if (!w_settings_shell_list) return;
Fl_Native_File_Chooser dialog;
dialog.title("Export selected shell commands:");
dialog.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
dialog.filter("FLUID Files\t*.flcmd\n");
Fluid.proj.projectfile_path(path, FL_PATH_MAX);
dialog.directory(path);
Fluid.proj.basename(preset, FL_PATH_MAX);
strlcat(preset, ".flcmd", FL_PATH_MAX);
dialog.preset_file(preset);
if (dialog.show() != 0) return;
Fl_Preferences file(dialog.filename(), "flcmd.fluid.fltk.org", 0, (Fl_Preferences::Root)(Fl_Preferences::C_LOCALE|Fl_Preferences::CLEAR));
Fl_Preferences shell_commands(file, "shell_commands");
int i, index, n;
index = 0;
n = w_settings_shell_list->size();
for (i = 0; i < n; i++) {
if (w_settings_shell_list->selected(i+1)) {
Fl_Preferences cmd(shell_commands, Fl_Preferences::Name(index++));
g_shell_config->list[i]->write(cmd, 1);
}
}
}
/**
Import shell commands from an external file and add them to the list.
*/
void Fd_Shell_Command_List::import_from_file() {
char path[FL_PATH_MAX];
char preset[FL_PATH_MAX];
int i, n;
if (!g_shell_config || (g_shell_config->list_size == 0)) return;
if (!w_settings_shell_list) return;
Fl_Native_File_Chooser dialog;
dialog.title("Import shell commands:");
dialog.type(Fl_Native_File_Chooser::BROWSE_FILE);
dialog.filter("FLUID Files\t*.flcmd\n");
Fluid.proj.projectfile_path(path, FL_PATH_MAX);
dialog.directory(path);
Fluid.proj.basename(preset, FL_PATH_MAX);
strlcat(preset, ".flcmd", FL_PATH_MAX);
dialog.preset_file(preset);
if (dialog.show() != 0) return;
Fl_Preferences file(dialog.filename(), "flcmd.fluid.fltk.org", 0, Fl_Preferences::C_LOCALE);
Fl_Preferences shell_commands(file, "shell_commands");
n = shell_commands.groups();
for (i = 0; i < n; i++) {
Fl_Preferences cmd_prefs(shell_commands, Fl_Preferences::Name(i));
Fd_Shell_Command *cmd = new Fd_Shell_Command();
cmd->storage = FLD_TOOL_STORE_USER;
cmd->read(cmd_prefs);
g_shell_config->add(cmd);
}
w_settings_shell_list->do_callback(w_settings_shell_list, LOAD);
w_settings_shell_cmd->do_callback(w_settings_shell_cmd, LOAD);
w_settings_shell_toolbox->do_callback(w_settings_shell_toolbox, LOAD);
g_shell_config->rebuild_shell_menu();
}
/**
A pointer to the list of shell commands if we are not in batch mode.
*/
Fd_Shell_Command_List *g_shell_config = 0;
|