# -*- rexx -*- # hello world # print a simple string on stdout say "Hello World" # argv # access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument) say ARG(1) # env # access environment variable say VALUE("HOME", , SYSTEM) # test file exists # return exit code error (non zero) if a file does not exist exit STREAM(ARG(1), 'C', 'QUERY EXISTS') == "" # test file readable # return exit code error (non zero) if a file is not readable exit \STREAM(ARG(1), 'C', 'READABLE') # formatting # print integers in a simple formatted string a = 1 ; b = 2 ; say a "+" b "=" a + b # system # call an external program and check the return value address SYSTEM "false" if RC == 0 then ; call LINEOUT 'STDERR', "false failed" address SYSTEM "echo done" # sed in place # remove #-comments from a file (modifying the file, i.e. in place) call rxFuncAdd 'sysLoadFuncs', 'rexxUtil', 'sysLoadFuncs' call sysLoadFuncs fin = ARG(1) ; fout = RANDOM() || ".tmp" do while LINES(fin) > 0 call LINEOUT fout, CHANGESTR("#", LINEIN(fin), "") end call STREAM fin, 'C', 'CLOSE' ; call STREAM fout, 'C', 'CLOSE' call sysFileDelete fin ; call sysMoveObject fout, fin call sysDropFuncs # compile what must be # find and compile .c files into .o when the .o is old or absent call rxFuncAdd 'sysLoadFuncs', 'rexxUtil', 'sysLoadFuncs' call sysLoadFuncs call sysFileTree '*.c', 'cfilelist.', 'sf' do i = 1 for cfilelist.0 cfilelist.i = WORD(cfilelist.i, 5) ofile = LEFT(cfilelist.i, LASTPOS(".c", cfilelist.i) - 1) || ".o" otime = STREAM(ofile, 'C', 'QUERY TIMESTAMP') if otime < STREAM(cfilelist.i, 'C', 'QUERY TIMESTAMP') then do say "compiling" cfilelist.i "to" ofile address SYSTEM "gcc -c -o" cfilelist.i ofile end end call sysDropFuncs # grep # grep with -F -i -h handling, usage, grep'ing many files call rxFuncAdd 'reLoadFuncs', 'rexxRE', 'reLoadFuncs' call reLoadFuncs cmdline = ARG(1) ; opt = "" ; regexp = "" ; filelist = "" do while cmdline <> "" parse var cmdline token cmdline if LEFT(token, 1) == "-" then if TRANSLATE(token) == "-H" | VERIFY(token, "-iF") \= 0 then call usage ; else ; opt = opt || CHANGESTR("-", token, "") else ; if regexp == "" then regexp = token else filelist = filelist token end /* 'F' not recognised by 'reComp'; just remove it */ opt = CHANGESTR("F", opt, "") if POS("i", opt) > 0 then ; opt = "" ; else ; opt = "c" cre = reComp(regexp, opt) do while filelist <> "" parse var filelist file filelist do while LINES(file) > 0 if reExec(cre, LINEIN(file), 'match.') then call LINEOUT , file || ":" || match.!match end end call reFree cre ; call reDropFuncs ; exit 0 usage : say "usage: grep [-h] | [-F|-i] regexp [files...]" call reDropFuncs ; exit 1