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