summaryrefslogtreecommitdiffstats
path: root/tdeprint/foomatic/make_driver_db_foomatic.c
blob: 986a2444779e5be82320fe7a5a679b3f78500798 (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
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define BUFFER_SIZE	1024
#define WORD_SIZE	256

int parseOverview(const char *dbdir, FILE *out);

int main(int argc, char *argv[])
{
	FILE	*out;

	if (argc != 3)
	{
		fprintf(stderr,"usage: make_driver_db_foomatic <dbdirectory> <dbfilename>\n");
		exit(-1);
	}
	out = fopen(argv[2],"w");
	if (out == NULL)
	{
		fprintf(stderr,"Unable to open DB file: %s\n",argv[2]);
		exit(-1);
	}
	/* first parse RHS driver DB */
	if (!parseOverview(argv[1], out))
		fprintf(stderr,"Unable to parse printer DB\n");
	return 0;
}

char* skipSpaces(char *c)
{
	char 	*cc = c;
	while (cc && *cc && isspace(*cc)) cc++;
	return cc;
}

int nextEntity(char *s, FILE *in)
{
	char	c, p[WORD_SIZE] = {0};
	int	index, istag;

	index = 0;
	do
	{
		c = fgetc(in);
	} while (isspace(c) && c != EOF);
	if (c == EOF)
		return -1;
	if (c == '<')
	{
		istag = 1;
		c = fgetc(in);
		if (c != '/')
			ungetc(c, in);
	}
	else
	{
		istag = 0;
		p[index++] = c;
	}
	while (index < WORD_SIZE)
	{
		c = fgetc(in);
		if (c != '>' && c != '<')
			p[index++] = c;
		else
		{
			if (c == '<')
				ungetc(c, in);
			break;
		}
	}
    
    p[WORD_SIZE - 1] = 0;
	strcpy(s, p);
	return istag;
}

void discardTag(const char *tagname, FILE *in)
{
	char	entity[WORD_SIZE];
	int		istag;

	while ((istag=nextEntity(entity, in)) != -1)
	{
		if (istag == 1 && strcmp(tagname, entity) == 0)
			break;
	}
}

void clearDrivers(char **d)
{
	int	index = 0;

	while (d[index])
	{
		free(d[index]);
		d[index++] = 0;
	}
}

int parseOverview(const char *dbdir, FILE *out)
{
	FILE	*in;
	char	printerid[WORD_SIZE], manu[WORD_SIZE], model[WORD_SIZE], tag[WORD_SIZE], driver[WORD_SIZE];
	char	*drivers[20] = {0};
	int		index = 0;
	char	cmd[BUFFER_SIZE] = {0};

	snprintf(cmd, BUFFER_SIZE, "foomatic-combo-xml -O -l %s", dbdir);
	in = popen(cmd, "r");
	if (in == NULL)
		return 0;
	if (nextEntity(tag, in) == 1 && strcmp(tag, "overview") == 0)
	{
		printf("-1\n");
		while (nextEntity(tag, in) == 1)
		{
			if (strcmp(tag, "overview") == 0 || strcmp(tag, "printer") != 0)
				break;
			strcpy(printerid, "");
			strcpy(manu, "");
			strcpy(model, "");
			clearDrivers(drivers);
			while (nextEntity(tag, in) == 1)
			{
				if (strcmp(tag, "id") == 0) nextEntity(printerid, in);
				else if (strcmp(tag, "make") == 0) nextEntity(manu, in);
				else if (strcmp(tag, "model") == 0) nextEntity(model, in);
				else if (strcmp(tag, "drivers") == 0)
				{
					index = 0;
					while (nextEntity(tag, in) == 1 && strcmp(tag, "driver") == 0 && index < 20)
					{
						nextEntity(driver, in);
						drivers[index++] = strdup(driver);
						nextEntity(driver, in);
					}
					continue;
				}
				else if (strcmp(tag, "printer") == 0)
				{
					printf("%s %s\n", manu, model);
					index = 0;
					while (drivers[index])
					{
						fprintf(out, "FILE=%s|%s\n", printerid, drivers[index]);
						fprintf(out, "MANUFACTURER=%s\n", manu);
						fprintf(out, "MODELNAME=%s\n", model);
						fprintf(out, "MODEL=%s\n", model);
						fprintf(out, "DESCRIPTION=%s %s (%s)\n", manu, model, drivers[index]);
						fprintf(out, "\n");
						index++;
					}
					break;
				}
				else
				{
					discardTag(tag, in);
					continue;
				}
				nextEntity(tag, in);
			}
		}
	}
	if (pclose(in) == 0)
		index = 1;
	else
		index = 0;
	return index;
}