diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-10-29 23:09:13 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-10-29 23:09:13 +0000 |
commit | 7f1acfdb8928de69bf02db6cf217f0f3b4af45ea (patch) | |
tree | 752654d9a3ef967d5409600962e31bea5cae14fc | |
parent | 24d8e7d787aa1940030a1beaabdd4388588ca512 (diff) | |
download | busybox-w32-7f1acfdb8928de69bf02db6cf217f0f3b4af45ea.tar.gz busybox-w32-7f1acfdb8928de69bf02db6cf217f0f3b4af45ea.tar.bz2 busybox-w32-7f1acfdb8928de69bf02db6cf217f0f3b4af45ea.zip |
More stuf. sed works.
-rw-r--r-- | Changelog | 1 | ||||
-rw-r--r-- | busybox.def.h | 6 | ||||
-rw-r--r-- | editors/sed.c | 32 | ||||
-rw-r--r-- | init.c | 84 | ||||
-rw-r--r-- | init/init.c | 84 | ||||
-rw-r--r-- | regexp.c | 40 | ||||
-rw-r--r-- | sed.c | 32 | ||||
-rw-r--r-- | utility.c | 6 |
8 files changed, 185 insertions, 100 deletions
@@ -7,6 +7,7 @@ | |||
7 | * Added new apps chvt and deallocvt | 7 | * Added new apps chvt and deallocvt |
8 | * Major adjustment of init.c. Code is now readable IMHO, | 8 | * Major adjustment of init.c. Code is now readable IMHO, |
9 | and much more solid. | 9 | and much more solid. |
10 | * Wrote sed -- weighs in at 1.8k (or 5.8k with full regular expressions!). | ||
10 | 11 | ||
11 | -Erik Andersen | 12 | -Erik Andersen |
12 | 13 | ||
diff --git a/busybox.def.h b/busybox.def.h index b71c53923..57f835e24 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
@@ -15,12 +15,12 @@ | |||
15 | #define BB_DMESG | 15 | #define BB_DMESG |
16 | //#define BB_DUTMP | 16 | //#define BB_DUTMP |
17 | //#define BB_FDFLUSH | 17 | //#define BB_FDFLUSH |
18 | #define BB_FIND | 18 | //#define BB_FIND |
19 | //#define BB_FSCK_MINIX | 19 | //#define BB_FSCK_MINIX |
20 | //#define BB_MKFS_MINIX | 20 | //#define BB_MKFS_MINIX |
21 | #define BB_CHVT | 21 | #define BB_CHVT |
22 | #define BB_DEALLOCVT | 22 | #define BB_DEALLOCVT |
23 | #define BB_GREP | 23 | //#define BB_GREP |
24 | //#define BB_HALT | 24 | //#define BB_HALT |
25 | #define BB_INIT | 25 | #define BB_INIT |
26 | #define BB_KILL | 26 | #define BB_KILL |
@@ -42,7 +42,7 @@ | |||
42 | //#define BB_PRINTF | 42 | //#define BB_PRINTF |
43 | #define BB_PS | 43 | #define BB_PS |
44 | #define BB_PWD | 44 | #define BB_PWD |
45 | //#define BB_REGEXP | 45 | #define BB_REGEXP |
46 | #define BB_REBOOT | 46 | #define BB_REBOOT |
47 | #define BB_RM | 47 | #define BB_RM |
48 | #define BB_RMDIR | 48 | #define BB_RMDIR |
diff --git a/editors/sed.c b/editors/sed.c index 0df53a7f8..f0a6a3b48 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -57,7 +57,8 @@ extern int sed_main (int argc, char **argv) | |||
57 | char *cp; | 57 | char *cp; |
58 | int ignoreCase=FALSE; | 58 | int ignoreCase=FALSE; |
59 | int foundOne=FALSE; | 59 | int foundOne=FALSE; |
60 | int noprintFlag=FALSE; | 60 | int printFlag=FALSE; |
61 | int quietFlag=FALSE; | ||
61 | int stopNow; | 62 | int stopNow; |
62 | char *haystack; | 63 | char *haystack; |
63 | 64 | ||
@@ -75,7 +76,7 @@ extern int sed_main (int argc, char **argv) | |||
75 | while (*++cp && stopNow==FALSE) | 76 | while (*++cp && stopNow==FALSE) |
76 | switch (*cp) { | 77 | switch (*cp) { |
77 | case 'n': | 78 | case 'n': |
78 | noprintFlag = TRUE; | 79 | quietFlag = TRUE; |
79 | break; | 80 | break; |
80 | case 'e': | 81 | case 'e': |
81 | if (*(cp+1)==0 && --argc < 0) { | 82 | if (*(cp+1)==0 && --argc < 0) { |
@@ -113,6 +114,23 @@ extern int sed_main (int argc, char **argv) | |||
113 | break; | 114 | break; |
114 | } | 115 | } |
115 | *pos=0; | 116 | *pos=0; |
117 | if (pos+2 != 0) { | ||
118 | while (*++pos) { | ||
119 | fprintf(stderr, "pos='%s'\n", pos); | ||
120 | switch (*pos) { | ||
121 | case 'i': | ||
122 | ignoreCase=TRUE; | ||
123 | break; | ||
124 | case 'p': | ||
125 | printFlag=TRUE; | ||
126 | break; | ||
127 | case 'g': | ||
128 | break; | ||
129 | default: | ||
130 | usage( sed_usage); | ||
131 | } | ||
132 | } | ||
133 | } | ||
116 | } | 134 | } |
117 | cp++; | 135 | cp++; |
118 | } | 136 | } |
@@ -135,13 +153,13 @@ extern int sed_main (int argc, char **argv) | |||
135 | continue; | 153 | continue; |
136 | } | 154 | } |
137 | 155 | ||
138 | haystack = (char*)malloc( BUF_SIZE); | 156 | haystack = (char*)malloc( 1024); |
139 | while (fgets (haystack, BUF_SIZE-1, fp)) { | 157 | while (fgets (haystack, 1023, fp)) { |
140 | 158 | ||
141 | foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); | 159 | foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); |
142 | if (noprintFlag==TRUE && foundOne==TRUE) | 160 | if (foundOne==TRUE && printFlag==TRUE) |
143 | fputs (haystack, stdout); | 161 | fputs (haystack, stdout); |
144 | else | 162 | if (quietFlag==FALSE) |
145 | fputs (haystack, stdout); | 163 | fputs (haystack, stdout); |
146 | /* Avoid any mem leaks */ | 164 | /* Avoid any mem leaks */ |
147 | free(haystack); | 165 | free(haystack); |
@@ -41,6 +41,9 @@ | |||
41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | /* Turn this on to debug init so it won't reboot when killed */ | ||
45 | #define DEBUG_INIT | ||
46 | |||
44 | #define CONSOLE "/dev/console" /* Logical system console */ | 47 | #define CONSOLE "/dev/console" /* Logical system console */ |
45 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
46 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
@@ -61,7 +64,7 @@ int device_open(char *device, int mode) | |||
61 | { | 64 | { |
62 | int m, f, fd = -1; | 65 | int m, f, fd = -1; |
63 | 66 | ||
64 | mode = m | O_NONBLOCK; | 67 | m = mode | O_NONBLOCK; |
65 | 68 | ||
66 | /* Retry up to 5 times */ | 69 | /* Retry up to 5 times */ |
67 | for (f = 0; f < 5; f++) | 70 | for (f = 0; f < 5; f++) |
@@ -99,25 +102,28 @@ void message(char *device, char *fmt, ...) | |||
99 | void set_term( int fd) | 102 | void set_term( int fd) |
100 | { | 103 | { |
101 | struct termios tty; | 104 | struct termios tty; |
105 | static const char control_characters[] = { | ||
106 | '\003', '\034', '\177', '\025', '\004', '\0', | ||
107 | '\1', '\0', '\021', '\023', '\032', '\0', '\022', | ||
108 | '\017', '\027', '\026', '\0' | ||
109 | }; | ||
102 | 110 | ||
103 | tcgetattr(fd, &tty); | 111 | tcgetattr(fd, &tty); |
104 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; | 112 | |
105 | tty.c_cflag |= HUPCL|CLOCAL; | 113 | /* input modes */ |
106 | 114 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY; | |
107 | tty.c_cc[VINTR] = 3; | 115 | |
108 | tty.c_cc[VQUIT] = 28; | 116 | /* use lineo dicipline 0 */ |
109 | tty.c_cc[VERASE] = 127; | 117 | tty.c_line = 0; |
110 | tty.c_cc[VKILL] = 24; | 118 | |
111 | tty.c_cc[VEOF] = 4; | 119 | /* output modes */ |
112 | tty.c_cc[VTIME] = 0; | ||
113 | tty.c_cc[VMIN] = 1; | ||
114 | tty.c_cc[VSTART] = 17; | ||
115 | tty.c_cc[VSTOP] = 19; | ||
116 | tty.c_cc[VSUSP] = 26; | ||
117 | |||
118 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY; | ||
119 | tty.c_oflag = OPOST|ONLCR; | 120 | tty.c_oflag = OPOST|ONLCR; |
120 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE; | 121 | |
122 | /* local modes */ | ||
123 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN; | ||
124 | |||
125 | /* control chars */ | ||
126 | memcpy(tty.c_cc, control_characters, sizeof(control_characters)); | ||
121 | 127 | ||
122 | tcsetattr(fd, TCSANOW, &tty); | 128 | tcsetattr(fd, TCSANOW, &tty); |
123 | } | 129 | } |
@@ -151,7 +157,7 @@ static void set_free_pages() | |||
151 | fclose(f); | 157 | fclose(f); |
152 | f = fopen("/proc/sys/vm/freepages", "w"); | 158 | f = fopen("/proc/sys/vm/freepages", "w"); |
153 | fprintf(f, "30\t40\t50\n"); | 159 | fprintf(f, "30\t40\t50\n"); |
154 | printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); | 160 | message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); |
155 | } | 161 | } |
156 | fclose(f); | 162 | fclose(f); |
157 | } | 163 | } |
@@ -165,7 +171,6 @@ static void console_init() | |||
165 | int tried_vtmaster = 0; | 171 | int tried_vtmaster = 0; |
166 | char *s; | 172 | char *s; |
167 | 173 | ||
168 | fprintf(stderr, "entering console_init()\n"); | ||
169 | if ((s = getenv("CONSOLE")) != NULL) | 174 | if ((s = getenv("CONSOLE")) != NULL) |
170 | console = s; | 175 | console = s; |
171 | else { | 176 | else { |
@@ -175,6 +180,7 @@ static void console_init() | |||
175 | 180 | ||
176 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { | 181 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { |
177 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); | 182 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); |
183 | message(log, "Yikes! /dev/console does not exist or is a symlink.\n"); | ||
178 | } | 184 | } |
179 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { | 185 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
180 | if (!tried_devcons) { | 186 | if (!tried_devcons) { |
@@ -193,7 +199,7 @@ static void console_init() | |||
193 | console = "/dev/null"; | 199 | console = "/dev/null"; |
194 | else | 200 | else |
195 | close(fd); | 201 | close(fd); |
196 | fprintf(stderr, "console=%s\n", console); | 202 | message(log, "console=%s\n", console); |
197 | } | 203 | } |
198 | 204 | ||
199 | static int waitfor(int pid) | 205 | static int waitfor(int pid) |
@@ -212,7 +218,7 @@ static int waitfor(int pid) | |||
212 | static pid_t run(const char * const* command, | 218 | static pid_t run(const char * const* command, |
213 | char *terminal, int get_enter) | 219 | char *terminal, int get_enter) |
214 | { | 220 | { |
215 | int i; | 221 | int i, f; |
216 | pid_t pid; | 222 | pid_t pid; |
217 | static const char press_enter[] = | 223 | static const char press_enter[] = |
218 | "\nPlease press Enter to activate this console. "; | 224 | "\nPlease press Enter to activate this console. "; |
@@ -224,9 +230,9 @@ static pid_t run(const char * const* command, | |||
224 | close(2); | 230 | close(2); |
225 | setsid(); | 231 | setsid(); |
226 | 232 | ||
227 | open(terminal, O_RDWR); | 233 | f=open(terminal, O_RDWR); |
228 | dup(0); | 234 | dup(f); |
229 | dup(0); | 235 | dup(f); |
230 | tcsetpgrp(0, getpgrp()); | 236 | tcsetpgrp(0, getpgrp()); |
231 | set_term(0); | 237 | set_term(0); |
232 | 238 | ||
@@ -240,7 +246,7 @@ static pid_t run(const char * const* command, | |||
240 | * specifies. | 246 | * specifies. |
241 | */ | 247 | */ |
242 | char c; | 248 | char c; |
243 | write(0, press_enter, sizeof(press_enter) - 1); | 249 | write(1, press_enter, sizeof(press_enter) - 1); |
244 | read(0, &c, 1); | 250 | read(0, &c, 1); |
245 | } | 251 | } |
246 | 252 | ||
@@ -269,7 +275,7 @@ static pid_t run(const char * const* command, | |||
269 | return pid; | 275 | return pid; |
270 | } | 276 | } |
271 | 277 | ||
272 | 278 | #ifndef DEBUG_INIT | |
273 | static void shutdown_system(void) | 279 | static void shutdown_system(void) |
274 | { | 280 | { |
275 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 281 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; |
@@ -314,6 +320,7 @@ static void reboot_signal(int sig) | |||
314 | reboot(RB_AUTOBOOT); | 320 | reboot(RB_AUTOBOOT); |
315 | exit(0); | 321 | exit(0); |
316 | } | 322 | } |
323 | #endif | ||
317 | 324 | ||
318 | extern int init_main(int argc, char **argv) | 325 | extern int init_main(int argc, char **argv) |
319 | { | 326 | { |
@@ -326,11 +333,17 @@ extern int init_main(int argc, char **argv) | |||
326 | const char* const shell_commands[] = { SHELL, " -", 0}; | 333 | const char* const shell_commands[] = { SHELL, " -", 0}; |
327 | const char* const* tty0_commands = init_commands; | 334 | const char* const* tty0_commands = init_commands; |
328 | const char* const* tty1_commands = shell_commands; | 335 | const char* const* tty1_commands = shell_commands; |
336 | #ifndef DEBUG_INIT | ||
337 | char *hello_msg_format = | ||
338 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | ||
339 | #else | ||
329 | char *hello_msg_format = | 340 | char *hello_msg_format = |
330 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; | 341 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
342 | #endif | ||
331 | const char *no_memory = | 343 | const char *no_memory = |
332 | "Sorry, your computer does not have enough memory.\r\n"; | 344 | "Sorry, your computer does not have enough memory.\r\n"; |
333 | 345 | ||
346 | #ifndef DEBUG_INIT | ||
334 | /* Set up sig handlers */ | 347 | /* Set up sig handlers */ |
335 | signal(SIGUSR1, halt_signal); | 348 | signal(SIGUSR1, halt_signal); |
336 | signal(SIGSEGV, halt_signal); | 349 | signal(SIGSEGV, halt_signal); |
@@ -344,7 +357,7 @@ extern int init_main(int argc, char **argv) | |||
344 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 357 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
345 | * SIGINT on CAD so we can shut things down gracefully... */ | 358 | * SIGINT on CAD so we can shut things down gracefully... */ |
346 | reboot(RB_DISABLE_CAD); | 359 | reboot(RB_DISABLE_CAD); |
347 | 360 | #endif | |
348 | /* Figure out where the default console should be */ | 361 | /* Figure out where the default console should be */ |
349 | console_init(); | 362 | console_init(); |
350 | 363 | ||
@@ -352,7 +365,7 @@ extern int init_main(int argc, char **argv) | |||
352 | close(0); | 365 | close(0); |
353 | close(1); | 366 | close(1); |
354 | close(2); | 367 | close(2); |
355 | //set_term(0); | 368 | set_term(0); |
356 | setsid(); | 369 | setsid(); |
357 | 370 | ||
358 | /* Make sure PATH is set to something sane */ | 371 | /* Make sure PATH is set to something sane */ |
@@ -360,17 +373,22 @@ extern int init_main(int argc, char **argv) | |||
360 | putenv(PATH_DEFAULT); | 373 | putenv(PATH_DEFAULT); |
361 | 374 | ||
362 | /* Hello world */ | 375 | /* Hello world */ |
376 | #ifndef DEBUG_INIT | ||
363 | message(log, hello_msg_format, BB_VER, BB_BT); | 377 | message(log, hello_msg_format, BB_VER, BB_BT); |
364 | fprintf(stderr, hello_msg_format, BB_VER, BB_BT); | 378 | message(console, hello_msg_format, BB_VER, BB_BT); |
379 | #else | ||
380 | message(log, hello_msg_format, getpid(), BB_VER, BB_BT); | ||
381 | message(console, hello_msg_format, getpid(), BB_VER, BB_BT); | ||
382 | #endif | ||
365 | 383 | ||
366 | 384 | ||
367 | /* Mount /proc */ | 385 | /* Mount /proc */ |
368 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { | 386 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { |
369 | fprintf(stderr, "Mounting /proc: done.\n"); | ||
370 | message(log, "Mounting /proc: done.\n"); | 387 | message(log, "Mounting /proc: done.\n"); |
388 | message(console, "Mounting /proc: done.\n"); | ||
371 | } else { | 389 | } else { |
372 | fprintf(stderr, "Mounting /proc: failed!\n"); | ||
373 | message(log, "Mounting /proc: failed!\n"); | 390 | message(log, "Mounting /proc: failed!\n"); |
391 | message(console, "Mounting /proc: failed!\n"); | ||
374 | } | 392 | } |
375 | 393 | ||
376 | /* Make sure there is enough memory to do something useful */ | 394 | /* Make sure there is enough memory to do something useful */ |
@@ -379,7 +397,7 @@ extern int init_main(int argc, char **argv) | |||
379 | int retval; | 397 | int retval; |
380 | retval = stat("/etc/fstab", &statbuf); | 398 | retval = stat("/etc/fstab", &statbuf); |
381 | if (retval) { | 399 | if (retval) { |
382 | fprintf(stderr, "%s", no_memory); | 400 | message(console, "%s", no_memory); |
383 | while (1) { | 401 | while (1) { |
384 | sleep(1); | 402 | sleep(1); |
385 | } | 403 | } |
@@ -387,7 +405,7 @@ extern int init_main(int argc, char **argv) | |||
387 | /* Try to turn on swap */ | 405 | /* Try to turn on swap */ |
388 | waitfor(run(swap_on_cmd, console, 0)); | 406 | waitfor(run(swap_on_cmd, console, 0)); |
389 | if (mem_total() < 2000) { | 407 | if (mem_total() < 2000) { |
390 | fprintf(stderr, "%s", no_memory); | 408 | message(console, "%s", no_memory); |
391 | while (1) { | 409 | while (1) { |
392 | sleep(1); | 410 | sleep(1); |
393 | } | 411 | } |
diff --git a/init/init.c b/init/init.c index b8cbbf64e..a56e23e5d 100644 --- a/init/init.c +++ b/init/init.c | |||
@@ -41,6 +41,9 @@ | |||
41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | /* Turn this on to debug init so it won't reboot when killed */ | ||
45 | #define DEBUG_INIT | ||
46 | |||
44 | #define CONSOLE "/dev/console" /* Logical system console */ | 47 | #define CONSOLE "/dev/console" /* Logical system console */ |
45 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
46 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
@@ -61,7 +64,7 @@ int device_open(char *device, int mode) | |||
61 | { | 64 | { |
62 | int m, f, fd = -1; | 65 | int m, f, fd = -1; |
63 | 66 | ||
64 | mode = m | O_NONBLOCK; | 67 | m = mode | O_NONBLOCK; |
65 | 68 | ||
66 | /* Retry up to 5 times */ | 69 | /* Retry up to 5 times */ |
67 | for (f = 0; f < 5; f++) | 70 | for (f = 0; f < 5; f++) |
@@ -99,25 +102,28 @@ void message(char *device, char *fmt, ...) | |||
99 | void set_term( int fd) | 102 | void set_term( int fd) |
100 | { | 103 | { |
101 | struct termios tty; | 104 | struct termios tty; |
105 | static const char control_characters[] = { | ||
106 | '\003', '\034', '\177', '\025', '\004', '\0', | ||
107 | '\1', '\0', '\021', '\023', '\032', '\0', '\022', | ||
108 | '\017', '\027', '\026', '\0' | ||
109 | }; | ||
102 | 110 | ||
103 | tcgetattr(fd, &tty); | 111 | tcgetattr(fd, &tty); |
104 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; | 112 | |
105 | tty.c_cflag |= HUPCL|CLOCAL; | 113 | /* input modes */ |
106 | 114 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY; | |
107 | tty.c_cc[VINTR] = 3; | 115 | |
108 | tty.c_cc[VQUIT] = 28; | 116 | /* use lineo dicipline 0 */ |
109 | tty.c_cc[VERASE] = 127; | 117 | tty.c_line = 0; |
110 | tty.c_cc[VKILL] = 24; | 118 | |
111 | tty.c_cc[VEOF] = 4; | 119 | /* output modes */ |
112 | tty.c_cc[VTIME] = 0; | ||
113 | tty.c_cc[VMIN] = 1; | ||
114 | tty.c_cc[VSTART] = 17; | ||
115 | tty.c_cc[VSTOP] = 19; | ||
116 | tty.c_cc[VSUSP] = 26; | ||
117 | |||
118 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY; | ||
119 | tty.c_oflag = OPOST|ONLCR; | 120 | tty.c_oflag = OPOST|ONLCR; |
120 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE; | 121 | |
122 | /* local modes */ | ||
123 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN; | ||
124 | |||
125 | /* control chars */ | ||
126 | memcpy(tty.c_cc, control_characters, sizeof(control_characters)); | ||
121 | 127 | ||
122 | tcsetattr(fd, TCSANOW, &tty); | 128 | tcsetattr(fd, TCSANOW, &tty); |
123 | } | 129 | } |
@@ -151,7 +157,7 @@ static void set_free_pages() | |||
151 | fclose(f); | 157 | fclose(f); |
152 | f = fopen("/proc/sys/vm/freepages", "w"); | 158 | f = fopen("/proc/sys/vm/freepages", "w"); |
153 | fprintf(f, "30\t40\t50\n"); | 159 | fprintf(f, "30\t40\t50\n"); |
154 | printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); | 160 | message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); |
155 | } | 161 | } |
156 | fclose(f); | 162 | fclose(f); |
157 | } | 163 | } |
@@ -165,7 +171,6 @@ static void console_init() | |||
165 | int tried_vtmaster = 0; | 171 | int tried_vtmaster = 0; |
166 | char *s; | 172 | char *s; |
167 | 173 | ||
168 | fprintf(stderr, "entering console_init()\n"); | ||
169 | if ((s = getenv("CONSOLE")) != NULL) | 174 | if ((s = getenv("CONSOLE")) != NULL) |
170 | console = s; | 175 | console = s; |
171 | else { | 176 | else { |
@@ -175,6 +180,7 @@ static void console_init() | |||
175 | 180 | ||
176 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { | 181 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { |
177 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); | 182 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); |
183 | message(log, "Yikes! /dev/console does not exist or is a symlink.\n"); | ||
178 | } | 184 | } |
179 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { | 185 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
180 | if (!tried_devcons) { | 186 | if (!tried_devcons) { |
@@ -193,7 +199,7 @@ static void console_init() | |||
193 | console = "/dev/null"; | 199 | console = "/dev/null"; |
194 | else | 200 | else |
195 | close(fd); | 201 | close(fd); |
196 | fprintf(stderr, "console=%s\n", console); | 202 | message(log, "console=%s\n", console); |
197 | } | 203 | } |
198 | 204 | ||
199 | static int waitfor(int pid) | 205 | static int waitfor(int pid) |
@@ -212,7 +218,7 @@ static int waitfor(int pid) | |||
212 | static pid_t run(const char * const* command, | 218 | static pid_t run(const char * const* command, |
213 | char *terminal, int get_enter) | 219 | char *terminal, int get_enter) |
214 | { | 220 | { |
215 | int i; | 221 | int i, f; |
216 | pid_t pid; | 222 | pid_t pid; |
217 | static const char press_enter[] = | 223 | static const char press_enter[] = |
218 | "\nPlease press Enter to activate this console. "; | 224 | "\nPlease press Enter to activate this console. "; |
@@ -224,9 +230,9 @@ static pid_t run(const char * const* command, | |||
224 | close(2); | 230 | close(2); |
225 | setsid(); | 231 | setsid(); |
226 | 232 | ||
227 | open(terminal, O_RDWR); | 233 | f=open(terminal, O_RDWR); |
228 | dup(0); | 234 | dup(f); |
229 | dup(0); | 235 | dup(f); |
230 | tcsetpgrp(0, getpgrp()); | 236 | tcsetpgrp(0, getpgrp()); |
231 | set_term(0); | 237 | set_term(0); |
232 | 238 | ||
@@ -240,7 +246,7 @@ static pid_t run(const char * const* command, | |||
240 | * specifies. | 246 | * specifies. |
241 | */ | 247 | */ |
242 | char c; | 248 | char c; |
243 | write(0, press_enter, sizeof(press_enter) - 1); | 249 | write(1, press_enter, sizeof(press_enter) - 1); |
244 | read(0, &c, 1); | 250 | read(0, &c, 1); |
245 | } | 251 | } |
246 | 252 | ||
@@ -269,7 +275,7 @@ static pid_t run(const char * const* command, | |||
269 | return pid; | 275 | return pid; |
270 | } | 276 | } |
271 | 277 | ||
272 | 278 | #ifndef DEBUG_INIT | |
273 | static void shutdown_system(void) | 279 | static void shutdown_system(void) |
274 | { | 280 | { |
275 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 281 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; |
@@ -314,6 +320,7 @@ static void reboot_signal(int sig) | |||
314 | reboot(RB_AUTOBOOT); | 320 | reboot(RB_AUTOBOOT); |
315 | exit(0); | 321 | exit(0); |
316 | } | 322 | } |
323 | #endif | ||
317 | 324 | ||
318 | extern int init_main(int argc, char **argv) | 325 | extern int init_main(int argc, char **argv) |
319 | { | 326 | { |
@@ -326,11 +333,17 @@ extern int init_main(int argc, char **argv) | |||
326 | const char* const shell_commands[] = { SHELL, " -", 0}; | 333 | const char* const shell_commands[] = { SHELL, " -", 0}; |
327 | const char* const* tty0_commands = init_commands; | 334 | const char* const* tty0_commands = init_commands; |
328 | const char* const* tty1_commands = shell_commands; | 335 | const char* const* tty1_commands = shell_commands; |
336 | #ifndef DEBUG_INIT | ||
337 | char *hello_msg_format = | ||
338 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | ||
339 | #else | ||
329 | char *hello_msg_format = | 340 | char *hello_msg_format = |
330 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; | 341 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
342 | #endif | ||
331 | const char *no_memory = | 343 | const char *no_memory = |
332 | "Sorry, your computer does not have enough memory.\r\n"; | 344 | "Sorry, your computer does not have enough memory.\r\n"; |
333 | 345 | ||
346 | #ifndef DEBUG_INIT | ||
334 | /* Set up sig handlers */ | 347 | /* Set up sig handlers */ |
335 | signal(SIGUSR1, halt_signal); | 348 | signal(SIGUSR1, halt_signal); |
336 | signal(SIGSEGV, halt_signal); | 349 | signal(SIGSEGV, halt_signal); |
@@ -344,7 +357,7 @@ extern int init_main(int argc, char **argv) | |||
344 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 357 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
345 | * SIGINT on CAD so we can shut things down gracefully... */ | 358 | * SIGINT on CAD so we can shut things down gracefully... */ |
346 | reboot(RB_DISABLE_CAD); | 359 | reboot(RB_DISABLE_CAD); |
347 | 360 | #endif | |
348 | /* Figure out where the default console should be */ | 361 | /* Figure out where the default console should be */ |
349 | console_init(); | 362 | console_init(); |
350 | 363 | ||
@@ -352,7 +365,7 @@ extern int init_main(int argc, char **argv) | |||
352 | close(0); | 365 | close(0); |
353 | close(1); | 366 | close(1); |
354 | close(2); | 367 | close(2); |
355 | //set_term(0); | 368 | set_term(0); |
356 | setsid(); | 369 | setsid(); |
357 | 370 | ||
358 | /* Make sure PATH is set to something sane */ | 371 | /* Make sure PATH is set to something sane */ |
@@ -360,17 +373,22 @@ extern int init_main(int argc, char **argv) | |||
360 | putenv(PATH_DEFAULT); | 373 | putenv(PATH_DEFAULT); |
361 | 374 | ||
362 | /* Hello world */ | 375 | /* Hello world */ |
376 | #ifndef DEBUG_INIT | ||
363 | message(log, hello_msg_format, BB_VER, BB_BT); | 377 | message(log, hello_msg_format, BB_VER, BB_BT); |
364 | fprintf(stderr, hello_msg_format, BB_VER, BB_BT); | 378 | message(console, hello_msg_format, BB_VER, BB_BT); |
379 | #else | ||
380 | message(log, hello_msg_format, getpid(), BB_VER, BB_BT); | ||
381 | message(console, hello_msg_format, getpid(), BB_VER, BB_BT); | ||
382 | #endif | ||
365 | 383 | ||
366 | 384 | ||
367 | /* Mount /proc */ | 385 | /* Mount /proc */ |
368 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { | 386 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { |
369 | fprintf(stderr, "Mounting /proc: done.\n"); | ||
370 | message(log, "Mounting /proc: done.\n"); | 387 | message(log, "Mounting /proc: done.\n"); |
388 | message(console, "Mounting /proc: done.\n"); | ||
371 | } else { | 389 | } else { |
372 | fprintf(stderr, "Mounting /proc: failed!\n"); | ||
373 | message(log, "Mounting /proc: failed!\n"); | 390 | message(log, "Mounting /proc: failed!\n"); |
391 | message(console, "Mounting /proc: failed!\n"); | ||
374 | } | 392 | } |
375 | 393 | ||
376 | /* Make sure there is enough memory to do something useful */ | 394 | /* Make sure there is enough memory to do something useful */ |
@@ -379,7 +397,7 @@ extern int init_main(int argc, char **argv) | |||
379 | int retval; | 397 | int retval; |
380 | retval = stat("/etc/fstab", &statbuf); | 398 | retval = stat("/etc/fstab", &statbuf); |
381 | if (retval) { | 399 | if (retval) { |
382 | fprintf(stderr, "%s", no_memory); | 400 | message(console, "%s", no_memory); |
383 | while (1) { | 401 | while (1) { |
384 | sleep(1); | 402 | sleep(1); |
385 | } | 403 | } |
@@ -387,7 +405,7 @@ extern int init_main(int argc, char **argv) | |||
387 | /* Try to turn on swap */ | 405 | /* Try to turn on swap */ |
388 | waitfor(run(swap_on_cmd, console, 0)); | 406 | waitfor(run(swap_on_cmd, console, 0)); |
389 | if (mem_total() < 2000) { | 407 | if (mem_total() < 2000) { |
390 | fprintf(stderr, "%s", no_memory); | 408 | message(console, "%s", no_memory); |
391 | while (1) { | 409 | while (1) { |
392 | sleep(1); | 410 | sleep(1); |
393 | } | 411 | } |
@@ -7,7 +7,7 @@ | |||
7 | #include <ctype.h> | 7 | #include <ctype.h> |
8 | 8 | ||
9 | 9 | ||
10 | #if ( defined BB_GREP || defined BB_FIND ) | 10 | #if ( defined BB_GREP || defined BB_FIND || defined BB_SED) |
11 | 11 | ||
12 | /* This also tries to find a needle in a haystack, but uses | 12 | /* This also tries to find a needle in a haystack, but uses |
13 | * real regular expressions.... The fake regular expression | 13 | * real regular expressions.... The fake regular expression |
@@ -25,27 +25,39 @@ extern int find_match(char *haystack, char *needle, int ignoreCase) | |||
25 | return( status); | 25 | return( status); |
26 | } | 26 | } |
27 | 27 | ||
28 | #if defined BB_SED | ||
28 | /* This performs substitutions after a regexp match has been found. | 29 | /* This performs substitutions after a regexp match has been found. |
29 | * The new string is returned. It is malloc'ed, and do must be freed. */ | 30 | * The new string is returned. It is malloc'ed, and do must be freed. */ |
30 | extern char* replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) | 31 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
31 | { | 32 | { |
32 | int status; | 33 | int status; |
33 | char* newHaystack; | ||
34 | struct regexp* re; | 34 | struct regexp* re; |
35 | newHaystack = (char *)malloc((unsigned)(strlen(haystack) - | 35 | char *s, buf[BUF_SIZE], *d = buf; |
36 | strlen(needle) + strlen(newNeedle)); | 36 | |
37 | re = regcomp( needle); | 37 | re = regcomp( needle); |
38 | status = regexec(re, haystack, FALSE, ignoreCase); | 38 | status = regexec(re, haystack, FALSE, ignoreCase); |
39 | 39 | if (status==TRUE) { | |
40 | return( newHaystack) | 40 | s=haystack; |
41 | } | 41 | |
42 | 42 | do { | |
43 | 43 | /* copy stuff from before the match */ | |
44 | extern void regsub(regexp* re, char* src, char* dst) | 44 | while (s < re->startp[0]) |
45 | 45 | *d++ = *s++; | |
46 | /* substitute for the matched part */ | ||
47 | regsub(re, newNeedle, d); | ||
48 | s = re->endp[0]; | ||
49 | d += strlen(d); | ||
50 | } while (regexec(re, s, FALSE, ignoreCase) == TRUE); | ||
51 | /* copy stuff from after the match */ | ||
52 | while ( (*d++ = *s++) ) {} | ||
53 | d[-1] = '\n'; | ||
54 | d[0] = '\0'; | ||
55 | strcpy(haystack, buf); | ||
56 | } | ||
46 | free( re); | 57 | free( re); |
47 | return( status); | 58 | return( status); |
48 | } | 59 | } |
60 | #endif | ||
49 | 61 | ||
50 | 62 | ||
51 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to | 63 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to |
@@ -695,7 +707,7 @@ extern int regexec(struct regexp* re, char* str, int bol, int ignoreCase) | |||
695 | 707 | ||
696 | 708 | ||
697 | 709 | ||
698 | 710 | #if defined BB_SED | |
699 | /* This performs substitutions after a regexp match has been found. */ | 711 | /* This performs substitutions after a regexp match has been found. */ |
700 | extern void regsub(regexp* re, char* src, char* dst) | 712 | extern void regsub(regexp* re, char* src, char* dst) |
701 | { | 713 | { |
@@ -841,7 +853,7 @@ extern void regsub(regexp* re, char* src, char* dst) | |||
841 | if (previous1) | 853 | if (previous1) |
842 | strcpy(previous1, start); | 854 | strcpy(previous1, start); |
843 | } | 855 | } |
844 | 856 | #endif | |
845 | 857 | ||
846 | #endif /* BB_REGEXP */ | 858 | #endif /* BB_REGEXP */ |
847 | 859 | ||
@@ -57,7 +57,8 @@ extern int sed_main (int argc, char **argv) | |||
57 | char *cp; | 57 | char *cp; |
58 | int ignoreCase=FALSE; | 58 | int ignoreCase=FALSE; |
59 | int foundOne=FALSE; | 59 | int foundOne=FALSE; |
60 | int noprintFlag=FALSE; | 60 | int printFlag=FALSE; |
61 | int quietFlag=FALSE; | ||
61 | int stopNow; | 62 | int stopNow; |
62 | char *haystack; | 63 | char *haystack; |
63 | 64 | ||
@@ -75,7 +76,7 @@ extern int sed_main (int argc, char **argv) | |||
75 | while (*++cp && stopNow==FALSE) | 76 | while (*++cp && stopNow==FALSE) |
76 | switch (*cp) { | 77 | switch (*cp) { |
77 | case 'n': | 78 | case 'n': |
78 | noprintFlag = TRUE; | 79 | quietFlag = TRUE; |
79 | break; | 80 | break; |
80 | case 'e': | 81 | case 'e': |
81 | if (*(cp+1)==0 && --argc < 0) { | 82 | if (*(cp+1)==0 && --argc < 0) { |
@@ -113,6 +114,23 @@ extern int sed_main (int argc, char **argv) | |||
113 | break; | 114 | break; |
114 | } | 115 | } |
115 | *pos=0; | 116 | *pos=0; |
117 | if (pos+2 != 0) { | ||
118 | while (*++pos) { | ||
119 | fprintf(stderr, "pos='%s'\n", pos); | ||
120 | switch (*pos) { | ||
121 | case 'i': | ||
122 | ignoreCase=TRUE; | ||
123 | break; | ||
124 | case 'p': | ||
125 | printFlag=TRUE; | ||
126 | break; | ||
127 | case 'g': | ||
128 | break; | ||
129 | default: | ||
130 | usage( sed_usage); | ||
131 | } | ||
132 | } | ||
133 | } | ||
116 | } | 134 | } |
117 | cp++; | 135 | cp++; |
118 | } | 136 | } |
@@ -135,13 +153,13 @@ extern int sed_main (int argc, char **argv) | |||
135 | continue; | 153 | continue; |
136 | } | 154 | } |
137 | 155 | ||
138 | haystack = (char*)malloc( BUF_SIZE); | 156 | haystack = (char*)malloc( 1024); |
139 | while (fgets (haystack, BUF_SIZE-1, fp)) { | 157 | while (fgets (haystack, 1023, fp)) { |
140 | 158 | ||
141 | foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); | 159 | foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); |
142 | if (noprintFlag==TRUE && foundOne==TRUE) | 160 | if (foundOne==TRUE && printFlag==TRUE) |
143 | fputs (haystack, stdout); | 161 | fputs (haystack, stdout); |
144 | else | 162 | if (quietFlag==FALSE) |
145 | fputs (haystack, stdout); | 163 | fputs (haystack, stdout); |
146 | /* Avoid any mem leaks */ | 164 | /* Avoid any mem leaks */ |
147 | free(haystack); | 165 | free(haystack); |
@@ -777,7 +777,7 @@ int get_console_fd(char* tty_name) | |||
777 | #endif | 777 | #endif |
778 | 778 | ||
779 | 779 | ||
780 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND ) | 780 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED) |
781 | 781 | ||
782 | /* Do a case insensitive strstr() */ | 782 | /* Do a case insensitive strstr() */ |
783 | char* stristr(char *haystack, const char *needle) | 783 | char* stristr(char *haystack, const char *needle) |
@@ -817,7 +817,7 @@ extern int find_match(char *haystack, char *needle, int ignoreCase) | |||
817 | /* This performs substitutions after a string match has been found. */ | 817 | /* This performs substitutions after a string match has been found. */ |
818 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) | 818 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
819 | { | 819 | { |
820 | int foundOne; | 820 | int foundOne=0; |
821 | char *where, *slider, *slider1, *oldhayStack; | 821 | char *where, *slider, *slider1, *oldhayStack; |
822 | 822 | ||
823 | if (ignoreCase == FALSE) | 823 | if (ignoreCase == FALSE) |
@@ -847,7 +847,7 @@ extern int replace_match(char *haystack, char *needle, char *newNeedle, int igno | |||
847 | } | 847 | } |
848 | free( oldhayStack); | 848 | free( oldhayStack); |
849 | 849 | ||
850 | if (foundOne) | 850 | if (foundOne > 0) |
851 | return TRUE; | 851 | return TRUE; |
852 | else | 852 | else |
853 | return FALSE; | 853 | return FALSE; |