File: parsetest.cpp

package info (click to toggle)
ccbuild 2.0.7%2Bgit20160227.c1179286-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,692 kB
  • sloc: cpp: 5,211; ansic: 1,913; sh: 260; makefile: 26
file content (247 lines) | stat: -rw-r--r-- 5,492 bytes parent folder | download | duplicates (5)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
  This file is part of ccbuild.
  Copyright (C) 2013  A. Bram Neijt <bneijt@gmail.com>

  ccbuild is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  ccbuild is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with ccbuild.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>


#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iterator>
#include <getopt.h>

#define DEBUGLEVEL 3


#define debugLevel2(x) cerr << x << endl;
#define debugLevel4(x) cerr << x << endl;

//Timeofday
#include <sys/time.h>
#include <time.h>
using namespace std;


int main(int argcbbb, char *argvbbb[])
{
	//Use getopt
	while(getopt(argcbbb, argvbbb, "l:b:") != EOF);
	               static struct option long_options[] = {
                   {0, 0, 0, 0}
               };
	//Use getopt_long
	int index;
	while(getopt_long(argcbbb, argvbbb, "l:b:", long_options, &index) != EOF);

	//Normally class members or function arguments

  string argument("-lstdc++ -lpng -lz");
  vector<string> d_link, d_compile;

  //Start of function code

	debugLevel2("Adding argument: " << argument);

	//Trim the string
	string::size_type pos = argument.find_first_not_of(" \t");
	if(pos != string::npos)
		argument = argument.erase(0, pos);

	pos = argument.find_last_not_of(" \t");
	if(pos != string::npos && pos + 1 < argument.size())
		argument = argument.erase(pos + 1);

	//TODO: BASH evaluate and parse arguments!!!
	//Store argument as been seen

	//TODO: BASH evaluate all substrings (like `echo -- -lpng`)!!

	//Create argc, argv
	char skipping = 0;

	//memcpy argument.c_str naar text;
	unsigned textSize = argument.size() +1;
	char *text = new char[textSize];	//For the zero ending ;-)
	strncpy(text, argument.c_str(), textSize);
	debugLevel2("Argument text: " << text);
	//for(unsigned i = 0; i < argument.size(); ++i)
	//	argv[i] = 0;

	unsigned argc = 0;

	for(unsigned i=0; i < argument.size(); ++i)
	{
		//Skip quoted strings
		//SHOULD STILL FIND AND EXPAND QUOTED STRINGS
		if(skipping == 0)
		if(argument[i] == '"' || argument[i] == '\'' || argument[i] == '`')
		{
			skipping = argument[i];
			continue;
		}
		if(argument[i] == '\\')
		{
			//TODO: Escapes are possible and allowed???
			++i;
			continue;
		}
		if(argument[i] == skipping)
		{
			skipping = 0;
			continue;
		}

		if(skipping != 0)
			continue;

		if(argument[i] == ' ')
		{
			text[i] = '\0';
			++argc;
		}
	}
	++argc;

	unsigned argcc = 1;
	char **argv = new char* [argc +1];
	argv[argc] = 0;	//Terminating zero, needed????

	argv[0] = &text[0];
	for(unsigned i=0; i < textSize -1; ++i)
	{
		if(text[i] == '\0')
		{
			++i;
			argv[argcc] = &text[i];
			argcc++;
		}
	}

	//TEST CODE
	debugLevel2("Argc: " << argc);
	for(unsigned i=0; i < argc; ++i)
			debugLevel2("argv[" << i << "] \'" << argv[i] << "\'");


	/*
		Include options:
			-I
			-include
		Linker options:
			-L (directory serach path)
			-l
	ETC!
	*/

	bool done = false;
	while(!done)
	{
		string option;
		//TEST CODE
		debugLevel2("Argc: " << argc);
		for(unsigned i=0; i < argc; ++i)
				debugLevel2("argv[" << i << "] \'" << argv[i] << "\'");

		int c = getopt(argc, argv, "I:i:l:L:D:");

		if(c <= 0xFF)
		{
			option = "-";
			option += static_cast<char>(c);
			option += (optarg ? optarg : "");
		}

    switch (c)
    {
      case '?':
      	cerr << "Unknown option: \'" << optopt << "\'\n";
      	done = true;
      	break;
      case ':':
				cerr << "Missing option value\n";
				done = true;
        break;
    	case EOF:
    		//Hmmm... left with a fes options... dunno what to do here yet
      	//copy(argv + optind, argv + argc, back_inserter(d_argv));
      	done = true;
        break;
      //Compiler options
			case 'I':
				debugLevel2("Add compile option: " << option << "\n");
				if(find(d_compile.begin(), d_compile.end(), option) == d_compile.end())
					d_compile.push_back(option);
      	break;
			case 'D':
				debugLevel2("Add compile option: " << option << "\n");
				if(find(d_compile.begin(), d_compile.end(), option) == d_compile.end())
					d_compile.push_back(option);
      	break;
			case 'i':
				//-include e.a.
				debugLevel2("Add compile option: " << option << "\n");
				if(find(d_compile.begin(), d_compile.end(), option) == d_compile.end())
					d_compile.push_back(option);
      	break;

      //Default to linker options!
			default:
				debugLevel2("Add link option: " << option << "\n");
				if(find(d_link.begin(), d_link.end(), option) == d_link.end())
					d_link.push_back(option);
				break;
		}
	}

	/* DEPRECATED include and libs parsing
  //Parse argument and addLibrary addInclude etc.
  if(argument.size() > 2)
  {
		//cout << "Adding argument: " << argument << "\n";
    if(argument[1] == 'I')
      d_includes[argument.substr(2)] = true;
    if(argument[1] == 'l')
      d_libs[argument.substr(2)] = true;
  }
  */


  delete[] text;
	delete[] argv;

  debugLevel4("leaving function");












}