Author Topic: 4OS2: quoting variables  (Read 115 times)

Anton Monroe

  • Newbie
  • *
  • Posts: 27
  • Karma: +3/-0
4OS2: quoting variables
« on: July 22, 2026, 09:59:56 pm »
Some time ago there was a discussion on the 4OS2 bug tracker about the possibility of a @quoted[] function to quote a variable that contains special characters. I managed to get the same effect with a 'quote' and 'unquote' aliases but they only worked most of the time. So today I decided to try to perfect them. After a lot of trial and error with setdos /x I gave up and decided to just let Rexx do it. Note that the argument is the =name= of the variable, not its value.


setlocal
rem  add double quotes to a variable only if it is not already quoted
alias quote=`%@rexx[vv=value('%1',,'os2environment');if left(vv,1) \= '"'|right(vv,1) \= '"' then; %=
  vv = '"'||vv||'"';call value '%1', vv, 'os2environment']`

rem  strip a pair of double quotes from a variable
alias unquote=`%@rexx[vv=value('%1',,'os2environment');if left(vv,1) == '"'&right(vv,1) == '"' then; %=
  vv = substr(vv,2,length(vv)-2);call value '%1', vv, 'os2environment']`

setdos /x-4568
set aa=`<>?/,.\[]|$%^&*()`
echo original:
echo ----- %aa
quote aa
echo quoted by Rexx:
echo ---- %aa
unquote aa
echo unquoted by Rexx:
echo ----- %aa
setdos /x0



Are there any better ideas? Has someone already solved the quote/unquote problem?


Steven Levine

  • Full Member
  • ***
  • Posts: 150
  • Karma: +16/-0
Re: 4OS2: quoting variables
« Reply #1 on: July 24, 2026, 02:43:03 am »
Effective quoting in 4OS2 is non-trivial.  Your REXX solution is good when it suits what the application needs.

When I need to quote a value for use on the shell command lines, I typically use

::=== QuoteXForShell() Quote %X for shell ===

:QuoteXForShell
  :: Quote if has whitespace unless already quoted
  iff %@ascii[%X] ne %@ascii["] then
    iff %@index[%X, ] ne -1 then
      set X="%X"
    elseiff %@index[%X,+] ne -1 then
      set X="%X"
    endiff
  endiff
  return
  :: end QuoteXForShell

This can be extended as needed to detect other special characters.

One of the things that makes quoting difficult is the 4OS2 parsing logic.  It does not completely understand quoted strings.  Some of the limitations can be handled by setdos, but this is cumbersome in subroutines because there is no built-in
way to save and restore current setdos /x setting.

What I usually end up with is something like

  set SAVED=%@execstr[setdos | sed -ne s"/EXPANSION=//p"]
  setdos /x-1456
  ...
  setdos /x0
  setdos /X%SAVED

When I really need robust string processing I generally write the script in REXX.  Like you I have a large library of reusable functions and a tool to update existing code when a library function changes so while the script will be bulky compared to a 4OS2 solution, it's no harder to write.