Coverity: Core module errors fixed
[scilab.git] / scilab / modules / core / src / cpp / storeCommand.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 /*--------------------------------------------------------------------------*/
16 extern "C"
17 {
18 #include "storeCommand.h"
19 }
20
21 #include "parser.hxx"
22 #include "tasks.hxx"
23 #include "scilabWrite.hxx"
24 #include "scilabexception.hxx"
25 #include "localization.hxx"
26 #include "runner.hxx"
27 #include "threadmanagement.hxx"
28
29 /*--------------------------------------------------------------------------*/
30 /*
31  *  Command queue functions
32  *  This function is used to store Scilab command in a queue
33  *
34  *  PUBLIC : int StoreCommand( char *command)
35  *           int StoreConsoleCommand(char *command)
36  *           int StorePrioritaryCommand(char *command)
37  *           int C2F(ismenu)()
38  *           int C2F(getmen)(char * btn_cmd,int * lb, int * entry)
39  */
40 /*--------------------------------------------------------------------------*/
41 struct CommandRec
42 {
43     char*   m_command;              /* command info one string two integers */
44     int     m_isInterruptible;      /* 1 if the command execution can be interrupted */
45     int     m_isPrioritary;         /* 1 if the command is prioritary */
46     command_origin_t     m_iCommandOrigin;       /* Indicate who have stored the command (ie: console, tcl) */
47     CommandRec(char* command, int isInterruptible, int isPrioritary, command_origin_t iCmdOrigin) :
48         m_command(command), m_isInterruptible(isInterruptible), m_isPrioritary(isPrioritary), m_iCommandOrigin(iCmdOrigin) {}
49 };
50 /*--------------------------------------------------------------------------*/
51 static std::list<CommandRec> commandQueue;
52 static std::list<CommandRec> commandQueuePrioritary;
53 /*--------------------------------------------------------------------------*/
54 int StoreCommandWithFlags(const char* command, int iPrioritary, int iInterruptible, command_origin_t iCmdOrigin)
55 {
56     ThreadManagement::LockStoreCommand();
57     if (iPrioritary)
58     {
59         commandQueuePrioritary.emplace_back(os_strdup(command), iPrioritary, iInterruptible, iCmdOrigin);
60
61         // Awake Runner to execute this prioritary command
62         ThreadManagement::SendAwakeRunnerSignal();
63     }
64     else
65     {
66         commandQueue.emplace_back(os_strdup(command), iPrioritary, iInterruptible, iCmdOrigin);
67     }
68
69     ThreadManagement::UnlockStoreCommand();
70     // Awake Scilab to execute a new command
71     ThreadManagement::SendCommandStoredSignal();
72
73     return 0;
74 }
75
76 int StoreCommand(const char *command)
77 {
78     ThreadManagement::LockStoreCommand();
79     commandQueue.emplace_back(os_strdup(command),
80                               /*is prioritary*/ 0,
81                               /* is interruptible*/ 1,
82                               /* cmd origin */ NONE);
83
84     ThreadManagement::UnlockStoreCommand();
85     // Awake Scilab to execute a new command
86     ThreadManagement::SendCommandStoredSignal();
87
88     return 0;
89 }
90
91 int StoreConsoleCommand(const char *command, int iWaitFor)
92 {
93     ThreadManagement::LockStoreCommand();
94     commandQueuePrioritary.emplace_back(os_strdup(command),
95                                         /*is prioritary*/ 1,
96                                         /* is interruptible*/ 1,
97                                         /* cmd origin */ CONSOLE);
98
99     // Awake Scilab to execute a new command
100     ThreadManagement::SendCommandStoredSignal();
101     // Awake Runner to execute this prioritary command
102     ThreadManagement::SendAwakeRunnerSignal();
103
104     if (iWaitFor)
105     {
106         // make this wait before unlock the Store Command will prevent
107         // dead lock in case where an other thread get this command
108         // and execute it before this thread is waiting for.
109         ThreadManagement::WaitForConsoleExecDoneSignal();
110     }
111     else
112     {
113         ThreadManagement::UnlockStoreCommand();
114     }
115
116     return 0;
117 }
118
119 int StorePrioritaryCommand(const char *command)
120 {
121     ThreadManagement::LockStoreCommand();
122     commandQueuePrioritary.emplace_back(os_strdup(command),
123                                         /*is prioritary*/ 1,
124                                         /* is interruptible*/ 0,
125                                         /* cmd origin */ NONE);
126
127     // Awake Scilab to execute a new command
128     ThreadManagement::SendCommandStoredSignal();
129     // Awake Runner to execute this prioritary command
130     ThreadManagement::SendAwakeRunnerSignal();
131
132     ThreadManagement::UnlockStoreCommand();
133
134     return 0;
135 }
136
137 int isEmptyCommandQueue(void)
138 {
139     return (commandQueuePrioritary.empty() && commandQueue.empty());
140 }
141
142 /*
143  * Gets the next command to execute
144  * and remove it from the queue
145  */
146 int GetCommand(char** cmd, int* piPrioritary, int* piInterruptible, command_origin_t* piCmdOrigin)
147 {
148     int iCommandReturned = 0;
149
150     ThreadManagement::LockStoreCommand();
151     if (commandQueuePrioritary.empty() == false)
152     {
153         *cmd = os_strdup(commandQueuePrioritary.front().m_command);
154         *piInterruptible = commandQueuePrioritary.front().m_isInterruptible;
155         *piPrioritary = commandQueuePrioritary.front().m_isPrioritary;
156         *piCmdOrigin = commandQueuePrioritary.front().m_iCommandOrigin;
157
158         FREE (commandQueuePrioritary.front().m_command);
159         commandQueuePrioritary.pop_front();
160
161         iCommandReturned = 1;
162     }
163     else if (commandQueue.empty() == false)
164     {
165         *cmd = os_strdup(commandQueue.front().m_command);
166         *piInterruptible = commandQueue.front().m_isInterruptible;
167         *piPrioritary = commandQueue.front().m_isPrioritary;
168         *piCmdOrigin = commandQueue.front().m_iCommandOrigin;
169
170         FREE (commandQueue.front().m_command);
171         commandQueue.pop_front();
172
173         iCommandReturned = 1;
174     }
175     ThreadManagement::UnlockStoreCommand();
176
177     return iCommandReturned;
178 }
179
180 /*--------------------------------------------------------------------------*/
181 int ismenu(void)
182 {
183     //#pragma message("WARNING : ismenu is deprecated. It will be removed _BEFORE_ Scilab 6.0.")
184     // FIXME : Do not forget to remove me.
185     return 0;
186 }
187 /*--------------------------------------------------------------------------*/