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