1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <malloc.h> |
---|
5 | #include <ctype.h> |
---|
6 | |
---|
7 | static const char fileIdent[] = "$Id$"; |
---|
8 | |
---|
9 | void sepbar(char *in) { |
---|
10 | char buff[1024]; |
---|
11 | WINDOW *sepwin; |
---|
12 | owl_messagelist *ml; |
---|
13 | owl_view *v; |
---|
14 | int x, y, i; |
---|
15 | char *foo, *appendtosepbar; |
---|
16 | |
---|
17 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
18 | ml=owl_global_get_msglist(&g); |
---|
19 | v=owl_global_get_current_view(&g); |
---|
20 | |
---|
21 | werase(sepwin); |
---|
22 | wattron(sepwin, A_REVERSE); |
---|
23 | whline(sepwin, ACS_HLINE, owl_global_get_cols(&g)); |
---|
24 | |
---|
25 | wmove(sepwin, 0, 2); |
---|
26 | |
---|
27 | if (owl_messagelist_get_size(ml)==0) { |
---|
28 | strcpy(buff, " (-/-) "); |
---|
29 | } else { |
---|
30 | sprintf(buff, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1, |
---|
31 | owl_view_get_size(v), |
---|
32 | owl_messagelist_get_size(ml)); |
---|
33 | } |
---|
34 | waddstr(sepwin, buff); |
---|
35 | |
---|
36 | foo=owl_view_get_filtname(v); |
---|
37 | if (strcmp(foo, "all")) wattroff(sepwin, A_REVERSE); |
---|
38 | waddstr(sepwin, " "); |
---|
39 | waddstr(sepwin, owl_view_get_filtname(v)); |
---|
40 | waddstr(sepwin, " "); |
---|
41 | if (strcmp(foo, "all")) wattron(sepwin, A_REVERSE); |
---|
42 | |
---|
43 | if (owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) { |
---|
44 | getyx(sepwin, y, x); |
---|
45 | wmove(sepwin, y, x+2); |
---|
46 | wattron(sepwin, A_BOLD); |
---|
47 | waddstr(sepwin, " <truncated> "); |
---|
48 | wattroff(sepwin, A_BOLD); |
---|
49 | } |
---|
50 | |
---|
51 | i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g)); |
---|
52 | if ((i != -1) && |
---|
53 | (i < owl_view_get_size(v)-1)) { |
---|
54 | getyx(sepwin, y, x); |
---|
55 | wmove(sepwin, y, x+2); |
---|
56 | wattron(sepwin, A_BOLD); |
---|
57 | waddstr(sepwin, " <more> "); |
---|
58 | wattroff(sepwin, A_BOLD); |
---|
59 | } |
---|
60 | |
---|
61 | if (owl_global_get_rightshift(&g)>0) { |
---|
62 | getyx(sepwin, y, x); |
---|
63 | wmove(sepwin, y, x+2); |
---|
64 | sprintf(buff, " right: %i ", owl_global_get_rightshift(&g)); |
---|
65 | waddstr(sepwin, buff); |
---|
66 | } |
---|
67 | |
---|
68 | if (owl_global_is_zaway(&g)) { |
---|
69 | getyx(sepwin, y, x); |
---|
70 | wmove(sepwin, y, x+2); |
---|
71 | wattron(sepwin, A_BOLD); |
---|
72 | wattroff(sepwin, A_REVERSE); |
---|
73 | waddstr(sepwin, " ZAWAY "); |
---|
74 | wattron(sepwin, A_REVERSE); |
---|
75 | wattroff(sepwin, A_BOLD); |
---|
76 | } |
---|
77 | |
---|
78 | if (owl_global_get_curmsg_vert_offset(&g)) { |
---|
79 | getyx(sepwin, y, x); |
---|
80 | wmove(sepwin, y, x+2); |
---|
81 | wattron(sepwin, A_BOLD); |
---|
82 | wattroff(sepwin, A_REVERSE); |
---|
83 | waddstr(sepwin, " SCROLL "); |
---|
84 | wattron(sepwin, A_REVERSE); |
---|
85 | wattroff(sepwin, A_BOLD); |
---|
86 | } |
---|
87 | |
---|
88 | if (in) { |
---|
89 | getyx(sepwin, y, x); |
---|
90 | wmove(sepwin, y, x+2); |
---|
91 | waddstr(sepwin, in); |
---|
92 | } |
---|
93 | |
---|
94 | appendtosepbar = owl_global_get_appendtosepbar(&g); |
---|
95 | if (appendtosepbar && *appendtosepbar) { |
---|
96 | getyx(sepwin, y, x); |
---|
97 | wmove(sepwin, y, x+2); |
---|
98 | waddstr(sepwin, " "); |
---|
99 | waddstr(sepwin, owl_global_get_appendtosepbar(&g)); |
---|
100 | waddstr(sepwin, " "); |
---|
101 | } |
---|
102 | |
---|
103 | getyx(sepwin, y, x); |
---|
104 | wmove(sepwin, y, owl_global_get_cols(&g)-1); |
---|
105 | |
---|
106 | wattroff(sepwin, A_BOLD); |
---|
107 | wattroff(sepwin, A_REVERSE); |
---|
108 | wnoutrefresh(sepwin); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void pophandler_quit(int ch) { |
---|
113 | if (ch=='q') { |
---|
114 | owl_popwin_close(owl_global_get_popwin(&g)); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | char **atokenize(char *buffer, char *sep, int *i) { |
---|
119 | /* each element of return must be freed by user */ |
---|
120 | char **args; |
---|
121 | char *workbuff, *foo; |
---|
122 | int done=0, first=1, count=0; |
---|
123 | |
---|
124 | workbuff=owl_malloc(strlen(buffer)+1); |
---|
125 | memcpy(workbuff, buffer, strlen(buffer)+1); |
---|
126 | |
---|
127 | args=NULL; |
---|
128 | while (!done) { |
---|
129 | if (first) { |
---|
130 | first=0; |
---|
131 | foo=(char *)strtok(workbuff, sep); |
---|
132 | } else { |
---|
133 | foo=(char *)strtok(NULL, sep); |
---|
134 | } |
---|
135 | if (foo==NULL) { |
---|
136 | done=1; |
---|
137 | } else { |
---|
138 | args=(char **)owl_realloc(args, sizeof(char *) * (count+1)); |
---|
139 | args[count]=owl_malloc(strlen(foo)+1); |
---|
140 | strcpy(args[count], foo); |
---|
141 | count++; |
---|
142 | } |
---|
143 | } |
---|
144 | *i=count; |
---|
145 | owl_free(workbuff); |
---|
146 | return(args); |
---|
147 | } |
---|
148 | |
---|
149 | /* skips n tokens and returns where that would be. |
---|
150 | * TODO: handle quotes. */ |
---|
151 | char *skiptokens(char *buff, int n) { |
---|
152 | int inquotes=0; |
---|
153 | while (*buff && n>0) { |
---|
154 | while (*buff == ' ') buff++; |
---|
155 | while (*buff && (inquotes || *buff != ' ')) { |
---|
156 | if (*buff == '"') inquotes=!inquotes; |
---|
157 | buff++; |
---|
158 | } |
---|
159 | while (*buff == ' ') buff++; |
---|
160 | n--; |
---|
161 | } |
---|
162 | return buff; |
---|
163 | } |
---|
164 | |
---|
165 | void atokenize_free(char **tok, int nels) { |
---|
166 | int i; |
---|
167 | for (i=0; i<nels; i++) { |
---|
168 | owl_free(tok[i]); |
---|
169 | } |
---|
170 | owl_free(tok); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | void owl_parsefree(char **argv, int argc) { |
---|
175 | int i; |
---|
176 | |
---|
177 | if (!argv) return; |
---|
178 | |
---|
179 | for (i=0; i<argc; i++) { |
---|
180 | if (argv[i]) owl_free(argv[i]); |
---|
181 | } |
---|
182 | owl_free(argv); |
---|
183 | } |
---|
184 | |
---|
185 | char **owl_parseline(char *line, int *argc) { |
---|
186 | /* break a command line up into argv, argc. The caller must free |
---|
187 | the returned values. If there is an error argc will be set to |
---|
188 | -1, argv will be NULL and the caller does not need to free |
---|
189 | anything */ |
---|
190 | |
---|
191 | char **argv; |
---|
192 | int i, len, between=1; |
---|
193 | char *curarg; |
---|
194 | char quote; |
---|
195 | |
---|
196 | argv=owl_malloc(sizeof(char *)); |
---|
197 | len=strlen(line); |
---|
198 | curarg=owl_malloc(len+10); |
---|
199 | strcpy(curarg, ""); |
---|
200 | quote='\0'; |
---|
201 | *argc=0; |
---|
202 | for (i=0; i<len+1; i++) { |
---|
203 | /* find the first real character */ |
---|
204 | if (between) { |
---|
205 | if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') { |
---|
206 | continue; |
---|
207 | } else { |
---|
208 | between=0; |
---|
209 | i--; |
---|
210 | continue; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | /* deal with a quote character */ |
---|
215 | if (line[i]=='"' || line[i]=="'"[0]) { |
---|
216 | /* if this type of quote is open, close it */ |
---|
217 | if (quote==line[i]) { |
---|
218 | quote='\0'; |
---|
219 | continue; |
---|
220 | } |
---|
221 | |
---|
222 | /* if no quoting is open then open with this */ |
---|
223 | if (quote=='\0') { |
---|
224 | quote=line[i]; |
---|
225 | continue; |
---|
226 | } |
---|
227 | |
---|
228 | /* if another type of quote is open then treat this as a literal */ |
---|
229 | curarg[strlen(curarg)+1]='\0'; |
---|
230 | curarg[strlen(curarg)]=line[i]; |
---|
231 | continue; |
---|
232 | } |
---|
233 | |
---|
234 | /* if it's not a space or end of command, then use it */ |
---|
235 | if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') { |
---|
236 | curarg[strlen(curarg)+1]='\0'; |
---|
237 | curarg[strlen(curarg)]=line[i]; |
---|
238 | continue; |
---|
239 | } |
---|
240 | |
---|
241 | /* otherwise, if we're not in quotes, add the whole argument */ |
---|
242 | if (quote=='\0') { |
---|
243 | /* add the argument */ |
---|
244 | argv=owl_realloc(argv, sizeof(char *)*((*argc)+1)); |
---|
245 | argv[*argc]=owl_malloc(strlen(curarg)+2); |
---|
246 | strcpy(argv[*argc], curarg); |
---|
247 | *argc=*argc+1; |
---|
248 | strcpy(curarg, ""); |
---|
249 | between=1; |
---|
250 | continue; |
---|
251 | } |
---|
252 | |
---|
253 | /* if it is a space and we're in quotes, then use it */ |
---|
254 | curarg[strlen(curarg)+1]='\0'; |
---|
255 | curarg[strlen(curarg)]=line[i]; |
---|
256 | } |
---|
257 | |
---|
258 | /* check for unbalanced quotes */ |
---|
259 | if (quote!='\0') { |
---|
260 | owl_parsefree(argv, *argc); |
---|
261 | *argc=-1; |
---|
262 | return(NULL); |
---|
263 | } |
---|
264 | |
---|
265 | return(argv); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | int owl_util_find_trans(char *in, int len) { |
---|
271 | /* return the index of the last char before a change from the first |
---|
272 | one */ |
---|
273 | int i; |
---|
274 | for (i=1; i<len; i++) { |
---|
275 | if (in[i] != in[0]) return(i-1); |
---|
276 | } |
---|
277 | return(i); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | void downstr(char *foo) { |
---|
282 | int i; |
---|
283 | for (i=0; foo[i]!='\0'; i++) { |
---|
284 | foo[i]=tolower(foo[i]); |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | char *stristr(char *a, char *b) { |
---|
289 | char *x, *y, *ret; |
---|
290 | |
---|
291 | if ((x=owl_strdup(a))==NULL) return(NULL); |
---|
292 | if ((y=owl_strdup(b))==NULL) return(NULL); |
---|
293 | downstr(x); |
---|
294 | downstr(y); |
---|
295 | ret=strstr(x, y); |
---|
296 | owl_free(x); |
---|
297 | owl_free(y); |
---|
298 | return(ret); |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | void *owl_malloc(size_t size) { |
---|
303 | return(malloc(size)); |
---|
304 | } |
---|
305 | |
---|
306 | void owl_free(void *ptr) { |
---|
307 | free(ptr); |
---|
308 | } |
---|
309 | |
---|
310 | char *owl_strdup(const char *s1) { |
---|
311 | return(strdup(s1)); |
---|
312 | } |
---|
313 | |
---|
314 | void *owl_realloc(void *ptr, size_t size) { |
---|
315 | return(realloc(ptr, size)); |
---|
316 | } |
---|
317 | |
---|
318 | char *pretty_sender(char *in) { |
---|
319 | char *out, *ptr; |
---|
320 | |
---|
321 | /* the caller must free the return */ |
---|
322 | out=owl_strdup(in); |
---|
323 | ptr=strchr(out, '@'); |
---|
324 | if (ptr) { |
---|
325 | if (!strcasecmp(ptr+1, ZGetRealm())) { |
---|
326 | *ptr='\0'; |
---|
327 | } |
---|
328 | } |
---|
329 | return(out); |
---|
330 | } |
---|
331 | |
---|
332 | char *long_sender(char *in) { |
---|
333 | char *out, *ptr; |
---|
334 | |
---|
335 | /* the caller must free the return */ |
---|
336 | out=owl_malloc(strlen(in)+100); |
---|
337 | strcpy(out, in); |
---|
338 | ptr=strchr(out, '@'); |
---|
339 | if (ptr) return(out); |
---|
340 | sprintf(out, "%s@%s", out, ZGetRealm()); |
---|
341 | return(out); |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | char *owl_getquoting(char *line) { |
---|
346 | if (line[0]=='\0') return("'"); |
---|
347 | if (strchr(line, '\'')) return("\""); |
---|
348 | if (strchr(line, '"')) return("'"); |
---|
349 | if (strchr(line, ' ')) return("'"); |
---|
350 | return(""); |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | int owl_util_string_to_color(char *color) { |
---|
355 | if (!strcasecmp(color, "black")) { |
---|
356 | return(OWL_COLOR_BLACK); |
---|
357 | } else if (!strcasecmp(color, "red")) { |
---|
358 | return(OWL_COLOR_RED); |
---|
359 | } else if (!strcasecmp(color, "green")) { |
---|
360 | return(OWL_COLOR_GREEN); |
---|
361 | } else if (!strcasecmp(color, "yellow")) { |
---|
362 | return(OWL_COLOR_YELLOW); |
---|
363 | } else if (!strcasecmp(color, "blue")) { |
---|
364 | return(OWL_COLOR_BLUE); |
---|
365 | } else if (!strcasecmp(color, "magenta")) { |
---|
366 | return(OWL_COLOR_MAGENTA); |
---|
367 | } else if (!strcasecmp(color, "cyan")) { |
---|
368 | return(OWL_COLOR_CYAN); |
---|
369 | } else if (!strcasecmp(color, "white")) { |
---|
370 | return(OWL_COLOR_WHITE); |
---|
371 | } else if (!strcasecmp(color, "default")) { |
---|
372 | return(OWL_COLOR_DEFAULT); |
---|
373 | } |
---|
374 | return(OWL_COLOR_DEFAULT); |
---|
375 | } |
---|
376 | |
---|
377 | char *owl_util_color_to_string(int color) { |
---|
378 | if (color==OWL_COLOR_BLACK) return("black"); |
---|
379 | if (color==OWL_COLOR_RED) return("red"); |
---|
380 | if (color==OWL_COLOR_GREEN) return("green"); |
---|
381 | if (color==OWL_COLOR_YELLOW) return("yellow"); |
---|
382 | if (color==OWL_COLOR_BLUE) return("blue"); |
---|
383 | if (color==OWL_COLOR_MAGENTA) return("magenta"); |
---|
384 | if (color==OWL_COLOR_CYAN) return("cyan"); |
---|
385 | if (color==OWL_COLOR_WHITE) return("white"); |
---|
386 | if (color==OWL_COLOR_DEFAULT) return("default"); |
---|
387 | return("Unknown color"); |
---|
388 | } |
---|