blob: c46413167343e38be157c360b285c69e3da01231 [file] [log] [blame]
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001/*
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
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000018 * %c <blk2> block number
Theodore Ts'o21c84b71997-04-29 16:15:03 +000019 * %di <dirent>->ino inode number
20 * %dn <dirent>->name string
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000021 * %dr <dirent>->rec_len
22 * %dl <dirent>->name_len
Theodore Ts'o21c84b71997-04-29 16:15:03 +000023 * %D <dir> inode number
24 * %g <group> integer
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
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000035 * %m <com_err error message>
Theodore Ts'o21c84b71997-04-29 16:15:03 +000036 * %N <num>
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.
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000044 * %s <str> miscellaneous string
45 * %S backup superblock
Theodore Ts'o21c84b71997-04-29 16:15:03 +000046 *
47 * The following '@' expansions are supported:
48 *
Theodore Ts'of8188ff1997-11-14 05:23:04 +000049 * @A error allocating
Theodore Ts'o21c84b71997-04-29 16:15:03 +000050 * @b block
51 * @B bitmap
52 * @C conflicts with some other fs block
53 * @i inode
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +000054 * @I illegal
Theodore Ts'o21c84b71997-04-29 16:15:03 +000055 * @D deleted
56 * @d directory
57 * @e entry
58 * @E Entry '%Dn' in %p (%i)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000059 * @f filesystem
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060 * @F for @i %i (%Q) is
61 * @g group
62 * @l lost+found
63 * @L is a link
64 * @u unattached
65 * @r root inode
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 * @s should be
67 * @S superblock
Theodore Ts'o21c84b71997-04-29 16:15:03 +000068 * @z zero-length
69 */
70
71#include <stdlib.h>
72#include <unistd.h>
73#include <string.h>
74#include <ctype.h>
75#include <termios.h>
76
77#include "e2fsck.h"
78
79#include "problem.h"
80
81#ifdef __GNUC__
82#define _INLINE_ __inline__
83#else
84#define _INLINE_
85#endif
86
87/*
88 * This structure defines the abbreviations used by the text strings
89 * below. The first character in the string is the index letter. An
90 * abbreviation of the form '@<i>' is expanded by looking up the index
91 * letter <i> in the table below.
92 */
93static const char *abbrevs[] = {
Theodore Ts'of8188ff1997-11-14 05:23:04 +000094 "Aerror allocating",
Theodore Ts'o21c84b71997-04-29 16:15:03 +000095 "bblock",
96 "Bbitmap",
97 "Cconflicts with some other fs @b",
98 "iinode",
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +000099 "Iillegal",
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000100 "Ddeleted",
101 "ddirectory",
102 "eentry",
103 "E@e '%Dn' in %p (%i)",
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000104 "ffilesystem",
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000105 "Ffor @i %i (%Q) is",
106 "ggroup",
107 "llost+found",
108 "Lis a link",
109 "uunattached",
110 "rroot @i",
111 "sshould be",
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000112 "Ssuper@b",
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000113 "zzero-length",
114 "@@",
115 0
116 };
117
118/*
119 * Give more user friendly names to the "special" inodes.
120 */
121#define num_special_inodes 7
122static const char *special_inode_name[] =
123{
124 "<The NULL inode>", /* 0 */
125 "<The bad blocks inode>", /* 1 */
126 "/", /* 2 */
127 "<The ACL index inode>", /* 3 */
128 "<The ACL data inode>", /* 4 */
129 "<The boot loader inode>", /* 5 */
130 "<The undelete directory inode>" /* 6 */
131};
132
133/*
134 * This function prints a pathname, using the ext2fs_get_pathname
135 * function
136 */
137static void print_pathname(ext2_filsys fs, ino_t dir, ino_t ino)
138{
139 errcode_t retval;
140 char *path;
141
142 if (!dir && (ino < num_special_inodes)) {
143 fputs(special_inode_name[ino], stdout);
144 return;
145 }
146
147 retval = ext2fs_get_pathname(fs, dir, ino, &path);
148 if (retval)
149 fputs("???", stdout);
150 else {
151 fputs(path, stdout);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000152 ext2fs_free_mem((void **) &path);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000153 }
154}
155
156/*
157 * This function handles the '@' expansion. We allow recursive
158 * expansion; an @ expression can contain further '@' and '%'
159 * expressions.
160 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000161static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
162 struct problem_context *pctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000163 int *first)
164{
165 const char **cpp, *str;
166
167 /* Search for the abbreviation */
168 for (cpp = abbrevs; *cpp; cpp++) {
169 if (ch == *cpp[0])
170 break;
171 }
172 if (*cpp) {
173 str = (*cpp) + 1;
174 if (*first && islower(*str)) {
175 *first = 0;
176 fputc(toupper(*str++), stdout);
177 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000178 print_e2fsck_message(ctx, str, pctx, *first);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000179 } else
180 printf("@%c", ch);
181}
182
183/*
184 * This function expands '%kX' expressions
185 */
186static _INLINE_ void expand_inode_expression(char ch,
187 struct problem_context *ctx)
188{
189 struct ext2_inode *inode;
190 char * time_str;
191 time_t t;
192
193 if (!ctx || !ctx->inode)
194 goto no_inode;
195
196 inode = ctx->inode;
197
198 switch (ch) {
199 case 's':
200 printf("%u", inode->i_size);
201 break;
202 case 'b':
203 printf("%u", inode->i_blocks);
204 break;
205 case 'l':
206 printf("%d", inode->i_links_count);
207 break;
208 case 'm':
209 printf("0%o", inode->i_mode);
210 break;
211 case 'M':
212 t = inode->i_mtime;
213 time_str = ctime(&t);
214 printf("%.24s", time_str);
215 break;
216 case 'F':
217 printf("%u", inode->i_faddr);
218 break;
219 case 'f':
220 printf("%u", inode->i_file_acl);
221 break;
222 case 'd':
223 printf("%u", inode->i_dir_acl);
224 break;
225 default:
226 no_inode:
227 printf("%%I%c", ch);
228 break;
229 }
230}
231
232/*
233 * This function expands '%dX' expressions
234 */
235static _INLINE_ void expand_dirent_expression(char ch,
236 struct problem_context *ctx)
237{
238 struct ext2_dir_entry *dirent;
239 int len;
240
241 if (!ctx || !ctx->dirent)
242 goto no_dirent;
243
244 dirent = ctx->dirent;
245
246 switch (ch) {
247 case 'i':
248 printf("%u", dirent->inode);
249 break;
250 case 'n':
251 len = dirent->name_len;
252 if (len > EXT2_NAME_LEN)
253 len = EXT2_NAME_LEN;
254 if (len > dirent->rec_len)
255 len = dirent->rec_len;
256 printf("%.*s", dirent->name_len, dirent->name);
257 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000258 case 'r':
259 printf("%u", dirent->rec_len);
260 break;
261 case 'l':
262 printf("%u", dirent->name_len);
263 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000264 default:
265 no_dirent:
266 printf("%%D%c", ch);
267 break;
268 }
269}
270
271static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
272 struct problem_context *ctx)
273{
274 if (!ctx)
275 goto no_context;
276
277 switch (ch) {
278 case '%':
279 fputc('%', stdout);
280 break;
281 case 'b':
282 printf("%u", ctx->blk);
283 break;
284 case 'B':
285 printf("%d", ctx->blkcount);
286 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000287 case 'c':
288 printf("%u", ctx->blk2);
289 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000290 case 'd':
291 printf("%lu", ctx->dir);
292 break;
293 case 'g':
294 printf("%d", ctx->group);
295 break;
296 case 'i':
297 printf("%lu", ctx->ino);
298 break;
299 case 'j':
300 printf("%lu", ctx->ino2);
301 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000302 case 'm':
303 printf("%s", error_message(ctx->errcode));
304 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000305 case 'N':
306 printf("%u", ctx->num);
307 break;
308 case 'p':
309 print_pathname(fs, ctx->ino, 0);
310 break;
311 case 'P':
312 print_pathname(fs, ctx->ino2,
313 ctx->dirent ? ctx->dirent->inode : 0);
314 break;
315 case 'q':
316 print_pathname(fs, ctx->dir, 0);
317 break;
318 case 'Q':
319 print_pathname(fs, ctx->dir, ctx->ino);
320 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000321 case 'S':
322 printf("%d", get_backup_sb(fs));
323 break;
324 case 's':
325 printf("%s", ctx->str);
326 break;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000327 default:
328 no_context:
329 printf("%%%c", ch);
330 break;
331 }
332}
333
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000334void print_e2fsck_message(e2fsck_t ctx, const char *msg,
335 struct problem_context *pctx, int first)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000336{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000337 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000338 const char * cp;
339 int i;
340
341 for (cp = msg; *cp; cp++) {
342 if (cp[0] == '@') {
343 cp++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000344 expand_at_expression(ctx, *cp, pctx, &first);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000345 } else if (cp[0] == '%' && cp[1] == 'I') {
346 cp += 2;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000347 expand_inode_expression(*cp, pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000348 } else if (cp[0] == '%' && cp[1] == 'D') {
349 cp += 2;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000350 expand_dirent_expression(*cp, pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000351 } else if ((cp[0] == '%')) {
352 cp++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000353 expand_percent_expression(fs, *cp, pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000354 } else {
355 for (i=0; cp[i]; i++)
356 if ((cp[i] == '@') || cp[i] == '%')
357 break;
358 printf("%.*s", i, cp);
359 cp += i-1;
360 }
361 first = 0;
362 }
363}