-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.c
More file actions
175 lines (155 loc) · 4.45 KB
/
variable.c
File metadata and controls
175 lines (155 loc) · 4.45 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* SPDX-License-Identifier: BSD-3-Clause-Clear
* https://spdx.org/licenses/BSD-3-Clause-Clear.html#licenseText
*
* Copyright (c) 2020-1025 Arvind Sajeev (arvind.sajeev@gmail.com)
* All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "edpat.h"
#include "scripts.h"
#include "utils.h"
#include "variable.h"
#include "print.h"
typedef struct {
char *varName;
char *varValue;
} VAR_INFO;
static VAR_INFO VarList[MAX_VAR_COUNT];
static int NextFreeVarListIndex = 0;
/***********************
* VariableStoreValue()
*
* Store a variable and its value in VarList[] table
*
* Arguments : testScriptStatement - INPUT. A null terminated string
* which a the Test script statement in format
* $<varName>=<varVal>
*
* Return: - EDPAT_SUCESS or EDPAT_FAILED
*
***********************/
EDPAT_RETVAL VariableStoreValue(const char *testScriptStatement)
{
char tmp[MAX_SCRIPT_STATEMENT_LEN+1];
char *varName;
char *varValue;
int i;
/* check syntax and seperate variable name and its value
from statment */
strncpy(tmp,&testScriptStatement[1],MAX_SCRIPT_STATEMENT_LEN-1);
tmp[MAX_SCRIPT_STATEMENT_LEN]=0;
varName = tmp;
varValue = strchr(tmp,'=');
if (NULL == varValue)
{
ScriptErrorMsgPrint("Expecting the format $vaname=value");
return EDPAT_FAILED;
}
/* cz this is the point where the = sign is found puttinh a 0
makes it two different strings */
varValue[0] = 0; // null teminate varName;
varValue++; // skil '='
TrimStr(varName);
TrimStr(varValue);
/* check variable is already existing. If yes, overwrite value
log this in logfile if in verbose mode */
for(i=0; i < NextFreeVarListIndex; i++)
{
if (0 == strcmp(varName,VarList[i].varName))
{
VerboseStringPrint("Variable '%s' value '%s' is "
" replace with new value '%s'.",
varName,
VarList[i].varValue,
varValue);
free(VarList[i].varValue);
VarList[i].varValue = malloc(strlen(varValue)+1);
strcpy(VarList[i].varValue,varValue);
return EDPAT_SUCCESS;
}
}
// value does not exits. So create a new entry
if (MAX_VAR_COUNT <= (NextFreeVarListIndex+1))
{
ScriptErrorMsgPrint("Too many variables %d defined."
"only %d are allowed",
NextFreeVarListIndex, MAX_VAR_COUNT);
return EDPAT_FAILED;
}
VarList[NextFreeVarListIndex].varName = malloc(strlen(varName)+1);
VarList[NextFreeVarListIndex].varValue = malloc(strlen(varValue)+1);
strcpy(VarList[NextFreeVarListIndex].varName,varName);
strcpy(VarList[NextFreeVarListIndex].varValue,varValue);
NextFreeVarListIndex++;
VerboseStringPrint("Variable '%s' with value '%s' added.",
varName,varValue);
VariablePrintValues();
return EDPAT_SUCCESS;
}
/***********************
* VariableGetValue()
*
* Get value of a varible from the table.
*
* Arguments : varName - Name for the variable for which value
* is needed.
*
* Return : value of the variable. NULL if not found.
*
**********************/
char *VariableGetValue(const char *varName)
{
int i;
for(i=0; i < NextFreeVarListIndex; i++)
{
if (0 == strcmp(VarList[i].varName,varName))
{
return VarList[i].varValue;
}
}
return NULL;
}
/***********************
* VariablePrintValues()
*
* Print variable name and its value on the screent.
*
* Arguments : void
*
* Return : void
*
***********************/
void VariablePrintValues(void)
{
int i;
static char msg[MAX_SCRIPT_STATEMENT_LEN+1];
static char var[MAX_SCRIPT_LINE_LEN+1];
strncpy(msg,"Variables Stored:",MAX_SCRIPT_STATEMENT_LEN);
msg[MAX_SCRIPT_STATEMENT_LEN]=0;
for(i=0; i < NextFreeVarListIndex; i++)
{
sprintf(var,"\n%s='%s'",
VarList[i].varName,
VarList[i].varValue);
if ( MAX_SCRIPT_STATEMENT_LEN > (strlen(msg)+strlen(var)))
{
strcat(msg,var);
}
else
{
VerboseStringPrint(msg);
strncpy(msg,"Variables Stored(Next set):",
MAX_SCRIPT_LINE_LEN);
msg[MAX_SCRIPT_STATEMENT_LEN]=0;
}
}
VerboseStringPrint(msg);
return;
}