[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); |
---|
[330bcec] | 49 | if (strcmp(foo, owl_global_get_view_home(&g))) wattroff(sepwin, A_REVERSE); |
---|
[7d4fbcd] | 50 | waddstr(sepwin, " "); |
---|
| 51 | waddstr(sepwin, owl_view_get_filtname(v)); |
---|
| 52 | waddstr(sepwin, " "); |
---|
[330bcec] | 53 | if (strcmp(foo, owl_global_get_view_home(&g))) wattron(sepwin, A_REVERSE); |
---|
[7d4fbcd] | 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 | |
---|
[4b660cc] | 80 | if (owl_global_is_zaway(&g) || owl_global_is_aaway(&g)) { |
---|
[7d4fbcd] | 81 | getyx(sepwin, y, x); |
---|
| 82 | wmove(sepwin, y, x+2); |
---|
| 83 | wattron(sepwin, A_BOLD); |
---|
| 84 | wattroff(sepwin, A_REVERSE); |
---|
[4b660cc] | 85 | if (owl_global_is_zaway(&g) && owl_global_is_aaway(&g)) { |
---|
| 86 | waddstr(sepwin, " AWAY "); |
---|
| 87 | } else if (owl_global_is_zaway(&g)) { |
---|
| 88 | waddstr(sepwin, " Z-AWAY "); |
---|
| 89 | } else if (owl_global_is_aaway(&g)) { |
---|
| 90 | waddstr(sepwin, " A-AWAY "); |
---|
| 91 | } |
---|
[7d4fbcd] | 92 | wattron(sepwin, A_REVERSE); |
---|
| 93 | wattroff(sepwin, A_BOLD); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if (owl_global_get_curmsg_vert_offset(&g)) { |
---|
| 97 | getyx(sepwin, y, x); |
---|
| 98 | wmove(sepwin, y, x+2); |
---|
| 99 | wattron(sepwin, A_BOLD); |
---|
| 100 | wattroff(sepwin, A_REVERSE); |
---|
| 101 | waddstr(sepwin, " SCROLL "); |
---|
| 102 | wattron(sepwin, A_REVERSE); |
---|
| 103 | wattroff(sepwin, A_BOLD); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (in) { |
---|
| 107 | getyx(sepwin, y, x); |
---|
| 108 | wmove(sepwin, y, x+2); |
---|
| 109 | waddstr(sepwin, in); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | appendtosepbar = owl_global_get_appendtosepbar(&g); |
---|
| 113 | if (appendtosepbar && *appendtosepbar) { |
---|
| 114 | getyx(sepwin, y, x); |
---|
| 115 | wmove(sepwin, y, x+2); |
---|
| 116 | waddstr(sepwin, " "); |
---|
| 117 | waddstr(sepwin, owl_global_get_appendtosepbar(&g)); |
---|
| 118 | waddstr(sepwin, " "); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | getyx(sepwin, y, x); |
---|
| 122 | wmove(sepwin, y, owl_global_get_cols(&g)-1); |
---|
| 123 | |
---|
| 124 | wattroff(sepwin, A_BOLD); |
---|
| 125 | wattroff(sepwin, A_REVERSE); |
---|
| 126 | wnoutrefresh(sepwin); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
[e4eebe8] | 130 | void pophandler_quit(int ch) |
---|
| 131 | { |
---|
[7d4fbcd] | 132 | if (ch=='q') { |
---|
| 133 | owl_popwin_close(owl_global_get_popwin(&g)); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
[e4eebe8] | 137 | char **atokenize(char *buffer, char *sep, int *i) |
---|
| 138 | { |
---|
[7d4fbcd] | 139 | /* each element of return must be freed by user */ |
---|
| 140 | char **args; |
---|
| 141 | char *workbuff, *foo; |
---|
| 142 | int done=0, first=1, count=0; |
---|
| 143 | |
---|
| 144 | workbuff=owl_malloc(strlen(buffer)+1); |
---|
| 145 | memcpy(workbuff, buffer, strlen(buffer)+1); |
---|
| 146 | |
---|
| 147 | args=NULL; |
---|
| 148 | while (!done) { |
---|
| 149 | if (first) { |
---|
| 150 | first=0; |
---|
| 151 | foo=(char *)strtok(workbuff, sep); |
---|
| 152 | } else { |
---|
| 153 | foo=(char *)strtok(NULL, sep); |
---|
| 154 | } |
---|
| 155 | if (foo==NULL) { |
---|
| 156 | done=1; |
---|
| 157 | } else { |
---|
| 158 | args=(char **)owl_realloc(args, sizeof(char *) * (count+1)); |
---|
| 159 | args[count]=owl_malloc(strlen(foo)+1); |
---|
| 160 | strcpy(args[count], foo); |
---|
| 161 | count++; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | *i=count; |
---|
| 165 | owl_free(workbuff); |
---|
| 166 | return(args); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | char *skiptokens(char *buff, int n) { |
---|
[42abb10] | 170 | /* skips n tokens and returns where that would be. |
---|
| 171 | * TODO: handle quotes more sanely. */ |
---|
| 172 | |
---|
[7d4fbcd] | 173 | int inquotes=0; |
---|
| 174 | while (*buff && n>0) { |
---|
| 175 | while (*buff == ' ') buff++; |
---|
| 176 | while (*buff && (inquotes || *buff != ' ')) { |
---|
[e1c4636] | 177 | if (*buff == '"' || *buff == '\'') inquotes=!inquotes; |
---|
[7d4fbcd] | 178 | buff++; |
---|
| 179 | } |
---|
| 180 | while (*buff == ' ') buff++; |
---|
| 181 | n--; |
---|
| 182 | } |
---|
| 183 | return buff; |
---|
| 184 | } |
---|
| 185 | |
---|
[f36222f] | 186 | /* Return a "nice" version of the path. Tilde expansion is done, and |
---|
| 187 | * duplicate slashes are removed. Caller must free the return. |
---|
| 188 | */ |
---|
| 189 | char *owl_util_makepath(char *in) |
---|
| 190 | { |
---|
| 191 | int i, j, x; |
---|
| 192 | char *out, user[MAXPATHLEN]; |
---|
| 193 | struct passwd *pw; |
---|
| 194 | |
---|
| 195 | out=owl_malloc(MAXPATHLEN+1); |
---|
| 196 | out[0]='\0'; |
---|
| 197 | j=strlen(in); |
---|
| 198 | x=0; |
---|
| 199 | for (i=0; i<j; i++) { |
---|
| 200 | if (in[i]=='~') { |
---|
| 201 | if ( (i==(j-1)) || /* last character */ |
---|
| 202 | (in[i+1]=='/') ) { /* ~/ */ |
---|
| 203 | /* use my homedir */ |
---|
| 204 | pw=getpwuid(getuid()); |
---|
| 205 | if (!pw) { |
---|
| 206 | out[x]=in[i]; |
---|
| 207 | } else { |
---|
| 208 | out[x]='\0'; |
---|
| 209 | strcat(out, pw->pw_dir); |
---|
| 210 | x+=strlen(pw->pw_dir); |
---|
| 211 | } |
---|
| 212 | } else { |
---|
| 213 | /* another user homedir */ |
---|
| 214 | int a, b; |
---|
| 215 | b=0; |
---|
| 216 | for (a=i+1; i<j; a++) { |
---|
| 217 | if (in[a]==' ' || in[a]=='/') { |
---|
| 218 | break; |
---|
| 219 | } else { |
---|
| 220 | user[b]=in[a]; |
---|
| 221 | i++; |
---|
| 222 | b++; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | user[b]='\0'; |
---|
| 226 | pw=getpwnam(user); |
---|
| 227 | if (!pw) { |
---|
[8766d44] | 228 | out[x]=in[i]; |
---|
[f36222f] | 229 | } else { |
---|
| 230 | out[x]='\0'; |
---|
| 231 | strcat(out, pw->pw_dir); |
---|
| 232 | x+=strlen(pw->pw_dir); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } else if (in[i]=='/') { |
---|
| 236 | /* check for a double / */ |
---|
| 237 | if (i<(j-1) && (in[i+1]=='/')) { |
---|
| 238 | /* do nothing */ |
---|
| 239 | } else { |
---|
| 240 | out[x]=in[i]; |
---|
| 241 | x++; |
---|
| 242 | } |
---|
| 243 | } else { |
---|
| 244 | out[x]=in[i]; |
---|
| 245 | x++; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | out[x]='\0'; |
---|
| 249 | return(out); |
---|
| 250 | } |
---|
| 251 | |
---|
[e4eebe8] | 252 | void atokenize_free(char **tok, int nels) |
---|
| 253 | { |
---|
[7d4fbcd] | 254 | int i; |
---|
| 255 | for (i=0; i<nels; i++) { |
---|
| 256 | owl_free(tok[i]); |
---|
| 257 | } |
---|
| 258 | owl_free(tok); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | |
---|
[e4eebe8] | 262 | void owl_parsefree(char **argv, int argc) |
---|
| 263 | { |
---|
[7d4fbcd] | 264 | int i; |
---|
| 265 | |
---|
| 266 | if (!argv) return; |
---|
| 267 | |
---|
| 268 | for (i=0; i<argc; i++) { |
---|
| 269 | if (argv[i]) owl_free(argv[i]); |
---|
| 270 | } |
---|
| 271 | owl_free(argv); |
---|
| 272 | } |
---|
| 273 | |
---|
[e4eebe8] | 274 | char **owl_parseline(char *line, int *argc) |
---|
| 275 | { |
---|
[7d4fbcd] | 276 | /* break a command line up into argv, argc. The caller must free |
---|
| 277 | the returned values. If there is an error argc will be set to |
---|
| 278 | -1, argv will be NULL and the caller does not need to free |
---|
| 279 | anything */ |
---|
| 280 | |
---|
| 281 | char **argv; |
---|
| 282 | int i, len, between=1; |
---|
| 283 | char *curarg; |
---|
| 284 | char quote; |
---|
| 285 | |
---|
| 286 | argv=owl_malloc(sizeof(char *)); |
---|
| 287 | len=strlen(line); |
---|
| 288 | curarg=owl_malloc(len+10); |
---|
| 289 | strcpy(curarg, ""); |
---|
| 290 | quote='\0'; |
---|
| 291 | *argc=0; |
---|
| 292 | for (i=0; i<len+1; i++) { |
---|
| 293 | /* find the first real character */ |
---|
| 294 | if (between) { |
---|
| 295 | if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') { |
---|
| 296 | continue; |
---|
| 297 | } else { |
---|
| 298 | between=0; |
---|
| 299 | i--; |
---|
| 300 | continue; |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | /* deal with a quote character */ |
---|
| 305 | if (line[i]=='"' || line[i]=="'"[0]) { |
---|
| 306 | /* if this type of quote is open, close it */ |
---|
| 307 | if (quote==line[i]) { |
---|
| 308 | quote='\0'; |
---|
| 309 | continue; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /* if no quoting is open then open with this */ |
---|
| 313 | if (quote=='\0') { |
---|
| 314 | quote=line[i]; |
---|
| 315 | continue; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /* if another type of quote is open then treat this as a literal */ |
---|
| 319 | curarg[strlen(curarg)+1]='\0'; |
---|
| 320 | curarg[strlen(curarg)]=line[i]; |
---|
| 321 | continue; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /* if it's not a space or end of command, then use it */ |
---|
| 325 | if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') { |
---|
| 326 | curarg[strlen(curarg)+1]='\0'; |
---|
| 327 | curarg[strlen(curarg)]=line[i]; |
---|
| 328 | continue; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /* otherwise, if we're not in quotes, add the whole argument */ |
---|
| 332 | if (quote=='\0') { |
---|
| 333 | /* add the argument */ |
---|
| 334 | argv=owl_realloc(argv, sizeof(char *)*((*argc)+1)); |
---|
| 335 | argv[*argc]=owl_malloc(strlen(curarg)+2); |
---|
| 336 | strcpy(argv[*argc], curarg); |
---|
| 337 | *argc=*argc+1; |
---|
| 338 | strcpy(curarg, ""); |
---|
| 339 | between=1; |
---|
| 340 | continue; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | /* if it is a space and we're in quotes, then use it */ |
---|
| 344 | curarg[strlen(curarg)+1]='\0'; |
---|
| 345 | curarg[strlen(curarg)]=line[i]; |
---|
| 346 | } |
---|
| 347 | |
---|
[d524c83] | 348 | owl_free(curarg); |
---|
[95caa16] | 349 | |
---|
[7d4fbcd] | 350 | /* check for unbalanced quotes */ |
---|
| 351 | if (quote!='\0') { |
---|
| 352 | owl_parsefree(argv, *argc); |
---|
| 353 | *argc=-1; |
---|
| 354 | return(NULL); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | return(argv); |
---|
| 358 | } |
---|
| 359 | |
---|
[de03334] | 360 | /* caller must free the return */ |
---|
[5b85d19] | 361 | char *owl_util_minutes_to_timestr(int in) |
---|
[de03334] | 362 | { |
---|
[f1e629d] | 363 | int days, hours; |
---|
[de03334] | 364 | long run; |
---|
| 365 | char *out; |
---|
| 366 | |
---|
[5b85d19] | 367 | run=in; |
---|
[de03334] | 368 | |
---|
[5b85d19] | 369 | days=run/1440; |
---|
| 370 | run-=days*1440; |
---|
| 371 | hours=run/60; |
---|
| 372 | run-=hours*60; |
---|
[de03334] | 373 | |
---|
| 374 | if (days>0) { |
---|
[5b85d19] | 375 | out=owl_sprintf("%i d %2.2i:%2.2i", days, hours, run); |
---|
[de03334] | 376 | } else { |
---|
[3ba3af0] | 377 | out=owl_sprintf(" %2.2i:%2.2i", hours, run); |
---|
[de03334] | 378 | } |
---|
| 379 | return(out); |
---|
| 380 | } |
---|
| 381 | |
---|
[e4eebe8] | 382 | /* return the index of the last char before a change from the first one */ |
---|
| 383 | int owl_util_find_trans(char *in, int len) |
---|
| 384 | { |
---|
[7d4fbcd] | 385 | int i; |
---|
| 386 | for (i=1; i<len; i++) { |
---|
| 387 | if (in[i] != in[0]) return(i-1); |
---|
| 388 | } |
---|
| 389 | return(i); |
---|
| 390 | } |
---|
| 391 | |
---|
[c2c5c77] | 392 | int owl_util_find_trans_short(short *in, int len) |
---|
| 393 | { |
---|
| 394 | int i; |
---|
| 395 | for (i=1; i<len; i++) { |
---|
| 396 | if (in[i] != in[0]) return(i-1); |
---|
| 397 | } |
---|
| 398 | return(i); |
---|
| 399 | } |
---|
| 400 | |
---|
[e4eebe8] | 401 | /* Caller must free response. |
---|
| 402 | * Takes in strings which are space-separated lists of tokens |
---|
| 403 | * and returns a single string containing no token more than once. |
---|
| 404 | * If prohibit is non-null, no token may start with a character |
---|
| 405 | * in prohibit. |
---|
| 406 | */ |
---|
| 407 | char *owl_util_uniq(char *A, char *B, char *prohibit) |
---|
| 408 | { |
---|
[42abb10] | 409 | |
---|
[e50cd56] | 410 | char *cat, **tok; |
---|
| 411 | int toklen, i, j, first=1; |
---|
| 412 | cat = owl_malloc(strlen(A)+strlen(B)+3); |
---|
| 413 | strcpy(cat, A); |
---|
| 414 | strcat(cat, " "); |
---|
| 415 | strcat(cat, B); |
---|
| 416 | tok = atokenize(cat, " ", &toklen); |
---|
| 417 | strcpy(cat, ""); |
---|
| 418 | for (i=0; i<toklen; i++) { |
---|
| 419 | int dup=0; |
---|
| 420 | for (j=0; j<i; j++) { |
---|
| 421 | if (!strcmp(tok[i], tok[j])) dup=1; |
---|
| 422 | } |
---|
| 423 | if (!dup && (!prohibit || !strchr(prohibit, tok[i][0]))) { |
---|
| 424 | if (!first) { |
---|
| 425 | strcat(cat, " "); |
---|
| 426 | } |
---|
| 427 | first=0; |
---|
| 428 | strcat(cat, tok[i]); |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | atokenize_free(tok, toklen); |
---|
| 432 | return(cat); |
---|
| 433 | } |
---|
| 434 | |
---|
[42abb10] | 435 | /* hooks for doing memory allocation et. al. in owl */ |
---|
| 436 | |
---|
[e4eebe8] | 437 | void *owl_malloc(size_t size) |
---|
| 438 | { |
---|
[34509d5] | 439 | return(g_malloc(size)); |
---|
[7d4fbcd] | 440 | } |
---|
| 441 | |
---|
[e4eebe8] | 442 | void owl_free(void *ptr) |
---|
| 443 | { |
---|
[34509d5] | 444 | g_free(ptr); |
---|
[7d4fbcd] | 445 | } |
---|
| 446 | |
---|
[e4eebe8] | 447 | char *owl_strdup(const char *s1) |
---|
| 448 | { |
---|
[34509d5] | 449 | return(g_strdup(s1)); |
---|
[7d4fbcd] | 450 | } |
---|
| 451 | |
---|
[e4eebe8] | 452 | void *owl_realloc(void *ptr, size_t size) |
---|
| 453 | { |
---|
[34509d5] | 454 | return(g_realloc(ptr, size)); |
---|
[7d4fbcd] | 455 | } |
---|
| 456 | |
---|
[e4eebe8] | 457 | /* allocates memory and returns the string or null. |
---|
| 458 | * caller must free the string. |
---|
| 459 | */ |
---|
| 460 | char *owl_sprintf(const char *fmt, ...) |
---|
| 461 | { |
---|
[1c6c4d3] | 462 | va_list ap; |
---|
[28ee32b] | 463 | char *ret = NULL; |
---|
| 464 | va_start(ap, fmt); |
---|
| 465 | ret = g_strdup_vprintf(fmt, ap); |
---|
| 466 | va_end(ap); |
---|
| 467 | return ret; |
---|
[1c6c4d3] | 468 | } |
---|
| 469 | |
---|
[28ee32b] | 470 | |
---|
[12c35df] | 471 | /* Return the owl color associated with the named color. Return -1 |
---|
| 472 | * if the named color is not available |
---|
| 473 | */ |
---|
[e4eebe8] | 474 | int owl_util_string_to_color(char *color) |
---|
| 475 | { |
---|
[c2c5c77] | 476 | int c; |
---|
[7d4fbcd] | 477 | if (!strcasecmp(color, "black")) { |
---|
| 478 | return(OWL_COLOR_BLACK); |
---|
| 479 | } else if (!strcasecmp(color, "red")) { |
---|
| 480 | return(OWL_COLOR_RED); |
---|
| 481 | } else if (!strcasecmp(color, "green")) { |
---|
| 482 | return(OWL_COLOR_GREEN); |
---|
| 483 | } else if (!strcasecmp(color, "yellow")) { |
---|
| 484 | return(OWL_COLOR_YELLOW); |
---|
| 485 | } else if (!strcasecmp(color, "blue")) { |
---|
| 486 | return(OWL_COLOR_BLUE); |
---|
| 487 | } else if (!strcasecmp(color, "magenta")) { |
---|
| 488 | return(OWL_COLOR_MAGENTA); |
---|
| 489 | } else if (!strcasecmp(color, "cyan")) { |
---|
| 490 | return(OWL_COLOR_CYAN); |
---|
| 491 | } else if (!strcasecmp(color, "white")) { |
---|
| 492 | return(OWL_COLOR_WHITE); |
---|
| 493 | } else if (!strcasecmp(color, "default")) { |
---|
| 494 | return(OWL_COLOR_DEFAULT); |
---|
| 495 | } |
---|
[c2c5c77] | 496 | c = atoi(color); |
---|
| 497 | if (c >= -1 && c < COLORS) { |
---|
| 498 | return(c); |
---|
| 499 | } |
---|
[12c35df] | 500 | return(-1); |
---|
[7d4fbcd] | 501 | } |
---|
| 502 | |
---|
[e4eebe8] | 503 | /* Return a string name of the given owl color */ |
---|
| 504 | char *owl_util_color_to_string(int color) |
---|
| 505 | { |
---|
[7d4fbcd] | 506 | if (color==OWL_COLOR_BLACK) return("black"); |
---|
| 507 | if (color==OWL_COLOR_RED) return("red"); |
---|
| 508 | if (color==OWL_COLOR_GREEN) return("green"); |
---|
| 509 | if (color==OWL_COLOR_YELLOW) return("yellow"); |
---|
| 510 | if (color==OWL_COLOR_BLUE) return("blue"); |
---|
| 511 | if (color==OWL_COLOR_MAGENTA) return("magenta"); |
---|
| 512 | if (color==OWL_COLOR_CYAN) return("cyan"); |
---|
| 513 | if (color==OWL_COLOR_WHITE) return("white"); |
---|
| 514 | if (color==OWL_COLOR_DEFAULT) return("default"); |
---|
| 515 | return("Unknown color"); |
---|
| 516 | } |
---|
[e1c4636] | 517 | |
---|
[e4eebe8] | 518 | /* Get the default tty name. Caller must free the return */ |
---|
| 519 | char *owl_util_get_default_tty() |
---|
| 520 | { |
---|
[5145235] | 521 | char *out, *tmp; |
---|
[61e79a9] | 522 | |
---|
| 523 | if (getenv("DISPLAY")) { |
---|
| 524 | out=owl_strdup(getenv("DISPLAY")); |
---|
[5145235] | 525 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
---|
| 526 | out=owl_strdup(tmp); |
---|
[61e79a9] | 527 | if (!strncmp(out, "/dev/", 5)) { |
---|
| 528 | owl_free(out); |
---|
[5145235] | 529 | out=owl_strdup(tmp+5); |
---|
[61e79a9] | 530 | } |
---|
| 531 | } else { |
---|
[5145235] | 532 | out=owl_strdup("unknown"); |
---|
[61e79a9] | 533 | } |
---|
| 534 | return(out); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | |
---|
[e4eebe8] | 538 | /* Animation hack */ |
---|
| 539 | void owl_hack_animate() |
---|
| 540 | { |
---|
[4b464a4] | 541 | owl_messagelist *ml; |
---|
| 542 | owl_message *m; |
---|
| 543 | owl_fmtext *fm; |
---|
| 544 | char *text, *ptr; |
---|
| 545 | int place; |
---|
| 546 | |
---|
| 547 | /* grab the first message and make sure its id is 0 */ |
---|
| 548 | ml=owl_global_get_msglist(&g); |
---|
| 549 | m=owl_messagelist_get_element(ml, 0); |
---|
| 550 | if (!m) return; |
---|
| 551 | if (owl_message_get_id(m)!=0) return; |
---|
| 552 | |
---|
| 553 | fm=owl_message_get_fmtext(m); |
---|
| 554 | text=owl_fmtext_get_text(fm); |
---|
| 555 | |
---|
| 556 | ptr=strstr(text, "OvO"); |
---|
| 557 | if (ptr) { |
---|
| 558 | place=ptr-text; |
---|
| 559 | owl_fmtext_set_char(fm, place, '-'); |
---|
| 560 | owl_fmtext_set_char(fm, place+2, '-'); |
---|
| 561 | |
---|
| 562 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
[a15a84f] | 563 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 564 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 565 | /* TODO: this is a broken kludge */ |
---|
| 566 | if (owl_global_get_viewwin(&g)) { |
---|
| 567 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 568 | } |
---|
| 569 | } |
---|
[4b464a4] | 570 | owl_global_set_needrefresh(&g); |
---|
| 571 | return; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | ptr=strstr(text, "-v-"); |
---|
| 575 | if (ptr) { |
---|
| 576 | place=ptr-text; |
---|
| 577 | owl_fmtext_set_char(fm, place, 'O'); |
---|
| 578 | owl_fmtext_set_char(fm, place+2, 'O'); |
---|
| 579 | |
---|
| 580 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
[a15a84f] | 581 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 582 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 583 | /* TODO: this is a broken kludge */ |
---|
| 584 | if (owl_global_get_viewwin(&g)) { |
---|
| 585 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 586 | } |
---|
| 587 | } |
---|
[4b464a4] | 588 | owl_global_set_needrefresh(&g); |
---|
| 589 | return; |
---|
| 590 | } |
---|
| 591 | } |
---|
[e1c4636] | 592 | |
---|
[e4eebe8] | 593 | /* strip leading and trailing new lines. Caller must free the |
---|
| 594 | * return. |
---|
| 595 | */ |
---|
| 596 | char *owl_util_stripnewlines(char *in) |
---|
| 597 | { |
---|
[7e3e00a] | 598 | |
---|
| 599 | char *tmp, *ptr1, *ptr2, *out; |
---|
| 600 | |
---|
| 601 | ptr1=tmp=owl_strdup(in); |
---|
| 602 | while (ptr1[0]=='\n') { |
---|
| 603 | ptr1++; |
---|
| 604 | } |
---|
| 605 | ptr2=ptr1+strlen(ptr1)-1; |
---|
[1bb1e67] | 606 | while (ptr2>ptr1 && ptr2[0]=='\n') { |
---|
[7e3e00a] | 607 | ptr2[0]='\0'; |
---|
| 608 | ptr2--; |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | out=owl_strdup(ptr1); |
---|
| 612 | owl_free(tmp); |
---|
| 613 | return(out); |
---|
| 614 | } |
---|
| 615 | |
---|
[38cf544c] | 616 | /* Delete the line matching "line" from the named file. If no such |
---|
| 617 | * line is found the file is left intact. If backup==1 then create a |
---|
| 618 | * backupfile containing the original contents. This is an |
---|
| 619 | * inefficient impelementation which reads the entire file into |
---|
| 620 | * memory. |
---|
| 621 | */ |
---|
| 622 | void owl_util_file_deleteline(char *filename, char *line, int backup) |
---|
| 623 | { |
---|
| 624 | char buff[LINE], *text; |
---|
[e6449bc] | 625 | char *backupfilename=""; |
---|
| 626 | FILE *file, *backupfile=NULL; |
---|
[38cf544c] | 627 | int size, newline; |
---|
| 628 | |
---|
| 629 | /* open the file for reading */ |
---|
| 630 | file=fopen(filename, "r"); |
---|
| 631 | if (!file) { |
---|
[836ea3a3] | 632 | owl_function_error("Error opening file %s", filename); |
---|
[38cf544c] | 633 | return; |
---|
| 634 | } |
---|
[e6449bc] | 635 | |
---|
[38cf544c] | 636 | /* open the backup file for writing */ |
---|
| 637 | if (backup) { |
---|
| 638 | backupfilename=owl_sprintf("%s.backup", filename); |
---|
| 639 | backupfile=fopen(backupfilename, "w"); |
---|
| 640 | if (!backupfile) { |
---|
[836ea3a3] | 641 | owl_function_error("Error opening file %s for writing", backupfilename); |
---|
[378fa14] | 642 | owl_free(backupfilename); |
---|
[38cf544c] | 643 | return; |
---|
| 644 | } |
---|
[378fa14] | 645 | owl_free(backupfilename); |
---|
[38cf544c] | 646 | } |
---|
| 647 | |
---|
| 648 | /* we'll read the entire file into memory, minus the line we don't want and |
---|
| 649 | * and at the same time create the backup file if necessary |
---|
| 650 | */ |
---|
| 651 | text=owl_malloc(LINE); |
---|
| 652 | strcpy(text, ""); |
---|
| 653 | size=LINE; |
---|
| 654 | while (fgets(buff, LINE, file)!=NULL) { |
---|
| 655 | /* strip the newline */ |
---|
| 656 | newline=0; |
---|
| 657 | if (buff[strlen(buff)-1]=='\n') { |
---|
| 658 | buff[strlen(buff)-1]='\0'; |
---|
| 659 | newline=1; |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | /* if we don't match the line, add to saved text in memory */ |
---|
| 663 | if (strcasecmp(buff, line)) { |
---|
| 664 | size+=LINE; |
---|
| 665 | text=owl_realloc(text, size); |
---|
| 666 | strcat(text, buff); |
---|
| 667 | if (newline) strcat(text, "\n"); |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | /* write to backupfile if necessary */ |
---|
[6a50af2] | 671 | if (backup) { |
---|
| 672 | fputs(buff, backupfile); |
---|
| 673 | if (newline) fputs("\n", backupfile); |
---|
| 674 | } |
---|
[38cf544c] | 675 | } |
---|
[65ad073] | 676 | if (backup) fclose(backupfile); |
---|
[38cf544c] | 677 | fclose(file); |
---|
| 678 | |
---|
| 679 | /* now rewrite the original file from memory */ |
---|
| 680 | file=fopen(filename, "w"); |
---|
| 681 | if (!file) { |
---|
[836ea3a3] | 682 | owl_function_error("WARNING: Error opening %s for writing. Use %s to restore.", filename, backupfilename); |
---|
[38cf544c] | 683 | owl_function_beep(); |
---|
| 684 | owl_free(line); |
---|
| 685 | return; |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | fputs(text, file); |
---|
| 689 | fclose(file); |
---|
| 690 | } |
---|
| 691 | |
---|
[40458b9] | 692 | /* add the string 'str' to the list 'list' of strings, only if it |
---|
| 693 | * is not already present |
---|
| 694 | */ |
---|
| 695 | void owl_util_list_add_unique_string(owl_list *list, char *str) |
---|
| 696 | { |
---|
| 697 | int i, j; |
---|
| 698 | |
---|
| 699 | j=owl_list_get_size(list); |
---|
| 700 | for (i=0; i<j; i++) { |
---|
| 701 | if (!strcmp(str, owl_list_get_element(list, i))) return; |
---|
| 702 | } |
---|
| 703 | owl_list_append_element(list, owl_strdup(str)); |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | int owl_util_common_strings_in_lists(owl_list *a, owl_list *b) |
---|
| 707 | { |
---|
| 708 | int i, j, x, y; |
---|
| 709 | |
---|
| 710 | j=owl_list_get_size(a); |
---|
| 711 | for (i=0; i<j; i++) { |
---|
| 712 | y=owl_list_get_size(b); |
---|
| 713 | for (x=0; x<y; x++) { |
---|
| 714 | if (!strcmp(owl_list_get_element(a, i), owl_list_get_element(b, x))) return(1); |
---|
| 715 | } |
---|
| 716 | } |
---|
| 717 | return(0); |
---|
| 718 | } |
---|
| 719 | |
---|
[fe67f1f] | 720 | int owl_util_max(int a, int b) |
---|
| 721 | { |
---|
| 722 | if (a>b) return(a); |
---|
| 723 | return(b); |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | int owl_util_min(int a, int b) |
---|
| 727 | { |
---|
| 728 | if (a<b) return(a); |
---|
| 729 | return(b); |
---|
| 730 | } |
---|
| 731 | |
---|
[7a20e4c] | 732 | /* Return the base class or instance from a zephyr class, by removing |
---|
| 733 | leading `un' or trailing `.d'. |
---|
[be5aa09] | 734 | The caller is responsible for freeing the allocated string. |
---|
[7a20e4c] | 735 | */ |
---|
| 736 | char * owl_util_baseclass(char * class) |
---|
| 737 | { |
---|
[59916e8] | 738 | char *start, *end; |
---|
| 739 | |
---|
[f166580] | 740 | start = class; |
---|
[59916e8] | 741 | while(!strncmp(start, "un", 2)) { |
---|
| 742 | start += 2; |
---|
[7a20e4c] | 743 | } |
---|
[f166580] | 744 | |
---|
| 745 | start = owl_strdup(start); |
---|
[59916e8] | 746 | end = start + strlen(start) - 1; |
---|
[04166e9] | 747 | while(end > start && *end == 'd' && *(end-1) == '.') { |
---|
[7a20e4c] | 748 | end -= 2; |
---|
| 749 | } |
---|
| 750 | *(end + 1) = 0; |
---|
[59916e8] | 751 | |
---|
[f166580] | 752 | return start; |
---|
[7a20e4c] | 753 | } |
---|
| 754 | |
---|
[5376a95] | 755 | char * owl_get_datadir() |
---|
| 756 | { |
---|
| 757 | char * datadir = getenv("BARNOWL_DATA_DIR"); |
---|
| 758 | if(datadir != NULL) |
---|
| 759 | return strchr(datadir, '=') + 1; |
---|
| 760 | return DATADIR; |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | /* Strips format characters from a valid utf-8 string. Returns the |
---|
| 764 | empty string if 'in' does not validate. */ |
---|
| 765 | char * owl_strip_format_chars(char *in) |
---|
| 766 | { |
---|
| 767 | char *r; |
---|
| 768 | if (g_utf8_validate(in, -1, NULL)) { |
---|
| 769 | char *s, *p; |
---|
| 770 | r = owl_malloc(strlen(in)+1); |
---|
| 771 | r[0] = '\0'; |
---|
| 772 | s = in; |
---|
| 773 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 774 | while(p) { |
---|
| 775 | /* If it's a format character, copy up to it, and skip all |
---|
| 776 | immediately following format characters. */ |
---|
| 777 | if (_owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
| 778 | strncat(r, s, p-s); |
---|
| 779 | p = g_utf8_next_char(p); |
---|
| 780 | while (p && _owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
| 781 | p = g_utf8_next_char(p); |
---|
| 782 | } |
---|
| 783 | s = p; |
---|
| 784 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 785 | } |
---|
| 786 | else { |
---|
| 787 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | if (s) strcat(r,s); |
---|
| 791 | } |
---|
| 792 | else { |
---|
| 793 | r = owl_strdup(""); |
---|
| 794 | } |
---|
| 795 | return r; |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | /* If in is not UTF-8, convert from ISO-8859-1. We may want to allow |
---|
| 799 | * the caller to specify an alternative in the future. We also strip |
---|
| 800 | * out characters in Unicode Plane 16, as we use that plane internally |
---|
| 801 | * for formatting. |
---|
| 802 | */ |
---|
[6201646] | 803 | char * owl_validate_or_convert(char *in) |
---|
[5376a95] | 804 | { |
---|
[6201646] | 805 | if (g_utf8_validate(in, -1, NULL)) { |
---|
[5376a95] | 806 | return owl_strip_format_chars(in); |
---|
| 807 | } |
---|
| 808 | else { |
---|
[6201646] | 809 | return g_convert(in, -1, |
---|
[5376a95] | 810 | "UTF-8", "ISO-8859-1", |
---|
| 811 | NULL, NULL, NULL); |
---|
| 812 | } |
---|
[89f5338] | 813 | } |
---|
[7b1d048] | 814 | /* Attempts to convert 'in' to ISO-8859-1. Returns that if possible, |
---|
| 815 | else returns UTF-8. |
---|
| 816 | */ |
---|
| 817 | char * owl_get_iso_8859_1_if_possible(char *in) |
---|
| 818 | { |
---|
| 819 | char *out; |
---|
| 820 | if (g_utf8_validate(in, -1, NULL)) { |
---|
| 821 | out = g_convert(in, -1, |
---|
| 822 | "ISO-8859-1", "UTF-8", |
---|
| 823 | NULL, NULL, NULL); |
---|
| 824 | if (!out) { |
---|
| 825 | out = owl_strdup(in); |
---|
| 826 | } |
---|
| 827 | } |
---|
| 828 | else { |
---|
| 829 | out = owl_strdup(""); |
---|
| 830 | } |
---|
| 831 | return out; |
---|
| 832 | } |
---|
[89f5338] | 833 | |
---|
[e1c4636] | 834 | /**************************************************************************/ |
---|
| 835 | /************************* REGRESSION TESTS *******************************/ |
---|
| 836 | /**************************************************************************/ |
---|
| 837 | |
---|
| 838 | #ifdef OWL_INCLUDE_REG_TESTS |
---|
| 839 | |
---|
[0138478] | 840 | #include "test.h" |
---|
[e1c4636] | 841 | |
---|
[e4eebe8] | 842 | int owl_util_regtest(void) |
---|
| 843 | { |
---|
[e1c4636] | 844 | int numfailed=0; |
---|
| 845 | |
---|
[1cf3f8d3] | 846 | printf("# BEGIN testing owl_util\n"); |
---|
[e1c4636] | 847 | |
---|
| 848 | FAIL_UNLESS("owl_util_substitute 1", |
---|
[e3d9c77] | 849 | !strcmp("foo", owl_text_substitute("foo", "", "Y"))); |
---|
| 850 | FAIL_UNLESS("owl_text_substitute 2", |
---|
| 851 | !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ"))); |
---|
| 852 | FAIL_UNLESS("owl_text_substitute 3", |
---|
| 853 | !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o"))); |
---|
| 854 | FAIL_UNLESS("owl_text_substitute 4", |
---|
| 855 | !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo"))); |
---|
[e1c4636] | 856 | |
---|
| 857 | FAIL_UNLESS("skiptokens 1", |
---|
| 858 | !strcmp("bar quux", skiptokens("foo bar quux", 1))); |
---|
| 859 | FAIL_UNLESS("skiptokens 2", |
---|
| 860 | !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); |
---|
| 861 | |
---|
[e50cd56] | 862 | FAIL_UNLESS("owl_util_uniq 1", |
---|
| 863 | !strcmp("foo bar x", owl_util_uniq("foo", "bar x", "-"))); |
---|
| 864 | FAIL_UNLESS("owl_util_uniq 2", |
---|
| 865 | !strcmp("foo bar x", owl_util_uniq("foo", "bar -y x", "-"))); |
---|
| 866 | FAIL_UNLESS("owl_util_uniq 3", |
---|
| 867 | !strcmp("meep foo bar", owl_util_uniq("meep foo", "bar foo meep", "-"))); |
---|
| 868 | |
---|
[1cf3f8d3] | 869 | // if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); |
---|
| 870 | printf("# END testing owl_util (%d failures)\n", numfailed); |
---|
[e1c4636] | 871 | return(numfailed); |
---|
| 872 | } |
---|
| 873 | |
---|
| 874 | #endif /* OWL_INCLUDE_REG_TESTS */ |
---|