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