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