home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / locale / response / mkresponse.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-18  |  2.4 KB  |  144 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <getopt.h>
  6. #include <locale.h>
  7. #include <localeinfo.h>
  8.  
  9. unsigned char *codename;
  10.  
  11. struct response_info rinfo;
  12.  
  13. unsigned char **lines[] =
  14. {
  15.   (unsigned char **) &rinfo.yesexpr,
  16.   (unsigned char **) &rinfo.noexpr,
  17.   NULL
  18. };
  19.  
  20. int parse (void);
  21.  
  22. int
  23. main (int argc, char *argv[])
  24. {
  25.   int i;
  26.   unsigned char *outname = "LC_RESPONSE";
  27.  
  28.   while ((i = getopt (argc, argv, "o:")) != EOF)
  29.     {
  30.       switch (i)
  31.     {
  32.     case 'o':
  33.       outname = optarg;
  34.       break;
  35.     }
  36.     }
  37.  
  38.   if (argc - optind > 1)
  39.     {
  40.       (void) fprintf (stderr, "Usage: %s [-o out_file_name] [file]\n", argv[0]);
  41.       return 3;
  42.     }
  43.   else if ((argc - optind) == 1)
  44.     {
  45.       if (freopen (argv[optind], "r", stdin) == NULL)
  46.     {
  47.       perror (argv[optind]);
  48.       return 2;
  49.     }
  50.     }
  51.  
  52.   if (!parse ())
  53.     return 1;
  54.  
  55.   return !write_out (outname);
  56. }
  57.  
  58. void
  59. write_str (char *str, FILE * ofp)
  60. {
  61.   short int slen = strlen (str) + 1;
  62.  
  63.   (void) fwrite (&slen, sizeof (slen), 1, ofp);
  64.   (void) fwrite (str, sizeof (char), slen, ofp);
  65. }
  66.  
  67. int
  68. write_out (outname)
  69.      unsigned char *outname;
  70. {
  71.   FILE *ofp = fopen (outname, "w");
  72.  
  73.   if (ofp == NULL)
  74.     return 0;
  75.  
  76.   write_str (rinfo.yesexpr, ofp);
  77.   write_str (rinfo.noexpr, ofp);
  78. #ifndef NOGUARD
  79.   (void) fwrite (codename, sizeof (unsigned char), strlen (codename) + 1, ofp);
  80. #endif
  81.   (void) fclose (ofp);
  82.   return 1;
  83. }
  84.  
  85. unsigned char iline[1024];
  86.  
  87. int
  88. parse ()
  89. {
  90.   int codename_seen = 0;
  91.   int lineno = 0;
  92.   unsigned char ***target = lines;
  93.   unsigned char *cp;
  94.  
  95.   while (fgets (iline, sizeof (iline), stdin) != NULL)
  96.     {
  97.       lineno++;
  98.       if (iline[0] == '#')
  99.     continue;
  100.       if ((cp = strchr (iline, '\n')) == NULL)
  101.     {
  102.       (void) fprintf (stderr, "Line %d: buffer overflow\n", lineno);
  103.       return 0;
  104.     }
  105.       *cp = '\0';
  106.       if (strlen (iline) == 0)
  107.     continue;
  108.       if (!codename_seen)
  109.     {
  110.       if (grok_codename ())
  111.         {
  112.           codename_seen = 1;
  113.           continue;
  114.         }
  115.       else
  116.         return 0;
  117.     }
  118.       **target++ = strdup (iline);
  119.       if (*target == NULL)
  120.     return 1;
  121.     }
  122.   return 0;
  123. }
  124.  
  125. int
  126. grok_codename ()
  127. {
  128.   unsigned char *cp = iline;
  129.  
  130.   while (*cp && *cp == ' ' && *cp == '\t')
  131.     cp++;
  132.   if (*cp == '\0')
  133.     return 0;
  134.   if (strncmp (cp, "codeset ", 8) && strncmp (cp, "codeset\t", 8))
  135.     return 0;
  136.   cp += 8;
  137.   while (*cp && *cp == ' ' && *cp == '\t')
  138.     cp++;
  139.   if (*cp == '\0')
  140.     return 0;
  141.   codename = strdup (cp);
  142.   return 1;
  143. }
  144.