| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - 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 | * - 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 | /** @file Input module
|
|---|
| 30 | *
|
|---|
| 31 | * Reads source code from a file.
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <stdlib.h>
|
|---|
| 36 | #include "mytypes.h"
|
|---|
| 37 | #include "strtab.h"
|
|---|
| 38 |
|
|---|
| 39 | #include "input.h"
|
|---|
| 40 |
|
|---|
| 41 | #define INPUT_BUFFER_SIZE 256
|
|---|
| 42 |
|
|---|
| 43 | static int input_init_file(input_t *input, char *fname);
|
|---|
| 44 | static void input_init_interactive(input_t *input);
|
|---|
| 45 | static void input_init_string(input_t *input, const char *str);
|
|---|
| 46 |
|
|---|
| 47 | /** Create new input object for reading from file. */
|
|---|
| 48 | int input_new_file(input_t **input, char *fname)
|
|---|
| 49 | {
|
|---|
| 50 | *input = malloc(sizeof(input_t));
|
|---|
| 51 | if (*input == NULL)
|
|---|
| 52 | return ENOMEM;
|
|---|
| 53 |
|
|---|
| 54 | return input_init_file(*input, fname);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /** Create new input object for reading from interactive input. */
|
|---|
| 58 | int input_new_interactive(input_t **input)
|
|---|
| 59 | {
|
|---|
| 60 | *input = malloc(sizeof(input_t));
|
|---|
| 61 | if (*input == NULL)
|
|---|
| 62 | return ENOMEM;
|
|---|
| 63 |
|
|---|
| 64 | input_init_interactive(*input);
|
|---|
| 65 | return EOK;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** Create new input object for reading from string. */
|
|---|
| 69 | int input_new_string(input_t **input, const char *str)
|
|---|
| 70 | {
|
|---|
| 71 | *input = malloc(sizeof(input_t));
|
|---|
| 72 | if (*input == NULL)
|
|---|
| 73 | return ENOMEM;
|
|---|
| 74 |
|
|---|
| 75 | input_init_string(*input, str);
|
|---|
| 76 | return EOK;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Initialize input object for reading from file. */
|
|---|
| 80 | static int input_init_file(input_t *input, char *fname)
|
|---|
| 81 | {
|
|---|
| 82 | FILE *f;
|
|---|
| 83 |
|
|---|
| 84 | f = fopen(fname, "rt");
|
|---|
| 85 | if (f == NULL)
|
|---|
| 86 | return ENOENT;
|
|---|
| 87 |
|
|---|
| 88 | input->buffer = malloc(INPUT_BUFFER_SIZE);
|
|---|
| 89 | if (input->buffer == NULL) {
|
|---|
| 90 | printf("Memory allocation failed.\n");
|
|---|
| 91 | exit(1);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | input->line_no = 0;
|
|---|
| 95 | input->fin = f;
|
|---|
| 96 | return EOK;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /** Initialize input object for reading from interactive input. */
|
|---|
| 100 | static void input_init_interactive(input_t *input)
|
|---|
| 101 | {
|
|---|
| 102 | input->buffer = malloc(INPUT_BUFFER_SIZE);
|
|---|
| 103 | if (input->buffer == NULL) {
|
|---|
| 104 | printf("Memory allocation failed.\n");
|
|---|
| 105 | exit(1);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | input->line_no = 0;
|
|---|
| 109 | input->fin = NULL;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /** Initialize input object for reading from string. */
|
|---|
| 113 | static void input_init_string(input_t *input, const char *str)
|
|---|
| 114 | {
|
|---|
| 115 | input->buffer = malloc(INPUT_BUFFER_SIZE);
|
|---|
| 116 | if (input->buffer == NULL) {
|
|---|
| 117 | printf("Memory allocation failed.\n");
|
|---|
| 118 | exit(1);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | input->str = str;
|
|---|
| 122 | input->line_no = 0;
|
|---|
| 123 | input->fin = NULL;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Get next line of input. */
|
|---|
| 127 | int input_get_line(input_t *input, char **line)
|
|---|
| 128 | {
|
|---|
| 129 | const char *sp;
|
|---|
| 130 | char *dp;
|
|---|
| 131 | size_t cnt;
|
|---|
| 132 |
|
|---|
| 133 | if (input->fin != NULL) {
|
|---|
| 134 | /* Reading from file. */
|
|---|
| 135 | if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL)
|
|---|
| 136 | input->buffer[0] = '\0';
|
|---|
| 137 |
|
|---|
| 138 | if (ferror(input->fin))
|
|---|
| 139 | return EIO;
|
|---|
| 140 |
|
|---|
| 141 | *line = input->buffer;
|
|---|
| 142 | } else if (input->str != NULL) {
|
|---|
| 143 | /* Reading from a string constant. */
|
|---|
| 144 |
|
|---|
| 145 | /* Copy one line. */
|
|---|
| 146 | sp = input->str;
|
|---|
| 147 | dp = input->buffer;
|
|---|
| 148 | cnt = 0;
|
|---|
| 149 | while (*sp != '\n' && *sp != '\0' &&
|
|---|
| 150 | cnt < INPUT_BUFFER_SIZE - 2) {
|
|---|
| 151 | *dp++ = *sp++;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /* Advance to start of next line. */
|
|---|
| 155 | if (*sp == '\n')
|
|---|
| 156 | *dp++ = *sp++;
|
|---|
| 157 |
|
|---|
| 158 | *dp++ = '\0';
|
|---|
| 159 | input->str = sp;
|
|---|
| 160 | *line = input->buffer;
|
|---|
| 161 | } else {
|
|---|
| 162 | /* Interactive mode */
|
|---|
| 163 | if (input->line_no == 0)
|
|---|
| 164 | printf("sbi> ");
|
|---|
| 165 | else
|
|---|
| 166 | printf("... ");
|
|---|
| 167 |
|
|---|
| 168 | if (fgets(input->buffer, INPUT_BUFFER_SIZE, stdin) == NULL)
|
|---|
| 169 | input->buffer[0] = '\0';
|
|---|
| 170 |
|
|---|
| 171 | if (ferror(stdin))
|
|---|
| 172 | return EIO;
|
|---|
| 173 |
|
|---|
| 174 | *line = input->buffer;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | ++input->line_no;
|
|---|
| 178 | return EOK;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /** Get number of the last provided line of input. */
|
|---|
| 182 | int input_get_line_no(input_t *input)
|
|---|
| 183 | {
|
|---|
| 184 | return input->line_no;
|
|---|
| 185 | }
|
|---|