1 /*
2  * txMain.c --
3  *
4  * 	This module handles output to the text terminal as well as
5  *	collecting input and sending the commands to the window package.
6  *
7  *     *********************************************************************
8  *     * Copyright (C) 1985, 1990 Regents of the University of California. *
9  *     * Permission to use, copy, modify, and distribute this              *
10  *     * software and its documentation for any purpose and without        *
11  *     * fee is hereby granted, provided that the above copyright          *
12  *     * notice appear in all copies.  The University of California        *
13  *     * makes no representations about the suitability of this            *
14  *     * software for any purpose.  It is provided "as is" without         *
15  *     * express or implied warranty.  Export of this software outside     *
16  *     * of the United States of America may require an export license.    *
17  *     *********************************************************************
18  */
19 
20 #ifndef lint
21 static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/textio/txMain.c,v 1.2 2010/03/08 13:33:34 tim Exp $";
22 #endif  /* not lint */
23 
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include "utils/magsgtty.h"
29 #include "utils/magic.h"
30 #include "textio/textio.h"
31 #include "utils/geometry.h"
32 #include "textio/txcommands.h"
33 #include "textio/textioInt.h"
34 #include "windows/windows.h"
35 #include "tiles/tile.h"
36 #include "utils/hash.h"
37 #include "database/database.h"
38 #include "dbwind/dbwind.h"
39 
40 /* Global variables that indicate if we are reading or writing to a tty.
41  */
42 global bool TxStdinIsatty;
43 global bool TxStdoutIsatty;
44 
45 #ifdef USE_READLINE
46 #ifdef HAVE_READLINE
47 #include <readline/readline.h>
48 #include <readline/history.h>
49 #else
50 #include "readline/readline.h"
51 #include "readline/history.h"
52 #endif
53 
54 int TxPrefix(void);
55 char **magic_completion_function(char *, int, int);
56 extern HashTable cellname_hash;
57 
58 /* The readline completion function requires a command list containing	*/
59 /* just the command name (without the accompanying help text line)	*/
60 extern char **magic_command_list;
61 
62 #endif
63 
64 
65 /*
66  * ----------------------------------------------------------------------------
67  * TxInit:
68  *
69  *	Initialize this module.
70  *
71  *
72  * Results:
73  *	None.
74  *
75  * Side effects:
76  *	misc.
77  * ----------------------------------------------------------------------------
78  */
79 
80 void
TxInit()81 TxInit()
82 {
83     static char sebuf[BUFSIZ];
84 
85     setbuf(stderr, sebuf);
86     setbuf(stdin, (char *) NULL);  /* required for LPENDIN in textio to work */
87     TxStdinIsatty = (isatty(fileno(stdin)));
88 
89 #ifdef MAGIC_WRAPPER
90     TxStdoutIsatty = 0;	/* Tx loop is non-interactive */
91 #else
92     TxStdoutIsatty = (isatty(fileno(stdout)));
93 #endif
94 
95     txCommandsInit();
96 }
97 
98 #ifdef USE_READLINE
99 
100 void
TxInitReadline()101 TxInitReadline()
102 {
103     int i, j;
104     char **commandTable;
105     char nobell[] = "set bell-style none";
106 
107     rl_getc_function = TxGetChar;
108     rl_pre_input_hook = TxPrefix;
109     rl_readline_name = "magic";
110 
111     /* the default behavior is for no terminal bell to ever ring */
112     rl_parse_and_bind(nobell);
113 
114     /* read ~/.inputrc (or whatever INPUTRC is set to) to allow users to override */
115     rl_read_init_file(NULL);
116 
117     /* removed "=" and "(" because styles contain them */
118     rl_completer_word_break_characters = " \t\n\"\\'`@$><;|&{";
119 
120     rl_attempted_completion_function = (CPPFunction *)magic_completion_function;
121     HashInit(&cellname_hash, 128, HT_STRINGKEYS);
122 
123     i = j = 0;
124     commandTable = WindGetCommandTable(DBWclientID);
125     while(commandTable[i++] != (char *)NULL ) {
126       j++;
127     }
128     i = 0;
129     commandTable = WindGetCommandTable(windClientID);
130     while(commandTable[i++] != (char *)NULL ) {
131       j++;
132     }
133 
134     magic_command_list = (char **)mallocMagic(sizeof(char *) * (j + 1));
135 
136     i = j = 0;
137     commandTable = WindGetCommandTable(DBWclientID);
138     while( commandTable[i] != (char *)NULL ) {
139       int k = 0;
140       while( !isspace(commandTable[i][k]) && (commandTable[i][k] != '\0') ) {
141         k++;
142       }
143       if( k > 0 ) {
144         magic_command_list[j] = (char *)mallocMagic((k+1)*sizeof(char));
145         strncpy(magic_command_list[j], commandTable[i], k);
146         magic_command_list[j][k] = '\0';
147         j++;
148       }
149       i++;
150     }
151     i = 0;
152     commandTable = WindGetCommandTable(windClientID);
153     while( commandTable[i] != (char *)NULL ) {
154       int k = 0;
155       while( !isspace(commandTable[i][k]) && (commandTable[i][k] != '\0') ) {
156         k++;
157       }
158       if( k > 0 ) {
159         magic_command_list[j] = (char *)mallocMagic((k+1)*sizeof(char));
160         strncpy(magic_command_list[j], commandTable[i], k);
161         magic_command_list[j][k] = '\0';
162         j++;
163       }
164       i++;
165     }
166     magic_command_list[j] = (char *)NULL;
167     rl_completion_query_items = MAX(j+1, 250);
168 }
169 
170 #endif
171 
172