added "more" command
[unix-history] / usr / src / usr.bin / window / cmd2.c
index 8a36262..41600d5 100644 (file)
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)cmd2.c      1.7 83/07/28";
+static char sccsid[] = "@(#)cmd2.c     3.33 %G%";
 #endif
 
 #endif
 
-#include "defs.h"
-
-struct ww *getwin();
-struct ww *openwin();
-char *strtime();
-
-dohelp()
-{
-       register struct ww *w;
+/*
+ * Copyright (c) 1983 Regents of the University of California,
+ * All rights reserved.  Redistribution permitted subject to
+ * the terms of the Berkeley Software License Agreement.
+ */
 
 
-       if ((w = openwin(wwncol - 1, "Help")) == 0) {
-               wwputs("Can't open help window.  ", cmdwin);
-               return;
-       }
-       wwprintf(w, "The escape character is ^P, which gets you into command mode.\r\n");
-       wwprintf(w, "The commands are:\r\n");
-       wwprintf(w, "[1-9]   Select window [1-9] and exit command mode.\r\n");
-       wwprintf(w, "%%[1-9]  Select window [1-9].\r\n");
-       wwprintf(w, "c[1-9]  Close window [1-9].\r\n");
-       wwprintf(w, "C       Close all windows.\r\n");
-       wwprintf(w, "S       Show all windows in sequence.\r\n");
-       wwprintf(w, "R       Force refresh after every newline (current window only).\r\n");
-       wwprintf(w, "r       Don't refresh every line.\r\n");
-       wwprintf(w, "w       Open a new window.\r\n");
-       wwprintf(w, "^U      Scroll up.\r\n");
-       wwprintf(w, "^D      Scroll down.\r\n");
-       wwprintf(w, "[hjkl]  Move cursor [left, down, up, right].\r\n");
-       /*
-       wwprintf(w, "s       Print IO statistics.\r\n");
-       wwprintf(w, "t       Print resource usage of this program.\r\n");
-       wwprintf(w, "T       Print resource usage of children.\r\n");
-       */
-       wwprintf(w, "escape  Exit command mode.\r\n");
-       wwprintf(w, "^L      Redraw screen.\r\n");
-       wwprintf(w, "^Z      Suspend.\r\n");
-       wwprintf(w, ".       Quit.\r\n");
-       waitnl(w);
-       closewin(w);
-}
+#include "defs.h"
 
 
-dotime(flag)
+char *help_shortcmd[] = {
+       "#       Select window # and return to conversation mode",
+       "%#      Select window # but stay in command mode",
+       "escape  Return to conversation mode without changing window",
+       "^^      Return to conversation mode and change to previous window",
+       "c#      Close window #",
+       "w       Open a new window",
+       "m#      Move window #",
+       "M#      Move window # to its previous position",
+       "s#      Change the size of window #",
+       "S#      Change window # to its previous size",
+       "^Y      Scroll up one line",
+       "^E      Scroll down one line",
+       "^U      Scroll up half a window",
+       "^D      Scroll down half a window",
+       "^B      Scroll up a full window",
+       "^F      Scroll down a full window",
+       "h       Move cursor left",
+       "j       Move cursor down",
+       "k       Move cursor up",
+       "l       Move cursor right",
+       "^S      Stop output in current window",
+       "^Q      Restart output in current window",
+       "^L      Redraw screen",
+       "^Z      Suspend",
+       "q       Quit",
+       ":       Enter a long command",
+       0
+};
+char *help_longcmd[] = {
+       ":alias name string ...  Make `name' an alias for `string ...'",
+       ":alias                  Show all aliases",
+       ":close # ...            Close windows",
+       ":close all              Close all windows",
+       ":cursor modes           Set the cursor modes",
+       ":echo # string ...      Print `string ...' in window #",
+       ":escape c               Set escape character to `c'",
+       ":foreground # flag      Make # a foreground window, if `flag' is true",
+       ":label # string         Set label of window # to `string'",
+       ":list                   List all open windows",
+       ":nline lines            Set default window buffer size to `lines'",
+       ":select #               Select window #",
+       ":shell string ...       Set default shell program to `string ...'",
+       ":source filename        Execute commands in `filename'",
+       ":terse flag             Set terse mode",
+       ":unalias name           Undefine `name' as an alias",
+       ":unset variable         Deallocate `variable'",
+       ":variable               List all variables",
+       ":window row col nrow ncol [nline label pty frame shell]",
+       "                        Open a window at `row', `col' of size `nrow', `ncol',",
+       "                        with `nline' lines in the buffer, and `label'",
+       ":write # string ...     Write `string ...' to window # as input",
+       0
+};
+
+c_help()
 {
        register struct ww *w;
 {
        register struct ww *w;
-       struct rusage rusage;
-       struct timeval timeval;
 
 
-       if ((w = openwin(8, "Timing and Resource Usage")) == 0) {
-               wwputs("Can't open time window.  ", cmdwin);
+       if ((w = openiwin(wwnrow - 3, "Help")) == 0) {
+               error("Can't open help window: %s.", wwerror());
                return;
        }
                return;
        }
-
-       gettimeofday(&timeval, &timezone);
-       timeval.tv_sec -= starttime.tv_sec;
-       if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
-               timeval.tv_sec--;
-               timeval.tv_usec += 1000000;
-       }
-       getrusage(flag, &rusage);
-
-       wwprintf(w, "time\t\tutime\t\tstime\t\tmaxrss\tixrss\tidrss\tisrss\r\n");
-       wwprintf(w, "%-16s", strtime(&timeval));
-       wwprintf(w, "%-16s", strtime(&rusage.ru_utime));
-       wwprintf(w, "%-16s", strtime(&rusage.ru_stime));
-       wwprintf(w, "%D\t%D\t%D\t%D\r\n",
-               rusage.ru_maxrss, rusage.ru_ixrss,
-               rusage.ru_idrss, rusage.ru_isrss);
-       wwprintf(w, "minflt\tmajflt\tnswap\tinblk\toublk\tmsgsnd\tmsgrcv\tnsigs\tnvcsw\tnivcsw\r\n");
-       wwprintf(w, "%D\%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\r\n",
-               rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
-               rusage.ru_inblock, rusage.ru_oublock,
-               rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
-               rusage.ru_nvcsw, rusage.ru_nivcsw);
-
-       waitnl(w);
-       closewin(w);
+       wwprintf(w, "The escape character is %c.\n", escapec);
+       wwprintf(w, "(# represents one of the digits from 1 to 9.)\n\n");
+       if (help_print(w, "Short commands", help_shortcmd) >= 0)
+               (void) help_print(w, "Long commands", help_longcmd);
+       closeiwin(w);
 }
 
 }
 
-char *
-strtime(t)
-register struct timeval *t;
+help_print(w, name, list)
+register struct ww *w;
+char *name;
+register char **list;
 {
 {
-       char fill = 0;
-       static char buf[20];
-       register char *p = buf;
-
-       if (t->tv_sec > 60*60) {
-               sprintf(p, "%D:", t->tv_sec / (60*60));
-               while (*p++)
-                       ;
-               p--;
-               t->tv_sec %= 60*60;
-               fill++;
-       }
-       if (t->tv_sec > 60) {
-               sprintf(p, fill ? "%02D:" : "%D:", t->tv_sec / 60);
-               while (*p++)
-                       ;
-               p--;
-               t->tv_sec %= 60;
-               fill++;
-       }
-       sprintf(p, fill ? "%02D.%02d" : "%D.%02D",
-               t->tv_sec, t->tv_usec / 10000);
-       return buf;
+       wwprintf(w, "%s:\n\n", name);
+       while (*list)
+               switch (more(w, 0)) {
+               case 0:
+                       wwputs(*list++, w);
+                       wwputc('\n', w);
+                       break;
+               case 1:
+                       wwprintf(w, "%s: (continued)\n\n", name);
+                       break;
+               case 2:
+                       return -1;
+               }
+       return more(w, 1) == 2 ? -1 : 0;
 }
 
 }
 
-dostat()
+c_quit()
 {
 {
-       register struct ww *w;
+       char oldterse = terse;
 
 
-       if ((w = openwin(6, "IO Statics")) == 0) {
-               wwputs("Can't open statistics window.  ", cmdwin);
-               return;
-       }
-       wwprintf(w, "nread\tnreadz\tnreade\tnreadc\tnwrite\tnwritec\r\n");
-       wwprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\r\n",
-               nread, nreadz, nreade, nreadc, wwnwrite, wwnwritec);
-       waitnl(w);
-       closewin(w);
-}
-
-doquit()
-{
+       setterse(0);
        wwputs("Really quit [yn]? ", cmdwin);
        wwputs("Really quit [yn]? ", cmdwin);
-       wwsetcursor(WCurRow(cmdwin->ww_win), WCurCol(cmdwin->ww_win));
-       while (bpeekc() < 0)
-               bread();
-       if (bgetc() == 'y') {
+       wwcurtowin(cmdwin);
+       while (wwpeekc() < 0)
+               wwiomux();
+       if (wwgetc() == 'y') {
                wwputs("Yes", cmdwin);
                quit++;
        } else
                wwputs("Yes", cmdwin);
                quit++;
        } else
-               wwputs("\r\n", cmdwin);
-}
-
-struct ww *
-openwin(nrow, label)
-char *label;
-{
-       register struct ww *w;
-
-       if ((w = wwopen(WW_NONE, 0, nrow, wwncol, 1, 0)) == 0)
-               return 0;
-       wwframe(w);
-       wwlabel(w, (wwncol - strlen(label)) / 2 + 1, label, WINVERSE);
-       wwsetcurwin(w);
-       return w;
-}
-
-waitnl(w)
-register struct ww *w;
-{
-       wwsetcurwin(w);
-       wwprintf(w, "\r\nType return to continue: ");
-       wwsetcursor(WCurRow(w->ww_win), WCurCol(w->ww_win));
-       while (bgetc() < 0)
-               bread();
-}
-
-closewin(w)
-register struct ww *w;
-{
-       wwclose(w);
-       wwsetcurwin(cmdwin);
+               wwputc('\n', cmdwin);
+       setterse(!quit && oldterse);
 }
 }