Guest

kbdmapcmd.c for syslinux (load keyboard map and run command)

By: mctpyt on May 20th, 2011  |  syntax: C  |  size: 2.73 KB  |  hits: 237  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /* ----------------------------------------------------------------------- *
  2.  *
  3.  *   Copyright 2011 Mariano Street
  4.  *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
  5.  *   Copyright 2009 Erwan Velu - All Rights Reserved
  6.  *
  7.  *   This file is based on kbdmap.c and ifcpu.c.
  8.  *
  9.  *   This program is free software: you can redistribute it and/or modify
  10.  *   it under the terms of the GNU General Public License as published by
  11.  *   the Free Software Foundation, either version 3 of the License, or
  12.  *   (at your option) any later version.
  13.  *
  14.  *   This program is distributed in the hope that it will be useful,
  15.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *   GNU General Public License for more details.
  18.  *
  19.  *   You should have received a copy of the GNU General Public License
  20.  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  *
  22.  * ----------------------------------------------------------------------- */
  23.  
  24. #include <alloca.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <console.h>
  28. #include <syslinux/boot.h>
  29. #include <syslinux/loadfile.h>
  30. #include <syslinux/keyboard.h>
  31.  
  32. static inline void error(const char *msg)
  33. {
  34.     fputs(msg, stderr);
  35. }
  36.  
  37. static inline int load_keymap(char *file)
  38. {
  39.     const struct syslinux_keyboard_map *const kmap = syslinux_keyboard_map();
  40.     size_t map_size;
  41.     void *kbdmap;
  42.    
  43.     if (kmap->version != 1) {
  44.         error("Syslinux core version mismatch\n");
  45.         return 1;
  46.     }
  47.    
  48.     if (loadfile(file, &kbdmap, &map_size)) {
  49.         error("Keyboard map file load error\n");
  50.         return 1;
  51.     }
  52.    
  53.     if (map_size != kmap->length) {
  54.         error("Keyboard map file format error\n");
  55.         return 1;
  56.     }
  57.    
  58.     memcpy(kmap->map, kbdmap, map_size);
  59.     return 0;
  60. }
  61.  
  62. inline static void boot_args(char **args)
  63. {
  64.     // Join strings from an array into a single string.
  65.     int len = 0, a = 0;
  66.     char **pp;
  67.     const char *p;
  68.     char c, *q, *str;
  69.    
  70.     for (pp = args; *pp; pp++)
  71.         len += strlen(*pp) + 1;
  72.    
  73.     q = str = alloca(len);
  74.     for (pp = args; *pp; pp++) {
  75.         p = *pp;
  76.         while (c = *p++)
  77.             *q++ = c;
  78.         *q++ = ' ';
  79.         a = 1;
  80.     }
  81.     q -= a;
  82.     *q = '\0';
  83.    
  84.     if (!str[0])
  85.         syslinux_run_default();
  86.     else
  87.         syslinux_run_command(str);
  88. }
  89.  
  90. int main(int argc, char *argv[])
  91. {
  92.     openconsole(&dev_null_r, &dev_stdcon_w);
  93.    
  94.     if (argc < 3) {
  95.         error("Usage: kbdmap <mapfile> <command> [<command parameters>]\n");
  96.         return 1;
  97.     }
  98.    
  99.     if (load_keymap(argv[1]) == 1)
  100.         return 1;
  101.    
  102.     boot_args(&argv[2]);
  103.     return 0;
  104. }