Audacity 3.2.0
Functions | Variables
PipeServer.cpp File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
Include dependency graph for PipeServer.cpp:

Go to the source code of this file.

Functions

int DoSrv (char *pIn)
 
int DoSrvMore (char *pOut, size_t nMax)
 
void PipeServer ()
 

Variables

const char fifotmpl [] = "/tmp/audacity_script_pipe.%s.%d"
 
const int nBuff = 1024
 

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;
94
95 return 1;
96}
wxT("CloseDown"))
size_t currentPosition
unsigned int currentLine
wxString Str2
wxArrayString aStr

References aStr, currentLine, currentPosition, Str2, and wxT().

Referenced by PipeServer().

Here is the call graph for this function:
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}
size_t smin(size_t a, size_t b)

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:

◆ 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}
int DoSrv(char *pIn)
int DoSrvMore(char *pOut, size_t nMax)
const int nBuff
Definition: PipeServer.cpp:119
const char fifotmpl[]
Definition: PipeServer.cpp:117

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:

Variable Documentation

◆ fifotmpl

const char fifotmpl[] = "/tmp/audacity_script_pipe.%s.%d"

Definition at line 117 of file PipeServer.cpp.

Referenced by PipeServer().

◆ nBuff

const int nBuff = 1024

Definition at line 119 of file PipeServer.cpp.

Referenced by PipeServer().