Fix subclassing to support nested messages.
[wine] / programs / winepath / winepath.c
1 /*
2  * Translate between Wine and Unix paths
3  *
4  * Copyright 2002 Mike Wetherell
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <windows.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define VERSION "0.1 (" PACKAGE_STRING ")"
28
29 enum {
30     SHORTFORMAT = 1,
31     LONGFORMAT  = 2,
32     UNIXFORMAT  = 4
33 };
34
35 static char *progname;
36
37 /* Wine specific functions */
38 extern BOOL process_init(char *argv[]);
39 typedef BOOL (WINAPI *wine_get_unix_file_name_t) ( LPCWSTR dos, LPSTR buffer, DWORD len );
40 /*
41  * handle an option
42  */
43 int option(int shortopt, char *longopt)
44 {
45     const char *helpmsg =
46     "Convert PATH(s) to Unix or Windows long or short paths.\n"
47     "\n"
48     "  -u, --unix    output Unix format\n"
49     "  -l, --long    output Windows long format\n"
50     "  -s, --short   output Windows short format \n"
51     "  -h, --help    output this help message and exit\n"
52     "  -v, --version output version information and exit\n"
53     "\n"
54     "The input paths can be in any format. If more than one option is given\n"
55     "then the input paths are output in all formats specified, in the order\n"
56     "Unix, long, short. If no option is given the default is Unix format.\n";
57
58     switch (shortopt) {
59         case 'h':
60             printf("Usage: %s [OPTION] [PATH]...\n", progname);
61             printf(helpmsg);
62             exit(0);
63         case 'v':
64             printf("%s version " VERSION "\n", progname);
65             exit(0);
66         case 'l':
67             return LONGFORMAT;
68         case 's':
69             return SHORTFORMAT;
70         case 'u':
71             return UNIXFORMAT;
72     }
73
74     fprintf(stderr, "%s: invalid option ", progname);
75     if (longopt)
76         fprintf(stderr, "'%s'\n", longopt);
77     else
78         fprintf(stderr, "'-%c'\n", shortopt);
79     fprintf(stderr, "Try '%s --help' for help\n", progname);
80     exit(2);
81 }
82
83 /*
84  * Parse command line options
85  */
86 int parse_options(char *argv[])
87 {
88     int outputformats = 0;
89     int done = 0;
90     char *longopts[] = { "long", "short", "unix", "help", "version", "" };
91     int i, j;
92
93     for (i = 1; argv[i] && !done; )
94     {
95         if (argv[i][0] != '-') {
96             /* not an option */
97             i++;
98             continue;
99         }
100
101         if (argv[i][1] == '-') {
102             if (argv[i][2] == 0) {
103                 /* '--' end of options */
104                 done = 1;
105             } else {
106                 /* long option */
107                 for (j = 0; longopts[j][0]; j++)
108                     if (strcmp(argv[i]+2, longopts[j]) == 0)
109                         break;
110                 outputformats |= option(longopts[j][0], argv[i]);
111             }
112         } else {
113             /* short options */
114             for (j = 1; argv[i][j]; j++)
115                 outputformats |= option(argv[i][j], NULL);
116         }
117
118         /* remove option */
119         for (j = i + 1; argv[j - 1]; j++)
120             argv[j - 1] = argv[j];
121     }
122
123     return outputformats;
124 }
125
126 /*
127  * Main function
128  */
129 int main(int argc, char *argv[])
130 {
131     wine_get_unix_file_name_t wine_get_unix_file_name_ptr = NULL;
132     static char path[MAX_PATH];
133     int outputformats;
134     int i;
135
136     progname = argv[0];
137     outputformats = parse_options(argv);
138     if (outputformats == 0)
139         outputformats = UNIXFORMAT;
140
141     if (outputformats & UNIXFORMAT) {
142         wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
143             GetProcAddress(GetModuleHandle("KERNEL32"),
144                            "wine_get_unix_file_name");
145         if (wine_get_unix_file_name_ptr == NULL) {
146             fprintf(stderr, "%s: cannot get the address of "
147                             "'wine_get_unix_file_name'\n", progname);
148             exit(3);
149         }
150     }
151
152     for (i = 1; argv[i]; i++)
153     {
154         *path='\0';
155         if (outputformats & LONGFORMAT) {
156             GetFullPathNameA(argv[i], sizeof(path), path, NULL);
157             printf("%s\n", path);
158         }
159         if (outputformats & SHORTFORMAT) {
160             GetShortPathNameA(argv[i], path, sizeof(path));
161             printf("%s\n", path);
162         }
163         if (outputformats & UNIXFORMAT) {
164             WCHAR dosW[MAX_PATH];
165
166             MultiByteToWideChar(CP_ACP, 0, argv[i], -1, dosW, MAX_PATH);
167             wine_get_unix_file_name_ptr(dosW, path, sizeof(path));
168             printf("%s\n", path);
169         }
170     }
171
172     exit(0);
173 }