+/*
+ * Parse an option passed to an update command.
+ *
+ * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
+ * parsed.
+ *
+ * @option: Text string for the option (beginning with --)
+ *
+ * @cmd: `struct wimlib_update_command' that is being constructed for
+ * this command.
+ *
+ * Returns true if the option was recognized; false if not.
+ */
+static bool
+update_command_add_option(int op, const tchar *option,
+ struct wimlib_update_command *cmd)
+{
+ bool recognized = true;
+ switch (op) {
+ case WIMLIB_UPDATE_OP_ADD:
+ if (!tstrcmp(option, T("--verbose")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
+ else if (!tstrcmp(option, T("--unix-data")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
+ else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
+ else if (!tstrcmp(option, T("--strict-acls")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
+ else if (!tstrcmp(option, T("--dereference")))
+ cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
+ else
+ recognized = false;
+ break;
+ case WIMLIB_UPDATE_OP_DELETE:
+ if (!tstrcmp(option, T("--force")))
+ cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
+ else if (!tstrcmp(option, T("--recursive")))
+ cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
+ else
+ recognized = false;
+ break;
+ default:
+ recognized = false;
+ break;
+ }
+ return recognized;
+}
+
+/* How many nonoption arguments each `imagex update' command expects */
+static const unsigned update_command_num_nonoptions[] = {
+ [WIMLIB_UPDATE_OP_ADD] = 2,
+ [WIMLIB_UPDATE_OP_DELETE] = 1,
+ [WIMLIB_UPDATE_OP_RENAME] = 2,
+};
+
+static void
+update_command_add_nonoption(int op, const tchar *nonoption,
+ struct wimlib_update_command *cmd,
+ unsigned num_nonoptions)
+{
+ switch (op) {
+ case WIMLIB_UPDATE_OP_ADD:
+ if (num_nonoptions == 0)
+ cmd->add.fs_source_path = (tchar*)nonoption;
+ else
+ cmd->add.wim_target_path = (tchar*)nonoption;
+ break;
+ case WIMLIB_UPDATE_OP_DELETE:
+ cmd->delete_.wim_path = (tchar*)nonoption;
+ break;
+ case WIMLIB_UPDATE_OP_RENAME:
+ if (num_nonoptions == 0)
+ cmd->rename.wim_source_path = (tchar*)nonoption;
+ else
+ cmd->rename.wim_target_path = (tchar*)nonoption;
+ break;
+ }
+}
+
+/*
+ * Parse a command passed on stdin to `imagex update'.
+ *
+ * @line: Text of the command.
+ * @len: Length of the line, including a null terminator
+ * at line[len - 1].
+ *
+ * @command: A `struct wimlib_update_command' to fill in from the parsed
+ * line.
+ *
+ * @line_number: Line number of the command, for diagnostics.
+ *
+ * Returns true on success; returns false on parse error.
+ */
+static bool
+parse_update_command(tchar *line, size_t len,
+ struct wimlib_update_command *command,
+ size_t line_number)
+{
+ int ret;
+ tchar *command_name;
+ int op;
+ size_t num_nonoptions;
+
+ /* Get the command name ("add", "delete", "rename") */
+ ret = parse_string(&line, &len, &command_name);
+ if (ret != PARSE_STRING_SUCCESS)
+ return false;
+
+ if (!tstrcasecmp(command_name, T("add"))) {
+ op = WIMLIB_UPDATE_OP_ADD;
+ } else if (!tstrcasecmp(command_name, T("delete"))) {
+ op = WIMLIB_UPDATE_OP_DELETE;
+ } else if (!tstrcasecmp(command_name, T("rename"))) {
+ op = WIMLIB_UPDATE_OP_RENAME;
+ } else {
+ imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
+ command_name, line_number);
+ return false;
+ }
+ command->op = op;
+
+ /* Parse additional options and non-options as needed */
+ num_nonoptions = 0;
+ for (;;) {
+ tchar *next_string;
+
+ ret = parse_string(&line, &len, &next_string);
+ if (ret == PARSE_STRING_NONE) /* End of line */
+ break;
+ else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
+ return false;
+ if (next_string[0] == T('-') && next_string[1] == T('-')) {
+ /* Option */
+ if (!update_command_add_option(op, next_string, command))
+ {
+ imagex_error(T("Unrecognized option \"%"TS"\" to "
+ "update command \"%"TS"\" on line %zu"),
+ next_string, command_name, line_number);
+
+ return false;
+ }
+ } else {
+ /* Nonoption */
+ if (num_nonoptions == update_command_num_nonoptions[op])
+ {
+ imagex_error(T("Unexpected argument \"%"TS"\" in "
+ "update command on line %zu\n"
+ " (The \"%"TS"\" command only "
+ "takes %zu nonoption arguments!)\n"),
+ next_string, line_number,
+ command_name, num_nonoptions);
+ return false;
+ }
+ update_command_add_nonoption(op, next_string,
+ command, num_nonoptions);
+ num_nonoptions++;
+ }
+ }
+
+ if (num_nonoptions != update_command_num_nonoptions[op]) {
+ imagex_error(T("Not enough arguments to update command "
+ "\"%"TS"\" on line %zu"), command_name, line_number);
+ return false;
+ }
+ return true;
+}
+
+static struct wimlib_update_command *
+parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
+ size_t *num_cmds_ret)
+{
+ ssize_t nlines;
+ tchar *p;
+ struct wimlib_update_command *cmds;
+ size_t i, j;
+
+ nlines = text_file_count_lines(cmd_file_contents_p,
+ &cmd_file_nchars);
+ if (nlines < 0)
+ return NULL;
+
+ /* Always allocate at least 1 slot, just in case the implementation of
+ * calloc() returns NULL if 0 bytes are requested. */
+ cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
+ if (!cmds) {
+ imagex_error(T("out of memory"));
+ return NULL;
+ }
+ p = *cmd_file_contents_p;
+ j = 0;
+ for (i = 0; i < nlines; i++) {
+ /* XXX: Could use rawmemchr() here instead, but it may not be
+ * available on all platforms. */
+ tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
+ size_t len = endp - p + 1;
+ *endp = T('\0');
+ if (!is_comment_line(p, len)) {
+ if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
+ free(cmds);
+ return NULL;
+ }
+ }
+ p = endp + 1;
+ }
+ *num_cmds_ret = j;
+ return cmds;
+}
+
+/* Apply one image, or all images, from a WIM file into a directory, OR apply
+ * one image from a WIM file to a NTFS volume. */
+static int
+imagex_apply(int argc, tchar **argv, int cmd)
+{