| 1 | /* Copyright (C) 2008 Ricardo Catalinas Jiménez <jimenezrick@gmail.com> |
| 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation, either version 3 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "fm.h" |
| 18 | |
| 19 | char *program_name; |
| 20 | |
| 21 | void print_errno_and_exit(int last_errno, char *file, int line) |
| 22 | { |
| 23 | char *message_format = "%s (%s:%i)"; |
| 24 | char *message; |
| 25 | |
| 26 | endwin(); |
| 27 | message = g_strdup_printf(message_format, program_name, file, line); |
| 28 | errno = last_errno; |
| 29 | perror(message); |
| 30 | free(message); |
| 31 | exit(EXIT_FAILURE); |
| 32 | } |
| 33 | |
| 34 | void handle_sigsegv(int signal_number) |
| 35 | { |
| 36 | endwin(); |
| 37 | fprintf(stderr, "%s\n", g_strsignal(SIGSEGV)); |
| 38 | exit(EXIT_FAILURE); |
| 39 | } |
| 40 | |
| 41 | void handle_sigint(int signal_number) |
| 42 | { |
| 43 | endwin(); |
| 44 | exit(EXIT_SUCCESS); |
| 45 | } |
| 46 | |
| 47 | void set_sighandlers(void) |
| 48 | { |
| 49 | struct sigaction act; |
| 50 | |
| 51 | act.sa_handler = handle_sigsegv; |
| 52 | sigemptyset(&act.sa_mask); |
| 53 | act.sa_flags = 0; |
| 54 | sigaction(SIGSEGV, &act, NULL); |
| 55 | |
| 56 | act.sa_handler = handle_sigint; |
| 57 | sigaction(SIGINT, &act, NULL); |
| 58 | |
| 59 | act.sa_handler = handle_sigwinch; |
| 60 | sigaction(SIGWINCH, &act, NULL); |
| 61 | } |
| 62 | |
| 63 | void unset_sighandlers(void) |
| 64 | { |
| 65 | struct sigaction act; |
| 66 | |
| 67 | act.sa_handler = SIG_IGN; |
| 68 | sigemptyset(&act.sa_mask); |
| 69 | act.sa_flags = 0; |
| 70 | |
| 71 | sigaction(SIGSEGV, &act, NULL); |
| 72 | sigaction(SIGINT, &act, NULL); |
| 73 | sigaction(SIGWINCH, &act, NULL); |
| 74 | } |
| 75 | |
| 76 | int main(int argc, char *argv[]) |
| 77 | { |
| 78 | if (argc > 1 && !strcmp(argv[1], "-h")) { |
| 79 | printf("Usage: fm [<root_directory>]\n"); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | if (setlocale(LC_ALL, "") == NULL) |
| 84 | fprintf(stderr, "Error: Can't set the specified locale\n"); |
| 85 | |
| 86 | set_sighandlers(); |
| 87 | program_name = argv[0]; |
| 88 | if (argc == 1) |
| 89 | init_ui(NULL); |
| 90 | else |
| 91 | init_ui(argv[1]); |
| 92 | for (;;) { |
| 93 | get_key(); |
| 94 | refresh_screen(); |
| 95 | } |
| 96 | return 0; |
| 97 | } |