Changeset 37f527b in mainline for uspace/app/sbi/src/input.c
- Timestamp:
- 2010-03-26T21:55:23Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4204ad9
- Parents:
- b535aeb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/input.c
rb535aeb r37f527b 42 42 #define INPUT_BUFFER_SIZE 256 43 43 44 static int input_init(input_t *input, char *fname); 44 static int input_init_file(input_t *input, char *fname); 45 static void input_init_interactive(input_t *input); 46 static void input_init_string(input_t *input, const char *str); 45 47 46 /** Create new input object . */47 int input_new (input_t **input, char *fname)48 /** Create new input object for reading from file. */ 49 int input_new_file(input_t **input, char *fname) 48 50 { 49 51 *input = malloc(sizeof(input_t)); … … 51 53 return ENOMEM; 52 54 53 return input_init (*input, fname);55 return input_init_file(*input, fname); 54 56 } 55 57 56 /** Initialize input object. */ 57 static int input_init(input_t *input, char *fname) 58 /** Create new input object for reading from interactive input. */ 59 int 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. */ 70 int 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. */ 81 static int input_init_file(input_t *input, char *fname) 58 82 { 59 83 FILE *f; … … 74 98 } 75 99 100 /** Initialize input object for reading from interactive input. */ 101 static 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. */ 114 static 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 76 127 /** Get next line of input. */ 77 128 int input_get_line(input_t *input, char **line) 78 129 { 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; 81 133 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'; 84 138 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 86 178 ++input->line_no; 87 179 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.