taskkill: Implement basic command-line parsing.
[wine] / programs / taskkill / taskkill.c
1 /*
2  * Task termination utility
3  *
4  * Copyright 2008 Andrew Riedi
5  * Copyright 2010 Andrew Nguyen
6  *
7  * This library 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 library 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 library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <windows.h>
23 #include <wine/debug.h>
24 #include <wine/unicode.h>
25
26 #include "taskkill.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
29
30 int force_termination;
31
32 WCHAR **task_list;
33 unsigned int task_count;
34
35 static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
36 {
37     int wlen;
38     DWORD count, ret;
39     WCHAR msg_buffer[8192];
40
41     wlen = vsprintfW(msg_buffer, msg, va_args);
42
43     ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
44     if (!ret)
45     {
46         DWORD len;
47         char *msgA;
48
49         len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
50             NULL, 0, NULL, NULL);
51         msgA = HeapAlloc(GetProcessHeap(), 0, len);
52         if (!msgA)
53             return 0;
54
55         WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
56             NULL, NULL);
57         WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
58         HeapFree(GetProcessHeap(), 0, msgA);
59     }
60
61     return count;
62 }
63
64 static int taskkill_printfW(const WCHAR *msg, ...)
65 {
66     va_list va_args;
67     int len;
68
69     va_start(va_args, msg);
70     len = taskkill_vprintfW(msg, va_args);
71     va_end(va_args);
72
73     return len;
74 }
75
76 static int taskkill_message_printfW(int msg, ...)
77 {
78     va_list va_args;
79     WCHAR msg_buffer[8192];
80     int len;
81
82     LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
83         sizeof(msg_buffer)/sizeof(WCHAR));
84
85     va_start(va_args, msg);
86     len = taskkill_vprintfW(msg_buffer, va_args);
87     va_end(va_args);
88
89     return len;
90 }
91
92 static int taskkill_message(int msg)
93 {
94     static const WCHAR formatW[] = {'%','s',0};
95     WCHAR msg_buffer[8192];
96
97     LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
98         sizeof(msg_buffer)/sizeof(WCHAR));
99
100     return taskkill_printfW(formatW, msg_buffer);
101 }
102
103 static BOOL add_to_task_list(WCHAR *name)
104 {
105     static unsigned int list_size = 16;
106
107     if (!task_list)
108     {
109         task_list = HeapAlloc(GetProcessHeap(), 0,
110                                    list_size * sizeof(*task_list));
111         if (!task_list)
112             return FALSE;
113     }
114     else if (task_count == list_size)
115     {
116         void *realloc_list;
117
118         list_size *= 2;
119         realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
120                                    list_size * sizeof(*task_list));
121         if (!realloc_list)
122             return FALSE;
123
124         task_list = realloc_list;
125     }
126
127     task_list[task_count++] = name;
128     return TRUE;
129 }
130
131 /* FIXME Argument processing does not match behavior observed on Windows.
132  * Stringent argument counting and processing is performed, and unrecognized
133  * options are detected as parameters when placed after options that accept one. */
134 static BOOL process_arguments(int argc, WCHAR *argv[])
135 {
136     static const WCHAR slashForceTerminate[] = {'/','f',0};
137     static const WCHAR slashImage[] = {'/','i','m',0};
138     static const WCHAR slashPID[] = {'/','p','i','d',0};
139     static const WCHAR slashHelp[] = {'/','?',0};
140
141     if (argc > 1)
142     {
143         int i;
144         BOOL has_im = 0, has_pid = 0;
145
146         /* Only the lone help option is recognized. */
147         if (argc == 2 && !strcmpW(slashHelp, argv[1]))
148         {
149             taskkill_message(STRING_USAGE);
150             exit(0);
151         }
152
153         for (i = 1; i < argc; i++)
154         {
155             int got_im = 0, got_pid = 0;
156
157             if (!strcmpiW(slashForceTerminate, argv[i]))
158                 force_termination = 1;
159             /* Options /IM and /PID appear to behave identically, except for
160              * the fact that they cannot be specified at the same time. */
161             else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
162                      (got_pid = !strcmpiW(slashPID, argv[i])))
163             {
164                 if (!argv[i + 1])
165                 {
166                     taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
167                     taskkill_message(STRING_USAGE);
168                     return FALSE;
169                 }
170
171                 if (got_im) has_im = 1;
172                 if (got_pid) has_pid = 1;
173
174                 if (has_im && has_pid)
175                 {
176                     taskkill_message(STRING_MUTUAL_EXCLUSIVE);
177                     taskkill_message(STRING_USAGE);
178                     return FALSE;
179                 }
180
181                 if (!add_to_task_list(argv[i + 1]))
182                     return FALSE;
183                 i++;
184             }
185             else
186             {
187                 taskkill_message(STRING_INVALID_OPTION);
188                 taskkill_message(STRING_USAGE);
189                 return FALSE;
190             }
191         }
192     }
193     else
194     {
195         taskkill_message(STRING_MISSING_OPTION);
196         taskkill_message(STRING_USAGE);
197         return FALSE;
198     }
199
200     return TRUE;
201 }
202
203 int wmain(int argc, WCHAR *argv[])
204 {
205     if (!process_arguments(argc, argv))
206     {
207         HeapFree(GetProcessHeap(), 0, task_list);
208         return 1;
209     }
210
211     WINE_FIXME("taskkill.exe functionality is not implemented\n");
212
213     HeapFree(GetProcessHeap(), 0, task_list);
214     return 0;
215 }