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