6 * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
7 * Copyright (C) 2009, 2010, 2011, MinGW Project
10 * Implementation of the main program function, which is invoked by
11 * the command line start-up stub when arguments are supplied; this
12 * causes the application to continue running as a CLI process.
15 * This is free software. Permission is granted to copy, modify and
16 * redistribute this software, under the provisions of the GNU General
17 * Public License, Version 3, (or, at your option, any later version),
18 * as published by the Free Software Foundation; see the file COPYING
19 * for licensing details.
21 * Note, in particular, that this software is provided "as is", in the
22 * hope that it may prove useful, but WITHOUT WARRANTY OF ANY KIND; not
23 * even an implied WARRANTY OF MERCHANTABILITY, nor of FITNESS FOR ANY
24 * PARTICULAR PURPOSE. Under no circumstances will the author, or the
25 * MinGW Project, accept liability for any damages, however caused,
26 * arising from the use of this software.
42 EXTERN_C void cli_setopts( struct pkgopts *opts )
44 /* Start-up hook used to make the table of command line options,
45 * as parsed by the CLI start-up module, available within the DLL.
47 (void) pkgOptions( OPTION_TABLE_ASSIGN, opts );
50 EXTERN_C pkgOpts *pkgOptions( int action, struct pkgopts *ref )
52 /* Global accessor for the program options data table.
54 static pkgOpts *table = NULL;
55 if( action == OPTION_TABLE_ASSIGN )
57 * This is a request to initialise the data table reference;
58 * it is typically called at program start-up, to record the
59 * location of the data table into which the CLI start-up
60 * module stores the result of its CLI options parse.
62 table = (pkgOpts *)(ref);
64 /* In all cases, we return the assigned data table location.
69 class pkgArchiveNameList
71 /* A locally implemented class, managing a LIFO stack of
72 * package names; this is used when processing the source
73 * and licence requests, to track the packages processed,
74 * so that we may avoid inadvertent duplicate processing.
77 const char *name; // name of package tracked
78 pkgArchiveNameList *next; // pointer to next stack entry
81 inline bool NotRecorded( const char *candidate )
83 /* Walk the stack of tracked package names, to determine
84 * if an entry matching "candidate" is already present;
85 * returns false if such an entry exists, otherwise true
86 * to indicate that it may be added as a unique entry.
88 pkgArchiveNameList *check = this;
89 while( check != NULL )
91 /* We haven't walked off the bottom of the stack yet...
93 if( strcmp( check->name, candidate ) == 0 )
95 /* ...and the current entry matches the candidate;
96 * thus the candidate will not be stacked, so we
97 * may discard it from the heap.
99 free( (void *)(candidate) );
101 /* We've found a match, so there is no point in
102 * continuing the search; simply return false to
103 * signal rejection of the candidate.
107 /* No matching entry found yet; walk down to the next
108 * stack entry, if any, to continue the search.
112 /* We walked off the bottom of the stack, without finding
113 * any match; return true to accept the candidate.
117 inline pkgArchiveNameList *Record( const char *candidate )
119 /* Add a new entry at the top of the stack, to record
120 * the processing of an archive named by "candidate";
121 * on entry "this" is the current stack pointer, and
122 * we return the new stack pointer, referring to the
123 * added entry which becomes the new top of stack.
125 pkgArchiveNameList *retptr = new pkgArchiveNameList();
126 retptr->name = candidate; retptr->next = this;
129 inline ~pkgArchiveNameList()
131 /* Completely clear the stack, releasing the heap memory
132 * allocated to record the stacked package names.
134 free( (void *)(name) );
139 static pkgArchiveNameList *pkgProcessedArchives; // stack pointer
141 EXTERN_C int climain( int argc, char **argv )
144 { /* Set up the diagnostic message handler, using the console's
145 * `stderr' stream for notifications, and tagging messages with
146 * the program basename derived from argv[0]...
149 char progname[ 1 + strlen( dmh_progname = strdup( basename( *argv++ ))) ];
150 dmh_init( DMH_SUBSYSTEM_TTY, strcpy( progname, dmh_progname ) );
151 free( dmh_progname );
153 /* Interpret the `action keyword', specifying the action to be
154 * performed on this invocation...
156 int action = action_code( *argv );
159 * The specified action keyword was invalid;
160 * force an abort through a DMH_FATAL notification...
162 dmh_notify( DMH_FATAL, "%s: unknown action keyword\n", *argv );
164 /* If we get to here, then the specified action identifies a
165 * valid operation; load the package database, according to the
166 * local `profile' configuration, and invoke the operation.
169 if( access( dfile = xmlfile( profile_key ), R_OK ) != 0 )
171 /* The user hasn't provided a custom configuration profile...
173 dmh_notify( DMH_WARNING, "%s: user configuration file missing\n", dfile );
175 /* ...release the memory allocated by xmlfile(), to store its path name,
176 * then try the mingw-get distribution default profile instead.
178 free( (void *)(dfile) );
179 dmh_notify( DMH_INFO, "%s: trying system default configuration\n",
180 dfile = xmlfile( defaults_key ) );
183 pkgXmlDocument dbase( dfile );
186 /* We successfully loaded the basic settings...
187 * The configuration file name was pushed on to the heap,
188 * by xmlfile(); we don't need that any more, (because it
189 * is reproduced within the database image itself), so
190 * free the heap copy, to avoid memory leaks.
192 free( (void *)(dfile) );
194 /* Merge all package lists, as specified in the "repository"
195 * section of the "profile", into the XML database tree...
197 if( dbase.BindRepositories( action == ACTION_UPDATE ) == NULL )
199 * ...bailing out, on an invalid profile specification...
201 dmh_notify( DMH_FATAL, "%s: invalid application profile\n", dbase.Value() );
203 /* If the requested action was "update", then we've already done it,
204 * as a side effect of binding the cached repository catalogues...
206 if( action != ACTION_UPDATE )
208 /* ...otherwise, we need to load the system map...
210 dbase.LoadSystemMap();
212 /* ...and invoke the appropriate action handler.
219 * "list" and "show" actions are synonymous;
220 * invoke the info-display handler.
222 dbase.DisplayPackageInfo( argc, argv );
228 * Process the "source" or "licence" request for one
229 * or more packages; begin with an empty stack of names,
230 * for tracking packages as processed.
232 pkgProcessedArchives = NULL;
233 if( pkgOptions()->Test( OPTION_ALL_DEPS ) == OPTION_ALL_DEPS )
235 /* The "--all-related" option is in effect; for each
236 * package identified on the command line...
240 * ...schedule a request to install the package,
241 * together with all of its runtime dependencies...
243 dbase.Schedule( ACTION_INSTALL, *++argv );
245 /* ...but DON'T proceed with installation; rather
246 * process the "source" or "licence" request for
247 * each scheduled package.
249 dbase.GetScheduledSourceArchives( (unsigned long)(action) );
253 * The "--all-related" option is NOT in effect; simply
254 * process the "source" or "licence" request exclusively
255 * in respect of each package named on the command line.
257 dbase.GetSourceArchive( *++argv, (unsigned long)(action) );
259 /* Finally, clear the stack of processed package names.
261 delete pkgProcessedArchives;
267 * This is a special case of the upgrade request, for which
268 * no explicit package names have been specified; in this case
269 * we retrieve the list of all installed packages, scheduling
270 * each of them for upgrade...
272 dbase.RescheduleInstalledPackages( ACTION_UPGRADE );
274 /* ...subsequently falling through to complete the action,
275 * using the default processing mechanism; (note that in this
276 * case no further scheduling will be performed, because there
277 * are no additional package names specified in the argv list).
280 /* ...schedule the specified action for each additional command line
281 * argument, (each of which is assumed to represent a package name)...
285 * (Skipped if argv < 2 on entry).
287 dbase.Schedule( (unsigned long)(action), *++argv );
289 /* ...finally, execute all scheduled actions, and update the
290 * system map accordingly.
292 dbase.ExecuteActions();
293 dbase.UpdateSystemMap();
296 /* If we get this far, then all actions completed successfully;
302 /* If we get to here, then the package database load failed;
303 * once more, we force an abort through a DMH_FATAL notification...
305 * Note: although dmh_notify does not return, in the DMH_FATAL case,
306 * GCC cannot know this, so we pretend that it gives us a return value,
307 * to avoid a possible warning about reaching the end of a non-void
308 * function without a return value assignment...
310 return dmh_notify( DMH_FATAL, "%s: cannot load configuration\n", dfile );
313 catch( dmh_exception &e )
315 /* An error occurred; it should already have been diagnosed,
316 * so simply bail out.
324 void pkgActionItem::GetSourceArchive( pkgXmlNode *package, unsigned long category )
326 /* Handle a 'mingw-get source ...' or a 'mingw-get licence ...' request
327 * in respect of the source code or licence archive for a single package.
329 const char *src = package->SourceArchiveName( category );
330 if( (src != NULL) && pkgProcessedArchives->NotRecorded( src ) )
332 if( pkgOptions()->Test( OPTION_PRINT_URIS ) == OPTION_PRINT_URIS )
334 /* The --print-uris option is in effect; this is all
335 * that we are expected to do.
341 { /* The --print-uris option is not in effect; we must at
342 * least check that the source package is available in the
343 * source archive cache, and if not, download it...
345 const char *path_template; flags |= ACTION_DOWNLOAD;
346 DownloadSingleArchive( src, path_template = (category == ACTION_SOURCE)
347 ? pkgSourceArchivePath() : pkgArchivePath()
350 /* ...then, unless the --download-only option is in effect...
352 if( pkgOptions()->Test( OPTION_DOWNLOAD_ONLY ) != OPTION_DOWNLOAD_ONLY )
354 /* ...we establish the current working directory as the
355 * destination where it should be unpacked...
357 char source_archive[mkpath( NULL, path_template, src, NULL )];
358 mkpath( source_archive, path_template, src, NULL );
360 /* ...and extract the content from the source archive.
362 pkgTarArchiveExtractor unpack( source_archive, "." );
364 /* The path_template was allocated on the heap; we are
365 * done with it, so release the memory allocation...
367 free( (void *)(path_template) );
370 /* Record the current archive name as "processed", so we may
371 * avoid any inadvertent duplicate processing.
373 pkgProcessedArchives = pkgProcessedArchives->Record( src );
377 void pkgActionItem::GetScheduledSourceArchives( unsigned long category )
379 /* Process "source" or "licence" requests in respect of a list of
380 * packages, (scheduled as if for installation); this is the handler
381 * for the case when the "--all-related" option is in effect for a
382 * "source" or "licence" request.
386 /* The package list is NOT empty; ensure that we begin with
387 * a reference to its first entry.
389 pkgActionItem *scheduled = this;
390 while( scheduled->prev != NULL ) scheduled = scheduled->prev;
392 /* For each scheduled list entry...
394 while( scheduled != NULL )
396 /* ...process the "source" or "licence" request, as appropriate,
397 * in respect of the associated package...
399 scheduled->GetSourceArchive( scheduled->Selection(), category );
401 * ...then move on to the next entry, if any.
403 scheduled = scheduled->next;
408 void pkgXmlDocument::GetSourceArchive( const char *name, unsigned long category )
410 /* Look up a named package reference in the XML catalogue,
411 * then forward it as a pkgActionItem, for processing of an
412 * associated "source" or "licence" request.
414 pkgXmlNode *pkg = FindPackageByName( name );
415 if( pkg->IsElementOfType( package_key ) )
417 /* We found a top-level specification for the required package...
419 pkgXmlNode *component = pkg->FindFirstAssociate( component_key );
420 if( component != NULL )
422 * When this package is subdivided into components,
423 * then we derive the source reference from the first
429 /* Now inspect the "release" specifications within the
430 * selected package/component definition...
432 if( (pkg = pkg->FindFirstAssociate( release_key )) != NULL )
434 /* ...creating a pkgActionItem...
436 pkgActionItem latest;
437 pkgXmlNode *selected = pkg;
439 /* ...and examining each release in turn...
443 /* ...select the most recent release, and assign it
444 * to the pkgActionItem reference...
446 if( latest.SelectIfMostRecentFit( pkg ) == pkg )
447 latest.SelectPackage( selected = pkg );
449 /* ...continuing until we have examined all available
450 * release specifications.
452 pkg = pkg->FindNextAssociate( release_key );
455 /* Finally, hand off the "source" or "licence" processing
456 * request, based on the most recent release selection, to
457 * the pkgActionItem we've just instantiated.
459 latest.GetSourceArchive( selected, category );
463 /* $RCSfile$: end of file */