| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 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 <locale.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include "misc.h" |
| 23 |
#include "msg.h" |
| 24 |
#include "i18n.h" |
| 25 |
#include "ifile.h" |
| 26 |
#include "para_bundle.h" |
| 27 |
#include "sorter.h" |
| 28 |
|
| 29 |
enum { |
| 30 |
OPT_SILENT = 256, |
| 31 |
OPT_MMAP |
| 32 |
}; |
| 33 |
|
| 34 |
const char * argp_program_version = "sort-dctrl (dctrl-tools) " VERSION; |
| 35 |
const char * argp_program_bug_address = MAINTAINER; |
| 36 |
|
| 37 |
static struct argp_option options[] = { |
| 38 |
{ "copying", 'C', 0, 0, N_("Print out the copyright license."), 0 }, |
| 39 |
{ "errorlevel", 'l', N_("LEVEL"), 0, N_("Set debugging level to LEVEL."), 0 }, |
| 40 |
{ "key-spec", 'k', N_("KEYSPEC"), 0, N_("Specify sort keys."), 0 }, |
| 41 |
{ "mmap", OPT_MMAP, 0, 0, N_("Attempt mmapping input files"), 0 }, |
| 42 |
{ 0, 0, 0, 0, 0, 0 } |
| 43 |
}; |
| 44 |
|
| 45 |
#define MAX_FNAMES 4096 |
| 46 |
|
| 47 |
struct arguments { |
| 48 |
/* Sort key specifications */ |
| 49 |
keys_t keys; |
| 50 |
/* Number of file names seen. */ |
| 51 |
size_t num_fnames; |
| 52 |
/* File names seen on the command line. */ |
| 53 |
struct ifile fname[MAX_FNAMES]; |
| 54 |
}; |
| 55 |
|
| 56 |
static error_t parse_opt (int key, char * arg, struct argp_state * state) |
| 57 |
{ |
| 58 |
struct arguments * args = state->input; |
| 59 |
switch (key) { |
| 60 |
case 'C': |
| 61 |
if (!to_stdout (COPYING)) fail(); |
| 62 |
exit(0); |
| 63 |
case 'k': |
| 64 |
{ |
| 65 |
char *carg = strdup(arg); |
| 66 |
if (carg == 0) fatal_enomem(0); |
| 67 |
for (char *s = strtok(carg, ","); s != 0; s = strtok(0, ",")) { |
| 68 |
char *flags = strrchr(s, ':'); |
| 69 |
if (flags == 0) { |
| 70 |
flags = ""; |
| 71 |
} else { |
| 72 |
*(flags++) = '\0'; |
| 73 |
} |
| 74 |
struct key key; |
| 75 |
key.field_inx = fieldtrie_insert(s)->inx; |
| 76 |
key.type = FT_STRING; |
| 77 |
key.reverse = false; |
| 78 |
for (char *p = flags; *p != '\0'; p++) { |
| 79 |
switch (*p) { |
| 80 |
case 'r': |
| 81 |
key.reverse = true; |
| 82 |
break; |
| 83 |
case 'v': case 'n': |
| 84 |
key.type = FT_VERSION; |
| 85 |
break; |
| 86 |
default: |
| 87 |
message(L_FATAL, 0, |
| 88 |
_("invalid key flag")); |
| 89 |
fail(); |
| 90 |
} |
| 91 |
} |
| 92 |
debug("parse_opt: append key %d %d %d", |
| 93 |
key.field_inx, key.type, key.reverse); |
| 94 |
keys_append(&args->keys, key); |
| 95 |
} |
| 96 |
debug_message("parse_opt: k", 0); |
| 97 |
break; |
| 98 |
} |
| 99 |
case 'l': |
| 100 |
{ |
| 101 |
int ll = str2loglevel(arg); |
| 102 |
if (ll < 0) |
| 103 |
{ |
| 104 |
message(L_FATAL, 0, _("no such log level '%s'"), arg); |
| 105 |
fail(); |
| 106 |
} |
| 107 |
set_loglevel(ll); |
| 108 |
debug_message("parse_opt: l", 0); |
| 109 |
} |
| 110 |
break; |
| 111 |
case OPT_MMAP: |
| 112 |
debug_message("parse_opt: mmap", 0); |
| 113 |
/* ignore */ |
| 114 |
break; |
| 115 |
case ARGP_KEY_ARG: |
| 116 |
debug_message("parse_opt: argument", 0); |
| 117 |
{ |
| 118 |
char const * s; |
| 119 |
if (args->num_fnames >= MAX_FNAMES) { |
| 120 |
message(L_FATAL, 0, _("too many file names")); |
| 121 |
fail(); |
| 122 |
} |
| 123 |
s = strdup(arg); |
| 124 |
if (s == 0) fatal_enomem(0); |
| 125 |
args->fname[args->num_fnames++] = |
| 126 |
(struct ifile){ .mode = m_read, .s = s }; |
| 127 |
} |
| 128 |
break; |
| 129 |
case ARGP_KEY_END: |
| 130 |
case ARGP_KEY_ARGS: case ARGP_KEY_INIT: case ARGP_KEY_SUCCESS: |
| 131 |
case ARGP_KEY_ERROR: case ARGP_KEY_FINI: case ARGP_KEY_NO_ARGS: |
| 132 |
debug_message("parse_opt: ignored", 0); |
| 133 |
break; |
| 134 |
default: |
| 135 |
return ARGP_ERR_UNKNOWN; |
| 136 |
} |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
static char progdoc [] = N_("sort-dctrl -- sort Debian control files"); |
| 141 |
|
| 142 |
static struct argp argp = { options, parse_opt, 0, progdoc, 0, 0, 0 }; |
| 143 |
|
| 144 |
int main(int argc, char * argv[]) |
| 145 |
{ |
| 146 |
setlocale(LC_ALL, ""); |
| 147 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 148 |
textdomain(PACKAGE); |
| 149 |
|
| 150 |
fieldtrie_init(); |
| 151 |
|
| 152 |
static struct arguments args; |
| 153 |
keys_init(&args.keys); |
| 154 |
msg_set_progname(argv[0]); |
| 155 |
argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, &args); |
| 156 |
|
| 157 |
debug("number of keys: %zu", args.keys.nks); |
| 158 |
|
| 159 |
if (args.keys.nks == 0) { |
| 160 |
size_t inx = fieldtrie_insert("Package")->inx; |
| 161 |
keys_append(&args.keys, (struct key){ .field_inx = inx, |
| 162 |
.type = FT_STRING, |
| 163 |
.reverse = false }); |
| 164 |
} |
| 165 |
|
| 166 |
if (args.num_fnames == 0) { |
| 167 |
args.fname[args.num_fnames++] = (struct ifile){ |
| 168 |
.mode = m_read, |
| 169 |
.s = "-" |
| 170 |
}; |
| 171 |
} |
| 172 |
|
| 173 |
struct para_bundle pb; |
| 174 |
bundle_init(&pb); |
| 175 |
|
| 176 |
for (size_t i = 0; i < args.num_fnames; i++) { |
| 177 |
bundle_slurp(&pb, args.fname[i], false); |
| 178 |
} |
| 179 |
|
| 180 |
sort_bundle(&args.keys, &pb); |
| 181 |
size_t num_paras = bundle_size(&pb); |
| 182 |
para_t ** paras = bundle_vec(&pb); |
| 183 |
for (size_t i = 0; i < num_paras; i++) { |
| 184 |
struct fsaf_read_rv r = get_whole_para(paras[i]); |
| 185 |
fwrite(r.b, 1, r.len, stdout); |
| 186 |
putchar('\n'); |
| 187 |
putchar('\n'); |
| 188 |
} |
| 189 |
return 0; |
| 190 |
} |