jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
12KB

  1. /*
  2. Copyright (C) 2006-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include "JackConstants.h"
  19. #include "JackTools.h"
  20. #include "JackError.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #ifdef WIN32
  24. #include <process.h>
  25. #endif
  26. using namespace std;
  27. namespace Jack {
  28. #define DEFAULT_TMP_DIR "/tmp"
  29. char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
  30. int JackTools::GetPID() {
  31. #ifdef WIN32
  32. return _getpid();
  33. #else
  34. return getpid();
  35. #endif
  36. }
  37. int JackTools::GetUID() {
  38. #ifdef WIN32
  39. return _getpid();
  40. //#error "No getuid function available"
  41. #else
  42. return getuid();
  43. #endif
  44. }
  45. const char* JackTools::DefaultServerName() {
  46. const char* server_name;
  47. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  48. server_name = JACK_DEFAULT_SERVER_NAME;
  49. return server_name;
  50. }
  51. /* returns the name of the per-user subdirectory of jack_tmpdir */
  52. #ifdef WIN32
  53. char* JackTools::UserDir() {
  54. return "";
  55. }
  56. char* JackTools::ServerDir(const char* server_name, char* server_dir) {
  57. return "";
  58. }
  59. void JackTools::CleanupFiles(const char* server_name) {}
  60. int JackTools::GetTmpdir() {
  61. return 0;
  62. }
  63. #else
  64. char* JackTools::UserDir() {
  65. static char user_dir[PATH_MAX + 1] = "";
  66. /* format the path name on the first call */
  67. if (user_dir[0] == '\0') {
  68. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  69. snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
  70. } else {
  71. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
  72. }
  73. }
  74. return user_dir;
  75. }
  76. /* returns the name of the per-server subdirectory of jack_user_dir() */
  77. char* JackTools::ServerDir(const char* server_name, char* server_dir) {
  78. /* format the path name into the suppled server_dir char array,
  79. * assuming that server_dir is at least as large as PATH_MAX+1 */
  80. snprintf(server_dir, PATH_MAX + 1, "%s/%s", UserDir(), server_name);
  81. return server_dir;
  82. }
  83. void JackTools::CleanupFiles(const char* server_name) {
  84. DIR* dir;
  85. struct dirent *dirent;
  86. char dir_name[PATH_MAX + 1] = "";
  87. ServerDir(server_name, dir_name);
  88. /* On termination, we remove all files that jackd creates so
  89. * subsequent attempts to start jackd will not believe that an
  90. * instance is already running. If the server crashes or is
  91. * terminated with SIGKILL, this is not possible. So, cleanup
  92. * is also attempted when jackd starts.
  93. *
  94. * There are several tricky issues. First, the previous JACK
  95. * server may have run for a different user ID, so its files
  96. * may be inaccessible. This is handled by using a separate
  97. * JACK_TMP_DIR subdirectory for each user. Second, there may
  98. * be other servers running with different names. Each gets
  99. * its own subdirectory within the per-user directory. The
  100. * current process has already registered as `server_name', so
  101. * we know there is no other server actively using that name.
  102. */
  103. /* nothing to do if the server directory does not exist */
  104. if ((dir = opendir(dir_name)) == NULL) {
  105. return;
  106. }
  107. /* unlink all the files in this directory, they are mine */
  108. while ((dirent = readdir(dir)) != NULL) {
  109. char fullpath[PATH_MAX + 1];
  110. if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
  111. continue;
  112. }
  113. snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
  114. if (unlink(fullpath)) {
  115. jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
  116. }
  117. }
  118. closedir(dir);
  119. /* now, delete the per-server subdirectory, itself */
  120. if (rmdir(dir_name)) {
  121. jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
  122. }
  123. /* finally, delete the per-user subdirectory, if empty */
  124. if (rmdir(UserDir())) {
  125. if (errno != ENOTEMPTY) {
  126. jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
  127. }
  128. }
  129. }
  130. int JackTools::GetTmpdir() {
  131. FILE* in;
  132. size_t len;
  133. char buf[PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  134. if ((in = popen("jackd -l", "r")) == NULL) {
  135. return -1;
  136. }
  137. if (fgets(buf, sizeof(buf), in) == NULL) {
  138. fclose(in);
  139. return -1;
  140. }
  141. len = strlen(buf);
  142. if (buf[len - 1] != '\n') {
  143. /* didn't get a whole line */
  144. fclose(in);
  145. return -1;
  146. }
  147. jack_tmpdir = (char *)malloc(len);
  148. memcpy(jack_tmpdir, buf, len - 1);
  149. jack_tmpdir[len - 1] = '\0';
  150. fclose(in);
  151. return 0;
  152. }
  153. #endif
  154. void JackTools::RewriteName(const char* name, char* new_name) {
  155. size_t i;
  156. for (i = 0; i < strlen(name); i++) {
  157. if ((name[i] == '/') || (name[i] == '\\'))
  158. new_name[i] = '_';
  159. else
  160. new_name[i] = name[i];
  161. }
  162. new_name[i] = '\0';
  163. }
  164. // class JackArgParser ***************************************************
  165. JackArgParser::JackArgParser ( const char* arg )
  166. {
  167. jack_log ( "JackArgParser::JackArgParser, arg_string : '%s'", arg );
  168. fArgc = 0;
  169. //if empty string
  170. if ( strlen(arg) == 0 )
  171. return;
  172. fArgString = string(arg);
  173. //else parse the arg string
  174. const size_t arg_len = fArgString.length();
  175. unsigned int i = 0;
  176. size_t pos = 0;
  177. size_t start = 0;
  178. size_t copy_start = 0;
  179. size_t copy_length = 0;
  180. //we need a 'space terminated' string
  181. fArgString += " ";
  182. //first fill a vector with args
  183. do {
  184. //find the first non-space character from the actual position
  185. start = fArgString.find_first_not_of ( ' ', start );
  186. //get the next quote or space position
  187. pos = fArgString.find_first_of ( " \"" , start );
  188. //no more quotes or spaces, consider the end of the string
  189. if ( pos == string::npos )
  190. pos = arg_len;
  191. //if double quote
  192. if ( fArgString[pos] == '\"' ) {
  193. //first character : copy the substring
  194. if ( pos == start ) {
  195. copy_start = start + 1;
  196. pos = fArgString.find ( '\"', ++pos );
  197. copy_length = pos - copy_start;
  198. start = pos + 1;
  199. }
  200. //else there is someting before the quote, first copy that
  201. else {
  202. copy_start = start;
  203. copy_length = pos - copy_start;
  204. start = pos;
  205. }
  206. }
  207. //if space
  208. if ( fArgString[pos] == ' ' ) {
  209. //short option descriptor
  210. if ( ( fArgString[start] == '-' ) && ( fArgString[start + 1] != '-' ) ) {
  211. copy_start = start;
  212. copy_length = 2;
  213. start += copy_length;
  214. }
  215. //else copy all the space delimitated string
  216. else {
  217. copy_start = start;
  218. copy_length = pos - copy_start;
  219. start = pos + 1;
  220. }
  221. }
  222. //then push the substring to the args vector
  223. fArgv.push_back ( fArgString.substr ( copy_start, copy_length ) );
  224. jack_log ( "JackArgParser::JackArgParser, add : '%s'", (*fArgv.rbegin()).c_str() );
  225. } while ( start < arg_len );
  226. //finally count the options
  227. for ( i = 0; i < fArgv.size(); i++ )
  228. if ( fArgv[i].at(0) == '-' )
  229. fArgc++;
  230. }
  231. JackArgParser::~JackArgParser()
  232. {}
  233. string JackArgParser::GetArgString()
  234. {
  235. return fArgString;
  236. }
  237. int JackArgParser::GetNumArgv()
  238. {
  239. return fArgv.size();
  240. }
  241. int JackArgParser::GetArgc()
  242. {
  243. return fArgc;
  244. }
  245. int JackArgParser::GetArgv ( vector<string>& argv )
  246. {
  247. argv = fArgv;
  248. return 0;
  249. }
  250. int JackArgParser::GetArgv ( char** argv )
  251. {
  252. //argv must be NULL
  253. if ( argv )
  254. return -1;
  255. //else allocate and fill it
  256. argv = (char**)calloc (fArgv.size(), sizeof(char*));
  257. for ( unsigned int i = 0; i < fArgv.size(); i++ )
  258. {
  259. argv[i] = (char*)calloc(fArgv[i].length(), sizeof(char));
  260. fill_n ( argv[i], fArgv[i].length() + 1, 0 );
  261. fArgv[i].copy ( argv[i], fArgv[i].length() );
  262. }
  263. return 0;
  264. }
  265. void JackArgParser::DeleteArgv ( const char** argv )
  266. {
  267. unsigned int i;
  268. for ( i = 0; i < fArgv.size(); i++ )
  269. free((void*)argv[i]);
  270. free((void*)argv);
  271. }
  272. int JackArgParser::ParseParams ( jack_driver_desc_t* desc, JSList** param_list )
  273. {
  274. string options_list;
  275. unsigned long i = 0;
  276. unsigned int param = 0;
  277. size_t param_id = 0;
  278. JSList* params = NULL;
  279. jack_driver_param_t* intclient_param;
  280. for ( i = 0; i < desc->nparams; i++ )
  281. options_list += desc->params[i].character;
  282. for ( param = 0; param < fArgv.size(); param++ )
  283. {
  284. if ( fArgv[param][0] == '-' )
  285. {
  286. //valid option
  287. if ( ( param_id = options_list.find_first_of ( fArgv[param].at(1) ) ) != string::npos )
  288. {
  289. //TODO : find if (and where) it's correctly deleted...
  290. intclient_param = new jack_driver_param_t;
  291. intclient_param->character = desc->params[param_id].character;
  292. switch ( desc->params[param_id].type )
  293. {
  294. case JackDriverParamInt:
  295. intclient_param->value.i = atoi ( fArgv[param + 1].c_str() );
  296. break;
  297. case JackDriverParamUInt:
  298. intclient_param->value.ui = strtoul ( fArgv[param + 1].c_str(), NULL, 10 );
  299. break;
  300. case JackDriverParamChar:
  301. intclient_param->value.c = fArgv[param + 1][0];
  302. break;
  303. case JackDriverParamString:
  304. fArgv[param + 1].copy ( intclient_param->value.str, min(static_cast<int>(fArgv[param + 1].length()), JACK_DRIVER_PARAM_STRING_MAX) );
  305. break;
  306. case JackDriverParamBool:
  307. if ( ( fArgv[param + 1].compare ( "false" ) == 0 ) ||
  308. ( fArgv[param + 1].compare ( "off" ) == 0 ) ||
  309. ( fArgv[param + 1].compare ( "no" ) == 0 ) ||
  310. ( fArgv[param + 1].compare ( "0" ) == 0 ) ||
  311. ( fArgv[param + 1].compare ( "(null)" ) == 0 ) )
  312. intclient_param->value.i = false;
  313. else
  314. intclient_param->value.i = true;
  315. break;
  316. }
  317. //add to the list
  318. params = jack_slist_append ( params, intclient_param );
  319. }
  320. //invalid option
  321. else
  322. jack_error ( "Invalid option '%c'", fArgv[param][1] );
  323. }
  324. }
  325. if ( param_list )
  326. *param_list = params;
  327. return 0;
  328. }
  329. }