File: cdw_dvd_rw_format_regex.c

package info (click to toggle)
cdw 0.7.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,952 kB
  • sloc: ansic: 31,667; sh: 1,496; makefile: 274
file content (185 lines) | stat: -rw-r--r-- 5,164 bytes parent folder | download
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
/* cdw
 * Copyright (C) 2002 Varkonyi Balazs
 * Copyright (C) 2007 - 2012 Kamil Ignacak
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>


#include "cdw_processwin.h"
#include "cdw_regex_dispatch.h"
#include "cdw_dvd_rw_format_regex.h"
#include "cdw_thread.h" /* PIPE_BUFFER_SIZE */
#include "cdw_debug.h"



/* extern char stdout_pipe_buffer[PIPE_BUFFER_SIZE + 1]; */
extern char stderr_pipe_buffer[PIPE_BUFFER_SIZE + 1];

bool dvd_rw_format_pipe_regexp_performed;

static regex_t eregex1;
static regmatch_t *ematches1 = NULL;

/* there is one operation that needs to be done the first time
   a handler is called, and this is a flag for it */
static bool first_call_to_handler = true;

static void cdw_dvd_rw_format_handle_blanking(regex_t *eregex, regmatch_t *ematches);




/* regular expressions setup code */
void cdw_dvd_rw_format_stderr_regexp_prepare(void)
{
	dvd_rw_format_pipe_regexp_performed = false;
	first_call_to_handler = true;

	/* "blanking 85.1%" */
	/* "formatting 85.1%" */

	/* currently cdw utilizes dvd+rw-format only to blank media;
	   dvd+rw-format erases DVD-RW by normal blanking ("blanking 85.1%"
	   is displayed); DVD+RW is erased by formatting ("formatting 85.1%" is
	   displayed */

	/* don't put 'blanking' nor 'formatting' into regexp because
	   dvd+rw-format puts '^H' bytes between it and percent value */
	int rv = regcomp(&eregex1, "([0-9]+)([.]*)([0-9]*)%", REG_EXTENDED);
	ematches1 = (regmatch_t *) calloc(1, (eregex1.re_nsub + 1) * sizeof(regmatch_t));
	if (rv) {
		cdw_regex_regcomp_error_handler(__func__, rv, &eregex1, 501);
	}

	return;
}





void cdw_dvd_rw_format_stderr_regexp_execute(void)
{
	cdw_vdm ("INFO: stderr_pipe_buffer before cleaning = \"%s\"\n", stderr_pipe_buffer);

	/* remove funny characters from buffer, but don't operate on whole
	   string - only few first chars are interesting */
	int line_width = 80;
	for (int i = 0; i < line_width && i < PIPE_BUFFER_SIZE; i++) {
		if (!isprint(stderr_pipe_buffer[i])) {
			if (stderr_pipe_buffer[i] == '\0') {
				break;
			} else if (stderr_pipe_buffer[i] == '\r'
				   || stderr_pipe_buffer[i] == '\n'
				   || stderr_pipe_buffer[i] == '\b') {

				stderr_pipe_buffer[i] = '\0';
				break;
			} else {
				stderr_pipe_buffer[i] = ' ';
			}
		}
	}
	cdw_vdm ("INFO: stderr_pipe_buffer after cleaning = \"%s\"\n", stderr_pipe_buffer);

	int rv = regexec(&eregex1, stderr_pipe_buffer, eregex1.re_nsub + 1, ematches1, 0);
	if (rv == 0) {
		if (first_call_to_handler) {
			first_call_to_handler = false;
			/* this erases "preparing, please wait" message displayed by
			   dvd_rw_format_interface.c module */
			cdw_processwin_display_sub_info("");
		}
		cdw_sdm ("calling cdw_dvd_rw_format_handle_blanking() (stderr)\n");
		cdw_dvd_rw_format_handle_blanking(&eregex1, ematches1);
	}

	return;
}





void cdw_dvd_rw_format_stderr_regexp_destroy(void)
{
	regfree(&eregex1);
	free(ematches1);
	ematches1 = NULL;

	return;
}





void cdw_dvd_rw_format_handle_blanking(regex_t *regex, regmatch_t *matches)
{
	cdw_sdm ("called\n");
	dvd_rw_format_pipe_regexp_performed = true;
	/* regexp for catching blanking / formatting is
	     1      2      3
	  ([0-9]+)([.]*)([0-9]*)% */
	cdw_regex_assert_subex_number(regex->re_nsub, 3);

	int perc_decimal = 0, perc_fract = 0;

	for (unsigned int i = 1; i <= regex->re_nsub; i++) {
		if (i == 2) {
			continue;
		}
		char submatch[PIPE_BUFFER_SIZE];
		int len = cdw_regex_get_submatch(matches, i, stderr_pipe_buffer, submatch);
		if (len == -1) {
			cdw_vdm ("ERROR: len of subexpr %d is negative: %d\n", i, len);
			continue;
		}
		if (i == 1) { /* percentage - decimal part */
			if (!strcmp(submatch, "??")) {
				perc_decimal = 0;
			} else {
				perc_decimal = atoi((char *) &submatch);
			}
			cdw_vdm ("INFO: submatch %d, decimal part = \"%s\" -> %d\n", i, submatch, perc_decimal);
		} else if (i == 3) { /* percentage - fractional part */
			if (!strcmp(submatch, "??")) {
				perc_fract = 0;
			} else {
				perc_fract = atoi((char *) &submatch);
			}
			cdw_vdm ("INFO: submatch %d, fractional part = \"%s\" -> %d\n", i, submatch, perc_fract);
		} else {
			;
		}
	}

	/* float percent = perc_a + 0.1 * perc_b; */
	/* NOTE: consider implementing
	   conditional_processwin_display_progress_float(float done, long total, char *string) */

	/* display data */
	cdw_processwin_display_progress_conditional(perc_decimal, 100, (char *) NULL);

	return;
}