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