| 1 | #include "owl.h" |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <unistd.h> |
|---|
| 5 | #include <ctype.h> |
|---|
| 6 | #include <pwd.h> |
|---|
| 7 | #include <sys/stat.h> |
|---|
| 8 | #include <sys/types.h> |
|---|
| 9 | |
|---|
| 10 | void sepbar(const char *in) |
|---|
| 11 | { |
|---|
| 12 | WINDOW *sepwin; |
|---|
| 13 | const owl_messagelist *ml; |
|---|
| 14 | const owl_view *v; |
|---|
| 15 | int x, y, i; |
|---|
| 16 | const 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 | if (owl_global_is_fancylines(&g)) { |
|---|
| 25 | whline(sepwin, ACS_HLINE, owl_global_get_cols(&g)); |
|---|
| 26 | } else { |
|---|
| 27 | whline(sepwin, '-', owl_global_get_cols(&g)); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | if (owl_global_is_sepbar_disable(&g)) { |
|---|
| 31 | getyx(sepwin, y, x); |
|---|
| 32 | wmove(sepwin, y, owl_global_get_cols(&g)-1); |
|---|
| 33 | return; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | wmove(sepwin, 0, 2); |
|---|
| 37 | |
|---|
| 38 | if (owl_messagelist_get_size(ml) == 0) |
|---|
| 39 | waddstr(sepwin, " (-/-) "); |
|---|
| 40 | else |
|---|
| 41 | wprintw(sepwin, " (%i/%i/%i) ", owl_global_get_curmsg(&g) + 1, |
|---|
| 42 | owl_view_get_size(v), |
|---|
| 43 | owl_messagelist_get_size(ml)); |
|---|
| 44 | |
|---|
| 45 | foo=owl_view_get_filtname(v); |
|---|
| 46 | if (strcmp(foo, owl_global_get_view_home(&g))) |
|---|
| 47 | wattroff(sepwin, A_REVERSE); |
|---|
| 48 | wprintw(sepwin, " %s ", owl_view_get_filtname(v)); |
|---|
| 49 | if (strcmp(foo, owl_global_get_view_home(&g))) |
|---|
| 50 | wattron(sepwin, A_REVERSE); |
|---|
| 51 | |
|---|
| 52 | if (owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) { |
|---|
| 53 | getyx(sepwin, y, x); |
|---|
| 54 | wmove(sepwin, y, x+2); |
|---|
| 55 | wattron(sepwin, A_BOLD); |
|---|
| 56 | waddstr(sepwin, " <truncated> "); |
|---|
| 57 | wattroff(sepwin, A_BOLD); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g)); |
|---|
| 61 | if ((i != -1) && |
|---|
| 62 | (i < owl_view_get_size(v)-1)) { |
|---|
| 63 | getyx(sepwin, y, x); |
|---|
| 64 | wmove(sepwin, y, x+2); |
|---|
| 65 | wattron(sepwin, A_BOLD); |
|---|
| 66 | waddstr(sepwin, " <more> "); |
|---|
| 67 | wattroff(sepwin, A_BOLD); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | if (owl_global_get_rightshift(&g)>0) { |
|---|
| 71 | getyx(sepwin, y, x); |
|---|
| 72 | wmove(sepwin, y, x+2); |
|---|
| 73 | wprintw(sepwin, " right: %i ", owl_global_get_rightshift(&g)); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | if (owl_global_is_zaway(&g) || owl_global_is_aaway(&g)) { |
|---|
| 77 | getyx(sepwin, y, x); |
|---|
| 78 | wmove(sepwin, y, x+2); |
|---|
| 79 | wattron(sepwin, A_BOLD); |
|---|
| 80 | wattroff(sepwin, A_REVERSE); |
|---|
| 81 | if (owl_global_is_zaway(&g) && owl_global_is_aaway(&g)) { |
|---|
| 82 | waddstr(sepwin, " AWAY "); |
|---|
| 83 | } else if (owl_global_is_zaway(&g)) { |
|---|
| 84 | waddstr(sepwin, " Z-AWAY "); |
|---|
| 85 | } else if (owl_global_is_aaway(&g)) { |
|---|
| 86 | waddstr(sepwin, " A-AWAY "); |
|---|
| 87 | } |
|---|
| 88 | wattron(sepwin, A_REVERSE); |
|---|
| 89 | wattroff(sepwin, A_BOLD); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if (owl_global_get_curmsg_vert_offset(&g)) { |
|---|
| 93 | getyx(sepwin, y, x); |
|---|
| 94 | wmove(sepwin, y, x+2); |
|---|
| 95 | wattron(sepwin, A_BOLD); |
|---|
| 96 | wattroff(sepwin, A_REVERSE); |
|---|
| 97 | waddstr(sepwin, " SCROLL "); |
|---|
| 98 | wattron(sepwin, A_REVERSE); |
|---|
| 99 | wattroff(sepwin, A_BOLD); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if (in) { |
|---|
| 103 | getyx(sepwin, y, x); |
|---|
| 104 | wmove(sepwin, y, x+2); |
|---|
| 105 | waddstr(sepwin, in); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | appendtosepbar = owl_global_get_appendtosepbar(&g); |
|---|
| 109 | if (appendtosepbar && *appendtosepbar) { |
|---|
| 110 | getyx(sepwin, y, x); |
|---|
| 111 | wmove(sepwin, y, x+2); |
|---|
| 112 | waddstr(sepwin, " "); |
|---|
| 113 | waddstr(sepwin, owl_global_get_appendtosepbar(&g)); |
|---|
| 114 | waddstr(sepwin, " "); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | getyx(sepwin, y, x); |
|---|
| 118 | wmove(sepwin, y, owl_global_get_cols(&g)-1); |
|---|
| 119 | |
|---|
| 120 | wattroff(sepwin, A_BOLD); |
|---|
| 121 | wattroff(sepwin, A_REVERSE); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | char **atokenize(const char *buffer, const char *sep, int *i) |
|---|
| 125 | { |
|---|
| 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_strdup(buffer); |
|---|
| 132 | |
|---|
| 133 | args=NULL; |
|---|
| 134 | while (!done) { |
|---|
| 135 | if (first) { |
|---|
| 136 | first=0; |
|---|
| 137 | foo=strtok(workbuff, sep); |
|---|
| 138 | } else { |
|---|
| 139 | foo=strtok(NULL, sep); |
|---|
| 140 | } |
|---|
| 141 | if (foo==NULL) { |
|---|
| 142 | done=1; |
|---|
| 143 | } else { |
|---|
| 144 | args=owl_realloc(args, sizeof(char *) * (count+1)); |
|---|
| 145 | args[count] = owl_strdup(foo); |
|---|
| 146 | count++; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | *i=count; |
|---|
| 150 | owl_free(workbuff); |
|---|
| 151 | return(args); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | const char *skiptokens(const char *buff, int n) { |
|---|
| 155 | /* skips n tokens and returns where that would be. */ |
|---|
| 156 | char quote = 0; |
|---|
| 157 | while (*buff && n>0) { |
|---|
| 158 | while (*buff == ' ') buff++; |
|---|
| 159 | while (*buff && (quote || *buff != ' ')) { |
|---|
| 160 | if(quote) { |
|---|
| 161 | if(*buff == quote) quote = 0; |
|---|
| 162 | } else if(*buff == '"' || *buff == '\'') { |
|---|
| 163 | quote = *buff; |
|---|
| 164 | } |
|---|
| 165 | buff++; |
|---|
| 166 | } |
|---|
| 167 | while (*buff == ' ') buff++; |
|---|
| 168 | n--; |
|---|
| 169 | } |
|---|
| 170 | return buff; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* Return a "nice" version of the path. Tilde expansion is done, and |
|---|
| 174 | * duplicate slashes are removed. Caller must free the return. |
|---|
| 175 | */ |
|---|
| 176 | char *owl_util_makepath(const char *in) |
|---|
| 177 | { |
|---|
| 178 | int i, j, x; |
|---|
| 179 | char *out, user[MAXPATHLEN]; |
|---|
| 180 | struct passwd *pw; |
|---|
| 181 | |
|---|
| 182 | out=owl_malloc(MAXPATHLEN+1); |
|---|
| 183 | out[0]='\0'; |
|---|
| 184 | j=strlen(in); |
|---|
| 185 | x=0; |
|---|
| 186 | for (i=0; i<j; i++) { |
|---|
| 187 | if (in[i]=='~') { |
|---|
| 188 | if ( (i==(j-1)) || /* last character */ |
|---|
| 189 | (in[i+1]=='/') ) { /* ~/ */ |
|---|
| 190 | /* use my homedir */ |
|---|
| 191 | pw=getpwuid(getuid()); |
|---|
| 192 | if (!pw) { |
|---|
| 193 | out[x]=in[i]; |
|---|
| 194 | } else { |
|---|
| 195 | out[x]='\0'; |
|---|
| 196 | strcat(out, pw->pw_dir); |
|---|
| 197 | x+=strlen(pw->pw_dir); |
|---|
| 198 | } |
|---|
| 199 | } else { |
|---|
| 200 | /* another user homedir */ |
|---|
| 201 | int a, b; |
|---|
| 202 | b=0; |
|---|
| 203 | for (a=i+1; i<j; a++) { |
|---|
| 204 | if (in[a]==' ' || in[a]=='/') { |
|---|
| 205 | break; |
|---|
| 206 | } else { |
|---|
| 207 | user[b]=in[a]; |
|---|
| 208 | i++; |
|---|
| 209 | b++; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | user[b]='\0'; |
|---|
| 213 | pw=getpwnam(user); |
|---|
| 214 | if (!pw) { |
|---|
| 215 | out[x]=in[i]; |
|---|
| 216 | } else { |
|---|
| 217 | out[x]='\0'; |
|---|
| 218 | strcat(out, pw->pw_dir); |
|---|
| 219 | x+=strlen(pw->pw_dir); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | } else if (in[i]=='/') { |
|---|
| 223 | /* check for a double / */ |
|---|
| 224 | if (i<(j-1) && (in[i+1]=='/')) { |
|---|
| 225 | /* do nothing */ |
|---|
| 226 | } else { |
|---|
| 227 | out[x]=in[i]; |
|---|
| 228 | x++; |
|---|
| 229 | } |
|---|
| 230 | } else { |
|---|
| 231 | out[x]=in[i]; |
|---|
| 232 | x++; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | out[x]='\0'; |
|---|
| 236 | return(out); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | void atokenize_delete(char **tok, int nels) |
|---|
| 240 | { |
|---|
| 241 | int i; |
|---|
| 242 | for (i=0; i<nels; i++) { |
|---|
| 243 | owl_free(tok[i]); |
|---|
| 244 | } |
|---|
| 245 | owl_free(tok); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | void owl_parse_delete(char **argv, int argc) |
|---|
| 250 | { |
|---|
| 251 | int i; |
|---|
| 252 | |
|---|
| 253 | if (!argv) return; |
|---|
| 254 | |
|---|
| 255 | for (i=0; i<argc; i++) { |
|---|
| 256 | if (argv[i]) owl_free(argv[i]); |
|---|
| 257 | } |
|---|
| 258 | owl_free(argv); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | char **owl_parseline(const char *line, int *argc) |
|---|
| 262 | { |
|---|
| 263 | /* break a command line up into argv, argc. The caller must free |
|---|
| 264 | the returned values. If there is an error argc will be set to |
|---|
| 265 | -1, argv will be NULL and the caller does not need to free |
|---|
| 266 | anything */ |
|---|
| 267 | |
|---|
| 268 | char **argv; |
|---|
| 269 | int i, len, between=1; |
|---|
| 270 | char *curarg; |
|---|
| 271 | char quote; |
|---|
| 272 | |
|---|
| 273 | argv=owl_malloc(sizeof(char *)); |
|---|
| 274 | len=strlen(line); |
|---|
| 275 | curarg=owl_malloc(len+10); |
|---|
| 276 | strcpy(curarg, ""); |
|---|
| 277 | quote='\0'; |
|---|
| 278 | *argc=0; |
|---|
| 279 | for (i=0; i<len+1; i++) { |
|---|
| 280 | /* find the first real character */ |
|---|
| 281 | if (between) { |
|---|
| 282 | if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') { |
|---|
| 283 | continue; |
|---|
| 284 | } else { |
|---|
| 285 | between=0; |
|---|
| 286 | i--; |
|---|
| 287 | continue; |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | /* deal with a quote character */ |
|---|
| 292 | if (line[i]=='"' || line[i]=="'"[0]) { |
|---|
| 293 | /* if this type of quote is open, close it */ |
|---|
| 294 | if (quote==line[i]) { |
|---|
| 295 | quote='\0'; |
|---|
| 296 | continue; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /* if no quoting is open then open with this */ |
|---|
| 300 | if (quote=='\0') { |
|---|
| 301 | quote=line[i]; |
|---|
| 302 | continue; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | /* if another type of quote is open then treat this as a literal */ |
|---|
| 306 | curarg[strlen(curarg)+1]='\0'; |
|---|
| 307 | curarg[strlen(curarg)]=line[i]; |
|---|
| 308 | continue; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | /* if it's not a space or end of command, then use it */ |
|---|
| 312 | if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') { |
|---|
| 313 | curarg[strlen(curarg)+1]='\0'; |
|---|
| 314 | curarg[strlen(curarg)]=line[i]; |
|---|
| 315 | continue; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | /* otherwise, if we're not in quotes, add the whole argument */ |
|---|
| 319 | if (quote=='\0') { |
|---|
| 320 | /* add the argument */ |
|---|
| 321 | argv=owl_realloc(argv, sizeof(char *)*((*argc)+1)); |
|---|
| 322 | argv[*argc] = owl_strdup(curarg); |
|---|
| 323 | *argc=*argc+1; |
|---|
| 324 | strcpy(curarg, ""); |
|---|
| 325 | between=1; |
|---|
| 326 | continue; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | /* if it is a space and we're in quotes, then use it */ |
|---|
| 330 | curarg[strlen(curarg)+1]='\0'; |
|---|
| 331 | curarg[strlen(curarg)]=line[i]; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | owl_free(curarg); |
|---|
| 335 | |
|---|
| 336 | /* check for unbalanced quotes */ |
|---|
| 337 | if (quote!='\0') { |
|---|
| 338 | owl_parse_delete(argv, *argc); |
|---|
| 339 | *argc=-1; |
|---|
| 340 | return(NULL); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | return(argv); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /* caller must free the return */ |
|---|
| 347 | char *owl_util_minutes_to_timestr(int in) |
|---|
| 348 | { |
|---|
| 349 | int days, hours; |
|---|
| 350 | long run; |
|---|
| 351 | char *out; |
|---|
| 352 | |
|---|
| 353 | run=in; |
|---|
| 354 | |
|---|
| 355 | days=run/1440; |
|---|
| 356 | run-=days*1440; |
|---|
| 357 | hours=run/60; |
|---|
| 358 | run-=hours*60; |
|---|
| 359 | |
|---|
| 360 | if (days>0) { |
|---|
| 361 | out=owl_sprintf("%i d %2.2i:%2.2li", days, hours, run); |
|---|
| 362 | } else { |
|---|
| 363 | out=owl_sprintf(" %2.2i:%2.2li", hours, run); |
|---|
| 364 | } |
|---|
| 365 | return(out); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | /* hooks for doing memory allocation et. al. in owl */ |
|---|
| 369 | |
|---|
| 370 | void *owl_malloc(size_t size) |
|---|
| 371 | { |
|---|
| 372 | return(g_malloc(size)); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | void owl_free(void *ptr) |
|---|
| 376 | { |
|---|
| 377 | g_free(ptr); |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | char *owl_strdup(const char *s1) |
|---|
| 381 | { |
|---|
| 382 | return(g_strdup(s1)); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | void *owl_realloc(void *ptr, size_t size) |
|---|
| 386 | { |
|---|
| 387 | return(g_realloc(ptr, size)); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | /* allocates memory and returns the string or null. |
|---|
| 391 | * caller must free the string. |
|---|
| 392 | */ |
|---|
| 393 | char *owl_sprintf(const char *fmt, ...) |
|---|
| 394 | { |
|---|
| 395 | va_list ap; |
|---|
| 396 | char *ret = NULL; |
|---|
| 397 | va_start(ap, fmt); |
|---|
| 398 | ret = g_strdup_vprintf(fmt, ap); |
|---|
| 399 | va_end(ap); |
|---|
| 400 | return ret; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | /* These are in order of their value in owl.h */ |
|---|
| 404 | static const struct { |
|---|
| 405 | int number; |
|---|
| 406 | const char *name; |
|---|
| 407 | } color_map[] = { |
|---|
| 408 | {OWL_COLOR_INVALID, "invalid"}, |
|---|
| 409 | {OWL_COLOR_DEFAULT, "default"}, |
|---|
| 410 | {OWL_COLOR_BLACK, "black"}, |
|---|
| 411 | {OWL_COLOR_RED, "red"}, |
|---|
| 412 | {OWL_COLOR_GREEN, "green"}, |
|---|
| 413 | {OWL_COLOR_YELLOW,"yellow"}, |
|---|
| 414 | {OWL_COLOR_BLUE, "blue"}, |
|---|
| 415 | {OWL_COLOR_MAGENTA, "magenta"}, |
|---|
| 416 | {OWL_COLOR_CYAN, "cyan"}, |
|---|
| 417 | {OWL_COLOR_WHITE, "white"}, |
|---|
| 418 | }; |
|---|
| 419 | |
|---|
| 420 | /* Return the owl color associated with the named color. Return -1 |
|---|
| 421 | * if the named color is not available |
|---|
| 422 | */ |
|---|
| 423 | int owl_util_string_to_color(const char *color) |
|---|
| 424 | { |
|---|
| 425 | int c, i; |
|---|
| 426 | char *p; |
|---|
| 427 | |
|---|
| 428 | for (i = 0; i < (sizeof(color_map)/sizeof(color_map[0])); i++) |
|---|
| 429 | if (strcasecmp(color, color_map[i].name) == 0) |
|---|
| 430 | return color_map[i].number; |
|---|
| 431 | |
|---|
| 432 | c = strtol(color, &p, 10); |
|---|
| 433 | if (p != color && c >= -1 && c < COLORS) { |
|---|
| 434 | return(c); |
|---|
| 435 | } |
|---|
| 436 | return(OWL_COLOR_INVALID); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | /* Return a string name of the given owl color */ |
|---|
| 440 | const char *owl_util_color_to_string(int color) |
|---|
| 441 | { |
|---|
| 442 | if (color >= OWL_COLOR_INVALID && color <= OWL_COLOR_WHITE) |
|---|
| 443 | return color_map[color - OWL_COLOR_INVALID].name; |
|---|
| 444 | return("Unknown color"); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | /* Get the default tty name. Caller must free the return */ |
|---|
| 448 | char *owl_util_get_default_tty(void) |
|---|
| 449 | { |
|---|
| 450 | const char *tmp; |
|---|
| 451 | char *out; |
|---|
| 452 | |
|---|
| 453 | if (getenv("DISPLAY")) { |
|---|
| 454 | out=owl_strdup(getenv("DISPLAY")); |
|---|
| 455 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
|---|
| 456 | out=owl_strdup(tmp); |
|---|
| 457 | if (!strncmp(out, "/dev/", 5)) { |
|---|
| 458 | owl_free(out); |
|---|
| 459 | out=owl_strdup(tmp+5); |
|---|
| 460 | } |
|---|
| 461 | } else { |
|---|
| 462 | out=owl_strdup("unknown"); |
|---|
| 463 | } |
|---|
| 464 | return(out); |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | /* strip leading and trailing new lines. Caller must free the |
|---|
| 468 | * return. |
|---|
| 469 | */ |
|---|
| 470 | char *owl_util_stripnewlines(const char *in) |
|---|
| 471 | { |
|---|
| 472 | |
|---|
| 473 | char *tmp, *ptr1, *ptr2, *out; |
|---|
| 474 | |
|---|
| 475 | ptr1=tmp=owl_strdup(in); |
|---|
| 476 | while (ptr1[0]=='\n') { |
|---|
| 477 | ptr1++; |
|---|
| 478 | } |
|---|
| 479 | ptr2=ptr1+strlen(ptr1)-1; |
|---|
| 480 | while (ptr2>ptr1 && ptr2[0]=='\n') { |
|---|
| 481 | ptr2[0]='\0'; |
|---|
| 482 | ptr2--; |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | out=owl_strdup(ptr1); |
|---|
| 486 | owl_free(tmp); |
|---|
| 487 | return(out); |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | /* Delete all lines matching "line" from the named file. If no such |
|---|
| 491 | * line is found the file is left intact. If backup==1 then leave a |
|---|
| 492 | * backup file containing the original contents. The match is |
|---|
| 493 | * case-insensitive. |
|---|
| 494 | * |
|---|
| 495 | * Returns the number of lines removed |
|---|
| 496 | */ |
|---|
| 497 | int owl_util_file_deleteline(const char *filename, const char *line, int backup) |
|---|
| 498 | { |
|---|
| 499 | char *backupfile, *newfile, *buf = NULL; |
|---|
| 500 | FILE *old, *new; |
|---|
| 501 | struct stat st; |
|---|
| 502 | int numremoved = 0; |
|---|
| 503 | |
|---|
| 504 | if ((old = fopen(filename, "r")) == NULL) { |
|---|
| 505 | owl_function_error("Cannot open %s (for reading): %s", |
|---|
| 506 | filename, strerror(errno)); |
|---|
| 507 | return 0; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | if (fstat(fileno(old), &st) != 0) { |
|---|
| 511 | owl_function_error("Cannot stat %s: %s", filename, strerror(errno)); |
|---|
| 512 | return 0; |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | newfile = owl_sprintf("%s.new", filename); |
|---|
| 516 | if ((new = fopen(newfile, "w")) == NULL) { |
|---|
| 517 | owl_function_error("Cannot open %s (for writing): %s", |
|---|
| 518 | filename, strerror(errno)); |
|---|
| 519 | free(newfile); |
|---|
| 520 | fclose(old); |
|---|
| 521 | return 0; |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | if (fchmod(fileno(new), st.st_mode & 0777) != 0) { |
|---|
| 525 | owl_function_error("Cannot set permissions on %s: %s", |
|---|
| 526 | filename, strerror(errno)); |
|---|
| 527 | unlink(newfile); |
|---|
| 528 | fclose(new); |
|---|
| 529 | free(newfile); |
|---|
| 530 | fclose(old); |
|---|
| 531 | return 0; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | while (owl_getline_chomp(&buf, old)) |
|---|
| 535 | if (strcasecmp(buf, line) != 0) |
|---|
| 536 | fprintf(new, "%s\n", buf); |
|---|
| 537 | else |
|---|
| 538 | numremoved++; |
|---|
| 539 | owl_free(buf); |
|---|
| 540 | |
|---|
| 541 | fclose(new); |
|---|
| 542 | fclose(old); |
|---|
| 543 | |
|---|
| 544 | if (backup) { |
|---|
| 545 | backupfile = owl_sprintf("%s.backup", filename); |
|---|
| 546 | unlink(backupfile); |
|---|
| 547 | if (link(filename, backupfile) != 0) { |
|---|
| 548 | owl_function_error("Cannot link %s: %s", backupfile, strerror(errno)); |
|---|
| 549 | owl_free(backupfile); |
|---|
| 550 | unlink(newfile); |
|---|
| 551 | owl_free(newfile); |
|---|
| 552 | return 0; |
|---|
| 553 | } |
|---|
| 554 | owl_free(backupfile); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | if (rename(newfile, filename) != 0) { |
|---|
| 558 | owl_function_error("Cannot move %s to %s: %s", |
|---|
| 559 | newfile, filename, strerror(errno)); |
|---|
| 560 | numremoved = 0; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | unlink(newfile); |
|---|
| 564 | owl_free(newfile); |
|---|
| 565 | |
|---|
| 566 | return numremoved; |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | int owl_util_max(int a, int b) |
|---|
| 570 | { |
|---|
| 571 | if (a>b) return(a); |
|---|
| 572 | return(b); |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | int owl_util_min(int a, int b) |
|---|
| 576 | { |
|---|
| 577 | if (a<b) return(a); |
|---|
| 578 | return(b); |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | /* Return the base class or instance from a zephyr class, by removing |
|---|
| 582 | leading `un' or trailing `.d'. |
|---|
| 583 | The caller is responsible for freeing the allocated string. |
|---|
| 584 | */ |
|---|
| 585 | char * owl_util_baseclass(const char * class) |
|---|
| 586 | { |
|---|
| 587 | char *start, *end; |
|---|
| 588 | |
|---|
| 589 | while(!strncmp(class, "un", 2)) { |
|---|
| 590 | class += 2; |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | start = owl_strdup(class); |
|---|
| 594 | end = start + strlen(start) - 1; |
|---|
| 595 | while(end > start && *end == 'd' && *(end-1) == '.') { |
|---|
| 596 | end -= 2; |
|---|
| 597 | } |
|---|
| 598 | *(end + 1) = 0; |
|---|
| 599 | |
|---|
| 600 | return start; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | const char * owl_get_datadir(void) |
|---|
| 604 | { |
|---|
| 605 | const char * datadir = getenv("BARNOWL_DATA_DIR"); |
|---|
| 606 | if(datadir != NULL) |
|---|
| 607 | return datadir; |
|---|
| 608 | return DATADIR; |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | /* Strips format characters from a valid utf-8 string. Returns the |
|---|
| 612 | empty string if 'in' does not validate. */ |
|---|
| 613 | char * owl_strip_format_chars(const char *in) |
|---|
| 614 | { |
|---|
| 615 | char *r; |
|---|
| 616 | if (g_utf8_validate(in, -1, NULL)) { |
|---|
| 617 | const char *s, *p; |
|---|
| 618 | r = owl_malloc(strlen(in)+1); |
|---|
| 619 | r[0] = '\0'; |
|---|
| 620 | s = in; |
|---|
| 621 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 622 | while(p) { |
|---|
| 623 | /* If it's a format character, copy up to it, and skip all |
|---|
| 624 | immediately following format characters. */ |
|---|
| 625 | if (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
|---|
| 626 | strncat(r, s, p-s); |
|---|
| 627 | p = g_utf8_next_char(p); |
|---|
| 628 | while (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
|---|
| 629 | p = g_utf8_next_char(p); |
|---|
| 630 | } |
|---|
| 631 | s = p; |
|---|
| 632 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 633 | } |
|---|
| 634 | else { |
|---|
| 635 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 636 | } |
|---|
| 637 | } |
|---|
| 638 | if (s) strcat(r,s); |
|---|
| 639 | } |
|---|
| 640 | else { |
|---|
| 641 | r = owl_strdup(""); |
|---|
| 642 | } |
|---|
| 643 | return r; |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | /* If in is not UTF-8, convert from ISO-8859-1. We may want to allow |
|---|
| 647 | * the caller to specify an alternative in the future. We also strip |
|---|
| 648 | * out characters in Unicode Plane 16, as we use that plane internally |
|---|
| 649 | * for formatting. |
|---|
| 650 | */ |
|---|
| 651 | char * owl_validate_or_convert(const char *in) |
|---|
| 652 | { |
|---|
| 653 | if (g_utf8_validate(in, -1, NULL)) { |
|---|
| 654 | return owl_strip_format_chars(in); |
|---|
| 655 | } |
|---|
| 656 | else { |
|---|
| 657 | return g_convert(in, -1, |
|---|
| 658 | "UTF-8", "ISO-8859-1", |
|---|
| 659 | NULL, NULL, NULL); |
|---|
| 660 | } |
|---|
| 661 | } |
|---|
| 662 | /* |
|---|
| 663 | * Validate 'in' as UTF-8, and either return a copy of it, or an empty |
|---|
| 664 | * string if it is invalid utf-8. |
|---|
| 665 | */ |
|---|
| 666 | char * owl_validate_utf8(const char *in) |
|---|
| 667 | { |
|---|
| 668 | char *out; |
|---|
| 669 | if (g_utf8_validate(in, -1, NULL)) { |
|---|
| 670 | out = owl_strdup(in); |
|---|
| 671 | } else { |
|---|
| 672 | out = owl_strdup(""); |
|---|
| 673 | } |
|---|
| 674 | return out; |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | /* This is based on _extract() and _isCJ() from perl's Text::WrapI18N */ |
|---|
| 678 | int owl_util_can_break_after(gunichar c) |
|---|
| 679 | { |
|---|
| 680 | |
|---|
| 681 | if (c == ' ') return 1; |
|---|
| 682 | if (c >= 0x3000 && c <= 0x312f) { |
|---|
| 683 | /* CJK punctuations, Hiragana, Katakana, Bopomofo */ |
|---|
| 684 | if (c == 0x300a || c == 0x300c || c == 0x300e || |
|---|
| 685 | c == 0x3010 || c == 0x3014 || c == 0x3016 || |
|---|
| 686 | c == 0x3018 || c == 0x301a) |
|---|
| 687 | return 0; |
|---|
| 688 | return 1; |
|---|
| 689 | } |
|---|
| 690 | if (c >= 0x31a0 && c <= 0x31bf) {return 1;} /* Bopomofo */ |
|---|
| 691 | if (c >= 0x31f0 && c <= 0x31ff) {return 1;} /* Katakana extension */ |
|---|
| 692 | if (c >= 0x3400 && c <= 0x9fff) {return 1;} /* Han Ideogram */ |
|---|
| 693 | if (c >= 0xf900 && c <= 0xfaff) {return 1;} /* Han Ideogram */ |
|---|
| 694 | if (c >= 0x20000 && c <= 0x2ffff) {return 1;} /* Han Ideogram */ |
|---|
| 695 | return 0; |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | char *owl_escape_highbit(const char *str) |
|---|
| 699 | { |
|---|
| 700 | GString *out = g_string_new(""); |
|---|
| 701 | unsigned char c; |
|---|
| 702 | while((c = (*str++))) { |
|---|
| 703 | if(c == '\\') { |
|---|
| 704 | g_string_append(out, "\\\\"); |
|---|
| 705 | } else if(c & 0x80) { |
|---|
| 706 | g_string_append_printf(out, "\\x%02x", (int)c); |
|---|
| 707 | } else { |
|---|
| 708 | g_string_append_c(out, c); |
|---|
| 709 | } |
|---|
| 710 | } |
|---|
| 711 | return g_string_free(out, 0); |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | /* innards of owl_getline{,_chomp} below */ |
|---|
| 715 | static int owl_getline_internal(char **s, FILE *fp, int newline) |
|---|
| 716 | { |
|---|
| 717 | int size = 0; |
|---|
| 718 | int target = 0; |
|---|
| 719 | int count = 0; |
|---|
| 720 | int c; |
|---|
| 721 | |
|---|
| 722 | while (1) { |
|---|
| 723 | c = getc(fp); |
|---|
| 724 | if ((target + 1) > size) { |
|---|
| 725 | size += BUFSIZ; |
|---|
| 726 | *s = owl_realloc(*s, size); |
|---|
| 727 | } |
|---|
| 728 | if (c == EOF) |
|---|
| 729 | break; |
|---|
| 730 | count++; |
|---|
| 731 | if (c != '\n' || newline) |
|---|
| 732 | (*s)[target++] = c; |
|---|
| 733 | if (c == '\n') |
|---|
| 734 | break; |
|---|
| 735 | } |
|---|
| 736 | (*s)[target] = 0; |
|---|
| 737 | |
|---|
| 738 | return count; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | /* Read a line from fp, allocating memory to hold it, returning the number of |
|---|
| 742 | * byte read. *s should either be NULL or a pointer to memory allocated with |
|---|
| 743 | * owl_malloc; it will be owl_realloc'd as appropriate. The caller must |
|---|
| 744 | * eventually free it. (This is roughly the interface of getline in the gnu |
|---|
| 745 | * libc). |
|---|
| 746 | * |
|---|
| 747 | * The final newline will be included if it's there. |
|---|
| 748 | */ |
|---|
| 749 | int owl_getline(char **s, FILE *fp) |
|---|
| 750 | { |
|---|
| 751 | return owl_getline_internal(s, fp, 1); |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | /* As above, but omitting the final newline */ |
|---|
| 755 | int owl_getline_chomp(char **s, FILE *fp) |
|---|
| 756 | { |
|---|
| 757 | return owl_getline_internal(s, fp, 0); |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | /* Read the rest of the input available in fp into a string. */ |
|---|
| 761 | char *owl_slurp(FILE *fp) |
|---|
| 762 | { |
|---|
| 763 | char *buf = NULL; |
|---|
| 764 | char *p; |
|---|
| 765 | int size = 0; |
|---|
| 766 | int count; |
|---|
| 767 | |
|---|
| 768 | while (1) { |
|---|
| 769 | buf = owl_realloc(buf, size + BUFSIZ); |
|---|
| 770 | p = &buf[size]; |
|---|
| 771 | size += BUFSIZ; |
|---|
| 772 | |
|---|
| 773 | if ((count = fread(p, 1, BUFSIZ, fp)) < BUFSIZ) |
|---|
| 774 | break; |
|---|
| 775 | } |
|---|
| 776 | p[count] = 0; |
|---|
| 777 | |
|---|
| 778 | return buf; |
|---|
| 779 | } |
|---|