/ src / lshw.cc
lshw.cc
  1  #include "hw.h"
  2  #include "print.h"
  3  #include "main.h"
  4  #include "version.h"
  5  #include "options.h"
  6  #include "osutils.h"
  7  #include "config.h"
  8  
  9  #include <unistd.h>
 10  #include <stdio.h>
 11  #include <string.h>
 12  #include <stdlib.h>
 13  #include <iostream>
 14  
 15  #ifndef NONLS
 16  #include <locale.h>
 17  #endif
 18  
 19  __ID("@(#) $Id$");
 20  
 21  void usage(const char *progname)
 22  {
 23    fprintf(stderr, "Hardware Lister (lshw) - %s\n", getpackageversion());
 24    fprintf(stderr, _("usage: %s [-format] [-options ...]\n"), progname);
 25    fprintf(stderr, _("       %s -version\n"), progname);
 26    fprintf(stderr, "\n");
 27    fprintf(stderr, _("\t-version        print program version (%s)\n"), getpackageversion());
 28    fprintf(stderr, _("\nformat can be\n"));
 29    fprintf(stderr, _("\t-html           output hardware tree as HTML\n"));
 30    fprintf(stderr, _("\t-xml            output hardware tree as XML\n"));
 31    fprintf(stderr, _("\t-json           output hardware tree as a JSON object\n"));
 32    fprintf(stderr, _("\t-short          output hardware paths\n"));
 33    fprintf(stderr, _("\t-businfo        output bus information\n"));
 34    if(getenv("DISPLAY") && exists(SBINDIR"/gtk-lshw"))
 35      fprintf(stderr, _("\t-X              use graphical interface\n"));
 36    fprintf(stderr, _("\noptions can be\n"));
 37  #ifdef SQLITE
 38    fprintf(stderr, _("\t-dump filename  display output and dump collected information into a file (SQLite database)\n"));
 39  #endif
 40    fprintf(stderr, _("\t-class CLASS    only show a certain class of hardware\n"));
 41    fprintf(stderr, _("\t-C CLASS        same as '-class CLASS'\n"));
 42    fprintf(stderr, _("\t-c CLASS        same as '-class CLASS'\n"));
 43    fprintf(stderr,
 44      _("\t-disable TEST   disable a test (like pci, isapnp, cpuid, etc.)\n"));
 45    fprintf(stderr,
 46      _("\t-enable TEST    enable a test (like pci, isapnp, cpuid, etc.)\n"));
 47    fprintf(stderr, _("\t-quiet          don't display status\n"));
 48    fprintf(stderr, _("\t-sanitize       sanitize output (remove sensitive information like serial numbers, etc.)\n"));
 49    fprintf(stderr, _("\t-numeric        output numeric IDs (for PCI, USB, etc.)\n"));
 50    fprintf(stderr, _("\t-notime         exclude volatile attributes (timestamps) from output\n"));
 51    fprintf(stderr, "\n");
 52  }
 53  
 54  
 55  void status(const char *message)
 56  {
 57    static size_t lastlen = 0;
 58  
 59    if(enabled("output:quiet") || disabled("output:verbose"))
 60      return;
 61  
 62    if (isatty(2))
 63    {
 64      fprintf(stderr, "\r");
 65      for (size_t i = 0; i < lastlen; i++)
 66        fprintf(stderr, " ");
 67      fprintf(stderr, "\r%s\r", message);
 68      fflush(stderr);
 69      lastlen = strlen(message);
 70    }
 71  }
 72  
 73  
 74  int main(int argc,
 75  char **argv)
 76  {
 77  
 78  #ifndef NONLS
 79    setlocale (LC_ALL, "");
 80    bindtextdomain (PACKAGE, LOCALEDIR);
 81    bind_textdomain_codeset (PACKAGE, "UTF-8");
 82    textdomain (PACKAGE);
 83  #endif
 84  
 85    disable("isapnp");
 86  
 87    disable("output:list");
 88    disable("output:json");
 89    disable("output:db");
 90    disable("output:xml");
 91    disable("output:html");
 92    disable("output:hwpath");
 93    disable("output:businfo");
 94    disable("output:X");
 95    disable("output:quiet");
 96    disable("output:sanitize");
 97    disable("output:numeric");
 98    enable("output:time");
 99  
100  // define some aliases for nodes classes
101    alias("disc", "disk");
102    alias("cpu", "processor");
103    alias("lan", "network");
104    alias("net", "network");
105    alias("video", "display");
106    alias("sound", "multimedia");
107    alias("modem", "communication");
108  
109    if (!parse_options(argc, argv))
110    {
111      usage(argv[0]);
112      exit(1);
113    }
114  
115    while (argc >= 2)
116    {
117      bool validoption = false;
118  
119      if (strcmp(argv[1], "-version") == 0)
120      {
121        const char *latest = checkupdates();
122        printf("%s\n", getpackageversion());
123        if(latest)
124        {
125          if(strcmp(latest, getpackageversion()) != 0)
126            fprintf(stderr, _("the latest version is %s\n"), latest);
127        }
128        exit(0);
129      }
130  
131      if (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0)
132      {
133        usage(argv[0]);
134        exit(0);
135      }
136  
137      if (strcmp(argv[1], "-verbose") == 0)
138      {
139        disable("output:quiet");
140        enable("output:verbose");
141        validoption = true;
142      }
143  
144      if (strcmp(argv[1], "-quiet") == 0)
145      {
146        disable("output:verbose");
147        enable("output:quiet");
148        validoption = true;
149      }
150  
151      if (strcmp(argv[1], "-json") == 0)
152      {
153        enable("output:json");
154        validoption = true;
155      }
156  
157      if (strcmp(argv[1], "-xml") == 0)
158      {
159        enable("output:xml");
160        validoption = true;
161      }
162  
163      if (strcmp(argv[1], "-html") == 0)
164      {
165        enable("output:html");
166        validoption = true;
167      }
168  
169      if (strcmp(argv[1], "-short") == 0)
170      {
171        enable("output:hwpath");
172        validoption = true;
173      }
174  
175      if (strcmp(argv[1], "-businfo") == 0)
176      {
177        enable("output:businfo");
178        validoption = true;
179      }
180  
181      if (strcmp(argv[1], "-X") == 0)
182      {
183        enable("output:X");
184        validoption = true;
185      }
186  
187      if ((strcmp(argv[1], "-sanitize") == 0) ||
188         (strcmp(argv[1], "-sanitise") == 0))
189      {
190        enable("output:sanitize");
191        validoption = true;
192      }
193  
194      if (strcmp(argv[1], "-numeric") == 0)
195      {
196        enable("output:numeric");
197        validoption = true;
198      }
199  
200      if (strcmp(argv[1], "-notime") == 0)
201      {
202          disable("output:time");
203          validoption = true;
204      }
205  
206      if(validoption)
207      {	/* shift */
208        memmove(argv+1, argv+2, (argc-1)*(sizeof(argv[0])));
209        argc--;
210      }
211      else
212      {
213        usage(argv[0]);
214        exit(1);
215      }
216    }
217  
218    if (argc >= 2)
219    {
220      usage(argv[0]);
221      exit(1);
222    }
223  
224    if(enabled("output:X")) execl(SBINDIR"/gtk-lshw", SBINDIR"/gtk-lshw", NULL);
225  
226    if (geteuid() != 0)
227    {
228      fprintf(stderr, _("WARNING: you should run this program as super-user.\n"));
229    }
230  
231    {
232      hwNode computer("computer",
233        hw::system);
234  
235      scan_system(computer);
236  
237      if (enabled("output:hwpath"))
238        printhwpath(computer);
239      else
240      if (enabled("output:businfo"))
241        printbusinfo(computer);
242      else
243      {
244        if (enabled("output:json"))
245          cout << computer.asJSON() << endl;
246        else
247        if (enabled("output:xml"))
248          cout << computer.asXML();
249        else
250          print(computer, enabled("output:html"));
251      }
252  
253      if(enabled("output:db"))
254        computer.dump(getenv("OUTFILE"));
255    }
256  
257    if (geteuid() != 0)
258    {
259      fprintf(stderr, _("WARNING: output may be incomplete or inaccurate, you should run this program as super-user.\n"));
260    }
261  
262  
263    return 0;
264  }