gerbv
gerb_file.c
Go to the documentation of this file.
1 /*
2  * gEDA - GNU Electronic Design Automation
3  * This file is a part of gerbv.
4  *
5  * Copyright (C) 2000-2002 Stefan Petersen (spe@stacken.kth.se)
6  *
7  * $Id$
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22  */
23 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_MMAN_H
43 #include <sys/mman.h>
44 #endif
45 #include <errno.h>
46 #include <glib/gstdio.h>
47 
48 #include "common.h"
49 #include "gerbv.h"
50 #include "gerb_file.h"
51 
52 /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */
53 #undef DPRINTF
54 #define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
55 
56 gerb_file_t *
57 gerb_fopen(char const * filename)
58 {
59  gerb_file_t *fd;
60  struct stat statinfo;
61 
62  DPRINTF("---> Entering gerb_fopen, filename = %s\n", filename);
63 
64  /* g_new() aborts on allocation failure, so no NULL check needed. */
65  fd = g_new(gerb_file_t, 1);
66 
67  DPRINTF(" Doing fopen\n");
68  /* fopen() can't open files with non ASCII filenames on windows */
69  fd->fd = g_fopen(filename, "rb");
70  if (fd->fd == NULL) {
71  int saved_errno = errno;
72  g_free(fd);
73  errno = saved_errno;
74  return NULL;
75  }
76 
77  DPRINTF(" Doing fstat\n");
78  fd->ptr = 0;
79  fd->fileno = fileno(fd->fd);
80  if (fstat(fd->fileno, &statinfo) < 0) {
81  int saved_errno = errno;
82  fclose(fd->fd);
83  g_free(fd);
84  errno = saved_errno;
85  return NULL;
86  }
87 
88  DPRINTF(" Checking S_ISREG\n");
89  if (!S_ISREG(statinfo.st_mode)) {
90  fclose(fd->fd);
91  g_free(fd);
92  errno = EISDIR;
93  return NULL;
94  }
95 
96  DPRINTF(" Checking statinfo.st_size\n");
97  if ((int)statinfo.st_size == 0) {
98  fclose(fd->fd);
99  g_free(fd);
100  errno = EIO; /* More compatible with the world outside Linux */
101  return NULL;
102  }
103 
104 #ifdef HAVE_SYS_MMAN_H
105 
106  DPRINTF(" Doing mmap\n");
107  fd->datalen = (int)statinfo.st_size;
108  fd->data = (char *)mmap(0, statinfo.st_size, PROT_READ, MAP_PRIVATE,
109  fd->fileno, 0);
110  if(fd->data == MAP_FAILED) {
111  int saved_errno = errno;
112  fclose(fd->fd);
113  g_free(fd);
114  errno = saved_errno;
115  return NULL;
116  } else {
117  /* Copy into a heap buffer with null terminator so strtol/strtod
118  * have a safe stopping point — mmap does not guarantee '\0'
119  * after the file content.
120  * g_malloc() aborts on allocation failure, so no NULL check
121  * needed. */
122  char *buf = (char *)g_malloc(fd->datalen + 1);
123  memcpy(buf, fd->data, fd->datalen);
124  buf[fd->datalen] = '\0';
125  munmap(fd->data, fd->datalen);
126  fd->data = buf;
127  }
128 
129 #else
130  /* all systems without mmap, not only MINGW32 */
131 
132  DPRINTF(" Doing calloc\n");
133  fd->datalen = (int)statinfo.st_size;
134  fd->data = calloc(1, statinfo.st_size + 1);
135  if (fd->data == NULL) {
136  int saved_errno = errno;
137  fclose(fd->fd);
138  g_free(fd);
139  errno = saved_errno;
140  return NULL;
141  }
142  if (fread((void*)fd->data, 1, statinfo.st_size, fd->fd) != statinfo.st_size) {
143  int saved_errno = errno;
144  fclose(fd->fd);
145  g_free(fd->data);
146  g_free(fd);
147  errno = saved_errno;
148  return NULL;
149  }
150  rewind (fd->fd);
151 
152 #endif
153 
154  DPRINTF(" Setting filename\n");
155  fd->filename = g_strdup(filename);
156 
157  DPRINTF("<--- Leaving gerb_fopen\n");
158  return fd;
159 } /* gerb_fopen */
160 
161 
162 int
163 gerb_fgetc(gerb_file_t *fd)
164 {
165 
166  if (fd->ptr >= fd->datalen)
167  return EOF;
168 
169  return (int) fd->data[fd->ptr++];
170 } /* gerb_fgetc */
171 
172 
173 int
174 gerb_fgetint(gerb_file_t *fd, int *len)
175 {
176  long int result;
177  char *end;
178 
179  if (fd->ptr >= fd->datalen) {
180  if (len)
181  *len = 0;
182  return 0;
183  }
184 
185  errno = 0;
186  result = strtol(fd->data + fd->ptr, &end, 10);
187  if (errno) {
188  GERB_COMPILE_ERROR(_("Failed to read integer"));
189  return 0;
190  }
191 
192  if (len) {
193  *len = end - (fd->data + fd->ptr);
194  }
195 
196  fd->ptr = end - fd->data;
197 
198  if (len && (result < 0))
199  *len -= 1;
200 
201  return (int)result;
202 } /* gerb_fgetint */
203 
204 
205 double
206 gerb_fgetdouble(gerb_file_t *fd)
207 {
208  char *start;
209  int remaining;
210  double result;
211  char *end;
212 
213  if (fd->ptr >= fd->datalen)
214  return 0.0;
215 
216  start = fd->data + fd->ptr;
217  remaining = fd->datalen - fd->ptr;
218 
219  /* Prevent strtod from consuming hex float notation (0x.../0X...).
220  * In Gerber aperture macros, x/X is the multiplication operator,
221  * so "0X25.4" must parse as "0" followed by "X25.4", not as a
222  * hexadecimal floating-point literal. */
223  if (remaining >= 2
224  && start[0] == '0' && (start[1] == 'x' || start[1] == 'X')) {
225  fd->ptr += 1;
226  return 0.0;
227  }
228 
229  errno = 0;
230  result = strtod(start, &end);
231  if (errno) {
232  GERB_COMPILE_ERROR(_("Failed to read double"));
233  return 0.0;
234  }
235 
236  fd->ptr = end - fd->data;
237 
238  return result;
239 } /* gerb_fgetdouble */
240 
241 
242 char *
243 gerb_fgetstring(gerb_file_t *fd, char term)
244 {
245  char *strend = NULL;
246  char *newstr;
247  char *i, *iend;
248  int len;
249 
250  iend = fd->data + fd->datalen;
251  for (i = fd->data + fd->ptr; i < iend; i++) {
252  if (*i == term) {
253  strend = i;
254  break;
255  }
256  }
257 
258  if (strend == NULL)
259  return NULL;
260 
261  len = strend - (fd->data + fd->ptr);
262 
263  /* g_malloc() aborts on allocation failure, so no NULL check needed. */
264  newstr = (char *)g_malloc(len + 1);
265  strncpy(newstr, fd->data + fd->ptr, len);
266  newstr[len] = '\0';
267  fd->ptr += len;
268 
269  return newstr;
270 } /* gerb_fgetstring */
271 
272 
273 void
274 gerb_ungetc(gerb_file_t *fd)
275 {
276  if (fd->ptr)
277  fd->ptr--;
278 
279  return;
280 } /* gerb_ungetc */
281 
282 
283 void
284 gerb_fclose(gerb_file_t *fd)
285 {
286  if (fd) {
287  g_free(fd->filename);
288 
289  /* fd->data is always heap-allocated: the mmap path now copies
290  * into a g_malloc'd buffer (for null termination) before
291  * munmap, and the non-mmap path uses calloc. */
292  g_free(fd->data);
293  if (fclose(fd->fd) == EOF)
294  GERB_FATAL_ERROR("fclose: %s", strerror(errno));
295  g_free(fd);
296  }
297 
298  return;
299 } /* gerb_fclose */
300 
301 
302 char *
303 gerb_find_file(char const * filename, char **paths)
304 {
305  char *curr_path = NULL;
306  char *complete_path = NULL;
307  int i;
308 
309 #ifdef DEBUG
310  if( DEBUG > 0 ) {
311  for (i = 0; paths[i] != NULL; i++) {
312  printf("%s(): paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
313  }
314  }
315 #endif
316 
317  for (i = 0; paths[i] != NULL; i++) {
318  DPRINTF("%s(): Try paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
319 
320  /*
321  * Environment variables start with a $ sign
322  */
323  if (paths[i][0] == '$') {
324  char *env_name, *env_value, *tmp;
325  int len;
326 
327  /* Extract environment name. Remember we start with a $ */
328 
329  tmp = strchr(paths[i], G_DIR_SEPARATOR);
330  if (tmp == NULL)
331  len = strlen(paths[i]) - 1;
332  else
333  len = tmp - paths[i] - 1;
334  /* g_malloc() aborts on allocation failure, so no NULL check
335  * needed. */
336  env_name = (char *)g_malloc(len + 1);
337  strncpy(env_name, (char *)(paths[i] + 1), len);
338  env_name[len] = '\0';
339 
340  env_value = getenv(env_name);
341  DPRINTF("%s(): Trying \"%s\" = \"%s\" from the environment\n",
342  __FUNCTION__, env_name,
343  env_value == NULL ? "(null)" : env_value);
344 
345  if (env_value == NULL) {
346  curr_path = NULL;
347  } else {
348  curr_path = (char *)g_malloc(strlen(env_value) + strlen(&paths[i][len + 1]) + 1);
349  strcpy(curr_path, env_value);
350  strcat(curr_path, &paths[i][len + 1]);
351  g_free(env_name);
352  }
353  } else {
354  curr_path = paths[i];
355  }
356 
357  if (curr_path != NULL) {
358  /*
359  * Build complete path (inc. filename) and check if file exists.
360  */
361  /* g_build_filename() uses g_malloc() internally — aborts on
362  * allocation failure, so no NULL check needed. */
363  complete_path = g_build_filename(curr_path, filename, NULL);
364 
365  if (paths[i][0] == '$') {
366  g_free(curr_path);
367  curr_path = NULL;
368  }
369 
370  DPRINTF("%s(): Tring to access \"%s\"\n", __FUNCTION__,
371  complete_path);
372 
373  if (access(complete_path, R_OK) != -1)
374  break;
375 
376  g_free(complete_path);
377  complete_path = NULL;
378  }
379  }
380 
381  if (complete_path == NULL)
382  errno = ENOENT;
383 
384  DPRINTF("%s(): returning complete_path = \"%s\"\n", __FUNCTION__,
385  complete_path == NULL ? "(null)" : complete_path);
386 
387  return complete_path;
388 } /* gerb_find_file */
char * gerb_find_file(char const *filename, char **paths)
Search for files in directories pointed out by paths, a NULL terminated list of directories to search...
Definition: gerb_file.c:303
Header info for the file parsing support functions.
The main header file for the libgerbv library.