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