示例#1
0
文件: fmt.c 项目: AhmadTux/freebsd
/* Here is perhaps the right place to mention that this code is
 * all in top-down order. Hence, |main| comes first.
 */
int
main(int argc, char *argv[]) {
  int ch;			/* used for |getopt| processing */
  wchar_t *tmp;
  size_t len;
  const char *src;

  (void) setlocale(LC_CTYPE, "");

  /* 1. Grok parameters. */

  while ((ch = getopt(argc, argv, "0123456789cd:hl:mnpst:w:")) != -1)
  switch(ch) {
    case 'c':
      centerP = 1;
      format_troff = 1;
      continue;
    case 'd':
      src = optarg;
      len = mbsrtowcs(NULL, &src, 0, NULL);
      if (len == (size_t)-1)
        err(EX_USAGE, "bad sentence-ending character set");
      tmp = XMALLOC((len + 1) * sizeof(wchar_t));
      mbsrtowcs(tmp, &src, len + 1, NULL);
      sentence_enders = tmp;
      continue;
    case 'l':
      output_tab_width
        = get_nonnegative(optarg, "output tab width must be non-negative", 1);
      continue;
    case 'm':
      grok_mail_headers = 1;
      continue;
    case 'n':
      format_troff = 1;
      continue;
    case 'p':
      allow_indented_paragraphs = 1;
      continue;
    case 's':
      coalesce_spaces_P = 1;
      continue;
    case 't':
      tab_width = get_positive(optarg, "tab width must be positive", 1);
      continue;
    case 'w':
      goal_length = get_positive(optarg, "width must be positive", 1);
      max_length = goal_length;
      continue;
    case '0': case '1': case '2': case '3': case '4': case '5':
    case '6': case '7': case '8': case '9':
    /* XXX  this is not a stylistically approved use of getopt() */
      if (goal_length==0) {
        char *p;
        p = argv[optind - 1];
        if (p[0] == '-' && p[1] == ch && !p[2])
             goal_length = get_positive(++p, "width must be nonzero", 1);
        else
             goal_length = get_positive(argv[optind]+1,
                 "width must be nonzero", 1);
        max_length = goal_length;
      }
      continue;
    case 'h': default:
      fprintf(stderr,
"usage:   fmt [-cmps] [-d chars] [-l num] [-t num]\n"
"             [-w width | -width | goal [maximum]] [file ...]\n"
"Options: -c     center each line instead of formatting\n"
"         -d <chars> double-space after <chars> at line end\n"
"         -l <n> turn each <n> spaces at start of line into a tab\n"
"         -m     try to make sure mail header lines stay separate\n"
"         -n     format lines beginning with a dot\n"
"         -p     allow indented paragraphs\n"
"         -s     coalesce whitespace inside lines\n"
"         -t <n> have tabs every <n> columns\n"
"         -w <n> set maximum width to <n>\n"
"         goal   set target width to goal\n");
      exit(ch=='h' ? 0 : EX_USAGE);
  }
  argc -= optind; argv += optind;

  /* [ goal [ maximum ] ] */

  if (argc>0 && goal_length==0
      && (goal_length=get_positive(*argv,"goal length must be positive", 0))
         != 0) {
    --argc; ++argv;
    if (argc>0
        && (max_length=get_positive(*argv,"max length must be positive", 0))
           != 0) {
      --argc; ++argv;
      if (max_length<goal_length)
        errx(EX_USAGE, "max length must be >= goal length");
    }
  }
  if (goal_length==0) goal_length = 65;
  if (max_length==0) max_length = goal_length+10;
  if (max_length >= SIZE_T_MAX / sizeof (wchar_t)) errx(EX_USAGE, "max length too large");
  /* really needn't be longer */
  output_buffer = XMALLOC((max_length+1) * sizeof(wchar_t));

  /* 2. Process files. */

  if (argc>0) {
    while (argc-->0) process_named_file(*argv++);
  }
  else {
    process_stream(stdin, "standard input");
  }

  /* We're done. */

  return n_errors ? EX_NOINPUT : 0;

}
示例#2
0
/* Here is perhaps the right place to mention that this code is
 * all in top-down order. Hence, |main| comes first.
 */
int
main(int argc, char *argv[])
{
	int ch;			/* used for |getopt| processing */

	(void)setlocale(LC_CTYPE, "");

	/* 1. Grok parameters. */
	while ((ch = getopt(argc, argv, "0123456789cd:hl:mnpst:w:")) != -1) {
		switch (ch) {
		case 'c':
			centerP = 1;
			break;
		case 'd':
			sentence_enders = optarg;
			break;
		case 'l':
			output_tab_width
				= get_positive(optarg, "output tab width must be positive", 1);
			break;
		case 'm':
			grok_mail_headers = 1;
			break;
		case 'n':
			format_troff = 1;
			break;
		case 'p':
			allow_indented_paragraphs = 1;
			break;
		case 's':
			coalesce_spaces_P = 1;
			break;
		case 't':
			tab_width = get_positive(optarg, "tab width must be positive", 1);
			break;
		case 'w':
			goal_length = get_positive(optarg, "width must be positive", 1);
			max_length = goal_length;
			break;
		case '0': case '1': case '2': case '3': case '4': case '5':
		case '6': case '7': case '8': case '9':
			/* XXX  this is not a stylistically approved use of getopt() */
			if (goal_length == 0) {
				char *p;

				p = argv[optind - 1];
				if (p[0] == '-' && p[1] == ch && !p[2])
					goal_length = get_positive(++p, "width must be nonzero", 1);
				else
					goal_length = get_positive(argv[optind]+1,
							"width must be nonzero", 1);
				max_length = goal_length;
			}
			break;
		case 'h':
		default:
			usage();
			/* NOT REACHED */
		}
	}

	argc -= optind;
	argv += optind;

	/* [ goal [ maximum ] ] */
	if (argc > 0 && goal_length == 0 &&
	    (goal_length = get_positive(*argv,"goal length must be positive", 0)) != 0) {
		--argc;
		++argv;
		if (argc > 0 && (max_length = get_positive(*argv,"max length must be positive", 0)) != 0) {
			--argc;
			++argv;
			if (max_length < goal_length)
				errx(EX_USAGE, "max length must be >= goal length");
		}
	}

	if (goal_length == 0)
		goal_length = 65;
	if (max_length == 0)
		max_length = goal_length+10;
	output_buffer = XMALLOC(max_length+1);	/* really needn't be longer */

	/* 2. Process files. */

	if (argc > 0) {
		while (argc-- > 0)
			process_named_file(*argv++);
	} else {
		process_stream(stdin, "standard input");
	}

	/* We're done. */
	return n_errors ? EX_NOINPUT : 0;

}