/
build.c
2732 lines (2393 loc) · 81.7 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-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* Copyright 2009 Lex Trotman <elextr(at)gmail(dot)com>
*
* 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.
*
* $Id$
*/
/*
* Build commands and menu items.
*/
#include "geany.h"
#include "build.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <glib/gstdio.h>
#ifdef G_OS_UNIX
# include <sys/types.h>
# include <sys/wait.h>
# include <signal.h>
#else
# include <windows.h>
#endif
#include "prefs.h"
#include "support.h"
#include "document.h"
#include "utils.h"
#include "ui_utils.h"
#include "dialogs.h"
#include "msgwindow.h"
#include "filetypes.h"
#include "keybindings.h"
#include "vte.h"
#include "project.h"
#include "editor.h"
#include "win32.h"
#include "toolbar.h"
#include "geanymenubuttonaction.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;
#ifdef G_OS_WIN32
static const gchar RUN_SCRIPT_CMD[] = "geany_run_script.bat";
#else
static const gchar RUN_SCRIPT_CMD[] = "./geany_run_script.sh";
#endif
/* pack group (<8) and command (<32) into a user_data pointer */
#define GRP_CMD_TO_POINTER(grp, cmd) GINT_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_INT(gptr)&0x1f)
#define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_INT(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 gint build_groups_count[GEANY_GBG_COUNT] = { 3, 4, 2 };
static gint build_items_count = 9;
#ifndef G_OS_WIN32
static void build_exit_cb(GPid child_pid, gint status, gpointer user_data);
static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data);
#endif
static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose);
static GPid 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(const gchar *line, gint color);
static void show_build_commands_dialog(void);
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)
{
return bc->entries[id];
}
static gchar *buildcmd_label(GeanyBuildCommand *bc)
{
return id_to_str(bc, GEANY_BC_LABEL);
}
static gchar *buildcmd_cmd(GeanyBuildCommand *bc)
{
return id_to_str(bc, GEANY_BC_COMMAND);
}
static gchar *buildcmd_working_dir(GeanyBuildCommand *bc)
{
return id_to_str(bc, GEANY_BC_WORKING_DIR);
}
static const gchar *config_keys[] = {
[GEANY_BC_LABEL] = "LB",
[GEANY_BC_COMMAND] = "CM",
[GEANY_BC_WORKING_DIR] = "WD"
};
/*-----------------------------------------------------
*
* 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)
{
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->filecmds);
cl[GEANY_GBG_FT][GEANY_BCS_HOME_FT] = &(ft->homefilecmds);
cl[GEANY_GBG_FT][GEANY_BCS_PROJ] = &(ft->projfilecmds);
cl[GEANY_GBG_NON_FT][GEANY_BCS_FT] = &(ft->ftdefcmds);
cl[GEANY_GBG_EXEC][GEANY_BCS_FT] = &(ft->execcmds);
cl[GEANY_GBG_EXEC][GEANY_BCS_HOME_FT] = &(ft->homeexeccmds);
cl[GEANY_GBG_EXEC][GEANY_BCS_PROJ_FT] = &(ft->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");
}
}
/* 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[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
return &(cmds[cmdindex]); \
}
#define return_ft_cmd_if(src, cmds)\
if (ft != NULL && ft->cmds != NULL \
&& ft->cmds[cmdindex].exists && below>src)\
{ \
*fr=src; \
if (printbuildcmds) \
printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
return &(ft->cmds[cmdindex]); \
}
/* get the next lowest command taking priority into account */
static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, gint cmdgrp, gint cmdindex,
gint below, gint *from)
{
/* Note: parameter below used in macros above */
GeanyFiletype *ft = NULL;
gint sink, *fr = &sink;
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, gint grp, gint cmdindex, gint *from)
{
return get_next_build_cmd(doc, grp, cmdindex, GEANY_BCS_COUNT, from);
}
#define return_nonblank_regex(src, ptr)\
if (NZV(ptr)) \
{ *fr = (src); return &(ptr); }
/* like get_build_cmd, but for regexen, used by filetypes */
gchar **build_get_regex(GeanyBuildGroup grp, GeanyFiletype *ft, gint *from)
{
gint 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->projerror_regex_string);
return_nonblank_regex(GEANY_BCS_HOME_FT, ft->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;
}
/* get pointer to the command group array */
static GeanyBuildCommand *get_build_group(GeanyBuildSource src, 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->ftdefcmds;
case GEANY_BCS_FT: return ft->filecmds;
case GEANY_BCS_HOME_FT: return ft->homefilecmds;
case GEANY_BCS_PREF: return ft->homefilecmds;
case GEANY_BCS_PROJ: return ft->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->execcmds: NULL;
case GEANY_BCS_HOME_FT: return ft ? ft->homeexeccmds: NULL;
case GEANY_BCS_PROJ_FT: return ft ? ft->projexeccmds: NULL;
case GEANY_BCS_PREF: return exec_pref;
case GEANY_BCS_PROJ: return exec_proj;
default: return NULL;
}
break;
default:
return NULL;
}
}
/** 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.
*
* @see build_menu_update
**/
void build_remove_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, gint cmd)
{
GeanyBuildCommand *bc;
gint 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 (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 @a 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, gint cmd)
{
GeanyBuildCommand *bc;
if (src >= GEANY_BCS_COUNT || grp >= GEANY_GBG_COUNT || cmd >= build_groups_count[grp])
return NULL;
bc = get_build_group(src, grp);
if (bc == NULL)
return NULL;
return &(bc[cmd]);
}
/** Get the @a GeanyBuildCommand structure for the menu item.
*
* 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 src pointer to @a gint to return which source provided the command. Ignored if @a NULL.
* Values are one of @a GeanyBuildSource but returns a signed type not the enum.
*
* @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
* This is a pointer to an internal structure and must not be freed.
*
* @see build_menu_update
**/
/* parameter checked version of get_build_cmd for external interface */
GeanyBuildCommand *build_get_current_menu_item(GeanyBuildGroup grp, gint cmd, gint *src)
{
if (*src >= GEANY_BCS_COUNT || grp >= GEANY_GBG_COUNT || cmd >= build_groups_count[grp])
return NULL;
return get_build_cmd(NULL, grp, cmd, src);
}
/* Clear all error indicators in all documents. */
static void clear_errors(GeanyDocument *doc)
{
guint i;
for (i = 0; i < documents_array->len; i++)
{
if (documents[i]->is_valid)
editor_indicator_clear_errors(documents[i]->editor);
}
}
#ifdef G_OS_WIN32
static void parse_build_output(const gchar **output, gint status)
{
guint x, i, len;
gchar *line, **lines;
for (x = 0; x < 2; x++)
{
if (NZV(output[x]))
{
lines = g_strsplit_set(output[x], "\r\n", -1);
len = g_strv_length(lines);
for (i = 0; i < len; i++)
{
if (NZV(lines[i]))
{
line = lines[i];
while (*line != '\0')
{ /* replace any conrol characters in the output */
if (*line < 32)
*line = 32;
line++;
}
process_build_output_line(lines[i], COLOR_BLACK);
}
}
g_strfreev(lines);
}
}
show_build_result_message(status != 0);
utils_beep();
build_info.pid = 0;
/* enable build items again */
build_menu_update(NULL);
}
#endif
/* Replaces occurences of %e and %p with the appropriate filenames,
* %d and %p replacements should be in UTF8 */
static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *src)
{
GString *stack;
gchar *filename = NULL;
gchar *replacement;
gchar *executable = NULL;
gchar *ret_str; /* to be freed when not in use anymore */
stack = g_string_new(src);
if (doc != NULL && doc->file_name != NULL)
{
filename = utils_get_utf8_from_locale(doc->file_name);
/* replace %f with the filename (including extension) */
replacement = g_path_get_basename(filename);
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(filename);
utils_string_replace_all(stack, "%d", replacement);
g_free(replacement);
/* replace %e with the filename (excluding extension) */
executable = utils_remove_ext_from_filename(filename);
replacement = g_path_get_basename(executable);
utils_string_replace_all(stack, "%e", 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 && filename != NULL)
replacement = g_path_get_dirname(filename);
}
utils_string_replace_all(stack, "%p", replacement);
g_free(replacement);
ret_str = utils_get_utf8_from_locale(stack->str);
g_free(executable);
g_free(filename);
g_string_free(stack, TRUE);
return ret_str; /* 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 GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
{
GError *error = NULL;
gchar **argv;
gchar *working_dir;
gchar *utf8_working_dir;
gchar *cmd_string;
gchar *utf8_cmd_string;
#ifdef G_OS_WIN32
gchar *output[2];
gint status;
#else
gint stdout_fd;
gint stderr_fd;
#endif
if (!((doc != NULL && NZV(doc->file_name)) || NZV(dir)))
{
geany_debug("Failed to run command with no working directory");
ui_set_statusbar(TRUE, _("Process failed, no working directory"));
return (GPid) 1;
}
if (doc != NULL)
clear_errors(doc);
setptr(current_dir_entered, NULL);
cmd_string = g_strdup(cmd);
#ifdef G_OS_WIN32
argv = g_strsplit(cmd_string, " ", 0);
#else
argv = g_new0(gchar *, 4);
argv[0] = g_strdup("/bin/sh");
argv[1] = g_strdup("-c");
argv[2] = cmd_string;
argv[3] = NULL;
#endif
utf8_cmd_string = utils_get_utf8_from_locale(cmd_string);
utf8_working_dir = NZV(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)"), utf8_cmd_string, utf8_working_dir);
g_free(utf8_working_dir);
g_free(utf8_cmd_string);
/* 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 : FILETYPE_ID(doc->file_type);
build_info.message_count = 0;
#ifdef G_OS_WIN32
if (! utils_spawn_sync(working_dir, argv, NULL, G_SPAWN_SEARCH_PATH,
NULL, NULL, &output[0], &output[1], &status, &error))
#else
if (! g_spawn_async_with_pipes(working_dir, argv, NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
&(build_info.pid), NULL, &stdout_fd, &stderr_fd, &error))
#endif
{
geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
g_strfreev(argv);
g_error_free(error);
g_free(working_dir);
error = NULL;
return (GPid) 0;
}
#ifdef G_OS_WIN32
parse_build_output((const gchar**) output, status);
g_free(output[0]);
g_free(output[1]);
#else
if (build_info.pid > 0)
{
g_child_watch_add(build_info.pid, (GChildWatchFunc) build_exit_cb, NULL);
build_menu_update(doc);
ui_progress_bar_start(NULL);
}
/* use GIOChannels to monitor stdout and stderr */
utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
TRUE, build_iofunc, GINT_TO_POINTER(0));
utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
TRUE, build_iofunc, GINT_TO_POINTER(1));
#endif
g_strfreev(argv);
g_free(working_dir);
return build_info.pid;
}
/* Returns: NULL if there was an error, or the working directory the script was created in.
* vte_cmd_nonscript is the location of a string which is filled with the command to be used
* when vc->skip_run_script is set, otherwise it will be set to NULL */
static gchar *prepare_run_script(GeanyDocument *doc, gchar **vte_cmd_nonscript, gint cmdindex)
{
gchar *locale_filename = NULL;
gboolean have_project;
GeanyProject *project = app->project;
GeanyBuildCommand *cmd = NULL;
gchar *executable = NULL;
gchar *working_dir = NULL;
const gchar *cmd_working_dir;
gboolean autoclose = FALSE;
gboolean result = FALSE;
gchar *tmp;
gchar *cmd_string;
if (vte_cmd_nonscript != NULL)
*vte_cmd_nonscript = NULL;
locale_filename = utils_get_locale_from_utf8(doc->file_name);
have_project = project != NULL;
cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
cmd_string = build_replace_placeholder(doc, buildcmd_cmd(cmd));
cmd_working_dir = buildcmd_working_dir(cmd);
if (! NZV(cmd_working_dir))
cmd_working_dir = "%d";
working_dir = build_replace_placeholder(doc, cmd_working_dir); /* in utf-8 */
/* only test whether working dir exists, don't change it or else Windows support will break
* (gspawn-win32-helper.exe is used by GLib and must be in $PATH which means current working
* dir where geany.exe was started from, so we can't change it) */
if (!NZV(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, _("Failed to change the working directory to \"%s\""),
NZV(working_dir) ? working_dir : "<NULL>" );
utils_free_pointers(2, cmd_string, working_dir, NULL);
return NULL;
}
#ifdef HAVE_VTE
if (vte_info.load_vte && vc != NULL && vc->run_in_vte)
{
if (vc->skip_run_script)
{
if (vte_cmd_nonscript != NULL)
*vte_cmd_nonscript = cmd_string;
utils_free_pointers(2, executable, locale_filename, NULL);
return working_dir;
}
else
/* don't wait for user input at the end of script when we are running in VTE */
autoclose = TRUE;
}
#endif
/* RUN_SCRIPT_CMD should be ok in UTF8 without converting in locale because it
* contains no umlauts */
tmp = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
result = build_create_shellscript(tmp, cmd_string, autoclose);
if (! result)
{
ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created)"),
NZV(cmd_string) ? cmd_string : NULL);
}
utils_free_pointers(4, cmd_string, tmp, executable, locale_filename, NULL);
if (result)
return working_dir;
g_free(working_dir);
return NULL;
}
static GPid build_run_cmd(GeanyDocument *doc, gint cmdindex)
{
gchar *working_dir;
gchar *vte_cmd_nonscript = NULL;
GError *error = NULL;
if (doc == NULL || doc->file_name == NULL)
return (GPid) 0;
working_dir = prepare_run_script(doc, &vte_cmd_nonscript, cmdindex);
if (working_dir == NULL)
return (GPid) 0;
run_info[cmdindex].file_type_id = FILETYPE_ID(doc->file_type);
#ifdef HAVE_VTE
if (vte_info.load_vte && vc != NULL && vc->run_in_vte)
{
gchar *vte_cmd;
if (vc->skip_run_script)
{
setptr(vte_cmd_nonscript, utils_get_utf8_from_locale(vte_cmd_nonscript));
vte_cmd = g_strconcat(vte_cmd_nonscript, "\n", NULL);
g_free(vte_cmd_nonscript);
}
else
vte_cmd = g_strconcat("\n/bin/sh ", RUN_SCRIPT_CMD, "\n", NULL);
/* change into current directory if it is not done by default */
if (! vc->follow_path)
{
/* we need to convert the working_dir back to UTF-8 because the VTE expects it */
gchar *utf8_working_dir = utils_get_utf8_from_locale(working_dir);
vte_cwd(utf8_working_dir, TRUE);
g_free(utf8_working_dir);
}
if (! vte_send_cmd(vte_cmd))
{
ui_set_statusbar(FALSE,
_("Could not execute the file in the VTE because it probably contains a command."));
geany_debug("Could not execute the file in the VTE because it probably contains a command.");
}
/* 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 = NULL;
gchar **term_argv = NULL;
guint term_argv_len, i;
gchar **argv = NULL;
/* get the terminal path */
locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
/* split the term_cmd, so arguments will work too */
term_argv = g_strsplit(locale_term_cmd, " ", -1);
term_argv_len = g_strv_length(term_argv);
/* check that terminal exists (to prevent misleading error messages) */
if (term_argv[0] != NULL)
{
gchar *tmp = term_argv[0];
/* g_find_program_in_path checks whether tmp exists and is executable */
term_argv[0] = g_find_program_in_path(tmp);
g_free(tmp);
}
if (term_argv[0] == NULL)
{
ui_set_statusbar(TRUE,
_("Could not find terminal \"%s\" "
"(check path for Terminal tool setting in Preferences)"), tool_prefs.term_cmd);
run_info[cmdindex].pid = (GPid) 1;
goto free_strings;
}
argv = g_new0(gchar *, term_argv_len + 3);
for (i = 0; i < term_argv_len; i++)
{
argv[i] = g_strdup(term_argv[i]);
}
#ifdef G_OS_WIN32
/* command line arguments only for cmd.exe */
if (strstr(argv[0], "cmd.exe") != NULL)
{
argv[term_argv_len] = g_strdup("/Q /C");
argv[term_argv_len + 1] = g_strdup(RUN_SCRIPT_CMD);
}
else
{
argv[term_argv_len] = g_strdup(RUN_SCRIPT_CMD);
argv[term_argv_len + 1] = NULL;
}
#else
argv[term_argv_len ] = g_strdup("-e");
argv[term_argv_len + 1] = g_strconcat("/bin/sh ", RUN_SCRIPT_CMD, NULL);
#endif
argv[term_argv_len + 2] = NULL;
if (! g_spawn_async(working_dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
NULL, NULL, &(run_info[cmdindex].pid), &error))
{
geany_debug("g_spawn_async() failed: %s", error->message);
ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
g_unlink(RUN_SCRIPT_CMD);
g_error_free(error);
error = NULL;
run_info[cmdindex].pid = (GPid) 0;
}
if (run_info[cmdindex].pid > 0)
{
g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
(gpointer)&(run_info[cmdindex]));
build_menu_update(doc);
}
free_strings:
g_strfreev(argv);
g_strfreev(term_argv);
g_free(locale_term_cmd);
}
g_free(working_dir);
return run_info[cmdindex].pid;
}
static void process_build_output_line(const gchar *str, gint color)
{
gchar *msg, *tmp;
gchar *filename;
gint line;
msg = g_strdup(str);
g_strchomp(msg);
if (! NZV(msg))
return;
if (build_parse_make_dir(msg, &tmp))
{
setptr(current_dir_entered, tmp);
}
msgwin_parse_compiler_error_line(msg, current_dir_entered, &filename, &line);
if (line != -1 && filename != NULL)
{
GeanyDocument *doc = document_find_by_filename(filename);
/* limit number of indicators */
if (doc && editor_prefs.use_indicators &&
build_info.message_count < GEANY_BUILD_ERR_HIGHLIGHT_MAX)
{
if (line > 0) /* some compilers, like pdflatex report errors on line 0 */
line--; /* so only adjust the line number if it is greater than 0 */
editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line);