Line |
Branch |
Exec |
Source |
1 |
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
2 |
|
|
* |
3 |
|
|
* Copyright (C) 2010 Red Hat, Inc |
4 |
|
|
* Copyright (C) 2008 William Jon McCann <jmccann@redhat.com> |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 |
|
|
* |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <config.h> |
22 |
|
|
|
23 |
|
|
#include <glib.h> |
24 |
|
|
#include "info-cleanup.h" |
25 |
|
|
|
26 |
|
|
typedef struct |
27 |
|
|
{ |
28 |
|
|
char *regex; |
29 |
|
|
char *replacement; |
30 |
|
|
} ReplaceStrings; |
31 |
|
|
|
32 |
|
|
static char * |
33 |
|
✗ |
prettify_info (const char *info) |
34 |
|
|
{ |
35 |
|
✗ |
g_autofree char *escaped = NULL; |
36 |
|
✗ |
g_autofree gchar *pretty = NULL; |
37 |
|
|
int i; |
38 |
|
|
static const ReplaceStrings rs[] = { |
39 |
|
|
{ "Mesa DRI ", ""}, |
40 |
|
|
{ "Mesa Intel", "Intel"}, |
41 |
|
|
{ "[(]R[)]", "\302\256"}, |
42 |
|
|
{ "[(](tm|TM)[)]", "\342\204\242"}, |
43 |
|
|
{ "(ATI|EPYC|AMD FX|Radeon|Ryzen|Threadripper|GeForce RTX) ", "\\1\342\204\242 "}, |
44 |
|
|
{ "Gallium \\d+\\.\\d+ on (.*)", "\\1"}, |
45 |
|
|
{ " CPU| Processor| \\S+-Core| @ \\d+\\.\\d+GHz", ""}, |
46 |
|
|
{ " x86|/MMX|/SSE2|/PCIe", ""}, |
47 |
|
|
{ " [(][^)]*(DRM|MESA|LLVM)[^)]*[)]?", ""}, |
48 |
|
|
{ "Graphics Controller", "Graphics"}, |
49 |
|
|
{ ".*llvmpipe.*", "Software Rendering"}, |
50 |
|
|
}; |
51 |
|
|
|
52 |
|
✗ |
if (*info == '\0') |
53 |
|
✗ |
return NULL; |
54 |
|
|
|
55 |
|
✗ |
escaped = g_markup_escape_text (info, -1); |
56 |
|
✗ |
pretty = g_strdup (g_strstrip (escaped)); |
57 |
|
|
|
58 |
|
✗ |
for (i = 0; i < G_N_ELEMENTS (rs); i++) |
59 |
|
|
{ |
60 |
|
✗ |
g_autoptr(GError) error = NULL; |
61 |
|
✗ |
g_autoptr(GRegex) re = NULL; |
62 |
|
✗ |
g_autofree gchar *new = NULL; |
63 |
|
|
|
64 |
|
✗ |
re = g_regex_new (rs[i].regex, 0, 0, &error); |
65 |
|
✗ |
if (re == NULL) |
66 |
|
|
{ |
67 |
|
✗ |
g_warning ("Error building regex: %s", error->message); |
68 |
|
✗ |
continue; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
✗ |
new = g_regex_replace (re, |
72 |
|
|
pretty, |
73 |
|
|
-1, |
74 |
|
|
0, |
75 |
|
✗ |
rs[i].replacement, |
76 |
|
|
0, |
77 |
|
|
&error); |
78 |
|
|
|
79 |
|
✗ |
if (error != NULL) |
80 |
|
|
{ |
81 |
|
✗ |
g_warning ("Error replacing %s: %s", rs[i].regex, error->message); |
82 |
|
✗ |
continue; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
✗ |
g_free (pretty); |
86 |
|
✗ |
pretty = g_steal_pointer (&new); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
✗ |
return g_steal_pointer (&pretty); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
static char * |
93 |
|
✗ |
remove_duplicate_whitespace (const char *old) |
94 |
|
|
{ |
95 |
|
✗ |
g_autofree gchar *new = NULL; |
96 |
|
✗ |
g_autoptr(GRegex) re = NULL; |
97 |
|
✗ |
g_autoptr(GError) error = NULL; |
98 |
|
|
|
99 |
|
✗ |
if (old == NULL) |
100 |
|
✗ |
return NULL; |
101 |
|
|
|
102 |
|
✗ |
re = g_regex_new ("[ \t\n\r]+", G_REGEX_MULTILINE, 0, &error); |
103 |
|
✗ |
if (re == NULL) |
104 |
|
|
{ |
105 |
|
✗ |
g_warning ("Error building regex: %s", error->message); |
106 |
|
✗ |
return g_strdup (old); |
107 |
|
|
} |
108 |
|
✗ |
new = g_regex_replace (re, |
109 |
|
|
old, |
110 |
|
|
-1, |
111 |
|
|
0, |
112 |
|
|
" ", |
113 |
|
|
0, |
114 |
|
|
&error); |
115 |
|
✗ |
if (new == NULL) |
116 |
|
|
{ |
117 |
|
✗ |
g_warning ("Error replacing string: %s", error->message); |
118 |
|
✗ |
return g_strdup (old); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
✗ |
return g_steal_pointer (&new); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
char * |
125 |
|
✗ |
info_cleanup (const char *input) |
126 |
|
|
{ |
127 |
|
✗ |
g_autofree char *pretty = NULL; |
128 |
|
|
|
129 |
|
✗ |
pretty = prettify_info (input); |
130 |
|
✗ |
return remove_duplicate_whitespace (pretty); |
131 |
|
|
} |
132 |
|
|
|