Capturing user input. ;P

daniel daniel <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Message-ID <CAF5U=udrVSbKHuo1ra9-_QF22Ew_jhWTKxhpt+8bEeSYBpJu3g@mail.gmail.com>
Here is a neat m4 hack I've come up with to capture user input from within
m4.

There are two variations I propose to capture user input:

1. The first method captures user input from the shell, exploiting pipelines
from the shell and how this relates to /dev/fd/0:
Code:
dnl call this file m4stdin.m4
pushdef(userParam,`include(/dev/fd/0)')
eval(userParam+1)

Discussion:
Given the shell command: { echo 1|m4 m4stdin.m4;}
the output will be: 2

2. The second method exploits the `read' shell builtin command:
Code:
changequote([,])
define(askUser,enter A or B: [pushdef(userParam,esyscmd({ read a;echo
$a;}))dnl
ifelse(userParam,[A
],[include(110721m4a.m4)],dnl
ifelse(userParam,[B
],[include(110721m4b.m4)]))][popdef([userParam])])dnl
askUser()

Discussion:
on running this code, the user will be prompted with the text: "enter A or
B: "
the user will then type either an A or a B; then, in this example, i've
implemented
conditional file inclusion (i.e., if the user inputs A, then 110721m4a.m4
will be
evaluated; if the user inputs B, then 110721m4b.m4 will be included and
evaluated.)



;P
Daniel.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.