[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> |
---|
[435001d] | 7 | #include <sys/stat.h> |
---|
| 8 | #include <sys/types.h> |
---|
[7d4fbcd] | 9 | |
---|
[e19eb97] | 10 | void sepbar(const char *in) |
---|
[e4eebe8] | 11 | { |
---|
[7d4fbcd] | 12 | WINDOW *sepwin; |
---|
[3eb599d] | 13 | const owl_messagelist *ml; |
---|
[9e5c9f3] | 14 | const owl_view *v; |
---|
[7d4fbcd] | 15 | int x, y, i; |
---|
[e19eb97] | 16 | const char *foo, *appendtosepbar; |
---|
[7d4fbcd] | 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); |
---|
[c15bbfb] | 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 | } |
---|
[7d4fbcd] | 29 | |
---|
[73624b4] | 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 | |
---|
[96c3265] | 36 | wmove(sepwin, 0, 2); |
---|
[7d4fbcd] | 37 | |
---|
[96c3265] | 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)); |
---|
[7d4fbcd] | 44 | |
---|
| 45 | foo=owl_view_get_filtname(v); |
---|
[96c3265] | 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); |
---|
[7d4fbcd] | 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); |
---|
[96c3265] | 73 | wprintw(sepwin, " right: %i ", owl_global_get_rightshift(&g)); |
---|
[7d4fbcd] | 74 | } |
---|
| 75 | |
---|
[4b660cc] | 76 | if (owl_global_is_zaway(&g) || owl_global_is_aaway(&g)) { |
---|
[7d4fbcd] | 77 | getyx(sepwin, y, x); |
---|
| 78 | wmove(sepwin, y, x+2); |
---|
| 79 | wattron(sepwin, A_BOLD); |
---|
| 80 | wattroff(sepwin, A_REVERSE); |
---|
[4b660cc] | 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 | } |
---|
[7d4fbcd] | 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 | |
---|
[e19eb97] | 124 | char **atokenize(const char *buffer, const char *sep, int *i) |
---|
[e4eebe8] | 125 | { |
---|
[7d4fbcd] | 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 | |
---|
[46d940a] | 131 | workbuff = owl_strdup(buffer); |
---|
[7d4fbcd] | 132 | |
---|
| 133 | args=NULL; |
---|
| 134 | while (!done) { |
---|
| 135 | if (first) { |
---|
| 136 | first=0; |
---|
[4d86e06] | 137 | foo=strtok(workbuff, sep); |
---|
[7d4fbcd] | 138 | } else { |
---|
[4d86e06] | 139 | foo=strtok(NULL, sep); |
---|
[7d4fbcd] | 140 | } |
---|
| 141 | if (foo==NULL) { |
---|
| 142 | done=1; |
---|
| 143 | } else { |
---|
[4d86e06] | 144 | args=owl_realloc(args, sizeof(char *) * (count+1)); |
---|
[36486be] | 145 | args[count] = owl_strdup(foo); |
---|
[7d4fbcd] | 146 | count++; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | *i=count; |
---|
| 150 | owl_free(workbuff); |
---|
| 151 | return(args); |
---|
| 152 | } |
---|
| 153 | |
---|
[e19eb97] | 154 | const char *skiptokens(const char *buff, int n) { |
---|
[e30ed92] | 155 | /* skips n tokens and returns where that would be. */ |
---|
| 156 | char quote = 0; |
---|
[7d4fbcd] | 157 | while (*buff && n>0) { |
---|
| 158 | while (*buff == ' ') buff++; |
---|
[e30ed92] | 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++; |
---|
[7d4fbcd] | 166 | } |
---|
| 167 | while (*buff == ' ') buff++; |
---|
| 168 | n--; |
---|
| 169 | } |
---|
| 170 | return buff; |
---|
| 171 | } |
---|
| 172 | |
---|
[f36222f] | 173 | /* Return a "nice" version of the path. Tilde expansion is done, and |
---|
| 174 | * duplicate slashes are removed. Caller must free the return. |
---|
| 175 | */ |
---|
[e19eb97] | 176 | char *owl_util_makepath(const char *in) |
---|
[f36222f] | 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) { |
---|
[8766d44] | 215 | out[x]=in[i]; |
---|
[f36222f] | 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 | |
---|
[1672650] | 239 | void atokenize_delete(char **tok, int nels) |
---|
[e4eebe8] | 240 | { |
---|
[7d4fbcd] | 241 | int i; |
---|
| 242 | for (i=0; i<nels; i++) { |
---|
| 243 | owl_free(tok[i]); |
---|
| 244 | } |
---|
| 245 | owl_free(tok); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[40d22cf] | 249 | void owl_parse_delete(char **argv, int argc) |
---|
[e4eebe8] | 250 | { |
---|
[7d4fbcd] | 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 | |
---|
[e19eb97] | 261 | char **owl_parseline(const char *line, int *argc) |
---|
[e4eebe8] | 262 | { |
---|
[7d4fbcd] | 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)); |
---|
[36486be] | 322 | argv[*argc] = owl_strdup(curarg); |
---|
[7d4fbcd] | 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 | |
---|
[d524c83] | 334 | owl_free(curarg); |
---|
[95caa16] | 335 | |
---|
[7d4fbcd] | 336 | /* check for unbalanced quotes */ |
---|
| 337 | if (quote!='\0') { |
---|
[40d22cf] | 338 | owl_parse_delete(argv, *argc); |
---|
[7d4fbcd] | 339 | *argc=-1; |
---|
| 340 | return(NULL); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | return(argv); |
---|
| 344 | } |
---|
| 345 | |
---|
[de03334] | 346 | /* caller must free the return */ |
---|
[5b85d19] | 347 | char *owl_util_minutes_to_timestr(int in) |
---|
[de03334] | 348 | { |
---|
[f1e629d] | 349 | int days, hours; |
---|
[de03334] | 350 | long run; |
---|
| 351 | char *out; |
---|
| 352 | |
---|
[5b85d19] | 353 | run=in; |
---|
[de03334] | 354 | |
---|
[5b85d19] | 355 | days=run/1440; |
---|
| 356 | run-=days*1440; |
---|
| 357 | hours=run/60; |
---|
| 358 | run-=hours*60; |
---|
[de03334] | 359 | |
---|
| 360 | if (days>0) { |
---|
[6eaf35b] | 361 | out=owl_sprintf("%i d %2.2i:%2.2li", days, hours, run); |
---|
[de03334] | 362 | } else { |
---|
[6eaf35b] | 363 | out=owl_sprintf(" %2.2i:%2.2li", hours, run); |
---|
[de03334] | 364 | } |
---|
| 365 | return(out); |
---|
| 366 | } |
---|
| 367 | |
---|
[42abb10] | 368 | /* hooks for doing memory allocation et. al. in owl */ |
---|
| 369 | |
---|
[e4eebe8] | 370 | void *owl_malloc(size_t size) |
---|
| 371 | { |
---|
[34509d5] | 372 | return(g_malloc(size)); |
---|
[7d4fbcd] | 373 | } |
---|
| 374 | |
---|
[e4eebe8] | 375 | void owl_free(void *ptr) |
---|
| 376 | { |
---|
[34509d5] | 377 | g_free(ptr); |
---|
[7d4fbcd] | 378 | } |
---|
| 379 | |
---|
[e4eebe8] | 380 | char *owl_strdup(const char *s1) |
---|
| 381 | { |
---|
[34509d5] | 382 | return(g_strdup(s1)); |
---|
[7d4fbcd] | 383 | } |
---|
| 384 | |
---|
[e4eebe8] | 385 | void *owl_realloc(void *ptr, size_t size) |
---|
| 386 | { |
---|
[34509d5] | 387 | return(g_realloc(ptr, size)); |
---|
[7d4fbcd] | 388 | } |
---|
| 389 | |
---|
[e4eebe8] | 390 | /* allocates memory and returns the string or null. |
---|
| 391 | * caller must free the string. |
---|
| 392 | */ |
---|
| 393 | char *owl_sprintf(const char *fmt, ...) |
---|
| 394 | { |
---|
[1c6c4d3] | 395 | va_list ap; |
---|
[28ee32b] | 396 | char *ret = NULL; |
---|
| 397 | va_start(ap, fmt); |
---|
| 398 | ret = g_strdup_vprintf(fmt, ap); |
---|
| 399 | va_end(ap); |
---|
| 400 | return ret; |
---|
[1c6c4d3] | 401 | } |
---|
| 402 | |
---|
[a3e61a2] | 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 | }; |
---|
[28ee32b] | 419 | |
---|
[12c35df] | 420 | /* Return the owl color associated with the named color. Return -1 |
---|
| 421 | * if the named color is not available |
---|
| 422 | */ |
---|
[e19eb97] | 423 | int owl_util_string_to_color(const char *color) |
---|
[e4eebe8] | 424 | { |
---|
[a3e61a2] | 425 | int c, i; |
---|
[1b9d3cc] | 426 | char *p; |
---|
[a3e61a2] | 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 | |
---|
[1b9d3cc] | 432 | c = strtol(color, &p, 10); |
---|
| 433 | if (p != color && c >= -1 && c < COLORS) { |
---|
[c2c5c77] | 434 | return(c); |
---|
| 435 | } |
---|
[601733d] | 436 | return(OWL_COLOR_INVALID); |
---|
[7d4fbcd] | 437 | } |
---|
| 438 | |
---|
[e4eebe8] | 439 | /* Return a string name of the given owl color */ |
---|
[e19eb97] | 440 | const char *owl_util_color_to_string(int color) |
---|
[e4eebe8] | 441 | { |
---|
[a3e61a2] | 442 | if (color >= OWL_COLOR_INVALID && color <= OWL_COLOR_WHITE) |
---|
| 443 | return color_map[color - OWL_COLOR_INVALID].name; |
---|
[7d4fbcd] | 444 | return("Unknown color"); |
---|
| 445 | } |
---|
[e1c4636] | 446 | |
---|
[e4eebe8] | 447 | /* Get the default tty name. Caller must free the return */ |
---|
[c79a047] | 448 | char *owl_util_get_default_tty(void) |
---|
[e4eebe8] | 449 | { |
---|
[e19eb97] | 450 | const char *tmp; |
---|
[65b2173] | 451 | char *out; |
---|
[61e79a9] | 452 | |
---|
| 453 | if (getenv("DISPLAY")) { |
---|
| 454 | out=owl_strdup(getenv("DISPLAY")); |
---|
[5145235] | 455 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
---|
| 456 | out=owl_strdup(tmp); |
---|
[61e79a9] | 457 | if (!strncmp(out, "/dev/", 5)) { |
---|
| 458 | owl_free(out); |
---|
[5145235] | 459 | out=owl_strdup(tmp+5); |
---|
[61e79a9] | 460 | } |
---|
| 461 | } else { |
---|
[5145235] | 462 | out=owl_strdup("unknown"); |
---|
[61e79a9] | 463 | } |
---|
| 464 | return(out); |
---|
| 465 | } |
---|
| 466 | |
---|
[e4eebe8] | 467 | /* strip leading and trailing new lines. Caller must free the |
---|
| 468 | * return. |
---|
| 469 | */ |
---|
[e19eb97] | 470 | char *owl_util_stripnewlines(const char *in) |
---|
[e4eebe8] | 471 | { |
---|
[7e3e00a] | 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; |
---|
[1bb1e67] | 480 | while (ptr2>ptr1 && ptr2[0]=='\n') { |
---|
[7e3e00a] | 481 | ptr2[0]='\0'; |
---|
| 482 | ptr2--; |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | out=owl_strdup(ptr1); |
---|
| 486 | owl_free(tmp); |
---|
| 487 | return(out); |
---|
| 488 | } |
---|
| 489 | |
---|
[946058b] | 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. |
---|
[da60ba9] | 494 | * |
---|
| 495 | * Returns the number of lines removed |
---|
[38cf544c] | 496 | */ |
---|
[da60ba9] | 497 | int owl_util_file_deleteline(const char *filename, const char *line, int backup) |
---|
[38cf544c] | 498 | { |
---|
[946058b] | 499 | char *backupfile, *newfile, *buf = NULL; |
---|
| 500 | FILE *old, *new; |
---|
| 501 | struct stat st; |
---|
[da60ba9] | 502 | int numremoved = 0; |
---|
[38cf544c] | 503 | |
---|
[946058b] | 504 | if ((old = fopen(filename, "r")) == NULL) { |
---|
| 505 | owl_function_error("Cannot open %s (for reading): %s", |
---|
| 506 | filename, strerror(errno)); |
---|
[da60ba9] | 507 | return 0; |
---|
[38cf544c] | 508 | } |
---|
[e6449bc] | 509 | |
---|
[946058b] | 510 | if (fstat(fileno(old), &st) != 0) { |
---|
| 511 | owl_function_error("Cannot stat %s: %s", filename, strerror(errno)); |
---|
| 512 | return 0; |
---|
[38cf544c] | 513 | } |
---|
| 514 | |
---|
[946058b] | 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 |
---|
[da60ba9] | 538 | numremoved++; |
---|
[a61daae] | 539 | owl_free(buf); |
---|
[38cf544c] | 540 | |
---|
[946058b] | 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; |
---|
[6a50af2] | 553 | } |
---|
[946058b] | 554 | owl_free(backupfile); |
---|
[38cf544c] | 555 | } |
---|
| 556 | |
---|
[946058b] | 557 | if (rename(newfile, filename) != 0) { |
---|
| 558 | owl_function_error("Cannot move %s to %s: %s", |
---|
| 559 | newfile, filename, strerror(errno)); |
---|
| 560 | numremoved = 0; |
---|
[38cf544c] | 561 | } |
---|
| 562 | |
---|
[946058b] | 563 | unlink(newfile); |
---|
| 564 | owl_free(newfile); |
---|
[da60ba9] | 565 | |
---|
| 566 | return numremoved; |
---|
[38cf544c] | 567 | } |
---|
| 568 | |
---|
[fe67f1f] | 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 | |
---|
[7a20e4c] | 581 | /* Return the base class or instance from a zephyr class, by removing |
---|
| 582 | leading `un' or trailing `.d'. |
---|
[be5aa09] | 583 | The caller is responsible for freeing the allocated string. |
---|
[7a20e4c] | 584 | */ |
---|
[e19eb97] | 585 | char * owl_util_baseclass(const char * class) |
---|
[7a20e4c] | 586 | { |
---|
[59916e8] | 587 | char *start, *end; |
---|
| 588 | |
---|
[fa4562c] | 589 | while(!strncmp(class, "un", 2)) { |
---|
| 590 | class += 2; |
---|
[7a20e4c] | 591 | } |
---|
[f166580] | 592 | |
---|
[fa4562c] | 593 | start = owl_strdup(class); |
---|
[59916e8] | 594 | end = start + strlen(start) - 1; |
---|
[04166e9] | 595 | while(end > start && *end == 'd' && *(end-1) == '.') { |
---|
[7a20e4c] | 596 | end -= 2; |
---|
| 597 | } |
---|
| 598 | *(end + 1) = 0; |
---|
[59916e8] | 599 | |
---|
[f166580] | 600 | return start; |
---|
[7a20e4c] | 601 | } |
---|
| 602 | |
---|
[c79a047] | 603 | const char * owl_get_datadir(void) |
---|
[5376a95] | 604 | { |
---|
[e19eb97] | 605 | const char * datadir = getenv("BARNOWL_DATA_DIR"); |
---|
[5376a95] | 606 | if(datadir != NULL) |
---|
[7bf51d5] | 607 | return datadir; |
---|
[5376a95] | 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. */ |
---|
[7f6a8a2] | 613 | char * owl_strip_format_chars(const char *in) |
---|
[5376a95] | 614 | { |
---|
| 615 | char *r; |
---|
| 616 | if (g_utf8_validate(in, -1, NULL)) { |
---|
[7f6a8a2] | 617 | const char *s, *p; |
---|
[5376a95] | 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. */ |
---|
[c1522ec] | 625 | if (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
[5376a95] | 626 | strncat(r, s, p-s); |
---|
| 627 | p = g_utf8_next_char(p); |
---|
[f119757] | 628 | while (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
[5376a95] | 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 | */ |
---|
[7f6a8a2] | 651 | char * owl_validate_or_convert(const char *in) |
---|
[5376a95] | 652 | { |
---|
[6201646] | 653 | if (g_utf8_validate(in, -1, NULL)) { |
---|
[5376a95] | 654 | return owl_strip_format_chars(in); |
---|
| 655 | } |
---|
| 656 | else { |
---|
[6201646] | 657 | return g_convert(in, -1, |
---|
[5376a95] | 658 | "UTF-8", "ISO-8859-1", |
---|
| 659 | NULL, NULL, NULL); |
---|
| 660 | } |
---|
[89f5338] | 661 | } |
---|
[4b17a6c] | 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. |
---|
[7b1d048] | 665 | */ |
---|
[7f6a8a2] | 666 | char * owl_validate_utf8(const char *in) |
---|
[7b1d048] | 667 | { |
---|
| 668 | char *out; |
---|
| 669 | if (g_utf8_validate(in, -1, NULL)) { |
---|
[4b17a6c] | 670 | out = owl_strdup(in); |
---|
| 671 | } else { |
---|
[7b1d048] | 672 | out = owl_strdup(""); |
---|
| 673 | } |
---|
| 674 | return out; |
---|
| 675 | } |
---|
[89f5338] | 676 | |
---|
[84027015] | 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 | } |
---|
[eea72a13] | 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 | } |
---|
[6ace255] | 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 | } |
---|