summaryrefslogtreecommitdiff
path: root/csslint/csslint.c
blob: 8ba591aaa9e2c0ae0075311553e068a655ff64fa (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */

/*
 * csslint.c : a small tester program for CSS2 input
 *
 * Copyright (C) 2002-2003 Gaël Chamoulaud <strider@freespiders.org>
 *                         Dodji Seketeli  <dodji@seketeli.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "libcroco.h"

#include <glib.h>
#include <string.h>

/**
 *The options data structure.
 *The variable of this data structure are set
 *during the  parsing the command line by the
 *parse_command_line() function.
 */
struct Options
{
	gboolean show_version ;
	gboolean use_cssom ;

	gchar ** files_list ;
};

void
csslint_parse_cmd_line (int a_argc, char **a_argv, 
			struct Options *a_options);

static void
csslint_show_version (const char *name);

static void
csslint_usage (const char *name);

static enum CRStatus
csslint_cssom_parser_parse (guchar * a_file_uri);

/**
 *Parses the command line.
 *@param a_argc the argc parameter of the main routine.
 *@param the argv parameter of the main routine.
 *@param a_options out parameter the parsed options.
 */
void
csslint_parse_cmd_line (int a_argc, char **a_argv,
			struct Options *a_options)
{
	int i= 0 ;

	g_return_if_fail (a_options) ;
	        
        if (a_argc <= 1) 
        {
                csslint_usage(a_argv[0]);
        }
		
	for (i = 1 ; i < a_argc ; i++)
	{
		if (a_argv[i][0] != '-') break ;
		
		if ((!strcmp (a_argv[i], "-version")) || 
                    (!strcmp (a_argv[i], "--version")))
		{
			a_options->show_version = TRUE ;
		}
		if ((!strcmp (a_argv[i], "-cssom")) || 
                    (!strcmp (a_argv[i], "--cssom")))
		{
			a_options->use_cssom = TRUE ;
		}
	}
	
	if (i >= a_argc)
	{
		a_options->files_list = NULL ;
	}
	else
	{
		a_options->files_list = &a_argv[i] ;
	}
}

/**
 *Displays the version text.
 *@param a_argc the argc variable passed to the main function.
 *@param a_argv the argv variable passed to the main function.
 */
static void 
csslint_show_version (const char *name) 
{
    fprintf(stderr, "%s: using libcroco version %s\n", name, LIBCROCO_VERSION);
    fprintf(stderr, "   compiled with: ");
#ifdef CROCO_HAVE_LIBXML2
	fprintf(stderr, "LIBXML2 ");
#endif
#ifdef CROCO_SELENG_ENABLED
	fprintf(stderr, "SELENG ");
#endif
#ifdef CROCO_TESTS_ENABLED
	fprintf(stderr, "TESTS ");
#endif
    fprintf(stderr, "\n");
}

/**
 *Displays the usage text.
 *@param a_argc the argc variable passed to the main function.
 *@param a_argv the argv variable passed to the main function.
 */
static void 
csslint_usage(const char *name) 
{
    printf("Usage : %s [options] CSS2files ...\n", name);
    printf("\tParse the CSS2 files and output the result of the parsing\n");

	/* Listing of different options */
    printf("\t--version : display the version of the CSS2 library used\n");
	printf("\t--cssom : parse a CSS file and builds a CSS object model\n");

    printf("\nLibcroco project home page: http://www.freespiders.org/projects/libcroco\n");
    printf("To report bugs or get some help check: http://bugzilla.gnome.org/\n");
}

/**
 *The test of the cr_input_read_byte() method.
 *Reads the each byte of a_file_uri using the
 *cr_input_read_byte() method. Each byte is send to
 *stdout.
 *@param a_file_uri the file to read.
 *@return CR_OK upon successfull completion of the
 *function, an error code otherwise.
 */
static enum CRStatus
csslint_cssom_parser_parse (guchar * a_file_uri)
{
	enum CRStatus status = CR_OK ;
	CROMParser *parser = NULL ;
	CRStyleSheet *stylesheet = NULL ;
	
	g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR) ;
		
	parser = cr_om_parser_new (NULL) ;
	status = cr_om_parser_parse_file (parser, 
                                          a_file_uri, CR_ASCII,
					  &stylesheet) ;
	if (status == CR_OK && stylesheet)
	{
		cr_stylesheet_dump (stylesheet, stdout) ;
		cr_stylesheet_destroy (stylesheet) ;
	}
	cr_om_parser_destroy (parser) ;

	return status ;
}

int 
main (int argc, char **argv) 
{
	struct Options options;
	enum CRStatus status = CR_OK;
	
        memset (&options, 0, sizeof (struct Options)) ;
        options.use_cssom = TRUE ;
	csslint_parse_cmd_line (argc, argv, &options);

	if (options.show_version == TRUE)
	{
		csslint_show_version(argv[0]);
		return 0;
	}

	if (options.use_cssom == TRUE)
	{
		if (options.files_list != NULL)
		{
			status = 
                                csslint_cssom_parser_parse 
                                (options.files_list[0]);
		} 
		else
		{
			csslint_show_version(argv[0]);
			return 0;
		}

		if (status == CR_OK)
		{
			g_print ("\nOK\n");
		}
		else
		{
			g_print ("\nKO\n");
		}
	}
    
	return 0;
}