openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

bltsh.c (3508B)


      1 /* $Id: //depot/blt/bin/bltsh/bltsh.c#12 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <errno.h>
     33 #include <unistd.h>
     34 #include <sys/stat.h>
     35 #include <blt/syscall.h>
     36 
     37 void grab_console(void);
     38 char **params;
     39 void __libc_init_console_input (void);
     40 
     41 void do_command(int argc, char **argv)
     42 {
     43 	struct stat s;
     44 	int thid;
     45 	
     46 	if(!strcmp(argv[0], "exit")) os_terminate (1);
     47 	
     48 	if(!stat(argv[0],&s)){
     49 		if((thid = execve(argv[0],argv,NULL)) > 0){
     50 			thr_wait(thid);
     51 			grab_console();
     52 		} else {
     53 			printf("bltsh: failed to execve(): %s\n", argv[0]);
     54 		}	
     55 	} else {
     56 		/* try our "path" */
     57 		char *x = (char *) malloc(7 + strlen(argv[0]));
     58 		
     59 		strcpy(x,"/boot/");
     60 		strcpy(x+6,argv[0]);
     61 		free(argv[0]);
     62 		argv[0] = x;
     63 		
     64 		if(!stat(x,&s)){
     65 			if((thid = execve(argv[0],argv,NULL)) > 0){
     66 				thr_wait(thid);
     67 				grab_console();
     68 			} else {
     69 				printf("bltsh: failed to execve(): %s\n", argv[0]);
     70 			}
     71 		} else {
     72 			printf("bltsh: no such file or directory: %s\n", argv[0]);
     73 		}
     74 	}	
     75 }
     76 
     77 int main (void)
     78 {
     79 	char line[256], *c;
     80 	int len, space, i, p_argc;
     81 
     82 	__libc_init_console_input ();
     83 	printf ("\n");
     84 
     85 	for (;;)
     86 	{
     87 		printf ("$ ");
     88 
     89 		*line = len = 0;
     90 		while (read (0, line + len++, 1) > 0)
     91 		{
     92 			if ((line[len - 1] == 8) && (len > 1)) /* BS */
     93 				len -= 2;
     94 			else if (line[len - 1] == '\n')
     95 			{
     96 				line[len-- - 1] = 0;
     97 				for (i = space = 0, p_argc = 2; i < len; i++)
     98 					if ((line[i] == ' ') && !space)
     99 						space = 1;
    100 					else if ((line[i] != ' ') && space)
    101 					{
    102 						p_argc++;
    103 						space = 0;
    104 					}
    105 				if ((*line != '#') && *line)
    106 				{
    107 					params = malloc (sizeof (char *) * p_argc);
    108 					c = line;
    109 					for (i = 0; i < p_argc - 1; i++)
    110 					{
    111 						for (len = 0; c[len] && (c[len] != ' '); len++) ;
    112 						params[i] = malloc (len + 1);
    113 						strlcpy (params[i], c, len + 1);
    114 						c += len + 1;
    115 					}
    116 					params[p_argc - 1] = NULL;
    117 					
    118 					do_command(p_argc, params);
    119 					
    120 					for(i=0;i<p_argc;i++)
    121 						free(params[i]);
    122 					free(params);
    123 				}
    124 				len = 0;
    125 				break;
    126 			}
    127 		}
    128 	}
    129 
    130 	return 0;
    131 }
    132