summaryrefslogtreecommitdiffstats
path: root/private/utils/rdisk/rdisk.c
blob: bfb3a3ecd1a54dc6a634ae5f3c281b2d4c92e666 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*++

Module Name:

    repair.c

Abstract:

    This module contains the functions that create the main window
    of the Repair Utility.

    The syntax of this utility is:

        repair [-s]

        -s: Operate in the silent mode. The apllication will not have a client
            area, and some dialogs that allow the user to confirm some
            operations are not displayed.
            When the utility is operating in the silent mode, it will display
            the name of Setup program in the title bar of all error and
            warning dialogs displayed. This name to be displayed in the
            title bar is defined on resource.rc under IDS_SETUP.

            If this switch is not specified this utility will be run on
            its normal mode, and it will wait for the user to start
            the process of saving repair information.

Author:

    Jaime Sasson - 24-Jan-1994

Environment:

    ULIB, Windows

--*/


#include "precomp.h"
#pragma hdrstop

//
// Global Variables
//

HWND    _hWndMain                = NULL;
HANDLE  _hModule                 = NULL;
PWSTR   _pszApplicationClass     = (PWSTR)( L"Repair" );
BOOLEAN _SilentMode              = FALSE;
WCHAR   _szApplicationName[128];
INT     _ReturnCode;
BOOLEAN _AutoSkipRepairDisk      = FALSE;
BOOLEAN _AutoDoRepairDisk        = FALSE;


BOOLEAN
SaveCurrentConfiguration(
    );

BOOLEAN
CopyFilesToRepairDisk(
    IN  WCHAR   Drive
    );


BOOLEAN
FormatRepairDisk(
    IN PWCHAR Drive
    );

BOOLEAN
CreateRepairDisk(
    IN BOOLEAN DisplayConfirmCreateDisk
    );


HCURSOR
DisplayHourGlass(
    )

/*++

Routine Description:

    Changes the cursor currently displayed to the hour glass.

Arguments:

    None.

Return Value:

    HCURSOR - Handle to the cursor currently displayed.

--*/

{
    HCURSOR hCursor;

    hCursor = SetCursor( LoadCursor( NULL, IDC_WAIT ) );
    ShowCursor( TRUE );
    return( hCursor );

}



VOID
RestoreCursor(
    IN HCURSOR  Cursor
    )

/*++

Routine Description:

    Replace the currently selected cursor with the one whose handle was
    received as parameter.

Arguments:

    Cursor - Handle to the new cursor to be displayed.

Return Value:

    None.

--*/

{
    SetCursor( Cursor );
    ShowCursor( FALSE );
}


PWSTR
QueryFormattedString(
    UINT  MessageResId,
    ...
    )
/*++

Routine Description:

    Retrieve, format a string.


Arguments:

    MessageResId - Id of the message to be displayed in the message box.

    Optional list of strings to be inserted in the string to be retrieved.

Return Value:

    PWSTR  - Retrurns a formatted string. The caller of this function must
             free the buffer returned, using LocalFree().

--*/


{
    WCHAR   message[1024];
    PWSTR   FormattedString;
    va_list arglist;


    va_start(arglist, MessageResId);

    LoadString(_hModule,MessageResId,message,sizeof(message)/sizeof(WCHAR));
    FormatMessage( FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                   message,
                   0,
                   0,
                   (PVOID)&FormattedString,
                   1024,
                   &arglist );


    va_end(arglist);

    return(FormattedString);
}



UINT
DisplayMsgBox(
    HWND  hwnd,
    UINT  MessageResId,
    UINT  MsgBoxFlags,
    ...
    )
/*++

Routine Description:

    Retrieve, format and display a message in a message box.


Arguments:

    hWnd - Handle of the windows that is displaying the message box.

    MessageResId - Id of the message to be displayed in the message box.

    MsgBoxFlags - Flags that define the style of the message box.

    Optional list of strings to be inserted in the text to be displayed in
    the message box.

Return Value:

    UINT - Returns one of the following, depending on what the user chose:

           IDABORT, IDCANCEL, IDIGNORE, IDNO, IDOK, IDRETRY, IDYES

--*/


{
    WCHAR message[1024];
    WCHAR msgbody[1024];
    va_list arglist;

    va_start(arglist, MsgBoxFlags);

    LoadString(_hModule,MessageResId,message,sizeof(message)/sizeof(WCHAR));

    FormatMessage( FORMAT_MESSAGE_FROM_STRING,
                   message,
                   0,
                   0,
                   msgbody,
                   sizeof( msgbody ) / sizeof( WCHAR ),
                   &arglist );


    va_end(arglist);

    return(MessageBox(hwnd,msgbody,_szApplicationName,MsgBoxFlags));
}



long
MainWndProc(
    IN HWND     hWnd,
    IN UINT     message,
    IN WPARAM   wParam,
    IN LPARAM   lParam
    )

/*++

Routine Description:

    MainWndProc is the WndProc for application's main window. It is primarily
    responsible for creating the MDICLIENT window and servicing menu
    commands.

Arguments:

    Standard Windows' Proc parameters.

Return Value:

    long    - Returns 0 if the message was handled.

--*/

{

    static HWND LastFocus = NULL;

    switch( message ) {

    case WM_CREATE:
        {
            int     ScreenHeight;
            int     ScreenWidth;

            //
            // Display the main window in the center of the screen.
            //

            ScreenWidth  = GetSystemMetrics( SM_CXSCREEN );
            ScreenHeight = GetSystemMetrics( SM_CYSCREEN );

            SetWindowPos( hWnd,
                          NULL,
                          ( ScreenWidth  - (( LPCREATESTRUCT ) lParam )->cx ) / 2,
                          ( ScreenHeight - (( LPCREATESTRUCT ) lParam )->cy ) / 2,
                          0,
                          0,
                          SWP_NOSIZE | SWP_NOREDRAW | SWP_NOZORDER
                          );
            return 0;
        }

    case WM_INITDIALOG:
        {
            if( !_SilentMode ) {

                LastFocus = GetDlgItem( hWnd, IDB_SAVE_CURRENT_INFO );
                SetFocus( LastFocus );
                SetHelpContext( IDH_SAVE_REPAIR_INFO );

            }
            return 0;
        }

    case WM_KILLFOCUS:

        LastFocus = GetFocus();
        break;

    case WM_SETFOCUS:

        SetFocus( LastFocus );
        break;

    case WM_CLOSE:
        //
        //  Save application settings
        //
#if 0
        if( !_SilentMode && _SaveSettingsOnExit ) {
            SaveApplicationSettings();
        }
#endif
        return DefWindowProc( hWnd, message, wParam, lParam );

    case WM_DESTROY:

        if( !_SilentMode ) {
            QuitHelp();
        }
        PostQuitMessage( 0 );
        break;

    case WM_INITMENUPOPUP:

//        InitializeMenuPopup( wParam, lParam );
        break;

    case WM_MENUSELECT:

        if( !_SilentMode ) {
            SetMenuItemHelpContext( wParam, lParam );
        }
        break;

    case WM_COMMAND:

        switch( LOWORD( wParam )) {

        case IDB_SAVE_CURRENT_INFO:
            {
                HCURSOR Cursor;
                WCHAR   Drive;
                INT     SaveHelpContext;

                SaveHelpContext = GetHelpContext();
                SetHelpContext( IDH_REPLACE_PREVIOUS );

                Drive = ( WCHAR )'A';
                Cursor = DisplayHourGlass();
                if( SaveCurrentConfiguration() ) {

                    SetHelpContext( IDH_CREATE_REPAIR );
                    if( CreateRepairDisk( TRUE ) ) {
                        CopyFilesToRepairDisk(Drive);
                    }
                }
                SetHelpContext( SaveHelpContext );
                SetFocus( GetDlgItem( hWnd, IDB_SAVE_CURRENT_INFO ) );
                RestoreCursor( Cursor );
                break;
            }

        case IDB_CREATE_DISK:
            {
                HCURSOR Cursor;
                WCHAR   Drive;
                INT     SaveHelpContext;

                SaveHelpContext = GetHelpContext();
                SetHelpContext( IDH_CREATE_REPAIR );

                Drive = ( WCHAR )'A';
                Cursor = DisplayHourGlass();

                if( CreateRepairDisk( FALSE ) ) {
                    CopyFilesToRepairDisk(Drive);
                }
                SetHelpContext( SaveHelpContext );
                SetFocus( GetDlgItem( hWnd, IDB_CREATE_DISK ) );
                RestoreCursor( Cursor );
                break;
            }

         case IDB_EXIT:
            {
                if( !_SilentMode ) {
                    SendMessage( hWnd, WM_CLOSE, 0, 0 );
                }
                break;
            }

         case IDB_HELP:
            {
                if( !_SilentMode ) {
                    DisplayHelpIndex();
                    SetFocus( GetDlgItem( hWnd, IDB_HELP ) );
                }
                break;
            }

         default:

                return DefWindowProc( hWnd,  message, wParam, lParam );
        }
        break;

    case AP_AUTOMATIC_MODE:
    {
        WCHAR   Drive;
        HCURSOR Cursor;
        Drive = ( WCHAR )'A';
        Cursor = DisplayHourGlass();

        if(SaveCurrentConfiguration()
        && (    _AutoSkipRepairDisk
             || (CreateRepairDisk((BOOLEAN)(!_AutoDoRepairDisk)) && CopyFilesToRepairDisk(Drive))))
        {
            _ReturnCode = 1;
        } else {
            _ReturnCode = 0;
        }
        RestoreCursor( Cursor );
        PostMessage( hWnd, WM_CLOSE, 0, 0L );
        break;
    }
    case AP_HELP_KEY:

        if( !_SilentMode ) {
            DisplayHelp();
        }
        break;

    default:

        return DefWindowProc( hWnd, message, wParam, lParam );
    }

    return 0L;
}

BOOLEAN
InitializeApp(
    )

/*++

Routine Description:

    Initialize the application by registering the main window class, the child
    window class, and by creating the application's main window.

Arguments:

    None.

Return Value:


    BOOLEAN - Returns TRUE if the application was successfully initialized.
              Returns FALSE otherwise.

--*/

{
    WNDCLASS        wc;
    ULONG           StringId;

    //
    // Register application's main window class.
    //
    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = ( WNDPROC )MainWndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = DLGWINDOWEXTRA;
    wc.hInstance        = ( HINSTANCE )_hModule;
    wc.hIcon            = LoadIcon( ( HINSTANCE )_hModule, MAKEINTRESOURCE( IDI_REPAIR_UTILITY ));
    wc.hCursor          = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground    = ( HBRUSH )( COLOR_BTNFACE + 1 );
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = _pszApplicationClass;

    if( ! RegisterClass( &wc )) {
        return FALSE;
    }

    InitCommonControls();

    //
    //  Get the application name
    //

    StringId = ( _SilentMode )? IDS_SETUP : IDS_APPLICATION_NAME;
    LoadString( _hModule,
                StringId,
                _szApplicationName,
                sizeof(_szApplicationName)/sizeof(WCHAR));

    //
    // Create the main window.
    //

    _hWndMain = CreateDialog( _hModule,
                              MAKEINTRESOURCE( IDD_REPAIR ),
                              NULL,
                              MainWndProc );

    if( _hWndMain == NULL ) {
        GetLastError();
        return FALSE;
    }

    UpdateWindow( _hWndMain );
    if( !_SilentMode ) {
        ShowWindow( _hWndMain, SW_SHOWNORMAL );
    } else {
        ShowWindow( _hWndMain, SW_HIDE );
        PostMessage( _hWndMain, AP_AUTOMATIC_MODE, 0, 0 );
    }
    return TRUE;
}


INT _CRTAPI1
main(
    IN INT      argc,
    IN PCHAR    argv[ ]
    )

/*++

Routine Description:

    Entry point to the application.

Arguments:

    Standard C program arguments.

Return Value:


--*/

{
    MSG         msg;
    HANDLE      hAccel;

    UNREFERENCED_PARAMETER( argc );
    UNREFERENCED_PARAMETER( argv );

    _SilentMode = FALSE;

    if( argc > 1 ) {
        PSTR   p;

        p = argv[ 1 ];

        if( ( _strnicmp( p, ( LPSTR )"-s", 2 ) == 0 ) ||
            ( _strnicmp( p, ( LPSTR )"/s", 2 ) == 0 ) ) {

            _SilentMode = TRUE;

            if( p[2] == '-' ) {
                _AutoSkipRepairDisk = TRUE;
            } else {
                if( p[2] == '+' ) {
                    _AutoDoRepairDisk = TRUE;
                }
            }
        }
    }

    _hModule = GetModuleHandle( NULL );

    //
    // Register window classes and create the main window
    //
    if( ! InitializeApp( )) {
        return 0;
    }

    hAccel = LoadAccelerators( ( HINSTANCE )_hModule, MAKEINTRESOURCE( IDA_REPAIR_UTILITY ));
    if( !_SilentMode ) {
        InitializeHelp( (LPWSTR)L"rdisk.hlp" );
    }

    while( GetMessage( &msg, NULL, 0, 0 )) {

        if( ! TranslateAccelerator( _hWndMain, ( HACCEL )hAccel, &msg ) &&
            ! IsDialogMessage( _hWndMain, &msg )) {

            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
    if( !_SilentMode ) {
        CleanUpHelp();
    }
    return _ReturnCode;
}