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