PageRenderTime 34ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/c/coreutils-7.4/src/csplit.c

http://timoseven.googlecode.com/
C | 1487 lines | 1064 code | 252 blank | 171 comment | 195 complexity | f8af443bce30d6420d6455af37c06f9e MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
  1. /* csplit - split a file into sections determined by context lines
  2. Copyright (C) 91, 1995-2009 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Stuart Kemp, cpsrk@groper.jcu.edu.au.
  14. Modified by David MacKenzie, djm@gnu.ai.mit.edu. */
  15. #include <config.h>
  16. #include <getopt.h>
  17. #include <sys/types.h>
  18. #include <signal.h>
  19. #include "system.h"
  20. #include <regex.h>
  21. #include "error.h"
  22. #include "fd-reopen.h"
  23. #include "quote.h"
  24. #include "safe-read.h"
  25. #include "stdio--.h"
  26. #include "xstrtol.h"
  27. /* The official name of this program (e.g., no `g' prefix). */
  28. #define PROGRAM_NAME "csplit"
  29. #define AUTHORS \
  30. proper_name ("Stuart Kemp"), \
  31. proper_name ("David MacKenzie")
  32. /* The default prefix for output file names. */
  33. #define DEFAULT_PREFIX "xx"
  34. /* A compiled pattern arg. */
  35. struct control
  36. {
  37. intmax_t offset; /* Offset from regexp to split at. */
  38. uintmax_t lines_required; /* Number of lines required. */
  39. uintmax_t repeat; /* Repeat count. */
  40. int argnum; /* ARGV index. */
  41. bool repeat_forever; /* True if `*' used as a repeat count. */
  42. bool ignore; /* If true, produce no output (for regexp). */
  43. bool regexpr; /* True if regular expression was used. */
  44. struct re_pattern_buffer re_compiled; /* Compiled regular expression. */
  45. };
  46. /* Initial size of data area in buffers. */
  47. #define START_SIZE 8191
  48. /* Increment size for data area. */
  49. #define INCR_SIZE 2048
  50. /* Number of lines kept in each node in line list. */
  51. #define CTRL_SIZE 80
  52. #ifdef DEBUG
  53. /* Some small values to test the algorithms. */
  54. # define START_SIZE 200
  55. # define INCR_SIZE 10
  56. # define CTRL_SIZE 1
  57. #endif
  58. /* A string with a length count. */
  59. struct cstring
  60. {
  61. size_t len;
  62. char *str;
  63. };
  64. /* Pointers to the beginnings of lines in the buffer area.
  65. These structures are linked together if needed. */
  66. struct line
  67. {
  68. size_t used; /* Number of offsets used in this struct. */
  69. size_t insert_index; /* Next offset to use when inserting line. */
  70. size_t retrieve_index; /* Next index to use when retrieving line. */
  71. struct cstring starts[CTRL_SIZE]; /* Lines in the data area. */
  72. struct line *next; /* Next in linked list. */
  73. };
  74. /* The structure to hold the input lines.
  75. Contains a pointer to the data area and a list containing
  76. pointers to the individual lines. */
  77. struct buffer_record
  78. {
  79. size_t bytes_alloc; /* Size of the buffer area. */
  80. size_t bytes_used; /* Bytes used in the buffer area. */
  81. uintmax_t start_line; /* First line number in this buffer. */
  82. uintmax_t first_available; /* First line that can be retrieved. */
  83. size_t num_lines; /* Number of complete lines in this buffer. */
  84. char *buffer; /* Data area. */
  85. struct line *line_start; /* Head of list of pointers to lines. */
  86. struct line *curr_line; /* The line start record currently in use. */
  87. struct buffer_record *next;
  88. };
  89. static void close_output_file (void);
  90. static void create_output_file (void);
  91. static void delete_all_files (bool);
  92. static void save_line_to_file (const struct cstring *line);
  93. void usage (int status);
  94. /* Start of buffer list. */
  95. static struct buffer_record *head = NULL;
  96. /* Partially read line. */
  97. static char *hold_area = NULL;
  98. /* Number of bytes in `hold_area'. */
  99. static size_t hold_count = 0;
  100. /* Number of the last line in the buffers. */
  101. static uintmax_t last_line_number = 0;
  102. /* Number of the line currently being examined. */
  103. static uintmax_t current_line = 0;
  104. /* If true, we have read EOF. */
  105. static bool have_read_eof = false;
  106. /* Name of output files. */
  107. static char *volatile filename_space = NULL;
  108. /* Prefix part of output file names. */
  109. static char const *volatile prefix = NULL;
  110. /* Suffix part of output file names. */
  111. static char *volatile suffix = NULL;
  112. /* Number of digits to use in output file names. */
  113. static int volatile digits = 2;
  114. /* Number of files created so far. */
  115. static unsigned int volatile files_created = 0;
  116. /* Number of bytes written to current file. */
  117. static uintmax_t bytes_written;
  118. /* Output file pointer. */
  119. static FILE *output_stream = NULL;
  120. /* Output file name. */
  121. static char *output_filename = NULL;
  122. /* Perhaps it would be cleaner to pass arg values instead of indexes. */
  123. static char **global_argv;
  124. /* If true, do not print the count of bytes in each output file. */
  125. static bool suppress_count;
  126. /* If true, remove output files on error. */
  127. static bool volatile remove_files;
  128. /* If true, remove all output files which have a zero length. */
  129. static bool elide_empty_files;
  130. /* The compiled pattern arguments, which determine how to split
  131. the input file. */
  132. static struct control *controls;
  133. /* Number of elements in `controls'. */
  134. static size_t control_used;
  135. /* The set of signals that are caught. */
  136. static sigset_t caught_signals;
  137. static struct option const longopts[] =
  138. {
  139. {"digits", required_argument, NULL, 'n'},
  140. {"quiet", no_argument, NULL, 'q'},
  141. {"silent", no_argument, NULL, 's'},
  142. {"keep-files", no_argument, NULL, 'k'},
  143. {"elide-empty-files", no_argument, NULL, 'z'},
  144. {"prefix", required_argument, NULL, 'f'},
  145. {"suffix-format", required_argument, NULL, 'b'},
  146. {GETOPT_HELP_OPTION_DECL},
  147. {GETOPT_VERSION_OPTION_DECL},
  148. {NULL, 0, NULL, 0}
  149. };
  150. /* Optionally remove files created so far; then exit.
  151. Called when an error detected. */
  152. static void
  153. cleanup (void)
  154. {
  155. sigset_t oldset;
  156. close_output_file ();
  157. sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
  158. delete_all_files (false);
  159. sigprocmask (SIG_SETMASK, &oldset, NULL);
  160. }
  161. static void cleanup_fatal (void) ATTRIBUTE_NORETURN;
  162. static void
  163. cleanup_fatal (void)
  164. {
  165. cleanup ();
  166. exit (EXIT_FAILURE);
  167. }
  168. extern void
  169. xalloc_die (void)
  170. {
  171. error (0, 0, "%s", _("memory exhausted"));
  172. cleanup_fatal ();
  173. }
  174. static void
  175. interrupt_handler (int sig)
  176. {
  177. delete_all_files (true);
  178. /* The signal has been reset to SIG_DFL, but blocked during this
  179. handler. Force the default action of this signal once the
  180. handler returns and the block is removed. */
  181. raise (sig);
  182. }
  183. /* Keep track of NUM bytes of a partial line in buffer START.
  184. These bytes will be retrieved later when another large buffer is read. */
  185. static void
  186. save_to_hold_area (char *start, size_t num)
  187. {
  188. free (hold_area);
  189. hold_area = start;
  190. hold_count = num;
  191. }
  192. /* Read up to MAX_N_BYTES bytes from the input stream into DEST.
  193. Return the number of bytes read. */
  194. static size_t
  195. read_input (char *dest, size_t max_n_bytes)
  196. {
  197. size_t bytes_read;
  198. if (max_n_bytes == 0)
  199. return 0;
  200. bytes_read = safe_read (STDIN_FILENO, dest, max_n_bytes);
  201. if (bytes_read == 0)
  202. have_read_eof = true;
  203. if (bytes_read == SAFE_READ_ERROR)
  204. {
  205. error (0, errno, _("read error"));
  206. cleanup_fatal ();
  207. }
  208. return bytes_read;
  209. }
  210. /* Initialize existing line record P. */
  211. static void
  212. clear_line_control (struct line *p)
  213. {
  214. p->used = 0;
  215. p->insert_index = 0;
  216. p->retrieve_index = 0;
  217. }
  218. /* Return a new, initialized line record. */
  219. static struct line *
  220. new_line_control (void)
  221. {
  222. struct line *p = xmalloc (sizeof *p);
  223. p->next = NULL;
  224. clear_line_control (p);
  225. return p;
  226. }
  227. /* Record LINE_START, which is the address of the start of a line
  228. of length LINE_LEN in the large buffer, in the lines buffer of B. */
  229. static void
  230. keep_new_line (struct buffer_record *b, char *line_start, size_t line_len)
  231. {
  232. struct line *l;
  233. /* If there is no existing area to keep line info, get some. */
  234. if (b->line_start == NULL)
  235. b->line_start = b->curr_line = new_line_control ();
  236. /* If existing area for lines is full, get more. */
  237. if (b->curr_line->used == CTRL_SIZE)
  238. {
  239. b->curr_line->next = new_line_control ();
  240. b->curr_line = b->curr_line->next;
  241. }
  242. l = b->curr_line;
  243. /* Record the start of the line, and update counters. */
  244. l->starts[l->insert_index].str = line_start;
  245. l->starts[l->insert_index].len = line_len;
  246. l->used++;
  247. l->insert_index++;
  248. }
  249. /* Scan the buffer in B for newline characters
  250. and record the line start locations and lengths in B.
  251. Return the number of lines found in this buffer.
  252. There may be an incomplete line at the end of the buffer;
  253. a pointer is kept to this area, which will be used when
  254. the next buffer is filled. */
  255. static size_t
  256. record_line_starts (struct buffer_record *b)
  257. {
  258. char *line_start; /* Start of current line. */
  259. char *line_end; /* End of each line found. */
  260. size_t bytes_left; /* Length of incomplete last line. */
  261. size_t lines; /* Number of lines found. */
  262. size_t line_length; /* Length of each line found. */
  263. if (b->bytes_used == 0)
  264. return 0;
  265. lines = 0;
  266. line_start = b->buffer;
  267. bytes_left = b->bytes_used;
  268. for (;;)
  269. {
  270. line_end = memchr (line_start, '\n', bytes_left);
  271. if (line_end == NULL)
  272. break;
  273. line_length = line_end - line_start + 1;
  274. keep_new_line (b, line_start, line_length);
  275. bytes_left -= line_length;
  276. line_start = line_end + 1;
  277. lines++;
  278. }
  279. /* Check for an incomplete last line. */
  280. if (bytes_left)
  281. {
  282. if (have_read_eof)
  283. {
  284. keep_new_line (b, line_start, bytes_left);
  285. lines++;
  286. }
  287. else
  288. save_to_hold_area (xmemdup (line_start, bytes_left), bytes_left);
  289. }
  290. b->num_lines = lines;
  291. b->first_available = b->start_line = last_line_number + 1;
  292. last_line_number += lines;
  293. return lines;
  294. }
  295. /* Return a new buffer with room to store SIZE bytes, plus
  296. an extra byte for safety. */
  297. static struct buffer_record *
  298. create_new_buffer (size_t size)
  299. {
  300. struct buffer_record *new_buffer = xmalloc (sizeof *new_buffer);
  301. new_buffer->buffer = xmalloc (size + 1);
  302. new_buffer->bytes_alloc = size;
  303. new_buffer->line_start = new_buffer->curr_line = NULL;
  304. return new_buffer;
  305. }
  306. /* Return a new buffer of at least MINSIZE bytes. If a buffer of at
  307. least that size is currently free, use it, otherwise create a new one. */
  308. static struct buffer_record *
  309. get_new_buffer (size_t min_size)
  310. {
  311. struct buffer_record *new_buffer; /* Buffer to return. */
  312. size_t alloc_size; /* Actual size that will be requested. */
  313. alloc_size = START_SIZE;
  314. if (alloc_size < min_size)
  315. {
  316. size_t s = min_size - alloc_size + INCR_SIZE - 1;
  317. alloc_size += s - s % INCR_SIZE;
  318. }
  319. new_buffer = create_new_buffer (alloc_size);
  320. new_buffer->num_lines = 0;
  321. new_buffer->bytes_used = 0;
  322. new_buffer->start_line = new_buffer->first_available = last_line_number + 1;
  323. new_buffer->next = NULL;
  324. return new_buffer;
  325. }
  326. static void
  327. free_buffer (struct buffer_record *buf)
  328. {
  329. free (buf->buffer);
  330. buf->buffer = NULL;
  331. }
  332. /* Append buffer BUF to the linked list of buffers that contain
  333. some data yet to be processed. */
  334. static void
  335. save_buffer (struct buffer_record *buf)
  336. {
  337. struct buffer_record *p;
  338. buf->next = NULL;
  339. buf->curr_line = buf->line_start;
  340. if (head == NULL)
  341. head = buf;
  342. else
  343. {
  344. for (p = head; p->next; p = p->next)
  345. /* Do nothing. */ ;
  346. p->next = buf;
  347. }
  348. }
  349. /* Fill a buffer of input.
  350. Set the initial size of the buffer to a default.
  351. Fill the buffer (from the hold area and input stream)
  352. and find the individual lines.
  353. If no lines are found (the buffer is too small to hold the next line),
  354. release the current buffer (whose contents would have been put in the
  355. hold area) and repeat the process with another large buffer until at least
  356. one entire line has been read.
  357. Return true if a new buffer was obtained, otherwise false
  358. (in which case end-of-file must have been encountered). */
  359. static bool
  360. load_buffer (void)
  361. {
  362. struct buffer_record *b;
  363. size_t bytes_wanted = START_SIZE; /* Minimum buffer size. */
  364. size_t bytes_avail; /* Size of new buffer created. */
  365. size_t lines_found; /* Number of lines in this new buffer. */
  366. char *p; /* Place to load into buffer. */
  367. if (have_read_eof)
  368. return false;
  369. /* We must make the buffer at least as large as the amount of data
  370. in the partial line left over from the last call. */
  371. if (bytes_wanted < hold_count)
  372. bytes_wanted = hold_count;
  373. while (1)
  374. {
  375. b = get_new_buffer (bytes_wanted);
  376. bytes_avail = b->bytes_alloc; /* Size of buffer returned. */
  377. p = b->buffer;
  378. /* First check the `holding' area for a partial line. */
  379. if (hold_count)
  380. {
  381. memcpy (p, hold_area, hold_count);
  382. p += hold_count;
  383. b->bytes_used += hold_count;
  384. bytes_avail -= hold_count;
  385. hold_count = 0;
  386. }
  387. b->bytes_used += read_input (p, bytes_avail);
  388. lines_found = record_line_starts (b);
  389. if (!lines_found)
  390. free_buffer (b);
  391. if (lines_found || have_read_eof)
  392. break;
  393. if (xalloc_oversized (2, b->bytes_alloc))
  394. xalloc_die ();
  395. bytes_wanted = 2 * b->bytes_alloc;
  396. free_buffer (b);
  397. free (b);
  398. }
  399. if (lines_found)
  400. save_buffer (b);
  401. else
  402. free (b);
  403. return lines_found != 0;
  404. }
  405. /* Return the line number of the first line that has not yet been retrieved. */
  406. static uintmax_t
  407. get_first_line_in_buffer (void)
  408. {
  409. if (head == NULL && !load_buffer ())
  410. error (EXIT_FAILURE, errno, _("input disappeared"));
  411. return head->first_available;
  412. }
  413. /* Return a pointer to the logical first line in the buffer and make the
  414. next line the logical first line.
  415. Return NULL if there is no more input. */
  416. static struct cstring *
  417. remove_line (void)
  418. {
  419. /* If non-NULL, this is the buffer for which the previous call
  420. returned the final line. So now, presuming that line has been
  421. processed, we can free the buffer and reset this pointer. */
  422. static struct buffer_record *prev_buf = NULL;
  423. struct cstring *line; /* Return value. */
  424. struct line *l; /* For convenience. */
  425. if (prev_buf)
  426. {
  427. free_buffer (prev_buf);
  428. prev_buf = NULL;
  429. }
  430. if (head == NULL && !load_buffer ())
  431. return NULL;
  432. if (current_line < head->first_available)
  433. current_line = head->first_available;
  434. ++(head->first_available);
  435. l = head->curr_line;
  436. line = &l->starts[l->retrieve_index];
  437. /* Advance index to next line. */
  438. if (++l->retrieve_index == l->used)
  439. {
  440. /* Go on to the next line record. */
  441. head->curr_line = l->next;
  442. if (head->curr_line == NULL || head->curr_line->used == 0)
  443. {
  444. /* Go on to the next data block.
  445. but first record the current one so we can free it
  446. once the line we're returning has been processed. */
  447. prev_buf = head;
  448. head = head->next;
  449. }
  450. }
  451. return line;
  452. }
  453. /* Search the buffers for line LINENUM, reading more input if necessary.
  454. Return a pointer to the line, or NULL if it is not found in the file. */
  455. static struct cstring *
  456. find_line (uintmax_t linenum)
  457. {
  458. struct buffer_record *b;
  459. if (head == NULL && !load_buffer ())
  460. return NULL;
  461. if (linenum < head->start_line)
  462. return NULL;
  463. for (b = head;;)
  464. {
  465. if (linenum < b->start_line + b->num_lines)
  466. {
  467. /* The line is in this buffer. */
  468. struct line *l;
  469. size_t offset; /* How far into the buffer the line is. */
  470. l = b->line_start;
  471. offset = linenum - b->start_line;
  472. /* Find the control record. */
  473. while (offset >= CTRL_SIZE)
  474. {
  475. l = l->next;
  476. offset -= CTRL_SIZE;
  477. }
  478. return &l->starts[offset];
  479. }
  480. if (b->next == NULL && !load_buffer ())
  481. return NULL;
  482. b = b->next; /* Try the next data block. */
  483. }
  484. }
  485. /* Return true if at least one more line is available for input. */
  486. static bool
  487. no_more_lines (void)
  488. {
  489. return find_line (current_line + 1) == NULL;
  490. }
  491. /* Open NAME as standard input. */
  492. static void
  493. set_input_file (const char *name)
  494. {
  495. if (! STREQ (name, "-") && fd_reopen (STDIN_FILENO, name, O_RDONLY, 0) < 0)
  496. error (EXIT_FAILURE, errno, _("cannot open %s for reading"), quote (name));
  497. }
  498. /* Write all lines from the beginning of the buffer up to, but
  499. not including, line LAST_LINE, to the current output file.
  500. If IGNORE is true, do not output lines selected here.
  501. ARGNUM is the index in ARGV of the current pattern. */
  502. static void
  503. write_to_file (uintmax_t last_line, bool ignore, int argnum)
  504. {
  505. struct cstring *line;
  506. uintmax_t first_line; /* First available input line. */
  507. uintmax_t lines; /* Number of lines to output. */
  508. uintmax_t i;
  509. first_line = get_first_line_in_buffer ();
  510. if (first_line > last_line)
  511. {
  512. error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
  513. cleanup_fatal ();
  514. }
  515. lines = last_line - first_line;
  516. for (i = 0; i < lines; i++)
  517. {
  518. line = remove_line ();
  519. if (line == NULL)
  520. {
  521. error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
  522. cleanup_fatal ();
  523. }
  524. if (!ignore)
  525. save_line_to_file (line);
  526. }
  527. }
  528. /* Output any lines left after all regexps have been processed. */
  529. static void
  530. dump_rest_of_file (void)
  531. {
  532. struct cstring *line;
  533. while ((line = remove_line ()) != NULL)
  534. save_line_to_file (line);
  535. }
  536. /* Handle an attempt to read beyond EOF under the control of record P,
  537. on iteration REPETITION if nonzero. */
  538. static void handle_line_error (const struct control *, uintmax_t)
  539. ATTRIBUTE_NORETURN;
  540. static void
  541. handle_line_error (const struct control *p, uintmax_t repetition)
  542. {
  543. char buf[INT_BUFSIZE_BOUND (uintmax_t)];
  544. fprintf (stderr, _("%s: %s: line number out of range"),
  545. program_name, quote (umaxtostr (p->lines_required, buf)));
  546. if (repetition)
  547. fprintf (stderr, _(" on repetition %s\n"), umaxtostr (repetition, buf));
  548. else
  549. fprintf (stderr, "\n");
  550. cleanup_fatal ();
  551. }
  552. /* Determine the line number that marks the end of this file,
  553. then get those lines and save them to the output file.
  554. P is the control record.
  555. REPETITION is the repetition number. */
  556. static void
  557. process_line_count (const struct control *p, uintmax_t repetition)
  558. {
  559. uintmax_t linenum;
  560. uintmax_t last_line_to_save = p->lines_required * (repetition + 1);
  561. struct cstring *line;
  562. create_output_file ();
  563. linenum = get_first_line_in_buffer ();
  564. while (linenum++ < last_line_to_save)
  565. {
  566. line = remove_line ();
  567. if (line == NULL)
  568. handle_line_error (p, repetition);
  569. save_line_to_file (line);
  570. }
  571. close_output_file ();
  572. /* Ensure that the line number specified is not 1 greater than
  573. the number of lines in the file. */
  574. if (no_more_lines ())
  575. handle_line_error (p, repetition);
  576. }
  577. static void regexp_error (struct control *, uintmax_t, bool) ATTRIBUTE_NORETURN;
  578. static void
  579. regexp_error (struct control *p, uintmax_t repetition, bool ignore)
  580. {
  581. fprintf (stderr, _("%s: %s: match not found"),
  582. program_name, quote (global_argv[p->argnum]));
  583. if (repetition)
  584. {
  585. char buf[INT_BUFSIZE_BOUND (uintmax_t)];
  586. fprintf (stderr, _(" on repetition %s\n"), umaxtostr (repetition, buf));
  587. }
  588. else
  589. fprintf (stderr, "\n");
  590. if (!ignore)
  591. {
  592. dump_rest_of_file ();
  593. close_output_file ();
  594. }
  595. cleanup_fatal ();
  596. }
  597. /* Read the input until a line matches the regexp in P, outputting
  598. it unless P->IGNORE is true.
  599. REPETITION is this repeat-count; 0 means the first time. */
  600. static void
  601. process_regexp (struct control *p, uintmax_t repetition)
  602. {
  603. struct cstring *line; /* From input file. */
  604. size_t line_len; /* To make "$" in regexps work. */
  605. uintmax_t break_line; /* First line number of next file. */
  606. bool ignore = p->ignore; /* If true, skip this section. */
  607. regoff_t ret;
  608. if (!ignore)
  609. create_output_file ();
  610. /* If there is no offset for the regular expression, or
  611. it is positive, then it is not necessary to buffer the lines. */
  612. if (p->offset >= 0)
  613. {
  614. for (;;)
  615. {
  616. line = find_line (++current_line);
  617. if (line == NULL)
  618. {
  619. if (p->repeat_forever)
  620. {
  621. if (!ignore)
  622. {
  623. dump_rest_of_file ();
  624. close_output_file ();
  625. }
  626. exit (EXIT_SUCCESS);
  627. }
  628. else
  629. regexp_error (p, repetition, ignore);
  630. }
  631. line_len = line->len;
  632. if (line->str[line_len - 1] == '\n')
  633. line_len--;
  634. ret = re_search (&p->re_compiled, line->str, line_len,
  635. 0, line_len, NULL);
  636. if (ret == -2)
  637. {
  638. error (0, 0, _("error in regular expression search"));
  639. cleanup_fatal ();
  640. }
  641. if (ret == -1)
  642. {
  643. line = remove_line ();
  644. if (!ignore)
  645. save_line_to_file (line);
  646. }
  647. else
  648. break;
  649. }
  650. }
  651. else
  652. {
  653. /* Buffer the lines. */
  654. for (;;)
  655. {
  656. line = find_line (++current_line);
  657. if (line == NULL)
  658. {
  659. if (p->repeat_forever)
  660. {
  661. if (!ignore)
  662. {
  663. dump_rest_of_file ();
  664. close_output_file ();
  665. }
  666. exit (EXIT_SUCCESS);
  667. }
  668. else
  669. regexp_error (p, repetition, ignore);
  670. }
  671. line_len = line->len;
  672. if (line->str[line_len - 1] == '\n')
  673. line_len--;
  674. ret = re_search (&p->re_compiled, line->str, line_len,
  675. 0, line_len, NULL);
  676. if (ret == -2)
  677. {
  678. error (0, 0, _("error in regular expression search"));
  679. cleanup_fatal ();
  680. }
  681. if (ret != -1)
  682. break;
  683. }
  684. }
  685. /* Account for any offset from this regexp. */
  686. break_line = current_line + p->offset;
  687. write_to_file (break_line, ignore, p->argnum);
  688. if (!ignore)
  689. close_output_file ();
  690. if (p->offset > 0)
  691. current_line = break_line;
  692. }
  693. /* Split the input file according to the control records we have built. */
  694. static void
  695. split_file (void)
  696. {
  697. size_t i;
  698. for (i = 0; i < control_used; i++)
  699. {
  700. uintmax_t j;
  701. if (controls[i].regexpr)
  702. {
  703. for (j = 0; (controls[i].repeat_forever
  704. || j <= controls[i].repeat); j++)
  705. process_regexp (&controls[i], j);
  706. }
  707. else
  708. {
  709. for (j = 0; (controls[i].repeat_forever
  710. || j <= controls[i].repeat); j++)
  711. process_line_count (&controls[i], j);
  712. }
  713. }
  714. create_output_file ();
  715. dump_rest_of_file ();
  716. close_output_file ();
  717. }
  718. /* Return the name of output file number NUM.
  719. This function is called from a signal handler, so it should invoke
  720. only reentrant functions that are async-signal-safe. POSIX does
  721. not guarantee this for the functions called below, but we don't
  722. know of any hosts where this implementation isn't safe. */
  723. static char *
  724. make_filename (unsigned int num)
  725. {
  726. strcpy (filename_space, prefix);
  727. if (suffix)
  728. sprintf (filename_space + strlen (prefix), suffix, num);
  729. else
  730. sprintf (filename_space + strlen (prefix), "%0*u", digits, num);
  731. return filename_space;
  732. }
  733. /* Create the next output file. */
  734. static void
  735. create_output_file (void)
  736. {
  737. sigset_t oldset;
  738. bool fopen_ok;
  739. int fopen_errno;
  740. output_filename = make_filename (files_created);
  741. /* Create the output file in a critical section, to avoid races. */
  742. sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
  743. output_stream = fopen (output_filename, "w");
  744. fopen_ok = (output_stream != NULL);
  745. fopen_errno = errno;
  746. files_created += fopen_ok;
  747. sigprocmask (SIG_SETMASK, &oldset, NULL);
  748. if (! fopen_ok)
  749. {
  750. error (0, fopen_errno, "%s", output_filename);
  751. cleanup_fatal ();
  752. }
  753. bytes_written = 0;
  754. }
  755. /* If requested, delete all the files we have created. This function
  756. must be called only from critical sections. */
  757. static void
  758. delete_all_files (bool in_signal_handler)
  759. {
  760. unsigned int i;
  761. if (! remove_files)
  762. return;
  763. for (i = 0; i < files_created; i++)
  764. {
  765. const char *name = make_filename (i);
  766. if (unlink (name) != 0 && !in_signal_handler)
  767. error (0, errno, "%s", name);
  768. }
  769. files_created = 0;
  770. }
  771. /* Close the current output file and print the count
  772. of characters in this file. */
  773. static void
  774. close_output_file (void)
  775. {
  776. if (output_stream)
  777. {
  778. if (ferror (output_stream))
  779. {
  780. error (0, 0, _("write error for %s"), quote (output_filename));
  781. output_stream = NULL;
  782. cleanup_fatal ();
  783. }
  784. if (fclose (output_stream) != 0)
  785. {
  786. error (0, errno, "%s", output_filename);
  787. output_stream = NULL;
  788. cleanup_fatal ();
  789. }
  790. if (bytes_written == 0 && elide_empty_files)
  791. {
  792. sigset_t oldset;
  793. bool unlink_ok;
  794. int unlink_errno;
  795. /* Remove the output file in a critical section, to avoid races. */
  796. sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
  797. unlink_ok = (unlink (output_filename) == 0);
  798. unlink_errno = errno;
  799. files_created -= unlink_ok;
  800. sigprocmask (SIG_SETMASK, &oldset, NULL);
  801. if (! unlink_ok)
  802. error (0, unlink_errno, "%s", output_filename);
  803. }
  804. else
  805. {
  806. if (!suppress_count)
  807. {
  808. char buf[INT_BUFSIZE_BOUND (uintmax_t)];
  809. fprintf (stdout, "%s\n", umaxtostr (bytes_written, buf));
  810. }
  811. }
  812. output_stream = NULL;
  813. }
  814. }
  815. /* Save line LINE to the output file and
  816. increment the character count for the current file. */
  817. static void
  818. save_line_to_file (const struct cstring *line)
  819. {
  820. fwrite (line->str, sizeof (char), line->len, output_stream);
  821. bytes_written += line->len;
  822. }
  823. /* Return a new, initialized control record. */
  824. static struct control *
  825. new_control_record (void)
  826. {
  827. static size_t control_allocated = 0; /* Total space allocated. */
  828. struct control *p;
  829. if (control_used == control_allocated)
  830. controls = X2NREALLOC (controls, &control_allocated);
  831. p = &controls[control_used++];
  832. p->regexpr = false;
  833. p->repeat = 0;
  834. p->repeat_forever = false;
  835. p->lines_required = 0;
  836. p->offset = 0;
  837. return p;
  838. }
  839. /* Check if there is a numeric offset after a regular expression.
  840. STR is the entire command line argument.
  841. P is the control record for this regular expression.
  842. NUM is the numeric part of STR. */
  843. static void
  844. check_for_offset (struct control *p, const char *str, const char *num)
  845. {
  846. if (xstrtoimax (num, NULL, 10, &p->offset, "") != LONGINT_OK)
  847. error (EXIT_FAILURE, 0, _("%s: integer expected after delimiter"), str);
  848. }
  849. /* Given that the first character of command line arg STR is '{',
  850. make sure that the rest of the string is a valid repeat count
  851. and store its value in P.
  852. ARGNUM is the ARGV index of STR. */
  853. static void
  854. parse_repeat_count (int argnum, struct control *p, char *str)
  855. {
  856. uintmax_t val;
  857. char *end;
  858. end = str + strlen (str) - 1;
  859. if (*end != '}')
  860. error (EXIT_FAILURE, 0, _("%s: `}' is required in repeat count"), str);
  861. *end = '\0';
  862. if (str+1 == end-1 && *(str+1) == '*')
  863. p->repeat_forever = true;
  864. else
  865. {
  866. if (xstrtoumax (str + 1, NULL, 10, &val, "") != LONGINT_OK)
  867. {
  868. error (EXIT_FAILURE, 0,
  869. _("%s}: integer required between `{' and `}'"),
  870. global_argv[argnum]);
  871. }
  872. p->repeat = val;
  873. }
  874. *end = '}';
  875. }
  876. /* Extract the regular expression from STR and check for a numeric offset.
  877. STR should start with the regexp delimiter character.
  878. Return a new control record for the regular expression.
  879. ARGNUM is the ARGV index of STR.
  880. Unless IGNORE is true, mark these lines for output. */
  881. static struct control *
  882. extract_regexp (int argnum, bool ignore, char const *str)
  883. {
  884. size_t len; /* Number of bytes in this regexp. */
  885. char delim = *str;
  886. char const *closing_delim;
  887. struct control *p;
  888. const char *err;
  889. closing_delim = strrchr (str + 1, delim);
  890. if (closing_delim == NULL)
  891. error (EXIT_FAILURE, 0,
  892. _("%s: closing delimiter `%c' missing"), str, delim);
  893. len = closing_delim - str - 1;
  894. p = new_control_record ();
  895. p->argnum = argnum;
  896. p->ignore = ignore;
  897. p->regexpr = true;
  898. p->re_compiled.buffer = NULL;
  899. p->re_compiled.allocated = 0;
  900. p->re_compiled.fastmap = xmalloc (UCHAR_MAX + 1);
  901. p->re_compiled.translate = NULL;
  902. re_syntax_options =
  903. RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
  904. err = re_compile_pattern (str + 1, len, &p->re_compiled);
  905. if (err)
  906. {
  907. error (0, 0, _("%s: invalid regular expression: %s"), str, err);
  908. cleanup_fatal ();
  909. }
  910. if (closing_delim[1])
  911. check_for_offset (p, str, closing_delim + 1);
  912. return p;
  913. }
  914. /* Extract the break patterns from args START through ARGC - 1 of ARGV.
  915. After each pattern, check if the next argument is a repeat count. */
  916. static void
  917. parse_patterns (int argc, int start, char **argv)
  918. {
  919. int i; /* Index into ARGV. */
  920. struct control *p; /* New control record created. */
  921. uintmax_t val;
  922. static uintmax_t last_val = 0;
  923. for (i = start; i < argc; i++)
  924. {
  925. if (*argv[i] == '/' || *argv[i] == '%')
  926. {
  927. p = extract_regexp (i, *argv[i] == '%', argv[i]);
  928. }
  929. else
  930. {
  931. p = new_control_record ();
  932. p->argnum = i;
  933. if (xstrtoumax (argv[i], NULL, 10, &val, "") != LONGINT_OK)
  934. error (EXIT_FAILURE, 0, _("%s: invalid pattern"), argv[i]);
  935. if (val == 0)
  936. error (EXIT_FAILURE, 0,
  937. _("%s: line number must be greater than zero"),
  938. argv[i]);
  939. if (val < last_val)
  940. {
  941. char buf[INT_BUFSIZE_BOUND (uintmax_t)];
  942. error (EXIT_FAILURE, 0,
  943. _("line number %s is smaller than preceding line number, %s"),
  944. quote (argv[i]), umaxtostr (last_val, buf));
  945. }
  946. if (val == last_val)
  947. error (0, 0,
  948. _("warning: line number %s is the same as preceding line number"),
  949. quote (argv[i]));
  950. last_val = val;
  951. p->lines_required = val;
  952. }
  953. if (i + 1 < argc && *argv[i + 1] == '{')
  954. {
  955. /* We have a repeat count. */
  956. i++;
  957. parse_repeat_count (i, p, argv[i]);
  958. }
  959. }
  960. }
  961. static unsigned int
  962. get_format_flags (char **format_ptr)
  963. {
  964. unsigned int count = 0;
  965. for (; **format_ptr; (*format_ptr)++)
  966. {
  967. switch (**format_ptr)
  968. {
  969. case '-':
  970. break;
  971. case '+':
  972. case ' ':
  973. count |= 1;
  974. break;
  975. case '#':
  976. count |= 2; /* Allow for 0x prefix preceding an `x' conversion. */
  977. break;
  978. default:
  979. return count;
  980. }
  981. }
  982. return count;
  983. }
  984. static size_t
  985. get_format_width (char **format_ptr)
  986. {
  987. unsigned long int val = 0;
  988. if (ISDIGIT (**format_ptr)
  989. && (xstrtoul (*format_ptr, format_ptr, 10, &val, NULL) != LONGINT_OK
  990. || SIZE_MAX < val))
  991. error (EXIT_FAILURE, 0, _("invalid format width"));
  992. /* Allow for enough octal digits to represent the value of UINT_MAX,
  993. even if the field width is less than that. */
  994. return MAX (val, (sizeof (unsigned int) * CHAR_BIT + 2) / 3);
  995. }
  996. static size_t
  997. get_format_prec (char **format_ptr)
  998. {
  999. if (**format_ptr != '.')
  1000. return 0;
  1001. (*format_ptr)++;
  1002. if (! ISDIGIT (**format_ptr))
  1003. return 0;
  1004. else
  1005. {
  1006. unsigned long int val;
  1007. if (xstrtoul (*format_ptr, format_ptr, 10, &val, NULL) != LONGINT_OK
  1008. || SIZE_MAX < val)
  1009. error (EXIT_FAILURE, 0, _("invalid format precision"));
  1010. return val;
  1011. }
  1012. }
  1013. static void
  1014. get_format_conv_type (char **format_ptr)
  1015. {
  1016. unsigned char ch = *(*format_ptr)++;
  1017. switch (ch)
  1018. {
  1019. case 'd':
  1020. case 'i':
  1021. case 'o':
  1022. case 'u':
  1023. case 'x':
  1024. case 'X':
  1025. break;
  1026. case 0:
  1027. error (EXIT_FAILURE, 0, _("missing conversion specifier in suffix"));
  1028. break;
  1029. default:
  1030. if (isprint (ch))
  1031. error (EXIT_FAILURE, 0,
  1032. _("invalid conversion specifier in suffix: %c"), ch);
  1033. else
  1034. error (EXIT_FAILURE, 0,
  1035. _("invalid conversion specifier in suffix: \\%.3o"), ch);
  1036. }
  1037. }
  1038. static size_t
  1039. max_out (char *format)
  1040. {
  1041. size_t out_count = 0;
  1042. bool percent = false;
  1043. while (*format)
  1044. {
  1045. if (*format++ != '%')
  1046. out_count++;
  1047. else if (*format == '%')
  1048. {
  1049. format++;
  1050. out_count++;
  1051. }
  1052. else
  1053. {
  1054. if (percent)
  1055. error (EXIT_FAILURE, 0,
  1056. _("too many %% conversion specifications in suffix"));
  1057. percent = true;
  1058. out_count += get_format_flags (&format);
  1059. {
  1060. size_t width = get_format_width (&format);
  1061. size_t prec = get_format_prec (&format);
  1062. out_count += MAX (width, prec);
  1063. }
  1064. get_format_conv_type (&format);
  1065. }
  1066. }
  1067. if (! percent)
  1068. error (EXIT_FAILURE, 0,
  1069. _("missing %% conversion specification in suffix"));
  1070. return out_count;
  1071. }
  1072. int
  1073. main (int argc, char **argv)
  1074. {
  1075. int optc;
  1076. unsigned long int val;
  1077. initialize_main (&argc, &argv);
  1078. set_program_name (argv[0]);
  1079. setlocale (LC_ALL, "");
  1080. bindtextdomain (PACKAGE, LOCALEDIR);
  1081. textdomain (PACKAGE);
  1082. atexit (close_stdout);
  1083. global_argv = argv;
  1084. controls = NULL;
  1085. control_used = 0;
  1086. suppress_count = false;
  1087. remove_files = true;
  1088. prefix = DEFAULT_PREFIX;
  1089. while ((optc = getopt_long (argc, argv, "f:b:kn:sqz", longopts, NULL)) != -1)
  1090. switch (optc)
  1091. {
  1092. case 'f':
  1093. prefix = optarg;
  1094. break;
  1095. case 'b':
  1096. suffix = optarg;
  1097. break;
  1098. case 'k':
  1099. remove_files = false;
  1100. break;
  1101. case 'n':
  1102. if (xstrtoul (optarg, NULL, 10, &val, "") != LONGINT_OK
  1103. || val > INT_MAX)
  1104. error (EXIT_FAILURE, 0, _("%s: invalid number"), optarg);
  1105. digits = val;
  1106. break;
  1107. case 's':
  1108. case 'q':
  1109. suppress_count = true;
  1110. break;
  1111. case 'z':
  1112. elide_empty_files = true;
  1113. break;
  1114. case_GETOPT_HELP_CHAR;
  1115. case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
  1116. default:
  1117. usage (EXIT_FAILURE);
  1118. }
  1119. if (argc - optind < 2)
  1120. {
  1121. if (argc <= optind)
  1122. error (0, 0, _("missing operand"));
  1123. else
  1124. error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
  1125. usage (EXIT_FAILURE);
  1126. }
  1127. if (suffix)
  1128. filename_space = xmalloc (strlen (prefix) + max_out (suffix) + 2);
  1129. else
  1130. filename_space = xmalloc (strlen (prefix) + digits + 2);
  1131. set_input_file (argv[optind++]);
  1132. parse_patterns (argc, optind, argv);
  1133. {
  1134. int i;
  1135. static int const sig[] =
  1136. {
  1137. /* The usual suspects. */
  1138. SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
  1139. #ifdef SIGPOLL
  1140. SIGPOLL,
  1141. #endif
  1142. #ifdef SIGPROF
  1143. SIGPROF,
  1144. #endif
  1145. #ifdef SIGVTALRM
  1146. SIGVTALRM,
  1147. #endif
  1148. #ifdef SIGXCPU
  1149. SIGXCPU,
  1150. #endif
  1151. #ifdef SIGXFSZ
  1152. SIGXFSZ,
  1153. #endif
  1154. };
  1155. enum { nsigs = sizeof sig / sizeof sig[0] };
  1156. struct sigaction act;
  1157. sigemptyset (&caught_signals);
  1158. for (i = 0; i < nsigs; i++)
  1159. {
  1160. sigaction (sig[i], NULL, &act);
  1161. if (act.sa_handler != SIG_IGN)
  1162. sigaddset (&caught_signals, sig[i]);
  1163. }
  1164. act.sa_handler = interrupt_handler;
  1165. act.sa_mask = caught_signals;
  1166. act.sa_flags = SA_NODEFER | SA_RESETHAND;
  1167. for (i = 0; i < nsigs; i++)
  1168. if (sigismember (&caught_signals, sig[i]))
  1169. sigaction (sig[i], &act, NULL);
  1170. }
  1171. split_file ();
  1172. if (close (STDIN_FILENO) != 0)
  1173. {
  1174. error (0, errno, _("read error"));
  1175. cleanup_fatal ();
  1176. }
  1177. exit (EXIT_SUCCESS);
  1178. }
  1179. void
  1180. usage (int status)
  1181. {
  1182. if (status != EXIT_SUCCESS)
  1183. fprintf (stderr, _("Try `%s --help' for more information.\n"),
  1184. program_name);
  1185. else
  1186. {
  1187. printf (_("\
  1188. Usage: %s [OPTION]... FILE PATTERN...\n\
  1189. "),
  1190. program_name);
  1191. fputs (_("\
  1192. Output pieces of FILE separated by PATTERN(s) to files `xx00', `xx01', ...,\n\
  1193. and output byte counts of each piece to standard output.\n\
  1194. \n\
  1195. "), stdout);
  1196. fputs (_("\
  1197. Mandatory arguments to long options are mandatory for short options too.\n\
  1198. "), stdout);
  1199. fputs (_("\
  1200. -b, --suffix-format=FORMAT use sprintf FORMAT instead of %02d\n\
  1201. -f, --prefix=PREFIX use PREFIX instead of `xx'\n\
  1202. -k, --keep-files do not remove output files on errors\n\
  1203. "), stdout);
  1204. fputs (_("\
  1205. -n, --digits=DIGITS use specified number of digits instead of 2\n\
  1206. -s, --quiet, --silent do not print counts of output file sizes\n\
  1207. -z, --elide-empty-files remove empty output files\n\
  1208. "), stdout);
  1209. fputs (HELP_OPTION_DESCRIPTION, stdout);
  1210. fputs (VERSION_OPTION_DESCRIPTION, stdout);
  1211. fputs (_("\
  1212. \n\
  1213. Read standard input if FILE is -. Each PATTERN may be:\n\
  1214. "), stdout);
  1215. fputs (_("\
  1216. \n\
  1217. INTEGER copy up to but not including specified line number\n\
  1218. /REGEXP/[OFFSET] copy up to but not including a matching line\n\
  1219. %REGEXP%[OFFSET] skip to, but not including a matching line\n\
  1220. {INTEGER} repeat the previous pattern specified number of times\n\
  1221. {*} repeat the previous pattern as many times as possible\n\
  1222. \n\
  1223. A line OFFSET is a required `+' or `-' followed by a positive integer.\n\
  1224. "), stdout);
  1225. emit_bug_reporting_address ();
  1226. }
  1227. exit (status);
  1228. }