]> diplodocus.org Git - nmh/blob - sbr/utils.c
Add Arch Linux to the often-built-and-tested distro list.
[nmh] / sbr / utils.c
1 /* utils.c -- various utility routines
2 *
3 * This code is Copyright (c) 2006, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "read_yes_or_no_if_tty.h"
10 #include "concat.h"
11 #include "r1bindex.h"
12 #include "context_foil.h"
13 #include "context_replace.h"
14 #include "context_read.h"
15 #include "context_find.h"
16 #include "print_help.h"
17 #include "error.h"
18 #include "h/utils.h"
19 #include "h/signals.h"
20 #include "m_mktemp.h"
21 #include "makedir.h"
22 #include <fcntl.h>
23 #include <limits.h>
24 #include "read_line.h"
25
26 extern char *mhdocdir;
27
28 /* plurals gives the letter ess to indicate a plural noun, or an empty
29 * string as plurals+1 for the singular noun. Used by the PLURALS
30 * macro. */
31 const char plurals[] = "s";
32
33 /*
34 * We allocate space for messages (msgs array)
35 * this number of elements at a time.
36 */
37 #define MAXMSGS 256
38
39 /* Call malloc(3), exiting on NULL return. */
40 void *
41 mh_xmalloc(size_t size)
42 {
43 void *p;
44
45 if (size == 0)
46 size = 1; /* Some mallocs don't like 0. */
47 p = malloc(size);
48 if (!p)
49 die("malloc failed, size wanted: %zu", size);
50
51 return p;
52 }
53
54 /* Call realloc(3), exiting on NULL return. */
55 void *
56 mh_xrealloc(void *ptr, size_t size)
57 {
58 void *new;
59
60 /* Copy POSIX behaviour, coping with non-POSIX systems. */
61 if (size == 0) {
62 free(ptr);
63 return mh_xmalloc(1); /* Get a unique pointer. */
64 }
65 if (!ptr)
66 return mh_xmalloc(size);
67
68 new = realloc(ptr, size);
69 if (!new)
70 die("realloc failed, size wanted: %zu", size);
71
72 return new;
73 }
74
75 /* Call calloc(3), exiting on NULL return. */
76 void *
77 mh_xcalloc(size_t nelem, size_t elsize)
78 {
79 void *p;
80
81 if (!nelem || !elsize)
82 return mh_xmalloc(1); /* Get a unique pointer. */
83
84 p = calloc(nelem, elsize);
85 if (!p)
86 die("calloc failed, size wanted: %zu * %zu", nelem, elsize);
87
88 return p;
89 }
90
91 /* Duplicate a NUL-terminated string, exit on failure. */
92 char *
93 mh_xstrdup(const char *src)
94 {
95 size_t n;
96 char *dest;
97
98 n = strlen(src) + 1; /* Ignore possibility of overflow. */
99 dest = mh_xmalloc(n);
100 memcpy(dest, src, n);
101
102 return dest;
103 }
104
105 /*
106 * Return the present working directory, if the current directory does not
107 * exist, or is too long, make / the pwd.
108 */
109 char *
110 pwd(void)
111 {
112 char *cp;
113 static char curwd[PATH_MAX];
114
115 if (!getcwd (curwd, PATH_MAX)) {
116 inform("unable to determine working directory, continuing...");
117 if (!mypath || !*mypath
118 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
119 strcpy (curwd, "/");
120 if (chdir (curwd) < 0) {
121 advise (curwd, "chdir");
122 }
123 }
124 return curwd;
125 }
126
127 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
128 *cp = '\0';
129
130 return curwd;
131 }
132
133 /* add returns a newly malloc'd string, exiting on failure. The order
134 * of the parameters is unusual. A NULL parameter is treated as an
135 * empty string. s1 is free'd. Use mh_xstrdup(s) rather than add(s,
136 * NULL), with FENDNULL() if s might be NULL.
137 *
138 * add(NULL, NULL) -> ""
139 * add(NULL, "foo") -> "foo"
140 * add("bar", NULL) -> "bar"
141 * add("bar", "foo") -> "foobar"
142 */
143 char *
144 add (const char *s2, char *s1)
145 {
146 char *cp;
147 size_t len1 = 0, len2 = 0;
148
149 if (s1)
150 len1 = strlen (s1);
151 if (s2)
152 len2 = strlen (s2);
153
154 cp = mh_xmalloc (len1 + len2 + 1);
155
156 /* Copy s1 and free it */
157 if (s1) {
158 memcpy (cp, s1, len1);
159 free (s1);
160 }
161
162 /* Copy s2 */
163 if (s2)
164 memcpy (cp + len1, s2, len2);
165
166 /* Now NULL terminate the string */
167 cp[len1 + len2] = '\0';
168
169 return cp;
170 }
171
172 /*
173 * addlist
174 * Append an item to a comma separated list
175 */
176 char *
177 addlist (char *list, const char *item)
178 {
179 if (list)
180 list = add (", ", list);
181
182 return add (item, list);
183 }
184
185 /*
186 * folder_exists
187 * Check to see if a folder exists.
188 */
189 int
190 folder_exists(const char *folder)
191 {
192 struct stat st;
193
194 return stat(folder, &st) != -1;
195 }
196
197 /*
198 * create_folder
199 * Check to see if a folder exists, if not, prompt the user to create
200 * it.
201 */
202 void
203 create_folder(char *folder, int autocreate, void (*done_callback)(int))
204 {
205 struct stat st;
206 extern int errno;
207 char *cp;
208
209 if (stat (folder, &st) == -1) {
210 if (errno != ENOENT)
211 adios (folder, "error on folder");
212 if (autocreate == 0) {
213 /* ask before creating folder */
214 cp = concat ("Create folder \"", folder, "\"? ", NULL);
215 if (!read_yes_or_no_if_tty (cp))
216 done_callback (1);
217 free (cp);
218 } else if (autocreate == -1) {
219 /* do not create, so exit */
220 done_callback (1);
221 }
222 if (!makedir (folder))
223 die("unable to create folder %s", folder);
224 }
225 }
226
227 /*
228 * num_digits
229 * Return the number of digits in a nonnegative integer.
230 */
231 int
232 num_digits (int n)
233 {
234 int ndigits = 0;
235
236 /* Sanity check */
237 if (n < 0)
238 die("oops, num_digits called with negative value");
239
240 if (n == 0)
241 return 1;
242
243 while (n) {
244 n /= 10;
245 ndigits++;
246 }
247
248 return ndigits;
249 }
250
251 /*
252 * Append a message arg to an array of them, resizing it if necessary.
253 * Really a simple vector-of-(char *) maintenance routine.
254 */
255 void
256 app_msgarg(struct msgs_array *msgs, char *cp)
257 {
258 if(msgs->size >= msgs->max) {
259 msgs->max += MAXMSGS;
260 msgs->msgs = mh_xrealloc(msgs->msgs,
261 msgs->max * sizeof(*msgs->msgs));
262 }
263 msgs->msgs[msgs->size++] = cp;
264 }
265
266 /*
267 * Append a message number to an array of them, resizing it if necessary.
268 * Like app_msgarg, but with a vector-of-ints instead.
269 */
270
271 void
272 app_msgnum(struct msgnum_array *msgs, int msgnum)
273 {
274 if (msgs->size >= msgs->max) {
275 msgs->max += MAXMSGS;
276 msgs->msgnums = mh_xrealloc(msgs->msgnums,
277 msgs->max * sizeof(*msgs->msgnums));
278 }
279 msgs->msgnums[msgs->size++] = msgnum;
280 }
281
282
283 /*
284 * Finds first occurrence of str in buf. buf is not a C string but a
285 * byte array of length buflen. str is a null-terminated C string.
286 * find_str() does not modify buf but passes back a non-const char *
287 * pointer so that the caller can modify it.
288 */
289 char *
290 find_str (const char buf[], size_t buflen, const char *str)
291 {
292 const size_t len = strlen (str);
293 size_t i;
294
295 for (i = 0; i + len <= buflen; ++i, ++buf) {
296 if (! memcmp (buf, str, len)) return (char *) buf;
297 }
298
299 return NULL;
300 }
301
302
303 /*
304 * Finds last occurrence of str in buf. buf is not a C string but a
305 * byte array of length buflen. str is a null-terminated C string.
306 * find_str() does not modify buf but passes back a non-const char *
307 * pointer so that the caller can modify it.
308 */
309 char *
310 rfind_str (const char buf[], size_t buflen, const char *str)
311 {
312 const size_t len = strlen (str);
313 size_t i;
314
315 for (i = 0, buf += buflen - len; i + len <= buflen; ++i, --buf) {
316 if (! memcmp (buf, str, len)) return (char *) buf;
317 }
318
319 return NULL;
320 }
321
322
323 /* POSIX doesn't have strcasestr() so emulate it. */
324 char *
325 nmh_strcasestr (const char *s1, const char *s2)
326 {
327 const size_t len = strlen (s2);
328
329 if (isupper ((unsigned char) s2[0]) || islower ((unsigned char)s2[0])) {
330 char first[3];
331 first[0] = (char) toupper ((unsigned char) s2[0]);
332 first[1] = (char) tolower ((unsigned char) s2[0]);
333 first[2] = '\0';
334
335 for (s1 = strpbrk (s1, first); s1; s1 = strpbrk (++s1, first)) {
336 if (! strncasecmp (s1, s2, len)) return (char *) s1;
337 }
338 } else {
339 for (s1 = strchr (s1, s2[0]); s1; s1 = strchr (++s1, s2[0])) {
340 if (! strncasecmp (s1, s2, len)) return (char *) s1;
341 }
342 }
343
344 return NULL;
345 }
346
347
348 /* truncpy copies at most size - 1 chars from non-NULL src to non-NULL,
349 * non-overlapping, dst, and ensures dst is NUL terminated. If size is
350 * zero then it aborts as dst cannot be NUL terminated.
351 *
352 * It's to be used when truncation is intended and correct, e.g.
353 * reporting a possibly very long external string back to the user. One
354 * of its advantages over strncpy(3) is it doesn't pad in the common
355 * case of no truncation. */
356 void
357 trunccpy(char *dst, const char *src, size_t size)
358 {
359 if (!size) {
360 inform("trunccpy: zero-length destination: \"%.20s\"",
361 src ? src : "null");
362 abort();
363 }
364
365 if (strnlen(src, size) < size) {
366 strcpy(dst, src);
367 } else {
368 memcpy(dst, src, size - 1);
369 dst[size - 1] = '\0';
370 }
371 }
372
373
374 /* has_prefix returns true if non-NULL s starts with non-NULL prefix. */
375 bool
376 has_prefix(const char *s, const char *prefix)
377 {
378 while (*s && *s == *prefix) {
379 s++;
380 prefix++;
381 }
382
383 return *prefix == '\0';
384 }
385
386
387 /* has_suffix returns true if non-NULL s ends with non-NULL suffix. */
388 bool
389 has_suffix(const char *s, const char *suffix)
390 {
391 size_t ls, lsuf;
392
393 ls = strlen(s);
394 lsuf = strlen(suffix);
395
396 return lsuf <= ls && !strcmp(s + ls - lsuf, suffix);
397 }
398
399
400 /* has_suffix_c returns true if non-NULL string s ends with a c before the
401 * terminating NUL. */
402 bool
403 has_suffix_c(const char *s, int c)
404 {
405 return *s && s[strlen(s) - 1] == c;
406 }
407
408
409 /* trim_suffix_c deletes c from the end of non-NULL string s if it's
410 * present, shortening s by 1. Only one instance of c is removed. */
411 void
412 trim_suffix_c(char *s, int c)
413 {
414 if (!*s)
415 return;
416
417 s += strlen(s) - 1;
418 if (*s == c)
419 *s = '\0';
420 }
421
422
423 /* to_lower runs all of s through tolower(3). */
424 void
425 to_lower(char *s)
426 {
427 unsigned char *b;
428
429 for (b = (unsigned char *)s; (*b = tolower(*b)); b++)
430 ;
431 }
432
433
434 /* to_upper runs all of s through toupper(3). */
435 void
436 to_upper(char *s)
437 {
438 unsigned char *b;
439
440 for (b = (unsigned char *)s; (*b = toupper(*b)); b++)
441 ;
442 }
443
444
445 int
446 nmh_init(const char *argv0, bool read_context, bool check_version)
447 {
448 int status = OK;
449 char *locale;
450
451 invo_name = r1bindex ((char *) argv0, '/');
452
453 if (setup_signal_handlers()) {
454 admonish("sigaction", "unable to set up signal handlers");
455 }
456
457 /* POSIX atexit() does not define any error conditions. */
458 if (atexit(remove_registered_files_atexit)) {
459 admonish("atexit", "unable to register atexit function");
460 }
461
462 /* Read context, if supposed to. */
463 if (read_context) {
464 char *cp;
465
466 context_read();
467
468 bool allow_version_check = true;
469 bool check_older_version = false;
470 if (!check_version ||
471 ((cp = context_find ("Welcome")) && strcasecmp (cp, "disable") == 0)) {
472 allow_version_check = false;
473 } else if ((cp = getenv ("MHCONTEXT")) != NULL && *cp != '\0') {
474 /* Context file comes from $MHCONTEXT, so only print the message
475 if the context file has an older version. If it does, or if it
476 doesn't have a version at all, update the version. */
477 check_older_version = true;
478 }
479
480 /* Check to see if the user is running a different (or older, if
481 specified) version of nmh than they had run before, and notify them
482 if so. */
483 if (allow_version_check && isatty (fileno (stdin)) &&
484 isatty (fileno (stdout)) && isatty (fileno (stderr))) {
485 if (nmh_version_changed (check_older_version)) {
486 printf ("==================================================="
487 "=====================\n");
488 printf ("Welcome to nmh version %s\n\n", VERSION);
489 printf ("See the release notes in %s/NEWS\n\n",
490 mhdocdir);
491 print_intro (stdout, 1);
492 printf ("\nThis message will not be repeated until "
493 "nmh is next updated.\n");
494 printf ("==================================================="
495 "=====================\n\n");
496
497 fputs ("Press enter to continue: ", stdout);
498 (void) read_line ();
499 putchar ('\n');
500 }
501 }
502 } else {
503 if ((status = context_foil(NULL)) != OK) {
504 advise("", "failed to create minimal profile/context");
505 }
506 }
507
508 /* Allow the user to set a locale in their profile. Otherwise, use the
509 "" string to pull it from their environment, see setlocale(3). */
510 if ((locale = context_find ("locale")) == NULL) {
511 locale = "";
512 }
513
514 if (! setlocale (LC_ALL, locale)) {
515 inform("setlocale failed, check your LC_ALL, LC_CTYPE, and LANG "
516 "environment variables, continuing...");
517 }
518
519 return status;
520 }
521
522
523 /*
524 * Check stored version, and return 1 if out-of-date or non-existent.
525 * Because the output of "mhparam version" is prefixed with "nmh-",
526 * use that prefix here.
527 */
528 int
529 nmh_version_changed (int older)
530 {
531 const char *const context_version = context_find("Version");
532
533 if (older) {
534 /* Convert the version strings to floats and compare them. This will
535 break for versions with multiple decimal points, etc. */
536 const float current_version = strtof (VERSION, NULL);
537 const float old_version =
538 context_version && has_prefix(context_version, "nmh-")
539 ? strtof (context_version + 4, NULL)
540 : 99999999;
541
542 if (context_version == NULL || old_version < current_version) {
543 context_replace ("Version", "nmh-" VERSION);
544 }
545
546 return old_version < current_version;
547 }
548
549 if (context_version == NULL || strcmp(context_version, "nmh-" VERSION) != 0) {
550 context_replace ("Version", "nmh-" VERSION);
551 return 1;
552 }
553
554 return 0;
555 }
556
557
558 /* contains8bit returns true if any byte from start onwards fails
559 * isascii(3), i.e. is outside [0, 0x7f]. If start is NULL it returns
560 * false. Bytes are examined until a NUL byte, or, if end is not NULL,
561 * whilst start is before end. */
562 bool
563 contains8bit(const char *start, const char *end)
564 {
565 const char *p;
566 char c;
567
568 if (!start)
569 return false;
570
571 p = start;
572 if (end) {
573 while (p < end && (c = (*p++)))
574 if (!isascii((unsigned char)c))
575 return true;
576 } else {
577 while ((c = (*p++)))
578 if (!isascii((unsigned char)c))
579 return true;
580 }
581
582 return false;
583 }
584
585
586 /*
587 * See if input has any 8-bit bytes.
588 */
589 int
590 scan_input (int fd, int *eightbit)
591 {
592 int state;
593 char buf[BUFSIZ];
594
595 *eightbit = 0;
596 lseek(fd, 0, SEEK_SET);
597
598 while ((state = read (fd, buf, sizeof buf)) > 0) {
599 if (contains8bit (buf, buf + state)) {
600 *eightbit = 1;
601 return OK;
602 }
603 }
604
605 return state == NOTOK ? NOTOK : OK;
606 }
607
608
609 /*
610 * Convert an int to a char string.
611 */
612 char *
613 m_str(int value)
614 {
615 return m_strn(value, 0);
616 }
617
618
619 /*
620 * Convert an int to a char string, of limited width if > 0.
621 */
622 #define STR(s) #s
623 /* SIZE(n) includes NUL. n must just be digits, not an equation. */
624 #define SIZE(n) (sizeof STR(n))
625
626 char *
627 m_strn(int value, unsigned int width)
628 {
629 /* Need to include space for negative sign. But don't use INT_MIN
630 because it could be a macro that would fool SIZE(n). */
631 static char buffer[SIZE(-INT_MAX)];
632 const int num_chars = snprintf(buffer, sizeof buffer, "%d", value);
633
634 return num_chars > 0 && (width == 0 || (unsigned int) num_chars <= width)
635 ? buffer
636 : "?";
637 }