GRASS Programmer's Manual  6.4.2(2012)
parse_mon.c
Go to the documentation of this file.
00001 /* parse_mon - parse monitorcap entry */
00002 
00003 #include <stdio.h>
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <grass/gis.h>
00008 #include <grass/glocale.h>
00009 #include <grass/raster.h>
00010 #include <grass/monitors.h>
00011 
00012 static FILE *monitors = NULL;
00013 static struct MON_CAP cap;
00014 
00015 static char *substr(char *, char *);
00016 static int read_line(FILE *, char *, int);
00017 
00018 struct MON_CAP *R_parse_monitorcap(int field, char *key)
00019 {
00020     int rewound;
00021     char line[1024];
00022     char *p;
00023     char file[500];
00024     char *gisbase;
00025 
00026     gisbase = G_gisbase();
00027 
00028     rewound = 0;
00029     if (!(field == MON_NEXT || field == MON_NAME ||
00030           field == MON_PATH || field == MON_LINK || field == MON_CLOSE))
00031         return (NULL);
00032     if (monitors == NULL) {
00033         sprintf(file, "%s/etc/monitorcap", gisbase);
00034         if ((monitors = fopen(file, "r")) == NULL)
00035             G_fatal_error("Unable to open %s", file);
00036     }
00037     else {
00038         if (field == MON_CLOSE) {
00039             fclose(monitors);
00040             monitors = NULL;
00041             return (NULL);
00042         }
00043     }
00044     while (-1) {
00045         if (read_line(monitors, line, sizeof line)) {
00046             if (field == MON_NEXT)
00047                 return (NULL);
00048             rewind(monitors);
00049             if (read_line(monitors, line, sizeof line) || rewound)
00050                 return (NULL);
00051             rewound = -1;
00052         }
00053         cap.path = cap.comment = cap.link = cap.tty = cap.where = NULL;
00054         if ((cap.name = G_malloc(strlen(line) + 1)) == NULL)
00055             return (NULL);
00056         strcpy(cap.name, line);
00057         if ((p = substr(":", cap.name)) != NULL) {
00058             *p++ = '\0';
00059             cap.path = p;
00060             if ((p = substr(":", p)) != NULL) {
00061                 *p++ = '\0';
00062                 cap.comment = p;
00063                 if ((p = substr(":", p)) != NULL) {
00064                     *p++ = '\0';
00065                     cap.link = p;
00066                     if ((p = substr(":", p)) != NULL) {
00067                         *p++ = '\0';
00068                         cap.tty = p;
00069                         if ((p = substr(":", p)) != NULL) {
00070                             *p++ = 0;
00071                             cap.where = p;
00072                             if ((p = substr("\n", p)) != NULL)
00073                                 *p = '\0';
00074                         }
00075                     }
00076                 }
00077             }
00078         }
00079         if (cap.path == NULL || cap.link == NULL || cap.where == NULL ||
00080             cap.tty == NULL || cap.comment == NULL)
00081             G_free(cap.name);
00082         else {
00083             sprintf(line, "%s/%s", gisbase, cap.path);
00084             cap.path = G_store(line);
00085 
00086             if (field == MON_NEXT ||
00087                 (field == MON_NAME && !strcmp(key, cap.name))
00088                 || (field == MON_PATH && !strcmp(key, cap.path))
00089                 || (field == MON_LINK && !strcmp(key, cap.link)))
00090                 return (&cap);
00091             else
00092                 G_free(cap.name);
00093         }
00094     }
00095 }
00096 
00097 /* read_line - read a line, possibly continued with a "\" into a buffer */
00098 
00099 static int read_line(FILE * file,       /* file from which to read */
00100                      char *line,        /* buffer in which to put it */
00101                      int size)
00102 {                               /* size of buffer */
00103     int length, full_line, eof, done;
00104     char c, last_c;
00105 
00106     *line = '\0';
00107     for (length = full_line = eof = 0; !full_line && !eof;) {   /*   one entire line at a time */
00108         while (1) {
00109             eof = (fgets(line + length, size - length - 1, file) == NULL);
00110             if (eof)
00111                 break;
00112             if (*(line + length) != '#')
00113                 break;
00114         }
00115         length = strlen(line) - 1;
00116         if (*(line + length) == '\n') {
00117             if (*(line + length - 1) == '\\')
00118                 --length;
00119             else
00120                 full_line = -1;
00121         }
00122         else {
00123             if (length != -1) {
00124                 fprintf(stderr, "error:  input line too long\n");
00125                 full_line = -1;
00126                 last_c = c = ' ';
00127                 for (done = 0; !done;) {
00128                     if ((c = getc(file)) != EOF) {
00129                         if (c == '\n' && last_c != '\\')
00130                             done = -1;
00131                         else
00132                             last_c = c;
00133                     }
00134                     else
00135                         eof = done = -1;
00136                 }
00137             }
00138         }
00139     }
00140     return (eof);
00141 }
00142 
00143 /* substr - find substring in a string.  returns pointer to start of */
00144 /* substring or NULL if not found */
00145 
00146 static char *substr(char *string, char *buffer)
00147 {
00148     int start, i, found;
00149     char c;
00150 
00151     start = i = found = 0;
00152     while ((c = *(buffer + start + i)) != '\0' && !found) {
00153         if (c == *(string + i)) {
00154             if (*(string + ++i) == '\0')
00155                 found = -1;
00156         }
00157         else {
00158             start++;
00159             i = 0;
00160         }
00161     }
00162     if (found)
00163         return (buffer + start);
00164     else
00165         return (NULL);
00166 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines