aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJason Woodward2004-10-03 19:46:19 +0000
committerJason Woodward2004-10-03 19:46:19 +0000
commit502a8bc3130cc0b48eb4dfb8e534df4ed28dde16 (patch)
tree86e43085482e8c755c46456e74ae61cd495e1e33 /src
parentb2f967bb055de02ddb0f6ce9076f5b8ae4d152d8 (diff)
downloadslapt-get-502a8bc3130cc0b48eb4dfb8e534df4ed28dde16.tar.gz
added init_pkg_action_args() and free_pkg_action_args() and merged Michel Hermier's main.c patch cleaning up main
Diffstat (limited to 'src')
-rw-r--r--src/action.c42
-rw-r--r--src/action.h3
-rw-r--r--src/main.c133
3 files changed, 97 insertions, 81 deletions
diff --git a/src/action.c b/src/action.c
index 753bbf4..57d7bec 100644
--- a/src/action.c
+++ b/src/action.c
@@ -115,7 +115,7 @@ void pkg_action_install(const rc_config *global_config,const pkg_action_args_t *
}else{
printf(_("%s is up to date.\n"),installed_pkg->name);
}
-
+
}
}
@@ -150,9 +150,9 @@ void pkg_action_list(void){
if( get_exact_pkg(installed_pkgs,pkgs->pkgs[i]->name,pkgs->pkgs[i]->version) != NULL )
bool_installed = 1;
- printf("%s %s [inst=%s]: %s\n",
- pkgs->pkgs[i]->name,
- pkgs->pkgs[i]->version,
+ printf("%s %s [inst=%s]: %s\n",
+ pkgs->pkgs[i]->name,
+ pkgs->pkgs[i]->version,
bool_installed == 1
? _("yes")
: _("no"),
@@ -254,9 +254,9 @@ void pkg_action_search(const char *pattern){
if( get_exact_pkg(installed_pkgs,matches->pkgs[i]->name,matches->pkgs[i]->version) != NULL )
bool_installed = 1;
- printf("%s %s [inst=%s]: %s\n",
- matches->pkgs[i]->name,
- matches->pkgs[i]->version,
+ printf("%s %s [inst=%s]: %s\n",
+ matches->pkgs[i]->name,
+ matches->pkgs[i]->version,
bool_installed == 1
? _("yes")
: _("no"),
@@ -326,9 +326,8 @@ void pkg_action_show(const char *pkg_name){
pkg = get_newest_pkg(avail_pkgs,pkg_name);
}
-
if( pkg != NULL ){
-
+
if( get_exact_pkg(installed_pkgs,pkg->name,pkg->version) != NULL)
bool_installed = 1;
@@ -345,11 +344,11 @@ void pkg_action_show(const char *pkg_name){
printf(_("Package Description:\n"));
printf("%s",pkg->description);
printf(_("Package Installed: %s\n"),
- bool_installed == 1
+ bool_installed == 1
? _("yes")
: _("no")
);
-
+
}else{
printf(_("No such package: %s\n"),pkg_name);
}
@@ -476,3 +475,24 @@ void pkg_action_upgrade_all(const rc_config *global_config){
free_transaction(&tran);
}
+pkg_action_args_t *init_pkg_action_args(int arg_count){
+ pkg_action_args_t *paa;
+
+ paa = malloc( sizeof *paa );
+ paa->pkgs = malloc( sizeof *paa->pkgs * arg_count );
+ paa->count = 0;
+
+ return paa;
+}
+
+void free_pkg_action_args(pkg_action_args_t *paa){
+ int i;
+
+ for(i = 0; i < paa->count; i++){
+ free(paa->pkgs[i]);
+ }
+
+ free(paa->pkgs);
+ free(paa);
+}
+
diff --git a/src/action.h b/src/action.h
index 994b38e..2b13f5a 100644
--- a/src/action.h
+++ b/src/action.h
@@ -22,6 +22,9 @@ struct _pkg_action_args {
};
typedef struct _pkg_action_args pkg_action_args_t;
+pkg_action_args_t *init_pkg_action_args(int arg_count);
+void free_pkg_action_args(pkg_action_args_t *paa);
+
void pkg_action_install(const rc_config *global_config,const pkg_action_args_t *action_args);
void pkg_action_list(void);
void pkg_action_list_installed(void);
diff --git a/src/main.c b/src/main.c
index d2ea2c3..a9e5499 100644
--- a/src/main.c
+++ b/src/main.c
@@ -59,6 +59,7 @@ int main( int argc, char *argv[] ){
};
int option_index = 0;
/* */
+ pkg_action_args_t *paa;
setvbuf(stdout, (char *)NULL, _IONBF, 0); /* unbuffer stdout */
@@ -177,102 +178,94 @@ int main( int argc, char *argv[] ){
}
}
+ /* Check optionnal arguments presence */
+ switch(do_action){
+ case INSTALL:
+ case REMOVE:
+ case SHOW:
+ case SEARCH:
+ if( optind >= argc )
+ do_action = 0;
+ break;
+ default:
+ if (optind < argc)
+ do_action = USAGE;
+ break;
+ }
+
+ if( do_action == USAGE ){
+ usage();
+ exit(1);
+ }
+
/* create the working directory if needed */
working_dir_init(global_config);
chdir(global_config->working_dir);
- if( do_action == UPDATE ){
- if( update_pkg_cache(global_config) == 1 ) exit(1);
- }else if( do_action == INSTALL ){
- if (optind < argc) {
- int i;
- pkg_action_args_t *paa;
-
- paa = malloc( sizeof *paa );
- paa->pkgs = malloc( sizeof *paa->pkgs * (argc - optind) );
- paa->count = 0;
+ switch(do_action){
+ case UPDATE:
+ if( update_pkg_cache(global_config) == 1 ) exit(1);
+ break;
+ case INSTALL:
+ paa = init_pkg_action_args((argc - optind));
while (optind < argc){
paa->pkgs[paa->count] = malloc(
- ( strlen(argv[optind]) + 1 ) * sizeof *paa->pkgs[paa->count]
+ ( strlen(argv[optind]) + 1 ) * sizeof *paa->pkgs[paa->count]
);
memcpy(paa->pkgs[paa->count],argv[optind],strlen(argv[optind]) + 1);
++optind;
++paa->count;
}
pkg_action_install( global_config, paa );
- for(i = 0; i < paa->count; i++){
- free(paa->pkgs[i]);
- }
- free(paa->pkgs);
- free(paa);
- }else{
- usage();
- exit(1);
- }
- }else if( do_action == REMOVE ){
- if (optind < argc) {
- int i;
- pkg_action_args_t *paa;
-
- paa = malloc( sizeof *paa );
- paa->pkgs = malloc( sizeof *paa->pkgs * (argc - optind) );
- paa->count = 0;
+ free_pkg_action_args(paa);
+ break;
+ case REMOVE:
+ paa = init_pkg_action_args((argc - optind));
while (optind < argc){
paa->pkgs[paa->count] = malloc(
- ( strlen(argv[optind]) + 1 ) * sizeof *paa->pkgs[paa->count]
+ ( strlen(argv[optind]) + 1 ) * sizeof *paa->pkgs[paa->count]
);
memcpy(paa->pkgs[paa->count],argv[optind],strlen(argv[optind]) + 1);
++optind;
++paa->count;
}
pkg_action_remove( global_config, paa );
- for(i = 0; i < paa->count; i++){
- free(paa->pkgs[i]);
- }
- free(paa->pkgs);
- free(paa);
- }else{
- usage();
- exit(1);
- }
- }else if( do_action == SHOW ){
- if (optind < argc) {
+ free_pkg_action_args(paa);
+ break;
+ case SHOW:
while (optind < argc){
pkg_action_show( argv[optind++] );
}
- }else{
- usage();
- exit(1);
- }
- }else if( do_action == SEARCH ){
- if (optind < argc) {
+ break;
+ case SEARCH:
while (optind < argc){
pkg_action_search( argv[optind++] );
}
- }else{
- usage();
- exit(1);
- }
- }else if( do_action == UPGRADE ){
- pkg_action_upgrade_all(global_config);
- }else if( do_action == LIST ){
- pkg_action_list();
- }else if( do_action == INSTALLED ){
- pkg_action_list_installed();
- }else if( do_action == CLEAN ){
- /* clean out local cache */
- clean_pkg_dir(global_config->working_dir);
- chdir(global_config->working_dir);
- }else if( do_action == SHOWVERSION ){
- version_info();
- }else if( do_action == AUTOCLEAN ){
- purge_old_cached_pkgs(global_config->working_dir);
- }else if( do_action == 0 ){ /* default initialized value */
- usage();
- exit(1);
- }else{
- usage();
- exit(1);
+ break;
+ case UPGRADE:
+ pkg_action_upgrade_all(global_config);
+ break;
+ case LIST:
+ pkg_action_list();
+ break;
+ case INSTALLED:
+ pkg_action_list_installed();
+ break;
+ case CLEAN:
+ /* clean out local cache */
+ clean_pkg_dir(global_config->working_dir);
+ chdir(global_config->working_dir);
+ break;
+ case SHOWVERSION:
+ version_info();
+ break;
+ case AUTOCLEAN:
+ purge_old_cached_pkgs(global_config->working_dir, NULL);
+ break;
+ case USAGE:
+ default:
+ printf("main.c(l.%d): This should never be reached\n", __LINE__);
+ exit(255);
}
free_rc_config(global_config);