open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2010, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435/******************************************************************************36*37* parse-paste.c: HyperDoc routines for paste-in areas.38*39* Copyright The Numerical Algorithms Group Limited 1991, 1992, 1993.40*41****************************************************************************/4243#include "openaxiom-c-macros.h"44#include "debug.h"45#include "halloc.h"46#include "sockio.h"47#include "parse.h"48#include "hyper.h"49#include "display.h"50#include "group.h"51#include "lex.h"5253static void load_patch(PatchStore * patch);5455short int gInPaste;565758void59parse_paste()60{61TextNode *pn = curr_node;62PasteNode *paste;63SourceInputKind where;6465if (gParserRegion != Scrolling) {66fprintf(stderr, "(HyperDoc) Paste areas are only allowed in the scrolling area:");67print_page_and_filename();68jump();69}70gInPaste++;7172/* now I need to get the name */73get_token();74if (token.type != openaxiom_Lbrace_token) {75fprintf(stderr, "(HyperDoc) A paste area needs a name:\n");76print_next_ten_tokens();77print_page_and_filename();78jump();79}80pn->data.text = alloc_string(get_input_string());81pn->type = openaxiom_Paste_token;8283/*84* now see if there is already an entry in the hash_table for this thing,85* if not create it and put it there.86*/87paste = (PasteNode *) hash_find(gWindow->fPasteHashTable, pn->data.text);88if (paste == 0) {89paste = alloc_paste_node(pn->data.text);90hash_insert(gWindow->fPasteHashTable, (char *)paste, paste->name);91}92else if (paste->haspaste) {93fprintf(stderr, "(HyperDoc) Tried to redefine paste area %s\n", paste->name);94print_page_and_filename();95/* jump(); */96}97paste->haspaste = 1;98paste->paste_item = current_item();99get_token();100if (token.type == openaxiom_Lsquarebrace_token) {101/* user wishes to specify a where to send the command */102where = get_where();103if (where == SourceInputKind::Error) {104paste->where = where;105fprintf(stderr, "(HyperDoc) \\begin{paste} was expecting [lisp|unix|ht]\n");106print_next_ten_tokens();107print_page_and_filename();108jump();109}110else111paste->where = where;112get_token();113}114else115paste->where = SourceInputKind::File;116117/* now try to get the command argument or page name */118if (token.type != openaxiom_Lbrace_token) {119paste->where = { };120fprintf(stderr, "(HyperDoc) \\begin{paste} was expecting an argument\n");121print_next_ten_tokens();122print_page_and_filename();123jump();124}125paste->arg_node = alloc_node();126curr_node = paste->arg_node;127parse_HyperDoc();128curr_node->type = openaxiom_Endarg_token;129130gWindow->fDisplayedWindow = gWindow->fScrollWindow;131132/* Now try to find the displaying text */133pn->next = alloc_node();134curr_node = pn->next;135parse_HyperDoc();136curr_node->type = openaxiom_Endpaste_token;137paste->end_node = curr_node;138139paste->begin_node = pn;140gInPaste--;141}142143void144parse_pastebutton()145{146PasteNode *paste;147TextNode *pb;148149/*150* this routine parse a \pastebutton expression. The syntax is151* \pastebutton{name}152*/153pb = curr_node;154pb->type = openaxiom_Pastebutton_token;155156/* first thing I should do is get the name */157get_token();158if (token.type != openaxiom_Lbrace_token) {159fprintf(stderr, "(HyperDoc) \\pastebutton needs a name\n");160print_page_and_filename();161print_next_ten_tokens();162jump();163}164pb->data.text = alloc_string(get_input_string());165166/*167* now I should see if the paste area has already been parsed, and if not168* I should create a spot in the hash table for it169*/170paste = (PasteNode *) hash_find(gWindow->fPasteHashTable, pb->data.text);171if (paste == 0) {172paste = alloc_paste_node(pb->data.text);173hash_insert(gWindow->fPasteHashTable,(char *) paste, paste->name);174}175else if (paste->hasbutton) {176fprintf(stderr, "(HyperDoc) Tried to redefine paste area %s\n", paste->name);177print_page_and_filename();178/* jump(); */179}180paste->hasbutton = 1;181182/* Now we need to parse the HyperDoc and for the displayed text */183184get_token();185if (token.type != openaxiom_Lbrace_token) {186fprintf(stderr, "(HyperDoc) \\pastebutton was expecting a { \n");187print_page_and_filename();188print_next_ten_tokens();189jump();190}191pb->next = alloc_node();192curr_node = pb->next;193parse_HyperDoc();194curr_node->type = openaxiom_Endpastebutton_token;195196/* once that is done I need only make the window for this link */197pb->link = make_paste_window(paste);198}199200201/*202* this routine is responsible for parsing a patch from a file. To do this I203* guess er will init_scanner, then parse, the parsed piece of text204* will replace the current PasteNode which will be squashed down to205* nothing, and then discarded.206*/207208HyperDocPage *209parse_patch(PasteNode *paste)210{211TextNode *new_paste;212TextNode *end_node;213TextNode *begin_node;214TextNode *arg_node;215TextNode *old;216TextNode *next_node;217InputItem *paste_item = paste->paste_item;218SourceInputKind where = paste->where;219GroupItem *g = paste->group;220ItemStack *is = paste->item_stack;221PatchStore *patch;222char *patch_name;223int ret_value = 1;224225/* prepare to throw away the current paste node */226end_node = paste->end_node;227next_node = end_node->next;228begin_node = paste->begin_node;229arg_node = paste->arg_node;230old = begin_node->next;231232/* now read the new stuff and add it in between all this stuff */233234switch (where) {235case SourceInputKind::File:236patch_name = print_to_string(arg_node);237patch = (PatchStore *) hash_find(gWindow->fPatchHashTable, patch_name);238if (!patch) {239fprintf(stderr, "(HyperDoc) Unknown patch name %s\n", patch_name);240BeepAtTheUser();241return 0;242}243if (!patch->loaded)244load_patch(patch);245input_type = SourceInputKind::String;246input_string = patch->string;247break;248case SourceInputKind::SpadSocket:249input_type = SourceInputKind::SpadSocket;250ret_value = issue_serverpaste(arg_node);251if (ret_value < 0) {252paste->where = where;253paste->end_node = end_node;254paste->arg_node = arg_node;255paste->group = g;256paste->item_stack = is;257paste->haspaste = 1;258return 0;259}260break;261case SourceInputKind::UnixFD:262input_type = SourceInputKind::UnixFD;263issue_unixpaste(arg_node);264break;265default:266fprintf(stderr, "(HyperDoc) \\parsebutton error: Unknown where\n");267exit(-1);268break;269}270271paste->where = { };272paste->end_node = paste->arg_node = paste->begin_node = 0;273paste->group = 0;274paste->item_stack = 0;275paste->haspaste = 0;276paste->paste_item = 0;277278279/* set the jump buffer in case it is needed */280if (setjmp(jmpbuf)) {281/*** OOOPS, an error occurred ****/282fprintf(stderr, "(HyperDoc) Had an error parsing a patch: Goodbye!\n");283exit(-1);284}285286287end_node->next = 0;288free_node(old, 1);289290init_parse_patch(gWindow->page);291init_paste_item(paste_item);292get_token();293if (token.type != openaxiom_Patch_token) {294fprintf(stderr, "(HyperDoc) Pastebutton %s was expecting a patch\n",295paste->name);296jump();297}298if (input_type == SourceInputKind::String) {299get_token();300if (token.type != openaxiom_Lbrace_token) {301token_name(token.type);302fprintf(stderr, "(HyperDoc) Unexpected %s \n", ebuffer);303print_page_and_filename();304jump();305}306307get_token();308if (token.type != openaxiom_Word_token) {309token_name(token.type);310fprintf(stderr, "(HyperDoc) Unexpected %s \n", ebuffer);311print_page_and_filename();312jump();313}314315get_token();316if (token.type != openaxiom_Rbrace_token) {317token_name(token.type);318fprintf(stderr, "(HyperDoc) Unexpected %s \n", ebuffer);319print_page_and_filename();320jump();321}322}323new_paste = alloc_node();324curr_node = new_paste;325parse_HyperDoc();326327/* Once I am back, I need only reallign all the text structures */328curr_node->type = openaxiom_Noop_token;329curr_node->next = next_node;330begin_node->next = new_paste;331begin_node->type = openaxiom_Noop_token;332free(begin_node->data.text);333begin_node->data.text = 0;334335gWindow->fDisplayedWindow = gWindow->fScrollWindow;336337repaste_item();338339paste_page(begin_node);340341/* so now I should just be able to disappear */342return gWindow->page;343}344345static void346load_patch(PatchStore *patch)347{348long start_fpos;349int size = 0;350int limsize;351char *trace;352353354save_scanner_state();355cfile = find_fp(patch->fpos);356357init_scanner();358359/** First thing I should do is make sure that the name is correct ***/360start_fpos = fpos;361get_expected_token(openaxiom_Patch_token);362get_expected_token(openaxiom_Lbrace_token);363get_expected_token(openaxiom_Word_token);364if (strcmp(token.id, patch->name)) {365/** WOW, Somehow I had the location of the wrong macro **/366fprintf(stderr, "(HyperDoc) Expected patch name %s: got instead %s in load_patch\n",367patch->name, token.id);368jump();369}370get_expected_token(openaxiom_Rbrace_token);371372scan_HyperDoc();373fseek(cfile, patch->fpos.pos + start_fpos, 0);374limsize = fpos - start_fpos + 1;375patch->string = (char *) halloc((limsize + 1) * sizeof(char), "Patch String");376for (size = 1, trace = patch->string; size < limsize; size++)377*trace++ = getc(cfile);378*trace = '\0';379patch->loaded = 1;380restore_scanner_state();381}382383384385386