Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <curses.h>
21 #include <panel.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <string.h>
26 #include <err.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
33 #include "got_diff.h"
35 #ifndef MIN
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 #endif
39 #ifndef nitems
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 #endif
43 enum tog_view_id {
44 TOG_VIEW_LOG,
45 TOG_VIEW_DIFF,
46 TOG_VIEW_BLAME,
47 };
49 struct tog_cmd {
50 const char *name;
51 const struct got_error *(*cmd_main)(int, char *[]);
52 void (*cmd_usage)(void);
53 enum tog_view_id view;
54 const char *descr;
55 };
57 __dead void usage(void);
58 __dead void usage_log(void);
59 __dead void usage_diff(void);
60 __dead void usage_blame(void);
62 const struct got_error* cmd_log(int, char *[]);
63 const struct got_error* cmd_diff(int, char *[]);
64 const struct got_error* cmd_blame(int, char *[]);
66 struct tog_cmd tog_commands[] = {
67 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
68 "show repository history" },
69 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
70 "compare files and directories" },
71 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
72 "show line-by-line file history" },
73 };
75 /* globals */
76 WINDOW *tog_main_win;
77 PANEL *tog_main_panel;
78 static struct tog_log_view {
79 WINDOW *window;
80 PANEL *panel;
81 } tog_log_view;
83 __dead void
84 usage_log(void)
85 {
86 endwin();
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 getprogname());
89 exit(1);
90 }
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
94 {
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
99 char *line = NULL;
100 char *id_str = NULL;
101 const size_t id_display_len = 8;
102 const size_t author_display_len = 16;
103 size_t id_len, author_len, logmsg_len, avail;
104 int i, col;
106 err = got_object_id_str(&id_str, id);
107 if (err)
108 return err;
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
114 goto done;
116 logmsg = logmsg0;
117 while (*logmsg == '\n')
118 logmsg++;
119 newline = strchr(logmsg, '\n');
120 if (newline)
121 *newline = '\0';
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
127 goto done;
129 author = author0;
130 smallerthan = strchr(author, '<');
131 if (smallerthan)
132 *smallerthan = '\0';
133 else {
134 char *at = strchr(author, '@');
135 if (at)
136 *at = '\0';
138 author_len = strlen(author);
140 avail = COLS - 1;
141 line = calloc(avail + 1, sizeof(*line));
142 if (line == NULL) {
143 err = got_error_from_errno();
144 goto done;
147 col = 0;
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 if (col >= avail)
150 goto draw;
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
154 if (col >= avail)
155 goto draw;
156 line[col++] = ' ';
157 i++;
159 if (col >= avail)
160 goto draw;
161 line[col++] = ' ';
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 if (col >= avail)
164 goto draw;
165 line[col++] = author[i];
167 while (i < author_display_len) {
168 if (col >= avail)
169 goto draw;
170 line[col++] = ' ';
171 i++;
173 if (col >= avail)
174 goto draw;
175 line[col++] = ' ';
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
179 while (col < avail)
180 line[col++] = ' ';
181 draw:
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
184 done:
185 free(logmsg0);
186 free(author0);
187 free(line);
188 free(id_str);
189 return err;
191 struct commit_queue_entry {
192 TAILQ_ENTRY(commit_queue_entry) entry;
193 struct got_object_id *id;
194 struct got_commit_object *commit;
195 };
196 TAILQ_HEAD(commit_queue, commit_queue_entry);
198 static struct commit_queue_entry *
199 alloc_commit_queue_entry(struct got_commit_object *commit,
200 struct got_object_id *id)
202 struct commit_queue_entry *entry;
204 entry = calloc(1, sizeof(*entry));
205 if (entry == NULL)
206 return NULL;
208 entry->id = id;
209 entry->commit = commit;
210 return entry;
213 static void
214 pop_commit(struct commit_queue *commits)
216 struct commit_queue_entry *entry;
218 entry = TAILQ_FIRST(commits);
219 TAILQ_REMOVE(commits, entry, entry);
220 got_object_commit_close(entry->commit);
221 free(entry->id);
222 free(entry);
225 static void
226 free_commits(struct commit_queue *commits)
228 while (!TAILQ_EMPTY(commits))
229 pop_commit(commits);
233 static const struct got_error *
234 fetch_parent_commit(struct commit_queue_entry **pentry,
235 struct commit_queue_entry *entry, struct got_repository *repo)
237 const struct got_error *err = NULL;
238 struct got_object *obj = NULL;
239 struct got_commit_object *commit;
240 struct got_object_id *id;
241 struct got_parent_id *pid;
243 *pentry = NULL;
245 /* Follow the first parent (TODO: handle merge commits). */
246 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
247 if (pid == NULL)
248 return NULL;
249 err = got_object_open(&obj, repo, pid->id);
250 if (err)
251 return err;
252 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 err = got_error(GOT_ERR_OBJ_TYPE);
254 got_object_close(obj);
255 return err;
258 err = got_object_commit_open(&commit, repo, obj);
259 got_object_close(obj);
260 if (err)
261 return err;
263 id = got_object_id_dup(pid->id);
264 if (id == NULL) {
265 err = got_error_from_errno();
266 got_object_commit_close(commit);
267 return err;;
270 *pentry = alloc_commit_queue_entry(commit, id);
271 if (*pentry == NULL) {
272 err = got_error_from_errno();
273 got_object_commit_close(commit);
276 return err;;
279 static const struct got_error *
280 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
282 const struct got_error *err = NULL;
283 struct got_reference *head_ref;
285 *head_id = NULL;
287 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
288 if (err)
289 return err;
291 err = got_ref_resolve(head_id, repo, head_ref);
292 got_ref_close(head_ref);
293 if (err) {
294 *head_id = NULL;
295 return err;
298 return NULL;
301 static const struct got_error *
302 prepend_commits(int *ncommits, struct commit_queue *commits,
303 struct got_object_id *first_id, struct got_object_id *last_id,
304 int limit, struct got_repository *repo)
306 const struct got_error *err = NULL;
307 struct got_object *first_obj = NULL, *last_obj = NULL;
308 struct got_commit_object *commit = NULL;
309 struct got_object_id *id = NULL;
310 struct commit_queue_entry *entry, *old_head_entry;
312 *ncommits = 0;
314 err = got_object_open(&first_obj, repo, first_id);
315 if (err)
316 goto done;
317 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
319 goto done;
321 err = got_object_open(&last_obj, repo, last_id);
322 if (err)
323 goto done;
324 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
326 goto done;
329 err = got_object_commit_open(&commit, repo, first_obj);
330 if (err)
331 goto done;
333 id = got_object_id_dup(first_id);
334 if (id == NULL) {
335 err = got_error_from_errno();
336 goto done;
339 entry = alloc_commit_queue_entry(commit, id);
340 if (entry == NULL)
341 return got_error_from_errno();
343 old_head_entry = TAILQ_FIRST(commits);
344 if (old_head_entry)
345 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
346 else
347 TAILQ_INSERT_HEAD(commits, entry, entry);
349 *ncommits = 1;
351 /*
352 * Fetch parent commits.
353 * XXX If first and last commit aren't ancestrally related this loop
354 * we will keep iterating until a root commit is encountered.
355 */
356 while (1) {
357 struct commit_queue_entry *pentry;
359 err = fetch_parent_commit(&pentry, entry, repo);
360 if (err)
361 goto done;
362 if (pentry == NULL)
363 break;
365 /*
366 * Fill up to old HEAD commit if commit queue was not empty.
367 * We must not leave a gap in history.
368 */
369 if (old_head_entry &&
370 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
371 break;
373 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
374 (*ncommits)++;
375 if (*ncommits >= limit)
376 break;
378 /* Fill up to last requested commit if queue was empty. */
379 if (old_head_entry == NULL &&
380 got_object_id_cmp(pentry->id, last_id) == 0)
381 break;
383 entry = pentry;
386 done:
387 if (first_obj)
388 got_object_close(first_obj);
389 if (last_obj)
390 got_object_close(last_obj);
391 return err;
394 static const struct got_error *
395 fetch_commits(struct commit_queue_entry **start_entry,
396 struct got_object_id *start_id, struct commit_queue *commits,
397 int limit, struct got_repository *repo)
399 const struct got_error *err;
400 struct commit_queue_entry *entry;
401 int ncommits = 0;
402 struct got_object_id *head_id = NULL;
404 *start_entry = NULL;
406 err = get_head_commit_id(&head_id, repo);
407 if (err)
408 return err;
410 /* Prepend HEAD commit and all ancestors up to start commit. */
411 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
412 repo);
413 if (err)
414 return err;
416 if (got_object_id_cmp(head_id, start_id) == 0)
417 *start_entry = TAILQ_FIRST(commits);
418 else
419 *start_entry = TAILQ_LAST(commits, commit_queue);
421 if (ncommits >= limit)
422 return NULL;
424 /* Append more commits from start commit up to the requested limit. */
425 entry = TAILQ_LAST(commits, commit_queue);
426 while (entry && ncommits < limit) {
427 struct commit_queue_entry *pentry;
429 err = fetch_parent_commit(&pentry, entry, repo);
430 if (err)
431 break;
432 if (pentry)
433 TAILQ_INSERT_TAIL(commits, pentry, entry);
434 entry = pentry;
435 ncommits++;
438 if (err)
439 *start_entry = NULL;
440 return err;
443 static const struct got_error *
444 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
445 int selected, int limit)
447 const struct got_error *err = NULL;
448 struct commit_queue_entry *entry;
449 int ncommits = 0;
451 wclear(tog_log_view.window);
453 entry = first;
454 *last = first;
455 while (entry) {
456 if (ncommits == limit)
457 break;
458 if (ncommits == selected)
459 wstandout(tog_log_view.window);
460 err = draw_commit(entry->commit, entry->id);
461 if (ncommits == selected)
462 wstandend(tog_log_view.window);
463 if (err)
464 break;
465 ncommits++;
466 *last = entry;
467 entry = TAILQ_NEXT(entry, entry);
470 update_panels();
471 doupdate();
473 return err;
476 static void
477 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
478 struct commit_queue *commits)
480 struct commit_queue_entry *entry;
481 int nscrolled = 0;
483 entry = TAILQ_FIRST(commits);
484 if (*first_displayed_entry == entry)
485 return;
487 entry = *first_displayed_entry;
488 while (entry && nscrolled < n) {
489 entry = TAILQ_PREV(entry, commit_queue, entry);
490 if (entry) {
491 *first_displayed_entry = entry;
492 nscrolled++;
497 static const struct got_error *
498 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
499 struct commit_queue_entry *last_displayed_entry,
500 struct commit_queue *commits, struct got_repository *repo)
502 const struct got_error *err = NULL;
503 struct commit_queue_entry *entry;
504 int nscrolled = 0;
506 if (last_displayed_entry->commit->nparents == 0)
507 return NULL;
509 entry = *first_displayed_entry;
510 do {
511 struct commit_queue_entry *pentry;
513 pentry = TAILQ_NEXT(entry, entry);
514 if (pentry == NULL) {
515 err = fetch_parent_commit(&pentry, entry, repo);
516 if (err)
517 break;
518 if (pentry == NULL) {
519 *first_displayed_entry = entry;
520 return NULL;
522 TAILQ_INSERT_TAIL(commits, pentry, entry);
523 last_displayed_entry = pentry;
526 *first_displayed_entry = pentry;
527 entry = pentry;
529 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
530 err = fetch_parent_commit(&pentry, last_displayed_entry,
531 repo);
532 if (err)
533 break;
534 if (pentry) {
535 TAILQ_INSERT_TAIL(commits, pentry, entry);
536 last_displayed_entry = pentry;
539 } while (++nscrolled < n);
541 return NULL;
544 static int
545 num_parents(struct commit_queue_entry *entry)
547 int nparents = 0;
549 while (entry) {
550 entry = TAILQ_NEXT(entry, entry);
551 nparents++;
554 return nparents;
557 static const struct got_error *
558 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
560 const struct got_error *err = NULL;
561 struct got_object_id *id;
562 int ch, done = 0, selected = 0, nparents;
563 struct commit_queue commits;
564 struct commit_queue_entry *first_displayed_entry = NULL;
565 struct commit_queue_entry *last_displayed_entry = NULL;
567 id = got_object_id_dup(start_id);
568 if (id == NULL)
569 return got_error_from_errno();
571 if (tog_log_view.window == NULL) {
572 tog_log_view.window = newwin(0, 0, 0, 0);
573 if (tog_log_view.window == NULL)
574 return got_error_from_errno();
575 keypad(tog_log_view.window, TRUE);
577 if (tog_log_view.panel == NULL) {
578 tog_log_view.panel = new_panel(tog_log_view.window);
579 if (tog_log_view.panel == NULL)
580 return got_error_from_errno();
583 TAILQ_INIT(&commits);
584 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
585 if (err)
586 goto done;
587 while (!done) {
588 err = draw_commits(&last_displayed_entry, first_displayed_entry,
589 selected, LINES);
590 if (err)
591 goto done;
593 nodelay(stdscr, FALSE);
594 ch = wgetch(tog_log_view.window);
595 switch (ch) {
596 case ERR:
597 if (errno) {
598 err = got_error_from_errno();
599 goto done;
601 break;
602 case 'q':
603 done = 1;
604 break;
605 case 'k':
606 case KEY_UP:
607 if (selected > 0)
608 selected--;
609 if (selected > 0)
610 break;
611 scroll_up(&first_displayed_entry, 1, &commits);
612 break;
613 case KEY_PPAGE:
614 if (TAILQ_FIRST(&commits) ==
615 first_displayed_entry) {
616 selected = 0;
617 break;
619 scroll_up(&first_displayed_entry, LINES,
620 &commits);
621 break;
622 case 'j':
623 case KEY_DOWN:
624 nparents = num_parents(first_displayed_entry);
625 if (selected < LINES - 1 &&
626 selected < nparents - 1)
627 selected++;
628 if (selected < LINES - 1 &&
629 selected < nparents - 1)
630 break;
631 err = scroll_down(&first_displayed_entry, 1,
632 last_displayed_entry, &commits, repo);
633 if (err)
634 goto done;
635 break;
636 case KEY_NPAGE:
637 nparents = num_parents(first_displayed_entry);
638 if (nparents < LINES - 1 &&
639 selected < nparents - 1) {
640 selected = nparents - 1;
641 break;
643 err = scroll_down(&first_displayed_entry,
644 MIN(nparents, LINES), last_displayed_entry,
645 &commits, repo);
646 if (err)
647 goto done;
648 nparents = num_parents(first_displayed_entry);
649 if (selected > nparents)
650 selected = nparents - 1;
651 break;
652 case KEY_RESIZE:
653 if (selected > LINES)
654 selected = LINES - 1;
655 break;
656 default:
657 break;
659 nodelay(stdscr, TRUE);
661 done:
662 free_commits(&commits);
663 return err;
666 const struct got_error *
667 cmd_log(int argc, char *argv[])
669 const struct got_error *error;
670 struct got_repository *repo;
671 struct got_object_id *start_id = NULL;
672 char *repo_path = NULL;
673 char *start_commit = NULL;
674 int ch;
676 #ifndef PROFILE
677 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
678 err(1, "pledge");
679 #endif
681 while ((ch = getopt(argc, argv, "c:")) != -1) {
682 switch (ch) {
683 case 'c':
684 start_commit = optarg;
685 break;
686 default:
687 usage();
688 /* NOTREACHED */
692 argc -= optind;
693 argv += optind;
695 if (argc == 0) {
696 repo_path = getcwd(NULL, 0);
697 if (repo_path == NULL)
698 return got_error_from_errno();
699 } else if (argc == 1) {
700 repo_path = realpath(argv[0], NULL);
701 if (repo_path == NULL)
702 return got_error_from_errno();
703 } else
704 usage_log();
706 error = got_repo_open(&repo, repo_path);
707 free(repo_path);
708 if (error != NULL)
709 return error;
711 if (start_commit == NULL) {
712 error = get_head_commit_id(&start_id, repo);
713 if (error != NULL)
714 return error;
715 } else {
716 struct got_object *obj;
717 error = got_object_open_by_id_str(&obj, repo, start_commit);
718 if (error == NULL) {
719 start_id = got_object_get_id(obj);
720 if (start_id == NULL)
721 error = got_error_from_errno();
724 if (error != NULL)
725 return error;
726 error = show_log_view(start_id, repo);
727 free(start_id);
728 got_repo_close(repo);
729 return error;
732 __dead void
733 usage_diff(void)
735 endwin();
736 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
737 getprogname());
738 exit(1);
741 const struct got_error *
742 cmd_diff(int argc, char *argv[])
744 return got_error(GOT_ERR_NOT_IMPL);
747 __dead void
748 usage_blame(void)
750 endwin();
751 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
752 getprogname());
753 exit(1);
756 const struct got_error *
757 cmd_blame(int argc, char *argv[])
759 return got_error(GOT_ERR_NOT_IMPL);
762 static const struct got_error *
763 init_curses(void)
765 initscr();
766 cbreak();
767 noecho();
768 nonl();
769 intrflush(stdscr, FALSE);
770 keypad(stdscr, TRUE);
771 curs_set(0);
773 tog_main_win = newwin(0, 0, 0, 0);
774 if (tog_main_win == NULL)
775 return got_error_from_errno();
776 tog_main_panel = new_panel(tog_main_win);
777 if (tog_main_panel == NULL)
778 return got_error_from_errno();
780 return NULL;
783 __dead void
784 usage(void)
786 int i;
788 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
789 "Available commands:\n", getprogname());
790 for (i = 0; i < nitems(tog_commands); i++) {
791 struct tog_cmd *cmd = &tog_commands[i];
792 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
794 exit(1);
797 static char **
798 make_argv(const char *arg0, const char *arg1)
800 char **argv;
801 int argc = (arg1 == NULL ? 1 : 2);
803 argv = calloc(argc, sizeof(char *));
804 if (argv == NULL)
805 err(1, "calloc");
806 argv[0] = strdup(arg0);
807 if (argv[0] == NULL)
808 err(1, "calloc");
809 if (arg1) {
810 argv[1] = strdup(arg1);
811 if (argv[1] == NULL)
812 err(1, "calloc");
815 return argv;
818 int
819 main(int argc, char *argv[])
821 const struct got_error *error = NULL;
822 struct tog_cmd *cmd = NULL;
823 int ch, hflag = 0;
824 char **cmd_argv = NULL;
826 setlocale(LC_ALL, "");
828 while ((ch = getopt(argc, argv, "h")) != -1) {
829 switch (ch) {
830 case 'h':
831 hflag = 1;
832 break;
833 default:
834 usage();
835 /* NOTREACHED */
839 argc -= optind;
840 argv += optind;
841 optind = 0;
842 optreset = 1;
844 if (argc == 0) {
845 /* Build an argument vector which runs a default command. */
846 cmd = &tog_commands[0];
847 cmd_argv = make_argv(cmd->name, NULL);
848 argc = 1;
849 } else {
850 int i;
852 /* Did the user specific a command? */
853 for (i = 0; i < nitems(tog_commands); i++) {
854 if (strncmp(tog_commands[i].name, argv[0],
855 strlen(argv[0])) == 0) {
856 cmd = &tog_commands[i];
857 if (hflag)
858 tog_commands[i].cmd_usage();
859 break;
862 if (cmd == NULL) {
863 /* Did the user specify a repository? */
864 char *repo_path = realpath(argv[0], NULL);
865 if (repo_path) {
866 struct got_repository *repo;
867 error = got_repo_open(&repo, repo_path);
868 if (error == NULL)
869 got_repo_close(repo);
870 } else
871 error = got_error_from_errno();
872 if (error) {
873 fprintf(stderr, "%s: '%s' is neither a known "
874 "command nor a path to a repository\n",
875 getprogname(), argv[0]);
876 free(repo_path);
877 return 1;
879 cmd = &tog_commands[0];
880 cmd_argv = make_argv(cmd->name, repo_path);
881 argc = 2;
882 free(repo_path);
886 error = init_curses();
887 if (error) {
888 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
889 return 1;
892 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
893 if (error)
894 goto done;
895 done:
896 endwin();
897 free(cmd_argv);
898 if (error)
899 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
900 return 0;