| 1 | /* |
| 2 | A parser for file-listings formatted like 'ls -l'. |
| 3 | |
| 4 | Copyright (C) 2016 |
| 5 | Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of the Midnight Commander. |
| 8 | |
| 9 | The Midnight Commander is free software: you can redistribute it |
| 10 | and/or modify it under the terms of the GNU General Public License as |
| 11 | published by the Free Software Foundation, either version 3 of the License, |
| 12 | or (at your option) any later version. |
| 13 | |
| 14 | The Midnight Commander is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | /** \file |
| 24 | * \brief A parser for file-listings formatted like 'ls -l'. |
| 25 | * |
| 26 | * This program parses file-listings the same way MC does. |
| 27 | * It's basically a wrapper around vfs_parse_ls_lga(). |
| 28 | * You can feed it the output of any of extfs's helpers. |
| 29 | * |
| 30 | * After parsing, the data is written out to stdout. The output |
| 31 | * format can be either YAML or a format similar to 'ls -l'. |
| 32 | * |
| 33 | * This program is the main tool our tester script is going to use. |
| 34 | */ |
| 35 | |
| 36 | #include <config.h> |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #include "lib/global.h" |
| 42 | |
| 43 | #include "lib/vfs/utilvfs.h" /* vfs_parse_ls_lga() */ |
| 44 | #include "lib/util.h" /* string_perm() */ |
| 45 | #include "lib/timefmt.h" /* FMT_LOCALTIME */ |
| 46 | #include "lib/widget.h" /* for the prototype of message() only */ |
| 47 | |
| 48 | /*** global variables ****************************************************************************/ |
| 49 | |
| 50 | /*** file scope macro definitions ****************************************************************/ |
| 51 | |
| 52 | /*** file scope type declarations ****************************************************************/ |
| 53 | |
| 54 | typedef enum |
| 55 | { |
| 56 | FORMAT_YAML, |
| 57 | FORMAT_LS |
| 58 | } output_format_t; |
| 59 | |
| 60 | /*** file scope variables ************************************************************************/ |
| 61 | |
| 62 | /* Command-line options. */ |
| 63 | static gboolean opt_drop_mtime = FALSE; |
| 64 | static gboolean opt_drop_ids = FALSE; |
| 65 | static gboolean opt_symbolic_ids = FALSE; |
| 66 | static output_format_t opt_output_format = FORMAT_LS; |
| 67 | |
| 68 | /* Misc. */ |
| 69 | static int error_count = 0; |
| 70 | |
| 71 | /* forward declarations */ |
| 72 | static gboolean |
| 73 | parse_format_name_argument (const gchar * option_name, const gchar * value, gpointer data, |
| 74 | GError ** error); |
| 75 | |
| 76 | static GOptionEntry entries[] = { |
| 77 | {"drop-mtime", 0, 0, G_OPTION_ARG_NONE, &opt_drop_mtime, "Don't include mtime in the output.", |
| 78 | NULL}, |
| 79 | {"drop-ids", 0, 0, G_OPTION_ARG_NONE, &opt_drop_ids, "Don't include uid/gid in the output.", |
| 80 | NULL}, |
| 81 | {"symbolic-ids", 0, 0, G_OPTION_ARG_NONE, &opt_symbolic_ids, |
| 82 | "Print the strings '<<uid>>'/'<<gid>>' instead of the numeric IDs when they match the process' uid/gid.", |
| 83 | NULL}, |
| 84 | {"format", 'f', 0, G_OPTION_ARG_CALLBACK, parse_format_name_argument, |
| 85 | "Output format. Default: ls.", "<ls|yaml>"}, |
| 86 | {NULL, '\0', 0, 0, NULL, NULL, NULL} /* Make the compiler shut up by initializing everything. */ |
| 87 | }; |
| 88 | |
| 89 | /*** file scope functions ************************************************************************/ |
| 90 | /* --------------------------------------------------------------------------------------------- */ |
| 91 | /** |
| 92 | * Command-line handling. |
| 93 | */ |
| 94 | /* --------------------------------------------------------------------------------------------- */ |
| 95 | |
| 96 | static gboolean |
| 97 | parse_format_name_argument (const gchar * option_name, const gchar * value, gpointer data, |
| 98 | GError ** error) |
| 99 | { |
| 100 | (void) option_name; |
| 101 | (void) data; |
| 102 | |
| 103 | if (strcmp (value, "yaml") == 0) |
| 104 | opt_output_format = FORMAT_YAML; |
| 105 | else if (strcmp (value, "ls") == 0) |
| 106 | opt_output_format = FORMAT_LS; |
| 107 | else |
| 108 | { |
| 109 | g_set_error (error, MC_ERROR, G_OPTION_ERROR_FAILED, "unknown output format '%s'", value); |
| 110 | return FALSE; |
| 111 | } |
| 112 | |
| 113 | return TRUE; |
| 114 | } |
| 115 | |
| 116 | /* --------------------------------------------------------------------------------------------- */ |
| 117 | |
| 118 | static gboolean |
| 119 | parse_command_line (int *argc, char **argv[]) |
| 120 | { |
| 121 | GError *error = NULL; |
| 122 | GOptionContext *context; |
| 123 | |
| 124 | context = |
| 125 | g_option_context_new |
| 126 | ("- Parses its input, which is expected to be in a format similar to 'ls -l', and writes the result to stdout."); |
| 127 | g_option_context_add_main_entries (context, entries, NULL); |
| 128 | if (!g_option_context_parse (context, argc, argv, &error)) |
| 129 | { |
| 130 | g_print ("option parsing failed: %s\n", error->message); |
| 131 | return FALSE; |
| 132 | } |
| 133 | |
| 134 | return TRUE; |
| 135 | } |
| 136 | |
| 137 | /* --------------------------------------------------------------------------------------------- */ |
| 138 | /** |
| 139 | * Utility functions. |
| 140 | */ |
| 141 | /* --------------------------------------------------------------------------------------------- */ |
| 142 | |
| 143 | static const char * |
| 144 | my_itoa (int i) |
| 145 | { |
| 146 | static char buf[BUF_SMALL]; |
| 147 | sprintf (buf, "%d", i); |
| 148 | return buf; |
| 149 | } |
| 150 | |
| 151 | /* --------------------------------------------------------------------------------------------- */ |
| 152 | |
| 153 | /** |
| 154 | * Returns the uid as-is, or as "<<uid>>" if the same as current user. |
| 155 | */ |
| 156 | static const char * |
| 157 | symbolic_uid (uid_t uid) |
| 158 | { |
| 159 | return (opt_symbolic_ids && uid == getuid ())? "<<uid>>" : my_itoa ((int) uid); |
| 160 | } |
| 161 | |
| 162 | /* --------------------------------------------------------------------------------------------- */ |
| 163 | |
| 164 | static const char * |
| 165 | symbolic_gid (gid_t gid) |
| 166 | { |
| 167 | return (opt_symbolic_ids && gid == getgid ())? "<<gid>>" : my_itoa ((int) gid); |
| 168 | } |
| 169 | |
| 170 | /* --------------------------------------------------------------------------------------------- */ |
| 171 | |
| 172 | /** |
| 173 | * Cuts off a string's line-end (as in Perl). |
| 174 | */ |
| 175 | static void |
| 176 | chomp (char *s) |
| 177 | { |
| 178 | int i; |
| 179 | |
| 180 | i = strlen (s); |
| 181 | |
| 182 | /* Code taken from vfs_parse_ls_lga(), with modifications. */ |
| 183 | if ((--i >= 0) && (s[i] == '\r' || s[i] == '\n')) |
| 184 | s[i] = '\0'; |
| 185 | if ((--i >= 0) && (s[i] == '\r' || s[i] == '\n')) |
| 186 | s[i] = '\0'; |
| 187 | } |
| 188 | |
| 189 | /* --------------------------------------------------------------------------------------------- */ |
| 190 | |
| 191 | static const char * |
| 192 | string_date (time_t time) |
| 193 | { |
| 194 | static char buf[BUF_SMALL]; |
| 195 | /* |
| 196 | * If we ever want to be able to re-parse the output of this program, |
| 197 | * we'll need to use the American brain-damaged MM-DD-YYYY instead of |
| 198 | * YYYY-MM-DD because vfs_parse_ls_lga() doesn't currently recognize |
| 199 | * the latter. |
| 200 | */ |
| 201 | FMT_LOCALTIME (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", time); |
| 202 | return buf; |
| 203 | } |
| 204 | |
| 205 | /* --------------------------------------------------------------------------------------------- */ |
| 206 | |
| 207 | /** |
| 208 | * Override MC's message(). |
| 209 | * |
| 210 | * vfs_parse_ls_lga() calls this on error. Since MC's uses tty/widgets, it |
| 211 | * will crash us. We replace it with a plain version. |
| 212 | */ |
| 213 | void |
| 214 | message (int flags, const char *title, const char *text, ...) |
| 215 | { |
| 216 | char *p; |
| 217 | va_list ap; |
| 218 | |
| 219 | (void) flags; |
| 220 | (void) title; |
| 221 | |
| 222 | va_start (ap, text); |
| 223 | p = g_strdup_vprintf (text, ap); |
| 224 | va_end (ap); |
| 225 | printf ("message(): vfs_parse_ls_lga(): parsing error at: %s\n", p); |
| 226 | g_free (p); |
| 227 | } |
| 228 | |
| 229 | /* --------------------------------------------------------------------------------------------- */ |
| 230 | /** |
| 231 | * YAML output format. |
| 232 | */ |
| 233 | /* --------------------------------------------------------------------------------------------- */ |
| 234 | |
| 235 | static void |
| 236 | yaml_dump_stbuf (const struct stat *st) |
| 237 | { |
| 238 | /* Various casts and flags here were taken/inspired by info_show_info(). */ |
| 239 | printf (" perm: %s\n", string_perm (st->st_mode)); |
| 240 | if (!opt_drop_ids) |
| 241 | { |
| 242 | printf (" uid: %s\n", symbolic_uid (st->st_uid)); |
| 243 | printf (" gid: %s\n", symbolic_gid (st->st_gid)); |
| 244 | } |
| 245 | printf (" size: %" PRIuMAX "\n", (uintmax_t) st->st_size); |
| 246 | printf (" nlink: %d\n", (int) st->st_nlink); |
| 247 | if (!opt_drop_mtime) |
| 248 | printf (" mtime: %s\n", string_date (st->st_mtime)); |
| 249 | } |
| 250 | |
| 251 | /* --------------------------------------------------------------------------------------------- */ |
| 252 | |
| 253 | static void |
| 254 | yaml_dump_string (const char *name, const char *val) |
| 255 | { |
| 256 | char *q = g_shell_quote (val); |
| 257 | printf (" %s: %s\n", name, q); |
| 258 | g_free (q); |
| 259 | } |
| 260 | |
| 261 | /* --------------------------------------------------------------------------------------------- */ |
| 262 | |
| 263 | static void |
| 264 | yaml_dump_record (gboolean success, const char *input_line, const struct stat *st, |
| 265 | const char *filename, const char *linkname) |
| 266 | { |
| 267 | printf ("-\n"); /* Start new item in the list. */ |
| 268 | |
| 269 | if (success) |
| 270 | { |
| 271 | if (filename) |
| 272 | yaml_dump_string ("name", filename); |
| 273 | if (linkname) |
| 274 | yaml_dump_string ("linkname", linkname); |
| 275 | yaml_dump_stbuf (st); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | yaml_dump_string ("cannot parse input line", input_line); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* --------------------------------------------------------------------------------------------- */ |
| 284 | /** |
| 285 | * 'ls' output format. |
| 286 | */ |
| 287 | /* --------------------------------------------------------------------------------------------- */ |
| 288 | |
| 289 | static void |
| 290 | ls_dump_stbuf (const struct stat *st) |
| 291 | { |
| 292 | /* Various casts and flags here were taken/inspired by info_show_info(). */ |
| 293 | printf ("%s %3d ", string_perm (st->st_mode), (int) st->st_nlink); |
| 294 | if (!opt_drop_ids) |
| 295 | { |
| 296 | printf ("%8s ", symbolic_uid (st->st_uid)); |
| 297 | printf ("%8s ", symbolic_gid (st->st_gid)); |
| 298 | } |
| 299 | printf ("%10" PRIuMAX " ", (uintmax_t) st->st_size); |
| 300 | if (!opt_drop_mtime) |
| 301 | printf ("%s ", string_date (st->st_mtime)); |
| 302 | } |
| 303 | |
| 304 | /* --------------------------------------------------------------------------------------------- */ |
| 305 | |
| 306 | static void |
| 307 | ls_dump_record (gboolean success, const char *input_line, const struct stat *st, |
| 308 | const char *filename, const char *linkname) |
| 309 | { |
| 310 | if (success) |
| 311 | { |
| 312 | ls_dump_stbuf (st); |
| 313 | if (filename) |
| 314 | printf ("%s", filename); |
| 315 | if (linkname) |
| 316 | printf (" -> %s", linkname); |
| 317 | printf ("\n"); |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | printf ("cannot parse input line: '%s'\n", input_line); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* ------------------------------------------------------------------------------ */ |
| 326 | /** |
| 327 | * Main code. |
| 328 | */ |
| 329 | /* ------------------------------------------------------------------------------ */ |
| 330 | |
| 331 | static void |
| 332 | process_ls_line (const char *line) |
| 333 | { |
| 334 | struct stat st; |
| 335 | char *filename, *linkname; |
| 336 | gboolean success; |
| 337 | |
| 338 | memset (&st, 0, sizeof st); |
| 339 | filename = NULL; |
| 340 | linkname = NULL; |
| 341 | |
| 342 | success = vfs_parse_ls_lga (line, &st, &filename, &linkname, NULL); |
| 343 | |
| 344 | if (!success) |
| 345 | error_count++; |
| 346 | |
| 347 | if (opt_output_format == FORMAT_YAML) |
| 348 | yaml_dump_record (success, line, &st, filename, linkname); |
| 349 | else |
| 350 | ls_dump_record (success, line, &st, filename, linkname); |
| 351 | |
| 352 | g_free (filename); |
| 353 | g_free (linkname); |
| 354 | } |
| 355 | |
| 356 | /* ------------------------------------------------------------------------------ */ |
| 357 | |
| 358 | static void |
| 359 | process_input (FILE * input) |
| 360 | { |
| 361 | char line[BUF_4K]; |
| 362 | |
| 363 | while (fgets (line, sizeof line, input) != NULL) |
| 364 | { |
| 365 | chomp (line); /* Not mandatory. Makes error messages nicer. */ |
| 366 | if (strncmp (line, "total ", 6) == 0) /* Convenience only: makes 'ls -l' parse cleanly. */ |
| 367 | continue; |
| 368 | process_ls_line (line); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* ------------------------------------------------------------------------------ */ |
| 373 | |
| 374 | int |
| 375 | main (int argc, char *argv[]) |
| 376 | { |
| 377 | FILE *input; |
| 378 | |
| 379 | if (!parse_command_line (&argc, &argv)) |
| 380 | return EXIT_FAILURE; |
| 381 | |
| 382 | if (argc >= 2) |
| 383 | { |
| 384 | if ((input = fopen (argv[1], "r")) == NULL) |
| 385 | { |
| 386 | perror (argv[1]); |
| 387 | return EXIT_FAILURE; |
| 388 | } |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | input = stdin; |
| 393 | } |
| 394 | |
| 395 | process_input (input); |
| 396 | |
| 397 | return (error_count > 0) ? EXIT_FAILURE : EXIT_SUCCESS; |
| 398 | } |
| 399 | |
| 400 | /* ------------------------------------------------------------------------------ */ |