1/*
2
3Copyright 1988, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29#ifdef HAVE_CONFIG_H
30# include "config.h"
31#endif
32
33#include <X11/Xos.h>
34#include <X11/Xlib.h>
35#include <stdio.h>
36#include <ctype.h>
37#include <stdlib.h>
38#include "xmodmap.h"
39
40#define NOTINFILEFILENAME "commandline"
41const char *inputFilename = NOTINFILEFILENAME;
42int lineno = 0;
43
44void process_file (const char *filename)	/* NULL means use stdin */
45{
46    FILE *fp;
47    char buffer[BUFSIZ];
48
49    /* open the file, eventually we'll want to pipe through cpp */
50
51    if (!filename) {
52	fp = stdin;
53	inputFilename = "stdin";
54    } else {
55	fp = fopen (filename, "r");
56	if (!fp) {
57	    fprintf (stderr, "%s:  unable to open file '%s' for reading\n",
58		     ProgramName, filename);
59	    parse_errors++;
60	    return;
61	}
62	inputFilename = filename;
63    }
64
65
66    /* read the input and filter */
67
68    if (verbose) {
69	printf ("! %s:\n", inputFilename);
70    }
71
72    for (lineno = 0; ; lineno++) {
73	buffer[0] = '\0';
74	if (fgets (buffer, BUFSIZ, fp) == NULL)
75	  break;
76
77	process_line (buffer);
78    }
79
80    inputFilename = NOTINFILEFILENAME;
81    lineno = 0;
82    (void) fclose (fp);
83}
84
85
86void process_line (const char *line)
87{
88    int len;
89    int i;
90    char *cp, *buffer;
91
92    /* copy line to buffer since it may point to unwritable data */
93    len = strlen(line);
94    cp = buffer = strdup(line);
95    if (buffer == NULL) {
96	fprintf(stderr, "%s: Could not allocate %d bytes\n", ProgramName, len);
97	Exit(-1);
98    }
99
100    for (i = 0; i < len; i++) {		/* look for blank lines */
101	register char c = buffer[i];
102	if (!(isspace(c) || c == '\n')) break;
103    }
104    if (i == len) goto done;
105
106    cp = &buffer[i];
107
108    if (*cp == '!') goto done;		/* look for comments */
109    len -= (cp - buffer);		/* adjust len by how much we skipped */
110
111					/* pipe through cpp */
112
113					/* strip trailing space */
114    for (i = len-1; i >= 0; i--) {
115	register char c = cp[i];
116	if (!(isspace(c) || c == '\n')) break;
117    }
118    if (i >= 0) cp[len = (i+1)] = '\0';  /* nul terminate */
119
120    if (verbose) {
121	printf ("! %d:  %s\n", lineno+1, cp);
122    }
123
124    /* handle input */
125    handle_line (cp, len);
126
127  done:
128    free(buffer);
129}
130