Line data Source code
1 : /**
2 : * This file is part of rmlint.
3 : *
4 : * rmlint is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * rmlint is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with rmlint. If not, see <http://www.gnu.org/licenses/>.
16 : *
17 : ** Authors:
18 : *
19 : * - Christopher <sahib> Pahl 2010-2014 (https://github.com/sahib)
20 : * - Daniel <SeeSpotRun> T. 2014-2014 (https://github.com/SeeSpotRun)
21 : *
22 : ** Hosted on http://github.com/sahib/rmlint
23 : *
24 : **/
25 :
26 : #include <stdlib.h>
27 : #include <string.h>
28 : #include <locale.h>
29 :
30 : #include "../lib/api.h"
31 : #include "../lib/config.h"
32 :
33 : #if HAVE_JSON_GLIB && !GLIB_CHECK_VERSION(2, 36, 0)
34 : #include <glib-object.h>
35 : #endif
36 :
37 0 : static char *remove_color_escapes(char *message) {
38 0 : char *dst = message;
39 0 : for(char *src = message; src && *src; src++) {
40 0 : if(*src == '\x1b') {
41 0 : src = strchr(src, 'm');
42 : } else {
43 0 : *dst++ = *src;
44 : }
45 : }
46 :
47 0 : if(dst)
48 0 : *dst = 0;
49 0 : return message;
50 : }
51 :
52 17220850 : static void logging_callback(_U const gchar *log_domain,
53 : GLogLevelFlags log_level,
54 : const gchar *message,
55 : gpointer user_data) {
56 17220850 : RmSession *session = user_data;
57 17220850 : if(session->cfg->verbosity >= log_level) {
58 0 : if(!session->cfg->with_stderr_color) {
59 0 : message = remove_color_escapes((char *)message);
60 : }
61 0 : fputs(message, stderr);
62 : }
63 17220850 : }
64 :
65 : /* Global variables, signal handlers cannot be passed userdata */
66 : static volatile int CTRLC_COUNTER = 0;
67 : static volatile RmSession *SESSION_POINTER = NULL;
68 :
69 0 : static void signal_handler(int signum) {
70 0 : switch(signum) {
71 : case SIGINT:
72 0 : if(CTRLC_COUNTER++ == 0) {
73 0 : rm_session_abort((RmSession *)SESSION_POINTER);
74 0 : rm_log_warning_line(_("Received Interrupt, stopping..."));
75 : } else {
76 0 : rm_log_warning_line(_("Received second Interrupt, stopping hard."));
77 0 : rm_session_clear((RmSession *)SESSION_POINTER);
78 0 : exit(EXIT_FAILURE);
79 : }
80 0 : break;
81 : case SIGSEGV:
82 0 : rm_log_error_line(_("Aborting due to a fatal error. (signal received: %s)"),
83 : g_strsignal(signum));
84 0 : rm_log_error_line(_("Please file a bug report (See rmlint -h)"));
85 : default:
86 0 : exit(EXIT_FAILURE);
87 : break;
88 : }
89 0 : }
90 :
91 54884 : static void i18n_init(void) {
92 : #if HAVE_LIBINTL
93 : /* Tell gettext where to search for .mo files */
94 54884 : bindtextdomain(RM_GETTEXT_PACKAGE, INSTALL_PREFIX "/share/locale");
95 54884 : bind_textdomain_codeset(RM_GETTEXT_PACKAGE, "UTF-8");
96 :
97 : /* Make printing umlauts work */
98 54884 : setlocale(LC_ALL, "");
99 :
100 : /* Say we're the textdomain "rmlint"
101 : * so gettext can find us in
102 : * /usr/share/locale/de/LC_MESSAGEs/rmlint.mo
103 : * */
104 54884 : textdomain(RM_GETTEXT_PACKAGE);
105 : #endif
106 54884 : }
107 :
108 54884 : int main(int argc, const char **argv) {
109 54884 : int exit_state = EXIT_FAILURE;
110 :
111 : RmCfg cfg;
112 54884 : rm_cfg_set_default(&cfg);
113 :
114 : RmSession session;
115 54884 : rm_session_init(&session, &cfg);
116 :
117 : /* call logging_callback on every message */
118 54884 : g_log_set_default_handler(logging_callback, &session);
119 :
120 54884 : i18n_init();
121 :
122 : /* Register signals */
123 54884 : SESSION_POINTER = &session;
124 :
125 : struct sigaction sa;
126 54884 : sigemptyset(&sa.sa_mask);
127 54884 : sa.sa_flags = 0;
128 54884 : sa.sa_handler = signal_handler;
129 :
130 54884 : sigaction(SIGINT, &sa, NULL);
131 54884 : sigaction(SIGSEGV, &sa, NULL);
132 54884 : sigaction(SIGFPE, &sa, NULL);
133 54884 : sigaction(SIGABRT, &sa, NULL);
134 :
135 : #if HAVE_JSON_GLIB && !GLIB_CHECK_VERSION(2, 36, 0)
136 : /* Very old glib. Debian, Im looking at you. */
137 : g_type_init();
138 : #endif
139 :
140 : /* Parse commandline */
141 54884 : if(rm_cmd_parse_args(argc, (char **)argv, &session) != 0) {
142 : /* Do all the real work */
143 52985 : exit_state = rm_cmd_main(&session);
144 : }
145 :
146 52994 : rm_session_clear(&session);
147 52994 : return exit_state;
148 : }
|