main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* Top level for GDB, the GNU debugger.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include <sys/file.h>
  18. #include <stdio.h>
  19. #include <setjmp.h>
  20. #include <signal.h>
  21. #include <sys/param.h>
  22. #include "defs.h"
  23. #include "command.h"
  24. /* Version number of GDB, as a string. */
  25. extern char *version;
  26. /* Chain containing all defined commands. */
  27. struct cmd_list_element *cmdlist;
  28. /* Chain containing all defined info subcommands. */
  29. struct cmd_list_element *infolist;
  30. /* stdio stream that command input is being read from. */
  31. FILE *instream;
  32. void free_command_lines ();
  33. char *read_line ();
  34. static void initialize_main ();
  35. void command_loop ();
  36. static void source_command ();
  37. void print_gdb_version ();
  38. /* gdb prints this when reading a command interactively */
  39. static char *prompt;
  40. /* Buffer used for reading command lines, and the size
  41. allocated for it so far. */
  42. char *line;
  43. int linesize;
  44. /* This is how `error' returns to command level. */
  45. jmp_buf to_top_level;
  46. return_to_top_level ()
  47. {
  48. quit_flag = 0;
  49. immediate_quit = 0;
  50. clear_breakpoint_commands ();
  51. clear_momentary_breakpoints ();
  52. do_cleanups (0);
  53. longjmp (to_top_level, 1);
  54. }
  55. main (argc, argv, envp)
  56. int argc;
  57. char **argv;
  58. char **envp;
  59. {
  60. extern void request_quit ();
  61. int count;
  62. int inhibit_gdbinit = 0;
  63. int quiet = 0;
  64. int batch = 0;
  65. register int i;
  66. quit_flag = 0;
  67. linesize = 100;
  68. line = (char *) xmalloc (linesize);
  69. instream = stdin;
  70. /* Run the init function of each source file */
  71. initialize_all_files ();
  72. initialize_main (); /* But that omits this file! Do it now */
  73. signal (SIGINT, request_quit);
  74. signal (SIGQUIT, SIG_IGN);
  75. /* Look for flag arguments. */
  76. for (i = 1; i < argc; i++)
  77. {
  78. if (!strcmp (argv[i], "-q") || !strcmp (argv[i], "-quiet"))
  79. quiet = 1;
  80. else if (!strcmp (argv[i], "-nx"))
  81. inhibit_gdbinit = 1;
  82. else if (!strcmp (argv[i], "-batch"))
  83. batch = 1, quiet = 1;
  84. else if (argv[i][0] == '-')
  85. i++;
  86. }
  87. if (!quiet)
  88. print_gdb_version ();
  89. /* Process the command line arguments. */
  90. count = 0;
  91. for (i = 1; i < argc; i++)
  92. {
  93. register char *arg = argv[i];
  94. /* Args starting with - say what to do with the following arg
  95. as a filename. */
  96. if (arg[0] == '-')
  97. {
  98. extern void exec_file_command (), symbol_file_command ();
  99. extern void core_file_command (), directory_command ();
  100. if (!strcmp (arg, "-q") || !strcmp (arg, "-nx")
  101. || !strcmp (arg, "quiet") || !strcmp (arg, "-batch"))
  102. /* Already processed above */
  103. continue;
  104. if (++i == argc)
  105. fprintf (stderr, "No argument follows \"%s\".\n", arg);
  106. if (!setjmp (to_top_level))
  107. {
  108. /* -s foo: get syms from foo. -e foo: execute foo.
  109. -se foo: do both with foo. -c foo: use foo as core dump. */
  110. if (!strcmp (arg, "-se"))
  111. {
  112. exec_file_command (argv[i], !batch);
  113. symbol_file_command (argv[i], !batch);
  114. }
  115. else if (!strcmp (arg, "-s") || !strcmp (arg, "-symbols"))
  116. symbol_file_command (argv[i], !batch);
  117. else if (!strcmp (arg, "-e") || !strcmp (arg, "-exec"))
  118. exec_file_command (argv[i], !batch);
  119. else if (!strcmp (arg, "-c") || !strcmp (arg, "-core"))
  120. core_file_command (argv[i], !batch);
  121. /* -x foo: execute commands from foo. */
  122. else if (!strcmp (arg, "-x") || !strcmp (arg, "-command")
  123. || !strcmp (arg, "-commands"))
  124. source_command (argv[i]);
  125. /* -d foo: add directory `foo' to source-file directory
  126. search-list */
  127. else if (!strcmp (arg, "-d") || !strcmp (arg, "-dir")
  128. || !strcmp (arg, "-directory"))
  129. directory_command (argv[i], 0);
  130. else
  131. error ("Unknown command-line switch: \"%s\"\n", arg);
  132. }
  133. }
  134. else
  135. {
  136. /* Args not thus accounted for
  137. are treated as, first, the symbol/executable file
  138. and, second, the core dump file. */
  139. count++;
  140. if (!setjmp (to_top_level))
  141. switch (count)
  142. {
  143. case 1:
  144. exec_file_command (arg, !batch);
  145. symbol_file_command (arg, !batch);
  146. break;
  147. case 2:
  148. core_file_command (arg, !batch);
  149. break;
  150. case 3:
  151. fprintf (stderr, "Excess command line args ignored. (%s%s)\n",
  152. arg, (i == argc - 1) ? "" : " ...");
  153. }
  154. }
  155. }
  156. /* Read init file, if it exists in home directory */
  157. if (getenv ("HOME"))
  158. {
  159. char *s;
  160. s = (char *) xmalloc (strlen (getenv ("HOME")) + 10);
  161. strcpy (s, getenv ("HOME"));
  162. strcat (s, "/.gdbinit");
  163. if (!inhibit_gdbinit && access (s, R_OK) == 0)
  164. if (!setjmp (to_top_level))
  165. source_command (s);
  166. }
  167. /* Read init file, if it exists in current directory. */
  168. if (!inhibit_gdbinit && access (".gdbinit", R_OK) == 0)
  169. if (!setjmp (to_top_level))
  170. source_command (".gdbinit");
  171. if (batch)
  172. fatal ("Attempt to read commands from stdin in batch mode.");
  173. if (!quiet)
  174. printf ("Type \"help\" for a list of commands.\n");
  175. /* The command loop. */
  176. while (1)
  177. {
  178. if (!setjmp (to_top_level))
  179. command_loop ();
  180. clearerr (stdin); /* Don't get hung if C-d is typed. */
  181. }
  182. }
  183. /* Execute the line P as a command.
  184. Pass FROM_TTY as second argument to the defining function. */
  185. void
  186. execute_command (p, from_tty)
  187. char *p;
  188. int from_tty;
  189. {
  190. register struct cmd_list_element *c;
  191. register struct command_line *cmdlines;
  192. free_all_values ();
  193. while (*p == ' ' || *p == '\t') p++;
  194. if (*p)
  195. {
  196. c = lookup_cmd (&p, cmdlist, "", 0);
  197. if (c->class == (int) class_user)
  198. {
  199. if (*p)
  200. error ("User-defined commands cannot take command-line arguments: \"%s\"",
  201. p);
  202. cmdlines = (struct command_line *) c->function;
  203. if (cmdlines == (struct command_line *) 0)
  204. /* Null command */
  205. return;
  206. while (cmdlines)
  207. {
  208. execute_command (cmdlines->line, 0);
  209. cmdlines = cmdlines->next;
  210. }
  211. }
  212. else if (c->function == 0)
  213. error ("That is not a command, just a help topic.");
  214. else
  215. /* Pass null arg rather than an empty one. */
  216. (*c->function) (*p ? p : 0, from_tty);
  217. }
  218. }
  219. /* Read commands from `instream' and execute them
  220. until end of file. */
  221. void
  222. command_loop ()
  223. {
  224. while (!feof (instream))
  225. {
  226. if (instream == stdin)
  227. printf ("%s", prompt);
  228. fflush (stdout);
  229. quit_flag = 0;
  230. execute_command (read_line (instream == stdin), instream == stdin);
  231. /* Do any commands attached to breakpoint we stopped at. */
  232. do_breakpoint_commands ();
  233. }
  234. }
  235. static void
  236. stop_sig ()
  237. {
  238. signal (SIGTSTP, SIG_DFL);
  239. sigsetmask (0);
  240. kill (getpid (), SIGTSTP);
  241. signal (SIGTSTP, stop_sig);
  242. printf ("%s", prompt);
  243. fflush (stdout);
  244. /* Forget about any previous command -- null line now will do nothing. */
  245. *line = 0;
  246. }
  247. /* Commands call this if they do not want to be repeated by null lines. */
  248. void
  249. dont_repeat ()
  250. {
  251. *line = 0;
  252. }
  253. /* Read one line from the command input stream `instream'
  254. into the buffer `line' (whose current length is `linesize').
  255. The buffer is made bigger as necessary.
  256. Returns the address of the start of the line. */
  257. char *
  258. read_line (repeat)
  259. int repeat;
  260. {
  261. register char *p = line;
  262. register char *p1;
  263. register int c;
  264. char *nline;
  265. /* Control-C quits instantly if typed while in this loop
  266. since it should not wait until the user types a newline. */
  267. immediate_quit++;
  268. signal (SIGTSTP, stop_sig);
  269. while (1)
  270. {
  271. c = fgetc (instream);
  272. if (c == -1 || c == '\n')
  273. break;
  274. if (p - line == linesize - 1)
  275. {
  276. linesize *= 2;
  277. nline = (char *) xrealloc (line, linesize);
  278. p += nline - line;
  279. line = nline;
  280. }
  281. *p++ = c;
  282. }
  283. signal (SIGTSTP, SIG_DFL);
  284. immediate_quit--;
  285. /* If we just got an empty line, and that is supposed
  286. to repeat the previous command, leave the last input unchanged. */
  287. if (p == line && repeat)
  288. return line;
  289. /* If line is a comment, clear it out. */
  290. p1 = line;
  291. while ((c = *p1) == ' ' || c == '\t') p1++;
  292. if (c == '#')
  293. p = line;
  294. *p = 0;
  295. return line;
  296. }
  297. /* Read lines from the input stream
  298. and accumulate them in a chain of struct command_line's
  299. which is then returned. */
  300. struct command_line *
  301. read_command_lines ()
  302. {
  303. struct command_line *first = 0;
  304. register struct command_line *next, *tail = 0;
  305. register char *p, *p1;
  306. struct cleanup *old_chain = 0;
  307. while (1)
  308. {
  309. dont_repeat ();
  310. p = read_line (1);
  311. /* Remove leading and trailing blanks. */
  312. while (*p == ' ' || *p == '\t') p++;
  313. p1 = p + strlen (p);
  314. while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
  315. /* Is this "end"? */
  316. if (p1 - p == 3 && !strncmp (p, "end", 3))
  317. break;
  318. /* No => add this line to the chain of command lines. */
  319. next = (struct command_line *) xmalloc (sizeof (struct command_line));
  320. next->line = savestring (p, p1 - p);
  321. next->next = 0;
  322. if (tail)
  323. {
  324. tail->next = next;
  325. }
  326. else
  327. {
  328. /* We just read the first line.
  329. From now on, arrange to throw away the lines we have
  330. if we quit or get an error while inside this function. */
  331. first = next;
  332. old_chain = make_cleanup (free_command_lines, &first);
  333. }
  334. tail = next;
  335. }
  336. dont_repeat ();
  337. /* Now we are about to return the chain to our caller,
  338. so freeing it becomes his responsibility. */
  339. if (first)
  340. discard_cleanups (old_chain);
  341. return first;
  342. }
  343. /* Free a chain of struct command_line's. */
  344. void
  345. free_command_lines (lptr)
  346. struct command_line **lptr;
  347. {
  348. register struct command_line *l = *lptr;
  349. register struct command_line *next;
  350. while (l)
  351. {
  352. next = l->next;
  353. free (l->line);
  354. free (l);
  355. l = next;
  356. }
  357. }
  358. /* Add an element to the list of info subcommands. */
  359. void
  360. add_info (name, fun, doc)
  361. char *name;
  362. void (*fun) ();
  363. char *doc;
  364. {
  365. add_cmd (name, 0, fun, doc, &infolist);
  366. }
  367. /* Add an alias to the list of info subcommands. */
  368. void
  369. add_info_alias (name, oldname, abbrev_flag)
  370. char *name;
  371. char *oldname;
  372. int abbrev_flag;
  373. {
  374. add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  375. }
  376. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  377. Therefore, its own definition is called only for "info" with no args. */
  378. static void
  379. info_command ()
  380. {
  381. printf ("\"info\" must be followed by the name of an info command.\n");
  382. help_cmd (0, infolist, "info ", -1, stdout);
  383. }
  384. /* Add an element to the list of commands. */
  385. void
  386. add_com (name, class, fun, doc)
  387. char *name;
  388. int class;
  389. void (*fun) ();
  390. char *doc;
  391. {
  392. add_cmd (name, class, fun, doc, &cmdlist);
  393. }
  394. /* Add an alias or abbreviation command to the list of commands. */
  395. void
  396. add_com_alias (name, oldname, class, abbrev_flag)
  397. char *name;
  398. char *oldname;
  399. int class;
  400. int abbrev_flag;
  401. {
  402. add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  403. }
  404. void
  405. error_no_arg (why)
  406. char *why;
  407. {
  408. error ("Argument required (%s).", why);
  409. }
  410. static void
  411. help_command (command, from_tty)
  412. char *command;
  413. int from_tty; /* Ignored */
  414. {
  415. help_cmd (command, cmdlist, "", -2, stdout);
  416. }
  417. static void
  418. validate_comname (comname)
  419. char *comname;
  420. {
  421. register char *p;
  422. if (comname == 0)
  423. error_no_arg ("name of command to define");
  424. p = comname;
  425. while (*p)
  426. {
  427. if (!(*p >= 'A' && *p <= 'Z')
  428. && !(*p >= 'a' && *p <= 'z')
  429. && !(*p >= '1' && *p <= '9')
  430. && *p != '-')
  431. error ("Junk in argument list: \"%s\"", p);
  432. p++;
  433. }
  434. }
  435. static void
  436. define_command (comname, from_tty)
  437. char *comname;
  438. int from_tty;
  439. {
  440. register struct command_line *cmds;
  441. register struct cmd_list_element *c;
  442. char *tem = comname;
  443. validate_comname (comname);
  444. c = lookup_cmd (&tem, cmdlist, "", -1);
  445. if (c)
  446. {
  447. if (c->class == (int) class_user || c->class == (int) class_alias)
  448. tem = "Redefine command \"%s\"? ";
  449. else
  450. tem = "Really redefine built-in command \"%s\"? ";
  451. if (!query (tem, comname))
  452. error ("Command \"%s\" not redefined.", comname);
  453. }
  454. if (from_tty)
  455. printf ("Type commands for definition of \"%s\".\n\
  456. End with a line saying just \"end\".\n", comname);
  457. cmds = read_command_lines ();
  458. if (c && c->class == (int) class_user)
  459. free_command_lines (&c->function);
  460. add_com (comname, class_user, cmds,
  461. (c && c->class == class_user)
  462. ? c->doc : savestring ("User-defined.", 13));
  463. }
  464. static void
  465. document_command (comname, from_tty)
  466. char *comname;
  467. int from_tty;
  468. {
  469. struct command_line *doclines;
  470. register struct cmd_list_element *c;
  471. char *tem = comname;
  472. validate_comname (comname);
  473. c = lookup_cmd (&tem, cmdlist, "", 0);
  474. if (c->class != (int) class_user)
  475. error ("Command \"%s\" is built-in.", comname);
  476. if (from_tty)
  477. printf ("Type documentation for \"%s\".\n\
  478. End with a line saying just \"end\".\n", comname);
  479. doclines = read_command_lines ();
  480. if (c->doc) free (c->doc);
  481. {
  482. register struct command_line *cl1;
  483. register int len = 0;
  484. for (cl1 = doclines; cl1; cl1 = cl1->next)
  485. len += strlen (cl1->line) + 1;
  486. c->doc = (char *) xmalloc (len + 1);
  487. *c->doc = 0;
  488. for (cl1 = doclines; cl1; cl1 = cl1->next)
  489. {
  490. strcat (c->doc, cl1->line);
  491. if (cl1->next)
  492. strcat (c->doc, "\n");
  493. }
  494. }
  495. free_command_lines (&doclines);
  496. }
  497. static void
  498. copying_info ()
  499. {
  500. immediate_quit++;
  501. printf (" GDB GENERAL PUBLIC LICENSE\n\
  502. \n\
  503. Copyright (C) 1986 Richard M. Stallman\n\
  504. Everyone is permitted to copy and distribute verbatim copies\n\
  505. of this license, but changing it is not allowed.\n\
  506. \n\
  507. The license agreements of most software companies keep you at the\n\
  508. mercy of those companies. By contrast, our general public license is\n\
  509. intended to give everyone the right to share GDB. To make sure that\n\
  510. you get the rights we want you to have, we need to make restrictions\n\
  511. that forbid anyone to deny you these rights or to ask you to surrender\n\
  512. the rights. Hence this license agreement.\n\
  513. \n\
  514. Specifically, we want to make sure that you have the right to give\n\
  515. away copies of GDB, that you receive source code or else can get it\n\
  516. if you want it, that you can change GDB or use pieces of it in new\n\
  517. free programs, and that you know you can do these things.\n\
  518. --Type Return to print more--");
  519. fflush (stdout);
  520. read_line ();
  521. printf ("\
  522. To make sure that everyone has such rights, we have to forbid you to\n\
  523. deprive anyone else of these rights. For example, if you distribute\n\
  524. copies of GDB, you must give the recipients all the rights that you\n\
  525. have. You must make sure that they, too, receive or can get the\n\
  526. source code. And you must tell them their rights.\n\
  527. \n\
  528. Also, for our own protection, we must make certain that everyone\n\
  529. finds out that there is no warranty for GDB. If GDB is modified by\n\
  530. someone else and passed on, we want its recipients to know that what\n\
  531. they have is not what we distributed, so that any problems introduced\n\
  532. by others will not reflect on our reputation.\n\
  533. \n\
  534. Therefore we (Richard Stallman and the Free Software Foundation,\n\
  535. Inc.) make the following terms which say what you must do to be\n\
  536. allowed to distribute or change GDB.\n\
  537. --Type Return to print more--");
  538. fflush (stdout);
  539. read_line ();
  540. printf ("\
  541. COPYING POLICIES\n\
  542. \n\
  543. 1. You may copy and distribute verbatim copies of GDB source code as\n\
  544. you receive it, in any medium, provided that you conspicuously and\n\
  545. appropriately publish on each copy a valid copyright notice \"Copyright\n\
  546. \(C) 1987 Free Software Foundation, Inc.\" (or with the year updated if\n\
  547. that is appropriate); keep intact the notices on all files that refer\n\
  548. to this License Agreement and to the absence of any warranty; and give\n\
  549. any other recipients of the GDB program a copy of this License\n\
  550. Agreement along with the program.\n\
  551. \n\
  552. 2. You may modify your copy or copies of GDB or any portion of it,\n\
  553. and copy and distribute such modifications under the terms of\n\
  554. Paragraph 1 above, provided that you also do the following:\n\
  555. \n\
  556. a) cause the modified files to carry prominent notices stating\n\
  557. that you changed the files and the date of any change; and\n\
  558. --Type Return to print more--");
  559. fflush (stdout);
  560. read_line ();
  561. printf ("\
  562. b) cause the whole of any work that you distribute or publish,\n\
  563. that in whole or in part contains or is a derivative of GDB\n\
  564. or any part thereof, to be freely distributed\n\
  565. and licensed to all third parties on terms identical to those\n\
  566. contained in this License Agreement (except that you may choose\n\
  567. to grant more extensive warranty protection to third parties,\n\
  568. at your option).\n\
  569. \n\
  570. c) if the modified program serves as a debugger, cause it\n\
  571. when started running in the simplest and usual way, to print\n\
  572. an announcement including a valid copyright notice\n\
  573. \"Copyright (C) 1987 Free Software Foundation, Inc.\" (or with\n\
  574. the year updated if appropriate), saying that there\n\
  575. is no warranty (or else, saying that you provide\n\
  576. a warranty) and that users may redistribute the program under\n\
  577. these conditions, and telling the user how to view a copy of\n\
  578. this License Agreement.\n\
  579. --Type Return to print more--");
  580. fflush (stdout);
  581. read_line ();
  582. printf ("\
  583. 3. You may copy and distribute GDB or any portion of it in\n\
  584. compiled, executable or object code form under the terms of Paragraphs\n\
  585. 1 and 2 above provided that you do the following:\n\
  586. \n\
  587. a) cause each such copy to be accompanied by the\n\
  588. corresponding machine-readable source code, which must\n\
  589. be distributed under the terms of Paragraphs 1 and 2 above; or,\n\
  590. \n\
  591. b) cause each such copy to be accompanied by a\n\
  592. written offer, with no time limit, to give any third party\n\
  593. free (except for a nominal shipping charge) a machine readable\n\
  594. copy of the corresponding source code, to be distributed\n\
  595. under the terms of Paragraphs 1 and 2 above; or,\n\n");
  596. printf ("\
  597. c) in the case of a recipient of GDB in compiled, executable\n\
  598. or object code form (without the corresponding source code) you\n\
  599. shall cause copies you distribute to be accompanied by a copy\n\
  600. of the written offer of source code which you received along\n\
  601. with the copy you received.\n\
  602. --Type Return to print more--");
  603. fflush (stdout);
  604. read_line ();
  605. printf ("\
  606. 4. You may not copy, sublicense, distribute or transfer GDB\n\
  607. except as expressly provided under this License Agreement. Any attempt\n\
  608. otherwise to copy, sublicense, distribute or transfer GDB is void and\n\
  609. your rights to use the program under this License agreement shall be\n\
  610. automatically terminated. However, parties who have received computer\n\
  611. software programs from you with this License Agreement will not have\n\
  612. their licenses terminated so long as such parties remain in full compliance.\n\
  613. \n\
  614. In other words, go ahead and share GDB, but don't try to stop\n\
  615. anyone else from sharing it farther. Help stamp out software hoarding!\n\
  616. ");
  617. immediate_quit--;
  618. }
  619. static void
  620. warranty_info ()
  621. {
  622. immediate_quit++;
  623. printf (" NO WARRANTY\n\
  624. \n\
  625. BECAUSE GDB IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY NO\n\
  626. WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT\n\
  627. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,\n\
  628. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE GDB \"AS IS\" WITHOUT\n\
  629. WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT\n\
  630. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n\
  631. A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND\n\
  632. PERFORMANCE OF GDB IS WITH YOU. SHOULD GDB PROVE DEFECTIVE, YOU\n\
  633. ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n");
  634. printf ("\
  635. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.\n\
  636. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY\n\
  637. WHO MAY MODIFY AND REDISTRIBUTE GDB, BE LIABLE TO\n\
  638. YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER\n\
  639. SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR\n\
  640. INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA\n\
  641. BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A\n\
  642. FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GDB, EVEN\n\
  643. IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR\n\
  644. ANY CLAIM BY ANY OTHER PARTY.\n");
  645. immediate_quit--;
  646. }
  647. static void
  648. print_gdb_version ()
  649. {
  650. printf ("GDB %s, Copyright (C) 1987 Free Software Foundation, Inc.\n\
  651. There is ABSOLUTELY NO WARRANTY for GDB; type \"info warranty\" for details.\n\
  652. GDB is free software and you are welcome to distribute copies of it\n\
  653. under certain conditions; type \"info copying\" to see the conditions.\n",
  654. version);
  655. }
  656. static void
  657. version_info ()
  658. {
  659. immediate_quit++;
  660. print_gdb_version ();
  661. immediate_quit--;
  662. }
  663. static void
  664. set_prompt_command (text)
  665. char *text;
  666. {
  667. char *p, *q;
  668. register int c;
  669. char *new;
  670. if (text == 0)
  671. error_no_arg ("string to which to set prompt");
  672. new = (char *) xmalloc (strlen (text) + 2);
  673. p = text; q = new;
  674. while (c = *p++)
  675. {
  676. if (c == '\\')
  677. {
  678. /* \ at end of argument is used after spaces
  679. so they won't be lost. */
  680. if (*p == 0)
  681. break;
  682. c = parse_escape (&p);
  683. if (c == 0)
  684. break; /* C loses */
  685. else if (c > 0)
  686. *q++ = c;
  687. }
  688. else
  689. *q++ = c;
  690. }
  691. if (*(p - 1) != '\\')
  692. *q++ = ' ';
  693. *q++ = '\0';
  694. new = (char *) xrealloc (new, q - new);
  695. free (prompt);
  696. prompt = new;
  697. }
  698. static void
  699. quit_command ()
  700. {
  701. if (have_inferior_p ())
  702. {
  703. if (query ("The program is running. Quit anyway? "))
  704. kill_inferior ();
  705. else
  706. error ("Not confirmed.");
  707. }
  708. exit (0);
  709. }
  710. int
  711. input_from_terminal_p ()
  712. {
  713. instream == stdin;
  714. }
  715. static void
  716. pwd_command (arg, from_tty)
  717. char *arg;
  718. int from_tty;
  719. {
  720. char buf[MAXPATHLEN];
  721. if (arg) error ("The \"pwd\" command does not take an argument: %s", arg);
  722. printf ("Working directory %s.\n", getwd (buf));
  723. }
  724. static void
  725. cd_command (dir, from_tty)
  726. char *dir;
  727. int from_tty;
  728. {
  729. if (dir == 0)
  730. error_no_arg ("new working directory");
  731. if (chdir (dir) < 0)
  732. perror_with_name (dir);
  733. if (from_tty)
  734. pwd_command ((char *) 0, 1);
  735. }
  736. /* Clean up on error during a "source" command.
  737. Close the file opened by the command
  738. and restore the previous input stream. */
  739. static void
  740. source_cleanup (stream)
  741. FILE *stream;
  742. {
  743. fclose (instream);
  744. instream = stream;
  745. }
  746. static void
  747. source_command (file)
  748. char *file;
  749. {
  750. FILE *stream;
  751. struct cleanup *cleanups;
  752. if (file == 0)
  753. error_no_arg ("file to read commands from");
  754. stream = fopen (file, "r");
  755. if (stream == 0)
  756. perror_with_name (file);
  757. cleanups = make_cleanup (source_cleanup, instream);
  758. instream = stream;
  759. command_loop ();
  760. do_cleanups (cleanups);
  761. }
  762. static void
  763. echo_command (text)
  764. char *text;
  765. {
  766. char *p = text;
  767. register int c;
  768. if (text)
  769. while (c = *p++)
  770. {
  771. if (c == '\\')
  772. {
  773. /* \ at end of argument is used after spaces
  774. so they won't be lost. */
  775. if (*p == 0)
  776. return;
  777. c = parse_escape (&p);
  778. if (c >= 0)
  779. fputc (c, stdout);
  780. }
  781. else
  782. fputc (c, stdout);
  783. }
  784. }
  785. static void
  786. dump_me_command ()
  787. {
  788. if (query ("Should GDB dump core? "))
  789. {
  790. signal (SIGQUIT, SIG_DFL);
  791. kill (getpid (), SIGQUIT);
  792. }
  793. }
  794. static void
  795. initialize_main ()
  796. {
  797. prompt = savestring ("(gdb) ", 6);
  798. /* Define the classes of commands.
  799. They will appear in the help list in the reverse of this order. */
  800. add_cmd ("obscure", class_obscure, 0, "Obscure features.", &cmdlist);
  801. add_cmd ("alias", class_alias, 0, "Aliases of other commands.", &cmdlist);
  802. add_cmd ("user", class_user, 0, "User-defined commands.\n\
  803. The commands in this class are those defined by the user.\n\
  804. Use the \"define\" command to define a command.", &cmdlist);
  805. add_cmd ("support", class_support, 0, "Support facilities.", &cmdlist);
  806. add_cmd ("status", class_info, 0, "Status inquiries.", &cmdlist);
  807. add_cmd ("files", class_files, 0, "Specifying and examining files.", &cmdlist);
  808. add_cmd ("breakpoints", class_breakpoint, 0, "Making program stop at certain points.", &cmdlist);
  809. add_cmd ("data", class_vars, 0, "Examining data.", &cmdlist);
  810. add_cmd ("stack", class_stack, 0, "Examining the stack.\n\
  811. The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
  812. counting from zero for the innermost (currently executing) frame.\n\n\
  813. At any time gdb identifies one frame as the \"selected\" frame.\n\
  814. Variable lookups are done with respect to the selected frame.\n\
  815. When the program being debugged stops, gdb selects the innermost frame.\n\
  816. The commands below can be used to select other frames by number or address.",
  817. &cmdlist);
  818. add_cmd ("running", class_run, 0, "Running the program.", &cmdlist);
  819. add_com ("pwd", class_files, pwd_command,
  820. "Print working directory. This is used for your program as well.");
  821. add_com ("cd", class_files, cd_command,
  822. "Set working directory to DIR for debugger and program being debugged.\n\
  823. The change does not take effect for the program being debugged\n\
  824. until the next time it is started.");
  825. add_com ("set-prompt", class_support, set_prompt_command,
  826. "Change gdb's prompt from the default of \"(gdb)\"");
  827. add_com ("echo", class_support, echo_command,
  828. "Print a constant string. Give string as argument.\n\
  829. C escape sequences may be used in the argument.\n\
  830. No newline is added at the end of the argument;\n\
  831. use \"\\n\" if you want a newline to be printed.\n\
  832. Since leading and trailing whitespace are ignored in command arguments,\n\
  833. if you want to print some you must use \"\\\" before leading whitespace\n\
  834. to be printed or after trailing whitespace.");
  835. add_com ("document", class_support, document_command,
  836. "Document a user-defined command.\n\
  837. Give command name as argument. Give documentation on following lines.\n\
  838. End with a line of just \"end\".");
  839. add_com ("define", class_support, define_command,
  840. "Define a new command name. Command name is argument.\n\
  841. Definition appears on following lines, one command per line.\n\
  842. End with a line of just \"end\".\n\
  843. Use the \"document\" command to give documentation for the new command.\n\
  844. Commands defined in this way do not take arguments.");
  845. add_com ("source", class_support, source_command,
  846. "Read commands from a file named FILE.\n\
  847. Note that the file \".gdbinit\" is read automatically in this way\n\
  848. when gdb is started.");
  849. add_com ("quit", class_support, quit_command, "Exit gdb.");
  850. add_com ("help", class_support, help_command, "Print list of commands.");
  851. add_com_alias ("q", "quit", class_support, 1);
  852. add_com_alias ("h", "help", class_support, 1);
  853. add_com ("dump-me", class_obscure, dump_me_command,
  854. "Get fatal error; make debugger dump its core.");
  855. add_prefix_cmd ("info", class_info, info_command,
  856. "Generic command for printing status.",
  857. &infolist, "info ", 0, &cmdlist);
  858. add_com_alias ("i", "info", class_info, 1);
  859. add_info ("copying", copying_info, "Conditions for redistributing copies of GDB.");
  860. add_info ("warranty", warranty_info, "Various kinds of warranty you do not have.");
  861. add_info ("version", version_info, "Report what version of GDB this is.");
  862. }