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 | snprintf(buff, 1024, " (%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 | snprintf(buff, 1024, " 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 | /* allocates memory and returns the string or null. |
---|
319 | * caller must free the string. |
---|
320 | * from Linux sprintf man page. |
---|
321 | */ |
---|
322 | char *owl_sprintf(const char *fmt, ...) { |
---|
323 | int n, size = 100; |
---|
324 | char *p; |
---|
325 | va_list ap; |
---|
326 | if ((p = owl_malloc (size)) == NULL) |
---|
327 | return NULL; |
---|
328 | while (1) { |
---|
329 | /* Try to print in the allocated space. */ |
---|
330 | va_start(ap, fmt); |
---|
331 | n = vsnprintf (p, size, fmt, ap); |
---|
332 | va_end(ap); |
---|
333 | /* If that worked, return the string. */ |
---|
334 | if (n > -1 && n < size) |
---|
335 | return p; |
---|
336 | /* Else try again with more space. */ |
---|
337 | if (n > -1) /* glibc 2.1 */ |
---|
338 | size = n+1; /* precisely what is needed */ |
---|
339 | else /* glibc 2.0 */ |
---|
340 | size *= 2; /* twice the old size */ |
---|
341 | if ((p = owl_realloc (p, size)) == NULL) |
---|
342 | return NULL; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | char *pretty_sender(char *in) { |
---|
347 | char *out, *ptr; |
---|
348 | |
---|
349 | /* the caller must free the return */ |
---|
350 | out=owl_strdup(in); |
---|
351 | ptr=strchr(out, '@'); |
---|
352 | if (ptr) { |
---|
353 | if (!strcasecmp(ptr+1, ZGetRealm())) { |
---|
354 | *ptr='\0'; |
---|
355 | } |
---|
356 | } |
---|
357 | return(out); |
---|
358 | } |
---|
359 | |
---|
360 | char *long_sender(char *in) { |
---|
361 | char *ptr; |
---|
362 | |
---|
363 | /* the caller must free the return */ |
---|
364 | if (NULL != (ptr=strchr(in, '@'))) { |
---|
365 | return owl_strdup(in); |
---|
366 | } else { |
---|
367 | return owl_sprintf("%s@%s", in, ZGetRealm()); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | |
---|
372 | char *owl_getquoting(char *line) { |
---|
373 | if (line[0]=='\0') return("'"); |
---|
374 | if (strchr(line, '\'')) return("\""); |
---|
375 | if (strchr(line, '"')) return("'"); |
---|
376 | if (strchr(line, ' ')) return("'"); |
---|
377 | return(""); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | int owl_util_string_to_color(char *color) { |
---|
382 | if (!strcasecmp(color, "black")) { |
---|
383 | return(OWL_COLOR_BLACK); |
---|
384 | } else if (!strcasecmp(color, "red")) { |
---|
385 | return(OWL_COLOR_RED); |
---|
386 | } else if (!strcasecmp(color, "green")) { |
---|
387 | return(OWL_COLOR_GREEN); |
---|
388 | } else if (!strcasecmp(color, "yellow")) { |
---|
389 | return(OWL_COLOR_YELLOW); |
---|
390 | } else if (!strcasecmp(color, "blue")) { |
---|
391 | return(OWL_COLOR_BLUE); |
---|
392 | } else if (!strcasecmp(color, "magenta")) { |
---|
393 | return(OWL_COLOR_MAGENTA); |
---|
394 | } else if (!strcasecmp(color, "cyan")) { |
---|
395 | return(OWL_COLOR_CYAN); |
---|
396 | } else if (!strcasecmp(color, "white")) { |
---|
397 | return(OWL_COLOR_WHITE); |
---|
398 | } else if (!strcasecmp(color, "default")) { |
---|
399 | return(OWL_COLOR_DEFAULT); |
---|
400 | } |
---|
401 | return(OWL_COLOR_DEFAULT); |
---|
402 | } |
---|
403 | |
---|
404 | char *owl_util_color_to_string(int color) { |
---|
405 | if (color==OWL_COLOR_BLACK) return("black"); |
---|
406 | if (color==OWL_COLOR_RED) return("red"); |
---|
407 | if (color==OWL_COLOR_GREEN) return("green"); |
---|
408 | if (color==OWL_COLOR_YELLOW) return("yellow"); |
---|
409 | if (color==OWL_COLOR_BLUE) return("blue"); |
---|
410 | if (color==OWL_COLOR_MAGENTA) return("magenta"); |
---|
411 | if (color==OWL_COLOR_CYAN) return("cyan"); |
---|
412 | if (color==OWL_COLOR_WHITE) return("white"); |
---|
413 | if (color==OWL_COLOR_DEFAULT) return("default"); |
---|
414 | return("Unknown color"); |
---|
415 | } |
---|