• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2008-2009 Gene Cumm - All Rights Reserved
4  *
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, Inc., 53 Temple Place Ste 330,
8  *   Boston MA 02111-1307, USA; either version 2 of the License, or
9  *   (at your option) any later version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12 
13 /*
14  * rosh.h
15  *
16  * Read-Only shell; Header
17  */
18 
19 /*
20  * History
21  * b034	Improve debug functions to simpler code
22  * b021	Move much PreProcessing stuff to rosh.h
23  * b018	Create rosh_debug() macro
24  * b012	Version of rosh.c at time of creating this file.
25  */
26 
27 #ifndef ROSH_H
28 #define ROSH_H
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdbool.h>		/* macro: true false */
33 #include <string.h>		/* strcpy() strlen() memcpy() strchr() */
34 #include <sys/types.h>
35 #include <limits.h>
36 #include <sys/stat.h>		/* fstat() */
37 #include <fcntl.h>		/* open(); open mode macros */
38 #include <dirent.h>		/* fdopendir() opendir() readdir() closedir() DIR */
39 #include <unistd.h>		/* getcwd() getopt() */
40 #include <errno.h>		/* errno; error macros */
41 #include <netinet/in.h>		/* For htonl/ntohl/htons/ntohs */
42 #include <ctype.h>		/* isspace() */
43 
44 #include <getkey.h>
45 #include <consoles.h>		/* console_ansi_raw() console_ansi_std() */
46 // #include <getopt.h>		/* getopt_long() */
47 
48 #ifdef DO_DEBUG
49 # define ROSH_DEBUG	printf
50 # define ROSH_DEBUG_ARGV_V	rosh_pr_argv_v
51 /* define ROSH_DEBUG(f, ...)	printf (f, ## __VA_ARGS__) */
52 # ifdef DO_DEBUG2
53 #  define ROSH_DEBUG2	printf
54 #  define ROSH_DEBUG2_ARGV_V	rosh_pr_argv_v
55 # else /* DO_DEBUG2 */
56 	/* This forces a format argument into the function call */
57 #  define ROSH_DEBUG2(f, ...)	((void)0)
58 #  define ROSH_DEBUG2_ARGV_V(argc, argv)	((void)0)
59 # endif	/* DO_DEBUG2 */
60 #else /* DO_DEBUG */
61 # define ROSH_DEBUG(f, ...)	((void)0)
62 # define ROSH_DEBUG_ARGV_V(argc, argv)	((void)0)
63 # define ROSH_DEBUG2(f, ...)	((void)0)
64 # define ROSH_DEBUG2_ARGV_V(argc, argv)	((void)0)
65 #endif /* DO_DEBUG */
66 #define ROSH_DEBUG2_STAT(f, ...)	((void)0)
67 // #define ROSH_DEBUG2_STAT	ROSH_DEBUG2
68 
69 #ifdef __COM32__
70 #define ROSH_IS_COM32	1
71 #include <console.h>		/* openconsole() */
72 #include <syslinux/config.h>	/* Has info on the SYSLINUX variant */
73 #include <syslinux/boot.h>	/* syslinux_run_command() */
74 #include <syslinux/reboot.h>
75 #define ROSH_COM32(f, ...)	printf (f, ## __VA_ARGS__)
76 #define rosh_console_std()		console_ansi_std()
77 #define rosh_console_raw()		console_ansi_raw()
78 
stat(const char * pathname,struct stat * buf)79 int stat(const char *pathname, struct stat *buf)
80 {
81     int fd, status, ret = -1;
82     DIR *d;
83 
84     ROSH_DEBUG2_STAT("stat:opendir(%s) ", pathname);
85     d = opendir(pathname);
86     if (d != NULL) {
87 	ROSH_DEBUG2_STAT("stat:closedir() ");
88 	closedir(d);
89 	ret = 0;
90 	buf->st_mode = S_IFDIR | 0555;
91 	buf->st_size = 0;
92     } else if ((errno == 0) || (errno == ENOENT) || (errno == ENOTDIR)) {
93 	ROSH_DEBUG2_STAT("(%d)stat:open() ", errno);
94 	fd = open(pathname, O_RDONLY);
95 	if (fd != -1) {
96 	    ROSH_DEBUG2_STAT("(%d)stat:fstat() ", fd);
97 	    status = fstat(fd, buf);
98 	    (void)status;
99 	    ROSH_DEBUG2_STAT("stat:close() ");
100 	    close(fd);
101 	    ret = 0;
102 	}
103     }
104     return ret;
105 }
106 
rosh_get_env_ver(char * dest,size_t n)107 int rosh_get_env_ver(char *dest, size_t n)
108 {
109     const struct syslinux_version *slv = syslinux_version();
110     strncpy(dest, slv->version_string, n);
111     return 0;
112 }
113 
114 #else
115 #  include <termios.h>
116 #  include <sys/ioctl.h>
117 #  include <sys/utsname.h>
118 #  define ROSH_IS_COM32	0
119 
syslinux_config_file(void)120 static inline char *syslinux_config_file(void)
121 {
122     return "";
123 }
124 
rosh_get_env_ver(char * dest,size_t n)125 int rosh_get_env_ver(char *dest, size_t n)
126 {
127     int ret, len;
128     struct utsname env;
129     ret= uname(&env);
130     if (ret >= 0) {
131 	strncpy(dest, env.sysname, n);
132 	len = strlen(dest);
133 	strncpy(dest + len, " ", (n - len));
134 	len = strlen(dest);
135 	strncpy(dest + len, env.release, (n - len));
136     }
137     return ret;
138 }
139 
getscreensize(int fd,int * rows,int * cols)140 static inline int getscreensize(int fd, int *rows, int *cols)
141 {
142     char *str;
143     int rv;
144     struct winsize ws;
145     if (rows)
146 	*rows = 0;
147     if (cols)
148 	*cols = 0;
149     str = NULL;
150     if (fd == 1) {
151 	ioctl(0, TIOCGWINSZ, &ws);
152 	if (rows)
153 	    *rows = ws.ws_row;
154 	if (cols)
155 	    *cols = ws.ws_col;
156 	if (rows && !*rows) {
157 	    str = getenv("LINES");
158 	    if (str)
159 		*rows = atoi(str);
160 	}
161 	if (cols && !*cols) {
162 	    str = getenv("COLUMNS");
163 	    if (str)
164 		*cols = atoi(str);
165 	}
166     }
167     if (!rows || !cols)
168 	rv = -1;
169     else if (!*rows || !*cols)
170 	rv = -2;
171     else
172 	rv = 0;
173     return rv;
174 }
175 
176 /*
177  * Switches console over to raw input mode.  Allows get_key to get just
178  * 1 key sequence (without delay or display)
179  */
rosh_console_raw(void)180 void rosh_console_raw(void)
181 {
182     struct termios tio;
183 
184     console_ansi_raw();		/* Allows get_key to get just 1 key sequence
185 				   (w/o delay or display */
186     /* Deal with the changes that haven't been replicated to ansiraw.c */
187     tcgetattr(0, &tio);
188     tio.c_iflag &= ~IGNCR;
189     tcsetattr(0, TCSAFLUSH, &tio);
190 }
191 
192 /*
193  * Switches back to standard getline mode.
194  */
rosh_console_std(void)195 void rosh_console_std(void)
196 {
197     struct termios tio;
198 
199     console_ansi_std();
200     tcgetattr(0, &tio);
201     tio.c_iflag |= ICRNL;
202     tio.c_iflag &= ~IGNCR;
203     tcsetattr(0, TCSANOW, &tio);
204 }
205 
syslinux_reboot(int warm)206 void syslinux_reboot(int warm)
207 {
208     printf("Test Reboot(%d)\n", warm);
209 }
210 
211 #define ROSH_COM32(f, ...)	((void)0)
212 #define syslinux_run_command(f)	((void)0)
213 #endif /* __COM32__ */
214 
215 #define SEP	'/'
216 
217 /* Size of buffer string */
218 #define ROSH_BUF_SZ	16384
219 /* Size of screen output buffer (80*40) //HERE */
220 #define ROSH_SBUF_SZ	((80 + 2) * 40)
221 
222 /* Size of command buffer string */
223 #ifdef MAX_CMDLINE_LEN
224 #  define ROSH_CMD_SZ		MAX_CMDLINE_LEN
225 #elif COMMAND_LINE_SIZE
226 #  define ROSH_CMD_SZ		COMMAND_LINE_SIZE
227 #else
228 #  define ROSH_CMD_SZ_LG2	12
229 #  define ROSH_CMD_SZ		(1 << ROSH_CMD_SZ_LG2)
230 #endif /* MAX_CMDLINE_LEN */
231 
232 /* Size of path buffer string */
233 #ifdef PATH_MAX
234 #  define ROSH_PATH_SZ		PATH_MAX
235 #elif NAME_MAX
236 #  define ROSH_PATH_SZ		NAME_MAX
237 #elif FILENAME_MAX
238 #  define ROSH_PATH_SZ		FILENAME_MAX
239 #else
240 #  define ROSH_PATH_SZ_LG2	8
241 #  define ROSH_PATH_SZ		(1 << ROSH_PATH_SZ_LG2)
242 #endif /* PATH_MAX */
243 
244 #define ROSH_OPT_SZ	8
245 
246 const char rosh_beta_str[] =
247     "  ROSH is currently beta.  Constructive feedback is appreciated";
248 
249 const char rosh_cd_norun_str[] =
250     " -- cd (Change Directory) not implemented for use with run and exit.\n";
251 
252 const char rosh_help_cd_str[] = "cd    Change directory\n\
253    with no argument, return to original directory from entry to rosh\n\
254    with one argument, change to that directory";
255 
256 const char rosh_help_ls_str[] = "ls    List contents of current directory\n\
257   -l  Long format\n\
258   -i  Inode; print Inode of file\n\
259   -F  Classify; Add a 1-character suffix to classify files";
260 
261 const char rosh_help_str1[] =
262     "Commands: ? cat cd cfg dir exit help less ls man more pwd run quit ver";
263 
264 const char rosh_help_str2[] =
265     "Commands: (short generally non-ambiguous abreviations are also allowed)\n\
266   h     HELP\n     ALSO ? help man\n     ALSO help <command>\n\
267   cat   Concatenate file to console\n    cat <file>\n\
268   cd    Change to directory <dir>\n    cd <dir>\n\
269   less  Page a file with rewind\n\
270   ls    List contents of current directory\n    ls <dir>\n\
271     ALSO l dir\n\
272   more  Page a file\n\
273   pwd   display Present Working Directory\n\
274   run   Run a program/kernel with options\n\
275   r     Reboot (if COM32)\n        Also reboot\n\
276   exit  Exit to previous environment\n    ALSO quit";
277 
278 const char rosh_help_str_adv[] = "No additional help available for '%s'";
279 
280 const char rosh_ls_opt_str[] = "lFi";
281 
282 #endif /* Not ROSH_H */
283