mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 09:23:44 +00:00
Move the function quote_arg/1, which is used to quote arguments to shell
commands, out of options.m and into its own module. It is called from
several other places other than options.m and its implementation details
have nothing to do with the other contents of options.m.
Rename the function to quote_shell_cmd_arg/1.
compiler/shell_util.m:
New module for quote_arg/1 and its supporting predicates.
compiler/libs.m:
Include the new module.
compiler/options.m:
compiler/compile_target_code.m:
compiler/file_util.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
Import the new module where necessary.
compiler/notes/compiler_design.m:
Update this document.
96 lines
2.9 KiB
Mathematica
96 lines
2.9 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2002-2007, 2015, 2017 The University of Melbourne.
|
|
% Copyright (C) 2022 The Mercury team.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: shell_util.m.
|
|
% Main author: stayl.
|
|
%
|
|
% Utilities for interacting with the shell.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module libs.shell_util.
|
|
:- interface.
|
|
|
|
% Quote an argument to a shell command.
|
|
%
|
|
:- func quote_shell_cmd_arg(string) = string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module char.
|
|
:- import_module dir.
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
quote_shell_cmd_arg(Arg0) = Arg :-
|
|
% XXX Instead of using dir.use_windows_paths, this should really
|
|
% test whether we are using a Unix or Windows shell.
|
|
( if dir.use_windows_paths then
|
|
( if
|
|
( string.contains_match(char.is_whitespace, Arg0)
|
|
; Arg0 = ""
|
|
)
|
|
then
|
|
Arg = """" ++ Arg0 ++ """"
|
|
else
|
|
Arg = Arg0
|
|
)
|
|
else
|
|
ArgList = quote_arg_unix(string.to_char_list(Arg0)),
|
|
(
|
|
ArgList = [],
|
|
Arg = """"""
|
|
;
|
|
ArgList = [_ | _],
|
|
( if
|
|
list.member(Char, ArgList),
|
|
not
|
|
( char.is_alnum_or_underscore(Char)
|
|
; Char = ('-')
|
|
; Char = ('/')
|
|
; Char = ('.')
|
|
; Char = (',')
|
|
; Char = (':')
|
|
)
|
|
then
|
|
Arg = """" ++ string.from_char_list(ArgList) ++ """"
|
|
else
|
|
Arg = string.from_char_list(ArgList)
|
|
)
|
|
)
|
|
).
|
|
|
|
:- func quote_arg_unix(list(char)) = list(char).
|
|
|
|
quote_arg_unix([]) = [].
|
|
quote_arg_unix([Char | Chars0]) = Chars :-
|
|
Chars1 = quote_arg_unix(Chars0),
|
|
( if quote_char_unix(Char) then
|
|
Chars = [('\\'), Char | Chars1]
|
|
else
|
|
Chars = [Char | Chars1]
|
|
).
|
|
|
|
:- pred quote_char_unix(char::in) is semidet.
|
|
|
|
quote_char_unix('\\').
|
|
quote_char_unix('"').
|
|
quote_char_unix('`').
|
|
quote_char_unix('$').
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module libs.shell_util.
|
|
%-----------------------------------------------------------------------------%
|