]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - spaceman/init.c
Merge branch 'libxfs-4.18-sync' into for-next
[thirdparty/xfsprogs-dev.git] / spaceman / init.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 Red Hat, Inc
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "command.h"
9 #include "input.h"
10 #include "init.h"
11 #include "path.h"
12 #include "space.h"
13
14 char *progname;
15 int exitcode;
16
17 void
18 usage(void)
19 {
20 fprintf(stderr,
21 _("Usage: %s [-c cmd] file\n"),
22 progname);
23 exit(1);
24 }
25
26 static void
27 init_commands(void)
28 {
29 print_init();
30 help_init();
31 info_init();
32 prealloc_init();
33 quit_init();
34 trim_init();
35 freesp_init();
36 }
37
38 static int
39 init_args_command(
40 int index)
41 {
42 if (index >= filecount)
43 return 0;
44 file = &filetable[index++];
45 return index;
46 }
47
48 static int
49 init_check_command(
50 const cmdinfo_t *ct)
51 {
52 if (!(ct->flags & CMD_FLAG_ONESHOT))
53 return 0;
54 return 1;
55 }
56
57 void
58 init(
59 int argc,
60 char **argv)
61 {
62 int c;
63 xfs_fsop_geom_t geometry = { 0 };
64 struct fs_path fsp;
65
66 progname = basename(argv[0]);
67 setlocale(LC_ALL, "");
68 bindtextdomain(PACKAGE, LOCALEDIR);
69 textdomain(PACKAGE);
70
71 fs_table_initialise(0, NULL, 0, NULL);
72 while ((c = getopt(argc, argv, "c:p:V")) != EOF) {
73 switch (c) {
74 case 'c':
75 add_user_command(optarg);
76 break;
77 case 'p':
78 progname = optarg;
79 break;
80 case 'V':
81 printf(_("%s version %s\n"), progname, VERSION);
82 exit(0);
83 default:
84 usage();
85 }
86 }
87
88 if (optind != argc - 1)
89 usage();
90
91 if ((c = openfile(argv[optind], &geometry, &fsp)) < 0)
92 exit(1);
93 if (!platform_test_xfs_fd(c))
94 printf(_("Not an XFS filesystem!\n"));
95 if (addfile(argv[optind], c, &geometry, &fsp) < 0)
96 exit(1);
97
98 init_commands();
99 add_command_iterator(init_args_command);
100 add_check_command(init_check_command);
101 }
102
103 int
104 main(
105 int argc,
106 char **argv)
107 {
108 init(argc, argv);
109 command_loop();
110 return exitcode;
111 }