diff options
author | netblue30 <netblue30@protonmail.com> | 2022-04-25 15:34:24 -0400 |
---|---|---|
committer | netblue30 <netblue30@protonmail.com> | 2022-04-25 15:34:24 -0400 |
commit | 1cdfa6f9554c42eb3a817e2cdf68f10e02be9f00 (patch) | |
tree | 2d4dc520cc6b110bf3ca620e1f9523cf73cc3510 /src/fzenity | |
parent | fix firecfg --guide (diff) | |
download | firejail-1cdfa6f9554c42eb3a817e2cdf68f10e02be9f00.tar.gz firejail-1cdfa6f9554c42eb3a817e2cdf68f10e02be9f00.tar.zst firejail-1cdfa6f9554c42eb3a817e2cdf68f10e02be9f00.zip |
more on firecfg --guide: fzenity
Diffstat (limited to 'src/fzenity')
-rw-r--r-- | src/fzenity/Makefile.in | 17 | ||||
-rw-r--r-- | src/fzenity/main.c | 176 |
2 files changed, 193 insertions, 0 deletions
diff --git a/src/fzenity/Makefile.in b/src/fzenity/Makefile.in new file mode 100644 index 000000000..d9f976165 --- /dev/null +++ b/src/fzenity/Makefile.in | |||
@@ -0,0 +1,17 @@ | |||
1 | .PHONY: all | ||
2 | all: fzenity | ||
3 | |||
4 | include ../common.mk | ||
5 | |||
6 | %.o : %.c $(H_FILE_LIST) ../include/common.h | ||
7 | $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(INCLUDE) -c $< -o $@ | ||
8 | |||
9 | fzenity: $(OBJS) | ||
10 | $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(EXTRA_LDFLAGS) | ||
11 | |||
12 | .PHONY: clean | ||
13 | clean:; rm -fr *.o fzenity *.gcov *.gcda *.gcno *.plist | ||
14 | |||
15 | .PHONY: distclean | ||
16 | distclean: clean | ||
17 | rm -fr Makefile | ||
diff --git a/src/fzenity/main.c b/src/fzenity/main.c new file mode 100644 index 000000000..4a0d3abac --- /dev/null +++ b/src/fzenity/main.c | |||
@@ -0,0 +1,176 @@ | |||
1 | #include "../include/common.h" | ||
2 | #include <sys/ioctl.h> | ||
3 | |||
4 | static char *arg_title = NULL; | ||
5 | static char *arg_text = NULL; | ||
6 | static int arg_info = 0; | ||
7 | static int arg_question = 0; | ||
8 | |||
9 | static inline void ansi_topleft(void) { | ||
10 | char str[] = {0x1b, '[', '1', ';', '1', 'H', '\0'}; | ||
11 | printf("%s", str); | ||
12 | fflush(0); | ||
13 | } | ||
14 | |||
15 | static inline void ansi_clrscr(void) { | ||
16 | ansi_topleft(); | ||
17 | char str[] = {0x1b, '[', '0', 'J', '\0'}; | ||
18 | printf("%s", str); | ||
19 | fflush(0); | ||
20 | } | ||
21 | |||
22 | char *remove_markup(char *in) { | ||
23 | char *out = malloc(strlen(in) + 1); | ||
24 | if (!out) | ||
25 | errExit("malloc"); | ||
26 | memset(out, 0, strlen(in) + 1); | ||
27 | |||
28 | char *ptr = in; | ||
29 | char *outptr = out; | ||
30 | while (*ptr != '\0') { | ||
31 | // skip <> markup | ||
32 | if (*ptr == '<') { | ||
33 | while (*ptr != '\0' && *ptr != '>') | ||
34 | ptr++; | ||
35 | if (*ptr == '\0') { | ||
36 | fprintf(stderr, "Error: invalid markup\n"); | ||
37 | exit(0); | ||
38 | } | ||
39 | ptr++; | ||
40 | } | ||
41 | // replace literal \n with char '\n' | ||
42 | else if (*ptr == '\\' && *(ptr + 1) == 'n') { | ||
43 | ptr += 2; | ||
44 | *outptr++ = '\n'; | ||
45 | continue; | ||
46 | } | ||
47 | // replace '/n' with ' ' | ||
48 | else if (*ptr == '\n') { | ||
49 | if (*(ptr + 1) == '\n') { | ||
50 | *outptr++ = '\n'; | ||
51 | *outptr++ = '\n'; | ||
52 | ptr += 2; | ||
53 | } | ||
54 | else { | ||
55 | *outptr++ = ' '; | ||
56 | ptr++; | ||
57 | } | ||
58 | } | ||
59 | else | ||
60 | *outptr++ = *ptr++; | ||
61 | } | ||
62 | |||
63 | return out; | ||
64 | } | ||
65 | |||
66 | char *print_line(char *in, int col) { | ||
67 | char *ptr = in; | ||
68 | int i = 0; | ||
69 | while (*ptr != '\n' && *ptr != '\0' && i < col) { | ||
70 | ptr++; | ||
71 | i++; | ||
72 | } | ||
73 | |||
74 | if (*ptr == '\n') { | ||
75 | *ptr++ = '\0'; | ||
76 | printf("%s\n", in); | ||
77 | return ptr++; | ||
78 | } | ||
79 | else if (i == col) { | ||
80 | while (*ptr != ' ' && ptr != in) | ||
81 | ptr--; | ||
82 | *ptr++ = '\0'; | ||
83 | printf("%s\n", in); | ||
84 | return ptr; | ||
85 | } | ||
86 | assert(0); | ||
87 | return NULL; | ||
88 | } | ||
89 | |||
90 | void paginate(char *in) { | ||
91 | struct winsize w; | ||
92 | int col = 80; | ||
93 | if (ioctl(0, TIOCGWINSZ, &w) == 0) | ||
94 | col = w.ws_col; | ||
95 | |||
96 | char *ptr = in; | ||
97 | while (*ptr != '\0') { | ||
98 | if (strlen(ptr) < col) { | ||
99 | printf("%s", ptr); | ||
100 | return; | ||
101 | } | ||
102 | ptr =print_line(ptr, col); | ||
103 | } | ||
104 | |||
105 | return; | ||
106 | } | ||
107 | |||
108 | static void info(void) { | ||
109 | ansi_clrscr(); | ||
110 | if (arg_text == NULL) { | ||
111 | fprintf(stderr, "Error: --text argument required\n"); | ||
112 | exit(1); | ||
113 | } | ||
114 | |||
115 | if (arg_title) | ||
116 | printf("%s\n\n", arg_title); | ||
117 | |||
118 | char *ptr = strstr(arg_text, "Press OK to continue"); | ||
119 | if (ptr) | ||
120 | *ptr = '\0'; | ||
121 | char *out = remove_markup(arg_text); | ||
122 | paginate(out); | ||
123 | free(out); | ||
124 | |||
125 | printf("\nContinue? (Y/N): "); | ||
126 | |||
127 | int c = getchar(); | ||
128 | if (c == 'y' || c == 'Y') | ||
129 | exit(0); | ||
130 | exit(1); | ||
131 | } | ||
132 | |||
133 | static void question(void) { | ||
134 | ansi_clrscr(); | ||
135 | if (arg_text == NULL) { | ||
136 | fprintf(stderr, "Error: --text argument required\n"); | ||
137 | exit(1); | ||
138 | } | ||
139 | |||
140 | if (arg_title) | ||
141 | printf("%s\n\n", arg_title); | ||
142 | |||
143 | char *ptr = strstr(arg_text, "Press OK to continue"); | ||
144 | if (ptr) | ||
145 | *ptr = '\0'; | ||
146 | char *out = remove_markup(arg_text); | ||
147 | paginate(out); | ||
148 | free(out); | ||
149 | |||
150 | printf("\n\n(Y/N): "); | ||
151 | |||
152 | int c = getchar(); | ||
153 | if (c == 'y' || c == 'Y') | ||
154 | exit(0); | ||
155 | exit(1); | ||
156 | } | ||
157 | |||
158 | int main(int argc, char **argv) { | ||
159 | int i; | ||
160 | for (i = 1; i < argc; i++) { | ||
161 | //printf("argv %d: #%s#\n", i, argv[i]); | ||
162 | if (strcmp(argv[i], "--info") == 0) | ||
163 | arg_info = 1; | ||
164 | else if (strcmp(argv[i], "--question") == 0) | ||
165 | arg_question = 1; | ||
166 | else if (strncmp(argv[i], "--text=", 7) == 0) | ||
167 | arg_text = argv[i] + 7; | ||
168 | } | ||
169 | |||
170 | if (arg_question) | ||
171 | question(); | ||
172 | else if (arg_info) | ||
173 | info(); | ||
174 | |||
175 | return 0; | ||
176 | } | ||