Audacity  3.0.3
ScripterCallback.cpp
Go to the documentation of this file.
1 // ScripterCallback.cpp :
2 //
3 // A loadable module that connects a windows named pipe
4 // to a registered service function that is able to
5 // process a single command at a time.
6 //
7 // The service function is provided by the application
8 // and not by libscript. mod_script_pipe was developed for
9 // Audacity. Because it forwards commands
10 // rather than handling them itself it can be used in
11 // other projects too.
12 //
13 // Enabling other programs to connect to Audacity via a pipe is a potential
14 // security risk. Use at your own risk.
15 
16 #include <wx/wx.h>
17 #include "ScripterCallback.h"
19 
20 /*
21 //#define ModuleDispatchName "ModuleDispatch"
22 See the example in this file. It has several cases/options in it.
23 */
24 
25 #include "ModuleConstants.h"
26 
27 extern void PipeServer();
28 typedef DLL_IMPORT int (*tpExecScriptServerFunc)( wxString * pIn, wxString * pOut);
30 
31 
32 extern "C" {
33 
34 // And here is our special registration function.
36 {
37  if( pFn )
38  {
39  pScriptServerFn = pFn;
40  PipeServer();
41  }
42 
43  return 4;
44 }
45 
48 {
49  switch (type) {
50  case ModuleInitialize:
52  break;
53  default:
54  break;
55  }
56  return 1;
57 }
58 
59 wxString Str2;
60 wxArrayString aStr;
61 unsigned int currentLine;
63 
64 // Send the received command to Audacity and build an array of response lines.
65 // The response lines can be retrieved by calling DoSrvMore repeatedly.
66 int DoSrv(char *pIn)
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 }
97 
98 size_t smin(size_t a, size_t b) { return a < b ? a : b; }
99 
100 // Write up to nMax characters of the prepared (by DoSrv) response lines.
101 // Returns the number of characters sent, including null.
102 // Zero returned if and only if there's nothing else to send.
103 int DoSrvMore(char *pOut, size_t nMax)
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 }
141 
142 } // End extern "C"
DLL_API
#define DLL_API
Definition: ModuleConstants.h:22
tpExecScriptServerFunc
DLL_IMPORT int(* tpExecScriptServerFunc)(wxString *pIn, wxString *pOut)
Definition: ScripterCallback.cpp:28
currentPosition
size_t currentPosition
Definition: ScripterCallback.cpp:62
DoSrv
int DoSrv(char *pIn)
Definition: ScripterCallback.cpp:66
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
ScripterCallback.h
RegScriptServerFunc
int DLL_API RegScriptServerFunc(tpExecScriptServerFunc pFn)
Definition: ScripterCallback.cpp:35
DLL_IMPORT
#define DLL_IMPORT
Definition: ModuleConstants.h:23
ModuleConstants.h
ModuleDispatchTypes
ModuleDispatchTypes
Definition: ModuleConstants.h:27
currentLine
unsigned int currentLine
Definition: ScripterCallback.cpp:61
Str2
wxString Str2
Definition: ScripterCallback.cpp:59
DEFINE_VERSION_CHECK
Definition: ModNullCallback.cpp:73
DoSrvMore
int DoSrvMore(char *pOut, size_t nMax)
Definition: ScripterCallback.cpp:103
PipeServer
void PipeServer()
Definition: PipeServer.cpp:124
ScriptCommandRelay.h
Contains declarations for ScriptCommandRelay.
ModuleDispatch
DEFINE_VERSION_CHECK DLL_API int ModuleDispatch(ModuleDispatchTypes type)
Definition: ScripterCallback.cpp:47
ModuleInitialize
@ ModuleInitialize
Definition: ModuleConstants.h:28