From d540eef0805b312640355094a7f3109fd8dfff59 Mon Sep 17 00:00:00 2001 From: Diomidis Spinellis Date: Sat, 22 Mar 2025 15:22:41 +0200 Subject: [PATCH] Create processing context out of CLI options --- LICENSE | 2 +- src/uu/sed/src/command.rs | 28 ++++++++- src/uu/sed/src/sed.rs | 129 +++++++++++++++++++++++++++++++++++++- 3 files changed, 154 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index 6bd7307..c66459c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 uutils +Copyright (c) 2025 Diomidis Spinellis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/uu/sed/src/command.rs b/src/uu/sed/src/command.rs index c395c8f..4e85fa5 100644 --- a/src/uu/sed/src/command.rs +++ b/src/uu/sed/src/command.rs @@ -1,7 +1,10 @@ // Definitions for the compiled code data structures // -// This file is part of the uutils sed package. +// SPDX-License-Identifier: MIT +// Copyright (c) 2025 Diomidis Spinellis // +// This file is part of the uutils sed package. +// It is licensed under the MIT License. // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -13,6 +16,29 @@ use std::collections::HashMap; use std::fs::File; use std::path::PathBuf; // For file descriptors and equivalent +// Compilation and processing context +#[derive(Debug)] +pub struct Context { + // Input file/string and position + pub input: String, + pub line: usize, + + // Command-line flags with corresponding names + pub all_output_files: bool, + pub debug: bool, + pub regexp_extended: bool, + pub follow_symlinks: bool, + pub in_place: bool, + pub in_place_suffix: Option, + pub length: usize, + pub quiet: bool, + pub posix: bool, + pub separate: bool, + pub sandbox: bool, + pub unbuffered: bool, + pub null_data: bool, +} + // The specification of a script: through a string or a file #[derive(Debug, PartialEq)] pub enum ScriptValue { diff --git a/src/uu/sed/src/sed.rs b/src/uu/sed/src/sed.rs index 23e7637..d6f81d3 100644 --- a/src/uu/sed/src/sed.rs +++ b/src/uu/sed/src/sed.rs @@ -1,5 +1,10 @@ -// This file is part of the uutils sed package. +// Program entry point and CLI processing // +// SPDX-License-Identifier: MIT +// Copyright (c) 2025 Diomidis Spinellis +// +// This file is part of the uutils sed package. +// It is licensed under the MIT License. // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -7,7 +12,7 @@ pub mod command; pub mod compiler; pub mod processor; -use crate::command::ScriptValue; +use crate::command::{Context, ScriptValue}; use crate::compiler::compile; use crate::processor::process; use clap::{arg, Arg, ArgMatches, Command}; @@ -96,6 +101,8 @@ fn get_scripts_files(matches: &ArgMatches) -> UResult<(Vec, Vec UResult<()> { let matches = uu_app().try_get_matches_from(args)?; let (scripts, files) = get_scripts_files(&matches)?; + let _context = build_context(&matches); + let executable = compile(scripts)?; process(executable, files)?; Ok(()) @@ -123,7 +130,8 @@ pub fn uu_app() -> Command { .short('E') .long("regexp-extended") .short_alias('r') - .help("Use extended regular expressions."), + .help("Use extended regular expressions.") + .action(clap::ArgAction::SetTrue), arg!(-e --expression