3 // Copyright (C) 2007 Daniel Hokka Zakrisson
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <sys/types.h>
31 #define ENSC_WRAPPERS_PREFIX "vtag: "
32 #define ENSC_WRAPPERS_VSERVER 1
35 #define CMD_HELP 0x1000
36 #define CMD_VERSION 0x1001
37 #define CMD_TAG 0x4000
38 #define CMD_CREATE 0x4001
39 #define CMD_MIGRATE 0x4002
40 #define CMD_SILENTEXIST 0x4003
41 #define CMD_SILENT 0x4004
46 { "help", no_argument, 0, CMD_HELP },
47 { "version", no_argument, 0, CMD_VERSION },
48 { "tag", required_argument, 0, CMD_TAG },
49 { "create", no_argument, 0, CMD_CREATE },
50 { "migrate", no_argument, 0, CMD_MIGRATE },
51 { "silent", no_argument, 0, CMD_SILENT },
52 { "silentexist", no_argument, 0, CMD_SILENTEXIST },
64 int wrapper_exit_code = 255;
67 showHelp(int fd, char const *cmd, int res)
69 WRITE_MSG(fd, "Usage:\n ");
72 " (--create|--migrate) --tag <tag> <opts>* [--] <program> <args>*\n "
75 " --silentexist ... be silent when context exists already; useful\n"
76 " for '--create' only\n"
77 " --silent ... if the feature is not supported, just execute\n"
80 "'vtag --create' exits with code 254 iff the context exists already.\n"
82 "Please report bugs to " PACKAGE_BUGREPORT "\n");
91 "vtag " VERSION " -- sets the process's filesystem tag\n"
92 "This program is part of " PACKAGE_STRING "\n\n"
93 "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
94 VERSION_COPYRIGHT_DISCLAIMER);
98 static inline ALWAYSINLINE int
99 doit(struct Arguments const *args, char *argv[])
103 if (!vc_isSupported(vcFEATURE_PPTAG)) {
104 if (args->verbosity >= 1) {
106 perror(ENSC_WRAPPERS_PREFIX);
107 return wrapper_exit_code;
113 if (args->do_create) {
114 tag = vc_tag_create(args->tag);
118 if (!args->is_silentexist)
119 perror(ENSC_WRAPPERS_PREFIX "vc_tag_create()");
122 perror(ENSC_WRAPPERS_PREFIX "vc_tag_create()");
123 return wrapper_exit_code;
130 if (args->do_migrate)
131 Evc_tag_migrate(tag);
134 execvp(argv[optind], argv+optind);
136 PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
137 return wrapper_exit_code;
140 int main (int argc, char *argv[])
142 struct Arguments args = {
146 .is_silentexist = false,
151 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
155 case CMD_HELP : showHelp(1, argv[0], 0);
156 case CMD_VERSION : showVersion();
157 case CMD_CREATE : args.do_create = true; break;
158 case CMD_MIGRATE : args.do_migrate = true; break;
159 case CMD_SILENTEXIST : args.is_silentexist = true; break;
160 case CMD_TAG : args.tag = Evc_tagopt2tag(optarg,true); break;
161 case CMD_SILENT : args.verbosity--; break;
164 WRITE_MSG(2, "Try '");
165 WRITE_STR(2, argv[0]);
166 WRITE_MSG(2, " --help' for more information.\n");
167 return wrapper_exit_code;
172 if (!args.do_create && !args.do_migrate)
173 WRITE_MSG(2, "Neither '--create' nor '--migrate' specified; try '--help' for more information\n");
174 else if (args.do_create && args.do_migrate)
175 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
176 else if (args.tag==VC_NOCTX)
177 WRITE_MSG(2, "You must specify the tag with '--tag'; try '--help' for more information\n");
178 else if (optind>=argc)
179 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
181 return doit(&args, argv);
183 return wrapper_exit_code;