Commit 41ff9ecb authored by Michael Aaron Murphy's avatar Michael Aaron Murphy
Browse files

Public items now have crate-level visibility

This is the next step towards using Ion as a library for an upcoming project.
parent a61f0ec9
......@@ -5,7 +5,7 @@ use types::Identifier;
#[derive(Debug)]
/// A possible error that can be caused when attempting to obtain or execute a
/// function that is supposed to return a string from across the FFI boundaries.
pub enum StringError {
pub(crate) enum StringError {
/// This occurs when a symbol could not be loaded from the library in question. It is an
/// error that infers that the problem is with the plugin, not Ion itself.
SymbolErr(io::Error),
......
......@@ -7,22 +7,22 @@ use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
use sys;
pub use sys::signals::{block, unblock};
pub(crate) use sys::signals::{block, unblock};
pub static PENDING: AtomicUsize = ATOMIC_USIZE_INIT;
/// Suspends a given process by it's process ID.
pub fn suspend(pid: u32) { let _ = sys::killpg(pid, sys::SIGSTOP); }
pub(crate) fn suspend(pid: u32) { let _ = sys::killpg(pid, sys::SIGSTOP); }
/// Resumes a given process by it's process ID.
pub fn resume(pid: u32) { let _ = sys::killpg(pid, sys::SIGCONT); }
pub(crate) fn resume(pid: u32) { let _ = sys::killpg(pid, sys::SIGCONT); }
/// The purpose of the signal handler is to ignore signals when it is active, and then continue
/// listening to signals once the handler is dropped.
pub struct SignalHandler;
pub(crate) struct SignalHandler;
impl SignalHandler {
pub fn new() -> SignalHandler {
pub(crate) fn new() -> SignalHandler {
block();
SignalHandler
}
......
pub const SUCCESS: i32 = 0;
pub const FAILURE: i32 = 1;
pub const BAD_ARG: i32 = 2;
pub const COULD_NOT_EXEC: i32 = 126;
pub const NO_SUCH_COMMAND: i32 = 127;
pub const TERMINATED: i32 = 143;
pub(crate) const SUCCESS: i32 = 0;
pub(crate) const FAILURE: i32 = 1;
pub(crate) const BAD_ARG: i32 = 2;
pub(crate) const COULD_NOT_EXEC: i32 = 126;
pub(crate) const NO_SUCH_COMMAND: i32 = 127;
pub(crate) const TERMINATED: i32 = 143;
pub fn get_signal_code(signal: i32) -> i32 { 128 + signal }
pub(crate) fn get_signal_code(signal: i32) -> i32 { 128 + signal }
......@@ -22,7 +22,7 @@ lazy_static! {
}
#[derive(Debug)]
pub struct Variables {
pub(crate) struct Variables {
pub hashmaps: HashMapVariableContext,
pub arrays: ArrayVariableContext,
pub variables: VariableContext,
......@@ -81,13 +81,13 @@ impl Default for Variables {
const PLUGIN: u8 = 1;
impl Variables {
pub fn has_plugin_support(&self) -> bool { self.flags & PLUGIN != 0 }
pub(crate) fn has_plugin_support(&self) -> bool { self.flags & PLUGIN != 0 }
pub fn enable_plugins(&mut self) { self.flags |= PLUGIN; }
pub(crate) fn enable_plugins(&mut self) { self.flags |= PLUGIN; }
pub fn disable_plugins(&mut self) { self.flags &= 255 ^ PLUGIN; }
pub(crate) fn disable_plugins(&mut self) { self.flags &= 255 ^ PLUGIN; }
pub fn read<I: IntoIterator>(&mut self, args: I) -> i32
pub(crate) fn read<I: IntoIterator>(&mut self, args: I) -> i32
where I::Item: AsRef<str>
{
if sys::isatty(sys::STDIN_FILENO) {
......@@ -111,7 +111,7 @@ impl Variables {
SUCCESS
}
pub fn set_var(&mut self, name: &str, value: &str) {
pub(crate) fn set_var(&mut self, name: &str, value: &str) {
if name == "NS_PLUGINS" {
match value {
"0" => self.disable_plugins(),
......@@ -132,7 +132,7 @@ impl Variables {
}
}
pub fn set_array(&mut self, name: &str, value: Array) {
pub(crate) fn set_array(&mut self, name: &str, value: Array) {
if !name.is_empty() {
if value.is_empty() {
self.arrays.remove(name);
......@@ -143,7 +143,7 @@ impl Variables {
}
#[allow(dead_code)]
pub fn set_hashmap_value(&mut self, name: &str, key: &str, value: &str) {
pub(crate) fn set_hashmap_value(&mut self, name: &str, key: &str, value: &str) {
if !name.is_empty() {
if let Some(map) = self.hashmaps.get_mut(name) {
map.insert(key.into(), value.into());
......@@ -156,11 +156,11 @@ impl Variables {
}
}
pub fn get_map(&self, name: &str) -> Option<&HashMap> { self.hashmaps.get(name) }
pub(crate) fn get_map(&self, name: &str) -> Option<&HashMap> { self.hashmaps.get(name) }
pub fn get_array(&self, name: &str) -> Option<&Array> { self.arrays.get(name) }
pub(crate) fn get_array(&self, name: &str) -> Option<&Array> { self.arrays.get(name) }
pub fn unset_array(&mut self, name: &str) -> Option<Array> { self.arrays.remove(name) }
pub(crate) fn unset_array(&mut self, name: &str) -> Option<Array> { self.arrays.remove(name) }
/// Obtains the value for the **SWD** variable.
///
......@@ -202,7 +202,7 @@ impl Variables {
swd
}
pub fn get_var(&self, name: &str) -> Option<Value> {
pub(crate) fn get_var(&self, name: &str) -> Option<Value> {
match name {
"SWD" => return Some(self.get_simplified_directory()),
"MWD" => return Some(self.get_minimal_directory()),
......@@ -253,23 +253,23 @@ impl Variables {
}
}
pub fn get_var_or_empty(&self, name: &str) -> Value { self.get_var(name).unwrap_or_default() }
pub(crate) fn get_var_or_empty(&self, name: &str) -> Value { self.get_var(name).unwrap_or_default() }
pub fn unset_var(&mut self, name: &str) -> Option<Value> { self.variables.remove(name) }
pub(crate) fn unset_var(&mut self, name: &str) -> Option<Value> { self.variables.remove(name) }
pub fn get_vars(&self) -> Vec<Identifier> {
pub(crate) fn get_vars(&self) -> Vec<Identifier> {
self.variables.keys().cloned().chain(env::vars().map(|(k, _)| k.into())).collect()
}
pub fn is_valid_variable_character(c: char) -> bool {
pub(crate) fn is_valid_variable_character(c: char) -> bool {
c.is_alphanumeric() || c == '_' || c == '?'
}
pub fn is_valid_variable_name(name: &str) -> bool {
pub(crate) fn is_valid_variable_name(name: &str) -> bool {
name.chars().all(Variables::is_valid_variable_character)
}
pub fn tilde_expansion(&self, word: &str, dir_stack: &DirectoryStack) -> Option<String> {
pub(crate) fn tilde_expansion(&self, word: &str, dir_stack: &DirectoryStack) -> Option<String> {
let mut chars = word.char_indices();
let tilde_prefix;
......@@ -338,7 +338,7 @@ impl Variables {
}
#[allow(dead_code)]
pub fn command_expansion(&self, command: &str) -> Option<Value> {
pub(crate) fn command_expansion(&self, command: &str) -> Option<Value> {
if let Ok(exe) = env::current_exe() {
if let Ok(output) = process::Command::new(exe).arg("-c").arg(command).output() {
if let Ok(mut stdout) = String::from_utf8(output.stdout) {
......@@ -355,7 +355,7 @@ impl Variables {
}
#[allow(dead_code)]
pub fn is_hashmap_reference(key: &str) -> Option<(Identifier, Key)> {
pub(crate) fn is_hashmap_reference(key: &str) -> Option<(Identifier, Key)> {
let mut key_iter = key.split('[');
if let Some(map_name) = key_iter.next() {
......
......@@ -5,45 +5,45 @@ use std::os::unix::io::RawFd;
use syscall::SigAction;
pub const PATH_SEPARATOR: &str = ";";
pub(crate) const PATH_SEPARATOR: &str = ";";
pub const O_CLOEXEC: usize = syscall::O_CLOEXEC;
pub const SIGHUP: i32 = syscall::SIGHUP as i32;
pub const SIGINT: i32 = syscall::SIGINT as i32;
pub const SIGTERM: i32 = syscall::SIGTERM as i32;
pub const SIGCONT: i32 = syscall::SIGCONT as i32;
pub const SIGSTOP: i32 = syscall::SIGSTOP as i32;
pub const SIGTSTP: i32 = syscall::SIGTSTP as i32;
pub(crate) const O_CLOEXEC: usize = syscall::O_CLOEXEC;
pub(crate) const SIGHUP: i32 = syscall::SIGHUP as i32;
pub(crate) const SIGINT: i32 = syscall::SIGINT as i32;
pub(crate) const SIGTERM: i32 = syscall::SIGTERM as i32;
pub(crate) const SIGCONT: i32 = syscall::SIGCONT as i32;
pub(crate) const SIGSTOP: i32 = syscall::SIGSTOP as i32;
pub(crate) const SIGTSTP: i32 = syscall::SIGTSTP as i32;
pub const STDIN_FILENO: RawFd = 0;
pub const STDOUT_FILENO: RawFd = 1;
pub const STDERR_FILENO: RawFd = 2;
pub(crate) const STDIN_FILENO: RawFd = 0;
pub(crate) const STDOUT_FILENO: RawFd = 1;
pub(crate) const STDERR_FILENO: RawFd = 2;
pub fn is_root() -> bool { syscall::geteuid().map(|id| id == 0).unwrap_or(false) }
pub(crate) fn is_root() -> bool { syscall::geteuid().map(|id| id == 0).unwrap_or(false) }
pub unsafe fn fork() -> io::Result<u32> { cvt(syscall::clone(0)).map(|pid| pid as u32) }
pub fn getpid() -> io::Result<u32> { cvt(syscall::getpid()).map(|pid| pid as u32) }
pub(crate) fn getpid() -> io::Result<u32> { cvt(syscall::getpid()).map(|pid| pid as u32) }
pub fn kill(pid: u32, signal: i32) -> io::Result<()> {
pub(crate) fn kill(pid: u32, signal: i32) -> io::Result<()> {
cvt(syscall::kill(pid as usize, signal as usize)).and(Ok(()))
}
pub fn killpg(pgid: u32, signal: i32) -> io::Result<()> {
pub(crate) fn killpg(pgid: u32, signal: i32) -> io::Result<()> {
cvt(syscall::kill(-(pgid as isize) as usize, signal as usize)).and(Ok(()))
}
pub fn pipe2(flags: usize) -> io::Result<(RawFd, RawFd)> {
pub(crate) fn pipe2(flags: usize) -> io::Result<(RawFd, RawFd)> {
let mut fds = [0; 2];
cvt(syscall::pipe2(&mut fds, flags))?;
Ok((fds[0], fds[1]))
}
pub fn setpgid(pid: u32, pgid: u32) -> io::Result<()> {
pub(crate) fn setpgid(pid: u32, pgid: u32) -> io::Result<()> {
cvt(syscall::setpgid(pid as usize, pgid as usize)).and(Ok(()))
}
pub fn signal(signal: i32, handler: extern "C" fn(i32)) -> io::Result<()> {
pub(crate) fn signal(signal: i32, handler: extern "C" fn(i32)) -> io::Result<()> {
let new = SigAction {
sa_handler: unsafe { mem::transmute(handler) },
sa_mask: [0; 2],
......@@ -52,7 +52,7 @@ pub fn signal(signal: i32, handler: extern "C" fn(i32)) -> io::Result<()> {
cvt(syscall::sigaction(signal as usize, Some(&new), None)).and(Ok(()))
}
pub fn reset_signal(signal: i32) -> io::Result<()> {
pub(crate) fn reset_signal(signal: i32) -> io::Result<()> {
let new = SigAction {
sa_handler: unsafe { mem::transmute(syscall::flag::SIG_DFL) },
sa_mask: [0; 2],
......@@ -61,7 +61,7 @@ pub fn reset_signal(signal: i32) -> io::Result<()> {
cvt(syscall::sigaction(signal as usize, Some(&new), None)).and(Ok(()))
}
pub fn tcsetpgrp(tty_fd: RawFd, pgid: u32) -> io::Result<()> {
pub(crate) fn tcsetpgrp(tty_fd: RawFd, pgid: u32) -> io::Result<()> {
let fd = cvt(syscall::dup(tty_fd, b"pgrp"))?;
let pgid_usize = pgid as usize;
......@@ -74,13 +74,13 @@ pub fn tcsetpgrp(tty_fd: RawFd, pgid: u32) -> io::Result<()> {
cvt(res).and(Ok(()))
}
pub fn dup(fd: RawFd) -> io::Result<RawFd> { cvt(syscall::dup(fd, &[])) }
pub(crate) fn dup(fd: RawFd) -> io::Result<RawFd> { cvt(syscall::dup(fd, &[])) }
pub fn dup2(old: RawFd, new: RawFd) -> io::Result<RawFd> { cvt(syscall::dup2(old, new, &[])) }
pub(crate) fn dup2(old: RawFd, new: RawFd) -> io::Result<RawFd> { cvt(syscall::dup2(old, new, &[])) }
pub fn close(fd: RawFd) -> io::Result<()> { cvt(syscall::close(fd)).and(Ok(())) }
pub(crate) fn close(fd: RawFd) -> io::Result<()> { cvt(syscall::close(fd)).and(Ok(())) }
pub fn isatty(fd: RawFd) -> bool {
pub(crate) fn isatty(fd: RawFd) -> bool {
if let Ok(tfd) = syscall::dup(fd, b"termios") {
let _ = syscall::close(tfd);
true
......@@ -96,12 +96,12 @@ fn cvt(result: Result<usize, syscall::Error>) -> io::Result<usize> {
// TODO
pub mod signals {
pub fn block() {}
pub(crate) fn block() {}
/// Unblocks the SIGTSTP/SIGTTOU/SIGTTIN/SIGCHLD signals so children processes can be
/// controlled
/// by the shell.
pub fn unblock() {}
pub(crate) fn unblock() {}
}
pub mod job_control {
......@@ -115,7 +115,7 @@ pub mod job_control {
use std::sync::{Arc, Mutex};
use syscall;
pub fn watch_background(
pub(crate) fn watch_background(
_fg: Arc<ForegroundSignals>,
_processes: Arc<Mutex<Vec<BackgroundProcess>>>,
_pid: u32,
......@@ -125,7 +125,7 @@ pub mod job_control {
}
pub fn watch_foreground<'a, F, D>(
pub(crate) fn watch_foreground<'a, F, D>(
shell: &mut Shell<'a>,
_pid: u32,
last_pid: u32,
......@@ -178,7 +178,7 @@ pub mod job_control {
}
pub mod variables {
pub fn get_user_home(_username: &str) -> Option<String> {
pub(crate) fn get_user_home(_username: &str) -> Option<String> {
// TODO
None
}
......
......@@ -4,35 +4,35 @@ use libc::{c_int, pid_t, sighandler_t};
use std::io;
use std::os::unix::io::RawFd;
pub const PATH_SEPARATOR: &str = ":";
pub(crate) const PATH_SEPARATOR: &str = ":";
pub const O_CLOEXEC: usize = libc::O_CLOEXEC as usize;
pub const SIGHUP: i32 = libc::SIGHUP;
pub const SIGINT: i32 = libc::SIGINT;
pub const SIGTERM: i32 = libc::SIGTERM;
pub const SIGCONT: i32 = libc::SIGCONT;
pub const SIGSTOP: i32 = libc::SIGSTOP;
pub const SIGTSTP: i32 = libc::SIGTSTP;
pub(crate) const O_CLOEXEC: usize = libc::O_CLOEXEC as usize;
pub(crate) const SIGHUP: i32 = libc::SIGHUP;
pub(crate) const SIGINT: i32 = libc::SIGINT;
pub(crate) const SIGTERM: i32 = libc::SIGTERM;
pub(crate) const SIGCONT: i32 = libc::SIGCONT;
pub(crate) const SIGSTOP: i32 = libc::SIGSTOP;
pub(crate) const SIGTSTP: i32 = libc::SIGTSTP;
pub const STDOUT_FILENO: i32 = libc::STDOUT_FILENO;
pub const STDERR_FILENO: i32 = libc::STDERR_FILENO;
pub const STDIN_FILENO: i32 = libc::STDIN_FILENO;
pub(crate) const STDOUT_FILENO: i32 = libc::STDOUT_FILENO;
pub(crate) const STDERR_FILENO: i32 = libc::STDERR_FILENO;
pub(crate) const STDIN_FILENO: i32 = libc::STDIN_FILENO;
pub fn is_root() -> bool { unsafe { libc::geteuid() == 0 } }
pub(crate) fn is_root() -> bool { unsafe { libc::geteuid() == 0 } }
pub unsafe fn fork() -> io::Result<u32> { cvt(libc::fork()).map(|pid| pid as u32) }
pub fn getpid() -> io::Result<u32> { cvt(unsafe { libc::getpid() }).map(|pid| pid as u32) }
pub(crate) fn getpid() -> io::Result<u32> { cvt(unsafe { libc::getpid() }).map(|pid| pid as u32) }
pub fn kill(pid: u32, signal: i32) -> io::Result<()> {
pub(crate) fn kill(pid: u32, signal: i32) -> io::Result<()> {
cvt(unsafe { libc::kill(pid as pid_t, signal as c_int) }).and(Ok(()))
}
pub fn killpg(pgid: u32, signal: i32) -> io::Result<()> {
pub(crate) fn killpg(pgid: u32, signal: i32) -> io::Result<()> {
cvt(unsafe { libc::kill(-(pgid as pid_t), signal as c_int) }).and(Ok(()))
}
pub fn pipe2(flags: usize) -> io::Result<(RawFd, RawFd)> {
pub(crate) fn pipe2(flags: usize) -> io::Result<(RawFd, RawFd)> {
let mut fds = [0; 2];
#[cfg(not(target_os = "macos"))]
......@@ -44,11 +44,11 @@ pub fn pipe2(flags: usize) -> io::Result<(RawFd, RawFd)> {
Ok((fds[0], fds[1]))
}
pub fn setpgid(pid: u32, pgid: u32) -> io::Result<()> {
pub(crate) fn setpgid(pid: u32, pgid: u32) -> io::Result<()> {
cvt(unsafe { libc::setpgid(pid as pid_t, pgid as pid_t) }).and(Ok(()))
}
pub fn signal(signal: i32, handler: extern "C" fn(i32)) -> io::Result<()> {
pub(crate) fn signal(signal: i32, handler: extern "C" fn(i32)) -> io::Result<()> {
if unsafe { libc::signal(signal as c_int, handler as sighandler_t) } == libc::SIG_ERR {
Err(io::Error::last_os_error())
} else {
......@@ -56,7 +56,7 @@ pub fn signal(signal: i32, handler: extern "C" fn(i32)) -> io::Result<()> {
}
}
pub fn reset_signal(signal: i32) -> io::Result<()> {
pub(crate) fn reset_signal(signal: i32) -> io::Result<()> {
if unsafe { libc::signal(signal as c_int, libc::SIG_DFL) } == libc::SIG_ERR {
Err(io::Error::last_os_error())
} else {
......@@ -64,17 +64,17 @@ pub fn reset_signal(signal: i32) -> io::Result<()> {
}
}
pub fn tcsetpgrp(fd: RawFd, pgrp: u32) -> io::Result<()> {
pub(crate) fn tcsetpgrp(fd: RawFd, pgrp: u32) -> io::Result<()> {
cvt(unsafe { libc::tcsetpgrp(fd as c_int, pgrp as pid_t) }).and(Ok(()))
}
pub fn dup(fd: RawFd) -> io::Result<RawFd> { cvt(unsafe { libc::dup(fd) }) }
pub(crate) fn dup(fd: RawFd) -> io::Result<RawFd> { cvt(unsafe { libc::dup(fd) }) }
pub fn dup2(old: RawFd, new: RawFd) -> io::Result<RawFd> { cvt(unsafe { libc::dup2(old, new) }) }
pub(crate) fn dup2(old: RawFd, new: RawFd) -> io::Result<RawFd> { cvt(unsafe { libc::dup2(old, new) }) }
pub fn close(fd: RawFd) -> io::Result<()> { cvt(unsafe { libc::close(fd) }).and(Ok(())) }
pub(crate) fn close(fd: RawFd) -> io::Result<()> { cvt(unsafe { libc::close(fd) }).and(Ok(())) }
pub fn isatty(fd: RawFd) -> bool { unsafe { libc::isatty(fd) == 1 } }
pub(crate) fn isatty(fd: RawFd) -> bool { unsafe { libc::isatty(fd) == 1 } }
// Support functions for converting libc return values to io errors {
trait IsMinusOne {
......@@ -103,7 +103,7 @@ fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
pub mod signals {
/// Blocks the SIGTSTP/SIGTTOU/SIGTTIN/SIGCHLD signals so that the shell never receives
/// them.
pub fn block() {
pub(crate) fn block() {
unsafe {
use libc::*;
use std::mem;
......@@ -121,7 +121,7 @@ pub mod signals {
/// Unblocks the SIGTSTP/SIGTTOU/SIGTTIN/SIGCHLD signals so children processes can be
/// controlled
/// by the shell.
pub fn unblock() {
pub(crate) fn unblock() {
unsafe {
use libc::*;
use std::mem;
......@@ -155,7 +155,7 @@ pub mod job_control {
use nix::{Errno, Error};
use nix::sys::signal::Signal;
pub fn watch_background(
pub(crate) fn watch_background(
fg: Arc<ForegroundSignals>,
processes: Arc<Mutex<Vec<BackgroundProcess>>>,
pid: u32,
......@@ -223,7 +223,7 @@ pub mod job_control {
}
}
pub fn watch_foreground<'a, F, D>(
pub(crate) fn watch_foreground<'a, F, D>(
shell: &mut Shell<'a>,
_pid: u32,
last_pid: u32,
......@@ -277,7 +277,7 @@ pub mod variables {
use users_unix::get_user_by_name;
use users_unix::os::unix::UserExt;
pub fn get_user_home(username: &str) -> Option<String> {
pub(crate) fn get_user_home(username: &str) -> Option<String> {
match get_user_by_name(username) {
Some(user) => Some(user.home_dir().to_string_lossy().into_owned()),
None => None,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment