home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / MICROC.ZIP / CLS.C < prev    next >
C/C++ Source or Header  |  2000-06-15  |  4KB  |  158 lines

  1. /*
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.
  11.  
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program; if not, write to the Free Software
  14.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16.  
  17. /*
  18. ** This program clears the screen and optionally sets the color.
  19. ** Requires ANSI.SYS to be loaded.
  20. **
  21. ** author: James Hall
  22. */
  23.  
  24. #include <stdio.h>
  25. #include "freedos.h"
  26. #include "getopt.c"
  27.  
  28. int ansi_mode, attr_only, attr_flag, cls_fgcolor, cls_bgcolor, cls_attr;
  29.  
  30. char *ATTRS[] = 
  31. {"normal", "bright", NULL, NULL, "underlined", "blinking", NULL, "reversed", "invisible"};
  32.  
  33. char *COLORS[] = 
  34. {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"};
  35.  
  36. void usage (void);
  37.  
  38. void cls (void);
  39.  
  40. main (int argc, char **argv)
  41.  
  42. {
  43. int i, j;
  44.  
  45. ansi_mode = TRUE;
  46. attr_only = FALSE;
  47. attr_flag = FALSE;
  48. cls_attr = 0;
  49. cls_fgcolor = 7;
  50. cls_bgcolor = 0;
  51.  
  52. /* Scan the command line */
  53. while ((i = getopt (argc, argv, "ABRS", NULL)) != EOF)
  54.     {
  55.     switch (i)
  56.         {
  57.         case 'A':
  58.             ansi_mode = TRUE;
  59.             break;
  60.  
  61.         case 'B':
  62.             ansi_mode = FALSE;
  63.             break;
  64.  
  65.         /* We are only setting attributes */
  66.         case 'S':
  67.             attr_only = TRUE;
  68.             break;
  69.  
  70.         case 'R':
  71.             attr_flag = TRUE;
  72.             break;
  73.  
  74.         /* Usage */
  75.         default:
  76.             usage ();
  77.             break;
  78.         }
  79.     }
  80.  
  81. if ((argv[optind] == NULL) || (attr_flag == TRUE))
  82.     cls ();
  83.  
  84. i = optind;
  85.  
  86. /* See if the first arg is an attribute */
  87. for (j = 0; j < 10; j++)
  88.     if ((strstr (ATTRS[j], argv[i]) == ATTRS[j]) && (ATTRS[j] != NULL))
  89.         {
  90.         cls_attr = j;
  91.         attr_flag = TRUE;
  92.         i++;
  93.         break;
  94.         };
  95.  
  96.     /* See if current arg is a color */
  97. for (j = 0; j < 9; j++)
  98.     {
  99.     if (strstr (COLORS[j], argv[i]) == COLORS[j])
  100.         {
  101.         cls_fgcolor = j;
  102.         attr_flag = TRUE;
  103.         break;
  104.         }
  105.     if (j == 8)
  106.         usage ();
  107.     }
  108.  
  109. /* See if the next arg is a color and not NULL, skip "on" if found */
  110. i++; if (argv[i] != NULL)
  111.     for (j = 0; j < 9; j++)
  112.         {
  113.         if (!strcmp ("on", argv[i]))
  114.             i++;
  115.         if (strstr (COLORS[j], argv[i]) == COLORS[j])
  116.             {
  117.             cls_bgcolor = j;
  118.             attr_flag = TRUE;
  119.             break;
  120.             }
  121.         if (j == 8)
  122.             usage ();
  123.         }
  124.  
  125. cls ();
  126. }
  127.  
  128. void cls ()
  129.  
  130. {
  131. if (ansi_mode)
  132.     {
  133.     if (attr_flag)
  134.         printf ("\033[%d;%d;%dm", cls_attr, 
  135.             cls_fgcolor+30, cls_bgcolor+40);
  136.     if (!attr_only)
  137.         printf ("\033[2J");
  138.     }
  139. else
  140.     printf ("BIOS clear screen.\n");
  141.     exit (0);
  142. }
  143.  
  144. /**** Print usage statement */
  145. void usage(void)
  146. {
  147.     printp ("CLS","Clears the video monitor and sets colors.");
  148.     printc ("1995", "M. Toal, P. Mikalajunas, and J. Hall"); 
  149.     printu ("CLS", "[attribute] [color] [on] [color]");
  150.     printo ("A", "Forse ANSI mode");
  151.     printo ("B", "Force BIOS mode");
  152.     printo ("R", "Restore screen to normal white on black");
  153.     printo ("S", "Set attributes and colors only, do not clear screen.");
  154.     fprintf (stderr, "       Attributes: Normal Bright Underlined Blinking Reversed Invisible\n");
  155.     fprintf (stderr, "       Colors: Black Red Green Yellow Blue Magenta Cyan White\n");
  156.     exit (1);
  157. }
  158.