Plazza::~Plazza() { if (this->_body != NULL) { if (delwin(_body) == ERR) throw errorException(std::string("Can't destroy the window")); } if (this->_footer != NULL) { if (delwin(_footer) == ERR) throw errorException(std::string("Can't destroy the window")); } if (endwin() == ERR) throw errorException(std::string("Can't restore the window")); }
void Foam::IOerror::abort() { if (!throwExceptions_ && JobInfo::constructed) { jobInfo.add("FatalIOError", operator dictionary()); jobInfo.abort(); } if (abort_) { Perr<< endl << *this << endl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; printStack(Perr); ::abort(); } if (Pstream::parRun()) { Perr<< endl << *this << endl << "\nFOAM parallel run aborting\n" << endl; printStack(Perr); Pstream::abort(); } else { if (throwExceptions_) { // Make a copy of the error to throw IOerror errorException(*this); // Rewind the message buffer for the next error message messageStreamPtr_->rewind(); throw errorException; } else { Perr<< endl << *this << endl << "\nFOAM aborting\n" << endl; printStack(Perr); ::abort(); } } }
void Plazza::createNewKitchens(int res) { static int id = 0; res = (res / (_nbCooks * 2)) + 1; while (res > 0) { //Pipe creation Pipe *_pipe = new Pipe(id); //Kitchen creation Kitchen *kitchen = new Kitchen(_multiCooking, _nbCooks, _timeMillisecond, _body, id); displayKitchen(id); _kitchenList.push_back(kitchen); --res; ++id; //Fork init _pid = fork(); if (_pid == -1) throw errorException("Error : fork failed"); if (_pid == 0) // CHILD { _pipe->setSon(true); kitchen->run(_x, _y, _pipe); } else // FATHER { _pipe->setSon(false); kitchen->setPipe(_pipe); _pipeList.push_back(_pipe); } } }
void Foam::error::exit(const int errNo) { if (!throwExceptions_ && JobInfo::constructed) { jobInfo.add("FatalError", operator dictionary()); jobInfo.exit(); } if (abort_) { abort(); } if (Pstream::parRun()) { Perr<< endl << *this << endl << "\nFOAM parallel run exiting\n" << endl; Pstream::exit(errNo); } else { if (throwExceptions_) { // Make a copy of the error to throw error errorException(*this); // Rewind the message buffer for the next error message messageStreamPtr_->rewind(); throw errorException; } else { Perr<< endl << *this << endl << "\nFOAM exiting\n" << endl; ::exit(1); } } }
Mutex::~Mutex() { // std::cout << "unlock" << std::endl; if (pthread_mutex_unlock(&_mutex) != 0) throw errorException("Error : phtread_mutex_unlock failed\n");; }
Mutex::Mutex(pthread_mutex_t &mutex) : _mutex(mutex) { // std::cout << "lock" << std::endl; if (pthread_mutex_lock(&_mutex) != 0) throw errorException("Error : phtread_mutex_lock failed\n"); }
/** * Load a JavaScript file then run the function corresponding to the service by * passing the conf, inputs and outputs parameters by value as JavaScript * Objects. * * @param main_conf the conf maps containing the main.cfg settings * @param request the map containing the HTTP request * @param s the service structure * @param inputs the maps containing the inputs * @param outputs the maps containing the outputs * @return SERVICE_SUCCEEDED or SERVICE_FAILED if the service run, -1 * if the service failed to load or throw error at runtime. */ int zoo_js_support(maps** main_conf,map* request,service* s,maps **inputs,maps **outputs) { /*maps* main=*main_conf; maps* _inputs=*inputs; maps* _outputs=*outputs;*/ /* The class of the global object. */ JSClass global_class= { "global", JSCLASS_GLOBAL_FLAGS, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, JSCLASS_NO_OPTIONAL_MEMBERS }; /* JS variables. */ JSRuntime *rt; JSContext *cx; JSObject *global; /* Create a JS runtime. */ rt = JS_NewRuntime(8L * 1024L * 1024L); if (rt == NULL) return 1; /* Create a context. */ cx = JS_NewContext(rt,8192); if (cx == NULL){ return 1; } JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); JS_SetVersion(cx, JSVERSION_LATEST); JS_SetErrorReporter(cx, reportError); /* Create the global object. */ global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); /* Populate the global object with the standard globals, like Object and Array. */ if (!JS_InitStandardClasses(cx, global)){ return 1; } /* Define specific function and global variable to share with JS runtime */ jsval tmp=INT_TO_JSVAL(3); if (!JS_SetProperty(cx, global, "SERVICE_SUCCEEDED", &tmp)) return 1; tmp=INT_TO_JSVAL(4); if (!JS_SetProperty(cx, global, "SERVICE_FAILED", &tmp)) return 1; if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0)) return 1; if (!JS_DefineFunction(cx, global, "ZOOTranslate", JSTranslate, 4, 0)) return 1; if (!JS_DefineFunction(cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0)) return 1; if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0)) return 1; if (!JS_DefineFunction(cx, global, "importScripts", JSLoadScripts, 1, 0)) return 1; /** * Add private context object */ void* cxPrivate = request; JS_SetContextPrivate(cx,cxPrivate); map* tmpm1=getMap(request,"metapath"); char ntmp[1024]; map* cwdMap=getMapFromMaps(*main_conf,"main","servicePath"); if(cwdMap!=NULL) sprintf(ntmp,"%s",cwdMap->value); else getcwd(ntmp,1024); /** * Load the first part of the ZOO-API */ char *api0=(char*)malloc((strlen(ntmp)+17)*sizeof(char)); sprintf(api0,"%s/ZOO-proj4js.js",ntmp); #ifdef JS_DEBUG fprintf(stderr,"Trying to load %s\n",api0); #endif JSObject *api_script1=loadZooApiFile(cx,global,api0); free(api0); fflush(stderr); char *api1=(char*)malloc((strlen(ntmp)+13)*sizeof(char)); sprintf(api1,"%s/ZOO-api.js",ntmp); #ifdef JS_DEBUG fprintf(stderr,"Trying to load %s\n",api1); #endif JSObject *api_script2=loadZooApiFile(cx,global,api1); free(api1); fflush(stderr); /* Your application code here. This may include JSAPI calls to create your own custom JS objects and run scripts. */ //maps* out=*outputs; int res=SERVICE_FAILED; //maps* mc=*main_conf; map* tmpm2=getMap(s->content,"serviceProvider"); char *filename=(char*)malloc(strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+3); sprintf(filename,"%s/%s/%s",ntmp,tmpm1->value,tmpm2->value); filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+2]=0; #ifdef JS_DEBUG fprintf(stderr,"FILENAME %s\n",filename); #endif struct stat file_status; stat(filename, &file_status); //char *source=(char*)malloc(file_status.st_size); //uint16 lineno; jsval rval; JSBool ok ; JSObject *script = JS_CompileFile(cx, global, filename); if(script!=NULL){ (void)JS_ExecuteScript(cx, global, script, &rval); } else{ char tmp1[1024]; sprintf(tmp1,"Unable to load JavaScript file %s",filename); free(filename); errorException(*main_conf,tmp1,"NoApplicableCode",NULL); JS_MaybeGC(cx); JS_DestroyContext(cx); JS_DestroyRuntime(rt); JS_ShutDown(); return -1; } /* Call a function in obj's scope. */ jsval argv[3]; JSObject *jsargv1=JSObject_FromMaps(cx,*main_conf); argv[0] = OBJECT_TO_JSVAL(jsargv1); JSObject *jsargv2=JSObject_FromMaps(cx,*inputs); argv[1] = OBJECT_TO_JSVAL(jsargv2); JSObject *jsargv3=JSObject_FromMaps(cx,*outputs); argv[2] = OBJECT_TO_JSVAL(jsargv3); jsval rval1=JSVAL_NULL; #ifdef JS_DEBUG fprintf(stderr, "object %p\n", (void *) argv[2]); #endif ok = JS_CallFunctionName(cx, global, s->name, 3, argv, &rval1); #ifdef JS_DEBUG fprintf(stderr, "object %p\n", (void *) argv[2]); #endif JSObject *d; if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) { #ifdef JS_DEBUG fprintf(stderr,"Function run sucessfully !\n"); #endif /* Should get a number back from the service function call. */ ok = JS_ValueToObject(cx, rval1, &d); }else{ /* Unable to run JS function */ char tmp1[1024]; if(strlen(dbg)==0) sprintf(dbg,"No result was found after the function call"); sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg); #ifdef JS_DEBUG fprintf(stderr,"%s",tmp1); #endif errorException(*main_conf,tmp1,"NoApplicableCode",NULL); free(filename); JS_MaybeGC(cx); JS_DestroyContext(cx); JS_DestroyRuntime(rt); JS_ShutDown(); // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847 return -1; } //jsval t=OBJECT_TO_JSVAL(d); if(JS_IsArrayObject(cx,d)){ #ifdef JS_DEBUG fprintf(stderr,"An array was returned !\n"); #endif jsuint len; if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){ #ifdef JS_DEBUG fprintf(stderr,"outputs array is empty\n"); #endif } jsval tmp1; JSBool hasResult=JS_GetElement(cx,d,0,&tmp1); res=JSVAL_TO_INT(tmp1); #ifdef JS_DEBUG fprintf(stderr," * %d * \n",res); #endif if(res==SERVICE_SUCCEEDED){ jsval tmp2; JSBool hasElement=JS_GetElement(cx,d,1,&tmp2); if(hasElement==JS_TRUE){ freeMaps(outputs); free(*outputs); *outputs=mapsFromJSObject(cx,tmp2); } }else{ jsval tmp3; JSBool hasConf=JS_GetElement(cx,d,1,&tmp3); if(hasConf==JS_TRUE){ freeMaps(main_conf); free(*main_conf); *main_conf=mapsFromJSObject(cx,tmp3); } } } else{ #ifdef JS_DEBUG fprintf(stderr,"The service didn't return an array !\n"); #endif /** * Extract result */ jsval tmp1; JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1); res=JSVAL_TO_INT(tmp1); #ifdef JS_DEBUG fprintf(stderr," * %d * \n",res); #endif /** * Extract outputs when available. */ jsval tmp2; JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2); if(!JSVAL_IS_VOID(tmp2) && hasElement==JS_TRUE){ freeMaps(outputs); free(*outputs); *outputs=mapsFromJSObject(cx,tmp2); } JS_MaybeGC(cx); #ifdef JS_DEBUG if(JSVAL_IS_VOID(tmp2)) fprintf(stderr,"No outputs property returned\n"); else{ if(JS_IsArrayObject(cx,JSVAL_TO_OBJECT(tmp2))) fprintf(stderr,"outputs is an array as expected\n"); else fprintf(stderr,"outputs is not an array as expected\n"); } JS_MaybeGC(cx); #endif /** * Extract conf when available. */ jsval tmp3; JSBool hasConf=JS_GetProperty(cx,d,"conf",&tmp3); if(!JSVAL_IS_VOID(tmp3) && hasConf==JS_TRUE){ freeMaps(main_conf); free(*main_conf); *main_conf=mapsFromJSObject(cx,tmp3); } JS_MaybeGC(cx); #ifdef JS_DEBUG dumpMaps(*outputs); #endif } /* Cleanup. */ JS_MaybeGC(cx); JS_DestroyContext(cx); JS_DestroyRuntime(rt); JS_ShutDown(); free(filename); #ifdef JS_DEBUG fprintf(stderr,"Returned value %d\n",res); #endif return res; }
/** * Load a PHP script then run the function corresponding to the service by * passing the conf, inputs and outputs parameters by reference. * * @param main_conf the conf maps containing the main.cfg settings * @param request the map containing the HTTP request * @param s the service structure * @param real_inputs the maps containing the inputs * @param real_outputs the maps containing the outputs */ int zoo_php_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ maps* m=*main_conf; maps* inputs=*real_inputs; maps* outputs=*real_outputs; map* libp = getMapFromMaps(m, "main", "libPath"); int res=SERVICE_FAILED; map* tmp=getMap(s->content,"serviceProvider"); if (tmp == NULL || tmp->value == NULL) { return errorException(m, "Missing serviceProvider (library file)", "NoApplicableCode", NULL); } map* cwd=getMapFromMaps(m,"lenv","cwd"); #ifdef IGNORE_METAPATH map* mp = createMap("metapath", ""); #else map* mp = getMap(request, "metapath"); #endif char *scriptName; if (libp != NULL && libp->value != NULL) { scriptName = (char*) malloc((strlen(libp->value) + strlen(tmp->value) + 2)*sizeof(char)); sprintf (scriptName, "%s/%s", libp->value, tmp->value); } else { if(mp!=NULL && strlen(mp->value)>0){ scriptName=(char*)malloc((strlen(cwd->value)+strlen(mp->value)+strlen(tmp->value)+3)*sizeof(char)); sprintf(scriptName,"%s/%s/%s",cwd->value,mp->value,tmp->value); }else{ scriptName=(char*)malloc((strlen(cwd->value)+strlen(tmp->value)+2)*sizeof(char)); sprintf(scriptName,"%s/%s",cwd->value,tmp->value); } } zend_file_handle iscript; iscript.type=ZEND_HANDLE_FD; iscript.opened_path=NULL; iscript.filename=tmp->value; iscript.free_filename=0; FILE* temp=fopen(scriptName,"rb"); if(temp==NULL){ char tmp1[1024]; sprintf(tmp1,_("Unable to load the PHP file %s"),tmp->value); free(scriptName); return errorException(m,tmp1,"NoApplicableCode",NULL); } iscript.handle.fd=fileno(temp); php_embed_init(0,NULL PTSRMLS_CC); zend_try { zend_startup_module(&zoo_module_entry); php_execute_script(&iscript TSRMLS_CC); zval *iargs[3]; zval iret, ifunc,ifile; ZVAL_STRING(&ifunc, s->name, 0); iargs[0] = php_Array_from_maps(*main_conf); iargs[1] = php_Array_from_maps(*real_inputs); iargs[2] = php_Array_from_maps(*real_outputs); if((res=call_user_function(EG(function_table), NULL, &ifunc, &iret, 3, iargs TSRMLS_CC))==SUCCESS){ HashTable* t=HASH_OF(iargs[2]); HashTable* t1=HASH_OF(iargs[0]); freeMaps(real_outputs); free(*real_outputs); freeMaps(main_conf); free(*main_conf); *real_outputs=php_maps_from_Array(t); *main_conf=php_maps_from_Array(t1); res=Z_LVAL(iret); }else{ free(scriptName); fclose(temp); return errorException(m,"Unable to process.","NoApplicableCode",NULL);; } } zend_catch { free(scriptName); fclose(temp); return errorException(m,"Unable to process.","NoApplicableCode",NULL);; } zend_end_try(); free(scriptName); fclose(temp); php_embed_shutdown(TSRMLS_C); return res; }
void inputNode::run () { #if JSON==1 interpreter::to_jsonRun(this); #endif string aux0, aux1; char c, e; if (prompt_) { runNode *prompt = prompt_; nexpNode::resolved(prompt); #if JSON==1 interpreter::to_jsonRun(this); #endif ostringstream oss; if (stringNode *s = dynamic_cast<stringNode*>(prompt)) { oss << s->strvalue (); } else if (arithNode *n = dynamic_cast<arithNode*>(prompt)) { oss << n->numvalue (); } else if (logicNode *l = dynamic_cast<logicNode*>(prompt)) { oss << l->boolvalue (); } else { throw errorException ( "Wrong data type", "inputNode: prompt is a wrong type", 1 ); } cout << oss.str(); #if JSON==1 interpreter::to_jsonOut(this, oss.str() + ((type_ == 1)?" ":"\n")); #endif switch (type_) { default: case 0: cout << endl; break; case 1: cout << " "; break; } } #if JSON==1 if(interpreter::get()->json) { aux0 = interpreter::to_jsonInput(this); }else { #endif switch (type_) { default: case 0: getline(cin, aux0);; while (true) { getline(cin, aux1); if (aux1.empty()) { break; } else { aux0 += ('\n' + aux1); } } break; case 1: getline(cin, aux0); break; } #if JSON==1 } #endif strNode s (aux0); asigNode asig (id_, &s); asig.run (); strvalue_ = aux0; #if JSON==1 interpreter::to_jsonSetValue(this, strvalue_); #endif }
/** * Try to load file from cache or download a remote file if not in cache * * @param m the maps containing the settings of the main.cfg file * @param content the map to update * @param hInternet the HINTERNET pointer * @param url the url to fetch * @return 0 */ int loadRemoteFile(maps** m,map** content,HINTERNET* hInternet,char *url){ char* fcontent = NULL; char* cached=isInCache(*m,url); char *mimeType=NULL; int fsize=0; map* t=getMap(*content,"xlink:href"); if(t==NULL){ t=getMap((*content),"href"); addToMap(*content,"xlink:href",url); } if(cached!=NULL){ struct stat f_status; int s=stat(cached, &f_status); if(s==0){ fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); FILE* f=fopen(cached,"rb"); fread(fcontent,f_status.st_size,1,f); fsize=f_status.st_size; fcontent[fsize]=0; fclose(f); addToMap(*content,"cache_file",cached); } cached[strlen(cached)-1]='m'; s=stat(cached, &f_status); if(s==0){ mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1)); FILE* f=fopen(cached,"rb"); fread(mimeType,f_status.st_size,1,f); mimeType[f_status.st_size]=0; fclose(f); } }else{ addRequestToQueue(m,hInternet,url,true); return 0; } if(fsize==0){ return errorException(*m, _("Unable to download the file."), "InternalError",NULL); } if(mimeType!=NULL){ addToMap(*content,"fmimeType",mimeType); } map* tmpMap=getMapOrFill(content,"value",""); free(tmpMap->value); tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); if(tmpMap->value==NULL || fcontent == NULL) return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); char ltmp1[256]; sprintf(ltmp1,"%d",fsize); addToMap(*content,"size",ltmp1); if(cached==NULL){ addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); } else{ addToMap(*content,"isCached","true"); map* tmp=getMapFromMaps(*m,"main","cacheDir"); if(tmp!=NULL){ map *c=getMap((*content),"xlink:href"); char* md5str=getMd5(c->value); char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); sprintf(fname,"%s/%s.zca",tmp->value,md5str); addToMap(*content,"cache_file",fname); free(fname); } } free(fcontent); free(mimeType); free(cached); return 0; }
/** * Read the downloaded file for a specific input * * @param m the maps containing the settings of the main.cfg file * @param in the input * @param index the input index * @param hInternet the internet connection * @param error the error map pointer * @return 0 in case of success, -1 in case of failure */ int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){ map* tmp1; char sindex[5]; maps* content=*in; map* length=getMap(content->content,"length"); int shouldClean=-1; if(length==NULL){ length=createMap("length","1"); shouldClean=1; } for(int i=0;i<atoi(length->value);i++){ char* fcontent; char *mimeType=NULL; int fsize=0; char cname[15]; char vname[11]; char vname1[11]; char sname[9]; char mname[15]; char icname[14]; char xname[16]; char oname[12]; if(*index>0) sprintf(vname1,"value_%d",*index); else sprintf(vname1,"value"); if(i>0){ tmp1=getMap(content->content,cname); sprintf(cname,"cache_file_%d",i); sprintf(vname,"value_%d",i); sprintf(sname,"size_%d",i); sprintf(mname,"mimeType_%d",i); sprintf(icname,"isCached_%d",i); sprintf(xname,"Reference_%d",i); sprintf(oname,"Order_%d",i); }else{ sprintf(cname,"cache_file"); sprintf(vname,"value"); sprintf(sname,"size"); sprintf(mname,"mimeType"); sprintf(icname,"isCached"); sprintf(xname,"Reference"); sprintf(oname,"Order"); } map* tmap=getMap(content->content,oname); sprintf(sindex,"%d",*index+1); if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ if(getMap(content->content,icname)==NULL){ fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); if(fcontent == NULL){ errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); return -1; } size_t dwRead; InternetReadFile(hInternet->ihandle[*index], (LPVOID)fcontent, hInternet->ihandle[*index].nDataLen, &dwRead); fcontent[hInternet->ihandle[*index].nDataLen]=0; fsize=hInternet->ihandle[*index].nDataLen; if(hInternet->ihandle[*index].mimeType==NULL) mimeType=zStrdup("none"); else mimeType=zStrdup(hInternet->ihandle[*index].mimeType); map* tmpMap=getMapOrFill(&(*in)->content,vname,""); free(tmpMap->value); tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); if(tmpMap->value==NULL){ return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); } memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); if(hInternet->ihandle[*index].code!=200){ const char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d."); char *error_msg=(char*)malloc((strlen(error_rep_str)+strlen(content->name)+4)*sizeof(char)); sprintf(error_msg,error_rep_str,content->name,hInternet->ihandle[*index].code); if(*error==NULL){ *error=createMap("text",error_msg); addToMap(*error,"locator",content->name); addToMap(*error,"code","InvalidParameterValue"); }else{ int nb=1; map* tmpMap=getMap(*error,"length"); if(tmpMap!=NULL) nb=atoi(tmpMap->value); setMapArray(*error,"text",nb,error_msg); setMapArray(*error,"locator",nb,content->name); setMapArray(*error,"code",nb,"InvalidParameterValue"); } return -1; } char ltmp1[256]; sprintf(ltmp1,"%d",fsize); map* tmp=getMapFromMaps(*m,"main","cacheDir"); if(tmp!=NULL){ char* md5str=getMd5(tmp1->value); char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); sprintf(fname,"%s/%s.zca",tmp->value,md5str); addToMap((*in)->content,cname,fname); free(fname); } addToMap((*in)->content,sname,ltmp1); addToMap((*in)->content,mname,mimeType); addToCache(*m,tmp1->value,fcontent,mimeType,fsize, NULL, 0); free(fcontent); free(mimeType); *index++; } } } if(shouldClean>0){ freeMap(&length); free(length); } return 0; }