PostScript 执行程序的用户交互
我正在用 PostScript 构建一个应用程序,需要在提示时从用户那里获取输入(我将使用 GhostScript 执行程序,并且文件不会发送到打印机)。我在 PostScript 语言参考手册中看不到任何表明这是可能的内容,而且我不想回到执行人员,那么这可能吗?
I'm building an application in PostScript that needs to take input fom the user at a prompt (I will be using the GhostScript executive, and the file won't be sent to the printer). I can't see anything in my PostScript Language Reference Manual that suggests this is possible, and I don't want to drop back to the executive, so is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并不是那么绝望!但这也并不容易。除了 %stdin 之外,还有两个特殊文件可供您读取。
(%lineedit)(r)file dup bytesavailable string readstring pop
将从标准输入读取一行到字符串中。还有(%statementedit)
文件,该文件将一直读取,直到键入语法上有效的后记片段(加上换行符)。这将匹配括号和花括号,但不匹配方括号。在阅读之前,您应该发出类似(> )printlush
的提示。另一件事是,您可以通过将所有这些包装在
stopped
上下文中来捕获 ^D。您可以使用它来廉价地检测空输入行,而不是在读取字符串后弹出该布尔值。另请注意,在 EOF 上触发的停止来自
file
中的错误(ghostscript 将其称为/invalidfilename
),但尽管定义了file
作为一个操作符,操作符应该在发出错误信号时将它们的参数推回到堆栈上,我注意到 Ghostscript 不一定会这样做(我忘记了它留下了什么,但它不是像你期望的那样 2 个字符串) ,所以你可能想把mark
在前面,cleartomark pop
在整个块后面。特殊文件
(%lineedit)
和(%statementedit)
(如果可用)将成功处理退格键和 control-U 以及可能的其他控件。我相信真正的 Adobe 打印机会用某种状态消息响应 ^T。但我从来没有见过。附言。 :!
我的 后记调试器。这是一个更好的版本的调试器,但它可能不太适合用作 例子。
It's not that hopeless! But it ain't exactly easy, either. There are two other special files besides %stdin that you can read from.
(%lineedit)(r)file dup bytesavailable string readstring pop
will read a line from stdin into a string. There is also the(%statementedit)
file which will read until a syntactically valid postscript fragment is typed (plus newline). This will match parentheses and curlies, but not square brackets. Before reading, you should issue a prompt like(> )print flush
.One more thing, you can catch ^D by wrapping all this in a
stopped
context.Instead of popping that bool after readstring, you could use it to cheaply detect an empty input line. Note also, that the stop that triggers on EOF is from an error in
file
(ghostscript calls it/invalidfilename
), but althoughfile
is defined as an operator, and operators are supposed to push their arguments back on the stack when signaling an error, I've noticed ghostscript doesn't necessarily do this (I forget what it leaves, but it's not 2 strings like you'd expect), so you might want to putmark
in front andcleartomark pop
after this whole block.The special files
(%lineedit)
and(%statementedit)
, if available, will successfully process backspaces and control-U and possibly other control. I believe real Adobe printers will respond to ^T with some kind of status message. But I've never seen it.PS. :!
I've got a more extensive example of interactive postscript in my postscript debugger. Here's a better version of the debugger, but it's probably less useable as an example.
PostScript 并非设计为交互式语言,因此对于用户输入没有很好的规定。
您可以从 stdin 读取输入,并且可以写入 stdout 或 stderr,因此如果它们连接到控制台,那么理论上您可以通过写入 stdout 来提示用户输入,然后从 stdin 读回输入。
请注意,从标准输入读取不允许用户执行诸如退格错误之类的操作。至少在视觉上不是这样,数据将被发送到可以处理退格字符的 PostScript 程序。
这是我能想到的实现这一目标的唯一方法。
PostScript isn't designed as an interactive language, so there is no great provision for user input.
You can read input from stdin, and you cat write to stdout or stderr, so if those are wired up to a console then you can theoretically prompt the user for input by writing to stdout, and read the input back from stdin.
Note that reading from stdin won't allow the user to do things like backspace over errors. At least not visually, the data will be sent to your PostScript program which could process the backspace characters.
That's about the only way to achieve this that I can think of though.