aboutsummaryrefslogtreecommitdiffstats
path: root/src/fzenity/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fzenity/main.c')
-rw-r--r--src/fzenity/main.c176
1 files changed, 176 insertions, 0 deletions
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
4static char *arg_title = NULL;
5static char *arg_text = NULL;
6static int arg_info = 0;
7static int arg_question = 0;
8
9static inline void ansi_topleft(void) {
10 char str[] = {0x1b, '[', '1', ';', '1', 'H', '\0'};
11 printf("%s", str);
12 fflush(0);
13}
14
15static inline void ansi_clrscr(void) {
16 ansi_topleft();
17 char str[] = {0x1b, '[', '0', 'J', '\0'};
18 printf("%s", str);
19 fflush(0);
20}
21
22char *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
66char *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
90void 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
108static 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
133static 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
158int 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}