Changeset e2ea8d7e in mainline


Ignore:
Timestamp:
2008-08-27T05:36:12Z (16 years ago)
Author:
Tim Post <echo@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1b4b7b6
Parents:
b510d52
Message:

Housekeeping list, complete lingering things before they get forgotten:

  • cli_*() now sets a global cli_errno, error functions cleaned up
  • Finish internal cli_*() functions in util.c
  • Don't expose cli_init() or cli_finit()
  • Get rid of unused globals
  • Don't set globals in commands themselves
  • Update README files
  • Fix stale comments
Location:
uspace/app/bdsh
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/builtins/README

    rb510d52 re2ea8d7e  
    55Examples of what should be a built-in and not a module would be:
    66
    7 cd     (the cwd needs to be updated)
    8 prompt (the prompt needs to be updated)
    9 enable (the euid needs to be updated)
     7cd     (cliuser_t->cwd needs to be updated)
     8
     9In the future, more user preferences will be set via built-in commands,
     10such as the formatting of the prompt string (HelenOS doesn't yet have
     11an environment, much less PS*, even if it did we'd likely do it a little
     12differently).
    1013
    1114.... etc.
    1215
    13 Anything that does _not_ need to write to this structure should be included
    14 as a module, not a built in.
     16Anything that does _not_ need to use this structure should be included
     17as a module, not a built in. If you want to include a new command, there
     18is a 99% chance that you want it to be a module.
    1519
    16 For now, a skeleton directory of stuff that will exist lives here.
     20
     21
  • uspace/app/bdsh/cmds/modules/README

    rb510d52 re2ea8d7e  
    1 Modules are commands or full programs (anything can be made into a module that can return
    2 int type) should go here. Note, modules do not update the structures containing user info
    3 such as the working directory, euid, etc.
     1Modules are commands or full programs (anything can be made into a module
     2that can return int type) should go here. Note, modules do not (can not)
     3update or read cliuser_t.
    44
    5 Stuff that needs to write to the user structures contained in scli.h should be made as
    6 built-in commands, not modules.
     5Stuff that needs to write to the user structures contained in scli.h should
     6be made as built-in commands, not modules, but there are very few times when
     7you would want to do that.
     8
     9See the README file in the bdsh root directory for a quick overview of how to
     10write a new command, or convert an existig stand-alone program into a module
     11for BDSH.
    712
    813
     14
     15
  • uspace/app/bdsh/cmds/modules/help/help.c

    rb510d52 re2ea8d7e  
    4141static char *cmdname = "help";
    4242extern const char *progname;
    43 extern unsigned int cli_interactive;
    4443
    4544#define HELP_IS_MODULE   1
     
    127126        }
    128127
    129         printf("%sAvailable commands are:\n", cli_interactive ? "\n  " : "");
    130         if (cli_interactive)
    131                 printf(
    132                         "  ------------------------------------------------------------\n");
     128        printf("\n  Available commands are:\n");
     129        printf("  ------------------------------------------------------------\n");
    133130
    134131        /* First, show a list of built in commands that are available in this mode */
     
    156153        }
    157154
    158         /* Provide  a little more information and inform them of history / line
    159          * editing features if they are present */
    160         if (cli_interactive)
    161                 printf("\n  Try %s %s for more information on how `%s' works.\n\n",
    162                         cmdname, cmdname, cmdname);
     155        printf("\n  Try %s %s for more information on how `%s' works.\n\n",
     156                cmdname, cmdname, cmdname);
    163157
    164158        return CMD_SUCCESS;
  • uspace/app/bdsh/cmds/modules/quit/quit.c

    rb510d52 re2ea8d7e  
    3636
    3737static char *cmdname = "quit";
    38 unsigned int cli_quit = 0;
    3938
    40 extern volatile int cli_lasterror;
     39extern volatile unsigned int cli_quit;
    4140extern const char *progname;
    4241
  • uspace/app/bdsh/errors.c

    rb510d52 re2ea8d7e  
    3939#include "errstr.h"
    4040
     41volatile int cli_errno = CL_EOK;
     42extern volatile unsigned int cli_quit;
     43
    4144/* Error printing, translation and handling functions */
    4245
    43 volatile int cli_lasterr = 0;
    44 extern volatile unsigned int cli_verbocity;
    4546
    4647/* Look up errno in cl_errors and return the corresponding string.
    4748 * Return NULL if not found */
    48 char *err2str(int errno)
     49static char *err2str(int err)
    4950{
    5051
    51         if (NULL != cl_errors[errno])
    52                 return cl_errors[errno];
     52        if (NULL != cl_errors[err])
     53                return cl_errors[err];
    5354
    5455        return (char *)NULL;
     
    5960 * cli_quit int that tells the main program loop to exit immediately */
    6061
    61 void cli_error(int errno, const char *fmt, ...)
     62void cli_error(int err, const char *fmt, ...)
    6263{
    6364        va_list vargs;
     
    6667        va_end(vargs);
    6768
    68         if (NULL != err2str(errno))
    69                 printf(" (%s)\n", err2str(errno));
     69        if (NULL != err2str(err))
     70                printf(" (%s)\n", err2str(err));
    7071        else
    71                 printf(" (Unknown Error %d)\n", errno);
     72                printf(" (Unknown Error %d)\n", err);
    7273
    73         if (errno < 0)
    74                 exit(EXIT_FAILURE);
     74        /* If fatal, raise cli_quit so that we try to exit
     75         * gracefully. This will break the main loop and
     76         * invoke the destructor */
     77        if (err == CL_EFATAL)
     78                cli_quit = 1;
    7579
    76 }
     80        return;
    7781
    78 /* Just a smart printf(), print the string only if cli_verbocity is high */
    79 void cli_verbose(const char *fmt, ...)
    80 {
    81         if (cli_verbocity) {
    82                 va_list vargs;
    83 
    84                 printf("[*] ");
    85                 va_start(vargs, fmt);
    86                 vprintf(fmt, vargs);
    87                 va_end(vargs);
    88                 printf("\n");
    89         }
    90         return;
    9182}
    9283
     
    9485
    9586
     87
  • uspace/app/bdsh/errors.h

    rb510d52 re2ea8d7e  
    33
    44/* Various error levels */
    5 #define CL_EFATAL   -1
     5#define CL_EFATAL  -1
    66#define CL_EOK     0
    77#define CL_EFAIL   1
     
    1313#define CL_EEXEC   7
    1414#define CL_EEXISTS 8
     15#define CL_ETOOBIG 9
    1516
    16 extern char *err2str(int);
     17/* Just like 'errno' */
     18extern volatile int cli_errno;
     19
    1720extern void cli_error(int, const char *, ...);
    18 extern void cli_verbose(const char *, ...);
     21
    1922#endif
  • uspace/app/bdsh/errstr.h

    rb510d52 re2ea8d7e  
    1414        "Bad command or file name",
    1515        "Entry already exists",
     16        "Object too large",
    1617        NULL
    1718};
    1819
     20static char *err2str(int);
     21
    1922#endif
    2023
  • uspace/app/bdsh/exec.c

    rb510d52 re2ea8d7e  
    6666char *find_command(char *cmd)
    6767{
    68         char *path_orig, *path_tok;
     68        char *path_tok;
    6969        char *path[PATH_MAX];
    7070        int n = 0, i = 0;
     
    7777                return (char *) cmd;
    7878        }
    79         path_orig = PATH;
    80         path_tok = cli_strdup(path_orig);
     79
     80        path_tok = cli_strdup(PATH);
    8181
    8282        /* Extract the PATH env to a path[] array */
  • uspace/app/bdsh/input.c

    rb510d52 re2ea8d7e  
    4747void cli_restricted(char *cmd)
    4848{
    49         cli_verbose("%s is not available in %s mode\n", cmd,
     49        printf("%s is not available in %s mode\n", cmd,
    5050                cli_interactive ? "interactive" : "non-interactive");
    5151
  • uspace/app/bdsh/scli.c

    rb510d52 re2ea8d7e  
    4343static cliuser_t usr;
    4444
    45 /* Modified by the 'quit' module, which is compiled before this */
    46 extern unsigned int cli_quit;
    47 
    48 /* Globals that are modified during start-up that modules/builtins should
    49  * be aware of. */
     45/* Globals that are modified during start-up that modules/builtins
     46 * should be aware of. */
     47volatile unsigned int cli_quit = 0;
    5048volatile unsigned int cli_interactive = 1;
    5149volatile unsigned int cli_verbocity = 1;
     
    5553const char *progname = PACKAGE_NAME;
    5654
     55/* These are not exposed, even to builtins */
     56static int cli_init(cliuser_t *usr);
     57static void cli_finit(cliuser_t *usr);
     58
    5759/* (re)allocates memory to store the current working directory, gets
    58  * and updates the current working directory, formats the prompt string */
     60 * and updates the current working directory, formats the prompt
     61 * string */
    5962unsigned int cli_set_prompt(cliuser_t *usr)
    6063{
     
    7881                snprintf(usr->cwd, PATH_MAX, "(unknown)");
    7982
    80         snprintf(usr->prompt,
    81                         PATH_MAX,
    82                         "%s # ",
    83                         usr->cwd);
     83        if (1 < cli_psprintf(&usr->prompt, "%s # ", usr->cwd)) {
     84                cli_error(cli_errno, "Failed to set prompt");
     85                return 1;
     86        }
    8487
    8588        return 0;
    8689}
    8790
    88 int cli_init(cliuser_t *usr)
     91/* Constructor */
     92static int cli_init(cliuser_t *usr)
    8993{
    9094        usr->line = (char *) NULL;
     
    99103
    100104/* Destructor */
    101 void cli_finit(cliuser_t *usr)
     105static void cli_finit(cliuser_t *usr)
    102106{
    103107        if (NULL != usr->line)
  • uspace/app/bdsh/scli.h

    rb510d52 re2ea8d7e  
    1515
    1616extern unsigned int cli_set_prompt(cliuser_t *usr);
    17 extern int cli_init(cliuser_t *usr);
    18 extern void cli_finit(cliuser_t *usr);
    1917
    2018#endif
  • uspace/app/bdsh/util.c

    rb510d52 re2ea8d7e  
    5353#include "util.h"
    5454
     55extern volatile int cli_errno;
     56
    5557/* some platforms do not have strdup, implement it here.
    5658 * Returns a pointer to an allocated string or NULL on failure */
     
    6062        void *ret = malloc(len);
    6163
    62         if (ret == NULL)
     64        if (ret == NULL) {
     65                cli_errno = CL_ENOMEM;
    6366                return (char *) NULL;
    64 
     67        }
     68
     69        cli_errno = CL_EOK;
    6570        return (char *) memcpy(ret, s1, len);
    6671}
     
    8085        *s1 = realloc(*s1, len);
    8186
    82         if (*s1 == NULL)
    83                 return -1;
     87        if (*s1 == NULL) {
     88                cli_errno = CL_ENOMEM;
     89                return -1;
     90        }
    8491
    8592        memset(*s1, 0, sizeof(*s1));
    8693        memcpy(*s1, s2, len);
     94        cli_errno = CL_EOK;
    8795        return (int) len;
    8896}
    8997
    90 /* An asprintf() for concantenating paths. Allocates the system PATH_MAX value,
    91  * expands the formatted string and re-sizes the block s1 points to accordingly.
    92  *
    93  * Returns the length of the new s1 on success, -1 on failure. On failure, an
    94  * attempt is made to return s1 unmodified for sanity, in this case 0 is returned.
    95  * to indicate that s1 was not modified.
    96  *
    97  * FIXME: ugly hack to get around asprintf(), if you use this, CHECK ITS VALUE! */
     98/* An asprintf() for formatting paths, similar to asprintf() but ensures
     99 * the returned allocated string is <= PATH_MAX. On failure, an attempt
     100 * is made to return the original string (if not null) unmodified.
     101 *
     102 * Returns: Length of the new string on success, 0 if the string was handed
     103 * back unmodified, -1 on failure. On failure, cli_errno is set.
     104 *
     105 * We do not use POSIX_PATH_MAX, as it is typically much smaller than the
     106 * PATH_MAX defined by the kernel.
     107 *
     108 * Use this like:
     109 * if (1 > cli_psprintf(&char, "%s/%s", foo, bar)) {
     110 *   cli_error(cli_errno, "Failed to format path");
     111 *   stop_what_your_doing_as_your_out_of_memory();
     112 * }
     113 */
     114
    98115int cli_psprintf(char **s1, const char *fmt, ...)
    99116{
    100117        va_list ap;
    101118        size_t needed, base = PATH_MAX + 1;
     119        int skipped = 0;
     120        char *orig = NULL;
    102121        char *tmp = (char *) malloc(base);
    103122
    104         if (NULL == tmp)
    105                 return -1;
    106 
    107         char *orig = *s1;
    108 
     123        /* Don't even touch s1, not enough memory */
     124        if (NULL == tmp) {
     125                cli_errno = CL_ENOMEM;
     126                return -1;
     127        }
     128
     129        /* If re-allocating s1, save a copy in case we fail */
     130        if (NULL != *s1)
     131                orig = cli_strdup(*s1);
     132
     133        /* Print the string to tmp so we can determine the size that
     134         * we actually need */
    109135        memset(tmp, 0, sizeof(tmp));
    110136        va_start(ap, fmt);
    111         vsnprintf(tmp, base, fmt, ap);
     137        /* vsnprintf will return the # of bytes not written */
     138        skipped = vsnprintf(tmp, base, fmt, ap);
    112139        va_end(ap);
     140
     141        /* realloc/alloc s1 to be just the size that we need */
    113142        needed = strlen(tmp) + 1;
    114143        *s1 = realloc(*s1, needed);
     144
    115145        if (NULL == *s1) {
     146                /* No string lived here previously, or we failed to
     147                 * make a copy of it, either way there's nothing we
     148                 * can do. */
     149                if (NULL == *orig) {
     150                        cli_errno = CL_ENOMEM;
     151                        return -1;
     152                }
     153                /* We can't even allocate enough size to restore the
     154                 * saved copy, just give up */
    116155                *s1 = realloc(*s1, strlen(orig) + 1);
    117156                if (NULL == *s1) {
    118157                        free(tmp);
     158                        free(orig);
     159                        cli_errno = CL_ENOMEM;
    119160                        return -1;
    120161                }
     162                /* Give the string back as we found it */
    121163                memset(*s1, 0, sizeof(*s1));
    122164                memcpy(*s1, orig, strlen(orig) + 1);
    123165                free(tmp);
     166                free(orig);
     167                cli_errno = CL_ENOMEM;
    124168                return 0;
    125169        }
     170
     171        /* Ok, great, we have enough room */
    126172        memset(*s1, 0, sizeof(*s1));
    127173        memcpy(*s1, tmp, needed);
    128174        free(tmp);
    129175
     176        /* Free tmp only if s1 was reallocated instead of allocated */
     177        if (NULL != orig)
     178                free(orig);
     179
     180        if (skipped) {
     181                /* s1 was bigger than PATH_MAX when expanded, however part
     182                 * of the string was printed. Tell the caller not to use it */
     183                cli_errno = CL_ETOOBIG;
     184                return -1;
     185        }
     186
     187        /* Success! */
     188        cli_errno = CL_EOK;
    130189        return (int) needed;
    131190}
     
    137196        int c, sc;
    138197
    139         if (s == NULL && (s = *last) == NULL)
     198        if (s == NULL && (s = *last) == NULL) {
     199                cli_errno = CL_EFAIL;
    140200                return (NULL);
     201        }
    141202
    142203cont:
Note: See TracChangeset for help on using the changeset viewer.