00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> 00002 * All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * Redistributions of source code must retain the above copyright notice, this 00008 * list of conditions and the following disclaimer. 00009 * 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * Neither the name of the original program's authors nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 #include <stdio.h> 00032 #include <stdlib.h> 00033 #include <str.h> 00034 #include <unistd.h> 00035 #include "config.h" 00036 #include "scli.h" 00037 #include "input.h" 00038 #include "util.h" 00039 #include "errors.h" 00040 #include "cmds/cmds.h" 00041 00042 /* See scli.h */ 00043 static cliuser_t usr; 00044 00045 /* Globals that are modified during start-up that modules/builtins 00046 * should be aware of. */ 00047 volatile unsigned int cli_quit = 0; 00048 volatile unsigned int cli_verbocity = 1; 00049 00050 /* The official name of this program 00051 * (change to your liking in configure.ac and re-run autoconf) */ 00052 const char *progname = PACKAGE_NAME; 00053 00054 /* These are not exposed, even to builtins */ 00055 static int cli_init(cliuser_t *); 00056 static void cli_finit(cliuser_t *); 00057 00058 /* Constructor */ 00059 static int cli_init(cliuser_t *usr) 00060 { 00061 usr->line = (char *) NULL; 00062 usr->name = "root"; 00063 usr->cwd = (char *) NULL; 00064 usr->prompt = (char *) NULL; 00065 usr->lasterr = 0; 00066 00067 if (input_init() != 0) 00068 return 1; 00069 00070 return (int) cli_set_prompt(usr); 00071 } 00072 00073 /* Destructor */ 00074 static void cli_finit(cliuser_t *usr) 00075 { 00076 if (NULL != usr->line) 00077 free(usr->line); 00078 if (NULL != usr->prompt) 00079 free(usr->prompt); 00080 if (NULL != usr->cwd) 00081 free(usr->cwd); 00082 } 00083 00084 int main(int argc, char *argv[]) 00085 { 00086 int ret = 0; 00087 00088 if (cli_init(&usr)) 00089 exit(EXIT_FAILURE); 00090 00091 while (!cli_quit) { 00092 get_input(&usr); 00093 if (NULL != usr.line) { 00094 ret = tok_input(&usr); 00095 cli_set_prompt(&usr); 00096 usr.lasterr = ret; 00097 } 00098 } 00099 00100 printf("Leaving %s.\n", progname); 00101 00102 cli_finit(&usr); 00103 return ret; 00104 }