| 1 | /******************************************************************************* |
|---|
| 2 | * |
|---|
| 3 | * McStas, neutron ray-tracing package |
|---|
| 4 | * Copyright 1997-2002, All rights reserved |
|---|
| 5 | * Risoe National Laboratory, Roskilde, Denmark |
|---|
| 6 | * Institut Laue Langevin, Grenoble, France |
|---|
| 7 | * |
|---|
| 8 | * Kernel: file.c |
|---|
| 9 | * |
|---|
| 10 | * %Identification |
|---|
| 11 | * Written by: K.N. |
|---|
| 12 | * Date: Sep 25, 1998 |
|---|
| 13 | * Origin: Risoe |
|---|
| 14 | * Release: McStas 1.6 |
|---|
| 15 | * Version: $Revision: 1.19 $ |
|---|
| 16 | * |
|---|
| 17 | * Code to handle files and command line arguments. |
|---|
| 18 | * |
|---|
| 19 | * $Id: file.c,v 1.19 2007-03-12 14:57:18 farhi Exp $ |
|---|
| 20 | * |
|---|
| 21 | * $Log: file.c,v $ |
|---|
| 22 | * Revision 1.19 2007-03-12 14:57:18 farhi |
|---|
| 23 | * Cosmetics |
|---|
| 24 | * |
|---|
| 25 | * Revision 1.18 2006/04/19 13:06:25 farhi |
|---|
| 26 | * * Updated Release, Version and Origin fields in headers |
|---|
| 27 | * * Improved setversion to update all McStasx.y occurencies into current release |
|---|
| 28 | * * Added 'string' type for DEFINITION parameters to be handled as this type so that auto-quoting occurs in mcgui |
|---|
| 29 | * * Added possibility to save log of the session to a file (appended) in mcgui |
|---|
| 30 | * * Made Scilab use either TCL_EvalStr or TK_EvalStr |
|---|
| 31 | * |
|---|
| 32 | * Revision 1.17 2005/03/02 10:07:23 farhi |
|---|
| 33 | * Now displays info/warning when using contributed components |
|---|
| 34 | * |
|---|
| 35 | * Revision 1.16 2004/09/10 15:09:56 farhi |
|---|
| 36 | * Use same macro symbols for mcstas kernel and run-time for code uniformity |
|---|
| 37 | * |
|---|
| 38 | * Revision 1.15 2003/02/11 12:28:45 farhi |
|---|
| 39 | * Variouxs bug fixes after tests in the lib directory |
|---|
| 40 | * mcstas_r : disable output with --no-out.. flag. Fix 1D McStas output |
|---|
| 41 | * read_table:corrected MC_SYS_DIR -> MCSTAS define |
|---|
| 42 | * monitor_nd-lib: fix Log(signal) log(coord) |
|---|
| 43 | * HOPG.trm: reduce 4000 points -> 400 which is enough and faster to resample |
|---|
| 44 | * Progress_bar: precent -> percent parameter |
|---|
| 45 | * CS: ---------------------------------------------------------------------- |
|---|
| 46 | * |
|---|
| 47 | * Revision 1.3 2000/02/15 07:40:59 kn |
|---|
| 48 | * Also search for components in a fixed list of system dir subdirectories. |
|---|
| 49 | * |
|---|
| 50 | * Revision 1.2 1998/10/02 08:36:21 kn |
|---|
| 51 | * Fixed header comment. |
|---|
| 52 | * |
|---|
| 53 | * Revision 1.1 1998/10/01 08:13:41 kn |
|---|
| 54 | * Initial revision |
|---|
| 55 | * |
|---|
| 56 | *******************************************************************************/ |
|---|
| 57 | |
|---|
| 58 | #include <stdlib.h> |
|---|
| 59 | #include <stdio.h> |
|---|
| 60 | |
|---|
| 61 | #include "mcstas.h" |
|---|
| 62 | |
|---|
| 63 | static List search_list = NULL; |
|---|
| 64 | |
|---|
| 65 | /* MOD: E. Farhi, Oct 2nd, 2001: add obsolete dir. Aug 27th, 2002: added share+contrib */ |
|---|
| 66 | static char *sys_subdir_table[] = |
|---|
| 67 | { "samples", "monitors", "sources", "optics", "misc" , "obsolete", "contrib", "share", "examples" }; |
|---|
| 68 | |
|---|
| 69 | /* Attempt to open FILE in directory DIR (or current directory if DIR is |
|---|
| 70 | NULL). */ |
|---|
| 71 | static FILE * |
|---|
| 72 | try_open_file(char *dir, char *name) |
|---|
| 73 | { |
|---|
| 74 | char *path = |
|---|
| 75 | dir != NULL ? str_cat (dir, MC_PATHSEP_S, name, NULL) : str_dup(name); |
|---|
| 76 | FILE *f = fopen(path, "r"); |
|---|
| 77 | str_free(path); |
|---|
| 78 | return f; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /* This variable stores the full path for the definition of a component |
|---|
| 82 | as found by the function try_open_component() (called from |
|---|
| 83 | open_component_search()). */ |
|---|
| 84 | char *component_pathname; |
|---|
| 85 | |
|---|
| 86 | static FILE * |
|---|
| 87 | try_open_component(char *dir, char *name) |
|---|
| 88 | { |
|---|
| 89 | static char *suffixes[] = {".comp", ".cmp", ".com"}; |
|---|
| 90 | int i; |
|---|
| 91 | |
|---|
| 92 | for(i = 0; i < sizeof(suffixes)/sizeof(*suffixes); i++) |
|---|
| 93 | { |
|---|
| 94 | char *path = |
|---|
| 95 | dir != NULL ? |
|---|
| 96 | str_cat (dir, MC_PATHSEP_S, name, suffixes[i], NULL) : |
|---|
| 97 | str_cat(name, suffixes[i], NULL); |
|---|
| 98 | FILE *f = fopen(path, "r"); |
|---|
| 99 | if(f != NULL) |
|---|
| 100 | { |
|---|
| 101 | component_pathname = path; |
|---|
| 102 | return f; |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | str_free(path); |
|---|
| 106 | } |
|---|
| 107 | return NULL; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | char * |
|---|
| 111 | get_sys_dir(void) |
|---|
| 112 | { |
|---|
| 113 | static char *sys_dir = NULL; |
|---|
| 114 | |
|---|
| 115 | if(sys_dir == NULL) |
|---|
| 116 | { |
|---|
| 117 | sys_dir = getenv("MCSTAS"); |
|---|
| 118 | if(sys_dir == NULL) |
|---|
| 119 | sys_dir = MCSTAS; |
|---|
| 120 | sys_dir = str_dup(sys_dir); |
|---|
| 121 | } |
|---|
| 122 | return sys_dir; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /* Generic file search function. */ |
|---|
| 126 | static FILE * |
|---|
| 127 | generic_open_file_search(char *name, FILE *(*try_open)(char *, char *)) |
|---|
| 128 | { |
|---|
| 129 | List_handle liter; |
|---|
| 130 | char *dir; |
|---|
| 131 | FILE *f; |
|---|
| 132 | int i; |
|---|
| 133 | |
|---|
| 134 | f = (*try_open)(NULL, name); |
|---|
| 135 | if(f != NULL) |
|---|
| 136 | return f; |
|---|
| 137 | if(search_list != NULL) |
|---|
| 138 | { |
|---|
| 139 | liter = list_iterate(search_list); |
|---|
| 140 | while(dir = list_next(liter)) |
|---|
| 141 | { |
|---|
| 142 | f = (*try_open)(dir, name); |
|---|
| 143 | if(f != NULL) |
|---|
| 144 | return f; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | /* Now look in the system directory. */ |
|---|
| 148 | f = (*try_open)(get_sys_dir(), name); |
|---|
| 149 | if(f != NULL) |
|---|
| 150 | return f; |
|---|
| 151 | /* Finally look in the fixed list of system subdirectories. */ |
|---|
| 152 | for(i = 0; i < sizeof(sys_subdir_table)/sizeof(char *); i++) { |
|---|
| 153 | dir = str_cat(get_sys_dir(), MC_PATHSEP_S, sys_subdir_table[i], NULL); |
|---|
| 154 | f = (*try_open)(dir, name); |
|---|
| 155 | str_free(dir); |
|---|
| 156 | if(f != NULL) |
|---|
| 157 | { |
|---|
| 158 | if (!strcmp(sys_subdir_table[i], "obsolete")) fprintf(stderr, "Warning: '%s' is an obsolete component (not maintained).\n", name); |
|---|
| 159 | if (!strcmp(sys_subdir_table[i], "contrib")) fprintf(stderr, "Info: '%s' is a contributed component.\n", name); |
|---|
| 160 | return f; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | /* Not found. */ |
|---|
| 164 | return NULL; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /* Open file, searching the full search path. */ |
|---|
| 168 | FILE * |
|---|
| 169 | open_file_search(char *name) |
|---|
| 170 | { |
|---|
| 171 | return generic_open_file_search(name, try_open_file); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /* Open component definition, searching the full search path. */ |
|---|
| 175 | FILE * |
|---|
| 176 | open_component_search(char *name) |
|---|
| 177 | { |
|---|
| 178 | return generic_open_file_search(name, try_open_component); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /* Open file, searching only the system directory. */ |
|---|
| 182 | FILE * |
|---|
| 183 | open_file_search_sys(char *name) |
|---|
| 184 | { |
|---|
| 185 | return try_open_file(get_sys_dir(), name); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | /* Add a directory to the search path. */ |
|---|
| 190 | void |
|---|
| 191 | add_search_dir(char *name) |
|---|
| 192 | { |
|---|
| 193 | if(search_list == NULL) |
|---|
| 194 | search_list = list_create(); |
|---|
| 195 | list_add(search_list, name); |
|---|
| 196 | } |
|---|
| 197 | |
|---|