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