|
| 1 | +// Copyright (c) 2010-2025 The YP-Spur Authors, except where otherwise indicated. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to |
| 5 | +// deal in the Software without restriction, including without limitation the |
| 6 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +#include <setjmp.h> |
| 22 | +#include <signal.h> |
| 23 | +#include <stdio.h> |
| 24 | + |
| 25 | +#ifdef __MINGW32__ |
| 26 | +#include <windows.h> |
| 27 | +#endif // __MINGW32__ |
| 28 | + |
| 29 | +#include <ypspur/param.h> |
| 30 | +#include <ypspur/serial.h> |
| 31 | +#include <ypspur/ssm_spur_handler.h> |
| 32 | +#include <ypspur/yprintf.h> |
| 33 | + |
| 34 | +#if HAVE_SIGLONGJMP |
| 35 | +sigjmp_buf ctrlc_capture; |
| 36 | +#elif HAVE_LONGJMP |
| 37 | +jmp_buf ctrlc_capture; |
| 38 | +#endif // HAVE_SIGLONGJMP |
| 39 | + |
| 40 | +#if defined(__MINGW32__) |
| 41 | +BOOL WINAPI win32_ctrlc_handler(DWORD type) |
| 42 | +{ |
| 43 | + fprintf(stderr, "\n"); |
| 44 | +#ifdef HAVE_SSM |
| 45 | + // SSM終了処理 |
| 46 | + if (!option(OPTION_WITHOUT_SSM)) |
| 47 | + { |
| 48 | + end_ypspurSSM(); |
| 49 | + } |
| 50 | +#endif // HAVE_SSM |
| 51 | + if (!(option(OPTION_WITHOUT_DEVICE))) |
| 52 | + { |
| 53 | + serial_close(); |
| 54 | + } |
| 55 | + |
| 56 | + return TRUE; |
| 57 | +} |
| 58 | +#else |
| 59 | +void emergency(int sig) |
| 60 | +{ |
| 61 | + fprintf(stderr, "\n"); |
| 62 | +#if HAVE_SIGLONGJMP |
| 63 | + siglongjmp(ctrlc_capture, 1); |
| 64 | +#elif HAVE_LONGJMP |
| 65 | + longjmp(ctrlc_capture, 1); |
| 66 | +#else |
| 67 | +#ifdef HAVE_SSM |
| 68 | + // SSM終了処理 |
| 69 | + if (!option(OPTION_WITHOUT_SSM)) |
| 70 | + { |
| 71 | + end_ypspurSSM(); |
| 72 | + } |
| 73 | +#endif // HAVE_SSM |
| 74 | + if (!(option(OPTION_WITHOUT_DEVICE))) |
| 75 | + { |
| 76 | + serial_close(); |
| 77 | + } |
| 78 | + |
| 79 | + exit(0); |
| 80 | +#endif // HAVE_SIGLONGJMP |
| 81 | +} |
| 82 | +#endif // defined(__MINGW32__) |
| 83 | + |
| 84 | +void enable_ctrlc_handling(const int enable) |
| 85 | +{ |
| 86 | +#if defined(__MINGW32__) |
| 87 | + if (enable) |
| 88 | + { |
| 89 | + if (!SetConsoleCtrlHandler(win32_ctrlc_handler, TRUE)) |
| 90 | + { |
| 91 | + yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler registration failed.\n"); |
| 92 | + } |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + if (!SetConsoleCtrlHandler(NULL, FALSE)) |
| 97 | + { |
| 98 | + yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler restoration failed.\n"); |
| 99 | + } |
| 100 | + } |
| 101 | +#else |
| 102 | + if (enable) |
| 103 | + { |
| 104 | + signal(SIGINT, emergency); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + signal(SIGINT, SIG_DFL); |
| 109 | + } |
| 110 | +#endif // defined(__MINGW32__) |
| 111 | +} |
| 112 | + |
| 113 | +int ctrlc_setjmp() |
| 114 | +{ |
| 115 | +#if HAVE_SIGLONGJMP |
| 116 | + return sigsetjmp(ctrlc_capture, 1); |
| 117 | +#elif HAVE_LONGJMP |
| 118 | + return setjmp(ctrlc_capture); |
| 119 | +#else |
| 120 | + return 0; |
| 121 | +#endif // HAVE_SIGLONGJMP |
| 122 | +} |
0 commit comments