wcmd: paging output

Eric Pouech eric.pouech at wanadoo.fr
Sun Feb 23 03:27:53 CST 2003


this patch allows dir /p command to take into account the actual screen 
height. it also moves the paging code out of directory handling. This 
could be used in future patchs for MORE implementation.

A+
-- 
Eric Pouech
-------------- next part --------------
Name:          wcmd_paged
ChangeLog:     
	- made support for paged output a generic feature in wcmd
	- rewrote DIR command accordingly
License:       X11
GenDate:       2003/02/23 09:22:55 UTC
ModifiedFiles: programs/wcmd/directory.c programs/wcmd/wcmd.h programs/wcmd/wcmdmain.c
AddedFiles:    
===================================================================
RCS file: /home/cvs/cvsroot/wine/wine/programs/wcmd/directory.c,v
retrieving revision 1.15
diff -u -u -r1.15 directory.c
--- programs/wcmd/directory.c	7 Jan 2003 23:11:12 -0000	1.15
+++ programs/wcmd/directory.c	23 Feb 2003 09:02:16 -0000
@@ -43,9 +43,8 @@
 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
 extern DWORD errorlevel;
 
-int file_total, dir_total, line_count, page_mode, recurse, wide, bare,
-    max_width;
-ULONGLONG byte_total;
+static int file_total, dir_total, recurse, wide, bare, max_width;
+static ULONGLONG byte_total;
 
 /*****************************************************************************
  * WCMD_directory
@@ -54,19 +53,18 @@
  *
  */
 
-void WCMD_directory () {
+void WCMD_directory (void) {
 
 char path[MAX_PATH], drive[8];
-int status;
+int status, paged_mode;
 ULARGE_INTEGER avail, total, free;
 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
 
-  line_count = 5;
   byte_total = 0;
   file_total = dir_total = 0;
 
   /* Handle args */
-  page_mode = (strstr(quals, "/P") != NULL);
+  paged_mode = (strstr(quals, "/P") != NULL);
   recurse = (strstr(quals, "/S") != NULL);
   wide    = (strstr(quals, "/W") != NULL);
   bare    = (strstr(quals, "/B") != NULL);
@@ -78,11 +76,15 @@
      GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
      max_width = consoleInfo.dwSize.X;
   }
+  if (paged_mode) {
+     WCMD_enter_paged_mode();
+  }
 
   if (param1[0] == '\0') strcpy (param1, ".");
   status = GetFullPathName (param1, sizeof(path), path, NULL);
   if (!status) {
     WCMD_print_error();
+    if (paged_mode) WCMD_leave_paged_mode();
     return;
   }
   lstrcpyn (drive, path, 3);
@@ -90,6 +92,7 @@
   if (!bare) {
      status = WCMD_volume (0, drive);
      if (!status) {
+         if (paged_mode) WCMD_leave_paged_mode();
        return;
      }
   }
@@ -108,6 +111,7 @@
        WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
      }
   }
+  if (paged_mode) WCMD_leave_paged_mode();
 }
 
 /*****************************************************************************
@@ -128,12 +132,11 @@
 char mem_err[] = "Memory Allocation Error";
 char *p;
 char real_path[MAX_PATH];
-DWORD count;
 WIN32_FIND_DATA *fd;
 FILETIME ft;
 SYSTEMTIME st;
 HANDLE hff;
-int status, dir_count, file_count, entry_count, i, widest, linesout, cur_width, tmp_width;
+int status, dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
 ULARGE_INTEGER byte_count, file_size;
 
   dir_count = 0;
@@ -141,7 +144,6 @@
   entry_count = 0;
   byte_count.QuadPart = 0;
   widest = 0;
-  linesout = 0;
   cur_width = 0;
 
 /*
@@ -202,14 +204,6 @@
   if (!bare) {
      if (level != 0) WCMD_output ("\n\n");
      WCMD_output ("Directory of %s\n\n", real_path);
-     if (page_mode) {
-       line_count += 2;
-       if (line_count > 23) {
-         line_count = 0;
-         WCMD_output (anykey);
-         ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
-       }
-     }
   }
 
   for (i=0; i<entry_count; i++) {
@@ -224,9 +218,7 @@
 
       tmp_width = cur_width;
       if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
-          WCMD_output ("[");
-          WCMD_output ("%s", (fd+i)->cFileName);
-          WCMD_output ("]");
+          WCMD_output ("[%s]", (fd+i)->cFileName);
           dir_count++;
           tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
       } else {
@@ -247,7 +239,6 @@
       if ((cur_width + widest) > max_width) {
           WCMD_output ("\n");
           cur_width = 0;
-          linesout++;
       } else {
           WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
       }
@@ -258,12 +249,10 @@
       if (!bare) {
          WCMD_output ("%10s  %8s  <DIR>         %s\n",
       	     datestring, timestring, (fd+i)->cFileName);
-         linesout++;
       } else {
          if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
                (strcmp((fd+i)->cFileName, "..") == 0))) {
             WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
-            linesout++;
          }
       }
     }
@@ -281,32 +270,14 @@
          WCMD_output ("%10s  %8s    %10s  %s\n",
      	     datestring, timestring,
 	         WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
-         linesout++;
       } else {
          WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
-         linesout++;
-      }
-    }
-    if (page_mode) {
-      line_count = line_count + linesout;
-      linesout = 0;
-      if (line_count > 23) {
-        line_count = 0;
-        WCMD_output (anykey);
-        ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
       }
     }
   }
 
   if (wide && cur_width>0) {
       WCMD_output ("\n");
-      if (page_mode) {
-        if (++line_count > 23) {
-          line_count = 0;
-          WCMD_output (anykey);
-          ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
-        }
-      }
   }
 
   if (!bare) {
@@ -316,13 +287,6 @@
      else {
        WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
      }
-     if (page_mode) {
-       if (++line_count > 23) {
-         line_count = 0;
-         WCMD_output (anykey);
-         ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
-       }
-     }
   }
   byte_total = byte_total + byte_count.QuadPart;
   file_total = file_total + file_count;
@@ -331,13 +295,6 @@
   if (!bare) {
      if (dir_count == 1) WCMD_output ("1 directory         ");
      else WCMD_output ("%8d directories", dir_count);
-     if (page_mode) {
-       if (++line_count > 23) {
-         line_count = 0;
-         WCMD_output (anykey);
-         ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
-       }
-     }
   }
   for (i=0; i<entry_count; i++) {
     if ((recurse) &&
Index: programs/wcmd/wcmd.h
===================================================================
RCS file: /home/cvs/cvsroot/wine/wine/programs/wcmd/wcmd.h,v
retrieving revision 1.9
diff -u -u -r1.9 wcmd.h
--- programs/wcmd/wcmd.h	11 Feb 2003 22:01:11 -0000	1.9
+++ programs/wcmd/wcmd.h	23 Feb 2003 09:02:51 -0000
@@ -40,10 +40,12 @@
 void WCMD_delete (int recurse);
 void WCMD_directory (void);
 void WCMD_echo (char *);
+void WCMD_enter_paged_mode(void);
 void WCMD_for (char *);
 void WCMD_give_help (char *command);
 void WCMD_goto (void);
 void WCMD_if (char *);
+void WCMD_leave_paged_mode(void);
 void WCMD_move (void);
 void WCMD_output (char *format, ...);
 void WCMD_output_asis (char *message);
@@ -52,7 +54,7 @@
 void WCMD_pipe (char *command);
 void WCMD_print_error (void);
 void WCMD_process_command (char *command);
-int WCMD_read_console (char *string, int str_len);
+int  WCMD_read_console (char *string, int str_len);
 void WCMD_remove_dir (void);
 void WCMD_rename (void);
 void WCMD_run_program (char *command);
Index: programs/wcmd/wcmdmain.c
===================================================================
RCS file: /home/cvs/cvsroot/wine/wine/programs/wcmd/wcmdmain.c,v
retrieving revision 1.22
diff -u -u -r1.22 wcmdmain.c
--- programs/wcmd/wcmdmain.c	11 Feb 2003 22:01:11 -0000	1.22
+++ programs/wcmd/wcmdmain.c	23 Feb 2003 09:00:44 -0000
@@ -587,12 +589,31 @@
 
 va_list ap;
 char string[1024];
-DWORD count;
 
   va_start(ap,format);
   vsprintf (string, format, ap);
-  WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
   va_end(ap);
+  WCMD_output_asis(string);
+}
+
+
+static int line_count;
+static int max_height;
+static BOOL paged_mode;
+
+void WCMD_enter_paged_mode(void)
+{
+CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
+
+  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
+  max_height = consoleInfo.dwSize.Y;
+  paged_mode = TRUE;
+  line_count = 5; /* keep 5 lines from previous output */
+}
+
+void WCMD_leave_paged_mode(void)
+{
+  paged_mode = FALSE;
 }
 
 /*******************************************************************
@@ -602,9 +623,26 @@
 
 void WCMD_output_asis (char *message) {
   DWORD count;
-  WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
-}
+  char* ptr;
+  char string[1024];
 
+  if (paged_mode) {
+    do {
+      if ((ptr = strchr(message, '\n')) != NULL) ptr++;
+      WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, 
+                 (ptr) ? ptr - message : lstrlen(message), &count, NULL);
+      if (ptr) {
+        if (++line_count >= max_height - 1) {
+          line_count = 0;
+          WCMD_output_asis (anykey);
+          ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
+        }
+      }
+    } while ((message = ptr) != NULL);
+  } else {
+      WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
+  }
+}
 
 
 /***************************************************************************


More information about the wine-patches mailing list