beginning linux-programming (2)

804

Upload: programming-passion

Post on 18-Jul-2015

63 views

Category:

Engineering


1 download

TRANSCRIPT

  • Table of ContentsBeginning Linux Programming, Second Edition.............................................................................................1

    Foreword..............................................................................................................................................................5

    Introduction.........................................................................................................................................................6 Welcome.................................................................................................................................................6 Who's this Book For?..............................................................................................................................6 What's Covered in the Book...................................................................................................................6 What You Need to Use this Book...........................................................................................................8 Source Code............................................................................................................................................9 Conventions............................................................................................................................................9 Tell Us What You Think.......................................................................................................................10 Why Should I Return the Reply Card?.................................................................................................10

    Chapter 1: Getting Started.............................................................................................................................11 Overview...............................................................................................................................................11 What is UNIX?.....................................................................................................................................11 What is Linux?......................................................................................................................................11 Distributions..........................................................................................................................................11 The GNU Project and the Free Software Foundation...........................................................................12 Programming Linux..............................................................................................................................13 UNIX Programs....................................................................................................................................13

    The C Compiler..............................................................................................................................14Try It Out Our First UNIX C Program........................................................................................14How It Works..................................................................................................................................15

    Getting Help..........................................................................................................................................15Try It Out Manual Pages and info................................................................................................16

    Development System Roadmap............................................................................................................17 Programs.........................................................................................................................................17 Header Files....................................................................................................................................18 Library Files...................................................................................................................................19 Static Libraries...............................................................................................................................19Try It Out Static Libraries............................................................................................................20 Shared Libraries.............................................................................................................................22

    UNIX Philosophy.................................................................................................................................23 Simplicity.......................................................................................................................................23 Focus..............................................................................................................................................23 Reusable Components....................................................................................................................23 Filters..............................................................................................................................................23 Open File Formats..........................................................................................................................23 Flexibility.......................................................................................................................................23

    Summary...............................................................................................................................................24

    Chapter 2: Shell Programming.......................................................................................................................25 Overview...............................................................................................................................................25 What is a Shell?....................................................................................................................................26 Pipes and Redirection...........................................................................................................................27

    Redirecting Output.........................................................................................................................27 Redirecting Input............................................................................................................................28 Pipes...............................................................................................................................................28

    i

  • Table of Contents Chapter 2: Shell Programming

    The Shell as a Programming Language................................................................................................29 Interactive Programs.......................................................................................................................29 Creating a Script.............................................................................................................................30 Making a Script Executable...........................................................................................................31

    Shell Syntax..........................................................................................................................................32 Variables.........................................................................................................................................33 Conditions......................................................................................................................................36 Control Structures...........................................................................................................................38 Functions........................................................................................................................................49Try It Out A Simple Function......................................................................................................50How It Works..................................................................................................................................50Try It Out Returning a Value.......................................................................................................51How It Works..................................................................................................................................52 Commands......................................................................................................................................52 Command Execution......................................................................................................................62 Here Documents.............................................................................................................................66Try It Out Using Here Documents...............................................................................................66Try It Out Another Use for a Here Document.............................................................................67How It Works..................................................................................................................................67 Debugging Scripts..........................................................................................................................67

    Putting it All Together..........................................................................................................................68 Requirements..................................................................................................................................68 Design.............................................................................................................................................69Try It Out A CD Application.......................................................................................................70Notes................................................................................................................................................76

    Summary...............................................................................................................................................77

    Chapter 3: Working with Files.......................................................................................................................78 Overview...............................................................................................................................................78 UNIX File Structure..............................................................................................................................78

    Directories......................................................................................................................................79 Files and Devices............................................................................................................................79

    System Calls and Device Drivers.........................................................................................................81 Library Functions..................................................................................................................................82 Lowlevel File Access..........................................................................................................................82

    write.................................................................................................................................................83read..................................................................................................................................................83open.................................................................................................................................................84Initial Permissions...........................................................................................................................85umask...............................................................................................................................................86close.................................................................................................................................................87ioctl..................................................................................................................................................87Try It Out A File Copy Program..................................................................................................87 Other System Calls for Managing Files.........................................................................................89

    The Standard I/O Library......................................................................................................................91fopen................................................................................................................................................92fread.................................................................................................................................................92fwrite...............................................................................................................................................93fclose...............................................................................................................................................93

    ii

  • Table of Contents Chapter 3: Working with Files

    fflush................................................................................................................................................93fseek.................................................................................................................................................93fgetc, getc, getchar...........................................................................................................................94fputc, putc, putchar..........................................................................................................................94fgets, gets.........................................................................................................................................94 Formatted Input and Output...........................................................................................................95 Other Stream Functions..................................................................................................................98 Try It Out Another File Copy Program.......................................................................................99 Stream Errors..................................................................................................................................99 Streams and File Descriptors........................................................................................................100

    File and Directory Maintenance..........................................................................................................101chmod............................................................................................................................................101chown............................................................................................................................................101unlink, link, symlink......................................................................................................................101mkdir, rmdir...................................................................................................................................102chdir, getcwd.................................................................................................................................102

    Scanning Directories...........................................................................................................................103opendir...........................................................................................................................................103readdir............................................................................................................................................103telldir.............................................................................................................................................104seekdir...........................................................................................................................................104closedir..........................................................................................................................................104 Try It Out A Directory Scanning Program................................................................................105How It Works................................................................................................................................106

    Errors...................................................................................................................................................107 Advanced Topics................................................................................................................................107

    fcntl................................................................................................................................................108mmap.............................................................................................................................................109 Try It Out Using mmap.............................................................................................................110

    Summary.............................................................................................................................................111

    Chapter 4: The UNIX Environment............................................................................................................112 Overview.............................................................................................................................................112 Program Arguments............................................................................................................................112

    Try It Out Program Arguments.................................................................................................113How It Works................................................................................................................................114 getopt............................................................................................................................................114 Try It Out getopt.......................................................................................................................115How It Works................................................................................................................................116

    Environment Variables.......................................................................................................................116Try It Out getenv and putenv0...................................................................................................117 Use of Environment Variables.....................................................................................................118 The environ Variable....................................................................................................................119Try It Out environ......................................................................................................................119How It Works................................................................................................................................119

    Time and Date.....................................................................................................................................119Try It Out time...........................................................................................................................120How It Works................................................................................................................................121 Try It Out gmtime.....................................................................................................................121

    iii

  • Table of Contents Chapter 4: The UNIX Environment

    How It Works................................................................................................................................122 Try It Out ctime.........................................................................................................................123How It Works................................................................................................................................123 Try It Out strftime and strptime................................................................................................125How It Works................................................................................................................................126

    Temporary Files..................................................................................................................................126 Try It Out tmpnam and tmpfile.................................................................................................127How It Works................................................................................................................................127

    User Information.................................................................................................................................128Try It Out User Information.......................................................................................................129How It Works................................................................................................................................130 Other User Information Functions...............................................................................................130

    Host Information.................................................................................................................................131Try It Out Host Information.......................................................................................................131How It Works................................................................................................................................132 Licensing......................................................................................................................................132

    Logging...............................................................................................................................................132Try It Out syslog........................................................................................................................134How It Works................................................................................................................................134 Configuring Logs.........................................................................................................................134Try It Out logmask.....................................................................................................................135How It Works................................................................................................................................136

    Resources and Limits..........................................................................................................................136 Try It Out Resource Limits.......................................................................................................138How It Works................................................................................................................................140

    Summary.............................................................................................................................................140

    Chapter 5: Terminals....................................................................................................................................141 Overview.............................................................................................................................................141 Reading from and Writing to the Terminal.........................................................................................141

    Try It Out Menu Routines in C..................................................................................................141How It Works................................................................................................................................142Why It Doesn't Quite Work...........................................................................................................143 Handling Redirected Output.........................................................................................................144Try It Out Checking for Output Redirection..............................................................................144How It Works................................................................................................................................145 Talking to the Terminal................................................................................................................145Try It Out Using /dev/tty............................................................................................................146

    The Terminal Driver and the General Terminal Interface..................................................................147 Overview......................................................................................................................................147 Hardware Model...........................................................................................................................148

    The termios Structure..........................................................................................................................149 Input Modes..................................................................................................................................150 Output Modes...............................................................................................................................151 Control Modes..............................................................................................................................152 Local Modes.................................................................................................................................152 Special Control Characters...........................................................................................................153 Terminal Speed.............................................................................................................................156 Additional Functions....................................................................................................................156

    iv

  • Table of Contents Chapter 5: Terminals

    Try It Out A Password Program with termios...........................................................................157How It Works................................................................................................................................158Try It Out Reading Each Character............................................................................................158How It Works................................................................................................................................159

    Terminal Output..................................................................................................................................159 Terminal Type..............................................................................................................................159 Identify Your Terminal Type.......................................................................................................160 Using terminfo Capabilities..........................................................................................................162

    Detecting Keystrokes..........................................................................................................................167Try It Out Your Very Own kbhit...............................................................................................167How It Works................................................................................................................................169 Pseudo Terminals.........................................................................................................................169

    Summary.............................................................................................................................................169

    Chapter 6: Curses..........................................................................................................................................170 Overview.............................................................................................................................................170 Compiling with curses........................................................................................................................170 Concepts..............................................................................................................................................171

    Try It Out A Simple curses Program.........................................................................................172 Initialization and Termination.............................................................................................................173 Output to the Screen............................................................................................................................173 Reading from the Screen.....................................................................................................................174 Clearing the Screen.............................................................................................................................175 Moving the Cursor..............................................................................................................................175 Character Attributes............................................................................................................................175

    Try It Out Moving, Inserting and Attributes..............................................................................176 The Keyboard......................................................................................................................................177

    Keyboard Modes..........................................................................................................................177 Keyboard Input.............................................................................................................................178Try It Out Keyboard Modes and Input........................................................................................178How It Works................................................................................................................................179

    Windows.............................................................................................................................................180 The WINDOW Structure..............................................................................................................180 Generalized Functions..................................................................................................................180 Moving and Updating a Window.................................................................................................181Try It Out Multiple Windows....................................................................................................182 Optimizing Screen Refreshes.......................................................................................................184

    Subwindows........................................................................................................................................185Try It Out Subwindows..............................................................................................................185How It Works................................................................................................................................187

    The Keypad.........................................................................................................................................187 Try It Out Using the Keypad.....................................................................................................188

    Color...................................................................................................................................................189 Try It Out Colors.......................................................................................................................190 Redefining Colors.........................................................................................................................191

    Pads.....................................................................................................................................................191Try It Out Using a Pad...............................................................................................................192

    The CD Collection Application..........................................................................................................193Try It Out A New CD Collection Application...........................................................................194

    v

  • Table of Contents Chapter 6: Curses

    Try It Out Looking at main........................................................................................................196Try It Out The Menu..................................................................................................................196 Try It Out Database File Manipulation.....................................................................................198Try It Out Querying the CD Database.......................................................................................202

    Summary.............................................................................................................................................206

    Chapter 7: Data Management......................................................................................................................207 Overview.............................................................................................................................................207 Managing Memory..............................................................................................................................207

    Simple Memory Allocation..........................................................................................................207Try It Out Simple Memory Allocation......................................................................................208How It Works................................................................................................................................208 Allocating Lots of Memory..........................................................................................................208 Try It Out Asking for all Physical Memory..............................................................................209How It Works................................................................................................................................209Try It Out Available Memory....................................................................................................210How It Works................................................................................................................................210 Abusing Memory..........................................................................................................................211Try It Out Abuse Your Memory................................................................................................211How It Works................................................................................................................................212 The Null Pointer...........................................................................................................................212 Try It Out Accessing a Null Pointer..........................................................................................212How It Works................................................................................................................................213How It Works................................................................................................................................213 Freeing Memory...........................................................................................................................213Try It Out Freeing Memory.......................................................................................................214How It Works................................................................................................................................214 Other Memory Allocation Functions...........................................................................................214

    File Locking........................................................................................................................................215 Creating Lock Files......................................................................................................................215Try It Out Creating a Lock File.................................................................................................216How It Works................................................................................................................................216 Try It Out Cooperative Lock Files............................................................................................217How It Works................................................................................................................................218 Locking Regions...........................................................................................................................218 Use of read and write with Locking.............................................................................................221Try It Out Locking a File with fcntl...........................................................................................221How It Works................................................................................................................................222Try It Out Testing Locks on a File.............................................................................................223How It Works................................................................................................................................225 Competing Locks.........................................................................................................................226 Try It Out Competing Locks.....................................................................................................226How It Works................................................................................................................................228 Other Lock Commands................................................................................................................228 Deadlocks.....................................................................................................................................229

    Databases............................................................................................................................................229 The dbm Database........................................................................................................................229 The dbm Routines........................................................................................................................230 dbm Access Functions..................................................................................................................232

    vi

  • Table of Contents Chapter 7: Data Management

    Additional dbm Functions............................................................................................................235 The CD Application............................................................................................................................237

    The CD Application Using dbm...................................................................................................238Try It Out cd_data.h...................................................................................................................238Try It Out app_ui.c.....................................................................................................................239 Try It Out cd_access.c...............................................................................................................247

    Summary.............................................................................................................................................253

    Chapter 8: Development Tools.....................................................................................................................254 Overview.............................................................................................................................................254 Problems of Multiple Source Files.....................................................................................................254 The make Command and Makefiles...................................................................................................255

    The Syntax of Makefiles..............................................................................................................255 Options and Parameters to make..................................................................................................255 Comments in a makefile...............................................................................................................258 Macros in a makefile....................................................................................................................258Try It Out A Makefile with Macros...........................................................................................259How It Works................................................................................................................................259 Multiple Targets...........................................................................................................................260Try It Out Multiple Targets........................................................................................................260How It Works................................................................................................................................262 Builtin Rules..............................................................................................................................262 Suffix Rules..................................................................................................................................263Try It Out Suffix Rules..............................................................................................................263How It Works................................................................................................................................264 Managing Libraries with make.....................................................................................................264Try It Out Managing a Library..................................................................................................264How It Works................................................................................................................................265 Advanced Topic: Makefiles and Subdirectories..........................................................................266 GNU make and gcc......................................................................................................................266Try It Out gcc MM...................................................................................................................267How It Works................................................................................................................................267

    Source Code Control...........................................................................................................................267 RCS..............................................................................................................................................267 SCCS............................................................................................................................................273 CVS..............................................................................................................................................274

    Writing a Manual Page.......................................................................................................................278 Distributing Software..........................................................................................................................281

    The patch Program.......................................................................................................................281 Other Distribution Utilities...........................................................................................................283

    Summary.............................................................................................................................................285

    Chapter 9: Debugging....................................................................................................................................286 Types of Error.....................................................................................................................................286

    Specification Errors......................................................................................................................286 Design Errors................................................................................................................................286 Coding Errors...............................................................................................................................286

    General Debugging Techniques..........................................................................................................287 A Program with Bugs...................................................................................................................287

    vii

  • Table of Contents Chapter 9: Debugging

    Code Inspection............................................................................................................................289 Instrumentation.............................................................................................................................290Try It Out Debug Information....................................................................................................291How It Works................................................................................................................................291 Controlled Execution....................................................................................................................292

    Debugging with gdb............................................................................................................................293 Starting gdb..................................................................................................................................293 Running a Program.......................................................................................................................294 Stack Trace...................................................................................................................................294 Examining Variables....................................................................................................................295 Listing the Program......................................................................................................................296 Setting Breakpoints......................................................................................................................296 Patching with the Debugger.........................................................................................................299 Learning more about gdb.............................................................................................................300

    More Debugging Tools.......................................................................................................................300 Lint: Removing the Fluff from Your Programs...........................................................................301 Function Call Tools......................................................................................................................302 Execution Profiling.......................................................................................................................304

    Assertions............................................................................................................................................304 Problems with assert.....................................................................................................................305 Try It Out assert........................................................................................................................305How It Works................................................................................................................................306

    Memory Debugging............................................................................................................................306 ElectricFence................................................................................................................................307Try It Out ElectricFence............................................................................................................307How It Works................................................................................................................................308 Checker.........................................................................................................................................308Try It Out Checker.....................................................................................................................308How It Works................................................................................................................................309

    Resources............................................................................................................................................310 Summary.............................................................................................................................................310

    Chapter 10: Processes and Signals...............................................................................................................311 Overview.............................................................................................................................................311 What is a Process?..............................................................................................................................311 Process Structure.................................................................................................................................311

    The Process Table........................................................................................................................313 Viewing Processes........................................................................................................................313 System Processes..........................................................................................................................314 Process Scheduling.......................................................................................................................315

    Starting New Processes.......................................................................................................................316Try It Out system.......................................................................................................................316How It Works................................................................................................................................317Replacing a Process Image............................................................................................................317 Try It Out execlp.......................................................................................................................318How It Works................................................................................................................................319Duplicating a Process Image.........................................................................................................319 Try It Out fork...........................................................................................................................320How It Works................................................................................................................................321

    viii

  • Table of Contents Chapter 10: Processes and Signals

    Waiting for a Process...................................................................................................................321Try It Out wait............................................................................................................................322How It Works................................................................................................................................323 Zombie Processes.........................................................................................................................323Try It Out Zombies....................................................................................................................324How It Works................................................................................................................................324 Input and Output Redirection.......................................................................................................325 Try It Out Redirection...............................................................................................................325How It Works................................................................................................................................326 Threads.........................................................................................................................................326

    Signals.................................................................................................................................................326 Try It Out Signal Handling.......................................................................................................328How It Works................................................................................................................................329 Sending Signals............................................................................................................................330Try It Out An Alarm Clock........................................................................................................330How It Works................................................................................................................................331 Signal Sets....................................................................................................................................334

    Summary.............................................................................................................................................337

    Chapter 11: POSIX Threads.........................................................................................................................338 What is a Thread?...............................................................................................................................338

    Advantages and Drawbacks of Threads.......................................................................................338 Checking for Thread Support..............................................................................................................339

    Try it out POSIX compliance test..............................................................................................339How it works.................................................................................................................................340

    A First Threads Program.....................................................................................................................340Try it out a simple threaded program.........................................................................................342How it works.................................................................................................................................343

    Simultaneous Execution.....................................................................................................................344Try it out simultaneous execution of two threads......................................................................344How it works.................................................................................................................................345

    Synchronization..................................................................................................................................345 Synchronization with Semaphores...............................................................................................345Try it out a thread semaphore....................................................................................................347How it works.................................................................................................................................349 Synchronization with Mutexes.....................................................................................................350Try it out a thread mutex............................................................................................................350How it works.................................................................................................................................352

    Thread Attributes................................................................................................................................353detachedstate.................................................................................................................................354schedpolicy....................................................................................................................................354schedparam....................................................................................................................................355inheritsched...................................................................................................................................355scope..............................................................................................................................................355stacksize.........................................................................................................................................355Try it out setting the detached state attribute.............................................................................355How it works.................................................................................................................................356Thread Attributes Scheduling....................................................................................................357Try is out scheduling..................................................................................................................357

    ix

  • Table of Contents Chapter 11: POSIX Threads

    How it works.................................................................................................................................357 Canceling a Thread.............................................................................................................................357

    Try it out canceling a thread......................................................................................................358How it works.................................................................................................................................360

    Threads in Abundance........................................................................................................................360Try it out many threads..............................................................................................................360How it works.................................................................................................................................362

    Summary.............................................................................................................................................363

    Chapter 12: Interprocess Communication: Pipes....................................................................................364 Overview.............................................................................................................................................364 What is a Pipe?....................................................................................................................................364 Process Pipes.......................................................................................................................................365

    popen.............................................................................................................................................365pclose.............................................................................................................................................365 Try It Out Reading Output From an External Program............................................................365How It Works................................................................................................................................366 Sending Output to popen..............................................................................................................366Try It Out Sending Output to an External Program...................................................................366How It Works................................................................................................................................367

    The Pipe Call......................................................................................................................................369Try It Out The pipe Function.....................................................................................................370How It Works................................................................................................................................371 Try It Out Pipes across a fork...................................................................................................371How It Works................................................................................................................................372

    Parent and Child Processes.................................................................................................................372Try It Out Pipes and exec...........................................................................................................372How It Works................................................................................................................................373 Reading Closed Pipes...................................................................................................................374 Pipes Used as Standard Input and Output....................................................................................374

    Named Pipes: FIFOs...........................................................................................................................377Try It Out Creating a Named Pipe.............................................................................................378How It Works................................................................................................................................378 Accessing a FIFO.........................................................................................................................378 Try It Out Accessing a FIFO File.............................................................................................379How It Works................................................................................................................................379

    Advanced Topic: Client/Server using FIFOs......................................................................................385 Try It Out An Example Client/Server Application...................................................................385How It Works................................................................................................................................388

    The CD Application............................................................................................................................388 Aims.............................................................................................................................................389 Implementation.............................................................................................................................390Try It Out The Header File, cliserv.h.........................................................................................392 Client Interface Functions............................................................................................................393Try It Out The Client's Interpreter.............................................................................................393 The Server Interface.....................................................................................................................399Try It Out server.c......................................................................................................................399 The Pipe........................................................................................................................................402Try It Out Pipes Implementation Header...................................................................................402

    x

  • Table of Contents Chapter 12: Interprocess Communication: Pipes

    Application Summary...................................................................................................................407 Summary.............................................................................................................................................407

    Chapter 13: Semaphores, Message Queues and Shared Memory.............................................................409 Semaphores.........................................................................................................................................409

    Semaphore Definition...................................................................................................................410 A Theoretical Example.................................................................................................................410 UNIX Semaphore Facilities.........................................................................................................411 Using Semaphores........................................................................................................................413Try It Out Semaphores...............................................................................................................414How It Works................................................................................................................................416 Semaphore Summary...................................................................................................................417

    Shared Memory...................................................................................................................................417 Overview......................................................................................................................................417 Shared Memory Functions...........................................................................................................418 Shared Memory Summary............................................................................................................423

    Message Queues..................................................................................................................................423 Overview......................................................................................................................................424 Message Queue Functions............................................................................................................424 Message Queue Summary............................................................................................................429

    The Application..................................................................................................................................429Try It Out Revising the Server Functions..................................................................................429Try It Out Revising the Client Functions...................................................................................431

    IPC Status Commands........................................................................................................................433 Semaphores..................................................................................................................................433 Shared Memory............................................................................................................................433 Message Queues...........................................................................................................................433

    Summary.............................................................................................................................................434

    Chapter 14: Sockets.......................................................................................................................................435 Overview.............................................................................................................................................435 What is a Socket?................................................................................................................................435 Socket Connections.............................................................................................................................435

    Try It Out A Simple Local Client..............................................................................................436 Try It Out A Simple Local Server.............................................................................................437 Socket Attributes..........................................................................................................................439 Creating a Socket..........................................................................................................................441 Socket Addresses..........................................................................................................................442 Naming a Socket..........................................................................................................................442 Creating a Socket Queue..............................................................................................................443 Accepting Connections.................................................................................................................443 Requesting Connections...............................................................................................................444 Closing a Socket...........................................................................................................................445 Socket Communications...............................................................................................................445Try It Out Network Client..........................................................................................................446How It Works................................................................................................................................446 Try It Out Network Server........................................................................................................446How It Works................................................................................................................................447 Host and Network Byte Ordering.................................................................................................447

    xi

  • Table of Contents Chapter 14: Sockets

    Network Information..........................................................................................................................449 Try It Out Network Information...............................................................................................450How It Works................................................................................................................................451 Try It Out Connecting to a Standard Service............................................................................452How It Works................................................................................................................................453 The Internet Daemon....................................................................................................................453 Socket Options.............................................................................................................................454

    Multiple Clients..................................................................................................................................454Try It Out A Server for Multiple Clients...................................................................................455How It Works................................................................................................................................457

    select...................................................................................................................................................457Try It Out select.........................................................................................................................458How It Works................................................................................................................................460 Multiple Clients............................................................................................................................460Try It Out An Improved Multiple Client/Server........................................................................460

    Summary.............................................................................................................................................463

    Chapter 15: Tcl: Tool Command Language...............................................................................................464 Overview.............................................................................................................................................464 A Tcl Overview...................................................................................................................................464

    Our First Tcl Program..................................................................................................................464 Tcl Commands.............................................................................................................................465 Variables and Values....................................................................................................................466 Quoting and Substitution..............................................................................................................467 Calculation....................................................................................................................................470 Control Structures.........................................................................................................................471 Error Handling..............................................................................................................................473 String Operations..........................................................................................................................474 Arrays...........................................................................................................................................479 Lists..............................................................................................................................................481 Procedures....................................................................................................................................486 Try It Out Procedures................................................................................................................486How It Works................................................................................................................................487 Input and Output...........................................................................................................................487

    A Tcl Program.....................................................................................................................................491Try It Out A Concordance Program...........................................................................................491How It Works................................................................................................................................493Network Support...........................................................................................................................493 Try It Out socket.......................................................................................................................494How It Works................................................................................................................................494

    Creating a New Tcl.............................................................................................................................494 Tcl Extensions.....................................................................................................................................494

    expect............................................................................................................................................494 [incr Tcl].......................................................................................................................................495 TclX..............................................................................................................................................495 Graphics........................................................................................................................................495

    Summary.............................................................................................................................................495

    xii

  • Table of Contents Chapter 16: Programming for X..................................................................................................................496

    Overview.............................................................................................................................................496 What is X?...........................................................................................................................................496

    X Server.........................................................................................................................................497X Protocol......................................................................................................................................497Xlib................................................................................................................................................497X Clients........................................................................................................................................497 X Toolkits.....................................................................................................................................497

    X Window Manager............................................................................................................................498 The X Programming Model................................................................................................................499

    Start Up.........................................................................................................................................499 Main Loop....................................................................................................................................500 Clean Up.......................................................................................................................................500

    Fast Track X Programming.................................................................................................................501 The Tk Toolkit....................................................................................................................................501

    Windows Programming................................................................................................................502Try It Out Saying Hello.............................................................................................................503How It Works................................................................................................................................503 Configuration Files.......................................................................................................................504 More Commands..........................................................................................................................504 Tk Widgets...................................................................................................................................505Try It Out Learning More..........................................................................................................505How It Works................................................................................................................................506 Tk's Builtin Dialogs...................................................................................................................529Color Chooser................................................................................................................................529Get Open/Save Files......................................................................................................................530 Color Schemes..............................................................................................................................531 Fonts.............................................................................................................................................532 Bindings........................................................................................................................................532 BindTags......................................................................................................................................533 Geometry Management................................................................................................................535 Focus and Navigation...................................................................................................................537 Option Database...........................................................................................................................538 Interapplication Communication................................................................................................539 Selection.......................................................................................................................................539 Clipboard......................................................................................................................................540 Window Manager.........................................................................................................................541 Dynamic/Static Loading...............................................................................................................542 Safe Tk.........................................................................................................................................543

    A MegaWidget.................................................................................................................................544Package File Generation................................................................................................................553

    An Application Using the Tree MegaWidget...................................................................................554 Tk Process Log Viewer.......................................................................................................................556

    Internationalization.......................................................................................................................566 Where Now?.................................................................................................................................567Tix.................................................................................................................................................567[incr Tk].........................................................................................................................................567BLT...............................................................................................................................................567

    Summary.............................................................................................................................................568

    xiii

  • Table of Contents Chapter 17: Programming GNOME using GTK+.....................................................................................569

    Overview...................................................................................................................