Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
- Fixed a few bugs that happened when entering multiple tags.
  • Loading branch information
hippie68 authored May 13, 2024
1 parent 31824e3 commit a56bf8a
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 377 deletions.
1 change: 1 addition & 0 deletions pkgrename.c/include/strings.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef STRINGS_H
#define STRINGS_H

void trim_string(char *string, char *ltrim, char *rtrim);
char *strwrd(const char *string, char *word);
char *strreplace(char *string, char *search, char *replace);
void mixed_case(char *string);
Expand Down
96 changes: 49 additions & 47 deletions pkgrename.c/pkgrename.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static struct scan *pkgrename(struct scan *scan)
if (n > 1) {
// Remove all tags but the 1st.
// Note: if there ever is demand, this line can be removed to
// implement multiple release tags.
// automatically retreive all tags from the changelog.
*(strchr(release, ',')) = '\0';

if (! option_query)
Expand Down Expand Up @@ -452,7 +452,7 @@ static struct scan *pkgrename(struct scan *scan)
strreplace(new_basename, "%merged_ver%", merged_ver);
strreplace(new_basename, "%msum%", msum);
strreplace(new_basename, "%region%", region);
if (tag_release_group[0] != '\0')
if (tag_release_group[0] != '\0')
strreplace(new_basename, "%release_group%", tag_release_group);
else
strreplace(new_basename, "%release_group%", release_group);
Expand Down Expand Up @@ -699,36 +699,30 @@ static struct scan *pkgrename(struct scan *scan)
char tag[MAX_TAG_LEN] = "";
printf("\nEnter new tag: ");
scan_string(tag, MAX_TAG_LEN, "", get_tag);
trim_string(tag, " ,", " ,");
char *result;
int n_results;
if (tag[0] != '\0') {
// Trim leading whitespace and commas.
size_t len = strlen(tag);
size_t i = 0;
while(tag[i] == ' ' || tag[i] == ',')
i++;
if (i > 0)
memmove(tag, tag + i, len - i + 1);

// Trim trailing whitespace and commas.
i = len - 1;
while (tag[i] == ' ' || tag[i] == ',')
tag[i--] = '\0';

// Get entered known release groups.
if ((result = get_release_group(tag)) != NULL) {
printf("Using \"%s\" as release group.\n", result);
strncpy(tag_release_group, result, MAX_TAG_LEN);
tag_release_group[MAX_TAG_LEN] = '\0';
}

// Get entered known releases.
if ((n_results = get_release(&result, tag)) != 0) {
printf("Using \"%s\" as release.\n", result);
strncpy(tag_release, result, MAX_TAG_LEN);
tag_release[MAX_TAG_LEN] = '\0';
}
}

// Get Backport tags and unknown tags.
int unknown_tag_entered = 0;
int show_database_hint = 0;
char *tok = strtok(tag, ",");
while (tok) {
trim_string(tok, " ", " ");
if (strcasecmp(tok, "Backport") == 0) {
if (backport) {
printf("Backport tag disabled.\n");
Expand All @@ -743,50 +737,58 @@ static struct scan *pkgrename(struct scan *scan)
if (strcasecmp(tag_release_group, tok) != 0)
printf("Cannot enter multiple release groups (\"%s\").\n", tok);
} else {
// Ugly hack to erase previously entered tags
// that are not yet in the database.
if (tag_release[0] && n_results == 0) {
tag_release[0] = '\0';
n_results = 1;
// Make sure existing releases reset when a new
// unknown tag was entered.
if (unknown_tag_entered == 0) {
unknown_tag_entered = 1;
if (n_results == 0)
tag_release[0] = '\0';
}

if (strwrd(tag_release, tok) == NULL) {
if (strwrd(tag_release, tok) == NULL) { // Ignore duplicates.
printf("Using \"%s\" as release. ", tok);
set_color(BRIGHT_YELLOW, stdout);
printf("%s", "<- MISSING FROM DATABASE\n");
set_color(RESET, stdout);
show_database_hint = 1;

if (tag_release[0]) {
size_t len = strlen(tag_release);
if (strchr(tag_release, ',')) {
// Insert tok in alphabetic order.
char buf[MAX_TAG_LEN] = "";
char *next_tag_start = tag_release;
while(1) {
char *next_tag_end = strchr(next_tag_start, ',');
if (next_tag_end == NULL)
next_tag_end = tag_release + strlen(tag_release);
memcpy(buf, next_tag_start, next_tag_end - next_tag_start);
buf[next_tag_end - next_tag_start] = '\0';
if (strcasecmp(buf, tok) > 0) {
memset(buf, 0, MAX_TAG_LEN);
strncpy(buf, tag_release, next_tag_start - tag_release);
strncat(buf, tok, MAX_TAG_LEN - 1 - strlen(buf));
if (next_tag_start != 0)
strcat(buf, ",");
// Add current tok in alphabetic order.
char buf[MAX_TAG_LEN] = "";
char *next_tag_start = tag_release;
while(1) {
char *next_tag_end = strchr(next_tag_start, ',');
if (next_tag_end == NULL)
next_tag_end = tag_release + strlen(tag_release);
memcpy(buf, next_tag_start, next_tag_end - next_tag_start);
buf[next_tag_end - next_tag_start] = '\0';

// Found correct order -> insert.
if (strcasecmp(buf, tok) > 0) {
memset(buf, 0, MAX_TAG_LEN);
strncpy(buf, tag_release, next_tag_start - tag_release);
strncat(buf, tok, MAX_TAG_LEN - 1 - strlen(buf));
if (next_tag_start != 0)
strcat(buf, ",");
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
strncat(buf, next_tag_start, MAX_TAG_LEN - 1 - strlen(buf));
strncat(buf, next_tag_start, MAX_TAG_LEN - 1 - strlen(buf));
#pragma GCC diagnostic pop
memcpy(tag_release, buf, MAX_TAG_LEN);
break;
}
next_tag_start = next_tag_end + 1;
memcpy(tag_release, buf, MAX_TAG_LEN);
break;
}

// Reached the end -> append.
if (*next_tag_end == '\0') {
size_t len = strlen(tag_release);
snprintf(tag_release + len, MAX_TAG_LEN - len, ",%s", tok);
break;
}
} else
snprintf(tag_release + len, MAX_TAG_LEN - len, ",%s", tok);

next_tag_start = next_tag_end + 1;
}
} else {
// First value; simply copy it.
strncpy(tag_release + strlen(tag_release), tok, MAX_TAG_LEN);
tag_release[MAX_TAG_LEN] = '\0';
}
Expand All @@ -799,7 +801,7 @@ static struct scan *pkgrename(struct scan *scan)

if (show_database_hint) {
set_color(BRIGHT_YELLOW, stdout);
puts("\nUse option --tags or --tagfile to add missing tags to the database.");
puts("\nUse command line option --tags or --tagfile to add missing tags to the database.");
set_color(RESET, stdout);
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkgrename.c/src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum long_only_options {
OPT_SET_TYPE,
OPT_TAGFILE,
OPT_TAGS,
OPT_TAG_SEPARATOR,
OPT_VERSION,
};

Expand Down Expand Up @@ -65,7 +66,7 @@ static struct option opts[] = {

void print_version(void)
{
printf("Version 1.08, build date: %s\n", __DATE__);
printf("Version 1.08a, build date: %s\n", __DATE__);
printf("Get the latest version at "
"\"%s\".\n", HOMEPAGE_LINK);
printf("Report bugs, request features, or add missing tags at "
Expand Down
Loading

0 comments on commit a56bf8a

Please sign in to comment.