Audacity  3.0.3
Typedefs | Functions | Variables
ScripterCallback.cpp File Reference
#include <wx/wx.h>
#include "ScripterCallback.h"
#include "commands/ScriptCommandRelay.h"
#include "ModuleConstants.h"
Include dependency graph for ScripterCallback.cpp:

Go to the source code of this file.

Typedefs

typedef DLL_IMPORT int(* tpExecScriptServerFunc) (wxString *pIn, wxString *pOut)
 

Functions

void PipeServer ()
 
int DLL_API RegScriptServerFunc (tpExecScriptServerFunc pFn)
 
DEFINE_VERSION_CHECK DLL_API int ModuleDispatch (ModuleDispatchTypes type)
 
int DoSrv (char *pIn)
 
size_t smin (size_t a, size_t b)
 
int DoSrvMore (char *pOut, size_t nMax)
 

Variables

static tpExecScriptServerFunc pScriptServerFn =NULL
 
wxString Str2
 
wxArrayString aStr
 
unsigned int currentLine
 
size_t currentPosition
 

Typedef Documentation

◆ tpExecScriptServerFunc

typedef DLL_IMPORT int(* tpExecScriptServerFunc) (wxString *pIn, wxString *pOut)

Definition at line 28 of file ScripterCallback.cpp.

Function Documentation

◆ DoSrv()

int DoSrv ( char *  pIn)

Definition at line 66 of file ScripterCallback.cpp.

67 {
68  // Interpret string as unicode.
69  // wxWidgets (now) uses unicode internally.
70  // Scripts must send unicode strings (if going beyond 7-bit ASCII).
71  // Important for filenames in commands.
72  wxString Str1(pIn, wxConvUTF8);
73  Str1.Replace( wxT("\r"), wxT(""));
74  Str1.Replace( wxT("\n"), wxT(""));
75  Str2 = wxEmptyString;
76  (*pScriptServerFn)( &Str1 , &Str2);
77 
78  Str2 += wxT('\n');
79  size_t outputLength = Str2.Length();
80  aStr.Clear();
81  size_t iStart = 0;
82  size_t i;
83  for(i = 0; i < outputLength; ++i)
84  {
85  if( Str2[i] == wxT('\n') )
86  {
87  aStr.Add( Str2.Mid( iStart, i-iStart) + wxT("\n") );
88  iStart = i+1;
89  }
90  }
91 
92  currentLine = 0;
93  currentPosition = 0;
94 
95  return 1;
96 }

References aStr, currentLine, currentPosition, and Str2.

Referenced by PipeServer().

Here is the caller graph for this function:

◆ DoSrvMore()

int DoSrvMore ( char *  pOut,
size_t  nMax 
)

Definition at line 103 of file ScripterCallback.cpp.

104 {
105  wxASSERT(currentLine >= 0);
106  wxASSERT(currentPosition >= 0);
107 
108  size_t totalLines = aStr.GetCount();
109  while (currentLine < totalLines)
110  {
111  wxCharBuffer lineString = aStr[currentLine].ToUTF8();
112  size_t lineLength = lineString.length();
113  size_t charsLeftInLine = lineLength - currentPosition;
114 
115  wxASSERT(charsLeftInLine >= 0);
116 
117  if (charsLeftInLine == 0)
118  {
119  // Move to next line
120  ++currentLine;
121  currentPosition = 0;
122  }
123  else
124  {
125  // Write as much of the rest of the line as will fit in the buffer
126  size_t charsToWrite = smin(charsLeftInLine, nMax - 1);
127  memcpy(pOut,
128  &(lineString.data()[currentPosition]),
129  charsToWrite);
130  pOut[charsToWrite] = '\0';
131  currentPosition += charsToWrite;
132  // Need to cast to prevent compiler warnings
133  int charsWritten = static_cast<int>(charsToWrite + 1);
134  // (Check cast was safe)
135  wxASSERT(static_cast<size_t>(charsWritten) == charsToWrite + 1);
136  return charsWritten;
137  }
138  }
139  return 0;
140 }

References aStr, currentLine, currentPosition, and smin().

Referenced by PipeServer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ModuleDispatch()

DEFINE_VERSION_CHECK DLL_API int ModuleDispatch ( ModuleDispatchTypes  type)

Definition at line 47 of file ScripterCallback.cpp.

48 {
49  switch (type) {
50  case ModuleInitialize:
52  break;
53  default:
54  break;
55  }
56  return 1;
57 }

References ModuleInitialize, RegScriptServerFunc(), and ScriptCommandRelay::StartScriptServer().

Here is the call graph for this function:

◆ PipeServer()

void PipeServer ( )

Definition at line 124 of file PipeServer.cpp.

125 {
126  FILE *fromFifo = NULL;
127  FILE *toFifo = NULL;
128  int rc;
129  char buf[nBuff];
130  char toFifoName[nBuff];
131  char fromFifoName[nBuff];
132 
133  sprintf(toFifoName, fifotmpl, "to", getuid());
134  sprintf(fromFifoName, fifotmpl, "from", getuid());
135 
136  unlink(toFifoName);
137  unlink(fromFifoName);
138 
139  // TODO avoid symlink security issues?
140 
141  rc = mkfifo(fromFifoName, S_IRWXU) & mkfifo(toFifoName, S_IRWXU);
142  if (rc < 0)
143  {
144  perror("Unable to create fifos");
145  printf("Ignoring...");
146 // return;
147  }
148 
149  // open to (incoming) pipe first.
150  toFifo = fopen(toFifoName, "r");
151  if (toFifo == NULL)
152  {
153  perror("Unable to open fifo to server from script");
154  if (fromFifo != NULL)
155  fclose(fromFifo);
156  return;
157  }
158 
159  // open from (outgoing) pipe second. This could block if there is no reader.
160  fromFifo = fopen(fromFifoName, "w");
161  if (fromFifo == NULL)
162  {
163  perror("Unable to open fifo from server to script");
164  return;
165  }
166 
167  while (fgets(buf, sizeof(buf), toFifo) != NULL)
168  {
169  int len = strlen(buf);
170  if (len <= 1)
171  {
172  continue;
173  }
174 
175  buf[len - 1] = '\0';
176 
177  printf("Server received %s\n", buf);
178  DoSrv(buf);
179 
180  while (true)
181  {
182  len = DoSrvMore(buf, nBuff);
183  if (len <= 1)
184  {
185  break;
186  }
187  printf("Server sending %s",buf);
188 
189  // len - 1 because we do not send the null character
190  fwrite(buf, 1, len - 1, fromFifo);
191  }
192  fflush(fromFifo);
193  }
194 
195  printf("Read failed on fifo, quitting\n");
196 
197  if (toFifo != NULL)
198  fclose(toFifo);
199 
200  if (fromFifo != NULL)
201  fclose(fromFifo);
202 
203  unlink(toFifoName);
204  unlink(fromFifoName);
205 }

References DoSrv(), DoSrvMore(), fifotmpl, and nBuff.

Referenced by RegScriptServerFunc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegScriptServerFunc()

int DLL_API RegScriptServerFunc ( tpExecScriptServerFunc  pFn)

Definition at line 35 of file ScripterCallback.cpp.

36 {
37  if( pFn )
38  {
39  pScriptServerFn = pFn;
40  PipeServer();
41  }
42 
43  return 4;
44 }

References PipeServer(), and pScriptServerFn.

Referenced by ModuleDispatch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ smin()

size_t smin ( size_t  a,
size_t  b 
)

Definition at line 98 of file ScripterCallback.cpp.

98 { return a < b ? a : b; }

Referenced by DoSrvMore().

Here is the caller graph for this function:

Variable Documentation

◆ aStr

wxArrayString aStr

Definition at line 60 of file ScripterCallback.cpp.

Referenced by DoSrv(), and DoSrvMore().

◆ currentLine

unsigned int currentLine

Definition at line 61 of file ScripterCallback.cpp.

Referenced by DoSrv(), and DoSrvMore().

◆ currentPosition

size_t currentPosition

Definition at line 62 of file ScripterCallback.cpp.

Referenced by DoSrv(), DoSrvMore(), and AccessibleLinksFormatter::Populate().

◆ pScriptServerFn

tpExecScriptServerFunc pScriptServerFn =NULL
static

Definition at line 29 of file ScripterCallback.cpp.

Referenced by RegScriptServerFunc().

◆ Str2

wxString Str2

Definition at line 59 of file ScripterCallback.cpp.

Referenced by DoSrv(), and ExecForLisp().

currentPosition
size_t currentPosition
Definition: ScripterCallback.cpp:62
fifotmpl
const char fifotmpl[]
Definition: PipeServer.cpp:117
aStr
wxArrayString aStr
Definition: ScripterCallback.cpp:60
pScriptServerFn
static tpExecScriptServerFunc pScriptServerFn
Definition: ScripterCallback.cpp:29
smin
size_t smin(size_t a, size_t b)
Definition: ScripterCallback.cpp:98
ScriptCommandRelay::StartScriptServer
static void StartScriptServer(tpRegScriptServerFunc scriptFn)
Starts the script server.
Definition: ScriptCommandRelay.cpp:80
RegScriptServerFunc
int DLL_API RegScriptServerFunc(tpExecScriptServerFunc pFn)
Definition: ScripterCallback.cpp:35
DoSrvMore
int DoSrvMore(char *pOut, size_t nMax)
Definition: ScripterCallback.cpp:103
nBuff
const int nBuff
Definition: PipeServer.cpp:119
currentLine
unsigned int currentLine
Definition: ScripterCallback.cpp:61
Str2
wxString Str2
Definition: ScripterCallback.cpp:59
DoSrv
int DoSrv(char *pIn)
Definition: ScripterCallback.cpp:66
PipeServer
void PipeServer()
Definition: PipeServer.cpp:124
ModuleInitialize
@ ModuleInitialize
Definition: ModuleConstants.h:28