| 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 clean_up(void) |
| 22 | { |
| 23 | #ifdef INOTIFY |
| 24 | stop_inotify(); |
| 25 | #endif |
| 26 | g_list_free(lines); |
| 27 | destroy_directory_content(tree_root); |
| 28 | free_node_data(tree_root, NULL); |
| 29 | g_node_destroy(tree_root); |
| 30 | } |
| 31 | |
| 32 | void print_errno_and_exit(int last_errno, char *file, int line) |
| 33 | { |
| 34 | char *message_format = "%s (%s:%i)"; |
| 35 | char *message; |
| 36 | |
| 37 | endwin(); |
| 38 | message = g_strdup_printf(message_format, program_name, file, line); |
| 39 | errno = last_errno; |
| 40 | perror(message); |
| 41 | free(message); |
| 42 | exit(EXIT_FAILURE); |
| 43 | } |
| 44 | |
| 45 | void handle_sigsegv(int signal_number) |
| 46 | { |
| 47 | endwin(); |
| 48 | fprintf(stderr, "%s\n", g_strsignal(SIGSEGV)); |
| 49 | exit(EXIT_FAILURE); |
| 50 | } |
| 51 | |
| 52 | void handle_sigint(int signal_number) |
| 53 | { |
| 54 | endwin(); |
| 55 | exit(EXIT_SUCCESS); |
| 56 | } |
| 57 | |
| 58 | void set_sighandlers(void) |
| 59 | { |
| 60 | struct sigaction act; |
| 61 | |
| 62 | act.sa_handler = handle_sigsegv; |
| 63 | sigemptyset(&act.sa_mask); |
| 64 | act.sa_flags = 0; |
| 65 | sigaction(SIGSEGV, &act, NULL); |
| 66 | |
| 67 | act.sa_handler = handle_sigint; |
| 68 | sigaction(SIGINT, &act, NULL); |
| 69 | |
| 70 | act.sa_handler = handle_sigwinch; |
| 71 | sigaction(SIGWINCH, &act, NULL); |
| 72 | } |
| 73 | |
| 74 | void unset_sighandlers(void) |
| 75 | { |
| 76 | struct sigaction act; |
| 77 | |
| 78 | act.sa_handler = SIG_IGN; |
| 79 | sigemptyset(&act.sa_mask); |
| 80 | act.sa_flags = 0; |
| 81 | |
| 82 | sigaction(SIGSEGV, &act, NULL); |
| 83 | sigaction(SIGINT, &act, NULL); |
| 84 | sigaction(SIGWINCH, &act, NULL); |
| 85 | } |
| 86 | |
| 87 | void print_usage(void) |
| 88 | { |
| 89 | printf("Usage: %s [-h|-d] [<tree_root>]\n", program_name); |
| 90 | printf("The root of the displayed tree is `tree_root'.\n"); |
| 91 | printf("\t-h: show this help\n\t-d: print on stderr inotify events debug info\n"); |
| 92 | } |
| 93 | |
| 94 | int main(int argc, char *argv[]) |
| 95 | { |
| 96 | program_name = argv[0]; |
| 97 | if (argc > 1 && !strcmp(argv[1], "-h")) { |
| 98 | print_usage(); |
| 99 | return 0; |
| 100 | } else if ((argc == 2 || argc == 3) && !strcmp(argv[1], "-d")) { |
| 101 | #ifdef INOTIFY |
| 102 | debug_inotify = TRUE; |
| 103 | #endif |
| 104 | } else if ((argc > 3 && !strcmp(argv[1], "-d")) || argc > 2) { |
| 105 | fprintf(stderr, "%s: invalid option\n", program_name); |
| 106 | print_usage(); |
| 107 | return 1; |
| 108 | } |
| 109 | if (setlocale(LC_ALL, "") == NULL) |
| 110 | fprintf(stderr, "Error: Can't set the specified locale\n"); |
| 111 | |
| 112 | set_sighandlers(); |
| 113 | if (argc == 1) |
| 114 | init_ui(NULL); |
| 115 | else if (argc == 2 && !strcmp(argv[1], "-d")) |
| 116 | init_ui(NULL); |
| 117 | else if (argc == 2) |
| 118 | init_ui(argv[1]); |
| 119 | else |
| 120 | init_ui(argv[2]); |
| 121 | |
| 122 | for (;;) { |
| 123 | get_key(); |
| 124 | refresh_screen(); |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |