]> diplodocus.org Git - nmh/blob - sbr/utils.c
Replace getcpy() with mh_xstrdup() where the string isn't NULL.
[nmh] / sbr / utils.c
1
2 /*
3 * utils.c -- various utility routines
4 *
5 * This code is Copyright (c) 2006, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12 #include <fcntl.h>
13
14 /* sbr/signals.c */
15 extern int setup_signal_handlers();
16
17 /* sbr/m_mktemp.c */
18 extern void remove_registered_files_atexit();
19
20 extern char *mhdocdir;
21
22 /*
23 * We allocate space for messages (msgs array)
24 * this number of elements at a time.
25 */
26 #define MAXMSGS 256
27
28 /* Call malloc(3), exiting on NULL return. */
29 void *mh_xmalloc(size_t size)
30 {
31 void *p;
32
33 if (size == 0)
34 size = 1; /* Some mallocs don't like 0. */
35 p = malloc(size);
36 if (!p)
37 adios(NULL, "malloc failed, size wanted: %zu", size);
38
39 return p;
40 }
41
42 /* Call realloc(3), exiting on NULL return. */
43 void *mh_xrealloc(void *ptr, size_t size)
44 {
45 void *new;
46
47 /* Copy POSIX behaviour, coping with non-POSIX systems. */
48 if (size == 0) {
49 if (ptr) {
50 free(ptr);
51 }
52 return mh_xmalloc(1); /* Get a unique pointer. */
53 }
54 if (!ptr)
55 return mh_xmalloc(size);
56
57 new = realloc(ptr, size);
58 if (!new)
59 adios(NULL, "realloc failed, size wanted: %zu", size);
60
61 return new;
62 }
63
64 /* Call calloc(3), exiting on NULL return. */
65 void *mh_xcalloc(size_t nelem, size_t elsize)
66 {
67 void *p;
68
69 if (!nelem || !elsize)
70 return mh_xmalloc(1); /* Get a unique pointer. */
71
72 p = calloc(nelem, elsize);
73 if (!p)
74 adios(NULL, "calloc failed, size wanted: %zu * %zu", nelem, elsize);
75
76 return p;
77 }
78
79 /* Duplicate a NUL-terminated string, exit on failure. */
80 char *mh_xstrdup(const char *src)
81 {
82 size_t n;
83 char *dest;
84
85 n = strlen(src) + 1; /* Ignore possibility of overflow. */
86 dest = mh_xmalloc(n);
87 memcpy(dest, src, n);
88
89 return dest;
90 }
91
92 /* Call free(3), if ptr isn't NULL. */
93 void mh_xfree(void *ptr)
94 {
95 if (ptr)
96 free(ptr); /* Some very old platforms can't cope with NULL. */
97 }
98
99 /*
100 * Return the present working directory, if the current directory does not
101 * exist, or is too long, make / the pwd.
102 */
103 char *
104 pwd(void)
105 {
106 char *cp;
107 static char curwd[PATH_MAX];
108
109 if (!getcwd (curwd, PATH_MAX)) {
110 admonish (NULL, "unable to determine working directory");
111 if (!mypath || !*mypath
112 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
113 strcpy (curwd, "/");
114 if (chdir (curwd) < 0) {
115 advise (curwd, "chdir");
116 }
117 }
118 return curwd;
119 }
120
121 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
122 *cp = '\0';
123
124 return curwd;
125 }
126
127 /*
128 * add -- If "s1" is NULL, this routine just creates a
129 * -- copy of "s2" into newly malloc'ed memory.
130 * --
131 * -- If "s1" is not NULL, then copy the concatenation
132 * -- of "s1" and "s2" (note the order) into newly
133 * -- malloc'ed memory. Then free "s1".
134 */
135 char *
136 add (const char *s2, char *s1)
137 {
138 char *cp;
139 size_t len1 = 0, len2 = 0;
140
141 if (s1)
142 len1 = strlen (s1);
143 if (s2)
144 len2 = strlen (s2);
145
146 cp = mh_xmalloc (len1 + len2 + 1);
147
148 /* Copy s1 and free it */
149 if (s1) {
150 memcpy (cp, s1, len1);
151 free (s1);
152 }
153
154 /* Copy s2 */
155 if (s2)
156 memcpy (cp + len1, s2, len2);
157
158 /* Now NULL terminate the string */
159 cp[len1 + len2] = '\0';
160
161 return cp;
162 }
163
164 /*
165 * addlist
166 * Append an item to a comma separated list
167 */
168 char *
169 addlist (char *list, const char *item)
170 {
171 if (list)
172 list = add (", ", list);
173
174 return add (item, list);
175 }
176
177 /*
178 * folder_exists
179 * Check to see if a folder exists.
180 */
181 int folder_exists(const char *folder)
182 {
183 struct stat st;
184 int exists = 0;
185
186 if (stat (folder, &st) == -1) {
187 /* The folder either doesn't exist, or we hit an error. Either way
188 * return a failure.
189 */
190 exists = 0;
191 } else {
192 /* We can see a folder with the right name */
193 exists = 1;
194 }
195
196 return exists;
197 }
198
199
200 /*
201 * create_folder
202 * Check to see if a folder exists, if not, prompt the user to create
203 * it.
204 */
205 void create_folder(char *folder, int autocreate, void (*done_callback)(int))
206 {
207 struct stat st;
208 extern int errno;
209 char *cp;
210
211 if (stat (folder, &st) == -1) {
212 if (errno != ENOENT)
213 adios (folder, "error on folder");
214 if (autocreate == 0) {
215 /* ask before creating folder */
216 cp = concat ("Create folder \"", folder, "\"? ", NULL);
217 if (!read_yes_or_no_if_tty (cp))
218 done_callback (1);
219 free (cp);
220 } else if (autocreate == -1) {
221 /* do not create, so exit */
222 done_callback (1);
223 }
224 if (!makedir (folder))
225 adios (NULL, "unable to create folder %s", folder);
226 }
227 }
228
229 /*
230 * num_digits
231 * Return the number of digits in a nonnegative integer.
232 */
233 int
234 num_digits (int n)
235 {
236 int ndigits = 0;
237
238 /* Sanity check */
239 if (n < 0)
240 adios (NULL, "oops, num_digits called with negative value");
241
242 if (n == 0)
243 return 1;
244
245 while (n) {
246 n /= 10;
247 ndigits++;
248 }
249
250 return ndigits;
251 }
252
253 /*
254 * Append a message arg to an array of them, resizing it if necessary.
255 * Really a simple vector-of-(char *) maintenance routine.
256 */
257 void
258 app_msgarg(struct msgs_array *msgs, char *cp)
259 {
260 if(msgs->size >= msgs->max) {
261 msgs->max += MAXMSGS;
262 msgs->msgs = mh_xrealloc(msgs->msgs,
263 msgs->max * sizeof(*msgs->msgs));
264 }
265 msgs->msgs[msgs->size++] = cp;
266 }
267
268 /*
269 * Append a message number to an array of them, resizing it if necessary.
270 * Like app_msgarg, but with a vector-of-ints instead.
271 */
272
273 void
274 app_msgnum(struct msgnum_array *msgs, int msgnum)
275 {
276 if (msgs->size >= msgs->max) {
277 msgs->max += MAXMSGS;
278 msgs->msgnums = mh_xrealloc(msgs->msgnums,
279 msgs->max * sizeof(*msgs->msgnums));
280 }
281 msgs->msgnums[msgs->size++] = msgnum;
282 }
283
284 /* Open a form or components file */
285 int
286 open_form(char **form, char *def)
287 {
288 int in;
289 if (*form) {
290 if ((in = open (etcpath (*form), O_RDONLY)) == NOTOK)
291 adios (*form, "unable to open form file");
292 } else {
293 if ((in = open (etcpath (def), O_RDONLY)) == NOTOK)
294 adios (def, "unable to open default components file");
295 *form = def;
296 }
297 return in;
298 }
299
300
301 /*
302 * Finds first occurrence of str in buf. buf is not a C string but a
303 * byte array of length buflen. str is a null-terminated C string.
304 * find_str() does not modify buf but passes back a non-const char *
305 * pointer so that the caller can modify it.
306 */
307 char *
308 find_str (const char buf[], size_t buflen, const char *str) {
309 const size_t len = strlen (str);
310 size_t i;
311
312 for (i = 0; i + len <= buflen; ++i, ++buf) {
313 if (! memcmp (buf, str, len)) return (char *) buf;
314 }
315
316 return NULL;
317 }
318
319
320 /*
321 * Finds last occurrence of str in buf. buf is not a C string but a
322 * byte array of length buflen. str is a null-terminated C string.
323 * find_str() does not modify buf but passes back a non-const char *
324 * pointer so that the caller can modify it.
325 */
326 char *
327 rfind_str (const char buf[], size_t buflen, const char *str) {
328 const size_t len = strlen (str);
329 size_t i;
330
331 for (i = 0, buf += buflen - len; i + len <= buflen; ++i, --buf) {
332 if (! memcmp (buf, str, len)) return (char *) buf;
333 }
334
335 return NULL;
336 }
337
338
339 /* POSIX doesn't have strcasestr() so emulate it. */
340 char *
341 nmh_strcasestr (const char *s1, const char *s2) {
342 const size_t len = strlen (s2);
343
344 if (isupper ((unsigned char) s2[0]) || islower ((unsigned char)s2[0])) {
345 char first[3];
346 first[0] = (char) toupper ((unsigned char) s2[0]);
347 first[1] = (char) tolower ((unsigned char) s2[0]);
348 first[2] = '\0';
349
350 for (s1 = strpbrk (s1, first); s1; s1 = strpbrk (++s1, first)) {
351 if (! strncasecmp (s1, s2, len)) return (char *) s1;
352 }
353 } else {
354 for (s1 = strchr (s1, s2[0]); s1; s1 = strchr (++s1, s2[0])) {
355 if (! strncasecmp (s1, s2, len)) return (char *) s1;
356 }
357 }
358
359 return NULL;
360 }
361
362
363 int
364 nmh_init(const char *argv0, int read_context) {
365 if (! setlocale(LC_ALL, "")) {
366 admonish(NULL, "setlocale failed, check your LC_ALL, LC_CTYPE, and "
367 "LANG environment variables");
368 }
369
370 invo_name = r1bindex ((char *) argv0, '/');
371
372 if (setup_signal_handlers()) {
373 admonish("sigaction", "unable to set up signal handlers");
374 }
375
376 /* POSIX atexit() does not define any error conditions. */
377 if (atexit(remove_registered_files_atexit)) {
378 admonish("atexit", "unable to register atexit function");
379 }
380
381 /* Read context, if supposed to. */
382 if (read_context) {
383 int allow_version_check = 1;
384 int check_older_version = 0;
385 char *cp;
386
387 context_read();
388
389 if (read_context != 1 ||
390 ((cp = context_find ("Welcome")) && strcasecmp (cp, "disable") == 0)) {
391 allow_version_check = 0;
392 } else if ((cp = getenv ("MHCONTEXT")) != NULL && *cp != '\0') {
393 /* Context file comes from $MHCONTEXT, so only print the message
394 if the context file has an older version. If it does, or if it
395 doesn't have a version at all, update the version. */
396 check_older_version = 1;
397 }
398
399 /* Check to see if the user is running a different (or older, if
400 specified) version of nmh than they had run bfore, and notify them
401 if so. But only if read_context was set to a value to enable. */
402 if (allow_version_check && isatty (fileno (stdin)) &&
403 isatty (fileno (stdout)) && isatty (fileno (stderr))) {
404 if (nmh_version_changed (check_older_version)) {
405 printf ("==================================================="
406 "=====================\n");
407 printf ("Welcome to nmh version %s\n\n", VERSION);
408 printf ("See the release notes in %s/NEWS\n\n",
409 mhdocdir);
410 print_intro (stdout, 1);
411 printf ("\nThis message will not be repeated until "
412 "nmh is next updated.\n");
413 printf ("==================================================="
414 "=====================\n\n");
415
416 fputs ("Press enter to continue: ", stdout);
417 (void) read_line ();
418 putchar ('\n');
419 }
420 }
421
422 return OK;
423 } else {
424 int status = context_foil(NULL);
425 if (status != OK) {
426 advise("", "failed to create minimal profile/context");
427 }
428 return status;
429 }
430 }
431
432
433 /*
434 * Check stored version, and return 1 if out-of-date or non-existent.
435 * Because the output of "mhparam version" is prefixed with "nmh-",
436 * use that prefix here.
437 */
438 int
439 nmh_version_changed (int older) {
440 const char *const context_version = context_find("Version");
441
442 if (older) {
443 /* Convert the version strings to floats and compare them. This will
444 break for versions with multiple decimal points, etc. */
445 const float current_version = strtof (VERSION, NULL);
446 const float old_version =
447 context_version && strncmp (context_version, "nmh-", 4) == 0
448 ? strtof (context_version + 4, NULL)
449 : 99999999;
450
451 if (context_version == NULL || old_version < current_version) {
452 context_replace ("Version", "nmh-" VERSION);
453 }
454
455 return old_version < current_version ? 1 : 0;
456 } else {
457 if (context_version == NULL || strcmp(context_version, "nmh-" VERSION) != 0) {
458 context_replace ("Version", "nmh-" VERSION);
459
460 return 1;
461 } else {
462 return 0;
463 }
464 }
465 }
466
467
468 /* Returns copy of argument str with all characters converted to upper
469 case, and trimmed whitespace (see cpytrim()) . */
470 char *
471 upcase (const char *str) {
472 char *up = cpytrim (str);
473 char *cp;
474
475 for (cp = up; *cp; ++cp) { *cp = toupper ((unsigned char) *cp); }
476
477 return up;
478 }
479
480
481 /*
482 * Scan for any 8-bit characters. Return 1 if they exist.
483 *
484 * Scan up until the given endpoint (but not the actual endpoint itself).
485 * If the endpoint is NULL, scan until a '\0' is reached.
486 */
487
488 int
489 contains8bit(const char *start, const char *end)
490 {
491 if (! start)
492 return 0;
493
494 while (*start != '\0' && (!end || (start < end)))
495 if (! isascii((unsigned char) *start++))
496 return 1;
497
498 return 0;
499 }
500
501
502 /*
503 * See if input has any 8-bit bytes.
504 */
505 int
506 scan_input (int fd, int *eightbit) {
507 int state;
508 char buf[BUFSIZ];
509
510 *eightbit = 0;
511 lseek (fd, (off_t) 0, SEEK_SET);
512
513 while ((state = read (fd, buf, sizeof buf)) > 0) {
514 if (contains8bit (buf, buf + state)) {
515 *eightbit = 1;
516 return OK;
517 }
518 }
519
520 return state == NOTOK ? NOTOK : OK;
521 }