2N/A/* grub-editenv.c - tool to edit environment block. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <config.h>
2N/A#include <grub/types.h>
2N/A#include <grub/emu/misc.h>
2N/A#include <grub/util/misc.h>
2N/A#include <grub/lib/envblk.h>
2N/A#include <grub/i18n.h>
2N/A
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <argp.h>
2N/A
2N/A#include "progname.h"
2N/A
2N/A#define DEFAULT_ENVBLK_SIZE 1024
2N/A#define DEFAULT_ENVBLK_PATH DEFAULT_DIRECTORY "/" GRUB_ENVBLK_DEFCFG
2N/A
2N/Astatic struct argp_option options[] = {
2N/A {0, 0, 0, OPTION_DOC, N_("Commands:"), 1},
2N/A {"create", 0, 0, OPTION_DOC|OPTION_NO_USAGE,
2N/A N_("Create a blank environment block file."), 0},
2N/A {"list", 0, 0, OPTION_DOC|OPTION_NO_USAGE,
2N/A N_("List the current variables."), 0},
2N/A {"set [name=value ...]", 0, 0, OPTION_DOC|OPTION_NO_USAGE,
2N/A N_("Set variables."), 0},
2N/A {"unset [name ....]", 0, 0, OPTION_DOC|OPTION_NO_USAGE,
2N/A N_("Delete variables."), 0},
2N/A
2N/A {0, 0, 0, OPTION_DOC, N_("Options:"), -1},
2N/A {"verbose", 'v', 0, 0, N_("Print verbose messages."), 0},
2N/A
2N/A { 0, 0, 0, 0, 0, 0 }
2N/A};
2N/A
2N/A/* Print the version information. */
2N/Astatic void
2N/Aprint_version (FILE *stream, struct argp_state *state)
2N/A{
2N/A fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
2N/A}
2N/Avoid (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
2N/A
2N/A/* Set the bug report address */
2N/Aconst char *argp_program_bug_address = "<"PACKAGE_BUGREPORT">";
2N/A
2N/Aerror_t argp_parser (int key, char *arg, struct argp_state *state)
2N/A{
2N/A switch (key)
2N/A {
2N/A case 'v':
2N/A verbosity++;
2N/A break;
2N/A
2N/A case ARGP_KEY_NO_ARGS:
2N/A fprintf (stderr, "%s",
2N/A _("You need to specify at least one command.\n"));
2N/A argp_usage (state);
2N/A break;
2N/A
2N/A default:
2N/A return ARGP_ERR_UNKNOWN;
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic char *
2N/Ahelp_filter (int key, const char *text, void *input __attribute__ ((unused)))
2N/A{
2N/A switch (key)
2N/A {
2N/A case ARGP_KEY_HELP_POST_DOC:
2N/A return xasprintf(text, DEFAULT_ENVBLK_PATH);
2N/A
2N/A default:
2N/A return (char *) text;
2N/A }
2N/A}
2N/A
2N/Astruct argp argp = {
2N/A options, argp_parser, N_("FILENAME COMMAND"),
2N/A "\n"N_("\
2N/ATool to edit environment block.")
2N/A"\v"N_("\
2N/AIf FILENAME is '-', the default value %s is used."),
2N/A NULL, help_filter, NULL
2N/A};
2N/A
2N/Astatic void
2N/Acreate_envblk_file (const char *name)
2N/A{
2N/A FILE *fp;
2N/A char *buf;
2N/A char *namenew;
2N/A
2N/A buf = malloc (DEFAULT_ENVBLK_SIZE);
2N/A if (! buf)
2N/A grub_util_error (_("out of memory"));
2N/A
2N/A namenew = xasprintf ("%s.new", name);
2N/A fp = fopen (namenew, "wb");
2N/A if (! fp)
2N/A grub_util_error (_("cannot open the file %s"), namenew);
2N/A
2N/A memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
2N/A memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
2N/A DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
2N/A
2N/A if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
2N/A grub_util_error (_("cannot write to the file %s"), namenew);
2N/A
2N/A fsync (fileno (fp));
2N/A free (buf);
2N/A fclose (fp);
2N/A
2N/A if (rename (namenew, name) < 0)
2N/A grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
2N/A free (namenew);
2N/A}
2N/A
2N/Astatic grub_envblk_t
2N/Aopen_envblk_file (const char *name)
2N/A{
2N/A FILE *fp;
2N/A char *buf;
2N/A size_t size;
2N/A grub_envblk_t envblk;
2N/A
2N/A fp = fopen (name, "rb");
2N/A if (! fp)
2N/A {
2N/A /* Create the file implicitly. */
2N/A create_envblk_file (name);
2N/A fp = fopen (name, "rb");
2N/A if (! fp)
2N/A grub_util_error (_("cannot open the file %s"), name);
2N/A }
2N/A
2N/A if (fseek (fp, 0, SEEK_END) < 0)
2N/A grub_util_error (_("cannot seek the file %s"), name);
2N/A
2N/A size = (size_t) ftell (fp);
2N/A
2N/A if (fseek (fp, 0, SEEK_SET) < 0)
2N/A grub_util_error (_("cannot seek the file %s"), name);
2N/A
2N/A buf = malloc (size);
2N/A if (! buf)
2N/A grub_util_error (_("out of memory"));
2N/A
2N/A if (fread (buf, 1, size, fp) != size)
2N/A grub_util_error (_("cannot read the file %s"), name);
2N/A
2N/A fclose (fp);
2N/A
2N/A envblk = grub_envblk_open (buf, size);
2N/A if (! envblk)
2N/A grub_util_error (_("invalid environment block"));
2N/A
2N/A return envblk;
2N/A}
2N/A
2N/Astatic void
2N/Alist_variables (const char *name)
2N/A{
2N/A grub_envblk_t envblk;
2N/A
2N/A auto int print_var (const char *name, const char *value);
2N/A int print_var (const char *name, const char *value)
2N/A {
2N/A printf ("%s=%s\n", name, value);
2N/A return 0;
2N/A }
2N/A
2N/A envblk = open_envblk_file (name);
2N/A grub_envblk_iterate (envblk, print_var);
2N/A grub_envblk_close (envblk);
2N/A}
2N/A
2N/Astatic void
2N/Awrite_envblk (const char *name, grub_envblk_t envblk)
2N/A{
2N/A FILE *fp;
2N/A
2N/A fp = fopen (name, "wb");
2N/A if (! fp)
2N/A grub_util_error (_("cannot open the file %s"), name);
2N/A
2N/A if (fwrite (grub_envblk_buffer (envblk), 1, grub_envblk_size (envblk), fp)
2N/A != grub_envblk_size (envblk))
2N/A grub_util_error (_("cannot write to the file %s"), name);
2N/A
2N/A fsync (fileno (fp));
2N/A fclose (fp);
2N/A}
2N/A
2N/Astatic void
2N/Aset_variables (const char *name, int argc, char *argv[])
2N/A{
2N/A grub_envblk_t envblk;
2N/A
2N/A envblk = open_envblk_file (name);
2N/A while (argc)
2N/A {
2N/A char *p;
2N/A
2N/A p = strchr (argv[0], '=');
2N/A if (! p)
2N/A grub_util_error (_("invalid parameter %s"), argv[0]);
2N/A
2N/A *(p++) = 0;
2N/A
2N/A if (! grub_envblk_set (envblk, argv[0], p))
2N/A grub_util_error (_("environment block too small"));
2N/A
2N/A argc--;
2N/A argv++;
2N/A }
2N/A
2N/A write_envblk (name, envblk);
2N/A grub_envblk_close (envblk);
2N/A}
2N/A
2N/Astatic void
2N/Aunset_variables (const char *name, int argc, char *argv[])
2N/A{
2N/A grub_envblk_t envblk;
2N/A
2N/A envblk = open_envblk_file (name);
2N/A while (argc)
2N/A {
2N/A grub_envblk_delete (envblk, argv[0]);
2N/A
2N/A argc--;
2N/A argv++;
2N/A }
2N/A
2N/A write_envblk (name, envblk);
2N/A grub_envblk_close (envblk);
2N/A}
2N/A
2N/Aint
2N/Amain (int argc, char *argv[])
2N/A{
2N/A char *filename;
2N/A char *command;
2N/A int index, arg_count;
2N/A
2N/A set_program_name (argv[0]);
2N/A
2N/A grub_util_init_nls ();
2N/A
2N/A /* Parse our arguments */
2N/A if (argp_parse (&argp, argc, argv, 0, &index, 0) != 0)
2N/A {
2N/A fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
2N/A exit(1);
2N/A }
2N/A
2N/A arg_count = argc - index;
2N/A
2N/A if (arg_count == 1)
2N/A {
2N/A filename = DEFAULT_ENVBLK_PATH;
2N/A command = argv[index++];
2N/A }
2N/A else
2N/A {
2N/A filename = argv[index++];
2N/A if (strcmp (filename, "-") == 0)
2N/A filename = DEFAULT_ENVBLK_PATH;
2N/A command = argv[index++];
2N/A }
2N/A
2N/A if (strcmp (command, "create") == 0)
2N/A create_envblk_file (filename);
2N/A else if (strcmp (command, "list") == 0)
2N/A list_variables (filename);
2N/A else if (strcmp (command, "set") == 0)
2N/A set_variables (filename, argc - index, argv + index);
2N/A else if (strcmp (command, "unset") == 0)
2N/A unset_variables (filename, argc - index, argv + index);
2N/A else
2N/A {
2N/A char *program = xstrdup(program_name);
2N/A fprintf (stderr, _("Unknown command `%s'.\n"), command);
2N/A argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
2N/A free(program);
2N/A exit(1);
2N/A }
2N/A
2N/A return 0;
2N/A}