[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 | |
---|
| 282 | |
---|
[e4eebe8] | 283 | /* return the index of the last char before a change from the first one */ |
---|
| 284 | int owl_util_find_trans(char *in, int len) |
---|
| 285 | { |
---|
[7d4fbcd] | 286 | int i; |
---|
| 287 | for (i=1; i<len; i++) { |
---|
| 288 | if (in[i] != in[0]) return(i-1); |
---|
| 289 | } |
---|
| 290 | return(i); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | |
---|
[e4eebe8] | 294 | /* downcase the string 'foo' */ |
---|
| 295 | void downstr(char *foo) |
---|
| 296 | { |
---|
[7d4fbcd] | 297 | int i; |
---|
| 298 | for (i=0; foo[i]!='\0'; i++) { |
---|
| 299 | foo[i]=tolower(foo[i]); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
[e4eebe8] | 303 | /* exactly like strstr but case insensitive */ |
---|
| 304 | char *stristr(char *a, char *b) |
---|
| 305 | { |
---|
[7d4fbcd] | 306 | char *x, *y, *ret; |
---|
| 307 | |
---|
| 308 | if ((x=owl_strdup(a))==NULL) return(NULL); |
---|
| 309 | if ((y=owl_strdup(b))==NULL) return(NULL); |
---|
| 310 | downstr(x); |
---|
| 311 | downstr(y); |
---|
| 312 | ret=strstr(x, y); |
---|
[1fd0b25] | 313 | if (ret==NULL) { |
---|
| 314 | owl_free(x); |
---|
| 315 | owl_free(y); |
---|
| 316 | return(NULL); |
---|
| 317 | } |
---|
| 318 | ret=ret-x+a; |
---|
[7d4fbcd] | 319 | owl_free(x); |
---|
| 320 | owl_free(y); |
---|
| 321 | return(ret); |
---|
| 322 | } |
---|
| 323 | |
---|
[e4eebe8] | 324 | /* Caller must free response. |
---|
| 325 | * Takes in strings which are space-separated lists of tokens |
---|
| 326 | * and returns a single string containing no token more than once. |
---|
| 327 | * If prohibit is non-null, no token may start with a character |
---|
| 328 | * in prohibit. |
---|
| 329 | */ |
---|
| 330 | char *owl_util_uniq(char *A, char *B, char *prohibit) |
---|
| 331 | { |
---|
[42abb10] | 332 | |
---|
[e50cd56] | 333 | char *cat, **tok; |
---|
| 334 | int toklen, i, j, first=1; |
---|
| 335 | cat = owl_malloc(strlen(A)+strlen(B)+3); |
---|
| 336 | strcpy(cat, A); |
---|
| 337 | strcat(cat, " "); |
---|
| 338 | strcat(cat, B); |
---|
| 339 | tok = atokenize(cat, " ", &toklen); |
---|
| 340 | strcpy(cat, ""); |
---|
| 341 | for (i=0; i<toklen; i++) { |
---|
| 342 | int dup=0; |
---|
| 343 | for (j=0; j<i; j++) { |
---|
| 344 | if (!strcmp(tok[i], tok[j])) dup=1; |
---|
| 345 | } |
---|
| 346 | if (!dup && (!prohibit || !strchr(prohibit, tok[i][0]))) { |
---|
| 347 | if (!first) { |
---|
| 348 | strcat(cat, " "); |
---|
| 349 | } |
---|
| 350 | first=0; |
---|
| 351 | strcat(cat, tok[i]); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | atokenize_free(tok, toklen); |
---|
| 355 | return(cat); |
---|
| 356 | } |
---|
| 357 | |
---|
[e4eebe8] | 358 | /* return 1 if a string is only whitespace, otherwise 0 */ |
---|
| 359 | int only_whitespace(char *s) |
---|
| 360 | { |
---|
[10b866d] | 361 | int i; |
---|
| 362 | for (i=0; s[i]; i++) { |
---|
[1fd0b25] | 363 | if (!isspace((int) s[i])) return(0); |
---|
[10b866d] | 364 | } |
---|
| 365 | return(1); |
---|
| 366 | } |
---|
[7d4fbcd] | 367 | |
---|
[42abb10] | 368 | /* hooks for doing memory allocation et. al. in owl */ |
---|
| 369 | |
---|
[e4eebe8] | 370 | void *owl_malloc(size_t size) |
---|
| 371 | { |
---|
[7d4fbcd] | 372 | return(malloc(size)); |
---|
| 373 | } |
---|
| 374 | |
---|
[e4eebe8] | 375 | void owl_free(void *ptr) |
---|
| 376 | { |
---|
[7d4fbcd] | 377 | free(ptr); |
---|
| 378 | } |
---|
| 379 | |
---|
[e4eebe8] | 380 | char *owl_strdup(const char *s1) |
---|
| 381 | { |
---|
[7d4fbcd] | 382 | return(strdup(s1)); |
---|
| 383 | } |
---|
| 384 | |
---|
[e4eebe8] | 385 | void *owl_realloc(void *ptr, size_t size) |
---|
| 386 | { |
---|
[7d4fbcd] | 387 | return(realloc(ptr, size)); |
---|
| 388 | } |
---|
| 389 | |
---|
[e4eebe8] | 390 | |
---|
| 391 | /* allocates memory and returns the string or null. |
---|
| 392 | * caller must free the string. |
---|
| 393 | * from Linux sprintf man page. |
---|
| 394 | */ |
---|
| 395 | char *owl_sprintf(const char *fmt, ...) |
---|
| 396 | { |
---|
[1c6c4d3] | 397 | int n, size = 100; |
---|
| 398 | char *p; |
---|
| 399 | va_list ap; |
---|
[42abb10] | 400 | if ((p = owl_malloc (size)) == NULL) return (NULL); |
---|
[1c6c4d3] | 401 | while (1) { |
---|
| 402 | /* Try to print in the allocated space. */ |
---|
| 403 | va_start(ap, fmt); |
---|
| 404 | n = vsnprintf (p, size, fmt, ap); |
---|
| 405 | va_end(ap); |
---|
| 406 | /* If that worked, return the string. */ |
---|
| 407 | if (n > -1 && n < size) |
---|
| 408 | return p; |
---|
| 409 | /* Else try again with more space. */ |
---|
| 410 | if (n > -1) /* glibc 2.1 */ |
---|
| 411 | size = n+1; /* precisely what is needed */ |
---|
| 412 | else /* glibc 2.0 */ |
---|
| 413 | size *= 2; /* twice the old size */ |
---|
| 414 | if ((p = owl_realloc (p, size)) == NULL) |
---|
| 415 | return NULL; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
[e4eebe8] | 419 | /* Strip a local realm fron the zephyr user name. |
---|
| 420 | * The caller must free the return |
---|
| 421 | */ |
---|
| 422 | char *short_zuser(char *in) |
---|
| 423 | { |
---|
[be97670] | 424 | char *out, *ptr; |
---|
| 425 | |
---|
[7d4fbcd] | 426 | out=owl_strdup(in); |
---|
| 427 | ptr=strchr(out, '@'); |
---|
| 428 | if (ptr) { |
---|
| 429 | if (!strcasecmp(ptr+1, ZGetRealm())) { |
---|
| 430 | *ptr='\0'; |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | return(out); |
---|
| 434 | } |
---|
| 435 | |
---|
[e4eebe8] | 436 | /* Append a local realm to the zephyr user name if necessary. |
---|
| 437 | * The caller must free the return. |
---|
| 438 | */ |
---|
| 439 | char *long_zuser(char *in) |
---|
| 440 | { |
---|
[1c6c4d3] | 441 | char *ptr; |
---|
[7d4fbcd] | 442 | |
---|
[1c6c4d3] | 443 | if (NULL != (ptr=strchr(in, '@'))) { |
---|
| 444 | return owl_strdup(in); |
---|
| 445 | } else { |
---|
| 446 | return owl_sprintf("%s@%s", in, ZGetRealm()); |
---|
| 447 | } |
---|
[7d4fbcd] | 448 | } |
---|
[f9c43ae] | 449 | |
---|
| 450 | |
---|
[e4eebe8] | 451 | /* strip out the instance from a zsender's principal. Preserves the |
---|
| 452 | * realm if present. daemon.webzephyr is a special case. The |
---|
| 453 | * caller must free the return |
---|
| 454 | */ |
---|
| 455 | char *owl_util_smartstripped_user(char *in) |
---|
| 456 | { |
---|
[f9c43ae] | 457 | char *ptr, *realm, *out; |
---|
| 458 | |
---|
| 459 | out=owl_strdup(in); |
---|
| 460 | |
---|
| 461 | /* bail immeaditly if we don't have to do any work */ |
---|
| 462 | ptr=strchr(in, '.'); |
---|
| 463 | if (!strchr(in, '/') && !ptr) { |
---|
[5a6e6b9] | 464 | /* no '/' and no '.' */ |
---|
[f9c43ae] | 465 | return(out); |
---|
| 466 | } |
---|
| 467 | if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) { |
---|
[5a6e6b9] | 468 | /* There's a '.' but it's in the realm */ |
---|
[f9c43ae] | 469 | return(out); |
---|
| 470 | } |
---|
[5a6e6b9] | 471 | if (!strncasecmp(in, "daemon.webzephyr", strlen("daemon.webzephyr"))) { |
---|
| 472 | return(out); |
---|
| 473 | } |
---|
| 474 | |
---|
[f9c43ae] | 475 | /* remove the realm from ptr, but hold on to it */ |
---|
| 476 | realm=strchr(out, '@'); |
---|
| 477 | if (realm) realm[0]='\0'; |
---|
| 478 | |
---|
| 479 | /* strip */ |
---|
| 480 | ptr=strchr(out, '.'); |
---|
| 481 | if (!ptr) ptr=strchr(out, '/'); |
---|
| 482 | ptr[0]='\0'; |
---|
| 483 | |
---|
| 484 | /* reattach the realm if we had one */ |
---|
| 485 | if (realm) { |
---|
| 486 | strcat(out, "@"); |
---|
| 487 | strcat(out, realm+1); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | return(out); |
---|
| 491 | } |
---|
[7d4fbcd] | 492 | |
---|
[e4eebe8] | 493 | char *owl_getquoting(char *line) |
---|
| 494 | { |
---|
[7d4fbcd] | 495 | if (line[0]=='\0') return("'"); |
---|
| 496 | if (strchr(line, '\'')) return("\""); |
---|
| 497 | if (strchr(line, '"')) return("'"); |
---|
| 498 | if (strchr(line, ' ')) return("'"); |
---|
| 499 | return(""); |
---|
| 500 | } |
---|
| 501 | |
---|
[e4eebe8] | 502 | |
---|
| 503 | |
---|
| 504 | /* Return a string with any occurances of 'from' replaced with 'to'. |
---|
| 505 | * Does not currently handle backslash quoting, but may in the future. |
---|
| 506 | * Caller must free returned string. |
---|
| 507 | */ |
---|
| 508 | char *owl_util_substitute(char *in, char *from, char *to) |
---|
| 509 | { |
---|
[42abb10] | 510 | |
---|
[e1c4636] | 511 | char *out; |
---|
| 512 | int outlen, tolen, fromlen, inpos=0, outpos=0; |
---|
| 513 | |
---|
| 514 | if (!*from) return owl_strdup(in); |
---|
| 515 | |
---|
| 516 | outlen = strlen(in)+1; |
---|
| 517 | tolen = strlen(to); |
---|
| 518 | fromlen = strlen(from); |
---|
| 519 | out = malloc(outlen); |
---|
| 520 | |
---|
| 521 | while (in[inpos]) { |
---|
| 522 | if (!strncmp(in+inpos, from, fromlen)) { |
---|
| 523 | outlen += tolen; |
---|
| 524 | out = owl_realloc(out, outlen); |
---|
| 525 | strcpy(out+outpos, to); |
---|
| 526 | inpos += fromlen; |
---|
| 527 | outpos += tolen; |
---|
| 528 | } else { |
---|
| 529 | out[outpos] = in[inpos]; |
---|
| 530 | inpos++; outpos++; |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | out[outpos] = '\0'; |
---|
| 534 | return(out); |
---|
| 535 | } |
---|
| 536 | |
---|
[e4eebe8] | 537 | /* replace all instances of character a in buff with the character |
---|
| 538 | * b. buff must be null terminated. |
---|
| 539 | */ |
---|
| 540 | void owl_util_tr(char *buff, char a, char b) |
---|
| 541 | { |
---|
[ed2412d] | 542 | int i; |
---|
| 543 | |
---|
| 544 | owl_function_debugmsg("In: %s", buff); |
---|
| 545 | for (i=0; buff[i]!='\0'; i++) { |
---|
| 546 | if (buff[i]==a) buff[i]=b; |
---|
| 547 | } |
---|
| 548 | owl_function_debugmsg("Out: %s", buff); |
---|
| 549 | } |
---|
[7d4fbcd] | 550 | |
---|
[e4eebe8] | 551 | |
---|
| 552 | /* Return the owl color associated with the named color */ |
---|
| 553 | int owl_util_string_to_color(char *color) |
---|
| 554 | { |
---|
[7d4fbcd] | 555 | if (!strcasecmp(color, "black")) { |
---|
| 556 | return(OWL_COLOR_BLACK); |
---|
| 557 | } else if (!strcasecmp(color, "red")) { |
---|
| 558 | return(OWL_COLOR_RED); |
---|
| 559 | } else if (!strcasecmp(color, "green")) { |
---|
| 560 | return(OWL_COLOR_GREEN); |
---|
| 561 | } else if (!strcasecmp(color, "yellow")) { |
---|
| 562 | return(OWL_COLOR_YELLOW); |
---|
| 563 | } else if (!strcasecmp(color, "blue")) { |
---|
| 564 | return(OWL_COLOR_BLUE); |
---|
| 565 | } else if (!strcasecmp(color, "magenta")) { |
---|
| 566 | return(OWL_COLOR_MAGENTA); |
---|
| 567 | } else if (!strcasecmp(color, "cyan")) { |
---|
| 568 | return(OWL_COLOR_CYAN); |
---|
| 569 | } else if (!strcasecmp(color, "white")) { |
---|
| 570 | return(OWL_COLOR_WHITE); |
---|
| 571 | } else if (!strcasecmp(color, "default")) { |
---|
| 572 | return(OWL_COLOR_DEFAULT); |
---|
| 573 | } |
---|
| 574 | return(OWL_COLOR_DEFAULT); |
---|
| 575 | } |
---|
| 576 | |
---|
[e4eebe8] | 577 | /* Return a string name of the given owl color */ |
---|
| 578 | char *owl_util_color_to_string(int color) |
---|
| 579 | { |
---|
[7d4fbcd] | 580 | if (color==OWL_COLOR_BLACK) return("black"); |
---|
| 581 | if (color==OWL_COLOR_RED) return("red"); |
---|
| 582 | if (color==OWL_COLOR_GREEN) return("green"); |
---|
| 583 | if (color==OWL_COLOR_YELLOW) return("yellow"); |
---|
| 584 | if (color==OWL_COLOR_BLUE) return("blue"); |
---|
| 585 | if (color==OWL_COLOR_MAGENTA) return("magenta"); |
---|
| 586 | if (color==OWL_COLOR_CYAN) return("cyan"); |
---|
| 587 | if (color==OWL_COLOR_WHITE) return("white"); |
---|
| 588 | if (color==OWL_COLOR_DEFAULT) return("default"); |
---|
| 589 | return("Unknown color"); |
---|
| 590 | } |
---|
[e1c4636] | 591 | |
---|
[e4eebe8] | 592 | /* Get the default tty name. Caller must free the return */ |
---|
| 593 | char *owl_util_get_default_tty() |
---|
| 594 | { |
---|
[5145235] | 595 | char *out, *tmp; |
---|
[61e79a9] | 596 | |
---|
| 597 | if (getenv("DISPLAY")) { |
---|
| 598 | out=owl_strdup(getenv("DISPLAY")); |
---|
[5145235] | 599 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
---|
| 600 | out=owl_strdup(tmp); |
---|
[61e79a9] | 601 | if (!strncmp(out, "/dev/", 5)) { |
---|
| 602 | owl_free(out); |
---|
[5145235] | 603 | out=owl_strdup(tmp+5); |
---|
[61e79a9] | 604 | } |
---|
| 605 | } else { |
---|
[5145235] | 606 | out=owl_strdup("unknown"); |
---|
[61e79a9] | 607 | } |
---|
| 608 | return(out); |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | |
---|
[e4eebe8] | 612 | /* Animation hack */ |
---|
| 613 | void owl_hack_animate() |
---|
| 614 | { |
---|
[4b464a4] | 615 | owl_messagelist *ml; |
---|
| 616 | owl_message *m; |
---|
| 617 | owl_fmtext *fm; |
---|
| 618 | char *text, *ptr; |
---|
| 619 | int place; |
---|
| 620 | |
---|
| 621 | /* grab the first message and make sure its id is 0 */ |
---|
| 622 | ml=owl_global_get_msglist(&g); |
---|
| 623 | m=owl_messagelist_get_element(ml, 0); |
---|
| 624 | if (!m) return; |
---|
| 625 | if (owl_message_get_id(m)!=0) return; |
---|
| 626 | |
---|
| 627 | fm=owl_message_get_fmtext(m); |
---|
| 628 | text=owl_fmtext_get_text(fm); |
---|
| 629 | |
---|
| 630 | ptr=strstr(text, "OvO"); |
---|
| 631 | if (ptr) { |
---|
| 632 | place=ptr-text; |
---|
| 633 | owl_fmtext_set_char(fm, place, '-'); |
---|
| 634 | owl_fmtext_set_char(fm, place+2, '-'); |
---|
| 635 | |
---|
| 636 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
[a15a84f] | 637 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 638 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 639 | /* TODO: this is a broken kludge */ |
---|
| 640 | if (owl_global_get_viewwin(&g)) { |
---|
| 641 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 642 | } |
---|
| 643 | } |
---|
[4b464a4] | 644 | owl_global_set_needrefresh(&g); |
---|
| 645 | return; |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | ptr=strstr(text, "-v-"); |
---|
| 649 | if (ptr) { |
---|
| 650 | place=ptr-text; |
---|
| 651 | owl_fmtext_set_char(fm, place, 'O'); |
---|
| 652 | owl_fmtext_set_char(fm, place+2, 'O'); |
---|
| 653 | |
---|
| 654 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
[a15a84f] | 655 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 656 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 657 | /* TODO: this is a broken kludge */ |
---|
| 658 | if (owl_global_get_viewwin(&g)) { |
---|
| 659 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 660 | } |
---|
| 661 | } |
---|
[4b464a4] | 662 | owl_global_set_needrefresh(&g); |
---|
| 663 | return; |
---|
| 664 | } |
---|
| 665 | } |
---|
[e1c4636] | 666 | |
---|
[e4eebe8] | 667 | /* strip leading and trailing new lines. Caller must free the |
---|
| 668 | * return. |
---|
| 669 | */ |
---|
| 670 | char *owl_util_stripnewlines(char *in) |
---|
| 671 | { |
---|
[7e3e00a] | 672 | |
---|
| 673 | char *tmp, *ptr1, *ptr2, *out; |
---|
| 674 | |
---|
| 675 | ptr1=tmp=owl_strdup(in); |
---|
| 676 | while (ptr1[0]=='\n') { |
---|
| 677 | ptr1++; |
---|
| 678 | } |
---|
| 679 | ptr2=ptr1+strlen(ptr1)-1; |
---|
| 680 | while (ptr2[0]=='\n' && ptr2>ptr1) { |
---|
| 681 | ptr2[0]='\0'; |
---|
| 682 | ptr2--; |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | out=owl_strdup(ptr1); |
---|
| 686 | owl_free(tmp); |
---|
| 687 | return(out); |
---|
| 688 | } |
---|
| 689 | |
---|
[38cf544c] | 690 | /* Delete the line matching "line" from the named file. If no such |
---|
| 691 | * line is found the file is left intact. If backup==1 then create a |
---|
| 692 | * backupfile containing the original contents. This is an |
---|
| 693 | * inefficient impelementation which reads the entire file into |
---|
| 694 | * memory. |
---|
| 695 | */ |
---|
| 696 | void owl_util_file_deleteline(char *filename, char *line, int backup) |
---|
| 697 | { |
---|
| 698 | char buff[LINE], *text; |
---|
| 699 | char *backupfilename; |
---|
| 700 | FILE *file, *backupfile; |
---|
| 701 | int size, newline; |
---|
| 702 | |
---|
| 703 | /* open the file for reading */ |
---|
| 704 | file=fopen(filename, "r"); |
---|
| 705 | if (!file) { |
---|
| 706 | owl_function_makemsg("Error opening file %s", filename); |
---|
| 707 | return; |
---|
| 708 | } |
---|
| 709 | |
---|
| 710 | /* open the backup file for writing */ |
---|
| 711 | if (backup) { |
---|
| 712 | backupfilename=owl_sprintf("%s.backup", filename); |
---|
| 713 | backupfile=fopen(backupfilename, "w"); |
---|
| 714 | if (!backupfile) { |
---|
| 715 | owl_function_makemsg("Error opening file %s for writing", backupfilename); |
---|
[378fa14] | 716 | owl_free(backupfilename); |
---|
[38cf544c] | 717 | return; |
---|
| 718 | } |
---|
[378fa14] | 719 | owl_free(backupfilename); |
---|
[38cf544c] | 720 | } |
---|
| 721 | |
---|
| 722 | /* we'll read the entire file into memory, minus the line we don't want and |
---|
| 723 | * and at the same time create the backup file if necessary |
---|
| 724 | */ |
---|
| 725 | text=owl_malloc(LINE); |
---|
| 726 | strcpy(text, ""); |
---|
| 727 | size=LINE; |
---|
| 728 | while (fgets(buff, LINE, file)!=NULL) { |
---|
| 729 | /* strip the newline */ |
---|
| 730 | newline=0; |
---|
| 731 | if (buff[strlen(buff)-1]=='\n') { |
---|
| 732 | buff[strlen(buff)-1]='\0'; |
---|
| 733 | newline=1; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | /* if we don't match the line, add to saved text in memory */ |
---|
| 737 | if (strcasecmp(buff, line)) { |
---|
| 738 | size+=LINE; |
---|
| 739 | text=owl_realloc(text, size); |
---|
| 740 | strcat(text, buff); |
---|
| 741 | if (newline) strcat(text, "\n"); |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | /* write to backupfile if necessary */ |
---|
| 745 | if (backup) fputs(buff, backupfile); |
---|
| 746 | } |
---|
[65ad073] | 747 | if (backup) fclose(backupfile); |
---|
[38cf544c] | 748 | fclose(file); |
---|
| 749 | |
---|
| 750 | /* now rewrite the original file from memory */ |
---|
| 751 | file=fopen(filename, "w"); |
---|
| 752 | if (!file) { |
---|
| 753 | owl_function_makemsg("WARNING: Error opening %s for writing. Use %s to restore.", filename, backupfilename); |
---|
| 754 | owl_function_beep(); |
---|
| 755 | owl_free(line); |
---|
| 756 | return; |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | fputs(text, file); |
---|
| 760 | fclose(file); |
---|
| 761 | } |
---|
| 762 | |
---|
[e1c4636] | 763 | /**************************************************************************/ |
---|
| 764 | /************************* REGRESSION TESTS *******************************/ |
---|
| 765 | /**************************************************************************/ |
---|
| 766 | |
---|
| 767 | #ifdef OWL_INCLUDE_REG_TESTS |
---|
| 768 | |
---|
| 769 | #define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc) |
---|
| 770 | |
---|
[e4eebe8] | 771 | int owl_util_regtest(void) |
---|
| 772 | { |
---|
[e1c4636] | 773 | int numfailed=0; |
---|
| 774 | |
---|
| 775 | printf("BEGIN testing owl_util\n"); |
---|
| 776 | |
---|
| 777 | FAIL_UNLESS("owl_util_substitute 1", |
---|
| 778 | !strcmp("foo", owl_util_substitute("foo", "", "Y"))); |
---|
| 779 | FAIL_UNLESS("owl_util_substitute 2", |
---|
| 780 | !strcmp("fYZYZ", owl_util_substitute("foo", "o", "YZ"))); |
---|
| 781 | FAIL_UNLESS("owl_util_substitute 3", |
---|
| 782 | !strcmp("foo", owl_util_substitute("fYZYZ", "YZ", "o"))); |
---|
| 783 | FAIL_UNLESS("owl_util_substitute 4", |
---|
| 784 | !strcmp("/u/foo/meep", owl_util_substitute("~/meep", "~", "/u/foo"))); |
---|
| 785 | |
---|
| 786 | FAIL_UNLESS("skiptokens 1", |
---|
| 787 | !strcmp("bar quux", skiptokens("foo bar quux", 1))); |
---|
| 788 | FAIL_UNLESS("skiptokens 2", |
---|
| 789 | !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); |
---|
| 790 | |
---|
[e50cd56] | 791 | FAIL_UNLESS("owl_util_uniq 1", |
---|
| 792 | !strcmp("foo bar x", owl_util_uniq("foo", "bar x", "-"))); |
---|
| 793 | FAIL_UNLESS("owl_util_uniq 2", |
---|
| 794 | !strcmp("foo bar x", owl_util_uniq("foo", "bar -y x", "-"))); |
---|
| 795 | FAIL_UNLESS("owl_util_uniq 3", |
---|
| 796 | !strcmp("meep foo bar", owl_util_uniq("meep foo", "bar foo meep", "-"))); |
---|
| 797 | |
---|
[e1c4636] | 798 | if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); |
---|
| 799 | printf("END testing owl_util (%d failures)\n", numfailed); |
---|
| 800 | return(numfailed); |
---|
| 801 | } |
---|
| 802 | |
---|
| 803 | #endif /* OWL_INCLUDE_REG_TESTS */ |
---|