1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <unistd.h> |
---|
6 | #include <ctype.h> |
---|
7 | |
---|
8 | static const char fileIdent[] = "$Id$"; |
---|
9 | |
---|
10 | void sepbar(char *in) |
---|
11 | { |
---|
12 | char buff[1024]; |
---|
13 | WINDOW *sepwin; |
---|
14 | owl_messagelist *ml; |
---|
15 | owl_view *v; |
---|
16 | int x, y, i; |
---|
17 | char *foo, *appendtosepbar; |
---|
18 | |
---|
19 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
20 | ml=owl_global_get_msglist(&g); |
---|
21 | v=owl_global_get_current_view(&g); |
---|
22 | |
---|
23 | werase(sepwin); |
---|
24 | wattron(sepwin, A_REVERSE); |
---|
25 | if (owl_global_is_fancylines(&g)) { |
---|
26 | whline(sepwin, ACS_HLINE, owl_global_get_cols(&g)); |
---|
27 | } else { |
---|
28 | whline(sepwin, '-', owl_global_get_cols(&g)); |
---|
29 | } |
---|
30 | |
---|
31 | if (owl_global_is_sepbar_disable(&g)) { |
---|
32 | getyx(sepwin, y, x); |
---|
33 | wmove(sepwin, y, owl_global_get_cols(&g)-1); |
---|
34 | return; |
---|
35 | } |
---|
36 | |
---|
37 | wmove(sepwin, 0, 2); |
---|
38 | |
---|
39 | if (owl_messagelist_get_size(ml)==0) { |
---|
40 | strcpy(buff, " (-/-) "); |
---|
41 | } else { |
---|
42 | snprintf(buff, 1024, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1, |
---|
43 | owl_view_get_size(v), |
---|
44 | owl_messagelist_get_size(ml)); |
---|
45 | } |
---|
46 | waddstr(sepwin, buff); |
---|
47 | |
---|
48 | foo=owl_view_get_filtname(v); |
---|
49 | if (strcmp(foo, "all")) wattroff(sepwin, A_REVERSE); |
---|
50 | waddstr(sepwin, " "); |
---|
51 | waddstr(sepwin, owl_view_get_filtname(v)); |
---|
52 | waddstr(sepwin, " "); |
---|
53 | if (strcmp(foo, "all")) wattron(sepwin, A_REVERSE); |
---|
54 | |
---|
55 | if (owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) { |
---|
56 | getyx(sepwin, y, x); |
---|
57 | wmove(sepwin, y, x+2); |
---|
58 | wattron(sepwin, A_BOLD); |
---|
59 | waddstr(sepwin, " <truncated> "); |
---|
60 | wattroff(sepwin, A_BOLD); |
---|
61 | } |
---|
62 | |
---|
63 | i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g)); |
---|
64 | if ((i != -1) && |
---|
65 | (i < owl_view_get_size(v)-1)) { |
---|
66 | getyx(sepwin, y, x); |
---|
67 | wmove(sepwin, y, x+2); |
---|
68 | wattron(sepwin, A_BOLD); |
---|
69 | waddstr(sepwin, " <more> "); |
---|
70 | wattroff(sepwin, A_BOLD); |
---|
71 | } |
---|
72 | |
---|
73 | if (owl_global_get_rightshift(&g)>0) { |
---|
74 | getyx(sepwin, y, x); |
---|
75 | wmove(sepwin, y, x+2); |
---|
76 | snprintf(buff, 1024, " right: %i ", owl_global_get_rightshift(&g)); |
---|
77 | waddstr(sepwin, buff); |
---|
78 | } |
---|
79 | |
---|
80 | if (owl_global_is_zaway(&g)) { |
---|
81 | getyx(sepwin, y, x); |
---|
82 | wmove(sepwin, y, x+2); |
---|
83 | wattron(sepwin, A_BOLD); |
---|
84 | wattroff(sepwin, A_REVERSE); |
---|
85 | waddstr(sepwin, " ZAWAY "); |
---|
86 | wattron(sepwin, A_REVERSE); |
---|
87 | wattroff(sepwin, A_BOLD); |
---|
88 | } |
---|
89 | |
---|
90 | if (owl_global_get_curmsg_vert_offset(&g)) { |
---|
91 | getyx(sepwin, y, x); |
---|
92 | wmove(sepwin, y, x+2); |
---|
93 | wattron(sepwin, A_BOLD); |
---|
94 | wattroff(sepwin, A_REVERSE); |
---|
95 | waddstr(sepwin, " SCROLL "); |
---|
96 | wattron(sepwin, A_REVERSE); |
---|
97 | wattroff(sepwin, A_BOLD); |
---|
98 | } |
---|
99 | |
---|
100 | if (in) { |
---|
101 | getyx(sepwin, y, x); |
---|
102 | wmove(sepwin, y, x+2); |
---|
103 | waddstr(sepwin, in); |
---|
104 | } |
---|
105 | |
---|
106 | appendtosepbar = owl_global_get_appendtosepbar(&g); |
---|
107 | if (appendtosepbar && *appendtosepbar) { |
---|
108 | getyx(sepwin, y, x); |
---|
109 | wmove(sepwin, y, x+2); |
---|
110 | waddstr(sepwin, " "); |
---|
111 | waddstr(sepwin, owl_global_get_appendtosepbar(&g)); |
---|
112 | waddstr(sepwin, " "); |
---|
113 | } |
---|
114 | |
---|
115 | getyx(sepwin, y, x); |
---|
116 | wmove(sepwin, y, owl_global_get_cols(&g)-1); |
---|
117 | |
---|
118 | wattroff(sepwin, A_BOLD); |
---|
119 | wattroff(sepwin, A_REVERSE); |
---|
120 | wnoutrefresh(sepwin); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void pophandler_quit(int ch) |
---|
125 | { |
---|
126 | if (ch=='q') { |
---|
127 | owl_popwin_close(owl_global_get_popwin(&g)); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | char **atokenize(char *buffer, char *sep, int *i) |
---|
132 | { |
---|
133 | /* each element of return must be freed by user */ |
---|
134 | char **args; |
---|
135 | char *workbuff, *foo; |
---|
136 | int done=0, first=1, count=0; |
---|
137 | |
---|
138 | workbuff=owl_malloc(strlen(buffer)+1); |
---|
139 | memcpy(workbuff, buffer, strlen(buffer)+1); |
---|
140 | |
---|
141 | args=NULL; |
---|
142 | while (!done) { |
---|
143 | if (first) { |
---|
144 | first=0; |
---|
145 | foo=(char *)strtok(workbuff, sep); |
---|
146 | } else { |
---|
147 | foo=(char *)strtok(NULL, sep); |
---|
148 | } |
---|
149 | if (foo==NULL) { |
---|
150 | done=1; |
---|
151 | } else { |
---|
152 | args=(char **)owl_realloc(args, sizeof(char *) * (count+1)); |
---|
153 | args[count]=owl_malloc(strlen(foo)+1); |
---|
154 | strcpy(args[count], foo); |
---|
155 | count++; |
---|
156 | } |
---|
157 | } |
---|
158 | *i=count; |
---|
159 | owl_free(workbuff); |
---|
160 | return(args); |
---|
161 | } |
---|
162 | |
---|
163 | char *skiptokens(char *buff, int n) { |
---|
164 | /* skips n tokens and returns where that would be. |
---|
165 | * TODO: handle quotes more sanely. */ |
---|
166 | |
---|
167 | int inquotes=0; |
---|
168 | while (*buff && n>0) { |
---|
169 | while (*buff == ' ') buff++; |
---|
170 | while (*buff && (inquotes || *buff != ' ')) { |
---|
171 | if (*buff == '"' || *buff == '\'') inquotes=!inquotes; |
---|
172 | buff++; |
---|
173 | } |
---|
174 | while (*buff == ' ') buff++; |
---|
175 | n--; |
---|
176 | } |
---|
177 | return buff; |
---|
178 | } |
---|
179 | |
---|
180 | void atokenize_free(char **tok, int nels) |
---|
181 | { |
---|
182 | int i; |
---|
183 | for (i=0; i<nels; i++) { |
---|
184 | owl_free(tok[i]); |
---|
185 | } |
---|
186 | owl_free(tok); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void owl_parsefree(char **argv, int argc) |
---|
191 | { |
---|
192 | int i; |
---|
193 | |
---|
194 | if (!argv) return; |
---|
195 | |
---|
196 | for (i=0; i<argc; i++) { |
---|
197 | if (argv[i]) owl_free(argv[i]); |
---|
198 | } |
---|
199 | owl_free(argv); |
---|
200 | } |
---|
201 | |
---|
202 | char **owl_parseline(char *line, int *argc) |
---|
203 | { |
---|
204 | /* break a command line up into argv, argc. The caller must free |
---|
205 | the returned values. If there is an error argc will be set to |
---|
206 | -1, argv will be NULL and the caller does not need to free |
---|
207 | anything */ |
---|
208 | |
---|
209 | char **argv; |
---|
210 | int i, len, between=1; |
---|
211 | char *curarg; |
---|
212 | char quote; |
---|
213 | |
---|
214 | argv=owl_malloc(sizeof(char *)); |
---|
215 | len=strlen(line); |
---|
216 | curarg=owl_malloc(len+10); |
---|
217 | strcpy(curarg, ""); |
---|
218 | quote='\0'; |
---|
219 | *argc=0; |
---|
220 | for (i=0; i<len+1; i++) { |
---|
221 | /* find the first real character */ |
---|
222 | if (between) { |
---|
223 | if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') { |
---|
224 | continue; |
---|
225 | } else { |
---|
226 | between=0; |
---|
227 | i--; |
---|
228 | continue; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /* deal with a quote character */ |
---|
233 | if (line[i]=='"' || line[i]=="'"[0]) { |
---|
234 | /* if this type of quote is open, close it */ |
---|
235 | if (quote==line[i]) { |
---|
236 | quote='\0'; |
---|
237 | continue; |
---|
238 | } |
---|
239 | |
---|
240 | /* if no quoting is open then open with this */ |
---|
241 | if (quote=='\0') { |
---|
242 | quote=line[i]; |
---|
243 | continue; |
---|
244 | } |
---|
245 | |
---|
246 | /* if another type of quote is open then treat this as a literal */ |
---|
247 | curarg[strlen(curarg)+1]='\0'; |
---|
248 | curarg[strlen(curarg)]=line[i]; |
---|
249 | continue; |
---|
250 | } |
---|
251 | |
---|
252 | /* if it's not a space or end of command, then use it */ |
---|
253 | if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') { |
---|
254 | curarg[strlen(curarg)+1]='\0'; |
---|
255 | curarg[strlen(curarg)]=line[i]; |
---|
256 | continue; |
---|
257 | } |
---|
258 | |
---|
259 | /* otherwise, if we're not in quotes, add the whole argument */ |
---|
260 | if (quote=='\0') { |
---|
261 | /* add the argument */ |
---|
262 | argv=owl_realloc(argv, sizeof(char *)*((*argc)+1)); |
---|
263 | argv[*argc]=owl_malloc(strlen(curarg)+2); |
---|
264 | strcpy(argv[*argc], curarg); |
---|
265 | *argc=*argc+1; |
---|
266 | strcpy(curarg, ""); |
---|
267 | between=1; |
---|
268 | continue; |
---|
269 | } |
---|
270 | |
---|
271 | /* if it is a space and we're in quotes, then use it */ |
---|
272 | curarg[strlen(curarg)+1]='\0'; |
---|
273 | curarg[strlen(curarg)]=line[i]; |
---|
274 | } |
---|
275 | |
---|
276 | /* check for unbalanced quotes */ |
---|
277 | if (quote!='\0') { |
---|
278 | owl_parsefree(argv, *argc); |
---|
279 | *argc=-1; |
---|
280 | return(NULL); |
---|
281 | } |
---|
282 | |
---|
283 | return(argv); |
---|
284 | } |
---|
285 | |
---|
286 | /* caller must free the return */ |
---|
287 | char *owl_util_minutes_to_timestr(int in) |
---|
288 | { |
---|
289 | int days, hours; |
---|
290 | long run; |
---|
291 | char *out; |
---|
292 | |
---|
293 | run=in; |
---|
294 | |
---|
295 | days=run/1440; |
---|
296 | run-=days*1440; |
---|
297 | hours=run/60; |
---|
298 | run-=hours*60; |
---|
299 | |
---|
300 | if (days>0) { |
---|
301 | out=owl_sprintf("%i d %2.2i:%2.2i", days, hours, run); |
---|
302 | } else { |
---|
303 | out=owl_sprintf(" %2.2i:%2.2i", hours, run); |
---|
304 | } |
---|
305 | return(out); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | /* return the index of the last char before a change from the first one */ |
---|
310 | int owl_util_find_trans(char *in, int len) |
---|
311 | { |
---|
312 | int i; |
---|
313 | for (i=1; i<len; i++) { |
---|
314 | if (in[i] != in[0]) return(i-1); |
---|
315 | } |
---|
316 | return(i); |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | /* downcase the string 'foo' */ |
---|
321 | void downstr(char *foo) |
---|
322 | { |
---|
323 | int i; |
---|
324 | for (i=0; foo[i]!='\0'; i++) { |
---|
325 | foo[i]=tolower(foo[i]); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | /* exactly like strstr but case insensitive */ |
---|
330 | char *stristr(char *a, char *b) |
---|
331 | { |
---|
332 | char *x, *y, *ret; |
---|
333 | |
---|
334 | if ((x=owl_strdup(a))==NULL) return(NULL); |
---|
335 | if ((y=owl_strdup(b))==NULL) return(NULL); |
---|
336 | downstr(x); |
---|
337 | downstr(y); |
---|
338 | ret=strstr(x, y); |
---|
339 | if (ret==NULL) { |
---|
340 | owl_free(x); |
---|
341 | owl_free(y); |
---|
342 | return(NULL); |
---|
343 | } |
---|
344 | ret=ret-x+a; |
---|
345 | owl_free(x); |
---|
346 | owl_free(y); |
---|
347 | return(ret); |
---|
348 | } |
---|
349 | |
---|
350 | /* Caller must free response. |
---|
351 | * Takes in strings which are space-separated lists of tokens |
---|
352 | * and returns a single string containing no token more than once. |
---|
353 | * If prohibit is non-null, no token may start with a character |
---|
354 | * in prohibit. |
---|
355 | */ |
---|
356 | char *owl_util_uniq(char *A, char *B, char *prohibit) |
---|
357 | { |
---|
358 | |
---|
359 | char *cat, **tok; |
---|
360 | int toklen, i, j, first=1; |
---|
361 | cat = owl_malloc(strlen(A)+strlen(B)+3); |
---|
362 | strcpy(cat, A); |
---|
363 | strcat(cat, " "); |
---|
364 | strcat(cat, B); |
---|
365 | tok = atokenize(cat, " ", &toklen); |
---|
366 | strcpy(cat, ""); |
---|
367 | for (i=0; i<toklen; i++) { |
---|
368 | int dup=0; |
---|
369 | for (j=0; j<i; j++) { |
---|
370 | if (!strcmp(tok[i], tok[j])) dup=1; |
---|
371 | } |
---|
372 | if (!dup && (!prohibit || !strchr(prohibit, tok[i][0]))) { |
---|
373 | if (!first) { |
---|
374 | strcat(cat, " "); |
---|
375 | } |
---|
376 | first=0; |
---|
377 | strcat(cat, tok[i]); |
---|
378 | } |
---|
379 | } |
---|
380 | atokenize_free(tok, toklen); |
---|
381 | return(cat); |
---|
382 | } |
---|
383 | |
---|
384 | /* return 1 if a string is only whitespace, otherwise 0 */ |
---|
385 | int only_whitespace(char *s) |
---|
386 | { |
---|
387 | int i; |
---|
388 | for (i=0; s[i]; i++) { |
---|
389 | if (!isspace((int) s[i])) return(0); |
---|
390 | } |
---|
391 | return(1); |
---|
392 | } |
---|
393 | |
---|
394 | /* hooks for doing memory allocation et. al. in owl */ |
---|
395 | |
---|
396 | void *owl_malloc(size_t size) |
---|
397 | { |
---|
398 | return(malloc(size)); |
---|
399 | } |
---|
400 | |
---|
401 | void owl_free(void *ptr) |
---|
402 | { |
---|
403 | free(ptr); |
---|
404 | } |
---|
405 | |
---|
406 | char *owl_strdup(const char *s1) |
---|
407 | { |
---|
408 | return(strdup(s1)); |
---|
409 | } |
---|
410 | |
---|
411 | void *owl_realloc(void *ptr, size_t size) |
---|
412 | { |
---|
413 | return(realloc(ptr, size)); |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | /* allocates memory and returns the string or null. |
---|
418 | * caller must free the string. |
---|
419 | * from Linux sprintf man page. |
---|
420 | */ |
---|
421 | char *owl_sprintf(const char *fmt, ...) |
---|
422 | { |
---|
423 | int n, size = 100; |
---|
424 | char *p; |
---|
425 | va_list ap; |
---|
426 | if ((p = owl_malloc (size)) == NULL) return (NULL); |
---|
427 | while (1) { |
---|
428 | /* Try to print in the allocated space. */ |
---|
429 | va_start(ap, fmt); |
---|
430 | n = vsnprintf (p, size, fmt, ap); |
---|
431 | va_end(ap); |
---|
432 | /* If that worked, return the string. */ |
---|
433 | if (n > -1 && n < size) |
---|
434 | return p; |
---|
435 | /* Else try again with more space. */ |
---|
436 | if (n > -1) /* glibc 2.1 */ |
---|
437 | size = n+1; /* precisely what is needed */ |
---|
438 | else /* glibc 2.0 */ |
---|
439 | size *= 2; /* twice the old size */ |
---|
440 | if ((p = owl_realloc (p, size)) == NULL) |
---|
441 | return NULL; |
---|
442 | } |
---|
443 | } |
---|
444 | |
---|
445 | /* Strip a local realm fron the zephyr user name. |
---|
446 | * The caller must free the return |
---|
447 | */ |
---|
448 | char *short_zuser(char *in) |
---|
449 | { |
---|
450 | char *out, *ptr; |
---|
451 | |
---|
452 | out=owl_strdup(in); |
---|
453 | ptr=strchr(out, '@'); |
---|
454 | if (ptr) { |
---|
455 | if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) { |
---|
456 | *ptr='\0'; |
---|
457 | } |
---|
458 | } |
---|
459 | return(out); |
---|
460 | } |
---|
461 | |
---|
462 | /* Append a local realm to the zephyr user name if necessary. |
---|
463 | * The caller must free the return. |
---|
464 | */ |
---|
465 | char *long_zuser(char *in) |
---|
466 | { |
---|
467 | char *ptr; |
---|
468 | |
---|
469 | if (NULL != (ptr=strchr(in, '@'))) { |
---|
470 | return owl_strdup(in); |
---|
471 | } else { |
---|
472 | return owl_sprintf("%s@%s", in, owl_zephyr_get_realm()); |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | |
---|
477 | /* strip out the instance from a zsender's principal. Preserves the |
---|
478 | * realm if present. daemon.webzephyr is a special case. The |
---|
479 | * caller must free the return |
---|
480 | */ |
---|
481 | char *owl_util_smartstripped_user(char *in) |
---|
482 | { |
---|
483 | char *ptr, *realm, *out; |
---|
484 | |
---|
485 | out=owl_strdup(in); |
---|
486 | |
---|
487 | /* bail immeaditly if we don't have to do any work */ |
---|
488 | ptr=strchr(in, '.'); |
---|
489 | if (!strchr(in, '/') && !ptr) { |
---|
490 | /* no '/' and no '.' */ |
---|
491 | return(out); |
---|
492 | } |
---|
493 | if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) { |
---|
494 | /* There's a '.' but it's in the realm */ |
---|
495 | return(out); |
---|
496 | } |
---|
497 | if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) { |
---|
498 | return(out); |
---|
499 | } |
---|
500 | |
---|
501 | /* remove the realm from ptr, but hold on to it */ |
---|
502 | realm=strchr(out, '@'); |
---|
503 | if (realm) realm[0]='\0'; |
---|
504 | |
---|
505 | /* strip */ |
---|
506 | ptr=strchr(out, '.'); |
---|
507 | if (!ptr) ptr=strchr(out, '/'); |
---|
508 | ptr[0]='\0'; |
---|
509 | |
---|
510 | /* reattach the realm if we had one */ |
---|
511 | if (realm) { |
---|
512 | strcat(out, "@"); |
---|
513 | strcat(out, realm+1); |
---|
514 | } |
---|
515 | |
---|
516 | return(out); |
---|
517 | } |
---|
518 | |
---|
519 | char *owl_getquoting(char *line) |
---|
520 | { |
---|
521 | if (line[0]=='\0') return("'"); |
---|
522 | if (strchr(line, '\'')) return("\""); |
---|
523 | if (strchr(line, '"')) return("'"); |
---|
524 | if (strchr(line, ' ')) return("'"); |
---|
525 | return(""); |
---|
526 | } |
---|
527 | |
---|
528 | |
---|
529 | |
---|
530 | /* Return a string with any occurances of 'from' replaced with 'to'. |
---|
531 | * Does not currently handle backslash quoting, but may in the future. |
---|
532 | * Caller must free returned string. |
---|
533 | */ |
---|
534 | char *owl_util_substitute(char *in, char *from, char *to) |
---|
535 | { |
---|
536 | |
---|
537 | char *out; |
---|
538 | int outlen, tolen, fromlen, inpos=0, outpos=0; |
---|
539 | |
---|
540 | if (!*from) return owl_strdup(in); |
---|
541 | |
---|
542 | outlen = strlen(in)+1; |
---|
543 | tolen = strlen(to); |
---|
544 | fromlen = strlen(from); |
---|
545 | out = malloc(outlen); |
---|
546 | |
---|
547 | while (in[inpos]) { |
---|
548 | if (!strncmp(in+inpos, from, fromlen)) { |
---|
549 | outlen += tolen; |
---|
550 | out = owl_realloc(out, outlen); |
---|
551 | strcpy(out+outpos, to); |
---|
552 | inpos += fromlen; |
---|
553 | outpos += tolen; |
---|
554 | } else { |
---|
555 | out[outpos] = in[inpos]; |
---|
556 | inpos++; outpos++; |
---|
557 | } |
---|
558 | } |
---|
559 | out[outpos] = '\0'; |
---|
560 | return(out); |
---|
561 | } |
---|
562 | |
---|
563 | /* replace all instances of character a in buff with the character |
---|
564 | * b. buff must be null terminated. |
---|
565 | */ |
---|
566 | void owl_util_tr(char *buff, char a, char b) |
---|
567 | { |
---|
568 | int i; |
---|
569 | |
---|
570 | owl_function_debugmsg("In: %s", buff); |
---|
571 | for (i=0; buff[i]!='\0'; i++) { |
---|
572 | if (buff[i]==a) buff[i]=b; |
---|
573 | } |
---|
574 | owl_function_debugmsg("Out: %s", buff); |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | /* Return the owl color associated with the named color */ |
---|
579 | int owl_util_string_to_color(char *color) |
---|
580 | { |
---|
581 | if (!strcasecmp(color, "black")) { |
---|
582 | return(OWL_COLOR_BLACK); |
---|
583 | } else if (!strcasecmp(color, "red")) { |
---|
584 | return(OWL_COLOR_RED); |
---|
585 | } else if (!strcasecmp(color, "green")) { |
---|
586 | return(OWL_COLOR_GREEN); |
---|
587 | } else if (!strcasecmp(color, "yellow")) { |
---|
588 | return(OWL_COLOR_YELLOW); |
---|
589 | } else if (!strcasecmp(color, "blue")) { |
---|
590 | return(OWL_COLOR_BLUE); |
---|
591 | } else if (!strcasecmp(color, "magenta")) { |
---|
592 | return(OWL_COLOR_MAGENTA); |
---|
593 | } else if (!strcasecmp(color, "cyan")) { |
---|
594 | return(OWL_COLOR_CYAN); |
---|
595 | } else if (!strcasecmp(color, "white")) { |
---|
596 | return(OWL_COLOR_WHITE); |
---|
597 | } else if (!strcasecmp(color, "default")) { |
---|
598 | return(OWL_COLOR_DEFAULT); |
---|
599 | } |
---|
600 | return(OWL_COLOR_DEFAULT); |
---|
601 | } |
---|
602 | |
---|
603 | /* Return a string name of the given owl color */ |
---|
604 | char *owl_util_color_to_string(int color) |
---|
605 | { |
---|
606 | if (color==OWL_COLOR_BLACK) return("black"); |
---|
607 | if (color==OWL_COLOR_RED) return("red"); |
---|
608 | if (color==OWL_COLOR_GREEN) return("green"); |
---|
609 | if (color==OWL_COLOR_YELLOW) return("yellow"); |
---|
610 | if (color==OWL_COLOR_BLUE) return("blue"); |
---|
611 | if (color==OWL_COLOR_MAGENTA) return("magenta"); |
---|
612 | if (color==OWL_COLOR_CYAN) return("cyan"); |
---|
613 | if (color==OWL_COLOR_WHITE) return("white"); |
---|
614 | if (color==OWL_COLOR_DEFAULT) return("default"); |
---|
615 | return("Unknown color"); |
---|
616 | } |
---|
617 | |
---|
618 | /* Get the default tty name. Caller must free the return */ |
---|
619 | char *owl_util_get_default_tty() |
---|
620 | { |
---|
621 | char *out, *tmp; |
---|
622 | |
---|
623 | if (getenv("DISPLAY")) { |
---|
624 | out=owl_strdup(getenv("DISPLAY")); |
---|
625 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
---|
626 | out=owl_strdup(tmp); |
---|
627 | if (!strncmp(out, "/dev/", 5)) { |
---|
628 | owl_free(out); |
---|
629 | out=owl_strdup(tmp+5); |
---|
630 | } |
---|
631 | } else { |
---|
632 | out=owl_strdup("unknown"); |
---|
633 | } |
---|
634 | return(out); |
---|
635 | } |
---|
636 | |
---|
637 | |
---|
638 | /* Animation hack */ |
---|
639 | void owl_hack_animate() |
---|
640 | { |
---|
641 | owl_messagelist *ml; |
---|
642 | owl_message *m; |
---|
643 | owl_fmtext *fm; |
---|
644 | char *text, *ptr; |
---|
645 | int place; |
---|
646 | |
---|
647 | /* grab the first message and make sure its id is 0 */ |
---|
648 | ml=owl_global_get_msglist(&g); |
---|
649 | m=owl_messagelist_get_element(ml, 0); |
---|
650 | if (!m) return; |
---|
651 | if (owl_message_get_id(m)!=0) return; |
---|
652 | |
---|
653 | fm=owl_message_get_fmtext(m); |
---|
654 | text=owl_fmtext_get_text(fm); |
---|
655 | |
---|
656 | ptr=strstr(text, "OvO"); |
---|
657 | if (ptr) { |
---|
658 | place=ptr-text; |
---|
659 | owl_fmtext_set_char(fm, place, '-'); |
---|
660 | owl_fmtext_set_char(fm, place+2, '-'); |
---|
661 | |
---|
662 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
663 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
664 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
665 | /* TODO: this is a broken kludge */ |
---|
666 | if (owl_global_get_viewwin(&g)) { |
---|
667 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
668 | } |
---|
669 | } |
---|
670 | owl_global_set_needrefresh(&g); |
---|
671 | return; |
---|
672 | } |
---|
673 | |
---|
674 | ptr=strstr(text, "-v-"); |
---|
675 | if (ptr) { |
---|
676 | place=ptr-text; |
---|
677 | owl_fmtext_set_char(fm, place, 'O'); |
---|
678 | owl_fmtext_set_char(fm, place+2, 'O'); |
---|
679 | |
---|
680 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
681 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
682 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
683 | /* TODO: this is a broken kludge */ |
---|
684 | if (owl_global_get_viewwin(&g)) { |
---|
685 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
686 | } |
---|
687 | } |
---|
688 | owl_global_set_needrefresh(&g); |
---|
689 | return; |
---|
690 | } |
---|
691 | } |
---|
692 | |
---|
693 | /* strip leading and trailing new lines. Caller must free the |
---|
694 | * return. |
---|
695 | */ |
---|
696 | char *owl_util_stripnewlines(char *in) |
---|
697 | { |
---|
698 | |
---|
699 | char *tmp, *ptr1, *ptr2, *out; |
---|
700 | |
---|
701 | ptr1=tmp=owl_strdup(in); |
---|
702 | while (ptr1[0]=='\n') { |
---|
703 | ptr1++; |
---|
704 | } |
---|
705 | ptr2=ptr1+strlen(ptr1)-1; |
---|
706 | while (ptr2[0]=='\n' && ptr2>ptr1) { |
---|
707 | ptr2[0]='\0'; |
---|
708 | ptr2--; |
---|
709 | } |
---|
710 | |
---|
711 | out=owl_strdup(ptr1); |
---|
712 | owl_free(tmp); |
---|
713 | return(out); |
---|
714 | } |
---|
715 | |
---|
716 | /* Delete the line matching "line" from the named file. If no such |
---|
717 | * line is found the file is left intact. If backup==1 then create a |
---|
718 | * backupfile containing the original contents. This is an |
---|
719 | * inefficient impelementation which reads the entire file into |
---|
720 | * memory. |
---|
721 | */ |
---|
722 | void owl_util_file_deleteline(char *filename, char *line, int backup) |
---|
723 | { |
---|
724 | char buff[LINE], *text; |
---|
725 | char *backupfilename=""; |
---|
726 | FILE *file, *backupfile=NULL; |
---|
727 | int size, newline; |
---|
728 | |
---|
729 | /* open the file for reading */ |
---|
730 | file=fopen(filename, "r"); |
---|
731 | if (!file) { |
---|
732 | owl_function_error("Error opening file %s", filename); |
---|
733 | return; |
---|
734 | } |
---|
735 | |
---|
736 | /* open the backup file for writing */ |
---|
737 | if (backup) { |
---|
738 | backupfilename=owl_sprintf("%s.backup", filename); |
---|
739 | backupfile=fopen(backupfilename, "w"); |
---|
740 | if (!backupfile) { |
---|
741 | owl_function_error("Error opening file %s for writing", backupfilename); |
---|
742 | owl_free(backupfilename); |
---|
743 | return; |
---|
744 | } |
---|
745 | owl_free(backupfilename); |
---|
746 | } |
---|
747 | |
---|
748 | /* we'll read the entire file into memory, minus the line we don't want and |
---|
749 | * and at the same time create the backup file if necessary |
---|
750 | */ |
---|
751 | text=owl_malloc(LINE); |
---|
752 | strcpy(text, ""); |
---|
753 | size=LINE; |
---|
754 | while (fgets(buff, LINE, file)!=NULL) { |
---|
755 | /* strip the newline */ |
---|
756 | newline=0; |
---|
757 | if (buff[strlen(buff)-1]=='\n') { |
---|
758 | buff[strlen(buff)-1]='\0'; |
---|
759 | newline=1; |
---|
760 | } |
---|
761 | |
---|
762 | /* if we don't match the line, add to saved text in memory */ |
---|
763 | if (strcasecmp(buff, line)) { |
---|
764 | size+=LINE; |
---|
765 | text=owl_realloc(text, size); |
---|
766 | strcat(text, buff); |
---|
767 | if (newline) strcat(text, "\n"); |
---|
768 | } |
---|
769 | |
---|
770 | /* write to backupfile if necessary */ |
---|
771 | if (backup) { |
---|
772 | fputs(buff, backupfile); |
---|
773 | if (newline) fputs("\n", backupfile); |
---|
774 | } |
---|
775 | } |
---|
776 | if (backup) fclose(backupfile); |
---|
777 | fclose(file); |
---|
778 | |
---|
779 | /* now rewrite the original file from memory */ |
---|
780 | file=fopen(filename, "w"); |
---|
781 | if (!file) { |
---|
782 | owl_function_error("WARNING: Error opening %s for writing. Use %s to restore.", filename, backupfilename); |
---|
783 | owl_function_beep(); |
---|
784 | owl_free(line); |
---|
785 | return; |
---|
786 | } |
---|
787 | |
---|
788 | fputs(text, file); |
---|
789 | fclose(file); |
---|
790 | } |
---|
791 | |
---|
792 | /**************************************************************************/ |
---|
793 | /************************* REGRESSION TESTS *******************************/ |
---|
794 | /**************************************************************************/ |
---|
795 | |
---|
796 | #ifdef OWL_INCLUDE_REG_TESTS |
---|
797 | |
---|
798 | #define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc) |
---|
799 | |
---|
800 | int owl_util_regtest(void) |
---|
801 | { |
---|
802 | int numfailed=0; |
---|
803 | |
---|
804 | printf("BEGIN testing owl_util\n"); |
---|
805 | |
---|
806 | FAIL_UNLESS("owl_util_substitute 1", |
---|
807 | !strcmp("foo", owl_util_substitute("foo", "", "Y"))); |
---|
808 | FAIL_UNLESS("owl_util_substitute 2", |
---|
809 | !strcmp("fYZYZ", owl_util_substitute("foo", "o", "YZ"))); |
---|
810 | FAIL_UNLESS("owl_util_substitute 3", |
---|
811 | !strcmp("foo", owl_util_substitute("fYZYZ", "YZ", "o"))); |
---|
812 | FAIL_UNLESS("owl_util_substitute 4", |
---|
813 | !strcmp("/u/foo/meep", owl_util_substitute("~/meep", "~", "/u/foo"))); |
---|
814 | |
---|
815 | FAIL_UNLESS("skiptokens 1", |
---|
816 | !strcmp("bar quux", skiptokens("foo bar quux", 1))); |
---|
817 | FAIL_UNLESS("skiptokens 2", |
---|
818 | !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); |
---|
819 | |
---|
820 | FAIL_UNLESS("owl_util_uniq 1", |
---|
821 | !strcmp("foo bar x", owl_util_uniq("foo", "bar x", "-"))); |
---|
822 | FAIL_UNLESS("owl_util_uniq 2", |
---|
823 | !strcmp("foo bar x", owl_util_uniq("foo", "bar -y x", "-"))); |
---|
824 | FAIL_UNLESS("owl_util_uniq 3", |
---|
825 | !strcmp("meep foo bar", owl_util_uniq("meep foo", "bar foo meep", "-"))); |
---|
826 | |
---|
827 | if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); |
---|
828 | printf("END testing owl_util (%d failures)\n", numfailed); |
---|
829 | return(numfailed); |
---|
830 | } |
---|
831 | |
---|
832 | #endif /* OWL_INCLUDE_REG_TESTS */ |
---|