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 * %D <dir> inode number
25 * %i <ino> inode number
26 * %Is <inode> -> i_size
27 * %Ib <inode> -> i_blocks
28 * %Il <inode> -> i_links_count
29 * %Im <inode> -> i_mode
30 * %IM <inode> -> i_mtime
31 * %IF <inode> -> i_faddr
32 * %If <inode> -> i_file_acl
33 * %Id <inode> -> i_dir_acl
34 * %j <ino2> inode number
35 * %m <com_err error message>
37 * %p ext2fs_get_pathname of directory <ino>
38 * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as
39 * the containing directory. (If dirent is NULL
40 * then return the pathname of directory <ino2>)
41 * %q ext2fs_get_pathname of directory <dir>
42 * %Q ext2fs_get_pathname of directory <ino> with <dir> as
43 * the containing directory.
44 * %s <str> miscellaneous string
45 * %S backup superblock
47 * The following '@' expansions are supported:
51 * @C conflicts with some other fs block
57 * @E Entry '%Dn' in %p (%i)
59 * @F for @i %i (%Q) is
81 #define _INLINE_ __inline__
87 * This structure defines the abbreviations used by the text strings
88 * below. The first character in the string is the index letter. An
89 * abbreviation of the form '@<i>' is expanded by looking up the index
90 * letter <i> in the table below.
92 static const char *abbrevs[] = {
95 "Cconflicts with some other fs @b",
101 "E@e '%Dn' in %p (%i)",
103 "Ffor @i %i (%Q) is",
117 * Give more user friendly names to the "special" inodes.
119 #define num_special_inodes 7
120 static const char *special_inode_name[] =
122 "<The NULL inode>", /* 0 */
123 "<The bad blocks inode>", /* 1 */
125 "<The ACL index inode>", /* 3 */
126 "<The ACL data inode>", /* 4 */
127 "<The boot loader inode>", /* 5 */
128 "<The undelete directory inode>" /* 6 */
132 * This function prints a pathname, using the ext2fs_get_pathname
135 static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
140 if (!dir && (ino < num_special_inodes)) {
141 fputs(special_inode_name[ino], stdout);
145 retval = ext2fs_get_pathname(fs, dir, ino, &path);
147 fputs("???", stdout);
155 * This function handles the '@' expansion. We allow recursive
156 * expansion; an @ expression can contain further '@' and '%'
159 static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
160 struct problem_context *pctx,
163 const char **cpp, *str;
165 /* Search for the abbreviation */
166 for (cpp = abbrevs; *cpp; cpp++) {
172 if (*first && islower(*str)) {
174 fputc(toupper(*str++), stdout);
176 print_e2fsck_message(ctx, str, pctx, *first);
182 * This function expands '%kX' expressions
184 static _INLINE_ void expand_inode_expression(char ch,
185 struct problem_context *ctx)
187 struct ext2_inode *inode;
191 if (!ctx || !ctx->inode)
198 printf("%u", inode->i_size);
201 printf("%u", inode->i_blocks);
204 printf("%d", inode->i_links_count);
207 printf("0%o", inode->i_mode);
211 time_str = ctime(&t);
212 printf("%.24s", time_str);
215 printf("%u", inode->i_faddr);
218 printf("%u", inode->i_file_acl);
221 printf("%u", inode->i_dir_acl);
231 * This function expands '%dX' expressions
233 static _INLINE_ void expand_dirent_expression(char ch,
234 struct problem_context *ctx)
236 struct ext2_dir_entry *dirent;
239 if (!ctx || !ctx->dirent)
242 dirent = ctx->dirent;
246 printf("%u", dirent->inode);
249 len = dirent->name_len;
250 if (len > EXT2_NAME_LEN)
252 if (len > dirent->rec_len)
253 len = dirent->rec_len;
254 printf("%.*s", dirent->name_len, dirent->name);
257 printf("%u", dirent->rec_len);
260 printf("%u", dirent->name_len);
269 static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
270 struct problem_context *ctx)
280 printf("%u", ctx->blk);
283 printf("%d", ctx->blkcount);
286 printf("%u", ctx->blk2);
289 printf("%lu", ctx->dir);
292 printf("%d", ctx->group);
295 printf("%lu", ctx->ino);
298 printf("%lu", ctx->ino2);
301 printf("%s", error_message(ctx->errcode));
304 printf("%u", ctx->num);
307 print_pathname(fs, ctx->ino, 0);
310 print_pathname(fs, ctx->ino2,
311 ctx->dirent ? ctx->dirent->inode : 0);
314 print_pathname(fs, ctx->dir, 0);
317 print_pathname(fs, ctx->dir, ctx->ino);
320 printf("%d", get_backup_sb(fs));
323 printf("%s", ctx->str);
332 void print_e2fsck_message(e2fsck_t ctx, const char *msg,
333 struct problem_context *pctx, int first)
335 ext2_filsys fs = ctx->fs;
339 for (cp = msg; *cp; cp++) {
342 expand_at_expression(ctx, *cp, pctx, &first);
343 } else if (cp[0] == '%' && cp[1] == 'I') {
345 expand_inode_expression(*cp, pctx);
346 } else if (cp[0] == '%' && cp[1] == 'D') {
348 expand_dirent_expression(*cp, pctx);
349 } else if ((cp[0] == '%')) {
351 expand_percent_expression(fs, *cp, pctx);
353 for (i=0; cp[i]; i++)
354 if ((cp[i] == '@') || cp[i] == '%')
356 printf("%.*s", i, cp);