1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <ctype.h> |
---|
6 | #include <pwd.h> |
---|
7 | #include <sys/stat.h> |
---|
8 | #include <sys/types.h> |
---|
9 | #include <assert.h> |
---|
10 | #include <glib.h> |
---|
11 | #include <glib/gstdio.h> |
---|
12 | #include <glib-object.h> |
---|
13 | |
---|
14 | const char *skiptokens(const char *buff, int n) { |
---|
15 | /* skips n tokens and returns where that would be. */ |
---|
16 | char quote = 0; |
---|
17 | while (*buff && n>0) { |
---|
18 | while (*buff == ' ') buff++; |
---|
19 | while (*buff && (quote || *buff != ' ')) { |
---|
20 | if(quote) { |
---|
21 | if(*buff == quote) quote = 0; |
---|
22 | } else if(*buff == '"' || *buff == '\'') { |
---|
23 | quote = *buff; |
---|
24 | } |
---|
25 | buff++; |
---|
26 | } |
---|
27 | while (*buff == ' ') buff++; |
---|
28 | n--; |
---|
29 | } |
---|
30 | return buff; |
---|
31 | } |
---|
32 | |
---|
33 | /* Return a "nice" version of the path. Tilde expansion is done, and |
---|
34 | * duplicate slashes are removed. Caller must free the return. |
---|
35 | */ |
---|
36 | char *owl_util_makepath(const char *in) |
---|
37 | { |
---|
38 | int i, j, x; |
---|
39 | char *out, user[MAXPATHLEN]; |
---|
40 | struct passwd *pw; |
---|
41 | |
---|
42 | out=owl_malloc(MAXPATHLEN+1); |
---|
43 | out[0]='\0'; |
---|
44 | j=strlen(in); |
---|
45 | x=0; |
---|
46 | for (i=0; i<j; i++) { |
---|
47 | if (in[i]=='~') { |
---|
48 | if ( (i==(j-1)) || /* last character */ |
---|
49 | (in[i+1]=='/') ) { /* ~/ */ |
---|
50 | /* use my homedir */ |
---|
51 | pw=getpwuid(getuid()); |
---|
52 | if (!pw) { |
---|
53 | out[x]=in[i]; |
---|
54 | } else { |
---|
55 | out[x]='\0'; |
---|
56 | strcat(out, pw->pw_dir); |
---|
57 | x+=strlen(pw->pw_dir); |
---|
58 | } |
---|
59 | } else { |
---|
60 | /* another user homedir */ |
---|
61 | int a, b; |
---|
62 | b=0; |
---|
63 | for (a=i+1; i<j; a++) { |
---|
64 | if (in[a]==' ' || in[a]=='/') { |
---|
65 | break; |
---|
66 | } else { |
---|
67 | user[b]=in[a]; |
---|
68 | i++; |
---|
69 | b++; |
---|
70 | } |
---|
71 | } |
---|
72 | user[b]='\0'; |
---|
73 | pw=getpwnam(user); |
---|
74 | if (!pw) { |
---|
75 | out[x]=in[i]; |
---|
76 | } else { |
---|
77 | out[x]='\0'; |
---|
78 | strcat(out, pw->pw_dir); |
---|
79 | x+=strlen(pw->pw_dir); |
---|
80 | } |
---|
81 | } |
---|
82 | } else if (in[i]=='/') { |
---|
83 | /* check for a double / */ |
---|
84 | if (i<(j-1) && (in[i+1]=='/')) { |
---|
85 | /* do nothing */ |
---|
86 | } else { |
---|
87 | out[x]=in[i]; |
---|
88 | x++; |
---|
89 | } |
---|
90 | } else { |
---|
91 | out[x]=in[i]; |
---|
92 | x++; |
---|
93 | } |
---|
94 | } |
---|
95 | out[x]='\0'; |
---|
96 | return(out); |
---|
97 | } |
---|
98 | |
---|
99 | void owl_parse_delete(char **argv, int argc) |
---|
100 | { |
---|
101 | int i; |
---|
102 | |
---|
103 | if (!argv) return; |
---|
104 | |
---|
105 | for (i=0; i<argc; i++) { |
---|
106 | if (argv[i]) owl_free(argv[i]); |
---|
107 | } |
---|
108 | owl_free(argv); |
---|
109 | } |
---|
110 | |
---|
111 | char **owl_parseline(const char *line, int *argc) |
---|
112 | { |
---|
113 | /* break a command line up into argv, argc. The caller must free |
---|
114 | the returned values. If there is an error argc will be set to |
---|
115 | -1, argv will be NULL and the caller does not need to free |
---|
116 | anything */ |
---|
117 | |
---|
118 | char **argv; |
---|
119 | int i, len, between=1; |
---|
120 | char *curarg; |
---|
121 | char quote; |
---|
122 | |
---|
123 | argv=owl_malloc(sizeof(char *)); |
---|
124 | len=strlen(line); |
---|
125 | curarg=owl_malloc(len+10); |
---|
126 | strcpy(curarg, ""); |
---|
127 | quote='\0'; |
---|
128 | *argc=0; |
---|
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 */ |
---|
156 | curarg[strlen(curarg)+1]='\0'; |
---|
157 | curarg[strlen(curarg)]=line[i]; |
---|
158 | continue; |
---|
159 | } |
---|
160 | |
---|
161 | /* if it's not a space or end of command, then use it */ |
---|
162 | if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') { |
---|
163 | curarg[strlen(curarg)+1]='\0'; |
---|
164 | curarg[strlen(curarg)]=line[i]; |
---|
165 | continue; |
---|
166 | } |
---|
167 | |
---|
168 | /* otherwise, if we're not in quotes, add the whole argument */ |
---|
169 | if (quote=='\0') { |
---|
170 | /* add the argument */ |
---|
171 | argv=owl_realloc(argv, sizeof(char *)*((*argc)+1)); |
---|
172 | argv[*argc] = owl_strdup(curarg); |
---|
173 | *argc=*argc+1; |
---|
174 | strcpy(curarg, ""); |
---|
175 | between=1; |
---|
176 | continue; |
---|
177 | } |
---|
178 | |
---|
179 | /* if it is a space and we're in quotes, then use it */ |
---|
180 | curarg[strlen(curarg)+1]='\0'; |
---|
181 | curarg[strlen(curarg)]=line[i]; |
---|
182 | } |
---|
183 | |
---|
184 | owl_free(curarg); |
---|
185 | |
---|
186 | /* check for unbalanced quotes */ |
---|
187 | if (quote!='\0') { |
---|
188 | owl_parse_delete(argv, *argc); |
---|
189 | *argc=-1; |
---|
190 | return(NULL); |
---|
191 | } |
---|
192 | |
---|
193 | return(argv); |
---|
194 | } |
---|
195 | |
---|
196 | /* Appends a quoted version of arg suitable for placing in a |
---|
197 | * command-line to a GString. Does not append a space. */ |
---|
198 | void owl_string_append_quoted_arg(GString *buf, const char *arg) |
---|
199 | { |
---|
200 | const char *argp; |
---|
201 | if (arg[0] == '\0') { |
---|
202 | /* Quote the empty string. */ |
---|
203 | g_string_append(buf, "''"); |
---|
204 | } else if (arg[strcspn(arg, "'\" \n\t")] == '\0') { |
---|
205 | /* If there are no nasty characters, return as-is. */ |
---|
206 | g_string_append(buf, arg); |
---|
207 | } else if (!strchr(arg, '\'')) { |
---|
208 | /* Single-quote if possible. */ |
---|
209 | g_string_append_c(buf, '\''); |
---|
210 | g_string_append(buf, arg); |
---|
211 | g_string_append_c(buf, '\''); |
---|
212 | } else { |
---|
213 | /* Nasty case: double-quote, but change all internal "s to "'"'" |
---|
214 | * so that they are single-quoted because we're too cool for |
---|
215 | * backslashes. |
---|
216 | */ |
---|
217 | g_string_append_c(buf, '"'); |
---|
218 | for (argp = arg; *argp; argp++) { |
---|
219 | if (*argp == '"') |
---|
220 | g_string_append(buf, "\"'\"'\""); |
---|
221 | else |
---|
222 | g_string_append_c(buf, *argp); |
---|
223 | } |
---|
224 | g_string_append_c(buf, '"'); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | /* Returns a quoted version of arg suitable for placing in a |
---|
229 | * command-line. Result should be freed with owl_free. */ |
---|
230 | char *owl_arg_quote(const char *arg) |
---|
231 | { |
---|
232 | GString *buf = g_string_new("");; |
---|
233 | owl_string_append_quoted_arg(buf, arg); |
---|
234 | return g_string_free(buf, false); |
---|
235 | } |
---|
236 | |
---|
237 | /* caller must free the return */ |
---|
238 | char *owl_util_minutes_to_timestr(int in) |
---|
239 | { |
---|
240 | int days, hours; |
---|
241 | long run; |
---|
242 | char *out; |
---|
243 | |
---|
244 | run=in; |
---|
245 | |
---|
246 | days=run/1440; |
---|
247 | run-=days*1440; |
---|
248 | hours=run/60; |
---|
249 | run-=hours*60; |
---|
250 | |
---|
251 | if (days>0) { |
---|
252 | out=owl_sprintf("%i d %2.2i:%2.2li", days, hours, run); |
---|
253 | } else { |
---|
254 | out=owl_sprintf(" %2.2i:%2.2li", hours, run); |
---|
255 | } |
---|
256 | return(out); |
---|
257 | } |
---|
258 | |
---|
259 | /* hooks for doing memory allocation et. al. in owl */ |
---|
260 | |
---|
261 | void *owl_malloc(size_t size) |
---|
262 | { |
---|
263 | return(g_malloc(size)); |
---|
264 | } |
---|
265 | |
---|
266 | void owl_free(void *ptr) |
---|
267 | { |
---|
268 | g_free(ptr); |
---|
269 | } |
---|
270 | |
---|
271 | char *owl_strdup(const char *s1) |
---|
272 | { |
---|
273 | return(g_strdup(s1)); |
---|
274 | } |
---|
275 | |
---|
276 | void *owl_realloc(void *ptr, size_t size) |
---|
277 | { |
---|
278 | return(g_realloc(ptr, size)); |
---|
279 | } |
---|
280 | |
---|
281 | /* allocates memory and returns the string or null. |
---|
282 | * caller must free the string. |
---|
283 | */ |
---|
284 | char *owl_sprintf(const char *fmt, ...) |
---|
285 | { |
---|
286 | va_list ap; |
---|
287 | char *ret = NULL; |
---|
288 | va_start(ap, fmt); |
---|
289 | ret = g_strdup_vprintf(fmt, ap); |
---|
290 | va_end(ap); |
---|
291 | return ret; |
---|
292 | } |
---|
293 | |
---|
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 | }; |
---|
310 | |
---|
311 | /* Return the owl color associated with the named color. Return -1 |
---|
312 | * if the named color is not available |
---|
313 | */ |
---|
314 | int owl_util_string_to_color(const char *color) |
---|
315 | { |
---|
316 | int c, i; |
---|
317 | char *p; |
---|
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 | |
---|
323 | c = strtol(color, &p, 10); |
---|
324 | if (p != color && c >= -1 && c < COLORS) { |
---|
325 | return(c); |
---|
326 | } |
---|
327 | return(OWL_COLOR_INVALID); |
---|
328 | } |
---|
329 | |
---|
330 | /* Return a string name of the given owl color */ |
---|
331 | const char *owl_util_color_to_string(int color) |
---|
332 | { |
---|
333 | if (color >= OWL_COLOR_INVALID && color <= OWL_COLOR_WHITE) |
---|
334 | return color_map[color - OWL_COLOR_INVALID].name; |
---|
335 | return("Unknown color"); |
---|
336 | } |
---|
337 | |
---|
338 | /* Get the default tty name. Caller must free the return */ |
---|
339 | char *owl_util_get_default_tty(void) |
---|
340 | { |
---|
341 | const char *tmp; |
---|
342 | char *out; |
---|
343 | |
---|
344 | if (getenv("DISPLAY")) { |
---|
345 | out=owl_strdup(getenv("DISPLAY")); |
---|
346 | } else if ((tmp=ttyname(fileno(stdout)))!=NULL) { |
---|
347 | out=owl_strdup(tmp); |
---|
348 | if (!strncmp(out, "/dev/", 5)) { |
---|
349 | owl_free(out); |
---|
350 | out=owl_strdup(tmp+5); |
---|
351 | } |
---|
352 | } else { |
---|
353 | out=owl_strdup("unknown"); |
---|
354 | } |
---|
355 | return(out); |
---|
356 | } |
---|
357 | |
---|
358 | /* strip leading and trailing new lines. Caller must free the |
---|
359 | * return. |
---|
360 | */ |
---|
361 | char *owl_util_stripnewlines(const char *in) |
---|
362 | { |
---|
363 | |
---|
364 | char *tmp, *ptr1, *ptr2, *out; |
---|
365 | |
---|
366 | ptr1=tmp=owl_strdup(in); |
---|
367 | while (ptr1[0]=='\n') { |
---|
368 | ptr1++; |
---|
369 | } |
---|
370 | ptr2=ptr1+strlen(ptr1)-1; |
---|
371 | while (ptr2>ptr1 && ptr2[0]=='\n') { |
---|
372 | ptr2[0]='\0'; |
---|
373 | ptr2--; |
---|
374 | } |
---|
375 | |
---|
376 | out=owl_strdup(ptr1); |
---|
377 | owl_free(tmp); |
---|
378 | return(out); |
---|
379 | } |
---|
380 | |
---|
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 | */ |
---|
392 | gchar *owl_util_recursive_resolve_link(const char *filename) |
---|
393 | { |
---|
394 | gchar *last_path = g_strdup(filename); |
---|
395 | GError *err = NULL; |
---|
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; |
---|
422 | } |
---|
423 | return last_path; |
---|
424 | } |
---|
425 | |
---|
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. |
---|
430 | * |
---|
431 | * Returns the number of lines removed on success. Returns -1 on failure. |
---|
432 | */ |
---|
433 | int owl_util_file_deleteline(const char *filename, const char *line, int backup) |
---|
434 | { |
---|
435 | char *backupfile, *newfile, *buf = NULL; |
---|
436 | gchar *actual_filename; /* gchar; we need to g_free it */ |
---|
437 | FILE *old, *new; |
---|
438 | struct stat st; |
---|
439 | int numremoved = 0; |
---|
440 | |
---|
441 | if ((old = fopen(filename, "r")) == NULL) { |
---|
442 | owl_function_error("Cannot open %s (for reading): %s", |
---|
443 | filename, strerror(errno)); |
---|
444 | return -1; |
---|
445 | } |
---|
446 | |
---|
447 | if (fstat(fileno(old), &st) != 0) { |
---|
448 | owl_function_error("Cannot stat %s: %s", filename, strerror(errno)); |
---|
449 | return -1; |
---|
450 | } |
---|
451 | |
---|
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 | |
---|
457 | newfile = owl_sprintf("%s.new", actual_filename); |
---|
458 | if ((new = fopen(newfile, "w")) == NULL) { |
---|
459 | owl_function_error("Cannot open %s (for writing): %s", |
---|
460 | actual_filename, strerror(errno)); |
---|
461 | owl_free(newfile); |
---|
462 | fclose(old); |
---|
463 | free(actual_filename); |
---|
464 | return -1; |
---|
465 | } |
---|
466 | |
---|
467 | if (fchmod(fileno(new), st.st_mode & 0777) != 0) { |
---|
468 | owl_function_error("Cannot set permissions on %s: %s", |
---|
469 | actual_filename, strerror(errno)); |
---|
470 | unlink(newfile); |
---|
471 | fclose(new); |
---|
472 | owl_free(newfile); |
---|
473 | fclose(old); |
---|
474 | free(actual_filename); |
---|
475 | return -1; |
---|
476 | } |
---|
477 | |
---|
478 | while (owl_getline_chomp(&buf, old)) |
---|
479 | if (strcasecmp(buf, line) != 0) |
---|
480 | fprintf(new, "%s\n", buf); |
---|
481 | else |
---|
482 | numremoved++; |
---|
483 | owl_free(buf); |
---|
484 | |
---|
485 | fclose(new); |
---|
486 | fclose(old); |
---|
487 | |
---|
488 | if (backup) { |
---|
489 | backupfile = owl_sprintf("%s.backup", actual_filename); |
---|
490 | unlink(backupfile); |
---|
491 | if (link(actual_filename, backupfile) != 0) { |
---|
492 | owl_function_error("Cannot link %s: %s", backupfile, strerror(errno)); |
---|
493 | owl_free(backupfile); |
---|
494 | unlink(newfile); |
---|
495 | owl_free(newfile); |
---|
496 | return -1; |
---|
497 | } |
---|
498 | owl_free(backupfile); |
---|
499 | } |
---|
500 | |
---|
501 | if (rename(newfile, actual_filename) != 0) { |
---|
502 | owl_function_error("Cannot move %s to %s: %s", |
---|
503 | newfile, actual_filename, strerror(errno)); |
---|
504 | numremoved = -1; |
---|
505 | } |
---|
506 | |
---|
507 | unlink(newfile); |
---|
508 | owl_free(newfile); |
---|
509 | |
---|
510 | g_free(actual_filename); |
---|
511 | |
---|
512 | return numremoved; |
---|
513 | } |
---|
514 | |
---|
515 | /* Return the base class or instance from a zephyr class, by removing |
---|
516 | leading `un' or trailing `.d'. |
---|
517 | The caller is responsible for freeing the allocated string. |
---|
518 | */ |
---|
519 | char * owl_util_baseclass(const char * class) |
---|
520 | { |
---|
521 | char *start, *end; |
---|
522 | |
---|
523 | while(!strncmp(class, "un", 2)) { |
---|
524 | class += 2; |
---|
525 | } |
---|
526 | |
---|
527 | start = owl_strdup(class); |
---|
528 | end = start + strlen(start) - 1; |
---|
529 | while(end > start && *end == 'd' && *(end-1) == '.') { |
---|
530 | end -= 2; |
---|
531 | } |
---|
532 | *(end + 1) = 0; |
---|
533 | |
---|
534 | return start; |
---|
535 | } |
---|
536 | |
---|
537 | const char * owl_get_datadir(void) |
---|
538 | { |
---|
539 | const char * datadir = getenv("BARNOWL_DATA_DIR"); |
---|
540 | if(datadir != NULL) |
---|
541 | return datadir; |
---|
542 | return DATADIR; |
---|
543 | } |
---|
544 | |
---|
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 | |
---|
553 | /* Strips format characters from a valid utf-8 string. Returns the |
---|
554 | empty string if 'in' does not validate. */ |
---|
555 | char * owl_strip_format_chars(const char *in) |
---|
556 | { |
---|
557 | char *r; |
---|
558 | if (g_utf8_validate(in, -1, NULL)) { |
---|
559 | const char *s, *p; |
---|
560 | r = owl_malloc(strlen(in)+1); |
---|
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. */ |
---|
567 | if (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
568 | strncat(r, s, p-s); |
---|
569 | p = g_utf8_next_char(p); |
---|
570 | while (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
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 { |
---|
583 | r = owl_strdup(""); |
---|
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. |
---|
592 | */ |
---|
593 | char * owl_validate_or_convert(const char *in) |
---|
594 | { |
---|
595 | if (g_utf8_validate(in, -1, NULL)) { |
---|
596 | return owl_strip_format_chars(in); |
---|
597 | } |
---|
598 | else { |
---|
599 | return g_convert(in, -1, |
---|
600 | "UTF-8", "ISO-8859-1", |
---|
601 | NULL, NULL, NULL); |
---|
602 | } |
---|
603 | } |
---|
604 | /* |
---|
605 | * Validate 'in' as UTF-8, and either return a copy of it, or an empty |
---|
606 | * string if it is invalid utf-8. |
---|
607 | */ |
---|
608 | char * owl_validate_utf8(const char *in) |
---|
609 | { |
---|
610 | char *out; |
---|
611 | if (g_utf8_validate(in, -1, NULL)) { |
---|
612 | out = owl_strdup(in); |
---|
613 | } else { |
---|
614 | out = owl_strdup(""); |
---|
615 | } |
---|
616 | return out; |
---|
617 | } |
---|
618 | |
---|
619 | /* This is based on _extract() and _isCJ() from perl's Text::WrapI18N */ |
---|
620 | int owl_util_can_break_after(gunichar c) |
---|
621 | { |
---|
622 | |
---|
623 | if (c == ' ') return 1; |
---|
624 | if (c >= 0x3000 && c <= 0x312f) { |
---|
625 | /* CJK punctuations, Hiragana, Katakana, Bopomofo */ |
---|
626 | if (c == 0x300a || c == 0x300c || c == 0x300e || |
---|
627 | c == 0x3010 || c == 0x3014 || c == 0x3016 || |
---|
628 | c == 0x3018 || c == 0x301a) |
---|
629 | return 0; |
---|
630 | return 1; |
---|
631 | } |
---|
632 | if (c >= 0x31a0 && c <= 0x31bf) {return 1;} /* Bopomofo */ |
---|
633 | if (c >= 0x31f0 && c <= 0x31ff) {return 1;} /* Katakana extension */ |
---|
634 | if (c >= 0x3400 && c <= 0x9fff) {return 1;} /* Han Ideogram */ |
---|
635 | if (c >= 0xf900 && c <= 0xfaff) {return 1;} /* Han Ideogram */ |
---|
636 | if (c >= 0x20000 && c <= 0x2ffff) {return 1;} /* Han Ideogram */ |
---|
637 | return 0; |
---|
638 | } |
---|
639 | |
---|
640 | char *owl_escape_highbit(const char *str) |
---|
641 | { |
---|
642 | GString *out = g_string_new(""); |
---|
643 | unsigned char c; |
---|
644 | while((c = (*str++))) { |
---|
645 | if(c == '\\') { |
---|
646 | g_string_append(out, "\\\\"); |
---|
647 | } else if(c & 0x80) { |
---|
648 | g_string_append_printf(out, "\\x%02x", (int)c); |
---|
649 | } else { |
---|
650 | g_string_append_c(out, c); |
---|
651 | } |
---|
652 | } |
---|
653 | return g_string_free(out, 0); |
---|
654 | } |
---|
655 | |
---|
656 | /* innards of owl_getline{,_chomp} below */ |
---|
657 | static int owl_getline_internal(char **s, FILE *fp, int newline) |
---|
658 | { |
---|
659 | int size = 0; |
---|
660 | int target = 0; |
---|
661 | int count = 0; |
---|
662 | int c; |
---|
663 | |
---|
664 | while (1) { |
---|
665 | c = getc(fp); |
---|
666 | if ((target + 1) > size) { |
---|
667 | size += BUFSIZ; |
---|
668 | *s = owl_realloc(*s, size); |
---|
669 | } |
---|
670 | if (c == EOF) |
---|
671 | break; |
---|
672 | count++; |
---|
673 | if (c != '\n' || newline) |
---|
674 | (*s)[target++] = c; |
---|
675 | if (c == '\n') |
---|
676 | break; |
---|
677 | } |
---|
678 | (*s)[target] = 0; |
---|
679 | |
---|
680 | return count; |
---|
681 | } |
---|
682 | |
---|
683 | /* Read a line from fp, allocating memory to hold it, returning the number of |
---|
684 | * byte read. *s should either be NULL or a pointer to memory allocated with |
---|
685 | * owl_malloc; it will be owl_realloc'd as appropriate. The caller must |
---|
686 | * eventually free it. (This is roughly the interface of getline in the gnu |
---|
687 | * libc). |
---|
688 | * |
---|
689 | * The final newline will be included if it's there. |
---|
690 | */ |
---|
691 | int owl_getline(char **s, FILE *fp) |
---|
692 | { |
---|
693 | return owl_getline_internal(s, fp, 1); |
---|
694 | } |
---|
695 | |
---|
696 | /* As above, but omitting the final newline */ |
---|
697 | int owl_getline_chomp(char **s, FILE *fp) |
---|
698 | { |
---|
699 | return owl_getline_internal(s, fp, 0); |
---|
700 | } |
---|
701 | |
---|
702 | /* Read the rest of the input available in fp into a string. */ |
---|
703 | char *owl_slurp(FILE *fp) |
---|
704 | { |
---|
705 | char *buf = NULL; |
---|
706 | char *p; |
---|
707 | int size = 0; |
---|
708 | int count; |
---|
709 | |
---|
710 | while (1) { |
---|
711 | buf = owl_realloc(buf, size + BUFSIZ); |
---|
712 | p = &buf[size]; |
---|
713 | size += BUFSIZ; |
---|
714 | |
---|
715 | if ((count = fread(p, 1, BUFSIZ, fp)) < BUFSIZ) |
---|
716 | break; |
---|
717 | } |
---|
718 | p[count] = 0; |
---|
719 | |
---|
720 | return buf; |
---|
721 | } |
---|
722 | |
---|
723 | gulong owl_dirty_window_on_signal(owl_window *w, gpointer sender, const gchar *detailed_signal) |
---|
724 | { |
---|
725 | return owl_signal_connect_object(sender, detailed_signal, G_CALLBACK(owl_window_dirty), w, G_CONNECT_SWAPPED); |
---|
726 | } |
---|
727 | |
---|
728 | typedef struct { /*noproto*/ |
---|
729 | GObject *sender; |
---|
730 | gulong signal_id; |
---|
731 | } SignalData; |
---|
732 | |
---|
733 | static void _closure_invalidated(gpointer data, GClosure *closure); |
---|
734 | |
---|
735 | /* |
---|
736 | * GObject's g_signal_connect_object has a documented bug. This function is |
---|
737 | * identical except it does not leak the signal handler. |
---|
738 | */ |
---|
739 | gulong owl_signal_connect_object(gpointer sender, const gchar *detailed_signal, GCallback c_handler, gpointer receiver, GConnectFlags connect_flags) |
---|
740 | { |
---|
741 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (sender), 0); |
---|
742 | g_return_val_if_fail (detailed_signal != NULL, 0); |
---|
743 | g_return_val_if_fail (c_handler != NULL, 0); |
---|
744 | |
---|
745 | if (receiver) { |
---|
746 | SignalData *sdata; |
---|
747 | GClosure *closure; |
---|
748 | gulong signal_id; |
---|
749 | |
---|
750 | g_return_val_if_fail (G_IS_OBJECT (receiver), 0); |
---|
751 | |
---|
752 | closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, receiver); |
---|
753 | signal_id = g_signal_connect_closure (sender, detailed_signal, closure, connect_flags & G_CONNECT_AFTER); |
---|
754 | |
---|
755 | /* Register the missing hooks */ |
---|
756 | sdata = g_slice_new0(SignalData); |
---|
757 | sdata->sender = sender; |
---|
758 | sdata->signal_id = signal_id; |
---|
759 | |
---|
760 | g_closure_add_invalidate_notifier(closure, sdata, _closure_invalidated); |
---|
761 | |
---|
762 | return signal_id; |
---|
763 | } else { |
---|
764 | return g_signal_connect_data(sender, detailed_signal, c_handler, NULL, NULL, connect_flags); |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | /* |
---|
769 | * There are three ways the signal could come to an end: |
---|
770 | * |
---|
771 | * 1. The user explicitly disconnects it with the returned signal_id. |
---|
772 | * - In that case, the disconnection unref's the closure, causing it |
---|
773 | * to first be invalidated. The handler's already disconnected, so |
---|
774 | * we have no work to do. |
---|
775 | * 2. The sender gets destroyed. |
---|
776 | * - GObject will disconnect each signal which then goes into the above |
---|
777 | * case. Our handler does no work. |
---|
778 | * 3. The receiver gets destroyed. |
---|
779 | * - The GClosure was created by g_cclosure_new_object_{,swap} which gets |
---|
780 | * invalidated when the receiver is destroyed. We then follow through case 1 |
---|
781 | * again, but *this* time, the handler has not been disconnected. We then |
---|
782 | * clean up ourselves. |
---|
783 | * |
---|
784 | * We can't actually hook into this process earlier with weakrefs as GObject |
---|
785 | * will, on object dispose, first disconnect signals, then invalidate closures, |
---|
786 | * and notify weakrefs last. |
---|
787 | */ |
---|
788 | static void _closure_invalidated(gpointer data, GClosure *closure) |
---|
789 | { |
---|
790 | SignalData *sdata = data; |
---|
791 | if (g_signal_handler_is_connected(sdata->sender, sdata->signal_id)) { |
---|
792 | g_signal_handler_disconnect(sdata->sender, sdata->signal_id); |
---|
793 | } |
---|
794 | g_slice_free(SignalData, sdata); |
---|
795 | } |
---|
796 | |
---|