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