1 /*
2 
3 Copyright 2021, dettus@dettus.net
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12    this list of conditions and the following disclaimer in the documentation
13    and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 */
28 #include <stdio.h>
29 #include <string.h>
30 #include "configuration.h"
31 
retrievefromini(FILE * f,char * section,char * entry,char * retstring,int retstringspace)32 int retrievefromini(FILE *f,char* section,char* entry,char* retstring,int retstringspace)
33 {
34 	char line[1024];
35 	int i;
36 	int j;
37 	int l,ls,le;
38 	char lc;
39 	int state;
40 	int found;
41 	found=0;
42 	state=0;	// state 0: search for the [section]
43 			// state 1: search for the entry=
44 			// state 2: done
45 
46 	ls=strlen(section);
47 	le=strlen(entry);
48 	fseek(f,0,SEEK_SET);
49 	state=0;
50 	do
51 	{
52 		if (fgets(line,sizeof(line),f)==NULL) return 0;
53 		l=strlen(line);
54 		j=0;
55 		lc=0;
56 		// reduce the line: remove spaces, unless they are escaped.
57 		for (i=0;i<l;i++)
58 		{
59 			char c;
60 			c=line[i];
61 			if (lc=='\\' && c>=32)	// escaped
62 			{
63 				line[j++]=c;	// keep this character
64 			}
65 			else if (c==';' || c<32)
66 			{
67 				line[j++]=0;	// terminate the line here
68 			}
69 			else if (c>32 && c!='\\')
70 			{
71 				line[j++]=c;
72 			}
73 			lc=c;
74 		}
75 		line[j]=0;
76 		l=strlen(line);
77 		if (l)
78 		{
79 			if (line[0]=='[' && state==0) // this is a section, see if it is a match.
80 			{
81 				int match;
82 				match=(ls==l);	// cannot be a match
83 				for (i=0;i<l && match;i++)
84 				{
85 					if ((line[i]&0x5f)!=(section[i]&0x5f)) match=0;	// upper case search
86 				}
87 				if (match)
88 				{
89 					state=1;		// start looking for the entry
90 				}
91 			}
92 			else if (line[0]=='[' && state==1)	// this is a section, the search is over
93 			{
94 				state=2;
95 			}
96 			else if (state==1)
97 			{
98 				int match;
99 				int i;
100 				match=0;
101 				if (l>le && line[le]=='=') match=1;
102 
103 				for (i=0;i<le && match;i++)
104 				{
105 					if ((line[i]&0x5f)!=(entry[i]&0x5f)) match=0;	// upper case search
106 				}
107 				if (match && retstringspace>(l-le))
108 				{
109 					memcpy(retstring,&line[le+1],l-le);
110 					found=1;
111 				}
112 			}
113 		}
114 	}
115 	while (!feof(f) && state!=2 && !found);
116 	return found;
117 }
retrievefromcommandline(int argc,char ** argv,char * parameter,char * retstring,int retstringspace)118 int retrievefromcommandline(int argc,char** argv,char* parameter, char* retstring,int retstringspace)
119 {
120 	int i;
121 	int found;
122 	found=0;
123 	for (i=0;i<argc;i++)
124 	{
125 		if (strlen(argv[i])==strlen(parameter))
126 		{
127 			if (strncmp(argv[i],parameter,strlen(parameter))==0)
128 			{
129 				found=1;
130 				if (retstring!=NULL)
131 				{
132 					if (i==(argc-1)) found=-1;
133 					else {
134 						if (retstringspace>strlen(argv[i+1]))
135 						{
136 							memcpy(retstring,argv[i+1],strlen(argv[i+1])+1);
137 						} else found=-2;
138 					}
139 				}
140 			}
141 		}
142 	}
143 	return found;
144 }
145 
146