File: shell.cpp

package info (click to toggle)
cupt 2.10.4%2Bnmu2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,144 kB
  • sloc: cpp: 23,642; perl: 1,599; sh: 40; makefile: 19
file content (173 lines) | stat: -rw-r--r-- 4,380 bytes parent folder | download | duplicates (3)
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
/**************************************************************************
*   Copyright (C) 2010-2011 by Eugene V. Lyubimkin                        *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License                  *
*   (version 3 or above) as published by the Free Software Foundation.    *
*                                                                         *
*   This program 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 GPL                        *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
**************************************************************************/
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include <dlfcn.h>
#include <readline/readline.h>
#include <readline/history.h>

#include <cupt/cache/package.hpp>

#include "../common.hpp"
#include "../cupt.hpp"
#include "../handlers.hpp"

bool shellMode = false;

class Readline
{
	string __prompt;
	static char* (*__dl_readline)(const char*);
	static void (*__dl_add_history)(const char*);

	string __gnu_readline()
	{
		char* buffer = __dl_readline(__prompt.c_str());

		if (!buffer)
		{
			return string();
		}
		string result(buffer);
		free(buffer);

		if (!result.empty())
		{
			__dl_add_history(result.c_str());
		}

		return result;
	}

	string __simple_get_line()
	{
		string result;
		cout << __prompt;
		std::getline(cin, result);
		return result;
	}
 public:
	Readline(const string& prompt)
		: __prompt(prompt)
	{}

	string getLine()
	{
		string result;

		start:

		result = (__dl_readline && __dl_add_history) ? __gnu_readline() : __simple_get_line();

		if (result.empty())
		{
			goto start;
		}
		else if (result == "exit" || result == "quit" || result == ":q" || result == "q")
		{
			result.clear();
		}

		return result;
	}

	static void init()
	{
		auto handle = dlopen("libreadline.so.7", RTLD_NOW);
		if (!handle)
		{
			warn2(__("unable to dynamically find libreadline.so.7: dlopen: %s"), dlerror());
			return;
		}

		__dl_readline = reinterpret_cast< decltype(__dl_readline) >(dlsym(handle, "readline"));
		if (!__dl_readline)
		{
			warn2(__("unable to dynamically bind the symbol '%s': %s"), "readline", dlerror());
		}

		__dl_add_history = reinterpret_cast< decltype(__dl_add_history) >(dlsym(handle, "add_history"));
		if (!__dl_add_history)
		{
			warn2(__("unable to dynamically bind the symbol '%s': %s"), "add_history", dlerror());
		}
	}
};

char* (*Readline::__dl_readline)(const char*) = NULL;
void (*Readline::__dl_add_history)(const char*) = NULL;

void convertLineToArgcArgv(const string& cuptCommand, const string& line, int& argc, char**& argv)
{
	auto arguments = convertLineToShellArguments(line);

	argc = arguments.size() + 1;
	argv = new char*[argc];
	argv[0] = strdup(cuptCommand.c_str());
	for (int i = 1; i < argc; ++i)
	{
		argv[i] = strdup(arguments[i-1].c_str());
	}
}

void freeArgcArgv(int argc, char** argv)
{
	for (int i = 0; i < argc; ++i)
	{
		free(argv[i]);
	}
	delete [] argv;
}

int shell(Context& context)
{
	shellMode = true;

	vector< string > arguments;
	bpo::options_description noOptions;
	parseOptions(context, noOptions, arguments);
	checkNoExtraArguments(arguments);

	Readline::init();

	cout << __("This is an interactive shell of the cupt package manager.\n");

	const shared_ptr< Config > oldConfig(new Config(*(context.getConfig())));
	const string cuptCommand = context.argv[0];

	Readline term(/* prompt */ "cupt> ");
	string line;
	while (line = term.getLine(), !line.empty())
	{
		int argc;
		char** argv;

		convertLineToArgcArgv(cuptCommand, line, argc, argv);

		mainEx(argc, argv, context);

		*(context.getConfig()) = *oldConfig;
		freeArgcArgv(argc, argv);
	}

	return 0;
}