File tree Expand file tree Collapse file tree 4 files changed +94
-0
lines changed
Expand file tree Collapse file tree 4 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1515#include "error.h"
1616#include "opt.h"
1717#include "opt_usage.h"
18+ #include "sighandler.h"
1819
1920#endif /* CLI_cli_h__ */
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (C) the libgit2 contributors. All rights reserved.
3+ *
4+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5+ * a Linking Exception. For full terms see the included COPYING file.
6+ */
7+
8+ #ifndef CLI_sighandler_h__
9+ #define CLI_sighandler_h__
10+
11+ /**
12+ * Sets up a signal handler that will run when the process is interrupted
13+ * (via SIGINT on POSIX or Control-C or Control-Break on Windows).
14+ *
15+ * @param handler The function to run on interrupt
16+ * @return 0 on success, -1 on failure
17+ */
18+ int cli_sighandler_set_interrupt (void (* handler )(void ));
19+
20+ #endif /* CLI_sighandler_h__ */
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (C) the libgit2 contributors. All rights reserved.
3+ *
4+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5+ * a Linking Exception. For full terms see the included COPYING file.
6+ */
7+
8+ #include <stdint.h>
9+ #include <signal.h>
10+ #include "git2_util.h"
11+ #include "cli.h"
12+
13+ static void (* interrupt_handler )(void ) = NULL ;
14+
15+ static void interrupt_proxy (int signal )
16+ {
17+ GIT_UNUSED (signal );
18+ interrupt_handler ();
19+ }
20+
21+ int cli_sighandler_set_interrupt (void (* handler )(void ))
22+ {
23+ void (* result )(int );
24+
25+ if ((interrupt_handler = handler ) != NULL )
26+ result = signal (SIGINT , interrupt_proxy );
27+ else
28+ result = signal (SIGINT , SIG_DFL );
29+
30+ if (result == SIG_ERR ) {
31+ git_error_set (GIT_ERROR_OS , "could not set signal handler" );
32+ return -1 ;
33+ }
34+
35+ return 0 ;
36+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (C) the libgit2 contributors. All rights reserved.
3+ *
4+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5+ * a Linking Exception. For full terms see the included COPYING file.
6+ */
7+
8+ #include "git2_util.h"
9+ #include <windows.h>
10+
11+ #include "cli.h"
12+
13+ static void (* interrupt_handler )(void ) = NULL ;
14+
15+ static BOOL WINAPI interrupt_proxy (DWORD signal )
16+ {
17+ GIT_UNUSED (signal );
18+ interrupt_handler ();
19+ return TRUE;
20+ }
21+
22+ int cli_sighandler_set_interrupt (void (* handler )(void ))
23+ {
24+ BOOL result ;
25+
26+ if ((interrupt_handler = handler ) != NULL )
27+ result = SetConsoleCtrlHandler (interrupt_proxy , FALSE);
28+ else
29+ result = SetConsoleCtrlHandler (NULL , FALSE);
30+
31+ if (!result ) {
32+ git_error_set (GIT_ERROR_OS , "could not set control control handler" );
33+ return -1 ;
34+ }
35+
36+ return 0 ;
37+ }
You can’t perform that action at this time.
0 commit comments