[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
[5145235] | 4 | #include <unistd.h> |
---|
[7d4fbcd] | 5 | #include <ctype.h> |
---|
[f36222f] | 6 | #include <pwd.h> |
---|
[7d4fbcd] | 7 | |
---|
[1aee7d9] | 8 | static const char fileIdent[] = "$Id$"; |
---|
| 9 | |
---|
[e4eebe8] | 10 | void sepbar(char *in) |
---|
| 11 | { |
---|
[7d4fbcd] | 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); |
---|
[c15bbfb] | 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 | } |
---|
[7d4fbcd] | 30 | |
---|
[73624b4] | 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 | |
---|
[7d4fbcd] | 37 | wmove(sepwin, 0, 2); |
---|
| 38 | |
---|
| 39 | if (owl_messagelist_get_size(ml)==0) { |
---|
| 40 | strcpy(buff, " (-/-) "); |
---|
| 41 | } else { |
---|
[1c6c4d3] | 42 | snprintf(buff, 1024, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1, |
---|
[7d4fbcd] | 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); |
---|
[1c6c4d3] | 76 | snprintf(buff, 1024, " right: %i ", owl_global_get_rightshift(&g)); |
---|
[7d4fbcd] | 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 | |
---|
[e4eebe8] | 124 | void pophandler_quit(int ch) |
---|
| 125 | { |
---|
[7d4fbcd] | 126 | if (ch=='q') { |
---|
| 127 | owl_popwin_close(owl_global_get_popwin(&g)); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
[e4eebe8] | 131 | char **atokenize(char *buffer, char *sep, int *i) |
---|
| 132 | { |
---|
[7d4fbcd] | 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) { |
---|
[42abb10] | 164 | /* skips n tokens and returns where that would be. |
---|
| 165 | * TODO: handle quotes more sanely. */ |
---|
| 166 | |
---|
[7d4fbcd] | 167 | int inquotes=0; |
---|
| 168 | while (*buff && n>0) { |
---|
| 169 | while (*buff == ' ') buff++; |
---|
| 170 | while (*buff && (inquotes || *buff != ' ')) { |
---|
[e1c4636] | 171 | if (*buff == '"' || *buff == '\'') inquotes=!inquotes; |
---|
[7d4fbcd] | 172 | buff++; |
---|
| 173 | } |
---|
| 174 | while (*buff == ' ') buff++; |
---|
| 175 | n--; |
---|
| 176 | } |
---|
| 177 | return buff; |
---|
| 178 | } |
---|
| 179 | |
---|
[f36222f] | 180 | /* Return a "nice" version of the path. Tilde expansion is done, and |
---|
| 181 | * duplicate slashes are removed. Caller must free the return. |
---|
| 182 | */ |
---|
| 183 | char *owl_util_makepath(char *in) |
---|
| 184 | { |
---|
| 185 | int i, j, x; |
---|
| 186 | char *out, user[MAXPATHLEN]; |
---|
| 187 | struct passwd *pw; |
---|
| 188 | |
---|
| 189 | out=owl_malloc(MAXPATHLEN+1); |
---|
| 190 | out[0]='\0'; |
---|
| 191 | j=strlen(in); |
---|
| 192 | x=0; |
---|
| 193 | for (i=0; i<j; i++) { |
---|
| 194 | if (in[i]=='~') { |
---|
| 195 | if ( (i==(j-1)) || /* last character */ |
---|
| 196 | (in[i+1]=='/') ) { /* ~/ */ |
---|
| 197 | /* use my homedir */ |
---|
| 198 | pw=getpwuid(getuid()); |
---|
| 199 | if (!pw) { |
---|
| 200 | out[x]=in[i]; |
---|
| 201 | } else { |
---|
| 202 | out[x]='\0'; |
---|
| 203 | strcat(out, pw->pw_dir); |
---|
| 204 | x+=strlen(pw->pw_dir); |
---|
| 205 | } |
---|
| 206 | } else { |
---|
| 207 | /* another user homedir */ |
---|
| 208 | int a, b; |
---|
| 209 | b=0; |
---|
| 210 | for (a=i+1; i<j; a++) { |
---|
| 211 | if (in[a]==' ' || in[a]=='/') { |
---|
| 212 | break; |
---|
| 213 | } else { |
---|
| 214 | user[b]=in[a]; |
---|
| 215 | i++; |
---|
| 216 | b++; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | user[b]='\0'; |
---|
| 220 | pw=getpwnam(user); |
---|
| 221 | if (!pw) { |
---|
[8766d44] | 222 | out[x]=in[i]; |
---|
[f36222f] | 223 | } else { |
---|
| 224 | out[x]='\0'; |
---|
| 225 | strcat(out, pw->pw_dir); |
---|
| 226 | x+=strlen(pw->pw_dir); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | } else if (in[i]=='/') { |
---|
| 230 | /* check for a double / */ |
---|
| 231 | if (i<(j-1) && (in[i+1]=='/')) { |
---|
| 232 | /* do nothing */ |
---|
| 233 | } else { |
---|
| 234 | out[x]=in[i]; |
---|
| 235 | x++; |
---|
| 236 | } |
---|
| 237 | } else { |
---|
| 238 | out[x]=in[i]; |
---|
| 239 | x++; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | out[x]='\0'; |
---|
| 243 | return(out); |
---|
| 244 | } |
---|
| 245 | |
---|
[e4eebe8] | 246 | void atokenize_free(char **tok, int nels) |
---|
| 247 | { |
---|
[7d4fbcd] | 248 | int i; |
---|
| 249 | for (i=0; i<nels; i++) { |
---|
| 250 | owl_free(tok[i]); |
---|
| 251 | } |
---|
| 252 | owl_free(tok); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | |
---|
[e4eebe8] | 256 | void owl_parsefree(char **argv, int argc) |
---|
| 257 | { |
---|
[7d4fbcd] | 258 | int i; |
---|
| 259 | |
---|
| 260 | if (!argv) return; |
---|
| 261 | |
---|
| 262 | for (i=0; i<argc; i++) { |
---|
| 263 | if (argv[i]) owl_free(argv[i]); |
---|
| 264 | } |
---|
| 265 | owl_free(argv); |
---|
| 266 | } |
---|
| 267 | |
---|
[e4eebe8] | 268 | char **owl_parseline(char *line, int *argc) |
---|
| 269 | { |
---|
[7d4fbcd] | 270 | /* break a command line up into argv, argc. The caller must free |
---|
| 271 | the returned values. If there is an error argc will be set to |
---|
| 272 | -1, argv will be NULL and the caller does not need to free |
---|
| 273 | anything */ |
---|
| 274 | |
---|
| 275 | char **argv; |
---|
| 276 | int i, len, between=1; |
---|
| 277 | char *curarg; |
---|
| 278 | char quote; |
---|
| 279 | |
---|
| 280 | argv=owl_malloc(sizeof(char *)); |
---|
| 281 | len=strlen(line); |
---|
| 282 | curarg=owl_malloc(len+10); |
---|
| 283 | strcpy(curarg, ""); |
---|
| 284 | quote='\0'; |
---|
| 285 | *argc=0; |
---|
| 286 | for (i=0; i<len+1; i++) { |
---|
| 287 | /* find the first real character */ |
---|
| 288 | if (between) { |
---|
| 289 | if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') { |
---|
| 290 | continue; |
---|
| 291 | } else { |
---|
| 292 | between=0; |
---|
| 293 | i--; |
---|
| 294 | continue; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | /* deal with a quote character */ |
---|
| 299 | if (line[i]=='"' || line[i]=="'"[0]) { |
---|
| 300 | /* if this type of quote is open, close it */ |
---|
| 301 | if (quote==line[i]) { |
---|
| 302 | quote='\0'; |
---|
| 303 | continue; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /* if no quoting is open then open with this */ |
---|
| 307 | if (quote=='\0') { |
---|
| 308 | quote=line[i]; |
---|
| 309 | continue; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /* if another type of quote is open then treat this as a literal */ |
---|
| 313 | curarg[strlen(curarg)+1]='\0'; |
---|
| 314 | curarg[strlen(curarg)]=line[i]; |
---|
| 315 | continue; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /* if it's not a space or end of command, then use it */ |
---|
| 319 | if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') { |
---|
| 320 | curarg[strlen(curarg)+1]='\0'; |
---|
| 321 | curarg[strlen(curarg)]=line[i]; |
---|
| 322 | continue; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /* otherwise, if we're not in quotes, add the whole argument */ |
---|
| 326 | if (quote=='\0') { |
---|
| 327 | /* add the argument */ |
---|
| 328 | argv=owl_realloc(argv, sizeof(char *)*((*argc)+1)); |
---|
| 329 | argv[*argc]=owl_malloc(strlen(curarg)+2); |
---|
| 330 | strcpy(argv[*argc], curarg); |
---|
| 331 | *argc=*argc+1; |
---|
| 332 | strcpy(curarg, ""); |
---|
| 333 | between=1; |
---|
| 334 | continue; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | /* if it is a space and we're in quotes, then use it */ |
---|
| 338 | curarg[strlen(curarg)+1]='\0'; |
---|
| 339 | curarg[strlen(curarg)]=line[i]; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /* check for unbalanced quotes */ |
---|
| 343 | if (quote!='\0') { |
---|
| 344 | owl_parsefree(argv, *argc); |
---|
| 345 | *argc=-1; |
---|
| 346 | return(NULL); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | return(argv); |
---|
| 350 | } |
---|
| 351 | |
---|
[de03334] | 352 | /* caller must free the return */ |
---|
[5b85d19] | 353 | char *owl_util_minutes_to_timestr(int in) |
---|
[de03334] | 354 | { |
---|
[f1e629d] | 355 | int days, hours; |
---|
[de03334] | 356 | long run; |
---|
| 357 | char *out; |
---|
| 358 | |
---|
[5b85d19] | 359 | run=in; |
---|
[de03334] | 360 | |
---|
[5b85d19] | 361 | days=run/1440; |
---|
| 362 | run-=days*1440; |
---|
| 363 | hours=run/60; |
---|
| 364 | run-=hours*60; |
---|
[de03334] | 365 | |
---|
| 366 | if (days>0) { |
---|
[5b85d19] | 367 | out=owl_sprintf("%i d %2.2i:%2.2i", days, hours, run); |
---|
[de03334] | 368 | } else { |
---|
[3ba3af0] | 369 | out=owl_sprintf(" %2.2i:%2.2i", hours, run); |
---|
[de03334] | 370 | } |
---|
| 371 | return(out); |
---|
| 372 | } |
---|
| 373 | |
---|
[e4eebe8] | 374 | /* return the index of the last char before a change from the first one */ |
---|
| 375 | int owl_util_find_trans(char *in, int len) |
---|
| 376 | { |
---|
[7d4fbcd] | 377 | int i; |
---|
| 378 | for (i=1; i<len; i++) { |
---|
| 379 | if (in[i] != in[0]) return(i-1); |
---|
| 380 | } |
---|
| 381 | return(i); |
---|
| 382 | } |
---|
| 383 | |
---|
[e4eebe8] | 384 | /* downcase the string 'foo' */ |
---|
| 385 | void downstr(char *foo) |
---|
| 386 | { |
---|
[7d4fbcd] | 387 | int i; |
---|
| 388 | for (i=0; foo[i]!='\0'; i++) { |
---|
| 389 | foo[i]=tolower(foo[i]); |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | |
---|
[e4eebe8] | 393 | /* Caller must free response. |
---|
| 394 | * Takes in strings which are space-separated lists of tokens |
---|
| 395 | * and returns a single string containing no token more than once. |
---|
| 396 | * If prohibit is non-null, no token may start with a character |
---|
| 397 | * in prohibit. |
---|
| 398 | */ |
---|
| 399 | char *owl_util_uniq(char *A, char *B, char *prohibit) |
---|
| 400 | { |
---|
[42abb10] | 401 | |
---|
[e50cd56] | 402 | char *cat, **tok; |
---|
| 403 | int toklen, i, j, first=1; |
---|
| 404 | cat = owl_malloc(strlen(A)+strlen(B)+3); |
---|
| 405 | strcpy(cat, A); |
---|
| 406 | strcat(cat, " "); |
---|
| 407 | strcat(cat, B); |
---|
| 408 | tok = atokenize(cat, " ", &toklen); |
---|
| 409 | strcpy(cat, ""); |
---|
| 410 | for (i=0; i<toklen; i++) { |
---|
| 411 | int dup=0; |
---|
| 412 | for (j=0; j<i; j++) { |
---|
| 413 | if (!strcmp(tok[i], tok[j])) dup=1; |
---|
| 414 | } |
---|
| 415 | if (!dup && (!prohibit || !strchr(prohibit, tok[i][0]))) { |
---|
| 416 | if (!first) { |
---|
| 417 | strcat(cat, " "); |
---|
| 418 | } |
---|
| 419 | first=0; |
---|
| 420 | strcat(cat, tok[i]); |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | atokenize_free(tok, toklen); |
---|
| 424 | return(cat); |
---|
| 425 | } |
---|
| 426 | |
---|
[42abb10] | 427 | /* hooks for doing memory allocation et. al. in owl */ |
---|
| 428 | |
---|
[e4eebe8] | 429 | void *owl_malloc(size_t size) |
---|
| 430 | { |
---|
[7d4fbcd] | 431 | return(malloc(size)); |
---|
| 432 | } |
---|
| 433 | |
---|
[e4eebe8] | 434 | void owl_free(void *ptr) |
---|
| 435 | { |
---|
[7d4fbcd] | 436 | free(ptr); |
---|
| 437 | } |
---|
| 438 | |
---|
[e4eebe8] | 439 | char *owl_strdup(const char *s1) |
---|
| 440 | { |
---|
[7d4fbcd] | 441 | return(strdup(s1)); |
---|
| 442 | } |
---|
| 443 | |
---|
[e4eebe8] | 444 | void *owl_realloc(void *ptr, size_t size) |
---|
| 445 | { |
---|
[7d4fbcd] | 446 | return(realloc(ptr, size)); |
---|
| 447 | } |
---|
| 448 | |
---|
[e4eebe8] | 449 | /* allocates memory and returns the string or null. |
---|
| 450 | * caller must free the string. |
---|
| 451 | * from Linux sprintf man page. |
---|
| 452 | */ |
---|
| 453 | char *owl_sprintf(const char *fmt, ...) |
---|
| 454 | { |
---|
[1c6c4d3] | 455 | int n, size = 100; |
---|
| 456 | char *p; |
---|
| 457 | va_list ap; |
---|
[42abb10] | 458 | if ((p = owl_malloc (size)) == NULL) return (NULL); |
---|
[1c6c4d3] | 459 | while (1) { |
---|
| 460 | /* Try to print in the allocated space. */ |
---|
| 461 | va_start(ap, fmt); |
---|
| 462 | n = vsnprintf (p, size, fmt, ap); |
---|
| 463 | va_end(ap); |
---|
| 464 | /* If that worked, return the string. */ |
---|
| 465 | if (n > -1 && n < size) |
---|
| 466 | return p; |
---|
| 467 | /* Else try again with more space. */ |
---|
| 468 | if (n > -1) /* glibc 2.1 */ |
---|
| 469 | size = n+1; /* precisely what is needed */ |
---|
| 470 | else /* glibc 2.0 */ |
---|
| 471 | size *= 2; /* twice the old size */ |
---|
| 472 | if ((p = owl_realloc (p, size)) == NULL) |
---|
| 473 | return NULL; |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
[12c35df] | 477 | /* Return the owl color associated with the named color. Return -1 |
---|
| 478 | * if the named color is not available |
---|
| 479 | */ |
---|
[e4eebe8] | 480 | int owl_util_string_to_color(char *color) |
---|
| 481 | { |
---|
[7d4fbcd] | 482 | if (!strcasecmp(color, "black")) { |
---|
| 483 | return(OWL_COLOR_BLACK); |
---|
| 484 | } else if (!strcasecmp(color, "red")) { |
---|
| 485 | return(OWL_COLOR_RED); |
---|
| 486 | } else if (!strcasecmp(color, "green")) { |
---|
| 487 | return(OWL_COLOR_GREEN); |
---|
| 488 | } else if (!strcasecmp(color, "yellow")) { |
---|
| 489 | return(OWL_COLOR_YELLOW); |
---|
| 490 | } else if (!strcasecmp(color, "blue")) { |
---|
| 491 | return(OWL_COLOR_BLUE); |
---|
| 492 | } else if (!strcasecmp(color, "magenta")) { |
---|
| 493 | return(OWL_COLOR_MAGENTA); |
---|
| 494 | } else if (!strcasecmp(color, "cyan")) { |
---|
| 495 | return(OWL_COLOR_CYAN); |
---|
| 496 | } else if (!strcasecmp(color, "white")) { |
---|
| 497 | return(OWL_COLOR_WHITE); |
---|
| 498 | } else if (!strcasecmp(color, "default")) { |
---|
| 499 | return(OWL_COLOR_DEFAULT); |
---|
| 500 | } |
---|
[12c35df] | 501 | return(-1); |
---|
[7d4fbcd] | 502 | } |
---|
| 503 | |
---|
[e4eebe8] | 504 | /* Return a string name of the given owl color */ |
---|
| 505 | char *owl_util_color_to_string(int color) |
---|
| 506 | { |
---|
[7d4fbcd] | 507 | if (color==OWL_COLOR_BLACK) return("black"); |
---|
| 508 | if (color==OWL_COLOR_RED) return("red"); |
---|
| 509 | if (color==OWL_COLOR_GREEN) return("green"); |
---|
| 510 | if (color==OWL_COLOR_YELLOW) return("yellow"); |
---|
| 511 | if (color==OWL_COLOR_BLUE) return("blue"); |
---|
| 512 | if (color==OWL_COLOR_MAGENTA) return("magenta"); |
---|
| 513 | if (color==OWL_COLOR_CYAN) return("cyan"); |
---|
| 514 | if (color==OWL_COLOR_WHITE) return("white"); |
---|
| 515 | if (color==OWL_COLOR_DEFAULT) return("default"); |
---|
| 516 | return("Unknown color"); |
---|
| 517 | } |
---|
[e1c4636] | 518 | |
---|
[e4eebe8] | 519 | /* Get the default tty name. Caller must free the return */ |
---|
| 520 | char *owl_util_get_default_tty() |
---|
| 521 | { |
---|
[5145235] | 522 | char *out, *tmp; |
---|
[61e79a9] | 523 | |
---|
| 524 | if (getenv("DISPLAY")) { |
---|
| 525 | out=owl_strdup(getenv("DISPLAY")); |
---|
[5145235] | 526 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
---|
| 527 | out=owl_strdup(tmp); |
---|
[61e79a9] | 528 | if (!strncmp(out, "/dev/", 5)) { |
---|
| 529 | owl_free(out); |
---|
[5145235] | 530 | out=owl_strdup(tmp+5); |
---|
[61e79a9] | 531 | } |
---|
| 532 | } else { |
---|
[5145235] | 533 | out=owl_strdup("unknown"); |
---|
[61e79a9] | 534 | } |
---|
| 535 | return(out); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | |
---|
[e4eebe8] | 539 | /* Animation hack */ |
---|
| 540 | void owl_hack_animate() |
---|
| 541 | { |
---|
[4b464a4] | 542 | owl_messagelist *ml; |
---|
| 543 | owl_message *m; |
---|
| 544 | owl_fmtext *fm; |
---|
| 545 | char *text, *ptr; |
---|
| 546 | int place; |
---|
| 547 | |
---|
| 548 | /* grab the first message and make sure its id is 0 */ |
---|
| 549 | ml=owl_global_get_msglist(&g); |
---|
| 550 | m=owl_messagelist_get_element(ml, 0); |
---|
| 551 | if (!m) return; |
---|
| 552 | if (owl_message_get_id(m)!=0) return; |
---|
| 553 | |
---|
| 554 | fm=owl_message_get_fmtext(m); |
---|
| 555 | text=owl_fmtext_get_text(fm); |
---|
| 556 | |
---|
| 557 | ptr=strstr(text, "OvO"); |
---|
| 558 | if (ptr) { |
---|
| 559 | place=ptr-text; |
---|
| 560 | owl_fmtext_set_char(fm, place, '-'); |
---|
| 561 | owl_fmtext_set_char(fm, place+2, '-'); |
---|
| 562 | |
---|
| 563 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
[a15a84f] | 564 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 565 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 566 | /* TODO: this is a broken kludge */ |
---|
| 567 | if (owl_global_get_viewwin(&g)) { |
---|
| 568 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 569 | } |
---|
| 570 | } |
---|
[4b464a4] | 571 | owl_global_set_needrefresh(&g); |
---|
| 572 | return; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | ptr=strstr(text, "-v-"); |
---|
| 576 | if (ptr) { |
---|
| 577 | place=ptr-text; |
---|
| 578 | owl_fmtext_set_char(fm, place, 'O'); |
---|
| 579 | owl_fmtext_set_char(fm, place+2, 'O'); |
---|
| 580 | |
---|
| 581 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
[a15a84f] | 582 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 583 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 584 | /* TODO: this is a broken kludge */ |
---|
| 585 | if (owl_global_get_viewwin(&g)) { |
---|
| 586 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 587 | } |
---|
| 588 | } |
---|
[4b464a4] | 589 | owl_global_set_needrefresh(&g); |
---|
| 590 | return; |
---|
| 591 | } |
---|
| 592 | } |
---|
[e1c4636] | 593 | |
---|
[e4eebe8] | 594 | /* strip leading and trailing new lines. Caller must free the |
---|
| 595 | * return. |
---|
| 596 | */ |
---|
| 597 | char *owl_util_stripnewlines(char *in) |
---|
| 598 | { |
---|
[7e3e00a] | 599 | |
---|
| 600 | char *tmp, *ptr1, *ptr2, *out; |
---|
| 601 | |
---|
| 602 | ptr1=tmp=owl_strdup(in); |
---|
| 603 | while (ptr1[0]=='\n') { |
---|
| 604 | ptr1++; |
---|
| 605 | } |
---|
| 606 | ptr2=ptr1+strlen(ptr1)-1; |
---|
| 607 | while (ptr2[0]=='\n' && ptr2>ptr1) { |
---|
| 608 | ptr2[0]='\0'; |
---|
| 609 | ptr2--; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | out=owl_strdup(ptr1); |
---|
| 613 | owl_free(tmp); |
---|
| 614 | return(out); |
---|
| 615 | } |
---|
| 616 | |
---|
[38cf544c] | 617 | /* Delete the line matching "line" from the named file. If no such |
---|
| 618 | * line is found the file is left intact. If backup==1 then create a |
---|
| 619 | * backupfile containing the original contents. This is an |
---|
| 620 | * inefficient impelementation which reads the entire file into |
---|
| 621 | * memory. |
---|
| 622 | */ |
---|
| 623 | void owl_util_file_deleteline(char *filename, char *line, int backup) |
---|
| 624 | { |
---|
| 625 | char buff[LINE], *text; |
---|
[e6449bc] | 626 | char *backupfilename=""; |
---|
| 627 | FILE *file, *backupfile=NULL; |
---|
[38cf544c] | 628 | int size, newline; |
---|
| 629 | |
---|
| 630 | /* open the file for reading */ |
---|
| 631 | file=fopen(filename, "r"); |
---|
| 632 | if (!file) { |
---|
[836ea3a3] | 633 | owl_function_error("Error opening file %s", filename); |
---|
[38cf544c] | 634 | return; |
---|
| 635 | } |
---|
[e6449bc] | 636 | |
---|
[38cf544c] | 637 | /* open the backup file for writing */ |
---|
| 638 | if (backup) { |
---|
| 639 | backupfilename=owl_sprintf("%s.backup", filename); |
---|
| 640 | backupfile=fopen(backupfilename, "w"); |
---|
| 641 | if (!backupfile) { |
---|
[836ea3a3] | 642 | owl_function_error("Error opening file %s for writing", backupfilename); |
---|
[378fa14] | 643 | owl_free(backupfilename); |
---|
[38cf544c] | 644 | return; |
---|
| 645 | } |
---|
[378fa14] | 646 | owl_free(backupfilename); |
---|
[38cf544c] | 647 | } |
---|
| 648 | |
---|
| 649 | /* we'll read the entire file into memory, minus the line we don't want and |
---|
| 650 | * and at the same time create the backup file if necessary |
---|
| 651 | */ |
---|
| 652 | text=owl_malloc(LINE); |
---|
| 653 | strcpy(text, ""); |
---|
| 654 | size=LINE; |
---|
| 655 | while (fgets(buff, LINE, file)!=NULL) { |
---|
| 656 | /* strip the newline */ |
---|
| 657 | newline=0; |
---|
| 658 | if (buff[strlen(buff)-1]=='\n') { |
---|
| 659 | buff[strlen(buff)-1]='\0'; |
---|
| 660 | newline=1; |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | /* if we don't match the line, add to saved text in memory */ |
---|
| 664 | if (strcasecmp(buff, line)) { |
---|
| 665 | size+=LINE; |
---|
| 666 | text=owl_realloc(text, size); |
---|
| 667 | strcat(text, buff); |
---|
| 668 | if (newline) strcat(text, "\n"); |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | /* write to backupfile if necessary */ |
---|
[6a50af2] | 672 | if (backup) { |
---|
| 673 | fputs(buff, backupfile); |
---|
| 674 | if (newline) fputs("\n", backupfile); |
---|
| 675 | } |
---|
[38cf544c] | 676 | } |
---|
[65ad073] | 677 | if (backup) fclose(backupfile); |
---|
[38cf544c] | 678 | fclose(file); |
---|
| 679 | |
---|
| 680 | /* now rewrite the original file from memory */ |
---|
| 681 | file=fopen(filename, "w"); |
---|
| 682 | if (!file) { |
---|
[836ea3a3] | 683 | owl_function_error("WARNING: Error opening %s for writing. Use %s to restore.", filename, backupfilename); |
---|
[38cf544c] | 684 | owl_function_beep(); |
---|
| 685 | owl_free(line); |
---|
| 686 | return; |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | fputs(text, file); |
---|
| 690 | fclose(file); |
---|
| 691 | } |
---|
| 692 | |
---|
[e1c4636] | 693 | /**************************************************************************/ |
---|
| 694 | /************************* REGRESSION TESTS *******************************/ |
---|
| 695 | /**************************************************************************/ |
---|
| 696 | |
---|
| 697 | #ifdef OWL_INCLUDE_REG_TESTS |
---|
| 698 | |
---|
| 699 | #define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc) |
---|
| 700 | |
---|
[e4eebe8] | 701 | int owl_util_regtest(void) |
---|
| 702 | { |
---|
[e1c4636] | 703 | int numfailed=0; |
---|
| 704 | |
---|
| 705 | printf("BEGIN testing owl_util\n"); |
---|
| 706 | |
---|
| 707 | FAIL_UNLESS("owl_util_substitute 1", |
---|
[e3d9c77] | 708 | !strcmp("foo", owl_text_substitute("foo", "", "Y"))); |
---|
| 709 | FAIL_UNLESS("owl_text_substitute 2", |
---|
| 710 | !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ"))); |
---|
| 711 | FAIL_UNLESS("owl_text_substitute 3", |
---|
| 712 | !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o"))); |
---|
| 713 | FAIL_UNLESS("owl_text_substitute 4", |
---|
| 714 | !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo"))); |
---|
[e1c4636] | 715 | |
---|
| 716 | FAIL_UNLESS("skiptokens 1", |
---|
| 717 | !strcmp("bar quux", skiptokens("foo bar quux", 1))); |
---|
| 718 | FAIL_UNLESS("skiptokens 2", |
---|
| 719 | !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); |
---|
| 720 | |
---|
[e50cd56] | 721 | FAIL_UNLESS("owl_util_uniq 1", |
---|
| 722 | !strcmp("foo bar x", owl_util_uniq("foo", "bar x", "-"))); |
---|
| 723 | FAIL_UNLESS("owl_util_uniq 2", |
---|
| 724 | !strcmp("foo bar x", owl_util_uniq("foo", "bar -y x", "-"))); |
---|
| 725 | FAIL_UNLESS("owl_util_uniq 3", |
---|
| 726 | !strcmp("meep foo bar", owl_util_uniq("meep foo", "bar foo meep", "-"))); |
---|
| 727 | |
---|
[e1c4636] | 728 | if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); |
---|
| 729 | printf("END testing owl_util (%d failures)\n", numfailed); |
---|
| 730 | return(numfailed); |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | #endif /* OWL_INCLUDE_REG_TESTS */ |
---|