source: trunk/dll/systemf.c@ 771

Last change on this file since 771 was 771, checked in by Gregg Young, 18 years ago

Increase subject to 1024 reduce DosSleep times

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.7 KB
RevLine 
[78]1
2/***********************************************************************
3
4 $Id: systemf.c 771 2007-08-06 03:05:35Z gyoung $
5
6 System Interfaces
7
8 Copyright (c) 1993-98 M. Kimes
[330]9 Copyright (c) 2003, 2006 Steven H.Levine
[78]10
[330]11 21 Nov 03 SHL Comments
12 31 Jul 04 SHL Indent -i2
13 01 Aug 04 SHL Rework lstrip/rstrip usage
14 17 Jul 06 SHL Use Runtime_Error
[373]15 26 Jul 06 SHL Use convert_nl_to_nul
[441]16 15 Aug 06 SHL More error popups
[519]17 01 Nov 06 SHL runemf2: temp fix for hung windows caused by termq errors
[540]18 03 Nov 06 SHL runemf2: rework termination queue logic to work for multiple threads
[552]19 07 Jan 07 GKY Move error strings etc. to string file
[78]20
21***********************************************************************/
22
[2]23#define INCL_WIN
24#define INCL_WINERRORS
25#define INCL_DOS
26#define INCL_DOSERRORS
[330]27#include <os2.h>
[2]28
29#include <stdlib.h>
30#include <stdio.h>
31#include <stdarg.h>
32#include <string.h>
33#include <ctype.h>
34#include <time.h>
[330]35
[2]36#include "fm3dll.h"
37#include "fm3dlg.h"
38#include "fm3str.h"
39
[330]40static PSZ pszSrcFile = __FILE__;
41
[632]42#pragma alloc_text(SYSTEMF,ShowSession,ExecOnList,runemf2)
[2]43
[105]44#define MAXSTRG (4096) /* used to build command line strings */
[2]45
[519]46//== ShowSession() bring session for foreground ==
[2]47
[105]48BOOL ShowSession(HWND hwnd, PID pid)
[78]49{
[105]50 HSWITCH hswitch;
51 SWCNTRL swctl;
52 ULONG rc;
[2]53
[519]54 hswitch = WinQuerySwitchHandle(pid ? (HWND)0 : hwnd, pid);
[441]55 if (hswitch) {
[105]56 rc = WinQuerySwitchEntry(hswitch, &swctl);
[441]57 if (!rc) {
[105]58 if (swctl.idProcess == pid && swctl.uchVisibility == SWL_VISIBLE)
59 rc = WinSwitchToProgram(hswitch);
60 if (!rc)
61 return TRUE;
[78]62 // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Failed: %lu/%lx",rc,rc);
[2]63
[105]64 }
65 }
66 return FALSE;
[2]67}
68
[519]69//== ExecOnList() Invoke runemf2 for command and file/directory list ==
70
[105]71int ExecOnList(HWND hwnd, char *command, int flags, char *tpath,
72 char **list, char *prompt)
[78]73{
[105]74 /* executes the command once for all files in list */
[2]75
[105]76 char path[CCHMAXPATH], commandline[2048], modpath[CCHMAXPATH], listfile[CCHMAXPATH],
77 *p, *pp, drive, *file, *ext, *dot;
[2]78 register int x;
[105]79 BOOL spaces;
[2]80
[441]81 if (!command || !*command) {
82 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
[105]83 return -1;
[441]84 }
[2]85 *listfile = 0;
[123]86 bstrip(command);
[2]87
88 *path = 0;
[105]89 if (tpath && *tpath)
90 strcpy(path, tpath);
[441]91 else if (*command != '<' || !strchr(command, '>')) {
[105]92 strcpy(path, command + (*command == '"'));
93 if (*command == '\"')
94 p = strchr(path, '\"');
[2]95 else
[105]96 p = strchr(path, ' ');
97 if (p)
[2]98 *p = 0;
[105]99 p = strrchr(path, '\\');
100 if (!p)
101 p = strrchr(path, ':');
[441]102 if (p) {
103 if (*p == ':') {
[105]104 p++;
105 *p = '\\';
106 p++;
[2]107 }
108 *p = 0;
109 }
110 else
111 *path = 0;
112 }
[441]113 if (!*path) {
[105]114 if (list && list[0])
115 strcpy(path, list[0]);
116 p = strrchr(path, '\\');
117 if (!p)
118 p = strrchr(path, ':');
[441]119 if (p) {
120 if (*p == ':') {
[105]121 p++;
122 *p = '\\';
123 p++;
[2]124 }
125 *p = 0;
126 }
127 else
128 *path = 0;
129 }
130 *modpath = 0;
[105]131 if (list && list[0])
132 strcpy(modpath, list[0]);
133 p = strrchr(modpath, '\\');
134 if (!p)
135 p = strrchr(modpath, ':');
[441]136 if (p) {
137 if (*p == ':') {
[2]138 p++;
139 *p = '\\';
140 p++;
141 }
142 *p = 0;
143 }
144 else
145 *modpath = 0;
[105]146 if (!*modpath)
147 strcpy(modpath, path);
148 if (*path)
[2]149 MakeFullName(path);
[105]150 if (*modpath)
[2]151 MakeFullName(modpath);
[105]152 if (IsFullName(path))
153 drive = toupper(*path);
154 else
155 drive = 0;
[2]156
[105]157 p = command; // substitue for special % sequences
[2]158
[105]159 pp = commandline;
160 *commandline = 0;
[441]161 while (*p) {
162 if (*p == '%') {
163 switch (*(p + 1)) {
[105]164 case '!': /* write list to file, add filename */
[441]165 if (list) {
166 if (!*listfile) {
[105]167 FILE *fp;
[2]168
[105]169 save_dir2(listfile);
170 if (listfile[strlen(listfile) - 1] != '\\')
171 strcat(listfile, "\\");
172 sprintf(&listfile[strlen(listfile)], "%s%03x",
[766]173 LISTTEMPROOT, (clock() & 4095));
[330]174 fp = xfopen(listfile, "w",pszSrcFile,__LINE__);
[441]175 if (fp) {
[105]176 for (x = 0; list[x]; x++)
177 {
178 fputs(list[x], fp);
179 if (list[x + 1])
180 fputc('\n', fp);
181 }
182 fclose(fp);
183 }
184 }
185 strcpy(pp, listfile);
186 pp += strlen(listfile);
187 }
188 p += 2;
189 break;
[2]190
[105]191 case 'c': /* add name of command processor */
192 {
193 char *env = GetCmdSpec(FALSE);
[2]194
[441]195 if (needs_quoting(env) && !strchr(env, '\"')) {
[105]196 *pp = '\"';
197 pp++;
198 spaces = TRUE;
199 }
200 else
201 spaces = FALSE;
202 strcpy(pp, env);
203 p += 2;
204 pp += strlen(env);
[441]205 if (spaces) {
[105]206 *pp = '\"';
207 pp++;
208 }
209 }
210 break;
[2]211
[105]212 case 't': /* add Target directory */
[441]213 if (needs_quoting(targetdir) && !strchr(targetdir, '\"')) {
[105]214 *pp = '\"';
215 pp++;
216 spaces = TRUE;
217 }
218 else
219 spaces = FALSE;
220 strcpy(pp, targetdir);
221 p += 2;
222 pp += strlen(targetdir);
[441]223 if (spaces) {
[105]224 *pp = '\"';
225 pp++;
226 }
227 break;
[2]228
[105]229 case '$': /* add drive letter */
230 if (drive)
231 *pp = drive;
[441]232 else {
[766]233 ULONG ulDriveNum = 3, ulDriveMap;
[2]234
[105]235 DosQCurDisk(&ulDriveNum, &ulDriveMap);
236 *pp = (char) (ulDriveNum + '@');
237 }
238 pp++;
239 p += 2;
240 break;
[2]241
[105]242 case 'U': /* add path of first list component */
243 case 'u':
[441]244 if (*modpath) {
245 if (needs_quoting(modpath) && !strchr(modpath, '\"')) {
[105]246 spaces = TRUE;
247 *pp = '\"';
248 pp++;
249 }
250 else
251 spaces = FALSE;
[441]252 if (*(p + 1) == 'u') {
[105]253 strcpy(pp, modpath);
254 pp += strlen(modpath);
255 }
[441]256 else {
[105]257 strcpy(pp, modpath + 2);
258 pp += strlen(modpath + 2);
259 }
[441]260 if (spaces) {
261 if (modpath[strlen(modpath) - 1] == '\\') {
[105]262 *pp = '\\';
263 pp++;
264 }
265 *pp = '\"';
266 pp++;
267 }
268 }
[441]269 else {
[105]270 char temp[CCHMAXPATH];
[2]271
[105]272 save_dir2(temp);
[441]273 if (needs_quoting(temp) && !strchr(temp, '\"')) {
[105]274 spaces = TRUE;
275 *pp = '\"';
276 pp++;
277 }
278 else
279 spaces = FALSE;
280 strcpy(pp, temp);
281 pp += strlen(temp);
[441]282 if (spaces) {
283 if (temp[strlen(temp) - 1] == '\\') {
[105]284 *pp = '\\';
285 pp++;
286 }
287 *pp = '\"';
288 pp++;
289 }
290 }
291 p += 2;
292 break;
[2]293
[105]294 case 'P': /* add path of execution */
295 case 'p':
[441]296 if (*path) {
297 if (needs_quoting(path) && !strchr(path, '\"')) {
[105]298 spaces = TRUE;
299 *pp = '\"';
300 pp++;
301 }
302 else
303 spaces = FALSE;
[441]304 if (*(p + 1) == 'p') {
[105]305 strcpy(pp, path);
306 pp += strlen(path);
307 }
[441]308 else {
[105]309 strcpy(pp, path + 2);
310 pp += strlen(path + 2);
311 }
[441]312 if (spaces) {
313 if (path[strlen(path) - 1] == '\\') {
[105]314 *pp = '\\';
315 pp++;
316 }
317 *pp = '\"';
318 pp++;
319 }
320 }
[441]321 else {
[105]322 char temp[CCHMAXPATH];
[2]323
[105]324 save_dir2(temp);
[441]325 if (needs_quoting(temp) && !strchr(temp, '\"')) {
[105]326 spaces = TRUE;
327 *pp = '\"';
328 pp++;
329 }
330 else
331 spaces = FALSE;
332 strcpy(pp, temp);
333 pp += strlen(temp);
[441]334 if (spaces) {
335 if (temp[strlen(temp) - 1] == '\\') {
[105]336 *pp = '\\';
337 pp++;
338 }
339 *pp = '\"';
340 pp++;
341 }
342 }
343 p += 2;
344 break;
[2]345
[105]346 case 'D':
[441]347 if (hwndMain) {
[105]348 PCNRITEM pci;
[2]349
[105]350 pci = (PCNRITEM) WinSendMsg(WinWindowFromID(WinWindowFromID(
351 hwndTree, FID_CLIENT), TREE_CNR),
352 CM_QUERYRECORDEMPHASIS,
353 MPFROMLONG(CMA_FIRST),
354 MPFROMSHORT(CRA_CURSORED));
[730]355 if (pci && (int) pci != -1 && *pci->pszFileName) {
356 if (needs_quoting(pci->pszFileName) &&
357 !strchr(pci->pszFileName, '\"'))
[105]358 {
359 *pp = '\"';
360 pp++;
361 spaces = TRUE;
362 }
363 else
364 spaces = FALSE;
[730]365 strcpy(pp, pci->pszFileName);
366 pp += strlen(pci->pszFileName);
[441]367 if (spaces) {
[105]368 *pp = '\"';
369 pp++;
370 }
371 }
372 }
373 p += 2;
374 break;
[2]375
[105]376 case 'd':
[441]377 if (hwndMain) {
[105]378 HENUM henum;
379 char retstr[CCHMAXPATH];
380 HWND hwndC, hwndDir;
381 USHORT id;
382 BOOL first = TRUE;
[2]383
[105]384 henum = WinBeginEnumWindows(hwndMain);
[441]385 while ((hwndC = WinGetNextWindow(henum)) != NULLHANDLE) {
386 if (hwndC != hwndTree) {
[105]387 id = WinQueryWindowUShort(hwndC, QWS_ID);
[441]388 if (id) {
[105]389 hwndDir = WinWindowFromID(hwndC, FID_CLIENT);
[441]390 if (hwndDir) {
[105]391 hwndDir = WinWindowFromID(hwndDir, DIR_CNR);
[441]392 if (hwndDir) {
[105]393 *retstr = 0;
394 WinSendMsg(hwndC, UM_CONTAINERDIR, MPFROMP(retstr), MPVOID);
[441]395 if (*retstr) {
396 if (!first) {
[105]397 *pp = ' ';
398 pp++;
399 }
400 first = FALSE;
[441]401 if (needs_quoting(retstr) && !strchr(retstr, '\"')) {
[105]402 *pp = '\"';
403 pp++;
404 spaces = TRUE;
405 }
406 else
407 spaces = FALSE;
408 strcpy(pp, retstr);
409 pp += strlen(retstr);
[441]410 if (spaces) {
[105]411 *pp = '\"';
412 pp++;
413 }
414 }
415 }
416 }
417 }
418 }
419 }
420 WinEndEnumWindows(henum);
421 }
422 p += 2;
423 break;
[2]424
[105]425 case '%':
426 *pp = '%';
427 pp++;
428 p += 2;
429 break;
[2]430
[105]431 case 'R':
432 case 'F':
433 case 'A':
434 case 'r':
435 case 'f':
436 case 'a':
437 case 'e':
[441]438 if (list) {
[105]439 for (x = 0; list[x]; x++)
440 {
441 file = strrchr(list[x], '\\');
442 if (!file)
443 file = strrchr(list[x], ':');
444 if (file)
445 file++;
446 else
447 file = list[x];
448 ext = strrchr(file, '.');
449 dot = ext;
450 if (ext)
451 ext++;
[441]452 switch (*(p + 1)) {
[105]453 case 'R':
454 case 'r':
455 if (pp + strlen(list[x]) > commandline + 1250)
456 goto BreakOut;
[441]457 if (*(p + 1) == 'r') {
[105]458 strcpy(pp, list[x]);
459 pp += strlen(list[x]);
460 }
[441]461 else {
[105]462 strcpy(pp, list[x] + 2);
463 pp += strlen(list[x] + 2);
464 }
465 break;
[2]466
[105]467 case 'F':
468 case 'f':
469 if (*(p + 1) == 'F' && dot)
470 *dot = 0;
471 if (pp + strlen(file) > commandline + 1250)
472 goto BreakOut;
[441]473 if (needs_quoting(file)) {
[105]474 spaces = TRUE;
475 *pp = '\"';
476 pp++;
477 }
478 else
479 spaces = FALSE;
480 strcpy(pp, file);
481 pp += strlen(file);
482 if (*(p + 1) == 'F' && dot)
483 *dot = '.';
[441]484 if (spaces) {
485 if (*(pp - 1) != '\"') {
[105]486 *pp = '\"';
487 pp++;
488 }
489 }
490 break;
[2]491
[105]492 case 'A':
493 case 'a':
494 if (pp + strlen(list[x]) > commandline + 1250)
495 goto BreakOut;
[441]496 if (needs_quoting(list[x]) && !strchr(list[x], '\"')) {
[105]497 spaces = TRUE;
498 *pp = '\"';
499 pp++;
500 }
501 else
502 spaces = FALSE;
[441]503 if (*(p + 1) == 'a') {
[105]504 strcpy(pp, list[x]);
505 pp += strlen(list[x]);
506 }
[441]507 else {
[105]508 strcpy(pp, list[x] + 2);
509 pp += strlen(list[x] + 2);
510 }
[441]511 if (spaces) {
512 if (list[x][strlen(list[x]) - 1] == '\\') {
[105]513 *pp = '\\';
514 pp++;
515 }
516 *pp = '\"';
517 pp++;
518 }
519 break;
[2]520
[105]521 case 'e':
[441]522 if (ext) {
[105]523 if (pp + strlen(ext) > commandline + 1250)
524 goto BreakOut;
[441]525 if (needs_quoting(ext)) {
[105]526 spaces = TRUE;
527 *pp = '\"';
528 pp++;
[2]529 }
[105]530 else
531 spaces = FALSE;
532 strcpy(pp, ext);
533 pp += strlen(ext);
[441]534 if (spaces) {
535 if (*(pp - 1) != '\"') {
[105]536 *pp = '\"';
537 pp++;
538 }
[2]539 }
[105]540 }
541 break;
542 }
[441]543 if (list[x + 1]) {
[105]544 *pp = ' ';
545 pp++;
546 }
547 }
[2]548 }
[105]549 p += 2;
550 break;
[2]551
[105]552 default:
553 *pp = *p;
554 p++;
555 pp++;
556 break;
557 }
558 }
[441]559 else {
[105]560 *pp = *p;
561 pp++;
562 p++;
563 }
564 *pp = 0;
565 }
566
[2]567BreakOut:
568
[105]569 {
570 EXECARGS ex;
571 ULONG size;
572 int ret;
[2]573
[105]574 memset(&ex, 0, sizeof(EXECARGS));
575 size = sizeof(ex.environment) - 1;
576 PrfQueryProfileData(fmprof, FM3Str, command, ex.environment, &size);
[441]577 if (flags & PROMPT) {
578 /* allow editing command line */
[105]579 ex.flags = (flags & (~PROMPT));
580 ex.commandline = commandline;
581 strcpy(ex.path, path);
582 if (prompt)
583 strcpy(ex.title, prompt);
584 ret = WinDlgBox(HWND_DESKTOP, hwnd, CmdLineDlgProc, FM3ModHandle,
585 EXEC_FRAME, &ex);
586 if (ret != 1)
587 return (ret == 0) ? -1 : -2;
588 }
589 else
590 ex.flags = flags;
591 ex.flags &= (~PROMPT);
592 return runemf2(ex.flags, hwnd, path,
593 (*ex.environment) ? ex.environment : NULL,
594 "%s", commandline);
595 }
[2]596}
597
[519]598//== runemf2() run requested app, return -1 if problem starting else return app rc ==
[441]599
[540]600int runemf2(int type, HWND hwnd, char *pszDirectory, char *pszEnvironment,
[105]601 char *formatstring,...)
[78]602{
[105]603 /* example:
604
[2]605 * status = runemf2(SEPARATE | WINDOWED,
606 * hwnd,
607 * NullStr,
608 * NULL,
609 * "%s /C %s",
610 * getenv("COMSPEC"),
611 * batchfilename);
[105]612 *
613 * use (HWND)0 for hwnd if window handle not handy.
614 */
[2]615
616 /*
617 * type bitmapped flag -- see FM3DLL.H
618 */
619
[105]620 va_list parguments;
621 int ret = -1;
[540]622 RESULTCODES results;
[519]623 STARTDATA sdata;
[105]624 REQUESTDATA rq;
[562]625 ULONG ulSessID;
626 ULONG ulLength;
627 UINT ctr;
628 ULONG ulAppType;
[105]629 PID sessPID;
630 BOOL wasquote;
[540]631 char *pszPgm, *pszArgs = NULL;
632 char szObject[32] = "", *p, szSavedir[CCHMAXPATH];
633 BOOL useTermQ = FALSE;
634 char szTempdir[CCHMAXPATH];
635
[519]636 typedef struct {
637 USHORT usSessID;
638 USHORT usRC;
639 } TERMINFO;
640
[540]641 TERMINFO *pTermInfo;
[105]642 BYTE bPriority;
643 APIRET rc;
[519]644 PIB *ppib;
645 TIB *ptib;
[2]646
[540]647 // Shared by all threads
648# define TERMQ_BASE_NAME "\\QUEUES\\FM3WAIT"
649 static char szTermQName[30];
650 static HQUEUE hTermQ;
651 static HEV hTermQSem;
652
653 if (pszDirectory && *pszDirectory) {
654 if (!DosQueryPathInfo(pszDirectory,
[105]655 FIL_QUERYFULLNAME,
[540]656 szTempdir,
657 sizeof(szTempdir)))
658 pszDirectory = szTempdir;
[105]659 }
[2]660
[105]661 if (!hwnd)
662 hwnd = HWND_DESKTOP;
[2]663
[540]664 rc = DosAllocMem((PVOID)&pszPgm,
[105]665 MAXSTRG,
666 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[330]667 if (rc) {
668 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
[105]669 return -1;
[330]670 }
[2]671
[540]672 *szSavedir = 0;
[2]673
[540]674 *pszPgm = 0;
[105]675 va_start(parguments,
676 formatstring);
[540]677 vsprintf(pszPgm,
[105]678 formatstring,
679 parguments);
680 va_end(parguments);
[2]681
[540]682 if (pszEnvironment) {
683 p = &pszEnvironment[strlen(pszEnvironment)] + 1;
[105]684 *p = 0;
[540]685 p = pszEnvironment;
[373]686 while ((p = convert_nl_to_nul(p)) != NULL)
687 ; // loop
[105]688 }
[2]689
[540]690 if (!*pszPgm) {
[105]691 p = GetCmdSpec(FALSE);
[540]692 strcpy(pszPgm, p);
693 if (!*pszPgm) {
[441]694 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
[105]695 return -1;
[441]696 }
[105]697 }
[2]698
[540]699 if (*pszPgm) {
700 if (*pszPgm == '<' && strchr(pszPgm, '>')) {
[441]701 /* is a workplace object */
[105]702 HOBJECT hWPSObject;
703 char temp;
[2]704
[540]705 p = strchr(pszPgm, '>');
[105]706 p++;
707 temp = *p;
[330]708 if (temp) {
[540]709 rc = DosAllocMem((PVOID)&pszArgs,
[105]710 MAXSTRG * 2,
711 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[330]712 if (rc)
[540]713 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
[330]714 }
[105]715 else
[540]716 pszArgs = NULL;
[105]717 *p = 0;
718 /* Find the handle of the WPS object */
[540]719 hWPSObject = WinQueryObject(pszPgm);
[105]720 *p = temp;
[441]721 if (hWPSObject != NULLHANDLE) {
[540]722 if (pszArgs && *p) {
723 sprintf(pszArgs,"OPEN=DEFAULT;PARAMETERS=\"%s\"",p);
724 WinSetObjectData(hWPSObject,pszArgs);
[105]725 }
726 else
[441]727 WinSetObjectData(hWPSObject,"OPEN=DEFAULT");
[105]728 ret = 0;
729 }
730 goto ObjectInterrupt;
731 }
[2]732
[105]733 if ((type & 15) == SYNCHRONOUS ||
734 (type & 15) == ASYNCHRONOUS ||
735 (type & 15) == DETACHED)
736 {
[540]737 strip_lead_char(" \t", pszPgm);
738 p = pszPgm;
[105]739 wasquote = FALSE;
740 while (*p &&
741 (wasquote ||
742 (*p != ' ' &&
743 *p != '\t')))
744 {
[441]745 if (*p == '\"') {
746 if (!wasquote) {
[105]747 wasquote = TRUE;
748 memmove(p,
749 p + 1,
750 strlen(p));
751 while (*p == ' ' ||
752 *p == '\t')
753 p++;
754 }
[441]755 else {
[105]756 memmove(p,
757 p + 1,
758 strlen(p));
759 break;
760 }
761 }
762 else
763 p++;
764 }
[441]765 if (*p) {
[105]766 *p = 0;
767 p++;
768 }
769 else
[540]770 p = pszPgm;
[105]771 p[strlen(p) + 1] = 0; /* double-terminate args */
[540]772 if (*pszPgm) {
773 if (!strchr(pszPgm, '\\') &&
774 !strchr(pszPgm, ':') &&
775 pszDirectory &&
776 *pszDirectory)
[105]777 {
[540]778 save_dir2(szSavedir);
779 switch_to(pszDirectory);
[105]780 }
[562]781 rc = DosQueryAppType(pszPgm,&ulAppType);
[540]782 if (!strchr(pszPgm, '\\') &&
783 !strchr(pszPgm, ':') &&
784 pszDirectory &&
785 *pszDirectory)
786 switch_to(szSavedir);
[441]787 if (rc) {
[562]788 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,
789 GetPString(IDS_DOSQAPPTYPEFAILEDTEXT),
790 pszPgm);
[540]791 DosFreeMem(pszPgm);
792 if (pszArgs)
793 DosFreeMem(pszArgs);
[105]794 return -1;
795 }
[562]796 if (ulAppType) {
797 if (ulAppType & FAPPTYP_DLL || ulAppType & FAPPTYP_VIRTDRV ||
798 ulAppType & FAPPTYP_PHYSDRV || ulAppType & FAPPTYP_PROTDLL)
[105]799 {
[562]800 Runtime_Error(pszSrcFile, __LINE__,
801 GetPString(IDS_APPTYPEUNEXPECTEDTEXT),
802 ulAppType, pszPgm);
[540]803 if (pszPgm)
804 DosFreeMem(pszPgm);
805 if (pszArgs)
806 DosFreeMem(pszArgs);
[105]807 return -1;
808 }
[562]809 if (ulAppType & FAPPTYP_DOS || ulAppType & FAPPTYP_WINDOWSREAL ||
810 ulAppType & FAPPTYP_WINDOWSPROT || ulAppType & FAPPTYP_WINDOWSPROT31)
[105]811 {
[562]812 Runtime_Error(pszSrcFile, __LINE__,
813 GetPString(IDS_APPTYPEUNEXPECTEDTEXT),
814 ulAppType, pszPgm);
[540]815 if (pszPgm)
816 DosFreeMem(pszPgm);
817 if (pszArgs)
818 DosFreeMem(pszArgs);
[105]819 return -1;
820 }
821 }
[540]822 memset(&results, 0, sizeof(results));
823 if (pszDirectory && *pszDirectory) {
824 save_dir2(szSavedir);
825 switch_to(pszDirectory);
[105]826 }
[540]827 ret = DosExecPgm(szObject, sizeof(szObject),
828 ((type & 15) == ASYNCHRONOUS ? EXEC_ASYNC : 0) +
829 ((type & 15) == DETACHED ? EXEC_BACKGROUND : 0),
830 pszPgm, pszEnvironment, &results, pszPgm);
831 if (pszDirectory && *pszDirectory)
832 switch_to(szSavedir);
[330]833 if (ret) {
834 Dos_Error(MB_ENTER,ret,hwnd,pszSrcFile,__LINE__,
[540]835 GetPString(IDS_DOSEXECPGMFAILEDTEXT), pszPgm);
[330]836 }
[105]837 }
838 }
[441]839 else {
[519]840 if (~type & FULLSCREEN)
[105]841 type |= WINDOWED;
[540]842 rc = DosAllocMem((PVOID) & pszArgs, MAXSTRG * 2,
[105]843 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[441]844 if (rc) {
[540]845 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
846 DosFreeMem(pszPgm);
[105]847 return -1;
848 }
[540]849 *pszArgs = 0;
850 memset(&sdata, 0, sizeof(sdata));
851 strip_lead_char(" \t", pszPgm);
852 p = pszPgm;
[105]853 wasquote = FALSE;
[441]854 while (*p && (wasquote || (*p != ' ' && *p != '\t'))) {
855 if (*p == '\"') {
856 if (!wasquote) {
[105]857 wasquote = TRUE;
858 memmove(p, p + 1, strlen(p));
859 while (*p == ' ' || *p == '\t')
860 p++;
861 }
[441]862 else {
[105]863 memmove(p, p + 1, strlen(p));
864 break;
865 }
866 }
867 else
868 p++;
[540]869 } // while
[441]870 if (*p) {
[105]871 *p = 0;
872 p++;
873 }
874 else
875 p = NullStr;
876 if (*p)
[540]877 strcpy(pszArgs, p);
[2]878
[540]879 p = strrchr(pszPgm, '.');
[441]880 if (p) {
[105]881 char temp[CCHMAXPATH + 1];
[2]882
[441]883 if (!stricmp(p, ".BAT")) {
[540]884 strcpy(temp, pszPgm);
885 strcpy(pszPgm, pszArgs);
886 strcpy(pszArgs, "/C ");
887 strcat(pszArgs, temp);
888 strcat(pszArgs, " ");
889 strcat(pszArgs, pszPgm);
890 strcpy(pszPgm, GetCmdSpec(TRUE)); // DOS
[105]891 }
[540]892 else if (!stricmp(p, ".CMD") || !stricmp(p, ".BTM")) {
893 // Assume 4OS2 is BTM
894 strcpy(temp, pszPgm);
895 strcpy(pszPgm, pszArgs);
896 strcpy(pszArgs, "/C ");
897 strcat(pszArgs, temp);
898 strcat(pszArgs, " ");
899 strcat(pszArgs, pszPgm);
900 strcpy(pszPgm, GetCmdSpec(FALSE)); // OS/2
[105]901 }
902 }
[2]903
[105]904 /* goddamned OS/2 limit */
[2]905
[540]906 if (strlen(pszPgm) + strlen(pszArgs) > 1024)
907 pszArgs[1024 - strlen(pszPgm)] = 0;
[2]908
[540]909 if (!strchr(pszPgm, '\\') &&
910 !strchr(pszPgm, ':') &&
911 pszDirectory &&
912 *pszDirectory)
[105]913 {
[540]914 save_dir2(szSavedir);
915 switch_to(pszDirectory);
[105]916 }
[562]917 rc = DosQueryAppType(pszPgm,&ulAppType);
[540]918 if (!strchr(pszPgm, '\\') &&
919 !strchr(pszPgm, ':') &&
920 pszDirectory &&
921 *pszDirectory)
922 switch_to(szSavedir);
[441]923 if (rc) {
[562]924 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,
925 GetPString(IDS_DOSQAPPTYPEFAILEDTEXT),
926 pszPgm);
[540]927 DosFreeMem(pszPgm);
928 if (pszArgs)
929 DosFreeMem(pszArgs);
[105]930 return -1;
931 }
[2]932
[562]933 if (ulAppType) {
934 if (ulAppType & (FAPPTYP_DLL | FAPPTYP_VIRTDRV | FAPPTYP_PHYSDRV | FAPPTYP_PROTDLL))
[105]935 {
[562]936 Runtime_Error(pszSrcFile, __LINE__,
937 GetPString(IDS_APPTYPEUNEXPECTEDTEXT),
938 pszPgm);
[540]939 DosFreeMem(pszPgm);
940 if (pszArgs)
941 DosFreeMem(pszArgs);
[105]942 return -1;
943 }
[562]944 ulAppType &= ~FAPPTYP_BOUND;
945 if (ulAppType & (FAPPTYP_DOS | FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT | FAPPTYP_WINDOWSPROT31))
[105]946 {
[562]947 if (ulAppType & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT | FAPPTYP_WINDOWSPROT31))
[105]948 {
[540]949 if (~type & FULLSCREEN &&
[562]950 ulAppType & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT | FAPPTYP_WINDOWSPROT31))
[105]951 {
[540]952 ret = RunSeamless(pszPgm, pszArgs, hwnd);
953 if (pszPgm)
954 DosFreeMem(pszPgm);
955 if (pszArgs)
956 DosFreeMem(pszArgs);
[441]957 return ret ? 0 : -1;
[105]958 }
[441]959 else {
[540]960 strcat(pszPgm, " ");
961 strcat(pszPgm, pszArgs);
962 *pszArgs = 0;
[562]963 if (ulAppType & (FAPPTYP_WINDOWSPROT | FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT31))
[540]964 strcat(pszArgs, "/3 ");
965 strcat(pszArgs, pszPgm);
966 strcpy(pszPgm, "WINOS2.COM");
[105]967 }
968 }
[441]969 else {
[540]970 if (~type & FULLSCREEN) {
[105]971 type |= WINDOWED;
[562]972 ulAppType = SSF_TYPE_WINDOWEDVDM;
[105]973 }
[441]974 else {
[540]975 type &= ~WINDOWED;
[562]976 ulAppType = SSF_TYPE_VDM;
[105]977 }
978 }
979 }
[562]980 else if (ulAppType & FAPPTYP_32BIT) {
981 ulAppType &= ~FAPPTYP_32BIT;
982 if (ulAppType == FAPPTYP_WINDOWAPI)
983 ulAppType = SSF_TYPE_PM;
984 else if (ulAppType == FAPPTYP_WINDOWCOMPAT)
985 ulAppType = SSF_TYPE_WINDOWABLEVIO;
986 else if (ulAppType == FAPPTYP_NOTWINDOWCOMPAT) {
987 ulAppType = SSF_TYPE_FULLSCREEN;
[540]988 type &= ~WINDOWED;
[105]989 type |= FULLSCREEN;
990 }
991 else /* ? */
[562]992 ulAppType = SSF_TYPE_WINDOWABLEVIO;
[105]993 }
[562]994 else if (ulAppType == FAPPTYP_WINDOWAPI)
995 ulAppType = SSF_TYPE_PM;
996 else if (ulAppType == FAPPTYP_WINDOWCOMPAT)
997 ulAppType = SSF_TYPE_WINDOWABLEVIO;
998 else if (ulAppType == FAPPTYP_NOTWINDOWCOMPAT) {
[540]999 type &= ~WINDOWED;
[562]1000 ulAppType = SSF_TYPE_FULLSCREEN;
[105]1001 }
1002 else
[562]1003 ulAppType = SSF_TYPE_DEFAULT;
[540]1004 if ((type & FULLSCREEN || ~type & WINDOWED) &&
[562]1005 ulAppType == SSF_TYPE_WINDOWABLEVIO)
[105]1006 {
[562]1007 ulAppType = SSF_TYPE_FULLSCREEN;
[105]1008 }
[519]1009 // fixme parens?
[540]1010 else if (type & FULLSCREEN ||
[562]1011 (type & WINDOWED && ulAppType == SSF_TYPE_WINDOWEDVDM))
1012 {
1013 ulAppType = SSF_TYPE_VDM;
1014 }
[105]1015 }
[562]1016 if (ulAppType == SSF_TYPE_WINDOWEDVDM && type & SEPARATEKEEP) {
[519]1017 type &= ~SEPARATEKEEP;
[105]1018 type |= SEPARATE;
1019 }
[2]1020
[519]1021 DosGetInfoBlocks(&ptib, &ppib);
1022
[540]1023 if (~type & WAIT)
1024 useTermQ = FALSE;
[519]1025 else {
[540]1026 rc = 0;
1027 DosEnterCritSec();
1028 if (!hTermQ) {
1029 // Create term queue and event semaphore just once
1030 sprintf(szTermQName, TERMQ_BASE_NAME "_%x", ppib->pib_ulpid);
1031 rc = DosCreateQueue(&hTermQ, QUE_FIFO | QUE_CONVERT_ADDRESS, szTermQName);
1032 if (rc) {
1033 hTermQ = (HQUEUE)0; // Try to survive
1034 DosExitCritSec();
1035 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,"DosCreateQueue");
1036 }
1037 else {
1038 rc = DosCreateEventSem(NULL,(PHEV)&hTermQSem,0,FALSE);
1039 if (rc) {
1040 hTermQSem = (HEV)0; // Try to survive
1041 DosCloseQueue(hTermQ);
1042 hTermQ = (HQUEUE)0; // Try to survive
1043 DosExitCritSec();
1044 Dos_Error(MB_ENTER,rc,HWND_DESKTOP,pszSrcFile,__LINE__,"DoCreateEventSem");
1045 }
[562]1046 // if (!rc) fprintf(stderr,"%s %d qcreated ptib %x hTermQ %x\n",__FILE__, __LINE__,ptib,hTermQ);
[540]1047 }
1048 } // if 1st time
1049 useTermQ = hTermQ && hTermQSem;
1050 if (!rc)
1051 DosExitCritSec();
1052 } // if wait
[562]1053
1054 memset(&sdata,0,sizeof(sdata));
[519]1055 sdata.Length = sizeof(sdata);
[562]1056 sdata.Related = type & (WAIT | CHILD) ? SSF_RELATED_CHILD :
1057 SSF_RELATED_INDEPENDENT;
[519]1058 sdata.FgBg = type & BACKGROUND ? SSF_FGBG_BACK : SSF_FGBG_FORE;
1059 sdata.TraceOpt = SSF_TRACEOPT_NONE;
[540]1060 sdata.PgmName = pszPgm;
[562]1061 if (*pszArgs)
1062 sdata.PgmInputs = pszArgs;
1063 if (useTermQ)
1064 sdata.TermQ = szTermQName;
[540]1065 sdata.Environment = pszEnvironment;
[519]1066 sdata.InheritOpt = SSF_INHERTOPT_PARENT;
[562]1067 sdata.SessionType = ulAppType;
[540]1068 sdata.ObjectBuffer = szObject;
1069 sdata.ObjectBuffLen = sizeof(szObject);
[562]1070 if ((type & 15) == SEPARATEKEEP)
1071 sdata.PgmControl |= SSF_CONTROL_NOAUTOCLOSE;
1072 if (type & MAXIMIZED)
1073 sdata.PgmControl |= SSF_CONTROL_MAXIMIZE;
1074 if (type & MINIMIZED)
1075 sdata.PgmControl |= SSF_CONTROL_MINIMIZE;
1076 if (type & INVISIBLE)
1077 sdata.PgmControl |= SSF_CONTROL_INVISIBLE;
1078
[540]1079 if (pszDirectory && *pszDirectory) {
1080 save_dir2(szSavedir);
1081 switch_to(pszDirectory);
[105]1082 }
[562]1083
[563]1084 // printf("%s %d DosStartsession thread 0x%x data\n ",
[562]1085 // __FILE__, __LINE__,ptib->tib_ordinal); fflush(stdout); // 10 Mar 07 SHL hang
[563]1086 // printf(" %d %d %d %s %s %s %d %d\n %s %x %x\n",
[562]1087 // sdata.Length , sdata.Related, sdata.FgBg, sdata.PgmName,
[563]1088 // sdata.PgmInputs, sdata.TermQ, sdata.InheritOpt,
1089 // sdata.SessionType, szTermQName,
1090 // hTermQ, hTermQSem); fflush(stdout);
[519]1091 ret = DosStartSession(&sdata, &ulSessID, &sessPID);
[562]1092
[563]1093 // if (type & WAIT) {
1094 // printf("%s %d DosStartession thread 0x%x rc = %d sess = %u pid = 0x%x\n",
1095 // __FILE__, __LINE__, ptib->tib_ordinal,ret, ulSessID, sessPID); fflush(stdout); // 10 Mar 07 SHL hang
1096 // }
1097 // else {
1098 // printf("%s %d DosStartession thread 0x%x nowait rc = %d\n",
1099 // __FILE__, __LINE__, ptib->tib_ordinal,ret); fflush(stdout); // 10 Mar 07 SHL hang
1100 // }
1101
[540]1102 if (pszDirectory && *pszDirectory)
1103 switch_to(szSavedir);
[562]1104
[330]1105 if (ret && ret != ERROR_SMG_START_IN_BACKGROUND) {
1106 Dos_Error(MB_CANCEL,ret,hwnd,pszSrcFile,__LINE__,
[540]1107 GetPString(IDS_DOSSTARTSESSIONFAILEDTEXT),pszPgm,pszArgs);
[330]1108 }
[441]1109 else if (type & WAIT) {
[105]1110 if (!(type & (BACKGROUND | MINIMIZED | INVISIBLE)))
1111 ShowSession(hwnd, sessPID);
[2]1112
[540]1113 if (!useTermQ) {
[562]1114 STATUSDATA sd;
[540]1115 // Could not create queue - fallback - fixme to be gone?
[563]1116 // printf("%s %d waiting wo/termq\n", __FILE__, __LINE__); fflush(stdout); // 12 Mar 07 SHL hang
[2]1117
[105]1118 memset(&sd, 0, sizeof(sd));
1119 sd.Length = (USHORT) sizeof(sd);
1120 sd.SelectInd = SET_SESSION_UNCHANGED;
1121 sd.BondInd = SET_SESSION_UNCHANGED;
1122 for (ctr = 0;; ctr++)
1123 {
[771]1124 DosSleep(100);//05 Aug 07 GKY 200
[519]1125 if (DosSetSession(ulSessID, &sd)) // Check if session gone (i.e. finished)
[105]1126 break;
[519]1127 if (ctr > 10) {
[563]1128 // printf("%s %d thread 0x%x showing slow sess %u pid 0x%x\n",
1129 // __FILE__, __LINE__,ptib->tib_ordinal,ulSessID,sessPID); fflush(stdout); // 12 Mar 07 SHL
[519]1130 ShowSession(hwnd, sessPID); // Show every 2 seconds
1131 ctr = 0;
1132 }
[105]1133 }
1134 }
[441]1135 else {
[105]1136 for (ctr = 0;; ctr++)
1137 {
[540]1138 if (ctr < 20) {
1139 rc = DosReadQueue(hTermQ, &rq, &ulLength, (PPVOID)&pTermInfo, 0,
1140 DCWW_NOWAIT, &bPriority, hTermQSem);
1141 if (rc == ERROR_QUE_EMPTY) {
[771]1142 DosSleep(50);//05 Aug 07 GKY 100
[540]1143 continue;
[105]1144 }
1145 }
[441]1146 else {
[562]1147 if (ctr == 20) {
[563]1148 // printf("%s %d thread 0x%x showing slow sess %u pid 0x%x\n",
[562]1149 // __FILE__, __LINE__,ptib->tib_ordinal,ulSessID,sessPID); fflush(stdout);
[540]1150 ShowSession(hwnd, sessPID); // Show long running session
[562]1151 }
[540]1152 rc = DosReadQueue(hTermQ, &rq, &ulLength, (PPVOID)&pTermInfo, 0,
1153 DCWW_WAIT, &bPriority, 0);
1154 }
1155
1156 if (rc) {
1157 // Oh heck
1158 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,"DosReadQueue");
[771]1159 DosSleep(100);//05 Aug 07 GKY 500
[540]1160 continue;
1161 }
1162
[563]1163 // printf("%s %d DosReadQueue thread 0x%x sess %u sessRC %u rq.pid 0x%x rq.data 0x%x\n",
[562]1164 // __FILE__, __LINE__,ptib->tib_ordinal,pTermInfo->usSessID,pTermInfo->usRC,rq.pid, rq.ulData); fflush(stdout);
1165
[540]1166 if (pTermInfo->usSessID == ulSessID)
1167 break; // Our session is done
1168
[562]1169 // Requeue session for other thread
[540]1170 {
1171 static ULONG ulLastSessID;
[563]1172 // printf("%s %d requeue thread 0x%x our sess %u term sess %u term rc %u\n",
[562]1173 // __FILE__, __LINE__,ptib->tib_ordinal,ulSessID,pTermInfo->usSessID,pTermInfo->usRC); fflush(stdout);
1174 // fixme to be gone when no longer needed for debug?
[540]1175 if (ulLastSessID) {
[771]1176 DosSleep(100);//05 Aug 07 GKY 500
[540]1177 ulLastSessID = pTermInfo->usSessID;
[519]1178 }
[562]1179 // requeue term report for other thread and do not free yet
[540]1180 rc = DosWriteQueue(hTermQ, rq.ulData, ulLength,(PVOID)pTermInfo, bPriority);
1181 if (rc)
1182 Dos_Error(MB_CANCEL,rc,hwnd,pszSrcFile,__LINE__,"DosWriteQueue");
[771]1183 DosSleep(50); //05 Aug 07 GKY 100 // Let other thread see queue entry
[105]1184 }
[519]1185 } // for
[540]1186
[562]1187 ret = pTermInfo->usRC == 0; // Set 1 if rc 0 else 0
[563]1188 // printf("%s %d thread 0x%x term for sess %u\n",
1189 // __FILE__, __LINE__,ptib->tib_ordinal,ulSessID);fflush(stdout);
[540]1190 DosFreeMem(pTermInfo);
[105]1191 }
[540]1192 } // if wait
[105]1193 else if (!(type & (BACKGROUND | MINIMIZED | INVISIBLE)))
1194 ShowSession(hwnd, sessPID);
1195 }
1196 }
[2]1197
1198ObjectInterrupt:
1199
[540]1200 if (pszPgm)
1201 DosFreeMem(pszPgm);
1202 if (pszArgs)
1203 DosFreeMem(pszArgs);
[562]1204
[105]1205 return ret;
[2]1206}
1207
[519]1208//== Exec() Start application with WinStartApp ==
1209
[105]1210HAPP Exec(HWND hwndNotify, BOOL child, char *startdir, char *env,
[519]1211 PROGTYPE *progt, ULONG fl, char *formatstring,...)
[78]1212{
[105]1213 PROGDETAILS pgd;
[2]1214 register char *p;
[105]1215 char *parameters = NULL, *executable = NULL;
[519]1216 HAPP happ = (HAPP)0;
[105]1217 ULONG ulOptions = SAF_INSTALLEDCMDLINE;
1218 BOOL wasquote;
1219 va_list parguments;
[2]1220
[105]1221 if (child)
[2]1222 ulOptions |= SAF_STARTCHILDAPP;
1223
[330]1224 executable = xmallocz(MAXSTRG,pszSrcFile,__LINE__);
1225 if (executable) {
[105]1226 va_start(parguments, formatstring);
1227 vsprintf(executable, formatstring, parguments);
[2]1228 va_end(parguments);
[105]1229 strip_lead_char(" \t", executable);
[441]1230 if (*executable) {
[330]1231 parameters = xmalloc(MAXSTRG,pszSrcFile,__LINE__);
[441]1232 if (parameters) {
[105]1233 p = executable;
1234 wasquote = FALSE;
[441]1235 while (*p && (wasquote || (*p != ' ' && *p != '\t'))) {
1236 if (*p == '\"') {
1237 if (!wasquote) {
[105]1238 wasquote = TRUE;
1239 memmove(p, p + 1, strlen(p));
1240 while (*p == ' ' || *p == '\t')
1241 p++;
1242 }
[441]1243 else {
[105]1244 memmove(p, p + 1, strlen(p));
1245 break;
1246 }
1247 }
1248 else
1249 p++;
1250 }
[441]1251 if (*p) {
[105]1252 *p = 0;
1253 p++;
1254 }
1255 else
1256 p = NullStr;
1257 if (*p)
1258 strcpy(parameters, p);
[2]1259
[441]1260 if (p && (!stricmp(p, ".BAT") || !stricmp(p, ".CMD"))) {
[105]1261 char *temp;
[2]1262
[330]1263 temp = xmalloc(CCHMAXPATH * 2,pszSrcFile,__LINE__);
[441]1264 if (temp) {
1265 if (!stricmp(p, ".BAT")) {
[105]1266 strcpy(temp, executable);
1267 strcpy(executable, parameters);
1268 strcpy(parameters, "/C ");
1269 strcat(parameters, temp);
1270 strcat(parameters, " ");
1271 strcat(parameters, executable);
1272 strcpy(executable, GetCmdSpec(TRUE));
1273 }
[441]1274 else if (!stricmp(p, ".CMD")) {
[105]1275 strcpy(temp, executable);
1276 strcpy(executable, parameters);
1277 strcpy(parameters, "/C ");
1278 strcat(parameters, temp);
1279 strcat(parameters, " ");
1280 strcat(parameters, executable);
1281 strcpy(executable, GetCmdSpec(FALSE));
1282 }
1283 free(temp);
1284 }
1285 }
[2]1286
[105]1287 memset(&pgd, 0, sizeof(pgd));
1288 pgd.Length = sizeof(pgd);
1289 pgd.progt = *progt;
1290 pgd.swpInitial.fl = fl;
1291 pgd.pszEnvironment = env;
1292 pgd.pszStartupDir = startdir;
[562]1293 pgd.pszParameters = *parameters ? parameters : NULL;
[105]1294 pgd.pszExecutable = executable;
1295 pgd.swpInitial.hwndInsertBehind = HWND_TOP;
1296 happ = WinStartApp(hwndNotify, &pgd, NULL, NULL, ulOptions);
1297 free(parameters);
[2]1298 }
1299 }
1300 free(executable);
1301 }
1302 return happ;
1303}
Note: See TracBrowser for help on using the repository browser.