Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / e2fsck / message.c
1 /*
2  * message.c --- print e2fsck messages (with compression)
3  *
4  * Copyright 1996, 1997 by Theodore Ts'o
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  *
11  * print_e2fsck_message() prints a message to the user, using
12  * compression techniques and expansions of abbreviations.
13  *
14  * The following % expansions are supported:
15  *
16  *      %b      <blk>                   block number
17  *      %B      <blkcount>              integer
18  *      %c      <blk2>                  block number
19  *      %di     <dirent>->ino           inode number
20  *      %dn     <dirent>->name          string
21  *      %dr     <dirent>->rec_len
22  *      %dl     <dirent>->name_len
23  *      %dt     <dirent>->filetype
24  *      %D      <dir>                   inode number
25  *      %g      <group>                 integer
26  *      %i      <ino>                   inode number
27  *      %Is     <inode> -> i_size
28  *      %Ib     <inode> -> i_blocks
29  *      %Il     <inode> -> i_links_count
30  *      %Im     <inode> -> i_mode
31  *      %IM     <inode> -> i_mtime
32  *      %IF     <inode> -> i_faddr
33  *      %If     <inode> -> i_file_acl
34  *      %Id     <inode> -> i_dir_acl
35  *      %j      <ino2>                  inode number
36  *      %m      <com_err error message>
37  *      %N      <num>
38  *      %p      ext2fs_get_pathname of directory <ino>
39  *      %P      ext2fs_get_pathname of <dirent>->ino with <ino2> as
40  *                      the containing directory.  (If dirent is NULL
41  *                      then return the pathname of directory <ino2>)
42  *      %q      ext2fs_get_pathname of directory <dir>
43  *      %Q      ext2fs_get_pathname of directory <ino> with <dir> as
44  *                      the containing directory.
45  *      %s      <str>                   miscellaneous string
46  *      %S      backup superblock
47  *
48  * The following '@' expansions are supported:
49  *
50  *      @A      error allocating
51  *      @b      block
52  *      @B      bitmap
53  *      @C      conflicts with some other fs block
54  *      @i      inode
55  *      @I      illegal
56  *      @D      deleted
57  *      @d      directory
58  *      @e      entry
59  *      @E      Entry '%Dn' in %p (%i)
60  *      @f      filesystem
61  *      @F      for @i %i (%Q) is
62  *      @g      group
63  *      @l      lost+found
64  *      @L      is a link
65  *      @u      unattached
66  *      @r      root inode
67  *      @s      should be 
68  *      @S      superblock
69  *      @z      zero-length
70  */
71
72 #include <stdlib.h>
73 #include <unistd.h>
74 #include <string.h>
75 #include <ctype.h>
76 #include <termios.h>
77
78 #include "e2fsck.h"
79
80 #include "problem.h"
81
82 #ifdef __GNUC__
83 #define _INLINE_ __inline__
84 #else
85 #define _INLINE_
86 #endif
87
88 /*
89  * This structure defines the abbreviations used by the text strings
90  * below.  The first character in the string is the index letter.  An
91  * abbreviation of the form '@<i>' is expanded by looking up the index
92  * letter <i> in the table below.
93  */
94 static const char *abbrevs[] = {
95         "Aerror allocating",
96         "bblock",
97         "Bbitmap",
98         "Cconflicts with some other fs @b",
99         "iinode",
100         "Iillegal",
101         "Ddeleted",
102         "ddirectory",
103         "eentry",
104         "E@e '%Dn' in %p (%i)",
105         "ffilesystem",
106         "Ffor @i %i (%Q) is",
107         "ggroup",
108         "llost+found",
109         "Lis a link",
110         "uunattached",
111         "rroot @i",
112         "sshould be",
113         "Ssuper@b",
114         "zzero-length",
115         "@@",
116         0
117         };
118
119 /*
120  * Give more user friendly names to the "special" inodes.
121  */
122 #define num_special_inodes      7
123 static const char *special_inode_name[] =
124 {
125         "<The NULL inode>",                     /* 0 */
126         "<The bad blocks inode>",               /* 1 */
127         "/",                                    /* 2 */
128         "<The ACL index inode>",                /* 3 */
129         "<The ACL data inode>",                 /* 4 */
130         "<The boot loader inode>",              /* 5 */
131         "<The undelete directory inode>"        /* 6 */
132 };
133
134 /*
135  * This function does "safe" printing.  It will convert non-printable
136  * ASCII characters using '^' and M- notation.
137  */
138 static void safe_print(const unsigned char *cp, int len)
139 {
140         unsigned char   ch;
141
142         if (len < 0)
143                 len = strlen(cp);
144
145         while (len--) {
146                 ch = *cp++;
147                 if (ch > 128) {
148                         fputs("M-", stdout);
149                         ch -= 128;
150                 }
151                 if (ch < 32) {
152                         fputc('^', stdout);
153                         ch += 32;
154                 }
155                 fputc(ch, stdout);
156         }
157 }
158
159
160 /*
161  * This function prints a pathname, using the ext2fs_get_pathname
162  * function
163  */
164 static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
165 {
166         errcode_t       retval;
167         char            *path;
168
169         if (!dir && (ino < num_special_inodes)) {
170                 fputs(special_inode_name[ino], stdout);
171                 return;
172         }
173         
174         retval = ext2fs_get_pathname(fs, dir, ino, &path);
175         if (retval)
176                 fputs("???", stdout);
177         else {
178                 safe_print(path, -1);
179                 ext2fs_free_mem((void **) &path);
180         }
181 }
182
183 /*
184  * This function handles the '@' expansion.  We allow recursive
185  * expansion; an @ expression can contain further '@' and '%'
186  * expressions. 
187  */
188 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
189                                           struct problem_context *pctx,
190                                           int *first)
191 {
192         const char **cpp, *str;
193         
194         /* Search for the abbreviation */
195         for (cpp = abbrevs; *cpp; cpp++) {
196                 if (ch == *cpp[0])
197                         break;
198         }
199         if (*cpp) {
200                 str = (*cpp) + 1;
201                 if (*first && islower(*str)) {
202                         *first = 0;
203                         fputc(toupper(*str++), stdout);
204                 }
205                 print_e2fsck_message(ctx, str, pctx, *first);
206         } else
207                 printf("@%c", ch);
208 }
209
210 /*
211  * This function expands '%kX' expressions
212  */
213 static _INLINE_ void expand_inode_expression(char ch,
214                                                struct problem_context *ctx)
215 {
216         struct ext2_inode       *inode;
217         char *                  time_str;
218         time_t                  t;
219
220         if (!ctx || !ctx->inode)
221                 goto no_inode;
222         
223         inode = ctx->inode;
224         
225         switch (ch) {
226         case 's':
227                 if (LINUX_S_ISDIR(inode->i_mode))
228                         printf("%u", inode->i_size);
229                 else {
230 #ifdef EXT2_NO_64_TYPE
231                         if (inode->i_size_high)
232                                 printf("0x%x%08x", inode->i_size_high,
233                                        inode->i_size);
234                         else
235                                 printf("%u", inode->i_size);
236 #else
237                         printf("%llu", (inode->i_size | 
238                                         ((__u64) inode->i_size_high << 32)));
239 #endif
240                 }
241                 break;
242         case 'b':
243                 printf("%u", inode->i_blocks);
244                 break;
245         case 'l':
246                 printf("%d", inode->i_links_count);
247                 break;
248         case 'm':
249                 printf("0%o", inode->i_mode);
250                 break;
251         case 'M':
252                 t = inode->i_mtime;
253                 time_str = ctime(&t);
254                 printf("%.24s", time_str);
255                 break;
256         case 'F':
257                 printf("%u", inode->i_faddr);
258                 break;
259         case 'f':
260                 printf("%u", inode->i_file_acl);
261                 break;
262         case 'd':
263                 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
264                               inode->i_dir_acl : 0));
265                 break;
266         default:
267         no_inode:
268                 printf("%%I%c", ch);
269                 break;
270         }
271 }
272
273 /*
274  * This function expands '%dX' expressions
275  */
276 static _INLINE_ void expand_dirent_expression(char ch,
277                                               struct problem_context *ctx)
278 {
279         struct ext2_dir_entry   *dirent;
280         int     len;
281         
282         if (!ctx || !ctx->dirent)
283                 goto no_dirent;
284         
285         dirent = ctx->dirent;
286         
287         switch (ch) {
288         case 'i':
289                 printf("%u", dirent->inode);
290                 break;
291         case 'n':
292                 len = dirent->name_len & 0xFF;
293                 if (len > EXT2_NAME_LEN)
294                         len = EXT2_NAME_LEN;
295                 if (len > dirent->rec_len)
296                         len = dirent->rec_len;
297                 safe_print(dirent->name, len);
298                 break;
299         case 'r':
300                 printf("%u", dirent->rec_len);
301                 break;
302         case 'l':
303                 printf("%u", dirent->name_len & 0xFF);
304                 break;
305         case 't':
306                 printf("%u", dirent->name_len >> 8);
307                 break;
308         default:
309         no_dirent:
310                 printf("%%D%c", ch);
311                 break;
312         }
313 }
314
315 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
316                                                struct problem_context *ctx)
317 {
318         if (!ctx)
319                 goto no_context;
320         
321         switch (ch) {
322         case '%':
323                 fputc('%', stdout);
324                 break;
325         case 'b':
326                 printf("%u", ctx->blk);
327                 break;
328         case 'B':
329 #ifdef EXT2_NO_64_TYPE
330                 printf("%d", ctx->blkcount);
331 #else
332                 printf("%lld", ctx->blkcount);
333 #endif
334                 break;
335         case 'c':
336                 printf("%u", ctx->blk2);
337                 break;
338         case 'd':
339                 printf("%lu", ctx->dir);
340                 break;
341         case 'g':
342                 printf("%d", ctx->group);
343                 break;
344         case 'i':
345                 printf("%lu", ctx->ino);
346                 break;
347         case 'j':
348                 printf("%lu", ctx->ino2);
349                 break;
350         case 'm':
351                 printf("%s", error_message(ctx->errcode));
352                 break;
353         case 'N':
354 #ifdef EXT2_NO_64_TYPE
355                 printf("%u", ctx->num);
356 #else
357                 printf("%llu", ctx->num);
358 #endif
359                 break;
360         case 'p':
361                 print_pathname(fs, ctx->ino, 0);
362                 break;
363         case 'P':
364                 print_pathname(fs, ctx->ino2,
365                                ctx->dirent ? ctx->dirent->inode : 0);
366                 break;
367         case 'q':
368                 print_pathname(fs, ctx->dir, 0);
369                 break;
370         case 'Q':
371                 print_pathname(fs, ctx->dir, ctx->ino);
372                 break;
373         case 'S':
374                 printf("%d", get_backup_sb(fs));
375                 break;
376         case 's':
377                 printf("%s", ctx->str);
378                 break;
379         default:
380         no_context:
381                 printf("%%%c", ch);
382                 break;
383         }
384 }       
385
386 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
387                           struct problem_context *pctx, int first)
388 {
389         ext2_filsys fs = ctx->fs;
390         const char *    cp;
391         int             i;
392
393         e2fsck_clear_progbar(ctx);
394         for (cp = msg; *cp; cp++) {
395                 if (cp[0] == '@') {
396                         cp++;
397                         expand_at_expression(ctx, *cp, pctx, &first);
398                 } else if (cp[0] == '%' && cp[1] == 'I') {
399                         cp += 2;
400                         expand_inode_expression(*cp, pctx);
401                 } else if (cp[0] == '%' && cp[1] == 'D') {
402                         cp += 2;
403                         expand_dirent_expression(*cp, pctx);
404                 } else if ((cp[0] == '%')) {
405                         cp++;
406                         expand_percent_expression(fs, *cp, pctx);
407                 } else {
408                         for (i=0; cp[i]; i++)
409                                 if ((cp[i] == '@') || cp[i] == '%')
410                                         break;
411                         printf("%.*s", i, cp);
412                         cp += i-1;
413                 }
414                 first = 0;
415         }
416 }