File: error.c

package info (click to toggle)
fuse-emulator 1.0.0.1a%2Bdfsg1-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,568 kB
  • sloc: ansic: 67,895; sh: 10,265; perl: 3,386; makefile: 787; yacc: 227; lex: 139
file content (184 lines) | stat: -rw-r--r-- 4,675 bytes parent folder | download | duplicates (2)
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
/* error.c: The error reporting widget
   Copyright (c) 2002-2005 Philip Kendall

   $Id: error.c 4103 2009-11-21 10:16:36Z fredm $

   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.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

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

#include "fuse.h"
#include "widget_internals.h"

widget_error_t *error_info;

int
ui_error_specific( ui_error_level severity, const char *message )
{
  widget_error_t error_info;
  /* Can't output widgets if we don't have a display yet */
  if( !display_ui_initialised ) return 0;


  error_info.severity = severity;
  error_info.message  = message;

  fuse_emulation_pause();
  widget_do( WIDGET_TYPE_ERROR, &error_info );
  fuse_emulation_unpause();

  return 0;
}

int widget_error_draw( void *data )
{
  char **lines; size_t count;
  size_t i;

  error_info = (widget_error_t*)data;
  if( split_message( error_info->message, &lines, &count, 28 ) ) return 1;
  
  widget_dialog_with_border( 1, 2, 30, count+2 );

  switch( error_info->severity ) {
  case UI_ERROR_INFO:
    widget_printstring( 10, 16, WIDGET_COLOUR_TITLE, "Info" );
    break;
  case UI_ERROR_WARNING:
    widget_printstring( 10, 16, WIDGET_COLOUR_TITLE, "Warning" );
    break;
  case UI_ERROR_ERROR:
    widget_printstring( 10, 16, WIDGET_COLOUR_TITLE, "Error" );
    break;
  default:
    widget_printstring( 10, 16, WIDGET_COLOUR_TITLE, "(Unknown message)" );
    break;
  }

  for( i=0; i<count; i++ ) {
    widget_printstring( 17, i*8+24, WIDGET_COLOUR_FOREGROUND, lines[i] );
    free( lines[i] );
  }

  free( lines );

  widget_display_lines( 2, count + 3 );

  return 0;
}

int
split_message( const char *message, char ***lines, size_t *count,
	       size_t line_length )
{
  const char *ptr = message;
  int position;

  line_length *= 8;

  /* Setup so we'll allocate the first line as soon as we get the first
     word */
  *lines = NULL; *count = 0; position = line_length;

  while( *ptr ) {

    /* Skip any whitespace */
    while( *ptr && isspace( *ptr ) ) ptr++; message = ptr;

    /* Find end of word */
    while( *ptr && !isspace( *ptr ) ) ptr++;

    /* message now points to a word of length (ptr-message); if
       that's longer than an entire line (most likely filenames), just
       take the last bit */
    while( widget_substringwidth( message, ptr - message ) >= line_length )
      message++;

    /* Check we've got room for the word, plus some prefixing space */
    if( position + widget_substringwidth( message, ptr - message ) + 4
 	>= line_length ) {

      char **new_lines; size_t i;

      /* If we've filled the screen, stop */
      if( *count == 18 ) return 0;

      new_lines = realloc( (*lines), (*count + 1) * sizeof( char** ) );
      if( new_lines == NULL ) {
	for( i=0; i<*count; i++ ) free( (*lines)[i] );
	if(*lines) free( (*lines) );
	return 1;
      }
      (*lines) = new_lines;

      (*lines)[*count] = malloc( (line_length+1) );
      if( (*lines)[*count] == NULL ) {
	for( i=0; i<*count; i++ ) free( (*lines)[i] );
	free( (*lines) );
	return 1;
      }
      
      strncpy( (*lines)[*count], message, ptr - message );
      position = widget_substringwidth( message, ptr - message );
      (*lines)[*count][ptr - message] = '\0';

      (*count)++;

    } else {		/* Enough room on this line */

      strcat( (*lines)[*count-1], " " );
      (*lines)[*count-1][strlen( (*lines)[*count-1] ) + ptr - message] = '\0';
      strncat( (*lines)[*count-1], message, ptr - message );
      position += widget_substringwidth( message, ptr - message ) + 4;

    }

    message = ptr;
    
  }

  return 0;
}

void
widget_error_keyhandler( input_key key )
{
  switch( key ) {

  case INPUT_KEY_Escape:
    widget_end_widget( WIDGET_FINISHED_CANCEL );
    return;

  case INPUT_KEY_Return:
  case INPUT_KEY_KP_Enter:
    widget_end_widget( WIDGET_FINISHED_OK );
    return;

  default:	/* Keep gcc happy */
    break;

  }
}