[pacman-dev] [PATCH v2] wip: pacman: rework the UI of -F

morganamilo morganamilo at gmail.com
Sat Apr 20 19:36:17 UTC 2019


Reworks the UI of -F according to FS#47949

In short -F replaces both -Fs and -Fo.
--regex/-x has been replaced with --search/-s.
--oneline/-o can be used to change the output format to match the old -Fo

Signed-off-by: morganamilo <morganamilo at gmail.com>
---

v1:
	This patch is WIP. Functional changes made,
	documentation still needs to be changed.

	Additionally I think https://bugs.archlinux.org/task/47949#comment143477
	Is a good idea and I will probably be included in v2

v2:
	added --oneline/-o

diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index f45ed436..523b54a5 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -90,8 +90,8 @@ typedef struct __config_t {
 	unsigned short op_s_search;
 	unsigned short op_s_upgrade;
 
-	unsigned short op_f_regex;
 	unsigned short op_f_machinereadable;
+	unsigned short op_f_oneline;
 
 	unsigned short group;
 	unsigned short noask;
@@ -200,6 +200,7 @@ enum {
 	OP_SEARCH,
 	OP_REGEX,
 	OP_MACHINEREADABLE,
+	OP_ONELINE,
 	OP_UNREQUIRED,
 	OP_UPGRADES,
 	OP_SYSUPGRADE,
diff --git a/src/pacman/files.c b/src/pacman/files.c
index 3ebd9b9b..905e16ed 100644
--- a/src/pacman/files.c
+++ b/src/pacman/files.c
@@ -49,54 +49,13 @@ static void dump_pkg_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg)
 	}
 }
 
-static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) {
-	int ret = 0;
-	alpm_list_t *t;
-
-	for(t = targets; t; t = alpm_list_next(t)) {
-		char *filename = t->data;
-		int found = 0;
-		alpm_list_t *s;
-		size_t len = strlen(filename);
-
-		while(len > 1 && filename[0] == '/') {
-			filename++;
-			len--;
-		}
-
-		for(s = syncs; s; s = alpm_list_next(s)) {
-			alpm_list_t *p;
-			alpm_db_t *repo = s->data;
-			alpm_list_t *packages = alpm_db_get_pkgcache(repo);
-
-			for(p = packages; p; p = alpm_list_next(p)) {
-				alpm_pkg_t *pkg = p->data;
-				alpm_filelist_t *files = alpm_pkg_get_files(pkg);
-
-				if(alpm_filelist_contains(files, filename)) {
-					if(config->op_f_machinereadable) {
-						print_line_machinereadable(repo, pkg, filename);
-					} else if(!config->quiet) {
-						const colstr_t *colstr = &config->colstr;
-						printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename,
-								colstr->repo, alpm_db_get_name(repo), colstr->title,
-								alpm_pkg_get_name(pkg), colstr->version,
-								alpm_pkg_get_version(pkg), colstr->nocolor);
-					} else {
-						printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg));
-					}
-
-					found = 1;
-				}
-			}
-		}
-
-		if(!found) {
-			ret++;
-		}
-	}
-
-	return 0;
+static void print_owned_by(alpm_db_t *db, alpm_pkg_t *pkg, char *filename)
+{
+	const colstr_t *colstr = &config->colstr;
+	printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename,
+		colstr->repo, alpm_db_get_name(db), colstr->title,
+		alpm_pkg_get_name(pkg), colstr->version,
+		alpm_pkg_get_version(pkg), colstr->nocolor);
 }
 
 static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg)
@@ -110,6 +69,12 @@ static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg)
 			char *filename = ml->data;
 			print_line_machinereadable(repo, pkg, filename);
 		}
+	} else if(config->op_f_oneline) {
+		alpm_list_t *ml;
+		for(ml = match; ml; ml = alpm_list_next(ml)) {
+			char *filename = ml->data;
+			print_owned_by(repo, pkg, filename);
+		}
 	} else if(config->quiet) {
 		printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg));
 	} else {
@@ -138,6 +103,15 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) {
 		alpm_list_t *s;
 		int found = 0;
 		regex_t reg;
+		size_t len = strlen(targ);
+		char *exact_file = strchr(targ, '/');
+
+		if(exact_file != NULL) {
+			while(len > 1 && targ[0] == '/') {
+				targ++;
+				len--;
+			}
+		}
 
 		if(regex) {
 			if(regcomp(&reg, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) {
@@ -153,26 +127,40 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) {
 			int m;
 
 			for(p = packages; p; p = alpm_list_next(p)) {
-				size_t f = 0;
-				char* c;
 				alpm_pkg_t *pkg = p->data;
 				alpm_filelist_t *files = alpm_pkg_get_files(pkg);
 				alpm_list_t *match = NULL;
 
-				while(f < files->count) {
-					c = strrchr(files->files[f].name, '/');
-					if(c && *(c + 1)) {
-						if(regex) {
-							m = regexec(&reg, (c + 1), 0, 0, 0);
-						} else {
-							m = strcmp(c + 1, targ);
+				if(exact_file != NULL) {
+					if (regex) {
+						for(size_t f = 0; f < files->count; f++) {
+							char *c = files->files[f].name;
+							if(regexec(&reg, c, 0, 0, 0) == 0) {
+								match = alpm_list_add(match, files->files[f].name);
+								found = 1;
+							}
 						}
-						if(m == 0) {
-							match = alpm_list_add(match, files->files[f].name);
+					} else {
+						if(alpm_filelist_contains(files, targ)) {
+							match = alpm_list_add(match, targ);
 							found = 1;
 						}
 					}
-					f++;
+				} else {
+					for(size_t f = 0; f < files->count; f++) {
+						char *c = strrchr(files->files[f].name, '/');
+						if(c && *(c + 1)) {
+							if(regex) {
+								m = regexec(&reg, (c + 1), 0, 0, 0);
+							} else {
+								m = strcmp(c + 1, targ);
+							}
+							if(m == 0) {
+								match = alpm_list_add(match, files->files[f].name);
+								found = 1;
+							}
+						}
+					}
 				}
 
 				if(match != NULL) {
@@ -307,30 +295,16 @@ int pacman_files(alpm_list_t *targets)
 		}
 	}
 
-	if(targets == NULL && (config->op_q_owns | config->op_s_search)) {
-		pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
-		return 1;
-	}
-
-	/* determine the owner of a file */
-	if(config->op_q_owns) {
-		return files_fileowner(files_dbs, targets);
-	}
-
-	/* search for a file */
-	if(config->op_s_search) {
-		return files_search(files_dbs, targets, config->op_f_regex);
-	}
-
 	/* get a listing of files in sync DBs */
 	if(config->op_q_list) {
 		return files_list(files_dbs, targets);
 	}
 
-	if(targets != NULL) {
-		pm_printf(ALPM_LOG_ERROR, _("no options specified (use -h for help)\n"));
+	if(targets == NULL) {
+		pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
 		return 1;
 	}
 
-	return 0;
+	/* search for a file */
+	return files_search(files_dbs, targets, config->op_q_search);
 }
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 3bb622e6..45b3ab9b 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -753,29 +753,25 @@ static int parsearg_files(int opt)
 		return 0;
 	}
 	switch(opt) {
-		case OP_OWNS:
-		case 'o':
-			config->op_q_owns = 1;
-			break;
 		case OP_LIST:
 		case 'l':
 			config->op_q_list = 1;
 			break;
 		case OP_SEARCH:
 		case 's':
-			config->op_s_search = 1;
+			config->op_q_search = 1;
 			break;
 		case OP_REFRESH:
 		case 'y':
 			(config->op_s_sync)++;
 			break;
-		case OP_REGEX:
-		case 'x':
-			config->op_f_regex = 1;
-			break;
 		case OP_MACHINEREADABLE:
 			config->op_f_machinereadable = 1;
 			break;
+		case OP_ONELINE:
+		case 'o':
+			config->op_f_oneline = 1;
+			break;
 		case OP_QUIET:
 		case 'q':
 			config->quiet = 1;
@@ -788,13 +784,8 @@ static int parsearg_files(int opt)
 
 static void checkargs_files(void)
 {
-	if(config->op_q_owns) {
-		invalid_opt(config->op_q_list, "--owns", "--list");
-		invalid_opt(config->op_q_search, "--owns", "--search");
-		invalid_opt(config->op_f_regex, "--owns", "--regex");
-	} else if(config->op_q_list) {
-		invalid_opt(config->op_q_search, "--list", "--search");
-		invalid_opt(config->op_f_regex, "--list", "--regex");
+	if(config->op_q_search) {
+		invalid_opt(config->op_q_list, "--search", "--list");
 	}
 }
 
@@ -918,6 +909,7 @@ static int parseargs(int argc, char *argv[])
 		{"native",     no_argument,       0, OP_NATIVE},
 		{"nosave",     no_argument,       0, OP_NOSAVE},
 		{"owns",       no_argument,       0, OP_OWNS},
+		{"oneline",    no_argument,       0, OP_ONELINE},
 		{"file",       no_argument,       0, OP_FILE},
 		{"print",      no_argument,       0, OP_PRINT},
 		{"quiet",      no_argument,       0, OP_QUIET},
-- 
2.21.0


More information about the pacman-dev mailing list