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