Changeset 2642 for neercs/trunk
- Timestamp:
- Aug 2, 2008, 1:32:35 PM (15 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/main.c
r2641 r2642 80 80 { 81 81 struct screen_list *screen_list = NULL; 82 struct passwd *user_info;83 char *user_path = NULL, *user_dir = NULL;84 82 int i, args, s=0; 85 83 long long unsigned int last_key_time = 0; 86 84 int mainret = -1; 87 int attach = 0, forceattach = 0; 88 int *to_grab = NULL; 89 char **to_start = NULL; 90 int nb_to_grab = 0; 85 91 86 92 87 screen_list = create_screen_list(); … … 103 98 args = 1; 104 99 105 /* Build local config file path */ 106 user_dir = getenv("HOME"); 107 if(!user_dir) 108 { 109 user_info = getpwuid(getuid()); 110 if(user_info) 111 { 112 user_dir = user_info->pw_dir; 113 } 114 } 115 if(user_dir) 116 { 117 user_path = malloc(strlen(user_dir) + strlen("/.neercsrc") + 1); 118 sprintf(user_path, "%s/%s", user_dir, ".neercsrc"); 119 } 120 121 122 screen_list->recurrent_list = (struct recurrent_list*) malloc(sizeof(struct recurrent_list)); 123 screen_list->recurrent_list->recurrent = (struct recurrent**) malloc(sizeof(struct recurrent*)); 124 if(!screen_list->recurrent_list->recurrent) 125 { 126 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 127 goto end; 128 } 129 screen_list->recurrent_list->count = 0; 130 131 for(;;) 132 { 133 int option_index = 0; 134 int pidopt; 135 static struct myoption long_options[] = 136 { 137 { "config", 1, NULL, 'c' }, 138 #if defined USE_GRAB 139 { "pid", 1, NULL, 'P' }, 140 #endif 141 { "help", 0, NULL, 'h' }, 142 { "version", 0, NULL, 'v' }, 143 }; 144 #if defined USE_GRAB 145 int c = mygetopt(argc, argv, "c:S:R::r::P:hv", long_options, &option_index); 146 #else 147 int c = mygetopt(argc, argv, "c:S:R::r::hv", long_options, &option_index); 148 #endif 149 if(c == -1) 150 break; 151 152 switch(c) 153 { 154 case 'c': /* --config */ 155 if(user_path) 156 free(user_path); 157 user_path = strdup(myoptarg); 158 s+=2; 159 break; 160 case 'S': 161 if(!screen_list->session_name) 162 screen_list->session_name = strdup(myoptarg); 163 s+=2; 164 break; 165 case 'P': /* --pid */ 166 pidopt = atoi(myoptarg); 167 if(pidopt <= 0) 168 { 169 fprintf(stderr, "Invalid pid %d\n", pidopt); 170 if(to_grab) 171 free(to_grab); 172 return -1; 173 } 174 if(!to_grab) 175 { 176 /* At most argc-1-s times -P <pid> + final 0 */ 177 to_grab = (int *)malloc(((argc-1-s)/2+1)*sizeof(int)); 178 if(!to_grab) 179 { 180 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 181 goto end; 182 } 183 } 184 to_grab[nb_to_grab++] = pidopt; 185 to_grab[nb_to_grab] = 0; 186 s+=2; 187 break; 188 case 'r': 189 forceattach = 1; 190 case 'R': 191 if(attach) 192 { 193 fprintf(stderr, "Attaching can only be requested once\n"); 194 goto end; 195 } 196 if(myoptarg) 197 { 198 if(screen_list->session_name) 199 free(screen_list->session_name); 200 screen_list->session_name = strdup(myoptarg); 201 s+=1; 202 } 203 attach = 1; 204 s+=1; 205 break; 206 case 'h': /* --help */ 207 usage(argc, argv); 208 mainret = 0; 209 goto end; 210 break; 211 case 'v': /* --version */ 212 version(); 213 mainret = 0; 214 goto end; 215 break; 216 default: 217 fprintf(stderr, "Unknown argument #%d\n", myoptind); 218 goto end; 219 break; 220 } 221 } 100 101 s = handle_command_line(argc, argv, screen_list); 102 if(s<0) goto end; 222 103 223 104 if(s < argc - 1) 224 105 { 225 to_start = (char**)malloc((argc-s)*sizeof(char*));226 if(! to_start)106 screen_list->to_start = (char**)malloc((argc-s)*sizeof(char*)); 107 if(!screen_list->to_start) 227 108 { 228 109 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); … … 231 112 for(i=0; i<(argc-1) - s; i++) 232 113 { 233 to_start[i] = strdup(argv[i+s+1]);234 } 235 to_start[argc-1-s] = NULL;114 screen_list->to_start[i] = strdup(argv[i+s+1]); 115 } 116 screen_list->to_start[argc-1-s] = NULL; 236 117 } 237 118 … … 240 121 241 122 /* Then local one */ 242 if( user_path)243 { 244 read_configuration_file( user_path, screen_list);245 free( user_path);246 } 247 248 if( attach)123 if(screen_list->user_path) 124 { 125 read_configuration_file(screen_list->user_path, screen_list); 126 free(screen_list->user_path); 127 } 128 129 if(screen_list->attach) 249 130 { 250 131 char **sockets; 251 if( nb_to_grab || (argc-1 > s))132 if(screen_list->nb_to_grab || (argc-1 > s)) 252 133 { 253 134 fprintf(stderr, "-R can not be associated with commands or pids!\n"); … … 293 174 free(screen_list->socket_path[SOCK_SERVER]); 294 175 screen_list->socket_path[SOCK_SERVER] = NULL; 295 attach = 0;176 screen_list->attach = 0; 296 177 } 297 178 for(i=0; sockets[i]; i++) … … 302 183 { 303 184 fprintf(stderr, "No socket found!\n"); 304 attach = 0;305 } 306 if( forceattach && !attach)185 screen_list->attach = 0; 186 } 187 if(screen_list->forceattach && !screen_list->attach) 307 188 goto end; 308 189 } … … 330 211 331 212 /* Fork the server if needed */ 332 if(! attach)333 { 334 if(start_server( to_grab,to_start, screen_list))213 if(!screen_list->attach) 214 { 215 if(start_server(screen_list->to_grab, screen_list->to_start, screen_list)) 335 216 goto end; 336 217 } … … 344 225 ssize_t n; 345 226 char buf[128*1024]; 346 227 if(!screen_list) goto end; 347 228 if (screen_list->socket[SOCK_CLIENT] && (n = read(screen_list->socket[SOCK_CLIENT], buf, sizeof(buf)-1)) > 0) 348 229 { … … 458 339 459 340 struct screen_list *screen_list = NULL; 341 struct passwd *user_info; 342 char *user_dir = NULL; 460 343 461 344 /* Create screen list */ … … 496 379 screen_list->session_name = NULL; 497 380 screen_list->default_shell = NULL; 498 381 screen_list->user_path = NULL; 499 382 screen_list->autolock_timeout = -1; 383 screen_list->to_grab = NULL; 384 screen_list->to_start = NULL; 385 screen_list->nb_to_grab = 0; 386 screen_list->attach = 0; 387 screen_list->forceattach = 0; 500 388 501 389 … … 507 395 memset(screen_list->lockpass, 0, 1024); 508 396 397 398 399 /* Build local config file path */ 400 user_dir = getenv("HOME"); 401 if(!user_dir) 402 { 403 user_info = getpwuid(getuid()); 404 if(user_info) 405 { 406 user_dir = user_info->pw_dir; 407 } 408 } 409 if(user_dir) 410 { 411 screen_list->user_path = malloc(strlen(user_dir) + strlen("/.neercsrc") + 1); 412 sprintf(screen_list->user_path, "%s/%s", user_dir, ".neercsrc"); 413 } 414 415 416 screen_list->recurrent_list = (struct recurrent_list*) malloc(sizeof(struct recurrent_list)); 417 screen_list->recurrent_list->recurrent = (struct recurrent**) malloc(sizeof(struct recurrent*)); 418 if(!screen_list->recurrent_list->recurrent) 419 { 420 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 421 free(screen_list); 422 free(screen_list->screen); 423 return NULL; 424 } 425 screen_list->recurrent_list->count = 0; 426 427 428 509 429 return screen_list; 510 430 } 431 432 433 434 int handle_command_line(int argc, char *argv[], struct screen_list *screen_list) 435 { 436 int s = 0; 437 for(;;) 438 { 439 int option_index = 0; 440 int pidopt; 441 static struct myoption long_options[] = 442 { 443 { "config", 1, NULL, 'c' }, 444 #if defined USE_GRAB 445 { "pid", 1, NULL, 'P' }, 446 #endif 447 { "help", 0, NULL, 'h' }, 448 { "version", 0, NULL, 'v' }, 449 }; 450 #if defined USE_GRAB 451 int c = mygetopt(argc, argv, "c:S:R::r::P:hv", long_options, &option_index); 452 #else 453 int c = mygetopt(argc, argv, "c:S:R::r::hv", long_options, &option_index); 454 #endif 455 if(c == -1) 456 break; 457 458 switch(c) 459 { 460 case 'c': /* --config */ 461 if(screen_list->user_path) 462 free(screen_list->user_path); 463 screen_list->user_path = strdup(myoptarg); 464 s+=2; 465 break; 466 case 'S': 467 if(!screen_list->session_name) 468 screen_list->session_name = strdup(myoptarg); 469 s+=2; 470 break; 471 case 'P': /* --pid */ 472 pidopt = atoi(myoptarg); 473 if(pidopt <= 0) 474 { 475 fprintf(stderr, "Invalid pid %d\n", pidopt); 476 if(screen_list->to_grab) 477 free(screen_list->to_grab); 478 return -1; 479 } 480 if(!screen_list->to_grab) 481 { 482 /* At most argc-1-s times -P <pid> + final 0 */ 483 screen_list->to_grab = (int *)malloc(((argc-1-s)/2+1)*sizeof(int)); 484 if(!screen_list->to_grab) 485 { 486 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 487 return -1; 488 } 489 } 490 screen_list->to_grab[screen_list->nb_to_grab++] = pidopt; 491 screen_list->to_grab[screen_list->nb_to_grab] = 0; 492 s+=2; 493 break; 494 case 'r': 495 screen_list->forceattach = 1; 496 case 'R': 497 if(screen_list->attach) 498 { 499 fprintf(stderr, "Attaching can only be requested once\n"); 500 return -1; 501 } 502 if(myoptarg) 503 { 504 if(screen_list->session_name) 505 free(screen_list->session_name); 506 screen_list->session_name = strdup(myoptarg); 507 s+=1; 508 } 509 screen_list->attach = 1; 510 s+=1; 511 break; 512 case 'h': /* --help */ 513 usage(argc, argv); 514 return -1; 515 break; 516 case 'v': /* --version */ 517 version(); 518 return -1; 519 break; 520 default: 521 fprintf(stderr, "Unknown argument #%d\n", myoptind); 522 return -1; 523 break; 524 } 525 } 526 return s; 527 } -
neercs/trunk/src/neercs.h
r2641 r2642 182 182 struct screen **screen; /* Windows */ 183 183 184 /* Option parsing and configuration */ 184 185 struct option *config; 185 186 char *default_shell; 186 187 struct recurrent_list *recurrent_list; 188 char *user_path; 189 int *to_grab; 190 char **to_start; 191 int nb_to_grab; 192 int attach, forceattach; 187 193 188 194 cucul_canvas_t *cv; … … 210 216 void usage(int argc, char **argv); 211 217 218 int handle_command_line(int argc, char *argv[], struct screen_list *screen_list); 212 219 213 220 struct screen_list *create_screen_list(void); -
neercs/trunk/src/server.c
r2641 r2642 388 388 if(screen_list->recurrent_list) free(screen_list->recurrent_list); 389 389 390 if(screen_list->session_name) 390 if(screen_list->session_name) { 391 391 free(screen_list->session_name); 392 393 if(screen_list) 392 screen_list->session_name = NULL; 393 } 394 395 if(screen_list) { 394 396 free(screen_list); 397 screen_list = NULL; 398 } 395 399 396 400 return mainret;
Note: See TracChangeset
for help on using the changeset viewer.