Implemented old Win 2.x string functions.
[wine] / dlls / kernel / kernel_main.c
1 /*
2  * Kernel initialization code
3  */
4
5 #include <assert.h>
6 #include <ctype.h>
7
8 #include "winbase.h"
9 #include "wine/winbase16.h"
10
11 #include "neexe.h"
12 #include "module.h"
13 #include "task.h"
14 #include "selectors.h"
15 #include "miscemu.h"
16 #include "global.h"
17
18 extern void CODEPAGE_Init(void);
19 extern BOOL THUNK_Init(void);
20 extern void COMM_Init(void);
21
22
23 /***********************************************************************
24  *           KERNEL process initialisation routine
25  */
26 static BOOL process_attach(void)
27 {
28     HMODULE16 hModule;
29     STARTUPINFOA startup_info;
30     UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
31
32     /* Setup codepage info */
33     CODEPAGE_Init();
34
35     /* Initialize thunking */
36     if (!THUNK_Init()) return FALSE;
37
38     /* Initialize DOS memory */
39     if (!DOSMEM_Init(0)) return FALSE;
40
41     if ((hModule = LoadLibrary16( "krnl386.exe" )) < 32) return FALSE;
42
43     /* Initialize special KERNEL entry points */
44
45     /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
46     NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
47
48     /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
49     NE_SetEntryPoint( hModule, 454, __get_cs() );
50     NE_SetEntryPoint( hModule, 455, __get_ds() );
51
52     /* Initialize KERNEL.THHOOK */
53     TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN((SEGPTR)NE_GetEntryPoint( hModule, 332 )));
54
55     /* Initialize the real-mode selector entry points */
56 #define SET_ENTRY_POINT( num, addr ) \
57     NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
58                       DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
59                       FALSE, FALSE, FALSE ))
60
61     SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
62     SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
63     SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
64     SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
65     SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
66     SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
67     NE_SetEntryPoint( hModule, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
68     NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
69     NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
70     NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
71 #undef SET_ENTRY_POINT
72
73     /* Force loading of some dlls */
74     if (LoadLibrary16( "system" ) < 32) return FALSE;
75     if (LoadLibrary16( "wprocs" ) < 32) return FALSE;
76
77     /* Initialize communications */
78     COMM_Init();
79
80     /* Read DOS config.sys */
81     if (!DOSCONF_ReadConfig()) return FALSE;
82
83     /* Create 16-bit task */
84     GetStartupInfoA( &startup_info );
85     if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
86     if (!TASK_Create( (NE_MODULE *)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
87                       cmdShow, NtCurrentTeb(), NULL, 0 ))
88         return FALSE;
89
90     return TRUE;
91 }
92
93 /***********************************************************************
94  *           KERNEL initialisation routine
95  */
96 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
97 {
98     switch(reason)
99     {
100     case DLL_PROCESS_ATTACH:
101         return process_attach();
102     case DLL_PROCESS_DETACH:
103         WriteOutProfiles16();
104         break;
105     }
106     return TRUE;
107 }
108
109 /***********************************************************************
110  *              KERNEL_nop
111  *
112  * Entry point for kernel functions that do nothing.
113  */
114 LONG WINAPI KERNEL_nop(void) { return 0; }
115
116
117 /***************************************************************************
118  *
119  * Win 2.x string functions now moved to USER
120  *
121  * We rather want to implement them here instead of doing Callouts
122  */
123 SEGPTR WINAPI KERNEL_AnsiNext16(SEGPTR current)
124 {
125     return (*(char *)PTR_SEG_TO_LIN(current)) ? current + 1 : current;
126 }
127         
128 SEGPTR WINAPI KERNEL_AnsiPrev16( SEGPTR start, SEGPTR current )
129 {
130     return (current==start)?start:current-1;
131 }
132
133 SEGPTR WINAPI KERNEL_AnsiUpper16( SEGPTR strOrChar )
134 {
135     /* uppercase only one char if strOrChar < 0x10000 */
136     if (HIWORD(strOrChar))
137     {
138         char *s = PTR_SEG_TO_LIN(strOrChar);
139         while (*s) {
140             *s = toupper(*s);
141             s++;
142         }
143         return strOrChar;
144     }
145     else return toupper((char)strOrChar);
146 }
147
148 SEGPTR WINAPI KERNEL_AnsiLower16( SEGPTR strOrChar )
149 {
150     /* lowercase only one char if strOrChar < 0x10000 */
151     if (HIWORD(strOrChar))
152     {
153         char *s = PTR_SEG_TO_LIN(strOrChar);
154         while (*s) {
155             *s = tolower(*s);
156             s++;
157         }
158         return strOrChar;
159     }
160     else return tolower((char)strOrChar);
161 }