char * QMkString(char * source) { char * string; if(source) { int len = 0; int i, j = 0; char ch; for(i = 0; (ch = source[i]); i++) { len++; if(ch == '\"' || ch == '\\') len++; } string = __ecereNameSpace__ecere__com__eSystem_New(sizeof(char) * (len + 3)); string[j++] = '\"'; for(i = 0; (ch = source[i]); i++) { if(ch == '\"' || ch == '\\') string[j++] = '\\'; string[j++] = ch; } string[j++] = '\"'; string[j] = '\0'; } else string = __ecereNameSpace__ecere__sys__CopyString("0"); return string; }
_DualPipe * _DualPipeOpen(PipeOpenMode mode, const char * commandLine, const char * env, void ** inputPtr, void ** outputPtr) { _DualPipe * f = null; #define PIPE_READ 0 #define PIPE_WRITE 1 #if !defined(__WIN32__) { FILE * input = null, * output = null; int hInput[2] = { 0 }, hOutput[2] = { 0 }; pid_t pid; bool result = true; if((mode & POM_error) || (mode & POM_output)) if(pipe(hOutput)) result = false; if((mode & POM_input)) if(pipe(hInput)) result = false; if(result) { pid = fork(); if(pid > 0) { // This process if(hInput[PIPE_READ]) { close(hInput[PIPE_READ]); output = fdopen(hInput[PIPE_WRITE],"w"); } if(hOutput[PIPE_WRITE]) { close(hOutput[PIPE_WRITE]); input = fdopen(hOutput[PIPE_READ],"r"); } } else if(pid == 0) { // Child process char * tokens[129]; int numTokens; char * commandLineCopy = __ecereNameSpace__ecere__sys__CopyString(commandLine); if(hInput[PIPE_WRITE]) close(hInput[PIPE_WRITE]); if(hOutput[PIPE_READ]) close(hOutput[PIPE_READ]); if((mode & POM_error) && hOutput[PIPE_WRITE] != STDERR_FILENO) dup2(hOutput[PIPE_WRITE], STDERR_FILENO); if((mode & POM_output) && hOutput[PIPE_WRITE] != STDOUT_FILENO) dup2(hOutput[PIPE_WRITE], STDOUT_FILENO); if(hOutput[PIPE_WRITE] && hOutput[PIPE_WRITE] != STDOUT_FILENO) close(hOutput[PIPE_WRITE]); if((mode & POM_input) && hInput[PIPE_READ] != STDIN_FILENO) { dup2(hInput[PIPE_READ], STDIN_FILENO); close(hInput[PIPE_READ]); } #if 0 //#ifdef _DEBUG fprintf(stderr, "\n_DualPipeOpen (in child): %s\n\n", commandLineCopy); #endif numTokens = __ecereNameSpace__ecere__sys__Tokenize(commandLineCopy, sizeof(tokens) / sizeof(tokens[0]) - 1, tokens, forArgsPassing); #if 0 //#ifdef _DEBUG { int c; for(c=0; c<numTokens; c++) fprintf(stderr, "argv[%d]: %s\n", c, tokens[c]); fprintf(stderr, "\n"); } #endif tokens[numTokens] = null; if(env) { char * envTokens[129]; char * envCopy = __ecereNameSpace__ecere__sys__CopyString(env); int numEnvTokens = __ecereNameSpace__ecere__sys__Tokenize(envCopy, sizeof(envTokens) / sizeof(envTokens[0]) - 1, envTokens, false); envTokens[numEnvTokens] = null; if(execve(tokens[0], tokens, envTokens) < 0) { __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); __ecereNameSpace__ecere__com__eSystem_Delete(envCopy); exit(1); } __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); __ecereNameSpace__ecere__com__eSystem_Delete(envCopy); exit(0); } else { if(execvp(tokens[0], (char **)tokens) < 0) { __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); exit(1); } __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); exit(0); } } if(input || output) { f = calloc(1, sizeof(_DualPipe)); *inputPtr = f->input = input; *outputPtr = f->output = output; f->pid = pid; } } else { if(hInput[PIPE_READ]) close(hInput[PIPE_READ]); if(hInput[PIPE_WRITE]) close(hInput[PIPE_WRITE]); if(hOutput[PIPE_WRITE]) close(hOutput[PIPE_WRITE]); if(hOutput[PIPE_READ]) close(hOutput[PIPE_READ]); } } #else { HANDLE hOutput[2] = { 0 },hOutputRead = 0; HANDLE hInput[2] = { 0 }, hInputWrite = 0; HANDLE hError[2] = { 0 }, hErrorRead = 0; HANDLE hStdErr = 0, hStdIn = 0, hStdOut = 0; SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi = { 0 }; STARTUPINFO si = { 0 }; uint16 * _wcommandLine = __ecereNameSpace__ecere__sys__UTF8toUTF16(commandLine, null); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = null; sa.bInheritHandle = TRUE; // Force redirecting if GUI application if(!(mode & POM_error)) hStdErr = GetStdHandle(STD_ERROR_HANDLE); if(!(mode & POM_input)) hStdIn = GetStdHandle(STD_INPUT_HANDLE); if(!(mode & POM_output)) hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if((mode & POM_output) || !hStdOut) CreatePipe(&hOutput[PIPE_READ],&hOutput[PIPE_WRITE],&sa,0); if(( (mode & POM_error) && !(mode & POM_output)) || (!(mode & POM_error) && !hStdErr)) CreatePipe(&hError[PIPE_READ], &hError[PIPE_WRITE],&sa,0); if((mode & POM_input) || !hStdIn) CreatePipe(&hInput[PIPE_READ], &hInput[PIPE_WRITE], &sa,0); if(hInput[PIPE_READ]) DuplicateHandle(GetCurrentProcess(),hInput[PIPE_WRITE],GetCurrentProcess(),&hInputWrite,0,FALSE,DUPLICATE_SAME_ACCESS); if((mode & POM_error) && (mode & POM_output)) { DuplicateHandle(GetCurrentProcess(),hOutput[PIPE_READ],GetCurrentProcess(),&hOutputRead,0,FALSE,DUPLICATE_SAME_ACCESS); DuplicateHandle(GetCurrentProcess(),hOutput[PIPE_WRITE],GetCurrentProcess(),&hError[PIPE_WRITE],0,TRUE,DUPLICATE_SAME_ACCESS); } else { if(hOutput[PIPE_WRITE]) DuplicateHandle(GetCurrentProcess(),hOutput[PIPE_READ],GetCurrentProcess(),&hOutputRead,0,FALSE,DUPLICATE_SAME_ACCESS); if(hError[PIPE_WRITE]) DuplicateHandle(GetCurrentProcess(),hError[PIPE_READ],GetCurrentProcess(),&hErrorRead,0,FALSE,DUPLICATE_SAME_ACCESS); } if(hOutput[PIPE_READ]) CloseHandle(hOutput[PIPE_READ]); if(hError[PIPE_READ]) CloseHandle(hError[PIPE_READ]); if(hInput[PIPE_WRITE]) CloseHandle(hInput[PIPE_WRITE]); // Set up the start up info struct. si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = (mode & POM_showWindow) ? SW_SHOW : SW_HIDE; si.hStdOutput = hOutput[PIPE_WRITE] ? hOutput[PIPE_WRITE] : hStdOut; si.hStdInput = hInput [PIPE_READ] ? hInput [PIPE_READ] : hStdIn; if((mode & POM_error) && (mode & POM_output)) si.hStdError = hOutput[PIPE_WRITE]; else if((mode & POM_error)) si.hStdError = hError[PIPE_WRITE]; else si.hStdError = hError[PIPE_WRITE] ? hError[PIPE_WRITE] : hStdErr; if(CreateProcess(null,_wcommandLine,null,null,TRUE, 0,(void *)env,null ,&si,&pi)) { CloseHandle(pi.hThread); f = calloc(1, sizeof(_DualPipe)); f->inputHandle = hOutputRead; f->outputHandle = hInputWrite; f->hProcess = pi.hProcess; f->pid = pi.dwProcessId; *inputPtr = null; *outputPtr = null; } else { if(hOutputRead) CloseHandle(hOutputRead); if(hInputWrite) CloseHandle(hInputWrite); if(hErrorRead) CloseHandle(hErrorRead); } if(hInput [PIPE_READ]) CloseHandle(hInput [PIPE_READ]); if(hOutput[PIPE_WRITE]) CloseHandle(hOutput[PIPE_WRITE]); if(hError [PIPE_WRITE]) CloseHandle(hError [PIPE_WRITE]); __ecereNameSpace__ecere__com__eSystem_Delete(_wcommandLine); } #endif return f; }
struct Specifier * CopySpecifier(struct Specifier * spec) { if(spec) switch(spec->type) { case 0: return MkSpecifier(spec->specifier); case 2: { struct Identifier * id = CopyIdentifier(spec->id); struct __ecereNameSpace__ecere__sys__OldList * list = MkList(); struct Enumerator * enumerator; if(spec->list) { for(enumerator = (*spec->list).first; enumerator; enumerator = enumerator->next) ListAdd(list, CopyEnumerator(enumerator)); } return MkEnum(id, list); } case 3: case 4: { struct Identifier * id = CopyIdentifier(spec->id); struct __ecereNameSpace__ecere__sys__OldList * list = (((void *)0)); struct ClassDef * def; struct Specifier * s; if(spec->definitions) { list = MkList(); if(spec->list) { for(def = (*spec->list).first; def; def = def->next) ListAdd(list, CopyClassDef(def)); } } s = MkStructOrUnion(spec->type, id, list); s->extDeclStruct = CopyExtDecl(spec->extDeclStruct); return s; } case 1: { struct Specifier * copy = (copy = __ecereNameSpace__ecere__com__eInstance_New(__ecereClass_Specifier), copy->type = 1, copy->name = __ecereNameSpace__ecere__sys__CopyString(spec->name), copy->symbol = spec->symbol, copy->templateArgs = (((void *)0)), copy); return copy; } case 7: return MkSpecifierSubClass(CopySpecifier(spec->_class)); case 8: return __extension__ ({ struct Specifier * __ecereInstance1 = __ecereNameSpace__ecere__com__eInstance_New(__ecereClass_Specifier); __ecereInstance1->loc = spec->loc, __ecereInstance1->type = 8, __ecereInstance1->templateParameter = spec->templateParameter, __ecereInstance1; }); case 5: return MkSpecifierExtended(CopyExtDecl(spec->extDecl)); }