* check.t: add new regression test "typeset-padding-1" according to TFM

* edit.c: remove debug stuff again; next time better use shl.c functions ;)
* sh.h: add format attributes to a few shf functions
* histrap.c, var.c: fix format string mistakes
* main.c, sh.h: error_prefix and warningf take bool not int
* misc.c: make chvt() stuff use shf_* functions
* misc.c: rewrite the TIOCSTTY stuff to be better integrated in mksh,
  since it originally was an external patch
* misc.c: chvt() no longer fails if e.g. chown fails due to e.g. R/O / fs
* var.c: fix typeset padding for right-justified zero-filled
This commit is contained in:
tg 2006-11-10 01:13:52 +00:00
parent c2aec39358
commit 273ca89019
7 changed files with 81 additions and 100 deletions

17
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.71 2006/11/09 15:02:30 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.72 2006/11/10 01:13:50 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -3865,6 +3865,19 @@ expected-stdout-pattern:
expected-stderr-pattern:
/^X*$/
---
name: typeset-padding-1
description:
Check if left/right justification works as per TFM
stdin:
typeset -L10 ln=0hall0
typeset -R10 rn=0hall0
typeset -ZL10 lz=0hall0
typeset -ZR10 rz=0hall0
typeset -Z10 rx=" hallo "
print "<$ln> <$rn> <$lz> <$rz> <$rx>"
expected-stdout:
<0hall0 > < 0hall0> <hall0 > <00000hall0> <0000 hallo>
---
name: version-1
description:
Check version of shell.
@ -3872,5 +3885,5 @@ category: pdksh
stdin:
echo $KSH_VERSION
expected-stdout:
@(#)MIRBSD KSH R29 2006/11/09
@(#)MIRBSD KSH R29 2006/11/10
---

24
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.63 2006/11/09 22:38:31 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.64 2006/11/10 01:13:50 tg Exp $");
/* tty driver characters we are interested in */
typedef struct {
@ -56,28 +56,6 @@ static int x_file_glob(int, const char *, int, char ***);
static int x_command_glob(int, const char *, int, char ***);
static int x_locate_word(const char *, int, int, int *, int *);
#if 0
static void D(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
static void
D(const char *fmt, ...)
{
static FILE *_Dfp = NULL;
va_list ap;
if (_Dfp == NULL) {
if ((_Dfp = fopen("/tmp/mksh.dbg", "ab+")) == NULL)
abort();
fprintf(_Dfp, "\n\nOpening from %ld\n", (long)getpid());
}
va_start(ap, fmt);
vfprintf(_Dfp, fmt, ap);
fflush(_Dfp);
}
#else
#define D(x) /* nothing */
#endif
/* +++ generic editing functions +++ */
/* Called from main */

View File

@ -3,7 +3,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.26 2006/11/09 14:19:31 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.27 2006/11/10 01:13:51 tg Exp $");
#if !defined(__sun__)
#define DO_HISTORY
@ -198,7 +198,8 @@ c_fc(char **wp)
hist_source->line - (int) (histptr - hp));
/* print multi-line commands correctly */
for (s = *hp; (t = strchr(s, '\n')); s = t)
shf_fprintf(shl_stdout, "%.*s\t", ++t - s, s);
shf_fprintf(shl_stdout, "%.*s\t",
(int)(++t - s), s);
shf_fprintf(shl_stdout, "%s\n", s);
}
shf_flush(shl_stdout);

6
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.52 2006/11/09 22:08:07 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.53 2006/11/10 01:13:51 tg Exp $");
extern char **environ;
@ -758,7 +758,7 @@ errorf(const char *fmt, ...)
/* like errorf(), but no unwind is done */
void
warningf(int fileline, const char *fmt, ...)
warningf(bool fileline, const char *fmt, ...)
{
va_list va;
@ -820,7 +820,7 @@ internal_errorf(int jump, const char *fmt, ...)
/* used by error reporting functions to print "ksh: .kshrc[25]: " */
void
error_prefix(int fileline)
error_prefix(bool fileline)
{
/* Avoid foo: foo[2]: ... */
if (!fileline || !source || !source->file ||

96
misc.c
View File

@ -3,7 +3,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.22 2006/11/09 23:55:51 tg Exp $\t"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.23 2006/11/10 01:13:52 tg Exp $\t"
MKSH_SH_H_ID);
unsigned char chtypes[UCHAR_MAX + 1]; /* type bits for unsigned char */
@ -11,7 +11,9 @@ unsigned char chtypes[UCHAR_MAX + 1]; /* type bits for unsigned char */
static int do_gmatch(const unsigned char *, const unsigned char *,
const unsigned char *, const unsigned char *);
static const unsigned char *cclass(const unsigned char *, int);
static int parse_T(char *);
#ifdef TIOCSCTTY
static void parse_T(char *);
#endif
static char *do_phys_path(XString *, char *, const char *);
/*
@ -341,9 +343,13 @@ parse_args(char **argv,
case 'T':
if (what != OF_FIRSTTIME)
break;
if (parse_T(go.optarg))
return -1;
#ifndef TIOCSCTTY
bi_errorf("no TIOCSCTTY ioctl");
return -1;
#else
change_flag(FTALKING, OF_CMDLINE, 1);
parse_T(go.optarg);
#endif
break;
case '?':
@ -1376,80 +1382,56 @@ do_phys_path(XString *xsp, char *xp, const char *pathl)
return xp;
}
#if !defined(TIOCSCTTY)
#define NO_CHVT "no TIOCSCTTY ioctl"
#else
static const char *
chvt(char *f)
#ifdef TIOCSCTTY
static void
parse_T(char *fn)
{
char dv[20];
struct stat sb;
int fd;
if (chown(f, 0, 0))
return "chown";
if (chmod(f, 0600))
return "chmod";
if (stat(fn, &sb)) {
memcpy(dv, "/dev/ttyC", 9);
strlcpy(dv + 9, fn, 20 - 9);
if (stat(dv, &sb)) {
memmove(dv + 8, dv + 9, 20 - 9);
if (stat(dv, &sb))
bi_errorf("chvt: can't find tty %s", fn);
}
fn = dv;
}
if (!(sb.st_mode & S_IFCHR))
bi_errorf("chvt: not a char device: %s", fn);
if ((sb.st_uid != 0) && chown(fn, 0, 0))
warningf(false, "chvt: cannot chown root %s", fn);
if (((sb.st_mode & 07777) != 0600) && chmod(fn, 0600))
warningf(false, "chvt: cannot chmod 0600 %s", fn);
#if !defined(__sun__) && !defined(__gnu_linux__) && !defined(__INTERIX)
if (revoke(f))
return "revoke";
if (revoke(fn))
warningf(false, "chvt: cannot revoke %s", fn);
#endif
if ((fd = open(f, O_RDWR)) == -1) {
if ((fd = open(fn, O_RDWR)) == -1) {
sleep(1);
if ((fd = open(f, O_RDWR)) == -1)
return "open";
if ((fd = open(fn, O_RDWR)) == -1)
bi_errorf("chvt: cannot open %s", fn);
}
switch (fork()) {
case -1:
return "fork";
bi_errorf("fork failed");
case 0:
break;
default:
_exit(0);
}
if (setsid() == -1)
return "setsid";
bi_errorf("chvt: setsid failed");
if (ioctl(fd, TIOCSCTTY, NULL) == -1)
return "ioctl";
bi_errorf("chvt: TIOCSCTTY failed");
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
return NULL;
}
#endif
static int
parse_T(char *fn __attribute__((unused)))
{
#ifdef NO_CHVT
warningf(0, "chvt: %s", NO_CHVT);
return -1;
#else
const char *rv;
char dv[20];
struct stat sb;
strlcpy(dv, fn, 20);
if (stat(dv, &sb)) {
snprintf(dv, 20, "/dev/ttyC%s", fn);
if (stat(dv, &sb)) {
snprintf(dv, 20, "/dev/tty%s", fn);
if (stat(dv, &sb)) {
warningf(0, "chvt: can't find tty %s", fn);
return -1;
}
}
}
if (!(sb.st_mode & S_IFCHR)) {
warningf(0, "chvt: not a char device: %s", fn);
return -1;
}
if ((rv = chvt(dv)) != NULL) {
warningf(0, "chvt: failed to %s: %s", rv, strerror(errno));
return -1;
}
return 0;
#endif
}

21
sh.h
View File

@ -8,8 +8,8 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.67 2006/11/09 23:55:52 tg Exp $"
#define MKSH_VERSION "R29 2006/11/09"
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.68 2006/11/10 01:13:52 tg Exp $"
#define MKSH_VERSION "R29 2006/11/10"
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
@ -1182,13 +1182,13 @@ void cleanup_parents_env(void);
void cleanup_proc_env(void);
void errorf(const char *, ...)
__attribute__((__noreturn__, __format__ (printf, 1, 2)));
void warningf(int, const char *, ...)
void warningf(bool, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void bi_errorf(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
void internal_errorf(int, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void error_prefix(int);
void error_prefix(bool);
void shellf(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
void shprintf(const char *, ...)
@ -1261,10 +1261,15 @@ int shf_ungetc(int, struct shf *);
int shf_putchar(int, struct shf *);
int shf_puts(const char *, struct shf *);
int shf_write(const char *, int, struct shf *);
int shf_fprintf(struct shf *, const char *, ...);
int shf_snprintf(char *, int, const char *, ...);
char *shf_smprintf(const char *, ...);
int shf_vfprintf(struct shf *, const char *, va_list);
int shf_fprintf(struct shf *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
int shf_snprintf(char *, int, const char *, ...)
__attribute__((__format__ (printf, 3, 4)))
__attribute__((__bounded__ (__string__,1,2)));
char *shf_smprintf(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
int shf_vfprintf(struct shf *, const char *, va_list)
__attribute__((__format__ (printf, 2, 0)));
/* syn.c */
void initkeywords(void);
struct op *compile(Source *);

12
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.23 2006/08/18 18:48:26 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.24 2006/11/10 01:13:52 tg Exp $");
/*
* Variables
@ -510,10 +510,12 @@ formatstr(struct tbl *vp, const char *s)
s += slen - vp->u2.field;
slen = vp->u2.field;
}
shf_snprintf(p, nlen + 1,
((vp->flag & ZEROFIL) && digit(*s)) ?
"%0*s%.*s" : "%*s%.*s",
vp->u2.field - slen, null, slen, s);
if (vp->u2.field - slen)
memset(p, (vp->flag & ZEROFIL) ? '0' : ' ',
vp->u2.field - slen);
shf_snprintf(p + vp->u2.field - slen,
nlen + 1 - (vp->u2.field - slen),
"%.*s", slen, s);
} else {
/* strip leading spaces/zeros */
while (isspace((unsigned char)*s))