krx51100.c
来自「answer of the c programming language sec」· C语言 代码 · 共 660 行 · 第 1/2 页
C
660 行
return longopts[longopt_match].val;
}
else
return optopt;
}
int getopt_long(int argc, char **argv, char *shortopts,
GETOPT_LONG_OPTION_T * longopts, int *longind)
{
return getopt_internal(argc, argv, shortopts, longopts, longind, 0);
}
void help(void)
{
puts( "OPTIONS" );
puts( "" );
puts( "-i, --initial When shrinking, make"
" initial spaces/tabs on a line tabs" );
puts( " and expand every other"
" tab on the line into spaces." );
puts( "-t=tablist, "
"Specify list of tab stops. "
"Default is every 8 characters." );
puts( "--tabs=tablist, "
"The parameter tablist is a list"
" of tab stops separated by" );
puts( "-tablist "
"commas; if no commas are present,"
" the program will put a" );
puts( " "
"tab stop every x places, "
"with x being the number in the" );
puts( " parameter." );
puts( "" );
puts( "--help Print usage message"
" and exit successfully." );
puts( "" );
puts( "--version Print version "
"information and exit successfully." );
}
void version(void)
{
puts( "detab - expand tabs into spaces" );
puts( "Version 1.0" );
puts( "Written by Gregory Pietsch" );
}
/* allocate memory, die on error */
void *xmalloc(size_t n)
{
void *p = malloc(n);
if (p == NULL) {
fprintf(stderr, "%s: out of memory\n", program_name);
exit(EXIT_FAILURE);
}
return p;
}
/* reallocate memory, die on error */
void *xrealloc(void *p, size_t n)
{
void *s;
if (n == 0) {
if (p != NULL)
free(p);
return NULL;
}
if (p == NULL)
return xmalloc(n);
s = realloc(p, n);
if (s == NULL) {
fprintf(stderr, "%s: out of memory\n", program_name);
exit(EXIT_FAILURE);
}
return s;
}
/* Determine the location of the first character in the string s1
* that is not a character in s2. The terminating null is not
* considered part of the string.
*/
char *xstrcpbrk(char *s1, char *s2)
{
char *sc1;
char *sc2;
for (sc1 = s1; *sc1 != '\0'; sc1++)
for (sc2 = s2;; sc2++)
if (*sc2 == '\0')
return sc1;
else if (*sc1 == *sc2)
break;
return NULL; /* terminating nulls match */
}
/* compare function for qsort() */
int ul_cmp(const void *a, const void *b)
{
unsigned long *ula = (unsigned long *) a;
unsigned long *ulb = (unsigned long *) b;
return (*ula < *ulb) ? -1 : (*ula > *ulb);
}
/* handle a tab stop list -- assumes param isn't NULL */
void handle_tab_stops(char *s)
{
char *p;
unsigned long ul;
size_t len = strlen(s);
if (xstrcpbrk(s, "0123456789,") != NULL) {
/* funny param */
fprintf(stderr, "%s: invalid parameter\n", program_name);
exit(EXIT_FAILURE);
}
if (strchr(s, ',') == NULL) {
tab_every = strtoul(s, NULL, 10);
if (tab_every == 0)
tab_every = 8;
}
else {
tab_stop_list = xrealloc(tab_stop_list,
(num_tab_stops_allocked += len) * (sizeof(unsigned
long)));
for (p = s; (p = strtok(p, ",")) != NULL; p = NULL) {
ul = strtoul(p, NULL, 10);
tab_stop_list[num_tab_stops++] = ul;
}
qsort(tab_stop_list, num_tab_stops, sizeof(unsigned long),
ul_cmp);
}
}
void parse_args(int argc, char **argv)
{
int opt;
do {
switch ((opt = getopt_long(argc, argv, shortopts, longopts,
NULL))) {
case 'i': /* initial */
flag_initial = 1;
break;
case 't': /* tab stops */
handle_tab_stops(optarg);
break;
case '?': /* invalid option */
fprintf(stderr,"For help, type:\n\t%s --help\n",
program_name);
exit(EXIT_FAILURE);
case 1:
case 0:
if (show_help || show_version) {
if (show_help)
help();
if (show_version)
version();
exit(EXIT_SUCCESS);
}
break;
default:
break;
}
} while (opt != EOF);
}
/* output exactly n spaces */
void output_spaces(size_t n)
{
int x = n; /* assume n is small */
printf("%*s", x, "");
}
/* get next highest tab stop */
unsigned long get_next_tab(unsigned long x)
{
size_t i;
if (tab_stop_list == NULL) {
/* use tab_every */
x += (tab_every - (x % tab_every));
return x;
}
else {
for (i = 0; i < num_tab_stops && tab_stop_list[i] <= x; i++);
return (i >= num_tab_stops) ? 0 : tab_stop_list[i];
}
}
/* the function that does the dirty work */
void tab(FILE * f)
{
unsigned long linelength = 0;
int c;
int in_initials = 1;
size_t num_spaces = 0;
unsigned long next_tab;
while ((c = getc(f)) != EOF) {
if (c != ' ' && c != '\t' && num_spaces > 0) {
/* output spaces and possible tabs */
if (flag_expand
|| (flag_initial && !in_initials)
|| num_spaces == 1) {
/* output spaces anyway */
output_spaces(num_spaces);
linelength += num_spaces;
num_spaces = 0;
}
else
while (num_spaces != 0) {
next_tab = get_next_tab(linelength);
if (next_tab > 0 && next_tab <= linelength +
num_spaces) {
/* output a tab */
putc('\t', stdout);
num_spaces -= (next_tab - linelength);
linelength = next_tab;
}
else {
/* output spaces */
output_spaces(num_spaces);
linelength += num_spaces;
num_spaces = 0;
}
}
}
switch (c) {
case ' ': /* space */
num_spaces++;
break;
case '\b': /* backspace */
/* preserve backspaces in output; decrement length for
tabbing
* purposes
*/
putc(c, stdout);
if (linelength > 0)
linelength--;
break;
case '\n': /* newline */
putc(c, stdout);
in_initials = 1;
linelength = 0;
break;
case '\t': /* tab */
next_tab = get_next_tab(linelength + num_spaces);
if (next_tab == 0) {
while ((next_tab = get_next_tab(linelength)) != 0) {
/* output tabs */
putc('\t', stdout);
num_spaces -= (next_tab - linelength);
linelength = next_tab;
}
/* output spaces */
output_spaces(num_spaces);
num_spaces = 0;
putc('\t', stdout);
linelength += num_spaces + 1;
}
else
num_spaces = next_tab - linelength;
break;
default:
putc(c, stdout);
in_initials = 0;
linelength++;
break;
}
}
}
int main(int argc, char **argv)
{
int i;
FILE *fp;
char *allocked_argvs = xmalloc(argc + 1);
char **new_argv = xmalloc((argc + 1) * sizeof(char *));
char *p;
program_name = argv[0];
memset(allocked_argvs, 0, argc + 1);
for (i = 0; i < argc; i++) {
p = argv[i];
if (isdigit(p[1])) {
new_argv[i] = xmalloc(strlen(p) + 2);
sprintf(new_argv[i], "-t%s", p + 1);
allocked_argvs[i] = 1;
}
else
new_argv[i] = p;
}
new_argv[argc] = NULL;
parse_args(argc, new_argv);
if (optind == argc)
tab(stdin);
else {
for (i = optind; i < argc; i++) {
if (strcmp(argv[i], "-") == 0)
fp = stdin;
else {
fp = fopen(argv[i], "r");
if (fp == NULL) {
fprintf(stderr, "%s: can't open %s\n",
argv[0], argv[i]);
abort();
}
}
tab(fp);
if (fp != stdin)
fclose(fp);
}
}
/* free everything we can */
for (i = 0; i < argc; i++)
if (allocked_argvs[i])
free(new_argv[i]);
free(allocked_argvs);
if (tab_stop_list != NULL)
free(tab_stop_list);
return EXIT_SUCCESS;
}
/* END OF FILE detab.c */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?