From b79275a66f2d6be11aeeb79ac08cb3c2487e0e16 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Sun, 13 Apr 2003 10:09:57 +0000 Subject: [PATCH] Forgot to sync the CVS. Check in the code again. --- g10/ChangeLog | 15 +++++ g10/g10.c | 3 + g10/keylist.c | 1 + g10/mainproc.c | 154 +++++++++++++++++++++++++++++-------------------- g10/packet.h | 2 + 5 files changed, 113 insertions(+), 62 deletions(-) diff --git a/g10/ChangeLog b/g10/ChangeLog index 5a6b97a71..4f54e6f67 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,18 @@ +2003-04-13 Timo Schulz + + * mainproc.c (mainproc_open_output): New. Support for + --output when GPG is in the list mode. + (mainproc_get_output): New. + + * keylist.c (print_fingerprint): If the mode is zero and + we are in the colon mode, we try to get the file handle + via mainproc_get_output(). + + * g10.c (main): Use the new code to support --output in + the list mode. + + * main.h: Add prototypes. + 2003-04-10 Werner Koch * passphrase.c (read_passphrase_from_fd): Do a dummy read if the diff --git a/g10/g10.c b/g10/g10.c index 25db4d407..7cb5f3a04 100644 --- a/g10/g10.c +++ b/g10/g10.c @@ -2545,10 +2545,13 @@ main( int argc, char **argv ) set_packet_list_mode(1); opt.list_packets=1; } + if( opt.outfile ) + mainproc_open_output( opt.outfile ); rc = proc_packets(NULL, a ); if( rc ) log_error("processing message failed: %s\n", g10_errstr(rc) ); iobuf_close(a); + mainproc_open_output( NULL ); } break; } diff --git a/g10/keylist.c b/g10/keylist.c index 49c7afdb5..141a66219 100644 --- a/g10/keylist.c +++ b/g10/keylist.c @@ -1050,6 +1050,7 @@ print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode ) fingerprint_from_pk (pk, array, &n); p = array; if (opt.with_colons && !mode) { + fp = mainproc_get_output( ); fprintf (fp, "fpr:::::::::"); for (i=0; i < n ; i++, p++ ) fprintf (fp, "%02X", *p ); diff --git a/g10/mainproc.c b/g10/mainproc.c index ac3f68e58..d0a3c087a 100644 --- a/g10/mainproc.c +++ b/g10/mainproc.c @@ -79,12 +79,38 @@ struct mainproc_context { }; +static FILE *list_fp = NULL; + static int do_proc_packets( CTX c, IOBUF a ); static void list_node( CTX c, KBNODE node ); static void proc_tree( CTX c, KBNODE node ); +void +mainproc_open_output( const char * file ) +{ + if( !file ) { + if( list_fp && list_fp != stderr && list_fp != stdout ) { + fclose( list_fp ); + list_fp = stdout; + } + return; + } + if( *file != '-' ) + list_fp = fopen( file, "w" ); + if( !list_fp ) + list_fp = stdout; +} + + +FILE* +mainproc_get_output( void ) +{ + return list_fp? list_fp : stdout; +} + + static void release_list( CTX c ) { @@ -733,27 +759,28 @@ do_check_sig( CTX c, KBNODE node, int *is_selfsig, int *is_expkey ) static void -print_userid( PACKET *pkt ) +print_userid (PACKET *pkt) { - if( !pkt ) - BUG(); - if( pkt->pkttype != PKT_USER_ID ) { - printf("ERROR: unexpected packet type %d", pkt->pkttype ); - return; + if (!pkt) + BUG(); + if (pkt->pkttype != PKT_USER_ID) + { + printf("ERROR: unexpected packet type %d", pkt->pkttype ); + return; } - if( opt.with_colons ) - { - if(pkt->pkt.user_id->attrib_data) - printf("%u %lu", - pkt->pkt.user_id->numattribs, - pkt->pkt.user_id->attrib_len); - else - print_string( stdout, pkt->pkt.user_id->name, - pkt->pkt.user_id->len, ':'); - } - else - print_utf8_string( stdout, pkt->pkt.user_id->name, - pkt->pkt.user_id->len ); + if (opt.with_colons) + { + if (pkt->pkt.user_id->attrib_data) + fprintf (list_fp, "%u %lu", + pkt->pkt.user_id->numattribs, + pkt->pkt.user_id->attrib_len); + else + print_string (list_fp, pkt->pkt.user_id->name, + pkt->pkt.user_id->len, ':'); + } + else + print_utf8_string (list_fp, pkt->pkt.user_id->name, + pkt->pkt.user_id->len); } @@ -827,31 +854,31 @@ list_node( CTX c, KBNODE node ) c->trustletter = opt.fast_list_mode? 0 : get_validity_info( pk, NULL ); } - printf("%s:", mainkey? "pub":"sub" ); + fprintf( list_fp, "%s:", mainkey? "pub":"sub" ); if( c->trustletter ) - putchar( c->trustletter ); - printf(":%u:%d:%08lX%08lX:%s:%s:", + fputc( c->trustletter, list_fp ); + fprintf( list_fp, ":%u:%d:%08lX%08lX:%s:%s:", nbits_from_pk( pk ), pk->pubkey_algo, (ulong)keyid[0],(ulong)keyid[1], colon_datestr_from_pk( pk ), colon_strtime (pk->expiredate) ); if( c->local_id ) - printf("%lu", c->local_id ); - putchar(':'); + fprintf( list_fp, "%lu", c->local_id ); + fputc( ':', list_fp ); if( mainkey && !opt.fast_list_mode ) - putchar( get_ownertrust_info (pk) ); - putchar(':'); + fputc( get_ownertrust_info (pk), list_fp ); + fputc( ':', list_fp ); if( node->next && node->next->pkt->pkttype == PKT_RING_TRUST) { - putchar('\n'); any=1; + fputc('\n', list_fp ); any=1; if( opt.fingerprint ) print_fingerprint( pk, NULL, 0 ); - printf("rtv:1:%u:\n", + fprintf( list_fp, "rtv:1:%u:\n", node->next->pkt->pkt.ring_trust->trustval ); } } else - printf("%s %4u%c/%08lX %s ", + fprintf( list_fp, "%s %4u%c/%08lX %s ", mainkey? "pub":"sub", nbits_from_pk( pk ), pubkey_letter( pk->pubkey_algo ), @@ -864,9 +891,9 @@ list_node( CTX c, KBNODE node ) if( node->pkt->pkttype == PKT_SIGNATURE ) { if( !any ) { if( node->pkt->pkt.signature->sig_class == 0x20 ) - puts("[revoked]"); + fputs("[revoked]", list_fp ); else - putchar('\n'); + fputc('\n', list_fp ); any = 1; } list_node(c, node ); @@ -874,27 +901,27 @@ list_node( CTX c, KBNODE node ) else if( node->pkt->pkttype == PKT_USER_ID ) { if( any ) { if( opt.with_colons ) - printf("%s:::::::::", + fprintf( list_fp, "%s:::::::::", node->pkt->pkt.user_id->attrib_data?"uat":"uid"); else - printf( "uid%*s", 28, "" ); + fprintf( list_fp, "uid%*s", 28, "" ); } print_userid( node->pkt ); if( opt.with_colons ) - putchar(':'); - putchar('\n'); + fputc( ':', list_fp ); + fputc( '\n', list_fp ); if( opt.fingerprint && !any ) print_fingerprint( pk, NULL, 0 ); if( node->next && node->next->pkt->pkttype == PKT_RING_TRUST ) { - printf("rtv:2:%u:\n", + fprintf( list_fp, "rtv:2:%u:\n", node->next->pkt->pkt.ring_trust->trustval ); } any=1; } else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) { if( !any ) { - putchar('\n'); + fputc('\n', list_fp ); any = 1; } list_node(c, node ); @@ -902,11 +929,11 @@ list_node( CTX c, KBNODE node ) } } else if( pk->expiredate ) { /* of subkey */ - printf(_(" [expires: %s]"), expirestr_from_pk( pk ) ); + fprintf( list_fp, _(" [expires: %s]"), expirestr_from_pk( pk ) ); } if( !any ) - putchar('\n'); + fputc('\n', list_fp ); if( !mainkey && opt.fingerprint > 1 ) print_fingerprint( pk, NULL, 0 ); } @@ -917,7 +944,7 @@ list_node( CTX c, KBNODE node ) if( opt.with_colons ) { u32 keyid[2]; keyid_from_sk( sk, keyid ); - printf("%s::%u:%d:%08lX%08lX:%s:%s:::", + fprintf( list_fp, "%s::%u:%d:%08lX%08lX:%s:%s:::", mainkey? "sec":"ssb", nbits_from_sk( sk ), sk->pubkey_algo, @@ -927,7 +954,7 @@ list_node( CTX c, KBNODE node ) /* fixme: add LID */ ); } else - printf("%s %4u%c/%08lX %s ", + fprintf( list_fp, "%s %4u%c/%08lX %s ", mainkey? "sec":"ssb", nbits_from_sk( sk ), pubkey_letter( sk->pubkey_algo ), @@ -939,9 +966,9 @@ list_node( CTX c, KBNODE node ) if( node->pkt->pkttype == PKT_SIGNATURE ) { if( !any ) { if( node->pkt->pkt.signature->sig_class == 0x20 ) - puts("[revoked]"); + fputs("[revoked]", list_fp); else - putchar('\n'); + fputc('\n', list_fp); any = 1; } list_node(c, node ); @@ -949,22 +976,22 @@ list_node( CTX c, KBNODE node ) else if( node->pkt->pkttype == PKT_USER_ID ) { if( any ) { if( opt.with_colons ) - printf("%s:::::::::", + fprintf( list_fp, "%s:::::::::", node->pkt->pkt.user_id->attrib_data?"uat":"uid"); else - printf( "uid%*s", 28, "" ); + fprintf( list_fp, "uid%*s", 28, "" ); } print_userid( node->pkt ); if( opt.with_colons ) - putchar(':'); - putchar('\n'); + fputc(':', list_fp); + fputc('\n', list_fp); if( opt.fingerprint && !any ) print_fingerprint( NULL, sk, 0 ); any=1; } else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) { if( !any ) { - putchar('\n'); + fputc('\n', list_fp); any = 1; } list_node(c, node ); @@ -988,11 +1015,11 @@ list_node( CTX c, KBNODE node ) return; if( sig->sig_class == 0x20 || sig->sig_class == 0x30 ) - fputs("rev", stdout); + fputs("rev", list_fp); else - fputs("sig", stdout); + fputs("sig", list_fp); if( opt.check_sigs ) { - fflush(stdout); + fflush(list_fp); switch( (rc2=do_check_sig( c, node, &is_selfsig, NULL )) ) { case 0: sigrc = '!'; break; case G10ERR_BAD_SIGN: sigrc = '-'; break; @@ -1016,35 +1043,36 @@ list_node( CTX c, KBNODE node ) } } if( opt.with_colons ) { - putchar(':'); + fputc(':', list_fp); if( sigrc != ' ' ) - putchar(sigrc); - printf("::%d:%08lX%08lX:%s::::", sig->pubkey_algo, + fputc(sigrc, list_fp); + fprintf( list_fp, "::%d:%08lX%08lX:%s::::", sig->pubkey_algo, (ulong)sig->keyid[0], (ulong)sig->keyid[1], colon_datestr_from_sig(sig)); } else - printf("%c %08lX %s ", + fprintf( list_fp, "%c %08lX %s ", sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig)); if( sigrc == '%' ) - printf("[%s] ", g10_errstr(rc2) ); + fprintf( list_fp, "[%s] ", g10_errstr(rc2) ); else if( sigrc == '?' ) ; else if( is_selfsig ) { if( opt.with_colons ) - putchar(':'); - fputs( sig->sig_class == 0x18? "[keybind]":"[selfsig]", stdout); + fputc(':', list_fp); + fputs( sig->sig_class == 0x18? "[keybind]":"[selfsig]", list_fp ); if( opt.with_colons ) - putchar(':'); + fputc(':', list_fp); } else if( !opt.fast_list_mode ) { p = get_user_id( sig->keyid, &n ); - print_string( stdout, p, n, opt.with_colons ); + print_string( list_fp, p, n, opt.with_colons ); m_free(p); } if( opt.with_colons ) - printf(":%02x%c:", sig->sig_class, sig->flags.exportable?'x':'l'); - putchar('\n'); + fprintf( list_fp, ":%02x%c:", sig->sig_class, + sig->flags.exportable?'x':'l'); + fputc('\n', list_fp); } else log_error("invalid node with packet of type %d\n", node->pkt->pkttype); @@ -1104,6 +1132,8 @@ do_proc_packets( CTX c, IOBUF a ) int any_data=0; int newpkt; + if( !list_fp ) + list_fp = stdout; c->iobuf = a; init_packet(pkt); while( (rc=parse_packet(a, pkt)) != -1 ) { diff --git a/g10/packet.h b/g10/packet.h index 492e8aa85..182ccdac3 100644 --- a/g10/packet.h +++ b/g10/packet.h @@ -356,6 +356,8 @@ int proc_signature_packets( void *ctx, IOBUF a, STRLIST signedfiles, const char *sigfile ); int proc_encryption_packets( void *ctx, IOBUF a ); int list_packets( IOBUF a ); +void mainproc_open_output( const char * file ); +FILE* mainproc_get_output( void ); /*-- parse-packet.c --*/ int set_packet_list_mode( int mode );