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