From d244269886c8448239b88126983243481eeb71b3 Mon Sep 17 00:00:00 2001 From: David MacKenzie Date: Thu, 29 Apr 1993 06:45:39 +0000 Subject: [PATCH] Clean up usage messages and option parsers. Add program name to some error messages. --- binutils/ar.c | 88 +++++++++++++++++++++++++--------------------- binutils/copy.c | 22 ++++++++---- binutils/nm.c | 72 ++++++++++++++++++++++--------------- binutils/objdump.c | 21 ++++++----- binutils/size.c | 74 ++++++++++++++++++-------------------- 5 files changed, 155 insertions(+), 122 deletions(-) diff --git a/binutils/ar.c b/binutils/ar.c index d034ab2d6cc..0d52a35e949 100644 --- a/binutils/ar.c +++ b/binutils/ar.c @@ -95,13 +95,16 @@ int preserve_dates = 0; than the corresponding files. */ int newer_only = 0; -/* write a __.SYMDEF member into the modified archive. */ -boolean write_armap = false; -/* - Nonzero means don't update __.SYMDEF unless command line explicitly - requested it -*/ -int ignore_symdef = 0; + +/* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF + member). -1 means we've been explicitly asked to not write a symbol table; + +1 means we've been explictly asked to write it; + 0 is the default. + Traditionally, the default in BSD has been to not write the table. + However, for Posix.2 compliance the default is now to write a symbol table + if any of the members are object files. */ +int write_armap = 0; + /* Nonzero means it's the name of an existing member; position new or moved files with respect to this one. @@ -196,13 +199,24 @@ DEFUN(map_over_members,(function, files, count), boolean operation_alters_arch = false; +extern char *program_version; + void do_show_version () { - extern char *program_version; printf ("%s version %s\n", program_name, program_version); } +void +usage () +{ + fprintf(stderr, "ar %s\n\ +Usage: %s [-]{dmpqrtx}[abcilosuv] [membername] archive-file file...\n\ + %s -M [ 0 || (is_ranlib < 0 && strcmp(temp, "ranlib") == 0)) { if (argc < 2) - fatal("Too few command arguments."); + usage (); arg_ptr = argv[1]; if (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "-v") == 0) { do_show_version(); @@ -257,7 +271,7 @@ main(argc, argv) } if (argc < 2) - fatal("Too few command arguments."); + usage (); arg_ptr = argv[1]; @@ -314,7 +328,7 @@ main(argc, argv) show_version = true; break; case 's': - write_armap = true; + write_armap = 1; break; case 'u': newer_only = 1; @@ -347,14 +361,14 @@ main(argc, argv) if (show_version) exit(0); else - fatal("Too few command arguments."); + usage (); if (mri_mode) { mri_emul(); } else { if ((operation == none || operation == print_table) - && write_armap == true) + && write_armap == 1) ranlib_only(argv[2]); if (operation == none) @@ -370,16 +384,7 @@ main(argc, argv) inarch_filename = argv[arg_index++]; - if (arg_index < argc) { - files = argv + arg_index; - while (arg_index < argc) - if (!strcmp(argv[arg_index++], "__.SYMDEF")) { - ignore_symdef = 1; - break; - } - } - else - files = NULL; + files = arg_index < argc ? argv + arg_index : NULL; if (operation == quick_append) { if (files != NULL) @@ -415,7 +420,7 @@ main(argc, argv) break; case replace: - if (files != NULL || write_armap) + if (files != NULL || write_armap > 0) replace_members(files); break; @@ -470,6 +475,7 @@ open_inarch(archive_filename) #endif if (inarch == NULL) { bloser: + fprintf (stderr, "%s: ", program_name); bfd_perror(archive_filename); exit(1); } @@ -602,7 +608,7 @@ extract_file(abfd) struct utimbuf tb; tb.actime = buf.st_mtime; tb.modtime = buf.st_mtime; - utime(abfd->filename, tb); /* FIXME check result */ + utime(abfd->filename, &tb); /* FIXME check result */ #else /* ! POSIX_UTIME */ #ifdef USE_UTIME long tb[2]; @@ -660,6 +666,7 @@ do_quick_append(archive_filename, files_to_append) temp = bfd_openr(archive_filename, NULL); #endif if (temp == NULL) { + fprintf (stderr, "%s: ", program_name); bfd_perror(archive_filename); exit(1); } @@ -682,6 +689,7 @@ do_quick_append(archive_filename, files_to_append) for (; files_to_append && *files_to_append; ++files_to_append) { struct ar_hdr *hdr = bfd_special_undocumented_glue(temp, *files_to_append); if (hdr == NULL) { + fprintf (stderr, "%s: ", program_name); bfd_perror(*files_to_append); exit(1); } @@ -690,10 +698,15 @@ do_quick_append(archive_filename, files_to_append) ifile = fopen(*files_to_append, FOPEN_RB); if (ifile == NULL) + { bfd_perror(program_name); + } if (stat(*files_to_append, &sbuf) != 0) + { + fprintf (stderr, "%s: ", program_name); bfd_perror(*files_to_append); + } tocopy = sbuf.st_size; @@ -736,7 +749,10 @@ write_archive() bfd_fatal(inarch->filename); bfd_set_format(obfd, bfd_archive); - obfd->has_armap = write_armap; + + /* Request writing the archive symbol table unless we've + been explicitly requested not to. */ + obfd->has_armap = write_armap >= 0; if (bfd_set_archive_head(obfd, contents_head) != true) bfd_fatal(inarch->filename); @@ -795,12 +811,13 @@ delete_members(files_to_delete) /* In a.out systems, the armap is optional. It's also called __.SYMDEF. So if the user asked to delete it, we should remember - that fact. The name is NULL in COFF archives, so using this as a - key is as good as anything I suppose - */ + that fact. This isn't quite right for COFF systems (where + __.SYMDEF might be regular member), but it's very unlikely + to be a problem. FIXME */ + if (!strcmp(*files_to_delete, "__.SYMDEF")) { inarch->has_armap = false; - write_armap = false; + write_armap = -1; continue; } @@ -890,15 +907,6 @@ replace_members(files_to_move) bfd *current; bfd **current_ptr; bfd *temp; - /* - If the first item in the archive is an __.SYMDEF then remove it - */ - if (inarch->next && - strcmp(inarch->next->filename, "__.SYMDEF") == 0) { - inarch->next = inarch->next->next; - } - - while (files_to_move && *files_to_move) { current_ptr = &inarch->next; @@ -984,7 +992,7 @@ static void ranlib_only(archname) char *archname; { - write_armap = true; + write_armap = 1; open_inarch(archname); write_archive(); exit(0); diff --git a/binutils/copy.c b/binutils/copy.c index 74aa0965f79..66295b52ec1 100644 --- a/binutils/copy.c +++ b/binutils/copy.c @@ -104,9 +104,13 @@ static void copy_usage() { - fprintf(stderr, - "Usage %s [-vVSgxX] [-I informat] [-O outformat] infile [outfile]\n", - program_name); + fprintf(stderr, "copy %s\n\ +Usage: %s [-vVSgxX] [-I format] [-O format] [-F format]\n\ + [--format=format] [--target=format] [--input-format=format]\n\ + [--output-format=format] [--strip-all] [--strip-debug]\n\ + [--discard-all] [--discard-locals] [--verbose] [--version]\n\ + infile [outfile]\n", + program_version, program_name); exit(1); } @@ -114,7 +118,12 @@ static void strip_usage() { - fprintf(stderr, "Usage %s [-vVsSgxX] [-I informat] [-O outformat] filename ...\n", program_name); + fprintf(stderr, "strip %s\n\ +Usage: %s [-vVsSgxX] [-I format] [-O format] [-F format]\n\ + [--format=format] [--target=format] [--input-format=format]\n\ + [--output-format=format] [--strip-all] [--strip-debug]\n\ + [--discard-all] [--discard-locals] [--verbose] [--version] file...\n", + program_version, program_name); exit(1); } @@ -498,7 +507,6 @@ main(argc, argv) { int i; int c; /* sez which option char */ - int option_index = 0; /* used by getopt and ignored by us */ program_name = argv[0]; @@ -515,7 +523,7 @@ main(argc, argv) if (is_strip) { while ((c = getopt_long(argc, argv, "I:O:F:sSgxXVv", - strip_options, &option_index)) + strip_options, (int *) 0)) != EOF) { switch (c) { case 'I': @@ -578,7 +586,7 @@ main(argc, argv) /* Invoked as "copy", not "strip" */ while ((c = getopt_long(argc, argv, "I:s:O:d:F:b:SgxXVv", - strip_options, &option_index)) + strip_options, (int *) 0)) != EOF) { switch (c) { case 'I': diff --git a/binutils/nm.c b/binutils/nm.c index 8a9b6427ca0..4fe3e58eb5b 100644 --- a/binutils/nm.c +++ b/binutils/nm.c @@ -28,13 +28,14 @@ static boolean display_file PARAMS ((char *filename)); static void -do_one_rel_file PARAMS ((bfd *file)); +do_one_rel_file PARAMS ((bfd* file, bfd *archive)); static unsigned int filter_symbols PARAMS ((bfd *file, asymbol **syms, unsigned long symcount)); static void -print_symbols PARAMS ((bfd *file, asymbol **syms, unsigned long symcount)); +print_symbols PARAMS ((bfd *file, asymbol **syms, unsigned long symcount, + bfd *archive)); static void print_symdef_entry PARAMS ((bfd * abfd)); @@ -62,14 +63,14 @@ extern int print_version; struct option long_options[] = { {"debug-syms", no_argument, &print_debug_syms, 1}, {"extern-only", no_argument, &external_only, 1}, - {"no-sort", no_argument, &no_sort, 1}, + {"no-sort", no_argument, &no_sort, 1}, {"numeric-sort", no_argument, &sort_numerically, 1}, {"print-armap", no_argument, &print_armap, 1}, {"print-file-name", no_argument, &file_on_each_line, 1}, {"reverse-sort", no_argument, &reverse_sort, 1}, - {"target", optional_argument, (int *)NULL, 0}, + {"target", optional_argument, 0, 200}, {"undefined-only", no_argument, &undefined_only, 1}, - {"version", no_argument, &show_version, 1}, + {"version", no_argument, &show_version, 1}, {0, no_argument, 0, 0} }; @@ -80,9 +81,12 @@ int show_names = 0; void usage () { - fprintf(stderr, "nm %s\nUsage: %s [-agnoprsuV] filename...\n", + fprintf(stderr, "nm %s\n\ +Usage: %s [-agnoprsuV] [--debug-syms] [--extern-only] [--print-armap]\n\ + [--print-file-name] [--numeric-sort] [--no-sort] [--reverse-sort]\n\ + [--undefined-only] [--target=bfdname] [file...]\n", program_version, program_name); - exit(0); + exit(1); } int @@ -91,13 +95,12 @@ main (argc, argv) char **argv; { int c; /* sez which option char */ - int option_index = 0; /* used by getopt and ignored by us */ int retval; program_name = *argv; bfd_init(); - while ((c = getopt_long(argc, argv, "agnoprsuV", long_options, &option_index)) != EOF) { + while ((c = getopt_long(argc, argv, "agnoprsuvABV", long_options, (int *) 0)) != EOF) { switch (c) { case 'a': print_debug_syms = 1; break; case 'g': external_only = 1; break; @@ -107,14 +110,18 @@ main (argc, argv) case 'r': reverse_sort = 1; break; case 's': print_armap = 1; break; case 'u': undefined_only = 1; break; + case 'v': case 'V': show_version = 1; break; - case 0: - if (!strcmp("target",(long_options[option_index]).name)) { - target = optarg; - } + /* For MIPS compatibility, -A selects System V style output, -B + selects BSD style output. These are not implemented. When + they are, they should be added to usage (). */ + case 'A': break; + case 'B': break; - break; /* we've been given a long option */ + case 200: /* --target */ + target = optarg; + break; default: usage (); @@ -128,7 +135,7 @@ main (argc, argv) on sucess -- the inverse of the C sense. */ /* OK, all options now parsed. If no filename specified, do a.out. */ - if (option_index == argc) return !display_file ("a.out"); + if (optind == argc) return !display_file ("a.out"); retval = 0; show_names = (argc -optind)>1; @@ -156,7 +163,8 @@ display_file (filename) file = bfd_openr(filename, target); if (file == NULL) { - fprintf (stderr, "\n%s: can't open '%s'.\n", program_name, filename); + fprintf (stderr, "%s: ", program_name); + bfd_perror (filename); return false; } @@ -165,16 +173,17 @@ display_file (filename) if (show_names) { printf ("\n%s:\n",filename); } - do_one_rel_file (file); + do_one_rel_file (file, NULL); } else if (bfd_check_format (file, bfd_archive)) { if (!bfd_check_format (file, bfd_archive)) { - fprintf (stderr, "%s: %s: unknown format.\n", program_name, filename); + fprintf (stderr, "%s: %s: unknown format\n", program_name, filename); retval = false; goto closer; } - printf("\n%s:\n", filename); + if (!file_on_each_line) + printf("\n%s:\n", filename); if (print_armap) print_symdef_entry (file); for (;;) { arfile = bfd_openr_next_archived_file (file, arfile); @@ -188,13 +197,14 @@ display_file (filename) if (!bfd_check_format(arfile, bfd_object)) printf("%s: not an object file\n", arfile->filename); else { - printf ("\n%s:\n", arfile->filename); - do_one_rel_file (arfile) ; + if (!file_on_each_line) + printf ("\n%s:\n", arfile->filename); + do_one_rel_file (arfile, file) ; } } } else { - fprintf (stderr, "\n%s: %s: unknown format.\n", program_name, filename); + fprintf (stderr, "\n%s: %s: unknown format\n", program_name, filename); retval = false; } @@ -249,8 +259,9 @@ int (*(sorters[2][2])) PARAMS ((CONST void *, CONST void *)) = { }; static void -do_one_rel_file (abfd) +do_one_rel_file (abfd, archive_bfd) bfd *abfd; + bfd *archive_bfd; /* If non-NULL: archive containing abfd. */ { unsigned int storage; asymbol **syms; @@ -266,7 +277,7 @@ do_one_rel_file (abfd) nosymz: fprintf (stderr, "%s: Symflags set but there are none?\n", bfd_get_filename (abfd)); - exit (1); + return; } syms = (asymbol **) xmalloc (storage); @@ -287,7 +298,7 @@ do_one_rel_file (abfd) if (print_each_filename && !file_on_each_line) printf("\n%s:\n", bfd_get_filename(abfd)); - print_symbols (abfd, syms, symcount); + print_symbols (abfd, syms, symcount, archive_bfd); free (syms); } @@ -315,7 +326,7 @@ filter_symbols (abfd, syms, symcount) } else if (external_only) { keep = ((flags & BSF_GLOBAL) || (sym->section == &bfd_und_section) - || (sym->section == &bfd_com_section)); + || (bfd_is_com_section (sym->section))); } else { keep = 1; } @@ -333,15 +344,20 @@ filter_symbols (abfd, syms, symcount) } static void -print_symbols (abfd, syms, symcount) +print_symbols (abfd, syms, symcount, archive_bfd) bfd *abfd; asymbol **syms; unsigned long symcount; + bfd *archive_bfd; { asymbol **sym = syms, **end = syms + symcount; for (; sym < end; ++sym) { - if (file_on_each_line) printf("%s:", bfd_get_filename(abfd)); + if (file_on_each_line) { + if (archive_bfd) + printf("%s:", bfd_get_filename(archive_bfd)); + printf("%s:", bfd_get_filename(abfd)); + } if (undefined_only) { if ((*sym)->section == &bfd_und_section) diff --git a/binutils/objdump.c b/binutils/objdump.c index 1c1c58954ec..545293b8da8 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -88,10 +88,10 @@ dump_symbols PARAMS ((bfd *abfd)); void usage () { - fprintf (stderr, "\ -usage: %s [-ahifdrtxsl] [-m machine] [-j section_name]\n\ - [--syms] [--reloc] [--header] [--version] obj ...\n", - program_name); + fprintf (stderr, "objdump %s\n\ +Usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] [-b bfdname]\n\ + [--syms] [--reloc] [--header] [--version] objfile...\n", + program_version, program_name); exit (1); } @@ -104,7 +104,8 @@ static struct option long_options[]= #ifdef ELF_STAB_DISPLAY {"stabs", no_argument, &dump_stab_section_info, 1}, #endif - {0, no_argument, 0, 0}}; + {0, no_argument, 0, 0} +}; static void @@ -745,6 +746,7 @@ display_file (filename, target) file = bfd_openr (filename, target); if (file == NULL) { + fprintf (stderr, "%s: ", program_name); bfd_perror (filename); return; } @@ -760,7 +762,10 @@ display_file (filename, target) if (arfile == NULL) { if (bfd_error != no_more_archived_files) - bfd_perror (bfd_get_filename (file)); + { + fprintf (stderr, "%s: ", program_name); + bfd_perror (bfd_get_filename (file)); + } return; } @@ -1052,12 +1057,12 @@ main (argc, argv) extern char *optarg; char *target = default_target; boolean seenflag = false; - int ind = 0; bfd_init (); program_name = *argv; - while ((c = getopt_long (argc, argv, "ib:m:Vdlfahrtxsj:", long_options, &ind)) + while ((c = getopt_long (argc, argv, "ib:m:Vdlfahrtxsj:", long_options, + (int *) 0)) != EOF) { seenflag = true; diff --git a/binutils/size.c b/binutils/size.c index e30799b23fd..2c587bb05dd 100644 --- a/binutils/size.c +++ b/binutils/size.c @@ -26,7 +26,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ o - We also handle core files. o - We also handle archives. If you write shell scripts which manipulate this info then you may be - out of luck; there's no +predantic switch. + out of luck; there's no --predantic option. */ #include "bfd.h" @@ -64,31 +64,26 @@ print_sizes PARAMS ((bfd *file)); void usage () { - fprintf (stderr, "size %s\nUsage: %s -{dox}{AB}V files ...\n", - program_version, program_name); - fputs("\t+radix={8|10|16} -- select appropriate output radix.\n\ -\t-d -- output in decimal\n\ -\t-o -- output in octal\n\ -\t-x -- output in hex", stderr); - fputs("\t+format={Berkeley|SysV} -- select display format.\n\ -\t-A -- SysV(AT&T) format\n\ -\t-B -- BSD format", stderr); + fprintf (stderr, "size %s\n\ +Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\ + [--target=bfdname] [--version] [--help] [file...]\n", + program_version, program_name); #if BSD_DEFAULT - fputs("\t (Default is +format=Berkeley)", stderr); + fputs (" (default is --format=berkeley)\n", stderr); #else - fputs("\t (Default is +format=SysV)", stderr); + fputs (" (default is --format=sysv)\n", stderr); #endif - fputs("\t-V, +version -- display program version, etc.\n\ -\t+help -- this message\n", stderr); - exit(1); + exit (1); } -struct option long_options[] = {{"radix", no_argument, 0, 0}, - {"format", required_argument, 0, 0}, - {"version", no_argument, &show_version, 1}, - {"target", optional_argument, NULL, 0}, - {"help", no_argument, &show_help, 1}, - {0, no_argument, 0, 0}}; +struct option long_options[] = { + {"format", required_argument, 0, 200}, + {"radix", required_argument, 0, 201}, + {"target", required_argument, 0, 202}, + {"version", no_argument, &show_version, 1}, + {"help", no_argument, &show_help, 1}, + {0, no_argument, 0, 0} +}; int main (argc, argv) @@ -97,32 +92,29 @@ main (argc, argv) { int temp; int c; /* sez which option char */ - int option_index = 0; extern int optind; /* steps thru options */ + program_name = *argv; bfd_init(); while ((c = getopt_long(argc, argv, "ABVdox", long_options, - &option_index)) != EOF) + (int *) 0)) != EOF) switch(c) { - case 0: - if (!strcmp("format",(long_options[option_index]).name)) { + case 200: /* --format */ switch(*optarg) { case 'B': case 'b': berkeley_format = 1; break; case 'S': case 's': berkeley_format = 0; break; - default: printf("Unknown option to +format: %s\n", optarg); + default: fprintf(stderr, "invalid argument to --format: %s\n", optarg); usage(); } break; - } - if (!strcmp("target",(long_options[option_index]).name)) { + case 202: /* --target */ target = optarg; break; - } - if (!strcmp("radix",(long_options[option_index]).name)) { + case 201: /* --radix */ #ifdef ANSI_LIBRARIES temp = strtol(optarg, NULL, 10); #else @@ -135,8 +127,8 @@ main (argc, argv) default: printf("Unknown radix: %s\n", optarg); usage(); } - } - break; + break; + case 'A': berkeley_format = 0; break; case 'B': berkeley_format = 1; break; case 'V': show_version = 1; break; @@ -202,6 +194,7 @@ display_file(filename) file = bfd_openr (filename, target); if (file == NULL) { + fprintf (stderr, "%s: ", program_name); bfd_perror (filename); return_code = 1; return; @@ -215,6 +208,7 @@ display_file(filename) arfile = bfd_openr_next_archived_file (file, arfile); if (arfile == NULL) { if (bfd_error != no_more_archived_files) { + fprintf (stderr, "%s: ", program_name); bfd_perror (bfd_get_filename (file)); return_code = 2; } @@ -237,8 +231,9 @@ lprint_number (width, num) int width; bfd_size_type num; { - printf ((radix == decimal ? "%-*ld\t" : - ((radix == octal) ? "%-*lo\t" : "%-*lx\t")), width, (long)num); + printf ((radix == decimal ? "%-*lu\t" : + ((radix == octal) ? "%-*lo\t" : "%-*lx\t")), + width, (unsigned long)num); } void @@ -246,8 +241,9 @@ rprint_number(width, num) int width; bfd_size_type num; { - printf ((radix == decimal ? "%*ld\t" : - ((radix == octal) ? "%*lo\t" : "%*lx\t")), width, (long)num); + printf ((radix == decimal ? "%*lu\t" : + ((radix == octal) ? "%*lo\t" : "%*lx\t")), + width, (unsigned long)num); } static char *bss_section_name = ".bss"; @@ -304,8 +300,8 @@ bfd *abfd; lprint_number (7, textsize); lprint_number (7, datasize); lprint_number (7, bsssize); - printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7ld\t%-7lx\t"), - (long)total, (long)total); + printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7lu\t%-7lx\t"), + (unsigned long)total, (unsigned long)total); fputs(bfd_get_filename(abfd), stdout); if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename); @@ -322,7 +318,7 @@ sysv_internal_printer(file, sec, ignore) { bfd_size_type size = bfd_section_size (file, sec); if (sec!= &bfd_abs_section - && sec!= &bfd_com_section + && ! bfd_is_com_section (sec) && sec!=&bfd_und_section) { -- 2.30.2