Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / zjab / cmds.c
blob08391eba8c3edd984d42e60e50ef7ca3d3a992db
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include "net.h"
28 #include "xmpp.h"
29 #include "cmds.h"
31 static char *cmd_window;
32 static char *cmd_cache;
33 static unsigned cmd_len;
35 #ifdef LINUX
36 void setcolor (unsigned char t, unsigned char f)
38 /* TODO */
40 #endif
42 static int getstring (char *str)
44 if (cmd_len > CMDS_CACHE_SIZE-1) {
45 setcolor (4, 0);
46 printf ("\nERROR -> commands cache buffer is full ! Clearing cache ..\n");
47 setcolor (15, 0);
49 cmd_len = 0;
50 return 0;
53 char c = getchar ();
55 if (c != -1) {
56 if (c == '\n') {
57 unsigned l = cmd_len;
58 cmd_len = 0;
60 return l;
62 if (c == '\b') {
63 if (cmd_len > 0)
64 cmd_len --;
66 return 0;
69 str[cmd_len] = c;
70 cmd_len ++;
73 return 0;
76 int cmds_handler (char *buffer, unsigned len)
78 if (!strncmp (buffer, "/to ", 4)) {
79 if (cmd_window)
80 free (cmd_window);
82 cmd_window = strndup (buffer+4, len-4);
84 if (!cmd_window)
85 return -1;
87 printf ("> Current chat window: %s\n", cmd_window);
89 return 0;
90 } else if (!strncmp (buffer, "/help", len)) {
91 printf ("> zjab help\n\t/to\t-\tSpecify JID of chat window\n\t/status\t-\tChange status\n\t/quit\t-\tQuit the zjab\n");
93 return 0;
94 } else if (!strncmp (buffer, "/status ", 8)) {
95 printf ("> Status was changed to \"%s\"\n", buffer+8);
96 xmpp_presence ("online", buffer+8);
97 return 0;
98 } else if (!strncmp (buffer, "/quit", len))
99 return -1;
101 if (!cmd_window) {
102 printf ("> Unknown chat window, please use /help\n");
103 return 0;
106 if (cmd_window)
107 xmpp_message (cmd_window, buffer, len);
109 setcolor (14, 0);
110 printf ("> %s\n", buffer);
111 setcolor (7, 0);
113 return 0;
116 /* get input from keyboard */
117 int cmds_get ()
119 int l = getstring (cmd_cache);
121 if (l > 0) {
122 cmd_cache[l] = '\0';
124 return cmds_handler (cmd_cache, l);
127 return 1;
130 int cmds_init ()
132 cmd_cache = (char *) malloc (sizeof (char) * CMDS_CACHE_SIZE);
134 if (!cmd_cache)
135 return -1;
137 /* set socket "1" to non-blocking */
138 int oldFlag = fcntl (1, F_GETFL, 0);
139 if (fcntl (1, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
140 printf ("Cant set socket to nonblocking mode\n");
141 return -1;
144 cmd_len = 0;
145 cmd_window = 0;
147 return 0;