program.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include "os/os.h"
00039 #include "mytypes.h"
00040 #include "stype.h"
00041 #include "input.h"
00042 #include "lex.h"
00043 #include "parse.h"
00044 
00045 #include "program.h"
00046 
00047 #define MAX_NAME_SIZE 64
00048 static char name_buf[MAX_NAME_SIZE + 1];
00049 
00057 int program_file_process(stree_program_t *program, const char *fname)
00058 {
00059         input_t *input;
00060         lex_t lex;
00061         parse_t parse;
00062         int rc;
00063 
00064         rc = input_new_file(&input, fname);
00065         if (rc != EOK) {
00066                 printf("Failed opening source file '%s'.\n", fname);
00067                 return EIO;
00068         }
00069 
00070         /* Parse input file. */
00071         lex_init(&lex, input);
00072         parse_init(&parse, program, &lex);
00073         parse_module(&parse);
00074 
00075         /* Check for parse errors. */
00076         if (parse.error)
00077                 return EINVAL;
00078 
00079         return EOK;
00080 }
00081 
00093 int program_lib_process(stree_program_t *program)
00094 {
00095         int rc;
00096         char *path, *fname;
00097         char *tmp;
00098         char *cp;
00099 
00100         FILE *f;
00101 
00102         path = os_get_lib_path();
00103         fname = os_str_acat(path, "/libflist");
00104 
00105         f = fopen(fname, "rt");
00106         if (f == NULL) {
00107                 printf("Failed opening library list file '%s'.\n", fname);
00108                 free(path);
00109                 free(fname);
00110                 return EIO;
00111         }
00112 
00113         free(fname);
00114 
00115         while (b_true) {
00116                 if (fgets(name_buf, MAX_NAME_SIZE + 1, f) == NULL)
00117                         break;
00118 
00119                 /*
00120                  * Remove trailing newline, if present.
00121                  */
00122                 cp = name_buf;
00123                 while (*cp != '\0' && *cp != '\n')
00124                         ++cp;
00125 
00126                 if (*cp == '\n')
00127                         *cp = '\0';
00128 
00129                 tmp = os_str_acat(path, "/");
00130                 fname = os_str_acat(tmp, name_buf);
00131                 free(tmp);
00132 
00133                 rc = program_file_process(program, fname);
00134                 if (rc != EOK) {
00135                         free(fname);
00136                         free(path);
00137                         fclose(f);
00138                         return rc;
00139                 }
00140 
00141                 free(fname);
00142         }
00143 
00144         if (ferror(f)) {
00145                 printf("Error reading from library list file.\n");
00146                 free(path);
00147                 fclose(f);
00148                 return EIO;
00149         }
00150 
00151         free(path);
00152         fclose(f);
00153 
00154         return EOK;
00155 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7