source: trunk/third/sysinfo/var.c @ 12269

Revision 12269, 4.3 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12268, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (c) 1992-1998 Michael A. Cooper.
3 * This software may be freely used and distributed provided it is not
4 * sold for profit or used in part or in whole for commercial gain
5 * without prior written agreement, and the author is credited
6 * appropriately.
7 */
8
9#ifndef lint
10static char *RCSid = "$Revision: 1.1.1.3 $";
11#endif
12
13/*
14 * Variable functions
15 */
16
17#include "defs.h"
18
19/*
20 * Get Variable Values
21 */
22extern char *VarGetVal(Variable, Params)
23    char                       *Variable;
24    Opaque_t                    Params;
25    /*ARGSUSED*/
26{
27    static char                 OSnameBuf[100];
28    static char                 OSverBuf[100];
29    static char                 OSmajverBuf[100];
30    static char                 KArchBuf[100];
31    register char              *Value;
32    register char              *cp;
33    register char              *cp2;
34    extern char                *CurrentConfDir;
35
36    if (EQ(Variable, "OSname")) {
37        if (Value = GetOSName()) {
38            /*
39             * Copy Value into OSnameBuf while skipping '/' and
40             * lower casing
41             */
42            for (cp = Value, cp2 = OSnameBuf;
43                 cp && *cp && cp2 < &OSnameBuf[sizeof(OSnameBuf)]; ++cp)
44                if (*cp != '/')
45                    *cp2++ = tolower(*cp);
46            return(OSnameBuf);
47        }
48    } else if (EQ(Variable, "OSver")) {
49        if (Value = GetOSVer()) {
50            (void) strcpy(OSverBuf, Value);
51            strtolower(OSverBuf);
52            return(OSverBuf);
53        }
54    } else if (EQ(Variable, "OSmajver")) {
55        if (Value = GetOSVer()) {
56            (void) strcpy(OSmajverBuf, Value);
57            if (cp = strchr(OSmajverBuf, '.'))
58                *cp = CNULL;
59            strtolower(OSmajverBuf);
60            return(OSmajverBuf);
61        }
62    } else if (EQ(Variable, "KArch")) {
63        if (Value = GetKernArch()) {
64            (void) strcpy(KArchBuf, Value);
65            if (cp = strchr(KArchBuf, '.'))
66                *cp = CNULL;
67            strtolower(KArchBuf);
68            return(KArchBuf);
69        }
70    } else if (EQ(Variable, "ConfDir")) {
71        return(CurrentConfDir);
72    } else if (EQ(Variable, "DefConfFile")) {
73        return(DEFAULT_CONFIG_FILE);
74    }
75
76    return((char *) NULL);
77}
78
79/*
80 * Substitute variables in String and return result.
81 */
82extern char *VarSub(String, ErrBuff, ErrBuffLen, GetVarFunc, VarParams)
83    char                       *String;
84    char                       *ErrBuff;
85    size_t                      ErrBuffLen;
86    char                     *(*GetVarFunc)();
87    Opaque_t                    VarParams;
88{
89    static char                 Buf[BUFSIZ];
90    static char                 Var[BUFSIZ];
91    register char              *BufPtr;
92    register char              *StrPtr;
93    register char              *Value;
94    int                         ValLen;
95    int                         BufLen;
96    char                       *Start;
97    char                        EndChar;
98
99    Buf[0] = CNULL;
100    for (BufPtr = Buf, StrPtr = String; StrPtr && *StrPtr; ) {
101        if (*StrPtr == '$') {
102            ++StrPtr;
103
104            /* Look at start and end */
105            EndChar = CNULL;
106            if (*StrPtr == '{')
107                EndChar = '}';
108            if (*StrPtr == '(')
109                EndChar = ')';
110            if (!EndChar) {
111                (void) snprintf(ErrBuff, ErrBuffLen,
112                               "Variables must start with `(' or `{': `%s'",
113                               String);
114                return((char *) NULL);
115            }
116            ++StrPtr;
117            if (!StrPtr || !*StrPtr) {
118                (void) snprintf(ErrBuff, ErrBuffLen,
119                                "Empty variable: `%s'", String);
120                return((char *) NULL);
121            }
122
123            /* Set the Start and find the end of the variable */
124            Start = StrPtr;
125            for ( ; StrPtr && *StrPtr && *StrPtr != EndChar; ++StrPtr);
126            if (!StrPtr || !*StrPtr) {
127                (void) snprintf(ErrBuff, ErrBuffLen,
128                               "Variable `%s' has no ending `%c' character",
129                               Start, EndChar);
130                return((char *) NULL);
131            }
132
133            /* Extract Variable name */
134            if (StrPtr - Start >= sizeof(Var)) {
135                (void) snprintf(ErrBuff, sizeof(ErrBuff), 
136                       "Variable name is too long `%s' (maximum is %d)",
137                               Start, sizeof(Var));
138                return((char *) NULL);
139            }
140            (void) strncpy(Var, Start, StrPtr - Start);
141            Var[StrPtr - Start] = CNULL;
142
143            /* Get the Variable's Value */
144            Value = (*GetVarFunc)(Var, VarParams);
145            if (!Value) {
146                (void) snprintf(ErrBuff, ErrBuffLen,
147                                "Invalid variable name: `%s'", Var);
148                return((char *) NULL);
149            }
150
151            /* Append Value to Buf */
152            ValLen = strlen(Value);
153            BufLen = strlen(Buf);
154            if (ValLen + BufLen + 1 >= sizeof(Buf)) {
155                (void) snprintf(ErrBuff, ErrBuffLen,
156                                "Variable buffer size too small.");
157                return((char *) NULL);
158            }
159            (void) strncat(Buf, Value, ValLen);
160
161            /* Fix up pointers */
162            BufLen += ValLen;
163            BufPtr = &Buf[BufLen];
164            ++StrPtr;
165        } else
166            *BufPtr++ = *StrPtr++;
167        *BufPtr = CNULL;
168    }
169
170    if (!Buf[0])
171        (void) snprintf(ErrBuff, ErrBuffLen, "Empty buffer");
172
173    return( (Buf[0]) ? Buf : (char *) NULL );
174}
Note: See TracBrowser for help on using the repository browser.