| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2007, 2008, 2012, 2015 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <argp.h> |
| 20 |
#include <assert.h> |
| 21 |
#include <locale.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include "misc.h" |
| 25 |
#include "msg.h" |
| 26 |
#include "i18n.h" |
| 27 |
#include "ifile.h" |
| 28 |
#include "para_bundle.h" |
| 29 |
|
| 30 |
enum { |
| 31 |
OPT_SILENT = 256, |
| 32 |
OPT_MMAP |
| 33 |
}; |
| 34 |
|
| 35 |
const char * argp_program_version = "join-dctrl (dctrl-tools) " VERSION; |
| 36 |
const char * argp_program_bug_address = MAINTAINER; |
| 37 |
|
| 38 |
static struct argp_option options[] = { |
| 39 |
{ "1st-join-field", '1', N_("FIELD"), 0, N_("Specify the join field to use for the first file"), 0 }, |
| 40 |
{ "2nd-join-field", '2', N_("FIELD"), 0, N_("Specify the join field to use for the second file"), 0 }, |
| 41 |
{ "join-field", 'j', N_("FIELD"), 0, N_("Specify the common join field"), 0 }, |
| 42 |
{ "unpairable-from", 'a', N_("FIELDNO"), 0, N_("Print unpairable records from the indicated file (either 1 or 2)"), 0 }, |
| 43 |
{ "output-fields", 'o', N_("FIELDSPEC"),0, N_("Specify the format of the output file"), 0 }, |
| 44 |
{ "copying", 'C', 0, 0, N_("Print out the copyright license."), 0 }, |
| 45 |
{ "errorlevel", 'l', N_("LEVEL"), 0, N_("Set debugging level to LEVEL."), 0 }, |
| 46 |
{ "mmap", OPT_MMAP, 0, 0, N_("Attempt mmapping input files"), 0 }, |
| 47 |
{ 0, 0, 0, 0, 0, 0 } |
| 48 |
}; |
| 49 |
|
| 50 |
#define MAX_FNAMES 2 |
| 51 |
|
| 52 |
struct arguments { |
| 53 |
size_t unpairables; /* Print unpairable paragraphs from ... |
| 54 |
0 nowhere, |
| 55 |
1 the first file |
| 56 |
2 the second file |
| 57 |
*/ |
| 58 |
struct field_attr *join_field[MAX_FNAMES]; |
| 59 |
size_t num_fnames; |
| 60 |
struct ifile fname[MAX_FNAMES]; |
| 61 |
size_t num_show_fields; |
| 62 |
struct show_field { |
| 63 |
size_t file_inx; /* indexes join_field, can be -1 to indicate |
| 64 |
common (ignoring nulls) join field |
| 65 |
*/ |
| 66 |
struct field_attr *field; // null if file_inx is -1 |
| 67 |
char *showname; |
| 68 |
} show_fields[MAX_FIELDS]; |
| 69 |
}; |
| 70 |
|
| 71 |
static error_t parse_opt (int key, char * arg, struct argp_state * state) |
| 72 |
{ |
| 73 |
struct arguments * args = state->input; |
| 74 |
switch (key) { |
| 75 |
case 'a': |
| 76 |
if (arg[0] == '1' && arg[1] == '\0') { |
| 77 |
args->unpairables = 1; |
| 78 |
} else if (arg[1] == '2' && arg[1] == '\0') { |
| 79 |
args->unpairables = 2; |
| 80 |
} else { |
| 81 |
message(L_FATAL, 0, _("malformed argument to '-a'")); |
| 82 |
fail(); |
| 83 |
} |
| 84 |
break; |
| 85 |
case '1': case '2': case 'j': { |
| 86 |
static const char the_other_key[] = { '2', '1' }; |
| 87 |
static const char *errmsg[] = { |
| 88 |
N_("the join field of the first file has already been specified"), |
| 89 |
N_("the join field of the second file has already been specified") |
| 90 |
}; |
| 91 |
for (size_t i = 0; i < 2; i++) { |
| 92 |
if (key == the_other_key[i]) continue; |
| 93 |
if (args->join_field[0] != NULL) { |
| 94 |
message(L_FATAL, 0, "%s", gettext(errmsg[i])); |
| 95 |
fail(); |
| 96 |
} |
| 97 |
} |
| 98 |
for (size_t i = 0; i < 2; i++) { |
| 99 |
if (key == the_other_key[i]) continue; |
| 100 |
char *str = strdup(arg); |
| 101 |
if (str == NULL) fatal_enomem(0); |
| 102 |
args->join_field[i] = fieldtrie_insert(str); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
break; |
| 107 |
case 'o': { |
| 108 |
char * carg = strdup(arg); |
| 109 |
if (carg == 0) fatal_enomem(0); |
| 110 |
for (char * s = strtok(carg, ","); s != 0; s = strtok(0, ",")){ |
| 111 |
if (args->num_show_fields >= MAX_FIELDS) { |
| 112 |
message(L_FATAL, 0, |
| 113 |
_("too many output fields")); |
| 114 |
fail(); |
| 115 |
} |
| 116 |
struct show_field *of = |
| 117 |
&args->show_fields[args->num_show_fields++]; |
| 118 |
|
| 119 |
if (s[0] == '0' && s[1] == '\0') { |
| 120 |
of->file_inx = -1; |
| 121 |
of->field = NULL; |
| 122 |
of->showname = NULL; |
| 123 |
continue; |
| 124 |
} |
| 125 |
char * fld = strchr(s, '.'); |
| 126 |
if (fld == NULL) { |
| 127 |
message(L_FATAL, 0, |
| 128 |
_("missing '.' in output " |
| 129 |
"field specification")); |
| 130 |
fail(); |
| 131 |
} |
| 132 |
*fld = '\0'; |
| 133 |
++fld; |
| 134 |
char * shw = strchr(fld, ':'); |
| 135 |
if (shw != NULL) { |
| 136 |
*shw = '\0'; |
| 137 |
++shw; |
| 138 |
} |
| 139 |
int file_inx = 0; |
| 140 |
if (s[0] == '1' && s[1] == '\0') { |
| 141 |
file_inx = 0; |
| 142 |
} else if (s[0] == '2' && s[1] == '\0') { |
| 143 |
file_inx = 1; |
| 144 |
} else { |
| 145 |
message(L_FATAL, 0, |
| 146 |
_("expected either '1.' or " |
| 147 |
"'2.' at the start of the " |
| 148 |
"field specification")); |
| 149 |
fail(); |
| 150 |
} |
| 151 |
of->file_inx = file_inx; |
| 152 |
of->field = fieldtrie_insert(fld); |
| 153 |
of->showname = shw == NULL ? NULL : strdup(shw); |
| 154 |
if (shw != NULL && of->showname == NULL) { |
| 155 |
fatal_enomem(0); |
| 156 |
} |
| 157 |
} |
| 158 |
free(carg); |
| 159 |
} |
| 160 |
break; |
| 161 |
case 'C': |
| 162 |
if (!to_stdout (COPYING)) fail(); |
| 163 |
exit(0); |
| 164 |
case 'l': { |
| 165 |
int ll = str2loglevel(arg); |
| 166 |
if (ll < 0) |
| 167 |
{ |
| 168 |
message(L_FATAL, 0, _("no such log level '%s'"), arg); |
| 169 |
fail(); |
| 170 |
} |
| 171 |
set_loglevel(ll); |
| 172 |
debug_message("parse_opt: l", 0); |
| 173 |
} |
| 174 |
break; |
| 175 |
case ARGP_KEY_ARG: |
| 176 |
debug_message("parse_opt: argument", 0); |
| 177 |
{ |
| 178 |
char const * s; |
| 179 |
if (args->num_fnames >= MAX_FNAMES) { |
| 180 |
message(L_FATAL, 0, _("too many file names")); |
| 181 |
fail(); |
| 182 |
} |
| 183 |
s = strdup(arg); |
| 184 |
if (s == 0) fatal_enomem(0); |
| 185 |
args->fname[args->num_fnames++] = |
| 186 |
(struct ifile){ .mode = m_read, .s = s }; |
| 187 |
} |
| 188 |
break; |
| 189 |
case OPT_MMAP: |
| 190 |
debug_message("parse_opt: mmap", 0); |
| 191 |
/* ignore */ |
| 192 |
break; |
| 193 |
case ARGP_KEY_END: |
| 194 |
case ARGP_KEY_ARGS: case ARGP_KEY_INIT: case ARGP_KEY_SUCCESS: |
| 195 |
case ARGP_KEY_ERROR: case ARGP_KEY_FINI: case ARGP_KEY_NO_ARGS: |
| 196 |
debug_message("parse_opt: ignored", 0); |
| 197 |
break; |
| 198 |
default: |
| 199 |
return ARGP_ERR_UNKNOWN; |
| 200 |
} |
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
/* Print the paragraph configuration in "para" (just just_this, if in |
| 205 |
* range). */ |
| 206 |
void print_para_config(struct arguments *args, para_t para[], size_t just_this) |
| 207 |
{ |
| 208 |
if (args->num_show_fields == 0) { |
| 209 |
for (size_t i = 0; i < args->num_fnames; i++) { |
| 210 |
size_t n = para_num_fields(¶[i]); |
| 211 |
for (size_t j = 0; j < n; j++) { |
| 212 |
for (struct field_datum * fd = |
| 213 |
find_field(¶[i], j).first; |
| 214 |
fd != NULL; fd = fd->next) { |
| 215 |
struct fsaf_read_rv fld = |
| 216 |
fsaf_read(para[i].common->fp, |
| 217 |
fd->name_start, |
| 218 |
fd->end - |
| 219 |
fd->name_start); |
| 220 |
fwrite(fld.b, 1, fld.len, stdout); |
| 221 |
putchar('\n'); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
putchar('\n'); |
| 226 |
return; |
| 227 |
} |
| 228 |
for (size_t i = 0; i < args->num_show_fields; i++) { |
| 229 |
struct show_field *sf = &args->show_fields[i]; |
| 230 |
struct fsaf_read_rv body = { .b = "", .len = 0 }; |
| 231 |
const char *showname = NULL; |
| 232 |
if (sf->file_inx == (size_t)-1) { |
| 233 |
for (size_t j = 0; |
| 234 |
body.len == 0 && j < args->num_fnames; |
| 235 |
j++) { |
| 236 |
if (just_this < args->num_fnames && |
| 237 |
j != just_this) { |
| 238 |
continue; |
| 239 |
} |
| 240 |
body = get_field(¶[j], |
| 241 |
args->join_field[j]->inx, |
| 242 |
-1); |
| 243 |
|
| 244 |
showname = args->join_field[j]->name; |
| 245 |
} |
| 246 |
} else { |
| 247 |
if (just_this < args->num_fnames && |
| 248 |
sf->file_inx != just_this) { |
| 249 |
continue; |
| 250 |
} |
| 251 |
body = get_field(¶[sf->file_inx], |
| 252 |
sf->field->inx, |
| 253 |
-1); |
| 254 |
showname = sf->showname == NULL |
| 255 |
? sf->field->name |
| 256 |
: sf->showname; |
| 257 |
} |
| 258 |
fputs(showname, stdout); |
| 259 |
fputs(": ", stdout); |
| 260 |
fwrite(body.b, 1, body.len, stdout); |
| 261 |
putchar('\n'); |
| 262 |
} |
| 263 |
putchar('\n'); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
static char progdoc [] = N_("join-dctrl -- join two Debian control files"); |
| 268 |
|
| 269 |
static struct argp argp = { options, parse_opt, 0, progdoc, 0, 0, 0 }; |
| 270 |
|
| 271 |
int main(int argc, char * argv[]) |
| 272 |
{ |
| 273 |
setlocale(LC_ALL, ""); |
| 274 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 275 |
textdomain(PACKAGE); |
| 276 |
|
| 277 |
fieldtrie_init(); |
| 278 |
|
| 279 |
static struct arguments args; |
| 280 |
|
| 281 |
//keys_init(&args.keys); |
| 282 |
msg_set_progname(argv[0]); |
| 283 |
argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, &args); |
| 284 |
|
| 285 |
if (args.num_fnames != 2) { |
| 286 |
message(L_FATAL, 0, _("need exactly two input files")); |
| 287 |
fail(); |
| 288 |
} |
| 289 |
|
| 290 |
int fd[MAX_FNAMES]; |
| 291 |
FSAF *fp[MAX_FNAMES]; |
| 292 |
para_parser_t pp[MAX_FNAMES]; |
| 293 |
para_t para[MAX_FNAMES]; |
| 294 |
|
| 295 |
for (size_t i = 0; i < args.num_fnames; i++) { |
| 296 |
fd[i] = open_ifile(args.fname[i]); |
| 297 |
if (fd[i] == -1) fail(); |
| 298 |
if (!chk_ifile(args.fname[i], fd[i])) fail(); |
| 299 |
fp[i] = fsaf_fdopen(fd[i], args.fname[i].s); |
| 300 |
|
| 301 |
para_parser_init(&pp[i], fp[i], true, false, |
| 302 |
args.num_show_fields == 0); |
| 303 |
para_init(&pp[i], ¶[i]); |
| 304 |
para_parse_next(¶[i]); |
| 305 |
} |
| 306 |
|
| 307 |
for (size_t i = 0; i < args.num_fnames; i++) { |
| 308 |
for (size_t j = i+1; j < args.num_fnames; j++) { |
| 309 |
if (fd[i] == fd[j]) { |
| 310 |
message(L_FATAL, 0, |
| 311 |
_("cannot join a stream with itself")); |
| 312 |
fail(); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
while (true) { |
| 318 |
struct fsaf_read_rv a; |
| 319 |
again: |
| 320 |
a = get_field(¶[0], args.join_field[0]->inx, -1); |
| 321 |
_Bool did_work = 0; |
| 322 |
for (size_t i = 1; i < args.num_fnames; i++) { |
| 323 |
if (para_eof(&pp[i]) || para_eof(&pp[i-1])) continue; |
| 324 |
did_work = 1; |
| 325 |
struct fsaf_read_rv b = |
| 326 |
get_field(¶[i], |
| 327 |
args.join_field[i]->inx, -1); |
| 328 |
#ifdef INCLUDE_DEBUG_MSGS |
| 329 |
if (do_msg(L_DEBUG)) { |
| 330 |
fprintf(stderr, "join-cmp %.*s %.*s => ", |
| 331 |
(int)a.len, a.b, (int)b.len, b.b); |
| 332 |
} |
| 333 |
#endif |
| 334 |
size_t len = a.len < b.len ? a.len : b.len; |
| 335 |
int r = strncmp(a.b,b.b,len); |
| 336 |
if (r == 0) { |
| 337 |
if (a.len < b.len) r = -1; |
| 338 |
if (a.len > b.len) r = 1; |
| 339 |
} |
| 340 |
#ifdef INCLUDE_DEBUG_MSGS |
| 341 |
if (do_msg(L_DEBUG)) { |
| 342 |
fprintf(stderr, "%d\n", r); |
| 343 |
} |
| 344 |
#endif |
| 345 |
if (r == 0) { |
| 346 |
a = b; |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
size_t k; |
| 351 |
if (r < 0) { |
| 352 |
k = i - 1; |
| 353 |
} else if (r > 0) { |
| 354 |
k = i; |
| 355 |
} |
| 356 |
|
| 357 |
if (args.unpairables == k + 1) { |
| 358 |
print_para_config(&args, para, |
| 359 |
args.unpairables-1); |
| 360 |
} |
| 361 |
|
| 362 |
para_parse_next(¶[k]); |
| 363 |
goto again; |
| 364 |
} |
| 365 |
if (!did_work) goto done; |
| 366 |
#ifdef INCLUDE_DEBUG_MSGS |
| 367 |
if (do_msg(L_DEBUG)) { |
| 368 |
fprintf(stderr, "join-show!\n"); |
| 369 |
} |
| 370 |
#endif |
| 371 |
/* now all paras should be in a join configuration; |
| 372 |
let's print out the result */ |
| 373 |
print_para_config(&args, para, (size_t)(-1)); |
| 374 |
para_parse_next(¶[0]); |
| 375 |
} |
| 376 |
done: |
| 377 |
return errors_reported() ? 2 : 0; |
| 378 |
} |
| 379 |
|
| 380 |
#if MAX_FNAMES != 2 |
| 381 |
// for example, there are command line options for handling only two files |
| 382 |
#error "it's not enough to just edit the #define of MAX_FNAMES :)" |
| 383 |
#endif |