Beispiel #1
0
int main(int argc, char *argv[]) {
    int maxword = 6;
    struct tnode *root, *dummy;
    char word[MAXWORD];    
    root = NULL;
    
    if (argc > 1)
        maxword = abs(atoi(argv[1]));

    while (getword(word, maxword) != EOF)
        if (strlen(word) == maxword && isalpha(word[0]))
            root = addtree(root, word);

    if ((argc > 2) && (strcmp(argv[2], "-d") == 0)) {
        dummy = talloc();
        dummy->word = strdupl("");
        dummy->count = 0;
        dummy->print = 0;
        dummy->left = dummy->right = NULL;

        while (printed < units) {
            d = dummy;
            treeprint(root, 1);
            printf("%4d %s\n", d->count, d->word);
            d->print = 1;
            printed++;
        }
    }

    else if ((argc > 2) && (strcmp(argv[2], "-r") == 0)) {
        dummy = talloc();
        dummy->word = strdupl("");
        dummy->count = 100;
        dummy->print = 0;
        dummy->left = dummy->right = NULL;
        
        while (printed < units) {
            d = dummy;
            treeprint(root, 2);
            printf("%4d %s\n", d->count, d->word);
            d->print = 1;
            printed++;
        }
    }

    else
        treeprint(root, 0);

    return 0;
}
Beispiel #2
0
struct tnode *newnode(char *w) 
{
	struct tnode *p = talloc();
 
	p -> word = strdupl(w);
	p -> count = 1;
 
	p -> left = p -> right = NULL;
	return p;
}
int main()
try {
    // test new duplicate function
    char a[] = "test";
    char* a_dpl = strdupl(a,5);
    print_str(a_dpl);
    cout << "\n";
    char b[] = { 't','e','s','t' };
    char* b_dpl = strdupl(b,4);
    print_str(b_dpl);
    cout << "\n";
    delete[] b_dpl;
    delete[] a_dpl;

    // test new find substring function
    char s[] = "xxxtestxxx";
    char x[] = "test";
    char* sub = findx(s,x);
    print_str(sub);
    cout << "\n";
    char s2[] = { 'x','x','t','e','s','t','x','x' };
    char x2[] = { 't','e','s','t' };
    char* sub2 = findx(s2,x2,8,4);
    print_str(sub2,6);
    cout << "\n";

    // test new comparison function
    char str1[] = "atest";
    char str2[] = "btest";
    cout << str1 << " to " << str2 << " is " << strcomp(str1,str2) << "\n";
    char str3[] = { 't','e','s','t','1' };
    char str4[] = { 't','e','s','t','1', 'a' };
    cout << str3 << " to " << str4 << " is " << strcomp(str3,str4,5,6) << "\n";
}
catch (exception& e) {
    cerr << "exception: " << e.what() << endl;
}
catch (...) {
    cerr << "exception\n";
}
Beispiel #4
0
char *
get_gid_name (gid_t gid)
{
#   if (defined (DOES_UID))
    static struct gids {                /*  Table of cache'd gids            */
        gid_t id;
        char  *name;
    } cache [GID_CACHE_MAX];
    static int
    cache_size = 0,                 /*  Number of gid's in cache         */
    cache_oldest = 0;               /*  Oldest entry in cache            */
    int
    cache_scan;                     /*  Scan through cache               */
    struct group
        *group_entry;

    /*  First, look for gid in cache                                         */
    for (cache_scan = 0; cache_scan < cache_size; cache_scan++)
        if (cache [cache_scan].id == gid)
            return (cache [cache_scan].name);

    /*  Add new name to cache: if cache was full, kick-out oldest entry      */
    if (cache_size == GID_CACHE_MAX)
    {
        cache_scan = cache_oldest++;
        cache_oldest %= GID_CACHE_MAX;
        free (cache [cache_scan].name);
    }
    else
        cache_scan = cache_size++;

    cache [cache_scan].id = gid;
#   if (defined (__VMS__))
    cache [cache_scan].name = "<none>";
#   else
    if ((group_entry = getgrgid (gid)) == NULL)
        cache [cache_scan].name = "<none>";
    else
        cache [cache_scan].name = strdupl (group_entry-> gr_name);
#   endif

    return (cache [cache_scan].name);

#   elif (defined (__MSDOS__))
    return (gid == 0? "group": "<none>");

#   endif
}
Beispiel #5
0
char *
get_uid_name (uid_t uid)
{
#   if (defined (DOES_UID))
    static struct uids {                /*  Table of cached uids             */
        uid_t id;
        char  *name;
    } cache [UID_CACHE_MAX];
    static int
    cache_size = 0,                 /*  Number of uid's in cache         */
    cache_oldest = 0;               /*  Oldest entry in cache            */
    int
    cache_scan;                     /*  Scan through cache               */
    struct passwd
        *passwd_entry;

    /*  First, look for uid in cache                                         */
    for (cache_scan = 0; cache_scan < cache_size; cache_scan++)
        if (cache [cache_scan].id == uid)
            return (cache [cache_scan].name);

    /*  Add new name to cache: if cache was full, kick-out oldest entry      */
    if (cache_size == UID_CACHE_MAX)
    {
        cache_scan = cache_oldest++;
        cache_oldest %= UID_CACHE_MAX;
        free (cache [cache_scan].name);
    }
    else
        cache_scan = cache_size++;

    cache [cache_scan].id = uid;
    if ((passwd_entry = getpwuid (uid)) == NULL)
        cache [cache_scan].name = "<none>";
    else
        cache [cache_scan].name = strdupl (passwd_entry-> pw_name);

    return (cache [cache_scan].name);

#   elif (defined (__MSDOS__))
    return (uid == 0? "user": "******");

#   endif
}
Beispiel #6
0
///adds node to binary tree. checks whether word is already in tree and increments it.
///if not it creates a node to the left node or right node of the current root depeding on the word
struct node *addnode(struct node *p, char *w)
{
    int cond;

    if (p == NULL)
    {
        p = talloc();
        p->word = strdupl(w);
        p->count = 1;
        p->left = p->right = NULL;
    }
    else if ((cond = strcmp(w, p->word)) == 0)
        p->count++;
    else if (cond < 0)
        p->left = addnode(p->left, w);
    else
        p->right = addnode(p->right, w);
    return p;
}
Beispiel #7
0
int main() {
    
    // test exercise 1
    char s[] = "Test";
    char* s_cpy = strdupl(s);
    print_str(s_cpy);
    cout << "\n";
    delete[] s_cpy;
    
    // test exercise 2
    char s2[] = "This is a test phrase";
    char x[] = "test";
    cout << "s: " << s2 << "\n";
    cout << "x: " << x << "\n";
    print_str(findx(s2,x));
    cout << "\n";
    
    // test exercise 3
    const int max = 5;
    char a[max];
    char b[max];
    while (cin>>a>>b) {
        int comp = strcmp(a,b);
        switch (comp) {
            case -1:
                cout << a << " < " << b << "\n";
                break;
            case 0:
                cout << a << " == " << b << "\n";
                break;
            case 1:
                cout << a << " > " << b << "\n";
                break;
            default:
                error("invalid result returned by strcmp");
                break;
        }
    }
}
Beispiel #8
0
int
main (int argc, char *argv [])
{
    int
        argn;                           /*  Argument number                  */
    Bool
        args_ok = TRUE,                 /*  Were the arguments okay?         */
        quiet_mode = FALSE;             /*  -q means suppress messages       */
    char
        *port,                          /*  Port to connect to               */
        **argparm;                      /*  Argument parameter to pick-up    */
    static char
        command [LINE_MAX];             /*  Command to pass to agent         */

    /*  These are the arguments we may get on the command line               */
    port       = NULL;
    strclr (command);

    argparm = NULL;                     /*  Argument parameter to pick-up    */
    for (argn = 1; argn < argc; argn++)
      {
        /*  If argparm is set, we have to collect an argument parameter      */
        if (argparm)
          {
            if (*argv [argn] != '-')    /*  Parameter can't start with '-'   */
              {
                *argparm = strdupl (argv [argn]);
                argparm = NULL;
              }
            else
              {
                args_ok = FALSE;
                break;
              }
          }
        else
        if (*argv [argn] == '-')
          {
            switch (argv [argn][1])
              {
                /*  These switches take a parameter                          */
                case 'p':
                    argparm = &port; break;

                /*  These switches have an immediate effect                  */
                case 'q':
                    quiet_mode = TRUE;
                    break;
                case 't':
                    smtsock_trace (TRUE);
                    break;
                case 'v':
                    coprintf ("Syscli %s", SYSCLI_VERSION);
                    coprintf (PRODUCT);
                    coprintf (BUILDMODEL);
                    coprintf (COPYRIGHT);
                    coprintf ("Built on: %s", BUILDDATE);
                    exit (EXIT_SUCCESS);
                case 'h':
                    coprintf ("Syscli %s", SYSCLI_VERSION);
                    coprintf (COPYRIGHT);
                    coprintf (USAGE);
                    exit (EXIT_SUCCESS);

                /*  Anything else is an error                                */
                default:
                    args_ok = FALSE;
              }
          }
        else
          {
            strcat (command, " ");
            strcat (command, argv [argn]);
          }
      }

    /*  If there was a missing parameter or an argument error, quit          */
    if (argparm)
      {
        puts ("Argument missing - type 'syscli -h' for help");
        exit (EXIT_FAILURE);
      }
    else
    if (!args_ok)
      {
        puts ("Invalid arguments - type 'syscli -h' for help");
        exit (EXIT_FAILURE);
      }
    
    /*  Handle the remaining arguments we got                                */
    if (!port)
        port = SYSMAN_DEFAULT_PORT;
    if (quiet_mode)
      {
        fclose (stdout);                /*  Kill standard output             */
        fclose (stderr);                /*   and standard error              */
      }
    else
      {
        puts ("Syscli " SYSCLI_VERSION);
        puts (COPYRIGHT);
      }

    smt_init ();                        /*  Initialise SMT kernel            */
    sysclia_init (command, port);       /*  Initialise SYSMAN client agent   */
    smt_exec_full ();                   /*  Run until completed              */
    smt_term ();                        /*  Shut-down SMT kernel             */
    mem_assert ();                      /*  Assert memory is clean           */
    return (0);
}
Beispiel #9
0
int
main (int argc, char *argv [])
{
    int
        argn;                           /*  Argument number                  */
    Bool
        args_ok = TRUE,                 /*  Were the arguments okay?         */
        quiet_mode = FALSE,             /*  -q means suppress messages       */
        background = FALSE;             /*  -s means run in background       */
    char
        *workdir,                       /*  Working directory                */
        *port,                          /*  Value for listen port            */
        **argparm;                      /*  Argument parameter to pick-up    */

    /*  First off, switch to user's id                                       */
    set_uid_user ();

    /*  These are the arguments we may get on the command line               */
    workdir    = NULL;
    port       = NULL;

    argparm = NULL;                     /*  Argument parameter to pick-up    */
    for (argn = 1; argn < argc; argn++)
      {
        /*  If argparm is set, we have to collect an argument parameter      */
        if (argparm)
          {
            if (*argv [argn] != '-')    /*  Parameter can't start with '-'   */
              {
                *argparm = strdupl (argv [argn]);
                argparm = NULL;
              }
            else
              {
                args_ok = FALSE;
                break;
              }
          }
        else
        if (*argv [argn] == '-')
          {
            switch (argv [argn][1])
              {
                /*  These switches take a parameter                          */
                case 'w':
                    argparm = &workdir;  break;
                case 'p':
                    argparm = &port; break;

                /*  These switches have an immediate effect                  */
                case 'q':
                    quiet_mode = TRUE;
                    break;
                case 's':
                    background = TRUE;
                    break;
                case 'S':
                    background = FALSE;
                    break;
                case 't':
                    smtsock_trace (TRUE);
                    break;
                case 'v':
                    coprintf ("Sysman %s", SYSMAN_VERSION);
                    coprintf (PRODUCT);
                    coprintf (BUILDMODEL);
                    coprintf (COPYRIGHT);
                    coprintf ("Built on: %s", BUILDDATE);
                    exit (EXIT_SUCCESS);
                case 'h':
                    coprintf ("Sysman %s", SYSMAN_VERSION);
                    coprintf (COPYRIGHT);
                    coprintf (USAGE);
                    exit (EXIT_SUCCESS);

                /*  Anything else is an error                                */
                default:
                    args_ok = FALSE;
              }
          }
        else
          {
            args_ok = FALSE;
            break;
          }
      }

    /*  If there was a missing parameter or an argument error, quit          */
    if (argparm)
      {
        puts ("Argument missing - type 'sysman -h' for help");
        exit (EXIT_FAILURE);
      }
    else
    if (!args_ok)
      {
        puts ("Invalid arguments - type 'sysman -h' for help");
        exit (EXIT_FAILURE);
      }
      
    /*  Set server working directory if necessary                            */
    if (workdir
    &&  set_curdir (workdir))
      {
        printf ("Can't work in '%s' - %s\n", workdir, strerror (errno));
        exit (EXIT_FAILURE);
      }
    
    /*  Handle the remaining arguments we got                                */
    if (!port)
        port = SYSMAN_DEFAULT_PORT;
    if (quiet_mode)
      {
        fclose (stdout);                /*  Kill standard output             */
        fclose (stderr);                /*   and standard error              */
      }
    else
      {
        puts ("Sysman " SYSMAN_VERSION);
        puts (COPYRIGHT);
      }

    if (background)
      {
        const char
           *background_args [] = { "-s", NULL };

        puts ("Moving into the background");
        if (process_server (NULL, NULL, argc, argv, background_args) != 0)
          {
            puts ("Backgrounding failed.  Giving up.");
            exit (EXIT_FAILURE);
          }
      }

    smt_init ();                        /*  Initialise SMT kernel            */
    if (sysmana_init (port) == 0)       /*  Initialise SYSMAN agent          */
        smt_exec_full ();               /*  Run until completed              */
    else
        printf ("Initialisation error\n");
    smt_term ();                        /*  Shut-down SMT kernel             */

    mem_assert ();

    return (EXIT_SUCCESS);
}
Beispiel #10
0
static int getnrfont(FILE *fp)	/* read the nroff description file */
{
	Chwid chtemp[NCHARS];
	static Chwid chinit;
	int i, nw, n, wid, code, type;
	char buf[100], ch[100], s1[100], s2[100];
	wchar_t wc;

	code = 0;
	chinit.wid = 1;
	chinit.str = "";
	for (i = 0; i < ALPHABET; i++) {
		chtemp[i] = chinit;	/* zero out to begin with */
		chtemp[i].num = chtemp[i].code = i;	/* every alphabetic character is itself */
		chtemp[i].wid = 1;	/* default ascii widths */
	}
	skipline(fp);
	nw = ALPHABET;
	while (fgets(buf, sizeof buf, fp) != NULL) {
		sscanf(buf, "%s %s %[^\n]", ch, s1, s2);
		if (!eq(s1, "\"")) {	/* genuine new character */
			sscanf(s1, "%d", &wid);
		} /* else it's a synonym for prev character, */
			/* so leave previous values intact */

		/* decide what kind of alphabet it might come from */

		if (strlen(ch) == 1) {	/* it's ascii */
			n = ch[0];	/* origin includes non-graphics */
			chtemp[n].num = ch[0];
		} else if (ch[0] == '\\' && ch[1] == '0') {
			n = strtol(ch+1, 0, 0);	/* \0octal or \0xhex */
			chtemp[n].num = n;
#ifdef UNICODE
		} else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
			chtemp[nw].num = chadd(ch, MBchar, Install);
			n = nw;
			nw++;
#endif	/*UNICODE*/
		} else {
			if (strcmp(ch, "---") == 0) { /* no name */
				sprintf(ch, "%d", code);
				type = Number;
			} else
				type = Troffchar;
/* BUG in here somewhere when same character occurs twice in table */
			chtemp[nw].num = chadd(ch, type, Install);
			n = nw;
			nw++;
		}
		chtemp[n].wid = wid;
		chtemp[n].str = strdupl(parse(s2, Type));
	}
	t.tfont.nchars = nw;
	t.tfont.wp = (Chwid *) malloc(nw * sizeof(Chwid));
	if (t.tfont.wp == NULL)
		return -1;
	for (i = 0; i < nw; i++)
		t.tfont.wp[i] = chtemp[i];
	return 1;
}
Beispiel #11
0
void n_ptinit(void)
{
	int i;
	char *p;
	char opt[50], cmd[100];
	FILE *fp;

	hmot = n_hmot;
	makem = n_makem;
	setabs = n_setabs;
	setch = n_setch;
	sethl = n_sethl;
	setht = n_setht;
	setslant = n_setslant;
	vmot = n_vmot;
	xlss = n_xlss;
	findft = n_findft;
	width = n_width;
	mchbits = n_mchbits;
	ptlead = n_ptlead;
	ptout = n_ptout;
	ptpause = n_ptpause;
	setfont = n_setfont;
	setps = n_setps;
	setwd = n_setwd;

	if ((p = getenv("NROFFTERM")) != 0)
		strcpy(devname, p);
	if (termtab[0] == 0)
		strcpy(termtab,DWBntermdir);
	if (fontdir[0] == 0)
		strcpy(fontdir, "");
	if (devname[0] == 0)
		strcpy(devname, NDEVNAME);
	pl = 11*INCH;
	po = PO;
	hyf = 0;
	ascii = 1;
	lg = 0;
	fontlab[1] = 'R';
	fontlab[2] = 'I';
	fontlab[3] = 'B';
	fontlab[4] = PAIR('B','I');
	fontlab[5] = 'D';
	bdtab[3] = 3;
	bdtab[4] = 3;

	/* hyphalg = 0;	/* for testing */

	strcat(termtab, devname);
	if ((fp = fopen(unsharp(termtab), "r")) == NULL) {
		ERROR "cannot open %s", termtab WARN;
		exit(-1);
	}


/* this loop isn't robust about input format errors. */
/* it assumes  name, name-value pairs..., charset */
/* god help us if we get out of sync. */

	fscanf(fp, "%s", cmd);	/* should be device name... */
	if (!is(devname) && trace)
		ERROR "wrong terminal name: saw %s, wanted %s", cmd, devname WARN;
	for (;;) {
		fscanf(fp, "%s", cmd);
		if (is("charset"))
			break;
		fscanf(fp, " %[^\n]", opt);
		if (is("bset")) t.bset = atoi(opt);
		else if (is("breset")) t.breset = atoi(opt);
		else if (is("Hor")) t.Hor = atoi(opt);
		else if (is("Vert")) t.Vert = atoi(opt);
		else if (is("Newline")) t.Newline = atoi(opt);
		else if (is("Char")) t.Char = atoi(opt);
		else if (is("Em")) t.Em = atoi(opt);
		else if (is("Halfline")) t.Halfline = atoi(opt);
		else if (is("Adj")) t.Adj = atoi(opt);
		else if (is("twinit")) t.twinit = strdupl(parse(opt, Notype));
		else if (is("twrest")) t.twrest = strdupl(parse(opt, Notype));
		else if (is("twnl")) t.twnl = strdupl(parse(opt, Notype));
		else if (is("hlr")) t.hlr = strdupl(parse(opt, Notype));
		else if (is("hlf")) t.hlf = strdupl(parse(opt, Notype));
		else if (is("flr")) t.flr = strdupl(parse(opt, Notype));
		else if (is("bdon")) t.bdon = strdupl(parse(opt, Notype));
		else if (is("bdoff")) t.bdoff = strdupl(parse(opt, Notype));
		else if (is("iton")) t.iton = strdupl(parse(opt, Notype));
		else if (is("itoff")) t.itoff = strdupl(parse(opt, Notype));
		else if (is("ploton")) t.ploton = strdupl(parse(opt, Notype));
		else if (is("plotoff")) t.plotoff = strdupl(parse(opt, Notype));
		else if (is("up")) t.up = strdupl(parse(opt, Notype));
		else if (is("down")) t.down = strdupl(parse(opt, Notype));
		else if (is("right")) t.right = strdupl(parse(opt, Notype));
		else if (is("left")) t.left = strdupl(parse(opt, Notype));
		else
			ERROR "bad tab.%s file, %s %s", devname, cmd, opt WARN;
	}

	getnrfont(fp);
	fclose(fp);

	sps = EM;
	ics = EM * 2;
	dtab = 8 * t.Em;
	for (i = 0; i < 16; i++)
		tabtab[i] = dtab * (i + 1);
	pl = 11 * INCH;
	po = PO;
	spacesz = SS;
	lss = lss1 = VS;
	ll = ll1 = lt = lt1 = LL;
	smnt = nfonts = 5;	/* R I B BI S */
	n_specnames();	/* install names like "hyphen", etc. */
	if (eqflg)
		t.Adj = t.Hor;
}
Beispiel #12
0
int
main (int argc, char *argv [])
{
    int
        argn;                           /*  Argument number                  */
    Bool
        args_ok = TRUE,                 /*  Were the arguments okay?         */
        quiet_mode = FALSE;             /*  -q means suppress messages       */
    char
        *workdir,                       /*  Working directory                */
        *rootdir,                       /*  Default root directory           */
        *cgidir,                        /*  CGI program directory            */
        *ftproot,                       /*  Default FTP root directory       */
        *portbase,                      /*  Value for IP portbase            */
        *background,                    /*  -s means run in background       */
        **argparm;                      /*  Argument parameter to pick-up    */

    /*  First off, switch to user's id                                       */
    set_uid_user ();

    /*  These are the arguments we may get on the command line               */
    workdir    = NULL;
    rootdir    = NULL;
    cgidir     = NULL;
    portbase   = NULL;
    background = NULL;
    ftproot    = NULL;

    argparm = NULL;                     /*  Argument parameter to pick-up    */
    for (argn = 1; argn < argc; argn++)
      {
        /*  If argparm is set, we have to collect an argument parameter      */
        if (argparm)
          {
            if (*argv [argn] != '-')    /*  Parameter can't start with '-'   */
              {
                *argparm = strdupl (argv [argn]);
                argparm = NULL;
              }
            else
              {
                args_ok = FALSE;
                break;
              }
          }
        else
        if (*argv [argn] == '-')
          {
            switch (argv [argn][1])
              {
                /*  These switches take a parameter                          */
                case 'w':
                    argparm = &workdir;  break;
                case 'r':
                    argparm = &rootdir;  break;
                case 'c':
                    argparm = &cgidir;   break;
                case 'b':
                    argparm = &portbase; break;
                case 'f':
                    argparm = &ftproot;  break;

                /*  These switches have an immediate effect                  */
                case 'q':
                    quiet_mode = TRUE;
                    break;
                case 's':
                    background = "1";
                    break;
                case 'S':
                    background = "0";
                    break;
                case 't':
                    smtsock_trace (TRUE);
                    break;
                case 'v':
                    coprintf (PRODUCT);
                    coprintf (BUILDMODEL);
                    coprintf (COPYRIGHT);
                    coprintf ("Built on: %s", BUILDDATE);
                    exit (EXIT_SUCCESS);
                case 'h':
                    coprintf (SERVER_NAME);
                    coprintf (COPYRIGHT);
                    coprintf (USAGE);
                    exit (EXIT_SUCCESS);

                /*  Anything else is an error                                */
                default:
                    args_ok = FALSE;
              }
          }
        else
          {
            args_ok = FALSE;
            break;
          }
      }

    /*  If there was a missing parameter or an argument error, quit          */
    if (argparm)
      {
        puts ("Argument missing - type 'xitami -h' for help");
        exit (EXIT_FAILURE);
      }
    else
    if (!args_ok)
      {
        puts ("Invalid arguments - type 'xitami -h' for help");
        exit (EXIT_FAILURE);
      }
      
    /*  Set server working directory if necessary                            */
    if (workdir
    &&  set_curdir (workdir))
      {
        printf ("Can't work in '%s' - %s\n", workdir, strerror (errno));
        exit (EXIT_FAILURE);
      }

    /*  Load configuration data, if any, into the config_table               */
    config = ini_dyn_load (NULL, "xitami.cfg");
    ini_dyn_load (config, CONFIG ("server:defaults"));

    /*  Initialise arguments, taking defaults from the config_table          */
    if (!rootdir)
        rootdir    = CONFIG ("server:webpages");
    if (!cgidir)
        cgidir     = CONFIG ("server:cgi-bin");
    if (!portbase)
        portbase   = CONFIG ("server:portbase");
    if (!background)
        background = CONFIG ("server:background");
    if (!ftproot)
        ftproot    = CONFIG ("ftp:root");

    /*  Now, handle the remaining arguments we got                           */
    ip_portbase = atoi (portbase);
    if (quiet_mode)
      {
        fclose (stdout);                /*  Kill standard output             */
        fclose (stderr);                /*   and standard error              */
      }
    else
      {
        puts (SERVER_NAME);
        puts (COPYRIGHT);
      }

    if (*background == '1')
      {
        const char
           *background_args [] = { "-s", NULL };

        puts ("Moving into the background");
        if (process_server (NULL, NULL, argc, argv, background_args) != 0)
          {
            puts ("Backgrounding failed.  Giving up.");
            exit (EXIT_FAILURE);
          }
      }

    /*  Initialise the SMT kernel                                            */
    smt_init ();
    server_name = "Xitami";

    /*  Load the agents we want to use                                       */
    if (*CONFIG ("lrwp:enabled") == '1')
        xilrwp_init ();                 /*  LRWP service agent               */
    if (*CONFIG ("security:admin") == '1')
        xiadmin_init ();                /*  Administration agent             */
    if (*CONFIG ("server:supervisor") == '1')
        xisuper_init ();                /*  Supervisor agent                 */
    xierror_init ();                    /*  Error-simulation agent           */
    xiredir_init ();                    /*  Redirection agent                */
    xiddns_init ();                     /*  Dynamic DNS registration         */
    xiimap_init ();                     /*  Image mapping agent              */
    xixlog_init ();                     /*  Extended logging agent           */
    xixssi_init ();                     /*  Internal SSI processor           */
    xixxml_init ();                     /*  Internal XML processor           */
    smthttp_init (rootdir, cgidir);     /*  HTTP agent, required             */
    smtftpc_init (ftproot);             /*  FTP service agent                */
    smtpipe_init (CONFIG ("server:pipedef"));  /*  Transfer pipe agent       */

    smt_exec_full ();                   /*  Run SMT until completed          */
    smt_term ();

    /*  Deallocate configuration symbol table                                */
    sym_delete_table (config);

    /*  Check that all memory was cleanly released                           */
    mem_assert ();

    return (EXIT_SUCCESS);
}
Beispiel #13
0
/*  ---------------------------------------------------------------------[<]-
    Function: service_start

    Synopsis: Main routine for xitami web server. The service stops when
    server_stop_event is signaled.
    ---------------------------------------------------------------------[>]-*/
static void
service_start (int argc, char **argv)
{
    int
        argn;                           /*  Argument number                  */
    Bool
        args_ok     = TRUE,             /*  Were the arguments okay?         */
        quite_mode  = FALSE;            /*  -q means suppress all output     */
    char
        **argparm = NULL;               /*  Argument parameter to pick-up    */
    DWORD
        wait;

    argparm = NULL;
    for (argn = 1; argn < argc; argn++)
      {
        /*  If argparm is set, we have to collect an argument parameter      */
        if (argparm)
          {
            if (*argv [argn] != '-')    /*  Parameter can't start with '-'   */
              {
                free (*argparm);
                *argparm = strdupl (argv [argn]);
                argparm = NULL;
              }
            else
              {
                args_ok = FALSE;
                break;
              }
          }
        else
        if (*argv [argn] == '-')
          {
            switch (argv [argn][1])
              {
                /*  These switches have an immediate effect                  */
                case 'q':
                    quite_mode = TRUE;
                    break;
                /*  Used only for service                                    */
                case 'i':
                case 'd':
                case 'u':
                case 'v':
                case 'h':
                    break;
                /*  Anything else is an error                                */
                default:
                    args_ok = FALSE;
              }
          }
        else
          {
            args_ok = FALSE;
            break;
          }
      }


    /*  If there was a missing parameter or an argument error, quit          */
    if (argparm)
      {
        add_to_message_log (
            strprintf (
                "Argument missing - type '%s -h' for help",
                application_name));
        return;
      }
    else
    if (!args_ok)
      {
        add_to_message_log (
            strprintf (
                "Invalid arguments - type '%s -h' for help",
                application_name));
        return;
      }
    /* Service initialization                                                */

    /* Report the status to the service control manager.                     */
    if (!report_status (
            SERVICE_START_PENDING,      /* Service state                     */
            NO_ERROR,                   /* Exit code                         */
            3000))                      /* wait hint                         */
        return;

    /* Create the event object. The control handler function signals         */
    /* this event when it receives the "stop" control code.                  */
    server_stop_event = CreateEvent (
                            NULL,       /* no security attributes            */
                            TRUE,       /* manual reset event                */
                            FALSE,      /* not-signalled                     */
                            NULL);      /* no name                           */

    if (server_stop_event == NULL)
        return;

    /* report the status to the service control manager.                     */
    if (!report_status (
            SERVICE_START_PENDING,      /* Service state                     */
            NO_ERROR,                   /* Exit code                         */
            3000))                      /* wait hint                         */
      {
        CloseHandle (server_stop_event);
        return;
      }
    if (quite_mode)
      {
        fclose (stdout);                /*  Kill standard output             */
        fclose (stderr);                /*   and standard error              */
      }
    /*  Report the status to the service control manager.                    */
    if (!report_status (SERVICE_RUNNING, NO_ERROR, 0))                
      {
        CloseHandle (server_stop_event);
        return;
      }


    smt_init ();
    if (smt_agents_init_fct != NULL)
        control_break = (*smt_agents_init_fct)(application_config);

    while (!control_break)
      {
        FOREVER 
          {
            __try 
              {
                if (!smt_exec_step ())  /*  Run just one step                */
                  {
                    control_break = TRUE;
                    break;
                  }
              }
            __except (report_smt_error ()) 
              {
                /*  Fatal error handling                                     */
                smt_term  ();           /*  Shut-down SMT kernel             */
                mem_freeall ();         /*  Free ALL allocated memory        */
                break;                  /*  We'll recover control            */
              }
            wait = WaitForSingleObject (server_stop_event, 0);
            if (wait != WAIT_TIMEOUT)
                smt_shutdown ();        /*  Shut down the HTTP server        */
          }
        if (control_break)
            break;
      }

    if (smt_agents_term_fct != NULL)
        (*smt_agents_term_fct)();
    smt_term ();

    CloseHandle (server_stop_event);

    free_resources ();
}