Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2000 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314/*******************************************************************/15/* */16/* TEXMACS-SPECIFIC STUFF */17/* */18/*******************************************************************/19#include "pari.h"2021#include "paripriv.h"22#include "gp.h"2324#define DATA_BEGIN ((char) 2)25#define DATA_END ((char) 5)26#define DATA_ESCAPE ((char) 27)2728/*******************************************************************/29/* */30/* READLINE INTERFACE */31/* */32/*******************************************************************/33#ifdef READLINE34static pari_rl_interface pari_rl;35#endif36static int did_complete = 0;3738#ifdef READLINE39BEGINEXTERN40#include <readline/readline.h>41ENDEXTERN4243static void44print_escape_string(char *s)45{46long l = strlen(s);47char *t, *t0 = (char*)pari_malloc(l * 3 + 3);4849t = t0; *t++ = '"';50for ( ;*s; *t++ = *s++)51switch(*s)52{53case DATA_BEGIN:54case DATA_END:55case DATA_ESCAPE: *t++ = DATA_ESCAPE; continue;5657case '\\':58case '"': *t++ = '\\'; continue;59}60*t++ = '"';61*t = '\0'; puts(t0); pari_free(t0);62}6364/* completion required, cursor on s + pos. Complete wrt strict left prefix */65static void66tm_completion(const char *s, long pos)67{68char **matches, *text;69long w;7071matches = pari_completion_matches(&pari_rl, s, pos, &w);72text = *pari_rl.line_buffer + w;73printf("%cscheme:(tuple",DATA_BEGIN);74if (matches)75{76long i, prelen = (rl_line_buffer+pos) - text;77char *t = (char*)pari_malloc(prelen+1);78strncpy(t, text, prelen); t[prelen] = 0; /* prefix */79printf(" ");80print_escape_string(t); pari_free(t);81for (i = matches[1]? 1: 0; matches[i]; i++)82{83printf(" ");84print_escape_string(matches[i] + prelen);85pari_free(matches[i]);86}87pari_free(matches);88}89printf(")%c", DATA_END);90fflush(stdout);91}92#else93/* no-op */94static void95tm_completion(const char *s, long pos) { (void)s; (void)pos; }96#endif9798typedef struct {99char *cmd;100long n; /* number of args */101char **v; /* args */102} tm_cmd;103104static void105tm_parse_command(tm_cmd *c, const char *ch)106{107long l = strlen(ch);108char *t, *s = (char*)ch, *send = s+l-1;109char **A;110pari_stack s_A;111112if (*s != DATA_BEGIN || *send-- != DATA_END)113pari_err(e_MISC, "missing DATA_[BEGIN | END] in TeXmacs command");114s++;115if (strncmp(s, "special:", 8)) pari_err(e_MISC, "unrecognized TeXmacs command");116s += 8;117if (*s != '(' || *send-- != ')')118pari_err(e_MISC, "missing enclosing parentheses for TeXmacs command");119s++; t = s;120pari_skip_alpha(&s);121c->cmd = pari_strndup(t, s - t);122pari_stack_init(&s_A,sizeof(*A),(void**)&A);123for (c->n = 0; s <= send; c->n++)124{125char *u = (char*)pari_malloc(strlen(s) + 1);126pari_skip_space(&s);127if (*s == '"') s = pari_translate_string(s, u, t);128else129{ /* read integer */130t = s;131while (isdigit((int)*s)) s++;132strncpy(u, t, s - t); u[s-t] = 0;133}134pari_stack_pushp(&s_A, u);135}136c->v = A;137}138139static void140tm_free_cmd(tm_cmd *c)141{142while (c->n--) pari_free((void*)c->v[c->n]);143pari_free((void*)c->v);144}145146static void147tm_handle_command(const char *s)148{149tm_cmd c;150tm_parse_command(&c, s);151if (strcmp(c.cmd, "complete"))152pari_err(e_MISC,"Texmacs command %s not implemented", c.cmd);153if (c.n != 2)154pari_err(e_MISC,"was expecting 2 arguments for Texmacs command");155tm_completion(c.v[0], atol(c.v[1]));156tm_free_cmd(&c);157did_complete = 1;158}159160/****/161162int163tm_is_interactive(void) { return 0; }164165static int tm_is_waiting = 0;166/* tell TeXmacs GP will start outputing data */167void168tm_start_output(void)169{170if (!tm_is_waiting) { printf("%cverbatim:",DATA_BEGIN); fflush(stdout); }171tm_is_waiting = 1;172}173/* tell TeXmacs GP is done and is waiting for new data */174void175tm_end_output(void)176{177if (tm_is_waiting) { printf("%c", DATA_END); fflush(stdout); }178tm_is_waiting = 0;179}180char *181tm_fgets(char *s, int n, FILE *f)182{183if (!did_complete)184{ /* we need input */185tm_start_output();186tm_end_output();187}188return fgets(s,n,f);189}190191int192tm_get_line(const char *prompt, const char *prompt_cont, filtre_t *F)193{194int res = get_line_from_file(prompt, F, pari_infile);195(void)prompt_cont;196if (res)197{198char *s = F->buf->buf;199did_complete = 0;200if (pari_infile == stdin && *s == DATA_BEGIN)201{ tm_handle_command(s); *s = 0; }202else203tm_start_output();204}205return res;206}207208void209tm_output(GEN z)210{211char *sz = GENtoTeXstr(z);212printf("%clatex:", DATA_BEGIN);213printf("\\magenta\\%%%lu = ", GP_DATA->hist->total);214printf("$\\blue %s$%c", sz,DATA_END);215pari_free(sz); fflush(stdout);216pari_flush();217}218219void220init_texmacs(void)221{222#ifdef READLINE223printf("%ccommand:(cas-supports-completions-set! \"pari\")%c\n",224DATA_BEGIN, DATA_END);225pari_use_readline(pari_rl);226#endif227cb_pari_fgets_interactive = tm_fgets;228cb_pari_get_line_interactive = tm_get_line;229230cb_pari_start_output = tm_start_output;231cb_pari_end_output = tm_end_output;232cb_pari_is_interactive = tm_is_interactive;233cb_gp_output = tm_output;234disable_color = 1;235tm_start_output();236}237238239