/*
   <p><i>CODES.C</i> - display input characters</p>

   <kbd>
   Copyright (C) 2006  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>

   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#define VERSION   "1.2"
#define COPYRIGHT "Martin Gregorie, 2009"

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "keyreader.h"

extern int errno;

int main(int argc, char *argv[])
{
   char	c;
   char	cntrl_string[16];
   int	i;

   for (i = 1; i < argc; i++)
   {
      if (strcmp(argv[i], "-?") == 0
          || strcmp(argv[i], "-h") == 0
          || strcmp(argv[i], "--help") == 0)
      {
         printf("codes %s. (c) %s\n\n", VERSION, COPYRIGHT);
         printf("Syntax: codes -?\n");
         printf("Function: Display input characters\n");
         printf("          as char, numeric and hex\n");
         printf("          ^D terminates the program\n");
         printf("Options:  none\n");
         exit(0);
      }
      else
         error("codes", "unknown argument(s) supplied", "");
   }

   /*
      Initialise single key reading.
   */
   krinit();
   
   /*
      Now handle input characters, reading them one at a time and
      outputting each as ASCII, decimal and hex.
   */
   printf("Input ctrl/D to quit\n\n");
   while ((c = krgetchar()) >= 0)
   {
      /*
         This is the Raspberry Pi 0xff is the response to Ctrl-D
      */ 
      if (c == 0xff)
         break;
         
      /*
         Show what we just read in some detail
      */
      if (c == 0x07)
      {
         strcpy(cntrl_string, "(Bell)");
      }
      else
      if (c == 0x08)
      {
         strcpy(cntrl_string, "(Backspace)");
      }
      else
      if (c == 0x09)
      {
         strcpy(cntrl_string, "(TAB)");
      }
      else
      if (c == 0x0a)
      {
         strcpy(cntrl_string, "(LF)");
      }
      else
      if (c == 0x0d)
      {
         strcpy(cntrl_string, "(CR)");
      }
      else
      if (c == 0x1b)
      {
         strcpy(cntrl_string, "(Esc)");
      }
      else
      if (c == 0x7f)
      {
         strcpy(cntrl_string, "(DEL)");
      }
      else
      if (iscntrl(c))
      {
         sprintf(cntrl_string, "(ctrl-%c)", 'a' + c - 1);
      }
      else
      {
         strcpy(cntrl_string, "");
      }

      printf("%c = %d  0x%02x  %s\n", 
             (isascii(c) && isprint(c) ? c : '.'),
             c,
             c,
             cntrl_string);
   }

   if (c == -1 || c == 0xff)
   {
      printf("codes: run ended\n");
   }
   else
   {
      logmsg("codes: %s\n", strerror(errno));
   }

   krend();
   exit(errno);
}

