1 // winmain.cpp : Defines the entry point for the application.
2 //
3 // $NHDT-Date: 1432512799 2015/05/25 00:13:19 $  $NHDT-Branch: master $:$NHDT-Revision: 1.5 $
4 
5 #include "winMS.h"
6 #include <string.h>
7 
8 #define MAX_CMDLINE_PARAM 255
9 
10 extern int FDECL(main, (int, char **));
11 static TCHAR *_get_cmd_arg(TCHAR *pCmdLine);
12 
13 int APIENTRY
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nCmdShow)14 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine,
15         int nCmdShow)
16 {
17     int argc;
18     char *argv[MAX_CMDLINE_PARAM];
19     size_t len;
20     TCHAR *p;
21     TCHAR wbuf[NHSTR_BUFSIZE];
22     char buf[NHSTR_BUFSIZE];
23 
24     /* get command line parameters */
25     p = _get_cmd_arg(
26 #if defined(WIN_CE_PS2xx) || defined(WIN32_PLATFORM_HPCPRO)
27         lpCmdLine
28 #else
29         GetCommandLine()
30 #endif
31         );
32     for (argc = 1; p && argc < MAX_CMDLINE_PARAM; argc++) {
33         len = _tcslen(p);
34         if (len > 0) {
35             argv[argc] = _strdup(NH_W2A(p, buf, BUFSZ));
36         } else {
37             argv[argc] = "";
38         }
39         p = _get_cmd_arg(NULL);
40     }
41     GetModuleFileName(NULL, wbuf, BUFSZ);
42     argv[0] = _strdup(NH_W2A(wbuf, buf, BUFSZ));
43 
44     main(argc, argv);
45 
46     return 0;
47 }
48 
49 TCHAR *
_get_cmd_arg(TCHAR * pCmdLine)50 _get_cmd_arg(TCHAR *pCmdLine)
51 {
52     static TCHAR *pArgs = NULL;
53     TCHAR *pRetArg;
54     BOOL bQuoted;
55 
56     if (!pCmdLine && !pArgs)
57         return NULL;
58     if (!pArgs)
59         pArgs = pCmdLine;
60 
61     /* skip whitespace */
62     for (pRetArg = pArgs; *pRetArg && _istspace(*pRetArg);
63          pRetArg = CharNext(pRetArg))
64         ;
65     if (!*pRetArg) {
66         pArgs = NULL;
67         return NULL;
68     }
69 
70     /* check for quote */
71     if (*pRetArg == TEXT('"')) {
72         bQuoted = TRUE;
73         pRetArg = CharNext(pRetArg);
74         pArgs = _tcschr(pRetArg, TEXT('"'));
75     } else {
76         /* skip to whitespace */
77         for (pArgs = pRetArg; *pArgs && !_istspace(*pArgs);
78              pArgs = CharNext(pArgs))
79             ;
80     }
81 
82     if (pArgs && *pArgs) {
83         TCHAR *p;
84         p = pArgs;
85         pArgs = CharNext(pArgs);
86         *p = (TCHAR) 0;
87     } else {
88         pArgs = NULL;
89     }
90 
91     return pRetArg;
92 }
93 
94 #ifndef STRNCMPI
lowc(c)95 char lowc(c) /* force 'c' into lowercase */
96 char c;
97 {
98     return ((char) (('A' <= c && c <= 'Z') ? (c | 040) : c));
99 }
100 
strncmpi(s1,s2,n)101 int strncmpi(s1, s2, n) /* case insensitive counted string comparison */
102 register const char *s1, *s2;
103 register int n; /*(should probably be size_t, which is usually unsigned)*/
104 {               /*{ aka strncasecmp }*/
105     register char t1, t2;
106 
107     while (n--) {
108         if (!*s2)
109             return (*s1 != 0); /* s1 >= s2 */
110         else if (!*s1)
111             return -1; /* s1  < s2 */
112         t1 = lowc(*s1++);
113         t2 = lowc(*s2++);
114         if (t1 != t2)
115             return (t1 > t2) ? 1 : -1;
116     }
117     return 0; /* s1 == s2 */
118 }
119 #endif /* STRNCMPI */
120