File: getarg.c

package info (click to toggle)
icmake 6.30-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,360 kB
  • ctags: 1,415
  • sloc: ansic: 7,727; makefile: 1,465; sh: 244; asm: 126; cpp: 39
file content (78 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
\fucref{getarg}{char $*$getarg (\params)}
    {
        {int} {n} {{\em n}-th argument to retrieve}
        {int} {*increment} {address of counter increment}
    }
    {argument in string format}
    {xstrdup()}
    {}
    {getarg.c}
    {

        Function {\em getarg()} can be called to retrieve arguments from the
        stack (e.g., the arguments of a {\em print()} statement) in string
        format. Parameter {\em n} specifies the argument to retrieve: 0 is the
        last pushed argument, 1 is the argument pushed before that, etc..

        Parameter {\em increment} tells the caller whether to start retrieving
        a next argument or not. In the case of a list argument, {\em getarg()}
        sets this flag to 0 while the list is not yet completely processed.

        When a list argument is processed or when the retrieved argument is not
        a list, the flag value is set to 1.

        The return value points to a string duplicate. The caller should free
        this memory when it is no longer needed.

    }
*/

#include "builtin.ih"

char *getarg (int n, int *flag)
{
    char
        convbuf [50];
    static unsigned
        listindex;
    register char
        *ret;
    VAR_ *base = top() - n;
    *flag = 1;                              /* assume that done with args */

    if (typeValue(base) & e_int)            /* incase of an int.. */
    {
        listindex = 0;
        sprintf (convbuf, "%d", intValue(base));
        return (xstrdup (convbuf));
    }

    if (typeValue(base) & e_str)            /* incase of a string.. */
    {
        listindex = 0;
        return xstrdup(stringStr(base));
    }

                                            /* incase of a list: */
    if (!listSize(base))
    {
        listindex = 0;
        ret = xstrdup("");
    }
    else
    {
        ret = xstrdup(listAt(base, listindex));
        listindex++;
        if (listindex < listSize(base))
            *flag = 0;                      /* if more elements, not done */
        else                                /* with args.. */
            listindex = 0;                  /* otherwise: returnflag = 1, */
    }                                       /* no next elements to get */

    return (ret);
}