int prSpeakText(struct VMGlobals *g, int numArgsPushed){ OSErr theErr = noErr; PyrSlot *obj = g->sp-2; PyrSlot *a = g->sp-1; PyrSlot *str = g->sp; int chan; slotIntVal(a, &chan); chan = sc_clip(chan, 0, kMaxSpeechChannels); if(speechStrings[chan] != NULL) { post("voice %i already speaking\n", chan); return errNone; } else { // speechStrings[chan] = (char*)pyr_pool_compile->Alloc((a->uo->size + 1)* sizeof(char)); speechStrings[chan] = (char*) malloc((str->uo->size + 1)* sizeof(char)); MEMFAIL(speechStrings[chan]); slotStrVal(str, speechStrings[chan], str->uo->size+1); //if(!fCurSpeechChannel) theErr = NewSpeechChannel( NULL, &fCurSpeechChannel ); theErr = SpeakText( fCurSpeechChannel[chan], speechStrings[chan], strlen(speechStrings[chan])); //should be freed only after the text was spoken! // todo move this bit to the callback! // pyr_pool_compile->Free(theTextToSpeak); } return errNone; }
int prFileCopy(struct VMGlobals * g, int numArgsPushed) { PyrSlot *b = g->sp - 1, *c = g->sp; char filename1[PATH_MAX]; char filename2[PATH_MAX]; int error; error = slotStrVal(b, filename1, PATH_MAX); if (error != errNone) return error; error = slotStrVal(c, filename2, PATH_MAX); if (error != errNone) return error; boost::filesystem::copy(filename1, filename2); return errNone; }
int ScIDE_Send(struct VMGlobals *g, int numArgsPushed) { if (!gIpcClient) { error("ScIDE not connected\n"); return errFailed; } if( !gMainVMGlobals->canCallOS ) { error("You can not use ScIDE:prSend functionality in the current thread.\nTry scheduling on AppClock instead.\n"); return errFailed; } PyrSlot * idSlot = g->sp - 1; char id[255]; if (slotStrVal( idSlot, id, 255 )) return errWrongType; PyrSlot * argSlot = g->sp; try { YAMLSerializer serializer(argSlot); sendSelectorAndData(gIpcClient->mSocket, QString(id), QString::fromUtf8(serializer.data())); } catch (std::exception const & e) { postfl("Exception during ScIDE_Send: %s\n", e.what()); return errFailed; } return errNone; }
int prSCDoc_ParseFile(struct VMGlobals* g, int numArgsPushed) { PyrSlot *a, *b, *c; char filename[PATH_MAX]; int mode, err; a = g->sp - 2; b = g->sp - 1; c = g->sp; err = slotStrVal(b, filename, PATH_MAX); if (err) return err; err = slotIntVal(c, &mode); if (err) return err; DocNode *n = scdoc_parse_file(filename, mode); if(n) { // doc_node_dump(n); _doc_traverse(g, n, NULL, a); doc_node_free_tree(n); } else { SetNil(a); } return errNone; }
int ScIDE_GetDocTextMirror(struct VMGlobals *g, int numArgsPushed) { if (!gIpcClient) { error("ScIDE not connected\n"); return errFailed; } PyrSlot * returnSlot = g->sp - numArgsPushed + 1; PyrSlot * docIDSlot = g->sp - 2; char id[255]; if (slotStrVal( docIDSlot, id, 255 )) return errWrongType; int pos, range, err = errNone; PyrSlot * posSlot = g->sp-1; err = slotIntVal(posSlot, &pos); if (err) return err; PyrSlot * rangeSlot = g->sp; err = slotIntVal(rangeSlot, &range); if (err) return err; QByteArray key = QByteArray(id); QString docText = gIpcClient->getTextMirrorForDocument(key, pos, range); PyrString* pyrString = newPyrString(g->gc, docText.toLatin1().constData(), 0, true); SetObject(returnSlot, pyrString); return errNone; }
int ScIDE_Send(struct VMGlobals *g, int numArgsPushed) { if (!gIpcClient) { error("ScIDE not connected\n"); return errFailed; } PyrSlot * idSlot = g->sp - 1; char id[255]; if (slotStrVal( idSlot, id, 255 )) return errWrongType; PyrSlot * argSlot = g->sp; try { YAMLSerializer serializer(argSlot); QDataStream stream(gIpcClient->mSocket); stream.setVersion(QDataStream::Qt_4_6); stream << QString(id); stream << QString::fromUtf8(serializer.data()); } catch (std::exception const & e) { postfl("Exception during ScIDE_Send: %s\n", e.what()); return errFailed; } return errNone; }
int ScIDE_SetDocSelectionMirror(struct VMGlobals *g, int numArgsPushed) { if (!gIpcClient) { error("ScIDE not connected\n"); return errFailed; } PyrSlot * docIDSlot = g->sp - 2; char id[255]; if (slotStrVal( docIDSlot, id, 255 )) return errWrongType; int start, range, err = errNone; PyrSlot * startSlot = g->sp-1; err = slotIntVal(startSlot, &start); if (err) return err; PyrSlot * rangeSlot = g->sp; err = slotIntVal(rangeSlot, &range); if (err) return err; QByteArray key = QByteArray(id); gIpcClient->setSelectionMirrorForDocument(key, start, range); return errNone; }
static int prSerialPort_Open(struct VMGlobals *g, int numArgsPushed) { PyrSlot *args = g->sp - 1 - SerialPort::kNumOptions; int err; PyrSlot* self = args+0; if (getSerialPort(self) != 0) return errFailed; char portName[PATH_MAX]; err = slotStrVal(args+1, portName, sizeof(portName)); printf("portName %s\n", portName); if (err) return err; SerialPort::Options options; SerialPort* port = 0; options.exclusive = IsTrue(args+2); int baudrate; err = slotIntVal(args+3, &baudrate); if (err) return err; options.baudrate = baudrate; int databits; err = slotIntVal(args+4, &databits); if (err) return err; options.databits = databits; options.stopbit = IsTrue(args+5); int parity; err = slotIntVal(args+6, &parity); if (err) return err; options.parity = (SerialPort::Parity)parity; options.crtscts = IsTrue(args+7); options.xonxoff = IsTrue(args+8); try { port = new SerialPort(slotRawObject(self), portName, options); } catch (SerialPort::Error& e) { std::ostringstream os; os << "SerialPort Error: " << e.what(); post(os.str().c_str()); return errFailed; } SetPtr(slotRawObject(self)->slots+0, port); return errNone; }
int prFileType(struct VMGlobals * g, int numArgsPushed) { PyrSlot *a = g->sp - 1, *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; boost::filesystem::file_status s(boost::filesystem::symlink_status(filename)); SetInt(a, s.type()); return errNone; }
int prFileExists(struct VMGlobals * g, int numArgsPushed) { PyrSlot *a = g->sp - 1, *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; bool res = boost::filesystem::exists(filename); SetBool(a, res); return errNone; }
int ScIDE_SetDocTextMirror(struct VMGlobals *g, int numArgsPushed) { if (!gIpcClient) { error("ScIDE not connected\n"); return errFailed; } PyrSlot * docIDSlot = g->sp - 3; char id[255]; if (slotStrVal( docIDSlot, id, 255 )) return errWrongType; PyrSlot * textSlot = g->sp - 2; int length = slotStrLen(textSlot); if(length == -1) return errWrongType; std::vector<char> text(length + 1); if (slotStrVal( textSlot, text.data(), length + 1)) return errWrongType; int pos, range, err = errNone; PyrSlot * posSlot = g->sp-1; err = slotIntVal(posSlot, &pos); if (err) return err; PyrSlot * rangeSlot = g->sp; err = slotIntVal(rangeSlot, &range); if (err) return err; QByteArray key = QByteArray(id); QString docText = QString(text.data()); gIpcClient->setTextMirrorForDocument(key, docText, pos, range); return errNone; }
int prString_System(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp; char cmdline[1024]; int err = slotStrVal(a, cmdline, 1023); if (err) return err; int res = system(cmdline); SetInt(a, res); return errNone; }
int prFileSize(struct VMGlobals * g, int numArgsPushed) { PyrSlot *a = g->sp - 1, *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; uintmax_t sz = boost::filesystem::file_size(filename); SetInt(a, (int)sz); // kengo: return errNone; }
int prFileMTime(struct VMGlobals * g, int numArgsPushed) { PyrSlot *a = g->sp - 1, *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; time_t mtime = boost::filesystem::last_write_time(filename); SetInt(a, (int)mtime); // kengo: return errNone; }
int prFileDelete(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp - 1, *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; int err = unlink(filename); SetBool(a, err == 0); return errNone; }
int prGetHostByName(VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp; char hostname[256]; int err = slotStrVal(a, hostname, 255); if (err) return err; struct hostent *he = gethostbyname(hostname); if (!he) return errFailed; SetInt(a, ntohl(*(int*)he->h_addr)); return errNone; }
int prFileMkDir(struct VMGlobals * g, int numArgsPushed) { PyrSlot *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; boost::system::error_code error_code; boost::filesystem::create_directories(filename, error_code); if (error_code) postfl("Warning: %s (\"%s\")\n", error_code.message().c_str(), filename); return errNone; }
int ScIDE_Connect(struct VMGlobals *g, int numArgsPushed) { if (gIpcClient) { error("ScIDE already connected\n"); return errFailed; } PyrSlot * ideNameSlot = g->sp; char ideName[1024]; int status = slotStrVal(ideNameSlot, ideName, 1024); if (status != errNone) return errWrongType; gIpcClient = new SCIpcClient(ideName); return errNone; }
int prString_Dirname(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp; char path[PATH_MAX]; int err = slotStrVal(a, path, PATH_MAX); if (err) return err; char *dirname0 = dirname(path); int size = strlen(dirname0); PyrString *strobj = newPyrStringN(g->gc, size, 0, true); memcpy(strobj->s, dirname0, size); SetObject(a, strobj); return errNone; }
int prFileDelete(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp - 1, *b = g->sp; char filename[PATH_MAX]; int error = slotStrVal(b, filename, PATH_MAX); if (error != errNone) return error; boost::system::error_code error_code; boost::filesystem::remove(filename, error_code); if (error_code) SetFalse(a); else SetTrue(a); return errNone; }
int prHID_API_Open( VMGlobals* g, int numArgsPushed ){ PyrSlot *args = g->sp - numArgsPushed + 1; PyrSlot* self = args + 0; PyrSlot* arg1 = args + 1; PyrSlot* arg2 = args + 2; PyrSlot* arg3 = args + 3; int err; char path[256]; int vendorid; int productid; // char serial_number[256]; // could also use serial_number as specification to open device, but this is not working yet err = slotIntVal( arg1, &vendorid ); if ( err != errNone ) return err; err = slotIntVal( arg2, &productid ); if ( err != errNone ) return err; int result; err = slotStrVal(arg3, path, sizeof(path)); if (err) return err; result = SC_HID_APIManager::instance().open_device_path( path, vendorid, productid ); /* // could also use serial_number as specification to open device, but this is not working yet if ( NotNil( arg3 ) ){ err = slotStrVal(arg3, serial_number, sizeof(serial_number)); if (err) return err; // open device result = SC_HID_APIManager::instance().open_device( vendorid, productid, serial_number ); } else { // open device result = SC_HID_APIManager::instance().open_device( vendorid, productid, NULL ); } */ SetInt( self, result ); return errNone; }
int prString_POpen(struct VMGlobals *g, int numArgsPushed) { struct sc_process *process; PyrSlot *a = g->sp - 1; PyrSlot *b = g->sp; int err; if (!isKindOfSlot(a, class_string)) return errWrongType; char *cmdline = (char*)malloc(a->uo->size + 1); err = slotStrVal(a, cmdline, a->uo->size + 1); if(err) { free(cmdline); return errFailed; } #ifdef SC_IPHONE SetInt(a, 0); return errNone; #endif process = (struct sc_process *)malloc(sizeof(struct sc_process)); process->stream = sc_popen(cmdline, &process->pid, "r"); setvbuf(process->stream, 0, _IONBF, 0); process->postOutput = IsTrue(b); free(cmdline); if(process->stream == NULL) { free(process); return errFailed; } pthread_t thread; pthread_create(&thread, NULL, string_popen_thread_func, (void*)process); pthread_detach(thread); SetInt(a, process->pid); return errNone; }
int prLID_Open(VMGlobals *g, int numArgsPushed) { PyrSlot* args = g->sp - 1; int err; PyrObject* obj = SC_LID::getObject(args+0); if (!obj) return errWrongType; char path[PATH_MAX]; err = slotStrVal(args+1, path, sizeof(path)); if (err) return err; SC_LID* dev = new SC_LID(obj); err = dev->open(path); if (err) { delete dev; return err; } return errNone; }
int prStrFTime(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp - 1; PyrSlot *b = g->sp; PyrSlot *slots = a->uo->slots; if (IsNil(slots + 0)) { SetNil(a); return errNone; } struct tm tm0; if (slotIntVal(slots+0, &tm0.tm_year)) return errWrongType; tm0.tm_year -= 1900; if (slotIntVal(slots+1, &tm0.tm_mon)) return errWrongType; tm0.tm_mon --; if (slotIntVal(slots+2, &tm0.tm_mday)) return errWrongType; if (slotIntVal(slots+3, &tm0.tm_hour)) return errWrongType; if (slotIntVal(slots+4, &tm0.tm_min)) return errWrongType; if (slotIntVal(slots+5, &tm0.tm_sec)) return errWrongType; if (slotIntVal(slots+6, &tm0.tm_wday)) return errWrongType; char format[1024]; if (slotStrVal(b, format, 1024)) return errWrongType; char buffer[1024]; if (strftime(buffer, 1024, format, &tm0) != 0) { int size = strlen(buffer); PyrString *strobj = newPyrStringN(g->gc, size, 0, true); memcpy(strobj->s, buffer, size); SetObject(a, strobj); } else { error("could not convert the date to string with the give format"); return errFailed; } return errNone; }
int prGetHostByName(VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp; char hostname[256]; int err = slotStrVal(a, hostname, 255); if (err) return err; struct hostent *he = gethostbyname(hostname); if (!he) { #ifdef _WIN32 int err = WSAGetLastError(); error("gethostbyname(\"%s\") failed with error code %i.\n", hostname, err); #endif return errFailed; } SetInt(a, sc_ntohl(*(int*)he->h_addr)); return errNone; }
int ScIDE_GetDocSelectionRange(struct VMGlobals *g, int numArgsPushed) { if (!gIpcClient) { error("ScIDE not connected\n"); return errFailed; } PyrSlot * returnSlot = g->sp - numArgsPushed + 1; PyrSlot * docIDSlot = g->sp; char id[255]; if (slotStrVal( docIDSlot, id, 255 )) return errWrongType; QByteArray key = QByteArray(id); QPair<int, int>selection = gIpcClient->getSelectionMirrorForDocument(key); SetInt(returnSlot, selection.second); return errNone; }
int prFileRealPath(struct VMGlobals* g, int numArgsPushed ) { PyrSlot *a = g->sp - 1, *b = g->sp; char ipath[PATH_MAX]; char opath[PATH_MAX]; int err; err = slotStrVal(b, ipath, PATH_MAX); if (err) return err; bool isAlias = false; if(sc_ResolveIfAlias(ipath, opath, isAlias, PATH_MAX)!=0) { return errFailed; } boost::system::error_code error_code; boost::filesystem::path p = boost::filesystem::canonical(opath,error_code); if(error_code) { SetNil(a); return errNone; } strcpy(opath,p.string().c_str()); #if SC_DARWIN CFStringRef cfstring = CFStringCreateWithCString(NULL, opath, kCFStringEncodingUTF8); err = !CFStringGetFileSystemRepresentation(cfstring, opath, PATH_MAX); CFRelease(cfstring); if (err) return errFailed; #endif // SC_DARWIN PyrString* pyrString = newPyrString(g->gc, opath, 0, true); SetObject(a, pyrString); return errNone; }
int prBootInProcessServer(VMGlobals *g, int numArgsPushed) { PyrSlot *a = g->sp; if (!gInternalSynthServer.mWorld) { SetPrintFunc(&vpost); WorldOptions options = kDefaultWorldOptions; PyrObject *optionsObj = a->uo; PyrSlot *optionsSlots = optionsObj->slots; static char mInputStreamsEnabled[512], mOutputStreamsEnabled[512], mDeviceName[512]; int err; err = slotIntVal(optionsSlots + 0, (int*)&options.mNumAudioBusChannels); if (err) return err; err = slotIntVal(optionsSlots + 1, (int*)&options.mNumControlBusChannels); if (err) return err; err = slotIntVal(optionsSlots + 2, (int*)&options.mNumInputBusChannels); if (err) return err; err = slotIntVal(optionsSlots + 3, (int*)&options.mNumOutputBusChannels); if (err) return err; err = slotIntVal(optionsSlots + 4, (int*)&options.mNumBuffers); if (err) return err; err = slotIntVal(optionsSlots + 5, (int*)&options.mMaxNodes); if (err) return err; err = slotIntVal(optionsSlots + 6, (int*)&options.mMaxGraphDefs); if (err) return err; err = slotIntVal(optionsSlots + 8, (int*)&options.mBufLength); if (err) return err; if (NotNil(optionsSlots + 9)) { err = slotIntVal(optionsSlots + 9, (int*)&options.mPreferredHardwareBufferFrameSize); if (err) return err; } err = slotIntVal(optionsSlots + 10, (int*)&options.mRealTimeMemorySize); if (err) return err; err = slotIntVal(optionsSlots + 11, (int*)&options.mNumRGens); if (err) return err; err = slotIntVal(optionsSlots + 12, (int*)&options.mMaxWireBufs); if (err) return err; if (NotNil(optionsSlots + 13)) { err = slotIntVal(optionsSlots + 13, (int*)&options.mPreferredSampleRate); if (err) return err; } options.mLoadGraphDefs = IsTrue(optionsSlots + 14) ? 1 : 0; #ifdef SC_DARWIN err = slotStrVal(optionsSlots+15, mInputStreamsEnabled, 512); if(err) options.mInputStreamsEnabled = NULL; else options.mInputStreamsEnabled = mInputStreamsEnabled; err = slotStrVal(optionsSlots+16, mOutputStreamsEnabled, 512); if(err) options.mOutputStreamsEnabled = NULL; else options.mOutputStreamsEnabled = mOutputStreamsEnabled; #endif err = slotStrVal(optionsSlots+17, mDeviceName, 512); if(err) options.mInDeviceName = options.mOutDeviceName = NULL; else options.mInDeviceName = options.mOutDeviceName = mDeviceName; options.mNumSharedControls = gInternalSynthServer.mNumSharedControls; options.mSharedControls = gInternalSynthServer.mSharedControls; gInternalSynthServer.mWorld = World_New(&options); } return errNone; }
int prString_FindRegexp(struct VMGlobals *g, int numArgsPushed) { int err; PyrSlot *a = g->sp - 2; // source string PyrSlot *b = g->sp - 1; // pattern PyrSlot *c = g->sp; // offset if (!isKindOfSlot(b, class_string) || (NotInt(c))) return errWrongType; // post("prString_FindRegexp\n"); int maxfind = MAXREGEXFIND; int offset = slotRawInt(c); int stringsize = slotRawObject(a)->size + 1; int patternsize = slotRawObject(b)->size + 1; char *string = (char*)malloc(slotRawObject(a)->size + 1); err = slotStrVal(a, string, slotRawObject(a)->size + 1); if (err){ free(string); return err; } char *pattern = (char*)malloc(slotRawObject(b)->size + 1); err = slotStrVal(b, pattern, slotRawObject(b)->size + 1); if (err) return err; UParseError uerr; UErrorCode status = (UErrorCode)0; UChar *regexStr; UChar *ustring; regexStr = (UChar*)malloc((patternsize)*sizeof(UChar)); u_charsToUChars (pattern, regexStr, patternsize); ustring = (UChar*)malloc((stringsize)*sizeof(UChar)); u_charsToUChars (string+offset, ustring, stringsize-offset); unsigned flags = UREGEX_MULTILINE; int groupNumber = 0; SCRegExRegion * what; int indx = 0; int size = 0; URegularExpression *expression = uregex_open(regexStr, -1, flags, &uerr, &status); if(U_FAILURE(status)) goto nilout; if(!U_FAILURE(status)) { uregex_setText(expression, ustring, -1, &status); what = (SCRegExRegion*)malloc((maxfind)*sizeof(SCRegExRegion)); for(int i=0; i< maxfind; i++) { SCRegExRegion range; range.matched = false; what[i] = range; } int32_t groups = uregex_groupCount(expression, &status) + 1; if(U_FAILURE(status)) goto nilout; // post("groups: %i\n", groups); while (uregex_findNext(expression, &status) && size<maxfind) { if(U_FAILURE(status)) return errNone; for(int i=0; i< groups; ++i){ what[size].group = i; what[size].start = sc_clip(uregex_start(expression, i, &status), 0, stringsize) ; if(U_FAILURE(status)) goto nilout; what[size].end = sc_clip(uregex_end(expression, i, &status), 0, stringsize); what[size].matched = true; // post("index:%i, size:%i, start %i, end %i\n", i, size, what[i].start, what[i].end); size = indx++ + 1; if(U_FAILURE(status)) goto nilout; } } PyrObject *result_array = newPyrArray(g->gc, size, 0, true); result_array->size = 0; if (size>0) //(matched) { for (int i = 0; i < size; i++) { if (what[0].matched == false) { result_array->size++; SetNil(result_array->slots+i); } else { result_array->size++; int match_start = what[i].start; int match_length = what[i].end - what[i].start; // post("for i:%i, start %i, end %i\n", i, what[i].start, what[i].end); // char *match = (char*)malloc(match_length); char match[match_length]; strncpy(match, string + offset + match_start, match_length); match[match_length] = 0; PyrObject *array = newPyrArray(g->gc, 2, 0, true); array->size = 2; SetInt(array->slots, match_start + offset); PyrObject *matched_string = (PyrObject*)newPyrString(g->gc, match, 0, true); SetObject(array->slots+1, matched_string); g->gc->GCWrite(matched_string, array->slots + 1); SetObject(result_array->slots + i, array); g->gc->GCWrite(array, result_array->slots + i); } } } else { SetNil(a); } free(what); free(pattern); free(regexStr); free(ustring); free(string); SetObject(a, result_array); g->gc->GCWrite(result_array,a); //uregex_close(expression); return errNone; } nilout: free(string); free(what); free(pattern); free(regexStr); free(ustring); SetNil(a); return errNone; }