]> diplodocus.org Git - nmh/blob - sbr/utils.c
A value of 0 for the width switch of scan(1), inc(1), ap(1), dp(1),
[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
21 /*
22 * We allocate space for messages (msgs array)
23 * this number of elements at a time.
24 */
25 #define MAXMSGS 256
26
27 /*
28 * Safely call malloc
29 */
30 void *
31 mh_xmalloc(size_t size)
32 {
33 void *memory;
34
35 if (size == 0)
36 adios(NULL, "Tried to malloc 0 bytes");
37
38 memory = malloc(size);
39 if (!memory)
40 adios(NULL, "Malloc failed");
41
42 return memory;
43 }
44
45 /*
46 * Safely call realloc
47 */
48 void *
49 mh_xrealloc(void *ptr, size_t size)
50 {
51 void *memory;
52
53 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
54 if (!ptr)
55 return mh_xmalloc(size);
56
57 if (size == 0)
58 adios(NULL, "Tried to realloc 0bytes");
59
60 memory = realloc(ptr, size);
61 if (!memory)
62 adios(NULL, "Realloc failed");
63
64 return memory;
65 }
66
67 /*
68 * Return the present working directory, if the current directory does not
69 * exist, or is too long, make / the pwd.
70 */
71 char *
72 pwd(void)
73 {
74 register char *cp;
75 static char curwd[PATH_MAX];
76
77 if (!getcwd (curwd, PATH_MAX)) {
78 admonish (NULL, "unable to determine working directory");
79 if (!mypath || !*mypath
80 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
81 strcpy (curwd, "/");
82 if (chdir (curwd) < 0) {
83 advise (curwd, "chdir");
84 }
85 }
86 return curwd;
87 }
88
89 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
90 *cp = '\0';
91
92 return curwd;
93 }
94
95 /*
96 * add -- If "s1" is NULL, this routine just creates a
97 * -- copy of "s2" into newly malloc'ed memory.
98 * --
99 * -- If "s1" is not NULL, then copy the concatenation
100 * -- of "s1" and "s2" (note the order) into newly
101 * -- malloc'ed memory. Then free "s1".
102 */
103 char *
104 add (const char *s2, char *s1)
105 {
106 char *cp;
107 size_t len1 = 0, len2 = 0;
108
109 if (s1)
110 len1 = strlen (s1);
111 if (s2)
112 len2 = strlen (s2);
113
114 cp = mh_xmalloc (len1 + len2 + 1);
115
116 /* Copy s1 and free it */
117 if (s1) {
118 memcpy (cp, s1, len1);
119 free (s1);
120 }
121
122 /* Copy s2 */
123 if (s2)
124 memcpy (cp + len1, s2, len2);
125
126 /* Now NULL terminate the string */
127 cp[len1 + len2] = '\0';
128
129 return cp;
130 }
131
132 /*
133 * addlist
134 * Append an item to a comma separated list
135 */
136 char *
137 addlist (char *list, const char *item)
138 {
139 if (list)
140 list = add (", ", list);
141
142 return add (item, list);
143 }
144
145 /*
146 * folder_exists
147 * Check to see if a folder exists.
148 */
149 int folder_exists(const char *folder)
150 {
151 struct stat st;
152 int exists = 0;
153
154 if (stat (folder, &st) == -1) {
155 /* The folder either doesn't exist, or we hit an error. Either way
156 * return a failure.
157 */
158 exists = 0;
159 } else {
160 /* We can see a folder with the right name */
161 exists = 1;
162 }
163
164 return exists;
165 }
166
167
168 /*
169 * create_folder
170 * Check to see if a folder exists, if not, prompt the user to create
171 * it.
172 */
173 void create_folder(char *folder, int autocreate, void (*done_callback)(int))
174 {
175 struct stat st;
176 extern int errno;
177 char *cp;
178
179 if (stat (folder, &st) == -1) {
180 if (errno != ENOENT)
181 adios (folder, "error on folder");
182 if (autocreate == 0) {
183 /* ask before creating folder */
184 cp = concat ("Create folder \"", folder, "\"? ", NULL);
185 if (!getanswer (cp))
186 done_callback (1);
187 free (cp);
188 } else if (autocreate == -1) {
189 /* do not create, so exit */
190 done_callback (1);
191 }
192 if (!makedir (folder))
193 adios (NULL, "unable to create folder %s", folder);
194 }
195 }
196
197 /*
198 * num_digits
199 * Return the number of digits in a nonnegative integer.
200 */
201 int
202 num_digits (int n)
203 {
204 int ndigits = 0;
205
206 /* Sanity check */
207 if (n < 0)
208 adios (NULL, "oops, num_digits called with negative value");
209
210 if (n == 0)
211 return 1;
212
213 while (n) {
214 n /= 10;
215 ndigits++;
216 }
217
218 return ndigits;
219 }
220
221 /*
222 * Append a message arg to an array of them, resizing it if necessary.
223 * Really a simple vector-of-(char *) maintenance routine.
224 */
225 void
226 app_msgarg(struct msgs_array *msgs, char *cp)
227 {
228 if(msgs->size >= msgs->max) {
229 msgs->max += MAXMSGS;
230 msgs->msgs = mh_xrealloc(msgs->msgs,
231 msgs->max * sizeof(*msgs->msgs));
232 }
233 msgs->msgs[msgs->size++] = cp;
234 }
235
236 /*
237 * Append a message number to an array of them, resizing it if necessary.
238 * Like app_msgarg, but with a vector-of-ints instead.
239 */
240
241 void
242 app_msgnum(struct msgnum_array *msgs, int msgnum)
243 {
244 if (msgs->size >= msgs->max) {
245 msgs->max += MAXMSGS;
246 msgs->msgnums = mh_xrealloc(msgs->msgnums,
247 msgs->max * sizeof(*msgs->msgnums));
248 }
249 msgs->msgnums[msgs->size++] = msgnum;
250 }
251
252 /* Open a form or components file */
253 int
254 open_form(char **form, char *def)
255 {
256 int in;
257 if (*form) {
258 if ((in = open (etcpath (*form), O_RDONLY)) == NOTOK)
259 adios (*form, "unable to open form file");
260 } else {
261 if ((in = open (etcpath (def), O_RDONLY)) == NOTOK)
262 adios (def, "unable to open default components file");
263 *form = def;
264 }
265 return in;
266 }
267
268
269 /*
270 * Finds first occurrence of str in buf. buf is not a C string but a
271 * byte array of length buflen. str is a null-terminated C string.
272 * find_str() does not modify buf but passes back a non-const char *
273 * pointer so that the caller can modify it.
274 */
275 char *
276 find_str (const char buf[], size_t buflen, const char *str) {
277 const size_t len = strlen (str);
278 size_t i;
279
280 for (i = 0; i + len <= buflen; ++i, ++buf) {
281 if (! memcmp (buf, str, len)) return (char *) buf;
282 }
283
284 return NULL;
285 }
286
287
288 /*
289 * Finds last 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 rfind_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, buf += buflen - len; i + len <= buflen; ++i, --buf) {
300 if (! memcmp (buf, str, len)) return (char *) buf;
301 }
302
303 return NULL;
304 }
305
306
307 /* POSIX doesn't have strcasestr() so emulate it. */
308 char *
309 nmh_strcasestr (const char *s1, const char *s2) {
310 const size_t len = strlen (s2);
311
312 if (isupper ((unsigned char) s2[0]) || islower ((unsigned char)s2[0])) {
313 char first[3];
314 first[0] = (char) toupper ((unsigned char) s2[0]);
315 first[1] = (char) tolower ((unsigned char) s2[0]);
316 first[2] = '\0';
317
318 for (s1 = strpbrk (s1, first); s1; s1 = strpbrk (++s1, first)) {
319 if (! strncasecmp (s1, s2, len)) return (char *) s1;
320 }
321 } else {
322 for (s1 = strchr (s1, s2[0]); s1; s1 = strchr (++s1, s2[0])) {
323 if (! strncasecmp (s1, s2, len)) return (char *) s1;
324 }
325 }
326
327 return NULL;
328 }
329
330
331 int
332 nmh_init(const char *argv0, int read_context) {
333 if (! setlocale(LC_ALL, "")) {
334 admonish(NULL, "setlocale failed, check your LC_ALL, LC_CTYPE, and "
335 "LANG environment variables");
336 }
337
338 invo_name = r1bindex ((char *) argv0, '/');
339
340 if (setup_signal_handlers()) {
341 admonish("sigaction", "unable to set up signal handlers");
342 }
343
344 /* POSIX atexit() does not define any error conditions. */
345 if (atexit(remove_registered_files_atexit)) {
346 admonish("atexit", "unable to register atexit function");
347 }
348
349 if (read_context) {
350 context_read();
351 return OK;
352 } else {
353 int status = context_foil(NULL);
354 if (status != OK) {
355 advise("", "failed to create minimal profile/conext");
356 }
357 return status;
358 }
359 }
360
361
362 /* Returns copy of argument str with all characters converted to upper
363 case, and trimmed whitespace (see cpytrim()) . */
364 char *
365 upcase (const char *str) {
366 char *up = cpytrim (str);
367 char *cp;
368
369 for (cp = up; *cp; ++cp) { *cp = toupper ((unsigned char) *cp); }
370
371 return up;
372 }
373
374
375 /*
376 * Scan for any 8-bit characters. Return 1 if they exist.
377 *
378 * Scan up until the given endpoint (but not the actual endpoint itself).
379 * If the endpoint is NULL, scan until a '\0' is reached.
380 */
381
382 int
383 contains8bit(const char *start, const char *end)
384 {
385 if (! start)
386 return 0;
387
388 while (*start != '\0' && (!end || (start < end)))
389 if (! isascii((unsigned char) *start++))
390 return 1;
391
392 return 0;
393 }