Chapter 21. Expect, C, And C++

In the other chapters of this book, I have described how to use Expect with the command-oriented Tcl environment. However, Expect can be used without Tcl. In this chapter, I will describe how to call Expect functions from C by using the Expect library. This library is compatible with C++ as well, and most of the examples are identical between C and C++. For other languages, you are on your own. However, if you know how to call C routines from your favorite language, you should be able to do the same with Expect.

Much of the functions in the library work analogously to their counterparts in the Expect program. Accordingly, it will be very helpful to have some experience with Expect before using the library. Concepts such as spawned processes and glob patterns versus regular expressions are not explained here.

This chapter is not meant to encourage the use of C or C++. Especially for Expect-like programming, working in Tcl is much, much easier than working with C or C++ and their usual edit-compile-debug cycles. Unlike typical compiled programs, most of the debugging of Expect programs is not getting the compiler to accept programs—rather, it is getting the dialogue correct. And this is much faster to do with Tcl.

If you are aware of the trade-offs between C and C++ and Tcl and have good cause to use this library, plow ahead. But if you do not know Tcl and simply want to avoid learning another language, I would suggest taking a step back, reexamining your ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.