summaryrefslogtreecommitdiffstats
path: root/private/utils/print/print.cxx
blob: 6e5a089cb2ec530bb6a2faacd627bc67cb192c10 (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
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

        print.cxx

Abstract:


Author:

        Jaime F. Sasson - jaimes - 14-Jun-1991

Environment:

        ULIB, User Mode

--*/

#include "ulib.hxx"
#include "arg.hxx"
#include "path.hxx"
#include "wstring.hxx"
#include "system.hxx"
#include "array.hxx"
#include "arrayit.hxx"
#include "smsg.hxx"
#include "stream.hxx"
#include "rtmsg.h"
#include "prtstrm.hxx"
#include "file.hxx"
#include "print.hxx"

extern "C" {
#include <stdio.h>
#include <string.h>
}

PSTREAM Get_Standard_Input_Stream();
PSTREAM Get_Standard_Output_Stream();

DEFINE_CONSTRUCTOR( PRINT, PROGRAM );


BOOLEAN
PRINT::Initialize(
        )

/*++

Routine Description:

        Initializes a PRINT class.

Arguments:

        None.

Return Value:

        BOOLEAN - Indicates if the initialization succeeded.


--*/


{
        ARGUMENT_LEXEMIZER      ArgLex;
        ARRAY                           LexArray;

        ARRAY                           ArgumentArray;
        FLAG_ARGUMENT           FlagDisplayHelp;

        STRING_ARGUMENT         ProgramNameArgument;
        PWSTRING                        InvalidArgument;

        PATH_ARGUMENT           DeviceArgument;
        PPATH                           DevicePath;
        PCWSTRING                       DeviceString;
        PPATH                           FilePath;
        PCWSTRING                       FileString;
        PARRAY                          PathArray;
        PARRAY_ITERATOR         PathArrayIterator;
        PFSN_FILE                       FsnFile;


        _StandardOutput = Get_Standard_Output_Stream();

        //
        // Initialize MESSAGE class
        //
        _Message.Initialize( _StandardOutput, Get_Standard_Input_Stream() );

        //
        //      Parse command line
        //
        if ( !LexArray.Initialize() ) {
                DebugAbort( "LexArray.Initialize() failed \n" );
                return( FALSE );
    }
        if ( !ArgLex.Initialize( &LexArray ) ) {
                DebugAbort( "ArgLex.Initialize() failed \n" );
                return( FALSE );
    }
        ArgLex.PutSwitches( "/" );
        ArgLex.SetCaseSensitive( FALSE );
        ArgLex.PutStartQuotes( "\"" );
        ArgLex.PutEndQuotes( "\"" );

        if( !ArgLex.PrepareToParse() ) {
                DebugAbort( "ArgLex.PrepareToParse() failed \n" );
                return( FALSE );
        }
        if ( !ArgumentArray.Initialize() ) {
                DebugAbort( "ArgumentArray.Initialize() failed \n" );
                return( FALSE );
        }
        if( !ProgramNameArgument.Initialize("*") ||
                !DeviceArgument.Initialize( "/D:*" ) ||
                !_BufferSize.Initialize( "/B:*" ) ||
                !_Ticks1.Initialize( "/U:*" ) ||
                !_Ticks2.Initialize( "/M:*" ) ||
                !_Ticks3.Initialize( "/S:*" ) ||
                !_NumberOfFiles.Initialize( "/Q:*" ) ||
                !_FlagRemoveFiles.Initialize( "/T" ) ||
                !_Files.Initialize( "*", FALSE, TRUE ) ||
                !_FlagCancelPrinting.Initialize( "/C" ) ||
                !_FlagAddFiles.Initialize( "/P" ) ||
                !FlagDisplayHelp.Initialize( "/?" ) ) {
                DebugAbort( "Unable to initialize flag or string arguments \n" );
                return( FALSE );
        }
        if( !ArgumentArray.Put( &ProgramNameArgument ) ||
                !ArgumentArray.Put( &DeviceArgument ) ||
                !ArgumentArray.Put( &_BufferSize ) ||
                !ArgumentArray.Put( &_Ticks1 ) ||
                !ArgumentArray.Put( &_Ticks2 ) ||
                !ArgumentArray.Put( &_Ticks3 ) ||
                !ArgumentArray.Put( &_NumberOfFiles ) ||
                !ArgumentArray.Put( &_FlagRemoveFiles ) ||
                !ArgumentArray.Put( &_Files ) ||
                !ArgumentArray.Put( &_FlagCancelPrinting ) ||
                !ArgumentArray.Put( &_FlagAddFiles ) ||
                !ArgumentArray.Put( &FlagDisplayHelp ) ) {
                DebugAbort( "ArgumentArray.Put() failed \n" );
                return( FALSE );
        }
        if( !ArgLex.DoParsing( &ArgumentArray ) ) {
                InvalidArgument = ArgLex.QueryInvalidArgument();
                DebugPtrAssert( InvalidArgument );
                _Message.Set( MSG_PRINT_INVALID_SWITCH );
                _Message.Display( "%W", InvalidArgument );
                return( FALSE );
        }

        //
        //      /B: /U: /M: /S: /Q: /T: /C: and /P: are not implemented
        //      if one of these arguments was found in the command line
        //      then inform user, and don't print anything
        //
        if( _BufferSize.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/B:" );
                return( FALSE );
        }
        if( _Ticks1.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/U:" );
                return( FALSE );
        }
        if( _Ticks2.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/M:" );
                return( FALSE );
        }
        if( _Ticks3.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/S:" );
                return( FALSE );
        }
        if( _NumberOfFiles.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/Q:" );
                return( FALSE );
        }
        if( _FlagRemoveFiles.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/T:" );
                return( FALSE );
        }
        if( _FlagCancelPrinting.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/C:" );
                return( FALSE );
        }
        if( _FlagAddFiles.IsValueSet() ) {
                _Message.Set( MSG_PRINT_NOT_IMPLEMENTED );
                _Message.Display( "%s", "/P:" );
                return( FALSE );
        }

        //
        //      Displays help message if /? was found in the command line
        //
        if( FlagDisplayHelp.QueryFlag() ) {
                _Message.Set( MSG_PRINT_HELP_MESSAGE );
                _Message.Display( " " );
                return( FALSE );
        }

        //
        //      If no filename was specified, display error message
        //
        if( _Files.QueryPathCount() == 0 ) {
                _Message.Set( MSG_PRINT_NO_FILE );
                _Message.Display( " " );
                return( FALSE );
        }

        //
        // Get device name if one exists. Otherwise use PRN as default
        //
        if( !DeviceArgument.IsValueSet() ) {
                DevicePath = NEW( PATH );
                DebugPtrAssert( DevicePath );
        if( !DevicePath->Initialize( (LPWSTR)L"PRN" ) ) {
                        _Message.Set( MSG_PRINT_UNABLE_INIT_DEVICE );
                        _Message.Display( "%s", "PRN" );
                        return( FALSE );
                }
        } else {
                DevicePath = DeviceArgument.GetPath();
                DebugPtrAssert( DevicePath );
        }
        if( !_Printer.Initialize( DevicePath ) ) {
                DeviceString = DevicePath->GetPathString();
                DebugPtrAssert( DeviceString );
                _Message.Set( MSG_PRINT_UNABLE_INIT_DEVICE );
                _Message.Display( "%W", DeviceString );
                if( !DeviceArgument.IsValueSet() ) {
                        DELETE( DevicePath );
                }
                return( FALSE );
        }

        //
        //      Get FSNODE of each file and put them in an array
        //
        PathArray = _Files.GetPathArray();
        DebugPtrAssert( PathArray );
        PathArrayIterator = ( PARRAY_ITERATOR )PathArray->QueryIterator();
        DebugPtrAssert( PathArrayIterator );

        if( !_FsnFileArray.Initialize() ) {
                DebugAbort( "_FsnFileArray.Initialize() failed \n" );
                return( FALSE );
        }
        while( ( FilePath = ( PPATH )PathArrayIterator->GetNext() ) != NULL ) {
                FsnFile = SYSTEM::QueryFile( FilePath );
                if( FsnFile != NULL ) {
                        _FsnFileArray.Put( ( POBJECT )FsnFile );
                } else {
                        FileString = FilePath->GetPathString();
                        _Message.Set( MSG_PRINT_FILE_NOT_FOUND );
                        _Message.Display( "%W", FileString );
                }
        }
        DELETE( PathArrayIterator );
        return( TRUE );
}


BOOLEAN
PRINT::PrintFiles(
        )

/*++

Routine Description:

        Prints the files specified by the user.

Arguments:

        None.

Return Value:

        BOOLEAN - TRUE if all files were printed.


--*/

{
        PFSN_FILE                       FsnFile;
        PSTREAM                         FileStream;
        PARRAY_ITERATOR         ArrayIterator;
        PBYTE                           Buffer;
        ULONG                           Size;
        ULONG                           BytesRead;
        ULONG                           BytesWritten;
        PCPATH                          FilePath;
        PCWSTRING                       FileName;
        INT                             _BufferStreamType;
        INT                             BytesConverted;
        INT                             cbStuff;
        LPSTR                           lpStuffANSI;
        BOOL                            fUsedDefault;


        Size = 512;
        Buffer = ( PBYTE )MALLOC( ( size_t )Size );
        lpStuffANSI = ( LPSTR )MALLOC( ( size_t )Size );
        DebugPtrAssert( Buffer );
        DebugPtrAssert( lpStuffANSI );
        ArrayIterator = ( PARRAY_ITERATOR )_FsnFileArray.QueryIterator();
        DebugPtrAssert( ArrayIterator );
        while( ( FsnFile = ( PFSN_FILE )ArrayIterator->GetNext() ) != NULL ) {
                FileStream = ( PSTREAM )FsnFile->QueryStream( READ_ACCESS );
                DebugPtrAssert( FileStream );

                FileName = FsnFile->GetPath()->GetPathString();
                if( FileName != NULL ) {
                    _Message.Set( MSG_PRINT_PRINTING );
                    _Message.Display( "%W", FileName );
                }

                _BufferStreamType = -1;
                while( !FileStream->IsAtEnd() ) {
                        FileStream->Read( Buffer, Size, &BytesRead );
                        // is file unicode?
                        if (_BufferStreamType < 0) {
                           if (IsTextUnicode((LPTSTR)Buffer, (INT)BytesRead, 
							NULL) ) {
                              _BufferStreamType = 1;
                           } else {
                              _BufferStreamType = 0;
                           }
                        }
                        // does the buffer need to be converted?
                        if (_BufferStreamType == 1) {
                   
                           cbStuff = Size;
                           BytesConverted = WideCharToMultiByte(CP_ACP,0,
                                   (LPTSTR)Buffer,BytesRead/sizeof(WCHAR),
                                   lpStuffANSI,cbStuff,NULL,&fUsedDefault) ;
                           DebugAssert(cbStuff>0);
                           DebugAssert(BytesConverted >= 0);
                           _Printer.Write((PBYTE)lpStuffANSI,BytesConverted,&BytesWritten );
                        } else {
                           _Printer.Write( Buffer, BytesRead, &BytesWritten );
                        }
                }
                _Printer.WriteByte( '\f' );
                DELETE( FileStream );
        }
        DELETE( ArrayIterator );
        return( TRUE );
}




BOOL
PRINT::Terminate(
        )

/*++

Routine Description:

        Deletes objects created during initialization.

Arguments:

        None.

Return Value:

        None.


--*/

{
        return( TRUE );
}





ULONG _CRTAPI1
main()

{
        DEFINE_CLASS_DESCRIPTOR( PRINT );


        {

                PRINT   Print;


                if( Print.Initialize() ) {
                        Print.PrintFiles();
                }
        //      Print.Terminate();
                return( 0 );
        }
}