Add memtest support.
[syslinux-debian/hramrach.git] / com32 / modules / whichsys.c
blob777cb9e85164122a48ca71c25ca5dbf883d1667b
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2010 Gert Hulselmans - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * whichsys.c
16 * Detemine which command to execute, based on the Syslinux bootloader variant
17 * from which you run it.
19 * Usage: whichsys.c32 [-iso- command] [-pxe- command] [-sys- command]
20 * Examples: whichsys.c32 -iso- chain.c32 hd0 -sys- chain.c32 hd1 swap
21 * whichsys.c32 -iso- config iso.cfg -pxe- config pxe.cfg
25 #include <stdio.h>
26 #include <alloca.h>
27 #include <console.h>
28 #include <string.h>
29 #include <syslinux/boot.h>
30 #include "syslinux/config.h"
33 static struct syslinux_parameter {
34 char **arg[1];
35 bool option;
36 } isolinux, pxelinux, syslinux;
38 /* XXX: this really should be librarized */
39 static void boot_args(char **args)
41 int len = 0, a = 0;
42 char **pp;
43 const char *p;
44 char c, *q, *str;
46 for (pp = args; *pp; pp++)
47 len += strlen(*pp) + 1;
49 q = str = alloca(len);
50 for (pp = args; *pp; pp++) {
51 p = *pp;
52 while ((c = *p++))
53 *q++ = c;
54 *q++ = ' ';
55 a = 1;
57 q -= a;
58 *q = '\0';
60 if (!str[0])
61 syslinux_run_default();
62 else
63 syslinux_run_command(str);
66 static void usage(void)
68 static const char usage[] = "\
69 Usage: whichsys.c32 [-iso- command] [-pxe- command] [-sys- command]\n\
70 Examples: whichsys.c32 -iso- chain.c32 hd0 -sys- chain.c32 hd1 swap\n\
71 whichsys.c32 -iso- config iso.cfg -pxe- config pxe.cfg\n";
72 fprintf(stderr, usage);
75 int main(int argc, char *argv[])
77 const union syslinux_derivative_info *sdi;
79 int arg = 0;
81 /* If no argument got passed, let's show the usage */
82 if (argc == 1) {
83 usage();
84 return 0;
87 arg++;
89 while (arg < argc) {
90 if (!strcmp(argv[arg], "-iso-")) {
91 argv[arg] = NULL;
92 isolinux.arg[0] = &argv[arg + 1];
93 isolinux.option = true;
95 if (!strcmp(argv[arg], "-pxe-")) {
96 argv[arg] = NULL;
97 pxelinux.arg[0] = &argv[arg + 1];
98 pxelinux.option = true;
100 if (!strcmp(argv[arg], "-sys-")) {
101 argv[arg] = NULL;
102 syslinux.arg[0] = &argv[arg + 1];
103 syslinux.option = true;
105 arg++;
108 sdi = syslinux_derivative_info();
110 switch (sdi->c.filesystem) {
111 case SYSLINUX_FS_ISOLINUX:
112 isolinux.option ? boot_args(isolinux.arg[0]) : fprintf(stderr, "No command specified for ISOLINUX.\n\n"); usage();
113 break;
114 case SYSLINUX_FS_PXELINUX:
115 pxelinux.option ? boot_args(pxelinux.arg[0]) : fprintf(stderr, "No command specified for PXELINUX.\n\n"); usage();
116 break;
117 case SYSLINUX_FS_SYSLINUX:
118 syslinux.option ? boot_args(syslinux.arg[0]) : fprintf(stderr, "No command specified for SYSLINUX.\n\n"); usage();
119 break;
120 case SYSLINUX_FS_UNKNOWN:
121 default:
122 fprintf(stderr, "Unknown Syslinux filesystem\n\n");
125 return -1;