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