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