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