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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* clippy (CLI preparator in python) main executable
* Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
*/
#include "config.h"
#include <Python.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include "getopt.h"
#include "command_graph.h"
#include "clippy.h"
#if PY_VERSION_HEX >= 0x03080000
/* new python init/config API added in Python 3.8 */
int main(int argc, char **argv)
{
PyStatus status;
PyPreConfig preconfig[1];
PyConfig config[1];
PyPreConfig_InitPythonConfig(preconfig);
preconfig->configure_locale = 0;
preconfig->coerce_c_locale = 1;
preconfig->coerce_c_locale_warn = 0;
preconfig->isolated = 0;
preconfig->utf8_mode = 1;
preconfig->parse_argv = 0;
status = Py_PreInitializeFromBytesArgs(preconfig, argc, argv);
if (PyStatus_Exception(status))
Py_ExitStatusException(status);
PyConfig_InitPythonConfig(config);
#if PY_VERSION_HEX >= 0x030b0000 /* 3.11 */
config->safe_path = 0;
#endif
status = PyConfig_SetBytesArgv(config, argc, argv);
if (PyStatus_Exception(status))
Py_ExitStatusException(status);
PyConfig_SetBytesString(config, &config->program_name,
argc > 0 ? argv[0] : "clippy");
if (argc > 1)
PyConfig_SetBytesString(config, &config->run_filename, argv[1]);
PyImport_AppendInittab("_clippy", command_py_init);
status = Py_InitializeFromConfig(config);
if (PyStatus_Exception(status))
Py_ExitStatusException(status);
PyConfig_Clear(config);
return Py_RunMain();
}
#else /* Python < 3.8 */
/* old python init/config API, deprecated in Python 3.11 */
#if PY_MAJOR_VERSION >= 3
#define pychar wchar_t
static wchar_t *wconv(const char *s)
{
size_t outlen = s ? mbstowcs(NULL, s, 0) : 0;
wchar_t *out = malloc((outlen + 1) * sizeof(wchar_t));
if (outlen > 0)
mbstowcs(out, s, outlen);
out[outlen] = 0;
return out;
}
#else
#define pychar char
#define wconv(x) x
#endif
int main(int argc, char **argv)
{
pychar **wargv;
#if PY_VERSION_HEX >= 0x03040000 /* 3.4 */
Py_SetStandardStreamEncoding("UTF-8", NULL);
#endif
wchar_t *name = wconv(argv[0]);
Py_SetProgramName(name);
PyImport_AppendInittab("_clippy", command_py_init);
Py_Initialize();
wargv = malloc(argc * sizeof(pychar *));
for (int i = 1; i < argc; i++)
wargv[i - 1] = wconv(argv[i]);
PySys_SetArgv(argc - 1, wargv);
const char *pyfile = argc > 1 ? argv[1] : NULL;
FILE *fp;
if (pyfile) {
fp = fopen(pyfile, "r");
if (!fp) {
fprintf(stderr, "%s: %s\n", pyfile, strerror(errno));
free(name);
return 1;
}
} else {
fp = stdin;
char *ver = strdup(Py_GetVersion());
char *cr = strchr(ver, '\n');
if (cr)
*cr = ' ';
fprintf(stderr, "clippy interactive shell\n(Python %s)\n", ver);
free(ver);
PyRun_SimpleString(
"import rlcompleter, readline\n"
"readline.parse_and_bind('tab: complete')");
}
if (PyRun_AnyFile(fp, pyfile)) {
if (PyErr_Occurred())
PyErr_Print();
free(name);
return 1;
}
Py_Finalize();
#if PY_MAJOR_VERSION >= 3
for (int i = 1; i < argc; i++)
free(wargv[i - 1]);
#endif
free(name);
free(wargv);
return 0;
}
#endif /* Python < 3.8 */
/* and now for the ugly part... provide simplified logging functions so we
* don't need to link libzebra (which would be a circular build dep) */
#include "log.h"
PRINTFRR(3, 0)
void vzlogx(const struct xref_logmsg *xref, int prio,
const char *format, va_list args)
{
vfprintf(stderr, format, args);
fputs("\n", stderr);
}
void memory_oom(size_t size, const char *name)
{
abort();
}
|