chiark / gitweb /
Install serialmgr and run_sympathy in examples directory (uncompressed)
[sympathy.git] / src / cmd.c
1 /* 
2  * cmd.c:
3  *
4  * Copyright (c) 2008 James McKenzie <sympathy@madingley.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id: cmd.c,v 1.13 2008/03/07 13:16:02 james Exp $";
10
11 /* 
12  * $Log: cmd.c,v $
13  * Revision 1.13  2008/03/07 13:16:02  james
14  * *** empty log message ***
15  *
16  * Revision 1.12  2008/03/07 12:37:04  james
17  * *** empty log message ***
18  *
19  * Revision 1.11  2008/03/03 06:04:42  james
20  * *** empty log message ***
21  *
22  * Revision 1.10  2008/03/02 10:50:32  staffcvs
23  * *** empty log message ***
24  *
25  * Revision 1.9  2008/03/02 10:37:56  james
26  * *** empty log message ***
27  *
28  * Revision 1.8  2008/02/29 22:50:29  james
29  * *** empty log message ***
30  *
31  * Revision 1.7  2008/02/28 22:00:42  james
32  * *** empty log message ***
33  *
34  * Revision 1.6  2008/02/28 16:57:51  james
35  * *** empty log message ***
36  *
37  * Revision 1.5  2008/02/28 15:37:06  james
38  * *** empty log message ***
39  *
40  * Revision 1.4  2008/02/28 11:27:48  james
41  * *** empty log message ***
42  *
43  * Revision 1.3  2008/02/22 17:07:00  james
44  * *** empty log message ***
45  *
46  * Revision 1.2  2008/02/15 23:52:12  james
47  * *** empty log message ***
48  *
49  * Revision 1.1  2008/02/15 15:14:19  james
50  * *** empty log message ***
51  *
52  */
53
54 #include "project.h"
55
56
57 int
58 cmd_parse (Cmd * c, Context * ctx, ANSI * a, char *buf)
59 {
60   if (!strcmp (buf, "quit"))
61     c->disconnect++;
62   else if (!strcmp (buf, "flow"))
63     ctx->k->set_flow (ctx->k, ctx, 1);
64   else if (!strcmp (buf, "noflow"))
65     ctx->k->set_flow (ctx->k, ctx, 0);
66   else if (!strcmp (buf, "ansi"))
67     ctx->k->set_ansi (ctx->k, ctx, 0);
68   else if (!strcmp (buf, "noansi"))
69     ctx->k->set_ansi (ctx->k, ctx, 1);
70   else if (!strncmp (buf, "baud", 4))
71     ctx->k->set_baud (ctx->k, ctx, atoi (buf + 4));
72   else if (!strcmp (buf, "break"))
73     ctx->k->send_break (ctx->k, ctx);
74   else if (!strcmp (buf, "hangup"))
75     ctx->k->hangup (ctx->k, ctx);
76   else if (!strcmp (buf, "reset"))
77     ctx->k->reset (ctx->k, ctx);
78   else if (!strcmp (buf, "expand")) {
79     int w = a->terminal->size.x;
80     int h = a->terminal->size.y - 1;
81     ctx->k->set_size (ctx->k, ctx, w, h);
82   } else if (!strncmp (buf, "width", 5))
83     ctx->k->set_size (ctx->k, ctx, atoi (buf + 5), 0);
84   else if (!strncmp (buf, "height", 6))
85     ctx->k->set_size (ctx->k, ctx, 0, atoi (buf + 6));
86   else
87     return -1;
88
89   return 0;
90
91
92 }
93
94 void
95 cmd_show_status (Cmd * c, Context * ctx)
96 {
97   if (!ctx->v)
98     return;
99
100   if (c->error)
101     vt102_status_line (ctx->v, "Command not recognized - press any key");
102   else if (!c->active)
103     vt102_status_line (ctx->v, c->csl);
104   else
105     vt102_status_line (ctx->v, c->buf);
106
107 }
108
109 int
110 cmd_key (Cmd * c, Context * ctx, ANSI * a, int key)
111 {
112
113   if (c->error) {
114     c->error = 0;
115     c->active = 0;
116     cmd_show_status (c, ctx);
117     return 0;
118   }
119
120   if (key == 13) {
121     if (cmd_parse (c, ctx, a, c->buf + 1)) {
122       c->error++;
123     } else {
124       c->active = 0;
125     }
126     cmd_show_status (c, ctx);
127     return 0;
128   }
129
130   if (((key == 8) || (key == 127)) && (c->ptr > 1)) {
131     c->ptr--;
132     c->buf[c->ptr] = 0;
133   }
134
135   if ((key >= 32) && (key < 127)) {
136
137     c->buf[c->ptr] = key;
138     c->ptr++;
139     c->buf[c->ptr] = 0;
140
141   }
142
143   cmd_show_status (c, ctx);
144
145   return 0;
146 }
147
148
149 int
150 cmd_deactivate (Cmd * c, Context * ctx)
151 {
152   c->active = 0;
153   cmd_show_status (c, ctx);
154   return 0;
155 }
156
157 int
158 cmd_activate (Cmd * c, Context * ctx)
159 {
160   c->active = 1;
161   c->ptr = 1;
162   c->buf[0] = ':';
163   c->buf[1] = 0;
164
165   cmd_show_status (c, ctx);
166
167   return 0;
168 }
169
170 void
171 cmd_new_status (Cmd * c, Context * ctx, char *msg)
172 {
173   strcpy (c->csl, msg);
174   cmd_show_status (c, ctx);
175 }
176
177
178
179 Cmd *
180 cmd_new (void)
181 {
182   Cmd *ret;
183
184   ret = (Cmd *) xmalloc (sizeof (Cmd));
185
186   ret->disconnect = 0;
187   ret->active = 0;
188   ret->error = 0;
189   ret->csl[0] = 0;
190 }