Changeset 2642


Ignore:
Timestamp:
08/02/08 13:32:35 (5 years ago)
Author:
jylam
Message:
  • Moved most of the command line parsing to its own function
Location:
neercs/trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/main.c

    r2641 r2642  
    8080{ 
    8181    struct screen_list *screen_list = NULL; 
    82     struct passwd *user_info; 
    83     char *user_path = NULL, *user_dir = NULL; 
    8482    int i, args, s=0; 
    8583    long long unsigned int last_key_time = 0; 
    8684    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 
    9186 
    9287    screen_list = create_screen_list(); 
     
    10398        args = 1; 
    10499 
    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; 
    222103 
    223104    if(s < argc - 1) 
    224105    { 
    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) 
    227108        { 
    228109            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 
     
    231112        for(i=0; i<(argc-1) - s; i++) 
    232113        { 
    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; 
    236117    } 
    237118 
     
    240121 
    241122    /* 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) 
    249130    { 
    250131        char **sockets; 
    251         if(nb_to_grab || (argc-1 > s)) 
     132        if(screen_list->nb_to_grab || (argc-1 > s)) 
    252133        { 
    253134            fprintf(stderr, "-R can not be associated with commands or pids!\n"); 
     
    293174                free(screen_list->socket_path[SOCK_SERVER]); 
    294175                screen_list->socket_path[SOCK_SERVER] = NULL; 
    295                 attach = 0; 
     176                screen_list->attach = 0; 
    296177            } 
    297178            for(i=0; sockets[i]; i++) 
     
    302183        { 
    303184            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) 
    307188            goto end; 
    308189    } 
     
    330211 
    331212    /* 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)) 
    335216            goto end; 
    336217    } 
     
    344225        ssize_t n; 
    345226        char buf[128*1024]; 
    346  
     227        if(!screen_list) goto end; 
    347228        if (screen_list->socket[SOCK_CLIENT] && (n = read(screen_list->socket[SOCK_CLIENT], buf, sizeof(buf)-1)) > 0) 
    348229        { 
     
    458339 
    459340    struct screen_list *screen_list = NULL; 
     341    struct passwd *user_info; 
     342    char *user_dir = NULL; 
    460343 
    461344    /* Create screen list */ 
     
    496379    screen_list->session_name  = NULL; 
    497380    screen_list->default_shell = NULL; 
    498  
     381    screen_list->user_path     = NULL; 
    499382    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; 
    500388 
    501389 
     
    507395    memset(screen_list->lockpass, 0, 1024); 
    508396 
     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 
    509429    return screen_list; 
    510430} 
     431 
     432 
     433 
     434int 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  
    182182    struct screen **screen;      /* Windows */ 
    183183 
     184    /* Option parsing and configuration */ 
    184185    struct option *config; 
    185186    char *default_shell; 
    186187    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; 
    187193 
    188194    cucul_canvas_t *cv; 
     
    210216void usage(int argc, char **argv); 
    211217 
     218int handle_command_line(int argc, char *argv[], struct screen_list *screen_list); 
    212219 
    213220struct screen_list *create_screen_list(void); 
  • neercs/trunk/src/server.c

    r2641 r2642  
    388388    if(screen_list->recurrent_list)            free(screen_list->recurrent_list); 
    389389 
    390     if(screen_list->session_name) 
     390    if(screen_list->session_name) { 
    391391        free(screen_list->session_name); 
    392  
    393     if(screen_list) 
     392        screen_list->session_name = NULL; 
     393    } 
     394 
     395    if(screen_list) { 
    394396        free(screen_list); 
     397        screen_list = NULL; 
     398    } 
    395399 
    396400    return mainret; 
Note: See TracChangeset for help on using the changeset viewer.