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