aboutsummaryrefslogtreecommitdiffhomepage
path: root/tokenize.c
blob: bad79a9fc33138599b28fef2d3a4b33446146c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/** file tokenize.c
  Small utility command for tokenizing an argument.
  \c tokenize is used for splitting a text string into separate parts (i.e. tokenizing) with a user supplied delimiter character. 
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "config.h"

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

/**
   Print help message
*/
void print_help();

/**
   Main program
*/
int main( int argc, char **argv )
{
	char *delim = " \t";
	int empty_ok = 0;
	int i;
	extern int optind;	
	
	while( 1 )
	{
#ifdef __GLIBC__
		static struct option
			long_options[] =
			{
				{
					"with-empty", no_argument, 0, 'e' 
				}
				,
				{
					"no-empty", no_argument, 0, 'n' 
				}
				,
				{
					"delimiter", required_argument, 0, 'd' 
				}
				,
				{
					"help", no_argument, 0, 'h' 
				}
				,
				{
					"version", no_argument, 0, 'v' 
				}
				,
				{ 
					0, 0, 0, 0 
				}
			}
		;		
		
		int opt_index = 0;
		
		int opt = getopt_long( argc,
							   argv, 
							   "end:hv", 
							   long_options, 
							   &opt_index );
#else
		int opt = getopt( argc,
						  argv, 
						  "end:hv" );
#endif
		if( opt == -1 )
			break;
			
		switch( opt )
		{
			case 0:
				break;
				
			case 'e':				
				empty_ok = 1;
				break;

			case 'n':				
				empty_ok = 0;
				break;

			case 'd':				
				delim = optarg;
				break;
			case 'h':
				print_help();
				exit(0);				
								
			case 'v':
				printf( "tokenize, version %s\n", PACKAGE_VERSION );
				exit( 0 );								

			case '?':
				return 1;
				
		}
		
	}		
	
	for( i=optind; i<argc; i++ )
	{
		char *curr;
		int printed=0;
		for( curr = argv[i]; *curr; curr++ )
		{
			if( strchr( delim, *curr )==0 )
			{
				printed = 1;
				putchar( *curr );
			}
			else
			{
				if( empty_ok || printed )
					putchar( '\n' );
				printed=0;
			}
		}
		if( printed )
			putchar( '\n' );
	}
	
}