Initial revision
[mmh] / sbr / getarguments.c
1
2 /*
3  * getarguments.c -- Get the argument vector ready to go.
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10 char **
11 getarguments (char *invo_name, int argc, char **argv, int check_context)
12 {
13     char *cp, **ap, **bp, **arguments;
14     int n = 0;
15
16     /*
17      * Check if profile/context specifies any arguments
18      */
19     if (check_context && (cp = context_find (invo_name))) {
20         cp = getcpy (cp);               /* make copy    */
21         ap = brkstring (cp, " ", "\n"); /* split string */
22
23         /* Count number of arguments split */
24         bp = ap;
25         while (*bp++)
26             n++;
27     }
28
29     if (!(arguments = (char **) malloc ((argc + n) * sizeof(*arguments))))
30         adios (NULL, "unable to malloc argument storage");
31     bp = arguments;
32
33     /* Copy any arguments from profile/context */
34     if (n > 0) {
35         while (*ap)
36             *bp++ = *ap++;
37      }
38
39     /* Copy arguments from command line */
40     argv++;
41     while (*argv)
42         *bp++ = *argv++;
43
44     /* Now NULL terminate the array */
45     *bp = NULL;
46
47     return arguments;
48 }