source: trunk/dll/systemf.c@ 330

Last change on this file since 330 was 330, checked in by root, 19 years ago

Use Runtime_Error

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