Dan Kegel | ba02ae4 | 2003-01-21 20:14:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Start a program using ShellExecuteEx, optionally wait for it to finish |
| 3 | * Compatible with Microsoft's "c:\windows\command\start.exe" |
| 4 | * |
| 5 | * Copyright 2003 Dan Kegel |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <windows.h> |
| 23 | #include <winuser.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <shlobj.h> |
| 27 | |
| 28 | #include "resources.h" |
| 29 | |
| 30 | /** |
| 31 | Output given message to stdout without formatting. |
| 32 | */ |
| 33 | static void output(const char *message) |
| 34 | { |
| 35 | DWORD count; |
| 36 | WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), message, strlen(message), &count, NULL); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | Output given message, |
| 41 | followed by ": ", |
| 42 | followed by description of given GetLastError() value to stdout, |
| 43 | followed by a trailing newline, |
| 44 | then terminate. |
| 45 | */ |
| 46 | static void fatal_error(const char *msg, DWORD error_code) |
| 47 | { |
| 48 | LPVOID lpMsgBuf; |
| 49 | int status; |
| 50 | |
| 51 | output(msg); |
| 52 | output(": "); |
| 53 | status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, (LPTSTR) & lpMsgBuf, 0, NULL); |
| 54 | if (!status) { |
| 55 | output("FormatMessage failed\n"); |
| 56 | } else { |
| 57 | output(lpMsgBuf); |
| 58 | LocalFree((HLOCAL) lpMsgBuf); |
| 59 | output("\n"); |
| 60 | } |
| 61 | ExitProcess(1); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | Output given message from string table, |
| 66 | followed by ": ", |
| 67 | followed by description of given GetLastError() value to stdout, |
| 68 | followed by a trailing newline, |
| 69 | then terminate. |
| 70 | */ |
| 71 | static void fatal_string_error(int which, DWORD error_code) |
| 72 | { |
| 73 | char msg[2048]; |
| 74 | |
| 75 | if (!LoadString(GetModuleHandle(NULL), which, |
| 76 | msg, sizeof(msg))) |
| 77 | fatal_error("LoadString failed", GetLastError()); |
| 78 | |
| 79 | fatal_error(msg, error_code); |
| 80 | } |
| 81 | |
| 82 | static void fatal_string(int which) |
| 83 | { |
| 84 | char msg[2048]; |
| 85 | |
| 86 | if (!LoadString(GetModuleHandle(NULL), which, |
| 87 | msg, sizeof(msg))) |
| 88 | fatal_error("LoadString failed", GetLastError()); |
| 89 | |
| 90 | output(msg); |
| 91 | ExitProcess(1); |
| 92 | } |
| 93 | |
| 94 | static void usage() |
| 95 | { |
| 96 | fatal_string(STRING_USAGE); |
| 97 | } |
| 98 | |
| 99 | static void license() |
| 100 | { |
| 101 | fatal_string(STRING_LICENSE); |
| 102 | } |
| 103 | |
| 104 | int main(int argc, char *argv[]) |
| 105 | { |
| 106 | char arguments[MAX_PATH]; |
| 107 | char *p; |
| 108 | SHELLEXECUTEINFO sei; |
| 109 | int argi; |
| 110 | |
| 111 | memset(&sei, 0, sizeof(sei)); |
| 112 | sei.cbSize = sizeof(sei); |
| 113 | sei.lpVerb = "open"; |
| 114 | sei.nShow = SW_SHOWNORMAL; |
| 115 | /* Dunno what these mean, but it looks like winMe's start uses them */ |
| 116 | sei.fMask = SEE_MASK_FLAG_DDEWAIT|SEE_MASK_FLAG_NO_UI; |
| 117 | |
| 118 | /* Canonical Microsoft commandline flag processing: |
| 119 | * flags start with /, are case insensitive, |
| 120 | * and may be run together in same word. |
| 121 | */ |
| 122 | for (argi=1; argi<argc; argi++) { |
| 123 | int ci; |
| 124 | |
| 125 | if (argv[argi][0] != '/') |
| 126 | break; |
| 127 | |
| 128 | /* Handle all options in this word */ |
| 129 | for (ci=0; argv[argi][ci]; ) { |
| 130 | /* Skip slash */ |
| 131 | ci++; |
| 132 | switch(argv[argi][ci]) { |
| 133 | case 'l': |
| 134 | case 'L': |
| 135 | license(); |
| 136 | break; /* notreached */ |
| 137 | case 'm': |
| 138 | case 'M': |
| 139 | if (argv[argi][ci+1] == 'a' || argv[argi][ci+1] == 'A') |
| 140 | sei.nShow = SW_SHOWMAXIMIZED; |
| 141 | else |
| 142 | sei.nShow = SW_SHOWMINIMIZED; |
| 143 | break; |
| 144 | case 'r': |
| 145 | case 'R': |
| 146 | /* sei.nShow = SW_SHOWNORMAL; */ |
| 147 | break; |
| 148 | case 'w': |
| 149 | case 'W': |
| 150 | sei.fMask |= SEE_MASK_NOCLOSEPROCESS; |
| 151 | break; |
| 152 | default: |
| 153 | printf("Option '%s' not recognized\n", argv[argi]+ci-1); |
| 154 | usage(); |
| 155 | } |
| 156 | /* Skip to next slash */ |
| 157 | while (argv[argi][ci] && (argv[argi][ci] != '/')) |
| 158 | ci++; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (argi == argc) |
| 163 | usage(); |
| 164 | |
| 165 | sei.lpFile = argv[argi++]; |
| 166 | |
| 167 | /* FIXME - prone to overflow */ |
| 168 | arguments[0] = 0; |
| 169 | for (p = arguments; argi < argc; argi++) |
| 170 | p += sprintf(p, " %s", argv[argi]); |
| 171 | |
| 172 | sei.lpParameters = arguments; |
| 173 | |
| 174 | if (!ShellExecuteEx(&sei)) |
| 175 | fatal_string_error(STRING_EXECFAIL, GetLastError()); |
| 176 | |
| 177 | if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) { |
| 178 | DWORD exitcode; |
| 179 | DWORD waitcode; |
| 180 | waitcode = WaitForSingleObject(sei.hProcess, INFINITE); |
| 181 | if (waitcode) |
| 182 | fatal_error("WaitForSingleObject", GetLastError()); |
| 183 | if (!GetExitCodeProcess(sei.hProcess, &exitcode)) |
| 184 | fatal_error("GetExitCodeProcess", GetLastError()); |
| 185 | /* fixme: haven't tested whether exit code works properly */ |
| 186 | ExitProcess(exitcode); |
| 187 | } |
| 188 | |
| 189 | ExitProcess(0); |
| 190 | } |