~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to .pc/debian-changes/src/inter.c

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2014-04-07 15:43:04 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20140407154304-21r03zdnfc571kz0
Tags: 1.0.17-1
* Take over package from pkg-gnome
* New upstream release. Closes: #716731
* Bump standards version.
* Update Vcs fields to Git.
* Move to single-debian-patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GENIUS Calculator
 
2
 * Copyright (C) 1997-2011 Jiri (George) Lebl
 
3
 *
 
4
 * Author: Jiri (George) Lebl
 
5
 *
 
6
 * This file is part of Genius.
 
7
 *
 
8
 * Genius is free software: you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation, either version 3 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <glib.h>
 
28
#include <ctype.h>
 
29
#include <readline/readline.h>
 
30
#include <readline/history.h>
 
31
 
 
32
#include "calc.h"
 
33
#include "dict.h"
 
34
#include "plugin.h"
 
35
 
 
36
#include "inter.h"
 
37
 
 
38
static int toplevelokg = TRUE;
 
39
 
 
40
static int
 
41
ok_for_top(char *s)
 
42
{
 
43
        char *t = g_strstrip(g_strdup(s));
 
44
        if(strncmp(t,"plugin",strlen(t))==0 ||
 
45
           strncmp(t,"load",strlen(t))==0 ||
 
46
           strncmp(t,"cd",strlen(t))==0 ||
 
47
           strncmp(t,"pwd",strlen(t))==0 ||
 
48
           strncmp(t,"ls",strlen(t))==0) {
 
49
                g_free(t);
 
50
                return TRUE;
 
51
        } else {
 
52
                g_free(t);
 
53
                return FALSE;
 
54
        }
 
55
}
 
56
 
 
57
GelETree *
 
58
get_p_expression(void)
 
59
{
 
60
        GString *gs;
 
61
        char *prompt = "genius> ";
 
62
        
 
63
        gel_interrupted = FALSE;
 
64
        
 
65
        gs = g_string_new("");
 
66
        
 
67
        for(;;) {
 
68
                int finished;
 
69
                char *s;
 
70
                GelETree *ret;
 
71
                int oldtop = toplevelokg;
 
72
 
 
73
                toplevelokg = ok_for_top(gs->str);
 
74
                s = readline(prompt);
 
75
                toplevelokg = oldtop;
 
76
                
 
77
                if(gel_interrupted) {
 
78
                        g_string_free(gs,TRUE);
 
79
                        if(s) free(s);
 
80
                        return NULL;
 
81
                }
 
82
 
 
83
                prompt = "      > ";
 
84
                if(!s) {
 
85
                        gel_got_eof = TRUE;
 
86
                        g_string_append_c(gs,'\n');
 
87
                        ret = gel_parseexp(gs->str, NULL, TRUE, FALSE, NULL, NULL);
 
88
                        g_string_free(gs,TRUE);
 
89
                        return ret;
 
90
                }
 
91
                if(!*s) {
 
92
                        free(s);
 
93
                        continue;
 
94
                }
 
95
                add_history(s);
 
96
                g_string_append(gs,s);
 
97
                free(s);
 
98
                g_string_append_c(gs,'\n');
 
99
                
 
100
                ret = gel_parseexp(gs->str, NULL, TRUE, TRUE, &finished, NULL);
 
101
                if (gel_got_eof)
 
102
                        gel_got_eof = FALSE;
 
103
                if(finished) {
 
104
                        g_string_free(gs,TRUE);
 
105
                        return ret;
 
106
                }
 
107
        }
 
108
}
 
109
 
 
110
static GString *p_expr = NULL;
 
111
static int old_toplevelokg = FALSE;
 
112
void (*got_expr_func)(GelETree *) = NULL;
 
113
 
 
114
static void
 
115
write_all_state_to_rl(FILE *fp)
 
116
{
 
117
        GSList *li;
 
118
        int count;
 
119
        char *s;
 
120
 
 
121
        li = d_getcontext();
 
122
        count = 0;
 
123
        for(li=d_getcontext();li;li=li->next) {
 
124
                GelEFunc *f = li->data;
 
125
                if(!f->id || !f->id->token)
 
126
                        continue;
 
127
                count++;
 
128
        }
 
129
        fprintf(fp,"FUNCTIONS %d\n",count);
 
130
        for(li=d_getcontext();li;li=li->next) {
 
131
                GelEFunc *f = li->data;
 
132
                if(!f->id || !f->id->token)
 
133
                        continue;
 
134
                fprintf(fp,"%s\n",f->id->token);
 
135
        }
 
136
 
 
137
        count = 0;
 
138
        for(li = gel_plugin_list;li;li=li->next) {
 
139
                GelPlugin *plg = li->data;
 
140
                if(!plg->base)
 
141
                        continue;
 
142
                count++;
 
143
        }
 
144
        fprintf(fp,"PLUGINS %d\n",count);
 
145
        for(li = gel_plugin_list;li;li=li->next) {
 
146
                GelPlugin *plg = li->data;
 
147
                if(!plg->base)
 
148
                        continue;
 
149
                fprintf(fp,"%s\n",plg->base);
 
150
        }
 
151
 
 
152
        s = g_get_current_dir ();
 
153
        fprintf (fp, "CWD %s\n", s);
 
154
        g_free (s);
 
155
 
 
156
        if(toplevelokg)
 
157
                fprintf(fp,"TOPLEVEL OK\n");
 
158
        else
 
159
                fprintf(fp,"TOPLEVEL NOT OK\n");
 
160
 
 
161
        fflush(fp);
 
162
}
 
163
 
 
164
void
 
165
get_cb_p_expression(char *s, FILE *torlfp)
 
166
{
 
167
        int finished;
 
168
        GelETree *ret;
 
169
        /*             "genius> "*/
 
170
        char *prompt = "      > ";
 
171
        toplevelokg = old_toplevelokg;
 
172
 
 
173
        if(gel_interrupted) {
 
174
                prompt = "\001\e[1m\002genius>\001\e[0m\002 ";
 
175
                gel_interrupted = FALSE;
 
176
                if(p_expr) g_string_free(p_expr,TRUE);
 
177
                p_expr = NULL;
 
178
                goto done_with_get;
 
179
        }
 
180
 
 
181
        if(!s) {
 
182
                gel_got_eof = TRUE;
 
183
                g_string_append_c(p_expr, '\n');
 
184
                ret = gel_parseexp(p_expr->str, NULL, TRUE, FALSE, NULL, NULL);
 
185
                g_string_free(p_expr, TRUE);
 
186
                p_expr = NULL;
 
187
                (*got_expr_func)(ret);
 
188
                gel_interrupted = FALSE;
 
189
                prompt = "\001\e[1m\002genius>\001\e[0m\002 ";
 
190
                goto done_with_get;
 
191
        }
 
192
        if(!*s) {
 
193
                goto done_with_get;
 
194
        }
 
195
        add_history(s);
 
196
        g_string_append(p_expr,s);
 
197
        g_string_append_c(p_expr,'\n');
 
198
 
 
199
        ret = gel_parseexp(p_expr->str, NULL, TRUE, TRUE, &finished, NULL);
 
200
        if (gel_got_eof)
 
201
                gel_got_eof = FALSE;
 
202
        if(finished) {
 
203
                g_string_free(p_expr,TRUE);
 
204
                p_expr = NULL;
 
205
                (*got_expr_func)(ret);
 
206
                gel_interrupted = FALSE;
 
207
                prompt = "\001\e[1m\002genius>\001\e[0m\002 ";
 
208
        }
 
209
done_with_get:
 
210
        if(!p_expr) p_expr = g_string_new("");
 
211
        old_toplevelokg = toplevelokg;
 
212
        toplevelokg = ok_for_top(p_expr->str);
 
213
 
 
214
        write_all_state_to_rl(torlfp);
 
215
        fprintf(torlfp,"READLINE %s\n",prompt);
 
216
        fflush(torlfp);
 
217
}
 
218
 
 
219
void
 
220
start_cb_p_expression(void (*get_func)(GelETree *), FILE *torlfp)
 
221
{
 
222
        gel_interrupted = FALSE;
 
223
        
 
224
        gel_rewind_file_info();
 
225
        
 
226
        if(p_expr) g_string_free(p_expr,TRUE);
 
227
        p_expr = g_string_new("");
 
228
        
 
229
        got_expr_func = get_func;
 
230
        
 
231
        old_toplevelokg = toplevelokg;
 
232
        toplevelokg = ok_for_top(p_expr->str);
 
233
 
 
234
        write_all_state_to_rl(torlfp);
 
235
        fprintf(torlfp,"READLINE \001\e[1m\002genius>\001\e[0m\002 \n");
 
236
        fflush(torlfp);
 
237
}
 
238
 
 
239
void
 
240
stop_cb_p_expression(void)
 
241
{
 
242
        if(p_expr) g_string_free(p_expr,TRUE);
 
243
        p_expr = NULL;
 
244
 
 
245
        toplevelokg = old_toplevelokg;
 
246
        
 
247
        got_expr_func = NULL;
 
248
}
 
249
 
 
250
 
 
251
 
 
252
static int addtoplevels = TRUE;
 
253
extern const char *genius_toplevels[];
 
254
extern const char *genius_operators[];
 
255
 
 
256
 
 
257
static char *
 
258
command_generator (const char *text, int state)
 
259
{
 
260
        static int oi,ti,len;
 
261
        static GSList *fli;
 
262
 
 
263
        if(!state) {
 
264
                oi = 0;
 
265
                if(addtoplevels)
 
266
                        ti = 0;
 
267
                else
 
268
                        ti = -1;
 
269
                len = strlen (text);
 
270
                fli = d_getcontext();
 
271
        }
 
272
        
 
273
        while(ti>=0 && genius_toplevels[ti]) {
 
274
                const char *s = genius_toplevels[ti++];
 
275
                if(strncmp(s,text,len)==0)
 
276
                        return strdup(s);
 
277
        }
 
278
 
 
279
        while(genius_operators[oi]) {
 
280
                const char *s = genius_operators[oi++];
 
281
                if(strncmp(s,text,len)==0)
 
282
                        return strdup(s);
 
283
        }
 
284
 
 
285
        while(fli) {
 
286
                GelEFunc *f = fli->data;
 
287
                fli = g_slist_next(fli);
 
288
                if(!f->id || !f->id->token)
 
289
                        continue;
 
290
                if(strncmp(f->id->token,text,len)==0)
 
291
                        return strdup(f->id->token);
 
292
        }
 
293
 
 
294
        return NULL;
 
295
}
 
296
 
 
297
static char *
 
298
plugin_generator (const char *text, int state)
 
299
{
 
300
        static int len;
 
301
        static GSList *li;
 
302
 
 
303
        if(!state) {
 
304
                len = strlen (text);
 
305
                li = gel_plugin_list;
 
306
        }
 
307
 
 
308
        while(li) {
 
309
                GelPlugin *plg = li->data;
 
310
                li = g_slist_next(li);
 
311
                if(!plg->base)
 
312
                        continue;
 
313
                if(strncmp(plg->base,text,len)==0)
 
314
                        return strdup(plg->base);
 
315
        }
 
316
 
 
317
        return NULL;
 
318
}
 
319
 
 
320
/* Note: keep in sync with genius-readline-helper.c */
 
321
/* FIXME: make this common */
 
322
static char **
 
323
tab_completion (const char *text, int start, int end)
 
324
{
 
325
        char *p;
 
326
        for(p=rl_line_buffer;*p==' ' || *p=='\t';p++)
 
327
                ;
 
328
        if(toplevelokg &&
 
329
           (strncmp(p,"load ",5)==0 ||
 
330
            strncmp(p,"load\t",5)==0)) {
 
331
                return NULL;
 
332
        }
 
333
        if(toplevelokg &&
 
334
           (strncmp(p,"ls ",3)==0 ||
 
335
            strncmp(p,"ls\t",3)==0)) {
 
336
                return NULL;
 
337
        }
 
338
        if(toplevelokg &&
 
339
           (strncmp(p,"cd ",3)==0 ||
 
340
            strncmp(p,"cd\t",3)==0)) {
 
341
                return NULL;
 
342
        }
 
343
 
 
344
        if(toplevelokg &&
 
345
           (strncmp(p,"plugin ",7)==0 ||
 
346
            strncmp(p,"plugin\t",7)==0)) {
 
347
                return rl_completion_matches (text, plugin_generator);
 
348
        }
 
349
        
 
350
        if(toplevelokg &&
 
351
           (!*p ||
 
352
            strncmp(p,"load",strlen(p))==0 ||
 
353
            strncmp(p,"cd",strlen(p))==0 ||
 
354
            strncmp(p,"ls",strlen(p))==0 ||
 
355
            strncmp(p,"pwd",strlen(p))==0 ||
 
356
            strncmp(p,"help",strlen(p))==0 ||
 
357
            strncmp(p,"plugin",strlen(p))==0))
 
358
                addtoplevels = TRUE;
 
359
        else
 
360
                addtoplevels = FALSE;
 
361
 
 
362
        return rl_completion_matches (text, command_generator);
 
363
}
 
364
 
 
365
void
 
366
init_inter(void)
 
367
{
 
368
        rl_readline_name = "Genius";
 
369
        rl_attempted_completion_function =
 
370
                (CPPFunction *)tab_completion;
 
371
}