Fix potential NULL dereference
[geany-mirror.git] / src / build.c
blob05ab82a7f866166e745247fa034060ab4f683343
1 /*
2 * build.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2009 Lex Trotman <elextr(at)gmail(dot)com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * Build commands and menu items.
26 /* TODO: tidy code:
27 * Use intermediate pointers for common subexpressions.
28 * Replace defines with enums.
29 * Other TODOs in code. */
31 #include "geany.h"
32 #include "build.h"
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <glib/gstdio.h>
41 #ifdef G_OS_UNIX
42 # include <sys/types.h>
43 # include <sys/wait.h>
44 # include <signal.h>
45 #else
46 # include <windows.h>
47 #endif
49 #include "prefs.h"
50 #include "support.h"
51 #include "document.h"
52 #include "utils.h"
53 #include "ui_utils.h"
54 #include "dialogs.h"
55 #include "msgwindow.h"
56 #include "filetypes.h"
57 #include "keybindings.h"
58 #include "vte.h"
59 #include "project.h"
60 #include "editor.h"
61 #include "win32.h"
62 #include "toolbar.h"
63 #include "geanymenubuttonaction.h"
65 /* g_spawn_async_with_pipes doesn't work on Windows */
66 #ifdef G_OS_WIN32
67 #define SYNC_SPAWN
68 #endif
70 /* Number of editor indicators to draw - limited as this can affect performance */
71 #define GEANY_BUILD_ERR_HIGHLIGHT_MAX 50
74 GeanyBuildInfo build_info = {GEANY_GBG_FT, 0, 0, NULL, GEANY_FILETYPES_NONE, NULL, 0};
76 static gchar *current_dir_entered = NULL;
78 typedef struct RunInfo
80 GPid pid;
81 gint file_type_id;
82 } RunInfo;
84 static RunInfo *run_info;
86 #ifdef G_OS_WIN32
87 static const gchar RUN_SCRIPT_CMD[] = "geany_run_script.bat";
88 #else
89 static const gchar RUN_SCRIPT_CMD[] = "./geany_run_script.sh";
90 #endif
92 /* pack group (<8) and command (<32) into a user_data pointer */
93 #define GRP_CMD_TO_POINTER(grp, cmd) GINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
94 #define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
95 #define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_INT(gptr)&0x1f)
96 #define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_INT(gptr)&0xe0) >> 5)
98 static gpointer last_toolbutton_action = GBO_TO_POINTER(GEANY_GBO_BUILD);
100 static BuildMenuItems menu_items = {NULL, {NULL, NULL, NULL, NULL}};
102 static struct
104 GtkAction *run_action;
105 GtkAction *compile_action;
106 GtkAction *build_action;
107 GtkWidget *toolmenu;
109 GtkWidget *toolitem_build;
110 GtkWidget *toolitem_make_all;
111 GtkWidget *toolitem_make_custom;
112 GtkWidget *toolitem_make_object;
113 GtkWidget *toolitem_set_args;
115 widgets;
117 static gint build_groups_count[GEANY_GBG_COUNT] = { 3, 4, 2 };
118 static gint build_items_count = 9;
120 #ifndef SYNC_SPAWN
121 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data);
122 static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data);
123 #endif
124 static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose);
125 static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir);
126 static void set_stop_button(gboolean stop);
127 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data);
128 static void on_set_build_commands_activate(GtkWidget *w, gpointer u);
129 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data);
130 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data);
131 static void kill_process(GPid *pid);
132 static void show_build_result_message(gboolean failure);
133 static void process_build_output_line(const gchar *line, gint color);
134 static void show_build_commands_dialog(void);
137 void build_finalize(void)
139 g_free(build_info.dir);
140 g_free(build_info.custom_target);
142 if (menu_items.menu != NULL && GTK_IS_WIDGET(menu_items.menu))
143 gtk_widget_destroy(menu_items.menu);
147 /* note: copied from keybindings.c, may be able to go away */
148 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id,
149 GtkAccelGroup *accel_group, GtkWidget *menuitem)
151 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
153 if (kb->key != 0)
154 gtk_widget_add_accelerator(menuitem, "activate", accel_group,
155 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
159 /* convenience routines to access parts of GeanyBuildCommand */
160 static gchar *id_to_str(GeanyBuildCommand *bc, gint id)
162 switch (id)
164 case GEANY_BC_LABEL:
165 return bc->label;
166 case GEANY_BC_COMMAND:
167 return bc->command;
168 case GEANY_BC_WORKING_DIR:
169 return bc->working_dir;
171 g_assert(0);
172 return NULL;
176 static void set_command(GeanyBuildCommand *bc, gint id, gchar *str)
178 switch (id)
180 case GEANY_BC_LABEL:
181 setptr(bc->label, str);
182 break;
183 case GEANY_BC_COMMAND:
184 setptr(bc->command, str);
185 break;
186 case GEANY_BC_WORKING_DIR:
187 setptr(bc->working_dir, str);
188 break;
189 default:
190 g_assert(0);
195 static const gchar *config_keys[GEANY_BC_CMDENTRIES_COUNT] = {
196 "LB", /* label */
197 "CM", /* command */
198 "WD" /* working directory */
201 /*-----------------------------------------------------
203 * Execute commands and handle results
205 *-----------------------------------------------------*/
207 /* the various groups of commands not in the filetype struct */
208 static GeanyBuildCommand *ft_def = NULL;
209 static GeanyBuildCommand *non_ft_proj = NULL;
210 static GeanyBuildCommand *non_ft_pref = NULL;
211 static GeanyBuildCommand *non_ft_def = NULL;
212 static GeanyBuildCommand *exec_proj = NULL;
213 static GeanyBuildCommand *exec_pref = NULL;
214 static GeanyBuildCommand *exec_def = NULL;
215 /* and the regexen not in the filetype structure */
216 static gchar *regex_pref = NULL;
217 /* project non-fileregex string */
218 static gchar *regex_proj = NULL;
220 /* control if build commands are printed by get_build_cmd, for debug purposes only*/
221 #ifndef PRINTBUILDCMDS
222 #define PRINTBUILDCMDS FALSE
223 #endif
224 static gboolean printbuildcmds = PRINTBUILDCMDS;
227 /* for debug only, print the commands structures in priority order */
228 static void printfcmds(void)
230 #if 0
231 GeanyBuildCommand **cl[GEANY_GBG_COUNT][GEANY_BCS_COUNT] = {
232 /* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
233 * GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
234 { &ft_def, NULL, NULL, NULL, NULL, NULL },
235 { &non_ft_def, NULL, NULL, &non_ft_pref, NULL, &non_ft_proj },
236 { &exec_def, NULL, NULL, &exec_pref, NULL, &exec_proj }
238 GeanyFiletype *ft = NULL;
239 GeanyDocument *doc;
240 gint i, j, k, l, m;
241 enum GeanyBuildCmdEntries n;
242 gint cc[GEANY_BCS_COUNT];
243 gchar c;
245 doc = document_get_current();
246 if (doc != NULL)
247 ft = doc->file_type;
248 if (ft != NULL)
250 printf("filetype %s\n",ft->name);
251 cl[GEANY_GBG_FT][GEANY_BCS_FT] = &(ft->filecmds);
252 cl[GEANY_GBG_FT][GEANY_BCS_HOME_FT] = &(ft->homefilecmds);
253 cl[GEANY_GBG_FT][GEANY_BCS_PROJ] = &(ft->projfilecmds);
254 cl[GEANY_GBG_NON_FT][GEANY_BCS_FT] = &(ft->ftdefcmds);
255 cl[GEANY_GBG_EXEC][GEANY_BCS_FT] = &(ft->execcmds);
256 cl[GEANY_GBG_EXEC][GEANY_BCS_HOME_FT] = &(ft->homeexeccmds);
257 cl[GEANY_GBG_EXEC][GEANY_BCS_PROJ_FT] = &(ft->projexeccmds);
259 for (i = 0; i < GEANY_BCS_COUNT; ++i)
261 m = 1;
262 for (j = 0; j < GEANY_GBG_COUNT; ++j)
264 for (k = 0; k < build_groups_count[j]; ++k)
265 if (cl[j][i] != NULL && *(cl[j][i]) != NULL && (*(cl[j][i]))[k].exists)
267 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
269 if ((*(cl[j][i]))[k].entries[n] != NULL &&
270 (l = strlen((*(cl[j][i]))[k].entries[n])) > m)
272 m = l;
277 cc[i] = m;
279 for (i = 0; i < GEANY_GBG_COUNT; ++i)
281 for (k = 0; k < build_groups_count[i]; ++k)
283 for (l = 0; l < 2; ++l)
285 c = ' ';
286 for (j = 0; j < GEANY_BCS_COUNT; ++j)
288 if (cl[i][j] != NULL && *(cl[i][j]) != NULL && (*(cl[i][j]))[k].exists)
290 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
292 if ((*(cl[i][j]))[k].entries[i] != NULL)
293 printf("%c %*.*s",c,cc[j],cc[j],(*(cl[i][j]))[k].entries[i]);
294 else
295 printf("%c %*.*s",c,cc[j],cc[j]," ");
298 else
299 printf("%c %*.*s",c,cc[j],cc[j]," ");
300 c = ',';
302 printf("\n");
305 printf("\n");
307 #endif
311 /* macros to save typing and make the logic visible */
312 #define return_cmd_if(src, cmds)\
313 if (cmds != NULL && cmds[cmdindex].exists && below>src)\
315 *fr=src; \
316 if (printbuildcmds) \
317 printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
318 return &(cmds[cmdindex]); \
321 #define return_ft_cmd_if(src, cmds)\
322 if (ft != NULL && ft->cmds != NULL \
323 && ft->cmds[cmdindex].exists && below>src)\
325 *fr=src; \
326 if (printbuildcmds) \
327 printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
328 return &(ft->cmds[cmdindex]); \
332 /* get the next lowest command taking priority into account */
333 static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, gint cmdgrp, gint cmdindex,
334 gint below, gint *from)
336 /* Note: parameter below used in macros above */
337 GeanyFiletype *ft = NULL;
338 gint sink, *fr = &sink;
340 if (printbuildcmds)
341 printfcmds();
342 if (cmdgrp >= GEANY_GBG_COUNT)
343 return NULL;
344 if (from != NULL)
345 fr = from;
346 if (doc == NULL)
347 doc = document_get_current();
348 if (doc != NULL)
349 ft = doc->file_type;
351 switch (cmdgrp)
353 case GEANY_GBG_FT: /* order proj ft, home ft, ft, defft */
354 if (ft != NULL)
356 return_ft_cmd_if(GEANY_BCS_PROJ, projfilecmds);
357 return_ft_cmd_if(GEANY_BCS_PREF, homefilecmds);
358 return_ft_cmd_if(GEANY_BCS_FT, filecmds);
360 return_cmd_if(GEANY_BCS_DEF, ft_def);
361 break;
362 case GEANY_GBG_NON_FT: /* order proj, pref, def */
363 return_cmd_if(GEANY_BCS_PROJ, non_ft_proj);
364 return_cmd_if(GEANY_BCS_PREF, non_ft_pref);
365 return_ft_cmd_if(GEANY_BCS_FT, ftdefcmds);
366 return_cmd_if(GEANY_BCS_DEF, non_ft_def);
367 break;
368 case GEANY_GBG_EXEC: /* order proj, proj ft, pref, home ft, ft, def */
369 return_cmd_if(GEANY_BCS_PROJ, exec_proj);
370 return_ft_cmd_if(GEANY_BCS_PROJ_FT, projexeccmds);
371 return_cmd_if(GEANY_BCS_PREF, exec_pref);
372 return_ft_cmd_if(GEANY_BCS_FT, homeexeccmds);
373 return_ft_cmd_if(GEANY_BCS_FT, execcmds);
374 return_cmd_if(GEANY_BCS_DEF, exec_def);
375 break;
376 default:
377 break;
379 return NULL;
383 /* shortcut to start looking at the top */
384 static GeanyBuildCommand *get_build_cmd(GeanyDocument *doc, gint grp, gint cmdindex, gint *from)
386 return get_next_build_cmd(doc, grp, cmdindex, GEANY_BCS_COUNT, from);
390 #define return_nonblank_regex(src, ptr)\
391 if (NZV(ptr)) \
392 { *fr = (src); return &(ptr); }
395 /* like get_build_cmd, but for regexen, used by filetypes */
396 gchar **build_get_regex(GeanyBuildGroup grp, GeanyFiletype *ft, gint *from)
398 gint sink, *fr = &sink;
400 if (from != NULL)
401 fr = from;
402 if (grp == GEANY_GBG_FT)
404 if (ft == NULL)
406 GeanyDocument *doc = document_get_current();
407 if (doc != NULL)
408 ft = doc->file_type;
410 if (ft == NULL)
411 return NULL;
412 return_nonblank_regex(GEANY_BCS_PROJ, ft->projerror_regex_string);
413 return_nonblank_regex(GEANY_BCS_HOME_FT, ft->homeerror_regex_string);
414 return_nonblank_regex(GEANY_BCS_FT, ft->error_regex_string);
416 else if (grp == GEANY_GBG_NON_FT)
418 return_nonblank_regex(GEANY_BCS_PROJ, regex_proj);
419 return_nonblank_regex(GEANY_BCS_PREF, regex_pref);
421 return NULL;
425 /* get pointer to the command group array */
426 static GeanyBuildCommand *get_build_group(GeanyBuildSource src, GeanyBuildGroup grp)
428 GeanyDocument *doc;
429 GeanyFiletype *ft = NULL;
431 switch (grp)
433 case GEANY_GBG_FT:
434 if ((doc = document_get_current()) == NULL)
435 return NULL;
436 if ((ft = doc->file_type) == NULL)
437 return NULL;
439 switch (src)
441 case GEANY_BCS_DEF: return ft->ftdefcmds;
442 case GEANY_BCS_FT: return ft->filecmds;
443 case GEANY_BCS_HOME_FT: return ft->homefilecmds;
444 case GEANY_BCS_PREF: return ft->homefilecmds;
445 case GEANY_BCS_PROJ: return ft->projfilecmds;
446 default: return NULL;
448 break;
449 case GEANY_GBG_NON_FT:
450 switch (src)
452 case GEANY_BCS_DEF: return non_ft_def;
453 case GEANY_BCS_PREF: return non_ft_pref;
454 case GEANY_BCS_PROJ: return non_ft_proj;
455 default: return NULL;
457 break;
458 case GEANY_GBG_EXEC:
459 if ((doc = document_get_current()) != NULL)
460 ft = doc->file_type;
461 switch (src)
463 case GEANY_BCS_DEF: return exec_def;
464 case GEANY_BCS_FT: return ft ? ft->execcmds: NULL;
465 case GEANY_BCS_HOME_FT: return ft ? ft->homeexeccmds: NULL;
466 case GEANY_BCS_PROJ_FT: return ft ? ft->projexeccmds: NULL;
467 case GEANY_BCS_PREF: return exec_pref;
468 case GEANY_BCS_PROJ: return exec_proj;
469 default: return NULL;
471 break;
472 default:
473 return NULL;
478 /* * Remove the specified Build menu item.
480 * Makes the specified menu item configuration no longer exist. This
481 * is different to setting fields to blank because the menu item
482 * will be deleted from the configuration file on saving
483 * (except the system filetypes settings @see Build Menu Configuration
484 * section of the Manual).
486 * @param src the source of the menu item to remove.
487 * @param grp the group of the command to remove.
488 * @param cmd the index (from 0) of the command within the group. A negative
489 * value will remove the whole group.
491 * If any parameter is out of range does nothing.
493 * @see build_menu_update
495 void build_remove_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, gint cmd)
497 GeanyBuildCommand *bc;
498 gint i;
500 bc = get_build_group(src, grp);
501 if (bc == NULL)
502 return;
503 if (cmd < 0)
505 for (i = 0; i < build_groups_count[grp]; ++i)
506 bc[i].exists = FALSE;
508 else if (cmd < build_groups_count[grp])
509 bc[cmd].exists = FALSE;
513 /* * Get the @a GeanyBuildCommand structure for the specified Build menu item.
515 * Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
516 * hidden by higher priority commands.
518 * @param src the source of the specified menu item.
519 * @param grp the group of the specified menu item.
520 * @param cmd the index of the command within the group.
522 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
523 * This is a pointer to an internal structure and must not be freed.
525 * @see build_menu_update
527 GeanyBuildCommand *build_get_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, gint cmd)
529 GeanyBuildCommand *bc;
531 if (src >= GEANY_BCS_COUNT || grp >= GEANY_GBG_COUNT || cmd >= build_groups_count[grp])
532 return NULL;
533 bc = get_build_group(src, grp);
534 if (bc == NULL)
535 return NULL;
536 return &(bc[cmd]);
540 /* * Get the @a GeanyBuildCommand structure for the menu item.
542 * Get the current highest priority command specified by @a grp and @a cmd. This is the one
543 * that the menu item will use if activated.
545 * @param grp the group of the specified menu item.
546 * @param cmd the index of the command within the group.
547 * @param src pointer to @a gint to return which source provided the command. Ignored if @a NULL.
548 * Values are one of @a GeanyBuildSource but returns a signed type not the enum.
550 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
551 * This is a pointer to an internal structure and must not be freed.
553 * @see build_menu_update
555 /* parameter checked version of get_build_cmd for external interface */
556 GeanyBuildCommand *build_get_current_menu_item(GeanyBuildGroup grp, gint cmd, gint *src)
558 if (*src >= GEANY_BCS_COUNT || grp >= GEANY_GBG_COUNT || cmd >= build_groups_count[grp])
559 return NULL;
560 return get_build_cmd(NULL, grp, cmd, src);
564 /* Clear all error indicators in all documents. */
565 static void clear_errors(GeanyDocument *doc)
567 guint i;
569 for (i = 0; i < documents_array->len; i++)
571 if (documents[i]->is_valid)
572 editor_indicator_clear_errors(documents[i]->editor);
577 #ifdef SYNC_SPAWN
578 static void parse_build_output(const gchar **output, gint status)
580 guint x, i, len;
581 gchar *line, **lines;
583 for (x = 0; x < 2; x++)
585 if (NZV(output[x]))
587 lines = g_strsplit_set(output[x], "\r\n", -1);
588 len = g_strv_length(lines);
590 for (i = 0; i < len; i++)
592 if (NZV(lines[i]))
594 line = lines[i];
595 while (*line != '\0')
596 { /* replace any control characters in the output */
597 if (*line < 32)
598 *line = 32;
599 line++;
601 process_build_output_line(lines[i], COLOR_BLACK);
604 g_strfreev(lines);
608 show_build_result_message(status != 0);
609 utils_beep();
611 build_info.pid = 0;
612 /* enable build items again */
613 build_menu_update(NULL);
615 #endif
618 /* Replaces occurences of %e and %p with the appropriate filenames,
619 * %d and %p replacements should be in UTF8 */
620 static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *src)
622 GString *stack;
623 gchar *filename = NULL;
624 gchar *replacement;
625 gchar *executable = NULL;
626 gchar *ret_str; /* to be freed when not in use anymore */
628 stack = g_string_new(src);
629 if (doc != NULL && doc->file_name != NULL)
631 filename = utils_get_utf8_from_locale(doc->file_name);
633 /* replace %f with the filename (including extension) */
634 replacement = g_path_get_basename(filename);
635 utils_string_replace_all(stack, "%f", replacement);
636 g_free(replacement);
638 /* replace %d with the absolute path of the dir of the current file */
639 replacement = g_path_get_dirname(filename);
640 utils_string_replace_all(stack, "%d", replacement);
641 g_free(replacement);
643 /* replace %e with the filename (excluding extension) */
644 executable = utils_remove_ext_from_filename(filename);
645 replacement = g_path_get_basename(executable);
646 utils_string_replace_all(stack, "%e", replacement);
647 g_free(replacement);
650 /* replace %p with the current project's (absolute) base directory */
651 replacement = NULL; /* prevent double free if no replacement found */
652 if (app->project)
654 replacement = project_get_base_path();
656 else if (strstr(stack->str, "%p"))
657 { /* fall back to %d */
658 ui_set_statusbar(FALSE, _("failed to substitute %%p, no project active"));
659 if (doc != NULL && filename != NULL)
660 replacement = g_path_get_dirname(filename);
663 utils_string_replace_all(stack, "%p", replacement);
664 g_free(replacement);
666 ret_str = utils_get_utf8_from_locale(stack->str);
667 g_free(executable);
668 g_free(filename);
669 g_string_free(stack, TRUE);
671 return ret_str; /* don't forget to free src also if needed */
675 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
676 * idx document directory */
677 static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
679 GError *error = NULL;
680 gchar **argv;
681 gchar *working_dir;
682 gchar *utf8_working_dir;
683 gchar *cmd_string;
684 gchar *utf8_cmd_string;
685 #ifdef SYNC_SPAWN
686 gchar *output[2];
687 gint status;
688 #else
689 gint stdout_fd;
690 gint stderr_fd;
691 #endif
693 if (!((doc != NULL && NZV(doc->file_name)) || NZV(dir)))
695 geany_debug("Failed to run command with no working directory");
696 ui_set_statusbar(TRUE, _("Process failed, no working directory"));
697 return (GPid) 1;
700 if (doc != NULL)
701 clear_errors(doc);
702 setptr(current_dir_entered, NULL);
704 cmd_string = g_strdup(cmd);
706 #ifdef G_OS_WIN32
707 argv = g_strsplit(cmd_string, " ", 0);
708 #else
709 argv = g_new0(gchar *, 4);
710 argv[0] = g_strdup("/bin/sh");
711 argv[1] = g_strdup("-c");
712 argv[2] = cmd_string;
713 argv[3] = NULL;
714 #endif
716 utf8_cmd_string = utils_get_utf8_from_locale(cmd_string);
717 utf8_working_dir = NZV(dir) ? g_strdup(dir) : g_path_get_dirname(doc->file_name);
718 working_dir = utils_get_locale_from_utf8(utf8_working_dir);
720 gtk_list_store_clear(msgwindow.store_compiler);
721 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
722 msgwin_compiler_add(COLOR_BLUE, _("%s (in directory: %s)"), utf8_cmd_string, utf8_working_dir);
723 g_free(utf8_working_dir);
724 g_free(utf8_cmd_string);
726 /* set the build info for the message window */
727 g_free(build_info.dir);
728 build_info.dir = g_strdup(working_dir);
729 build_info.file_type_id = (doc == NULL) ? GEANY_FILETYPES_NONE : doc->file_type->id;
730 build_info.message_count = 0;
732 #ifdef SYNC_SPAWN
733 if (! utils_spawn_sync(working_dir, argv, NULL, G_SPAWN_SEARCH_PATH,
734 NULL, NULL, &output[0], &output[1], &status, &error))
735 #else
736 if (! g_spawn_async_with_pipes(working_dir, argv, NULL,
737 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
738 &(build_info.pid), NULL, &stdout_fd, &stderr_fd, &error))
739 #endif
741 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
742 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
743 g_strfreev(argv);
744 g_error_free(error);
745 g_free(working_dir);
746 error = NULL;
747 return (GPid) 0;
750 #ifdef SYNC_SPAWN
751 parse_build_output((const gchar**) output, status);
752 g_free(output[0]);
753 g_free(output[1]);
754 #else
755 if (build_info.pid > 0)
757 g_child_watch_add(build_info.pid, (GChildWatchFunc) build_exit_cb, NULL);
758 build_menu_update(doc);
759 ui_progress_bar_start(NULL);
762 /* use GIOChannels to monitor stdout and stderr */
763 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
764 TRUE, build_iofunc, GINT_TO_POINTER(0));
765 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
766 TRUE, build_iofunc, GINT_TO_POINTER(1));
767 #endif
769 g_strfreev(argv);
770 g_free(working_dir);
772 return build_info.pid;
776 /* Returns: NULL if there was an error, or the working directory the script was created in.
777 * vte_cmd_nonscript is the location of a string which is filled with the command to be used
778 * when vc->skip_run_script is set, otherwise it will be set to NULL */
779 static gchar *prepare_run_script(GeanyDocument *doc, gchar **vte_cmd_nonscript, gint cmdindex)
781 gchar *locale_filename = NULL;
782 GeanyBuildCommand *cmd = NULL;
783 gchar *executable = NULL;
784 gchar *working_dir = NULL;
785 const gchar *cmd_working_dir;
786 gboolean autoclose = FALSE;
787 gboolean result = FALSE;
788 gchar *tmp;
789 gchar *cmd_string;
791 if (vte_cmd_nonscript != NULL)
792 *vte_cmd_nonscript = NULL;
794 locale_filename = utils_get_locale_from_utf8(doc->file_name);
796 cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
798 cmd_string = build_replace_placeholder(doc, cmd->command);
799 cmd_working_dir = cmd->working_dir;
800 if (! NZV(cmd_working_dir))
801 cmd_working_dir = "%d";
802 working_dir = build_replace_placeholder(doc, cmd_working_dir); /* in utf-8 */
804 /* only test whether working dir exists, don't change it or else Windows support will break
805 * (gspawn-win32-helper.exe is used by GLib and must be in $PATH which means current working
806 * dir where geany.exe was started from, so we can't change it) */
807 if (!NZV(working_dir) || ! g_file_test(working_dir, G_FILE_TEST_EXISTS) ||
808 ! g_file_test(working_dir, G_FILE_TEST_IS_DIR))
810 ui_set_statusbar(TRUE, _("Failed to change the working directory to \"%s\""),
811 NZV(working_dir) ? working_dir : "<NULL>" );
812 utils_free_pointers(2, cmd_string, working_dir, NULL);
813 return NULL;
816 #ifdef HAVE_VTE
817 if (vte_info.have_vte && vc->run_in_vte)
819 if (vc->skip_run_script)
821 if (vte_cmd_nonscript != NULL)
822 *vte_cmd_nonscript = cmd_string;
824 utils_free_pointers(2, executable, locale_filename, NULL);
825 return working_dir;
827 else
828 /* don't wait for user input at the end of script when we are running in VTE */
829 autoclose = TRUE;
831 #endif
833 /* RUN_SCRIPT_CMD should be ok in UTF8 without converting in locale because it
834 * contains no umlauts */
835 tmp = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
836 result = build_create_shellscript(tmp, cmd_string, autoclose);
837 if (! result)
839 ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created)"),
840 NZV(cmd_string) ? cmd_string : NULL);
843 utils_free_pointers(4, cmd_string, tmp, executable, locale_filename, NULL);
845 if (result)
846 return working_dir;
848 g_free(working_dir);
849 return NULL;
853 static GPid build_run_cmd(GeanyDocument *doc, gint cmdindex)
855 gchar *working_dir;
856 gchar *vte_cmd_nonscript = NULL;
857 GError *error = NULL;
859 if (doc == NULL || doc->file_name == NULL)
860 return (GPid) 0;
862 working_dir = prepare_run_script(doc, &vte_cmd_nonscript, cmdindex);
863 if (working_dir == NULL)
864 return (GPid) 0;
866 run_info[cmdindex].file_type_id = doc->file_type->id;
868 #ifdef HAVE_VTE
869 if (vte_info.have_vte && vc->run_in_vte)
871 gchar *vte_cmd;
873 if (vc->skip_run_script)
875 setptr(vte_cmd_nonscript, utils_get_utf8_from_locale(vte_cmd_nonscript));
876 vte_cmd = g_strconcat(vte_cmd_nonscript, "\n", NULL);
877 g_free(vte_cmd_nonscript);
879 else
880 vte_cmd = g_strconcat("\n/bin/sh ", RUN_SCRIPT_CMD, "\n", NULL);
882 /* change into current directory if it is not done by default */
883 if (! vc->follow_path)
885 /* we need to convert the working_dir back to UTF-8 because the VTE expects it */
886 gchar *utf8_working_dir = utils_get_utf8_from_locale(working_dir);
887 vte_cwd(utf8_working_dir, TRUE);
888 g_free(utf8_working_dir);
890 if (! vte_send_cmd(vte_cmd))
892 ui_set_statusbar(FALSE,
893 _("Could not execute the file in the VTE because it probably contains a command."));
894 geany_debug("Could not execute the file in the VTE because it probably contains a command.");
897 /* show the VTE */
898 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
899 gtk_widget_grab_focus(vc->vte);
900 msgwin_show_hide(TRUE);
902 run_info[cmdindex].pid = 1;
904 g_free(vte_cmd);
906 else
907 #endif
909 gchar *locale_term_cmd = NULL;
910 gchar **term_argv = NULL;
911 guint term_argv_len, i;
912 gchar **argv = NULL;
914 /* get the terminal path */
915 locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
916 /* split the term_cmd, so arguments will work too */
917 term_argv = g_strsplit(locale_term_cmd, " ", -1);
918 term_argv_len = g_strv_length(term_argv);
920 /* check that terminal exists (to prevent misleading error messages) */
921 if (term_argv[0] != NULL)
923 gchar *tmp = term_argv[0];
924 /* g_find_program_in_path checks whether tmp exists and is executable */
925 term_argv[0] = g_find_program_in_path(tmp);
926 g_free(tmp);
928 if (term_argv[0] == NULL)
930 ui_set_statusbar(TRUE,
931 _("Could not find terminal \"%s\" "
932 "(check path for Terminal tool setting in Preferences)"), tool_prefs.term_cmd);
933 run_info[cmdindex].pid = (GPid) 1;
934 goto free_strings;
937 argv = g_new0(gchar *, term_argv_len + 3);
938 for (i = 0; i < term_argv_len; i++)
940 argv[i] = g_strdup(term_argv[i]);
942 #ifdef G_OS_WIN32
943 /* command line arguments only for cmd.exe */
944 if (strstr(argv[0], "cmd.exe") != NULL)
946 argv[term_argv_len] = g_strdup("/Q /C");
947 argv[term_argv_len + 1] = g_strdup(RUN_SCRIPT_CMD);
949 else
951 argv[term_argv_len] = g_strdup(RUN_SCRIPT_CMD);
952 argv[term_argv_len + 1] = NULL;
954 #else
955 argv[term_argv_len ] = g_strdup("-e");
956 argv[term_argv_len + 1] = g_strconcat("/bin/sh ", RUN_SCRIPT_CMD, NULL);
957 #endif
958 argv[term_argv_len + 2] = NULL;
960 if (! g_spawn_async(working_dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
961 NULL, NULL, &(run_info[cmdindex].pid), &error))
963 geany_debug("g_spawn_async() failed: %s", error->message);
964 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
965 g_unlink(RUN_SCRIPT_CMD);
966 g_error_free(error);
967 error = NULL;
968 run_info[cmdindex].pid = (GPid) 0;
971 if (run_info[cmdindex].pid > 0)
973 g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
974 (gpointer)&(run_info[cmdindex]));
975 build_menu_update(doc);
977 free_strings:
978 g_strfreev(argv);
979 g_strfreev(term_argv);
980 g_free(locale_term_cmd);
983 g_free(working_dir);
984 return run_info[cmdindex].pid;
988 static void process_build_output_line(const gchar *str, gint color)
990 gchar *msg, *tmp;
991 gchar *filename;
992 gint line;
994 msg = g_strdup(str);
996 g_strchomp(msg);
998 if (! NZV(msg))
1000 g_free(msg);
1001 return;
1004 if (build_parse_make_dir(msg, &tmp))
1006 setptr(current_dir_entered, tmp);
1008 msgwin_parse_compiler_error_line(msg, current_dir_entered, &filename, &line);
1010 if (line != -1 && filename != NULL)
1012 GeanyDocument *doc = document_find_by_filename(filename);
1014 /* limit number of indicators */
1015 if (doc && editor_prefs.use_indicators &&
1016 build_info.message_count < GEANY_BUILD_ERR_HIGHLIGHT_MAX)
1018 if (line > 0) /* some compilers, like pdflatex report errors on line 0 */
1019 line--; /* so only adjust the line number if it is greater than 0 */
1020 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line);
1022 build_info.message_count++;
1023 color = COLOR_RED; /* error message parsed on the line */
1025 g_free(filename);
1027 msgwin_compiler_add_string(color, msg);
1028 g_free(msg);
1032 #ifndef SYNC_SPAWN
1033 static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
1035 if (cond & (G_IO_IN | G_IO_PRI))
1037 gchar *msg;
1038 GIOStatus st;
1040 while ((st = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL)) == G_IO_STATUS_NORMAL && msg)
1042 gint color = (GPOINTER_TO_INT(data)) ? COLOR_DARK_RED : COLOR_BLACK;
1044 process_build_output_line(msg, color);
1045 g_free(msg);
1047 if (st == G_IO_STATUS_ERROR || st == G_IO_STATUS_EOF) return FALSE;
1049 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
1050 return FALSE;
1052 return TRUE;
1054 #endif
1057 gboolean build_parse_make_dir(const gchar *string, gchar **prefix)
1059 const gchar *pos;
1061 *prefix = NULL;
1063 if (string == NULL)
1064 return FALSE;
1066 if ((pos = strstr(string, "Entering directory")) != NULL)
1068 gsize len;
1069 gchar *input;
1071 /* get the start of the path */
1072 pos = strstr(string, "/");
1074 if (pos == NULL)
1075 return FALSE;
1077 input = g_strdup(pos);
1079 /* kill the ' at the end of the path */
1080 len = strlen(input);
1081 input[len - 1] = '\0';
1082 input = g_realloc(input, len); /* shorten by 1 */
1083 *prefix = input;
1085 return TRUE;
1088 if (strstr(string, "Leaving directory") != NULL)
1090 *prefix = NULL;
1091 return TRUE;
1094 return FALSE;
1098 static void show_build_result_message(gboolean failure)
1100 gchar *msg;
1102 if (failure)
1104 msg = _("Compilation failed.");
1105 msgwin_compiler_add_string(COLOR_BLUE, msg);
1106 /* If msgwindow is hidden, user will want to display it to see the error */
1107 if (! ui_prefs.msgwindow_visible)
1109 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1110 msgwin_show_hide(TRUE);
1112 else
1113 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1114 ui_set_statusbar(FALSE, "%s", msg);
1116 else
1118 msg = _("Compilation finished successfully.");
1119 msgwin_compiler_add_string(COLOR_BLUE, msg);
1120 if (! ui_prefs.msgwindow_visible ||
1121 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1122 ui_set_statusbar(FALSE, "%s", msg);
1127 #ifndef SYNC_SPAWN
1128 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data)
1130 gboolean failure = FALSE;
1132 #ifdef G_OS_WIN32
1133 failure = status;
1134 #else
1135 if (WIFEXITED(status))
1137 if (WEXITSTATUS(status) != EXIT_SUCCESS)
1138 failure = TRUE;
1140 else if (WIFSIGNALED(status))
1142 /* the terminating signal: WTERMSIG (status)); */
1143 failure = TRUE;
1145 else
1146 { /* any other failure occured */
1147 failure = TRUE;
1149 #endif
1150 show_build_result_message(failure);
1152 utils_beep();
1153 g_spawn_close_pid(child_pid);
1155 build_info.pid = 0;
1156 /* enable build items again */
1157 build_menu_update(NULL);
1158 ui_progress_bar_stop();
1160 #endif
1163 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data)
1165 RunInfo *run_info_data = user_data;
1167 g_spawn_close_pid(child_pid);
1169 run_info_data->pid = 0;
1170 /* reset the stop button and menu item to the original meaning */
1171 build_menu_update(NULL);
1175 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1176 * fname is the full file name (including path) for the script to create */
1177 static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose)
1179 FILE *fp;
1180 gchar *str;
1181 #ifdef G_OS_WIN32
1182 gchar *expanded_cmd;
1183 #endif
1185 fp = g_fopen(fname, "w");
1186 if (! fp)
1187 return FALSE;
1188 #ifdef G_OS_WIN32
1189 /* Expand environment variables like %blah%. */
1190 expanded_cmd = win32_expand_environment_variables(cmd);
1191 str = g_strdup_printf("%s\n\n%s\ndel \"%%0\"\n\npause\n", expanded_cmd, (autoclose) ? "" : "pause");
1192 g_free(expanded_cmd);
1193 #else
1194 str = g_strdup_printf(
1195 "#!/bin/sh\n\nrm $0\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1196 \n\n%s\n", cmd, (autoclose) ? "" :
1197 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1198 "dash\ndummy_var=\"\"\nread dummy_var");
1199 #endif
1201 fputs(str, fp);
1202 g_free(str);
1204 fclose(fp);
1206 return TRUE;
1210 typedef void Callback(GtkWidget *w, gpointer u);
1212 /* run the command catenating cmd_cat if present */
1213 static void build_command(GeanyDocument *doc, GeanyBuildGroup grp, gint cmd, gchar *cmd_cat)
1215 gchar *dir;
1216 gchar *full_command, *subs_command;
1217 GeanyBuildCommand *buildcmd = get_build_cmd(doc, grp, cmd, NULL);
1218 gchar *cmdstr;
1220 if (buildcmd == NULL)
1221 return;
1223 cmdstr = buildcmd->command;
1225 if (cmd_cat != NULL)
1227 if (cmdstr != NULL)
1228 full_command = g_strconcat(cmdstr, cmd_cat, NULL);
1229 else
1230 full_command = g_strdup(cmd_cat);
1232 else
1233 full_command = cmdstr;
1235 dir = build_replace_placeholder(doc, buildcmd->working_dir);
1236 subs_command = build_replace_placeholder(doc, full_command);
1237 build_info.grp = grp;
1238 build_info.cmd = cmd;
1239 build_spawn_cmd(doc, subs_command, dir);
1240 g_free(subs_command);
1241 g_free(dir);
1242 if (cmd_cat != NULL)
1243 g_free(full_command);
1244 build_menu_update(doc);
1249 /*----------------------------------------------------------------
1251 * Create build menu and handle callbacks (&toolbar callbacks)
1253 *----------------------------------------------------------------*/
1254 static void on_make_custom_input_response(const gchar *input)
1256 GeanyDocument *doc = document_get_current();
1258 setptr(build_info.custom_target, g_strdup(input));
1259 build_command(doc, GBO_TO_GBG(GEANY_GBO_CUSTOM), GBO_TO_CMD(GEANY_GBO_CUSTOM),
1260 build_info.custom_target);
1264 static void on_build_menu_item(GtkWidget *w, gpointer user_data)
1266 GeanyDocument *doc = document_get_current();
1267 GeanyBuildCommand *bc;
1268 gint grp = GPOINTER_TO_GRP(user_data);
1269 gint cmd = GPOINTER_TO_CMD(user_data);
1271 if (doc && doc->changed)
1273 if (!document_save_file(doc, FALSE))
1274 return;
1276 g_signal_emit_by_name(geany_object, "build-start");
1278 if (grp == GEANY_GBG_NON_FT && cmd == GBO_TO_CMD(GEANY_GBO_CUSTOM))
1280 static GtkWidget *dialog = NULL; /* keep dialog for combo history */
1282 if (! dialog)
1284 dialog = dialogs_show_input_persistent(_("Custom Text"), GTK_WINDOW(main_widgets.window),
1285 _("Enter custom text here, all entered text is appended to the command."),
1286 build_info.custom_target, &on_make_custom_input_response);
1288 else
1290 gtk_widget_show(dialog);
1292 return;
1294 else if (grp == GEANY_GBG_EXEC)
1296 if (run_info[cmd].pid > (GPid) 1)
1298 kill_process(&run_info[cmd].pid);
1299 return;
1301 bc = get_build_cmd(doc, grp, cmd, NULL);
1302 if (bc != NULL && strcmp(bc->command, "builtin") == 0)
1304 gchar *uri;
1305 if (doc == NULL)
1306 return;
1307 uri = g_strconcat("file:///", g_path_skip_root(doc->file_name), NULL);
1308 utils_open_browser(uri);
1309 g_free(uri);
1312 else
1313 build_run_cmd(doc, cmd);
1315 else
1316 build_command(doc, grp, cmd, NULL);
1320 /* group codes for menu items other than the known commands
1321 * value order is important, see the following table for use */
1323 /* the rest in each group */
1324 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1325 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1326 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1327 /* the separator */
1328 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1329 /* the fixed items */
1330 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1331 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1332 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1333 #define MENU_DONE (MENU_COMMANDS + 1)
1336 static struct BuildMenuItemSpec {
1337 const gchar *stock_id;
1338 const gint key_binding;
1339 const gint build_grp;
1340 const gint build_cmd;
1341 const gchar *fix_label;
1342 Callback *cb;
1343 } build_menu_specs[] = {
1344 {GTK_STOCK_CONVERT, GEANY_KEYS_BUILD_COMPILE, GBO_TO_GBG(GEANY_GBO_COMPILE),
1345 GBO_TO_CMD(GEANY_GBO_COMPILE), NULL, on_build_menu_item},
1346 {GEANY_STOCK_BUILD, GEANY_KEYS_BUILD_LINK, GBO_TO_GBG(GEANY_GBO_BUILD),
1347 GBO_TO_CMD(GEANY_GBO_BUILD), NULL, on_build_menu_item},
1348 {NULL, -1, MENU_FT_REST,
1349 GBO_TO_CMD(GEANY_GBO_BUILD) + 1, NULL, on_build_menu_item},
1350 {NULL, -1, MENU_SEPARATOR,
1351 GBF_SEP_1, NULL, NULL},
1352 {NULL, GEANY_KEYS_BUILD_MAKE, GBO_TO_GBG(GEANY_GBO_MAKE_ALL),
1353 GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL, on_build_menu_item},
1354 {NULL, GEANY_KEYS_BUILD_MAKEOWNTARGET, GBO_TO_GBG(GEANY_GBO_CUSTOM),
1355 GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL, on_build_menu_item},
1356 {NULL, GEANY_KEYS_BUILD_MAKEOBJECT, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT),
1357 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL, on_build_menu_item},
1358 {NULL, -1, MENU_NON_FT_REST,
1359 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT) + 1, NULL, on_build_menu_item},
1360 {NULL, -1, MENU_SEPARATOR,
1361 GBF_SEP_2, NULL, NULL},
1362 {GTK_STOCK_GO_DOWN, GEANY_KEYS_BUILD_NEXTERROR, MENU_NEXT_ERROR,
1363 GBF_NEXT_ERROR, N_("_Next Error"), on_build_next_error},
1364 {GTK_STOCK_GO_UP, GEANY_KEYS_BUILD_PREVIOUSERROR, MENU_PREV_ERROR,
1365 GBF_PREV_ERROR, N_("_Previous Error"), on_build_previous_error},
1366 {NULL, -1, MENU_SEPARATOR,
1367 GBF_SEP_3, NULL, NULL},
1368 {GTK_STOCK_EXECUTE, GEANY_KEYS_BUILD_RUN, GBO_TO_GBG(GEANY_GBO_EXEC),
1369 GBO_TO_CMD(GEANY_GBO_EXEC), NULL, on_build_menu_item},
1370 {NULL, -1, MENU_EXEC_REST,
1371 GBO_TO_CMD(GEANY_GBO_EXEC) + 1, NULL, on_build_menu_item},
1372 {NULL, -1, MENU_SEPARATOR,
1373 GBF_SEP_4, NULL, NULL},
1374 {GTK_STOCK_PREFERENCES, GEANY_KEYS_BUILD_OPTIONS, MENU_COMMANDS,
1375 GBF_COMMANDS, N_("_Set Build Commands"), on_set_build_commands_activate},
1376 {NULL, -1, MENU_DONE,
1377 0, NULL, NULL}
1381 static void create_build_menu_item(GtkWidget *menu, GeanyKeyGroup *group, GtkAccelGroup *ag,
1382 struct BuildMenuItemSpec *bs, const gchar *lbl, gint grp, gint cmd)
1384 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(lbl);
1386 if (bs->stock_id != NULL)
1388 GtkWidget *image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1389 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1391 gtk_widget_show(item);
1392 if (bs->key_binding >= 0)
1393 add_menu_accel(group, (guint) bs->key_binding, ag, item);
1394 gtk_container_add(GTK_CONTAINER(menu), item);
1395 if (bs->cb != NULL)
1397 g_signal_connect(item, "activate", G_CALLBACK(bs->cb), GRP_CMD_TO_POINTER(grp,cmd));
1399 menu_items.menu_item[grp][cmd] = item;
1403 static void create_build_menu(BuildMenuItems *build_menu_items)
1405 GtkWidget *menu;
1406 GtkAccelGroup *accel_group = gtk_accel_group_new();
1407 GeanyKeyGroup *keygroup = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
1408 gint i, j;
1410 menu = gtk_menu_new();
1411 build_menu_items->menu_item[GEANY_GBG_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_FT]);
1412 build_menu_items->menu_item[GEANY_GBG_NON_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_NON_FT]);
1413 build_menu_items->menu_item[GEANY_GBG_EXEC] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_EXEC]);
1414 build_menu_items->menu_item[GBG_FIXED] = g_new0(GtkWidget*, GBF_COUNT);
1416 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1418 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1419 if (bs->build_grp == MENU_SEPARATOR)
1421 GtkWidget *item = gtk_separator_menu_item_new();
1422 gtk_widget_show(item);
1423 gtk_container_add(GTK_CONTAINER(menu), item);
1424 build_menu_items->menu_item[GBG_FIXED][bs->build_cmd] = item;
1426 else if (bs->fix_label != NULL)
1428 create_build_menu_item(menu, keygroup, accel_group, bs, _(bs->fix_label),
1429 GBG_FIXED, bs->build_cmd);
1431 else if (bs->build_grp >= MENU_FT_REST && bs->build_grp <= MENU_SEPARATOR)
1433 gint grp = bs->build_grp - GEANY_GBG_COUNT;
1434 for (j = bs->build_cmd; j < build_groups_count[grp]; ++j)
1436 GeanyBuildCommand *bc = get_build_cmd(NULL, grp, j, NULL);
1437 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1438 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, grp, j);
1441 else
1443 GeanyBuildCommand *bc = get_build_cmd(NULL, bs->build_grp, bs->build_cmd, NULL);
1444 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1445 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, bs->build_grp, bs->build_cmd);
1448 build_menu_items->menu = menu;
1449 gtk_widget_show(menu);
1450 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_build1")), menu);
1454 /* portability to various GTK versions needs checking
1455 * conforms to description of gtk_accel_label as child of menu item
1456 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1457 static void geany_menu_item_set_label(GtkWidget *w, const gchar *label)
1459 GtkWidget *c = gtk_bin_get_child(GTK_BIN(w));
1461 gtk_label_set_text_with_mnemonic(GTK_LABEL(c), label);
1465 /* * Update the build menu to reflect changes in configuration or status.
1467 * Sets the labels and number of visible items to match the highest
1468 * priority configured commands. Also sets sensitivity if build commands are
1469 * running and switches executes to stop when commands are running.
1471 * @param doc The current document, if available, to save looking it up.
1472 * If @c NULL it will be looked up.
1474 * Call this after modifying any fields of a GeanyBuildCommand structure.
1476 * @see Build Menu Configuration section of the Manual.
1479 void build_menu_update(GeanyDocument *doc)
1481 gint i, cmdcount, cmd, grp;
1482 gboolean vis = FALSE;
1483 gboolean have_path, build_running, exec_running, have_errors, cmd_sensitivity;
1484 gboolean can_compile, can_build, can_make, run_sensitivity = FALSE, run_running = FALSE;
1485 GeanyBuildCommand *bc;
1487 if (menu_items.menu == NULL)
1488 create_build_menu(&menu_items);
1489 if (doc == NULL)
1490 doc = document_get_current();
1491 have_path = doc != NULL && doc->file_name != NULL;
1492 build_running = build_info.pid > (GPid) 1;
1493 have_errors = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_compiler), NULL) > 0;
1494 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1496 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1497 switch (bs->build_grp)
1499 case MENU_SEPARATOR:
1500 if (vis == TRUE)
1502 gtk_widget_show_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1503 vis = FALSE;
1505 else
1506 gtk_widget_hide_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1507 break;
1508 case MENU_NEXT_ERROR:
1509 case MENU_PREV_ERROR:
1510 gtk_widget_set_sensitive(menu_items.menu_item[GBG_FIXED][bs->build_cmd], have_errors);
1511 vis |= TRUE;
1512 break;
1513 case MENU_COMMANDS:
1514 vis |= TRUE;
1515 break;
1516 default: /* all configurable commands */
1517 if (bs->build_grp >= GEANY_GBG_COUNT)
1519 grp = bs->build_grp - GEANY_GBG_COUNT;
1520 cmdcount = build_groups_count[grp];
1522 else
1524 grp = bs->build_grp;
1525 cmdcount = bs->build_cmd + 1;
1527 for (cmd = bs->build_cmd; cmd < cmdcount; ++cmd)
1529 GtkWidget *menu_item = menu_items.menu_item[grp][cmd];
1530 const gchar *label;
1531 bc = get_build_cmd(doc, grp, cmd, NULL);
1532 if (bc)
1533 label = bc->label;
1534 else
1535 label = NULL;
1537 if (grp < GEANY_GBG_EXEC)
1539 cmd_sensitivity =
1540 (grp == GEANY_GBG_FT && bc != NULL && have_path && ! build_running) ||
1541 (grp == GEANY_GBG_NON_FT && bc != NULL && ! build_running);
1542 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1543 if (bc != NULL && NZV(label))
1545 geany_menu_item_set_label(menu_item, label);
1546 gtk_widget_show_all(menu_item);
1547 vis |= TRUE;
1549 else
1550 gtk_widget_hide_all(menu_item);
1552 else
1554 GtkWidget *image;
1555 exec_running = run_info[cmd].pid > (GPid) 1;
1556 cmd_sensitivity = (bc != NULL) || exec_running;
1557 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1558 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1559 run_sensitivity = cmd_sensitivity;
1560 if (! exec_running)
1562 image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1564 else
1566 image = gtk_image_new_from_stock(GTK_STOCK_STOP, GTK_ICON_SIZE_MENU);
1568 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1569 run_running = exec_running;
1570 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
1571 if (bc != NULL && NZV(label))
1573 geany_menu_item_set_label(menu_item, label);
1574 gtk_widget_show_all(menu_item);
1575 vis |= TRUE;
1577 else
1578 gtk_widget_hide_all(menu_item);
1584 run_sensitivity &= (doc != NULL);
1585 can_build = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_BUILD), NULL) != NULL
1586 && have_path && ! build_running;
1587 if (widgets.toolitem_build != NULL)
1588 gtk_widget_set_sensitive(widgets.toolitem_build, can_build);
1589 can_make = FALSE;
1590 if (widgets.toolitem_make_all != NULL)
1591 gtk_widget_set_sensitive(widgets.toolitem_make_all,
1592 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL) != NULL
1593 && ! build_running));
1594 if (widgets.toolitem_make_custom != NULL)
1595 gtk_widget_set_sensitive(widgets.toolitem_make_custom,
1596 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL) != NULL
1597 && ! build_running));
1598 if (widgets.toolitem_make_object != NULL)
1599 gtk_widget_set_sensitive(widgets.toolitem_make_object,
1600 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL) != NULL
1601 && ! build_running));
1602 if (widgets.toolitem_set_args != NULL)
1603 gtk_widget_set_sensitive(widgets.toolitem_set_args, TRUE);
1605 can_compile = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_COMPILE), NULL) != NULL
1606 && have_path && ! build_running;
1607 gtk_action_set_sensitive(widgets.compile_action, can_compile);
1608 gtk_action_set_sensitive(widgets.build_action, can_make);
1609 gtk_action_set_sensitive(widgets.run_action, run_sensitivity);
1611 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1612 set_stop_button(run_running);
1617 /* Call build_menu_update() instead of calling this directly. */
1618 static void set_stop_button(gboolean stop)
1620 const gchar *button_stock_id = NULL;
1621 GtkToolButton *run_button;
1623 run_button = GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1624 if (run_button != NULL)
1625 button_stock_id = gtk_tool_button_get_stock_id(run_button);
1627 if (stop && utils_str_equal(button_stock_id, GTK_STOCK_STOP))
1628 return;
1629 if (! stop && utils_str_equal(button_stock_id, GTK_STOCK_EXECUTE))
1630 return;
1632 /* use the run button also as stop button */
1633 if (stop)
1635 if (run_button != NULL)
1636 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_STOP);
1638 else
1640 if (run_button != NULL)
1641 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_EXECUTE);
1646 static void on_set_build_commands_activate(GtkWidget *w, gpointer u)
1648 /* For now, just show the project dialog */
1649 if (app->project)
1650 project_build_properties();
1651 else
1652 show_build_commands_dialog();
1656 static void on_toolbutton_build_activate(GtkWidget *menuitem, gpointer user_data)
1658 last_toolbutton_action = user_data;
1659 g_object_set(widgets.build_action, "tooltip", _("Build the current file"), NULL);
1660 on_build_menu_item(menuitem, user_data);
1664 static void on_toolbutton_make_activate(GtkWidget *menuitem, gpointer user_data)
1666 gchar *msg;
1668 last_toolbutton_action = user_data;
1669 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_ALL))
1670 msg = _("Build the current file with Make and the default target");
1671 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_CUSTOM))
1672 msg = _("Build the current file with Make and the specified target");
1673 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT))
1674 msg = _("Compile the current file with Make");
1675 else
1676 msg = NULL;
1677 g_object_set(widgets.build_action, "tooltip", msg, NULL);
1678 on_build_menu_item(menuitem, user_data);
1682 static void kill_process(GPid *pid)
1684 /* Unix: SIGQUIT is not the best signal to use because it causes a core dump (this should not
1685 * perforce necessary for just killing a process). But we must use a signal which we can
1686 * ignore because the main process get it too, it is declared to ignore in main.c. */
1687 gint result;
1689 #ifdef G_OS_WIN32
1690 g_return_if_fail(*pid != NULL);
1691 result = TerminateProcess(*pid, 0);
1692 /* TerminateProcess() returns TRUE on success, for the check below we have to convert
1693 * it to FALSE (and vice versa) */
1694 result = ! result;
1695 #else
1696 g_return_if_fail(*pid > 1);
1697 result = kill(*pid, SIGQUIT);
1698 #endif
1700 if (result != 0)
1701 ui_set_statusbar(TRUE, _("Process could not be stopped (%s)."), g_strerror(errno));
1702 else
1704 *pid = 0;
1705 build_menu_update(NULL);
1710 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data)
1712 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_compiler),
1713 msgwin_goto_compiler_file_line))
1715 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1717 else
1718 ui_set_statusbar(FALSE, _("No more build errors."));
1722 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data)
1724 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_compiler),
1725 msgwin_goto_compiler_file_line))
1727 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1729 else
1730 ui_set_statusbar(FALSE, _("No more build errors."));
1734 void build_toolbutton_build_clicked(GtkAction *action, gpointer unused)
1736 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_BUILD))
1738 on_build_menu_item(NULL, GBO_TO_POINTER(GEANY_GBO_BUILD));
1740 else
1742 on_build_menu_item(NULL, last_toolbutton_action);
1747 /*------------------------------------------------------
1749 * Create and handle the build menu configuration dialog
1751 *-------------------------------------------------------*/
1752 typedef struct RowWidgets
1754 GtkWidget *entries[GEANY_BC_CMDENTRIES_COUNT];
1755 GeanyBuildSource src;
1756 GeanyBuildSource dst;
1757 GeanyBuildCommand *cmdsrc;
1758 gint grp;
1759 gint cmd;
1760 gboolean cleared;
1761 gboolean used_dst;
1762 } RowWidgets;
1764 static GdkColor *insensitive_color;
1766 static void set_row_color(RowWidgets *r, GdkColor *color )
1768 enum GeanyBuildCmdEntries i;
1770 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1771 gtk_widget_modify_text(r->entries[i], GTK_STATE_NORMAL, color);
1775 static void set_build_command_entry_text(GtkWidget *wid, const gchar *text)
1777 if (GTK_IS_BUTTON(wid))
1778 gtk_button_set_label(GTK_BUTTON(wid), text);
1779 else
1780 gtk_entry_set_text(GTK_ENTRY(wid), text);
1784 static void on_clear_dialog_row(GtkWidget *unused, gpointer user_data)
1786 RowWidgets *r = user_data;
1787 gint src;
1788 enum GeanyBuildCmdEntries i;
1789 GeanyBuildCommand *bc = get_next_build_cmd(NULL, r->grp, r->cmd, r->dst, &src);
1791 if (bc != NULL)
1793 r->cmdsrc = bc;
1794 r->src = src;
1795 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1797 set_build_command_entry_text(r->entries[i],
1798 id_to_str(bc,i) != NULL ? id_to_str(bc,i) : "");
1801 else
1803 r->cmdsrc = NULL;
1804 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1806 set_build_command_entry_text(r->entries[i], "");
1809 r->used_dst = FALSE;
1810 set_row_color(r, insensitive_color);
1811 r->cleared = TRUE;
1815 static void on_clear_dialog_regex_row(GtkEntry *regex, gpointer unused)
1817 gtk_entry_set_text(regex,"");
1821 static void on_label_button_clicked(GtkWidget *wid, gpointer user_data)
1823 RowWidgets *r = user_data;
1824 GtkWidget *top_level = gtk_widget_get_toplevel(wid);
1825 const gchar *old = gtk_button_get_label(GTK_BUTTON(wid));
1826 gchar *str;
1828 if (GTK_WIDGET_TOPLEVEL(top_level) && GTK_IS_WINDOW(top_level))
1829 str = dialogs_show_input(_("Set menu item label"), GTK_WINDOW(top_level), NULL, old);
1830 else
1831 str = dialogs_show_input(_("Set menu item label"), NULL, NULL, old);
1833 if (!str)
1834 return;
1836 gtk_button_set_label(GTK_BUTTON(wid), str);
1837 g_free(str);
1838 r->used_dst = TRUE;
1839 set_row_color(r, NULL);
1843 static void on_entry_focus(GtkWidget *wid, GdkEventFocus *unused, gpointer user_data)
1845 RowWidgets *r = user_data;
1847 r->used_dst = TRUE;
1848 set_row_color(r, NULL);
1852 /* Column headings, array NULL-terminated */
1853 static const gchar *colheads[] =
1855 "#",
1856 N_("Label"),
1857 N_("Command"),
1858 N_("Working directory"),
1859 N_("Reset"),
1860 NULL
1863 /* column names */
1864 #define DC_ITEM 0
1865 #define DC_ENTRIES 1
1866 #define DC_CLEAR 4
1867 #define DC_N_COL 5
1869 static const guint entry_x_padding = 3;
1870 static const guint entry_y_padding = 0;
1873 static RowWidgets *build_add_dialog_row(GeanyDocument *doc, GtkTable *table, guint row,
1874 GeanyBuildSource dst, gint grp, gint cmd, gboolean dir)
1876 GtkWidget *label, *clear, *clearicon;
1877 RowWidgets *roww;
1878 GeanyBuildCommand *bc;
1879 gint src;
1880 enum GeanyBuildCmdEntries i;
1881 guint column = 0;
1882 gchar *text;
1884 text = g_strdup_printf("%d.", cmd + 1);
1885 label = gtk_label_new(text);
1886 g_free(text);
1887 insensitive_color = &(gtk_widget_get_style(label)->text[GTK_STATE_INSENSITIVE]);
1888 gtk_table_attach(table, label, column, column + 1, row, row + 1, GTK_FILL,
1889 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1890 roww = g_new0(RowWidgets, 1);
1891 roww->src = GEANY_BCS_COUNT;
1892 roww->grp = grp;
1893 roww->cmd = cmd;
1894 roww->dst = dst;
1895 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1897 gint xflags = (i == GEANY_BC_COMMAND) ? GTK_FILL | GTK_EXPAND : GTK_FILL;
1899 column += 1;
1900 if (i == GEANY_BC_LABEL)
1902 GtkWidget *wid = roww->entries[i] = gtk_button_new();
1903 gtk_button_set_use_underline(GTK_BUTTON(wid), TRUE);
1904 gtk_widget_set_tooltip_text(wid, _("Click to set menu item label"));
1905 g_signal_connect(wid, "clicked", G_CALLBACK(on_label_button_clicked), roww);
1907 else
1909 roww->entries[i] = gtk_entry_new();
1910 g_signal_connect(roww->entries[i], "focus-in-event", G_CALLBACK(on_entry_focus), roww);
1912 gtk_table_attach(table, roww->entries[i], column, column + 1, row, row + 1, xflags,
1913 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1915 column++;
1916 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1917 clear = gtk_button_new();
1918 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1919 g_signal_connect(clear, "clicked", G_CALLBACK(on_clear_dialog_row), roww);
1920 gtk_table_attach(table, clear, column, column + 1, row, row + 1, GTK_FILL,
1921 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1922 roww->cmdsrc = bc = get_build_cmd(doc, grp, cmd, &src);
1923 if (bc != NULL)
1924 roww->src = src;
1926 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1928 const gchar *str = "";
1930 if (bc != NULL )
1932 if ((str = id_to_str(bc, i)) == NULL)
1933 str = "";
1934 else if ((gint)dst == src)
1935 roww->used_dst = TRUE;
1937 set_build_command_entry_text(roww->entries[i], str);
1939 if (bc != NULL && ((gint)dst > src))
1940 set_row_color(roww, insensitive_color);
1941 if (bc != NULL && (src > (gint)dst || (grp == GEANY_GBG_FT && (doc == NULL || doc->file_type == NULL))))
1943 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1944 gtk_widget_set_sensitive(roww->entries[i], FALSE);
1945 gtk_widget_set_sensitive(clear, FALSE);
1947 return roww;
1951 typedef struct BuildTableFields
1953 RowWidgets **rows;
1954 GtkWidget *fileregex;
1955 GtkWidget *nonfileregex;
1956 gchar **fileregexstring;
1957 gchar **nonfileregexstring;
1958 } BuildTableFields;
1961 GtkWidget *build_commands_table(GeanyDocument *doc, GeanyBuildSource dst, BuildTableData *table_data,
1962 GeanyFiletype *ft)
1964 GtkWidget *label, *sep, *clearicon, *clear;
1965 BuildTableFields *fields;
1966 GtkTable *table;
1967 const gchar **ch;
1968 gchar *txt;
1969 guint col, row, cmdindex;
1970 gint cmd;
1971 gint src;
1972 gboolean sensitivity;
1973 guint sep_padding = entry_y_padding + 3;
1975 table = GTK_TABLE(gtk_table_new(build_items_count + 12, 5, FALSE));
1976 fields = g_new0(BuildTableFields, 1);
1977 fields->rows = g_new0(RowWidgets*, build_items_count);
1978 for (ch = colheads, col = 0; *ch != NULL; ch++, col++)
1980 label = gtk_label_new(_(*ch));
1981 gtk_table_attach(table, label, col, col + 1, 0, 1,
1982 GTK_FILL, GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1984 sep = gtk_hseparator_new();
1985 gtk_table_attach(table, sep, 0, DC_N_COL, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND,
1986 entry_x_padding, sep_padding);
1987 if (ft != NULL && ft->id != GEANY_FILETYPES_NONE)
1988 txt = g_strdup_printf(_("%s commands"), ft->name);
1989 else
1990 txt = g_strdup_printf(_("%s commands"), _("No filetype"));
1992 label = ui_label_new_bold(txt);
1993 g_free(txt);
1994 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1995 gtk_table_attach(table, label, 0, DC_N_COL, 2, 3, GTK_FILL, GTK_FILL | GTK_EXPAND,
1996 entry_x_padding, entry_y_padding);
1997 for (row = 3, cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++row, ++cmdindex, ++cmd)
1998 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_FT, cmd, FALSE);
1999 label = gtk_label_new(_("Error regular expression:"));
2000 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2001 entry_x_padding, entry_y_padding);
2002 fields->fileregex = gtk_entry_new();
2003 fields->fileregexstring = build_get_regex(GEANY_GBG_FT, NULL, &src);
2004 sensitivity = (ft == NULL) ? FALSE : TRUE;
2005 if (fields->fileregexstring != NULL && *(fields->fileregexstring) != NULL)
2007 gtk_entry_set_text(GTK_ENTRY(fields->fileregex), *(fields->fileregexstring));
2008 if (src > (gint)dst)
2009 sensitivity = FALSE;
2011 gtk_table_attach(table, fields->fileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
2012 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2013 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2014 clear = gtk_button_new();
2015 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2016 g_signal_connect_swapped(clear, "clicked",
2017 G_CALLBACK(on_clear_dialog_regex_row), (fields->fileregex));
2018 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
2019 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2020 gtk_widget_set_sensitive(fields->fileregex, sensitivity);
2021 gtk_widget_set_sensitive(clear, sensitivity);
2022 ++row;
2023 sep = gtk_hseparator_new();
2024 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2025 entry_x_padding, sep_padding);
2026 ++row;
2027 label = ui_label_new_bold(_("Independent commands"));
2028 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2029 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2030 entry_x_padding, entry_y_padding);
2031 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++row, ++cmdindex, ++cmd)
2032 fields->rows[cmdindex] = build_add_dialog_row(
2033 doc, table, row, dst, GEANY_GBG_NON_FT, cmd, TRUE);
2034 label = gtk_label_new(_("Error regular expression:"));
2035 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL,
2036 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2037 fields->nonfileregex = gtk_entry_new();
2038 fields->nonfileregexstring = build_get_regex(GEANY_GBG_NON_FT, NULL, &src);
2039 sensitivity = TRUE;
2040 if (fields->nonfileregexstring != NULL && *(fields->nonfileregexstring) != NULL)
2042 gtk_entry_set_text(GTK_ENTRY(fields->nonfileregex), *(fields->nonfileregexstring));
2043 sensitivity = src > (gint)dst ? FALSE : TRUE;
2045 gtk_table_attach(table, fields->nonfileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
2046 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2047 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2048 clear = gtk_button_new();
2049 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2050 g_signal_connect_swapped(clear, "clicked",
2051 G_CALLBACK(on_clear_dialog_regex_row), (fields->nonfileregex));
2052 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
2053 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2054 gtk_widget_set_sensitive(fields->nonfileregex, sensitivity);
2055 gtk_widget_set_sensitive(clear, sensitivity);
2056 ++row;
2057 label = gtk_label_new(NULL);
2058 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2059 _("Note: Item 2 opens a dialog and appends the response to the command."));
2060 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2061 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2062 entry_x_padding, entry_y_padding);
2063 ++row;
2064 sep = gtk_hseparator_new();
2065 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2066 entry_x_padding, sep_padding);
2067 ++row;
2068 label = ui_label_new_bold(_("Execute commands"));
2069 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2070 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2071 entry_x_padding, entry_y_padding);
2072 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++row, ++cmdindex, ++cmd)
2073 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_EXEC, cmd, TRUE);
2074 sep = gtk_hseparator_new();
2075 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2076 entry_x_padding, sep_padding);
2077 ++row;
2078 label = gtk_label_new(NULL);
2079 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2080 _("%d, %e, %f, %p are substituted in command and directory fields, see manual for details."));
2081 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2082 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2083 entry_x_padding, entry_y_padding);
2084 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
2085 ++row;
2086 *table_data = fields;
2087 return GTK_WIDGET(table);
2091 void build_free_fields(BuildTableData table_data)
2093 gint cmdindex;
2095 for (cmdindex = 0; cmdindex < build_items_count; ++cmdindex)
2096 g_free(table_data->rows[cmdindex]);
2097 g_free(table_data->rows);
2098 g_free(table_data);
2102 /* string compare where null pointers match null or 0 length strings */
2103 #if 0
2104 static gint stcmp(const gchar *a, const gchar *b)
2106 if (a == NULL && b == NULL)
2107 return 0;
2108 if (a == NULL && b != NULL)
2109 return strlen(b);
2110 if (a != NULL && b == NULL)
2111 return strlen(a);
2112 return strcmp(a, b);
2114 #endif
2117 static const gchar *get_build_command_entry_text(GtkWidget *wid)
2119 if (GTK_IS_BUTTON(wid))
2120 return gtk_button_get_label(GTK_BUTTON(wid));
2121 else
2122 return gtk_entry_get_text(GTK_ENTRY(wid));
2126 static gboolean read_row(BuildDestination *dst, BuildTableData table_data, gint drow, gint grp, gint cmd)
2128 gchar *entries[GEANY_BC_CMDENTRIES_COUNT];
2129 gboolean changed = FALSE;
2130 enum GeanyBuildCmdEntries i;
2132 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2134 entries[i] = g_strdup(get_build_command_entry_text(table_data->rows[drow]->entries[i]));
2136 if (table_data->rows[drow]->cleared)
2138 if (dst->dst[grp] != NULL)
2140 if (*(dst->dst[grp]) == NULL)
2141 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2142 (*(dst->dst[grp]))[cmd].exists = FALSE;
2143 (*(dst->dst[grp]))[cmd].changed = TRUE;
2144 changed = TRUE;
2147 if (table_data->rows[drow]->used_dst == TRUE)
2149 if (dst->dst[grp] != NULL)
2151 if (*(dst->dst[grp]) == NULL)
2152 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2153 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2154 set_command(&(*(dst->dst[grp]))[cmd], i, entries[i]);
2155 (*(dst->dst[grp]))[cmd].exists = TRUE;
2156 (*(dst->dst[grp]))[cmd].changed = TRUE;
2157 changed = TRUE;
2160 else
2162 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2163 g_free(entries[i]);
2165 return changed;
2169 static gboolean read_regex(GtkWidget *regexentry, gchar **src, gchar **dst)
2171 gboolean changed = FALSE;
2172 const gchar *reg = gtk_entry_get_text(GTK_ENTRY(regexentry));
2174 if (((src == NULL /* originally there was no regex */
2175 || *src == NULL) /* or it was NULL*/
2176 && NZV(reg)) /* and something was typed */
2177 || (src != NULL /* originally there was a regex*/
2178 && (*src == NULL /* and either it was NULL */
2179 || strcmp(*src, reg) != 0))) /* or it has been changed */
2181 if (dst != NULL)
2183 setptr(*dst, g_strdup(reg));
2184 changed = TRUE;
2187 return changed;
2191 static gboolean build_read_commands(BuildDestination *dst, BuildTableData table_data, gint response)
2193 gint cmdindex, cmd;
2194 gboolean changed = FALSE;
2196 if (response == GTK_RESPONSE_ACCEPT)
2198 for (cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++cmdindex, ++cmd)
2199 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_FT, cmd);
2200 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++cmdindex, ++cmd)
2201 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_NON_FT, cmd);
2202 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++cmdindex, ++cmd)
2203 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_EXEC, cmd);
2204 changed |= read_regex(table_data->fileregex, table_data->fileregexstring, dst->fileregexstr);
2205 changed |= read_regex(table_data->nonfileregex, table_data->nonfileregexstring, dst->nonfileregexstr);
2207 return changed;
2211 void build_read_project(GeanyFiletype *ft, BuildTableData build_properties)
2213 BuildDestination menu_dst;
2215 if (ft != NULL)
2217 menu_dst.dst[GEANY_GBG_FT] = &(ft->projfilecmds);
2218 menu_dst.fileregexstr = &(ft->projerror_regex_string);
2220 else
2222 menu_dst.dst[GEANY_GBG_FT] = NULL;
2223 menu_dst.fileregexstr = NULL;
2225 menu_dst.dst[GEANY_GBG_NON_FT] = &non_ft_proj;
2226 menu_dst.dst[GEANY_GBG_EXEC] = &exec_proj;
2227 menu_dst.nonfileregexstr = &regex_proj;
2229 build_read_commands(&menu_dst, build_properties, GTK_RESPONSE_ACCEPT);
2233 static void show_build_commands_dialog(void)
2235 GtkWidget *dialog, *table, *vbox;
2236 GeanyDocument *doc = document_get_current();
2237 GeanyFiletype *ft = NULL;
2238 const gchar *title = _("Set Build Commands");
2239 gint response;
2240 BuildTableData table_data;
2241 BuildDestination prefdsts;
2243 if (doc != NULL)
2244 ft = doc->file_type;
2245 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
2246 GTK_DIALOG_DESTROY_WITH_PARENT,
2247 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2248 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
2249 table = build_commands_table(doc, GEANY_BCS_PREF, &table_data, ft);
2250 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
2251 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
2252 gtk_widget_show_all(dialog);
2253 /* run modally to prevent user changing idx filetype */
2254 response = gtk_dialog_run(GTK_DIALOG(dialog));
2256 prefdsts.dst[GEANY_GBG_NON_FT] = &non_ft_pref;
2257 if (ft != NULL)
2259 prefdsts.dst[GEANY_GBG_FT] = &(ft->homefilecmds);
2260 prefdsts.fileregexstr = &(ft->homeerror_regex_string);
2261 prefdsts.dst[GEANY_GBG_EXEC] = &(ft->homeexeccmds);
2263 else
2265 prefdsts.dst[GEANY_GBG_FT] = NULL;
2266 prefdsts.fileregexstr = NULL;
2267 prefdsts.dst[GEANY_GBG_EXEC] = NULL;
2269 prefdsts.nonfileregexstr = &regex_pref;
2270 if (build_read_commands(&prefdsts, table_data, response) && ft != NULL)
2271 filetypes_save_commands(ft);
2272 build_free_fields(table_data);
2274 build_menu_update(doc);
2275 gtk_widget_destroy(dialog);
2279 /* Creates the relevant build menu if necessary. */
2280 BuildMenuItems *build_get_menu_items(gint filetype_idx)
2282 BuildMenuItems *items;
2284 items = &menu_items;
2285 if (items->menu == NULL)
2286 create_build_menu(items);
2287 return items;
2291 /*----------------------------------------------------------
2293 * Load and store configuration
2295 * ---------------------------------------------------------*/
2296 static const gchar *build_grp_name = "build-menu";
2298 /* config format for build-menu group is prefix_gg_nn_xx=value
2299 * where gg = FT, NF, EX for the command group
2300 * nn = 2 digit command number
2301 * xx = LB for label, CM for command and WD for working dir */
2302 static const gchar *groups[GEANY_GBG_COUNT] = { "FT", "NF", "EX" };
2303 static const gchar *fixedkey="xx_xx_xx";
2305 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2306 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2307 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2309 static void build_load_menu_grp(GKeyFile *config, GeanyBuildCommand **dst, gint grp,
2310 gchar *prefix, gboolean loc)
2312 gint cmd;
2313 gsize prefixlen; /* NOTE prefixlen used in macros above */
2314 GeanyBuildCommand *dstcmd;
2315 gchar *key;
2316 static gchar cmdbuf[3] = " ";
2318 if (*dst == NULL)
2319 *dst = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2320 dstcmd = *dst;
2321 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2322 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2323 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2325 gchar *label;
2326 if (cmd < 0 || cmd >= 100)
2327 return; /* ensure no buffer overflow */
2328 sprintf(cmdbuf, "%02d", cmd);
2329 set_key_grp(key, groups[grp]);
2330 set_key_cmd(key, cmdbuf);
2331 set_key_fld(key, "LB");
2332 if (loc)
2333 label = g_key_file_get_locale_string(config, build_grp_name, key, NULL, NULL);
2334 else
2335 label = g_key_file_get_string(config, build_grp_name, key, NULL);
2336 if (label != NULL)
2338 dstcmd[cmd].exists = TRUE;
2339 setptr(dstcmd[cmd].label, label);
2340 set_key_fld(key,"CM");
2341 setptr(dstcmd[cmd].command,
2342 g_key_file_get_string(config, build_grp_name, key, NULL));
2343 set_key_fld(key,"WD");
2344 setptr(dstcmd[cmd].working_dir,
2345 g_key_file_get_string(config, build_grp_name, key, NULL));
2347 else dstcmd[cmd].exists = FALSE;
2349 g_free(key);
2353 /* for the specified source load new format build menu items or try to make some sense of
2354 * old format setings, not done perfectly but better than ignoring them */
2355 void build_load_menu(GKeyFile *config, GeanyBuildSource src, gpointer p)
2357 GeanyFiletype *ft;
2358 GeanyProject *pj;
2359 gchar **ftlist;
2360 gchar *value, *basedir, *makebasedir;
2361 gboolean bvalue = FALSE;
2363 if (g_key_file_has_group(config, build_grp_name))
2365 switch (src)
2367 case GEANY_BCS_FT:
2368 ft = (GeanyFiletype*)p;
2369 if (ft == NULL)
2370 return;
2371 build_load_menu_grp(config, &(ft->filecmds), GEANY_GBG_FT, NULL, TRUE);
2372 build_load_menu_grp(config, &(ft->ftdefcmds), GEANY_GBG_NON_FT, NULL, TRUE);
2373 build_load_menu_grp(config, &(ft->execcmds), GEANY_GBG_EXEC, NULL, TRUE);
2374 setptr(ft->error_regex_string,
2375 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2376 break;
2377 case GEANY_BCS_HOME_FT:
2378 ft = (GeanyFiletype*)p;
2379 if (ft == NULL)
2380 return;
2381 build_load_menu_grp(config, &(ft->homefilecmds), GEANY_GBG_FT, NULL, FALSE);
2382 build_load_menu_grp(config, &(ft->homeexeccmds), GEANY_GBG_EXEC, NULL, FALSE);
2383 setptr(ft->homeerror_regex_string,
2384 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2385 break;
2386 case GEANY_BCS_PREF:
2387 build_load_menu_grp(config, &non_ft_pref, GEANY_GBG_NON_FT, NULL, FALSE);
2388 build_load_menu_grp(config, &exec_pref, GEANY_GBG_EXEC, NULL, FALSE);
2389 setptr(regex_pref, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2390 break;
2391 case GEANY_BCS_PROJ:
2392 build_load_menu_grp(config, &non_ft_proj, GEANY_GBG_NON_FT, NULL, FALSE);
2393 build_load_menu_grp(config, &exec_proj, GEANY_GBG_EXEC, NULL, FALSE);
2394 setptr(regex_proj, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2395 pj = (GeanyProject*)p;
2396 if (p == NULL)
2397 return;
2398 ftlist = g_key_file_get_string_list(config, build_grp_name, "filetypes", NULL, NULL);
2399 if (ftlist != NULL)
2401 gchar **ftname;
2402 if (pj->build_filetypes_list == NULL)
2403 pj->build_filetypes_list = g_ptr_array_new();
2404 g_ptr_array_set_size(pj->build_filetypes_list, 0);
2405 for (ftname = ftlist; *ftname != NULL; ++ftname)
2407 ft = filetypes_lookup_by_name(*ftname);
2408 if (ft != NULL)
2410 gchar *regkey = g_strdup_printf("%serror_regex", *ftname);
2411 g_ptr_array_add(pj->build_filetypes_list, ft);
2412 setptr(ft->projerror_regex_string,
2413 g_key_file_get_string(config, build_grp_name, regkey, NULL));
2414 g_free(regkey);
2415 build_load_menu_grp(config, &(ft->projfilecmds), GEANY_GBG_FT, *ftname, FALSE);
2416 build_load_menu_grp(config, &(ft->projexeccmds), GEANY_GBG_EXEC, *ftname, FALSE);
2419 g_free(ftlist);
2421 break;
2422 default: /* defaults don't load from config, see build_init */
2423 break;
2427 /* load old [build_settings] values if there is no value defined by [build-menu] */
2429 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2430 /* TODO: rewrite as function */
2431 #define ASSIGNIF(type, id, string, value) \
2432 if (NZV(value) && ! type[GBO_TO_CMD(id)].exists) { \
2433 type[GBO_TO_CMD(id)].exists = TRUE; \
2434 setptr(type[GBO_TO_CMD(id)].label, g_strdup(string)); \
2435 setptr(type[GBO_TO_CMD(id)].command, (value)); \
2436 setptr(type[GBO_TO_CMD(id)].working_dir, NULL); \
2437 type[GBO_TO_CMD(id)].old = TRUE; \
2438 } else \
2439 g_free(value);
2441 switch (src)
2443 case GEANY_BCS_FT:
2444 ft = (GeanyFiletype*)p;
2445 value = g_key_file_get_string(config, "build_settings", "compiler", NULL);
2446 if (value != NULL)
2448 if (ft->filecmds == NULL)
2449 ft->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2450 ASSIGNIF(ft->filecmds, GEANY_GBO_COMPILE, _("_Compile"), value);
2452 value = g_key_file_get_string(config, "build_settings", "linker", NULL);
2453 if (value != NULL)
2455 if (ft->filecmds == NULL)
2456 ft->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2457 ASSIGNIF(ft->filecmds, GEANY_GBO_BUILD, _("_Build"), value);
2459 value = g_key_file_get_string(config, "build_settings", "run_cmd", NULL);
2460 if (value != NULL)
2462 if (ft->execcmds == NULL)
2463 ft->execcmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2464 ASSIGNIF(ft->execcmds, GEANY_GBO_EXEC, _("_Execute"), value);
2466 if (ft->error_regex_string == NULL)
2467 ft->error_regex_string = g_key_file_get_string(config, "build_settings", "error_regex", NULL);
2468 break;
2469 case GEANY_BCS_PROJ:
2470 if (non_ft_pref == NULL)
2471 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2472 basedir = project_get_base_path();
2473 if (basedir == NULL)
2474 basedir = g_strdup("%d");
2475 bvalue = g_key_file_get_boolean(config, "project", "make_in_base_path", NULL);
2476 if (bvalue)
2477 makebasedir = g_strdup(basedir);
2478 else
2479 makebasedir = g_strdup("%d");
2480 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].old)
2481 setptr(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].working_dir, g_strdup(makebasedir));
2482 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].old)
2483 setptr(non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].working_dir, g_strdup(makebasedir));
2484 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].old)
2485 setptr(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].working_dir, g_strdup("%d"));
2486 value = g_key_file_get_string(config, "project", "run_cmd", NULL);
2487 if (NZV(value))
2489 if (exec_proj == NULL)
2490 exec_proj = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2491 if (! exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists)
2493 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists = TRUE;
2494 setptr(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].label, g_strdup(_("_Execute")));
2495 setptr(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].command, value);
2496 setptr(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].working_dir, g_strdup(basedir));
2497 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].old = TRUE;
2500 g_free(makebasedir);
2501 g_free(basedir);
2502 break;
2503 case GEANY_BCS_PREF:
2504 value = g_key_file_get_string(config, "tools", "make_cmd", NULL);
2505 if (value != NULL)
2507 if (non_ft_pref == NULL)
2508 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2509 ASSIGNIF(non_ft_pref, GEANY_GBO_CUSTOM, _("Make Custom _Target"),
2510 g_strdup_printf("%s ", value));
2511 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_OBJECT, _("Make _Object"),
2512 g_strdup_printf("%s %%e.o",value));
2513 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_ALL, _("_Make"), value);
2515 break;
2516 default:
2517 break;
2522 static gint build_save_menu_grp(GKeyFile *config, GeanyBuildCommand *src, gint grp, gchar *prefix)
2524 gint cmd;
2525 gsize prefixlen; /* NOTE prefixlen used in macros above */
2526 gchar *key;
2527 gint count = 0;
2528 enum GeanyBuildCmdEntries i;
2530 if (src == NULL)
2531 return 0;
2532 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2533 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2534 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2536 if (src[cmd].exists) ++count;
2537 if (src[cmd].changed)
2539 static gchar cmdbuf[4] = " ";
2540 if (cmd < 0 || cmd >= 100)
2541 return count; /* ensure no buffer overflow */
2542 sprintf(cmdbuf, "%02d", cmd);
2543 set_key_grp(key, groups[grp]);
2544 set_key_cmd(key, cmdbuf);
2545 if (src[cmd].exists)
2547 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2549 set_key_fld(key, config_keys[i]);
2550 g_key_file_set_string(config, build_grp_name, key, id_to_str(&src[cmd], i));
2553 else
2555 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2557 set_key_fld(key, config_keys[i]);
2558 g_key_file_remove_key(config, build_grp_name, key, NULL);
2563 g_free(key);
2564 return count;
2568 typedef struct ForEachData
2570 GKeyFile *config;
2571 GPtrArray *ft_names;
2572 } ForEachData;
2575 static void foreach_project_filetype(gpointer data, gpointer user_data)
2577 GeanyFiletype *ft = data;
2578 ForEachData *d = user_data;
2579 gint i = 0;
2580 gchar *regkey = g_strdup_printf("%serror_regex", ft->name);
2582 i += build_save_menu_grp(d->config, ft->projfilecmds, GEANY_GBG_FT, ft->name);
2583 i += build_save_menu_grp(d->config, ft->projexeccmds, GEANY_GBG_EXEC, ft->name);
2584 if (NZV(ft->projerror_regex_string))
2586 g_key_file_set_string(d->config, build_grp_name, regkey, ft->projerror_regex_string);
2587 i++;
2589 else
2590 g_key_file_remove_key(d->config, build_grp_name, regkey, NULL);
2591 g_free(regkey);
2592 if (i > 0)
2593 g_ptr_array_add(d->ft_names, ft->name);
2597 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
2598 void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
2600 GeanyFiletype *ft;
2601 GeanyProject *pj;
2602 ForEachData data;
2604 switch (src)
2606 case GEANY_BCS_HOME_FT:
2607 ft = (GeanyFiletype*)ptr;
2608 if (ft == NULL)
2609 return;
2610 build_save_menu_grp(config, ft->homefilecmds, GEANY_GBG_FT, NULL);
2611 build_save_menu_grp(config, ft->homeexeccmds, GEANY_GBG_EXEC, NULL);
2612 if (NZV(ft->homeerror_regex_string))
2613 g_key_file_set_string(config, build_grp_name, "error_regex", ft->homeerror_regex_string);
2614 else
2615 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2616 break;
2617 case GEANY_BCS_PREF:
2618 build_save_menu_grp(config, non_ft_pref, GEANY_GBG_NON_FT, NULL);
2619 build_save_menu_grp(config, exec_pref, GEANY_GBG_EXEC, NULL);
2620 if (NZV(regex_pref))
2621 g_key_file_set_string(config, build_grp_name, "error_regex", regex_pref);
2622 else
2623 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2624 break;
2625 case GEANY_BCS_PROJ:
2626 pj = (GeanyProject*)ptr;
2627 build_save_menu_grp(config, non_ft_proj, GEANY_GBG_NON_FT, NULL);
2628 build_save_menu_grp(config, exec_proj, GEANY_GBG_EXEC, NULL);
2629 if (NZV(regex_proj))
2630 g_key_file_set_string(config, build_grp_name, "error_regex", regex_proj);
2631 else
2632 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2633 if (pj->build_filetypes_list != NULL)
2635 data.config = config;
2636 data.ft_names = g_ptr_array_new();
2637 g_ptr_array_foreach(pj->build_filetypes_list, foreach_project_filetype, (gpointer)(&data));
2638 if (data.ft_names->pdata != NULL)
2639 g_key_file_set_string_list(config, build_grp_name, "filetypes",
2640 (const gchar**)(data.ft_names->pdata), data.ft_names->len);
2641 else
2642 g_key_file_remove_key(config, build_grp_name, "filetypes", NULL);
2643 g_ptr_array_free(data.ft_names, TRUE);
2645 break;
2646 default: /* defaults and GEANY_BCS_FT can't save */
2647 break;
2652 void build_set_group_count(GeanyBuildGroup grp, gint count)
2654 gint i, sum;
2656 if (count > build_groups_count[grp])
2657 build_groups_count[grp] = count;
2658 for (i = 0, sum = 0; i < GEANY_GBG_COUNT; ++i)
2659 sum += build_groups_count[i];
2660 build_items_count = sum;
2664 gint build_get_group_count(GeanyBuildGroup grp)
2666 return build_groups_count[grp];
2670 static void on_project_close(void)
2672 /* remove project regexen */
2673 setptr(regex_proj, NULL);
2677 static struct
2679 const gchar *label;
2680 const gchar *command;
2681 const gchar *working_dir;
2682 GeanyBuildCommand **ptr;
2683 gint index;
2684 } default_cmds[] = {
2685 { N_("_Make"), "make", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_ALL)},
2686 { N_("Make Custom _Target"), "make ", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_CUSTOM)},
2687 { N_("Make _Object"), "make %e.o", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)},
2688 { N_("_Execute"), "./%e", NULL, &exec_def, GBO_TO_CMD(GEANY_GBO_EXEC)},
2689 { NULL, NULL, NULL, NULL, 0 }
2693 void build_init(void)
2695 GtkWidget *item;
2696 GtkWidget *toolmenu;
2697 gint cmdindex;
2699 g_signal_connect(geany_object, "project-close", on_project_close, NULL);
2701 ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2702 non_ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2703 exec_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2704 run_info = g_new0(RunInfo, build_groups_count[GEANY_GBG_EXEC]);
2706 for (cmdindex = 0; default_cmds[cmdindex].command != NULL; ++cmdindex)
2708 GeanyBuildCommand *cmd = &((*(default_cmds[cmdindex].ptr))[ default_cmds[cmdindex].index ]);
2709 cmd->exists = TRUE;
2710 cmd->label = g_strdup(_(default_cmds[cmdindex].label));
2711 cmd->command = g_strdup(default_cmds[cmdindex].command);
2712 cmd->working_dir = g_strdup(default_cmds[cmdindex].working_dir);
2715 /* create the toolbar Build item sub menu */
2716 toolmenu = gtk_menu_new();
2717 g_object_ref(toolmenu);
2719 /* build the code */
2720 item = ui_image_menu_item_new(GEANY_STOCK_BUILD, _("_Build"));
2721 gtk_widget_show(item);
2722 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2723 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_build_activate),
2724 GBO_TO_POINTER(GEANY_GBO_BUILD));
2725 widgets.toolitem_build = item;
2727 item = gtk_separator_menu_item_new();
2728 gtk_widget_show(item);
2729 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2731 /* build the code with make all */
2732 item = gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2733 gtk_widget_show(item);
2734 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2735 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2736 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL));
2737 widgets.toolitem_make_all = item;
2739 /* build the code with make custom */
2740 item = gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target"));
2741 gtk_widget_show(item);
2742 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2743 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2744 GBO_TO_POINTER(GEANY_GBO_CUSTOM));
2745 widgets.toolitem_make_custom = item;
2747 /* build the code with make object */
2748 item = gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2749 gtk_widget_show(item);
2750 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2751 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2752 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT));
2753 widgets.toolitem_make_object = item;
2755 item = gtk_separator_menu_item_new();
2756 gtk_widget_show(item);
2757 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2759 /* arguments */
2760 item = ui_image_menu_item_new(GTK_STOCK_PREFERENCES, _("_Set Build Commands"));
2761 gtk_widget_show(item);
2762 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2763 g_signal_connect(item, "activate", G_CALLBACK(on_set_build_commands_activate), NULL);
2764 widgets.toolitem_set_args = item;
2766 /* get toolbar action pointers */
2767 widgets.build_action = toolbar_get_action_by_name("Build");
2768 widgets.compile_action = toolbar_get_action_by_name("Compile");
2769 widgets.run_action = toolbar_get_action_by_name("Run");
2770 widgets.toolmenu = toolmenu;
2771 /* set the submenu to the toolbar item */
2772 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets.build_action), toolmenu);