File indexing completed on 2024-05-12 11:32:33

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "rcallbacks.h"
0007 
0008 #include "rserver.h"
0009 
0010 #include <QDebug>
0011 #include <QStringList>
0012 
0013 #include <R_ext/Rdynload.h>
0014 #include <R_ext/Callbacks.h>
0015 #include <Rinternals.h>
0016 
0017 #ifdef Q_OS_WIN
0018 #include <winsock2.h>
0019 #include <windows.h>
0020 #undef ERROR     // clashes with R
0021 #define Win32    // needed for R includes
0022 #include <R_ext/RStartup.h>
0023 #include <R_ext/Utils.h>
0024 #include <R_ext/libextern.h>
0025 
0026 structRstart RK_R_Params;
0027 
0028 extern "C" {
0029     // why oh why isn't Rinterface.h available on Windows?
0030     LibExtern void* R_GlobalContext;
0031     LibExtern uintptr_t R_CStackLimit;
0032     LibExtern void R_SaveGlobalEnvToFile(char*);
0033 }
0034 #else
0035 #include <Rinterface.h>
0036 #endif
0037 
0038 #include <stdio.h>
0039 
0040 RServer* server;
0041 Expression* currentExpression;
0042 
0043 void setupCallbacks(RServer* r)
0044 {
0045     qDebug()<<"RServer: "<<"setting up callbacks";
0046 
0047     server=r;
0048     currentExpression=nullptr;
0049 
0050     R_Outputfile=nullptr;
0051     R_Consolefile=nullptr;
0052 
0053     ptr_R_WriteConsole=nullptr;
0054     ptr_R_WriteConsoleEx=onWriteConsoleEx;
0055     ptr_R_ShowMessage=onShowMessage;
0056     ptr_R_Busy=onBusy;
0057     ptr_R_ReadConsole=onReadConsole;
0058     ptr_R_ShowFiles=onShowFiles;
0059 }
0060 
0061 void setCurrentExpression(Expression* expr)
0062 {
0063     currentExpression=expr;
0064 }
0065 
0066 
0067 enum OutputType { NormalOutput=0, ErrorOutput=1 };
0068 
0069 void onWriteConsoleEx(const char* text, int size, int otype)
0070 {
0071     const QString string=QString::fromUtf8(text, size);
0072     if (otype==NormalOutput)
0073     {
0074         currentExpression->std_buffer+=string;
0075     }else
0076     {
0077         currentExpression->err_buffer+=string;
0078     }
0079 }
0080 
0081 void onShowMessage(const char* text)
0082 {
0083     const QString string=QString::fromUtf8(text);
0084     currentExpression->std_buffer+=string;
0085 }
0086 
0087 void onBusy(int which)
0088 {
0089     qDebug()<<"RServer: "<<"onBusy: "<<which;
0090 }
0091 
0092 int onReadConsole(const char* prompt, unsigned char* buf, int buflen, int hist)
0093 {
0094     Q_UNUSED(hist);
0095     qDebug()<<"RServer: "<<"readConsole: "<<prompt;
0096 
0097     QString input=server->requestInput(QLatin1String(prompt));
0098 
0099     if(input.size()>buflen)
0100         input.truncate(buflen);
0101 
0102     strcpy( (char*) buf, input.toStdString().c_str());
0103 
0104     return input.size();
0105 }
0106 
0107 int  onShowFiles(int nfile, const char** file, const char** headers, const char* wtitle, Rboolean del, const char* pager)
0108 {
0109     int i;
0110     qDebug()<<"RServer: "<<"show files: ";
0111     for (i=0;i<nfile; i++)
0112     {
0113         qDebug()<<"RServer: "<<"show file "<<file[i]<<" header: "<<headers[i];
0114     }
0115 
0116     qDebug()<<"RServer: "<<" title: "<<wtitle[i];
0117     qDebug()<<"RServer: "<<"del: "<<del;
0118     qDebug()<<"RServer: "<<"pager: "<<pager;
0119 
0120     QStringList files;
0121     for(int i=0;i<nfile; i++)
0122         server->addFileToOutput(QString::fromLocal8Bit(file[i]));
0123 
0124     currentExpression->hasOtherResults=true;
0125 
0126     return 0;
0127 }