This assumes the format of the token list is
{..."..."...}
The \normalize
macro receives as a mandatory argument the token list to normalize, possibly in “stored” form. The optional argument should be a control sequence that will receive the normalized string as replacement text (no check about it being defined is performed). If no optional argument is given, the mandatory argument should be a control sequence storing the string, which will be redefined.
\documentclass{article}
\makeatletter
\newcommand{\normalize}{\@dblarg\norm@lize}
\def\norm@lize[#1]#2{%
\expandafter\expandafter\expandafter
\remove@quotes
\expandafter
\@firstofone#2\remove@quotes{#1}%
}
% remove the last #3 in the following if you want to remove
% the tokens after the second quote symbol
\def\remove@quotes#1"#2"#3\remove@quotes#4{\def#4{#1#2#3}}
\makeatother
\def\mycommand{{"/home/user/di rect ory/"/}}
\normalize[\tmp]{\mycommand}\show\tmp
\normalize{\mycommand}\show\mycommand
\normalize[\another]{{"/home/user/di rect ory/"/}}\show\another
\stop
Here's the output on the terminal:
> \tmp=macro:
->/home/user/di rect ory//.
l.18 \normalize[\tmp]{\mycommand}\show\tmp
?
> \mycommand=macro:
->/home/user/di rect ory//.
l.20 \normalize{\mycommand}\show\mycommand
?
> \another=macro:
->/home/user/di rect ory//.
l.22 ...{"/home/user/di rect ory/"/}}\show\another
?
If the code \def#4{#1#2#3}
is changed to \def#4{#1#2}
, the tokens after the closing quote are discarded and the output on the terminal is
> \tmp=macro:
->/home/user/di rect ory/.
l.18 \normalize[\tmp]{\mycommand}\show\tmp
?
> \mycommand=macro:
->/home/user/di rect ory/.
l.20 \normalize{\mycommand}\show\mycommand
?
> \another=macro:
->/home/user/di rect ory/.
l.22 ...{"/home/user/di rect ory/"/}}\show\another
?
Now that the input format seems to be fully known, here's a different solution for both cases.
\documentclass{article}
\makeatletter
\newcommand\extractrealpath{\@dblarg\extract@real@path}
\def\extract@real@path[#1]#2{%
% remove the braces and stringify the quotes
\edef\erp@temp{\expandafter\@firstofone#2}%
% remove the trailing slash
\expandafter\erp@remove@slash\erp@temp\@nil
\erp@remove@quotes
\let#1\erp@temp
}
\def\erp@remove@slash#1/\@nil{%
\def\erp@temp{#1}%
}
\begingroup\catcode`\"=12
\gdef\erp@remove@quotes{\expandafter\erp@remove@quotes@aux\erp@temp""\@nil}
\gdef\erp@remove@quotes@aux#1"#2"#3\@nil{%
\ifx\hfuzz#2\hfuzz
% no quotes
\def\erp@temp{#1}%
\else
\def\erp@temp{#2}%
\fi
}
\endgroup
\makeatother
\begin{document}
\def\mycommand{{\string "/home/user/di rect ory/\string "/}}
\extractrealpath[\saved]\mycommand
\texttt{\saved}
\def\mycommand{{/home/user/directory//}}
\extractrealpath\mycommand
\texttt{\mycommand}
\end{document}
os << "\\makeatletter\n" << "\\def\\input <at> path{{" << docdir << "/}}\n" << "\\makeatother\n";