]> diplodocus.org Git - nmh/blob - sbr/getarguments.c
a7ac1bea07f6c861aebf1387a00e4463df145e02
[nmh] / sbr / getarguments.c
1 /* getarguments.c -- Get the argument vector ready to go.
2 *
3 * This code is Copyright (c) 2002, 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 "context_find.h"
10 #include "brkstring.h"
11 #include "h/utils.h"
12
13 char **
14 getarguments (char *invo_name, int argc, char **argv, int check_context)
15 {
16 char *cp = NULL, **ap = NULL, **bp = NULL, **arguments = NULL;
17 int n = 0;
18
19 /*
20 * Check if profile/context specifies any arguments
21 */
22 if (check_context && (cp = context_find (invo_name))) {
23 cp = mh_xstrdup(cp); /* make copy */
24 ap = brkstring (cp, " ", "\n"); /* split string */
25
26 /* Count number of arguments split */
27 bp = ap;
28 while (*bp++)
29 n++;
30 }
31
32 arguments = mh_xmalloc ((argc + n) * sizeof(*arguments));
33 bp = arguments;
34
35 /* Copy any arguments from profile/context */
36 if (ap != NULL && n > 0) {
37 while (*ap)
38 *bp++ = *ap++;
39 }
40
41 /* Copy arguments from command line */
42 argv++;
43 while (*argv)
44 *bp++ = *argv++;
45
46 /* Now NULL terminate the array */
47 *bp = NULL;
48
49 return arguments;
50 }