Back to index

lshw  02.16
lshw.cc
Go to the documentation of this file.
00001 #include "hw.h"
00002 #include "print.h"
00003 #include "main.h"
00004 #include "version.h"
00005 #include "options.h"
00006 #include "osutils.h"
00007 #include "config.h"
00008 
00009 #include <unistd.h>
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <stdlib.h>
00013 #include <iostream>
00014 
00015 #ifndef NONLS
00016 #include <locale.h>
00017 #endif
00018 
00019 __ID("@(#) $Id: lshw.cc 2433 2012-01-10 22:01:30Z lyonel $");
00020 
00021 void usage(const char *progname)
00022 {
00023   fprintf(stderr, "Hardware Lister (lshw) - %s\n", getpackageversion());
00024   fprintf(stderr, _("usage: %s [-format] [-options ...]\n"), progname);
00025   fprintf(stderr, _("       %s -version\n"), progname);
00026   fprintf(stderr, "\n");
00027   fprintf(stderr, _("\t-version        print program version (%s)\n"), getpackageversion());
00028   fprintf(stderr, _("\nformat can be\n"));
00029   fprintf(stderr, _("\t-html           output hardware tree as HTML\n"));
00030   fprintf(stderr, _("\t-xml            output hardware tree as XML\n"));
00031   fprintf(stderr, _("\t-short          output hardware paths\n"));
00032   fprintf(stderr, _("\t-businfo        output bus information\n"));
00033   if(getenv("DISPLAY") && exists(SBINDIR"/gtk-lshw"))
00034     fprintf(stderr, _("\t-X              use graphical interface\n"));
00035   fprintf(stderr, _("\noptions can be\n"));
00036 #ifdef SQLITE
00037   fprintf(stderr, _("\t-dump OUTFILE   save hardware tree to a file\n"));
00038 #endif
00039   fprintf(stderr, _("\t-class CLASS    only show a certain class of hardware\n"));
00040   fprintf(stderr, _("\t-C CLASS        same as '-class CLASS'\n"));
00041   fprintf(stderr, _("\t-c CLASS        same as '-class CLASS'\n"));
00042   fprintf(stderr,
00043     _("\t-disable TEST   disable a test (like pci, isapnp, cpuid, etc. )\n"));
00044   fprintf(stderr,
00045     _("\t-enable TEST    enable a test (like pci, isapnp, cpuid, etc. )\n"));
00046   fprintf(stderr, _("\t-quiet          don't display status\n"));
00047   fprintf(stderr, _("\t-sanitize       sanitize output (remove sensitive information like serial numbers, etc.)\n"));
00048   fprintf(stderr, _("\t-numeric        output numeric IDs (for PCI, USB, etc.)\n"));
00049   fprintf(stderr, "\n");
00050 }
00051 
00052 
00053 void status(const char *message)
00054 {
00055   static size_t lastlen = 0;
00056 
00057   if(enabled("output:quiet") || disabled("output:verbose"))
00058     return;
00059 
00060   if (isatty(2))
00061   {
00062     fprintf(stderr, "\r");
00063     for (size_t i = 0; i < lastlen; i++)
00064       fprintf(stderr, " ");
00065     fprintf(stderr, "\r%s\r", message);
00066     fflush(stderr);
00067     lastlen = strlen(message);
00068   }
00069 }
00070 
00071 
00072 int main(int argc,
00073 char **argv)
00074 {
00075 
00076 #ifndef NONLS
00077   setlocale (LC_ALL, "");
00078   bindtextdomain (PACKAGE, LOCALEDIR);
00079   bind_textdomain_codeset (PACKAGE, "UTF-8");
00080   textdomain (PACKAGE);
00081 #endif
00082 
00083   disable("isapnp");
00084 
00085   disable("output:json");
00086   disable("output:db");
00087   disable("output:xml");
00088   disable("output:html");
00089   disable("output:hwpath");
00090   disable("output:businfo");
00091   disable("output:X");
00092   disable("output:quiet");
00093   disable("output:sanitize");
00094   disable("output:numeric");
00095 
00096 // define some aliases for nodes classes
00097   alias("disc", "disk");
00098   alias("cpu", "processor");
00099   alias("lan", "network");
00100   alias("net", "network");
00101   alias("video", "display");
00102   alias("sound", "multimedia");
00103   alias("modem", "communication");
00104 
00105   if (!parse_options(argc, argv))
00106   {
00107     usage(argv[0]);
00108     exit(1);
00109   }
00110 
00111   while (argc >= 2)
00112   {
00113     bool validoption = false;
00114 
00115     if (strcmp(argv[1], "-version") == 0)
00116     {
00117       const char *latest = checkupdates();
00118       printf("%s\n", getpackageversion());
00119       if(latest)
00120       {
00121         if(strcmp(latest, getpackageversion()) != 0)
00122           fprintf(stderr, _("the latest version is %s\n"), latest);
00123       }
00124       exit(0);
00125     }
00126 
00127     if (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0)
00128     {
00129       usage(argv[0]);
00130       exit(0);
00131     }
00132 
00133     if (strcmp(argv[1], "-verbose") == 0)
00134     {
00135       disable("output:quiet");
00136       enable("output:verbose");
00137       validoption = true;
00138     }
00139 
00140     if (strcmp(argv[1], "-quiet") == 0)
00141     {
00142       disable("output:verbose");
00143       enable("output:quiet");
00144       validoption = true;
00145     }
00146 
00147     if (strcmp(argv[1], "-json") == 0)
00148     {
00149       enable("output:json");
00150       validoption = true;
00151     }
00152 
00153     if (strcmp(argv[1], "-xml") == 0)
00154     {
00155       enable("output:xml");
00156       validoption = true;
00157     }
00158 
00159     if (strcmp(argv[1], "-html") == 0)
00160     {
00161       enable("output:html");
00162       validoption = true;
00163     }
00164 
00165     if (strcmp(argv[1], "-short") == 0)
00166     {
00167       enable("output:hwpath");
00168       validoption = true;
00169     }
00170 
00171     if (strcmp(argv[1], "-businfo") == 0)
00172     {
00173       enable("output:businfo");
00174       validoption = true;
00175     }
00176 
00177     if (strcmp(argv[1], "-X") == 0)
00178     {
00179       enable("output:X");
00180       validoption = true;
00181     }
00182 
00183     if ((strcmp(argv[1], "-sanitize") == 0) ||
00184        (strcmp(argv[1], "-sanitise") == 0))
00185     {
00186       enable("output:sanitize");
00187       validoption = true;
00188     }
00189 
00190     if (strcmp(argv[1], "-numeric") == 0)
00191     {
00192       enable("output:numeric");
00193       validoption = true;
00194     }
00195 
00196     if(validoption)
00197     {  /* shift */
00198       memmove(argv+1, argv+2, (argc-1)*(sizeof(argv[0])));
00199       argc--;
00200     }
00201     else
00202     {
00203       usage(argv[0]);
00204       exit(1);
00205     }
00206   }
00207 
00208   if (argc >= 2)
00209   {
00210     usage(argv[0]);
00211     exit(1);
00212   }
00213 
00214   if(enabled("output:X")) execl(SBINDIR"/gtk-lshw", SBINDIR"/gtk-lshw", NULL);
00215 
00216   if (geteuid() != 0)
00217   {
00218     fprintf(stderr, _("WARNING: you should run this program as super-user.\n"));
00219   }
00220 
00221   {
00222     hwNode computer("computer",
00223       hw::system);
00224 
00225     scan_system(computer);
00226 
00227     if (enabled("output:hwpath"))
00228       printhwpath(computer);
00229     else
00230     if (enabled("output:businfo"))
00231       printbusinfo(computer);
00232     else
00233     {
00234       if (enabled("output:json"))
00235         cout << computer.asJSON() << endl;
00236       else
00237       if (enabled("output:xml"))
00238         cout << computer.asXML();
00239       else
00240         print(computer, enabled("output:html"));
00241     }
00242 
00243     if(enabled("output:db"))
00244       computer.dump(getenv("OUTFILE"));
00245   }
00246 
00247   if (geteuid() != 0)
00248   {
00249     fprintf(stderr, _("WARNING: output may be incomplete or inaccurate, you should run this program as super-user.\n"));
00250   }
00251 
00252 
00253   return 0;
00254 }