Skip to content

Commit 87b549a

Browse files
committed
Reduce code
1 parent da2e7f8 commit 87b549a

File tree

9 files changed

+41
-166
lines changed

9 files changed

+41
-166
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
paths:
1515
- 'src/**'
1616
- 'build/**'
17-
- 'configure'
17+
- 'gen-config'
1818
- 'Makefile'
1919
pull_request:
2020

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
-include config.mk
1+
ifeq ($(wildcard config.mk),)
2+
$(error config.mk is missing. Please run gen-config step before building.)
3+
endif
4+
include config.mk
25
all :
36
$(CC) $(CFLAGS) cprintf.c main.c -o cprintf
47
$(STRIP) cprintf

build/build-loong64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ apt install -y wget make clang git libseccomp-dev libcap-dev libc-dev binutils
88
apt install -y upx
99
git clone https://github.com/moe-hacker/cprintf
1010
cd cprintf
11-
./configure -s
11+
./gen-config -s
1212
make
1313
strip cprintf
1414
tar -cvf ../loong64.tar ./cprintf ./LICENSE

build/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mkdir output
88
mkdir output2
99
git clone https://github.com/moe-hacker/cprintf
1010
cd cprintf
11-
./configure -s
11+
./gen-config -s
1212
make
1313
strip cprintf
1414
cp cprintf ../output2/cprintf

build/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ sudo apt install qemu-user-static pkg-config libglib2.0-dev qemu-system-misc pyt
1717
# Build qemu-loongarch64
1818
git clone https://github.com/qemu/qemu.git
1919
cd qemu
20-
./configure --static --disable-system --target-list=loongarch64-linux-user
20+
./gen-config --static --disable-system --target-list=loongarch64-linux-user
2121
make -j$(nproc)
2222
sudo cp build/loongarch64-linux-user/qemu-loongarch64 /usr/bin/qemu-loongarch64-static
2323
sudo cp build/qemu-loongarch64 /usr/bin/qemu-loongarch64-static

cprintf.c

Lines changed: 4 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@
3434
#define false ((_Bool) + 0u)
3535
#endif
3636
char *cprintf_base_color = "254;228;208";
37-
static void print_rgb_fg_color(const char *_Nonnull color)
38-
{
39-
/*
40-
* print \033[1;38;2;R;G;Bm format color.
41-
*/
42-
char buf[17];
43-
for (size_t i = 1; i < strlen(color) - 1; i++) {
44-
buf[i - 1] = color[i];
45-
buf[i] = 0;
46-
}
47-
printf("\033[38;2;%sm", buf);
48-
}
4937
static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull color)
5038
{
5139
/*
@@ -58,18 +46,6 @@ static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull colo
5846
}
5947
fprintf(stream, "\033[38;2;%sm", buf);
6048
}
61-
static void print_rgb_bg_color(const char *_Nonnull color)
62-
{
63-
/*
64-
* print \033[1;38;2;R;G;Bm format color.
65-
*/
66-
char buf[17];
67-
for (size_t i = 1; i < strlen(color) - 1; i++) {
68-
buf[i - 1] = color[i];
69-
buf[i] = 0;
70-
}
71-
printf("\033[48;2;%sm", buf);
72-
}
7349
static void fprint_rgb_bg_color(FILE *_Nonnull stream, const char *_Nonnull color)
7450
{
7551
/*
@@ -167,58 +143,6 @@ static const char *cfprintf_print_fg_color(FILE *_Nonnull stream, const char *_N
167143
}
168144
return ret;
169145
}
170-
static const char *cprintf_print_fg_color(const char *_Nonnull buf)
171-
{
172-
/*
173-
* Only valid {color} will be recognized,
174-
* and for other '{' without 'color}', we print a '{'.
175-
* we return the pointer to the last character that is
176-
* not recognized as color.
177-
*/
178-
const char *ret = buf;
179-
char color[17] = { '\0' };
180-
for (int i = 0; i < 16; i++) {
181-
if (buf[i] == '}') {
182-
color[i] = buf[i];
183-
color[i + 1] = 0;
184-
ret = &(buf[i]);
185-
break;
186-
}
187-
color[i] = buf[i];
188-
color[i + 1] = 0;
189-
}
190-
if (strcmp(color, "{clear}") == 0) {
191-
printf("\033[0m");
192-
} else if (strcmp(color, "{black}") == 0) {
193-
printf("\033[30m");
194-
} else if (strcmp(color, "{red}") == 0) {
195-
printf("\033[31m");
196-
} else if (strcmp(color, "{green}") == 0) {
197-
printf("\033[32m");
198-
} else if (strcmp(color, "{yellow}") == 0) {
199-
printf("\033[33m");
200-
} else if (strcmp(color, "{blue}") == 0) {
201-
printf("\033[34m");
202-
} else if (strcmp(color, "{purple}") == 0) {
203-
printf("\033[35m");
204-
} else if (strcmp(color, "{cyan}") == 0) {
205-
printf("\033[36m");
206-
} else if (strcmp(color, "{white}") == 0) {
207-
printf("\033[37m");
208-
} else if (strcmp(color, "{base}") == 0) {
209-
printf("\033[1;38;2;%sm", cprintf_base_color);
210-
} else if (strcmp(color, "{underline}") == 0) {
211-
printf("\033[4m");
212-
} else if (strcmp(color, "{highlight}") == 0) {
213-
printf("\033[1m");
214-
} else if (is_rgb_color(color)) {
215-
print_rgb_fg_color(color);
216-
} else {
217-
ret = buf;
218-
printf("{");
219-
}
220-
return ret;
221-
}
222146
static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_Nonnull buf)
223147
{
224148
/*
@@ -271,70 +195,18 @@ static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_N
271195
}
272196
return ret;
273197
}
274-
static const char *cprintf_print_bg_color(const char *_Nonnull buf)
275-
{
276-
/*
277-
* Only valid [color] will be recognized,
278-
* and for other '[' without 'color]', we print a '['.
279-
* we return the pointer to the last character that is
280-
* not recognized as color.
281-
*/
282-
const char *ret = buf;
283-
char color[17] = { '\0' };
284-
for (int i = 0; i < 16; i++) {
285-
if (buf[i] == ']') {
286-
color[i] = buf[i];
287-
color[i + 1] = 0;
288-
ret = &(buf[i]);
289-
break;
290-
}
291-
color[i] = buf[i];
292-
color[i + 1] = 0;
293-
}
294-
if (strcmp(color, "[clear]") == 0) {
295-
printf("\033[0m");
296-
} else if (strcmp(color, "[black]") == 0) {
297-
printf("\033[40m");
298-
} else if (strcmp(color, "[red]") == 0) {
299-
printf("\033[41m");
300-
} else if (strcmp(color, "[green]") == 0) {
301-
printf("\033[42m");
302-
} else if (strcmp(color, "[yellow]") == 0) {
303-
printf("\033[43m");
304-
} else if (strcmp(color, "[blue]") == 0) {
305-
printf("\033[44m");
306-
} else if (strcmp(color, "[purple]") == 0) {
307-
printf("\033[45m");
308-
} else if (strcmp(color, "[cyan]") == 0) {
309-
printf("\033[46m");
310-
} else if (strcmp(color, "[white]") == 0) {
311-
printf("\033[47m");
312-
} else if (strcmp(color, "[base]") == 0) {
313-
printf("\033[1;48;2;%sm", cprintf_base_color);
314-
} else if (strcmp(color, "[underline]") == 0) {
315-
printf("\033[4m");
316-
} else if (strcmp(color, "[highlight]") == 0) {
317-
printf("\033[1m");
318-
} else if (is_rgb_color(color)) {
319-
print_rgb_bg_color(color);
320-
} else {
321-
ret = buf;
322-
printf("[");
323-
}
324-
return ret;
325-
}
326-
void __cprintf(const char *_Nonnull buf)
198+
void cprintf__(const char *_Nonnull buf)
327199
{
328200
const char *p = NULL;
329201
p = buf;
330202
for (size_t i = 0; i < strlen(buf); i++) {
331203
// Search for '{'.
332204
if (*p == '{') {
333205
// *p will be moved because we need to skip the {color} string.
334-
p = cprintf_print_fg_color(p);
206+
p = cfprintf_print_fg_color(stdout, p);
335207
} else if (*p == '[') {
336208
// *p will be moved because we need to skip the [color] string.
337-
p = cprintf_print_bg_color(p);
209+
p = cfprintf_print_bg_color(stdout, p);
338210
} else {
339211
printf("%c", *p);
340212
}
@@ -347,7 +219,7 @@ void __cprintf(const char *_Nonnull buf)
347219
printf("\033[0m");
348220
fflush(stdout);
349221
}
350-
void __cfprintf(FILE *_Nonnull stream, const char *_Nonnull buf)
222+
void cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf)
351223
{
352224
const char *p = NULL;
353225
p = buf;

configure renamed to gen-config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ check_headers_and_libs() {
136136
return 0
137137
}
138138
show_help() {
139-
echo "Usage: ./configure [OPTION]..."
139+
echo "Usage: ./gen-config [OPTION]..."
140140
echo " -h, --help show help"
141141
echo " -s, --static compile static binary"
142142
echo " -d, --dev compile dev version"

include/cprintf.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
#ifndef _Nonnull
4141
#define _Nonnull
4242
#endif
43-
void __cprintf(const char *_Nonnull buf);
44-
void __cfprintf(FILE *_Nonnull stream, const char *_Nonnull buf);
43+
void cprintf__(const char *_Nonnull buf);
44+
void cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf);
4545
// Get the size of the string to print.
4646
// We give another 514 bytes to avoid buffer overflow.
4747
// We call snprintf() twice, but never mind, it's fast enough.
48-
#define cprintf_get_bufsize(format, ...) (snprintf(NULL, 0, format, ##__VA_ARGS__) > 0 ? (size_t)snprintf(NULL, 0, format, ##__VA_ARGS__) + 514 : 514)
48+
#define cprintf_get_bufsize__(format, ...) (snprintf(NULL, 0, format, ##__VA_ARGS__) > 0 ? (size_t)snprintf(NULL, 0, format, ##__VA_ARGS__) + 514 : 514)
4949
// The `base` color.
5050
extern char *cprintf_base_color;
5151
/*
@@ -54,27 +54,27 @@ extern char *cprintf_base_color;
5454
* we use sprintf() to write it to buf[],
5555
* so that we only need to parse the string in buf[].
5656
*/
57-
#define cprintf(format, ...) \
58-
{ \
59-
char *__cprintf_buf = NULL; \
60-
size_t bufsize = cprintf_get_bufsize(format, ##__VA_ARGS__); \
61-
if (bufsize != 0) { \
62-
__cprintf_buf = malloc(bufsize); \
63-
sprintf(__cprintf_buf, format, ##__VA_ARGS__); \
64-
__cprintf(__cprintf_buf); \
65-
free(__cprintf_buf); \
66-
} \
57+
#define cprintf(format, ...) \
58+
{ \
59+
char *cprintf_buf__ = NULL; \
60+
size_t bufsize = cprintf_get_bufsize__(format, ##__VA_ARGS__); \
61+
if (bufsize != 0) { \
62+
cprintf_buf__ = malloc(bufsize); \
63+
sprintf(cprintf_buf__, format, ##__VA_ARGS__); \
64+
cprintf__(cprintf_buf__); \
65+
free(cprintf_buf__); \
66+
} \
6767
}
68-
#define cfprintf(stream, format, ...) \
69-
{ \
70-
char *__cprintf_buf = NULL; \
71-
size_t bufsize = cprintf_get_bufsize(format, ##__VA_ARGS__); \
72-
if (bufsize != 0) { \
73-
__cprintf_buf = malloc(bufsize); \
74-
sprintf(__cprintf_buf, format, ##__VA_ARGS__); \
75-
__cfprintf(stream, __cprintf_buf); \
76-
free(__cprintf_buf); \
77-
} \
68+
#define cfprintf(stream, format, ...) \
69+
{ \
70+
char *cprintf_buf__ = NULL; \
71+
size_t bufsize = cprintf_get_bufsize__(format, ##__VA_ARGS__); \
72+
if (bufsize != 0) { \
73+
cprintf_buf__ = malloc(bufsize); \
74+
sprintf(cprintf_buf__, format, ##__VA_ARGS__); \
75+
cfprintf__(stream, cprintf_buf__); \
76+
free(cprintf_buf__); \
77+
} \
7878
}
7979
#define CPRINTF_MAJOR 1
80-
#define CPRINTF_MINOR 4
80+
#define CPRINTF_MINOR 5

main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
*/
3030
#include "include/cprintf.h"
31-
static char *__cprintf_parse(const char *_Nonnull buf)
31+
static char *cprintf_parse__(const char *_Nonnull buf)
3232
{
3333
char *ret = malloc(strlen(buf) + 114);
3434
ret[0] = '\0';
@@ -74,16 +74,16 @@ static char *__cprintf_parse(const char *_Nonnull buf)
7474
}
7575
return ret;
7676
}
77-
static void __cprintf__(const char *_Nonnull buf)
77+
static void do_cprintf__(const char *_Nonnull buf)
7878
{
79-
char *__buf = __cprintf_parse(buf);
79+
char *__buf = cprintf_parse__(buf);
8080
cprintf("%s", __buf);
8181
free(__buf);
8282
}
8383
int main(int argc, char **argv)
8484
{
8585
for (int i = 1; i < argc; i++) {
86-
__cprintf__(argv[i]);
86+
do_cprintf__(argv[i]);
8787
if (i < argc - 1) {
8888
printf(" ");
8989
}

0 commit comments

Comments
 (0)