Changeset 2488 for neercs


Ignore:
Timestamp:
Jun 25, 2008, 12:33:40 AM (12 years ago)
Author:
Pascal Terjan
Message:
  • Have -r/-R/-S like screen
Location:
neercs/trunk/src
Files:
3 edited

Legend:

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

    r2483 r2488  
    1414#include "neercs.h"
    1515
    16 static char * build_socket_path(char *socket_dir)
     16static char * build_socket_path(char *socket_dir, char *session_name)
    1717{
    1818    char *path, *dir;
     
    2626        dir = "/tmp";
    2727    if(path)
    28         snprintf(path, PATH_MAX+1, "%s/neercs.%d.sock", dir, getpid());
     28        snprintf(path, PATH_MAX+1, "%s/neercs.%s.sock", dir, session_name);
    2929    return path;
    3030}
     
    4646
    4747    myaddr.sun_family = AF_UNIX;
    48     screen_list->socket_path = build_socket_path(screen_list->socket_path);
     48    screen_list->socket_path = build_socket_path(screen_list->socket_path, screen_list->session_name);
    4949    strncpy(myaddr.sun_path, screen_list->socket_path, sizeof(myaddr.sun_path) - 1);
    5050    unlink(myaddr.sun_path);
     
    159159}
    160160
    161 char ** list_sockets(char *socket_dir)
     161char ** list_sockets(char *socket_dir, char *session_name)
    162162{
    163163    char *pattern, *dir;
     
    179179        return globbuf.gl_pathv;
    180180
    181     snprintf(pattern, PATH_MAX, "%s/neercs.*.sock", dir);
     181    if(session_name && strlen(session_name)+strlen(dir)+13<PATH_MAX)
     182        sprintf(pattern, "%s/neercs.%s.sock", dir, session_name);
     183    else
     184        snprintf(pattern, PATH_MAX, "%s/neercs.*.sock", dir);
    182185    pattern[PATH_MAX] = '\0';
    183186
  • neercs/trunk/src/main.c

    r2487 r2488  
    6868    printf("Example : %s zsh top \n\n", argv[0]);
    6969    printf("Options :\n");
    70     printf("\t--config\t\t-c <file>\t\tuse given config file\n");
    71     printf("\t--pid\t\t-P <pid>\t\tattach <pid>\n");
    72     printf("\t\t\t-R\t\t\tre-attach another neercs\n");
     70    printf("\t--config\t-c <file>\t\tuse given config file\n");
     71    printf("\t--pid\t\t-P <pid>\t\tgrab <pid>\n");
     72    printf("\t\t\t-r [session]\t\treattach to a detached neercs\n");
     73    printf("\t\t\t-R [session]\t\treattach if possible, otherwise start a new session\n");
     74    printf("\t\t\t-S <name>\t\tname this session <name> instead of <pid>\n");
    7375    printf("\t--version\t-v \t\t\tdisplay version and exit\n");
    7476    printf("\t--help\t\t-h \t\t\tthis help\n");
     
    8284    struct recurrent_list *recurrent_list = NULL;
    8385    struct passwd *user_info;
    84     char *default_shell = NULL, *user_path = NULL;
     86    char *default_shell = NULL, *user_path = NULL, *session_name = NULL;
    8587    int i, w, h, args, s=0;
    8688    int eof = 0, refresh = 1, command = 0;
     
    8890    int lock_offset = 0;
    8991    int mainret = 0;
    90     int attach = 0;
     92    int attach = 0, forceattach = 0;
    9193    int *to_grab = NULL;
    9294    int nb_to_grab = 0;
     
    142144    screen_list->socket_dir = NULL;
    143145    screen_list->socket_path = NULL;
     146    screen_list->session_name = NULL;
    144147    memset(screen_list->lockmsg, 0, 1024);
    145148    memset(screen_list->lockpass, 0, 1024);
     
    168171            };
    169172#if defined USE_GRAB
    170         int c = mygetopt(argc, argv, "c:RP:hv", long_options, &option_index);
     173        int c = mygetopt(argc, argv, "c:S:R::r::P:hv", long_options, &option_index);
    171174#else
    172         int c = mygetopt(argc, argv, "c:Rhv", long_options, &option_index);
     175        int c = mygetopt(argc, argv, "c:S:R::r::hv", long_options, &option_index);
    173176#endif
    174177        if(c == -1)
     
    181184                free(user_path);
    182185            user_path = strdup(myoptarg);
     186            s+=2;
     187            break;
     188        case 'S':
     189            if(screen_list->session_name)
     190                free(screen_list->session_name);
     191            screen_list->session_name = strdup(myoptarg);
    183192            s+=2;
    184193            break;
     
    205214            s+=2;
    206215            break;
     216        case 'r':
     217            forceattach = 1;
    207218        case 'R':
     219            if(attach)
     220            {
     221                fprintf(stderr, "Attaching can only be requested once\n");
     222                return -1;
     223            }
     224            if(myoptarg)
     225            {
     226                session_name = strdup(myoptarg);
     227                s+=1;
     228            }
    208229            attach = 1;
    209230            s+=1;
     
    242263            return -1;
    243264        }
    244         sockets = list_sockets(screen_list->socket_dir);
     265        sockets = list_sockets(screen_list->socket_dir, session_name);
     266        if(!screen_list->session_name)
     267            screen_list->session_name = session_name;
     268        else
     269            free(session_name);
    245270        if(sockets && sockets[0])
    246271        {
     
    255280            fprintf(stderr, "No socket found!\n");
    256281        }
    257         return -1;
     282        if(forceattach)
     283            return -1;
     284    }
     285
     286    /* Build default session name */
     287    if(!screen_list->session_name)
     288    {
     289        char mypid[32]; /* FIXME Compute the length of PID_MAX ? */
     290        snprintf(mypid, 31, "%d", getpid());
     291        mypid[31]= '\0';
     292        screen_list->session_name = strdup(mypid);
     293        if(!screen_list->session_name)
     294        {
     295            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
     296            return -1;
     297        }
    258298    }
    259299
  • neercs/trunk/src/neercs.h

    r2478 r2488  
    7676    char *socket_path;           /* Socket to ask for attaching */
    7777    char *socket_dir;            /* Where to create the socket */
     78    char *session_name;          /* Name of the session */
    7879
    7980    /* Lock */
     
    138139int create_socket(struct screen_list* screen_list);
    139140int read_socket(struct screen_list* screen_list, cucul_canvas_t * cv, caca_display_t ** dp);
    140 char ** list_sockets(char *socket_dir);
     141char ** list_sockets(char *socket_dir, char *session_name);
    141142
    142143/* Screens management */
Note: See TracChangeset for help on using the changeset viewer.