home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / sed-3.02 / sed / sed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-02  |  5.8 KB  |  236 lines

  1. #define COPYRIGHT_NOTICE "Copyright (C) 1998 Free Software Foundation, Inc."
  2. #define BUG_ADDRESS "bug-gnu-utils@gnu.org"
  3.  
  4. /*  GNU SED, a batch stream editor.
  5.     Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1998 \
  6.     Free Software Foundation, Inc."
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 2, or (at your option)
  11.     any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  21.  
  22.  
  23. #include "config.h"
  24. #include <stdio.h>
  25.  
  26. #ifndef HAVE_STRING_H
  27. # include <strings.h>
  28. #else
  29. # include <string.h>
  30. #endif
  31.  
  32. #ifdef HAVE_STDLIB_H
  33. # include <stdlib.h>
  34. #endif
  35.  
  36. #ifdef HAVE_MMAP
  37. # ifdef HAVE_UNISTD_H
  38. #  include <unistd.h>
  39. # endif
  40. # include <sys/types.h>
  41. # include <sys/mman.h>
  42. # ifndef MAP_FAILED
  43. #  define MAP_FAILED    ((char *)-1)    /* what a stupid concept... */
  44. # endif
  45. # include <sys/stat.h>
  46. # ifndef S_ISREG
  47. #  define S_ISREG(m)    (((m)&S_IFMT) == S_IFMT)
  48. # endif
  49. #endif /* HAVE_MMAP */
  50.  
  51. #ifdef HAVE_SYS_TYPES_H
  52. # include <sys/types.h>
  53. #endif
  54. #include "regex-sed.h"
  55. #include "getopt.h"
  56. #include "basicdefs.h"
  57. #include "utils.h"
  58. #include "sed.h"
  59.  
  60. #ifndef HAVE_STDLIB_H
  61.  extern char *getenv P_((const char *));
  62. #endif
  63.  
  64. #ifdef STUB_FROM_RX_LIBRARY_USAGE
  65. extern void print_rexp P_((void));
  66. extern int rx_basic_unfaniverse_delay;
  67. #endif /*STUB_FROM_RX_LIBRARY_USAGE*/
  68.  
  69. static void usage P_((int));
  70.  
  71. flagT use_extended_syntax_p = 0;
  72.  
  73. flagT rx_testing = 0;
  74.  
  75. /* If set, don't write out the line unless explicitly told to */
  76. flagT no_default_output = 0;
  77.  
  78. /* Do we need to be pedantically POSIX compliant? */
  79. flagT POSIXLY_CORRECT;
  80.  
  81. static void
  82. usage(status)
  83.   int status;
  84. {
  85.   FILE *out = status ? stderr : stdout;
  86.  
  87.   fprintf(out, "\
  88. Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n\
  89. \n\
  90.   -n, --quiet, --silent\n\
  91.                  suppress automatic printing of pattern space\n\
  92.   -e script, --expression=script\n\
  93.                  add the script to the commands to be executed\n\
  94.   -f script-file, --file=script-file\n\
  95.                  add the contents of script-file to the commands to be executed\n\
  96.       --help     display this help and exit\n\
  97.   -V, --version  output version information and exit\n\
  98. \n\
  99. If no -e, --expression, -f, or --file option is given, then the first\n\
  100. non-option argument is taken as the sed script to interpret.  All\n\
  101. remaining arguments are names of input files; if no input files are\n\
  102. specified, then the standard input is read.\n\
  103. \n", myname);
  104.   fprintf(out, "E-mail bug reports to: %s .\n\
  105. Be sure to include the word ``%s'' somewhere in the ``Subject:'' field.\n",
  106.       BUG_ADDRESS, PACKAGE);
  107.   exit(status);
  108. }
  109.  
  110. int
  111. main(argc, argv)
  112.   int argc;
  113.   char **argv;
  114. {
  115.   static struct option longopts[] = {
  116.     {"rxtest", 0, NULL, 'r'},
  117.     {"expression", 1, NULL, 'e'},
  118.     {"file", 1, NULL, 'f'},
  119.     {"quiet", 0, NULL, 'n'},
  120.     {"silent", 0, NULL, 'n'},
  121.     {"version", 0, NULL, 'V'},
  122.     {"help", 0, NULL, 'h'},
  123.     {NULL, 0, NULL, 0}
  124.   };
  125.  
  126.   /* The complete compiled SED program that we are going to run: */
  127.   struct vector *the_program = NULL;
  128.   int opt;
  129.   flagT bad_input;    /* If this variable is non-zero at exit, one or
  130.                more of the input files couldn't be opened. */
  131.  
  132.   POSIXLY_CORRECT = (getenv("POSIXLY_CORRECT") != NULL);
  133. #ifdef STUB_FROM_RX_LIBRARY_USAGE
  134.   if (!rx_default_cache)
  135.     print_rexp();
  136.   /* Allow roughly a megabyte of cache space. */
  137.   rx_default_cache->bytes_allowed = 1<<20;
  138.   rx_basic_unfaniverse_delay = 512 * 4;
  139. #endif /*STUB_FROM_RX_LIBRARY_USAGE*/
  140.  
  141.   myname = *argv;
  142.   while ((opt = getopt_long(argc, argv, "hnrVe:f:", longopts, NULL)) != EOF)
  143.     {
  144.       switch (opt)
  145.     {
  146.     case 'n':
  147.       no_default_output = 1;
  148.       break;
  149.     case 'e':
  150.       the_program = compile_string(the_program, optarg);
  151.       break;
  152.     case 'f':
  153.       the_program = compile_file(the_program, optarg);
  154.       break;
  155.  
  156.     case 'r':
  157.       use_extended_syntax_p = 1;
  158.       rx_testing = 1;
  159.       break;
  160.     case 'V':
  161.       fprintf(stdout, "GNU %s version %s\n\n", PACKAGE, VERSION);
  162.       fprintf(stdout, "%s\n\
  163. This is free software; see the source for copying conditions.  There is NO\n\
  164. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
  165. to the extent permitted by law.\n\
  166. ", COPYRIGHT_NOTICE);
  167.       exit(0);
  168.     case 'h':
  169.       usage(0);
  170.     default:
  171.       usage(4);
  172.     }
  173.     }
  174.  
  175.   if (!the_program)
  176.     {
  177.       if (optind < argc)
  178.     the_program = compile_string(the_program, argv[optind++]);
  179.       else
  180.     usage(4);
  181.     }
  182.   check_final_program(the_program);
  183.  
  184.   bad_input = process_files(the_program, argv+optind);
  185.  
  186.   close_all_files();
  187.   ck_fclose(stdout);
  188.   if (bad_input)
  189.     exit(2);
  190.   return 0;
  191. }
  192.  
  193.  
  194. /* Attempt to mmap() a file.  On failure, just return a zero,
  195.    otherwise set *base and *len and return non-zero.  */
  196. flagT
  197. map_file(fp, base, len)
  198.   FILE *fp;
  199.   VOID **base;
  200.   size_t *len;
  201. {
  202. #ifdef HAVE_MMAP
  203.   struct stat s;
  204.   VOID *nbase;
  205.  
  206.   if (fstat(fileno(fp), &s) == 0
  207.       && S_ISREG(s.st_mode)
  208.       && s.st_size == CAST(size_t)s.st_size)
  209.     {
  210.       /* "As if" the whole file was read into memory at this moment... */
  211.       nbase = VCAST(VOID *)mmap(NULL, CAST(size_t)s.st_size, PROT_READ,
  212.                 MAP_PRIVATE, fileno(fp), CAST(off_t)0);
  213.       if (nbase != MAP_FAILED)
  214.     {
  215.       *base = nbase;
  216.       *len =  s.st_size;
  217.       return 1;
  218.     }
  219.     }
  220. #endif /* HAVE_MMAP */
  221.  
  222.   return 0;
  223. }
  224.  
  225. /* Attempt to munmap() a memory region. */
  226. void
  227. unmap_file(base, len)
  228.   VOID *base;
  229.   size_t len;
  230. {
  231. #ifdef HAVE_MMAP
  232.   if (base)
  233.     munmap(VCAST(caddr_t)base, len);
  234. #endif
  235. }
  236.