1 /*
2    Skins engine.
3    Work with colors - backward compatibility
4 
5    Copyright (C) 2009-2021
6    Free Software Foundation, Inc.
7 
8    Written by:
9    Slava Zanko <slavazanko@gmail.com>, 2009
10    Egmont Koblinger <egmont@gmail.com>, 2010
11    Andrew Borodin <aborodin@vmail.ru>, 2012
12 
13    This file is part of the Midnight Commander.
14 
15    The Midnight Commander is free software: you can redistribute it
16    and/or modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation, either version 3 of the License,
18    or (at your option) any later version.
19 
20    The Midnight Commander is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License for more details.
24 
25    You should have received a copy of the GNU General Public License
26    along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  */
28 
29 #include <config.h>
30 #include <stdlib.h>
31 #include <string.h>             /* strcmp() */
32 #include <sys/types.h>          /* size_t */
33 
34 #include "internal.h"
35 
36 #include "lib/tty/color.h"
37 
38 /*** global variables ****************************************************************************/
39 
40 /*** file scope macro definitions ****************************************************************/
41 
42 /*** file scope type declarations ****************************************************************/
43 
44 typedef struct mc_skin_colors_old_struct
45 {
46     const char *old_color;
47     const char *group;
48     const char *key;
49 } mc_skin_colors_old_t;
50 
51 /*** file scope variables ************************************************************************/
52 
53 /* keep this table alphabetically sorted */
54 static const mc_skin_colors_old_t old_colors[] = {
55     {"bbarbutton", "buttonbar", "button"},
56     {"bbarhotkey", "buttonbar", "hotkey"},
57     {"commandlinemark", "core", "commandlinemark"},
58     {"dfocus", "dialog", "dfocus"},
59     {"dhotfocus", "dialog", "dhotfocus"},
60     {"dhotnormal", "dialog", "dhotnormal"},
61     {"disabled", "core", "disabled"},
62     {"dnormal", "dialog", "_default_"},
63     {"editbg", "editor", "editbg"},
64     {"editbold", "editor", "editbold"},
65     {"editframe", "editor", "editframe"},
66     {"editframeactive", "editor", "editframeactive"},
67     {"editframedrag", "editor", "editframedrag"},
68     {"editlinestate", "editor", "editlinestate"},
69     {"editmarked", "editor", "editmarked"},
70     {"editnormal", "editor", "_default_"},
71     {"editwhitespace", "editor", "editwhitespace"},
72     {"errdhotfocus", "error", "errdhotfocus"},
73     {"errdhotnormal", "error", "errdhotnormal"},
74     {"errors", "error", "_default_"},
75     {"gauge", "core", "gauge"},
76     {"header", "core", "header"},
77     {"helpbold", "help", "helpbold"},
78     {"helpitalic", "help", "helpitalic"},
79     {"helplink", "help", "helplink"},
80     {"helpnormal", "help", "_default_"},
81     {"helpslink", "help", "helpslink"},
82     {"input", "core", "input"},
83     {"inputmark", "core", "inputmark"},
84     {"inputunchanged", "core", "inputunchanged"},
85     {"marked", "core", "marked"},
86     {"markselect", "core", "markselect"},
87     {"menuhot", "menu", "menuhot"},
88     {"menuhotsel", "menu", "menuhotsel"},
89     {"menuinactive", "menu", "menuinactive"},
90     {"menunormal", "menu", "_default_"},
91     {"menusel", "menu", "menusel"},
92     {"normal", "core", "_default_"},
93     {"pmenunormal", "popupmenu", "_default_"},
94     {"pmenusel", "popupmenu", "menusel"},
95     {"pmenutitle", "popupmenu", "menutitle"},
96     {"reverse", "core", "reverse"},
97     {"selected", "core", "selected"},
98     {"statusbar", "statusbar", "_default_"},
99     {"viewbold", "viewer", "viewbold"},
100     {"viewnormal", "viewer", "_default_"},
101     {"viewselected", "viewer", "viewselected"},
102     {"viewunderline", "viewer", "viewunderline"}
103 };
104 
105 static const size_t num_old_colors = G_N_ELEMENTS (old_colors);
106 
107 /*** file scope functions ************************************************************************/
108 
109 static int
old_color_comparator(const void * p1,const void * p2)110 old_color_comparator (const void *p1, const void *p2)
111 {
112     const mc_skin_colors_old_t *m1 = (const mc_skin_colors_old_t *) p1;
113     const mc_skin_colors_old_t *m2 = (const mc_skin_colors_old_t *) p2;
114 
115     return strcmp (m1->old_color, m2->old_color);
116 }
117 
118 /* --------------------------------------------------------------------------------------------- */
119 
120 static gboolean
mc_skin_colors_old_transform(const char * old_color,const char ** group,const char ** key)121 mc_skin_colors_old_transform (const char *old_color, const char **group, const char **key)
122 {
123     const mc_skin_colors_old_t oc = { old_color, NULL, NULL };
124     mc_skin_colors_old_t *res;
125 
126     if (old_color == NULL)
127         return FALSE;
128 
129     res = (mc_skin_colors_old_t *) bsearch (&oc, old_colors, num_old_colors,
130                                             sizeof (old_colors[0]), old_color_comparator);
131 
132     if (res == NULL)
133         return FALSE;
134 
135     if (group != NULL)
136         *group = res->group;
137     if (key != NULL)
138         *key = res->key;
139     return TRUE;
140 }
141 
142 /* --------------------------------------------------------------------------------------------- */
143 
144 static void
mc_skin_colors_old_configure_one(mc_skin_t * mc_skin,const char * the_color_string)145 mc_skin_colors_old_configure_one (mc_skin_t * mc_skin, const char *the_color_string)
146 {
147     gchar **colors, **orig_colors;
148 
149     if (the_color_string == NULL)
150         return;
151 
152     orig_colors = g_strsplit (the_color_string, ":", -1);
153     if (orig_colors == NULL)
154         return;
155 
156     for (colors = orig_colors; *colors != NULL; colors++)
157     {
158         gchar **key_val;
159         const gchar *skin_group, *skin_key;
160 
161         key_val = g_strsplit_set (*colors, "=,", 4);
162 
163         if (key_val == NULL)
164             continue;
165 
166         if (key_val[1] != NULL && mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
167         {
168             gchar *skin_val;
169 
170             if (key_val[2] == NULL)
171                 skin_val = g_strdup_printf ("%s;", key_val[1]);
172             else if (key_val[3] == NULL)
173                 skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
174             else
175                 skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
176 
177             mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
178             g_free (skin_val);
179         }
180 
181         g_strfreev (key_val);
182     }
183     g_strfreev (orig_colors);
184 }
185 
186 /* --------------------------------------------------------------------------------------------- */
187 /*** public functions ****************************************************************************/
188 /* --------------------------------------------------------------------------------------------- */
189 
190 void
mc_skin_colors_old_configure(mc_skin_t * mc_skin)191 mc_skin_colors_old_configure (mc_skin_t * mc_skin)
192 {
193     mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.setup_color_string);
194     mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.term_color_string);
195     mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
196     mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.command_line_colors);
197 }
198 
199 /* --------------------------------------------------------------------------------------------- */
200