aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2019-07-18 18:34:54 +0300
committerSergey Poznyakoff <gray@gnu.org>2019-07-18 18:34:54 +0300
commitf6657a46e2422d903d62565868f77c430a612a42 (patch)
treeb70b4f45003e067de55a8ac425792c98fd06035d /src
parent3a16bc5620b4c664248bc5b5c586ddd0103f7050 (diff)
downloadwydawca-f6657a46e2422d903d62565868f77c430a612a42.tar.gz
wydawca-f6657a46e2422d903d62565868f77c430a612a42.tar.bz2
Fix unnecessary code duplication
Use grecs_txtacc.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am1
-rw-r--r--src/builtin.c14
-rw-r--r--src/cmdline.opt20
-rw-r--r--src/directive.c8
-rw-r--r--src/meta.c28
-rw-r--r--src/report.c14
-rw-r--r--src/triplet.c8
-rw-r--r--src/txtacc.c173
-rw-r--r--src/verify.c6
-rw-r--r--src/wydawca.h13
10 files changed, 52 insertions, 233 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index b20d563..2ba575e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -53,7 +53,6 @@ wydawca_SOURCES=\
vtab.c\
null.c\
timer.c\
- txtacc.c\
report.c\
$(WATCHER_C)
diff --git a/src/builtin.c b/src/builtin.c
index 1b5a962..6acc0c1 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -149,7 +149,7 @@ parse_cmp_type(const char *pat, strcmp_fn * cmpfn, int *rf)
}
struct builtin_data_storage {
- struct txtacc *acc;
+ struct grecs_txtacc *acc;
char **wp;
};
@@ -164,7 +164,7 @@ builtin_lookup(struct dictionary *dict, void *handle, const char *req)
int i;
int rc;
size_t count = 0;
- struct txtacc *acc;
+ struct grecs_txtacc *acc;
int flags = 0;
strcmp_fn cmpfn = cmp_exact;
struct builtin_data_storage *bds;
@@ -175,7 +175,7 @@ builtin_lookup(struct dictionary *dict, void *handle, const char *req)
return 0;
}
- acc = txtacc_create();
+ acc = grecs_txtacc_create();
for (i = 0; i < dict->parmc; i++) {
char *pat = dict->parmv[i];
@@ -194,7 +194,7 @@ builtin_lookup(struct dictionary *dict, void *handle, const char *req)
size_t j;
for (j = 1; j <= ncol; j++) {
char *val = dict->parmv[i + j];
- txtacc_grow(acc, val, strlen(val) + 1);
+ grecs_txtacc_grow(acc, val, strlen(val) + 1);
}
count++;
}
@@ -206,7 +206,7 @@ builtin_lookup(struct dictionary *dict, void *handle, const char *req)
dict->ncol = ncol;
if (count == 0) {
- txtacc_free(acc);
+ grecs_txtacc_free(acc);
bds = NULL;
rc = 1;
} else {
@@ -217,7 +217,7 @@ builtin_lookup(struct dictionary *dict, void *handle, const char *req)
count *= ncol;
bds->wp = grecs_calloc(count, sizeof(bds->wp[0]));
bds->acc = acc;
- p = txtacc_finish(acc, 0);
+ p = grecs_txtacc_finish(acc, 0);
for (i = 0; i < count; i++) {
bds->wp[i] = p;
@@ -236,7 +236,7 @@ builtin_free_result(struct dictionary *dict, void *handle)
{
if (dict->storage) {
struct builtin_data_storage *bds = dict->storage;
- txtacc_free(bds->acc);
+ grecs_txtacc_free(bds->acc);
free(bds->wp);
free(bds);
dict->storage = NULL;
diff --git a/src/cmdline.opt b/src/cmdline.opt
index a0b1b37..c553e2e 100644
--- a/src/cmdline.opt
+++ b/src/cmdline.opt
@@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along
with wydawca. If not, see <http://www.gnu.org/licenses/>. */
-static struct txtacc *pp_cmd_acc;
+static struct grecs_txtacc *pp_cmd_acc;
static struct grecs_list *source_list;
static struct grecs_list *tag_list;
@@ -152,14 +152,14 @@ BEGIN
char *p;
if (!pp_cmd_acc)
- pp_cmd_acc = txtacc_create ();
- txtacc_grow(pp_cmd_acc, " \"-D", 4);
+ pp_cmd_acc = grecs_txtacc_create ();
+ grecs_txtacc_grow(pp_cmd_acc, " \"-D", 4);
for (p = optarg; *p; p++) {
if (*p == '\\' || *p == '"')
- txtacc_1grow(pp_cmd_acc, '\\');
- txtacc_1grow(pp_cmd_acc, *p);
+ grecs_txtacc_grow_char(pp_cmd_acc, '\\');
+ grecs_txtacc_grow_char(pp_cmd_acc, *p);
}
- txtacc_1grow(pp_cmd_acc, '"');
+ grecs_txtacc_grow_char(pp_cmd_acc, '"');
END
OPTION(preprocessor,,COMMAND,
@@ -218,10 +218,10 @@ parse_options(int argc, char *argv[])
if (pp_cmd_acc && grecs_preprocessor) {
char *cmd;
size_t len = strlen(grecs_preprocessor);
- txtacc_1grow(pp_cmd_acc, 0);
- txtacc_grow(pp_cmd_acc, grecs_preprocessor, len + 1);
- cmd = txtacc_finish(pp_cmd_acc, 1);
- txtacc_free(pp_cmd_acc);
+ grecs_txtacc_grow_char(pp_cmd_acc, 0);
+ grecs_txtacc_grow(pp_cmd_acc, grecs_preprocessor, len + 1);
+ cmd = grecs_txtacc_finish(pp_cmd_acc, 1);
+ grecs_txtacc_free(pp_cmd_acc);
memmove(cmd + len + 1, cmd, strlen(cmd) + 1);
memcpy(cmd, grecs_preprocessor, len);
cmd[len] = ' ';
diff --git a/src/directive.c b/src/directive.c
index 4a622c0..5dd1724 100644
--- a/src/directive.c
+++ b/src/directive.c
@@ -488,10 +488,10 @@ run_check_script(const char *script, struct wy_triplet *trp,
while (grecs_getline(&buf, &size, fp) > 0) {
size_t len = strlen(buf);
wy_debug(3, (_("read: %s"), buf));
- txtacc_grow(trp->acc, buf, len);
+ grecs_txtacc_grow(trp->acc, buf, len);
total += size;
}
- txtacc_1grow(trp->acc, 0);
+ grecs_txtacc_grow_char(trp->acc, 0);
wy_debug(3, (_("bytes read: %lu"), (unsigned long)total));
fclose(fp);
@@ -500,7 +500,9 @@ run_check_script(const char *script, struct wy_triplet *trp,
signal(SIGCHLD, oldsig);
if (total)
- trp->check_diag = txtacc_finish(trp->acc, 0);
+ trp->check_diag = grecs_txtacc_finish(trp->acc, 0);
+ else
+ grecs_txtacc_clear(trp->acc);
trp->check_result = status;
if (WIFEXITED(status)) {
diff --git a/src/meta.c b/src/meta.c
index 75bd414..734be50 100644
--- a/src/meta.c
+++ b/src/meta.c
@@ -54,23 +54,23 @@ meta_expand_string(const char *string, struct wy_metadef *def, void *data,
{
const char *p, *s;
char *res;
- struct txtacc *acc;
+ struct grecs_txtacc *acc;
if (!string)
return NULL;
- acc = txtacc_create();
+ acc = grecs_txtacc_create();
for (p = string; *p;) {
char *e;
size_t len = strcspn(p, "$");
- txtacc_grow(acc, p, len);
+ grecs_txtacc_grow(acc, p, len);
p += len;
if (*p == '$') {
switch (*++p) {
case '$':
- txtacc_grow(acc, p, 1);
+ grecs_txtacc_grow(acc, p, 1);
p++;
break;
@@ -93,13 +93,15 @@ meta_expand_string(const char *string, struct wy_metadef *def, void *data,
s,
&newval,
&len);
- txtacc_grow(acc, newval, len);
+ grecs_txtacc_grow(acc, newval,
+ len);
free(newval);
} else
- txtacc_grow(acc, s, strlen(s));
+ grecs_txtacc_grow(acc, s,
+ strlen(s));
p = e + 1;
} else {
- txtacc_grow(acc, p - 1, 2);
+ grecs_txtacc_grow(acc, p - 1, 2);
p++;
}
break;
@@ -120,18 +122,18 @@ meta_expand_string(const char *string, struct wy_metadef *def, void *data,
dictionary_quote_string(dict,
handle, s,
&newval, &len);
- txtacc_grow(acc, newval, len);
+ grecs_txtacc_grow(acc, newval, len);
free(newval);
} else
- txtacc_grow(acc, s, len);
+ grecs_txtacc_grow(acc, s, len);
p++;
}
} else
- txtacc_grow(acc, p, 1);
+ grecs_txtacc_grow(acc, p, 1);
}
- txtacc_1grow(acc, 0);
- res = txtacc_finish(acc, 1);
- txtacc_free(acc);
+ grecs_txtacc_grow_char(acc, 0);
+ res = grecs_txtacc_finish(acc, 1);
+ grecs_txtacc_free(acc);
return res;
}
diff --git a/src/report.c b/src/report.c
index e1a108d..c24fe33 100644
--- a/src/report.c
+++ b/src/report.c
@@ -17,16 +17,16 @@
#include "wydawca.h"
-static struct txtacc *report_acc;
+static struct grecs_txtacc *report_acc;
char *report_string;
void
report_init()
{
if (!report_acc)
- report_acc = txtacc_create();
+ report_acc = grecs_txtacc_create();
else
- txtacc_free_string(report_acc, report_string);
+ grecs_txtacc_free_string(report_acc, report_string);
}
void
@@ -40,8 +40,8 @@ report_add(const char *fmt, ...)
grecs_vasprintf(&str, &size, fmt, ap);
va_end(ap);
if (str) {
- txtacc_grow(report_acc, str, strlen(str));
- txtacc_1grow(report_acc, '\n');
+ grecs_txtacc_grow_string(report_acc, str);
+ grecs_txtacc_grow_char(report_acc, '\n');
}
free(str);
}
@@ -49,6 +49,6 @@ report_add(const char *fmt, ...)
void
report_finish()
{
- txtacc_1grow(report_acc, 0);
- report_string = txtacc_finish(report_acc, 0);
+ grecs_txtacc_grow_char(report_acc, 0);
+ report_string = grecs_txtacc_finish(report_acc, 0);
}
diff --git a/src/triplet.c b/src/triplet.c
index 0ab597d..067bc01 100644
--- a/src/triplet.c
+++ b/src/triplet.c
@@ -169,7 +169,7 @@ hash_triplet_free(void *data)
free(tp->directive);
free(tp->blurb);
free(tp->tmp);
- txtacc_free(tp->acc);
+ grecs_txtacc_free(tp->acc);
/* Free uploader and admin lists */
wy_userlist_free(tp->uploader_list);
@@ -182,8 +182,8 @@ char *
triplet_strdup(struct wy_triplet *tp, const char *str)
{
size_t len = strlen(str);
- txtacc_grow(tp->acc, str, len + 1);
- return txtacc_finish(tp->acc, 0);
+ grecs_txtacc_grow(tp->acc, str, len + 1);
+ return grecs_txtacc_finish(tp->acc, 0);
}
/* Register a file in the triplet table */
@@ -216,7 +216,7 @@ register_file(struct file_info *finfo, const struct spool *spool)
free(key.name);
if (install) {
ret->spool = spool;
- ret->acc = txtacc_create();
+ ret->acc = grecs_txtacc_create();
}
ret->file[finfo->type] = *finfo;
triplet_list_unlink(ret);
diff --git a/src/txtacc.c b/src/txtacc.c
deleted file mode 100644
index fac030c..0000000
--- a/src/txtacc.c
+++ /dev/null
@@ -1,173 +0,0 @@
-/* wydawca - automatic release submission daemon
- Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013, 2017, 2019 Sergey
- Poznyakoff
-
- Wydawca is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 3 of the License, or (at your
- option) any later version.
-
- Wydawca is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with wydawca. If not, see <http://www.gnu.org/licenses/>. */
-
-#include "wydawca.h"
-
-struct txtacc_entry {
- char *buf; /* Text buffer */
- size_t size; /* Buffer size */
- size_t len; /* Actual number of bytes in buffer */
-};
-#define TXTACC_BUFSIZE 1024
-#define txtacc_entry_freesize(e) ((e)->size - (e)->len)
-
-struct txtacc {
- struct grecs_list *cur; /* Current build list */
- struct grecs_list *mem; /* List of already allocated elements */
-};
-
-static struct txtacc_entry *
-txtacc_alloc_entry(struct grecs_list *list, size_t size)
-{
- struct txtacc_entry *p = grecs_malloc(sizeof(*p));
- p->buf = grecs_malloc(size);
- p->size = size;
- p->len = 0;
- grecs_list_append(list, p);
- return p;
-}
-
-static struct txtacc_entry *
-txtacc_cur_entry(struct txtacc *acc)
-{
- struct txtacc_entry *ent;
-
- if (grecs_list_size(acc->cur) == 0)
- return txtacc_alloc_entry(acc->cur, TXTACC_BUFSIZE);
- ent = acc->cur->tail->data;
- if (txtacc_entry_freesize(ent) == 0)
- ent = txtacc_alloc_entry(acc->cur, TXTACC_BUFSIZE);
- return ent;
-}
-
-static void
-txtacc_entry_append(struct txtacc_entry *ent, const char *p, size_t size)
-{
- memcpy(ent->buf + ent->len, p, size);
- ent->len += size;
-}
-
-static void
-txtacc_entry_tailor(struct txtacc_entry *ent)
-{
- if (ent->size > ent->len) {
- char *p = realloc(ent->buf, ent->len);
- if (!p)
- return;
- ent->buf = p;
- ent->size = ent->len;
- }
-}
-
-static void
-txtacc_entry_free(void *p)
-{
- if (p) {
- struct txtacc_entry *ent = p;
- free(ent->buf);
- free(ent);
- }
-}
-
-struct txtacc *
-txtacc_create()
-{
- struct txtacc *acc = grecs_malloc(sizeof(*acc));
- acc->cur = grecs_list_create();
- acc->cur->free_entry = txtacc_entry_free;
- acc->mem = grecs_list_create();
- acc->mem->free_entry = txtacc_entry_free;
- return acc;
-}
-
-void
-txtacc_free(struct txtacc *acc)
-{
- grecs_list_free(acc->cur);
- grecs_list_free(acc->mem);
- free(acc);
-}
-
-void
-txtacc_grow(struct txtacc *acc, const char *buf, size_t size)
-{
- while (size) {
- struct txtacc_entry *ent = txtacc_cur_entry(acc);
- size_t rest = txtacc_entry_freesize(ent);
- if (rest > size)
- rest = size;
- txtacc_entry_append(ent, buf, rest);
- buf += rest;
- size -= rest;
- }
-}
-
-char *
-txtacc_finish(struct txtacc *acc, int steal)
-{
- struct grecs_list_entry *ep;
- struct txtacc_entry *txtent;
- size_t size;
- char *p;
-
- switch (grecs_list_size(acc->cur)) {
- case 0:
- return NULL;
-
- case 1:
- txtent = acc->cur->head->data;
- acc->cur->head->data = NULL;
- txtacc_entry_tailor(txtent);
- grecs_list_append(acc->mem, txtent);
- break;
-
- default:
- size = 0;
- for (ep = acc->cur->head; ep; ep = ep->next) {
- txtent = ep->data;
- size += txtent->len;
- }
-
- txtent = txtacc_alloc_entry(acc->mem, size);
- for (ep = acc->cur->head; ep; ep = ep->next) {
- struct txtacc_entry *tp = ep->data;
- txtacc_entry_append(txtent, tp->buf, tp->len);
- }
- }
-
- grecs_list_clear(acc->cur);
- p = txtent->buf;
- if (steal) {
- grecs_list_remove_tail(acc->mem);
- free(txtent);
- }
- return p;
-}
-
-void
-txtacc_free_string(struct txtacc *acc, char *str)
-{
- struct grecs_list_entry *ep;
- for (ep = acc->mem->head; ep; ep = ep->next) {
- struct txtacc_entry *tp = ep->data;
- if (tp->buf == str) {
- grecs_list_remove_entry(acc->mem, ep);
- free(tp->buf);
- return;
- }
- }
-}
diff --git a/src/verify.c b/src/verify.c
index 0963dae..fa7ad5b 100644
--- a/src/verify.c
+++ b/src/verify.c
@@ -160,9 +160,9 @@ fill_project_name(struct wy_triplet *trp)
trp->file[file_directive].name);
return 1;
}
- txtacc_grow(trp->acc, trp->relative_dir, len);
- txtacc_1grow(trp->acc, 0);
- trp->project = txtacc_finish(trp->acc, 0);
+ grecs_txtacc_grow(trp->acc, trp->relative_dir, len);
+ grecs_txtacc_grow_char(trp->acc, 0);
+ trp->project = grecs_txtacc_finish(trp->acc, 0);
} else
trp->project = trp->relative_dir;
diff --git a/src/wydawca.h b/src/wydawca.h
index 0239093..b448904 100644
--- a/src/wydawca.h
+++ b/src/wydawca.h
@@ -165,7 +165,7 @@ struct wy_triplet {
here */
char *tmp; /* Temporary storage */
size_t tmpsize; /* Size of memory allocated in tmp */
- struct txtacc *acc; /* Text accumulator for string allocation */
+ struct grecs_txtacc *acc; /* Text accumulator for string allocation */
/* Triplets are joined in two doubly-linked lists:
1) a cronological list, with prev pointing to a triplet older
than this one, and next pointing to a triplet newer than it: */
@@ -574,17 +574,6 @@ int push_dir(const char *dirname);
int pop_dir(void);
char *getcwd_alloc(void);
-struct txtacc *txtacc_create(void);
-void txtacc_free(struct txtacc *acc);
-void txtacc_free_string(struct txtacc *acc, char *str);
-void txtacc_grow(struct txtacc *acc, const char *buf, size_t size);
-#define txtacc_1grow(acc, c) do { \
- char __ch = c; \
- txtacc_grow (acc, &__ch, 1); \
- } while (0)
-
-char *txtacc_finish(struct txtacc *acc, int steal);
-
#ifdef WITH_INOTIFY
int watcher_init(void);
int watcher_run(int);

Return to:

Send suggestions and report system problems to the System administrator.