home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xmodmap / pf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-17  |  2.6 KB  |  110 lines

  1. /*
  2.  * xmodmap - program for loading keymap definitions into server
  3.  *
  4.  * $XConsortium: pf.c,v 1.4 91/07/17 22:26:40 rws Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Jim Fulton, MIT X Consortium
  19.  */
  20.  
  21. #include <X11/Xos.h>
  22. #include <X11/Xlib.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "xmodmap.h"
  26.  
  27. #define NOTINFILEFILENAME "commandline"
  28. char *inputFilename = NOTINFILEFILENAME;
  29. int lineno = 0;
  30.  
  31. void process_file (filename)
  32.     char *filename;            /* NULL means use stdin */
  33. {
  34.     FILE *fp;
  35.     char buffer[BUFSIZ];
  36.  
  37.     /* open the file, eventually we'll want to pipe through cpp */
  38.  
  39.     if (!filename) {
  40.     fp = stdin;
  41.     inputFilename = "stdin"; 
  42.     } else {
  43.     fp = fopen (filename, "r");
  44.     if (!fp) {
  45.         fprintf (stderr, "%s:  unable to open file '%s' for reading\n",
  46.              ProgramName, filename);
  47.         parse_errors++;
  48.         return;
  49.     }
  50.     inputFilename = filename;
  51.     }
  52.  
  53.  
  54.     /* read the input and filter */
  55.  
  56.     if (verbose) {
  57.     printf ("! %s:\n", inputFilename);
  58.     }
  59.  
  60.     for (lineno = 0; ; lineno++) {
  61.     buffer[0] = '\0';
  62.     if (fgets (buffer, BUFSIZ, fp) == NULL)
  63.       break;
  64.  
  65.     process_line (buffer);
  66.     }
  67.  
  68.     inputFilename = NOTINFILEFILENAME;
  69.     lineno = 0;
  70.     (void) fclose (fp);
  71. }
  72.  
  73.  
  74. void process_line (buffer)
  75.     char *buffer;
  76. {
  77.     int len;
  78.     int i;
  79.     char *cp;
  80.  
  81.     len = strlen (buffer);
  82.  
  83.     for (i = 0; i < len; i++) {        /* look for blank lines */
  84.     register char c = buffer[i];
  85.     if (!(isspace(c) || c == '\n')) break;
  86.     }
  87.     if (i == len) return;
  88.  
  89.     cp = &buffer[i];
  90.  
  91.     if (*cp == '!') return;        /* look for comments */
  92.     len -= (cp - buffer);        /* adjust len by how much we skipped */
  93.  
  94.                     /* pipe through cpp */
  95.  
  96.                     /* strip trailing space */
  97.     for (i = len-1; i >= 0; i--) {
  98.     register char c = cp[i];
  99.     if (!(isspace(c) || c == '\n')) break;
  100.     }
  101.     if (i >= 0) cp[len = (i+1)] = '\0';  /* nul terminate */
  102.  
  103.     if (verbose) {
  104.     printf ("! %d:  %s\n", lineno, cp);
  105.     }
  106.  
  107.     /* handle input */
  108.     handle_line (cp, len);
  109. }
  110.