Changeset 37f527b in mainline for uspace/app/sbi/src/input.c


Ignore:
Timestamp:
2010-03-26T21:55:23Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4204ad9
Parents:
b535aeb
Message:

Update SBI to rev. 144.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/input.c

    rb535aeb r37f527b  
    4242#define INPUT_BUFFER_SIZE 256
    4343
    44 static int input_init(input_t *input, char *fname);
     44static int input_init_file(input_t *input, char *fname);
     45static void input_init_interactive(input_t *input);
     46static void input_init_string(input_t *input, const char *str);
    4547
    46 /** Create new input object. */
    47 int input_new(input_t **input, char *fname)
     48/** Create new input object for reading from file. */
     49int input_new_file(input_t **input, char *fname)
    4850{
    4951        *input = malloc(sizeof(input_t));
     
    5153                return ENOMEM;
    5254
    53         return input_init(*input, fname);
     55        return input_init_file(*input, fname);
    5456}
    5557
    56 /** Initialize input object. */
    57 static int input_init(input_t *input, char *fname)
     58/** Create new input object for reading from interactive input. */
     59int input_new_interactive(input_t **input)
     60{
     61        *input = malloc(sizeof(input_t));
     62        if (*input == NULL)
     63                return ENOMEM;
     64
     65        input_init_interactive(*input);
     66        return EOK;
     67}
     68
     69/** Create new input object for reading from string. */
     70int input_new_string(input_t **input, const char *str)
     71{
     72        *input = malloc(sizeof(input_t));
     73        if (*input == NULL)
     74                return ENOMEM;
     75
     76        input_init_string(*input, str);
     77        return EOK;
     78}
     79
     80/** Initialize input object for reading from file. */
     81static int input_init_file(input_t *input, char *fname)
    5882{
    5983        FILE *f;
     
    7498}
    7599
     100/** Initialize input object for reading from interactive input. */
     101static void input_init_interactive(input_t *input)
     102{
     103        input->buffer = malloc(INPUT_BUFFER_SIZE);
     104        if (input->buffer == NULL) {
     105                printf("Memory allocation failed.\n");
     106                exit(1);
     107        }
     108
     109        input->line_no = 0;
     110        input->fin = NULL;
     111}
     112
     113/** Initialize input object for reading from string. */
     114static void input_init_string(input_t *input, const char *str)
     115{
     116        input->buffer = malloc(INPUT_BUFFER_SIZE);
     117        if (input->buffer == NULL) {
     118                printf("Memory allocation failed.\n");
     119                exit(1);
     120        }
     121
     122        input->str = str;
     123        input->line_no = 0;
     124        input->fin = NULL;
     125}
     126
    76127/** Get next line of input. */
    77128int input_get_line(input_t *input, char **line)
    78129{
    79         if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL)
    80                 input->buffer[0] = '\0';
     130        const char *sp;
     131        char *dp;
     132        size_t cnt;
    81133
    82         if (ferror(input->fin))
    83                 return EIO;
     134        if (input->fin != NULL) {
     135                /* Reading from file. */
     136                if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL)
     137                        input->buffer[0] = '\0';
    84138
    85         *line = input->buffer;
     139                if (ferror(input->fin))
     140                        return EIO;
     141
     142                *line = input->buffer;
     143        } else if (input->str != NULL) {
     144                /* Reading from a string constant. */
     145
     146                /* Copy one line. */
     147                sp = input->str;
     148                dp = input->buffer;
     149                cnt = 0;
     150                while (*sp != '\n' && *sp != '\0' &&
     151                    cnt < INPUT_BUFFER_SIZE - 2) {
     152                        *dp++ = *sp++;
     153                }
     154
     155                /* Advance to start of next line. */
     156                if (*sp == '\n')
     157                        *dp++ = *sp++;
     158
     159                *dp++ = '\0';
     160                input->str = sp;
     161                *line = input->buffer;
     162        } else {
     163                /* Interactive mode */
     164                if (input->line_no == 0)
     165                        printf("sbi> ");
     166                else
     167                        printf("...  ");
     168
     169                if (fgets(input->buffer, INPUT_BUFFER_SIZE, stdin) == NULL)
     170                        input->buffer[0] = '\0';
     171
     172                if (ferror(stdin))
     173                        return EIO;
     174
     175                *line = input->buffer;
     176        }
     177
    86178        ++input->line_no;
    87179        return EOK;
Note: See TracChangeset for help on using the changeset viewer.