Skip to content

Commit

Permalink
Merge pull request #909 from 0x00002a/0x2a/feat/shell-escape-cwd
Browse files Browse the repository at this point in the history
Add shell escape working directory option
  • Loading branch information
pkgw authored Sep 10, 2022
2 parents f186048 + 4f3b36d commit fe1c27f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions crates/docmodel/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ pub struct OutputProfile {
/// shell-escape opens enormous security holes. It should only ever be
/// activated with fully trusted input.
pub shell_escape: bool,

/// Directory to use as the cwd for shell escaped execution.
///
/// Setting this to $(pwd) gives the same relative path shell-escape behaviour
/// (e.g. for \inputminted), as other engines, such as xelatex
///
/// Directory is not managed and any files created in it will not be deleted.
///
pub shell_escape_cwd: Option<String>,
}

/// The output target type of a document build.
Expand Down Expand Up @@ -295,6 +304,7 @@ pub(crate) fn default_outputs() -> HashMap<String, OutputProfile> {
index_file: DEFAULT_INDEX_FILE.to_owned(),
postamble_file: DEFAULT_POSTAMBLE_FILE.to_owned(),
shell_escape: false,
shell_escape_cwd: None,
},
);
outputs
Expand Down Expand Up @@ -335,6 +345,7 @@ mod syntax {
#[serde(rename = "postamble")]
pub postamble_file: Option<String>,
pub shell_escape: Option<bool>,
pub shell_escape_cwd: Option<String>,
}

impl OutputProfile {
Expand Down Expand Up @@ -364,6 +375,7 @@ mod syntax {
};

let shell_escape = if !rt.shell_escape { None } else { Some(true) };
let shell_escape_cwd = rt.shell_escape_cwd.clone();

OutputProfile {
name: rt.name.clone(),
Expand All @@ -373,6 +385,7 @@ mod syntax {
index_file,
postamble_file,
shell_escape,
shell_escape_cwd,
}
}

Expand All @@ -399,6 +412,7 @@ mod syntax {
.clone()
.unwrap_or_else(|| DEFAULT_POSTAMBLE_FILE.to_owned()),
shell_escape: self.shell_escape.unwrap_or_default(),
shell_escape_cwd: self.shell_escape_cwd.clone(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/src/v2cli/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ the set of unstable options is subject to change at any time.
| `-Z paper-size=<spec>` | Change the initial paper size. Default: `letter` |
| `-Z search-path=<path>` | Also look in `<path>` for files (unless `--untrusted` has been specified), like TEXINPUTS. Can be specified multiple times. |
| `-Z shell-escape` | Enable `\write18` (unless `--untrusted` has been specified) |
| `-Z shell-escape-cwd=<path>` | Working directory to use for \write18. Use $(pwd) for same behaviour as most other engines (e.g. for relative paths in \inputminted). Implies -Z shell-escape |
6 changes: 5 additions & 1 deletion src/docmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ impl DocumentExt for Document {

if profile.shell_escape {
// For now, this is the only option we allow.
sess_builder.shell_escape_with_temp_dir();
if let Some(cwd) = &profile.shell_escape_cwd {
sess_builder.shell_escape_with_work_dir(cwd);
} else {
sess_builder.shell_escape_with_temp_dir();
}
}

if setup_options.only_cached {
Expand Down
4 changes: 3 additions & 1 deletion src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,9 @@ impl ProcessingSessionBuilder {
} else {
match self.shell_escape_mode {
ShellEscapeMode::Defaulted => {
if self.unstables.shell_escape {
if let Some(ref cwd) = self.unstables.shell_escape_cwd {
ShellEscapeMode::ExternallyManagedDir(cwd.into())
} else if self.unstables.shell_escape {
ShellEscapeMode::TempDir
} else {
ShellEscapeMode::Disabled
Expand Down
13 changes: 13 additions & 0 deletions src/unstable_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const HELPMSG: &str = r#"Available unstable options:
-Z search-path=<path> Also look in <path> for files, like TEXINPUTS. Can be specified
multiple times.
-Z shell-escape Enable \write18
-Z shell-escape-cwd Working directory to use for \write18. Use $(pwd) for same behaviour as
most other engines (e.g. for relative paths in \inputminted).
Implies -Z shell-escape
"#;

// Each entry of this should correspond to a field of UnstableOptions.
Expand All @@ -37,6 +40,7 @@ pub enum UnstableArg {
PaperSize(String),
SearchPath(PathBuf),
ShellEscapeEnabled,
ShellEscapeCwd(String),
}

impl FromStr for UnstableArg {
Expand Down Expand Up @@ -90,6 +94,10 @@ impl FromStr for UnstableArg {

"shell-escape" => require_no_value(value, UnstableArg::ShellEscapeEnabled),

"shell-escape-cwd" => {
require_value("path").map(|s| UnstableArg::ShellEscapeCwd(s.to_string()))
}

_ => Err(format!("Unknown unstable option '{}'", arg).into()),
}
}
Expand All @@ -102,6 +110,7 @@ pub struct UnstableOptions {
pub shell_escape: bool,
pub min_crossrefs: Option<i32>,
pub extra_search_paths: Vec<PathBuf>,
pub shell_escape_cwd: Option<String>,
}

impl UnstableOptions {
Expand All @@ -123,6 +132,10 @@ impl UnstableOptions {
PaperSize(size) => opts.paper_size = Some(size),
ShellEscapeEnabled => opts.shell_escape = true,
SearchPath(p) => opts.extra_search_paths.push(p),
ShellEscapeCwd(p) => {
opts.shell_escape_cwd = Some(p);
opts.shell_escape = true;
}
}
}

Expand Down

0 comments on commit fe1c27f

Please sign in to comment.