pf.c revision 1a30de1f
11a30de1fSmrg/* $Xorg: pf.c,v 1.4 2001/02/09 02:05:56 xorgcvs Exp $ */ 21a30de1fSmrg/* 31a30de1fSmrg 41a30de1fSmrgCopyright 1988, 1998 The Open Group 51a30de1fSmrg 61a30de1fSmrgPermission to use, copy, modify, distribute, and sell this software and its 71a30de1fSmrgdocumentation for any purpose is hereby granted without fee, provided that 81a30de1fSmrgthe above copyright notice appear in all copies and that both that 91a30de1fSmrgcopyright notice and this permission notice appear in supporting 101a30de1fSmrgdocumentation. 111a30de1fSmrg 121a30de1fSmrgThe above copyright notice and this permission notice shall be included 131a30de1fSmrgin all copies or substantial portions of the Software. 141a30de1fSmrg 151a30de1fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 161a30de1fSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 171a30de1fSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 181a30de1fSmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 191a30de1fSmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 201a30de1fSmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 211a30de1fSmrgOTHER DEALINGS IN THE SOFTWARE. 221a30de1fSmrg 231a30de1fSmrgExcept as contained in this notice, the name of The Open Group shall 241a30de1fSmrgnot be used in advertising or otherwise to promote the sale, use or 251a30de1fSmrgother dealings in this Software without prior written authorization 261a30de1fSmrgfrom The Open Group. 271a30de1fSmrg 281a30de1fSmrg*/ 291a30de1fSmrg 301a30de1fSmrg#include <X11/Xos.h> 311a30de1fSmrg#include <X11/Xlib.h> 321a30de1fSmrg#include <stdio.h> 331a30de1fSmrg#include <ctype.h> 341a30de1fSmrg#include "xmodmap.h" 351a30de1fSmrg 361a30de1fSmrg#define NOTINFILEFILENAME "commandline" 371a30de1fSmrgconst char *inputFilename = NOTINFILEFILENAME; 381a30de1fSmrgint lineno = 0; 391a30de1fSmrg 401a30de1fSmrgvoid process_file (const char *filename) /* NULL means use stdin */ 411a30de1fSmrg{ 421a30de1fSmrg FILE *fp; 431a30de1fSmrg char buffer[BUFSIZ]; 441a30de1fSmrg 451a30de1fSmrg /* open the file, eventually we'll want to pipe through cpp */ 461a30de1fSmrg 471a30de1fSmrg if (!filename) { 481a30de1fSmrg fp = stdin; 491a30de1fSmrg inputFilename = "stdin"; 501a30de1fSmrg } else { 511a30de1fSmrg fp = fopen (filename, "r"); 521a30de1fSmrg if (!fp) { 531a30de1fSmrg fprintf (stderr, "%s: unable to open file '%s' for reading\n", 541a30de1fSmrg ProgramName, filename); 551a30de1fSmrg parse_errors++; 561a30de1fSmrg return; 571a30de1fSmrg } 581a30de1fSmrg inputFilename = filename; 591a30de1fSmrg } 601a30de1fSmrg 611a30de1fSmrg 621a30de1fSmrg /* read the input and filter */ 631a30de1fSmrg 641a30de1fSmrg if (verbose) { 651a30de1fSmrg printf ("! %s:\n", inputFilename); 661a30de1fSmrg } 671a30de1fSmrg 681a30de1fSmrg for (lineno = 0; ; lineno++) { 691a30de1fSmrg buffer[0] = '\0'; 701a30de1fSmrg if (fgets (buffer, BUFSIZ, fp) == NULL) 711a30de1fSmrg break; 721a30de1fSmrg 731a30de1fSmrg process_line (buffer); 741a30de1fSmrg } 751a30de1fSmrg 761a30de1fSmrg inputFilename = NOTINFILEFILENAME; 771a30de1fSmrg lineno = 0; 781a30de1fSmrg (void) fclose (fp); 791a30de1fSmrg} 801a30de1fSmrg 811a30de1fSmrg 821a30de1fSmrgvoid process_line (char *buffer) 831a30de1fSmrg{ 841a30de1fSmrg int len; 851a30de1fSmrg int i; 861a30de1fSmrg char *cp; 871a30de1fSmrg 881a30de1fSmrg /* copy buffer since it may point to unwritable date */ 891a30de1fSmrg len = strlen(buffer); 901a30de1fSmrg cp = chk_malloc(len + 1); 911a30de1fSmrg strcpy(cp, buffer); 921a30de1fSmrg buffer = cp; 931a30de1fSmrg 941a30de1fSmrg for (i = 0; i < len; i++) { /* look for blank lines */ 951a30de1fSmrg register char c = buffer[i]; 961a30de1fSmrg if (!(isspace(c) || c == '\n')) break; 971a30de1fSmrg } 981a30de1fSmrg if (i == len) return; 991a30de1fSmrg 1001a30de1fSmrg cp = &buffer[i]; 1011a30de1fSmrg 1021a30de1fSmrg if (*cp == '!') return; /* look for comments */ 1031a30de1fSmrg len -= (cp - buffer); /* adjust len by how much we skipped */ 1041a30de1fSmrg 1051a30de1fSmrg /* pipe through cpp */ 1061a30de1fSmrg 1071a30de1fSmrg /* strip trailing space */ 1081a30de1fSmrg for (i = len-1; i >= 0; i--) { 1091a30de1fSmrg register char c = cp[i]; 1101a30de1fSmrg if (!(isspace(c) || c == '\n')) break; 1111a30de1fSmrg } 1121a30de1fSmrg if (i >= 0) cp[len = (i+1)] = '\0'; /* nul terminate */ 1131a30de1fSmrg 1141a30de1fSmrg if (verbose) { 1151a30de1fSmrg printf ("! %d: %s\n", lineno+1, cp); 1161a30de1fSmrg } 1171a30de1fSmrg 1181a30de1fSmrg /* handle input */ 1191a30de1fSmrg handle_line (cp, len); 1201a30de1fSmrg} 121