/
build.c
2839 lines (2500 loc) · 85.5 KB
/
build.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
/*
* build.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2005 The Geany contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Build commands and menu items.
*/
/* TODO: tidy code:
* Use intermediate pointers for common subexpressions.
* Replace defines with enums.
* Other TODOs in code. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "app.h"
#include "build.h"
#include "dialogs.h"
#include "document.h"
#include "filetypesprivate.h"
#include "geanymenubuttonaction.h"
#include "geanyobject.h"
#include "keybindingsprivate.h"
#include "msgwindow.h"
#include "prefs.h"
#include "projectprivate.h"
#include "sciwrappers.h"
#include "spawn.h"
#include "support.h"
#include "toolbar.h"
#include "ui_utils.h"
#include "utils.h"
#include "vte.h"
#include "win32.h"
#include "gtkcompat.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <glib/gstdio.h>
/* Number of editor indicators to draw - limited as this can affect performance */
#define GEANY_BUILD_ERR_HIGHLIGHT_MAX 50
GeanyBuildInfo build_info = {GEANY_GBG_FT, 0, 0, NULL, GEANY_FILETYPES_NONE, NULL, 0};
static gchar *current_dir_entered = NULL;
typedef struct RunInfo
{
GPid pid;
gint file_type_id;
} RunInfo;
static RunInfo *run_info;
#ifndef G_OS_WIN32
static const gchar RUN_SCRIPT_CMD[] = "geany_run_script_XXXXXX.sh";
#endif
/* Order is important (see GBO_TO_GBG, GBO_TO_CMD below) */
/* * Geany Known Build Commands.
* These commands are named after their default use.
* Only these commands can currently have keybindings.
**/
typedef enum
{
GEANY_GBO_COMPILE, /* *< default compile file */
GEANY_GBO_BUILD, /* *< default build file */
GEANY_GBO_MAKE_ALL, /* *< default make */
GEANY_GBO_CUSTOM, /* *< default make user specified target */
GEANY_GBO_MAKE_OBJECT, /* *< default make object, make %e.o */
GEANY_GBO_EXEC, /* *< default execute ./%e */
GEANY_GBO_COUNT /* *< count of how many */
} GeanyBuildType;
/* * Convert @c GeanyBuildType to @c GeanyBuildGroup.
*
* This macro converts @c GeanyBuildType enum values (the "known" commands)
* to the group they are part of.
*
* @param gbo the @c GeanyBuildType value.
*
* @return the @c GeanyBuildGroup group that @a gbo is in.
*
* Note this is a macro so that it can be used in static initialisers.
**/
#define GBO_TO_GBG(gbo) \
((gbo) > GEANY_GBO_EXEC ? GEANY_GBG_COUNT : \
((gbo) >= GEANY_GBO_EXEC ? GEANY_GBG_EXEC : \
((gbo) >= GEANY_GBO_MAKE_ALL ? GEANY_GBG_NON_FT : GEANY_GBG_FT)))
/* * Convert @c GeanyBuildType to command index.
*
* This macro converts @c GeanyBuildType enum values (the "known" commands)
* to the index within the group.
*
* @param gbo the @c GeanyBuildType value.
*
* @return the index of the @a gbo command in its group.
*
* Note this is a macro so that it can be used in static initialisers.
**/
#define GBO_TO_CMD(gbo) \
((gbo) >= GEANY_GBO_COUNT ? (gbo) - GEANY_GBO_COUNT : \
((gbo) >= GEANY_GBO_EXEC ? (gbo) - GEANY_GBO_EXEC : \
((gbo) >= GEANY_GBO_MAKE_ALL ? \
(gbo) - GEANY_GBO_MAKE_ALL : (gbo))))
/* pack group (<8) and command (<32) into a user_data pointer */
#define GRP_CMD_TO_POINTER(grp, cmd) GUINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
#define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
#define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_UINT(gptr)&0x1f)
#define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_UINT(gptr)&0xe0) >> 5)
static gpointer last_toolbutton_action = GBO_TO_POINTER(GEANY_GBO_BUILD);
static BuildMenuItems menu_items = {NULL, {NULL, NULL, NULL, NULL}};
static struct
{
GtkAction *run_action;
GtkAction *compile_action;
GtkAction *build_action;
GtkWidget *toolmenu;
GtkWidget *toolitem_build;
GtkWidget *toolitem_make_all;
GtkWidget *toolitem_make_custom;
GtkWidget *toolitem_make_object;
GtkWidget *toolitem_set_args;
}
widgets;
static guint build_groups_count[GEANY_GBG_COUNT] = { 3, 4, 2 };
static guint build_items_count = 9;
static void build_exit_cb(GPid pid, gint status, gpointer user_data);
static void build_iofunc(GString *string, GIOCondition condition, gpointer data);
#ifndef G_OS_WIN32
static gchar *build_create_shellscript(const gchar *working_dir, const gchar *cmd, gboolean autoclose, GError **error);
#endif
static void build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir);
static void set_stop_button(gboolean stop);
static void run_exit_cb(GPid child_pid, gint status, gpointer user_data);
static void on_set_build_commands_activate(GtkWidget *w, gpointer u);
static void on_build_next_error(GtkWidget *menuitem, gpointer user_data);
static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data);
static void kill_process(GPid *pid);
static void show_build_result_message(gboolean failure);
static void process_build_output_line(gchar *msg, gint color);
static void show_build_commands_dialog(void);
static void on_build_menu_item(GtkWidget *w, gpointer user_data);
void build_finalize(void)
{
g_free(build_info.dir);
g_free(build_info.custom_target);
if (menu_items.menu != NULL && GTK_IS_WIDGET(menu_items.menu))
gtk_widget_destroy(menu_items.menu);
}
/* note: copied from keybindings.c, may be able to go away */
static void add_menu_accel(GeanyKeyGroup *group, guint kb_id,
GtkAccelGroup *accel_group, GtkWidget *menuitem)
{
GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
if (kb->key != 0)
gtk_widget_add_accelerator(menuitem, "activate", accel_group,
kb->key, kb->mods, GTK_ACCEL_VISIBLE);
}
/* convenience routines to access parts of GeanyBuildCommand */
static gchar *id_to_str(GeanyBuildCommand *bc, gint id)
{
switch (id)
{
case GEANY_BC_LABEL:
return bc->label;
case GEANY_BC_COMMAND:
return bc->command;
case GEANY_BC_WORKING_DIR:
return bc->working_dir;
}
g_assert(0);
return NULL;
}
static void set_command(GeanyBuildCommand *bc, gint id, gchar *str)
{
switch (id)
{
case GEANY_BC_LABEL:
SETPTR(bc->label, str);
break;
case GEANY_BC_COMMAND:
SETPTR(bc->command, str);
break;
case GEANY_BC_WORKING_DIR:
SETPTR(bc->working_dir, str);
break;
default:
g_assert(0);
}
}
static const gchar *config_keys[GEANY_BC_CMDENTRIES_COUNT] = {
"LB", /* label */
"CM", /* command */
"WD" /* working directory */
};
/*-----------------------------------------------------
*
* Execute commands and handle results
*
*-----------------------------------------------------*/
/* the various groups of commands not in the filetype struct */
static GeanyBuildCommand *ft_def = NULL;
static GeanyBuildCommand *non_ft_proj = NULL;
static GeanyBuildCommand *non_ft_pref = NULL;
static GeanyBuildCommand *non_ft_def = NULL;
static GeanyBuildCommand *exec_proj = NULL;
static GeanyBuildCommand *exec_pref = NULL;
static GeanyBuildCommand *exec_def = NULL;
/* and the regexen not in the filetype structure */
static gchar *regex_pref = NULL;
/* project non-fileregex string */
static gchar *regex_proj = NULL;
/* control if build commands are printed by get_build_cmd, for debug purposes only*/
#ifndef PRINTBUILDCMDS
#define PRINTBUILDCMDS FALSE
#endif
static gboolean printbuildcmds = PRINTBUILDCMDS;
/* for debug only, print the commands structures in priority order */
static void printfcmds(void)
{
#if 0
GeanyBuildCommand **cl[GEANY_GBG_COUNT][GEANY_BCS_COUNT] = {
/* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
* GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
{ &ft_def, NULL, NULL, NULL, NULL, NULL },
{ &non_ft_def, NULL, NULL, &non_ft_pref, NULL, &non_ft_proj },
{ &exec_def, NULL, NULL, &exec_pref, NULL, &exec_proj }
};
GeanyFiletype *ft = NULL;
GeanyDocument *doc;
gint i, j, k, l, m;
enum GeanyBuildCmdEntries n;
gint cc[GEANY_BCS_COUNT];
gchar c;
doc = document_get_current();
if (doc != NULL)
ft = doc->file_type;
if (ft != NULL)
{
printf("filetype %s\n",ft->name);
cl[GEANY_GBG_FT][GEANY_BCS_FT] = &(ft->priv->filecmds);
cl[GEANY_GBG_FT][GEANY_BCS_HOME_FT] = &(ft->priv->homefilecmds);
cl[GEANY_GBG_FT][GEANY_BCS_PROJ] = &(ft->priv->projfilecmds);
cl[GEANY_GBG_NON_FT][GEANY_BCS_FT] = &(ft->priv->ftdefcmds);
cl[GEANY_GBG_EXEC][GEANY_BCS_FT] = &(ft->priv->execcmds);
cl[GEANY_GBG_EXEC][GEANY_BCS_HOME_FT] = &(ft->priv->homeexeccmds);
cl[GEANY_GBG_EXEC][GEANY_BCS_PROJ_FT] = &(ft->priv->projexeccmds);
}
for (i = 0; i < GEANY_BCS_COUNT; ++i)
{
m = 1;
for (j = 0; j < GEANY_GBG_COUNT; ++j)
{
for (k = 0; k < build_groups_count[j]; ++k)
if (cl[j][i] != NULL && *(cl[j][i]) != NULL && (*(cl[j][i]))[k].exists)
{
for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
{
if ((*(cl[j][i]))[k].entries[n] != NULL &&
(l = strlen((*(cl[j][i]))[k].entries[n])) > m)
{
m = l;
}
}
}
}
cc[i] = m;
}
for (i = 0; i < GEANY_GBG_COUNT; ++i)
{
for (k = 0; k < build_groups_count[i]; ++k)
{
for (l = 0; l < 2; ++l)
{
c = ' ';
for (j = 0; j < GEANY_BCS_COUNT; ++j)
{
if (cl[i][j] != NULL && *(cl[i][j]) != NULL && (*(cl[i][j]))[k].exists)
{
for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
{
if ((*(cl[i][j]))[k].entries[i] != NULL)
printf("%c %*.*s",c,cc[j],cc[j],(*(cl[i][j]))[k].entries[i]);
else
printf("%c %*.*s",c,cc[j],cc[j]," ");
}
}
else
printf("%c %*.*s",c,cc[j],cc[j]," ");
c = ',';
}
printf("\n");
}
}
printf("\n");
}
#endif
}
/* macros to save typing and make the logic visible */
#define return_cmd_if(src, cmds)\
if (cmds != NULL && cmds[cmdindex].exists && below>src)\
{ \
*fr=src; \
if (printbuildcmds) \
printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
return &(cmds[cmdindex]); \
}
#define return_ft_cmd_if(src, cmds)\
if (ft != NULL && ft->priv->cmds != NULL \
&& ft->priv->cmds[cmdindex].exists && below>src)\
{ \
*fr=src; \
if (printbuildcmds) \
printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
return &(ft->priv->cmds[cmdindex]); \
}
/* get the next lowest command taking priority into account */
static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, guint cmdgrp, guint cmdindex,
guint below, guint *from)
{
/* Note: parameter below used in macros above */
GeanyFiletype *ft = NULL;
guint sink, *fr = &sink;
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
if (printbuildcmds)
printfcmds();
if (cmdgrp >= GEANY_GBG_COUNT)
return NULL;
if (from != NULL)
fr = from;
if (doc == NULL)
doc = document_get_current();
if (doc != NULL)
ft = doc->file_type;
switch (cmdgrp)
{
case GEANY_GBG_FT: /* order proj ft, home ft, ft, defft */
if (ft != NULL)
{
return_ft_cmd_if(GEANY_BCS_PROJ, projfilecmds);
return_ft_cmd_if(GEANY_BCS_PREF, homefilecmds);
return_ft_cmd_if(GEANY_BCS_FT, filecmds);
}
return_cmd_if(GEANY_BCS_DEF, ft_def);
break;
case GEANY_GBG_NON_FT: /* order proj, pref, def */
return_cmd_if(GEANY_BCS_PROJ, non_ft_proj);
return_cmd_if(GEANY_BCS_PREF, non_ft_pref);
return_ft_cmd_if(GEANY_BCS_FT, ftdefcmds);
return_cmd_if(GEANY_BCS_DEF, non_ft_def);
break;
case GEANY_GBG_EXEC: /* order proj, proj ft, pref, home ft, ft, def */
return_cmd_if(GEANY_BCS_PROJ, exec_proj);
return_ft_cmd_if(GEANY_BCS_PROJ_FT, projexeccmds);
return_cmd_if(GEANY_BCS_PREF, exec_pref);
return_ft_cmd_if(GEANY_BCS_FT, homeexeccmds);
return_ft_cmd_if(GEANY_BCS_FT, execcmds);
return_cmd_if(GEANY_BCS_DEF, exec_def);
break;
default:
break;
}
return NULL;
}
/* shortcut to start looking at the top */
static GeanyBuildCommand *get_build_cmd(GeanyDocument *doc, guint grp, guint cmdindex, guint *from)
{
return get_next_build_cmd(doc, grp, cmdindex, GEANY_BCS_COUNT, from);
}
#define return_nonblank_regex(src, ptr)\
if (!EMPTY(ptr)) \
{ *fr = (src); return &(ptr); }
/* like get_build_cmd, but for regexen, used by filetypes */
gchar **build_get_regex(GeanyBuildGroup grp, GeanyFiletype *ft, guint *from)
{
guint sink, *fr = &sink;
if (from != NULL)
fr = from;
if (grp == GEANY_GBG_FT)
{
if (ft == NULL)
{
GeanyDocument *doc = document_get_current();
if (doc != NULL)
ft = doc->file_type;
}
if (ft == NULL)
return NULL;
return_nonblank_regex(GEANY_BCS_PROJ, ft->priv->projerror_regex_string);
return_nonblank_regex(GEANY_BCS_HOME_FT, ft->priv->homeerror_regex_string);
return_nonblank_regex(GEANY_BCS_FT, ft->error_regex_string);
}
else if (grp == GEANY_GBG_NON_FT)
{
return_nonblank_regex(GEANY_BCS_PROJ, regex_proj);
return_nonblank_regex(GEANY_BCS_PREF, regex_pref);
}
return NULL;
}
static GeanyBuildCommand **get_build_group_pointer(const GeanyBuildSource src, const GeanyBuildGroup grp)
{
GeanyDocument *doc;
GeanyFiletype *ft = NULL;
switch (grp)
{
case GEANY_GBG_FT:
if ((doc = document_get_current()) == NULL)
return NULL;
if ((ft = doc->file_type) == NULL)
return NULL;
switch (src)
{
case GEANY_BCS_DEF: return &(ft->priv->ftdefcmds);
case GEANY_BCS_FT: return &(ft->priv->filecmds);
case GEANY_BCS_HOME_FT: return &(ft->priv->homefilecmds);
case GEANY_BCS_PREF: return &(ft->priv->homefilecmds);
case GEANY_BCS_PROJ: return &(ft->priv->projfilecmds);
default: return NULL;
}
break;
case GEANY_GBG_NON_FT:
switch (src)
{
case GEANY_BCS_DEF: return &(non_ft_def);
case GEANY_BCS_PREF: return &(non_ft_pref);
case GEANY_BCS_PROJ: return &(non_ft_proj);
default: return NULL;
}
break;
case GEANY_GBG_EXEC:
if ((doc = document_get_current()) != NULL)
ft = doc->file_type;
switch (src)
{
case GEANY_BCS_DEF: return &(exec_def);
case GEANY_BCS_FT: return ft ? &(ft->priv->execcmds): NULL;
case GEANY_BCS_HOME_FT: return ft ? &(ft->priv->homeexeccmds): NULL;
case GEANY_BCS_PROJ_FT: return ft ? &(ft->priv->projexeccmds): NULL;
case GEANY_BCS_PREF: return &(exec_pref);
case GEANY_BCS_PROJ: return &(exec_proj);
default: return NULL;
}
break;
default:
return NULL;
}
}
/* get pointer to the command group array */
static GeanyBuildCommand *get_build_group(const GeanyBuildSource src, const GeanyBuildGroup grp)
{
GeanyBuildCommand **g = get_build_group_pointer(src, grp);
if (g == NULL) return NULL;
return *g;
}
/** Remove the specified Build menu item.
*
* Makes the specified menu item configuration no longer exist. This
* is different to setting fields to blank because the menu item
* will be deleted from the configuration file on saving
* (except the system filetypes settings @see Build Menu Configuration
* section of the Manual).
*
* @param src the source of the menu item to remove.
* @param grp the group of the command to remove.
* @param cmd the index (from 0) of the command within the group. A negative
* value will remove the whole group.
*
* If any parameter is out of range does nothing.
*
* Updates the menu.
*
**/
GEANY_API_SYMBOL
void build_remove_menu_item(const GeanyBuildSource src, const GeanyBuildGroup grp, const gint cmd)
{
GeanyBuildCommand *bc;
guint i;
bc = get_build_group(src, grp);
if (bc == NULL)
return;
if (cmd < 0)
{
for (i = 0; i < build_groups_count[grp]; ++i)
bc[i].exists = FALSE;
}
else if ((guint) cmd < build_groups_count[grp])
bc[cmd].exists = FALSE;
}
/* * Get the @a GeanyBuildCommand structure for the specified Build menu item.
*
* Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
* hidden by higher priority commands.
*
* @param src the source of the specified menu item.
* @param grp the group of the specified menu item.
* @param cmd the index of the command within the group.
*
* @return a pointer to the @a GeanyBuildCommand structure or @c NULL if it doesn't exist.
* This is a pointer to an internal structure and must not be freed.
*
* @see build_menu_update
**/
GeanyBuildCommand *build_get_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, guint cmd)
{
GeanyBuildCommand *bc;
g_return_val_if_fail(src < GEANY_BCS_COUNT, NULL);
g_return_val_if_fail(grp < GEANY_GBG_COUNT, NULL);
g_return_val_if_fail(cmd < build_groups_count[grp], NULL);
bc = get_build_group(src, grp);
if (bc == NULL)
return NULL;
return &(bc[cmd]);
}
/** Get the string for the menu item field.
*
* Get the current highest priority command specified by @a grp and @a cmd. This is the one
* that the menu item will use if activated.
*
* @param grp the group of the specified menu item.
* @param cmd the index of the command within the group.
* @param fld the field to return
*
* @return @nullable a pointer to the constant string or @c NULL if it doesn't exist.
* This is a pointer to an internal structure and must not be freed.
*
**/
GEANY_API_SYMBOL
const gchar *build_get_current_menu_item(const GeanyBuildGroup grp, const guint cmd,
const GeanyBuildCmdEntries fld)
{
GeanyBuildCommand *c;
gchar *str = NULL;
g_return_val_if_fail(grp < GEANY_GBG_COUNT, NULL);
g_return_val_if_fail(fld < GEANY_BC_CMDENTRIES_COUNT, NULL);
g_return_val_if_fail(cmd < build_groups_count[grp], NULL);
c = get_build_cmd(NULL, grp, cmd, NULL);
if (c == NULL) return NULL;
switch (fld)
{
case GEANY_BC_COMMAND:
str = c->command;
break;
case GEANY_BC_LABEL:
str = c->label;
break;
case GEANY_BC_WORKING_DIR:
str = c->working_dir;
break;
default:
break;
}
return str;
}
/** Set the string for the menu item field.
*
* Set the specified field of the command specified by @a src, @a grp and @a cmd.
*
* @param src the source of the menu item
* @param grp the group of the specified menu item.
* @param cmd the index of the menu item within the group.
* @param fld the field in the menu item command to set
* @param val the value to set the field to, is copied
*
**/
GEANY_API_SYMBOL
void build_set_menu_item(const GeanyBuildSource src, const GeanyBuildGroup grp,
const guint cmd, const GeanyBuildCmdEntries fld, const gchar *val)
{
GeanyBuildCommand **g;
g_return_if_fail(src < GEANY_BCS_COUNT);
g_return_if_fail(grp < GEANY_GBG_COUNT);
g_return_if_fail(fld < GEANY_BC_CMDENTRIES_COUNT);
g_return_if_fail(cmd < build_groups_count[grp]);
g = get_build_group_pointer(src, grp);
if (g == NULL) return;
if (*g == NULL )
{
*g = g_new0(GeanyBuildCommand, build_groups_count[grp]);
}
switch (fld)
{
case GEANY_BC_COMMAND:
SETPTR((*g)[cmd].command, g_strdup(val));
(*g)[cmd].exists = TRUE;
break;
case GEANY_BC_LABEL:
SETPTR((*g)[cmd].label, g_strdup(val));
(*g)[cmd].exists = TRUE;
break;
case GEANY_BC_WORKING_DIR:
SETPTR((*g)[cmd].working_dir, g_strdup(val));
(*g)[cmd].exists = TRUE;
break;
default:
break;
}
build_menu_update(NULL);
}
/** Activate the menu item.
*
* Activate the menu item specified by @a grp and @a cmd.
*
* @param grp the group of the specified menu item.
* @param cmd the index of the command within the group.
*
**/
GEANY_API_SYMBOL
void build_activate_menu_item(const GeanyBuildGroup grp, const guint cmd)
{
on_build_menu_item(NULL, GRP_CMD_TO_POINTER(grp, cmd));
}
/* Clear all error indicators in all documents. */
static void clear_all_errors(void)
{
guint i;
foreach_document(i)
{
editor_indicator_clear_errors(documents[i]->editor);
}
}
/* Replaces occurrences of %e and %p with the appropriate filenames and
* %l with current line number. %d and %p replacements should be in UTF8 */
static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *src)
{
GString *stack;
gchar *replacement;
gchar *executable = NULL;
gint line_num;
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
stack = g_string_new(src);
if (doc != NULL && doc->file_name != NULL)
{
/* replace %f with the filename (including extension) */
replacement = g_path_get_basename(doc->file_name);
utils_string_replace_all(stack, "%f", replacement);
g_free(replacement);
/* replace %d with the absolute path of the dir of the current file */
replacement = g_path_get_dirname(doc->file_name);
utils_string_replace_all(stack, "%d", replacement);
g_free(replacement);
/* replace %e with the filename (excluding extension) */
executable = utils_remove_ext_from_filename(doc->file_name);
replacement = g_path_get_basename(executable);
utils_string_replace_all(stack, "%e", replacement);
g_free(replacement);
/* replace %l with the current 1-based line number */
line_num = sci_get_current_line(doc->editor->sci) + 1;
replacement = g_strdup_printf("%i", line_num);
utils_string_replace_all(stack, "%l", replacement);
g_free(replacement);
}
/* replace %p with the current project's (absolute) base directory */
replacement = NULL; /* prevent double free if no replacement found */
if (app->project)
{
replacement = project_get_base_path();
}
else if (strstr(stack->str, "%p"))
{ /* fall back to %d */
ui_set_statusbar(FALSE, _("failed to substitute %%p, no project active"));
if (doc != NULL && doc->file_name != NULL)
replacement = g_path_get_dirname(doc->file_name);
}
utils_string_replace_all(stack, "%p", replacement);
g_free(replacement);
g_free(executable);
return g_string_free(stack, FALSE); /* don't forget to free src also if needed */
}
/* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
* idx document directory */
static void build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
{
GError *error = NULL;
gchar *argv[] = { "/bin/sh", "-c", NULL, NULL };
gchar *working_dir;
gchar *utf8_working_dir;
gchar *cmd_string;
g_return_if_fail(doc == NULL || doc->is_valid);
if ((doc == NULL || EMPTY(doc->file_name)) && EMPTY(dir))
{
geany_debug("Failed to run command with no working directory");
ui_set_statusbar(TRUE, _("Process failed, no working directory"));
return;
}
clear_all_errors();
SETPTR(current_dir_entered, NULL);
utf8_working_dir = !EMPTY(dir) ? g_strdup(dir) : g_path_get_dirname(doc->file_name);
working_dir = utils_get_locale_from_utf8(utf8_working_dir);
gtk_list_store_clear(msgwindow.store_compiler);
gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
msgwin_compiler_add(COLOR_BLUE, _("%s (in directory: %s)"), cmd, utf8_working_dir);
g_free(utf8_working_dir);
#ifdef G_OS_UNIX
cmd_string = utils_get_locale_from_utf8(cmd);
argv[2] = cmd_string;
cmd = NULL; /* under Unix, use argv to start cmd via sh for compatibility */
#else
/* Expand environment variables like %blah%. */
cmd_string = win32_expand_environment_variables(cmd);
argv[0] = NULL; /* under Windows, run cmd directly */
cmd = cmd_string;
#endif
/* set the build info for the message window */
g_free(build_info.dir);
build_info.dir = g_strdup(working_dir);
build_info.file_type_id = (doc == NULL) ? GEANY_FILETYPES_NONE : doc->file_type->id;
build_info.message_count = 0;
if (!spawn_with_callbacks(working_dir, cmd, argv, NULL, 0, NULL, NULL, build_iofunc,
GINT_TO_POINTER(0), 0, build_iofunc, GINT_TO_POINTER(1), 0, build_exit_cb, NULL,
&build_info.pid, &error))
{
geany_debug("build command spawning failed: %s", error->message);
ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
g_error_free(error);
}
g_free(working_dir);
g_free(cmd_string);
}
/* Returns: NULL if there was an error, or the command to be executed. If Geany is
* set to use a run script, the returned value is a path to the script that runs
* the command; otherwise the command itself is returned. working_dir is a pointer
* to the working directory from which the command is executed. Both strings are
* in the locale encoding. */
static gchar *prepare_run_cmd(GeanyDocument *doc, gchar **working_dir, guint cmdindex)
{
GeanyBuildCommand *cmd = NULL;
const gchar *cmd_working_dir;
gboolean autoclose = FALSE;
gchar *cmd_string_utf8, *working_dir_utf8, *run_cmd, *cmd_string;
GError *error = NULL;
cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
cmd_string_utf8 = build_replace_placeholder(doc, cmd->command);
cmd_working_dir = cmd->working_dir;
if (EMPTY(cmd_working_dir))
cmd_working_dir = "%d";
working_dir_utf8 = build_replace_placeholder(doc, cmd_working_dir);
*working_dir = utils_get_locale_from_utf8(working_dir_utf8);
if (EMPTY(*working_dir) || ! g_file_test(*working_dir, G_FILE_TEST_EXISTS) ||
! g_file_test(*working_dir, G_FILE_TEST_IS_DIR))
{
ui_set_statusbar(TRUE, _("Invalid working directory \"%s\""),
!EMPTY(working_dir_utf8) ? working_dir_utf8 : "<NULL>" );
utils_free_pointers(3, cmd_string_utf8, working_dir_utf8, *working_dir, NULL);
return NULL;
}
cmd_string = utils_get_locale_from_utf8(cmd_string_utf8);
#ifdef HAVE_VTE
if (vte_info.have_vte && vc->run_in_vte)
{
if (vc->skip_run_script)
{
utils_free_pointers(2, cmd_string_utf8, working_dir_utf8, NULL);
return cmd_string;
}
else
/* don't wait for user input at the end of script when we are running in VTE */
autoclose = TRUE;
}
#endif
#ifdef G_OS_WIN32
/* Expand environment variables like %blah%. */
SETPTR(cmd_string, win32_expand_environment_variables(cmd_string));
gchar *helper = g_build_filename(utils_resource_dir(RESOURCE_DIR_LIBEXEC), "geany-run-helper", NULL);
/* escape helper appropriately */
/* FIXME: check the Windows rules, but it should not matter too much here as \es and "es are not
* allowed in paths anyway */
run_cmd = g_strdup_printf("\"%s\" \"%s\" %d %s", helper, *working_dir, autoclose ? 1 : 0, cmd_string);
g_free(helper);
#else
run_cmd = build_create_shellscript(*working_dir, cmd_string, autoclose, &error);
if (!run_cmd)
{
ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created: %s)"),
!EMPTY(cmd_string_utf8) ? cmd_string_utf8 : NULL, error->message);
g_error_free(error);
g_free(*working_dir);
}
#endif
utils_free_pointers(3, cmd_string_utf8, working_dir_utf8, cmd_string, NULL);
return run_cmd;
}
static void build_run_cmd(GeanyDocument *doc, guint cmdindex)
{
gchar *working_dir;
gchar *run_cmd = NULL;
if (! DOC_VALID(doc) || doc->file_name == NULL)
return;
run_cmd = prepare_run_cmd(doc, &working_dir, cmdindex);
if (run_cmd == NULL)
return;
run_info[cmdindex].file_type_id = doc->file_type->id;
#ifdef HAVE_VTE
if (vte_info.have_vte && vc->run_in_vte)
{
gchar *vte_cmd;
/* VTE expects commands in UTF-8 */
SETPTR(run_cmd, utils_get_utf8_from_locale(run_cmd));
SETPTR(working_dir, utils_get_utf8_from_locale(working_dir));
if (vc->skip_run_script)
vte_cmd = g_strconcat(run_cmd, "\n", NULL);
else
vte_cmd = g_strconcat("\n/bin/sh ", run_cmd, "\n", NULL);
vte_cwd(working_dir, TRUE);
if (! vte_send_cmd(vte_cmd))
{
const gchar *msg = _("File not executed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
ui_set_statusbar(FALSE, "%s", msg);
geany_debug("%s", msg);
if (!vc->skip_run_script)
g_unlink(run_cmd);
}
/* show the VTE */
gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
gtk_widget_grab_focus(vc->vte);
msgwin_show_hide(TRUE);
run_info[cmdindex].pid = 1;
g_free(vte_cmd);
}
else
#endif
{
gchar *locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
GError *error = NULL;
#ifdef G_OS_WIN32
if (g_regex_match_simple("^[ \"]*cmd([.]exe)?[\" ]", locale_term_cmd, 0, 0))
{
/* if passing an argument to cmd.exe, respect its quoting rules */
GString *escaped_run_cmd = g_string_new(NULL);
for (gchar *p = run_cmd; *p; p++)
{
if (strchr("()%!^\"<>&| ", *p)) // cmd.exe metacharacters
g_string_append_c(escaped_run_cmd, '^');
g_string_append_c(escaped_run_cmd, *p);
}
SETPTR(run_cmd, g_string_free(escaped_run_cmd, FALSE));
}
#endif
utils_str_replace_all(&locale_term_cmd, "%c", run_cmd);
if (spawn_async(working_dir, locale_term_cmd, NULL, NULL, &(run_info[cmdindex].pid),
&error))
{
g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
(gpointer) &(run_info[cmdindex]));
build_menu_update(doc);
}
else
{
gchar *utf8_term_cmd = utils_get_utf8_from_locale(locale_term_cmd);
ui_set_statusbar(TRUE, _("Cannot execute build command \"%s\": %s. "
"Check the Terminal setting in Preferences"), utf8_term_cmd, error->message);
g_free(utf8_term_cmd);
g_error_free(error);
#ifndef G_OS_WIN32
g_unlink(run_cmd);
#endif
run_info[cmdindex].pid = (GPid) 0;
}
}
g_free(working_dir);
g_free(run_cmd);
}
static void process_build_output_line(gchar *msg, gint color)
{
gchar *tmp;