167754Smsmith/******************************************************************************
267754Smsmith *
367754Smsmith * Module Name: dbhistry - debugger HISTORY command
467754Smsmith *
567754Smsmith *****************************************************************************/
667754Smsmith
7217365Sjkim/*
8245582Sjkim * Copyright (C) 2000 - 2013, Intel Corp.
970243Smsmith * All rights reserved.
1067754Smsmith *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
2567754Smsmith *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
2967754Smsmith *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
4367754Smsmith
4467754Smsmith
45193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
46193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
47193341Sjkim#include <contrib/dev/acpica/include/acdebug.h>
4867754Smsmith
49102550Siwasaki#ifdef ACPI_DEBUGGER
5067754Smsmith
51102550Siwasaki#define _COMPONENT          ACPI_CA_DEBUGGER
5291116Smsmith        ACPI_MODULE_NAME    ("dbhistry")
5367754Smsmith
5467754Smsmith
5567754Smsmith#define HI_NO_HISTORY       0
5667754Smsmith#define HI_RECORD_HISTORY   1
57250838Sjkim#define HISTORY_SIZE        40
5867754Smsmith
5967754Smsmith
6067754Smsmithtypedef struct HistoryInfo
6167754Smsmith{
62250838Sjkim    char                    *Command;
6367754Smsmith    UINT32                  CmdNum;
6467754Smsmith
6567754Smsmith} HISTORY_INFO;
6667754Smsmith
6767754Smsmith
6899679Siwasakistatic HISTORY_INFO         AcpiGbl_HistoryBuffer[HISTORY_SIZE];
6999679Siwasakistatic UINT16               AcpiGbl_LoHistory = 0;
7099679Siwasakistatic UINT16               AcpiGbl_NumHistory = 0;
7199679Siwasakistatic UINT16               AcpiGbl_NextHistoryIndex = 0;
7299679Siwasakistatic UINT32               AcpiGbl_NextCmdNum = 1;
7367754Smsmith
7467754Smsmith
7567754Smsmith/*******************************************************************************
7667754Smsmith *
7767754Smsmith * FUNCTION:    AcpiDbAddToHistory
7867754Smsmith *
7967754Smsmith * PARAMETERS:  CommandLine     - Command to add
8067754Smsmith *
8167754Smsmith * RETURN:      None
8267754Smsmith *
8367754Smsmith * DESCRIPTION: Add a command line to the history buffer.
8467754Smsmith *
8567754Smsmith ******************************************************************************/
8667754Smsmith
8767754Smsmithvoid
8867754SmsmithAcpiDbAddToHistory (
89114237Snjl    char                    *CommandLine)
9067754Smsmith{
91250838Sjkim    UINT16                  CmdLen;
92250838Sjkim    UINT16                  BufferLen;
9367754Smsmith
9467754Smsmith    /* Put command into the next available slot */
9567754Smsmith
96250838Sjkim    CmdLen = (UINT16) ACPI_STRLEN (CommandLine);
97250838Sjkim    if (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command != NULL)
98250838Sjkim    {
99250838Sjkim        BufferLen = (UINT16) ACPI_STRLEN (
100250838Sjkim            AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command);
101250838Sjkim        if (CmdLen > BufferLen)
102250838Sjkim        {
103250838Sjkim            AcpiOsFree (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].
104250838Sjkim                Command);
105250838Sjkim            AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
106250838Sjkim                AcpiOsAllocate (CmdLen + 1);
107250838Sjkim        }
108250838Sjkim    }
109250838Sjkim    else
110250838Sjkim    {
111250838Sjkim        AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
112250838Sjkim            AcpiOsAllocate (CmdLen + 1);
113250838Sjkim    }
114250838Sjkim
115151937Sjkim    ACPI_STRCPY (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command,
116151937Sjkim        CommandLine);
11767754Smsmith
118250838Sjkim    AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].CmdNum =
119250838Sjkim        AcpiGbl_NextCmdNum;
12083174Smsmith
12167754Smsmith    /* Adjust indexes */
12267754Smsmith
12383174Smsmith    if ((AcpiGbl_NumHistory == HISTORY_SIZE) &&
12483174Smsmith        (AcpiGbl_NextHistoryIndex == AcpiGbl_LoHistory))
12567754Smsmith    {
12683174Smsmith        AcpiGbl_LoHistory++;
12783174Smsmith        if (AcpiGbl_LoHistory >= HISTORY_SIZE)
12867754Smsmith        {
12983174Smsmith            AcpiGbl_LoHistory = 0;
13067754Smsmith        }
13167754Smsmith    }
13267754Smsmith
13383174Smsmith    AcpiGbl_NextHistoryIndex++;
13483174Smsmith    if (AcpiGbl_NextHistoryIndex >= HISTORY_SIZE)
13567754Smsmith    {
13683174Smsmith        AcpiGbl_NextHistoryIndex = 0;
13767754Smsmith    }
13867754Smsmith
13983174Smsmith    AcpiGbl_NextCmdNum++;
14083174Smsmith    if (AcpiGbl_NumHistory < HISTORY_SIZE)
14167754Smsmith    {
14283174Smsmith        AcpiGbl_NumHistory++;
14367754Smsmith    }
14467754Smsmith}
14567754Smsmith
14667754Smsmith
14767754Smsmith/*******************************************************************************
14867754Smsmith *
14967754Smsmith * FUNCTION:    AcpiDbDisplayHistory
15067754Smsmith *
15167754Smsmith * PARAMETERS:  None
15267754Smsmith *
15367754Smsmith * RETURN:      None
15467754Smsmith *
15567754Smsmith * DESCRIPTION: Display the contents of the history buffer
15667754Smsmith *
15767754Smsmith ******************************************************************************/
15867754Smsmith
15967754Smsmithvoid
160151937SjkimAcpiDbDisplayHistory (
161151937Sjkim    void)
16267754Smsmith{
163193267Sjkim    UINT32                  i;
16467754Smsmith    UINT16                  HistoryIndex;
16567754Smsmith
16667754Smsmith
16783174Smsmith    HistoryIndex = AcpiGbl_LoHistory;
16867754Smsmith
16967754Smsmith    /* Dump entire history buffer */
17067754Smsmith
17183174Smsmith    for (i = 0; i < AcpiGbl_NumHistory; i++)
17267754Smsmith    {
173250838Sjkim        if (AcpiGbl_HistoryBuffer[HistoryIndex].Command)
174250838Sjkim        {
175250838Sjkim            AcpiOsPrintf ("%3ld  %s\n",
176250838Sjkim                AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum,
177250838Sjkim                AcpiGbl_HistoryBuffer[HistoryIndex].Command);
178250838Sjkim        }
17967754Smsmith
18067754Smsmith        HistoryIndex++;
18167754Smsmith        if (HistoryIndex >= HISTORY_SIZE)
18267754Smsmith        {
18367754Smsmith            HistoryIndex = 0;
18467754Smsmith        }
18567754Smsmith    }
18667754Smsmith}
18767754Smsmith
18867754Smsmith
18967754Smsmith/*******************************************************************************
19067754Smsmith *
19167754Smsmith * FUNCTION:    AcpiDbGetFromHistory
19267754Smsmith *
19367754Smsmith * PARAMETERS:  CommandNumArg           - String containing the number of the
19467754Smsmith *                                        command to be retrieved
19567754Smsmith *
196151937Sjkim * RETURN:      Pointer to the retrieved command. Null on error.
19767754Smsmith *
19867754Smsmith * DESCRIPTION: Get a command from the history buffer
19967754Smsmith *
20067754Smsmith ******************************************************************************/
20167754Smsmith
202114237Snjlchar *
20367754SmsmithAcpiDbGetFromHistory (
204114237Snjl    char                    *CommandNumArg)
20567754Smsmith{
206193267Sjkim    UINT32                  i;
20767754Smsmith    UINT16                  HistoryIndex;
20867754Smsmith    UINT32                  CmdNum;
20967754Smsmith
21067754Smsmith
21167754Smsmith    if (CommandNumArg == NULL)
21267754Smsmith    {
21383174Smsmith        CmdNum = AcpiGbl_NextCmdNum - 1;
21467754Smsmith    }
21567754Smsmith
21667754Smsmith    else
21767754Smsmith    {
21891116Smsmith        CmdNum = ACPI_STRTOUL (CommandNumArg, NULL, 0);
21967754Smsmith    }
22067754Smsmith
22167754Smsmith    /* Search history buffer */
22267754Smsmith
22383174Smsmith    HistoryIndex = AcpiGbl_LoHistory;
22483174Smsmith    for (i = 0; i < AcpiGbl_NumHistory; i++)
22567754Smsmith    {
22683174Smsmith        if (AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum == CmdNum)
22767754Smsmith        {
228250838Sjkim            /* Found the command, return it */
22967754Smsmith
23083174Smsmith            return (AcpiGbl_HistoryBuffer[HistoryIndex].Command);
23167754Smsmith        }
23267754Smsmith
23367754Smsmith
23467754Smsmith        HistoryIndex++;
23567754Smsmith        if (HistoryIndex >= HISTORY_SIZE)
23667754Smsmith        {
23767754Smsmith            HistoryIndex = 0;
23867754Smsmith        }
23967754Smsmith    }
24067754Smsmith
241209746Sjkim    AcpiOsPrintf ("Invalid history number: %u\n", HistoryIndex);
24267754Smsmith    return (NULL);
24367754Smsmith}
24467754Smsmith
245102550Siwasaki#endif /* ACPI_DEBUGGER */
246