[41ee4f]: src / clistub.c  Maximize  Restore  History

Download this file

590 lines (526 with data), 18.8 kB

  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
/*
* clistub.c
*
* $Id$
*
* Written by Keith Marshall <keithmarshall@users.sourceforge.net>
* Copyright (C) 2009, 2010, 2011, MinGW Project
*
*
* Initiation stub for command line invocation of mingw-get
*
*
* This is free software. Permission is granted to copy, modify and
* redistribute this software, under the provisions of the GNU General
* Public License, Version 3, (or, at your option, any later version),
* as published by the Free Software Foundation; see the file COPYING
* for licensing details.
*
* Note, in particular, that this software is provided "as is", in the
* hope that it may prove useful, but WITHOUT WARRANTY OF ANY KIND; not
* even an implied WARRANTY OF MERCHANTABILITY, nor of FITNESS FOR ANY
* PARTICULAR PURPOSE. Under no circumstances will the author, or the
* MinGW Project, accept liability for any damages, however caused,
* arising from the use of this software.
*
*/
#define _WIN32_WINNT 0x500
#define WIN32_LEAN_AND_MEAN
#include "debug.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <process.h>
#include <getopt.h>
#include "pkgopts.h"
#define EXIT_FATAL EXIT_FAILURE + 1
wchar_t *AppPathNameW( const wchar_t *relpath )
{
/* UTF-16LE implementation; NOT thread safe...
*
* Map "relpath" into the file system hierarchy with logical root
* at the prefix where the application suite is installed, such that
* it becomes "d:\prefix\relpath".
*
* If the application's executables are installed in a directory called
* "bin" or "sbin", then this final directory is excluded from the mapped
* prefix, (i.e. a program installed as "d:\prefix\bin\prog.exe" returns
* the mapped path as "d:\prefix\relpath", rather than as the inclusive
* path "d:\prefix\bin\relpath"); in any other case, the prefix is taken
* as the path to the directory in which the program file is installed,
* (i.e. a program installed as "d:\prefix\foo\prog.exe" returns the
* mapped path as "d:\prefix\foo\relpath".
*
* Mapped path is returned in this static buffer...
*/
static wchar_t retpath[MAX_PATH], *tail = NULL;
if( tail == NULL )
{
/* First time initialisation...
*
* Fill in the static local buffer, with the appropriate "prefix"
* string, marking the point at which "relpath" is to be appended.
*
* On subsequent calls, we reuse this static buffer, leaving the
* "prefix" element unchanged, but simply overwriting the "relpath"
* element from any prior call; this is NOT thread safe!
*/
wchar_t prev = L'\\';
wchar_t bindir[] = L"bin", *bindir_p = bindir;
wchar_t *mark, *scan = mark = tail = retpath;
/* Ascertain the installation path of the calling executable.
*/
int chk = GetModuleFileNameW( NULL, retpath, MAX_PATH );
/* Validate it; reject any result which doesn't fit in "retpath".
*/
if( (chk == 0) || ((chk == MAX_PATH) && (retpath[--chk] != L'\0')) )
return tail = NULL;
/* Parse it, to locate the end of the effective "prefix" string...
*/
do { if( *scan == L'/' )
/*
* This is a sanity check; it should not be necessary, but...
*
* Enforce use of "\" rather than "/" as path component separator;
* ( "LoadLibrary" may be broken, since it seems to care! )
*/
*scan = L'\\';
if( *scan && (prev == L'\\') )
{
/* We found the start a new path name component directory,
* (or maybe the file name, at the end); mark it as a possible
* final element of the path name, leaving "tail" pointing to
* the previously marked element.
*/
tail = mark;
mark = scan;
}
} while( (prev = *scan++) != L'\0' );
/* When we get to here, "mark" should point to the executable file name,
* at the end of the path name string, while "tail" should point to the
* last directory in the installation path; we now check, without regard
* to case, if this final directory name is "bin" or "sbin"...
*/
if( (*(scan = tail) == L's') || (*scan == L'S') )
/*
* Might be "sbin"; skip the initial "s", and check for "bin"...
*/
++scan;
while( *bindir_p && ((*scan++ | L'\x20') == *bindir_p++) )
/*
* ...could still match "bin"...
*/ ;
if( *bindir_p || (*scan != L'\\') )
/*
* No, it didn't match; adjust "tail", so we leave the final
* directory name as part of "prefix".
*/
tail = mark;
}
if( relpath == NULL )
/*
* No "relpath" argument given; simply truncate, to return only
* the effective "prefix" string...
*/
*tail = L'\0';
else
{ wchar_t *append = tail;
do { /*
* Append the specified path to the application's root,
* again, taking care to use "\" as the separator character...
*/
*append++ = (*relpath == L'/') ? L'\\' : *relpath;
} while( *relpath++ && ((append - retpath) < MAX_PATH) );
if( *--append != L'\0' )
/*
* Abort, if we didn't properly terminate the return string.
*/
return NULL;
}
return retpath;
}
extern const char *version_identification;
static const char *help_text =
"Manage MinGW and MSYS installations (command line user interface).\n\n"
"Usage:\n"
" mingw-get [OPTIONS] ACTION [package-spec ...]\n\n"
" mingw-get update\n"
" mingw-get [OPTIONS] {install | upgrade | remove} package-spec ...\n"
" mingw-get [OPTIONS] {show | list} [package-spec ...]\n\n"
"Options:\n"
" --help, -h Show this help text\n"
" --version, -V Show version and licence information\n"
" --verbose, -v Increase verbosity of diagnostic or\n"
" progress reporting output; repeat up\n"
" to three times for maximum verbosity\n"
" --verbose=N Set verbosity level to N; (0 <= N <= 3)\n"
/* The "--trace" option is available only when dynamic tracing
* debugging support is compiled in; don't advertise it otherwise.
*/
#if DEBUG_ENABLED( DEBUG_TRACE_DYNAMIC )
" --trace=N Enable tracing feature N; (debugging aid)\n"
#endif
/* The following are always available...
*/
" --reinstall When performing an install or upgrade\n"
" operation, reinstall any named package\n"
" for which the most recent release is\n"
" already installed\n"
"\n"
"Actions:\n"
" update Update local copy of repository catalogues\n"
" list, show List and show details of available packages\n"
" install Install new packages\n"
" upgrade Upgrade previously installed packages\n"
" remove Remove previously installed packages\n\n"
"Package Specifications:\n"
" [subsystem-]name[-component]:\n"
" msys-bash-doc The 'doc' component of the bash package for MSYS\n"
" mingw32-gdb All components of the gdb package for MinGW\n\n"
"Use 'mingw-get list' to identify possible package names\n"
"and the components associated with each.\n\n";
#define IMPLEMENT_INITIATION_RITES PHASE_ONE_RITES
#include "rites.c"
static __inline__ __attribute__((__always_inline__))
char **cli_setargv( HMODULE my_dll, struct pkgopts *opts, char **argv )
{
/* A local wrapper function to facilitate passing pre-parsed
* command line options while performing the "climain()" call
* into mingw-get-0.dll
*
* Note that this requires a version of mingw-get-0.dll which
* provides the "cli_setopts()" hook...
*/
typedef void (*dll_hook)(struct pkgopts *);
dll_hook cli_setopts = (dll_hook)(GetProcAddress( my_dll, "cli_setopts" ));
if( cli_setopts != NULL )
/*
* ...which allows us to pass the pre-parsed options.
*/
cli_setopts( opts );
/* In any case, we always return the argument vector which is
* to be passed in the "climain()" call itself.
*/
return argv;
}
/* FIXME: We may ultimately choose to factor the following atoi() replacement
* into a separate, free-standing module. For now we keep it here, as a static
* function, but we keep the required #include adjacent.
*/
#include <ctype.h>
static int xatoi( const char *input )
{
/* A replacement for the standard atoi() function; this implementation
* supports conversion of octal or hexadecimal representations, in addition
* to the decimal representation required by standard atoi().
*
* We begin by initialising the result accumulator to zero...
*/
int result = 0;
/* ...then, provided we have an input string to interpret...
*/
if( input != NULL )
{
/* ...we proceed with interpretation and accumulation of the result,
* noting that we may have to handle an initial minus sign, (but we
* don't know yet, so assume that not for now).
*/
int negate = 0;
while( isspace( *input ) )
/*
* Ignore leading white space.
*/
++input;
if( *input == '-' )
/*
* An initial minus sign requires negation
* of the accumulated result...
*/
negate = *input++;
else if( *input == '+' )
/*
* ...while an initial plus sign is redundant,
* and may simply be ignored.
*/
++input;
if( *input == '0' )
{
/* An initial zero signifies either hexadecimal
* or octal representation...
*/
if( (*++input | '\x20') == 'x' )
/*
* ...with following 'x' or 'X' indicating
* the hexadecimal case.
*/
while( isxdigit( *++input ) )
{
/* Interpret sequence of hexadecimal digits...
*/
result = (result << 4) + *input;
if( *input > 'F' )
/*
* ...with ASCII to binary conversion for
* lower case digits 'a'..'f',...
*/
result -= 'a' - 10;
else if( *input > '9' )
/*
* ...ASCII to binary conversion for
* upper case digits 'A'..'F',...
*/
result -= 'A' - 10;
else
/* ...or ASCII to decimal conversion,
* as appropriate.
*/
result -= '0';
}
else while( (*input >= '0') && (*input < '8') )
/*
* Interpret sequence of octal digits...
*/
result = (result << 3) + *input++ - '0';
}
else while( isdigit( *input ) )
/*
* Interpret sequence of decimal digits...
*/
result = (result << 1) + (result << 3) + *input++ - '0';
if( negate )
/*
* We had an initial minus sign, so we must
* return the negated result...
*/
return (0 - result);
}
/* ...otherwise, we simply return the accumulated result
*/
return result;
}
#define atmost( lim, val ) ((lim) < (val)) ? (lim) : (val)
int main( int argc, char **argv )
{
/* Make a note of...
*/
const char *progname = basename( *argv ); /* ...this program's name */
wchar_t *approot; /* and where it is installed */
/* Provide storage for interpretation of any parsed command line options.
* Note that we could also initialise them here, but then we would need to
* give attention to the number of initialisers required; to save us that
* concern we will defer to an initialisation loop, below.
*/
struct pkgopts parsed_options;
if( argc > 1 )
{
/* The user specified arguments on the command line...
* Interpret any which specify processing options for this application,
* (these are all specified in GNU `long only' style).
*/
int optref;
struct option options[] =
{
/* Option Name Argument Category Store To Return Value
* -------------- ------------------ -------- ----------------
*/
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{ "verbose", optional_argument, NULL, OPTION_VERBOSE },
{ "reinstall", no_argument, &optref, OPTION_REINSTALL },
# if DEBUG_ENABLED( DEBUG_TRACE_DYNAMIC )
/* The "--trace" option is supported only when dynamic tracing
* debugging support has been compiled in.
*/
{ "trace", required_argument, &optref, OPTION_TRACE },
# endif
/* This list must be terminated by a null definition...
*/
{ NULL, 0, NULL, 0 }
};
int opt, offset;
for( opt = OPTION_FLAGS; opt < OPTION_TABLE_SIZE; ++opt )
/*
* Ensure that all entries within the options table are initialised
* to zero, (equivalent to NULL for pointer entries)...
*/
parsed_options.flags[opt].numeric = 0;
while( (opt = getopt_long_only( argc, argv, "vVh", options, &offset )) != -1 )
/*
* Parse any user specified options from the command line...
*/
switch( opt )
{
case 'V':
/* This is a request to display the version of the application;
* emit the requisite informational message, and quit.
*/
printf( version_identification );
return EXIT_SUCCESS;
case 'h':
/* This is a request to display help text and quit.
*/
printf( help_text );
return EXIT_SUCCESS;
case OPTION_VERBOSE:
/* This is a request to set an explicit verbosity level,
* (minimum zero, maximum three), or if no explicit argument
* is specified, to increment verbosity as does "-v".
*/
if( optarg != NULL )
{
/* This is the case where an explicit level was specified...
*/
parsed_options.flags[OPTION_FLAGS].numeric =
(parsed_options.flags[OPTION_FLAGS].numeric & ~OPTION_VERBOSE)
| atmost( OPTION_VERBOSE_MAX, xatoi( optarg ));
break;
}
case 'v':
/* This is a request to increment the verbosity level
* from its initial zero setting, to a maximum of three.
*/
if( (parsed_options.flags[OPTION_FLAGS].numeric & OPTION_VERBOSE) < 3 )
++parsed_options.flags[OPTION_FLAGS].numeric;
break;
case OPTION_GENERIC:
switch( optref & OPTION_STORAGE_CLASS )
{
/* This represents a generic option specification,
* allowing for storage of a option argument of the
* specified class into a specified option slot...
*/
unsigned shift;
case OPTION_STORE_STRING:
/* This is a simple store of a option argument
* which represents a character string.
*/
parsed_options.flags[optref & 0xfff].string = optarg;
break;
case OPTION_STORE_NUMBER:
/* This is also a simple store of the argument value,
* in this case interpreted as a number.
*/
parsed_options.flags[optref & 0xfff].numeric = xatoi( optarg );
break;
case OPTION_MERGE_NUMBER:
/* In this case, we combine the value of the argument,
* again interpreted as a number, with the original value
* stored in the option slot, forming the bitwise logical
* .OR. of the pair of values.
*/
parsed_options.flags[optref & 0xfff].numeric |= xatoi( optarg );
break;
default:
/* This is a mask and store operation for a specified
* bit-field within the first pair of flags slots; in
* this case, the optref value itself specifies a 12-bit
* value, a 12-bit combining mask, and an alignment shift
* count between 0 and 52, in 4-bit increments.
*/
if( (shift = (optref & OPTION_SHIFT_MASK) >> 22) < 53 )
{
uint64_t value = optref & 0xfff;
uint64_t mask = (optref & 0xfff000) >> 12;
*(uint64_t *)(parsed_options.flags) &= ~(mask << shift);
*(uint64_t *)(parsed_options.flags) |= value << shift;
}
}
break;
default:
/* User specified an invalid or unsupported option...
*/
if( opt != '?' )
fprintf( stderr, "%s: option '-%s' not yet supported\n",
progname, options[offset].name
);
return EXIT_FAILURE;
}
}
/* Establish the installation path for the mingw-get application...
*/
if( (approot = AppPathNameW( NULL )) != NULL )
{
/* ...and set up the APPROOT environment variable to refer to
* the associated installation prefix...
*/
char approot_setup[1 + snprintf( NULL, 0, "APPROOT=%S", approot )];
snprintf( approot_setup, sizeof( approot_setup ), "APPROOT=%S", approot );
putenv( approot_setup );
}
if( argc > 1 )
{
/* The user specified arguments on the command line...
* we load the supporting DLL into the current process context,
* then, remaining in command line mode, we jump to its main
* command line processing routine...
*/
int lock;
char *argv_base = *argv;
typedef int (*dll_entry)( int, char ** );
HMODULE my_dll = LoadLibraryW( AppPathNameW( MINGW_GET_DLL ) );
dll_entry climain = (dll_entry)(GetProcAddress( my_dll, "climain" ));
if( climain == NULL )
{
/* ...bailing out, on failure to load the DLL.
*/
fprintf( stderr, "%s: %S: shared library load failed\n",
progname, MINGW_GET_DLL
);
return EXIT_FATAL;
}
/* Adjust argc and argv to discount parsed options...
*/
argc -= optind;
argv += optind - 1;
/*
* ...while preserving the original argv[0] reference within
* the first remaining argument to be passed to climain().
*/
*argv = argv_base;
/* We want only one mingw-get process accessing the XML database
* at any time; attempt to acquire an exclusive access lock...
*/
if( (lock = pkgInitRites( progname )) >= 0 )
{
/* ...and proceed, only if successful.
* A non-zero return value indicates that a fatal error occurred.
*/
int rc = climain( argc, cli_setargv( my_dll, &parsed_options, argv ) );
/* We must release the mingw-get DLL code, BEFORE we invoke
* last rites processing, (otherwise the last rites clean-up
* handler exhibits abnormal behaviour when it is exec'd).
*/
FreeLibrary( my_dll );
if (rc == 0)
return pkgLastRites( lock, progname );
else
{
(void) pkgLastRites( lock, progname );
return EXIT_FATAL;
}
}
/* If we get to here, then we failed to acquire a lock;
* we MUST abort!
*/
return EXIT_FATAL;
}
else
{ /* No arguments were specified on the command line...
* we interpret this as a request to start up in GUI mode...
*/
wchar_t *libexec_path = AppPathNameW( MINGW_GET_GUI );
char gui_program[1 + snprintf( NULL, 0, "%S", libexec_path )];
snprintf( gui_program, sizeof( gui_program ), "%S", libexec_path );
int status = execv( gui_program, (const char* const*)(argv) );
/* If we get to here, then the GUI could not be started...
* Issue a diagnostic message, before abnormal termination.
*/
fprintf( stderr,
"%s: %S: unable to start GUI; helper program not installed\n",
progname, MINGW_GET_GUI
);
return EXIT_FATAL;
}
}
/* $RCSfile$: end of file */

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks