]>
Commit | Line | Data |
---|---|---|
29ad6f68 DL |
1 | /* |
2 | * clippy (CLI preparator in python) main executable | |
3 | * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include "config.h" | |
21 | #include <Python.h> | |
22 | #include <string.h> | |
23 | #include <stdlib.h> | |
24 | #include <wchar.h> | |
25 | #include "getopt.h" | |
26 | ||
27 | #include "command_graph.h" | |
28 | #include "clippy.h" | |
29 | ||
30 | #if PY_MAJOR_VERSION >= 3 | |
31 | #define pychar wchar_t | |
32 | static wchar_t *wconv(const char *s) | |
33 | { | |
45ec351d | 34 | size_t outlen = s ? mbstowcs(NULL, s, 0) : 0; |
29ad6f68 | 35 | wchar_t *out = malloc((outlen + 1) * sizeof(wchar_t)); |
45ec351d | 36 | |
37 | if (outlen > 0) | |
38 | mbstowcs(out, s, outlen); | |
29ad6f68 DL |
39 | out[outlen] = 0; |
40 | return out; | |
41 | } | |
42 | #else | |
43 | #define pychar char | |
44 | #define wconv(x) x | |
45 | #endif | |
46 | ||
47 | int main(int argc, char **argv) | |
48 | { | |
49 | pychar **wargv; | |
50 | ||
51 | #if PY_VERSION_HEX >= 0x03040000 /* 3.4 */ | |
52 | Py_SetStandardStreamEncoding("UTF-8", NULL); | |
53 | #endif | |
0f307338 | 54 | wchar_t *name = wconv(argv[0]); |
4da4b9d4 | 55 | Py_SetProgramName(name); |
29ad6f68 DL |
56 | PyImport_AppendInittab("_clippy", command_py_init); |
57 | ||
58 | Py_Initialize(); | |
59 | ||
60 | wargv = malloc(argc * sizeof(pychar *)); | |
61 | for (int i = 1; i < argc; i++) | |
62 | wargv[i - 1] = wconv(argv[i]); | |
63 | PySys_SetArgv(argc - 1, wargv); | |
64 | ||
65 | const char *pyfile = argc > 1 ? argv[1] : NULL; | |
66 | FILE *fp; | |
67 | if (pyfile) { | |
68 | fp = fopen(pyfile, "r"); | |
69 | if (!fp) { | |
70 | fprintf(stderr, "%s: %s\n", pyfile, strerror(errno)); | |
f52aee04 DS |
71 | |
72 | free(name); | |
29ad6f68 DL |
73 | return 1; |
74 | } | |
75 | } else { | |
76 | fp = stdin; | |
77 | char *ver = strdup(Py_GetVersion()); | |
78 | char *cr = strchr(ver, '\n'); | |
79 | if (cr) | |
80 | *cr = ' '; | |
81 | fprintf(stderr, "clippy interactive shell\n(Python %s)\n", ver); | |
82 | free(ver); | |
d62a17ae | 83 | PyRun_SimpleString( |
84 | "import rlcompleter, readline\n" | |
85 | "readline.parse_and_bind('tab: complete')"); | |
29ad6f68 DL |
86 | } |
87 | ||
88 | if (PyRun_AnyFile(fp, pyfile)) { | |
89 | if (PyErr_Occurred()) | |
90 | PyErr_Print(); | |
f52aee04 DS |
91 | |
92 | free(name); | |
29ad6f68 DL |
93 | return 1; |
94 | } | |
d62a17ae | 95 | Py_Finalize(); |
29ad6f68 DL |
96 | |
97 | #if PY_MAJOR_VERSION >= 3 | |
98 | for (int i = 1; i < argc; i++) | |
99 | free(wargv[i - 1]); | |
100 | #endif | |
4da4b9d4 | 101 | free(name); |
29ad6f68 DL |
102 | free(wargv); |
103 | return 0; | |
104 | } | |
105 | ||
106 | /* and now for the ugly part... provide simplified logging functions so we | |
107 | * don't need to link libzebra (which would be a circular build dep) */ | |
108 | ||
29ad6f68 | 109 | #include "log.h" |
29ad6f68 | 110 | |
131879fb DL |
111 | void vzlogx(const struct xref_logmsg *xref, int prio, |
112 | const char *format, va_list args) | |
0bdeb5e5 DL |
113 | { |
114 | vfprintf(stderr, format, args); | |
115 | fputs("\n", stderr); | |
116 | } | |
29ad6f68 | 117 | |
d62a17ae | 118 | void memory_oom(size_t size, const char *name) |
29ad6f68 DL |
119 | { |
120 | abort(); | |
121 | } |