PageRenderTime 480ms CodeModel.GetById 121ms app.highlight 212ms RepoModel.GetById 140ms app.codeStats 0ms

/windows/src/Winport/Sysdep/sysmisc.cpp

https://github.com/coliveira/wintexmacs-copy
C++ | 799 lines | 546 code | 190 blank | 63 comment | 136 complexity | 434ab176f36cb33c7a4d45a0f419619e MD5 | raw file
  1/******************************************************************************
  2* MODULE     : sysmisc.cpp
  3* DESCRIPTION: Windows version of various POSIX functions
  4* COPYRIGHT  : (C) 2003 Dan Martens dan_martens@lycos.com
  5*******************************************************************************
  6* This software falls under the GNU general public license and comes WITHOUT
  7* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
  8* If you don't have this file, write to the Free Software Foundation, Inc.,
  9* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 10******************************************************************************/
 11
 12#include <stdlib.h>
 13#include <windows.h>
 14#include <time.h>
 15#include <stdio.h>
 16#include <shellapi.h>
 17#include <sys/misc.h>
 18#include <sys/socket.h>
 19
 20#define BUFFER_SIZE	1024
 21
 22typedef struct BUFFER_STRUCT_{
 23	char *buffer;
 24	DWORD bufferSize;
 25}BUFFER_STRUCT;
 26
 27BUFFER_STRUCT getEnvBuffer = {NULL, 0};
 28
 29void GetLocaleLanguage(char *buffer);
 30int ShellOut(char *file, char *operation);
 31int WaitForProcess(HANDLE process);
 32int HandlePkCompilation(char *command);
 33int HandleTfmCompilation(char *command);
 34char* stristr(char *theString, char *toSearch);
 35int RunGhostScript(char *cmd);
 36int HandleModifierMapRequest(char *commandLine);
 37void ReplaceArg(char *theString, char *argString, char *replaceString);
 38
 39bool SysMiscInitialize(){
 40
 41/*	char path[1024], texHome[1024], *temp;
 42
 43	temp = getenv("Path");
 44	sprintf(path, "%s;", temp);
 45	temp = getenv("TEX_HOME");
 46
 47	if(!temp)
 48		return false;
 49
 50	sprintf(texHome, "%s\\miktex\\bin", temp);
 51	strcat(path, texHome);
 52
 53	printf("%s", path);
 54	setenv("Path", path, 0);
 55*/
 56	return true;
 57}
 58
 59void ConvertPathing(char *toConvert){
 60
 61	if(!toConvert)
 62		return;
 63
 64	for(int i = 0; i < strlen(toConvert); i++){
 65		if(toConvert[i] == '/')
 66			toConvert[i] = '\\';
 67	}
 68
 69}
 70
 71int gettimeofday(struct timeval *tp, void *tzp){
 72	SYSTEMTIME sysTime;
 73
 74	GetSystemTime(&sysTime);
 75	
 76	tp->tv_sec = time(NULL);
 77	tp->tv_usec = sysTime.wMilliseconds * 1000;
 78
 79	return 0;
 80}
 81
 82int setenv(const char *name, const char *value, int rewrite){
 83
 84//	printf("Call to setenv, %s=%s\n", name, value);
 85	if(SetEnvironmentVariable(name, value))
 86		return 1;
 87	else
 88		return 0;
 89
 90	return 0;
 91}
 92
 93OS_VERSION GetOsVersion(){
 94
 95	OSVERSIONINFO vInfo;
 96
 97	vInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
 98	GetVersionEx(&vInfo);
 99
100	switch(vInfo.dwMajorVersion){
101
102		case 3: return OS_VERSION_WIN_NT351;
103		case 4:
104				switch(vInfo.dwMinorVersion){
105					case 0: 
106						switch(vInfo.dwPlatformId){
107							case VER_PLATFORM_WIN32_WINDOWS: 
108								return OS_VERSION_WIN_95;
109							case VER_PLATFORM_WIN32_NT:
110								return OS_VERSION_WIN_NT40;
111							default:
112								return OS_VERSION_UNSUPPORTED;
113						}
114
115					case 10: return OS_VERSION_WIN_98;
116					case 90: return OS_VERSION_WIN_ME;
117					default: return OS_VERSION_UNSUPPORTED;
118				}
119		case 5:
120
121				switch(vInfo.dwMinorVersion){
122					case 0: return OS_VERSION_WIN_2000;
123					case 1: return OS_VERSION_WIN_XP;
124					case 2: return OS_VERSION_WIN_SERVER_2003;
125					default: return OS_VERSION_UNSUPPORTED;
126				}
127		default: return OS_VERSION_UNSUPPORTED;
128	}
129
130}
131
132bool SetBufferSize(BUFFER_STRUCT *buffer, DWORD size){
133
134	if(buffer->bufferSize >= size)
135		return true;
136
137	if((buffer->buffer = (char*)realloc(buffer->buffer, size)) == NULL)
138		return false;
139
140	buffer->bufferSize = size;
141	memset(buffer->buffer, 0, buffer->bufferSize);
142
143	return true;
144}
145
146char* getenv(char *name){
147
148	char temp[BUFFER_SIZE];
149	OS_VERSION version;
150
151	//printf("Call to getenv, %s\n", name);
152	memset(getEnvBuffer.buffer, 0, getEnvBuffer.bufferSize);
153	version = GetOsVersion();
154	strcpy(temp, name);
155
156	if(strcmp(name, "TEXMACS_STYLE_PATH") == 0)
157		int x = 1;
158
159	if(temp[0] == '$')
160		memcpy(temp, &temp[1], strlen(temp));
161
162	if(strcmp(temp, "HOME") == 0){
163
164		if((version == OS_VERSION_WIN_2000)
165			|| (version == OS_VERSION_WIN_XP)
166			|| (version == OS_VERSION_WIN_SERVER_2003)
167			|| (version == OS_VERSION_WIN_NT40))
168			strcpy(temp, "USERPROFILE");
169		else
170			strcpy(temp, "TEXMACS_PATH");
171	}
172
173	if(strcmp(temp, "LANG") == 0){
174		SetBufferSize(&getEnvBuffer, 100);
175		GetLocaleLanguage(getEnvBuffer.buffer);
176		//printf("Returning value %s=%s\n", name, getEnvBuffer.buffer);
177		return getEnvBuffer.buffer;
178	}
179
180	else{
181
182getVariable:
183		int retVal = GetEnvironmentVariable(temp, getEnvBuffer.buffer, getEnvBuffer.bufferSize);
184
185		if((retVal > 0) && (retVal <= getEnvBuffer.bufferSize)){
186			//printf("Returning value %s=%s\n", name, getEnvBuffer.buffer);	
187			return getEnvBuffer.buffer;
188		}
189		else if((retVal > getEnvBuffer.bufferSize) && SetBufferSize(&getEnvBuffer, (DWORD)retVal)){
190			goto getVariable;
191		}
192
193		else{
194			//printf("Invalid Environment Variable, returning NULL");
195			return NULL;
196		}
197	}
198}
199
200char* ExpandEnvString(char *toExpand){
201
202	static char returnBuffer[MAX_PATH];
203	char tempBuffer[MAX_PATH];
204	int len;
205
206	strcpy(tempBuffer, toExpand);
207	len = strlen(tempBuffer);
208
209	for(int i = 0; i < len; i++){
210		if(tempBuffer[i] == '$'){
211			tempBuffer[i] = '%';
212			for(int x = i; x <= len; x++){
213				if((tempBuffer[x] == '\\') 
214					|| (tempBuffer[x] == '\"')
215					|| (x == len)){
216					memcpy(&tempBuffer[x + 1], &tempBuffer[x], (len - x) + 1);
217					tempBuffer[x] = '%';
218					len++;
219					break;
220				}
221				i++;
222			}
223		}
224	}
225
226	ExpandEnvironmentStrings(tempBuffer, returnBuffer, MAX_PATH);
227
228	return returnBuffer;
229}
230
231char* GetWorkingDirectory(){
232
233	static char buffer[1024];
234
235	GetCurrentDirectory(1024, buffer);
236
237	return buffer;
238}
239
240void GetLocaleLanguage(char *buffer){
241
242	LANGID langid;
243	char *ptr;
244
245	langid = GetSystemDefaultLangID();
246
247	switch(langid){
248		case 0x0809: ptr = "en_GB"; break;
249		case 0x0405: ptr = "cs_CZ"; break;
250		case 0x0413:  
251		case 0x0813: ptr = "nl_NL"; break;
252		case 0x0409:
253		case 0x0c09:
254		case 0x1009:
255		case 0x1409:
256		case 0x1809:
257		case 0x1c09:
258		case 0x2009:
259		case 0x2409:
260		case 0x2809:
261		case 0x2c09:
262		case 0x3009:
263		case 0x3409: ptr = "en_US"; break;
264		case 0x040c:
265		case 0x080c:
266		case 0x0c0c:
267		case 0x100c:
268		case 0x140c:
269		case 0x180c: ptr = "fr_FR"; break;
270		case 0x0407:
271		case 0x0807:
272		case 0x0c07:
273		case 0x1007:
274		case 0x1407: ptr = "de_DE"; break;
275		case 0x040e: ptr = "hu_HU"; break;
276		case 0x0410:
277		case 0x0810: ptr = "it_IT"; break;
278		case 0x0415: ptr = "pl_PL"; break;
279		case 0x0416:
280		case 0x0816: ptr = "pt_PT"; break;
281		case 0x0418: ptr = "ro_RO"; break;
282		case 0x0419: ptr = "ru_RU"; break;
283		case 0x040a:
284		case 0x080a:
285		case 0x0c0a:
286		case 0x100a:
287		case 0x140a:
288		case 0x180a:
289		case 0x1c0a:
290		case 0x200a:
291		case 0x240a:
292		case 0x280a:
293		case 0x2c0a:
294		case 0x300a:
295		case 0x340a:
296		case 0x380a:
297		case 0x3c0a:
298		case 0x400a:
299		case 0x440a:
300		case 0x480a:
301		case 0x4c0a:
302		case 0x500a: ptr = "es_ES"; break;
303		case 0x041d:
304		case 0x081d: ptr = "sv_SV"; break;
305		case 0x0422: ptr = "uk_UA"; break;
306		default: ptr = "en_US"; break;
307	}
308
309	strcpy(buffer, ptr);
310}
311
312void QuoteArgs(char **toQuote, int numArgs){
313
314	char *ptr;
315	int len;
316
317	len = strlen(*toQuote);
318	ptr = *toQuote;
319
320	for(int i = 0; i < numArgs; i++){
321		
322		while(isalpha(*ptr)) ptr++;
323		while(isspace(*ptr)) ptr++;
324
325		if(*ptr == '\"'){
326				ptr++;
327				while(*ptr != '\"') ptr++;
328		}
329		
330		else{
331			memcpy(ptr + 1, ptr, len - (ptr - *toQuote) + 1);
332			len++;
333
334			*ptr = '\"';
335
336			while(!isspace(*ptr) && (*ptr != '\0')) ptr++;
337
338			memcpy(ptr + 1, ptr, len - (ptr - *toQuote) + 1);
339			len++;
340			*ptr = '\"';
341		}
342		ptr++;
343		*toQuote = ptr;
344	}
345
346}
347
348int _system(char *command){
349
350	char temp[1024];
351	char *ptr, *ptr2, *ptr3;
352	int len;
353	bool doneParsing = false;
354
355	strcpy(temp, command);
356	len = strlen(temp);
357	ptr = temp;
358
359	//printf("Call to _system to run \"%s\"\n", command);
360
361	for(int i = 0; i < len; i++){
362		if(temp[i] == '$'){
363			temp[i] = '%';
364			for(int x = i; x <= len; x++){
365				if((temp[x] == '\\') 
366					|| (temp[x] == '\"')
367					|| (x == len)){
368					memcpy(&temp[x + 1], &temp[x], (len - x) + 1);
369					temp[x] = '%';
370					len++;
371					break;
372				}
373				i++;
374			}
375		}
376	}
377
378	while(!doneParsing){
379
380		len = strlen(ptr);
381		/*if((ptr2 = strstr(ptr, "mkdir")) != NULL){
382
383			ptr2 += 6;
384			memcpy(ptr2 + 1, ptr2, len - (ptr2 - temp) + 1);
385			len++;
386			*ptr2 = '\"';
387			ptr2++;
388
389			while((*ptr2 != '\0') 
390					&& (*ptr2 != '>') 
391					&& (*ptr2 != '<')
392					&& (*ptr2 != '|'))
393					ptr2++;
394
395			memcpy(ptr2 + 1, ptr2, len - (ptr2 - temp) + 1);
396			len++;
397			*ptr2 = '\"';
398			ptr = ptr2;
399		}*/
400
401		if((ptr2 = strstr(ptr, "mkdir ")) != NULL){
402
403//			QuoteArgs(&ptr2, 1);
404		}
405
406
407		else if((ptr2 = strstr(ptr, "cp ")) != NULL){
408			
409			memcpy(ptr2 + 2, ptr2, len - (ptr2 - temp) + 1);
410			ptr2[0] = 'c';
411			ptr2[1] = 'o';
412			ptr2[2] = 'p';
413			ptr2[3] = 'y';
414			ptr2[4] = ' ';
415
416//			QuoteArgs(&ptr2, 2);
417
418
419		}
420
421		else if((ptr2 = strstr(ptr, "cat ")) != NULL){
422
423			memcpy(ptr2 + 1, ptr2, len - (ptr2 - temp) + 1);
424			ptr2[0] = 't';
425			ptr2[1] = 'y';
426			ptr2[2] = 'p';
427			ptr2[3] = 'e';
428			ptr2[4] = ' ';
429
430//			QuoteArgs(&ptr2, 1);
431
432			while((*ptr2 == ' ') || (*ptr2 == '>') && (*ptr2 != '\0')) ptr2++;
433
434//			QuoteArgs(&ptr2, 1);
435		}
436		
437		else if((ptr2 = strstr(ptr, "pwd ")) != NULL){
438		
439			memcpy(ptr2 + 2, ptr2 + 3, len - (ptr2 - temp) + 1);
440			ptr2[0] = 'c';
441			ptr2[1] = 'd';
442			ptr2[2] = ' ';
443		}
444
445		else if((ptr2 = strstr(ptr, "rm ")) != NULL){
446
447			ReplaceArg(ptr2, "-f", "/Q");
448				
449			memcpy(ptr2 + 1, ptr2, len - (ptr2 - temp) + 1);
450			ptr2[0] = 'd';
451			ptr2[1] = 'e';
452			ptr2[2] = 'l';
453			ptr2[3] = ' ';
454
455	//		QuoteArgs(&ptr2, 1);
456		}
457
458		if((ptr2 = strstr(ptr, "lpr ")) != NULL){
459
460			return ShellOut(ptr2 + 4, "print");
461		}
462
463		else if ((ptr2 = strstr(ptr, "ghostview ")) != NULL){
464
465			return ShellOut(ptr2 + 10, "open");
466		}
467
468		else if ((ptr2 = strstr(ptr, "gs ")) != NULL){
469
470			return RunGhostScript(ptr2 + 3);
471		}
472
473		else if ((ptr2 = stristr(ptr, "makepk ")) != NULL){
474
475			return HandlePkCompilation(ptr2);
476		}
477
478		else if ((ptr2 = stristr(ptr, "maketfm ")) != NULL){
479
480			return HandleTfmCompilation(ptr2);
481		}
482
483		else if((ptr2 = stristr(ptr, "xmodmap ")) != NULL){
484			
485			return HandleModifierMapRequest(ptr2);
486		}
487
488		else
489			doneParsing = true;
490
491		ptr = ptr2;
492	}
493
494	//printf("_system converted UNIX command \"%s\" to Windows command \"%s\"\n", command, temp);
495	return system(temp);
496	//return system(ExpandEnvString(temp));
497}
498
499void ReplaceArg(char *theString, char *argString, char *replaceString){
500
501	char *ptr;
502	int length1, length2, length3;
503
504	ptr = strstr(theString, argString);
505
506	if(ptr == NULL) return;
507
508	length3 = strlen(theString);
509	length1 = strlen(argString);
510	length2 = strlen(replaceString);
511
512	if(length1 < length2)
513		memcpy(ptr, ptr + (length2 - length1), length3 - (ptr - theString));
514
515	else if(length1 > length2)
516		memcpy(ptr + (length1 - length2), ptr - (length1 - length2), length3 - (ptr - theString));
517	
518	memcpy(ptr, replaceString, length2);
519}
520
521char* stristr(char *theString, char *toSearch){
522
523	int length = strlen(theString);
524	char *copyString;
525	char *copySearch;
526	char *ret;
527	int i;
528
529	copyString = (char*)malloc(strlen(theString) + 1);
530	copySearch = (char*)malloc(strlen(toSearch) + 1);
531	strcpy(copyString, theString);
532	strcpy(copySearch, toSearch);
533
534	for(i = 0; i < length; i++)
535		copyString[i] = tolower(copyString[i]);
536
537	length = strlen(toSearch);
538
539	for(i = 0; i < length; i++)
540		copySearch[i] = tolower(copySearch[i]);
541
542	ret = strstr(copyString, copySearch);
543
544	free(copySearch);
545	free(copyString);
546
547	if(!ret)
548		return NULL;
549
550	return theString + (ret - copyString);
551}
552
553int RunGhostScript(char *cmd){
554
555	char command[MAX_PATH + 100];
556	int length = strlen(cmd);
557	char *cmdPtr, *commandPtr;
558
559	strcpy(command, "\"%GS_PATH%\\gs*\\bin\\gswin32.exe\" -dNODISPLAY ");
560	cmdPtr = cmd;
561	commandPtr = command + strlen(command);
562
563	while(*cmdPtr != '\0'){
564			
565		if(!((cmdPtr + 3) > (cmd + length))
566			&& (*cmdPtr == '=')
567			&& (*(cmdPtr + 1) == 'x')
568			&& (*(cmdPtr + 2) == '1')
569			&& (*(cmdPtr + 3) == '1')
570			&& (*(cmdPtr + 4) == ' ')){
571			memcpy(commandPtr, "#mswindll ", strlen("#mswindll "));
572			cmdPtr += strlen("x11 ");
573			commandPtr += strlen("#mswindll ");
574			continue;
575		}
576
577		*commandPtr = *cmdPtr;
578		commandPtr++;
579		cmdPtr++;
580	}
581	*commandPtr = '\0';
582
583	return system(ExpandEnvString(ExpandEnvString(command)));
584}
585
586int HandleTfmCompilation(char *command){
587
588	system(command);
589	return 1;
590}
591
592int HandlePkCompilation(char *command){
593
594	char *ptr;
595	char directory[MAX_PATH];
596	char dpi[10];
597	char name[MAX_PATH];
598	char oldFileName[MAX_PATH];
599	char newFileName[MAX_PATH];
600	int i, argCount, x;
601	char endChar;
602
603	endChar = ' ';
604	if(ptr = strstr(command, "--dest-dir ")){
605
606		i = 0;
607		ptr += strlen("--dest-dir ");
608
609		if(ptr[i] == '"'){
610			endChar = '"';
611			ptr++;
612		}
613
614		while(ptr[i] != endChar){
615			directory[i] = ptr[i];
616			i++;
617		}
618
619		directory[i] = '\0';
620	}
621
622	else if(ptr = strstr(command, "-D ")){
623		i = 0;
624		ptr += strlen("-D ");
625
626		while(ptr[i] != endChar){
627			directory[i] = ptr[i];
628			i++;
629		}
630
631		directory[i] = '\0';
632	}
633
634	else {
635		return system(command);
636	}
637
638	ptr = stristr(command, "makepk ");
639	ptr += strlen("makepk ");
640
641	i = 0;
642	argCount = 1;
643	while(true){
644		while(isspace(ptr[i])) i++;
645		if(ptr[i] == '-'){
646			while(!isspace(ptr[i])) i++;
647			while(isspace(ptr[i])) i++;
648			while(ptr[i] == endChar) i++;
649			while(ptr[i] != endChar) i++;
650			while(ptr[i] == endChar) i++;
651		}
652
653		else{
654			argCount++;
655			if(argCount == 2){
656				x = 0;
657				while(!isspace(ptr[i])) {
658					name[x] = ptr[i];
659					i++;
660					x++;
661				}
662
663				name[x] = '\0';
664			}
665
666			if(argCount == 3){
667				x = 0;
668				while(isalnum(ptr[i])) {
669					dpi[x] = ptr[i];
670					i++;
671					x++;
672				}
673
674				dpi[x] = '\0';
675				break;
676			}
677
678			while(!isspace(ptr[i])) i++;
679			while(isspace(ptr[i])) i++;
680		}
681	}
682
683	sprintf(oldFileName, "%s\\%s.pk", directory, name);
684	sprintf(newFileName, "%s\\%s.%spk", directory, name, dpi);
685
686	system(command);
687
688	if(!MoveFileEx(oldFileName, newFileName, 
689		MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED)){
690		DeleteFile(oldFileName);
691		return 0;
692	}
693
694	return 1;
695}
696
697int ShellOut(char *file, char *operation){
698
699	char *fileName;
700	SHELLEXECUTEINFO executeInfo;
701
702	memset(&executeInfo, 0, sizeof(SHELLEXECUTEINFO));
703	fileName = file;
704
705	int i = 0;
706
707//	while(isspace(fileName[i])) i++;
708//	while(!isspace(fileName[i]) && (fileName[i] != '\0')) i++;
709//	fileName[i] = '\0';
710
711	executeInfo.cbSize = sizeof(SHELLEXECUTEINFO);
712	executeInfo.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS;
713	executeInfo.hwnd = GetDesktopWindow();
714	executeInfo.lpVerb = operation;
715	executeInfo.lpFile = fileName;
716	executeInfo.nShow = SW_SHOWNORMAL;
717	//if((int)ShellExecute(GetDesktopWindow(), "print", fileName, NULL, NULL, SW_SHOWNORMAL) < 32){
718	
719	ShellExecuteEx(&executeInfo);
720
721	if((int)executeInfo.hInstApp < 32){
722		MessageBox(NULL, "Unable to print postscript file, please install ghostscript",
723					"Print Failure", MB_OK | MB_ICONERROR);
724		return 0;
725	}
726
727	WaitForProcess(executeInfo.hProcess);
728	return 1;
729}
730
731int WaitForProcess(HANDLE process){
732
733	DWORD status;
734
735	while(GetExitCodeProcess(process, &status) != 0){
736
737		if(status != STILL_ACTIVE){
738			return 1;
739		}
740
741		Sleep(1000);
742	}
743
744	return 0;
745}
746
747int HandleModifierMapRequest(char *commandLine){
748
749	return 1;
750}
751
752int select(int  n,fd_set *readfds,fd_set *writefds,
753		   fd_set *exceptfds, timeval *timeout){
754
755	Sleep(timeout->tv_sec * 100 + timeout->tv_usec / 100);
756	return 0;
757}
758
759//int execve(const char *filename,char *const argv [],
760//		   char *const envp[]);
761int pipe(int *fildes){
762	return 0;
763}
764
765int fork(){
766	return 0;
767}
768
769int close(int fildes){
770	
771	if(SOCKET_IsSocket(fildes))
772		return SOCKET_close(fildes);
773	else
774		return ERROR;
775}
776
777size_t read(int d, void *buf, size_t nbytes){
778	
779	if(SOCKET_IsSocket(d))
780		return SOCKET_read(d, buf, nbytes);
781
782	return ERROR;
783}
784
785int wait(int *status){
786	return 0;
787}
788
789size_t write(int d, const void *buf, size_t nbytes){
790	
791	if(SOCKET_IsSocket(d))
792		return SOCKET_write(d, buf, nbytes);
793
794	return ERROR;
795}
796
797int kill(pid_t pid, int sig){
798	return 0;
799}