Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty/vt: consolemap: introduce enum translation_map and use it

Again, instead of magic constants in the code, declare an enum and be a
little bit more explicit. Both in the translations definition and in the
loops etc.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Link: https://lore.kernel.org/r/20220607104946.18710-17-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby and committed by
Greg Kroah-Hartman
5a904a93 949fafcd

+32 -25
+20 -19
drivers/tty/vt/consolemap.c
··· 38 38 39 39 static unsigned short translations[][256] = { 40 40 /* 8-bit Latin-1 mapped to Unicode -- trivial mapping */ 41 - { 41 + [LAT1_MAP] = { 42 42 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 43 43 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 44 44 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, ··· 71 71 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 72 72 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 73 73 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff 74 - }, 74 + }, 75 75 /* VT100 graphics mapped to Unicode */ 76 - { 76 + [GRAF_MAP] = { 77 77 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 78 78 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 79 79 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, ··· 108 108 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff 109 109 }, 110 110 /* IBM Codepage 437 mapped to Unicode */ 111 - { 112 - 0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 111 + [IBMPC_MAP] = { 112 + 0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 113 113 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c, 114 114 0x25b6, 0x25c0, 0x2195, 0x203c, 0x00b6, 0x00a7, 0x25ac, 0x21a8, 115 115 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc, ··· 141 141 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229, 142 142 0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248, 143 143 0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0 144 - }, 144 + }, 145 145 /* User mapping -- default to codes for direct font mapping */ 146 - { 146 + [USER_MAP] = { 147 147 0xf000, 0xf001, 0xf002, 0xf003, 0xf004, 0xf005, 0xf006, 0xf007, 148 148 0xf008, 0xf009, 0xf00a, 0xf00b, 0xf00c, 0xf00d, 0xf00e, 0xf00f, 149 149 0xf010, 0xf011, 0xf012, 0xf013, 0xf014, 0xf015, 0xf016, 0xf017, ··· 184 184 185 185 #define MAX_GLYPH 512 /* Max possible glyph value */ 186 186 187 - static int inv_translate[MAX_NR_CONSOLES]; 187 + static enum translation_map inv_translate[MAX_NR_CONSOLES]; 188 188 189 189 #define UNI_DIRS 32U 190 190 #define UNI_DIR_ROWS 32U ··· 208 208 u16 **uni_pgdir[UNI_DIRS]; 209 209 unsigned long refcount; 210 210 unsigned long sum; 211 - unsigned char *inverse_translations[4]; 211 + unsigned char *inverse_translations[LAST_MAP + 1]; 212 212 u16 *inverse_trans_unicode; 213 213 }; 214 214 215 215 static struct uni_pagedict *dflt; 216 216 217 - static void set_inverse_transl(struct vc_data *conp, struct uni_pagedict *p, int i) 217 + static void set_inverse_transl(struct vc_data *conp, struct uni_pagedict *p, 218 + enum translation_map m) 218 219 { 219 220 int j, glyph; 220 - unsigned short *t = translations[i]; 221 + unsigned short *t = translations[m]; 221 222 unsigned char *q; 222 223 223 224 if (!p) 224 225 return; 225 - q = p->inverse_translations[i]; 226 + q = p->inverse_translations[m]; 226 227 227 228 if (!q) { 228 - q = p->inverse_translations[i] = kmalloc(MAX_GLYPH, GFP_KERNEL); 229 + q = p->inverse_translations[m] = kmalloc(MAX_GLYPH, GFP_KERNEL); 229 230 if (!q) 230 231 return; 231 232 } ··· 277 276 } 278 277 } 279 278 280 - unsigned short *set_translate(int m, struct vc_data *vc) 279 + unsigned short *set_translate(enum translation_map m, struct vc_data *vc) 281 280 { 282 281 inv_translate[vc->vc_num] = m; 283 282 return translations[m]; ··· 293 292 u16 inverse_translate(const struct vc_data *conp, u16 glyph, bool use_unicode) 294 293 { 295 294 struct uni_pagedict *p; 296 - int m; 295 + enum translation_map m; 297 296 298 297 if (glyph >= MAX_GLYPH) 299 298 return 0; ··· 670 669 if (con_unify_unimap(vc, p)) 671 670 goto out_unlock; 672 671 673 - for (i = 0; i <= 3; i++) 674 - set_inverse_transl(vc, p, i); /* Update inverse translations */ 672 + for (enum translation_map m = FIRST_MAP; m <= LAST_MAP; m++) 673 + set_inverse_transl(vc, p, m); /* Update inverse translations */ 675 674 set_inverse_trans_unicode(vc, p); 676 675 677 676 out_unlock: ··· 732 731 return err; 733 732 } 734 733 735 - for (i = 0; i <= 3; i++) 736 - set_inverse_transl(vc, p, i); /* Update all inverse translations */ 734 + for (enum translation_map m = FIRST_MAP; m <= LAST_MAP; m++) 735 + set_inverse_transl(vc, p, m); /* Update all inverse translations */ 737 736 set_inverse_trans_unicode(vc, p); 738 737 dflt = p; 739 738 return err;
+12 -6
include/linux/consolemap.h
··· 7 7 #ifndef __LINUX_CONSOLEMAP_H__ 8 8 #define __LINUX_CONSOLEMAP_H__ 9 9 10 - #define LAT1_MAP 0 11 - #define GRAF_MAP 1 12 - #define IBMPC_MAP 2 13 - #define USER_MAP 3 10 + enum translation_map { 11 + LAT1_MAP, 12 + GRAF_MAP, 13 + IBMPC_MAP, 14 + USER_MAP, 15 + 16 + FIRST_MAP = LAT1_MAP, 17 + LAST_MAP = USER_MAP, 18 + }; 14 19 15 20 #include <linux/types.h> 16 21 ··· 23 18 24 19 #ifdef CONFIG_CONSOLE_TRANSLATIONS 25 20 u16 inverse_translate(const struct vc_data *conp, u16 glyph, bool use_unicode); 26 - unsigned short *set_translate(int m, struct vc_data *vc); 21 + unsigned short *set_translate(enum translation_map m, struct vc_data *vc); 27 22 int conv_uni_to_pc(struct vc_data *conp, long ucs); 28 23 u32 conv_8bit_to_uni(unsigned char c); 29 24 int conv_uni_to_8bit(u32 uni); ··· 35 30 return glyph; 36 31 } 37 32 38 - static inline unsigned short *set_translate(int m, struct vc_data *vc) 33 + static inline unsigned short *set_translate(enum translation_map m, 34 + struct vc_data *vc) 39 35 { 40 36 return NULL; 41 37 }