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