2 * message.c --- print e2fsck messages (with compression)
4 * Copyright 1996, 1997 by Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
11 * print_e2fsck_message() prints a message to the user, using
12 * compression techniques and expansions of abbreviations.
14 * The following % expansions are supported:
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
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>
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
48 * The following '@' expansions are supported:
54 * @C conflicts with some other fs block
60 * @E Entry '%Dn' in %p (%i)
62 * @F for @i %i (%Q) is
84 #define _INLINE_ __inline__
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.
95 static const char *abbrevs[] = {
96 N_("Aerror allocating"),
100 N_("Cconflicts with some other fs @b"),
106 N_("E@e '%Dn' in %p (%i)"),
108 N_("Ffor @i %i (%Q) is"),
122 * Give more user friendly names to the "special" inodes.
124 #define num_special_inodes 7
125 static const char *special_inode_name[] =
127 N_("<The NULL inode>"), /* 0 */
128 N_("<The bad blocks inode>"), /* 1 */
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 */
137 * This function does "safe" printing. It will convert non-printable
138 * ASCII characters using '^' and M- notation.
140 static void safe_print(const char *cp, int len)
153 if ((ch < 32) || (ch == 0x7f)) {
155 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
163 * This function prints a pathname, using the ext2fs_get_pathname
166 static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
171 if (!dir && (ino < num_special_inodes)) {
172 fputs(_(special_inode_name[ino]), stdout);
176 retval = ext2fs_get_pathname(fs, dir, ino, &path);
178 fputs("???", stdout);
180 safe_print(path, -1);
181 ext2fs_free_mem((void **) &path);
186 * This function handles the '@' expansion. We allow recursive
187 * expansion; an @ expression can contain further '@' and '%'
190 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
191 struct problem_context *pctx,
194 const char **cpp, *str;
196 /* Search for the abbreviation */
197 for (cpp = abbrevs; *cpp; cpp++) {
203 if (*first && islower(*str)) {
205 fputc(toupper(*str++), stdout);
207 print_e2fsck_message(ctx, _(str), pctx, *first);
213 * This function expands '%kX' expressions
215 static _INLINE_ void expand_inode_expression(char ch,
216 struct problem_context *ctx)
218 struct ext2_inode *inode;
222 if (!ctx || !ctx->inode)
229 if (LINUX_S_ISDIR(inode->i_mode))
230 printf("%u", inode->i_size);
232 #ifdef EXT2_NO_64_TYPE
233 if (inode->i_size_high)
234 printf("0x%x%08x", inode->i_size_high,
237 printf("%u", inode->i_size);
239 printf("%llu", (inode->i_size |
240 ((__u64) inode->i_size_high << 32)));
245 printf("%u", inode->i_blocks);
248 printf("%d", inode->i_links_count);
251 printf("0%o", inode->i_mode);
255 time_str = ctime(&t);
256 printf("%.24s", time_str);
259 printf("%u", inode->i_faddr);
262 printf("%u", inode->i_file_acl);
265 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
266 inode->i_dir_acl : 0));
276 * This function expands '%dX' expressions
278 static _INLINE_ void expand_dirent_expression(char ch,
279 struct problem_context *ctx)
281 struct ext2_dir_entry *dirent;
284 if (!ctx || !ctx->dirent)
287 dirent = ctx->dirent;
291 printf("%u", dirent->inode);
294 len = dirent->name_len & 0xFF;
295 if (len > EXT2_NAME_LEN)
297 if (len > dirent->rec_len)
298 len = dirent->rec_len;
299 safe_print(dirent->name, len);
302 printf("%u", dirent->rec_len);
305 printf("%u", dirent->name_len & 0xFF);
308 printf("%u", dirent->name_len >> 8);
317 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
318 struct problem_context *ctx)
328 printf("%u", ctx->blk);
331 #ifdef EXT2_NO_64_TYPE
332 printf("%d", ctx->blkcount);
334 printf("%lld", ctx->blkcount);
338 printf("%u", ctx->blk2);
341 printf("%lu", ctx->dir);
344 printf("%d", ctx->group);
347 printf("%lu", ctx->ino);
350 printf("%lu", ctx->ino2);
353 printf("%s", error_message(ctx->errcode));
356 #ifdef EXT2_NO_64_TYPE
357 printf("%u", ctx->num);
359 printf("%llu", ctx->num);
363 print_pathname(fs, ctx->ino, 0);
366 print_pathname(fs, ctx->ino2,
367 ctx->dirent ? ctx->dirent->inode : 0);
370 print_pathname(fs, ctx->dir, 0);
373 print_pathname(fs, ctx->dir, ctx->ino);
376 printf("%d", get_backup_sb(fs));
379 printf("%s", ctx->str);
388 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
389 struct problem_context *pctx, int first)
391 ext2_filsys fs = ctx->fs;
395 e2fsck_clear_progbar(ctx);
396 for (cp = msg; *cp; cp++) {
399 expand_at_expression(ctx, *cp, pctx, &first);
400 } else if (cp[0] == '%' && cp[1] == 'I') {
402 expand_inode_expression(*cp, pctx);
403 } else if (cp[0] == '%' && cp[1] == 'D') {
405 expand_dirent_expression(*cp, pctx);
406 } else if ((cp[0] == '%')) {
408 expand_percent_expression(fs, *cp, pctx);
410 for (i=0; cp[i]; i++)
411 if ((cp[i] == '@') || cp[i] == '%')
413 printf("%.*s", i, cp);